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_vs.h"
38 #include "tgsi/tgsi_dump.h"
39 #include "util/u_math.h"
40 #include "util/u_prim.h"
41 #include "util/u_format.h"
42 #include "util/u_draw.h"
45 DEBUG_GET_ONCE_BOOL_OPTION(draw_fse
, "DRAW_FSE", FALSE
)
46 DEBUG_GET_ONCE_BOOL_OPTION(draw_no_fse
, "DRAW_NO_FSE", FALSE
)
48 /* Overall we split things into:
49 * - frontend -- prepare fetch_elts, draw_elts - eg vsplit
50 * - middle -- fetch, shade, cliptest, viewport
51 * - pipeline -- the prim pipeline: clipping, wide lines, etc
52 * - backend -- the vbuf_render provided by the driver.
55 draw_pt_arrays(struct draw_context
*draw
,
60 struct draw_pt_front_end
*frontend
= NULL
;
61 struct draw_pt_middle_end
*middle
= NULL
;
64 /* Sanitize primitive length:
68 draw_pt_split_prim(prim
, &first
, &incr
);
69 count
= draw_pt_trim_count(count
, first
, incr
);
74 if (!draw
->force_passthrough
) {
75 unsigned gs_out_prim
= (draw
->gs
.geometry_shader
?
76 draw
->gs
.geometry_shader
->output_primitive
:
83 if (draw_need_pipeline(draw
,
91 draw
->clip_user
) && !draw
->pt
.test_fse
) {
98 if (draw
->pt
.middle
.llvm
) {
99 middle
= draw
->pt
.middle
.llvm
;
102 middle
= draw
->pt
.middle
.fetch_emit
;
103 else if (opt
== PT_SHADE
&& !draw
->pt
.no_fse
)
104 middle
= draw
->pt
.middle
.fetch_shade_emit
;
106 middle
= draw
->pt
.middle
.general
;
109 frontend
= draw
->pt
.frontend
;
112 if (draw
->pt
.prim
!= prim
|| draw
->pt
.opt
!= opt
) {
113 /* In certain conditions switching primitives requires us to flush
114 * and validate the different stages. One example is when smooth
115 * lines are active but first drawn with triangles and then with
118 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
120 } else if (draw
->pt
.eltSize
!= draw
->pt
.user
.eltSize
) {
121 /* Flush draw state if eltSize changed.
122 * This could be improved so only the frontend is flushed since it
123 * converts all indices to ushorts and the fetch part of the middle
124 * always perpares both linear and indexed.
126 frontend
->flush( frontend
, DRAW_FLUSH_STATE_CHANGE
);
132 frontend
= draw
->pt
.front
.vsplit
;
134 frontend
->prepare( frontend
, prim
, middle
, opt
);
136 draw
->pt
.frontend
= frontend
;
137 draw
->pt
.eltSize
= draw
->pt
.user
.eltSize
;
138 draw
->pt
.prim
= prim
;
142 frontend
->run( frontend
, start
, count
);
147 void draw_pt_flush( struct draw_context
*draw
, unsigned flags
)
149 if (draw
->pt
.frontend
) {
150 draw
->pt
.frontend
->flush( draw
->pt
.frontend
, flags
);
152 /* don't prepare if we only are flushing the backend */
153 if (!(flags
& DRAW_FLUSH_BACKEND
))
154 draw
->pt
.frontend
= NULL
;
160 boolean
draw_pt_init( struct draw_context
*draw
)
162 draw
->pt
.test_fse
= debug_get_option_draw_fse();
163 draw
->pt
.no_fse
= debug_get_option_draw_no_fse();
165 draw
->pt
.front
.vsplit
= draw_pt_vsplit(draw
);
166 if (!draw
->pt
.front
.vsplit
)
169 draw
->pt
.middle
.fetch_emit
= draw_pt_fetch_emit( draw
);
170 if (!draw
->pt
.middle
.fetch_emit
)
173 draw
->pt
.middle
.fetch_shade_emit
= draw_pt_middle_fse( draw
);
174 if (!draw
->pt
.middle
.fetch_shade_emit
)
177 draw
->pt
.middle
.general
= draw_pt_fetch_pipeline_or_emit( draw
);
178 if (!draw
->pt
.middle
.general
)
183 draw
->pt
.middle
.llvm
= draw_pt_fetch_pipeline_or_emit_llvm( draw
);
190 void draw_pt_destroy( struct draw_context
*draw
)
192 if (draw
->pt
.middle
.llvm
) {
193 draw
->pt
.middle
.llvm
->destroy( draw
->pt
.middle
.llvm
);
194 draw
->pt
.middle
.llvm
= NULL
;
197 if (draw
->pt
.middle
.general
) {
198 draw
->pt
.middle
.general
->destroy( draw
->pt
.middle
.general
);
199 draw
->pt
.middle
.general
= NULL
;
202 if (draw
->pt
.middle
.fetch_emit
) {
203 draw
->pt
.middle
.fetch_emit
->destroy( draw
->pt
.middle
.fetch_emit
);
204 draw
->pt
.middle
.fetch_emit
= NULL
;
207 if (draw
->pt
.middle
.fetch_shade_emit
) {
208 draw
->pt
.middle
.fetch_shade_emit
->destroy( draw
->pt
.middle
.fetch_shade_emit
);
209 draw
->pt
.middle
.fetch_shade_emit
= NULL
;
212 if (draw
->pt
.front
.vsplit
) {
213 draw
->pt
.front
.vsplit
->destroy( draw
->pt
.front
.vsplit
);
214 draw
->pt
.front
.vsplit
= NULL
;
220 * Debug- print the first 'count' vertices.
223 draw_print_arrays(struct draw_context
*draw
, uint prim
, int start
, uint count
)
227 debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
230 for (i
= 0; i
< count
; i
++) {
234 if (draw
->pt
.user
.eltSize
) {
238 elts
= (const char *) draw
->pt
.user
.elts
;
239 elts
+= draw
->pt
.index_buffer
.offset
;
241 switch (draw
->pt
.user
.eltSize
) {
244 const ubyte
*elem
= (const ubyte
*) elts
;
245 ii
= elem
[start
+ i
];
250 const ushort
*elem
= (const ushort
*) elts
;
251 ii
= elem
[start
+ i
];
256 const uint
*elem
= (const uint
*) elts
;
257 ii
= elem
[start
+ i
];
264 ii
+= draw
->pt
.user
.eltBias
;
265 debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start
, i
,
266 draw
->pt
.user
.eltBias
, ii
);
269 /* non-indexed arrays */
271 debug_printf("Vertex %u:\n", ii
);
274 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; j
++) {
275 uint buf
= draw
->pt
.vertex_element
[j
].vertex_buffer_index
;
276 ubyte
*ptr
= (ubyte
*) draw
->pt
.user
.vbuffer
[buf
];
278 if (draw
->pt
.vertex_element
[j
].instance_divisor
) {
279 ii
= draw
->instance_id
/ draw
->pt
.vertex_element
[j
].instance_divisor
;
282 ptr
+= draw
->pt
.vertex_buffer
[buf
].buffer_offset
;
283 ptr
+= draw
->pt
.vertex_buffer
[buf
].stride
* ii
;
284 ptr
+= draw
->pt
.vertex_element
[j
].src_offset
;
286 debug_printf(" Attr %u: ", j
);
287 switch (draw
->pt
.vertex_element
[j
].src_format
) {
288 case PIPE_FORMAT_R32_FLOAT
:
290 float *v
= (float *) ptr
;
291 debug_printf("R %f @ %p\n", v
[0], (void *) v
);
294 case PIPE_FORMAT_R32G32_FLOAT
:
296 float *v
= (float *) ptr
;
297 debug_printf("RG %f %f @ %p\n", v
[0], v
[1], (void *) v
);
300 case PIPE_FORMAT_R32G32B32_FLOAT
:
302 float *v
= (float *) ptr
;
303 debug_printf("RGB %f %f %f @ %p\n", v
[0], v
[1], v
[2], (void *) v
);
306 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
308 float *v
= (float *) ptr
;
309 debug_printf("RGBA %f %f %f %f @ %p\n", v
[0], v
[1], v
[2], v
[3],
313 case PIPE_FORMAT_B8G8R8A8_UNORM
:
315 ubyte
*u
= (ubyte
*) ptr
;
316 debug_printf("BGRA %d %d %d %d @ %p\n", u
[0], u
[1], u
[2], u
[3],
321 debug_printf("other format %s (fix me)\n",
322 util_format_name(draw
->pt
.vertex_element
[j
].src_format
));
329 /** Helper code for below */
330 #define PRIM_RESTART_LOOP(elements) \
332 for (i = start; i < end; i++) { \
333 if (elements[i] == info->restart_index) { \
334 if (cur_count > 0) { \
335 /* draw elts up to prev pos */ \
336 draw_pt_arrays(draw, prim, cur_start, cur_count); \
338 /* begin new prim at next elt */ \
346 if (cur_count > 0) { \
347 draw_pt_arrays(draw, prim, cur_start, cur_count); \
353 * For drawing prims with primitive restart enabled.
354 * Scan for restart indexes and draw the runs of elements/vertices between
358 draw_pt_arrays_restart(struct draw_context
*draw
,
359 const struct pipe_draw_info
*info
)
361 const unsigned prim
= info
->mode
;
362 const unsigned start
= info
->start
;
363 const unsigned count
= info
->count
;
364 const unsigned end
= start
+ count
;
365 unsigned i
, cur_start
, cur_count
;
367 assert(info
->primitive_restart
);
369 if (draw
->pt
.user
.elts
) {
370 /* indexed prims (draw_elements) */
374 switch (draw
->pt
.user
.eltSize
) {
377 const ubyte
*elt_ub
= (const ubyte
*) draw
->pt
.user
.elts
;
378 PRIM_RESTART_LOOP(elt_ub
);
383 const ushort
*elt_us
= (const ushort
*) draw
->pt
.user
.elts
;
384 PRIM_RESTART_LOOP(elt_us
);
389 const uint
*elt_ui
= (const uint
*) draw
->pt
.user
.elts
;
390 PRIM_RESTART_LOOP(elt_ui
);
394 assert(0 && "bad eltSize in draw_arrays()");
398 /* Non-indexed prims (draw_arrays).
399 * Primitive restart should have been handled in the state tracker.
401 draw_pt_arrays(draw
, prim
, start
, count
);
408 * Non-instanced drawing.
409 * \sa draw_arrays_instanced
412 draw_arrays(struct draw_context
*draw
, unsigned prim
,
413 unsigned start
, unsigned count
)
415 draw_arrays_instanced(draw
, prim
, start
, count
, 0, 1);
424 draw_arrays_instanced(struct draw_context
*draw
,
428 unsigned startInstance
,
429 unsigned instanceCount
)
431 struct pipe_draw_info info
;
433 util_draw_init_info(&info
);
438 info
.start_instance
= startInstance
;
439 info
.instance_count
= instanceCount
;
441 info
.indexed
= (draw
->pt
.user
.elts
!= NULL
);
443 info
.min_index
= start
;
444 info
.max_index
= start
+ count
- 1;
447 draw_vbo(draw
, &info
);
452 * Draw vertex arrays.
453 * This is the main entrypoint into the drawing module. If drawing an indexed
454 * primitive, the draw_set_index_buffer() and draw_set_mapped_index_buffer()
455 * functions should have already been called to specify the element/index
456 * buffer information.
459 draw_vbo(struct draw_context
*draw
,
460 const struct pipe_draw_info
*info
)
463 unsigned index_limit
;
465 assert(info
->instance_count
> 0);
467 assert(draw
->pt
.user
.elts
);
469 draw
->pt
.user
.eltSize
=
470 (info
->indexed
) ? draw
->pt
.index_buffer
.index_size
: 0;
472 draw
->pt
.user
.eltBias
= info
->index_bias
;
473 draw
->pt
.user
.min_index
= info
->min_index
;
474 draw
->pt
.user
.max_index
= info
->max_index
;
477 debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
478 info
->mode
, info
->start
, info
->count
);
481 tgsi_dump(draw
->vs
.vertex_shader
->state
.tokens
, 0);
485 debug_printf("Elements:\n");
486 for (i
= 0; i
< draw
->pt
.nr_vertex_elements
; i
++) {
487 debug_printf(" %u: src_offset=%u inst_div=%u vbuf=%u format=%s\n",
489 draw
->pt
.vertex_element
[i
].src_offset
,
490 draw
->pt
.vertex_element
[i
].instance_divisor
,
491 draw
->pt
.vertex_element
[i
].vertex_buffer_index
,
492 util_format_name(draw
->pt
.vertex_element
[i
].src_format
));
494 debug_printf("Buffers:\n");
495 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
496 debug_printf(" %u: stride=%u offset=%u ptr=%p\n",
498 draw
->pt
.vertex_buffer
[i
].stride
,
499 draw
->pt
.vertex_buffer
[i
].buffer_offset
,
500 draw
->pt
.user
.vbuffer
[i
]);
505 draw_print_arrays(draw
, info
->mode
, info
->start
, MIN2(info
->count
, 20));
507 index_limit
= util_draw_max_index(draw
->pt
.vertex_buffer
,
508 draw
->pt
.nr_vertex_buffers
,
509 draw
->pt
.vertex_element
,
510 draw
->pt
.nr_vertex_elements
,
513 if (index_limit
== 0) {
514 /* one of the buffers is too small to do any valid drawing */
515 debug_warning("draw: VBO too small to draw anything\n");
519 draw
->pt
.max_index
= index_limit
- 1;
523 * TODO: We could use draw->pt.max_index to further narrow
524 * the min_index/max_index hints given by the state tracker.
527 for (instance
= 0; instance
< info
->instance_count
; instance
++) {
528 draw
->instance_id
= instance
+ info
->start_instance
;
530 if (info
->primitive_restart
) {
531 draw_pt_arrays_restart(draw
, info
);
534 draw_pt_arrays(draw
, info
->mode
, info
->start
, info
->count
);