draw: No need to make max_vertices even.
[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
196
197
198
199
200 static void fetch_emit_run( struct draw_pt_middle_end *middle,
201 const unsigned *fetch_elts,
202 unsigned fetch_count,
203 const ushort *draw_elts,
204 unsigned draw_count,
205 unsigned prim_flags )
206 {
207 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
208 struct draw_context *draw = feme->draw;
209 void *hw_verts;
210
211 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
212 */
213 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
214
215 if (fetch_count >= UNDEFINED_VERTEX_ID) {
216 assert(0);
217 return;
218 }
219
220 draw->render->allocate_vertices( draw->render,
221 (ushort)feme->translate->key.output_stride,
222 (ushort)fetch_count );
223
224 hw_verts = draw->render->map_vertices( draw->render );
225 if (!hw_verts) {
226 assert(0);
227 return;
228 }
229
230
231 /* Single routine to fetch vertices and emit HW verts.
232 */
233 feme->translate->run_elts( feme->translate,
234 fetch_elts,
235 fetch_count,
236 draw->instance_id,
237 hw_verts );
238
239 if (0) {
240 unsigned i;
241 for (i = 0; i < fetch_count; i++) {
242 debug_printf("\n\nvertex %d:\n", i);
243 draw_dump_emitted_vertex( feme->vinfo,
244 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
245 }
246 }
247
248 draw->render->unmap_vertices( draw->render,
249 0,
250 (ushort)(fetch_count - 1) );
251
252 /* XXX: Draw arrays path to avoid re-emitting index list again and
253 * again.
254 */
255 draw->render->draw_elements( draw->render,
256 draw_elts,
257 draw_count );
258
259 /* Done -- that was easy, wasn't it:
260 */
261 draw->render->release_vertices( draw->render );
262
263 }
264
265
266 static void fetch_emit_run_linear( struct draw_pt_middle_end *middle,
267 unsigned start,
268 unsigned count,
269 unsigned prim_flags )
270 {
271 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
272 struct draw_context *draw = feme->draw;
273 void *hw_verts;
274
275 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
276 */
277 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
278
279 if (count >= UNDEFINED_VERTEX_ID)
280 goto fail;
281
282 if (!draw->render->allocate_vertices( draw->render,
283 (ushort)feme->translate->key.output_stride,
284 (ushort)count ))
285 goto fail;
286
287 hw_verts = draw->render->map_vertices( draw->render );
288 if (!hw_verts)
289 goto fail;
290
291 /* Single routine to fetch vertices and emit HW verts.
292 */
293 feme->translate->run( feme->translate,
294 start,
295 count,
296 draw->instance_id,
297 hw_verts );
298
299 if (0) {
300 unsigned i;
301 for (i = 0; i < count; i++) {
302 debug_printf("\n\nvertex %d:\n", i);
303 draw_dump_emitted_vertex( feme->vinfo,
304 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
305 }
306 }
307
308 draw->render->unmap_vertices( draw->render, 0, count - 1 );
309
310 /* XXX: Draw arrays path to avoid re-emitting index list again and
311 * again.
312 */
313 draw->render->draw_arrays( draw->render, 0, count );
314
315 /* Done -- that was easy, wasn't it:
316 */
317 draw->render->release_vertices( draw->render );
318 return;
319
320 fail:
321 assert(0);
322 return;
323 }
324
325
326 static boolean fetch_emit_run_linear_elts( struct draw_pt_middle_end *middle,
327 unsigned start,
328 unsigned count,
329 const ushort *draw_elts,
330 unsigned draw_count,
331 unsigned prim_flags )
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_elements( 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