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,
59
bool index_bias_varies,
60
const struct pipe_draw_start_count_bias *draw_info,
63
struct draw_pt_front_end *frontend = NULL;
64
struct draw_pt_middle_end *middle = NULL;
65
unsigned opt = PT_SHADE;
67
unsigned out_prim = prim;
69
if (draw->gs.geometry_shader)
70
out_prim = draw->gs.geometry_shader->output_primitive;
71
else if (draw->tes.tess_eval_shader)
72
out_prim = get_tes_output_prim(draw->tes.tess_eval_shader);
78
if (draw_need_pipeline(draw,
86
draw->clip_user) && !draw->pt.test_fse) {
90
if (draw->pt.middle.llvm) {
91
middle = draw->pt.middle.llvm;
93
if (opt == PT_SHADE && !draw->pt.no_fse)
94
middle = draw->pt.middle.fetch_shade_emit;
96
middle = draw->pt.middle.general;
99
frontend = draw->pt.frontend;
102
if (draw->pt.prim != prim || draw->pt.opt != opt) {
103
/* In certain conditions switching primitives requires us to flush
104
* and validate the different stages. One example is when smooth
105
* lines are active but first drawn with triangles and then with
108
draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
110
} else if (draw->pt.eltSize != draw->pt.user.eltSize) {
111
/* Flush draw state if eltSize changed.
112
* This could be improved so only the frontend is flushed since it
113
* converts all indices to ushorts and the fetch part of the middle
114
* always prepares both linear and indexed.
116
frontend->flush( frontend, DRAW_FLUSH_STATE_CHANGE );
122
frontend = draw->pt.front.vsplit;
124
frontend->prepare( frontend, prim, middle, opt );
126
draw->pt.frontend = frontend;
127
draw->pt.eltSize = draw->pt.user.eltSize;
128
draw->pt.prim = prim;
132
if (draw->pt.rebind_parameters) {
133
/* update constants, viewport dims, clip planes, etc */
134
middle->bind_parameters(middle);
135
draw->pt.rebind_parameters = FALSE;
139
for (unsigned i = 0; i < num_draws; i++) {
140
unsigned count = draw_info[i].count;
141
/* Sanitize primitive length:
143
unsigned first, incr;
145
if (prim == PIPE_PRIM_PATCHES) {
146
first = draw->pt.vertices_per_patch;
147
incr = draw->pt.vertices_per_patch;
149
draw_pt_split_prim(prim, &first, &incr);
150
count = draw_pt_trim_count(draw_info[i].count, first, incr);
151
draw->pt.user.eltBias = draw->pt.user.eltSize ?
152
(index_bias_varies ? draw_info[i].index_bias : draw_info[0].index_bias) :
155
frontend->run( frontend, draw_info[i].start, count );
157
if (draw->pt.user.increment_draw_id)
158
draw->pt.user.drawid++;
164
void draw_pt_flush( struct draw_context *draw, unsigned flags )
168
if (draw->pt.frontend) {
169
draw->pt.frontend->flush( draw->pt.frontend, flags );
171
/* don't prepare if we only are flushing the backend */
172
if (flags & DRAW_FLUSH_STATE_CHANGE)
173
draw->pt.frontend = NULL;
176
if (flags & DRAW_FLUSH_PARAMETER_CHANGE) {
177
draw->pt.rebind_parameters = TRUE;
183
boolean draw_pt_init( struct draw_context *draw )
185
draw->pt.test_fse = debug_get_option_draw_fse();
186
draw->pt.no_fse = debug_get_option_draw_no_fse();
188
draw->pt.front.vsplit = draw_pt_vsplit(draw);
189
if (!draw->pt.front.vsplit)
192
draw->pt.middle.fetch_shade_emit = draw_pt_middle_fse( draw );
193
if (!draw->pt.middle.fetch_shade_emit)
196
draw->pt.middle.general = draw_pt_fetch_pipeline_or_emit( draw );
197
if (!draw->pt.middle.general)
200
#ifdef DRAW_LLVM_AVAILABLE
202
draw->pt.middle.llvm = draw_pt_fetch_pipeline_or_emit_llvm( draw );
209
void draw_pt_destroy( struct draw_context *draw )
211
if (draw->pt.middle.llvm) {
212
draw->pt.middle.llvm->destroy( draw->pt.middle.llvm );
213
draw->pt.middle.llvm = NULL;
216
if (draw->pt.middle.general) {
217
draw->pt.middle.general->destroy( draw->pt.middle.general );
218
draw->pt.middle.general = NULL;
221
if (draw->pt.middle.fetch_shade_emit) {
222
draw->pt.middle.fetch_shade_emit->destroy( draw->pt.middle.fetch_shade_emit );
223
draw->pt.middle.fetch_shade_emit = NULL;
226
if (draw->pt.front.vsplit) {
227
draw->pt.front.vsplit->destroy( draw->pt.front.vsplit );
228
draw->pt.front.vsplit = NULL;
234
* Debug- print the first 'count' vertices.
237
draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count, int index_bias)
241
debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
244
for (i = 0; i < count; i++) {
248
if (draw->pt.user.eltSize) {
251
switch (draw->pt.user.eltSize) {
254
const ubyte *elem = (const ubyte *) draw->pt.user.elts;
255
ii = elem[start + i];
260
const ushort *elem = (const ushort *) draw->pt.user.elts;
261
ii = elem[start + i];
266
const uint *elem = (const uint *) draw->pt.user.elts;
267
ii = elem[start + i];
275
debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start, i,
279
/* non-indexed arrays */
281
debug_printf("Vertex %u:\n", ii);
284
for (j = 0; j < draw->pt.nr_vertex_elements; j++) {
285
uint buf = draw->pt.vertex_element[j].vertex_buffer_index;
286
ubyte *ptr = (ubyte *) draw->pt.user.vbuffer[buf].map;
288
if (draw->pt.vertex_element[j].instance_divisor) {
289
ii = draw->instance_id / draw->pt.vertex_element[j].instance_divisor;
292
ptr += draw->pt.vertex_buffer[buf].buffer_offset;
293
ptr += draw->pt.vertex_buffer[buf].stride * ii;
294
ptr += draw->pt.vertex_element[j].src_offset;
296
debug_printf(" Attr %u: ", j);
297
switch (draw->pt.vertex_element[j].src_format) {
298
case PIPE_FORMAT_R32_FLOAT:
300
float *v = (float *) ptr;
301
debug_printf("R %f @ %p\n", v[0], (void *) v);
304
case PIPE_FORMAT_R32G32_FLOAT:
306
float *v = (float *) ptr;
307
debug_printf("RG %f %f @ %p\n", v[0], v[1], (void *) v);
310
case PIPE_FORMAT_R32G32B32_FLOAT:
312
float *v = (float *) ptr;
313
debug_printf("RGB %f %f %f @ %p\n", v[0], v[1], v[2], (void *) v);
316
case PIPE_FORMAT_R32G32B32A32_FLOAT:
318
float *v = (float *) ptr;
319
debug_printf("RGBA %f %f %f %f @ %p\n", v[0], v[1], v[2], v[3],
323
case PIPE_FORMAT_B8G8R8A8_UNORM:
325
ubyte *u = (ubyte *) ptr;
326
debug_printf("BGRA %d %d %d %d @ %p\n", u[0], u[1], u[2], u[3],
330
case PIPE_FORMAT_A8R8G8B8_UNORM:
332
ubyte *u = (ubyte *) ptr;
333
debug_printf("ARGB %d %d %d %d @ %p\n", u[0], u[1], u[2], u[3],
338
debug_printf("other format %s (fix me)\n",
339
util_format_name(draw->pt.vertex_element[j].src_format));
346
/** Helper code for below */
348
prim_restart_loop(struct draw_context *draw,
349
const struct pipe_draw_info *info,
350
const struct pipe_draw_start_count_bias *draw_info,
351
const void *elements)
353
const unsigned elt_max = draw->pt.user.eltMax;
354
struct pipe_draw_start_count_bias cur = *draw_info;
357
/* The largest index within a loop using the i variable as the index.
358
* Used for overflow detection */
359
const unsigned MAX_LOOP_IDX = 0xffffffff;
361
for (unsigned j = 0; j < draw_info->count; j++) {
362
unsigned restart_idx = 0;
363
unsigned i = draw_overflow_uadd(draw_info->start, j, MAX_LOOP_IDX);
364
switch (draw->pt.user.eltSize) {
366
restart_idx = ((const uint8_t*)elements)[i];
369
restart_idx = ((const uint16_t*)elements)[i];
372
restart_idx = ((const uint32_t*)elements)[i];
375
assert(0 && "bad eltSize in draw_arrays()");
378
if (i < elt_max && restart_idx == info->restart_index) {
380
/* draw elts up to prev pos */
381
draw_pt_arrays(draw, info->mode, info->index_bias_varies, &cur, 1);
383
/* begin new prim at next elt */
392
draw_pt_arrays(draw, info->mode, info->index_bias_varies, &cur, 1);
397
* For drawing prims with primitive restart enabled.
398
* Scan for restart indexes and draw the runs of elements/vertices between
402
draw_pt_arrays_restart(struct draw_context *draw,
403
const struct pipe_draw_info *info,
404
const struct pipe_draw_start_count_bias *draw_info,
407
const unsigned prim = info->mode;
409
assert(info->primitive_restart);
411
if (draw->pt.user.eltSize) {
412
/* indexed prims (draw_elements) */
413
for (unsigned i = 0; i < num_draws; i++)
414
prim_restart_loop(draw, info, &draw_info[i], draw->pt.user.elts);
417
/* Non-indexed prims (draw_arrays).
418
* Primitive restart should have been handled in gallium frontends.
420
draw_pt_arrays(draw, prim, info->index_bias_varies, draw_info, num_draws);
426
* Resolve true values within pipe_draw_info.
427
* If we're rendering from transform feedback/stream output
428
* buffers both the count and max_index need to be computed
429
* from the attached stream output target.
432
resolve_draw_info(const struct pipe_draw_info *raw_info,
433
const struct pipe_draw_indirect_info *indirect,
434
const struct pipe_draw_start_count_bias *raw_draw,
435
struct pipe_draw_info *info,
436
struct pipe_draw_start_count_bias *draw,
437
struct pipe_vertex_buffer *vertex_buffer)
439
memcpy(info, raw_info, sizeof(struct pipe_draw_info));
440
memcpy(draw, raw_draw, sizeof(struct pipe_draw_start_count_bias));
442
struct draw_so_target *target =
443
(struct draw_so_target *)indirect->count_from_stream_output;
444
assert(vertex_buffer != NULL);
445
draw->count = vertex_buffer->stride == 0 ? 0 :
446
target->internal_offset / vertex_buffer->stride;
448
/* Stream output draw can not be indexed */
449
debug_assert(!info->index_size);
450
info->max_index = draw->count - 1;
454
* Loop over all instances and execute draws for them.
457
draw_instances(struct draw_context *draw,
458
unsigned drawid_offset,
459
const struct pipe_draw_info *info,
460
const struct pipe_draw_start_count_bias *draws,
465
draw->start_instance = info->start_instance;
467
for (instance = 0; instance < info->instance_count; instance++) {
468
unsigned instance_idx = instance + info->start_instance;
469
draw->instance_id = instance;
470
/* check for overflow */
471
if (instance_idx < instance ||
472
instance_idx < draw->start_instance) {
473
/* if we overflown just set the instance id to the max */
474
draw->instance_id = 0xffffffff;
477
draw->pt.user.drawid = drawid_offset;
478
draw_new_instance(draw);
480
if (info->primitive_restart) {
481
draw_pt_arrays_restart(draw, info, draws, num_draws);
484
draw_pt_arrays(draw, info->mode, info->index_bias_varies, draws, num_draws);
490
* Draw vertex arrays.
491
* This is the main entrypoint into the drawing module. If drawing an indexed
492
* primitive, the draw_set_indexes() function should have already been called
493
* to specify the element/index buffer information.
496
draw_vbo(struct draw_context *draw,
497
const struct pipe_draw_info *info,
498
unsigned drawid_offset,
499
const struct pipe_draw_indirect_info *indirect,
500
const struct pipe_draw_start_count_bias *draws,
502
uint8_t patch_vertices)
504
unsigned index_limit;
505
unsigned fpstate = util_fpstate_get();
506
struct pipe_draw_info resolved_info;
507
struct pipe_draw_start_count_bias resolved_draw;
508
struct pipe_draw_info *use_info = (struct pipe_draw_info *)info;
509
struct pipe_draw_start_count_bias *use_draws = (struct pipe_draw_start_count_bias *)draws;
511
if (info->instance_count == 0)
514
/* Make sure that denorms are treated like zeros. This is
515
* the behavior required by D3D10. OpenGL doesn't care.
517
util_fpstate_set_denorms_to_zero(fpstate);
519
if (indirect && indirect->count_from_stream_output) {
520
resolve_draw_info(info, indirect, &draws[0], &resolved_info,
521
&resolved_draw, &(draw->pt.vertex_buffer[0]));
522
use_info = &resolved_info;
523
use_draws = &resolved_draw;
527
if (info->index_size)
528
assert(draw->pt.user.elts);
530
draw->pt.user.min_index = use_info->index_bounds_valid ? use_info->min_index : 0;
531
draw->pt.user.max_index = use_info->index_bounds_valid ? use_info->max_index : ~0;
532
draw->pt.user.eltSize = use_info->index_size ? draw->pt.user.eltSizeIB : 0;
533
draw->pt.user.drawid = drawid_offset;
534
draw->pt.user.increment_draw_id = use_info->increment_draw_id;
535
draw->pt.user.viewid = 0;
536
draw->pt.vertices_per_patch = patch_vertices;
539
for (unsigned i = 0; i < num_draws; i++)
540
debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
541
use_info->mode, use_draws[i].start, use_draws[i].count);
545
tgsi_dump(draw->vs.vertex_shader->state.tokens, 0);
549
debug_printf("Elements:\n");
550
for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
551
debug_printf(" %u: src_offset=%u inst_div=%u vbuf=%u format=%s\n",
553
draw->pt.vertex_element[i].src_offset,
554
draw->pt.vertex_element[i].instance_divisor,
555
draw->pt.vertex_element[i].vertex_buffer_index,
556
util_format_name(draw->pt.vertex_element[i].src_format));
558
debug_printf("Buffers:\n");
559
for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
560
debug_printf(" %u: stride=%u offset=%u size=%d ptr=%p\n",
562
draw->pt.vertex_buffer[i].stride,
563
draw->pt.vertex_buffer[i].buffer_offset,
564
(int) draw->pt.user.vbuffer[i].size,
565
draw->pt.user.vbuffer[i].map);
570
for (unsigned i = 0; i < num_draws; i++)
571
draw_print_arrays(draw, use_info->mode, use_draws[i].start, MIN2(use_draws[i].count, 20),
572
use_info->index_bias_varies ? use_draws[i].index_bias : use_draws[0].index_bias);
575
index_limit = util_draw_max_index(draw->pt.vertex_buffer,
576
draw->pt.vertex_element,
577
draw->pt.nr_vertex_elements,
579
#ifdef DRAW_LLVM_AVAILABLE
583
if (index_limit == 0) {
584
/* one of the buffers is too small to do any valid drawing */
585
debug_warning("draw: VBO too small to draw anything\n");
586
util_fpstate_set(fpstate);
591
/* If we're collecting stats then make sure we start from scratch */
592
if (draw->collect_statistics) {
593
memset(&draw->statistics, 0, sizeof(draw->statistics));
596
draw->pt.max_index = index_limit - 1;
597
draw->start_index = use_draws[0].start;
600
* TODO: We could use draw->pt.max_index to further narrow
601
* the min_index/max_index hints given by gallium frontends.
604
if (use_info->view_mask) {
605
u_foreach_bit(i, use_info->view_mask) {
606
draw->pt.user.viewid = i;
607
draw_instances(draw, drawid_offset, use_info, use_draws, num_draws);
610
draw_instances(draw, drawid_offset, use_info, use_draws, num_draws);
612
/* If requested emit the pipeline statistics for this run */
613
if (draw->collect_statistics) {
614
draw->render->pipeline_statistics(draw->render, &draw->statistics);
616
util_fpstate_set(fpstate);