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
.front
.vsplit
;
111 frontend
->prepare( frontend
, prim
, middle
, opt
);
113 frontend
->run(frontend
, start
, count
);
115 frontend
->finish( frontend
);
121 boolean
draw_pt_init( struct draw_context
*draw
)
123 draw
->pt
.test_fse
= debug_get_option_draw_fse();
124 draw
->pt
.no_fse
= debug_get_option_draw_no_fse();
126 draw
->pt
.front
.vsplit
= draw_pt_vsplit(draw
);
127 if (!draw
->pt
.front
.vsplit
)
130 draw
->pt
.middle
.fetch_emit
= draw_pt_fetch_emit( draw
);
131 if (!draw
->pt
.middle
.fetch_emit
)
134 draw
->pt
.middle
.fetch_shade_emit
= draw_pt_middle_fse( draw
);
135 if (!draw
->pt
.middle
.fetch_shade_emit
)
138 draw
->pt
.middle
.general
= draw_pt_fetch_pipeline_or_emit( draw
);
139 if (!draw
->pt
.middle
.general
)
144 draw
->pt
.middle
.llvm
= draw_pt_fetch_pipeline_or_emit_llvm( draw
);
151 void draw_pt_destroy( struct draw_context
*draw
)
153 if (draw
->pt
.middle
.llvm
) {
154 draw
->pt
.middle
.llvm
->destroy( draw
->pt
.middle
.llvm
);
155 draw
->pt
.middle
.llvm
= NULL
;
158 if (draw
->pt
.middle
.general
) {
159 draw
->pt
.middle
.general
->destroy( draw
->pt
.middle
.general
);
160 draw
->pt
.middle
.general
= NULL
;
163 if (draw
->pt
.middle
.fetch_emit
) {
164 draw
->pt
.middle
.fetch_emit
->destroy( draw
->pt
.middle
.fetch_emit
);
165 draw
->pt
.middle
.fetch_emit
= NULL
;
168 if (draw
->pt
.middle
.fetch_shade_emit
) {
169 draw
->pt
.middle
.fetch_shade_emit
->destroy( draw
->pt
.middle
.fetch_shade_emit
);
170 draw
->pt
.middle
.fetch_shade_emit
= NULL
;
173 if (draw
->pt
.front
.vsplit
) {
174 draw
->pt
.front
.vsplit
->destroy( draw
->pt
.front
.vsplit
);
175 draw
->pt
.front
.vsplit
= NULL
;
181 * Debug- print the first 'count' vertices.
184 draw_print_arrays(struct draw_context
*draw
, uint prim
, int start
, uint count
)
188 debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
191 for (i
= 0; i
< count
; i
++) {
195 if (draw
->pt
.user
.eltSize
) {
199 elts
= (const char *) draw
->pt
.user
.elts
;
200 elts
+= draw
->pt
.index_buffer
.offset
;
202 switch (draw
->pt
.user
.eltSize
) {
205 const ubyte
*elem
= (const ubyte
*) elts
;
206 ii
= elem
[start
+ i
];
211 const ushort
*elem
= (const ushort
*) elts
;
212 ii
= elem
[start
+ i
];
217 const uint
*elem
= (const uint
*) elts
;
218 ii
= elem
[start
+ i
];
225 ii
+= draw
->pt
.user
.eltBias
;
226 debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start
, i
,
227 draw
->pt
.user
.eltBias
, ii
);
230 /* non-indexed arrays */
232 debug_printf("Vertex %u:\n", ii
);
235 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; j
++) {
236 uint buf
= draw
->pt
.vertex_element
[j
].vertex_buffer_index
;
237 ubyte
*ptr
= (ubyte
*) draw
->pt
.user
.vbuffer
[buf
];
239 if (draw
->pt
.vertex_element
[j
].instance_divisor
) {
240 ii
= draw
->instance_id
/ draw
->pt
.vertex_element
[j
].instance_divisor
;
243 ptr
+= draw
->pt
.vertex_buffer
[buf
].buffer_offset
;
244 ptr
+= draw
->pt
.vertex_buffer
[buf
].stride
* ii
;
245 ptr
+= draw
->pt
.vertex_element
[j
].src_offset
;
247 debug_printf(" Attr %u: ", j
);
248 switch (draw
->pt
.vertex_element
[j
].src_format
) {
249 case PIPE_FORMAT_R32_FLOAT
:
251 float *v
= (float *) ptr
;
252 debug_printf("R %f @ %p\n", v
[0], (void *) v
);
255 case PIPE_FORMAT_R32G32_FLOAT
:
257 float *v
= (float *) ptr
;
258 debug_printf("RG %f %f @ %p\n", v
[0], v
[1], (void *) v
);
261 case PIPE_FORMAT_R32G32B32_FLOAT
:
263 float *v
= (float *) ptr
;
264 debug_printf("RGB %f %f %f @ %p\n", v
[0], v
[1], v
[2], (void *) v
);
267 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
269 float *v
= (float *) ptr
;
270 debug_printf("RGBA %f %f %f %f @ %p\n", v
[0], v
[1], v
[2], v
[3],
274 case PIPE_FORMAT_B8G8R8A8_UNORM
:
276 ubyte
*u
= (ubyte
*) ptr
;
277 debug_printf("BGRA %d %d %d %d @ %p\n", u
[0], u
[1], u
[2], u
[3],
282 debug_printf("other format %s (fix me)\n",
283 util_format_name(draw
->pt
.vertex_element
[j
].src_format
));
290 /** Helper code for below */
291 #define PRIM_RESTART_LOOP(elements) \
293 for (i = start; i < end; i++) { \
294 if (elements[i] == info->restart_index) { \
295 if (cur_count > 0) { \
296 /* draw elts up to prev pos */ \
297 draw_pt_arrays(draw, prim, cur_start, cur_count); \
299 /* begin new prim at next elt */ \
307 if (cur_count > 0) { \
308 draw_pt_arrays(draw, prim, cur_start, cur_count); \
314 * For drawing prims with primitive restart enabled.
315 * Scan for restart indexes and draw the runs of elements/vertices between
319 draw_pt_arrays_restart(struct draw_context
*draw
,
320 const struct pipe_draw_info
*info
)
322 const unsigned prim
= info
->mode
;
323 const unsigned start
= info
->start
;
324 const unsigned count
= info
->count
;
325 const unsigned end
= start
+ count
;
326 unsigned i
, cur_start
, cur_count
;
328 assert(info
->primitive_restart
);
330 if (draw
->pt
.user
.elts
) {
331 /* indexed prims (draw_elements) */
335 switch (draw
->pt
.user
.eltSize
) {
338 const ubyte
*elt_ub
= (const ubyte
*) draw
->pt
.user
.elts
;
339 PRIM_RESTART_LOOP(elt_ub
);
344 const ushort
*elt_us
= (const ushort
*) draw
->pt
.user
.elts
;
345 PRIM_RESTART_LOOP(elt_us
);
350 const uint
*elt_ui
= (const uint
*) draw
->pt
.user
.elts
;
351 PRIM_RESTART_LOOP(elt_ui
);
355 assert(0 && "bad eltSize in draw_arrays()");
359 /* Non-indexed prims (draw_arrays).
360 * Primitive restart should have been handled in the state tracker.
362 draw_pt_arrays(draw
, prim
, start
, count
);
369 * Non-instanced drawing.
370 * \sa draw_arrays_instanced
373 draw_arrays(struct draw_context
*draw
, unsigned prim
,
374 unsigned start
, unsigned count
)
376 draw_arrays_instanced(draw
, prim
, start
, count
, 0, 1);
385 draw_arrays_instanced(struct draw_context
*draw
,
389 unsigned startInstance
,
390 unsigned instanceCount
)
392 struct pipe_draw_info info
;
394 util_draw_init_info(&info
);
399 info
.start_instance
= startInstance
;
400 info
.instance_count
= instanceCount
;
402 info
.indexed
= (draw
->pt
.user
.elts
!= NULL
);
404 info
.min_index
= start
;
405 info
.max_index
= start
+ count
- 1;
408 draw_vbo(draw
, &info
);
413 * Draw vertex arrays.
414 * This is the main entrypoint into the drawing module. If drawing an indexed
415 * primitive, the draw_set_index_buffer() and draw_set_mapped_index_buffer()
416 * functions should have already been called to specify the element/index
417 * buffer information.
420 draw_vbo(struct draw_context
*draw
,
421 const struct pipe_draw_info
*info
)
423 unsigned reduced_prim
= u_reduced_prim(info
->mode
);
425 unsigned index_limit
;
427 assert(info
->instance_count
> 0);
429 assert(draw
->pt
.user
.elts
);
431 draw
->pt
.user
.eltSize
=
432 (info
->indexed
) ? draw
->pt
.index_buffer
.index_size
: 0;
434 draw
->pt
.user
.eltBias
= info
->index_bias
;
435 draw
->pt
.user
.min_index
= info
->min_index
;
436 draw
->pt
.user
.max_index
= info
->max_index
;
438 if (reduced_prim
!= draw
->reduced_prim
) {
439 draw_do_flush(draw
, DRAW_FLUSH_STATE_CHANGE
);
440 draw
->reduced_prim
= reduced_prim
;
444 debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
445 info
->mode
, info
->start
, info
->count
);
448 tgsi_dump(draw
->vs
.vertex_shader
->state
.tokens
, 0);
452 debug_printf("Elements:\n");
453 for (i
= 0; i
< draw
->pt
.nr_vertex_elements
; i
++) {
454 debug_printf(" %u: src_offset=%u inst_div=%u vbuf=%u format=%s\n",
456 draw
->pt
.vertex_element
[i
].src_offset
,
457 draw
->pt
.vertex_element
[i
].instance_divisor
,
458 draw
->pt
.vertex_element
[i
].vertex_buffer_index
,
459 util_format_name(draw
->pt
.vertex_element
[i
].src_format
));
461 debug_printf("Buffers:\n");
462 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
463 debug_printf(" %u: stride=%u offset=%u ptr=%p\n",
465 draw
->pt
.vertex_buffer
[i
].stride
,
466 draw
->pt
.vertex_buffer
[i
].buffer_offset
,
467 draw
->pt
.user
.vbuffer
[i
]);
472 draw_print_arrays(draw
, info
->mode
, info
->start
, MIN2(info
->count
, 20));
474 index_limit
= util_draw_max_index(draw
->pt
.vertex_buffer
,
475 draw
->pt
.nr_vertex_buffers
,
476 draw
->pt
.vertex_element
,
477 draw
->pt
.nr_vertex_elements
,
480 if (index_limit
== 0) {
481 /* one of the buffers is too small to do any valid drawing */
482 debug_warning("draw: VBO too small to draw anything\n");
486 draw
->pt
.max_index
= index_limit
- 1;
490 * TODO: We could use draw->pt.max_index to further narrow
491 * the min_index/max_index hints given by the state tracker.
494 for (instance
= 0; instance
< info
->instance_count
; instance
++) {
495 draw
->instance_id
= instance
+ info
->start_instance
;
497 if (info
->primitive_restart
) {
498 draw_pt_arrays_restart(draw
, info
);
501 draw_pt_arrays(draw
, info
->mode
, info
->start
, info
->count
);