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