2 * Copyright 2012 Advanced Micro Devices, Inc.
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:
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
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.
25 #include "si_build_pm4.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 #include "util/u_blend.h"
37 #include "gfx10_format_table.h"
39 static unsigned si_map_swizzle(unsigned swizzle
)
43 return V_008F0C_SQ_SEL_Y
;
45 return V_008F0C_SQ_SEL_Z
;
47 return V_008F0C_SQ_SEL_W
;
49 return V_008F0C_SQ_SEL_0
;
51 return V_008F0C_SQ_SEL_1
;
52 default: /* PIPE_SWIZZLE_X */
53 return V_008F0C_SQ_SEL_X
;
57 /* 12.4 fixed-point */
58 static unsigned si_pack_float_12p4(float x
)
60 return x
<= 0 ? 0 : x
>= 4096 ? 0xffff : x
* 16;
64 * Inferred framebuffer and blender state.
66 * CB_TARGET_MASK is emitted here to avoid a hang with dual source blending
67 * if there is not enough PS outputs.
69 static void si_emit_cb_render_state(struct si_context
*sctx
)
71 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
72 struct si_state_blend
*blend
= sctx
->queued
.named
.blend
;
73 /* CB_COLORn_INFO.FORMAT=INVALID should disable unbound colorbuffers,
74 * but you never know. */
75 uint32_t cb_target_mask
= sctx
->framebuffer
.colorbuf_enabled_4bit
& blend
->cb_target_mask
;
78 /* Avoid a hang that happens when dual source blending is enabled
79 * but there is not enough color outputs. This is undefined behavior,
80 * so disable color writes completely.
82 * Reproducible with Unigine Heaven 4.0 and drirc missing.
84 if (blend
->dual_src_blend
&& sctx
->ps_shader
.cso
&&
85 (sctx
->ps_shader
.cso
->info
.colors_written
& 0x3) != 0x3)
88 /* GFX9: Flush DFSM when CB_TARGET_MASK changes.
89 * I think we don't have to do anything between IBs.
91 if (sctx
->screen
->dpbb_allowed
&& sctx
->last_cb_target_mask
!= cb_target_mask
) {
92 sctx
->last_cb_target_mask
= cb_target_mask
;
94 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
95 radeon_emit(cs
, EVENT_TYPE(V_028A90_BREAK_BATCH
) | EVENT_INDEX(0));
98 unsigned initial_cdw
= cs
->current
.cdw
;
99 radeon_opt_set_context_reg(sctx
, R_028238_CB_TARGET_MASK
, SI_TRACKED_CB_TARGET_MASK
,
102 if (sctx
->chip_class
>= GFX8
) {
103 /* DCC MSAA workaround.
104 * Alternatively, we can set CB_COLORi_DCC_CONTROL.OVERWRITE_-
105 * COMBINER_DISABLE, but that would be more complicated.
108 blend
->dcc_msaa_corruption_4bit
& cb_target_mask
&& sctx
->framebuffer
.nr_samples
>= 2;
109 unsigned watermark
= sctx
->framebuffer
.dcc_overwrite_combiner_watermark
;
111 radeon_opt_set_context_reg(
112 sctx
, R_028424_CB_DCC_CONTROL
, SI_TRACKED_CB_DCC_CONTROL
,
113 S_028424_OVERWRITE_COMBINER_MRT_SHARING_DISABLE(sctx
->chip_class
<= GFX9
) |
114 S_028424_OVERWRITE_COMBINER_WATERMARK(watermark
) |
115 S_028424_OVERWRITE_COMBINER_DISABLE(oc_disable
) |
116 S_028424_DISABLE_CONSTANT_ENCODE_REG(sctx
->screen
->info
.has_dcc_constant_encode
));
119 /* RB+ register settings. */
120 if (sctx
->screen
->info
.rbplus_allowed
) {
121 unsigned spi_shader_col_format
=
122 sctx
->ps_shader
.cso
? sctx
->ps_shader
.current
->key
.part
.ps
.epilog
.spi_shader_col_format
124 unsigned sx_ps_downconvert
= 0;
125 unsigned sx_blend_opt_epsilon
= 0;
126 unsigned sx_blend_opt_control
= 0;
128 for (i
= 0; i
< sctx
->framebuffer
.state
.nr_cbufs
; i
++) {
129 struct si_surface
*surf
= (struct si_surface
*)sctx
->framebuffer
.state
.cbufs
[i
];
130 unsigned format
, swap
, spi_format
, colormask
;
131 bool has_alpha
, has_rgb
;
134 /* If the color buffer is not set, the driver sets 32_R
135 * as the SPI color format, because the hw doesn't allow
136 * holes between color outputs, so also set this to
139 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_32_R
<< (i
* 4);
143 format
= G_028C70_FORMAT(surf
->cb_color_info
);
144 swap
= G_028C70_COMP_SWAP(surf
->cb_color_info
);
145 spi_format
= (spi_shader_col_format
>> (i
* 4)) & 0xf;
146 colormask
= (cb_target_mask
>> (i
* 4)) & 0xf;
148 /* Set if RGB and A are present. */
149 has_alpha
= !G_028C74_FORCE_DST_ALPHA_1(surf
->cb_color_attrib
);
151 if (format
== V_028C70_COLOR_8
|| format
== V_028C70_COLOR_16
||
152 format
== V_028C70_COLOR_32
)
153 has_rgb
= !has_alpha
;
157 /* Check the colormask and export format. */
158 if (!(colormask
& (PIPE_MASK_RGBA
& ~PIPE_MASK_A
)))
160 if (!(colormask
& PIPE_MASK_A
))
163 if (spi_format
== V_028714_SPI_SHADER_ZERO
) {
168 /* Disable value checking for disabled channels. */
170 sx_blend_opt_control
|= S_02875C_MRT0_COLOR_OPT_DISABLE(1) << (i
* 4);
172 sx_blend_opt_control
|= S_02875C_MRT0_ALPHA_OPT_DISABLE(1) << (i
* 4);
174 /* Enable down-conversion for 32bpp and smaller formats. */
176 case V_028C70_COLOR_8
:
177 case V_028C70_COLOR_8_8
:
178 case V_028C70_COLOR_8_8_8_8
:
179 /* For 1 and 2-channel formats, use the superset thereof. */
180 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
||
181 spi_format
== V_028714_SPI_SHADER_UINT16_ABGR
||
182 spi_format
== V_028714_SPI_SHADER_SINT16_ABGR
) {
183 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_8_8_8_8
<< (i
* 4);
184 sx_blend_opt_epsilon
|= V_028758_8BIT_FORMAT
<< (i
* 4);
188 case V_028C70_COLOR_5_6_5
:
189 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
190 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_5_6_5
<< (i
* 4);
191 sx_blend_opt_epsilon
|= V_028758_6BIT_FORMAT
<< (i
* 4);
195 case V_028C70_COLOR_1_5_5_5
:
196 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
197 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_1_5_5_5
<< (i
* 4);
198 sx_blend_opt_epsilon
|= V_028758_5BIT_FORMAT
<< (i
* 4);
202 case V_028C70_COLOR_4_4_4_4
:
203 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
204 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_4_4_4_4
<< (i
* 4);
205 sx_blend_opt_epsilon
|= V_028758_4BIT_FORMAT
<< (i
* 4);
209 case V_028C70_COLOR_32
:
210 if (swap
== V_028C70_SWAP_STD
&& spi_format
== V_028714_SPI_SHADER_32_R
)
211 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_32_R
<< (i
* 4);
212 else if (swap
== V_028C70_SWAP_ALT_REV
&& spi_format
== V_028714_SPI_SHADER_32_AR
)
213 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_32_A
<< (i
* 4);
216 case V_028C70_COLOR_16
:
217 case V_028C70_COLOR_16_16
:
218 /* For 1-channel formats, use the superset thereof. */
219 if (spi_format
== V_028714_SPI_SHADER_UNORM16_ABGR
||
220 spi_format
== V_028714_SPI_SHADER_SNORM16_ABGR
||
221 spi_format
== V_028714_SPI_SHADER_UINT16_ABGR
||
222 spi_format
== V_028714_SPI_SHADER_SINT16_ABGR
) {
223 if (swap
== V_028C70_SWAP_STD
|| swap
== V_028C70_SWAP_STD_REV
)
224 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_16_16_GR
<< (i
* 4);
226 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_16_16_AR
<< (i
* 4);
230 case V_028C70_COLOR_10_11_11
:
231 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
)
232 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_10_11_11
<< (i
* 4);
235 case V_028C70_COLOR_2_10_10_10
:
236 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
237 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_2_10_10_10
<< (i
* 4);
238 sx_blend_opt_epsilon
|= V_028758_10BIT_FORMAT
<< (i
* 4);
242 case V_028C70_COLOR_5_9_9_9
:
243 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
)
244 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_9_9_9_E5
<< (i
* 4);
249 /* If there are no color outputs, the first color export is
250 * always enabled as 32_R, so also set this to enable RB+.
252 if (!sx_ps_downconvert
)
253 sx_ps_downconvert
= V_028754_SX_RT_EXPORT_32_R
;
255 /* SX_PS_DOWNCONVERT, SX_BLEND_OPT_EPSILON, SX_BLEND_OPT_CONTROL */
256 radeon_opt_set_context_reg3(sctx
, R_028754_SX_PS_DOWNCONVERT
, SI_TRACKED_SX_PS_DOWNCONVERT
,
257 sx_ps_downconvert
, sx_blend_opt_epsilon
, sx_blend_opt_control
);
259 if (initial_cdw
!= cs
->current
.cdw
)
260 sctx
->context_roll
= true;
267 static uint32_t si_translate_blend_function(int blend_func
)
269 switch (blend_func
) {
271 return V_028780_COMB_DST_PLUS_SRC
;
272 case PIPE_BLEND_SUBTRACT
:
273 return V_028780_COMB_SRC_MINUS_DST
;
274 case PIPE_BLEND_REVERSE_SUBTRACT
:
275 return V_028780_COMB_DST_MINUS_SRC
;
277 return V_028780_COMB_MIN_DST_SRC
;
279 return V_028780_COMB_MAX_DST_SRC
;
281 PRINT_ERR("Unknown blend function %d\n", blend_func
);
288 static uint32_t si_translate_blend_factor(int blend_fact
)
290 switch (blend_fact
) {
291 case PIPE_BLENDFACTOR_ONE
:
292 return V_028780_BLEND_ONE
;
293 case PIPE_BLENDFACTOR_SRC_COLOR
:
294 return V_028780_BLEND_SRC_COLOR
;
295 case PIPE_BLENDFACTOR_SRC_ALPHA
:
296 return V_028780_BLEND_SRC_ALPHA
;
297 case PIPE_BLENDFACTOR_DST_ALPHA
:
298 return V_028780_BLEND_DST_ALPHA
;
299 case PIPE_BLENDFACTOR_DST_COLOR
:
300 return V_028780_BLEND_DST_COLOR
;
301 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
302 return V_028780_BLEND_SRC_ALPHA_SATURATE
;
303 case PIPE_BLENDFACTOR_CONST_COLOR
:
304 return V_028780_BLEND_CONSTANT_COLOR
;
305 case PIPE_BLENDFACTOR_CONST_ALPHA
:
306 return V_028780_BLEND_CONSTANT_ALPHA
;
307 case PIPE_BLENDFACTOR_ZERO
:
308 return V_028780_BLEND_ZERO
;
309 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
310 return V_028780_BLEND_ONE_MINUS_SRC_COLOR
;
311 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
312 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA
;
313 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
314 return V_028780_BLEND_ONE_MINUS_DST_ALPHA
;
315 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
316 return V_028780_BLEND_ONE_MINUS_DST_COLOR
;
317 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
318 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR
;
319 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
320 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA
;
321 case PIPE_BLENDFACTOR_SRC1_COLOR
:
322 return V_028780_BLEND_SRC1_COLOR
;
323 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
324 return V_028780_BLEND_SRC1_ALPHA
;
325 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
326 return V_028780_BLEND_INV_SRC1_COLOR
;
327 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
328 return V_028780_BLEND_INV_SRC1_ALPHA
;
330 PRINT_ERR("Bad blend factor %d not supported!\n", blend_fact
);
337 static uint32_t si_translate_blend_opt_function(int blend_func
)
339 switch (blend_func
) {
341 return V_028760_OPT_COMB_ADD
;
342 case PIPE_BLEND_SUBTRACT
:
343 return V_028760_OPT_COMB_SUBTRACT
;
344 case PIPE_BLEND_REVERSE_SUBTRACT
:
345 return V_028760_OPT_COMB_REVSUBTRACT
;
347 return V_028760_OPT_COMB_MIN
;
349 return V_028760_OPT_COMB_MAX
;
351 return V_028760_OPT_COMB_BLEND_DISABLED
;
355 static uint32_t si_translate_blend_opt_factor(int blend_fact
, bool is_alpha
)
357 switch (blend_fact
) {
358 case PIPE_BLENDFACTOR_ZERO
:
359 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL
;
360 case PIPE_BLENDFACTOR_ONE
:
361 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
;
362 case PIPE_BLENDFACTOR_SRC_COLOR
:
363 return is_alpha
? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
364 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0
;
365 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
366 return is_alpha
? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
367 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1
;
368 case PIPE_BLENDFACTOR_SRC_ALPHA
:
369 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
;
370 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
371 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
;
372 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
373 return is_alpha
? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
374 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0
;
376 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE
;
380 static void si_blend_check_commutativity(struct si_screen
*sscreen
, struct si_state_blend
*blend
,
381 enum pipe_blend_func func
, enum pipe_blendfactor src
,
382 enum pipe_blendfactor dst
, unsigned chanmask
)
384 /* Src factor is allowed when it does not depend on Dst */
385 static const uint32_t src_allowed
=
386 (1u << PIPE_BLENDFACTOR_ONE
) | (1u << PIPE_BLENDFACTOR_SRC_COLOR
) |
387 (1u << PIPE_BLENDFACTOR_SRC_ALPHA
) | (1u << PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
) |
388 (1u << PIPE_BLENDFACTOR_CONST_COLOR
) | (1u << PIPE_BLENDFACTOR_CONST_ALPHA
) |
389 (1u << PIPE_BLENDFACTOR_SRC1_COLOR
) | (1u << PIPE_BLENDFACTOR_SRC1_ALPHA
) |
390 (1u << PIPE_BLENDFACTOR_ZERO
) | (1u << PIPE_BLENDFACTOR_INV_SRC_COLOR
) |
391 (1u << PIPE_BLENDFACTOR_INV_SRC_ALPHA
) | (1u << PIPE_BLENDFACTOR_INV_CONST_COLOR
) |
392 (1u << PIPE_BLENDFACTOR_INV_CONST_ALPHA
) | (1u << PIPE_BLENDFACTOR_INV_SRC1_COLOR
) |
393 (1u << PIPE_BLENDFACTOR_INV_SRC1_ALPHA
);
395 if (dst
== PIPE_BLENDFACTOR_ONE
&& (src_allowed
& (1u << src
))) {
396 /* Addition is commutative, but floating point addition isn't
397 * associative: subtle changes can be introduced via different
400 * Out-of-order is also non-deterministic, which means that
401 * this breaks OpenGL invariance requirements. So only enable
402 * out-of-order additive blending if explicitly allowed by a
405 if (func
== PIPE_BLEND_MAX
|| func
== PIPE_BLEND_MIN
||
406 (func
== PIPE_BLEND_ADD
&& sscreen
->commutative_blend_add
))
407 blend
->commutative_4bit
|= chanmask
;
412 * Get rid of DST in the blend factors by commuting the operands:
413 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
415 static void si_blend_remove_dst(unsigned *func
, unsigned *src_factor
, unsigned *dst_factor
,
416 unsigned expected_dst
, unsigned replacement_src
)
418 if (*src_factor
== expected_dst
&& *dst_factor
== PIPE_BLENDFACTOR_ZERO
) {
419 *src_factor
= PIPE_BLENDFACTOR_ZERO
;
420 *dst_factor
= replacement_src
;
422 /* Commuting the operands requires reversing subtractions. */
423 if (*func
== PIPE_BLEND_SUBTRACT
)
424 *func
= PIPE_BLEND_REVERSE_SUBTRACT
;
425 else if (*func
== PIPE_BLEND_REVERSE_SUBTRACT
)
426 *func
= PIPE_BLEND_SUBTRACT
;
430 static void *si_create_blend_state_mode(struct pipe_context
*ctx
,
431 const struct pipe_blend_state
*state
, unsigned mode
)
433 struct si_context
*sctx
= (struct si_context
*)ctx
;
434 struct si_state_blend
*blend
= CALLOC_STRUCT(si_state_blend
);
435 struct si_pm4_state
*pm4
= &blend
->pm4
;
436 uint32_t sx_mrt_blend_opt
[8] = {0};
437 uint32_t color_control
= 0;
438 bool logicop_enable
= state
->logicop_enable
&& state
->logicop_func
!= PIPE_LOGICOP_COPY
;
443 blend
->alpha_to_coverage
= state
->alpha_to_coverage
;
444 blend
->alpha_to_one
= state
->alpha_to_one
;
445 blend
->dual_src_blend
= util_blend_state_is_dual(state
, 0);
446 blend
->logicop_enable
= logicop_enable
;
448 unsigned num_shader_outputs
= state
->max_rt
+ 1; /* estimate */
449 if (blend
->dual_src_blend
)
450 num_shader_outputs
= MAX2(num_shader_outputs
, 2);
452 if (logicop_enable
) {
453 color_control
|= S_028808_ROP3(state
->logicop_func
| (state
->logicop_func
<< 4));
455 color_control
|= S_028808_ROP3(0xcc);
458 if (state
->alpha_to_coverage
&& state
->alpha_to_coverage_dither
) {
459 si_pm4_set_reg(pm4
, R_028B70_DB_ALPHA_TO_MASK
,
460 S_028B70_ALPHA_TO_MASK_ENABLE(state
->alpha_to_coverage
) |
461 S_028B70_ALPHA_TO_MASK_OFFSET0(3) | S_028B70_ALPHA_TO_MASK_OFFSET1(1) |
462 S_028B70_ALPHA_TO_MASK_OFFSET2(0) | S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
463 S_028B70_OFFSET_ROUND(1));
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(2) | S_028B70_ALPHA_TO_MASK_OFFSET1(2) |
468 S_028B70_ALPHA_TO_MASK_OFFSET2(2) | S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
469 S_028B70_OFFSET_ROUND(0));
472 if (state
->alpha_to_coverage
)
473 blend
->need_src_alpha_4bit
|= 0xf;
475 blend
->cb_target_mask
= 0;
476 blend
->cb_target_enabled_4bit
= 0;
478 for (int i
= 0; i
< num_shader_outputs
; i
++) {
479 /* state->rt entries > 0 only written if independent blending */
480 const int j
= state
->independent_blend_enable
? i
: 0;
482 unsigned eqRGB
= state
->rt
[j
].rgb_func
;
483 unsigned srcRGB
= state
->rt
[j
].rgb_src_factor
;
484 unsigned dstRGB
= state
->rt
[j
].rgb_dst_factor
;
485 unsigned eqA
= state
->rt
[j
].alpha_func
;
486 unsigned srcA
= state
->rt
[j
].alpha_src_factor
;
487 unsigned dstA
= state
->rt
[j
].alpha_dst_factor
;
489 unsigned srcRGB_opt
, dstRGB_opt
, srcA_opt
, dstA_opt
;
490 unsigned blend_cntl
= 0;
492 sx_mrt_blend_opt
[i
] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED
) |
493 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED
);
495 /* Only set dual source blending for MRT0 to avoid a hang. */
496 if (i
>= 1 && blend
->dual_src_blend
) {
497 /* Vulkan does this for dual source blending. */
499 blend_cntl
|= S_028780_ENABLE(1);
501 si_pm4_set_reg(pm4
, R_028780_CB_BLEND0_CONTROL
+ i
* 4, blend_cntl
);
505 /* Only addition and subtraction equations are supported with
506 * dual source blending.
508 if (blend
->dual_src_blend
&& (eqRGB
== PIPE_BLEND_MIN
|| eqRGB
== PIPE_BLEND_MAX
||
509 eqA
== PIPE_BLEND_MIN
|| eqA
== PIPE_BLEND_MAX
)) {
510 assert(!"Unsupported equation for dual source blending");
511 si_pm4_set_reg(pm4
, R_028780_CB_BLEND0_CONTROL
+ i
* 4, blend_cntl
);
515 /* cb_render_state will disable unused ones */
516 blend
->cb_target_mask
|= (unsigned)state
->rt
[j
].colormask
<< (4 * i
);
517 if (state
->rt
[j
].colormask
)
518 blend
->cb_target_enabled_4bit
|= 0xf << (4 * i
);
520 if (!state
->rt
[j
].colormask
|| !state
->rt
[j
].blend_enable
) {
521 si_pm4_set_reg(pm4
, R_028780_CB_BLEND0_CONTROL
+ i
* 4, blend_cntl
);
525 si_blend_check_commutativity(sctx
->screen
, blend
, eqRGB
, srcRGB
, dstRGB
, 0x7 << (4 * i
));
526 si_blend_check_commutativity(sctx
->screen
, blend
, eqA
, srcA
, dstA
, 0x8 << (4 * i
));
528 /* Blending optimizations for RB+.
529 * These transformations don't change the behavior.
531 * First, get rid of DST in the blend factors:
532 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
534 si_blend_remove_dst(&eqRGB
, &srcRGB
, &dstRGB
, PIPE_BLENDFACTOR_DST_COLOR
,
535 PIPE_BLENDFACTOR_SRC_COLOR
);
536 si_blend_remove_dst(&eqA
, &srcA
, &dstA
, PIPE_BLENDFACTOR_DST_COLOR
,
537 PIPE_BLENDFACTOR_SRC_COLOR
);
538 si_blend_remove_dst(&eqA
, &srcA
, &dstA
, PIPE_BLENDFACTOR_DST_ALPHA
,
539 PIPE_BLENDFACTOR_SRC_ALPHA
);
541 /* Look up the ideal settings from tables. */
542 srcRGB_opt
= si_translate_blend_opt_factor(srcRGB
, false);
543 dstRGB_opt
= si_translate_blend_opt_factor(dstRGB
, false);
544 srcA_opt
= si_translate_blend_opt_factor(srcA
, true);
545 dstA_opt
= si_translate_blend_opt_factor(dstA
, true);
547 /* Handle interdependencies. */
548 if (util_blend_factor_uses_dest(srcRGB
, false))
549 dstRGB_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE
;
550 if (util_blend_factor_uses_dest(srcA
, false))
551 dstA_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE
;
553 if (srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
&&
554 (dstRGB
== PIPE_BLENDFACTOR_ZERO
|| dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
555 dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
))
556 dstRGB_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0
;
558 /* Set the final value. */
559 sx_mrt_blend_opt
[i
] = S_028760_COLOR_SRC_OPT(srcRGB_opt
) |
560 S_028760_COLOR_DST_OPT(dstRGB_opt
) |
561 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB
)) |
562 S_028760_ALPHA_SRC_OPT(srcA_opt
) | S_028760_ALPHA_DST_OPT(dstA_opt
) |
563 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA
));
565 /* Set blend state. */
566 blend_cntl
|= S_028780_ENABLE(1);
567 blend_cntl
|= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB
));
568 blend_cntl
|= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB
));
569 blend_cntl
|= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB
));
571 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
572 blend_cntl
|= S_028780_SEPARATE_ALPHA_BLEND(1);
573 blend_cntl
|= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA
));
574 blend_cntl
|= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA
));
575 blend_cntl
|= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA
));
577 si_pm4_set_reg(pm4
, R_028780_CB_BLEND0_CONTROL
+ i
* 4, blend_cntl
);
579 blend
->blend_enable_4bit
|= 0xfu
<< (i
* 4);
581 if (sctx
->chip_class
>= GFX8
&& sctx
->chip_class
<= GFX10
)
582 blend
->dcc_msaa_corruption_4bit
|= 0xfu
<< (i
* 4);
584 /* This is only important for formats without alpha. */
585 if (srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
|| dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
586 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
587 dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
588 srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
|| dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
)
589 blend
->need_src_alpha_4bit
|= 0xfu
<< (i
* 4);
592 if (sctx
->chip_class
>= GFX8
&& sctx
->chip_class
<= GFX10
&& logicop_enable
)
593 blend
->dcc_msaa_corruption_4bit
|= blend
->cb_target_enabled_4bit
;
595 if (blend
->cb_target_mask
) {
596 color_control
|= S_028808_MODE(mode
);
598 color_control
|= S_028808_MODE(V_028808_CB_DISABLE
);
601 if (sctx
->screen
->info
.rbplus_allowed
) {
602 /* Disable RB+ blend optimizations for dual source blending.
605 if (blend
->dual_src_blend
) {
606 for (int i
= 0; i
< num_shader_outputs
; i
++) {
607 sx_mrt_blend_opt
[i
] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE
) |
608 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE
);
612 for (int i
= 0; i
< num_shader_outputs
; i
++)
613 si_pm4_set_reg(pm4
, R_028760_SX_MRT0_BLEND_OPT
+ i
* 4, sx_mrt_blend_opt
[i
]);
615 /* RB+ doesn't work with dual source blending, logic op, and RESOLVE. */
616 if (blend
->dual_src_blend
|| logicop_enable
|| mode
== V_028808_CB_RESOLVE
)
617 color_control
|= S_028808_DISABLE_DUAL_QUAD(1);
620 si_pm4_set_reg(pm4
, R_028808_CB_COLOR_CONTROL
, color_control
);
624 static void *si_create_blend_state(struct pipe_context
*ctx
, const struct pipe_blend_state
*state
)
626 return si_create_blend_state_mode(ctx
, state
, V_028808_CB_NORMAL
);
629 static void si_bind_blend_state(struct pipe_context
*ctx
, void *state
)
631 struct si_context
*sctx
= (struct si_context
*)ctx
;
632 struct si_state_blend
*old_blend
= sctx
->queued
.named
.blend
;
633 struct si_state_blend
*blend
= (struct si_state_blend
*)state
;
636 blend
= (struct si_state_blend
*)sctx
->noop_blend
;
638 si_pm4_bind_state(sctx
, blend
, blend
);
640 if (old_blend
->cb_target_mask
!= blend
->cb_target_mask
||
641 old_blend
->dual_src_blend
!= blend
->dual_src_blend
||
642 (old_blend
->dcc_msaa_corruption_4bit
!= blend
->dcc_msaa_corruption_4bit
&&
643 sctx
->framebuffer
.nr_samples
>= 2 && sctx
->screen
->dcc_msaa_allowed
))
644 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.cb_render_state
);
646 if (old_blend
->cb_target_mask
!= blend
->cb_target_mask
||
647 old_blend
->alpha_to_coverage
!= blend
->alpha_to_coverage
||
648 old_blend
->alpha_to_one
!= blend
->alpha_to_one
||
649 old_blend
->dual_src_blend
!= blend
->dual_src_blend
||
650 old_blend
->blend_enable_4bit
!= blend
->blend_enable_4bit
||
651 old_blend
->need_src_alpha_4bit
!= blend
->need_src_alpha_4bit
)
652 sctx
->do_update_shaders
= true;
654 if (sctx
->screen
->dpbb_allowed
&&
655 (old_blend
->alpha_to_coverage
!= blend
->alpha_to_coverage
||
656 old_blend
->blend_enable_4bit
!= blend
->blend_enable_4bit
||
657 old_blend
->cb_target_enabled_4bit
!= blend
->cb_target_enabled_4bit
))
658 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.dpbb_state
);
660 if (sctx
->screen
->has_out_of_order_rast
&&
661 ((old_blend
->blend_enable_4bit
!= blend
->blend_enable_4bit
||
662 old_blend
->cb_target_enabled_4bit
!= blend
->cb_target_enabled_4bit
||
663 old_blend
->commutative_4bit
!= blend
->commutative_4bit
||
664 old_blend
->logicop_enable
!= blend
->logicop_enable
)))
665 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_config
);
668 static void si_delete_blend_state(struct pipe_context
*ctx
, void *state
)
670 struct si_context
*sctx
= (struct si_context
*)ctx
;
672 if (sctx
->queued
.named
.blend
== state
)
673 si_bind_blend_state(ctx
, sctx
->noop_blend
);
675 si_pm4_delete_state(sctx
, blend
, (struct si_state_blend
*)state
);
678 static void si_set_blend_color(struct pipe_context
*ctx
, const struct pipe_blend_color
*state
)
680 struct si_context
*sctx
= (struct si_context
*)ctx
;
681 static const struct pipe_blend_color zeros
;
683 sctx
->blend_color
.state
= *state
;
684 sctx
->blend_color
.any_nonzeros
= memcmp(state
, &zeros
, sizeof(*state
)) != 0;
685 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.blend_color
);
688 static void si_emit_blend_color(struct si_context
*sctx
)
690 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
692 radeon_set_context_reg_seq(cs
, R_028414_CB_BLEND_RED
, 4);
693 radeon_emit_array(cs
, (uint32_t *)sctx
->blend_color
.state
.color
, 4);
700 static void si_set_clip_state(struct pipe_context
*ctx
, const struct pipe_clip_state
*state
)
702 struct si_context
*sctx
= (struct si_context
*)ctx
;
703 struct pipe_constant_buffer cb
;
704 static const struct pipe_clip_state zeros
;
706 if (memcmp(&sctx
->clip_state
.state
, state
, sizeof(*state
)) == 0)
709 sctx
->clip_state
.state
= *state
;
710 sctx
->clip_state
.any_nonzeros
= memcmp(state
, &zeros
, sizeof(*state
)) != 0;
711 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.clip_state
);
714 cb
.user_buffer
= state
->ucp
;
715 cb
.buffer_offset
= 0;
716 cb
.buffer_size
= 4 * 4 * 8;
717 si_set_rw_buffer(sctx
, SI_VS_CONST_CLIP_PLANES
, &cb
);
718 pipe_resource_reference(&cb
.buffer
, NULL
);
721 static void si_emit_clip_state(struct si_context
*sctx
)
723 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
725 radeon_set_context_reg_seq(cs
, R_0285BC_PA_CL_UCP_0_X
, 6 * 4);
726 radeon_emit_array(cs
, (uint32_t *)sctx
->clip_state
.state
.ucp
, 6 * 4);
729 static void si_emit_clip_regs(struct si_context
*sctx
)
731 struct si_shader
*vs
= si_get_vs_state(sctx
);
732 struct si_shader_selector
*vs_sel
= vs
->selector
;
733 struct si_shader_info
*info
= &vs_sel
->info
;
734 struct si_state_rasterizer
*rs
= sctx
->queued
.named
.rasterizer
;
735 bool window_space
= info
->stage
== MESA_SHADER_VERTEX
?
736 info
->base
.vs
.window_space_position
: 0;
737 unsigned clipdist_mask
= vs_sel
->clipdist_mask
;
738 unsigned ucp_mask
= clipdist_mask
? 0 : rs
->clip_plane_enable
& SIX_BITS
;
739 unsigned culldist_mask
= vs_sel
->culldist_mask
;
742 if (vs
->key
.opt
.clip_disable
) {
743 assert(!info
->base
.cull_distance_array_size
);
747 total_mask
= clipdist_mask
| culldist_mask
;
749 /* Clip distances on points have no effect, so need to be implemented
750 * as cull distances. This applies for the clipvertex case as well.
752 * Setting this for primitives other than points should have no adverse
755 clipdist_mask
&= rs
->clip_plane_enable
;
756 culldist_mask
|= clipdist_mask
;
758 unsigned initial_cdw
= sctx
->gfx_cs
->current
.cdw
;
759 unsigned pa_cl_cntl
= S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask
& 0x0F) != 0) |
760 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask
& 0xF0) != 0) |
761 S_02881C_BYPASS_VTX_RATE_COMBINER(sctx
->chip_class
>= GFX10_3
) |
762 S_02881C_BYPASS_PRIM_RATE_COMBINER(sctx
->chip_class
>= GFX10_3
) |
763 clipdist_mask
| (culldist_mask
<< 8);
765 if (sctx
->chip_class
>= GFX10
) {
766 radeon_opt_set_context_reg_rmw(sctx
, R_02881C_PA_CL_VS_OUT_CNTL
,
767 SI_TRACKED_PA_CL_VS_OUT_CNTL__CL
, pa_cl_cntl
,
768 ~SI_TRACKED_PA_CL_VS_OUT_CNTL__VS_MASK
);
770 radeon_opt_set_context_reg(sctx
, R_02881C_PA_CL_VS_OUT_CNTL
, SI_TRACKED_PA_CL_VS_OUT_CNTL__CL
,
771 vs_sel
->pa_cl_vs_out_cntl
| pa_cl_cntl
);
773 radeon_opt_set_context_reg(sctx
, R_028810_PA_CL_CLIP_CNTL
, SI_TRACKED_PA_CL_CLIP_CNTL
,
774 rs
->pa_cl_clip_cntl
| ucp_mask
| S_028810_CLIP_DISABLE(window_space
));
776 if (initial_cdw
!= sctx
->gfx_cs
->current
.cdw
)
777 sctx
->context_roll
= true;
781 * inferred state between framebuffer and rasterizer
783 static void si_update_poly_offset_state(struct si_context
*sctx
)
785 struct si_state_rasterizer
*rs
= sctx
->queued
.named
.rasterizer
;
787 if (!rs
->uses_poly_offset
|| !sctx
->framebuffer
.state
.zsbuf
) {
788 si_pm4_bind_state(sctx
, poly_offset
, NULL
);
792 /* Use the user format, not db_render_format, so that the polygon
793 * offset behaves as expected by applications.
795 switch (sctx
->framebuffer
.state
.zsbuf
->texture
->format
) {
796 case PIPE_FORMAT_Z16_UNORM
:
797 si_pm4_bind_state(sctx
, poly_offset
, &rs
->pm4_poly_offset
[0]);
799 default: /* 24-bit */
800 si_pm4_bind_state(sctx
, poly_offset
, &rs
->pm4_poly_offset
[1]);
802 case PIPE_FORMAT_Z32_FLOAT
:
803 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
804 si_pm4_bind_state(sctx
, poly_offset
, &rs
->pm4_poly_offset
[2]);
813 static uint32_t si_translate_fill(uint32_t func
)
816 case PIPE_POLYGON_MODE_FILL
:
817 return V_028814_X_DRAW_TRIANGLES
;
818 case PIPE_POLYGON_MODE_LINE
:
819 return V_028814_X_DRAW_LINES
;
820 case PIPE_POLYGON_MODE_POINT
:
821 return V_028814_X_DRAW_POINTS
;
824 return V_028814_X_DRAW_POINTS
;
828 static void *si_create_rs_state(struct pipe_context
*ctx
, const struct pipe_rasterizer_state
*state
)
830 struct si_screen
*sscreen
= ((struct si_context
*)ctx
)->screen
;
831 struct si_state_rasterizer
*rs
= CALLOC_STRUCT(si_state_rasterizer
);
832 struct si_pm4_state
*pm4
= &rs
->pm4
;
834 float psize_min
, psize_max
;
840 if (!state
->front_ccw
) {
841 rs
->cull_front
= !!(state
->cull_face
& PIPE_FACE_FRONT
);
842 rs
->cull_back
= !!(state
->cull_face
& PIPE_FACE_BACK
);
844 rs
->cull_back
= !!(state
->cull_face
& PIPE_FACE_FRONT
);
845 rs
->cull_front
= !!(state
->cull_face
& PIPE_FACE_BACK
);
847 rs
->depth_clamp_any
= !state
->depth_clip_near
|| !state
->depth_clip_far
;
848 rs
->provoking_vertex_first
= state
->flatshade_first
;
849 rs
->scissor_enable
= state
->scissor
;
850 rs
->clip_halfz
= state
->clip_halfz
;
851 rs
->two_side
= state
->light_twoside
;
852 rs
->multisample_enable
= state
->multisample
;
853 rs
->force_persample_interp
= state
->force_persample_interp
;
854 rs
->clip_plane_enable
= state
->clip_plane_enable
;
855 rs
->half_pixel_center
= state
->half_pixel_center
;
856 rs
->line_stipple_enable
= state
->line_stipple_enable
;
857 rs
->poly_stipple_enable
= state
->poly_stipple_enable
;
858 rs
->line_smooth
= state
->line_smooth
;
859 rs
->line_width
= state
->line_width
;
860 rs
->poly_smooth
= state
->poly_smooth
;
861 rs
->uses_poly_offset
= state
->offset_point
|| state
->offset_line
|| state
->offset_tri
;
862 rs
->clamp_fragment_color
= state
->clamp_fragment_color
;
863 rs
->clamp_vertex_color
= state
->clamp_vertex_color
;
864 rs
->flatshade
= state
->flatshade
;
865 rs
->flatshade_first
= state
->flatshade_first
;
866 rs
->sprite_coord_enable
= state
->sprite_coord_enable
;
867 rs
->rasterizer_discard
= state
->rasterizer_discard
;
868 rs
->polygon_mode_enabled
=
869 (state
->fill_front
!= PIPE_POLYGON_MODE_FILL
&& !(state
->cull_face
& PIPE_FACE_FRONT
)) ||
870 (state
->fill_back
!= PIPE_POLYGON_MODE_FILL
&& !(state
->cull_face
& PIPE_FACE_BACK
));
871 rs
->polygon_mode_is_lines
=
872 (state
->fill_front
== PIPE_POLYGON_MODE_LINE
&& !(state
->cull_face
& PIPE_FACE_FRONT
)) ||
873 (state
->fill_back
== PIPE_POLYGON_MODE_LINE
&& !(state
->cull_face
& PIPE_FACE_BACK
));
874 rs
->pa_sc_line_stipple
= state
->line_stipple_enable
875 ? S_028A0C_LINE_PATTERN(state
->line_stipple_pattern
) |
876 S_028A0C_REPEAT_COUNT(state
->line_stipple_factor
)
878 rs
->pa_cl_clip_cntl
= S_028810_DX_CLIP_SPACE_DEF(state
->clip_halfz
) |
879 S_028810_ZCLIP_NEAR_DISABLE(!state
->depth_clip_near
) |
880 S_028810_ZCLIP_FAR_DISABLE(!state
->depth_clip_far
) |
881 S_028810_DX_RASTERIZATION_KILL(state
->rasterizer_discard
) |
882 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
885 pm4
, R_0286D4_SPI_INTERP_CONTROL_0
,
886 S_0286D4_FLAT_SHADE_ENA(1) | S_0286D4_PNT_SPRITE_ENA(state
->point_quad_rasterization
) |
887 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S
) |
888 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T
) |
889 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0
) |
890 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1
) |
891 S_0286D4_PNT_SPRITE_TOP_1(state
->sprite_coord_mode
!= PIPE_SPRITE_COORD_UPPER_LEFT
));
893 /* point size 12.4 fixed point */
894 tmp
= (unsigned)(state
->point_size
* 8.0);
895 si_pm4_set_reg(pm4
, R_028A00_PA_SU_POINT_SIZE
, S_028A00_HEIGHT(tmp
) | S_028A00_WIDTH(tmp
));
897 if (state
->point_size_per_vertex
) {
898 psize_min
= util_get_min_point_size(state
);
899 psize_max
= SI_MAX_POINT_SIZE
;
901 /* Force the point size to be as if the vertex output was disabled. */
902 psize_min
= state
->point_size
;
903 psize_max
= state
->point_size
;
905 rs
->max_point_size
= psize_max
;
907 /* Divide by two, because 0.5 = 1 pixel. */
908 si_pm4_set_reg(pm4
, R_028A04_PA_SU_POINT_MINMAX
,
909 S_028A04_MIN_SIZE(si_pack_float_12p4(psize_min
/ 2)) |
910 S_028A04_MAX_SIZE(si_pack_float_12p4(psize_max
/ 2)));
912 si_pm4_set_reg(pm4
, R_028A08_PA_SU_LINE_CNTL
,
913 S_028A08_WIDTH(si_pack_float_12p4(state
->line_width
/ 2)));
915 pm4
, R_028A48_PA_SC_MODE_CNTL_0
,
916 S_028A48_LINE_STIPPLE_ENABLE(state
->line_stipple_enable
) |
917 S_028A48_MSAA_ENABLE(state
->multisample
|| state
->poly_smooth
|| state
->line_smooth
) |
918 S_028A48_VPORT_SCISSOR_ENABLE(1) |
919 S_028A48_ALTERNATE_RBS_PER_TILE(sscreen
->info
.chip_class
>= GFX9
));
921 si_pm4_set_reg(pm4
, R_028B7C_PA_SU_POLY_OFFSET_CLAMP
, fui(state
->offset_clamp
));
922 si_pm4_set_reg(pm4
, R_028814_PA_SU_SC_MODE_CNTL
,
923 S_028814_PROVOKING_VTX_LAST(!state
->flatshade_first
) |
924 S_028814_CULL_FRONT((state
->cull_face
& PIPE_FACE_FRONT
) ? 1 : 0) |
925 S_028814_CULL_BACK((state
->cull_face
& PIPE_FACE_BACK
) ? 1 : 0) |
926 S_028814_FACE(!state
->front_ccw
) |
927 S_028814_POLY_OFFSET_FRONT_ENABLE(util_get_offset(state
, state
->fill_front
)) |
928 S_028814_POLY_OFFSET_BACK_ENABLE(util_get_offset(state
, state
->fill_back
)) |
929 S_028814_POLY_OFFSET_PARA_ENABLE(state
->offset_point
|| state
->offset_line
) |
930 S_028814_POLY_MODE(rs
->polygon_mode_enabled
) |
931 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(state
->fill_front
)) |
932 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(state
->fill_back
)));
934 if (!rs
->uses_poly_offset
)
937 rs
->pm4_poly_offset
= CALLOC(3, sizeof(struct si_pm4_state
));
938 if (!rs
->pm4_poly_offset
) {
943 /* Precalculate polygon offset states for 16-bit, 24-bit, and 32-bit zbuffers. */
944 for (i
= 0; i
< 3; i
++) {
945 struct si_pm4_state
*pm4
= &rs
->pm4_poly_offset
[i
];
946 float offset_units
= state
->offset_units
;
947 float offset_scale
= state
->offset_scale
* 16.0f
;
948 uint32_t pa_su_poly_offset_db_fmt_cntl
= 0;
950 if (!state
->offset_units_unscaled
) {
952 case 0: /* 16-bit zbuffer */
953 offset_units
*= 4.0f
;
954 pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
956 case 1: /* 24-bit zbuffer */
957 offset_units
*= 2.0f
;
958 pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
960 case 2: /* 32-bit zbuffer */
961 offset_units
*= 1.0f
;
962 pa_su_poly_offset_db_fmt_cntl
=
963 S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) | S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
968 si_pm4_set_reg(pm4
, R_028B80_PA_SU_POLY_OFFSET_FRONT_SCALE
, fui(offset_scale
));
969 si_pm4_set_reg(pm4
, R_028B84_PA_SU_POLY_OFFSET_FRONT_OFFSET
, fui(offset_units
));
970 si_pm4_set_reg(pm4
, R_028B88_PA_SU_POLY_OFFSET_BACK_SCALE
, fui(offset_scale
));
971 si_pm4_set_reg(pm4
, R_028B8C_PA_SU_POLY_OFFSET_BACK_OFFSET
, fui(offset_units
));
972 si_pm4_set_reg(pm4
, R_028B78_PA_SU_POLY_OFFSET_DB_FMT_CNTL
, pa_su_poly_offset_db_fmt_cntl
);
978 static void si_bind_rs_state(struct pipe_context
*ctx
, void *state
)
980 struct si_context
*sctx
= (struct si_context
*)ctx
;
981 struct si_state_rasterizer
*old_rs
= (struct si_state_rasterizer
*)sctx
->queued
.named
.rasterizer
;
982 struct si_state_rasterizer
*rs
= (struct si_state_rasterizer
*)state
;
985 rs
= (struct si_state_rasterizer
*)sctx
->discard_rasterizer_state
;
987 if (old_rs
->multisample_enable
!= rs
->multisample_enable
) {
988 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.db_render_state
);
990 /* Update the small primitive filter workaround if necessary. */
991 if (sctx
->screen
->info
.has_msaa_sample_loc_bug
&& sctx
->framebuffer
.nr_samples
> 1)
992 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_sample_locs
);
995 sctx
->current_vs_state
&= C_VS_STATE_CLAMP_VERTEX_COLOR
;
996 sctx
->current_vs_state
|= S_VS_STATE_CLAMP_VERTEX_COLOR(rs
->clamp_vertex_color
);
998 si_pm4_bind_state(sctx
, rasterizer
, rs
);
999 si_update_poly_offset_state(sctx
);
1001 if (old_rs
->scissor_enable
!= rs
->scissor_enable
)
1002 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.scissors
);
1004 if (old_rs
->line_width
!= rs
->line_width
|| old_rs
->max_point_size
!= rs
->max_point_size
||
1005 old_rs
->half_pixel_center
!= rs
->half_pixel_center
)
1006 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.guardband
);
1008 if (old_rs
->clip_halfz
!= rs
->clip_halfz
)
1009 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.viewports
);
1011 if (old_rs
->clip_plane_enable
!= rs
->clip_plane_enable
||
1012 old_rs
->pa_cl_clip_cntl
!= rs
->pa_cl_clip_cntl
)
1013 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.clip_regs
);
1015 if (old_rs
->clip_plane_enable
!= rs
->clip_plane_enable
||
1016 old_rs
->rasterizer_discard
!= rs
->rasterizer_discard
||
1017 old_rs
->sprite_coord_enable
!= rs
->sprite_coord_enable
||
1018 old_rs
->flatshade
!= rs
->flatshade
|| old_rs
->two_side
!= rs
->two_side
||
1019 old_rs
->multisample_enable
!= rs
->multisample_enable
||
1020 old_rs
->poly_stipple_enable
!= rs
->poly_stipple_enable
||
1021 old_rs
->poly_smooth
!= rs
->poly_smooth
|| old_rs
->line_smooth
!= rs
->line_smooth
||
1022 old_rs
->clamp_fragment_color
!= rs
->clamp_fragment_color
||
1023 old_rs
->force_persample_interp
!= rs
->force_persample_interp
)
1024 sctx
->do_update_shaders
= true;
1027 static void si_delete_rs_state(struct pipe_context
*ctx
, void *state
)
1029 struct si_context
*sctx
= (struct si_context
*)ctx
;
1030 struct si_state_rasterizer
*rs
= (struct si_state_rasterizer
*)state
;
1032 if (sctx
->queued
.named
.rasterizer
== state
)
1033 si_bind_rs_state(ctx
, sctx
->discard_rasterizer_state
);
1035 FREE(rs
->pm4_poly_offset
);
1036 si_pm4_delete_state(sctx
, rasterizer
, rs
);
1040 * infeered state between dsa and stencil ref
1042 static void si_emit_stencil_ref(struct si_context
*sctx
)
1044 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
1045 struct pipe_stencil_ref
*ref
= &sctx
->stencil_ref
.state
;
1046 struct si_dsa_stencil_ref_part
*dsa
= &sctx
->stencil_ref
.dsa_part
;
1048 radeon_set_context_reg_seq(cs
, R_028430_DB_STENCILREFMASK
, 2);
1049 radeon_emit(cs
, S_028430_STENCILTESTVAL(ref
->ref_value
[0]) |
1050 S_028430_STENCILMASK(dsa
->valuemask
[0]) |
1051 S_028430_STENCILWRITEMASK(dsa
->writemask
[0]) | S_028430_STENCILOPVAL(1));
1052 radeon_emit(cs
, S_028434_STENCILTESTVAL_BF(ref
->ref_value
[1]) |
1053 S_028434_STENCILMASK_BF(dsa
->valuemask
[1]) |
1054 S_028434_STENCILWRITEMASK_BF(dsa
->writemask
[1]) |
1055 S_028434_STENCILOPVAL_BF(1));
1058 static void si_set_stencil_ref(struct pipe_context
*ctx
, const struct pipe_stencil_ref
*state
)
1060 struct si_context
*sctx
= (struct si_context
*)ctx
;
1062 if (memcmp(&sctx
->stencil_ref
.state
, state
, sizeof(*state
)) == 0)
1065 sctx
->stencil_ref
.state
= *state
;
1066 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.stencil_ref
);
1073 static uint32_t si_translate_stencil_op(int s_op
)
1076 case PIPE_STENCIL_OP_KEEP
:
1077 return V_02842C_STENCIL_KEEP
;
1078 case PIPE_STENCIL_OP_ZERO
:
1079 return V_02842C_STENCIL_ZERO
;
1080 case PIPE_STENCIL_OP_REPLACE
:
1081 return V_02842C_STENCIL_REPLACE_TEST
;
1082 case PIPE_STENCIL_OP_INCR
:
1083 return V_02842C_STENCIL_ADD_CLAMP
;
1084 case PIPE_STENCIL_OP_DECR
:
1085 return V_02842C_STENCIL_SUB_CLAMP
;
1086 case PIPE_STENCIL_OP_INCR_WRAP
:
1087 return V_02842C_STENCIL_ADD_WRAP
;
1088 case PIPE_STENCIL_OP_DECR_WRAP
:
1089 return V_02842C_STENCIL_SUB_WRAP
;
1090 case PIPE_STENCIL_OP_INVERT
:
1091 return V_02842C_STENCIL_INVERT
;
1093 PRINT_ERR("Unknown stencil op %d", s_op
);
1100 static bool si_dsa_writes_stencil(const struct pipe_stencil_state
*s
)
1102 return s
->enabled
&& s
->writemask
&&
1103 (s
->fail_op
!= PIPE_STENCIL_OP_KEEP
|| s
->zfail_op
!= PIPE_STENCIL_OP_KEEP
||
1104 s
->zpass_op
!= PIPE_STENCIL_OP_KEEP
);
1107 static bool si_order_invariant_stencil_op(enum pipe_stencil_op op
)
1109 /* REPLACE is normally order invariant, except when the stencil
1110 * reference value is written by the fragment shader. Tracking this
1111 * interaction does not seem worth the effort, so be conservative. */
1112 return op
!= PIPE_STENCIL_OP_INCR
&& op
!= PIPE_STENCIL_OP_DECR
&& op
!= PIPE_STENCIL_OP_REPLACE
;
1115 /* Compute whether, assuming Z writes are disabled, this stencil state is order
1116 * invariant in the sense that the set of passing fragments as well as the
1117 * final stencil buffer result does not depend on the order of fragments. */
1118 static bool si_order_invariant_stencil_state(const struct pipe_stencil_state
*state
)
1120 return !state
->enabled
|| !state
->writemask
||
1121 /* The following assumes that Z writes are disabled. */
1122 (state
->func
== PIPE_FUNC_ALWAYS
&& si_order_invariant_stencil_op(state
->zpass_op
) &&
1123 si_order_invariant_stencil_op(state
->zfail_op
)) ||
1124 (state
->func
== PIPE_FUNC_NEVER
&& si_order_invariant_stencil_op(state
->fail_op
));
1127 static void *si_create_dsa_state(struct pipe_context
*ctx
,
1128 const struct pipe_depth_stencil_alpha_state
*state
)
1130 struct si_context
*sctx
= (struct si_context
*)ctx
;
1131 struct si_state_dsa
*dsa
= CALLOC_STRUCT(si_state_dsa
);
1132 struct si_pm4_state
*pm4
= &dsa
->pm4
;
1133 unsigned db_depth_control
;
1134 uint32_t db_stencil_control
= 0;
1140 dsa
->stencil_ref
.valuemask
[0] = state
->stencil
[0].valuemask
;
1141 dsa
->stencil_ref
.valuemask
[1] = state
->stencil
[1].valuemask
;
1142 dsa
->stencil_ref
.writemask
[0] = state
->stencil
[0].writemask
;
1143 dsa
->stencil_ref
.writemask
[1] = state
->stencil
[1].writemask
;
1146 S_028800_Z_ENABLE(state
->depth
.enabled
) | S_028800_Z_WRITE_ENABLE(state
->depth
.writemask
) |
1147 S_028800_ZFUNC(state
->depth
.func
) | S_028800_DEPTH_BOUNDS_ENABLE(state
->depth
.bounds_test
);
1150 if (state
->stencil
[0].enabled
) {
1151 db_depth_control
|= S_028800_STENCIL_ENABLE(1);
1152 db_depth_control
|= S_028800_STENCILFUNC(state
->stencil
[0].func
);
1153 db_stencil_control
|=
1154 S_02842C_STENCILFAIL(si_translate_stencil_op(state
->stencil
[0].fail_op
));
1155 db_stencil_control
|=
1156 S_02842C_STENCILZPASS(si_translate_stencil_op(state
->stencil
[0].zpass_op
));
1157 db_stencil_control
|=
1158 S_02842C_STENCILZFAIL(si_translate_stencil_op(state
->stencil
[0].zfail_op
));
1160 if (state
->stencil
[1].enabled
) {
1161 db_depth_control
|= S_028800_BACKFACE_ENABLE(1);
1162 db_depth_control
|= S_028800_STENCILFUNC_BF(state
->stencil
[1].func
);
1163 db_stencil_control
|=
1164 S_02842C_STENCILFAIL_BF(si_translate_stencil_op(state
->stencil
[1].fail_op
));
1165 db_stencil_control
|=
1166 S_02842C_STENCILZPASS_BF(si_translate_stencil_op(state
->stencil
[1].zpass_op
));
1167 db_stencil_control
|=
1168 S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(state
->stencil
[1].zfail_op
));
1173 if (state
->alpha
.enabled
) {
1174 dsa
->alpha_func
= state
->alpha
.func
;
1176 si_pm4_set_reg(pm4
, R_00B030_SPI_SHADER_USER_DATA_PS_0
+ SI_SGPR_ALPHA_REF
* 4,
1177 fui(state
->alpha
.ref_value
));
1179 dsa
->alpha_func
= PIPE_FUNC_ALWAYS
;
1182 si_pm4_set_reg(pm4
, R_028800_DB_DEPTH_CONTROL
, db_depth_control
);
1183 if (state
->stencil
[0].enabled
)
1184 si_pm4_set_reg(pm4
, R_02842C_DB_STENCIL_CONTROL
, db_stencil_control
);
1185 if (state
->depth
.bounds_test
) {
1186 si_pm4_set_reg(pm4
, R_028020_DB_DEPTH_BOUNDS_MIN
, fui(state
->depth
.bounds_min
));
1187 si_pm4_set_reg(pm4
, R_028024_DB_DEPTH_BOUNDS_MAX
, fui(state
->depth
.bounds_max
));
1190 dsa
->depth_enabled
= state
->depth
.enabled
;
1191 dsa
->depth_write_enabled
= state
->depth
.enabled
&& state
->depth
.writemask
;
1192 dsa
->stencil_enabled
= state
->stencil
[0].enabled
;
1193 dsa
->stencil_write_enabled
=
1194 state
->stencil
[0].enabled
&&
1195 (si_dsa_writes_stencil(&state
->stencil
[0]) || si_dsa_writes_stencil(&state
->stencil
[1]));
1196 dsa
->db_can_write
= dsa
->depth_write_enabled
|| dsa
->stencil_write_enabled
;
1198 bool zfunc_is_ordered
=
1199 state
->depth
.func
== PIPE_FUNC_NEVER
|| state
->depth
.func
== PIPE_FUNC_LESS
||
1200 state
->depth
.func
== PIPE_FUNC_LEQUAL
|| state
->depth
.func
== PIPE_FUNC_GREATER
||
1201 state
->depth
.func
== PIPE_FUNC_GEQUAL
;
1203 bool nozwrite_and_order_invariant_stencil
=
1204 !dsa
->db_can_write
||
1205 (!dsa
->depth_write_enabled
&& si_order_invariant_stencil_state(&state
->stencil
[0]) &&
1206 si_order_invariant_stencil_state(&state
->stencil
[1]));
1208 dsa
->order_invariance
[1].zs
=
1209 nozwrite_and_order_invariant_stencil
|| (!dsa
->stencil_write_enabled
&& zfunc_is_ordered
);
1210 dsa
->order_invariance
[0].zs
= !dsa
->depth_write_enabled
|| zfunc_is_ordered
;
1212 dsa
->order_invariance
[1].pass_set
=
1213 nozwrite_and_order_invariant_stencil
||
1214 (!dsa
->stencil_write_enabled
&&
1215 (state
->depth
.func
== PIPE_FUNC_ALWAYS
|| state
->depth
.func
== PIPE_FUNC_NEVER
));
1216 dsa
->order_invariance
[0].pass_set
=
1217 !dsa
->depth_write_enabled
||
1218 (state
->depth
.func
== PIPE_FUNC_ALWAYS
|| state
->depth
.func
== PIPE_FUNC_NEVER
);
1220 dsa
->order_invariance
[1].pass_last
= sctx
->screen
->assume_no_z_fights
&&
1221 !dsa
->stencil_write_enabled
&& dsa
->depth_write_enabled
&&
1223 dsa
->order_invariance
[0].pass_last
=
1224 sctx
->screen
->assume_no_z_fights
&& dsa
->depth_write_enabled
&& zfunc_is_ordered
;
1229 static void si_bind_dsa_state(struct pipe_context
*ctx
, void *state
)
1231 struct si_context
*sctx
= (struct si_context
*)ctx
;
1232 struct si_state_dsa
*old_dsa
= sctx
->queued
.named
.dsa
;
1233 struct si_state_dsa
*dsa
= state
;
1236 dsa
= (struct si_state_dsa
*)sctx
->noop_dsa
;
1238 si_pm4_bind_state(sctx
, dsa
, dsa
);
1240 if (memcmp(&dsa
->stencil_ref
, &sctx
->stencil_ref
.dsa_part
,
1241 sizeof(struct si_dsa_stencil_ref_part
)) != 0) {
1242 sctx
->stencil_ref
.dsa_part
= dsa
->stencil_ref
;
1243 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.stencil_ref
);
1246 if (old_dsa
->alpha_func
!= dsa
->alpha_func
)
1247 sctx
->do_update_shaders
= true;
1249 if (sctx
->screen
->dpbb_allowed
&& ((old_dsa
->depth_enabled
!= dsa
->depth_enabled
||
1250 old_dsa
->stencil_enabled
!= dsa
->stencil_enabled
||
1251 old_dsa
->db_can_write
!= dsa
->db_can_write
)))
1252 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.dpbb_state
);
1254 if (sctx
->screen
->has_out_of_order_rast
&&
1255 (memcmp(old_dsa
->order_invariance
, dsa
->order_invariance
,
1256 sizeof(old_dsa
->order_invariance
))))
1257 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_config
);
1260 static void si_delete_dsa_state(struct pipe_context
*ctx
, void *state
)
1262 struct si_context
*sctx
= (struct si_context
*)ctx
;
1264 if (sctx
->queued
.named
.dsa
== state
)
1265 si_bind_dsa_state(ctx
, sctx
->noop_dsa
);
1267 si_pm4_delete_state(sctx
, dsa
, (struct si_state_dsa
*)state
);
1270 static void *si_create_db_flush_dsa(struct si_context
*sctx
)
1272 struct pipe_depth_stencil_alpha_state dsa
= {};
1274 return sctx
->b
.create_depth_stencil_alpha_state(&sctx
->b
, &dsa
);
1277 /* DB RENDER STATE */
1279 static void si_set_active_query_state(struct pipe_context
*ctx
, bool enable
)
1281 struct si_context
*sctx
= (struct si_context
*)ctx
;
1283 /* Pipeline stat & streamout queries. */
1285 sctx
->flags
&= ~SI_CONTEXT_STOP_PIPELINE_STATS
;
1286 sctx
->flags
|= SI_CONTEXT_START_PIPELINE_STATS
;
1288 sctx
->flags
&= ~SI_CONTEXT_START_PIPELINE_STATS
;
1289 sctx
->flags
|= SI_CONTEXT_STOP_PIPELINE_STATS
;
1292 /* Occlusion queries. */
1293 if (sctx
->occlusion_queries_disabled
!= !enable
) {
1294 sctx
->occlusion_queries_disabled
= !enable
;
1295 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.db_render_state
);
1299 void si_set_occlusion_query_state(struct si_context
*sctx
, bool old_perfect_enable
)
1301 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.db_render_state
);
1303 bool perfect_enable
= sctx
->num_perfect_occlusion_queries
!= 0;
1305 if (perfect_enable
!= old_perfect_enable
)
1306 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_config
);
1309 void si_save_qbo_state(struct si_context
*sctx
, struct si_qbo_state
*st
)
1311 st
->saved_compute
= sctx
->cs_shader_state
.program
;
1313 si_get_pipe_constant_buffer(sctx
, PIPE_SHADER_COMPUTE
, 0, &st
->saved_const0
);
1314 si_get_shader_buffers(sctx
, PIPE_SHADER_COMPUTE
, 0, 3, st
->saved_ssbo
);
1316 st
->saved_ssbo_writable_mask
= 0;
1318 for (unsigned i
= 0; i
< 3; i
++) {
1319 if (sctx
->const_and_shader_buffers
[PIPE_SHADER_COMPUTE
].writable_mask
&
1320 (1u << si_get_shaderbuf_slot(i
)))
1321 st
->saved_ssbo_writable_mask
|= 1 << i
;
1325 void si_restore_qbo_state(struct si_context
*sctx
, struct si_qbo_state
*st
)
1327 sctx
->b
.bind_compute_state(&sctx
->b
, st
->saved_compute
);
1329 sctx
->b
.set_constant_buffer(&sctx
->b
, PIPE_SHADER_COMPUTE
, 0, &st
->saved_const0
);
1330 pipe_resource_reference(&st
->saved_const0
.buffer
, NULL
);
1332 sctx
->b
.set_shader_buffers(&sctx
->b
, PIPE_SHADER_COMPUTE
, 0, 3, st
->saved_ssbo
,
1333 st
->saved_ssbo_writable_mask
);
1334 for (unsigned i
= 0; i
< 3; ++i
)
1335 pipe_resource_reference(&st
->saved_ssbo
[i
].buffer
, NULL
);
1338 static void si_emit_db_render_state(struct si_context
*sctx
)
1340 struct si_state_rasterizer
*rs
= sctx
->queued
.named
.rasterizer
;
1341 unsigned db_shader_control
, db_render_control
, db_count_control
;
1342 unsigned initial_cdw
= sctx
->gfx_cs
->current
.cdw
;
1344 /* DB_RENDER_CONTROL */
1345 if (sctx
->dbcb_depth_copy_enabled
|| sctx
->dbcb_stencil_copy_enabled
) {
1346 db_render_control
= S_028000_DEPTH_COPY(sctx
->dbcb_depth_copy_enabled
) |
1347 S_028000_STENCIL_COPY(sctx
->dbcb_stencil_copy_enabled
) |
1348 S_028000_COPY_CENTROID(1) | S_028000_COPY_SAMPLE(sctx
->dbcb_copy_sample
);
1349 } else if (sctx
->db_flush_depth_inplace
|| sctx
->db_flush_stencil_inplace
) {
1350 db_render_control
= S_028000_DEPTH_COMPRESS_DISABLE(sctx
->db_flush_depth_inplace
) |
1351 S_028000_STENCIL_COMPRESS_DISABLE(sctx
->db_flush_stencil_inplace
);
1353 db_render_control
= S_028000_DEPTH_CLEAR_ENABLE(sctx
->db_depth_clear
) |
1354 S_028000_STENCIL_CLEAR_ENABLE(sctx
->db_stencil_clear
);
1357 /* DB_COUNT_CONTROL (occlusion queries) */
1358 if (sctx
->num_occlusion_queries
> 0 && !sctx
->occlusion_queries_disabled
) {
1359 bool perfect
= sctx
->num_perfect_occlusion_queries
> 0;
1360 bool gfx10_perfect
= sctx
->chip_class
>= GFX10
&& perfect
;
1362 if (sctx
->chip_class
>= GFX7
) {
1363 unsigned log_sample_rate
= sctx
->framebuffer
.log_samples
;
1365 db_count_control
= S_028004_PERFECT_ZPASS_COUNTS(perfect
) |
1366 S_028004_DISABLE_CONSERVATIVE_ZPASS_COUNTS(gfx10_perfect
) |
1367 S_028004_SAMPLE_RATE(log_sample_rate
) | S_028004_ZPASS_ENABLE(1) |
1368 S_028004_SLICE_EVEN_ENABLE(1) | S_028004_SLICE_ODD_ENABLE(1);
1370 db_count_control
= S_028004_PERFECT_ZPASS_COUNTS(perfect
) |
1371 S_028004_SAMPLE_RATE(sctx
->framebuffer
.log_samples
);
1374 /* Disable occlusion queries. */
1375 if (sctx
->chip_class
>= GFX7
) {
1376 db_count_control
= 0;
1378 db_count_control
= S_028004_ZPASS_INCREMENT_DISABLE(1);
1382 radeon_opt_set_context_reg2(sctx
, R_028000_DB_RENDER_CONTROL
, SI_TRACKED_DB_RENDER_CONTROL
,
1383 db_render_control
, db_count_control
);
1385 /* DB_RENDER_OVERRIDE2 */
1386 radeon_opt_set_context_reg(
1387 sctx
, R_028010_DB_RENDER_OVERRIDE2
, SI_TRACKED_DB_RENDER_OVERRIDE2
,
1388 S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(sctx
->db_depth_disable_expclear
) |
1389 S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(sctx
->db_stencil_disable_expclear
) |
1390 S_028010_DECOMPRESS_Z_ON_FLUSH(sctx
->framebuffer
.nr_samples
>= 4) |
1391 S_028010_CENTROID_COMPUTATION_MODE(sctx
->chip_class
>= GFX10_3
? 2 : 0));
1393 db_shader_control
= sctx
->ps_db_shader_control
;
1395 /* Bug workaround for smoothing (overrasterization) on GFX6. */
1396 if (sctx
->chip_class
== GFX6
&& sctx
->smoothing_enabled
) {
1397 db_shader_control
&= C_02880C_Z_ORDER
;
1398 db_shader_control
|= S_02880C_Z_ORDER(V_02880C_LATE_Z
);
1401 /* Disable the gl_SampleMask fragment shader output if MSAA is disabled. */
1402 if (!rs
->multisample_enable
)
1403 db_shader_control
&= C_02880C_MASK_EXPORT_ENABLE
;
1405 if (sctx
->screen
->info
.has_rbplus
&& !sctx
->screen
->info
.rbplus_allowed
)
1406 db_shader_control
|= S_02880C_DUAL_QUAD_DISABLE(1);
1408 radeon_opt_set_context_reg(sctx
, R_02880C_DB_SHADER_CONTROL
, SI_TRACKED_DB_SHADER_CONTROL
,
1411 if (initial_cdw
!= sctx
->gfx_cs
->current
.cdw
)
1412 sctx
->context_roll
= true;
1416 * format translation
1418 static uint32_t si_translate_colorformat(enum chip_class chip_class
,
1419 enum pipe_format format
)
1421 const struct util_format_description
*desc
= util_format_description(format
);
1423 return V_028C70_COLOR_INVALID
;
1425 #define HAS_SIZE(x, y, z, w) \
1426 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
1427 desc->channel[2].size == (z) && desc->channel[3].size == (w))
1429 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
1430 return V_028C70_COLOR_10_11_11
;
1432 if (chip_class
>= GFX10_3
&&
1433 format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) /* isn't plain */
1434 return V_028C70_COLOR_5_9_9_9
;
1436 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1437 return V_028C70_COLOR_INVALID
;
1439 /* hw cannot support mixed formats (except depth/stencil, since
1440 * stencil is not written to). */
1441 if (desc
->is_mixed
&& desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
)
1442 return V_028C70_COLOR_INVALID
;
1444 switch (desc
->nr_channels
) {
1446 switch (desc
->channel
[0].size
) {
1448 return V_028C70_COLOR_8
;
1450 return V_028C70_COLOR_16
;
1452 return V_028C70_COLOR_32
;
1456 if (desc
->channel
[0].size
== desc
->channel
[1].size
) {
1457 switch (desc
->channel
[0].size
) {
1459 return V_028C70_COLOR_8_8
;
1461 return V_028C70_COLOR_16_16
;
1463 return V_028C70_COLOR_32_32
;
1465 } else if (HAS_SIZE(8, 24, 0, 0)) {
1466 return V_028C70_COLOR_24_8
;
1467 } else if (HAS_SIZE(24, 8, 0, 0)) {
1468 return V_028C70_COLOR_8_24
;
1472 if (HAS_SIZE(5, 6, 5, 0)) {
1473 return V_028C70_COLOR_5_6_5
;
1474 } else if (HAS_SIZE(32, 8, 24, 0)) {
1475 return V_028C70_COLOR_X24_8_32_FLOAT
;
1479 if (desc
->channel
[0].size
== desc
->channel
[1].size
&&
1480 desc
->channel
[0].size
== desc
->channel
[2].size
&&
1481 desc
->channel
[0].size
== desc
->channel
[3].size
) {
1482 switch (desc
->channel
[0].size
) {
1484 return V_028C70_COLOR_4_4_4_4
;
1486 return V_028C70_COLOR_8_8_8_8
;
1488 return V_028C70_COLOR_16_16_16_16
;
1490 return V_028C70_COLOR_32_32_32_32
;
1492 } else if (HAS_SIZE(5, 5, 5, 1)) {
1493 return V_028C70_COLOR_1_5_5_5
;
1494 } else if (HAS_SIZE(1, 5, 5, 5)) {
1495 return V_028C70_COLOR_5_5_5_1
;
1496 } else if (HAS_SIZE(10, 10, 10, 2)) {
1497 return V_028C70_COLOR_2_10_10_10
;
1501 return V_028C70_COLOR_INVALID
;
1504 static uint32_t si_colorformat_endian_swap(uint32_t colorformat
)
1506 if (SI_BIG_ENDIAN
) {
1507 switch (colorformat
) {
1508 /* 8-bit buffers. */
1509 case V_028C70_COLOR_8
:
1510 return V_028C70_ENDIAN_NONE
;
1512 /* 16-bit buffers. */
1513 case V_028C70_COLOR_5_6_5
:
1514 case V_028C70_COLOR_1_5_5_5
:
1515 case V_028C70_COLOR_4_4_4_4
:
1516 case V_028C70_COLOR_16
:
1517 case V_028C70_COLOR_8_8
:
1518 return V_028C70_ENDIAN_8IN16
;
1520 /* 32-bit buffers. */
1521 case V_028C70_COLOR_8_8_8_8
:
1522 case V_028C70_COLOR_2_10_10_10
:
1523 case V_028C70_COLOR_8_24
:
1524 case V_028C70_COLOR_24_8
:
1525 case V_028C70_COLOR_16_16
:
1526 return V_028C70_ENDIAN_8IN32
;
1528 /* 64-bit buffers. */
1529 case V_028C70_COLOR_16_16_16_16
:
1530 return V_028C70_ENDIAN_8IN16
;
1532 case V_028C70_COLOR_32_32
:
1533 return V_028C70_ENDIAN_8IN32
;
1535 /* 128-bit buffers. */
1536 case V_028C70_COLOR_32_32_32_32
:
1537 return V_028C70_ENDIAN_8IN32
;
1539 return V_028C70_ENDIAN_NONE
; /* Unsupported. */
1542 return V_028C70_ENDIAN_NONE
;
1546 static uint32_t si_translate_dbformat(enum pipe_format format
)
1549 case PIPE_FORMAT_Z16_UNORM
:
1550 return V_028040_Z_16
;
1551 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1552 case PIPE_FORMAT_X8Z24_UNORM
:
1553 case PIPE_FORMAT_Z24X8_UNORM
:
1554 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1555 return V_028040_Z_24
; /* deprecated on AMD GCN */
1556 case PIPE_FORMAT_Z32_FLOAT
:
1557 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1558 return V_028040_Z_32_FLOAT
;
1560 return V_028040_Z_INVALID
;
1565 * Texture translation
1568 static uint32_t si_translate_texformat(struct pipe_screen
*screen
, enum pipe_format format
,
1569 const struct util_format_description
*desc
,
1572 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1573 bool uniform
= true;
1576 assert(sscreen
->info
.chip_class
<= GFX9
);
1578 /* Colorspace (return non-RGB formats directly). */
1579 switch (desc
->colorspace
) {
1580 /* Depth stencil formats */
1581 case UTIL_FORMAT_COLORSPACE_ZS
:
1583 case PIPE_FORMAT_Z16_UNORM
:
1584 return V_008F14_IMG_DATA_FORMAT_16
;
1585 case PIPE_FORMAT_X24S8_UINT
:
1586 case PIPE_FORMAT_S8X24_UINT
:
1588 * Implemented as an 8_8_8_8 data format to fix texture
1589 * gathers in stencil sampling. This affects at least
1590 * GL45-CTS.texture_cube_map_array.sampling on GFX8.
1592 if (sscreen
->info
.chip_class
<= GFX8
)
1593 return V_008F14_IMG_DATA_FORMAT_8_8_8_8
;
1595 if (format
== PIPE_FORMAT_X24S8_UINT
)
1596 return V_008F14_IMG_DATA_FORMAT_8_24
;
1598 return V_008F14_IMG_DATA_FORMAT_24_8
;
1599 case PIPE_FORMAT_Z24X8_UNORM
:
1600 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1601 return V_008F14_IMG_DATA_FORMAT_8_24
;
1602 case PIPE_FORMAT_X8Z24_UNORM
:
1603 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1604 return V_008F14_IMG_DATA_FORMAT_24_8
;
1605 case PIPE_FORMAT_S8_UINT
:
1606 return V_008F14_IMG_DATA_FORMAT_8
;
1607 case PIPE_FORMAT_Z32_FLOAT
:
1608 return V_008F14_IMG_DATA_FORMAT_32
;
1609 case PIPE_FORMAT_X32_S8X24_UINT
:
1610 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1611 return V_008F14_IMG_DATA_FORMAT_X24_8_32
;
1616 case UTIL_FORMAT_COLORSPACE_YUV
:
1617 goto out_unknown
; /* TODO */
1619 case UTIL_FORMAT_COLORSPACE_SRGB
:
1620 if (desc
->nr_channels
!= 4 && desc
->nr_channels
!= 1)
1628 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
1629 if (!sscreen
->info
.has_format_bc1_through_bc7
)
1633 case PIPE_FORMAT_RGTC1_SNORM
:
1634 case PIPE_FORMAT_LATC1_SNORM
:
1635 case PIPE_FORMAT_RGTC1_UNORM
:
1636 case PIPE_FORMAT_LATC1_UNORM
:
1637 return V_008F14_IMG_DATA_FORMAT_BC4
;
1638 case PIPE_FORMAT_RGTC2_SNORM
:
1639 case PIPE_FORMAT_LATC2_SNORM
:
1640 case PIPE_FORMAT_RGTC2_UNORM
:
1641 case PIPE_FORMAT_LATC2_UNORM
:
1642 return V_008F14_IMG_DATA_FORMAT_BC5
;
1648 if (desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
&&
1649 (sscreen
->info
.family
== CHIP_STONEY
|| sscreen
->info
.family
== CHIP_VEGA10
||
1650 sscreen
->info
.family
== CHIP_RAVEN
|| sscreen
->info
.family
== CHIP_RAVEN2
)) {
1652 case PIPE_FORMAT_ETC1_RGB8
:
1653 case PIPE_FORMAT_ETC2_RGB8
:
1654 case PIPE_FORMAT_ETC2_SRGB8
:
1655 return V_008F14_IMG_DATA_FORMAT_ETC2_RGB
;
1656 case PIPE_FORMAT_ETC2_RGB8A1
:
1657 case PIPE_FORMAT_ETC2_SRGB8A1
:
1658 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1
;
1659 case PIPE_FORMAT_ETC2_RGBA8
:
1660 case PIPE_FORMAT_ETC2_SRGBA8
:
1661 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA
;
1662 case PIPE_FORMAT_ETC2_R11_UNORM
:
1663 case PIPE_FORMAT_ETC2_R11_SNORM
:
1664 return V_008F14_IMG_DATA_FORMAT_ETC2_R
;
1665 case PIPE_FORMAT_ETC2_RG11_UNORM
:
1666 case PIPE_FORMAT_ETC2_RG11_SNORM
:
1667 return V_008F14_IMG_DATA_FORMAT_ETC2_RG
;
1673 if (desc
->layout
== UTIL_FORMAT_LAYOUT_BPTC
) {
1674 if (!sscreen
->info
.has_format_bc1_through_bc7
)
1678 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1679 case PIPE_FORMAT_BPTC_SRGBA
:
1680 return V_008F14_IMG_DATA_FORMAT_BC7
;
1681 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1682 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1683 return V_008F14_IMG_DATA_FORMAT_BC6
;
1689 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
1691 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
1692 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
1693 return V_008F14_IMG_DATA_FORMAT_GB_GR
;
1694 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
1695 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
1696 return V_008F14_IMG_DATA_FORMAT_BG_RG
;
1702 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
1703 if (!sscreen
->info
.has_format_bc1_through_bc7
)
1707 case PIPE_FORMAT_DXT1_RGB
:
1708 case PIPE_FORMAT_DXT1_RGBA
:
1709 case PIPE_FORMAT_DXT1_SRGB
:
1710 case PIPE_FORMAT_DXT1_SRGBA
:
1711 return V_008F14_IMG_DATA_FORMAT_BC1
;
1712 case PIPE_FORMAT_DXT3_RGBA
:
1713 case PIPE_FORMAT_DXT3_SRGBA
:
1714 return V_008F14_IMG_DATA_FORMAT_BC2
;
1715 case PIPE_FORMAT_DXT5_RGBA
:
1716 case PIPE_FORMAT_DXT5_SRGBA
:
1717 return V_008F14_IMG_DATA_FORMAT_BC3
;
1723 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
1724 return V_008F14_IMG_DATA_FORMAT_5_9_9_9
;
1725 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
1726 return V_008F14_IMG_DATA_FORMAT_10_11_11
;
1729 /* R8G8Bx_SNORM - TODO CxV8U8 */
1731 /* hw cannot support mixed formats (except depth/stencil, since only
1733 if (desc
->is_mixed
&& desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
)
1736 /* See whether the components are of the same size. */
1737 for (i
= 1; i
< desc
->nr_channels
; i
++) {
1738 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
1741 /* Non-uniform formats. */
1743 switch (desc
->nr_channels
) {
1745 if (desc
->channel
[0].size
== 5 && desc
->channel
[1].size
== 6 &&
1746 desc
->channel
[2].size
== 5) {
1747 return V_008F14_IMG_DATA_FORMAT_5_6_5
;
1751 if (desc
->channel
[0].size
== 5 && desc
->channel
[1].size
== 5 &&
1752 desc
->channel
[2].size
== 5 && desc
->channel
[3].size
== 1) {
1753 return V_008F14_IMG_DATA_FORMAT_1_5_5_5
;
1755 if (desc
->channel
[0].size
== 1 && desc
->channel
[1].size
== 5 &&
1756 desc
->channel
[2].size
== 5 && desc
->channel
[3].size
== 5) {
1757 return V_008F14_IMG_DATA_FORMAT_5_5_5_1
;
1759 if (desc
->channel
[0].size
== 10 && desc
->channel
[1].size
== 10 &&
1760 desc
->channel
[2].size
== 10 && desc
->channel
[3].size
== 2) {
1761 return V_008F14_IMG_DATA_FORMAT_2_10_10_10
;
1768 if (first_non_void
< 0 || first_non_void
> 3)
1771 /* uniform formats */
1772 switch (desc
->channel
[first_non_void
].size
) {
1774 switch (desc
->nr_channels
) {
1775 #if 0 /* Not supported for render targets */
1777 return V_008F14_IMG_DATA_FORMAT_4_4
;
1780 return V_008F14_IMG_DATA_FORMAT_4_4_4_4
;
1784 switch (desc
->nr_channels
) {
1786 return V_008F14_IMG_DATA_FORMAT_8
;
1788 return V_008F14_IMG_DATA_FORMAT_8_8
;
1790 return V_008F14_IMG_DATA_FORMAT_8_8_8_8
;
1794 switch (desc
->nr_channels
) {
1796 return V_008F14_IMG_DATA_FORMAT_16
;
1798 return V_008F14_IMG_DATA_FORMAT_16_16
;
1800 return V_008F14_IMG_DATA_FORMAT_16_16_16_16
;
1804 switch (desc
->nr_channels
) {
1806 return V_008F14_IMG_DATA_FORMAT_32
;
1808 return V_008F14_IMG_DATA_FORMAT_32_32
;
1809 #if 0 /* Not supported for render targets */
1811 return V_008F14_IMG_DATA_FORMAT_32_32_32
;
1814 return V_008F14_IMG_DATA_FORMAT_32_32_32_32
;
1822 static unsigned si_tex_wrap(unsigned wrap
)
1826 case PIPE_TEX_WRAP_REPEAT
:
1827 return V_008F30_SQ_TEX_WRAP
;
1828 case PIPE_TEX_WRAP_CLAMP
:
1829 return V_008F30_SQ_TEX_CLAMP_HALF_BORDER
;
1830 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1831 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL
;
1832 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1833 return V_008F30_SQ_TEX_CLAMP_BORDER
;
1834 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1835 return V_008F30_SQ_TEX_MIRROR
;
1836 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1837 return V_008F30_SQ_TEX_MIRROR_ONCE_HALF_BORDER
;
1838 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1839 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL
;
1840 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1841 return V_008F30_SQ_TEX_MIRROR_ONCE_BORDER
;
1845 static unsigned si_tex_mipfilter(unsigned filter
)
1848 case PIPE_TEX_MIPFILTER_NEAREST
:
1849 return V_008F38_SQ_TEX_Z_FILTER_POINT
;
1850 case PIPE_TEX_MIPFILTER_LINEAR
:
1851 return V_008F38_SQ_TEX_Z_FILTER_LINEAR
;
1853 case PIPE_TEX_MIPFILTER_NONE
:
1854 return V_008F38_SQ_TEX_Z_FILTER_NONE
;
1858 static unsigned si_tex_compare(unsigned compare
)
1862 case PIPE_FUNC_NEVER
:
1863 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER
;
1864 case PIPE_FUNC_LESS
:
1865 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS
;
1866 case PIPE_FUNC_EQUAL
:
1867 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL
;
1868 case PIPE_FUNC_LEQUAL
:
1869 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL
;
1870 case PIPE_FUNC_GREATER
:
1871 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER
;
1872 case PIPE_FUNC_NOTEQUAL
:
1873 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL
;
1874 case PIPE_FUNC_GEQUAL
:
1875 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL
;
1876 case PIPE_FUNC_ALWAYS
:
1877 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS
;
1881 static unsigned si_tex_dim(struct si_screen
*sscreen
, struct si_texture
*tex
, unsigned view_target
,
1882 unsigned nr_samples
)
1884 unsigned res_target
= tex
->buffer
.b
.b
.target
;
1886 if (view_target
== PIPE_TEXTURE_CUBE
|| view_target
== PIPE_TEXTURE_CUBE_ARRAY
)
1887 res_target
= view_target
;
1888 /* If interpreting cubemaps as something else, set 2D_ARRAY. */
1889 else if (res_target
== PIPE_TEXTURE_CUBE
|| res_target
== PIPE_TEXTURE_CUBE_ARRAY
)
1890 res_target
= PIPE_TEXTURE_2D_ARRAY
;
1892 /* GFX9 allocates 1D textures as 2D. */
1893 if ((res_target
== PIPE_TEXTURE_1D
|| res_target
== PIPE_TEXTURE_1D_ARRAY
) &&
1894 sscreen
->info
.chip_class
== GFX9
&&
1895 tex
->surface
.u
.gfx9
.resource_type
== RADEON_RESOURCE_2D
) {
1896 if (res_target
== PIPE_TEXTURE_1D
)
1897 res_target
= PIPE_TEXTURE_2D
;
1899 res_target
= PIPE_TEXTURE_2D_ARRAY
;
1902 switch (res_target
) {
1904 case PIPE_TEXTURE_1D
:
1905 return V_008F1C_SQ_RSRC_IMG_1D
;
1906 case PIPE_TEXTURE_1D_ARRAY
:
1907 return V_008F1C_SQ_RSRC_IMG_1D_ARRAY
;
1908 case PIPE_TEXTURE_2D
:
1909 case PIPE_TEXTURE_RECT
:
1910 return nr_samples
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA
: V_008F1C_SQ_RSRC_IMG_2D
;
1911 case PIPE_TEXTURE_2D_ARRAY
:
1912 return nr_samples
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
: V_008F1C_SQ_RSRC_IMG_2D_ARRAY
;
1913 case PIPE_TEXTURE_3D
:
1914 return V_008F1C_SQ_RSRC_IMG_3D
;
1915 case PIPE_TEXTURE_CUBE
:
1916 case PIPE_TEXTURE_CUBE_ARRAY
:
1917 return V_008F1C_SQ_RSRC_IMG_CUBE
;
1922 * Format support testing
1925 static bool si_is_sampler_format_supported(struct pipe_screen
*screen
, enum pipe_format format
)
1927 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1929 if (sscreen
->info
.chip_class
>= GFX10
) {
1930 const struct gfx10_format
*fmt
= &gfx10_format_table
[format
];
1931 if (!fmt
->img_format
|| fmt
->buffers_only
)
1936 const struct util_format_description
*desc
= util_format_description(format
);
1940 return si_translate_texformat(screen
, format
, desc
,
1941 util_format_get_first_non_void_channel(format
)) != ~0U;
1944 static uint32_t si_translate_buffer_dataformat(struct pipe_screen
*screen
,
1945 const struct util_format_description
*desc
,
1950 assert(((struct si_screen
*)screen
)->info
.chip_class
<= GFX9
);
1952 if (desc
->format
== PIPE_FORMAT_R11G11B10_FLOAT
)
1953 return V_008F0C_BUF_DATA_FORMAT_10_11_11
;
1955 assert(first_non_void
>= 0);
1957 if (desc
->nr_channels
== 4 && desc
->channel
[0].size
== 10 && desc
->channel
[1].size
== 10 &&
1958 desc
->channel
[2].size
== 10 && desc
->channel
[3].size
== 2)
1959 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10
;
1961 /* See whether the components are of the same size. */
1962 for (i
= 0; i
< desc
->nr_channels
; i
++) {
1963 if (desc
->channel
[first_non_void
].size
!= desc
->channel
[i
].size
)
1964 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
1967 switch (desc
->channel
[first_non_void
].size
) {
1969 switch (desc
->nr_channels
) {
1971 case 3: /* 3 loads */
1972 return V_008F0C_BUF_DATA_FORMAT_8
;
1974 return V_008F0C_BUF_DATA_FORMAT_8_8
;
1976 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8
;
1980 switch (desc
->nr_channels
) {
1982 case 3: /* 3 loads */
1983 return V_008F0C_BUF_DATA_FORMAT_16
;
1985 return V_008F0C_BUF_DATA_FORMAT_16_16
;
1987 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16
;
1991 switch (desc
->nr_channels
) {
1993 return V_008F0C_BUF_DATA_FORMAT_32
;
1995 return V_008F0C_BUF_DATA_FORMAT_32_32
;
1997 return V_008F0C_BUF_DATA_FORMAT_32_32_32
;
1999 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32
;
2003 /* Legacy double formats. */
2004 switch (desc
->nr_channels
) {
2005 case 1: /* 1 load */
2006 return V_008F0C_BUF_DATA_FORMAT_32_32
;
2007 case 2: /* 1 load */
2008 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32
;
2009 case 3: /* 3 loads */
2010 return V_008F0C_BUF_DATA_FORMAT_32_32
;
2011 case 4: /* 2 loads */
2012 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32
;
2017 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
2020 static uint32_t si_translate_buffer_numformat(struct pipe_screen
*screen
,
2021 const struct util_format_description
*desc
,
2024 assert(((struct si_screen
*)screen
)->info
.chip_class
<= GFX9
);
2026 if (desc
->format
== PIPE_FORMAT_R11G11B10_FLOAT
)
2027 return V_008F0C_BUF_NUM_FORMAT_FLOAT
;
2029 assert(first_non_void
>= 0);
2031 switch (desc
->channel
[first_non_void
].type
) {
2032 case UTIL_FORMAT_TYPE_SIGNED
:
2033 case UTIL_FORMAT_TYPE_FIXED
:
2034 if (desc
->channel
[first_non_void
].size
>= 32 || desc
->channel
[first_non_void
].pure_integer
)
2035 return V_008F0C_BUF_NUM_FORMAT_SINT
;
2036 else if (desc
->channel
[first_non_void
].normalized
)
2037 return V_008F0C_BUF_NUM_FORMAT_SNORM
;
2039 return V_008F0C_BUF_NUM_FORMAT_SSCALED
;
2041 case UTIL_FORMAT_TYPE_UNSIGNED
:
2042 if (desc
->channel
[first_non_void
].size
>= 32 || desc
->channel
[first_non_void
].pure_integer
)
2043 return V_008F0C_BUF_NUM_FORMAT_UINT
;
2044 else if (desc
->channel
[first_non_void
].normalized
)
2045 return V_008F0C_BUF_NUM_FORMAT_UNORM
;
2047 return V_008F0C_BUF_NUM_FORMAT_USCALED
;
2049 case UTIL_FORMAT_TYPE_FLOAT
:
2051 return V_008F0C_BUF_NUM_FORMAT_FLOAT
;
2055 static unsigned si_is_vertex_format_supported(struct pipe_screen
*screen
, enum pipe_format format
,
2058 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2059 const struct util_format_description
*desc
;
2061 unsigned data_format
;
2063 assert((usage
& ~(PIPE_BIND_SHADER_IMAGE
| PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_VERTEX_BUFFER
)) ==
2066 desc
= util_format_description(format
);
2070 /* There are no native 8_8_8 or 16_16_16 data formats, and we currently
2071 * select 8_8_8_8 and 16_16_16_16 instead. This works reasonably well
2072 * for read-only access (with caveats surrounding bounds checks), but
2073 * obviously fails for write access which we have to implement for
2074 * shader images. Luckily, OpenGL doesn't expect this to be supported
2075 * anyway, and so the only impact is on PBO uploads / downloads, which
2076 * shouldn't be expected to be fast for GL_RGB anyway.
2078 if (desc
->block
.bits
== 3 * 8 || desc
->block
.bits
== 3 * 16) {
2079 if (usage
& (PIPE_BIND_SHADER_IMAGE
| PIPE_BIND_SAMPLER_VIEW
)) {
2080 usage
&= ~(PIPE_BIND_SHADER_IMAGE
| PIPE_BIND_SAMPLER_VIEW
);
2086 if (sscreen
->info
.chip_class
>= GFX10
) {
2087 const struct gfx10_format
*fmt
= &gfx10_format_table
[format
];
2088 if (!fmt
->img_format
|| fmt
->img_format
>= 128)
2093 first_non_void
= util_format_get_first_non_void_channel(format
);
2094 data_format
= si_translate_buffer_dataformat(screen
, desc
, first_non_void
);
2095 if (data_format
== V_008F0C_BUF_DATA_FORMAT_INVALID
)
2101 static bool si_is_colorbuffer_format_supported(enum chip_class chip_class
,
2102 enum pipe_format format
)
2104 return si_translate_colorformat(chip_class
, format
) != V_028C70_COLOR_INVALID
&&
2105 si_translate_colorswap(format
, false) != ~0U;
2108 static bool si_is_zs_format_supported(enum pipe_format format
)
2110 return si_translate_dbformat(format
) != V_028040_Z_INVALID
;
2113 static bool si_is_format_supported(struct pipe_screen
*screen
, enum pipe_format format
,
2114 enum pipe_texture_target target
, unsigned sample_count
,
2115 unsigned storage_sample_count
, unsigned usage
)
2117 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2118 unsigned retval
= 0;
2120 if (target
>= PIPE_MAX_TEXTURE_TYPES
) {
2121 PRINT_ERR("radeonsi: unsupported texture type %d\n", target
);
2125 if (MAX2(1, sample_count
) < MAX2(1, storage_sample_count
))
2128 if (sample_count
> 1) {
2129 if (!screen
->get_param(screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
))
2132 /* Only power-of-two sample counts are supported. */
2133 if (!util_is_power_of_two_or_zero(sample_count
) ||
2134 !util_is_power_of_two_or_zero(storage_sample_count
))
2137 /* Chips with 1 RB don't increment occlusion queries at 16x MSAA sample rate,
2138 * so don't expose 16 samples there.
2140 const unsigned max_eqaa_samples
= sscreen
->info
.num_render_backends
== 1 ? 8 : 16;
2141 const unsigned max_samples
= 8;
2143 /* MSAA support without framebuffer attachments. */
2144 if (format
== PIPE_FORMAT_NONE
&& sample_count
<= max_eqaa_samples
)
2147 if (!sscreen
->info
.has_eqaa_surface_allocator
|| util_format_is_depth_or_stencil(format
)) {
2148 /* Color without EQAA or depth/stencil. */
2149 if (sample_count
> max_samples
|| sample_count
!= storage_sample_count
)
2152 /* Color with EQAA. */
2153 if (sample_count
> max_eqaa_samples
|| storage_sample_count
> max_samples
)
2158 if (usage
& (PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_SHADER_IMAGE
)) {
2159 if (target
== PIPE_BUFFER
) {
2160 retval
|= si_is_vertex_format_supported(
2161 screen
, format
, usage
& (PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_SHADER_IMAGE
));
2163 if (si_is_sampler_format_supported(screen
, format
))
2164 retval
|= usage
& (PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_SHADER_IMAGE
);
2168 if ((usage
& (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DISPLAY_TARGET
| PIPE_BIND_SCANOUT
|
2169 PIPE_BIND_SHARED
| PIPE_BIND_BLENDABLE
)) &&
2170 si_is_colorbuffer_format_supported(sscreen
->info
.chip_class
, format
)) {
2171 retval
|= usage
& (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DISPLAY_TARGET
| PIPE_BIND_SCANOUT
|
2173 if (!util_format_is_pure_integer(format
) && !util_format_is_depth_or_stencil(format
))
2174 retval
|= usage
& PIPE_BIND_BLENDABLE
;
2177 if ((usage
& PIPE_BIND_DEPTH_STENCIL
) && si_is_zs_format_supported(format
)) {
2178 retval
|= PIPE_BIND_DEPTH_STENCIL
;
2181 if (usage
& PIPE_BIND_VERTEX_BUFFER
) {
2182 retval
|= si_is_vertex_format_supported(screen
, format
, PIPE_BIND_VERTEX_BUFFER
);
2185 if ((usage
& PIPE_BIND_LINEAR
) && !util_format_is_compressed(format
) &&
2186 !(usage
& PIPE_BIND_DEPTH_STENCIL
))
2187 retval
|= PIPE_BIND_LINEAR
;
2189 return retval
== usage
;
2193 * framebuffer handling
2196 static void si_choose_spi_color_formats(struct si_surface
*surf
, unsigned format
, unsigned swap
,
2197 unsigned ntype
, bool is_depth
)
2199 struct ac_spi_color_formats formats
= {};
2201 ac_choose_spi_color_formats(format
, swap
, ntype
, is_depth
, &formats
);
2203 surf
->spi_shader_col_format
= formats
.normal
;
2204 surf
->spi_shader_col_format_alpha
= formats
.alpha
;
2205 surf
->spi_shader_col_format_blend
= formats
.blend
;
2206 surf
->spi_shader_col_format_blend_alpha
= formats
.blend_alpha
;
2209 static void si_initialize_color_surface(struct si_context
*sctx
, struct si_surface
*surf
)
2211 struct si_texture
*tex
= (struct si_texture
*)surf
->base
.texture
;
2212 unsigned color_info
, color_attrib
;
2213 unsigned format
, swap
, ntype
, endian
;
2214 const struct util_format_description
*desc
;
2216 unsigned blend_clamp
= 0, blend_bypass
= 0;
2218 desc
= util_format_description(surf
->base
.format
);
2219 for (firstchan
= 0; firstchan
< 4; firstchan
++) {
2220 if (desc
->channel
[firstchan
].type
!= UTIL_FORMAT_TYPE_VOID
) {
2224 if (firstchan
== 4 || desc
->channel
[firstchan
].type
== UTIL_FORMAT_TYPE_FLOAT
) {
2225 ntype
= V_028C70_NUMBER_FLOAT
;
2227 ntype
= V_028C70_NUMBER_UNORM
;
2228 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
)
2229 ntype
= V_028C70_NUMBER_SRGB
;
2230 else if (desc
->channel
[firstchan
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
2231 if (desc
->channel
[firstchan
].pure_integer
) {
2232 ntype
= V_028C70_NUMBER_SINT
;
2234 assert(desc
->channel
[firstchan
].normalized
);
2235 ntype
= V_028C70_NUMBER_SNORM
;
2237 } else if (desc
->channel
[firstchan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
2238 if (desc
->channel
[firstchan
].pure_integer
) {
2239 ntype
= V_028C70_NUMBER_UINT
;
2241 assert(desc
->channel
[firstchan
].normalized
);
2242 ntype
= V_028C70_NUMBER_UNORM
;
2247 format
= si_translate_colorformat(sctx
->chip_class
, surf
->base
.format
);
2248 if (format
== V_028C70_COLOR_INVALID
) {
2249 PRINT_ERR("Invalid CB format: %d, disabling CB.\n", surf
->base
.format
);
2251 assert(format
!= V_028C70_COLOR_INVALID
);
2252 swap
= si_translate_colorswap(surf
->base
.format
, false);
2253 endian
= si_colorformat_endian_swap(format
);
2255 /* blend clamp should be set for all NORM/SRGB types */
2256 if (ntype
== V_028C70_NUMBER_UNORM
|| ntype
== V_028C70_NUMBER_SNORM
||
2257 ntype
== V_028C70_NUMBER_SRGB
)
2260 /* set blend bypass according to docs if SINT/UINT or
2261 8/24 COLOR variants */
2262 if (ntype
== V_028C70_NUMBER_UINT
|| ntype
== V_028C70_NUMBER_SINT
||
2263 format
== V_028C70_COLOR_8_24
|| format
== V_028C70_COLOR_24_8
||
2264 format
== V_028C70_COLOR_X24_8_32_FLOAT
) {
2269 if (ntype
== V_028C70_NUMBER_UINT
|| ntype
== V_028C70_NUMBER_SINT
) {
2270 if (format
== V_028C70_COLOR_8
|| format
== V_028C70_COLOR_8_8
||
2271 format
== V_028C70_COLOR_8_8_8_8
)
2272 surf
->color_is_int8
= true;
2273 else if (format
== V_028C70_COLOR_10_10_10_2
|| format
== V_028C70_COLOR_2_10_10_10
)
2274 surf
->color_is_int10
= true;
2278 S_028C70_FORMAT(format
) | S_028C70_COMP_SWAP(swap
) | S_028C70_BLEND_CLAMP(blend_clamp
) |
2279 S_028C70_BLEND_BYPASS(blend_bypass
) | S_028C70_SIMPLE_FLOAT(1) |
2280 S_028C70_ROUND_MODE(ntype
!= V_028C70_NUMBER_UNORM
&& ntype
!= V_028C70_NUMBER_SNORM
&&
2281 ntype
!= V_028C70_NUMBER_SRGB
&& format
!= V_028C70_COLOR_8_24
&&
2282 format
!= V_028C70_COLOR_24_8
) |
2283 S_028C70_NUMBER_TYPE(ntype
) | S_028C70_ENDIAN(endian
);
2285 /* Intensity is implemented as Red, so treat it that way. */
2286 color_attrib
= S_028C74_FORCE_DST_ALPHA_1(desc
->swizzle
[3] == PIPE_SWIZZLE_1
||
2287 util_format_is_intensity(surf
->base
.format
));
2289 if (tex
->buffer
.b
.b
.nr_samples
> 1) {
2290 unsigned log_samples
= util_logbase2(tex
->buffer
.b
.b
.nr_samples
);
2291 unsigned log_fragments
= util_logbase2(tex
->buffer
.b
.b
.nr_storage_samples
);
2293 color_attrib
|= S_028C74_NUM_SAMPLES(log_samples
) | S_028C74_NUM_FRAGMENTS(log_fragments
);
2295 if (tex
->surface
.fmask_offset
) {
2296 color_info
|= S_028C70_COMPRESSION(1);
2297 unsigned fmask_bankh
= util_logbase2(tex
->surface
.u
.legacy
.fmask
.bankh
);
2299 if (sctx
->chip_class
== GFX6
) {
2300 /* due to a hw bug, FMASK_BANK_HEIGHT must be set on GFX6 too */
2301 color_attrib
|= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh
);
2306 if (sctx
->chip_class
>= GFX10
) {
2307 unsigned min_compressed_block_size
= V_028C78_MIN_BLOCK_SIZE_32B
;
2309 /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
2310 64 for APU because all of our APUs to date use DIMMs which have
2311 a request granularity size of 64B while all other chips have a
2313 if (!sctx
->screen
->info
.has_dedicated_vram
)
2314 min_compressed_block_size
= V_028C78_MIN_BLOCK_SIZE_64B
;
2316 surf
->cb_dcc_control
= S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(V_028C78_MAX_BLOCK_SIZE_256B
) |
2317 S_028C78_MAX_COMPRESSED_BLOCK_SIZE(tex
->surface
.u
.gfx9
.dcc
.max_compressed_block_size
) |
2318 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size
) |
2319 S_028C78_INDEPENDENT_64B_BLOCKS(tex
->surface
.u
.gfx9
.dcc
.independent_64B_blocks
) |
2320 S_028C78_INDEPENDENT_128B_BLOCKS(tex
->surface
.u
.gfx9
.dcc
.independent_128B_blocks
);
2321 } else if (sctx
->chip_class
>= GFX8
) {
2322 unsigned max_uncompressed_block_size
= V_028C78_MAX_BLOCK_SIZE_256B
;
2323 unsigned min_compressed_block_size
= V_028C78_MIN_BLOCK_SIZE_32B
;
2325 /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
2326 64 for APU because all of our APUs to date use DIMMs which have
2327 a request granularity size of 64B while all other chips have a
2329 if (!sctx
->screen
->info
.has_dedicated_vram
)
2330 min_compressed_block_size
= V_028C78_MIN_BLOCK_SIZE_64B
;
2332 if (tex
->buffer
.b
.b
.nr_storage_samples
> 1) {
2333 if (tex
->surface
.bpe
== 1)
2334 max_uncompressed_block_size
= V_028C78_MAX_BLOCK_SIZE_64B
;
2335 else if (tex
->surface
.bpe
== 2)
2336 max_uncompressed_block_size
= V_028C78_MAX_BLOCK_SIZE_128B
;
2339 surf
->cb_dcc_control
= S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size
) |
2340 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size
) |
2341 S_028C78_INDEPENDENT_64B_BLOCKS(1);
2344 /* This must be set for fast clear to work without FMASK. */
2345 if (!tex
->surface
.fmask_size
&& sctx
->chip_class
== GFX6
) {
2346 unsigned bankh
= util_logbase2(tex
->surface
.u
.legacy
.bankh
);
2347 color_attrib
|= S_028C74_FMASK_BANK_HEIGHT(bankh
);
2350 /* GFX10 field has the same base shift as the GFX6 field */
2351 unsigned color_view
= S_028C6C_SLICE_START(surf
->base
.u
.tex
.first_layer
) |
2352 S_028C6C_SLICE_MAX_GFX10(surf
->base
.u
.tex
.last_layer
);
2353 unsigned mip0_depth
= util_max_layer(&tex
->buffer
.b
.b
, 0);
2355 if (sctx
->chip_class
>= GFX10
) {
2356 color_view
|= S_028C6C_MIP_LEVEL_GFX10(surf
->base
.u
.tex
.level
);
2358 surf
->cb_color_attrib3
= S_028EE0_MIP0_DEPTH(mip0_depth
) |
2359 S_028EE0_RESOURCE_TYPE(tex
->surface
.u
.gfx9
.resource_type
) |
2360 S_028EE0_RESOURCE_LEVEL(1);
2361 } else if (sctx
->chip_class
== GFX9
) {
2362 color_view
|= S_028C6C_MIP_LEVEL_GFX9(surf
->base
.u
.tex
.level
);
2363 color_attrib
|= S_028C74_MIP0_DEPTH(mip0_depth
) |
2364 S_028C74_RESOURCE_TYPE(tex
->surface
.u
.gfx9
.resource_type
);
2367 if (sctx
->chip_class
>= GFX9
) {
2368 surf
->cb_color_attrib2
= S_028C68_MIP0_WIDTH(surf
->width0
- 1) |
2369 S_028C68_MIP0_HEIGHT(surf
->height0
- 1) |
2370 S_028C68_MAX_MIP(tex
->buffer
.b
.b
.last_level
);
2373 surf
->cb_color_view
= color_view
;
2374 surf
->cb_color_info
= color_info
;
2375 surf
->cb_color_attrib
= color_attrib
;
2377 /* Determine pixel shader export format */
2378 si_choose_spi_color_formats(surf
, format
, swap
, ntype
, tex
->is_depth
);
2380 surf
->color_initialized
= true;
2383 static void si_init_depth_surface(struct si_context
*sctx
, struct si_surface
*surf
)
2385 struct si_texture
*tex
= (struct si_texture
*)surf
->base
.texture
;
2386 unsigned level
= surf
->base
.u
.tex
.level
;
2387 unsigned format
, stencil_format
;
2388 uint32_t z_info
, s_info
;
2390 format
= si_translate_dbformat(tex
->db_render_format
);
2391 stencil_format
= tex
->surface
.has_stencil
? V_028044_STENCIL_8
: V_028044_STENCIL_INVALID
;
2393 assert(format
!= V_028040_Z_INVALID
);
2394 if (format
== V_028040_Z_INVALID
)
2395 PRINT_ERR("Invalid DB format: %d, disabling DB.\n", tex
->buffer
.b
.b
.format
);
2397 surf
->db_depth_view
= S_028008_SLICE_START(surf
->base
.u
.tex
.first_layer
) |
2398 S_028008_SLICE_MAX(surf
->base
.u
.tex
.last_layer
);
2399 surf
->db_htile_data_base
= 0;
2400 surf
->db_htile_surface
= 0;
2402 if (sctx
->chip_class
>= GFX10
) {
2403 surf
->db_depth_view
|= S_028008_SLICE_START_HI(surf
->base
.u
.tex
.first_layer
>> 11) |
2404 S_028008_SLICE_MAX_HI(surf
->base
.u
.tex
.last_layer
>> 11);
2407 if (sctx
->chip_class
>= GFX9
) {
2408 assert(tex
->surface
.u
.gfx9
.surf_offset
== 0);
2409 surf
->db_depth_base
= tex
->buffer
.gpu_address
>> 8;
2410 surf
->db_stencil_base
= (tex
->buffer
.gpu_address
+ tex
->surface
.u
.gfx9
.stencil_offset
) >> 8;
2411 z_info
= S_028038_FORMAT(format
) |
2412 S_028038_NUM_SAMPLES(util_logbase2(tex
->buffer
.b
.b
.nr_samples
)) |
2413 S_028038_SW_MODE(tex
->surface
.u
.gfx9
.surf
.swizzle_mode
) |
2414 S_028038_MAXMIP(tex
->buffer
.b
.b
.last_level
);
2415 s_info
= S_02803C_FORMAT(stencil_format
) |
2416 S_02803C_SW_MODE(tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
);
2418 if (sctx
->chip_class
== GFX9
) {
2419 surf
->db_z_info2
= S_028068_EPITCH(tex
->surface
.u
.gfx9
.surf
.epitch
);
2420 surf
->db_stencil_info2
= S_02806C_EPITCH(tex
->surface
.u
.gfx9
.stencil
.epitch
);
2422 surf
->db_depth_view
|= S_028008_MIPID(level
);
2423 surf
->db_depth_size
=
2424 S_02801C_X_MAX(tex
->buffer
.b
.b
.width0
- 1) | S_02801C_Y_MAX(tex
->buffer
.b
.b
.height0
- 1);
2426 if (si_htile_enabled(tex
, level
, PIPE_MASK_ZS
)) {
2427 z_info
|= S_028038_TILE_SURFACE_ENABLE(1) | S_028038_ALLOW_EXPCLEAR(1);
2429 if (tex
->surface
.has_stencil
&& !tex
->htile_stencil_disabled
) {
2430 /* Stencil buffer workaround ported from the GFX6-GFX8 code.
2431 * See that for explanation.
2433 s_info
|= S_02803C_ALLOW_EXPCLEAR(tex
->buffer
.b
.b
.nr_samples
<= 1);
2435 /* Use all HTILE for depth if there's no stencil. */
2436 s_info
|= S_02803C_TILE_STENCIL_DISABLE(1);
2439 surf
->db_htile_data_base
= (tex
->buffer
.gpu_address
+ tex
->surface
.htile_offset
) >> 8;
2440 surf
->db_htile_surface
=
2441 S_028ABC_FULL_CACHE(1) | S_028ABC_PIPE_ALIGNED(1);
2442 if (sctx
->chip_class
== GFX9
) {
2443 surf
->db_htile_surface
|= S_028ABC_RB_ALIGNED(1);
2448 struct legacy_surf_level
*levelinfo
= &tex
->surface
.u
.legacy
.level
[level
];
2450 assert(levelinfo
->nblk_x
% 8 == 0 && levelinfo
->nblk_y
% 8 == 0);
2452 surf
->db_depth_base
=
2453 (tex
->buffer
.gpu_address
+ tex
->surface
.u
.legacy
.level
[level
].offset
) >> 8;
2454 surf
->db_stencil_base
=
2455 (tex
->buffer
.gpu_address
+ tex
->surface
.u
.legacy
.stencil_level
[level
].offset
) >> 8;
2458 S_028040_FORMAT(format
) | S_028040_NUM_SAMPLES(util_logbase2(tex
->buffer
.b
.b
.nr_samples
));
2459 s_info
= S_028044_FORMAT(stencil_format
);
2460 surf
->db_depth_info
= 0;
2462 if (sctx
->chip_class
>= GFX7
) {
2463 struct radeon_info
*info
= &sctx
->screen
->info
;
2464 unsigned index
= tex
->surface
.u
.legacy
.tiling_index
[level
];
2465 unsigned stencil_index
= tex
->surface
.u
.legacy
.stencil_tiling_index
[level
];
2466 unsigned macro_index
= tex
->surface
.u
.legacy
.macro_tile_index
;
2467 unsigned tile_mode
= info
->si_tile_mode_array
[index
];
2468 unsigned stencil_tile_mode
= info
->si_tile_mode_array
[stencil_index
];
2469 unsigned macro_mode
= info
->cik_macrotile_mode_array
[macro_index
];
2471 surf
->db_depth_info
|= S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode
)) |
2472 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode
)) |
2473 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode
)) |
2474 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode
)) |
2475 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode
)) |
2476 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode
));
2477 z_info
|= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode
));
2478 s_info
|= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode
));
2480 unsigned tile_mode_index
= si_tile_mode_index(tex
, level
, false);
2481 z_info
|= S_028040_TILE_MODE_INDEX(tile_mode_index
);
2482 tile_mode_index
= si_tile_mode_index(tex
, level
, true);
2483 s_info
|= S_028044_TILE_MODE_INDEX(tile_mode_index
);
2486 surf
->db_depth_size
= S_028058_PITCH_TILE_MAX((levelinfo
->nblk_x
/ 8) - 1) |
2487 S_028058_HEIGHT_TILE_MAX((levelinfo
->nblk_y
/ 8) - 1);
2488 surf
->db_depth_slice
=
2489 S_02805C_SLICE_TILE_MAX((levelinfo
->nblk_x
* levelinfo
->nblk_y
) / 64 - 1);
2491 if (si_htile_enabled(tex
, level
, PIPE_MASK_ZS
)) {
2492 z_info
|= S_028040_TILE_SURFACE_ENABLE(1) | S_028040_ALLOW_EXPCLEAR(1);
2494 if (tex
->surface
.has_stencil
) {
2495 /* Workaround: For a not yet understood reason, the
2496 * combination of MSAA, fast stencil clear and stencil
2497 * decompress messes with subsequent stencil buffer
2498 * uses. Problem was reproduced on Verde, Bonaire,
2499 * Tonga, and Carrizo.
2501 * Disabling EXPCLEAR works around the problem.
2503 * Check piglit's arb_texture_multisample-stencil-clear
2504 * test if you want to try changing this.
2506 if (tex
->buffer
.b
.b
.nr_samples
<= 1)
2507 s_info
|= S_028044_ALLOW_EXPCLEAR(1);
2510 surf
->db_htile_data_base
= (tex
->buffer
.gpu_address
+ tex
->surface
.htile_offset
) >> 8;
2511 surf
->db_htile_surface
= S_028ABC_FULL_CACHE(1);
2515 surf
->db_z_info
= z_info
;
2516 surf
->db_stencil_info
= s_info
;
2518 surf
->depth_initialized
= true;
2521 void si_update_fb_dirtiness_after_rendering(struct si_context
*sctx
)
2523 if (sctx
->decompression_enabled
)
2526 if (sctx
->framebuffer
.state
.zsbuf
) {
2527 struct pipe_surface
*surf
= sctx
->framebuffer
.state
.zsbuf
;
2528 struct si_texture
*tex
= (struct si_texture
*)surf
->texture
;
2530 tex
->dirty_level_mask
|= 1 << surf
->u
.tex
.level
;
2532 if (tex
->surface
.has_stencil
)
2533 tex
->stencil_dirty_level_mask
|= 1 << surf
->u
.tex
.level
;
2536 unsigned compressed_cb_mask
= sctx
->framebuffer
.compressed_cb_mask
;
2537 while (compressed_cb_mask
) {
2538 unsigned i
= u_bit_scan(&compressed_cb_mask
);
2539 struct pipe_surface
*surf
= sctx
->framebuffer
.state
.cbufs
[i
];
2540 struct si_texture
*tex
= (struct si_texture
*)surf
->texture
;
2542 if (tex
->surface
.fmask_offset
) {
2543 tex
->dirty_level_mask
|= 1 << surf
->u
.tex
.level
;
2544 tex
->fmask_is_identity
= false;
2546 if (tex
->dcc_gather_statistics
)
2547 tex
->separate_dcc_dirty
= true;
2551 static void si_dec_framebuffer_counters(const struct pipe_framebuffer_state
*state
)
2553 for (int i
= 0; i
< state
->nr_cbufs
; ++i
) {
2554 struct si_surface
*surf
= NULL
;
2555 struct si_texture
*tex
;
2557 if (!state
->cbufs
[i
])
2559 surf
= (struct si_surface
*)state
->cbufs
[i
];
2560 tex
= (struct si_texture
*)surf
->base
.texture
;
2562 p_atomic_dec(&tex
->framebuffers_bound
);
2566 static void si_set_framebuffer_state(struct pipe_context
*ctx
,
2567 const struct pipe_framebuffer_state
*state
)
2569 struct si_context
*sctx
= (struct si_context
*)ctx
;
2570 struct si_surface
*surf
= NULL
;
2571 struct si_texture
*tex
;
2572 bool old_any_dst_linear
= sctx
->framebuffer
.any_dst_linear
;
2573 unsigned old_nr_samples
= sctx
->framebuffer
.nr_samples
;
2574 unsigned old_colorbuf_enabled_4bit
= sctx
->framebuffer
.colorbuf_enabled_4bit
;
2575 bool old_has_zsbuf
= !!sctx
->framebuffer
.state
.zsbuf
;
2576 bool old_has_stencil
=
2578 ((struct si_texture
*)sctx
->framebuffer
.state
.zsbuf
->texture
)->surface
.has_stencil
;
2579 bool unbound
= false;
2582 /* Reject zero-sized framebuffers due to a hw bug on GFX6 that occurs
2583 * when PA_SU_HARDWARE_SCREEN_OFFSET != 0 and any_scissor.BR_X/Y <= 0.
2584 * We could implement the full workaround here, but it's a useless case.
2586 if ((!state
->width
|| !state
->height
) && (state
->nr_cbufs
|| state
->zsbuf
)) {
2587 unreachable("the framebuffer shouldn't have zero area");
2591 si_update_fb_dirtiness_after_rendering(sctx
);
2593 for (i
= 0; i
< sctx
->framebuffer
.state
.nr_cbufs
; i
++) {
2594 if (!sctx
->framebuffer
.state
.cbufs
[i
])
2597 tex
= (struct si_texture
*)sctx
->framebuffer
.state
.cbufs
[i
]->texture
;
2598 if (tex
->dcc_gather_statistics
)
2599 vi_separate_dcc_stop_query(sctx
, tex
);
2602 /* Disable DCC if the formats are incompatible. */
2603 for (i
= 0; i
< state
->nr_cbufs
; i
++) {
2604 if (!state
->cbufs
[i
])
2607 surf
= (struct si_surface
*)state
->cbufs
[i
];
2608 tex
= (struct si_texture
*)surf
->base
.texture
;
2610 if (!surf
->dcc_incompatible
)
2613 /* Since the DCC decompression calls back into set_framebuffer-
2614 * _state, we need to unbind the framebuffer, so that
2615 * vi_separate_dcc_stop_query isn't called twice with the same
2619 util_copy_framebuffer_state(&sctx
->framebuffer
.state
, NULL
);
2623 if (vi_dcc_enabled(tex
, surf
->base
.u
.tex
.level
))
2624 if (!si_texture_disable_dcc(sctx
, tex
))
2625 si_decompress_dcc(sctx
, tex
);
2627 surf
->dcc_incompatible
= false;
2630 /* Only flush TC when changing the framebuffer state, because
2631 * the only client not using TC that can change textures is
2634 * Wait for compute shaders because of possible transitions:
2635 * - FB write -> shader read
2636 * - shader write -> FB read
2638 * DB caches are flushed on demand (using si_decompress_textures).
2640 * When MSAA is enabled, CB and TC caches are flushed on demand
2641 * (after FMASK decompression). Shader write -> FB read transitions
2642 * cannot happen for MSAA textures, because MSAA shader images are
2645 * Only flush and wait for CB if there is actually a bound color buffer.
2647 if (sctx
->framebuffer
.uncompressed_cb_mask
) {
2648 si_make_CB_shader_coherent(sctx
, sctx
->framebuffer
.nr_samples
,
2649 sctx
->framebuffer
.CB_has_shader_readable_metadata
,
2650 sctx
->framebuffer
.all_DCC_pipe_aligned
);
2653 sctx
->flags
|= SI_CONTEXT_CS_PARTIAL_FLUSH
;
2655 /* u_blitter doesn't invoke depth decompression when it does multiple
2656 * blits in a row, but the only case when it matters for DB is when
2657 * doing generate_mipmap. So here we flush DB manually between
2658 * individual generate_mipmap blits.
2659 * Note that lower mipmap levels aren't compressed.
2661 if (sctx
->generate_mipmap_for_depth
) {
2662 si_make_DB_shader_coherent(sctx
, 1, false, sctx
->framebuffer
.DB_has_shader_readable_metadata
);
2663 } else if (sctx
->chip_class
== GFX9
) {
2664 /* It appears that DB metadata "leaks" in a sequence of:
2666 * - DCC decompress for shader image writes (with DB disabled)
2667 * - render with DEPTH_BEFORE_SHADER=1
2668 * Flushing DB metadata works around the problem.
2670 sctx
->flags
|= SI_CONTEXT_FLUSH_AND_INV_DB_META
;
2673 /* Take the maximum of the old and new count. If the new count is lower,
2674 * dirtying is needed to disable the unbound colorbuffers.
2676 sctx
->framebuffer
.dirty_cbufs
|=
2677 (1 << MAX2(sctx
->framebuffer
.state
.nr_cbufs
, state
->nr_cbufs
)) - 1;
2678 sctx
->framebuffer
.dirty_zsbuf
|= sctx
->framebuffer
.state
.zsbuf
!= state
->zsbuf
;
2680 si_dec_framebuffer_counters(&sctx
->framebuffer
.state
);
2681 util_copy_framebuffer_state(&sctx
->framebuffer
.state
, state
);
2683 sctx
->framebuffer
.colorbuf_enabled_4bit
= 0;
2684 sctx
->framebuffer
.spi_shader_col_format
= 0;
2685 sctx
->framebuffer
.spi_shader_col_format_alpha
= 0;
2686 sctx
->framebuffer
.spi_shader_col_format_blend
= 0;
2687 sctx
->framebuffer
.spi_shader_col_format_blend_alpha
= 0;
2688 sctx
->framebuffer
.color_is_int8
= 0;
2689 sctx
->framebuffer
.color_is_int10
= 0;
2691 sctx
->framebuffer
.compressed_cb_mask
= 0;
2692 sctx
->framebuffer
.uncompressed_cb_mask
= 0;
2693 sctx
->framebuffer
.displayable_dcc_cb_mask
= 0;
2694 sctx
->framebuffer
.nr_samples
= util_framebuffer_get_num_samples(state
);
2695 sctx
->framebuffer
.nr_color_samples
= sctx
->framebuffer
.nr_samples
;
2696 sctx
->framebuffer
.log_samples
= util_logbase2(sctx
->framebuffer
.nr_samples
);
2697 sctx
->framebuffer
.any_dst_linear
= false;
2698 sctx
->framebuffer
.CB_has_shader_readable_metadata
= false;
2699 sctx
->framebuffer
.DB_has_shader_readable_metadata
= false;
2700 sctx
->framebuffer
.all_DCC_pipe_aligned
= true;
2701 sctx
->framebuffer
.min_bytes_per_pixel
= 0;
2702 sctx
->framebuffer
.color_big_page
= true;
2703 sctx
->framebuffer
.zs_big_page
= true;
2705 for (i
= 0; i
< state
->nr_cbufs
; i
++) {
2706 if (!state
->cbufs
[i
])
2709 surf
= (struct si_surface
*)state
->cbufs
[i
];
2710 tex
= (struct si_texture
*)surf
->base
.texture
;
2712 if (!surf
->color_initialized
) {
2713 si_initialize_color_surface(sctx
, surf
);
2716 sctx
->framebuffer
.colorbuf_enabled_4bit
|= 0xf << (i
* 4);
2717 sctx
->framebuffer
.spi_shader_col_format
|= surf
->spi_shader_col_format
<< (i
* 4);
2718 sctx
->framebuffer
.spi_shader_col_format_alpha
|= surf
->spi_shader_col_format_alpha
<< (i
* 4);
2719 sctx
->framebuffer
.spi_shader_col_format_blend
|= surf
->spi_shader_col_format_blend
<< (i
* 4);
2720 sctx
->framebuffer
.spi_shader_col_format_blend_alpha
|= surf
->spi_shader_col_format_blend_alpha
2723 sctx
->framebuffer
.color_big_page
&=
2724 tex
->buffer
.bo_alignment
% (64 * 1024) == 0;
2726 if (surf
->color_is_int8
)
2727 sctx
->framebuffer
.color_is_int8
|= 1 << i
;
2728 if (surf
->color_is_int10
)
2729 sctx
->framebuffer
.color_is_int10
|= 1 << i
;
2731 if (tex
->surface
.fmask_offset
)
2732 sctx
->framebuffer
.compressed_cb_mask
|= 1 << i
;
2734 sctx
->framebuffer
.uncompressed_cb_mask
|= 1 << i
;
2736 if (tex
->surface
.display_dcc_offset
)
2737 sctx
->framebuffer
.displayable_dcc_cb_mask
|= 1 << i
;
2739 /* Don't update nr_color_samples for non-AA buffers.
2740 * (e.g. destination of MSAA resolve)
2742 if (tex
->buffer
.b
.b
.nr_samples
>= 2 &&
2743 tex
->buffer
.b
.b
.nr_storage_samples
< tex
->buffer
.b
.b
.nr_samples
) {
2744 sctx
->framebuffer
.nr_color_samples
=
2745 MIN2(sctx
->framebuffer
.nr_color_samples
, tex
->buffer
.b
.b
.nr_storage_samples
);
2746 sctx
->framebuffer
.nr_color_samples
= MAX2(1, sctx
->framebuffer
.nr_color_samples
);
2749 if (tex
->surface
.is_linear
)
2750 sctx
->framebuffer
.any_dst_linear
= true;
2752 if (vi_dcc_enabled(tex
, surf
->base
.u
.tex
.level
)) {
2753 sctx
->framebuffer
.CB_has_shader_readable_metadata
= true;
2755 if (sctx
->chip_class
>= GFX9
&& !tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
)
2756 sctx
->framebuffer
.all_DCC_pipe_aligned
= false;
2759 si_context_add_resource_size(sctx
, surf
->base
.texture
);
2761 p_atomic_inc(&tex
->framebuffers_bound
);
2763 if (tex
->dcc_gather_statistics
) {
2764 /* Dirty tracking must be enabled for DCC usage analysis. */
2765 sctx
->framebuffer
.compressed_cb_mask
|= 1 << i
;
2766 vi_separate_dcc_start_query(sctx
, tex
);
2769 /* Update the minimum but don't keep 0. */
2770 if (!sctx
->framebuffer
.min_bytes_per_pixel
||
2771 tex
->surface
.bpe
< sctx
->framebuffer
.min_bytes_per_pixel
)
2772 sctx
->framebuffer
.min_bytes_per_pixel
= tex
->surface
.bpe
;
2775 /* For optimal DCC performance. */
2776 if (sctx
->chip_class
>= GFX10
)
2777 sctx
->framebuffer
.dcc_overwrite_combiner_watermark
= 6;
2779 sctx
->framebuffer
.dcc_overwrite_combiner_watermark
= 4;
2781 struct si_texture
*zstex
= NULL
;
2784 surf
= (struct si_surface
*)state
->zsbuf
;
2785 zstex
= (struct si_texture
*)surf
->base
.texture
;
2787 if (!surf
->depth_initialized
) {
2788 si_init_depth_surface(sctx
, surf
);
2791 sctx
->framebuffer
.zs_big_page
= zstex
->buffer
.bo_alignment
% (64 * 1024) == 0;
2793 if (vi_tc_compat_htile_enabled(zstex
, surf
->base
.u
.tex
.level
, PIPE_MASK_ZS
))
2794 sctx
->framebuffer
.DB_has_shader_readable_metadata
= true;
2796 si_context_add_resource_size(sctx
, surf
->base
.texture
);
2798 /* Update the minimum but don't keep 0. */
2799 if (!sctx
->framebuffer
.min_bytes_per_pixel
||
2800 zstex
->surface
.bpe
< sctx
->framebuffer
.min_bytes_per_pixel
)
2801 sctx
->framebuffer
.min_bytes_per_pixel
= zstex
->surface
.bpe
;
2804 si_update_ps_colorbuf0_slot(sctx
);
2805 si_update_poly_offset_state(sctx
);
2806 si_update_ngg_small_prim_precision(sctx
);
2807 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.cb_render_state
);
2808 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.framebuffer
);
2810 if (sctx
->screen
->dpbb_allowed
)
2811 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.dpbb_state
);
2813 if (sctx
->framebuffer
.any_dst_linear
!= old_any_dst_linear
)
2814 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_config
);
2816 if (sctx
->screen
->has_out_of_order_rast
&&
2817 (sctx
->framebuffer
.colorbuf_enabled_4bit
!= old_colorbuf_enabled_4bit
||
2818 !!sctx
->framebuffer
.state
.zsbuf
!= old_has_zsbuf
||
2819 (zstex
&& zstex
->surface
.has_stencil
!= old_has_stencil
)))
2820 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_config
);
2822 if (sctx
->framebuffer
.nr_samples
!= old_nr_samples
) {
2823 struct pipe_constant_buffer constbuf
= {0};
2825 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_config
);
2826 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.db_render_state
);
2828 constbuf
.buffer
= sctx
->sample_pos_buffer
;
2830 /* Set sample locations as fragment shader constants. */
2831 switch (sctx
->framebuffer
.nr_samples
) {
2833 constbuf
.buffer_offset
= 0;
2836 constbuf
.buffer_offset
=
2837 (ubyte
*)sctx
->sample_positions
.x2
- (ubyte
*)sctx
->sample_positions
.x1
;
2840 constbuf
.buffer_offset
=
2841 (ubyte
*)sctx
->sample_positions
.x4
- (ubyte
*)sctx
->sample_positions
.x1
;
2844 constbuf
.buffer_offset
=
2845 (ubyte
*)sctx
->sample_positions
.x8
- (ubyte
*)sctx
->sample_positions
.x1
;
2848 constbuf
.buffer_offset
=
2849 (ubyte
*)sctx
->sample_positions
.x16
- (ubyte
*)sctx
->sample_positions
.x1
;
2852 PRINT_ERR("Requested an invalid number of samples %i.\n", sctx
->framebuffer
.nr_samples
);
2855 constbuf
.buffer_size
= sctx
->framebuffer
.nr_samples
* 2 * 4;
2856 si_set_rw_buffer(sctx
, SI_PS_CONST_SAMPLE_POSITIONS
, &constbuf
);
2858 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_sample_locs
);
2861 sctx
->do_update_shaders
= true;
2863 if (!sctx
->decompression_enabled
) {
2864 /* Prevent textures decompression when the framebuffer state
2865 * changes come from the decompression passes themselves.
2867 sctx
->need_check_render_feedback
= true;
2871 static void si_emit_framebuffer_state(struct si_context
*sctx
)
2873 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
2874 struct pipe_framebuffer_state
*state
= &sctx
->framebuffer
.state
;
2875 unsigned i
, nr_cbufs
= state
->nr_cbufs
;
2876 struct si_texture
*tex
= NULL
;
2877 struct si_surface
*cb
= NULL
;
2878 unsigned cb_color_info
= 0;
2880 /* Enable CMASK/FMASK/HTILE/DCC caching in L2 for small chips. */
2881 unsigned meta_write_policy
, meta_read_policy
;
2882 /* TODO: investigate whether LRU improves performance on other chips too */
2883 if (sctx
->screen
->info
.num_render_backends
<= 4) {
2884 meta_write_policy
= V_02807C_CACHE_LRU_WR
; /* cache writes */
2885 meta_read_policy
= V_02807C_CACHE_LRU_RD
; /* cache reads */
2887 meta_write_policy
= V_02807C_CACHE_STREAM
; /* write combine */
2888 meta_read_policy
= V_02807C_CACHE_NOA
; /* don't cache reads */
2892 for (i
= 0; i
< nr_cbufs
; i
++) {
2893 uint64_t cb_color_base
, cb_color_fmask
, cb_color_cmask
, cb_dcc_base
;
2894 unsigned cb_color_attrib
;
2896 if (!(sctx
->framebuffer
.dirty_cbufs
& (1 << i
)))
2899 cb
= (struct si_surface
*)state
->cbufs
[i
];
2901 radeon_set_context_reg(cs
, R_028C70_CB_COLOR0_INFO
+ i
* 0x3C,
2902 S_028C70_FORMAT(V_028C70_COLOR_INVALID
));
2906 tex
= (struct si_texture
*)cb
->base
.texture
;
2907 radeon_add_to_buffer_list(
2908 sctx
, sctx
->gfx_cs
, &tex
->buffer
, RADEON_USAGE_READWRITE
,
2909 tex
->buffer
.b
.b
.nr_samples
> 1 ? RADEON_PRIO_COLOR_BUFFER_MSAA
: RADEON_PRIO_COLOR_BUFFER
);
2911 if (tex
->cmask_buffer
&& tex
->cmask_buffer
!= &tex
->buffer
) {
2912 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, tex
->cmask_buffer
, RADEON_USAGE_READWRITE
,
2913 RADEON_PRIO_SEPARATE_META
);
2916 if (tex
->dcc_separate_buffer
)
2917 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, tex
->dcc_separate_buffer
,
2918 RADEON_USAGE_READWRITE
, RADEON_PRIO_SEPARATE_META
);
2920 /* Compute mutable surface parameters. */
2921 cb_color_base
= tex
->buffer
.gpu_address
>> 8;
2923 cb_color_cmask
= tex
->cmask_base_address_reg
;
2925 cb_color_info
= cb
->cb_color_info
| tex
->cb_color_info
;
2926 cb_color_attrib
= cb
->cb_color_attrib
;
2928 if (cb
->base
.u
.tex
.level
> 0)
2929 cb_color_info
&= C_028C70_FAST_CLEAR
;
2931 if (tex
->surface
.fmask_offset
) {
2932 cb_color_fmask
= (tex
->buffer
.gpu_address
+ tex
->surface
.fmask_offset
) >> 8;
2933 cb_color_fmask
|= tex
->surface
.fmask_tile_swizzle
;
2937 if (vi_dcc_enabled(tex
, cb
->base
.u
.tex
.level
)) {
2938 bool is_msaa_resolve_dst
= state
->cbufs
[0] && state
->cbufs
[0]->texture
->nr_samples
> 1 &&
2939 state
->cbufs
[1] == &cb
->base
&&
2940 state
->cbufs
[1]->texture
->nr_samples
<= 1;
2942 if (!is_msaa_resolve_dst
)
2943 cb_color_info
|= S_028C70_DCC_ENABLE(1);
2946 ((!tex
->dcc_separate_buffer
? tex
->buffer
.gpu_address
: 0) + tex
->surface
.dcc_offset
) >>
2949 unsigned dcc_tile_swizzle
= tex
->surface
.tile_swizzle
;
2950 dcc_tile_swizzle
&= (tex
->surface
.dcc_alignment
- 1) >> 8;
2951 cb_dcc_base
|= dcc_tile_swizzle
;
2954 if (sctx
->chip_class
>= GFX10
) {
2955 unsigned cb_color_attrib3
;
2957 /* Set mutable surface parameters. */
2958 cb_color_base
+= tex
->surface
.u
.gfx9
.surf_offset
>> 8;
2959 cb_color_base
|= tex
->surface
.tile_swizzle
;
2960 if (!tex
->surface
.fmask_offset
)
2961 cb_color_fmask
= cb_color_base
;
2962 if (cb
->base
.u
.tex
.level
> 0)
2963 cb_color_cmask
= cb_color_base
;
2965 cb_color_attrib3
= cb
->cb_color_attrib3
|
2966 S_028EE0_COLOR_SW_MODE(tex
->surface
.u
.gfx9
.surf
.swizzle_mode
) |
2967 S_028EE0_FMASK_SW_MODE(tex
->surface
.u
.gfx9
.fmask
.swizzle_mode
) |
2968 S_028EE0_CMASK_PIPE_ALIGNED(1) |
2969 S_028EE0_DCC_PIPE_ALIGNED(tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
);
2971 radeon_set_context_reg_seq(cs
, R_028C60_CB_COLOR0_BASE
+ i
* 0x3C, 14);
2972 radeon_emit(cs
, cb_color_base
); /* CB_COLOR0_BASE */
2973 radeon_emit(cs
, 0); /* hole */
2974 radeon_emit(cs
, 0); /* hole */
2975 radeon_emit(cs
, cb
->cb_color_view
); /* CB_COLOR0_VIEW */
2976 radeon_emit(cs
, cb_color_info
); /* CB_COLOR0_INFO */
2977 radeon_emit(cs
, cb_color_attrib
); /* CB_COLOR0_ATTRIB */
2978 radeon_emit(cs
, cb
->cb_dcc_control
); /* CB_COLOR0_DCC_CONTROL */
2979 radeon_emit(cs
, cb_color_cmask
); /* CB_COLOR0_CMASK */
2980 radeon_emit(cs
, 0); /* hole */
2981 radeon_emit(cs
, cb_color_fmask
); /* CB_COLOR0_FMASK */
2982 radeon_emit(cs
, 0); /* hole */
2983 radeon_emit(cs
, tex
->color_clear_value
[0]); /* CB_COLOR0_CLEAR_WORD0 */
2984 radeon_emit(cs
, tex
->color_clear_value
[1]); /* CB_COLOR0_CLEAR_WORD1 */
2985 radeon_emit(cs
, cb_dcc_base
); /* CB_COLOR0_DCC_BASE */
2987 radeon_set_context_reg(cs
, R_028E40_CB_COLOR0_BASE_EXT
+ i
* 4, cb_color_base
>> 32);
2988 radeon_set_context_reg(cs
, R_028E60_CB_COLOR0_CMASK_BASE_EXT
+ i
* 4,
2989 cb_color_cmask
>> 32);
2990 radeon_set_context_reg(cs
, R_028E80_CB_COLOR0_FMASK_BASE_EXT
+ i
* 4,
2991 cb_color_fmask
>> 32);
2992 radeon_set_context_reg(cs
, R_028EA0_CB_COLOR0_DCC_BASE_EXT
+ i
* 4, cb_dcc_base
>> 32);
2993 radeon_set_context_reg(cs
, R_028EC0_CB_COLOR0_ATTRIB2
+ i
* 4, cb
->cb_color_attrib2
);
2994 radeon_set_context_reg(cs
, R_028EE0_CB_COLOR0_ATTRIB3
+ i
* 4, cb_color_attrib3
);
2995 } else if (sctx
->chip_class
== GFX9
) {
2996 struct gfx9_surf_meta_flags meta
= {
3001 if (tex
->surface
.dcc_offset
)
3002 meta
= tex
->surface
.u
.gfx9
.dcc
;
3004 /* Set mutable surface parameters. */
3005 cb_color_base
+= tex
->surface
.u
.gfx9
.surf_offset
>> 8;
3006 cb_color_base
|= tex
->surface
.tile_swizzle
;
3007 if (!tex
->surface
.fmask_offset
)
3008 cb_color_fmask
= cb_color_base
;
3009 if (cb
->base
.u
.tex
.level
> 0)
3010 cb_color_cmask
= cb_color_base
;
3011 cb_color_attrib
|= S_028C74_COLOR_SW_MODE(tex
->surface
.u
.gfx9
.surf
.swizzle_mode
) |
3012 S_028C74_FMASK_SW_MODE(tex
->surface
.u
.gfx9
.fmask
.swizzle_mode
) |
3013 S_028C74_RB_ALIGNED(meta
.rb_aligned
) |
3014 S_028C74_PIPE_ALIGNED(meta
.pipe_aligned
);
3016 radeon_set_context_reg_seq(cs
, R_028C60_CB_COLOR0_BASE
+ i
* 0x3C, 15);
3017 radeon_emit(cs
, cb_color_base
); /* CB_COLOR0_BASE */
3018 radeon_emit(cs
, S_028C64_BASE_256B(cb_color_base
>> 32)); /* CB_COLOR0_BASE_EXT */
3019 radeon_emit(cs
, cb
->cb_color_attrib2
); /* CB_COLOR0_ATTRIB2 */
3020 radeon_emit(cs
, cb
->cb_color_view
); /* CB_COLOR0_VIEW */
3021 radeon_emit(cs
, cb_color_info
); /* CB_COLOR0_INFO */
3022 radeon_emit(cs
, cb_color_attrib
); /* CB_COLOR0_ATTRIB */
3023 radeon_emit(cs
, cb
->cb_dcc_control
); /* CB_COLOR0_DCC_CONTROL */
3024 radeon_emit(cs
, cb_color_cmask
); /* CB_COLOR0_CMASK */
3025 radeon_emit(cs
, S_028C80_BASE_256B(cb_color_cmask
>> 32)); /* CB_COLOR0_CMASK_BASE_EXT */
3026 radeon_emit(cs
, cb_color_fmask
); /* CB_COLOR0_FMASK */
3027 radeon_emit(cs
, S_028C88_BASE_256B(cb_color_fmask
>> 32)); /* CB_COLOR0_FMASK_BASE_EXT */
3028 radeon_emit(cs
, tex
->color_clear_value
[0]); /* CB_COLOR0_CLEAR_WORD0 */
3029 radeon_emit(cs
, tex
->color_clear_value
[1]); /* CB_COLOR0_CLEAR_WORD1 */
3030 radeon_emit(cs
, cb_dcc_base
); /* CB_COLOR0_DCC_BASE */
3031 radeon_emit(cs
, S_028C98_BASE_256B(cb_dcc_base
>> 32)); /* CB_COLOR0_DCC_BASE_EXT */
3033 radeon_set_context_reg(cs
, R_0287A0_CB_MRT0_EPITCH
+ i
* 4,
3034 S_0287A0_EPITCH(tex
->surface
.u
.gfx9
.surf
.epitch
));
3036 /* Compute mutable surface parameters (GFX6-GFX8). */
3037 const struct legacy_surf_level
*level_info
=
3038 &tex
->surface
.u
.legacy
.level
[cb
->base
.u
.tex
.level
];
3039 unsigned pitch_tile_max
, slice_tile_max
, tile_mode_index
;
3040 unsigned cb_color_pitch
, cb_color_slice
, cb_color_fmask_slice
;
3042 cb_color_base
+= level_info
->offset
>> 8;
3043 /* Only macrotiled modes can set tile swizzle. */
3044 if (level_info
->mode
== RADEON_SURF_MODE_2D
)
3045 cb_color_base
|= tex
->surface
.tile_swizzle
;
3047 if (!tex
->surface
.fmask_offset
)
3048 cb_color_fmask
= cb_color_base
;
3049 if (cb
->base
.u
.tex
.level
> 0)
3050 cb_color_cmask
= cb_color_base
;
3052 cb_dcc_base
+= level_info
->dcc_offset
>> 8;
3054 pitch_tile_max
= level_info
->nblk_x
/ 8 - 1;
3055 slice_tile_max
= level_info
->nblk_x
* level_info
->nblk_y
/ 64 - 1;
3056 tile_mode_index
= si_tile_mode_index(tex
, cb
->base
.u
.tex
.level
, false);
3058 cb_color_attrib
|= S_028C74_TILE_MODE_INDEX(tile_mode_index
);
3059 cb_color_pitch
= S_028C64_TILE_MAX(pitch_tile_max
);
3060 cb_color_slice
= S_028C68_TILE_MAX(slice_tile_max
);
3062 if (tex
->surface
.fmask_offset
) {
3063 if (sctx
->chip_class
>= GFX7
)
3065 S_028C64_FMASK_TILE_MAX(tex
->surface
.u
.legacy
.fmask
.pitch_in_pixels
/ 8 - 1);
3067 S_028C74_FMASK_TILE_MODE_INDEX(tex
->surface
.u
.legacy
.fmask
.tiling_index
);
3068 cb_color_fmask_slice
= S_028C88_TILE_MAX(tex
->surface
.u
.legacy
.fmask
.slice_tile_max
);
3070 /* This must be set for fast clear to work without FMASK. */
3071 if (sctx
->chip_class
>= GFX7
)
3072 cb_color_pitch
|= S_028C64_FMASK_TILE_MAX(pitch_tile_max
);
3073 cb_color_attrib
|= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index
);
3074 cb_color_fmask_slice
= S_028C88_TILE_MAX(slice_tile_max
);
3077 radeon_set_context_reg_seq(cs
, R_028C60_CB_COLOR0_BASE
+ i
* 0x3C,
3078 sctx
->chip_class
>= GFX8
? 14 : 13);
3079 radeon_emit(cs
, cb_color_base
); /* CB_COLOR0_BASE */
3080 radeon_emit(cs
, cb_color_pitch
); /* CB_COLOR0_PITCH */
3081 radeon_emit(cs
, cb_color_slice
); /* CB_COLOR0_SLICE */
3082 radeon_emit(cs
, cb
->cb_color_view
); /* CB_COLOR0_VIEW */
3083 radeon_emit(cs
, cb_color_info
); /* CB_COLOR0_INFO */
3084 radeon_emit(cs
, cb_color_attrib
); /* CB_COLOR0_ATTRIB */
3085 radeon_emit(cs
, cb
->cb_dcc_control
); /* CB_COLOR0_DCC_CONTROL */
3086 radeon_emit(cs
, cb_color_cmask
); /* CB_COLOR0_CMASK */
3087 radeon_emit(cs
, tex
->surface
.u
.legacy
.cmask_slice_tile_max
); /* CB_COLOR0_CMASK_SLICE */
3088 radeon_emit(cs
, cb_color_fmask
); /* CB_COLOR0_FMASK */
3089 radeon_emit(cs
, cb_color_fmask_slice
); /* CB_COLOR0_FMASK_SLICE */
3090 radeon_emit(cs
, tex
->color_clear_value
[0]); /* CB_COLOR0_CLEAR_WORD0 */
3091 radeon_emit(cs
, tex
->color_clear_value
[1]); /* CB_COLOR0_CLEAR_WORD1 */
3093 if (sctx
->chip_class
>= GFX8
) /* R_028C94_CB_COLOR0_DCC_BASE */
3094 radeon_emit(cs
, cb_dcc_base
);
3098 if (sctx
->framebuffer
.dirty_cbufs
& (1 << i
))
3099 radeon_set_context_reg(cs
, R_028C70_CB_COLOR0_INFO
+ i
* 0x3C, 0);
3102 if (state
->zsbuf
&& sctx
->framebuffer
.dirty_zsbuf
) {
3103 struct si_surface
*zb
= (struct si_surface
*)state
->zsbuf
;
3104 struct si_texture
*tex
= (struct si_texture
*)zb
->base
.texture
;
3105 unsigned db_z_info
= zb
->db_z_info
;
3106 unsigned db_stencil_info
= zb
->db_stencil_info
;
3107 unsigned db_htile_surface
= zb
->db_htile_surface
;
3109 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, &tex
->buffer
, RADEON_USAGE_READWRITE
,
3110 zb
->base
.texture
->nr_samples
> 1 ? RADEON_PRIO_DEPTH_BUFFER_MSAA
3111 : RADEON_PRIO_DEPTH_BUFFER
);
3113 /* Set fields dependent on tc_compatile_htile. */
3114 if (sctx
->chip_class
>= GFX9
&&
3115 vi_tc_compat_htile_enabled(tex
, zb
->base
.u
.tex
.level
, PIPE_MASK_ZS
)) {
3116 unsigned max_zplanes
= 4;
3118 if (tex
->db_render_format
== PIPE_FORMAT_Z16_UNORM
&& tex
->buffer
.b
.b
.nr_samples
> 1)
3121 db_z_info
|= S_028038_DECOMPRESS_ON_N_ZPLANES(max_zplanes
+ 1);
3123 if (sctx
->chip_class
>= GFX10
) {
3124 db_z_info
|= S_028040_ITERATE_FLUSH(1);
3125 db_stencil_info
|= S_028044_ITERATE_FLUSH(!tex
->htile_stencil_disabled
);
3127 db_z_info
|= S_028038_ITERATE_FLUSH(1);
3128 db_stencil_info
|= S_02803C_ITERATE_FLUSH(1);
3132 if (sctx
->chip_class
>= GFX10
) {
3133 bool zs_big_page
= sctx
->chip_class
>= GFX10_3
&&
3134 sctx
->framebuffer
.zs_big_page
;
3136 radeon_set_context_reg(cs
, R_028014_DB_HTILE_DATA_BASE
, zb
->db_htile_data_base
);
3137 radeon_set_context_reg(cs
, R_02801C_DB_DEPTH_SIZE_XY
, zb
->db_depth_size
);
3139 radeon_set_context_reg_seq(cs
, R_02803C_DB_DEPTH_INFO
, 7);
3140 radeon_emit(cs
, S_02803C_RESOURCE_LEVEL(1)); /* DB_DEPTH_INFO */
3141 radeon_emit(cs
, db_z_info
| /* DB_Z_INFO */
3142 S_028038_ZRANGE_PRECISION(tex
->depth_clear_value
!= 0));
3143 radeon_emit(cs
, db_stencil_info
); /* DB_STENCIL_INFO */
3144 radeon_emit(cs
, zb
->db_depth_base
); /* DB_Z_READ_BASE */
3145 radeon_emit(cs
, zb
->db_stencil_base
); /* DB_STENCIL_READ_BASE */
3146 radeon_emit(cs
, zb
->db_depth_base
); /* DB_Z_WRITE_BASE */
3147 radeon_emit(cs
, zb
->db_stencil_base
); /* DB_STENCIL_WRITE_BASE */
3149 radeon_set_context_reg_seq(cs
, R_028068_DB_Z_READ_BASE_HI
, 6);
3150 radeon_emit(cs
, zb
->db_depth_base
>> 32); /* DB_Z_READ_BASE_HI */
3151 radeon_emit(cs
, zb
->db_stencil_base
>> 32); /* DB_STENCIL_READ_BASE_HI */
3152 radeon_emit(cs
, zb
->db_depth_base
>> 32); /* DB_Z_WRITE_BASE_HI */
3153 radeon_emit(cs
, zb
->db_stencil_base
>> 32); /* DB_STENCIL_WRITE_BASE_HI */
3154 radeon_emit(cs
, zb
->db_htile_data_base
>> 32); /* DB_HTILE_DATA_BASE_HI */
3155 radeon_emit(cs
, /* DB_RMI_L2_CACHE_CONTROL */
3156 S_02807C_Z_WR_POLICY(V_02807C_CACHE_STREAM
) |
3157 S_02807C_S_WR_POLICY(V_02807C_CACHE_STREAM
) |
3158 S_02807C_HTILE_WR_POLICY(meta_write_policy
) |
3159 S_02807C_ZPCPSD_WR_POLICY(V_02807C_CACHE_STREAM
) |
3160 S_02807C_Z_RD_POLICY(V_02807C_CACHE_NOA
) |
3161 S_02807C_S_RD_POLICY(V_02807C_CACHE_NOA
) |
3162 S_02807C_HTILE_RD_POLICY(meta_read_policy
) |
3163 S_02807C_Z_BIG_PAGE(zs_big_page
) |
3164 S_02807C_S_BIG_PAGE(zs_big_page
));
3165 } else if (sctx
->chip_class
== GFX9
) {
3166 radeon_set_context_reg_seq(cs
, R_028014_DB_HTILE_DATA_BASE
, 3);
3167 radeon_emit(cs
, zb
->db_htile_data_base
); /* DB_HTILE_DATA_BASE */
3169 S_028018_BASE_HI(zb
->db_htile_data_base
>> 32)); /* DB_HTILE_DATA_BASE_HI */
3170 radeon_emit(cs
, zb
->db_depth_size
); /* DB_DEPTH_SIZE */
3172 radeon_set_context_reg_seq(cs
, R_028038_DB_Z_INFO
, 10);
3173 radeon_emit(cs
, db_z_info
| /* DB_Z_INFO */
3174 S_028038_ZRANGE_PRECISION(tex
->depth_clear_value
!= 0));
3175 radeon_emit(cs
, db_stencil_info
); /* DB_STENCIL_INFO */
3176 radeon_emit(cs
, zb
->db_depth_base
); /* DB_Z_READ_BASE */
3177 radeon_emit(cs
, S_028044_BASE_HI(zb
->db_depth_base
>> 32)); /* DB_Z_READ_BASE_HI */
3178 radeon_emit(cs
, zb
->db_stencil_base
); /* DB_STENCIL_READ_BASE */
3179 radeon_emit(cs
, S_02804C_BASE_HI(zb
->db_stencil_base
>> 32)); /* DB_STENCIL_READ_BASE_HI */
3180 radeon_emit(cs
, zb
->db_depth_base
); /* DB_Z_WRITE_BASE */
3181 radeon_emit(cs
, S_028054_BASE_HI(zb
->db_depth_base
>> 32)); /* DB_Z_WRITE_BASE_HI */
3182 radeon_emit(cs
, zb
->db_stencil_base
); /* DB_STENCIL_WRITE_BASE */
3184 S_02805C_BASE_HI(zb
->db_stencil_base
>> 32)); /* DB_STENCIL_WRITE_BASE_HI */
3186 radeon_set_context_reg_seq(cs
, R_028068_DB_Z_INFO2
, 2);
3187 radeon_emit(cs
, zb
->db_z_info2
); /* DB_Z_INFO2 */
3188 radeon_emit(cs
, zb
->db_stencil_info2
); /* DB_STENCIL_INFO2 */
3191 /* Set fields dependent on tc_compatile_htile. */
3192 if (si_htile_enabled(tex
, zb
->base
.u
.tex
.level
, PIPE_MASK_ZS
)) {
3193 if (!tex
->surface
.has_stencil
&& !tex
->tc_compatible_htile
) {
3194 /* Use all of the htile_buffer for depth if there's no stencil.
3195 * This must not be set when TC-compatible HTILE is enabled
3198 db_stencil_info
|= S_028044_TILE_STENCIL_DISABLE(1);
3201 if (tex
->tc_compatible_htile
) {
3202 db_htile_surface
|= S_028ABC_TC_COMPATIBLE(1);
3204 /* 0 = full compression. N = only compress up to N-1 Z planes. */
3205 if (tex
->buffer
.b
.b
.nr_samples
<= 1)
3206 db_z_info
|= S_028040_DECOMPRESS_ON_N_ZPLANES(5);
3207 else if (tex
->buffer
.b
.b
.nr_samples
<= 4)
3208 db_z_info
|= S_028040_DECOMPRESS_ON_N_ZPLANES(3);
3210 db_z_info
|= S_028040_DECOMPRESS_ON_N_ZPLANES(2);
3214 radeon_set_context_reg(cs
, R_028014_DB_HTILE_DATA_BASE
, zb
->db_htile_data_base
);
3216 radeon_set_context_reg_seq(cs
, R_02803C_DB_DEPTH_INFO
, 9);
3217 radeon_emit(cs
, zb
->db_depth_info
| /* DB_DEPTH_INFO */
3218 S_02803C_ADDR5_SWIZZLE_MASK(!tex
->tc_compatible_htile
));
3219 radeon_emit(cs
, db_z_info
| /* DB_Z_INFO */
3220 S_028040_ZRANGE_PRECISION(tex
->depth_clear_value
!= 0));
3221 radeon_emit(cs
, db_stencil_info
); /* DB_STENCIL_INFO */
3222 radeon_emit(cs
, zb
->db_depth_base
); /* DB_Z_READ_BASE */
3223 radeon_emit(cs
, zb
->db_stencil_base
); /* DB_STENCIL_READ_BASE */
3224 radeon_emit(cs
, zb
->db_depth_base
); /* DB_Z_WRITE_BASE */
3225 radeon_emit(cs
, zb
->db_stencil_base
); /* DB_STENCIL_WRITE_BASE */
3226 radeon_emit(cs
, zb
->db_depth_size
); /* DB_DEPTH_SIZE */
3227 radeon_emit(cs
, zb
->db_depth_slice
); /* DB_DEPTH_SLICE */
3230 radeon_set_context_reg_seq(cs
, R_028028_DB_STENCIL_CLEAR
, 2);
3231 radeon_emit(cs
, tex
->stencil_clear_value
); /* R_028028_DB_STENCIL_CLEAR */
3232 radeon_emit(cs
, fui(tex
->depth_clear_value
)); /* R_02802C_DB_DEPTH_CLEAR */
3234 radeon_set_context_reg(cs
, R_028008_DB_DEPTH_VIEW
, zb
->db_depth_view
);
3235 radeon_set_context_reg(cs
, R_028ABC_DB_HTILE_SURFACE
, db_htile_surface
);
3236 } else if (sctx
->framebuffer
.dirty_zsbuf
) {
3237 if (sctx
->chip_class
== GFX9
)
3238 radeon_set_context_reg_seq(cs
, R_028038_DB_Z_INFO
, 2);
3240 radeon_set_context_reg_seq(cs
, R_028040_DB_Z_INFO
, 2);
3242 radeon_emit(cs
, S_028040_FORMAT(V_028040_Z_INVALID
)); /* DB_Z_INFO */
3243 radeon_emit(cs
, S_028044_FORMAT(V_028044_STENCIL_INVALID
)); /* DB_STENCIL_INFO */
3246 /* Framebuffer dimensions. */
3247 /* PA_SC_WINDOW_SCISSOR_TL is set in si_init_cs_preamble_state */
3248 radeon_set_context_reg(cs
, R_028208_PA_SC_WINDOW_SCISSOR_BR
,
3249 S_028208_BR_X(state
->width
) | S_028208_BR_Y(state
->height
));
3252 bool color_big_page
= sctx
->chip_class
>= GFX10_3
&&
3253 sctx
->framebuffer
.color_big_page
;
3254 radeon_set_context_reg(cs
, R_028410_CB_RMI_GL2_CACHE_CONTROL
,
3255 S_028410_CMASK_WR_POLICY(meta_write_policy
) |
3256 S_028410_FMASK_WR_POLICY(meta_write_policy
) |
3257 S_028410_DCC_WR_POLICY(meta_write_policy
) |
3258 S_028410_COLOR_WR_POLICY(V_028410_CACHE_STREAM
) |
3259 S_028410_CMASK_RD_POLICY(meta_read_policy
) |
3260 S_028410_FMASK_RD_POLICY(meta_read_policy
) |
3261 S_028410_DCC_RD_POLICY(meta_read_policy
) |
3262 S_028410_COLOR_RD_POLICY(V_028410_CACHE_NOA
) |
3263 S_028410_FMASK_BIG_PAGE(color_big_page
) |
3264 S_028410_COLOR_BIG_PAGE(color_big_page
));
3267 if (sctx
->screen
->dfsm_allowed
) {
3268 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
3269 radeon_emit(cs
, EVENT_TYPE(V_028A90_BREAK_BATCH
) | EVENT_INDEX(0));
3272 sctx
->framebuffer
.dirty_cbufs
= 0;
3273 sctx
->framebuffer
.dirty_zsbuf
= false;
3276 static void si_emit_msaa_sample_locs(struct si_context
*sctx
)
3278 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
3279 struct si_state_rasterizer
*rs
= sctx
->queued
.named
.rasterizer
;
3280 unsigned nr_samples
= sctx
->framebuffer
.nr_samples
;
3281 bool has_msaa_sample_loc_bug
= sctx
->screen
->info
.has_msaa_sample_loc_bug
;
3283 /* Smoothing (only possible with nr_samples == 1) uses the same
3284 * sample locations as the MSAA it simulates.
3286 if (nr_samples
<= 1 && sctx
->smoothing_enabled
)
3287 nr_samples
= SI_NUM_SMOOTH_AA_SAMPLES
;
3289 /* On Polaris, the small primitive filter uses the sample locations
3290 * even when MSAA is off, so we need to make sure they're set to 0.
3292 * GFX10 uses sample locations unconditionally, so they always need
3295 if ((nr_samples
>= 2 || has_msaa_sample_loc_bug
|| sctx
->chip_class
>= GFX10
) &&
3296 nr_samples
!= sctx
->sample_locs_num_samples
) {
3297 sctx
->sample_locs_num_samples
= nr_samples
;
3298 si_emit_sample_locations(cs
, nr_samples
);
3301 if (sctx
->family
>= CHIP_POLARIS10
) {
3302 unsigned small_prim_filter_cntl
=
3303 S_028830_SMALL_PRIM_FILTER_ENABLE(1) |
3305 S_028830_LINE_FILTER_DISABLE(sctx
->family
<= CHIP_POLARIS12
);
3307 /* The alternative of setting sample locations to 0 would
3308 * require a DB flush to avoid Z errors, see
3309 * https://bugs.freedesktop.org/show_bug.cgi?id=96908
3311 if (has_msaa_sample_loc_bug
&& sctx
->framebuffer
.nr_samples
> 1 && !rs
->multisample_enable
)
3312 small_prim_filter_cntl
&= C_028830_SMALL_PRIM_FILTER_ENABLE
;
3314 radeon_opt_set_context_reg(sctx
, R_028830_PA_SU_SMALL_PRIM_FILTER_CNTL
,
3315 SI_TRACKED_PA_SU_SMALL_PRIM_FILTER_CNTL
, small_prim_filter_cntl
);
3318 /* The exclusion bits can be set to improve rasterization efficiency
3319 * if no sample lies on the pixel boundary (-8 sample offset).
3321 bool exclusion
= sctx
->chip_class
>= GFX7
&& (!rs
->multisample_enable
|| nr_samples
!= 16);
3322 radeon_opt_set_context_reg(
3323 sctx
, R_02882C_PA_SU_PRIM_FILTER_CNTL
, SI_TRACKED_PA_SU_PRIM_FILTER_CNTL
,
3324 S_02882C_XMAX_RIGHT_EXCLUSION(exclusion
) | S_02882C_YMAX_BOTTOM_EXCLUSION(exclusion
));
3327 static bool si_out_of_order_rasterization(struct si_context
*sctx
)
3329 struct si_state_blend
*blend
= sctx
->queued
.named
.blend
;
3330 struct si_state_dsa
*dsa
= sctx
->queued
.named
.dsa
;
3332 if (!sctx
->screen
->has_out_of_order_rast
)
3335 unsigned colormask
= sctx
->framebuffer
.colorbuf_enabled_4bit
;
3337 colormask
&= blend
->cb_target_enabled_4bit
;
3339 /* Conservative: No logic op. */
3340 if (colormask
&& blend
->logicop_enable
)
3343 struct si_dsa_order_invariance dsa_order_invariant
= {.zs
= true,
3345 .pass_last
= false};
3347 if (sctx
->framebuffer
.state
.zsbuf
) {
3348 struct si_texture
*zstex
= (struct si_texture
*)sctx
->framebuffer
.state
.zsbuf
->texture
;
3349 bool has_stencil
= zstex
->surface
.has_stencil
;
3350 dsa_order_invariant
= dsa
->order_invariance
[has_stencil
];
3351 if (!dsa_order_invariant
.zs
)
3354 /* The set of PS invocations is always order invariant,
3355 * except when early Z/S tests are requested. */
3356 if (sctx
->ps_shader
.cso
&& sctx
->ps_shader
.cso
->info
.writes_memory
&&
3357 sctx
->ps_shader
.cso
->info
.base
.fs
.early_fragment_tests
&&
3358 !dsa_order_invariant
.pass_set
)
3361 if (sctx
->num_perfect_occlusion_queries
!= 0 && !dsa_order_invariant
.pass_set
)
3368 unsigned blendmask
= colormask
& blend
->blend_enable_4bit
;
3371 /* Only commutative blending. */
3372 if (blendmask
& ~blend
->commutative_4bit
)
3375 if (!dsa_order_invariant
.pass_set
)
3379 if (colormask
& ~blendmask
) {
3380 if (!dsa_order_invariant
.pass_last
)
3387 static void si_emit_msaa_config(struct si_context
*sctx
)
3389 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
3390 unsigned num_tile_pipes
= sctx
->screen
->info
.num_tile_pipes
;
3391 /* 33% faster rendering to linear color buffers */
3392 bool dst_is_linear
= sctx
->framebuffer
.any_dst_linear
;
3393 bool out_of_order_rast
= si_out_of_order_rasterization(sctx
);
3394 unsigned sc_mode_cntl_1
=
3395 S_028A4C_WALK_SIZE(dst_is_linear
) | S_028A4C_WALK_FENCE_ENABLE(!dst_is_linear
) |
3396 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes
== 2 ? 2 : 3) |
3397 S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(out_of_order_rast
) |
3398 S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7) |
3400 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) | S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
3401 S_028A4C_TILE_WALK_ORDER_ENABLE(1) | S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
3402 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) | S_028A4C_FORCE_EOV_REZ_ENABLE(1);
3403 unsigned db_eqaa
= S_028804_HIGH_QUALITY_INTERSECTIONS(1) | S_028804_INCOHERENT_EQAA_READS(1) |
3404 S_028804_INTERPOLATE_COMP_Z(1) | S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
3405 unsigned coverage_samples
, color_samples
, z_samples
;
3406 struct si_state_rasterizer
*rs
= sctx
->queued
.named
.rasterizer
;
3408 /* S: Coverage samples (up to 16x):
3409 * - Scan conversion samples (PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES)
3410 * - CB FMASK samples (CB_COLORi_ATTRIB.NUM_SAMPLES)
3412 * Z: Z/S samples (up to 8x, must be <= coverage samples and >= color samples):
3413 * - Value seen by DB (DB_Z_INFO.NUM_SAMPLES)
3414 * - Value seen by CB, must be correct even if Z/S is unbound (DB_EQAA.MAX_ANCHOR_SAMPLES)
3415 * # Missing samples are derived from Z planes if Z is compressed (up to 16x quality), or
3416 * # from the closest defined sample if Z is uncompressed (same quality as the number of
3419 * F: Color samples (up to 8x, must be <= coverage samples):
3420 * - CB color samples (CB_COLORi_ATTRIB.NUM_FRAGMENTS)
3421 * - PS iter samples (DB_EQAA.PS_ITER_SAMPLES)
3423 * Can be anything between coverage and color samples:
3424 * - SampleMaskIn samples (PA_SC_AA_CONFIG.MSAA_EXPOSED_SAMPLES)
3425 * - SampleMaskOut samples (DB_EQAA.MASK_EXPORT_NUM_SAMPLES)
3426 * - Alpha-to-coverage samples (DB_EQAA.ALPHA_TO_MASK_NUM_SAMPLES)
3427 * - Occlusion query samples (DB_COUNT_CONTROL.SAMPLE_RATE)
3428 * # All are currently set the same as coverage samples.
3430 * If color samples < coverage samples, FMASK has a higher bpp to store an "unknown"
3431 * flag for undefined color samples. A shader-based resolve must handle unknowns
3432 * or mask them out with AND. Unknowns can also be guessed from neighbors via
3433 * an edge-detect shader-based resolve, which is required to make "color samples = 1"
3434 * useful. The CB resolve always drops unknowns.
3436 * Sensible AA configurations:
3437 * EQAA 16s 8z 8f - might look the same as 16x MSAA if Z is compressed
3438 * EQAA 16s 8z 4f - might look the same as 16x MSAA if Z is compressed
3439 * EQAA 16s 4z 4f - might look the same as 16x MSAA if Z is compressed
3440 * EQAA 8s 8z 8f = 8x MSAA
3441 * EQAA 8s 8z 4f - might look the same as 8x MSAA
3442 * EQAA 8s 8z 2f - might look the same as 8x MSAA with low-density geometry
3443 * EQAA 8s 4z 4f - might look the same as 8x MSAA if Z is compressed
3444 * EQAA 8s 4z 2f - might look the same as 8x MSAA with low-density geometry if Z is compressed
3445 * EQAA 4s 4z 4f = 4x MSAA
3446 * EQAA 4s 4z 2f - might look the same as 4x MSAA with low-density geometry
3447 * EQAA 2s 2z 2f = 2x MSAA
3449 if (sctx
->framebuffer
.nr_samples
> 1 && rs
->multisample_enable
) {
3450 coverage_samples
= sctx
->framebuffer
.nr_samples
;
3451 color_samples
= sctx
->framebuffer
.nr_color_samples
;
3453 if (sctx
->framebuffer
.state
.zsbuf
) {
3454 z_samples
= sctx
->framebuffer
.state
.zsbuf
->texture
->nr_samples
;
3455 z_samples
= MAX2(1, z_samples
);
3457 z_samples
= coverage_samples
;
3459 } else if (sctx
->smoothing_enabled
) {
3460 coverage_samples
= color_samples
= z_samples
= SI_NUM_SMOOTH_AA_SAMPLES
;
3462 coverage_samples
= color_samples
= z_samples
= 1;
3465 /* Required by OpenGL line rasterization.
3467 * TODO: We should also enable perpendicular endcaps for AA lines,
3468 * but that requires implementing line stippling in the pixel
3469 * shader. SC can only do line stippling with axis-aligned
3472 unsigned sc_line_cntl
= S_028BDC_DX10_DIAMOND_TEST_ENA(1);
3473 unsigned sc_aa_config
= 0;
3475 if (coverage_samples
> 1) {
3476 /* distance from the pixel center, indexed by log2(nr_samples) */
3477 static unsigned max_dist
[] = {
3484 unsigned log_samples
= util_logbase2(coverage_samples
);
3485 unsigned log_z_samples
= util_logbase2(z_samples
);
3486 unsigned ps_iter_samples
= si_get_ps_iter_samples(sctx
);
3487 unsigned log_ps_iter_samples
= util_logbase2(ps_iter_samples
);
3489 sc_line_cntl
|= S_028BDC_EXPAND_LINE_WIDTH(1);
3490 sc_aa_config
= S_028BE0_MSAA_NUM_SAMPLES(log_samples
) |
3491 S_028BE0_MAX_SAMPLE_DIST(max_dist
[log_samples
]) |
3492 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples
) |
3493 S_028BE0_COVERED_CENTROID_IS_CENTER(sctx
->chip_class
>= GFX10_3
);
3495 if (sctx
->framebuffer
.nr_samples
> 1) {
3496 db_eqaa
|= S_028804_MAX_ANCHOR_SAMPLES(log_z_samples
) |
3497 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples
) |
3498 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples
) |
3499 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples
);
3500 sc_mode_cntl_1
|= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples
> 1);
3501 } else if (sctx
->smoothing_enabled
) {
3502 db_eqaa
|= S_028804_OVERRASTERIZATION_AMOUNT(log_samples
);
3506 unsigned initial_cdw
= cs
->current
.cdw
;
3508 /* R_028BDC_PA_SC_LINE_CNTL, R_028BE0_PA_SC_AA_CONFIG */
3509 radeon_opt_set_context_reg2(sctx
, R_028BDC_PA_SC_LINE_CNTL
, SI_TRACKED_PA_SC_LINE_CNTL
,
3510 sc_line_cntl
, sc_aa_config
);
3511 /* R_028804_DB_EQAA */
3512 radeon_opt_set_context_reg(sctx
, R_028804_DB_EQAA
, SI_TRACKED_DB_EQAA
, db_eqaa
);
3513 /* R_028A4C_PA_SC_MODE_CNTL_1 */
3514 radeon_opt_set_context_reg(sctx
, R_028A4C_PA_SC_MODE_CNTL_1
, SI_TRACKED_PA_SC_MODE_CNTL_1
,
3517 if (initial_cdw
!= cs
->current
.cdw
) {
3518 sctx
->context_roll
= true;
3520 /* GFX9: Flush DFSM when the AA mode changes. */
3521 if (sctx
->screen
->dfsm_allowed
) {
3522 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
3523 radeon_emit(cs
, EVENT_TYPE(V_028A90_FLUSH_DFSM
) | EVENT_INDEX(0));
3528 void si_update_ps_iter_samples(struct si_context
*sctx
)
3530 if (sctx
->framebuffer
.nr_samples
> 1)
3531 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.msaa_config
);
3532 if (sctx
->screen
->dpbb_allowed
)
3533 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.dpbb_state
);
3536 static void si_set_min_samples(struct pipe_context
*ctx
, unsigned min_samples
)
3538 struct si_context
*sctx
= (struct si_context
*)ctx
;
3540 /* The hardware can only do sample shading with 2^n samples. */
3541 min_samples
= util_next_power_of_two(min_samples
);
3543 if (sctx
->ps_iter_samples
== min_samples
)
3546 sctx
->ps_iter_samples
= min_samples
;
3547 sctx
->do_update_shaders
= true;
3549 si_update_ps_iter_samples(sctx
);
3557 * Build the sampler view descriptor for a buffer texture.
3558 * @param state 256-bit descriptor; only the high 128 bits are filled in
3560 void si_make_buffer_descriptor(struct si_screen
*screen
, struct si_resource
*buf
,
3561 enum pipe_format format
, unsigned offset
, unsigned size
,
3564 const struct util_format_description
*desc
;
3566 unsigned num_records
;
3568 desc
= util_format_description(format
);
3569 stride
= desc
->block
.bits
/ 8;
3571 num_records
= size
/ stride
;
3572 num_records
= MIN2(num_records
, (buf
->b
.b
.width0
- offset
) / stride
);
3574 /* The NUM_RECORDS field has a different meaning depending on the chip,
3575 * instruction type, STRIDE, and SWIZZLE_ENABLE.
3578 * - If STRIDE == 0, it's in byte units.
3579 * - If STRIDE != 0, it's in units of STRIDE, used with inst.IDXEN.
3582 * - For SMEM and STRIDE == 0, it's in byte units.
3583 * - For SMEM and STRIDE != 0, it's in units of STRIDE.
3584 * - For VMEM and STRIDE == 0 or SWIZZLE_ENABLE == 0, it's in byte units.
3585 * - For VMEM and STRIDE != 0 and SWIZZLE_ENABLE == 1, it's in units of STRIDE.
3586 * NOTE: There is incompatibility between VMEM and SMEM opcodes due to SWIZZLE_-
3587 * ENABLE. The workaround is to set STRIDE = 0 if SWIZZLE_ENABLE == 0 when
3588 * using SMEM. This can be done in the shader by clearing STRIDE with s_and.
3589 * That way the same descriptor can be used by both SMEM and VMEM.
3592 * - For SMEM and STRIDE == 0, it's in byte units.
3593 * - For SMEM and STRIDE != 0, it's in units of STRIDE.
3594 * - For VMEM and inst.IDXEN == 0 or STRIDE == 0, it's in byte units.
3595 * - For VMEM and inst.IDXEN == 1 and STRIDE != 0, it's in units of STRIDE.
3597 if (screen
->info
.chip_class
== GFX8
)
3598 num_records
*= stride
;
3601 state
[5] = S_008F04_STRIDE(stride
);
3602 state
[6] = num_records
;
3603 state
[7] = S_008F0C_DST_SEL_X(si_map_swizzle(desc
->swizzle
[0])) |
3604 S_008F0C_DST_SEL_Y(si_map_swizzle(desc
->swizzle
[1])) |
3605 S_008F0C_DST_SEL_Z(si_map_swizzle(desc
->swizzle
[2])) |
3606 S_008F0C_DST_SEL_W(si_map_swizzle(desc
->swizzle
[3]));
3608 if (screen
->info
.chip_class
>= GFX10
) {
3609 const struct gfx10_format
*fmt
= &gfx10_format_table
[format
];
3611 /* OOB_SELECT chooses the out-of-bounds check:
3612 * - 0: (index >= NUM_RECORDS) || (offset >= STRIDE)
3613 * - 1: index >= NUM_RECORDS
3614 * - 2: NUM_RECORDS == 0
3615 * - 3: if SWIZZLE_ENABLE == 0: offset >= NUM_RECORDS
3616 * else: swizzle_address >= NUM_RECORDS
3618 state
[7] |= S_008F0C_FORMAT(fmt
->img_format
) |
3619 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_STRUCTURED_WITH_OFFSET
) |
3620 S_008F0C_RESOURCE_LEVEL(1);
3623 unsigned num_format
, data_format
;
3625 first_non_void
= util_format_get_first_non_void_channel(format
);
3626 num_format
= si_translate_buffer_numformat(&screen
->b
, desc
, first_non_void
);
3627 data_format
= si_translate_buffer_dataformat(&screen
->b
, desc
, first_non_void
);
3629 state
[7] |= S_008F0C_NUM_FORMAT(num_format
) | S_008F0C_DATA_FORMAT(data_format
);
3633 static unsigned gfx9_border_color_swizzle(const unsigned char swizzle
[4])
3635 unsigned bc_swizzle
= V_008F20_BC_SWIZZLE_XYZW
;
3637 if (swizzle
[3] == PIPE_SWIZZLE_X
) {
3638 /* For the pre-defined border color values (white, opaque
3639 * black, transparent black), the only thing that matters is
3640 * that the alpha channel winds up in the correct place
3641 * (because the RGB channels are all the same) so either of
3642 * these enumerations will work.
3644 if (swizzle
[2] == PIPE_SWIZZLE_Y
)
3645 bc_swizzle
= V_008F20_BC_SWIZZLE_WZYX
;
3647 bc_swizzle
= V_008F20_BC_SWIZZLE_WXYZ
;
3648 } else if (swizzle
[0] == PIPE_SWIZZLE_X
) {
3649 if (swizzle
[1] == PIPE_SWIZZLE_Y
)
3650 bc_swizzle
= V_008F20_BC_SWIZZLE_XYZW
;
3652 bc_swizzle
= V_008F20_BC_SWIZZLE_XWYZ
;
3653 } else if (swizzle
[1] == PIPE_SWIZZLE_X
) {
3654 bc_swizzle
= V_008F20_BC_SWIZZLE_YXWZ
;
3655 } else if (swizzle
[2] == PIPE_SWIZZLE_X
) {
3656 bc_swizzle
= V_008F20_BC_SWIZZLE_ZYXW
;
3663 * Build the sampler view descriptor for a texture.
3665 static void gfx10_make_texture_descriptor(
3666 struct si_screen
*screen
, struct si_texture
*tex
, bool sampler
, enum pipe_texture_target target
,
3667 enum pipe_format pipe_format
, const unsigned char state_swizzle
[4], unsigned first_level
,
3668 unsigned last_level
, unsigned first_layer
, unsigned last_layer
, unsigned width
, unsigned height
,
3669 unsigned depth
, uint32_t *state
, uint32_t *fmask_state
)
3671 struct pipe_resource
*res
= &tex
->buffer
.b
.b
;
3672 const struct util_format_description
*desc
;
3673 unsigned img_format
;
3674 unsigned char swizzle
[4];
3678 desc
= util_format_description(pipe_format
);
3679 img_format
= gfx10_format_table
[pipe_format
].img_format
;
3681 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
3682 const unsigned char swizzle_xxxx
[4] = {0, 0, 0, 0};
3683 const unsigned char swizzle_yyyy
[4] = {1, 1, 1, 1};
3684 const unsigned char swizzle_wwww
[4] = {3, 3, 3, 3};
3685 bool is_stencil
= false;
3687 switch (pipe_format
) {
3688 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
3689 case PIPE_FORMAT_X32_S8X24_UINT
:
3690 case PIPE_FORMAT_X8Z24_UNORM
:
3691 util_format_compose_swizzles(swizzle_yyyy
, state_swizzle
, swizzle
);
3694 case PIPE_FORMAT_X24S8_UINT
:
3696 * X24S8 is implemented as an 8_8_8_8 data format, to
3697 * fix texture gathers. This affects at least
3698 * GL45-CTS.texture_cube_map_array.sampling on GFX8.
3700 util_format_compose_swizzles(swizzle_wwww
, state_swizzle
, swizzle
);
3704 util_format_compose_swizzles(swizzle_xxxx
, state_swizzle
, swizzle
);
3705 is_stencil
= pipe_format
== PIPE_FORMAT_S8_UINT
;
3708 if (tex
->upgraded_depth
&& !is_stencil
) {
3709 assert(img_format
== V_008F0C_IMG_FORMAT_32_FLOAT
);
3710 img_format
= V_008F0C_IMG_FORMAT_32_FLOAT_CLAMP
;
3713 util_format_compose_swizzles(desc
->swizzle
, state_swizzle
, swizzle
);
3716 if (!sampler
&& (res
->target
== PIPE_TEXTURE_CUBE
|| res
->target
== PIPE_TEXTURE_CUBE_ARRAY
)) {
3717 /* For the purpose of shader images, treat cube maps as 2D
3720 type
= V_008F1C_SQ_RSRC_IMG_2D_ARRAY
;
3722 type
= si_tex_dim(screen
, tex
, target
, res
->nr_samples
);
3725 if (type
== V_008F1C_SQ_RSRC_IMG_1D_ARRAY
) {
3727 depth
= res
->array_size
;
3728 } else if (type
== V_008F1C_SQ_RSRC_IMG_2D_ARRAY
|| type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
) {
3729 if (sampler
|| res
->target
!= PIPE_TEXTURE_3D
)
3730 depth
= res
->array_size
;
3731 } else if (type
== V_008F1C_SQ_RSRC_IMG_CUBE
)
3732 depth
= res
->array_size
/ 6;
3735 state
[1] = S_00A004_FORMAT(img_format
) | S_00A004_WIDTH_LO(width
- 1);
3736 state
[2] = S_00A008_WIDTH_HI((width
- 1) >> 2) | S_00A008_HEIGHT(height
- 1) |
3737 S_00A008_RESOURCE_LEVEL(1);
3739 S_00A00C_DST_SEL_X(si_map_swizzle(swizzle
[0])) |
3740 S_00A00C_DST_SEL_Y(si_map_swizzle(swizzle
[1])) |
3741 S_00A00C_DST_SEL_Z(si_map_swizzle(swizzle
[2])) |
3742 S_00A00C_DST_SEL_W(si_map_swizzle(swizzle
[3])) |
3743 S_00A00C_BASE_LEVEL(res
->nr_samples
> 1 ? 0 : first_level
) |
3744 S_00A00C_LAST_LEVEL(res
->nr_samples
> 1 ? util_logbase2(res
->nr_samples
) : last_level
) |
3745 S_00A00C_BC_SWIZZLE(gfx9_border_color_swizzle(desc
->swizzle
)) | S_00A00C_TYPE(type
);
3746 /* Depth is the the last accessible layer on gfx9+. The hw doesn't need
3747 * to know the total number of layers.
3750 S_00A010_DEPTH((type
== V_008F1C_SQ_RSRC_IMG_3D
&& sampler
) ? depth
- 1 : last_layer
) |
3751 S_00A010_BASE_ARRAY(first_layer
);
3752 state
[5] = S_00A014_ARRAY_PITCH(!!(type
== V_008F1C_SQ_RSRC_IMG_3D
&& !sampler
)) |
3753 S_00A014_MAX_MIP(res
->nr_samples
> 1 ? util_logbase2(res
->nr_samples
)
3754 : tex
->buffer
.b
.b
.last_level
) |
3755 S_00A014_PERF_MOD(4) |
3756 S_00A014_BIG_PAGE(screen
->info
.chip_class
>= GFX10_3
&&
3757 tex
->buffer
.bo_alignment
% (64 * 1024) == 0);
3761 if (vi_dcc_enabled(tex
, first_level
)) {
3762 state
[6] |= S_00A018_MAX_UNCOMPRESSED_BLOCK_SIZE(V_028C78_MAX_BLOCK_SIZE_256B
) |
3763 S_00A018_MAX_COMPRESSED_BLOCK_SIZE(tex
->surface
.u
.gfx9
.dcc
.max_compressed_block_size
) |
3764 S_00A018_ALPHA_IS_ON_MSB(vi_alpha_is_on_msb(screen
, pipe_format
));
3767 /* Initialize the sampler view for FMASK. */
3768 if (tex
->surface
.fmask_offset
) {
3771 va
= tex
->buffer
.gpu_address
+ tex
->surface
.fmask_offset
;
3773 #define FMASK(s, f) (((unsigned)(MAX2(1, s)) * 16) + (MAX2(1, f)))
3774 switch (FMASK(res
->nr_samples
, res
->nr_storage_samples
)) {
3776 format
= V_008F0C_IMG_FORMAT_FMASK8_S2_F1
;
3779 format
= V_008F0C_IMG_FORMAT_FMASK8_S2_F2
;
3782 format
= V_008F0C_IMG_FORMAT_FMASK8_S4_F1
;
3785 format
= V_008F0C_IMG_FORMAT_FMASK8_S4_F2
;
3788 format
= V_008F0C_IMG_FORMAT_FMASK8_S4_F4
;
3791 format
= V_008F0C_IMG_FORMAT_FMASK8_S8_F1
;
3794 format
= V_008F0C_IMG_FORMAT_FMASK16_S8_F2
;
3797 format
= V_008F0C_IMG_FORMAT_FMASK32_S8_F4
;
3800 format
= V_008F0C_IMG_FORMAT_FMASK32_S8_F8
;
3803 format
= V_008F0C_IMG_FORMAT_FMASK16_S16_F1
;
3806 format
= V_008F0C_IMG_FORMAT_FMASK32_S16_F2
;
3809 format
= V_008F0C_IMG_FORMAT_FMASK64_S16_F4
;
3812 format
= V_008F0C_IMG_FORMAT_FMASK64_S16_F8
;
3815 unreachable("invalid nr_samples");
3818 fmask_state
[0] = (va
>> 8) | tex
->surface
.fmask_tile_swizzle
;
3819 fmask_state
[1] = S_00A004_BASE_ADDRESS_HI(va
>> 40) | S_00A004_FORMAT(format
) |
3820 S_00A004_WIDTH_LO(width
- 1);
3821 fmask_state
[2] = S_00A008_WIDTH_HI((width
- 1) >> 2) | S_00A008_HEIGHT(height
- 1) |
3822 S_00A008_RESOURCE_LEVEL(1);
3824 S_00A00C_DST_SEL_X(V_008F1C_SQ_SEL_X
) | S_00A00C_DST_SEL_Y(V_008F1C_SQ_SEL_X
) |
3825 S_00A00C_DST_SEL_Z(V_008F1C_SQ_SEL_X
) | S_00A00C_DST_SEL_W(V_008F1C_SQ_SEL_X
) |
3826 S_00A00C_SW_MODE(tex
->surface
.u
.gfx9
.fmask
.swizzle_mode
) |
3827 S_00A00C_TYPE(si_tex_dim(screen
, tex
, target
, 0));
3828 fmask_state
[4] = S_00A010_DEPTH(last_layer
) | S_00A010_BASE_ARRAY(first_layer
);
3830 fmask_state
[6] = S_00A018_META_PIPE_ALIGNED(1);
3836 * Build the sampler view descriptor for a texture (SI-GFX9).
3838 static void si_make_texture_descriptor(struct si_screen
*screen
, struct si_texture
*tex
,
3839 bool sampler
, enum pipe_texture_target target
,
3840 enum pipe_format pipe_format
,
3841 const unsigned char state_swizzle
[4], unsigned first_level
,
3842 unsigned last_level
, unsigned first_layer
,
3843 unsigned last_layer
, unsigned width
, unsigned height
,
3844 unsigned depth
, uint32_t *state
, uint32_t *fmask_state
)
3846 struct pipe_resource
*res
= &tex
->buffer
.b
.b
;
3847 const struct util_format_description
*desc
;
3848 unsigned char swizzle
[4];
3850 unsigned num_format
, data_format
, type
, num_samples
;
3853 desc
= util_format_description(pipe_format
);
3855 num_samples
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? MAX2(1, res
->nr_samples
)
3856 : MAX2(1, res
->nr_storage_samples
);
3858 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
3859 const unsigned char swizzle_xxxx
[4] = {0, 0, 0, 0};
3860 const unsigned char swizzle_yyyy
[4] = {1, 1, 1, 1};
3861 const unsigned char swizzle_wwww
[4] = {3, 3, 3, 3};
3863 switch (pipe_format
) {
3864 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
3865 case PIPE_FORMAT_X32_S8X24_UINT
:
3866 case PIPE_FORMAT_X8Z24_UNORM
:
3867 util_format_compose_swizzles(swizzle_yyyy
, state_swizzle
, swizzle
);
3869 case PIPE_FORMAT_X24S8_UINT
:
3871 * X24S8 is implemented as an 8_8_8_8 data format, to
3872 * fix texture gathers. This affects at least
3873 * GL45-CTS.texture_cube_map_array.sampling on GFX8.
3875 if (screen
->info
.chip_class
<= GFX8
)
3876 util_format_compose_swizzles(swizzle_wwww
, state_swizzle
, swizzle
);
3878 util_format_compose_swizzles(swizzle_yyyy
, state_swizzle
, swizzle
);
3881 util_format_compose_swizzles(swizzle_xxxx
, state_swizzle
, swizzle
);
3884 util_format_compose_swizzles(desc
->swizzle
, state_swizzle
, swizzle
);
3887 first_non_void
= util_format_get_first_non_void_channel(pipe_format
);
3889 switch (pipe_format
) {
3890 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
3891 num_format
= V_008F14_IMG_NUM_FORMAT_UNORM
;
3894 if (first_non_void
< 0) {
3895 if (util_format_is_compressed(pipe_format
)) {
3896 switch (pipe_format
) {
3897 case PIPE_FORMAT_DXT1_SRGB
:
3898 case PIPE_FORMAT_DXT1_SRGBA
:
3899 case PIPE_FORMAT_DXT3_SRGBA
:
3900 case PIPE_FORMAT_DXT5_SRGBA
:
3901 case PIPE_FORMAT_BPTC_SRGBA
:
3902 case PIPE_FORMAT_ETC2_SRGB8
:
3903 case PIPE_FORMAT_ETC2_SRGB8A1
:
3904 case PIPE_FORMAT_ETC2_SRGBA8
:
3905 num_format
= V_008F14_IMG_NUM_FORMAT_SRGB
;
3907 case PIPE_FORMAT_RGTC1_SNORM
:
3908 case PIPE_FORMAT_LATC1_SNORM
:
3909 case PIPE_FORMAT_RGTC2_SNORM
:
3910 case PIPE_FORMAT_LATC2_SNORM
:
3911 case PIPE_FORMAT_ETC2_R11_SNORM
:
3912 case PIPE_FORMAT_ETC2_RG11_SNORM
:
3913 /* implies float, so use SNORM/UNORM to determine
3914 whether data is signed or not */
3915 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
3916 num_format
= V_008F14_IMG_NUM_FORMAT_SNORM
;
3919 num_format
= V_008F14_IMG_NUM_FORMAT_UNORM
;
3922 } else if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
3923 num_format
= V_008F14_IMG_NUM_FORMAT_UNORM
;
3925 num_format
= V_008F14_IMG_NUM_FORMAT_FLOAT
;
3927 } else if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
3928 num_format
= V_008F14_IMG_NUM_FORMAT_SRGB
;
3930 num_format
= V_008F14_IMG_NUM_FORMAT_UNORM
;
3932 switch (desc
->channel
[first_non_void
].type
) {
3933 case UTIL_FORMAT_TYPE_FLOAT
:
3934 num_format
= V_008F14_IMG_NUM_FORMAT_FLOAT
;
3936 case UTIL_FORMAT_TYPE_SIGNED
:
3937 if (desc
->channel
[first_non_void
].normalized
)
3938 num_format
= V_008F14_IMG_NUM_FORMAT_SNORM
;
3939 else if (desc
->channel
[first_non_void
].pure_integer
)
3940 num_format
= V_008F14_IMG_NUM_FORMAT_SINT
;
3942 num_format
= V_008F14_IMG_NUM_FORMAT_SSCALED
;
3944 case UTIL_FORMAT_TYPE_UNSIGNED
:
3945 if (desc
->channel
[first_non_void
].normalized
)
3946 num_format
= V_008F14_IMG_NUM_FORMAT_UNORM
;
3947 else if (desc
->channel
[first_non_void
].pure_integer
)
3948 num_format
= V_008F14_IMG_NUM_FORMAT_UINT
;
3950 num_format
= V_008F14_IMG_NUM_FORMAT_USCALED
;
3955 data_format
= si_translate_texformat(&screen
->b
, pipe_format
, desc
, first_non_void
);
3956 if (data_format
== ~0) {
3960 /* S8 with Z32 HTILE needs a special format. */
3961 if (screen
->info
.chip_class
== GFX9
&& pipe_format
== PIPE_FORMAT_S8_UINT
)
3962 data_format
= V_008F14_IMG_DATA_FORMAT_S8_32
;
3964 if (!sampler
&& (res
->target
== PIPE_TEXTURE_CUBE
|| res
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
3965 (screen
->info
.chip_class
<= GFX8
&& res
->target
== PIPE_TEXTURE_3D
))) {
3966 /* For the purpose of shader images, treat cube maps and 3D
3967 * textures as 2D arrays. For 3D textures, the address
3968 * calculations for mipmaps are different, so we rely on the
3969 * caller to effectively disable mipmaps.
3971 type
= V_008F1C_SQ_RSRC_IMG_2D_ARRAY
;
3973 assert(res
->target
!= PIPE_TEXTURE_3D
|| (first_level
== 0 && last_level
== 0));
3975 type
= si_tex_dim(screen
, tex
, target
, num_samples
);
3978 if (type
== V_008F1C_SQ_RSRC_IMG_1D_ARRAY
) {
3980 depth
= res
->array_size
;
3981 } else if (type
== V_008F1C_SQ_RSRC_IMG_2D_ARRAY
|| type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
) {
3982 if (sampler
|| res
->target
!= PIPE_TEXTURE_3D
)
3983 depth
= res
->array_size
;
3984 } else if (type
== V_008F1C_SQ_RSRC_IMG_CUBE
)
3985 depth
= res
->array_size
/ 6;
3988 state
[1] = (S_008F14_DATA_FORMAT(data_format
) | S_008F14_NUM_FORMAT(num_format
));
3989 state
[2] = (S_008F18_WIDTH(width
- 1) | S_008F18_HEIGHT(height
- 1) | S_008F18_PERF_MOD(4));
3990 state
[3] = (S_008F1C_DST_SEL_X(si_map_swizzle(swizzle
[0])) |
3991 S_008F1C_DST_SEL_Y(si_map_swizzle(swizzle
[1])) |
3992 S_008F1C_DST_SEL_Z(si_map_swizzle(swizzle
[2])) |
3993 S_008F1C_DST_SEL_W(si_map_swizzle(swizzle
[3])) |
3994 S_008F1C_BASE_LEVEL(num_samples
> 1 ? 0 : first_level
) |
3995 S_008F1C_LAST_LEVEL(num_samples
> 1 ? util_logbase2(num_samples
) : last_level
) |
3996 S_008F1C_TYPE(type
));
3998 state
[5] = S_008F24_BASE_ARRAY(first_layer
);
4002 if (screen
->info
.chip_class
== GFX9
) {
4003 unsigned bc_swizzle
= gfx9_border_color_swizzle(desc
->swizzle
);
4005 /* Depth is the the last accessible layer on Gfx9.
4006 * The hw doesn't need to know the total number of layers.
4008 if (type
== V_008F1C_SQ_RSRC_IMG_3D
)
4009 state
[4] |= S_008F20_DEPTH(depth
- 1);
4011 state
[4] |= S_008F20_DEPTH(last_layer
);
4013 state
[4] |= S_008F20_BC_SWIZZLE(bc_swizzle
);
4014 state
[5] |= S_008F24_MAX_MIP(num_samples
> 1 ? util_logbase2(num_samples
)
4015 : tex
->buffer
.b
.b
.last_level
);
4017 state
[3] |= S_008F1C_POW2_PAD(res
->last_level
> 0);
4018 state
[4] |= S_008F20_DEPTH(depth
- 1);
4019 state
[5] |= S_008F24_LAST_ARRAY(last_layer
);
4022 if (vi_dcc_enabled(tex
, first_level
)) {
4023 state
[6] = S_008F28_ALPHA_IS_ON_MSB(vi_alpha_is_on_msb(screen
, pipe_format
));
4025 /* The last dword is unused by hw. The shader uses it to clear
4026 * bits in the first dword of sampler state.
4028 if (screen
->info
.chip_class
<= GFX7
&& res
->nr_samples
<= 1) {
4029 if (first_level
== last_level
)
4030 state
[7] = C_008F30_MAX_ANISO_RATIO
;
4032 state
[7] = 0xffffffff;
4036 /* Initialize the sampler view for FMASK. */
4037 if (tex
->surface
.fmask_offset
) {
4038 uint32_t data_format
, num_format
;
4040 va
= tex
->buffer
.gpu_address
+ tex
->surface
.fmask_offset
;
4042 #define FMASK(s, f) (((unsigned)(MAX2(1, s)) * 16) + (MAX2(1, f)))
4043 if (screen
->info
.chip_class
== GFX9
) {
4044 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK
;
4045 switch (FMASK(res
->nr_samples
, res
->nr_storage_samples
)) {
4047 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_8_2_1
;
4050 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_8_2_2
;
4053 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_8_4_1
;
4056 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_8_4_2
;
4059 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_8_4_4
;
4062 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_8_8_1
;
4065 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_16_8_2
;
4068 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_32_8_4
;
4071 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_32_8_8
;
4074 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_16_16_1
;
4077 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_32_16_2
;
4080 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_64_16_4
;
4083 num_format
= V_008F14_IMG_NUM_FORMAT_FMASK_64_16_8
;
4086 unreachable("invalid nr_samples");
4089 switch (FMASK(res
->nr_samples
, res
->nr_storage_samples
)) {
4091 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F1
;
4094 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2
;
4097 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F1
;
4100 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F2
;
4103 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4
;
4106 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S8_F1
;
4109 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK16_S8_F2
;
4112 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F4
;
4115 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8
;
4118 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK16_S16_F1
;
4121 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK32_S16_F2
;
4124 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK64_S16_F4
;
4127 data_format
= V_008F14_IMG_DATA_FORMAT_FMASK64_S16_F8
;
4130 unreachable("invalid nr_samples");
4132 num_format
= V_008F14_IMG_NUM_FORMAT_UINT
;
4136 fmask_state
[0] = (va
>> 8) | tex
->surface
.fmask_tile_swizzle
;
4137 fmask_state
[1] = S_008F14_BASE_ADDRESS_HI(va
>> 40) | S_008F14_DATA_FORMAT(data_format
) |
4138 S_008F14_NUM_FORMAT(num_format
);
4139 fmask_state
[2] = S_008F18_WIDTH(width
- 1) | S_008F18_HEIGHT(height
- 1);
4141 S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X
) | S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X
) |
4142 S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X
) | S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X
) |
4143 S_008F1C_TYPE(si_tex_dim(screen
, tex
, target
, 0));
4145 fmask_state
[5] = S_008F24_BASE_ARRAY(first_layer
);
4149 if (screen
->info
.chip_class
== GFX9
) {
4150 fmask_state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.fmask
.swizzle_mode
);
4152 S_008F20_DEPTH(last_layer
) | S_008F20_PITCH(tex
->surface
.u
.gfx9
.fmask
.epitch
);
4153 fmask_state
[5] |= S_008F24_META_PIPE_ALIGNED(1) |
4154 S_008F24_META_RB_ALIGNED(1);
4156 fmask_state
[3] |= S_008F1C_TILING_INDEX(tex
->surface
.u
.legacy
.fmask
.tiling_index
);
4157 fmask_state
[4] |= S_008F20_DEPTH(depth
- 1) |
4158 S_008F20_PITCH(tex
->surface
.u
.legacy
.fmask
.pitch_in_pixels
- 1);
4159 fmask_state
[5] |= S_008F24_LAST_ARRAY(last_layer
);
4165 * Create a sampler view.
4167 * @param ctx context
4168 * @param texture texture
4169 * @param state sampler view template
4170 * @param width0 width0 override (for compressed textures as int)
4171 * @param height0 height0 override (for compressed textures as int)
4172 * @param force_level set the base address to the level (for compressed textures)
4174 struct pipe_sampler_view
*si_create_sampler_view_custom(struct pipe_context
*ctx
,
4175 struct pipe_resource
*texture
,
4176 const struct pipe_sampler_view
*state
,
4177 unsigned width0
, unsigned height0
,
4178 unsigned force_level
)
4180 struct si_context
*sctx
= (struct si_context
*)ctx
;
4181 struct si_sampler_view
*view
= CALLOC_STRUCT(si_sampler_view
);
4182 struct si_texture
*tex
= (struct si_texture
*)texture
;
4183 unsigned base_level
, first_level
, last_level
;
4184 unsigned char state_swizzle
[4];
4185 unsigned height
, depth
, width
;
4186 unsigned last_layer
= state
->u
.tex
.last_layer
;
4187 enum pipe_format pipe_format
;
4188 const struct legacy_surf_level
*surflevel
;
4193 /* initialize base object */
4194 view
->base
= *state
;
4195 view
->base
.texture
= NULL
;
4196 view
->base
.reference
.count
= 1;
4197 view
->base
.context
= ctx
;
4200 pipe_resource_reference(&view
->base
.texture
, texture
);
4202 if (state
->format
== PIPE_FORMAT_X24S8_UINT
|| state
->format
== PIPE_FORMAT_S8X24_UINT
||
4203 state
->format
== PIPE_FORMAT_X32_S8X24_UINT
|| state
->format
== PIPE_FORMAT_S8_UINT
)
4204 view
->is_stencil_sampler
= true;
4206 /* Buffer resource. */
4207 if (texture
->target
== PIPE_BUFFER
) {
4208 si_make_buffer_descriptor(sctx
->screen
, si_resource(texture
), state
->format
,
4209 state
->u
.buf
.offset
, state
->u
.buf
.size
, view
->state
);
4213 state_swizzle
[0] = state
->swizzle_r
;
4214 state_swizzle
[1] = state
->swizzle_g
;
4215 state_swizzle
[2] = state
->swizzle_b
;
4216 state_swizzle
[3] = state
->swizzle_a
;
4219 first_level
= state
->u
.tex
.first_level
;
4220 last_level
= state
->u
.tex
.last_level
;
4223 depth
= texture
->depth0
;
4225 if (sctx
->chip_class
<= GFX8
&& force_level
) {
4226 assert(force_level
== first_level
&& force_level
== last_level
);
4227 base_level
= force_level
;
4230 width
= u_minify(width
, force_level
);
4231 height
= u_minify(height
, force_level
);
4232 depth
= u_minify(depth
, force_level
);
4235 /* This is not needed if gallium frontends set last_layer correctly. */
4236 if (state
->target
== PIPE_TEXTURE_1D
|| state
->target
== PIPE_TEXTURE_2D
||
4237 state
->target
== PIPE_TEXTURE_RECT
|| state
->target
== PIPE_TEXTURE_CUBE
)
4238 last_layer
= state
->u
.tex
.first_layer
;
4240 /* Texturing with separate depth and stencil. */
4241 pipe_format
= state
->format
;
4243 /* Depth/stencil texturing sometimes needs separate texture. */
4244 if (tex
->is_depth
&& !si_can_sample_zs(tex
, view
->is_stencil_sampler
)) {
4245 if (!tex
->flushed_depth_texture
&& !si_init_flushed_depth_texture(ctx
, texture
)) {
4246 pipe_resource_reference(&view
->base
.texture
, NULL
);
4251 assert(tex
->flushed_depth_texture
);
4253 /* Override format for the case where the flushed texture
4254 * contains only Z or only S.
4256 if (tex
->flushed_depth_texture
->buffer
.b
.b
.format
!= tex
->buffer
.b
.b
.format
)
4257 pipe_format
= tex
->flushed_depth_texture
->buffer
.b
.b
.format
;
4259 tex
= tex
->flushed_depth_texture
;
4262 surflevel
= tex
->surface
.u
.legacy
.level
;
4264 if (tex
->db_compatible
) {
4265 if (!view
->is_stencil_sampler
)
4266 pipe_format
= tex
->db_render_format
;
4268 switch (pipe_format
) {
4269 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
4270 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
4272 case PIPE_FORMAT_X8Z24_UNORM
:
4273 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
4274 /* Z24 is always stored like this for DB
4277 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
4279 case PIPE_FORMAT_X24S8_UINT
:
4280 case PIPE_FORMAT_S8X24_UINT
:
4281 case PIPE_FORMAT_X32_S8X24_UINT
:
4282 pipe_format
= PIPE_FORMAT_S8_UINT
;
4283 surflevel
= tex
->surface
.u
.legacy
.stencil_level
;
4289 view
->dcc_incompatible
=
4290 vi_dcc_formats_are_incompatible(texture
, state
->u
.tex
.first_level
, state
->format
);
4292 sctx
->screen
->make_texture_descriptor(
4293 sctx
->screen
, tex
, true, state
->target
, pipe_format
, state_swizzle
, first_level
, last_level
,
4294 state
->u
.tex
.first_layer
, last_layer
, width
, height
, depth
, view
->state
, view
->fmask_state
);
4296 const struct util_format_description
*desc
= util_format_description(pipe_format
);
4297 view
->is_integer
= false;
4299 for (unsigned i
= 0; i
< desc
->nr_channels
; ++i
) {
4300 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
)
4303 /* Whether the number format is {U,S}{SCALED,INT} */
4304 view
->is_integer
= (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
4305 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) &&
4306 (desc
->channel
[i
].pure_integer
|| !desc
->channel
[i
].normalized
);
4310 view
->base_level_info
= &surflevel
[base_level
];
4311 view
->base_level
= base_level
;
4312 view
->block_width
= util_format_get_blockwidth(pipe_format
);
4316 static struct pipe_sampler_view
*si_create_sampler_view(struct pipe_context
*ctx
,
4317 struct pipe_resource
*texture
,
4318 const struct pipe_sampler_view
*state
)
4320 return si_create_sampler_view_custom(ctx
, texture
, state
, texture
? texture
->width0
: 0,
4321 texture
? texture
->height0
: 0, 0);
4324 static void si_sampler_view_destroy(struct pipe_context
*ctx
, struct pipe_sampler_view
*state
)
4326 struct si_sampler_view
*view
= (struct si_sampler_view
*)state
;
4328 pipe_resource_reference(&state
->texture
, NULL
);
4332 static bool wrap_mode_uses_border_color(unsigned wrap
, bool linear_filter
)
4334 return wrap
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
|| wrap
== PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
||
4335 (linear_filter
&& (wrap
== PIPE_TEX_WRAP_CLAMP
|| wrap
== PIPE_TEX_WRAP_MIRROR_CLAMP
));
4338 static uint32_t si_translate_border_color(struct si_context
*sctx
,
4339 const struct pipe_sampler_state
*state
,
4340 const union pipe_color_union
*color
, bool is_integer
)
4342 bool linear_filter
= state
->min_img_filter
!= PIPE_TEX_FILTER_NEAREST
||
4343 state
->mag_img_filter
!= PIPE_TEX_FILTER_NEAREST
;
4345 if (!wrap_mode_uses_border_color(state
->wrap_s
, linear_filter
) &&
4346 !wrap_mode_uses_border_color(state
->wrap_t
, linear_filter
) &&
4347 !wrap_mode_uses_border_color(state
->wrap_r
, linear_filter
))
4348 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK
);
4350 #define simple_border_types(elt) \
4352 if (color->elt[0] == 0 && color->elt[1] == 0 && color->elt[2] == 0 && color->elt[3] == 0) \
4353 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK); \
4354 if (color->elt[0] == 0 && color->elt[1] == 0 && color->elt[2] == 0 && color->elt[3] == 1) \
4355 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK); \
4356 if (color->elt[0] == 1 && color->elt[1] == 1 && color->elt[2] == 1 && color->elt[3] == 1) \
4357 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE); \
4361 simple_border_types(ui
);
4363 simple_border_types(f
);
4365 #undef simple_border_types
4369 /* Check if the border has been uploaded already. */
4370 for (i
= 0; i
< sctx
->border_color_count
; i
++)
4371 if (memcmp(&sctx
->border_color_table
[i
], color
, sizeof(*color
)) == 0)
4374 if (i
>= SI_MAX_BORDER_COLORS
) {
4375 /* Getting 4096 unique border colors is very unlikely. */
4376 fprintf(stderr
, "radeonsi: The border color table is full. "
4377 "Any new border colors will be just black. "
4378 "Please file a bug.\n");
4379 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK
);
4382 if (i
== sctx
->border_color_count
) {
4383 /* Upload a new border color. */
4384 memcpy(&sctx
->border_color_table
[i
], color
, sizeof(*color
));
4385 util_memcpy_cpu_to_le32(&sctx
->border_color_map
[i
], color
, sizeof(*color
));
4386 sctx
->border_color_count
++;
4389 return S_008F3C_BORDER_COLOR_PTR(i
) |
4390 S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_REGISTER
);
4393 static inline int S_FIXED(float value
, unsigned frac_bits
)
4395 return value
* (1 << frac_bits
);
4398 static inline unsigned si_tex_filter(unsigned filter
, unsigned max_aniso
)
4400 if (filter
== PIPE_TEX_FILTER_LINEAR
)
4401 return max_aniso
> 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR
4402 : V_008F38_SQ_TEX_XY_FILTER_BILINEAR
;
4404 return max_aniso
> 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT
4405 : V_008F38_SQ_TEX_XY_FILTER_POINT
;
4408 static inline unsigned si_tex_aniso_filter(unsigned filter
)
4421 static void *si_create_sampler_state(struct pipe_context
*ctx
,
4422 const struct pipe_sampler_state
*state
)
4424 struct si_context
*sctx
= (struct si_context
*)ctx
;
4425 struct si_screen
*sscreen
= sctx
->screen
;
4426 struct si_sampler_state
*rstate
= CALLOC_STRUCT(si_sampler_state
);
4427 unsigned max_aniso
= sscreen
->force_aniso
>= 0 ? sscreen
->force_aniso
: state
->max_anisotropy
;
4428 unsigned max_aniso_ratio
= si_tex_aniso_filter(max_aniso
);
4429 bool trunc_coord
= state
->min_img_filter
== PIPE_TEX_FILTER_NEAREST
&&
4430 state
->mag_img_filter
== PIPE_TEX_FILTER_NEAREST
&&
4431 state
->compare_mode
== PIPE_TEX_COMPARE_NONE
;
4432 union pipe_color_union clamped_border_color
;
4439 rstate
->magic
= SI_SAMPLER_STATE_MAGIC
;
4442 (S_008F30_CLAMP_X(si_tex_wrap(state
->wrap_s
)) | S_008F30_CLAMP_Y(si_tex_wrap(state
->wrap_t
)) |
4443 S_008F30_CLAMP_Z(si_tex_wrap(state
->wrap_r
)) | S_008F30_MAX_ANISO_RATIO(max_aniso_ratio
) |
4444 S_008F30_DEPTH_COMPARE_FUNC(si_tex_compare(state
->compare_func
)) |
4445 S_008F30_FORCE_UNNORMALIZED(!state
->normalized_coords
) |
4446 S_008F30_ANISO_THRESHOLD(max_aniso_ratio
>> 1) | S_008F30_ANISO_BIAS(max_aniso_ratio
) |
4447 S_008F30_DISABLE_CUBE_WRAP(!state
->seamless_cube_map
) |
4448 S_008F30_TRUNC_COORD(trunc_coord
) |
4449 S_008F30_COMPAT_MODE(sctx
->chip_class
== GFX8
|| sctx
->chip_class
== GFX9
));
4450 rstate
->val
[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(state
->min_lod
, 0, 15), 8)) |
4451 S_008F34_MAX_LOD(S_FIXED(CLAMP(state
->max_lod
, 0, 15), 8)) |
4452 S_008F34_PERF_MIP(max_aniso_ratio
? max_aniso_ratio
+ 6 : 0));
4453 rstate
->val
[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(state
->lod_bias
, -16, 16), 8)) |
4454 S_008F38_XY_MAG_FILTER(si_tex_filter(state
->mag_img_filter
, max_aniso
)) |
4455 S_008F38_XY_MIN_FILTER(si_tex_filter(state
->min_img_filter
, max_aniso
)) |
4456 S_008F38_MIP_FILTER(si_tex_mipfilter(state
->min_mip_filter
)) |
4457 S_008F38_MIP_POINT_PRECLAMP(0));
4458 rstate
->val
[3] = si_translate_border_color(sctx
, state
, &state
->border_color
, false);
4460 if (sscreen
->info
.chip_class
>= GFX10
) {
4461 rstate
->val
[2] |= S_008F38_ANISO_OVERRIDE_GFX10(1);
4463 rstate
->val
[2] |= S_008F38_DISABLE_LSB_CEIL(sctx
->chip_class
<= GFX8
) |
4464 S_008F38_FILTER_PREC_FIX(1) |
4465 S_008F38_ANISO_OVERRIDE_GFX8(sctx
->chip_class
>= GFX8
);
4468 /* Create sampler resource for integer textures. */
4469 memcpy(rstate
->integer_val
, rstate
->val
, sizeof(rstate
->val
));
4470 rstate
->integer_val
[3] = si_translate_border_color(sctx
, state
, &state
->border_color
, true);
4472 /* Create sampler resource for upgraded depth textures. */
4473 memcpy(rstate
->upgraded_depth_val
, rstate
->val
, sizeof(rstate
->val
));
4475 for (unsigned i
= 0; i
< 4; ++i
) {
4476 /* Use channel 0 on purpose, so that we can use OPAQUE_WHITE
4477 * when the border color is 1.0. */
4478 clamped_border_color
.f
[i
] = CLAMP(state
->border_color
.f
[0], 0, 1);
4481 if (memcmp(&state
->border_color
, &clamped_border_color
, sizeof(clamped_border_color
)) == 0) {
4482 if (sscreen
->info
.chip_class
<= GFX9
)
4483 rstate
->upgraded_depth_val
[3] |= S_008F3C_UPGRADED_DEPTH(1);
4485 rstate
->upgraded_depth_val
[3] =
4486 si_translate_border_color(sctx
, state
, &clamped_border_color
, false);
4492 static void si_set_sample_mask(struct pipe_context
*ctx
, unsigned sample_mask
)
4494 struct si_context
*sctx
= (struct si_context
*)ctx
;
4496 if (sctx
->sample_mask
== (uint16_t)sample_mask
)
4499 sctx
->sample_mask
= sample_mask
;
4500 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.sample_mask
);
4503 static void si_emit_sample_mask(struct si_context
*sctx
)
4505 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
4506 unsigned mask
= sctx
->sample_mask
;
4508 /* Needed for line and polygon smoothing as well as for the Polaris
4509 * small primitive filter. We expect the gallium frontend to take care of
4512 assert(mask
== 0xffff || sctx
->framebuffer
.nr_samples
> 1 ||
4513 (mask
& 1 && sctx
->blitter
->running
));
4515 radeon_set_context_reg_seq(cs
, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0
, 2);
4516 radeon_emit(cs
, mask
| (mask
<< 16));
4517 radeon_emit(cs
, mask
| (mask
<< 16));
4520 static void si_delete_sampler_state(struct pipe_context
*ctx
, void *state
)
4523 struct si_sampler_state
*s
= state
;
4525 assert(s
->magic
== SI_SAMPLER_STATE_MAGIC
);
4532 * Vertex elements & buffers
4535 struct si_fast_udiv_info32
si_compute_fast_udiv_info32(uint32_t D
, unsigned num_bits
)
4537 struct util_fast_udiv_info info
= util_compute_fast_udiv_info(D
, num_bits
, 32);
4539 struct si_fast_udiv_info32 result
= {
4548 static void *si_create_vertex_elements(struct pipe_context
*ctx
, unsigned count
,
4549 const struct pipe_vertex_element
*elements
)
4551 struct si_screen
*sscreen
= (struct si_screen
*)ctx
->screen
;
4552 struct si_vertex_elements
*v
= CALLOC_STRUCT(si_vertex_elements
);
4553 bool used
[SI_NUM_VERTEX_BUFFERS
] = {};
4554 struct si_fast_udiv_info32 divisor_factors
[SI_MAX_ATTRIBS
] = {};
4555 STATIC_ASSERT(sizeof(struct si_fast_udiv_info32
) == 16);
4556 STATIC_ASSERT(sizeof(divisor_factors
[0].multiplier
) == 4);
4557 STATIC_ASSERT(sizeof(divisor_factors
[0].pre_shift
) == 4);
4558 STATIC_ASSERT(sizeof(divisor_factors
[0].post_shift
) == 4);
4559 STATIC_ASSERT(sizeof(divisor_factors
[0].increment
) == 4);
4562 assert(count
<= SI_MAX_ATTRIBS
);
4568 unsigned alloc_count
=
4569 count
> sscreen
->num_vbos_in_user_sgprs
? count
- sscreen
->num_vbos_in_user_sgprs
: 0;
4570 v
->vb_desc_list_alloc_size
= align(alloc_count
* 16, SI_CPDMA_ALIGNMENT
);
4572 for (i
= 0; i
< count
; ++i
) {
4573 const struct util_format_description
*desc
;
4574 const struct util_format_channel_description
*channel
;
4576 unsigned vbo_index
= elements
[i
].vertex_buffer_index
;
4578 if (vbo_index
>= SI_NUM_VERTEX_BUFFERS
) {
4583 unsigned instance_divisor
= elements
[i
].instance_divisor
;
4584 if (instance_divisor
) {
4585 v
->uses_instance_divisors
= true;
4587 if (instance_divisor
== 1) {
4588 v
->instance_divisor_is_one
|= 1u << i
;
4590 v
->instance_divisor_is_fetched
|= 1u << i
;
4591 divisor_factors
[i
] = si_compute_fast_udiv_info32(instance_divisor
, 32);
4595 if (!used
[vbo_index
]) {
4596 v
->first_vb_use_mask
|= 1 << i
;
4597 used
[vbo_index
] = true;
4600 desc
= util_format_description(elements
[i
].src_format
);
4601 first_non_void
= util_format_get_first_non_void_channel(elements
[i
].src_format
);
4602 channel
= first_non_void
>= 0 ? &desc
->channel
[first_non_void
] : NULL
;
4604 v
->format_size
[i
] = desc
->block
.bits
/ 8;
4605 v
->src_offset
[i
] = elements
[i
].src_offset
;
4606 v
->vertex_buffer_index
[i
] = vbo_index
;
4608 bool always_fix
= false;
4609 union si_vs_fix_fetch fix_fetch
;
4610 unsigned log_hw_load_size
; /* the load element size as seen by the hardware */
4613 log_hw_load_size
= MIN2(2, util_logbase2(desc
->block
.bits
) - 3);
4616 switch (channel
->type
) {
4617 case UTIL_FORMAT_TYPE_FLOAT
:
4618 fix_fetch
.u
.format
= AC_FETCH_FORMAT_FLOAT
;
4620 case UTIL_FORMAT_TYPE_FIXED
:
4621 fix_fetch
.u
.format
= AC_FETCH_FORMAT_FIXED
;
4623 case UTIL_FORMAT_TYPE_SIGNED
: {
4624 if (channel
->pure_integer
)
4625 fix_fetch
.u
.format
= AC_FETCH_FORMAT_SINT
;
4626 else if (channel
->normalized
)
4627 fix_fetch
.u
.format
= AC_FETCH_FORMAT_SNORM
;
4629 fix_fetch
.u
.format
= AC_FETCH_FORMAT_SSCALED
;
4632 case UTIL_FORMAT_TYPE_UNSIGNED
: {
4633 if (channel
->pure_integer
)
4634 fix_fetch
.u
.format
= AC_FETCH_FORMAT_UINT
;
4635 else if (channel
->normalized
)
4636 fix_fetch
.u
.format
= AC_FETCH_FORMAT_UNORM
;
4638 fix_fetch
.u
.format
= AC_FETCH_FORMAT_USCALED
;
4642 unreachable("bad format type");
4645 switch (elements
[i
].src_format
) {
4646 case PIPE_FORMAT_R11G11B10_FLOAT
:
4647 fix_fetch
.u
.format
= AC_FETCH_FORMAT_FLOAT
;
4650 unreachable("bad other format");
4654 if (desc
->channel
[0].size
== 10) {
4655 fix_fetch
.u
.log_size
= 3; /* special encoding for 2_10_10_10 */
4656 log_hw_load_size
= 2;
4658 /* The hardware always treats the 2-bit alpha channel as
4659 * unsigned, so a shader workaround is needed. The affected
4660 * chips are GFX8 and older except Stoney (GFX8.1).
4662 always_fix
= sscreen
->info
.chip_class
<= GFX8
&& sscreen
->info
.family
!= CHIP_STONEY
&&
4663 channel
->type
== UTIL_FORMAT_TYPE_SIGNED
;
4664 } else if (elements
[i
].src_format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
4665 fix_fetch
.u
.log_size
= 3; /* special encoding */
4666 fix_fetch
.u
.format
= AC_FETCH_FORMAT_FIXED
;
4667 log_hw_load_size
= 2;
4669 fix_fetch
.u
.log_size
= util_logbase2(channel
->size
) - 3;
4670 fix_fetch
.u
.num_channels_m1
= desc
->nr_channels
- 1;
4673 * - doubles (multiple loads + truncate to float)
4674 * - 32-bit requiring a conversion
4676 always_fix
= (fix_fetch
.u
.log_size
== 3) ||
4677 (fix_fetch
.u
.log_size
== 2 && fix_fetch
.u
.format
!= AC_FETCH_FORMAT_FLOAT
&&
4678 fix_fetch
.u
.format
!= AC_FETCH_FORMAT_UINT
&&
4679 fix_fetch
.u
.format
!= AC_FETCH_FORMAT_SINT
);
4681 /* Also fixup 8_8_8 and 16_16_16. */
4682 if (desc
->nr_channels
== 3 && fix_fetch
.u
.log_size
<= 1) {
4684 log_hw_load_size
= fix_fetch
.u
.log_size
;
4688 if (desc
->swizzle
[0] != PIPE_SWIZZLE_X
) {
4689 assert(desc
->swizzle
[0] == PIPE_SWIZZLE_Z
&&
4690 (desc
->swizzle
[2] == PIPE_SWIZZLE_X
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
));
4691 fix_fetch
.u
.reverse
= 1;
4694 /* Force the workaround for unaligned access here already if the
4695 * offset relative to the vertex buffer base is unaligned.
4697 * There is a theoretical case in which this is too conservative:
4698 * if the vertex buffer's offset is also unaligned in just the
4699 * right way, we end up with an aligned address after all.
4700 * However, this case should be extremely rare in practice (it
4701 * won't happen in well-behaved applications), and taking it
4702 * into account would complicate the fast path (where everything
4703 * is nicely aligned).
4705 bool check_alignment
=
4706 log_hw_load_size
>= 1 &&
4707 (sscreen
->info
.chip_class
== GFX6
|| sscreen
->info
.chip_class
>= GFX10
);
4708 bool opencode
= sscreen
->options
.vs_fetch_always_opencode
;
4710 if (check_alignment
&& (elements
[i
].src_offset
& ((1 << log_hw_load_size
) - 1)) != 0)
4713 if (always_fix
|| check_alignment
|| opencode
)
4714 v
->fix_fetch
[i
] = fix_fetch
.bits
;
4717 v
->fix_fetch_opencode
|= 1 << i
;
4718 if (opencode
|| always_fix
)
4719 v
->fix_fetch_always
|= 1 << i
;
4721 if (check_alignment
&& !opencode
) {
4722 assert(log_hw_load_size
== 1 || log_hw_load_size
== 2);
4724 v
->fix_fetch_unaligned
|= 1 << i
;
4725 v
->hw_load_is_dword
|= (log_hw_load_size
- 1) << i
;
4726 v
->vb_alignment_check_mask
|= 1 << vbo_index
;
4729 v
->rsrc_word3
[i
] = S_008F0C_DST_SEL_X(si_map_swizzle(desc
->swizzle
[0])) |
4730 S_008F0C_DST_SEL_Y(si_map_swizzle(desc
->swizzle
[1])) |
4731 S_008F0C_DST_SEL_Z(si_map_swizzle(desc
->swizzle
[2])) |
4732 S_008F0C_DST_SEL_W(si_map_swizzle(desc
->swizzle
[3]));
4734 if (sscreen
->info
.chip_class
>= GFX10
) {
4735 const struct gfx10_format
*fmt
= &gfx10_format_table
[elements
[i
].src_format
];
4736 assert(fmt
->img_format
!= 0 && fmt
->img_format
< 128);
4737 v
->rsrc_word3
[i
] |= S_008F0C_FORMAT(fmt
->img_format
) | S_008F0C_RESOURCE_LEVEL(1);
4739 unsigned data_format
, num_format
;
4740 data_format
= si_translate_buffer_dataformat(ctx
->screen
, desc
, first_non_void
);
4741 num_format
= si_translate_buffer_numformat(ctx
->screen
, desc
, first_non_void
);
4742 v
->rsrc_word3
[i
] |= S_008F0C_NUM_FORMAT(num_format
) | S_008F0C_DATA_FORMAT(data_format
);
4746 if (v
->instance_divisor_is_fetched
) {
4747 unsigned num_divisors
= util_last_bit(v
->instance_divisor_is_fetched
);
4749 v
->instance_divisor_factor_buffer
= (struct si_resource
*)pipe_buffer_create(
4750 &sscreen
->b
, 0, PIPE_USAGE_DEFAULT
, num_divisors
* sizeof(divisor_factors
[0]));
4751 if (!v
->instance_divisor_factor_buffer
) {
4756 sscreen
->ws
->buffer_map(v
->instance_divisor_factor_buffer
->buf
, NULL
, PIPE_TRANSFER_WRITE
);
4757 memcpy(map
, divisor_factors
, num_divisors
* sizeof(divisor_factors
[0]));
4762 static void si_bind_vertex_elements(struct pipe_context
*ctx
, void *state
)
4764 struct si_context
*sctx
= (struct si_context
*)ctx
;
4765 struct si_vertex_elements
*old
= sctx
->vertex_elements
;
4766 struct si_vertex_elements
*v
= (struct si_vertex_elements
*)state
;
4768 sctx
->vertex_elements
= v
;
4769 sctx
->num_vertex_elements
= v
? v
->count
: 0;
4771 if (sctx
->num_vertex_elements
) {
4772 sctx
->vertex_buffers_dirty
= true;
4774 sctx
->vertex_buffer_pointer_dirty
= false;
4775 sctx
->vertex_buffer_user_sgprs_dirty
= false;
4778 if (v
&& (!old
|| old
->count
!= v
->count
||
4779 old
->uses_instance_divisors
!= v
->uses_instance_divisors
||
4780 /* we don't check which divisors changed */
4781 v
->uses_instance_divisors
||
4782 (old
->vb_alignment_check_mask
^ v
->vb_alignment_check_mask
) &
4783 sctx
->vertex_buffer_unaligned
||
4784 ((v
->vb_alignment_check_mask
& sctx
->vertex_buffer_unaligned
) &&
4785 memcmp(old
->vertex_buffer_index
, v
->vertex_buffer_index
,
4786 sizeof(v
->vertex_buffer_index
[0]) * v
->count
)) ||
4787 /* fix_fetch_{always,opencode,unaligned} and hw_load_is_dword are
4788 * functions of fix_fetch and the src_offset alignment.
4789 * If they change and fix_fetch doesn't, it must be due to different
4790 * src_offset alignment, which is reflected in fix_fetch_opencode. */
4791 old
->fix_fetch_opencode
!= v
->fix_fetch_opencode
||
4792 memcmp(old
->fix_fetch
, v
->fix_fetch
, sizeof(v
->fix_fetch
[0]) * v
->count
)))
4793 sctx
->do_update_shaders
= true;
4795 if (v
&& v
->instance_divisor_is_fetched
) {
4796 struct pipe_constant_buffer cb
;
4798 cb
.buffer
= &v
->instance_divisor_factor_buffer
->b
.b
;
4799 cb
.user_buffer
= NULL
;
4800 cb
.buffer_offset
= 0;
4801 cb
.buffer_size
= 0xffffffff;
4802 si_set_rw_buffer(sctx
, SI_VS_CONST_INSTANCE_DIVISORS
, &cb
);
4806 static void si_delete_vertex_element(struct pipe_context
*ctx
, void *state
)
4808 struct si_context
*sctx
= (struct si_context
*)ctx
;
4809 struct si_vertex_elements
*v
= (struct si_vertex_elements
*)state
;
4811 if (sctx
->vertex_elements
== state
) {
4812 sctx
->vertex_elements
= NULL
;
4813 sctx
->num_vertex_elements
= 0;
4815 si_resource_reference(&v
->instance_divisor_factor_buffer
, NULL
);
4819 static void si_set_vertex_buffers(struct pipe_context
*ctx
, unsigned start_slot
, unsigned count
,
4820 const struct pipe_vertex_buffer
*buffers
)
4822 struct si_context
*sctx
= (struct si_context
*)ctx
;
4823 struct pipe_vertex_buffer
*dst
= sctx
->vertex_buffer
+ start_slot
;
4824 unsigned updated_mask
= u_bit_consecutive(start_slot
, count
);
4825 uint32_t orig_unaligned
= sctx
->vertex_buffer_unaligned
;
4826 uint32_t unaligned
= 0;
4829 assert(start_slot
+ count
<= ARRAY_SIZE(sctx
->vertex_buffer
));
4832 for (i
= 0; i
< count
; i
++) {
4833 const struct pipe_vertex_buffer
*src
= buffers
+ i
;
4834 struct pipe_vertex_buffer
*dsti
= dst
+ i
;
4835 struct pipe_resource
*buf
= src
->buffer
.resource
;
4836 unsigned slot_bit
= 1 << (start_slot
+ i
);
4838 pipe_resource_reference(&dsti
->buffer
.resource
, buf
);
4839 dsti
->buffer_offset
= src
->buffer_offset
;
4840 dsti
->stride
= src
->stride
;
4842 if (dsti
->buffer_offset
& 3 || dsti
->stride
& 3)
4843 unaligned
|= slot_bit
;
4845 si_context_add_resource_size(sctx
, buf
);
4847 si_resource(buf
)->bind_history
|= PIPE_BIND_VERTEX_BUFFER
;
4850 for (i
= 0; i
< count
; i
++) {
4851 pipe_resource_reference(&dst
[i
].buffer
.resource
, NULL
);
4853 unaligned
&= ~updated_mask
;
4855 sctx
->vertex_buffers_dirty
= true;
4856 sctx
->vertex_buffer_unaligned
= (orig_unaligned
& ~updated_mask
) | unaligned
;
4858 /* Check whether alignment may have changed in a way that requires
4859 * shader changes. This check is conservative: a vertex buffer can only
4860 * trigger a shader change if the misalignment amount changes (e.g.
4861 * from byte-aligned to short-aligned), but we only keep track of
4862 * whether buffers are at least dword-aligned, since that should always
4863 * be the case in well-behaved applications anyway.
4865 if (sctx
->vertex_elements
&& (sctx
->vertex_elements
->vb_alignment_check_mask
&
4866 (unaligned
| orig_unaligned
) & updated_mask
))
4867 sctx
->do_update_shaders
= true;
4874 static void si_set_tess_state(struct pipe_context
*ctx
, const float default_outer_level
[4],
4875 const float default_inner_level
[2])
4877 struct si_context
*sctx
= (struct si_context
*)ctx
;
4878 struct pipe_constant_buffer cb
;
4881 memcpy(array
, default_outer_level
, sizeof(float) * 4);
4882 memcpy(array
+ 4, default_inner_level
, sizeof(float) * 2);
4885 cb
.user_buffer
= NULL
;
4886 cb
.buffer_size
= sizeof(array
);
4888 si_upload_const_buffer(sctx
, (struct si_resource
**)&cb
.buffer
, (void *)array
, sizeof(array
),
4891 si_set_rw_buffer(sctx
, SI_HS_CONST_DEFAULT_TESS_LEVELS
, &cb
);
4892 pipe_resource_reference(&cb
.buffer
, NULL
);
4895 static void si_texture_barrier(struct pipe_context
*ctx
, unsigned flags
)
4897 struct si_context
*sctx
= (struct si_context
*)ctx
;
4899 si_update_fb_dirtiness_after_rendering(sctx
);
4901 /* Multisample surfaces are flushed in si_decompress_textures. */
4902 if (sctx
->framebuffer
.uncompressed_cb_mask
) {
4903 si_make_CB_shader_coherent(sctx
, sctx
->framebuffer
.nr_samples
,
4904 sctx
->framebuffer
.CB_has_shader_readable_metadata
,
4905 sctx
->framebuffer
.all_DCC_pipe_aligned
);
4909 /* This only ensures coherency for shader image/buffer stores. */
4910 static void si_memory_barrier(struct pipe_context
*ctx
, unsigned flags
)
4912 struct si_context
*sctx
= (struct si_context
*)ctx
;
4914 if (!(flags
& ~PIPE_BARRIER_UPDATE
))
4917 /* Subsequent commands must wait for all shader invocations to
4919 sctx
->flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
| SI_CONTEXT_CS_PARTIAL_FLUSH
;
4921 if (flags
& PIPE_BARRIER_CONSTANT_BUFFER
)
4922 sctx
->flags
|= SI_CONTEXT_INV_SCACHE
| SI_CONTEXT_INV_VCACHE
;
4924 if (flags
& (PIPE_BARRIER_VERTEX_BUFFER
| PIPE_BARRIER_SHADER_BUFFER
| PIPE_BARRIER_TEXTURE
|
4925 PIPE_BARRIER_IMAGE
| PIPE_BARRIER_STREAMOUT_BUFFER
| PIPE_BARRIER_GLOBAL_BUFFER
)) {
4926 /* As far as I can tell, L1 contents are written back to L2
4927 * automatically at end of shader, but the contents of other
4928 * L1 caches might still be stale. */
4929 sctx
->flags
|= SI_CONTEXT_INV_VCACHE
;
4932 if (flags
& PIPE_BARRIER_INDEX_BUFFER
) {
4933 /* Indices are read through TC L2 since GFX8.
4936 if (sctx
->screen
->info
.chip_class
<= GFX7
)
4937 sctx
->flags
|= SI_CONTEXT_WB_L2
;
4940 /* MSAA color, any depth and any stencil are flushed in
4941 * si_decompress_textures when needed.
4943 if (flags
& PIPE_BARRIER_FRAMEBUFFER
&& sctx
->framebuffer
.uncompressed_cb_mask
) {
4944 sctx
->flags
|= SI_CONTEXT_FLUSH_AND_INV_CB
;
4946 if (sctx
->chip_class
<= GFX8
)
4947 sctx
->flags
|= SI_CONTEXT_WB_L2
;
4950 /* Indirect buffers use TC L2 on GFX9, but not older hw. */
4951 if (sctx
->screen
->info
.chip_class
<= GFX8
&& flags
& PIPE_BARRIER_INDIRECT_BUFFER
)
4952 sctx
->flags
|= SI_CONTEXT_WB_L2
;
4955 static void *si_create_blend_custom(struct si_context
*sctx
, unsigned mode
)
4957 struct pipe_blend_state blend
;
4959 memset(&blend
, 0, sizeof(blend
));
4960 blend
.independent_blend_enable
= true;
4961 blend
.rt
[0].colormask
= 0xf;
4962 return si_create_blend_state_mode(&sctx
->b
, &blend
, mode
);
4965 void si_init_state_compute_functions(struct si_context
*sctx
)
4967 sctx
->b
.create_sampler_state
= si_create_sampler_state
;
4968 sctx
->b
.delete_sampler_state
= si_delete_sampler_state
;
4969 sctx
->b
.create_sampler_view
= si_create_sampler_view
;
4970 sctx
->b
.sampler_view_destroy
= si_sampler_view_destroy
;
4971 sctx
->b
.memory_barrier
= si_memory_barrier
;
4974 void si_init_state_functions(struct si_context
*sctx
)
4976 sctx
->atoms
.s
.framebuffer
.emit
= si_emit_framebuffer_state
;
4977 sctx
->atoms
.s
.msaa_sample_locs
.emit
= si_emit_msaa_sample_locs
;
4978 sctx
->atoms
.s
.db_render_state
.emit
= si_emit_db_render_state
;
4979 sctx
->atoms
.s
.dpbb_state
.emit
= si_emit_dpbb_state
;
4980 sctx
->atoms
.s
.msaa_config
.emit
= si_emit_msaa_config
;
4981 sctx
->atoms
.s
.sample_mask
.emit
= si_emit_sample_mask
;
4982 sctx
->atoms
.s
.cb_render_state
.emit
= si_emit_cb_render_state
;
4983 sctx
->atoms
.s
.blend_color
.emit
= si_emit_blend_color
;
4984 sctx
->atoms
.s
.clip_regs
.emit
= si_emit_clip_regs
;
4985 sctx
->atoms
.s
.clip_state
.emit
= si_emit_clip_state
;
4986 sctx
->atoms
.s
.stencil_ref
.emit
= si_emit_stencil_ref
;
4988 sctx
->b
.create_blend_state
= si_create_blend_state
;
4989 sctx
->b
.bind_blend_state
= si_bind_blend_state
;
4990 sctx
->b
.delete_blend_state
= si_delete_blend_state
;
4991 sctx
->b
.set_blend_color
= si_set_blend_color
;
4993 sctx
->b
.create_rasterizer_state
= si_create_rs_state
;
4994 sctx
->b
.bind_rasterizer_state
= si_bind_rs_state
;
4995 sctx
->b
.delete_rasterizer_state
= si_delete_rs_state
;
4997 sctx
->b
.create_depth_stencil_alpha_state
= si_create_dsa_state
;
4998 sctx
->b
.bind_depth_stencil_alpha_state
= si_bind_dsa_state
;
4999 sctx
->b
.delete_depth_stencil_alpha_state
= si_delete_dsa_state
;
5001 sctx
->custom_dsa_flush
= si_create_db_flush_dsa(sctx
);
5002 sctx
->custom_blend_resolve
= si_create_blend_custom(sctx
, V_028808_CB_RESOLVE
);
5003 sctx
->custom_blend_fmask_decompress
= si_create_blend_custom(sctx
, V_028808_CB_FMASK_DECOMPRESS
);
5004 sctx
->custom_blend_eliminate_fastclear
=
5005 si_create_blend_custom(sctx
, V_028808_CB_ELIMINATE_FAST_CLEAR
);
5006 sctx
->custom_blend_dcc_decompress
= si_create_blend_custom(sctx
, V_028808_CB_DCC_DECOMPRESS
);
5008 sctx
->b
.set_clip_state
= si_set_clip_state
;
5009 sctx
->b
.set_stencil_ref
= si_set_stencil_ref
;
5011 sctx
->b
.set_framebuffer_state
= si_set_framebuffer_state
;
5013 sctx
->b
.set_sample_mask
= si_set_sample_mask
;
5015 sctx
->b
.create_vertex_elements_state
= si_create_vertex_elements
;
5016 sctx
->b
.bind_vertex_elements_state
= si_bind_vertex_elements
;
5017 sctx
->b
.delete_vertex_elements_state
= si_delete_vertex_element
;
5018 sctx
->b
.set_vertex_buffers
= si_set_vertex_buffers
;
5020 sctx
->b
.texture_barrier
= si_texture_barrier
;
5021 sctx
->b
.set_min_samples
= si_set_min_samples
;
5022 sctx
->b
.set_tess_state
= si_set_tess_state
;
5024 sctx
->b
.set_active_query_state
= si_set_active_query_state
;
5027 void si_init_screen_state_functions(struct si_screen
*sscreen
)
5029 sscreen
->b
.is_format_supported
= si_is_format_supported
;
5031 if (sscreen
->info
.chip_class
>= GFX10
) {
5032 sscreen
->make_texture_descriptor
= gfx10_make_texture_descriptor
;
5034 sscreen
->make_texture_descriptor
= si_make_texture_descriptor
;
5038 static void si_set_grbm_gfx_index(struct si_context
*sctx
, struct si_pm4_state
*pm4
, unsigned value
)
5040 unsigned reg
= sctx
->chip_class
>= GFX7
? R_030800_GRBM_GFX_INDEX
: R_00802C_GRBM_GFX_INDEX
;
5041 si_pm4_set_reg(pm4
, reg
, value
);
5044 static void si_set_grbm_gfx_index_se(struct si_context
*sctx
, struct si_pm4_state
*pm4
, unsigned se
)
5046 assert(se
== ~0 || se
< sctx
->screen
->info
.max_se
);
5047 si_set_grbm_gfx_index(sctx
, pm4
,
5048 (se
== ~0 ? S_030800_SE_BROADCAST_WRITES(1) : S_030800_SE_INDEX(se
)) |
5049 S_030800_SH_BROADCAST_WRITES(1) |
5050 S_030800_INSTANCE_BROADCAST_WRITES(1));
5053 static void si_write_harvested_raster_configs(struct si_context
*sctx
, struct si_pm4_state
*pm4
,
5054 unsigned raster_config
, unsigned raster_config_1
)
5056 unsigned num_se
= MAX2(sctx
->screen
->info
.max_se
, 1);
5057 unsigned raster_config_se
[4];
5060 ac_get_harvested_configs(&sctx
->screen
->info
, raster_config
, &raster_config_1
, raster_config_se
);
5062 for (se
= 0; se
< num_se
; se
++) {
5063 si_set_grbm_gfx_index_se(sctx
, pm4
, se
);
5064 si_pm4_set_reg(pm4
, R_028350_PA_SC_RASTER_CONFIG
, raster_config_se
[se
]);
5066 si_set_grbm_gfx_index(sctx
, pm4
, ~0);
5068 if (sctx
->chip_class
>= GFX7
) {
5069 si_pm4_set_reg(pm4
, R_028354_PA_SC_RASTER_CONFIG_1
, raster_config_1
);
5073 static void si_set_raster_config(struct si_context
*sctx
, struct si_pm4_state
*pm4
)
5075 struct si_screen
*sscreen
= sctx
->screen
;
5076 unsigned num_rb
= MIN2(sscreen
->info
.num_render_backends
, 16);
5077 unsigned rb_mask
= sscreen
->info
.enabled_rb_mask
;
5078 unsigned raster_config
= sscreen
->pa_sc_raster_config
;
5079 unsigned raster_config_1
= sscreen
->pa_sc_raster_config_1
;
5081 if (!rb_mask
|| util_bitcount(rb_mask
) >= num_rb
) {
5082 /* Always use the default config when all backends are enabled
5083 * (or when we failed to determine the enabled backends).
5085 si_pm4_set_reg(pm4
, R_028350_PA_SC_RASTER_CONFIG
, raster_config
);
5086 if (sctx
->chip_class
>= GFX7
)
5087 si_pm4_set_reg(pm4
, R_028354_PA_SC_RASTER_CONFIG_1
, raster_config_1
);
5089 si_write_harvested_raster_configs(sctx
, pm4
, raster_config
, raster_config_1
);
5093 void si_init_cs_preamble_state(struct si_context
*sctx
, bool uses_reg_shadowing
)
5095 struct si_screen
*sscreen
= sctx
->screen
;
5096 uint64_t border_color_va
= sctx
->border_color_buffer
->gpu_address
;
5097 bool has_clear_state
= sscreen
->info
.has_clear_state
;
5098 struct si_pm4_state
*pm4
= CALLOC_STRUCT(si_pm4_state
);
5103 if (!uses_reg_shadowing
) {
5104 si_pm4_cmd_add(pm4
, PKT3(PKT3_CONTEXT_CONTROL
, 1, 0));
5105 si_pm4_cmd_add(pm4
, CC0_UPDATE_LOAD_ENABLES(1));
5106 si_pm4_cmd_add(pm4
, CC1_UPDATE_SHADOW_ENABLES(1));
5108 if (has_clear_state
) {
5109 si_pm4_cmd_add(pm4
, PKT3(PKT3_CLEAR_STATE
, 0, 0));
5110 si_pm4_cmd_add(pm4
, 0);
5114 /* CLEAR_STATE doesn't restore these correctly. */
5115 si_pm4_set_reg(pm4
, R_028240_PA_SC_GENERIC_SCISSOR_TL
, S_028240_WINDOW_OFFSET_DISABLE(1));
5116 si_pm4_set_reg(pm4
, R_028244_PA_SC_GENERIC_SCISSOR_BR
,
5117 S_028244_BR_X(16384) | S_028244_BR_Y(16384));
5119 si_pm4_set_reg(pm4
, R_028A18_VGT_HOS_MAX_TESS_LEVEL
, fui(64));
5120 if (!has_clear_state
)
5121 si_pm4_set_reg(pm4
, R_028A1C_VGT_HOS_MIN_TESS_LEVEL
, fui(0));
5123 if (!has_clear_state
) {
5124 si_pm4_set_reg(pm4
, R_028230_PA_SC_EDGERULE
,
5125 S_028230_ER_TRI(0xA) | S_028230_ER_POINT(0xA) | S_028230_ER_RECT(0xA) |
5126 /* Required by DX10_DIAMOND_TEST_ENA: */
5127 S_028230_ER_LINE_LR(0x1A) | S_028230_ER_LINE_RL(0x26) |
5128 S_028230_ER_LINE_TB(0xA) | S_028230_ER_LINE_BT(0xA));
5129 si_pm4_set_reg(pm4
, R_028820_PA_CL_NANINF_CNTL
, 0);
5130 si_pm4_set_reg(pm4
, R_028AC0_DB_SRESULTS_COMPARE_STATE0
, 0x0);
5131 si_pm4_set_reg(pm4
, R_028AC4_DB_SRESULTS_COMPARE_STATE1
, 0x0);
5132 si_pm4_set_reg(pm4
, R_028AC8_DB_PRELOAD_CONTROL
, 0x0);
5133 si_pm4_set_reg(pm4
, R_02800C_DB_RENDER_OVERRIDE
, 0);
5134 si_pm4_set_reg(pm4
, R_028A5C_VGT_GS_PER_VS
, 0x2);
5135 si_pm4_set_reg(pm4
, R_028A8C_VGT_PRIMITIVEID_RESET
, 0x0);
5136 si_pm4_set_reg(pm4
, R_028B98_VGT_STRMOUT_BUFFER_CONFIG
, 0x0);
5137 si_pm4_set_reg(pm4
, R_028AB8_VGT_VTX_CNT_EN
, 0x0);
5140 si_pm4_set_reg(pm4
, R_028080_TA_BC_BASE_ADDR
, border_color_va
>> 8);
5141 if (sctx
->chip_class
>= GFX7
)
5142 si_pm4_set_reg(pm4
, R_028084_TA_BC_BASE_ADDR_HI
, S_028084_ADDRESS(border_color_va
>> 40));
5144 if (sctx
->chip_class
== GFX6
) {
5145 si_pm4_set_reg(pm4
, R_008A14_PA_CL_ENHANCE
,
5146 S_008A14_NUM_CLIP_SEQ(3) | S_008A14_CLIP_VTX_REORDER_ENA(1));
5149 if (sctx
->chip_class
<= GFX7
|| !has_clear_state
) {
5150 si_pm4_set_reg(pm4
, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL
, 14);
5151 si_pm4_set_reg(pm4
, R_028C5C_VGT_OUT_DEALLOC_CNTL
, 16);
5153 /* CLEAR_STATE doesn't clear these correctly on certain generations.
5154 * I don't know why. Deduced by trial and error.
5156 si_pm4_set_reg(pm4
, R_028B28_VGT_STRMOUT_DRAW_OPAQUE_OFFSET
, 0);
5157 si_pm4_set_reg(pm4
, R_028204_PA_SC_WINDOW_SCISSOR_TL
, S_028204_WINDOW_OFFSET_DISABLE(1));
5158 si_pm4_set_reg(pm4
, R_028030_PA_SC_SCREEN_SCISSOR_TL
, 0);
5159 si_pm4_set_reg(pm4
, R_028034_PA_SC_SCREEN_SCISSOR_BR
,
5160 S_028034_BR_X(16384) | S_028034_BR_Y(16384));
5163 if (sctx
->chip_class
>= GFX7
) {
5164 /* Compute LATE_ALLOC_VS.LIMIT. */
5165 unsigned num_cu_per_sh
= sscreen
->info
.min_good_cu_per_sa
;
5166 unsigned late_alloc_wave64
= 0; /* The limit is per SA. */
5167 unsigned cu_mask_vs
= 0xffff;
5168 unsigned cu_mask_gs
= 0xffff;
5170 if (sctx
->chip_class
>= GFX10
) {
5171 /* For Wave32, the hw will launch twice the number of late
5172 * alloc waves, so 1 == 2x wave32.
5174 if (!sscreen
->info
.use_late_alloc
) {
5175 late_alloc_wave64
= 0;
5176 } else if (num_cu_per_sh
<= 6) {
5177 late_alloc_wave64
= num_cu_per_sh
- 2;
5179 late_alloc_wave64
= (num_cu_per_sh
- 2) * 4;
5181 /* CU2 & CU3 disabled because of the dual CU design */
5182 /* Late alloc is not used for NGG on Navi14 due to a hw bug. */
5183 cu_mask_vs
= 0xfff3;
5184 cu_mask_gs
= sscreen
->use_ngg
&& sctx
->family
!= CHIP_NAVI14
? 0xfff3 : 0xffff;
5187 if (!sscreen
->info
.use_late_alloc
) {
5188 late_alloc_wave64
= 0;
5189 } else if (num_cu_per_sh
<= 4) {
5190 /* Too few available compute units per SA. Disallowing
5191 * VS to run on one CU could hurt us more than late VS
5192 * allocation would help.
5194 * 2 is the highest safe number that allows us to keep
5197 late_alloc_wave64
= 2;
5199 /* This is a good initial value, allowing 1 late_alloc
5200 * wave per SIMD on num_cu - 2.
5202 late_alloc_wave64
= (num_cu_per_sh
- 2) * 4;
5205 if (late_alloc_wave64
> 2)
5206 cu_mask_vs
= 0xfffe; /* 1 CU disabled */
5209 /* VS can't execute on one CU if the limit is > 2. */
5210 si_pm4_set_reg(pm4
, R_00B118_SPI_SHADER_PGM_RSRC3_VS
,
5211 S_00B118_CU_EN(cu_mask_vs
) | S_00B118_WAVE_LIMIT(0x3F));
5212 si_pm4_set_reg(pm4
, R_00B11C_SPI_SHADER_LATE_ALLOC_VS
, S_00B11C_LIMIT(late_alloc_wave64
));
5213 si_pm4_set_reg(pm4
, R_00B21C_SPI_SHADER_PGM_RSRC3_GS
,
5214 S_00B21C_CU_EN(cu_mask_gs
) | S_00B21C_WAVE_LIMIT(0x3F));
5215 si_pm4_set_reg(pm4
, R_00B01C_SPI_SHADER_PGM_RSRC3_PS
,
5216 S_00B01C_CU_EN(0xffff) | S_00B01C_WAVE_LIMIT(0x3F));
5219 if (sctx
->chip_class
<= GFX8
) {
5220 si_set_raster_config(sctx
, pm4
);
5222 /* FIXME calculate these values somehow ??? */
5223 si_pm4_set_reg(pm4
, R_028A54_VGT_GS_PER_ES
, SI_GS_PER_ES
);
5224 si_pm4_set_reg(pm4
, R_028A58_VGT_ES_PER_GS
, 0x40);
5226 /* These registers, when written, also overwrite the CLEAR_STATE
5227 * context, so we can't rely on CLEAR_STATE setting them.
5228 * It would be an issue if there was another UMD changing them.
5230 si_pm4_set_reg(pm4
, R_028400_VGT_MAX_VTX_INDX
, ~0);
5231 si_pm4_set_reg(pm4
, R_028404_VGT_MIN_VTX_INDX
, 0);
5232 si_pm4_set_reg(pm4
, R_028408_VGT_INDX_OFFSET
, 0);
5235 if (sctx
->chip_class
>= GFX7
&& sctx
->chip_class
<= GFX8
) {
5236 si_pm4_set_reg(pm4
, R_00B51C_SPI_SHADER_PGM_RSRC3_LS
,
5237 S_00B51C_CU_EN(0xffff) | S_00B51C_WAVE_LIMIT(0x3F));
5238 si_pm4_set_reg(pm4
, R_00B41C_SPI_SHADER_PGM_RSRC3_HS
, S_00B41C_WAVE_LIMIT(0x3F));
5239 si_pm4_set_reg(pm4
, R_00B31C_SPI_SHADER_PGM_RSRC3_ES
,
5240 S_00B31C_CU_EN(0xffff) | S_00B31C_WAVE_LIMIT(0x3F));
5242 /* If this is 0, Bonaire can hang even if GS isn't being used.
5243 * Other chips are unaffected. These are suboptimal values,
5244 * but we don't use on-chip GS.
5246 si_pm4_set_reg(pm4
, R_028A44_VGT_GS_ONCHIP_CNTL
,
5247 S_028A44_ES_VERTS_PER_SUBGRP(64) | S_028A44_GS_PRIMS_PER_SUBGRP(4));
5250 if (sctx
->chip_class
== GFX8
) {
5251 unsigned vgt_tess_distribution
;
5253 vgt_tess_distribution
= S_028B50_ACCUM_ISOLINE(32) | S_028B50_ACCUM_TRI(11) |
5254 S_028B50_ACCUM_QUAD(11) | S_028B50_DONUT_SPLIT(16);
5256 /* Testing with Unigine Heaven extreme tesselation yielded best results
5257 * with TRAP_SPLIT = 3.
5259 if (sctx
->family
== CHIP_FIJI
|| sctx
->family
>= CHIP_POLARIS10
)
5260 vgt_tess_distribution
|= S_028B50_TRAP_SPLIT(3);
5262 si_pm4_set_reg(pm4
, R_028B50_VGT_TESS_DISTRIBUTION
, vgt_tess_distribution
);
5265 if (sscreen
->info
.chip_class
<= GFX9
) {
5266 si_pm4_set_reg(pm4
, R_028AA0_VGT_INSTANCE_STEP_RATE_0
, 1);
5269 if (sctx
->chip_class
== GFX9
) {
5270 si_pm4_set_reg(pm4
, R_030920_VGT_MAX_VTX_INDX
, ~0);
5271 si_pm4_set_reg(pm4
, R_030924_VGT_MIN_VTX_INDX
, 0);
5272 si_pm4_set_reg(pm4
, R_030928_VGT_INDX_OFFSET
, 0);
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));
5279 si_pm4_set_reg(pm4
, R_028B50_VGT_TESS_DISTRIBUTION
,
5280 S_028B50_ACCUM_ISOLINE(40) | S_028B50_ACCUM_TRI(30) | S_028B50_ACCUM_QUAD(24) |
5281 S_028B50_DONUT_SPLIT(24) | S_028B50_TRAP_SPLIT(6));
5282 si_pm4_set_reg(pm4
, R_028C48_PA_SC_BINNER_CNTL_1
,
5283 S_028C48_MAX_ALLOC_COUNT(sscreen
->info
.pbb_max_alloc_count
- 1) |
5284 S_028C48_MAX_PRIM_PER_BATCH(1023));
5285 si_pm4_set_reg(pm4
, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL
,
5286 S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1));
5288 si_pm4_set_reg(pm4
, R_030968_VGT_INSTANCE_BASE_ID
, 0);
5289 si_pm4_set_reg(pm4
, R_0301EC_CP_COHER_START_DELAY
,
5290 sctx
->chip_class
>= GFX10
? 0x20 : 0);
5293 if (sctx
->chip_class
>= GFX10
) {
5294 /* Logical CUs 16 - 31 */
5295 si_pm4_set_reg(pm4
, R_00B004_SPI_SHADER_PGM_RSRC4_PS
, S_00B004_CU_EN(0xffff));
5296 si_pm4_set_reg(pm4
, R_00B104_SPI_SHADER_PGM_RSRC4_VS
, S_00B104_CU_EN(0xffff));
5297 si_pm4_set_reg(pm4
, R_00B404_SPI_SHADER_PGM_RSRC4_HS
, S_00B404_CU_EN(0xffff));
5299 si_pm4_set_reg(pm4
, R_00B0C8_SPI_SHADER_USER_ACCUM_PS_0
, 0);
5300 si_pm4_set_reg(pm4
, R_00B0CC_SPI_SHADER_USER_ACCUM_PS_1
, 0);
5301 si_pm4_set_reg(pm4
, R_00B0D0_SPI_SHADER_USER_ACCUM_PS_2
, 0);
5302 si_pm4_set_reg(pm4
, R_00B0D4_SPI_SHADER_USER_ACCUM_PS_3
, 0);
5303 si_pm4_set_reg(pm4
, R_00B1C8_SPI_SHADER_USER_ACCUM_VS_0
, 0);
5304 si_pm4_set_reg(pm4
, R_00B1CC_SPI_SHADER_USER_ACCUM_VS_1
, 0);
5305 si_pm4_set_reg(pm4
, R_00B1D0_SPI_SHADER_USER_ACCUM_VS_2
, 0);
5306 si_pm4_set_reg(pm4
, R_00B1D4_SPI_SHADER_USER_ACCUM_VS_3
, 0);
5307 si_pm4_set_reg(pm4
, R_00B2C8_SPI_SHADER_USER_ACCUM_ESGS_0
, 0);
5308 si_pm4_set_reg(pm4
, R_00B2CC_SPI_SHADER_USER_ACCUM_ESGS_1
, 0);
5309 si_pm4_set_reg(pm4
, R_00B2D0_SPI_SHADER_USER_ACCUM_ESGS_2
, 0);
5310 si_pm4_set_reg(pm4
, R_00B2D4_SPI_SHADER_USER_ACCUM_ESGS_3
, 0);
5311 si_pm4_set_reg(pm4
, R_00B4C8_SPI_SHADER_USER_ACCUM_LSHS_0
, 0);
5312 si_pm4_set_reg(pm4
, R_00B4CC_SPI_SHADER_USER_ACCUM_LSHS_1
, 0);
5313 si_pm4_set_reg(pm4
, R_00B4D0_SPI_SHADER_USER_ACCUM_LSHS_2
, 0);
5314 si_pm4_set_reg(pm4
, R_00B4D4_SPI_SHADER_USER_ACCUM_LSHS_3
, 0);
5316 si_pm4_set_reg(pm4
, R_00B0C0_SPI_SHADER_REQ_CTRL_PS
,
5317 S_00B0C0_SOFT_GROUPING_EN(1) |
5318 S_00B0C0_NUMBER_OF_REQUESTS_PER_CU(4 - 1));
5319 si_pm4_set_reg(pm4
, R_00B1C0_SPI_SHADER_REQ_CTRL_VS
, 0);
5321 si_pm4_set_reg(pm4
, R_028428_CB_COVERAGE_OUT_CONTROL
, 0);
5322 si_pm4_set_reg(pm4
, R_028A98_VGT_DRAW_PAYLOAD_CNTL
, 0);
5324 /* Break up a pixel wave if it contains deallocs for more than
5325 * half the parameter cache.
5327 * To avoid a deadlock where pixel waves aren't launched
5328 * because they're waiting for more pixels while the frontend
5329 * is stuck waiting for PC space, the maximum allowed value is
5330 * the size of the PC minus the largest possible allocation for
5331 * a single primitive shader subgroup.
5333 si_pm4_set_reg(pm4
, R_028C50_PA_SC_NGG_MODE_CNTL
, S_028C50_MAX_DEALLOCS_IN_WAVE(512));
5334 /* Reuse for legacy (non-NGG) only. */
5335 si_pm4_set_reg(pm4
, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL
, 14);
5337 if (!has_clear_state
) {
5338 si_pm4_set_reg(pm4
, R_02835C_PA_SC_TILE_STEERING_OVERRIDE
,
5339 sscreen
->info
.pa_sc_tile_steering_override
);
5343 si_pm4_set_reg(pm4
, R_030964_GE_MAX_VTX_INDX
, ~0);
5344 si_pm4_set_reg(pm4
, R_030924_GE_MIN_VTX_INDX
, 0);
5345 si_pm4_set_reg(pm4
, R_030928_GE_INDX_OFFSET
, 0);
5346 si_pm4_set_reg(pm4
, R_03097C_GE_STEREO_CNTL
, 0);
5347 si_pm4_set_reg(pm4
, R_030988_GE_USER_VGPR_EN
, 0);
5350 if (sctx
->chip_class
>= GFX10_3
) {
5351 si_pm4_set_reg(pm4
, R_028750_SX_PS_DOWNCONVERT_CONTROL
, 0xff);
5352 si_pm4_set_reg(pm4
, 0x28848, 1 << 9); /* This fixes sample shading. */
5355 sctx
->cs_preamble_state
= pm4
;