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 "spirv/nir_spirv.h"
39 #include <llvm-c/Core.h>
40 #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"
53 radv_pipeline_destroy(struct radv_device
*device
,
54 struct radv_pipeline
*pipeline
,
55 const VkAllocationCallbacks
* allocator
)
57 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
58 if (pipeline
->shaders
[i
])
59 radv_shader_variant_destroy(device
, pipeline
->shaders
[i
]);
61 if (pipeline
->gs_copy_shader
)
62 radv_shader_variant_destroy(device
, pipeline
->gs_copy_shader
);
65 free(pipeline
->cs
.buf
);
66 vk_free2(&device
->alloc
, allocator
, pipeline
);
69 void radv_DestroyPipeline(
72 const VkAllocationCallbacks
* pAllocator
)
74 RADV_FROM_HANDLE(radv_device
, device
, _device
);
75 RADV_FROM_HANDLE(radv_pipeline
, pipeline
, _pipeline
);
80 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
83 static void radv_dump_pipeline_stats(struct radv_device
*device
, struct radv_pipeline
*pipeline
)
87 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
88 if (!pipeline
->shaders
[i
])
91 radv_shader_dump_stats(device
, pipeline
->shaders
[i
], i
, stderr
);
95 static uint32_t get_hash_flags(struct radv_device
*device
)
97 uint32_t hash_flags
= 0;
99 if (device
->instance
->debug_flags
& RADV_DEBUG_UNSAFE_MATH
)
100 hash_flags
|= RADV_HASH_SHADER_UNSAFE_MATH
;
101 if (device
->instance
->perftest_flags
& RADV_PERFTEST_SISCHED
)
102 hash_flags
|= RADV_HASH_SHADER_SISCHED
;
107 radv_pipeline_scratch_init(struct radv_device
*device
,
108 struct radv_pipeline
*pipeline
)
110 unsigned scratch_bytes_per_wave
= 0;
111 unsigned max_waves
= 0;
112 unsigned min_waves
= 1;
114 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
115 if (pipeline
->shaders
[i
]) {
116 unsigned max_stage_waves
= device
->scratch_waves
;
118 scratch_bytes_per_wave
= MAX2(scratch_bytes_per_wave
,
119 pipeline
->shaders
[i
]->config
.scratch_bytes_per_wave
);
121 max_stage_waves
= MIN2(max_stage_waves
,
122 4 * device
->physical_device
->rad_info
.num_good_compute_units
*
123 (256 / pipeline
->shaders
[i
]->config
.num_vgprs
));
124 max_waves
= MAX2(max_waves
, max_stage_waves
);
128 if (pipeline
->shaders
[MESA_SHADER_COMPUTE
]) {
129 unsigned group_size
= pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[0] *
130 pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[1] *
131 pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[2];
132 min_waves
= MAX2(min_waves
, round_up_u32(group_size
, 64));
135 if (scratch_bytes_per_wave
)
136 max_waves
= MIN2(max_waves
, 0xffffffffu
/ scratch_bytes_per_wave
);
138 if (scratch_bytes_per_wave
&& max_waves
< min_waves
) {
139 /* Not really true at this moment, but will be true on first
140 * execution. Avoid having hanging shaders. */
141 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
143 pipeline
->scratch_bytes_per_wave
= scratch_bytes_per_wave
;
144 pipeline
->max_waves
= max_waves
;
148 static uint32_t si_translate_blend_function(VkBlendOp op
)
151 case VK_BLEND_OP_ADD
:
152 return V_028780_COMB_DST_PLUS_SRC
;
153 case VK_BLEND_OP_SUBTRACT
:
154 return V_028780_COMB_SRC_MINUS_DST
;
155 case VK_BLEND_OP_REVERSE_SUBTRACT
:
156 return V_028780_COMB_DST_MINUS_SRC
;
157 case VK_BLEND_OP_MIN
:
158 return V_028780_COMB_MIN_DST_SRC
;
159 case VK_BLEND_OP_MAX
:
160 return V_028780_COMB_MAX_DST_SRC
;
166 static uint32_t si_translate_blend_factor(VkBlendFactor factor
)
169 case VK_BLEND_FACTOR_ZERO
:
170 return V_028780_BLEND_ZERO
;
171 case VK_BLEND_FACTOR_ONE
:
172 return V_028780_BLEND_ONE
;
173 case VK_BLEND_FACTOR_SRC_COLOR
:
174 return V_028780_BLEND_SRC_COLOR
;
175 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR
:
176 return V_028780_BLEND_ONE_MINUS_SRC_COLOR
;
177 case VK_BLEND_FACTOR_DST_COLOR
:
178 return V_028780_BLEND_DST_COLOR
;
179 case VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR
:
180 return V_028780_BLEND_ONE_MINUS_DST_COLOR
;
181 case VK_BLEND_FACTOR_SRC_ALPHA
:
182 return V_028780_BLEND_SRC_ALPHA
;
183 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
:
184 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA
;
185 case VK_BLEND_FACTOR_DST_ALPHA
:
186 return V_028780_BLEND_DST_ALPHA
;
187 case VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA
:
188 return V_028780_BLEND_ONE_MINUS_DST_ALPHA
;
189 case VK_BLEND_FACTOR_CONSTANT_COLOR
:
190 return V_028780_BLEND_CONSTANT_COLOR
;
191 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
:
192 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR
;
193 case VK_BLEND_FACTOR_CONSTANT_ALPHA
:
194 return V_028780_BLEND_CONSTANT_ALPHA
;
195 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
:
196 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA
;
197 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
:
198 return V_028780_BLEND_SRC_ALPHA_SATURATE
;
199 case VK_BLEND_FACTOR_SRC1_COLOR
:
200 return V_028780_BLEND_SRC1_COLOR
;
201 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR
:
202 return V_028780_BLEND_INV_SRC1_COLOR
;
203 case VK_BLEND_FACTOR_SRC1_ALPHA
:
204 return V_028780_BLEND_SRC1_ALPHA
;
205 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
:
206 return V_028780_BLEND_INV_SRC1_ALPHA
;
212 static uint32_t si_translate_blend_opt_function(VkBlendOp op
)
215 case VK_BLEND_OP_ADD
:
216 return V_028760_OPT_COMB_ADD
;
217 case VK_BLEND_OP_SUBTRACT
:
218 return V_028760_OPT_COMB_SUBTRACT
;
219 case VK_BLEND_OP_REVERSE_SUBTRACT
:
220 return V_028760_OPT_COMB_REVSUBTRACT
;
221 case VK_BLEND_OP_MIN
:
222 return V_028760_OPT_COMB_MIN
;
223 case VK_BLEND_OP_MAX
:
224 return V_028760_OPT_COMB_MAX
;
226 return V_028760_OPT_COMB_BLEND_DISABLED
;
230 static uint32_t si_translate_blend_opt_factor(VkBlendFactor factor
, bool is_alpha
)
233 case VK_BLEND_FACTOR_ZERO
:
234 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL
;
235 case VK_BLEND_FACTOR_ONE
:
236 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
;
237 case VK_BLEND_FACTOR_SRC_COLOR
:
238 return is_alpha
? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
239 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0
;
240 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR
:
241 return is_alpha
? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
242 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1
;
243 case VK_BLEND_FACTOR_SRC_ALPHA
:
244 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
;
245 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
:
246 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
;
247 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
:
248 return is_alpha
? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
249 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0
;
251 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE
;
256 * Get rid of DST in the blend factors by commuting the operands:
257 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
259 static void si_blend_remove_dst(unsigned *func
, unsigned *src_factor
,
260 unsigned *dst_factor
, unsigned expected_dst
,
261 unsigned replacement_src
)
263 if (*src_factor
== expected_dst
&&
264 *dst_factor
== VK_BLEND_FACTOR_ZERO
) {
265 *src_factor
= VK_BLEND_FACTOR_ZERO
;
266 *dst_factor
= replacement_src
;
268 /* Commuting the operands requires reversing subtractions. */
269 if (*func
== VK_BLEND_OP_SUBTRACT
)
270 *func
= VK_BLEND_OP_REVERSE_SUBTRACT
;
271 else if (*func
== VK_BLEND_OP_REVERSE_SUBTRACT
)
272 *func
= VK_BLEND_OP_SUBTRACT
;
276 static bool si_blend_factor_uses_dst(unsigned factor
)
278 return factor
== VK_BLEND_FACTOR_DST_COLOR
||
279 factor
== VK_BLEND_FACTOR_DST_ALPHA
||
280 factor
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
||
281 factor
== VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA
||
282 factor
== VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR
;
285 static bool is_dual_src(VkBlendFactor factor
)
288 case VK_BLEND_FACTOR_SRC1_COLOR
:
289 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR
:
290 case VK_BLEND_FACTOR_SRC1_ALPHA
:
291 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
:
298 static unsigned si_choose_spi_color_format(VkFormat vk_format
,
300 bool blend_need_alpha
)
302 const struct vk_format_description
*desc
= vk_format_description(vk_format
);
303 unsigned format
, ntype
, swap
;
305 /* Alpha is needed for alpha-to-coverage.
306 * Blending may be with or without alpha.
308 unsigned normal
= 0; /* most optimal, may not support blending or export alpha */
309 unsigned alpha
= 0; /* exports alpha, but may not support blending */
310 unsigned blend
= 0; /* supports blending, but may not export alpha */
311 unsigned blend_alpha
= 0; /* least optimal, supports blending and exports alpha */
313 format
= radv_translate_colorformat(vk_format
);
314 ntype
= radv_translate_color_numformat(vk_format
, desc
,
315 vk_format_get_first_non_void_channel(vk_format
));
316 swap
= radv_translate_colorswap(vk_format
, false);
318 /* Choose the SPI color formats. These are required values for Stoney/RB+.
319 * Other chips have multiple choices, though they are not necessarily better.
322 case V_028C70_COLOR_5_6_5
:
323 case V_028C70_COLOR_1_5_5_5
:
324 case V_028C70_COLOR_5_5_5_1
:
325 case V_028C70_COLOR_4_4_4_4
:
326 case V_028C70_COLOR_10_11_11
:
327 case V_028C70_COLOR_11_11_10
:
328 case V_028C70_COLOR_8
:
329 case V_028C70_COLOR_8_8
:
330 case V_028C70_COLOR_8_8_8_8
:
331 case V_028C70_COLOR_10_10_10_2
:
332 case V_028C70_COLOR_2_10_10_10
:
333 if (ntype
== V_028C70_NUMBER_UINT
)
334 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_UINT16_ABGR
;
335 else if (ntype
== V_028C70_NUMBER_SINT
)
336 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_SINT16_ABGR
;
338 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_FP16_ABGR
;
341 case V_028C70_COLOR_16
:
342 case V_028C70_COLOR_16_16
:
343 case V_028C70_COLOR_16_16_16_16
:
344 if (ntype
== V_028C70_NUMBER_UNORM
||
345 ntype
== V_028C70_NUMBER_SNORM
) {
346 /* UNORM16 and SNORM16 don't support blending */
347 if (ntype
== V_028C70_NUMBER_UNORM
)
348 normal
= alpha
= V_028714_SPI_SHADER_UNORM16_ABGR
;
350 normal
= alpha
= V_028714_SPI_SHADER_SNORM16_ABGR
;
352 /* Use 32 bits per channel for blending. */
353 if (format
== V_028C70_COLOR_16
) {
354 if (swap
== V_028C70_SWAP_STD
) { /* R */
355 blend
= V_028714_SPI_SHADER_32_R
;
356 blend_alpha
= V_028714_SPI_SHADER_32_AR
;
357 } else if (swap
== V_028C70_SWAP_ALT_REV
) /* A */
358 blend
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
361 } else if (format
== V_028C70_COLOR_16_16
) {
362 if (swap
== V_028C70_SWAP_STD
) { /* RG */
363 blend
= V_028714_SPI_SHADER_32_GR
;
364 blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
365 } else if (swap
== V_028C70_SWAP_ALT
) /* RA */
366 blend
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
369 } else /* 16_16_16_16 */
370 blend
= blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
371 } else if (ntype
== V_028C70_NUMBER_UINT
)
372 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_UINT16_ABGR
;
373 else if (ntype
== V_028C70_NUMBER_SINT
)
374 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_SINT16_ABGR
;
375 else if (ntype
== V_028C70_NUMBER_FLOAT
)
376 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_FP16_ABGR
;
381 case V_028C70_COLOR_32
:
382 if (swap
== V_028C70_SWAP_STD
) { /* R */
383 blend
= normal
= V_028714_SPI_SHADER_32_R
;
384 alpha
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
385 } else if (swap
== V_028C70_SWAP_ALT_REV
) /* A */
386 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_AR
;
391 case V_028C70_COLOR_32_32
:
392 if (swap
== V_028C70_SWAP_STD
) { /* RG */
393 blend
= normal
= V_028714_SPI_SHADER_32_GR
;
394 alpha
= blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
395 } else if (swap
== V_028C70_SWAP_ALT
) /* RA */
396 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_AR
;
401 case V_028C70_COLOR_32_32_32_32
:
402 case V_028C70_COLOR_8_24
:
403 case V_028C70_COLOR_24_8
:
404 case V_028C70_COLOR_X24_8_32_FLOAT
:
405 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_ABGR
;
409 unreachable("unhandled blend format");
412 if (blend_enable
&& blend_need_alpha
)
414 else if(blend_need_alpha
)
416 else if(blend_enable
)
423 radv_pipeline_compute_spi_color_formats(struct radv_pipeline
*pipeline
,
424 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
425 uint32_t blend_enable
,
426 uint32_t blend_need_alpha
,
427 bool single_cb_enable
,
428 bool blend_mrt0_is_dual_src
)
430 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
431 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
432 struct radv_blend_state
*blend
= &pipeline
->graphics
.blend
;
433 unsigned col_format
= 0;
435 for (unsigned i
= 0; i
< (single_cb_enable
? 1 : subpass
->color_count
); ++i
) {
438 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
) {
439 cf
= V_028714_SPI_SHADER_ZERO
;
441 struct radv_render_pass_attachment
*attachment
= pass
->attachments
+ subpass
->color_attachments
[i
].attachment
;
443 cf
= si_choose_spi_color_format(attachment
->format
,
444 blend_enable
& (1 << i
),
445 blend_need_alpha
& (1 << i
));
448 col_format
|= cf
<< (4 * i
);
451 blend
->cb_shader_mask
= ac_get_cb_shader_mask(col_format
);
453 if (blend_mrt0_is_dual_src
)
454 col_format
|= (col_format
& 0xf) << 4;
455 blend
->spi_shader_col_format
= col_format
;
459 format_is_int8(VkFormat format
)
461 const struct vk_format_description
*desc
= vk_format_description(format
);
462 int channel
= vk_format_get_first_non_void_channel(format
);
464 return channel
>= 0 && desc
->channel
[channel
].pure_integer
&&
465 desc
->channel
[channel
].size
== 8;
469 format_is_int10(VkFormat format
)
471 const struct vk_format_description
*desc
= vk_format_description(format
);
473 if (desc
->nr_channels
!= 4)
475 for (unsigned i
= 0; i
< 4; i
++) {
476 if (desc
->channel
[i
].pure_integer
&& desc
->channel
[i
].size
== 10)
482 unsigned radv_format_meta_fs_key(VkFormat format
)
484 unsigned col_format
= si_choose_spi_color_format(format
, false, false) - 1;
485 bool is_int8
= format_is_int8(format
);
486 bool is_int10
= format_is_int10(format
);
488 return col_format
+ (is_int8
? 3 : is_int10
? 5 : 0);
492 radv_pipeline_compute_get_int_clamp(const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
493 unsigned *is_int8
, unsigned *is_int10
)
495 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
496 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
500 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
501 struct radv_render_pass_attachment
*attachment
;
503 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
)
506 attachment
= pass
->attachments
+ subpass
->color_attachments
[i
].attachment
;
508 if (format_is_int8(attachment
->format
))
510 if (format_is_int10(attachment
->format
))
516 radv_pipeline_init_blend_state(struct radv_pipeline
*pipeline
,
517 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
518 const struct radv_graphics_pipeline_create_info
*extra
)
520 const VkPipelineColorBlendStateCreateInfo
*vkblend
= pCreateInfo
->pColorBlendState
;
521 const VkPipelineMultisampleStateCreateInfo
*vkms
= pCreateInfo
->pMultisampleState
;
522 struct radv_blend_state
*blend
= &pipeline
->graphics
.blend
;
523 unsigned mode
= V_028808_CB_NORMAL
;
524 uint32_t blend_enable
= 0, blend_need_alpha
= 0;
525 bool blend_mrt0_is_dual_src
= false;
527 bool single_cb_enable
= false;
532 if (extra
&& extra
->custom_blend_mode
) {
533 single_cb_enable
= true;
534 mode
= extra
->custom_blend_mode
;
536 blend
->cb_color_control
= 0;
537 if (vkblend
->logicOpEnable
)
538 blend
->cb_color_control
|= S_028808_ROP3(vkblend
->logicOp
| (vkblend
->logicOp
<< 4));
540 blend
->cb_color_control
|= S_028808_ROP3(0xcc);
542 blend
->db_alpha_to_mask
= S_028B70_ALPHA_TO_MASK_OFFSET0(2) |
543 S_028B70_ALPHA_TO_MASK_OFFSET1(2) |
544 S_028B70_ALPHA_TO_MASK_OFFSET2(2) |
545 S_028B70_ALPHA_TO_MASK_OFFSET3(2);
547 if (vkms
&& vkms
->alphaToCoverageEnable
) {
548 blend
->db_alpha_to_mask
|= S_028B70_ALPHA_TO_MASK_ENABLE(1);
551 blend
->cb_target_mask
= 0;
552 for (i
= 0; i
< vkblend
->attachmentCount
; i
++) {
553 const VkPipelineColorBlendAttachmentState
*att
= &vkblend
->pAttachments
[i
];
554 unsigned blend_cntl
= 0;
555 unsigned srcRGB_opt
, dstRGB_opt
, srcA_opt
, dstA_opt
;
556 VkBlendOp eqRGB
= att
->colorBlendOp
;
557 VkBlendFactor srcRGB
= att
->srcColorBlendFactor
;
558 VkBlendFactor dstRGB
= att
->dstColorBlendFactor
;
559 VkBlendOp eqA
= att
->alphaBlendOp
;
560 VkBlendFactor srcA
= att
->srcAlphaBlendFactor
;
561 VkBlendFactor dstA
= att
->dstAlphaBlendFactor
;
563 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
);
565 if (!att
->colorWriteMask
)
568 blend
->cb_target_mask
|= (unsigned)att
->colorWriteMask
<< (4 * i
);
569 if (!att
->blendEnable
) {
570 blend
->cb_blend_control
[i
] = blend_cntl
;
574 if (is_dual_src(srcRGB
) || is_dual_src(dstRGB
) || is_dual_src(srcA
) || is_dual_src(dstA
))
576 blend_mrt0_is_dual_src
= true;
578 if (eqRGB
== VK_BLEND_OP_MIN
|| eqRGB
== VK_BLEND_OP_MAX
) {
579 srcRGB
= VK_BLEND_FACTOR_ONE
;
580 dstRGB
= VK_BLEND_FACTOR_ONE
;
582 if (eqA
== VK_BLEND_OP_MIN
|| eqA
== VK_BLEND_OP_MAX
) {
583 srcA
= VK_BLEND_FACTOR_ONE
;
584 dstA
= VK_BLEND_FACTOR_ONE
;
587 /* Blending optimizations for RB+.
588 * These transformations don't change the behavior.
590 * First, get rid of DST in the blend factors:
591 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
593 si_blend_remove_dst(&eqRGB
, &srcRGB
, &dstRGB
,
594 VK_BLEND_FACTOR_DST_COLOR
,
595 VK_BLEND_FACTOR_SRC_COLOR
);
597 si_blend_remove_dst(&eqA
, &srcA
, &dstA
,
598 VK_BLEND_FACTOR_DST_COLOR
,
599 VK_BLEND_FACTOR_SRC_COLOR
);
601 si_blend_remove_dst(&eqA
, &srcA
, &dstA
,
602 VK_BLEND_FACTOR_DST_ALPHA
,
603 VK_BLEND_FACTOR_SRC_ALPHA
);
605 /* Look up the ideal settings from tables. */
606 srcRGB_opt
= si_translate_blend_opt_factor(srcRGB
, false);
607 dstRGB_opt
= si_translate_blend_opt_factor(dstRGB
, false);
608 srcA_opt
= si_translate_blend_opt_factor(srcA
, true);
609 dstA_opt
= si_translate_blend_opt_factor(dstA
, true);
611 /* Handle interdependencies. */
612 if (si_blend_factor_uses_dst(srcRGB
))
613 dstRGB_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE
;
614 if (si_blend_factor_uses_dst(srcA
))
615 dstA_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE
;
617 if (srcRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
&&
618 (dstRGB
== VK_BLEND_FACTOR_ZERO
||
619 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA
||
620 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
))
621 dstRGB_opt
= V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0
;
623 /* Set the final value. */
624 blend
->sx_mrt_blend_opt
[i
] =
625 S_028760_COLOR_SRC_OPT(srcRGB_opt
) |
626 S_028760_COLOR_DST_OPT(dstRGB_opt
) |
627 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB
)) |
628 S_028760_ALPHA_SRC_OPT(srcA_opt
) |
629 S_028760_ALPHA_DST_OPT(dstA_opt
) |
630 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA
));
631 blend_cntl
|= S_028780_ENABLE(1);
633 blend_cntl
|= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB
));
634 blend_cntl
|= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB
));
635 blend_cntl
|= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB
));
636 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
637 blend_cntl
|= S_028780_SEPARATE_ALPHA_BLEND(1);
638 blend_cntl
|= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA
));
639 blend_cntl
|= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA
));
640 blend_cntl
|= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA
));
642 blend
->cb_blend_control
[i
] = blend_cntl
;
644 blend_enable
|= 1 << i
;
646 if (srcRGB
== VK_BLEND_FACTOR_SRC_ALPHA
||
647 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA
||
648 srcRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
||
649 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
||
650 srcRGB
== VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
||
651 dstRGB
== VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
)
652 blend_need_alpha
|= 1 << i
;
654 for (i
= vkblend
->attachmentCount
; i
< 8; i
++) {
655 blend
->cb_blend_control
[i
] = 0;
656 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
);
659 /* disable RB+ for now */
660 if (pipeline
->device
->physical_device
->has_rbplus
)
661 blend
->cb_color_control
|= S_028808_DISABLE_DUAL_QUAD(1);
663 if (blend
->cb_target_mask
)
664 blend
->cb_color_control
|= S_028808_MODE(mode
);
666 blend
->cb_color_control
|= S_028808_MODE(V_028808_CB_DISABLE
);
668 radv_pipeline_compute_spi_color_formats(pipeline
, pCreateInfo
,
669 blend_enable
, blend_need_alpha
, single_cb_enable
, blend_mrt0_is_dual_src
);
672 static uint32_t si_translate_stencil_op(enum VkStencilOp op
)
675 case VK_STENCIL_OP_KEEP
:
676 return V_02842C_STENCIL_KEEP
;
677 case VK_STENCIL_OP_ZERO
:
678 return V_02842C_STENCIL_ZERO
;
679 case VK_STENCIL_OP_REPLACE
:
680 return V_02842C_STENCIL_REPLACE_TEST
;
681 case VK_STENCIL_OP_INCREMENT_AND_CLAMP
:
682 return V_02842C_STENCIL_ADD_CLAMP
;
683 case VK_STENCIL_OP_DECREMENT_AND_CLAMP
:
684 return V_02842C_STENCIL_SUB_CLAMP
;
685 case VK_STENCIL_OP_INVERT
:
686 return V_02842C_STENCIL_INVERT
;
687 case VK_STENCIL_OP_INCREMENT_AND_WRAP
:
688 return V_02842C_STENCIL_ADD_WRAP
;
689 case VK_STENCIL_OP_DECREMENT_AND_WRAP
:
690 return V_02842C_STENCIL_SUB_WRAP
;
696 radv_pipeline_init_depth_stencil_state(struct radv_pipeline
*pipeline
,
697 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
698 const struct radv_graphics_pipeline_create_info
*extra
)
700 const VkPipelineDepthStencilStateCreateInfo
*vkds
= pCreateInfo
->pDepthStencilState
;
701 struct radv_depth_stencil_state
*ds
= &pipeline
->graphics
.ds
;
706 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
707 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
708 if (subpass
->depth_stencil_attachment
.attachment
== VK_ATTACHMENT_UNUSED
)
711 struct radv_render_pass_attachment
*attachment
= pass
->attachments
+ subpass
->depth_stencil_attachment
.attachment
;
712 bool has_depth_attachment
= vk_format_is_depth(attachment
->format
);
713 bool has_stencil_attachment
= vk_format_is_stencil(attachment
->format
);
715 if (has_depth_attachment
) {
716 ds
->db_depth_control
= S_028800_Z_ENABLE(vkds
->depthTestEnable
? 1 : 0) |
717 S_028800_Z_WRITE_ENABLE(vkds
->depthWriteEnable
? 1 : 0) |
718 S_028800_ZFUNC(vkds
->depthCompareOp
) |
719 S_028800_DEPTH_BOUNDS_ENABLE(vkds
->depthBoundsTestEnable
? 1 : 0);
721 /* from amdvlk: For 4xAA and 8xAA need to decompress on flush for better performance */
722 ds
->db_render_override2
|= S_028010_DECOMPRESS_Z_ON_FLUSH(attachment
->samples
> 2);
725 if (has_stencil_attachment
&& vkds
->stencilTestEnable
) {
726 ds
->db_depth_control
|= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);
727 ds
->db_depth_control
|= S_028800_STENCILFUNC(vkds
->front
.compareOp
);
728 ds
->db_stencil_control
|= S_02842C_STENCILFAIL(si_translate_stencil_op(vkds
->front
.failOp
));
729 ds
->db_stencil_control
|= S_02842C_STENCILZPASS(si_translate_stencil_op(vkds
->front
.passOp
));
730 ds
->db_stencil_control
|= S_02842C_STENCILZFAIL(si_translate_stencil_op(vkds
->front
.depthFailOp
));
732 ds
->db_depth_control
|= S_028800_STENCILFUNC_BF(vkds
->back
.compareOp
);
733 ds
->db_stencil_control
|= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(vkds
->back
.failOp
));
734 ds
->db_stencil_control
|= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(vkds
->back
.passOp
));
735 ds
->db_stencil_control
|= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(vkds
->back
.depthFailOp
));
740 ds
->db_render_control
|= S_028000_DEPTH_CLEAR_ENABLE(extra
->db_depth_clear
);
741 ds
->db_render_control
|= S_028000_STENCIL_CLEAR_ENABLE(extra
->db_stencil_clear
);
743 ds
->db_render_control
|= S_028000_RESUMMARIZE_ENABLE(extra
->db_resummarize
);
744 ds
->db_render_control
|= S_028000_DEPTH_COMPRESS_DISABLE(extra
->db_flush_depth_inplace
);
745 ds
->db_render_control
|= S_028000_STENCIL_COMPRESS_DISABLE(extra
->db_flush_stencil_inplace
);
746 ds
->db_render_override2
|= S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra
->db_depth_disable_expclear
);
747 ds
->db_render_override2
|= S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra
->db_stencil_disable_expclear
);
751 static uint32_t si_translate_fill(VkPolygonMode func
)
754 case VK_POLYGON_MODE_FILL
:
755 return V_028814_X_DRAW_TRIANGLES
;
756 case VK_POLYGON_MODE_LINE
:
757 return V_028814_X_DRAW_LINES
;
758 case VK_POLYGON_MODE_POINT
:
759 return V_028814_X_DRAW_POINTS
;
762 return V_028814_X_DRAW_POINTS
;
766 radv_pipeline_init_raster_state(struct radv_pipeline
*pipeline
,
767 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
769 const VkPipelineRasterizationStateCreateInfo
*vkraster
= pCreateInfo
->pRasterizationState
;
770 struct radv_raster_state
*raster
= &pipeline
->graphics
.raster
;
772 raster
->spi_interp_control
=
773 S_0286D4_FLAT_SHADE_ENA(1) |
774 S_0286D4_PNT_SPRITE_ENA(1) |
775 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S
) |
776 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T
) |
777 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0
) |
778 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1
) |
779 S_0286D4_PNT_SPRITE_TOP_1(0); // vulkan is top to bottom - 1.0 at bottom
782 raster
->pa_cl_clip_cntl
= S_028810_PS_UCP_MODE(3) |
783 S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.
784 S_028810_ZCLIP_NEAR_DISABLE(vkraster
->depthClampEnable
? 1 : 0) |
785 S_028810_ZCLIP_FAR_DISABLE(vkraster
->depthClampEnable
? 1 : 0) |
786 S_028810_DX_RASTERIZATION_KILL(vkraster
->rasterizerDiscardEnable
? 1 : 0) |
787 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
789 raster
->pa_su_vtx_cntl
=
790 S_028BE4_PIX_CENTER(1) | // TODO verify
791 S_028BE4_ROUND_MODE(V_028BE4_X_ROUND_TO_EVEN
) |
792 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH
);
794 raster
->pa_su_sc_mode_cntl
=
795 S_028814_FACE(vkraster
->frontFace
) |
796 S_028814_CULL_FRONT(!!(vkraster
->cullMode
& VK_CULL_MODE_FRONT_BIT
)) |
797 S_028814_CULL_BACK(!!(vkraster
->cullMode
& VK_CULL_MODE_BACK_BIT
)) |
798 S_028814_POLY_MODE(vkraster
->polygonMode
!= VK_POLYGON_MODE_FILL
) |
799 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(vkraster
->polygonMode
)) |
800 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(vkraster
->polygonMode
)) |
801 S_028814_POLY_OFFSET_FRONT_ENABLE(vkraster
->depthBiasEnable
? 1 : 0) |
802 S_028814_POLY_OFFSET_BACK_ENABLE(vkraster
->depthBiasEnable
? 1 : 0) |
803 S_028814_POLY_OFFSET_PARA_ENABLE(vkraster
->depthBiasEnable
? 1 : 0);
807 static uint8_t radv_pipeline_get_ps_iter_samples(const VkPipelineMultisampleStateCreateInfo
*vkms
)
809 uint32_t num_samples
= vkms
->rasterizationSamples
;
810 uint32_t ps_iter_samples
= 1;
812 if (vkms
->sampleShadingEnable
) {
813 ps_iter_samples
= ceil(vkms
->minSampleShading
* num_samples
);
814 ps_iter_samples
= util_next_power_of_two(ps_iter_samples
);
816 return ps_iter_samples
;
820 radv_pipeline_init_multisample_state(struct radv_pipeline
*pipeline
,
821 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
823 const VkPipelineMultisampleStateCreateInfo
*vkms
= pCreateInfo
->pMultisampleState
;
824 struct radv_multisample_state
*ms
= &pipeline
->graphics
.ms
;
825 unsigned num_tile_pipes
= pipeline
->device
->physical_device
->rad_info
.num_tile_pipes
;
826 int ps_iter_samples
= 1;
827 uint32_t mask
= 0xffff;
830 ms
->num_samples
= vkms
->rasterizationSamples
;
835 ps_iter_samples
= radv_pipeline_get_ps_iter_samples(vkms
);
836 if (vkms
&& !vkms
->sampleShadingEnable
&& pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.force_persample
) {
837 ps_iter_samples
= ms
->num_samples
;
840 ms
->pa_sc_line_cntl
= S_028BDC_DX10_DIAMOND_TEST_ENA(1);
841 ms
->pa_sc_aa_config
= 0;
842 ms
->db_eqaa
= S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
843 S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
844 ms
->pa_sc_mode_cntl_1
=
845 S_028A4C_WALK_FENCE_ENABLE(1) | //TODO linear dst fixes
846 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes
== 2 ? 2 : 3) |
848 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
849 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
850 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
851 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
852 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
853 S_028A4C_FORCE_EOV_REZ_ENABLE(1);
854 ms
->pa_sc_mode_cntl_0
= S_028A48_ALTERNATE_RBS_PER_TILE(pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
) |
855 S_028A48_VPORT_SCISSOR_ENABLE(1);
857 if (ms
->num_samples
> 1) {
858 unsigned log_samples
= util_logbase2(ms
->num_samples
);
859 unsigned log_ps_iter_samples
= util_logbase2(ps_iter_samples
);
860 ms
->pa_sc_mode_cntl_0
|= S_028A48_MSAA_ENABLE(1);
861 ms
->pa_sc_line_cntl
|= S_028BDC_EXPAND_LINE_WIDTH(1); /* CM_R_028BDC_PA_SC_LINE_CNTL */
862 ms
->db_eqaa
|= S_028804_MAX_ANCHOR_SAMPLES(log_samples
) |
863 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples
) |
864 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples
) |
865 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples
);
866 ms
->pa_sc_aa_config
|= S_028BE0_MSAA_NUM_SAMPLES(log_samples
) |
867 S_028BE0_MAX_SAMPLE_DIST(radv_cayman_get_maxdist(log_samples
)) |
868 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples
); /* CM_R_028BE0_PA_SC_AA_CONFIG */
869 ms
->pa_sc_mode_cntl_1
|= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples
> 1);
870 if (ps_iter_samples
> 1)
871 pipeline
->graphics
.spi_baryc_cntl
|= S_0286E0_POS_FLOAT_LOCATION(2);
874 const struct VkPipelineRasterizationStateRasterizationOrderAMD
*raster_order
=
875 vk_find_struct_const(pCreateInfo
->pRasterizationState
->pNext
, PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD
);
876 if (raster_order
&& raster_order
->rasterizationOrder
== VK_RASTERIZATION_ORDER_RELAXED_AMD
) {
877 ms
->pa_sc_mode_cntl_1
|= S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(1) |
878 S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7);
881 if (vkms
&& vkms
->pSampleMask
) {
882 mask
= vkms
->pSampleMask
[0] & 0xffff;
885 ms
->pa_sc_aa_mask
[0] = mask
| (mask
<< 16);
886 ms
->pa_sc_aa_mask
[1] = mask
| (mask
<< 16);
890 radv_prim_can_use_guardband(enum VkPrimitiveTopology topology
)
893 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST
:
894 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST
:
895 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
:
896 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
897 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
899 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
:
900 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
:
901 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
:
902 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
903 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
904 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
:
907 unreachable("unhandled primitive type");
912 si_translate_prim(enum VkPrimitiveTopology topology
)
915 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST
:
916 return V_008958_DI_PT_POINTLIST
;
917 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST
:
918 return V_008958_DI_PT_LINELIST
;
919 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
:
920 return V_008958_DI_PT_LINESTRIP
;
921 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
:
922 return V_008958_DI_PT_TRILIST
;
923 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
:
924 return V_008958_DI_PT_TRISTRIP
;
925 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
:
926 return V_008958_DI_PT_TRIFAN
;
927 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
928 return V_008958_DI_PT_LINELIST_ADJ
;
929 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
930 return V_008958_DI_PT_LINESTRIP_ADJ
;
931 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
932 return V_008958_DI_PT_TRILIST_ADJ
;
933 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
934 return V_008958_DI_PT_TRISTRIP_ADJ
;
935 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
:
936 return V_008958_DI_PT_PATCH
;
944 si_conv_gl_prim_to_gs_out(unsigned gl_prim
)
947 case 0: /* GL_POINTS */
948 return V_028A6C_OUTPRIM_TYPE_POINTLIST
;
949 case 1: /* GL_LINES */
950 case 3: /* GL_LINE_STRIP */
951 case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */
952 case 0x8E7A: /* GL_ISOLINES */
953 return V_028A6C_OUTPRIM_TYPE_LINESTRIP
;
955 case 4: /* GL_TRIANGLES */
956 case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */
957 case 5: /* GL_TRIANGLE_STRIP */
958 case 7: /* GL_QUADS */
959 return V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
967 si_conv_prim_to_gs_out(enum VkPrimitiveTopology topology
)
970 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST
:
971 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
:
972 return V_028A6C_OUTPRIM_TYPE_POINTLIST
;
973 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST
:
974 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
:
975 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
976 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
977 return V_028A6C_OUTPRIM_TYPE_LINESTRIP
;
978 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
:
979 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
:
980 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
:
981 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
982 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
983 return V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
990 static unsigned si_map_swizzle(unsigned swizzle
)
994 return V_008F0C_SQ_SEL_Y
;
996 return V_008F0C_SQ_SEL_Z
;
998 return V_008F0C_SQ_SEL_W
;
1000 return V_008F0C_SQ_SEL_0
;
1002 return V_008F0C_SQ_SEL_1
;
1003 default: /* VK_SWIZZLE_X */
1004 return V_008F0C_SQ_SEL_X
;
1009 static unsigned radv_dynamic_state_mask(VkDynamicState state
)
1012 case VK_DYNAMIC_STATE_VIEWPORT
:
1013 return RADV_DYNAMIC_VIEWPORT
;
1014 case VK_DYNAMIC_STATE_SCISSOR
:
1015 return RADV_DYNAMIC_SCISSOR
;
1016 case VK_DYNAMIC_STATE_LINE_WIDTH
:
1017 return RADV_DYNAMIC_LINE_WIDTH
;
1018 case VK_DYNAMIC_STATE_DEPTH_BIAS
:
1019 return RADV_DYNAMIC_DEPTH_BIAS
;
1020 case VK_DYNAMIC_STATE_BLEND_CONSTANTS
:
1021 return RADV_DYNAMIC_BLEND_CONSTANTS
;
1022 case VK_DYNAMIC_STATE_DEPTH_BOUNDS
:
1023 return RADV_DYNAMIC_DEPTH_BOUNDS
;
1024 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
:
1025 return RADV_DYNAMIC_STENCIL_COMPARE_MASK
;
1026 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
:
1027 return RADV_DYNAMIC_STENCIL_WRITE_MASK
;
1028 case VK_DYNAMIC_STATE_STENCIL_REFERENCE
:
1029 return RADV_DYNAMIC_STENCIL_REFERENCE
;
1030 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT
:
1031 return RADV_DYNAMIC_DISCARD_RECTANGLE
;
1033 unreachable("Unhandled dynamic state");
1037 static uint32_t radv_pipeline_needed_dynamic_state(const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1039 uint32_t states
= RADV_DYNAMIC_ALL
;
1041 /* If rasterization is disabled we do not care about any of the dynamic states,
1042 * since they are all rasterization related only. */
1043 if (pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
)
1046 if (!pCreateInfo
->pRasterizationState
->depthBiasEnable
)
1047 states
&= ~RADV_DYNAMIC_DEPTH_BIAS
;
1049 if (!pCreateInfo
->pDepthStencilState
||
1050 !pCreateInfo
->pDepthStencilState
->depthBoundsTestEnable
)
1051 states
&= ~RADV_DYNAMIC_DEPTH_BOUNDS
;
1053 if (!pCreateInfo
->pDepthStencilState
||
1054 !pCreateInfo
->pDepthStencilState
->stencilTestEnable
)
1055 states
&= ~(RADV_DYNAMIC_STENCIL_COMPARE_MASK
|
1056 RADV_DYNAMIC_STENCIL_WRITE_MASK
|
1057 RADV_DYNAMIC_STENCIL_REFERENCE
);
1059 if (!vk_find_struct_const(pCreateInfo
->pNext
, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT
))
1060 states
&= ~RADV_DYNAMIC_DISCARD_RECTANGLE
;
1062 /* TODO: blend constants & line width. */
1069 radv_pipeline_init_dynamic_state(struct radv_pipeline
*pipeline
,
1070 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1072 uint32_t needed_states
= radv_pipeline_needed_dynamic_state(pCreateInfo
);
1073 uint32_t states
= needed_states
;
1074 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
1075 struct radv_subpass
*subpass
= &pass
->subpasses
[pCreateInfo
->subpass
];
1077 pipeline
->dynamic_state
= default_dynamic_state
;
1078 pipeline
->graphics
.needed_dynamic_state
= needed_states
;
1080 if (pCreateInfo
->pDynamicState
) {
1081 /* Remove all of the states that are marked as dynamic */
1082 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
1083 for (uint32_t s
= 0; s
< count
; s
++)
1084 states
&= ~radv_dynamic_state_mask(pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
1087 struct radv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
1089 if (needed_states
& RADV_DYNAMIC_VIEWPORT
) {
1090 assert(pCreateInfo
->pViewportState
);
1092 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
1093 if (states
& RADV_DYNAMIC_VIEWPORT
) {
1094 typed_memcpy(dynamic
->viewport
.viewports
,
1095 pCreateInfo
->pViewportState
->pViewports
,
1096 pCreateInfo
->pViewportState
->viewportCount
);
1100 if (needed_states
& RADV_DYNAMIC_SCISSOR
) {
1101 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
1102 if (states
& RADV_DYNAMIC_SCISSOR
) {
1103 typed_memcpy(dynamic
->scissor
.scissors
,
1104 pCreateInfo
->pViewportState
->pScissors
,
1105 pCreateInfo
->pViewportState
->scissorCount
);
1109 if (states
& RADV_DYNAMIC_LINE_WIDTH
) {
1110 assert(pCreateInfo
->pRasterizationState
);
1111 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
1114 if (states
& RADV_DYNAMIC_DEPTH_BIAS
) {
1115 assert(pCreateInfo
->pRasterizationState
);
1116 dynamic
->depth_bias
.bias
=
1117 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
1118 dynamic
->depth_bias
.clamp
=
1119 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
1120 dynamic
->depth_bias
.slope
=
1121 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
1124 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1126 * pColorBlendState is [...] NULL if the pipeline has rasterization
1127 * disabled or if the subpass of the render pass the pipeline is
1128 * created against does not use any color attachments.
1130 bool uses_color_att
= false;
1131 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
1132 if (subpass
->color_attachments
[i
].attachment
!= VK_ATTACHMENT_UNUSED
) {
1133 uses_color_att
= true;
1138 if (uses_color_att
&& states
& RADV_DYNAMIC_BLEND_CONSTANTS
) {
1139 assert(pCreateInfo
->pColorBlendState
);
1140 typed_memcpy(dynamic
->blend_constants
,
1141 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
1144 /* If there is no depthstencil attachment, then don't read
1145 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1146 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1147 * no need to override the depthstencil defaults in
1148 * radv_pipeline::dynamic_state when there is no depthstencil attachment.
1150 * Section 9.2 of the Vulkan 1.0.15 spec says:
1152 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1153 * disabled or if the subpass of the render pass the pipeline is created
1154 * against does not use a depth/stencil attachment.
1156 if (needed_states
&&
1157 subpass
->depth_stencil_attachment
.attachment
!= VK_ATTACHMENT_UNUSED
) {
1158 assert(pCreateInfo
->pDepthStencilState
);
1160 if (states
& RADV_DYNAMIC_DEPTH_BOUNDS
) {
1161 dynamic
->depth_bounds
.min
=
1162 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
1163 dynamic
->depth_bounds
.max
=
1164 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
1167 if (states
& RADV_DYNAMIC_STENCIL_COMPARE_MASK
) {
1168 dynamic
->stencil_compare_mask
.front
=
1169 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
1170 dynamic
->stencil_compare_mask
.back
=
1171 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
1174 if (states
& RADV_DYNAMIC_STENCIL_WRITE_MASK
) {
1175 dynamic
->stencil_write_mask
.front
=
1176 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
1177 dynamic
->stencil_write_mask
.back
=
1178 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
1181 if (states
& RADV_DYNAMIC_STENCIL_REFERENCE
) {
1182 dynamic
->stencil_reference
.front
=
1183 pCreateInfo
->pDepthStencilState
->front
.reference
;
1184 dynamic
->stencil_reference
.back
=
1185 pCreateInfo
->pDepthStencilState
->back
.reference
;
1189 const VkPipelineDiscardRectangleStateCreateInfoEXT
*discard_rectangle_info
=
1190 vk_find_struct_const(pCreateInfo
->pNext
, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT
);
1191 if (states
& RADV_DYNAMIC_DISCARD_RECTANGLE
) {
1192 dynamic
->discard_rectangle
.count
= discard_rectangle_info
->discardRectangleCount
;
1193 typed_memcpy(dynamic
->discard_rectangle
.rectangles
,
1194 discard_rectangle_info
->pDiscardRectangles
,
1195 discard_rectangle_info
->discardRectangleCount
);
1199 for (unsigned i
= 0; i
< (1u << MAX_DISCARD_RECTANGLES
); ++i
) {
1200 /* Interpret i as a bitmask, and then set the bit in the mask if
1201 * that combination of rectangles in which the pixel is contained
1202 * should pass the cliprect test. */
1203 unsigned relevant_subset
= i
& ((1u << discard_rectangle_info
->discardRectangleCount
) - 1);
1205 if (discard_rectangle_info
->discardRectangleMode
== VK_DISCARD_RECTANGLE_MODE_INCLUSIVE_EXT
&&
1209 if (discard_rectangle_info
->discardRectangleMode
== VK_DISCARD_RECTANGLE_MODE_EXCLUSIVE_EXT
&&
1215 pipeline
->graphics
.pa_sc_cliprect_rule
= mask
;
1217 /* Allow from all rectangle combinations */
1218 pipeline
->graphics
.pa_sc_cliprect_rule
= 0xffff;
1220 pipeline
->dynamic_state
.mask
= states
;
1223 static void calculate_gfx9_gs_info(const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1224 struct radv_pipeline
*pipeline
)
1226 struct ac_shader_variant_info
*gs_info
= &pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
;
1227 struct ac_es_output_info
*es_info
= radv_pipeline_has_tess(pipeline
) ?
1228 &gs_info
->tes
.es_info
: &gs_info
->vs
.es_info
;
1229 unsigned gs_num_invocations
= MAX2(gs_info
->gs
.invocations
, 1);
1230 bool uses_adjacency
;
1231 switch(pCreateInfo
->pInputAssemblyState
->topology
) {
1232 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
1233 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
1234 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
1235 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
1236 uses_adjacency
= true;
1239 uses_adjacency
= false;
1243 /* All these are in dwords: */
1244 /* We can't allow using the whole LDS, because GS waves compete with
1245 * other shader stages for LDS space. */
1246 const unsigned max_lds_size
= 8 * 1024;
1247 const unsigned esgs_itemsize
= es_info
->esgs_itemsize
/ 4;
1248 unsigned esgs_lds_size
;
1250 /* All these are per subgroup: */
1251 const unsigned max_out_prims
= 32 * 1024;
1252 const unsigned max_es_verts
= 255;
1253 const unsigned ideal_gs_prims
= 64;
1254 unsigned max_gs_prims
, gs_prims
;
1255 unsigned min_es_verts
, es_verts
, worst_case_es_verts
;
1257 if (uses_adjacency
|| gs_num_invocations
> 1)
1258 max_gs_prims
= 127 / gs_num_invocations
;
1262 /* MAX_PRIMS_PER_SUBGROUP = gs_prims * max_vert_out * gs_invocations.
1263 * Make sure we don't go over the maximum value.
1265 if (gs_info
->gs
.vertices_out
> 0) {
1266 max_gs_prims
= MIN2(max_gs_prims
,
1268 (gs_info
->gs
.vertices_out
* gs_num_invocations
));
1270 assert(max_gs_prims
> 0);
1272 /* If the primitive has adjacency, halve the number of vertices
1273 * that will be reused in multiple primitives.
1275 min_es_verts
= gs_info
->gs
.vertices_in
/ (uses_adjacency
? 2 : 1);
1277 gs_prims
= MIN2(ideal_gs_prims
, max_gs_prims
);
1278 worst_case_es_verts
= MIN2(min_es_verts
* gs_prims
, max_es_verts
);
1280 /* Compute ESGS LDS size based on the worst case number of ES vertices
1281 * needed to create the target number of GS prims per subgroup.
1283 esgs_lds_size
= esgs_itemsize
* worst_case_es_verts
;
1285 /* If total LDS usage is too big, refactor partitions based on ratio
1286 * of ESGS item sizes.
1288 if (esgs_lds_size
> max_lds_size
) {
1289 /* Our target GS Prims Per Subgroup was too large. Calculate
1290 * the maximum number of GS Prims Per Subgroup that will fit
1291 * into LDS, capped by the maximum that the hardware can support.
1293 gs_prims
= MIN2((max_lds_size
/ (esgs_itemsize
* min_es_verts
)),
1295 assert(gs_prims
> 0);
1296 worst_case_es_verts
= MIN2(min_es_verts
* gs_prims
,
1299 esgs_lds_size
= esgs_itemsize
* worst_case_es_verts
;
1300 assert(esgs_lds_size
<= max_lds_size
);
1303 /* Now calculate remaining ESGS information. */
1305 es_verts
= MIN2(esgs_lds_size
/ esgs_itemsize
, max_es_verts
);
1307 es_verts
= max_es_verts
;
1309 /* Vertices for adjacency primitives are not always reused, so restore
1310 * it for ES_VERTS_PER_SUBGRP.
1312 min_es_verts
= gs_info
->gs
.vertices_in
;
1314 /* For normal primitives, the VGT only checks if they are past the ES
1315 * verts per subgroup after allocating a full GS primitive and if they
1316 * are, kick off a new subgroup. But if those additional ES verts are
1317 * unique (e.g. not reused) we need to make sure there is enough LDS
1318 * space to account for those ES verts beyond ES_VERTS_PER_SUBGRP.
1320 es_verts
-= min_es_verts
- 1;
1322 uint32_t es_verts_per_subgroup
= es_verts
;
1323 uint32_t gs_prims_per_subgroup
= gs_prims
;
1324 uint32_t gs_inst_prims_in_subgroup
= gs_prims
* gs_num_invocations
;
1325 uint32_t max_prims_per_subgroup
= gs_inst_prims_in_subgroup
* gs_info
->gs
.vertices_out
;
1326 pipeline
->graphics
.gs
.lds_size
= align(esgs_lds_size
, 128) / 128;
1327 pipeline
->graphics
.gs
.vgt_gs_onchip_cntl
=
1328 S_028A44_ES_VERTS_PER_SUBGRP(es_verts_per_subgroup
) |
1329 S_028A44_GS_PRIMS_PER_SUBGRP(gs_prims_per_subgroup
) |
1330 S_028A44_GS_INST_PRIMS_IN_SUBGRP(gs_inst_prims_in_subgroup
);
1331 pipeline
->graphics
.gs
.vgt_gs_max_prims_per_subgroup
=
1332 S_028A94_MAX_PRIMS_PER_SUBGROUP(max_prims_per_subgroup
);
1333 pipeline
->graphics
.gs
.vgt_esgs_ring_itemsize
= esgs_itemsize
;
1334 assert(max_prims_per_subgroup
<= max_out_prims
);
1338 calculate_gs_ring_sizes(struct radv_pipeline
*pipeline
)
1340 struct radv_device
*device
= pipeline
->device
;
1341 unsigned num_se
= device
->physical_device
->rad_info
.max_se
;
1342 unsigned wave_size
= 64;
1343 unsigned max_gs_waves
= 32 * num_se
; /* max 32 per SE on GCN */
1344 unsigned gs_vertex_reuse
= 16 * num_se
; /* GS_VERTEX_REUSE register (per SE) */
1345 unsigned alignment
= 256 * num_se
;
1346 /* The maximum size is 63.999 MB per SE. */
1347 unsigned max_size
= ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se
;
1348 struct ac_shader_variant_info
*gs_info
= &pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
;
1349 struct ac_es_output_info
*es_info
;
1350 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
)
1351 es_info
= radv_pipeline_has_tess(pipeline
) ? &gs_info
->tes
.es_info
: &gs_info
->vs
.es_info
;
1353 es_info
= radv_pipeline_has_tess(pipeline
) ?
1354 &pipeline
->shaders
[MESA_SHADER_TESS_EVAL
]->info
.tes
.es_info
:
1355 &pipeline
->shaders
[MESA_SHADER_VERTEX
]->info
.vs
.es_info
;
1357 /* Calculate the minimum size. */
1358 unsigned min_esgs_ring_size
= align(es_info
->esgs_itemsize
* gs_vertex_reuse
*
1359 wave_size
, alignment
);
1360 /* These are recommended sizes, not minimum sizes. */
1361 unsigned esgs_ring_size
= max_gs_waves
* 2 * wave_size
*
1362 es_info
->esgs_itemsize
* gs_info
->gs
.vertices_in
;
1363 unsigned gsvs_ring_size
= max_gs_waves
* 2 * wave_size
*
1364 gs_info
->gs
.max_gsvs_emit_size
* 1; // no streams in VK (gs->max_gs_stream + 1);
1366 min_esgs_ring_size
= align(min_esgs_ring_size
, alignment
);
1367 esgs_ring_size
= align(esgs_ring_size
, alignment
);
1368 gsvs_ring_size
= align(gsvs_ring_size
, alignment
);
1370 if (pipeline
->device
->physical_device
->rad_info
.chip_class
<= VI
)
1371 pipeline
->graphics
.esgs_ring_size
= CLAMP(esgs_ring_size
, min_esgs_ring_size
, max_size
);
1373 pipeline
->graphics
.gs
.vgt_esgs_ring_itemsize
= es_info
->esgs_itemsize
/ 4;
1374 pipeline
->graphics
.gsvs_ring_size
= MIN2(gsvs_ring_size
, max_size
);
1377 static void si_multiwave_lds_size_workaround(struct radv_device
*device
,
1380 /* SPI barrier management bug:
1381 * Make sure we have at least 4k of LDS in use to avoid the bug.
1382 * It applies to workgroup sizes of more than one wavefront.
1384 if (device
->physical_device
->rad_info
.family
== CHIP_BONAIRE
||
1385 device
->physical_device
->rad_info
.family
== CHIP_KABINI
||
1386 device
->physical_device
->rad_info
.family
== CHIP_MULLINS
)
1387 *lds_size
= MAX2(*lds_size
, 8);
1390 struct radv_shader_variant
*
1391 radv_get_vertex_shader(struct radv_pipeline
*pipeline
)
1393 if (pipeline
->shaders
[MESA_SHADER_VERTEX
])
1394 return pipeline
->shaders
[MESA_SHADER_VERTEX
];
1395 if (pipeline
->shaders
[MESA_SHADER_TESS_CTRL
])
1396 return pipeline
->shaders
[MESA_SHADER_TESS_CTRL
];
1397 return pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
1400 static struct radv_shader_variant
*
1401 radv_get_tess_eval_shader(struct radv_pipeline
*pipeline
)
1403 if (pipeline
->shaders
[MESA_SHADER_TESS_EVAL
])
1404 return pipeline
->shaders
[MESA_SHADER_TESS_EVAL
];
1405 return pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
1409 calculate_tess_state(struct radv_pipeline
*pipeline
,
1410 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1412 unsigned num_tcs_input_cp
= pCreateInfo
->pTessellationState
->patchControlPoints
;
1413 unsigned num_tcs_output_cp
, num_tcs_inputs
, num_tcs_outputs
;
1414 unsigned num_tcs_patch_outputs
;
1415 unsigned input_vertex_size
, output_vertex_size
, pervertex_output_patch_size
;
1416 unsigned input_patch_size
, output_patch_size
, output_patch0_offset
;
1417 unsigned lds_size
, hardware_lds_size
;
1418 unsigned perpatch_output_offset
;
1419 unsigned num_patches
;
1420 struct radv_tessellation_state
*tess
= &pipeline
->graphics
.tess
;
1422 /* This calculates how shader inputs and outputs among VS, TCS, and TES
1423 * are laid out in LDS. */
1424 num_tcs_inputs
= util_last_bit64(radv_get_vertex_shader(pipeline
)->info
.vs
.outputs_written
);
1426 num_tcs_outputs
= util_last_bit64(pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.tcs
.outputs_written
); //tcs->outputs_written
1427 num_tcs_output_cp
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.tcs
.tcs_vertices_out
; //TCS VERTICES OUT
1428 num_tcs_patch_outputs
= util_last_bit64(pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.tcs
.patch_outputs_written
);
1430 /* Ensure that we only need one wave per SIMD so we don't need to check
1431 * resource usage. Also ensures that the number of tcs in and out
1432 * vertices per threadgroup are at most 256.
1434 input_vertex_size
= num_tcs_inputs
* 16;
1435 output_vertex_size
= num_tcs_outputs
* 16;
1437 input_patch_size
= num_tcs_input_cp
* input_vertex_size
;
1439 pervertex_output_patch_size
= num_tcs_output_cp
* output_vertex_size
;
1440 output_patch_size
= pervertex_output_patch_size
+ num_tcs_patch_outputs
* 16;
1441 /* Ensure that we only need one wave per SIMD so we don't need to check
1442 * resource usage. Also ensures that the number of tcs in and out
1443 * vertices per threadgroup are at most 256.
1445 num_patches
= 64 / MAX2(num_tcs_input_cp
, num_tcs_output_cp
) * 4;
1447 /* Make sure that the data fits in LDS. This assumes the shaders only
1448 * use LDS for the inputs and outputs.
1450 hardware_lds_size
= pipeline
->device
->physical_device
->rad_info
.chip_class
>= CIK
? 65536 : 32768;
1451 num_patches
= MIN2(num_patches
, hardware_lds_size
/ (input_patch_size
+ output_patch_size
));
1453 /* Make sure the output data fits in the offchip buffer */
1454 num_patches
= MIN2(num_patches
,
1455 (pipeline
->device
->tess_offchip_block_dw_size
* 4) /
1458 /* Not necessary for correctness, but improves performance. The
1459 * specific value is taken from the proprietary driver.
1461 num_patches
= MIN2(num_patches
, 40);
1463 /* SI bug workaround - limit LS-HS threadgroups to only one wave. */
1464 if (pipeline
->device
->physical_device
->rad_info
.chip_class
== SI
) {
1465 unsigned one_wave
= 64 / MAX2(num_tcs_input_cp
, num_tcs_output_cp
);
1466 num_patches
= MIN2(num_patches
, one_wave
);
1469 output_patch0_offset
= input_patch_size
* num_patches
;
1470 perpatch_output_offset
= output_patch0_offset
+ pervertex_output_patch_size
;
1472 lds_size
= output_patch0_offset
+ output_patch_size
* num_patches
;
1474 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= CIK
) {
1475 assert(lds_size
<= 65536);
1476 lds_size
= align(lds_size
, 512) / 512;
1478 assert(lds_size
<= 32768);
1479 lds_size
= align(lds_size
, 256) / 256;
1481 si_multiwave_lds_size_workaround(pipeline
->device
, &lds_size
);
1483 tess
->lds_size
= lds_size
;
1485 tess
->tcs_in_layout
= (input_patch_size
/ 4) |
1486 ((input_vertex_size
/ 4) << 13);
1487 tess
->tcs_out_layout
= (output_patch_size
/ 4) |
1488 ((output_vertex_size
/ 4) << 13);
1489 tess
->tcs_out_offsets
= (output_patch0_offset
/ 16) |
1490 ((perpatch_output_offset
/ 16) << 16);
1491 tess
->offchip_layout
= (pervertex_output_patch_size
* num_patches
<< 16) |
1492 (num_tcs_output_cp
<< 9) | num_patches
;
1494 tess
->ls_hs_config
= S_028B58_NUM_PATCHES(num_patches
) |
1495 S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp
) |
1496 S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp
);
1497 tess
->num_patches
= num_patches
;
1498 tess
->num_tcs_input_cp
= num_tcs_input_cp
;
1500 struct radv_shader_variant
*tes
= radv_get_tess_eval_shader(pipeline
);
1501 unsigned type
= 0, partitioning
= 0, topology
= 0, distribution_mode
= 0;
1503 switch (tes
->info
.tes
.primitive_mode
) {
1505 type
= V_028B6C_TESS_TRIANGLE
;
1508 type
= V_028B6C_TESS_QUAD
;
1511 type
= V_028B6C_TESS_ISOLINE
;
1515 switch (tes
->info
.tes
.spacing
) {
1516 case TESS_SPACING_EQUAL
:
1517 partitioning
= V_028B6C_PART_INTEGER
;
1519 case TESS_SPACING_FRACTIONAL_ODD
:
1520 partitioning
= V_028B6C_PART_FRAC_ODD
;
1522 case TESS_SPACING_FRACTIONAL_EVEN
:
1523 partitioning
= V_028B6C_PART_FRAC_EVEN
;
1529 bool ccw
= tes
->info
.tes
.ccw
;
1530 const VkPipelineTessellationDomainOriginStateCreateInfoKHR
*domain_origin_state
=
1531 vk_find_struct_const(pCreateInfo
->pTessellationState
,
1532 PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO_KHR
);
1534 if (domain_origin_state
&& domain_origin_state
->domainOrigin
!= VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT_KHR
)
1537 if (tes
->info
.tes
.point_mode
)
1538 topology
= V_028B6C_OUTPUT_POINT
;
1539 else if (tes
->info
.tes
.primitive_mode
== GL_ISOLINES
)
1540 topology
= V_028B6C_OUTPUT_LINE
;
1542 topology
= V_028B6C_OUTPUT_TRIANGLE_CCW
;
1544 topology
= V_028B6C_OUTPUT_TRIANGLE_CW
;
1546 if (pipeline
->device
->has_distributed_tess
) {
1547 if (pipeline
->device
->physical_device
->rad_info
.family
== CHIP_FIJI
||
1548 pipeline
->device
->physical_device
->rad_info
.family
>= CHIP_POLARIS10
)
1549 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_TRAPEZOIDS
;
1551 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_DONUTS
;
1553 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_NO_DIST
;
1555 tess
->tf_param
= S_028B6C_TYPE(type
) |
1556 S_028B6C_PARTITIONING(partitioning
) |
1557 S_028B6C_TOPOLOGY(topology
) |
1558 S_028B6C_DISTRIBUTION_MODE(distribution_mode
);
1561 static const struct radv_prim_vertex_count prim_size_table
[] = {
1562 [V_008958_DI_PT_NONE
] = {0, 0},
1563 [V_008958_DI_PT_POINTLIST
] = {1, 1},
1564 [V_008958_DI_PT_LINELIST
] = {2, 2},
1565 [V_008958_DI_PT_LINESTRIP
] = {2, 1},
1566 [V_008958_DI_PT_TRILIST
] = {3, 3},
1567 [V_008958_DI_PT_TRIFAN
] = {3, 1},
1568 [V_008958_DI_PT_TRISTRIP
] = {3, 1},
1569 [V_008958_DI_PT_LINELIST_ADJ
] = {4, 4},
1570 [V_008958_DI_PT_LINESTRIP_ADJ
] = {4, 1},
1571 [V_008958_DI_PT_TRILIST_ADJ
] = {6, 6},
1572 [V_008958_DI_PT_TRISTRIP_ADJ
] = {6, 2},
1573 [V_008958_DI_PT_RECTLIST
] = {3, 3},
1574 [V_008958_DI_PT_LINELOOP
] = {2, 1},
1575 [V_008958_DI_PT_POLYGON
] = {3, 1},
1576 [V_008958_DI_PT_2D_TRI_STRIP
] = {0, 0},
1579 static const struct ac_vs_output_info
*get_vs_output_info(const struct radv_pipeline
*pipeline
)
1581 if (radv_pipeline_has_gs(pipeline
))
1582 return &pipeline
->gs_copy_shader
->info
.vs
.outinfo
;
1583 else if (radv_pipeline_has_tess(pipeline
))
1584 return &pipeline
->shaders
[MESA_SHADER_TESS_EVAL
]->info
.tes
.outinfo
;
1586 return &pipeline
->shaders
[MESA_SHADER_VERTEX
]->info
.vs
.outinfo
;
1589 static void calculate_vgt_gs_mode(struct radv_pipeline
*pipeline
)
1591 const struct ac_vs_output_info
*outinfo
= get_vs_output_info(pipeline
);
1593 pipeline
->graphics
.vgt_primitiveid_en
= false;
1594 pipeline
->graphics
.vgt_gs_mode
= 0;
1596 if (radv_pipeline_has_gs(pipeline
)) {
1597 struct radv_shader_variant
*gs
=
1598 pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
1600 pipeline
->graphics
.vgt_gs_mode
=
1601 ac_vgt_gs_mode(gs
->info
.gs
.vertices_out
,
1602 pipeline
->device
->physical_device
->rad_info
.chip_class
);
1603 } else if (outinfo
->export_prim_id
) {
1604 pipeline
->graphics
.vgt_gs_mode
= S_028A40_MODE(V_028A40_GS_SCENARIO_A
);
1605 pipeline
->graphics
.vgt_primitiveid_en
= true;
1609 static uint32_t offset_to_ps_input(uint32_t offset
, bool flat_shade
)
1611 uint32_t ps_input_cntl
;
1612 if (offset
<= AC_EXP_PARAM_OFFSET_31
) {
1613 ps_input_cntl
= S_028644_OFFSET(offset
);
1615 ps_input_cntl
|= S_028644_FLAT_SHADE(1);
1617 /* The input is a DEFAULT_VAL constant. */
1618 assert(offset
>= AC_EXP_PARAM_DEFAULT_VAL_0000
&&
1619 offset
<= AC_EXP_PARAM_DEFAULT_VAL_1111
);
1620 offset
-= AC_EXP_PARAM_DEFAULT_VAL_0000
;
1621 ps_input_cntl
= S_028644_OFFSET(0x20) |
1622 S_028644_DEFAULT_VAL(offset
);
1624 return ps_input_cntl
;
1627 static void calculate_ps_inputs(struct radv_pipeline
*pipeline
)
1629 struct radv_shader_variant
*ps
;
1630 const struct ac_vs_output_info
*outinfo
= get_vs_output_info(pipeline
);
1632 ps
= pipeline
->shaders
[MESA_SHADER_FRAGMENT
];
1634 unsigned ps_offset
= 0;
1636 if (ps
->info
.fs
.prim_id_input
) {
1637 unsigned vs_offset
= outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
];
1638 if (vs_offset
!= AC_EXP_PARAM_UNDEFINED
) {
1639 pipeline
->graphics
.ps_input_cntl
[ps_offset
] = offset_to_ps_input(vs_offset
, true);
1644 if (ps
->info
.fs
.layer_input
) {
1645 unsigned vs_offset
= outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
];
1646 if (vs_offset
!= AC_EXP_PARAM_UNDEFINED
)
1647 pipeline
->graphics
.ps_input_cntl
[ps_offset
] = offset_to_ps_input(vs_offset
, true);
1649 pipeline
->graphics
.ps_input_cntl
[ps_offset
] = offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000
, true);
1653 if (ps
->info
.fs
.has_pcoord
) {
1655 val
= S_028644_PT_SPRITE_TEX(1) | S_028644_OFFSET(0x20);
1656 pipeline
->graphics
.ps_input_cntl
[ps_offset
] = val
;
1660 for (unsigned i
= 0; i
< 32 && (1u << i
) <= ps
->info
.fs
.input_mask
; ++i
) {
1663 if (!(ps
->info
.fs
.input_mask
& (1u << i
)))
1666 vs_offset
= outinfo
->vs_output_param_offset
[VARYING_SLOT_VAR0
+ i
];
1667 if (vs_offset
== AC_EXP_PARAM_UNDEFINED
) {
1668 pipeline
->graphics
.ps_input_cntl
[ps_offset
] = S_028644_OFFSET(0x20);
1673 flat_shade
= !!(ps
->info
.fs
.flat_shaded_mask
& (1u << ps_offset
));
1675 pipeline
->graphics
.ps_input_cntl
[ps_offset
] = offset_to_ps_input(vs_offset
, flat_shade
);
1679 pipeline
->graphics
.ps_input_cntl_num
= ps_offset
;
1683 radv_link_shaders(struct radv_pipeline
*pipeline
, nir_shader
**shaders
)
1685 nir_shader
* ordered_shaders
[MESA_SHADER_STAGES
];
1686 int shader_count
= 0;
1688 if(shaders
[MESA_SHADER_FRAGMENT
]) {
1689 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_FRAGMENT
];
1691 if(shaders
[MESA_SHADER_GEOMETRY
]) {
1692 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_GEOMETRY
];
1694 if(shaders
[MESA_SHADER_TESS_EVAL
]) {
1695 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_TESS_EVAL
];
1697 if(shaders
[MESA_SHADER_TESS_CTRL
]) {
1698 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_TESS_CTRL
];
1700 if(shaders
[MESA_SHADER_VERTEX
]) {
1701 ordered_shaders
[shader_count
++] = shaders
[MESA_SHADER_VERTEX
];
1704 for (int i
= 1; i
< shader_count
; ++i
) {
1705 nir_lower_io_arrays_to_elements(ordered_shaders
[i
],
1706 ordered_shaders
[i
- 1]);
1708 nir_remove_dead_variables(ordered_shaders
[i
],
1709 nir_var_shader_out
);
1710 nir_remove_dead_variables(ordered_shaders
[i
- 1],
1713 bool progress
= nir_remove_unused_varyings(ordered_shaders
[i
],
1714 ordered_shaders
[i
- 1]);
1717 nir_lower_global_vars_to_local(ordered_shaders
[i
]);
1718 radv_optimize_nir(ordered_shaders
[i
]);
1719 nir_lower_global_vars_to_local(ordered_shaders
[i
- 1]);
1720 radv_optimize_nir(ordered_shaders
[i
- 1]);
1726 static struct radv_pipeline_key
1727 radv_generate_graphics_pipeline_key(struct radv_pipeline
*pipeline
,
1728 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1729 bool has_view_index
)
1731 const VkPipelineVertexInputStateCreateInfo
*input_state
=
1732 pCreateInfo
->pVertexInputState
;
1733 struct radv_pipeline_key key
;
1734 memset(&key
, 0, sizeof(key
));
1736 key
.has_multiview_view_index
= has_view_index
;
1738 uint32_t binding_input_rate
= 0;
1739 for (unsigned i
= 0; i
< input_state
->vertexBindingDescriptionCount
; ++i
) {
1740 if (input_state
->pVertexBindingDescriptions
[i
].inputRate
)
1741 binding_input_rate
|= 1u << input_state
->pVertexBindingDescriptions
[i
].binding
;
1744 for (unsigned i
= 0; i
< input_state
->vertexAttributeDescriptionCount
; ++i
) {
1746 binding
= input_state
->pVertexAttributeDescriptions
[i
].binding
;
1747 if (binding_input_rate
& (1u << binding
))
1748 key
.instance_rate_inputs
|= 1u << input_state
->pVertexAttributeDescriptions
[i
].location
;
1751 if (pCreateInfo
->pTessellationState
)
1752 key
.tess_input_vertices
= pCreateInfo
->pTessellationState
->patchControlPoints
;
1755 if (pCreateInfo
->pMultisampleState
&&
1756 pCreateInfo
->pMultisampleState
->rasterizationSamples
> 1) {
1757 uint32_t num_samples
= pCreateInfo
->pMultisampleState
->rasterizationSamples
;
1758 uint32_t ps_iter_samples
= radv_pipeline_get_ps_iter_samples(pCreateInfo
->pMultisampleState
);
1759 key
.multisample
= true;
1760 key
.log2_num_samples
= util_logbase2(num_samples
);
1761 key
.log2_ps_iter_samples
= util_logbase2(ps_iter_samples
);
1764 key
.col_format
= pipeline
->graphics
.blend
.spi_shader_col_format
;
1765 if (pipeline
->device
->physical_device
->rad_info
.chip_class
< VI
)
1766 radv_pipeline_compute_get_int_clamp(pCreateInfo
, &key
.is_int8
, &key
.is_int10
);
1772 radv_fill_shader_keys(struct ac_shader_variant_key
*keys
,
1773 const struct radv_pipeline_key
*key
,
1776 keys
[MESA_SHADER_VERTEX
].vs
.instance_rate_inputs
= key
->instance_rate_inputs
;
1778 if (nir
[MESA_SHADER_TESS_CTRL
]) {
1779 keys
[MESA_SHADER_VERTEX
].vs
.as_ls
= true;
1780 keys
[MESA_SHADER_TESS_CTRL
].tcs
.input_vertices
= key
->tess_input_vertices
;
1781 keys
[MESA_SHADER_TESS_CTRL
].tcs
.primitive_mode
= nir
[MESA_SHADER_TESS_EVAL
]->info
.tess
.primitive_mode
;
1783 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
));
1786 if (nir
[MESA_SHADER_GEOMETRY
]) {
1787 if (nir
[MESA_SHADER_TESS_CTRL
])
1788 keys
[MESA_SHADER_TESS_EVAL
].tes
.as_es
= true;
1790 keys
[MESA_SHADER_VERTEX
].vs
.as_es
= true;
1793 for(int i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
1794 keys
[i
].has_multiview_view_index
= key
->has_multiview_view_index
;
1796 keys
[MESA_SHADER_FRAGMENT
].fs
.multisample
= key
->multisample
;
1797 keys
[MESA_SHADER_FRAGMENT
].fs
.col_format
= key
->col_format
;
1798 keys
[MESA_SHADER_FRAGMENT
].fs
.is_int8
= key
->is_int8
;
1799 keys
[MESA_SHADER_FRAGMENT
].fs
.is_int10
= key
->is_int10
;
1800 keys
[MESA_SHADER_FRAGMENT
].fs
.log2_ps_iter_samples
= key
->log2_ps_iter_samples
;
1801 keys
[MESA_SHADER_FRAGMENT
].fs
.log2_num_samples
= key
->log2_num_samples
;
1805 merge_tess_info(struct shader_info
*tes_info
,
1806 const struct shader_info
*tcs_info
)
1808 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
1810 * "PointMode. Controls generation of points rather than triangles
1811 * or lines. This functionality defaults to disabled, and is
1812 * enabled if either shader stage includes the execution mode.
1814 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
1815 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
1816 * and OutputVertices, it says:
1818 * "One mode must be set in at least one of the tessellation
1821 * So, the fields can be set in either the TCS or TES, but they must
1822 * agree if set in both. Our backend looks at TES, so bitwise-or in
1823 * the values from the TCS.
1825 assert(tcs_info
->tess
.tcs_vertices_out
== 0 ||
1826 tes_info
->tess
.tcs_vertices_out
== 0 ||
1827 tcs_info
->tess
.tcs_vertices_out
== tes_info
->tess
.tcs_vertices_out
);
1828 tes_info
->tess
.tcs_vertices_out
|= tcs_info
->tess
.tcs_vertices_out
;
1830 assert(tcs_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
1831 tes_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
1832 tcs_info
->tess
.spacing
== tes_info
->tess
.spacing
);
1833 tes_info
->tess
.spacing
|= tcs_info
->tess
.spacing
;
1835 assert(tcs_info
->tess
.primitive_mode
== 0 ||
1836 tes_info
->tess
.primitive_mode
== 0 ||
1837 tcs_info
->tess
.primitive_mode
== tes_info
->tess
.primitive_mode
);
1838 tes_info
->tess
.primitive_mode
|= tcs_info
->tess
.primitive_mode
;
1839 tes_info
->tess
.ccw
|= tcs_info
->tess
.ccw
;
1840 tes_info
->tess
.point_mode
|= tcs_info
->tess
.point_mode
;
1844 void radv_create_shaders(struct radv_pipeline
*pipeline
,
1845 struct radv_device
*device
,
1846 struct radv_pipeline_cache
*cache
,
1847 struct radv_pipeline_key key
,
1848 const VkPipelineShaderStageCreateInfo
**pStages
)
1850 struct radv_shader_module fs_m
= {0};
1851 struct radv_shader_module
*modules
[MESA_SHADER_STAGES
] = { 0, };
1852 nir_shader
*nir
[MESA_SHADER_STAGES
] = {0};
1853 void *codes
[MESA_SHADER_STAGES
] = {0};
1854 unsigned code_sizes
[MESA_SHADER_STAGES
] = {0};
1855 struct ac_shader_variant_key keys
[MESA_SHADER_STAGES
] = {{{{0}}}};
1856 unsigned char hash
[20], gs_copy_hash
[20];
1858 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
1860 modules
[i
] = radv_shader_module_from_handle(pStages
[i
]->module
);
1861 if (modules
[i
]->nir
)
1862 _mesa_sha1_compute(modules
[i
]->nir
->info
.name
,
1863 strlen(modules
[i
]->nir
->info
.name
),
1868 radv_hash_shaders(hash
, pStages
, pipeline
->layout
, &key
, get_hash_flags(device
));
1869 memcpy(gs_copy_hash
, hash
, 20);
1870 gs_copy_hash
[0] ^= 1;
1872 if (modules
[MESA_SHADER_GEOMETRY
]) {
1873 struct radv_shader_variant
*variants
[MESA_SHADER_STAGES
] = {0};
1874 radv_create_shader_variants_from_pipeline_cache(device
, cache
, gs_copy_hash
, variants
);
1875 pipeline
->gs_copy_shader
= variants
[MESA_SHADER_GEOMETRY
];
1878 if (radv_create_shader_variants_from_pipeline_cache(device
, cache
, hash
, pipeline
->shaders
) &&
1879 (!modules
[MESA_SHADER_GEOMETRY
] || pipeline
->gs_copy_shader
)) {
1880 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
1881 if (pipeline
->shaders
[i
])
1882 pipeline
->active_stages
|= mesa_to_vk_shader_stage(i
);
1887 if (!modules
[MESA_SHADER_FRAGMENT
] && !modules
[MESA_SHADER_COMPUTE
]) {
1889 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
1890 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "noop_fs");
1891 fs_m
.nir
= fs_b
.shader
;
1892 modules
[MESA_SHADER_FRAGMENT
] = &fs_m
;
1895 /* Determine first and last stage. */
1896 unsigned first
= MESA_SHADER_STAGES
;
1898 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1901 if (first
== MESA_SHADER_STAGES
)
1907 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
1908 const VkPipelineShaderStageCreateInfo
*stage
= pStages
[i
];
1913 nir
[i
] = radv_shader_compile_to_nir(device
, modules
[i
],
1914 stage
? stage
->pName
: "main", i
,
1915 stage
? stage
->pSpecializationInfo
: NULL
);
1916 pipeline
->active_stages
|= mesa_to_vk_shader_stage(i
);
1918 /* We don't want to alter meta shaders IR directly so clone it
1921 if (nir
[i
]->info
.name
) {
1922 nir
[i
] = nir_shader_clone(NULL
, nir
[i
]);
1925 if (first
!= last
) {
1926 nir_variable_mode mask
= 0;
1929 mask
= mask
| nir_var_shader_in
;
1932 mask
= mask
| nir_var_shader_out
;
1934 nir_lower_io_to_scalar_early(nir
[i
], mask
);
1935 radv_optimize_nir(nir
[i
]);
1939 nir_compact_varyings(nir
[prev
], nir
[i
], true);
1944 if (nir
[MESA_SHADER_TESS_CTRL
]) {
1945 nir_lower_tes_patch_vertices(nir
[MESA_SHADER_TESS_EVAL
], nir
[MESA_SHADER_TESS_CTRL
]->info
.tess
.tcs_vertices_out
);
1946 merge_tess_info(&nir
[MESA_SHADER_TESS_EVAL
]->info
, &nir
[MESA_SHADER_TESS_CTRL
]->info
);
1949 radv_link_shaders(pipeline
, nir
);
1951 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
1952 if (modules
[i
] && radv_can_dump_shader(device
, modules
[i
]))
1953 nir_print_shader(nir
[i
], stderr
);
1956 radv_fill_shader_keys(keys
, &key
, nir
);
1958 if (nir
[MESA_SHADER_FRAGMENT
]) {
1959 if (!pipeline
->shaders
[MESA_SHADER_FRAGMENT
]) {
1960 pipeline
->shaders
[MESA_SHADER_FRAGMENT
] =
1961 radv_shader_variant_create(device
, modules
[MESA_SHADER_FRAGMENT
], &nir
[MESA_SHADER_FRAGMENT
], 1,
1962 pipeline
->layout
, keys
+ MESA_SHADER_FRAGMENT
,
1963 &codes
[MESA_SHADER_FRAGMENT
], &code_sizes
[MESA_SHADER_FRAGMENT
]);
1966 /* TODO: These are no longer used as keys we should refactor this */
1967 keys
[MESA_SHADER_VERTEX
].vs
.export_prim_id
=
1968 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.fs
.prim_id_input
;
1969 keys
[MESA_SHADER_TESS_EVAL
].tes
.export_prim_id
=
1970 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.fs
.prim_id_input
;
1973 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&& modules
[MESA_SHADER_TESS_CTRL
]) {
1974 if (!pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]) {
1975 struct nir_shader
*combined_nir
[] = {nir
[MESA_SHADER_VERTEX
], nir
[MESA_SHADER_TESS_CTRL
]};
1976 struct ac_shader_variant_key key
= keys
[MESA_SHADER_TESS_CTRL
];
1977 key
.tcs
.vs_key
= keys
[MESA_SHADER_VERTEX
].vs
;
1978 pipeline
->shaders
[MESA_SHADER_TESS_CTRL
] = radv_shader_variant_create(device
, modules
[MESA_SHADER_TESS_CTRL
], combined_nir
, 2,
1980 &key
, &codes
[MESA_SHADER_TESS_CTRL
],
1981 &code_sizes
[MESA_SHADER_TESS_CTRL
]);
1983 modules
[MESA_SHADER_VERTEX
] = NULL
;
1986 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&& modules
[MESA_SHADER_GEOMETRY
]) {
1987 gl_shader_stage pre_stage
= modules
[MESA_SHADER_TESS_EVAL
] ? MESA_SHADER_TESS_EVAL
: MESA_SHADER_VERTEX
;
1988 if (!pipeline
->shaders
[MESA_SHADER_GEOMETRY
]) {
1989 struct nir_shader
*combined_nir
[] = {nir
[pre_stage
], nir
[MESA_SHADER_GEOMETRY
]};
1990 pipeline
->shaders
[MESA_SHADER_GEOMETRY
] = radv_shader_variant_create(device
, modules
[MESA_SHADER_GEOMETRY
], combined_nir
, 2,
1992 &keys
[pre_stage
] , &codes
[MESA_SHADER_GEOMETRY
],
1993 &code_sizes
[MESA_SHADER_GEOMETRY
]);
1995 modules
[pre_stage
] = NULL
;
1998 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
1999 if(modules
[i
] && !pipeline
->shaders
[i
]) {
2000 pipeline
->shaders
[i
] = radv_shader_variant_create(device
, modules
[i
], &nir
[i
], 1,
2002 keys
+ i
, &codes
[i
],
2007 if(modules
[MESA_SHADER_GEOMETRY
]) {
2008 void *gs_copy_code
= NULL
;
2009 unsigned gs_copy_code_size
= 0;
2010 if (!pipeline
->gs_copy_shader
) {
2011 pipeline
->gs_copy_shader
= radv_create_gs_copy_shader(
2012 device
, nir
[MESA_SHADER_GEOMETRY
], &gs_copy_code
,
2014 keys
[MESA_SHADER_GEOMETRY
].has_multiview_view_index
);
2017 if (pipeline
->gs_copy_shader
) {
2018 void *code
[MESA_SHADER_STAGES
] = {0};
2019 unsigned code_size
[MESA_SHADER_STAGES
] = {0};
2020 struct radv_shader_variant
*variants
[MESA_SHADER_STAGES
] = {0};
2022 code
[MESA_SHADER_GEOMETRY
] = gs_copy_code
;
2023 code_size
[MESA_SHADER_GEOMETRY
] = gs_copy_code_size
;
2024 variants
[MESA_SHADER_GEOMETRY
] = pipeline
->gs_copy_shader
;
2026 radv_pipeline_cache_insert_shaders(device
, cache
,
2035 radv_pipeline_cache_insert_shaders(device
, cache
, hash
, pipeline
->shaders
,
2036 (const void**)codes
, code_sizes
);
2038 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
2040 if (modules
[i
] && !pipeline
->device
->keep_shader_info
)
2041 ralloc_free(nir
[i
]);
2045 ralloc_free(fs_m
.nir
);
2049 radv_pipeline_stage_to_user_data_0(struct radv_pipeline
*pipeline
,
2050 gl_shader_stage stage
, enum chip_class chip_class
)
2052 bool has_gs
= radv_pipeline_has_gs(pipeline
);
2053 bool has_tess
= radv_pipeline_has_tess(pipeline
);
2055 case MESA_SHADER_FRAGMENT
:
2056 return R_00B030_SPI_SHADER_USER_DATA_PS_0
;
2057 case MESA_SHADER_VERTEX
:
2058 if (chip_class
>= GFX9
) {
2059 return has_tess
? R_00B430_SPI_SHADER_USER_DATA_LS_0
:
2060 has_gs
? R_00B330_SPI_SHADER_USER_DATA_ES_0
:
2061 R_00B130_SPI_SHADER_USER_DATA_VS_0
;
2064 return R_00B530_SPI_SHADER_USER_DATA_LS_0
;
2066 return has_gs
? R_00B330_SPI_SHADER_USER_DATA_ES_0
: R_00B130_SPI_SHADER_USER_DATA_VS_0
;
2067 case MESA_SHADER_GEOMETRY
:
2068 return chip_class
>= GFX9
? R_00B330_SPI_SHADER_USER_DATA_ES_0
:
2069 R_00B230_SPI_SHADER_USER_DATA_GS_0
;
2070 case MESA_SHADER_COMPUTE
:
2071 return R_00B900_COMPUTE_USER_DATA_0
;
2072 case MESA_SHADER_TESS_CTRL
:
2073 return chip_class
>= GFX9
? R_00B430_SPI_SHADER_USER_DATA_LS_0
:
2074 R_00B430_SPI_SHADER_USER_DATA_HS_0
;
2075 case MESA_SHADER_TESS_EVAL
:
2076 if (chip_class
>= GFX9
) {
2077 return has_gs
? R_00B330_SPI_SHADER_USER_DATA_ES_0
:
2078 R_00B130_SPI_SHADER_USER_DATA_VS_0
;
2081 return R_00B330_SPI_SHADER_USER_DATA_ES_0
;
2083 return R_00B130_SPI_SHADER_USER_DATA_VS_0
;
2085 unreachable("unknown shader");
2089 struct radv_bin_size_entry
{
2095 radv_compute_bin_size(struct radv_pipeline
*pipeline
, const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
2097 static const struct radv_bin_size_entry color_size_table
[][3][9] = {
2101 /* One shader engine */
2107 { UINT_MAX
, { 0, 0}},
2110 /* Two shader engines */
2116 { UINT_MAX
, { 0, 0}},
2119 /* Four shader engines */
2124 { UINT_MAX
, { 0, 0}},
2130 /* One shader engine */
2136 { UINT_MAX
, { 0, 0}},
2139 /* Two shader engines */
2145 { UINT_MAX
, { 0, 0}},
2148 /* Four shader engines */
2155 { UINT_MAX
, { 0, 0}},
2161 /* One shader engine */
2168 { UINT_MAX
, { 0, 0}},
2171 /* Two shader engines */
2179 { UINT_MAX
, { 0, 0}},
2182 /* Four shader engines */
2190 { UINT_MAX
, { 0, 0}},
2194 static const struct radv_bin_size_entry ds_size_table
[][3][9] = {
2198 // One shader engine
2205 { UINT_MAX
, { 0, 0}},
2208 // Two shader engines
2216 { UINT_MAX
, { 0, 0}},
2219 // Four shader engines
2227 { UINT_MAX
, { 0, 0}},
2233 // One shader engine
2241 { UINT_MAX
, { 0, 0}},
2244 // Two shader engines
2253 { UINT_MAX
, { 0, 0}},
2256 // Four shader engines
2265 { UINT_MAX
, { 0, 0}},
2271 // One shader engine
2279 { UINT_MAX
, { 0, 0}},
2282 // Two shader engines
2291 { UINT_MAX
, { 0, 0}},
2294 // Four shader engines
2302 { UINT_MAX
, { 0, 0}},
2307 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
2308 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
2309 VkExtent2D extent
= {512, 512};
2311 unsigned log_num_rb_per_se
=
2312 util_logbase2_ceil(pipeline
->device
->physical_device
->rad_info
.num_render_backends
/
2313 pipeline
->device
->physical_device
->rad_info
.max_se
);
2314 unsigned log_num_se
= util_logbase2_ceil(pipeline
->device
->physical_device
->rad_info
.max_se
);
2316 unsigned total_samples
= 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline
->graphics
.ms
.pa_sc_mode_cntl_1
);
2317 unsigned ps_iter_samples
= 1u << G_028804_PS_ITER_SAMPLES(pipeline
->graphics
.ms
.db_eqaa
);
2318 unsigned effective_samples
= total_samples
;
2319 unsigned cb_target_mask
= pipeline
->graphics
.blend
.cb_target_mask
;
2320 unsigned color_bytes_per_pixel
= 0;
2322 for (unsigned i
= 0; i
< subpass
->color_count
; i
++) {
2323 if (!(cb_target_mask
& (0xf << (i
* 4))))
2326 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
)
2329 VkFormat format
= pass
->attachments
[subpass
->color_attachments
[i
].attachment
].format
;
2330 color_bytes_per_pixel
+= vk_format_get_blocksize(format
);
2333 /* MSAA images typically don't use all samples all the time. */
2334 if (effective_samples
>= 2 && ps_iter_samples
<= 1)
2335 effective_samples
= 2;
2336 color_bytes_per_pixel
*= effective_samples
;
2338 const struct radv_bin_size_entry
*color_entry
= color_size_table
[log_num_rb_per_se
][log_num_se
];
2339 while(color_entry
->bpp
<= color_bytes_per_pixel
)
2342 extent
= color_entry
->extent
;
2344 if (subpass
->depth_stencil_attachment
.attachment
!= VK_ATTACHMENT_UNUSED
) {
2345 struct radv_render_pass_attachment
*attachment
= pass
->attachments
+ subpass
->depth_stencil_attachment
.attachment
;
2347 /* Coefficients taken from AMDVLK */
2348 unsigned depth_coeff
= vk_format_is_depth(attachment
->format
) ? 5 : 0;
2349 unsigned stencil_coeff
= vk_format_is_stencil(attachment
->format
) ? 1 : 0;
2350 unsigned ds_bytes_per_pixel
= 4 * (depth_coeff
+ stencil_coeff
) * total_samples
;
2352 const struct radv_bin_size_entry
*ds_entry
= ds_size_table
[log_num_rb_per_se
][log_num_se
];
2353 while(ds_entry
->bpp
<= ds_bytes_per_pixel
)
2356 extent
.width
= MIN2(extent
.width
, ds_entry
->extent
.width
);
2357 extent
.height
= MIN2(extent
.height
, ds_entry
->extent
.height
);
2364 radv_pipeline_generate_binning_state(struct radeon_winsys_cs
*cs
,
2365 struct radv_pipeline
*pipeline
,
2366 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
2368 if (pipeline
->device
->physical_device
->rad_info
.chip_class
< GFX9
)
2371 uint32_t pa_sc_binner_cntl_0
=
2372 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC
) |
2373 S_028C44_DISABLE_START_OF_PRIM(1);
2374 uint32_t db_dfsm_control
= S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF
);
2376 VkExtent2D bin_size
= radv_compute_bin_size(pipeline
, pCreateInfo
);
2378 unsigned context_states_per_bin
; /* allowed range: [1, 6] */
2379 unsigned persistent_states_per_bin
; /* allowed range: [1, 32] */
2380 unsigned fpovs_per_batch
; /* allowed range: [0, 255], 0 = unlimited */
2382 switch (pipeline
->device
->physical_device
->rad_info
.family
) {
2384 context_states_per_bin
= 1;
2385 persistent_states_per_bin
= 1;
2386 fpovs_per_batch
= 63;
2389 context_states_per_bin
= 6;
2390 persistent_states_per_bin
= 32;
2391 fpovs_per_batch
= 63;
2394 unreachable("unhandled family while determining binning state.");
2397 if (pipeline
->device
->pbb_allowed
&& bin_size
.width
&& bin_size
.height
) {
2398 pa_sc_binner_cntl_0
=
2399 S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED
) |
2400 S_028C44_BIN_SIZE_X(bin_size
.width
== 16) |
2401 S_028C44_BIN_SIZE_Y(bin_size
.height
== 16) |
2402 S_028C44_BIN_SIZE_X_EXTEND(util_logbase2(MAX2(bin_size
.width
, 32)) - 5) |
2403 S_028C44_BIN_SIZE_Y_EXTEND(util_logbase2(MAX2(bin_size
.height
, 32)) - 5) |
2404 S_028C44_CONTEXT_STATES_PER_BIN(context_states_per_bin
- 1) |
2405 S_028C44_PERSISTENT_STATES_PER_BIN(persistent_states_per_bin
- 1) |
2406 S_028C44_DISABLE_START_OF_PRIM(1) |
2407 S_028C44_FPOVS_PER_BATCH(fpovs_per_batch
) |
2408 S_028C44_OPTIMAL_BIN_SELECTION(1);
2411 radeon_set_context_reg(cs
, R_028C44_PA_SC_BINNER_CNTL_0
,
2412 pa_sc_binner_cntl_0
);
2413 radeon_set_context_reg(cs
, R_028060_DB_DFSM_CONTROL
,
2419 radv_pipeline_generate_depth_stencil_state(struct radeon_winsys_cs
*cs
,
2420 struct radv_pipeline
*pipeline
)
2422 struct radv_depth_stencil_state
*ds
= &pipeline
->graphics
.ds
;
2423 radeon_set_context_reg(cs
, R_028800_DB_DEPTH_CONTROL
, ds
->db_depth_control
);
2424 radeon_set_context_reg(cs
, R_02842C_DB_STENCIL_CONTROL
, ds
->db_stencil_control
);
2426 radeon_set_context_reg(cs
, R_028000_DB_RENDER_CONTROL
, ds
->db_render_control
);
2427 radeon_set_context_reg(cs
, R_028010_DB_RENDER_OVERRIDE2
, ds
->db_render_override2
);
2431 radv_pipeline_generate_blend_state(struct radeon_winsys_cs
*cs
,
2432 struct radv_pipeline
*pipeline
)
2434 radeon_set_context_reg_seq(cs
, R_028780_CB_BLEND0_CONTROL
, 8);
2435 radeon_emit_array(cs
, pipeline
->graphics
.blend
.cb_blend_control
,
2437 radeon_set_context_reg(cs
, R_028808_CB_COLOR_CONTROL
, pipeline
->graphics
.blend
.cb_color_control
);
2438 radeon_set_context_reg(cs
, R_028B70_DB_ALPHA_TO_MASK
, pipeline
->graphics
.blend
.db_alpha_to_mask
);
2440 if (pipeline
->device
->physical_device
->has_rbplus
) {
2442 radeon_set_context_reg_seq(cs
, R_028760_SX_MRT0_BLEND_OPT
, 8);
2443 radeon_emit_array(cs
, pipeline
->graphics
.blend
.sx_mrt_blend_opt
, 8);
2445 radeon_set_context_reg_seq(cs
, R_028754_SX_PS_DOWNCONVERT
, 3);
2446 radeon_emit(cs
, 0); /* R_028754_SX_PS_DOWNCONVERT */
2447 radeon_emit(cs
, 0); /* R_028758_SX_BLEND_OPT_EPSILON */
2448 radeon_emit(cs
, 0); /* R_02875C_SX_BLEND_OPT_CONTROL */
2454 radv_pipeline_generate_raster_state(struct radeon_winsys_cs
*cs
,
2455 struct radv_pipeline
*pipeline
)
2457 struct radv_raster_state
*raster
= &pipeline
->graphics
.raster
;
2459 radeon_set_context_reg(cs
, R_028810_PA_CL_CLIP_CNTL
,
2460 raster
->pa_cl_clip_cntl
);
2461 radeon_set_context_reg(cs
, R_0286D4_SPI_INTERP_CONTROL_0
,
2462 raster
->spi_interp_control
);
2463 radeon_set_context_reg(cs
, R_028BE4_PA_SU_VTX_CNTL
,
2464 raster
->pa_su_vtx_cntl
);
2465 radeon_set_context_reg(cs
, R_028814_PA_SU_SC_MODE_CNTL
,
2466 raster
->pa_su_sc_mode_cntl
);
2471 radv_pipeline_generate_multisample_state(struct radeon_winsys_cs
*cs
,
2472 struct radv_pipeline
*pipeline
)
2474 struct radv_multisample_state
*ms
= &pipeline
->graphics
.ms
;
2476 radeon_set_context_reg_seq(cs
, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0
, 2);
2477 radeon_emit(cs
, ms
->pa_sc_aa_mask
[0]);
2478 radeon_emit(cs
, ms
->pa_sc_aa_mask
[1]);
2480 radeon_set_context_reg(cs
, R_028804_DB_EQAA
, ms
->db_eqaa
);
2481 radeon_set_context_reg(cs
, R_028A4C_PA_SC_MODE_CNTL_1
, ms
->pa_sc_mode_cntl_1
);
2483 if (pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.info
.ps
.needs_sample_positions
) {
2485 struct ac_userdata_info
*loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_FRAGMENT
, AC_UD_PS_SAMPLE_POS_OFFSET
);
2486 uint32_t base_reg
= pipeline
->user_data_0
[MESA_SHADER_FRAGMENT
];
2487 if (loc
->sgpr_idx
== -1)
2489 assert(loc
->num_sgprs
== 1);
2490 assert(!loc
->indirect
);
2491 switch (pipeline
->graphics
.ms
.num_samples
) {
2509 radeon_set_sh_reg(cs
, base_reg
+ loc
->sgpr_idx
* 4, offset
);
2514 radv_pipeline_generate_hw_vs(struct radeon_winsys_cs
*cs
,
2515 struct radv_pipeline
*pipeline
,
2516 struct radv_shader_variant
*shader
)
2518 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
2520 radeon_set_sh_reg_seq(cs
, R_00B120_SPI_SHADER_PGM_LO_VS
, 4);
2521 radeon_emit(cs
, va
>> 8);
2522 radeon_emit(cs
, va
>> 40);
2523 radeon_emit(cs
, shader
->rsrc1
);
2524 radeon_emit(cs
, shader
->rsrc2
);
2526 const struct ac_vs_output_info
*outinfo
= get_vs_output_info(pipeline
);
2527 unsigned clip_dist_mask
, cull_dist_mask
, total_mask
;
2528 clip_dist_mask
= outinfo
->clip_dist_mask
;
2529 cull_dist_mask
= outinfo
->cull_dist_mask
;
2530 total_mask
= clip_dist_mask
| cull_dist_mask
;
2531 bool misc_vec_ena
= outinfo
->writes_pointsize
||
2532 outinfo
->writes_layer
||
2533 outinfo
->writes_viewport_index
;
2535 radeon_set_context_reg(cs
, R_0286C4_SPI_VS_OUT_CONFIG
,
2536 S_0286C4_VS_EXPORT_COUNT(MAX2(1, outinfo
->param_exports
) - 1));
2538 radeon_set_context_reg(cs
, R_02870C_SPI_SHADER_POS_FORMAT
,
2539 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP
) |
2540 S_02870C_POS1_EXPORT_FORMAT(outinfo
->pos_exports
> 1 ?
2541 V_02870C_SPI_SHADER_4COMP
:
2542 V_02870C_SPI_SHADER_NONE
) |
2543 S_02870C_POS2_EXPORT_FORMAT(outinfo
->pos_exports
> 2 ?
2544 V_02870C_SPI_SHADER_4COMP
:
2545 V_02870C_SPI_SHADER_NONE
) |
2546 S_02870C_POS3_EXPORT_FORMAT(outinfo
->pos_exports
> 3 ?
2547 V_02870C_SPI_SHADER_4COMP
:
2548 V_02870C_SPI_SHADER_NONE
));
2550 radeon_set_context_reg(cs
, R_028818_PA_CL_VTE_CNTL
,
2551 S_028818_VTX_W0_FMT(1) |
2552 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
2553 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
2554 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
2556 radeon_set_context_reg(cs
, R_02881C_PA_CL_VS_OUT_CNTL
,
2557 S_02881C_USE_VTX_POINT_SIZE(outinfo
->writes_pointsize
) |
2558 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo
->writes_layer
) |
2559 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo
->writes_viewport_index
) |
2560 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena
) |
2561 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena
) |
2562 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask
& 0x0f) != 0) |
2563 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask
& 0xf0) != 0) |
2564 cull_dist_mask
<< 8 |
2567 if (pipeline
->device
->physical_device
->rad_info
.chip_class
<= VI
)
2568 radeon_set_context_reg(cs
, R_028AB4_VGT_REUSE_OFF
,
2569 outinfo
->writes_viewport_index
);
2573 radv_pipeline_generate_hw_es(struct radeon_winsys_cs
*cs
,
2574 struct radv_pipeline
*pipeline
,
2575 struct radv_shader_variant
*shader
)
2577 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
2579 radeon_set_sh_reg_seq(cs
, R_00B320_SPI_SHADER_PGM_LO_ES
, 4);
2580 radeon_emit(cs
, va
>> 8);
2581 radeon_emit(cs
, va
>> 40);
2582 radeon_emit(cs
, shader
->rsrc1
);
2583 radeon_emit(cs
, shader
->rsrc2
);
2587 radv_pipeline_generate_hw_ls(struct radeon_winsys_cs
*cs
,
2588 struct radv_pipeline
*pipeline
,
2589 struct radv_shader_variant
*shader
)
2591 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
2592 uint32_t rsrc2
= shader
->rsrc2
;
2594 radeon_set_sh_reg_seq(cs
, R_00B520_SPI_SHADER_PGM_LO_LS
, 2);
2595 radeon_emit(cs
, va
>> 8);
2596 radeon_emit(cs
, va
>> 40);
2598 rsrc2
|= S_00B52C_LDS_SIZE(pipeline
->graphics
.tess
.lds_size
);
2599 if (pipeline
->device
->physical_device
->rad_info
.chip_class
== CIK
&&
2600 pipeline
->device
->physical_device
->rad_info
.family
!= CHIP_HAWAII
)
2601 radeon_set_sh_reg(cs
, R_00B52C_SPI_SHADER_PGM_RSRC2_LS
, rsrc2
);
2603 radeon_set_sh_reg_seq(cs
, R_00B528_SPI_SHADER_PGM_RSRC1_LS
, 2);
2604 radeon_emit(cs
, shader
->rsrc1
);
2605 radeon_emit(cs
, rsrc2
);
2609 radv_pipeline_generate_hw_hs(struct radeon_winsys_cs
*cs
,
2610 struct radv_pipeline
*pipeline
,
2611 struct radv_shader_variant
*shader
)
2613 uint64_t va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
2615 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
2616 radeon_set_sh_reg_seq(cs
, R_00B410_SPI_SHADER_PGM_LO_LS
, 2);
2617 radeon_emit(cs
, va
>> 8);
2618 radeon_emit(cs
, va
>> 40);
2620 radeon_set_sh_reg_seq(cs
, R_00B428_SPI_SHADER_PGM_RSRC1_HS
, 2);
2621 radeon_emit(cs
, shader
->rsrc1
);
2622 radeon_emit(cs
, shader
->rsrc2
|
2623 S_00B42C_LDS_SIZE(pipeline
->graphics
.tess
.lds_size
));
2625 radeon_set_sh_reg_seq(cs
, R_00B420_SPI_SHADER_PGM_LO_HS
, 4);
2626 radeon_emit(cs
, va
>> 8);
2627 radeon_emit(cs
, va
>> 40);
2628 radeon_emit(cs
, shader
->rsrc1
);
2629 radeon_emit(cs
, shader
->rsrc2
);
2634 radv_pipeline_generate_vertex_shader(struct radeon_winsys_cs
*cs
,
2635 struct radv_pipeline
*pipeline
)
2637 struct radv_shader_variant
*vs
;
2639 radeon_set_context_reg(cs
, R_028A84_VGT_PRIMITIVEID_EN
, pipeline
->graphics
.vgt_primitiveid_en
);
2641 /* Skip shaders merged into HS/GS */
2642 vs
= pipeline
->shaders
[MESA_SHADER_VERTEX
];
2646 if (vs
->info
.vs
.as_ls
)
2647 radv_pipeline_generate_hw_ls(cs
, pipeline
, vs
);
2648 else if (vs
->info
.vs
.as_es
)
2649 radv_pipeline_generate_hw_es(cs
, pipeline
, vs
);
2651 radv_pipeline_generate_hw_vs(cs
, pipeline
, vs
);
2655 radv_pipeline_generate_tess_shaders(struct radeon_winsys_cs
*cs
,
2656 struct radv_pipeline
*pipeline
)
2658 if (!radv_pipeline_has_tess(pipeline
))
2661 struct radv_shader_variant
*tes
, *tcs
;
2663 tcs
= pipeline
->shaders
[MESA_SHADER_TESS_CTRL
];
2664 tes
= pipeline
->shaders
[MESA_SHADER_TESS_EVAL
];
2667 if (tes
->info
.tes
.as_es
)
2668 radv_pipeline_generate_hw_es(cs
, pipeline
, tes
);
2670 radv_pipeline_generate_hw_vs(cs
, pipeline
, tes
);
2673 radv_pipeline_generate_hw_hs(cs
, pipeline
, tcs
);
2675 radeon_set_context_reg(cs
, R_028B6C_VGT_TF_PARAM
,
2676 pipeline
->graphics
.tess
.tf_param
);
2678 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= CIK
)
2679 radeon_set_context_reg_idx(cs
, R_028B58_VGT_LS_HS_CONFIG
, 2,
2680 pipeline
->graphics
.tess
.ls_hs_config
);
2682 radeon_set_context_reg(cs
, R_028B58_VGT_LS_HS_CONFIG
,
2683 pipeline
->graphics
.tess
.ls_hs_config
);
2685 struct ac_userdata_info
*loc
;
2687 loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_TESS_CTRL
, AC_UD_TCS_OFFCHIP_LAYOUT
);
2688 if (loc
->sgpr_idx
!= -1) {
2689 uint32_t base_reg
= pipeline
->user_data_0
[MESA_SHADER_TESS_CTRL
];
2690 assert(loc
->num_sgprs
== 4);
2691 assert(!loc
->indirect
);
2692 radeon_set_sh_reg_seq(cs
, base_reg
+ loc
->sgpr_idx
* 4, 4);
2693 radeon_emit(cs
, pipeline
->graphics
.tess
.offchip_layout
);
2694 radeon_emit(cs
, pipeline
->graphics
.tess
.tcs_out_offsets
);
2695 radeon_emit(cs
, pipeline
->graphics
.tess
.tcs_out_layout
|
2696 pipeline
->graphics
.tess
.num_tcs_input_cp
<< 26);
2697 radeon_emit(cs
, pipeline
->graphics
.tess
.tcs_in_layout
);
2700 loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_TESS_EVAL
, AC_UD_TES_OFFCHIP_LAYOUT
);
2701 if (loc
->sgpr_idx
!= -1) {
2702 uint32_t base_reg
= pipeline
->user_data_0
[MESA_SHADER_TESS_EVAL
];
2703 assert(loc
->num_sgprs
== 1);
2704 assert(!loc
->indirect
);
2706 radeon_set_sh_reg(cs
, base_reg
+ loc
->sgpr_idx
* 4,
2707 pipeline
->graphics
.tess
.offchip_layout
);
2710 loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_VERTEX
, AC_UD_VS_LS_TCS_IN_LAYOUT
);
2711 if (loc
->sgpr_idx
!= -1) {
2712 uint32_t base_reg
= pipeline
->user_data_0
[MESA_SHADER_VERTEX
];
2713 assert(loc
->num_sgprs
== 1);
2714 assert(!loc
->indirect
);
2716 radeon_set_sh_reg(cs
, base_reg
+ loc
->sgpr_idx
* 4,
2717 pipeline
->graphics
.tess
.tcs_in_layout
);
2722 radv_pipeline_generate_geometry_shader(struct radeon_winsys_cs
*cs
,
2723 struct radv_pipeline
*pipeline
)
2725 struct radv_shader_variant
*gs
;
2728 radeon_set_context_reg(cs
, R_028A40_VGT_GS_MODE
, pipeline
->graphics
.vgt_gs_mode
);
2730 gs
= pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
2734 uint32_t gsvs_itemsize
= gs
->info
.gs
.max_gsvs_emit_size
>> 2;
2736 radeon_set_context_reg_seq(cs
, R_028A60_VGT_GSVS_RING_OFFSET_1
, 3);
2737 radeon_emit(cs
, gsvs_itemsize
);
2738 radeon_emit(cs
, gsvs_itemsize
);
2739 radeon_emit(cs
, gsvs_itemsize
);
2741 radeon_set_context_reg(cs
, R_028AB0_VGT_GSVS_RING_ITEMSIZE
, gsvs_itemsize
);
2743 radeon_set_context_reg(cs
, R_028B38_VGT_GS_MAX_VERT_OUT
, gs
->info
.gs
.vertices_out
);
2745 uint32_t gs_vert_itemsize
= gs
->info
.gs
.gsvs_vertex_size
;
2746 radeon_set_context_reg_seq(cs
, R_028B5C_VGT_GS_VERT_ITEMSIZE
, 4);
2747 radeon_emit(cs
, gs_vert_itemsize
>> 2);
2752 uint32_t gs_num_invocations
= gs
->info
.gs
.invocations
;
2753 radeon_set_context_reg(cs
, R_028B90_VGT_GS_INSTANCE_CNT
,
2754 S_028B90_CNT(MIN2(gs_num_invocations
, 127)) |
2755 S_028B90_ENABLE(gs_num_invocations
> 0));
2757 radeon_set_context_reg(cs
, R_028AAC_VGT_ESGS_RING_ITEMSIZE
,
2758 pipeline
->graphics
.gs
.vgt_esgs_ring_itemsize
);
2760 va
= radv_buffer_get_va(gs
->bo
) + gs
->bo_offset
;
2762 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
2763 radeon_set_sh_reg_seq(cs
, R_00B210_SPI_SHADER_PGM_LO_ES
, 2);
2764 radeon_emit(cs
, va
>> 8);
2765 radeon_emit(cs
, va
>> 40);
2767 radeon_set_sh_reg_seq(cs
, R_00B228_SPI_SHADER_PGM_RSRC1_GS
, 2);
2768 radeon_emit(cs
, gs
->rsrc1
);
2769 radeon_emit(cs
, gs
->rsrc2
|
2770 S_00B22C_LDS_SIZE(pipeline
->graphics
.gs
.lds_size
));
2772 radeon_set_context_reg(cs
, R_028A44_VGT_GS_ONCHIP_CNTL
, pipeline
->graphics
.gs
.vgt_gs_onchip_cntl
);
2773 radeon_set_context_reg(cs
, R_028A94_VGT_GS_MAX_PRIMS_PER_SUBGROUP
, pipeline
->graphics
.gs
.vgt_gs_max_prims_per_subgroup
);
2775 radeon_set_sh_reg_seq(cs
, R_00B220_SPI_SHADER_PGM_LO_GS
, 4);
2776 radeon_emit(cs
, va
>> 8);
2777 radeon_emit(cs
, va
>> 40);
2778 radeon_emit(cs
, gs
->rsrc1
);
2779 radeon_emit(cs
, gs
->rsrc2
);
2782 radv_pipeline_generate_hw_vs(cs
, pipeline
, pipeline
->gs_copy_shader
);
2784 struct ac_userdata_info
*loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_GEOMETRY
,
2785 AC_UD_GS_VS_RING_STRIDE_ENTRIES
);
2786 if (loc
->sgpr_idx
!= -1) {
2787 uint32_t stride
= gs
->info
.gs
.max_gsvs_emit_size
;
2788 uint32_t num_entries
= 64;
2789 bool is_vi
= pipeline
->device
->physical_device
->rad_info
.chip_class
>= VI
;
2792 num_entries
*= stride
;
2794 stride
= S_008F04_STRIDE(stride
);
2795 radeon_set_sh_reg_seq(cs
, R_00B230_SPI_SHADER_USER_DATA_GS_0
+ loc
->sgpr_idx
* 4, 2);
2796 radeon_emit(cs
, stride
);
2797 radeon_emit(cs
, num_entries
);
2803 radv_pipeline_generate_fragment_shader(struct radeon_winsys_cs
*cs
,
2804 struct radv_pipeline
*pipeline
)
2806 struct radv_shader_variant
*ps
;
2808 struct radv_blend_state
*blend
= &pipeline
->graphics
.blend
;
2809 assert (pipeline
->shaders
[MESA_SHADER_FRAGMENT
]);
2811 ps
= pipeline
->shaders
[MESA_SHADER_FRAGMENT
];
2812 va
= radv_buffer_get_va(ps
->bo
) + ps
->bo_offset
;
2814 radeon_set_sh_reg_seq(cs
, R_00B020_SPI_SHADER_PGM_LO_PS
, 4);
2815 radeon_emit(cs
, va
>> 8);
2816 radeon_emit(cs
, va
>> 40);
2817 radeon_emit(cs
, ps
->rsrc1
);
2818 radeon_emit(cs
, ps
->rsrc2
);
2820 radeon_set_context_reg(cs
, R_02880C_DB_SHADER_CONTROL
,
2821 pipeline
->graphics
.db_shader_control
);
2823 radeon_set_context_reg(cs
, R_0286CC_SPI_PS_INPUT_ENA
,
2824 ps
->config
.spi_ps_input_ena
);
2826 radeon_set_context_reg(cs
, R_0286D0_SPI_PS_INPUT_ADDR
,
2827 ps
->config
.spi_ps_input_addr
);
2829 radeon_set_context_reg(cs
, R_0286D8_SPI_PS_IN_CONTROL
,
2830 S_0286D8_NUM_INTERP(ps
->info
.fs
.num_interp
));
2832 radeon_set_context_reg(cs
, R_0286E0_SPI_BARYC_CNTL
, pipeline
->graphics
.spi_baryc_cntl
);
2834 radeon_set_context_reg(cs
, R_028710_SPI_SHADER_Z_FORMAT
,
2835 pipeline
->graphics
.shader_z_format
);
2837 radeon_set_context_reg(cs
, R_028714_SPI_SHADER_COL_FORMAT
, blend
->spi_shader_col_format
);
2839 radeon_set_context_reg(cs
, R_028238_CB_TARGET_MASK
, blend
->cb_target_mask
);
2840 radeon_set_context_reg(cs
, R_02823C_CB_SHADER_MASK
, blend
->cb_shader_mask
);
2842 if (pipeline
->device
->dfsm_allowed
) {
2843 /* optimise this? */
2844 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
2845 radeon_emit(cs
, EVENT_TYPE(V_028A90_FLUSH_DFSM
) | EVENT_INDEX(0));
2848 if (pipeline
->graphics
.ps_input_cntl_num
) {
2849 radeon_set_context_reg_seq(cs
, R_028644_SPI_PS_INPUT_CNTL_0
, pipeline
->graphics
.ps_input_cntl_num
);
2850 for (unsigned i
= 0; i
< pipeline
->graphics
.ps_input_cntl_num
; i
++) {
2851 radeon_emit(cs
, pipeline
->graphics
.ps_input_cntl
[i
]);
2857 radv_pipeline_generate_vgt_vertex_reuse(struct radeon_winsys_cs
*cs
,
2858 struct radv_pipeline
*pipeline
)
2860 if (pipeline
->device
->physical_device
->rad_info
.family
< CHIP_POLARIS10
)
2863 radeon_set_context_reg(cs
, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL
,
2864 pipeline
->graphics
.vtx_reuse_depth
);
2868 radv_pipeline_generate_pm4(struct radv_pipeline
*pipeline
,
2869 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
2871 pipeline
->cs
.buf
= malloc(4 * 256);
2872 pipeline
->cs
.max_dw
= 256;
2874 radv_pipeline_generate_depth_stencil_state(&pipeline
->cs
, pipeline
);
2875 radv_pipeline_generate_blend_state(&pipeline
->cs
, pipeline
);
2876 radv_pipeline_generate_raster_state(&pipeline
->cs
, pipeline
);
2877 radv_pipeline_generate_multisample_state(&pipeline
->cs
, pipeline
);
2878 radv_pipeline_generate_vertex_shader(&pipeline
->cs
, pipeline
);
2879 radv_pipeline_generate_tess_shaders(&pipeline
->cs
, pipeline
);
2880 radv_pipeline_generate_geometry_shader(&pipeline
->cs
, pipeline
);
2881 radv_pipeline_generate_fragment_shader(&pipeline
->cs
, pipeline
);
2882 radv_pipeline_generate_vgt_vertex_reuse(&pipeline
->cs
, pipeline
);
2883 radv_pipeline_generate_binning_state(&pipeline
->cs
, pipeline
, pCreateInfo
);
2885 radeon_set_context_reg(&pipeline
->cs
, R_0286E8_SPI_TMPRING_SIZE
,
2886 S_0286E8_WAVES(pipeline
->max_waves
) |
2887 S_0286E8_WAVESIZE(pipeline
->scratch_bytes_per_wave
>> 10));
2889 radeon_set_context_reg(&pipeline
->cs
, R_028B54_VGT_SHADER_STAGES_EN
, pipeline
->graphics
.vgt_shader_stages_en
);
2891 if (pipeline
->device
->physical_device
->rad_info
.chip_class
>= CIK
) {
2892 radeon_set_uconfig_reg_idx(&pipeline
->cs
, R_030908_VGT_PRIMITIVE_TYPE
, 1, pipeline
->graphics
.prim
);
2894 radeon_set_config_reg(&pipeline
->cs
, R_008958_VGT_PRIMITIVE_TYPE
, pipeline
->graphics
.prim
);
2896 radeon_set_context_reg(&pipeline
->cs
, R_028A6C_VGT_GS_OUT_PRIM_TYPE
, pipeline
->graphics
.gs_out
);
2898 radeon_set_context_reg(&pipeline
->cs
, R_02820C_PA_SC_CLIPRECT_RULE
, pipeline
->graphics
.pa_sc_cliprect_rule
);
2900 assert(pipeline
->cs
.cdw
<= pipeline
->cs
.max_dw
);
2904 radv_pipeline_init(struct radv_pipeline
*pipeline
,
2905 struct radv_device
*device
,
2906 struct radv_pipeline_cache
*cache
,
2907 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
2908 const struct radv_graphics_pipeline_create_info
*extra
,
2909 const VkAllocationCallbacks
*alloc
)
2912 bool has_view_index
= false;
2914 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
2915 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
2916 if (subpass
->view_mask
)
2917 has_view_index
= true;
2919 alloc
= &device
->alloc
;
2921 pipeline
->device
= device
;
2922 pipeline
->layout
= radv_pipeline_layout_from_handle(pCreateInfo
->layout
);
2923 assert(pipeline
->layout
);
2925 radv_pipeline_init_blend_state(pipeline
, pCreateInfo
, extra
);
2927 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = { 0, };
2928 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
2929 gl_shader_stage stage
= ffs(pCreateInfo
->pStages
[i
].stage
) - 1;
2930 pStages
[stage
] = &pCreateInfo
->pStages
[i
];
2933 radv_create_shaders(pipeline
, device
, cache
,
2934 radv_generate_graphics_pipeline_key(pipeline
, pCreateInfo
, has_view_index
),
2937 pipeline
->graphics
.spi_baryc_cntl
= S_0286E0_FRONT_FACE_ALL_BITS(1);
2938 radv_pipeline_init_depth_stencil_state(pipeline
, pCreateInfo
, extra
);
2939 radv_pipeline_init_raster_state(pipeline
, pCreateInfo
);
2940 radv_pipeline_init_multisample_state(pipeline
, pCreateInfo
);
2941 pipeline
->graphics
.prim
= si_translate_prim(pCreateInfo
->pInputAssemblyState
->topology
);
2942 pipeline
->graphics
.can_use_guardband
= radv_prim_can_use_guardband(pCreateInfo
->pInputAssemblyState
->topology
);
2944 if (radv_pipeline_has_gs(pipeline
)) {
2945 pipeline
->graphics
.gs_out
= si_conv_gl_prim_to_gs_out(pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
.gs
.output_prim
);
2946 pipeline
->graphics
.can_use_guardband
= pipeline
->graphics
.gs_out
== V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
2948 pipeline
->graphics
.gs_out
= si_conv_prim_to_gs_out(pCreateInfo
->pInputAssemblyState
->topology
);
2950 if (extra
&& extra
->use_rectlist
) {
2951 pipeline
->graphics
.prim
= V_008958_DI_PT_RECTLIST
;
2952 pipeline
->graphics
.gs_out
= V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
2953 pipeline
->graphics
.can_use_guardband
= true;
2955 pipeline
->graphics
.prim_restart_enable
= !!pCreateInfo
->pInputAssemblyState
->primitiveRestartEnable
;
2956 /* prim vertex count will need TESS changes */
2957 pipeline
->graphics
.prim_vertex_count
= prim_size_table
[pipeline
->graphics
.prim
];
2959 radv_pipeline_init_dynamic_state(pipeline
, pCreateInfo
);
2961 /* Ensure that some export memory is always allocated, for two reasons:
2963 * 1) Correctness: The hardware ignores the EXEC mask if no export
2964 * memory is allocated, so KILL and alpha test do not work correctly
2966 * 2) Performance: Every shader needs at least a NULL export, even when
2967 * it writes no color/depth output. The NULL export instruction
2968 * stalls without this setting.
2970 * Don't add this to CB_SHADER_MASK.
2972 struct radv_shader_variant
*ps
= pipeline
->shaders
[MESA_SHADER_FRAGMENT
];
2973 if (!pipeline
->graphics
.blend
.spi_shader_col_format
) {
2974 if (!ps
->info
.fs
.writes_z
&&
2975 !ps
->info
.fs
.writes_stencil
&&
2976 !ps
->info
.fs
.writes_sample_mask
)
2977 pipeline
->graphics
.blend
.spi_shader_col_format
= V_028714_SPI_SHADER_32_R
;
2981 pipeline
->graphics
.db_shader_control
= 0;
2982 if (ps
->info
.fs
.early_fragment_test
|| !ps
->info
.info
.ps
.writes_memory
)
2983 z_order
= V_02880C_EARLY_Z_THEN_LATE_Z
;
2985 z_order
= V_02880C_LATE_Z
;
2987 pipeline
->graphics
.db_shader_control
=
2988 S_02880C_Z_EXPORT_ENABLE(ps
->info
.fs
.writes_z
) |
2989 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps
->info
.fs
.writes_stencil
) |
2990 S_02880C_KILL_ENABLE(!!ps
->info
.fs
.can_discard
) |
2991 S_02880C_MASK_EXPORT_ENABLE(ps
->info
.fs
.writes_sample_mask
) |
2992 S_02880C_Z_ORDER(z_order
) |
2993 S_02880C_DEPTH_BEFORE_SHADER(ps
->info
.fs
.early_fragment_test
) |
2994 S_02880C_EXEC_ON_HIER_FAIL(ps
->info
.info
.ps
.writes_memory
) |
2995 S_02880C_EXEC_ON_NOOP(ps
->info
.info
.ps
.writes_memory
);
2997 if (pipeline
->device
->physical_device
->has_rbplus
)
2998 pipeline
->graphics
.db_shader_control
|= S_02880C_DUAL_QUAD_DISABLE(1);
3000 unsigned shader_z_format
=
3001 ac_get_spi_shader_z_format(ps
->info
.fs
.writes_z
,
3002 ps
->info
.fs
.writes_stencil
,
3003 ps
->info
.fs
.writes_sample_mask
);
3004 pipeline
->graphics
.shader_z_format
= shader_z_format
;
3006 calculate_vgt_gs_mode(pipeline
);
3007 calculate_ps_inputs(pipeline
);
3009 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
3010 if (pipeline
->shaders
[i
]) {
3011 pipeline
->need_indirect_descriptor_sets
|= pipeline
->shaders
[i
]->info
.need_indirect_descriptor_sets
;
3015 uint32_t stages
= 0;
3016 if (radv_pipeline_has_tess(pipeline
)) {
3017 stages
|= S_028B54_LS_EN(V_028B54_LS_STAGE_ON
) |
3018 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
3020 if (radv_pipeline_has_gs(pipeline
))
3021 stages
|= S_028B54_ES_EN(V_028B54_ES_STAGE_DS
) |
3023 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER
);
3025 stages
|= S_028B54_VS_EN(V_028B54_VS_STAGE_DS
);
3027 } else if (radv_pipeline_has_gs(pipeline
))
3028 stages
|= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL
) |
3030 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER
);
3032 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
3033 stages
|= S_028B54_MAX_PRIMGRP_IN_WAVE(2);
3035 pipeline
->graphics
.vgt_shader_stages_en
= stages
;
3037 if (radv_pipeline_has_gs(pipeline
)) {
3038 calculate_gs_ring_sizes(pipeline
);
3039 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
3040 calculate_gfx9_gs_info(pCreateInfo
, pipeline
);
3043 if (radv_pipeline_has_tess(pipeline
)) {
3044 if (pipeline
->graphics
.prim
== V_008958_DI_PT_PATCH
) {
3045 pipeline
->graphics
.prim_vertex_count
.min
= pCreateInfo
->pTessellationState
->patchControlPoints
;
3046 pipeline
->graphics
.prim_vertex_count
.incr
= 1;
3048 calculate_tess_state(pipeline
, pCreateInfo
);
3051 if (radv_pipeline_has_tess(pipeline
))
3052 pipeline
->graphics
.primgroup_size
= pipeline
->graphics
.tess
.num_patches
;
3053 else if (radv_pipeline_has_gs(pipeline
))
3054 pipeline
->graphics
.primgroup_size
= 64;
3056 pipeline
->graphics
.primgroup_size
= 128; /* recommended without a GS */
3058 pipeline
->graphics
.partial_es_wave
= false;
3059 if (pipeline
->device
->has_distributed_tess
) {
3060 if (radv_pipeline_has_gs(pipeline
)) {
3061 if (device
->physical_device
->rad_info
.chip_class
<= VI
)
3062 pipeline
->graphics
.partial_es_wave
= true;
3065 /* GS requirement. */
3066 if (SI_GS_PER_ES
/ pipeline
->graphics
.primgroup_size
>= pipeline
->device
->gs_table_depth
- 3)
3067 pipeline
->graphics
.partial_es_wave
= true;
3069 pipeline
->graphics
.wd_switch_on_eop
= false;
3070 if (device
->physical_device
->rad_info
.chip_class
>= CIK
) {
3071 unsigned prim
= pipeline
->graphics
.prim
;
3072 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
3073 * 4 shader engines. Set 1 to pass the assertion below.
3074 * The other cases are hardware requirements. */
3075 if (device
->physical_device
->rad_info
.max_se
< 4 ||
3076 prim
== V_008958_DI_PT_POLYGON
||
3077 prim
== V_008958_DI_PT_LINELOOP
||
3078 prim
== V_008958_DI_PT_TRIFAN
||
3079 prim
== V_008958_DI_PT_TRISTRIP_ADJ
||
3080 (pipeline
->graphics
.prim_restart_enable
&&
3081 (device
->physical_device
->rad_info
.family
< CHIP_POLARIS10
||
3082 (prim
!= V_008958_DI_PT_POINTLIST
&&
3083 prim
!= V_008958_DI_PT_LINESTRIP
&&
3084 prim
!= V_008958_DI_PT_TRISTRIP
))))
3085 pipeline
->graphics
.wd_switch_on_eop
= true;
3088 pipeline
->graphics
.ia_switch_on_eoi
= false;
3089 if (pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.fs
.prim_id_input
)
3090 pipeline
->graphics
.ia_switch_on_eoi
= true;
3091 if (radv_pipeline_has_gs(pipeline
) &&
3092 pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
.info
.uses_prim_id
)
3093 pipeline
->graphics
.ia_switch_on_eoi
= true;
3094 if (radv_pipeline_has_tess(pipeline
)) {
3095 /* SWITCH_ON_EOI must be set if PrimID is used. */
3096 if (pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]->info
.info
.uses_prim_id
||
3097 radv_get_tess_eval_shader(pipeline
)->info
.info
.uses_prim_id
)
3098 pipeline
->graphics
.ia_switch_on_eoi
= true;
3101 pipeline
->graphics
.partial_vs_wave
= false;
3102 if (radv_pipeline_has_tess(pipeline
)) {
3103 /* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
3104 if ((device
->physical_device
->rad_info
.family
== CHIP_TAHITI
||
3105 device
->physical_device
->rad_info
.family
== CHIP_PITCAIRN
||
3106 device
->physical_device
->rad_info
.family
== CHIP_BONAIRE
) &&
3107 radv_pipeline_has_gs(pipeline
))
3108 pipeline
->graphics
.partial_vs_wave
= true;
3109 /* Needed for 028B6C_DISTRIBUTION_MODE != 0 */
3110 if (device
->has_distributed_tess
) {
3111 if (radv_pipeline_has_gs(pipeline
)) {
3112 if (device
->physical_device
->rad_info
.family
== CHIP_TONGA
||
3113 device
->physical_device
->rad_info
.family
== CHIP_FIJI
||
3114 device
->physical_device
->rad_info
.family
== CHIP_POLARIS10
||
3115 device
->physical_device
->rad_info
.family
== CHIP_POLARIS11
||
3116 device
->physical_device
->rad_info
.family
== CHIP_POLARIS12
)
3117 pipeline
->graphics
.partial_vs_wave
= true;
3119 pipeline
->graphics
.partial_vs_wave
= true;
3124 pipeline
->graphics
.base_ia_multi_vgt_param
=
3125 S_028AA8_PRIMGROUP_SIZE(pipeline
->graphics
.primgroup_size
- 1) |
3126 /* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */
3127 S_028AA8_MAX_PRIMGRP_IN_WAVE(device
->physical_device
->rad_info
.chip_class
== VI
? 2 : 0) |
3128 S_030960_EN_INST_OPT_BASIC(device
->physical_device
->rad_info
.chip_class
>= GFX9
) |
3129 S_030960_EN_INST_OPT_ADV(device
->physical_device
->rad_info
.chip_class
>= GFX9
);
3131 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
3132 pCreateInfo
->pVertexInputState
;
3133 struct radv_vertex_elements_info
*velems
= &pipeline
->vertex_elements
;
3135 for (uint32_t i
= 0; i
< vi_info
->vertexAttributeDescriptionCount
; i
++) {
3136 const VkVertexInputAttributeDescription
*desc
=
3137 &vi_info
->pVertexAttributeDescriptions
[i
];
3138 unsigned loc
= desc
->location
;
3139 const struct vk_format_description
*format_desc
;
3141 uint32_t num_format
, data_format
;
3142 format_desc
= vk_format_description(desc
->format
);
3143 first_non_void
= vk_format_get_first_non_void_channel(desc
->format
);
3145 num_format
= radv_translate_buffer_numformat(format_desc
, first_non_void
);
3146 data_format
= radv_translate_buffer_dataformat(format_desc
, first_non_void
);
3148 velems
->rsrc_word3
[loc
] = S_008F0C_DST_SEL_X(si_map_swizzle(format_desc
->swizzle
[0])) |
3149 S_008F0C_DST_SEL_Y(si_map_swizzle(format_desc
->swizzle
[1])) |
3150 S_008F0C_DST_SEL_Z(si_map_swizzle(format_desc
->swizzle
[2])) |
3151 S_008F0C_DST_SEL_W(si_map_swizzle(format_desc
->swizzle
[3])) |
3152 S_008F0C_NUM_FORMAT(num_format
) |
3153 S_008F0C_DATA_FORMAT(data_format
);
3154 velems
->format_size
[loc
] = format_desc
->block
.bits
/ 8;
3155 velems
->offset
[loc
] = desc
->offset
;
3156 velems
->binding
[loc
] = desc
->binding
;
3157 velems
->count
= MAX2(velems
->count
, loc
+ 1);
3160 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
3161 const VkVertexInputBindingDescription
*desc
=
3162 &vi_info
->pVertexBindingDescriptions
[i
];
3164 pipeline
->binding_stride
[desc
->binding
] = desc
->stride
;
3167 for (uint32_t i
= 0; i
< MESA_SHADER_STAGES
; i
++)
3168 pipeline
->user_data_0
[i
] = radv_pipeline_stage_to_user_data_0(pipeline
, i
, device
->physical_device
->rad_info
.chip_class
);
3170 struct ac_userdata_info
*loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_VERTEX
,
3171 AC_UD_VS_BASE_VERTEX_START_INSTANCE
);
3172 if (loc
->sgpr_idx
!= -1) {
3173 pipeline
->graphics
.vtx_base_sgpr
= pipeline
->user_data_0
[MESA_SHADER_VERTEX
];
3174 pipeline
->graphics
.vtx_base_sgpr
+= loc
->sgpr_idx
* 4;
3175 if (radv_get_vertex_shader(pipeline
)->info
.info
.vs
.needs_draw_id
)
3176 pipeline
->graphics
.vtx_emit_num
= 3;
3178 pipeline
->graphics
.vtx_emit_num
= 2;
3181 pipeline
->graphics
.vtx_reuse_depth
= 30;
3182 if (radv_pipeline_has_tess(pipeline
) &&
3183 radv_get_tess_eval_shader(pipeline
)->info
.tes
.spacing
== TESS_SPACING_FRACTIONAL_ODD
) {
3184 pipeline
->graphics
.vtx_reuse_depth
= 14;
3187 if (device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
) {
3188 radv_dump_pipeline_stats(device
, pipeline
);
3191 result
= radv_pipeline_scratch_init(device
, pipeline
);
3192 radv_pipeline_generate_pm4(pipeline
, pCreateInfo
);
3198 radv_graphics_pipeline_create(
3200 VkPipelineCache _cache
,
3201 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
3202 const struct radv_graphics_pipeline_create_info
*extra
,
3203 const VkAllocationCallbacks
*pAllocator
,
3204 VkPipeline
*pPipeline
)
3206 RADV_FROM_HANDLE(radv_device
, device
, _device
);
3207 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
3208 struct radv_pipeline
*pipeline
;
3211 pipeline
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
3212 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3213 if (pipeline
== NULL
)
3214 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3216 result
= radv_pipeline_init(pipeline
, device
, cache
,
3217 pCreateInfo
, extra
, pAllocator
);
3218 if (result
!= VK_SUCCESS
) {
3219 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
3223 *pPipeline
= radv_pipeline_to_handle(pipeline
);
3228 VkResult
radv_CreateGraphicsPipelines(
3230 VkPipelineCache pipelineCache
,
3232 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
3233 const VkAllocationCallbacks
* pAllocator
,
3234 VkPipeline
* pPipelines
)
3236 VkResult result
= VK_SUCCESS
;
3239 for (; i
< count
; i
++) {
3241 r
= radv_graphics_pipeline_create(_device
,
3244 NULL
, pAllocator
, &pPipelines
[i
]);
3245 if (r
!= VK_SUCCESS
) {
3247 pPipelines
[i
] = VK_NULL_HANDLE
;
3256 radv_compute_generate_pm4(struct radv_pipeline
*pipeline
)
3258 struct radv_shader_variant
*compute_shader
;
3259 struct radv_device
*device
= pipeline
->device
;
3260 unsigned compute_resource_limits
;
3261 unsigned waves_per_threadgroup
;
3264 pipeline
->cs
.buf
= malloc(20 * 4);
3265 pipeline
->cs
.max_dw
= 20;
3267 compute_shader
= pipeline
->shaders
[MESA_SHADER_COMPUTE
];
3268 va
= radv_buffer_get_va(compute_shader
->bo
) + compute_shader
->bo_offset
;
3270 radeon_set_sh_reg_seq(&pipeline
->cs
, R_00B830_COMPUTE_PGM_LO
, 2);
3271 radeon_emit(&pipeline
->cs
, va
>> 8);
3272 radeon_emit(&pipeline
->cs
, va
>> 40);
3274 radeon_set_sh_reg_seq(&pipeline
->cs
, R_00B848_COMPUTE_PGM_RSRC1
, 2);
3275 radeon_emit(&pipeline
->cs
, compute_shader
->rsrc1
);
3276 radeon_emit(&pipeline
->cs
, compute_shader
->rsrc2
);
3278 radeon_set_sh_reg(&pipeline
->cs
, R_00B860_COMPUTE_TMPRING_SIZE
,
3279 S_00B860_WAVES(pipeline
->max_waves
) |
3280 S_00B860_WAVESIZE(pipeline
->scratch_bytes_per_wave
>> 10));
3282 /* Calculate best compute resource limits. */
3283 waves_per_threadgroup
=
3284 DIV_ROUND_UP(compute_shader
->info
.cs
.block_size
[0] *
3285 compute_shader
->info
.cs
.block_size
[1] *
3286 compute_shader
->info
.cs
.block_size
[2], 64);
3287 compute_resource_limits
=
3288 S_00B854_SIMD_DEST_CNTL(waves_per_threadgroup
% 4 == 0);
3290 if (device
->physical_device
->rad_info
.chip_class
>= CIK
) {
3291 unsigned num_cu_per_se
=
3292 device
->physical_device
->rad_info
.num_good_compute_units
/
3293 device
->physical_device
->rad_info
.max_se
;
3295 /* Force even distribution on all SIMDs in CU if the workgroup
3296 * size is 64. This has shown some good improvements if # of
3297 * CUs per SE is not a multiple of 4.
3299 if (num_cu_per_se
% 4 && waves_per_threadgroup
== 1)
3300 compute_resource_limits
|= S_00B854_FORCE_SIMD_DIST(1);
3303 radeon_set_sh_reg(&pipeline
->cs
, R_00B854_COMPUTE_RESOURCE_LIMITS
,
3304 compute_resource_limits
);
3306 radeon_set_sh_reg_seq(&pipeline
->cs
, R_00B81C_COMPUTE_NUM_THREAD_X
, 3);
3307 radeon_emit(&pipeline
->cs
,
3308 S_00B81C_NUM_THREAD_FULL(compute_shader
->info
.cs
.block_size
[0]));
3309 radeon_emit(&pipeline
->cs
,
3310 S_00B81C_NUM_THREAD_FULL(compute_shader
->info
.cs
.block_size
[1]));
3311 radeon_emit(&pipeline
->cs
,
3312 S_00B81C_NUM_THREAD_FULL(compute_shader
->info
.cs
.block_size
[2]));
3314 assert(pipeline
->cs
.cdw
<= pipeline
->cs
.max_dw
);
3317 static VkResult
radv_compute_pipeline_create(
3319 VkPipelineCache _cache
,
3320 const VkComputePipelineCreateInfo
* pCreateInfo
,
3321 const VkAllocationCallbacks
* pAllocator
,
3322 VkPipeline
* pPipeline
)
3324 RADV_FROM_HANDLE(radv_device
, device
, _device
);
3325 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
3326 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = { 0, };
3327 struct radv_pipeline
*pipeline
;
3330 pipeline
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
3331 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3332 if (pipeline
== NULL
)
3333 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3335 pipeline
->device
= device
;
3336 pipeline
->layout
= radv_pipeline_layout_from_handle(pCreateInfo
->layout
);
3337 assert(pipeline
->layout
);
3339 pStages
[MESA_SHADER_COMPUTE
] = &pCreateInfo
->stage
;
3340 radv_create_shaders(pipeline
, device
, cache
, (struct radv_pipeline_key
) {0}, pStages
);
3342 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
);
3343 pipeline
->need_indirect_descriptor_sets
|= pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.need_indirect_descriptor_sets
;
3344 result
= radv_pipeline_scratch_init(device
, pipeline
);
3345 if (result
!= VK_SUCCESS
) {
3346 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
3350 radv_compute_generate_pm4(pipeline
);
3352 *pPipeline
= radv_pipeline_to_handle(pipeline
);
3354 if (device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
) {
3355 radv_dump_pipeline_stats(device
, pipeline
);
3360 VkResult
radv_CreateComputePipelines(
3362 VkPipelineCache pipelineCache
,
3364 const VkComputePipelineCreateInfo
* pCreateInfos
,
3365 const VkAllocationCallbacks
* pAllocator
,
3366 VkPipeline
* pPipelines
)
3368 VkResult result
= VK_SUCCESS
;
3371 for (; i
< count
; i
++) {
3373 r
= radv_compute_pipeline_create(_device
, pipelineCache
,
3375 pAllocator
, &pPipelines
[i
]);
3376 if (r
!= VK_SUCCESS
) {
3378 pPipelines
[i
] = VK_NULL_HANDLE
;