Merge branch 'glsl2-head' into glsl2
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_fetch_emit.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "util/u_memory.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_vbuf.h"
37 #include "draw/draw_vertex.h"
38 #include "draw/draw_pt.h"
39 #include "translate/translate.h"
40 #include "translate/translate_cache.h"
41
42 /* The simplest 'middle end' in the new vertex code.
43 *
44 * The responsibilities of a middle end are to:
45 * - perform vertex fetch using
46 * - draw vertex element/buffer state
47 * - a list of fetch indices we received as an input
48 * - run the vertex shader
49 * - cliptest,
50 * - clip coord calculation
51 * - viewport transformation
52 * - if necessary, run the primitive pipeline, passing it:
53 * - a linear array of vertex_header vertices constructed here
54 * - a set of draw indices we received as an input
55 * - otherwise, drive the hw backend,
56 * - allocate space for hardware format vertices
57 * - translate the vertex-shader output vertices to hw format
58 * - calling the backend draw functions.
59 *
60 * For convenience, we provide a helper function to drive the hardware
61 * backend given similar inputs to those required to run the pipeline.
62 *
63 * In the case of passthrough mode, many of these actions are disabled
64 * or noops, so we end up doing:
65 *
66 * - perform vertex fetch
67 * - drive the hw backend
68 *
69 * IE, basically just vertex fetch to post-vs-format vertices,
70 * followed by a call to the backend helper function.
71 */
72
73
74 struct fetch_emit_middle_end {
75 struct draw_pt_middle_end base;
76 struct draw_context *draw;
77
78 struct translate *translate;
79 const struct vertex_info *vinfo;
80
81 /* Cache point size somewhere it's address won't change:
82 */
83 float point_size;
84
85 struct translate_cache *cache;
86 };
87
88
89
90
91 static void fetch_emit_prepare( struct draw_pt_middle_end *middle,
92 unsigned prim,
93 unsigned out_prim,
94 unsigned opt,
95 unsigned *max_vertices )
96 {
97 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
98 struct draw_context *draw = feme->draw;
99 const struct vertex_info *vinfo;
100 unsigned i, dst_offset;
101 boolean ok;
102 struct translate_key key;
103
104
105 ok = draw->render->set_primitive( draw->render,
106 out_prim );
107 if (!ok) {
108 assert(0);
109 return;
110 }
111
112 /* Must do this after set_primitive() above:
113 */
114 vinfo = feme->vinfo = draw->render->get_vertex_info(draw->render);
115
116
117
118 /* Transform from API vertices to HW vertices, skipping the
119 * pipeline_vertex intermediate step.
120 */
121 dst_offset = 0;
122 memset(&key, 0, sizeof(key));
123
124 for (i = 0; i < vinfo->num_attribs; i++) {
125 const struct pipe_vertex_element *src = &draw->pt.vertex_element[vinfo->attrib[i].src_index];
126
127 unsigned emit_sz = 0;
128 unsigned input_format = src->src_format;
129 unsigned input_buffer = src->vertex_buffer_index;
130 unsigned input_offset = src->src_offset;
131 unsigned output_format;
132
133 output_format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
134 emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit);
135
136 if (vinfo->attrib[i].emit == EMIT_OMIT)
137 continue;
138
139 if (vinfo->attrib[i].emit == EMIT_1F_PSIZE) {
140 input_format = PIPE_FORMAT_R32_FLOAT;
141 input_buffer = draw->pt.nr_vertex_buffers;
142 input_offset = 0;
143 }
144
145 key.element[i].type = TRANSLATE_ELEMENT_NORMAL;
146 key.element[i].input_format = input_format;
147 key.element[i].input_buffer = input_buffer;
148 key.element[i].input_offset = input_offset;
149 key.element[i].instance_divisor = src->instance_divisor;
150 key.element[i].output_format = output_format;
151 key.element[i].output_offset = dst_offset;
152
153 dst_offset += emit_sz;
154 }
155
156 key.nr_elements = vinfo->num_attribs;
157 key.output_stride = vinfo->size * 4;
158
159 /* Don't bother with caching at this stage:
160 */
161 if (!feme->translate ||
162 translate_key_compare(&feme->translate->key, &key) != 0)
163 {
164 translate_key_sanitize(&key);
165 feme->translate = translate_cache_find(feme->cache,
166 &key);
167
168
169 feme->translate->set_buffer(feme->translate,
170 draw->pt.nr_vertex_buffers,
171 &feme->point_size,
172 0,
173 ~0);
174 }
175
176 feme->point_size = draw->rasterizer->point_size;
177
178 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
179 feme->translate->set_buffer(feme->translate,
180 i,
181 ((char *)draw->pt.user.vbuffer[i] +
182 draw->pt.vertex_buffer[i].buffer_offset),
183 draw->pt.vertex_buffer[i].stride,
184 draw->pt.vertex_buffer[i].max_index);
185 }
186
187 *max_vertices = (draw->render->max_vertex_buffer_bytes /
188 (vinfo->size * 4));
189
190 /* Return an even number of verts.
191 * This prevents "parity" errors when splitting long triangle strips which
192 * can lead to front/back culling mix-ups.
193 * Every other triangle in a strip has an alternate front/back orientation
194 * so splitting at an odd position can cause the orientation of subsequent
195 * triangles to get reversed.
196 */
197 *max_vertices = *max_vertices & ~1;
198 }
199
200
201
202
203
204 static void fetch_emit_run( struct draw_pt_middle_end *middle,
205 const unsigned *fetch_elts,
206 unsigned fetch_count,
207 const ushort *draw_elts,
208 unsigned draw_count )
209 {
210 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
211 struct draw_context *draw = feme->draw;
212 void *hw_verts;
213
214 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
215 */
216 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
217
218 if (fetch_count >= UNDEFINED_VERTEX_ID) {
219 assert(0);
220 return;
221 }
222
223 draw->render->allocate_vertices( draw->render,
224 (ushort)feme->translate->key.output_stride,
225 (ushort)fetch_count );
226
227 hw_verts = draw->render->map_vertices( draw->render );
228 if (!hw_verts) {
229 assert(0);
230 return;
231 }
232
233
234 /* Single routine to fetch vertices and emit HW verts.
235 */
236 feme->translate->run_elts( feme->translate,
237 fetch_elts,
238 fetch_count,
239 draw->instance_id,
240 hw_verts );
241
242 if (0) {
243 unsigned i;
244 for (i = 0; i < fetch_count; i++) {
245 debug_printf("\n\nvertex %d:\n", i);
246 draw_dump_emitted_vertex( feme->vinfo,
247 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
248 }
249 }
250
251 draw->render->unmap_vertices( draw->render,
252 0,
253 (ushort)(fetch_count - 1) );
254
255 /* XXX: Draw arrays path to avoid re-emitting index list again and
256 * again.
257 */
258 draw->render->draw_elements( draw->render,
259 draw_elts,
260 draw_count );
261
262 /* Done -- that was easy, wasn't it:
263 */
264 draw->render->release_vertices( draw->render );
265
266 }
267
268
269 static void fetch_emit_run_linear( struct draw_pt_middle_end *middle,
270 unsigned start,
271 unsigned count )
272 {
273 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
274 struct draw_context *draw = feme->draw;
275 void *hw_verts;
276
277 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
278 */
279 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
280
281 if (count >= UNDEFINED_VERTEX_ID)
282 goto fail;
283
284 if (!draw->render->allocate_vertices( draw->render,
285 (ushort)feme->translate->key.output_stride,
286 (ushort)count ))
287 goto fail;
288
289 hw_verts = draw->render->map_vertices( draw->render );
290 if (!hw_verts)
291 goto fail;
292
293 /* Single routine to fetch vertices and emit HW verts.
294 */
295 feme->translate->run( feme->translate,
296 start,
297 count,
298 draw->instance_id,
299 hw_verts );
300
301 if (0) {
302 unsigned i;
303 for (i = 0; i < count; i++) {
304 debug_printf("\n\nvertex %d:\n", i);
305 draw_dump_emitted_vertex( feme->vinfo,
306 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
307 }
308 }
309
310 draw->render->unmap_vertices( draw->render, 0, count - 1 );
311
312 /* XXX: Draw arrays path to avoid re-emitting index list again and
313 * again.
314 */
315 draw->render->draw_arrays( draw->render, 0, count );
316
317 /* Done -- that was easy, wasn't it:
318 */
319 draw->render->release_vertices( draw->render );
320 return;
321
322 fail:
323 assert(0);
324 return;
325 }
326
327
328 static boolean fetch_emit_run_linear_elts( struct draw_pt_middle_end *middle,
329 unsigned start,
330 unsigned count,
331 const ushort *draw_elts,
332 unsigned draw_count )
333 {
334 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
335 struct draw_context *draw = feme->draw;
336 void *hw_verts;
337
338 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
339 */
340 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
341
342 if (count >= UNDEFINED_VERTEX_ID)
343 return FALSE;
344
345 if (!draw->render->allocate_vertices( draw->render,
346 (ushort)feme->translate->key.output_stride,
347 (ushort)count ))
348 return FALSE;
349
350 hw_verts = draw->render->map_vertices( draw->render );
351 if (!hw_verts)
352 return FALSE;
353
354 /* Single routine to fetch vertices and emit HW verts.
355 */
356 feme->translate->run( feme->translate,
357 start,
358 count,
359 draw->instance_id,
360 hw_verts );
361
362 draw->render->unmap_vertices( draw->render, 0, (ushort)(count - 1) );
363
364 /* XXX: Draw arrays path to avoid re-emitting index list again and
365 * again.
366 */
367 draw->render->draw_elements( draw->render,
368 draw_elts,
369 draw_count );
370
371 /* Done -- that was easy, wasn't it:
372 */
373 draw->render->release_vertices( draw->render );
374
375 return TRUE;
376 }
377
378
379
380
381 static void fetch_emit_finish( struct draw_pt_middle_end *middle )
382 {
383 /* nothing to do */
384 }
385
386 static void fetch_emit_destroy( struct draw_pt_middle_end *middle )
387 {
388 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
389
390 if (feme->cache)
391 translate_cache_destroy(feme->cache);
392
393 FREE(middle);
394 }
395
396
397 struct draw_pt_middle_end *draw_pt_fetch_emit( struct draw_context *draw )
398 {
399 struct fetch_emit_middle_end *fetch_emit = CALLOC_STRUCT( fetch_emit_middle_end );
400 if (fetch_emit == NULL)
401 return NULL;
402
403 fetch_emit->cache = translate_cache_create();
404 if (!fetch_emit->cache) {
405 FREE(fetch_emit);
406 return NULL;
407 }
408
409 fetch_emit->base.prepare = fetch_emit_prepare;
410 fetch_emit->base.run = fetch_emit_run;
411 fetch_emit->base.run_linear = fetch_emit_run_linear;
412 fetch_emit->base.run_linear_elts = fetch_emit_run_linear_elts;
413 fetch_emit->base.finish = fetch_emit_finish;
414 fetch_emit->base.destroy = fetch_emit_destroy;
415
416 fetch_emit->draw = draw;
417
418 return &fetch_emit->base;
419 }
420