Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pt.c
4565 views
1
/**************************************************************************
2
*
3
* Copyright 2007 VMware, Inc.
4
* All Rights Reserved.
5
*
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:
13
*
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
16
* of the Software.
17
*
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.
25
*
26
**************************************************************************/
27
28
/*
29
* Authors:
30
* Keith Whitwell <[email protected]>
31
*/
32
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"
45
46
47
DEBUG_GET_ONCE_BOOL_OPTION(draw_fse, "DRAW_FSE", FALSE)
48
DEBUG_GET_ONCE_BOOL_OPTION(draw_no_fse, "DRAW_NO_FSE", FALSE)
49
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.
55
*/
56
static boolean
57
draw_pt_arrays(struct draw_context *draw,
58
unsigned prim,
59
bool index_bias_varies,
60
const struct pipe_draw_start_count_bias *draw_info,
61
unsigned num_draws)
62
{
63
struct draw_pt_front_end *frontend = NULL;
64
struct draw_pt_middle_end *middle = NULL;
65
unsigned opt = PT_SHADE;
66
67
unsigned out_prim = prim;
68
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);
73
74
if (!draw->render) {
75
opt |= PT_PIPELINE;
76
}
77
78
if (draw_need_pipeline(draw,
79
draw->rasterizer,
80
out_prim)) {
81
opt |= PT_PIPELINE;
82
}
83
84
if ((draw->clip_xy ||
85
draw->clip_z ||
86
draw->clip_user) && !draw->pt.test_fse) {
87
opt |= PT_CLIPTEST;
88
}
89
90
if (draw->pt.middle.llvm) {
91
middle = draw->pt.middle.llvm;
92
} else {
93
if (opt == PT_SHADE && !draw->pt.no_fse)
94
middle = draw->pt.middle.fetch_shade_emit;
95
else
96
middle = draw->pt.middle.general;
97
}
98
99
frontend = draw->pt.frontend;
100
101
if (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
106
* lines.
107
*/
108
draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
109
frontend = NULL;
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.
115
*/
116
frontend->flush( frontend, DRAW_FLUSH_STATE_CHANGE );
117
frontend = NULL;
118
}
119
}
120
121
if (!frontend) {
122
frontend = draw->pt.front.vsplit;
123
124
frontend->prepare( frontend, prim, middle, opt );
125
126
draw->pt.frontend = frontend;
127
draw->pt.eltSize = draw->pt.user.eltSize;
128
draw->pt.prim = prim;
129
draw->pt.opt = opt;
130
}
131
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;
136
}
137
138
139
for (unsigned i = 0; i < num_draws; i++) {
140
unsigned count = draw_info[i].count;
141
/* Sanitize primitive length:
142
*/
143
unsigned first, incr;
144
145
if (prim == PIPE_PRIM_PATCHES) {
146
first = draw->pt.vertices_per_patch;
147
incr = draw->pt.vertices_per_patch;
148
} else
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) :
153
0;
154
if (count >= first)
155
frontend->run( frontend, draw_info[i].start, count );
156
157
if (draw->pt.user.increment_draw_id)
158
draw->pt.user.drawid++;
159
}
160
161
return TRUE;
162
}
163
164
void draw_pt_flush( struct draw_context *draw, unsigned flags )
165
{
166
assert(flags);
167
168
if (draw->pt.frontend) {
169
draw->pt.frontend->flush( draw->pt.frontend, flags );
170
171
/* don't prepare if we only are flushing the backend */
172
if (flags & DRAW_FLUSH_STATE_CHANGE)
173
draw->pt.frontend = NULL;
174
}
175
176
if (flags & DRAW_FLUSH_PARAMETER_CHANGE) {
177
draw->pt.rebind_parameters = TRUE;
178
}
179
}
180
181
182
183
boolean draw_pt_init( struct draw_context *draw )
184
{
185
draw->pt.test_fse = debug_get_option_draw_fse();
186
draw->pt.no_fse = debug_get_option_draw_no_fse();
187
188
draw->pt.front.vsplit = draw_pt_vsplit(draw);
189
if (!draw->pt.front.vsplit)
190
return FALSE;
191
192
draw->pt.middle.fetch_shade_emit = draw_pt_middle_fse( draw );
193
if (!draw->pt.middle.fetch_shade_emit)
194
return FALSE;
195
196
draw->pt.middle.general = draw_pt_fetch_pipeline_or_emit( draw );
197
if (!draw->pt.middle.general)
198
return FALSE;
199
200
#ifdef DRAW_LLVM_AVAILABLE
201
if (draw->llvm)
202
draw->pt.middle.llvm = draw_pt_fetch_pipeline_or_emit_llvm( draw );
203
#endif
204
205
return TRUE;
206
}
207
208
209
void draw_pt_destroy( struct draw_context *draw )
210
{
211
if (draw->pt.middle.llvm) {
212
draw->pt.middle.llvm->destroy( draw->pt.middle.llvm );
213
draw->pt.middle.llvm = NULL;
214
}
215
216
if (draw->pt.middle.general) {
217
draw->pt.middle.general->destroy( draw->pt.middle.general );
218
draw->pt.middle.general = NULL;
219
}
220
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;
224
}
225
226
if (draw->pt.front.vsplit) {
227
draw->pt.front.vsplit->destroy( draw->pt.front.vsplit );
228
draw->pt.front.vsplit = NULL;
229
}
230
}
231
232
233
/**
234
* Debug- print the first 'count' vertices.
235
*/
236
static void
237
draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count, int index_bias)
238
{
239
uint i;
240
241
debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
242
prim, start, count);
243
244
for (i = 0; i < count; i++) {
245
uint ii = 0;
246
uint j;
247
248
if (draw->pt.user.eltSize) {
249
/* indexed arrays */
250
251
switch (draw->pt.user.eltSize) {
252
case 1:
253
{
254
const ubyte *elem = (const ubyte *) draw->pt.user.elts;
255
ii = elem[start + i];
256
}
257
break;
258
case 2:
259
{
260
const ushort *elem = (const ushort *) draw->pt.user.elts;
261
ii = elem[start + i];
262
}
263
break;
264
case 4:
265
{
266
const uint *elem = (const uint *) draw->pt.user.elts;
267
ii = elem[start + i];
268
}
269
break;
270
default:
271
assert(0);
272
return;
273
}
274
ii += index_bias;
275
debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start, i,
276
index_bias, ii);
277
}
278
else {
279
/* non-indexed arrays */
280
ii = start + i;
281
debug_printf("Vertex %u:\n", ii);
282
}
283
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;
287
288
if (draw->pt.vertex_element[j].instance_divisor) {
289
ii = draw->instance_id / draw->pt.vertex_element[j].instance_divisor;
290
}
291
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;
295
296
debug_printf(" Attr %u: ", j);
297
switch (draw->pt.vertex_element[j].src_format) {
298
case PIPE_FORMAT_R32_FLOAT:
299
{
300
float *v = (float *) ptr;
301
debug_printf("R %f @ %p\n", v[0], (void *) v);
302
}
303
break;
304
case PIPE_FORMAT_R32G32_FLOAT:
305
{
306
float *v = (float *) ptr;
307
debug_printf("RG %f %f @ %p\n", v[0], v[1], (void *) v);
308
}
309
break;
310
case PIPE_FORMAT_R32G32B32_FLOAT:
311
{
312
float *v = (float *) ptr;
313
debug_printf("RGB %f %f %f @ %p\n", v[0], v[1], v[2], (void *) v);
314
}
315
break;
316
case PIPE_FORMAT_R32G32B32A32_FLOAT:
317
{
318
float *v = (float *) ptr;
319
debug_printf("RGBA %f %f %f %f @ %p\n", v[0], v[1], v[2], v[3],
320
(void *) v);
321
}
322
break;
323
case PIPE_FORMAT_B8G8R8A8_UNORM:
324
{
325
ubyte *u = (ubyte *) ptr;
326
debug_printf("BGRA %d %d %d %d @ %p\n", u[0], u[1], u[2], u[3],
327
(void *) u);
328
}
329
break;
330
case PIPE_FORMAT_A8R8G8B8_UNORM:
331
{
332
ubyte *u = (ubyte *) ptr;
333
debug_printf("ARGB %d %d %d %d @ %p\n", u[0], u[1], u[2], u[3],
334
(void *) u);
335
}
336
break;
337
default:
338
debug_printf("other format %s (fix me)\n",
339
util_format_name(draw->pt.vertex_element[j].src_format));
340
}
341
}
342
}
343
}
344
345
346
/** Helper code for below */
347
static inline void
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)
352
{
353
const unsigned elt_max = draw->pt.user.eltMax;
354
struct pipe_draw_start_count_bias cur = *draw_info;
355
cur.count = 0;
356
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;
360
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) {
365
case 1:
366
restart_idx = ((const uint8_t*)elements)[i];
367
break;
368
case 2:
369
restart_idx = ((const uint16_t*)elements)[i];
370
break;
371
case 4:
372
restart_idx = ((const uint32_t*)elements)[i];
373
break;
374
default:
375
assert(0 && "bad eltSize in draw_arrays()");
376
}
377
378
if (i < elt_max && restart_idx == info->restart_index) {
379
if (cur.count > 0) {
380
/* draw elts up to prev pos */
381
draw_pt_arrays(draw, info->mode, info->index_bias_varies, &cur, 1);
382
}
383
/* begin new prim at next elt */
384
cur.start = i + 1;
385
cur.count = 0;
386
}
387
else {
388
cur.count++;
389
}
390
}
391
if (cur.count > 0) {
392
draw_pt_arrays(draw, info->mode, info->index_bias_varies, &cur, 1);
393
}
394
}
395
396
/**
397
* For drawing prims with primitive restart enabled.
398
* Scan for restart indexes and draw the runs of elements/vertices between
399
* the restarts.
400
*/
401
static void
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,
405
unsigned num_draws)
406
{
407
const unsigned prim = info->mode;
408
409
assert(info->primitive_restart);
410
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);
415
}
416
else {
417
/* Non-indexed prims (draw_arrays).
418
* Primitive restart should have been handled in gallium frontends.
419
*/
420
draw_pt_arrays(draw, prim, info->index_bias_varies, draw_info, num_draws);
421
}
422
}
423
424
425
/**
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.
430
*/
431
static void
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)
438
{
439
memcpy(info, raw_info, sizeof(struct pipe_draw_info));
440
memcpy(draw, raw_draw, sizeof(struct pipe_draw_start_count_bias));
441
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;
447
448
/* Stream output draw can not be indexed */
449
debug_assert(!info->index_size);
450
info->max_index = draw->count - 1;
451
}
452
453
/*
454
* Loop over all instances and execute draws for them.
455
*/
456
static void
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,
461
unsigned num_draws)
462
{
463
unsigned instance;
464
465
draw->start_instance = info->start_instance;
466
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;
475
}
476
477
draw->pt.user.drawid = drawid_offset;
478
draw_new_instance(draw);
479
480
if (info->primitive_restart) {
481
draw_pt_arrays_restart(draw, info, draws, num_draws);
482
}
483
else {
484
draw_pt_arrays(draw, info->mode, info->index_bias_varies, draws, num_draws);
485
}
486
}
487
}
488
489
/**
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.
494
*/
495
void
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,
501
unsigned num_draws)
502
{
503
unsigned index_limit;
504
unsigned fpstate = util_fpstate_get();
505
struct pipe_draw_info resolved_info;
506
struct pipe_draw_start_count_bias resolved_draw;
507
struct pipe_draw_info *use_info = (struct pipe_draw_info *)info;
508
struct pipe_draw_start_count_bias *use_draws = (struct pipe_draw_start_count_bias *)draws;
509
510
if (info->instance_count == 0)
511
return;
512
513
/* Make sure that denorms are treated like zeros. This is
514
* the behavior required by D3D10. OpenGL doesn't care.
515
*/
516
util_fpstate_set_denorms_to_zero(fpstate);
517
518
if (indirect && indirect->count_from_stream_output) {
519
resolve_draw_info(info, indirect, &draws[0], &resolved_info,
520
&resolved_draw, &(draw->pt.vertex_buffer[0]));
521
use_info = &resolved_info;
522
use_draws = &resolved_draw;
523
num_draws = 1;
524
}
525
526
if (info->index_size)
527
assert(draw->pt.user.elts);
528
529
draw->pt.user.min_index = use_info->index_bounds_valid ? use_info->min_index : 0;
530
draw->pt.user.max_index = use_info->index_bounds_valid ? use_info->max_index : ~0;
531
draw->pt.user.eltSize = use_info->index_size ? draw->pt.user.eltSizeIB : 0;
532
draw->pt.user.drawid = drawid_offset;
533
draw->pt.user.increment_draw_id = use_info->increment_draw_id;
534
draw->pt.user.viewid = 0;
535
draw->pt.vertices_per_patch = use_info->vertices_per_patch;
536
537
if (0) {
538
for (unsigned i = 0; i < num_draws; i++)
539
debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
540
use_info->mode, use_draws[i].start, use_draws[i].count);
541
}
542
543
if (0)
544
tgsi_dump(draw->vs.vertex_shader->state.tokens, 0);
545
546
if (0) {
547
unsigned int i;
548
debug_printf("Elements:\n");
549
for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
550
debug_printf(" %u: src_offset=%u inst_div=%u vbuf=%u format=%s\n",
551
i,
552
draw->pt.vertex_element[i].src_offset,
553
draw->pt.vertex_element[i].instance_divisor,
554
draw->pt.vertex_element[i].vertex_buffer_index,
555
util_format_name(draw->pt.vertex_element[i].src_format));
556
}
557
debug_printf("Buffers:\n");
558
for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
559
debug_printf(" %u: stride=%u offset=%u size=%d ptr=%p\n",
560
i,
561
draw->pt.vertex_buffer[i].stride,
562
draw->pt.vertex_buffer[i].buffer_offset,
563
(int) draw->pt.user.vbuffer[i].size,
564
draw->pt.user.vbuffer[i].map);
565
}
566
}
567
568
if (0) {
569
for (unsigned i = 0; i < num_draws; i++)
570
draw_print_arrays(draw, use_info->mode, use_draws[i].start, MIN2(use_draws[i].count, 20),
571
use_info->index_bias_varies ? use_draws[i].index_bias : use_draws[0].index_bias);
572
}
573
574
index_limit = util_draw_max_index(draw->pt.vertex_buffer,
575
draw->pt.vertex_element,
576
draw->pt.nr_vertex_elements,
577
use_info);
578
#ifdef DRAW_LLVM_AVAILABLE
579
if (!draw->llvm)
580
#endif
581
{
582
if (index_limit == 0) {
583
/* one of the buffers is too small to do any valid drawing */
584
debug_warning("draw: VBO too small to draw anything\n");
585
util_fpstate_set(fpstate);
586
return;
587
}
588
}
589
590
/* If we're collecting stats then make sure we start from scratch */
591
if (draw->collect_statistics) {
592
memset(&draw->statistics, 0, sizeof(draw->statistics));
593
}
594
595
draw->pt.max_index = index_limit - 1;
596
draw->start_index = use_draws[0].start;
597
598
/*
599
* TODO: We could use draw->pt.max_index to further narrow
600
* the min_index/max_index hints given by gallium frontends.
601
*/
602
603
if (use_info->view_mask) {
604
u_foreach_bit(i, use_info->view_mask) {
605
draw->pt.user.viewid = i;
606
draw_instances(draw, drawid_offset, use_info, use_draws, num_draws);
607
}
608
} else
609
draw_instances(draw, drawid_offset, use_info, use_draws, num_draws);
610
611
/* If requested emit the pipeline statistics for this run */
612
if (draw->collect_statistics) {
613
draw->render->pipeline_statistics(draw->render, &draw->statistics);
614
}
615
util_fpstate_set(fpstate);
616
}
617
618