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