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