1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
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"
46 DEBUG_GET_ONCE_BOOL_OPTION(draw_fse
, "DRAW_FSE", FALSE
)
47 DEBUG_GET_ONCE_BOOL_OPTION(draw_no_fse
, "DRAW_NO_FSE", FALSE
)
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.
56 draw_pt_arrays(struct draw_context
*draw
,
61 struct draw_pt_front_end
*frontend
= NULL
;
62 struct draw_pt_middle_end
*middle
= NULL
;
65 /* Sanitize primitive length:
69 draw_pt_split_prim(prim
, &first
, &incr
);
70 count
= draw_pt_trim_count(count
, first
, incr
);
75 if (!draw
->force_passthrough
) {
76 unsigned gs_out_prim
= (draw
->gs
.geometry_shader
?
77 draw
->gs
.geometry_shader
->output_primitive
:
84 if (draw_need_pipeline(draw
,
92 draw
->clip_user
) && !draw
->pt
.test_fse
) {
99 if (draw
->pt
.middle
.llvm
) {
100 middle
= draw
->pt
.middle
.llvm
;
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
;
107 middle
= draw
->pt
.middle
.general
;
110 frontend
= draw
->pt
.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
119 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
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.
127 frontend
->flush( frontend
, DRAW_FLUSH_STATE_CHANGE
);
133 frontend
= draw
->pt
.front
.vsplit
;
135 frontend
->prepare( frontend
, prim
, middle
, opt
);
137 draw
->pt
.frontend
= frontend
;
138 draw
->pt
.eltSize
= draw
->pt
.user
.eltSize
;
139 draw
->pt
.prim
= prim
;
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
;
149 frontend
->run( frontend
, start
, count
);
154 void draw_pt_flush( struct draw_context
*draw
, unsigned flags
)
158 if (draw
->pt
.frontend
) {
159 draw
->pt
.frontend
->flush( draw
->pt
.frontend
, flags
);
161 /* don't prepare if we only are flushing the backend */
162 if (flags
& DRAW_FLUSH_STATE_CHANGE
)
163 draw
->pt
.frontend
= NULL
;
166 if (flags
& DRAW_FLUSH_PARAMETER_CHANGE
) {
167 draw
->pt
.rebind_parameters
= TRUE
;
173 boolean
draw_pt_init( struct draw_context
*draw
)
175 draw
->pt
.test_fse
= debug_get_option_draw_fse();
176 draw
->pt
.no_fse
= debug_get_option_draw_no_fse();
178 draw
->pt
.front
.vsplit
= draw_pt_vsplit(draw
);
179 if (!draw
->pt
.front
.vsplit
)
182 draw
->pt
.middle
.fetch_emit
= draw_pt_fetch_emit( draw
);
183 if (!draw
->pt
.middle
.fetch_emit
)
186 draw
->pt
.middle
.fetch_shade_emit
= draw_pt_middle_fse( draw
);
187 if (!draw
->pt
.middle
.fetch_shade_emit
)
190 draw
->pt
.middle
.general
= draw_pt_fetch_pipeline_or_emit( draw
);
191 if (!draw
->pt
.middle
.general
)
196 draw
->pt
.middle
.llvm
= draw_pt_fetch_pipeline_or_emit_llvm( draw
);
203 void draw_pt_destroy( struct draw_context
*draw
)
205 if (draw
->pt
.middle
.llvm
) {
206 draw
->pt
.middle
.llvm
->destroy( draw
->pt
.middle
.llvm
);
207 draw
->pt
.middle
.llvm
= NULL
;
210 if (draw
->pt
.middle
.general
) {
211 draw
->pt
.middle
.general
->destroy( draw
->pt
.middle
.general
);
212 draw
->pt
.middle
.general
= NULL
;
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
;
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
;
225 if (draw
->pt
.front
.vsplit
) {
226 draw
->pt
.front
.vsplit
->destroy( draw
->pt
.front
.vsplit
);
227 draw
->pt
.front
.vsplit
= NULL
;
233 * Debug- print the first 'count' vertices.
236 draw_print_arrays(struct draw_context
*draw
, uint prim
, int start
, uint count
)
240 debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
243 for (i
= 0; i
< count
; i
++) {
247 if (draw
->pt
.user
.eltSize
) {
250 switch (draw
->pt
.user
.eltSize
) {
253 const ubyte
*elem
= (const ubyte
*) draw
->pt
.user
.elts
;
254 ii
= elem
[start
+ i
];
259 const ushort
*elem
= (const ushort
*) draw
->pt
.user
.elts
;
260 ii
= elem
[start
+ i
];
265 const uint
*elem
= (const uint
*) draw
->pt
.user
.elts
;
266 ii
= elem
[start
+ i
];
273 ii
+= draw
->pt
.user
.eltBias
;
274 debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start
, i
,
275 draw
->pt
.user
.eltBias
, ii
);
278 /* non-indexed arrays */
280 debug_printf("Vertex %u:\n", ii
);
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
;
287 if (draw
->pt
.vertex_element
[j
].instance_divisor
) {
288 ii
= draw
->instance_id
/ draw
->pt
.vertex_element
[j
].instance_divisor
;
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
;
295 debug_printf(" Attr %u: ", j
);
296 switch (draw
->pt
.vertex_element
[j
].src_format
) {
297 case PIPE_FORMAT_R32_FLOAT
:
299 float *v
= (float *) ptr
;
300 debug_printf("R %f @ %p\n", v
[0], (void *) v
);
303 case PIPE_FORMAT_R32G32_FLOAT
:
305 float *v
= (float *) ptr
;
306 debug_printf("RG %f %f @ %p\n", v
[0], v
[1], (void *) v
);
309 case PIPE_FORMAT_R32G32B32_FLOAT
:
311 float *v
= (float *) ptr
;
312 debug_printf("RGB %f %f %f @ %p\n", v
[0], v
[1], v
[2], (void *) v
);
315 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
317 float *v
= (float *) ptr
;
318 debug_printf("RGBA %f %f %f %f @ %p\n", v
[0], v
[1], v
[2], v
[3],
322 case PIPE_FORMAT_B8G8R8A8_UNORM
:
324 ubyte
*u
= (ubyte
*) ptr
;
325 debug_printf("BGRA %d %d %d %d @ %p\n", u
[0], u
[1], u
[2], u
[3],
329 case PIPE_FORMAT_A8R8G8B8_UNORM
:
331 ubyte
*u
= (ubyte
*) ptr
;
332 debug_printf("ARGB %d %d %d %d @ %p\n", u
[0], u
[1], u
[2], u
[3],
337 debug_printf("other format %s (fix me)\n",
338 util_format_name(draw
->pt
.vertex_element
[j
].src_format
));
345 /** Helper code for below */
346 #define PRIM_RESTART_LOOP(elements) \
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); \
354 /* begin new prim at next elt */ \
362 if (cur_count > 0) { \
363 draw_pt_arrays(draw, prim, cur_start, cur_count); \
369 * For drawing prims with primitive restart enabled.
370 * Scan for restart indexes and draw the runs of elements/vertices between
374 draw_pt_arrays_restart(struct draw_context
*draw
,
375 const struct pipe_draw_info
*info
)
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
;
384 assert(info
->primitive_restart
);
386 if (draw
->pt
.user
.eltSize
) {
387 /* indexed prims (draw_elements) */
391 switch (draw
->pt
.user
.eltSize
) {
394 const ubyte
*elt_ub
= (const ubyte
*) draw
->pt
.user
.elts
;
395 PRIM_RESTART_LOOP(elt_ub
);
400 const ushort
*elt_us
= (const ushort
*) draw
->pt
.user
.elts
;
401 PRIM_RESTART_LOOP(elt_us
);
406 const uint
*elt_ui
= (const uint
*) draw
->pt
.user
.elts
;
407 PRIM_RESTART_LOOP(elt_ui
);
411 assert(0 && "bad eltSize in draw_arrays()");
415 /* Non-indexed prims (draw_arrays).
416 * Primitive restart should have been handled in the state tracker.
418 draw_pt_arrays(draw
, prim
, start
, count
);
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.
430 resolve_draw_info(const struct pipe_draw_info
*raw_info
,
431 struct pipe_draw_info
*info
)
433 memcpy(info
, raw_info
, sizeof(struct pipe_draw_info
));
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
;
440 /* Stream output draw can not be indexed */
441 debug_assert(!info
->indexed
);
442 info
->max_index
= info
->count
- 1;
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.
453 draw_vbo(struct draw_context
*draw
,
454 const struct pipe_draw_info
*info
)
457 unsigned index_limit
;
459 struct pipe_draw_info resolved_info
;
461 resolve_draw_info(info
, &resolved_info
);
462 info
= &resolved_info
;
464 assert(info
->instance_count
> 0);
466 assert(draw
->pt
.user
.elts
);
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;
476 debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
477 info
->mode
, info
->start
, count
);
480 tgsi_dump(draw
->vs
.vertex_shader
->state
.tokens
, 0);
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",
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
));
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",
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
);
505 draw_print_arrays(draw
, info
->mode
, info
->start
, MIN2(count
, 20));
507 index_limit
= util_draw_max_index(draw
->pt
.vertex_buffer
,
508 draw
->pt
.vertex_element
,
509 draw
->pt
.nr_vertex_elements
,
512 if (index_limit
== 0) {
513 /* one of the buffers is too small to do any valid drawing */
514 debug_warning("draw: VBO too small to draw anything\n");
518 /* If we're collecting stats then make sure we start from scratch */
519 if (draw
->collect_statistics
) {
520 memset(&draw
->statistics
, 0, sizeof(draw
->statistics
));
523 draw
->pt
.max_index
= index_limit
- 1;
526 * TODO: We could use draw->pt.max_index to further narrow
527 * the min_index/max_index hints given by the state tracker.
530 for (instance
= 0; instance
< info
->instance_count
; instance
++) {
531 draw
->instance_id
= instance
+ info
->start_instance
;
533 draw_new_instance(draw
);
535 if (info
->primitive_restart
) {
536 draw_pt_arrays_restart(draw
, info
);
539 draw_pt_arrays(draw
, info
->mode
, info
->start
, count
);
543 /* If requested emit the pipeline statistics for this run */
544 if (draw
->collect_statistics
) {
545 draw
->render
->pipeline_statistics(draw
->render
, &draw
->statistics
);