draw: allow overflows in the llvm paths
[mesa.git] / src / gallium / auxiliary / draw / draw_pt.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 "draw/draw_context.h"
34 #include "draw/draw_gs.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_pt.h"
37 #include "draw/draw_vbuf.h"
38 #include "draw/draw_vs.h"
39 #include "tgsi/tgsi_dump.h"
40 #include "util/u_math.h"
41 #include "util/u_prim.h"
42 #include "util/u_format.h"
43 #include "util/u_draw.h"
44
45
46 DEBUG_GET_ONCE_BOOL_OPTION(draw_fse, "DRAW_FSE", FALSE)
47 DEBUG_GET_ONCE_BOOL_OPTION(draw_no_fse, "DRAW_NO_FSE", FALSE)
48
49 /* Overall we split things into:
50 * - frontend -- prepare fetch_elts, draw_elts - eg vsplit
51 * - middle -- fetch, shade, cliptest, viewport
52 * - pipeline -- the prim pipeline: clipping, wide lines, etc
53 * - backend -- the vbuf_render provided by the driver.
54 */
55 static boolean
56 draw_pt_arrays(struct draw_context *draw,
57 unsigned prim,
58 unsigned start,
59 unsigned count)
60 {
61 struct draw_pt_front_end *frontend = NULL;
62 struct draw_pt_middle_end *middle = NULL;
63 unsigned opt = 0;
64
65 /* Sanitize primitive length:
66 */
67 {
68 unsigned first, incr;
69 draw_pt_split_prim(prim, &first, &incr);
70 count = draw_pt_trim_count(count, first, incr);
71 if (count < first)
72 return TRUE;
73 }
74
75 if (!draw->force_passthrough) {
76 unsigned gs_out_prim = (draw->gs.geometry_shader ?
77 draw->gs.geometry_shader->output_primitive :
78 prim);
79
80 if (!draw->render) {
81 opt |= PT_PIPELINE;
82 }
83
84 if (draw_need_pipeline(draw,
85 draw->rasterizer,
86 gs_out_prim)) {
87 opt |= PT_PIPELINE;
88 }
89
90 if ((draw->clip_xy ||
91 draw->clip_z ||
92 draw->clip_user) && !draw->pt.test_fse) {
93 opt |= PT_CLIPTEST;
94 }
95
96 opt |= PT_SHADE;
97 }
98
99 if (draw->pt.middle.llvm) {
100 middle = draw->pt.middle.llvm;
101 } else {
102 if (opt == 0)
103 middle = draw->pt.middle.fetch_emit;
104 else if (opt == PT_SHADE && !draw->pt.no_fse)
105 middle = draw->pt.middle.fetch_shade_emit;
106 else
107 middle = draw->pt.middle.general;
108 }
109
110 frontend = draw->pt.frontend;
111
112 if (frontend ) {
113 if (draw->pt.prim != prim || draw->pt.opt != opt) {
114 /* In certain conditions switching primitives requires us to flush
115 * and validate the different stages. One example is when smooth
116 * lines are active but first drawn with triangles and then with
117 * lines.
118 */
119 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
120 frontend = NULL;
121 } else if (draw->pt.eltSize != draw->pt.user.eltSize) {
122 /* Flush draw state if eltSize changed.
123 * This could be improved so only the frontend is flushed since it
124 * converts all indices to ushorts and the fetch part of the middle
125 * always prepares both linear and indexed.
126 */
127 frontend->flush( frontend, DRAW_FLUSH_STATE_CHANGE );
128 frontend = NULL;
129 }
130 }
131
132 if (!frontend) {
133 frontend = draw->pt.front.vsplit;
134
135 frontend->prepare( frontend, prim, middle, opt );
136
137 draw->pt.frontend = frontend;
138 draw->pt.eltSize = draw->pt.user.eltSize;
139 draw->pt.prim = prim;
140 draw->pt.opt = opt;
141 }
142
143 if (draw->pt.rebind_parameters) {
144 /* update constants, viewport dims, clip planes, etc */
145 middle->bind_parameters(middle);
146 draw->pt.rebind_parameters = FALSE;
147 }
148
149 frontend->run( frontend, start, count );
150
151 return TRUE;
152 }
153
154 void draw_pt_flush( struct draw_context *draw, unsigned flags )
155 {
156 assert(flags);
157
158 if (draw->pt.frontend) {
159 draw->pt.frontend->flush( draw->pt.frontend, flags );
160
161 /* don't prepare if we only are flushing the backend */
162 if (flags & DRAW_FLUSH_STATE_CHANGE)
163 draw->pt.frontend = NULL;
164 }
165
166 if (flags & DRAW_FLUSH_PARAMETER_CHANGE) {
167 draw->pt.rebind_parameters = TRUE;
168 }
169 }
170
171
172
173 boolean draw_pt_init( struct draw_context *draw )
174 {
175 draw->pt.test_fse = debug_get_option_draw_fse();
176 draw->pt.no_fse = debug_get_option_draw_no_fse();
177
178 draw->pt.front.vsplit = draw_pt_vsplit(draw);
179 if (!draw->pt.front.vsplit)
180 return FALSE;
181
182 draw->pt.middle.fetch_emit = draw_pt_fetch_emit( draw );
183 if (!draw->pt.middle.fetch_emit)
184 return FALSE;
185
186 draw->pt.middle.fetch_shade_emit = draw_pt_middle_fse( draw );
187 if (!draw->pt.middle.fetch_shade_emit)
188 return FALSE;
189
190 draw->pt.middle.general = draw_pt_fetch_pipeline_or_emit( draw );
191 if (!draw->pt.middle.general)
192 return FALSE;
193
194 #if HAVE_LLVM
195 if (draw->llvm)
196 draw->pt.middle.llvm = draw_pt_fetch_pipeline_or_emit_llvm( draw );
197 #endif
198
199 return TRUE;
200 }
201
202
203 void draw_pt_destroy( struct draw_context *draw )
204 {
205 if (draw->pt.middle.llvm) {
206 draw->pt.middle.llvm->destroy( draw->pt.middle.llvm );
207 draw->pt.middle.llvm = NULL;
208 }
209
210 if (draw->pt.middle.general) {
211 draw->pt.middle.general->destroy( draw->pt.middle.general );
212 draw->pt.middle.general = NULL;
213 }
214
215 if (draw->pt.middle.fetch_emit) {
216 draw->pt.middle.fetch_emit->destroy( draw->pt.middle.fetch_emit );
217 draw->pt.middle.fetch_emit = NULL;
218 }
219
220 if (draw->pt.middle.fetch_shade_emit) {
221 draw->pt.middle.fetch_shade_emit->destroy( draw->pt.middle.fetch_shade_emit );
222 draw->pt.middle.fetch_shade_emit = NULL;
223 }
224
225 if (draw->pt.front.vsplit) {
226 draw->pt.front.vsplit->destroy( draw->pt.front.vsplit );
227 draw->pt.front.vsplit = NULL;
228 }
229 }
230
231
232 /**
233 * Debug- print the first 'count' vertices.
234 */
235 static void
236 draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count)
237 {
238 uint i;
239
240 debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
241 prim, start, count);
242
243 for (i = 0; i < count; i++) {
244 uint ii = 0;
245 uint j;
246
247 if (draw->pt.user.eltSize) {
248 /* indexed arrays */
249
250 switch (draw->pt.user.eltSize) {
251 case 1:
252 {
253 const ubyte *elem = (const ubyte *) draw->pt.user.elts;
254 ii = elem[start + i];
255 }
256 break;
257 case 2:
258 {
259 const ushort *elem = (const ushort *) draw->pt.user.elts;
260 ii = elem[start + i];
261 }
262 break;
263 case 4:
264 {
265 const uint *elem = (const uint *) draw->pt.user.elts;
266 ii = elem[start + i];
267 }
268 break;
269 default:
270 assert(0);
271 return;
272 }
273 ii += draw->pt.user.eltBias;
274 debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start, i,
275 draw->pt.user.eltBias, ii);
276 }
277 else {
278 /* non-indexed arrays */
279 ii = start + i;
280 debug_printf("Vertex %u:\n", ii);
281 }
282
283 for (j = 0; j < draw->pt.nr_vertex_elements; j++) {
284 uint buf = draw->pt.vertex_element[j].vertex_buffer_index;
285 ubyte *ptr = (ubyte *) draw->pt.user.vbuffer[buf].map;
286
287 if (draw->pt.vertex_element[j].instance_divisor) {
288 ii = draw->instance_id / draw->pt.vertex_element[j].instance_divisor;
289 }
290
291 ptr += draw->pt.vertex_buffer[buf].buffer_offset;
292 ptr += draw->pt.vertex_buffer[buf].stride * ii;
293 ptr += draw->pt.vertex_element[j].src_offset;
294
295 debug_printf(" Attr %u: ", j);
296 switch (draw->pt.vertex_element[j].src_format) {
297 case PIPE_FORMAT_R32_FLOAT:
298 {
299 float *v = (float *) ptr;
300 debug_printf("R %f @ %p\n", v[0], (void *) v);
301 }
302 break;
303 case PIPE_FORMAT_R32G32_FLOAT:
304 {
305 float *v = (float *) ptr;
306 debug_printf("RG %f %f @ %p\n", v[0], v[1], (void *) v);
307 }
308 break;
309 case PIPE_FORMAT_R32G32B32_FLOAT:
310 {
311 float *v = (float *) ptr;
312 debug_printf("RGB %f %f %f @ %p\n", v[0], v[1], v[2], (void *) v);
313 }
314 break;
315 case PIPE_FORMAT_R32G32B32A32_FLOAT:
316 {
317 float *v = (float *) ptr;
318 debug_printf("RGBA %f %f %f %f @ %p\n", v[0], v[1], v[2], v[3],
319 (void *) v);
320 }
321 break;
322 case PIPE_FORMAT_B8G8R8A8_UNORM:
323 {
324 ubyte *u = (ubyte *) ptr;
325 debug_printf("BGRA %d %d %d %d @ %p\n", u[0], u[1], u[2], u[3],
326 (void *) u);
327 }
328 break;
329 case PIPE_FORMAT_A8R8G8B8_UNORM:
330 {
331 ubyte *u = (ubyte *) ptr;
332 debug_printf("ARGB %d %d %d %d @ %p\n", u[0], u[1], u[2], u[3],
333 (void *) u);
334 }
335 break;
336 default:
337 debug_printf("other format %s (fix me)\n",
338 util_format_name(draw->pt.vertex_element[j].src_format));
339 }
340 }
341 }
342 }
343
344
345 /** Helper code for below */
346 #define PRIM_RESTART_LOOP(elements) \
347 do { \
348 for (i = start; i < end; i++) { \
349 if (i < elt_max && elements[i] == info->restart_index) { \
350 if (cur_count > 0) { \
351 /* draw elts up to prev pos */ \
352 draw_pt_arrays(draw, prim, cur_start, cur_count); \
353 } \
354 /* begin new prim at next elt */ \
355 cur_start = i + 1; \
356 cur_count = 0; \
357 } \
358 else { \
359 cur_count++; \
360 } \
361 } \
362 if (cur_count > 0) { \
363 draw_pt_arrays(draw, prim, cur_start, cur_count); \
364 } \
365 } while (0)
366
367
368 /**
369 * For drawing prims with primitive restart enabled.
370 * Scan for restart indexes and draw the runs of elements/vertices between
371 * the restarts.
372 */
373 static void
374 draw_pt_arrays_restart(struct draw_context *draw,
375 const struct pipe_draw_info *info)
376 {
377 const unsigned prim = info->mode;
378 const unsigned start = info->start;
379 const unsigned count = info->count;
380 const unsigned end = start + count;
381 const unsigned elt_max = draw->pt.user.eltMax;
382 unsigned i, cur_start, cur_count;
383
384 assert(info->primitive_restart);
385
386 if (draw->pt.user.eltSize) {
387 /* indexed prims (draw_elements) */
388 cur_start = start;
389 cur_count = 0;
390
391 switch (draw->pt.user.eltSize) {
392 case 1:
393 {
394 const ubyte *elt_ub = (const ubyte *) draw->pt.user.elts;
395 PRIM_RESTART_LOOP(elt_ub);
396 }
397 break;
398 case 2:
399 {
400 const ushort *elt_us = (const ushort *) draw->pt.user.elts;
401 PRIM_RESTART_LOOP(elt_us);
402 }
403 break;
404 case 4:
405 {
406 const uint *elt_ui = (const uint *) draw->pt.user.elts;
407 PRIM_RESTART_LOOP(elt_ui);
408 }
409 break;
410 default:
411 assert(0 && "bad eltSize in draw_arrays()");
412 }
413 }
414 else {
415 /* Non-indexed prims (draw_arrays).
416 * Primitive restart should have been handled in the state tracker.
417 */
418 draw_pt_arrays(draw, prim, start, count);
419 }
420 }
421
422
423 /**
424 * Resolve true values within pipe_draw_info.
425 * If we're rendering from transform feedback/stream output
426 * buffers both the count and max_index need to be computed
427 * from the attached stream output target.
428 */
429 static void
430 resolve_draw_info(const struct pipe_draw_info *raw_info,
431 struct pipe_draw_info *info)
432 {
433 memcpy(info, raw_info, sizeof(struct pipe_draw_info));
434
435 if (raw_info->count_from_stream_output) {
436 struct draw_so_target *target =
437 (struct draw_so_target *)info->count_from_stream_output;
438 info->count = target->emitted_vertices;
439
440 /* Stream output draw can not be indexed */
441 debug_assert(!info->indexed);
442 info->max_index = info->count - 1;
443 }
444 }
445
446 /**
447 * Draw vertex arrays.
448 * This is the main entrypoint into the drawing module. If drawing an indexed
449 * primitive, the draw_set_indexes() function should have already been called
450 * to specify the element/index buffer information.
451 */
452 void
453 draw_vbo(struct draw_context *draw,
454 const struct pipe_draw_info *info)
455 {
456 unsigned instance;
457 unsigned index_limit;
458 unsigned count;
459 struct pipe_draw_info resolved_info;
460
461 resolve_draw_info(info, &resolved_info);
462 info = &resolved_info;
463
464 assert(info->instance_count > 0);
465 if (info->indexed)
466 assert(draw->pt.user.elts);
467
468 count = info->count;
469
470 draw->pt.user.eltBias = info->index_bias;
471 draw->pt.user.min_index = info->min_index;
472 draw->pt.user.max_index = info->max_index;
473 draw->pt.user.eltSize = info->indexed ? draw->pt.user.eltSizeIB : 0;
474
475 if (0)
476 debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
477 info->mode, info->start, count);
478
479 if (0)
480 tgsi_dump(draw->vs.vertex_shader->state.tokens, 0);
481
482 if (0) {
483 unsigned int i;
484 debug_printf("Elements:\n");
485 for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
486 debug_printf(" %u: src_offset=%u inst_div=%u vbuf=%u format=%s\n",
487 i,
488 draw->pt.vertex_element[i].src_offset,
489 draw->pt.vertex_element[i].instance_divisor,
490 draw->pt.vertex_element[i].vertex_buffer_index,
491 util_format_name(draw->pt.vertex_element[i].src_format));
492 }
493 debug_printf("Buffers:\n");
494 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
495 debug_printf(" %u: stride=%u offset=%u size=%d ptr=%p\n",
496 i,
497 draw->pt.vertex_buffer[i].stride,
498 draw->pt.vertex_buffer[i].buffer_offset,
499 (int) draw->pt.user.vbuffer[i].size,
500 draw->pt.user.vbuffer[i].map);
501 }
502 }
503
504 if (0)
505 draw_print_arrays(draw, info->mode, info->start, MIN2(count, 20));
506
507 index_limit = util_draw_max_index(draw->pt.vertex_buffer,
508 draw->pt.vertex_element,
509 draw->pt.nr_vertex_elements,
510 info);
511 #if HAVE_LLVM
512 if (!draw->llvm)
513 #endif
514 {
515 if (index_limit == 0) {
516 /* one of the buffers is too small to do any valid drawing */
517 debug_warning("draw: VBO too small to draw anything\n");
518 return;
519 }
520 }
521
522 /* If we're collecting stats then make sure we start from scratch */
523 if (draw->collect_statistics) {
524 memset(&draw->statistics, 0, sizeof(draw->statistics));
525 }
526
527 draw->pt.max_index = index_limit - 1;
528
529 /*
530 * TODO: We could use draw->pt.max_index to further narrow
531 * the min_index/max_index hints given by the state tracker.
532 */
533
534 for (instance = 0; instance < info->instance_count; instance++) {
535 draw->instance_id = instance + info->start_instance;
536
537 draw_new_instance(draw);
538
539 if (info->primitive_restart) {
540 draw_pt_arrays_restart(draw, info);
541 }
542 else {
543 draw_pt_arrays(draw, info->mode, info->start, count);
544 }
545 }
546
547 /* If requested emit the pipeline statistics for this run */
548 if (draw->collect_statistics) {
549 draw->render->pipeline_statistics(draw->render, &draw->statistics);
550 }
551 }