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