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