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