draw_pt_fetch.c revision 7ddb925b8bc6c18eba953e34d2b630a3a6593f05
1/**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "pipe/p_util.h"
29#include "draw/draw_context.h"
30#include "draw/draw_private.h"
31#include "draw/draw_vbuf.h"
32#include "draw/draw_vertex.h"
33#include "draw/draw_pt.h"
34#include "translate/translate.h"
35#include "translate/translate_cache.h"
36
37
38struct pt_fetch {
39   struct draw_context *draw;
40
41   struct translate *translate;
42
43   unsigned vertex_size;
44   boolean need_edgeflags;
45
46   struct translate_cache *cache;
47};
48
49/* Perform the fetch from API vertex elements & vertex buffers, to a
50 * contiguous set of float[4] attributes as required for the
51 * vertex_shader->run_linear() method.
52 *
53 * This is used in all cases except pure passthrough
54 * (draw_pt_fetch_emit.c) which has its own version to translate
55 * directly to hw vertices.
56 *
57 */
58void draw_pt_fetch_prepare( struct pt_fetch *fetch,
59			    unsigned vertex_size )
60{
61   struct draw_context *draw = fetch->draw;
62   unsigned i, nr = 0;
63   unsigned dst_offset = 0;
64   struct translate_key key;
65
66   fetch->vertex_size = vertex_size;
67
68   /* Always emit/leave space for a vertex header.
69    *
70    * It's worth considering whether the vertex headers should contain
71    * a pointer to the 'data', rather than having it inline.
72    * Something to look at after we've fully switched over to the pt
73    * paths.
74    */
75   {
76      /* Need to set header->vertex_id = 0xffff somehow.
77       */
78      key.element[nr].input_format = PIPE_FORMAT_R32_FLOAT;
79      key.element[nr].input_buffer = draw->pt.nr_vertex_buffers;
80      key.element[nr].input_offset = 0;
81      key.element[nr].output_format = PIPE_FORMAT_R32_FLOAT;
82      key.element[nr].output_offset = dst_offset;
83      dst_offset += 1 * sizeof(float);
84      nr++;
85
86
87      /* Just leave the clip[] array untouched.
88       */
89      dst_offset += 4 * sizeof(float);
90   }
91
92
93   for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
94      key.element[nr].input_format = draw->pt.vertex_element[i].src_format;
95      key.element[nr].input_buffer = draw->pt.vertex_element[i].vertex_buffer_index;
96      key.element[nr].input_offset = draw->pt.vertex_element[i].src_offset;
97      key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
98      key.element[nr].output_offset = dst_offset;
99
100      dst_offset += 4 * sizeof(float);
101      nr++;
102   }
103
104   assert(dst_offset <= vertex_size);
105
106   key.nr_elements = nr;
107   key.output_stride = vertex_size;
108
109
110   if (!fetch->translate ||
111       translate_key_compare(&fetch->translate->key, &key) != 0)
112   {
113      translate_key_sanitize(&key);
114      fetch->translate = translate_cache_find(fetch->cache, &key);
115
116      {
117	 static struct vertex_header vh = { 0, 1, 0, 0xffff };
118	 fetch->translate->set_buffer(fetch->translate,
119				      draw->pt.nr_vertex_buffers,
120				      &vh,
121				      0);
122      }
123   }
124
125   fetch->need_edgeflags = ((draw->rasterizer->fill_cw != PIPE_POLYGON_MODE_FILL ||
126                             draw->rasterizer->fill_ccw != PIPE_POLYGON_MODE_FILL) &&
127                            draw->pt.user.edgeflag);
128}
129
130
131
132
133void draw_pt_fetch_run( struct pt_fetch *fetch,
134			const unsigned *elts,
135			unsigned count,
136			char *verts )
137{
138   struct draw_context *draw = fetch->draw;
139   struct translate *translate = fetch->translate;
140   unsigned i;
141
142   for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
143      translate->set_buffer(translate,
144			    i,
145			    ((char *)draw->pt.user.vbuffer[i] +
146			     draw->pt.vertex_buffer[i].buffer_offset),
147			    draw->pt.vertex_buffer[i].pitch );
148   }
149
150   translate->run_elts( translate,
151			elts,
152			count,
153			verts );
154
155   /* Edgeflags are hard to fit into a translate program, populate
156    * them separately if required.  In the setup above they are
157    * defaulted to one, so only need this if there is reason to change
158    * that default:
159    */
160   if (fetch->need_edgeflags) {
161      for (i = 0; i < count; i++) {
162         struct vertex_header *vh = (struct vertex_header *)(verts + i * fetch->vertex_size);
163         vh->edgeflag = draw_pt_get_edgeflag( draw, elts[i] );
164      }
165   }
166}
167
168
169void draw_pt_fetch_run_linear( struct pt_fetch *fetch,
170                               unsigned start,
171                               unsigned count,
172                               char *verts )
173{
174   struct draw_context *draw = fetch->draw;
175   struct translate *translate = fetch->translate;
176   unsigned i;
177
178   for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
179      translate->set_buffer(translate,
180			    i,
181			    ((char *)draw->pt.user.vbuffer[i] +
182			     draw->pt.vertex_buffer[i].buffer_offset),
183			    draw->pt.vertex_buffer[i].pitch );
184   }
185
186   translate->run( translate,
187                   start,
188                   count,
189                   verts );
190
191   /* Edgeflags are hard to fit into a translate program, populate
192    * them separately if required.  In the setup above they are
193    * defaulted to one, so only need this if there is reason to change
194    * that default:
195    */
196   if (fetch->need_edgeflags) {
197      for (i = 0; i < count; i++) {
198         struct vertex_header *vh = (struct vertex_header *)(verts + i * fetch->vertex_size);
199         vh->edgeflag = draw_pt_get_edgeflag( draw, start + i );
200      }
201   }
202}
203
204
205struct pt_fetch *draw_pt_fetch_create( struct draw_context *draw )
206{
207   struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
208   if (!fetch)
209      return NULL;
210
211   fetch->draw = draw;
212   fetch->cache = translate_cache_create();
213   if (!fetch->cache) {
214      FREE(fetch);
215      return NULL;
216   }
217
218   return fetch;
219}
220
221void draw_pt_fetch_destroy( struct pt_fetch *fetch )
222{
223   if (fetch->cache)
224      translate_cache_destroy(fetch->cache);
225
226   FREE(fetch);
227}
228
229