radeonsi: don't allow user indices with indirect draws
[mesa.git] / src / gallium / drivers / radeonsi / si_state_draw.c
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Christian König <christian.koenig@amd.com>
25 */
26
27 #include "si_pipe.h"
28 #include "radeon/r600_cs.h"
29 #include "sid.h"
30 #include "gfx9d.h"
31
32 #include "util/u_index_modify.h"
33 #include "util/u_upload_mgr.h"
34 #include "util/u_prim.h"
35
36 #include "ac_debug.h"
37
38 static unsigned si_conv_pipe_prim(unsigned mode)
39 {
40 static const unsigned prim_conv[] = {
41 [PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
42 [PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
43 [PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
44 [PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
45 [PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
46 [PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
47 [PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
48 [PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
49 [PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
50 [PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
51 [PIPE_PRIM_LINES_ADJACENCY] = V_008958_DI_PT_LINELIST_ADJ,
52 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_008958_DI_PT_LINESTRIP_ADJ,
53 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_008958_DI_PT_TRILIST_ADJ,
54 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_008958_DI_PT_TRISTRIP_ADJ,
55 [PIPE_PRIM_PATCHES] = V_008958_DI_PT_PATCH,
56 [R600_PRIM_RECTANGLE_LIST] = V_008958_DI_PT_RECTLIST
57 };
58 assert(mode < ARRAY_SIZE(prim_conv));
59 return prim_conv[mode];
60 }
61
62 static unsigned si_conv_prim_to_gs_out(unsigned mode)
63 {
64 static const int prim_conv[] = {
65 [PIPE_PRIM_POINTS] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
66 [PIPE_PRIM_LINES] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
67 [PIPE_PRIM_LINE_LOOP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
68 [PIPE_PRIM_LINE_STRIP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
69 [PIPE_PRIM_TRIANGLES] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
70 [PIPE_PRIM_TRIANGLE_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
71 [PIPE_PRIM_TRIANGLE_FAN] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
72 [PIPE_PRIM_QUADS] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
73 [PIPE_PRIM_QUAD_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
74 [PIPE_PRIM_POLYGON] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
75 [PIPE_PRIM_LINES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
76 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
77 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
78 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
79 [PIPE_PRIM_PATCHES] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
80 [R600_PRIM_RECTANGLE_LIST] = V_028A6C_OUTPRIM_TYPE_TRISTRIP
81 };
82 assert(mode < ARRAY_SIZE(prim_conv));
83
84 return prim_conv[mode];
85 }
86
87 /**
88 * This calculates the LDS size for tessellation shaders (VS, TCS, TES).
89 * LS.LDS_SIZE is shared by all 3 shader stages.
90 *
91 * The information about LDS and other non-compile-time parameters is then
92 * written to userdata SGPRs.
93 */
94 static void si_emit_derived_tess_state(struct si_context *sctx,
95 const struct pipe_draw_info *info,
96 unsigned *num_patches)
97 {
98 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
99 struct si_shader_ctx_state *ls = &sctx->vs_shader;
100 /* The TES pointer will only be used for sctx->last_tcs.
101 * It would be wrong to think that TCS = TES. */
102 struct si_shader_selector *tcs =
103 sctx->tcs_shader.cso ? sctx->tcs_shader.cso : sctx->tes_shader.cso;
104 unsigned tes_sh_base = sctx->shader_userdata.sh_base[PIPE_SHADER_TESS_EVAL];
105 unsigned num_tcs_input_cp = info->vertices_per_patch;
106 unsigned num_tcs_output_cp, num_tcs_inputs, num_tcs_outputs;
107 unsigned num_tcs_patch_outputs;
108 unsigned input_vertex_size, output_vertex_size, pervertex_output_patch_size;
109 unsigned input_patch_size, output_patch_size, output_patch0_offset;
110 unsigned perpatch_output_offset, lds_size, ls_rsrc2;
111 unsigned tcs_in_layout, tcs_out_layout, tcs_out_offsets;
112 unsigned offchip_layout, hardware_lds_size, ls_hs_config;
113
114 if (sctx->last_ls == ls->current &&
115 sctx->last_tcs == tcs &&
116 sctx->last_tes_sh_base == tes_sh_base &&
117 sctx->last_num_tcs_input_cp == num_tcs_input_cp) {
118 *num_patches = sctx->last_num_patches;
119 return;
120 }
121
122 sctx->last_ls = ls->current;
123 sctx->last_tcs = tcs;
124 sctx->last_tes_sh_base = tes_sh_base;
125 sctx->last_num_tcs_input_cp = num_tcs_input_cp;
126
127 /* This calculates how shader inputs and outputs among VS, TCS, and TES
128 * are laid out in LDS. */
129 num_tcs_inputs = util_last_bit64(ls->cso->outputs_written);
130
131 if (sctx->tcs_shader.cso) {
132 num_tcs_outputs = util_last_bit64(tcs->outputs_written);
133 num_tcs_output_cp = tcs->info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
134 num_tcs_patch_outputs = util_last_bit64(tcs->patch_outputs_written);
135 } else {
136 /* No TCS. Route varyings from LS to TES. */
137 num_tcs_outputs = num_tcs_inputs;
138 num_tcs_output_cp = num_tcs_input_cp;
139 num_tcs_patch_outputs = 2; /* TESSINNER + TESSOUTER */
140 }
141
142 input_vertex_size = num_tcs_inputs * 16;
143 output_vertex_size = num_tcs_outputs * 16;
144
145 input_patch_size = num_tcs_input_cp * input_vertex_size;
146
147 pervertex_output_patch_size = num_tcs_output_cp * output_vertex_size;
148 output_patch_size = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
149
150 /* Ensure that we only need one wave per SIMD so we don't need to check
151 * resource usage. Also ensures that the number of tcs in and out
152 * vertices per threadgroup are at most 256.
153 */
154 *num_patches = 64 / MAX2(num_tcs_input_cp, num_tcs_output_cp) * 4;
155
156 /* Make sure that the data fits in LDS. This assumes the shaders only
157 * use LDS for the inputs and outputs.
158 */
159 hardware_lds_size = sctx->b.chip_class >= CIK ? 65536 : 32768;
160 *num_patches = MIN2(*num_patches, hardware_lds_size / (input_patch_size +
161 output_patch_size));
162
163 /* Make sure the output data fits in the offchip buffer */
164 *num_patches = MIN2(*num_patches,
165 (sctx->screen->tess_offchip_block_dw_size * 4) /
166 output_patch_size);
167
168 /* Not necessary for correctness, but improves performance. The
169 * specific value is taken from the proprietary driver.
170 */
171 *num_patches = MIN2(*num_patches, 40);
172
173 /* SI bug workaround - limit LS-HS threadgroups to only one wave. */
174 if (sctx->b.chip_class == SI) {
175 unsigned one_wave = 64 / MAX2(num_tcs_input_cp, num_tcs_output_cp);
176 *num_patches = MIN2(*num_patches, one_wave);
177 }
178
179 sctx->last_num_patches = *num_patches;
180
181 output_patch0_offset = input_patch_size * *num_patches;
182 perpatch_output_offset = output_patch0_offset + pervertex_output_patch_size;
183
184 lds_size = output_patch0_offset + output_patch_size * *num_patches;
185 ls_rsrc2 = ls->current->config.rsrc2;
186
187 if (sctx->b.chip_class >= CIK) {
188 assert(lds_size <= 65536);
189 lds_size = align(lds_size, 512) / 512;
190 } else {
191 assert(lds_size <= 32768);
192 lds_size = align(lds_size, 256) / 256;
193 }
194 si_multiwave_lds_size_workaround(sctx->screen, &lds_size);
195 ls_rsrc2 |= S_00B52C_LDS_SIZE(lds_size);
196
197 /* Due to a hw bug, RSRC2_LS must be written twice with another
198 * LS register written in between. */
199 if (sctx->b.chip_class == CIK && sctx->b.family != CHIP_HAWAII)
200 radeon_set_sh_reg(cs, R_00B52C_SPI_SHADER_PGM_RSRC2_LS, ls_rsrc2);
201 radeon_set_sh_reg_seq(cs, R_00B528_SPI_SHADER_PGM_RSRC1_LS, 2);
202 radeon_emit(cs, ls->current->config.rsrc1);
203 radeon_emit(cs, ls_rsrc2);
204
205 /* Compute userdata SGPRs. */
206 assert(((input_vertex_size / 4) & ~0xff) == 0);
207 assert(((output_vertex_size / 4) & ~0xff) == 0);
208 assert(((input_patch_size / 4) & ~0x1fff) == 0);
209 assert(((output_patch_size / 4) & ~0x1fff) == 0);
210 assert(((output_patch0_offset / 16) & ~0xffff) == 0);
211 assert(((perpatch_output_offset / 16) & ~0xffff) == 0);
212 assert(num_tcs_input_cp <= 32);
213 assert(num_tcs_output_cp <= 32);
214
215 tcs_in_layout = S_VS_STATE_LS_OUT_PATCH_SIZE(input_patch_size / 4) |
216 S_VS_STATE_LS_OUT_VERTEX_SIZE(input_vertex_size / 4);
217 tcs_out_layout = (output_patch_size / 4) |
218 ((output_vertex_size / 4) << 13);
219 tcs_out_offsets = (output_patch0_offset / 16) |
220 ((perpatch_output_offset / 16) << 16);
221 offchip_layout = (pervertex_output_patch_size * *num_patches << 16) |
222 (num_tcs_output_cp << 9) | *num_patches;
223
224 /* Set them for LS. */
225 sctx->current_vs_state &= C_VS_STATE_LS_OUT_PATCH_SIZE &
226 C_VS_STATE_LS_OUT_VERTEX_SIZE;
227 sctx->current_vs_state |= tcs_in_layout;
228
229 /* Set them for TCS. */
230 radeon_set_sh_reg_seq(cs,
231 R_00B430_SPI_SHADER_USER_DATA_HS_0 + SI_SGPR_TCS_OFFCHIP_LAYOUT * 4, 4);
232 radeon_emit(cs, offchip_layout);
233 radeon_emit(cs, tcs_out_offsets);
234 radeon_emit(cs, tcs_out_layout | (num_tcs_input_cp << 26));
235 radeon_emit(cs, tcs_in_layout);
236
237 /* Set them for TES. */
238 radeon_set_sh_reg_seq(cs, tes_sh_base + SI_SGPR_TCS_OFFCHIP_LAYOUT * 4, 1);
239 radeon_emit(cs, offchip_layout);
240
241 ls_hs_config = S_028B58_NUM_PATCHES(*num_patches) |
242 S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp) |
243 S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp);
244
245 if (sctx->b.chip_class >= CIK)
246 radeon_set_context_reg_idx(cs, R_028B58_VGT_LS_HS_CONFIG, 2,
247 ls_hs_config);
248 else
249 radeon_set_context_reg(cs, R_028B58_VGT_LS_HS_CONFIG,
250 ls_hs_config);
251 }
252
253 static unsigned si_num_prims_for_vertices(const struct pipe_draw_info *info)
254 {
255 switch (info->mode) {
256 case PIPE_PRIM_PATCHES:
257 return info->count / info->vertices_per_patch;
258 case R600_PRIM_RECTANGLE_LIST:
259 return info->count / 3;
260 default:
261 return u_prims_for_vertices(info->mode, info->count);
262 }
263 }
264
265 static unsigned
266 si_get_init_multi_vgt_param(struct si_screen *sscreen,
267 union si_vgt_param_key *key)
268 {
269 STATIC_ASSERT(sizeof(union si_vgt_param_key) == 4);
270 unsigned max_primgroup_in_wave = 2;
271
272 /* SWITCH_ON_EOP(0) is always preferable. */
273 bool wd_switch_on_eop = false;
274 bool ia_switch_on_eop = false;
275 bool ia_switch_on_eoi = false;
276 bool partial_vs_wave = false;
277 bool partial_es_wave = false;
278
279 if (key->u.uses_tess) {
280 /* SWITCH_ON_EOI must be set if PrimID is used. */
281 if (key->u.tcs_tes_uses_prim_id)
282 ia_switch_on_eoi = true;
283
284 /* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
285 if ((sscreen->b.family == CHIP_TAHITI ||
286 sscreen->b.family == CHIP_PITCAIRN ||
287 sscreen->b.family == CHIP_BONAIRE) &&
288 key->u.uses_gs)
289 partial_vs_wave = true;
290
291 /* Needed for 028B6C_DISTRIBUTION_MODE != 0 */
292 if (sscreen->has_distributed_tess) {
293 if (key->u.uses_gs) {
294 partial_es_wave = true;
295
296 /* GPU hang workaround. */
297 if (sscreen->b.family == CHIP_TONGA ||
298 sscreen->b.family == CHIP_FIJI ||
299 sscreen->b.family == CHIP_POLARIS10 ||
300 sscreen->b.family == CHIP_POLARIS11)
301 partial_vs_wave = true;
302 } else {
303 partial_vs_wave = true;
304 }
305 }
306 }
307
308 /* This is a hardware requirement. */
309 if (key->u.line_stipple_enabled ||
310 (sscreen->b.debug_flags & DBG_SWITCH_ON_EOP)) {
311 ia_switch_on_eop = true;
312 wd_switch_on_eop = true;
313 }
314
315 if (sscreen->b.chip_class >= CIK) {
316 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
317 * 4 shader engines. Set 1 to pass the assertion below.
318 * The other cases are hardware requirements.
319 *
320 * Polaris supports primitive restart with WD_SWITCH_ON_EOP=0
321 * for points, line strips, and tri strips.
322 */
323 if (sscreen->b.info.max_se < 4 ||
324 key->u.prim == PIPE_PRIM_POLYGON ||
325 key->u.prim == PIPE_PRIM_LINE_LOOP ||
326 key->u.prim == PIPE_PRIM_TRIANGLE_FAN ||
327 key->u.prim == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY ||
328 (key->u.primitive_restart &&
329 (sscreen->b.family < CHIP_POLARIS10 ||
330 (key->u.prim != PIPE_PRIM_POINTS &&
331 key->u.prim != PIPE_PRIM_LINE_STRIP &&
332 key->u.prim != PIPE_PRIM_TRIANGLE_STRIP))) ||
333 key->u.count_from_stream_output)
334 wd_switch_on_eop = true;
335
336 /* Hawaii hangs if instancing is enabled and WD_SWITCH_ON_EOP is 0.
337 * We don't know that for indirect drawing, so treat it as
338 * always problematic. */
339 if (sscreen->b.family == CHIP_HAWAII &&
340 key->u.uses_instancing)
341 wd_switch_on_eop = true;
342
343 /* Performance recommendation for 4 SE Gfx7-8 parts if
344 * instances are smaller than a primgroup.
345 * Assume indirect draws always use small instances.
346 * This is needed for good VS wave utilization.
347 */
348 if (sscreen->b.chip_class <= VI &&
349 sscreen->b.info.max_se == 4 &&
350 key->u.multi_instances_smaller_than_primgroup)
351 wd_switch_on_eop = true;
352
353 /* Required on CIK and later. */
354 if (sscreen->b.info.max_se > 2 && !wd_switch_on_eop)
355 ia_switch_on_eoi = true;
356
357 /* Required by Hawaii and, for some special cases, by VI. */
358 if (ia_switch_on_eoi &&
359 (sscreen->b.family == CHIP_HAWAII ||
360 (sscreen->b.chip_class == VI &&
361 (key->u.uses_gs || max_primgroup_in_wave != 2))))
362 partial_vs_wave = true;
363
364 /* Instancing bug on Bonaire. */
365 if (sscreen->b.family == CHIP_BONAIRE && ia_switch_on_eoi &&
366 key->u.uses_instancing)
367 partial_vs_wave = true;
368
369 /* If the WD switch is false, the IA switch must be false too. */
370 assert(wd_switch_on_eop || !ia_switch_on_eop);
371 }
372
373 /* If SWITCH_ON_EOI is set, PARTIAL_ES_WAVE must be set too. */
374 if (ia_switch_on_eoi)
375 partial_es_wave = true;
376
377 return S_028AA8_SWITCH_ON_EOP(ia_switch_on_eop) |
378 S_028AA8_SWITCH_ON_EOI(ia_switch_on_eoi) |
379 S_028AA8_PARTIAL_VS_WAVE_ON(partial_vs_wave) |
380 S_028AA8_PARTIAL_ES_WAVE_ON(partial_es_wave) |
381 S_028AA8_WD_SWITCH_ON_EOP(sscreen->b.chip_class >= CIK ? wd_switch_on_eop : 0) |
382 S_028AA8_MAX_PRIMGRP_IN_WAVE(sscreen->b.chip_class >= VI ?
383 max_primgroup_in_wave : 0) |
384 S_030960_EN_INST_OPT_BASIC(sscreen->b.chip_class >= GFX9) |
385 S_030960_EN_INST_OPT_ADV(sscreen->b.chip_class >= GFX9);
386 }
387
388 void si_init_ia_multi_vgt_param_table(struct si_context *sctx)
389 {
390 for (int prim = 0; prim <= R600_PRIM_RECTANGLE_LIST; prim++)
391 for (int uses_instancing = 0; uses_instancing < 2; uses_instancing++)
392 for (int multi_instances = 0; multi_instances < 2; multi_instances++)
393 for (int primitive_restart = 0; primitive_restart < 2; primitive_restart++)
394 for (int count_from_so = 0; count_from_so < 2; count_from_so++)
395 for (int line_stipple = 0; line_stipple < 2; line_stipple++)
396 for (int uses_tess = 0; uses_tess < 2; uses_tess++)
397 for (int tess_uses_primid = 0; tess_uses_primid < 2; tess_uses_primid++)
398 for (int uses_gs = 0; uses_gs < 2; uses_gs++) {
399 union si_vgt_param_key key;
400
401 key.index = 0;
402 key.u.prim = prim;
403 key.u.uses_instancing = uses_instancing;
404 key.u.multi_instances_smaller_than_primgroup = multi_instances;
405 key.u.primitive_restart = primitive_restart;
406 key.u.count_from_stream_output = count_from_so;
407 key.u.line_stipple_enabled = line_stipple;
408 key.u.uses_tess = uses_tess;
409 key.u.tcs_tes_uses_prim_id = tess_uses_primid;
410 key.u.uses_gs = uses_gs;
411
412 sctx->ia_multi_vgt_param[key.index] =
413 si_get_init_multi_vgt_param(sctx->screen, &key);
414 }
415 }
416
417 static unsigned si_get_ia_multi_vgt_param(struct si_context *sctx,
418 const struct pipe_draw_info *info,
419 unsigned num_patches)
420 {
421 union si_vgt_param_key key = sctx->ia_multi_vgt_param_key;
422 unsigned primgroup_size;
423 unsigned ia_multi_vgt_param;
424
425 if (sctx->tes_shader.cso) {
426 primgroup_size = num_patches; /* must be a multiple of NUM_PATCHES */
427 } else if (sctx->gs_shader.cso) {
428 primgroup_size = 64; /* recommended with a GS */
429 } else {
430 primgroup_size = 128; /* recommended without a GS and tess */
431 }
432
433 key.u.prim = info->mode;
434 key.u.uses_instancing = info->indirect || info->instance_count > 1;
435 key.u.multi_instances_smaller_than_primgroup =
436 info->indirect ||
437 (info->instance_count > 1 &&
438 (info->count_from_stream_output ||
439 si_num_prims_for_vertices(info) < primgroup_size));
440 key.u.primitive_restart = info->primitive_restart;
441 key.u.count_from_stream_output = info->count_from_stream_output != NULL;
442
443 ia_multi_vgt_param = sctx->ia_multi_vgt_param[key.index] |
444 S_028AA8_PRIMGROUP_SIZE(primgroup_size - 1);
445
446 if (sctx->gs_shader.cso) {
447 /* GS requirement. */
448 if (SI_GS_PER_ES / primgroup_size >= sctx->screen->gs_table_depth - 3)
449 ia_multi_vgt_param |= S_028AA8_PARTIAL_ES_WAVE_ON(1);
450
451 /* GS hw bug with single-primitive instances and SWITCH_ON_EOI.
452 * The hw doc says all multi-SE chips are affected, but Vulkan
453 * only applies it to Hawaii. Do what Vulkan does.
454 */
455 if (sctx->b.family == CHIP_HAWAII &&
456 G_028AA8_SWITCH_ON_EOI(ia_multi_vgt_param) &&
457 (info->indirect ||
458 (info->instance_count > 1 &&
459 (info->count_from_stream_output ||
460 si_num_prims_for_vertices(info) <= 1))))
461 sctx->b.flags |= SI_CONTEXT_VGT_FLUSH;
462 }
463
464 return ia_multi_vgt_param;
465 }
466
467 /* rast_prim is the primitive type after GS. */
468 static void si_emit_rasterizer_prim_state(struct si_context *sctx)
469 {
470 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
471 enum pipe_prim_type rast_prim = sctx->current_rast_prim;
472 struct si_state_rasterizer *rs = sctx->emitted.named.rasterizer;
473
474 /* Skip this if not rendering lines. */
475 if (rast_prim != PIPE_PRIM_LINES &&
476 rast_prim != PIPE_PRIM_LINE_LOOP &&
477 rast_prim != PIPE_PRIM_LINE_STRIP &&
478 rast_prim != PIPE_PRIM_LINES_ADJACENCY &&
479 rast_prim != PIPE_PRIM_LINE_STRIP_ADJACENCY)
480 return;
481
482 if (rast_prim == sctx->last_rast_prim &&
483 rs->pa_sc_line_stipple == sctx->last_sc_line_stipple)
484 return;
485
486 /* For lines, reset the stipple pattern at each primitive. Otherwise,
487 * reset the stipple pattern at each packet (line strips, line loops).
488 */
489 radeon_set_context_reg(cs, R_028A0C_PA_SC_LINE_STIPPLE,
490 rs->pa_sc_line_stipple |
491 S_028A0C_AUTO_RESET_CNTL(rast_prim == PIPE_PRIM_LINES ? 1 : 2));
492
493 sctx->last_rast_prim = rast_prim;
494 sctx->last_sc_line_stipple = rs->pa_sc_line_stipple;
495 }
496
497 static void si_emit_vs_state(struct si_context *sctx,
498 const struct pipe_draw_info *info)
499 {
500 sctx->current_vs_state &= C_VS_STATE_INDEXED;
501 sctx->current_vs_state |= S_VS_STATE_INDEXED(!!info->indexed);
502
503 if (sctx->current_vs_state != sctx->last_vs_state) {
504 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
505
506 radeon_set_sh_reg(cs,
507 sctx->shader_userdata.sh_base[PIPE_SHADER_VERTEX] +
508 SI_SGPR_VS_STATE_BITS * 4,
509 sctx->current_vs_state);
510
511 sctx->last_vs_state = sctx->current_vs_state;
512 }
513 }
514
515 static void si_emit_draw_registers(struct si_context *sctx,
516 const struct pipe_draw_info *info,
517 unsigned num_patches)
518 {
519 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
520 unsigned prim = si_conv_pipe_prim(info->mode);
521 unsigned gs_out_prim = si_conv_prim_to_gs_out(sctx->current_rast_prim);
522 unsigned ia_multi_vgt_param;
523
524 ia_multi_vgt_param = si_get_ia_multi_vgt_param(sctx, info, num_patches);
525
526 /* Draw state. */
527 if (ia_multi_vgt_param != sctx->last_multi_vgt_param) {
528 if (sctx->b.chip_class >= GFX9)
529 radeon_set_uconfig_reg_idx(cs, R_030960_IA_MULTI_VGT_PARAM, 4, ia_multi_vgt_param);
530 else if (sctx->b.chip_class >= CIK)
531 radeon_set_context_reg_idx(cs, R_028AA8_IA_MULTI_VGT_PARAM, 1, ia_multi_vgt_param);
532 else
533 radeon_set_context_reg(cs, R_028AA8_IA_MULTI_VGT_PARAM, ia_multi_vgt_param);
534
535 sctx->last_multi_vgt_param = ia_multi_vgt_param;
536 }
537 if (prim != sctx->last_prim) {
538 if (sctx->b.chip_class >= CIK)
539 radeon_set_uconfig_reg_idx(cs, R_030908_VGT_PRIMITIVE_TYPE, 1, prim);
540 else
541 radeon_set_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE, prim);
542
543 sctx->last_prim = prim;
544 }
545
546 if (gs_out_prim != sctx->last_gs_out_prim) {
547 radeon_set_context_reg(cs, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out_prim);
548 sctx->last_gs_out_prim = gs_out_prim;
549 }
550
551 /* Primitive restart. */
552 if (info->primitive_restart != sctx->last_primitive_restart_en) {
553 if (sctx->b.chip_class >= GFX9)
554 radeon_set_uconfig_reg(cs, R_03092C_VGT_MULTI_PRIM_IB_RESET_EN,
555 info->primitive_restart);
556 else
557 radeon_set_context_reg(cs, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN,
558 info->primitive_restart);
559
560 sctx->last_primitive_restart_en = info->primitive_restart;
561
562 }
563 if (info->primitive_restart &&
564 (info->restart_index != sctx->last_restart_index ||
565 sctx->last_restart_index == SI_RESTART_INDEX_UNKNOWN)) {
566 radeon_set_context_reg(cs, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX,
567 info->restart_index);
568 sctx->last_restart_index = info->restart_index;
569 }
570 }
571
572 static void si_emit_draw_packets(struct si_context *sctx,
573 const struct pipe_draw_info *info,
574 const struct pipe_index_buffer *ib)
575 {
576 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
577 unsigned sh_base_reg = sctx->shader_userdata.sh_base[PIPE_SHADER_VERTEX];
578 bool render_cond_bit = sctx->b.render_cond && !sctx->b.render_cond_force_off;
579 uint32_t index_max_size = 0;
580 uint64_t index_va = 0;
581
582 if (info->count_from_stream_output) {
583 struct r600_so_target *t =
584 (struct r600_so_target*)info->count_from_stream_output;
585 uint64_t va = t->buf_filled_size->gpu_address +
586 t->buf_filled_size_offset;
587
588 radeon_set_context_reg(cs, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE,
589 t->stride_in_dw);
590
591 radeon_emit(cs, PKT3(PKT3_COPY_DATA, 4, 0));
592 radeon_emit(cs, COPY_DATA_SRC_SEL(COPY_DATA_MEM) |
593 COPY_DATA_DST_SEL(COPY_DATA_REG) |
594 COPY_DATA_WR_CONFIRM);
595 radeon_emit(cs, va); /* src address lo */
596 radeon_emit(cs, va >> 32); /* src address hi */
597 radeon_emit(cs, R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE >> 2);
598 radeon_emit(cs, 0); /* unused */
599
600 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
601 t->buf_filled_size, RADEON_USAGE_READ,
602 RADEON_PRIO_SO_FILLED_SIZE);
603 }
604
605 /* draw packet */
606 if (info->indexed) {
607 if (ib->index_size != sctx->last_index_size) {
608 unsigned index_type;
609
610 /* index type */
611 switch (ib->index_size) {
612 case 1:
613 index_type = V_028A7C_VGT_INDEX_8;
614 break;
615 case 2:
616 index_type = V_028A7C_VGT_INDEX_16 |
617 (SI_BIG_ENDIAN && sctx->b.chip_class <= CIK ?
618 V_028A7C_VGT_DMA_SWAP_16_BIT : 0);
619 break;
620 case 4:
621 index_type = V_028A7C_VGT_INDEX_32 |
622 (SI_BIG_ENDIAN && sctx->b.chip_class <= CIK ?
623 V_028A7C_VGT_DMA_SWAP_32_BIT : 0);
624 break;
625 default:
626 assert(!"unreachable");
627 return;
628 }
629
630 if (sctx->b.chip_class >= GFX9) {
631 radeon_set_uconfig_reg_idx(cs, R_03090C_VGT_INDEX_TYPE,
632 2, index_type);
633 } else {
634 radeon_emit(cs, PKT3(PKT3_INDEX_TYPE, 0, 0));
635 radeon_emit(cs, index_type);
636 }
637
638 sctx->last_index_size = ib->index_size;
639 }
640
641 index_max_size = (ib->buffer->width0 - ib->offset) /
642 ib->index_size;
643 index_va = r600_resource(ib->buffer)->gpu_address + ib->offset;
644
645 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
646 (struct r600_resource *)ib->buffer,
647 RADEON_USAGE_READ, RADEON_PRIO_INDEX_BUFFER);
648 } else {
649 /* On CI and later, non-indexed draws overwrite VGT_INDEX_TYPE,
650 * so the state must be re-emitted before the next indexed draw.
651 */
652 if (sctx->b.chip_class >= CIK)
653 sctx->last_index_size = -1;
654 }
655
656 if (info->indirect) {
657 uint64_t indirect_va = r600_resource(info->indirect)->gpu_address;
658
659 assert(indirect_va % 8 == 0);
660
661 si_invalidate_draw_sh_constants(sctx);
662
663 radeon_emit(cs, PKT3(PKT3_SET_BASE, 2, 0));
664 radeon_emit(cs, 1);
665 radeon_emit(cs, indirect_va);
666 radeon_emit(cs, indirect_va >> 32);
667
668 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
669 (struct r600_resource *)info->indirect,
670 RADEON_USAGE_READ, RADEON_PRIO_DRAW_INDIRECT);
671
672 unsigned di_src_sel = info->indexed ? V_0287F0_DI_SRC_SEL_DMA
673 : V_0287F0_DI_SRC_SEL_AUTO_INDEX;
674
675 assert(info->indirect_offset % 4 == 0);
676
677 if (info->indexed) {
678 radeon_emit(cs, PKT3(PKT3_INDEX_BASE, 1, 0));
679 radeon_emit(cs, index_va);
680 radeon_emit(cs, index_va >> 32);
681
682 radeon_emit(cs, PKT3(PKT3_INDEX_BUFFER_SIZE, 0, 0));
683 radeon_emit(cs, index_max_size);
684 }
685
686 if (!sctx->screen->has_draw_indirect_multi) {
687 radeon_emit(cs, PKT3(info->indexed ? PKT3_DRAW_INDEX_INDIRECT
688 : PKT3_DRAW_INDIRECT,
689 3, render_cond_bit));
690 radeon_emit(cs, info->indirect_offset);
691 radeon_emit(cs, (sh_base_reg + SI_SGPR_BASE_VERTEX * 4 - SI_SH_REG_OFFSET) >> 2);
692 radeon_emit(cs, (sh_base_reg + SI_SGPR_START_INSTANCE * 4 - SI_SH_REG_OFFSET) >> 2);
693 radeon_emit(cs, di_src_sel);
694 } else {
695 uint64_t count_va = 0;
696
697 if (info->indirect_params) {
698 struct r600_resource *params_buf =
699 (struct r600_resource *)info->indirect_params;
700
701 radeon_add_to_buffer_list(
702 &sctx->b, &sctx->b.gfx, params_buf,
703 RADEON_USAGE_READ, RADEON_PRIO_DRAW_INDIRECT);
704
705 count_va = params_buf->gpu_address + info->indirect_params_offset;
706 }
707
708 radeon_emit(cs, PKT3(info->indexed ? PKT3_DRAW_INDEX_INDIRECT_MULTI :
709 PKT3_DRAW_INDIRECT_MULTI,
710 8, render_cond_bit));
711 radeon_emit(cs, info->indirect_offset);
712 radeon_emit(cs, (sh_base_reg + SI_SGPR_BASE_VERTEX * 4 - SI_SH_REG_OFFSET) >> 2);
713 radeon_emit(cs, (sh_base_reg + SI_SGPR_START_INSTANCE * 4 - SI_SH_REG_OFFSET) >> 2);
714 radeon_emit(cs, ((sh_base_reg + SI_SGPR_DRAWID * 4 - SI_SH_REG_OFFSET) >> 2) |
715 S_2C3_DRAW_INDEX_ENABLE(1) |
716 S_2C3_COUNT_INDIRECT_ENABLE(!!info->indirect_params));
717 radeon_emit(cs, info->indirect_count);
718 radeon_emit(cs, count_va);
719 radeon_emit(cs, count_va >> 32);
720 radeon_emit(cs, info->indirect_stride);
721 radeon_emit(cs, di_src_sel);
722 }
723 } else {
724 int base_vertex;
725
726 radeon_emit(cs, PKT3(PKT3_NUM_INSTANCES, 0, 0));
727 radeon_emit(cs, info->instance_count);
728
729 /* Base vertex and start instance. */
730 base_vertex = info->indexed ? info->index_bias : info->start;
731
732 if (base_vertex != sctx->last_base_vertex ||
733 sctx->last_base_vertex == SI_BASE_VERTEX_UNKNOWN ||
734 info->start_instance != sctx->last_start_instance ||
735 info->drawid != sctx->last_drawid ||
736 sh_base_reg != sctx->last_sh_base_reg) {
737 radeon_set_sh_reg_seq(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, 3);
738 radeon_emit(cs, base_vertex);
739 radeon_emit(cs, info->start_instance);
740 radeon_emit(cs, info->drawid);
741
742 sctx->last_base_vertex = base_vertex;
743 sctx->last_start_instance = info->start_instance;
744 sctx->last_drawid = info->drawid;
745 sctx->last_sh_base_reg = sh_base_reg;
746 }
747
748 if (info->indexed) {
749 index_va += info->start * ib->index_size;
750
751 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_2, 4, render_cond_bit));
752 radeon_emit(cs, index_max_size);
753 radeon_emit(cs, index_va);
754 radeon_emit(cs, index_va >> 32);
755 radeon_emit(cs, info->count);
756 radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA);
757 } else {
758 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_AUTO, 1, render_cond_bit));
759 radeon_emit(cs, info->count);
760 radeon_emit(cs, V_0287F0_DI_SRC_SEL_AUTO_INDEX |
761 S_0287F0_USE_OPAQUE(!!info->count_from_stream_output));
762 }
763 }
764 }
765
766 static void si_emit_surface_sync(struct r600_common_context *rctx,
767 unsigned cp_coher_cntl)
768 {
769 struct radeon_winsys_cs *cs = rctx->gfx.cs;
770
771 if (rctx->chip_class >= GFX9) {
772 /* Flush caches and wait for the caches to assert idle. */
773 radeon_emit(cs, PKT3(PKT3_ACQUIRE_MEM, 5, 0));
774 radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
775 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
776 radeon_emit(cs, 0xffffff); /* CP_COHER_SIZE_HI */
777 radeon_emit(cs, 0); /* CP_COHER_BASE */
778 radeon_emit(cs, 0); /* CP_COHER_BASE_HI */
779 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
780 } else {
781 /* ACQUIRE_MEM is only required on a compute ring. */
782 radeon_emit(cs, PKT3(PKT3_SURFACE_SYNC, 3, 0));
783 radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
784 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
785 radeon_emit(cs, 0); /* CP_COHER_BASE */
786 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
787 }
788 }
789
790 void si_emit_cache_flush(struct si_context *sctx)
791 {
792 struct r600_common_context *rctx = &sctx->b;
793 struct radeon_winsys_cs *cs = rctx->gfx.cs;
794 uint32_t cp_coher_cntl = 0;
795 uint32_t flush_cb_db = rctx->flags & (SI_CONTEXT_FLUSH_AND_INV_CB |
796 SI_CONTEXT_FLUSH_AND_INV_DB);
797
798 if (rctx->flags & (SI_CONTEXT_FLUSH_AND_INV_CB |
799 SI_CONTEXT_FLUSH_AND_INV_DB))
800 sctx->b.num_fb_cache_flushes++;
801
802 /* SI has a bug that it always flushes ICACHE and KCACHE if either
803 * bit is set. An alternative way is to write SQC_CACHES, but that
804 * doesn't seem to work reliably. Since the bug doesn't affect
805 * correctness (it only does more work than necessary) and
806 * the performance impact is likely negligible, there is no plan
807 * to add a workaround for it.
808 */
809
810 if (rctx->flags & SI_CONTEXT_INV_ICACHE)
811 cp_coher_cntl |= S_0085F0_SH_ICACHE_ACTION_ENA(1);
812 if (rctx->flags & SI_CONTEXT_INV_SMEM_L1)
813 cp_coher_cntl |= S_0085F0_SH_KCACHE_ACTION_ENA(1);
814
815 if (rctx->chip_class <= VI) {
816 if (rctx->flags & SI_CONTEXT_FLUSH_AND_INV_CB) {
817 cp_coher_cntl |= S_0085F0_CB_ACTION_ENA(1) |
818 S_0085F0_CB0_DEST_BASE_ENA(1) |
819 S_0085F0_CB1_DEST_BASE_ENA(1) |
820 S_0085F0_CB2_DEST_BASE_ENA(1) |
821 S_0085F0_CB3_DEST_BASE_ENA(1) |
822 S_0085F0_CB4_DEST_BASE_ENA(1) |
823 S_0085F0_CB5_DEST_BASE_ENA(1) |
824 S_0085F0_CB6_DEST_BASE_ENA(1) |
825 S_0085F0_CB7_DEST_BASE_ENA(1);
826
827 /* Necessary for DCC */
828 if (rctx->chip_class == VI)
829 r600_gfx_write_event_eop(rctx, V_028A90_FLUSH_AND_INV_CB_DATA_TS,
830 0, 0, NULL, 0, 0, 0);
831 }
832 if (rctx->flags & SI_CONTEXT_FLUSH_AND_INV_DB)
833 cp_coher_cntl |= S_0085F0_DB_ACTION_ENA(1) |
834 S_0085F0_DB_DEST_BASE_ENA(1);
835 }
836
837 if (rctx->flags & SI_CONTEXT_FLUSH_AND_INV_CB) {
838 /* Flush CMASK/FMASK/DCC. SURFACE_SYNC will wait for idle. */
839 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
840 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_CB_META) | EVENT_INDEX(0));
841 }
842 if (rctx->flags & SI_CONTEXT_FLUSH_AND_INV_DB) {
843 /* Flush HTILE. SURFACE_SYNC will wait for idle. */
844 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
845 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_DB_META) | EVENT_INDEX(0));
846 }
847
848 /* Wait for shader engines to go idle.
849 * VS and PS waits are unnecessary if SURFACE_SYNC is going to wait
850 * for everything including CB/DB cache flushes.
851 */
852 if (!flush_cb_db) {
853 if (rctx->flags & SI_CONTEXT_PS_PARTIAL_FLUSH) {
854 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
855 radeon_emit(cs, EVENT_TYPE(V_028A90_PS_PARTIAL_FLUSH) | EVENT_INDEX(4));
856 /* Only count explicit shader flushes, not implicit ones
857 * done by SURFACE_SYNC.
858 */
859 rctx->num_vs_flushes++;
860 rctx->num_ps_flushes++;
861 } else if (rctx->flags & SI_CONTEXT_VS_PARTIAL_FLUSH) {
862 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
863 radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
864 rctx->num_vs_flushes++;
865 }
866 }
867
868 if (rctx->flags & SI_CONTEXT_CS_PARTIAL_FLUSH &&
869 sctx->compute_is_busy) {
870 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
871 radeon_emit(cs, EVENT_TYPE(V_028A90_CS_PARTIAL_FLUSH | EVENT_INDEX(4)));
872 rctx->num_cs_flushes++;
873 sctx->compute_is_busy = false;
874 }
875
876 /* VGT state synchronization. */
877 if (rctx->flags & SI_CONTEXT_VGT_FLUSH) {
878 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
879 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
880 }
881 if (rctx->flags & SI_CONTEXT_VGT_STREAMOUT_SYNC) {
882 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
883 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_STREAMOUT_SYNC) | EVENT_INDEX(0));
884 }
885
886 /* GFX9: Wait for idle if we're flushing CB or DB. ACQUIRE_MEM doesn't
887 * wait for idle on GFX9. We have to use a TS event.
888 */
889 if (sctx->b.chip_class >= GFX9 && flush_cb_db) {
890 struct r600_resource *rbuf = NULL;
891 uint64_t va;
892 unsigned offset = 0, tc_flags, cb_db_event;
893
894 /* Set the CB/DB flush event. */
895 switch (flush_cb_db) {
896 case SI_CONTEXT_FLUSH_AND_INV_CB:
897 cb_db_event = V_028A90_FLUSH_AND_INV_CB_DATA_TS;
898 break;
899 case SI_CONTEXT_FLUSH_AND_INV_DB:
900 cb_db_event = V_028A90_FLUSH_AND_INV_DB_DATA_TS;
901 break;
902 default:
903 /* both CB & DB */
904 cb_db_event = V_028A90_CACHE_FLUSH_AND_INV_TS_EVENT;
905 }
906
907 /* TC | TC_WB = invalidate L2 data
908 * TC_MD | TC_WB = invalidate L2 metadata
909 * TC | TC_WB | TC_MD = invalidate L2 data & metadata
910 *
911 * The metadata cache must always be invalidated for coherency
912 * between CB/DB and shaders. (metadata = HTILE, CMASK, DCC)
913 *
914 * TC must be invalidated on GFX9 only if the CB/DB surface is
915 * not pipe-aligned. If the surface is RB-aligned, it might not
916 * strictly be pipe-aligned since RB alignment takes precendence.
917 */
918 tc_flags = EVENT_TC_WB_ACTION_ENA |
919 EVENT_TC_MD_ACTION_ENA;
920
921 /* Ideally flush TC together with CB/DB. */
922 if (rctx->flags & SI_CONTEXT_INV_GLOBAL_L2) {
923 tc_flags |= EVENT_TC_ACTION_ENA |
924 EVENT_TCL1_ACTION_ENA;
925
926 /* Clear the flags. */
927 rctx->flags &= ~(SI_CONTEXT_INV_GLOBAL_L2 |
928 SI_CONTEXT_WRITEBACK_GLOBAL_L2 |
929 SI_CONTEXT_INV_VMEM_L1);
930 }
931
932 /* Allocate memory for the fence. */
933 u_suballocator_alloc(rctx->allocator_zeroed_memory, 4, 4,
934 &offset, (struct pipe_resource**)&rbuf);
935 va = rbuf->gpu_address + offset;
936
937 r600_gfx_write_event_eop(rctx, cb_db_event, tc_flags, 1,
938 rbuf, va, 0, 1);
939 r600_gfx_wait_fence(rctx, va, 1, 0xffffffff);
940 }
941
942 /* Make sure ME is idle (it executes most packets) before continuing.
943 * This prevents read-after-write hazards between PFP and ME.
944 */
945 if (cp_coher_cntl ||
946 (rctx->flags & (SI_CONTEXT_CS_PARTIAL_FLUSH |
947 SI_CONTEXT_INV_VMEM_L1 |
948 SI_CONTEXT_INV_GLOBAL_L2 |
949 SI_CONTEXT_WRITEBACK_GLOBAL_L2))) {
950 radeon_emit(cs, PKT3(PKT3_PFP_SYNC_ME, 0, 0));
951 radeon_emit(cs, 0);
952 }
953
954 /* SI-CI-VI only:
955 * When one of the CP_COHER_CNTL.DEST_BASE flags is set, SURFACE_SYNC
956 * waits for idle, so it should be last. SURFACE_SYNC is done in PFP.
957 *
958 * cp_coher_cntl should contain all necessary flags except TC flags
959 * at this point.
960 *
961 * SI-CIK don't support L2 write-back.
962 */
963 if (rctx->flags & SI_CONTEXT_INV_GLOBAL_L2 ||
964 (rctx->chip_class <= CIK &&
965 (rctx->flags & SI_CONTEXT_WRITEBACK_GLOBAL_L2))) {
966 /* Invalidate L1 & L2. (L1 is always invalidated on SI)
967 * WB must be set on VI+ when TC_ACTION is set.
968 */
969 si_emit_surface_sync(rctx, cp_coher_cntl |
970 S_0085F0_TC_ACTION_ENA(1) |
971 S_0085F0_TCL1_ACTION_ENA(1) |
972 S_0301F0_TC_WB_ACTION_ENA(rctx->chip_class >= VI));
973 cp_coher_cntl = 0;
974 sctx->b.num_L2_invalidates++;
975 } else {
976 /* L1 invalidation and L2 writeback must be done separately,
977 * because both operations can't be done together.
978 */
979 if (rctx->flags & SI_CONTEXT_WRITEBACK_GLOBAL_L2) {
980 /* WB = write-back
981 * NC = apply to non-coherent MTYPEs
982 * (i.e. MTYPE <= 1, which is what we use everywhere)
983 *
984 * WB doesn't work without NC.
985 */
986 si_emit_surface_sync(rctx, cp_coher_cntl |
987 S_0301F0_TC_WB_ACTION_ENA(1) |
988 S_0301F0_TC_NC_ACTION_ENA(1));
989 cp_coher_cntl = 0;
990 sctx->b.num_L2_writebacks++;
991 }
992 if (rctx->flags & SI_CONTEXT_INV_VMEM_L1) {
993 /* Invalidate per-CU VMEM L1. */
994 si_emit_surface_sync(rctx, cp_coher_cntl |
995 S_0085F0_TCL1_ACTION_ENA(1));
996 cp_coher_cntl = 0;
997 }
998 }
999
1000 /* If TC flushes haven't cleared this... */
1001 if (cp_coher_cntl)
1002 si_emit_surface_sync(rctx, cp_coher_cntl);
1003
1004 if (rctx->flags & R600_CONTEXT_START_PIPELINE_STATS) {
1005 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1006 radeon_emit(cs, EVENT_TYPE(V_028A90_PIPELINESTAT_START) |
1007 EVENT_INDEX(0));
1008 } else if (rctx->flags & R600_CONTEXT_STOP_PIPELINE_STATS) {
1009 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1010 radeon_emit(cs, EVENT_TYPE(V_028A90_PIPELINESTAT_STOP) |
1011 EVENT_INDEX(0));
1012 }
1013
1014 rctx->flags = 0;
1015 }
1016
1017 static void si_get_draw_start_count(struct si_context *sctx,
1018 const struct pipe_draw_info *info,
1019 unsigned *start, unsigned *count)
1020 {
1021 if (info->indirect) {
1022 unsigned indirect_count;
1023 struct pipe_transfer *transfer;
1024 unsigned begin, end;
1025 unsigned map_size;
1026 unsigned *data;
1027
1028 if (info->indirect_params) {
1029 data = pipe_buffer_map_range(&sctx->b.b,
1030 info->indirect_params,
1031 info->indirect_params_offset,
1032 sizeof(unsigned),
1033 PIPE_TRANSFER_READ, &transfer);
1034
1035 indirect_count = *data;
1036
1037 pipe_buffer_unmap(&sctx->b.b, transfer);
1038 } else {
1039 indirect_count = info->indirect_count;
1040 }
1041
1042 if (!indirect_count) {
1043 *start = *count = 0;
1044 return;
1045 }
1046
1047 map_size = (indirect_count - 1) * info->indirect_stride + 3 * sizeof(unsigned);
1048 data = pipe_buffer_map_range(&sctx->b.b, info->indirect,
1049 info->indirect_offset, map_size,
1050 PIPE_TRANSFER_READ, &transfer);
1051
1052 begin = UINT_MAX;
1053 end = 0;
1054
1055 for (unsigned i = 0; i < indirect_count; ++i) {
1056 unsigned count = data[0];
1057 unsigned start = data[2];
1058
1059 if (count > 0) {
1060 begin = MIN2(begin, start);
1061 end = MAX2(end, start + count);
1062 }
1063
1064 data += info->indirect_stride / sizeof(unsigned);
1065 }
1066
1067 pipe_buffer_unmap(&sctx->b.b, transfer);
1068
1069 if (begin < end) {
1070 *start = begin;
1071 *count = end - begin;
1072 } else {
1073 *start = *count = 0;
1074 }
1075 } else {
1076 *start = info->start;
1077 *count = info->count;
1078 }
1079 }
1080
1081 void si_ce_pre_draw_synchronization(struct si_context *sctx)
1082 {
1083 if (sctx->ce_need_synchronization) {
1084 radeon_emit(sctx->ce_ib, PKT3(PKT3_INCREMENT_CE_COUNTER, 0, 0));
1085 radeon_emit(sctx->ce_ib, 1);
1086
1087 radeon_emit(sctx->b.gfx.cs, PKT3(PKT3_WAIT_ON_CE_COUNTER, 0, 0));
1088 radeon_emit(sctx->b.gfx.cs, 1);
1089 }
1090 }
1091
1092 void si_ce_post_draw_synchronization(struct si_context *sctx)
1093 {
1094 if (sctx->ce_need_synchronization) {
1095 radeon_emit(sctx->b.gfx.cs, PKT3(PKT3_INCREMENT_DE_COUNTER, 0, 0));
1096 radeon_emit(sctx->b.gfx.cs, 0);
1097
1098 sctx->ce_need_synchronization = false;
1099 }
1100 }
1101
1102 void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
1103 {
1104 struct si_context *sctx = (struct si_context *)ctx;
1105 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1106 const struct pipe_index_buffer *ib = &sctx->index_buffer;
1107 struct pipe_index_buffer ib_tmp; /* for index buffer uploads only */
1108 unsigned mask, dirty_tex_counter;
1109 enum pipe_prim_type rast_prim;
1110 unsigned num_patches = 0;
1111
1112 if (likely(!info->indirect)) {
1113 /* SI-CI treat instance_count==0 as instance_count==1. There is
1114 * no workaround for indirect draws, but we can at least skip
1115 * direct draws.
1116 */
1117 if (unlikely(!info->instance_count))
1118 return;
1119
1120 /* Handle count == 0. */
1121 if (unlikely(!info->count &&
1122 (info->indexed || !info->count_from_stream_output)))
1123 return;
1124 }
1125
1126 if (unlikely(!sctx->vs_shader.cso)) {
1127 assert(0);
1128 return;
1129 }
1130 if (unlikely(!sctx->ps_shader.cso && (!rs || !rs->rasterizer_discard))) {
1131 assert(0);
1132 return;
1133 }
1134 if (unlikely(!!sctx->tes_shader.cso != (info->mode == PIPE_PRIM_PATCHES))) {
1135 assert(0);
1136 return;
1137 }
1138
1139 /* Recompute and re-emit the texture resource states if needed. */
1140 dirty_tex_counter = p_atomic_read(&sctx->b.screen->dirty_tex_counter);
1141 if (unlikely(dirty_tex_counter != sctx->b.last_dirty_tex_counter)) {
1142 sctx->b.last_dirty_tex_counter = dirty_tex_counter;
1143 sctx->framebuffer.dirty_cbufs |=
1144 ((1 << sctx->framebuffer.state.nr_cbufs) - 1);
1145 sctx->framebuffer.dirty_zsbuf = true;
1146 sctx->framebuffer.do_update_surf_dirtiness = true;
1147 si_mark_atom_dirty(sctx, &sctx->framebuffer.atom);
1148 si_update_all_texture_descriptors(sctx);
1149 }
1150
1151 si_decompress_graphics_textures(sctx);
1152
1153 /* Set the rasterization primitive type.
1154 *
1155 * This must be done after si_decompress_textures, which can call
1156 * draw_vbo recursively, and before si_update_shaders, which uses
1157 * current_rast_prim for this draw_vbo call. */
1158 if (sctx->gs_shader.cso)
1159 rast_prim = sctx->gs_shader.cso->gs_output_prim;
1160 else if (sctx->tes_shader.cso)
1161 rast_prim = sctx->tes_shader.cso->info.properties[TGSI_PROPERTY_TES_PRIM_MODE];
1162 else
1163 rast_prim = info->mode;
1164
1165 if (rast_prim != sctx->current_rast_prim) {
1166 sctx->current_rast_prim = rast_prim;
1167 sctx->do_update_shaders = true;
1168 }
1169
1170 if (sctx->gs_shader.cso) {
1171 /* Determine whether the GS triangle strip adjacency fix should
1172 * be applied. Rotate every other triangle if
1173 * - triangle strips with adjacency are fed to the GS and
1174 * - primitive restart is disabled (the rotation doesn't help
1175 * when the restart occurs after an odd number of triangles).
1176 */
1177 bool gs_tri_strip_adj_fix =
1178 !sctx->tes_shader.cso &&
1179 info->mode == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY &&
1180 !info->primitive_restart;
1181
1182 if (gs_tri_strip_adj_fix != sctx->gs_tri_strip_adj_fix) {
1183 sctx->gs_tri_strip_adj_fix = gs_tri_strip_adj_fix;
1184 sctx->do_update_shaders = true;
1185 }
1186 }
1187
1188 if (sctx->do_update_shaders && !si_update_shaders(sctx))
1189 return;
1190
1191 if (!si_upload_graphics_shader_descriptors(sctx))
1192 return;
1193
1194 ib_tmp.buffer = NULL;
1195
1196 if (info->indexed) {
1197 /* Translate or upload, if needed. */
1198 /* 8-bit indices are supported on VI. */
1199 if (sctx->b.chip_class <= CIK && ib->index_size == 1) {
1200 unsigned start, count, start_offset, size;
1201 void *ptr;
1202
1203 si_get_draw_start_count(sctx, info, &start, &count);
1204 start_offset = start * 2;
1205 size = count * 2;
1206
1207 u_upload_alloc(ctx->stream_uploader, start_offset,
1208 size,
1209 si_optimal_tcc_alignment(sctx, size),
1210 &ib_tmp.offset, &ib_tmp.buffer, &ptr);
1211 if (!ib_tmp.buffer)
1212 return;
1213
1214 util_shorten_ubyte_elts_to_userptr(&sctx->b.b, ib, 0, 0,
1215 ib->offset + start,
1216 count, ptr);
1217
1218 /* info->start will be added by the drawing code */
1219 ib_tmp.offset -= start_offset;
1220 ib_tmp.index_size = 2;
1221 ib = &ib_tmp;
1222 } else if (ib->user_buffer && !ib->buffer) {
1223 unsigned start_offset;
1224
1225 assert(!info->indirect);
1226 start_offset = info->start * ib->index_size;
1227
1228 u_upload_data(ctx->stream_uploader, start_offset,
1229 info->count * ib->index_size,
1230 sctx->screen->b.info.tcc_cache_line_size,
1231 (char*)ib->user_buffer + start_offset,
1232 &ib_tmp.offset, &ib_tmp.buffer);
1233 if (!ib_tmp.buffer)
1234 return;
1235
1236 /* info->start will be added by the drawing code */
1237 ib_tmp.offset -= start_offset;
1238 ib_tmp.index_size = ib->index_size;
1239 ib = &ib_tmp;
1240 } else if (sctx->b.chip_class <= CIK &&
1241 r600_resource(ib->buffer)->TC_L2_dirty) {
1242 /* VI reads index buffers through TC L2, so it doesn't
1243 * need this. */
1244 sctx->b.flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
1245 r600_resource(ib->buffer)->TC_L2_dirty = false;
1246 }
1247 }
1248
1249 if (info->indirect) {
1250 /* Add the buffer size for memory checking in need_cs_space. */
1251 r600_context_add_resource_size(ctx, info->indirect);
1252
1253 if (r600_resource(info->indirect)->TC_L2_dirty) {
1254 sctx->b.flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
1255 r600_resource(info->indirect)->TC_L2_dirty = false;
1256 }
1257
1258 if (info->indirect_params &&
1259 r600_resource(info->indirect_params)->TC_L2_dirty) {
1260 sctx->b.flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
1261 r600_resource(info->indirect_params)->TC_L2_dirty = false;
1262 }
1263 }
1264
1265 si_need_cs_space(sctx);
1266
1267 /* Since we've called r600_context_add_resource_size for vertex buffers,
1268 * this must be called after si_need_cs_space, because we must let
1269 * need_cs_space flush before we add buffers to the buffer list.
1270 */
1271 if (!si_upload_vertex_buffer_descriptors(sctx))
1272 return;
1273
1274 /* GFX9 scissor bug workaround. There is also a more efficient but
1275 * more involved alternative workaround. */
1276 if (sctx->b.chip_class == GFX9 &&
1277 si_is_atom_dirty(sctx, &sctx->b.scissors.atom))
1278 sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH;
1279
1280 /* Flush caches before the first state atom, which does L2 prefetches. */
1281 if (sctx->b.flags)
1282 si_emit_cache_flush(sctx);
1283
1284 /* Emit state atoms. */
1285 mask = sctx->dirty_atoms;
1286 while (mask) {
1287 struct r600_atom *atom = sctx->atoms.array[u_bit_scan(&mask)];
1288
1289 atom->emit(&sctx->b, atom);
1290 }
1291 sctx->dirty_atoms = 0;
1292
1293 /* Emit states. */
1294 mask = sctx->dirty_states;
1295 while (mask) {
1296 unsigned i = u_bit_scan(&mask);
1297 struct si_pm4_state *state = sctx->queued.array[i];
1298
1299 if (!state || sctx->emitted.array[i] == state)
1300 continue;
1301
1302 si_pm4_emit(sctx, state);
1303 sctx->emitted.array[i] = state;
1304 }
1305 sctx->dirty_states = 0;
1306
1307 si_emit_rasterizer_prim_state(sctx);
1308 if (sctx->tes_shader.cso)
1309 si_emit_derived_tess_state(sctx, info, &num_patches);
1310 si_emit_vs_state(sctx, info);
1311 si_emit_draw_registers(sctx, info, num_patches);
1312
1313 si_ce_pre_draw_synchronization(sctx);
1314 si_emit_draw_packets(sctx, info, ib);
1315 si_ce_post_draw_synchronization(sctx);
1316
1317 if (sctx->trace_buf)
1318 si_trace_emit(sctx);
1319
1320 /* Workaround for a VGT hang when streamout is enabled.
1321 * It must be done after drawing. */
1322 if ((sctx->b.family == CHIP_HAWAII ||
1323 sctx->b.family == CHIP_TONGA ||
1324 sctx->b.family == CHIP_FIJI) &&
1325 r600_get_strmout_en(&sctx->b)) {
1326 sctx->b.flags |= SI_CONTEXT_VGT_STREAMOUT_SYNC;
1327 }
1328
1329 if (sctx->framebuffer.do_update_surf_dirtiness) {
1330 /* Set the depth buffer as dirty. */
1331 if (sctx->framebuffer.state.zsbuf) {
1332 struct pipe_surface *surf = sctx->framebuffer.state.zsbuf;
1333 struct r600_texture *rtex = (struct r600_texture *)surf->texture;
1334
1335 if (!rtex->tc_compatible_htile)
1336 rtex->dirty_level_mask |= 1 << surf->u.tex.level;
1337
1338 if (rtex->surface.flags & RADEON_SURF_SBUFFER)
1339 rtex->stencil_dirty_level_mask |= 1 << surf->u.tex.level;
1340 }
1341 if (sctx->framebuffer.compressed_cb_mask) {
1342 struct pipe_surface *surf;
1343 struct r600_texture *rtex;
1344 unsigned mask = sctx->framebuffer.compressed_cb_mask;
1345
1346 do {
1347 unsigned i = u_bit_scan(&mask);
1348 surf = sctx->framebuffer.state.cbufs[i];
1349 rtex = (struct r600_texture*)surf->texture;
1350
1351 if (rtex->fmask.size)
1352 rtex->dirty_level_mask |= 1 << surf->u.tex.level;
1353 if (rtex->dcc_gather_statistics)
1354 rtex->separate_dcc_dirty = true;
1355 } while (mask);
1356 }
1357 sctx->framebuffer.do_update_surf_dirtiness = false;
1358 }
1359
1360 pipe_resource_reference(&ib_tmp.buffer, NULL);
1361 sctx->b.num_draw_calls++;
1362 if (G_0286E8_WAVESIZE(sctx->spi_tmpring_size))
1363 sctx->b.num_spill_draw_calls++;
1364 }
1365
1366 void si_trace_emit(struct si_context *sctx)
1367 {
1368 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
1369
1370 sctx->trace_id++;
1371 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, sctx->trace_buf,
1372 RADEON_USAGE_READWRITE, RADEON_PRIO_TRACE);
1373 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 3, 0));
1374 radeon_emit(cs, S_370_DST_SEL(V_370_MEMORY_SYNC) |
1375 S_370_WR_CONFIRM(1) |
1376 S_370_ENGINE_SEL(V_370_ME));
1377 radeon_emit(cs, sctx->trace_buf->gpu_address);
1378 radeon_emit(cs, sctx->trace_buf->gpu_address >> 32);
1379 radeon_emit(cs, sctx->trace_id);
1380 radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
1381 radeon_emit(cs, AC_ENCODE_TRACE_POINT(sctx->trace_id));
1382 }