312611adc07125c630273a96359e6fb2d5b6cef9
[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 cb_shader_mask;
62 uint32_t db_alpha_to_mask;
63
64 uint32_t commutative_4bit;
65
66 bool single_cb_enable;
67 bool mrt0_is_dual_src;
68 };
69
70 struct radv_dsa_order_invariance {
71 /* Whether the final result in Z/S buffers is guaranteed to be
72 * invariant under changes to the order in which fragments arrive.
73 */
74 bool zs;
75
76 /* Whether the set of fragments that pass the combined Z/S test is
77 * guaranteed to be invariant under changes to the order in which
78 * fragments arrive.
79 */
80 bool pass_set;
81 };
82
83 struct radv_tessellation_state {
84 uint32_t ls_hs_config;
85 unsigned num_patches;
86 unsigned lds_size;
87 uint32_t tf_param;
88 };
89
90 static const VkPipelineMultisampleStateCreateInfo *
91 radv_pipeline_get_multisample_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
92 {
93 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable)
94 return pCreateInfo->pMultisampleState;
95 return NULL;
96 }
97
98 static const VkPipelineTessellationStateCreateInfo *
99 radv_pipeline_get_tessellation_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
100 {
101 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
102 if (pCreateInfo->pStages[i].stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
103 pCreateInfo->pStages[i].stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
104 return pCreateInfo->pTessellationState;
105 }
106 }
107 return NULL;
108 }
109
110 static const VkPipelineDepthStencilStateCreateInfo *
111 radv_pipeline_get_depth_stencil_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
112 {
113 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
114 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
115
116 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
117 subpass->depth_stencil_attachment)
118 return pCreateInfo->pDepthStencilState;
119 return NULL;
120 }
121
122 static const VkPipelineColorBlendStateCreateInfo *
123 radv_pipeline_get_color_blend_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
124 {
125 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
126 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
127
128 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
129 subpass->has_color_att)
130 return pCreateInfo->pColorBlendState;
131 return NULL;
132 }
133
134 bool radv_pipeline_has_ngg(const struct radv_pipeline *pipeline)
135 {
136 struct radv_shader_variant *variant = NULL;
137 if (pipeline->shaders[MESA_SHADER_GEOMETRY])
138 variant = pipeline->shaders[MESA_SHADER_GEOMETRY];
139 else if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
140 variant = pipeline->shaders[MESA_SHADER_TESS_EVAL];
141 else if (pipeline->shaders[MESA_SHADER_VERTEX])
142 variant = pipeline->shaders[MESA_SHADER_VERTEX];
143 else
144 return false;
145 return variant->info.is_ngg;
146 }
147
148 bool radv_pipeline_has_ngg_passthrough(const struct radv_pipeline *pipeline)
149 {
150 assert(radv_pipeline_has_ngg(pipeline));
151
152 struct radv_shader_variant *variant = NULL;
153 if (pipeline->shaders[MESA_SHADER_GEOMETRY])
154 variant = pipeline->shaders[MESA_SHADER_GEOMETRY];
155 else if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
156 variant = pipeline->shaders[MESA_SHADER_TESS_EVAL];
157 else if (pipeline->shaders[MESA_SHADER_VERTEX])
158 variant = pipeline->shaders[MESA_SHADER_VERTEX];
159 else
160 return false;
161 return variant->info.is_ngg_passthrough;
162 }
163
164 bool radv_pipeline_has_gs_copy_shader(const struct radv_pipeline *pipeline)
165 {
166 if (!radv_pipeline_has_gs(pipeline))
167 return false;
168
169 /* The GS copy shader is required if the pipeline has GS on GFX6-GFX9.
170 * On GFX10, it might be required in rare cases if it's not possible to
171 * enable NGG.
172 */
173 if (radv_pipeline_has_ngg(pipeline))
174 return false;
175
176 assert(pipeline->gs_copy_shader);
177 return true;
178 }
179
180 static void
181 radv_pipeline_destroy(struct radv_device *device,
182 struct radv_pipeline *pipeline,
183 const VkAllocationCallbacks* allocator)
184 {
185 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i)
186 if (pipeline->shaders[i])
187 radv_shader_variant_destroy(device, pipeline->shaders[i]);
188
189 if (pipeline->gs_copy_shader)
190 radv_shader_variant_destroy(device, pipeline->gs_copy_shader);
191
192 if(pipeline->cs.buf)
193 free(pipeline->cs.buf);
194
195 vk_object_base_finish(&pipeline->base);
196 vk_free2(&device->vk.alloc, allocator, pipeline);
197 }
198
199 void radv_DestroyPipeline(
200 VkDevice _device,
201 VkPipeline _pipeline,
202 const VkAllocationCallbacks* pAllocator)
203 {
204 RADV_FROM_HANDLE(radv_device, device, _device);
205 RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline);
206
207 if (!_pipeline)
208 return;
209
210 radv_pipeline_destroy(device, pipeline, pAllocator);
211 }
212
213 static uint32_t get_hash_flags(struct radv_device *device)
214 {
215 uint32_t hash_flags = 0;
216
217 if (device->instance->debug_flags & RADV_DEBUG_NO_NGG)
218 hash_flags |= RADV_HASH_SHADER_NO_NGG;
219 if (device->physical_device->cs_wave_size == 32)
220 hash_flags |= RADV_HASH_SHADER_CS_WAVE32;
221 if (device->physical_device->ps_wave_size == 32)
222 hash_flags |= RADV_HASH_SHADER_PS_WAVE32;
223 if (device->physical_device->ge_wave_size == 32)
224 hash_flags |= RADV_HASH_SHADER_GE_WAVE32;
225 if (device->physical_device->use_aco)
226 hash_flags |= RADV_HASH_SHADER_ACO;
227 return hash_flags;
228 }
229
230 static VkResult
231 radv_pipeline_scratch_init(struct radv_device *device,
232 struct radv_pipeline *pipeline)
233 {
234 unsigned scratch_bytes_per_wave = 0;
235 unsigned max_waves = 0;
236 unsigned min_waves = 1;
237
238 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
239 if (pipeline->shaders[i] &&
240 pipeline->shaders[i]->config.scratch_bytes_per_wave) {
241 unsigned max_stage_waves = device->scratch_waves;
242
243 scratch_bytes_per_wave = MAX2(scratch_bytes_per_wave,
244 pipeline->shaders[i]->config.scratch_bytes_per_wave);
245
246 max_stage_waves = MIN2(max_stage_waves,
247 4 * device->physical_device->rad_info.num_good_compute_units *
248 (256 / pipeline->shaders[i]->config.num_vgprs));
249 max_waves = MAX2(max_waves, max_stage_waves);
250 }
251 }
252
253 if (pipeline->shaders[MESA_SHADER_COMPUTE]) {
254 unsigned group_size = pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[0] *
255 pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[1] *
256 pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[2];
257 min_waves = MAX2(min_waves, round_up_u32(group_size, 64));
258 }
259
260 pipeline->scratch_bytes_per_wave = scratch_bytes_per_wave;
261 pipeline->max_waves = max_waves;
262 return VK_SUCCESS;
263 }
264
265 static uint32_t si_translate_blend_logic_op(VkLogicOp op)
266 {
267 switch (op) {
268 case VK_LOGIC_OP_CLEAR:
269 return V_028808_ROP3_CLEAR;
270 case VK_LOGIC_OP_AND:
271 return V_028808_ROP3_AND;
272 case VK_LOGIC_OP_AND_REVERSE:
273 return V_028808_ROP3_AND_REVERSE;
274 case VK_LOGIC_OP_COPY:
275 return V_028808_ROP3_COPY;
276 case VK_LOGIC_OP_AND_INVERTED:
277 return V_028808_ROP3_AND_INVERTED;
278 case VK_LOGIC_OP_NO_OP:
279 return V_028808_ROP3_NO_OP;
280 case VK_LOGIC_OP_XOR:
281 return V_028808_ROP3_XOR;
282 case VK_LOGIC_OP_OR:
283 return V_028808_ROP3_OR;
284 case VK_LOGIC_OP_NOR:
285 return V_028808_ROP3_NOR;
286 case VK_LOGIC_OP_EQUIVALENT:
287 return V_028808_ROP3_EQUIVALENT;
288 case VK_LOGIC_OP_INVERT:
289 return V_028808_ROP3_INVERT;
290 case VK_LOGIC_OP_OR_REVERSE:
291 return V_028808_ROP3_OR_REVERSE;
292 case VK_LOGIC_OP_COPY_INVERTED:
293 return V_028808_ROP3_COPY_INVERTED;
294 case VK_LOGIC_OP_OR_INVERTED:
295 return V_028808_ROP3_OR_INVERTED;
296 case VK_LOGIC_OP_NAND:
297 return V_028808_ROP3_NAND;
298 case VK_LOGIC_OP_SET:
299 return V_028808_ROP3_SET;
300 default:
301 unreachable("Unhandled logic op");
302 }
303 }
304
305
306 static uint32_t si_translate_blend_function(VkBlendOp op)
307 {
308 switch (op) {
309 case VK_BLEND_OP_ADD:
310 return V_028780_COMB_DST_PLUS_SRC;
311 case VK_BLEND_OP_SUBTRACT:
312 return V_028780_COMB_SRC_MINUS_DST;
313 case VK_BLEND_OP_REVERSE_SUBTRACT:
314 return V_028780_COMB_DST_MINUS_SRC;
315 case VK_BLEND_OP_MIN:
316 return V_028780_COMB_MIN_DST_SRC;
317 case VK_BLEND_OP_MAX:
318 return V_028780_COMB_MAX_DST_SRC;
319 default:
320 return 0;
321 }
322 }
323
324 static uint32_t si_translate_blend_factor(VkBlendFactor factor)
325 {
326 switch (factor) {
327 case VK_BLEND_FACTOR_ZERO:
328 return V_028780_BLEND_ZERO;
329 case VK_BLEND_FACTOR_ONE:
330 return V_028780_BLEND_ONE;
331 case VK_BLEND_FACTOR_SRC_COLOR:
332 return V_028780_BLEND_SRC_COLOR;
333 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:
334 return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
335 case VK_BLEND_FACTOR_DST_COLOR:
336 return V_028780_BLEND_DST_COLOR;
337 case VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR:
338 return V_028780_BLEND_ONE_MINUS_DST_COLOR;
339 case VK_BLEND_FACTOR_SRC_ALPHA:
340 return V_028780_BLEND_SRC_ALPHA;
341 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:
342 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
343 case VK_BLEND_FACTOR_DST_ALPHA:
344 return V_028780_BLEND_DST_ALPHA;
345 case VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA:
346 return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
347 case VK_BLEND_FACTOR_CONSTANT_COLOR:
348 return V_028780_BLEND_CONSTANT_COLOR;
349 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR:
350 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
351 case VK_BLEND_FACTOR_CONSTANT_ALPHA:
352 return V_028780_BLEND_CONSTANT_ALPHA;
353 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA:
354 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
355 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:
356 return V_028780_BLEND_SRC_ALPHA_SATURATE;
357 case VK_BLEND_FACTOR_SRC1_COLOR:
358 return V_028780_BLEND_SRC1_COLOR;
359 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:
360 return V_028780_BLEND_INV_SRC1_COLOR;
361 case VK_BLEND_FACTOR_SRC1_ALPHA:
362 return V_028780_BLEND_SRC1_ALPHA;
363 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:
364 return V_028780_BLEND_INV_SRC1_ALPHA;
365 default:
366 return 0;
367 }
368 }
369
370 static uint32_t si_translate_blend_opt_function(VkBlendOp op)
371 {
372 switch (op) {
373 case VK_BLEND_OP_ADD:
374 return V_028760_OPT_COMB_ADD;
375 case VK_BLEND_OP_SUBTRACT:
376 return V_028760_OPT_COMB_SUBTRACT;
377 case VK_BLEND_OP_REVERSE_SUBTRACT:
378 return V_028760_OPT_COMB_REVSUBTRACT;
379 case VK_BLEND_OP_MIN:
380 return V_028760_OPT_COMB_MIN;
381 case VK_BLEND_OP_MAX:
382 return V_028760_OPT_COMB_MAX;
383 default:
384 return V_028760_OPT_COMB_BLEND_DISABLED;
385 }
386 }
387
388 static uint32_t si_translate_blend_opt_factor(VkBlendFactor factor, bool is_alpha)
389 {
390 switch (factor) {
391 case VK_BLEND_FACTOR_ZERO:
392 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;
393 case VK_BLEND_FACTOR_ONE:
394 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;
395 case VK_BLEND_FACTOR_SRC_COLOR:
396 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
397 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;
398 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:
399 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
400 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;
401 case VK_BLEND_FACTOR_SRC_ALPHA:
402 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;
403 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:
404 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;
405 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:
406 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
407 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
408 default:
409 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
410 }
411 }
412
413 /**
414 * Get rid of DST in the blend factors by commuting the operands:
415 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
416 */
417 static void si_blend_remove_dst(unsigned *func, unsigned *src_factor,
418 unsigned *dst_factor, unsigned expected_dst,
419 unsigned replacement_src)
420 {
421 if (*src_factor == expected_dst &&
422 *dst_factor == VK_BLEND_FACTOR_ZERO) {
423 *src_factor = VK_BLEND_FACTOR_ZERO;
424 *dst_factor = replacement_src;
425
426 /* Commuting the operands requires reversing subtractions. */
427 if (*func == VK_BLEND_OP_SUBTRACT)
428 *func = VK_BLEND_OP_REVERSE_SUBTRACT;
429 else if (*func == VK_BLEND_OP_REVERSE_SUBTRACT)
430 *func = VK_BLEND_OP_SUBTRACT;
431 }
432 }
433
434 static bool si_blend_factor_uses_dst(unsigned factor)
435 {
436 return factor == VK_BLEND_FACTOR_DST_COLOR ||
437 factor == VK_BLEND_FACTOR_DST_ALPHA ||
438 factor == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
439 factor == VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA ||
440 factor == VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;
441 }
442
443 static bool is_dual_src(VkBlendFactor factor)
444 {
445 switch (factor) {
446 case VK_BLEND_FACTOR_SRC1_COLOR:
447 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:
448 case VK_BLEND_FACTOR_SRC1_ALPHA:
449 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:
450 return true;
451 default:
452 return false;
453 }
454 }
455
456 static unsigned radv_choose_spi_color_format(VkFormat vk_format,
457 bool blend_enable,
458 bool blend_need_alpha)
459 {
460 const struct vk_format_description *desc = vk_format_description(vk_format);
461 struct ac_spi_color_formats formats = {};
462 unsigned format, ntype, swap;
463
464 format = radv_translate_colorformat(vk_format);
465 ntype = radv_translate_color_numformat(vk_format, desc,
466 vk_format_get_first_non_void_channel(vk_format));
467 swap = radv_translate_colorswap(vk_format, false);
468
469 ac_choose_spi_color_formats(format, swap, ntype, false, &formats);
470
471 if (blend_enable && blend_need_alpha)
472 return formats.blend_alpha;
473 else if(blend_need_alpha)
474 return formats.alpha;
475 else if(blend_enable)
476 return formats.blend;
477 else
478 return formats.normal;
479 }
480
481 static void
482 radv_pipeline_compute_spi_color_formats(struct radv_pipeline *pipeline,
483 const VkGraphicsPipelineCreateInfo *pCreateInfo,
484 struct radv_blend_state *blend)
485 {
486 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
487 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
488 unsigned col_format = 0;
489 unsigned num_targets;
490
491 for (unsigned i = 0; i < (blend->single_cb_enable ? 1 : subpass->color_count); ++i) {
492 unsigned cf;
493
494 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED) {
495 cf = V_028714_SPI_SHADER_ZERO;
496 } else {
497 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->color_attachments[i].attachment;
498 bool blend_enable =
499 blend->blend_enable_4bit & (0xfu << (i * 4));
500
501 cf = radv_choose_spi_color_format(attachment->format,
502 blend_enable,
503 blend->need_src_alpha & (1 << i));
504 }
505
506 col_format |= cf << (4 * i);
507 }
508
509 if (!(col_format & 0xf) && blend->need_src_alpha & (1 << 0)) {
510 /* When a subpass doesn't have any color attachments, write the
511 * alpha channel of MRT0 when alpha coverage is enabled because
512 * the depth attachment needs it.
513 */
514 col_format |= V_028714_SPI_SHADER_32_AR;
515 }
516
517 /* If the i-th target format is set, all previous target formats must
518 * be non-zero to avoid hangs.
519 */
520 num_targets = (util_last_bit(col_format) + 3) / 4;
521 for (unsigned i = 0; i < num_targets; i++) {
522 if (!(col_format & (0xf << (i * 4)))) {
523 col_format |= V_028714_SPI_SHADER_32_R << (i * 4);
524 }
525 }
526
527 /* The output for dual source blending should have the same format as
528 * the first output.
529 */
530 if (blend->mrt0_is_dual_src)
531 col_format |= (col_format & 0xf) << 4;
532
533 blend->spi_shader_col_format = col_format;
534 }
535
536 static bool
537 format_is_int8(VkFormat format)
538 {
539 const struct vk_format_description *desc = vk_format_description(format);
540 int channel = vk_format_get_first_non_void_channel(format);
541
542 return channel >= 0 && desc->channel[channel].pure_integer &&
543 desc->channel[channel].size == 8;
544 }
545
546 static bool
547 format_is_int10(VkFormat format)
548 {
549 const struct vk_format_description *desc = vk_format_description(format);
550
551 if (desc->nr_channels != 4)
552 return false;
553 for (unsigned i = 0; i < 4; i++) {
554 if (desc->channel[i].pure_integer && desc->channel[i].size == 10)
555 return true;
556 }
557 return false;
558 }
559
560 /*
561 * Ordered so that for each i,
562 * radv_format_meta_fs_key(radv_fs_key_format_exemplars[i]) == i.
563 */
564 const VkFormat radv_fs_key_format_exemplars[NUM_META_FS_KEYS] = {
565 VK_FORMAT_R32_SFLOAT,
566 VK_FORMAT_R32G32_SFLOAT,
567 VK_FORMAT_R8G8B8A8_UNORM,
568 VK_FORMAT_R16G16B16A16_UNORM,
569 VK_FORMAT_R16G16B16A16_SNORM,
570 VK_FORMAT_R16G16B16A16_UINT,
571 VK_FORMAT_R16G16B16A16_SINT,
572 VK_FORMAT_R32G32B32A32_SFLOAT,
573 VK_FORMAT_R8G8B8A8_UINT,
574 VK_FORMAT_R8G8B8A8_SINT,
575 VK_FORMAT_A2R10G10B10_UINT_PACK32,
576 VK_FORMAT_A2R10G10B10_SINT_PACK32,
577 };
578
579 unsigned radv_format_meta_fs_key(VkFormat format)
580 {
581 unsigned col_format = radv_choose_spi_color_format(format, false, false);
582
583 assert(col_format != V_028714_SPI_SHADER_32_AR);
584 if (col_format >= V_028714_SPI_SHADER_32_AR)
585 --col_format; /* Skip V_028714_SPI_SHADER_32_AR since there is no such VkFormat */
586
587 --col_format; /* Skip V_028714_SPI_SHADER_ZERO */
588 bool is_int8 = format_is_int8(format);
589 bool is_int10 = format_is_int10(format);
590
591 return col_format + (is_int8 ? 3 : is_int10 ? 5 : 0);
592 }
593
594 static void
595 radv_pipeline_compute_get_int_clamp(const VkGraphicsPipelineCreateInfo *pCreateInfo,
596 unsigned *is_int8, unsigned *is_int10)
597 {
598 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
599 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
600 *is_int8 = 0;
601 *is_int10 = 0;
602
603 for (unsigned i = 0; i < subpass->color_count; ++i) {
604 struct radv_render_pass_attachment *attachment;
605
606 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
607 continue;
608
609 attachment = pass->attachments + subpass->color_attachments[i].attachment;
610
611 if (format_is_int8(attachment->format))
612 *is_int8 |= 1 << i;
613 if (format_is_int10(attachment->format))
614 *is_int10 |= 1 << i;
615 }
616 }
617
618 static void
619 radv_blend_check_commutativity(struct radv_blend_state *blend,
620 VkBlendOp op, VkBlendFactor src,
621 VkBlendFactor dst, unsigned chanmask)
622 {
623 /* Src factor is allowed when it does not depend on Dst. */
624 static const uint32_t src_allowed =
625 (1u << VK_BLEND_FACTOR_ONE) |
626 (1u << VK_BLEND_FACTOR_SRC_COLOR) |
627 (1u << VK_BLEND_FACTOR_SRC_ALPHA) |
628 (1u << VK_BLEND_FACTOR_SRC_ALPHA_SATURATE) |
629 (1u << VK_BLEND_FACTOR_CONSTANT_COLOR) |
630 (1u << VK_BLEND_FACTOR_CONSTANT_ALPHA) |
631 (1u << VK_BLEND_FACTOR_SRC1_COLOR) |
632 (1u << VK_BLEND_FACTOR_SRC1_ALPHA) |
633 (1u << VK_BLEND_FACTOR_ZERO) |
634 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR) |
635 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA) |
636 (1u << VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR) |
637 (1u << VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA) |
638 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR) |
639 (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA);
640
641 if (dst == VK_BLEND_FACTOR_ONE &&
642 (src_allowed & (1u << src))) {
643 /* Addition is commutative, but floating point addition isn't
644 * associative: subtle changes can be introduced via different
645 * rounding. Be conservative, only enable for min and max.
646 */
647 if (op == VK_BLEND_OP_MAX || op == VK_BLEND_OP_MIN)
648 blend->commutative_4bit |= chanmask;
649 }
650 }
651
652 static struct radv_blend_state
653 radv_pipeline_init_blend_state(struct radv_pipeline *pipeline,
654 const VkGraphicsPipelineCreateInfo *pCreateInfo,
655 const struct radv_graphics_pipeline_create_info *extra)
656 {
657 const VkPipelineColorBlendStateCreateInfo *vkblend = radv_pipeline_get_color_blend_state(pCreateInfo);
658 const VkPipelineMultisampleStateCreateInfo *vkms = radv_pipeline_get_multisample_state(pCreateInfo);
659 struct radv_blend_state blend = {0};
660 unsigned mode = V_028808_CB_NORMAL;
661 int i;
662
663 if (extra && extra->custom_blend_mode) {
664 blend.single_cb_enable = true;
665 mode = extra->custom_blend_mode;
666 }
667
668 blend.cb_color_control = 0;
669 if (vkblend) {
670 if (vkblend->logicOpEnable)
671 blend.cb_color_control |= S_028808_ROP3(si_translate_blend_logic_op(vkblend->logicOp));
672 else
673 blend.cb_color_control |= S_028808_ROP3(V_028808_ROP3_COPY);
674 }
675
676 blend.db_alpha_to_mask = S_028B70_ALPHA_TO_MASK_OFFSET0(3) |
677 S_028B70_ALPHA_TO_MASK_OFFSET1(1) |
678 S_028B70_ALPHA_TO_MASK_OFFSET2(0) |
679 S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
680 S_028B70_OFFSET_ROUND(1);
681
682 if (vkms && vkms->alphaToCoverageEnable) {
683 blend.db_alpha_to_mask |= S_028B70_ALPHA_TO_MASK_ENABLE(1);
684 blend.need_src_alpha |= 0x1;
685 }
686
687 blend.cb_target_mask = 0;
688 if (vkblend) {
689 for (i = 0; i < vkblend->attachmentCount; i++) {
690 const VkPipelineColorBlendAttachmentState *att = &vkblend->pAttachments[i];
691 unsigned blend_cntl = 0;
692 unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;
693 VkBlendOp eqRGB = att->colorBlendOp;
694 VkBlendFactor srcRGB = att->srcColorBlendFactor;
695 VkBlendFactor dstRGB = att->dstColorBlendFactor;
696 VkBlendOp eqA = att->alphaBlendOp;
697 VkBlendFactor srcA = att->srcAlphaBlendFactor;
698 VkBlendFactor dstA = att->dstAlphaBlendFactor;
699
700 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);
701
702 if (!att->colorWriteMask)
703 continue;
704
705 blend.cb_target_mask |= (unsigned)att->colorWriteMask << (4 * i);
706 blend.cb_target_enabled_4bit |= 0xf << (4 * i);
707 if (!att->blendEnable) {
708 blend.cb_blend_control[i] = blend_cntl;
709 continue;
710 }
711
712 if (is_dual_src(srcRGB) || is_dual_src(dstRGB) || is_dual_src(srcA) || is_dual_src(dstA))
713 if (i == 0)
714 blend.mrt0_is_dual_src = true;
715
716 if (eqRGB == VK_BLEND_OP_MIN || eqRGB == VK_BLEND_OP_MAX) {
717 srcRGB = VK_BLEND_FACTOR_ONE;
718 dstRGB = VK_BLEND_FACTOR_ONE;
719 }
720 if (eqA == VK_BLEND_OP_MIN || eqA == VK_BLEND_OP_MAX) {
721 srcA = VK_BLEND_FACTOR_ONE;
722 dstA = VK_BLEND_FACTOR_ONE;
723 }
724
725 radv_blend_check_commutativity(&blend, eqRGB, srcRGB, dstRGB,
726 0x7 << (4 * i));
727 radv_blend_check_commutativity(&blend, eqA, srcA, dstA,
728 0x8 << (4 * i));
729
730 /* Blending optimizations for RB+.
731 * These transformations don't change the behavior.
732 *
733 * First, get rid of DST in the blend factors:
734 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
735 */
736 si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB,
737 VK_BLEND_FACTOR_DST_COLOR,
738 VK_BLEND_FACTOR_SRC_COLOR);
739
740 si_blend_remove_dst(&eqA, &srcA, &dstA,
741 VK_BLEND_FACTOR_DST_COLOR,
742 VK_BLEND_FACTOR_SRC_COLOR);
743
744 si_blend_remove_dst(&eqA, &srcA, &dstA,
745 VK_BLEND_FACTOR_DST_ALPHA,
746 VK_BLEND_FACTOR_SRC_ALPHA);
747
748 /* Look up the ideal settings from tables. */
749 srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);
750 dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);
751 srcA_opt = si_translate_blend_opt_factor(srcA, true);
752 dstA_opt = si_translate_blend_opt_factor(dstA, true);
753
754 /* Handle interdependencies. */
755 if (si_blend_factor_uses_dst(srcRGB))
756 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
757 if (si_blend_factor_uses_dst(srcA))
758 dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
759
760 if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE &&
761 (dstRGB == VK_BLEND_FACTOR_ZERO ||
762 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
763 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE))
764 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
765
766 /* Set the final value. */
767 blend.sx_mrt_blend_opt[i] =
768 S_028760_COLOR_SRC_OPT(srcRGB_opt) |
769 S_028760_COLOR_DST_OPT(dstRGB_opt) |
770 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |
771 S_028760_ALPHA_SRC_OPT(srcA_opt) |
772 S_028760_ALPHA_DST_OPT(dstA_opt) |
773 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));
774 blend_cntl |= S_028780_ENABLE(1);
775
776 blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
777 blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
778 blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
779 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
780 blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
781 blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
782 blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
783 blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
784 }
785 blend.cb_blend_control[i] = blend_cntl;
786
787 blend.blend_enable_4bit |= 0xfu << (i * 4);
788
789 if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
790 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
791 srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
792 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
793 srcRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA ||
794 dstRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA)
795 blend.need_src_alpha |= 1 << i;
796 }
797 for (i = vkblend->attachmentCount; i < 8; i++) {
798 blend.cb_blend_control[i] = 0;
799 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);
800 }
801 }
802
803 if (pipeline->device->physical_device->rad_info.has_rbplus) {
804 /* Disable RB+ blend optimizations for dual source blending. */
805 if (blend.mrt0_is_dual_src) {
806 for (i = 0; i < 8; i++) {
807 blend.sx_mrt_blend_opt[i] =
808 S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE) |
809 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE);
810 }
811 }
812
813 /* RB+ doesn't work with dual source blending, logic op and
814 * RESOLVE.
815 */
816 if (blend.mrt0_is_dual_src ||
817 (vkblend && vkblend->logicOpEnable) ||
818 mode == V_028808_CB_RESOLVE)
819 blend.cb_color_control |= S_028808_DISABLE_DUAL_QUAD(1);
820 }
821
822 if (blend.cb_target_mask)
823 blend.cb_color_control |= S_028808_MODE(mode);
824 else
825 blend.cb_color_control |= S_028808_MODE(V_028808_CB_DISABLE);
826
827 radv_pipeline_compute_spi_color_formats(pipeline, pCreateInfo, &blend);
828 return blend;
829 }
830
831 static uint32_t si_translate_stencil_op(enum VkStencilOp op)
832 {
833 switch (op) {
834 case VK_STENCIL_OP_KEEP:
835 return V_02842C_STENCIL_KEEP;
836 case VK_STENCIL_OP_ZERO:
837 return V_02842C_STENCIL_ZERO;
838 case VK_STENCIL_OP_REPLACE:
839 return V_02842C_STENCIL_REPLACE_TEST;
840 case VK_STENCIL_OP_INCREMENT_AND_CLAMP:
841 return V_02842C_STENCIL_ADD_CLAMP;
842 case VK_STENCIL_OP_DECREMENT_AND_CLAMP:
843 return V_02842C_STENCIL_SUB_CLAMP;
844 case VK_STENCIL_OP_INVERT:
845 return V_02842C_STENCIL_INVERT;
846 case VK_STENCIL_OP_INCREMENT_AND_WRAP:
847 return V_02842C_STENCIL_ADD_WRAP;
848 case VK_STENCIL_OP_DECREMENT_AND_WRAP:
849 return V_02842C_STENCIL_SUB_WRAP;
850 default:
851 return 0;
852 }
853 }
854
855 static uint32_t si_translate_fill(VkPolygonMode func)
856 {
857 switch(func) {
858 case VK_POLYGON_MODE_FILL:
859 return V_028814_X_DRAW_TRIANGLES;
860 case VK_POLYGON_MODE_LINE:
861 return V_028814_X_DRAW_LINES;
862 case VK_POLYGON_MODE_POINT:
863 return V_028814_X_DRAW_POINTS;
864 default:
865 assert(0);
866 return V_028814_X_DRAW_POINTS;
867 }
868 }
869
870 static uint8_t radv_pipeline_get_ps_iter_samples(const VkGraphicsPipelineCreateInfo *pCreateInfo)
871 {
872 const VkPipelineMultisampleStateCreateInfo *vkms = pCreateInfo->pMultisampleState;
873 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
874 struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
875 uint32_t ps_iter_samples = 1;
876 uint32_t num_samples;
877
878 /* From the Vulkan 1.1.129 spec, 26.7. Sample Shading:
879 *
880 * "If the VK_AMD_mixed_attachment_samples extension is enabled and the
881 * subpass uses color attachments, totalSamples is the number of
882 * samples of the color attachments. Otherwise, totalSamples is the
883 * value of VkPipelineMultisampleStateCreateInfo::rasterizationSamples
884 * specified at pipeline creation time."
885 */
886 if (subpass->has_color_att) {
887 num_samples = subpass->color_sample_count;
888 } else {
889 num_samples = vkms->rasterizationSamples;
890 }
891
892 if (vkms->sampleShadingEnable) {
893 ps_iter_samples = ceilf(vkms->minSampleShading * num_samples);
894 ps_iter_samples = util_next_power_of_two(ps_iter_samples);
895 }
896 return ps_iter_samples;
897 }
898
899 static bool
900 radv_is_depth_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)
901 {
902 return pCreateInfo->depthTestEnable &&
903 pCreateInfo->depthWriteEnable &&
904 pCreateInfo->depthCompareOp != VK_COMPARE_OP_NEVER;
905 }
906
907 static bool
908 radv_writes_stencil(const VkStencilOpState *state)
909 {
910 return state->writeMask &&
911 (state->failOp != VK_STENCIL_OP_KEEP ||
912 state->passOp != VK_STENCIL_OP_KEEP ||
913 state->depthFailOp != VK_STENCIL_OP_KEEP);
914 }
915
916 static bool
917 radv_is_stencil_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)
918 {
919 return pCreateInfo->stencilTestEnable &&
920 (radv_writes_stencil(&pCreateInfo->front) ||
921 radv_writes_stencil(&pCreateInfo->back));
922 }
923
924 static bool
925 radv_is_ds_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)
926 {
927 return radv_is_depth_write_enabled(pCreateInfo) ||
928 radv_is_stencil_write_enabled(pCreateInfo);
929 }
930
931 static bool
932 radv_order_invariant_stencil_op(VkStencilOp op)
933 {
934 /* REPLACE is normally order invariant, except when the stencil
935 * reference value is written by the fragment shader. Tracking this
936 * interaction does not seem worth the effort, so be conservative.
937 */
938 return op != VK_STENCIL_OP_INCREMENT_AND_CLAMP &&
939 op != VK_STENCIL_OP_DECREMENT_AND_CLAMP &&
940 op != VK_STENCIL_OP_REPLACE;
941 }
942
943 static bool
944 radv_order_invariant_stencil_state(const VkStencilOpState *state)
945 {
946 /* Compute whether, assuming Z writes are disabled, this stencil state
947 * is order invariant in the sense that the set of passing fragments as
948 * well as the final stencil buffer result does not depend on the order
949 * of fragments.
950 */
951 return !state->writeMask ||
952 /* The following assumes that Z writes are disabled. */
953 (state->compareOp == VK_COMPARE_OP_ALWAYS &&
954 radv_order_invariant_stencil_op(state->passOp) &&
955 radv_order_invariant_stencil_op(state->depthFailOp)) ||
956 (state->compareOp == VK_COMPARE_OP_NEVER &&
957 radv_order_invariant_stencil_op(state->failOp));
958 }
959
960 static bool
961 radv_pipeline_out_of_order_rast(struct radv_pipeline *pipeline,
962 struct radv_blend_state *blend,
963 const VkGraphicsPipelineCreateInfo *pCreateInfo)
964 {
965 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
966 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
967 const VkPipelineDepthStencilStateCreateInfo *vkds = radv_pipeline_get_depth_stencil_state(pCreateInfo);
968 const VkPipelineColorBlendStateCreateInfo *vkblend = radv_pipeline_get_color_blend_state(pCreateInfo);
969 unsigned colormask = blend->cb_target_enabled_4bit;
970
971 if (!pipeline->device->physical_device->out_of_order_rast_allowed)
972 return false;
973
974 /* Be conservative if a logic operation is enabled with color buffers. */
975 if (colormask && vkblend && vkblend->logicOpEnable)
976 return false;
977
978 /* Default depth/stencil invariance when no attachment is bound. */
979 struct radv_dsa_order_invariance dsa_order_invariant = {
980 .zs = true, .pass_set = true
981 };
982
983 if (vkds) {
984 struct radv_render_pass_attachment *attachment =
985 pass->attachments + subpass->depth_stencil_attachment->attachment;
986 bool has_stencil = vk_format_is_stencil(attachment->format);
987 struct radv_dsa_order_invariance order_invariance[2];
988 struct radv_shader_variant *ps =
989 pipeline->shaders[MESA_SHADER_FRAGMENT];
990
991 /* Compute depth/stencil order invariance in order to know if
992 * it's safe to enable out-of-order.
993 */
994 bool zfunc_is_ordered =
995 vkds->depthCompareOp == VK_COMPARE_OP_NEVER ||
996 vkds->depthCompareOp == VK_COMPARE_OP_LESS ||
997 vkds->depthCompareOp == VK_COMPARE_OP_LESS_OR_EQUAL ||
998 vkds->depthCompareOp == VK_COMPARE_OP_GREATER ||
999 vkds->depthCompareOp == VK_COMPARE_OP_GREATER_OR_EQUAL;
1000
1001 bool nozwrite_and_order_invariant_stencil =
1002 !radv_is_ds_write_enabled(vkds) ||
1003 (!radv_is_depth_write_enabled(vkds) &&
1004 radv_order_invariant_stencil_state(&vkds->front) &&
1005 radv_order_invariant_stencil_state(&vkds->back));
1006
1007 order_invariance[1].zs =
1008 nozwrite_and_order_invariant_stencil ||
1009 (!radv_is_stencil_write_enabled(vkds) &&
1010 zfunc_is_ordered);
1011 order_invariance[0].zs =
1012 !radv_is_depth_write_enabled(vkds) || zfunc_is_ordered;
1013
1014 order_invariance[1].pass_set =
1015 nozwrite_and_order_invariant_stencil ||
1016 (!radv_is_stencil_write_enabled(vkds) &&
1017 (vkds->depthCompareOp == VK_COMPARE_OP_ALWAYS ||
1018 vkds->depthCompareOp == VK_COMPARE_OP_NEVER));
1019 order_invariance[0].pass_set =
1020 !radv_is_depth_write_enabled(vkds) ||
1021 (vkds->depthCompareOp == VK_COMPARE_OP_ALWAYS ||
1022 vkds->depthCompareOp == VK_COMPARE_OP_NEVER);
1023
1024 dsa_order_invariant = order_invariance[has_stencil];
1025 if (!dsa_order_invariant.zs)
1026 return false;
1027
1028 /* The set of PS invocations is always order invariant,
1029 * except when early Z/S tests are requested.
1030 */
1031 if (ps &&
1032 ps->info.ps.writes_memory &&
1033 ps->info.ps.early_fragment_test &&
1034 !dsa_order_invariant.pass_set)
1035 return false;
1036
1037 /* Determine if out-of-order rasterization should be disabled
1038 * when occlusion queries are used.
1039 */
1040 pipeline->graphics.disable_out_of_order_rast_for_occlusion =
1041 !dsa_order_invariant.pass_set;
1042 }
1043
1044 /* No color buffers are enabled for writing. */
1045 if (!colormask)
1046 return true;
1047
1048 unsigned blendmask = colormask & blend->blend_enable_4bit;
1049
1050 if (blendmask) {
1051 /* Only commutative blending. */
1052 if (blendmask & ~blend->commutative_4bit)
1053 return false;
1054
1055 if (!dsa_order_invariant.pass_set)
1056 return false;
1057 }
1058
1059 if (colormask & ~blendmask)
1060 return false;
1061
1062 return true;
1063 }
1064
1065 static void
1066 radv_pipeline_init_multisample_state(struct radv_pipeline *pipeline,
1067 struct radv_blend_state *blend,
1068 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1069 {
1070 const VkPipelineMultisampleStateCreateInfo *vkms = radv_pipeline_get_multisample_state(pCreateInfo);
1071 struct radv_multisample_state *ms = &pipeline->graphics.ms;
1072 unsigned num_tile_pipes = pipeline->device->physical_device->rad_info.num_tile_pipes;
1073 bool out_of_order_rast = false;
1074 int ps_iter_samples = 1;
1075 uint32_t mask = 0xffff;
1076
1077 if (vkms) {
1078 ms->num_samples = vkms->rasterizationSamples;
1079
1080 /* From the Vulkan 1.1.129 spec, 26.7. Sample Shading:
1081 *
1082 * "Sample shading is enabled for a graphics pipeline:
1083 *
1084 * - If the interface of the fragment shader entry point of the
1085 * graphics pipeline includes an input variable decorated
1086 * with SampleId or SamplePosition. In this case
1087 * minSampleShadingFactor takes the value 1.0.
1088 * - Else if the sampleShadingEnable member of the
1089 * VkPipelineMultisampleStateCreateInfo structure specified
1090 * when creating the graphics pipeline is set to VK_TRUE. In
1091 * this case minSampleShadingFactor takes the value of
1092 * VkPipelineMultisampleStateCreateInfo::minSampleShading.
1093 *
1094 * Otherwise, sample shading is considered disabled."
1095 */
1096 if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.ps.force_persample) {
1097 ps_iter_samples = ms->num_samples;
1098 } else {
1099 ps_iter_samples = radv_pipeline_get_ps_iter_samples(pCreateInfo);
1100 }
1101 } else {
1102 ms->num_samples = 1;
1103 }
1104
1105 const struct VkPipelineRasterizationStateRasterizationOrderAMD *raster_order =
1106 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext, PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD);
1107 if (raster_order && raster_order->rasterizationOrder == VK_RASTERIZATION_ORDER_RELAXED_AMD) {
1108 /* Out-of-order rasterization is explicitly enabled by the
1109 * application.
1110 */
1111 out_of_order_rast = true;
1112 } else {
1113 /* Determine if the driver can enable out-of-order
1114 * rasterization internally.
1115 */
1116 out_of_order_rast =
1117 radv_pipeline_out_of_order_rast(pipeline, blend, pCreateInfo);
1118 }
1119
1120 ms->pa_sc_line_cntl = S_028BDC_DX10_DIAMOND_TEST_ENA(1);
1121 ms->pa_sc_aa_config = 0;
1122 ms->db_eqaa = S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
1123 S_028804_INCOHERENT_EQAA_READS(1) |
1124 S_028804_INTERPOLATE_COMP_Z(1) |
1125 S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
1126 ms->pa_sc_mode_cntl_1 =
1127 S_028A4C_WALK_FENCE_ENABLE(1) | //TODO linear dst fixes
1128 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |
1129 S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(out_of_order_rast) |
1130 S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7) |
1131 /* always 1: */
1132 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
1133 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
1134 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
1135 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
1136 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
1137 S_028A4C_FORCE_EOV_REZ_ENABLE(1);
1138 ms->pa_sc_mode_cntl_0 = S_028A48_ALTERNATE_RBS_PER_TILE(pipeline->device->physical_device->rad_info.chip_class >= GFX9) |
1139 S_028A48_VPORT_SCISSOR_ENABLE(1);
1140
1141 const VkPipelineRasterizationLineStateCreateInfoEXT *rast_line =
1142 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1143 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);
1144 if (rast_line) {
1145 ms->pa_sc_mode_cntl_0 |= S_028A48_LINE_STIPPLE_ENABLE(rast_line->stippledLineEnable);
1146 if (rast_line->lineRasterizationMode == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT) {
1147 /* From the Vulkan spec 1.1.129:
1148 *
1149 * "When VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT lines
1150 * are being rasterized, sample locations may all be
1151 * treated as being at the pixel center (this may
1152 * affect attribute and depth interpolation)."
1153 */
1154 ms->num_samples = 1;
1155 }
1156 }
1157
1158 if (ms->num_samples > 1) {
1159 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1160 struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
1161 uint32_t z_samples = subpass->depth_stencil_attachment ? subpass->depth_sample_count : ms->num_samples;
1162 unsigned log_samples = util_logbase2(ms->num_samples);
1163 unsigned log_z_samples = util_logbase2(z_samples);
1164 unsigned log_ps_iter_samples = util_logbase2(ps_iter_samples);
1165 ms->pa_sc_mode_cntl_0 |= S_028A48_MSAA_ENABLE(1);
1166 ms->pa_sc_line_cntl |= S_028BDC_EXPAND_LINE_WIDTH(1); /* CM_R_028BDC_PA_SC_LINE_CNTL */
1167 ms->db_eqaa |= S_028804_MAX_ANCHOR_SAMPLES(log_z_samples) |
1168 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |
1169 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |
1170 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples);
1171 ms->pa_sc_aa_config |= S_028BE0_MSAA_NUM_SAMPLES(log_samples) |
1172 S_028BE0_MAX_SAMPLE_DIST(radv_get_default_max_sample_dist(log_samples)) |
1173 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples) | /* CM_R_028BE0_PA_SC_AA_CONFIG */
1174 S_028BE0_COVERED_CENTROID_IS_CENTER_GFX103(pipeline->device->physical_device->rad_info.chip_class >= GFX10_3);
1175 ms->pa_sc_mode_cntl_1 |= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1);
1176 if (ps_iter_samples > 1)
1177 pipeline->graphics.spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(2);
1178 }
1179
1180 if (vkms && vkms->pSampleMask) {
1181 mask = vkms->pSampleMask[0] & 0xffff;
1182 }
1183
1184 ms->pa_sc_aa_mask[0] = mask | (mask << 16);
1185 ms->pa_sc_aa_mask[1] = mask | (mask << 16);
1186 }
1187
1188 static bool
1189 radv_prim_can_use_guardband(enum VkPrimitiveTopology topology)
1190 {
1191 switch (topology) {
1192 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
1193 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
1194 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
1195 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1196 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1197 return false;
1198 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
1199 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
1200 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
1201 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1202 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1203 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
1204 return true;
1205 default:
1206 unreachable("unhandled primitive type");
1207 }
1208 }
1209
1210 static uint32_t
1211 si_translate_prim(enum VkPrimitiveTopology topology)
1212 {
1213 switch (topology) {
1214 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
1215 return V_008958_DI_PT_POINTLIST;
1216 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
1217 return V_008958_DI_PT_LINELIST;
1218 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
1219 return V_008958_DI_PT_LINESTRIP;
1220 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
1221 return V_008958_DI_PT_TRILIST;
1222 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
1223 return V_008958_DI_PT_TRISTRIP;
1224 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
1225 return V_008958_DI_PT_TRIFAN;
1226 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1227 return V_008958_DI_PT_LINELIST_ADJ;
1228 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1229 return V_008958_DI_PT_LINESTRIP_ADJ;
1230 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1231 return V_008958_DI_PT_TRILIST_ADJ;
1232 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1233 return V_008958_DI_PT_TRISTRIP_ADJ;
1234 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
1235 return V_008958_DI_PT_PATCH;
1236 default:
1237 assert(0);
1238 return 0;
1239 }
1240 }
1241
1242 static uint32_t
1243 si_conv_gl_prim_to_gs_out(unsigned gl_prim)
1244 {
1245 switch (gl_prim) {
1246 case 0: /* GL_POINTS */
1247 return V_028A6C_OUTPRIM_TYPE_POINTLIST;
1248 case 1: /* GL_LINES */
1249 case 3: /* GL_LINE_STRIP */
1250 case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */
1251 case 0x8E7A: /* GL_ISOLINES */
1252 return V_028A6C_OUTPRIM_TYPE_LINESTRIP;
1253
1254 case 4: /* GL_TRIANGLES */
1255 case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */
1256 case 5: /* GL_TRIANGLE_STRIP */
1257 case 7: /* GL_QUADS */
1258 return V_028A6C_OUTPRIM_TYPE_TRISTRIP;
1259 default:
1260 assert(0);
1261 return 0;
1262 }
1263 }
1264
1265 static uint32_t
1266 si_conv_prim_to_gs_out(enum VkPrimitiveTopology topology)
1267 {
1268 switch (topology) {
1269 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
1270 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
1271 return V_028A6C_OUTPRIM_TYPE_POINTLIST;
1272 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
1273 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
1274 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1275 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1276 return V_028A6C_OUTPRIM_TYPE_LINESTRIP;
1277 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
1278 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
1279 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
1280 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1281 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1282 return V_028A6C_OUTPRIM_TYPE_TRISTRIP;
1283 default:
1284 assert(0);
1285 return 0;
1286 }
1287 }
1288
1289 static unsigned radv_dynamic_state_mask(VkDynamicState state)
1290 {
1291 switch(state) {
1292 case VK_DYNAMIC_STATE_VIEWPORT:
1293 return RADV_DYNAMIC_VIEWPORT;
1294 case VK_DYNAMIC_STATE_SCISSOR:
1295 return RADV_DYNAMIC_SCISSOR;
1296 case VK_DYNAMIC_STATE_LINE_WIDTH:
1297 return RADV_DYNAMIC_LINE_WIDTH;
1298 case VK_DYNAMIC_STATE_DEPTH_BIAS:
1299 return RADV_DYNAMIC_DEPTH_BIAS;
1300 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
1301 return RADV_DYNAMIC_BLEND_CONSTANTS;
1302 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
1303 return RADV_DYNAMIC_DEPTH_BOUNDS;
1304 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
1305 return RADV_DYNAMIC_STENCIL_COMPARE_MASK;
1306 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
1307 return RADV_DYNAMIC_STENCIL_WRITE_MASK;
1308 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
1309 return RADV_DYNAMIC_STENCIL_REFERENCE;
1310 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT:
1311 return RADV_DYNAMIC_DISCARD_RECTANGLE;
1312 case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT:
1313 return RADV_DYNAMIC_SAMPLE_LOCATIONS;
1314 case VK_DYNAMIC_STATE_LINE_STIPPLE_EXT:
1315 return RADV_DYNAMIC_LINE_STIPPLE;
1316 default:
1317 unreachable("Unhandled dynamic state");
1318 }
1319 }
1320
1321 static uint32_t radv_pipeline_needed_dynamic_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)
1322 {
1323 uint32_t states = RADV_DYNAMIC_ALL;
1324
1325 /* If rasterization is disabled we do not care about any of the dynamic states,
1326 * since they are all rasterization related only. */
1327 if (pCreateInfo->pRasterizationState->rasterizerDiscardEnable)
1328 return 0;
1329
1330 if (!pCreateInfo->pRasterizationState->depthBiasEnable)
1331 states &= ~RADV_DYNAMIC_DEPTH_BIAS;
1332
1333 if (!pCreateInfo->pDepthStencilState ||
1334 !pCreateInfo->pDepthStencilState->depthBoundsTestEnable)
1335 states &= ~RADV_DYNAMIC_DEPTH_BOUNDS;
1336
1337 if (!pCreateInfo->pDepthStencilState ||
1338 !pCreateInfo->pDepthStencilState->stencilTestEnable)
1339 states &= ~(RADV_DYNAMIC_STENCIL_COMPARE_MASK |
1340 RADV_DYNAMIC_STENCIL_WRITE_MASK |
1341 RADV_DYNAMIC_STENCIL_REFERENCE);
1342
1343 if (!vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT))
1344 states &= ~RADV_DYNAMIC_DISCARD_RECTANGLE;
1345
1346 if (!pCreateInfo->pMultisampleState ||
1347 !vk_find_struct_const(pCreateInfo->pMultisampleState->pNext,
1348 PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT))
1349 states &= ~RADV_DYNAMIC_SAMPLE_LOCATIONS;
1350
1351 if (!pCreateInfo->pRasterizationState ||
1352 !vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1353 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT))
1354 states &= ~RADV_DYNAMIC_LINE_STIPPLE;
1355
1356 /* TODO: blend constants & line width. */
1357
1358 return states;
1359 }
1360
1361
1362 static void
1363 radv_pipeline_init_dynamic_state(struct radv_pipeline *pipeline,
1364 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1365 {
1366 uint32_t needed_states = radv_pipeline_needed_dynamic_state(pCreateInfo);
1367 uint32_t states = needed_states;
1368 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1369 struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
1370
1371 pipeline->dynamic_state = default_dynamic_state;
1372 pipeline->graphics.needed_dynamic_state = needed_states;
1373
1374 if (pCreateInfo->pDynamicState) {
1375 /* Remove all of the states that are marked as dynamic */
1376 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
1377 for (uint32_t s = 0; s < count; s++)
1378 states &= ~radv_dynamic_state_mask(pCreateInfo->pDynamicState->pDynamicStates[s]);
1379 }
1380
1381 struct radv_dynamic_state *dynamic = &pipeline->dynamic_state;
1382
1383 if (needed_states & RADV_DYNAMIC_VIEWPORT) {
1384 assert(pCreateInfo->pViewportState);
1385
1386 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
1387 if (states & RADV_DYNAMIC_VIEWPORT) {
1388 typed_memcpy(dynamic->viewport.viewports,
1389 pCreateInfo->pViewportState->pViewports,
1390 pCreateInfo->pViewportState->viewportCount);
1391 }
1392 }
1393
1394 if (needed_states & RADV_DYNAMIC_SCISSOR) {
1395 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
1396 if (states & RADV_DYNAMIC_SCISSOR) {
1397 typed_memcpy(dynamic->scissor.scissors,
1398 pCreateInfo->pViewportState->pScissors,
1399 pCreateInfo->pViewportState->scissorCount);
1400 }
1401 }
1402
1403 if (states & RADV_DYNAMIC_LINE_WIDTH) {
1404 assert(pCreateInfo->pRasterizationState);
1405 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
1406 }
1407
1408 if (states & RADV_DYNAMIC_DEPTH_BIAS) {
1409 assert(pCreateInfo->pRasterizationState);
1410 dynamic->depth_bias.bias =
1411 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
1412 dynamic->depth_bias.clamp =
1413 pCreateInfo->pRasterizationState->depthBiasClamp;
1414 dynamic->depth_bias.slope =
1415 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
1416 }
1417
1418 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1419 *
1420 * pColorBlendState is [...] NULL if the pipeline has rasterization
1421 * disabled or if the subpass of the render pass the pipeline is
1422 * created against does not use any color attachments.
1423 */
1424 if (subpass->has_color_att && states & RADV_DYNAMIC_BLEND_CONSTANTS) {
1425 assert(pCreateInfo->pColorBlendState);
1426 typed_memcpy(dynamic->blend_constants,
1427 pCreateInfo->pColorBlendState->blendConstants, 4);
1428 }
1429
1430 /* If there is no depthstencil attachment, then don't read
1431 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1432 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1433 * no need to override the depthstencil defaults in
1434 * radv_pipeline::dynamic_state when there is no depthstencil attachment.
1435 *
1436 * Section 9.2 of the Vulkan 1.0.15 spec says:
1437 *
1438 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1439 * disabled or if the subpass of the render pass the pipeline is created
1440 * against does not use a depth/stencil attachment.
1441 */
1442 if (needed_states && subpass->depth_stencil_attachment) {
1443 assert(pCreateInfo->pDepthStencilState);
1444
1445 if (states & RADV_DYNAMIC_DEPTH_BOUNDS) {
1446 dynamic->depth_bounds.min =
1447 pCreateInfo->pDepthStencilState->minDepthBounds;
1448 dynamic->depth_bounds.max =
1449 pCreateInfo->pDepthStencilState->maxDepthBounds;
1450 }
1451
1452 if (states & RADV_DYNAMIC_STENCIL_COMPARE_MASK) {
1453 dynamic->stencil_compare_mask.front =
1454 pCreateInfo->pDepthStencilState->front.compareMask;
1455 dynamic->stencil_compare_mask.back =
1456 pCreateInfo->pDepthStencilState->back.compareMask;
1457 }
1458
1459 if (states & RADV_DYNAMIC_STENCIL_WRITE_MASK) {
1460 dynamic->stencil_write_mask.front =
1461 pCreateInfo->pDepthStencilState->front.writeMask;
1462 dynamic->stencil_write_mask.back =
1463 pCreateInfo->pDepthStencilState->back.writeMask;
1464 }
1465
1466 if (states & RADV_DYNAMIC_STENCIL_REFERENCE) {
1467 dynamic->stencil_reference.front =
1468 pCreateInfo->pDepthStencilState->front.reference;
1469 dynamic->stencil_reference.back =
1470 pCreateInfo->pDepthStencilState->back.reference;
1471 }
1472 }
1473
1474 const VkPipelineDiscardRectangleStateCreateInfoEXT *discard_rectangle_info =
1475 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);
1476 if (needed_states & RADV_DYNAMIC_DISCARD_RECTANGLE) {
1477 dynamic->discard_rectangle.count = discard_rectangle_info->discardRectangleCount;
1478 if (states & RADV_DYNAMIC_DISCARD_RECTANGLE) {
1479 typed_memcpy(dynamic->discard_rectangle.rectangles,
1480 discard_rectangle_info->pDiscardRectangles,
1481 discard_rectangle_info->discardRectangleCount);
1482 }
1483 }
1484
1485 if (needed_states & RADV_DYNAMIC_SAMPLE_LOCATIONS) {
1486 const VkPipelineSampleLocationsStateCreateInfoEXT *sample_location_info =
1487 vk_find_struct_const(pCreateInfo->pMultisampleState->pNext,
1488 PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT);
1489 /* If sampleLocationsEnable is VK_FALSE, the default sample
1490 * locations are used and the values specified in
1491 * sampleLocationsInfo are ignored.
1492 */
1493 if (sample_location_info->sampleLocationsEnable) {
1494 const VkSampleLocationsInfoEXT *pSampleLocationsInfo =
1495 &sample_location_info->sampleLocationsInfo;
1496
1497 assert(pSampleLocationsInfo->sampleLocationsCount <= MAX_SAMPLE_LOCATIONS);
1498
1499 dynamic->sample_location.per_pixel = pSampleLocationsInfo->sampleLocationsPerPixel;
1500 dynamic->sample_location.grid_size = pSampleLocationsInfo->sampleLocationGridSize;
1501 dynamic->sample_location.count = pSampleLocationsInfo->sampleLocationsCount;
1502 typed_memcpy(&dynamic->sample_location.locations[0],
1503 pSampleLocationsInfo->pSampleLocations,
1504 pSampleLocationsInfo->sampleLocationsCount);
1505 }
1506 }
1507
1508 const VkPipelineRasterizationLineStateCreateInfoEXT *rast_line_info =
1509 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1510 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);
1511 if (needed_states & RADV_DYNAMIC_LINE_STIPPLE) {
1512 dynamic->line_stipple.factor = rast_line_info->lineStippleFactor;
1513 dynamic->line_stipple.pattern = rast_line_info->lineStipplePattern;
1514 }
1515
1516 pipeline->dynamic_state.mask = states;
1517 }
1518
1519 static void
1520 gfx9_get_gs_info(const struct radv_pipeline_key *key,
1521 const struct radv_pipeline *pipeline,
1522 nir_shader **nir,
1523 struct radv_shader_info *infos,
1524 struct gfx9_gs_info *out)
1525 {
1526 struct radv_shader_info *gs_info = &infos[MESA_SHADER_GEOMETRY];
1527 struct radv_es_output_info *es_info;
1528 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9)
1529 es_info = nir[MESA_SHADER_TESS_CTRL] ? &gs_info->tes.es_info : &gs_info->vs.es_info;
1530 else
1531 es_info = nir[MESA_SHADER_TESS_CTRL] ?
1532 &infos[MESA_SHADER_TESS_EVAL].tes.es_info :
1533 &infos[MESA_SHADER_VERTEX].vs.es_info;
1534
1535 unsigned gs_num_invocations = MAX2(gs_info->gs.invocations, 1);
1536 bool uses_adjacency;
1537 switch(key->topology) {
1538 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1539 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1540 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1541 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1542 uses_adjacency = true;
1543 break;
1544 default:
1545 uses_adjacency = false;
1546 break;
1547 }
1548
1549 /* All these are in dwords: */
1550 /* We can't allow using the whole LDS, because GS waves compete with
1551 * other shader stages for LDS space. */
1552 const unsigned max_lds_size = 8 * 1024;
1553 const unsigned esgs_itemsize = es_info->esgs_itemsize / 4;
1554 unsigned esgs_lds_size;
1555
1556 /* All these are per subgroup: */
1557 const unsigned max_out_prims = 32 * 1024;
1558 const unsigned max_es_verts = 255;
1559 const unsigned ideal_gs_prims = 64;
1560 unsigned max_gs_prims, gs_prims;
1561 unsigned min_es_verts, es_verts, worst_case_es_verts;
1562
1563 if (uses_adjacency || gs_num_invocations > 1)
1564 max_gs_prims = 127 / gs_num_invocations;
1565 else
1566 max_gs_prims = 255;
1567
1568 /* MAX_PRIMS_PER_SUBGROUP = gs_prims * max_vert_out * gs_invocations.
1569 * Make sure we don't go over the maximum value.
1570 */
1571 if (gs_info->gs.vertices_out > 0) {
1572 max_gs_prims = MIN2(max_gs_prims,
1573 max_out_prims /
1574 (gs_info->gs.vertices_out * gs_num_invocations));
1575 }
1576 assert(max_gs_prims > 0);
1577
1578 /* If the primitive has adjacency, halve the number of vertices
1579 * that will be reused in multiple primitives.
1580 */
1581 min_es_verts = gs_info->gs.vertices_in / (uses_adjacency ? 2 : 1);
1582
1583 gs_prims = MIN2(ideal_gs_prims, max_gs_prims);
1584 worst_case_es_verts = MIN2(min_es_verts * gs_prims, max_es_verts);
1585
1586 /* Compute ESGS LDS size based on the worst case number of ES vertices
1587 * needed to create the target number of GS prims per subgroup.
1588 */
1589 esgs_lds_size = esgs_itemsize * worst_case_es_verts;
1590
1591 /* If total LDS usage is too big, refactor partitions based on ratio
1592 * of ESGS item sizes.
1593 */
1594 if (esgs_lds_size > max_lds_size) {
1595 /* Our target GS Prims Per Subgroup was too large. Calculate
1596 * the maximum number of GS Prims Per Subgroup that will fit
1597 * into LDS, capped by the maximum that the hardware can support.
1598 */
1599 gs_prims = MIN2((max_lds_size / (esgs_itemsize * min_es_verts)),
1600 max_gs_prims);
1601 assert(gs_prims > 0);
1602 worst_case_es_verts = MIN2(min_es_verts * gs_prims,
1603 max_es_verts);
1604
1605 esgs_lds_size = esgs_itemsize * worst_case_es_verts;
1606 assert(esgs_lds_size <= max_lds_size);
1607 }
1608
1609 /* Now calculate remaining ESGS information. */
1610 if (esgs_lds_size)
1611 es_verts = MIN2(esgs_lds_size / esgs_itemsize, max_es_verts);
1612 else
1613 es_verts = max_es_verts;
1614
1615 /* Vertices for adjacency primitives are not always reused, so restore
1616 * it for ES_VERTS_PER_SUBGRP.
1617 */
1618 min_es_verts = gs_info->gs.vertices_in;
1619
1620 /* For normal primitives, the VGT only checks if they are past the ES
1621 * verts per subgroup after allocating a full GS primitive and if they
1622 * are, kick off a new subgroup. But if those additional ES verts are
1623 * unique (e.g. not reused) we need to make sure there is enough LDS
1624 * space to account for those ES verts beyond ES_VERTS_PER_SUBGRP.
1625 */
1626 es_verts -= min_es_verts - 1;
1627
1628 uint32_t es_verts_per_subgroup = es_verts;
1629 uint32_t gs_prims_per_subgroup = gs_prims;
1630 uint32_t gs_inst_prims_in_subgroup = gs_prims * gs_num_invocations;
1631 uint32_t max_prims_per_subgroup = gs_inst_prims_in_subgroup * gs_info->gs.vertices_out;
1632 out->lds_size = align(esgs_lds_size, 128) / 128;
1633 out->vgt_gs_onchip_cntl = S_028A44_ES_VERTS_PER_SUBGRP(es_verts_per_subgroup) |
1634 S_028A44_GS_PRIMS_PER_SUBGRP(gs_prims_per_subgroup) |
1635 S_028A44_GS_INST_PRIMS_IN_SUBGRP(gs_inst_prims_in_subgroup);
1636 out->vgt_gs_max_prims_per_subgroup = S_028A94_MAX_PRIMS_PER_SUBGROUP(max_prims_per_subgroup);
1637 out->vgt_esgs_ring_itemsize = esgs_itemsize;
1638 assert(max_prims_per_subgroup <= max_out_prims);
1639 }
1640
1641 static void clamp_gsprims_to_esverts(unsigned *max_gsprims, unsigned max_esverts,
1642 unsigned min_verts_per_prim, bool use_adjacency)
1643 {
1644 unsigned max_reuse = max_esverts - min_verts_per_prim;
1645 if (use_adjacency)
1646 max_reuse /= 2;
1647 *max_gsprims = MIN2(*max_gsprims, 1 + max_reuse);
1648 }
1649
1650 static unsigned
1651 radv_get_num_input_vertices(nir_shader **nir)
1652 {
1653 if (nir[MESA_SHADER_GEOMETRY]) {
1654 nir_shader *gs = nir[MESA_SHADER_GEOMETRY];
1655
1656 return gs->info.gs.vertices_in;
1657 }
1658
1659 if (nir[MESA_SHADER_TESS_CTRL]) {
1660 nir_shader *tes = nir[MESA_SHADER_TESS_EVAL];
1661
1662 if (tes->info.tess.point_mode)
1663 return 1;
1664 if (tes->info.tess.primitive_mode == GL_ISOLINES)
1665 return 2;
1666 return 3;
1667 }
1668
1669 return 3;
1670 }
1671
1672 static void
1673 gfx10_get_ngg_info(const struct radv_pipeline_key *key,
1674 struct radv_pipeline *pipeline,
1675 nir_shader **nir,
1676 struct radv_shader_info *infos,
1677 struct gfx10_ngg_info *ngg)
1678 {
1679 struct radv_shader_info *gs_info = &infos[MESA_SHADER_GEOMETRY];
1680 struct radv_es_output_info *es_info =
1681 nir[MESA_SHADER_TESS_CTRL] ? &gs_info->tes.es_info : &gs_info->vs.es_info;
1682 unsigned gs_type = nir[MESA_SHADER_GEOMETRY] ? MESA_SHADER_GEOMETRY : MESA_SHADER_VERTEX;
1683 unsigned max_verts_per_prim = radv_get_num_input_vertices(nir);
1684 unsigned min_verts_per_prim =
1685 gs_type == MESA_SHADER_GEOMETRY ? max_verts_per_prim : 1;
1686 unsigned gs_num_invocations = nir[MESA_SHADER_GEOMETRY] ? MAX2(gs_info->gs.invocations, 1) : 1;
1687 bool uses_adjacency;
1688 switch(key->topology) {
1689 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1690 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1691 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1692 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1693 uses_adjacency = true;
1694 break;
1695 default:
1696 uses_adjacency = false;
1697 break;
1698 }
1699
1700 /* All these are in dwords: */
1701 /* We can't allow using the whole LDS, because GS waves compete with
1702 * other shader stages for LDS space.
1703 *
1704 * TODO: We should really take the shader's internal LDS use into
1705 * account. The linker will fail if the size is greater than
1706 * 8K dwords.
1707 */
1708 const unsigned max_lds_size = 8 * 1024 - 768;
1709 const unsigned target_lds_size = max_lds_size;
1710 unsigned esvert_lds_size = 0;
1711 unsigned gsprim_lds_size = 0;
1712
1713 /* All these are per subgroup: */
1714 bool max_vert_out_per_gs_instance = false;
1715 unsigned max_esverts_base = 256;
1716 unsigned max_gsprims_base = 128; /* default prim group size clamp */
1717
1718 /* Hardware has the following non-natural restrictions on the value
1719 * of GE_CNTL.VERT_GRP_SIZE based on based on the primitive type of
1720 * the draw:
1721 * - at most 252 for any line input primitive type
1722 * - at most 251 for any quad input primitive type
1723 * - at most 251 for triangle strips with adjacency (this happens to
1724 * be the natural limit for triangle *lists* with adjacency)
1725 */
1726 max_esverts_base = MIN2(max_esverts_base, 251 + max_verts_per_prim - 1);
1727
1728 if (gs_type == MESA_SHADER_GEOMETRY) {
1729 unsigned max_out_verts_per_gsprim =
1730 gs_info->gs.vertices_out * gs_num_invocations;
1731
1732 if (max_out_verts_per_gsprim <= 256) {
1733 if (max_out_verts_per_gsprim) {
1734 max_gsprims_base = MIN2(max_gsprims_base,
1735 256 / max_out_verts_per_gsprim);
1736 }
1737 } else {
1738 /* Use special multi-cycling mode in which each GS
1739 * instance gets its own subgroup. Does not work with
1740 * tessellation. */
1741 max_vert_out_per_gs_instance = true;
1742 max_gsprims_base = 1;
1743 max_out_verts_per_gsprim = gs_info->gs.vertices_out;
1744 }
1745
1746 esvert_lds_size = es_info->esgs_itemsize / 4;
1747 gsprim_lds_size = (gs_info->gs.gsvs_vertex_size / 4 + 1) * max_out_verts_per_gsprim;
1748 } else {
1749 /* VS and TES. */
1750 /* LDS size for passing data from GS to ES. */
1751 struct radv_streamout_info *so_info = nir[MESA_SHADER_TESS_CTRL]
1752 ? &infos[MESA_SHADER_TESS_EVAL].so
1753 : &infos[MESA_SHADER_VERTEX].so;
1754
1755 if (so_info->num_outputs)
1756 esvert_lds_size = 4 * so_info->num_outputs + 1;
1757
1758 /* GS stores Primitive IDs (one DWORD) into LDS at the address
1759 * corresponding to the ES thread of the provoking vertex. All
1760 * ES threads load and export PrimitiveID for their thread.
1761 */
1762 if (!nir[MESA_SHADER_TESS_CTRL] &&
1763 infos[MESA_SHADER_VERTEX].vs.outinfo.export_prim_id)
1764 esvert_lds_size = MAX2(esvert_lds_size, 1);
1765 }
1766
1767 unsigned max_gsprims = max_gsprims_base;
1768 unsigned max_esverts = max_esverts_base;
1769
1770 if (esvert_lds_size)
1771 max_esverts = MIN2(max_esverts, target_lds_size / esvert_lds_size);
1772 if (gsprim_lds_size)
1773 max_gsprims = MIN2(max_gsprims, target_lds_size / gsprim_lds_size);
1774
1775 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1776 clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, uses_adjacency);
1777 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1778
1779 if (esvert_lds_size || gsprim_lds_size) {
1780 /* Now that we have a rough proportionality between esverts
1781 * and gsprims based on the primitive type, scale both of them
1782 * down simultaneously based on required LDS space.
1783 *
1784 * We could be smarter about this if we knew how much vertex
1785 * reuse to expect.
1786 */
1787 unsigned lds_total = max_esverts * esvert_lds_size +
1788 max_gsprims * gsprim_lds_size;
1789 if (lds_total > target_lds_size) {
1790 max_esverts = max_esverts * target_lds_size / lds_total;
1791 max_gsprims = max_gsprims * target_lds_size / lds_total;
1792
1793 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1794 clamp_gsprims_to_esverts(&max_gsprims, max_esverts,
1795 min_verts_per_prim, uses_adjacency);
1796 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1797 }
1798 }
1799
1800 /* Round up towards full wave sizes for better ALU utilization. */
1801 if (!max_vert_out_per_gs_instance) {
1802 unsigned orig_max_esverts;
1803 unsigned orig_max_gsprims;
1804 unsigned wavesize;
1805
1806 if (gs_type == MESA_SHADER_GEOMETRY) {
1807 wavesize = gs_info->wave_size;
1808 } else {
1809 wavesize = nir[MESA_SHADER_TESS_CTRL]
1810 ? infos[MESA_SHADER_TESS_EVAL].wave_size
1811 : infos[MESA_SHADER_VERTEX].wave_size;
1812 }
1813
1814 do {
1815 orig_max_esverts = max_esverts;
1816 orig_max_gsprims = max_gsprims;
1817
1818 max_esverts = align(max_esverts, wavesize);
1819 max_esverts = MIN2(max_esverts, max_esverts_base);
1820 if (esvert_lds_size)
1821 max_esverts = MIN2(max_esverts,
1822 (max_lds_size - max_gsprims * gsprim_lds_size) /
1823 esvert_lds_size);
1824 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1825
1826 max_gsprims = align(max_gsprims, wavesize);
1827 max_gsprims = MIN2(max_gsprims, max_gsprims_base);
1828 if (gsprim_lds_size)
1829 max_gsprims = MIN2(max_gsprims,
1830 (max_lds_size - max_esverts * esvert_lds_size) /
1831 gsprim_lds_size);
1832 clamp_gsprims_to_esverts(&max_gsprims, max_esverts,
1833 min_verts_per_prim, uses_adjacency);
1834 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1835 } while (orig_max_esverts != max_esverts || orig_max_gsprims != max_gsprims);
1836 }
1837
1838 /* Hardware restriction: minimum value of max_esverts */
1839 max_esverts = MAX2(max_esverts, 23 + max_verts_per_prim);
1840
1841 unsigned max_out_vertices =
1842 max_vert_out_per_gs_instance ? gs_info->gs.vertices_out :
1843 gs_type == MESA_SHADER_GEOMETRY ?
1844 max_gsprims * gs_num_invocations * gs_info->gs.vertices_out :
1845 max_esverts;
1846 assert(max_out_vertices <= 256);
1847
1848 unsigned prim_amp_factor = 1;
1849 if (gs_type == MESA_SHADER_GEOMETRY) {
1850 /* Number of output primitives per GS input primitive after
1851 * GS instancing. */
1852 prim_amp_factor = gs_info->gs.vertices_out;
1853 }
1854
1855 /* The GE only checks against the maximum number of ES verts after
1856 * allocating a full GS primitive. So we need to ensure that whenever
1857 * this check passes, there is enough space for a full primitive without
1858 * vertex reuse.
1859 */
1860 ngg->hw_max_esverts = max_esverts - max_verts_per_prim + 1;
1861 ngg->max_gsprims = max_gsprims;
1862 ngg->max_out_verts = max_out_vertices;
1863 ngg->prim_amp_factor = prim_amp_factor;
1864 ngg->max_vert_out_per_gs_instance = max_vert_out_per_gs_instance;
1865 ngg->ngg_emit_size = max_gsprims * gsprim_lds_size;
1866 ngg->esgs_ring_size = 4 * max_esverts * esvert_lds_size;
1867
1868 if (gs_type == MESA_SHADER_GEOMETRY) {
1869 ngg->vgt_esgs_ring_itemsize = es_info->esgs_itemsize / 4;
1870 } else {
1871 ngg->vgt_esgs_ring_itemsize = 1;
1872 }
1873
1874 pipeline->graphics.esgs_ring_size = ngg->esgs_ring_size;
1875
1876 assert(ngg->hw_max_esverts >= 24); /* HW limitation */
1877 }
1878
1879 static void
1880 calculate_gs_ring_sizes(struct radv_pipeline *pipeline,
1881 const struct gfx9_gs_info *gs)
1882 {
1883 struct radv_device *device = pipeline->device;
1884 unsigned num_se = device->physical_device->rad_info.max_se;
1885 unsigned wave_size = 64;
1886 unsigned max_gs_waves = 32 * num_se; /* max 32 per SE on GCN */
1887 /* On GFX6-GFX7, the value comes from VGT_GS_VERTEX_REUSE = 16.
1888 * On GFX8+, the value comes from VGT_VERTEX_REUSE_BLOCK_CNTL = 30 (+2).
1889 */
1890 unsigned gs_vertex_reuse =
1891 (device->physical_device->rad_info.chip_class >= GFX8 ? 32 : 16) * num_se;
1892 unsigned alignment = 256 * num_se;
1893 /* The maximum size is 63.999 MB per SE. */
1894 unsigned max_size = ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se;
1895 struct radv_shader_info *gs_info = &pipeline->shaders[MESA_SHADER_GEOMETRY]->info;
1896
1897 /* Calculate the minimum size. */
1898 unsigned min_esgs_ring_size = align(gs->vgt_esgs_ring_itemsize * 4 * gs_vertex_reuse *
1899 wave_size, alignment);
1900 /* These are recommended sizes, not minimum sizes. */
1901 unsigned esgs_ring_size = max_gs_waves * 2 * wave_size *
1902 gs->vgt_esgs_ring_itemsize * 4 * gs_info->gs.vertices_in;
1903 unsigned gsvs_ring_size = max_gs_waves * 2 * wave_size *
1904 gs_info->gs.max_gsvs_emit_size;
1905
1906 min_esgs_ring_size = align(min_esgs_ring_size, alignment);
1907 esgs_ring_size = align(esgs_ring_size, alignment);
1908 gsvs_ring_size = align(gsvs_ring_size, alignment);
1909
1910 if (pipeline->device->physical_device->rad_info.chip_class <= GFX8)
1911 pipeline->graphics.esgs_ring_size = CLAMP(esgs_ring_size, min_esgs_ring_size, max_size);
1912
1913 pipeline->graphics.gsvs_ring_size = MIN2(gsvs_ring_size, max_size);
1914 }
1915
1916 static void si_multiwave_lds_size_workaround(struct radv_device *device,
1917 unsigned *lds_size)
1918 {
1919 /* If tessellation is all offchip and on-chip GS isn't used, this
1920 * workaround is not needed.
1921 */
1922 return;
1923
1924 /* SPI barrier management bug:
1925 * Make sure we have at least 4k of LDS in use to avoid the bug.
1926 * It applies to workgroup sizes of more than one wavefront.
1927 */
1928 if (device->physical_device->rad_info.family == CHIP_BONAIRE ||
1929 device->physical_device->rad_info.family == CHIP_KABINI)
1930 *lds_size = MAX2(*lds_size, 8);
1931 }
1932
1933 struct radv_shader_variant *
1934 radv_get_shader(struct radv_pipeline *pipeline,
1935 gl_shader_stage stage)
1936 {
1937 if (stage == MESA_SHADER_VERTEX) {
1938 if (pipeline->shaders[MESA_SHADER_VERTEX])
1939 return pipeline->shaders[MESA_SHADER_VERTEX];
1940 if (pipeline->shaders[MESA_SHADER_TESS_CTRL])
1941 return pipeline->shaders[MESA_SHADER_TESS_CTRL];
1942 if (pipeline->shaders[MESA_SHADER_GEOMETRY])
1943 return pipeline->shaders[MESA_SHADER_GEOMETRY];
1944 } else if (stage == MESA_SHADER_TESS_EVAL) {
1945 if (!radv_pipeline_has_tess(pipeline))
1946 return NULL;
1947 if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
1948 return pipeline->shaders[MESA_SHADER_TESS_EVAL];
1949 if (pipeline->shaders[MESA_SHADER_GEOMETRY])
1950 return pipeline->shaders[MESA_SHADER_GEOMETRY];
1951 }
1952 return pipeline->shaders[stage];
1953 }
1954
1955 static struct radv_tessellation_state
1956 calculate_tess_state(struct radv_pipeline *pipeline,
1957 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1958 {
1959 unsigned num_tcs_input_cp;
1960 unsigned num_tcs_output_cp;
1961 unsigned lds_size;
1962 unsigned num_patches;
1963 struct radv_tessellation_state tess = {0};
1964
1965 num_tcs_input_cp = pCreateInfo->pTessellationState->patchControlPoints;
1966 num_tcs_output_cp = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.tcs_vertices_out; //TCS VERTICES OUT
1967 num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
1968
1969 lds_size = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.lds_size;
1970
1971 if (pipeline->device->physical_device->rad_info.chip_class >= GFX7) {
1972 assert(lds_size <= 65536);
1973 lds_size = align(lds_size, 512) / 512;
1974 } else {
1975 assert(lds_size <= 32768);
1976 lds_size = align(lds_size, 256) / 256;
1977 }
1978 si_multiwave_lds_size_workaround(pipeline->device, &lds_size);
1979
1980 tess.lds_size = lds_size;
1981
1982 tess.ls_hs_config = S_028B58_NUM_PATCHES(num_patches) |
1983 S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp) |
1984 S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp);
1985 tess.num_patches = num_patches;
1986
1987 struct radv_shader_variant *tes = radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL);
1988 unsigned type = 0, partitioning = 0, topology = 0, distribution_mode = 0;
1989
1990 switch (tes->info.tes.primitive_mode) {
1991 case GL_TRIANGLES:
1992 type = V_028B6C_TESS_TRIANGLE;
1993 break;
1994 case GL_QUADS:
1995 type = V_028B6C_TESS_QUAD;
1996 break;
1997 case GL_ISOLINES:
1998 type = V_028B6C_TESS_ISOLINE;
1999 break;
2000 }
2001
2002 switch (tes->info.tes.spacing) {
2003 case TESS_SPACING_EQUAL:
2004 partitioning = V_028B6C_PART_INTEGER;
2005 break;
2006 case TESS_SPACING_FRACTIONAL_ODD:
2007 partitioning = V_028B6C_PART_FRAC_ODD;
2008 break;
2009 case TESS_SPACING_FRACTIONAL_EVEN:
2010 partitioning = V_028B6C_PART_FRAC_EVEN;
2011 break;
2012 default:
2013 break;
2014 }
2015
2016 bool ccw = tes->info.tes.ccw;
2017 const VkPipelineTessellationDomainOriginStateCreateInfo *domain_origin_state =
2018 vk_find_struct_const(pCreateInfo->pTessellationState,
2019 PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO);
2020
2021 if (domain_origin_state && domain_origin_state->domainOrigin != VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT)
2022 ccw = !ccw;
2023
2024 if (tes->info.tes.point_mode)
2025 topology = V_028B6C_OUTPUT_POINT;
2026 else if (tes->info.tes.primitive_mode == GL_ISOLINES)
2027 topology = V_028B6C_OUTPUT_LINE;
2028 else if (ccw)
2029 topology = V_028B6C_OUTPUT_TRIANGLE_CCW;
2030 else
2031 topology = V_028B6C_OUTPUT_TRIANGLE_CW;
2032
2033 if (pipeline->device->physical_device->rad_info.has_distributed_tess) {
2034 if (pipeline->device->physical_device->rad_info.family == CHIP_FIJI ||
2035 pipeline->device->physical_device->rad_info.family >= CHIP_POLARIS10)
2036 distribution_mode = V_028B6C_DISTRIBUTION_MODE_TRAPEZOIDS;
2037 else
2038 distribution_mode = V_028B6C_DISTRIBUTION_MODE_DONUTS;
2039 } else
2040 distribution_mode = V_028B6C_DISTRIBUTION_MODE_NO_DIST;
2041
2042 tess.tf_param = S_028B6C_TYPE(type) |
2043 S_028B6C_PARTITIONING(partitioning) |
2044 S_028B6C_TOPOLOGY(topology) |
2045 S_028B6C_DISTRIBUTION_MODE(distribution_mode);
2046
2047 return tess;
2048 }
2049
2050 static const struct radv_prim_vertex_count prim_size_table[] = {
2051 [V_008958_DI_PT_NONE] = {0, 0},
2052 [V_008958_DI_PT_POINTLIST] = {1, 1},
2053 [V_008958_DI_PT_LINELIST] = {2, 2},
2054 [V_008958_DI_PT_LINESTRIP] = {2, 1},
2055 [V_008958_DI_PT_TRILIST] = {3, 3},
2056 [V_008958_DI_PT_TRIFAN] = {3, 1},
2057 [V_008958_DI_PT_TRISTRIP] = {3, 1},
2058 [V_008958_DI_PT_LINELIST_ADJ] = {4, 4},
2059 [V_008958_DI_PT_LINESTRIP_ADJ] = {4, 1},
2060 [V_008958_DI_PT_TRILIST_ADJ] = {6, 6},
2061 [V_008958_DI_PT_TRISTRIP_ADJ] = {6, 2},
2062 [V_008958_DI_PT_RECTLIST] = {3, 3},
2063 [V_008958_DI_PT_LINELOOP] = {2, 1},
2064 [V_008958_DI_PT_POLYGON] = {3, 1},
2065 [V_008958_DI_PT_2D_TRI_STRIP] = {0, 0},
2066 };
2067
2068 static const struct radv_vs_output_info *get_vs_output_info(const struct radv_pipeline *pipeline)
2069 {
2070 if (radv_pipeline_has_gs(pipeline))
2071 if (radv_pipeline_has_ngg(pipeline))
2072 return &pipeline->shaders[MESA_SHADER_GEOMETRY]->info.vs.outinfo;
2073 else
2074 return &pipeline->gs_copy_shader->info.vs.outinfo;
2075 else if (radv_pipeline_has_tess(pipeline))
2076 return &pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.outinfo;
2077 else
2078 return &pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.outinfo;
2079 }
2080
2081 static void
2082 radv_link_shaders(struct radv_pipeline *pipeline, nir_shader **shaders)
2083 {
2084 nir_shader* ordered_shaders[MESA_SHADER_STAGES];
2085 int shader_count = 0;
2086
2087 if(shaders[MESA_SHADER_FRAGMENT]) {
2088 ordered_shaders[shader_count++] = shaders[MESA_SHADER_FRAGMENT];
2089 }
2090 if(shaders[MESA_SHADER_GEOMETRY]) {
2091 ordered_shaders[shader_count++] = shaders[MESA_SHADER_GEOMETRY];
2092 }
2093 if(shaders[MESA_SHADER_TESS_EVAL]) {
2094 ordered_shaders[shader_count++] = shaders[MESA_SHADER_TESS_EVAL];
2095 }
2096 if(shaders[MESA_SHADER_TESS_CTRL]) {
2097 ordered_shaders[shader_count++] = shaders[MESA_SHADER_TESS_CTRL];
2098 }
2099 if(shaders[MESA_SHADER_VERTEX]) {
2100 ordered_shaders[shader_count++] = shaders[MESA_SHADER_VERTEX];
2101 }
2102
2103 if (shader_count > 1) {
2104 unsigned first = ordered_shaders[shader_count - 1]->info.stage;
2105 unsigned last = ordered_shaders[0]->info.stage;
2106
2107 if (ordered_shaders[0]->info.stage == MESA_SHADER_FRAGMENT &&
2108 ordered_shaders[1]->info.has_transform_feedback_varyings)
2109 nir_link_xfb_varyings(ordered_shaders[1], ordered_shaders[0]);
2110
2111 for (int i = 0; i < shader_count; ++i) {
2112 nir_variable_mode mask = 0;
2113
2114 if (ordered_shaders[i]->info.stage != first)
2115 mask = mask | nir_var_shader_in;
2116
2117 if (ordered_shaders[i]->info.stage != last)
2118 mask = mask | nir_var_shader_out;
2119
2120 nir_lower_io_to_scalar_early(ordered_shaders[i], mask);
2121 radv_optimize_nir(ordered_shaders[i], false, false);
2122 }
2123 }
2124
2125 for (int i = 1; i < shader_count; ++i) {
2126 nir_lower_io_arrays_to_elements(ordered_shaders[i],
2127 ordered_shaders[i - 1]);
2128
2129 if (nir_link_opt_varyings(ordered_shaders[i],
2130 ordered_shaders[i - 1]))
2131 radv_optimize_nir(ordered_shaders[i - 1], false, false);
2132
2133 nir_remove_dead_variables(ordered_shaders[i],
2134 nir_var_shader_out, NULL);
2135 nir_remove_dead_variables(ordered_shaders[i - 1],
2136 nir_var_shader_in, NULL);
2137
2138 bool progress = nir_remove_unused_varyings(ordered_shaders[i],
2139 ordered_shaders[i - 1]);
2140
2141 nir_compact_varyings(ordered_shaders[i],
2142 ordered_shaders[i - 1], true);
2143
2144 if (progress) {
2145 if (nir_lower_global_vars_to_local(ordered_shaders[i])) {
2146 ac_lower_indirect_derefs(ordered_shaders[i],
2147 pipeline->device->physical_device->rad_info.chip_class);
2148 }
2149 radv_optimize_nir(ordered_shaders[i], false, false);
2150
2151 if (nir_lower_global_vars_to_local(ordered_shaders[i - 1])) {
2152 ac_lower_indirect_derefs(ordered_shaders[i - 1],
2153 pipeline->device->physical_device->rad_info.chip_class);
2154 }
2155 radv_optimize_nir(ordered_shaders[i - 1], false, false);
2156 }
2157 }
2158 }
2159
2160 static void
2161 radv_set_linked_driver_locations(struct radv_pipeline *pipeline, nir_shader **shaders,
2162 struct radv_shader_info infos[MESA_SHADER_STAGES])
2163 {
2164 bool has_tess = shaders[MESA_SHADER_TESS_CTRL];
2165 bool has_gs = shaders[MESA_SHADER_GEOMETRY];
2166
2167 if (!has_tess && !has_gs)
2168 return;
2169
2170 unsigned vs_info_idx = MESA_SHADER_VERTEX;
2171 unsigned tes_info_idx = MESA_SHADER_TESS_EVAL;
2172
2173 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {
2174 /* These are merged into the next stage */
2175 vs_info_idx = has_tess ? MESA_SHADER_TESS_CTRL : MESA_SHADER_GEOMETRY;
2176 tes_info_idx = has_gs ? MESA_SHADER_GEOMETRY : MESA_SHADER_TESS_EVAL;
2177 }
2178
2179 if (has_tess) {
2180 nir_linked_io_var_info vs2tcs =
2181 nir_assign_linked_io_var_locations(shaders[MESA_SHADER_VERTEX], shaders[MESA_SHADER_TESS_CTRL]);
2182 nir_linked_io_var_info tcs2tes =
2183 nir_assign_linked_io_var_locations(shaders[MESA_SHADER_TESS_CTRL], shaders[MESA_SHADER_TESS_EVAL]);
2184
2185 infos[vs_info_idx].vs.num_linked_outputs = vs2tcs.num_linked_io_vars;
2186 infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_inputs = vs2tcs.num_linked_io_vars;
2187 infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_outputs = tcs2tes.num_linked_io_vars;
2188 infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_patch_outputs = tcs2tes.num_linked_patch_io_vars;
2189 infos[tes_info_idx].tes.num_linked_inputs = tcs2tes.num_linked_io_vars;
2190 infos[tes_info_idx].tes.num_linked_patch_inputs = tcs2tes.num_linked_patch_io_vars;
2191
2192 if (has_gs) {
2193 nir_linked_io_var_info tes2gs =
2194 nir_assign_linked_io_var_locations(shaders[MESA_SHADER_TESS_EVAL], shaders[MESA_SHADER_GEOMETRY]);
2195
2196 infos[tes_info_idx].tes.num_linked_outputs = tes2gs.num_linked_io_vars;
2197 infos[MESA_SHADER_GEOMETRY].gs.num_linked_inputs = tes2gs.num_linked_io_vars;
2198 }
2199 } else if (has_gs) {
2200 nir_linked_io_var_info vs2gs =
2201 nir_assign_linked_io_var_locations(shaders[MESA_SHADER_VERTEX], shaders[MESA_SHADER_GEOMETRY]);
2202
2203 infos[vs_info_idx].vs.num_linked_outputs = vs2gs.num_linked_io_vars;
2204 infos[MESA_SHADER_GEOMETRY].gs.num_linked_inputs = vs2gs.num_linked_io_vars;
2205 }
2206 }
2207
2208 static uint32_t
2209 radv_get_attrib_stride(const VkPipelineVertexInputStateCreateInfo *input_state,
2210 uint32_t attrib_binding)
2211 {
2212 for (uint32_t i = 0; i < input_state->vertexBindingDescriptionCount; i++) {
2213 const VkVertexInputBindingDescription *input_binding =
2214 &input_state->pVertexBindingDescriptions[i];
2215
2216 if (input_binding->binding == attrib_binding)
2217 return input_binding->stride;
2218 }
2219
2220 return 0;
2221 }
2222
2223 static struct radv_pipeline_key
2224 radv_generate_graphics_pipeline_key(struct radv_pipeline *pipeline,
2225 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2226 const struct radv_blend_state *blend,
2227 bool has_view_index)
2228 {
2229 const VkPipelineVertexInputStateCreateInfo *input_state =
2230 pCreateInfo->pVertexInputState;
2231 const VkPipelineVertexInputDivisorStateCreateInfoEXT *divisor_state =
2232 vk_find_struct_const(input_state->pNext, PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT);
2233
2234 struct radv_pipeline_key key;
2235 memset(&key, 0, sizeof(key));
2236
2237 if (pCreateInfo->flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT)
2238 key.optimisations_disabled = 1;
2239
2240 key.has_multiview_view_index = has_view_index;
2241
2242 uint32_t binding_input_rate = 0;
2243 uint32_t instance_rate_divisors[MAX_VERTEX_ATTRIBS];
2244 for (unsigned i = 0; i < input_state->vertexBindingDescriptionCount; ++i) {
2245 if (input_state->pVertexBindingDescriptions[i].inputRate) {
2246 unsigned binding = input_state->pVertexBindingDescriptions[i].binding;
2247 binding_input_rate |= 1u << binding;
2248 instance_rate_divisors[binding] = 1;
2249 }
2250 }
2251 if (divisor_state) {
2252 for (unsigned i = 0; i < divisor_state->vertexBindingDivisorCount; ++i) {
2253 instance_rate_divisors[divisor_state->pVertexBindingDivisors[i].binding] =
2254 divisor_state->pVertexBindingDivisors[i].divisor;
2255 }
2256 }
2257
2258 for (unsigned i = 0; i < input_state->vertexAttributeDescriptionCount; ++i) {
2259 const VkVertexInputAttributeDescription *desc =
2260 &input_state->pVertexAttributeDescriptions[i];
2261 const struct vk_format_description *format_desc;
2262 unsigned location = desc->location;
2263 unsigned binding = desc->binding;
2264 unsigned num_format, data_format;
2265 int first_non_void;
2266
2267 if (binding_input_rate & (1u << binding)) {
2268 key.instance_rate_inputs |= 1u << location;
2269 key.instance_rate_divisors[location] = instance_rate_divisors[binding];
2270 }
2271
2272 format_desc = vk_format_description(desc->format);
2273 first_non_void = vk_format_get_first_non_void_channel(desc->format);
2274
2275 num_format = radv_translate_buffer_numformat(format_desc, first_non_void);
2276 data_format = radv_translate_buffer_dataformat(format_desc, first_non_void);
2277
2278 key.vertex_attribute_formats[location] = data_format | (num_format << 4);
2279 key.vertex_attribute_bindings[location] = desc->binding;
2280 key.vertex_attribute_offsets[location] = desc->offset;
2281 key.vertex_attribute_strides[location] = radv_get_attrib_stride(input_state, desc->binding);
2282
2283 if (pipeline->device->physical_device->rad_info.chip_class <= GFX8 &&
2284 pipeline->device->physical_device->rad_info.family != CHIP_STONEY) {
2285 VkFormat format = input_state->pVertexAttributeDescriptions[i].format;
2286 uint64_t adjust;
2287 switch(format) {
2288 case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
2289 case VK_FORMAT_A2B10G10R10_SNORM_PACK32:
2290 adjust = RADV_ALPHA_ADJUST_SNORM;
2291 break;
2292 case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
2293 case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:
2294 adjust = RADV_ALPHA_ADJUST_SSCALED;
2295 break;
2296 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
2297 case VK_FORMAT_A2B10G10R10_SINT_PACK32:
2298 adjust = RADV_ALPHA_ADJUST_SINT;
2299 break;
2300 default:
2301 adjust = 0;
2302 break;
2303 }
2304 key.vertex_alpha_adjust |= adjust << (2 * location);
2305 }
2306
2307 switch (desc->format) {
2308 case VK_FORMAT_B8G8R8A8_UNORM:
2309 case VK_FORMAT_B8G8R8A8_SNORM:
2310 case VK_FORMAT_B8G8R8A8_USCALED:
2311 case VK_FORMAT_B8G8R8A8_SSCALED:
2312 case VK_FORMAT_B8G8R8A8_UINT:
2313 case VK_FORMAT_B8G8R8A8_SINT:
2314 case VK_FORMAT_B8G8R8A8_SRGB:
2315 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
2316 case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
2317 case VK_FORMAT_A2R10G10B10_USCALED_PACK32:
2318 case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
2319 case VK_FORMAT_A2R10G10B10_UINT_PACK32:
2320 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
2321 key.vertex_post_shuffle |= 1 << location;
2322 break;
2323 default:
2324 break;
2325 }
2326 }
2327
2328 const VkPipelineTessellationStateCreateInfo *tess =
2329 radv_pipeline_get_tessellation_state(pCreateInfo);
2330 if (tess)
2331 key.tess_input_vertices = tess->patchControlPoints;
2332
2333 const VkPipelineMultisampleStateCreateInfo *vkms =
2334 radv_pipeline_get_multisample_state(pCreateInfo);
2335 if (vkms && vkms->rasterizationSamples > 1) {
2336 uint32_t num_samples = vkms->rasterizationSamples;
2337 uint32_t ps_iter_samples = radv_pipeline_get_ps_iter_samples(pCreateInfo);
2338 key.num_samples = num_samples;
2339 key.log2_ps_iter_samples = util_logbase2(ps_iter_samples);
2340 }
2341
2342 key.col_format = blend->spi_shader_col_format;
2343 if (pipeline->device->physical_device->rad_info.chip_class < GFX8)
2344 radv_pipeline_compute_get_int_clamp(pCreateInfo, &key.is_int8, &key.is_int10);
2345
2346 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10)
2347 key.topology = pCreateInfo->pInputAssemblyState->topology;
2348
2349 return key;
2350 }
2351
2352 static bool
2353 radv_nir_stage_uses_xfb(const nir_shader *nir)
2354 {
2355 nir_xfb_info *xfb = nir_gather_xfb_info(nir, NULL);
2356 bool uses_xfb = !!xfb;
2357
2358 ralloc_free(xfb);
2359 return uses_xfb;
2360 }
2361
2362 static void
2363 radv_fill_shader_keys(struct radv_device *device,
2364 struct radv_shader_variant_key *keys,
2365 const struct radv_pipeline_key *key,
2366 nir_shader **nir)
2367 {
2368 keys[MESA_SHADER_VERTEX].vs.instance_rate_inputs = key->instance_rate_inputs;
2369 keys[MESA_SHADER_VERTEX].vs.alpha_adjust = key->vertex_alpha_adjust;
2370 keys[MESA_SHADER_VERTEX].vs.post_shuffle = key->vertex_post_shuffle;
2371 for (unsigned i = 0; i < MAX_VERTEX_ATTRIBS; ++i) {
2372 keys[MESA_SHADER_VERTEX].vs.instance_rate_divisors[i] = key->instance_rate_divisors[i];
2373 keys[MESA_SHADER_VERTEX].vs.vertex_attribute_formats[i] = key->vertex_attribute_formats[i];
2374 keys[MESA_SHADER_VERTEX].vs.vertex_attribute_bindings[i] = key->vertex_attribute_bindings[i];
2375 keys[MESA_SHADER_VERTEX].vs.vertex_attribute_offsets[i] = key->vertex_attribute_offsets[i];
2376 keys[MESA_SHADER_VERTEX].vs.vertex_attribute_strides[i] = key->vertex_attribute_strides[i];
2377 }
2378 keys[MESA_SHADER_VERTEX].vs.outprim = si_conv_prim_to_gs_out(key->topology);
2379
2380 if (nir[MESA_SHADER_TESS_CTRL]) {
2381 keys[MESA_SHADER_VERTEX].vs_common_out.as_ls = true;
2382 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs = 0;
2383 keys[MESA_SHADER_TESS_CTRL].tcs.input_vertices = key->tess_input_vertices;
2384 keys[MESA_SHADER_TESS_CTRL].tcs.primitive_mode = nir[MESA_SHADER_TESS_EVAL]->info.tess.primitive_mode;
2385
2386 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));
2387 }
2388
2389 if (nir[MESA_SHADER_GEOMETRY]) {
2390 if (nir[MESA_SHADER_TESS_CTRL])
2391 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_es = true;
2392 else
2393 keys[MESA_SHADER_VERTEX].vs_common_out.as_es = true;
2394 }
2395
2396 if (device->physical_device->use_ngg) {
2397 if (nir[MESA_SHADER_TESS_CTRL]) {
2398 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = true;
2399 } else {
2400 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = true;
2401 }
2402
2403 if (nir[MESA_SHADER_TESS_CTRL] &&
2404 nir[MESA_SHADER_GEOMETRY] &&
2405 nir[MESA_SHADER_GEOMETRY]->info.gs.invocations *
2406 nir[MESA_SHADER_GEOMETRY]->info.gs.vertices_out > 256) {
2407 /* Fallback to the legacy path if tessellation is
2408 * enabled with extreme geometry because
2409 * EN_MAX_VERT_OUT_PER_GS_INSTANCE doesn't work and it
2410 * might hang.
2411 */
2412 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;
2413 }
2414
2415 if (!device->physical_device->use_ngg_gs) {
2416 if (nir[MESA_SHADER_GEOMETRY]) {
2417 if (nir[MESA_SHADER_TESS_CTRL])
2418 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;
2419 else
2420 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = false;
2421 }
2422 }
2423
2424 gl_shader_stage last_xfb_stage = MESA_SHADER_VERTEX;
2425
2426 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2427 if (nir[i])
2428 last_xfb_stage = i;
2429 }
2430
2431 bool uses_xfb = nir[last_xfb_stage] &&
2432 radv_nir_stage_uses_xfb(nir[last_xfb_stage]);
2433
2434 if (!device->physical_device->use_ngg_streamout && uses_xfb) {
2435 if (nir[MESA_SHADER_TESS_CTRL])
2436 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;
2437 else
2438 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = false;
2439 }
2440
2441 /* Determine if the pipeline is eligible for the NGG passthrough
2442 * mode. It can't be enabled for geometry shaders, for NGG
2443 * streamout or for vertex shaders that export the primitive ID
2444 * (this is checked later because we don't have the info here.)
2445 */
2446 if (!nir[MESA_SHADER_GEOMETRY] && !uses_xfb) {
2447 if (nir[MESA_SHADER_TESS_CTRL] &&
2448 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg) {
2449 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg_passthrough = true;
2450 } else if (nir[MESA_SHADER_VERTEX] &&
2451 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg) {
2452 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg_passthrough = true;
2453 }
2454 }
2455 }
2456
2457 for(int i = 0; i < MESA_SHADER_STAGES; ++i)
2458 keys[i].has_multiview_view_index = key->has_multiview_view_index;
2459
2460 keys[MESA_SHADER_FRAGMENT].fs.col_format = key->col_format;
2461 keys[MESA_SHADER_FRAGMENT].fs.is_int8 = key->is_int8;
2462 keys[MESA_SHADER_FRAGMENT].fs.is_int10 = key->is_int10;
2463 keys[MESA_SHADER_FRAGMENT].fs.log2_ps_iter_samples = key->log2_ps_iter_samples;
2464 keys[MESA_SHADER_FRAGMENT].fs.num_samples = key->num_samples;
2465
2466 if (nir[MESA_SHADER_COMPUTE]) {
2467 keys[MESA_SHADER_COMPUTE].cs.subgroup_size = key->compute_subgroup_size;
2468 }
2469 }
2470
2471 static uint8_t
2472 radv_get_wave_size(struct radv_device *device,
2473 const VkPipelineShaderStageCreateInfo *pStage,
2474 gl_shader_stage stage,
2475 const struct radv_shader_variant_key *key)
2476 {
2477 if (stage == MESA_SHADER_GEOMETRY && !key->vs_common_out.as_ngg)
2478 return 64;
2479 else if (stage == MESA_SHADER_COMPUTE) {
2480 if (key->cs.subgroup_size) {
2481 /* Return the required subgroup size if specified. */
2482 return key->cs.subgroup_size;
2483 }
2484 return device->physical_device->cs_wave_size;
2485 }
2486 else if (stage == MESA_SHADER_FRAGMENT)
2487 return device->physical_device->ps_wave_size;
2488 else
2489 return device->physical_device->ge_wave_size;
2490 }
2491
2492 static uint8_t
2493 radv_get_ballot_bit_size(struct radv_device *device,
2494 const VkPipelineShaderStageCreateInfo *pStage,
2495 gl_shader_stage stage,
2496 const struct radv_shader_variant_key *key)
2497 {
2498 if (stage == MESA_SHADER_COMPUTE && key->cs.subgroup_size)
2499 return key->cs.subgroup_size;
2500 return 64;
2501 }
2502
2503 static void
2504 radv_fill_shader_info(struct radv_pipeline *pipeline,
2505 const VkPipelineShaderStageCreateInfo **pStages,
2506 struct radv_shader_variant_key *keys,
2507 struct radv_shader_info *infos,
2508 nir_shader **nir)
2509 {
2510 unsigned active_stages = 0;
2511 unsigned filled_stages = 0;
2512
2513 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
2514 if (nir[i])
2515 active_stages |= (1 << i);
2516 }
2517
2518 if (nir[MESA_SHADER_FRAGMENT]) {
2519 radv_nir_shader_info_init(&infos[MESA_SHADER_FRAGMENT]);
2520 radv_nir_shader_info_pass(nir[MESA_SHADER_FRAGMENT],
2521 pipeline->layout,
2522 &keys[MESA_SHADER_FRAGMENT],
2523 &infos[MESA_SHADER_FRAGMENT],
2524 pipeline->device->physical_device->use_aco);
2525
2526 /* TODO: These are no longer used as keys we should refactor this */
2527 keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id =
2528 infos[MESA_SHADER_FRAGMENT].ps.prim_id_input;
2529 keys[MESA_SHADER_VERTEX].vs_common_out.export_layer_id =
2530 infos[MESA_SHADER_FRAGMENT].ps.layer_input;
2531 keys[MESA_SHADER_VERTEX].vs_common_out.export_clip_dists =
2532 !!infos[MESA_SHADER_FRAGMENT].ps.num_input_clips_culls;
2533 keys[MESA_SHADER_VERTEX].vs_common_out.export_viewport_index =
2534 infos[MESA_SHADER_FRAGMENT].ps.viewport_index_input;
2535 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_prim_id =
2536 infos[MESA_SHADER_FRAGMENT].ps.prim_id_input;
2537 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_layer_id =
2538 infos[MESA_SHADER_FRAGMENT].ps.layer_input;
2539 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_clip_dists =
2540 !!infos[MESA_SHADER_FRAGMENT].ps.num_input_clips_culls;
2541 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_viewport_index =
2542 infos[MESA_SHADER_FRAGMENT].ps.viewport_index_input;
2543
2544 /* NGG passthrough mode can't be enabled for vertex shaders
2545 * that export the primitive ID.
2546 *
2547 * TODO: I should really refactor the keys logic.
2548 */
2549 if (nir[MESA_SHADER_VERTEX] &&
2550 keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id) {
2551 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg_passthrough = false;
2552 }
2553
2554 filled_stages |= (1 << MESA_SHADER_FRAGMENT);
2555 }
2556
2557 if (nir[MESA_SHADER_TESS_CTRL]) {
2558 infos[MESA_SHADER_TESS_CTRL].tcs.tes_inputs_read =
2559 nir[MESA_SHADER_TESS_EVAL]->info.inputs_read;
2560 infos[MESA_SHADER_TESS_CTRL].tcs.tes_patch_inputs_read =
2561 nir[MESA_SHADER_TESS_EVAL]->info.patch_inputs_read;
2562 }
2563
2564 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9 &&
2565 nir[MESA_SHADER_TESS_CTRL]) {
2566 struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};
2567 struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];
2568 key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;
2569
2570 radv_nir_shader_info_init(&infos[MESA_SHADER_TESS_CTRL]);
2571
2572 for (int i = 0; i < 2; i++) {
2573 radv_nir_shader_info_pass(combined_nir[i],
2574 pipeline->layout, &key,
2575 &infos[MESA_SHADER_TESS_CTRL],
2576 pipeline->device->physical_device->use_aco);
2577 }
2578
2579 keys[MESA_SHADER_TESS_EVAL].tes.num_patches =
2580 infos[MESA_SHADER_TESS_CTRL].tcs.num_patches;
2581 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs =
2582 util_last_bit64(infos[MESA_SHADER_TESS_CTRL].tcs.outputs_written);
2583
2584 filled_stages |= (1 << MESA_SHADER_VERTEX);
2585 filled_stages |= (1 << MESA_SHADER_TESS_CTRL);
2586 }
2587
2588 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9 &&
2589 nir[MESA_SHADER_GEOMETRY]) {
2590 gl_shader_stage pre_stage = nir[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2591 struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};
2592
2593 radv_nir_shader_info_init(&infos[MESA_SHADER_GEOMETRY]);
2594
2595 for (int i = 0; i < 2; i++) {
2596 radv_nir_shader_info_pass(combined_nir[i],
2597 pipeline->layout,
2598 &keys[pre_stage],
2599 &infos[MESA_SHADER_GEOMETRY],
2600 pipeline->device->physical_device->use_aco);
2601 }
2602
2603 filled_stages |= (1 << pre_stage);
2604 filled_stages |= (1 << MESA_SHADER_GEOMETRY);
2605 }
2606
2607 active_stages ^= filled_stages;
2608 while (active_stages) {
2609 int i = u_bit_scan(&active_stages);
2610
2611 if (i == MESA_SHADER_TESS_CTRL) {
2612 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs =
2613 util_last_bit64(infos[MESA_SHADER_VERTEX].vs.ls_outputs_written);
2614 }
2615
2616 if (i == MESA_SHADER_TESS_EVAL) {
2617 keys[MESA_SHADER_TESS_EVAL].tes.num_patches =
2618 infos[MESA_SHADER_TESS_CTRL].tcs.num_patches;
2619 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs =
2620 util_last_bit64(infos[MESA_SHADER_TESS_CTRL].tcs.outputs_written);
2621 }
2622
2623 radv_nir_shader_info_init(&infos[i]);
2624 radv_nir_shader_info_pass(nir[i], pipeline->layout,
2625 &keys[i], &infos[i], pipeline->device->physical_device->use_aco);
2626 }
2627
2628 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
2629 if (nir[i]) {
2630 infos[i].wave_size =
2631 radv_get_wave_size(pipeline->device, pStages[i],
2632 i, &keys[i]);
2633 infos[i].ballot_bit_size =
2634 radv_get_ballot_bit_size(pipeline->device,
2635 pStages[i], i,
2636 &keys[i]);
2637 }
2638 }
2639 }
2640
2641 static void
2642 merge_tess_info(struct shader_info *tes_info,
2643 const struct shader_info *tcs_info)
2644 {
2645 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
2646 *
2647 * "PointMode. Controls generation of points rather than triangles
2648 * or lines. This functionality defaults to disabled, and is
2649 * enabled if either shader stage includes the execution mode.
2650 *
2651 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
2652 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
2653 * and OutputVertices, it says:
2654 *
2655 * "One mode must be set in at least one of the tessellation
2656 * shader stages."
2657 *
2658 * So, the fields can be set in either the TCS or TES, but they must
2659 * agree if set in both. Our backend looks at TES, so bitwise-or in
2660 * the values from the TCS.
2661 */
2662 assert(tcs_info->tess.tcs_vertices_out == 0 ||
2663 tes_info->tess.tcs_vertices_out == 0 ||
2664 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
2665 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
2666
2667 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
2668 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
2669 tcs_info->tess.spacing == tes_info->tess.spacing);
2670 tes_info->tess.spacing |= tcs_info->tess.spacing;
2671
2672 assert(tcs_info->tess.primitive_mode == 0 ||
2673 tes_info->tess.primitive_mode == 0 ||
2674 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
2675 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
2676 tes_info->tess.ccw |= tcs_info->tess.ccw;
2677 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
2678 }
2679
2680 static
2681 void radv_init_feedback(const VkPipelineCreationFeedbackCreateInfoEXT *ext)
2682 {
2683 if (!ext)
2684 return;
2685
2686 if (ext->pPipelineCreationFeedback) {
2687 ext->pPipelineCreationFeedback->flags = 0;
2688 ext->pPipelineCreationFeedback->duration = 0;
2689 }
2690
2691 for (unsigned i = 0; i < ext->pipelineStageCreationFeedbackCount; ++i) {
2692 ext->pPipelineStageCreationFeedbacks[i].flags = 0;
2693 ext->pPipelineStageCreationFeedbacks[i].duration = 0;
2694 }
2695 }
2696
2697 static
2698 void radv_start_feedback(VkPipelineCreationFeedbackEXT *feedback)
2699 {
2700 if (!feedback)
2701 return;
2702
2703 feedback->duration -= radv_get_current_time();
2704 feedback ->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT;
2705 }
2706
2707 static
2708 void radv_stop_feedback(VkPipelineCreationFeedbackEXT *feedback, bool cache_hit)
2709 {
2710 if (!feedback)
2711 return;
2712
2713 feedback->duration += radv_get_current_time();
2714 feedback ->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT |
2715 (cache_hit ? VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT : 0);
2716 }
2717
2718 VkResult radv_create_shaders(struct radv_pipeline *pipeline,
2719 struct radv_device *device,
2720 struct radv_pipeline_cache *cache,
2721 const struct radv_pipeline_key *key,
2722 const VkPipelineShaderStageCreateInfo **pStages,
2723 const VkPipelineCreateFlags flags,
2724 VkPipelineCreationFeedbackEXT *pipeline_feedback,
2725 VkPipelineCreationFeedbackEXT **stage_feedbacks)
2726 {
2727 struct radv_shader_module fs_m = {0};
2728 struct radv_shader_module *modules[MESA_SHADER_STAGES] = { 0, };
2729 nir_shader *nir[MESA_SHADER_STAGES] = {0};
2730 struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};
2731 struct radv_shader_variant_key keys[MESA_SHADER_STAGES] = {{{{{0}}}}};
2732 struct radv_shader_info infos[MESA_SHADER_STAGES] = {0};
2733 unsigned char hash[20], gs_copy_hash[20];
2734 bool keep_executable_info = (flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR) || device->keep_shader_info;
2735 bool keep_statistic_info = (flags & VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR) ||
2736 (device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS) ||
2737 device->keep_shader_info;
2738
2739 radv_start_feedback(pipeline_feedback);
2740
2741 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
2742 if (pStages[i]) {
2743 modules[i] = radv_shader_module_from_handle(pStages[i]->module);
2744 if (modules[i]->nir)
2745 _mesa_sha1_compute(modules[i]->nir->info.name,
2746 strlen(modules[i]->nir->info.name),
2747 modules[i]->sha1);
2748
2749 pipeline->active_stages |= mesa_to_vk_shader_stage(i);
2750 }
2751 }
2752
2753 radv_hash_shaders(hash, pStages, pipeline->layout, key, get_hash_flags(device));
2754 memcpy(gs_copy_hash, hash, 20);
2755 gs_copy_hash[0] ^= 1;
2756
2757 bool found_in_application_cache = true;
2758 if (modules[MESA_SHADER_GEOMETRY] && !keep_executable_info && !keep_statistic_info) {
2759 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2760 radv_create_shader_variants_from_pipeline_cache(device, cache, gs_copy_hash, variants,
2761 &found_in_application_cache);
2762 pipeline->gs_copy_shader = variants[MESA_SHADER_GEOMETRY];
2763 }
2764
2765 if (!keep_executable_info && !keep_statistic_info &&
2766 radv_create_shader_variants_from_pipeline_cache(device, cache, hash, pipeline->shaders,
2767 &found_in_application_cache) &&
2768 (!modules[MESA_SHADER_GEOMETRY] || pipeline->gs_copy_shader)) {
2769 radv_stop_feedback(pipeline_feedback, found_in_application_cache);
2770 return VK_SUCCESS;
2771 }
2772
2773 if (flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT) {
2774 radv_stop_feedback(pipeline_feedback, found_in_application_cache);
2775 return VK_PIPELINE_COMPILE_REQUIRED_EXT;
2776 }
2777
2778 if (!modules[MESA_SHADER_FRAGMENT] && !modules[MESA_SHADER_COMPUTE]) {
2779 nir_builder fs_b;
2780 nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
2781 fs_b.shader->info.name = ralloc_strdup(fs_b.shader, "noop_fs");
2782 fs_m.nir = fs_b.shader;
2783 modules[MESA_SHADER_FRAGMENT] = &fs_m;
2784 }
2785
2786 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
2787 const VkPipelineShaderStageCreateInfo *stage = pStages[i];
2788 unsigned subgroup_size = 64, ballot_bit_size = 64;
2789
2790 if (!modules[i])
2791 continue;
2792
2793 radv_start_feedback(stage_feedbacks[i]);
2794
2795 if (key->compute_subgroup_size) {
2796 /* Only compute shaders currently support requiring a
2797 * specific subgroup size.
2798 */
2799 assert(i == MESA_SHADER_COMPUTE);
2800 subgroup_size = key->compute_subgroup_size;
2801 ballot_bit_size = key->compute_subgroup_size;
2802 }
2803
2804 nir[i] = radv_shader_compile_to_nir(device, modules[i],
2805 stage ? stage->pName : "main", i,
2806 stage ? stage->pSpecializationInfo : NULL,
2807 flags, pipeline->layout,
2808 subgroup_size, ballot_bit_size);
2809
2810 /* We don't want to alter meta shaders IR directly so clone it
2811 * first.
2812 */
2813 if (nir[i]->info.name) {
2814 nir[i] = nir_shader_clone(NULL, nir[i]);
2815 }
2816
2817 radv_stop_feedback(stage_feedbacks[i], false);
2818 }
2819
2820 if (nir[MESA_SHADER_TESS_CTRL]) {
2821 nir_lower_patch_vertices(nir[MESA_SHADER_TESS_EVAL], nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out, NULL);
2822 merge_tess_info(&nir[MESA_SHADER_TESS_EVAL]->info, &nir[MESA_SHADER_TESS_CTRL]->info);
2823 }
2824
2825 if (!(flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT))
2826 radv_link_shaders(pipeline, nir);
2827
2828 radv_set_linked_driver_locations(pipeline, nir, infos);
2829
2830 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2831 if (nir[i]) {
2832 /* do this again since information such as outputs_read can be out-of-date */
2833 nir_shader_gather_info(nir[i], nir_shader_get_entrypoint(nir[i]));
2834
2835 if (device->physical_device->use_aco) {
2836 NIR_PASS_V(nir[i], nir_lower_non_uniform_access,
2837 nir_lower_non_uniform_ubo_access |
2838 nir_lower_non_uniform_ssbo_access |
2839 nir_lower_non_uniform_texture_access |
2840 nir_lower_non_uniform_image_access);
2841 } else
2842 NIR_PASS_V(nir[i], nir_lower_bool_to_int32);
2843 }
2844 }
2845
2846 if (nir[MESA_SHADER_FRAGMENT])
2847 radv_lower_fs_io(nir[MESA_SHADER_FRAGMENT]);
2848
2849 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2850 if (radv_can_dump_shader(device, modules[i], false))
2851 nir_print_shader(nir[i], stderr);
2852 }
2853
2854 radv_fill_shader_keys(device, keys, key, nir);
2855
2856 radv_fill_shader_info(pipeline, pStages, keys, infos, nir);
2857
2858 if ((nir[MESA_SHADER_VERTEX] &&
2859 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg) ||
2860 (nir[MESA_SHADER_TESS_EVAL] &&
2861 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg)) {
2862 struct gfx10_ngg_info *ngg_info;
2863
2864 if (nir[MESA_SHADER_GEOMETRY])
2865 ngg_info = &infos[MESA_SHADER_GEOMETRY].ngg_info;
2866 else if (nir[MESA_SHADER_TESS_CTRL])
2867 ngg_info = &infos[MESA_SHADER_TESS_EVAL].ngg_info;
2868 else
2869 ngg_info = &infos[MESA_SHADER_VERTEX].ngg_info;
2870
2871 gfx10_get_ngg_info(key, pipeline, nir, infos, ngg_info);
2872 } else if (nir[MESA_SHADER_GEOMETRY]) {
2873 struct gfx9_gs_info *gs_info =
2874 &infos[MESA_SHADER_GEOMETRY].gs_ring_info;
2875
2876 gfx9_get_gs_info(key, pipeline, nir, infos, gs_info);
2877 }
2878
2879 if(modules[MESA_SHADER_GEOMETRY]) {
2880 struct radv_shader_binary *gs_copy_binary = NULL;
2881 if (!pipeline->gs_copy_shader &&
2882 !radv_pipeline_has_ngg(pipeline)) {
2883 struct radv_shader_info info = {};
2884 struct radv_shader_variant_key key = {};
2885
2886 key.has_multiview_view_index =
2887 keys[MESA_SHADER_GEOMETRY].has_multiview_view_index;
2888
2889 radv_nir_shader_info_pass(nir[MESA_SHADER_GEOMETRY],
2890 pipeline->layout, &key,
2891 &info, pipeline->device->physical_device->use_aco);
2892 info.wave_size = 64; /* Wave32 not supported. */
2893 info.ballot_bit_size = 64;
2894
2895 pipeline->gs_copy_shader = radv_create_gs_copy_shader(
2896 device, nir[MESA_SHADER_GEOMETRY], &info,
2897 &gs_copy_binary, keep_executable_info, keep_statistic_info,
2898 keys[MESA_SHADER_GEOMETRY].has_multiview_view_index);
2899 }
2900
2901 if (!keep_executable_info && !keep_statistic_info && pipeline->gs_copy_shader) {
2902 struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};
2903 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2904
2905 binaries[MESA_SHADER_GEOMETRY] = gs_copy_binary;
2906 variants[MESA_SHADER_GEOMETRY] = pipeline->gs_copy_shader;
2907
2908 radv_pipeline_cache_insert_shaders(device, cache,
2909 gs_copy_hash,
2910 variants,
2911 binaries);
2912 }
2913 free(gs_copy_binary);
2914 }
2915
2916 if (nir[MESA_SHADER_FRAGMENT]) {
2917 if (!pipeline->shaders[MESA_SHADER_FRAGMENT]) {
2918 radv_start_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT]);
2919
2920 pipeline->shaders[MESA_SHADER_FRAGMENT] =
2921 radv_shader_variant_compile(device, modules[MESA_SHADER_FRAGMENT], &nir[MESA_SHADER_FRAGMENT], 1,
2922 pipeline->layout, keys + MESA_SHADER_FRAGMENT,
2923 infos + MESA_SHADER_FRAGMENT,
2924 keep_executable_info, keep_statistic_info,
2925 &binaries[MESA_SHADER_FRAGMENT]);
2926
2927 radv_stop_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT], false);
2928 }
2929 }
2930
2931 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_TESS_CTRL]) {
2932 if (!pipeline->shaders[MESA_SHADER_TESS_CTRL]) {
2933 struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};
2934 struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];
2935 key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;
2936
2937 radv_start_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL]);
2938
2939 pipeline->shaders[MESA_SHADER_TESS_CTRL] = radv_shader_variant_compile(device, modules[MESA_SHADER_TESS_CTRL], combined_nir, 2,
2940 pipeline->layout,
2941 &key, &infos[MESA_SHADER_TESS_CTRL], keep_executable_info,
2942 keep_statistic_info, &binaries[MESA_SHADER_TESS_CTRL]);
2943
2944 radv_stop_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL], false);
2945 }
2946 modules[MESA_SHADER_VERTEX] = NULL;
2947 keys[MESA_SHADER_TESS_EVAL].tes.num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2948 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.outputs_written);
2949 }
2950
2951 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_GEOMETRY]) {
2952 gl_shader_stage pre_stage = modules[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2953 if (!pipeline->shaders[MESA_SHADER_GEOMETRY]) {
2954 struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};
2955
2956 radv_start_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY]);
2957
2958 pipeline->shaders[MESA_SHADER_GEOMETRY] = radv_shader_variant_compile(device, modules[MESA_SHADER_GEOMETRY], combined_nir, 2,
2959 pipeline->layout,
2960 &keys[pre_stage], &infos[MESA_SHADER_GEOMETRY], keep_executable_info,
2961 keep_statistic_info, &binaries[MESA_SHADER_GEOMETRY]);
2962
2963 radv_stop_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY], false);
2964 }
2965 modules[pre_stage] = NULL;
2966 }
2967
2968 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2969 if(modules[i] && !pipeline->shaders[i]) {
2970 if (i == MESA_SHADER_TESS_CTRL) {
2971 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs = util_last_bit64(pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.ls_outputs_written);
2972 }
2973 if (i == MESA_SHADER_TESS_EVAL) {
2974 keys[MESA_SHADER_TESS_EVAL].tes.num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2975 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.outputs_written);
2976 }
2977
2978 radv_start_feedback(stage_feedbacks[i]);
2979
2980 pipeline->shaders[i] = radv_shader_variant_compile(device, modules[i], &nir[i], 1,
2981 pipeline->layout,
2982 keys + i, infos + i, keep_executable_info,
2983 keep_statistic_info, &binaries[i]);
2984
2985 radv_stop_feedback(stage_feedbacks[i], false);
2986 }
2987 }
2988
2989 if (!keep_executable_info && !keep_statistic_info) {
2990 radv_pipeline_cache_insert_shaders(device, cache, hash, pipeline->shaders,
2991 binaries);
2992 }
2993
2994 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2995 free(binaries[i]);
2996 if (nir[i]) {
2997 ralloc_free(nir[i]);
2998
2999 if (radv_can_dump_shader_stats(device, modules[i]))
3000 radv_shader_dump_stats(device,
3001 pipeline->shaders[i],
3002 i, stderr);
3003 }
3004 }
3005
3006 if (fs_m.nir)
3007 ralloc_free(fs_m.nir);
3008
3009 radv_stop_feedback(pipeline_feedback, false);
3010 return VK_SUCCESS;
3011 }
3012
3013 static uint32_t
3014 radv_pipeline_stage_to_user_data_0(struct radv_pipeline *pipeline,
3015 gl_shader_stage stage, enum chip_class chip_class)
3016 {
3017 bool has_gs = radv_pipeline_has_gs(pipeline);
3018 bool has_tess = radv_pipeline_has_tess(pipeline);
3019 bool has_ngg = radv_pipeline_has_ngg(pipeline);
3020
3021 switch (stage) {
3022 case MESA_SHADER_FRAGMENT:
3023 return R_00B030_SPI_SHADER_USER_DATA_PS_0;
3024 case MESA_SHADER_VERTEX:
3025 if (has_tess) {
3026 if (chip_class >= GFX10) {
3027 return R_00B430_SPI_SHADER_USER_DATA_HS_0;
3028 } else if (chip_class == GFX9) {
3029 return R_00B430_SPI_SHADER_USER_DATA_LS_0;
3030 } else {
3031 return R_00B530_SPI_SHADER_USER_DATA_LS_0;
3032 }
3033
3034 }
3035
3036 if (has_gs) {
3037 if (chip_class >= GFX10) {
3038 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3039 } else {
3040 return R_00B330_SPI_SHADER_USER_DATA_ES_0;
3041 }
3042 }
3043
3044 if (has_ngg)
3045 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3046
3047 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
3048 case MESA_SHADER_GEOMETRY:
3049 return chip_class == GFX9 ? R_00B330_SPI_SHADER_USER_DATA_ES_0 :
3050 R_00B230_SPI_SHADER_USER_DATA_GS_0;
3051 case MESA_SHADER_COMPUTE:
3052 return R_00B900_COMPUTE_USER_DATA_0;
3053 case MESA_SHADER_TESS_CTRL:
3054 return chip_class == GFX9 ? R_00B430_SPI_SHADER_USER_DATA_LS_0 :
3055 R_00B430_SPI_SHADER_USER_DATA_HS_0;
3056 case MESA_SHADER_TESS_EVAL:
3057 if (has_gs) {
3058 return chip_class >= GFX10 ? R_00B230_SPI_SHADER_USER_DATA_GS_0 :
3059 R_00B330_SPI_SHADER_USER_DATA_ES_0;
3060 } else if (has_ngg) {
3061 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3062 } else {
3063 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
3064 }
3065 default:
3066 unreachable("unknown shader");
3067 }
3068 }
3069
3070 struct radv_bin_size_entry {
3071 unsigned bpp;
3072 VkExtent2D extent;
3073 };
3074
3075 static VkExtent2D
3076 radv_gfx9_compute_bin_size(struct radv_pipeline *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo)
3077 {
3078 static const struct radv_bin_size_entry color_size_table[][3][9] = {
3079 {
3080 /* One RB / SE */
3081 {
3082 /* One shader engine */
3083 { 0, {128, 128}},
3084 { 1, { 64, 128}},
3085 { 2, { 32, 128}},
3086 { 3, { 16, 128}},
3087 { 17, { 0, 0}},
3088 { UINT_MAX, { 0, 0}},
3089 },
3090 {
3091 /* Two shader engines */
3092 { 0, {128, 128}},
3093 { 2, { 64, 128}},
3094 { 3, { 32, 128}},
3095 { 5, { 16, 128}},
3096 { 17, { 0, 0}},
3097 { UINT_MAX, { 0, 0}},
3098 },
3099 {
3100 /* Four shader engines */
3101 { 0, {128, 128}},
3102 { 3, { 64, 128}},
3103 { 5, { 16, 128}},
3104 { 17, { 0, 0}},
3105 { UINT_MAX, { 0, 0}},
3106 },
3107 },
3108 {
3109 /* Two RB / SE */
3110 {
3111 /* One shader engine */
3112 { 0, {128, 128}},
3113 { 2, { 64, 128}},
3114 { 3, { 32, 128}},
3115 { 5, { 16, 128}},
3116 { 33, { 0, 0}},
3117 { UINT_MAX, { 0, 0}},
3118 },
3119 {
3120 /* Two shader engines */
3121 { 0, {128, 128}},
3122 { 3, { 64, 128}},
3123 { 5, { 32, 128}},
3124 { 9, { 16, 128}},
3125 { 33, { 0, 0}},
3126 { UINT_MAX, { 0, 0}},
3127 },
3128 {
3129 /* Four shader engines */
3130 { 0, {256, 256}},
3131 { 2, {128, 256}},
3132 { 3, {128, 128}},
3133 { 5, { 64, 128}},
3134 { 9, { 16, 128}},
3135 { 33, { 0, 0}},
3136 { UINT_MAX, { 0, 0}},
3137 },
3138 },
3139 {
3140 /* Four RB / SE */
3141 {
3142 /* One shader engine */
3143 { 0, {128, 256}},
3144 { 2, {128, 128}},
3145 { 3, { 64, 128}},
3146 { 5, { 32, 128}},
3147 { 9, { 16, 128}},
3148 { 33, { 0, 0}},
3149 { UINT_MAX, { 0, 0}},
3150 },
3151 {
3152 /* Two shader engines */
3153 { 0, {256, 256}},
3154 { 2, {128, 256}},
3155 { 3, {128, 128}},
3156 { 5, { 64, 128}},
3157 { 9, { 32, 128}},
3158 { 17, { 16, 128}},
3159 { 33, { 0, 0}},
3160 { UINT_MAX, { 0, 0}},
3161 },
3162 {
3163 /* Four shader engines */
3164 { 0, {256, 512}},
3165 { 2, {256, 256}},
3166 { 3, {128, 256}},
3167 { 5, {128, 128}},
3168 { 9, { 64, 128}},
3169 { 17, { 16, 128}},
3170 { 33, { 0, 0}},
3171 { UINT_MAX, { 0, 0}},
3172 },
3173 },
3174 };
3175 static const struct radv_bin_size_entry ds_size_table[][3][9] = {
3176 {
3177 // One RB / SE
3178 {
3179 // One shader engine
3180 { 0, {128, 256}},
3181 { 2, {128, 128}},
3182 { 4, { 64, 128}},
3183 { 7, { 32, 128}},
3184 { 13, { 16, 128}},
3185 { 49, { 0, 0}},
3186 { UINT_MAX, { 0, 0}},
3187 },
3188 {
3189 // Two shader engines
3190 { 0, {256, 256}},
3191 { 2, {128, 256}},
3192 { 4, {128, 128}},
3193 { 7, { 64, 128}},
3194 { 13, { 32, 128}},
3195 { 25, { 16, 128}},
3196 { 49, { 0, 0}},
3197 { UINT_MAX, { 0, 0}},
3198 },
3199 {
3200 // Four shader engines
3201 { 0, {256, 512}},
3202 { 2, {256, 256}},
3203 { 4, {128, 256}},
3204 { 7, {128, 128}},
3205 { 13, { 64, 128}},
3206 { 25, { 16, 128}},
3207 { 49, { 0, 0}},
3208 { UINT_MAX, { 0, 0}},
3209 },
3210 },
3211 {
3212 // Two RB / SE
3213 {
3214 // One shader engine
3215 { 0, {256, 256}},
3216 { 2, {128, 256}},
3217 { 4, {128, 128}},
3218 { 7, { 64, 128}},
3219 { 13, { 32, 128}},
3220 { 25, { 16, 128}},
3221 { 97, { 0, 0}},
3222 { UINT_MAX, { 0, 0}},
3223 },
3224 {
3225 // Two shader engines
3226 { 0, {256, 512}},
3227 { 2, {256, 256}},
3228 { 4, {128, 256}},
3229 { 7, {128, 128}},
3230 { 13, { 64, 128}},
3231 { 25, { 32, 128}},
3232 { 49, { 16, 128}},
3233 { 97, { 0, 0}},
3234 { UINT_MAX, { 0, 0}},
3235 },
3236 {
3237 // Four shader engines
3238 { 0, {512, 512}},
3239 { 2, {256, 512}},
3240 { 4, {256, 256}},
3241 { 7, {128, 256}},
3242 { 13, {128, 128}},
3243 { 25, { 64, 128}},
3244 { 49, { 16, 128}},
3245 { 97, { 0, 0}},
3246 { UINT_MAX, { 0, 0}},
3247 },
3248 },
3249 {
3250 // Four RB / SE
3251 {
3252 // One shader engine
3253 { 0, {256, 512}},
3254 { 2, {256, 256}},
3255 { 4, {128, 256}},
3256 { 7, {128, 128}},
3257 { 13, { 64, 128}},
3258 { 25, { 32, 128}},
3259 { 49, { 16, 128}},
3260 { UINT_MAX, { 0, 0}},
3261 },
3262 {
3263 // Two shader engines
3264 { 0, {512, 512}},
3265 { 2, {256, 512}},
3266 { 4, {256, 256}},
3267 { 7, {128, 256}},
3268 { 13, {128, 128}},
3269 { 25, { 64, 128}},
3270 { 49, { 32, 128}},
3271 { 97, { 16, 128}},
3272 { UINT_MAX, { 0, 0}},
3273 },
3274 {
3275 // Four shader engines
3276 { 0, {512, 512}},
3277 { 4, {256, 512}},
3278 { 7, {256, 256}},
3279 { 13, {128, 256}},
3280 { 25, {128, 128}},
3281 { 49, { 64, 128}},
3282 { 97, { 16, 128}},
3283 { UINT_MAX, { 0, 0}},
3284 },
3285 },
3286 };
3287
3288 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
3289 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
3290 VkExtent2D extent = {512, 512};
3291
3292 unsigned log_num_rb_per_se =
3293 util_logbase2_ceil(pipeline->device->physical_device->rad_info.num_render_backends /
3294 pipeline->device->physical_device->rad_info.max_se);
3295 unsigned log_num_se = util_logbase2_ceil(pipeline->device->physical_device->rad_info.max_se);
3296
3297 unsigned total_samples = 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline->graphics.ms.pa_sc_aa_config);
3298 unsigned ps_iter_samples = 1u << G_028804_PS_ITER_SAMPLES(pipeline->graphics.ms.db_eqaa);
3299 unsigned effective_samples = total_samples;
3300 unsigned color_bytes_per_pixel = 0;
3301
3302 const VkPipelineColorBlendStateCreateInfo *vkblend =
3303 radv_pipeline_get_color_blend_state(pCreateInfo);
3304 if (vkblend) {
3305 for (unsigned i = 0; i < subpass->color_count; i++) {
3306 if (!vkblend->pAttachments[i].colorWriteMask)
3307 continue;
3308
3309 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
3310 continue;
3311
3312 VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;
3313 color_bytes_per_pixel += vk_format_get_blocksize(format);
3314 }
3315
3316 /* MSAA images typically don't use all samples all the time. */
3317 if (effective_samples >= 2 && ps_iter_samples <= 1)
3318 effective_samples = 2;
3319 color_bytes_per_pixel *= effective_samples;
3320 }
3321
3322 const struct radv_bin_size_entry *color_entry = color_size_table[log_num_rb_per_se][log_num_se];
3323 while(color_entry[1].bpp <= color_bytes_per_pixel)
3324 ++color_entry;
3325
3326 extent = color_entry->extent;
3327
3328 if (subpass->depth_stencil_attachment) {
3329 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;
3330
3331 /* Coefficients taken from AMDVLK */
3332 unsigned depth_coeff = vk_format_is_depth(attachment->format) ? 5 : 0;
3333 unsigned stencil_coeff = vk_format_is_stencil(attachment->format) ? 1 : 0;
3334 unsigned ds_bytes_per_pixel = 4 * (depth_coeff + stencil_coeff) * total_samples;
3335
3336 const struct radv_bin_size_entry *ds_entry = ds_size_table[log_num_rb_per_se][log_num_se];
3337 while(ds_entry[1].bpp <= ds_bytes_per_pixel)
3338 ++ds_entry;
3339
3340 if (ds_entry->extent.width * ds_entry->extent.height < extent.width * extent.height)
3341 extent = ds_entry->extent;
3342 }
3343
3344 return extent;
3345 }
3346
3347 static VkExtent2D
3348 radv_gfx10_compute_bin_size(struct radv_pipeline *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo)
3349 {
3350 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
3351 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
3352 VkExtent2D extent = {512, 512};
3353
3354 const unsigned db_tag_size = 64;
3355 const unsigned db_tag_count = 312;
3356 const unsigned color_tag_size = 1024;
3357 const unsigned color_tag_count = 31;
3358 const unsigned fmask_tag_size = 256;
3359 const unsigned fmask_tag_count = 44;
3360
3361 const unsigned rb_count = pipeline->device->physical_device->rad_info.num_render_backends;
3362 const unsigned pipe_count = MAX2(rb_count, pipeline->device->physical_device->rad_info.num_sdp_interfaces);
3363
3364 const unsigned db_tag_part = (db_tag_count * rb_count / pipe_count) * db_tag_size * pipe_count;
3365 const unsigned color_tag_part = (color_tag_count * rb_count / pipe_count) * color_tag_size * pipe_count;
3366 const unsigned fmask_tag_part = (fmask_tag_count * rb_count / pipe_count) * fmask_tag_size * pipe_count;
3367
3368 const unsigned total_samples = 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline->graphics.ms.pa_sc_aa_config);
3369 const unsigned samples_log = util_logbase2_ceil(total_samples);
3370
3371 unsigned color_bytes_per_pixel = 0;
3372 unsigned fmask_bytes_per_pixel = 0;
3373
3374 const VkPipelineColorBlendStateCreateInfo *vkblend =
3375 radv_pipeline_get_color_blend_state(pCreateInfo);
3376 if (vkblend) {
3377 for (unsigned i = 0; i < subpass->color_count; i++) {
3378 if (!vkblend->pAttachments[i].colorWriteMask)
3379 continue;
3380
3381 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
3382 continue;
3383
3384 VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;
3385 color_bytes_per_pixel += vk_format_get_blocksize(format);
3386
3387 if (total_samples > 1) {
3388 assert(samples_log <= 3);
3389 const unsigned fmask_array[] = {0, 1, 1, 4};
3390 fmask_bytes_per_pixel += fmask_array[samples_log];
3391 }
3392 }
3393
3394 color_bytes_per_pixel *= total_samples;
3395 }
3396 color_bytes_per_pixel = MAX2(color_bytes_per_pixel, 1);
3397
3398 const unsigned color_pixel_count_log = util_logbase2(color_tag_part / color_bytes_per_pixel);
3399 extent.width = 1ull << ((color_pixel_count_log + 1) / 2);
3400 extent.height = 1ull << (color_pixel_count_log / 2);
3401
3402 if (fmask_bytes_per_pixel) {
3403 const unsigned fmask_pixel_count_log = util_logbase2(fmask_tag_part / fmask_bytes_per_pixel);
3404
3405 const VkExtent2D fmask_extent = (VkExtent2D){
3406 .width = 1ull << ((fmask_pixel_count_log + 1) / 2),
3407 .height = 1ull << (color_pixel_count_log / 2)
3408 };
3409
3410 if (fmask_extent.width * fmask_extent.height < extent.width * extent.height)
3411 extent = fmask_extent;
3412 }
3413
3414 if (subpass->depth_stencil_attachment) {
3415 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;
3416
3417 /* Coefficients taken from AMDVLK */
3418 unsigned depth_coeff = vk_format_is_depth(attachment->format) ? 5 : 0;
3419 unsigned stencil_coeff = vk_format_is_stencil(attachment->format) ? 1 : 0;
3420 unsigned db_bytes_per_pixel = (depth_coeff + stencil_coeff) * total_samples;
3421
3422 const unsigned db_pixel_count_log = util_logbase2(db_tag_part / db_bytes_per_pixel);
3423
3424 const VkExtent2D db_extent = (VkExtent2D){
3425 .width = 1ull << ((db_pixel_count_log + 1) / 2),
3426 .height = 1ull << (color_pixel_count_log / 2)
3427 };
3428
3429 if (db_extent.width * db_extent.height < extent.width * extent.height)
3430 extent = db_extent;
3431 }
3432
3433 extent.width = MAX2(extent.width, 128);
3434 extent.height = MAX2(extent.width, 64);
3435
3436 return extent;
3437 }
3438
3439 static void
3440 radv_pipeline_generate_disabled_binning_state(struct radeon_cmdbuf *ctx_cs,
3441 struct radv_pipeline *pipeline,
3442 const VkGraphicsPipelineCreateInfo *pCreateInfo)
3443 {
3444 uint32_t pa_sc_binner_cntl_0 =
3445 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC) |
3446 S_028C44_DISABLE_START_OF_PRIM(1);
3447 uint32_t db_dfsm_control = S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF);
3448
3449 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3450 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
3451 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
3452 const VkPipelineColorBlendStateCreateInfo *vkblend =
3453 radv_pipeline_get_color_blend_state(pCreateInfo);
3454 unsigned min_bytes_per_pixel = 0;
3455
3456 if (vkblend) {
3457 for (unsigned i = 0; i < subpass->color_count; i++) {
3458 if (!vkblend->pAttachments[i].colorWriteMask)
3459 continue;
3460
3461 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
3462 continue;
3463
3464 VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;
3465 unsigned bytes = vk_format_get_blocksize(format);
3466 if (!min_bytes_per_pixel || bytes < min_bytes_per_pixel)
3467 min_bytes_per_pixel = bytes;
3468 }
3469 }
3470
3471 pa_sc_binner_cntl_0 =
3472 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_NEW_SC) |
3473 S_028C44_BIN_SIZE_X(0) |
3474 S_028C44_BIN_SIZE_Y(0) |
3475 S_028C44_BIN_SIZE_X_EXTEND(2) | /* 128 */
3476 S_028C44_BIN_SIZE_Y_EXTEND(min_bytes_per_pixel <= 4 ? 2 : 1) | /* 128 or 64 */
3477 S_028C44_DISABLE_START_OF_PRIM(1);
3478 }
3479
3480 pipeline->graphics.binning.pa_sc_binner_cntl_0 = pa_sc_binner_cntl_0;
3481 pipeline->graphics.binning.db_dfsm_control = db_dfsm_control;
3482 }
3483
3484 struct radv_binning_settings
3485 radv_get_binning_settings(const struct radv_physical_device *pdev)
3486 {
3487 struct radv_binning_settings settings;
3488 if (pdev->rad_info.has_dedicated_vram) {
3489 if (pdev->rad_info.num_render_backends > 4) {
3490 settings.context_states_per_bin = 1;
3491 settings.persistent_states_per_bin = 1;
3492 } else {
3493 settings.context_states_per_bin = 3;
3494 settings.persistent_states_per_bin = 8;
3495 }
3496 settings.fpovs_per_batch = 63;
3497 } else {
3498 /* The context states are affected by the scissor bug. */
3499 settings.context_states_per_bin = 6;
3500 /* 32 causes hangs for RAVEN. */
3501 settings.persistent_states_per_bin = 16;
3502 settings.fpovs_per_batch = 63;
3503 }
3504
3505 if (pdev->rad_info.has_gfx9_scissor_bug)
3506 settings.context_states_per_bin = 1;
3507
3508 return settings;
3509 }
3510
3511 static void
3512 radv_pipeline_generate_binning_state(struct radeon_cmdbuf *ctx_cs,
3513 struct radv_pipeline *pipeline,
3514 const VkGraphicsPipelineCreateInfo *pCreateInfo,
3515 const struct radv_blend_state *blend)
3516 {
3517 if (pipeline->device->physical_device->rad_info.chip_class < GFX9)
3518 return;
3519
3520 VkExtent2D bin_size;
3521 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3522 bin_size = radv_gfx10_compute_bin_size(pipeline, pCreateInfo);
3523 } else if (pipeline->device->physical_device->rad_info.chip_class == GFX9) {
3524 bin_size = radv_gfx9_compute_bin_size(pipeline, pCreateInfo);
3525 } else
3526 unreachable("Unhandled generation for binning bin size calculation");
3527
3528 if (pipeline->device->pbb_allowed && bin_size.width && bin_size.height) {
3529 struct radv_binning_settings settings =
3530 radv_get_binning_settings(pipeline->device->physical_device);
3531
3532 bool disable_start_of_prim = true;
3533 uint32_t db_dfsm_control = S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF);
3534
3535 const struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
3536
3537 if (pipeline->device->dfsm_allowed && ps &&
3538 !ps->info.ps.can_discard &&
3539 !ps->info.ps.writes_memory &&
3540 blend->cb_target_enabled_4bit) {
3541 db_dfsm_control = S_028060_PUNCHOUT_MODE(V_028060_AUTO);
3542 disable_start_of_prim = (blend->blend_enable_4bit & blend->cb_target_enabled_4bit) != 0;
3543 }
3544
3545 const uint32_t pa_sc_binner_cntl_0 =
3546 S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED) |
3547 S_028C44_BIN_SIZE_X(bin_size.width == 16) |
3548 S_028C44_BIN_SIZE_Y(bin_size.height == 16) |
3549 S_028C44_BIN_SIZE_X_EXTEND(util_logbase2(MAX2(bin_size.width, 32)) - 5) |
3550 S_028C44_BIN_SIZE_Y_EXTEND(util_logbase2(MAX2(bin_size.height, 32)) - 5) |
3551 S_028C44_CONTEXT_STATES_PER_BIN(settings.context_states_per_bin - 1) |
3552 S_028C44_PERSISTENT_STATES_PER_BIN(settings.persistent_states_per_bin - 1) |
3553 S_028C44_DISABLE_START_OF_PRIM(disable_start_of_prim) |
3554 S_028C44_FPOVS_PER_BATCH(settings.fpovs_per_batch) |
3555 S_028C44_OPTIMAL_BIN_SELECTION(1);
3556
3557 pipeline->graphics.binning.pa_sc_binner_cntl_0 = pa_sc_binner_cntl_0;
3558 pipeline->graphics.binning.db_dfsm_control = db_dfsm_control;
3559 } else
3560 radv_pipeline_generate_disabled_binning_state(ctx_cs, pipeline, pCreateInfo);
3561 }
3562
3563
3564 static void
3565 radv_pipeline_generate_depth_stencil_state(struct radeon_cmdbuf *ctx_cs,
3566 struct radv_pipeline *pipeline,
3567 const VkGraphicsPipelineCreateInfo *pCreateInfo,
3568 const struct radv_graphics_pipeline_create_info *extra)
3569 {
3570 const VkPipelineDepthStencilStateCreateInfo *vkds = radv_pipeline_get_depth_stencil_state(pCreateInfo);
3571 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
3572 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
3573 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
3574 struct radv_render_pass_attachment *attachment = NULL;
3575 uint32_t db_depth_control = 0, db_stencil_control = 0;
3576 uint32_t db_render_control = 0, db_render_override2 = 0;
3577 uint32_t db_render_override = 0;
3578
3579 if (subpass->depth_stencil_attachment)
3580 attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;
3581
3582 bool has_depth_attachment = attachment && vk_format_is_depth(attachment->format);
3583 bool has_stencil_attachment = attachment && vk_format_is_stencil(attachment->format);
3584
3585 if (vkds && has_depth_attachment) {
3586 db_depth_control = S_028800_Z_ENABLE(vkds->depthTestEnable ? 1 : 0) |
3587 S_028800_Z_WRITE_ENABLE(vkds->depthWriteEnable ? 1 : 0) |
3588 S_028800_ZFUNC(vkds->depthCompareOp) |
3589 S_028800_DEPTH_BOUNDS_ENABLE(vkds->depthBoundsTestEnable ? 1 : 0);
3590
3591 /* from amdvlk: For 4xAA and 8xAA need to decompress on flush for better performance */
3592 db_render_override2 |= S_028010_DECOMPRESS_Z_ON_FLUSH(attachment->samples > 2);
3593
3594 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10_3)
3595 db_render_override2 |= S_028010_CENTROID_COMPUTATION_MODE_GFX103(2);
3596 }
3597
3598 if (has_stencil_attachment && vkds && vkds->stencilTestEnable) {
3599 db_depth_control |= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);
3600 db_depth_control |= S_028800_STENCILFUNC(vkds->front.compareOp);
3601 db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(vkds->front.failOp));
3602 db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(vkds->front.passOp));
3603 db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(vkds->front.depthFailOp));
3604
3605 db_depth_control |= S_028800_STENCILFUNC_BF(vkds->back.compareOp);
3606 db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(vkds->back.failOp));
3607 db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(vkds->back.passOp));
3608 db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(vkds->back.depthFailOp));
3609 }
3610
3611 if (attachment && extra) {
3612 db_render_control |= S_028000_DEPTH_CLEAR_ENABLE(extra->db_depth_clear);
3613 db_render_control |= S_028000_STENCIL_CLEAR_ENABLE(extra->db_stencil_clear);
3614
3615 db_render_control |= S_028000_RESUMMARIZE_ENABLE(extra->resummarize_enable);
3616 db_render_control |= S_028000_DEPTH_COMPRESS_DISABLE(extra->depth_compress_disable);
3617 db_render_control |= S_028000_STENCIL_COMPRESS_DISABLE(extra->stencil_compress_disable);
3618 db_render_override2 |= S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra->db_depth_disable_expclear);
3619 db_render_override2 |= S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra->db_stencil_disable_expclear);
3620 }
3621
3622 db_render_override |= S_02800C_FORCE_HIS_ENABLE0(V_02800C_FORCE_DISABLE) |
3623 S_02800C_FORCE_HIS_ENABLE1(V_02800C_FORCE_DISABLE);
3624
3625 if (!pCreateInfo->pRasterizationState->depthClampEnable &&
3626 ps->info.ps.writes_z) {
3627 /* From VK_EXT_depth_range_unrestricted spec:
3628 *
3629 * "The behavior described in Primitive Clipping still applies.
3630 * If depth clamping is disabled the depth values are still
3631 * clipped to 0 ≤ zc ≤ wc before the viewport transform. If
3632 * depth clamping is enabled the above equation is ignored and
3633 * the depth values are instead clamped to the VkViewport
3634 * minDepth and maxDepth values, which in the case of this
3635 * extension can be outside of the 0.0 to 1.0 range."
3636 */
3637 db_render_override |= S_02800C_DISABLE_VIEWPORT_CLAMP(1);
3638 }
3639
3640 radeon_set_context_reg(ctx_cs, R_028800_DB_DEPTH_CONTROL, db_depth_control);
3641 radeon_set_context_reg(ctx_cs, R_02842C_DB_STENCIL_CONTROL, db_stencil_control);
3642
3643 radeon_set_context_reg(ctx_cs, R_028000_DB_RENDER_CONTROL, db_render_control);
3644 radeon_set_context_reg(ctx_cs, R_02800C_DB_RENDER_OVERRIDE, db_render_override);
3645 radeon_set_context_reg(ctx_cs, R_028010_DB_RENDER_OVERRIDE2, db_render_override2);
3646 }
3647
3648 static void
3649 radv_pipeline_generate_blend_state(struct radeon_cmdbuf *ctx_cs,
3650 struct radv_pipeline *pipeline,
3651 const struct radv_blend_state *blend)
3652 {
3653 radeon_set_context_reg_seq(ctx_cs, R_028780_CB_BLEND0_CONTROL, 8);
3654 radeon_emit_array(ctx_cs, blend->cb_blend_control,
3655 8);
3656 radeon_set_context_reg(ctx_cs, R_028808_CB_COLOR_CONTROL, blend->cb_color_control);
3657 radeon_set_context_reg(ctx_cs, R_028B70_DB_ALPHA_TO_MASK, blend->db_alpha_to_mask);
3658
3659 if (pipeline->device->physical_device->rad_info.has_rbplus) {
3660
3661 radeon_set_context_reg_seq(ctx_cs, R_028760_SX_MRT0_BLEND_OPT, 8);
3662 radeon_emit_array(ctx_cs, blend->sx_mrt_blend_opt, 8);
3663 }
3664
3665 radeon_set_context_reg(ctx_cs, R_028714_SPI_SHADER_COL_FORMAT, blend->spi_shader_col_format);
3666
3667 radeon_set_context_reg(ctx_cs, R_028238_CB_TARGET_MASK, blend->cb_target_mask);
3668 radeon_set_context_reg(ctx_cs, R_02823C_CB_SHADER_MASK, blend->cb_shader_mask);
3669
3670 pipeline->graphics.col_format = blend->spi_shader_col_format;
3671 pipeline->graphics.cb_target_mask = blend->cb_target_mask;
3672 }
3673
3674 static const VkConservativeRasterizationModeEXT
3675 radv_get_conservative_raster_mode(const VkPipelineRasterizationStateCreateInfo *pCreateInfo)
3676 {
3677 const VkPipelineRasterizationConservativeStateCreateInfoEXT *conservative_raster =
3678 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_RASTERIZATION_CONSERVATIVE_STATE_CREATE_INFO_EXT);
3679
3680 if (!conservative_raster)
3681 return VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT;
3682 return conservative_raster->conservativeRasterizationMode;
3683 }
3684
3685 static void
3686 radv_pipeline_generate_raster_state(struct radeon_cmdbuf *ctx_cs,
3687 struct radv_pipeline *pipeline,
3688 const VkGraphicsPipelineCreateInfo *pCreateInfo)
3689 {
3690 const VkPipelineRasterizationStateCreateInfo *vkraster = pCreateInfo->pRasterizationState;
3691 const VkConservativeRasterizationModeEXT mode =
3692 radv_get_conservative_raster_mode(vkraster);
3693 uint32_t pa_sc_conservative_rast = S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1);
3694 bool depth_clip_disable = vkraster->depthClampEnable;
3695
3696 const VkPipelineRasterizationDepthClipStateCreateInfoEXT *depth_clip_state =
3697 vk_find_struct_const(vkraster->pNext, PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT);
3698 if (depth_clip_state) {
3699 depth_clip_disable = !depth_clip_state->depthClipEnable;
3700 }
3701
3702 radeon_set_context_reg(ctx_cs, R_028810_PA_CL_CLIP_CNTL,
3703 S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.
3704 S_028810_ZCLIP_NEAR_DISABLE(depth_clip_disable ? 1 : 0) |
3705 S_028810_ZCLIP_FAR_DISABLE(depth_clip_disable ? 1 : 0) |
3706 S_028810_DX_RASTERIZATION_KILL(vkraster->rasterizerDiscardEnable ? 1 : 0) |
3707 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1));
3708
3709 radeon_set_context_reg(ctx_cs, R_0286D4_SPI_INTERP_CONTROL_0,
3710 S_0286D4_FLAT_SHADE_ENA(1) |
3711 S_0286D4_PNT_SPRITE_ENA(1) |
3712 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
3713 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
3714 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
3715 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
3716 S_0286D4_PNT_SPRITE_TOP_1(0)); /* vulkan is top to bottom - 1.0 at bottom */
3717
3718 radeon_set_context_reg(ctx_cs, R_028BE4_PA_SU_VTX_CNTL,
3719 S_028BE4_PIX_CENTER(1) | // TODO verify
3720 S_028BE4_ROUND_MODE(V_028BE4_X_ROUND_TO_EVEN) |
3721 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH));
3722
3723 radeon_set_context_reg(ctx_cs, R_028814_PA_SU_SC_MODE_CNTL,
3724 S_028814_FACE(vkraster->frontFace) |
3725 S_028814_CULL_FRONT(!!(vkraster->cullMode & VK_CULL_MODE_FRONT_BIT)) |
3726 S_028814_CULL_BACK(!!(vkraster->cullMode & VK_CULL_MODE_BACK_BIT)) |
3727 S_028814_POLY_MODE(vkraster->polygonMode != VK_POLYGON_MODE_FILL) |
3728 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(vkraster->polygonMode)) |
3729 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(vkraster->polygonMode)) |
3730 S_028814_POLY_OFFSET_FRONT_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
3731 S_028814_POLY_OFFSET_BACK_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
3732 S_028814_POLY_OFFSET_PARA_ENABLE(vkraster->depthBiasEnable ? 1 : 0));
3733
3734 /* Conservative rasterization. */
3735 if (mode != VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT) {
3736 struct radv_multisample_state *ms = &pipeline->graphics.ms;
3737
3738 ms->pa_sc_aa_config |= S_028BE0_AA_MASK_CENTROID_DTMN(1);
3739 ms->db_eqaa |= S_028804_ENABLE_POSTZ_OVERRASTERIZATION(1) |
3740 S_028804_OVERRASTERIZATION_AMOUNT(4);
3741
3742 pa_sc_conservative_rast = S_028C4C_PREZ_AA_MASK_ENABLE(1) |
3743 S_028C4C_POSTZ_AA_MASK_ENABLE(1) |
3744 S_028C4C_CENTROID_SAMPLE_OVERRIDE(1);
3745
3746 if (mode == VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT) {
3747 pa_sc_conservative_rast |=
3748 S_028C4C_OVER_RAST_ENABLE(1) |
3749 S_028C4C_OVER_RAST_SAMPLE_SELECT(0) |
3750 S_028C4C_UNDER_RAST_ENABLE(0) |
3751 S_028C4C_UNDER_RAST_SAMPLE_SELECT(1) |
3752 S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(1);
3753 } else {
3754 assert(mode == VK_CONSERVATIVE_RASTERIZATION_MODE_UNDERESTIMATE_EXT);
3755 pa_sc_conservative_rast |=
3756 S_028C4C_OVER_RAST_ENABLE(0) |
3757 S_028C4C_OVER_RAST_SAMPLE_SELECT(1) |
3758 S_028C4C_UNDER_RAST_ENABLE(1) |
3759 S_028C4C_UNDER_RAST_SAMPLE_SELECT(0) |
3760 S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(0);
3761 }
3762 }
3763
3764 radeon_set_context_reg(ctx_cs, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL,
3765 pa_sc_conservative_rast);
3766 }
3767
3768
3769 static void
3770 radv_pipeline_generate_multisample_state(struct radeon_cmdbuf *ctx_cs,
3771 struct radv_pipeline *pipeline)
3772 {
3773 struct radv_multisample_state *ms = &pipeline->graphics.ms;
3774
3775 radeon_set_context_reg_seq(ctx_cs, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0, 2);
3776 radeon_emit(ctx_cs, ms->pa_sc_aa_mask[0]);
3777 radeon_emit(ctx_cs, ms->pa_sc_aa_mask[1]);
3778
3779 radeon_set_context_reg(ctx_cs, R_028804_DB_EQAA, ms->db_eqaa);
3780 radeon_set_context_reg(ctx_cs, R_028A48_PA_SC_MODE_CNTL_0, ms->pa_sc_mode_cntl_0);
3781 radeon_set_context_reg(ctx_cs, R_028A4C_PA_SC_MODE_CNTL_1, ms->pa_sc_mode_cntl_1);
3782 radeon_set_context_reg(ctx_cs, R_028BDC_PA_SC_LINE_CNTL, ms->pa_sc_line_cntl);
3783 radeon_set_context_reg(ctx_cs, R_028BE0_PA_SC_AA_CONFIG, ms->pa_sc_aa_config);
3784
3785 /* The exclusion bits can be set to improve rasterization efficiency
3786 * if no sample lies on the pixel boundary (-8 sample offset). It's
3787 * currently always TRUE because the driver doesn't support 16 samples.
3788 */
3789 bool exclusion = pipeline->device->physical_device->rad_info.chip_class >= GFX7;
3790 radeon_set_context_reg(ctx_cs, R_02882C_PA_SU_PRIM_FILTER_CNTL,
3791 S_02882C_XMAX_RIGHT_EXCLUSION(exclusion) |
3792 S_02882C_YMAX_BOTTOM_EXCLUSION(exclusion));
3793
3794 /* GFX9: Flush DFSM when the AA mode changes. */
3795 if (pipeline->device->dfsm_allowed) {
3796 radeon_emit(ctx_cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
3797 radeon_emit(ctx_cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
3798 }
3799 }
3800
3801 static void
3802 radv_pipeline_generate_vgt_gs_mode(struct radeon_cmdbuf *ctx_cs,
3803 struct radv_pipeline *pipeline)
3804 {
3805 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3806 const struct radv_shader_variant *vs =
3807 pipeline->shaders[MESA_SHADER_TESS_EVAL] ?
3808 pipeline->shaders[MESA_SHADER_TESS_EVAL] :
3809 pipeline->shaders[MESA_SHADER_VERTEX];
3810 unsigned vgt_primitiveid_en = 0;
3811 uint32_t vgt_gs_mode = 0;
3812
3813 if (radv_pipeline_has_ngg(pipeline))
3814 return;
3815
3816 if (radv_pipeline_has_gs(pipeline)) {
3817 const struct radv_shader_variant *gs =
3818 pipeline->shaders[MESA_SHADER_GEOMETRY];
3819
3820 vgt_gs_mode = ac_vgt_gs_mode(gs->info.gs.vertices_out,
3821 pipeline->device->physical_device->rad_info.chip_class);
3822 } else if (outinfo->export_prim_id || vs->info.uses_prim_id) {
3823 vgt_gs_mode = S_028A40_MODE(V_028A40_GS_SCENARIO_A);
3824 vgt_primitiveid_en |= S_028A84_PRIMITIVEID_EN(1);
3825 }
3826
3827 radeon_set_context_reg(ctx_cs, R_028A84_VGT_PRIMITIVEID_EN, vgt_primitiveid_en);
3828 radeon_set_context_reg(ctx_cs, R_028A40_VGT_GS_MODE, vgt_gs_mode);
3829 }
3830
3831 static void
3832 radv_pipeline_generate_hw_vs(struct radeon_cmdbuf *ctx_cs,
3833 struct radeon_cmdbuf *cs,
3834 struct radv_pipeline *pipeline,
3835 struct radv_shader_variant *shader)
3836 {
3837 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3838
3839 radeon_set_sh_reg_seq(cs, R_00B120_SPI_SHADER_PGM_LO_VS, 4);
3840 radeon_emit(cs, va >> 8);
3841 radeon_emit(cs, S_00B124_MEM_BASE(va >> 40));
3842 radeon_emit(cs, shader->config.rsrc1);
3843 radeon_emit(cs, shader->config.rsrc2);
3844
3845 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3846 unsigned clip_dist_mask, cull_dist_mask, total_mask;
3847 clip_dist_mask = outinfo->clip_dist_mask;
3848 cull_dist_mask = outinfo->cull_dist_mask;
3849 total_mask = clip_dist_mask | cull_dist_mask;
3850 bool misc_vec_ena = outinfo->writes_pointsize ||
3851 outinfo->writes_layer ||
3852 outinfo->writes_viewport_index;
3853 unsigned spi_vs_out_config, nparams;
3854
3855 /* VS is required to export at least one param. */
3856 nparams = MAX2(outinfo->param_exports, 1);
3857 spi_vs_out_config = S_0286C4_VS_EXPORT_COUNT(nparams - 1);
3858
3859 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3860 spi_vs_out_config |= S_0286C4_NO_PC_EXPORT(outinfo->param_exports == 0);
3861 }
3862
3863 radeon_set_context_reg(ctx_cs, R_0286C4_SPI_VS_OUT_CONFIG, spi_vs_out_config);
3864
3865 radeon_set_context_reg(ctx_cs, R_02870C_SPI_SHADER_POS_FORMAT,
3866 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
3867 S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ?
3868 V_02870C_SPI_SHADER_4COMP :
3869 V_02870C_SPI_SHADER_NONE) |
3870 S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ?
3871 V_02870C_SPI_SHADER_4COMP :
3872 V_02870C_SPI_SHADER_NONE) |
3873 S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ?
3874 V_02870C_SPI_SHADER_4COMP :
3875 V_02870C_SPI_SHADER_NONE));
3876
3877 radeon_set_context_reg(ctx_cs, R_028818_PA_CL_VTE_CNTL,
3878 S_028818_VTX_W0_FMT(1) |
3879 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
3880 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
3881 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
3882
3883 radeon_set_context_reg(ctx_cs, R_02881C_PA_CL_VS_OUT_CNTL,
3884 S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |
3885 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |
3886 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |
3887 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |
3888 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |
3889 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |
3890 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |
3891 S_02881C_BYPASS_PRIM_RATE_COMBINER_GFX103(pipeline->device->physical_device->rad_info.chip_class >= GFX10_3) |
3892 cull_dist_mask << 8 |
3893 clip_dist_mask);
3894
3895 if (pipeline->device->physical_device->rad_info.chip_class <= GFX8)
3896 radeon_set_context_reg(ctx_cs, R_028AB4_VGT_REUSE_OFF,
3897 outinfo->writes_viewport_index);
3898 }
3899
3900 static void
3901 radv_pipeline_generate_hw_es(struct radeon_cmdbuf *cs,
3902 struct radv_pipeline *pipeline,
3903 struct radv_shader_variant *shader)
3904 {
3905 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3906
3907 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 4);
3908 radeon_emit(cs, va >> 8);
3909 radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));
3910 radeon_emit(cs, shader->config.rsrc1);
3911 radeon_emit(cs, shader->config.rsrc2);
3912 }
3913
3914 static void
3915 radv_pipeline_generate_hw_ls(struct radeon_cmdbuf *cs,
3916 struct radv_pipeline *pipeline,
3917 struct radv_shader_variant *shader,
3918 const struct radv_tessellation_state *tess)
3919 {
3920 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3921 uint32_t rsrc2 = shader->config.rsrc2;
3922
3923 radeon_set_sh_reg_seq(cs, R_00B520_SPI_SHADER_PGM_LO_LS, 2);
3924 radeon_emit(cs, va >> 8);
3925 radeon_emit(cs, S_00B524_MEM_BASE(va >> 40));
3926
3927 rsrc2 |= S_00B52C_LDS_SIZE(tess->lds_size);
3928 if (pipeline->device->physical_device->rad_info.chip_class == GFX7 &&
3929 pipeline->device->physical_device->rad_info.family != CHIP_HAWAII)
3930 radeon_set_sh_reg(cs, R_00B52C_SPI_SHADER_PGM_RSRC2_LS, rsrc2);
3931
3932 radeon_set_sh_reg_seq(cs, R_00B528_SPI_SHADER_PGM_RSRC1_LS, 2);
3933 radeon_emit(cs, shader->config.rsrc1);
3934 radeon_emit(cs, rsrc2);
3935 }
3936
3937 static void
3938 radv_pipeline_generate_hw_ngg(struct radeon_cmdbuf *ctx_cs,
3939 struct radeon_cmdbuf *cs,
3940 struct radv_pipeline *pipeline,
3941 struct radv_shader_variant *shader)
3942 {
3943 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3944 gl_shader_stage es_type =
3945 radv_pipeline_has_tess(pipeline) ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
3946 struct radv_shader_variant *es =
3947 es_type == MESA_SHADER_TESS_EVAL ? pipeline->shaders[MESA_SHADER_TESS_EVAL] : pipeline->shaders[MESA_SHADER_VERTEX];
3948 const struct gfx10_ngg_info *ngg_state = &shader->info.ngg_info;
3949
3950 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 2);
3951 radeon_emit(cs, va >> 8);
3952 radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));
3953 radeon_set_sh_reg_seq(cs, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 2);
3954 radeon_emit(cs, shader->config.rsrc1);
3955 radeon_emit(cs, shader->config.rsrc2);
3956
3957 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3958 unsigned clip_dist_mask, cull_dist_mask, total_mask;
3959 clip_dist_mask = outinfo->clip_dist_mask;
3960 cull_dist_mask = outinfo->cull_dist_mask;
3961 total_mask = clip_dist_mask | cull_dist_mask;
3962 bool misc_vec_ena = outinfo->writes_pointsize ||
3963 outinfo->writes_layer ||
3964 outinfo->writes_viewport_index;
3965 bool es_enable_prim_id = outinfo->export_prim_id ||
3966 (es && es->info.uses_prim_id);
3967 bool break_wave_at_eoi = false;
3968 unsigned ge_cntl;
3969 unsigned nparams;
3970
3971 if (es_type == MESA_SHADER_TESS_EVAL) {
3972 struct radv_shader_variant *gs =
3973 pipeline->shaders[MESA_SHADER_GEOMETRY];
3974
3975 if (es_enable_prim_id || (gs && gs->info.uses_prim_id))
3976 break_wave_at_eoi = true;
3977 }
3978
3979 nparams = MAX2(outinfo->param_exports, 1);
3980 radeon_set_context_reg(ctx_cs, R_0286C4_SPI_VS_OUT_CONFIG,
3981 S_0286C4_VS_EXPORT_COUNT(nparams - 1) |
3982 S_0286C4_NO_PC_EXPORT(outinfo->param_exports == 0));
3983
3984 radeon_set_context_reg(ctx_cs, R_028708_SPI_SHADER_IDX_FORMAT,
3985 S_028708_IDX0_EXPORT_FORMAT(V_028708_SPI_SHADER_1COMP));
3986 radeon_set_context_reg(ctx_cs, R_02870C_SPI_SHADER_POS_FORMAT,
3987 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
3988 S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ?
3989 V_02870C_SPI_SHADER_4COMP :
3990 V_02870C_SPI_SHADER_NONE) |
3991 S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ?
3992 V_02870C_SPI_SHADER_4COMP :
3993 V_02870C_SPI_SHADER_NONE) |
3994 S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ?
3995 V_02870C_SPI_SHADER_4COMP :
3996 V_02870C_SPI_SHADER_NONE));
3997
3998 radeon_set_context_reg(ctx_cs, R_028818_PA_CL_VTE_CNTL,
3999 S_028818_VTX_W0_FMT(1) |
4000 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
4001 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
4002 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
4003 radeon_set_context_reg(ctx_cs, R_02881C_PA_CL_VS_OUT_CNTL,
4004 S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |
4005 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |
4006 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |
4007 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |
4008 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |
4009 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |
4010 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |
4011 S_02881C_BYPASS_PRIM_RATE_COMBINER_GFX103(pipeline->device->physical_device->rad_info.chip_class >= GFX10_3) |
4012 cull_dist_mask << 8 |
4013 clip_dist_mask);
4014
4015 radeon_set_context_reg(ctx_cs, R_028A84_VGT_PRIMITIVEID_EN,
4016 S_028A84_PRIMITIVEID_EN(es_enable_prim_id) |
4017 S_028A84_NGG_DISABLE_PROVOK_REUSE(outinfo->export_prim_id));
4018
4019 radeon_set_context_reg(ctx_cs, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
4020 ngg_state->vgt_esgs_ring_itemsize);
4021
4022 /* NGG specific registers. */
4023 struct radv_shader_variant *gs = pipeline->shaders[MESA_SHADER_GEOMETRY];
4024 uint32_t gs_num_invocations = gs ? gs->info.gs.invocations : 1;
4025
4026 radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL,
4027 S_028A44_ES_VERTS_PER_SUBGRP(ngg_state->hw_max_esverts) |
4028 S_028A44_GS_PRIMS_PER_SUBGRP(ngg_state->max_gsprims) |
4029 S_028A44_GS_INST_PRIMS_IN_SUBGRP(ngg_state->max_gsprims * gs_num_invocations));
4030 radeon_set_context_reg(ctx_cs, R_0287FC_GE_MAX_OUTPUT_PER_SUBGROUP,
4031 S_0287FC_MAX_VERTS_PER_SUBGROUP(ngg_state->max_out_verts));
4032 radeon_set_context_reg(ctx_cs, R_028B4C_GE_NGG_SUBGRP_CNTL,
4033 S_028B4C_PRIM_AMP_FACTOR(ngg_state->prim_amp_factor) |
4034 S_028B4C_THDS_PER_SUBGRP(0)); /* for fast launch */
4035 radeon_set_context_reg(ctx_cs, R_028B90_VGT_GS_INSTANCE_CNT,
4036 S_028B90_CNT(gs_num_invocations) |
4037 S_028B90_ENABLE(gs_num_invocations > 1) |
4038 S_028B90_EN_MAX_VERT_OUT_PER_GS_INSTANCE(ngg_state->max_vert_out_per_gs_instance));
4039
4040 /* User edge flags are set by the pos exports. If user edge flags are
4041 * not used, we must use hw-generated edge flags and pass them via
4042 * the prim export to prevent drawing lines on internal edges of
4043 * decomposed primitives (such as quads) with polygon mode = lines.
4044 *
4045 * TODO: We should combine hw-generated edge flags with user edge
4046 * flags in the shader.
4047 */
4048 radeon_set_context_reg(ctx_cs, R_028838_PA_CL_NGG_CNTL,
4049 S_028838_INDEX_BUF_EDGE_FLAG_ENA(!radv_pipeline_has_tess(pipeline) &&
4050 !radv_pipeline_has_gs(pipeline)) |
4051 /* Reuse for NGG. */
4052 S_028838_VERTEX_REUSE_DEPTH_GFX103(pipeline->device->physical_device->rad_info.chip_class >= GFX10_3 ? 30 : 0));
4053
4054 ge_cntl = S_03096C_PRIM_GRP_SIZE(ngg_state->max_gsprims) |
4055 S_03096C_VERT_GRP_SIZE(256) | /* 256 = disable vertex grouping */
4056 S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi);
4057
4058 /* Bug workaround for a possible hang with non-tessellation cases.
4059 * Tessellation always sets GE_CNTL.VERT_GRP_SIZE = 0
4060 *
4061 * Requirement: GE_CNTL.VERT_GRP_SIZE = VGT_GS_ONCHIP_CNTL.ES_VERTS_PER_SUBGRP - 5
4062 */
4063 if (pipeline->device->physical_device->rad_info.chip_class == GFX10 &&
4064 !radv_pipeline_has_tess(pipeline) &&
4065 ngg_state->hw_max_esverts != 256) {
4066 ge_cntl &= C_03096C_VERT_GRP_SIZE;
4067
4068 if (ngg_state->hw_max_esverts > 5) {
4069 ge_cntl |= S_03096C_VERT_GRP_SIZE(ngg_state->hw_max_esverts - 5);
4070 }
4071 }
4072
4073 radeon_set_uconfig_reg(ctx_cs, R_03096C_GE_CNTL, ge_cntl);
4074 }
4075
4076 static void
4077 radv_pipeline_generate_hw_hs(struct radeon_cmdbuf *cs,
4078 struct radv_pipeline *pipeline,
4079 struct radv_shader_variant *shader,
4080 const struct radv_tessellation_state *tess)
4081 {
4082 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
4083
4084 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {
4085 unsigned hs_rsrc2 = shader->config.rsrc2;
4086
4087 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
4088 hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX10(tess->lds_size);
4089 } else {
4090 hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX9(tess->lds_size);
4091 }
4092
4093 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
4094 radeon_set_sh_reg_seq(cs, R_00B520_SPI_SHADER_PGM_LO_LS, 2);
4095 radeon_emit(cs, va >> 8);
4096 radeon_emit(cs, S_00B524_MEM_BASE(va >> 40));
4097 } else {
4098 radeon_set_sh_reg_seq(cs, R_00B410_SPI_SHADER_PGM_LO_LS, 2);
4099 radeon_emit(cs, va >> 8);
4100 radeon_emit(cs, S_00B414_MEM_BASE(va >> 40));
4101 }
4102
4103 radeon_set_sh_reg_seq(cs, R_00B428_SPI_SHADER_PGM_RSRC1_HS, 2);
4104 radeon_emit(cs, shader->config.rsrc1);
4105 radeon_emit(cs, hs_rsrc2);
4106 } else {
4107 radeon_set_sh_reg_seq(cs, R_00B420_SPI_SHADER_PGM_LO_HS, 4);
4108 radeon_emit(cs, va >> 8);
4109 radeon_emit(cs, S_00B424_MEM_BASE(va >> 40));
4110 radeon_emit(cs, shader->config.rsrc1);
4111 radeon_emit(cs, shader->config.rsrc2);
4112 }
4113 }
4114
4115 static void
4116 radv_pipeline_generate_vertex_shader(struct radeon_cmdbuf *ctx_cs,
4117 struct radeon_cmdbuf *cs,
4118 struct radv_pipeline *pipeline,
4119 const struct radv_tessellation_state *tess)
4120 {
4121 struct radv_shader_variant *vs;
4122
4123 /* Skip shaders merged into HS/GS */
4124 vs = pipeline->shaders[MESA_SHADER_VERTEX];
4125 if (!vs)
4126 return;
4127
4128 if (vs->info.vs.as_ls)
4129 radv_pipeline_generate_hw_ls(cs, pipeline, vs, tess);
4130 else if (vs->info.vs.as_es)
4131 radv_pipeline_generate_hw_es(cs, pipeline, vs);
4132 else if (vs->info.is_ngg)
4133 radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, vs);
4134 else
4135 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, vs);
4136 }
4137
4138 static void
4139 radv_pipeline_generate_tess_shaders(struct radeon_cmdbuf *ctx_cs,
4140 struct radeon_cmdbuf *cs,
4141 struct radv_pipeline *pipeline,
4142 const struct radv_tessellation_state *tess)
4143 {
4144 if (!radv_pipeline_has_tess(pipeline))
4145 return;
4146
4147 struct radv_shader_variant *tes, *tcs;
4148
4149 tcs = pipeline->shaders[MESA_SHADER_TESS_CTRL];
4150 tes = pipeline->shaders[MESA_SHADER_TESS_EVAL];
4151
4152 if (tes) {
4153 if (tes->info.is_ngg) {
4154 radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, tes);
4155 } else if (tes->info.tes.as_es)
4156 radv_pipeline_generate_hw_es(cs, pipeline, tes);
4157 else
4158 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, tes);
4159 }
4160
4161 radv_pipeline_generate_hw_hs(cs, pipeline, tcs, tess);
4162
4163 radeon_set_context_reg(ctx_cs, R_028B6C_VGT_TF_PARAM,
4164 tess->tf_param);
4165
4166 if (pipeline->device->physical_device->rad_info.chip_class >= GFX7)
4167 radeon_set_context_reg_idx(ctx_cs, R_028B58_VGT_LS_HS_CONFIG, 2,
4168 tess->ls_hs_config);
4169 else
4170 radeon_set_context_reg(ctx_cs, R_028B58_VGT_LS_HS_CONFIG,
4171 tess->ls_hs_config);
4172
4173 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10 &&
4174 !radv_pipeline_has_gs(pipeline) && !radv_pipeline_has_ngg(pipeline)) {
4175 radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL,
4176 S_028A44_ES_VERTS_PER_SUBGRP(250) |
4177 S_028A44_GS_PRIMS_PER_SUBGRP(126) |
4178 S_028A44_GS_INST_PRIMS_IN_SUBGRP(126));
4179 }
4180 }
4181
4182 static void
4183 radv_pipeline_generate_hw_gs(struct radeon_cmdbuf *ctx_cs,
4184 struct radeon_cmdbuf *cs,
4185 struct radv_pipeline *pipeline,
4186 struct radv_shader_variant *gs)
4187 {
4188 const struct gfx9_gs_info *gs_state = &gs->info.gs_ring_info;
4189 unsigned gs_max_out_vertices;
4190 uint8_t *num_components;
4191 uint8_t max_stream;
4192 unsigned offset;
4193 uint64_t va;
4194
4195 gs_max_out_vertices = gs->info.gs.vertices_out;
4196 max_stream = gs->info.gs.max_stream;
4197 num_components = gs->info.gs.num_stream_output_components;
4198
4199 offset = num_components[0] * gs_max_out_vertices;
4200
4201 radeon_set_context_reg_seq(ctx_cs, R_028A60_VGT_GSVS_RING_OFFSET_1, 3);
4202 radeon_emit(ctx_cs, offset);
4203 if (max_stream >= 1)
4204 offset += num_components[1] * gs_max_out_vertices;
4205 radeon_emit(ctx_cs, offset);
4206 if (max_stream >= 2)
4207 offset += num_components[2] * gs_max_out_vertices;
4208 radeon_emit(ctx_cs, offset);
4209 if (max_stream >= 3)
4210 offset += num_components[3] * gs_max_out_vertices;
4211 radeon_set_context_reg(ctx_cs, R_028AB0_VGT_GSVS_RING_ITEMSIZE, offset);
4212
4213 radeon_set_context_reg_seq(ctx_cs, R_028B5C_VGT_GS_VERT_ITEMSIZE, 4);
4214 radeon_emit(ctx_cs, num_components[0]);
4215 radeon_emit(ctx_cs, (max_stream >= 1) ? num_components[1] : 0);
4216 radeon_emit(ctx_cs, (max_stream >= 2) ? num_components[2] : 0);
4217 radeon_emit(ctx_cs, (max_stream >= 3) ? num_components[3] : 0);
4218
4219 uint32_t gs_num_invocations = gs->info.gs.invocations;
4220 radeon_set_context_reg(ctx_cs, R_028B90_VGT_GS_INSTANCE_CNT,
4221 S_028B90_CNT(MIN2(gs_num_invocations, 127)) |
4222 S_028B90_ENABLE(gs_num_invocations > 0));
4223
4224 radeon_set_context_reg(ctx_cs, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
4225 gs_state->vgt_esgs_ring_itemsize);
4226
4227 va = radv_buffer_get_va(gs->bo) + gs->bo_offset;
4228
4229 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {
4230 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
4231 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 2);
4232 radeon_emit(cs, va >> 8);
4233 radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));
4234 } else {
4235 radeon_set_sh_reg_seq(cs, R_00B210_SPI_SHADER_PGM_LO_ES, 2);
4236 radeon_emit(cs, va >> 8);
4237 radeon_emit(cs, S_00B214_MEM_BASE(va >> 40));
4238 }
4239
4240 radeon_set_sh_reg_seq(cs, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 2);
4241 radeon_emit(cs, gs->config.rsrc1);
4242 radeon_emit(cs, gs->config.rsrc2 | S_00B22C_LDS_SIZE(gs_state->lds_size));
4243
4244 radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL, gs_state->vgt_gs_onchip_cntl);
4245 radeon_set_context_reg(ctx_cs, R_028A94_VGT_GS_MAX_PRIMS_PER_SUBGROUP, gs_state->vgt_gs_max_prims_per_subgroup);
4246 } else {
4247 radeon_set_sh_reg_seq(cs, R_00B220_SPI_SHADER_PGM_LO_GS, 4);
4248 radeon_emit(cs, va >> 8);
4249 radeon_emit(cs, S_00B224_MEM_BASE(va >> 40));
4250 radeon_emit(cs, gs->config.rsrc1);
4251 radeon_emit(cs, gs->config.rsrc2);
4252 }
4253
4254 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, pipeline->gs_copy_shader);
4255 }
4256
4257 static void
4258 radv_pipeline_generate_geometry_shader(struct radeon_cmdbuf *ctx_cs,
4259 struct radeon_cmdbuf *cs,
4260 struct radv_pipeline *pipeline)
4261 {
4262 struct radv_shader_variant *gs;
4263
4264 gs = pipeline->shaders[MESA_SHADER_GEOMETRY];
4265 if (!gs)
4266 return;
4267
4268 if (gs->info.is_ngg)
4269 radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, gs);
4270 else
4271 radv_pipeline_generate_hw_gs(ctx_cs, cs, pipeline, gs);
4272
4273 radeon_set_context_reg(ctx_cs, R_028B38_VGT_GS_MAX_VERT_OUT,
4274 gs->info.gs.vertices_out);
4275 }
4276
4277 static uint32_t offset_to_ps_input(uint32_t offset, bool flat_shade,
4278 bool explicit, bool float16)
4279 {
4280 uint32_t ps_input_cntl;
4281 if (offset <= AC_EXP_PARAM_OFFSET_31) {
4282 ps_input_cntl = S_028644_OFFSET(offset);
4283 if (flat_shade || explicit)
4284 ps_input_cntl |= S_028644_FLAT_SHADE(1);
4285 if (explicit) {
4286 /* Force parameter cache to be read in passthrough
4287 * mode.
4288 */
4289 ps_input_cntl |= S_028644_OFFSET(1 << 5);
4290 }
4291 if (float16) {
4292 ps_input_cntl |= S_028644_FP16_INTERP_MODE(1) |
4293 S_028644_ATTR0_VALID(1);
4294 }
4295 } else {
4296 /* The input is a DEFAULT_VAL constant. */
4297 assert(offset >= AC_EXP_PARAM_DEFAULT_VAL_0000 &&
4298 offset <= AC_EXP_PARAM_DEFAULT_VAL_1111);
4299 offset -= AC_EXP_PARAM_DEFAULT_VAL_0000;
4300 ps_input_cntl = S_028644_OFFSET(0x20) |
4301 S_028644_DEFAULT_VAL(offset);
4302 }
4303 return ps_input_cntl;
4304 }
4305
4306 static void
4307 radv_pipeline_generate_ps_inputs(struct radeon_cmdbuf *ctx_cs,
4308 struct radv_pipeline *pipeline)
4309 {
4310 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
4311 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
4312 uint32_t ps_input_cntl[32];
4313
4314 unsigned ps_offset = 0;
4315
4316 if (ps->info.ps.prim_id_input) {
4317 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID];
4318 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
4319 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false, false);
4320 ++ps_offset;
4321 }
4322 }
4323
4324 if (ps->info.ps.layer_input ||
4325 ps->info.needs_multiview_view_index) {
4326 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_LAYER];
4327 if (vs_offset != AC_EXP_PARAM_UNDEFINED)
4328 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false, false);
4329 else
4330 ps_input_cntl[ps_offset] = offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000, true, false, false);
4331 ++ps_offset;
4332 }
4333
4334 if (ps->info.ps.viewport_index_input) {
4335 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_VIEWPORT];
4336 if (vs_offset != AC_EXP_PARAM_UNDEFINED)
4337 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false, false);
4338 else
4339 ps_input_cntl[ps_offset] = offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000, true, false, false);
4340 ++ps_offset;
4341 }
4342
4343 if (ps->info.ps.has_pcoord) {
4344 unsigned val;
4345 val = S_028644_PT_SPRITE_TEX(1) | S_028644_OFFSET(0x20);
4346 ps_input_cntl[ps_offset] = val;
4347 ps_offset++;
4348 }
4349
4350 if (ps->info.ps.num_input_clips_culls) {
4351 unsigned vs_offset;
4352
4353 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_CLIP_DIST0];
4354 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
4355 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, false, false, false);
4356 ++ps_offset;
4357 }
4358
4359 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_CLIP_DIST1];
4360 if (vs_offset != AC_EXP_PARAM_UNDEFINED &&
4361 ps->info.ps.num_input_clips_culls > 4) {
4362 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, false, false, false);
4363 ++ps_offset;
4364 }
4365 }
4366
4367 for (unsigned i = 0; i < 32 && (1u << i) <= ps->info.ps.input_mask; ++i) {
4368 unsigned vs_offset;
4369 bool flat_shade;
4370 bool explicit;
4371 bool float16;
4372 if (!(ps->info.ps.input_mask & (1u << i)))
4373 continue;
4374
4375 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_VAR0 + i];
4376 if (vs_offset == AC_EXP_PARAM_UNDEFINED) {
4377 ps_input_cntl[ps_offset] = S_028644_OFFSET(0x20);
4378 ++ps_offset;
4379 continue;
4380 }
4381
4382 flat_shade = !!(ps->info.ps.flat_shaded_mask & (1u << ps_offset));
4383 explicit = !!(ps->info.ps.explicit_shaded_mask & (1u << ps_offset));
4384 float16 = !!(ps->info.ps.float16_shaded_mask & (1u << ps_offset));
4385
4386 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, flat_shade, explicit, float16);
4387 ++ps_offset;
4388 }
4389
4390 if (ps_offset) {
4391 radeon_set_context_reg_seq(ctx_cs, R_028644_SPI_PS_INPUT_CNTL_0, ps_offset);
4392 for (unsigned i = 0; i < ps_offset; i++) {
4393 radeon_emit(ctx_cs, ps_input_cntl[i]);
4394 }
4395 }
4396 }
4397
4398 static uint32_t
4399 radv_compute_db_shader_control(const struct radv_device *device,
4400 const struct radv_pipeline *pipeline,
4401 const struct radv_shader_variant *ps)
4402 {
4403 unsigned conservative_z_export = V_02880C_EXPORT_ANY_Z;
4404 unsigned z_order;
4405 if (ps->info.ps.early_fragment_test || !ps->info.ps.writes_memory)
4406 z_order = V_02880C_EARLY_Z_THEN_LATE_Z;
4407 else
4408 z_order = V_02880C_LATE_Z;
4409
4410 if (ps->info.ps.depth_layout == FRAG_DEPTH_LAYOUT_GREATER)
4411 conservative_z_export = V_02880C_EXPORT_GREATER_THAN_Z;
4412 else if (ps->info.ps.depth_layout == FRAG_DEPTH_LAYOUT_LESS)
4413 conservative_z_export = V_02880C_EXPORT_LESS_THAN_Z;
4414
4415 bool disable_rbplus = device->physical_device->rad_info.has_rbplus &&
4416 !device->physical_device->rad_info.rbplus_allowed;
4417
4418 /* It shouldn't be needed to export gl_SampleMask when MSAA is disabled
4419 * but this appears to break Project Cars (DXVK). See
4420 * https://bugs.freedesktop.org/show_bug.cgi?id=109401
4421 */
4422 bool mask_export_enable = ps->info.ps.writes_sample_mask;
4423
4424 return S_02880C_Z_EXPORT_ENABLE(ps->info.ps.writes_z) |
4425 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps->info.ps.writes_stencil) |
4426 S_02880C_KILL_ENABLE(!!ps->info.ps.can_discard) |
4427 S_02880C_MASK_EXPORT_ENABLE(mask_export_enable) |
4428 S_02880C_CONSERVATIVE_Z_EXPORT(conservative_z_export) |
4429 S_02880C_Z_ORDER(z_order) |
4430 S_02880C_DEPTH_BEFORE_SHADER(ps->info.ps.early_fragment_test) |
4431 S_02880C_PRE_SHADER_DEPTH_COVERAGE_ENABLE(ps->info.ps.post_depth_coverage) |
4432 S_02880C_EXEC_ON_HIER_FAIL(ps->info.ps.writes_memory) |
4433 S_02880C_EXEC_ON_NOOP(ps->info.ps.writes_memory) |
4434 S_02880C_DUAL_QUAD_DISABLE(disable_rbplus);
4435 }
4436
4437 static void
4438 radv_pipeline_generate_fragment_shader(struct radeon_cmdbuf *ctx_cs,
4439 struct radeon_cmdbuf *cs,
4440 struct radv_pipeline *pipeline)
4441 {
4442 struct radv_shader_variant *ps;
4443 uint64_t va;
4444 assert (pipeline->shaders[MESA_SHADER_FRAGMENT]);
4445
4446 ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
4447 va = radv_buffer_get_va(ps->bo) + ps->bo_offset;
4448
4449 radeon_set_sh_reg_seq(cs, R_00B020_SPI_SHADER_PGM_LO_PS, 4);
4450 radeon_emit(cs, va >> 8);
4451 radeon_emit(cs, S_00B024_MEM_BASE(va >> 40));
4452 radeon_emit(cs, ps->config.rsrc1);
4453 radeon_emit(cs, ps->config.rsrc2);
4454
4455 radeon_set_context_reg(ctx_cs, R_02880C_DB_SHADER_CONTROL,
4456 radv_compute_db_shader_control(pipeline->device,
4457 pipeline, ps));
4458
4459 radeon_set_context_reg(ctx_cs, R_0286CC_SPI_PS_INPUT_ENA,
4460 ps->config.spi_ps_input_ena);
4461
4462 radeon_set_context_reg(ctx_cs, R_0286D0_SPI_PS_INPUT_ADDR,
4463 ps->config.spi_ps_input_addr);
4464
4465 radeon_set_context_reg(ctx_cs, R_0286D8_SPI_PS_IN_CONTROL,
4466 S_0286D8_NUM_INTERP(ps->info.ps.num_interp) |
4467 S_0286D8_PS_W32_EN(ps->info.wave_size == 32));
4468
4469 radeon_set_context_reg(ctx_cs, R_0286E0_SPI_BARYC_CNTL, pipeline->graphics.spi_baryc_cntl);
4470
4471 radeon_set_context_reg(ctx_cs, R_028710_SPI_SHADER_Z_FORMAT,
4472 ac_get_spi_shader_z_format(ps->info.ps.writes_z,
4473 ps->info.ps.writes_stencil,
4474 ps->info.ps.writes_sample_mask));
4475
4476 if (pipeline->device->dfsm_allowed) {
4477 /* optimise this? */
4478 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
4479 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
4480 }
4481 }
4482
4483 static void
4484 radv_pipeline_generate_vgt_vertex_reuse(struct radeon_cmdbuf *ctx_cs,
4485 struct radv_pipeline *pipeline)
4486 {
4487 if (pipeline->device->physical_device->rad_info.family < CHIP_POLARIS10 ||
4488 pipeline->device->physical_device->rad_info.chip_class >= GFX10)
4489 return;
4490
4491 unsigned vtx_reuse_depth = 30;
4492 if (radv_pipeline_has_tess(pipeline) &&
4493 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.tes.spacing == TESS_SPACING_FRACTIONAL_ODD) {
4494 vtx_reuse_depth = 14;
4495 }
4496 radeon_set_context_reg(ctx_cs, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL,
4497 S_028C58_VTX_REUSE_DEPTH(vtx_reuse_depth));
4498 }
4499
4500 static uint32_t
4501 radv_compute_vgt_shader_stages_en(const struct radv_pipeline *pipeline)
4502 {
4503 uint32_t stages = 0;
4504 if (radv_pipeline_has_tess(pipeline)) {
4505 stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) |
4506 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
4507
4508 if (radv_pipeline_has_gs(pipeline))
4509 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) |
4510 S_028B54_GS_EN(1);
4511 else if (radv_pipeline_has_ngg(pipeline))
4512 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS);
4513 else
4514 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS);
4515 } else if (radv_pipeline_has_gs(pipeline)) {
4516 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) |
4517 S_028B54_GS_EN(1);
4518 } else if (radv_pipeline_has_ngg(pipeline)) {
4519 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL);
4520 }
4521
4522 if (radv_pipeline_has_ngg(pipeline)) {
4523 stages |= S_028B54_PRIMGEN_EN(1);
4524 if (pipeline->streamout_shader)
4525 stages |= S_028B54_NGG_WAVE_ID_EN(1);
4526 if (radv_pipeline_has_ngg_passthrough(pipeline))
4527 stages |= S_028B54_PRIMGEN_PASSTHRU_EN(1);
4528 } else if (radv_pipeline_has_gs(pipeline)) {
4529 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
4530 }
4531
4532 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9)
4533 stages |= S_028B54_MAX_PRIMGRP_IN_WAVE(2);
4534
4535 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
4536 uint8_t hs_size = 64, gs_size = 64, vs_size = 64;
4537
4538 if (radv_pipeline_has_tess(pipeline))
4539 hs_size = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.wave_size;
4540
4541 if (pipeline->shaders[MESA_SHADER_GEOMETRY]) {
4542 vs_size = gs_size = pipeline->shaders[MESA_SHADER_GEOMETRY]->info.wave_size;
4543 if (pipeline->gs_copy_shader)
4544 vs_size = pipeline->gs_copy_shader->info.wave_size;
4545 } else if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
4546 vs_size = pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.wave_size;
4547 else if (pipeline->shaders[MESA_SHADER_VERTEX])
4548 vs_size = pipeline->shaders[MESA_SHADER_VERTEX]->info.wave_size;
4549
4550 if (radv_pipeline_has_ngg(pipeline))
4551 gs_size = vs_size;
4552
4553 /* legacy GS only supports Wave64 */
4554 stages |= S_028B54_HS_W32_EN(hs_size == 32 ? 1 : 0) |
4555 S_028B54_GS_W32_EN(gs_size == 32 ? 1 : 0) |
4556 S_028B54_VS_W32_EN(vs_size == 32 ? 1 : 0);
4557 }
4558
4559 return stages;
4560 }
4561
4562 static uint32_t
4563 radv_compute_cliprect_rule(const VkGraphicsPipelineCreateInfo *pCreateInfo)
4564 {
4565 const VkPipelineDiscardRectangleStateCreateInfoEXT *discard_rectangle_info =
4566 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);
4567
4568 if (!discard_rectangle_info)
4569 return 0xffff;
4570
4571 unsigned mask = 0;
4572
4573 for (unsigned i = 0; i < (1u << MAX_DISCARD_RECTANGLES); ++i) {
4574 /* Interpret i as a bitmask, and then set the bit in the mask if
4575 * that combination of rectangles in which the pixel is contained
4576 * should pass the cliprect test. */
4577 unsigned relevant_subset = i & ((1u << discard_rectangle_info->discardRectangleCount) - 1);
4578
4579 if (discard_rectangle_info->discardRectangleMode == VK_DISCARD_RECTANGLE_MODE_INCLUSIVE_EXT &&
4580 !relevant_subset)
4581 continue;
4582
4583 if (discard_rectangle_info->discardRectangleMode == VK_DISCARD_RECTANGLE_MODE_EXCLUSIVE_EXT &&
4584 relevant_subset)
4585 continue;
4586
4587 mask |= 1u << i;
4588 }
4589
4590 return mask;
4591 }
4592
4593 static void
4594 gfx10_pipeline_generate_ge_cntl(struct radeon_cmdbuf *ctx_cs,
4595 struct radv_pipeline *pipeline,
4596 const struct radv_tessellation_state *tess)
4597 {
4598 bool break_wave_at_eoi = false;
4599 unsigned primgroup_size;
4600 unsigned vertgroup_size = 256; /* 256 = disable vertex grouping */
4601
4602 if (radv_pipeline_has_tess(pipeline)) {
4603 primgroup_size = tess->num_patches; /* must be a multiple of NUM_PATCHES */
4604 } else if (radv_pipeline_has_gs(pipeline)) {
4605 const struct gfx9_gs_info *gs_state =
4606 &pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs_ring_info;
4607 unsigned vgt_gs_onchip_cntl = gs_state->vgt_gs_onchip_cntl;
4608 primgroup_size = G_028A44_GS_PRIMS_PER_SUBGRP(vgt_gs_onchip_cntl);
4609 } else {
4610 primgroup_size = 128; /* recommended without a GS and tess */
4611 }
4612
4613 if (radv_pipeline_has_tess(pipeline)) {
4614 if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.uses_prim_id ||
4615 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.uses_prim_id)
4616 break_wave_at_eoi = true;
4617 }
4618
4619 radeon_set_uconfig_reg(ctx_cs, R_03096C_GE_CNTL,
4620 S_03096C_PRIM_GRP_SIZE(primgroup_size) |
4621 S_03096C_VERT_GRP_SIZE(vertgroup_size) |
4622 S_03096C_PACKET_TO_ONE_PA(0) /* line stipple */ |
4623 S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi));
4624 }
4625
4626 static void
4627 radv_pipeline_generate_pm4(struct radv_pipeline *pipeline,
4628 const VkGraphicsPipelineCreateInfo *pCreateInfo,
4629 const struct radv_graphics_pipeline_create_info *extra,
4630 const struct radv_blend_state *blend,
4631 const struct radv_tessellation_state *tess,
4632 unsigned prim, unsigned gs_out)
4633 {
4634 struct radeon_cmdbuf *ctx_cs = &pipeline->ctx_cs;
4635 struct radeon_cmdbuf *cs = &pipeline->cs;
4636
4637 cs->max_dw = 64;
4638 ctx_cs->max_dw = 256;
4639 cs->buf = malloc(4 * (cs->max_dw + ctx_cs->max_dw));
4640 ctx_cs->buf = cs->buf + cs->max_dw;
4641
4642 radv_pipeline_generate_depth_stencil_state(ctx_cs, pipeline, pCreateInfo, extra);
4643 radv_pipeline_generate_blend_state(ctx_cs, pipeline, blend);
4644 radv_pipeline_generate_raster_state(ctx_cs, pipeline, pCreateInfo);
4645 radv_pipeline_generate_multisample_state(ctx_cs, pipeline);
4646 radv_pipeline_generate_vgt_gs_mode(ctx_cs, pipeline);
4647 radv_pipeline_generate_vertex_shader(ctx_cs, cs, pipeline, tess);
4648 radv_pipeline_generate_tess_shaders(ctx_cs, cs, pipeline, tess);
4649 radv_pipeline_generate_geometry_shader(ctx_cs, cs, pipeline);
4650 radv_pipeline_generate_fragment_shader(ctx_cs, cs, pipeline);
4651 radv_pipeline_generate_ps_inputs(ctx_cs, pipeline);
4652 radv_pipeline_generate_vgt_vertex_reuse(ctx_cs, pipeline);
4653 radv_pipeline_generate_binning_state(ctx_cs, pipeline, pCreateInfo, blend);
4654
4655 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10 && !radv_pipeline_has_ngg(pipeline))
4656 gfx10_pipeline_generate_ge_cntl(ctx_cs, pipeline, tess);
4657
4658 radeon_set_context_reg(ctx_cs, R_028B54_VGT_SHADER_STAGES_EN, radv_compute_vgt_shader_stages_en(pipeline));
4659
4660 if (pipeline->device->physical_device->rad_info.chip_class >= GFX7) {
4661 radeon_set_uconfig_reg_idx(pipeline->device->physical_device,
4662 cs, R_030908_VGT_PRIMITIVE_TYPE, 1, prim);
4663 } else {
4664 radeon_set_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE, prim);
4665 }
4666 radeon_set_context_reg(ctx_cs, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out);
4667
4668 radeon_set_context_reg(ctx_cs, R_02820C_PA_SC_CLIPRECT_RULE, radv_compute_cliprect_rule(pCreateInfo));
4669
4670 pipeline->ctx_cs_hash = _mesa_hash_data(ctx_cs->buf, ctx_cs->cdw * 4);
4671
4672 assert(ctx_cs->cdw <= ctx_cs->max_dw);
4673 assert(cs->cdw <= cs->max_dw);
4674 }
4675
4676 static struct radv_ia_multi_vgt_param_helpers
4677 radv_compute_ia_multi_vgt_param_helpers(struct radv_pipeline *pipeline,
4678 const struct radv_tessellation_state *tess,
4679 uint32_t prim)
4680 {
4681 struct radv_ia_multi_vgt_param_helpers ia_multi_vgt_param = {0};
4682 const struct radv_device *device = pipeline->device;
4683
4684 if (radv_pipeline_has_tess(pipeline))
4685 ia_multi_vgt_param.primgroup_size = tess->num_patches;
4686 else if (radv_pipeline_has_gs(pipeline))
4687 ia_multi_vgt_param.primgroup_size = 64;
4688 else
4689 ia_multi_vgt_param.primgroup_size = 128; /* recommended without a GS */
4690
4691 /* GS requirement. */
4692 ia_multi_vgt_param.partial_es_wave = false;
4693 if (radv_pipeline_has_gs(pipeline) && device->physical_device->rad_info.chip_class <= GFX8)
4694 if (SI_GS_PER_ES / ia_multi_vgt_param.primgroup_size >= pipeline->device->gs_table_depth - 3)
4695 ia_multi_vgt_param.partial_es_wave = true;
4696
4697 ia_multi_vgt_param.wd_switch_on_eop = false;
4698 if (device->physical_device->rad_info.chip_class >= GFX7) {
4699 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
4700 * 4 shader engines. Set 1 to pass the assertion below.
4701 * The other cases are hardware requirements. */
4702 if (device->physical_device->rad_info.max_se < 4 ||
4703 prim == V_008958_DI_PT_POLYGON ||
4704 prim == V_008958_DI_PT_LINELOOP ||
4705 prim == V_008958_DI_PT_TRIFAN ||
4706 prim == V_008958_DI_PT_TRISTRIP_ADJ ||
4707 (pipeline->graphics.prim_restart_enable &&
4708 (device->physical_device->rad_info.family < CHIP_POLARIS10 ||
4709 (prim != V_008958_DI_PT_POINTLIST &&
4710 prim != V_008958_DI_PT_LINESTRIP))))
4711 ia_multi_vgt_param.wd_switch_on_eop = true;
4712 }
4713
4714 ia_multi_vgt_param.ia_switch_on_eoi = false;
4715 if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.ps.prim_id_input)
4716 ia_multi_vgt_param.ia_switch_on_eoi = true;
4717 if (radv_pipeline_has_gs(pipeline) &&
4718 pipeline->shaders[MESA_SHADER_GEOMETRY]->info.uses_prim_id)
4719 ia_multi_vgt_param.ia_switch_on_eoi = true;
4720 if (radv_pipeline_has_tess(pipeline)) {
4721 /* SWITCH_ON_EOI must be set if PrimID is used. */
4722 if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.uses_prim_id ||
4723 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.uses_prim_id)
4724 ia_multi_vgt_param.ia_switch_on_eoi = true;
4725 }
4726
4727 ia_multi_vgt_param.partial_vs_wave = false;
4728 if (radv_pipeline_has_tess(pipeline)) {
4729 /* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
4730 if ((device->physical_device->rad_info.family == CHIP_TAHITI ||
4731 device->physical_device->rad_info.family == CHIP_PITCAIRN ||
4732 device->physical_device->rad_info.family == CHIP_BONAIRE) &&
4733 radv_pipeline_has_gs(pipeline))
4734 ia_multi_vgt_param.partial_vs_wave = true;
4735 /* Needed for 028B6C_DISTRIBUTION_MODE != 0 */
4736 if (device->physical_device->rad_info.has_distributed_tess) {
4737 if (radv_pipeline_has_gs(pipeline)) {
4738 if (device->physical_device->rad_info.chip_class <= GFX8)
4739 ia_multi_vgt_param.partial_es_wave = true;
4740 } else {
4741 ia_multi_vgt_param.partial_vs_wave = true;
4742 }
4743 }
4744 }
4745
4746 /* Workaround for a VGT hang when strip primitive types are used with
4747 * primitive restart.
4748 */
4749 if (pipeline->graphics.prim_restart_enable &&
4750 (prim == V_008958_DI_PT_LINESTRIP ||
4751 prim == V_008958_DI_PT_TRISTRIP ||
4752 prim == V_008958_DI_PT_LINESTRIP_ADJ ||
4753 prim == V_008958_DI_PT_TRISTRIP_ADJ)) {
4754 ia_multi_vgt_param.partial_vs_wave = true;
4755 }
4756
4757 if (radv_pipeline_has_gs(pipeline)) {
4758 /* On these chips there is the possibility of a hang if the
4759 * pipeline uses a GS and partial_vs_wave is not set.
4760 *
4761 * This mostly does not hit 4-SE chips, as those typically set
4762 * ia_switch_on_eoi and then partial_vs_wave is set for pipelines
4763 * with GS due to another workaround.
4764 *
4765 * Reproducer: https://bugs.freedesktop.org/show_bug.cgi?id=109242
4766 */
4767 if (device->physical_device->rad_info.family == CHIP_TONGA ||
4768 device->physical_device->rad_info.family == CHIP_FIJI ||
4769 device->physical_device->rad_info.family == CHIP_POLARIS10 ||
4770 device->physical_device->rad_info.family == CHIP_POLARIS11 ||
4771 device->physical_device->rad_info.family == CHIP_POLARIS12 ||
4772 device->physical_device->rad_info.family == CHIP_VEGAM) {
4773 ia_multi_vgt_param.partial_vs_wave = true;
4774 }
4775 }
4776
4777 ia_multi_vgt_param.base =
4778 S_028AA8_PRIMGROUP_SIZE(ia_multi_vgt_param.primgroup_size - 1) |
4779 /* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */
4780 S_028AA8_MAX_PRIMGRP_IN_WAVE(device->physical_device->rad_info.chip_class == GFX8 ? 2 : 0) |
4781 S_030960_EN_INST_OPT_BASIC(device->physical_device->rad_info.chip_class >= GFX9) |
4782 S_030960_EN_INST_OPT_ADV(device->physical_device->rad_info.chip_class >= GFX9);
4783
4784 return ia_multi_vgt_param;
4785 }
4786
4787
4788 static void
4789 radv_compute_vertex_input_state(struct radv_pipeline *pipeline,
4790 const VkGraphicsPipelineCreateInfo *pCreateInfo)
4791 {
4792 const VkPipelineVertexInputStateCreateInfo *vi_info =
4793 pCreateInfo->pVertexInputState;
4794 struct radv_vertex_elements_info *velems = &pipeline->vertex_elements;
4795
4796 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
4797 const VkVertexInputAttributeDescription *desc =
4798 &vi_info->pVertexAttributeDescriptions[i];
4799 unsigned loc = desc->location;
4800 const struct vk_format_description *format_desc;
4801
4802 format_desc = vk_format_description(desc->format);
4803
4804 velems->format_size[loc] = format_desc->block.bits / 8;
4805 }
4806
4807 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
4808 const VkVertexInputBindingDescription *desc =
4809 &vi_info->pVertexBindingDescriptions[i];
4810
4811 pipeline->binding_stride[desc->binding] = desc->stride;
4812 pipeline->num_vertex_bindings =
4813 MAX2(pipeline->num_vertex_bindings, desc->binding + 1);
4814 }
4815 }
4816
4817 static struct radv_shader_variant *
4818 radv_pipeline_get_streamout_shader(struct radv_pipeline *pipeline)
4819 {
4820 int i;
4821
4822 for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
4823 struct radv_shader_variant *shader =
4824 radv_get_shader(pipeline, i);
4825
4826 if (shader && shader->info.so.num_outputs > 0)
4827 return shader;
4828 }
4829
4830 return NULL;
4831 }
4832
4833 static VkResult
4834 radv_secure_compile(struct radv_pipeline *pipeline,
4835 struct radv_device *device,
4836 const struct radv_pipeline_key *key,
4837 const VkPipelineShaderStageCreateInfo **pStages,
4838 const VkPipelineCreateFlags flags,
4839 unsigned num_stages)
4840 {
4841 uint8_t allowed_pipeline_hashes[2][20];
4842 radv_hash_shaders(allowed_pipeline_hashes[0], pStages,
4843 pipeline->layout, key, get_hash_flags(device));
4844
4845 /* Generate the GC copy hash */
4846 memcpy(allowed_pipeline_hashes[1], allowed_pipeline_hashes[0], 20);
4847 allowed_pipeline_hashes[1][0] ^= 1;
4848
4849 uint8_t allowed_hashes[2][20];
4850 for (unsigned i = 0; i < 2; ++i) {
4851 disk_cache_compute_key(device->physical_device->disk_cache,
4852 allowed_pipeline_hashes[i], 20,
4853 allowed_hashes[i]);
4854 }
4855
4856 /* Do an early exit if all cache entries are already there. */
4857 bool may_need_copy_shader = pStages[MESA_SHADER_GEOMETRY];
4858 void *main_entry = disk_cache_get(device->physical_device->disk_cache, allowed_hashes[0], NULL);
4859 void *copy_entry = NULL;
4860 if (may_need_copy_shader)
4861 copy_entry = disk_cache_get(device->physical_device->disk_cache, allowed_hashes[1], NULL);
4862
4863 bool has_all_cache_entries = main_entry && (!may_need_copy_shader || copy_entry);
4864 free(main_entry);
4865 free(copy_entry);
4866
4867 if(has_all_cache_entries)
4868 return VK_SUCCESS;
4869
4870 unsigned process = 0;
4871 uint8_t sc_threads = device->instance->num_sc_threads;
4872 while (true) {
4873 mtx_lock(&device->sc_state->secure_compile_mutex);
4874 if (device->sc_state->secure_compile_thread_counter < sc_threads) {
4875 device->sc_state->secure_compile_thread_counter++;
4876 for (unsigned i = 0; i < sc_threads; i++) {
4877 if (!device->sc_state->secure_compile_processes[i].in_use) {
4878 device->sc_state->secure_compile_processes[i].in_use = true;
4879 process = i;
4880 break;
4881 }
4882 }
4883 mtx_unlock(&device->sc_state->secure_compile_mutex);
4884 break;
4885 }
4886 mtx_unlock(&device->sc_state->secure_compile_mutex);
4887 }
4888
4889 int fd_secure_input = device->sc_state->secure_compile_processes[process].fd_secure_input;
4890 int fd_secure_output = device->sc_state->secure_compile_processes[process].fd_secure_output;
4891
4892 /* Fork a copy of the slim untainted secure compile process */
4893 enum radv_secure_compile_type sc_type = RADV_SC_TYPE_FORK_DEVICE;
4894 write(fd_secure_input, &sc_type, sizeof(sc_type));
4895
4896 if (!radv_sc_read(fd_secure_output, &sc_type, sizeof(sc_type), true) ||
4897 sc_type != RADV_SC_TYPE_INIT_SUCCESS)
4898 return VK_ERROR_DEVICE_LOST;
4899
4900 fd_secure_input = device->sc_state->secure_compile_processes[process].fd_server;
4901 fd_secure_output = device->sc_state->secure_compile_processes[process].fd_client;
4902
4903 /* Write pipeline / shader module out to secure process via pipe */
4904 sc_type = RADV_SC_TYPE_COMPILE_PIPELINE;
4905 write(fd_secure_input, &sc_type, sizeof(sc_type));
4906
4907 /* Write pipeline layout out to secure process */
4908 struct radv_pipeline_layout *layout = pipeline->layout;
4909 write(fd_secure_input, layout, sizeof(struct radv_pipeline_layout));
4910 write(fd_secure_input, &layout->num_sets, sizeof(uint32_t));
4911 for (uint32_t set = 0; set < layout->num_sets; set++) {
4912 write(fd_secure_input, &layout->set[set].layout->layout_size, sizeof(uint32_t));
4913 write(fd_secure_input, layout->set[set].layout, layout->set[set].layout->layout_size);
4914 }
4915
4916 /* Write pipeline key out to secure process */
4917 write(fd_secure_input, key, sizeof(struct radv_pipeline_key));
4918
4919 /* Write pipeline create flags out to secure process */
4920 write(fd_secure_input, &flags, sizeof(VkPipelineCreateFlags));
4921
4922 /* Write stage and shader information out to secure process */
4923 write(fd_secure_input, &num_stages, sizeof(uint32_t));
4924 for (uint32_t i = 0; i < MESA_SHADER_STAGES; i++) {
4925 if (!pStages[i])
4926 continue;
4927
4928 /* Write stage out to secure process */
4929 gl_shader_stage stage = ffs(pStages[i]->stage) - 1;
4930 write(fd_secure_input, &stage, sizeof(gl_shader_stage));
4931
4932 /* Write entry point name out to secure process */
4933 size_t name_size = strlen(pStages[i]->pName) + 1;
4934 write(fd_secure_input, &name_size, sizeof(size_t));
4935 write(fd_secure_input, pStages[i]->pName, name_size);
4936
4937 /* Write shader module out to secure process */
4938 struct radv_shader_module *module = radv_shader_module_from_handle(pStages[i]->module);
4939 assert(!module->nir);
4940 size_t module_size = sizeof(struct radv_shader_module) + module->size;
4941 write(fd_secure_input, &module_size, sizeof(size_t));
4942 write(fd_secure_input, module, module_size);
4943
4944 /* Write specialization info out to secure process */
4945 const VkSpecializationInfo *specInfo = pStages[i]->pSpecializationInfo;
4946 bool has_spec_info = specInfo ? true : false;
4947 write(fd_secure_input, &has_spec_info, sizeof(bool));
4948 if (specInfo) {
4949 write(fd_secure_input, &specInfo->dataSize, sizeof(size_t));
4950 write(fd_secure_input, specInfo->pData, specInfo->dataSize);
4951
4952 write(fd_secure_input, &specInfo->mapEntryCount, sizeof(uint32_t));
4953 for (uint32_t j = 0; j < specInfo->mapEntryCount; j++)
4954 write(fd_secure_input, &specInfo->pMapEntries[j], sizeof(VkSpecializationMapEntry));
4955 }
4956 }
4957
4958 /* Read the data returned from the secure process */
4959 while (sc_type != RADV_SC_TYPE_COMPILE_PIPELINE_FINISHED) {
4960 if (!radv_sc_read(fd_secure_output, &sc_type, sizeof(sc_type), true))
4961 return VK_ERROR_DEVICE_LOST;
4962
4963 if (sc_type == RADV_SC_TYPE_WRITE_DISK_CACHE) {
4964 assert(device->physical_device->disk_cache);
4965
4966 uint8_t disk_sha1[20];
4967 if (!radv_sc_read(fd_secure_output, disk_sha1, sizeof(uint8_t) * 20, true))
4968 return VK_ERROR_DEVICE_LOST;
4969
4970 if (memcmp(disk_sha1, allowed_hashes[0], 20) &&
4971 memcmp(disk_sha1, allowed_hashes[1], 20))
4972 return VK_ERROR_DEVICE_LOST;
4973
4974 uint32_t entry_size;
4975 if (!radv_sc_read(fd_secure_output, &entry_size, sizeof(uint32_t), true))
4976 return VK_ERROR_DEVICE_LOST;
4977
4978 struct cache_entry *entry = malloc(entry_size);
4979 if (!radv_sc_read(fd_secure_output, entry, entry_size, true))
4980 return VK_ERROR_DEVICE_LOST;
4981
4982 disk_cache_put(device->physical_device->disk_cache,
4983 disk_sha1, entry, entry_size,
4984 NULL);
4985
4986 free(entry);
4987 } else if (sc_type == RADV_SC_TYPE_READ_DISK_CACHE) {
4988 uint8_t disk_sha1[20];
4989 if (!radv_sc_read(fd_secure_output, disk_sha1, sizeof(uint8_t) * 20, true))
4990 return VK_ERROR_DEVICE_LOST;
4991
4992 if (memcmp(disk_sha1, allowed_hashes[0], 20) &&
4993 memcmp(disk_sha1, allowed_hashes[1], 20))
4994 return VK_ERROR_DEVICE_LOST;
4995
4996 size_t size;
4997 struct cache_entry *entry = (struct cache_entry *)
4998 disk_cache_get(device->physical_device->disk_cache,
4999 disk_sha1, &size);
5000
5001 uint8_t found = entry ? 1 : 0;
5002 write(fd_secure_input, &found, sizeof(uint8_t));
5003
5004 if (found) {
5005 write(fd_secure_input, &size, sizeof(size_t));
5006 write(fd_secure_input, entry, size);
5007 }
5008
5009 free(entry);
5010 }
5011 }
5012
5013 sc_type = RADV_SC_TYPE_DESTROY_DEVICE;
5014 write(fd_secure_input, &sc_type, sizeof(sc_type));
5015
5016 mtx_lock(&device->sc_state->secure_compile_mutex);
5017 device->sc_state->secure_compile_thread_counter--;
5018 device->sc_state->secure_compile_processes[process].in_use = false;
5019 mtx_unlock(&device->sc_state->secure_compile_mutex);
5020
5021 return VK_SUCCESS;
5022 }
5023
5024 static VkResult
5025 radv_pipeline_init(struct radv_pipeline *pipeline,
5026 struct radv_device *device,
5027 struct radv_pipeline_cache *cache,
5028 const VkGraphicsPipelineCreateInfo *pCreateInfo,
5029 const struct radv_graphics_pipeline_create_info *extra)
5030 {
5031 VkResult result;
5032 bool has_view_index = false;
5033
5034 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
5035 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
5036 if (subpass->view_mask)
5037 has_view_index = true;
5038
5039 pipeline->device = device;
5040 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
5041 assert(pipeline->layout);
5042
5043 struct radv_blend_state blend = radv_pipeline_init_blend_state(pipeline, pCreateInfo, extra);
5044
5045 const VkPipelineCreationFeedbackCreateInfoEXT *creation_feedback =
5046 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
5047 radv_init_feedback(creation_feedback);
5048
5049 VkPipelineCreationFeedbackEXT *pipeline_feedback = creation_feedback ? creation_feedback->pPipelineCreationFeedback : NULL;
5050
5051 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
5052 VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = { 0 };
5053 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
5054 gl_shader_stage stage = ffs(pCreateInfo->pStages[i].stage) - 1;
5055 pStages[stage] = &pCreateInfo->pStages[i];
5056 if(creation_feedback)
5057 stage_feedbacks[stage] = &creation_feedback->pPipelineStageCreationFeedbacks[i];
5058 }
5059
5060 struct radv_pipeline_key key = radv_generate_graphics_pipeline_key(pipeline, pCreateInfo, &blend, has_view_index);
5061 if (radv_device_use_secure_compile(device->instance)) {
5062 return radv_secure_compile(pipeline, device, &key, pStages, pCreateInfo->flags, pCreateInfo->stageCount);
5063 } else {
5064 result = radv_create_shaders(pipeline, device, cache, &key, pStages,
5065 pCreateInfo->flags, pipeline_feedback,
5066 stage_feedbacks);
5067 if (result != VK_SUCCESS)
5068 return result;
5069 }
5070
5071 pipeline->graphics.spi_baryc_cntl = S_0286E0_FRONT_FACE_ALL_BITS(1);
5072 radv_pipeline_init_multisample_state(pipeline, &blend, pCreateInfo);
5073 uint32_t gs_out;
5074 uint32_t prim = si_translate_prim(pCreateInfo->pInputAssemblyState->topology);
5075
5076 pipeline->graphics.topology = pCreateInfo->pInputAssemblyState->topology;
5077 pipeline->graphics.can_use_guardband = radv_prim_can_use_guardband(pCreateInfo->pInputAssemblyState->topology);
5078
5079 if (radv_pipeline_has_gs(pipeline)) {
5080 gs_out = si_conv_gl_prim_to_gs_out(pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs.output_prim);
5081 pipeline->graphics.can_use_guardband = gs_out == V_028A6C_OUTPRIM_TYPE_TRISTRIP;
5082 } else if (radv_pipeline_has_tess(pipeline)) {
5083 if (pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.point_mode)
5084 gs_out = V_028A6C_OUTPRIM_TYPE_POINTLIST;
5085 else
5086 gs_out = si_conv_gl_prim_to_gs_out(pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.primitive_mode);
5087 pipeline->graphics.can_use_guardband = gs_out == V_028A6C_OUTPRIM_TYPE_TRISTRIP;
5088 } else {
5089 gs_out = si_conv_prim_to_gs_out(pCreateInfo->pInputAssemblyState->topology);
5090 }
5091 if (extra && extra->use_rectlist) {
5092 prim = V_008958_DI_PT_RECTLIST;
5093 gs_out = V_028A6C_OUTPRIM_TYPE_TRISTRIP;
5094 pipeline->graphics.can_use_guardband = true;
5095 if (radv_pipeline_has_ngg(pipeline))
5096 gs_out = V_028A6C_VGT_OUT_RECT_V0;
5097 }
5098 pipeline->graphics.prim_restart_enable = !!pCreateInfo->pInputAssemblyState->primitiveRestartEnable;
5099 /* prim vertex count will need TESS changes */
5100 pipeline->graphics.prim_vertex_count = prim_size_table[prim];
5101
5102 radv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
5103
5104 /* Ensure that some export memory is always allocated, for two reasons:
5105 *
5106 * 1) Correctness: The hardware ignores the EXEC mask if no export
5107 * memory is allocated, so KILL and alpha test do not work correctly
5108 * without this.
5109 * 2) Performance: Every shader needs at least a NULL export, even when
5110 * it writes no color/depth output. The NULL export instruction
5111 * stalls without this setting.
5112 *
5113 * Don't add this to CB_SHADER_MASK.
5114 *
5115 * GFX10 supports pixel shaders without exports by setting both the
5116 * color and Z formats to SPI_SHADER_ZERO. The hw will skip export
5117 * instructions if any are present.
5118 */
5119 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
5120 if ((pipeline->device->physical_device->rad_info.chip_class <= GFX9 ||
5121 ps->info.ps.can_discard) &&
5122 !blend.spi_shader_col_format) {
5123 if (!ps->info.ps.writes_z &&
5124 !ps->info.ps.writes_stencil &&
5125 !ps->info.ps.writes_sample_mask)
5126 blend.spi_shader_col_format = V_028714_SPI_SHADER_32_R;
5127 }
5128
5129 blend.cb_shader_mask = ps->info.ps.cb_shader_mask;
5130 if (blend.mrt0_is_dual_src) {
5131 blend.cb_shader_mask |= (blend.cb_shader_mask & 0xf) << 4;
5132 }
5133
5134 if (extra &&
5135 (extra->custom_blend_mode == V_028808_CB_ELIMINATE_FAST_CLEAR ||
5136 extra->custom_blend_mode == V_028808_CB_FMASK_DECOMPRESS ||
5137 extra->custom_blend_mode == V_028808_CB_DCC_DECOMPRESS ||
5138 extra->custom_blend_mode == V_028808_CB_RESOLVE)) {
5139 /* According to the CB spec states, CB_SHADER_MASK should be
5140 * set to enable writes to all four channels of MRT0.
5141 */
5142 blend.cb_shader_mask = 0xf;
5143 }
5144
5145 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
5146 if (pipeline->shaders[i]) {
5147 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[i]->info.need_indirect_descriptor_sets;
5148 }
5149 }
5150
5151 if (radv_pipeline_has_gs(pipeline) && !radv_pipeline_has_ngg(pipeline)) {
5152 struct radv_shader_variant *gs =
5153 pipeline->shaders[MESA_SHADER_GEOMETRY];
5154
5155 calculate_gs_ring_sizes(pipeline, &gs->info.gs_ring_info);
5156 }
5157
5158 struct radv_tessellation_state tess = {0};
5159 if (radv_pipeline_has_tess(pipeline)) {
5160 if (prim == V_008958_DI_PT_PATCH) {
5161 pipeline->graphics.prim_vertex_count.min = pCreateInfo->pTessellationState->patchControlPoints;
5162 pipeline->graphics.prim_vertex_count.incr = 1;
5163 }
5164 tess = calculate_tess_state(pipeline, pCreateInfo);
5165 }
5166
5167 pipeline->graphics.ia_multi_vgt_param = radv_compute_ia_multi_vgt_param_helpers(pipeline, &tess, prim);
5168
5169 radv_compute_vertex_input_state(pipeline, pCreateInfo);
5170
5171 for (uint32_t i = 0; i < MESA_SHADER_STAGES; i++)
5172 pipeline->user_data_0[i] = radv_pipeline_stage_to_user_data_0(pipeline, i, device->physical_device->rad_info.chip_class);
5173
5174 struct radv_userdata_info *loc = radv_lookup_user_sgpr(pipeline, MESA_SHADER_VERTEX,
5175 AC_UD_VS_BASE_VERTEX_START_INSTANCE);
5176 if (loc->sgpr_idx != -1) {
5177 pipeline->graphics.vtx_base_sgpr = pipeline->user_data_0[MESA_SHADER_VERTEX];
5178 pipeline->graphics.vtx_base_sgpr += loc->sgpr_idx * 4;
5179 if (radv_get_shader(pipeline, MESA_SHADER_VERTEX)->info.vs.needs_draw_id)
5180 pipeline->graphics.vtx_emit_num = 3;
5181 else
5182 pipeline->graphics.vtx_emit_num = 2;
5183 }
5184
5185 /* Find the last vertex shader stage that eventually uses streamout. */
5186 pipeline->streamout_shader = radv_pipeline_get_streamout_shader(pipeline);
5187
5188 result = radv_pipeline_scratch_init(device, pipeline);
5189 radv_pipeline_generate_pm4(pipeline, pCreateInfo, extra, &blend, &tess, prim, gs_out);
5190
5191 return result;
5192 }
5193
5194 VkResult
5195 radv_graphics_pipeline_create(
5196 VkDevice _device,
5197 VkPipelineCache _cache,
5198 const VkGraphicsPipelineCreateInfo *pCreateInfo,
5199 const struct radv_graphics_pipeline_create_info *extra,
5200 const VkAllocationCallbacks *pAllocator,
5201 VkPipeline *pPipeline)
5202 {
5203 RADV_FROM_HANDLE(radv_device, device, _device);
5204 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
5205 struct radv_pipeline *pipeline;
5206 VkResult result;
5207
5208 pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
5209 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
5210 if (pipeline == NULL)
5211 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
5212
5213 vk_object_base_init(&device->vk, &pipeline->base,
5214 VK_OBJECT_TYPE_PIPELINE);
5215
5216 result = radv_pipeline_init(pipeline, device, cache,
5217 pCreateInfo, extra);
5218 if (result != VK_SUCCESS) {
5219 radv_pipeline_destroy(device, pipeline, pAllocator);
5220 return result;
5221 }
5222
5223 *pPipeline = radv_pipeline_to_handle(pipeline);
5224
5225 return VK_SUCCESS;
5226 }
5227
5228 VkResult radv_CreateGraphicsPipelines(
5229 VkDevice _device,
5230 VkPipelineCache pipelineCache,
5231 uint32_t count,
5232 const VkGraphicsPipelineCreateInfo* pCreateInfos,
5233 const VkAllocationCallbacks* pAllocator,
5234 VkPipeline* pPipelines)
5235 {
5236 VkResult result = VK_SUCCESS;
5237 unsigned i = 0;
5238
5239 for (; i < count; i++) {
5240 VkResult r;
5241 r = radv_graphics_pipeline_create(_device,
5242 pipelineCache,
5243 &pCreateInfos[i],
5244 NULL, pAllocator, &pPipelines[i]);
5245 if (r != VK_SUCCESS) {
5246 result = r;
5247 pPipelines[i] = VK_NULL_HANDLE;
5248
5249 if (pCreateInfos[i].flags & VK_PIPELINE_CREATE_EARLY_RETURN_ON_FAILURE_BIT_EXT)
5250 break;
5251 }
5252 }
5253
5254 for (; i < count; ++i)
5255 pPipelines[i] = VK_NULL_HANDLE;
5256
5257 return result;
5258 }
5259
5260
5261 static void
5262 radv_compute_generate_pm4(struct radv_pipeline *pipeline)
5263 {
5264 struct radv_shader_variant *compute_shader;
5265 struct radv_device *device = pipeline->device;
5266 unsigned threads_per_threadgroup;
5267 unsigned threadgroups_per_cu = 1;
5268 unsigned waves_per_threadgroup;
5269 unsigned max_waves_per_sh = 0;
5270 uint64_t va;
5271
5272 pipeline->cs.max_dw = device->physical_device->rad_info.chip_class >= GFX10 ? 22 : 20;
5273 pipeline->cs.buf = malloc(pipeline->cs.max_dw * 4);
5274
5275 compute_shader = pipeline->shaders[MESA_SHADER_COMPUTE];
5276 va = radv_buffer_get_va(compute_shader->bo) + compute_shader->bo_offset;
5277
5278 radeon_set_sh_reg_seq(&pipeline->cs, R_00B830_COMPUTE_PGM_LO, 2);
5279 radeon_emit(&pipeline->cs, va >> 8);
5280 radeon_emit(&pipeline->cs, S_00B834_DATA(va >> 40));
5281
5282 radeon_set_sh_reg_seq(&pipeline->cs, R_00B848_COMPUTE_PGM_RSRC1, 2);
5283 radeon_emit(&pipeline->cs, compute_shader->config.rsrc1);
5284 radeon_emit(&pipeline->cs, compute_shader->config.rsrc2);
5285 if (device->physical_device->rad_info.chip_class >= GFX10) {
5286 radeon_set_sh_reg(&pipeline->cs, R_00B8A0_COMPUTE_PGM_RSRC3, compute_shader->config.rsrc3);
5287 }
5288
5289 /* Calculate best compute resource limits. */
5290 threads_per_threadgroup = compute_shader->info.cs.block_size[0] *
5291 compute_shader->info.cs.block_size[1] *
5292 compute_shader->info.cs.block_size[2];
5293 waves_per_threadgroup = DIV_ROUND_UP(threads_per_threadgroup,
5294 compute_shader->info.wave_size);
5295
5296 if (device->physical_device->rad_info.chip_class >= GFX10 &&
5297 waves_per_threadgroup == 1)
5298 threadgroups_per_cu = 2;
5299
5300 radeon_set_sh_reg(&pipeline->cs, R_00B854_COMPUTE_RESOURCE_LIMITS,
5301 ac_get_compute_resource_limits(&device->physical_device->rad_info,
5302 waves_per_threadgroup,
5303 max_waves_per_sh,
5304 threadgroups_per_cu));
5305
5306 radeon_set_sh_reg_seq(&pipeline->cs, R_00B81C_COMPUTE_NUM_THREAD_X, 3);
5307 radeon_emit(&pipeline->cs,
5308 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[0]));
5309 radeon_emit(&pipeline->cs,
5310 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[1]));
5311 radeon_emit(&pipeline->cs,
5312 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[2]));
5313
5314 assert(pipeline->cs.cdw <= pipeline->cs.max_dw);
5315 }
5316
5317 static struct radv_pipeline_key
5318 radv_generate_compute_pipeline_key(struct radv_pipeline *pipeline,
5319 const VkComputePipelineCreateInfo *pCreateInfo)
5320 {
5321 const VkPipelineShaderStageCreateInfo *stage = &pCreateInfo->stage;
5322 struct radv_pipeline_key key;
5323 memset(&key, 0, sizeof(key));
5324
5325 if (pCreateInfo->flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT)
5326 key.optimisations_disabled = 1;
5327
5328 const VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT *subgroup_size =
5329 vk_find_struct_const(stage->pNext,
5330 PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO_EXT);
5331
5332 if (subgroup_size) {
5333 assert(subgroup_size->requiredSubgroupSize == 32 ||
5334 subgroup_size->requiredSubgroupSize == 64);
5335 key.compute_subgroup_size = subgroup_size->requiredSubgroupSize;
5336 }
5337
5338 return key;
5339 }
5340
5341 static VkResult radv_compute_pipeline_create(
5342 VkDevice _device,
5343 VkPipelineCache _cache,
5344 const VkComputePipelineCreateInfo* pCreateInfo,
5345 const VkAllocationCallbacks* pAllocator,
5346 VkPipeline* pPipeline)
5347 {
5348 RADV_FROM_HANDLE(radv_device, device, _device);
5349 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
5350 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
5351 VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = { 0 };
5352 struct radv_pipeline *pipeline;
5353 VkResult result;
5354
5355 pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
5356 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
5357 if (pipeline == NULL)
5358 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
5359
5360 vk_object_base_init(&device->vk, &pipeline->base,
5361 VK_OBJECT_TYPE_PIPELINE);
5362
5363 pipeline->device = device;
5364 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
5365 assert(pipeline->layout);
5366
5367 const VkPipelineCreationFeedbackCreateInfoEXT *creation_feedback =
5368 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
5369 radv_init_feedback(creation_feedback);
5370
5371 VkPipelineCreationFeedbackEXT *pipeline_feedback = creation_feedback ? creation_feedback->pPipelineCreationFeedback : NULL;
5372 if (creation_feedback)
5373 stage_feedbacks[MESA_SHADER_COMPUTE] = &creation_feedback->pPipelineStageCreationFeedbacks[0];
5374
5375 pStages[MESA_SHADER_COMPUTE] = &pCreateInfo->stage;
5376
5377 struct radv_pipeline_key key =
5378 radv_generate_compute_pipeline_key(pipeline, pCreateInfo);
5379
5380 if (radv_device_use_secure_compile(device->instance)) {
5381 result = radv_secure_compile(pipeline, device, &key, pStages, pCreateInfo->flags, 1);
5382 *pPipeline = radv_pipeline_to_handle(pipeline);
5383
5384 return result;
5385 } else {
5386 result = radv_create_shaders(pipeline, device, cache, &key, pStages,
5387 pCreateInfo->flags, pipeline_feedback,
5388 stage_feedbacks);
5389 if (result != VK_SUCCESS) {
5390 radv_pipeline_destroy(device, pipeline, pAllocator);
5391 return result;
5392 }
5393 }
5394
5395 pipeline->user_data_0[MESA_SHADER_COMPUTE] = radv_pipeline_stage_to_user_data_0(pipeline, MESA_SHADER_COMPUTE, device->physical_device->rad_info.chip_class);
5396 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[MESA_SHADER_COMPUTE]->info.need_indirect_descriptor_sets;
5397 result = radv_pipeline_scratch_init(device, pipeline);
5398 if (result != VK_SUCCESS) {
5399 radv_pipeline_destroy(device, pipeline, pAllocator);
5400 return result;
5401 }
5402
5403 radv_compute_generate_pm4(pipeline);
5404
5405 *pPipeline = radv_pipeline_to_handle(pipeline);
5406
5407 return VK_SUCCESS;
5408 }
5409
5410 VkResult radv_CreateComputePipelines(
5411 VkDevice _device,
5412 VkPipelineCache pipelineCache,
5413 uint32_t count,
5414 const VkComputePipelineCreateInfo* pCreateInfos,
5415 const VkAllocationCallbacks* pAllocator,
5416 VkPipeline* pPipelines)
5417 {
5418 VkResult result = VK_SUCCESS;
5419
5420 unsigned i = 0;
5421 for (; i < count; i++) {
5422 VkResult r;
5423 r = radv_compute_pipeline_create(_device, pipelineCache,
5424 &pCreateInfos[i],
5425 pAllocator, &pPipelines[i]);
5426 if (r != VK_SUCCESS) {
5427 result = r;
5428 pPipelines[i] = VK_NULL_HANDLE;
5429
5430 if (pCreateInfos[i].flags & VK_PIPELINE_CREATE_EARLY_RETURN_ON_FAILURE_BIT_EXT)
5431 break;
5432 }
5433 }
5434
5435 for (; i < count; ++i)
5436 pPipelines[i] = VK_NULL_HANDLE;
5437
5438 return result;
5439 }
5440
5441
5442 static uint32_t radv_get_executable_count(const struct radv_pipeline *pipeline)
5443 {
5444 uint32_t ret = 0;
5445 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
5446 if (!pipeline->shaders[i])
5447 continue;
5448
5449 if (i == MESA_SHADER_GEOMETRY &&
5450 !radv_pipeline_has_ngg(pipeline)) {
5451 ret += 2u;
5452 } else {
5453 ret += 1u;
5454 }
5455
5456 }
5457 return ret;
5458 }
5459
5460 static struct radv_shader_variant *
5461 radv_get_shader_from_executable_index(const struct radv_pipeline *pipeline, int index, gl_shader_stage *stage)
5462 {
5463 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
5464 if (!pipeline->shaders[i])
5465 continue;
5466 if (!index) {
5467 *stage = i;
5468 return pipeline->shaders[i];
5469 }
5470
5471 --index;
5472
5473 if (i == MESA_SHADER_GEOMETRY &&
5474 !radv_pipeline_has_ngg(pipeline)) {
5475 if (!index) {
5476 *stage = i;
5477 return pipeline->gs_copy_shader;
5478 }
5479 --index;
5480 }
5481 }
5482
5483 *stage = -1;
5484 return NULL;
5485 }
5486
5487 /* Basically strlcpy (which does not exist on linux) specialized for
5488 * descriptions. */
5489 static void desc_copy(char *desc, const char *src) {
5490 int len = strlen(src);
5491 assert(len < VK_MAX_DESCRIPTION_SIZE);
5492 memcpy(desc, src, len);
5493 memset(desc + len, 0, VK_MAX_DESCRIPTION_SIZE - len);
5494 }
5495
5496 VkResult radv_GetPipelineExecutablePropertiesKHR(
5497 VkDevice _device,
5498 const VkPipelineInfoKHR* pPipelineInfo,
5499 uint32_t* pExecutableCount,
5500 VkPipelineExecutablePropertiesKHR* pProperties)
5501 {
5502 RADV_FROM_HANDLE(radv_pipeline, pipeline, pPipelineInfo->pipeline);
5503 const uint32_t total_count = radv_get_executable_count(pipeline);
5504
5505 if (!pProperties) {
5506 *pExecutableCount = total_count;
5507 return VK_SUCCESS;
5508 }
5509
5510 const uint32_t count = MIN2(total_count, *pExecutableCount);
5511 for (unsigned i = 0, executable_idx = 0;
5512 i < MESA_SHADER_STAGES && executable_idx < count; ++i) {
5513 if (!pipeline->shaders[i])
5514 continue;
5515 pProperties[executable_idx].stages = mesa_to_vk_shader_stage(i);
5516 const char *name = NULL;
5517 const char *description = NULL;
5518 switch(i) {
5519 case MESA_SHADER_VERTEX:
5520 name = "Vertex Shader";
5521 description = "Vulkan Vertex Shader";
5522 break;
5523 case MESA_SHADER_TESS_CTRL:
5524 if (!pipeline->shaders[MESA_SHADER_VERTEX]) {
5525 pProperties[executable_idx].stages |= VK_SHADER_STAGE_VERTEX_BIT;
5526 name = "Vertex + Tessellation Control Shaders";
5527 description = "Combined Vulkan Vertex and Tessellation Control Shaders";
5528 } else {
5529 name = "Tessellation Control Shader";
5530 description = "Vulkan Tessellation Control Shader";
5531 }
5532 break;
5533 case MESA_SHADER_TESS_EVAL:
5534 name = "Tessellation Evaluation Shader";
5535 description = "Vulkan Tessellation Evaluation Shader";
5536 break;
5537 case MESA_SHADER_GEOMETRY:
5538 if (radv_pipeline_has_tess(pipeline) && !pipeline->shaders[MESA_SHADER_TESS_EVAL]) {
5539 pProperties[executable_idx].stages |= VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
5540 name = "Tessellation Evaluation + Geometry Shaders";
5541 description = "Combined Vulkan Tessellation Evaluation and Geometry Shaders";
5542 } else if (!radv_pipeline_has_tess(pipeline) && !pipeline->shaders[MESA_SHADER_VERTEX]) {
5543 pProperties[executable_idx].stages |= VK_SHADER_STAGE_VERTEX_BIT;
5544 name = "Vertex + Geometry Shader";
5545 description = "Combined Vulkan Vertex and Geometry Shaders";
5546 } else {
5547 name = "Geometry Shader";
5548 description = "Vulkan Geometry Shader";
5549 }
5550 break;
5551 case MESA_SHADER_FRAGMENT:
5552 name = "Fragment Shader";
5553 description = "Vulkan Fragment Shader";
5554 break;
5555 case MESA_SHADER_COMPUTE:
5556 name = "Compute Shader";
5557 description = "Vulkan Compute Shader";
5558 break;
5559 }
5560
5561 pProperties[executable_idx].subgroupSize = pipeline->shaders[i]->info.wave_size;
5562 desc_copy(pProperties[executable_idx].name, name);
5563 desc_copy(pProperties[executable_idx].description, description);
5564
5565 ++executable_idx;
5566 if (i == MESA_SHADER_GEOMETRY &&
5567 !radv_pipeline_has_ngg(pipeline)) {
5568 assert(pipeline->gs_copy_shader);
5569 if (executable_idx >= count)
5570 break;
5571
5572 pProperties[executable_idx].stages = VK_SHADER_STAGE_GEOMETRY_BIT;
5573 pProperties[executable_idx].subgroupSize = 64;
5574 desc_copy(pProperties[executable_idx].name, "GS Copy Shader");
5575 desc_copy(pProperties[executable_idx].description,
5576 "Extra shader stage that loads the GS output ringbuffer into the rasterizer");
5577
5578 ++executable_idx;
5579 }
5580 }
5581
5582 VkResult result = *pExecutableCount < total_count ? VK_INCOMPLETE : VK_SUCCESS;
5583 *pExecutableCount = count;
5584 return result;
5585 }
5586
5587 VkResult radv_GetPipelineExecutableStatisticsKHR(
5588 VkDevice _device,
5589 const VkPipelineExecutableInfoKHR* pExecutableInfo,
5590 uint32_t* pStatisticCount,
5591 VkPipelineExecutableStatisticKHR* pStatistics)
5592 {
5593 RADV_FROM_HANDLE(radv_device, device, _device);
5594 RADV_FROM_HANDLE(radv_pipeline, pipeline, pExecutableInfo->pipeline);
5595 gl_shader_stage stage;
5596 struct radv_shader_variant *shader = radv_get_shader_from_executable_index(pipeline, pExecutableInfo->executableIndex, &stage);
5597
5598 enum chip_class chip_class = device->physical_device->rad_info.chip_class;
5599 unsigned lds_increment = chip_class >= GFX7 ? 512 : 256;
5600 unsigned max_waves = radv_get_max_waves(device, shader, stage);
5601
5602 VkPipelineExecutableStatisticKHR *s = pStatistics;
5603 VkPipelineExecutableStatisticKHR *end = s + (pStatistics ? *pStatisticCount : 0);
5604 VkResult result = VK_SUCCESS;
5605
5606 if (s < end) {
5607 desc_copy(s->name, "SGPRs");
5608 desc_copy(s->description, "Number of SGPR registers allocated per subgroup");
5609 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5610 s->value.u64 = shader->config.num_sgprs;
5611 }
5612 ++s;
5613
5614 if (s < end) {
5615 desc_copy(s->name, "VGPRs");
5616 desc_copy(s->description, "Number of VGPR registers allocated per subgroup");
5617 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5618 s->value.u64 = shader->config.num_vgprs;
5619 }
5620 ++s;
5621
5622 if (s < end) {
5623 desc_copy(s->name, "Spilled SGPRs");
5624 desc_copy(s->description, "Number of SGPR registers spilled per subgroup");
5625 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5626 s->value.u64 = shader->config.spilled_sgprs;
5627 }
5628 ++s;
5629
5630 if (s < end) {
5631 desc_copy(s->name, "Spilled VGPRs");
5632 desc_copy(s->description, "Number of VGPR registers spilled per subgroup");
5633 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5634 s->value.u64 = shader->config.spilled_vgprs;
5635 }
5636 ++s;
5637
5638 if (s < end) {
5639 desc_copy(s->name, "PrivMem VGPRs");
5640 desc_copy(s->description, "Number of VGPRs stored in private memory per subgroup");
5641 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5642 s->value.u64 = shader->info.private_mem_vgprs;
5643 }
5644 ++s;
5645
5646 if (s < end) {
5647 desc_copy(s->name, "Code size");
5648 desc_copy(s->description, "Code size in bytes");
5649 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5650 s->value.u64 = shader->exec_size;
5651 }
5652 ++s;
5653
5654 if (s < end) {
5655 desc_copy(s->name, "LDS size");
5656 desc_copy(s->description, "LDS size in bytes per workgroup");
5657 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5658 s->value.u64 = shader->config.lds_size * lds_increment;
5659 }
5660 ++s;
5661
5662 if (s < end) {
5663 desc_copy(s->name, "Scratch size");
5664 desc_copy(s->description, "Private memory in bytes per subgroup");
5665 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5666 s->value.u64 = shader->config.scratch_bytes_per_wave;
5667 }
5668 ++s;
5669
5670 if (s < end) {
5671 desc_copy(s->name, "Subgroups per SIMD");
5672 desc_copy(s->description, "The maximum number of subgroups in flight on a SIMD unit");
5673 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5674 s->value.u64 = max_waves;
5675 }
5676 ++s;
5677
5678 if (shader->statistics) {
5679 for (unsigned i = 0; i < shader->statistics->count; i++) {
5680 struct radv_compiler_statistic_info *info = &shader->statistics->infos[i];
5681 uint32_t value = shader->statistics->values[i];
5682 if (s < end) {
5683 desc_copy(s->name, info->name);
5684 desc_copy(s->description, info->desc);
5685 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5686 s->value.u64 = value;
5687 }
5688 ++s;
5689 }
5690 }
5691
5692 if (!pStatistics)
5693 *pStatisticCount = s - pStatistics;
5694 else if (s > end) {
5695 *pStatisticCount = end - pStatistics;
5696 result = VK_INCOMPLETE;
5697 } else {
5698 *pStatisticCount = s - pStatistics;
5699 }
5700
5701 return result;
5702 }
5703
5704 static VkResult radv_copy_representation(void *data, size_t *data_size, const char *src)
5705 {
5706 size_t total_size = strlen(src) + 1;
5707
5708 if (!data) {
5709 *data_size = total_size;
5710 return VK_SUCCESS;
5711 }
5712
5713 size_t size = MIN2(total_size, *data_size);
5714
5715 memcpy(data, src, size);
5716 if (size)
5717 *((char*)data + size - 1) = 0;
5718 return size < total_size ? VK_INCOMPLETE : VK_SUCCESS;
5719 }
5720
5721 VkResult radv_GetPipelineExecutableInternalRepresentationsKHR(
5722 VkDevice device,
5723 const VkPipelineExecutableInfoKHR* pExecutableInfo,
5724 uint32_t* pInternalRepresentationCount,
5725 VkPipelineExecutableInternalRepresentationKHR* pInternalRepresentations)
5726 {
5727 RADV_FROM_HANDLE(radv_pipeline, pipeline, pExecutableInfo->pipeline);
5728 gl_shader_stage stage;
5729 struct radv_shader_variant *shader = radv_get_shader_from_executable_index(pipeline, pExecutableInfo->executableIndex, &stage);
5730
5731 VkPipelineExecutableInternalRepresentationKHR *p = pInternalRepresentations;
5732 VkPipelineExecutableInternalRepresentationKHR *end = p + (pInternalRepresentations ? *pInternalRepresentationCount : 0);
5733 VkResult result = VK_SUCCESS;
5734 /* optimized NIR */
5735 if (p < end) {
5736 p->isText = true;
5737 desc_copy(p->name, "NIR Shader(s)");
5738 desc_copy(p->description, "The optimized NIR shader(s)");
5739 if (radv_copy_representation(p->pData, &p->dataSize, shader->nir_string) != VK_SUCCESS)
5740 result = VK_INCOMPLETE;
5741 }
5742 ++p;
5743
5744 /* backend IR */
5745 if (p < end) {
5746 p->isText = true;
5747 if (pipeline->device->physical_device->use_aco) {
5748 desc_copy(p->name, "ACO IR");
5749 desc_copy(p->description, "The ACO IR after some optimizations");
5750 } else {
5751 desc_copy(p->name, "LLVM IR");
5752 desc_copy(p->description, "The LLVM IR after some optimizations");
5753 }
5754 if (radv_copy_representation(p->pData, &p->dataSize, shader->ir_string) != VK_SUCCESS)
5755 result = VK_INCOMPLETE;
5756 }
5757 ++p;
5758
5759 /* Disassembler */
5760 if (p < end) {
5761 p->isText = true;
5762 desc_copy(p->name, "Assembly");
5763 desc_copy(p->description, "Final Assembly");
5764 if (radv_copy_representation(p->pData, &p->dataSize, shader->disasm_string) != VK_SUCCESS)
5765 result = VK_INCOMPLETE;
5766 }
5767 ++p;
5768
5769 if (!pInternalRepresentations)
5770 *pInternalRepresentationCount = p - pInternalRepresentations;
5771 else if(p > end) {
5772 result = VK_INCOMPLETE;
5773 *pInternalRepresentationCount = end - pInternalRepresentations;
5774 } else {
5775 *pInternalRepresentationCount = p - pInternalRepresentations;
5776 }
5777
5778 return result;
5779 }