1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
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"
47 DEBUG_GET_ONCE_BOOL_OPTION(draw_fse
, "DRAW_FSE", FALSE
)
48 DEBUG_GET_ONCE_BOOL_OPTION(draw_no_fse
, "DRAW_NO_FSE", FALSE
)
50 /* Overall we split things into:
51 * - frontend -- prepare fetch_elts, draw_elts - eg vsplit
52 * - middle -- fetch, shade, cliptest, viewport
53 * - pipeline -- the prim pipeline: clipping, wide lines, etc
54 * - backend -- the vbuf_render provided by the driver.
57 draw_pt_arrays(struct draw_context
*draw
,
62 struct draw_pt_front_end
*frontend
= NULL
;
63 struct draw_pt_middle_end
*middle
= NULL
;
66 /* Sanitize primitive length:
71 if (prim
== PIPE_PRIM_PATCHES
) {
72 first
= draw
->pt
.vertices_per_patch
;
73 incr
= draw
->pt
.vertices_per_patch
;
75 draw_pt_split_prim(prim
, &first
, &incr
);
76 count
= draw_pt_trim_count(count
, first
, incr
);
81 if (!draw
->force_passthrough
) {
82 unsigned out_prim
= prim
;
84 if (draw
->gs
.geometry_shader
)
85 out_prim
= draw
->gs
.geometry_shader
->output_primitive
;
86 else if (draw
->tes
.tess_eval_shader
)
87 out_prim
= get_tes_output_prim(draw
->tes
.tess_eval_shader
);
93 if (draw_need_pipeline(draw
,
101 draw
->clip_user
) && !draw
->pt
.test_fse
) {
108 if (draw
->pt
.middle
.llvm
) {
109 middle
= draw
->pt
.middle
.llvm
;
112 middle
= draw
->pt
.middle
.fetch_emit
;
113 else if (opt
== PT_SHADE
&& !draw
->pt
.no_fse
)
114 middle
= draw
->pt
.middle
.fetch_shade_emit
;
116 middle
= draw
->pt
.middle
.general
;
119 frontend
= draw
->pt
.frontend
;
122 if (draw
->pt
.prim
!= prim
|| draw
->pt
.opt
!= opt
) {
123 /* In certain conditions switching primitives requires us to flush
124 * and validate the different stages. One example is when smooth
125 * lines are active but first drawn with triangles and then with
128 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
130 } else if (draw
->pt
.eltSize
!= draw
->pt
.user
.eltSize
) {
131 /* Flush draw state if eltSize changed.
132 * This could be improved so only the frontend is flushed since it
133 * converts all indices to ushorts and the fetch part of the middle
134 * always prepares both linear and indexed.
136 frontend
->flush( frontend
, DRAW_FLUSH_STATE_CHANGE
);
142 frontend
= draw
->pt
.front
.vsplit
;
144 frontend
->prepare( frontend
, prim
, middle
, opt
);
146 draw
->pt
.frontend
= frontend
;
147 draw
->pt
.eltSize
= draw
->pt
.user
.eltSize
;
148 draw
->pt
.prim
= prim
;
152 if (draw
->pt
.rebind_parameters
) {
153 /* update constants, viewport dims, clip planes, etc */
154 middle
->bind_parameters(middle
);
155 draw
->pt
.rebind_parameters
= FALSE
;
158 frontend
->run( frontend
, start
, count
);
163 void draw_pt_flush( struct draw_context
*draw
, unsigned flags
)
167 if (draw
->pt
.frontend
) {
168 draw
->pt
.frontend
->flush( draw
->pt
.frontend
, flags
);
170 /* don't prepare if we only are flushing the backend */
171 if (flags
& DRAW_FLUSH_STATE_CHANGE
)
172 draw
->pt
.frontend
= NULL
;
175 if (flags
& DRAW_FLUSH_PARAMETER_CHANGE
) {
176 draw
->pt
.rebind_parameters
= TRUE
;
182 boolean
draw_pt_init( struct draw_context
*draw
)
184 draw
->pt
.test_fse
= debug_get_option_draw_fse();
185 draw
->pt
.no_fse
= debug_get_option_draw_no_fse();
187 draw
->pt
.front
.vsplit
= draw_pt_vsplit(draw
);
188 if (!draw
->pt
.front
.vsplit
)
191 draw
->pt
.middle
.fetch_emit
= draw_pt_fetch_emit( draw
);
192 if (!draw
->pt
.middle
.fetch_emit
)
195 draw
->pt
.middle
.fetch_shade_emit
= draw_pt_middle_fse( draw
);
196 if (!draw
->pt
.middle
.fetch_shade_emit
)
199 draw
->pt
.middle
.general
= draw_pt_fetch_pipeline_or_emit( draw
);
200 if (!draw
->pt
.middle
.general
)
203 #ifdef LLVM_AVAILABLE
205 draw
->pt
.middle
.llvm
= draw_pt_fetch_pipeline_or_emit_llvm( draw
);
212 void draw_pt_destroy( struct draw_context
*draw
)
214 if (draw
->pt
.middle
.llvm
) {
215 draw
->pt
.middle
.llvm
->destroy( draw
->pt
.middle
.llvm
);
216 draw
->pt
.middle
.llvm
= NULL
;
219 if (draw
->pt
.middle
.general
) {
220 draw
->pt
.middle
.general
->destroy( draw
->pt
.middle
.general
);
221 draw
->pt
.middle
.general
= NULL
;
224 if (draw
->pt
.middle
.fetch_emit
) {
225 draw
->pt
.middle
.fetch_emit
->destroy( draw
->pt
.middle
.fetch_emit
);
226 draw
->pt
.middle
.fetch_emit
= NULL
;
229 if (draw
->pt
.middle
.fetch_shade_emit
) {
230 draw
->pt
.middle
.fetch_shade_emit
->destroy( draw
->pt
.middle
.fetch_shade_emit
);
231 draw
->pt
.middle
.fetch_shade_emit
= NULL
;
234 if (draw
->pt
.front
.vsplit
) {
235 draw
->pt
.front
.vsplit
->destroy( draw
->pt
.front
.vsplit
);
236 draw
->pt
.front
.vsplit
= NULL
;
242 * Debug- print the first 'count' vertices.
245 draw_print_arrays(struct draw_context
*draw
, uint prim
, int start
, uint count
)
249 debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
252 for (i
= 0; i
< count
; i
++) {
256 if (draw
->pt
.user
.eltSize
) {
259 switch (draw
->pt
.user
.eltSize
) {
262 const ubyte
*elem
= (const ubyte
*) draw
->pt
.user
.elts
;
263 ii
= elem
[start
+ i
];
268 const ushort
*elem
= (const ushort
*) draw
->pt
.user
.elts
;
269 ii
= elem
[start
+ i
];
274 const uint
*elem
= (const uint
*) draw
->pt
.user
.elts
;
275 ii
= elem
[start
+ i
];
282 ii
+= draw
->pt
.user
.eltBias
;
283 debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start
, i
,
284 draw
->pt
.user
.eltBias
, ii
);
287 /* non-indexed arrays */
289 debug_printf("Vertex %u:\n", ii
);
292 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; j
++) {
293 uint buf
= draw
->pt
.vertex_element
[j
].vertex_buffer_index
;
294 ubyte
*ptr
= (ubyte
*) draw
->pt
.user
.vbuffer
[buf
].map
;
296 if (draw
->pt
.vertex_element
[j
].instance_divisor
) {
297 ii
= draw
->instance_id
/ draw
->pt
.vertex_element
[j
].instance_divisor
;
300 ptr
+= draw
->pt
.vertex_buffer
[buf
].buffer_offset
;
301 ptr
+= draw
->pt
.vertex_buffer
[buf
].stride
* ii
;
302 ptr
+= draw
->pt
.vertex_element
[j
].src_offset
;
304 debug_printf(" Attr %u: ", j
);
305 switch (draw
->pt
.vertex_element
[j
].src_format
) {
306 case PIPE_FORMAT_R32_FLOAT
:
308 float *v
= (float *) ptr
;
309 debug_printf("R %f @ %p\n", v
[0], (void *) v
);
312 case PIPE_FORMAT_R32G32_FLOAT
:
314 float *v
= (float *) ptr
;
315 debug_printf("RG %f %f @ %p\n", v
[0], v
[1], (void *) v
);
318 case PIPE_FORMAT_R32G32B32_FLOAT
:
320 float *v
= (float *) ptr
;
321 debug_printf("RGB %f %f %f @ %p\n", v
[0], v
[1], v
[2], (void *) v
);
324 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
326 float *v
= (float *) ptr
;
327 debug_printf("RGBA %f %f %f %f @ %p\n", v
[0], v
[1], v
[2], v
[3],
331 case PIPE_FORMAT_B8G8R8A8_UNORM
:
333 ubyte
*u
= (ubyte
*) ptr
;
334 debug_printf("BGRA %d %d %d %d @ %p\n", u
[0], u
[1], u
[2], u
[3],
338 case PIPE_FORMAT_A8R8G8B8_UNORM
:
340 ubyte
*u
= (ubyte
*) ptr
;
341 debug_printf("ARGB %d %d %d %d @ %p\n", u
[0], u
[1], u
[2], u
[3],
346 debug_printf("other format %s (fix me)\n",
347 util_format_name(draw
->pt
.vertex_element
[j
].src_format
));
354 /** Helper code for below */
355 #define PRIM_RESTART_LOOP(elements) \
357 for (j = 0; j < count; j++) { \
358 i = draw_overflow_uadd(start, j, MAX_LOOP_IDX); \
359 if (i < elt_max && elements[i] == info->restart_index) { \
360 if (cur_count > 0) { \
361 /* draw elts up to prev pos */ \
362 draw_pt_arrays(draw, prim, cur_start, cur_count); \
364 /* begin new prim at next elt */ \
372 if (cur_count > 0) { \
373 draw_pt_arrays(draw, prim, cur_start, cur_count); \
379 * For drawing prims with primitive restart enabled.
380 * Scan for restart indexes and draw the runs of elements/vertices between
384 draw_pt_arrays_restart(struct draw_context
*draw
,
385 const struct pipe_draw_info
*info
)
387 const unsigned prim
= info
->mode
;
388 const unsigned start
= info
->start
;
389 const unsigned count
= info
->count
;
390 const unsigned elt_max
= draw
->pt
.user
.eltMax
;
391 unsigned i
, j
, cur_start
, cur_count
;
392 /* The largest index within a loop using the i variable as the index.
393 * Used for overflow detection */
394 const unsigned MAX_LOOP_IDX
= 0xffffffff;
396 assert(info
->primitive_restart
);
398 if (draw
->pt
.user
.eltSize
) {
399 /* indexed prims (draw_elements) */
403 switch (draw
->pt
.user
.eltSize
) {
406 const ubyte
*elt_ub
= (const ubyte
*) draw
->pt
.user
.elts
;
407 PRIM_RESTART_LOOP(elt_ub
);
412 const ushort
*elt_us
= (const ushort
*) draw
->pt
.user
.elts
;
413 PRIM_RESTART_LOOP(elt_us
);
418 const uint
*elt_ui
= (const uint
*) draw
->pt
.user
.elts
;
419 PRIM_RESTART_LOOP(elt_ui
);
423 assert(0 && "bad eltSize in draw_arrays()");
427 /* Non-indexed prims (draw_arrays).
428 * Primitive restart should have been handled in the state tracker.
430 draw_pt_arrays(draw
, prim
, start
, count
);
436 * Resolve true values within pipe_draw_info.
437 * If we're rendering from transform feedback/stream output
438 * buffers both the count and max_index need to be computed
439 * from the attached stream output target.
442 resolve_draw_info(const struct pipe_draw_info
*raw_info
,
443 struct pipe_draw_info
*info
,
444 struct pipe_vertex_buffer
*vertex_buffer
)
446 memcpy(info
, raw_info
, sizeof(struct pipe_draw_info
));
448 if (raw_info
->count_from_stream_output
) {
449 struct draw_so_target
*target
=
450 (struct draw_so_target
*)info
->count_from_stream_output
;
451 assert(vertex_buffer
!= NULL
);
452 info
->count
= vertex_buffer
->stride
== 0 ? 0 :
453 target
->internal_offset
/ vertex_buffer
->stride
;
455 /* Stream output draw can not be indexed */
456 debug_assert(!info
->index_size
);
457 info
->max_index
= info
->count
- 1;
462 * Draw vertex arrays.
463 * This is the main entrypoint into the drawing module. If drawing an indexed
464 * primitive, the draw_set_indexes() function should have already been called
465 * to specify the element/index buffer information.
468 draw_vbo(struct draw_context
*draw
,
469 const struct pipe_draw_info
*info
)
472 unsigned index_limit
;
474 unsigned fpstate
= util_fpstate_get();
475 struct pipe_draw_info resolved_info
;
477 if (info
->instance_count
== 0)
480 /* Make sure that denorms are treated like zeros. This is
481 * the behavior required by D3D10. OpenGL doesn't care.
483 util_fpstate_set_denorms_to_zero(fpstate
);
485 resolve_draw_info(info
, &resolved_info
, &(draw
->pt
.vertex_buffer
[0]));
486 info
= &resolved_info
;
488 if (info
->index_size
)
489 assert(draw
->pt
.user
.elts
);
493 draw
->pt
.user
.eltBias
= info
->index_bias
;
494 draw
->pt
.user
.min_index
= info
->min_index
;
495 draw
->pt
.user
.max_index
= info
->max_index
;
496 draw
->pt
.user
.eltSize
= info
->index_size
? draw
->pt
.user
.eltSizeIB
: 0;
497 draw
->pt
.user
.drawid
= info
->drawid
;
499 draw
->pt
.vertices_per_patch
= info
->vertices_per_patch
;
502 debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
503 info
->mode
, info
->start
, count
);
506 tgsi_dump(draw
->vs
.vertex_shader
->state
.tokens
, 0);
510 debug_printf("Elements:\n");
511 for (i
= 0; i
< draw
->pt
.nr_vertex_elements
; i
++) {
512 debug_printf(" %u: src_offset=%u inst_div=%u vbuf=%u format=%s\n",
514 draw
->pt
.vertex_element
[i
].src_offset
,
515 draw
->pt
.vertex_element
[i
].instance_divisor
,
516 draw
->pt
.vertex_element
[i
].vertex_buffer_index
,
517 util_format_name(draw
->pt
.vertex_element
[i
].src_format
));
519 debug_printf("Buffers:\n");
520 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
521 debug_printf(" %u: stride=%u offset=%u size=%d ptr=%p\n",
523 draw
->pt
.vertex_buffer
[i
].stride
,
524 draw
->pt
.vertex_buffer
[i
].buffer_offset
,
525 (int) draw
->pt
.user
.vbuffer
[i
].size
,
526 draw
->pt
.user
.vbuffer
[i
].map
);
531 draw_print_arrays(draw
, info
->mode
, info
->start
, MIN2(count
, 20));
533 index_limit
= util_draw_max_index(draw
->pt
.vertex_buffer
,
534 draw
->pt
.vertex_element
,
535 draw
->pt
.nr_vertex_elements
,
537 #ifdef LLVM_AVAILABLE
541 if (index_limit
== 0) {
542 /* one of the buffers is too small to do any valid drawing */
543 debug_warning("draw: VBO too small to draw anything\n");
544 util_fpstate_set(fpstate
);
549 /* If we're collecting stats then make sure we start from scratch */
550 if (draw
->collect_statistics
) {
551 memset(&draw
->statistics
, 0, sizeof(draw
->statistics
));
554 draw
->pt
.max_index
= index_limit
- 1;
555 draw
->start_index
= info
->start
;
558 * TODO: We could use draw->pt.max_index to further narrow
559 * the min_index/max_index hints given by the state tracker.
562 for (instance
= 0; instance
< info
->instance_count
; instance
++) {
563 unsigned instance_idx
= instance
+ info
->start_instance
;
564 draw
->start_instance
= info
->start_instance
;
565 draw
->instance_id
= instance
;
566 /* check for overflow */
567 if (instance_idx
< instance
||
568 instance_idx
< draw
->start_instance
) {
569 /* if we overflown just set the instance id to the max */
570 draw
->instance_id
= 0xffffffff;
573 draw_new_instance(draw
);
575 if (info
->primitive_restart
) {
576 draw_pt_arrays_restart(draw
, info
);
579 draw_pt_arrays(draw
, info
->mode
, info
->start
, count
);
583 /* If requested emit the pipeline statistics for this run */
584 if (draw
->collect_statistics
) {
585 draw
->render
->pipeline_statistics(draw
->render
, &draw
->statistics
);
587 util_fpstate_set(fpstate
);