Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_state_draw.cpp
4570 views
1
/*
2
* Copyright 2012 Advanced Micro Devices, Inc.
3
* All Rights Reserved.
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* on the rights to use, copy, modify, merge, publish, distribute, sub
9
* license, and/or sell copies of the Software, and to permit persons to whom
10
* the Software is furnished to do so, subject to the following conditions:
11
*
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
14
* Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22
* USE OR OTHER DEALINGS IN THE SOFTWARE.
23
*/
24
25
#include "ac_sqtt.h"
26
#include "si_build_pm4.h"
27
#include "util/u_index_modify.h"
28
#include "util/u_prim.h"
29
#include "util/u_upload_mgr.h"
30
31
#if (GFX_VER == 6)
32
#define GFX(name) name##GFX6
33
#elif (GFX_VER == 7)
34
#define GFX(name) name##GFX7
35
#elif (GFX_VER == 8)
36
#define GFX(name) name##GFX8
37
#elif (GFX_VER == 9)
38
#define GFX(name) name##GFX9
39
#elif (GFX_VER == 10)
40
#define GFX(name) name##GFX10
41
#elif (GFX_VER == 103)
42
#define GFX(name) name##GFX10_3
43
#else
44
#error "Unknown gfx version"
45
#endif
46
47
/* special primitive types */
48
#define SI_PRIM_RECTANGLE_LIST PIPE_PRIM_MAX
49
50
ALWAYS_INLINE
51
static unsigned si_conv_pipe_prim(unsigned mode)
52
{
53
static const unsigned prim_conv[] = {
54
[PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
55
[PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
56
[PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
57
[PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
58
[PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
59
[PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
60
[PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
61
[PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
62
[PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
63
[PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
64
[PIPE_PRIM_LINES_ADJACENCY] = V_008958_DI_PT_LINELIST_ADJ,
65
[PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_008958_DI_PT_LINESTRIP_ADJ,
66
[PIPE_PRIM_TRIANGLES_ADJACENCY] = V_008958_DI_PT_TRILIST_ADJ,
67
[PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_008958_DI_PT_TRISTRIP_ADJ,
68
[PIPE_PRIM_PATCHES] = V_008958_DI_PT_PATCH,
69
[SI_PRIM_RECTANGLE_LIST] = V_008958_DI_PT_RECTLIST};
70
assert(mode < ARRAY_SIZE(prim_conv));
71
return prim_conv[mode];
72
}
73
74
static void si_prefetch_shader_async(struct si_context *sctx, struct si_pm4_state *state)
75
{
76
struct pipe_resource *bo = &state->shader->bo->b.b;
77
78
si_cp_dma_prefetch(sctx, bo, 0, bo->width0);
79
}
80
81
enum si_L2_prefetch_mode {
82
PREFETCH_BEFORE_DRAW = 1,
83
PREFETCH_AFTER_DRAW,
84
PREFETCH_ALL,
85
};
86
87
/**
88
* Prefetch shaders.
89
*/
90
template<chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS, si_has_ngg NGG,
91
si_L2_prefetch_mode mode>
92
static void si_prefetch_shaders(struct si_context *sctx)
93
{
94
unsigned mask = sctx->prefetch_L2_mask;
95
96
/* GFX6 doesn't support the L2 prefetch. */
97
if (GFX_VERSION < GFX7 || !mask)
98
return;
99
100
/* Prefetch shaders and VBO descriptors to TC L2. */
101
if (GFX_VERSION >= GFX9) {
102
/* Choose the right spot for the VBO prefetch. */
103
if (HAS_TESS) {
104
if (mode != PREFETCH_AFTER_DRAW) {
105
if (mask & SI_PREFETCH_HS)
106
si_prefetch_shader_async(sctx, sctx->queued.named.hs);
107
108
if (mode == PREFETCH_BEFORE_DRAW)
109
return;
110
}
111
112
if ((HAS_GS || NGG) && mask & SI_PREFETCH_GS)
113
si_prefetch_shader_async(sctx, sctx->queued.named.gs);
114
if (!NGG && mask & SI_PREFETCH_VS)
115
si_prefetch_shader_async(sctx, sctx->queued.named.vs);
116
} else if (HAS_GS || NGG) {
117
if (mode != PREFETCH_AFTER_DRAW) {
118
if (mask & SI_PREFETCH_GS)
119
si_prefetch_shader_async(sctx, sctx->queued.named.gs);
120
121
if (mode == PREFETCH_BEFORE_DRAW)
122
return;
123
}
124
125
if (!NGG && mask & SI_PREFETCH_VS)
126
si_prefetch_shader_async(sctx, sctx->queued.named.vs);
127
} else {
128
if (mode != PREFETCH_AFTER_DRAW) {
129
if (mask & SI_PREFETCH_VS)
130
si_prefetch_shader_async(sctx, sctx->queued.named.vs);
131
132
if (mode == PREFETCH_BEFORE_DRAW)
133
return;
134
}
135
}
136
} else {
137
/* GFX6-GFX8 */
138
/* Choose the right spot for the VBO prefetch. */
139
if (HAS_TESS) {
140
if (mode != PREFETCH_AFTER_DRAW) {
141
if (mask & SI_PREFETCH_LS)
142
si_prefetch_shader_async(sctx, sctx->queued.named.ls);
143
144
if (mode == PREFETCH_BEFORE_DRAW)
145
return;
146
}
147
148
if (mask & SI_PREFETCH_HS)
149
si_prefetch_shader_async(sctx, sctx->queued.named.hs);
150
if (mask & SI_PREFETCH_ES)
151
si_prefetch_shader_async(sctx, sctx->queued.named.es);
152
if (mask & SI_PREFETCH_GS)
153
si_prefetch_shader_async(sctx, sctx->queued.named.gs);
154
if (mask & SI_PREFETCH_VS)
155
si_prefetch_shader_async(sctx, sctx->queued.named.vs);
156
} else if (HAS_GS) {
157
if (mode != PREFETCH_AFTER_DRAW) {
158
if (mask & SI_PREFETCH_ES)
159
si_prefetch_shader_async(sctx, sctx->queued.named.es);
160
161
if (mode == PREFETCH_BEFORE_DRAW)
162
return;
163
}
164
165
if (mask & SI_PREFETCH_GS)
166
si_prefetch_shader_async(sctx, sctx->queued.named.gs);
167
if (mask & SI_PREFETCH_VS)
168
si_prefetch_shader_async(sctx, sctx->queued.named.vs);
169
} else {
170
if (mode != PREFETCH_AFTER_DRAW) {
171
if (mask & SI_PREFETCH_VS)
172
si_prefetch_shader_async(sctx, sctx->queued.named.vs);
173
174
if (mode == PREFETCH_BEFORE_DRAW)
175
return;
176
}
177
}
178
}
179
180
if (mask & SI_PREFETCH_PS)
181
si_prefetch_shader_async(sctx, sctx->queued.named.ps);
182
183
/* This must be cleared only when AFTER_DRAW is true. */
184
sctx->prefetch_L2_mask = 0;
185
}
186
187
/**
188
* This calculates the LDS size for tessellation shaders (VS, TCS, TES).
189
* LS.LDS_SIZE is shared by all 3 shader stages.
190
*
191
* The information about LDS and other non-compile-time parameters is then
192
* written to userdata SGPRs.
193
*/
194
static void si_emit_derived_tess_state(struct si_context *sctx,
195
unsigned num_tcs_input_cp,
196
unsigned *num_patches)
197
{
198
struct si_shader *ls_current;
199
struct si_shader_selector *ls;
200
/* The TES pointer will only be used for sctx->last_tcs.
201
* It would be wrong to think that TCS = TES. */
202
struct si_shader_selector *tcs =
203
sctx->shader.tcs.cso ? sctx->shader.tcs.cso : sctx->shader.tes.cso;
204
unsigned tess_uses_primid = sctx->ia_multi_vgt_param_key.u.tess_uses_prim_id;
205
bool has_primid_instancing_bug = sctx->chip_class == GFX6 && sctx->screen->info.max_se == 1;
206
unsigned tes_sh_base = sctx->shader_pointers.sh_base[PIPE_SHADER_TESS_EVAL];
207
208
/* Since GFX9 has merged LS-HS in the TCS state, set LS = TCS. */
209
if (sctx->chip_class >= GFX9) {
210
if (sctx->shader.tcs.cso)
211
ls_current = sctx->shader.tcs.current;
212
else
213
ls_current = sctx->fixed_func_tcs_shader.current;
214
215
ls = ls_current->key.part.tcs.ls;
216
} else {
217
ls_current = sctx->shader.vs.current;
218
ls = sctx->shader.vs.cso;
219
}
220
221
if (sctx->last_ls == ls_current && sctx->last_tcs == tcs &&
222
sctx->last_tes_sh_base == tes_sh_base && sctx->last_num_tcs_input_cp == num_tcs_input_cp &&
223
(!has_primid_instancing_bug || (sctx->last_tess_uses_primid == tess_uses_primid))) {
224
*num_patches = sctx->last_num_patches;
225
return;
226
}
227
228
sctx->last_ls = ls_current;
229
sctx->last_tcs = tcs;
230
sctx->last_tes_sh_base = tes_sh_base;
231
sctx->last_num_tcs_input_cp = num_tcs_input_cp;
232
sctx->last_tess_uses_primid = tess_uses_primid;
233
234
/* This calculates how shader inputs and outputs among VS, TCS, and TES
235
* are laid out in LDS. */
236
unsigned num_tcs_inputs = util_last_bit64(ls->outputs_written);
237
unsigned num_tcs_output_cp, num_tcs_outputs, num_tcs_patch_outputs;
238
239
if (sctx->shader.tcs.cso) {
240
num_tcs_outputs = util_last_bit64(tcs->outputs_written);
241
num_tcs_output_cp = tcs->info.base.tess.tcs_vertices_out;
242
num_tcs_patch_outputs = util_last_bit64(tcs->patch_outputs_written);
243
} else {
244
/* No TCS. Route varyings from LS to TES. */
245
num_tcs_outputs = num_tcs_inputs;
246
num_tcs_output_cp = num_tcs_input_cp;
247
num_tcs_patch_outputs = 2; /* TESSINNER + TESSOUTER */
248
}
249
250
unsigned input_vertex_size = ls->lshs_vertex_stride;
251
unsigned output_vertex_size = num_tcs_outputs * 16;
252
unsigned input_patch_size;
253
254
/* Allocate LDS for TCS inputs only if it's used. */
255
if (!ls_current->key.opt.same_patch_vertices ||
256
tcs->info.base.inputs_read & ~tcs->tcs_vgpr_only_inputs)
257
input_patch_size = num_tcs_input_cp * input_vertex_size;
258
else
259
input_patch_size = 0;
260
261
unsigned pervertex_output_patch_size = num_tcs_output_cp * output_vertex_size;
262
unsigned output_patch_size = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
263
unsigned lds_per_patch;
264
265
/* Compute the LDS size per patch.
266
*
267
* LDS is used to store TCS outputs if they are read, and to store tess
268
* factors if they are not defined in all invocations.
269
*/
270
if (tcs->info.base.outputs_read ||
271
tcs->info.base.patch_outputs_read ||
272
!tcs->info.tessfactors_are_def_in_all_invocs) {
273
lds_per_patch = input_patch_size + output_patch_size;
274
} else {
275
/* LDS will only store TCS inputs. The offchip buffer will only store TCS outputs. */
276
lds_per_patch = MAX2(input_patch_size, output_patch_size);
277
}
278
279
/* Ensure that we only need 4 waves per CU, so that we don't need to check
280
* resource usage (such as whether we have enough VGPRs to fit the whole
281
* threadgroup into the CU). It also ensures that the number of tcs in and out
282
* vertices per threadgroup are at most 256, which is the hw limit.
283
*/
284
unsigned max_verts_per_patch = MAX2(num_tcs_input_cp, num_tcs_output_cp);
285
*num_patches = 256 / max_verts_per_patch;
286
287
/* Not necessary for correctness, but higher numbers are slower.
288
* The hardware can do more, but the radeonsi shader constant is
289
* limited to 6 bits.
290
*/
291
*num_patches = MIN2(*num_patches, 64); /* e.g. 64 triangles in exactly 3 waves */
292
293
/* When distributed tessellation is unsupported, switch between SEs
294
* at a higher frequency to manually balance the workload between SEs.
295
*/
296
if (!sctx->screen->info.has_distributed_tess && sctx->screen->info.max_se > 1)
297
*num_patches = MIN2(*num_patches, 16); /* recommended */
298
299
/* Make sure the output data fits in the offchip buffer */
300
*num_patches =
301
MIN2(*num_patches, (sctx->screen->tess_offchip_block_dw_size * 4) / output_patch_size);
302
303
/* Make sure that the data fits in LDS. This assumes the shaders only
304
* use LDS for the inputs and outputs.
305
*
306
* The maximum allowed LDS size is 32K. Higher numbers can hang.
307
* Use 16K as the maximum, so that we can fit 2 workgroups on the same CU.
308
*/
309
ASSERTED unsigned max_lds_size = 32 * 1024; /* hw limit */
310
unsigned target_lds_size = 16 * 1024; /* target at least 2 workgroups per CU, 16K each */
311
*num_patches = MIN2(*num_patches, target_lds_size / lds_per_patch);
312
*num_patches = MAX2(*num_patches, 1);
313
assert(*num_patches * lds_per_patch <= max_lds_size);
314
315
/* Make sure that vector lanes are fully occupied by cutting off the last wave
316
* if it's only partially filled.
317
*/
318
unsigned temp_verts_per_tg = *num_patches * max_verts_per_patch;
319
unsigned wave_size = sctx->screen->ge_wave_size;
320
321
if (temp_verts_per_tg > wave_size &&
322
(wave_size - temp_verts_per_tg % wave_size >= MAX2(max_verts_per_patch, 8)))
323
*num_patches = (temp_verts_per_tg & ~(wave_size - 1)) / max_verts_per_patch;
324
325
if (sctx->chip_class == GFX6) {
326
/* GFX6 bug workaround, related to power management. Limit LS-HS
327
* threadgroups to only one wave.
328
*/
329
unsigned one_wave = wave_size / max_verts_per_patch;
330
*num_patches = MIN2(*num_patches, one_wave);
331
}
332
333
/* The VGT HS block increments the patch ID unconditionally
334
* within a single threadgroup. This results in incorrect
335
* patch IDs when instanced draws are used.
336
*
337
* The intended solution is to restrict threadgroups to
338
* a single instance by setting SWITCH_ON_EOI, which
339
* should cause IA to split instances up. However, this
340
* doesn't work correctly on GFX6 when there is no other
341
* SE to switch to.
342
*/
343
if (has_primid_instancing_bug && tess_uses_primid)
344
*num_patches = 1;
345
346
sctx->last_num_patches = *num_patches;
347
348
unsigned output_patch0_offset = input_patch_size * *num_patches;
349
unsigned perpatch_output_offset = output_patch0_offset + pervertex_output_patch_size;
350
351
/* Compute userdata SGPRs. */
352
assert(((input_vertex_size / 4) & ~0xff) == 0);
353
assert(((output_vertex_size / 4) & ~0xff) == 0);
354
assert(((input_patch_size / 4) & ~0x1fff) == 0);
355
assert(((output_patch_size / 4) & ~0x1fff) == 0);
356
assert(((output_patch0_offset / 16) & ~0xffff) == 0);
357
assert(((perpatch_output_offset / 16) & ~0xffff) == 0);
358
assert(num_tcs_input_cp <= 32);
359
assert(num_tcs_output_cp <= 32);
360
assert(*num_patches <= 64);
361
assert(((pervertex_output_patch_size * *num_patches) & ~0x1fffff) == 0);
362
363
uint64_t ring_va = (unlikely(sctx->ws->cs_is_secure(&sctx->gfx_cs)) ?
364
si_resource(sctx->tess_rings_tmz) : si_resource(sctx->tess_rings))->gpu_address;
365
assert((ring_va & u_bit_consecutive(0, 19)) == 0);
366
367
unsigned tcs_in_layout = S_VS_STATE_LS_OUT_PATCH_SIZE(input_patch_size / 4) |
368
S_VS_STATE_LS_OUT_VERTEX_SIZE(input_vertex_size / 4);
369
unsigned tcs_out_layout = (output_patch_size / 4) | (num_tcs_input_cp << 13) | ring_va;
370
unsigned tcs_out_offsets = (output_patch0_offset / 16) | ((perpatch_output_offset / 16) << 16);
371
unsigned offchip_layout =
372
(*num_patches - 1) | ((num_tcs_output_cp - 1) << 6) |
373
((pervertex_output_patch_size * *num_patches) << 11);
374
375
/* Compute the LDS size. */
376
unsigned lds_size = lds_per_patch * *num_patches;
377
378
if (sctx->chip_class >= GFX7) {
379
assert(lds_size <= 65536);
380
lds_size = align(lds_size, 512) / 512;
381
} else {
382
assert(lds_size <= 32768);
383
lds_size = align(lds_size, 256) / 256;
384
}
385
386
/* Set SI_SGPR_VS_STATE_BITS. */
387
sctx->current_vs_state &= C_VS_STATE_LS_OUT_PATCH_SIZE & C_VS_STATE_LS_OUT_VERTEX_SIZE;
388
sctx->current_vs_state |= tcs_in_layout;
389
390
/* We should be able to support in-shader LDS use with LLVM >= 9
391
* by just adding the lds_sizes together, but it has never
392
* been tested. */
393
assert(ls_current->config.lds_size == 0);
394
395
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
396
radeon_begin(cs);
397
398
if (sctx->chip_class >= GFX9) {
399
unsigned hs_rsrc2 = ls_current->config.rsrc2;
400
401
if (sctx->chip_class >= GFX10)
402
hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX10(lds_size);
403
else
404
hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX9(lds_size);
405
406
radeon_set_sh_reg(cs, R_00B42C_SPI_SHADER_PGM_RSRC2_HS, hs_rsrc2);
407
408
/* Set userdata SGPRs for merged LS-HS. */
409
radeon_set_sh_reg_seq(
410
cs, R_00B430_SPI_SHADER_USER_DATA_LS_0 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT * 4, 3);
411
radeon_emit(cs, offchip_layout);
412
radeon_emit(cs, tcs_out_offsets);
413
radeon_emit(cs, tcs_out_layout);
414
} else {
415
unsigned ls_rsrc2 = ls_current->config.rsrc2;
416
417
si_multiwave_lds_size_workaround(sctx->screen, &lds_size);
418
ls_rsrc2 |= S_00B52C_LDS_SIZE(lds_size);
419
420
/* Due to a hw bug, RSRC2_LS must be written twice with another
421
* LS register written in between. */
422
if (sctx->chip_class == GFX7 && sctx->family != CHIP_HAWAII)
423
radeon_set_sh_reg(cs, R_00B52C_SPI_SHADER_PGM_RSRC2_LS, ls_rsrc2);
424
radeon_set_sh_reg_seq(cs, R_00B528_SPI_SHADER_PGM_RSRC1_LS, 2);
425
radeon_emit(cs, ls_current->config.rsrc1);
426
radeon_emit(cs, ls_rsrc2);
427
428
/* Set userdata SGPRs for TCS. */
429
radeon_set_sh_reg_seq(
430
cs, R_00B430_SPI_SHADER_USER_DATA_HS_0 + GFX6_SGPR_TCS_OFFCHIP_LAYOUT * 4, 4);
431
radeon_emit(cs, offchip_layout);
432
radeon_emit(cs, tcs_out_offsets);
433
radeon_emit(cs, tcs_out_layout);
434
radeon_emit(cs, tcs_in_layout);
435
}
436
437
/* Set userdata SGPRs for TES. */
438
radeon_set_sh_reg_seq(cs, tes_sh_base + SI_SGPR_TES_OFFCHIP_LAYOUT * 4, 2);
439
radeon_emit(cs, offchip_layout);
440
radeon_emit(cs, ring_va);
441
radeon_end();
442
443
unsigned ls_hs_config =
444
S_028B58_NUM_PATCHES(*num_patches) |
445
S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp) |
446
S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp);
447
448
if (sctx->last_ls_hs_config != ls_hs_config) {
449
radeon_begin(cs);
450
if (sctx->chip_class >= GFX7) {
451
radeon_set_context_reg_idx(cs, R_028B58_VGT_LS_HS_CONFIG, 2, ls_hs_config);
452
} else {
453
radeon_set_context_reg(cs, R_028B58_VGT_LS_HS_CONFIG, ls_hs_config);
454
}
455
radeon_end_update_context_roll(sctx);
456
sctx->last_ls_hs_config = ls_hs_config;
457
}
458
}
459
460
static unsigned si_num_prims_for_vertices(enum pipe_prim_type prim,
461
unsigned count, unsigned vertices_per_patch)
462
{
463
switch (prim) {
464
case PIPE_PRIM_PATCHES:
465
return count / vertices_per_patch;
466
case PIPE_PRIM_POLYGON:
467
/* It's a triangle fan with different edge flags. */
468
return count >= 3 ? count - 2 : 0;
469
case SI_PRIM_RECTANGLE_LIST:
470
return count / 3;
471
default:
472
return u_decomposed_prims_for_vertices(prim, count);
473
}
474
}
475
476
static unsigned si_get_init_multi_vgt_param(struct si_screen *sscreen, union si_vgt_param_key *key)
477
{
478
STATIC_ASSERT(sizeof(union si_vgt_param_key) == 2);
479
unsigned max_primgroup_in_wave = 2;
480
481
/* SWITCH_ON_EOP(0) is always preferable. */
482
bool wd_switch_on_eop = false;
483
bool ia_switch_on_eop = false;
484
bool ia_switch_on_eoi = false;
485
bool partial_vs_wave = false;
486
bool partial_es_wave = false;
487
488
if (key->u.uses_tess) {
489
/* SWITCH_ON_EOI must be set if PrimID is used. */
490
if (key->u.tess_uses_prim_id)
491
ia_switch_on_eoi = true;
492
493
/* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
494
if ((sscreen->info.family == CHIP_TAHITI || sscreen->info.family == CHIP_PITCAIRN ||
495
sscreen->info.family == CHIP_BONAIRE) &&
496
key->u.uses_gs)
497
partial_vs_wave = true;
498
499
/* Needed for 028B6C_DISTRIBUTION_MODE != 0. (implies >= GFX8) */
500
if (sscreen->info.has_distributed_tess) {
501
if (key->u.uses_gs) {
502
if (sscreen->info.chip_class == GFX8)
503
partial_es_wave = true;
504
} else {
505
partial_vs_wave = true;
506
}
507
}
508
}
509
510
/* This is a hardware requirement. */
511
if (key->u.line_stipple_enabled || (sscreen->debug_flags & DBG(SWITCH_ON_EOP))) {
512
ia_switch_on_eop = true;
513
wd_switch_on_eop = true;
514
}
515
516
if (sscreen->info.chip_class >= GFX7) {
517
/* WD_SWITCH_ON_EOP has no effect on GPUs with less than
518
* 4 shader engines. Set 1 to pass the assertion below.
519
* The other cases are hardware requirements.
520
*
521
* Polaris supports primitive restart with WD_SWITCH_ON_EOP=0
522
* for points, line strips, and tri strips.
523
*/
524
if (sscreen->info.max_se <= 2 || key->u.prim == PIPE_PRIM_POLYGON ||
525
key->u.prim == PIPE_PRIM_LINE_LOOP || key->u.prim == PIPE_PRIM_TRIANGLE_FAN ||
526
key->u.prim == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY ||
527
(key->u.primitive_restart &&
528
(sscreen->info.family < CHIP_POLARIS10 ||
529
(key->u.prim != PIPE_PRIM_POINTS && key->u.prim != PIPE_PRIM_LINE_STRIP &&
530
key->u.prim != PIPE_PRIM_TRIANGLE_STRIP))) ||
531
key->u.count_from_stream_output)
532
wd_switch_on_eop = true;
533
534
/* Hawaii hangs if instancing is enabled and WD_SWITCH_ON_EOP is 0.
535
* We don't know that for indirect drawing, so treat it as
536
* always problematic. */
537
if (sscreen->info.family == CHIP_HAWAII && key->u.uses_instancing)
538
wd_switch_on_eop = true;
539
540
/* Performance recommendation for 4 SE Gfx7-8 parts if
541
* instances are smaller than a primgroup.
542
* Assume indirect draws always use small instances.
543
* This is needed for good VS wave utilization.
544
*/
545
if (sscreen->info.chip_class <= GFX8 && sscreen->info.max_se == 4 &&
546
key->u.multi_instances_smaller_than_primgroup)
547
wd_switch_on_eop = true;
548
549
/* Required on GFX7 and later. */
550
if (sscreen->info.max_se == 4 && !wd_switch_on_eop)
551
ia_switch_on_eoi = true;
552
553
/* HW engineers suggested that PARTIAL_VS_WAVE_ON should be set
554
* to work around a GS hang.
555
*/
556
if (key->u.uses_gs &&
557
(sscreen->info.family == CHIP_TONGA || sscreen->info.family == CHIP_FIJI ||
558
sscreen->info.family == CHIP_POLARIS10 || sscreen->info.family == CHIP_POLARIS11 ||
559
sscreen->info.family == CHIP_POLARIS12 || sscreen->info.family == CHIP_VEGAM))
560
partial_vs_wave = true;
561
562
/* Required by Hawaii and, for some special cases, by GFX8. */
563
if (ia_switch_on_eoi &&
564
(sscreen->info.family == CHIP_HAWAII ||
565
(sscreen->info.chip_class == GFX8 && (key->u.uses_gs || max_primgroup_in_wave != 2))))
566
partial_vs_wave = true;
567
568
/* Instancing bug on Bonaire. */
569
if (sscreen->info.family == CHIP_BONAIRE && ia_switch_on_eoi && key->u.uses_instancing)
570
partial_vs_wave = true;
571
572
/* This only applies to Polaris10 and later 4 SE chips.
573
* wd_switch_on_eop is already true on all other chips.
574
*/
575
if (!wd_switch_on_eop && key->u.primitive_restart)
576
partial_vs_wave = true;
577
578
/* If the WD switch is false, the IA switch must be false too. */
579
assert(wd_switch_on_eop || !ia_switch_on_eop);
580
}
581
582
/* If SWITCH_ON_EOI is set, PARTIAL_ES_WAVE must be set too. */
583
if (sscreen->info.chip_class <= GFX8 && ia_switch_on_eoi)
584
partial_es_wave = true;
585
586
return S_028AA8_SWITCH_ON_EOP(ia_switch_on_eop) | S_028AA8_SWITCH_ON_EOI(ia_switch_on_eoi) |
587
S_028AA8_PARTIAL_VS_WAVE_ON(partial_vs_wave) |
588
S_028AA8_PARTIAL_ES_WAVE_ON(partial_es_wave) |
589
S_028AA8_WD_SWITCH_ON_EOP(sscreen->info.chip_class >= GFX7 ? wd_switch_on_eop : 0) |
590
/* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */
591
S_028AA8_MAX_PRIMGRP_IN_WAVE(sscreen->info.chip_class == GFX8 ? max_primgroup_in_wave
592
: 0) |
593
S_030960_EN_INST_OPT_BASIC(sscreen->info.chip_class >= GFX9) |
594
S_030960_EN_INST_OPT_ADV(sscreen->info.chip_class >= GFX9);
595
}
596
597
static void si_init_ia_multi_vgt_param_table(struct si_context *sctx)
598
{
599
for (int prim = 0; prim <= SI_PRIM_RECTANGLE_LIST; prim++)
600
for (int uses_instancing = 0; uses_instancing < 2; uses_instancing++)
601
for (int multi_instances = 0; multi_instances < 2; multi_instances++)
602
for (int primitive_restart = 0; primitive_restart < 2; primitive_restart++)
603
for (int count_from_so = 0; count_from_so < 2; count_from_so++)
604
for (int line_stipple = 0; line_stipple < 2; line_stipple++)
605
for (int uses_tess = 0; uses_tess < 2; uses_tess++)
606
for (int tess_uses_primid = 0; tess_uses_primid < 2; tess_uses_primid++)
607
for (int uses_gs = 0; uses_gs < 2; uses_gs++) {
608
union si_vgt_param_key key;
609
610
key.index = 0;
611
key.u.prim = prim;
612
key.u.uses_instancing = uses_instancing;
613
key.u.multi_instances_smaller_than_primgroup = multi_instances;
614
key.u.primitive_restart = primitive_restart;
615
key.u.count_from_stream_output = count_from_so;
616
key.u.line_stipple_enabled = line_stipple;
617
key.u.uses_tess = uses_tess;
618
key.u.tess_uses_prim_id = tess_uses_primid;
619
key.u.uses_gs = uses_gs;
620
621
sctx->ia_multi_vgt_param[key.index] =
622
si_get_init_multi_vgt_param(sctx->screen, &key);
623
}
624
}
625
626
static bool si_is_line_stipple_enabled(struct si_context *sctx)
627
{
628
struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
629
630
return rs->line_stipple_enable && sctx->current_rast_prim != PIPE_PRIM_POINTS &&
631
(rs->polygon_mode_is_lines || util_prim_is_lines(sctx->current_rast_prim));
632
}
633
634
static bool num_instanced_prims_less_than(const struct pipe_draw_indirect_info *indirect,
635
enum pipe_prim_type prim,
636
unsigned min_vertex_count,
637
unsigned instance_count,
638
unsigned num_prims,
639
ubyte vertices_per_patch)
640
{
641
if (indirect) {
642
return indirect->buffer ||
643
(instance_count > 1 && indirect->count_from_stream_output);
644
} else {
645
return instance_count > 1 &&
646
si_num_prims_for_vertices(prim, min_vertex_count, vertices_per_patch) < num_prims;
647
}
648
}
649
650
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS> ALWAYS_INLINE
651
static unsigned si_get_ia_multi_vgt_param(struct si_context *sctx,
652
const struct pipe_draw_indirect_info *indirect,
653
enum pipe_prim_type prim, unsigned num_patches,
654
unsigned instance_count, bool primitive_restart,
655
unsigned min_vertex_count, ubyte vertices_per_patch)
656
{
657
union si_vgt_param_key key = sctx->ia_multi_vgt_param_key;
658
unsigned primgroup_size;
659
unsigned ia_multi_vgt_param;
660
661
if (HAS_TESS) {
662
primgroup_size = num_patches; /* must be a multiple of NUM_PATCHES */
663
} else if (HAS_GS) {
664
primgroup_size = 64; /* recommended with a GS */
665
} else {
666
primgroup_size = 128; /* recommended without a GS and tess */
667
}
668
669
key.u.prim = prim;
670
key.u.uses_instancing = (indirect && indirect->buffer) || instance_count > 1;
671
key.u.multi_instances_smaller_than_primgroup =
672
num_instanced_prims_less_than(indirect, prim, min_vertex_count, instance_count,
673
primgroup_size, vertices_per_patch);
674
key.u.primitive_restart = primitive_restart;
675
key.u.count_from_stream_output = indirect && indirect->count_from_stream_output;
676
key.u.line_stipple_enabled = si_is_line_stipple_enabled(sctx);
677
678
ia_multi_vgt_param =
679
sctx->ia_multi_vgt_param[key.index] | S_028AA8_PRIMGROUP_SIZE(primgroup_size - 1);
680
681
if (HAS_GS) {
682
/* GS requirement. */
683
if (GFX_VERSION <= GFX8 &&
684
SI_GS_PER_ES / primgroup_size >= sctx->screen->gs_table_depth - 3)
685
ia_multi_vgt_param |= S_028AA8_PARTIAL_ES_WAVE_ON(1);
686
687
/* GS hw bug with single-primitive instances and SWITCH_ON_EOI.
688
* The hw doc says all multi-SE chips are affected, but Vulkan
689
* only applies it to Hawaii. Do what Vulkan does.
690
*/
691
if (GFX_VERSION == GFX7 &&
692
sctx->family == CHIP_HAWAII && G_028AA8_SWITCH_ON_EOI(ia_multi_vgt_param) &&
693
num_instanced_prims_less_than(indirect, prim, min_vertex_count, instance_count, 2,
694
vertices_per_patch))
695
sctx->flags |= SI_CONTEXT_VGT_FLUSH;
696
}
697
698
return ia_multi_vgt_param;
699
}
700
701
ALWAYS_INLINE
702
static unsigned si_conv_prim_to_gs_out(unsigned mode)
703
{
704
static const int prim_conv[] = {
705
[PIPE_PRIM_POINTS] = V_028A6C_POINTLIST,
706
[PIPE_PRIM_LINES] = V_028A6C_LINESTRIP,
707
[PIPE_PRIM_LINE_LOOP] = V_028A6C_LINESTRIP,
708
[PIPE_PRIM_LINE_STRIP] = V_028A6C_LINESTRIP,
709
[PIPE_PRIM_TRIANGLES] = V_028A6C_TRISTRIP,
710
[PIPE_PRIM_TRIANGLE_STRIP] = V_028A6C_TRISTRIP,
711
[PIPE_PRIM_TRIANGLE_FAN] = V_028A6C_TRISTRIP,
712
[PIPE_PRIM_QUADS] = V_028A6C_TRISTRIP,
713
[PIPE_PRIM_QUAD_STRIP] = V_028A6C_TRISTRIP,
714
[PIPE_PRIM_POLYGON] = V_028A6C_TRISTRIP,
715
[PIPE_PRIM_LINES_ADJACENCY] = V_028A6C_LINESTRIP,
716
[PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_028A6C_LINESTRIP,
717
[PIPE_PRIM_TRIANGLES_ADJACENCY] = V_028A6C_TRISTRIP,
718
[PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_028A6C_TRISTRIP,
719
[PIPE_PRIM_PATCHES] = V_028A6C_POINTLIST,
720
[SI_PRIM_RECTANGLE_LIST] = V_028A6C_RECTLIST,
721
};
722
assert(mode < ARRAY_SIZE(prim_conv));
723
724
return prim_conv[mode];
725
}
726
727
/* rast_prim is the primitive type after GS. */
728
template<chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS, si_has_ngg NGG> ALWAYS_INLINE
729
static void si_emit_rasterizer_prim_state(struct si_context *sctx)
730
{
731
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
732
enum pipe_prim_type rast_prim = sctx->current_rast_prim;
733
struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
734
735
radeon_begin(cs);
736
737
if (unlikely(si_is_line_stipple_enabled(sctx))) {
738
/* For lines, reset the stipple pattern at each primitive. Otherwise,
739
* reset the stipple pattern at each packet (line strips, line loops).
740
*/
741
bool reset_per_prim = rast_prim == PIPE_PRIM_LINES ||
742
rast_prim == PIPE_PRIM_LINES_ADJACENCY;
743
/* 0 = no reset, 1 = reset per prim, 2 = reset per packet */
744
unsigned value =
745
rs->pa_sc_line_stipple | S_028A0C_AUTO_RESET_CNTL(reset_per_prim ? 1 : 2);
746
747
radeon_opt_set_context_reg(sctx, R_028A0C_PA_SC_LINE_STIPPLE, SI_TRACKED_PA_SC_LINE_STIPPLE,
748
value);
749
}
750
751
unsigned gs_out_prim = si_conv_prim_to_gs_out(rast_prim);
752
if (unlikely(gs_out_prim != sctx->last_gs_out_prim && (NGG || HAS_GS))) {
753
radeon_set_context_reg(cs, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out_prim);
754
sctx->last_gs_out_prim = gs_out_prim;
755
}
756
757
if (GFX_VERSION == GFX9)
758
radeon_end_update_context_roll(sctx);
759
else
760
radeon_end();
761
762
if (NGG) {
763
struct si_shader *hw_vs = si_get_vs_inline(sctx, HAS_TESS, HAS_GS)->current;
764
765
if (hw_vs->uses_vs_state_provoking_vertex) {
766
unsigned vtx_index = rs->flatshade_first ? 0 : gs_out_prim;
767
768
sctx->current_vs_state &= C_VS_STATE_PROVOKING_VTX_INDEX;
769
sctx->current_vs_state |= S_VS_STATE_PROVOKING_VTX_INDEX(vtx_index);
770
}
771
772
if (hw_vs->uses_vs_state_outprim) {
773
sctx->current_vs_state &= C_VS_STATE_OUTPRIM;
774
sctx->current_vs_state |= S_VS_STATE_OUTPRIM(gs_out_prim);
775
}
776
}
777
}
778
779
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS, si_has_ngg NGG>
780
ALWAYS_INLINE
781
static void si_emit_vs_state(struct si_context *sctx, unsigned index_size)
782
{
783
if (sctx->num_vs_blit_sgprs) {
784
/* Re-emit the state after we leave u_blitter. */
785
sctx->last_vs_state = ~0;
786
return;
787
}
788
789
if (sctx->shader.vs.cso->info.uses_base_vertex) {
790
sctx->current_vs_state &= C_VS_STATE_INDEXED;
791
sctx->current_vs_state |= S_VS_STATE_INDEXED(!!index_size);
792
}
793
794
if (sctx->current_vs_state != sctx->last_vs_state) {
795
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
796
797
/* For the API vertex shader (VS_STATE_INDEXED, LS_OUT_*). */
798
unsigned vs_base = si_get_user_data_base(GFX_VERSION, HAS_TESS, HAS_GS, NGG,
799
PIPE_SHADER_VERTEX);
800
radeon_begin(cs);
801
radeon_set_sh_reg(cs, vs_base + SI_SGPR_VS_STATE_BITS * 4,
802
sctx->current_vs_state);
803
804
/* Set CLAMP_VERTEX_COLOR and OUTPRIM in the last stage
805
* before the rasterizer.
806
*
807
* For TES or the GS copy shader without NGG:
808
*/
809
if (vs_base != R_00B130_SPI_SHADER_USER_DATA_VS_0) {
810
radeon_set_sh_reg(cs, R_00B130_SPI_SHADER_USER_DATA_VS_0 + SI_SGPR_VS_STATE_BITS * 4,
811
sctx->current_vs_state);
812
}
813
814
/* For NGG: */
815
if (GFX_VERSION >= GFX10 && vs_base != R_00B230_SPI_SHADER_USER_DATA_GS_0) {
816
radeon_set_sh_reg(cs, R_00B230_SPI_SHADER_USER_DATA_GS_0 + SI_SGPR_VS_STATE_BITS * 4,
817
sctx->current_vs_state);
818
}
819
radeon_end();
820
821
sctx->last_vs_state = sctx->current_vs_state;
822
}
823
}
824
825
ALWAYS_INLINE
826
static bool si_prim_restart_index_changed(struct si_context *sctx, bool primitive_restart,
827
unsigned restart_index)
828
{
829
return primitive_restart && (restart_index != sctx->last_restart_index ||
830
sctx->last_restart_index == SI_RESTART_INDEX_UNKNOWN);
831
}
832
833
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS> ALWAYS_INLINE
834
static void si_emit_ia_multi_vgt_param(struct si_context *sctx,
835
const struct pipe_draw_indirect_info *indirect,
836
enum pipe_prim_type prim, unsigned num_patches,
837
unsigned instance_count, bool primitive_restart,
838
unsigned min_vertex_count, ubyte vertices_per_patch)
839
{
840
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
841
unsigned ia_multi_vgt_param;
842
843
ia_multi_vgt_param =
844
si_get_ia_multi_vgt_param<GFX_VERSION, HAS_TESS, HAS_GS>
845
(sctx, indirect, prim, num_patches, instance_count, primitive_restart,
846
min_vertex_count, vertices_per_patch);
847
848
/* Draw state. */
849
if (ia_multi_vgt_param != sctx->last_multi_vgt_param) {
850
radeon_begin(cs);
851
852
if (GFX_VERSION == GFX9)
853
radeon_set_uconfig_reg_idx(cs, sctx->screen, GFX_VERSION,
854
R_030960_IA_MULTI_VGT_PARAM, 4, ia_multi_vgt_param);
855
else if (GFX_VERSION >= GFX7)
856
radeon_set_context_reg_idx(cs, R_028AA8_IA_MULTI_VGT_PARAM, 1, ia_multi_vgt_param);
857
else
858
radeon_set_context_reg(cs, R_028AA8_IA_MULTI_VGT_PARAM, ia_multi_vgt_param);
859
860
radeon_end();
861
862
sctx->last_multi_vgt_param = ia_multi_vgt_param;
863
}
864
}
865
866
/* GFX10 removed IA_MULTI_VGT_PARAM in exchange for GE_CNTL.
867
* We overload last_multi_vgt_param.
868
*/
869
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS, si_has_ngg NGG> ALWAYS_INLINE
870
static void gfx10_emit_ge_cntl(struct si_context *sctx, unsigned num_patches)
871
{
872
union si_vgt_param_key key = sctx->ia_multi_vgt_param_key;
873
unsigned ge_cntl;
874
875
if (NGG) {
876
if (HAS_TESS) {
877
ge_cntl = S_03096C_PRIM_GRP_SIZE(num_patches) |
878
S_03096C_VERT_GRP_SIZE(0) |
879
S_03096C_BREAK_WAVE_AT_EOI(key.u.tess_uses_prim_id);
880
} else {
881
ge_cntl = si_get_vs_inline(sctx, HAS_TESS, HAS_GS)->current->ge_cntl;
882
}
883
} else {
884
unsigned primgroup_size;
885
unsigned vertgroup_size;
886
887
if (HAS_TESS) {
888
primgroup_size = num_patches; /* must be a multiple of NUM_PATCHES */
889
vertgroup_size = 0;
890
} else if (HAS_GS) {
891
unsigned vgt_gs_onchip_cntl = sctx->shader.gs.current->ctx_reg.gs.vgt_gs_onchip_cntl;
892
primgroup_size = G_028A44_GS_PRIMS_PER_SUBGRP(vgt_gs_onchip_cntl);
893
vertgroup_size = G_028A44_ES_VERTS_PER_SUBGRP(vgt_gs_onchip_cntl);
894
} else {
895
primgroup_size = 128; /* recommended without a GS and tess */
896
vertgroup_size = 0;
897
}
898
899
ge_cntl = S_03096C_PRIM_GRP_SIZE(primgroup_size) | S_03096C_VERT_GRP_SIZE(vertgroup_size) |
900
S_03096C_BREAK_WAVE_AT_EOI(key.u.uses_tess && key.u.tess_uses_prim_id);
901
}
902
903
ge_cntl |= S_03096C_PACKET_TO_ONE_PA(si_is_line_stipple_enabled(sctx));
904
905
if (ge_cntl != sctx->last_multi_vgt_param) {
906
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
907
908
radeon_begin(cs);
909
radeon_set_uconfig_reg(cs, R_03096C_GE_CNTL, ge_cntl);
910
radeon_end();
911
sctx->last_multi_vgt_param = ge_cntl;
912
}
913
}
914
915
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS, si_has_ngg NGG> ALWAYS_INLINE
916
static void si_emit_draw_registers(struct si_context *sctx,
917
const struct pipe_draw_indirect_info *indirect,
918
enum pipe_prim_type prim, unsigned num_patches,
919
unsigned instance_count, ubyte vertices_per_patch,
920
bool primitive_restart, unsigned restart_index,
921
unsigned min_vertex_count)
922
{
923
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
924
925
if (GFX_VERSION >= GFX10)
926
gfx10_emit_ge_cntl<GFX_VERSION, HAS_TESS, HAS_GS, NGG>(sctx, num_patches);
927
else
928
si_emit_ia_multi_vgt_param<GFX_VERSION, HAS_TESS, HAS_GS>
929
(sctx, indirect, prim, num_patches, instance_count, primitive_restart,
930
min_vertex_count, vertices_per_patch);
931
932
radeon_begin(cs);
933
934
if (prim != sctx->last_prim) {
935
unsigned vgt_prim = si_conv_pipe_prim(prim);
936
937
if (GFX_VERSION >= GFX10)
938
radeon_set_uconfig_reg(cs, R_030908_VGT_PRIMITIVE_TYPE, vgt_prim);
939
else if (GFX_VERSION >= GFX7)
940
radeon_set_uconfig_reg_idx(cs, sctx->screen, GFX_VERSION, R_030908_VGT_PRIMITIVE_TYPE, 1, vgt_prim);
941
else
942
radeon_set_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE, vgt_prim);
943
944
sctx->last_prim = prim;
945
}
946
947
/* Primitive restart. */
948
if (primitive_restart != sctx->last_primitive_restart_en) {
949
if (GFX_VERSION >= GFX9)
950
radeon_set_uconfig_reg(cs, R_03092C_VGT_MULTI_PRIM_IB_RESET_EN, primitive_restart);
951
else
952
radeon_set_context_reg(cs, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, primitive_restart);
953
954
sctx->last_primitive_restart_en = primitive_restart;
955
}
956
if (si_prim_restart_index_changed(sctx, primitive_restart, restart_index)) {
957
radeon_set_context_reg(cs, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX, restart_index);
958
sctx->last_restart_index = restart_index;
959
if (GFX_VERSION == GFX9)
960
sctx->context_roll = true;
961
}
962
radeon_end();
963
}
964
965
#define EMIT_SQTT_END_DRAW do { \
966
if (GFX_VERSION >= GFX9 && unlikely(sctx->thread_trace_enabled)) { \
967
radeon_begin(&sctx->gfx_cs); \
968
radeon_emit(&sctx->gfx_cs, PKT3(PKT3_EVENT_WRITE, 0, 0)); \
969
radeon_emit(&sctx->gfx_cs, \
970
EVENT_TYPE(V_028A90_THREAD_TRACE_MARKER) | \
971
EVENT_INDEX(0)); \
972
radeon_end(); \
973
} \
974
} while (0)
975
976
template <chip_class GFX_VERSION, si_has_ngg NGG, si_has_prim_discard_cs ALLOW_PRIM_DISCARD_CS>
977
static void si_emit_draw_packets(struct si_context *sctx, const struct pipe_draw_info *info,
978
unsigned drawid_base,
979
const struct pipe_draw_indirect_info *indirect,
980
const struct pipe_draw_start_count_bias *draws,
981
unsigned num_draws, unsigned total_count,
982
struct pipe_resource *indexbuf, unsigned index_size,
983
unsigned index_offset, unsigned instance_count,
984
bool dispatch_prim_discard_cs, unsigned original_index_size)
985
{
986
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
987
988
if (unlikely(sctx->thread_trace_enabled)) {
989
si_sqtt_write_event_marker(sctx, &sctx->gfx_cs, sctx->sqtt_next_event,
990
UINT_MAX, UINT_MAX, UINT_MAX);
991
}
992
993
uint32_t use_opaque = 0;
994
995
if (indirect && indirect->count_from_stream_output) {
996
struct si_streamout_target *t = (struct si_streamout_target *)indirect->count_from_stream_output;
997
998
radeon_begin(cs);
999
radeon_set_context_reg(cs, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE, t->stride_in_dw);
1000
radeon_end();
1001
1002
si_cp_copy_data(sctx, &sctx->gfx_cs, COPY_DATA_REG, NULL,
1003
R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE >> 2, COPY_DATA_SRC_MEM,
1004
t->buf_filled_size, t->buf_filled_size_offset);
1005
use_opaque = S_0287F0_USE_OPAQUE(1);
1006
indirect = NULL;
1007
}
1008
1009
uint32_t index_max_size = 0;
1010
uint64_t index_va = 0;
1011
1012
radeon_begin(cs);
1013
1014
/* draw packet */
1015
if (index_size) {
1016
/* Register shadowing doesn't shadow INDEX_TYPE. */
1017
if (index_size != sctx->last_index_size || sctx->shadowed_regs) {
1018
unsigned index_type;
1019
1020
/* Index type computation. When we look at how we need to translate index_size,
1021
* we can see that we just need 2 shifts to get the hw value.
1022
*
1023
* 1 = 001b --> 10b = 2
1024
* 2 = 010b --> 00b = 0
1025
* 4 = 100b --> 01b = 1
1026
*/
1027
index_type = ((index_size >> 2) | (index_size << 1)) & 0x3;
1028
1029
if (GFX_VERSION <= GFX7 && SI_BIG_ENDIAN) {
1030
/* GFX7 doesn't support ubyte indices. */
1031
index_type |= index_size == 2 ? V_028A7C_VGT_DMA_SWAP_16_BIT
1032
: V_028A7C_VGT_DMA_SWAP_32_BIT;
1033
}
1034
1035
if (GFX_VERSION >= GFX9) {
1036
radeon_set_uconfig_reg_idx(cs, sctx->screen, GFX_VERSION,
1037
R_03090C_VGT_INDEX_TYPE, 2, index_type);
1038
} else {
1039
radeon_emit(cs, PKT3(PKT3_INDEX_TYPE, 0, 0));
1040
radeon_emit(cs, index_type);
1041
}
1042
1043
sctx->last_index_size = index_size;
1044
}
1045
1046
/* If !ALLOW_PRIM_DISCARD_CS, index_size == original_index_size. */
1047
if (!ALLOW_PRIM_DISCARD_CS || original_index_size) {
1048
index_max_size = (indexbuf->width0 - index_offset) >> util_logbase2(original_index_size);
1049
/* Skip draw calls with 0-sized index buffers.
1050
* They cause a hang on some chips, like Navi10-14.
1051
*/
1052
if (!index_max_size) {
1053
radeon_end();
1054
return;
1055
}
1056
1057
index_va = si_resource(indexbuf)->gpu_address + index_offset;
1058
1059
radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(indexbuf), RADEON_USAGE_READ,
1060
RADEON_PRIO_INDEX_BUFFER);
1061
}
1062
} else {
1063
/* On GFX7 and later, non-indexed draws overwrite VGT_INDEX_TYPE,
1064
* so the state must be re-emitted before the next indexed draw.
1065
*/
1066
if (GFX_VERSION >= GFX7)
1067
sctx->last_index_size = -1;
1068
}
1069
1070
unsigned sh_base_reg = sctx->shader_pointers.sh_base[PIPE_SHADER_VERTEX];
1071
bool render_cond_bit = sctx->render_cond_enabled;
1072
1073
if (indirect) {
1074
assert(num_draws == 1);
1075
uint64_t indirect_va = si_resource(indirect->buffer)->gpu_address;
1076
1077
assert(indirect_va % 8 == 0);
1078
1079
si_invalidate_draw_constants(sctx);
1080
1081
radeon_emit(cs, PKT3(PKT3_SET_BASE, 2, 0));
1082
radeon_emit(cs, 1);
1083
radeon_emit(cs, indirect_va);
1084
radeon_emit(cs, indirect_va >> 32);
1085
1086
radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(indirect->buffer),
1087
RADEON_USAGE_READ, RADEON_PRIO_DRAW_INDIRECT);
1088
1089
unsigned di_src_sel = index_size ? V_0287F0_DI_SRC_SEL_DMA : V_0287F0_DI_SRC_SEL_AUTO_INDEX;
1090
1091
assert(indirect->offset % 4 == 0);
1092
1093
if (index_size) {
1094
radeon_emit(cs, PKT3(PKT3_INDEX_BASE, 1, 0));
1095
radeon_emit(cs, index_va);
1096
radeon_emit(cs, index_va >> 32);
1097
1098
radeon_emit(cs, PKT3(PKT3_INDEX_BUFFER_SIZE, 0, 0));
1099
radeon_emit(cs, index_max_size);
1100
}
1101
1102
if (!sctx->screen->has_draw_indirect_multi) {
1103
radeon_emit(cs, PKT3(index_size ? PKT3_DRAW_INDEX_INDIRECT : PKT3_DRAW_INDIRECT, 3,
1104
render_cond_bit));
1105
radeon_emit(cs, indirect->offset);
1106
radeon_emit(cs, (sh_base_reg + SI_SGPR_BASE_VERTEX * 4 - SI_SH_REG_OFFSET) >> 2);
1107
radeon_emit(cs, (sh_base_reg + SI_SGPR_START_INSTANCE * 4 - SI_SH_REG_OFFSET) >> 2);
1108
radeon_emit(cs, di_src_sel);
1109
} else {
1110
uint64_t count_va = 0;
1111
1112
if (indirect->indirect_draw_count) {
1113
struct si_resource *params_buf = si_resource(indirect->indirect_draw_count);
1114
1115
radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, params_buf, RADEON_USAGE_READ,
1116
RADEON_PRIO_DRAW_INDIRECT);
1117
1118
count_va = params_buf->gpu_address + indirect->indirect_draw_count_offset;
1119
}
1120
1121
radeon_emit(cs,
1122
PKT3(index_size ? PKT3_DRAW_INDEX_INDIRECT_MULTI : PKT3_DRAW_INDIRECT_MULTI, 8,
1123
render_cond_bit));
1124
radeon_emit(cs, indirect->offset);
1125
radeon_emit(cs, (sh_base_reg + SI_SGPR_BASE_VERTEX * 4 - SI_SH_REG_OFFSET) >> 2);
1126
radeon_emit(cs, (sh_base_reg + SI_SGPR_START_INSTANCE * 4 - SI_SH_REG_OFFSET) >> 2);
1127
radeon_emit(cs, ((sh_base_reg + SI_SGPR_DRAWID * 4 - SI_SH_REG_OFFSET) >> 2) |
1128
S_2C3_DRAW_INDEX_ENABLE(sctx->shader.vs.cso->info.uses_drawid) |
1129
S_2C3_COUNT_INDIRECT_ENABLE(!!indirect->indirect_draw_count));
1130
radeon_emit(cs, indirect->draw_count);
1131
radeon_emit(cs, count_va);
1132
radeon_emit(cs, count_va >> 32);
1133
radeon_emit(cs, indirect->stride);
1134
radeon_emit(cs, di_src_sel);
1135
}
1136
} else {
1137
/* Register shadowing requires that we always emit PKT3_NUM_INSTANCES. */
1138
if (sctx->shadowed_regs ||
1139
sctx->last_instance_count == SI_INSTANCE_COUNT_UNKNOWN ||
1140
sctx->last_instance_count != instance_count) {
1141
radeon_emit(cs, PKT3(PKT3_NUM_INSTANCES, 0, 0));
1142
radeon_emit(cs, instance_count);
1143
sctx->last_instance_count = instance_count;
1144
}
1145
1146
/* Base vertex and start instance. */
1147
int base_vertex = original_index_size ? draws[0].index_bias : draws[0].start;
1148
1149
bool set_draw_id = sctx->vs_uses_draw_id;
1150
bool set_base_instance = sctx->vs_uses_base_instance;
1151
1152
if (sctx->num_vs_blit_sgprs) {
1153
/* Re-emit draw constants after we leave u_blitter. */
1154
si_invalidate_draw_sh_constants(sctx);
1155
1156
/* Blit VS doesn't use BASE_VERTEX, START_INSTANCE, and DRAWID. */
1157
radeon_set_sh_reg_seq(cs, sh_base_reg + SI_SGPR_VS_BLIT_DATA * 4, sctx->num_vs_blit_sgprs);
1158
radeon_emit_array(cs, sctx->vs_blit_sh_data, sctx->num_vs_blit_sgprs);
1159
} else if (base_vertex != sctx->last_base_vertex ||
1160
sctx->last_base_vertex == SI_BASE_VERTEX_UNKNOWN ||
1161
(set_base_instance &&
1162
(info->start_instance != sctx->last_start_instance ||
1163
sctx->last_start_instance == SI_START_INSTANCE_UNKNOWN)) ||
1164
(set_draw_id &&
1165
(drawid_base != sctx->last_drawid ||
1166
sctx->last_drawid == SI_DRAW_ID_UNKNOWN)) ||
1167
sh_base_reg != sctx->last_sh_base_reg) {
1168
if (set_base_instance) {
1169
radeon_set_sh_reg_seq(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, 3);
1170
radeon_emit(cs, base_vertex);
1171
radeon_emit(cs, drawid_base);
1172
radeon_emit(cs, info->start_instance);
1173
1174
sctx->last_start_instance = info->start_instance;
1175
sctx->last_drawid = drawid_base;
1176
} else if (set_draw_id) {
1177
radeon_set_sh_reg_seq(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, 2);
1178
radeon_emit(cs, base_vertex);
1179
radeon_emit(cs, drawid_base);
1180
1181
sctx->last_drawid = drawid_base;
1182
} else {
1183
radeon_set_sh_reg(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, base_vertex);
1184
}
1185
1186
sctx->last_base_vertex = base_vertex;
1187
sctx->last_sh_base_reg = sh_base_reg;
1188
}
1189
1190
/* Don't update draw_id in the following code if it doesn't increment. */
1191
bool increment_draw_id = num_draws > 1 && set_draw_id && info->increment_draw_id;
1192
1193
if (index_size) {
1194
if (ALLOW_PRIM_DISCARD_CS && dispatch_prim_discard_cs) {
1195
radeon_end();
1196
1197
si_dispatch_prim_discard_cs_and_draw(sctx, info, draws, num_draws,
1198
original_index_size, total_count, index_va,
1199
index_max_size);
1200
EMIT_SQTT_END_DRAW;
1201
return;
1202
}
1203
1204
/* NOT_EOP allows merging multiple draws into 1 wave, but only user VGPRs
1205
* can be changed between draws, and GS fast launch must be disabled.
1206
* NOT_EOP doesn't work on gfx9 and older.
1207
*
1208
* Instead of doing this, which evaluates the case conditions repeatedly:
1209
* for (all draws) {
1210
* if (case1);
1211
* else;
1212
* }
1213
*
1214
* Use this structuring to evaluate the case conditions once:
1215
* if (case1) for (all draws);
1216
* else for (all draws);
1217
*
1218
*/
1219
bool index_bias_varies = num_draws > 1 && info->index_bias_varies;
1220
1221
if (increment_draw_id) {
1222
if (index_bias_varies) {
1223
for (unsigned i = 0; i < num_draws; i++) {
1224
uint64_t va = index_va + draws[i].start * index_size;
1225
1226
if (i > 0) {
1227
radeon_set_sh_reg_seq(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, 2);
1228
radeon_emit(cs, draws[i].index_bias);
1229
radeon_emit(cs, drawid_base + i);
1230
}
1231
1232
radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_2, 4, render_cond_bit));
1233
radeon_emit(cs, index_max_size);
1234
radeon_emit(cs, va);
1235
radeon_emit(cs, va >> 32);
1236
radeon_emit(cs, draws[i].count);
1237
radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA); /* NOT_EOP disabled */
1238
}
1239
if (num_draws > 1) {
1240
sctx->last_base_vertex = draws[num_draws - 1].index_bias;
1241
sctx->last_drawid = drawid_base + num_draws - 1;
1242
}
1243
} else {
1244
/* Only DrawID varies. */
1245
for (unsigned i = 0; i < num_draws; i++) {
1246
uint64_t va = index_va + draws[i].start * index_size;
1247
1248
if (i > 0)
1249
radeon_set_sh_reg(cs, sh_base_reg + SI_SGPR_DRAWID * 4, drawid_base + i);
1250
1251
radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_2, 4, render_cond_bit));
1252
radeon_emit(cs, index_max_size);
1253
radeon_emit(cs, va);
1254
radeon_emit(cs, va >> 32);
1255
radeon_emit(cs, draws[i].count);
1256
radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA); /* NOT_EOP disabled */
1257
}
1258
if (num_draws > 1)
1259
sctx->last_drawid = drawid_base + num_draws - 1;
1260
}
1261
} else {
1262
if (info->index_bias_varies) {
1263
/* Only BaseVertex varies. */
1264
for (unsigned i = 0; i < num_draws; i++) {
1265
uint64_t va = index_va + draws[i].start * index_size;
1266
1267
if (i > 0)
1268
radeon_set_sh_reg(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, draws[i].index_bias);
1269
1270
radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_2, 4, render_cond_bit));
1271
radeon_emit(cs, index_max_size);
1272
radeon_emit(cs, va);
1273
radeon_emit(cs, va >> 32);
1274
radeon_emit(cs, draws[i].count);
1275
radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA); /* NOT_EOP disabled */
1276
}
1277
if (num_draws > 1)
1278
sctx->last_base_vertex = draws[num_draws - 1].index_bias;
1279
} else {
1280
/* DrawID and BaseVertex are constant. */
1281
if (GFX_VERSION == GFX10) {
1282
/* GFX10 has a bug that consecutive draw packets with NOT_EOP must not have
1283
* count == 0 in the last draw (which doesn't set NOT_EOP).
1284
*
1285
* So remove all trailing draws with count == 0.
1286
*/
1287
while (num_draws > 1 && !draws[num_draws - 1].count)
1288
num_draws--;
1289
}
1290
1291
for (unsigned i = 0; i < num_draws; i++) {
1292
uint64_t va = index_va + draws[i].start * index_size;
1293
1294
radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_2, 4, render_cond_bit));
1295
radeon_emit(cs, index_max_size);
1296
radeon_emit(cs, va);
1297
radeon_emit(cs, va >> 32);
1298
radeon_emit(cs, draws[i].count);
1299
radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA |
1300
S_0287F0_NOT_EOP(GFX_VERSION >= GFX10 && i < num_draws - 1));
1301
}
1302
}
1303
}
1304
} else {
1305
/* Set the index buffer for fast launch. The VS prolog will load the indices. */
1306
if (NGG && sctx->ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_INDEX_SIZE_PACKED(~0)) {
1307
index_max_size = (indexbuf->width0 - index_offset) >> util_logbase2(original_index_size);
1308
1309
radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(indexbuf),
1310
RADEON_USAGE_READ, RADEON_PRIO_INDEX_BUFFER);
1311
uint64_t base_index_va = si_resource(indexbuf)->gpu_address + index_offset;
1312
1313
for (unsigned i = 0; i < num_draws; i++) {
1314
uint64_t index_va = base_index_va + draws[i].start * original_index_size;
1315
1316
radeon_set_sh_reg_seq(cs, R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS, 2);
1317
radeon_emit(cs, index_va);
1318
radeon_emit(cs, index_va >> 32);
1319
1320
if (i > 0) {
1321
if (increment_draw_id) {
1322
unsigned draw_id = drawid_base + i;
1323
1324
radeon_set_sh_reg(cs, sh_base_reg + SI_SGPR_DRAWID * 4, draw_id);
1325
sctx->last_drawid = draw_id;
1326
}
1327
}
1328
1329
/* TODO: Do index buffer bounds checking? We don't do it in this case. */
1330
radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_AUTO, 1, render_cond_bit));
1331
radeon_emit(cs, draws[i].count);
1332
radeon_emit(cs, V_0287F0_DI_SRC_SEL_AUTO_INDEX);
1333
}
1334
radeon_end();
1335
1336
EMIT_SQTT_END_DRAW;
1337
return;
1338
}
1339
1340
for (unsigned i = 0; i < num_draws; i++) {
1341
if (i > 0) {
1342
if (increment_draw_id) {
1343
unsigned draw_id = drawid_base + i;
1344
1345
radeon_set_sh_reg_seq(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, 2);
1346
radeon_emit(cs, draws[i].start);
1347
radeon_emit(cs, draw_id);
1348
1349
sctx->last_drawid = draw_id;
1350
} else {
1351
radeon_set_sh_reg(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, draws[i].start);
1352
}
1353
}
1354
1355
radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_AUTO, 1, render_cond_bit));
1356
radeon_emit(cs, draws[i].count);
1357
radeon_emit(cs, V_0287F0_DI_SRC_SEL_AUTO_INDEX | use_opaque);
1358
}
1359
if (num_draws > 1 && !sctx->num_vs_blit_sgprs)
1360
sctx->last_base_vertex = draws[num_draws - 1].start;
1361
}
1362
}
1363
radeon_end();
1364
1365
EMIT_SQTT_END_DRAW;
1366
}
1367
1368
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS, si_has_ngg NGG> ALWAYS_INLINE
1369
static bool si_upload_and_prefetch_VB_descriptors(struct si_context *sctx)
1370
{
1371
unsigned count = sctx->num_vertex_elements;
1372
bool pointer_dirty, user_sgprs_dirty;
1373
1374
assert(count <= SI_MAX_ATTRIBS);
1375
1376
if (sctx->vertex_buffers_dirty) {
1377
assert(count);
1378
1379
struct si_vertex_elements *velems = sctx->vertex_elements;
1380
unsigned alloc_size = velems->vb_desc_list_alloc_size;
1381
uint32_t *ptr;
1382
1383
if (alloc_size) {
1384
/* Vertex buffer descriptors are the only ones which are uploaded directly
1385
* and don't go through si_upload_graphics_shader_descriptors.
1386
*/
1387
u_upload_alloc(sctx->b.const_uploader, 0, alloc_size,
1388
si_optimal_tcc_alignment(sctx, alloc_size), &sctx->vb_descriptors_offset,
1389
(struct pipe_resource **)&sctx->vb_descriptors_buffer, (void **)&ptr);
1390
if (!sctx->vb_descriptors_buffer) {
1391
sctx->vb_descriptors_offset = 0;
1392
sctx->vb_descriptors_gpu_list = NULL;
1393
return false;
1394
}
1395
1396
sctx->vb_descriptors_gpu_list = ptr;
1397
radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, sctx->vb_descriptors_buffer,
1398
RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
1399
/* GFX6 doesn't support the L2 prefetch. */
1400
if (GFX_VERSION >= GFX7)
1401
si_cp_dma_prefetch(sctx, &sctx->vb_descriptors_buffer->b.b, sctx->vb_descriptors_offset,
1402
alloc_size);
1403
} else {
1404
si_resource_reference(&sctx->vb_descriptors_buffer, NULL);
1405
}
1406
1407
unsigned first_vb_use_mask = velems->first_vb_use_mask;
1408
unsigned num_vbos_in_user_sgprs = sctx->screen->num_vbos_in_user_sgprs;
1409
1410
for (unsigned i = 0; i < count; i++) {
1411
struct pipe_vertex_buffer *vb;
1412
struct si_resource *buf;
1413
unsigned vbo_index = velems->vertex_buffer_index[i];
1414
uint32_t *desc = i < num_vbos_in_user_sgprs ? &sctx->vb_descriptor_user_sgprs[i * 4]
1415
: &ptr[(i - num_vbos_in_user_sgprs) * 4];
1416
1417
vb = &sctx->vertex_buffer[vbo_index];
1418
buf = si_resource(vb->buffer.resource);
1419
if (!buf) {
1420
memset(desc, 0, 16);
1421
continue;
1422
}
1423
1424
int64_t offset = (int64_t)((int)vb->buffer_offset) + velems->src_offset[i];
1425
1426
if (offset >= buf->b.b.width0) {
1427
assert(offset < buf->b.b.width0);
1428
memset(desc, 0, 16);
1429
continue;
1430
}
1431
1432
uint64_t va = buf->gpu_address + offset;
1433
1434
int64_t num_records = (int64_t)buf->b.b.width0 - offset;
1435
if (GFX_VERSION != GFX8 && vb->stride) {
1436
/* Round up by rounding down and adding 1 */
1437
num_records = (num_records - velems->format_size[i]) / vb->stride + 1;
1438
}
1439
assert(num_records >= 0 && num_records <= UINT_MAX);
1440
1441
uint32_t rsrc_word3 = velems->rsrc_word3[i];
1442
1443
/* OOB_SELECT chooses the out-of-bounds check:
1444
* - 1: index >= NUM_RECORDS (Structured)
1445
* - 3: offset >= NUM_RECORDS (Raw)
1446
*/
1447
if (GFX_VERSION >= GFX10)
1448
rsrc_word3 |= S_008F0C_OOB_SELECT(vb->stride ? V_008F0C_OOB_SELECT_STRUCTURED
1449
: V_008F0C_OOB_SELECT_RAW);
1450
1451
desc[0] = va;
1452
desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) | S_008F04_STRIDE(vb->stride);
1453
desc[2] = num_records;
1454
desc[3] = rsrc_word3;
1455
1456
if (first_vb_use_mask & (1 << i)) {
1457
radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(vb->buffer.resource),
1458
RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
1459
}
1460
}
1461
1462
sctx->vertex_buffers_dirty = false;
1463
1464
pointer_dirty = alloc_size != 0;
1465
user_sgprs_dirty = num_vbos_in_user_sgprs > 0;
1466
} else {
1467
pointer_dirty = sctx->vertex_buffer_pointer_dirty;
1468
user_sgprs_dirty = sctx->vertex_buffer_user_sgprs_dirty;
1469
}
1470
1471
if (pointer_dirty || user_sgprs_dirty) {
1472
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
1473
unsigned num_vbos_in_user_sgprs = sctx->screen->num_vbos_in_user_sgprs;
1474
unsigned sh_base = si_get_user_data_base(GFX_VERSION, HAS_TESS, HAS_GS, NGG,
1475
PIPE_SHADER_VERTEX);
1476
assert(count);
1477
1478
radeon_begin(cs);
1479
1480
/* Set the pointer to vertex buffer descriptors. */
1481
if (pointer_dirty && count > num_vbos_in_user_sgprs) {
1482
/* Find the location of the VB descriptor pointer. */
1483
unsigned sh_dw_offset = SI_VS_NUM_USER_SGPR;
1484
if (GFX_VERSION >= GFX9) {
1485
if (HAS_TESS)
1486
sh_dw_offset = GFX9_TCS_NUM_USER_SGPR;
1487
else if (HAS_GS)
1488
sh_dw_offset = GFX9_VSGS_NUM_USER_SGPR;
1489
}
1490
1491
radeon_set_sh_reg(cs, sh_base + sh_dw_offset * 4,
1492
sctx->vb_descriptors_buffer->gpu_address +
1493
sctx->vb_descriptors_offset);
1494
sctx->vertex_buffer_pointer_dirty = false;
1495
}
1496
1497
/* Set VB descriptors in user SGPRs. */
1498
if (user_sgprs_dirty) {
1499
assert(num_vbos_in_user_sgprs);
1500
1501
unsigned num_sgprs = MIN2(count, num_vbos_in_user_sgprs) * 4;
1502
1503
radeon_set_sh_reg_seq(cs, sh_base + SI_SGPR_VS_VB_DESCRIPTOR_FIRST * 4, num_sgprs);
1504
radeon_emit_array(cs, sctx->vb_descriptor_user_sgprs, num_sgprs);
1505
sctx->vertex_buffer_user_sgprs_dirty = false;
1506
}
1507
radeon_end();
1508
}
1509
1510
return true;
1511
}
1512
1513
static void si_get_draw_start_count(struct si_context *sctx, const struct pipe_draw_info *info,
1514
const struct pipe_draw_indirect_info *indirect,
1515
const struct pipe_draw_start_count_bias *draws,
1516
unsigned num_draws, unsigned *start, unsigned *count)
1517
{
1518
if (indirect && !indirect->count_from_stream_output) {
1519
unsigned indirect_count;
1520
struct pipe_transfer *transfer;
1521
unsigned begin, end;
1522
unsigned map_size;
1523
unsigned *data;
1524
1525
if (indirect->indirect_draw_count) {
1526
data = (unsigned*)
1527
pipe_buffer_map_range(&sctx->b, indirect->indirect_draw_count,
1528
indirect->indirect_draw_count_offset, sizeof(unsigned),
1529
PIPE_MAP_READ, &transfer);
1530
1531
indirect_count = *data;
1532
1533
pipe_buffer_unmap(&sctx->b, transfer);
1534
} else {
1535
indirect_count = indirect->draw_count;
1536
}
1537
1538
if (!indirect_count) {
1539
*start = *count = 0;
1540
return;
1541
}
1542
1543
map_size = (indirect_count - 1) * indirect->stride + 3 * sizeof(unsigned);
1544
data = (unsigned*)
1545
pipe_buffer_map_range(&sctx->b, indirect->buffer, indirect->offset, map_size,
1546
PIPE_MAP_READ, &transfer);
1547
1548
begin = UINT_MAX;
1549
end = 0;
1550
1551
for (unsigned i = 0; i < indirect_count; ++i) {
1552
unsigned count = data[0];
1553
unsigned start = data[2];
1554
1555
if (count > 0) {
1556
begin = MIN2(begin, start);
1557
end = MAX2(end, start + count);
1558
}
1559
1560
data += indirect->stride / sizeof(unsigned);
1561
}
1562
1563
pipe_buffer_unmap(&sctx->b, transfer);
1564
1565
if (begin < end) {
1566
*start = begin;
1567
*count = end - begin;
1568
} else {
1569
*start = *count = 0;
1570
}
1571
} else {
1572
unsigned min_element = UINT_MAX;
1573
unsigned max_element = 0;
1574
1575
for (unsigned i = 0; i < num_draws; i++) {
1576
min_element = MIN2(min_element, draws[i].start);
1577
max_element = MAX2(max_element, draws[i].start + draws[i].count);
1578
}
1579
1580
*start = min_element;
1581
*count = max_element - min_element;
1582
}
1583
}
1584
1585
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS, si_has_ngg NGG>
1586
static void si_emit_all_states(struct si_context *sctx, const struct pipe_draw_info *info,
1587
const struct pipe_draw_indirect_info *indirect,
1588
enum pipe_prim_type prim, unsigned instance_count,
1589
unsigned min_vertex_count, bool primitive_restart,
1590
unsigned skip_atom_mask)
1591
{
1592
unsigned num_patches = 0;
1593
1594
si_emit_rasterizer_prim_state<GFX_VERSION, HAS_TESS, HAS_GS, NGG>(sctx);
1595
if (HAS_TESS)
1596
si_emit_derived_tess_state(sctx, info->vertices_per_patch, &num_patches);
1597
1598
/* Emit state atoms. */
1599
unsigned mask = sctx->dirty_atoms & ~skip_atom_mask;
1600
if (mask) {
1601
do {
1602
sctx->atoms.array[u_bit_scan(&mask)].emit(sctx);
1603
} while (mask);
1604
1605
sctx->dirty_atoms &= skip_atom_mask;
1606
}
1607
1608
/* Emit states. */
1609
mask = sctx->dirty_states;
1610
if (mask) {
1611
do {
1612
unsigned i = u_bit_scan(&mask);
1613
struct si_pm4_state *state = sctx->queued.array[i];
1614
1615
/* All places should unset dirty_states if this doesn't pass. */
1616
assert(state && state != sctx->emitted.array[i]);
1617
1618
si_pm4_emit(sctx, state);
1619
sctx->emitted.array[i] = state;
1620
} while (mask);
1621
1622
sctx->dirty_states = 0;
1623
}
1624
1625
/* Emit draw states. */
1626
si_emit_vs_state<GFX_VERSION, HAS_TESS, HAS_GS, NGG>(sctx, info->index_size);
1627
si_emit_draw_registers<GFX_VERSION, HAS_TESS, HAS_GS, NGG>
1628
(sctx, indirect, prim, num_patches, instance_count, info->vertices_per_patch,
1629
primitive_restart, info->restart_index, min_vertex_count);
1630
}
1631
1632
static bool si_all_vs_resources_read_only(struct si_context *sctx, struct pipe_resource *indexbuf)
1633
{
1634
struct radeon_winsys *ws = sctx->ws;
1635
struct radeon_cmdbuf *cs = &sctx->gfx_cs;
1636
struct si_descriptors *buffers =
1637
&sctx->descriptors[si_const_and_shader_buffer_descriptors_idx(PIPE_SHADER_VERTEX)];
1638
struct si_shader_selector *vs = sctx->shader.vs.cso;
1639
struct si_vertex_elements *velems = sctx->vertex_elements;
1640
unsigned num_velems = velems->count;
1641
unsigned num_images = vs->info.base.num_images;
1642
1643
/* Index buffer. */
1644
if (indexbuf && ws->cs_is_buffer_referenced(cs, si_resource(indexbuf)->buf, RADEON_USAGE_WRITE))
1645
goto has_write_reference;
1646
1647
/* Vertex buffers. */
1648
for (unsigned i = 0; i < num_velems; i++) {
1649
if (!((1 << i) & velems->first_vb_use_mask))
1650
continue;
1651
1652
unsigned vb_index = velems->vertex_buffer_index[i];
1653
struct pipe_resource *res = sctx->vertex_buffer[vb_index].buffer.resource;
1654
if (!res)
1655
continue;
1656
1657
if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf, RADEON_USAGE_WRITE))
1658
goto has_write_reference;
1659
}
1660
1661
/* Constant and shader buffers. */
1662
for (unsigned i = 0; i < buffers->num_active_slots; i++) {
1663
unsigned index = buffers->first_active_slot + i;
1664
struct pipe_resource *res = sctx->const_and_shader_buffers[PIPE_SHADER_VERTEX].buffers[index];
1665
if (!res)
1666
continue;
1667
1668
if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf, RADEON_USAGE_WRITE))
1669
goto has_write_reference;
1670
}
1671
1672
/* Samplers. */
1673
if (vs->info.base.textures_used[0]) {
1674
unsigned num_samplers = BITSET_LAST_BIT(vs->info.base.textures_used);
1675
1676
for (unsigned i = 0; i < num_samplers; i++) {
1677
struct pipe_sampler_view *view = sctx->samplers[PIPE_SHADER_VERTEX].views[i];
1678
if (!view)
1679
continue;
1680
1681
if (ws->cs_is_buffer_referenced(cs, si_resource(view->texture)->buf, RADEON_USAGE_WRITE))
1682
goto has_write_reference;
1683
}
1684
}
1685
1686
/* Images. */
1687
if (num_images) {
1688
for (unsigned i = 0; i < num_images; i++) {
1689
struct pipe_resource *res = sctx->images[PIPE_SHADER_VERTEX].views[i].resource;
1690
if (!res)
1691
continue;
1692
1693
if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf, RADEON_USAGE_WRITE))
1694
goto has_write_reference;
1695
}
1696
}
1697
1698
return true;
1699
1700
has_write_reference:
1701
/* If the current gfx IB has enough packets, flush it to remove write
1702
* references to buffers.
1703
*/
1704
if (cs->prev_dw + cs->current.cdw > 2048) {
1705
si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
1706
assert(si_all_vs_resources_read_only(sctx, indexbuf));
1707
return true;
1708
}
1709
return false;
1710
}
1711
1712
static ALWAYS_INLINE bool pd_msg(const char *s)
1713
{
1714
if (SI_PRIM_DISCARD_DEBUG)
1715
printf("PD failed: %s\n", s);
1716
return false;
1717
}
1718
1719
#define DRAW_CLEANUP do { \
1720
if (index_size && indexbuf != info->index.resource) \
1721
pipe_resource_reference(&indexbuf, NULL); \
1722
} while (0)
1723
1724
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS, si_has_ngg NGG,
1725
si_has_prim_discard_cs ALLOW_PRIM_DISCARD_CS>
1726
static void si_draw_vbo(struct pipe_context *ctx,
1727
const struct pipe_draw_info *info,
1728
unsigned drawid_offset,
1729
const struct pipe_draw_indirect_info *indirect,
1730
const struct pipe_draw_start_count_bias *draws,
1731
unsigned num_draws)
1732
{
1733
/* Keep code that uses the least number of local variables as close to the beginning
1734
* of this function as possible to minimize register pressure.
1735
*
1736
* It doesn't matter where we return due to invalid parameters because such cases
1737
* shouldn't occur in practice.
1738
*/
1739
struct si_context *sctx = (struct si_context *)ctx;
1740
1741
/* Recompute and re-emit the texture resource states if needed. */
1742
unsigned dirty_tex_counter = p_atomic_read(&sctx->screen->dirty_tex_counter);
1743
if (unlikely(dirty_tex_counter != sctx->last_dirty_tex_counter)) {
1744
sctx->last_dirty_tex_counter = dirty_tex_counter;
1745
sctx->framebuffer.dirty_cbufs |= ((1 << sctx->framebuffer.state.nr_cbufs) - 1);
1746
sctx->framebuffer.dirty_zsbuf = true;
1747
si_mark_atom_dirty(sctx, &sctx->atoms.s.framebuffer);
1748
si_update_all_texture_descriptors(sctx);
1749
}
1750
1751
unsigned dirty_buf_counter = p_atomic_read(&sctx->screen->dirty_buf_counter);
1752
if (unlikely(dirty_buf_counter != sctx->last_dirty_buf_counter)) {
1753
sctx->last_dirty_buf_counter = dirty_buf_counter;
1754
/* Rebind all buffers unconditionally. */
1755
si_rebind_buffer(sctx, NULL);
1756
}
1757
1758
si_decompress_textures(sctx, u_bit_consecutive(0, SI_NUM_GRAPHICS_SHADERS));
1759
si_need_gfx_cs_space(sctx, num_draws);
1760
1761
if (HAS_TESS) {
1762
struct si_shader_selector *tcs = sctx->shader.tcs.cso;
1763
1764
/* The rarely occuring tcs == NULL case is not optimized. */
1765
bool same_patch_vertices =
1766
GFX_VERSION >= GFX9 &&
1767
tcs && info->vertices_per_patch == tcs->info.base.tess.tcs_vertices_out;
1768
1769
if (sctx->same_patch_vertices != same_patch_vertices) {
1770
sctx->same_patch_vertices = same_patch_vertices;
1771
sctx->do_update_shaders = true;
1772
}
1773
1774
if (GFX_VERSION == GFX9 && sctx->screen->info.has_ls_vgpr_init_bug) {
1775
/* Determine whether the LS VGPR fix should be applied.
1776
*
1777
* It is only required when num input CPs > num output CPs,
1778
* which cannot happen with the fixed function TCS. We should
1779
* also update this bit when switching from TCS to fixed
1780
* function TCS.
1781
*/
1782
bool ls_vgpr_fix =
1783
tcs && info->vertices_per_patch > tcs->info.base.tess.tcs_vertices_out;
1784
1785
if (ls_vgpr_fix != sctx->ls_vgpr_fix) {
1786
sctx->ls_vgpr_fix = ls_vgpr_fix;
1787
sctx->do_update_shaders = true;
1788
}
1789
}
1790
}
1791
1792
enum pipe_prim_type prim = info->mode;
1793
unsigned instance_count = info->instance_count;
1794
1795
/* GFX6-GFX7 treat instance_count==0 as instance_count==1. There is
1796
* no workaround for indirect draws, but we can at least skip
1797
* direct draws.
1798
* 'instance_count == 0' seems to be problematic on Renoir chips (#4866),
1799
* so simplify the condition and drop these draws for all <= GFX9 chips.
1800
*/
1801
if (GFX_VERSION <= GFX9 && unlikely(!indirect && !instance_count))
1802
return;
1803
1804
struct si_shader_selector *vs = sctx->shader.vs.cso;
1805
if (unlikely(!vs || sctx->num_vertex_elements < vs->num_vs_inputs ||
1806
!sctx->shader.ps.cso || (HAS_TESS != (prim == PIPE_PRIM_PATCHES)))) {
1807
assert(0);
1808
return;
1809
}
1810
1811
if (GFX_VERSION <= GFX9 && HAS_GS) {
1812
/* Determine whether the GS triangle strip adjacency fix should
1813
* be applied. Rotate every other triangle if triangle strips with
1814
* adjacency are fed to the GS. This doesn't work if primitive
1815
* restart occurs after an odd number of triangles.
1816
*/
1817
bool gs_tri_strip_adj_fix =
1818
!HAS_TESS && prim == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY;
1819
1820
if (gs_tri_strip_adj_fix != sctx->gs_tri_strip_adj_fix) {
1821
sctx->gs_tri_strip_adj_fix = gs_tri_strip_adj_fix;
1822
sctx->do_update_shaders = true;
1823
}
1824
}
1825
1826
struct pipe_resource *indexbuf = info->index.resource;
1827
unsigned index_size = info->index_size;
1828
unsigned index_offset = indirect && indirect->buffer ? draws[0].start * index_size : 0;
1829
1830
if (index_size) {
1831
/* Translate or upload, if needed. */
1832
/* 8-bit indices are supported on GFX8. */
1833
if (GFX_VERSION <= GFX7 && index_size == 1) {
1834
unsigned start, count, start_offset, size, offset;
1835
void *ptr;
1836
1837
si_get_draw_start_count(sctx, info, indirect, draws, num_draws, &start, &count);
1838
start_offset = start * 2;
1839
size = count * 2;
1840
1841
indexbuf = NULL;
1842
u_upload_alloc(ctx->stream_uploader, start_offset, size,
1843
si_optimal_tcc_alignment(sctx, size), &offset, &indexbuf, &ptr);
1844
if (unlikely(!indexbuf))
1845
return;
1846
1847
util_shorten_ubyte_elts_to_userptr(&sctx->b, info, 0, 0, index_offset + start, count, ptr);
1848
1849
/* info->start will be added by the drawing code */
1850
index_offset = offset - start_offset;
1851
index_size = 2;
1852
} else if (info->has_user_indices) {
1853
unsigned start_offset;
1854
1855
assert(!indirect);
1856
assert(num_draws == 1);
1857
start_offset = draws[0].start * index_size;
1858
1859
indexbuf = NULL;
1860
u_upload_data(ctx->stream_uploader, start_offset, draws[0].count * index_size,
1861
sctx->screen->info.tcc_cache_line_size,
1862
(char *)info->index.user + start_offset, &index_offset, &indexbuf);
1863
if (unlikely(!indexbuf))
1864
return;
1865
1866
/* info->start will be added by the drawing code */
1867
index_offset -= start_offset;
1868
} else if (GFX_VERSION <= GFX7 && si_resource(indexbuf)->TC_L2_dirty) {
1869
/* GFX8 reads index buffers through TC L2, so it doesn't
1870
* need this. */
1871
sctx->flags |= SI_CONTEXT_WB_L2;
1872
si_resource(indexbuf)->TC_L2_dirty = false;
1873
}
1874
}
1875
1876
unsigned min_direct_count = 0;
1877
unsigned total_direct_count = 0;
1878
1879
if (indirect) {
1880
/* Add the buffer size for memory checking in need_cs_space. */
1881
if (indirect->buffer)
1882
si_context_add_resource_size(sctx, indirect->buffer);
1883
1884
/* Indirect buffers use TC L2 on GFX9, but not older hw. */
1885
if (GFX_VERSION <= GFX8) {
1886
if (indirect->buffer && si_resource(indirect->buffer)->TC_L2_dirty) {
1887
sctx->flags |= SI_CONTEXT_WB_L2;
1888
si_resource(indirect->buffer)->TC_L2_dirty = false;
1889
}
1890
1891
if (indirect->indirect_draw_count &&
1892
si_resource(indirect->indirect_draw_count)->TC_L2_dirty) {
1893
sctx->flags |= SI_CONTEXT_WB_L2;
1894
si_resource(indirect->indirect_draw_count)->TC_L2_dirty = false;
1895
}
1896
}
1897
} else {
1898
total_direct_count = min_direct_count = draws[0].count;
1899
1900
for (unsigned i = 1; i < num_draws; i++) {
1901
unsigned count = draws[i].count;
1902
1903
total_direct_count += count;
1904
min_direct_count = MIN2(min_direct_count, count);
1905
}
1906
}
1907
1908
struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1909
bool primitive_restart =
1910
info->primitive_restart &&
1911
(!sctx->screen->options.prim_restart_tri_strips_only ||
1912
(prim != PIPE_PRIM_TRIANGLE_STRIP && prim != PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY));
1913
bool dispatch_prim_discard_cs = false;
1914
bool prim_discard_cs_instancing = false;
1915
unsigned original_index_size = index_size;
1916
1917
/* Determine if we can use the primitive discard compute shader. */
1918
/* TODO: this requires that primitives can be drawn out of order, so check depth/stencil/blend states. */
1919
if (ALLOW_PRIM_DISCARD_CS &&
1920
(total_direct_count > sctx->prim_discard_vertex_count_threshold
1921
? (sctx->compute_num_verts_rejected += total_direct_count, true)
1922
: /* Add, then return true. */
1923
(sctx->compute_num_verts_ineligible += total_direct_count,
1924
false)) && /* Add, then return false. */
1925
(!primitive_restart || pd_msg("primitive restart")) &&
1926
/* Supported prim types. */
1927
(1 << prim) & ((1 << PIPE_PRIM_TRIANGLES) | (1 << PIPE_PRIM_TRIANGLE_STRIP)) &&
1928
/* Instancing is limited to 16-bit indices, because InstanceID is packed into VertexID. */
1929
/* Instanced index_size == 0 requires that start + count < USHRT_MAX, so just reject it. */
1930
(instance_count == 1 ||
1931
(instance_count <= USHRT_MAX && index_size && index_size <= 2) ||
1932
pd_msg("instance_count too large or index_size == 4 or DrawArraysInstanced")) &&
1933
((drawid_offset == 0 && (num_draws == 1 || !info->increment_draw_id)) ||
1934
!sctx->shader.vs.cso->info.uses_drawid || pd_msg("draw_id > 0")) &&
1935
(!sctx->render_cond || pd_msg("render condition")) &&
1936
/* Forced enablement ignores pipeline statistics queries. */
1937
(sctx->screen->debug_flags & (DBG(PD) | DBG(ALWAYS_PD)) ||
1938
(!sctx->num_pipeline_stat_queries && !sctx->streamout.prims_gen_query_enabled) ||
1939
pd_msg("pipestat or primgen query")) &&
1940
(!sctx->vertex_elements->instance_divisor_is_fetched || pd_msg("loads instance divisors")) &&
1941
(!sctx->shader.ps.cso->info.uses_primid || pd_msg("PS uses PrimID")) &&
1942
!rs->polygon_mode_enabled &&
1943
#if SI_PRIM_DISCARD_DEBUG /* same as cso->prim_discard_cs_allowed */
1944
(!sctx->shader.vs.cso->info.uses_bindless_images || pd_msg("uses bindless images")) &&
1945
(!sctx->shader.vs.cso->info.uses_bindless_samplers || pd_msg("uses bindless samplers")) &&
1946
(!sctx->shader.vs.cso->info.base.writes_memory || pd_msg("writes memory")) &&
1947
(!sctx->shader.vs.cso->info.writes_viewport_index || pd_msg("writes viewport index")) &&
1948
!sctx->shader.vs.cso->info.base.vs.window_space_position &&
1949
!sctx->shader.vs.cso->so.num_outputs &&
1950
#else
1951
(sctx->shader.vs.cso->prim_discard_cs_allowed ||
1952
pd_msg("VS shader uses unsupported features")) &&
1953
#endif
1954
/* Check that all buffers are used for read only, because compute
1955
* dispatches can run ahead. */
1956
(si_all_vs_resources_read_only(sctx, index_size ? indexbuf : NULL) ||
1957
pd_msg("write reference"))) {
1958
switch (si_prepare_prim_discard_or_split_draw(sctx, info, drawid_offset, draws, num_draws,
1959
total_direct_count)) {
1960
case SI_PRIM_DISCARD_ENABLED:
1961
original_index_size = index_size;
1962
prim_discard_cs_instancing = instance_count > 1;
1963
dispatch_prim_discard_cs = true;
1964
1965
/* The compute shader changes/lowers the following: */
1966
prim = PIPE_PRIM_TRIANGLES;
1967
index_size = 4;
1968
instance_count = 1;
1969
sctx->compute_num_verts_rejected -= total_direct_count;
1970
sctx->compute_num_verts_accepted += total_direct_count;
1971
break;
1972
case SI_PRIM_DISCARD_DISABLED:
1973
break;
1974
case SI_PRIM_DISCARD_DRAW_SPLIT:
1975
case SI_PRIM_DISCARD_MULTI_DRAW_SPLIT:
1976
sctx->compute_num_verts_rejected -= total_direct_count;
1977
/* The multi draw was split into multiple ones and executed. Return. */
1978
DRAW_CLEANUP;
1979
return;
1980
}
1981
}
1982
1983
if (ALLOW_PRIM_DISCARD_CS &&
1984
prim_discard_cs_instancing != sctx->prim_discard_cs_instancing) {
1985
sctx->prim_discard_cs_instancing = prim_discard_cs_instancing;
1986
sctx->do_update_shaders = true;
1987
}
1988
1989
/* Set the rasterization primitive type.
1990
*
1991
* This must be done after si_decompress_textures, which can call
1992
* draw_vbo recursively, and before si_update_shaders, which uses
1993
* current_rast_prim for this draw_vbo call.
1994
*/
1995
if (!HAS_GS && !HAS_TESS) {
1996
enum pipe_prim_type rast_prim;
1997
1998
if (util_rast_prim_is_triangles(prim)) {
1999
rast_prim = PIPE_PRIM_TRIANGLES;
2000
} else {
2001
/* Only possibilities, POINTS, LINE*, RECTANGLES */
2002
rast_prim = prim;
2003
}
2004
2005
if (rast_prim != sctx->current_rast_prim) {
2006
if (util_prim_is_points_or_lines(sctx->current_rast_prim) !=
2007
util_prim_is_points_or_lines(rast_prim))
2008
si_mark_atom_dirty(sctx, &sctx->atoms.s.guardband);
2009
2010
sctx->current_rast_prim = rast_prim;
2011
sctx->do_update_shaders = true;
2012
}
2013
}
2014
2015
/* Update NGG culling settings. */
2016
uint8_t old_ngg_culling = sctx->ngg_culling;
2017
if (GFX_VERSION >= GFX10) {
2018
struct si_shader_selector *hw_vs = si_get_vs_inline(sctx, HAS_TESS, HAS_GS)->cso;
2019
2020
if (NGG && !HAS_GS && !dispatch_prim_discard_cs &&
2021
/* Tessellation sets ngg_cull_vert_threshold to UINT_MAX if the prim type
2022
* is not triangles, so this check is only needed without tessellation. */
2023
(HAS_TESS || sctx->current_rast_prim == PIPE_PRIM_TRIANGLES) &&
2024
total_direct_count > hw_vs->ngg_cull_vert_threshold) {
2025
uint8_t ngg_culling = sctx->viewport0_y_inverted ? rs->ngg_cull_flags_y_inverted :
2026
rs->ngg_cull_flags;
2027
2028
/* Use NGG fast launch for certain primitive types.
2029
* A draw must have at least 1 full primitive.
2030
* The fast launch doesn't work with tessellation.
2031
*
2032
* Small instances (including small draws) don't perform well with fast launch.
2033
* It's better to use normal launch with NOT_EOP for small draws, and it's
2034
* always better to use normal launch for small instances.
2035
*/
2036
if (!HAS_TESS && ngg_culling && min_direct_count >= 64 &&
2037
!(sctx->screen->debug_flags & DBG(NO_FAST_LAUNCH))) {
2038
if (prim == PIPE_PRIM_TRIANGLES && !index_size) {
2039
ngg_culling |= SI_NGG_CULL_GS_FAST_LAUNCH_TRI_LIST;
2040
} else if (prim == PIPE_PRIM_TRIANGLE_STRIP) {
2041
if (!index_size) {
2042
ngg_culling |= SI_NGG_CULL_GS_FAST_LAUNCH_TRI_STRIP;
2043
} else if (!primitive_restart) {
2044
ngg_culling |= SI_NGG_CULL_GS_FAST_LAUNCH_TRI_STRIP |
2045
SI_NGG_CULL_GS_FAST_LAUNCH_INDEX_SIZE_PACKED(MIN2(index_size, 3));
2046
/* The index buffer will be emulated. */
2047
index_size = 0;
2048
}
2049
}
2050
}
2051
2052
if (ngg_culling != old_ngg_culling) {
2053
/* If shader compilation is not ready, this setting will be rejected. */
2054
sctx->ngg_culling = ngg_culling;
2055
sctx->do_update_shaders = true;
2056
}
2057
} else if (old_ngg_culling) {
2058
sctx->ngg_culling = 0;
2059
sctx->do_update_shaders = true;
2060
}
2061
}
2062
2063
if (unlikely(sctx->do_update_shaders)) {
2064
if (unlikely(!si_update_shaders(sctx))) {
2065
DRAW_CLEANUP;
2066
return;
2067
}
2068
2069
/* Insert a VGT_FLUSH when enabling fast launch changes to prevent hangs.
2070
* See issues #2418, #2426, #2434
2071
*
2072
* This is the setting that is used by the draw.
2073
*/
2074
if (GFX_VERSION >= GFX10) {
2075
uint8_t ngg_culling = si_get_vs_inline(sctx, HAS_TESS, HAS_GS)->current->key.opt.ngg_culling;
2076
if (GFX_VERSION == GFX10 &&
2077
!(old_ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_ALL) &&
2078
ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_ALL)
2079
sctx->flags |= SI_CONTEXT_VGT_FLUSH;
2080
2081
if (old_ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_INDEX_SIZE_PACKED(~0) &&
2082
!(ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_INDEX_SIZE_PACKED(~0))) {
2083
/* Need to re-set these, because we have bound an index buffer there. */
2084
sctx->shader_pointers_dirty |=
2085
(1u << si_const_and_shader_buffer_descriptors_idx(PIPE_SHADER_GEOMETRY)) |
2086
(1u << si_sampler_and_image_descriptors_idx(PIPE_SHADER_GEOMETRY));
2087
si_mark_atom_dirty(sctx, &sctx->atoms.s.shader_pointers);
2088
}
2089
2090
/* Set this to the correct value determined by si_update_shaders. */
2091
sctx->ngg_culling = ngg_culling;
2092
}
2093
}
2094
2095
/* Since we've called si_context_add_resource_size for vertex buffers,
2096
* this must be called after si_need_cs_space, because we must let
2097
* need_cs_space flush before we add buffers to the buffer list.
2098
*
2099
* This must be done after si_update_shaders because si_update_shaders can
2100
* flush the CS when enabling tess and GS rings.
2101
*/
2102
if (sctx->bo_list_add_all_gfx_resources)
2103
si_gfx_resources_add_all_to_bo_list(sctx);
2104
2105
/* Graphics shader descriptors must be uploaded after si_update_shaders because
2106
* it binds tess and GS ring buffers.
2107
*/
2108
if (unlikely(!si_upload_graphics_shader_descriptors(sctx))) {
2109
DRAW_CLEANUP;
2110
return;
2111
}
2112
2113
/* Vega10/Raven scissor bug workaround. When any context register is
2114
* written (i.e. the GPU rolls the context), PA_SC_VPORT_SCISSOR
2115
* registers must be written too.
2116
*/
2117
unsigned masked_atoms = 0;
2118
bool gfx9_scissor_bug = false;
2119
2120
if (GFX_VERSION == GFX9 && sctx->screen->info.has_gfx9_scissor_bug) {
2121
masked_atoms |= si_get_atom_bit(sctx, &sctx->atoms.s.scissors);
2122
gfx9_scissor_bug = true;
2123
2124
if ((indirect && indirect->count_from_stream_output) ||
2125
sctx->dirty_atoms & si_atoms_that_always_roll_context() ||
2126
sctx->dirty_states & si_states_that_always_roll_context())
2127
sctx->context_roll = true;
2128
}
2129
2130
/* Use optimal packet order based on whether we need to sync the pipeline. */
2131
if (unlikely(sctx->flags & (SI_CONTEXT_FLUSH_AND_INV_CB | SI_CONTEXT_FLUSH_AND_INV_DB |
2132
SI_CONTEXT_PS_PARTIAL_FLUSH | SI_CONTEXT_CS_PARTIAL_FLUSH |
2133
SI_CONTEXT_VS_PARTIAL_FLUSH))) {
2134
/* If we have to wait for idle, set all states first, so that all
2135
* SET packets are processed in parallel with previous draw calls.
2136
* Then draw and prefetch at the end. This ensures that the time
2137
* the CUs are idle is very short.
2138
*/
2139
if (unlikely(sctx->flags & SI_CONTEXT_FLUSH_FOR_RENDER_COND))
2140
masked_atoms |= si_get_atom_bit(sctx, &sctx->atoms.s.render_cond);
2141
2142
/* Emit all states except possibly render condition. */
2143
si_emit_all_states<GFX_VERSION, HAS_TESS, HAS_GS, NGG>
2144
(sctx, info, indirect, prim, instance_count, min_direct_count,
2145
primitive_restart, masked_atoms);
2146
sctx->emit_cache_flush(sctx, &sctx->gfx_cs);
2147
/* <-- CUs are idle here. */
2148
2149
/* This uploads VBO descriptors, sets user SGPRs, and executes the L2 prefetch.
2150
* It should done after cache flushing.
2151
*/
2152
if (unlikely((!si_upload_and_prefetch_VB_descriptors<GFX_VERSION, HAS_TESS, HAS_GS, NGG>(sctx)))) {
2153
DRAW_CLEANUP;
2154
return;
2155
}
2156
2157
if (si_is_atom_dirty(sctx, &sctx->atoms.s.render_cond)) {
2158
sctx->atoms.s.render_cond.emit(sctx);
2159
sctx->dirty_atoms &= ~si_get_atom_bit(sctx, &sctx->atoms.s.render_cond);
2160
}
2161
2162
if (GFX_VERSION == GFX9 && gfx9_scissor_bug &&
2163
(sctx->context_roll || si_is_atom_dirty(sctx, &sctx->atoms.s.scissors))) {
2164
sctx->atoms.s.scissors.emit(sctx);
2165
sctx->dirty_atoms &= ~si_get_atom_bit(sctx, &sctx->atoms.s.scissors);
2166
}
2167
assert(sctx->dirty_atoms == 0);
2168
2169
si_emit_draw_packets<GFX_VERSION, NGG, ALLOW_PRIM_DISCARD_CS>
2170
(sctx, info, drawid_offset, indirect, draws, num_draws, total_direct_count, indexbuf,
2171
index_size, index_offset, instance_count, dispatch_prim_discard_cs,
2172
original_index_size);
2173
/* <-- CUs are busy here. */
2174
2175
/* Start prefetches after the draw has been started. Both will run
2176
* in parallel, but starting the draw first is more important.
2177
*/
2178
si_prefetch_shaders<GFX_VERSION, HAS_TESS, HAS_GS, NGG, PREFETCH_ALL>(sctx);
2179
} else {
2180
/* If we don't wait for idle, start prefetches first, then set
2181
* states, and draw at the end.
2182
*/
2183
if (sctx->flags)
2184
sctx->emit_cache_flush(sctx, &sctx->gfx_cs);
2185
2186
/* Only prefetch the API VS and VBO descriptors. */
2187
si_prefetch_shaders<GFX_VERSION, HAS_TESS, HAS_GS, NGG, PREFETCH_BEFORE_DRAW>(sctx);
2188
2189
/* This uploads VBO descriptors, sets user SGPRs, and executes the L2 prefetch.
2190
* It should done after cache flushing and after the VS prefetch.
2191
*/
2192
if (unlikely((!si_upload_and_prefetch_VB_descriptors<GFX_VERSION, HAS_TESS, HAS_GS, NGG>(sctx)))) {
2193
DRAW_CLEANUP;
2194
return;
2195
}
2196
2197
si_emit_all_states<GFX_VERSION, HAS_TESS, HAS_GS, NGG>
2198
(sctx, info, indirect, prim, instance_count, min_direct_count,
2199
primitive_restart, masked_atoms);
2200
2201
if (GFX_VERSION == GFX9 && gfx9_scissor_bug &&
2202
(sctx->context_roll || si_is_atom_dirty(sctx, &sctx->atoms.s.scissors))) {
2203
sctx->atoms.s.scissors.emit(sctx);
2204
sctx->dirty_atoms &= ~si_get_atom_bit(sctx, &sctx->atoms.s.scissors);
2205
}
2206
assert(sctx->dirty_atoms == 0);
2207
2208
si_emit_draw_packets<GFX_VERSION, NGG, ALLOW_PRIM_DISCARD_CS>
2209
(sctx, info, drawid_offset, indirect, draws, num_draws, total_direct_count, indexbuf,
2210
index_size, index_offset, instance_count, dispatch_prim_discard_cs,
2211
original_index_size);
2212
2213
/* Prefetch the remaining shaders after the draw has been
2214
* started. */
2215
si_prefetch_shaders<GFX_VERSION, HAS_TESS, HAS_GS, NGG, PREFETCH_AFTER_DRAW>(sctx);
2216
}
2217
2218
/* Clear the context roll flag after the draw call.
2219
* Only used by the gfx9 scissor bug.
2220
*/
2221
if (GFX_VERSION == GFX9)
2222
sctx->context_roll = false;
2223
2224
if (unlikely(sctx->current_saved_cs)) {
2225
si_trace_emit(sctx);
2226
si_log_draw_state(sctx, sctx->log);
2227
}
2228
2229
/* Workaround for a VGT hang when streamout is enabled.
2230
* It must be done after drawing. */
2231
if (((GFX_VERSION == GFX7 && sctx->family == CHIP_HAWAII) ||
2232
(GFX_VERSION == GFX8 && (sctx->family == CHIP_TONGA || sctx->family == CHIP_FIJI))) &&
2233
si_get_strmout_en(sctx)) {
2234
sctx->flags |= SI_CONTEXT_VGT_STREAMOUT_SYNC;
2235
}
2236
2237
if (unlikely(sctx->decompression_enabled)) {
2238
sctx->num_decompress_calls++;
2239
} else {
2240
sctx->num_draw_calls++;
2241
if (primitive_restart)
2242
sctx->num_prim_restart_calls++;
2243
}
2244
2245
if (!sctx->blitter_running && sctx->framebuffer.state.zsbuf) {
2246
struct si_texture *zstex = (struct si_texture *)sctx->framebuffer.state.zsbuf->texture;
2247
zstex->depth_cleared_level_mask &= ~BITFIELD_BIT(sctx->framebuffer.state.zsbuf->u.tex.level);
2248
}
2249
2250
/* TODO: Set displayable_dcc_dirty if image stores are used. */
2251
2252
DRAW_CLEANUP;
2253
}
2254
2255
static void si_draw_rectangle(struct blitter_context *blitter, void *vertex_elements_cso,
2256
blitter_get_vs_func get_vs, int x1, int y1, int x2, int y2,
2257
float depth, unsigned num_instances, enum blitter_attrib_type type,
2258
const union blitter_attrib *attrib)
2259
{
2260
struct pipe_context *pipe = util_blitter_get_pipe(blitter);
2261
struct si_context *sctx = (struct si_context *)pipe;
2262
2263
/* Pack position coordinates as signed int16. */
2264
sctx->vs_blit_sh_data[0] = (uint32_t)(x1 & 0xffff) | ((uint32_t)(y1 & 0xffff) << 16);
2265
sctx->vs_blit_sh_data[1] = (uint32_t)(x2 & 0xffff) | ((uint32_t)(y2 & 0xffff) << 16);
2266
sctx->vs_blit_sh_data[2] = fui(depth);
2267
2268
switch (type) {
2269
case UTIL_BLITTER_ATTRIB_COLOR:
2270
memcpy(&sctx->vs_blit_sh_data[3], attrib->color, sizeof(float) * 4);
2271
break;
2272
case UTIL_BLITTER_ATTRIB_TEXCOORD_XY:
2273
case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW:
2274
memcpy(&sctx->vs_blit_sh_data[3], &attrib->texcoord, sizeof(attrib->texcoord));
2275
break;
2276
case UTIL_BLITTER_ATTRIB_NONE:;
2277
}
2278
2279
pipe->bind_vs_state(pipe, si_get_blitter_vs(sctx, type, num_instances));
2280
2281
struct pipe_draw_info info = {};
2282
struct pipe_draw_start_count_bias draw;
2283
2284
info.mode = SI_PRIM_RECTANGLE_LIST;
2285
info.instance_count = num_instances;
2286
2287
draw.start = 0;
2288
draw.count = 3;
2289
2290
/* Don't set per-stage shader pointers for VS. */
2291
sctx->shader_pointers_dirty &= ~SI_DESCS_SHADER_MASK(VERTEX);
2292
sctx->vertex_buffer_pointer_dirty = false;
2293
sctx->vertex_buffer_user_sgprs_dirty = false;
2294
2295
pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
2296
}
2297
2298
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS,
2299
si_has_ngg NGG, si_has_prim_discard_cs ALLOW_PRIM_DISCARD_CS>
2300
static void si_init_draw_vbo(struct si_context *sctx)
2301
{
2302
/* Prim discard CS is only useful on gfx7+ because gfx6 doesn't have async compute. */
2303
if (ALLOW_PRIM_DISCARD_CS && GFX_VERSION < GFX8)
2304
return;
2305
2306
if (ALLOW_PRIM_DISCARD_CS && (HAS_TESS || HAS_GS))
2307
return;
2308
2309
if (NGG && GFX_VERSION < GFX10)
2310
return;
2311
2312
sctx->draw_vbo[HAS_TESS][HAS_GS][NGG][ALLOW_PRIM_DISCARD_CS] =
2313
si_draw_vbo<GFX_VERSION, HAS_TESS, HAS_GS, NGG, ALLOW_PRIM_DISCARD_CS>;
2314
}
2315
2316
template <chip_class GFX_VERSION, si_has_tess HAS_TESS, si_has_gs HAS_GS>
2317
static void si_init_draw_vbo_all_internal_options(struct si_context *sctx)
2318
{
2319
si_init_draw_vbo<GFX_VERSION, HAS_TESS, HAS_GS, NGG_OFF, PRIM_DISCARD_CS_OFF>(sctx);
2320
si_init_draw_vbo<GFX_VERSION, HAS_TESS, HAS_GS, NGG_OFF, PRIM_DISCARD_CS_ON>(sctx);
2321
si_init_draw_vbo<GFX_VERSION, HAS_TESS, HAS_GS, NGG_ON, PRIM_DISCARD_CS_OFF>(sctx);
2322
si_init_draw_vbo<GFX_VERSION, HAS_TESS, HAS_GS, NGG_ON, PRIM_DISCARD_CS_ON>(sctx);
2323
}
2324
2325
template <chip_class GFX_VERSION>
2326
static void si_init_draw_vbo_all_pipeline_options(struct si_context *sctx)
2327
{
2328
si_init_draw_vbo_all_internal_options<GFX_VERSION, TESS_OFF, GS_OFF>(sctx);
2329
si_init_draw_vbo_all_internal_options<GFX_VERSION, TESS_OFF, GS_ON>(sctx);
2330
si_init_draw_vbo_all_internal_options<GFX_VERSION, TESS_ON, GS_OFF>(sctx);
2331
si_init_draw_vbo_all_internal_options<GFX_VERSION, TESS_ON, GS_ON>(sctx);
2332
}
2333
2334
static void si_invalid_draw_vbo(struct pipe_context *pipe,
2335
const struct pipe_draw_info *info,
2336
unsigned drawid_offset,
2337
const struct pipe_draw_indirect_info *indirect,
2338
const struct pipe_draw_start_count_bias *draws,
2339
unsigned num_draws)
2340
{
2341
unreachable("vertex shader not bound");
2342
}
2343
2344
extern "C"
2345
void GFX(si_init_draw_functions_)(struct si_context *sctx)
2346
{
2347
assert(sctx->chip_class == GFX());
2348
2349
si_init_draw_vbo_all_pipeline_options<GFX()>(sctx);
2350
2351
/* Bind a fake draw_vbo, so that draw_vbo isn't NULL, which would skip
2352
* initialization of callbacks in upper layers (such as u_threaded_context).
2353
*/
2354
sctx->b.draw_vbo = si_invalid_draw_vbo;
2355
sctx->blitter->draw_rectangle = si_draw_rectangle;
2356
2357
si_init_ia_multi_vgt_param_table(sctx);
2358
}
2359
2360