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