Merge branch 'shader-file-reorg'
[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 {
215 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
216 struct draw_context *draw = feme->draw;
217 void *hw_verts;
218
219 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
220 */
221 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
222
223 if (fetch_count >= UNDEFINED_VERTEX_ID) {
224 assert(0);
225 return;
226 }
227
228 draw->render->allocate_vertices( draw->render,
229 (ushort)feme->translate->key.output_stride,
230 (ushort)fetch_count );
231
232 hw_verts = draw->render->map_vertices( draw->render );
233 if (!hw_verts) {
234 assert(0);
235 return;
236 }
237
238
239 /* Single routine to fetch vertices and emit HW verts.
240 */
241 feme->translate->run_elts( feme->translate,
242 fetch_elts,
243 fetch_count,
244 draw->instance_id,
245 hw_verts );
246
247 if (0) {
248 unsigned i;
249 for (i = 0; i < fetch_count; i++) {
250 debug_printf("\n\nvertex %d:\n", i);
251 draw_dump_emitted_vertex( feme->vinfo,
252 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
253 }
254 }
255
256 draw->render->unmap_vertices( draw->render,
257 0,
258 (ushort)(fetch_count - 1) );
259
260 /* XXX: Draw arrays path to avoid re-emitting index list again and
261 * again.
262 */
263 draw->render->draw_elements( draw->render,
264 draw_elts,
265 draw_count );
266
267 /* Done -- that was easy, wasn't it:
268 */
269 draw->render->release_vertices( draw->render );
270
271 }
272
273
274 static void fetch_emit_run_linear( struct draw_pt_middle_end *middle,
275 unsigned start,
276 unsigned count )
277 {
278 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
279 struct draw_context *draw = feme->draw;
280 void *hw_verts;
281
282 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
283 */
284 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
285
286 if (count >= UNDEFINED_VERTEX_ID)
287 goto fail;
288
289 if (!draw->render->allocate_vertices( draw->render,
290 (ushort)feme->translate->key.output_stride,
291 (ushort)count ))
292 goto fail;
293
294 hw_verts = draw->render->map_vertices( draw->render );
295 if (!hw_verts)
296 goto fail;
297
298 /* Single routine to fetch vertices and emit HW verts.
299 */
300 feme->translate->run( feme->translate,
301 start,
302 count,
303 draw->instance_id,
304 hw_verts );
305
306 if (0) {
307 unsigned i;
308 for (i = 0; i < count; i++) {
309 debug_printf("\n\nvertex %d:\n", i);
310 draw_dump_emitted_vertex( feme->vinfo,
311 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
312 }
313 }
314
315 draw->render->unmap_vertices( draw->render, 0, count - 1 );
316
317 /* XXX: Draw arrays path to avoid re-emitting index list again and
318 * again.
319 */
320 draw->render->draw_arrays( draw->render, 0, count );
321
322 /* Done -- that was easy, wasn't it:
323 */
324 draw->render->release_vertices( draw->render );
325 return;
326
327 fail:
328 assert(0);
329 return;
330 }
331
332
333 static boolean fetch_emit_run_linear_elts( struct draw_pt_middle_end *middle,
334 unsigned start,
335 unsigned count,
336 const ushort *draw_elts,
337 unsigned draw_count )
338 {
339 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
340 struct draw_context *draw = feme->draw;
341 void *hw_verts;
342
343 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
344 */
345 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
346
347 if (count >= UNDEFINED_VERTEX_ID)
348 return FALSE;
349
350 if (!draw->render->allocate_vertices( draw->render,
351 (ushort)feme->translate->key.output_stride,
352 (ushort)count ))
353 return FALSE;
354
355 hw_verts = draw->render->map_vertices( draw->render );
356 if (!hw_verts)
357 return FALSE;
358
359 /* Single routine to fetch vertices and emit HW verts.
360 */
361 feme->translate->run( feme->translate,
362 start,
363 count,
364 draw->instance_id,
365 hw_verts );
366
367 draw->render->unmap_vertices( draw->render, 0, (ushort)(count - 1) );
368
369 /* XXX: Draw arrays path to avoid re-emitting index list again and
370 * again.
371 */
372 draw->render->draw_elements( draw->render,
373 draw_elts,
374 draw_count );
375
376 /* Done -- that was easy, wasn't it:
377 */
378 draw->render->release_vertices( draw->render );
379
380 return TRUE;
381 }
382
383
384
385
386 static void fetch_emit_finish( struct draw_pt_middle_end *middle )
387 {
388 /* nothing to do */
389 }
390
391 static void fetch_emit_destroy( struct draw_pt_middle_end *middle )
392 {
393 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
394
395 if (feme->cache)
396 translate_cache_destroy(feme->cache);
397
398 FREE(middle);
399 }
400
401
402 struct draw_pt_middle_end *draw_pt_fetch_emit( struct draw_context *draw )
403 {
404 struct fetch_emit_middle_end *fetch_emit = CALLOC_STRUCT( fetch_emit_middle_end );
405 if (fetch_emit == NULL)
406 return NULL;
407
408 fetch_emit->cache = translate_cache_create();
409 if (!fetch_emit->cache) {
410 FREE(fetch_emit);
411 return NULL;
412 }
413
414 fetch_emit->base.prepare = fetch_emit_prepare;
415 fetch_emit->base.run = fetch_emit_run;
416 fetch_emit->base.run_linear = fetch_emit_run_linear;
417 fetch_emit->base.run_linear_elts = fetch_emit_run_linear_elts;
418 fetch_emit->base.finish = fetch_emit_finish;
419 fetch_emit->base.destroy = fetch_emit_destroy;
420
421 fetch_emit->draw = draw;
422
423 return &fetch_emit->base;
424 }
425