2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "util/mesa-sha1.h"
29 #include "util/u_atomic.h"
30 #include "radv_debug.h"
31 #include "radv_private.h"
33 #include "radv_shader.h"
35 #include "nir/nir_builder.h"
36 #include "nir/nir_xfb_info.h"
37 #include "spirv/nir_spirv.h"
40 #include <llvm-c/Core.h>
41 #include <llvm-c/TargetMachine.h>
44 #include "ac_binary.h"
45 #include "ac_llvm_util.h"
46 #include "ac_nir_to_llvm.h"
47 #include "vk_format.h"
48 #include "util/debug.h"
49 #include "ac_exp_param.h"
50 #include "ac_shader_util.h"
51 #include "main/menums.h"
53 struct radv_blend_state
{
54 uint32_t blend_enable_4bit
;
55 uint32_t need_src_alpha
;
57 uint32_t cb_color_control
;
58 uint32_t cb_target_mask
;
59 uint32_t cb_target_enabled_4bit
;
60 uint32_t sx_mrt_blend_opt
[8];
61 uint32_t cb_blend_control
[8];
63 uint32_t spi_shader_col_format
;
64 uint32_t cb_shader_mask
;
65 uint32_t db_alpha_to_mask
;
67 uint32_t commutative_4bit
;
69 bool single_cb_enable
;
70 bool mrt0_is_dual_src
;
73 struct radv_dsa_order_invariance
{
74 /* Whether the final result in Z/S buffers is guaranteed to be
75 * invariant under changes to the order in which fragments arrive.
79 /* Whether the set of fragments that pass the combined Z/S test is
80 * guaranteed to be invariant under changes to the order in which
86 struct radv_tessellation_state
{
87 uint32_t ls_hs_config
;
93 struct radv_gs_state
{
94 uint32_t vgt_gs_onchip_cntl
;
95 uint32_t vgt_gs_max_prims_per_subgroup
;
96 uint32_t vgt_esgs_ring_itemsize
;
100 struct radv_ngg_state
{
101 uint16_t ngg_emit_size
; /* in dwords */
102 uint32_t hw_max_esverts
;
103 uint32_t max_gsprims
;
104 uint32_t max_out_verts
;
105 uint32_t prim_amp_factor
;
106 uint32_t vgt_esgs_ring_itemsize
;
107 bool max_vert_out_per_gs_instance
;
110 bool radv_pipeline_has_ngg(const struct radv_pipeline
*pipeline
)
112 struct radv_shader_variant
*variant
= NULL
;
113 if (pipeline
->shaders
[MESA_SHADER_GEOMETRY
])
114 variant
= pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
115 else if (pipeline
->shaders
[MESA_SHADER_TESS_EVAL
])
116 variant
= pipeline
->shaders
[MESA_SHADER_TESS_EVAL
];
117 else if (pipeline
->shaders
[MESA_SHADER_VERTEX
])
118 variant
= pipeline
->shaders
[MESA_SHADER_VERTEX
];
121 return variant
->info
.is_ngg
;
124 bool radv_pipeline_has_gs_copy_shader(const struct radv_pipeline
*pipeline
)
126 if (!radv_pipeline_has_gs(pipeline
))
129 /* The GS copy shader is required if the pipeline has GS on GFX6-GFX9.
130 * On GFX10, it might be required in rare cases if it's not possible to
133 if (radv_pipeline_has_ngg(pipeline
))
136 assert(pipeline
->gs_copy_shader
);
141 radv_pipeline_destroy(struct radv_device
*device
,
142 struct radv_pipeline
*pipeline
,
143 const VkAllocationCallbacks
* allocator
)
145 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
146 if (pipeline
->shaders
[i
])
147 radv_shader_variant_destroy(device
, pipeline
->shaders
[i
]);
149 if (pipeline
->gs_copy_shader
)
150 radv_shader_variant_destroy(device
, pipeline
->gs_copy_shader
);
153 free(pipeline
->cs
.buf
);
154 vk_free2(&device
->alloc
, allocator
, pipeline
);
157 void radv_DestroyPipeline(
159 VkPipeline _pipeline
,
160 const VkAllocationCallbacks
* pAllocator
)
162 RADV_FROM_HANDLE(radv_device
, device
, _device
);
163 RADV_FROM_HANDLE(radv_pipeline
, pipeline
, _pipeline
);
168 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
171 static uint32_t get_hash_flags(struct radv_device
*device
)
173 uint32_t hash_flags
= 0;
175 if (device
->instance
->debug_flags
& RADV_DEBUG_UNSAFE_MATH
)
176 hash_flags
|= RADV_HASH_SHADER_UNSAFE_MATH
;
177 if (device
->instance
->debug_flags
& RADV_DEBUG_NO_NGG
)
178 hash_flags
|= RADV_HASH_SHADER_NO_NGG
;
179 if (device
->instance
->perftest_flags
& RADV_PERFTEST_SISCHED
)
180 hash_flags
|= RADV_HASH_SHADER_SISCHED
;
181 if (device
->physical_device
->cs_wave_size
== 32)
182 hash_flags
|= RADV_HASH_SHADER_CS_WAVE32
;
183 if (device
->physical_device
->ps_wave_size
== 32)
184 hash_flags
|= RADV_HASH_SHADER_PS_WAVE32
;
185 if (device
->physical_device
->ge_wave_size
== 32)
186 hash_flags
|= RADV_HASH_SHADER_GE_WAVE32
;
191 radv_pipeline_scratch_init(struct radv_device
*device
,
192 struct radv_pipeline
*pipeline
)
194 unsigned scratch_bytes_per_wave
= 0;
195 unsigned max_waves
= 0;
196 unsigned min_waves
= 1;
198 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
199 if (pipeline
->shaders
[i
]) {
200 unsigned max_stage_waves
= device
->scratch_waves
;
202 scratch_bytes_per_wave
= MAX2(scratch_bytes_per_wave
,
203 pipeline
->shaders
[i
]->config
.scratch_bytes_per_wave
);
205 max_stage_waves
= MIN2(max_stage_waves
,
206 4 * device
->physical_device
->rad_info
.num_good_compute_units
*
207 (256 / pipeline
->shaders
[i
]->config
.num_vgprs
));
208 max_waves
= MAX2(max_waves
, max_stage_waves
);
212 if (pipeline
->shaders
[MESA_SHADER_COMPUTE
]) {
213 unsigned group_size
= pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[0] *
214 pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[1] *
215 pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[2];
216 min_waves
= MAX2(min_waves
, round_up_u32(group_size
, 64));
219 if (scratch_bytes_per_wave
)
220 max_waves
= MIN2(max_waves
, 0xffffffffu
/ scratch_bytes_per_wave
);
222 if (scratch_bytes_per_wave
&& max_waves
< min_waves
) {
223 /* Not really true at this moment, but will be true on first
224 * execution. Avoid having hanging shaders. */
225 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
227 pipeline
->scratch_bytes_per_wave
= scratch_bytes_per_wave
;
228 pipeline
->max_waves
= max_waves
;
232 static uint32_t si_translate_blend_logic_op(VkLogicOp op
)
235 case VK_LOGIC_OP_CLEAR
:
236 return V_028808_ROP3_CLEAR
;
237 case VK_LOGIC_OP_AND
:
238 return V_028808_ROP3_AND
;
239 case VK_LOGIC_OP_AND_REVERSE
:
240 return V_028808_ROP3_AND_REVERSE
;
241 case VK_LOGIC_OP_COPY
:
242 return V_028808_ROP3_COPY
;
243 case VK_LOGIC_OP_AND_INVERTED
:
244 return V_028808_ROP3_AND_INVERTED
;
245 case VK_LOGIC_OP_NO_OP
:
246 return V_028808_ROP3_NO_OP
;
247 case VK_LOGIC_OP_XOR
:
248 return V_028808_ROP3_XOR
;
250 return V_028808_ROP3_OR
;
251 case VK_LOGIC_OP_NOR
:
252 return V_028808_ROP3_NOR
;
253 case VK_LOGIC_OP_EQUIVALENT
:
254 return V_028808_ROP3_EQUIVALENT
;
255 case VK_LOGIC_OP_INVERT
:
256 return V_028808_ROP3_INVERT
;
257 case VK_LOGIC_OP_OR_REVERSE
:
258 return V_028808_ROP3_OR_REVERSE
;
259 case VK_LOGIC_OP_COPY_INVERTED
:
260 return V_028808_ROP3_COPY_INVERTED
;
261 case VK_LOGIC_OP_OR_INVERTED
:
262 return V_028808_ROP3_OR_INVERTED
;
263 case VK_LOGIC_OP_NAND
:
264 return V_028808_ROP3_NAND
;
265 case VK_LOGIC_OP_SET
:
266 return V_028808_ROP3_SET
;
268 unreachable("Unhandled logic op");
273 static uint32_t si_translate_blend_function(VkBlendOp op
)
276 case VK_BLEND_OP_ADD
:
277 return V_028780_COMB_DST_PLUS_SRC
;
278 case VK_BLEND_OP_SUBTRACT
:
279 return V_028780_COMB_SRC_MINUS_DST
;
280 case VK_BLEND_OP_REVERSE_SUBTRACT
:
281 return V_028780_COMB_DST_MINUS_SRC
;
282 case VK_BLEND_OP_MIN
:
283 return V_028780_COMB_MIN_DST_SRC
;
284 case VK_BLEND_OP_MAX
:
285 return V_028780_COMB_MAX_DST_SRC
;
291 static uint32_t si_translate_blend_factor(VkBlendFactor factor
)
294 case VK_BLEND_FACTOR_ZERO
:
295 return V_028780_BLEND_ZERO
;
296 case VK_BLEND_FACTOR_ONE
:
297 return V_028780_BLEND_ONE
;
298 case VK_BLEND_FACTOR_SRC_COLOR
:
299 return V_028780_BLEND_SRC_COLOR
;
300 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR
:
301 return V_028780_BLEND_ONE_MINUS_SRC_COLOR
;
302 case VK_BLEND_FACTOR_DST_COLOR
:
303 return V_028780_BLEND_DST_COLOR
;
304 case VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR
:
305 return V_028780_BLEND_ONE_MINUS_DST_COLOR
;
306 case VK_BLEND_FACTOR_SRC_ALPHA
:
307 return V_028780_BLEND_SRC_ALPHA
;
308 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
:
309 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA
;
310 case VK_BLEND_FACTOR_DST_ALPHA
:
311 return V_028780_BLEND_DST_ALPHA
;
312 case VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA
:
313 return V_028780_BLEND_ONE_MINUS_DST_ALPHA
;
314 case VK_BLEND_FACTOR_CONSTANT_COLOR
:
315 return V_028780_BLEND_CONSTANT_COLOR
;
316 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
:
317 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR
;
318 case VK_BLEND_FACTOR_CONSTANT_ALPHA
:
319 return V_028780_BLEND_CONSTANT_ALPHA
;
320 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
:
321 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA
;
322 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
:
323 return V_028780_BLEND_SRC_ALPHA_SATURATE
;
324 case VK_BLEND_FACTOR_SRC1_COLOR
:
325 return V_028780_BLEND_SRC1_COLOR
;
326 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR
:
327 return V_028780_BLEND_INV_SRC1_COLOR
;
328 case VK_BLEND_FACTOR_SRC1_ALPHA
:
329 return V_028780_BLEND_SRC1_ALPHA
;
330 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
:
331 return V_028780_BLEND_INV_SRC1_ALPHA
;
337 static uint32_t si_translate_blend_opt_function(VkBlendOp op
)
340 case VK_BLEND_OP_ADD
:
341 return V_028760_OPT_COMB_ADD
;
342 case VK_BLEND_OP_SUBTRACT
:
343 return V_028760_OPT_COMB_SUBTRACT
;
344 case VK_BLEND_OP_REVERSE_SUBTRACT
:
345 return V_028760_OPT_COMB_REVSUBTRACT
;
346 case VK_BLEND_OP_MIN
:
347 return V_028760_OPT_COMB_MIN
;
348 case VK_BLEND_OP_MAX
:
349 return V_028760_OPT_COMB_MAX
;
351 return V_028760_OPT_COMB_BLEND_DISABLED
;
355 static uint32_t si_translate_blend_opt_factor(VkBlendFactor factor
, bool is_alpha
)
358 case VK_BLEND_FACTOR_ZERO
:
359 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL
;
360 case VK_BLEND_FACTOR_ONE
:
361 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
;
362 case VK_BLEND_FACTOR_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 VK_BLEND_FACTOR_ONE_MINUS_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 VK_BLEND_FACTOR_SRC_ALPHA
:
369 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
;
370 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
:
371 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
;
372 case VK_BLEND_FACTOR_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
;
381 * Get rid of DST in the blend factors by commuting the operands:
382 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
384 static void si_blend_remove_dst(unsigned *func
, unsigned *src_factor
,
385 unsigned *dst_factor
, unsigned expected_dst
,
386 unsigned replacement_src
)
388 if (*src_factor
== expected_dst
&&
389 *dst_factor
== VK_BLEND_FACTOR_ZERO
) {
390 *src_factor
= VK_BLEND_FACTOR_ZERO
;
391 *dst_factor
= replacement_src
;
393 /* Commuting the operands requires reversing subtractions. */
394 if (*func
== VK_BLEND_OP_SUBTRACT
)
395 *func
= VK_BLEND_OP_REVERSE_SUBTRACT
;
396 else if (*func
== VK_BLEND_OP_REVERSE_SUBTRACT
)
397 *func
= VK_BLEND_OP_SUBTRACT
;
401 static bool si_blend_factor_uses_dst(unsigned factor
)
403 return factor
== VK_BLEND_FACTOR_DST_COLOR
||
404 factor
== VK_BLEND_FACTOR_DST_ALPHA
||
405 factor
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
||
406 factor
== VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA
||
407 factor
== VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR
;
410 static bool is_dual_src(VkBlendFactor factor
)
413 case VK_BLEND_FACTOR_SRC1_COLOR
:
414 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR
:
415 case VK_BLEND_FACTOR_SRC1_ALPHA
:
416 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
:
423 static unsigned si_choose_spi_color_format(VkFormat vk_format
,
425 bool blend_need_alpha
)
427 const struct vk_format_description
*desc
= vk_format_description(vk_format
);
428 unsigned format
, ntype
, swap
;
430 /* Alpha is needed for alpha-to-coverage.
431 * Blending may be with or without alpha.
433 unsigned normal
= 0; /* most optimal, may not support blending or export alpha */
434 unsigned alpha
= 0; /* exports alpha, but may not support blending */
435 unsigned blend
= 0; /* supports blending, but may not export alpha */
436 unsigned blend_alpha
= 0; /* least optimal, supports blending and exports alpha */
438 format
= radv_translate_colorformat(vk_format
);
439 ntype
= radv_translate_color_numformat(vk_format
, desc
,
440 vk_format_get_first_non_void_channel(vk_format
));
441 swap
= radv_translate_colorswap(vk_format
, false);
443 /* Choose the SPI color formats. These are required values for Stoney/RB+.
444 * Other chips have multiple choices, though they are not necessarily better.
447 case V_028C70_COLOR_5_6_5
:
448 case V_028C70_COLOR_1_5_5_5
:
449 case V_028C70_COLOR_5_5_5_1
:
450 case V_028C70_COLOR_4_4_4_4
:
451 case V_028C70_COLOR_10_11_11
:
452 case V_028C70_COLOR_11_11_10
:
453 case V_028C70_COLOR_8
:
454 case V_028C70_COLOR_8_8
:
455 case V_028C70_COLOR_8_8_8_8
:
456 case V_028C70_COLOR_10_10_10_2
:
457 case V_028C70_COLOR_2_10_10_10
:
458 if (ntype
== V_028C70_NUMBER_UINT
)
459 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_UINT16_ABGR
;
460 else if (ntype
== V_028C70_NUMBER_SINT
)
461 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_SINT16_ABGR
;
463 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_FP16_ABGR
;
466 case V_028C70_COLOR_16
:
467 case V_028C70_COLOR_16_16
:
468 case V_028C70_COLOR_16_16_16_16
:
469 if (ntype
== V_028C70_NUMBER_UNORM
||
470 ntype
== V_028C70_NUMBER_SNORM
) {
471 /* UNORM16 and SNORM16 don't support blending */
472 if (ntype
== V_028C70_NUMBER_UNORM
)
473 normal
= alpha
= V_028714_SPI_SHADER_UNORM16_ABGR
;
475 normal
= alpha
= V_028714_SPI_SHADER_SNORM16_ABGR
;
477 /* Use 32 bits per channel for blending. */
478 if (format
== V_028C70_COLOR_16
) {
479 if (swap
== V_028C70_SWAP_STD
) { /* R */
480 blend
= V_028714_SPI_SHADER_32_R
;
481 blend_alpha
= V_028714_SPI_SHADER_32_AR
;
482 } else if (swap
== V_028C70_SWAP_ALT_REV
) /* A */
483 blend
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
486 } else if (format
== V_028C70_COLOR_16_16
) {
487 if (swap
== V_028C70_SWAP_STD
) { /* RG */
488 blend
= V_028714_SPI_SHADER_32_GR
;
489 blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
490 } else if (swap
== V_028C70_SWAP_ALT
) /* RA */
491 blend
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
494 } else /* 16_16_16_16 */
495 blend
= blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
496 } else if (ntype
== V_028C70_NUMBER_UINT
)
497 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_UINT16_ABGR
;
498 else if (ntype
== V_028C70_NUMBER_SINT
)
499 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_SINT16_ABGR
;
500 else if (ntype
== V_028C70_NUMBER_FLOAT
)
501 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_FP16_ABGR
;
506 case V_028C70_COLOR_32
:
507 if (swap
== V_028C70_SWAP_STD
) { /* R */
508 blend
= normal
= V_028714_SPI_SHADER_32_R
;
509 alpha
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
510 } else if (swap
== V_028C70_SWAP_ALT_REV
) /* A */
511 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_AR
;
516 case V_028C70_COLOR_32_32
:
517 if (swap
== V_028C70_SWAP_STD
) { /* RG */
518 blend
= normal
= V_028714_SPI_SHADER_32_GR
;
519 alpha
= blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
520 } else if (swap
== V_028C70_SWAP_ALT
) /* RA */
521 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_AR
;
526 case V_028C70_COLOR_32_32_32_32
:
527 case V_028C70_COLOR_8_24
:
528 case V_028C70_COLOR_24_8
:
529 case V_028C70_COLOR_X24_8_32_FLOAT
:
530 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_ABGR
;
534 unreachable("unhandled blend format");
537 if (blend_enable
&& blend_need_alpha
)
539 else if(blend_need_alpha
)
541 else if(blend_enable
)
548 radv_pipeline_compute_spi_color_formats(struct radv_pipeline
*pipeline
,
549 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
550 struct radv_blend_state
*blend
)
552 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
553 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
554 unsigned col_format
= 0;
555 unsigned num_targets
;
557 for (unsigned i
= 0; i
< (blend
->single_cb_enable
? 1 : subpass
->color_count
); ++i
) {
560 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
) {
561 cf
= V_028714_SPI_SHADER_ZERO
;
563 struct radv_render_pass_attachment
*attachment
= pass
->attachments
+ subpass
->color_attachments
[i
].attachment
;
565 blend
->blend_enable_4bit
& (0xfu
<< (i
* 4));
567 cf
= si_choose_spi_color_format(attachment
->format
,
569 blend
->need_src_alpha
& (1 << i
));
572 col_format
|= cf
<< (4 * i
);
575 if (!(col_format
& 0xf) && blend
->need_src_alpha
& (1 << 0)) {
576 /* When a subpass doesn't have any color attachments, write the
577 * alpha channel of MRT0 when alpha coverage is enabled because
578 * the depth attachment needs it.
580 col_format
|= V_028714_SPI_SHADER_32_AR
;
583 /* If the i-th target format is set, all previous target formats must
584 * be non-zero to avoid hangs.
586 num_targets
= (util_last_bit(col_format
) + 3) / 4;
587 for (unsigned i
= 0; i
< num_targets
; i
++) {
588 if (!(col_format
& (0xf << (i
* 4)))) {
589 col_format
|= V_028714_SPI_SHADER_32_R
<< (i
* 4);
593 /* The output for dual source blending should have the same format as
596 if (blend
->mrt0_is_dual_src
)
597 col_format
|= (col_format
& 0xf) << 4;
599 blend
->cb_shader_mask
= ac_get_cb_shader_mask(col_format
);
600 blend
->spi_shader_col_format
= col_format
;
604 format_is_int8(VkFormat format
)
606 const struct vk_format_description
*desc
= vk_format_description(format
);
607 int channel
= vk_format_get_first_non_void_channel(format
);
609 return channel
>= 0 && desc
->channel
[channel
].pure_integer
&&
610 desc
->channel
[channel
].size
== 8;
614 format_is_int10(VkFormat format
)
616 const struct vk_format_description
*desc
= vk_format_description(format
);
618 if (desc
->nr_channels
!= 4)
620 for (unsigned i
= 0; i
< 4; i
++) {
621 if (desc
->channel
[i
].pure_integer
&& desc
->channel
[i
].size
== 10)
628 * Ordered so that for each i,
629 * radv_format_meta_fs_key(radv_fs_key_format_exemplars[i]) == i.
631 const VkFormat radv_fs_key_format_exemplars
[NUM_META_FS_KEYS
] = {
632 VK_FORMAT_R32_SFLOAT
,
633 VK_FORMAT_R32G32_SFLOAT
,
634 VK_FORMAT_R8G8B8A8_UNORM
,
635 VK_FORMAT_R16G16B16A16_UNORM
,
636 VK_FORMAT_R16G16B16A16_SNORM
,
637 VK_FORMAT_R16G16B16A16_UINT
,
638 VK_FORMAT_R16G16B16A16_SINT
,
639 VK_FORMAT_R32G32B32A32_SFLOAT
,
640 VK_FORMAT_R8G8B8A8_UINT
,
641 VK_FORMAT_R8G8B8A8_SINT
,
642 VK_FORMAT_A2R10G10B10_UINT_PACK32
,
643 VK_FORMAT_A2R10G10B10_SINT_PACK32
,
646 unsigned radv_format_meta_fs_key(VkFormat format
)
648 unsigned col_format
= si_choose_spi_color_format(format
, false, false);
650 assert(col_format
!= V_028714_SPI_SHADER_32_AR
);
651 if (col_format
>= V_028714_SPI_SHADER_32_AR
)
652 --col_format
; /* Skip V_028714_SPI_SHADER_32_AR since there is no such VkFormat */
654 --col_format
; /* Skip V_028714_SPI_SHADER_ZERO */
655 bool is_int8
= format_is_int8(format
);
656 bool is_int10
= format_is_int10(format
);
658 return col_format
+ (is_int8
? 3 : is_int10
? 5 : 0);
662 radv_pipeline_compute_get_int_clamp(const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
663 unsigned *is_int8
, unsigned *is_int10
)
665 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
666 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
670 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
671 struct radv_render_pass_attachment
*attachment
;
673 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
)
676 attachment
= pass
->attachments
+ subpass
->color_attachments
[i
].attachment
;
678 if (format_is_int8(attachment
->format
))
680 if (format_is_int10(attachment
->format
))
686 radv_blend_check_commutativity(struct radv_blend_state
*blend
,
687 VkBlendOp op
, VkBlendFactor src
,
688 VkBlendFactor dst
, unsigned chanmask
)
690 /* Src factor is allowed when it does not depend on Dst. */
691 static const uint32_t src_allowed
=
692 (1u << VK_BLEND_FACTOR_ONE
) |
693 (1u << VK_BLEND_FACTOR_SRC_COLOR
) |
694 (1u << VK_BLEND_FACTOR_SRC_ALPHA
) |
695 (1u << VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
) |
696 (1u << VK_BLEND_FACTOR_CONSTANT_COLOR
) |
697 (1u << VK_BLEND_FACTOR_CONSTANT_ALPHA
) |
698 (1u << VK_BLEND_FACTOR_SRC1_COLOR
) |
699 (1u << VK_BLEND_FACTOR_SRC1_ALPHA
) |
700 (1u << VK_BLEND_FACTOR_ZERO
) |
701 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR
) |
702 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
) |
703 (1u << VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
) |
704 (1u << VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
) |
705 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR
) |
706 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
);
708 if (dst
== VK_BLEND_FACTOR_ONE
&&
709 (src_allowed
& (1u << src
))) {
710 /* Addition is commutative, but floating point addition isn't
711 * associative: subtle changes can be introduced via different
712 * rounding. Be conservative, only enable for min and max.
714 if (op
== VK_BLEND_OP_MAX
|| op
== VK_BLEND_OP_MIN
)
715 blend
->commutative_4bit
|= chanmask
;
719 static struct radv_blend_state
720 radv_pipeline_init_blend_state(struct radv_pipeline
*pipeline
,
721 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
722 const struct radv_graphics_pipeline_create_info
*extra
)
724 const VkPipelineColorBlendStateCreateInfo
*vkblend
= pCreateInfo
->pColorBlendState
;
725 const VkPipelineMultisampleStateCreateInfo
*vkms
= pCreateInfo
->pMultisampleState
;
726 struct radv_blend_state blend
= {0};
727 unsigned mode
= V_028808_CB_NORMAL
;
733 if (extra
&& extra
->custom_blend_mode
) {
734 blend
.single_cb_enable
= true;
735 mode
= extra
->custom_blend_mode
;
737 blend
.cb_color_control
= 0;
738 if (vkblend
->logicOpEnable
)
739 blend
.cb_color_control
|= S_028808_ROP3(si_translate_blend_logic_op(vkblend
->logicOp
));
741 blend
.cb_color_control
|= S_028808_ROP3(V_028808_ROP3_COPY
);
743 blend
.db_alpha_to_mask
= S_028B70_ALPHA_TO_MASK_OFFSET0(3) |
744 S_028B70_ALPHA_TO_MASK_OFFSET1(1) |
745 S_028B70_ALPHA_TO_MASK_OFFSET2(0) |
746 S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
747 S_028B70_OFFSET_ROUND(1);
749 if (vkms
&& vkms
->alphaToCoverageEnable
) {
750 blend
.db_alpha_to_mask
|= S_028B70_ALPHA_TO_MASK_ENABLE(1);
751 blend
.need_src_alpha
|= 0x1;
754 blend
.cb_target_mask
= 0;
755 for (i
= 0; i
< vkblend
->attachmentCount
; i
++) {
756 const VkPipelineColorBlendAttachmentState
*att
= &vkblend
->pAttachments
[i
];
757 unsigned blend_cntl
= 0;
758 unsigned srcRGB_opt
, dstRGB_opt
, srcA_opt
, dstA_opt
;
759 VkBlendOp eqRGB
= att
->colorBlendOp
;
760 VkBlendFactor srcRGB
= att
->srcColorBlendFactor
;
761 VkBlendFactor dstRGB
= att
->dstColorBlendFactor
;
762 VkBlendOp eqA
= att
->alphaBlendOp
;
763 VkBlendFactor srcA
= att
->srcAlphaBlendFactor
;
764 VkBlendFactor dstA
= att
->dstAlphaBlendFactor
;
766 blend
.sx_mrt_blend_opt
[i
] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED
) | S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED
);
768 if (!att
->colorWriteMask
)
771 blend
.cb_target_mask
|= (unsigned)att
->colorWriteMask
<< (4 * i
);
772 blend
.cb_target_enabled_4bit
|= 0xf << (4 * i
);
773 if (!att
->blendEnable
) {
774 blend
.cb_blend_control
[i
] = blend_cntl
;
778 if (is_dual_src(srcRGB
) || is_dual_src(dstRGB
) || is_dual_src(srcA
) || is_dual_src(dstA
))
780 blend
.mrt0_is_dual_src
= true;
782 if (eqRGB
== VK_BLEND_OP_MIN
|| eqRGB
== VK_BLEND_OP_MAX
) {
783 srcRGB
= VK_BLEND_FACTOR_ONE
;
784 dstRGB
= VK_BLEND_FACTOR_ONE
;
786 if (eqA
== VK_BLEND_OP_MIN
|| eqA
== VK_BLEND_OP_MAX
) {
787 srcA
= VK_BLEND_FACTOR_ONE
;
788 dstA
= VK_BLEND_FACTOR_ONE
;
791 radv_blend_check_commutativity(&blend
, eqRGB
, srcRGB
, dstRGB
,
793 radv_blend_check_commutativity(&blend
, eqA
, srcA
, dstA
,
796 /* Blending optimizations for RB+.
797 * These transformations don't change the behavior.
799 * First, get rid of DST in the blend factors:
800 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
802 si_blend_remove_dst(&eqRGB
, &srcRGB
, &dstRGB
,
803 VK_BLEND_FACTOR_DST_COLOR
,
804 VK_BLEND_FACTOR_SRC_COLOR
);
806 si_blend_remove_dst(&eqA
, &srcA
, &dstA
,
807 VK_BLEND_FACTOR_DST_COLOR
,
808 VK_BLEND_FACTOR_SRC_COLOR
);
810 si_blend_remove_dst(&eqA
, &srcA
, &dstA
,
811 VK_BLEND_FACTOR_DST_ALPHA
,
812 VK_BLEND_FACTOR_SRC_ALPHA
);
814 /* Look up the ideal settings from tables. */
815 srcRGB_opt
= si_translate_blend_opt_factor(srcRGB
, false);
816 dstRGB_opt
= si_translate_blend_opt_factor(dstRGB
, false);
817 srcA_opt
= si_translate_blend_opt_factor(srcA
, true);
818 dstA_opt
= si_translate_blend_opt_factor(dstA
, true);
820 /* Handle interdependencies. */
821 if (si_blend_factor_uses_dst(srcRGB
))
822 dstRGB_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE
;
823 if (si_blend_factor_uses_dst(srcA
))
824 dstA_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE
;
826 if (srcRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
&&
827 (dstRGB
== VK_BLEND_FACTOR_ZERO
||
828 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA
||
829 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
))
830 dstRGB_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0
;
832 /* Set the final value. */
833 blend
.sx_mrt_blend_opt
[i
] =
834 S_028760_COLOR_SRC_OPT(srcRGB_opt
) |
835 S_028760_COLOR_DST_OPT(dstRGB_opt
) |
836 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB
)) |
837 S_028760_ALPHA_SRC_OPT(srcA_opt
) |
838 S_028760_ALPHA_DST_OPT(dstA_opt
) |
839 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA
));
840 blend_cntl
|= S_028780_ENABLE(1);
842 blend_cntl
|= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB
));
843 blend_cntl
|= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB
));
844 blend_cntl
|= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB
));
845 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
846 blend_cntl
|= S_028780_SEPARATE_ALPHA_BLEND(1);
847 blend_cntl
|= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA
));
848 blend_cntl
|= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA
));
849 blend_cntl
|= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA
));
851 blend
.cb_blend_control
[i
] = blend_cntl
;
853 blend
.blend_enable_4bit
|= 0xfu
<< (i
* 4);
855 if (srcRGB
== VK_BLEND_FACTOR_SRC_ALPHA
||
856 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA
||
857 srcRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
||
858 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
||
859 srcRGB
== VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
||
860 dstRGB
== VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
)
861 blend
.need_src_alpha
|= 1 << i
;
863 for (i
= vkblend
->attachmentCount
; i
< 8; i
++) {
864 blend
.cb_blend_control
[i
] = 0;
865 blend
.sx_mrt_blend_opt
[i
] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED
) | S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED
);
868 if (pipeline
->device
->physical_device
->has_rbplus
) {
869 /* Disable RB+ blend optimizations for dual source blending. */
870 if (blend
.mrt0_is_dual_src
) {
871 for (i
= 0; i
< 8; i
++) {
872 blend
.sx_mrt_blend_opt
[i
] =
873 S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE
) |
874 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE
);
878 /* RB+ doesn't work with dual source blending, logic op and
881 if (blend
.mrt0_is_dual_src
|| vkblend
->logicOpEnable
||
882 mode
== V_028808_CB_RESOLVE
)
883 blend
.cb_color_control
|= S_028808_DISABLE_DUAL_QUAD(1);
886 if (blend
.cb_target_mask
)
887 blend
.cb_color_control
|= S_028808_MODE(mode
);
889 blend
.cb_color_control
|= S_028808_MODE(V_028808_CB_DISABLE
);
891 radv_pipeline_compute_spi_color_formats(pipeline
, pCreateInfo
, &blend
);
895 static uint32_t si_translate_stencil_op(enum VkStencilOp op
)
898 case VK_STENCIL_OP_KEEP
:
899 return V_02842C_STENCIL_KEEP
;
900 case VK_STENCIL_OP_ZERO
:
901 return V_02842C_STENCIL_ZERO
;
902 case VK_STENCIL_OP_REPLACE
:
903 return V_02842C_STENCIL_REPLACE_TEST
;
904 case VK_STENCIL_OP_INCREMENT_AND_CLAMP
:
905 return V_02842C_STENCIL_ADD_CLAMP
;
906 case VK_STENCIL_OP_DECREMENT_AND_CLAMP
:
907 return V_02842C_STENCIL_SUB_CLAMP
;
908 case VK_STENCIL_OP_INVERT
:
909 return V_02842C_STENCIL_INVERT
;
910 case VK_STENCIL_OP_INCREMENT_AND_WRAP
:
911 return V_02842C_STENCIL_ADD_WRAP
;
912 case VK_STENCIL_OP_DECREMENT_AND_WRAP
:
913 return V_02842C_STENCIL_SUB_WRAP
;
919 static uint32_t si_translate_fill(VkPolygonMode func
)
922 case VK_POLYGON_MODE_FILL
:
923 return V_028814_X_DRAW_TRIANGLES
;
924 case VK_POLYGON_MODE_LINE
:
925 return V_028814_X_DRAW_LINES
;
926 case VK_POLYGON_MODE_POINT
:
927 return V_028814_X_DRAW_POINTS
;
930 return V_028814_X_DRAW_POINTS
;
934 static uint8_t radv_pipeline_get_ps_iter_samples(const VkPipelineMultisampleStateCreateInfo
*vkms
)
936 uint32_t num_samples
= vkms
->rasterizationSamples
;
937 uint32_t ps_iter_samples
= 1;
939 if (vkms
->sampleShadingEnable
) {
940 ps_iter_samples
= ceil(vkms
->minSampleShading
* num_samples
);
941 ps_iter_samples
= util_next_power_of_two(ps_iter_samples
);
943 return ps_iter_samples
;
947 radv_is_depth_write_enabled(const VkPipelineDepthStencilStateCreateInfo
*pCreateInfo
)
949 return pCreateInfo
->depthTestEnable
&&
950 pCreateInfo
->depthWriteEnable
&&
951 pCreateInfo
->depthCompareOp
!= VK_COMPARE_OP_NEVER
;
955 radv_writes_stencil(const VkStencilOpState
*state
)
957 return state
->writeMask
&&
958 (state
->failOp
!= VK_STENCIL_OP_KEEP
||
959 state
->passOp
!= VK_STENCIL_OP_KEEP
||
960 state
->depthFailOp
!= VK_STENCIL_OP_KEEP
);
964 radv_is_stencil_write_enabled(const VkPipelineDepthStencilStateCreateInfo
*pCreateInfo
)
966 return pCreateInfo
->stencilTestEnable
&&
967 (radv_writes_stencil(&pCreateInfo
->front
) ||
968 radv_writes_stencil(&pCreateInfo
->back
));
972 radv_is_ds_write_enabled(const VkPipelineDepthStencilStateCreateInfo
*pCreateInfo
)
974 return radv_is_depth_write_enabled(pCreateInfo
) ||
975 radv_is_stencil_write_enabled(pCreateInfo
);
979 radv_order_invariant_stencil_op(VkStencilOp op
)
981 /* REPLACE is normally order invariant, except when the stencil
982 * reference value is written by the fragment shader. Tracking this
983 * interaction does not seem worth the effort, so be conservative.
985 return op
!= VK_STENCIL_OP_INCREMENT_AND_CLAMP
&&
986 op
!= VK_STENCIL_OP_DECREMENT_AND_CLAMP
&&
987 op
!= VK_STENCIL_OP_REPLACE
;
991 radv_order_invariant_stencil_state(const VkStencilOpState
*state
)
993 /* Compute whether, assuming Z writes are disabled, this stencil state
994 * is order invariant in the sense that the set of passing fragments as
995 * well as the final stencil buffer result does not depend on the order
998 return !state
->writeMask
||
999 /* The following assumes that Z writes are disabled. */
1000 (state
->compareOp
== VK_COMPARE_OP_ALWAYS
&&
1001 radv_order_invariant_stencil_op(state
->passOp
) &&
1002 radv_order_invariant_stencil_op(state
->depthFailOp
)) ||
1003 (state
->compareOp
== VK_COMPARE_OP_NEVER
&&
1004 radv_order_invariant_stencil_op(state
->failOp
));
1008 radv_pipeline_out_of_order_rast(struct radv_pipeline
*pipeline
,
1009 struct radv_blend_state
*blend
,
1010 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1012 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
1013 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
1014 unsigned colormask
= blend
->cb_target_enabled_4bit
;
1016 if (!pipeline
->device
->physical_device
->out_of_order_rast_allowed
)
1019 /* Be conservative if a logic operation is enabled with color buffers. */
1020 if (colormask
&& pCreateInfo
->pColorBlendState
->logicOpEnable
)
1023 /* Default depth/stencil invariance when no attachment is bound. */
1024 struct radv_dsa_order_invariance dsa_order_invariant
= {
1025 .zs
= true, .pass_set
= true
1028 if (pCreateInfo
->pDepthStencilState
&&
1029 subpass
->depth_stencil_attachment
) {
1030 const VkPipelineDepthStencilStateCreateInfo
*vkds
=
1031 pCreateInfo
->pDepthStencilState
;
1032 struct radv_render_pass_attachment
*attachment
=
1033 pass
->attachments
+ subpass
->depth_stencil_attachment
->attachment
;
1034 bool has_stencil
= vk_format_is_stencil(attachment
->format
);
1035 struct radv_dsa_order_invariance order_invariance
[2];
1036 struct radv_shader_variant
*ps
=
1037 pipeline
->shaders
[MESA_SHADER_FRAGMENT
];
1039 /* Compute depth/stencil order invariance in order to know if
1040 * it's safe to enable out-of-order.
1042 bool zfunc_is_ordered
=
1043 vkds
->depthCompareOp
== VK_COMPARE_OP_NEVER
||
1044 vkds
->depthCompareOp
== VK_COMPARE_OP_LESS
||
1045 vkds
->depthCompareOp
== VK_COMPARE_OP_LESS_OR_EQUAL
||
1046 vkds
->depthCompareOp
== VK_COMPARE_OP_GREATER
||
1047 vkds
->depthCompareOp
== VK_COMPARE_OP_GREATER_OR_EQUAL
;
1049 bool nozwrite_and_order_invariant_stencil
=
1050 !radv_is_ds_write_enabled(vkds
) ||
1051 (!radv_is_depth_write_enabled(vkds
) &&
1052 radv_order_invariant_stencil_state(&vkds
->front
) &&
1053 radv_order_invariant_stencil_state(&vkds
->back
));
1055 order_invariance
[1].zs
=
1056 nozwrite_and_order_invariant_stencil
||
1057 (!radv_is_stencil_write_enabled(vkds
) &&
1059 order_invariance
[0].zs
=
1060 !radv_is_depth_write_enabled(vkds
) || zfunc_is_ordered
;
1062 order_invariance
[1].pass_set
=
1063 nozwrite_and_order_invariant_stencil
||
1064 (!radv_is_stencil_write_enabled(vkds
) &&
1065 (vkds
->depthCompareOp
== VK_COMPARE_OP_ALWAYS
||
1066 vkds
->depthCompareOp
== VK_COMPARE_OP_NEVER
));
1067 order_invariance
[0].pass_set
=
1068 !radv_is_depth_write_enabled(vkds
) ||
1069 (vkds
->depthCompareOp
== VK_COMPARE_OP_ALWAYS
||
1070 vkds
->depthCompareOp
== VK_COMPARE_OP_NEVER
);
1072 dsa_order_invariant
= order_invariance
[has_stencil
];
1073 if (!dsa_order_invariant
.zs
)
1076 /* The set of PS invocations is always order invariant,
1077 * except when early Z/S tests are requested.
1080 ps
->info
.info
.ps
.writes_memory
&&
1081 ps
->info
.fs
.early_fragment_test
&&
1082 !dsa_order_invariant
.pass_set
)
1085 /* Determine if out-of-order rasterization should be disabled
1086 * when occlusion queries are used.
1088 pipeline
->graphics
.disable_out_of_order_rast_for_occlusion
=
1089 !dsa_order_invariant
.pass_set
;
1092 /* No color buffers are enabled for writing. */
1096 unsigned blendmask
= colormask
& blend
->blend_enable_4bit
;
1099 /* Only commutative blending. */
1100 if (blendmask
& ~blend
->commutative_4bit
)
1103 if (!dsa_order_invariant
.pass_set
)
1107 if (colormask
& ~blendmask
)
1114 radv_pipeline_init_multisample_state(struct radv_pipeline
*pipeline
,
1115 struct radv_blend_state
*blend
,
1116 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1118 const VkPipelineMultisampleStateCreateInfo
*vkms
= pCreateInfo
->pMultisampleState
;
1119 struct radv_multisample_state
*ms
= &pipeline
->graphics
.ms
;
1120 unsigned num_tile_pipes
= pipeline
->device
->physical_device
->rad_info
.num_tile_pipes
;
1121 bool out_of_order_rast
= false;
1122 int ps_iter_samples
= 1;
1123 uint32_t mask
= 0xffff;
1126 ms
->num_samples
= vkms
->rasterizationSamples
;
1128 ms
->num_samples
= 1;
1131 ps_iter_samples
= radv_pipeline_get_ps_iter_samples(vkms
);
1132 if (vkms
&& !vkms
->sampleShadingEnable
&& pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.force_persample
) {
1133 ps_iter_samples
= ms
->num_samples
;
1136 const struct VkPipelineRasterizationStateRasterizationOrderAMD
*raster_order
=
1137 vk_find_struct_const(pCreateInfo
->pRasterizationState
->pNext
, PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD
);
1138 if (raster_order
&& raster_order
->rasterizationOrder
== VK_RASTERIZATION_ORDER_RELAXED_AMD
) {
1139 /* Out-of-order rasterization is explicitly enabled by the
1142 out_of_order_rast
= true;
1144 /* Determine if the driver can enable out-of-order
1145 * rasterization internally.
1148 radv_pipeline_out_of_order_rast(pipeline
, blend
, pCreateInfo
);
1151 ms
->pa_sc_line_cntl
= S_028BDC_DX10_DIAMOND_TEST_ENA(1);
1152 ms
->pa_sc_aa_config
= 0;
1153 ms
->db_eqaa
= S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
1154 S_028804_INCOHERENT_EQAA_READS(1) |
1155 S_028804_INTERPOLATE_COMP_Z(1) |
1156 S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
1157 ms
->pa_sc_mode_cntl_1
=
1158 S_028A4C_WALK_FENCE_ENABLE(1) | //TODO linear dst fixes
1159 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes
== 2 ? 2 : 3) |
1160 S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(out_of_order_rast
) |
1161 S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7) |
1163 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
1164 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
1165 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
1166 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
1167 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
1168 S_028A4C_FORCE_EOV_REZ_ENABLE(1);
1169 ms
->pa_sc_mode_cntl_0
= S_028A48_ALTERNATE_RBS_PER_TILE(pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
) |
1170 S_028A48_VPORT_SCISSOR_ENABLE(1);
1172 if (ms
->num_samples
> 1) {
1173 unsigned log_samples
= util_logbase2(ms
->num_samples
);
1174 unsigned log_ps_iter_samples
= util_logbase2(ps_iter_samples
);
1175 ms
->pa_sc_mode_cntl_0
|= S_028A48_MSAA_ENABLE(1);
1176 ms
->pa_sc_line_cntl
|= S_028BDC_EXPAND_LINE_WIDTH(1); /* CM_R_028BDC_PA_SC_LINE_CNTL */
1177 ms
->db_eqaa
|= S_028804_MAX_ANCHOR_SAMPLES(log_samples
) |
1178 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples
) |
1179 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples
) |
1180 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples
);
1181 ms
->pa_sc_aa_config
|= S_028BE0_MSAA_NUM_SAMPLES(log_samples
) |
1182 S_028BE0_MAX_SAMPLE_DIST(radv_get_default_max_sample_dist(log_samples
)) |
1183 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples
); /* CM_R_028BE0_PA_SC_AA_CONFIG */
1184 ms
->pa_sc_mode_cntl_1
|= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples
> 1);
1185 if (ps_iter_samples
> 1)
1186 pipeline
->graphics
.spi_baryc_cntl
|= S_0286E0_POS_FLOAT_LOCATION(2);
1189 if (vkms
&& vkms
->pSampleMask
) {
1190 mask
= vkms
->pSampleMask
[0] & 0xffff;
1193 ms
->pa_sc_aa_mask
[0] = mask
| (mask
<< 16);
1194 ms
->pa_sc_aa_mask
[1] = mask
| (mask
<< 16);
1198 radv_prim_can_use_guardband(enum VkPrimitiveTopology topology
)
1201 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST
:
1202 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST
:
1203 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
:
1204 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
1205 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
1207 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
:
1208 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
:
1209 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
:
1210 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
1211 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
1212 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
:
1215 unreachable("unhandled primitive type");
1220 si_translate_prim(enum VkPrimitiveTopology topology
)
1223 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST
:
1224 return V_008958_DI_PT_POINTLIST
;
1225 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST
:
1226 return V_008958_DI_PT_LINELIST
;
1227 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
:
1228 return V_008958_DI_PT_LINESTRIP
;
1229 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
:
1230 return V_008958_DI_PT_TRILIST
;
1231 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
:
1232 return V_008958_DI_PT_TRISTRIP
;
1233 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
:
1234 return V_008958_DI_PT_TRIFAN
;
1235 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
1236 return V_008958_DI_PT_LINELIST_ADJ
;
1237 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
1238 return V_008958_DI_PT_LINESTRIP_ADJ
;
1239 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
1240 return V_008958_DI_PT_TRILIST_ADJ
;
1241 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
1242 return V_008958_DI_PT_TRISTRIP_ADJ
;
1243 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
:
1244 return V_008958_DI_PT_PATCH
;
1252 si_conv_gl_prim_to_gs_out(unsigned gl_prim
)
1255 case 0: /* GL_POINTS */
1256 return V_028A6C_OUTPRIM_TYPE_POINTLIST
;
1257 case 1: /* GL_LINES */
1258 case 3: /* GL_LINE_STRIP */
1259 case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */
1260 case 0x8E7A: /* GL_ISOLINES */
1261 return V_028A6C_OUTPRIM_TYPE_LINESTRIP
;
1263 case 4: /* GL_TRIANGLES */
1264 case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */
1265 case 5: /* GL_TRIANGLE_STRIP */
1266 case 7: /* GL_QUADS */
1267 return V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
1275 si_conv_prim_to_gs_out(enum VkPrimitiveTopology topology
)
1278 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST
:
1279 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
:
1280 return V_028A6C_OUTPRIM_TYPE_POINTLIST
;
1281 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST
:
1282 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
:
1283 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
1284 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
1285 return V_028A6C_OUTPRIM_TYPE_LINESTRIP
;
1286 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
:
1287 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
:
1288 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
:
1289 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
1290 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
1291 return V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
1298 static unsigned radv_dynamic_state_mask(VkDynamicState state
)
1301 case VK_DYNAMIC_STATE_VIEWPORT
:
1302 return RADV_DYNAMIC_VIEWPORT
;
1303 case VK_DYNAMIC_STATE_SCISSOR
:
1304 return RADV_DYNAMIC_SCISSOR
;
1305 case VK_DYNAMIC_STATE_LINE_WIDTH
:
1306 return RADV_DYNAMIC_LINE_WIDTH
;
1307 case VK_DYNAMIC_STATE_DEPTH_BIAS
:
1308 return RADV_DYNAMIC_DEPTH_BIAS
;
1309 case VK_DYNAMIC_STATE_BLEND_CONSTANTS
:
1310 return RADV_DYNAMIC_BLEND_CONSTANTS
;
1311 case VK_DYNAMIC_STATE_DEPTH_BOUNDS
:
1312 return RADV_DYNAMIC_DEPTH_BOUNDS
;
1313 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
:
1314 return RADV_DYNAMIC_STENCIL_COMPARE_MASK
;
1315 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
:
1316 return RADV_DYNAMIC_STENCIL_WRITE_MASK
;
1317 case VK_DYNAMIC_STATE_STENCIL_REFERENCE
:
1318 return RADV_DYNAMIC_STENCIL_REFERENCE
;
1319 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT
:
1320 return RADV_DYNAMIC_DISCARD_RECTANGLE
;
1321 case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT
:
1322 return RADV_DYNAMIC_SAMPLE_LOCATIONS
;
1324 unreachable("Unhandled dynamic state");
1328 static uint32_t radv_pipeline_needed_dynamic_state(const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1330 uint32_t states
= RADV_DYNAMIC_ALL
;
1332 /* If rasterization is disabled we do not care about any of the dynamic states,
1333 * since they are all rasterization related only. */
1334 if (pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
)
1337 if (!pCreateInfo
->pRasterizationState
->depthBiasEnable
)
1338 states
&= ~RADV_DYNAMIC_DEPTH_BIAS
;
1340 if (!pCreateInfo
->pDepthStencilState
||
1341 !pCreateInfo
->pDepthStencilState
->depthBoundsTestEnable
)
1342 states
&= ~RADV_DYNAMIC_DEPTH_BOUNDS
;
1344 if (!pCreateInfo
->pDepthStencilState
||
1345 !pCreateInfo
->pDepthStencilState
->stencilTestEnable
)
1346 states
&= ~(RADV_DYNAMIC_STENCIL_COMPARE_MASK
|
1347 RADV_DYNAMIC_STENCIL_WRITE_MASK
|
1348 RADV_DYNAMIC_STENCIL_REFERENCE
);
1350 if (!vk_find_struct_const(pCreateInfo
->pNext
, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT
))
1351 states
&= ~RADV_DYNAMIC_DISCARD_RECTANGLE
;
1353 if (!pCreateInfo
->pMultisampleState
||
1354 !vk_find_struct_const(pCreateInfo
->pMultisampleState
->pNext
,
1355 PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT
))
1356 states
&= ~RADV_DYNAMIC_SAMPLE_LOCATIONS
;
1358 /* TODO: blend constants & line width. */
1365 radv_pipeline_init_dynamic_state(struct radv_pipeline
*pipeline
,
1366 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1368 uint32_t needed_states
= radv_pipeline_needed_dynamic_state(pCreateInfo
);
1369 uint32_t states
= needed_states
;
1370 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
1371 struct radv_subpass
*subpass
= &pass
->subpasses
[pCreateInfo
->subpass
];
1373 pipeline
->dynamic_state
= default_dynamic_state
;
1374 pipeline
->graphics
.needed_dynamic_state
= needed_states
;
1376 if (pCreateInfo
->pDynamicState
) {
1377 /* Remove all of the states that are marked as dynamic */
1378 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
1379 for (uint32_t s
= 0; s
< count
; s
++)
1380 states
&= ~radv_dynamic_state_mask(pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
1383 struct radv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
1385 if (needed_states
& RADV_DYNAMIC_VIEWPORT
) {
1386 assert(pCreateInfo
->pViewportState
);
1388 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
1389 if (states
& RADV_DYNAMIC_VIEWPORT
) {
1390 typed_memcpy(dynamic
->viewport
.viewports
,
1391 pCreateInfo
->pViewportState
->pViewports
,
1392 pCreateInfo
->pViewportState
->viewportCount
);
1396 if (needed_states
& RADV_DYNAMIC_SCISSOR
) {
1397 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
1398 if (states
& RADV_DYNAMIC_SCISSOR
) {
1399 typed_memcpy(dynamic
->scissor
.scissors
,
1400 pCreateInfo
->pViewportState
->pScissors
,
1401 pCreateInfo
->pViewportState
->scissorCount
);
1405 if (states
& RADV_DYNAMIC_LINE_WIDTH
) {
1406 assert(pCreateInfo
->pRasterizationState
);
1407 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
1410 if (states
& RADV_DYNAMIC_DEPTH_BIAS
) {
1411 assert(pCreateInfo
->pRasterizationState
);
1412 dynamic
->depth_bias
.bias
=
1413 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
1414 dynamic
->depth_bias
.clamp
=
1415 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
1416 dynamic
->depth_bias
.slope
=
1417 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
1420 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1422 * pColorBlendState is [...] NULL if the pipeline has rasterization
1423 * disabled or if the subpass of the render pass the pipeline is
1424 * created against does not use any color attachments.
1426 if (subpass
->has_color_att
&& states
& RADV_DYNAMIC_BLEND_CONSTANTS
) {
1427 assert(pCreateInfo
->pColorBlendState
);
1428 typed_memcpy(dynamic
->blend_constants
,
1429 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
1432 /* If there is no depthstencil attachment, then don't read
1433 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1434 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1435 * no need to override the depthstencil defaults in
1436 * radv_pipeline::dynamic_state when there is no depthstencil attachment.
1438 * Section 9.2 of the Vulkan 1.0.15 spec says:
1440 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1441 * disabled or if the subpass of the render pass the pipeline is created
1442 * against does not use a depth/stencil attachment.
1444 if (needed_states
&& subpass
->depth_stencil_attachment
) {
1445 assert(pCreateInfo
->pDepthStencilState
);
1447 if (states
& RADV_DYNAMIC_DEPTH_BOUNDS
) {
1448 dynamic
->depth_bounds
.min
=
1449 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
1450 dynamic
->depth_bounds
.max
=
1451 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
1454 if (states
& RADV_DYNAMIC_STENCIL_COMPARE_MASK
) {
1455 dynamic
->stencil_compare_mask
.front
=
1456 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
1457 dynamic
->stencil_compare_mask
.back
=
1458 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
1461 if (states
& RADV_DYNAMIC_STENCIL_WRITE_MASK
) {
1462 dynamic
->stencil_write_mask
.front
=
1463 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
1464 dynamic
->stencil_write_mask
.back
=
1465 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
1468 if (states
& RADV_DYNAMIC_STENCIL_REFERENCE
) {
1469 dynamic
->stencil_reference
.front
=
1470 pCreateInfo
->pDepthStencilState
->front
.reference
;
1471 dynamic
->stencil_reference
.back
=
1472 pCreateInfo
->pDepthStencilState
->back
.reference
;
1476 const VkPipelineDiscardRectangleStateCreateInfoEXT
*discard_rectangle_info
=
1477 vk_find_struct_const(pCreateInfo
->pNext
, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT
);
1478 if (needed_states
& RADV_DYNAMIC_DISCARD_RECTANGLE
) {
1479 dynamic
->discard_rectangle
.count
= discard_rectangle_info
->discardRectangleCount
;
1480 if (states
& RADV_DYNAMIC_DISCARD_RECTANGLE
) {
1481 typed_memcpy(dynamic
->discard_rectangle
.rectangles
,
1482 discard_rectangle_info
->pDiscardRectangles
,
1483 discard_rectangle_info
->discardRectangleCount
);
1487 if (needed_states
& RADV_DYNAMIC_SAMPLE_LOCATIONS
) {
1488 const VkPipelineSampleLocationsStateCreateInfoEXT
*sample_location_info
=
1489 vk_find_struct_const(pCreateInfo
->pMultisampleState
->pNext
,
1490 PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT
);
1491 /* If sampleLocationsEnable is VK_FALSE, the default sample
1492 * locations are used and the values specified in
1493 * sampleLocationsInfo are ignored.
1495 if (sample_location_info
->sampleLocationsEnable
) {
1496 const VkSampleLocationsInfoEXT
*pSampleLocationsInfo
=
1497 &sample_location_info
->sampleLocationsInfo
;
1499 assert(pSampleLocationsInfo
->sampleLocationsCount
<= MAX_SAMPLE_LOCATIONS
);
1501 dynamic
->sample_location
.per_pixel
= pSampleLocationsInfo
->sampleLocationsPerPixel
;
1502 dynamic
->sample_location
.grid_size
= pSampleLocationsInfo
->sampleLocationGridSize
;
1503 dynamic
->sample_location
.count
= pSampleLocationsInfo
->sampleLocationsCount
;
1504 typed_memcpy(&dynamic
->sample_location
.locations
[0],
1505 pSampleLocationsInfo
->pSampleLocations
,
1506 pSampleLocationsInfo
->sampleLocationsCount
);
1510 pipeline
->dynamic_state
.mask
= states
;
1513 static struct radv_gs_state
1514 calculate_gs_info(const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1515 const struct radv_pipeline
*pipeline
)
1517 struct radv_gs_state gs
= {0};
1518 struct radv_shader_variant_info
*gs_info
= &pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
;
1519 struct radv_es_output_info
*es_info
;
1520 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
)
1521 es_info
= radv_pipeline_has_tess(pipeline
) ? &gs_info
->tes
.es_info
: &gs_info
->vs
.es_info
;
1523 es_info
= radv_pipeline_has_tess(pipeline
) ?
1524 &pipeline
->shaders
[MESA_SHADER_TESS_EVAL
]->info
.tes
.es_info
:
1525 &pipeline
->shaders
[MESA_SHADER_VERTEX
]->info
.vs
.es_info
;
1527 unsigned gs_num_invocations
= MAX2(gs_info
->gs
.invocations
, 1);
1528 bool uses_adjacency
;
1529 switch(pCreateInfo
->pInputAssemblyState
->topology
) {
1530 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
1531 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
1532 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
1533 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
1534 uses_adjacency
= true;
1537 uses_adjacency
= false;
1541 /* All these are in dwords: */
1542 /* We can't allow using the whole LDS, because GS waves compete with
1543 * other shader stages for LDS space. */
1544 const unsigned max_lds_size
= 8 * 1024;
1545 const unsigned esgs_itemsize
= es_info
->esgs_itemsize
/ 4;
1546 unsigned esgs_lds_size
;
1548 /* All these are per subgroup: */
1549 const unsigned max_out_prims
= 32 * 1024;
1550 const unsigned max_es_verts
= 255;
1551 const unsigned ideal_gs_prims
= 64;
1552 unsigned max_gs_prims
, gs_prims
;
1553 unsigned min_es_verts
, es_verts
, worst_case_es_verts
;
1555 if (uses_adjacency
|| gs_num_invocations
> 1)
1556 max_gs_prims
= 127 / gs_num_invocations
;
1560 /* MAX_PRIMS_PER_SUBGROUP = gs_prims * max_vert_out * gs_invocations.
1561 * Make sure we don't go over the maximum value.
1563 if (gs_info
->gs
.vertices_out
> 0) {
1564 max_gs_prims
= MIN2(max_gs_prims
,
1566 (gs_info
->gs
.vertices_out
* gs_num_invocations
));
1568 assert(max_gs_prims
> 0);
1570 /* If the primitive has adjacency, halve the number of vertices
1571 * that will be reused in multiple primitives.
1573 min_es_verts
= gs_info
->gs
.vertices_in
/ (uses_adjacency
? 2 : 1);
1575 gs_prims
= MIN2(ideal_gs_prims
, max_gs_prims
);
1576 worst_case_es_verts
= MIN2(min_es_verts
* gs_prims
, max_es_verts
);
1578 /* Compute ESGS LDS size based on the worst case number of ES vertices
1579 * needed to create the target number of GS prims per subgroup.
1581 esgs_lds_size
= esgs_itemsize
* worst_case_es_verts
;
1583 /* If total LDS usage is too big, refactor partitions based on ratio
1584 * of ESGS item sizes.
1586 if (esgs_lds_size
> max_lds_size
) {
1587 /* Our target GS Prims Per Subgroup was too large. Calculate
1588 * the maximum number of GS Prims Per Subgroup that will fit
1589 * into LDS, capped by the maximum that the hardware can support.
1591 gs_prims
= MIN2((max_lds_size
/ (esgs_itemsize
* min_es_verts
)),
1593 assert(gs_prims
> 0);
1594 worst_case_es_verts
= MIN2(min_es_verts
* gs_prims
,
1597 esgs_lds_size
= esgs_itemsize
* worst_case_es_verts
;
1598 assert(esgs_lds_size
<= max_lds_size
);
1601 /* Now calculate remaining ESGS information. */
1603 es_verts
= MIN2(esgs_lds_size
/ esgs_itemsize
, max_es_verts
);
1605 es_verts
= max_es_verts
;
1607 /* Vertices for adjacency primitives are not always reused, so restore
1608 * it for ES_VERTS_PER_SUBGRP.
1610 min_es_verts
= gs_info
->gs
.vertices_in
;
1612 /* For normal primitives, the VGT only checks if they are past the ES
1613 * verts per subgroup after allocating a full GS primitive and if they
1614 * are, kick off a new subgroup. But if those additional ES verts are
1615 * unique (e.g. not reused) we need to make sure there is enough LDS
1616 * space to account for those ES verts beyond ES_VERTS_PER_SUBGRP.
1618 es_verts
-= min_es_verts
- 1;
1620 uint32_t es_verts_per_subgroup
= es_verts
;
1621 uint32_t gs_prims_per_subgroup
= gs_prims
;
1622 uint32_t gs_inst_prims_in_subgroup
= gs_prims
* gs_num_invocations
;
1623 uint32_t max_prims_per_subgroup
= gs_inst_prims_in_subgroup
* gs_info
->gs
.vertices_out
;
1624 gs
.lds_size
= align(esgs_lds_size
, 128) / 128;
1625 gs
.vgt_gs_onchip_cntl
= S_028A44_ES_VERTS_PER_SUBGRP(es_verts_per_subgroup
) |
1626 S_028A44_GS_PRIMS_PER_SUBGRP(gs_prims_per_subgroup
) |
1627 S_028A44_GS_INST_PRIMS_IN_SUBGRP(gs_inst_prims_in_subgroup
);
1628 gs
.vgt_gs_max_prims_per_subgroup
= S_028A94_MAX_PRIMS_PER_SUBGROUP(max_prims_per_subgroup
);
1629 gs
.vgt_esgs_ring_itemsize
= esgs_itemsize
;
1630 assert(max_prims_per_subgroup
<= max_out_prims
);
1635 static void clamp_gsprims_to_esverts(unsigned *max_gsprims
, unsigned max_esverts
,
1636 unsigned min_verts_per_prim
, bool use_adjacency
)
1638 unsigned max_reuse
= max_esverts
- min_verts_per_prim
;
1641 *max_gsprims
= MIN2(*max_gsprims
, 1 + max_reuse
);
1645 radv_get_num_input_vertices(struct radv_pipeline
*pipeline
)
1647 if (radv_pipeline_has_gs(pipeline
)) {
1648 struct radv_shader_variant
*gs
=
1649 radv_get_shader(pipeline
, MESA_SHADER_GEOMETRY
);
1651 return gs
->info
.gs
.vertices_in
;
1654 if (radv_pipeline_has_tess(pipeline
)) {
1655 struct radv_shader_variant
*tes
= radv_get_shader(pipeline
, MESA_SHADER_TESS_EVAL
);
1657 if (tes
->info
.tes
.point_mode
)
1659 if (tes
->info
.tes
.primitive_mode
== GL_ISOLINES
)
1667 static struct radv_ngg_state
1668 calculate_ngg_info(const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1669 struct radv_pipeline
*pipeline
)
1671 struct radv_ngg_state ngg
= {0};
1672 struct radv_shader_variant_info
*gs_info
= &pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
;
1673 struct radv_es_output_info
*es_info
=
1674 radv_pipeline_has_tess(pipeline
) ? &gs_info
->tes
.es_info
: &gs_info
->vs
.es_info
;
1675 unsigned gs_type
= radv_pipeline_has_gs(pipeline
) ? MESA_SHADER_GEOMETRY
: MESA_SHADER_VERTEX
;
1676 unsigned max_verts_per_prim
= radv_get_num_input_vertices(pipeline
);
1677 unsigned min_verts_per_prim
=
1678 gs_type
== MESA_SHADER_GEOMETRY
? max_verts_per_prim
: 1;
1679 unsigned gs_num_invocations
= radv_pipeline_has_gs(pipeline
) ? MAX2(gs_info
->gs
.invocations
, 1) : 1;
1680 bool uses_adjacency
;
1681 switch(pCreateInfo
->pInputAssemblyState
->topology
) {
1682 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
1683 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
1684 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
1685 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
1686 uses_adjacency
= true;
1689 uses_adjacency
= false;
1693 /* All these are in dwords: */
1694 /* We can't allow using the whole LDS, because GS waves compete with
1695 * other shader stages for LDS space.
1697 * Streamout can increase the ESGS buffer size later on, so be more
1698 * conservative with streamout and use 4K dwords. This may be suboptimal.
1700 * Otherwise, use the limit of 7K dwords. The reason is that we need
1701 * to leave some headroom for the max_esverts increase at the end.
1703 * TODO: We should really take the shader's internal LDS use into
1704 * account. The linker will fail if the size is greater than
1707 const unsigned max_lds_size
= (0 /*gs_info->info.so.num_outputs*/ ? 4 : 7) * 1024 - 128;
1708 const unsigned target_lds_size
= max_lds_size
;
1709 unsigned esvert_lds_size
= 0;
1710 unsigned gsprim_lds_size
= 0;
1712 /* All these are per subgroup: */
1713 bool max_vert_out_per_gs_instance
= false;
1714 unsigned max_esverts_base
= 256;
1715 unsigned max_gsprims_base
= 128; /* default prim group size clamp */
1717 /* Hardware has the following non-natural restrictions on the value
1718 * of GE_CNTL.VERT_GRP_SIZE based on based on the primitive type of
1720 * - at most 252 for any line input primitive type
1721 * - at most 251 for any quad input primitive type
1722 * - at most 251 for triangle strips with adjacency (this happens to
1723 * be the natural limit for triangle *lists* with adjacency)
1725 max_esverts_base
= MIN2(max_esverts_base
, 251 + max_verts_per_prim
- 1);
1727 if (gs_type
== MESA_SHADER_GEOMETRY
) {
1728 unsigned max_out_verts_per_gsprim
=
1729 gs_info
->gs
.vertices_out
* gs_num_invocations
;
1731 if (max_out_verts_per_gsprim
<= 256) {
1732 if (max_out_verts_per_gsprim
) {
1733 max_gsprims_base
= MIN2(max_gsprims_base
,
1734 256 / max_out_verts_per_gsprim
);
1737 /* Use special multi-cycling mode in which each GS
1738 * instance gets its own subgroup. Does not work with
1740 max_vert_out_per_gs_instance
= true;
1741 max_gsprims_base
= 1;
1742 max_out_verts_per_gsprim
= gs_info
->gs
.vertices_out
;
1745 esvert_lds_size
= es_info
->esgs_itemsize
/ 4;
1746 gsprim_lds_size
= (gs_info
->gs
.gsvs_vertex_size
/ 4 + 1) * max_out_verts_per_gsprim
;
1748 /* TODO: This needs to be adjusted once LDS use for compaction
1749 * after culling is implemented. */
1751 if (es_info->info.so.num_outputs)
1752 esvert_lds_size = 4 * es_info->info.so.num_outputs + 1;
1756 unsigned max_gsprims
= max_gsprims_base
;
1757 unsigned max_esverts
= max_esverts_base
;
1759 if (esvert_lds_size
)
1760 max_esverts
= MIN2(max_esverts
, target_lds_size
/ esvert_lds_size
);
1761 if (gsprim_lds_size
)
1762 max_gsprims
= MIN2(max_gsprims
, target_lds_size
/ gsprim_lds_size
);
1764 max_esverts
= MIN2(max_esverts
, max_gsprims
* max_verts_per_prim
);
1765 clamp_gsprims_to_esverts(&max_gsprims
, max_esverts
, min_verts_per_prim
, uses_adjacency
);
1766 assert(max_esverts
>= max_verts_per_prim
&& max_gsprims
>= 1);
1768 if (esvert_lds_size
|| gsprim_lds_size
) {
1769 /* Now that we have a rough proportionality between esverts
1770 * and gsprims based on the primitive type, scale both of them
1771 * down simultaneously based on required LDS space.
1773 * We could be smarter about this if we knew how much vertex
1776 unsigned lds_total
= max_esverts
* esvert_lds_size
+
1777 max_gsprims
* gsprim_lds_size
;
1778 if (lds_total
> target_lds_size
) {
1779 max_esverts
= max_esverts
* target_lds_size
/ lds_total
;
1780 max_gsprims
= max_gsprims
* target_lds_size
/ lds_total
;
1782 max_esverts
= MIN2(max_esverts
, max_gsprims
* max_verts_per_prim
);
1783 clamp_gsprims_to_esverts(&max_gsprims
, max_esverts
,
1784 min_verts_per_prim
, uses_adjacency
);
1785 assert(max_esverts
>= max_verts_per_prim
&& max_gsprims
>= 1);
1789 /* Round up towards full wave sizes for better ALU utilization. */
1790 if (!max_vert_out_per_gs_instance
) {
1791 const unsigned wavesize
= pipeline
->device
->physical_device
->ge_wave_size
;
1792 unsigned orig_max_esverts
;
1793 unsigned orig_max_gsprims
;
1795 orig_max_esverts
= max_esverts
;
1796 orig_max_gsprims
= max_gsprims
;
1798 max_esverts
= align(max_esverts
, wavesize
);
1799 max_esverts
= MIN2(max_esverts
, max_esverts_base
);
1800 if (esvert_lds_size
)
1801 max_esverts
= MIN2(max_esverts
,
1802 (max_lds_size
- max_gsprims
* gsprim_lds_size
) /
1804 max_esverts
= MIN2(max_esverts
, max_gsprims
* max_verts_per_prim
);
1806 max_gsprims
= align(max_gsprims
, wavesize
);
1807 max_gsprims
= MIN2(max_gsprims
, max_gsprims_base
);
1808 if (gsprim_lds_size
)
1809 max_gsprims
= MIN2(max_gsprims
,
1810 (max_lds_size
- max_esverts
* esvert_lds_size
) /
1812 clamp_gsprims_to_esverts(&max_gsprims
, max_esverts
,
1813 min_verts_per_prim
, uses_adjacency
);
1814 assert(max_esverts
>= max_verts_per_prim
&& max_gsprims
>= 1);
1815 } while (orig_max_esverts
!= max_esverts
|| orig_max_gsprims
!= max_gsprims
);
1818 /* Hardware restriction: minimum value of max_esverts */
1819 max_esverts
= MAX2(max_esverts
, 23 + max_verts_per_prim
);
1821 unsigned max_out_vertices
=
1822 max_vert_out_per_gs_instance
? gs_info
->gs
.vertices_out
:
1823 gs_type
== MESA_SHADER_GEOMETRY
?
1824 max_gsprims
* gs_num_invocations
* gs_info
->gs
.vertices_out
:
1826 assert(max_out_vertices
<= 256);
1828 unsigned prim_amp_factor
= 1;
1829 if (gs_type
== MESA_SHADER_GEOMETRY
) {
1830 /* Number of output primitives per GS input primitive after
1832 prim_amp_factor
= gs_info
->gs
.vertices_out
;
1835 /* The GE only checks against the maximum number of ES verts after
1836 * allocating a full GS primitive. So we need to ensure that whenever
1837 * this check passes, there is enough space for a full primitive without
1840 ngg
.hw_max_esverts
= max_esverts
- max_verts_per_prim
+ 1;
1841 ngg
.max_gsprims
= max_gsprims
;
1842 ngg
.max_out_verts
= max_out_vertices
;
1843 ngg
.prim_amp_factor
= prim_amp_factor
;
1844 ngg
.max_vert_out_per_gs_instance
= max_vert_out_per_gs_instance
;
1845 ngg
.ngg_emit_size
= max_gsprims
* gsprim_lds_size
;
1847 if (gs_type
== MESA_SHADER_GEOMETRY
) {
1848 ngg
.vgt_esgs_ring_itemsize
= es_info
->esgs_itemsize
/ 4;
1850 ngg
.vgt_esgs_ring_itemsize
= 1;
1853 pipeline
->graphics
.esgs_ring_size
= 4 * max_esverts
* esvert_lds_size
;
1855 assert(ngg
.hw_max_esverts
>= 24); /* HW limitation */
1861 calculate_gs_ring_sizes(struct radv_pipeline
*pipeline
, const struct radv_gs_state
*gs
)
1863 struct radv_device
*device
= pipeline
->device
;
1864 unsigned num_se
= device
->physical_device
->rad_info
.max_se
;
1865 unsigned wave_size
= 64;
1866 unsigned max_gs_waves
= 32 * num_se
; /* max 32 per SE on GCN */
1867 /* On GFX6-GFX7, the value comes from VGT_GS_VERTEX_REUSE = 16.
1868 * On GFX8+, the value comes from VGT_VERTEX_REUSE_BLOCK_CNTL = 30 (+2).
1870 unsigned gs_vertex_reuse
=
1871 (device
->physical_device
->rad_info
.chip_class
>= GFX8
? 32 : 16) * num_se
;
1872 unsigned alignment
= 256 * num_se
;
1873 /* The maximum size is 63.999 MB per SE. */
1874 unsigned max_size
= ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se
;
1875 struct radv_shader_variant_info
*gs_info
= &pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
;
1877 /* Calculate the minimum size. */
1878 unsigned min_esgs_ring_size
= align(gs
->vgt_esgs_ring_itemsize
* 4 * gs_vertex_reuse
*
1879 wave_size
, alignment
);
1880 /* These are recommended sizes, not minimum sizes. */
1881 unsigned esgs_ring_size
= max_gs_waves
* 2 * wave_size
*
1882 gs
->vgt_esgs_ring_itemsize
* 4 * gs_info
->gs
.vertices_in
;
1883 unsigned gsvs_ring_size
= max_gs_waves
* 2 * wave_size
*
1884 gs_info
->gs
.max_gsvs_emit_size
;
1886 min_esgs_ring_size
= align(min_esgs_ring_size
, alignment
);
1887 esgs_ring_size
= align(esgs_ring_size
, alignment
);
1888 gsvs_ring_size
= align(gsvs_ring_size
, alignment
);
1890 if (pipeline
->device
->physical_device
->rad_info
.chip_class
<= GFX8
)
1891 pipeline
->graphics
.esgs_ring_size
= CLAMP(esgs_ring_size
, min_esgs_ring_size
, max_size
);
1893 pipeline
->graphics
.gsvs_ring_size
= MIN2(gsvs_ring_size
, max_size
);
1896 static void si_multiwave_lds_size_workaround(struct radv_device
*device
,
1899 /* If tessellation is all offchip and on-chip GS isn't used, this
1900 * workaround is not needed.
1904 /* SPI barrier management bug:
1905 * Make sure we have at least 4k of LDS in use to avoid the bug.
1906 * It applies to workgroup sizes of more than one wavefront.
1908 if (device
->physical_device
->rad_info
.family
== CHIP_BONAIRE
||
1909 device
->physical_device
->rad_info
.family
== CHIP_KABINI
)
1910 *lds_size
= MAX2(*lds_size
, 8);
1913 struct radv_shader_variant
*
1914 radv_get_shader(struct radv_pipeline
*pipeline
,
1915 gl_shader_stage stage
)
1917 if (stage
== MESA_SHADER_VERTEX
) {
1918 if (pipeline
->shaders
[MESA_SHADER_VERTEX
])
1919 return pipeline
->shaders
[MESA_SHADER_VERTEX
];
1920 if (pipeline
->shaders
[MESA_SHADER_TESS_CTRL
])
1921 return pipeline
->shaders
[MESA_SHADER_TESS_CTRL
];
1922 if (pipeline
->shaders
[MESA_SHADER_GEOMETRY
])
1923 return pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
1924 } else if (stage
== MESA_SHADER_TESS_EVAL
) {
1925 if (!radv_pipeline_has_tess(pipeline
))
1927 if (pipeline
->shaders
[MESA_SHADER_TESS_EVAL
])
1928 return pipeline
->shaders
[MESA_SHADER_TESS_EVAL
];
1929 if (pipeline
->shaders
[MESA_SHADER_GEOMETRY
])
1930 return pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
1932 return pipeline
->shaders
[stage
];
1935 static struct radv_tessellation_state
1936 calculate_tess_state(struct radv_pipeline
*pipeline
,
1937 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1939 unsigned num_tcs_input_cp
;
1940 unsigned num_tcs_output_cp
;
1942 unsigned num_patches
;
1943 struct radv_tessellation_state tess
= {0};
1945 num_tcs_input_cp
= pCreateInfo
->pTessellationState
->patchControlPoints
;
1946 num_tcs_output_cp
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.tcs
.tcs_vertices_out
; //TCS VERTICES OUT
1947 num_patches
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.tcs
.num_patches
;
1949 lds_size
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.tcs
.lds_size
;
1951 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX7
) {
1952 assert(lds_size
<= 65536);
1953 lds_size
= align(lds_size
, 512) / 512;
1955 assert(lds_size
<= 32768);
1956 lds_size
= align(lds_size
, 256) / 256;
1958 si_multiwave_lds_size_workaround(pipeline
->device
, &lds_size
);
1960 tess
.lds_size
= lds_size
;
1962 tess
.ls_hs_config
= S_028B58_NUM_PATCHES(num_patches
) |
1963 S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp
) |
1964 S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp
);
1965 tess
.num_patches
= num_patches
;
1967 struct radv_shader_variant
*tes
= radv_get_shader(pipeline
, MESA_SHADER_TESS_EVAL
);
1968 unsigned type
= 0, partitioning
= 0, topology
= 0, distribution_mode
= 0;
1970 switch (tes
->info
.tes
.primitive_mode
) {
1972 type
= V_028B6C_TESS_TRIANGLE
;
1975 type
= V_028B6C_TESS_QUAD
;
1978 type
= V_028B6C_TESS_ISOLINE
;
1982 switch (tes
->info
.tes
.spacing
) {
1983 case TESS_SPACING_EQUAL
:
1984 partitioning
= V_028B6C_PART_INTEGER
;
1986 case TESS_SPACING_FRACTIONAL_ODD
:
1987 partitioning
= V_028B6C_PART_FRAC_ODD
;
1989 case TESS_SPACING_FRACTIONAL_EVEN
:
1990 partitioning
= V_028B6C_PART_FRAC_EVEN
;
1996 bool ccw
= tes
->info
.tes
.ccw
;
1997 const VkPipelineTessellationDomainOriginStateCreateInfo
*domain_origin_state
=
1998 vk_find_struct_const(pCreateInfo
->pTessellationState
,
1999 PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO
);
2001 if (domain_origin_state
&& domain_origin_state
->domainOrigin
!= VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT
)
2004 if (tes
->info
.tes
.point_mode
)
2005 topology
= V_028B6C_OUTPUT_POINT
;
2006 else if (tes
->info
.tes
.primitive_mode
== GL_ISOLINES
)
2007 topology
= V_028B6C_OUTPUT_LINE
;
2009 topology
= V_028B6C_OUTPUT_TRIANGLE_CCW
;
2011 topology
= V_028B6C_OUTPUT_TRIANGLE_CW
;
2013 if (pipeline
->device
->has_distributed_tess
) {
2014 if (pipeline
->device
->physical_device
->rad_info
.family
== CHIP_FIJI
||
2015 pipeline
->device
->physical_device
->rad_info
.family
>= CHIP_POLARIS10
)
2016 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_TRAPEZOIDS
;
2018 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_DONUTS
;
2020 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_NO_DIST
;
2022 tess
.tf_param
= S_028B6C_TYPE(type
) |
2023 S_028B6C_PARTITIONING(partitioning
) |
2024 S_028B6C_TOPOLOGY(topology
) |
2025 S_028B6C_DISTRIBUTION_MODE(distribution_mode
);
2030 static const struct radv_prim_vertex_count prim_size_table
[] = {
2031 [V_008958_DI_PT_NONE
] = {0, 0},
2032 [V_008958_DI_PT_POINTLIST
] = {1, 1},
2033 [V_008958_DI_PT_LINELIST
] = {2, 2},
2034 [V_008958_DI_PT_LINESTRIP
] = {2, 1},
2035 [V_008958_DI_PT_TRILIST
] = {3, 3},
2036 [V_008958_DI_PT_TRIFAN
] = {3, 1},
2037 [V_008958_DI_PT_TRISTRIP
] = {3, 1},
2038 [V_008958_DI_PT_LINELIST_ADJ
] = {4, 4},
2039 [V_008958_DI_PT_LINESTRIP_ADJ
] = {4, 1},
2040 [V_008958_DI_PT_TRILIST_ADJ
] = {6, 6},
2041 [V_008958_DI_PT_TRISTRIP_ADJ
] = {6, 2},
2042 [V_008958_DI_PT_RECTLIST
] = {3, 3},
2043 [V_008958_DI_PT_LINELOOP
] = {2, 1},
2044 [V_008958_DI_PT_POLYGON
] = {3, 1},
2045 [V_008958_DI_PT_2D_TRI_STRIP
] = {0, 0},
2048 static const struct radv_vs_output_info
*get_vs_output_info(const struct radv_pipeline
*pipeline
)
2050 if (radv_pipeline_has_gs(pipeline
))
2051 if (radv_pipeline_has_ngg(pipeline
))
2052 return &pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
.vs
.outinfo
;
2054 return &pipeline
->gs_copy_shader
->info
.vs
.outinfo
;
2055 else if (radv_pipeline_has_tess(pipeline
))
2056 return &pipeline
->shaders
[MESA_SHADER_TESS_EVAL
]->info
.tes
.outinfo
;
2058 return &pipeline
->shaders
[MESA_SHADER_VERTEX
]->info
.vs
.outinfo
;
2062 radv_link_shaders(struct radv_pipeline
*pipeline
, nir_shader
**shaders
)
2064 nir_shader
* ordered_shaders
[MESA_SHADER_STAGES
];
2065 int shader_count
= 0;
2067 if(shaders
[MESA_SHADER_FRAGMENT
]) {
2068 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_FRAGMENT
];
2070 if(shaders
[MESA_SHADER_GEOMETRY
]) {
2071 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_GEOMETRY
];
2073 if(shaders
[MESA_SHADER_TESS_EVAL
]) {
2074 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_TESS_EVAL
];
2076 if(shaders
[MESA_SHADER_TESS_CTRL
]) {
2077 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_TESS_CTRL
];
2079 if(shaders
[MESA_SHADER_VERTEX
]) {
2080 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_VERTEX
];
2083 if (shader_count
> 1) {
2084 unsigned first
= ordered_shaders
[shader_count
- 1]->info
.stage
;
2085 unsigned last
= ordered_shaders
[0]->info
.stage
;
2087 if (ordered_shaders
[0]->info
.stage
== MESA_SHADER_FRAGMENT
&&
2088 ordered_shaders
[1]->info
.has_transform_feedback_varyings
)
2089 nir_link_xfb_varyings(ordered_shaders
[1], ordered_shaders
[0]);
2091 for (int i
= 0; i
< shader_count
; ++i
) {
2092 nir_variable_mode mask
= 0;
2094 if (ordered_shaders
[i
]->info
.stage
!= first
)
2095 mask
= mask
| nir_var_shader_in
;
2097 if (ordered_shaders
[i
]->info
.stage
!= last
)
2098 mask
= mask
| nir_var_shader_out
;
2100 nir_lower_io_to_scalar_early(ordered_shaders
[i
], mask
);
2101 radv_optimize_nir(ordered_shaders
[i
], false, false);
2105 for (int i
= 1; i
< shader_count
; ++i
) {
2106 nir_lower_io_arrays_to_elements(ordered_shaders
[i
],
2107 ordered_shaders
[i
- 1]);
2109 if (nir_link_opt_varyings(ordered_shaders
[i
],
2110 ordered_shaders
[i
- 1]))
2111 radv_optimize_nir(ordered_shaders
[i
- 1], false, false);
2113 nir_remove_dead_variables(ordered_shaders
[i
],
2114 nir_var_shader_out
);
2115 nir_remove_dead_variables(ordered_shaders
[i
- 1],
2118 bool progress
= nir_remove_unused_varyings(ordered_shaders
[i
],
2119 ordered_shaders
[i
- 1]);
2121 nir_compact_varyings(ordered_shaders
[i
],
2122 ordered_shaders
[i
- 1], true);
2125 if (nir_lower_global_vars_to_local(ordered_shaders
[i
])) {
2126 ac_lower_indirect_derefs(ordered_shaders
[i
],
2127 pipeline
->device
->physical_device
->rad_info
.chip_class
);
2129 radv_optimize_nir(ordered_shaders
[i
], false, false);
2131 if (nir_lower_global_vars_to_local(ordered_shaders
[i
- 1])) {
2132 ac_lower_indirect_derefs(ordered_shaders
[i
- 1],
2133 pipeline
->device
->physical_device
->rad_info
.chip_class
);
2135 radv_optimize_nir(ordered_shaders
[i
- 1], false, false);
2141 radv_get_attrib_stride(const VkPipelineVertexInputStateCreateInfo
*input_state
,
2142 uint32_t attrib_binding
)
2144 for (uint32_t i
= 0; i
< input_state
->vertexBindingDescriptionCount
; i
++) {
2145 const VkVertexInputBindingDescription
*input_binding
=
2146 &input_state
->pVertexBindingDescriptions
[i
];
2148 if (input_binding
->binding
== attrib_binding
)
2149 return input_binding
->stride
;
2155 static struct radv_pipeline_key
2156 radv_generate_graphics_pipeline_key(struct radv_pipeline
*pipeline
,
2157 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
2158 const struct radv_blend_state
*blend
,
2159 bool has_view_index
)
2161 const VkPipelineVertexInputStateCreateInfo
*input_state
=
2162 pCreateInfo
->pVertexInputState
;
2163 const VkPipelineVertexInputDivisorStateCreateInfoEXT
*divisor_state
=
2164 vk_find_struct_const(input_state
->pNext
, PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT
);
2166 struct radv_pipeline_key key
;
2167 memset(&key
, 0, sizeof(key
));
2169 if (pCreateInfo
->flags
& VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT
)
2170 key
.optimisations_disabled
= 1;
2172 key
.has_multiview_view_index
= has_view_index
;
2174 uint32_t binding_input_rate
= 0;
2175 uint32_t instance_rate_divisors
[MAX_VERTEX_ATTRIBS
];
2176 for (unsigned i
= 0; i
< input_state
->vertexBindingDescriptionCount
; ++i
) {
2177 if (input_state
->pVertexBindingDescriptions
[i
].inputRate
) {
2178 unsigned binding
= input_state
->pVertexBindingDescriptions
[i
].binding
;
2179 binding_input_rate
|= 1u << binding
;
2180 instance_rate_divisors
[binding
] = 1;
2183 if (divisor_state
) {
2184 for (unsigned i
= 0; i
< divisor_state
->vertexBindingDivisorCount
; ++i
) {
2185 instance_rate_divisors
[divisor_state
->pVertexBindingDivisors
[i
].binding
] =
2186 divisor_state
->pVertexBindingDivisors
[i
].divisor
;
2190 for (unsigned i
= 0; i
< input_state
->vertexAttributeDescriptionCount
; ++i
) {
2191 const VkVertexInputAttributeDescription
*desc
=
2192 &input_state
->pVertexAttributeDescriptions
[i
];
2193 const struct vk_format_description
*format_desc
;
2194 unsigned location
= desc
->location
;
2195 unsigned binding
= desc
->binding
;
2196 unsigned num_format
, data_format
;
2199 if (binding_input_rate
& (1u << binding
)) {
2200 key
.instance_rate_inputs
|= 1u << location
;
2201 key
.instance_rate_divisors
[location
] = instance_rate_divisors
[binding
];
2204 format_desc
= vk_format_description(desc
->format
);
2205 first_non_void
= vk_format_get_first_non_void_channel(desc
->format
);
2207 num_format
= radv_translate_buffer_numformat(format_desc
, first_non_void
);
2208 data_format
= radv_translate_buffer_dataformat(format_desc
, first_non_void
);
2210 key
.vertex_attribute_formats
[location
] = data_format
| (num_format
<< 4);
2211 key
.vertex_attribute_bindings
[location
] = desc
->binding
;
2212 key
.vertex_attribute_offsets
[location
] = desc
->offset
;
2213 key
.vertex_attribute_strides
[location
] = radv_get_attrib_stride(input_state
, desc
->binding
);
2215 if (pipeline
->device
->physical_device
->rad_info
.chip_class
<= GFX8
&&
2216 pipeline
->device
->physical_device
->rad_info
.family
!= CHIP_STONEY
) {
2217 VkFormat format
= input_state
->pVertexAttributeDescriptions
[i
].format
;
2220 case VK_FORMAT_A2R10G10B10_SNORM_PACK32
:
2221 case VK_FORMAT_A2B10G10R10_SNORM_PACK32
:
2222 adjust
= RADV_ALPHA_ADJUST_SNORM
;
2224 case VK_FORMAT_A2R10G10B10_SSCALED_PACK32
:
2225 case VK_FORMAT_A2B10G10R10_SSCALED_PACK32
:
2226 adjust
= RADV_ALPHA_ADJUST_SSCALED
;
2228 case VK_FORMAT_A2R10G10B10_SINT_PACK32
:
2229 case VK_FORMAT_A2B10G10R10_SINT_PACK32
:
2230 adjust
= RADV_ALPHA_ADJUST_SINT
;
2236 key
.vertex_alpha_adjust
|= adjust
<< (2 * location
);
2239 switch (desc
->format
) {
2240 case VK_FORMAT_B8G8R8A8_UNORM
:
2241 case VK_FORMAT_B8G8R8A8_SNORM
:
2242 case VK_FORMAT_B8G8R8A8_USCALED
:
2243 case VK_FORMAT_B8G8R8A8_SSCALED
:
2244 case VK_FORMAT_B8G8R8A8_UINT
:
2245 case VK_FORMAT_B8G8R8A8_SINT
:
2246 case VK_FORMAT_B8G8R8A8_SRGB
:
2247 case VK_FORMAT_A2R10G10B10_UNORM_PACK32
:
2248 case VK_FORMAT_A2R10G10B10_SNORM_PACK32
:
2249 case VK_FORMAT_A2R10G10B10_USCALED_PACK32
:
2250 case VK_FORMAT_A2R10G10B10_SSCALED_PACK32
:
2251 case VK_FORMAT_A2R10G10B10_UINT_PACK32
:
2252 case VK_FORMAT_A2R10G10B10_SINT_PACK32
:
2253 key
.vertex_post_shuffle
|= 1 << location
;
2260 if (pCreateInfo
->pTessellationState
)
2261 key
.tess_input_vertices
= pCreateInfo
->pTessellationState
->patchControlPoints
;
2264 if (pCreateInfo
->pMultisampleState
&&
2265 pCreateInfo
->pMultisampleState
->rasterizationSamples
> 1) {
2266 uint32_t num_samples
= pCreateInfo
->pMultisampleState
->rasterizationSamples
;
2267 uint32_t ps_iter_samples
= radv_pipeline_get_ps_iter_samples(pCreateInfo
->pMultisampleState
);
2268 key
.num_samples
= num_samples
;
2269 key
.log2_ps_iter_samples
= util_logbase2(ps_iter_samples
);
2272 key
.col_format
= blend
->spi_shader_col_format
;
2273 if (pipeline
->device
->physical_device
->rad_info
.chip_class
< GFX8
)
2274 radv_pipeline_compute_get_int_clamp(pCreateInfo
, &key
.is_int8
, &key
.is_int10
);
2280 radv_nir_stage_uses_xfb(const nir_shader
*nir
)
2282 nir_xfb_info
*xfb
= nir_gather_xfb_info(nir
, NULL
);
2283 bool uses_xfb
= !!xfb
;
2290 radv_fill_shader_keys(struct radv_device
*device
,
2291 struct radv_shader_variant_key
*keys
,
2292 const struct radv_pipeline_key
*key
,
2295 keys
[MESA_SHADER_VERTEX
].vs
.instance_rate_inputs
= key
->instance_rate_inputs
;
2296 keys
[MESA_SHADER_VERTEX
].vs
.alpha_adjust
= key
->vertex_alpha_adjust
;
2297 keys
[MESA_SHADER_VERTEX
].vs
.post_shuffle
= key
->vertex_post_shuffle
;
2298 for (unsigned i
= 0; i
< MAX_VERTEX_ATTRIBS
; ++i
) {
2299 keys
[MESA_SHADER_VERTEX
].vs
.instance_rate_divisors
[i
] = key
->instance_rate_divisors
[i
];
2300 keys
[MESA_SHADER_VERTEX
].vs
.vertex_attribute_formats
[i
] = key
->vertex_attribute_formats
[i
];
2301 keys
[MESA_SHADER_VERTEX
].vs
.vertex_attribute_bindings
[i
] = key
->vertex_attribute_bindings
[i
];
2302 keys
[MESA_SHADER_VERTEX
].vs
.vertex_attribute_offsets
[i
] = key
->vertex_attribute_offsets
[i
];
2303 keys
[MESA_SHADER_VERTEX
].vs
.vertex_attribute_strides
[i
] = key
->vertex_attribute_strides
[i
];
2306 if (nir
[MESA_SHADER_TESS_CTRL
]) {
2307 keys
[MESA_SHADER_VERTEX
].vs_common_out
.as_ls
= true;
2308 keys
[MESA_SHADER_TESS_CTRL
].tcs
.num_inputs
= 0;
2309 keys
[MESA_SHADER_TESS_CTRL
].tcs
.input_vertices
= key
->tess_input_vertices
;
2310 keys
[MESA_SHADER_TESS_CTRL
].tcs
.primitive_mode
= nir
[MESA_SHADER_TESS_EVAL
]->info
.tess
.primitive_mode
;
2312 keys
[MESA_SHADER_TESS_CTRL
].tcs
.tes_reads_tess_factors
= !!(nir
[MESA_SHADER_TESS_EVAL
]->info
.inputs_read
& (VARYING_BIT_TESS_LEVEL_INNER
| VARYING_BIT_TESS_LEVEL_OUTER
));
2315 if (nir
[MESA_SHADER_GEOMETRY
]) {
2316 if (nir
[MESA_SHADER_TESS_CTRL
])
2317 keys
[MESA_SHADER_TESS_EVAL
].vs_common_out
.as_es
= true;
2319 keys
[MESA_SHADER_VERTEX
].vs_common_out
.as_es
= true;
2322 if (device
->physical_device
->rad_info
.chip_class
>= GFX10
&&
2323 !(device
->instance
->debug_flags
& RADV_DEBUG_NO_NGG
)) {
2324 if (nir
[MESA_SHADER_TESS_CTRL
]) {
2325 keys
[MESA_SHADER_TESS_EVAL
].vs_common_out
.as_ngg
= true;
2327 keys
[MESA_SHADER_VERTEX
].vs_common_out
.as_ngg
= true;
2330 if (nir
[MESA_SHADER_TESS_CTRL
] &&
2331 nir
[MESA_SHADER_GEOMETRY
] &&
2332 nir
[MESA_SHADER_GEOMETRY
]->info
.gs
.invocations
*
2333 nir
[MESA_SHADER_GEOMETRY
]->info
.gs
.vertices_out
> 256) {
2334 /* Fallback to the legacy path if tessellation is
2335 * enabled with extreme geometry because
2336 * EN_MAX_VERT_OUT_PER_GS_INSTANCE doesn't work and it
2339 keys
[MESA_SHADER_TESS_EVAL
].vs_common_out
.as_ngg
= false;
2342 /* TODO: Implement streamout support for NGG. */
2343 gl_shader_stage last_xfb_stage
= MESA_SHADER_VERTEX
;
2345 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
2350 if (nir
[last_xfb_stage
] &&
2351 radv_nir_stage_uses_xfb(nir
[last_xfb_stage
])) {
2352 if (nir
[MESA_SHADER_TESS_CTRL
])
2353 keys
[MESA_SHADER_TESS_EVAL
].vs_common_out
.as_ngg
= false;
2355 keys
[MESA_SHADER_VERTEX
].vs_common_out
.as_ngg
= false;
2359 for(int i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
2360 keys
[i
].has_multiview_view_index
= key
->has_multiview_view_index
;
2362 keys
[MESA_SHADER_FRAGMENT
].fs
.col_format
= key
->col_format
;
2363 keys
[MESA_SHADER_FRAGMENT
].fs
.is_int8
= key
->is_int8
;
2364 keys
[MESA_SHADER_FRAGMENT
].fs
.is_int10
= key
->is_int10
;
2365 keys
[MESA_SHADER_FRAGMENT
].fs
.log2_ps_iter_samples
= key
->log2_ps_iter_samples
;
2366 keys
[MESA_SHADER_FRAGMENT
].fs
.num_samples
= key
->num_samples
;
2370 merge_tess_info(struct shader_info
*tes_info
,
2371 const struct shader_info
*tcs_info
)
2373 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
2375 * "PointMode. Controls generation of points rather than triangles
2376 * or lines. This functionality defaults to disabled, and is
2377 * enabled if either shader stage includes the execution mode.
2379 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
2380 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
2381 * and OutputVertices, it says:
2383 * "One mode must be set in at least one of the tessellation
2386 * So, the fields can be set in either the TCS or TES, but they must
2387 * agree if set in both. Our backend looks at TES, so bitwise-or in
2388 * the values from the TCS.
2390 assert(tcs_info
->tess
.tcs_vertices_out
== 0 ||
2391 tes_info
->tess
.tcs_vertices_out
== 0 ||
2392 tcs_info
->tess
.tcs_vertices_out
== tes_info
->tess
.tcs_vertices_out
);
2393 tes_info
->tess
.tcs_vertices_out
|= tcs_info
->tess
.tcs_vertices_out
;
2395 assert(tcs_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
2396 tes_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
2397 tcs_info
->tess
.spacing
== tes_info
->tess
.spacing
);
2398 tes_info
->tess
.spacing
|= tcs_info
->tess
.spacing
;
2400 assert(tcs_info
->tess
.primitive_mode
== 0 ||
2401 tes_info
->tess
.primitive_mode
== 0 ||
2402 tcs_info
->tess
.primitive_mode
== tes_info
->tess
.primitive_mode
);
2403 tes_info
->tess
.primitive_mode
|= tcs_info
->tess
.primitive_mode
;
2404 tes_info
->tess
.ccw
|= tcs_info
->tess
.ccw
;
2405 tes_info
->tess
.point_mode
|= tcs_info
->tess
.point_mode
;
2409 void radv_init_feedback(const VkPipelineCreationFeedbackCreateInfoEXT
*ext
)
2414 if (ext
->pPipelineCreationFeedback
) {
2415 ext
->pPipelineCreationFeedback
->flags
= 0;
2416 ext
->pPipelineCreationFeedback
->duration
= 0;
2419 for (unsigned i
= 0; i
< ext
->pipelineStageCreationFeedbackCount
; ++i
) {
2420 ext
->pPipelineStageCreationFeedbacks
[i
].flags
= 0;
2421 ext
->pPipelineStageCreationFeedbacks
[i
].duration
= 0;
2426 void radv_start_feedback(VkPipelineCreationFeedbackEXT
*feedback
)
2431 feedback
->duration
-= radv_get_current_time();
2432 feedback
->flags
= VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT
;
2436 void radv_stop_feedback(VkPipelineCreationFeedbackEXT
*feedback
, bool cache_hit
)
2441 feedback
->duration
+= radv_get_current_time();
2442 feedback
->flags
= VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT
|
2443 (cache_hit
? VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT
: 0);
2447 void radv_create_shaders(struct radv_pipeline
*pipeline
,
2448 struct radv_device
*device
,
2449 struct radv_pipeline_cache
*cache
,
2450 const struct radv_pipeline_key
*key
,
2451 const VkPipelineShaderStageCreateInfo
**pStages
,
2452 const VkPipelineCreateFlags flags
,
2453 VkPipelineCreationFeedbackEXT
*pipeline_feedback
,
2454 VkPipelineCreationFeedbackEXT
**stage_feedbacks
)
2456 struct radv_shader_module fs_m
= {0};
2457 struct radv_shader_module
*modules
[MESA_SHADER_STAGES
] = { 0, };
2458 nir_shader
*nir
[MESA_SHADER_STAGES
] = {0};
2459 struct radv_shader_binary
*binaries
[MESA_SHADER_STAGES
] = {NULL
};
2460 struct radv_shader_variant_key keys
[MESA_SHADER_STAGES
] = {{{{{0}}}}};
2461 unsigned char hash
[20], gs_copy_hash
[20];
2463 radv_start_feedback(pipeline_feedback
);
2465 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
2467 modules
[i
] = radv_shader_module_from_handle(pStages
[i
]->module
);
2468 if (modules
[i
]->nir
)
2469 _mesa_sha1_compute(modules
[i
]->nir
->info
.name
,
2470 strlen(modules
[i
]->nir
->info
.name
),
2473 pipeline
->active_stages
|= mesa_to_vk_shader_stage(i
);
2477 radv_hash_shaders(hash
, pStages
, pipeline
->layout
, key
, get_hash_flags(device
));
2478 memcpy(gs_copy_hash
, hash
, 20);
2479 gs_copy_hash
[0] ^= 1;
2481 bool found_in_application_cache
= true;
2482 if (modules
[MESA_SHADER_GEOMETRY
]) {
2483 struct radv_shader_variant
*variants
[MESA_SHADER_STAGES
] = {0};
2484 radv_create_shader_variants_from_pipeline_cache(device
, cache
, gs_copy_hash
, variants
,
2485 &found_in_application_cache
);
2486 pipeline
->gs_copy_shader
= variants
[MESA_SHADER_GEOMETRY
];
2489 if (radv_create_shader_variants_from_pipeline_cache(device
, cache
, hash
, pipeline
->shaders
,
2490 &found_in_application_cache
) &&
2491 (!modules
[MESA_SHADER_GEOMETRY
] || pipeline
->gs_copy_shader
)) {
2492 radv_stop_feedback(pipeline_feedback
, found_in_application_cache
);
2496 if (!modules
[MESA_SHADER_FRAGMENT
] && !modules
[MESA_SHADER_COMPUTE
]) {
2498 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
2499 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "noop_fs");
2500 fs_m
.nir
= fs_b
.shader
;
2501 modules
[MESA_SHADER_FRAGMENT
] = &fs_m
;
2504 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
2505 const VkPipelineShaderStageCreateInfo
*stage
= pStages
[i
];
2510 radv_start_feedback(stage_feedbacks
[i
]);
2512 nir
[i
] = radv_shader_compile_to_nir(device
, modules
[i
],
2513 stage
? stage
->pName
: "main", i
,
2514 stage
? stage
->pSpecializationInfo
: NULL
,
2515 flags
, pipeline
->layout
);
2517 /* We don't want to alter meta shaders IR directly so clone it
2520 if (nir
[i
]->info
.name
) {
2521 nir
[i
] = nir_shader_clone(NULL
, nir
[i
]);
2524 radv_stop_feedback(stage_feedbacks
[i
], false);
2527 if (nir
[MESA_SHADER_TESS_CTRL
]) {
2528 nir_lower_patch_vertices(nir
[MESA_SHADER_TESS_EVAL
], nir
[MESA_SHADER_TESS_CTRL
]->info
.tess
.tcs_vertices_out
, NULL
);
2529 merge_tess_info(&nir
[MESA_SHADER_TESS_EVAL
]->info
, &nir
[MESA_SHADER_TESS_CTRL
]->info
);
2532 if (!(flags
& VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT
))
2533 radv_link_shaders(pipeline
, nir
);
2535 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
2537 NIR_PASS_V(nir
[i
], nir_lower_non_uniform_access
,
2538 nir_lower_non_uniform_ubo_access
|
2539 nir_lower_non_uniform_ssbo_access
|
2540 nir_lower_non_uniform_texture_access
|
2541 nir_lower_non_uniform_image_access
);
2542 NIR_PASS_V(nir
[i
], nir_lower_bool_to_int32
);
2545 if (radv_can_dump_shader(device
, modules
[i
], false))
2546 nir_print_shader(nir
[i
], stderr
);
2549 radv_fill_shader_keys(device
, keys
, key
, nir
);
2551 if (nir
[MESA_SHADER_FRAGMENT
]) {
2552 if (!pipeline
->shaders
[MESA_SHADER_FRAGMENT
]) {
2553 radv_start_feedback(stage_feedbacks
[MESA_SHADER_FRAGMENT
]);
2555 pipeline
->shaders
[MESA_SHADER_FRAGMENT
] =
2556 radv_shader_variant_compile(device
, modules
[MESA_SHADER_FRAGMENT
], &nir
[MESA_SHADER_FRAGMENT
], 1,
2557 pipeline
->layout
, keys
+ MESA_SHADER_FRAGMENT
,
2558 &binaries
[MESA_SHADER_FRAGMENT
]);
2560 radv_stop_feedback(stage_feedbacks
[MESA_SHADER_FRAGMENT
], false);
2563 /* TODO: These are no longer used as keys we should refactor this */
2564 keys
[MESA_SHADER_VERTEX
].vs_common_out
.export_prim_id
=
2565 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.prim_id_input
;
2566 keys
[MESA_SHADER_VERTEX
].vs_common_out
.export_layer_id
=
2567 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.layer_input
;
2568 keys
[MESA_SHADER_VERTEX
].vs_common_out
.export_clip_dists
=
2569 !!pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.num_input_clips_culls
;
2570 keys
[MESA_SHADER_TESS_EVAL
].vs_common_out
.export_prim_id
=
2571 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.prim_id_input
;
2572 keys
[MESA_SHADER_TESS_EVAL
].vs_common_out
.export_layer_id
=
2573 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.layer_input
;
2574 keys
[MESA_SHADER_TESS_EVAL
].vs_common_out
.export_clip_dists
=
2575 !!pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.num_input_clips_culls
;
2578 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&& modules
[MESA_SHADER_TESS_CTRL
]) {
2579 if (!pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]) {
2580 struct nir_shader
*combined_nir
[] = {nir
[MESA_SHADER_VERTEX
], nir
[MESA_SHADER_TESS_CTRL
]};
2581 struct radv_shader_variant_key key
= keys
[MESA_SHADER_TESS_CTRL
];
2582 key
.tcs
.vs_key
= keys
[MESA_SHADER_VERTEX
].vs
;
2584 radv_start_feedback(stage_feedbacks
[MESA_SHADER_TESS_CTRL
]);
2586 pipeline
->shaders
[MESA_SHADER_TESS_CTRL
] = radv_shader_variant_compile(device
, modules
[MESA_SHADER_TESS_CTRL
], combined_nir
, 2,
2588 &key
, &binaries
[MESA_SHADER_TESS_CTRL
]);
2590 radv_stop_feedback(stage_feedbacks
[MESA_SHADER_TESS_CTRL
], false);
2592 modules
[MESA_SHADER_VERTEX
] = NULL
;
2593 keys
[MESA_SHADER_TESS_EVAL
].tes
.num_patches
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.tcs
.num_patches
;
2594 keys
[MESA_SHADER_TESS_EVAL
].tes
.tcs_num_outputs
= util_last_bit64(pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.info
.tcs
.outputs_written
);
2597 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&& modules
[MESA_SHADER_GEOMETRY
]) {
2598 gl_shader_stage pre_stage
= modules
[MESA_SHADER_TESS_EVAL
] ? MESA_SHADER_TESS_EVAL
: MESA_SHADER_VERTEX
;
2599 if (!pipeline
->shaders
[MESA_SHADER_GEOMETRY
]) {
2600 struct nir_shader
*combined_nir
[] = {nir
[pre_stage
], nir
[MESA_SHADER_GEOMETRY
]};
2602 radv_start_feedback(stage_feedbacks
[MESA_SHADER_GEOMETRY
]);
2604 pipeline
->shaders
[MESA_SHADER_GEOMETRY
] = radv_shader_variant_compile(device
, modules
[MESA_SHADER_GEOMETRY
], combined_nir
, 2,
2606 &keys
[pre_stage
] , &binaries
[MESA_SHADER_GEOMETRY
]);
2608 radv_stop_feedback(stage_feedbacks
[MESA_SHADER_GEOMETRY
], false);
2610 modules
[pre_stage
] = NULL
;
2613 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
2614 if(modules
[i
] && !pipeline
->shaders
[i
]) {
2615 if (i
== MESA_SHADER_TESS_CTRL
) {
2616 keys
[MESA_SHADER_TESS_CTRL
].tcs
.num_inputs
= util_last_bit64(pipeline
->shaders
[MESA_SHADER_VERTEX
]->info
.info
.vs
.ls_outputs_written
);
2618 if (i
== MESA_SHADER_TESS_EVAL
) {
2619 keys
[MESA_SHADER_TESS_EVAL
].tes
.num_patches
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.tcs
.num_patches
;
2620 keys
[MESA_SHADER_TESS_EVAL
].tes
.tcs_num_outputs
= util_last_bit64(pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.info
.tcs
.outputs_written
);
2623 radv_start_feedback(stage_feedbacks
[i
]);
2625 pipeline
->shaders
[i
] = radv_shader_variant_compile(device
, modules
[i
], &nir
[i
], 1,
2627 keys
+ i
, &binaries
[i
]);
2629 radv_stop_feedback(stage_feedbacks
[i
], false);
2633 if(modules
[MESA_SHADER_GEOMETRY
]) {
2634 struct radv_shader_binary
*gs_copy_binary
= NULL
;
2635 if (!pipeline
->gs_copy_shader
&&
2636 !radv_pipeline_has_ngg(pipeline
)) {
2637 pipeline
->gs_copy_shader
= radv_create_gs_copy_shader(
2638 device
, nir
[MESA_SHADER_GEOMETRY
], &gs_copy_binary
,
2639 keys
[MESA_SHADER_GEOMETRY
].has_multiview_view_index
);
2642 if (pipeline
->gs_copy_shader
) {
2643 struct radv_shader_binary
*binaries
[MESA_SHADER_STAGES
] = {NULL
};
2644 struct radv_shader_variant
*variants
[MESA_SHADER_STAGES
] = {0};
2646 binaries
[MESA_SHADER_GEOMETRY
] = gs_copy_binary
;
2647 variants
[MESA_SHADER_GEOMETRY
] = pipeline
->gs_copy_shader
;
2649 radv_pipeline_cache_insert_shaders(device
, cache
,
2654 free(gs_copy_binary
);
2657 radv_pipeline_cache_insert_shaders(device
, cache
, hash
, pipeline
->shaders
,
2660 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
2663 ralloc_free(nir
[i
]);
2665 if (radv_can_dump_shader_stats(device
, modules
[i
]))
2666 radv_shader_dump_stats(device
,
2667 pipeline
->shaders
[i
],
2673 ralloc_free(fs_m
.nir
);
2675 radv_stop_feedback(pipeline_feedback
, false);
2679 radv_pipeline_stage_to_user_data_0(struct radv_pipeline
*pipeline
,
2680 gl_shader_stage stage
, enum chip_class chip_class
)
2682 bool has_gs
= radv_pipeline_has_gs(pipeline
);
2683 bool has_tess
= radv_pipeline_has_tess(pipeline
);
2684 bool has_ngg
= radv_pipeline_has_ngg(pipeline
);
2687 case MESA_SHADER_FRAGMENT
:
2688 return R_00B030_SPI_SHADER_USER_DATA_PS_0
;
2689 case MESA_SHADER_VERTEX
:
2691 if (chip_class
>= GFX10
) {
2692 return R_00B430_SPI_SHADER_USER_DATA_HS_0
;
2693 } else if (chip_class
== GFX9
) {
2694 return R_00B430_SPI_SHADER_USER_DATA_LS_0
;
2696 return R_00B530_SPI_SHADER_USER_DATA_LS_0
;
2702 if (chip_class
>= GFX10
) {
2703 return R_00B230_SPI_SHADER_USER_DATA_GS_0
;
2705 return R_00B330_SPI_SHADER_USER_DATA_ES_0
;
2710 return R_00B230_SPI_SHADER_USER_DATA_GS_0
;
2712 return R_00B130_SPI_SHADER_USER_DATA_VS_0
;
2713 case MESA_SHADER_GEOMETRY
:
2714 return chip_class
== GFX9
? R_00B330_SPI_SHADER_USER_DATA_ES_0
:
2715 R_00B230_SPI_SHADER_USER_DATA_GS_0
;
2716 case MESA_SHADER_COMPUTE
:
2717 return R_00B900_COMPUTE_USER_DATA_0
;
2718 case MESA_SHADER_TESS_CTRL
:
2719 return chip_class
== GFX9
? R_00B430_SPI_SHADER_USER_DATA_LS_0
:
2720 R_00B430_SPI_SHADER_USER_DATA_HS_0
;
2721 case MESA_SHADER_TESS_EVAL
:
2723 return chip_class
>= GFX10
? R_00B230_SPI_SHADER_USER_DATA_GS_0
:
2724 R_00B330_SPI_SHADER_USER_DATA_ES_0
;
2725 } else if (has_ngg
) {
2726 return R_00B230_SPI_SHADER_USER_DATA_GS_0
;
2728 return R_00B130_SPI_SHADER_USER_DATA_VS_0
;
2731 unreachable("unknown shader");
2735 struct radv_bin_size_entry
{
2741 radv_gfx9_compute_bin_size(struct radv_pipeline
*pipeline
, const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
2743 static const struct radv_bin_size_entry color_size_table
[][3][9] = {
2747 /* One shader engine */
2753 { UINT_MAX
, { 0, 0}},
2756 /* Two shader engines */
2762 { UINT_MAX
, { 0, 0}},
2765 /* Four shader engines */
2770 { UINT_MAX
, { 0, 0}},
2776 /* One shader engine */
2782 { UINT_MAX
, { 0, 0}},
2785 /* Two shader engines */
2791 { UINT_MAX
, { 0, 0}},
2794 /* Four shader engines */
2801 { UINT_MAX
, { 0, 0}},
2807 /* One shader engine */
2814 { UINT_MAX
, { 0, 0}},
2817 /* Two shader engines */
2825 { UINT_MAX
, { 0, 0}},
2828 /* Four shader engines */
2836 { UINT_MAX
, { 0, 0}},
2840 static const struct radv_bin_size_entry ds_size_table
[][3][9] = {
2844 // One shader engine
2851 { UINT_MAX
, { 0, 0}},
2854 // Two shader engines
2862 { UINT_MAX
, { 0, 0}},
2865 // Four shader engines
2873 { UINT_MAX
, { 0, 0}},
2879 // One shader engine
2887 { UINT_MAX
, { 0, 0}},
2890 // Two shader engines
2899 { UINT_MAX
, { 0, 0}},
2902 // Four shader engines
2911 { UINT_MAX
, { 0, 0}},
2917 // One shader engine
2925 { UINT_MAX
, { 0, 0}},
2928 // Two shader engines
2937 { UINT_MAX
, { 0, 0}},
2940 // Four shader engines
2948 { UINT_MAX
, { 0, 0}},
2953 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
2954 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
2955 VkExtent2D extent
= {512, 512};
2957 unsigned log_num_rb_per_se
=
2958 util_logbase2_ceil(pipeline
->device
->physical_device
->rad_info
.num_render_backends
/
2959 pipeline
->device
->physical_device
->rad_info
.max_se
);
2960 unsigned log_num_se
= util_logbase2_ceil(pipeline
->device
->physical_device
->rad_info
.max_se
);
2962 unsigned total_samples
= 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline
->graphics
.ms
.pa_sc_aa_config
);
2963 unsigned ps_iter_samples
= 1u << G_028804_PS_ITER_SAMPLES(pipeline
->graphics
.ms
.db_eqaa
);
2964 unsigned effective_samples
= total_samples
;
2965 unsigned color_bytes_per_pixel
= 0;
2967 const VkPipelineColorBlendStateCreateInfo
*vkblend
= pCreateInfo
->pColorBlendState
;
2969 for (unsigned i
= 0; i
< subpass
->color_count
; i
++) {
2970 if (!vkblend
->pAttachments
[i
].colorWriteMask
)
2973 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
)
2976 VkFormat format
= pass
->attachments
[subpass
->color_attachments
[i
].attachment
].format
;
2977 color_bytes_per_pixel
+= vk_format_get_blocksize(format
);
2980 /* MSAA images typically don't use all samples all the time. */
2981 if (effective_samples
>= 2 && ps_iter_samples
<= 1)
2982 effective_samples
= 2;
2983 color_bytes_per_pixel
*= effective_samples
;
2986 const struct radv_bin_size_entry
*color_entry
= color_size_table
[log_num_rb_per_se
][log_num_se
];
2987 while(color_entry
[1].bpp
<= color_bytes_per_pixel
)
2990 extent
= color_entry
->extent
;
2992 if (subpass
->depth_stencil_attachment
) {
2993 struct radv_render_pass_attachment
*attachment
= pass
->attachments
+ subpass
->depth_stencil_attachment
->attachment
;
2995 /* Coefficients taken from AMDVLK */
2996 unsigned depth_coeff
= vk_format_is_depth(attachment
->format
) ? 5 : 0;
2997 unsigned stencil_coeff
= vk_format_is_stencil(attachment
->format
) ? 1 : 0;
2998 unsigned ds_bytes_per_pixel
= 4 * (depth_coeff
+ stencil_coeff
) * total_samples
;
3000 const struct radv_bin_size_entry
*ds_entry
= ds_size_table
[log_num_rb_per_se
][log_num_se
];
3001 while(ds_entry
[1].bpp
<= ds_bytes_per_pixel
)
3004 if (ds_entry
->extent
.width
* ds_entry
->extent
.height
< extent
.width
* extent
.height
)
3005 extent
= ds_entry
->extent
;
3012 radv_gfx10_compute_bin_size(struct radv_pipeline
*pipeline
, const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
3014 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
3015 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
3016 VkExtent2D extent
= {512, 512};
3018 unsigned sdp_interface_count
;
3020 switch(pipeline
->device
->physical_device
->rad_info
.family
) {
3023 sdp_interface_count
= 16;
3026 sdp_interface_count
= 8;
3029 unreachable("Unhandled GFX10 chip");
3032 const unsigned db_tag_size
= 64;
3033 const unsigned db_tag_count
= 312;
3034 const unsigned color_tag_size
= 1024;
3035 const unsigned color_tag_count
= 31;
3036 const unsigned fmask_tag_size
= 256;
3037 const unsigned fmask_tag_count
= 44;
3039 const unsigned rb_count
= pipeline
->device
->physical_device
->rad_info
.num_render_backends
;
3040 const unsigned pipe_count
= MAX2(rb_count
, sdp_interface_count
);
3042 const unsigned db_tag_part
= (db_tag_count
* rb_count
/ pipe_count
) * db_tag_size
* pipe_count
;
3043 const unsigned color_tag_part
= (color_tag_count
* rb_count
/ pipe_count
) * color_tag_size
* pipe_count
;
3044 const unsigned fmask_tag_part
= (fmask_tag_count
* rb_count
/ pipe_count
) * fmask_tag_size
* pipe_count
;
3046 const unsigned total_samples
= 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline
->graphics
.ms
.pa_sc_aa_config
);
3047 const unsigned samples_log
= util_logbase2_ceil(total_samples
);
3049 unsigned color_bytes_per_pixel
= 0;
3050 unsigned fmask_bytes_per_pixel
= 0;
3052 const VkPipelineColorBlendStateCreateInfo
*vkblend
= pCreateInfo
->pColorBlendState
;
3054 for (unsigned i
= 0; i
< subpass
->color_count
; i
++) {
3055 if (!vkblend
->pAttachments
[i
].colorWriteMask
)
3058 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
)
3061 VkFormat format
= pass
->attachments
[subpass
->color_attachments
[i
].attachment
].format
;
3062 color_bytes_per_pixel
+= vk_format_get_blocksize(format
);
3064 if (total_samples
> 1) {
3065 const unsigned fmask_array
[] = {0, 1, 1, 4};
3066 fmask_bytes_per_pixel
+= fmask_array
[samples_log
];
3070 color_bytes_per_pixel
*= total_samples
;
3072 color_bytes_per_pixel
= MAX2(color_bytes_per_pixel
, 1);
3074 const unsigned color_pixel_count_log
= util_logbase2(color_tag_part
/ color_bytes_per_pixel
);
3075 extent
.width
= 1ull << ((color_pixel_count_log
+ 1) / 2);
3076 extent
.height
= 1ull << (color_pixel_count_log
/ 2);
3078 if (fmask_bytes_per_pixel
) {
3079 const unsigned fmask_pixel_count_log
= util_logbase2(fmask_tag_part
/ fmask_bytes_per_pixel
);
3081 const VkExtent2D fmask_extent
= (VkExtent2D
){
3082 .width
= 1ull << ((fmask_pixel_count_log
+ 1) / 2),
3083 .height
= 1ull << (color_pixel_count_log
/ 2)
3086 if (fmask_extent
.width
* fmask_extent
.height
< extent
.width
* extent
.height
)
3087 extent
= fmask_extent
;
3090 if (subpass
->depth_stencil_attachment
) {
3091 struct radv_render_pass_attachment
*attachment
= pass
->attachments
+ subpass
->depth_stencil_attachment
->attachment
;
3093 /* Coefficients taken from AMDVLK */
3094 unsigned depth_coeff
= vk_format_is_depth(attachment
->format
) ? 5 : 0;
3095 unsigned stencil_coeff
= vk_format_is_stencil(attachment
->format
) ? 1 : 0;
3096 unsigned db_bytes_per_pixel
= (depth_coeff
+ stencil_coeff
) * total_samples
;
3098 const unsigned db_pixel_count_log
= util_logbase2(db_tag_part
/ db_bytes_per_pixel
);
3100 const VkExtent2D db_extent
= (VkExtent2D
){
3101 .width
= 1ull << ((db_pixel_count_log
+ 1) / 2),
3102 .height
= 1ull << (color_pixel_count_log
/ 2)
3105 if (db_extent
.width
* db_extent
.height
< extent
.width
* extent
.height
)
3109 extent
.width
= MAX2(extent
.width
, 128);
3110 extent
.height
= MAX2(extent
.width
, 64);
3116 radv_pipeline_generate_disabled_binning_state(struct radeon_cmdbuf
*ctx_cs
,
3117 struct radv_pipeline
*pipeline
,
3118 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
3120 uint32_t pa_sc_binner_cntl_0
=
3121 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC
) |
3122 S_028C44_DISABLE_START_OF_PRIM(1);
3123 uint32_t db_dfsm_control
= S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF
);
3125 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
3126 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
3127 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
3128 const VkPipelineColorBlendStateCreateInfo
*vkblend
= pCreateInfo
->pColorBlendState
;
3129 unsigned min_bytes_per_pixel
= 0;
3132 for (unsigned i
= 0; i
< subpass
->color_count
; i
++) {
3133 if (!vkblend
->pAttachments
[i
].colorWriteMask
)
3136 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
)
3139 VkFormat format
= pass
->attachments
[subpass
->color_attachments
[i
].attachment
].format
;
3140 unsigned bytes
= vk_format_get_blocksize(format
);
3141 if (!min_bytes_per_pixel
|| bytes
< min_bytes_per_pixel
)
3142 min_bytes_per_pixel
= bytes
;
3146 pa_sc_binner_cntl_0
=
3147 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_NEW_SC
) |
3148 S_028C44_BIN_SIZE_X(0) |
3149 S_028C44_BIN_SIZE_Y(0) |
3150 S_028C44_BIN_SIZE_X_EXTEND(2) | /* 128 */
3151 S_028C44_BIN_SIZE_Y_EXTEND(min_bytes_per_pixel
<= 4 ? 2 : 1) | /* 128 or 64 */
3152 S_028C44_DISABLE_START_OF_PRIM(1);
3155 pipeline
->graphics
.binning
.pa_sc_binner_cntl_0
= pa_sc_binner_cntl_0
;
3156 pipeline
->graphics
.binning
.db_dfsm_control
= db_dfsm_control
;
3160 radv_pipeline_generate_binning_state(struct radeon_cmdbuf
*ctx_cs
,
3161 struct radv_pipeline
*pipeline
,
3162 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
3164 if (pipeline
->device
->physical_device
->rad_info
.chip_class
< GFX9
)
3167 VkExtent2D bin_size
;
3168 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
3169 bin_size
= radv_gfx10_compute_bin_size(pipeline
, pCreateInfo
);
3170 } else if (pipeline
->device
->physical_device
->rad_info
.chip_class
== GFX9
) {
3171 bin_size
= radv_gfx9_compute_bin_size(pipeline
, pCreateInfo
);
3173 unreachable("Unhandled generation for binning bin size calculation");
3175 if (pipeline
->device
->pbb_allowed
&& bin_size
.width
&& bin_size
.height
) {
3176 unsigned context_states_per_bin
; /* allowed range: [1, 6] */
3177 unsigned persistent_states_per_bin
; /* allowed range: [1, 32] */
3178 unsigned fpovs_per_batch
; /* allowed range: [0, 255], 0 = unlimited */
3180 if (pipeline
->device
->physical_device
->rad_info
.has_dedicated_vram
) {
3181 context_states_per_bin
= 1;
3182 persistent_states_per_bin
= 1;
3183 fpovs_per_batch
= 63;
3185 /* The context states are affected by the scissor bug. */
3186 context_states_per_bin
= pipeline
->device
->physical_device
->has_scissor_bug
? 1 : 6;
3187 /* 32 causes hangs for RAVEN. */
3188 persistent_states_per_bin
= 16;
3189 fpovs_per_batch
= 63;
3192 const uint32_t pa_sc_binner_cntl_0
=
3193 S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED
) |
3194 S_028C44_BIN_SIZE_X(bin_size
.width
== 16) |
3195 S_028C44_BIN_SIZE_Y(bin_size
.height
== 16) |
3196 S_028C44_BIN_SIZE_X_EXTEND(util_logbase2(MAX2(bin_size
.width
, 32)) - 5) |
3197 S_028C44_BIN_SIZE_Y_EXTEND(util_logbase2(MAX2(bin_size
.height
, 32)) - 5) |
3198 S_028C44_CONTEXT_STATES_PER_BIN(context_states_per_bin
- 1) |
3199 S_028C44_PERSISTENT_STATES_PER_BIN(persistent_states_per_bin
- 1) |
3200 S_028C44_DISABLE_START_OF_PRIM(1) |
3201 S_028C44_FPOVS_PER_BATCH(fpovs_per_batch
) |
3202 S_028C44_OPTIMAL_BIN_SELECTION(1);
3204 uint32_t db_dfsm_control
= S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF
);
3206 pipeline
->graphics
.binning
.pa_sc_binner_cntl_0
= pa_sc_binner_cntl_0
;
3207 pipeline
->graphics
.binning
.db_dfsm_control
= db_dfsm_control
;
3209 radv_pipeline_generate_disabled_binning_state(ctx_cs
, pipeline
, pCreateInfo
);
3214 radv_pipeline_generate_depth_stencil_state(struct radeon_cmdbuf
*ctx_cs
,
3215 struct radv_pipeline
*pipeline
,
3216 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
3217 const struct radv_graphics_pipeline_create_info
*extra
)
3219 const VkPipelineDepthStencilStateCreateInfo
*vkds
= pCreateInfo
->pDepthStencilState
;
3220 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
3221 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
3222 struct radv_render_pass_attachment
*attachment
= NULL
;
3223 uint32_t db_depth_control
= 0, db_stencil_control
= 0;
3224 uint32_t db_render_control
= 0, db_render_override2
= 0;
3225 uint32_t db_render_override
= 0;
3227 if (subpass
->depth_stencil_attachment
)
3228 attachment
= pass
->attachments
+ subpass
->depth_stencil_attachment
->attachment
;
3230 bool has_depth_attachment
= attachment
&& vk_format_is_depth(attachment
->format
);
3231 bool has_stencil_attachment
= attachment
&& vk_format_is_stencil(attachment
->format
);
3233 if (vkds
&& has_depth_attachment
) {
3234 db_depth_control
= S_028800_Z_ENABLE(vkds
->depthTestEnable
? 1 : 0) |
3235 S_028800_Z_WRITE_ENABLE(vkds
->depthWriteEnable
? 1 : 0) |
3236 S_028800_ZFUNC(vkds
->depthCompareOp
) |
3237 S_028800_DEPTH_BOUNDS_ENABLE(vkds
->depthBoundsTestEnable
? 1 : 0);
3239 /* from amdvlk: For 4xAA and 8xAA need to decompress on flush for better performance */
3240 db_render_override2
|= S_028010_DECOMPRESS_Z_ON_FLUSH(attachment
->samples
> 2);
3243 if (has_stencil_attachment
&& vkds
&& vkds
->stencilTestEnable
) {
3244 db_depth_control
|= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);
3245 db_depth_control
|= S_028800_STENCILFUNC(vkds
->front
.compareOp
);
3246 db_stencil_control
|= S_02842C_STENCILFAIL(si_translate_stencil_op(vkds
->front
.failOp
));
3247 db_stencil_control
|= S_02842C_STENCILZPASS(si_translate_stencil_op(vkds
->front
.passOp
));
3248 db_stencil_control
|= S_02842C_STENCILZFAIL(si_translate_stencil_op(vkds
->front
.depthFailOp
));
3250 db_depth_control
|= S_028800_STENCILFUNC_BF(vkds
->back
.compareOp
);
3251 db_stencil_control
|= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(vkds
->back
.failOp
));
3252 db_stencil_control
|= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(vkds
->back
.passOp
));
3253 db_stencil_control
|= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(vkds
->back
.depthFailOp
));
3256 if (attachment
&& extra
) {
3257 db_render_control
|= S_028000_DEPTH_CLEAR_ENABLE(extra
->db_depth_clear
);
3258 db_render_control
|= S_028000_STENCIL_CLEAR_ENABLE(extra
->db_stencil_clear
);
3260 db_render_control
|= S_028000_RESUMMARIZE_ENABLE(extra
->db_resummarize
);
3261 db_render_control
|= S_028000_DEPTH_COMPRESS_DISABLE(extra
->db_flush_depth_inplace
);
3262 db_render_control
|= S_028000_STENCIL_COMPRESS_DISABLE(extra
->db_flush_stencil_inplace
);
3263 db_render_override2
|= S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra
->db_depth_disable_expclear
);
3264 db_render_override2
|= S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra
->db_stencil_disable_expclear
);
3267 db_render_override
|= S_02800C_FORCE_HIS_ENABLE0(V_02800C_FORCE_DISABLE
) |
3268 S_02800C_FORCE_HIS_ENABLE1(V_02800C_FORCE_DISABLE
);
3270 if (!pCreateInfo
->pRasterizationState
->depthClampEnable
) {
3271 /* From VK_EXT_depth_range_unrestricted spec:
3273 * "The behavior described in Primitive Clipping still applies.
3274 * If depth clamping is disabled the depth values are still
3275 * clipped to 0 ≤ zc ≤ wc before the viewport transform. If
3276 * depth clamping is enabled the above equation is ignored and
3277 * the depth values are instead clamped to the VkViewport
3278 * minDepth and maxDepth values, which in the case of this
3279 * extension can be outside of the 0.0 to 1.0 range."
3281 db_render_override
|= S_02800C_DISABLE_VIEWPORT_CLAMP(1);
3284 radeon_set_context_reg(ctx_cs
, R_028800_DB_DEPTH_CONTROL
, db_depth_control
);
3285 radeon_set_context_reg(ctx_cs
, R_02842C_DB_STENCIL_CONTROL
, db_stencil_control
);
3287 radeon_set_context_reg(ctx_cs
, R_028000_DB_RENDER_CONTROL
, db_render_control
);
3288 radeon_set_context_reg(ctx_cs
, R_02800C_DB_RENDER_OVERRIDE
, db_render_override
);
3289 radeon_set_context_reg(ctx_cs
, R_028010_DB_RENDER_OVERRIDE2
, db_render_override2
);
3293 radv_pipeline_generate_blend_state(struct radeon_cmdbuf
*ctx_cs
,
3294 struct radv_pipeline
*pipeline
,
3295 const struct radv_blend_state
*blend
)
3297 radeon_set_context_reg_seq(ctx_cs
, R_028780_CB_BLEND0_CONTROL
, 8);
3298 radeon_emit_array(ctx_cs
, blend
->cb_blend_control
,
3300 radeon_set_context_reg(ctx_cs
, R_028808_CB_COLOR_CONTROL
, blend
->cb_color_control
);
3301 radeon_set_context_reg(ctx_cs
, R_028B70_DB_ALPHA_TO_MASK
, blend
->db_alpha_to_mask
);
3303 if (pipeline
->device
->physical_device
->has_rbplus
) {
3305 radeon_set_context_reg_seq(ctx_cs
, R_028760_SX_MRT0_BLEND_OPT
, 8);
3306 radeon_emit_array(ctx_cs
, blend
->sx_mrt_blend_opt
, 8);
3309 radeon_set_context_reg(ctx_cs
, R_028714_SPI_SHADER_COL_FORMAT
, blend
->spi_shader_col_format
);
3311 radeon_set_context_reg(ctx_cs
, R_028238_CB_TARGET_MASK
, blend
->cb_target_mask
);
3312 radeon_set_context_reg(ctx_cs
, R_02823C_CB_SHADER_MASK
, blend
->cb_shader_mask
);
3314 pipeline
->graphics
.col_format
= blend
->spi_shader_col_format
;
3315 pipeline
->graphics
.cb_target_mask
= blend
->cb_target_mask
;
3318 static const VkConservativeRasterizationModeEXT
3319 radv_get_conservative_raster_mode(const VkPipelineRasterizationStateCreateInfo
*pCreateInfo
)
3321 const VkPipelineRasterizationConservativeStateCreateInfoEXT
*conservative_raster
=
3322 vk_find_struct_const(pCreateInfo
->pNext
, PIPELINE_RASTERIZATION_CONSERVATIVE_STATE_CREATE_INFO_EXT
);
3324 if (!conservative_raster
)
3325 return VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT
;
3326 return conservative_raster
->conservativeRasterizationMode
;
3330 radv_pipeline_generate_raster_state(struct radeon_cmdbuf
*ctx_cs
,
3331 struct radv_pipeline
*pipeline
,
3332 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
3334 const VkPipelineRasterizationStateCreateInfo
*vkraster
= pCreateInfo
->pRasterizationState
;
3335 const VkConservativeRasterizationModeEXT mode
=
3336 radv_get_conservative_raster_mode(vkraster
);
3337 uint32_t pa_sc_conservative_rast
= S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1);
3338 bool depth_clip_disable
= vkraster
->depthClampEnable
;
3340 const VkPipelineRasterizationDepthClipStateCreateInfoEXT
*depth_clip_state
=
3341 vk_find_struct_const(vkraster
->pNext
, PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT
);
3342 if (depth_clip_state
) {
3343 depth_clip_disable
= !depth_clip_state
->depthClipEnable
;
3346 radeon_set_context_reg(ctx_cs
, R_028810_PA_CL_CLIP_CNTL
,
3347 S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.
3348 S_028810_ZCLIP_NEAR_DISABLE(depth_clip_disable
? 1 : 0) |
3349 S_028810_ZCLIP_FAR_DISABLE(depth_clip_disable
? 1 : 0) |
3350 S_028810_DX_RASTERIZATION_KILL(vkraster
->rasterizerDiscardEnable
? 1 : 0) |
3351 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1));
3353 radeon_set_context_reg(ctx_cs
, R_0286D4_SPI_INTERP_CONTROL_0
,
3354 S_0286D4_FLAT_SHADE_ENA(1) |
3355 S_0286D4_PNT_SPRITE_ENA(1) |
3356 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S
) |
3357 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T
) |
3358 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0
) |
3359 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1
) |
3360 S_0286D4_PNT_SPRITE_TOP_1(0)); /* vulkan is top to bottom - 1.0 at bottom */
3362 radeon_set_context_reg(ctx_cs
, R_028BE4_PA_SU_VTX_CNTL
,
3363 S_028BE4_PIX_CENTER(1) | // TODO verify
3364 S_028BE4_ROUND_MODE(V_028BE4_X_ROUND_TO_EVEN
) |
3365 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH
));
3367 radeon_set_context_reg(ctx_cs
, R_028814_PA_SU_SC_MODE_CNTL
,
3368 S_028814_FACE(vkraster
->frontFace
) |
3369 S_028814_CULL_FRONT(!!(vkraster
->cullMode
& VK_CULL_MODE_FRONT_BIT
)) |
3370 S_028814_CULL_BACK(!!(vkraster
->cullMode
& VK_CULL_MODE_BACK_BIT
)) |
3371 S_028814_POLY_MODE(vkraster
->polygonMode
!= VK_POLYGON_MODE_FILL
) |
3372 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(vkraster
->polygonMode
)) |
3373 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(vkraster
->polygonMode
)) |
3374 S_028814_POLY_OFFSET_FRONT_ENABLE(vkraster
->depthBiasEnable
? 1 : 0) |
3375 S_028814_POLY_OFFSET_BACK_ENABLE(vkraster
->depthBiasEnable
? 1 : 0) |
3376 S_028814_POLY_OFFSET_PARA_ENABLE(vkraster
->depthBiasEnable
? 1 : 0));
3378 /* Conservative rasterization. */
3379 if (mode
!= VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT
) {
3380 struct radv_multisample_state
*ms
= &pipeline
->graphics
.ms
;
3382 ms
->pa_sc_aa_config
|= S_028BE0_AA_MASK_CENTROID_DTMN(1);
3383 ms
->db_eqaa
|= S_028804_ENABLE_POSTZ_OVERRASTERIZATION(1) |
3384 S_028804_OVERRASTERIZATION_AMOUNT(4);
3386 pa_sc_conservative_rast
= S_028C4C_PREZ_AA_MASK_ENABLE(1) |
3387 S_028C4C_POSTZ_AA_MASK_ENABLE(1) |
3388 S_028C4C_CENTROID_SAMPLE_OVERRIDE(1);
3390 if (mode
== VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT
) {
3391 pa_sc_conservative_rast
|=
3392 S_028C4C_OVER_RAST_ENABLE(1) |
3393 S_028C4C_OVER_RAST_SAMPLE_SELECT(0) |
3394 S_028C4C_UNDER_RAST_ENABLE(0) |
3395 S_028C4C_UNDER_RAST_SAMPLE_SELECT(1) |
3396 S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(1);
3398 assert(mode
== VK_CONSERVATIVE_RASTERIZATION_MODE_UNDERESTIMATE_EXT
);
3399 pa_sc_conservative_rast
|=
3400 S_028C4C_OVER_RAST_ENABLE(0) |
3401 S_028C4C_OVER_RAST_SAMPLE_SELECT(1) |
3402 S_028C4C_UNDER_RAST_ENABLE(1) |
3403 S_028C4C_UNDER_RAST_SAMPLE_SELECT(0) |
3404 S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(0);
3408 radeon_set_context_reg(ctx_cs
, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL
,
3409 pa_sc_conservative_rast
);
3414 radv_pipeline_generate_multisample_state(struct radeon_cmdbuf
*ctx_cs
,
3415 struct radv_pipeline
*pipeline
)
3417 struct radv_multisample_state
*ms
= &pipeline
->graphics
.ms
;
3419 radeon_set_context_reg_seq(ctx_cs
, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0
, 2);
3420 radeon_emit(ctx_cs
, ms
->pa_sc_aa_mask
[0]);
3421 radeon_emit(ctx_cs
, ms
->pa_sc_aa_mask
[1]);
3423 radeon_set_context_reg(ctx_cs
, R_028804_DB_EQAA
, ms
->db_eqaa
);
3424 radeon_set_context_reg(ctx_cs
, R_028A4C_PA_SC_MODE_CNTL_1
, ms
->pa_sc_mode_cntl_1
);
3426 /* The exclusion bits can be set to improve rasterization efficiency
3427 * if no sample lies on the pixel boundary (-8 sample offset). It's
3428 * currently always TRUE because the driver doesn't support 16 samples.
3430 bool exclusion
= pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX7
;
3431 radeon_set_context_reg(ctx_cs
, R_02882C_PA_SU_PRIM_FILTER_CNTL
,
3432 S_02882C_XMAX_RIGHT_EXCLUSION(exclusion
) |
3433 S_02882C_YMAX_BOTTOM_EXCLUSION(exclusion
));
3437 radv_pipeline_generate_vgt_gs_mode(struct radeon_cmdbuf
*ctx_cs
,
3438 struct radv_pipeline
*pipeline
)
3440 const struct radv_vs_output_info
*outinfo
= get_vs_output_info(pipeline
);
3441 const struct radv_shader_variant
*vs
=
3442 pipeline
->shaders
[MESA_SHADER_TESS_EVAL
] ?
3443 pipeline
->shaders
[MESA_SHADER_TESS_EVAL
] :
3444 pipeline
->shaders
[MESA_SHADER_VERTEX
];
3445 unsigned vgt_primitiveid_en
= 0;
3446 uint32_t vgt_gs_mode
= 0;
3448 if (radv_pipeline_has_ngg(pipeline
))
3451 if (radv_pipeline_has_gs(pipeline
)) {
3452 const struct radv_shader_variant
*gs
=
3453 pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
3455 vgt_gs_mode
= ac_vgt_gs_mode(gs
->info
.gs
.vertices_out
,
3456 pipeline
->device
->physical_device
->rad_info
.chip_class
);
3457 } else if (outinfo
->export_prim_id
|| vs
->info
.info
.uses_prim_id
) {
3458 vgt_gs_mode
= S_028A40_MODE(V_028A40_GS_SCENARIO_A
);
3459 vgt_primitiveid_en
|= S_028A84_PRIMITIVEID_EN(1);
3462 radeon_set_context_reg(ctx_cs
, R_028A84_VGT_PRIMITIVEID_EN
, vgt_primitiveid_en
);
3463 radeon_set_context_reg(ctx_cs
, R_028A40_VGT_GS_MODE
, vgt_gs_mode
);
3467 radv_pipeline_generate_hw_vs(struct radeon_cmdbuf
*ctx_cs
,
3468 struct radeon_cmdbuf
*cs
,
3469 struct radv_pipeline
*pipeline
,
3470 struct radv_shader_variant
*shader
)
3472 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
3474 radeon_set_sh_reg_seq(cs
, R_00B120_SPI_SHADER_PGM_LO_VS
, 4);
3475 radeon_emit(cs
, va
>> 8);
3476 radeon_emit(cs
, S_00B124_MEM_BASE(va
>> 40));
3477 radeon_emit(cs
, shader
->config
.rsrc1
);
3478 radeon_emit(cs
, shader
->config
.rsrc2
);
3480 const struct radv_vs_output_info
*outinfo
= get_vs_output_info(pipeline
);
3481 unsigned clip_dist_mask
, cull_dist_mask
, total_mask
;
3482 clip_dist_mask
= outinfo
->clip_dist_mask
;
3483 cull_dist_mask
= outinfo
->cull_dist_mask
;
3484 total_mask
= clip_dist_mask
| cull_dist_mask
;
3485 bool misc_vec_ena
= outinfo
->writes_pointsize
||
3486 outinfo
->writes_layer
||
3487 outinfo
->writes_viewport_index
;
3488 unsigned spi_vs_out_config
, nparams
;
3490 /* VS is required to export at least one param. */
3491 nparams
= MAX2(outinfo
->param_exports
, 1);
3492 spi_vs_out_config
= S_0286C4_VS_EXPORT_COUNT(nparams
- 1);
3494 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
3495 spi_vs_out_config
|= S_0286C4_NO_PC_EXPORT(outinfo
->param_exports
== 0);
3498 radeon_set_context_reg(ctx_cs
, R_0286C4_SPI_VS_OUT_CONFIG
, spi_vs_out_config
);
3500 radeon_set_context_reg(ctx_cs
, R_02870C_SPI_SHADER_POS_FORMAT
,
3501 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP
) |
3502 S_02870C_POS1_EXPORT_FORMAT(outinfo
->pos_exports
> 1 ?
3503 V_02870C_SPI_SHADER_4COMP
:
3504 V_02870C_SPI_SHADER_NONE
) |
3505 S_02870C_POS2_EXPORT_FORMAT(outinfo
->pos_exports
> 2 ?
3506 V_02870C_SPI_SHADER_4COMP
:
3507 V_02870C_SPI_SHADER_NONE
) |
3508 S_02870C_POS3_EXPORT_FORMAT(outinfo
->pos_exports
> 3 ?
3509 V_02870C_SPI_SHADER_4COMP
:
3510 V_02870C_SPI_SHADER_NONE
));
3512 radeon_set_context_reg(ctx_cs
, R_028818_PA_CL_VTE_CNTL
,
3513 S_028818_VTX_W0_FMT(1) |
3514 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
3515 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
3516 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
3518 radeon_set_context_reg(ctx_cs
, R_02881C_PA_CL_VS_OUT_CNTL
,
3519 S_02881C_USE_VTX_POINT_SIZE(outinfo
->writes_pointsize
) |
3520 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo
->writes_layer
) |
3521 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo
->writes_viewport_index
) |
3522 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena
) |
3523 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena
) |
3524 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask
& 0x0f) != 0) |
3525 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask
& 0xf0) != 0) |
3526 cull_dist_mask
<< 8 |
3529 if (pipeline
->device
->physical_device
->rad_info
.chip_class
<= GFX8
)
3530 radeon_set_context_reg(ctx_cs
, R_028AB4_VGT_REUSE_OFF
,
3531 outinfo
->writes_viewport_index
);
3535 radv_pipeline_generate_hw_es(struct radeon_cmdbuf
*cs
,
3536 struct radv_pipeline
*pipeline
,
3537 struct radv_shader_variant
*shader
)
3539 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
3541 radeon_set_sh_reg_seq(cs
, R_00B320_SPI_SHADER_PGM_LO_ES
, 4);
3542 radeon_emit(cs
, va
>> 8);
3543 radeon_emit(cs
, S_00B324_MEM_BASE(va
>> 40));
3544 radeon_emit(cs
, shader
->config
.rsrc1
);
3545 radeon_emit(cs
, shader
->config
.rsrc2
);
3549 radv_pipeline_generate_hw_ls(struct radeon_cmdbuf
*cs
,
3550 struct radv_pipeline
*pipeline
,
3551 struct radv_shader_variant
*shader
,
3552 const struct radv_tessellation_state
*tess
)
3554 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
3555 uint32_t rsrc2
= shader
->config
.rsrc2
;
3557 radeon_set_sh_reg_seq(cs
, R_00B520_SPI_SHADER_PGM_LO_LS
, 2);
3558 radeon_emit(cs
, va
>> 8);
3559 radeon_emit(cs
, S_00B524_MEM_BASE(va
>> 40));
3561 rsrc2
|= S_00B52C_LDS_SIZE(tess
->lds_size
);
3562 if (pipeline
->device
->physical_device
->rad_info
.chip_class
== GFX7
&&
3563 pipeline
->device
->physical_device
->rad_info
.family
!= CHIP_HAWAII
)
3564 radeon_set_sh_reg(cs
, R_00B52C_SPI_SHADER_PGM_RSRC2_LS
, rsrc2
);
3566 radeon_set_sh_reg_seq(cs
, R_00B528_SPI_SHADER_PGM_RSRC1_LS
, 2);
3567 radeon_emit(cs
, shader
->config
.rsrc1
);
3568 radeon_emit(cs
, rsrc2
);
3572 radv_pipeline_generate_hw_ngg(struct radeon_cmdbuf
*ctx_cs
,
3573 struct radeon_cmdbuf
*cs
,
3574 struct radv_pipeline
*pipeline
,
3575 struct radv_shader_variant
*shader
,
3576 const struct radv_ngg_state
*ngg_state
)
3578 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
3579 gl_shader_stage es_type
=
3580 radv_pipeline_has_tess(pipeline
) ? MESA_SHADER_TESS_EVAL
: MESA_SHADER_VERTEX
;
3581 struct radv_shader_variant
*es
=
3582 es_type
== MESA_SHADER_TESS_EVAL
? pipeline
->shaders
[MESA_SHADER_TESS_EVAL
] : pipeline
->shaders
[MESA_SHADER_VERTEX
];
3584 radeon_set_sh_reg_seq(cs
, R_00B320_SPI_SHADER_PGM_LO_ES
, 2);
3585 radeon_emit(cs
, va
>> 8);
3586 radeon_emit(cs
, S_00B324_MEM_BASE(va
>> 40));
3587 radeon_set_sh_reg_seq(cs
, R_00B228_SPI_SHADER_PGM_RSRC1_GS
, 2);
3588 radeon_emit(cs
, shader
->config
.rsrc1
);
3589 radeon_emit(cs
, shader
->config
.rsrc2
);
3591 const struct radv_vs_output_info
*outinfo
= get_vs_output_info(pipeline
);
3592 unsigned clip_dist_mask
, cull_dist_mask
, total_mask
;
3593 clip_dist_mask
= outinfo
->clip_dist_mask
;
3594 cull_dist_mask
= outinfo
->cull_dist_mask
;
3595 total_mask
= clip_dist_mask
| cull_dist_mask
;
3596 bool misc_vec_ena
= outinfo
->writes_pointsize
||
3597 outinfo
->writes_layer
||
3598 outinfo
->writes_viewport_index
;
3599 bool es_enable_prim_id
= outinfo
->export_prim_id
||
3600 (es
&& es
->info
.info
.uses_prim_id
);
3601 bool break_wave_at_eoi
= false;
3605 if (es_type
== MESA_SHADER_TESS_EVAL
) {
3606 struct radv_shader_variant
*gs
=
3607 pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
3609 if (es_enable_prim_id
|| (gs
&& gs
->info
.info
.uses_prim_id
))
3610 break_wave_at_eoi
= true;
3613 nparams
= MAX2(outinfo
->param_exports
, 1);
3614 radeon_set_context_reg(ctx_cs
, R_0286C4_SPI_VS_OUT_CONFIG
,
3615 S_0286C4_VS_EXPORT_COUNT(nparams
- 1) |
3616 S_0286C4_NO_PC_EXPORT(outinfo
->param_exports
== 0));
3618 radeon_set_context_reg(ctx_cs
, R_028708_SPI_SHADER_IDX_FORMAT
,
3619 S_028708_IDX0_EXPORT_FORMAT(V_028708_SPI_SHADER_1COMP
));
3620 radeon_set_context_reg(ctx_cs
, R_02870C_SPI_SHADER_POS_FORMAT
,
3621 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP
) |
3622 S_02870C_POS1_EXPORT_FORMAT(outinfo
->pos_exports
> 1 ?
3623 V_02870C_SPI_SHADER_4COMP
:
3624 V_02870C_SPI_SHADER_NONE
) |
3625 S_02870C_POS2_EXPORT_FORMAT(outinfo
->pos_exports
> 2 ?
3626 V_02870C_SPI_SHADER_4COMP
:
3627 V_02870C_SPI_SHADER_NONE
) |
3628 S_02870C_POS3_EXPORT_FORMAT(outinfo
->pos_exports
> 3 ?
3629 V_02870C_SPI_SHADER_4COMP
:
3630 V_02870C_SPI_SHADER_NONE
));
3632 radeon_set_context_reg(ctx_cs
, R_028818_PA_CL_VTE_CNTL
,
3633 S_028818_VTX_W0_FMT(1) |
3634 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
3635 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
3636 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
3637 radeon_set_context_reg(ctx_cs
, R_02881C_PA_CL_VS_OUT_CNTL
,
3638 S_02881C_USE_VTX_POINT_SIZE(outinfo
->writes_pointsize
) |
3639 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo
->writes_layer
) |
3640 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo
->writes_viewport_index
) |
3641 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena
) |
3642 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena
) |
3643 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask
& 0x0f) != 0) |
3644 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask
& 0xf0) != 0) |
3645 cull_dist_mask
<< 8 |
3648 radeon_set_context_reg(ctx_cs
, R_028A84_VGT_PRIMITIVEID_EN
,
3649 S_028A84_PRIMITIVEID_EN(es_enable_prim_id
) |
3650 S_028A84_NGG_DISABLE_PROVOK_REUSE(es_enable_prim_id
));
3652 radeon_set_context_reg(ctx_cs
, R_028AAC_VGT_ESGS_RING_ITEMSIZE
,
3653 ngg_state
->vgt_esgs_ring_itemsize
);
3655 /* NGG specific registers. */
3656 struct radv_shader_variant
*gs
= pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
3657 uint32_t gs_num_invocations
= gs
? gs
->info
.gs
.invocations
: 1;
3659 radeon_set_context_reg(ctx_cs
, R_028A44_VGT_GS_ONCHIP_CNTL
,
3660 S_028A44_ES_VERTS_PER_SUBGRP(ngg_state
->hw_max_esverts
) |
3661 S_028A44_GS_PRIMS_PER_SUBGRP(ngg_state
->max_gsprims
) |
3662 S_028A44_GS_INST_PRIMS_IN_SUBGRP(ngg_state
->max_gsprims
* gs_num_invocations
));
3663 radeon_set_context_reg(ctx_cs
, R_0287FC_GE_MAX_OUTPUT_PER_SUBGROUP
,
3664 S_0287FC_MAX_VERTS_PER_SUBGROUP(ngg_state
->max_out_verts
));
3665 radeon_set_context_reg(ctx_cs
, R_028B4C_GE_NGG_SUBGRP_CNTL
,
3666 S_028B4C_PRIM_AMP_FACTOR(ngg_state
->prim_amp_factor
) |
3667 S_028B4C_THDS_PER_SUBGRP(0)); /* for fast launch */
3668 radeon_set_context_reg(ctx_cs
, R_028B90_VGT_GS_INSTANCE_CNT
,
3669 S_028B90_CNT(gs_num_invocations
) |
3670 S_028B90_ENABLE(gs_num_invocations
> 1) |
3671 S_028B90_EN_MAX_VERT_OUT_PER_GS_INSTANCE(ngg_state
->max_vert_out_per_gs_instance
));
3673 /* User edge flags are set by the pos exports. If user edge flags are
3674 * not used, we must use hw-generated edge flags and pass them via
3675 * the prim export to prevent drawing lines on internal edges of
3676 * decomposed primitives (such as quads) with polygon mode = lines.
3678 * TODO: We should combine hw-generated edge flags with user edge
3679 * flags in the shader.
3681 radeon_set_context_reg(ctx_cs
, R_028838_PA_CL_NGG_CNTL
,
3682 S_028838_INDEX_BUF_EDGE_FLAG_ENA(!radv_pipeline_has_tess(pipeline
) &&
3683 !radv_pipeline_has_gs(pipeline
)));
3685 ge_cntl
= S_03096C_PRIM_GRP_SIZE(ngg_state
->max_gsprims
) |
3686 S_03096C_VERT_GRP_SIZE(ngg_state
->hw_max_esverts
) |
3687 S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi
);
3689 /* Bug workaround for a possible hang with non-tessellation cases.
3690 * Tessellation always sets GE_CNTL.VERT_GRP_SIZE = 0
3692 * Requirement: GE_CNTL.VERT_GRP_SIZE = VGT_GS_ONCHIP_CNTL.ES_VERTS_PER_SUBGRP - 5
3694 if ((pipeline
->device
->physical_device
->rad_info
.family
== CHIP_NAVI10
||
3695 pipeline
->device
->physical_device
->rad_info
.family
== CHIP_NAVI12
||
3696 pipeline
->device
->physical_device
->rad_info
.family
== CHIP_NAVI14
) &&
3697 !radv_pipeline_has_tess(pipeline
) &&
3698 ngg_state
->hw_max_esverts
!= 256) {
3699 ge_cntl
&= C_03096C_VERT_GRP_SIZE
;
3701 if (ngg_state
->hw_max_esverts
> 5) {
3702 ge_cntl
|= S_03096C_VERT_GRP_SIZE(ngg_state
->hw_max_esverts
- 5);
3706 radeon_set_uconfig_reg(ctx_cs
, R_03096C_GE_CNTL
, ge_cntl
);
3710 radv_pipeline_generate_hw_hs(struct radeon_cmdbuf
*cs
,
3711 struct radv_pipeline
*pipeline
,
3712 struct radv_shader_variant
*shader
,
3713 const struct radv_tessellation_state
*tess
)
3715 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
3717 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
3718 unsigned hs_rsrc2
= shader
->config
.rsrc2
;
3720 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
3721 hs_rsrc2
|= S_00B42C_LDS_SIZE_GFX10(tess
->lds_size
);
3723 hs_rsrc2
|= S_00B42C_LDS_SIZE_GFX9(tess
->lds_size
);
3726 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
3727 radeon_set_sh_reg_seq(cs
, R_00B520_SPI_SHADER_PGM_LO_LS
, 2);
3728 radeon_emit(cs
, va
>> 8);
3729 radeon_emit(cs
, S_00B524_MEM_BASE(va
>> 40));
3731 radeon_set_sh_reg_seq(cs
, R_00B410_SPI_SHADER_PGM_LO_LS
, 2);
3732 radeon_emit(cs
, va
>> 8);
3733 radeon_emit(cs
, S_00B414_MEM_BASE(va
>> 40));
3736 radeon_set_sh_reg_seq(cs
, R_00B428_SPI_SHADER_PGM_RSRC1_HS
, 2);
3737 radeon_emit(cs
, shader
->config
.rsrc1
);
3738 radeon_emit(cs
, hs_rsrc2
);
3740 radeon_set_sh_reg_seq(cs
, R_00B420_SPI_SHADER_PGM_LO_HS
, 4);
3741 radeon_emit(cs
, va
>> 8);
3742 radeon_emit(cs
, S_00B424_MEM_BASE(va
>> 40));
3743 radeon_emit(cs
, shader
->config
.rsrc1
);
3744 radeon_emit(cs
, shader
->config
.rsrc2
);
3749 radv_pipeline_generate_vertex_shader(struct radeon_cmdbuf
*ctx_cs
,
3750 struct radeon_cmdbuf
*cs
,
3751 struct radv_pipeline
*pipeline
,
3752 const struct radv_tessellation_state
*tess
,
3753 const struct radv_ngg_state
*ngg
)
3755 struct radv_shader_variant
*vs
;
3757 /* Skip shaders merged into HS/GS */
3758 vs
= pipeline
->shaders
[MESA_SHADER_VERTEX
];
3762 if (vs
->info
.vs
.as_ls
)
3763 radv_pipeline_generate_hw_ls(cs
, pipeline
, vs
, tess
);
3764 else if (vs
->info
.vs
.as_es
)
3765 radv_pipeline_generate_hw_es(cs
, pipeline
, vs
);
3766 else if (vs
->info
.is_ngg
)
3767 radv_pipeline_generate_hw_ngg(ctx_cs
, cs
, pipeline
, vs
, ngg
);
3769 radv_pipeline_generate_hw_vs(ctx_cs
, cs
, pipeline
, vs
);
3773 radv_pipeline_generate_tess_shaders(struct radeon_cmdbuf
*ctx_cs
,
3774 struct radeon_cmdbuf
*cs
,
3775 struct radv_pipeline
*pipeline
,
3776 const struct radv_tessellation_state
*tess
,
3777 const struct radv_ngg_state
*ngg
)
3779 if (!radv_pipeline_has_tess(pipeline
))
3782 struct radv_shader_variant
*tes
, *tcs
;
3784 tcs
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
];
3785 tes
= pipeline
->shaders
[MESA_SHADER_TESS_EVAL
];
3788 if (tes
->info
.is_ngg
) {
3789 radv_pipeline_generate_hw_ngg(ctx_cs
, cs
, pipeline
, tes
, ngg
);
3790 } else if (tes
->info
.tes
.as_es
)
3791 radv_pipeline_generate_hw_es(cs
, pipeline
, tes
);
3793 radv_pipeline_generate_hw_vs(ctx_cs
, cs
, pipeline
, tes
);
3796 radv_pipeline_generate_hw_hs(cs
, pipeline
, tcs
, tess
);
3798 radeon_set_context_reg(ctx_cs
, R_028B6C_VGT_TF_PARAM
,
3801 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX7
)
3802 radeon_set_context_reg_idx(ctx_cs
, R_028B58_VGT_LS_HS_CONFIG
, 2,
3803 tess
->ls_hs_config
);
3805 radeon_set_context_reg(ctx_cs
, R_028B58_VGT_LS_HS_CONFIG
,
3806 tess
->ls_hs_config
);
3810 radv_pipeline_generate_hw_gs(struct radeon_cmdbuf
*ctx_cs
,
3811 struct radeon_cmdbuf
*cs
,
3812 struct radv_pipeline
*pipeline
,
3813 struct radv_shader_variant
*gs
,
3814 const struct radv_gs_state
*gs_state
)
3816 unsigned gs_max_out_vertices
;
3817 uint8_t *num_components
;
3822 gs_max_out_vertices
= gs
->info
.gs
.vertices_out
;
3823 max_stream
= gs
->info
.info
.gs
.max_stream
;
3824 num_components
= gs
->info
.info
.gs
.num_stream_output_components
;
3826 offset
= num_components
[0] * gs_max_out_vertices
;
3828 radeon_set_context_reg_seq(ctx_cs
, R_028A60_VGT_GSVS_RING_OFFSET_1
, 3);
3829 radeon_emit(ctx_cs
, offset
);
3830 if (max_stream
>= 1)
3831 offset
+= num_components
[1] * gs_max_out_vertices
;
3832 radeon_emit(ctx_cs
, offset
);
3833 if (max_stream
>= 2)
3834 offset
+= num_components
[2] * gs_max_out_vertices
;
3835 radeon_emit(ctx_cs
, offset
);
3836 if (max_stream
>= 3)
3837 offset
+= num_components
[3] * gs_max_out_vertices
;
3838 radeon_set_context_reg(ctx_cs
, R_028AB0_VGT_GSVS_RING_ITEMSIZE
, offset
);
3840 radeon_set_context_reg_seq(ctx_cs
, R_028B5C_VGT_GS_VERT_ITEMSIZE
, 4);
3841 radeon_emit(ctx_cs
, num_components
[0]);
3842 radeon_emit(ctx_cs
, (max_stream
>= 1) ? num_components
[1] : 0);
3843 radeon_emit(ctx_cs
, (max_stream
>= 2) ? num_components
[2] : 0);
3844 radeon_emit(ctx_cs
, (max_stream
>= 3) ? num_components
[3] : 0);
3846 uint32_t gs_num_invocations
= gs
->info
.gs
.invocations
;
3847 radeon_set_context_reg(ctx_cs
, R_028B90_VGT_GS_INSTANCE_CNT
,
3848 S_028B90_CNT(MIN2(gs_num_invocations
, 127)) |
3849 S_028B90_ENABLE(gs_num_invocations
> 0));
3851 radeon_set_context_reg(ctx_cs
, R_028AAC_VGT_ESGS_RING_ITEMSIZE
,
3852 gs_state
->vgt_esgs_ring_itemsize
);
3854 va
= radv_buffer_get_va(gs
->bo
) + gs
->bo_offset
;
3856 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
3857 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
3858 radeon_set_sh_reg_seq(cs
, R_00B320_SPI_SHADER_PGM_LO_ES
, 2);
3859 radeon_emit(cs
, va
>> 8);
3860 radeon_emit(cs
, S_00B324_MEM_BASE(va
>> 40));
3862 radeon_set_sh_reg_seq(cs
, R_00B210_SPI_SHADER_PGM_LO_ES
, 2);
3863 radeon_emit(cs
, va
>> 8);
3864 radeon_emit(cs
, S_00B214_MEM_BASE(va
>> 40));
3867 radeon_set_sh_reg_seq(cs
, R_00B228_SPI_SHADER_PGM_RSRC1_GS
, 2);
3868 radeon_emit(cs
, gs
->config
.rsrc1
);
3869 radeon_emit(cs
, gs
->config
.rsrc2
| S_00B22C_LDS_SIZE(gs_state
->lds_size
));
3871 radeon_set_context_reg(ctx_cs
, R_028A44_VGT_GS_ONCHIP_CNTL
, gs_state
->vgt_gs_onchip_cntl
);
3872 radeon_set_context_reg(ctx_cs
, R_028A94_VGT_GS_MAX_PRIMS_PER_SUBGROUP
, gs_state
->vgt_gs_max_prims_per_subgroup
);
3874 radeon_set_sh_reg_seq(cs
, R_00B220_SPI_SHADER_PGM_LO_GS
, 4);
3875 radeon_emit(cs
, va
>> 8);
3876 radeon_emit(cs
, S_00B224_MEM_BASE(va
>> 40));
3877 radeon_emit(cs
, gs
->config
.rsrc1
);
3878 radeon_emit(cs
, gs
->config
.rsrc2
);
3881 radv_pipeline_generate_hw_vs(ctx_cs
, cs
, pipeline
, pipeline
->gs_copy_shader
);
3885 radv_pipeline_generate_geometry_shader(struct radeon_cmdbuf
*ctx_cs
,
3886 struct radeon_cmdbuf
*cs
,
3887 struct radv_pipeline
*pipeline
,
3888 const struct radv_gs_state
*gs_state
,
3889 const struct radv_ngg_state
*ngg_state
)
3891 struct radv_shader_variant
*gs
;
3893 gs
= pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
3897 if (gs
->info
.is_ngg
)
3898 radv_pipeline_generate_hw_ngg(ctx_cs
, cs
, pipeline
, gs
, ngg_state
);
3900 radv_pipeline_generate_hw_gs(ctx_cs
, cs
, pipeline
, gs
, gs_state
);
3902 radeon_set_context_reg(ctx_cs
, R_028B38_VGT_GS_MAX_VERT_OUT
,
3903 gs
->info
.gs
.vertices_out
);
3906 static uint32_t offset_to_ps_input(uint32_t offset
, bool flat_shade
, bool float16
)
3908 uint32_t ps_input_cntl
;
3909 if (offset
<= AC_EXP_PARAM_OFFSET_31
) {
3910 ps_input_cntl
= S_028644_OFFSET(offset
);
3912 ps_input_cntl
|= S_028644_FLAT_SHADE(1);
3914 ps_input_cntl
|= S_028644_FP16_INTERP_MODE(1) |
3915 S_028644_ATTR0_VALID(1);
3918 /* The input is a DEFAULT_VAL constant. */
3919 assert(offset
>= AC_EXP_PARAM_DEFAULT_VAL_0000
&&
3920 offset
<= AC_EXP_PARAM_DEFAULT_VAL_1111
);
3921 offset
-= AC_EXP_PARAM_DEFAULT_VAL_0000
;
3922 ps_input_cntl
= S_028644_OFFSET(0x20) |
3923 S_028644_DEFAULT_VAL(offset
);
3925 return ps_input_cntl
;
3929 radv_pipeline_generate_ps_inputs(struct radeon_cmdbuf
*ctx_cs
,
3930 struct radv_pipeline
*pipeline
)
3932 struct radv_shader_variant
*ps
= pipeline
->shaders
[MESA_SHADER_FRAGMENT
];
3933 const struct radv_vs_output_info
*outinfo
= get_vs_output_info(pipeline
);
3934 uint32_t ps_input_cntl
[32];
3936 unsigned ps_offset
= 0;
3938 if (ps
->info
.info
.ps
.prim_id_input
) {
3939 unsigned vs_offset
= outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
];
3940 if (vs_offset
!= AC_EXP_PARAM_UNDEFINED
) {
3941 ps_input_cntl
[ps_offset
] = offset_to_ps_input(vs_offset
, true, false);
3946 if (ps
->info
.info
.ps
.layer_input
||
3947 ps
->info
.info
.needs_multiview_view_index
) {
3948 unsigned vs_offset
= outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
];
3949 if (vs_offset
!= AC_EXP_PARAM_UNDEFINED
)
3950 ps_input_cntl
[ps_offset
] = offset_to_ps_input(vs_offset
, true, false);
3952 ps_input_cntl
[ps_offset
] = offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000
, true, false);
3956 if (ps
->info
.info
.ps
.has_pcoord
) {
3958 val
= S_028644_PT_SPRITE_TEX(1) | S_028644_OFFSET(0x20);
3959 ps_input_cntl
[ps_offset
] = val
;
3963 if (ps
->info
.info
.ps
.num_input_clips_culls
) {
3966 vs_offset
= outinfo
->vs_output_param_offset
[VARYING_SLOT_CLIP_DIST0
];
3967 if (vs_offset
!= AC_EXP_PARAM_UNDEFINED
) {
3968 ps_input_cntl
[ps_offset
] = offset_to_ps_input(vs_offset
, false, false);
3972 vs_offset
= outinfo
->vs_output_param_offset
[VARYING_SLOT_CLIP_DIST1
];
3973 if (vs_offset
!= AC_EXP_PARAM_UNDEFINED
&&
3974 ps
->info
.info
.ps
.num_input_clips_culls
> 4) {
3975 ps_input_cntl
[ps_offset
] = offset_to_ps_input(vs_offset
, false, false);
3980 for (unsigned i
= 0; i
< 32 && (1u << i
) <= ps
->info
.fs
.input_mask
; ++i
) {
3984 if (!(ps
->info
.fs
.input_mask
& (1u << i
)))
3987 vs_offset
= outinfo
->vs_output_param_offset
[VARYING_SLOT_VAR0
+ i
];
3988 if (vs_offset
== AC_EXP_PARAM_UNDEFINED
) {
3989 ps_input_cntl
[ps_offset
] = S_028644_OFFSET(0x20);
3994 flat_shade
= !!(ps
->info
.fs
.flat_shaded_mask
& (1u << ps_offset
));
3995 float16
= !!(ps
->info
.fs
.float16_shaded_mask
& (1u << ps_offset
));
3997 ps_input_cntl
[ps_offset
] = offset_to_ps_input(vs_offset
, flat_shade
, float16
);
4002 radeon_set_context_reg_seq(ctx_cs
, R_028644_SPI_PS_INPUT_CNTL_0
, ps_offset
);
4003 for (unsigned i
= 0; i
< ps_offset
; i
++) {
4004 radeon_emit(ctx_cs
, ps_input_cntl
[i
]);
4010 radv_compute_db_shader_control(const struct radv_device
*device
,
4011 const struct radv_pipeline
*pipeline
,
4012 const struct radv_shader_variant
*ps
)
4015 if (ps
->info
.fs
.early_fragment_test
|| !ps
->info
.info
.ps
.writes_memory
)
4016 z_order
= V_02880C_EARLY_Z_THEN_LATE_Z
;
4018 z_order
= V_02880C_LATE_Z
;
4020 bool disable_rbplus
= device
->physical_device
->has_rbplus
&&
4021 !device
->physical_device
->rbplus_allowed
;
4023 /* It shouldn't be needed to export gl_SampleMask when MSAA is disabled
4024 * but this appears to break Project Cars (DXVK). See
4025 * https://bugs.freedesktop.org/show_bug.cgi?id=109401
4027 bool mask_export_enable
= ps
->info
.info
.ps
.writes_sample_mask
;
4029 return S_02880C_Z_EXPORT_ENABLE(ps
->info
.info
.ps
.writes_z
) |
4030 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps
->info
.info
.ps
.writes_stencil
) |
4031 S_02880C_KILL_ENABLE(!!ps
->info
.fs
.can_discard
) |
4032 S_02880C_MASK_EXPORT_ENABLE(mask_export_enable
) |
4033 S_02880C_Z_ORDER(z_order
) |
4034 S_02880C_DEPTH_BEFORE_SHADER(ps
->info
.fs
.early_fragment_test
) |
4035 S_02880C_PRE_SHADER_DEPTH_COVERAGE_ENABLE(ps
->info
.fs
.post_depth_coverage
) |
4036 S_02880C_EXEC_ON_HIER_FAIL(ps
->info
.info
.ps
.writes_memory
) |
4037 S_02880C_EXEC_ON_NOOP(ps
->info
.info
.ps
.writes_memory
) |
4038 S_02880C_DUAL_QUAD_DISABLE(disable_rbplus
);
4042 radv_pipeline_generate_fragment_shader(struct radeon_cmdbuf
*ctx_cs
,
4043 struct radeon_cmdbuf
*cs
,
4044 struct radv_pipeline
*pipeline
)
4046 struct radv_shader_variant
*ps
;
4048 assert (pipeline
->shaders
[MESA_SHADER_FRAGMENT
]);
4050 ps
= pipeline
->shaders
[MESA_SHADER_FRAGMENT
];
4051 va
= radv_buffer_get_va(ps
->bo
) + ps
->bo_offset
;
4053 radeon_set_sh_reg_seq(cs
, R_00B020_SPI_SHADER_PGM_LO_PS
, 4);
4054 radeon_emit(cs
, va
>> 8);
4055 radeon_emit(cs
, S_00B024_MEM_BASE(va
>> 40));
4056 radeon_emit(cs
, ps
->config
.rsrc1
);
4057 radeon_emit(cs
, ps
->config
.rsrc2
);
4059 radeon_set_context_reg(ctx_cs
, R_02880C_DB_SHADER_CONTROL
,
4060 radv_compute_db_shader_control(pipeline
->device
,
4063 radeon_set_context_reg(ctx_cs
, R_0286CC_SPI_PS_INPUT_ENA
,
4064 ps
->config
.spi_ps_input_ena
);
4066 radeon_set_context_reg(ctx_cs
, R_0286D0_SPI_PS_INPUT_ADDR
,
4067 ps
->config
.spi_ps_input_addr
);
4069 radeon_set_context_reg(ctx_cs
, R_0286D8_SPI_PS_IN_CONTROL
,
4070 S_0286D8_NUM_INTERP(ps
->info
.fs
.num_interp
) |
4071 S_0286D8_PS_W32_EN(ps
->info
.info
.wave_size
== 32));
4073 radeon_set_context_reg(ctx_cs
, R_0286E0_SPI_BARYC_CNTL
, pipeline
->graphics
.spi_baryc_cntl
);
4075 radeon_set_context_reg(ctx_cs
, R_028710_SPI_SHADER_Z_FORMAT
,
4076 ac_get_spi_shader_z_format(ps
->info
.info
.ps
.writes_z
,
4077 ps
->info
.info
.ps
.writes_stencil
,
4078 ps
->info
.info
.ps
.writes_sample_mask
));
4080 if (pipeline
->device
->dfsm_allowed
) {
4081 /* optimise this? */
4082 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
4083 radeon_emit(cs
, EVENT_TYPE(V_028A90_FLUSH_DFSM
) | EVENT_INDEX(0));
4088 radv_pipeline_generate_vgt_vertex_reuse(struct radeon_cmdbuf
*ctx_cs
,
4089 struct radv_pipeline
*pipeline
)
4091 if (pipeline
->device
->physical_device
->rad_info
.family
< CHIP_POLARIS10
||
4092 pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
)
4095 unsigned vtx_reuse_depth
= 30;
4096 if (radv_pipeline_has_tess(pipeline
) &&
4097 radv_get_shader(pipeline
, MESA_SHADER_TESS_EVAL
)->info
.tes
.spacing
== TESS_SPACING_FRACTIONAL_ODD
) {
4098 vtx_reuse_depth
= 14;
4100 radeon_set_context_reg(ctx_cs
, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL
,
4101 S_028C58_VTX_REUSE_DEPTH(vtx_reuse_depth
));
4105 radv_compute_vgt_shader_stages_en(const struct radv_pipeline
*pipeline
)
4107 uint32_t stages
= 0;
4108 if (radv_pipeline_has_tess(pipeline
)) {
4109 stages
|= S_028B54_LS_EN(V_028B54_LS_STAGE_ON
) |
4110 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
4112 if (radv_pipeline_has_gs(pipeline
))
4113 stages
|= S_028B54_ES_EN(V_028B54_ES_STAGE_DS
) |
4115 else if (radv_pipeline_has_ngg(pipeline
))
4116 stages
|= S_028B54_ES_EN(V_028B54_ES_STAGE_DS
);
4118 stages
|= S_028B54_VS_EN(V_028B54_VS_STAGE_DS
);
4119 } else if (radv_pipeline_has_gs(pipeline
)) {
4120 stages
|= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL
) |
4122 } else if (radv_pipeline_has_ngg(pipeline
)) {
4123 stages
|= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL
);
4126 if (radv_pipeline_has_ngg(pipeline
)) {
4127 stages
|= S_028B54_PRIMGEN_EN(1);
4128 } else if (radv_pipeline_has_gs(pipeline
)) {
4129 stages
|= S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER
);
4132 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
)
4133 stages
|= S_028B54_MAX_PRIMGRP_IN_WAVE(2);
4135 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
4136 uint8_t hs_size
= 64, gs_size
= 64, vs_size
= 64;
4138 if (radv_pipeline_has_tess(pipeline
))
4139 hs_size
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.info
.wave_size
;
4141 if (pipeline
->shaders
[MESA_SHADER_GEOMETRY
]) {
4142 vs_size
= gs_size
= pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
.info
.wave_size
;
4143 if (pipeline
->gs_copy_shader
)
4144 vs_size
= pipeline
->gs_copy_shader
->info
.info
.wave_size
;
4145 } else if (pipeline
->shaders
[MESA_SHADER_TESS_EVAL
])
4146 vs_size
= pipeline
->shaders
[MESA_SHADER_TESS_EVAL
]->info
.info
.wave_size
;
4147 else if (pipeline
->shaders
[MESA_SHADER_VERTEX
])
4148 vs_size
= pipeline
->shaders
[MESA_SHADER_VERTEX
]->info
.info
.wave_size
;
4150 if (radv_pipeline_has_ngg(pipeline
))
4153 /* legacy GS only supports Wave64 */
4154 stages
|= S_028B54_HS_W32_EN(hs_size
== 32 ? 1 : 0) |
4155 S_028B54_GS_W32_EN(gs_size
== 32 ? 1 : 0) |
4156 S_028B54_VS_W32_EN(vs_size
== 32 ? 1 : 0);
4163 radv_compute_cliprect_rule(const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
4165 const VkPipelineDiscardRectangleStateCreateInfoEXT
*discard_rectangle_info
=
4166 vk_find_struct_const(pCreateInfo
->pNext
, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT
);
4168 if (!discard_rectangle_info
)
4173 for (unsigned i
= 0; i
< (1u << MAX_DISCARD_RECTANGLES
); ++i
) {
4174 /* Interpret i as a bitmask, and then set the bit in the mask if
4175 * that combination of rectangles in which the pixel is contained
4176 * should pass the cliprect test. */
4177 unsigned relevant_subset
= i
& ((1u << discard_rectangle_info
->discardRectangleCount
) - 1);
4179 if (discard_rectangle_info
->discardRectangleMode
== VK_DISCARD_RECTANGLE_MODE_INCLUSIVE_EXT
&&
4183 if (discard_rectangle_info
->discardRectangleMode
== VK_DISCARD_RECTANGLE_MODE_EXCLUSIVE_EXT
&&
4194 gfx10_pipeline_generate_ge_cntl(struct radeon_cmdbuf
*ctx_cs
,
4195 struct radv_pipeline
*pipeline
,
4196 const struct radv_tessellation_state
*tess
,
4197 const struct radv_gs_state
*gs_state
)
4199 bool break_wave_at_eoi
= false;
4200 unsigned primgroup_size
;
4201 unsigned vertgroup_size
;
4203 if (radv_pipeline_has_tess(pipeline
)) {
4204 primgroup_size
= tess
->num_patches
; /* must be a multiple of NUM_PATCHES */
4206 } else if (radv_pipeline_has_gs(pipeline
)) {
4207 unsigned vgt_gs_onchip_cntl
= gs_state
->vgt_gs_onchip_cntl
;
4208 primgroup_size
= G_028A44_GS_PRIMS_PER_SUBGRP(vgt_gs_onchip_cntl
);
4209 vertgroup_size
= G_028A44_ES_VERTS_PER_SUBGRP(vgt_gs_onchip_cntl
);
4211 primgroup_size
= 128; /* recommended without a GS and tess */
4215 if (radv_pipeline_has_tess(pipeline
)) {
4216 if (pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.info
.uses_prim_id
||
4217 radv_get_shader(pipeline
, MESA_SHADER_TESS_EVAL
)->info
.info
.uses_prim_id
)
4218 break_wave_at_eoi
= true;
4221 radeon_set_uconfig_reg(ctx_cs
, R_03096C_GE_CNTL
,
4222 S_03096C_PRIM_GRP_SIZE(primgroup_size
) |
4223 S_03096C_VERT_GRP_SIZE(vertgroup_size
) |
4224 S_03096C_PACKET_TO_ONE_PA(0) /* line stipple */ |
4225 S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi
));
4229 radv_pipeline_generate_pm4(struct radv_pipeline
*pipeline
,
4230 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
4231 const struct radv_graphics_pipeline_create_info
*extra
,
4232 const struct radv_blend_state
*blend
,
4233 const struct radv_tessellation_state
*tess
,
4234 const struct radv_gs_state
*gs
,
4235 const struct radv_ngg_state
*ngg
,
4236 unsigned prim
, unsigned gs_out
)
4238 struct radeon_cmdbuf
*ctx_cs
= &pipeline
->ctx_cs
;
4239 struct radeon_cmdbuf
*cs
= &pipeline
->cs
;
4242 ctx_cs
->max_dw
= 256;
4243 cs
->buf
= malloc(4 * (cs
->max_dw
+ ctx_cs
->max_dw
));
4244 ctx_cs
->buf
= cs
->buf
+ cs
->max_dw
;
4246 radv_pipeline_generate_depth_stencil_state(ctx_cs
, pipeline
, pCreateInfo
, extra
);
4247 radv_pipeline_generate_blend_state(ctx_cs
, pipeline
, blend
);
4248 radv_pipeline_generate_raster_state(ctx_cs
, pipeline
, pCreateInfo
);
4249 radv_pipeline_generate_multisample_state(ctx_cs
, pipeline
);
4250 radv_pipeline_generate_vgt_gs_mode(ctx_cs
, pipeline
);
4251 radv_pipeline_generate_vertex_shader(ctx_cs
, cs
, pipeline
, tess
, ngg
);
4252 radv_pipeline_generate_tess_shaders(ctx_cs
, cs
, pipeline
, tess
, ngg
);
4253 radv_pipeline_generate_geometry_shader(ctx_cs
, cs
, pipeline
, gs
, ngg
);
4254 radv_pipeline_generate_fragment_shader(ctx_cs
, cs
, pipeline
);
4255 radv_pipeline_generate_ps_inputs(ctx_cs
, pipeline
);
4256 radv_pipeline_generate_vgt_vertex_reuse(ctx_cs
, pipeline
);
4257 radv_pipeline_generate_binning_state(ctx_cs
, pipeline
, pCreateInfo
);
4259 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX10
&& !radv_pipeline_has_ngg(pipeline
))
4260 gfx10_pipeline_generate_ge_cntl(ctx_cs
, pipeline
, tess
, gs
);
4262 radeon_set_context_reg(ctx_cs
, R_0286E8_SPI_TMPRING_SIZE
,
4263 S_0286E8_WAVES(pipeline
->max_waves
) |
4264 S_0286E8_WAVESIZE(pipeline
->scratch_bytes_per_wave
>> 10));
4266 radeon_set_context_reg(ctx_cs
, R_028B54_VGT_SHADER_STAGES_EN
, radv_compute_vgt_shader_stages_en(pipeline
));
4268 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX7
) {
4269 radeon_set_uconfig_reg_idx(pipeline
->device
->physical_device
,
4270 cs
, R_030908_VGT_PRIMITIVE_TYPE
, 1, prim
);
4272 radeon_set_config_reg(cs
, R_008958_VGT_PRIMITIVE_TYPE
, prim
);
4274 radeon_set_context_reg(ctx_cs
, R_028A6C_VGT_GS_OUT_PRIM_TYPE
, gs_out
);
4276 radeon_set_context_reg(ctx_cs
, R_02820C_PA_SC_CLIPRECT_RULE
, radv_compute_cliprect_rule(pCreateInfo
));
4278 pipeline
->ctx_cs_hash
= _mesa_hash_data(ctx_cs
->buf
, ctx_cs
->cdw
* 4);
4280 assert(ctx_cs
->cdw
<= ctx_cs
->max_dw
);
4281 assert(cs
->cdw
<= cs
->max_dw
);
4284 static struct radv_ia_multi_vgt_param_helpers
4285 radv_compute_ia_multi_vgt_param_helpers(struct radv_pipeline
*pipeline
,
4286 const struct radv_tessellation_state
*tess
,
4289 struct radv_ia_multi_vgt_param_helpers ia_multi_vgt_param
= {0};
4290 const struct radv_device
*device
= pipeline
->device
;
4292 if (radv_pipeline_has_tess(pipeline
))
4293 ia_multi_vgt_param
.primgroup_size
= tess
->num_patches
;
4294 else if (radv_pipeline_has_gs(pipeline
))
4295 ia_multi_vgt_param
.primgroup_size
= 64;
4297 ia_multi_vgt_param
.primgroup_size
= 128; /* recommended without a GS */
4299 /* GS requirement. */
4300 ia_multi_vgt_param
.partial_es_wave
= false;
4301 if (radv_pipeline_has_gs(pipeline
) && device
->physical_device
->rad_info
.chip_class
<= GFX8
)
4302 if (SI_GS_PER_ES
/ ia_multi_vgt_param
.primgroup_size
>= pipeline
->device
->gs_table_depth
- 3)
4303 ia_multi_vgt_param
.partial_es_wave
= true;
4305 ia_multi_vgt_param
.wd_switch_on_eop
= false;
4306 if (device
->physical_device
->rad_info
.chip_class
>= GFX7
) {
4307 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
4308 * 4 shader engines. Set 1 to pass the assertion below.
4309 * The other cases are hardware requirements. */
4310 if (device
->physical_device
->rad_info
.max_se
< 4 ||
4311 prim
== V_008958_DI_PT_POLYGON
||
4312 prim
== V_008958_DI_PT_LINELOOP
||
4313 prim
== V_008958_DI_PT_TRIFAN
||
4314 prim
== V_008958_DI_PT_TRISTRIP_ADJ
||
4315 (pipeline
->graphics
.prim_restart_enable
&&
4316 (device
->physical_device
->rad_info
.family
< CHIP_POLARIS10
||
4317 (prim
!= V_008958_DI_PT_POINTLIST
&&
4318 prim
!= V_008958_DI_PT_LINESTRIP
))))
4319 ia_multi_vgt_param
.wd_switch_on_eop
= true;
4322 ia_multi_vgt_param
.ia_switch_on_eoi
= false;
4323 if (pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.prim_id_input
)
4324 ia_multi_vgt_param
.ia_switch_on_eoi
= true;
4325 if (radv_pipeline_has_gs(pipeline
) &&
4326 pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
.info
.uses_prim_id
)
4327 ia_multi_vgt_param
.ia_switch_on_eoi
= true;
4328 if (radv_pipeline_has_tess(pipeline
)) {
4329 /* SWITCH_ON_EOI must be set if PrimID is used. */
4330 if (pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.info
.uses_prim_id
||
4331 radv_get_shader(pipeline
, MESA_SHADER_TESS_EVAL
)->info
.info
.uses_prim_id
)
4332 ia_multi_vgt_param
.ia_switch_on_eoi
= true;
4335 ia_multi_vgt_param
.partial_vs_wave
= false;
4336 if (radv_pipeline_has_tess(pipeline
)) {
4337 /* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
4338 if ((device
->physical_device
->rad_info
.family
== CHIP_TAHITI
||
4339 device
->physical_device
->rad_info
.family
== CHIP_PITCAIRN
||
4340 device
->physical_device
->rad_info
.family
== CHIP_BONAIRE
) &&
4341 radv_pipeline_has_gs(pipeline
))
4342 ia_multi_vgt_param
.partial_vs_wave
= true;
4343 /* Needed for 028B6C_DISTRIBUTION_MODE != 0 */
4344 if (device
->has_distributed_tess
) {
4345 if (radv_pipeline_has_gs(pipeline
)) {
4346 if (device
->physical_device
->rad_info
.chip_class
<= GFX8
)
4347 ia_multi_vgt_param
.partial_es_wave
= true;
4349 ia_multi_vgt_param
.partial_vs_wave
= true;
4354 /* Workaround for a VGT hang when strip primitive types are used with
4355 * primitive restart.
4357 if (pipeline
->graphics
.prim_restart_enable
&&
4358 (prim
== V_008958_DI_PT_LINESTRIP
||
4359 prim
== V_008958_DI_PT_TRISTRIP
||
4360 prim
== V_008958_DI_PT_LINESTRIP_ADJ
||
4361 prim
== V_008958_DI_PT_TRISTRIP_ADJ
)) {
4362 ia_multi_vgt_param
.partial_vs_wave
= true;
4365 if (radv_pipeline_has_gs(pipeline
)) {
4366 /* On these chips there is the possibility of a hang if the
4367 * pipeline uses a GS and partial_vs_wave is not set.
4369 * This mostly does not hit 4-SE chips, as those typically set
4370 * ia_switch_on_eoi and then partial_vs_wave is set for pipelines
4371 * with GS due to another workaround.
4373 * Reproducer: https://bugs.freedesktop.org/show_bug.cgi?id=109242
4375 if (device
->physical_device
->rad_info
.family
== CHIP_TONGA
||
4376 device
->physical_device
->rad_info
.family
== CHIP_FIJI
||
4377 device
->physical_device
->rad_info
.family
== CHIP_POLARIS10
||
4378 device
->physical_device
->rad_info
.family
== CHIP_POLARIS11
||
4379 device
->physical_device
->rad_info
.family
== CHIP_POLARIS12
||
4380 device
->physical_device
->rad_info
.family
== CHIP_VEGAM
) {
4381 ia_multi_vgt_param
.partial_vs_wave
= true;
4385 ia_multi_vgt_param
.base
=
4386 S_028AA8_PRIMGROUP_SIZE(ia_multi_vgt_param
.primgroup_size
- 1) |
4387 /* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */
4388 S_028AA8_MAX_PRIMGRP_IN_WAVE(device
->physical_device
->rad_info
.chip_class
== GFX8
? 2 : 0) |
4389 S_030960_EN_INST_OPT_BASIC(device
->physical_device
->rad_info
.chip_class
>= GFX9
) |
4390 S_030960_EN_INST_OPT_ADV(device
->physical_device
->rad_info
.chip_class
>= GFX9
);
4392 return ia_multi_vgt_param
;
4397 radv_compute_vertex_input_state(struct radv_pipeline
*pipeline
,
4398 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
4400 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
4401 pCreateInfo
->pVertexInputState
;
4402 struct radv_vertex_elements_info
*velems
= &pipeline
->vertex_elements
;
4404 for (uint32_t i
= 0; i
< vi_info
->vertexAttributeDescriptionCount
; i
++) {
4405 const VkVertexInputAttributeDescription
*desc
=
4406 &vi_info
->pVertexAttributeDescriptions
[i
];
4407 unsigned loc
= desc
->location
;
4408 const struct vk_format_description
*format_desc
;
4410 format_desc
= vk_format_description(desc
->format
);
4412 velems
->format_size
[loc
] = format_desc
->block
.bits
/ 8;
4415 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
4416 const VkVertexInputBindingDescription
*desc
=
4417 &vi_info
->pVertexBindingDescriptions
[i
];
4419 pipeline
->binding_stride
[desc
->binding
] = desc
->stride
;
4420 pipeline
->num_vertex_bindings
=
4421 MAX2(pipeline
->num_vertex_bindings
, desc
->binding
+ 1);
4425 static struct radv_shader_variant
*
4426 radv_pipeline_get_streamout_shader(struct radv_pipeline
*pipeline
)
4430 for (i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
4431 struct radv_shader_variant
*shader
=
4432 radv_get_shader(pipeline
, i
);
4434 if (shader
&& shader
->info
.info
.so
.num_outputs
> 0)
4442 radv_pipeline_init(struct radv_pipeline
*pipeline
,
4443 struct radv_device
*device
,
4444 struct radv_pipeline_cache
*cache
,
4445 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
4446 const struct radv_graphics_pipeline_create_info
*extra
)
4449 bool has_view_index
= false;
4451 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
4452 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
4453 if (subpass
->view_mask
)
4454 has_view_index
= true;
4456 pipeline
->device
= device
;
4457 pipeline
->layout
= radv_pipeline_layout_from_handle(pCreateInfo
->layout
);
4458 assert(pipeline
->layout
);
4460 struct radv_blend_state blend
= radv_pipeline_init_blend_state(pipeline
, pCreateInfo
, extra
);
4462 const VkPipelineCreationFeedbackCreateInfoEXT
*creation_feedback
=
4463 vk_find_struct_const(pCreateInfo
->pNext
, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT
);
4464 radv_init_feedback(creation_feedback
);
4466 VkPipelineCreationFeedbackEXT
*pipeline_feedback
= creation_feedback
? creation_feedback
->pPipelineCreationFeedback
: NULL
;
4468 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = { 0, };
4469 VkPipelineCreationFeedbackEXT
*stage_feedbacks
[MESA_SHADER_STAGES
] = { 0 };
4470 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
4471 gl_shader_stage stage
= ffs(pCreateInfo
->pStages
[i
].stage
) - 1;
4472 pStages
[stage
] = &pCreateInfo
->pStages
[i
];
4473 if(creation_feedback
)
4474 stage_feedbacks
[stage
] = &creation_feedback
->pPipelineStageCreationFeedbacks
[i
];
4477 struct radv_pipeline_key key
= radv_generate_graphics_pipeline_key(pipeline
, pCreateInfo
, &blend
, has_view_index
);
4478 radv_create_shaders(pipeline
, device
, cache
, &key
, pStages
, pCreateInfo
->flags
, pipeline_feedback
, stage_feedbacks
);
4480 pipeline
->graphics
.spi_baryc_cntl
= S_0286E0_FRONT_FACE_ALL_BITS(1);
4481 radv_pipeline_init_multisample_state(pipeline
, &blend
, pCreateInfo
);
4483 uint32_t prim
= si_translate_prim(pCreateInfo
->pInputAssemblyState
->topology
);
4485 pipeline
->graphics
.can_use_guardband
= radv_prim_can_use_guardband(pCreateInfo
->pInputAssemblyState
->topology
);
4487 if (radv_pipeline_has_gs(pipeline
)) {
4488 gs_out
= si_conv_gl_prim_to_gs_out(pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
.gs
.output_prim
);
4489 pipeline
->graphics
.can_use_guardband
= gs_out
== V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
4490 } else if (radv_pipeline_has_tess(pipeline
)) {
4491 if (pipeline
->shaders
[MESA_SHADER_TESS_EVAL
]->info
.tes
.point_mode
)
4492 gs_out
= V_028A6C_OUTPRIM_TYPE_POINTLIST
;
4494 gs_out
= si_conv_gl_prim_to_gs_out(pipeline
->shaders
[MESA_SHADER_TESS_EVAL
]->info
.tes
.primitive_mode
);
4495 pipeline
->graphics
.can_use_guardband
= gs_out
== V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
4497 gs_out
= si_conv_prim_to_gs_out(pCreateInfo
->pInputAssemblyState
->topology
);
4499 if (extra
&& extra
->use_rectlist
) {
4500 prim
= V_008958_DI_PT_RECTLIST
;
4501 gs_out
= V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
4502 pipeline
->graphics
.can_use_guardband
= true;
4503 if (radv_pipeline_has_ngg(pipeline
))
4504 gs_out
= V_028A6C_VGT_OUT_RECT_V0
;
4506 pipeline
->graphics
.prim_restart_enable
= !!pCreateInfo
->pInputAssemblyState
->primitiveRestartEnable
;
4507 /* prim vertex count will need TESS changes */
4508 pipeline
->graphics
.prim_vertex_count
= prim_size_table
[prim
];
4510 radv_pipeline_init_dynamic_state(pipeline
, pCreateInfo
);
4512 /* Ensure that some export memory is always allocated, for two reasons:
4514 * 1) Correctness: The hardware ignores the EXEC mask if no export
4515 * memory is allocated, so KILL and alpha test do not work correctly
4517 * 2) Performance: Every shader needs at least a NULL export, even when
4518 * it writes no color/depth output. The NULL export instruction
4519 * stalls without this setting.
4521 * Don't add this to CB_SHADER_MASK.
4523 * GFX10 supports pixel shaders without exports by setting both the
4524 * color and Z formats to SPI_SHADER_ZERO. The hw will skip export
4525 * instructions if any are present.
4527 struct radv_shader_variant
*ps
= pipeline
->shaders
[MESA_SHADER_FRAGMENT
];
4528 if ((pipeline
->device
->physical_device
->rad_info
.chip_class
<= GFX9
||
4529 ps
->info
.fs
.can_discard
) &&
4530 !blend
.spi_shader_col_format
) {
4531 if (!ps
->info
.info
.ps
.writes_z
&&
4532 !ps
->info
.info
.ps
.writes_stencil
&&
4533 !ps
->info
.info
.ps
.writes_sample_mask
)
4534 blend
.spi_shader_col_format
= V_028714_SPI_SHADER_32_R
;
4537 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
4538 if (pipeline
->shaders
[i
]) {
4539 pipeline
->need_indirect_descriptor_sets
|= pipeline
->shaders
[i
]->info
.need_indirect_descriptor_sets
;
4543 struct radv_ngg_state ngg
= {0};
4544 struct radv_gs_state gs
= {0};
4546 if (radv_pipeline_has_ngg(pipeline
)) {
4547 ngg
= calculate_ngg_info(pCreateInfo
, pipeline
);
4548 } else if (radv_pipeline_has_gs(pipeline
)) {
4549 gs
= calculate_gs_info(pCreateInfo
, pipeline
);
4550 calculate_gs_ring_sizes(pipeline
, &gs
);
4553 struct radv_tessellation_state tess
= {0};
4554 if (radv_pipeline_has_tess(pipeline
)) {
4555 if (prim
== V_008958_DI_PT_PATCH
) {
4556 pipeline
->graphics
.prim_vertex_count
.min
= pCreateInfo
->pTessellationState
->patchControlPoints
;
4557 pipeline
->graphics
.prim_vertex_count
.incr
= 1;
4559 tess
= calculate_tess_state(pipeline
, pCreateInfo
);
4562 pipeline
->graphics
.ia_multi_vgt_param
= radv_compute_ia_multi_vgt_param_helpers(pipeline
, &tess
, prim
);
4564 radv_compute_vertex_input_state(pipeline
, pCreateInfo
);
4566 for (uint32_t i
= 0; i
< MESA_SHADER_STAGES
; i
++)
4567 pipeline
->user_data_0
[i
] = radv_pipeline_stage_to_user_data_0(pipeline
, i
, device
->physical_device
->rad_info
.chip_class
);
4569 struct radv_userdata_info
*loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_VERTEX
,
4570 AC_UD_VS_BASE_VERTEX_START_INSTANCE
);
4571 if (loc
->sgpr_idx
!= -1) {
4572 pipeline
->graphics
.vtx_base_sgpr
= pipeline
->user_data_0
[MESA_SHADER_VERTEX
];
4573 pipeline
->graphics
.vtx_base_sgpr
+= loc
->sgpr_idx
* 4;
4574 if (radv_get_shader(pipeline
, MESA_SHADER_VERTEX
)->info
.info
.vs
.needs_draw_id
)
4575 pipeline
->graphics
.vtx_emit_num
= 3;
4577 pipeline
->graphics
.vtx_emit_num
= 2;
4580 /* Find the last vertex shader stage that eventually uses streamout. */
4581 pipeline
->streamout_shader
= radv_pipeline_get_streamout_shader(pipeline
);
4583 result
= radv_pipeline_scratch_init(device
, pipeline
);
4584 radv_pipeline_generate_pm4(pipeline
, pCreateInfo
, extra
, &blend
, &tess
, &gs
, &ngg
, prim
, gs_out
);
4590 radv_graphics_pipeline_create(
4592 VkPipelineCache _cache
,
4593 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
4594 const struct radv_graphics_pipeline_create_info
*extra
,
4595 const VkAllocationCallbacks
*pAllocator
,
4596 VkPipeline
*pPipeline
)
4598 RADV_FROM_HANDLE(radv_device
, device
, _device
);
4599 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
4600 struct radv_pipeline
*pipeline
;
4603 pipeline
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
4604 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
4605 if (pipeline
== NULL
)
4606 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
4608 result
= radv_pipeline_init(pipeline
, device
, cache
,
4609 pCreateInfo
, extra
);
4610 if (result
!= VK_SUCCESS
) {
4611 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
4615 *pPipeline
= radv_pipeline_to_handle(pipeline
);
4620 VkResult
radv_CreateGraphicsPipelines(
4622 VkPipelineCache pipelineCache
,
4624 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
4625 const VkAllocationCallbacks
* pAllocator
,
4626 VkPipeline
* pPipelines
)
4628 VkResult result
= VK_SUCCESS
;
4631 for (; i
< count
; i
++) {
4633 r
= radv_graphics_pipeline_create(_device
,
4636 NULL
, pAllocator
, &pPipelines
[i
]);
4637 if (r
!= VK_SUCCESS
) {
4639 pPipelines
[i
] = VK_NULL_HANDLE
;
4648 radv_compute_generate_pm4(struct radv_pipeline
*pipeline
)
4650 struct radv_shader_variant
*compute_shader
;
4651 struct radv_device
*device
= pipeline
->device
;
4652 unsigned threads_per_threadgroup
;
4653 unsigned threadgroups_per_cu
= 1;
4654 unsigned waves_per_threadgroup
;
4655 unsigned max_waves_per_sh
= 0;
4658 pipeline
->cs
.buf
= malloc(20 * 4);
4659 pipeline
->cs
.max_dw
= 20;
4661 compute_shader
= pipeline
->shaders
[MESA_SHADER_COMPUTE
];
4662 va
= radv_buffer_get_va(compute_shader
->bo
) + compute_shader
->bo_offset
;
4664 radeon_set_sh_reg_seq(&pipeline
->cs
, R_00B830_COMPUTE_PGM_LO
, 2);
4665 radeon_emit(&pipeline
->cs
, va
>> 8);
4666 radeon_emit(&pipeline
->cs
, S_00B834_DATA(va
>> 40));
4668 radeon_set_sh_reg_seq(&pipeline
->cs
, R_00B848_COMPUTE_PGM_RSRC1
, 2);
4669 radeon_emit(&pipeline
->cs
, compute_shader
->config
.rsrc1
);
4670 radeon_emit(&pipeline
->cs
, compute_shader
->config
.rsrc2
);
4672 radeon_set_sh_reg(&pipeline
->cs
, R_00B860_COMPUTE_TMPRING_SIZE
,
4673 S_00B860_WAVES(pipeline
->max_waves
) |
4674 S_00B860_WAVESIZE(pipeline
->scratch_bytes_per_wave
>> 10));
4676 /* Calculate best compute resource limits. */
4677 threads_per_threadgroup
= compute_shader
->info
.cs
.block_size
[0] *
4678 compute_shader
->info
.cs
.block_size
[1] *
4679 compute_shader
->info
.cs
.block_size
[2];
4680 waves_per_threadgroup
= DIV_ROUND_UP(threads_per_threadgroup
,
4681 device
->physical_device
->cs_wave_size
);
4683 if (device
->physical_device
->rad_info
.chip_class
>= GFX10
&&
4684 waves_per_threadgroup
== 1)
4685 threadgroups_per_cu
= 2;
4687 radeon_set_sh_reg(&pipeline
->cs
, R_00B854_COMPUTE_RESOURCE_LIMITS
,
4688 ac_get_compute_resource_limits(&device
->physical_device
->rad_info
,
4689 waves_per_threadgroup
,
4691 threadgroups_per_cu
));
4693 radeon_set_sh_reg_seq(&pipeline
->cs
, R_00B81C_COMPUTE_NUM_THREAD_X
, 3);
4694 radeon_emit(&pipeline
->cs
,
4695 S_00B81C_NUM_THREAD_FULL(compute_shader
->info
.cs
.block_size
[0]));
4696 radeon_emit(&pipeline
->cs
,
4697 S_00B81C_NUM_THREAD_FULL(compute_shader
->info
.cs
.block_size
[1]));
4698 radeon_emit(&pipeline
->cs
,
4699 S_00B81C_NUM_THREAD_FULL(compute_shader
->info
.cs
.block_size
[2]));
4701 assert(pipeline
->cs
.cdw
<= pipeline
->cs
.max_dw
);
4704 static VkResult
radv_compute_pipeline_create(
4706 VkPipelineCache _cache
,
4707 const VkComputePipelineCreateInfo
* pCreateInfo
,
4708 const VkAllocationCallbacks
* pAllocator
,
4709 VkPipeline
* pPipeline
)
4711 RADV_FROM_HANDLE(radv_device
, device
, _device
);
4712 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
4713 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = { 0, };
4714 VkPipelineCreationFeedbackEXT
*stage_feedbacks
[MESA_SHADER_STAGES
] = { 0 };
4715 struct radv_pipeline
*pipeline
;
4718 pipeline
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
4719 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
4720 if (pipeline
== NULL
)
4721 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
4723 pipeline
->device
= device
;
4724 pipeline
->layout
= radv_pipeline_layout_from_handle(pCreateInfo
->layout
);
4725 assert(pipeline
->layout
);
4727 const VkPipelineCreationFeedbackCreateInfoEXT
*creation_feedback
=
4728 vk_find_struct_const(pCreateInfo
->pNext
, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT
);
4729 radv_init_feedback(creation_feedback
);
4731 VkPipelineCreationFeedbackEXT
*pipeline_feedback
= creation_feedback
? creation_feedback
->pPipelineCreationFeedback
: NULL
;
4732 if (creation_feedback
)
4733 stage_feedbacks
[MESA_SHADER_COMPUTE
] = &creation_feedback
->pPipelineStageCreationFeedbacks
[0];
4735 pStages
[MESA_SHADER_COMPUTE
] = &pCreateInfo
->stage
;
4736 radv_create_shaders(pipeline
, device
, cache
, &(struct radv_pipeline_key
) {0}, pStages
, pCreateInfo
->flags
, pipeline_feedback
, stage_feedbacks
);
4738 pipeline
->user_data_0
[MESA_SHADER_COMPUTE
] = radv_pipeline_stage_to_user_data_0(pipeline
, MESA_SHADER_COMPUTE
, device
->physical_device
->rad_info
.chip_class
);
4739 pipeline
->need_indirect_descriptor_sets
|= pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.need_indirect_descriptor_sets
;
4740 result
= radv_pipeline_scratch_init(device
, pipeline
);
4741 if (result
!= VK_SUCCESS
) {
4742 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
4746 radv_compute_generate_pm4(pipeline
);
4748 *pPipeline
= radv_pipeline_to_handle(pipeline
);
4753 VkResult
radv_CreateComputePipelines(
4755 VkPipelineCache pipelineCache
,
4757 const VkComputePipelineCreateInfo
* pCreateInfos
,
4758 const VkAllocationCallbacks
* pAllocator
,
4759 VkPipeline
* pPipelines
)
4761 VkResult result
= VK_SUCCESS
;
4764 for (; i
< count
; i
++) {
4766 r
= radv_compute_pipeline_create(_device
, pipelineCache
,
4768 pAllocator
, &pPipelines
[i
]);
4769 if (r
!= VK_SUCCESS
) {
4771 pPipelines
[i
] = VK_NULL_HANDLE
;