Merge branch '7.8'
[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 opt,
94 unsigned *max_vertices )
95 {
96 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
97 struct draw_context *draw = feme->draw;
98 const struct vertex_info *vinfo;
99 unsigned i, dst_offset;
100 boolean ok;
101 struct translate_key key;
102
103
104 ok = draw->render->set_primitive( draw->render,
105 prim );
106 if (!ok) {
107 assert(0);
108 return;
109 }
110
111 /* Must do this after set_primitive() above:
112 */
113 vinfo = feme->vinfo = draw->render->get_vertex_info(draw->render);
114
115
116
117 /* Transform from API vertices to HW vertices, skipping the
118 * pipeline_vertex intermediate step.
119 */
120 dst_offset = 0;
121 memset(&key, 0, sizeof(key));
122
123 for (i = 0; i < vinfo->num_attribs; i++) {
124 const struct pipe_vertex_element *src = &draw->pt.vertex_element[vinfo->attrib[i].src_index];
125
126 unsigned emit_sz = 0;
127 unsigned input_format = src->src_format;
128 unsigned input_buffer = src->vertex_buffer_index;
129 unsigned input_offset = src->src_offset;
130 unsigned output_format;
131
132 output_format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
133 emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit);
134
135 if (vinfo->attrib[i].emit == EMIT_OMIT)
136 continue;
137
138 if (vinfo->attrib[i].emit == EMIT_1F_PSIZE) {
139 input_format = PIPE_FORMAT_R32_FLOAT;
140 input_buffer = draw->pt.nr_vertex_buffers;
141 input_offset = 0;
142 }
143
144 key.element[i].type = TRANSLATE_ELEMENT_NORMAL;
145 key.element[i].input_format = input_format;
146 key.element[i].input_buffer = input_buffer;
147 key.element[i].input_offset = input_offset;
148 key.element[i].instance_divisor = src->instance_divisor;
149 key.element[i].output_format = output_format;
150 key.element[i].output_offset = dst_offset;
151
152 dst_offset += emit_sz;
153 }
154
155 key.nr_elements = vinfo->num_attribs;
156 key.output_stride = vinfo->size * 4;
157
158 /* Don't bother with caching at this stage:
159 */
160 if (!feme->translate ||
161 translate_key_compare(&feme->translate->key, &key) != 0)
162 {
163 translate_key_sanitize(&key);
164 feme->translate = translate_cache_find(feme->cache,
165 &key);
166
167
168 feme->translate->set_buffer(feme->translate,
169 draw->pt.nr_vertex_buffers,
170 &feme->point_size,
171 0,
172 ~0);
173 }
174
175 feme->point_size = draw->rasterizer->point_size;
176
177 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
178 feme->translate->set_buffer(feme->translate,
179 i,
180 ((char *)draw->pt.user.vbuffer[i] +
181 draw->pt.vertex_buffer[i].buffer_offset),
182 draw->pt.vertex_buffer[i].stride,
183 draw->pt.vertex_buffer[i].max_index);
184 }
185
186 *max_vertices = (draw->render->max_vertex_buffer_bytes /
187 (vinfo->size * 4));
188
189 /* Return an even number of verts.
190 * This prevents "parity" errors when splitting long triangle strips which
191 * can lead to front/back culling mix-ups.
192 * Every other triangle in a strip has an alternate front/back orientation
193 * so splitting at an odd position can cause the orientation of subsequent
194 * triangles to get reversed.
195 */
196 *max_vertices = *max_vertices & ~1;
197 }
198
199
200
201
202
203 static void fetch_emit_run( struct draw_pt_middle_end *middle,
204 const unsigned *fetch_elts,
205 unsigned fetch_count,
206 const ushort *draw_elts,
207 unsigned draw_count )
208 {
209 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
210 struct draw_context *draw = feme->draw;
211 void *hw_verts;
212
213 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
214 */
215 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
216
217 if (fetch_count >= UNDEFINED_VERTEX_ID) {
218 assert(0);
219 return;
220 }
221
222 draw->render->allocate_vertices( draw->render,
223 (ushort)feme->translate->key.output_stride,
224 (ushort)fetch_count );
225
226 hw_verts = draw->render->map_vertices( draw->render );
227 if (!hw_verts) {
228 assert(0);
229 return;
230 }
231
232
233 /* Single routine to fetch vertices and emit HW verts.
234 */
235 feme->translate->run_elts( feme->translate,
236 fetch_elts,
237 fetch_count,
238 draw->instance_id,
239 hw_verts );
240
241 if (0) {
242 unsigned i;
243 for (i = 0; i < fetch_count; i++) {
244 debug_printf("\n\nvertex %d:\n", i);
245 draw_dump_emitted_vertex( feme->vinfo,
246 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
247 }
248 }
249
250 draw->render->unmap_vertices( draw->render,
251 0,
252 (ushort)(fetch_count - 1) );
253
254 /* XXX: Draw arrays path to avoid re-emitting index list again and
255 * again.
256 */
257 draw->render->draw( draw->render,
258 draw_elts,
259 draw_count );
260
261 /* Done -- that was easy, wasn't it:
262 */
263 draw->render->release_vertices( draw->render );
264
265 }
266
267
268 static void fetch_emit_run_linear( struct draw_pt_middle_end *middle,
269 unsigned start,
270 unsigned count )
271 {
272 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
273 struct draw_context *draw = feme->draw;
274 void *hw_verts;
275
276 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
277 */
278 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
279
280 if (count >= UNDEFINED_VERTEX_ID)
281 goto fail;
282
283 if (!draw->render->allocate_vertices( draw->render,
284 (ushort)feme->translate->key.output_stride,
285 (ushort)count ))
286 goto fail;
287
288 hw_verts = draw->render->map_vertices( draw->render );
289 if (!hw_verts)
290 goto fail;
291
292 /* Single routine to fetch vertices and emit HW verts.
293 */
294 feme->translate->run( feme->translate,
295 start,
296 count,
297 draw->instance_id,
298 hw_verts );
299
300 if (0) {
301 unsigned i;
302 for (i = 0; i < count; i++) {
303 debug_printf("\n\nvertex %d:\n", i);
304 draw_dump_emitted_vertex( feme->vinfo,
305 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
306 }
307 }
308
309 draw->render->unmap_vertices( draw->render, 0, count - 1 );
310
311 /* XXX: Draw arrays path to avoid re-emitting index list again and
312 * again.
313 */
314 draw->render->draw_arrays( draw->render, 0, count );
315
316 /* Done -- that was easy, wasn't it:
317 */
318 draw->render->release_vertices( draw->render );
319 return;
320
321 fail:
322 assert(0);
323 return;
324 }
325
326
327 static boolean fetch_emit_run_linear_elts( struct draw_pt_middle_end *middle,
328 unsigned start,
329 unsigned count,
330 const ushort *draw_elts,
331 unsigned draw_count )
332 {
333 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
334 struct draw_context *draw = feme->draw;
335 void *hw_verts;
336
337 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
338 */
339 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
340
341 if (count >= UNDEFINED_VERTEX_ID)
342 return FALSE;
343
344 if (!draw->render->allocate_vertices( draw->render,
345 (ushort)feme->translate->key.output_stride,
346 (ushort)count ))
347 return FALSE;
348
349 hw_verts = draw->render->map_vertices( draw->render );
350 if (!hw_verts)
351 return FALSE;
352
353 /* Single routine to fetch vertices and emit HW verts.
354 */
355 feme->translate->run( feme->translate,
356 start,
357 count,
358 draw->instance_id,
359 hw_verts );
360
361 draw->render->unmap_vertices( draw->render, 0, (ushort)(count - 1) );
362
363 /* XXX: Draw arrays path to avoid re-emitting index list again and
364 * again.
365 */
366 draw->render->draw( draw->render,
367 draw_elts,
368 draw_count );
369
370 /* Done -- that was easy, wasn't it:
371 */
372 draw->render->release_vertices( draw->render );
373
374 return TRUE;
375 }
376
377
378
379
380 static void fetch_emit_finish( struct draw_pt_middle_end *middle )
381 {
382 /* nothing to do */
383 }
384
385 static void fetch_emit_destroy( struct draw_pt_middle_end *middle )
386 {
387 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
388
389 if (feme->cache)
390 translate_cache_destroy(feme->cache);
391
392 FREE(middle);
393 }
394
395
396 struct draw_pt_middle_end *draw_pt_fetch_emit( struct draw_context *draw )
397 {
398 struct fetch_emit_middle_end *fetch_emit = CALLOC_STRUCT( fetch_emit_middle_end );
399 if (fetch_emit == NULL)
400 return NULL;
401
402 fetch_emit->cache = translate_cache_create();
403 if (!fetch_emit->cache) {
404 FREE(fetch_emit);
405 return NULL;
406 }
407
408 fetch_emit->base.prepare = fetch_emit_prepare;
409 fetch_emit->base.run = fetch_emit_run;
410 fetch_emit->base.run_linear = fetch_emit_run_linear;
411 fetch_emit->base.run_linear_elts = fetch_emit_run_linear_elts;
412 fetch_emit->base.finish = fetch_emit_finish;
413 fetch_emit->base.destroy = fetch_emit_destroy;
414
415 fetch_emit->draw = draw;
416
417 return &fetch_emit->base;
418 }
419