f17e8b57a13e23b41d895e92d966cf92ce090087
[mesa.git] / src / gallium / drivers / radeonsi / si_state.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 "si_build_pm4.h"
26 #include "si_query.h"
27 #include "sid.h"
28 #include "util/fast_idiv_by_const.h"
29 #include "util/format/u_format.h"
30 #include "util/format/u_format_s3tc.h"
31 #include "util/u_dual_blend.h"
32 #include "util/u_memory.h"
33 #include "util/u_resource.h"
34 #include "util/u_upload_mgr.h"
35
36 struct gfx10_format {
37 unsigned img_format : 9;
38
39 /* Various formats are only supported with workarounds for vertex fetch,
40 * and some 32_32_32 formats are supported natively, but only for buffers
41 * (possibly with some image support, actually, but no filtering). */
42 bool buffers_only : 1;
43 };
44
45 #include "gfx10_format_table.h"
46
47 static unsigned si_map_swizzle(unsigned swizzle)
48 {
49 switch (swizzle) {
50 case PIPE_SWIZZLE_Y:
51 return V_008F0C_SQ_SEL_Y;
52 case PIPE_SWIZZLE_Z:
53 return V_008F0C_SQ_SEL_Z;
54 case PIPE_SWIZZLE_W:
55 return V_008F0C_SQ_SEL_W;
56 case PIPE_SWIZZLE_0:
57 return V_008F0C_SQ_SEL_0;
58 case PIPE_SWIZZLE_1:
59 return V_008F0C_SQ_SEL_1;
60 default: /* PIPE_SWIZZLE_X */
61 return V_008F0C_SQ_SEL_X;
62 }
63 }
64
65 /* 12.4 fixed-point */
66 static unsigned si_pack_float_12p4(float x)
67 {
68 return x <= 0 ? 0 : x >= 4096 ? 0xffff : x * 16;
69 }
70
71 /*
72 * Inferred framebuffer and blender state.
73 *
74 * CB_TARGET_MASK is emitted here to avoid a hang with dual source blending
75 * if there is not enough PS outputs.
76 */
77 static void si_emit_cb_render_state(struct si_context *sctx)
78 {
79 struct radeon_cmdbuf *cs = sctx->gfx_cs;
80 struct si_state_blend *blend = sctx->queued.named.blend;
81 /* CB_COLORn_INFO.FORMAT=INVALID should disable unbound colorbuffers,
82 * but you never know. */
83 uint32_t cb_target_mask = sctx->framebuffer.colorbuf_enabled_4bit & blend->cb_target_mask;
84 unsigned i;
85
86 /* Avoid a hang that happens when dual source blending is enabled
87 * but there is not enough color outputs. This is undefined behavior,
88 * so disable color writes completely.
89 *
90 * Reproducible with Unigine Heaven 4.0 and drirc missing.
91 */
92 if (blend->dual_src_blend && sctx->ps_shader.cso &&
93 (sctx->ps_shader.cso->info.colors_written & 0x3) != 0x3)
94 cb_target_mask = 0;
95
96 /* GFX9: Flush DFSM when CB_TARGET_MASK changes.
97 * I think we don't have to do anything between IBs.
98 */
99 if (sctx->screen->dpbb_allowed && sctx->last_cb_target_mask != cb_target_mask) {
100 sctx->last_cb_target_mask = cb_target_mask;
101
102 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
103 radeon_emit(cs, EVENT_TYPE(V_028A90_BREAK_BATCH) | EVENT_INDEX(0));
104 }
105
106 unsigned initial_cdw = cs->current.cdw;
107 radeon_opt_set_context_reg(sctx, R_028238_CB_TARGET_MASK, SI_TRACKED_CB_TARGET_MASK,
108 cb_target_mask);
109
110 if (sctx->chip_class >= GFX8) {
111 /* DCC MSAA workaround.
112 * Alternatively, we can set CB_COLORi_DCC_CONTROL.OVERWRITE_-
113 * COMBINER_DISABLE, but that would be more complicated.
114 */
115 bool oc_disable =
116 blend->dcc_msaa_corruption_4bit & cb_target_mask && sctx->framebuffer.nr_samples >= 2;
117 unsigned watermark = sctx->framebuffer.dcc_overwrite_combiner_watermark;
118
119 radeon_opt_set_context_reg(
120 sctx, R_028424_CB_DCC_CONTROL, SI_TRACKED_CB_DCC_CONTROL,
121 S_028424_OVERWRITE_COMBINER_MRT_SHARING_DISABLE(sctx->chip_class <= GFX9) |
122 S_028424_OVERWRITE_COMBINER_WATERMARK(watermark) |
123 S_028424_OVERWRITE_COMBINER_DISABLE(oc_disable) |
124 S_028424_DISABLE_CONSTANT_ENCODE_REG(sctx->screen->info.has_dcc_constant_encode));
125 }
126
127 /* RB+ register settings. */
128 if (sctx->screen->info.rbplus_allowed) {
129 unsigned spi_shader_col_format =
130 sctx->ps_shader.cso ? sctx->ps_shader.current->key.part.ps.epilog.spi_shader_col_format
131 : 0;
132 unsigned sx_ps_downconvert = 0;
133 unsigned sx_blend_opt_epsilon = 0;
134 unsigned sx_blend_opt_control = 0;
135
136 for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
137 struct si_surface *surf = (struct si_surface *)sctx->framebuffer.state.cbufs[i];
138 unsigned format, swap, spi_format, colormask;
139 bool has_alpha, has_rgb;
140
141 if (!surf) {
142 /* If the color buffer is not set, the driver sets 32_R
143 * as the SPI color format, because the hw doesn't allow
144 * holes between color outputs, so also set this to
145 * enable RB+.
146 */
147 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_R << (i * 4);
148 continue;
149 }
150
151 format = G_028C70_FORMAT(surf->cb_color_info);
152 swap = G_028C70_COMP_SWAP(surf->cb_color_info);
153 spi_format = (spi_shader_col_format >> (i * 4)) & 0xf;
154 colormask = (cb_target_mask >> (i * 4)) & 0xf;
155
156 /* Set if RGB and A are present. */
157 has_alpha = !G_028C74_FORCE_DST_ALPHA_1(surf->cb_color_attrib);
158
159 if (format == V_028C70_COLOR_8 || format == V_028C70_COLOR_16 ||
160 format == V_028C70_COLOR_32)
161 has_rgb = !has_alpha;
162 else
163 has_rgb = true;
164
165 /* Check the colormask and export format. */
166 if (!(colormask & (PIPE_MASK_RGBA & ~PIPE_MASK_A)))
167 has_rgb = false;
168 if (!(colormask & PIPE_MASK_A))
169 has_alpha = false;
170
171 if (spi_format == V_028714_SPI_SHADER_ZERO) {
172 has_rgb = false;
173 has_alpha = false;
174 }
175
176 /* Disable value checking for disabled channels. */
177 if (!has_rgb)
178 sx_blend_opt_control |= S_02875C_MRT0_COLOR_OPT_DISABLE(1) << (i * 4);
179 if (!has_alpha)
180 sx_blend_opt_control |= S_02875C_MRT0_ALPHA_OPT_DISABLE(1) << (i * 4);
181
182 /* Enable down-conversion for 32bpp and smaller formats. */
183 switch (format) {
184 case V_028C70_COLOR_8:
185 case V_028C70_COLOR_8_8:
186 case V_028C70_COLOR_8_8_8_8:
187 /* For 1 and 2-channel formats, use the superset thereof. */
188 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR ||
189 spi_format == V_028714_SPI_SHADER_UINT16_ABGR ||
190 spi_format == V_028714_SPI_SHADER_SINT16_ABGR) {
191 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_8_8_8_8 << (i * 4);
192 sx_blend_opt_epsilon |= V_028758_8BIT_FORMAT << (i * 4);
193 }
194 break;
195
196 case V_028C70_COLOR_5_6_5:
197 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
198 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_5_6_5 << (i * 4);
199 sx_blend_opt_epsilon |= V_028758_6BIT_FORMAT << (i * 4);
200 }
201 break;
202
203 case V_028C70_COLOR_1_5_5_5:
204 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
205 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_1_5_5_5 << (i * 4);
206 sx_blend_opt_epsilon |= V_028758_5BIT_FORMAT << (i * 4);
207 }
208 break;
209
210 case V_028C70_COLOR_4_4_4_4:
211 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
212 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_4_4_4_4 << (i * 4);
213 sx_blend_opt_epsilon |= V_028758_4BIT_FORMAT << (i * 4);
214 }
215 break;
216
217 case V_028C70_COLOR_32:
218 if (swap == V_028C70_SWAP_STD && spi_format == V_028714_SPI_SHADER_32_R)
219 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_R << (i * 4);
220 else if (swap == V_028C70_SWAP_ALT_REV && spi_format == V_028714_SPI_SHADER_32_AR)
221 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_A << (i * 4);
222 break;
223
224 case V_028C70_COLOR_16:
225 case V_028C70_COLOR_16_16:
226 /* For 1-channel formats, use the superset thereof. */
227 if (spi_format == V_028714_SPI_SHADER_UNORM16_ABGR ||
228 spi_format == V_028714_SPI_SHADER_SNORM16_ABGR ||
229 spi_format == V_028714_SPI_SHADER_UINT16_ABGR ||
230 spi_format == V_028714_SPI_SHADER_SINT16_ABGR) {
231 if (swap == V_028C70_SWAP_STD || swap == V_028C70_SWAP_STD_REV)
232 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_16_16_GR << (i * 4);
233 else
234 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_16_16_AR << (i * 4);
235 }
236 break;
237
238 case V_028C70_COLOR_10_11_11:
239 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR)
240 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_10_11_11 << (i * 4);
241 break;
242
243 case V_028C70_COLOR_2_10_10_10:
244 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
245 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_2_10_10_10 << (i * 4);
246 sx_blend_opt_epsilon |= V_028758_10BIT_FORMAT << (i * 4);
247 }
248 break;
249 }
250 }
251
252 /* If there are no color outputs, the first color export is
253 * always enabled as 32_R, so also set this to enable RB+.
254 */
255 if (!sx_ps_downconvert)
256 sx_ps_downconvert = V_028754_SX_RT_EXPORT_32_R;
257
258 /* SX_PS_DOWNCONVERT, SX_BLEND_OPT_EPSILON, SX_BLEND_OPT_CONTROL */
259 radeon_opt_set_context_reg3(sctx, R_028754_SX_PS_DOWNCONVERT, SI_TRACKED_SX_PS_DOWNCONVERT,
260 sx_ps_downconvert, sx_blend_opt_epsilon, sx_blend_opt_control);
261 }
262 if (initial_cdw != cs->current.cdw)
263 sctx->context_roll = true;
264 }
265
266 /*
267 * Blender functions
268 */
269
270 static uint32_t si_translate_blend_function(int blend_func)
271 {
272 switch (blend_func) {
273 case PIPE_BLEND_ADD:
274 return V_028780_COMB_DST_PLUS_SRC;
275 case PIPE_BLEND_SUBTRACT:
276 return V_028780_COMB_SRC_MINUS_DST;
277 case PIPE_BLEND_REVERSE_SUBTRACT:
278 return V_028780_COMB_DST_MINUS_SRC;
279 case PIPE_BLEND_MIN:
280 return V_028780_COMB_MIN_DST_SRC;
281 case PIPE_BLEND_MAX:
282 return V_028780_COMB_MAX_DST_SRC;
283 default:
284 PRINT_ERR("Unknown blend function %d\n", blend_func);
285 assert(0);
286 break;
287 }
288 return 0;
289 }
290
291 static uint32_t si_translate_blend_factor(int blend_fact)
292 {
293 switch (blend_fact) {
294 case PIPE_BLENDFACTOR_ONE:
295 return V_028780_BLEND_ONE;
296 case PIPE_BLENDFACTOR_SRC_COLOR:
297 return V_028780_BLEND_SRC_COLOR;
298 case PIPE_BLENDFACTOR_SRC_ALPHA:
299 return V_028780_BLEND_SRC_ALPHA;
300 case PIPE_BLENDFACTOR_DST_ALPHA:
301 return V_028780_BLEND_DST_ALPHA;
302 case PIPE_BLENDFACTOR_DST_COLOR:
303 return V_028780_BLEND_DST_COLOR;
304 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
305 return V_028780_BLEND_SRC_ALPHA_SATURATE;
306 case PIPE_BLENDFACTOR_CONST_COLOR:
307 return V_028780_BLEND_CONSTANT_COLOR;
308 case PIPE_BLENDFACTOR_CONST_ALPHA:
309 return V_028780_BLEND_CONSTANT_ALPHA;
310 case PIPE_BLENDFACTOR_ZERO:
311 return V_028780_BLEND_ZERO;
312 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
313 return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
314 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
315 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
316 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
317 return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
318 case PIPE_BLENDFACTOR_INV_DST_COLOR:
319 return V_028780_BLEND_ONE_MINUS_DST_COLOR;
320 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
321 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
322 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
323 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
324 case PIPE_BLENDFACTOR_SRC1_COLOR:
325 return V_028780_BLEND_SRC1_COLOR;
326 case PIPE_BLENDFACTOR_SRC1_ALPHA:
327 return V_028780_BLEND_SRC1_ALPHA;
328 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
329 return V_028780_BLEND_INV_SRC1_COLOR;
330 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
331 return V_028780_BLEND_INV_SRC1_ALPHA;
332 default:
333 PRINT_ERR("Bad blend factor %d not supported!\n", blend_fact);
334 assert(0);
335 break;
336 }
337 return 0;
338 }
339
340 static uint32_t si_translate_blend_opt_function(int blend_func)
341 {
342 switch (blend_func) {
343 case PIPE_BLEND_ADD:
344 return V_028760_OPT_COMB_ADD;
345 case PIPE_BLEND_SUBTRACT:
346 return V_028760_OPT_COMB_SUBTRACT;
347 case PIPE_BLEND_REVERSE_SUBTRACT:
348 return V_028760_OPT_COMB_REVSUBTRACT;
349 case PIPE_BLEND_MIN:
350 return V_028760_OPT_COMB_MIN;
351 case PIPE_BLEND_MAX:
352 return V_028760_OPT_COMB_MAX;
353 default:
354 return V_028760_OPT_COMB_BLEND_DISABLED;
355 }
356 }
357
358 static uint32_t si_translate_blend_opt_factor(int blend_fact, bool is_alpha)
359 {
360 switch (blend_fact) {
361 case PIPE_BLENDFACTOR_ZERO:
362 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;
363 case PIPE_BLENDFACTOR_ONE:
364 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;
365 case PIPE_BLENDFACTOR_SRC_COLOR:
366 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
367 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;
368 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
369 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
370 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;
371 case PIPE_BLENDFACTOR_SRC_ALPHA:
372 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;
373 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
374 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;
375 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
376 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
377 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
378 default:
379 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
380 }
381 }
382
383 static void si_blend_check_commutativity(struct si_screen *sscreen, struct si_state_blend *blend,
384 enum pipe_blend_func func, enum pipe_blendfactor src,
385 enum pipe_blendfactor dst, unsigned chanmask)
386 {
387 /* Src factor is allowed when it does not depend on Dst */
388 static const uint32_t src_allowed =
389 (1u << PIPE_BLENDFACTOR_ONE) | (1u << PIPE_BLENDFACTOR_SRC_COLOR) |
390 (1u << PIPE_BLENDFACTOR_SRC_ALPHA) | (1u << PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) |
391 (1u << PIPE_BLENDFACTOR_CONST_COLOR) | (1u << PIPE_BLENDFACTOR_CONST_ALPHA) |
392 (1u << PIPE_BLENDFACTOR_SRC1_COLOR) | (1u << PIPE_BLENDFACTOR_SRC1_ALPHA) |
393 (1u << PIPE_BLENDFACTOR_ZERO) | (1u << PIPE_BLENDFACTOR_INV_SRC_COLOR) |
394 (1u << PIPE_BLENDFACTOR_INV_SRC_ALPHA) | (1u << PIPE_BLENDFACTOR_INV_CONST_COLOR) |
395 (1u << PIPE_BLENDFACTOR_INV_CONST_ALPHA) | (1u << PIPE_BLENDFACTOR_INV_SRC1_COLOR) |
396 (1u << PIPE_BLENDFACTOR_INV_SRC1_ALPHA);
397
398 if (dst == PIPE_BLENDFACTOR_ONE && (src_allowed & (1u << src))) {
399 /* Addition is commutative, but floating point addition isn't
400 * associative: subtle changes can be introduced via different
401 * rounding.
402 *
403 * Out-of-order is also non-deterministic, which means that
404 * this breaks OpenGL invariance requirements. So only enable
405 * out-of-order additive blending if explicitly allowed by a
406 * setting.
407 */
408 if (func == PIPE_BLEND_MAX || func == PIPE_BLEND_MIN ||
409 (func == PIPE_BLEND_ADD && sscreen->commutative_blend_add))
410 blend->commutative_4bit |= chanmask;
411 }
412 }
413
414 /**
415 * Get rid of DST in the blend factors by commuting the operands:
416 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
417 */
418 static void si_blend_remove_dst(unsigned *func, unsigned *src_factor, unsigned *dst_factor,
419 unsigned expected_dst, unsigned replacement_src)
420 {
421 if (*src_factor == expected_dst && *dst_factor == PIPE_BLENDFACTOR_ZERO) {
422 *src_factor = PIPE_BLENDFACTOR_ZERO;
423 *dst_factor = replacement_src;
424
425 /* Commuting the operands requires reversing subtractions. */
426 if (*func == PIPE_BLEND_SUBTRACT)
427 *func = PIPE_BLEND_REVERSE_SUBTRACT;
428 else if (*func == PIPE_BLEND_REVERSE_SUBTRACT)
429 *func = PIPE_BLEND_SUBTRACT;
430 }
431 }
432
433 static bool si_blend_factor_uses_dst(unsigned factor)
434 {
435 return factor == PIPE_BLENDFACTOR_DST_COLOR || factor == PIPE_BLENDFACTOR_DST_ALPHA ||
436 factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
437 factor == PIPE_BLENDFACTOR_INV_DST_ALPHA || factor == PIPE_BLENDFACTOR_INV_DST_COLOR;
438 }
439
440 static void *si_create_blend_state_mode(struct pipe_context *ctx,
441 const struct pipe_blend_state *state, unsigned mode)
442 {
443 struct si_context *sctx = (struct si_context *)ctx;
444 struct si_state_blend *blend = CALLOC_STRUCT(si_state_blend);
445 struct si_pm4_state *pm4 = &blend->pm4;
446 uint32_t sx_mrt_blend_opt[8] = {0};
447 uint32_t color_control = 0;
448 bool logicop_enable = state->logicop_enable && state->logicop_func != PIPE_LOGICOP_COPY;
449
450 if (!blend)
451 return NULL;
452
453 blend->alpha_to_coverage = state->alpha_to_coverage;
454 blend->alpha_to_one = state->alpha_to_one;
455 blend->dual_src_blend = util_blend_state_is_dual(state, 0);
456 blend->logicop_enable = logicop_enable;
457
458 unsigned num_shader_outputs = state->max_rt + 1; /* estimate */
459 if (blend->dual_src_blend)
460 num_shader_outputs = MAX2(num_shader_outputs, 2);
461
462 if (logicop_enable) {
463 color_control |= S_028808_ROP3(state->logicop_func | (state->logicop_func << 4));
464 } else {
465 color_control |= S_028808_ROP3(0xcc);
466 }
467
468 if (state->alpha_to_coverage && state->alpha_to_coverage_dither) {
469 si_pm4_set_reg(pm4, R_028B70_DB_ALPHA_TO_MASK,
470 S_028B70_ALPHA_TO_MASK_ENABLE(state->alpha_to_coverage) |
471 S_028B70_ALPHA_TO_MASK_OFFSET0(3) | S_028B70_ALPHA_TO_MASK_OFFSET1(1) |
472 S_028B70_ALPHA_TO_MASK_OFFSET2(0) | S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
473 S_028B70_OFFSET_ROUND(1));
474 } else {
475 si_pm4_set_reg(pm4, R_028B70_DB_ALPHA_TO_MASK,
476 S_028B70_ALPHA_TO_MASK_ENABLE(state->alpha_to_coverage) |
477 S_028B70_ALPHA_TO_MASK_OFFSET0(2) | S_028B70_ALPHA_TO_MASK_OFFSET1(2) |
478 S_028B70_ALPHA_TO_MASK_OFFSET2(2) | S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
479 S_028B70_OFFSET_ROUND(0));
480 }
481
482 if (state->alpha_to_coverage)
483 blend->need_src_alpha_4bit |= 0xf;
484
485 blend->cb_target_mask = 0;
486 blend->cb_target_enabled_4bit = 0;
487
488 for (int i = 0; i < num_shader_outputs; i++) {
489 /* state->rt entries > 0 only written if independent blending */
490 const int j = state->independent_blend_enable ? i : 0;
491
492 unsigned eqRGB = state->rt[j].rgb_func;
493 unsigned srcRGB = state->rt[j].rgb_src_factor;
494 unsigned dstRGB = state->rt[j].rgb_dst_factor;
495 unsigned eqA = state->rt[j].alpha_func;
496 unsigned srcA = state->rt[j].alpha_src_factor;
497 unsigned dstA = state->rt[j].alpha_dst_factor;
498
499 unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;
500 unsigned blend_cntl = 0;
501
502 sx_mrt_blend_opt[i] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED) |
503 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED);
504
505 /* Only set dual source blending for MRT0 to avoid a hang. */
506 if (i >= 1 && blend->dual_src_blend) {
507 /* Vulkan does this for dual source blending. */
508 if (i == 1)
509 blend_cntl |= S_028780_ENABLE(1);
510
511 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
512 continue;
513 }
514
515 /* Only addition and subtraction equations are supported with
516 * dual source blending.
517 */
518 if (blend->dual_src_blend && (eqRGB == PIPE_BLEND_MIN || eqRGB == PIPE_BLEND_MAX ||
519 eqA == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MAX)) {
520 assert(!"Unsupported equation for dual source blending");
521 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
522 continue;
523 }
524
525 /* cb_render_state will disable unused ones */
526 blend->cb_target_mask |= (unsigned)state->rt[j].colormask << (4 * i);
527 if (state->rt[j].colormask)
528 blend->cb_target_enabled_4bit |= 0xf << (4 * i);
529
530 if (!state->rt[j].colormask || !state->rt[j].blend_enable) {
531 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
532 continue;
533 }
534
535 si_blend_check_commutativity(sctx->screen, blend, eqRGB, srcRGB, dstRGB, 0x7 << (4 * i));
536 si_blend_check_commutativity(sctx->screen, blend, eqA, srcA, dstA, 0x8 << (4 * i));
537
538 /* Blending optimizations for RB+.
539 * These transformations don't change the behavior.
540 *
541 * First, get rid of DST in the blend factors:
542 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
543 */
544 si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB, PIPE_BLENDFACTOR_DST_COLOR,
545 PIPE_BLENDFACTOR_SRC_COLOR);
546 si_blend_remove_dst(&eqA, &srcA, &dstA, PIPE_BLENDFACTOR_DST_COLOR,
547 PIPE_BLENDFACTOR_SRC_COLOR);
548 si_blend_remove_dst(&eqA, &srcA, &dstA, PIPE_BLENDFACTOR_DST_ALPHA,
549 PIPE_BLENDFACTOR_SRC_ALPHA);
550
551 /* Look up the ideal settings from tables. */
552 srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);
553 dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);
554 srcA_opt = si_translate_blend_opt_factor(srcA, true);
555 dstA_opt = si_translate_blend_opt_factor(dstA, true);
556
557 /* Handle interdependencies. */
558 if (si_blend_factor_uses_dst(srcRGB))
559 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
560 if (si_blend_factor_uses_dst(srcA))
561 dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
562
563 if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE &&
564 (dstRGB == PIPE_BLENDFACTOR_ZERO || dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
565 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE))
566 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
567
568 /* Set the final value. */
569 sx_mrt_blend_opt[i] = S_028760_COLOR_SRC_OPT(srcRGB_opt) |
570 S_028760_COLOR_DST_OPT(dstRGB_opt) |
571 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |
572 S_028760_ALPHA_SRC_OPT(srcA_opt) | S_028760_ALPHA_DST_OPT(dstA_opt) |
573 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));
574
575 /* Set blend state. */
576 blend_cntl |= S_028780_ENABLE(1);
577 blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
578 blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
579 blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
580
581 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
582 blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
583 blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
584 blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
585 blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
586 }
587 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
588
589 blend->blend_enable_4bit |= 0xfu << (i * 4);
590
591 if (sctx->chip_class >= GFX8 && sctx->chip_class <= GFX10)
592 blend->dcc_msaa_corruption_4bit |= 0xfu << (i * 4);
593
594 /* This is only important for formats without alpha. */
595 if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA || dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
596 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
597 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
598 srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA || dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA)
599 blend->need_src_alpha_4bit |= 0xfu << (i * 4);
600 }
601
602 if (sctx->chip_class >= GFX8 && sctx->chip_class <= GFX10 && logicop_enable)
603 blend->dcc_msaa_corruption_4bit |= blend->cb_target_enabled_4bit;
604
605 if (blend->cb_target_mask) {
606 color_control |= S_028808_MODE(mode);
607 } else {
608 color_control |= S_028808_MODE(V_028808_CB_DISABLE);
609 }
610
611 if (sctx->screen->info.rbplus_allowed) {
612 /* Disable RB+ blend optimizations for dual source blending.
613 * Vulkan does this.
614 */
615 if (blend->dual_src_blend) {
616 for (int i = 0; i < num_shader_outputs; i++) {
617 sx_mrt_blend_opt[i] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE) |
618 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE);
619 }
620 }
621
622 for (int i = 0; i < num_shader_outputs; i++)
623 si_pm4_set_reg(pm4, R_028760_SX_MRT0_BLEND_OPT + i * 4, sx_mrt_blend_opt[i]);
624
625 /* RB+ doesn't work with dual source blending, logic op, and RESOLVE. */
626 if (blend->dual_src_blend || logicop_enable || mode == V_028808_CB_RESOLVE)
627 color_control |= S_028808_DISABLE_DUAL_QUAD(1);
628 }
629
630 si_pm4_set_reg(pm4, R_028808_CB_COLOR_CONTROL, color_control);
631 return blend;
632 }
633
634 static void *si_create_blend_state(struct pipe_context *ctx, const struct pipe_blend_state *state)
635 {
636 return si_create_blend_state_mode(ctx, state, V_028808_CB_NORMAL);
637 }
638
639 static void si_bind_blend_state(struct pipe_context *ctx, void *state)
640 {
641 struct si_context *sctx = (struct si_context *)ctx;
642 struct si_state_blend *old_blend = sctx->queued.named.blend;
643 struct si_state_blend *blend = (struct si_state_blend *)state;
644
645 if (!blend)
646 blend = (struct si_state_blend *)sctx->noop_blend;
647
648 si_pm4_bind_state(sctx, blend, blend);
649
650 if (old_blend->cb_target_mask != blend->cb_target_mask ||
651 old_blend->dual_src_blend != blend->dual_src_blend ||
652 (old_blend->dcc_msaa_corruption_4bit != blend->dcc_msaa_corruption_4bit &&
653 sctx->framebuffer.nr_samples >= 2 && sctx->screen->dcc_msaa_allowed))
654 si_mark_atom_dirty(sctx, &sctx->atoms.s.cb_render_state);
655
656 if (old_blend->cb_target_mask != blend->cb_target_mask ||
657 old_blend->alpha_to_coverage != blend->alpha_to_coverage ||
658 old_blend->alpha_to_one != blend->alpha_to_one ||
659 old_blend->dual_src_blend != blend->dual_src_blend ||
660 old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
661 old_blend->need_src_alpha_4bit != blend->need_src_alpha_4bit)
662 sctx->do_update_shaders = true;
663
664 if (sctx->screen->dpbb_allowed &&
665 (old_blend->alpha_to_coverage != blend->alpha_to_coverage ||
666 old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
667 old_blend->cb_target_enabled_4bit != blend->cb_target_enabled_4bit))
668 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
669
670 if (sctx->screen->has_out_of_order_rast &&
671 ((old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
672 old_blend->cb_target_enabled_4bit != blend->cb_target_enabled_4bit ||
673 old_blend->commutative_4bit != blend->commutative_4bit ||
674 old_blend->logicop_enable != blend->logicop_enable)))
675 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
676 }
677
678 static void si_delete_blend_state(struct pipe_context *ctx, void *state)
679 {
680 struct si_context *sctx = (struct si_context *)ctx;
681
682 if (sctx->queued.named.blend == state)
683 si_bind_blend_state(ctx, sctx->noop_blend);
684
685 si_pm4_delete_state(sctx, blend, (struct si_state_blend *)state);
686 }
687
688 static void si_set_blend_color(struct pipe_context *ctx, const struct pipe_blend_color *state)
689 {
690 struct si_context *sctx = (struct si_context *)ctx;
691 static const struct pipe_blend_color zeros;
692
693 sctx->blend_color.state = *state;
694 sctx->blend_color.any_nonzeros = memcmp(state, &zeros, sizeof(*state)) != 0;
695 si_mark_atom_dirty(sctx, &sctx->atoms.s.blend_color);
696 }
697
698 static void si_emit_blend_color(struct si_context *sctx)
699 {
700 struct radeon_cmdbuf *cs = sctx->gfx_cs;
701
702 radeon_set_context_reg_seq(cs, R_028414_CB_BLEND_RED, 4);
703 radeon_emit_array(cs, (uint32_t *)sctx->blend_color.state.color, 4);
704 }
705
706 /*
707 * Clipping
708 */
709
710 static void si_set_clip_state(struct pipe_context *ctx, const struct pipe_clip_state *state)
711 {
712 struct si_context *sctx = (struct si_context *)ctx;
713 struct pipe_constant_buffer cb;
714 static const struct pipe_clip_state zeros;
715
716 if (memcmp(&sctx->clip_state.state, state, sizeof(*state)) == 0)
717 return;
718
719 sctx->clip_state.state = *state;
720 sctx->clip_state.any_nonzeros = memcmp(state, &zeros, sizeof(*state)) != 0;
721 si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_state);
722
723 cb.buffer = NULL;
724 cb.user_buffer = state->ucp;
725 cb.buffer_offset = 0;
726 cb.buffer_size = 4 * 4 * 8;
727 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES, &cb);
728 pipe_resource_reference(&cb.buffer, NULL);
729 }
730
731 static void si_emit_clip_state(struct si_context *sctx)
732 {
733 struct radeon_cmdbuf *cs = sctx->gfx_cs;
734
735 radeon_set_context_reg_seq(cs, R_0285BC_PA_CL_UCP_0_X, 6 * 4);
736 radeon_emit_array(cs, (uint32_t *)sctx->clip_state.state.ucp, 6 * 4);
737 }
738
739 static void si_emit_clip_regs(struct si_context *sctx)
740 {
741 struct si_shader *vs = si_get_vs_state(sctx);
742 struct si_shader_selector *vs_sel = vs->selector;
743 struct si_shader_info *info = &vs_sel->info;
744 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
745 unsigned window_space = info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
746 unsigned clipdist_mask = vs_sel->clipdist_mask;
747 unsigned ucp_mask = clipdist_mask ? 0 : rs->clip_plane_enable & SIX_BITS;
748 unsigned culldist_mask = vs_sel->culldist_mask;
749 unsigned total_mask;
750
751 if (vs->key.opt.clip_disable) {
752 assert(!info->culldist_writemask);
753 clipdist_mask = 0;
754 culldist_mask = 0;
755 }
756 total_mask = clipdist_mask | culldist_mask;
757
758 /* Clip distances on points have no effect, so need to be implemented
759 * as cull distances. This applies for the clipvertex case as well.
760 *
761 * Setting this for primitives other than points should have no adverse
762 * effects.
763 */
764 clipdist_mask &= rs->clip_plane_enable;
765 culldist_mask |= clipdist_mask;
766
767 unsigned initial_cdw = sctx->gfx_cs->current.cdw;
768 unsigned pa_cl_cntl = S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0F) != 0) |
769 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xF0) != 0) | clipdist_mask |
770 (culldist_mask << 8);
771
772 if (sctx->chip_class >= GFX10) {
773 radeon_opt_set_context_reg_rmw(sctx, R_02881C_PA_CL_VS_OUT_CNTL,
774 SI_TRACKED_PA_CL_VS_OUT_CNTL__CL, pa_cl_cntl,
775 ~SI_TRACKED_PA_CL_VS_OUT_CNTL__VS_MASK);
776 } else {
777 radeon_opt_set_context_reg(sctx, R_02881C_PA_CL_VS_OUT_CNTL, SI_TRACKED_PA_CL_VS_OUT_CNTL__CL,
778 vs_sel->pa_cl_vs_out_cntl | pa_cl_cntl);
779 }
780 radeon_opt_set_context_reg(sctx, R_028810_PA_CL_CLIP_CNTL, SI_TRACKED_PA_CL_CLIP_CNTL,
781 rs->pa_cl_clip_cntl | ucp_mask | S_028810_CLIP_DISABLE(window_space));
782
783 if (initial_cdw != sctx->gfx_cs->current.cdw)
784 sctx->context_roll = true;
785 }
786
787 /*
788 * inferred state between framebuffer and rasterizer
789 */
790 static void si_update_poly_offset_state(struct si_context *sctx)
791 {
792 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
793
794 if (!rs->uses_poly_offset || !sctx->framebuffer.state.zsbuf) {
795 si_pm4_bind_state(sctx, poly_offset, NULL);
796 return;
797 }
798
799 /* Use the user format, not db_render_format, so that the polygon
800 * offset behaves as expected by applications.
801 */
802 switch (sctx->framebuffer.state.zsbuf->texture->format) {
803 case PIPE_FORMAT_Z16_UNORM:
804 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[0]);
805 break;
806 default: /* 24-bit */
807 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[1]);
808 break;
809 case PIPE_FORMAT_Z32_FLOAT:
810 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
811 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[2]);
812 break;
813 }
814 }
815
816 /*
817 * Rasterizer
818 */
819
820 static uint32_t si_translate_fill(uint32_t func)
821 {
822 switch (func) {
823 case PIPE_POLYGON_MODE_FILL:
824 return V_028814_X_DRAW_TRIANGLES;
825 case PIPE_POLYGON_MODE_LINE:
826 return V_028814_X_DRAW_LINES;
827 case PIPE_POLYGON_MODE_POINT:
828 return V_028814_X_DRAW_POINTS;
829 default:
830 assert(0);
831 return V_028814_X_DRAW_POINTS;
832 }
833 }
834
835 static void *si_create_rs_state(struct pipe_context *ctx, const struct pipe_rasterizer_state *state)
836 {
837 struct si_screen *sscreen = ((struct si_context *)ctx)->screen;
838 struct si_state_rasterizer *rs = CALLOC_STRUCT(si_state_rasterizer);
839 struct si_pm4_state *pm4 = &rs->pm4;
840 unsigned tmp, i;
841 float psize_min, psize_max;
842
843 if (!rs) {
844 return NULL;
845 }
846
847 if (!state->front_ccw) {
848 rs->cull_front = !!(state->cull_face & PIPE_FACE_FRONT);
849 rs->cull_back = !!(state->cull_face & PIPE_FACE_BACK);
850 } else {
851 rs->cull_back = !!(state->cull_face & PIPE_FACE_FRONT);
852 rs->cull_front = !!(state->cull_face & PIPE_FACE_BACK);
853 }
854 rs->depth_clamp_any = !state->depth_clip_near || !state->depth_clip_far;
855 rs->provoking_vertex_first = state->flatshade_first;
856 rs->scissor_enable = state->scissor;
857 rs->clip_halfz = state->clip_halfz;
858 rs->two_side = state->light_twoside;
859 rs->multisample_enable = state->multisample;
860 rs->force_persample_interp = state->force_persample_interp;
861 rs->clip_plane_enable = state->clip_plane_enable;
862 rs->half_pixel_center = state->half_pixel_center;
863 rs->line_stipple_enable = state->line_stipple_enable;
864 rs->poly_stipple_enable = state->poly_stipple_enable;
865 rs->line_smooth = state->line_smooth;
866 rs->line_width = state->line_width;
867 rs->poly_smooth = state->poly_smooth;
868 rs->uses_poly_offset = state->offset_point || state->offset_line || state->offset_tri;
869 rs->clamp_fragment_color = state->clamp_fragment_color;
870 rs->clamp_vertex_color = state->clamp_vertex_color;
871 rs->flatshade = state->flatshade;
872 rs->flatshade_first = state->flatshade_first;
873 rs->sprite_coord_enable = state->sprite_coord_enable;
874 rs->rasterizer_discard = state->rasterizer_discard;
875 rs->polygon_mode_enabled =
876 (state->fill_front != PIPE_POLYGON_MODE_FILL && !(state->cull_face & PIPE_FACE_FRONT)) ||
877 (state->fill_back != PIPE_POLYGON_MODE_FILL && !(state->cull_face & PIPE_FACE_BACK));
878 rs->polygon_mode_is_lines =
879 (state->fill_front == PIPE_POLYGON_MODE_LINE && !(state->cull_face & PIPE_FACE_FRONT)) ||
880 (state->fill_back == PIPE_POLYGON_MODE_LINE && !(state->cull_face & PIPE_FACE_BACK));
881 rs->pa_sc_line_stipple = state->line_stipple_enable
882 ? S_028A0C_LINE_PATTERN(state->line_stipple_pattern) |
883 S_028A0C_REPEAT_COUNT(state->line_stipple_factor)
884 : 0;
885 rs->pa_cl_clip_cntl = S_028810_DX_CLIP_SPACE_DEF(state->clip_halfz) |
886 S_028810_ZCLIP_NEAR_DISABLE(!state->depth_clip_near) |
887 S_028810_ZCLIP_FAR_DISABLE(!state->depth_clip_far) |
888 S_028810_DX_RASTERIZATION_KILL(state->rasterizer_discard) |
889 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
890
891 si_pm4_set_reg(
892 pm4, R_0286D4_SPI_INTERP_CONTROL_0,
893 S_0286D4_FLAT_SHADE_ENA(1) | S_0286D4_PNT_SPRITE_ENA(state->point_quad_rasterization) |
894 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
895 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
896 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
897 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
898 S_0286D4_PNT_SPRITE_TOP_1(state->sprite_coord_mode != PIPE_SPRITE_COORD_UPPER_LEFT));
899
900 /* point size 12.4 fixed point */
901 tmp = (unsigned)(state->point_size * 8.0);
902 si_pm4_set_reg(pm4, R_028A00_PA_SU_POINT_SIZE, S_028A00_HEIGHT(tmp) | S_028A00_WIDTH(tmp));
903
904 if (state->point_size_per_vertex) {
905 psize_min = util_get_min_point_size(state);
906 psize_max = SI_MAX_POINT_SIZE;
907 } else {
908 /* Force the point size to be as if the vertex output was disabled. */
909 psize_min = state->point_size;
910 psize_max = state->point_size;
911 }
912 rs->max_point_size = psize_max;
913
914 /* Divide by two, because 0.5 = 1 pixel. */
915 si_pm4_set_reg(pm4, R_028A04_PA_SU_POINT_MINMAX,
916 S_028A04_MIN_SIZE(si_pack_float_12p4(psize_min / 2)) |
917 S_028A04_MAX_SIZE(si_pack_float_12p4(psize_max / 2)));
918
919 si_pm4_set_reg(pm4, R_028A08_PA_SU_LINE_CNTL,
920 S_028A08_WIDTH(si_pack_float_12p4(state->line_width / 2)));
921 si_pm4_set_reg(
922 pm4, R_028A48_PA_SC_MODE_CNTL_0,
923 S_028A48_LINE_STIPPLE_ENABLE(state->line_stipple_enable) |
924 S_028A48_MSAA_ENABLE(state->multisample || state->poly_smooth || state->line_smooth) |
925 S_028A48_VPORT_SCISSOR_ENABLE(1) |
926 S_028A48_ALTERNATE_RBS_PER_TILE(sscreen->info.chip_class >= GFX9));
927
928 si_pm4_set_reg(pm4, R_028B7C_PA_SU_POLY_OFFSET_CLAMP, fui(state->offset_clamp));
929 si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL,
930 S_028814_PROVOKING_VTX_LAST(!state->flatshade_first) |
931 S_028814_CULL_FRONT((state->cull_face & PIPE_FACE_FRONT) ? 1 : 0) |
932 S_028814_CULL_BACK((state->cull_face & PIPE_FACE_BACK) ? 1 : 0) |
933 S_028814_FACE(!state->front_ccw) |
934 S_028814_POLY_OFFSET_FRONT_ENABLE(util_get_offset(state, state->fill_front)) |
935 S_028814_POLY_OFFSET_BACK_ENABLE(util_get_offset(state, state->fill_back)) |
936 S_028814_POLY_OFFSET_PARA_ENABLE(state->offset_point || state->offset_line) |
937 S_028814_POLY_MODE(rs->polygon_mode_enabled) |
938 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(state->fill_front)) |
939 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(state->fill_back)));
940
941 if (!rs->uses_poly_offset)
942 return rs;
943
944 rs->pm4_poly_offset = CALLOC(3, sizeof(struct si_pm4_state));
945 if (!rs->pm4_poly_offset) {
946 FREE(rs);
947 return NULL;
948 }
949
950 /* Precalculate polygon offset states for 16-bit, 24-bit, and 32-bit zbuffers. */
951 for (i = 0; i < 3; i++) {
952 struct si_pm4_state *pm4 = &rs->pm4_poly_offset[i];
953 float offset_units = state->offset_units;
954 float offset_scale = state->offset_scale * 16.0f;
955 uint32_t pa_su_poly_offset_db_fmt_cntl = 0;
956
957 if (!state->offset_units_unscaled) {
958 switch (i) {
959 case 0: /* 16-bit zbuffer */
960 offset_units *= 4.0f;
961 pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
962 break;
963 case 1: /* 24-bit zbuffer */
964 offset_units *= 2.0f;
965 pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
966 break;
967 case 2: /* 32-bit zbuffer */
968 offset_units *= 1.0f;
969 pa_su_poly_offset_db_fmt_cntl =
970 S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) | S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
971 break;
972 }
973 }
974
975 si_pm4_set_reg(pm4, R_028B80_PA_SU_POLY_OFFSET_FRONT_SCALE, fui(offset_scale));
976 si_pm4_set_reg(pm4, R_028B84_PA_SU_POLY_OFFSET_FRONT_OFFSET, fui(offset_units));
977 si_pm4_set_reg(pm4, R_028B88_PA_SU_POLY_OFFSET_BACK_SCALE, fui(offset_scale));
978 si_pm4_set_reg(pm4, R_028B8C_PA_SU_POLY_OFFSET_BACK_OFFSET, fui(offset_units));
979 si_pm4_set_reg(pm4, R_028B78_PA_SU_POLY_OFFSET_DB_FMT_CNTL, pa_su_poly_offset_db_fmt_cntl);
980 }
981
982 return rs;
983 }
984
985 static void si_bind_rs_state(struct pipe_context *ctx, void *state)
986 {
987 struct si_context *sctx = (struct si_context *)ctx;
988 struct si_state_rasterizer *old_rs = (struct si_state_rasterizer *)sctx->queued.named.rasterizer;
989 struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
990
991 if (!rs)
992 rs = (struct si_state_rasterizer *)sctx->discard_rasterizer_state;
993
994 if (old_rs->multisample_enable != rs->multisample_enable) {
995 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
996
997 /* Update the small primitive filter workaround if necessary. */
998 if (sctx->screen->info.has_msaa_sample_loc_bug && sctx->framebuffer.nr_samples > 1)
999 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_sample_locs);
1000 }
1001
1002 sctx->current_vs_state &= C_VS_STATE_CLAMP_VERTEX_COLOR;
1003 sctx->current_vs_state |= S_VS_STATE_CLAMP_VERTEX_COLOR(rs->clamp_vertex_color);
1004
1005 si_pm4_bind_state(sctx, rasterizer, rs);
1006 si_update_poly_offset_state(sctx);
1007
1008 if (old_rs->scissor_enable != rs->scissor_enable)
1009 si_mark_atom_dirty(sctx, &sctx->atoms.s.scissors);
1010
1011 if (old_rs->line_width != rs->line_width || old_rs->max_point_size != rs->max_point_size ||
1012 old_rs->half_pixel_center != rs->half_pixel_center)
1013 si_mark_atom_dirty(sctx, &sctx->atoms.s.guardband);
1014
1015 if (old_rs->clip_halfz != rs->clip_halfz)
1016 si_mark_atom_dirty(sctx, &sctx->atoms.s.viewports);
1017
1018 if (old_rs->clip_plane_enable != rs->clip_plane_enable ||
1019 old_rs->pa_cl_clip_cntl != rs->pa_cl_clip_cntl)
1020 si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_regs);
1021
1022 if (old_rs->clip_plane_enable != rs->clip_plane_enable ||
1023 old_rs->rasterizer_discard != rs->rasterizer_discard ||
1024 old_rs->sprite_coord_enable != rs->sprite_coord_enable ||
1025 old_rs->flatshade != rs->flatshade || old_rs->two_side != rs->two_side ||
1026 old_rs->multisample_enable != rs->multisample_enable ||
1027 old_rs->poly_stipple_enable != rs->poly_stipple_enable ||
1028 old_rs->poly_smooth != rs->poly_smooth || old_rs->line_smooth != rs->line_smooth ||
1029 old_rs->clamp_fragment_color != rs->clamp_fragment_color ||
1030 old_rs->force_persample_interp != rs->force_persample_interp)
1031 sctx->do_update_shaders = true;
1032 }
1033
1034 static void si_delete_rs_state(struct pipe_context *ctx, void *state)
1035 {
1036 struct si_context *sctx = (struct si_context *)ctx;
1037 struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
1038
1039 if (sctx->queued.named.rasterizer == state)
1040 si_bind_rs_state(ctx, sctx->discard_rasterizer_state);
1041
1042 FREE(rs->pm4_poly_offset);
1043 si_pm4_delete_state(sctx, rasterizer, rs);
1044 }
1045
1046 /*
1047 * infeered state between dsa and stencil ref
1048 */
1049 static void si_emit_stencil_ref(struct si_context *sctx)
1050 {
1051 struct radeon_cmdbuf *cs = sctx->gfx_cs;
1052 struct pipe_stencil_ref *ref = &sctx->stencil_ref.state;
1053 struct si_dsa_stencil_ref_part *dsa = &sctx->stencil_ref.dsa_part;
1054
1055 radeon_set_context_reg_seq(cs, R_028430_DB_STENCILREFMASK, 2);
1056 radeon_emit(cs, S_028430_STENCILTESTVAL(ref->ref_value[0]) |
1057 S_028430_STENCILMASK(dsa->valuemask[0]) |
1058 S_028430_STENCILWRITEMASK(dsa->writemask[0]) | S_028430_STENCILOPVAL(1));
1059 radeon_emit(cs, S_028434_STENCILTESTVAL_BF(ref->ref_value[1]) |
1060 S_028434_STENCILMASK_BF(dsa->valuemask[1]) |
1061 S_028434_STENCILWRITEMASK_BF(dsa->writemask[1]) |
1062 S_028434_STENCILOPVAL_BF(1));
1063 }
1064
1065 static void si_set_stencil_ref(struct pipe_context *ctx, const struct pipe_stencil_ref *state)
1066 {
1067 struct si_context *sctx = (struct si_context *)ctx;
1068
1069 if (memcmp(&sctx->stencil_ref.state, state, sizeof(*state)) == 0)
1070 return;
1071
1072 sctx->stencil_ref.state = *state;
1073 si_mark_atom_dirty(sctx, &sctx->atoms.s.stencil_ref);
1074 }
1075
1076 /*
1077 * DSA
1078 */
1079
1080 static uint32_t si_translate_stencil_op(int s_op)
1081 {
1082 switch (s_op) {
1083 case PIPE_STENCIL_OP_KEEP:
1084 return V_02842C_STENCIL_KEEP;
1085 case PIPE_STENCIL_OP_ZERO:
1086 return V_02842C_STENCIL_ZERO;
1087 case PIPE_STENCIL_OP_REPLACE:
1088 return V_02842C_STENCIL_REPLACE_TEST;
1089 case PIPE_STENCIL_OP_INCR:
1090 return V_02842C_STENCIL_ADD_CLAMP;
1091 case PIPE_STENCIL_OP_DECR:
1092 return V_02842C_STENCIL_SUB_CLAMP;
1093 case PIPE_STENCIL_OP_INCR_WRAP:
1094 return V_02842C_STENCIL_ADD_WRAP;
1095 case PIPE_STENCIL_OP_DECR_WRAP:
1096 return V_02842C_STENCIL_SUB_WRAP;
1097 case PIPE_STENCIL_OP_INVERT:
1098 return V_02842C_STENCIL_INVERT;
1099 default:
1100 PRINT_ERR("Unknown stencil op %d", s_op);
1101 assert(0);
1102 break;
1103 }
1104 return 0;
1105 }
1106
1107 static bool si_dsa_writes_stencil(const struct pipe_stencil_state *s)
1108 {
1109 return s->enabled && s->writemask &&
1110 (s->fail_op != PIPE_STENCIL_OP_KEEP || s->zfail_op != PIPE_STENCIL_OP_KEEP ||
1111 s->zpass_op != PIPE_STENCIL_OP_KEEP);
1112 }
1113
1114 static bool si_order_invariant_stencil_op(enum pipe_stencil_op op)
1115 {
1116 /* REPLACE is normally order invariant, except when the stencil
1117 * reference value is written by the fragment shader. Tracking this
1118 * interaction does not seem worth the effort, so be conservative. */
1119 return op != PIPE_STENCIL_OP_INCR && op != PIPE_STENCIL_OP_DECR && op != PIPE_STENCIL_OP_REPLACE;
1120 }
1121
1122 /* Compute whether, assuming Z writes are disabled, this stencil state is order
1123 * invariant in the sense that the set of passing fragments as well as the
1124 * final stencil buffer result does not depend on the order of fragments. */
1125 static bool si_order_invariant_stencil_state(const struct pipe_stencil_state *state)
1126 {
1127 return !state->enabled || !state->writemask ||
1128 /* The following assumes that Z writes are disabled. */
1129 (state->func == PIPE_FUNC_ALWAYS && si_order_invariant_stencil_op(state->zpass_op) &&
1130 si_order_invariant_stencil_op(state->zfail_op)) ||
1131 (state->func == PIPE_FUNC_NEVER && si_order_invariant_stencil_op(state->fail_op));
1132 }
1133
1134 static void *si_create_dsa_state(struct pipe_context *ctx,
1135 const struct pipe_depth_stencil_alpha_state *state)
1136 {
1137 struct si_context *sctx = (struct si_context *)ctx;
1138 struct si_state_dsa *dsa = CALLOC_STRUCT(si_state_dsa);
1139 struct si_pm4_state *pm4 = &dsa->pm4;
1140 unsigned db_depth_control;
1141 uint32_t db_stencil_control = 0;
1142
1143 if (!dsa) {
1144 return NULL;
1145 }
1146
1147 dsa->stencil_ref.valuemask[0] = state->stencil[0].valuemask;
1148 dsa->stencil_ref.valuemask[1] = state->stencil[1].valuemask;
1149 dsa->stencil_ref.writemask[0] = state->stencil[0].writemask;
1150 dsa->stencil_ref.writemask[1] = state->stencil[1].writemask;
1151
1152 db_depth_control =
1153 S_028800_Z_ENABLE(state->depth.enabled) | S_028800_Z_WRITE_ENABLE(state->depth.writemask) |
1154 S_028800_ZFUNC(state->depth.func) | S_028800_DEPTH_BOUNDS_ENABLE(state->depth.bounds_test);
1155
1156 /* stencil */
1157 if (state->stencil[0].enabled) {
1158 db_depth_control |= S_028800_STENCIL_ENABLE(1);
1159 db_depth_control |= S_028800_STENCILFUNC(state->stencil[0].func);
1160 db_stencil_control |=
1161 S_02842C_STENCILFAIL(si_translate_stencil_op(state->stencil[0].fail_op));
1162 db_stencil_control |=
1163 S_02842C_STENCILZPASS(si_translate_stencil_op(state->stencil[0].zpass_op));
1164 db_stencil_control |=
1165 S_02842C_STENCILZFAIL(si_translate_stencil_op(state->stencil[0].zfail_op));
1166
1167 if (state->stencil[1].enabled) {
1168 db_depth_control |= S_028800_BACKFACE_ENABLE(1);
1169 db_depth_control |= S_028800_STENCILFUNC_BF(state->stencil[1].func);
1170 db_stencil_control |=
1171 S_02842C_STENCILFAIL_BF(si_translate_stencil_op(state->stencil[1].fail_op));
1172 db_stencil_control |=
1173 S_02842C_STENCILZPASS_BF(si_translate_stencil_op(state->stencil[1].zpass_op));
1174 db_stencil_control |=
1175 S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(state->stencil[1].zfail_op));
1176 }
1177 }
1178
1179 /* alpha */
1180 if (state->alpha.enabled) {
1181 dsa->alpha_func = state->alpha.func;
1182
1183 si_pm4_set_reg(pm4, R_00B030_SPI_SHADER_USER_DATA_PS_0 + SI_SGPR_ALPHA_REF * 4,
1184 fui(state->alpha.ref_value));
1185 } else {
1186 dsa->alpha_func = PIPE_FUNC_ALWAYS;
1187 }
1188
1189 si_pm4_set_reg(pm4, R_028800_DB_DEPTH_CONTROL, db_depth_control);
1190 if (state->stencil[0].enabled)
1191 si_pm4_set_reg(pm4, R_02842C_DB_STENCIL_CONTROL, db_stencil_control);
1192 if (state->depth.bounds_test) {
1193 si_pm4_set_reg(pm4, R_028020_DB_DEPTH_BOUNDS_MIN, fui(state->depth.bounds_min));
1194 si_pm4_set_reg(pm4, R_028024_DB_DEPTH_BOUNDS_MAX, fui(state->depth.bounds_max));
1195 }
1196
1197 dsa->depth_enabled = state->depth.enabled;
1198 dsa->depth_write_enabled = state->depth.enabled && state->depth.writemask;
1199 dsa->stencil_enabled = state->stencil[0].enabled;
1200 dsa->stencil_write_enabled =
1201 state->stencil[0].enabled &&
1202 (si_dsa_writes_stencil(&state->stencil[0]) || si_dsa_writes_stencil(&state->stencil[1]));
1203 dsa->db_can_write = dsa->depth_write_enabled || dsa->stencil_write_enabled;
1204
1205 bool zfunc_is_ordered =
1206 state->depth.func == PIPE_FUNC_NEVER || state->depth.func == PIPE_FUNC_LESS ||
1207 state->depth.func == PIPE_FUNC_LEQUAL || state->depth.func == PIPE_FUNC_GREATER ||
1208 state->depth.func == PIPE_FUNC_GEQUAL;
1209
1210 bool nozwrite_and_order_invariant_stencil =
1211 !dsa->db_can_write ||
1212 (!dsa->depth_write_enabled && si_order_invariant_stencil_state(&state->stencil[0]) &&
1213 si_order_invariant_stencil_state(&state->stencil[1]));
1214
1215 dsa->order_invariance[1].zs =
1216 nozwrite_and_order_invariant_stencil || (!dsa->stencil_write_enabled && zfunc_is_ordered);
1217 dsa->order_invariance[0].zs = !dsa->depth_write_enabled || zfunc_is_ordered;
1218
1219 dsa->order_invariance[1].pass_set =
1220 nozwrite_and_order_invariant_stencil ||
1221 (!dsa->stencil_write_enabled &&
1222 (state->depth.func == PIPE_FUNC_ALWAYS || state->depth.func == PIPE_FUNC_NEVER));
1223 dsa->order_invariance[0].pass_set =
1224 !dsa->depth_write_enabled ||
1225 (state->depth.func == PIPE_FUNC_ALWAYS || state->depth.func == PIPE_FUNC_NEVER);
1226
1227 dsa->order_invariance[1].pass_last = sctx->screen->assume_no_z_fights &&
1228 !dsa->stencil_write_enabled && dsa->depth_write_enabled &&
1229 zfunc_is_ordered;
1230 dsa->order_invariance[0].pass_last =
1231 sctx->screen->assume_no_z_fights && dsa->depth_write_enabled && zfunc_is_ordered;
1232
1233 return dsa;
1234 }
1235
1236 static void si_bind_dsa_state(struct pipe_context *ctx, void *state)
1237 {
1238 struct si_context *sctx = (struct si_context *)ctx;
1239 struct si_state_dsa *old_dsa = sctx->queued.named.dsa;
1240 struct si_state_dsa *dsa = state;
1241
1242 if (!dsa)
1243 dsa = (struct si_state_dsa *)sctx->noop_dsa;
1244
1245 si_pm4_bind_state(sctx, dsa, dsa);
1246
1247 if (memcmp(&dsa->stencil_ref, &sctx->stencil_ref.dsa_part,
1248 sizeof(struct si_dsa_stencil_ref_part)) != 0) {
1249 sctx->stencil_ref.dsa_part = dsa->stencil_ref;
1250 si_mark_atom_dirty(sctx, &sctx->atoms.s.stencil_ref);
1251 }
1252
1253 if (old_dsa->alpha_func != dsa->alpha_func)
1254 sctx->do_update_shaders = true;
1255
1256 if (sctx->screen->dpbb_allowed && ((old_dsa->depth_enabled != dsa->depth_enabled ||
1257 old_dsa->stencil_enabled != dsa->stencil_enabled ||
1258 old_dsa->db_can_write != dsa->db_can_write)))
1259 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
1260
1261 if (sctx->screen->has_out_of_order_rast &&
1262 (memcmp(old_dsa->order_invariance, dsa->order_invariance,
1263 sizeof(old_dsa->order_invariance))))
1264 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
1265 }
1266
1267 static void si_delete_dsa_state(struct pipe_context *ctx, void *state)
1268 {
1269 struct si_context *sctx = (struct si_context *)ctx;
1270
1271 if (sctx->queued.named.dsa == state)
1272 si_bind_dsa_state(ctx, sctx->noop_dsa);
1273
1274 si_pm4_delete_state(sctx, dsa, (struct si_state_dsa *)state);
1275 }
1276
1277 static void *si_create_db_flush_dsa(struct si_context *sctx)
1278 {
1279 struct pipe_depth_stencil_alpha_state dsa = {};
1280
1281 return sctx->b.create_depth_stencil_alpha_state(&sctx->b, &dsa);
1282 }
1283
1284 /* DB RENDER STATE */
1285
1286 static void si_set_active_query_state(struct pipe_context *ctx, bool enable)
1287 {
1288 struct si_context *sctx = (struct si_context *)ctx;
1289
1290 /* Pipeline stat & streamout queries. */
1291 if (enable) {
1292 sctx->flags &= ~SI_CONTEXT_STOP_PIPELINE_STATS;
1293 sctx->flags |= SI_CONTEXT_START_PIPELINE_STATS;
1294 } else {
1295 sctx->flags &= ~SI_CONTEXT_START_PIPELINE_STATS;
1296 sctx->flags |= SI_CONTEXT_STOP_PIPELINE_STATS;
1297 }
1298
1299 /* Occlusion queries. */
1300 if (sctx->occlusion_queries_disabled != !enable) {
1301 sctx->occlusion_queries_disabled = !enable;
1302 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
1303 }
1304 }
1305
1306 void si_set_occlusion_query_state(struct si_context *sctx, bool old_perfect_enable)
1307 {
1308 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
1309
1310 bool perfect_enable = sctx->num_perfect_occlusion_queries != 0;
1311
1312 if (perfect_enable != old_perfect_enable)
1313 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
1314 }
1315
1316 void si_save_qbo_state(struct si_context *sctx, struct si_qbo_state *st)
1317 {
1318 st->saved_compute = sctx->cs_shader_state.program;
1319
1320 si_get_pipe_constant_buffer(sctx, PIPE_SHADER_COMPUTE, 0, &st->saved_const0);
1321 si_get_shader_buffers(sctx, PIPE_SHADER_COMPUTE, 0, 3, st->saved_ssbo);
1322
1323 st->saved_ssbo_writable_mask = 0;
1324
1325 for (unsigned i = 0; i < 3; i++) {
1326 if (sctx->const_and_shader_buffers[PIPE_SHADER_COMPUTE].writable_mask &
1327 (1u << si_get_shaderbuf_slot(i)))
1328 st->saved_ssbo_writable_mask |= 1 << i;
1329 }
1330 }
1331
1332 void si_restore_qbo_state(struct si_context *sctx, struct si_qbo_state *st)
1333 {
1334 sctx->b.bind_compute_state(&sctx->b, st->saved_compute);
1335
1336 sctx->b.set_constant_buffer(&sctx->b, PIPE_SHADER_COMPUTE, 0, &st->saved_const0);
1337 pipe_resource_reference(&st->saved_const0.buffer, NULL);
1338
1339 sctx->b.set_shader_buffers(&sctx->b, PIPE_SHADER_COMPUTE, 0, 3, st->saved_ssbo,
1340 st->saved_ssbo_writable_mask);
1341 for (unsigned i = 0; i < 3; ++i)
1342 pipe_resource_reference(&st->saved_ssbo[i].buffer, NULL);
1343 }
1344
1345 static void si_emit_db_render_state(struct si_context *sctx)
1346 {
1347 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1348 unsigned db_shader_control, db_render_control, db_count_control;
1349 unsigned initial_cdw = sctx->gfx_cs->current.cdw;
1350
1351 /* DB_RENDER_CONTROL */
1352 if (sctx->dbcb_depth_copy_enabled || sctx->dbcb_stencil_copy_enabled) {
1353 db_render_control = S_028000_DEPTH_COPY(sctx->dbcb_depth_copy_enabled) |
1354 S_028000_STENCIL_COPY(sctx->dbcb_stencil_copy_enabled) |
1355 S_028000_COPY_CENTROID(1) | S_028000_COPY_SAMPLE(sctx->dbcb_copy_sample);
1356 } else if (sctx->db_flush_depth_inplace || sctx->db_flush_stencil_inplace) {
1357 db_render_control = S_028000_DEPTH_COMPRESS_DISABLE(sctx->db_flush_depth_inplace) |
1358 S_028000_STENCIL_COMPRESS_DISABLE(sctx->db_flush_stencil_inplace);
1359 } else {
1360 db_render_control = S_028000_DEPTH_CLEAR_ENABLE(sctx->db_depth_clear) |
1361 S_028000_STENCIL_CLEAR_ENABLE(sctx->db_stencil_clear);
1362 }
1363
1364 /* DB_COUNT_CONTROL (occlusion queries) */
1365 if (sctx->num_occlusion_queries > 0 && !sctx->occlusion_queries_disabled) {
1366 bool perfect = sctx->num_perfect_occlusion_queries > 0;
1367 bool gfx10_perfect = sctx->chip_class >= GFX10 && perfect;
1368
1369 if (sctx->chip_class >= GFX7) {
1370 unsigned log_sample_rate = sctx->framebuffer.log_samples;
1371
1372 db_count_control = S_028004_PERFECT_ZPASS_COUNTS(perfect) |
1373 S_028004_DISABLE_CONSERVATIVE_ZPASS_COUNTS(gfx10_perfect) |
1374 S_028004_SAMPLE_RATE(log_sample_rate) | S_028004_ZPASS_ENABLE(1) |
1375 S_028004_SLICE_EVEN_ENABLE(1) | S_028004_SLICE_ODD_ENABLE(1);
1376 } else {
1377 db_count_control = S_028004_PERFECT_ZPASS_COUNTS(perfect) |
1378 S_028004_SAMPLE_RATE(sctx->framebuffer.log_samples);
1379 }
1380 } else {
1381 /* Disable occlusion queries. */
1382 if (sctx->chip_class >= GFX7) {
1383 db_count_control = 0;
1384 } else {
1385 db_count_control = S_028004_ZPASS_INCREMENT_DISABLE(1);
1386 }
1387 }
1388
1389 radeon_opt_set_context_reg2(sctx, R_028000_DB_RENDER_CONTROL, SI_TRACKED_DB_RENDER_CONTROL,
1390 db_render_control, db_count_control);
1391
1392 /* DB_RENDER_OVERRIDE2 */
1393 radeon_opt_set_context_reg(
1394 sctx, R_028010_DB_RENDER_OVERRIDE2, SI_TRACKED_DB_RENDER_OVERRIDE2,
1395 S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(sctx->db_depth_disable_expclear) |
1396 S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(sctx->db_stencil_disable_expclear) |
1397 S_028010_DECOMPRESS_Z_ON_FLUSH(sctx->framebuffer.nr_samples >= 4));
1398
1399 db_shader_control = sctx->ps_db_shader_control;
1400
1401 /* Bug workaround for smoothing (overrasterization) on GFX6. */
1402 if (sctx->chip_class == GFX6 && sctx->smoothing_enabled) {
1403 db_shader_control &= C_02880C_Z_ORDER;
1404 db_shader_control |= S_02880C_Z_ORDER(V_02880C_LATE_Z);
1405 }
1406
1407 /* Disable the gl_SampleMask fragment shader output if MSAA is disabled. */
1408 if (!rs->multisample_enable)
1409 db_shader_control &= C_02880C_MASK_EXPORT_ENABLE;
1410
1411 if (sctx->screen->info.has_rbplus && !sctx->screen->info.rbplus_allowed)
1412 db_shader_control |= S_02880C_DUAL_QUAD_DISABLE(1);
1413
1414 radeon_opt_set_context_reg(sctx, R_02880C_DB_SHADER_CONTROL, SI_TRACKED_DB_SHADER_CONTROL,
1415 db_shader_control);
1416
1417 if (initial_cdw != sctx->gfx_cs->current.cdw)
1418 sctx->context_roll = true;
1419 }
1420
1421 /*
1422 * format translation
1423 */
1424 static uint32_t si_translate_colorformat(enum pipe_format format)
1425 {
1426 const struct util_format_description *desc = util_format_description(format);
1427 if (!desc)
1428 return V_028C70_COLOR_INVALID;
1429
1430 #define HAS_SIZE(x, y, z, w) \
1431 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
1432 desc->channel[2].size == (z) && desc->channel[3].size == (w))
1433
1434 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1435 return V_028C70_COLOR_10_11_11;
1436
1437 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1438 return V_028C70_COLOR_INVALID;
1439
1440 /* hw cannot support mixed formats (except depth/stencil, since
1441 * stencil is not written to). */
1442 if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1443 return V_028C70_COLOR_INVALID;
1444
1445 switch (desc->nr_channels) {
1446 case 1:
1447 switch (desc->channel[0].size) {
1448 case 8:
1449 return V_028C70_COLOR_8;
1450 case 16:
1451 return V_028C70_COLOR_16;
1452 case 32:
1453 return V_028C70_COLOR_32;
1454 }
1455 break;
1456 case 2:
1457 if (desc->channel[0].size == desc->channel[1].size) {
1458 switch (desc->channel[0].size) {
1459 case 8:
1460 return V_028C70_COLOR_8_8;
1461 case 16:
1462 return V_028C70_COLOR_16_16;
1463 case 32:
1464 return V_028C70_COLOR_32_32;
1465 }
1466 } else if (HAS_SIZE(8, 24, 0, 0)) {
1467 return V_028C70_COLOR_24_8;
1468 } else if (HAS_SIZE(24, 8, 0, 0)) {
1469 return V_028C70_COLOR_8_24;
1470 }
1471 break;
1472 case 3:
1473 if (HAS_SIZE(5, 6, 5, 0)) {
1474 return V_028C70_COLOR_5_6_5;
1475 } else if (HAS_SIZE(32, 8, 24, 0)) {
1476 return V_028C70_COLOR_X24_8_32_FLOAT;
1477 }
1478 break;
1479 case 4:
1480 if (desc->channel[0].size == desc->channel[1].size &&
1481 desc->channel[0].size == desc->channel[2].size &&
1482 desc->channel[0].size == desc->channel[3].size) {
1483 switch (desc->channel[0].size) {
1484 case 4:
1485 return V_028C70_COLOR_4_4_4_4;
1486 case 8:
1487 return V_028C70_COLOR_8_8_8_8;
1488 case 16:
1489 return V_028C70_COLOR_16_16_16_16;
1490 case 32:
1491 return V_028C70_COLOR_32_32_32_32;
1492 }
1493 } else if (HAS_SIZE(5, 5, 5, 1)) {
1494 return V_028C70_COLOR_1_5_5_5;
1495 } else if (HAS_SIZE(1, 5, 5, 5)) {
1496 return V_028C70_COLOR_5_5_5_1;
1497 } else if (HAS_SIZE(10, 10, 10, 2)) {
1498 return V_028C70_COLOR_2_10_10_10;
1499 }
1500 break;
1501 }
1502 return V_028C70_COLOR_INVALID;
1503 }
1504
1505 static uint32_t si_colorformat_endian_swap(uint32_t colorformat)
1506 {
1507 if (SI_BIG_ENDIAN) {
1508 switch (colorformat) {
1509 /* 8-bit buffers. */
1510 case V_028C70_COLOR_8:
1511 return V_028C70_ENDIAN_NONE;
1512
1513 /* 16-bit buffers. */
1514 case V_028C70_COLOR_5_6_5:
1515 case V_028C70_COLOR_1_5_5_5:
1516 case V_028C70_COLOR_4_4_4_4:
1517 case V_028C70_COLOR_16:
1518 case V_028C70_COLOR_8_8:
1519 return V_028C70_ENDIAN_8IN16;
1520
1521 /* 32-bit buffers. */
1522 case V_028C70_COLOR_8_8_8_8:
1523 case V_028C70_COLOR_2_10_10_10:
1524 case V_028C70_COLOR_8_24:
1525 case V_028C70_COLOR_24_8:
1526 case V_028C70_COLOR_16_16:
1527 return V_028C70_ENDIAN_8IN32;
1528
1529 /* 64-bit buffers. */
1530 case V_028C70_COLOR_16_16_16_16:
1531 return V_028C70_ENDIAN_8IN16;
1532
1533 case V_028C70_COLOR_32_32:
1534 return V_028C70_ENDIAN_8IN32;
1535
1536 /* 128-bit buffers. */
1537 case V_028C70_COLOR_32_32_32_32:
1538 return V_028C70_ENDIAN_8IN32;
1539 default:
1540 return V_028C70_ENDIAN_NONE; /* Unsupported. */
1541 }
1542 } else {
1543 return V_028C70_ENDIAN_NONE;
1544 }
1545 }
1546
1547 static uint32_t si_translate_dbformat(enum pipe_format format)
1548 {
1549 switch (format) {
1550 case PIPE_FORMAT_Z16_UNORM:
1551 return V_028040_Z_16;
1552 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1553 case PIPE_FORMAT_X8Z24_UNORM:
1554 case PIPE_FORMAT_Z24X8_UNORM:
1555 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1556 return V_028040_Z_24; /* deprecated on AMD GCN */
1557 case PIPE_FORMAT_Z32_FLOAT:
1558 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1559 return V_028040_Z_32_FLOAT;
1560 default:
1561 return V_028040_Z_INVALID;
1562 }
1563 }
1564
1565 /*
1566 * Texture translation
1567 */
1568
1569 static uint32_t si_translate_texformat(struct pipe_screen *screen, enum pipe_format format,
1570 const struct util_format_description *desc,
1571 int first_non_void)
1572 {
1573 struct si_screen *sscreen = (struct si_screen *)screen;
1574 bool uniform = true;
1575 int i;
1576
1577 assert(sscreen->info.chip_class <= GFX9);
1578
1579 /* Colorspace (return non-RGB formats directly). */
1580 switch (desc->colorspace) {
1581 /* Depth stencil formats */
1582 case UTIL_FORMAT_COLORSPACE_ZS:
1583 switch (format) {
1584 case PIPE_FORMAT_Z16_UNORM:
1585 return V_008F14_IMG_DATA_FORMAT_16;
1586 case PIPE_FORMAT_X24S8_UINT:
1587 case PIPE_FORMAT_S8X24_UINT:
1588 /*
1589 * Implemented as an 8_8_8_8 data format to fix texture
1590 * gathers in stencil sampling. This affects at least
1591 * GL45-CTS.texture_cube_map_array.sampling on GFX8.
1592 */
1593 if (sscreen->info.chip_class <= GFX8)
1594 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
1595
1596 if (format == PIPE_FORMAT_X24S8_UINT)
1597 return V_008F14_IMG_DATA_FORMAT_8_24;
1598 else
1599 return V_008F14_IMG_DATA_FORMAT_24_8;
1600 case PIPE_FORMAT_Z24X8_UNORM:
1601 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1602 return V_008F14_IMG_DATA_FORMAT_8_24;
1603 case PIPE_FORMAT_X8Z24_UNORM:
1604 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1605 return V_008F14_IMG_DATA_FORMAT_24_8;
1606 case PIPE_FORMAT_S8_UINT:
1607 return V_008F14_IMG_DATA_FORMAT_8;
1608 case PIPE_FORMAT_Z32_FLOAT:
1609 return V_008F14_IMG_DATA_FORMAT_32;
1610 case PIPE_FORMAT_X32_S8X24_UINT:
1611 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1612 return V_008F14_IMG_DATA_FORMAT_X24_8_32;
1613 default:
1614 goto out_unknown;
1615 }
1616
1617 case UTIL_FORMAT_COLORSPACE_YUV:
1618 goto out_unknown; /* TODO */
1619
1620 case UTIL_FORMAT_COLORSPACE_SRGB:
1621 if (desc->nr_channels != 4 && desc->nr_channels != 1)
1622 goto out_unknown;
1623 break;
1624
1625 default:
1626 break;
1627 }
1628
1629 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
1630 if (!sscreen->info.has_format_bc1_through_bc7)
1631 goto out_unknown;
1632
1633 switch (format) {
1634 case PIPE_FORMAT_RGTC1_SNORM:
1635 case PIPE_FORMAT_LATC1_SNORM:
1636 case PIPE_FORMAT_RGTC1_UNORM:
1637 case PIPE_FORMAT_LATC1_UNORM:
1638 return V_008F14_IMG_DATA_FORMAT_BC4;
1639 case PIPE_FORMAT_RGTC2_SNORM:
1640 case PIPE_FORMAT_LATC2_SNORM:
1641 case PIPE_FORMAT_RGTC2_UNORM:
1642 case PIPE_FORMAT_LATC2_UNORM:
1643 return V_008F14_IMG_DATA_FORMAT_BC5;
1644 default:
1645 goto out_unknown;
1646 }
1647 }
1648
1649 if (desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
1650 (sscreen->info.family == CHIP_STONEY || sscreen->info.family == CHIP_VEGA10 ||
1651 sscreen->info.family == CHIP_RAVEN)) {
1652 switch (format) {
1653 case PIPE_FORMAT_ETC1_RGB8:
1654 case PIPE_FORMAT_ETC2_RGB8:
1655 case PIPE_FORMAT_ETC2_SRGB8:
1656 return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
1657 case PIPE_FORMAT_ETC2_RGB8A1:
1658 case PIPE_FORMAT_ETC2_SRGB8A1:
1659 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
1660 case PIPE_FORMAT_ETC2_RGBA8:
1661 case PIPE_FORMAT_ETC2_SRGBA8:
1662 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
1663 case PIPE_FORMAT_ETC2_R11_UNORM:
1664 case PIPE_FORMAT_ETC2_R11_SNORM:
1665 return V_008F14_IMG_DATA_FORMAT_ETC2_R;
1666 case PIPE_FORMAT_ETC2_RG11_UNORM:
1667 case PIPE_FORMAT_ETC2_RG11_SNORM:
1668 return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
1669 default:
1670 goto out_unknown;
1671 }
1672 }
1673
1674 if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
1675 if (!sscreen->info.has_format_bc1_through_bc7)
1676 goto out_unknown;
1677
1678 switch (format) {
1679 case PIPE_FORMAT_BPTC_RGBA_UNORM:
1680 case PIPE_FORMAT_BPTC_SRGBA:
1681 return V_008F14_IMG_DATA_FORMAT_BC7;
1682 case PIPE_FORMAT_BPTC_RGB_FLOAT:
1683 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
1684 return V_008F14_IMG_DATA_FORMAT_BC6;
1685 default:
1686 goto out_unknown;
1687 }
1688 }
1689
1690 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
1691 switch (format) {
1692 case PIPE_FORMAT_R8G8_B8G8_UNORM:
1693 case PIPE_FORMAT_G8R8_B8R8_UNORM:
1694 return V_008F14_IMG_DATA_FORMAT_GB_GR;
1695 case PIPE_FORMAT_G8R8_G8B8_UNORM:
1696 case PIPE_FORMAT_R8G8_R8B8_UNORM:
1697 return V_008F14_IMG_DATA_FORMAT_BG_RG;
1698 default:
1699 goto out_unknown;
1700 }
1701 }
1702
1703 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
1704 if (!sscreen->info.has_format_bc1_through_bc7)
1705 goto out_unknown;
1706
1707 switch (format) {
1708 case PIPE_FORMAT_DXT1_RGB:
1709 case PIPE_FORMAT_DXT1_RGBA:
1710 case PIPE_FORMAT_DXT1_SRGB:
1711 case PIPE_FORMAT_DXT1_SRGBA:
1712 return V_008F14_IMG_DATA_FORMAT_BC1;
1713 case PIPE_FORMAT_DXT3_RGBA:
1714 case PIPE_FORMAT_DXT3_SRGBA:
1715 return V_008F14_IMG_DATA_FORMAT_BC2;
1716 case PIPE_FORMAT_DXT5_RGBA:
1717 case PIPE_FORMAT_DXT5_SRGBA:
1718 return V_008F14_IMG_DATA_FORMAT_BC3;
1719 default:
1720 goto out_unknown;
1721 }
1722 }
1723
1724 if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
1725 return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
1726 } else if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
1727 return V_008F14_IMG_DATA_FORMAT_10_11_11;
1728 }
1729
1730 /* R8G8Bx_SNORM - TODO CxV8U8 */
1731
1732 /* hw cannot support mixed formats (except depth/stencil, since only
1733 * depth is read).*/
1734 if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1735 goto out_unknown;
1736
1737 /* See whether the components are of the same size. */
1738 for (i = 1; i < desc->nr_channels; i++) {
1739 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
1740 }
1741
1742 /* Non-uniform formats. */
1743 if (!uniform) {
1744 switch (desc->nr_channels) {
1745 case 3:
1746 if (desc->channel[0].size == 5 && desc->channel[1].size == 6 &&
1747 desc->channel[2].size == 5) {
1748 return V_008F14_IMG_DATA_FORMAT_5_6_5;
1749 }
1750 goto out_unknown;
1751 case 4:
1752 if (desc->channel[0].size == 5 && desc->channel[1].size == 5 &&
1753 desc->channel[2].size == 5 && desc->channel[3].size == 1) {
1754 return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
1755 }
1756 if (desc->channel[0].size == 1 && desc->channel[1].size == 5 &&
1757 desc->channel[2].size == 5 && desc->channel[3].size == 5) {
1758 return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
1759 }
1760 if (desc->channel[0].size == 10 && desc->channel[1].size == 10 &&
1761 desc->channel[2].size == 10 && desc->channel[3].size == 2) {
1762 return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
1763 }
1764 goto out_unknown;
1765 }
1766 goto out_unknown;
1767 }
1768
1769 if (first_non_void < 0 || first_non_void > 3)
1770 goto out_unknown;
1771
1772 /* uniform formats */
1773 switch (desc->channel[first_non_void].size) {
1774 case 4:
1775 switch (desc->nr_channels) {
1776 #if 0 /* Not supported for render targets */
1777 case 2:
1778 return V_008F14_IMG_DATA_FORMAT_4_4;
1779 #endif
1780 case 4:
1781 return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
1782 }
1783 break;
1784 case 8:
1785 switch (desc->nr_channels) {
1786 case 1:
1787 return V_008F14_IMG_DATA_FORMAT_8;
1788 case 2:
1789 return V_008F14_IMG_DATA_FORMAT_8_8;
1790 case 4:
1791 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
1792 }
1793 break;
1794 case 16:
1795 switch (desc->nr_channels) {
1796 case 1:
1797 return V_008F14_IMG_DATA_FORMAT_16;
1798 case 2:
1799 return V_008F14_IMG_DATA_FORMAT_16_16;
1800 case 4:
1801 return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
1802 }
1803 break;
1804 case 32:
1805 switch (desc->nr_channels) {
1806 case 1:
1807 return V_008F14_IMG_DATA_FORMAT_32;
1808 case 2:
1809 return V_008F14_IMG_DATA_FORMAT_32_32;
1810 #if 0 /* Not supported for render targets */
1811 case 3:
1812 return V_008F14_IMG_DATA_FORMAT_32_32_32;
1813 #endif
1814 case 4:
1815 return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
1816 }
1817 }
1818
1819 out_unknown:
1820 return ~0;
1821 }
1822
1823 static unsigned si_tex_wrap(unsigned wrap)
1824 {
1825 switch (wrap) {
1826 default:
1827 case PIPE_TEX_WRAP_REPEAT:
1828 return V_008F30_SQ_TEX_WRAP;
1829 case PIPE_TEX_WRAP_CLAMP:
1830 return V_008F30_SQ_TEX_CLAMP_HALF_BORDER;
1831 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1832 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
1833 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1834 return V_008F30_SQ_TEX_CLAMP_BORDER;
1835 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1836 return V_008F30_SQ_TEX_MIRROR;
1837 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1838 return V_008F30_SQ_TEX_MIRROR_ONCE_HALF_BORDER;
1839 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1840 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
1841 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1842 return V_008F30_SQ_TEX_MIRROR_ONCE_BORDER;
1843 }
1844 }
1845
1846 static unsigned si_tex_mipfilter(unsigned filter)
1847 {
1848 switch (filter) {
1849 case PIPE_TEX_MIPFILTER_NEAREST:
1850 return V_008F38_SQ_TEX_Z_FILTER_POINT;
1851 case PIPE_TEX_MIPFILTER_LINEAR:
1852 return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
1853 default:
1854 case PIPE_TEX_MIPFILTER_NONE:
1855 return V_008F38_SQ_TEX_Z_FILTER_NONE;
1856 }
1857 }
1858
1859 static unsigned si_tex_compare(unsigned compare)
1860 {
1861 switch (compare) {
1862 default:
1863 case PIPE_FUNC_NEVER:
1864 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
1865 case PIPE_FUNC_LESS:
1866 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
1867 case PIPE_FUNC_EQUAL:
1868 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
1869 case PIPE_FUNC_LEQUAL:
1870 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
1871 case PIPE_FUNC_GREATER:
1872 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
1873 case PIPE_FUNC_NOTEQUAL:
1874 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
1875 case PIPE_FUNC_GEQUAL:
1876 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
1877 case PIPE_FUNC_ALWAYS:
1878 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
1879 }
1880 }
1881
1882 static unsigned si_tex_dim(struct si_screen *sscreen, struct si_texture *tex, unsigned view_target,
1883 unsigned nr_samples)
1884 {
1885 unsigned res_target = tex->buffer.b.b.target;
1886
1887 if (view_target == PIPE_TEXTURE_CUBE || view_target == PIPE_TEXTURE_CUBE_ARRAY)
1888 res_target = view_target;
1889 /* If interpreting cubemaps as something else, set 2D_ARRAY. */
1890 else if (res_target == PIPE_TEXTURE_CUBE || res_target == PIPE_TEXTURE_CUBE_ARRAY)
1891 res_target = PIPE_TEXTURE_2D_ARRAY;
1892
1893 /* GFX9 allocates 1D textures as 2D. */
1894 if ((res_target == PIPE_TEXTURE_1D || res_target == PIPE_TEXTURE_1D_ARRAY) &&
1895 sscreen->info.chip_class == GFX9 &&
1896 tex->surface.u.gfx9.resource_type == RADEON_RESOURCE_2D) {
1897 if (res_target == PIPE_TEXTURE_1D)
1898 res_target = PIPE_TEXTURE_2D;
1899 else
1900 res_target = PIPE_TEXTURE_2D_ARRAY;
1901 }
1902
1903 switch (res_target) {
1904 default:
1905 case PIPE_TEXTURE_1D:
1906 return V_008F1C_SQ_RSRC_IMG_1D;
1907 case PIPE_TEXTURE_1D_ARRAY:
1908 return V_008F1C_SQ_RSRC_IMG_1D_ARRAY;
1909 case PIPE_TEXTURE_2D:
1910 case PIPE_TEXTURE_RECT:
1911 return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA : V_008F1C_SQ_RSRC_IMG_2D;
1912 case PIPE_TEXTURE_2D_ARRAY:
1913 return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY : V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
1914 case PIPE_TEXTURE_3D:
1915 return V_008F1C_SQ_RSRC_IMG_3D;
1916 case PIPE_TEXTURE_CUBE:
1917 case PIPE_TEXTURE_CUBE_ARRAY:
1918 return V_008F1C_SQ_RSRC_IMG_CUBE;
1919 }
1920 }
1921
1922 /*
1923 * Format support testing
1924 */
1925
1926 static bool si_is_sampler_format_supported(struct pipe_screen *screen, enum pipe_format format)
1927 {
1928 struct si_screen *sscreen = (struct si_screen *)screen;
1929
1930 if (sscreen->info.chip_class >= GFX10) {
1931 const struct gfx10_format *fmt = &gfx10_format_table[format];
1932 if (!fmt->img_format || fmt->buffers_only)
1933 return false;
1934 return true;
1935 }
1936
1937 const struct util_format_description *desc = util_format_description(format);
1938 if (!desc)
1939 return false;
1940
1941 return si_translate_texformat(screen, format, desc,
1942 util_format_get_first_non_void_channel(format)) != ~0U;
1943 }
1944
1945 static uint32_t si_translate_buffer_dataformat(struct pipe_screen *screen,
1946 const struct util_format_description *desc,
1947 int first_non_void)
1948 {
1949 int i;
1950
1951 assert(((struct si_screen *)screen)->info.chip_class <= GFX9);
1952
1953 if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
1954 return V_008F0C_BUF_DATA_FORMAT_10_11_11;
1955
1956 assert(first_non_void >= 0);
1957
1958 if (desc->nr_channels == 4 && desc->channel[0].size == 10 && desc->channel[1].size == 10 &&
1959 desc->channel[2].size == 10 && desc->channel[3].size == 2)
1960 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
1961
1962 /* See whether the components are of the same size. */
1963 for (i = 0; i < desc->nr_channels; i++) {
1964 if (desc->channel[first_non_void].size != desc->channel[i].size)
1965 return V_008F0C_BUF_DATA_FORMAT_INVALID;
1966 }
1967
1968 switch (desc->channel[first_non_void].size) {
1969 case 8:
1970 switch (desc->nr_channels) {
1971 case 1:
1972 case 3: /* 3 loads */
1973 return V_008F0C_BUF_DATA_FORMAT_8;
1974 case 2:
1975 return V_008F0C_BUF_DATA_FORMAT_8_8;
1976 case 4:
1977 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
1978 }
1979 break;
1980 case 16:
1981 switch (desc->nr_channels) {
1982 case 1:
1983 case 3: /* 3 loads */
1984 return V_008F0C_BUF_DATA_FORMAT_16;
1985 case 2:
1986 return V_008F0C_BUF_DATA_FORMAT_16_16;
1987 case 4:
1988 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
1989 }
1990 break;
1991 case 32:
1992 switch (desc->nr_channels) {
1993 case 1:
1994 return V_008F0C_BUF_DATA_FORMAT_32;
1995 case 2:
1996 return V_008F0C_BUF_DATA_FORMAT_32_32;
1997 case 3:
1998 return V_008F0C_BUF_DATA_FORMAT_32_32_32;
1999 case 4:
2000 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2001 }
2002 break;
2003 case 64:
2004 /* Legacy double formats. */
2005 switch (desc->nr_channels) {
2006 case 1: /* 1 load */
2007 return V_008F0C_BUF_DATA_FORMAT_32_32;
2008 case 2: /* 1 load */
2009 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2010 case 3: /* 3 loads */
2011 return V_008F0C_BUF_DATA_FORMAT_32_32;
2012 case 4: /* 2 loads */
2013 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2014 }
2015 break;
2016 }
2017
2018 return V_008F0C_BUF_DATA_FORMAT_INVALID;
2019 }
2020
2021 static uint32_t si_translate_buffer_numformat(struct pipe_screen *screen,
2022 const struct util_format_description *desc,
2023 int first_non_void)
2024 {
2025 assert(((struct si_screen *)screen)->info.chip_class <= GFX9);
2026
2027 if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
2028 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
2029
2030 assert(first_non_void >= 0);
2031
2032 switch (desc->channel[first_non_void].type) {
2033 case UTIL_FORMAT_TYPE_SIGNED:
2034 case UTIL_FORMAT_TYPE_FIXED:
2035 if (desc->channel[first_non_void].size >= 32 || desc->channel[first_non_void].pure_integer)
2036 return V_008F0C_BUF_NUM_FORMAT_SINT;
2037 else if (desc->channel[first_non_void].normalized)
2038 return V_008F0C_BUF_NUM_FORMAT_SNORM;
2039 else
2040 return V_008F0C_BUF_NUM_FORMAT_SSCALED;
2041 break;
2042 case UTIL_FORMAT_TYPE_UNSIGNED:
2043 if (desc->channel[first_non_void].size >= 32 || desc->channel[first_non_void].pure_integer)
2044 return V_008F0C_BUF_NUM_FORMAT_UINT;
2045 else if (desc->channel[first_non_void].normalized)
2046 return V_008F0C_BUF_NUM_FORMAT_UNORM;
2047 else
2048 return V_008F0C_BUF_NUM_FORMAT_USCALED;
2049 break;
2050 case UTIL_FORMAT_TYPE_FLOAT:
2051 default:
2052 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
2053 }
2054 }
2055
2056 static unsigned si_is_vertex_format_supported(struct pipe_screen *screen, enum pipe_format format,
2057 unsigned usage)
2058 {
2059 struct si_screen *sscreen = (struct si_screen *)screen;
2060 const struct util_format_description *desc;
2061 int first_non_void;
2062 unsigned data_format;
2063
2064 assert((usage & ~(PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_VERTEX_BUFFER)) ==
2065 0);
2066
2067 desc = util_format_description(format);
2068 if (!desc)
2069 return 0;
2070
2071 /* There are no native 8_8_8 or 16_16_16 data formats, and we currently
2072 * select 8_8_8_8 and 16_16_16_16 instead. This works reasonably well
2073 * for read-only access (with caveats surrounding bounds checks), but
2074 * obviously fails for write access which we have to implement for
2075 * shader images. Luckily, OpenGL doesn't expect this to be supported
2076 * anyway, and so the only impact is on PBO uploads / downloads, which
2077 * shouldn't be expected to be fast for GL_RGB anyway.
2078 */
2079 if (desc->block.bits == 3 * 8 || desc->block.bits == 3 * 16) {
2080 if (usage & (PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW)) {
2081 usage &= ~(PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW);
2082 if (!usage)
2083 return 0;
2084 }
2085 }
2086
2087 if (sscreen->info.chip_class >= GFX10) {
2088 const struct gfx10_format *fmt = &gfx10_format_table[format];
2089 if (!fmt->img_format || fmt->img_format >= 128)
2090 return 0;
2091 return usage;
2092 }
2093
2094 first_non_void = util_format_get_first_non_void_channel(format);
2095 data_format = si_translate_buffer_dataformat(screen, desc, first_non_void);
2096 if (data_format == V_008F0C_BUF_DATA_FORMAT_INVALID)
2097 return 0;
2098
2099 return usage;
2100 }
2101
2102 static bool si_is_colorbuffer_format_supported(enum pipe_format format)
2103 {
2104 return si_translate_colorformat(format) != V_028C70_COLOR_INVALID &&
2105 si_translate_colorswap(format, false) != ~0U;
2106 }
2107
2108 static bool si_is_zs_format_supported(enum pipe_format format)
2109 {
2110 return si_translate_dbformat(format) != V_028040_Z_INVALID;
2111 }
2112
2113 static bool si_is_format_supported(struct pipe_screen *screen, enum pipe_format format,
2114 enum pipe_texture_target target, unsigned sample_count,
2115 unsigned storage_sample_count, unsigned usage)
2116 {
2117 struct si_screen *sscreen = (struct si_screen *)screen;
2118 unsigned retval = 0;
2119
2120 if (target >= PIPE_MAX_TEXTURE_TYPES) {
2121 PRINT_ERR("radeonsi: unsupported texture type %d\n", target);
2122 return false;
2123 }
2124
2125 if (MAX2(1, sample_count) < MAX2(1, storage_sample_count))
2126 return false;
2127
2128 if (sample_count > 1) {
2129 if (!screen->get_param(screen, PIPE_CAP_TEXTURE_MULTISAMPLE))
2130 return false;
2131
2132 /* Only power-of-two sample counts are supported. */
2133 if (!util_is_power_of_two_or_zero(sample_count) ||
2134 !util_is_power_of_two_or_zero(storage_sample_count))
2135 return false;
2136
2137 /* Chips with 1 RB don't increment occlusion queries at 16x MSAA sample rate,
2138 * so don't expose 16 samples there.
2139 */
2140 const unsigned max_eqaa_samples = sscreen->info.num_render_backends == 1 ? 8 : 16;
2141 const unsigned max_samples = 8;
2142
2143 /* MSAA support without framebuffer attachments. */
2144 if (format == PIPE_FORMAT_NONE && sample_count <= max_eqaa_samples)
2145 return true;
2146
2147 if (!sscreen->info.has_eqaa_surface_allocator || util_format_is_depth_or_stencil(format)) {
2148 /* Color without EQAA or depth/stencil. */
2149 if (sample_count > max_samples || sample_count != storage_sample_count)
2150 return false;
2151 } else {
2152 /* Color with EQAA. */
2153 if (sample_count > max_eqaa_samples || storage_sample_count > max_samples)
2154 return false;
2155 }
2156 }
2157
2158 if (usage & (PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_SHADER_IMAGE)) {
2159 if (target == PIPE_BUFFER) {
2160 retval |= si_is_vertex_format_supported(
2161 screen, format, usage & (PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_SHADER_IMAGE));
2162 } else {
2163 if (si_is_sampler_format_supported(screen, format))
2164 retval |= usage & (PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_SHADER_IMAGE);
2165 }
2166 }
2167
2168 if ((usage & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT |
2169 PIPE_BIND_SHARED | PIPE_BIND_BLENDABLE)) &&
2170 si_is_colorbuffer_format_supported(format)) {
2171 retval |= usage & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT |
2172 PIPE_BIND_SHARED);
2173 if (!util_format_is_pure_integer(format) && !util_format_is_depth_or_stencil(format))
2174 retval |= usage & PIPE_BIND_BLENDABLE;
2175 }
2176
2177 if ((usage & PIPE_BIND_DEPTH_STENCIL) && si_is_zs_format_supported(format)) {
2178 retval |= PIPE_BIND_DEPTH_STENCIL;
2179 }
2180
2181 if (usage & PIPE_BIND_VERTEX_BUFFER) {
2182 retval |= si_is_vertex_format_supported(screen, format, PIPE_BIND_VERTEX_BUFFER);
2183 }
2184
2185 if ((usage & PIPE_BIND_LINEAR) && !util_format_is_compressed(format) &&
2186 !(usage & PIPE_BIND_DEPTH_STENCIL))
2187 retval |= PIPE_BIND_LINEAR;
2188
2189 return retval == usage;
2190 }
2191
2192 /*
2193 * framebuffer handling
2194 */
2195
2196 static void si_choose_spi_color_formats(struct si_surface *surf, unsigned format, unsigned swap,
2197 unsigned ntype, bool is_depth)
2198 {
2199 /* Alpha is needed for alpha-to-coverage.
2200 * Blending may be with or without alpha.
2201 */
2202 unsigned normal = 0; /* most optimal, may not support blending or export alpha */
2203 unsigned alpha = 0; /* exports alpha, but may not support blending */
2204 unsigned blend = 0; /* supports blending, but may not export alpha */
2205 unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */
2206
2207 /* Choose the SPI color formats. These are required values for RB+.
2208 * Other chips have multiple choices, though they are not necessarily better.
2209 */
2210 switch (format) {
2211 case V_028C70_COLOR_5_6_5:
2212 case V_028C70_COLOR_1_5_5_5:
2213 case V_028C70_COLOR_5_5_5_1:
2214 case V_028C70_COLOR_4_4_4_4:
2215 case V_028C70_COLOR_10_11_11:
2216 case V_028C70_COLOR_11_11_10:
2217 case V_028C70_COLOR_8:
2218 case V_028C70_COLOR_8_8:
2219 case V_028C70_COLOR_8_8_8_8:
2220 case V_028C70_COLOR_10_10_10_2:
2221 case V_028C70_COLOR_2_10_10_10:
2222 if (ntype == V_028C70_NUMBER_UINT)
2223 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
2224 else if (ntype == V_028C70_NUMBER_SINT)
2225 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
2226 else
2227 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
2228 break;
2229
2230 case V_028C70_COLOR_16:
2231 case V_028C70_COLOR_16_16:
2232 case V_028C70_COLOR_16_16_16_16:
2233 if (ntype == V_028C70_NUMBER_UNORM || ntype == V_028C70_NUMBER_SNORM) {
2234 /* UNORM16 and SNORM16 don't support blending */
2235 if (ntype == V_028C70_NUMBER_UNORM)
2236 normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;
2237 else
2238 normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;
2239
2240 /* Use 32 bits per channel for blending. */
2241 if (format == V_028C70_COLOR_16) {
2242 if (swap == V_028C70_SWAP_STD) { /* R */
2243 blend = V_028714_SPI_SHADER_32_R;
2244 blend_alpha = V_028714_SPI_SHADER_32_AR;
2245 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
2246 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
2247 else
2248 assert(0);
2249 } else if (format == V_028C70_COLOR_16_16) {
2250 if (swap == V_028C70_SWAP_STD) { /* RG */
2251 blend = V_028714_SPI_SHADER_32_GR;
2252 blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2253 } else if (swap == V_028C70_SWAP_ALT) /* RA */
2254 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
2255 else
2256 assert(0);
2257 } else /* 16_16_16_16 */
2258 blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2259 } else if (ntype == V_028C70_NUMBER_UINT)
2260 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
2261 else if (ntype == V_028C70_NUMBER_SINT)
2262 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
2263 else if (ntype == V_028C70_NUMBER_FLOAT)
2264 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
2265 else
2266 assert(0);
2267 break;
2268
2269 case V_028C70_COLOR_32:
2270 if (swap == V_028C70_SWAP_STD) { /* R */
2271 blend = normal = V_028714_SPI_SHADER_32_R;
2272 alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;
2273 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
2274 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
2275 else
2276 assert(0);
2277 break;
2278
2279 case V_028C70_COLOR_32_32:
2280 if (swap == V_028C70_SWAP_STD) { /* RG */
2281 blend = normal = V_028714_SPI_SHADER_32_GR;
2282 alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2283 } else if (swap == V_028C70_SWAP_ALT) /* RA */
2284 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
2285 else
2286 assert(0);
2287 break;
2288
2289 case V_028C70_COLOR_32_32_32_32:
2290 case V_028C70_COLOR_8_24:
2291 case V_028C70_COLOR_24_8:
2292 case V_028C70_COLOR_X24_8_32_FLOAT:
2293 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
2294 break;
2295
2296 default:
2297 assert(0);
2298 return;
2299 }
2300
2301 /* The DB->CB copy needs 32_ABGR. */
2302 if (is_depth)
2303 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
2304
2305 surf->spi_shader_col_format = normal;
2306 surf->spi_shader_col_format_alpha = alpha;
2307 surf->spi_shader_col_format_blend = blend;
2308 surf->spi_shader_col_format_blend_alpha = blend_alpha;
2309 }
2310
2311 static void si_initialize_color_surface(struct si_context *sctx, struct si_surface *surf)
2312 {
2313 struct si_texture *tex = (struct si_texture *)surf->base.texture;
2314 unsigned color_info, color_attrib;
2315 unsigned format, swap, ntype, endian;
2316 const struct util_format_description *desc;
2317 int firstchan;
2318 unsigned blend_clamp = 0, blend_bypass = 0;
2319
2320 desc = util_format_description(surf->base.format);
2321 for (firstchan = 0; firstchan < 4; firstchan++) {
2322 if (desc->channel[firstchan].type != UTIL_FORMAT_TYPE_VOID) {
2323 break;
2324 }
2325 }
2326 if (firstchan == 4 || desc->channel[firstchan].type == UTIL_FORMAT_TYPE_FLOAT) {
2327 ntype = V_028C70_NUMBER_FLOAT;
2328 } else {
2329 ntype = V_028C70_NUMBER_UNORM;
2330 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
2331 ntype = V_028C70_NUMBER_SRGB;
2332 else if (desc->channel[firstchan].type == UTIL_FORMAT_TYPE_SIGNED) {
2333 if (desc->channel[firstchan].pure_integer) {
2334 ntype = V_028C70_NUMBER_SINT;
2335 } else {
2336 assert(desc->channel[firstchan].normalized);
2337 ntype = V_028C70_NUMBER_SNORM;
2338 }
2339 } else if (desc->channel[firstchan].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2340 if (desc->channel[firstchan].pure_integer) {
2341 ntype = V_028C70_NUMBER_UINT;
2342 } else {
2343 assert(desc->channel[firstchan].normalized);
2344 ntype = V_028C70_NUMBER_UNORM;
2345 }
2346 }
2347 }
2348
2349 format = si_translate_colorformat(surf->base.format);
2350 if (format == V_028C70_COLOR_INVALID) {
2351 PRINT_ERR("Invalid CB format: %d, disabling CB.\n", surf->base.format);
2352 }
2353 assert(format != V_028C70_COLOR_INVALID);
2354 swap = si_translate_colorswap(surf->base.format, false);
2355 endian = si_colorformat_endian_swap(format);
2356
2357 /* blend clamp should be set for all NORM/SRGB types */
2358 if (ntype == V_028C70_NUMBER_UNORM || ntype == V_028C70_NUMBER_SNORM ||
2359 ntype == V_028C70_NUMBER_SRGB)
2360 blend_clamp = 1;
2361
2362 /* set blend bypass according to docs if SINT/UINT or
2363 8/24 COLOR variants */
2364 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
2365 format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
2366 format == V_028C70_COLOR_X24_8_32_FLOAT) {
2367 blend_clamp = 0;
2368 blend_bypass = 1;
2369 }
2370
2371 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) {
2372 if (format == V_028C70_COLOR_8 || format == V_028C70_COLOR_8_8 ||
2373 format == V_028C70_COLOR_8_8_8_8)
2374 surf->color_is_int8 = true;
2375 else if (format == V_028C70_COLOR_10_10_10_2 || format == V_028C70_COLOR_2_10_10_10)
2376 surf->color_is_int10 = true;
2377 }
2378
2379 color_info =
2380 S_028C70_FORMAT(format) | S_028C70_COMP_SWAP(swap) | S_028C70_BLEND_CLAMP(blend_clamp) |
2381 S_028C70_BLEND_BYPASS(blend_bypass) | S_028C70_SIMPLE_FLOAT(1) |
2382 S_028C70_ROUND_MODE(ntype != V_028C70_NUMBER_UNORM && ntype != V_028C70_NUMBER_SNORM &&
2383 ntype != V_028C70_NUMBER_SRGB && format != V_028C70_COLOR_8_24 &&
2384 format != V_028C70_COLOR_24_8) |
2385 S_028C70_NUMBER_TYPE(ntype) | S_028C70_ENDIAN(endian);
2386
2387 /* Intensity is implemented as Red, so treat it that way. */
2388 color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == PIPE_SWIZZLE_1 ||
2389 util_format_is_intensity(surf->base.format));
2390
2391 if (tex->buffer.b.b.nr_samples > 1) {
2392 unsigned log_samples = util_logbase2(tex->buffer.b.b.nr_samples);
2393 unsigned log_fragments = util_logbase2(tex->buffer.b.b.nr_storage_samples);
2394
2395 color_attrib |= S_028C74_NUM_SAMPLES(log_samples) | S_028C74_NUM_FRAGMENTS(log_fragments);
2396
2397 if (tex->surface.fmask_offset) {
2398 color_info |= S_028C70_COMPRESSION(1);
2399 unsigned fmask_bankh = util_logbase2(tex->surface.u.legacy.fmask.bankh);
2400
2401 if (sctx->chip_class == GFX6) {
2402 /* due to a hw bug, FMASK_BANK_HEIGHT must be set on GFX6 too */
2403 color_attrib |= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh);
2404 }
2405 }
2406 }
2407
2408 if (sctx->chip_class >= GFX10) {
2409 unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
2410
2411 /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
2412 64 for APU because all of our APUs to date use DIMMs which have
2413 a request granularity size of 64B while all other chips have a
2414 32B request size */
2415 if (!sctx->screen->info.has_dedicated_vram)
2416 min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
2417
2418 surf->cb_dcc_control = S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(V_028C78_MAX_BLOCK_SIZE_256B) |
2419 S_028C78_MAX_COMPRESSED_BLOCK_SIZE(tex->surface.u.gfx9.dcc.max_compressed_block_size) |
2420 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
2421 S_028C78_INDEPENDENT_64B_BLOCKS(tex->surface.u.gfx9.dcc.independent_64B_blocks) |
2422 S_028C78_INDEPENDENT_128B_BLOCKS(tex->surface.u.gfx9.dcc.independent_128B_blocks);
2423 } else if (sctx->chip_class >= GFX8) {
2424 unsigned max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_256B;
2425 unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
2426
2427 /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
2428 64 for APU because all of our APUs to date use DIMMs which have
2429 a request granularity size of 64B while all other chips have a
2430 32B request size */
2431 if (!sctx->screen->info.has_dedicated_vram)
2432 min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
2433
2434 if (tex->buffer.b.b.nr_storage_samples > 1) {
2435 if (tex->surface.bpe == 1)
2436 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
2437 else if (tex->surface.bpe == 2)
2438 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
2439 }
2440
2441 surf->cb_dcc_control = S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
2442 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
2443 S_028C78_INDEPENDENT_64B_BLOCKS(1);
2444 }
2445
2446 /* This must be set for fast clear to work without FMASK. */
2447 if (!tex->surface.fmask_size && sctx->chip_class == GFX6) {
2448 unsigned bankh = util_logbase2(tex->surface.u.legacy.bankh);
2449 color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
2450 }
2451
2452 /* GFX10 field has the same base shift as the GFX6 field */
2453 unsigned color_view = S_028C6C_SLICE_START(surf->base.u.tex.first_layer) |
2454 S_028C6C_SLICE_MAX_GFX10(surf->base.u.tex.last_layer);
2455 unsigned mip0_depth = util_max_layer(&tex->buffer.b.b, 0);
2456
2457 if (sctx->chip_class >= GFX10) {
2458 color_view |= S_028C6C_MIP_LEVEL_GFX10(surf->base.u.tex.level);
2459
2460 surf->cb_color_attrib3 = S_028EE0_MIP0_DEPTH(mip0_depth) |
2461 S_028EE0_RESOURCE_TYPE(tex->surface.u.gfx9.resource_type) |
2462 S_028EE0_RESOURCE_LEVEL(1);
2463 } else if (sctx->chip_class == GFX9) {
2464 color_view |= S_028C6C_MIP_LEVEL_GFX9(surf->base.u.tex.level);
2465 color_attrib |= S_028C74_MIP0_DEPTH(mip0_depth) |
2466 S_028C74_RESOURCE_TYPE(tex->surface.u.gfx9.resource_type);
2467 }
2468
2469 if (sctx->chip_class >= GFX9) {
2470 surf->cb_color_attrib2 = S_028C68_MIP0_WIDTH(surf->width0 - 1) |
2471 S_028C68_MIP0_HEIGHT(surf->height0 - 1) |
2472 S_028C68_MAX_MIP(tex->buffer.b.b.last_level);
2473 }
2474
2475 surf->cb_color_view = color_view;
2476 surf->cb_color_info = color_info;
2477 surf->cb_color_attrib = color_attrib;
2478
2479 /* Determine pixel shader export format */
2480 si_choose_spi_color_formats(surf, format, swap, ntype, tex->is_depth);
2481
2482 surf->color_initialized = true;
2483 }
2484
2485 static void si_init_depth_surface(struct si_context *sctx, struct si_surface *surf)
2486 {
2487 struct si_texture *tex = (struct si_texture *)surf->base.texture;
2488 unsigned level = surf->base.u.tex.level;
2489 unsigned format, stencil_format;
2490 uint32_t z_info, s_info;
2491
2492 format = si_translate_dbformat(tex->db_render_format);
2493 stencil_format = tex->surface.has_stencil ? V_028044_STENCIL_8 : V_028044_STENCIL_INVALID;
2494
2495 assert(format != V_028040_Z_INVALID);
2496 if (format == V_028040_Z_INVALID)
2497 PRINT_ERR("Invalid DB format: %d, disabling DB.\n", tex->buffer.b.b.format);
2498
2499 surf->db_depth_view = S_028008_SLICE_START(surf->base.u.tex.first_layer) |
2500 S_028008_SLICE_MAX(surf->base.u.tex.last_layer);
2501 surf->db_htile_data_base = 0;
2502 surf->db_htile_surface = 0;
2503
2504 if (sctx->chip_class >= GFX10) {
2505 surf->db_depth_view |= S_028008_SLICE_START_HI(surf->base.u.tex.first_layer >> 11) |
2506 S_028008_SLICE_MAX_HI(surf->base.u.tex.last_layer >> 11);
2507 }
2508
2509 if (sctx->chip_class >= GFX9) {
2510 assert(tex->surface.u.gfx9.surf_offset == 0);
2511 surf->db_depth_base = tex->buffer.gpu_address >> 8;
2512 surf->db_stencil_base = (tex->buffer.gpu_address + tex->surface.u.gfx9.stencil_offset) >> 8;
2513 z_info = S_028038_FORMAT(format) |
2514 S_028038_NUM_SAMPLES(util_logbase2(tex->buffer.b.b.nr_samples)) |
2515 S_028038_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode) |
2516 S_028038_MAXMIP(tex->buffer.b.b.last_level);
2517 s_info = S_02803C_FORMAT(stencil_format) |
2518 S_02803C_SW_MODE(tex->surface.u.gfx9.stencil.swizzle_mode);
2519
2520 if (sctx->chip_class == GFX9) {
2521 surf->db_z_info2 = S_028068_EPITCH(tex->surface.u.gfx9.surf.epitch);
2522 surf->db_stencil_info2 = S_02806C_EPITCH(tex->surface.u.gfx9.stencil.epitch);
2523 }
2524 surf->db_depth_view |= S_028008_MIPID(level);
2525 surf->db_depth_size =
2526 S_02801C_X_MAX(tex->buffer.b.b.width0 - 1) | S_02801C_Y_MAX(tex->buffer.b.b.height0 - 1);
2527
2528 if (si_htile_enabled(tex, level, PIPE_MASK_ZS)) {
2529 z_info |= S_028038_TILE_SURFACE_ENABLE(1) | S_028038_ALLOW_EXPCLEAR(1);
2530
2531 if (tex->surface.has_stencil && !tex->htile_stencil_disabled) {
2532 /* Stencil buffer workaround ported from the GFX6-GFX8 code.
2533 * See that for explanation.
2534 */
2535 s_info |= S_02803C_ALLOW_EXPCLEAR(tex->buffer.b.b.nr_samples <= 1);
2536 } else {
2537 /* Use all HTILE for depth if there's no stencil. */
2538 s_info |= S_02803C_TILE_STENCIL_DISABLE(1);
2539 }
2540
2541 surf->db_htile_data_base = (tex->buffer.gpu_address + tex->surface.htile_offset) >> 8;
2542 surf->db_htile_surface =
2543 S_028ABC_FULL_CACHE(1) | S_028ABC_PIPE_ALIGNED(1);
2544 if (sctx->chip_class == GFX9) {
2545 surf->db_htile_surface |= S_028ABC_RB_ALIGNED(1);
2546 }
2547 }
2548 } else {
2549 /* GFX6-GFX8 */
2550 struct legacy_surf_level *levelinfo = &tex->surface.u.legacy.level[level];
2551
2552 assert(levelinfo->nblk_x % 8 == 0 && levelinfo->nblk_y % 8 == 0);
2553
2554 surf->db_depth_base =
2555 (tex->buffer.gpu_address + tex->surface.u.legacy.level[level].offset) >> 8;
2556 surf->db_stencil_base =
2557 (tex->buffer.gpu_address + tex->surface.u.legacy.stencil_level[level].offset) >> 8;
2558
2559 z_info =
2560 S_028040_FORMAT(format) | S_028040_NUM_SAMPLES(util_logbase2(tex->buffer.b.b.nr_samples));
2561 s_info = S_028044_FORMAT(stencil_format);
2562 surf->db_depth_info = 0;
2563
2564 if (sctx->chip_class >= GFX7) {
2565 struct radeon_info *info = &sctx->screen->info;
2566 unsigned index = tex->surface.u.legacy.tiling_index[level];
2567 unsigned stencil_index = tex->surface.u.legacy.stencil_tiling_index[level];
2568 unsigned macro_index = tex->surface.u.legacy.macro_tile_index;
2569 unsigned tile_mode = info->si_tile_mode_array[index];
2570 unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
2571 unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
2572
2573 surf->db_depth_info |= S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
2574 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
2575 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
2576 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
2577 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
2578 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
2579 z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
2580 s_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
2581 } else {
2582 unsigned tile_mode_index = si_tile_mode_index(tex, level, false);
2583 z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
2584 tile_mode_index = si_tile_mode_index(tex, level, true);
2585 s_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
2586 }
2587
2588 surf->db_depth_size = S_028058_PITCH_TILE_MAX((levelinfo->nblk_x / 8) - 1) |
2589 S_028058_HEIGHT_TILE_MAX((levelinfo->nblk_y / 8) - 1);
2590 surf->db_depth_slice =
2591 S_02805C_SLICE_TILE_MAX((levelinfo->nblk_x * levelinfo->nblk_y) / 64 - 1);
2592
2593 if (si_htile_enabled(tex, level, PIPE_MASK_ZS)) {
2594 z_info |= S_028040_TILE_SURFACE_ENABLE(1) | S_028040_ALLOW_EXPCLEAR(1);
2595
2596 if (tex->surface.has_stencil) {
2597 /* Workaround: For a not yet understood reason, the
2598 * combination of MSAA, fast stencil clear and stencil
2599 * decompress messes with subsequent stencil buffer
2600 * uses. Problem was reproduced on Verde, Bonaire,
2601 * Tonga, and Carrizo.
2602 *
2603 * Disabling EXPCLEAR works around the problem.
2604 *
2605 * Check piglit's arb_texture_multisample-stencil-clear
2606 * test if you want to try changing this.
2607 */
2608 if (tex->buffer.b.b.nr_samples <= 1)
2609 s_info |= S_028044_ALLOW_EXPCLEAR(1);
2610 }
2611
2612 surf->db_htile_data_base = (tex->buffer.gpu_address + tex->surface.htile_offset) >> 8;
2613 surf->db_htile_surface = S_028ABC_FULL_CACHE(1);
2614 }
2615 }
2616
2617 surf->db_z_info = z_info;
2618 surf->db_stencil_info = s_info;
2619
2620 surf->depth_initialized = true;
2621 }
2622
2623 void si_update_fb_dirtiness_after_rendering(struct si_context *sctx)
2624 {
2625 if (sctx->decompression_enabled)
2626 return;
2627
2628 if (sctx->framebuffer.state.zsbuf) {
2629 struct pipe_surface *surf = sctx->framebuffer.state.zsbuf;
2630 struct si_texture *tex = (struct si_texture *)surf->texture;
2631
2632 tex->dirty_level_mask |= 1 << surf->u.tex.level;
2633
2634 if (tex->surface.has_stencil)
2635 tex->stencil_dirty_level_mask |= 1 << surf->u.tex.level;
2636 }
2637
2638 unsigned compressed_cb_mask = sctx->framebuffer.compressed_cb_mask;
2639 while (compressed_cb_mask) {
2640 unsigned i = u_bit_scan(&compressed_cb_mask);
2641 struct pipe_surface *surf = sctx->framebuffer.state.cbufs[i];
2642 struct si_texture *tex = (struct si_texture *)surf->texture;
2643
2644 if (tex->surface.fmask_offset) {
2645 tex->dirty_level_mask |= 1 << surf->u.tex.level;
2646 tex->fmask_is_identity = false;
2647 }
2648 if (tex->dcc_gather_statistics)
2649 tex->separate_dcc_dirty = true;
2650 }
2651 }
2652
2653 static void si_dec_framebuffer_counters(const struct pipe_framebuffer_state *state)
2654 {
2655 for (int i = 0; i < state->nr_cbufs; ++i) {
2656 struct si_surface *surf = NULL;
2657 struct si_texture *tex;
2658
2659 if (!state->cbufs[i])
2660 continue;
2661 surf = (struct si_surface *)state->cbufs[i];
2662 tex = (struct si_texture *)surf->base.texture;
2663
2664 p_atomic_dec(&tex->framebuffers_bound);
2665 }
2666 }
2667
2668 static void si_set_framebuffer_state(struct pipe_context *ctx,
2669 const struct pipe_framebuffer_state *state)
2670 {
2671 struct si_context *sctx = (struct si_context *)ctx;
2672 struct si_surface *surf = NULL;
2673 struct si_texture *tex;
2674 bool old_any_dst_linear = sctx->framebuffer.any_dst_linear;
2675 unsigned old_nr_samples = sctx->framebuffer.nr_samples;
2676 unsigned old_colorbuf_enabled_4bit = sctx->framebuffer.colorbuf_enabled_4bit;
2677 bool old_has_zsbuf = !!sctx->framebuffer.state.zsbuf;
2678 bool old_has_stencil =
2679 old_has_zsbuf &&
2680 ((struct si_texture *)sctx->framebuffer.state.zsbuf->texture)->surface.has_stencil;
2681 bool unbound = false;
2682 int i;
2683
2684 /* Reject zero-sized framebuffers due to a hw bug on GFX6 that occurs
2685 * when PA_SU_HARDWARE_SCREEN_OFFSET != 0 and any_scissor.BR_X/Y <= 0.
2686 * We could implement the full workaround here, but it's a useless case.
2687 */
2688 if ((!state->width || !state->height) && (state->nr_cbufs || state->zsbuf)) {
2689 unreachable("the framebuffer shouldn't have zero area");
2690 return;
2691 }
2692
2693 si_update_fb_dirtiness_after_rendering(sctx);
2694
2695 for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
2696 if (!sctx->framebuffer.state.cbufs[i])
2697 continue;
2698
2699 tex = (struct si_texture *)sctx->framebuffer.state.cbufs[i]->texture;
2700 if (tex->dcc_gather_statistics)
2701 vi_separate_dcc_stop_query(sctx, tex);
2702 }
2703
2704 /* Disable DCC if the formats are incompatible. */
2705 for (i = 0; i < state->nr_cbufs; i++) {
2706 if (!state->cbufs[i])
2707 continue;
2708
2709 surf = (struct si_surface *)state->cbufs[i];
2710 tex = (struct si_texture *)surf->base.texture;
2711
2712 if (!surf->dcc_incompatible)
2713 continue;
2714
2715 /* Since the DCC decompression calls back into set_framebuffer-
2716 * _state, we need to unbind the framebuffer, so that
2717 * vi_separate_dcc_stop_query isn't called twice with the same
2718 * color buffer.
2719 */
2720 if (!unbound) {
2721 util_copy_framebuffer_state(&sctx->framebuffer.state, NULL);
2722 unbound = true;
2723 }
2724
2725 if (vi_dcc_enabled(tex, surf->base.u.tex.level))
2726 if (!si_texture_disable_dcc(sctx, tex))
2727 si_decompress_dcc(sctx, tex);
2728
2729 surf->dcc_incompatible = false;
2730 }
2731
2732 /* Only flush TC when changing the framebuffer state, because
2733 * the only client not using TC that can change textures is
2734 * the framebuffer.
2735 *
2736 * Wait for compute shaders because of possible transitions:
2737 * - FB write -> shader read
2738 * - shader write -> FB read
2739 *
2740 * DB caches are flushed on demand (using si_decompress_textures).
2741 *
2742 * When MSAA is enabled, CB and TC caches are flushed on demand
2743 * (after FMASK decompression). Shader write -> FB read transitions
2744 * cannot happen for MSAA textures, because MSAA shader images are
2745 * not supported.
2746 *
2747 * Only flush and wait for CB if there is actually a bound color buffer.
2748 */
2749 if (sctx->framebuffer.uncompressed_cb_mask) {
2750 si_make_CB_shader_coherent(sctx, sctx->framebuffer.nr_samples,
2751 sctx->framebuffer.CB_has_shader_readable_metadata,
2752 sctx->framebuffer.all_DCC_pipe_aligned);
2753 }
2754
2755 sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
2756
2757 /* u_blitter doesn't invoke depth decompression when it does multiple
2758 * blits in a row, but the only case when it matters for DB is when
2759 * doing generate_mipmap. So here we flush DB manually between
2760 * individual generate_mipmap blits.
2761 * Note that lower mipmap levels aren't compressed.
2762 */
2763 if (sctx->generate_mipmap_for_depth) {
2764 si_make_DB_shader_coherent(sctx, 1, false, sctx->framebuffer.DB_has_shader_readable_metadata);
2765 } else if (sctx->chip_class == GFX9) {
2766 /* It appears that DB metadata "leaks" in a sequence of:
2767 * - depth clear
2768 * - DCC decompress for shader image writes (with DB disabled)
2769 * - render with DEPTH_BEFORE_SHADER=1
2770 * Flushing DB metadata works around the problem.
2771 */
2772 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_DB_META;
2773 }
2774
2775 /* Take the maximum of the old and new count. If the new count is lower,
2776 * dirtying is needed to disable the unbound colorbuffers.
2777 */
2778 sctx->framebuffer.dirty_cbufs |=
2779 (1 << MAX2(sctx->framebuffer.state.nr_cbufs, state->nr_cbufs)) - 1;
2780 sctx->framebuffer.dirty_zsbuf |= sctx->framebuffer.state.zsbuf != state->zsbuf;
2781
2782 si_dec_framebuffer_counters(&sctx->framebuffer.state);
2783 util_copy_framebuffer_state(&sctx->framebuffer.state, state);
2784
2785 sctx->framebuffer.colorbuf_enabled_4bit = 0;
2786 sctx->framebuffer.spi_shader_col_format = 0;
2787 sctx->framebuffer.spi_shader_col_format_alpha = 0;
2788 sctx->framebuffer.spi_shader_col_format_blend = 0;
2789 sctx->framebuffer.spi_shader_col_format_blend_alpha = 0;
2790 sctx->framebuffer.color_is_int8 = 0;
2791 sctx->framebuffer.color_is_int10 = 0;
2792
2793 sctx->framebuffer.compressed_cb_mask = 0;
2794 sctx->framebuffer.uncompressed_cb_mask = 0;
2795 sctx->framebuffer.displayable_dcc_cb_mask = 0;
2796 sctx->framebuffer.nr_samples = util_framebuffer_get_num_samples(state);
2797 sctx->framebuffer.nr_color_samples = sctx->framebuffer.nr_samples;
2798 sctx->framebuffer.log_samples = util_logbase2(sctx->framebuffer.nr_samples);
2799 sctx->framebuffer.any_dst_linear = false;
2800 sctx->framebuffer.CB_has_shader_readable_metadata = false;
2801 sctx->framebuffer.DB_has_shader_readable_metadata = false;
2802 sctx->framebuffer.all_DCC_pipe_aligned = true;
2803 sctx->framebuffer.min_bytes_per_pixel = 0;
2804
2805 for (i = 0; i < state->nr_cbufs; i++) {
2806 if (!state->cbufs[i])
2807 continue;
2808
2809 surf = (struct si_surface *)state->cbufs[i];
2810 tex = (struct si_texture *)surf->base.texture;
2811
2812 if (!surf->color_initialized) {
2813 si_initialize_color_surface(sctx, surf);
2814 }
2815
2816 sctx->framebuffer.colorbuf_enabled_4bit |= 0xf << (i * 4);
2817 sctx->framebuffer.spi_shader_col_format |= surf->spi_shader_col_format << (i * 4);
2818 sctx->framebuffer.spi_shader_col_format_alpha |= surf->spi_shader_col_format_alpha << (i * 4);
2819 sctx->framebuffer.spi_shader_col_format_blend |= surf->spi_shader_col_format_blend << (i * 4);
2820 sctx->framebuffer.spi_shader_col_format_blend_alpha |= surf->spi_shader_col_format_blend_alpha
2821 << (i * 4);
2822
2823 if (surf->color_is_int8)
2824 sctx->framebuffer.color_is_int8 |= 1 << i;
2825 if (surf->color_is_int10)
2826 sctx->framebuffer.color_is_int10 |= 1 << i;
2827
2828 if (tex->surface.fmask_offset)
2829 sctx->framebuffer.compressed_cb_mask |= 1 << i;
2830 else
2831 sctx->framebuffer.uncompressed_cb_mask |= 1 << i;
2832
2833 if (tex->surface.display_dcc_offset)
2834 sctx->framebuffer.displayable_dcc_cb_mask |= 1 << i;
2835
2836 /* Don't update nr_color_samples for non-AA buffers.
2837 * (e.g. destination of MSAA resolve)
2838 */
2839 if (tex->buffer.b.b.nr_samples >= 2 &&
2840 tex->buffer.b.b.nr_storage_samples < tex->buffer.b.b.nr_samples) {
2841 sctx->framebuffer.nr_color_samples =
2842 MIN2(sctx->framebuffer.nr_color_samples, tex->buffer.b.b.nr_storage_samples);
2843 sctx->framebuffer.nr_color_samples = MAX2(1, sctx->framebuffer.nr_color_samples);
2844 }
2845
2846 if (tex->surface.is_linear)
2847 sctx->framebuffer.any_dst_linear = true;
2848
2849 if (vi_dcc_enabled(tex, surf->base.u.tex.level)) {
2850 sctx->framebuffer.CB_has_shader_readable_metadata = true;
2851
2852 if (sctx->chip_class >= GFX9 && !tex->surface.u.gfx9.dcc.pipe_aligned)
2853 sctx->framebuffer.all_DCC_pipe_aligned = false;
2854 }
2855
2856 si_context_add_resource_size(sctx, surf->base.texture);
2857
2858 p_atomic_inc(&tex->framebuffers_bound);
2859
2860 if (tex->dcc_gather_statistics) {
2861 /* Dirty tracking must be enabled for DCC usage analysis. */
2862 sctx->framebuffer.compressed_cb_mask |= 1 << i;
2863 vi_separate_dcc_start_query(sctx, tex);
2864 }
2865
2866 /* Update the minimum but don't keep 0. */
2867 if (!sctx->framebuffer.min_bytes_per_pixel ||
2868 tex->surface.bpe < sctx->framebuffer.min_bytes_per_pixel)
2869 sctx->framebuffer.min_bytes_per_pixel = tex->surface.bpe;
2870 }
2871
2872 /* For optimal DCC performance. */
2873 if (sctx->chip_class >= GFX10)
2874 sctx->framebuffer.dcc_overwrite_combiner_watermark = 6;
2875 else
2876 sctx->framebuffer.dcc_overwrite_combiner_watermark = 4;
2877
2878 struct si_texture *zstex = NULL;
2879
2880 if (state->zsbuf) {
2881 surf = (struct si_surface *)state->zsbuf;
2882 zstex = (struct si_texture *)surf->base.texture;
2883
2884 if (!surf->depth_initialized) {
2885 si_init_depth_surface(sctx, surf);
2886 }
2887
2888 if (vi_tc_compat_htile_enabled(zstex, surf->base.u.tex.level, PIPE_MASK_ZS))
2889 sctx->framebuffer.DB_has_shader_readable_metadata = true;
2890
2891 si_context_add_resource_size(sctx, surf->base.texture);
2892
2893 /* Update the minimum but don't keep 0. */
2894 if (!sctx->framebuffer.min_bytes_per_pixel ||
2895 zstex->surface.bpe < sctx->framebuffer.min_bytes_per_pixel)
2896 sctx->framebuffer.min_bytes_per_pixel = zstex->surface.bpe;
2897 }
2898
2899 si_update_ps_colorbuf0_slot(sctx);
2900 si_update_poly_offset_state(sctx);
2901 si_update_ngg_small_prim_precision(sctx);
2902 si_mark_atom_dirty(sctx, &sctx->atoms.s.cb_render_state);
2903 si_mark_atom_dirty(sctx, &sctx->atoms.s.framebuffer);
2904
2905 if (sctx->screen->dpbb_allowed)
2906 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
2907
2908 if (sctx->framebuffer.any_dst_linear != old_any_dst_linear)
2909 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
2910
2911 if (sctx->screen->has_out_of_order_rast &&
2912 (sctx->framebuffer.colorbuf_enabled_4bit != old_colorbuf_enabled_4bit ||
2913 !!sctx->framebuffer.state.zsbuf != old_has_zsbuf ||
2914 (zstex && zstex->surface.has_stencil != old_has_stencil)))
2915 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
2916
2917 if (sctx->framebuffer.nr_samples != old_nr_samples) {
2918 struct pipe_constant_buffer constbuf = {0};
2919
2920 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
2921 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
2922
2923 constbuf.buffer = sctx->sample_pos_buffer;
2924
2925 /* Set sample locations as fragment shader constants. */
2926 switch (sctx->framebuffer.nr_samples) {
2927 case 1:
2928 constbuf.buffer_offset = 0;
2929 break;
2930 case 2:
2931 constbuf.buffer_offset =
2932 (ubyte *)sctx->sample_positions.x2 - (ubyte *)sctx->sample_positions.x1;
2933 break;
2934 case 4:
2935 constbuf.buffer_offset =
2936 (ubyte *)sctx->sample_positions.x4 - (ubyte *)sctx->sample_positions.x1;
2937 break;
2938 case 8:
2939 constbuf.buffer_offset =
2940 (ubyte *)sctx->sample_positions.x8 - (ubyte *)sctx->sample_positions.x1;
2941 break;
2942 case 16:
2943 constbuf.buffer_offset =
2944 (ubyte *)sctx->sample_positions.x16 - (ubyte *)sctx->sample_positions.x1;
2945 break;
2946 default:
2947 PRINT_ERR("Requested an invalid number of samples %i.\n", sctx->framebuffer.nr_samples);
2948 assert(0);
2949 }
2950 constbuf.buffer_size = sctx->framebuffer.nr_samples * 2 * 4;
2951 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS, &constbuf);
2952
2953 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_sample_locs);
2954 }
2955
2956 sctx->do_update_shaders = true;
2957
2958 if (!sctx->decompression_enabled) {
2959 /* Prevent textures decompression when the framebuffer state
2960 * changes come from the decompression passes themselves.
2961 */
2962 sctx->need_check_render_feedback = true;
2963 }
2964 }
2965
2966 static void si_emit_framebuffer_state(struct si_context *sctx)
2967 {
2968 struct radeon_cmdbuf *cs = sctx->gfx_cs;
2969 struct pipe_framebuffer_state *state = &sctx->framebuffer.state;
2970 unsigned i, nr_cbufs = state->nr_cbufs;
2971 struct si_texture *tex = NULL;
2972 struct si_surface *cb = NULL;
2973 unsigned cb_color_info = 0;
2974
2975 /* Colorbuffers. */
2976 for (i = 0; i < nr_cbufs; i++) {
2977 uint64_t cb_color_base, cb_color_fmask, cb_color_cmask, cb_dcc_base;
2978 unsigned cb_color_attrib;
2979
2980 if (!(sctx->framebuffer.dirty_cbufs & (1 << i)))
2981 continue;
2982
2983 cb = (struct si_surface *)state->cbufs[i];
2984 if (!cb) {
2985 radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + i * 0x3C,
2986 S_028C70_FORMAT(V_028C70_COLOR_INVALID));
2987 continue;
2988 }
2989
2990 tex = (struct si_texture *)cb->base.texture;
2991 radeon_add_to_buffer_list(
2992 sctx, sctx->gfx_cs, &tex->buffer, RADEON_USAGE_READWRITE,
2993 tex->buffer.b.b.nr_samples > 1 ? RADEON_PRIO_COLOR_BUFFER_MSAA : RADEON_PRIO_COLOR_BUFFER);
2994
2995 if (tex->cmask_buffer && tex->cmask_buffer != &tex->buffer) {
2996 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, tex->cmask_buffer, RADEON_USAGE_READWRITE,
2997 RADEON_PRIO_SEPARATE_META);
2998 }
2999
3000 if (tex->dcc_separate_buffer)
3001 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, tex->dcc_separate_buffer,
3002 RADEON_USAGE_READWRITE, RADEON_PRIO_SEPARATE_META);
3003
3004 /* Compute mutable surface parameters. */
3005 cb_color_base = tex->buffer.gpu_address >> 8;
3006 cb_color_fmask = 0;
3007 cb_color_cmask = tex->cmask_base_address_reg;
3008 cb_dcc_base = 0;
3009 cb_color_info = cb->cb_color_info | tex->cb_color_info;
3010 cb_color_attrib = cb->cb_color_attrib;
3011
3012 if (cb->base.u.tex.level > 0)
3013 cb_color_info &= C_028C70_FAST_CLEAR;
3014
3015 if (tex->surface.fmask_offset) {
3016 cb_color_fmask = (tex->buffer.gpu_address + tex->surface.fmask_offset) >> 8;
3017 cb_color_fmask |= tex->surface.fmask_tile_swizzle;
3018 }
3019
3020 /* Set up DCC. */
3021 if (vi_dcc_enabled(tex, cb->base.u.tex.level)) {
3022 bool is_msaa_resolve_dst = state->cbufs[0] && state->cbufs[0]->texture->nr_samples > 1 &&
3023 state->cbufs[1] == &cb->base &&
3024 state->cbufs[1]->texture->nr_samples <= 1;
3025
3026 if (!is_msaa_resolve_dst)
3027 cb_color_info |= S_028C70_DCC_ENABLE(1);
3028
3029 cb_dcc_base =
3030 ((!tex->dcc_separate_buffer ? tex->buffer.gpu_address : 0) + tex->surface.dcc_offset) >>
3031 8;
3032
3033 unsigned dcc_tile_swizzle = tex->surface.tile_swizzle;
3034 dcc_tile_swizzle &= (tex->surface.dcc_alignment - 1) >> 8;
3035 cb_dcc_base |= dcc_tile_swizzle;
3036 }
3037
3038 if (sctx->chip_class >= GFX10) {
3039 unsigned cb_color_attrib3;
3040
3041 /* Set mutable surface parameters. */
3042 cb_color_base += tex->surface.u.gfx9.surf_offset >> 8;
3043 cb_color_base |= tex->surface.tile_swizzle;
3044 if (!tex->surface.fmask_offset)
3045 cb_color_fmask = cb_color_base;
3046 if (cb->base.u.tex.level > 0)
3047 cb_color_cmask = cb_color_base;
3048
3049 cb_color_attrib3 = cb->cb_color_attrib3 |
3050 S_028EE0_COLOR_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode) |
3051 S_028EE0_FMASK_SW_MODE(tex->surface.u.gfx9.fmask.swizzle_mode) |
3052 S_028EE0_CMASK_PIPE_ALIGNED(1) |
3053 S_028EE0_DCC_PIPE_ALIGNED(tex->surface.u.gfx9.dcc.pipe_aligned);
3054
3055 radeon_set_context_reg_seq(cs, R_028C60_CB_COLOR0_BASE + i * 0x3C, 14);
3056 radeon_emit(cs, cb_color_base); /* CB_COLOR0_BASE */
3057 radeon_emit(cs, 0); /* hole */
3058 radeon_emit(cs, 0); /* hole */
3059 radeon_emit(cs, cb->cb_color_view); /* CB_COLOR0_VIEW */
3060 radeon_emit(cs, cb_color_info); /* CB_COLOR0_INFO */
3061 radeon_emit(cs, cb_color_attrib); /* CB_COLOR0_ATTRIB */
3062 radeon_emit(cs, cb->cb_dcc_control); /* CB_COLOR0_DCC_CONTROL */
3063 radeon_emit(cs, cb_color_cmask); /* CB_COLOR0_CMASK */
3064 radeon_emit(cs, 0); /* hole */
3065 radeon_emit(cs, cb_color_fmask); /* CB_COLOR0_FMASK */
3066 radeon_emit(cs, 0); /* hole */
3067 radeon_emit(cs, tex->color_clear_value[0]); /* CB_COLOR0_CLEAR_WORD0 */
3068 radeon_emit(cs, tex->color_clear_value[1]); /* CB_COLOR0_CLEAR_WORD1 */
3069 radeon_emit(cs, cb_dcc_base); /* CB_COLOR0_DCC_BASE */
3070
3071 radeon_set_context_reg(cs, R_028E40_CB_COLOR0_BASE_EXT + i * 4, cb_color_base >> 32);
3072 radeon_set_context_reg(cs, R_028E60_CB_COLOR0_CMASK_BASE_EXT + i * 4,
3073 cb_color_cmask >> 32);
3074 radeon_set_context_reg(cs, R_028E80_CB_COLOR0_FMASK_BASE_EXT + i * 4,
3075 cb_color_fmask >> 32);
3076 radeon_set_context_reg(cs, R_028EA0_CB_COLOR0_DCC_BASE_EXT + i * 4, cb_dcc_base >> 32);
3077 radeon_set_context_reg(cs, R_028EC0_CB_COLOR0_ATTRIB2 + i * 4, cb->cb_color_attrib2);
3078 radeon_set_context_reg(cs, R_028EE0_CB_COLOR0_ATTRIB3 + i * 4, cb_color_attrib3);
3079 } else if (sctx->chip_class == GFX9) {
3080 struct gfx9_surf_meta_flags meta = {
3081 .rb_aligned = 1,
3082 .pipe_aligned = 1,
3083 };
3084
3085 if (tex->surface.dcc_offset)
3086 meta = tex->surface.u.gfx9.dcc;
3087
3088 /* Set mutable surface parameters. */
3089 cb_color_base += tex->surface.u.gfx9.surf_offset >> 8;
3090 cb_color_base |= tex->surface.tile_swizzle;
3091 if (!tex->surface.fmask_offset)
3092 cb_color_fmask = cb_color_base;
3093 if (cb->base.u.tex.level > 0)
3094 cb_color_cmask = cb_color_base;
3095 cb_color_attrib |= S_028C74_COLOR_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode) |
3096 S_028C74_FMASK_SW_MODE(tex->surface.u.gfx9.fmask.swizzle_mode) |
3097 S_028C74_RB_ALIGNED(meta.rb_aligned) |
3098 S_028C74_PIPE_ALIGNED(meta.pipe_aligned);
3099
3100 radeon_set_context_reg_seq(cs, R_028C60_CB_COLOR0_BASE + i * 0x3C, 15);
3101 radeon_emit(cs, cb_color_base); /* CB_COLOR0_BASE */
3102 radeon_emit(cs, S_028C64_BASE_256B(cb_color_base >> 32)); /* CB_COLOR0_BASE_EXT */
3103 radeon_emit(cs, cb->cb_color_attrib2); /* CB_COLOR0_ATTRIB2 */
3104 radeon_emit(cs, cb->cb_color_view); /* CB_COLOR0_VIEW */
3105 radeon_emit(cs, cb_color_info); /* CB_COLOR0_INFO */
3106 radeon_emit(cs, cb_color_attrib); /* CB_COLOR0_ATTRIB */
3107 radeon_emit(cs, cb->cb_dcc_control); /* CB_COLOR0_DCC_CONTROL */
3108 radeon_emit(cs, cb_color_cmask); /* CB_COLOR0_CMASK */
3109 radeon_emit(cs, S_028C80_BASE_256B(cb_color_cmask >> 32)); /* CB_COLOR0_CMASK_BASE_EXT */
3110 radeon_emit(cs, cb_color_fmask); /* CB_COLOR0_FMASK */
3111 radeon_emit(cs, S_028C88_BASE_256B(cb_color_fmask >> 32)); /* CB_COLOR0_FMASK_BASE_EXT */
3112 radeon_emit(cs, tex->color_clear_value[0]); /* CB_COLOR0_CLEAR_WORD0 */
3113 radeon_emit(cs, tex->color_clear_value[1]); /* CB_COLOR0_CLEAR_WORD1 */
3114 radeon_emit(cs, cb_dcc_base); /* CB_COLOR0_DCC_BASE */
3115 radeon_emit(cs, S_028C98_BASE_256B(cb_dcc_base >> 32)); /* CB_COLOR0_DCC_BASE_EXT */
3116
3117 radeon_set_context_reg(cs, R_0287A0_CB_MRT0_EPITCH + i * 4,
3118 S_0287A0_EPITCH(tex->surface.u.gfx9.surf.epitch));
3119 } else {
3120 /* Compute mutable surface parameters (GFX6-GFX8). */
3121 const struct legacy_surf_level *level_info =
3122 &tex->surface.u.legacy.level[cb->base.u.tex.level];
3123 unsigned pitch_tile_max, slice_tile_max, tile_mode_index;
3124 unsigned cb_color_pitch, cb_color_slice, cb_color_fmask_slice;
3125
3126 cb_color_base += level_info->offset >> 8;
3127 /* Only macrotiled modes can set tile swizzle. */
3128 if (level_info->mode == RADEON_SURF_MODE_2D)
3129 cb_color_base |= tex->surface.tile_swizzle;
3130
3131 if (!tex->surface.fmask_offset)
3132 cb_color_fmask = cb_color_base;
3133 if (cb->base.u.tex.level > 0)
3134 cb_color_cmask = cb_color_base;
3135 if (cb_dcc_base)
3136 cb_dcc_base += level_info->dcc_offset >> 8;
3137
3138 pitch_tile_max = level_info->nblk_x / 8 - 1;
3139 slice_tile_max = level_info->nblk_x * level_info->nblk_y / 64 - 1;
3140 tile_mode_index = si_tile_mode_index(tex, cb->base.u.tex.level, false);
3141
3142 cb_color_attrib |= S_028C74_TILE_MODE_INDEX(tile_mode_index);
3143 cb_color_pitch = S_028C64_TILE_MAX(pitch_tile_max);
3144 cb_color_slice = S_028C68_TILE_MAX(slice_tile_max);
3145
3146 if (tex->surface.fmask_offset) {
3147 if (sctx->chip_class >= GFX7)
3148 cb_color_pitch |=
3149 S_028C64_FMASK_TILE_MAX(tex->surface.u.legacy.fmask.pitch_in_pixels / 8 - 1);
3150 cb_color_attrib |=
3151 S_028C74_FMASK_TILE_MODE_INDEX(tex->surface.u.legacy.fmask.tiling_index);
3152 cb_color_fmask_slice = S_028C88_TILE_MAX(tex->surface.u.legacy.fmask.slice_tile_max);
3153 } else {
3154 /* This must be set for fast clear to work without FMASK. */
3155 if (sctx->chip_class >= GFX7)
3156 cb_color_pitch |= S_028C64_FMASK_TILE_MAX(pitch_tile_max);
3157 cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index);
3158 cb_color_fmask_slice = S_028C88_TILE_MAX(slice_tile_max);
3159 }
3160
3161 radeon_set_context_reg_seq(cs, R_028C60_CB_COLOR0_BASE + i * 0x3C,
3162 sctx->chip_class >= GFX8 ? 14 : 13);
3163 radeon_emit(cs, cb_color_base); /* CB_COLOR0_BASE */
3164 radeon_emit(cs, cb_color_pitch); /* CB_COLOR0_PITCH */
3165 radeon_emit(cs, cb_color_slice); /* CB_COLOR0_SLICE */
3166 radeon_emit(cs, cb->cb_color_view); /* CB_COLOR0_VIEW */
3167 radeon_emit(cs, cb_color_info); /* CB_COLOR0_INFO */
3168 radeon_emit(cs, cb_color_attrib); /* CB_COLOR0_ATTRIB */
3169 radeon_emit(cs, cb->cb_dcc_control); /* CB_COLOR0_DCC_CONTROL */
3170 radeon_emit(cs, cb_color_cmask); /* CB_COLOR0_CMASK */
3171 radeon_emit(cs, tex->surface.u.legacy.cmask_slice_tile_max); /* CB_COLOR0_CMASK_SLICE */
3172 radeon_emit(cs, cb_color_fmask); /* CB_COLOR0_FMASK */
3173 radeon_emit(cs, cb_color_fmask_slice); /* CB_COLOR0_FMASK_SLICE */
3174 radeon_emit(cs, tex->color_clear_value[0]); /* CB_COLOR0_CLEAR_WORD0 */
3175 radeon_emit(cs, tex->color_clear_value[1]); /* CB_COLOR0_CLEAR_WORD1 */
3176
3177 if (sctx->chip_class >= GFX8) /* R_028C94_CB_COLOR0_DCC_BASE */
3178 radeon_emit(cs, cb_dcc_base);
3179 }
3180 }
3181 for (; i < 8; i++)
3182 if (sctx->framebuffer.dirty_cbufs & (1 << i))
3183 radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + i * 0x3C, 0);
3184
3185 /* ZS buffer. */
3186 if (state->zsbuf && sctx->framebuffer.dirty_zsbuf) {
3187 struct si_surface *zb = (struct si_surface *)state->zsbuf;
3188 struct si_texture *tex = (struct si_texture *)zb->base.texture;
3189 unsigned db_z_info = zb->db_z_info;
3190 unsigned db_stencil_info = zb->db_stencil_info;
3191 unsigned db_htile_surface = zb->db_htile_surface;
3192
3193 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, &tex->buffer, RADEON_USAGE_READWRITE,
3194 zb->base.texture->nr_samples > 1 ? RADEON_PRIO_DEPTH_BUFFER_MSAA
3195 : RADEON_PRIO_DEPTH_BUFFER);
3196
3197 /* Set fields dependent on tc_compatile_htile. */
3198 if (sctx->chip_class >= GFX9 &&
3199 vi_tc_compat_htile_enabled(tex, zb->base.u.tex.level, PIPE_MASK_ZS)) {
3200 unsigned max_zplanes = 4;
3201
3202 if (tex->db_render_format == PIPE_FORMAT_Z16_UNORM && tex->buffer.b.b.nr_samples > 1)
3203 max_zplanes = 2;
3204
3205 db_z_info |= S_028038_DECOMPRESS_ON_N_ZPLANES(max_zplanes + 1);
3206
3207 if (sctx->chip_class >= GFX10) {
3208 db_z_info |= S_028040_ITERATE_FLUSH(1);
3209 db_stencil_info |= S_028044_ITERATE_FLUSH(!tex->htile_stencil_disabled);
3210 } else {
3211 db_z_info |= S_028038_ITERATE_FLUSH(1);
3212 db_stencil_info |= S_02803C_ITERATE_FLUSH(1);
3213 }
3214 }
3215
3216 if (sctx->chip_class >= GFX10) {
3217 radeon_set_context_reg(cs, R_028014_DB_HTILE_DATA_BASE, zb->db_htile_data_base);
3218 radeon_set_context_reg(cs, R_02801C_DB_DEPTH_SIZE_XY, zb->db_depth_size);
3219
3220 radeon_set_context_reg_seq(cs, R_02803C_DB_DEPTH_INFO, 7);
3221 radeon_emit(cs, S_02803C_RESOURCE_LEVEL(1)); /* DB_DEPTH_INFO */
3222 radeon_emit(cs, db_z_info | /* DB_Z_INFO */
3223 S_028038_ZRANGE_PRECISION(tex->depth_clear_value != 0));
3224 radeon_emit(cs, db_stencil_info); /* DB_STENCIL_INFO */
3225 radeon_emit(cs, zb->db_depth_base); /* DB_Z_READ_BASE */
3226 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_READ_BASE */
3227 radeon_emit(cs, zb->db_depth_base); /* DB_Z_WRITE_BASE */
3228 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_WRITE_BASE */
3229
3230 radeon_set_context_reg_seq(cs, R_028068_DB_Z_READ_BASE_HI, 5);
3231 radeon_emit(cs, zb->db_depth_base >> 32); /* DB_Z_READ_BASE_HI */
3232 radeon_emit(cs, zb->db_stencil_base >> 32); /* DB_STENCIL_READ_BASE_HI */
3233 radeon_emit(cs, zb->db_depth_base >> 32); /* DB_Z_WRITE_BASE_HI */
3234 radeon_emit(cs, zb->db_stencil_base >> 32); /* DB_STENCIL_WRITE_BASE_HI */
3235 radeon_emit(cs, zb->db_htile_data_base >> 32); /* DB_HTILE_DATA_BASE_HI */
3236 } else if (sctx->chip_class == GFX9) {
3237 radeon_set_context_reg_seq(cs, R_028014_DB_HTILE_DATA_BASE, 3);
3238 radeon_emit(cs, zb->db_htile_data_base); /* DB_HTILE_DATA_BASE */
3239 radeon_emit(cs,
3240 S_028018_BASE_HI(zb->db_htile_data_base >> 32)); /* DB_HTILE_DATA_BASE_HI */
3241 radeon_emit(cs, zb->db_depth_size); /* DB_DEPTH_SIZE */
3242
3243 radeon_set_context_reg_seq(cs, R_028038_DB_Z_INFO, 10);
3244 radeon_emit(cs, db_z_info | /* DB_Z_INFO */
3245 S_028038_ZRANGE_PRECISION(tex->depth_clear_value != 0));
3246 radeon_emit(cs, db_stencil_info); /* DB_STENCIL_INFO */
3247 radeon_emit(cs, zb->db_depth_base); /* DB_Z_READ_BASE */
3248 radeon_emit(cs, S_028044_BASE_HI(zb->db_depth_base >> 32)); /* DB_Z_READ_BASE_HI */
3249 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_READ_BASE */
3250 radeon_emit(cs, S_02804C_BASE_HI(zb->db_stencil_base >> 32)); /* DB_STENCIL_READ_BASE_HI */
3251 radeon_emit(cs, zb->db_depth_base); /* DB_Z_WRITE_BASE */
3252 radeon_emit(cs, S_028054_BASE_HI(zb->db_depth_base >> 32)); /* DB_Z_WRITE_BASE_HI */
3253 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_WRITE_BASE */
3254 radeon_emit(cs,
3255 S_02805C_BASE_HI(zb->db_stencil_base >> 32)); /* DB_STENCIL_WRITE_BASE_HI */
3256
3257 radeon_set_context_reg_seq(cs, R_028068_DB_Z_INFO2, 2);
3258 radeon_emit(cs, zb->db_z_info2); /* DB_Z_INFO2 */
3259 radeon_emit(cs, zb->db_stencil_info2); /* DB_STENCIL_INFO2 */
3260 } else {
3261 /* GFX6-GFX8 */
3262 /* Set fields dependent on tc_compatile_htile. */
3263 if (si_htile_enabled(tex, zb->base.u.tex.level, PIPE_MASK_ZS)) {
3264 if (!tex->surface.has_stencil && !tex->tc_compatible_htile) {
3265 /* Use all of the htile_buffer for depth if there's no stencil.
3266 * This must not be set when TC-compatible HTILE is enabled
3267 * due to a hw bug.
3268 */
3269 db_stencil_info |= S_028044_TILE_STENCIL_DISABLE(1);
3270 }
3271
3272 if (tex->tc_compatible_htile) {
3273 db_htile_surface |= S_028ABC_TC_COMPATIBLE(1);
3274
3275 /* 0 = full compression. N = only compress up to N-1 Z planes. */
3276 if (tex->buffer.b.b.nr_samples <= 1)
3277 db_z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(5);
3278 else if (tex->buffer.b.b.nr_samples <= 4)
3279 db_z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(3);
3280 else
3281 db_z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(2);
3282 }
3283 }
3284
3285 radeon_set_context_reg(cs, R_028014_DB_HTILE_DATA_BASE, zb->db_htile_data_base);
3286
3287 radeon_set_context_reg_seq(cs, R_02803C_DB_DEPTH_INFO, 9);
3288 radeon_emit(cs, zb->db_depth_info | /* DB_DEPTH_INFO */
3289 S_02803C_ADDR5_SWIZZLE_MASK(!tex->tc_compatible_htile));
3290 radeon_emit(cs, db_z_info | /* DB_Z_INFO */
3291 S_028040_ZRANGE_PRECISION(tex->depth_clear_value != 0));
3292 radeon_emit(cs, db_stencil_info); /* DB_STENCIL_INFO */
3293 radeon_emit(cs, zb->db_depth_base); /* DB_Z_READ_BASE */
3294 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_READ_BASE */
3295 radeon_emit(cs, zb->db_depth_base); /* DB_Z_WRITE_BASE */
3296 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_WRITE_BASE */
3297 radeon_emit(cs, zb->db_depth_size); /* DB_DEPTH_SIZE */
3298 radeon_emit(cs, zb->db_depth_slice); /* DB_DEPTH_SLICE */
3299 }
3300
3301 radeon_set_context_reg_seq(cs, R_028028_DB_STENCIL_CLEAR, 2);
3302 radeon_emit(cs, tex->stencil_clear_value); /* R_028028_DB_STENCIL_CLEAR */
3303 radeon_emit(cs, fui(tex->depth_clear_value)); /* R_02802C_DB_DEPTH_CLEAR */
3304
3305 radeon_set_context_reg(cs, R_028008_DB_DEPTH_VIEW, zb->db_depth_view);
3306 radeon_set_context_reg(cs, R_028ABC_DB_HTILE_SURFACE, db_htile_surface);
3307 } else if (sctx->framebuffer.dirty_zsbuf) {
3308 if (sctx->chip_class == GFX9)
3309 radeon_set_context_reg_seq(cs, R_028038_DB_Z_INFO, 2);
3310 else
3311 radeon_set_context_reg_seq(cs, R_028040_DB_Z_INFO, 2);
3312
3313 radeon_emit(cs, S_028040_FORMAT(V_028040_Z_INVALID)); /* DB_Z_INFO */
3314 radeon_emit(cs, S_028044_FORMAT(V_028044_STENCIL_INVALID)); /* DB_STENCIL_INFO */
3315 }
3316
3317 /* Framebuffer dimensions. */
3318 /* PA_SC_WINDOW_SCISSOR_TL is set in si_init_config() */
3319 radeon_set_context_reg(cs, R_028208_PA_SC_WINDOW_SCISSOR_BR,
3320 S_028208_BR_X(state->width) | S_028208_BR_Y(state->height));
3321
3322 if (sctx->screen->dfsm_allowed) {
3323 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
3324 radeon_emit(cs, EVENT_TYPE(V_028A90_BREAK_BATCH) | EVENT_INDEX(0));
3325 }
3326
3327 sctx->framebuffer.dirty_cbufs = 0;
3328 sctx->framebuffer.dirty_zsbuf = false;
3329 }
3330
3331 static void si_emit_msaa_sample_locs(struct si_context *sctx)
3332 {
3333 struct radeon_cmdbuf *cs = sctx->gfx_cs;
3334 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
3335 unsigned nr_samples = sctx->framebuffer.nr_samples;
3336 bool has_msaa_sample_loc_bug = sctx->screen->info.has_msaa_sample_loc_bug;
3337
3338 /* Smoothing (only possible with nr_samples == 1) uses the same
3339 * sample locations as the MSAA it simulates.
3340 */
3341 if (nr_samples <= 1 && sctx->smoothing_enabled)
3342 nr_samples = SI_NUM_SMOOTH_AA_SAMPLES;
3343
3344 /* On Polaris, the small primitive filter uses the sample locations
3345 * even when MSAA is off, so we need to make sure they're set to 0.
3346 *
3347 * GFX10 uses sample locations unconditionally, so they always need
3348 * to be set up.
3349 */
3350 if ((nr_samples >= 2 || has_msaa_sample_loc_bug || sctx->chip_class >= GFX10) &&
3351 nr_samples != sctx->sample_locs_num_samples) {
3352 sctx->sample_locs_num_samples = nr_samples;
3353 si_emit_sample_locations(cs, nr_samples);
3354 }
3355
3356 if (sctx->family >= CHIP_POLARIS10) {
3357 unsigned small_prim_filter_cntl =
3358 S_028830_SMALL_PRIM_FILTER_ENABLE(1) |
3359 /* line bug */
3360 S_028830_LINE_FILTER_DISABLE(sctx->family <= CHIP_POLARIS12);
3361
3362 /* The alternative of setting sample locations to 0 would
3363 * require a DB flush to avoid Z errors, see
3364 * https://bugs.freedesktop.org/show_bug.cgi?id=96908
3365 */
3366 if (has_msaa_sample_loc_bug && sctx->framebuffer.nr_samples > 1 && !rs->multisample_enable)
3367 small_prim_filter_cntl &= C_028830_SMALL_PRIM_FILTER_ENABLE;
3368
3369 radeon_opt_set_context_reg(sctx, R_028830_PA_SU_SMALL_PRIM_FILTER_CNTL,
3370 SI_TRACKED_PA_SU_SMALL_PRIM_FILTER_CNTL, small_prim_filter_cntl);
3371 }
3372
3373 /* The exclusion bits can be set to improve rasterization efficiency
3374 * if no sample lies on the pixel boundary (-8 sample offset).
3375 */
3376 bool exclusion = sctx->chip_class >= GFX7 && (!rs->multisample_enable || nr_samples != 16);
3377 radeon_opt_set_context_reg(
3378 sctx, R_02882C_PA_SU_PRIM_FILTER_CNTL, SI_TRACKED_PA_SU_PRIM_FILTER_CNTL,
3379 S_02882C_XMAX_RIGHT_EXCLUSION(exclusion) | S_02882C_YMAX_BOTTOM_EXCLUSION(exclusion));
3380 }
3381
3382 static bool si_out_of_order_rasterization(struct si_context *sctx)
3383 {
3384 struct si_state_blend *blend = sctx->queued.named.blend;
3385 struct si_state_dsa *dsa = sctx->queued.named.dsa;
3386
3387 if (!sctx->screen->has_out_of_order_rast)
3388 return false;
3389
3390 unsigned colormask = sctx->framebuffer.colorbuf_enabled_4bit;
3391
3392 colormask &= blend->cb_target_enabled_4bit;
3393
3394 /* Conservative: No logic op. */
3395 if (colormask && blend->logicop_enable)
3396 return false;
3397
3398 struct si_dsa_order_invariance dsa_order_invariant = {.zs = true,
3399 .pass_set = true,
3400 .pass_last = false};
3401
3402 if (sctx->framebuffer.state.zsbuf) {
3403 struct si_texture *zstex = (struct si_texture *)sctx->framebuffer.state.zsbuf->texture;
3404 bool has_stencil = zstex->surface.has_stencil;
3405 dsa_order_invariant = dsa->order_invariance[has_stencil];
3406 if (!dsa_order_invariant.zs)
3407 return false;
3408
3409 /* The set of PS invocations is always order invariant,
3410 * except when early Z/S tests are requested. */
3411 if (sctx->ps_shader.cso && sctx->ps_shader.cso->info.writes_memory &&
3412 sctx->ps_shader.cso->info.properties[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL] &&
3413 !dsa_order_invariant.pass_set)
3414 return false;
3415
3416 if (sctx->num_perfect_occlusion_queries != 0 && !dsa_order_invariant.pass_set)
3417 return false;
3418 }
3419
3420 if (!colormask)
3421 return true;
3422
3423 unsigned blendmask = colormask & blend->blend_enable_4bit;
3424
3425 if (blendmask) {
3426 /* Only commutative blending. */
3427 if (blendmask & ~blend->commutative_4bit)
3428 return false;
3429
3430 if (!dsa_order_invariant.pass_set)
3431 return false;
3432 }
3433
3434 if (colormask & ~blendmask) {
3435 if (!dsa_order_invariant.pass_last)
3436 return false;
3437 }
3438
3439 return true;
3440 }
3441
3442 static void si_emit_msaa_config(struct si_context *sctx)
3443 {
3444 struct radeon_cmdbuf *cs = sctx->gfx_cs;
3445 unsigned num_tile_pipes = sctx->screen->info.num_tile_pipes;
3446 /* 33% faster rendering to linear color buffers */
3447 bool dst_is_linear = sctx->framebuffer.any_dst_linear;
3448 bool out_of_order_rast = si_out_of_order_rasterization(sctx);
3449 unsigned sc_mode_cntl_1 =
3450 S_028A4C_WALK_SIZE(dst_is_linear) | S_028A4C_WALK_FENCE_ENABLE(!dst_is_linear) |
3451 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |
3452 S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(out_of_order_rast) |
3453 S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7) |
3454 /* always 1: */
3455 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) | S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
3456 S_028A4C_TILE_WALK_ORDER_ENABLE(1) | S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
3457 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) | S_028A4C_FORCE_EOV_REZ_ENABLE(1);
3458 unsigned db_eqaa = S_028804_HIGH_QUALITY_INTERSECTIONS(1) | S_028804_INCOHERENT_EQAA_READS(1) |
3459 S_028804_INTERPOLATE_COMP_Z(1) | S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
3460 unsigned coverage_samples, color_samples, z_samples;
3461 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
3462
3463 /* S: Coverage samples (up to 16x):
3464 * - Scan conversion samples (PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES)
3465 * - CB FMASK samples (CB_COLORi_ATTRIB.NUM_SAMPLES)
3466 *
3467 * Z: Z/S samples (up to 8x, must be <= coverage samples and >= color samples):
3468 * - Value seen by DB (DB_Z_INFO.NUM_SAMPLES)
3469 * - Value seen by CB, must be correct even if Z/S is unbound (DB_EQAA.MAX_ANCHOR_SAMPLES)
3470 * # Missing samples are derived from Z planes if Z is compressed (up to 16x quality), or
3471 * # from the closest defined sample if Z is uncompressed (same quality as the number of
3472 * # Z samples).
3473 *
3474 * F: Color samples (up to 8x, must be <= coverage samples):
3475 * - CB color samples (CB_COLORi_ATTRIB.NUM_FRAGMENTS)
3476 * - PS iter samples (DB_EQAA.PS_ITER_SAMPLES)
3477 *
3478 * Can be anything between coverage and color samples:
3479 * - SampleMaskIn samples (PA_SC_AA_CONFIG.MSAA_EXPOSED_SAMPLES)
3480 * - SampleMaskOut samples (DB_EQAA.MASK_EXPORT_NUM_SAMPLES)
3481 * - Alpha-to-coverage samples (DB_EQAA.ALPHA_TO_MASK_NUM_SAMPLES)
3482 * - Occlusion query samples (DB_COUNT_CONTROL.SAMPLE_RATE)
3483 * # All are currently set the same as coverage samples.
3484 *
3485 * If color samples < coverage samples, FMASK has a higher bpp to store an "unknown"
3486 * flag for undefined color samples. A shader-based resolve must handle unknowns
3487 * or mask them out with AND. Unknowns can also be guessed from neighbors via
3488 * an edge-detect shader-based resolve, which is required to make "color samples = 1"
3489 * useful. The CB resolve always drops unknowns.
3490 *
3491 * Sensible AA configurations:
3492 * EQAA 16s 8z 8f - might look the same as 16x MSAA if Z is compressed
3493 * EQAA 16s 8z 4f - might look the same as 16x MSAA if Z is compressed
3494 * EQAA 16s 4z 4f - might look the same as 16x MSAA if Z is compressed
3495 * EQAA 8s 8z 8f = 8x MSAA
3496 * EQAA 8s 8z 4f - might look the same as 8x MSAA
3497 * EQAA 8s 8z 2f - might look the same as 8x MSAA with low-density geometry
3498 * EQAA 8s 4z 4f - might look the same as 8x MSAA if Z is compressed
3499 * EQAA 8s 4z 2f - might look the same as 8x MSAA with low-density geometry if Z is compressed
3500 * EQAA 4s 4z 4f = 4x MSAA
3501 * EQAA 4s 4z 2f - might look the same as 4x MSAA with low-density geometry
3502 * EQAA 2s 2z 2f = 2x MSAA
3503 */
3504 if (sctx->framebuffer.nr_samples > 1 && rs->multisample_enable) {
3505 coverage_samples = sctx->framebuffer.nr_samples;
3506 color_samples = sctx->framebuffer.nr_color_samples;
3507
3508 if (sctx->framebuffer.state.zsbuf) {
3509 z_samples = sctx->framebuffer.state.zsbuf->texture->nr_samples;
3510 z_samples = MAX2(1, z_samples);
3511 } else {
3512 z_samples = coverage_samples;
3513 }
3514 } else if (sctx->smoothing_enabled) {
3515 coverage_samples = color_samples = z_samples = SI_NUM_SMOOTH_AA_SAMPLES;
3516 } else {
3517 coverage_samples = color_samples = z_samples = 1;
3518 }
3519
3520 /* Required by OpenGL line rasterization.
3521 *
3522 * TODO: We should also enable perpendicular endcaps for AA lines,
3523 * but that requires implementing line stippling in the pixel
3524 * shader. SC can only do line stippling with axis-aligned
3525 * endcaps.
3526 */
3527 unsigned sc_line_cntl = S_028BDC_DX10_DIAMOND_TEST_ENA(1);
3528 unsigned sc_aa_config = 0;
3529
3530 if (coverage_samples > 1) {
3531 /* distance from the pixel center, indexed by log2(nr_samples) */
3532 static unsigned max_dist[] = {
3533 0, /* unused */
3534 4, /* 2x MSAA */
3535 6, /* 4x MSAA */
3536 7, /* 8x MSAA */
3537 8, /* 16x MSAA */
3538 };
3539 unsigned log_samples = util_logbase2(coverage_samples);
3540 unsigned log_z_samples = util_logbase2(z_samples);
3541 unsigned ps_iter_samples = si_get_ps_iter_samples(sctx);
3542 unsigned log_ps_iter_samples = util_logbase2(ps_iter_samples);
3543
3544 sc_line_cntl |= S_028BDC_EXPAND_LINE_WIDTH(1);
3545 sc_aa_config = S_028BE0_MSAA_NUM_SAMPLES(log_samples) |
3546 S_028BE0_MAX_SAMPLE_DIST(max_dist[log_samples]) |
3547 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples);
3548
3549 if (sctx->framebuffer.nr_samples > 1) {
3550 db_eqaa |= S_028804_MAX_ANCHOR_SAMPLES(log_z_samples) |
3551 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |
3552 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |
3553 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples);
3554 sc_mode_cntl_1 |= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1);
3555 } else if (sctx->smoothing_enabled) {
3556 db_eqaa |= S_028804_OVERRASTERIZATION_AMOUNT(log_samples);
3557 }
3558 }
3559
3560 unsigned initial_cdw = cs->current.cdw;
3561
3562 /* R_028BDC_PA_SC_LINE_CNTL, R_028BE0_PA_SC_AA_CONFIG */
3563 radeon_opt_set_context_reg2(sctx, R_028BDC_PA_SC_LINE_CNTL, SI_TRACKED_PA_SC_LINE_CNTL,
3564 sc_line_cntl, sc_aa_config);
3565 /* R_028804_DB_EQAA */
3566 radeon_opt_set_context_reg(sctx, R_028804_DB_EQAA, SI_TRACKED_DB_EQAA, db_eqaa);
3567 /* R_028A4C_PA_SC_MODE_CNTL_1 */
3568 radeon_opt_set_context_reg(sctx, R_028A4C_PA_SC_MODE_CNTL_1, SI_TRACKED_PA_SC_MODE_CNTL_1,
3569 sc_mode_cntl_1);
3570
3571 if (initial_cdw != cs->current.cdw) {
3572 sctx->context_roll = true;
3573
3574 /* GFX9: Flush DFSM when the AA mode changes. */
3575 if (sctx->screen->dfsm_allowed) {
3576 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
3577 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
3578 }
3579 }
3580 }
3581
3582 void si_update_ps_iter_samples(struct si_context *sctx)
3583 {
3584 if (sctx->framebuffer.nr_samples > 1)
3585 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
3586 if (sctx->screen->dpbb_allowed)
3587 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
3588 }
3589
3590 static void si_set_min_samples(struct pipe_context *ctx, unsigned min_samples)
3591 {
3592 struct si_context *sctx = (struct si_context *)ctx;
3593
3594 /* The hardware can only do sample shading with 2^n samples. */
3595 min_samples = util_next_power_of_two(min_samples);
3596
3597 if (sctx->ps_iter_samples == min_samples)
3598 return;
3599
3600 sctx->ps_iter_samples = min_samples;
3601 sctx->do_update_shaders = true;
3602
3603 si_update_ps_iter_samples(sctx);
3604 }
3605
3606 /*
3607 * Samplers
3608 */
3609
3610 /**
3611 * Build the sampler view descriptor for a buffer texture.
3612 * @param state 256-bit descriptor; only the high 128 bits are filled in
3613 */
3614 void si_make_buffer_descriptor(struct si_screen *screen, struct si_resource *buf,
3615 enum pipe_format format, unsigned offset, unsigned size,
3616 uint32_t *state)
3617 {
3618 const struct util_format_description *desc;
3619 unsigned stride;
3620 unsigned num_records;
3621
3622 desc = util_format_description(format);
3623 stride = desc->block.bits / 8;
3624
3625 num_records = size / stride;
3626 num_records = MIN2(num_records, (buf->b.b.width0 - offset) / stride);
3627
3628 /* The NUM_RECORDS field has a different meaning depending on the chip,
3629 * instruction type, STRIDE, and SWIZZLE_ENABLE.
3630 *
3631 * GFX6-7,10:
3632 * - If STRIDE == 0, it's in byte units.
3633 * - If STRIDE != 0, it's in units of STRIDE, used with inst.IDXEN.
3634 *
3635 * GFX8:
3636 * - For SMEM and STRIDE == 0, it's in byte units.
3637 * - For SMEM and STRIDE != 0, it's in units of STRIDE.
3638 * - For VMEM and STRIDE == 0 or SWIZZLE_ENABLE == 0, it's in byte units.
3639 * - For VMEM and STRIDE != 0 and SWIZZLE_ENABLE == 1, it's in units of STRIDE.
3640 * NOTE: There is incompatibility between VMEM and SMEM opcodes due to SWIZZLE_-
3641 * ENABLE. The workaround is to set STRIDE = 0 if SWIZZLE_ENABLE == 0 when
3642 * using SMEM. This can be done in the shader by clearing STRIDE with s_and.
3643 * That way the same descriptor can be used by both SMEM and VMEM.
3644 *
3645 * GFX9:
3646 * - For SMEM and STRIDE == 0, it's in byte units.
3647 * - For SMEM and STRIDE != 0, it's in units of STRIDE.
3648 * - For VMEM and inst.IDXEN == 0 or STRIDE == 0, it's in byte units.
3649 * - For VMEM and inst.IDXEN == 1 and STRIDE != 0, it's in units of STRIDE.
3650 */
3651 if (screen->info.chip_class == GFX8)
3652 num_records *= stride;
3653
3654 state[4] = 0;
3655 state[5] = S_008F04_STRIDE(stride);
3656 state[6] = num_records;
3657 state[7] = S_008F0C_DST_SEL_X(si_map_swizzle(desc->swizzle[0])) |
3658 S_008F0C_DST_SEL_Y(si_map_swizzle(desc->swizzle[1])) |
3659 S_008F0C_DST_SEL_Z(si_map_swizzle(desc->swizzle[2])) |
3660 S_008F0C_DST_SEL_W(si_map_swizzle(desc->swizzle[3]));
3661
3662 if (screen->info.chip_class >= GFX10) {
3663 const struct gfx10_format *fmt = &gfx10_format_table[format];
3664
3665 /* OOB_SELECT chooses the out-of-bounds check:
3666 * - 0: (index >= NUM_RECORDS) || (offset >= STRIDE)
3667 * - 1: index >= NUM_RECORDS
3668 * - 2: NUM_RECORDS == 0
3669 * - 3: if SWIZZLE_ENABLE == 0: offset >= NUM_RECORDS
3670 * else: swizzle_address >= NUM_RECORDS
3671 */
3672 state[7] |= S_008F0C_FORMAT(fmt->img_format) |
3673 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_STRUCTURED_WITH_OFFSET) |
3674 S_008F0C_RESOURCE_LEVEL(1);
3675 } else {
3676 int first_non_void;
3677 unsigned num_format, data_format;
3678
3679 first_non_void = util_format_get_first_non_void_channel(format);
3680 num_format = si_translate_buffer_numformat(&screen->b, desc, first_non_void);
3681 data_format = si_translate_buffer_dataformat(&screen->b, desc, first_non_void);
3682
3683 state[7] |= S_008F0C_NUM_FORMAT(num_format) | S_008F0C_DATA_FORMAT(data_format);
3684 }
3685 }
3686
3687 static unsigned gfx9_border_color_swizzle(const unsigned char swizzle[4])
3688 {
3689 unsigned bc_swizzle = V_008F20_BC_SWIZZLE_XYZW;
3690
3691 if (swizzle[3] == PIPE_SWIZZLE_X) {
3692 /* For the pre-defined border color values (white, opaque
3693 * black, transparent black), the only thing that matters is
3694 * that the alpha channel winds up in the correct place
3695 * (because the RGB channels are all the same) so either of
3696 * these enumerations will work.
3697 */
3698 if (swizzle[2] == PIPE_SWIZZLE_Y)
3699 bc_swizzle = V_008F20_BC_SWIZZLE_WZYX;
3700 else
3701 bc_swizzle = V_008F20_BC_SWIZZLE_WXYZ;
3702 } else if (swizzle[0] == PIPE_SWIZZLE_X) {
3703 if (swizzle[1] == PIPE_SWIZZLE_Y)
3704 bc_swizzle = V_008F20_BC_SWIZZLE_XYZW;
3705 else
3706 bc_swizzle = V_008F20_BC_SWIZZLE_XWYZ;
3707 } else if (swizzle[1] == PIPE_SWIZZLE_X) {
3708 bc_swizzle = V_008F20_BC_SWIZZLE_YXWZ;
3709 } else if (swizzle[2] == PIPE_SWIZZLE_X) {
3710 bc_swizzle = V_008F20_BC_SWIZZLE_ZYXW;
3711 }
3712
3713 return bc_swizzle;
3714 }
3715
3716 /**
3717 * Build the sampler view descriptor for a texture.
3718 */
3719 static void gfx10_make_texture_descriptor(
3720 struct si_screen *screen, struct si_texture *tex, bool sampler, enum pipe_texture_target target,
3721 enum pipe_format pipe_format, const unsigned char state_swizzle[4], unsigned first_level,
3722 unsigned last_level, unsigned first_layer, unsigned last_layer, unsigned width, unsigned height,
3723 unsigned depth, uint32_t *state, uint32_t *fmask_state)
3724 {
3725 struct pipe_resource *res = &tex->buffer.b.b;
3726 const struct util_format_description *desc;
3727 unsigned img_format;
3728 unsigned char swizzle[4];
3729 unsigned type;
3730 uint64_t va;
3731
3732 desc = util_format_description(pipe_format);
3733 img_format = gfx10_format_table[pipe_format].img_format;
3734
3735 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
3736 const unsigned char swizzle_xxxx[4] = {0, 0, 0, 0};
3737 const unsigned char swizzle_yyyy[4] = {1, 1, 1, 1};
3738 const unsigned char swizzle_wwww[4] = {3, 3, 3, 3};
3739 bool is_stencil = false;
3740
3741 switch (pipe_format) {
3742 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
3743 case PIPE_FORMAT_X32_S8X24_UINT:
3744 case PIPE_FORMAT_X8Z24_UNORM:
3745 util_format_compose_swizzles(swizzle_yyyy, state_swizzle, swizzle);
3746 is_stencil = true;
3747 break;
3748 case PIPE_FORMAT_X24S8_UINT:
3749 /*
3750 * X24S8 is implemented as an 8_8_8_8 data format, to
3751 * fix texture gathers. This affects at least
3752 * GL45-CTS.texture_cube_map_array.sampling on GFX8.
3753 */
3754 util_format_compose_swizzles(swizzle_wwww, state_swizzle, swizzle);
3755 is_stencil = true;
3756 break;
3757 default:
3758 util_format_compose_swizzles(swizzle_xxxx, state_swizzle, swizzle);
3759 is_stencil = pipe_format == PIPE_FORMAT_S8_UINT;
3760 }
3761
3762 if (tex->upgraded_depth && !is_stencil) {
3763 assert(img_format == V_008F0C_IMG_FORMAT_32_FLOAT);
3764 img_format = V_008F0C_IMG_FORMAT_32_FLOAT_CLAMP;
3765 }
3766 } else {
3767 util_format_compose_swizzles(desc->swizzle, state_swizzle, swizzle);
3768 }
3769
3770 if (!sampler && (res->target == PIPE_TEXTURE_CUBE || res->target == PIPE_TEXTURE_CUBE_ARRAY)) {
3771 /* For the purpose of shader images, treat cube maps as 2D
3772 * arrays.
3773 */
3774 type = V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
3775 } else {
3776 type = si_tex_dim(screen, tex, target, res->nr_samples);
3777 }
3778
3779 if (type == V_008F1C_SQ_RSRC_IMG_1D_ARRAY) {
3780 height = 1;
3781 depth = res->array_size;
3782 } else if (type == V_008F1C_SQ_RSRC_IMG_2D_ARRAY || type == V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY) {
3783 if (sampler || res->target != PIPE_TEXTURE_3D)
3784 depth = res->array_size;
3785 } else if (type == V_008F1C_SQ_RSRC_IMG_CUBE)
3786 depth = res->array_size / 6;
3787
3788 state[0] = 0;
3789 state[1] = S_00A004_FORMAT(img_format) | S_00A004_WIDTH_LO(width - 1);
3790 state[2] = S_00A008_WIDTH_HI((width - 1) >> 2) | S_00A008_HEIGHT(height - 1) |
3791 S_00A008_RESOURCE_LEVEL(1);
3792 state[3] =
3793 S_00A00C_DST_SEL_X(si_map_swizzle(swizzle[0])) |
3794 S_00A00C_DST_SEL_Y(si_map_swizzle(swizzle[1])) |
3795 S_00A00C_DST_SEL_Z(si_map_swizzle(swizzle[2])) |
3796 S_00A00C_DST_SEL_W(si_map_swizzle(swizzle[3])) |
3797 S_00A00C_BASE_LEVEL(res->nr_samples > 1 ? 0 : first_level) |
3798 S_00A00C_LAST_LEVEL(res->nr_samples > 1 ? util_logbase2(res->nr_samples) : last_level) |
3799 S_00A00C_BC_SWIZZLE(gfx9_border_color_swizzle(desc->swizzle)) | S_00A00C_TYPE(type);
3800 /* Depth is the the last accessible layer on gfx9+. The hw doesn't need
3801 * to know the total number of layers.
3802 */
3803 state[4] =
3804 S_00A010_DEPTH((type == V_008F1C_SQ_RSRC_IMG_3D && sampler) ? depth - 1 : last_layer) |
3805 S_00A010_BASE_ARRAY(first_layer);
3806 state[5] = S_00A014_ARRAY_PITCH(!!(type == V_008F1C_SQ_RSRC_IMG_3D && !sampler)) |
3807 S_00A014_MAX_MIP(res->nr_samples > 1 ? util_logbase2(res->nr_samples)
3808 : tex->buffer.b.b.last_level) |
3809 S_00A014_PERF_MOD(4);
3810 state[6] = 0;
3811 state[7] = 0;
3812
3813 if (vi_dcc_enabled(tex, first_level)) {
3814 state[6] |= S_00A018_MAX_UNCOMPRESSED_BLOCK_SIZE(V_028C78_MAX_BLOCK_SIZE_256B) |
3815 S_00A018_MAX_COMPRESSED_BLOCK_SIZE(tex->surface.u.gfx9.dcc.max_compressed_block_size) |
3816 S_00A018_ALPHA_IS_ON_MSB(vi_alpha_is_on_msb(screen, pipe_format));
3817 }
3818
3819 /* Initialize the sampler view for FMASK. */
3820 if (tex->surface.fmask_offset) {
3821 uint32_t format;
3822
3823 va = tex->buffer.gpu_address + tex->surface.fmask_offset;
3824
3825 #define FMASK(s, f) (((unsigned)(MAX2(1, s)) * 16) + (MAX2(1, f)))
3826 switch (FMASK(res->nr_samples, res->nr_storage_samples)) {
3827 case FMASK(2, 1):
3828 format = V_008F0C_IMG_FORMAT_FMASK8_S2_F1;
3829 break;
3830 case FMASK(2, 2):
3831 format = V_008F0C_IMG_FORMAT_FMASK8_S2_F2;
3832 break;
3833 case FMASK(4, 1):
3834 format = V_008F0C_IMG_FORMAT_FMASK8_S4_F1;
3835 break;
3836 case FMASK(4, 2):
3837 format = V_008F0C_IMG_FORMAT_FMASK8_S4_F2;
3838 break;
3839 case FMASK(4, 4):
3840 format = V_008F0C_IMG_FORMAT_FMASK8_S4_F4;
3841 break;
3842 case FMASK(8, 1):
3843 format = V_008F0C_IMG_FORMAT_FMASK8_S8_F1;
3844 break;
3845 case FMASK(8, 2):
3846 format = V_008F0C_IMG_FORMAT_FMASK16_S8_F2;
3847 break;
3848 case FMASK(8, 4):
3849 format = V_008F0C_IMG_FORMAT_FMASK32_S8_F4;
3850 break;
3851 case FMASK(8, 8):
3852 format = V_008F0C_IMG_FORMAT_FMASK32_S8_F8;
3853 break;
3854 case FMASK(16, 1):
3855 format = V_008F0C_IMG_FORMAT_FMASK16_S16_F1;
3856 break;
3857 case FMASK(16, 2):
3858 format = V_008F0C_IMG_FORMAT_FMASK32_S16_F2;
3859 break;
3860 case FMASK(16, 4):
3861 format = V_008F0C_IMG_FORMAT_FMASK64_S16_F4;
3862 break;
3863 case FMASK(16, 8):
3864 format = V_008F0C_IMG_FORMAT_FMASK64_S16_F8;
3865 break;
3866 default:
3867 unreachable("invalid nr_samples");
3868 }
3869 #undef FMASK
3870 fmask_state[0] = (va >> 8) | tex->surface.fmask_tile_swizzle;
3871 fmask_state[1] = S_00A004_BASE_ADDRESS_HI(va >> 40) | S_00A004_FORMAT(format) |
3872 S_00A004_WIDTH_LO(width - 1);
3873 fmask_state[2] = S_00A008_WIDTH_HI((width - 1) >> 2) | S_00A008_HEIGHT(height - 1) |
3874 S_00A008_RESOURCE_LEVEL(1);
3875 fmask_state[3] =
3876 S_00A00C_DST_SEL_X(V_008F1C_SQ_SEL_X) | S_00A00C_DST_SEL_Y(V_008F1C_SQ_SEL_X) |
3877 S_00A00C_DST_SEL_Z(V_008F1C_SQ_SEL_X) | S_00A00C_DST_SEL_W(V_008F1C_SQ_SEL_X) |
3878 S_00A00C_SW_MODE(tex->surface.u.gfx9.fmask.swizzle_mode) |
3879 S_00A00C_TYPE(si_tex_dim(screen, tex, target, 0));
3880 fmask_state[4] = S_00A010_DEPTH(last_layer) | S_00A010_BASE_ARRAY(first_layer);
3881 fmask_state[5] = 0;
3882 fmask_state[6] = S_00A018_META_PIPE_ALIGNED(1);
3883 fmask_state[7] = 0;
3884 }
3885 }
3886
3887 /**
3888 * Build the sampler view descriptor for a texture (SI-GFX9).
3889 */
3890 static void si_make_texture_descriptor(struct si_screen *screen, struct si_texture *tex,
3891 bool sampler, enum pipe_texture_target target,
3892 enum pipe_format pipe_format,
3893 const unsigned char state_swizzle[4], unsigned first_level,
3894 unsigned last_level, unsigned first_layer,
3895 unsigned last_layer, unsigned width, unsigned height,
3896 unsigned depth, uint32_t *state, uint32_t *fmask_state)
3897 {
3898 struct pipe_resource *res = &tex->buffer.b.b;
3899 const struct util_format_description *desc;
3900 unsigned char swizzle[4];
3901 int first_non_void;
3902 unsigned num_format, data_format, type, num_samples;
3903 uint64_t va;
3904
3905 desc = util_format_description(pipe_format);
3906
3907 num_samples = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? MAX2(1, res->nr_samples)
3908 : MAX2(1, res->nr_storage_samples);
3909
3910 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
3911 const unsigned char swizzle_xxxx[4] = {0, 0, 0, 0};
3912 const unsigned char swizzle_yyyy[4] = {1, 1, 1, 1};
3913 const unsigned char swizzle_wwww[4] = {3, 3, 3, 3};
3914
3915 switch (pipe_format) {
3916 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
3917 case PIPE_FORMAT_X32_S8X24_UINT:
3918 case PIPE_FORMAT_X8Z24_UNORM:
3919 util_format_compose_swizzles(swizzle_yyyy, state_swizzle, swizzle);
3920 break;
3921 case PIPE_FORMAT_X24S8_UINT:
3922 /*
3923 * X24S8 is implemented as an 8_8_8_8 data format, to
3924 * fix texture gathers. This affects at least
3925 * GL45-CTS.texture_cube_map_array.sampling on GFX8.
3926 */
3927 if (screen->info.chip_class <= GFX8)
3928 util_format_compose_swizzles(swizzle_wwww, state_swizzle, swizzle);
3929 else
3930 util_format_compose_swizzles(swizzle_yyyy, state_swizzle, swizzle);
3931 break;
3932 default:
3933 util_format_compose_swizzles(swizzle_xxxx, state_swizzle, swizzle);
3934 }
3935 } else {
3936 util_format_compose_swizzles(desc->swizzle, state_swizzle, swizzle);
3937 }
3938
3939 first_non_void = util_format_get_first_non_void_channel(pipe_format);
3940
3941 switch (pipe_format) {
3942 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
3943 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3944 break;
3945 default:
3946 if (first_non_void < 0) {
3947 if (util_format_is_compressed(pipe_format)) {
3948 switch (pipe_format) {
3949 case PIPE_FORMAT_DXT1_SRGB:
3950 case PIPE_FORMAT_DXT1_SRGBA:
3951 case PIPE_FORMAT_DXT3_SRGBA:
3952 case PIPE_FORMAT_DXT5_SRGBA:
3953 case PIPE_FORMAT_BPTC_SRGBA:
3954 case PIPE_FORMAT_ETC2_SRGB8:
3955 case PIPE_FORMAT_ETC2_SRGB8A1:
3956 case PIPE_FORMAT_ETC2_SRGBA8:
3957 num_format = V_008F14_IMG_NUM_FORMAT_SRGB;
3958 break;
3959 case PIPE_FORMAT_RGTC1_SNORM:
3960 case PIPE_FORMAT_LATC1_SNORM:
3961 case PIPE_FORMAT_RGTC2_SNORM:
3962 case PIPE_FORMAT_LATC2_SNORM:
3963 case PIPE_FORMAT_ETC2_R11_SNORM:
3964 case PIPE_FORMAT_ETC2_RG11_SNORM:
3965 /* implies float, so use SNORM/UNORM to determine
3966 whether data is signed or not */
3967 case PIPE_FORMAT_BPTC_RGB_FLOAT:
3968 num_format = V_008F14_IMG_NUM_FORMAT_SNORM;
3969 break;
3970 default:
3971 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3972 break;
3973 }
3974 } else if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
3975 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3976 } else {
3977 num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
3978 }
3979 } else if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
3980 num_format = V_008F14_IMG_NUM_FORMAT_SRGB;
3981 } else {
3982 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3983
3984 switch (desc->channel[first_non_void].type) {
3985 case UTIL_FORMAT_TYPE_FLOAT:
3986 num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
3987 break;
3988 case UTIL_FORMAT_TYPE_SIGNED:
3989 if (desc->channel[first_non_void].normalized)
3990 num_format = V_008F14_IMG_NUM_FORMAT_SNORM;
3991 else if (desc->channel[first_non_void].pure_integer)
3992 num_format = V_008F14_IMG_NUM_FORMAT_SINT;
3993 else
3994 num_format = V_008F14_IMG_NUM_FORMAT_SSCALED;
3995 break;
3996 case UTIL_FORMAT_TYPE_UNSIGNED:
3997 if (desc->channel[first_non_void].normalized)
3998 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3999 else if (desc->channel[first_non_void].pure_integer)
4000 num_format = V_008F14_IMG_NUM_FORMAT_UINT;
4001 else
4002 num_format = V_008F14_IMG_NUM_FORMAT_USCALED;
4003 }
4004 }
4005 }
4006
4007 data_format = si_translate_texformat(&screen->b, pipe_format, desc, first_non_void);
4008 if (data_format == ~0) {
4009 data_format = 0;
4010 }
4011
4012 /* S8 with Z32 HTILE needs a special format. */
4013 if (screen->info.chip_class == GFX9 && pipe_format == PIPE_FORMAT_S8_UINT)
4014 data_format = V_008F14_IMG_DATA_FORMAT_S8_32;
4015
4016 if (!sampler && (res->target == PIPE_TEXTURE_CUBE || res->target == PIPE_TEXTURE_CUBE_ARRAY ||
4017 (screen->info.chip_class <= GFX8 && res->target == PIPE_TEXTURE_3D))) {
4018 /* For the purpose of shader images, treat cube maps and 3D
4019 * textures as 2D arrays. For 3D textures, the address
4020 * calculations for mipmaps are different, so we rely on the
4021 * caller to effectively disable mipmaps.
4022 */
4023 type = V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
4024
4025 assert(res->target != PIPE_TEXTURE_3D || (first_level == 0 && last_level == 0));
4026 } else {
4027 type = si_tex_dim(screen, tex, target, num_samples);
4028 }
4029
4030 if (type == V_008F1C_SQ_RSRC_IMG_1D_ARRAY) {
4031 height = 1;
4032 depth = res->array_size;
4033 } else if (type == V_008F1C_SQ_RSRC_IMG_2D_ARRAY || type == V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY) {
4034 if (sampler || res->target != PIPE_TEXTURE_3D)
4035 depth = res->array_size;
4036 } else if (type == V_008F1C_SQ_RSRC_IMG_CUBE)
4037 depth = res->array_size / 6;
4038
4039 state[0] = 0;
4040 state[1] = (S_008F14_DATA_FORMAT(data_format) | S_008F14_NUM_FORMAT(num_format));
4041 state[2] = (S_008F18_WIDTH(width - 1) | S_008F18_HEIGHT(height - 1) | S_008F18_PERF_MOD(4));
4042 state[3] = (S_008F1C_DST_SEL_X(si_map_swizzle(swizzle[0])) |
4043 S_008F1C_DST_SEL_Y(si_map_swizzle(swizzle[1])) |
4044 S_008F1C_DST_SEL_Z(si_map_swizzle(swizzle[2])) |
4045 S_008F1C_DST_SEL_W(si_map_swizzle(swizzle[3])) |
4046 S_008F1C_BASE_LEVEL(num_samples > 1 ? 0 : first_level) |
4047 S_008F1C_LAST_LEVEL(num_samples > 1 ? util_logbase2(num_samples) : last_level) |
4048 S_008F1C_TYPE(type));
4049 state[4] = 0;
4050 state[5] = S_008F24_BASE_ARRAY(first_layer);
4051 state[6] = 0;
4052 state[7] = 0;
4053
4054 if (screen->info.chip_class == GFX9) {
4055 unsigned bc_swizzle = gfx9_border_color_swizzle(desc->swizzle);
4056
4057 /* Depth is the the last accessible layer on Gfx9.
4058 * The hw doesn't need to know the total number of layers.
4059 */
4060 if (type == V_008F1C_SQ_RSRC_IMG_3D)
4061 state[4] |= S_008F20_DEPTH(depth - 1);
4062 else
4063 state[4] |= S_008F20_DEPTH(last_layer);
4064
4065 state[4] |= S_008F20_BC_SWIZZLE(bc_swizzle);
4066 state[5] |= S_008F24_MAX_MIP(num_samples > 1 ? util_logbase2(num_samples)
4067 : tex->buffer.b.b.last_level);
4068 } else {
4069 state[3] |= S_008F1C_POW2_PAD(res->last_level > 0);
4070 state[4] |= S_008F20_DEPTH(depth - 1);
4071 state[5] |= S_008F24_LAST_ARRAY(last_layer);
4072 }
4073
4074 if (vi_dcc_enabled(tex, first_level)) {
4075 state[6] = S_008F28_ALPHA_IS_ON_MSB(vi_alpha_is_on_msb(screen, pipe_format));
4076 } else {
4077 /* The last dword is unused by hw. The shader uses it to clear
4078 * bits in the first dword of sampler state.
4079 */
4080 if (screen->info.chip_class <= GFX7 && res->nr_samples <= 1) {
4081 if (first_level == last_level)
4082 state[7] = C_008F30_MAX_ANISO_RATIO;
4083 else
4084 state[7] = 0xffffffff;
4085 }
4086 }
4087
4088 /* Initialize the sampler view for FMASK. */
4089 if (tex->surface.fmask_offset) {
4090 uint32_t data_format, num_format;
4091
4092 va = tex->buffer.gpu_address + tex->surface.fmask_offset;
4093
4094 #define FMASK(s, f) (((unsigned)(MAX2(1, s)) * 16) + (MAX2(1, f)))
4095 if (screen->info.chip_class == GFX9) {
4096 data_format = V_008F14_IMG_DATA_FORMAT_FMASK;
4097 switch (FMASK(res->nr_samples, res->nr_storage_samples)) {
4098 case FMASK(2, 1):
4099 num_format = V_008F14_IMG_FMASK_8_2_1;
4100 break;
4101 case FMASK(2, 2):
4102 num_format = V_008F14_IMG_FMASK_8_2_2;
4103 break;
4104 case FMASK(4, 1):
4105 num_format = V_008F14_IMG_FMASK_8_4_1;
4106 break;
4107 case FMASK(4, 2):
4108 num_format = V_008F14_IMG_FMASK_8_4_2;
4109 break;
4110 case FMASK(4, 4):
4111 num_format = V_008F14_IMG_FMASK_8_4_4;
4112 break;
4113 case FMASK(8, 1):
4114 num_format = V_008F14_IMG_FMASK_8_8_1;
4115 break;
4116 case FMASK(8, 2):
4117 num_format = V_008F14_IMG_FMASK_16_8_2;
4118 break;
4119 case FMASK(8, 4):
4120 num_format = V_008F14_IMG_FMASK_32_8_4;
4121 break;
4122 case FMASK(8, 8):
4123 num_format = V_008F14_IMG_FMASK_32_8_8;
4124 break;
4125 case FMASK(16, 1):
4126 num_format = V_008F14_IMG_FMASK_16_16_1;
4127 break;
4128 case FMASK(16, 2):
4129 num_format = V_008F14_IMG_FMASK_32_16_2;
4130 break;
4131 case FMASK(16, 4):
4132 num_format = V_008F14_IMG_FMASK_64_16_4;
4133 break;
4134 case FMASK(16, 8):
4135 num_format = V_008F14_IMG_FMASK_64_16_8;
4136 break;
4137 default:
4138 unreachable("invalid nr_samples");
4139 }
4140 } else {
4141 switch (FMASK(res->nr_samples, res->nr_storage_samples)) {
4142 case FMASK(2, 1):
4143 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F1;
4144 break;
4145 case FMASK(2, 2):
4146 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2;
4147 break;
4148 case FMASK(4, 1):
4149 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F1;
4150 break;
4151 case FMASK(4, 2):
4152 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F2;
4153 break;
4154 case FMASK(4, 4):
4155 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4;
4156 break;
4157 case FMASK(8, 1):
4158 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S8_F1;
4159 break;
4160 case FMASK(8, 2):
4161 data_format = V_008F14_IMG_DATA_FORMAT_FMASK16_S8_F2;
4162 break;
4163 case FMASK(8, 4):
4164 data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F4;
4165 break;
4166 case FMASK(8, 8):
4167 data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8;
4168 break;
4169 case FMASK(16, 1):
4170 data_format = V_008F14_IMG_DATA_FORMAT_FMASK16_S16_F1;
4171 break;
4172 case FMASK(16, 2):
4173 data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S16_F2;
4174 break;
4175 case FMASK(16, 4):
4176 data_format = V_008F14_IMG_DATA_FORMAT_FMASK64_S16_F4;
4177 break;
4178 case FMASK(16, 8):
4179 data_format = V_008F14_IMG_DATA_FORMAT_FMASK64_S16_F8;
4180 break;
4181 default:
4182 unreachable("invalid nr_samples");
4183 }
4184 num_format = V_008F14_IMG_NUM_FORMAT_UINT;
4185 }
4186 #undef FMASK
4187
4188 fmask_state[0] = (va >> 8) | tex->surface.fmask_tile_swizzle;
4189 fmask_state[1] = S_008F14_BASE_ADDRESS_HI(va >> 40) | S_008F14_DATA_FORMAT(data_format) |
4190 S_008F14_NUM_FORMAT(num_format);
4191 fmask_state[2] = S_008F18_WIDTH(width - 1) | S_008F18_HEIGHT(height - 1);
4192 fmask_state[3] =
4193 S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X) | S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X) |
4194 S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X) | S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X) |
4195 S_008F1C_TYPE(si_tex_dim(screen, tex, target, 0));
4196 fmask_state[4] = 0;
4197 fmask_state[5] = S_008F24_BASE_ARRAY(first_layer);
4198 fmask_state[6] = 0;
4199 fmask_state[7] = 0;
4200
4201 if (screen->info.chip_class == GFX9) {
4202 fmask_state[3] |= S_008F1C_SW_MODE(tex->surface.u.gfx9.fmask.swizzle_mode);
4203 fmask_state[4] |=
4204 S_008F20_DEPTH(last_layer) | S_008F20_PITCH(tex->surface.u.gfx9.fmask.epitch);
4205 fmask_state[5] |= S_008F24_META_PIPE_ALIGNED(1) |
4206 S_008F24_META_RB_ALIGNED(1);
4207 } else {
4208 fmask_state[3] |= S_008F1C_TILING_INDEX(tex->surface.u.legacy.fmask.tiling_index);
4209 fmask_state[4] |= S_008F20_DEPTH(depth - 1) |
4210 S_008F20_PITCH(tex->surface.u.legacy.fmask.pitch_in_pixels - 1);
4211 fmask_state[5] |= S_008F24_LAST_ARRAY(last_layer);
4212 }
4213 }
4214 }
4215
4216 /**
4217 * Create a sampler view.
4218 *
4219 * @param ctx context
4220 * @param texture texture
4221 * @param state sampler view template
4222 * @param width0 width0 override (for compressed textures as int)
4223 * @param height0 height0 override (for compressed textures as int)
4224 * @param force_level set the base address to the level (for compressed textures)
4225 */
4226 struct pipe_sampler_view *si_create_sampler_view_custom(struct pipe_context *ctx,
4227 struct pipe_resource *texture,
4228 const struct pipe_sampler_view *state,
4229 unsigned width0, unsigned height0,
4230 unsigned force_level)
4231 {
4232 struct si_context *sctx = (struct si_context *)ctx;
4233 struct si_sampler_view *view = CALLOC_STRUCT(si_sampler_view);
4234 struct si_texture *tex = (struct si_texture *)texture;
4235 unsigned base_level, first_level, last_level;
4236 unsigned char state_swizzle[4];
4237 unsigned height, depth, width;
4238 unsigned last_layer = state->u.tex.last_layer;
4239 enum pipe_format pipe_format;
4240 const struct legacy_surf_level *surflevel;
4241
4242 if (!view)
4243 return NULL;
4244
4245 /* initialize base object */
4246 view->base = *state;
4247 view->base.texture = NULL;
4248 view->base.reference.count = 1;
4249 view->base.context = ctx;
4250
4251 assert(texture);
4252 pipe_resource_reference(&view->base.texture, texture);
4253
4254 if (state->format == PIPE_FORMAT_X24S8_UINT || state->format == PIPE_FORMAT_S8X24_UINT ||
4255 state->format == PIPE_FORMAT_X32_S8X24_UINT || state->format == PIPE_FORMAT_S8_UINT)
4256 view->is_stencil_sampler = true;
4257
4258 /* Buffer resource. */
4259 if (texture->target == PIPE_BUFFER) {
4260 si_make_buffer_descriptor(sctx->screen, si_resource(texture), state->format,
4261 state->u.buf.offset, state->u.buf.size, view->state);
4262 return &view->base;
4263 }
4264
4265 state_swizzle[0] = state->swizzle_r;
4266 state_swizzle[1] = state->swizzle_g;
4267 state_swizzle[2] = state->swizzle_b;
4268 state_swizzle[3] = state->swizzle_a;
4269
4270 base_level = 0;
4271 first_level = state->u.tex.first_level;
4272 last_level = state->u.tex.last_level;
4273 width = width0;
4274 height = height0;
4275 depth = texture->depth0;
4276
4277 if (sctx->chip_class <= GFX8 && force_level) {
4278 assert(force_level == first_level && force_level == last_level);
4279 base_level = force_level;
4280 first_level = 0;
4281 last_level = 0;
4282 width = u_minify(width, force_level);
4283 height = u_minify(height, force_level);
4284 depth = u_minify(depth, force_level);
4285 }
4286
4287 /* This is not needed if gallium frontends set last_layer correctly. */
4288 if (state->target == PIPE_TEXTURE_1D || state->target == PIPE_TEXTURE_2D ||
4289 state->target == PIPE_TEXTURE_RECT || state->target == PIPE_TEXTURE_CUBE)
4290 last_layer = state->u.tex.first_layer;
4291
4292 /* Texturing with separate depth and stencil. */
4293 pipe_format = state->format;
4294
4295 /* Depth/stencil texturing sometimes needs separate texture. */
4296 if (tex->is_depth && !si_can_sample_zs(tex, view->is_stencil_sampler)) {
4297 if (!tex->flushed_depth_texture && !si_init_flushed_depth_texture(ctx, texture)) {
4298 pipe_resource_reference(&view->base.texture, NULL);
4299 FREE(view);
4300 return NULL;
4301 }
4302
4303 assert(tex->flushed_depth_texture);
4304
4305 /* Override format for the case where the flushed texture
4306 * contains only Z or only S.
4307 */
4308 if (tex->flushed_depth_texture->buffer.b.b.format != tex->buffer.b.b.format)
4309 pipe_format = tex->flushed_depth_texture->buffer.b.b.format;
4310
4311 tex = tex->flushed_depth_texture;
4312 }
4313
4314 surflevel = tex->surface.u.legacy.level;
4315
4316 if (tex->db_compatible) {
4317 if (!view->is_stencil_sampler)
4318 pipe_format = tex->db_render_format;
4319
4320 switch (pipe_format) {
4321 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
4322 pipe_format = PIPE_FORMAT_Z32_FLOAT;
4323 break;
4324 case PIPE_FORMAT_X8Z24_UNORM:
4325 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
4326 /* Z24 is always stored like this for DB
4327 * compatibility.
4328 */
4329 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
4330 break;
4331 case PIPE_FORMAT_X24S8_UINT:
4332 case PIPE_FORMAT_S8X24_UINT:
4333 case PIPE_FORMAT_X32_S8X24_UINT:
4334 pipe_format = PIPE_FORMAT_S8_UINT;
4335 surflevel = tex->surface.u.legacy.stencil_level;
4336 break;
4337 default:;
4338 }
4339 }
4340
4341 view->dcc_incompatible =
4342 vi_dcc_formats_are_incompatible(texture, state->u.tex.first_level, state->format);
4343
4344 sctx->screen->make_texture_descriptor(
4345 sctx->screen, tex, true, state->target, pipe_format, state_swizzle, first_level, last_level,
4346 state->u.tex.first_layer, last_layer, width, height, depth, view->state, view->fmask_state);
4347
4348 const struct util_format_description *desc = util_format_description(pipe_format);
4349 view->is_integer = false;
4350
4351 for (unsigned i = 0; i < desc->nr_channels; ++i) {
4352 if (desc->channel[i].type == UTIL_FORMAT_TYPE_VOID)
4353 continue;
4354
4355 /* Whether the number format is {U,S}{SCALED,INT} */
4356 view->is_integer = (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED ||
4357 desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) &&
4358 (desc->channel[i].pure_integer || !desc->channel[i].normalized);
4359 break;
4360 }
4361
4362 view->base_level_info = &surflevel[base_level];
4363 view->base_level = base_level;
4364 view->block_width = util_format_get_blockwidth(pipe_format);
4365 return &view->base;
4366 }
4367
4368 static struct pipe_sampler_view *si_create_sampler_view(struct pipe_context *ctx,
4369 struct pipe_resource *texture,
4370 const struct pipe_sampler_view *state)
4371 {
4372 return si_create_sampler_view_custom(ctx, texture, state, texture ? texture->width0 : 0,
4373 texture ? texture->height0 : 0, 0);
4374 }
4375
4376 static void si_sampler_view_destroy(struct pipe_context *ctx, struct pipe_sampler_view *state)
4377 {
4378 struct si_sampler_view *view = (struct si_sampler_view *)state;
4379
4380 pipe_resource_reference(&state->texture, NULL);
4381 FREE(view);
4382 }
4383
4384 static bool wrap_mode_uses_border_color(unsigned wrap, bool linear_filter)
4385 {
4386 return wrap == PIPE_TEX_WRAP_CLAMP_TO_BORDER || wrap == PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER ||
4387 (linear_filter && (wrap == PIPE_TEX_WRAP_CLAMP || wrap == PIPE_TEX_WRAP_MIRROR_CLAMP));
4388 }
4389
4390 static uint32_t si_translate_border_color(struct si_context *sctx,
4391 const struct pipe_sampler_state *state,
4392 const union pipe_color_union *color, bool is_integer)
4393 {
4394 bool linear_filter = state->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
4395 state->mag_img_filter != PIPE_TEX_FILTER_NEAREST;
4396
4397 if (!wrap_mode_uses_border_color(state->wrap_s, linear_filter) &&
4398 !wrap_mode_uses_border_color(state->wrap_t, linear_filter) &&
4399 !wrap_mode_uses_border_color(state->wrap_r, linear_filter))
4400 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK);
4401
4402 #define simple_border_types(elt) \
4403 do { \
4404 if (color->elt[0] == 0 && color->elt[1] == 0 && color->elt[2] == 0 && color->elt[3] == 0) \
4405 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK); \
4406 if (color->elt[0] == 0 && color->elt[1] == 0 && color->elt[2] == 0 && color->elt[3] == 1) \
4407 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK); \
4408 if (color->elt[0] == 1 && color->elt[1] == 1 && color->elt[2] == 1 && color->elt[3] == 1) \
4409 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE); \
4410 } while (false)
4411
4412 if (is_integer)
4413 simple_border_types(ui);
4414 else
4415 simple_border_types(f);
4416
4417 #undef simple_border_types
4418
4419 int i;
4420
4421 /* Check if the border has been uploaded already. */
4422 for (i = 0; i < sctx->border_color_count; i++)
4423 if (memcmp(&sctx->border_color_table[i], color, sizeof(*color)) == 0)
4424 break;
4425
4426 if (i >= SI_MAX_BORDER_COLORS) {
4427 /* Getting 4096 unique border colors is very unlikely. */
4428 fprintf(stderr, "radeonsi: The border color table is full. "
4429 "Any new border colors will be just black. "
4430 "Please file a bug.\n");
4431 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK);
4432 }
4433
4434 if (i == sctx->border_color_count) {
4435 /* Upload a new border color. */
4436 memcpy(&sctx->border_color_table[i], color, sizeof(*color));
4437 util_memcpy_cpu_to_le32(&sctx->border_color_map[i], color, sizeof(*color));
4438 sctx->border_color_count++;
4439 }
4440
4441 return S_008F3C_BORDER_COLOR_PTR(i) |
4442 S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_REGISTER);
4443 }
4444
4445 static inline int S_FIXED(float value, unsigned frac_bits)
4446 {
4447 return value * (1 << frac_bits);
4448 }
4449
4450 static inline unsigned si_tex_filter(unsigned filter, unsigned max_aniso)
4451 {
4452 if (filter == PIPE_TEX_FILTER_LINEAR)
4453 return max_aniso > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR
4454 : V_008F38_SQ_TEX_XY_FILTER_BILINEAR;
4455 else
4456 return max_aniso > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT
4457 : V_008F38_SQ_TEX_XY_FILTER_POINT;
4458 }
4459
4460 static inline unsigned si_tex_aniso_filter(unsigned filter)
4461 {
4462 if (filter < 2)
4463 return 0;
4464 if (filter < 4)
4465 return 1;
4466 if (filter < 8)
4467 return 2;
4468 if (filter < 16)
4469 return 3;
4470 return 4;
4471 }
4472
4473 static void *si_create_sampler_state(struct pipe_context *ctx,
4474 const struct pipe_sampler_state *state)
4475 {
4476 struct si_context *sctx = (struct si_context *)ctx;
4477 struct si_screen *sscreen = sctx->screen;
4478 struct si_sampler_state *rstate = CALLOC_STRUCT(si_sampler_state);
4479 unsigned max_aniso = sscreen->force_aniso >= 0 ? sscreen->force_aniso : state->max_anisotropy;
4480 unsigned max_aniso_ratio = si_tex_aniso_filter(max_aniso);
4481 bool trunc_coord = state->min_img_filter == PIPE_TEX_FILTER_NEAREST &&
4482 state->mag_img_filter == PIPE_TEX_FILTER_NEAREST &&
4483 state->compare_mode == PIPE_TEX_COMPARE_NONE;
4484 union pipe_color_union clamped_border_color;
4485
4486 if (!rstate) {
4487 return NULL;
4488 }
4489
4490 #ifndef NDEBUG
4491 rstate->magic = SI_SAMPLER_STATE_MAGIC;
4492 #endif
4493 rstate->val[0] =
4494 (S_008F30_CLAMP_X(si_tex_wrap(state->wrap_s)) | S_008F30_CLAMP_Y(si_tex_wrap(state->wrap_t)) |
4495 S_008F30_CLAMP_Z(si_tex_wrap(state->wrap_r)) | S_008F30_MAX_ANISO_RATIO(max_aniso_ratio) |
4496 S_008F30_DEPTH_COMPARE_FUNC(si_tex_compare(state->compare_func)) |
4497 S_008F30_FORCE_UNNORMALIZED(!state->normalized_coords) |
4498 S_008F30_ANISO_THRESHOLD(max_aniso_ratio >> 1) | S_008F30_ANISO_BIAS(max_aniso_ratio) |
4499 S_008F30_DISABLE_CUBE_WRAP(!state->seamless_cube_map) |
4500 S_008F30_TRUNC_COORD(trunc_coord) |
4501 S_008F30_COMPAT_MODE(sctx->chip_class == GFX8 || sctx->chip_class == GFX9));
4502 rstate->val[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(state->min_lod, 0, 15), 8)) |
4503 S_008F34_MAX_LOD(S_FIXED(CLAMP(state->max_lod, 0, 15), 8)) |
4504 S_008F34_PERF_MIP(max_aniso_ratio ? max_aniso_ratio + 6 : 0));
4505 rstate->val[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(state->lod_bias, -16, 16), 8)) |
4506 S_008F38_XY_MAG_FILTER(si_tex_filter(state->mag_img_filter, max_aniso)) |
4507 S_008F38_XY_MIN_FILTER(si_tex_filter(state->min_img_filter, max_aniso)) |
4508 S_008F38_MIP_FILTER(si_tex_mipfilter(state->min_mip_filter)) |
4509 S_008F38_MIP_POINT_PRECLAMP(0));
4510 rstate->val[3] = si_translate_border_color(sctx, state, &state->border_color, false);
4511
4512 if (sscreen->info.chip_class >= GFX10) {
4513 rstate->val[2] |= S_008F38_ANISO_OVERRIDE_GFX10(1);
4514 } else {
4515 rstate->val[2] |= S_008F38_DISABLE_LSB_CEIL(sctx->chip_class <= GFX8) |
4516 S_008F38_FILTER_PREC_FIX(1) |
4517 S_008F38_ANISO_OVERRIDE_GFX6(sctx->chip_class >= GFX8);
4518 }
4519
4520 /* Create sampler resource for integer textures. */
4521 memcpy(rstate->integer_val, rstate->val, sizeof(rstate->val));
4522 rstate->integer_val[3] = si_translate_border_color(sctx, state, &state->border_color, true);
4523
4524 /* Create sampler resource for upgraded depth textures. */
4525 memcpy(rstate->upgraded_depth_val, rstate->val, sizeof(rstate->val));
4526
4527 for (unsigned i = 0; i < 4; ++i) {
4528 /* Use channel 0 on purpose, so that we can use OPAQUE_WHITE
4529 * when the border color is 1.0. */
4530 clamped_border_color.f[i] = CLAMP(state->border_color.f[0], 0, 1);
4531 }
4532
4533 if (memcmp(&state->border_color, &clamped_border_color, sizeof(clamped_border_color)) == 0) {
4534 if (sscreen->info.chip_class <= GFX9)
4535 rstate->upgraded_depth_val[3] |= S_008F3C_UPGRADED_DEPTH(1);
4536 } else {
4537 rstate->upgraded_depth_val[3] =
4538 si_translate_border_color(sctx, state, &clamped_border_color, false);
4539 }
4540
4541 return rstate;
4542 }
4543
4544 static void si_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
4545 {
4546 struct si_context *sctx = (struct si_context *)ctx;
4547
4548 if (sctx->sample_mask == (uint16_t)sample_mask)
4549 return;
4550
4551 sctx->sample_mask = sample_mask;
4552 si_mark_atom_dirty(sctx, &sctx->atoms.s.sample_mask);
4553 }
4554
4555 static void si_emit_sample_mask(struct si_context *sctx)
4556 {
4557 struct radeon_cmdbuf *cs = sctx->gfx_cs;
4558 unsigned mask = sctx->sample_mask;
4559
4560 /* Needed for line and polygon smoothing as well as for the Polaris
4561 * small primitive filter. We expect the gallium frontend to take care of
4562 * this for us.
4563 */
4564 assert(mask == 0xffff || sctx->framebuffer.nr_samples > 1 ||
4565 (mask & 1 && sctx->blitter->running));
4566
4567 radeon_set_context_reg_seq(cs, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0, 2);
4568 radeon_emit(cs, mask | (mask << 16));
4569 radeon_emit(cs, mask | (mask << 16));
4570 }
4571
4572 static void si_delete_sampler_state(struct pipe_context *ctx, void *state)
4573 {
4574 #ifndef NDEBUG
4575 struct si_sampler_state *s = state;
4576
4577 assert(s->magic == SI_SAMPLER_STATE_MAGIC);
4578 s->magic = 0;
4579 #endif
4580 free(state);
4581 }
4582
4583 /*
4584 * Vertex elements & buffers
4585 */
4586
4587 struct si_fast_udiv_info32 si_compute_fast_udiv_info32(uint32_t D, unsigned num_bits)
4588 {
4589 struct util_fast_udiv_info info = util_compute_fast_udiv_info(D, num_bits, 32);
4590
4591 struct si_fast_udiv_info32 result = {
4592 info.multiplier,
4593 info.pre_shift,
4594 info.post_shift,
4595 info.increment,
4596 };
4597 return result;
4598 }
4599
4600 static void *si_create_vertex_elements(struct pipe_context *ctx, unsigned count,
4601 const struct pipe_vertex_element *elements)
4602 {
4603 struct si_screen *sscreen = (struct si_screen *)ctx->screen;
4604 struct si_vertex_elements *v = CALLOC_STRUCT(si_vertex_elements);
4605 bool used[SI_NUM_VERTEX_BUFFERS] = {};
4606 struct si_fast_udiv_info32 divisor_factors[SI_MAX_ATTRIBS] = {};
4607 STATIC_ASSERT(sizeof(struct si_fast_udiv_info32) == 16);
4608 STATIC_ASSERT(sizeof(divisor_factors[0].multiplier) == 4);
4609 STATIC_ASSERT(sizeof(divisor_factors[0].pre_shift) == 4);
4610 STATIC_ASSERT(sizeof(divisor_factors[0].post_shift) == 4);
4611 STATIC_ASSERT(sizeof(divisor_factors[0].increment) == 4);
4612 int i;
4613
4614 assert(count <= SI_MAX_ATTRIBS);
4615 if (!v)
4616 return NULL;
4617
4618 v->count = count;
4619
4620 unsigned alloc_count =
4621 count > sscreen->num_vbos_in_user_sgprs ? count - sscreen->num_vbos_in_user_sgprs : 0;
4622 v->vb_desc_list_alloc_size = align(alloc_count * 16, SI_CPDMA_ALIGNMENT);
4623
4624 for (i = 0; i < count; ++i) {
4625 const struct util_format_description *desc;
4626 const struct util_format_channel_description *channel;
4627 int first_non_void;
4628 unsigned vbo_index = elements[i].vertex_buffer_index;
4629
4630 if (vbo_index >= SI_NUM_VERTEX_BUFFERS) {
4631 FREE(v);
4632 return NULL;
4633 }
4634
4635 unsigned instance_divisor = elements[i].instance_divisor;
4636 if (instance_divisor) {
4637 v->uses_instance_divisors = true;
4638
4639 if (instance_divisor == 1) {
4640 v->instance_divisor_is_one |= 1u << i;
4641 } else {
4642 v->instance_divisor_is_fetched |= 1u << i;
4643 divisor_factors[i] = si_compute_fast_udiv_info32(instance_divisor, 32);
4644 }
4645 }
4646
4647 if (!used[vbo_index]) {
4648 v->first_vb_use_mask |= 1 << i;
4649 used[vbo_index] = true;
4650 }
4651
4652 desc = util_format_description(elements[i].src_format);
4653 first_non_void = util_format_get_first_non_void_channel(elements[i].src_format);
4654 channel = first_non_void >= 0 ? &desc->channel[first_non_void] : NULL;
4655
4656 v->format_size[i] = desc->block.bits / 8;
4657 v->src_offset[i] = elements[i].src_offset;
4658 v->vertex_buffer_index[i] = vbo_index;
4659
4660 bool always_fix = false;
4661 union si_vs_fix_fetch fix_fetch;
4662 unsigned log_hw_load_size; /* the load element size as seen by the hardware */
4663
4664 fix_fetch.bits = 0;
4665 log_hw_load_size = MIN2(2, util_logbase2(desc->block.bits) - 3);
4666
4667 if (channel) {
4668 switch (channel->type) {
4669 case UTIL_FORMAT_TYPE_FLOAT:
4670 fix_fetch.u.format = AC_FETCH_FORMAT_FLOAT;
4671 break;
4672 case UTIL_FORMAT_TYPE_FIXED:
4673 fix_fetch.u.format = AC_FETCH_FORMAT_FIXED;
4674 break;
4675 case UTIL_FORMAT_TYPE_SIGNED: {
4676 if (channel->pure_integer)
4677 fix_fetch.u.format = AC_FETCH_FORMAT_SINT;
4678 else if (channel->normalized)
4679 fix_fetch.u.format = AC_FETCH_FORMAT_SNORM;
4680 else
4681 fix_fetch.u.format = AC_FETCH_FORMAT_SSCALED;
4682 break;
4683 }
4684 case UTIL_FORMAT_TYPE_UNSIGNED: {
4685 if (channel->pure_integer)
4686 fix_fetch.u.format = AC_FETCH_FORMAT_UINT;
4687 else if (channel->normalized)
4688 fix_fetch.u.format = AC_FETCH_FORMAT_UNORM;
4689 else
4690 fix_fetch.u.format = AC_FETCH_FORMAT_USCALED;
4691 break;
4692 }
4693 default:
4694 unreachable("bad format type");
4695 }
4696 } else {
4697 switch (elements[i].src_format) {
4698 case PIPE_FORMAT_R11G11B10_FLOAT:
4699 fix_fetch.u.format = AC_FETCH_FORMAT_FLOAT;
4700 break;
4701 default:
4702 unreachable("bad other format");
4703 }
4704 }
4705
4706 if (desc->channel[0].size == 10) {
4707 fix_fetch.u.log_size = 3; /* special encoding for 2_10_10_10 */
4708 log_hw_load_size = 2;
4709
4710 /* The hardware always treats the 2-bit alpha channel as
4711 * unsigned, so a shader workaround is needed. The affected
4712 * chips are GFX8 and older except Stoney (GFX8.1).
4713 */
4714 always_fix = sscreen->info.chip_class <= GFX8 && sscreen->info.family != CHIP_STONEY &&
4715 channel->type == UTIL_FORMAT_TYPE_SIGNED;
4716 } else if (elements[i].src_format == PIPE_FORMAT_R11G11B10_FLOAT) {
4717 fix_fetch.u.log_size = 3; /* special encoding */
4718 fix_fetch.u.format = AC_FETCH_FORMAT_FIXED;
4719 log_hw_load_size = 2;
4720 } else {
4721 fix_fetch.u.log_size = util_logbase2(channel->size) - 3;
4722 fix_fetch.u.num_channels_m1 = desc->nr_channels - 1;
4723
4724 /* Always fix up:
4725 * - doubles (multiple loads + truncate to float)
4726 * - 32-bit requiring a conversion
4727 */
4728 always_fix = (fix_fetch.u.log_size == 3) ||
4729 (fix_fetch.u.log_size == 2 && fix_fetch.u.format != AC_FETCH_FORMAT_FLOAT &&
4730 fix_fetch.u.format != AC_FETCH_FORMAT_UINT &&
4731 fix_fetch.u.format != AC_FETCH_FORMAT_SINT);
4732
4733 /* Also fixup 8_8_8 and 16_16_16. */
4734 if (desc->nr_channels == 3 && fix_fetch.u.log_size <= 1) {
4735 always_fix = true;
4736 log_hw_load_size = fix_fetch.u.log_size;
4737 }
4738 }
4739
4740 if (desc->swizzle[0] != PIPE_SWIZZLE_X) {
4741 assert(desc->swizzle[0] == PIPE_SWIZZLE_Z &&
4742 (desc->swizzle[2] == PIPE_SWIZZLE_X || desc->swizzle[2] == PIPE_SWIZZLE_0));
4743 fix_fetch.u.reverse = 1;
4744 }
4745
4746 /* Force the workaround for unaligned access here already if the
4747 * offset relative to the vertex buffer base is unaligned.
4748 *
4749 * There is a theoretical case in which this is too conservative:
4750 * if the vertex buffer's offset is also unaligned in just the
4751 * right way, we end up with an aligned address after all.
4752 * However, this case should be extremely rare in practice (it
4753 * won't happen in well-behaved applications), and taking it
4754 * into account would complicate the fast path (where everything
4755 * is nicely aligned).
4756 */
4757 bool check_alignment = log_hw_load_size >= 1 && (sscreen->info.chip_class == GFX6 ||
4758 sscreen->info.chip_class == GFX10);
4759 bool opencode = sscreen->options.vs_fetch_always_opencode;
4760
4761 if (check_alignment && (elements[i].src_offset & ((1 << log_hw_load_size) - 1)) != 0)
4762 opencode = true;
4763
4764 if (always_fix || check_alignment || opencode)
4765 v->fix_fetch[i] = fix_fetch.bits;
4766
4767 if (opencode)
4768 v->fix_fetch_opencode |= 1 << i;
4769 if (opencode || always_fix)
4770 v->fix_fetch_always |= 1 << i;
4771
4772 if (check_alignment && !opencode) {
4773 assert(log_hw_load_size == 1 || log_hw_load_size == 2);
4774
4775 v->fix_fetch_unaligned |= 1 << i;
4776 v->hw_load_is_dword |= (log_hw_load_size - 1) << i;
4777 v->vb_alignment_check_mask |= 1 << vbo_index;
4778 }
4779
4780 v->rsrc_word3[i] = S_008F0C_DST_SEL_X(si_map_swizzle(desc->swizzle[0])) |
4781 S_008F0C_DST_SEL_Y(si_map_swizzle(desc->swizzle[1])) |
4782 S_008F0C_DST_SEL_Z(si_map_swizzle(desc->swizzle[2])) |
4783 S_008F0C_DST_SEL_W(si_map_swizzle(desc->swizzle[3]));
4784
4785 if (sscreen->info.chip_class >= GFX10) {
4786 const struct gfx10_format *fmt = &gfx10_format_table[elements[i].src_format];
4787 assert(fmt->img_format != 0 && fmt->img_format < 128);
4788 v->rsrc_word3[i] |= S_008F0C_FORMAT(fmt->img_format) | S_008F0C_RESOURCE_LEVEL(1);
4789 } else {
4790 unsigned data_format, num_format;
4791 data_format = si_translate_buffer_dataformat(ctx->screen, desc, first_non_void);
4792 num_format = si_translate_buffer_numformat(ctx->screen, desc, first_non_void);
4793 v->rsrc_word3[i] |= S_008F0C_NUM_FORMAT(num_format) | S_008F0C_DATA_FORMAT(data_format);
4794 }
4795 }
4796
4797 if (v->instance_divisor_is_fetched) {
4798 unsigned num_divisors = util_last_bit(v->instance_divisor_is_fetched);
4799
4800 v->instance_divisor_factor_buffer = (struct si_resource *)pipe_buffer_create(
4801 &sscreen->b, 0, PIPE_USAGE_DEFAULT, num_divisors * sizeof(divisor_factors[0]));
4802 if (!v->instance_divisor_factor_buffer) {
4803 FREE(v);
4804 return NULL;
4805 }
4806 void *map =
4807 sscreen->ws->buffer_map(v->instance_divisor_factor_buffer->buf, NULL, PIPE_TRANSFER_WRITE);
4808 memcpy(map, divisor_factors, num_divisors * sizeof(divisor_factors[0]));
4809 }
4810 return v;
4811 }
4812
4813 static void si_bind_vertex_elements(struct pipe_context *ctx, void *state)
4814 {
4815 struct si_context *sctx = (struct si_context *)ctx;
4816 struct si_vertex_elements *old = sctx->vertex_elements;
4817 struct si_vertex_elements *v = (struct si_vertex_elements *)state;
4818
4819 sctx->vertex_elements = v;
4820 sctx->num_vertex_elements = v ? v->count : 0;
4821
4822 if (sctx->num_vertex_elements) {
4823 sctx->vertex_buffers_dirty = true;
4824 } else {
4825 sctx->vertex_buffer_pointer_dirty = false;
4826 sctx->vertex_buffer_user_sgprs_dirty = false;
4827 }
4828
4829 if (v && (!old || old->count != v->count ||
4830 old->uses_instance_divisors != v->uses_instance_divisors ||
4831 /* we don't check which divisors changed */
4832 v->uses_instance_divisors ||
4833 (old->vb_alignment_check_mask ^ v->vb_alignment_check_mask) &
4834 sctx->vertex_buffer_unaligned ||
4835 ((v->vb_alignment_check_mask & sctx->vertex_buffer_unaligned) &&
4836 memcmp(old->vertex_buffer_index, v->vertex_buffer_index,
4837 sizeof(v->vertex_buffer_index[0]) * v->count)) ||
4838 /* fix_fetch_{always,opencode,unaligned} and hw_load_is_dword are
4839 * functions of fix_fetch and the src_offset alignment.
4840 * If they change and fix_fetch doesn't, it must be due to different
4841 * src_offset alignment, which is reflected in fix_fetch_opencode. */
4842 old->fix_fetch_opencode != v->fix_fetch_opencode ||
4843 memcmp(old->fix_fetch, v->fix_fetch, sizeof(v->fix_fetch[0]) * v->count)))
4844 sctx->do_update_shaders = true;
4845
4846 if (v && v->instance_divisor_is_fetched) {
4847 struct pipe_constant_buffer cb;
4848
4849 cb.buffer = &v->instance_divisor_factor_buffer->b.b;
4850 cb.user_buffer = NULL;
4851 cb.buffer_offset = 0;
4852 cb.buffer_size = 0xffffffff;
4853 si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS, &cb);
4854 }
4855 }
4856
4857 static void si_delete_vertex_element(struct pipe_context *ctx, void *state)
4858 {
4859 struct si_context *sctx = (struct si_context *)ctx;
4860 struct si_vertex_elements *v = (struct si_vertex_elements *)state;
4861
4862 if (sctx->vertex_elements == state) {
4863 sctx->vertex_elements = NULL;
4864 sctx->num_vertex_elements = 0;
4865 }
4866 si_resource_reference(&v->instance_divisor_factor_buffer, NULL);
4867 FREE(state);
4868 }
4869
4870 static void si_set_vertex_buffers(struct pipe_context *ctx, unsigned start_slot, unsigned count,
4871 const struct pipe_vertex_buffer *buffers)
4872 {
4873 struct si_context *sctx = (struct si_context *)ctx;
4874 struct pipe_vertex_buffer *dst = sctx->vertex_buffer + start_slot;
4875 unsigned updated_mask = u_bit_consecutive(start_slot, count);
4876 uint32_t orig_unaligned = sctx->vertex_buffer_unaligned;
4877 uint32_t unaligned = 0;
4878 int i;
4879
4880 assert(start_slot + count <= ARRAY_SIZE(sctx->vertex_buffer));
4881
4882 if (buffers) {
4883 for (i = 0; i < count; i++) {
4884 const struct pipe_vertex_buffer *src = buffers + i;
4885 struct pipe_vertex_buffer *dsti = dst + i;
4886 struct pipe_resource *buf = src->buffer.resource;
4887 unsigned slot_bit = 1 << (start_slot + i);
4888
4889 pipe_resource_reference(&dsti->buffer.resource, buf);
4890 dsti->buffer_offset = src->buffer_offset;
4891 dsti->stride = src->stride;
4892
4893 if (dsti->buffer_offset & 3 || dsti->stride & 3)
4894 unaligned |= slot_bit;
4895
4896 si_context_add_resource_size(sctx, buf);
4897 if (buf)
4898 si_resource(buf)->bind_history |= PIPE_BIND_VERTEX_BUFFER;
4899 }
4900 } else {
4901 for (i = 0; i < count; i++) {
4902 pipe_resource_reference(&dst[i].buffer.resource, NULL);
4903 }
4904 unaligned &= ~updated_mask;
4905 }
4906 sctx->vertex_buffers_dirty = true;
4907 sctx->vertex_buffer_unaligned = (orig_unaligned & ~updated_mask) | unaligned;
4908
4909 /* Check whether alignment may have changed in a way that requires
4910 * shader changes. This check is conservative: a vertex buffer can only
4911 * trigger a shader change if the misalignment amount changes (e.g.
4912 * from byte-aligned to short-aligned), but we only keep track of
4913 * whether buffers are at least dword-aligned, since that should always
4914 * be the case in well-behaved applications anyway.
4915 */
4916 if (sctx->vertex_elements && (sctx->vertex_elements->vb_alignment_check_mask &
4917 (unaligned | orig_unaligned) & updated_mask))
4918 sctx->do_update_shaders = true;
4919 }
4920
4921 /*
4922 * Misc
4923 */
4924
4925 static void si_set_tess_state(struct pipe_context *ctx, const float default_outer_level[4],
4926 const float default_inner_level[2])
4927 {
4928 struct si_context *sctx = (struct si_context *)ctx;
4929 struct pipe_constant_buffer cb;
4930 float array[8];
4931
4932 memcpy(array, default_outer_level, sizeof(float) * 4);
4933 memcpy(array + 4, default_inner_level, sizeof(float) * 2);
4934
4935 cb.buffer = NULL;
4936 cb.user_buffer = NULL;
4937 cb.buffer_size = sizeof(array);
4938
4939 si_upload_const_buffer(sctx, (struct si_resource **)&cb.buffer, (void *)array, sizeof(array),
4940 &cb.buffer_offset);
4941
4942 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS, &cb);
4943 pipe_resource_reference(&cb.buffer, NULL);
4944 }
4945
4946 static void si_texture_barrier(struct pipe_context *ctx, unsigned flags)
4947 {
4948 struct si_context *sctx = (struct si_context *)ctx;
4949
4950 si_update_fb_dirtiness_after_rendering(sctx);
4951
4952 /* Multisample surfaces are flushed in si_decompress_textures. */
4953 if (sctx->framebuffer.uncompressed_cb_mask) {
4954 si_make_CB_shader_coherent(sctx, sctx->framebuffer.nr_samples,
4955 sctx->framebuffer.CB_has_shader_readable_metadata,
4956 sctx->framebuffer.all_DCC_pipe_aligned);
4957 }
4958 }
4959
4960 /* This only ensures coherency for shader image/buffer stores. */
4961 static void si_memory_barrier(struct pipe_context *ctx, unsigned flags)
4962 {
4963 struct si_context *sctx = (struct si_context *)ctx;
4964
4965 if (!(flags & ~PIPE_BARRIER_UPDATE))
4966 return;
4967
4968 /* Subsequent commands must wait for all shader invocations to
4969 * complete. */
4970 sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH | SI_CONTEXT_CS_PARTIAL_FLUSH;
4971
4972 if (flags & PIPE_BARRIER_CONSTANT_BUFFER)
4973 sctx->flags |= SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE;
4974
4975 if (flags & (PIPE_BARRIER_VERTEX_BUFFER | PIPE_BARRIER_SHADER_BUFFER | PIPE_BARRIER_TEXTURE |
4976 PIPE_BARRIER_IMAGE | PIPE_BARRIER_STREAMOUT_BUFFER | PIPE_BARRIER_GLOBAL_BUFFER)) {
4977 /* As far as I can tell, L1 contents are written back to L2
4978 * automatically at end of shader, but the contents of other
4979 * L1 caches might still be stale. */
4980 sctx->flags |= SI_CONTEXT_INV_VCACHE;
4981 }
4982
4983 if (flags & PIPE_BARRIER_INDEX_BUFFER) {
4984 /* Indices are read through TC L2 since GFX8.
4985 * L1 isn't used.
4986 */
4987 if (sctx->screen->info.chip_class <= GFX7)
4988 sctx->flags |= SI_CONTEXT_WB_L2;
4989 }
4990
4991 /* MSAA color, any depth and any stencil are flushed in
4992 * si_decompress_textures when needed.
4993 */
4994 if (flags & PIPE_BARRIER_FRAMEBUFFER && sctx->framebuffer.uncompressed_cb_mask) {
4995 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;
4996
4997 if (sctx->chip_class <= GFX8)
4998 sctx->flags |= SI_CONTEXT_WB_L2;
4999 }
5000
5001 /* Indirect buffers use TC L2 on GFX9, but not older hw. */
5002 if (sctx->screen->info.chip_class <= GFX8 && flags & PIPE_BARRIER_INDIRECT_BUFFER)
5003 sctx->flags |= SI_CONTEXT_WB_L2;
5004 }
5005
5006 static void *si_create_blend_custom(struct si_context *sctx, unsigned mode)
5007 {
5008 struct pipe_blend_state blend;
5009
5010 memset(&blend, 0, sizeof(blend));
5011 blend.independent_blend_enable = true;
5012 blend.rt[0].colormask = 0xf;
5013 return si_create_blend_state_mode(&sctx->b, &blend, mode);
5014 }
5015
5016 static void si_init_config(struct si_context *sctx);
5017
5018 void si_init_state_compute_functions(struct si_context *sctx)
5019 {
5020 sctx->b.create_sampler_state = si_create_sampler_state;
5021 sctx->b.delete_sampler_state = si_delete_sampler_state;
5022 sctx->b.create_sampler_view = si_create_sampler_view;
5023 sctx->b.sampler_view_destroy = si_sampler_view_destroy;
5024 sctx->b.memory_barrier = si_memory_barrier;
5025 }
5026
5027 void si_init_state_functions(struct si_context *sctx)
5028 {
5029 sctx->atoms.s.framebuffer.emit = si_emit_framebuffer_state;
5030 sctx->atoms.s.msaa_sample_locs.emit = si_emit_msaa_sample_locs;
5031 sctx->atoms.s.db_render_state.emit = si_emit_db_render_state;
5032 sctx->atoms.s.dpbb_state.emit = si_emit_dpbb_state;
5033 sctx->atoms.s.msaa_config.emit = si_emit_msaa_config;
5034 sctx->atoms.s.sample_mask.emit = si_emit_sample_mask;
5035 sctx->atoms.s.cb_render_state.emit = si_emit_cb_render_state;
5036 sctx->atoms.s.blend_color.emit = si_emit_blend_color;
5037 sctx->atoms.s.clip_regs.emit = si_emit_clip_regs;
5038 sctx->atoms.s.clip_state.emit = si_emit_clip_state;
5039 sctx->atoms.s.stencil_ref.emit = si_emit_stencil_ref;
5040
5041 sctx->b.create_blend_state = si_create_blend_state;
5042 sctx->b.bind_blend_state = si_bind_blend_state;
5043 sctx->b.delete_blend_state = si_delete_blend_state;
5044 sctx->b.set_blend_color = si_set_blend_color;
5045
5046 sctx->b.create_rasterizer_state = si_create_rs_state;
5047 sctx->b.bind_rasterizer_state = si_bind_rs_state;
5048 sctx->b.delete_rasterizer_state = si_delete_rs_state;
5049
5050 sctx->b.create_depth_stencil_alpha_state = si_create_dsa_state;
5051 sctx->b.bind_depth_stencil_alpha_state = si_bind_dsa_state;
5052 sctx->b.delete_depth_stencil_alpha_state = si_delete_dsa_state;
5053
5054 sctx->custom_dsa_flush = si_create_db_flush_dsa(sctx);
5055 sctx->custom_blend_resolve = si_create_blend_custom(sctx, V_028808_CB_RESOLVE);
5056 sctx->custom_blend_fmask_decompress = si_create_blend_custom(sctx, V_028808_CB_FMASK_DECOMPRESS);
5057 sctx->custom_blend_eliminate_fastclear =
5058 si_create_blend_custom(sctx, V_028808_CB_ELIMINATE_FAST_CLEAR);
5059 sctx->custom_blend_dcc_decompress = si_create_blend_custom(sctx, V_028808_CB_DCC_DECOMPRESS);
5060
5061 sctx->b.set_clip_state = si_set_clip_state;
5062 sctx->b.set_stencil_ref = si_set_stencil_ref;
5063
5064 sctx->b.set_framebuffer_state = si_set_framebuffer_state;
5065
5066 sctx->b.set_sample_mask = si_set_sample_mask;
5067
5068 sctx->b.create_vertex_elements_state = si_create_vertex_elements;
5069 sctx->b.bind_vertex_elements_state = si_bind_vertex_elements;
5070 sctx->b.delete_vertex_elements_state = si_delete_vertex_element;
5071 sctx->b.set_vertex_buffers = si_set_vertex_buffers;
5072
5073 sctx->b.texture_barrier = si_texture_barrier;
5074 sctx->b.set_min_samples = si_set_min_samples;
5075 sctx->b.set_tess_state = si_set_tess_state;
5076
5077 sctx->b.set_active_query_state = si_set_active_query_state;
5078
5079 si_init_config(sctx);
5080 }
5081
5082 void si_init_screen_state_functions(struct si_screen *sscreen)
5083 {
5084 sscreen->b.is_format_supported = si_is_format_supported;
5085
5086 if (sscreen->info.chip_class >= GFX10) {
5087 sscreen->make_texture_descriptor = gfx10_make_texture_descriptor;
5088 } else {
5089 sscreen->make_texture_descriptor = si_make_texture_descriptor;
5090 }
5091 }
5092
5093 static void si_set_grbm_gfx_index(struct si_context *sctx, struct si_pm4_state *pm4, unsigned value)
5094 {
5095 unsigned reg = sctx->chip_class >= GFX7 ? R_030800_GRBM_GFX_INDEX : R_00802C_GRBM_GFX_INDEX;
5096 si_pm4_set_reg(pm4, reg, value);
5097 }
5098
5099 static void si_set_grbm_gfx_index_se(struct si_context *sctx, struct si_pm4_state *pm4, unsigned se)
5100 {
5101 assert(se == ~0 || se < sctx->screen->info.max_se);
5102 si_set_grbm_gfx_index(sctx, pm4,
5103 (se == ~0 ? S_030800_SE_BROADCAST_WRITES(1) : S_030800_SE_INDEX(se)) |
5104 S_030800_SH_BROADCAST_WRITES(1) |
5105 S_030800_INSTANCE_BROADCAST_WRITES(1));
5106 }
5107
5108 static void si_write_harvested_raster_configs(struct si_context *sctx, struct si_pm4_state *pm4,
5109 unsigned raster_config, unsigned raster_config_1)
5110 {
5111 unsigned num_se = MAX2(sctx->screen->info.max_se, 1);
5112 unsigned raster_config_se[4];
5113 unsigned se;
5114
5115 ac_get_harvested_configs(&sctx->screen->info, raster_config, &raster_config_1, raster_config_se);
5116
5117 for (se = 0; se < num_se; se++) {
5118 si_set_grbm_gfx_index_se(sctx, pm4, se);
5119 si_pm4_set_reg(pm4, R_028350_PA_SC_RASTER_CONFIG, raster_config_se[se]);
5120 }
5121 si_set_grbm_gfx_index(sctx, pm4, ~0);
5122
5123 if (sctx->chip_class >= GFX7) {
5124 si_pm4_set_reg(pm4, R_028354_PA_SC_RASTER_CONFIG_1, raster_config_1);
5125 }
5126 }
5127
5128 static void si_set_raster_config(struct si_context *sctx, struct si_pm4_state *pm4)
5129 {
5130 struct si_screen *sscreen = sctx->screen;
5131 unsigned num_rb = MIN2(sscreen->info.num_render_backends, 16);
5132 unsigned rb_mask = sscreen->info.enabled_rb_mask;
5133 unsigned raster_config = sscreen->pa_sc_raster_config;
5134 unsigned raster_config_1 = sscreen->pa_sc_raster_config_1;
5135
5136 if (!rb_mask || util_bitcount(rb_mask) >= num_rb) {
5137 /* Always use the default config when all backends are enabled
5138 * (or when we failed to determine the enabled backends).
5139 */
5140 si_pm4_set_reg(pm4, R_028350_PA_SC_RASTER_CONFIG, raster_config);
5141 if (sctx->chip_class >= GFX7)
5142 si_pm4_set_reg(pm4, R_028354_PA_SC_RASTER_CONFIG_1, raster_config_1);
5143 } else {
5144 si_write_harvested_raster_configs(sctx, pm4, raster_config, raster_config_1);
5145 }
5146 }
5147
5148 static void si_init_config(struct si_context *sctx)
5149 {
5150 struct si_screen *sscreen = sctx->screen;
5151 uint64_t border_color_va = sctx->border_color_buffer->gpu_address;
5152 bool has_clear_state = sscreen->info.has_clear_state;
5153 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
5154
5155 if (!pm4)
5156 return;
5157
5158 si_pm4_cmd_begin(pm4, PKT3_CONTEXT_CONTROL);
5159 si_pm4_cmd_add(pm4, CC0_UPDATE_LOAD_ENABLES(1));
5160 si_pm4_cmd_add(pm4, CC1_UPDATE_SHADOW_ENABLES(1));
5161 si_pm4_cmd_end(pm4, false);
5162
5163 if (has_clear_state) {
5164 si_pm4_cmd_begin(pm4, PKT3_CLEAR_STATE);
5165 si_pm4_cmd_add(pm4, 0);
5166 si_pm4_cmd_end(pm4, false);
5167 }
5168
5169 if (sctx->chip_class <= GFX8)
5170 si_set_raster_config(sctx, pm4);
5171
5172 si_pm4_set_reg(pm4, R_028A18_VGT_HOS_MAX_TESS_LEVEL, fui(64));
5173 if (!has_clear_state)
5174 si_pm4_set_reg(pm4, R_028A1C_VGT_HOS_MIN_TESS_LEVEL, fui(0));
5175
5176 /* FIXME calculate these values somehow ??? */
5177 if (sctx->chip_class <= GFX8) {
5178 si_pm4_set_reg(pm4, R_028A54_VGT_GS_PER_ES, SI_GS_PER_ES);
5179 si_pm4_set_reg(pm4, R_028A58_VGT_ES_PER_GS, 0x40);
5180 }
5181
5182 if (!has_clear_state) {
5183 si_pm4_set_reg(pm4, R_028A5C_VGT_GS_PER_VS, 0x2);
5184 si_pm4_set_reg(pm4, R_028A8C_VGT_PRIMITIVEID_RESET, 0x0);
5185 si_pm4_set_reg(pm4, R_028B98_VGT_STRMOUT_BUFFER_CONFIG, 0x0);
5186 }
5187
5188 if (sscreen->info.chip_class <= GFX9)
5189 si_pm4_set_reg(pm4, R_028AA0_VGT_INSTANCE_STEP_RATE_0, 1);
5190 if (!has_clear_state)
5191 si_pm4_set_reg(pm4, R_028AB8_VGT_VTX_CNT_EN, 0x0);
5192 if (sctx->chip_class < GFX7)
5193 si_pm4_set_reg(pm4, R_008A14_PA_CL_ENHANCE,
5194 S_008A14_NUM_CLIP_SEQ(3) | S_008A14_CLIP_VTX_REORDER_ENA(1));
5195
5196 /* CLEAR_STATE doesn't restore these correctly. */
5197 si_pm4_set_reg(pm4, R_028240_PA_SC_GENERIC_SCISSOR_TL, S_028240_WINDOW_OFFSET_DISABLE(1));
5198 si_pm4_set_reg(pm4, R_028244_PA_SC_GENERIC_SCISSOR_BR,
5199 S_028244_BR_X(16384) | S_028244_BR_Y(16384));
5200
5201 /* CLEAR_STATE doesn't clear these correctly on certain generations.
5202 * I don't know why. Deduced by trial and error.
5203 */
5204 if (sctx->chip_class <= GFX7 || !has_clear_state) {
5205 si_pm4_set_reg(pm4, R_028B28_VGT_STRMOUT_DRAW_OPAQUE_OFFSET, 0);
5206 si_pm4_set_reg(pm4, R_028204_PA_SC_WINDOW_SCISSOR_TL, S_028204_WINDOW_OFFSET_DISABLE(1));
5207 si_pm4_set_reg(pm4, R_028030_PA_SC_SCREEN_SCISSOR_TL, 0);
5208 si_pm4_set_reg(pm4, R_028034_PA_SC_SCREEN_SCISSOR_BR,
5209 S_028034_BR_X(16384) | S_028034_BR_Y(16384));
5210 }
5211
5212 if (!has_clear_state) {
5213 si_pm4_set_reg(pm4, R_028230_PA_SC_EDGERULE,
5214 S_028230_ER_TRI(0xA) | S_028230_ER_POINT(0xA) | S_028230_ER_RECT(0xA) |
5215 /* Required by DX10_DIAMOND_TEST_ENA: */
5216 S_028230_ER_LINE_LR(0x1A) | S_028230_ER_LINE_RL(0x26) |
5217 S_028230_ER_LINE_TB(0xA) | S_028230_ER_LINE_BT(0xA));
5218 si_pm4_set_reg(pm4, R_028820_PA_CL_NANINF_CNTL, 0);
5219 si_pm4_set_reg(pm4, R_028AC0_DB_SRESULTS_COMPARE_STATE0, 0x0);
5220 si_pm4_set_reg(pm4, R_028AC4_DB_SRESULTS_COMPARE_STATE1, 0x0);
5221 si_pm4_set_reg(pm4, R_028AC8_DB_PRELOAD_CONTROL, 0x0);
5222 si_pm4_set_reg(pm4, R_02800C_DB_RENDER_OVERRIDE, 0);
5223 }
5224
5225 if (sctx->chip_class >= GFX10) {
5226 si_pm4_set_reg(pm4, R_028A98_VGT_DRAW_PAYLOAD_CNTL, 0);
5227 si_pm4_set_reg(pm4, R_030964_GE_MAX_VTX_INDX, ~0);
5228 si_pm4_set_reg(pm4, R_030924_GE_MIN_VTX_INDX, 0);
5229 si_pm4_set_reg(pm4, R_030928_GE_INDX_OFFSET, 0);
5230 si_pm4_set_reg(pm4, R_03097C_GE_STEREO_CNTL, 0);
5231 si_pm4_set_reg(pm4, R_030988_GE_USER_VGPR_EN, 0);
5232 } else if (sctx->chip_class == GFX9) {
5233 si_pm4_set_reg(pm4, R_030920_VGT_MAX_VTX_INDX, ~0);
5234 si_pm4_set_reg(pm4, R_030924_VGT_MIN_VTX_INDX, 0);
5235 si_pm4_set_reg(pm4, R_030928_VGT_INDX_OFFSET, 0);
5236 } else {
5237 /* These registers, when written, also overwrite the CLEAR_STATE
5238 * context, so we can't rely on CLEAR_STATE setting them.
5239 * It would be an issue if there was another UMD changing them.
5240 */
5241 si_pm4_set_reg(pm4, R_028400_VGT_MAX_VTX_INDX, ~0);
5242 si_pm4_set_reg(pm4, R_028404_VGT_MIN_VTX_INDX, 0);
5243 si_pm4_set_reg(pm4, R_028408_VGT_INDX_OFFSET, 0);
5244 }
5245
5246 if (sctx->chip_class >= GFX7) {
5247 if (sctx->chip_class >= GFX10) {
5248 /* Logical CUs 16 - 31 */
5249 si_pm4_set_reg(pm4, R_00B404_SPI_SHADER_PGM_RSRC4_HS, S_00B404_CU_EN(0xffff));
5250 si_pm4_set_reg(pm4, R_00B104_SPI_SHADER_PGM_RSRC4_VS, S_00B104_CU_EN(0xffff));
5251 si_pm4_set_reg(pm4, R_00B004_SPI_SHADER_PGM_RSRC4_PS, S_00B004_CU_EN(0xffff));
5252 }
5253
5254 if (sctx->chip_class >= GFX9) {
5255 si_pm4_set_reg(pm4, R_00B41C_SPI_SHADER_PGM_RSRC3_HS,
5256 S_00B41C_CU_EN(0xffff) | S_00B41C_WAVE_LIMIT(0x3F));
5257 } else {
5258 si_pm4_set_reg(pm4, R_00B51C_SPI_SHADER_PGM_RSRC3_LS,
5259 S_00B51C_CU_EN(0xffff) | S_00B51C_WAVE_LIMIT(0x3F));
5260 si_pm4_set_reg(pm4, R_00B41C_SPI_SHADER_PGM_RSRC3_HS, S_00B41C_WAVE_LIMIT(0x3F));
5261 si_pm4_set_reg(pm4, R_00B31C_SPI_SHADER_PGM_RSRC3_ES,
5262 S_00B31C_CU_EN(0xffff) | S_00B31C_WAVE_LIMIT(0x3F));
5263
5264 /* If this is 0, Bonaire can hang even if GS isn't being used.
5265 * Other chips are unaffected. These are suboptimal values,
5266 * but we don't use on-chip GS.
5267 */
5268 si_pm4_set_reg(pm4, R_028A44_VGT_GS_ONCHIP_CNTL,
5269 S_028A44_ES_VERTS_PER_SUBGRP(64) | S_028A44_GS_PRIMS_PER_SUBGRP(4));
5270 }
5271
5272 /* Compute LATE_ALLOC_VS.LIMIT. */
5273 unsigned num_cu_per_sh = sscreen->info.min_good_cu_per_sa;
5274 unsigned late_alloc_wave64 = 0; /* The limit is per SA. */
5275 unsigned cu_mask_vs = 0xffff;
5276 unsigned cu_mask_gs = 0xffff;
5277
5278 if (sctx->chip_class >= GFX10) {
5279 /* For Wave32, the hw will launch twice the number of late
5280 * alloc waves, so 1 == 2x wave32.
5281 */
5282 if (!sscreen->info.use_late_alloc) {
5283 late_alloc_wave64 = 0;
5284 } else if (num_cu_per_sh <= 6) {
5285 late_alloc_wave64 = num_cu_per_sh - 2;
5286 } else {
5287 late_alloc_wave64 = (num_cu_per_sh - 2) * 4;
5288
5289 /* CU2 & CU3 disabled because of the dual CU design */
5290 /* Late alloc is not used for NGG on Navi14 due to a hw bug. */
5291 cu_mask_vs = 0xfff3;
5292 cu_mask_gs = sscreen->use_ngg && sctx->family != CHIP_NAVI14 ? 0xfff3 : 0xffff;
5293 }
5294 } else {
5295 if (!sscreen->info.use_late_alloc) {
5296 late_alloc_wave64 = 0;
5297 } else if (num_cu_per_sh <= 4) {
5298 /* Too few available compute units per SA. Disallowing
5299 * VS to run on one CU could hurt us more than late VS
5300 * allocation would help.
5301 *
5302 * 2 is the highest safe number that allows us to keep
5303 * all CUs enabled.
5304 */
5305 late_alloc_wave64 = 2;
5306 } else {
5307 /* This is a good initial value, allowing 1 late_alloc
5308 * wave per SIMD on num_cu - 2.
5309 */
5310 late_alloc_wave64 = (num_cu_per_sh - 2) * 4;
5311 }
5312
5313 if (late_alloc_wave64 > 2)
5314 cu_mask_vs = 0xfffe; /* 1 CU disabled */
5315 }
5316
5317 /* VS can't execute on one CU if the limit is > 2. */
5318 si_pm4_set_reg(pm4, R_00B118_SPI_SHADER_PGM_RSRC3_VS,
5319 S_00B118_CU_EN(cu_mask_vs) | S_00B118_WAVE_LIMIT(0x3F));
5320 si_pm4_set_reg(pm4, R_00B11C_SPI_SHADER_LATE_ALLOC_VS, S_00B11C_LIMIT(late_alloc_wave64));
5321
5322 si_pm4_set_reg(pm4, R_00B21C_SPI_SHADER_PGM_RSRC3_GS,
5323 S_00B21C_CU_EN(cu_mask_gs) | S_00B21C_WAVE_LIMIT(0x3F));
5324
5325 si_pm4_set_reg(pm4, R_00B01C_SPI_SHADER_PGM_RSRC3_PS,
5326 S_00B01C_CU_EN(0xffff) | S_00B01C_WAVE_LIMIT(0x3F));
5327 }
5328
5329 if (sctx->chip_class >= GFX10) {
5330 /* Break up a pixel wave if it contains deallocs for more than
5331 * half the parameter cache.
5332 *
5333 * To avoid a deadlock where pixel waves aren't launched
5334 * because they're waiting for more pixels while the frontend
5335 * is stuck waiting for PC space, the maximum allowed value is
5336 * the size of the PC minus the largest possible allocation for
5337 * a single primitive shader subgroup.
5338 */
5339 si_pm4_set_reg(pm4, R_028C50_PA_SC_NGG_MODE_CNTL, S_028C50_MAX_DEALLOCS_IN_WAVE(512));
5340 si_pm4_set_reg(pm4, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 14);
5341
5342 if (!has_clear_state) {
5343 si_pm4_set_reg(pm4, R_02835C_PA_SC_TILE_STEERING_OVERRIDE,
5344 sscreen->info.pa_sc_tile_steering_override);
5345 }
5346
5347 /* Enable CMASK/FMASK/HTILE/DCC caching in L2 for small chips. */
5348 unsigned meta_write_policy, meta_read_policy;
5349 /* TODO: investigate whether LRU improves performance on other chips too */
5350 if (sscreen->info.num_render_backends <= 4) {
5351 meta_write_policy = V_02807C_CACHE_LRU_WR; /* cache writes */
5352 meta_read_policy = V_02807C_CACHE_LRU_RD; /* cache reads */
5353 } else {
5354 meta_write_policy = V_02807C_CACHE_STREAM_WR; /* write combine */
5355 meta_read_policy = V_02807C_CACHE_NOA_RD; /* don't cache reads */
5356 }
5357
5358 si_pm4_set_reg(pm4, R_02807C_DB_RMI_L2_CACHE_CONTROL,
5359 S_02807C_Z_WR_POLICY(V_02807C_CACHE_STREAM_WR) |
5360 S_02807C_S_WR_POLICY(V_02807C_CACHE_STREAM_WR) |
5361 S_02807C_HTILE_WR_POLICY(meta_write_policy) |
5362 S_02807C_ZPCPSD_WR_POLICY(V_02807C_CACHE_STREAM_WR) |
5363 S_02807C_Z_RD_POLICY(V_02807C_CACHE_NOA_RD) |
5364 S_02807C_S_RD_POLICY(V_02807C_CACHE_NOA_RD) |
5365 S_02807C_HTILE_RD_POLICY(meta_read_policy));
5366
5367 si_pm4_set_reg(
5368 pm4, R_028410_CB_RMI_GL2_CACHE_CONTROL,
5369 S_028410_CMASK_WR_POLICY(meta_write_policy) | S_028410_FMASK_WR_POLICY(meta_write_policy) |
5370 S_028410_DCC_WR_POLICY(meta_write_policy) |
5371 S_028410_COLOR_WR_POLICY(V_028410_CACHE_STREAM_WR) |
5372 S_028410_CMASK_RD_POLICY(meta_read_policy) |
5373 S_028410_FMASK_RD_POLICY(meta_read_policy) | S_028410_DCC_RD_POLICY(meta_read_policy) |
5374 S_028410_COLOR_RD_POLICY(V_028410_CACHE_NOA_RD));
5375 si_pm4_set_reg(pm4, R_028428_CB_COVERAGE_OUT_CONTROL, 0);
5376
5377 si_pm4_set_reg(pm4, R_00B0C0_SPI_SHADER_REQ_CTRL_PS,
5378 S_00B0C0_SOFT_GROUPING_EN(1) | S_00B0C0_NUMBER_OF_REQUESTS_PER_CU(4 - 1));
5379 si_pm4_set_reg(pm4, R_00B1C0_SPI_SHADER_REQ_CTRL_VS, 0);
5380 }
5381
5382 if (sctx->chip_class >= GFX9) {
5383 si_pm4_set_reg(pm4, R_028B50_VGT_TESS_DISTRIBUTION,
5384 S_028B50_ACCUM_ISOLINE(40) | S_028B50_ACCUM_TRI(30) | S_028B50_ACCUM_QUAD(24) |
5385 S_028B50_DONUT_SPLIT(24) | S_028B50_TRAP_SPLIT(6));
5386 } else if (sctx->chip_class >= GFX8) {
5387 unsigned vgt_tess_distribution;
5388
5389 vgt_tess_distribution = S_028B50_ACCUM_ISOLINE(32) | S_028B50_ACCUM_TRI(11) |
5390 S_028B50_ACCUM_QUAD(11) | S_028B50_DONUT_SPLIT(16);
5391
5392 /* Testing with Unigine Heaven extreme tesselation yielded best results
5393 * with TRAP_SPLIT = 3.
5394 */
5395 if (sctx->family == CHIP_FIJI || sctx->family >= CHIP_POLARIS10)
5396 vgt_tess_distribution |= S_028B50_TRAP_SPLIT(3);
5397
5398 si_pm4_set_reg(pm4, R_028B50_VGT_TESS_DISTRIBUTION, vgt_tess_distribution);
5399 } else if (!has_clear_state) {
5400 si_pm4_set_reg(pm4, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 14);
5401 si_pm4_set_reg(pm4, R_028C5C_VGT_OUT_DEALLOC_CNTL, 16);
5402 }
5403
5404 si_pm4_set_reg(pm4, R_028080_TA_BC_BASE_ADDR, border_color_va >> 8);
5405 if (sctx->chip_class >= GFX7) {
5406 si_pm4_set_reg(pm4, R_028084_TA_BC_BASE_ADDR_HI, S_028084_ADDRESS(border_color_va >> 40));
5407 }
5408 si_pm4_add_bo(pm4, sctx->border_color_buffer, RADEON_USAGE_READ, RADEON_PRIO_BORDER_COLORS);
5409
5410 if (sctx->chip_class >= GFX9) {
5411 si_pm4_set_reg(pm4, R_028C48_PA_SC_BINNER_CNTL_1,
5412 S_028C48_MAX_ALLOC_COUNT(sscreen->info.pbb_max_alloc_count - 1) |
5413 S_028C48_MAX_PRIM_PER_BATCH(1023));
5414 si_pm4_set_reg(pm4, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL,
5415 S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1));
5416 si_pm4_set_reg(pm4, R_030968_VGT_INSTANCE_BASE_ID, 0);
5417 }
5418
5419 sctx->init_config = pm4;
5420 }