radeonsi: determine secure flag must be set for gfx IB
[mesa.git] / src / gallium / drivers / radeonsi / si_state_draw.c
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_debug.h"
26 #include "si_build_pm4.h"
27 #include "sid.h"
28 #include "util/u_index_modify.h"
29 #include "util/u_log.h"
30 #include "util/u_prim.h"
31 #include "util/u_suballoc.h"
32 #include "util/u_upload_mgr.h"
33
34 /* special primitive types */
35 #define SI_PRIM_RECTANGLE_LIST PIPE_PRIM_MAX
36
37 static unsigned si_conv_pipe_prim(unsigned mode)
38 {
39 static const unsigned prim_conv[] = {
40 [PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
41 [PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
42 [PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
43 [PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
44 [PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
45 [PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
46 [PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
47 [PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
48 [PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
49 [PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
50 [PIPE_PRIM_LINES_ADJACENCY] = V_008958_DI_PT_LINELIST_ADJ,
51 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_008958_DI_PT_LINESTRIP_ADJ,
52 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_008958_DI_PT_TRILIST_ADJ,
53 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_008958_DI_PT_TRISTRIP_ADJ,
54 [PIPE_PRIM_PATCHES] = V_008958_DI_PT_PATCH,
55 [SI_PRIM_RECTANGLE_LIST] = V_008958_DI_PT_RECTLIST};
56 assert(mode < ARRAY_SIZE(prim_conv));
57 return prim_conv[mode];
58 }
59
60 /**
61 * This calculates the LDS size for tessellation shaders (VS, TCS, TES).
62 * LS.LDS_SIZE is shared by all 3 shader stages.
63 *
64 * The information about LDS and other non-compile-time parameters is then
65 * written to userdata SGPRs.
66 */
67 static void si_emit_derived_tess_state(struct si_context *sctx, const struct pipe_draw_info *info,
68 unsigned *num_patches)
69 {
70 struct radeon_cmdbuf *cs = sctx->gfx_cs;
71 struct si_shader *ls_current;
72 struct si_shader_selector *ls;
73 /* The TES pointer will only be used for sctx->last_tcs.
74 * It would be wrong to think that TCS = TES. */
75 struct si_shader_selector *tcs =
76 sctx->tcs_shader.cso ? sctx->tcs_shader.cso : sctx->tes_shader.cso;
77 unsigned tess_uses_primid = sctx->ia_multi_vgt_param_key.u.tess_uses_prim_id;
78 bool has_primid_instancing_bug = sctx->chip_class == GFX6 && sctx->screen->info.max_se == 1;
79 unsigned tes_sh_base = sctx->shader_pointers.sh_base[PIPE_SHADER_TESS_EVAL];
80 unsigned num_tcs_input_cp = info->vertices_per_patch;
81 unsigned num_tcs_output_cp, num_tcs_inputs, num_tcs_outputs;
82 unsigned num_tcs_patch_outputs;
83 unsigned input_vertex_size, output_vertex_size, pervertex_output_patch_size;
84 unsigned input_patch_size, output_patch_size, output_patch0_offset;
85 unsigned perpatch_output_offset, lds_size;
86 unsigned tcs_in_layout, tcs_out_layout, tcs_out_offsets;
87 unsigned offchip_layout, hardware_lds_size, ls_hs_config;
88
89 /* Since GFX9 has merged LS-HS in the TCS state, set LS = TCS. */
90 if (sctx->chip_class >= GFX9) {
91 if (sctx->tcs_shader.cso)
92 ls_current = sctx->tcs_shader.current;
93 else
94 ls_current = sctx->fixed_func_tcs_shader.current;
95
96 ls = ls_current->key.part.tcs.ls;
97 } else {
98 ls_current = sctx->vs_shader.current;
99 ls = sctx->vs_shader.cso;
100 }
101
102 if (sctx->last_ls == ls_current && sctx->last_tcs == tcs &&
103 sctx->last_tes_sh_base == tes_sh_base && sctx->last_num_tcs_input_cp == num_tcs_input_cp &&
104 (!has_primid_instancing_bug || (sctx->last_tess_uses_primid == tess_uses_primid))) {
105 *num_patches = sctx->last_num_patches;
106 return;
107 }
108
109 sctx->last_ls = ls_current;
110 sctx->last_tcs = tcs;
111 sctx->last_tes_sh_base = tes_sh_base;
112 sctx->last_num_tcs_input_cp = num_tcs_input_cp;
113 sctx->last_tess_uses_primid = tess_uses_primid;
114
115 /* This calculates how shader inputs and outputs among VS, TCS, and TES
116 * are laid out in LDS. */
117 num_tcs_inputs = util_last_bit64(ls->outputs_written);
118
119 if (sctx->tcs_shader.cso) {
120 num_tcs_outputs = util_last_bit64(tcs->outputs_written);
121 num_tcs_output_cp = tcs->info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
122 num_tcs_patch_outputs = util_last_bit64(tcs->patch_outputs_written);
123 } else {
124 /* No TCS. Route varyings from LS to TES. */
125 num_tcs_outputs = num_tcs_inputs;
126 num_tcs_output_cp = num_tcs_input_cp;
127 num_tcs_patch_outputs = 2; /* TESSINNER + TESSOUTER */
128 }
129
130 input_vertex_size = ls->lshs_vertex_stride;
131 output_vertex_size = num_tcs_outputs * 16;
132
133 input_patch_size = num_tcs_input_cp * input_vertex_size;
134
135 pervertex_output_patch_size = num_tcs_output_cp * output_vertex_size;
136 output_patch_size = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
137
138 /* Ensure that we only need one wave per SIMD so we don't need to check
139 * resource usage. Also ensures that the number of tcs in and out
140 * vertices per threadgroup are at most 256.
141 */
142 unsigned max_verts_per_patch = MAX2(num_tcs_input_cp, num_tcs_output_cp);
143 *num_patches = 256 / max_verts_per_patch;
144
145 /* Make sure that the data fits in LDS. This assumes the shaders only
146 * use LDS for the inputs and outputs.
147 *
148 * While GFX7 can use 64K per threadgroup, there is a hang on Stoney
149 * with 2 CUs if we use more than 32K. The closed Vulkan driver also
150 * uses 32K at most on all GCN chips.
151 */
152 hardware_lds_size = 32768;
153 *num_patches = MIN2(*num_patches, hardware_lds_size / (input_patch_size + output_patch_size));
154
155 /* Make sure the output data fits in the offchip buffer */
156 *num_patches =
157 MIN2(*num_patches, (sctx->screen->tess_offchip_block_dw_size * 4) / output_patch_size);
158
159 /* Not necessary for correctness, but improves performance.
160 * The hardware can do more, but the radeonsi shader constant is
161 * limited to 6 bits.
162 */
163 *num_patches = MIN2(*num_patches, 63); /* triangles: 3 full waves except 3 lanes */
164
165 /* When distributed tessellation is unsupported, switch between SEs
166 * at a higher frequency to compensate for it.
167 */
168 if (!sctx->screen->info.has_distributed_tess && sctx->screen->info.max_se > 1)
169 *num_patches = MIN2(*num_patches, 16); /* recommended */
170
171 /* Make sure that vector lanes are reasonably occupied. It probably
172 * doesn't matter much because this is LS-HS, and TES is likely to
173 * occupy significantly more CUs.
174 */
175 unsigned temp_verts_per_tg = *num_patches * max_verts_per_patch;
176 unsigned wave_size = sctx->screen->ge_wave_size;
177
178 if (temp_verts_per_tg > wave_size && temp_verts_per_tg % wave_size < wave_size * 3 / 4)
179 *num_patches = (temp_verts_per_tg & ~(wave_size - 1)) / max_verts_per_patch;
180
181 if (sctx->chip_class == GFX6) {
182 /* GFX6 bug workaround, related to power management. Limit LS-HS
183 * threadgroups to only one wave.
184 */
185 unsigned one_wave = wave_size / max_verts_per_patch;
186 *num_patches = MIN2(*num_patches, one_wave);
187 }
188
189 /* The VGT HS block increments the patch ID unconditionally
190 * within a single threadgroup. This results in incorrect
191 * patch IDs when instanced draws are used.
192 *
193 * The intended solution is to restrict threadgroups to
194 * a single instance by setting SWITCH_ON_EOI, which
195 * should cause IA to split instances up. However, this
196 * doesn't work correctly on GFX6 when there is no other
197 * SE to switch to.
198 */
199 if (has_primid_instancing_bug && tess_uses_primid)
200 *num_patches = 1;
201
202 sctx->last_num_patches = *num_patches;
203
204 output_patch0_offset = input_patch_size * *num_patches;
205 perpatch_output_offset = output_patch0_offset + pervertex_output_patch_size;
206
207 /* Compute userdata SGPRs. */
208 assert(((input_vertex_size / 4) & ~0xff) == 0);
209 assert(((output_vertex_size / 4) & ~0xff) == 0);
210 assert(((input_patch_size / 4) & ~0x1fff) == 0);
211 assert(((output_patch_size / 4) & ~0x1fff) == 0);
212 assert(((output_patch0_offset / 16) & ~0xffff) == 0);
213 assert(((perpatch_output_offset / 16) & ~0xffff) == 0);
214 assert(num_tcs_input_cp <= 32);
215 assert(num_tcs_output_cp <= 32);
216
217 uint64_t ring_va = si_resource(sctx->tess_rings)->gpu_address;
218 assert((ring_va & u_bit_consecutive(0, 19)) == 0);
219
220 tcs_in_layout = S_VS_STATE_LS_OUT_PATCH_SIZE(input_patch_size / 4) |
221 S_VS_STATE_LS_OUT_VERTEX_SIZE(input_vertex_size / 4);
222 tcs_out_layout = (output_patch_size / 4) | (num_tcs_input_cp << 13) | ring_va;
223 tcs_out_offsets = (output_patch0_offset / 16) | ((perpatch_output_offset / 16) << 16);
224 offchip_layout =
225 *num_patches | (num_tcs_output_cp << 6) | (pervertex_output_patch_size * *num_patches << 12);
226
227 /* Compute the LDS size. */
228 lds_size = output_patch0_offset + output_patch_size * *num_patches;
229
230 if (sctx->chip_class >= GFX7) {
231 assert(lds_size <= 65536);
232 lds_size = align(lds_size, 512) / 512;
233 } else {
234 assert(lds_size <= 32768);
235 lds_size = align(lds_size, 256) / 256;
236 }
237
238 /* Set SI_SGPR_VS_STATE_BITS. */
239 sctx->current_vs_state &= C_VS_STATE_LS_OUT_PATCH_SIZE & C_VS_STATE_LS_OUT_VERTEX_SIZE;
240 sctx->current_vs_state |= tcs_in_layout;
241
242 /* We should be able to support in-shader LDS use with LLVM >= 9
243 * by just adding the lds_sizes together, but it has never
244 * been tested. */
245 assert(ls_current->config.lds_size == 0);
246
247 if (sctx->chip_class >= GFX9) {
248 unsigned hs_rsrc2 = ls_current->config.rsrc2;
249
250 if (sctx->chip_class >= GFX10)
251 hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX10(lds_size);
252 else
253 hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX9(lds_size);
254
255 radeon_set_sh_reg(cs, R_00B42C_SPI_SHADER_PGM_RSRC2_HS, hs_rsrc2);
256
257 /* Set userdata SGPRs for merged LS-HS. */
258 radeon_set_sh_reg_seq(
259 cs, R_00B430_SPI_SHADER_USER_DATA_LS_0 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT * 4, 3);
260 radeon_emit(cs, offchip_layout);
261 radeon_emit(cs, tcs_out_offsets);
262 radeon_emit(cs, tcs_out_layout);
263 } else {
264 unsigned ls_rsrc2 = ls_current->config.rsrc2;
265
266 si_multiwave_lds_size_workaround(sctx->screen, &lds_size);
267 ls_rsrc2 |= S_00B52C_LDS_SIZE(lds_size);
268
269 /* Due to a hw bug, RSRC2_LS must be written twice with another
270 * LS register written in between. */
271 if (sctx->chip_class == GFX7 && sctx->family != CHIP_HAWAII)
272 radeon_set_sh_reg(cs, R_00B52C_SPI_SHADER_PGM_RSRC2_LS, ls_rsrc2);
273 radeon_set_sh_reg_seq(cs, R_00B528_SPI_SHADER_PGM_RSRC1_LS, 2);
274 radeon_emit(cs, ls_current->config.rsrc1);
275 radeon_emit(cs, ls_rsrc2);
276
277 /* Set userdata SGPRs for TCS. */
278 radeon_set_sh_reg_seq(
279 cs, R_00B430_SPI_SHADER_USER_DATA_HS_0 + GFX6_SGPR_TCS_OFFCHIP_LAYOUT * 4, 4);
280 radeon_emit(cs, offchip_layout);
281 radeon_emit(cs, tcs_out_offsets);
282 radeon_emit(cs, tcs_out_layout);
283 radeon_emit(cs, tcs_in_layout);
284 }
285
286 /* Set userdata SGPRs for TES. */
287 radeon_set_sh_reg_seq(cs, tes_sh_base + SI_SGPR_TES_OFFCHIP_LAYOUT * 4, 2);
288 radeon_emit(cs, offchip_layout);
289 radeon_emit(cs, ring_va);
290
291 ls_hs_config = S_028B58_NUM_PATCHES(*num_patches) | S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp) |
292 S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp);
293
294 if (sctx->last_ls_hs_config != ls_hs_config) {
295 if (sctx->chip_class >= GFX7) {
296 radeon_set_context_reg_idx(cs, R_028B58_VGT_LS_HS_CONFIG, 2, ls_hs_config);
297 } else {
298 radeon_set_context_reg(cs, R_028B58_VGT_LS_HS_CONFIG, ls_hs_config);
299 }
300 sctx->last_ls_hs_config = ls_hs_config;
301 sctx->context_roll = true;
302 }
303 }
304
305 static unsigned si_num_prims_for_vertices(const struct pipe_draw_info *info,
306 enum pipe_prim_type prim)
307 {
308 switch (prim) {
309 case PIPE_PRIM_PATCHES:
310 return info->count / info->vertices_per_patch;
311 case PIPE_PRIM_POLYGON:
312 return info->count >= 3;
313 case SI_PRIM_RECTANGLE_LIST:
314 return info->count / 3;
315 default:
316 return u_decomposed_prims_for_vertices(prim, info->count);
317 }
318 }
319
320 static unsigned si_get_init_multi_vgt_param(struct si_screen *sscreen, union si_vgt_param_key *key)
321 {
322 STATIC_ASSERT(sizeof(union si_vgt_param_key) == 4);
323 unsigned max_primgroup_in_wave = 2;
324
325 /* SWITCH_ON_EOP(0) is always preferable. */
326 bool wd_switch_on_eop = false;
327 bool ia_switch_on_eop = false;
328 bool ia_switch_on_eoi = false;
329 bool partial_vs_wave = false;
330 bool partial_es_wave = false;
331
332 if (key->u.uses_tess) {
333 /* SWITCH_ON_EOI must be set if PrimID is used. */
334 if (key->u.tess_uses_prim_id)
335 ia_switch_on_eoi = true;
336
337 /* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
338 if ((sscreen->info.family == CHIP_TAHITI || sscreen->info.family == CHIP_PITCAIRN ||
339 sscreen->info.family == CHIP_BONAIRE) &&
340 key->u.uses_gs)
341 partial_vs_wave = true;
342
343 /* Needed for 028B6C_DISTRIBUTION_MODE != 0. (implies >= GFX8) */
344 if (sscreen->info.has_distributed_tess) {
345 if (key->u.uses_gs) {
346 if (sscreen->info.chip_class == GFX8)
347 partial_es_wave = true;
348 } else {
349 partial_vs_wave = true;
350 }
351 }
352 }
353
354 /* This is a hardware requirement. */
355 if (key->u.line_stipple_enabled || (sscreen->debug_flags & DBG(SWITCH_ON_EOP))) {
356 ia_switch_on_eop = true;
357 wd_switch_on_eop = true;
358 }
359
360 if (sscreen->info.chip_class >= GFX7) {
361 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
362 * 4 shader engines. Set 1 to pass the assertion below.
363 * The other cases are hardware requirements.
364 *
365 * Polaris supports primitive restart with WD_SWITCH_ON_EOP=0
366 * for points, line strips, and tri strips.
367 */
368 if (sscreen->info.max_se <= 2 || key->u.prim == PIPE_PRIM_POLYGON ||
369 key->u.prim == PIPE_PRIM_LINE_LOOP || key->u.prim == PIPE_PRIM_TRIANGLE_FAN ||
370 key->u.prim == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY ||
371 (key->u.primitive_restart &&
372 (sscreen->info.family < CHIP_POLARIS10 ||
373 (key->u.prim != PIPE_PRIM_POINTS && key->u.prim != PIPE_PRIM_LINE_STRIP &&
374 key->u.prim != PIPE_PRIM_TRIANGLE_STRIP))) ||
375 key->u.count_from_stream_output)
376 wd_switch_on_eop = true;
377
378 /* Hawaii hangs if instancing is enabled and WD_SWITCH_ON_EOP is 0.
379 * We don't know that for indirect drawing, so treat it as
380 * always problematic. */
381 if (sscreen->info.family == CHIP_HAWAII && key->u.uses_instancing)
382 wd_switch_on_eop = true;
383
384 /* Performance recommendation for 4 SE Gfx7-8 parts if
385 * instances are smaller than a primgroup.
386 * Assume indirect draws always use small instances.
387 * This is needed for good VS wave utilization.
388 */
389 if (sscreen->info.chip_class <= GFX8 && sscreen->info.max_se == 4 &&
390 key->u.multi_instances_smaller_than_primgroup)
391 wd_switch_on_eop = true;
392
393 /* Required on GFX7 and later. */
394 if (sscreen->info.max_se == 4 && !wd_switch_on_eop)
395 ia_switch_on_eoi = true;
396
397 /* HW engineers suggested that PARTIAL_VS_WAVE_ON should be set
398 * to work around a GS hang.
399 */
400 if (key->u.uses_gs &&
401 (sscreen->info.family == CHIP_TONGA || sscreen->info.family == CHIP_FIJI ||
402 sscreen->info.family == CHIP_POLARIS10 || sscreen->info.family == CHIP_POLARIS11 ||
403 sscreen->info.family == CHIP_POLARIS12 || sscreen->info.family == CHIP_VEGAM))
404 partial_vs_wave = true;
405
406 /* Required by Hawaii and, for some special cases, by GFX8. */
407 if (ia_switch_on_eoi &&
408 (sscreen->info.family == CHIP_HAWAII ||
409 (sscreen->info.chip_class == GFX8 && (key->u.uses_gs || max_primgroup_in_wave != 2))))
410 partial_vs_wave = true;
411
412 /* Instancing bug on Bonaire. */
413 if (sscreen->info.family == CHIP_BONAIRE && ia_switch_on_eoi && key->u.uses_instancing)
414 partial_vs_wave = true;
415
416 /* This only applies to Polaris10 and later 4 SE chips.
417 * wd_switch_on_eop is already true on all other chips.
418 */
419 if (!wd_switch_on_eop && key->u.primitive_restart)
420 partial_vs_wave = true;
421
422 /* If the WD switch is false, the IA switch must be false too. */
423 assert(wd_switch_on_eop || !ia_switch_on_eop);
424 }
425
426 /* If SWITCH_ON_EOI is set, PARTIAL_ES_WAVE must be set too. */
427 if (sscreen->info.chip_class <= GFX8 && ia_switch_on_eoi)
428 partial_es_wave = true;
429
430 return S_028AA8_SWITCH_ON_EOP(ia_switch_on_eop) | S_028AA8_SWITCH_ON_EOI(ia_switch_on_eoi) |
431 S_028AA8_PARTIAL_VS_WAVE_ON(partial_vs_wave) |
432 S_028AA8_PARTIAL_ES_WAVE_ON(partial_es_wave) |
433 S_028AA8_WD_SWITCH_ON_EOP(sscreen->info.chip_class >= GFX7 ? wd_switch_on_eop : 0) |
434 /* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */
435 S_028AA8_MAX_PRIMGRP_IN_WAVE(sscreen->info.chip_class == GFX8 ? max_primgroup_in_wave
436 : 0) |
437 S_030960_EN_INST_OPT_BASIC(sscreen->info.chip_class >= GFX9) |
438 S_030960_EN_INST_OPT_ADV(sscreen->info.chip_class >= GFX9);
439 }
440
441 static void si_init_ia_multi_vgt_param_table(struct si_context *sctx)
442 {
443 for (int prim = 0; prim <= SI_PRIM_RECTANGLE_LIST; prim++)
444 for (int uses_instancing = 0; uses_instancing < 2; uses_instancing++)
445 for (int multi_instances = 0; multi_instances < 2; multi_instances++)
446 for (int primitive_restart = 0; primitive_restart < 2; primitive_restart++)
447 for (int count_from_so = 0; count_from_so < 2; count_from_so++)
448 for (int line_stipple = 0; line_stipple < 2; line_stipple++)
449 for (int uses_tess = 0; uses_tess < 2; uses_tess++)
450 for (int tess_uses_primid = 0; tess_uses_primid < 2; tess_uses_primid++)
451 for (int uses_gs = 0; uses_gs < 2; uses_gs++) {
452 union si_vgt_param_key key;
453
454 key.index = 0;
455 key.u.prim = prim;
456 key.u.uses_instancing = uses_instancing;
457 key.u.multi_instances_smaller_than_primgroup = multi_instances;
458 key.u.primitive_restart = primitive_restart;
459 key.u.count_from_stream_output = count_from_so;
460 key.u.line_stipple_enabled = line_stipple;
461 key.u.uses_tess = uses_tess;
462 key.u.tess_uses_prim_id = tess_uses_primid;
463 key.u.uses_gs = uses_gs;
464
465 sctx->ia_multi_vgt_param[key.index] =
466 si_get_init_multi_vgt_param(sctx->screen, &key);
467 }
468 }
469
470 static bool si_is_line_stipple_enabled(struct si_context *sctx)
471 {
472 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
473
474 return rs->line_stipple_enable && sctx->current_rast_prim != PIPE_PRIM_POINTS &&
475 (rs->polygon_mode_is_lines || util_prim_is_lines(sctx->current_rast_prim));
476 }
477
478 static unsigned si_get_ia_multi_vgt_param(struct si_context *sctx,
479 const struct pipe_draw_info *info,
480 enum pipe_prim_type prim, unsigned num_patches,
481 unsigned instance_count, bool primitive_restart)
482 {
483 union si_vgt_param_key key = sctx->ia_multi_vgt_param_key;
484 unsigned primgroup_size;
485 unsigned ia_multi_vgt_param;
486
487 if (sctx->tes_shader.cso) {
488 primgroup_size = num_patches; /* must be a multiple of NUM_PATCHES */
489 } else if (sctx->gs_shader.cso) {
490 primgroup_size = 64; /* recommended with a GS */
491 } else {
492 primgroup_size = 128; /* recommended without a GS and tess */
493 }
494
495 key.u.prim = prim;
496 key.u.uses_instancing = info->indirect || instance_count > 1;
497 key.u.multi_instances_smaller_than_primgroup =
498 info->indirect ||
499 (instance_count > 1 &&
500 (info->count_from_stream_output || si_num_prims_for_vertices(info, prim) < primgroup_size));
501 key.u.primitive_restart = primitive_restart;
502 key.u.count_from_stream_output = info->count_from_stream_output != NULL;
503 key.u.line_stipple_enabled = si_is_line_stipple_enabled(sctx);
504
505 ia_multi_vgt_param =
506 sctx->ia_multi_vgt_param[key.index] | S_028AA8_PRIMGROUP_SIZE(primgroup_size - 1);
507
508 if (sctx->gs_shader.cso) {
509 /* GS requirement. */
510 if (sctx->chip_class <= GFX8 &&
511 SI_GS_PER_ES / primgroup_size >= sctx->screen->gs_table_depth - 3)
512 ia_multi_vgt_param |= S_028AA8_PARTIAL_ES_WAVE_ON(1);
513
514 /* GS hw bug with single-primitive instances and SWITCH_ON_EOI.
515 * The hw doc says all multi-SE chips are affected, but Vulkan
516 * only applies it to Hawaii. Do what Vulkan does.
517 */
518 if (sctx->family == CHIP_HAWAII && G_028AA8_SWITCH_ON_EOI(ia_multi_vgt_param) &&
519 (info->indirect || (instance_count > 1 && (info->count_from_stream_output ||
520 si_num_prims_for_vertices(info, prim) <= 1))))
521 sctx->flags |= SI_CONTEXT_VGT_FLUSH;
522 }
523
524 return ia_multi_vgt_param;
525 }
526
527 static unsigned si_conv_prim_to_gs_out(unsigned mode)
528 {
529 static const int prim_conv[] = {
530 [PIPE_PRIM_POINTS] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
531 [PIPE_PRIM_LINES] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
532 [PIPE_PRIM_LINE_LOOP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
533 [PIPE_PRIM_LINE_STRIP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
534 [PIPE_PRIM_TRIANGLES] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
535 [PIPE_PRIM_TRIANGLE_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
536 [PIPE_PRIM_TRIANGLE_FAN] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
537 [PIPE_PRIM_QUADS] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
538 [PIPE_PRIM_QUAD_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
539 [PIPE_PRIM_POLYGON] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
540 [PIPE_PRIM_LINES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
541 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
542 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
543 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
544 [PIPE_PRIM_PATCHES] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
545 [SI_PRIM_RECTANGLE_LIST] = V_028A6C_VGT_OUT_RECT_V0,
546 };
547 assert(mode < ARRAY_SIZE(prim_conv));
548
549 return prim_conv[mode];
550 }
551
552 /* rast_prim is the primitive type after GS. */
553 static void si_emit_rasterizer_prim_state(struct si_context *sctx)
554 {
555 struct radeon_cmdbuf *cs = sctx->gfx_cs;
556 enum pipe_prim_type rast_prim = sctx->current_rast_prim;
557 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
558 unsigned initial_cdw = cs->current.cdw;
559
560 if (unlikely(si_is_line_stipple_enabled(sctx))) {
561 /* For lines, reset the stipple pattern at each primitive. Otherwise,
562 * reset the stipple pattern at each packet (line strips, line loops).
563 */
564 unsigned value =
565 rs->pa_sc_line_stipple | S_028A0C_AUTO_RESET_CNTL(rast_prim == PIPE_PRIM_LINES ? 1 : 2);
566
567 radeon_opt_set_context_reg(sctx, R_028A0C_PA_SC_LINE_STIPPLE, SI_TRACKED_PA_SC_LINE_STIPPLE,
568 value);
569 }
570
571 unsigned gs_out_prim = si_conv_prim_to_gs_out(rast_prim);
572 if (unlikely(gs_out_prim != sctx->last_gs_out_prim && (sctx->ngg || sctx->gs_shader.cso))) {
573 radeon_set_context_reg(cs, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out_prim);
574 sctx->last_gs_out_prim = gs_out_prim;
575 }
576
577 if (initial_cdw != cs->current.cdw)
578 sctx->context_roll = true;
579
580 if (sctx->ngg) {
581 unsigned vtx_index = rs->flatshade_first ? 0 : gs_out_prim;
582
583 sctx->current_vs_state &= C_VS_STATE_OUTPRIM & C_VS_STATE_PROVOKING_VTX_INDEX;
584 sctx->current_vs_state |=
585 S_VS_STATE_OUTPRIM(gs_out_prim) | S_VS_STATE_PROVOKING_VTX_INDEX(vtx_index);
586 }
587 }
588
589 static void si_emit_vs_state(struct si_context *sctx, const struct pipe_draw_info *info)
590 {
591 sctx->current_vs_state &= C_VS_STATE_INDEXED;
592 sctx->current_vs_state |= S_VS_STATE_INDEXED(!!info->index_size);
593
594 if (sctx->num_vs_blit_sgprs) {
595 /* Re-emit the state after we leave u_blitter. */
596 sctx->last_vs_state = ~0;
597 return;
598 }
599
600 if (sctx->current_vs_state != sctx->last_vs_state) {
601 struct radeon_cmdbuf *cs = sctx->gfx_cs;
602
603 /* For the API vertex shader (VS_STATE_INDEXED, LS_OUT_*). */
604 radeon_set_sh_reg(
605 cs, sctx->shader_pointers.sh_base[PIPE_SHADER_VERTEX] + SI_SGPR_VS_STATE_BITS * 4,
606 sctx->current_vs_state);
607
608 /* Set CLAMP_VERTEX_COLOR and OUTPRIM in the last stage
609 * before the rasterizer.
610 *
611 * For TES or the GS copy shader without NGG:
612 */
613 if (sctx->shader_pointers.sh_base[PIPE_SHADER_VERTEX] != R_00B130_SPI_SHADER_USER_DATA_VS_0) {
614 radeon_set_sh_reg(cs, R_00B130_SPI_SHADER_USER_DATA_VS_0 + SI_SGPR_VS_STATE_BITS * 4,
615 sctx->current_vs_state);
616 }
617
618 /* For NGG: */
619 if (sctx->screen->use_ngg &&
620 sctx->shader_pointers.sh_base[PIPE_SHADER_VERTEX] != R_00B230_SPI_SHADER_USER_DATA_GS_0) {
621 radeon_set_sh_reg(cs, R_00B230_SPI_SHADER_USER_DATA_GS_0 + SI_SGPR_VS_STATE_BITS * 4,
622 sctx->current_vs_state);
623 }
624
625 sctx->last_vs_state = sctx->current_vs_state;
626 }
627 }
628
629 static inline bool si_prim_restart_index_changed(struct si_context *sctx, bool primitive_restart,
630 unsigned restart_index)
631 {
632 return primitive_restart && (restart_index != sctx->last_restart_index ||
633 sctx->last_restart_index == SI_RESTART_INDEX_UNKNOWN);
634 }
635
636 static void si_emit_ia_multi_vgt_param(struct si_context *sctx, const struct pipe_draw_info *info,
637 enum pipe_prim_type prim, unsigned num_patches,
638 unsigned instance_count, bool primitive_restart)
639 {
640 struct radeon_cmdbuf *cs = sctx->gfx_cs;
641 unsigned ia_multi_vgt_param;
642
643 ia_multi_vgt_param =
644 si_get_ia_multi_vgt_param(sctx, info, prim, num_patches, instance_count, primitive_restart);
645
646 /* Draw state. */
647 if (ia_multi_vgt_param != sctx->last_multi_vgt_param) {
648 if (sctx->chip_class == GFX9)
649 radeon_set_uconfig_reg_idx(cs, sctx->screen, R_030960_IA_MULTI_VGT_PARAM, 4,
650 ia_multi_vgt_param);
651 else if (sctx->chip_class >= GFX7)
652 radeon_set_context_reg_idx(cs, R_028AA8_IA_MULTI_VGT_PARAM, 1, ia_multi_vgt_param);
653 else
654 radeon_set_context_reg(cs, R_028AA8_IA_MULTI_VGT_PARAM, ia_multi_vgt_param);
655
656 sctx->last_multi_vgt_param = ia_multi_vgt_param;
657 }
658 }
659
660 /* GFX10 removed IA_MULTI_VGT_PARAM in exchange for GE_CNTL.
661 * We overload last_multi_vgt_param.
662 */
663 static void gfx10_emit_ge_cntl(struct si_context *sctx, unsigned num_patches)
664 {
665 union si_vgt_param_key key = sctx->ia_multi_vgt_param_key;
666 unsigned ge_cntl;
667
668 if (sctx->ngg) {
669 if (sctx->tes_shader.cso) {
670 ge_cntl = S_03096C_PRIM_GRP_SIZE(num_patches) |
671 S_03096C_VERT_GRP_SIZE(256) | /* 256 = disable vertex grouping */
672 S_03096C_BREAK_WAVE_AT_EOI(key.u.tess_uses_prim_id);
673 } else {
674 ge_cntl = si_get_vs_state(sctx)->ge_cntl;
675 }
676 } else {
677 unsigned primgroup_size;
678 unsigned vertgroup_size = 256; /* 256 = disable vertex grouping */
679 ;
680
681 if (sctx->tes_shader.cso) {
682 primgroup_size = num_patches; /* must be a multiple of NUM_PATCHES */
683 } else if (sctx->gs_shader.cso) {
684 unsigned vgt_gs_onchip_cntl = sctx->gs_shader.current->ctx_reg.gs.vgt_gs_onchip_cntl;
685 primgroup_size = G_028A44_GS_PRIMS_PER_SUBGRP(vgt_gs_onchip_cntl);
686 } else {
687 primgroup_size = 128; /* recommended without a GS and tess */
688 }
689
690 ge_cntl = S_03096C_PRIM_GRP_SIZE(primgroup_size) | S_03096C_VERT_GRP_SIZE(vertgroup_size) |
691 S_03096C_BREAK_WAVE_AT_EOI(key.u.uses_tess && key.u.tess_uses_prim_id);
692 }
693
694 ge_cntl |= S_03096C_PACKET_TO_ONE_PA(si_is_line_stipple_enabled(sctx));
695
696 if (ge_cntl != sctx->last_multi_vgt_param) {
697 radeon_set_uconfig_reg(sctx->gfx_cs, R_03096C_GE_CNTL, ge_cntl);
698 sctx->last_multi_vgt_param = ge_cntl;
699 }
700 }
701
702 static void si_emit_draw_registers(struct si_context *sctx, const struct pipe_draw_info *info,
703 enum pipe_prim_type prim, unsigned num_patches,
704 unsigned instance_count, bool primitive_restart)
705 {
706 struct radeon_cmdbuf *cs = sctx->gfx_cs;
707 unsigned vgt_prim = si_conv_pipe_prim(prim);
708
709 if (sctx->chip_class >= GFX10)
710 gfx10_emit_ge_cntl(sctx, num_patches);
711 else
712 si_emit_ia_multi_vgt_param(sctx, info, prim, num_patches, instance_count, primitive_restart);
713
714 if (vgt_prim != sctx->last_prim) {
715 if (sctx->chip_class >= GFX10)
716 radeon_set_uconfig_reg(cs, R_030908_VGT_PRIMITIVE_TYPE, vgt_prim);
717 else if (sctx->chip_class >= GFX7)
718 radeon_set_uconfig_reg_idx(cs, sctx->screen, R_030908_VGT_PRIMITIVE_TYPE, 1, vgt_prim);
719 else
720 radeon_set_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE, vgt_prim);
721
722 sctx->last_prim = vgt_prim;
723 }
724
725 /* Primitive restart. */
726 if (primitive_restart != sctx->last_primitive_restart_en) {
727 if (sctx->chip_class >= GFX9)
728 radeon_set_uconfig_reg(cs, R_03092C_VGT_MULTI_PRIM_IB_RESET_EN, primitive_restart);
729 else
730 radeon_set_context_reg(cs, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, primitive_restart);
731
732 sctx->last_primitive_restart_en = primitive_restart;
733 }
734 if (si_prim_restart_index_changed(sctx, primitive_restart, info->restart_index)) {
735 radeon_set_context_reg(cs, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX, info->restart_index);
736 sctx->last_restart_index = info->restart_index;
737 sctx->context_roll = true;
738 }
739 }
740
741 static void si_emit_draw_packets(struct si_context *sctx, const struct pipe_draw_info *info,
742 struct pipe_resource *indexbuf, unsigned index_size,
743 unsigned index_offset, unsigned instance_count,
744 bool dispatch_prim_discard_cs, unsigned original_index_size)
745 {
746 struct pipe_draw_indirect_info *indirect = info->indirect;
747 struct radeon_cmdbuf *cs = sctx->gfx_cs;
748 unsigned sh_base_reg = sctx->shader_pointers.sh_base[PIPE_SHADER_VERTEX];
749 bool render_cond_bit = sctx->render_cond && !sctx->render_cond_force_off;
750 uint32_t index_max_size = 0;
751 uint64_t index_va = 0;
752
753 if (info->count_from_stream_output) {
754 struct si_streamout_target *t = (struct si_streamout_target *)info->count_from_stream_output;
755
756 radeon_set_context_reg(cs, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE, t->stride_in_dw);
757 si_cp_copy_data(sctx, sctx->gfx_cs, COPY_DATA_REG, NULL,
758 R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE >> 2, COPY_DATA_SRC_MEM,
759 t->buf_filled_size, t->buf_filled_size_offset);
760 }
761
762 /* draw packet */
763 if (index_size) {
764 if (index_size != sctx->last_index_size) {
765 unsigned index_type;
766
767 /* index type */
768 switch (index_size) {
769 case 1:
770 index_type = V_028A7C_VGT_INDEX_8;
771 break;
772 case 2:
773 index_type =
774 V_028A7C_VGT_INDEX_16 |
775 (SI_BIG_ENDIAN && sctx->chip_class <= GFX7 ? V_028A7C_VGT_DMA_SWAP_16_BIT : 0);
776 break;
777 case 4:
778 index_type =
779 V_028A7C_VGT_INDEX_32 |
780 (SI_BIG_ENDIAN && sctx->chip_class <= GFX7 ? V_028A7C_VGT_DMA_SWAP_32_BIT : 0);
781 break;
782 default:
783 assert(!"unreachable");
784 return;
785 }
786
787 if (sctx->chip_class >= GFX9) {
788 radeon_set_uconfig_reg_idx(cs, sctx->screen, R_03090C_VGT_INDEX_TYPE, 2, index_type);
789 } else {
790 radeon_emit(cs, PKT3(PKT3_INDEX_TYPE, 0, 0));
791 radeon_emit(cs, index_type);
792 }
793
794 sctx->last_index_size = index_size;
795 }
796
797 if (original_index_size) {
798 index_max_size = (indexbuf->width0 - index_offset) / original_index_size;
799 /* Skip draw calls with 0-sized index buffers.
800 * They cause a hang on some chips, like Navi10-14.
801 */
802 if (!index_max_size)
803 return;
804
805 index_va = si_resource(indexbuf)->gpu_address + index_offset;
806
807 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, si_resource(indexbuf), RADEON_USAGE_READ,
808 RADEON_PRIO_INDEX_BUFFER);
809 }
810 } else {
811 /* On GFX7 and later, non-indexed draws overwrite VGT_INDEX_TYPE,
812 * so the state must be re-emitted before the next indexed draw.
813 */
814 if (sctx->chip_class >= GFX7)
815 sctx->last_index_size = -1;
816 }
817
818 if (indirect) {
819 uint64_t indirect_va = si_resource(indirect->buffer)->gpu_address;
820
821 assert(indirect_va % 8 == 0);
822
823 si_invalidate_draw_sh_constants(sctx);
824
825 radeon_emit(cs, PKT3(PKT3_SET_BASE, 2, 0));
826 radeon_emit(cs, 1);
827 radeon_emit(cs, indirect_va);
828 radeon_emit(cs, indirect_va >> 32);
829
830 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, si_resource(indirect->buffer),
831 RADEON_USAGE_READ, RADEON_PRIO_DRAW_INDIRECT);
832
833 unsigned di_src_sel = index_size ? V_0287F0_DI_SRC_SEL_DMA : V_0287F0_DI_SRC_SEL_AUTO_INDEX;
834
835 assert(indirect->offset % 4 == 0);
836
837 if (index_size) {
838 radeon_emit(cs, PKT3(PKT3_INDEX_BASE, 1, 0));
839 radeon_emit(cs, index_va);
840 radeon_emit(cs, index_va >> 32);
841
842 radeon_emit(cs, PKT3(PKT3_INDEX_BUFFER_SIZE, 0, 0));
843 radeon_emit(cs, index_max_size);
844 }
845
846 if (!sctx->screen->has_draw_indirect_multi) {
847 radeon_emit(cs, PKT3(index_size ? PKT3_DRAW_INDEX_INDIRECT : PKT3_DRAW_INDIRECT, 3,
848 render_cond_bit));
849 radeon_emit(cs, indirect->offset);
850 radeon_emit(cs, (sh_base_reg + SI_SGPR_BASE_VERTEX * 4 - SI_SH_REG_OFFSET) >> 2);
851 radeon_emit(cs, (sh_base_reg + SI_SGPR_START_INSTANCE * 4 - SI_SH_REG_OFFSET) >> 2);
852 radeon_emit(cs, di_src_sel);
853 } else {
854 uint64_t count_va = 0;
855
856 if (indirect->indirect_draw_count) {
857 struct si_resource *params_buf = si_resource(indirect->indirect_draw_count);
858
859 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, params_buf, RADEON_USAGE_READ,
860 RADEON_PRIO_DRAW_INDIRECT);
861
862 count_va = params_buf->gpu_address + indirect->indirect_draw_count_offset;
863 }
864
865 radeon_emit(cs,
866 PKT3(index_size ? PKT3_DRAW_INDEX_INDIRECT_MULTI : PKT3_DRAW_INDIRECT_MULTI, 8,
867 render_cond_bit));
868 radeon_emit(cs, indirect->offset);
869 radeon_emit(cs, (sh_base_reg + SI_SGPR_BASE_VERTEX * 4 - SI_SH_REG_OFFSET) >> 2);
870 radeon_emit(cs, (sh_base_reg + SI_SGPR_START_INSTANCE * 4 - SI_SH_REG_OFFSET) >> 2);
871 radeon_emit(cs, ((sh_base_reg + SI_SGPR_DRAWID * 4 - SI_SH_REG_OFFSET) >> 2) |
872 S_2C3_DRAW_INDEX_ENABLE(1) |
873 S_2C3_COUNT_INDIRECT_ENABLE(!!indirect->indirect_draw_count));
874 radeon_emit(cs, indirect->draw_count);
875 radeon_emit(cs, count_va);
876 radeon_emit(cs, count_va >> 32);
877 radeon_emit(cs, indirect->stride);
878 radeon_emit(cs, di_src_sel);
879 }
880 } else {
881 int base_vertex;
882
883 if (sctx->last_instance_count == SI_INSTANCE_COUNT_UNKNOWN ||
884 sctx->last_instance_count != instance_count) {
885 radeon_emit(cs, PKT3(PKT3_NUM_INSTANCES, 0, 0));
886 radeon_emit(cs, instance_count);
887 sctx->last_instance_count = instance_count;
888 }
889
890 /* Base vertex and start instance. */
891 base_vertex = original_index_size ? info->index_bias : info->start;
892
893 if (sctx->num_vs_blit_sgprs) {
894 /* Re-emit draw constants after we leave u_blitter. */
895 si_invalidate_draw_sh_constants(sctx);
896
897 /* Blit VS doesn't use BASE_VERTEX, START_INSTANCE, and DRAWID. */
898 radeon_set_sh_reg_seq(cs, sh_base_reg + SI_SGPR_VS_BLIT_DATA * 4, sctx->num_vs_blit_sgprs);
899 radeon_emit_array(cs, sctx->vs_blit_sh_data, sctx->num_vs_blit_sgprs);
900 } else if (base_vertex != sctx->last_base_vertex ||
901 sctx->last_base_vertex == SI_BASE_VERTEX_UNKNOWN ||
902 info->start_instance != sctx->last_start_instance ||
903 info->drawid != sctx->last_drawid || sh_base_reg != sctx->last_sh_base_reg) {
904 radeon_set_sh_reg_seq(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, 3);
905 radeon_emit(cs, base_vertex);
906 radeon_emit(cs, info->start_instance);
907 radeon_emit(cs, info->drawid);
908
909 sctx->last_base_vertex = base_vertex;
910 sctx->last_start_instance = info->start_instance;
911 sctx->last_drawid = info->drawid;
912 sctx->last_sh_base_reg = sh_base_reg;
913 }
914
915 if (index_size) {
916 if (dispatch_prim_discard_cs) {
917 index_va += info->start * original_index_size;
918 index_max_size = MIN2(index_max_size, info->count);
919
920 si_dispatch_prim_discard_cs_and_draw(sctx, info, original_index_size, base_vertex,
921 index_va, index_max_size);
922 return;
923 }
924
925 index_va += info->start * index_size;
926
927 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_2, 4, render_cond_bit));
928 radeon_emit(cs, index_max_size);
929 radeon_emit(cs, index_va);
930 radeon_emit(cs, index_va >> 32);
931 radeon_emit(cs, info->count);
932 radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA);
933 } else {
934 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_AUTO, 1, render_cond_bit));
935 radeon_emit(cs, info->count);
936 radeon_emit(cs, V_0287F0_DI_SRC_SEL_AUTO_INDEX |
937 S_0287F0_USE_OPAQUE(!!info->count_from_stream_output));
938 }
939 }
940 }
941
942 void si_emit_surface_sync(struct si_context *sctx, struct radeon_cmdbuf *cs, unsigned cp_coher_cntl)
943 {
944 bool compute_ib = !sctx->has_graphics || cs == sctx->prim_discard_compute_cs;
945
946 assert(sctx->chip_class <= GFX9);
947
948 if (sctx->chip_class == GFX9 || compute_ib) {
949 /* Flush caches and wait for the caches to assert idle. */
950 radeon_emit(cs, PKT3(PKT3_ACQUIRE_MEM, 5, 0));
951 radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
952 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
953 radeon_emit(cs, 0xffffff); /* CP_COHER_SIZE_HI */
954 radeon_emit(cs, 0); /* CP_COHER_BASE */
955 radeon_emit(cs, 0); /* CP_COHER_BASE_HI */
956 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
957 } else {
958 /* ACQUIRE_MEM is only required on a compute ring. */
959 radeon_emit(cs, PKT3(PKT3_SURFACE_SYNC, 3, 0));
960 radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
961 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
962 radeon_emit(cs, 0); /* CP_COHER_BASE */
963 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
964 }
965
966 /* ACQUIRE_MEM has an implicit context roll if the current context
967 * is busy. */
968 if (!compute_ib)
969 sctx->context_roll = true;
970 }
971
972 void si_prim_discard_signal_next_compute_ib_start(struct si_context *sctx)
973 {
974 if (!si_compute_prim_discard_enabled(sctx))
975 return;
976
977 if (!sctx->barrier_buf) {
978 u_suballocator_alloc(sctx->allocator_zeroed_memory, 4, 4, &sctx->barrier_buf_offset,
979 (struct pipe_resource **)&sctx->barrier_buf);
980 }
981
982 /* Emit a placeholder to signal the next compute IB to start.
983 * See si_compute_prim_discard.c for explanation.
984 */
985 uint32_t signal = 1;
986 si_cp_write_data(sctx, sctx->barrier_buf, sctx->barrier_buf_offset, 4, V_370_MEM, V_370_ME,
987 &signal);
988
989 sctx->last_pkt3_write_data = &sctx->gfx_cs->current.buf[sctx->gfx_cs->current.cdw - 5];
990
991 /* Only the last occurence of WRITE_DATA will be executed.
992 * The packet will be enabled in si_flush_gfx_cs.
993 */
994 *sctx->last_pkt3_write_data = PKT3(PKT3_NOP, 3, 0);
995 }
996
997 void gfx10_emit_cache_flush(struct si_context *ctx)
998 {
999 struct radeon_cmdbuf *cs = ctx->gfx_cs;
1000 uint32_t gcr_cntl = 0;
1001 unsigned cb_db_event = 0;
1002 unsigned flags = ctx->flags;
1003
1004 if (!ctx->has_graphics) {
1005 /* Only process compute flags. */
1006 flags &= SI_CONTEXT_INV_ICACHE | SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE |
1007 SI_CONTEXT_INV_L2 | SI_CONTEXT_WB_L2 | SI_CONTEXT_INV_L2_METADATA |
1008 SI_CONTEXT_CS_PARTIAL_FLUSH;
1009 }
1010
1011 /* We don't need these. */
1012 assert(!(flags & (SI_CONTEXT_VGT_STREAMOUT_SYNC | SI_CONTEXT_FLUSH_AND_INV_DB_META)));
1013
1014 if (flags & SI_CONTEXT_VGT_FLUSH) {
1015 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1016 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
1017 }
1018
1019 if (flags & SI_CONTEXT_FLUSH_AND_INV_CB)
1020 ctx->num_cb_cache_flushes++;
1021 if (flags & SI_CONTEXT_FLUSH_AND_INV_DB)
1022 ctx->num_db_cache_flushes++;
1023
1024 if (flags & SI_CONTEXT_INV_ICACHE)
1025 gcr_cntl |= S_586_GLI_INV(V_586_GLI_ALL);
1026 if (flags & SI_CONTEXT_INV_SCACHE) {
1027 /* TODO: When writing to the SMEM L1 cache, we need to set SEQ
1028 * to FORWARD when both L1 and L2 are written out (WB or INV).
1029 */
1030 gcr_cntl |= S_586_GL1_INV(1) | S_586_GLK_INV(1);
1031 }
1032 if (flags & SI_CONTEXT_INV_VCACHE)
1033 gcr_cntl |= S_586_GL1_INV(1) | S_586_GLV_INV(1);
1034
1035 /* The L2 cache ops are:
1036 * - INV: - invalidate lines that reflect memory (were loaded from memory)
1037 * - don't touch lines that were overwritten (were stored by gfx clients)
1038 * - WB: - don't touch lines that reflect memory
1039 * - write back lines that were overwritten
1040 * - WB | INV: - invalidate lines that reflect memory
1041 * - write back lines that were overwritten
1042 *
1043 * GLM doesn't support WB alone. If WB is set, INV must be set too.
1044 */
1045 if (flags & SI_CONTEXT_INV_L2) {
1046 /* Writeback and invalidate everything in L2. */
1047 gcr_cntl |= S_586_GL2_INV(1) | S_586_GL2_WB(1) | S_586_GLM_INV(1) | S_586_GLM_WB(1);
1048 ctx->num_L2_invalidates++;
1049 } else if (flags & SI_CONTEXT_WB_L2) {
1050 gcr_cntl |= S_586_GL2_WB(1) | S_586_GLM_WB(1) | S_586_GLM_INV(1);
1051 } else if (flags & SI_CONTEXT_INV_L2_METADATA) {
1052 gcr_cntl |= S_586_GLM_INV(1) | S_586_GLM_WB(1);
1053 }
1054
1055 if (flags & (SI_CONTEXT_FLUSH_AND_INV_CB | SI_CONTEXT_FLUSH_AND_INV_DB)) {
1056 if (flags & SI_CONTEXT_FLUSH_AND_INV_CB) {
1057 /* Flush CMASK/FMASK/DCC. Will wait for idle later. */
1058 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1059 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_CB_META) | EVENT_INDEX(0));
1060 }
1061 if (flags & SI_CONTEXT_FLUSH_AND_INV_DB) {
1062 /* Flush HTILE. Will wait for idle later. */
1063 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1064 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_DB_META) | EVENT_INDEX(0));
1065 }
1066
1067 /* First flush CB/DB, then L1/L2. */
1068 gcr_cntl |= S_586_SEQ(V_586_SEQ_FORWARD);
1069
1070 if ((flags & (SI_CONTEXT_FLUSH_AND_INV_CB | SI_CONTEXT_FLUSH_AND_INV_DB)) ==
1071 (SI_CONTEXT_FLUSH_AND_INV_CB | SI_CONTEXT_FLUSH_AND_INV_DB)) {
1072 cb_db_event = V_028A90_CACHE_FLUSH_AND_INV_TS_EVENT;
1073 } else if (flags & SI_CONTEXT_FLUSH_AND_INV_CB) {
1074 cb_db_event = V_028A90_FLUSH_AND_INV_CB_DATA_TS;
1075 } else if (flags & SI_CONTEXT_FLUSH_AND_INV_DB) {
1076 cb_db_event = V_028A90_FLUSH_AND_INV_DB_DATA_TS;
1077 } else {
1078 assert(0);
1079 }
1080 } else {
1081 /* Wait for graphics shaders to go idle if requested. */
1082 if (flags & SI_CONTEXT_PS_PARTIAL_FLUSH) {
1083 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1084 radeon_emit(cs, EVENT_TYPE(V_028A90_PS_PARTIAL_FLUSH) | EVENT_INDEX(4));
1085 /* Only count explicit shader flushes, not implicit ones. */
1086 ctx->num_vs_flushes++;
1087 ctx->num_ps_flushes++;
1088 } else if (flags & SI_CONTEXT_VS_PARTIAL_FLUSH) {
1089 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1090 radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
1091 ctx->num_vs_flushes++;
1092 }
1093 }
1094
1095 if (flags & SI_CONTEXT_CS_PARTIAL_FLUSH && ctx->compute_is_busy) {
1096 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1097 radeon_emit(cs, EVENT_TYPE(V_028A90_CS_PARTIAL_FLUSH | EVENT_INDEX(4)));
1098 ctx->num_cs_flushes++;
1099 ctx->compute_is_busy = false;
1100 }
1101
1102 if (cb_db_event) {
1103 /* CB/DB flush and invalidate (or possibly just a wait for a
1104 * meta flush) via RELEASE_MEM.
1105 *
1106 * Combine this with other cache flushes when possible; this
1107 * requires affected shaders to be idle, so do it after the
1108 * CS_PARTIAL_FLUSH before (VS/PS partial flushes are always
1109 * implied).
1110 */
1111 uint64_t va;
1112
1113 /* Do the flush (enqueue the event and wait for it). */
1114 va = ctx->wait_mem_scratch->gpu_address;
1115 ctx->wait_mem_number++;
1116
1117 /* Get GCR_CNTL fields, because the encoding is different in RELEASE_MEM. */
1118 unsigned glm_wb = G_586_GLM_WB(gcr_cntl);
1119 unsigned glm_inv = G_586_GLM_INV(gcr_cntl);
1120 unsigned glv_inv = G_586_GLV_INV(gcr_cntl);
1121 unsigned gl1_inv = G_586_GL1_INV(gcr_cntl);
1122 assert(G_586_GL2_US(gcr_cntl) == 0);
1123 assert(G_586_GL2_RANGE(gcr_cntl) == 0);
1124 assert(G_586_GL2_DISCARD(gcr_cntl) == 0);
1125 unsigned gl2_inv = G_586_GL2_INV(gcr_cntl);
1126 unsigned gl2_wb = G_586_GL2_WB(gcr_cntl);
1127 unsigned gcr_seq = G_586_SEQ(gcr_cntl);
1128
1129 gcr_cntl &= C_586_GLM_WB & C_586_GLM_INV & C_586_GLV_INV & C_586_GL1_INV & C_586_GL2_INV &
1130 C_586_GL2_WB; /* keep SEQ */
1131
1132 si_cp_release_mem(ctx, cs, cb_db_event,
1133 S_490_GLM_WB(glm_wb) | S_490_GLM_INV(glm_inv) | S_490_GLV_INV(glv_inv) |
1134 S_490_GL1_INV(gl1_inv) | S_490_GL2_INV(gl2_inv) | S_490_GL2_WB(gl2_wb) |
1135 S_490_SEQ(gcr_seq),
1136 EOP_DST_SEL_MEM, EOP_INT_SEL_SEND_DATA_AFTER_WR_CONFIRM,
1137 EOP_DATA_SEL_VALUE_32BIT, ctx->wait_mem_scratch, va, ctx->wait_mem_number,
1138 SI_NOT_QUERY);
1139 si_cp_wait_mem(ctx, ctx->gfx_cs, va, ctx->wait_mem_number, 0xffffffff, WAIT_REG_MEM_EQUAL);
1140 }
1141
1142 /* Ignore fields that only modify the behavior of other fields. */
1143 if (gcr_cntl & C_586_GL1_RANGE & C_586_GL2_RANGE & C_586_SEQ) {
1144 /* Flush caches and wait for the caches to assert idle.
1145 * The cache flush is executed in the ME, but the PFP waits
1146 * for completion.
1147 */
1148 radeon_emit(cs, PKT3(PKT3_ACQUIRE_MEM, 6, 0));
1149 radeon_emit(cs, 0); /* CP_COHER_CNTL */
1150 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
1151 radeon_emit(cs, 0xffffff); /* CP_COHER_SIZE_HI */
1152 radeon_emit(cs, 0); /* CP_COHER_BASE */
1153 radeon_emit(cs, 0); /* CP_COHER_BASE_HI */
1154 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
1155 radeon_emit(cs, gcr_cntl); /* GCR_CNTL */
1156 } else if (cb_db_event || (flags & (SI_CONTEXT_VS_PARTIAL_FLUSH | SI_CONTEXT_PS_PARTIAL_FLUSH |
1157 SI_CONTEXT_CS_PARTIAL_FLUSH))) {
1158 /* We need to ensure that PFP waits as well. */
1159 radeon_emit(cs, PKT3(PKT3_PFP_SYNC_ME, 0, 0));
1160 radeon_emit(cs, 0);
1161 }
1162
1163 if (flags & SI_CONTEXT_START_PIPELINE_STATS) {
1164 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1165 radeon_emit(cs, EVENT_TYPE(V_028A90_PIPELINESTAT_START) | EVENT_INDEX(0));
1166 } else if (flags & SI_CONTEXT_STOP_PIPELINE_STATS) {
1167 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1168 radeon_emit(cs, EVENT_TYPE(V_028A90_PIPELINESTAT_STOP) | EVENT_INDEX(0));
1169 }
1170
1171 ctx->flags = 0;
1172 }
1173
1174 void si_emit_cache_flush(struct si_context *sctx)
1175 {
1176 struct radeon_cmdbuf *cs = sctx->gfx_cs;
1177 uint32_t flags = sctx->flags;
1178
1179 if (!sctx->has_graphics) {
1180 /* Only process compute flags. */
1181 flags &= SI_CONTEXT_INV_ICACHE | SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE |
1182 SI_CONTEXT_INV_L2 | SI_CONTEXT_WB_L2 | SI_CONTEXT_INV_L2_METADATA |
1183 SI_CONTEXT_CS_PARTIAL_FLUSH;
1184 }
1185
1186 uint32_t cp_coher_cntl = 0;
1187 const uint32_t flush_cb_db = flags & (SI_CONTEXT_FLUSH_AND_INV_CB | SI_CONTEXT_FLUSH_AND_INV_DB);
1188 const bool is_barrier =
1189 flush_cb_db ||
1190 /* INV_ICACHE == beginning of gfx IB. Checking
1191 * INV_ICACHE fixes corruption for DeusExMD with
1192 * compute-based culling, but I don't know why.
1193 */
1194 flags & (SI_CONTEXT_INV_ICACHE | SI_CONTEXT_PS_PARTIAL_FLUSH | SI_CONTEXT_VS_PARTIAL_FLUSH) ||
1195 (flags & SI_CONTEXT_CS_PARTIAL_FLUSH && sctx->compute_is_busy);
1196
1197 assert(sctx->chip_class <= GFX9);
1198
1199 if (flags & SI_CONTEXT_FLUSH_AND_INV_CB)
1200 sctx->num_cb_cache_flushes++;
1201 if (flags & SI_CONTEXT_FLUSH_AND_INV_DB)
1202 sctx->num_db_cache_flushes++;
1203
1204 /* GFX6 has a bug that it always flushes ICACHE and KCACHE if either
1205 * bit is set. An alternative way is to write SQC_CACHES, but that
1206 * doesn't seem to work reliably. Since the bug doesn't affect
1207 * correctness (it only does more work than necessary) and
1208 * the performance impact is likely negligible, there is no plan
1209 * to add a workaround for it.
1210 */
1211
1212 if (flags & SI_CONTEXT_INV_ICACHE)
1213 cp_coher_cntl |= S_0085F0_SH_ICACHE_ACTION_ENA(1);
1214 if (flags & SI_CONTEXT_INV_SCACHE)
1215 cp_coher_cntl |= S_0085F0_SH_KCACHE_ACTION_ENA(1);
1216
1217 if (sctx->chip_class <= GFX8) {
1218 if (flags & SI_CONTEXT_FLUSH_AND_INV_CB) {
1219 cp_coher_cntl |= S_0085F0_CB_ACTION_ENA(1) | S_0085F0_CB0_DEST_BASE_ENA(1) |
1220 S_0085F0_CB1_DEST_BASE_ENA(1) | S_0085F0_CB2_DEST_BASE_ENA(1) |
1221 S_0085F0_CB3_DEST_BASE_ENA(1) | S_0085F0_CB4_DEST_BASE_ENA(1) |
1222 S_0085F0_CB5_DEST_BASE_ENA(1) | S_0085F0_CB6_DEST_BASE_ENA(1) |
1223 S_0085F0_CB7_DEST_BASE_ENA(1);
1224
1225 /* Necessary for DCC */
1226 if (sctx->chip_class == GFX8)
1227 si_cp_release_mem(sctx, cs, V_028A90_FLUSH_AND_INV_CB_DATA_TS, 0, EOP_DST_SEL_MEM,
1228 EOP_INT_SEL_NONE, EOP_DATA_SEL_DISCARD, NULL, 0, 0, SI_NOT_QUERY);
1229 }
1230 if (flags & SI_CONTEXT_FLUSH_AND_INV_DB)
1231 cp_coher_cntl |= S_0085F0_DB_ACTION_ENA(1) | S_0085F0_DB_DEST_BASE_ENA(1);
1232 }
1233
1234 if (flags & SI_CONTEXT_FLUSH_AND_INV_CB) {
1235 /* Flush CMASK/FMASK/DCC. SURFACE_SYNC will wait for idle. */
1236 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1237 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_CB_META) | EVENT_INDEX(0));
1238 }
1239 if (flags & (SI_CONTEXT_FLUSH_AND_INV_DB | SI_CONTEXT_FLUSH_AND_INV_DB_META)) {
1240 /* Flush HTILE. SURFACE_SYNC will wait for idle. */
1241 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1242 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_DB_META) | EVENT_INDEX(0));
1243 }
1244
1245 /* Wait for shader engines to go idle.
1246 * VS and PS waits are unnecessary if SURFACE_SYNC is going to wait
1247 * for everything including CB/DB cache flushes.
1248 */
1249 if (!flush_cb_db) {
1250 if (flags & SI_CONTEXT_PS_PARTIAL_FLUSH) {
1251 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1252 radeon_emit(cs, EVENT_TYPE(V_028A90_PS_PARTIAL_FLUSH) | EVENT_INDEX(4));
1253 /* Only count explicit shader flushes, not implicit ones
1254 * done by SURFACE_SYNC.
1255 */
1256 sctx->num_vs_flushes++;
1257 sctx->num_ps_flushes++;
1258 } else if (flags & SI_CONTEXT_VS_PARTIAL_FLUSH) {
1259 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1260 radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
1261 sctx->num_vs_flushes++;
1262 }
1263 }
1264
1265 if (flags & SI_CONTEXT_CS_PARTIAL_FLUSH && sctx->compute_is_busy) {
1266 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1267 radeon_emit(cs, EVENT_TYPE(V_028A90_CS_PARTIAL_FLUSH) | EVENT_INDEX(4));
1268 sctx->num_cs_flushes++;
1269 sctx->compute_is_busy = false;
1270 }
1271
1272 /* VGT state synchronization. */
1273 if (flags & SI_CONTEXT_VGT_FLUSH) {
1274 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1275 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
1276 }
1277 if (flags & SI_CONTEXT_VGT_STREAMOUT_SYNC) {
1278 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1279 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_STREAMOUT_SYNC) | EVENT_INDEX(0));
1280 }
1281
1282 /* GFX9: Wait for idle if we're flushing CB or DB. ACQUIRE_MEM doesn't
1283 * wait for idle on GFX9. We have to use a TS event.
1284 */
1285 if (sctx->chip_class == GFX9 && flush_cb_db) {
1286 uint64_t va;
1287 unsigned tc_flags, cb_db_event;
1288
1289 /* Set the CB/DB flush event. */
1290 switch (flush_cb_db) {
1291 case SI_CONTEXT_FLUSH_AND_INV_CB:
1292 cb_db_event = V_028A90_FLUSH_AND_INV_CB_DATA_TS;
1293 break;
1294 case SI_CONTEXT_FLUSH_AND_INV_DB:
1295 cb_db_event = V_028A90_FLUSH_AND_INV_DB_DATA_TS;
1296 break;
1297 default:
1298 /* both CB & DB */
1299 cb_db_event = V_028A90_CACHE_FLUSH_AND_INV_TS_EVENT;
1300 }
1301
1302 /* These are the only allowed combinations. If you need to
1303 * do multiple operations at once, do them separately.
1304 * All operations that invalidate L2 also seem to invalidate
1305 * metadata. Volatile (VOL) and WC flushes are not listed here.
1306 *
1307 * TC | TC_WB = writeback & invalidate L2 & L1
1308 * TC | TC_WB | TC_NC = writeback & invalidate L2 for MTYPE == NC
1309 * TC_WB | TC_NC = writeback L2 for MTYPE == NC
1310 * TC | TC_NC = invalidate L2 for MTYPE == NC
1311 * TC | TC_MD = writeback & invalidate L2 metadata (DCC, etc.)
1312 * TCL1 = invalidate L1
1313 */
1314 tc_flags = 0;
1315
1316 if (flags & SI_CONTEXT_INV_L2_METADATA) {
1317 tc_flags = EVENT_TC_ACTION_ENA | EVENT_TC_MD_ACTION_ENA;
1318 }
1319
1320 /* Ideally flush TC together with CB/DB. */
1321 if (flags & SI_CONTEXT_INV_L2) {
1322 /* Writeback and invalidate everything in L2 & L1. */
1323 tc_flags = EVENT_TC_ACTION_ENA | EVENT_TC_WB_ACTION_ENA;
1324
1325 /* Clear the flags. */
1326 flags &= ~(SI_CONTEXT_INV_L2 | SI_CONTEXT_WB_L2 | SI_CONTEXT_INV_VCACHE);
1327 sctx->num_L2_invalidates++;
1328 }
1329
1330 /* Do the flush (enqueue the event and wait for it). */
1331 va = sctx->wait_mem_scratch->gpu_address;
1332 sctx->wait_mem_number++;
1333
1334 si_cp_release_mem(sctx, cs, cb_db_event, tc_flags, EOP_DST_SEL_MEM,
1335 EOP_INT_SEL_SEND_DATA_AFTER_WR_CONFIRM, EOP_DATA_SEL_VALUE_32BIT,
1336 sctx->wait_mem_scratch, va, sctx->wait_mem_number, SI_NOT_QUERY);
1337 si_cp_wait_mem(sctx, cs, va, sctx->wait_mem_number, 0xffffffff, WAIT_REG_MEM_EQUAL);
1338 }
1339
1340 /* Make sure ME is idle (it executes most packets) before continuing.
1341 * This prevents read-after-write hazards between PFP and ME.
1342 */
1343 if (sctx->has_graphics &&
1344 (cp_coher_cntl || (flags & (SI_CONTEXT_CS_PARTIAL_FLUSH | SI_CONTEXT_INV_VCACHE |
1345 SI_CONTEXT_INV_L2 | SI_CONTEXT_WB_L2)))) {
1346 radeon_emit(cs, PKT3(PKT3_PFP_SYNC_ME, 0, 0));
1347 radeon_emit(cs, 0);
1348 }
1349
1350 /* GFX6-GFX8 only:
1351 * When one of the CP_COHER_CNTL.DEST_BASE flags is set, SURFACE_SYNC
1352 * waits for idle, so it should be last. SURFACE_SYNC is done in PFP.
1353 *
1354 * cp_coher_cntl should contain all necessary flags except TC flags
1355 * at this point.
1356 *
1357 * GFX6-GFX7 don't support L2 write-back.
1358 */
1359 if (flags & SI_CONTEXT_INV_L2 || (sctx->chip_class <= GFX7 && (flags & SI_CONTEXT_WB_L2))) {
1360 /* Invalidate L1 & L2. (L1 is always invalidated on GFX6)
1361 * WB must be set on GFX8+ when TC_ACTION is set.
1362 */
1363 si_emit_surface_sync(sctx, sctx->gfx_cs,
1364 cp_coher_cntl | S_0085F0_TC_ACTION_ENA(1) | S_0085F0_TCL1_ACTION_ENA(1) |
1365 S_0301F0_TC_WB_ACTION_ENA(sctx->chip_class >= GFX8));
1366 cp_coher_cntl = 0;
1367 sctx->num_L2_invalidates++;
1368 } else {
1369 /* L1 invalidation and L2 writeback must be done separately,
1370 * because both operations can't be done together.
1371 */
1372 if (flags & SI_CONTEXT_WB_L2) {
1373 /* WB = write-back
1374 * NC = apply to non-coherent MTYPEs
1375 * (i.e. MTYPE <= 1, which is what we use everywhere)
1376 *
1377 * WB doesn't work without NC.
1378 */
1379 si_emit_surface_sync(
1380 sctx, sctx->gfx_cs,
1381 cp_coher_cntl | S_0301F0_TC_WB_ACTION_ENA(1) | S_0301F0_TC_NC_ACTION_ENA(1));
1382 cp_coher_cntl = 0;
1383 sctx->num_L2_writebacks++;
1384 }
1385 if (flags & SI_CONTEXT_INV_VCACHE) {
1386 /* Invalidate per-CU VMEM L1. */
1387 si_emit_surface_sync(sctx, sctx->gfx_cs, cp_coher_cntl | S_0085F0_TCL1_ACTION_ENA(1));
1388 cp_coher_cntl = 0;
1389 }
1390 }
1391
1392 /* If TC flushes haven't cleared this... */
1393 if (cp_coher_cntl)
1394 si_emit_surface_sync(sctx, sctx->gfx_cs, cp_coher_cntl);
1395
1396 if (is_barrier)
1397 si_prim_discard_signal_next_compute_ib_start(sctx);
1398
1399 if (flags & SI_CONTEXT_START_PIPELINE_STATS) {
1400 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1401 radeon_emit(cs, EVENT_TYPE(V_028A90_PIPELINESTAT_START) | EVENT_INDEX(0));
1402 } else if (flags & SI_CONTEXT_STOP_PIPELINE_STATS) {
1403 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1404 radeon_emit(cs, EVENT_TYPE(V_028A90_PIPELINESTAT_STOP) | EVENT_INDEX(0));
1405 }
1406
1407 sctx->flags = 0;
1408 }
1409
1410 static void si_get_draw_start_count(struct si_context *sctx, const struct pipe_draw_info *info,
1411 unsigned *start, unsigned *count)
1412 {
1413 struct pipe_draw_indirect_info *indirect = info->indirect;
1414
1415 if (indirect) {
1416 unsigned indirect_count;
1417 struct pipe_transfer *transfer;
1418 unsigned begin, end;
1419 unsigned map_size;
1420 unsigned *data;
1421
1422 if (indirect->indirect_draw_count) {
1423 data = pipe_buffer_map_range(&sctx->b, indirect->indirect_draw_count,
1424 indirect->indirect_draw_count_offset, sizeof(unsigned),
1425 PIPE_TRANSFER_READ, &transfer);
1426
1427 indirect_count = *data;
1428
1429 pipe_buffer_unmap(&sctx->b, transfer);
1430 } else {
1431 indirect_count = indirect->draw_count;
1432 }
1433
1434 if (!indirect_count) {
1435 *start = *count = 0;
1436 return;
1437 }
1438
1439 map_size = (indirect_count - 1) * indirect->stride + 3 * sizeof(unsigned);
1440 data = pipe_buffer_map_range(&sctx->b, indirect->buffer, indirect->offset, map_size,
1441 PIPE_TRANSFER_READ, &transfer);
1442
1443 begin = UINT_MAX;
1444 end = 0;
1445
1446 for (unsigned i = 0; i < indirect_count; ++i) {
1447 unsigned count = data[0];
1448 unsigned start = data[2];
1449
1450 if (count > 0) {
1451 begin = MIN2(begin, start);
1452 end = MAX2(end, start + count);
1453 }
1454
1455 data += indirect->stride / sizeof(unsigned);
1456 }
1457
1458 pipe_buffer_unmap(&sctx->b, transfer);
1459
1460 if (begin < end) {
1461 *start = begin;
1462 *count = end - begin;
1463 } else {
1464 *start = *count = 0;
1465 }
1466 } else {
1467 *start = info->start;
1468 *count = info->count;
1469 }
1470 }
1471
1472 static void si_emit_all_states(struct si_context *sctx, const struct pipe_draw_info *info,
1473 enum pipe_prim_type prim, unsigned instance_count,
1474 bool primitive_restart, unsigned skip_atom_mask)
1475 {
1476 unsigned num_patches = 0;
1477
1478 si_emit_rasterizer_prim_state(sctx);
1479 if (sctx->tes_shader.cso)
1480 si_emit_derived_tess_state(sctx, info, &num_patches);
1481
1482 /* Emit state atoms. */
1483 unsigned mask = sctx->dirty_atoms & ~skip_atom_mask;
1484 while (mask)
1485 sctx->atoms.array[u_bit_scan(&mask)].emit(sctx);
1486
1487 sctx->dirty_atoms &= skip_atom_mask;
1488
1489 /* Emit states. */
1490 mask = sctx->dirty_states;
1491 while (mask) {
1492 unsigned i = u_bit_scan(&mask);
1493 struct si_pm4_state *state = sctx->queued.array[i];
1494
1495 if (!state || sctx->emitted.array[i] == state)
1496 continue;
1497
1498 si_pm4_emit(sctx, state);
1499 sctx->emitted.array[i] = state;
1500 }
1501 sctx->dirty_states = 0;
1502
1503 /* Emit draw states. */
1504 si_emit_vs_state(sctx, info);
1505 si_emit_draw_registers(sctx, info, prim, num_patches, instance_count, primitive_restart);
1506 }
1507
1508 static bool si_all_vs_resources_read_only(struct si_context *sctx, struct pipe_resource *indexbuf)
1509 {
1510 struct radeon_winsys *ws = sctx->ws;
1511 struct radeon_cmdbuf *cs = sctx->gfx_cs;
1512
1513 /* Index buffer. */
1514 if (indexbuf && ws->cs_is_buffer_referenced(cs, si_resource(indexbuf)->buf, RADEON_USAGE_WRITE))
1515 goto has_write_reference;
1516
1517 /* Vertex buffers. */
1518 struct si_vertex_elements *velems = sctx->vertex_elements;
1519 unsigned num_velems = velems->count;
1520
1521 for (unsigned i = 0; i < num_velems; i++) {
1522 if (!((1 << i) & velems->first_vb_use_mask))
1523 continue;
1524
1525 unsigned vb_index = velems->vertex_buffer_index[i];
1526 struct pipe_resource *res = sctx->vertex_buffer[vb_index].buffer.resource;
1527 if (!res)
1528 continue;
1529
1530 if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf, RADEON_USAGE_WRITE))
1531 goto has_write_reference;
1532 }
1533
1534 /* Constant and shader buffers. */
1535 struct si_descriptors *buffers =
1536 &sctx->descriptors[si_const_and_shader_buffer_descriptors_idx(PIPE_SHADER_VERTEX)];
1537 for (unsigned i = 0; i < buffers->num_active_slots; i++) {
1538 unsigned index = buffers->first_active_slot + i;
1539 struct pipe_resource *res = sctx->const_and_shader_buffers[PIPE_SHADER_VERTEX].buffers[index];
1540 if (!res)
1541 continue;
1542
1543 if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf, RADEON_USAGE_WRITE))
1544 goto has_write_reference;
1545 }
1546
1547 /* Samplers. */
1548 struct si_shader_selector *vs = sctx->vs_shader.cso;
1549 if (vs->info.samplers_declared) {
1550 unsigned num_samplers = util_last_bit(vs->info.samplers_declared);
1551
1552 for (unsigned i = 0; i < num_samplers; i++) {
1553 struct pipe_sampler_view *view = sctx->samplers[PIPE_SHADER_VERTEX].views[i];
1554 if (!view)
1555 continue;
1556
1557 if (ws->cs_is_buffer_referenced(cs, si_resource(view->texture)->buf, RADEON_USAGE_WRITE))
1558 goto has_write_reference;
1559 }
1560 }
1561
1562 /* Images. */
1563 if (vs->info.images_declared) {
1564 unsigned num_images = util_last_bit(vs->info.images_declared);
1565
1566 for (unsigned i = 0; i < num_images; i++) {
1567 struct pipe_resource *res = sctx->images[PIPE_SHADER_VERTEX].views[i].resource;
1568 if (!res)
1569 continue;
1570
1571 if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf, RADEON_USAGE_WRITE))
1572 goto has_write_reference;
1573 }
1574 }
1575
1576 return true;
1577
1578 has_write_reference:
1579 /* If the current gfx IB has enough packets, flush it to remove write
1580 * references to buffers.
1581 */
1582 if (cs->prev_dw + cs->current.cdw > 2048) {
1583 si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
1584 assert(si_all_vs_resources_read_only(sctx, indexbuf));
1585 return true;
1586 }
1587 return false;
1588 }
1589
1590 static ALWAYS_INLINE bool pd_msg(const char *s)
1591 {
1592 if (SI_PRIM_DISCARD_DEBUG)
1593 printf("PD failed: %s\n", s);
1594 return false;
1595 }
1596
1597 static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
1598 {
1599 struct si_context *sctx = (struct si_context *)ctx;
1600 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1601 struct pipe_resource *indexbuf = info->index.resource;
1602 unsigned dirty_tex_counter, dirty_buf_counter;
1603 enum pipe_prim_type rast_prim, prim = info->mode;
1604 unsigned index_size = info->index_size;
1605 unsigned index_offset = info->indirect ? info->start * index_size : 0;
1606 unsigned instance_count = info->instance_count;
1607 bool primitive_restart =
1608 info->primitive_restart &&
1609 (!sctx->screen->options.prim_restart_tri_strips_only ||
1610 (prim != PIPE_PRIM_TRIANGLE_STRIP && prim != PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY));
1611
1612 if (likely(!info->indirect)) {
1613 /* GFX6-GFX7 treat instance_count==0 as instance_count==1. There is
1614 * no workaround for indirect draws, but we can at least skip
1615 * direct draws.
1616 */
1617 if (unlikely(!instance_count))
1618 return;
1619
1620 /* Handle count == 0. */
1621 if (unlikely(!info->count && (index_size || !info->count_from_stream_output)))
1622 return;
1623 }
1624
1625 struct si_shader_selector *vs = sctx->vs_shader.cso;
1626 if (unlikely(!vs || sctx->num_vertex_elements < vs->num_vs_inputs ||
1627 (!sctx->ps_shader.cso && !rs->rasterizer_discard) ||
1628 (!!sctx->tes_shader.cso != (prim == PIPE_PRIM_PATCHES)))) {
1629 assert(0);
1630 return;
1631 }
1632
1633 /* Recompute and re-emit the texture resource states if needed. */
1634 dirty_tex_counter = p_atomic_read(&sctx->screen->dirty_tex_counter);
1635 if (unlikely(dirty_tex_counter != sctx->last_dirty_tex_counter)) {
1636 sctx->last_dirty_tex_counter = dirty_tex_counter;
1637 sctx->framebuffer.dirty_cbufs |= ((1 << sctx->framebuffer.state.nr_cbufs) - 1);
1638 sctx->framebuffer.dirty_zsbuf = true;
1639 si_mark_atom_dirty(sctx, &sctx->atoms.s.framebuffer);
1640 si_update_all_texture_descriptors(sctx);
1641 }
1642
1643 dirty_buf_counter = p_atomic_read(&sctx->screen->dirty_buf_counter);
1644 if (unlikely(dirty_buf_counter != sctx->last_dirty_buf_counter)) {
1645 sctx->last_dirty_buf_counter = dirty_buf_counter;
1646 /* Rebind all buffers unconditionally. */
1647 si_rebind_buffer(sctx, NULL);
1648 }
1649
1650 si_decompress_textures(sctx, u_bit_consecutive(0, SI_NUM_GRAPHICS_SHADERS));
1651
1652 /* Set the rasterization primitive type.
1653 *
1654 * This must be done after si_decompress_textures, which can call
1655 * draw_vbo recursively, and before si_update_shaders, which uses
1656 * current_rast_prim for this draw_vbo call. */
1657 if (sctx->gs_shader.cso) {
1658 /* Only possibilities: POINTS, LINE_STRIP, TRIANGLES */
1659 rast_prim = sctx->gs_shader.cso->rast_prim;
1660 } else if (sctx->tes_shader.cso) {
1661 /* Only possibilities: POINTS, LINE_STRIP, TRIANGLES */
1662 rast_prim = sctx->tes_shader.cso->rast_prim;
1663 } else if (util_rast_prim_is_triangles(prim)) {
1664 rast_prim = PIPE_PRIM_TRIANGLES;
1665 } else {
1666 /* Only possibilities, POINTS, LINE*, RECTANGLES */
1667 rast_prim = prim;
1668 }
1669
1670 if (rast_prim != sctx->current_rast_prim) {
1671 if (util_prim_is_points_or_lines(sctx->current_rast_prim) !=
1672 util_prim_is_points_or_lines(rast_prim))
1673 si_mark_atom_dirty(sctx, &sctx->atoms.s.guardband);
1674
1675 sctx->current_rast_prim = rast_prim;
1676 sctx->do_update_shaders = true;
1677 }
1678
1679 if (sctx->tes_shader.cso && sctx->screen->info.has_ls_vgpr_init_bug) {
1680 /* Determine whether the LS VGPR fix should be applied.
1681 *
1682 * It is only required when num input CPs > num output CPs,
1683 * which cannot happen with the fixed function TCS. We should
1684 * also update this bit when switching from TCS to fixed
1685 * function TCS.
1686 */
1687 struct si_shader_selector *tcs = sctx->tcs_shader.cso;
1688 bool ls_vgpr_fix =
1689 tcs && info->vertices_per_patch > tcs->info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
1690
1691 if (ls_vgpr_fix != sctx->ls_vgpr_fix) {
1692 sctx->ls_vgpr_fix = ls_vgpr_fix;
1693 sctx->do_update_shaders = true;
1694 }
1695 }
1696
1697 if (sctx->chip_class <= GFX9 && sctx->gs_shader.cso) {
1698 /* Determine whether the GS triangle strip adjacency fix should
1699 * be applied. Rotate every other triangle if
1700 * - triangle strips with adjacency are fed to the GS and
1701 * - primitive restart is disabled (the rotation doesn't help
1702 * when the restart occurs after an odd number of triangles).
1703 */
1704 bool gs_tri_strip_adj_fix =
1705 !sctx->tes_shader.cso && prim == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY && !primitive_restart;
1706
1707 if (gs_tri_strip_adj_fix != sctx->gs_tri_strip_adj_fix) {
1708 sctx->gs_tri_strip_adj_fix = gs_tri_strip_adj_fix;
1709 sctx->do_update_shaders = true;
1710 }
1711 }
1712
1713 if (index_size) {
1714 /* Translate or upload, if needed. */
1715 /* 8-bit indices are supported on GFX8. */
1716 if (sctx->chip_class <= GFX7 && index_size == 1) {
1717 unsigned start, count, start_offset, size, offset;
1718 void *ptr;
1719
1720 si_get_draw_start_count(sctx, info, &start, &count);
1721 start_offset = start * 2;
1722 size = count * 2;
1723
1724 indexbuf = NULL;
1725 u_upload_alloc(ctx->stream_uploader, start_offset, size,
1726 si_optimal_tcc_alignment(sctx, size), &offset, &indexbuf, &ptr);
1727 if (!indexbuf)
1728 return;
1729
1730 util_shorten_ubyte_elts_to_userptr(&sctx->b, info, 0, 0, index_offset + start, count, ptr);
1731
1732 /* info->start will be added by the drawing code */
1733 index_offset = offset - start_offset;
1734 index_size = 2;
1735 } else if (info->has_user_indices) {
1736 unsigned start_offset;
1737
1738 assert(!info->indirect);
1739 start_offset = info->start * index_size;
1740
1741 indexbuf = NULL;
1742 u_upload_data(ctx->stream_uploader, start_offset, info->count * index_size,
1743 sctx->screen->info.tcc_cache_line_size,
1744 (char *)info->index.user + start_offset, &index_offset, &indexbuf);
1745 if (!indexbuf)
1746 return;
1747
1748 /* info->start will be added by the drawing code */
1749 index_offset -= start_offset;
1750 } else if (sctx->chip_class <= GFX7 && si_resource(indexbuf)->TC_L2_dirty) {
1751 /* GFX8 reads index buffers through TC L2, so it doesn't
1752 * need this. */
1753 sctx->flags |= SI_CONTEXT_WB_L2;
1754 si_resource(indexbuf)->TC_L2_dirty = false;
1755 }
1756 }
1757
1758 bool dispatch_prim_discard_cs = false;
1759 bool prim_discard_cs_instancing = false;
1760 unsigned original_index_size = index_size;
1761 unsigned direct_count = 0;
1762
1763 if (info->indirect) {
1764 struct pipe_draw_indirect_info *indirect = info->indirect;
1765
1766 /* Add the buffer size for memory checking in need_cs_space. */
1767 si_context_add_resource_size(sctx, indirect->buffer);
1768
1769 /* Indirect buffers use TC L2 on GFX9, but not older hw. */
1770 if (sctx->chip_class <= GFX8) {
1771 if (si_resource(indirect->buffer)->TC_L2_dirty) {
1772 sctx->flags |= SI_CONTEXT_WB_L2;
1773 si_resource(indirect->buffer)->TC_L2_dirty = false;
1774 }
1775
1776 if (indirect->indirect_draw_count &&
1777 si_resource(indirect->indirect_draw_count)->TC_L2_dirty) {
1778 sctx->flags |= SI_CONTEXT_WB_L2;
1779 si_resource(indirect->indirect_draw_count)->TC_L2_dirty = false;
1780 }
1781 }
1782 } else {
1783 /* Multiply by 3 for strips and fans to get an approximate vertex
1784 * count as triangles. */
1785 direct_count = info->count * instance_count * (prim == PIPE_PRIM_TRIANGLES ? 1 : 3);
1786 }
1787
1788 /* Determine if we can use the primitive discard compute shader. */
1789 if (si_compute_prim_discard_enabled(sctx) &&
1790 (direct_count > sctx->prim_discard_vertex_count_threshold
1791 ? (sctx->compute_num_verts_rejected += direct_count, true)
1792 : /* Add, then return true. */
1793 (sctx->compute_num_verts_ineligible += direct_count,
1794 false)) && /* Add, then return false. */
1795 (!info->count_from_stream_output || pd_msg("draw_opaque")) &&
1796 (primitive_restart ?
1797 /* Supported prim types with primitive restart: */
1798 (prim == PIPE_PRIM_TRIANGLE_STRIP || pd_msg("bad prim type with primitive restart")) &&
1799 /* Disallow instancing with primitive restart: */
1800 (instance_count == 1 || pd_msg("instance_count > 1 with primitive restart"))
1801 :
1802 /* Supported prim types without primitive restart + allow instancing: */
1803 (1 << prim) & ((1 << PIPE_PRIM_TRIANGLES) | (1 << PIPE_PRIM_TRIANGLE_STRIP) |
1804 (1 << PIPE_PRIM_TRIANGLE_FAN)) &&
1805 /* Instancing is limited to 16-bit indices, because InstanceID is packed into
1806 VertexID. */
1807 /* TODO: DrawArraysInstanced doesn't sometimes work, so it's disabled. */
1808 (instance_count == 1 ||
1809 (instance_count <= USHRT_MAX && index_size && index_size <= 2) ||
1810 pd_msg("instance_count too large or index_size == 4 or DrawArraysInstanced"))) &&
1811 (info->drawid == 0 || !sctx->vs_shader.cso->info.uses_drawid || pd_msg("draw_id > 0")) &&
1812 (!sctx->render_cond || pd_msg("render condition")) &&
1813 /* Forced enablement ignores pipeline statistics queries. */
1814 (sctx->screen->debug_flags & (DBG(PD) | DBG(ALWAYS_PD)) ||
1815 (!sctx->num_pipeline_stat_queries && !sctx->streamout.prims_gen_query_enabled) ||
1816 pd_msg("pipestat or primgen query")) &&
1817 (!sctx->vertex_elements->instance_divisor_is_fetched || pd_msg("loads instance divisors")) &&
1818 (!sctx->tes_shader.cso || pd_msg("uses tess")) &&
1819 (!sctx->gs_shader.cso || pd_msg("uses GS")) &&
1820 (!sctx->ps_shader.cso->info.uses_primid || pd_msg("PS uses PrimID")) &&
1821 !rs->polygon_mode_enabled &&
1822 #if SI_PRIM_DISCARD_DEBUG /* same as cso->prim_discard_cs_allowed */
1823 (!sctx->vs_shader.cso->info.uses_bindless_images || pd_msg("uses bindless images")) &&
1824 (!sctx->vs_shader.cso->info.uses_bindless_samplers || pd_msg("uses bindless samplers")) &&
1825 (!sctx->vs_shader.cso->info.writes_memory || pd_msg("writes memory")) &&
1826 (!sctx->vs_shader.cso->info.writes_viewport_index || pd_msg("writes viewport index")) &&
1827 !sctx->vs_shader.cso->info.properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION] &&
1828 !sctx->vs_shader.cso->so.num_outputs &&
1829 #else
1830 (sctx->vs_shader.cso->prim_discard_cs_allowed ||
1831 pd_msg("VS shader uses unsupported features")) &&
1832 #endif
1833 /* Check that all buffers are used for read only, because compute
1834 * dispatches can run ahead. */
1835 (si_all_vs_resources_read_only(sctx, index_size ? indexbuf : NULL) ||
1836 pd_msg("write reference"))) {
1837 switch (si_prepare_prim_discard_or_split_draw(sctx, info, primitive_restart)) {
1838 case SI_PRIM_DISCARD_ENABLED:
1839 original_index_size = index_size;
1840 prim_discard_cs_instancing = instance_count > 1;
1841 dispatch_prim_discard_cs = true;
1842
1843 /* The compute shader changes/lowers the following: */
1844 prim = PIPE_PRIM_TRIANGLES;
1845 index_size = 4;
1846 instance_count = 1;
1847 primitive_restart = false;
1848 sctx->compute_num_verts_rejected -= direct_count;
1849 sctx->compute_num_verts_accepted += direct_count;
1850 break;
1851 case SI_PRIM_DISCARD_DISABLED:
1852 break;
1853 case SI_PRIM_DISCARD_DRAW_SPLIT:
1854 sctx->compute_num_verts_rejected -= direct_count;
1855 goto return_cleanup;
1856 }
1857 }
1858
1859 if (prim_discard_cs_instancing != sctx->prim_discard_cs_instancing) {
1860 sctx->prim_discard_cs_instancing = prim_discard_cs_instancing;
1861 sctx->do_update_shaders = true;
1862 }
1863
1864 /* Update NGG culling settings. */
1865 if (sctx->ngg && !dispatch_prim_discard_cs && rast_prim == PIPE_PRIM_TRIANGLES &&
1866 (sctx->screen->always_use_ngg_culling ||
1867 /* At least 1024 non-indexed vertices (8 subgroups) are needed
1868 * per draw call (no TES/GS) to enable NGG culling.
1869 */
1870 (!index_size && direct_count >= 1024 &&
1871 (prim == PIPE_PRIM_TRIANGLES || prim == PIPE_PRIM_TRIANGLE_STRIP) &&
1872 !sctx->tes_shader.cso && !sctx->gs_shader.cso)) &&
1873 si_get_vs(sctx)->cso->ngg_culling_allowed) {
1874 unsigned ngg_culling = 0;
1875
1876 if (rs->rasterizer_discard) {
1877 ngg_culling |= SI_NGG_CULL_FRONT_FACE | SI_NGG_CULL_BACK_FACE;
1878 } else {
1879 /* Polygon mode can't use view and small primitive culling,
1880 * because it draws points or lines where the culling depends
1881 * on the point or line width.
1882 */
1883 if (!rs->polygon_mode_enabled)
1884 ngg_culling |= SI_NGG_CULL_VIEW_SMALLPRIMS;
1885
1886 if (sctx->viewports.y_inverted ? rs->cull_back : rs->cull_front)
1887 ngg_culling |= SI_NGG_CULL_FRONT_FACE;
1888 if (sctx->viewports.y_inverted ? rs->cull_front : rs->cull_back)
1889 ngg_culling |= SI_NGG_CULL_BACK_FACE;
1890 }
1891
1892 /* Use NGG fast launch for certain non-indexed primitive types.
1893 * A draw must have at least 1 full primitive.
1894 */
1895 if (ngg_culling && !index_size && direct_count >= 3 && !sctx->tes_shader.cso &&
1896 !sctx->gs_shader.cso) {
1897 if (prim == PIPE_PRIM_TRIANGLES)
1898 ngg_culling |= SI_NGG_CULL_GS_FAST_LAUNCH_TRI_LIST;
1899 else if (prim == PIPE_PRIM_TRIANGLE_STRIP)
1900 ngg_culling |= SI_NGG_CULL_GS_FAST_LAUNCH_TRI_STRIP;
1901 }
1902
1903 if (ngg_culling != sctx->ngg_culling) {
1904 /* Insert a VGT_FLUSH when enabling fast launch changes to prevent hangs.
1905 * See issues #2418, #2426, #2434
1906 */
1907 if (ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_ALL)
1908 sctx->flags |= SI_CONTEXT_VGT_FLUSH;
1909 sctx->ngg_culling = ngg_culling;
1910 sctx->do_update_shaders = true;
1911 }
1912 } else if (sctx->ngg_culling) {
1913 sctx->ngg_culling = false;
1914 sctx->do_update_shaders = true;
1915 }
1916
1917 if (sctx->do_update_shaders && !si_update_shaders(sctx))
1918 goto return_cleanup;
1919
1920 si_need_gfx_cs_space(sctx);
1921
1922 /* If we're using a secure context, determine if cs must be secure or not */
1923 if (unlikely(sctx->ws->ws_is_secure(sctx->ws))) {
1924 bool secure = si_gfx_resources_check_encrypted(sctx);
1925 if (secure != sctx->ws->cs_is_secure(sctx->gfx_cs)) {
1926 si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
1927 sctx->ws->cs_set_secure(sctx->gfx_cs, secure);
1928 }
1929 }
1930
1931 if (sctx->bo_list_add_all_gfx_resources)
1932 si_gfx_resources_add_all_to_bo_list(sctx);
1933
1934 /* Since we've called si_context_add_resource_size for vertex buffers,
1935 * this must be called after si_need_cs_space, because we must let
1936 * need_cs_space flush before we add buffers to the buffer list.
1937 */
1938 if (!si_upload_vertex_buffer_descriptors(sctx))
1939 goto return_cleanup;
1940
1941 /* Vega10/Raven scissor bug workaround. When any context register is
1942 * written (i.e. the GPU rolls the context), PA_SC_VPORT_SCISSOR
1943 * registers must be written too.
1944 */
1945 unsigned masked_atoms = 0;
1946
1947 if (sctx->screen->info.has_gfx9_scissor_bug) {
1948 masked_atoms |= si_get_atom_bit(sctx, &sctx->atoms.s.scissors);
1949
1950 if (info->count_from_stream_output ||
1951 sctx->dirty_atoms & si_atoms_that_always_roll_context() ||
1952 sctx->dirty_states & si_states_that_always_roll_context())
1953 sctx->context_roll = true;
1954 }
1955
1956 /* Use optimal packet order based on whether we need to sync the pipeline. */
1957 if (unlikely(sctx->flags & (SI_CONTEXT_FLUSH_AND_INV_CB | SI_CONTEXT_FLUSH_AND_INV_DB |
1958 SI_CONTEXT_PS_PARTIAL_FLUSH | SI_CONTEXT_CS_PARTIAL_FLUSH))) {
1959 /* If we have to wait for idle, set all states first, so that all
1960 * SET packets are processed in parallel with previous draw calls.
1961 * Then draw and prefetch at the end. This ensures that the time
1962 * the CUs are idle is very short.
1963 */
1964 if (unlikely(sctx->flags & SI_CONTEXT_FLUSH_FOR_RENDER_COND))
1965 masked_atoms |= si_get_atom_bit(sctx, &sctx->atoms.s.render_cond);
1966
1967 if (!si_upload_graphics_shader_descriptors(sctx))
1968 goto return_cleanup;
1969
1970 /* Emit all states except possibly render condition. */
1971 si_emit_all_states(sctx, info, prim, instance_count, primitive_restart, masked_atoms);
1972 sctx->emit_cache_flush(sctx);
1973 /* <-- CUs are idle here. */
1974
1975 if (si_is_atom_dirty(sctx, &sctx->atoms.s.render_cond))
1976 sctx->atoms.s.render_cond.emit(sctx);
1977
1978 if (sctx->screen->info.has_gfx9_scissor_bug &&
1979 (sctx->context_roll || si_is_atom_dirty(sctx, &sctx->atoms.s.scissors)))
1980 sctx->atoms.s.scissors.emit(sctx);
1981
1982 sctx->dirty_atoms = 0;
1983
1984 si_emit_draw_packets(sctx, info, indexbuf, index_size, index_offset, instance_count,
1985 dispatch_prim_discard_cs, original_index_size);
1986 /* <-- CUs are busy here. */
1987
1988 /* Start prefetches after the draw has been started. Both will run
1989 * in parallel, but starting the draw first is more important.
1990 */
1991 if (sctx->chip_class >= GFX7 && sctx->prefetch_L2_mask)
1992 cik_emit_prefetch_L2(sctx, false);
1993 } else {
1994 /* If we don't wait for idle, start prefetches first, then set
1995 * states, and draw at the end.
1996 */
1997 if (sctx->flags)
1998 sctx->emit_cache_flush(sctx);
1999
2000 /* Only prefetch the API VS and VBO descriptors. */
2001 if (sctx->chip_class >= GFX7 && sctx->prefetch_L2_mask)
2002 cik_emit_prefetch_L2(sctx, true);
2003
2004 if (!si_upload_graphics_shader_descriptors(sctx))
2005 goto return_cleanup;
2006
2007 si_emit_all_states(sctx, info, prim, instance_count, primitive_restart, masked_atoms);
2008
2009 if (sctx->screen->info.has_gfx9_scissor_bug &&
2010 (sctx->context_roll || si_is_atom_dirty(sctx, &sctx->atoms.s.scissors)))
2011 sctx->atoms.s.scissors.emit(sctx);
2012
2013 sctx->dirty_atoms = 0;
2014
2015 si_emit_draw_packets(sctx, info, indexbuf, index_size, index_offset, instance_count,
2016 dispatch_prim_discard_cs, original_index_size);
2017
2018 /* Prefetch the remaining shaders after the draw has been
2019 * started. */
2020 if (sctx->chip_class >= GFX7 && sctx->prefetch_L2_mask)
2021 cik_emit_prefetch_L2(sctx, false);
2022 }
2023
2024 /* Mark the displayable dcc buffer as dirty in order to update
2025 * it on the next call to si_flush_resource. */
2026 if (sctx->screen->info.use_display_dcc_with_retile_blit) {
2027 /* Don't use si_update_fb_dirtiness_after_rendering because it'll
2028 * cause unnecessary texture decompressions on each draw. */
2029 unsigned displayable_dcc_cb_mask = sctx->framebuffer.displayable_dcc_cb_mask;
2030 while (displayable_dcc_cb_mask) {
2031 unsigned i = u_bit_scan(&displayable_dcc_cb_mask);
2032 struct pipe_surface *surf = sctx->framebuffer.state.cbufs[i];
2033 struct si_texture *tex = (struct si_texture *)surf->texture;
2034 tex->displayable_dcc_dirty = true;
2035 }
2036 }
2037
2038 /* Clear the context roll flag after the draw call. */
2039 sctx->context_roll = false;
2040
2041 if (unlikely(sctx->current_saved_cs)) {
2042 si_trace_emit(sctx);
2043 si_log_draw_state(sctx, sctx->log);
2044 }
2045
2046 /* Workaround for a VGT hang when streamout is enabled.
2047 * It must be done after drawing. */
2048 if ((sctx->family == CHIP_HAWAII || sctx->family == CHIP_TONGA || sctx->family == CHIP_FIJI) &&
2049 si_get_strmout_en(sctx)) {
2050 sctx->flags |= SI_CONTEXT_VGT_STREAMOUT_SYNC;
2051 }
2052
2053 if (unlikely(sctx->decompression_enabled)) {
2054 sctx->num_decompress_calls++;
2055 } else {
2056 sctx->num_draw_calls++;
2057 if (sctx->framebuffer.state.nr_cbufs > 1)
2058 sctx->num_mrt_draw_calls++;
2059 if (primitive_restart)
2060 sctx->num_prim_restart_calls++;
2061 if (G_0286E8_WAVESIZE(sctx->spi_tmpring_size))
2062 sctx->num_spill_draw_calls++;
2063 }
2064
2065 return_cleanup:
2066 if (index_size && indexbuf != info->index.resource)
2067 pipe_resource_reference(&indexbuf, NULL);
2068 }
2069
2070 static void si_draw_rectangle(struct blitter_context *blitter, void *vertex_elements_cso,
2071 blitter_get_vs_func get_vs, int x1, int y1, int x2, int y2,
2072 float depth, unsigned num_instances, enum blitter_attrib_type type,
2073 const union blitter_attrib *attrib)
2074 {
2075 struct pipe_context *pipe = util_blitter_get_pipe(blitter);
2076 struct si_context *sctx = (struct si_context *)pipe;
2077
2078 /* Pack position coordinates as signed int16. */
2079 sctx->vs_blit_sh_data[0] = (uint32_t)(x1 & 0xffff) | ((uint32_t)(y1 & 0xffff) << 16);
2080 sctx->vs_blit_sh_data[1] = (uint32_t)(x2 & 0xffff) | ((uint32_t)(y2 & 0xffff) << 16);
2081 sctx->vs_blit_sh_data[2] = fui(depth);
2082
2083 switch (type) {
2084 case UTIL_BLITTER_ATTRIB_COLOR:
2085 memcpy(&sctx->vs_blit_sh_data[3], attrib->color, sizeof(float) * 4);
2086 break;
2087 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY:
2088 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW:
2089 memcpy(&sctx->vs_blit_sh_data[3], &attrib->texcoord, sizeof(attrib->texcoord));
2090 break;
2091 case UTIL_BLITTER_ATTRIB_NONE:;
2092 }
2093
2094 pipe->bind_vs_state(pipe, si_get_blitter_vs(sctx, type, num_instances));
2095
2096 struct pipe_draw_info info = {};
2097 info.mode = SI_PRIM_RECTANGLE_LIST;
2098 info.count = 3;
2099 info.instance_count = num_instances;
2100
2101 /* Don't set per-stage shader pointers for VS. */
2102 sctx->shader_pointers_dirty &= ~SI_DESCS_SHADER_MASK(VERTEX);
2103 sctx->vertex_buffer_pointer_dirty = false;
2104 sctx->vertex_buffer_user_sgprs_dirty = false;
2105
2106 si_draw_vbo(pipe, &info);
2107 }
2108
2109 void si_trace_emit(struct si_context *sctx)
2110 {
2111 struct radeon_cmdbuf *cs = sctx->gfx_cs;
2112 uint32_t trace_id = ++sctx->current_saved_cs->trace_id;
2113
2114 si_cp_write_data(sctx, sctx->current_saved_cs->trace_buf, 0, 4, V_370_MEM, V_370_ME, &trace_id);
2115
2116 radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
2117 radeon_emit(cs, AC_ENCODE_TRACE_POINT(trace_id));
2118
2119 if (sctx->log)
2120 u_log_flush(sctx->log);
2121 }
2122
2123 void si_init_draw_functions(struct si_context *sctx)
2124 {
2125 sctx->b.draw_vbo = si_draw_vbo;
2126
2127 sctx->blitter->draw_rectangle = si_draw_rectangle;
2128
2129 si_init_ia_multi_vgt_param_table(sctx);
2130 }