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
) {
237 switch (draw
->pt
.user
.eltSize
) {
240 const ubyte
*elem
= (const ubyte
*) draw
->pt
.user
.elts
;
241 ii
= elem
[start
+ i
];
246 const ushort
*elem
= (const ushort
*) draw
->pt
.user
.elts
;
247 ii
= elem
[start
+ i
];
252 const uint
*elem
= (const uint
*) draw
->pt
.user
.elts
;
253 ii
= elem
[start
+ i
];
260 ii
+= draw
->pt
.user
.eltBias
;
261 debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start
, i
,
262 draw
->pt
.user
.eltBias
, ii
);
265 /* non-indexed arrays */
267 debug_printf("Vertex %u:\n", ii
);
270 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; j
++) {
271 uint buf
= draw
->pt
.vertex_element
[j
].vertex_buffer_index
;
272 ubyte
*ptr
= (ubyte
*) draw
->pt
.user
.vbuffer
[buf
];
274 if (draw
->pt
.vertex_element
[j
].instance_divisor
) {
275 ii
= draw
->instance_id
/ draw
->pt
.vertex_element
[j
].instance_divisor
;
278 ptr
+= draw
->pt
.vertex_buffer
[buf
].buffer_offset
;
279 ptr
+= draw
->pt
.vertex_buffer
[buf
].stride
* ii
;
280 ptr
+= draw
->pt
.vertex_element
[j
].src_offset
;
282 debug_printf(" Attr %u: ", j
);
283 switch (draw
->pt
.vertex_element
[j
].src_format
) {
284 case PIPE_FORMAT_R32_FLOAT
:
286 float *v
= (float *) ptr
;
287 debug_printf("R %f @ %p\n", v
[0], (void *) v
);
290 case PIPE_FORMAT_R32G32_FLOAT
:
292 float *v
= (float *) ptr
;
293 debug_printf("RG %f %f @ %p\n", v
[0], v
[1], (void *) v
);
296 case PIPE_FORMAT_R32G32B32_FLOAT
:
298 float *v
= (float *) ptr
;
299 debug_printf("RGB %f %f %f @ %p\n", v
[0], v
[1], v
[2], (void *) v
);
302 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
304 float *v
= (float *) ptr
;
305 debug_printf("RGBA %f %f %f %f @ %p\n", v
[0], v
[1], v
[2], v
[3],
309 case PIPE_FORMAT_B8G8R8A8_UNORM
:
311 ubyte
*u
= (ubyte
*) ptr
;
312 debug_printf("BGRA %d %d %d %d @ %p\n", u
[0], u
[1], u
[2], u
[3],
317 debug_printf("other format %s (fix me)\n",
318 util_format_name(draw
->pt
.vertex_element
[j
].src_format
));
325 /** Helper code for below */
326 #define PRIM_RESTART_LOOP(elements) \
328 for (i = start; i < end; i++) { \
329 if (elements[i] == info->restart_index) { \
330 if (cur_count > 0) { \
331 /* draw elts up to prev pos */ \
332 draw_pt_arrays(draw, prim, cur_start, cur_count); \
334 /* begin new prim at next elt */ \
342 if (cur_count > 0) { \
343 draw_pt_arrays(draw, prim, cur_start, cur_count); \
349 * For drawing prims with primitive restart enabled.
350 * Scan for restart indexes and draw the runs of elements/vertices between
354 draw_pt_arrays_restart(struct draw_context
*draw
,
355 const struct pipe_draw_info
*info
)
357 const unsigned prim
= info
->mode
;
358 const unsigned start
= info
->start
;
359 const unsigned count
= info
->count
;
360 const unsigned end
= start
+ count
;
361 unsigned i
, cur_start
, cur_count
;
363 assert(info
->primitive_restart
);
365 if (draw
->pt
.user
.elts
) {
366 /* indexed prims (draw_elements) */
370 switch (draw
->pt
.user
.eltSize
) {
373 const ubyte
*elt_ub
= (const ubyte
*) draw
->pt
.user
.elts
;
374 PRIM_RESTART_LOOP(elt_ub
);
379 const ushort
*elt_us
= (const ushort
*) draw
->pt
.user
.elts
;
380 PRIM_RESTART_LOOP(elt_us
);
385 const uint
*elt_ui
= (const uint
*) draw
->pt
.user
.elts
;
386 PRIM_RESTART_LOOP(elt_ui
);
390 assert(0 && "bad eltSize in draw_arrays()");
394 /* Non-indexed prims (draw_arrays).
395 * Primitive restart should have been handled in the state tracker.
397 draw_pt_arrays(draw
, prim
, start
, count
);
404 * Non-instanced drawing.
405 * \sa draw_arrays_instanced
408 draw_arrays(struct draw_context
*draw
, unsigned prim
,
409 unsigned start
, unsigned count
)
411 draw_arrays_instanced(draw
, prim
, start
, count
, 0, 1);
420 draw_arrays_instanced(struct draw_context
*draw
,
424 unsigned startInstance
,
425 unsigned instanceCount
)
427 struct pipe_draw_info info
;
429 util_draw_init_info(&info
);
434 info
.start_instance
= startInstance
;
435 info
.instance_count
= instanceCount
;
437 info
.indexed
= (draw
->pt
.user
.elts
!= NULL
);
439 info
.min_index
= start
;
440 info
.max_index
= start
+ count
- 1;
443 draw_vbo(draw
, &info
);
448 * Draw vertex arrays.
449 * This is the main entrypoint into the drawing module. If drawing an indexed
450 * primitive, the draw_set_index_buffer() and draw_set_mapped_index_buffer()
451 * functions should have already been called to specify the element/index
452 * buffer information.
455 draw_vbo(struct draw_context
*draw
,
456 const struct pipe_draw_info
*info
)
459 unsigned index_limit
;
461 assert(info
->instance_count
> 0);
463 assert(draw
->pt
.user
.elts
);
465 draw
->pt
.user
.eltBias
= info
->index_bias
;
466 draw
->pt
.user
.min_index
= info
->min_index
;
467 draw
->pt
.user
.max_index
= info
->max_index
;
470 debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
471 info
->mode
, info
->start
, info
->count
);
474 tgsi_dump(draw
->vs
.vertex_shader
->state
.tokens
, 0);
478 debug_printf("Elements:\n");
479 for (i
= 0; i
< draw
->pt
.nr_vertex_elements
; i
++) {
480 debug_printf(" %u: src_offset=%u inst_div=%u vbuf=%u format=%s\n",
482 draw
->pt
.vertex_element
[i
].src_offset
,
483 draw
->pt
.vertex_element
[i
].instance_divisor
,
484 draw
->pt
.vertex_element
[i
].vertex_buffer_index
,
485 util_format_name(draw
->pt
.vertex_element
[i
].src_format
));
487 debug_printf("Buffers:\n");
488 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
489 debug_printf(" %u: stride=%u offset=%u ptr=%p\n",
491 draw
->pt
.vertex_buffer
[i
].stride
,
492 draw
->pt
.vertex_buffer
[i
].buffer_offset
,
493 draw
->pt
.user
.vbuffer
[i
]);
498 draw_print_arrays(draw
, info
->mode
, info
->start
, MIN2(info
->count
, 20));
500 index_limit
= util_draw_max_index(draw
->pt
.vertex_buffer
,
501 draw
->pt
.vertex_element
,
502 draw
->pt
.nr_vertex_elements
,
505 if (index_limit
== 0) {
506 /* one of the buffers is too small to do any valid drawing */
507 debug_warning("draw: VBO too small to draw anything\n");
511 draw
->pt
.max_index
= index_limit
- 1;
515 * TODO: We could use draw->pt.max_index to further narrow
516 * the min_index/max_index hints given by the state tracker.
519 for (instance
= 0; instance
< info
->instance_count
; instance
++) {
520 draw
->instance_id
= instance
+ info
->start_instance
;
522 if (info
->primitive_restart
) {
523 draw_pt_arrays_restart(draw
, info
);
526 draw_pt_arrays(draw
, info
->mode
, info
->start
, info
->count
);