radv: align the LDS size in calculate_tess_lds_size()
[mesa.git] / src / amd / vulkan / radv_pipeline.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
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:
14 *
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
17 * Software.
18 *
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
25 * IN THE SOFTWARE.
26 */
27
28 #include "util/disk_cache.h"
29 #include "util/mesa-sha1.h"
30 #include "util/u_atomic.h"
31 #include "radv_debug.h"
32 #include "radv_private.h"
33 #include "radv_cs.h"
34 #include "radv_shader.h"
35 #include "nir/nir.h"
36 #include "nir/nir_builder.h"
37 #include "nir/nir_xfb_info.h"
38 #include "spirv/nir_spirv.h"
39 #include "vk_util.h"
40
41 #include "sid.h"
42 #include "ac_binary.h"
43 #include "ac_llvm_util.h"
44 #include "ac_nir_to_llvm.h"
45 #include "vk_format.h"
46 #include "util/debug.h"
47 #include "ac_exp_param.h"
48 #include "ac_shader_util.h"
49
50 struct radv_blend_state {
51 uint32_t blend_enable_4bit;
52 uint32_t need_src_alpha;
53
54 uint32_t cb_color_control;
55 uint32_t cb_target_mask;
56 uint32_t cb_target_enabled_4bit;
57 uint32_t sx_mrt_blend_opt[8];
58 uint32_t cb_blend_control[8];
59
60 uint32_t spi_shader_col_format;
61 uint32_t col_format_is_int8;
62 uint32_t col_format_is_int10;
63 uint32_t cb_shader_mask;
64 uint32_t db_alpha_to_mask;
65
66 uint32_t commutative_4bit;
67
68 bool single_cb_enable;
69 bool mrt0_is_dual_src;
70 };
71
72 struct radv_dsa_order_invariance {
73 /* Whether the final result in Z/S buffers is guaranteed to be
74 * invariant under changes to the order in which fragments arrive.
75 */
76 bool zs;
77
78 /* Whether the set of fragments that pass the combined Z/S test is
79 * guaranteed to be invariant under changes to the order in which
80 * fragments arrive.
81 */
82 bool pass_set;
83 };
84
85 struct radv_tessellation_state {
86 uint32_t ls_hs_config;
87 unsigned num_lds_blocks;
88 uint32_t tf_param;
89 };
90
91 static const VkPipelineMultisampleStateCreateInfo *
92 radv_pipeline_get_multisample_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
93 {
94 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable)
95 return pCreateInfo->pMultisampleState;
96 return NULL;
97 }
98
99 static const VkPipelineTessellationStateCreateInfo *
100 radv_pipeline_get_tessellation_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
101 {
102 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
103 if (pCreateInfo->pStages[i].stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
104 pCreateInfo->pStages[i].stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
105 return pCreateInfo->pTessellationState;
106 }
107 }
108 return NULL;
109 }
110
111 static const VkPipelineDepthStencilStateCreateInfo *
112 radv_pipeline_get_depth_stencil_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
113 {
114 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
115 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
116
117 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
118 subpass->depth_stencil_attachment)
119 return pCreateInfo->pDepthStencilState;
120 return NULL;
121 }
122
123 static const VkPipelineColorBlendStateCreateInfo *
124 radv_pipeline_get_color_blend_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
125 {
126 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
127 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
128
129 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
130 subpass->has_color_att)
131 return pCreateInfo->pColorBlendState;
132 return NULL;
133 }
134
135 bool radv_pipeline_has_ngg(const struct radv_pipeline *pipeline)
136 {
137 struct radv_shader_variant *variant = NULL;
138 if (pipeline->shaders[MESA_SHADER_GEOMETRY])
139 variant = pipeline->shaders[MESA_SHADER_GEOMETRY];
140 else if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
141 variant = pipeline->shaders[MESA_SHADER_TESS_EVAL];
142 else if (pipeline->shaders[MESA_SHADER_VERTEX])
143 variant = pipeline->shaders[MESA_SHADER_VERTEX];
144 else
145 return false;
146 return variant->info.is_ngg;
147 }
148
149 bool radv_pipeline_has_ngg_passthrough(const struct radv_pipeline *pipeline)
150 {
151 assert(radv_pipeline_has_ngg(pipeline));
152
153 struct radv_shader_variant *variant = NULL;
154 if (pipeline->shaders[MESA_SHADER_GEOMETRY])
155 variant = pipeline->shaders[MESA_SHADER_GEOMETRY];
156 else if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
157 variant = pipeline->shaders[MESA_SHADER_TESS_EVAL];
158 else if (pipeline->shaders[MESA_SHADER_VERTEX])
159 variant = pipeline->shaders[MESA_SHADER_VERTEX];
160 else
161 return false;
162 return variant->info.is_ngg_passthrough;
163 }
164
165 bool radv_pipeline_has_gs_copy_shader(const struct radv_pipeline *pipeline)
166 {
167 if (!radv_pipeline_has_gs(pipeline))
168 return false;
169
170 /* The GS copy shader is required if the pipeline has GS on GFX6-GFX9.
171 * On GFX10, it might be required in rare cases if it's not possible to
172 * enable NGG.
173 */
174 if (radv_pipeline_has_ngg(pipeline))
175 return false;
176
177 assert(pipeline->gs_copy_shader);
178 return true;
179 }
180
181 static void
182 radv_pipeline_destroy(struct radv_device *device,
183 struct radv_pipeline *pipeline,
184 const VkAllocationCallbacks* allocator)
185 {
186 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i)
187 if (pipeline->shaders[i])
188 radv_shader_variant_destroy(device, pipeline->shaders[i]);
189
190 if (pipeline->gs_copy_shader)
191 radv_shader_variant_destroy(device, pipeline->gs_copy_shader);
192
193 if(pipeline->cs.buf)
194 free(pipeline->cs.buf);
195
196 vk_object_base_finish(&pipeline->base);
197 vk_free2(&device->vk.alloc, allocator, pipeline);
198 }
199
200 void radv_DestroyPipeline(
201 VkDevice _device,
202 VkPipeline _pipeline,
203 const VkAllocationCallbacks* pAllocator)
204 {
205 RADV_FROM_HANDLE(radv_device, device, _device);
206 RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline);
207
208 if (!_pipeline)
209 return;
210
211 radv_pipeline_destroy(device, pipeline, pAllocator);
212 }
213
214 static uint32_t get_hash_flags(struct radv_device *device)
215 {
216 uint32_t hash_flags = 0;
217
218 if (device->instance->debug_flags & RADV_DEBUG_NO_NGG)
219 hash_flags |= RADV_HASH_SHADER_NO_NGG;
220 if (device->physical_device->cs_wave_size == 32)
221 hash_flags |= RADV_HASH_SHADER_CS_WAVE32;
222 if (device->physical_device->ps_wave_size == 32)
223 hash_flags |= RADV_HASH_SHADER_PS_WAVE32;
224 if (device->physical_device->ge_wave_size == 32)
225 hash_flags |= RADV_HASH_SHADER_GE_WAVE32;
226 if (device->physical_device->use_llvm)
227 hash_flags |= RADV_HASH_SHADER_LLVM;
228 return hash_flags;
229 }
230
231 static VkResult
232 radv_pipeline_scratch_init(struct radv_device *device,
233 struct radv_pipeline *pipeline)
234 {
235 unsigned scratch_bytes_per_wave = 0;
236 unsigned max_waves = 0;
237 unsigned min_waves = 1;
238
239 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
240 if (pipeline->shaders[i] &&
241 pipeline->shaders[i]->config.scratch_bytes_per_wave) {
242 unsigned max_stage_waves = device->scratch_waves;
243
244 scratch_bytes_per_wave = MAX2(scratch_bytes_per_wave,
245 pipeline->shaders[i]->config.scratch_bytes_per_wave);
246
247 max_stage_waves = MIN2(max_stage_waves,
248 4 * device->physical_device->rad_info.num_good_compute_units *
249 (256 / pipeline->shaders[i]->config.num_vgprs));
250 max_waves = MAX2(max_waves, max_stage_waves);
251 }
252 }
253
254 if (pipeline->shaders[MESA_SHADER_COMPUTE]) {
255 unsigned group_size = pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[0] *
256 pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[1] *
257 pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[2];
258 min_waves = MAX2(min_waves, round_up_u32(group_size, 64));
259 }
260
261 pipeline->scratch_bytes_per_wave = scratch_bytes_per_wave;
262 pipeline->max_waves = max_waves;
263 return VK_SUCCESS;
264 }
265
266 static uint32_t si_translate_blend_logic_op(VkLogicOp op)
267 {
268 switch (op) {
269 case VK_LOGIC_OP_CLEAR:
270 return V_028808_ROP3_CLEAR;
271 case VK_LOGIC_OP_AND:
272 return V_028808_ROP3_AND;
273 case VK_LOGIC_OP_AND_REVERSE:
274 return V_028808_ROP3_AND_REVERSE;
275 case VK_LOGIC_OP_COPY:
276 return V_028808_ROP3_COPY;
277 case VK_LOGIC_OP_AND_INVERTED:
278 return V_028808_ROP3_AND_INVERTED;
279 case VK_LOGIC_OP_NO_OP:
280 return V_028808_ROP3_NO_OP;
281 case VK_LOGIC_OP_XOR:
282 return V_028808_ROP3_XOR;
283 case VK_LOGIC_OP_OR:
284 return V_028808_ROP3_OR;
285 case VK_LOGIC_OP_NOR:
286 return V_028808_ROP3_NOR;
287 case VK_LOGIC_OP_EQUIVALENT:
288 return V_028808_ROP3_EQUIVALENT;
289 case VK_LOGIC_OP_INVERT:
290 return V_028808_ROP3_INVERT;
291 case VK_LOGIC_OP_OR_REVERSE:
292 return V_028808_ROP3_OR_REVERSE;
293 case VK_LOGIC_OP_COPY_INVERTED:
294 return V_028808_ROP3_COPY_INVERTED;
295 case VK_LOGIC_OP_OR_INVERTED:
296 return V_028808_ROP3_OR_INVERTED;
297 case VK_LOGIC_OP_NAND:
298 return V_028808_ROP3_NAND;
299 case VK_LOGIC_OP_SET:
300 return V_028808_ROP3_SET;
301 default:
302 unreachable("Unhandled logic op");
303 }
304 }
305
306
307 static uint32_t si_translate_blend_function(VkBlendOp op)
308 {
309 switch (op) {
310 case VK_BLEND_OP_ADD:
311 return V_028780_COMB_DST_PLUS_SRC;
312 case VK_BLEND_OP_SUBTRACT:
313 return V_028780_COMB_SRC_MINUS_DST;
314 case VK_BLEND_OP_REVERSE_SUBTRACT:
315 return V_028780_COMB_DST_MINUS_SRC;
316 case VK_BLEND_OP_MIN:
317 return V_028780_COMB_MIN_DST_SRC;
318 case VK_BLEND_OP_MAX:
319 return V_028780_COMB_MAX_DST_SRC;
320 default:
321 return 0;
322 }
323 }
324
325 static uint32_t si_translate_blend_factor(VkBlendFactor factor)
326 {
327 switch (factor) {
328 case VK_BLEND_FACTOR_ZERO:
329 return V_028780_BLEND_ZERO;
330 case VK_BLEND_FACTOR_ONE:
331 return V_028780_BLEND_ONE;
332 case VK_BLEND_FACTOR_SRC_COLOR:
333 return V_028780_BLEND_SRC_COLOR;
334 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:
335 return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
336 case VK_BLEND_FACTOR_DST_COLOR:
337 return V_028780_BLEND_DST_COLOR;
338 case VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR:
339 return V_028780_BLEND_ONE_MINUS_DST_COLOR;
340 case VK_BLEND_FACTOR_SRC_ALPHA:
341 return V_028780_BLEND_SRC_ALPHA;
342 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:
343 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
344 case VK_BLEND_FACTOR_DST_ALPHA:
345 return V_028780_BLEND_DST_ALPHA;
346 case VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA:
347 return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
348 case VK_BLEND_FACTOR_CONSTANT_COLOR:
349 return V_028780_BLEND_CONSTANT_COLOR;
350 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR:
351 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
352 case VK_BLEND_FACTOR_CONSTANT_ALPHA:
353 return V_028780_BLEND_CONSTANT_ALPHA;
354 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA:
355 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
356 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:
357 return V_028780_BLEND_SRC_ALPHA_SATURATE;
358 case VK_BLEND_FACTOR_SRC1_COLOR:
359 return V_028780_BLEND_SRC1_COLOR;
360 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:
361 return V_028780_BLEND_INV_SRC1_COLOR;
362 case VK_BLEND_FACTOR_SRC1_ALPHA:
363 return V_028780_BLEND_SRC1_ALPHA;
364 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:
365 return V_028780_BLEND_INV_SRC1_ALPHA;
366 default:
367 return 0;
368 }
369 }
370
371 static uint32_t si_translate_blend_opt_function(VkBlendOp op)
372 {
373 switch (op) {
374 case VK_BLEND_OP_ADD:
375 return V_028760_OPT_COMB_ADD;
376 case VK_BLEND_OP_SUBTRACT:
377 return V_028760_OPT_COMB_SUBTRACT;
378 case VK_BLEND_OP_REVERSE_SUBTRACT:
379 return V_028760_OPT_COMB_REVSUBTRACT;
380 case VK_BLEND_OP_MIN:
381 return V_028760_OPT_COMB_MIN;
382 case VK_BLEND_OP_MAX:
383 return V_028760_OPT_COMB_MAX;
384 default:
385 return V_028760_OPT_COMB_BLEND_DISABLED;
386 }
387 }
388
389 static uint32_t si_translate_blend_opt_factor(VkBlendFactor factor, bool is_alpha)
390 {
391 switch (factor) {
392 case VK_BLEND_FACTOR_ZERO:
393 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;
394 case VK_BLEND_FACTOR_ONE:
395 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;
396 case VK_BLEND_FACTOR_SRC_COLOR:
397 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
398 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;
399 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:
400 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
401 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;
402 case VK_BLEND_FACTOR_SRC_ALPHA:
403 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;
404 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:
405 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;
406 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:
407 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
408 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
409 default:
410 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
411 }
412 }
413
414 /**
415 * Get rid of DST in the blend factors by commuting the operands:
416 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
417 */
418 static void si_blend_remove_dst(unsigned *func, unsigned *src_factor,
419 unsigned *dst_factor, unsigned expected_dst,
420 unsigned replacement_src)
421 {
422 if (*src_factor == expected_dst &&
423 *dst_factor == VK_BLEND_FACTOR_ZERO) {
424 *src_factor = VK_BLEND_FACTOR_ZERO;
425 *dst_factor = replacement_src;
426
427 /* Commuting the operands requires reversing subtractions. */
428 if (*func == VK_BLEND_OP_SUBTRACT)
429 *func = VK_BLEND_OP_REVERSE_SUBTRACT;
430 else if (*func == VK_BLEND_OP_REVERSE_SUBTRACT)
431 *func = VK_BLEND_OP_SUBTRACT;
432 }
433 }
434
435 static bool si_blend_factor_uses_dst(unsigned factor)
436 {
437 return factor == VK_BLEND_FACTOR_DST_COLOR ||
438 factor == VK_BLEND_FACTOR_DST_ALPHA ||
439 factor == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
440 factor == VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA ||
441 factor == VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;
442 }
443
444 static bool is_dual_src(VkBlendFactor factor)
445 {
446 switch (factor) {
447 case VK_BLEND_FACTOR_SRC1_COLOR:
448 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:
449 case VK_BLEND_FACTOR_SRC1_ALPHA:
450 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:
451 return true;
452 default:
453 return false;
454 }
455 }
456
457 static unsigned radv_choose_spi_color_format(VkFormat vk_format,
458 bool blend_enable,
459 bool blend_need_alpha)
460 {
461 const struct vk_format_description *desc = vk_format_description(vk_format);
462 struct ac_spi_color_formats formats = {};
463 unsigned format, ntype, swap;
464
465 format = radv_translate_colorformat(vk_format);
466 ntype = radv_translate_color_numformat(vk_format, desc,
467 vk_format_get_first_non_void_channel(vk_format));
468 swap = radv_translate_colorswap(vk_format, false);
469
470 ac_choose_spi_color_formats(format, swap, ntype, false, &formats);
471
472 if (blend_enable && blend_need_alpha)
473 return formats.blend_alpha;
474 else if(blend_need_alpha)
475 return formats.alpha;
476 else if(blend_enable)
477 return formats.blend;
478 else
479 return formats.normal;
480 }
481
482 static bool
483 format_is_int8(VkFormat format)
484 {
485 const struct vk_format_description *desc = vk_format_description(format);
486 int channel = vk_format_get_first_non_void_channel(format);
487
488 return channel >= 0 && desc->channel[channel].pure_integer &&
489 desc->channel[channel].size == 8;
490 }
491
492 static bool
493 format_is_int10(VkFormat format)
494 {
495 const struct vk_format_description *desc = vk_format_description(format);
496
497 if (desc->nr_channels != 4)
498 return false;
499 for (unsigned i = 0; i < 4; i++) {
500 if (desc->channel[i].pure_integer && desc->channel[i].size == 10)
501 return true;
502 }
503 return false;
504 }
505
506 static void
507 radv_pipeline_compute_spi_color_formats(struct radv_pipeline *pipeline,
508 const VkGraphicsPipelineCreateInfo *pCreateInfo,
509 struct radv_blend_state *blend)
510 {
511 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
512 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
513 unsigned col_format = 0, is_int8 = 0, is_int10 = 0;
514 unsigned num_targets;
515
516 for (unsigned i = 0; i < (blend->single_cb_enable ? 1 : subpass->color_count); ++i) {
517 unsigned cf;
518
519 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED ||
520 !(blend->cb_target_mask & (0xfu << (i * 4)))) {
521 cf = V_028714_SPI_SHADER_ZERO;
522 } else {
523 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->color_attachments[i].attachment;
524 bool blend_enable =
525 blend->blend_enable_4bit & (0xfu << (i * 4));
526
527 cf = radv_choose_spi_color_format(attachment->format,
528 blend_enable,
529 blend->need_src_alpha & (1 << i));
530
531 if (format_is_int8(attachment->format))
532 is_int8 |= 1 << i;
533 if (format_is_int10(attachment->format))
534 is_int10 |= 1 << i;
535 }
536
537 col_format |= cf << (4 * i);
538 }
539
540 if (!(col_format & 0xf) && blend->need_src_alpha & (1 << 0)) {
541 /* When a subpass doesn't have any color attachments, write the
542 * alpha channel of MRT0 when alpha coverage is enabled because
543 * the depth attachment needs it.
544 */
545 col_format |= V_028714_SPI_SHADER_32_AR;
546 }
547
548 /* If the i-th target format is set, all previous target formats must
549 * be non-zero to avoid hangs.
550 */
551 num_targets = (util_last_bit(col_format) + 3) / 4;
552 for (unsigned i = 0; i < num_targets; i++) {
553 if (!(col_format & (0xf << (i * 4)))) {
554 col_format |= V_028714_SPI_SHADER_32_R << (i * 4);
555 }
556 }
557
558 /* The output for dual source blending should have the same format as
559 * the first output.
560 */
561 if (blend->mrt0_is_dual_src)
562 col_format |= (col_format & 0xf) << 4;
563
564 blend->spi_shader_col_format = col_format;
565 blend->col_format_is_int8 = is_int8;
566 blend->col_format_is_int10 = is_int10;
567 }
568
569 /*
570 * Ordered so that for each i,
571 * radv_format_meta_fs_key(radv_fs_key_format_exemplars[i]) == i.
572 */
573 const VkFormat radv_fs_key_format_exemplars[NUM_META_FS_KEYS] = {
574 VK_FORMAT_R32_SFLOAT,
575 VK_FORMAT_R32G32_SFLOAT,
576 VK_FORMAT_R8G8B8A8_UNORM,
577 VK_FORMAT_R16G16B16A16_UNORM,
578 VK_FORMAT_R16G16B16A16_SNORM,
579 VK_FORMAT_R16G16B16A16_UINT,
580 VK_FORMAT_R16G16B16A16_SINT,
581 VK_FORMAT_R32G32B32A32_SFLOAT,
582 VK_FORMAT_R8G8B8A8_UINT,
583 VK_FORMAT_R8G8B8A8_SINT,
584 VK_FORMAT_A2R10G10B10_UINT_PACK32,
585 VK_FORMAT_A2R10G10B10_SINT_PACK32,
586 };
587
588 unsigned radv_format_meta_fs_key(VkFormat format)
589 {
590 unsigned col_format = radv_choose_spi_color_format(format, false, false);
591
592 assert(col_format != V_028714_SPI_SHADER_32_AR);
593 if (col_format >= V_028714_SPI_SHADER_32_AR)
594 --col_format; /* Skip V_028714_SPI_SHADER_32_AR since there is no such VkFormat */
595
596 --col_format; /* Skip V_028714_SPI_SHADER_ZERO */
597 bool is_int8 = format_is_int8(format);
598 bool is_int10 = format_is_int10(format);
599
600 return col_format + (is_int8 ? 3 : is_int10 ? 5 : 0);
601 }
602
603 static void
604 radv_blend_check_commutativity(struct radv_blend_state *blend,
605 VkBlendOp op, VkBlendFactor src,
606 VkBlendFactor dst, unsigned chanmask)
607 {
608 /* Src factor is allowed when it does not depend on Dst. */
609 static const uint32_t src_allowed =
610 (1u << VK_BLEND_FACTOR_ONE) |
611 (1u << VK_BLEND_FACTOR_SRC_COLOR) |
612 (1u << VK_BLEND_FACTOR_SRC_ALPHA) |
613 (1u << VK_BLEND_FACTOR_SRC_ALPHA_SATURATE) |
614 (1u << VK_BLEND_FACTOR_CONSTANT_COLOR) |
615 (1u << VK_BLEND_FACTOR_CONSTANT_ALPHA) |
616 (1u << VK_BLEND_FACTOR_SRC1_COLOR) |
617 (1u << VK_BLEND_FACTOR_SRC1_ALPHA) |
618 (1u << VK_BLEND_FACTOR_ZERO) |
619 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR) |
620 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA) |
621 (1u << VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR) |
622 (1u << VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA) |
623 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR) |
624 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA);
625
626 if (dst == VK_BLEND_FACTOR_ONE &&
627 (src_allowed & (1u << src))) {
628 /* Addition is commutative, but floating point addition isn't
629 * associative: subtle changes can be introduced via different
630 * rounding. Be conservative, only enable for min and max.
631 */
632 if (op == VK_BLEND_OP_MAX || op == VK_BLEND_OP_MIN)
633 blend->commutative_4bit |= chanmask;
634 }
635 }
636
637 static struct radv_blend_state
638 radv_pipeline_init_blend_state(struct radv_pipeline *pipeline,
639 const VkGraphicsPipelineCreateInfo *pCreateInfo,
640 const struct radv_graphics_pipeline_create_info *extra)
641 {
642 const VkPipelineColorBlendStateCreateInfo *vkblend = radv_pipeline_get_color_blend_state(pCreateInfo);
643 const VkPipelineMultisampleStateCreateInfo *vkms = radv_pipeline_get_multisample_state(pCreateInfo);
644 struct radv_blend_state blend = {0};
645 unsigned mode = V_028808_CB_NORMAL;
646 int i;
647
648 if (extra && extra->custom_blend_mode) {
649 blend.single_cb_enable = true;
650 mode = extra->custom_blend_mode;
651 }
652
653 blend.cb_color_control = 0;
654 if (vkblend) {
655 if (vkblend->logicOpEnable)
656 blend.cb_color_control |= S_028808_ROP3(si_translate_blend_logic_op(vkblend->logicOp));
657 else
658 blend.cb_color_control |= S_028808_ROP3(V_028808_ROP3_COPY);
659 }
660
661 blend.db_alpha_to_mask = S_028B70_ALPHA_TO_MASK_OFFSET0(3) |
662 S_028B70_ALPHA_TO_MASK_OFFSET1(1) |
663 S_028B70_ALPHA_TO_MASK_OFFSET2(0) |
664 S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
665 S_028B70_OFFSET_ROUND(1);
666
667 if (vkms && vkms->alphaToCoverageEnable) {
668 blend.db_alpha_to_mask |= S_028B70_ALPHA_TO_MASK_ENABLE(1);
669 blend.need_src_alpha |= 0x1;
670 }
671
672 blend.cb_target_mask = 0;
673 if (vkblend) {
674 for (i = 0; i < vkblend->attachmentCount; i++) {
675 const VkPipelineColorBlendAttachmentState *att = &vkblend->pAttachments[i];
676 unsigned blend_cntl = 0;
677 unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;
678 VkBlendOp eqRGB = att->colorBlendOp;
679 VkBlendFactor srcRGB = att->srcColorBlendFactor;
680 VkBlendFactor dstRGB = att->dstColorBlendFactor;
681 VkBlendOp eqA = att->alphaBlendOp;
682 VkBlendFactor srcA = att->srcAlphaBlendFactor;
683 VkBlendFactor dstA = att->dstAlphaBlendFactor;
684
685 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);
686
687 if (!att->colorWriteMask)
688 continue;
689
690 blend.cb_target_mask |= (unsigned)att->colorWriteMask << (4 * i);
691 blend.cb_target_enabled_4bit |= 0xf << (4 * i);
692 if (!att->blendEnable) {
693 blend.cb_blend_control[i] = blend_cntl;
694 continue;
695 }
696
697 if (is_dual_src(srcRGB) || is_dual_src(dstRGB) || is_dual_src(srcA) || is_dual_src(dstA))
698 if (i == 0)
699 blend.mrt0_is_dual_src = true;
700
701 if (eqRGB == VK_BLEND_OP_MIN || eqRGB == VK_BLEND_OP_MAX) {
702 srcRGB = VK_BLEND_FACTOR_ONE;
703 dstRGB = VK_BLEND_FACTOR_ONE;
704 }
705 if (eqA == VK_BLEND_OP_MIN || eqA == VK_BLEND_OP_MAX) {
706 srcA = VK_BLEND_FACTOR_ONE;
707 dstA = VK_BLEND_FACTOR_ONE;
708 }
709
710 radv_blend_check_commutativity(&blend, eqRGB, srcRGB, dstRGB,
711 0x7 << (4 * i));
712 radv_blend_check_commutativity(&blend, eqA, srcA, dstA,
713 0x8 << (4 * i));
714
715 /* Blending optimizations for RB+.
716 * These transformations don't change the behavior.
717 *
718 * First, get rid of DST in the blend factors:
719 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
720 */
721 si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB,
722 VK_BLEND_FACTOR_DST_COLOR,
723 VK_BLEND_FACTOR_SRC_COLOR);
724
725 si_blend_remove_dst(&eqA, &srcA, &dstA,
726 VK_BLEND_FACTOR_DST_COLOR,
727 VK_BLEND_FACTOR_SRC_COLOR);
728
729 si_blend_remove_dst(&eqA, &srcA, &dstA,
730 VK_BLEND_FACTOR_DST_ALPHA,
731 VK_BLEND_FACTOR_SRC_ALPHA);
732
733 /* Look up the ideal settings from tables. */
734 srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);
735 dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);
736 srcA_opt = si_translate_blend_opt_factor(srcA, true);
737 dstA_opt = si_translate_blend_opt_factor(dstA, true);
738
739 /* Handle interdependencies. */
740 if (si_blend_factor_uses_dst(srcRGB))
741 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
742 if (si_blend_factor_uses_dst(srcA))
743 dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
744
745 if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE &&
746 (dstRGB == VK_BLEND_FACTOR_ZERO ||
747 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
748 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE))
749 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
750
751 /* Set the final value. */
752 blend.sx_mrt_blend_opt[i] =
753 S_028760_COLOR_SRC_OPT(srcRGB_opt) |
754 S_028760_COLOR_DST_OPT(dstRGB_opt) |
755 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |
756 S_028760_ALPHA_SRC_OPT(srcA_opt) |
757 S_028760_ALPHA_DST_OPT(dstA_opt) |
758 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));
759 blend_cntl |= S_028780_ENABLE(1);
760
761 blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
762 blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
763 blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
764 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
765 blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
766 blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
767 blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
768 blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
769 }
770 blend.cb_blend_control[i] = blend_cntl;
771
772 blend.blend_enable_4bit |= 0xfu << (i * 4);
773
774 if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
775 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
776 srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
777 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
778 srcRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA ||
779 dstRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA)
780 blend.need_src_alpha |= 1 << i;
781 }
782 for (i = vkblend->attachmentCount; i < 8; i++) {
783 blend.cb_blend_control[i] = 0;
784 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);
785 }
786 }
787
788 if (pipeline->device->physical_device->rad_info.has_rbplus) {
789 /* Disable RB+ blend optimizations for dual source blending. */
790 if (blend.mrt0_is_dual_src) {
791 for (i = 0; i < 8; i++) {
792 blend.sx_mrt_blend_opt[i] =
793 S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE) |
794 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE);
795 }
796 }
797
798 /* RB+ doesn't work with dual source blending, logic op and
799 * RESOLVE.
800 */
801 if (blend.mrt0_is_dual_src ||
802 (vkblend && vkblend->logicOpEnable) ||
803 mode == V_028808_CB_RESOLVE)
804 blend.cb_color_control |= S_028808_DISABLE_DUAL_QUAD(1);
805 }
806
807 if (blend.cb_target_mask)
808 blend.cb_color_control |= S_028808_MODE(mode);
809 else
810 blend.cb_color_control |= S_028808_MODE(V_028808_CB_DISABLE);
811
812 radv_pipeline_compute_spi_color_formats(pipeline, pCreateInfo, &blend);
813 return blend;
814 }
815
816 static uint32_t si_translate_fill(VkPolygonMode func)
817 {
818 switch(func) {
819 case VK_POLYGON_MODE_FILL:
820 return V_028814_X_DRAW_TRIANGLES;
821 case VK_POLYGON_MODE_LINE:
822 return V_028814_X_DRAW_LINES;
823 case VK_POLYGON_MODE_POINT:
824 return V_028814_X_DRAW_POINTS;
825 default:
826 assert(0);
827 return V_028814_X_DRAW_POINTS;
828 }
829 }
830
831 static uint8_t radv_pipeline_get_ps_iter_samples(const VkGraphicsPipelineCreateInfo *pCreateInfo)
832 {
833 const VkPipelineMultisampleStateCreateInfo *vkms = pCreateInfo->pMultisampleState;
834 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
835 struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
836 uint32_t ps_iter_samples = 1;
837 uint32_t num_samples;
838
839 /* From the Vulkan 1.1.129 spec, 26.7. Sample Shading:
840 *
841 * "If the VK_AMD_mixed_attachment_samples extension is enabled and the
842 * subpass uses color attachments, totalSamples is the number of
843 * samples of the color attachments. Otherwise, totalSamples is the
844 * value of VkPipelineMultisampleStateCreateInfo::rasterizationSamples
845 * specified at pipeline creation time."
846 */
847 if (subpass->has_color_att) {
848 num_samples = subpass->color_sample_count;
849 } else {
850 num_samples = vkms->rasterizationSamples;
851 }
852
853 if (vkms->sampleShadingEnable) {
854 ps_iter_samples = ceilf(vkms->minSampleShading * num_samples);
855 ps_iter_samples = util_next_power_of_two(ps_iter_samples);
856 }
857 return ps_iter_samples;
858 }
859
860 static bool
861 radv_is_depth_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)
862 {
863 return pCreateInfo->depthTestEnable &&
864 pCreateInfo->depthWriteEnable &&
865 pCreateInfo->depthCompareOp != VK_COMPARE_OP_NEVER;
866 }
867
868 static bool
869 radv_writes_stencil(const VkStencilOpState *state)
870 {
871 return state->writeMask &&
872 (state->failOp != VK_STENCIL_OP_KEEP ||
873 state->passOp != VK_STENCIL_OP_KEEP ||
874 state->depthFailOp != VK_STENCIL_OP_KEEP);
875 }
876
877 static bool
878 radv_is_stencil_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)
879 {
880 return pCreateInfo->stencilTestEnable &&
881 (radv_writes_stencil(&pCreateInfo->front) ||
882 radv_writes_stencil(&pCreateInfo->back));
883 }
884
885 static bool
886 radv_is_ds_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)
887 {
888 return radv_is_depth_write_enabled(pCreateInfo) ||
889 radv_is_stencil_write_enabled(pCreateInfo);
890 }
891
892 static bool
893 radv_order_invariant_stencil_op(VkStencilOp op)
894 {
895 /* REPLACE is normally order invariant, except when the stencil
896 * reference value is written by the fragment shader. Tracking this
897 * interaction does not seem worth the effort, so be conservative.
898 */
899 return op != VK_STENCIL_OP_INCREMENT_AND_CLAMP &&
900 op != VK_STENCIL_OP_DECREMENT_AND_CLAMP &&
901 op != VK_STENCIL_OP_REPLACE;
902 }
903
904 static bool
905 radv_order_invariant_stencil_state(const VkStencilOpState *state)
906 {
907 /* Compute whether, assuming Z writes are disabled, this stencil state
908 * is order invariant in the sense that the set of passing fragments as
909 * well as the final stencil buffer result does not depend on the order
910 * of fragments.
911 */
912 return !state->writeMask ||
913 /* The following assumes that Z writes are disabled. */
914 (state->compareOp == VK_COMPARE_OP_ALWAYS &&
915 radv_order_invariant_stencil_op(state->passOp) &&
916 radv_order_invariant_stencil_op(state->depthFailOp)) ||
917 (state->compareOp == VK_COMPARE_OP_NEVER &&
918 radv_order_invariant_stencil_op(state->failOp));
919 }
920
921 static bool
922 radv_pipeline_has_dynamic_ds_states(const VkGraphicsPipelineCreateInfo *pCreateInfo)
923 {
924 VkDynamicState ds_states[] = {
925 VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT,
926 VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT,
927 VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT,
928 VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT,
929 VK_DYNAMIC_STATE_STENCIL_OP_EXT,
930 };
931
932 if (pCreateInfo->pDynamicState) {
933 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
934 for (uint32_t i = 0; i < count; i++) {
935 for (uint32_t j = 0; j < ARRAY_SIZE(ds_states); j++) {
936 if (pCreateInfo->pDynamicState->pDynamicStates[i] == ds_states[j])
937 return true;
938 }
939 }
940 }
941
942 return false;
943 }
944
945 static bool
946 radv_pipeline_out_of_order_rast(struct radv_pipeline *pipeline,
947 struct radv_blend_state *blend,
948 const VkGraphicsPipelineCreateInfo *pCreateInfo)
949 {
950 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
951 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
952 const VkPipelineDepthStencilStateCreateInfo *vkds = radv_pipeline_get_depth_stencil_state(pCreateInfo);
953 const VkPipelineColorBlendStateCreateInfo *vkblend = radv_pipeline_get_color_blend_state(pCreateInfo);
954 unsigned colormask = blend->cb_target_enabled_4bit;
955
956 if (!pipeline->device->physical_device->out_of_order_rast_allowed)
957 return false;
958
959 /* Be conservative if a logic operation is enabled with color buffers. */
960 if (colormask && vkblend && vkblend->logicOpEnable)
961 return false;
962
963 /* Be conservative if an extended dynamic depth/stencil state is
964 * enabled because the driver can't update out-of-order rasterization
965 * dynamically.
966 */
967 if (radv_pipeline_has_dynamic_ds_states(pCreateInfo))
968 return false;
969
970 /* Default depth/stencil invariance when no attachment is bound. */
971 struct radv_dsa_order_invariance dsa_order_invariant = {
972 .zs = true, .pass_set = true
973 };
974
975 if (vkds) {
976 struct radv_render_pass_attachment *attachment =
977 pass->attachments + subpass->depth_stencil_attachment->attachment;
978 bool has_stencil = vk_format_is_stencil(attachment->format);
979 struct radv_dsa_order_invariance order_invariance[2];
980 struct radv_shader_variant *ps =
981 pipeline->shaders[MESA_SHADER_FRAGMENT];
982
983 /* Compute depth/stencil order invariance in order to know if
984 * it's safe to enable out-of-order.
985 */
986 bool zfunc_is_ordered =
987 vkds->depthCompareOp == VK_COMPARE_OP_NEVER ||
988 vkds->depthCompareOp == VK_COMPARE_OP_LESS ||
989 vkds->depthCompareOp == VK_COMPARE_OP_LESS_OR_EQUAL ||
990 vkds->depthCompareOp == VK_COMPARE_OP_GREATER ||
991 vkds->depthCompareOp == VK_COMPARE_OP_GREATER_OR_EQUAL;
992
993 bool nozwrite_and_order_invariant_stencil =
994 !radv_is_ds_write_enabled(vkds) ||
995 (!radv_is_depth_write_enabled(vkds) &&
996 radv_order_invariant_stencil_state(&vkds->front) &&
997 radv_order_invariant_stencil_state(&vkds->back));
998
999 order_invariance[1].zs =
1000 nozwrite_and_order_invariant_stencil ||
1001 (!radv_is_stencil_write_enabled(vkds) &&
1002 zfunc_is_ordered);
1003 order_invariance[0].zs =
1004 !radv_is_depth_write_enabled(vkds) || zfunc_is_ordered;
1005
1006 order_invariance[1].pass_set =
1007 nozwrite_and_order_invariant_stencil ||
1008 (!radv_is_stencil_write_enabled(vkds) &&
1009 (vkds->depthCompareOp == VK_COMPARE_OP_ALWAYS ||
1010 vkds->depthCompareOp == VK_COMPARE_OP_NEVER));
1011 order_invariance[0].pass_set =
1012 !radv_is_depth_write_enabled(vkds) ||
1013 (vkds->depthCompareOp == VK_COMPARE_OP_ALWAYS ||
1014 vkds->depthCompareOp == VK_COMPARE_OP_NEVER);
1015
1016 dsa_order_invariant = order_invariance[has_stencil];
1017 if (!dsa_order_invariant.zs)
1018 return false;
1019
1020 /* The set of PS invocations is always order invariant,
1021 * except when early Z/S tests are requested.
1022 */
1023 if (ps &&
1024 ps->info.ps.writes_memory &&
1025 ps->info.ps.early_fragment_test &&
1026 !dsa_order_invariant.pass_set)
1027 return false;
1028
1029 /* Determine if out-of-order rasterization should be disabled
1030 * when occlusion queries are used.
1031 */
1032 pipeline->graphics.disable_out_of_order_rast_for_occlusion =
1033 !dsa_order_invariant.pass_set;
1034 }
1035
1036 /* No color buffers are enabled for writing. */
1037 if (!colormask)
1038 return true;
1039
1040 unsigned blendmask = colormask & blend->blend_enable_4bit;
1041
1042 if (blendmask) {
1043 /* Only commutative blending. */
1044 if (blendmask & ~blend->commutative_4bit)
1045 return false;
1046
1047 if (!dsa_order_invariant.pass_set)
1048 return false;
1049 }
1050
1051 if (colormask & ~blendmask)
1052 return false;
1053
1054 return true;
1055 }
1056
1057 static void
1058 radv_pipeline_init_multisample_state(struct radv_pipeline *pipeline,
1059 struct radv_blend_state *blend,
1060 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1061 {
1062 const VkPipelineMultisampleStateCreateInfo *vkms = radv_pipeline_get_multisample_state(pCreateInfo);
1063 struct radv_multisample_state *ms = &pipeline->graphics.ms;
1064 unsigned num_tile_pipes = pipeline->device->physical_device->rad_info.num_tile_pipes;
1065 bool out_of_order_rast = false;
1066 int ps_iter_samples = 1;
1067 uint32_t mask = 0xffff;
1068
1069 if (vkms) {
1070 ms->num_samples = vkms->rasterizationSamples;
1071
1072 /* From the Vulkan 1.1.129 spec, 26.7. Sample Shading:
1073 *
1074 * "Sample shading is enabled for a graphics pipeline:
1075 *
1076 * - If the interface of the fragment shader entry point of the
1077 * graphics pipeline includes an input variable decorated
1078 * with SampleId or SamplePosition. In this case
1079 * minSampleShadingFactor takes the value 1.0.
1080 * - Else if the sampleShadingEnable member of the
1081 * VkPipelineMultisampleStateCreateInfo structure specified
1082 * when creating the graphics pipeline is set to VK_TRUE. In
1083 * this case minSampleShadingFactor takes the value of
1084 * VkPipelineMultisampleStateCreateInfo::minSampleShading.
1085 *
1086 * Otherwise, sample shading is considered disabled."
1087 */
1088 if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.ps.force_persample) {
1089 ps_iter_samples = ms->num_samples;
1090 } else {
1091 ps_iter_samples = radv_pipeline_get_ps_iter_samples(pCreateInfo);
1092 }
1093 } else {
1094 ms->num_samples = 1;
1095 }
1096
1097 const struct VkPipelineRasterizationStateRasterizationOrderAMD *raster_order =
1098 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext, PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD);
1099 if (raster_order && raster_order->rasterizationOrder == VK_RASTERIZATION_ORDER_RELAXED_AMD) {
1100 /* Out-of-order rasterization is explicitly enabled by the
1101 * application.
1102 */
1103 out_of_order_rast = true;
1104 } else {
1105 /* Determine if the driver can enable out-of-order
1106 * rasterization internally.
1107 */
1108 out_of_order_rast =
1109 radv_pipeline_out_of_order_rast(pipeline, blend, pCreateInfo);
1110 }
1111
1112 ms->pa_sc_aa_config = 0;
1113 ms->db_eqaa = S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
1114 S_028804_INCOHERENT_EQAA_READS(1) |
1115 S_028804_INTERPOLATE_COMP_Z(1) |
1116 S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
1117 ms->pa_sc_mode_cntl_1 =
1118 S_028A4C_WALK_FENCE_ENABLE(1) | //TODO linear dst fixes
1119 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |
1120 S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(out_of_order_rast) |
1121 S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7) |
1122 /* always 1: */
1123 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
1124 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
1125 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
1126 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
1127 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
1128 S_028A4C_FORCE_EOV_REZ_ENABLE(1);
1129 ms->pa_sc_mode_cntl_0 = S_028A48_ALTERNATE_RBS_PER_TILE(pipeline->device->physical_device->rad_info.chip_class >= GFX9) |
1130 S_028A48_VPORT_SCISSOR_ENABLE(1);
1131
1132 const VkPipelineRasterizationLineStateCreateInfoEXT *rast_line =
1133 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1134 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);
1135 if (rast_line) {
1136 ms->pa_sc_mode_cntl_0 |= S_028A48_LINE_STIPPLE_ENABLE(rast_line->stippledLineEnable);
1137 if (rast_line->lineRasterizationMode == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT) {
1138 /* From the Vulkan spec 1.1.129:
1139 *
1140 * "When VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT lines
1141 * are being rasterized, sample locations may all be
1142 * treated as being at the pixel center (this may
1143 * affect attribute and depth interpolation)."
1144 */
1145 ms->num_samples = 1;
1146 }
1147 }
1148
1149 if (ms->num_samples > 1) {
1150 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1151 struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
1152 uint32_t z_samples = subpass->depth_stencil_attachment ? subpass->depth_sample_count : ms->num_samples;
1153 unsigned log_samples = util_logbase2(ms->num_samples);
1154 unsigned log_z_samples = util_logbase2(z_samples);
1155 unsigned log_ps_iter_samples = util_logbase2(ps_iter_samples);
1156 ms->pa_sc_mode_cntl_0 |= S_028A48_MSAA_ENABLE(1);
1157 ms->db_eqaa |= S_028804_MAX_ANCHOR_SAMPLES(log_z_samples) |
1158 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |
1159 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |
1160 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples);
1161 ms->pa_sc_aa_config |= S_028BE0_MSAA_NUM_SAMPLES(log_samples) |
1162 S_028BE0_MAX_SAMPLE_DIST(radv_get_default_max_sample_dist(log_samples)) |
1163 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples) | /* CM_R_028BE0_PA_SC_AA_CONFIG */
1164 S_028BE0_COVERED_CENTROID_IS_CENTER_GFX103(pipeline->device->physical_device->rad_info.chip_class >= GFX10_3);
1165 ms->pa_sc_mode_cntl_1 |= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1);
1166 if (ps_iter_samples > 1)
1167 pipeline->graphics.spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(2);
1168 }
1169
1170 if (vkms && vkms->pSampleMask) {
1171 mask = vkms->pSampleMask[0] & 0xffff;
1172 }
1173
1174 ms->pa_sc_aa_mask[0] = mask | (mask << 16);
1175 ms->pa_sc_aa_mask[1] = mask | (mask << 16);
1176 }
1177
1178 static bool
1179 radv_prim_can_use_guardband(enum VkPrimitiveTopology topology)
1180 {
1181 switch (topology) {
1182 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
1183 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
1184 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
1185 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1186 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1187 return false;
1188 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
1189 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
1190 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
1191 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1192 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1193 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
1194 return true;
1195 default:
1196 unreachable("unhandled primitive type");
1197 }
1198 }
1199
1200 static uint32_t
1201 si_conv_gl_prim_to_gs_out(unsigned gl_prim)
1202 {
1203 switch (gl_prim) {
1204 case 0: /* GL_POINTS */
1205 return V_028A6C_OUTPRIM_TYPE_POINTLIST;
1206 case 1: /* GL_LINES */
1207 case 3: /* GL_LINE_STRIP */
1208 case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */
1209 case 0x8E7A: /* GL_ISOLINES */
1210 return V_028A6C_OUTPRIM_TYPE_LINESTRIP;
1211
1212 case 4: /* GL_TRIANGLES */
1213 case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */
1214 case 5: /* GL_TRIANGLE_STRIP */
1215 case 7: /* GL_QUADS */
1216 return V_028A6C_OUTPRIM_TYPE_TRISTRIP;
1217 default:
1218 assert(0);
1219 return 0;
1220 }
1221 }
1222
1223 static uint32_t
1224 si_conv_prim_to_gs_out(enum VkPrimitiveTopology topology)
1225 {
1226 switch (topology) {
1227 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
1228 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
1229 return V_028A6C_OUTPRIM_TYPE_POINTLIST;
1230 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
1231 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
1232 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1233 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1234 return V_028A6C_OUTPRIM_TYPE_LINESTRIP;
1235 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
1236 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
1237 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
1238 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1239 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1240 return V_028A6C_OUTPRIM_TYPE_TRISTRIP;
1241 default:
1242 assert(0);
1243 return 0;
1244 }
1245 }
1246
1247 static unsigned radv_dynamic_state_mask(VkDynamicState state)
1248 {
1249 switch(state) {
1250 case VK_DYNAMIC_STATE_VIEWPORT:
1251 case VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT:
1252 return RADV_DYNAMIC_VIEWPORT;
1253 case VK_DYNAMIC_STATE_SCISSOR:
1254 case VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT:
1255 return RADV_DYNAMIC_SCISSOR;
1256 case VK_DYNAMIC_STATE_LINE_WIDTH:
1257 return RADV_DYNAMIC_LINE_WIDTH;
1258 case VK_DYNAMIC_STATE_DEPTH_BIAS:
1259 return RADV_DYNAMIC_DEPTH_BIAS;
1260 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
1261 return RADV_DYNAMIC_BLEND_CONSTANTS;
1262 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
1263 return RADV_DYNAMIC_DEPTH_BOUNDS;
1264 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
1265 return RADV_DYNAMIC_STENCIL_COMPARE_MASK;
1266 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
1267 return RADV_DYNAMIC_STENCIL_WRITE_MASK;
1268 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
1269 return RADV_DYNAMIC_STENCIL_REFERENCE;
1270 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT:
1271 return RADV_DYNAMIC_DISCARD_RECTANGLE;
1272 case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT:
1273 return RADV_DYNAMIC_SAMPLE_LOCATIONS;
1274 case VK_DYNAMIC_STATE_LINE_STIPPLE_EXT:
1275 return RADV_DYNAMIC_LINE_STIPPLE;
1276 case VK_DYNAMIC_STATE_CULL_MODE_EXT:
1277 return RADV_DYNAMIC_CULL_MODE;
1278 case VK_DYNAMIC_STATE_FRONT_FACE_EXT:
1279 return RADV_DYNAMIC_FRONT_FACE;
1280 case VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT:
1281 return RADV_DYNAMIC_PRIMITIVE_TOPOLOGY;
1282 case VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT:
1283 return RADV_DYNAMIC_DEPTH_TEST_ENABLE;
1284 case VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT:
1285 return RADV_DYNAMIC_DEPTH_WRITE_ENABLE;
1286 case VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT:
1287 return RADV_DYNAMIC_DEPTH_COMPARE_OP;
1288 case VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT:
1289 return RADV_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE;
1290 case VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT:
1291 return RADV_DYNAMIC_STENCIL_TEST_ENABLE;
1292 case VK_DYNAMIC_STATE_STENCIL_OP_EXT:
1293 return RADV_DYNAMIC_STENCIL_OP;
1294 case VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT:
1295 return RADV_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE;
1296 default:
1297 unreachable("Unhandled dynamic state");
1298 }
1299 }
1300
1301 static uint32_t radv_pipeline_needed_dynamic_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
1302 {
1303 uint32_t states = RADV_DYNAMIC_ALL;
1304
1305 /* If rasterization is disabled we do not care about any of the
1306 * dynamic states, since they are all rasterization related only,
1307 * except primitive topology and vertex binding stride.
1308 */
1309 if (pCreateInfo->pRasterizationState->rasterizerDiscardEnable)
1310 return RADV_DYNAMIC_PRIMITIVE_TOPOLOGY |
1311 RADV_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE;
1312
1313 if (!pCreateInfo->pRasterizationState->depthBiasEnable)
1314 states &= ~RADV_DYNAMIC_DEPTH_BIAS;
1315
1316 if (!pCreateInfo->pDepthStencilState ||
1317 !pCreateInfo->pDepthStencilState->depthBoundsTestEnable)
1318 states &= ~RADV_DYNAMIC_DEPTH_BOUNDS;
1319
1320 if (!pCreateInfo->pDepthStencilState ||
1321 !pCreateInfo->pDepthStencilState->stencilTestEnable)
1322 states &= ~(RADV_DYNAMIC_STENCIL_COMPARE_MASK |
1323 RADV_DYNAMIC_STENCIL_WRITE_MASK |
1324 RADV_DYNAMIC_STENCIL_REFERENCE);
1325
1326 if (!vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT))
1327 states &= ~RADV_DYNAMIC_DISCARD_RECTANGLE;
1328
1329 if (!pCreateInfo->pMultisampleState ||
1330 !vk_find_struct_const(pCreateInfo->pMultisampleState->pNext,
1331 PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT))
1332 states &= ~RADV_DYNAMIC_SAMPLE_LOCATIONS;
1333
1334 if (!pCreateInfo->pRasterizationState ||
1335 !vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1336 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT))
1337 states &= ~RADV_DYNAMIC_LINE_STIPPLE;
1338
1339 /* TODO: blend constants & line width. */
1340
1341 return states;
1342 }
1343
1344
1345 static void
1346 radv_pipeline_init_dynamic_state(struct radv_pipeline *pipeline,
1347 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1348 const struct radv_graphics_pipeline_create_info *extra)
1349 {
1350 uint32_t needed_states = radv_pipeline_needed_dynamic_state(pCreateInfo);
1351 uint32_t states = needed_states;
1352 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1353 struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
1354
1355 pipeline->dynamic_state = default_dynamic_state;
1356 pipeline->graphics.needed_dynamic_state = needed_states;
1357
1358 if (pCreateInfo->pDynamicState) {
1359 /* Remove all of the states that are marked as dynamic */
1360 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
1361 for (uint32_t s = 0; s < count; s++)
1362 states &= ~radv_dynamic_state_mask(pCreateInfo->pDynamicState->pDynamicStates[s]);
1363 }
1364
1365 struct radv_dynamic_state *dynamic = &pipeline->dynamic_state;
1366
1367 if (needed_states & RADV_DYNAMIC_VIEWPORT) {
1368 assert(pCreateInfo->pViewportState);
1369
1370 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
1371 if (states & RADV_DYNAMIC_VIEWPORT) {
1372 typed_memcpy(dynamic->viewport.viewports,
1373 pCreateInfo->pViewportState->pViewports,
1374 pCreateInfo->pViewportState->viewportCount);
1375 }
1376 }
1377
1378 if (needed_states & RADV_DYNAMIC_SCISSOR) {
1379 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
1380 if (states & RADV_DYNAMIC_SCISSOR) {
1381 typed_memcpy(dynamic->scissor.scissors,
1382 pCreateInfo->pViewportState->pScissors,
1383 pCreateInfo->pViewportState->scissorCount);
1384 }
1385 }
1386
1387 if (states & RADV_DYNAMIC_LINE_WIDTH) {
1388 assert(pCreateInfo->pRasterizationState);
1389 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
1390 }
1391
1392 if (states & RADV_DYNAMIC_DEPTH_BIAS) {
1393 assert(pCreateInfo->pRasterizationState);
1394 dynamic->depth_bias.bias =
1395 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
1396 dynamic->depth_bias.clamp =
1397 pCreateInfo->pRasterizationState->depthBiasClamp;
1398 dynamic->depth_bias.slope =
1399 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
1400 }
1401
1402 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1403 *
1404 * pColorBlendState is [...] NULL if the pipeline has rasterization
1405 * disabled or if the subpass of the render pass the pipeline is
1406 * created against does not use any color attachments.
1407 */
1408 if (subpass->has_color_att && states & RADV_DYNAMIC_BLEND_CONSTANTS) {
1409 assert(pCreateInfo->pColorBlendState);
1410 typed_memcpy(dynamic->blend_constants,
1411 pCreateInfo->pColorBlendState->blendConstants, 4);
1412 }
1413
1414 if (states & RADV_DYNAMIC_CULL_MODE) {
1415 dynamic->cull_mode =
1416 pCreateInfo->pRasterizationState->cullMode;
1417 }
1418
1419 if (states & RADV_DYNAMIC_FRONT_FACE) {
1420 dynamic->front_face =
1421 pCreateInfo->pRasterizationState->frontFace;
1422 }
1423
1424 if (states & RADV_DYNAMIC_PRIMITIVE_TOPOLOGY) {
1425 dynamic->primitive_topology =
1426 si_translate_prim(pCreateInfo->pInputAssemblyState->topology);
1427 if (extra && extra->use_rectlist) {
1428 dynamic->primitive_topology = V_008958_DI_PT_RECTLIST;
1429 }
1430 }
1431
1432 /* If there is no depthstencil attachment, then don't read
1433 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1434 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1435 * no need to override the depthstencil defaults in
1436 * radv_pipeline::dynamic_state when there is no depthstencil attachment.
1437 *
1438 * Section 9.2 of the Vulkan 1.0.15 spec says:
1439 *
1440 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1441 * disabled or if the subpass of the render pass the pipeline is created
1442 * against does not use a depth/stencil attachment.
1443 */
1444 if (needed_states && subpass->depth_stencil_attachment) {
1445 assert(pCreateInfo->pDepthStencilState);
1446
1447 if (states & RADV_DYNAMIC_DEPTH_BOUNDS) {
1448 dynamic->depth_bounds.min =
1449 pCreateInfo->pDepthStencilState->minDepthBounds;
1450 dynamic->depth_bounds.max =
1451 pCreateInfo->pDepthStencilState->maxDepthBounds;
1452 }
1453
1454 if (states & RADV_DYNAMIC_STENCIL_COMPARE_MASK) {
1455 dynamic->stencil_compare_mask.front =
1456 pCreateInfo->pDepthStencilState->front.compareMask;
1457 dynamic->stencil_compare_mask.back =
1458 pCreateInfo->pDepthStencilState->back.compareMask;
1459 }
1460
1461 if (states & RADV_DYNAMIC_STENCIL_WRITE_MASK) {
1462 dynamic->stencil_write_mask.front =
1463 pCreateInfo->pDepthStencilState->front.writeMask;
1464 dynamic->stencil_write_mask.back =
1465 pCreateInfo->pDepthStencilState->back.writeMask;
1466 }
1467
1468 if (states & RADV_DYNAMIC_STENCIL_REFERENCE) {
1469 dynamic->stencil_reference.front =
1470 pCreateInfo->pDepthStencilState->front.reference;
1471 dynamic->stencil_reference.back =
1472 pCreateInfo->pDepthStencilState->back.reference;
1473 }
1474
1475 if (states & RADV_DYNAMIC_DEPTH_TEST_ENABLE) {
1476 dynamic->depth_test_enable =
1477 pCreateInfo->pDepthStencilState->depthTestEnable;
1478 }
1479
1480 if (states & RADV_DYNAMIC_DEPTH_WRITE_ENABLE) {
1481 dynamic->depth_write_enable =
1482 pCreateInfo->pDepthStencilState->depthWriteEnable;
1483 }
1484
1485 if (states & RADV_DYNAMIC_DEPTH_COMPARE_OP) {
1486 dynamic->depth_compare_op =
1487 pCreateInfo->pDepthStencilState->depthCompareOp;
1488 }
1489
1490 if (states & RADV_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE) {
1491 dynamic->depth_bounds_test_enable =
1492 pCreateInfo->pDepthStencilState->depthBoundsTestEnable;
1493 }
1494
1495 if (states & RADV_DYNAMIC_STENCIL_TEST_ENABLE) {
1496 dynamic->stencil_test_enable =
1497 pCreateInfo->pDepthStencilState->stencilTestEnable;
1498 }
1499
1500 if (states & RADV_DYNAMIC_STENCIL_OP) {
1501 dynamic->stencil_op.front.compare_op =
1502 pCreateInfo->pDepthStencilState->front.compareOp;
1503 dynamic->stencil_op.front.fail_op =
1504 pCreateInfo->pDepthStencilState->front.failOp;
1505 dynamic->stencil_op.front.pass_op =
1506 pCreateInfo->pDepthStencilState->front.passOp;
1507 dynamic->stencil_op.front.depth_fail_op =
1508 pCreateInfo->pDepthStencilState->front.depthFailOp;
1509
1510 dynamic->stencil_op.back.compare_op =
1511 pCreateInfo->pDepthStencilState->back.compareOp;
1512 dynamic->stencil_op.back.fail_op =
1513 pCreateInfo->pDepthStencilState->back.failOp;
1514 dynamic->stencil_op.back.pass_op =
1515 pCreateInfo->pDepthStencilState->back.passOp;
1516 dynamic->stencil_op.back.depth_fail_op =
1517 pCreateInfo->pDepthStencilState->back.depthFailOp;
1518 }
1519 }
1520
1521 const VkPipelineDiscardRectangleStateCreateInfoEXT *discard_rectangle_info =
1522 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);
1523 if (needed_states & RADV_DYNAMIC_DISCARD_RECTANGLE) {
1524 dynamic->discard_rectangle.count = discard_rectangle_info->discardRectangleCount;
1525 if (states & RADV_DYNAMIC_DISCARD_RECTANGLE) {
1526 typed_memcpy(dynamic->discard_rectangle.rectangles,
1527 discard_rectangle_info->pDiscardRectangles,
1528 discard_rectangle_info->discardRectangleCount);
1529 }
1530 }
1531
1532 if (needed_states & RADV_DYNAMIC_SAMPLE_LOCATIONS) {
1533 const VkPipelineSampleLocationsStateCreateInfoEXT *sample_location_info =
1534 vk_find_struct_const(pCreateInfo->pMultisampleState->pNext,
1535 PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT);
1536 /* If sampleLocationsEnable is VK_FALSE, the default sample
1537 * locations are used and the values specified in
1538 * sampleLocationsInfo are ignored.
1539 */
1540 if (sample_location_info->sampleLocationsEnable) {
1541 const VkSampleLocationsInfoEXT *pSampleLocationsInfo =
1542 &sample_location_info->sampleLocationsInfo;
1543
1544 assert(pSampleLocationsInfo->sampleLocationsCount <= MAX_SAMPLE_LOCATIONS);
1545
1546 dynamic->sample_location.per_pixel = pSampleLocationsInfo->sampleLocationsPerPixel;
1547 dynamic->sample_location.grid_size = pSampleLocationsInfo->sampleLocationGridSize;
1548 dynamic->sample_location.count = pSampleLocationsInfo->sampleLocationsCount;
1549 typed_memcpy(&dynamic->sample_location.locations[0],
1550 pSampleLocationsInfo->pSampleLocations,
1551 pSampleLocationsInfo->sampleLocationsCount);
1552 }
1553 }
1554
1555 const VkPipelineRasterizationLineStateCreateInfoEXT *rast_line_info =
1556 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1557 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);
1558 if (needed_states & RADV_DYNAMIC_LINE_STIPPLE) {
1559 dynamic->line_stipple.factor = rast_line_info->lineStippleFactor;
1560 dynamic->line_stipple.pattern = rast_line_info->lineStipplePattern;
1561 }
1562
1563 if (!(states & RADV_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE))
1564 pipeline->graphics.uses_dynamic_stride = true;
1565
1566 pipeline->dynamic_state.mask = states;
1567 }
1568
1569 static void
1570 gfx9_get_gs_info(const struct radv_pipeline_key *key,
1571 const struct radv_pipeline *pipeline,
1572 nir_shader **nir,
1573 struct radv_shader_info *infos,
1574 struct gfx9_gs_info *out)
1575 {
1576 struct radv_shader_info *gs_info = &infos[MESA_SHADER_GEOMETRY];
1577 struct radv_es_output_info *es_info;
1578 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9)
1579 es_info = nir[MESA_SHADER_TESS_CTRL] ? &gs_info->tes.es_info : &gs_info->vs.es_info;
1580 else
1581 es_info = nir[MESA_SHADER_TESS_CTRL] ?
1582 &infos[MESA_SHADER_TESS_EVAL].tes.es_info :
1583 &infos[MESA_SHADER_VERTEX].vs.es_info;
1584
1585 unsigned gs_num_invocations = MAX2(gs_info->gs.invocations, 1);
1586 bool uses_adjacency;
1587 switch(key->topology) {
1588 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1589 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1590 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1591 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1592 uses_adjacency = true;
1593 break;
1594 default:
1595 uses_adjacency = false;
1596 break;
1597 }
1598
1599 /* All these are in dwords: */
1600 /* We can't allow using the whole LDS, because GS waves compete with
1601 * other shader stages for LDS space. */
1602 const unsigned max_lds_size = 8 * 1024;
1603 const unsigned esgs_itemsize = es_info->esgs_itemsize / 4;
1604 unsigned esgs_lds_size;
1605
1606 /* All these are per subgroup: */
1607 const unsigned max_out_prims = 32 * 1024;
1608 const unsigned max_es_verts = 255;
1609 const unsigned ideal_gs_prims = 64;
1610 unsigned max_gs_prims, gs_prims;
1611 unsigned min_es_verts, es_verts, worst_case_es_verts;
1612
1613 if (uses_adjacency || gs_num_invocations > 1)
1614 max_gs_prims = 127 / gs_num_invocations;
1615 else
1616 max_gs_prims = 255;
1617
1618 /* MAX_PRIMS_PER_SUBGROUP = gs_prims * max_vert_out * gs_invocations.
1619 * Make sure we don't go over the maximum value.
1620 */
1621 if (gs_info->gs.vertices_out > 0) {
1622 max_gs_prims = MIN2(max_gs_prims,
1623 max_out_prims /
1624 (gs_info->gs.vertices_out * gs_num_invocations));
1625 }
1626 assert(max_gs_prims > 0);
1627
1628 /* If the primitive has adjacency, halve the number of vertices
1629 * that will be reused in multiple primitives.
1630 */
1631 min_es_verts = gs_info->gs.vertices_in / (uses_adjacency ? 2 : 1);
1632
1633 gs_prims = MIN2(ideal_gs_prims, max_gs_prims);
1634 worst_case_es_verts = MIN2(min_es_verts * gs_prims, max_es_verts);
1635
1636 /* Compute ESGS LDS size based on the worst case number of ES vertices
1637 * needed to create the target number of GS prims per subgroup.
1638 */
1639 esgs_lds_size = esgs_itemsize * worst_case_es_verts;
1640
1641 /* If total LDS usage is too big, refactor partitions based on ratio
1642 * of ESGS item sizes.
1643 */
1644 if (esgs_lds_size > max_lds_size) {
1645 /* Our target GS Prims Per Subgroup was too large. Calculate
1646 * the maximum number of GS Prims Per Subgroup that will fit
1647 * into LDS, capped by the maximum that the hardware can support.
1648 */
1649 gs_prims = MIN2((max_lds_size / (esgs_itemsize * min_es_verts)),
1650 max_gs_prims);
1651 assert(gs_prims > 0);
1652 worst_case_es_verts = MIN2(min_es_verts * gs_prims,
1653 max_es_verts);
1654
1655 esgs_lds_size = esgs_itemsize * worst_case_es_verts;
1656 assert(esgs_lds_size <= max_lds_size);
1657 }
1658
1659 /* Now calculate remaining ESGS information. */
1660 if (esgs_lds_size)
1661 es_verts = MIN2(esgs_lds_size / esgs_itemsize, max_es_verts);
1662 else
1663 es_verts = max_es_verts;
1664
1665 /* Vertices for adjacency primitives are not always reused, so restore
1666 * it for ES_VERTS_PER_SUBGRP.
1667 */
1668 min_es_verts = gs_info->gs.vertices_in;
1669
1670 /* For normal primitives, the VGT only checks if they are past the ES
1671 * verts per subgroup after allocating a full GS primitive and if they
1672 * are, kick off a new subgroup. But if those additional ES verts are
1673 * unique (e.g. not reused) we need to make sure there is enough LDS
1674 * space to account for those ES verts beyond ES_VERTS_PER_SUBGRP.
1675 */
1676 es_verts -= min_es_verts - 1;
1677
1678 uint32_t es_verts_per_subgroup = es_verts;
1679 uint32_t gs_prims_per_subgroup = gs_prims;
1680 uint32_t gs_inst_prims_in_subgroup = gs_prims * gs_num_invocations;
1681 uint32_t max_prims_per_subgroup = gs_inst_prims_in_subgroup * gs_info->gs.vertices_out;
1682 out->lds_size = align(esgs_lds_size, 128) / 128;
1683 out->vgt_gs_onchip_cntl = S_028A44_ES_VERTS_PER_SUBGRP(es_verts_per_subgroup) |
1684 S_028A44_GS_PRIMS_PER_SUBGRP(gs_prims_per_subgroup) |
1685 S_028A44_GS_INST_PRIMS_IN_SUBGRP(gs_inst_prims_in_subgroup);
1686 out->vgt_gs_max_prims_per_subgroup = S_028A94_MAX_PRIMS_PER_SUBGROUP(max_prims_per_subgroup);
1687 out->vgt_esgs_ring_itemsize = esgs_itemsize;
1688 assert(max_prims_per_subgroup <= max_out_prims);
1689 }
1690
1691 static void clamp_gsprims_to_esverts(unsigned *max_gsprims, unsigned max_esverts,
1692 unsigned min_verts_per_prim, bool use_adjacency)
1693 {
1694 unsigned max_reuse = max_esverts - min_verts_per_prim;
1695 if (use_adjacency)
1696 max_reuse /= 2;
1697 *max_gsprims = MIN2(*max_gsprims, 1 + max_reuse);
1698 }
1699
1700 static unsigned
1701 radv_get_num_input_vertices(nir_shader **nir)
1702 {
1703 if (nir[MESA_SHADER_GEOMETRY]) {
1704 nir_shader *gs = nir[MESA_SHADER_GEOMETRY];
1705
1706 return gs->info.gs.vertices_in;
1707 }
1708
1709 if (nir[MESA_SHADER_TESS_CTRL]) {
1710 nir_shader *tes = nir[MESA_SHADER_TESS_EVAL];
1711
1712 if (tes->info.tess.point_mode)
1713 return 1;
1714 if (tes->info.tess.primitive_mode == GL_ISOLINES)
1715 return 2;
1716 return 3;
1717 }
1718
1719 return 3;
1720 }
1721
1722 static void
1723 gfx10_get_ngg_info(const struct radv_pipeline_key *key,
1724 struct radv_pipeline *pipeline,
1725 nir_shader **nir,
1726 struct radv_shader_info *infos,
1727 struct gfx10_ngg_info *ngg)
1728 {
1729 struct radv_shader_info *gs_info = &infos[MESA_SHADER_GEOMETRY];
1730 struct radv_es_output_info *es_info =
1731 nir[MESA_SHADER_TESS_CTRL] ? &gs_info->tes.es_info : &gs_info->vs.es_info;
1732 unsigned gs_type = nir[MESA_SHADER_GEOMETRY] ? MESA_SHADER_GEOMETRY : MESA_SHADER_VERTEX;
1733 unsigned max_verts_per_prim = radv_get_num_input_vertices(nir);
1734 unsigned min_verts_per_prim =
1735 gs_type == MESA_SHADER_GEOMETRY ? max_verts_per_prim : 1;
1736 unsigned gs_num_invocations = nir[MESA_SHADER_GEOMETRY] ? MAX2(gs_info->gs.invocations, 1) : 1;
1737 bool uses_adjacency;
1738 switch(key->topology) {
1739 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1740 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1741 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1742 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1743 uses_adjacency = true;
1744 break;
1745 default:
1746 uses_adjacency = false;
1747 break;
1748 }
1749
1750 /* All these are in dwords: */
1751 /* We can't allow using the whole LDS, because GS waves compete with
1752 * other shader stages for LDS space.
1753 *
1754 * TODO: We should really take the shader's internal LDS use into
1755 * account. The linker will fail if the size is greater than
1756 * 8K dwords.
1757 */
1758 const unsigned max_lds_size = 8 * 1024 - 768;
1759 const unsigned target_lds_size = max_lds_size;
1760 unsigned esvert_lds_size = 0;
1761 unsigned gsprim_lds_size = 0;
1762
1763 /* All these are per subgroup: */
1764 bool max_vert_out_per_gs_instance = false;
1765 unsigned max_esverts_base = 256;
1766 unsigned max_gsprims_base = 128; /* default prim group size clamp */
1767
1768 /* Hardware has the following non-natural restrictions on the value
1769 * of GE_CNTL.VERT_GRP_SIZE based on based on the primitive type of
1770 * the draw:
1771 * - at most 252 for any line input primitive type
1772 * - at most 251 for any quad input primitive type
1773 * - at most 251 for triangle strips with adjacency (this happens to
1774 * be the natural limit for triangle *lists* with adjacency)
1775 */
1776 max_esverts_base = MIN2(max_esverts_base, 251 + max_verts_per_prim - 1);
1777
1778 if (gs_type == MESA_SHADER_GEOMETRY) {
1779 unsigned max_out_verts_per_gsprim =
1780 gs_info->gs.vertices_out * gs_num_invocations;
1781
1782 if (max_out_verts_per_gsprim <= 256) {
1783 if (max_out_verts_per_gsprim) {
1784 max_gsprims_base = MIN2(max_gsprims_base,
1785 256 / max_out_verts_per_gsprim);
1786 }
1787 } else {
1788 /* Use special multi-cycling mode in which each GS
1789 * instance gets its own subgroup. Does not work with
1790 * tessellation. */
1791 max_vert_out_per_gs_instance = true;
1792 max_gsprims_base = 1;
1793 max_out_verts_per_gsprim = gs_info->gs.vertices_out;
1794 }
1795
1796 esvert_lds_size = es_info->esgs_itemsize / 4;
1797 gsprim_lds_size = (gs_info->gs.gsvs_vertex_size / 4 + 1) * max_out_verts_per_gsprim;
1798 } else {
1799 /* VS and TES. */
1800 /* LDS size for passing data from GS to ES. */
1801 struct radv_streamout_info *so_info = nir[MESA_SHADER_TESS_CTRL]
1802 ? &infos[MESA_SHADER_TESS_EVAL].so
1803 : &infos[MESA_SHADER_VERTEX].so;
1804
1805 if (so_info->num_outputs)
1806 esvert_lds_size = 4 * so_info->num_outputs + 1;
1807
1808 /* GS stores Primitive IDs (one DWORD) into LDS at the address
1809 * corresponding to the ES thread of the provoking vertex. All
1810 * ES threads load and export PrimitiveID for their thread.
1811 */
1812 if (!nir[MESA_SHADER_TESS_CTRL] &&
1813 infos[MESA_SHADER_VERTEX].vs.outinfo.export_prim_id)
1814 esvert_lds_size = MAX2(esvert_lds_size, 1);
1815 }
1816
1817 unsigned max_gsprims = max_gsprims_base;
1818 unsigned max_esverts = max_esverts_base;
1819
1820 if (esvert_lds_size)
1821 max_esverts = MIN2(max_esverts, target_lds_size / esvert_lds_size);
1822 if (gsprim_lds_size)
1823 max_gsprims = MIN2(max_gsprims, target_lds_size / gsprim_lds_size);
1824
1825 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1826 clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, uses_adjacency);
1827 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1828
1829 if (esvert_lds_size || gsprim_lds_size) {
1830 /* Now that we have a rough proportionality between esverts
1831 * and gsprims based on the primitive type, scale both of them
1832 * down simultaneously based on required LDS space.
1833 *
1834 * We could be smarter about this if we knew how much vertex
1835 * reuse to expect.
1836 */
1837 unsigned lds_total = max_esverts * esvert_lds_size +
1838 max_gsprims * gsprim_lds_size;
1839 if (lds_total > target_lds_size) {
1840 max_esverts = max_esverts * target_lds_size / lds_total;
1841 max_gsprims = max_gsprims * target_lds_size / lds_total;
1842
1843 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1844 clamp_gsprims_to_esverts(&max_gsprims, max_esverts,
1845 min_verts_per_prim, uses_adjacency);
1846 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1847 }
1848 }
1849
1850 /* Round up towards full wave sizes for better ALU utilization. */
1851 if (!max_vert_out_per_gs_instance) {
1852 unsigned orig_max_esverts;
1853 unsigned orig_max_gsprims;
1854 unsigned wavesize;
1855
1856 if (gs_type == MESA_SHADER_GEOMETRY) {
1857 wavesize = gs_info->wave_size;
1858 } else {
1859 wavesize = nir[MESA_SHADER_TESS_CTRL]
1860 ? infos[MESA_SHADER_TESS_EVAL].wave_size
1861 : infos[MESA_SHADER_VERTEX].wave_size;
1862 }
1863
1864 do {
1865 orig_max_esverts = max_esverts;
1866 orig_max_gsprims = max_gsprims;
1867
1868 max_esverts = align(max_esverts, wavesize);
1869 max_esverts = MIN2(max_esverts, max_esverts_base);
1870 if (esvert_lds_size)
1871 max_esverts = MIN2(max_esverts,
1872 (max_lds_size - max_gsprims * gsprim_lds_size) /
1873 esvert_lds_size);
1874 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1875
1876 max_gsprims = align(max_gsprims, wavesize);
1877 max_gsprims = MIN2(max_gsprims, max_gsprims_base);
1878 if (gsprim_lds_size)
1879 max_gsprims = MIN2(max_gsprims,
1880 (max_lds_size - max_esverts * esvert_lds_size) /
1881 gsprim_lds_size);
1882 clamp_gsprims_to_esverts(&max_gsprims, max_esverts,
1883 min_verts_per_prim, uses_adjacency);
1884 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1885 } while (orig_max_esverts != max_esverts || orig_max_gsprims != max_gsprims);
1886 }
1887
1888 /* Hardware restriction: minimum value of max_esverts */
1889 max_esverts = MAX2(max_esverts, 23 + max_verts_per_prim);
1890
1891 unsigned max_out_vertices =
1892 max_vert_out_per_gs_instance ? gs_info->gs.vertices_out :
1893 gs_type == MESA_SHADER_GEOMETRY ?
1894 max_gsprims * gs_num_invocations * gs_info->gs.vertices_out :
1895 max_esverts;
1896 assert(max_out_vertices <= 256);
1897
1898 unsigned prim_amp_factor = 1;
1899 if (gs_type == MESA_SHADER_GEOMETRY) {
1900 /* Number of output primitives per GS input primitive after
1901 * GS instancing. */
1902 prim_amp_factor = gs_info->gs.vertices_out;
1903 }
1904
1905 /* The GE only checks against the maximum number of ES verts after
1906 * allocating a full GS primitive. So we need to ensure that whenever
1907 * this check passes, there is enough space for a full primitive without
1908 * vertex reuse.
1909 */
1910 ngg->hw_max_esverts = max_esverts - max_verts_per_prim + 1;
1911 ngg->max_gsprims = max_gsprims;
1912 ngg->max_out_verts = max_out_vertices;
1913 ngg->prim_amp_factor = prim_amp_factor;
1914 ngg->max_vert_out_per_gs_instance = max_vert_out_per_gs_instance;
1915 ngg->ngg_emit_size = max_gsprims * gsprim_lds_size;
1916 ngg->esgs_ring_size = 4 * max_esverts * esvert_lds_size;
1917
1918 if (gs_type == MESA_SHADER_GEOMETRY) {
1919 ngg->vgt_esgs_ring_itemsize = es_info->esgs_itemsize / 4;
1920 } else {
1921 ngg->vgt_esgs_ring_itemsize = 1;
1922 }
1923
1924 pipeline->graphics.esgs_ring_size = ngg->esgs_ring_size;
1925
1926 assert(ngg->hw_max_esverts >= 24); /* HW limitation */
1927 }
1928
1929 static void
1930 calculate_gs_ring_sizes(struct radv_pipeline *pipeline,
1931 const struct gfx9_gs_info *gs)
1932 {
1933 struct radv_device *device = pipeline->device;
1934 unsigned num_se = device->physical_device->rad_info.max_se;
1935 unsigned wave_size = 64;
1936 unsigned max_gs_waves = 32 * num_se; /* max 32 per SE on GCN */
1937 /* On GFX6-GFX7, the value comes from VGT_GS_VERTEX_REUSE = 16.
1938 * On GFX8+, the value comes from VGT_VERTEX_REUSE_BLOCK_CNTL = 30 (+2).
1939 */
1940 unsigned gs_vertex_reuse =
1941 (device->physical_device->rad_info.chip_class >= GFX8 ? 32 : 16) * num_se;
1942 unsigned alignment = 256 * num_se;
1943 /* The maximum size is 63.999 MB per SE. */
1944 unsigned max_size = ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se;
1945 struct radv_shader_info *gs_info = &pipeline->shaders[MESA_SHADER_GEOMETRY]->info;
1946
1947 /* Calculate the minimum size. */
1948 unsigned min_esgs_ring_size = align(gs->vgt_esgs_ring_itemsize * 4 * gs_vertex_reuse *
1949 wave_size, alignment);
1950 /* These are recommended sizes, not minimum sizes. */
1951 unsigned esgs_ring_size = max_gs_waves * 2 * wave_size *
1952 gs->vgt_esgs_ring_itemsize * 4 * gs_info->gs.vertices_in;
1953 unsigned gsvs_ring_size = max_gs_waves * 2 * wave_size *
1954 gs_info->gs.max_gsvs_emit_size;
1955
1956 min_esgs_ring_size = align(min_esgs_ring_size, alignment);
1957 esgs_ring_size = align(esgs_ring_size, alignment);
1958 gsvs_ring_size = align(gsvs_ring_size, alignment);
1959
1960 if (pipeline->device->physical_device->rad_info.chip_class <= GFX8)
1961 pipeline->graphics.esgs_ring_size = CLAMP(esgs_ring_size, min_esgs_ring_size, max_size);
1962
1963 pipeline->graphics.gsvs_ring_size = MIN2(gsvs_ring_size, max_size);
1964 }
1965
1966 struct radv_shader_variant *
1967 radv_get_shader(struct radv_pipeline *pipeline,
1968 gl_shader_stage stage)
1969 {
1970 if (stage == MESA_SHADER_VERTEX) {
1971 if (pipeline->shaders[MESA_SHADER_VERTEX])
1972 return pipeline->shaders[MESA_SHADER_VERTEX];
1973 if (pipeline->shaders[MESA_SHADER_TESS_CTRL])
1974 return pipeline->shaders[MESA_SHADER_TESS_CTRL];
1975 if (pipeline->shaders[MESA_SHADER_GEOMETRY])
1976 return pipeline->shaders[MESA_SHADER_GEOMETRY];
1977 } else if (stage == MESA_SHADER_TESS_EVAL) {
1978 if (!radv_pipeline_has_tess(pipeline))
1979 return NULL;
1980 if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
1981 return pipeline->shaders[MESA_SHADER_TESS_EVAL];
1982 if (pipeline->shaders[MESA_SHADER_GEOMETRY])
1983 return pipeline->shaders[MESA_SHADER_GEOMETRY];
1984 }
1985 return pipeline->shaders[stage];
1986 }
1987
1988 static struct radv_tessellation_state
1989 calculate_tess_state(struct radv_pipeline *pipeline,
1990 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1991 {
1992 unsigned num_tcs_input_cp;
1993 unsigned num_tcs_output_cp;
1994 unsigned num_patches;
1995 struct radv_tessellation_state tess = {0};
1996
1997 num_tcs_input_cp = pCreateInfo->pTessellationState->patchControlPoints;
1998 num_tcs_output_cp = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.tcs_vertices_out; //TCS VERTICES OUT
1999 num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2000
2001 tess.num_lds_blocks = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_lds_blocks;
2002
2003 tess.ls_hs_config = S_028B58_NUM_PATCHES(num_patches) |
2004 S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp) |
2005 S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp);
2006
2007 struct radv_shader_variant *tes = radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL);
2008 unsigned type = 0, partitioning = 0, topology = 0, distribution_mode = 0;
2009
2010 switch (tes->info.tes.primitive_mode) {
2011 case GL_TRIANGLES:
2012 type = V_028B6C_TESS_TRIANGLE;
2013 break;
2014 case GL_QUADS:
2015 type = V_028B6C_TESS_QUAD;
2016 break;
2017 case GL_ISOLINES:
2018 type = V_028B6C_TESS_ISOLINE;
2019 break;
2020 }
2021
2022 switch (tes->info.tes.spacing) {
2023 case TESS_SPACING_EQUAL:
2024 partitioning = V_028B6C_PART_INTEGER;
2025 break;
2026 case TESS_SPACING_FRACTIONAL_ODD:
2027 partitioning = V_028B6C_PART_FRAC_ODD;
2028 break;
2029 case TESS_SPACING_FRACTIONAL_EVEN:
2030 partitioning = V_028B6C_PART_FRAC_EVEN;
2031 break;
2032 default:
2033 break;
2034 }
2035
2036 bool ccw = tes->info.tes.ccw;
2037 const VkPipelineTessellationDomainOriginStateCreateInfo *domain_origin_state =
2038 vk_find_struct_const(pCreateInfo->pTessellationState,
2039 PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO);
2040
2041 if (domain_origin_state && domain_origin_state->domainOrigin != VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT)
2042 ccw = !ccw;
2043
2044 if (tes->info.tes.point_mode)
2045 topology = V_028B6C_OUTPUT_POINT;
2046 else if (tes->info.tes.primitive_mode == GL_ISOLINES)
2047 topology = V_028B6C_OUTPUT_LINE;
2048 else if (ccw)
2049 topology = V_028B6C_OUTPUT_TRIANGLE_CCW;
2050 else
2051 topology = V_028B6C_OUTPUT_TRIANGLE_CW;
2052
2053 if (pipeline->device->physical_device->rad_info.has_distributed_tess) {
2054 if (pipeline->device->physical_device->rad_info.family == CHIP_FIJI ||
2055 pipeline->device->physical_device->rad_info.family >= CHIP_POLARIS10)
2056 distribution_mode = V_028B6C_DISTRIBUTION_MODE_TRAPEZOIDS;
2057 else
2058 distribution_mode = V_028B6C_DISTRIBUTION_MODE_DONUTS;
2059 } else
2060 distribution_mode = V_028B6C_DISTRIBUTION_MODE_NO_DIST;
2061
2062 tess.tf_param = S_028B6C_TYPE(type) |
2063 S_028B6C_PARTITIONING(partitioning) |
2064 S_028B6C_TOPOLOGY(topology) |
2065 S_028B6C_DISTRIBUTION_MODE(distribution_mode);
2066
2067 return tess;
2068 }
2069
2070 static const struct radv_vs_output_info *get_vs_output_info(const struct radv_pipeline *pipeline)
2071 {
2072 if (radv_pipeline_has_gs(pipeline))
2073 if (radv_pipeline_has_ngg(pipeline))
2074 return &pipeline->shaders[MESA_SHADER_GEOMETRY]->info.vs.outinfo;
2075 else
2076 return &pipeline->gs_copy_shader->info.vs.outinfo;
2077 else if (radv_pipeline_has_tess(pipeline))
2078 return &pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.outinfo;
2079 else
2080 return &pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.outinfo;
2081 }
2082
2083 static void
2084 radv_link_shaders(struct radv_pipeline *pipeline, nir_shader **shaders)
2085 {
2086 nir_shader* ordered_shaders[MESA_SHADER_STAGES];
2087 int shader_count = 0;
2088
2089 if(shaders[MESA_SHADER_FRAGMENT]) {
2090 ordered_shaders[shader_count++] = shaders[MESA_SHADER_FRAGMENT];
2091 }
2092 if(shaders[MESA_SHADER_GEOMETRY]) {
2093 ordered_shaders[shader_count++] = shaders[MESA_SHADER_GEOMETRY];
2094 }
2095 if(shaders[MESA_SHADER_TESS_EVAL]) {
2096 ordered_shaders[shader_count++] = shaders[MESA_SHADER_TESS_EVAL];
2097 }
2098 if(shaders[MESA_SHADER_TESS_CTRL]) {
2099 ordered_shaders[shader_count++] = shaders[MESA_SHADER_TESS_CTRL];
2100 }
2101 if(shaders[MESA_SHADER_VERTEX]) {
2102 ordered_shaders[shader_count++] = shaders[MESA_SHADER_VERTEX];
2103 }
2104
2105 if (shader_count > 1) {
2106 unsigned first = ordered_shaders[shader_count - 1]->info.stage;
2107 unsigned last = ordered_shaders[0]->info.stage;
2108
2109 if (ordered_shaders[0]->info.stage == MESA_SHADER_FRAGMENT &&
2110 ordered_shaders[1]->info.has_transform_feedback_varyings)
2111 nir_link_xfb_varyings(ordered_shaders[1], ordered_shaders[0]);
2112
2113 for (int i = 0; i < shader_count; ++i) {
2114 nir_variable_mode mask = 0;
2115
2116 if (ordered_shaders[i]->info.stage != first)
2117 mask = mask | nir_var_shader_in;
2118
2119 if (ordered_shaders[i]->info.stage != last)
2120 mask = mask | nir_var_shader_out;
2121
2122 nir_lower_io_to_scalar_early(ordered_shaders[i], mask);
2123 radv_optimize_nir(ordered_shaders[i], false, false);
2124 }
2125 }
2126
2127 for (int i = 1; i < shader_count; ++i) {
2128 nir_lower_io_arrays_to_elements(ordered_shaders[i],
2129 ordered_shaders[i - 1]);
2130
2131 if (nir_link_opt_varyings(ordered_shaders[i],
2132 ordered_shaders[i - 1]))
2133 radv_optimize_nir(ordered_shaders[i - 1], false, false);
2134
2135 nir_remove_dead_variables(ordered_shaders[i],
2136 nir_var_shader_out, NULL);
2137 nir_remove_dead_variables(ordered_shaders[i - 1],
2138 nir_var_shader_in, NULL);
2139
2140 bool progress = nir_remove_unused_varyings(ordered_shaders[i],
2141 ordered_shaders[i - 1]);
2142
2143 nir_compact_varyings(ordered_shaders[i],
2144 ordered_shaders[i - 1], true);
2145
2146 if (progress) {
2147 if (nir_lower_global_vars_to_local(ordered_shaders[i])) {
2148 ac_lower_indirect_derefs(ordered_shaders[i],
2149 pipeline->device->physical_device->rad_info.chip_class);
2150 }
2151 radv_optimize_nir(ordered_shaders[i], false, false);
2152
2153 if (nir_lower_global_vars_to_local(ordered_shaders[i - 1])) {
2154 ac_lower_indirect_derefs(ordered_shaders[i - 1],
2155 pipeline->device->physical_device->rad_info.chip_class);
2156 }
2157 radv_optimize_nir(ordered_shaders[i - 1], false, false);
2158 }
2159 }
2160 }
2161
2162 static void
2163 radv_set_linked_driver_locations(struct radv_pipeline *pipeline, nir_shader **shaders,
2164 struct radv_shader_info infos[MESA_SHADER_STAGES])
2165 {
2166 bool has_tess = shaders[MESA_SHADER_TESS_CTRL];
2167 bool has_gs = shaders[MESA_SHADER_GEOMETRY];
2168
2169 if (!has_tess && !has_gs)
2170 return;
2171
2172 unsigned vs_info_idx = MESA_SHADER_VERTEX;
2173 unsigned tes_info_idx = MESA_SHADER_TESS_EVAL;
2174
2175 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {
2176 /* These are merged into the next stage */
2177 vs_info_idx = has_tess ? MESA_SHADER_TESS_CTRL : MESA_SHADER_GEOMETRY;
2178 tes_info_idx = has_gs ? MESA_SHADER_GEOMETRY : MESA_SHADER_TESS_EVAL;
2179 }
2180
2181 if (has_tess) {
2182 nir_linked_io_var_info vs2tcs =
2183 nir_assign_linked_io_var_locations(shaders[MESA_SHADER_VERTEX], shaders[MESA_SHADER_TESS_CTRL]);
2184 nir_linked_io_var_info tcs2tes =
2185 nir_assign_linked_io_var_locations(shaders[MESA_SHADER_TESS_CTRL], shaders[MESA_SHADER_TESS_EVAL]);
2186
2187 infos[vs_info_idx].vs.num_linked_outputs = vs2tcs.num_linked_io_vars;
2188 infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_inputs = vs2tcs.num_linked_io_vars;
2189 infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_outputs = tcs2tes.num_linked_io_vars;
2190 infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_patch_outputs = tcs2tes.num_linked_patch_io_vars;
2191 infos[tes_info_idx].tes.num_linked_inputs = tcs2tes.num_linked_io_vars;
2192 infos[tes_info_idx].tes.num_linked_patch_inputs = tcs2tes.num_linked_patch_io_vars;
2193
2194 if (has_gs) {
2195 nir_linked_io_var_info tes2gs =
2196 nir_assign_linked_io_var_locations(shaders[MESA_SHADER_TESS_EVAL], shaders[MESA_SHADER_GEOMETRY]);
2197
2198 infos[tes_info_idx].tes.num_linked_outputs = tes2gs.num_linked_io_vars;
2199 infos[MESA_SHADER_GEOMETRY].gs.num_linked_inputs = tes2gs.num_linked_io_vars;
2200 }
2201 } else if (has_gs) {
2202 nir_linked_io_var_info vs2gs =
2203 nir_assign_linked_io_var_locations(shaders[MESA_SHADER_VERTEX], shaders[MESA_SHADER_GEOMETRY]);
2204
2205 infos[vs_info_idx].vs.num_linked_outputs = vs2gs.num_linked_io_vars;
2206 infos[MESA_SHADER_GEOMETRY].gs.num_linked_inputs = vs2gs.num_linked_io_vars;
2207 }
2208 }
2209
2210 static uint32_t
2211 radv_get_attrib_stride(const VkPipelineVertexInputStateCreateInfo *input_state,
2212 uint32_t attrib_binding)
2213 {
2214 for (uint32_t i = 0; i < input_state->vertexBindingDescriptionCount; i++) {
2215 const VkVertexInputBindingDescription *input_binding =
2216 &input_state->pVertexBindingDescriptions[i];
2217
2218 if (input_binding->binding == attrib_binding)
2219 return input_binding->stride;
2220 }
2221
2222 return 0;
2223 }
2224
2225 static struct radv_pipeline_key
2226 radv_generate_graphics_pipeline_key(struct radv_pipeline *pipeline,
2227 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2228 const struct radv_blend_state *blend)
2229 {
2230 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
2231 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
2232 const VkPipelineVertexInputStateCreateInfo *input_state =
2233 pCreateInfo->pVertexInputState;
2234 const VkPipelineVertexInputDivisorStateCreateInfoEXT *divisor_state =
2235 vk_find_struct_const(input_state->pNext, PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT);
2236
2237 struct radv_pipeline_key key;
2238 memset(&key, 0, sizeof(key));
2239
2240 if (pCreateInfo->flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT)
2241 key.optimisations_disabled = 1;
2242
2243 key.has_multiview_view_index = !!subpass->view_mask;
2244
2245 uint32_t binding_input_rate = 0;
2246 uint32_t instance_rate_divisors[MAX_VERTEX_ATTRIBS];
2247 for (unsigned i = 0; i < input_state->vertexBindingDescriptionCount; ++i) {
2248 if (input_state->pVertexBindingDescriptions[i].inputRate) {
2249 unsigned binding = input_state->pVertexBindingDescriptions[i].binding;
2250 binding_input_rate |= 1u << binding;
2251 instance_rate_divisors[binding] = 1;
2252 }
2253 }
2254 if (divisor_state) {
2255 for (unsigned i = 0; i < divisor_state->vertexBindingDivisorCount; ++i) {
2256 instance_rate_divisors[divisor_state->pVertexBindingDivisors[i].binding] =
2257 divisor_state->pVertexBindingDivisors[i].divisor;
2258 }
2259 }
2260
2261 for (unsigned i = 0; i < input_state->vertexAttributeDescriptionCount; ++i) {
2262 const VkVertexInputAttributeDescription *desc =
2263 &input_state->pVertexAttributeDescriptions[i];
2264 const struct vk_format_description *format_desc;
2265 unsigned location = desc->location;
2266 unsigned binding = desc->binding;
2267 unsigned num_format, data_format;
2268 int first_non_void;
2269
2270 if (binding_input_rate & (1u << binding)) {
2271 key.instance_rate_inputs |= 1u << location;
2272 key.instance_rate_divisors[location] = instance_rate_divisors[binding];
2273 }
2274
2275 format_desc = vk_format_description(desc->format);
2276 first_non_void = vk_format_get_first_non_void_channel(desc->format);
2277
2278 num_format = radv_translate_buffer_numformat(format_desc, first_non_void);
2279 data_format = radv_translate_buffer_dataformat(format_desc, first_non_void);
2280
2281 key.vertex_attribute_formats[location] = data_format | (num_format << 4);
2282 key.vertex_attribute_bindings[location] = desc->binding;
2283 key.vertex_attribute_offsets[location] = desc->offset;
2284 key.vertex_attribute_strides[location] = radv_get_attrib_stride(input_state, desc->binding);
2285
2286 if (pipeline->device->physical_device->rad_info.chip_class <= GFX8 &&
2287 pipeline->device->physical_device->rad_info.family != CHIP_STONEY) {
2288 VkFormat format = input_state->pVertexAttributeDescriptions[i].format;
2289 uint64_t adjust;
2290 switch(format) {
2291 case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
2292 case VK_FORMAT_A2B10G10R10_SNORM_PACK32:
2293 adjust = RADV_ALPHA_ADJUST_SNORM;
2294 break;
2295 case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
2296 case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:
2297 adjust = RADV_ALPHA_ADJUST_SSCALED;
2298 break;
2299 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
2300 case VK_FORMAT_A2B10G10R10_SINT_PACK32:
2301 adjust = RADV_ALPHA_ADJUST_SINT;
2302 break;
2303 default:
2304 adjust = 0;
2305 break;
2306 }
2307 key.vertex_alpha_adjust |= adjust << (2 * location);
2308 }
2309
2310 switch (desc->format) {
2311 case VK_FORMAT_B8G8R8A8_UNORM:
2312 case VK_FORMAT_B8G8R8A8_SNORM:
2313 case VK_FORMAT_B8G8R8A8_USCALED:
2314 case VK_FORMAT_B8G8R8A8_SSCALED:
2315 case VK_FORMAT_B8G8R8A8_UINT:
2316 case VK_FORMAT_B8G8R8A8_SINT:
2317 case VK_FORMAT_B8G8R8A8_SRGB:
2318 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
2319 case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
2320 case VK_FORMAT_A2R10G10B10_USCALED_PACK32:
2321 case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
2322 case VK_FORMAT_A2R10G10B10_UINT_PACK32:
2323 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
2324 key.vertex_post_shuffle |= 1 << location;
2325 break;
2326 default:
2327 break;
2328 }
2329 }
2330
2331 const VkPipelineTessellationStateCreateInfo *tess =
2332 radv_pipeline_get_tessellation_state(pCreateInfo);
2333 if (tess)
2334 key.tess_input_vertices = tess->patchControlPoints;
2335
2336 const VkPipelineMultisampleStateCreateInfo *vkms =
2337 radv_pipeline_get_multisample_state(pCreateInfo);
2338 if (vkms && vkms->rasterizationSamples > 1) {
2339 uint32_t num_samples = vkms->rasterizationSamples;
2340 uint32_t ps_iter_samples = radv_pipeline_get_ps_iter_samples(pCreateInfo);
2341 key.num_samples = num_samples;
2342 key.log2_ps_iter_samples = util_logbase2(ps_iter_samples);
2343 }
2344
2345 key.col_format = blend->spi_shader_col_format;
2346 key.is_dual_src = blend->mrt0_is_dual_src;
2347 if (pipeline->device->physical_device->rad_info.chip_class < GFX8) {
2348 key.is_int8 = blend->col_format_is_int8;
2349 key.is_int10 = blend->col_format_is_int10;
2350 }
2351
2352 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10)
2353 key.topology = pCreateInfo->pInputAssemblyState->topology;
2354
2355 return key;
2356 }
2357
2358 static bool
2359 radv_nir_stage_uses_xfb(const nir_shader *nir)
2360 {
2361 nir_xfb_info *xfb = nir_gather_xfb_info(nir, NULL);
2362 bool uses_xfb = !!xfb;
2363
2364 ralloc_free(xfb);
2365 return uses_xfb;
2366 }
2367
2368 static void
2369 radv_fill_shader_keys(struct radv_device *device,
2370 struct radv_shader_variant_key *keys,
2371 const struct radv_pipeline_key *key,
2372 nir_shader **nir)
2373 {
2374 keys[MESA_SHADER_VERTEX].vs.instance_rate_inputs = key->instance_rate_inputs;
2375 keys[MESA_SHADER_VERTEX].vs.alpha_adjust = key->vertex_alpha_adjust;
2376 keys[MESA_SHADER_VERTEX].vs.post_shuffle = key->vertex_post_shuffle;
2377 for (unsigned i = 0; i < MAX_VERTEX_ATTRIBS; ++i) {
2378 keys[MESA_SHADER_VERTEX].vs.instance_rate_divisors[i] = key->instance_rate_divisors[i];
2379 keys[MESA_SHADER_VERTEX].vs.vertex_attribute_formats[i] = key->vertex_attribute_formats[i];
2380 keys[MESA_SHADER_VERTEX].vs.vertex_attribute_bindings[i] = key->vertex_attribute_bindings[i];
2381 keys[MESA_SHADER_VERTEX].vs.vertex_attribute_offsets[i] = key->vertex_attribute_offsets[i];
2382 keys[MESA_SHADER_VERTEX].vs.vertex_attribute_strides[i] = key->vertex_attribute_strides[i];
2383 }
2384 keys[MESA_SHADER_VERTEX].vs.outprim = si_conv_prim_to_gs_out(key->topology);
2385
2386 if (nir[MESA_SHADER_TESS_CTRL]) {
2387 keys[MESA_SHADER_VERTEX].vs_common_out.as_ls = true;
2388 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs = 0;
2389 keys[MESA_SHADER_TESS_CTRL].tcs.input_vertices = key->tess_input_vertices;
2390 keys[MESA_SHADER_TESS_CTRL].tcs.primitive_mode = nir[MESA_SHADER_TESS_EVAL]->info.tess.primitive_mode;
2391
2392 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));
2393 }
2394
2395 if (nir[MESA_SHADER_GEOMETRY]) {
2396 if (nir[MESA_SHADER_TESS_CTRL])
2397 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_es = true;
2398 else
2399 keys[MESA_SHADER_VERTEX].vs_common_out.as_es = true;
2400 }
2401
2402 if (device->physical_device->use_ngg) {
2403 if (nir[MESA_SHADER_TESS_CTRL]) {
2404 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = true;
2405 } else {
2406 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = true;
2407 }
2408
2409 if (nir[MESA_SHADER_TESS_CTRL] &&
2410 nir[MESA_SHADER_GEOMETRY] &&
2411 nir[MESA_SHADER_GEOMETRY]->info.gs.invocations *
2412 nir[MESA_SHADER_GEOMETRY]->info.gs.vertices_out > 256) {
2413 /* Fallback to the legacy path if tessellation is
2414 * enabled with extreme geometry because
2415 * EN_MAX_VERT_OUT_PER_GS_INSTANCE doesn't work and it
2416 * might hang.
2417 */
2418 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;
2419 }
2420
2421 if (!device->physical_device->use_ngg_gs) {
2422 if (nir[MESA_SHADER_GEOMETRY]) {
2423 if (nir[MESA_SHADER_TESS_CTRL])
2424 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;
2425 else
2426 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = false;
2427 }
2428 }
2429
2430 gl_shader_stage last_xfb_stage = MESA_SHADER_VERTEX;
2431
2432 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2433 if (nir[i])
2434 last_xfb_stage = i;
2435 }
2436
2437 bool uses_xfb = nir[last_xfb_stage] &&
2438 radv_nir_stage_uses_xfb(nir[last_xfb_stage]);
2439
2440 if (!device->physical_device->use_ngg_streamout && uses_xfb) {
2441 if (nir[MESA_SHADER_TESS_CTRL])
2442 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;
2443 else
2444 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = false;
2445 }
2446
2447 /* Determine if the pipeline is eligible for the NGG passthrough
2448 * mode. It can't be enabled for geometry shaders, for NGG
2449 * streamout or for vertex shaders that export the primitive ID
2450 * (this is checked later because we don't have the info here.)
2451 */
2452 if (!nir[MESA_SHADER_GEOMETRY] && !uses_xfb) {
2453 if (nir[MESA_SHADER_TESS_CTRL] &&
2454 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg) {
2455 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg_passthrough = true;
2456 } else if (nir[MESA_SHADER_VERTEX] &&
2457 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg) {
2458 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg_passthrough = true;
2459 }
2460 }
2461 }
2462
2463 for(int i = 0; i < MESA_SHADER_STAGES; ++i)
2464 keys[i].has_multiview_view_index = key->has_multiview_view_index;
2465
2466 keys[MESA_SHADER_FRAGMENT].fs.col_format = key->col_format;
2467 keys[MESA_SHADER_FRAGMENT].fs.is_int8 = key->is_int8;
2468 keys[MESA_SHADER_FRAGMENT].fs.is_int10 = key->is_int10;
2469 keys[MESA_SHADER_FRAGMENT].fs.log2_ps_iter_samples = key->log2_ps_iter_samples;
2470 keys[MESA_SHADER_FRAGMENT].fs.num_samples = key->num_samples;
2471 keys[MESA_SHADER_FRAGMENT].fs.is_dual_src = key->is_dual_src;
2472
2473 if (nir[MESA_SHADER_COMPUTE]) {
2474 keys[MESA_SHADER_COMPUTE].cs.subgroup_size = key->compute_subgroup_size;
2475 }
2476 }
2477
2478 static uint8_t
2479 radv_get_wave_size(struct radv_device *device,
2480 const VkPipelineShaderStageCreateInfo *pStage,
2481 gl_shader_stage stage,
2482 const struct radv_shader_variant_key *key)
2483 {
2484 if (stage == MESA_SHADER_GEOMETRY && !key->vs_common_out.as_ngg)
2485 return 64;
2486 else if (stage == MESA_SHADER_COMPUTE) {
2487 if (key->cs.subgroup_size) {
2488 /* Return the required subgroup size if specified. */
2489 return key->cs.subgroup_size;
2490 }
2491 return device->physical_device->cs_wave_size;
2492 }
2493 else if (stage == MESA_SHADER_FRAGMENT)
2494 return device->physical_device->ps_wave_size;
2495 else
2496 return device->physical_device->ge_wave_size;
2497 }
2498
2499 static uint8_t
2500 radv_get_ballot_bit_size(struct radv_device *device,
2501 const VkPipelineShaderStageCreateInfo *pStage,
2502 gl_shader_stage stage,
2503 const struct radv_shader_variant_key *key)
2504 {
2505 if (stage == MESA_SHADER_COMPUTE && key->cs.subgroup_size)
2506 return key->cs.subgroup_size;
2507 return 64;
2508 }
2509
2510 static void
2511 radv_fill_shader_info(struct radv_pipeline *pipeline,
2512 const VkPipelineShaderStageCreateInfo **pStages,
2513 struct radv_shader_variant_key *keys,
2514 struct radv_shader_info *infos,
2515 nir_shader **nir)
2516 {
2517 unsigned active_stages = 0;
2518 unsigned filled_stages = 0;
2519
2520 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
2521 if (nir[i])
2522 active_stages |= (1 << i);
2523 }
2524
2525 if (nir[MESA_SHADER_FRAGMENT]) {
2526 radv_nir_shader_info_init(&infos[MESA_SHADER_FRAGMENT]);
2527 radv_nir_shader_info_pass(nir[MESA_SHADER_FRAGMENT],
2528 pipeline->layout,
2529 &keys[MESA_SHADER_FRAGMENT],
2530 &infos[MESA_SHADER_FRAGMENT],
2531 pipeline->device->physical_device->use_llvm);
2532
2533 /* TODO: These are no longer used as keys we should refactor this */
2534 keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id =
2535 infos[MESA_SHADER_FRAGMENT].ps.prim_id_input;
2536 keys[MESA_SHADER_VERTEX].vs_common_out.export_layer_id =
2537 infos[MESA_SHADER_FRAGMENT].ps.layer_input;
2538 keys[MESA_SHADER_VERTEX].vs_common_out.export_clip_dists =
2539 !!infos[MESA_SHADER_FRAGMENT].ps.num_input_clips_culls;
2540 keys[MESA_SHADER_VERTEX].vs_common_out.export_viewport_index =
2541 infos[MESA_SHADER_FRAGMENT].ps.viewport_index_input;
2542 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_prim_id =
2543 infos[MESA_SHADER_FRAGMENT].ps.prim_id_input;
2544 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_layer_id =
2545 infos[MESA_SHADER_FRAGMENT].ps.layer_input;
2546 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_clip_dists =
2547 !!infos[MESA_SHADER_FRAGMENT].ps.num_input_clips_culls;
2548 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_viewport_index =
2549 infos[MESA_SHADER_FRAGMENT].ps.viewport_index_input;
2550
2551 /* NGG passthrough mode can't be enabled for vertex shaders
2552 * that export the primitive ID.
2553 *
2554 * TODO: I should really refactor the keys logic.
2555 */
2556 if (nir[MESA_SHADER_VERTEX] &&
2557 keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id) {
2558 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg_passthrough = false;
2559 }
2560
2561 filled_stages |= (1 << MESA_SHADER_FRAGMENT);
2562 }
2563
2564 if (nir[MESA_SHADER_TESS_CTRL]) {
2565 infos[MESA_SHADER_TESS_CTRL].tcs.tes_inputs_read =
2566 nir[MESA_SHADER_TESS_EVAL]->info.inputs_read;
2567 infos[MESA_SHADER_TESS_CTRL].tcs.tes_patch_inputs_read =
2568 nir[MESA_SHADER_TESS_EVAL]->info.patch_inputs_read;
2569 }
2570
2571 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9 &&
2572 nir[MESA_SHADER_TESS_CTRL]) {
2573 struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};
2574 struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];
2575 key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;
2576
2577 radv_nir_shader_info_init(&infos[MESA_SHADER_TESS_CTRL]);
2578
2579 for (int i = 0; i < 2; i++) {
2580 radv_nir_shader_info_pass(combined_nir[i],
2581 pipeline->layout, &key,
2582 &infos[MESA_SHADER_TESS_CTRL],
2583 pipeline->device->physical_device->use_llvm);
2584 }
2585
2586 keys[MESA_SHADER_TESS_EVAL].tes.num_patches =
2587 infos[MESA_SHADER_TESS_CTRL].tcs.num_patches;
2588 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs =
2589 util_last_bit64(infos[MESA_SHADER_TESS_CTRL].tcs.outputs_written);
2590
2591 filled_stages |= (1 << MESA_SHADER_VERTEX);
2592 filled_stages |= (1 << MESA_SHADER_TESS_CTRL);
2593 }
2594
2595 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9 &&
2596 nir[MESA_SHADER_GEOMETRY]) {
2597 gl_shader_stage pre_stage = nir[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2598 struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};
2599
2600 radv_nir_shader_info_init(&infos[MESA_SHADER_GEOMETRY]);
2601
2602 for (int i = 0; i < 2; i++) {
2603 radv_nir_shader_info_pass(combined_nir[i],
2604 pipeline->layout,
2605 &keys[pre_stage],
2606 &infos[MESA_SHADER_GEOMETRY],
2607 pipeline->device->physical_device->use_llvm);
2608 }
2609
2610 filled_stages |= (1 << pre_stage);
2611 filled_stages |= (1 << MESA_SHADER_GEOMETRY);
2612 }
2613
2614 active_stages ^= filled_stages;
2615 while (active_stages) {
2616 int i = u_bit_scan(&active_stages);
2617
2618 if (i == MESA_SHADER_TESS_CTRL) {
2619 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs =
2620 util_last_bit64(infos[MESA_SHADER_VERTEX].vs.ls_outputs_written);
2621 }
2622
2623 if (i == MESA_SHADER_TESS_EVAL) {
2624 keys[MESA_SHADER_TESS_EVAL].tes.num_patches =
2625 infos[MESA_SHADER_TESS_CTRL].tcs.num_patches;
2626 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs =
2627 util_last_bit64(infos[MESA_SHADER_TESS_CTRL].tcs.outputs_written);
2628 }
2629
2630 radv_nir_shader_info_init(&infos[i]);
2631 radv_nir_shader_info_pass(nir[i], pipeline->layout,
2632 &keys[i], &infos[i], pipeline->device->physical_device->use_llvm);
2633 }
2634
2635 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
2636 if (nir[i]) {
2637 infos[i].wave_size =
2638 radv_get_wave_size(pipeline->device, pStages[i],
2639 i, &keys[i]);
2640 infos[i].ballot_bit_size =
2641 radv_get_ballot_bit_size(pipeline->device,
2642 pStages[i], i,
2643 &keys[i]);
2644 }
2645 }
2646 }
2647
2648 static void
2649 merge_tess_info(struct shader_info *tes_info,
2650 const struct shader_info *tcs_info)
2651 {
2652 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
2653 *
2654 * "PointMode. Controls generation of points rather than triangles
2655 * or lines. This functionality defaults to disabled, and is
2656 * enabled if either shader stage includes the execution mode.
2657 *
2658 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
2659 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
2660 * and OutputVertices, it says:
2661 *
2662 * "One mode must be set in at least one of the tessellation
2663 * shader stages."
2664 *
2665 * So, the fields can be set in either the TCS or TES, but they must
2666 * agree if set in both. Our backend looks at TES, so bitwise-or in
2667 * the values from the TCS.
2668 */
2669 assert(tcs_info->tess.tcs_vertices_out == 0 ||
2670 tes_info->tess.tcs_vertices_out == 0 ||
2671 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
2672 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
2673
2674 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
2675 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
2676 tcs_info->tess.spacing == tes_info->tess.spacing);
2677 tes_info->tess.spacing |= tcs_info->tess.spacing;
2678
2679 assert(tcs_info->tess.primitive_mode == 0 ||
2680 tes_info->tess.primitive_mode == 0 ||
2681 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
2682 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
2683 tes_info->tess.ccw |= tcs_info->tess.ccw;
2684 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
2685 }
2686
2687 static
2688 void radv_init_feedback(const VkPipelineCreationFeedbackCreateInfoEXT *ext)
2689 {
2690 if (!ext)
2691 return;
2692
2693 if (ext->pPipelineCreationFeedback) {
2694 ext->pPipelineCreationFeedback->flags = 0;
2695 ext->pPipelineCreationFeedback->duration = 0;
2696 }
2697
2698 for (unsigned i = 0; i < ext->pipelineStageCreationFeedbackCount; ++i) {
2699 ext->pPipelineStageCreationFeedbacks[i].flags = 0;
2700 ext->pPipelineStageCreationFeedbacks[i].duration = 0;
2701 }
2702 }
2703
2704 static
2705 void radv_start_feedback(VkPipelineCreationFeedbackEXT *feedback)
2706 {
2707 if (!feedback)
2708 return;
2709
2710 feedback->duration -= radv_get_current_time();
2711 feedback ->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT;
2712 }
2713
2714 static
2715 void radv_stop_feedback(VkPipelineCreationFeedbackEXT *feedback, bool cache_hit)
2716 {
2717 if (!feedback)
2718 return;
2719
2720 feedback->duration += radv_get_current_time();
2721 feedback ->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT |
2722 (cache_hit ? VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT : 0);
2723 }
2724
2725 VkResult radv_create_shaders(struct radv_pipeline *pipeline,
2726 struct radv_device *device,
2727 struct radv_pipeline_cache *cache,
2728 const struct radv_pipeline_key *key,
2729 const VkPipelineShaderStageCreateInfo **pStages,
2730 const VkPipelineCreateFlags flags,
2731 VkPipelineCreationFeedbackEXT *pipeline_feedback,
2732 VkPipelineCreationFeedbackEXT **stage_feedbacks)
2733 {
2734 struct radv_shader_module fs_m = {0};
2735 struct radv_shader_module *modules[MESA_SHADER_STAGES] = { 0, };
2736 nir_shader *nir[MESA_SHADER_STAGES] = {0};
2737 struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};
2738 struct radv_shader_variant_key keys[MESA_SHADER_STAGES] = {{{{{0}}}}};
2739 struct radv_shader_info infos[MESA_SHADER_STAGES] = {0};
2740 unsigned char hash[20], gs_copy_hash[20];
2741 bool keep_executable_info = (flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR) || device->keep_shader_info;
2742 bool keep_statistic_info = (flags & VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR) ||
2743 (device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS) ||
2744 device->keep_shader_info;
2745
2746 radv_start_feedback(pipeline_feedback);
2747
2748 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
2749 if (pStages[i]) {
2750 modules[i] = radv_shader_module_from_handle(pStages[i]->module);
2751 if (modules[i]->nir)
2752 _mesa_sha1_compute(modules[i]->nir->info.name,
2753 strlen(modules[i]->nir->info.name),
2754 modules[i]->sha1);
2755
2756 pipeline->active_stages |= mesa_to_vk_shader_stage(i);
2757 }
2758 }
2759
2760 radv_hash_shaders(hash, pStages, pipeline->layout, key, get_hash_flags(device));
2761 memcpy(gs_copy_hash, hash, 20);
2762 gs_copy_hash[0] ^= 1;
2763
2764 bool found_in_application_cache = true;
2765 if (modules[MESA_SHADER_GEOMETRY] && !keep_executable_info && !keep_statistic_info) {
2766 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2767 radv_create_shader_variants_from_pipeline_cache(device, cache, gs_copy_hash, variants,
2768 &found_in_application_cache);
2769 pipeline->gs_copy_shader = variants[MESA_SHADER_GEOMETRY];
2770 }
2771
2772 if (!keep_executable_info && !keep_statistic_info &&
2773 radv_create_shader_variants_from_pipeline_cache(device, cache, hash, pipeline->shaders,
2774 &found_in_application_cache) &&
2775 (!modules[MESA_SHADER_GEOMETRY] || pipeline->gs_copy_shader)) {
2776 radv_stop_feedback(pipeline_feedback, found_in_application_cache);
2777 return VK_SUCCESS;
2778 }
2779
2780 if (flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT) {
2781 radv_stop_feedback(pipeline_feedback, found_in_application_cache);
2782 return VK_PIPELINE_COMPILE_REQUIRED_EXT;
2783 }
2784
2785 if (!modules[MESA_SHADER_FRAGMENT] && !modules[MESA_SHADER_COMPUTE]) {
2786 nir_builder fs_b;
2787 nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
2788 fs_b.shader->info.name = ralloc_strdup(fs_b.shader, "noop_fs");
2789 fs_m.nir = fs_b.shader;
2790 modules[MESA_SHADER_FRAGMENT] = &fs_m;
2791 }
2792
2793 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
2794 const VkPipelineShaderStageCreateInfo *stage = pStages[i];
2795 unsigned subgroup_size = 64, ballot_bit_size = 64;
2796
2797 if (!modules[i])
2798 continue;
2799
2800 radv_start_feedback(stage_feedbacks[i]);
2801
2802 if (key->compute_subgroup_size) {
2803 /* Only compute shaders currently support requiring a
2804 * specific subgroup size.
2805 */
2806 assert(i == MESA_SHADER_COMPUTE);
2807 subgroup_size = key->compute_subgroup_size;
2808 ballot_bit_size = key->compute_subgroup_size;
2809 }
2810
2811 nir[i] = radv_shader_compile_to_nir(device, modules[i],
2812 stage ? stage->pName : "main", i,
2813 stage ? stage->pSpecializationInfo : NULL,
2814 flags, pipeline->layout,
2815 subgroup_size, ballot_bit_size);
2816
2817 /* We don't want to alter meta shaders IR directly so clone it
2818 * first.
2819 */
2820 if (nir[i]->info.name) {
2821 nir[i] = nir_shader_clone(NULL, nir[i]);
2822 }
2823
2824 radv_stop_feedback(stage_feedbacks[i], false);
2825 }
2826
2827 if (nir[MESA_SHADER_TESS_CTRL]) {
2828 nir_lower_patch_vertices(nir[MESA_SHADER_TESS_EVAL], nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out, NULL);
2829 merge_tess_info(&nir[MESA_SHADER_TESS_EVAL]->info, &nir[MESA_SHADER_TESS_CTRL]->info);
2830 }
2831
2832 if (!(flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT))
2833 radv_link_shaders(pipeline, nir);
2834
2835 radv_set_linked_driver_locations(pipeline, nir, infos);
2836
2837 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2838 if (nir[i]) {
2839 /* do this again since information such as outputs_read can be out-of-date */
2840 nir_shader_gather_info(nir[i], nir_shader_get_entrypoint(nir[i]));
2841
2842 if (device->physical_device->use_llvm) {
2843 NIR_PASS_V(nir[i], nir_lower_bool_to_int32);
2844 } else {
2845 NIR_PASS_V(nir[i], nir_lower_non_uniform_access,
2846 nir_lower_non_uniform_ubo_access |
2847 nir_lower_non_uniform_ssbo_access |
2848 nir_lower_non_uniform_texture_access |
2849 nir_lower_non_uniform_image_access);
2850 }
2851 }
2852 }
2853
2854 if (nir[MESA_SHADER_FRAGMENT])
2855 radv_lower_fs_io(nir[MESA_SHADER_FRAGMENT]);
2856
2857 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2858 if (radv_can_dump_shader(device, modules[i], false))
2859 nir_print_shader(nir[i], stderr);
2860 }
2861
2862 radv_fill_shader_keys(device, keys, key, nir);
2863
2864 radv_fill_shader_info(pipeline, pStages, keys, infos, nir);
2865
2866 if ((nir[MESA_SHADER_VERTEX] &&
2867 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg) ||
2868 (nir[MESA_SHADER_TESS_EVAL] &&
2869 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg)) {
2870 struct gfx10_ngg_info *ngg_info;
2871
2872 if (nir[MESA_SHADER_GEOMETRY])
2873 ngg_info = &infos[MESA_SHADER_GEOMETRY].ngg_info;
2874 else if (nir[MESA_SHADER_TESS_CTRL])
2875 ngg_info = &infos[MESA_SHADER_TESS_EVAL].ngg_info;
2876 else
2877 ngg_info = &infos[MESA_SHADER_VERTEX].ngg_info;
2878
2879 gfx10_get_ngg_info(key, pipeline, nir, infos, ngg_info);
2880 } else if (nir[MESA_SHADER_GEOMETRY]) {
2881 struct gfx9_gs_info *gs_info =
2882 &infos[MESA_SHADER_GEOMETRY].gs_ring_info;
2883
2884 gfx9_get_gs_info(key, pipeline, nir, infos, gs_info);
2885 }
2886
2887 if(modules[MESA_SHADER_GEOMETRY]) {
2888 struct radv_shader_binary *gs_copy_binary = NULL;
2889 if (!pipeline->gs_copy_shader &&
2890 !radv_pipeline_has_ngg(pipeline)) {
2891 struct radv_shader_info info = {};
2892 struct radv_shader_variant_key key = {};
2893
2894 key.has_multiview_view_index =
2895 keys[MESA_SHADER_GEOMETRY].has_multiview_view_index;
2896
2897 radv_nir_shader_info_pass(nir[MESA_SHADER_GEOMETRY],
2898 pipeline->layout, &key,
2899 &info, pipeline->device->physical_device->use_llvm);
2900 info.wave_size = 64; /* Wave32 not supported. */
2901 info.ballot_bit_size = 64;
2902
2903 pipeline->gs_copy_shader = radv_create_gs_copy_shader(
2904 device, nir[MESA_SHADER_GEOMETRY], &info,
2905 &gs_copy_binary, keep_executable_info, keep_statistic_info,
2906 keys[MESA_SHADER_GEOMETRY].has_multiview_view_index);
2907 }
2908
2909 if (!keep_executable_info && !keep_statistic_info && pipeline->gs_copy_shader) {
2910 struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};
2911 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2912
2913 binaries[MESA_SHADER_GEOMETRY] = gs_copy_binary;
2914 variants[MESA_SHADER_GEOMETRY] = pipeline->gs_copy_shader;
2915
2916 radv_pipeline_cache_insert_shaders(device, cache,
2917 gs_copy_hash,
2918 variants,
2919 binaries);
2920 }
2921 free(gs_copy_binary);
2922 }
2923
2924 if (nir[MESA_SHADER_FRAGMENT]) {
2925 if (!pipeline->shaders[MESA_SHADER_FRAGMENT]) {
2926 radv_start_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT]);
2927
2928 pipeline->shaders[MESA_SHADER_FRAGMENT] =
2929 radv_shader_variant_compile(device, modules[MESA_SHADER_FRAGMENT], &nir[MESA_SHADER_FRAGMENT], 1,
2930 pipeline->layout, keys + MESA_SHADER_FRAGMENT,
2931 infos + MESA_SHADER_FRAGMENT,
2932 keep_executable_info, keep_statistic_info,
2933 &binaries[MESA_SHADER_FRAGMENT]);
2934
2935 radv_stop_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT], false);
2936 }
2937 }
2938
2939 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_TESS_CTRL]) {
2940 if (!pipeline->shaders[MESA_SHADER_TESS_CTRL]) {
2941 struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};
2942 struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];
2943 key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;
2944
2945 radv_start_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL]);
2946
2947 pipeline->shaders[MESA_SHADER_TESS_CTRL] = radv_shader_variant_compile(device, modules[MESA_SHADER_TESS_CTRL], combined_nir, 2,
2948 pipeline->layout,
2949 &key, &infos[MESA_SHADER_TESS_CTRL], keep_executable_info,
2950 keep_statistic_info, &binaries[MESA_SHADER_TESS_CTRL]);
2951
2952 radv_stop_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL], false);
2953 }
2954 modules[MESA_SHADER_VERTEX] = NULL;
2955 keys[MESA_SHADER_TESS_EVAL].tes.num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2956 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.outputs_written);
2957 }
2958
2959 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_GEOMETRY]) {
2960 gl_shader_stage pre_stage = modules[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2961 if (!pipeline->shaders[MESA_SHADER_GEOMETRY]) {
2962 struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};
2963
2964 radv_start_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY]);
2965
2966 pipeline->shaders[MESA_SHADER_GEOMETRY] = radv_shader_variant_compile(device, modules[MESA_SHADER_GEOMETRY], combined_nir, 2,
2967 pipeline->layout,
2968 &keys[pre_stage], &infos[MESA_SHADER_GEOMETRY], keep_executable_info,
2969 keep_statistic_info, &binaries[MESA_SHADER_GEOMETRY]);
2970
2971 radv_stop_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY], false);
2972 }
2973 modules[pre_stage] = NULL;
2974 }
2975
2976 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2977 if(modules[i] && !pipeline->shaders[i]) {
2978 if (i == MESA_SHADER_TESS_CTRL) {
2979 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs = util_last_bit64(pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.ls_outputs_written);
2980 }
2981 if (i == MESA_SHADER_TESS_EVAL) {
2982 keys[MESA_SHADER_TESS_EVAL].tes.num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2983 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.outputs_written);
2984 }
2985
2986 radv_start_feedback(stage_feedbacks[i]);
2987
2988 pipeline->shaders[i] = radv_shader_variant_compile(device, modules[i], &nir[i], 1,
2989 pipeline->layout,
2990 keys + i, infos + i, keep_executable_info,
2991 keep_statistic_info, &binaries[i]);
2992
2993 radv_stop_feedback(stage_feedbacks[i], false);
2994 }
2995 }
2996
2997 if (!keep_executable_info && !keep_statistic_info) {
2998 radv_pipeline_cache_insert_shaders(device, cache, hash, pipeline->shaders,
2999 binaries);
3000 }
3001
3002 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
3003 free(binaries[i]);
3004 if (nir[i]) {
3005 ralloc_free(nir[i]);
3006
3007 if (radv_can_dump_shader_stats(device, modules[i]))
3008 radv_shader_dump_stats(device,
3009 pipeline->shaders[i],
3010 i, stderr);
3011 }
3012 }
3013
3014 if (fs_m.nir)
3015 ralloc_free(fs_m.nir);
3016
3017 radv_stop_feedback(pipeline_feedback, false);
3018 return VK_SUCCESS;
3019 }
3020
3021 static uint32_t
3022 radv_pipeline_stage_to_user_data_0(struct radv_pipeline *pipeline,
3023 gl_shader_stage stage, enum chip_class chip_class)
3024 {
3025 bool has_gs = radv_pipeline_has_gs(pipeline);
3026 bool has_tess = radv_pipeline_has_tess(pipeline);
3027 bool has_ngg = radv_pipeline_has_ngg(pipeline);
3028
3029 switch (stage) {
3030 case MESA_SHADER_FRAGMENT:
3031 return R_00B030_SPI_SHADER_USER_DATA_PS_0;
3032 case MESA_SHADER_VERTEX:
3033 if (has_tess) {
3034 if (chip_class >= GFX10) {
3035 return R_00B430_SPI_SHADER_USER_DATA_HS_0;
3036 } else if (chip_class == GFX9) {
3037 return R_00B430_SPI_SHADER_USER_DATA_LS_0;
3038 } else {
3039 return R_00B530_SPI_SHADER_USER_DATA_LS_0;
3040 }
3041
3042 }
3043
3044 if (has_gs) {
3045 if (chip_class >= GFX10) {
3046 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3047 } else {
3048 return R_00B330_SPI_SHADER_USER_DATA_ES_0;
3049 }
3050 }
3051
3052 if (has_ngg)
3053 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3054
3055 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
3056 case MESA_SHADER_GEOMETRY:
3057 return chip_class == GFX9 ? R_00B330_SPI_SHADER_USER_DATA_ES_0 :
3058 R_00B230_SPI_SHADER_USER_DATA_GS_0;
3059 case MESA_SHADER_COMPUTE:
3060 return R_00B900_COMPUTE_USER_DATA_0;
3061 case MESA_SHADER_TESS_CTRL:
3062 return chip_class == GFX9 ? R_00B430_SPI_SHADER_USER_DATA_LS_0 :
3063 R_00B430_SPI_SHADER_USER_DATA_HS_0;
3064 case MESA_SHADER_TESS_EVAL:
3065 if (has_gs) {
3066 return chip_class >= GFX10 ? R_00B230_SPI_SHADER_USER_DATA_GS_0 :
3067 R_00B330_SPI_SHADER_USER_DATA_ES_0;
3068 } else if (has_ngg) {
3069 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3070 } else {
3071 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
3072 }
3073 default:
3074 unreachable("unknown shader");
3075 }
3076 }
3077
3078 struct radv_bin_size_entry {
3079 unsigned bpp;