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