radv/gfx10: re-enable NGG GS
[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 gl_shader_stage last_xfb_stage = MESA_SHADER_VERTEX;
2439
2440 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2441 if (nir[i])
2442 last_xfb_stage = i;
2443 }
2444
2445 bool uses_xfb = nir[last_xfb_stage] &&
2446 radv_nir_stage_uses_xfb(nir[last_xfb_stage]);
2447
2448 if (!device->physical_device->use_ngg_streamout && uses_xfb) {
2449 if (nir[MESA_SHADER_TESS_CTRL])
2450 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;
2451 else
2452 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = false;
2453 }
2454
2455 /* Determine if the pipeline is eligible for the NGG passthrough
2456 * mode. It can't be enabled for geometry shaders, for NGG
2457 * streamout or for vertex shaders that export the primitive ID
2458 * (this is checked later because we don't have the info here.)
2459 */
2460 if (!nir[MESA_SHADER_GEOMETRY] && !uses_xfb) {
2461 if (nir[MESA_SHADER_TESS_CTRL] &&
2462 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg) {
2463 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg_passthrough = true;
2464 } else if (nir[MESA_SHADER_VERTEX] &&
2465 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg) {
2466 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg_passthrough = true;
2467 }
2468 }
2469 }
2470
2471 for(int i = 0; i < MESA_SHADER_STAGES; ++i)
2472 keys[i].has_multiview_view_index = key->has_multiview_view_index;
2473
2474 keys[MESA_SHADER_FRAGMENT].fs.col_format = key->col_format;
2475 keys[MESA_SHADER_FRAGMENT].fs.is_int8 = key->is_int8;
2476 keys[MESA_SHADER_FRAGMENT].fs.is_int10 = key->is_int10;
2477 keys[MESA_SHADER_FRAGMENT].fs.log2_ps_iter_samples = key->log2_ps_iter_samples;
2478 keys[MESA_SHADER_FRAGMENT].fs.num_samples = key->num_samples;
2479
2480 if (nir[MESA_SHADER_COMPUTE]) {
2481 keys[MESA_SHADER_COMPUTE].cs.subgroup_size = key->compute_subgroup_size;
2482 }
2483 }
2484
2485 static uint8_t
2486 radv_get_wave_size(struct radv_device *device,
2487 const VkPipelineShaderStageCreateInfo *pStage,
2488 gl_shader_stage stage,
2489 const struct radv_shader_variant_key *key)
2490 {
2491 if (stage == MESA_SHADER_GEOMETRY && !key->vs_common_out.as_ngg)
2492 return 64;
2493 else if (stage == MESA_SHADER_COMPUTE) {
2494 if (key->cs.subgroup_size) {
2495 /* Return the required subgroup size if specified. */
2496 return key->cs.subgroup_size;
2497 }
2498 return device->physical_device->cs_wave_size;
2499 }
2500 else if (stage == MESA_SHADER_FRAGMENT)
2501 return device->physical_device->ps_wave_size;
2502 else
2503 return device->physical_device->ge_wave_size;
2504 }
2505
2506 static void
2507 radv_fill_shader_info(struct radv_pipeline *pipeline,
2508 const VkPipelineShaderStageCreateInfo **pStages,
2509 struct radv_shader_variant_key *keys,
2510 struct radv_shader_info *infos,
2511 nir_shader **nir)
2512 {
2513 unsigned active_stages = 0;
2514 unsigned filled_stages = 0;
2515
2516 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
2517 if (nir[i])
2518 active_stages |= (1 << i);
2519 }
2520
2521 if (nir[MESA_SHADER_FRAGMENT]) {
2522 radv_nir_shader_info_init(&infos[MESA_SHADER_FRAGMENT]);
2523 radv_nir_shader_info_pass(nir[MESA_SHADER_FRAGMENT],
2524 pipeline->layout,
2525 &keys[MESA_SHADER_FRAGMENT],
2526 &infos[MESA_SHADER_FRAGMENT]);
2527
2528 /* TODO: These are no longer used as keys we should refactor this */
2529 keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id =
2530 infos[MESA_SHADER_FRAGMENT].ps.prim_id_input;
2531 keys[MESA_SHADER_VERTEX].vs_common_out.export_layer_id =
2532 infos[MESA_SHADER_FRAGMENT].ps.layer_input;
2533 keys[MESA_SHADER_VERTEX].vs_common_out.export_clip_dists =
2534 !!infos[MESA_SHADER_FRAGMENT].ps.num_input_clips_culls;
2535 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_prim_id =
2536 infos[MESA_SHADER_FRAGMENT].ps.prim_id_input;
2537 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_layer_id =
2538 infos[MESA_SHADER_FRAGMENT].ps.layer_input;
2539 keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_clip_dists =
2540 !!infos[MESA_SHADER_FRAGMENT].ps.num_input_clips_culls;
2541
2542 /* NGG passthrough mode can't be enabled for vertex shaders
2543 * that export the primitive ID.
2544 *
2545 * TODO: I should really refactor the keys logic.
2546 */
2547 if (nir[MESA_SHADER_VERTEX] &&
2548 keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id) {
2549 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg_passthrough = false;
2550 }
2551
2552 filled_stages |= (1 << MESA_SHADER_FRAGMENT);
2553 }
2554
2555 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9 &&
2556 nir[MESA_SHADER_TESS_CTRL]) {
2557 struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};
2558 struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];
2559 key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;
2560
2561 radv_nir_shader_info_init(&infos[MESA_SHADER_TESS_CTRL]);
2562
2563 for (int i = 0; i < 2; i++) {
2564 radv_nir_shader_info_pass(combined_nir[i],
2565 pipeline->layout, &key,
2566 &infos[MESA_SHADER_TESS_CTRL]);
2567 }
2568
2569 keys[MESA_SHADER_TESS_EVAL].tes.num_patches =
2570 infos[MESA_SHADER_TESS_CTRL].tcs.num_patches;
2571 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs =
2572 util_last_bit64(infos[MESA_SHADER_TESS_CTRL].tcs.outputs_written);
2573
2574 filled_stages |= (1 << MESA_SHADER_VERTEX);
2575 filled_stages |= (1 << MESA_SHADER_TESS_CTRL);
2576 }
2577
2578 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9 &&
2579 nir[MESA_SHADER_GEOMETRY]) {
2580 gl_shader_stage pre_stage = nir[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2581 struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};
2582
2583 radv_nir_shader_info_init(&infos[MESA_SHADER_GEOMETRY]);
2584
2585 for (int i = 0; i < 2; i++) {
2586 radv_nir_shader_info_pass(combined_nir[i],
2587 pipeline->layout,
2588 &keys[pre_stage],
2589 &infos[MESA_SHADER_GEOMETRY]);
2590 }
2591
2592 filled_stages |= (1 << pre_stage);
2593 filled_stages |= (1 << MESA_SHADER_GEOMETRY);
2594 }
2595
2596 active_stages ^= filled_stages;
2597 while (active_stages) {
2598 int i = u_bit_scan(&active_stages);
2599
2600 if (i == MESA_SHADER_TESS_CTRL) {
2601 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs =
2602 util_last_bit64(infos[MESA_SHADER_VERTEX].vs.ls_outputs_written);
2603 }
2604
2605 if (i == MESA_SHADER_TESS_EVAL) {
2606 keys[MESA_SHADER_TESS_EVAL].tes.num_patches =
2607 infos[MESA_SHADER_TESS_CTRL].tcs.num_patches;
2608 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs =
2609 util_last_bit64(infos[MESA_SHADER_TESS_CTRL].tcs.outputs_written);
2610 }
2611
2612 radv_nir_shader_info_init(&infos[i]);
2613 radv_nir_shader_info_pass(nir[i], pipeline->layout,
2614 &keys[i], &infos[i]);
2615 }
2616
2617 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
2618 if (nir[i])
2619 infos[i].wave_size =
2620 radv_get_wave_size(pipeline->device, pStages[i],
2621 i, &keys[i]);
2622 }
2623 }
2624
2625 static void
2626 merge_tess_info(struct shader_info *tes_info,
2627 const struct shader_info *tcs_info)
2628 {
2629 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
2630 *
2631 * "PointMode. Controls generation of points rather than triangles
2632 * or lines. This functionality defaults to disabled, and is
2633 * enabled if either shader stage includes the execution mode.
2634 *
2635 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
2636 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
2637 * and OutputVertices, it says:
2638 *
2639 * "One mode must be set in at least one of the tessellation
2640 * shader stages."
2641 *
2642 * So, the fields can be set in either the TCS or TES, but they must
2643 * agree if set in both. Our backend looks at TES, so bitwise-or in
2644 * the values from the TCS.
2645 */
2646 assert(tcs_info->tess.tcs_vertices_out == 0 ||
2647 tes_info->tess.tcs_vertices_out == 0 ||
2648 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
2649 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
2650
2651 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
2652 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
2653 tcs_info->tess.spacing == tes_info->tess.spacing);
2654 tes_info->tess.spacing |= tcs_info->tess.spacing;
2655
2656 assert(tcs_info->tess.primitive_mode == 0 ||
2657 tes_info->tess.primitive_mode == 0 ||
2658 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
2659 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
2660 tes_info->tess.ccw |= tcs_info->tess.ccw;
2661 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
2662 }
2663
2664 static
2665 void radv_init_feedback(const VkPipelineCreationFeedbackCreateInfoEXT *ext)
2666 {
2667 if (!ext)
2668 return;
2669
2670 if (ext->pPipelineCreationFeedback) {
2671 ext->pPipelineCreationFeedback->flags = 0;
2672 ext->pPipelineCreationFeedback->duration = 0;
2673 }
2674
2675 for (unsigned i = 0; i < ext->pipelineStageCreationFeedbackCount; ++i) {
2676 ext->pPipelineStageCreationFeedbacks[i].flags = 0;
2677 ext->pPipelineStageCreationFeedbacks[i].duration = 0;
2678 }
2679 }
2680
2681 static
2682 void radv_start_feedback(VkPipelineCreationFeedbackEXT *feedback)
2683 {
2684 if (!feedback)
2685 return;
2686
2687 feedback->duration -= radv_get_current_time();
2688 feedback ->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT;
2689 }
2690
2691 static
2692 void radv_stop_feedback(VkPipelineCreationFeedbackEXT *feedback, bool cache_hit)
2693 {
2694 if (!feedback)
2695 return;
2696
2697 feedback->duration += radv_get_current_time();
2698 feedback ->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT |
2699 (cache_hit ? VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT : 0);
2700 }
2701
2702 static
2703 bool radv_aco_supported_stage(gl_shader_stage stage, bool has_ts)
2704 {
2705 return (stage == MESA_SHADER_VERTEX && !has_ts) ||
2706 (stage == MESA_SHADER_GEOMETRY && !has_ts) ||
2707 stage == MESA_SHADER_FRAGMENT ||
2708 stage == MESA_SHADER_COMPUTE;
2709 }
2710
2711 void radv_create_shaders(struct radv_pipeline *pipeline,
2712 struct radv_device *device,
2713 struct radv_pipeline_cache *cache,
2714 const struct radv_pipeline_key *key,
2715 const VkPipelineShaderStageCreateInfo **pStages,
2716 const VkPipelineCreateFlags flags,
2717 VkPipelineCreationFeedbackEXT *pipeline_feedback,
2718 VkPipelineCreationFeedbackEXT **stage_feedbacks)
2719 {
2720 struct radv_shader_module fs_m = {0};
2721 struct radv_shader_module *modules[MESA_SHADER_STAGES] = { 0, };
2722 nir_shader *nir[MESA_SHADER_STAGES] = {0};
2723 struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};
2724 struct radv_shader_variant_key keys[MESA_SHADER_STAGES] = {{{{{0}}}}};
2725 struct radv_shader_info infos[MESA_SHADER_STAGES] = {0};
2726 unsigned char hash[20], gs_copy_hash[20];
2727 bool keep_executable_info = (flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR) || device->keep_shader_info;
2728
2729 radv_start_feedback(pipeline_feedback);
2730
2731 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
2732 if (pStages[i]) {
2733 modules[i] = radv_shader_module_from_handle(pStages[i]->module);
2734 if (modules[i]->nir)
2735 _mesa_sha1_compute(modules[i]->nir->info.name,
2736 strlen(modules[i]->nir->info.name),
2737 modules[i]->sha1);
2738
2739 pipeline->active_stages |= mesa_to_vk_shader_stage(i);
2740 }
2741 }
2742
2743 radv_hash_shaders(hash, pStages, pipeline->layout, key, get_hash_flags(device));
2744 memcpy(gs_copy_hash, hash, 20);
2745 gs_copy_hash[0] ^= 1;
2746
2747 bool found_in_application_cache = true;
2748 if (modules[MESA_SHADER_GEOMETRY] && !keep_executable_info) {
2749 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2750 radv_create_shader_variants_from_pipeline_cache(device, cache, gs_copy_hash, variants,
2751 &found_in_application_cache);
2752 pipeline->gs_copy_shader = variants[MESA_SHADER_GEOMETRY];
2753 }
2754
2755 if (!keep_executable_info &&
2756 radv_create_shader_variants_from_pipeline_cache(device, cache, hash, pipeline->shaders,
2757 &found_in_application_cache) &&
2758 (!modules[MESA_SHADER_GEOMETRY] || pipeline->gs_copy_shader)) {
2759 radv_stop_feedback(pipeline_feedback, found_in_application_cache);
2760 return;
2761 }
2762
2763 if (!modules[MESA_SHADER_FRAGMENT] && !modules[MESA_SHADER_COMPUTE]) {
2764 nir_builder fs_b;
2765 nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
2766 fs_b.shader->info.name = ralloc_strdup(fs_b.shader, "noop_fs");
2767 fs_m.nir = fs_b.shader;
2768 modules[MESA_SHADER_FRAGMENT] = &fs_m;
2769 }
2770
2771 bool has_ts = modules[MESA_SHADER_TESS_CTRL] || modules[MESA_SHADER_TESS_EVAL];
2772 bool use_aco = device->physical_device->use_aco;
2773
2774 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
2775 const VkPipelineShaderStageCreateInfo *stage = pStages[i];
2776
2777 if (!modules[i])
2778 continue;
2779
2780 radv_start_feedback(stage_feedbacks[i]);
2781
2782 bool aco = use_aco && radv_aco_supported_stage(i, has_ts);
2783 nir[i] = radv_shader_compile_to_nir(device, modules[i],
2784 stage ? stage->pName : "main", i,
2785 stage ? stage->pSpecializationInfo : NULL,
2786 flags, pipeline->layout, aco);
2787
2788 /* We don't want to alter meta shaders IR directly so clone it
2789 * first.
2790 */
2791 if (nir[i]->info.name) {
2792 nir[i] = nir_shader_clone(NULL, nir[i]);
2793 }
2794
2795 radv_stop_feedback(stage_feedbacks[i], false);
2796 }
2797
2798 if (nir[MESA_SHADER_TESS_CTRL]) {
2799 nir_lower_patch_vertices(nir[MESA_SHADER_TESS_EVAL], nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out, NULL);
2800 merge_tess_info(&nir[MESA_SHADER_TESS_EVAL]->info, &nir[MESA_SHADER_TESS_CTRL]->info);
2801 }
2802
2803 if (!(flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT))
2804 radv_link_shaders(pipeline, nir);
2805
2806 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2807 if (nir[i]) {
2808 NIR_PASS_V(nir[i], nir_lower_non_uniform_access,
2809 nir_lower_non_uniform_ubo_access |
2810 nir_lower_non_uniform_ssbo_access |
2811 nir_lower_non_uniform_texture_access |
2812 nir_lower_non_uniform_image_access);
2813
2814 bool aco = use_aco && radv_aco_supported_stage(i, has_ts);
2815 if (!aco)
2816 NIR_PASS_V(nir[i], nir_lower_bool_to_int32);
2817 }
2818 }
2819
2820 if (nir[MESA_SHADER_FRAGMENT])
2821 radv_lower_fs_io(nir[MESA_SHADER_FRAGMENT]);
2822
2823 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2824 if (radv_can_dump_shader(device, modules[i], false))
2825 nir_print_shader(nir[i], stderr);
2826 }
2827
2828 radv_fill_shader_keys(device, keys, key, nir);
2829
2830 radv_fill_shader_info(pipeline, pStages, keys, infos, nir);
2831
2832 if ((nir[MESA_SHADER_VERTEX] &&
2833 keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg) ||
2834 (nir[MESA_SHADER_TESS_EVAL] &&
2835 keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg)) {
2836 struct gfx10_ngg_info *ngg_info;
2837
2838 if (nir[MESA_SHADER_GEOMETRY])
2839 ngg_info = &infos[MESA_SHADER_GEOMETRY].ngg_info;
2840 else if (nir[MESA_SHADER_TESS_CTRL])
2841 ngg_info = &infos[MESA_SHADER_TESS_EVAL].ngg_info;
2842 else
2843 ngg_info = &infos[MESA_SHADER_VERTEX].ngg_info;
2844
2845 gfx10_get_ngg_info(key, pipeline, nir, infos, ngg_info);
2846 } else if (nir[MESA_SHADER_GEOMETRY]) {
2847 struct gfx9_gs_info *gs_info =
2848 &infos[MESA_SHADER_GEOMETRY].gs_ring_info;
2849
2850 gfx9_get_gs_info(key, pipeline, nir, infos, gs_info);
2851 }
2852
2853 if(modules[MESA_SHADER_GEOMETRY]) {
2854 struct radv_shader_binary *gs_copy_binary = NULL;
2855 if (!pipeline->gs_copy_shader &&
2856 !radv_pipeline_has_ngg(pipeline)) {
2857 struct radv_shader_info info = {};
2858 struct radv_shader_variant_key key = {};
2859
2860 key.has_multiview_view_index =
2861 keys[MESA_SHADER_GEOMETRY].has_multiview_view_index;
2862
2863 radv_nir_shader_info_pass(nir[MESA_SHADER_GEOMETRY],
2864 pipeline->layout, &key,
2865 &info);
2866 info.wave_size = 64; /* Wave32 not supported. */
2867
2868 pipeline->gs_copy_shader = radv_create_gs_copy_shader(
2869 device, nir[MESA_SHADER_GEOMETRY], &info,
2870 &gs_copy_binary, keep_executable_info,
2871 keys[MESA_SHADER_GEOMETRY].has_multiview_view_index,
2872 use_aco);
2873 }
2874
2875 if (!keep_executable_info && pipeline->gs_copy_shader) {
2876 struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};
2877 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2878
2879 binaries[MESA_SHADER_GEOMETRY] = gs_copy_binary;
2880 variants[MESA_SHADER_GEOMETRY] = pipeline->gs_copy_shader;
2881
2882 radv_pipeline_cache_insert_shaders(device, cache,
2883 gs_copy_hash,
2884 variants,
2885 binaries);
2886 }
2887 free(gs_copy_binary);
2888 }
2889
2890 if (nir[MESA_SHADER_FRAGMENT]) {
2891 if (!pipeline->shaders[MESA_SHADER_FRAGMENT]) {
2892 radv_start_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT]);
2893
2894 bool aco = use_aco && radv_aco_supported_stage(MESA_SHADER_FRAGMENT, has_ts);
2895 pipeline->shaders[MESA_SHADER_FRAGMENT] =
2896 radv_shader_variant_compile(device, modules[MESA_SHADER_FRAGMENT], &nir[MESA_SHADER_FRAGMENT], 1,
2897 pipeline->layout, keys + MESA_SHADER_FRAGMENT,
2898 infos + MESA_SHADER_FRAGMENT,
2899 keep_executable_info, aco,
2900 &binaries[MESA_SHADER_FRAGMENT]);
2901
2902 radv_stop_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT], false);
2903 }
2904 }
2905
2906 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_TESS_CTRL]) {
2907 if (!pipeline->shaders[MESA_SHADER_TESS_CTRL]) {
2908 struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};
2909 struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];
2910 key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;
2911
2912 radv_start_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL]);
2913
2914 pipeline->shaders[MESA_SHADER_TESS_CTRL] = radv_shader_variant_compile(device, modules[MESA_SHADER_TESS_CTRL], combined_nir, 2,
2915 pipeline->layout,
2916 &key, &infos[MESA_SHADER_TESS_CTRL], keep_executable_info,
2917 false, &binaries[MESA_SHADER_TESS_CTRL]);
2918
2919 radv_stop_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL], false);
2920 }
2921 modules[MESA_SHADER_VERTEX] = NULL;
2922 keys[MESA_SHADER_TESS_EVAL].tes.num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2923 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.outputs_written);
2924 }
2925
2926 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_GEOMETRY]) {
2927 gl_shader_stage pre_stage = modules[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2928 if (!pipeline->shaders[MESA_SHADER_GEOMETRY]) {
2929 struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};
2930
2931 radv_start_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY]);
2932
2933 bool aco = use_aco && radv_aco_supported_stage(MESA_SHADER_GEOMETRY, has_ts);
2934 pipeline->shaders[MESA_SHADER_GEOMETRY] = radv_shader_variant_compile(device, modules[MESA_SHADER_GEOMETRY], combined_nir, 2,
2935 pipeline->layout,
2936 &keys[pre_stage], &infos[MESA_SHADER_GEOMETRY], keep_executable_info,
2937 aco, &binaries[MESA_SHADER_GEOMETRY]);
2938
2939 radv_stop_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY], false);
2940 }
2941 modules[pre_stage] = NULL;
2942 }
2943
2944 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2945 if(modules[i] && !pipeline->shaders[i]) {
2946 if (i == MESA_SHADER_TESS_CTRL) {
2947 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs = util_last_bit64(pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.ls_outputs_written);
2948 }
2949 if (i == MESA_SHADER_TESS_EVAL) {
2950 keys[MESA_SHADER_TESS_EVAL].tes.num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2951 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.outputs_written);
2952 }
2953
2954 radv_start_feedback(stage_feedbacks[i]);
2955
2956 bool aco = use_aco && radv_aco_supported_stage(i, has_ts);
2957 pipeline->shaders[i] = radv_shader_variant_compile(device, modules[i], &nir[i], 1,
2958 pipeline->layout,
2959 keys + i, infos + i,keep_executable_info,
2960 aco, &binaries[i]);
2961
2962 radv_stop_feedback(stage_feedbacks[i], false);
2963 }
2964 }
2965
2966 if (!keep_executable_info) {
2967 radv_pipeline_cache_insert_shaders(device, cache, hash, pipeline->shaders,
2968 binaries);
2969 }
2970
2971 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2972 free(binaries[i]);
2973 if (nir[i]) {
2974 ralloc_free(nir[i]);
2975
2976 if (radv_can_dump_shader_stats(device, modules[i]))
2977 radv_shader_dump_stats(device,
2978 pipeline->shaders[i],
2979 i, stderr);
2980 }
2981 }
2982
2983 if (fs_m.nir)
2984 ralloc_free(fs_m.nir);
2985
2986 radv_stop_feedback(pipeline_feedback, false);
2987 }
2988
2989 static uint32_t
2990 radv_pipeline_stage_to_user_data_0(struct radv_pipeline *pipeline,
2991 gl_shader_stage stage, enum chip_class chip_class)
2992 {
2993 bool has_gs = radv_pipeline_has_gs(pipeline);
2994 bool has_tess = radv_pipeline_has_tess(pipeline);
2995 bool has_ngg = radv_pipeline_has_ngg(pipeline);
2996
2997 switch (stage) {
2998 case MESA_SHADER_FRAGMENT:
2999 return R_00B030_SPI_SHADER_USER_DATA_PS_0;
3000 case MESA_SHADER_VERTEX:
3001 if (has_tess) {
3002 if (chip_class >= GFX10) {
3003 return R_00B430_SPI_SHADER_USER_DATA_HS_0;
3004 } else if (chip_class == GFX9) {
3005 return R_00B430_SPI_SHADER_USER_DATA_LS_0;
3006 } else {
3007 return R_00B530_SPI_SHADER_USER_DATA_LS_0;
3008 }
3009
3010 }
3011
3012 if (has_gs) {
3013 if (chip_class >= GFX10) {
3014 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3015 } else {
3016 return R_00B330_SPI_SHADER_USER_DATA_ES_0;
3017 }
3018 }
3019
3020 if (has_ngg)
3021 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3022
3023 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
3024 case MESA_SHADER_GEOMETRY:
3025 return chip_class == GFX9 ? R_00B330_SPI_SHADER_USER_DATA_ES_0 :
3026 R_00B230_SPI_SHADER_USER_DATA_GS_0;
3027 case MESA_SHADER_COMPUTE:
3028 return R_00B900_COMPUTE_USER_DATA_0;
3029 case MESA_SHADER_TESS_CTRL:
3030 return chip_class == GFX9 ? R_00B430_SPI_SHADER_USER_DATA_LS_0 :
3031 R_00B430_SPI_SHADER_USER_DATA_HS_0;
3032 case MESA_SHADER_TESS_EVAL:
3033 if (has_gs) {
3034 return chip_class >= GFX10 ? R_00B230_SPI_SHADER_USER_DATA_GS_0 :
3035 R_00B330_SPI_SHADER_USER_DATA_ES_0;
3036 } else if (has_ngg) {
3037 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
3038 } else {
3039 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
3040 }
3041 default:
3042 unreachable("unknown shader");
3043 }
3044 }
3045
3046 struct radv_bin_size_entry {
3047 unsigned bpp;
3048 VkExtent2D extent;
3049 };
3050
3051 static VkExtent2D
3052 radv_gfx9_compute_bin_size(struct radv_pipeline *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo)
3053 {
3054 static const struct radv_bin_size_entry color_size_table[][3][9] = {
3055 {
3056 /* One RB / SE */
3057 {
3058 /* One shader engine */
3059 { 0, {128, 128}},
3060 { 1, { 64, 128}},
3061 { 2, { 32, 128}},
3062 { 3, { 16, 128}},
3063 { 17, { 0, 0}},
3064 { UINT_MAX, { 0, 0}},
3065 },
3066 {
3067 /* Two shader engines */
3068 { 0, {128, 128}},
3069 { 2, { 64, 128}},
3070 { 3, { 32, 128}},
3071 { 5, { 16, 128}},
3072 { 17, { 0, 0}},
3073 { UINT_MAX, { 0, 0}},
3074 },
3075 {
3076 /* Four shader engines */
3077 { 0, {128, 128}},
3078 { 3, { 64, 128}},
3079 { 5, { 16, 128}},
3080 { 17, { 0, 0}},
3081 { UINT_MAX, { 0, 0}},
3082 },
3083 },
3084 {
3085 /* Two RB / SE */
3086 {
3087 /* One shader engine */
3088 { 0, {128, 128}},
3089 { 2, { 64, 128}},
3090 { 3, { 32, 128}},
3091 { 5, { 16, 128}},
3092 { 33, { 0, 0}},
3093 { UINT_MAX, { 0, 0}},
3094 },
3095 {
3096 /* Two shader engines */
3097 { 0, {128, 128}},
3098 { 3, { 64, 128}},
3099 { 5, { 32, 128}},
3100 { 9, { 16, 128}},
3101 { 33, { 0, 0}},
3102 { UINT_MAX, { 0, 0}},
3103 },
3104 {
3105 /* Four shader engines */
3106 { 0, {256, 256}},
3107 { 2, {128, 256}},
3108 { 3, {128, 128}},
3109 { 5, { 64, 128}},
3110 { 9, { 16, 128}},
3111 { 33, { 0, 0}},
3112 { UINT_MAX, { 0, 0}},
3113 },
3114 },
3115 {
3116 /* Four RB / SE */
3117 {
3118 /* One shader engine */
3119 { 0, {128, 256}},
3120 { 2, {128, 128}},
3121 { 3, { 64, 128}},
3122 { 5, { 32, 128}},
3123 { 9, { 16, 128}},
3124 { 33, { 0, 0}},
3125 { UINT_MAX, { 0, 0}},
3126 },
3127 {
3128 /* Two shader engines */
3129 { 0, {256, 256}},
3130 { 2, {128, 256}},
3131 { 3, {128, 128}},
3132 { 5, { 64, 128}},
3133 { 9, { 32, 128}},
3134 { 17, { 16, 128}},
3135 { 33, { 0, 0}},
3136 { UINT_MAX, { 0, 0}},
3137 },
3138 {
3139 /* Four shader engines */
3140 { 0, {256, 512}},
3141 { 2, {256, 256}},
3142 { 3, {128, 256}},
3143 { 5, {128, 128}},
3144 { 9, { 64, 128}},
3145 { 17, { 16, 128}},
3146 { 33, { 0, 0}},
3147 { UINT_MAX, { 0, 0}},
3148 },
3149 },
3150 };
3151 static const struct radv_bin_size_entry ds_size_table[][3][9] = {
3152 {
3153 // One RB / SE
3154 {
3155 // One shader engine
3156 { 0, {128, 256}},
3157 { 2, {128, 128}},
3158 { 4, { 64, 128}},
3159 { 7, { 32, 128}},
3160 { 13, { 16, 128}},
3161 { 49, { 0, 0}},
3162 { UINT_MAX, { 0, 0}},
3163 },
3164 {
3165 // Two shader engines
3166 { 0, {256, 256}},
3167 { 2, {128, 256}},
3168 { 4, {128, 128}},
3169 { 7, { 64, 128}},
3170 { 13, { 32, 128}},
3171 { 25, { 16, 128}},
3172 { 49, { 0, 0}},
3173 { UINT_MAX, { 0, 0}},
3174 },
3175 {
3176 // Four shader engines
3177 { 0, {256, 512}},
3178 { 2, {256, 256}},
3179 { 4, {128, 256}},
3180 { 7, {128, 128}},
3181 { 13, { 64, 128}},
3182 { 25, { 16, 128}},
3183 { 49, { 0, 0}},
3184 { UINT_MAX, { 0, 0}},
3185 },
3186 },
3187 {
3188 // Two RB / SE
3189 {
3190 // One shader engine
3191 { 0, {256, 256}},
3192 { 2, {128, 256}},
3193 { 4, {128, 128}},
3194 { 7, { 64, 128}},
3195 { 13, { 32, 128}},
3196 { 25, { 16, 128}},
3197 { 97, { 0, 0}},
3198 { UINT_MAX, { 0, 0}},
3199 },
3200 {
3201 // Two shader engines
3202 { 0, {256, 512}},
3203 { 2, {256, 256}},
3204 { 4, {128, 256}},
3205 { 7, {128, 128}},
3206 { 13, { 64, 128}},
3207 { 25, { 32, 128}},
3208 { 49, { 16, 128}},
3209 { 97, { 0, 0}},
3210 { UINT_MAX, { 0, 0}},
3211 },
3212 {
3213 // Four shader engines
3214 { 0, {512, 512}},
3215 { 2, {256, 512}},
3216 { 4, {256, 256}},
3217 { 7, {128, 256}},
3218 { 13, {128, 128}},
3219 { 25, { 64, 128}},
3220 { 49, { 16, 128}},
3221 { 97, { 0, 0}},
3222 { UINT_MAX, { 0, 0}},
3223 },
3224 },
3225 {
3226 // Four RB / SE
3227 {
3228 // One shader engine
3229 { 0, {256, 512}},
3230 { 2, {256, 256}},
3231 { 4, {128, 256}},
3232 { 7, {128, 128}},
3233 { 13, { 64, 128}},
3234 { 25, { 32, 128}},
3235 { 49, { 16, 128}},
3236 { UINT_MAX, { 0, 0}},
3237 },
3238 {
3239 // Two shader engines
3240 { 0, {512, 512}},
3241 { 2, {256, 512}},
3242 { 4, {256, 256}},
3243 { 7, {128, 256}},
3244 { 13, {128, 128}},
3245 { 25, { 64, 128}},
3246 { 49, { 32, 128}},
3247 { 97, { 16, 128}},
3248 { UINT_MAX, { 0, 0}},
3249 },
3250 {
3251 // Four shader engines
3252 { 0, {512, 512}},
3253 { 4, {256, 512}},
3254 { 7, {256, 256}},
3255 { 13, {128, 256}},
3256 { 25, {128, 128}},
3257 { 49, { 64, 128}},
3258 { 97, { 16, 128}},
3259 { UINT_MAX, { 0, 0}},
3260 },
3261 },
3262 };
3263
3264 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
3265 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
3266 VkExtent2D extent = {512, 512};
3267
3268 unsigned log_num_rb_per_se =
3269 util_logbase2_ceil(pipeline->device->physical_device->rad_info.num_render_backends /
3270 pipeline->device->physical_device->rad_info.max_se);
3271 unsigned log_num_se = util_logbase2_ceil(pipeline->device->physical_device->rad_info.max_se);
3272
3273 unsigned total_samples = 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline->graphics.ms.pa_sc_aa_config);
3274 unsigned ps_iter_samples = 1u << G_028804_PS_ITER_SAMPLES(pipeline->graphics.ms.db_eqaa);
3275 unsigned effective_samples = total_samples;
3276 unsigned color_bytes_per_pixel = 0;
3277
3278 const VkPipelineColorBlendStateCreateInfo *vkblend =
3279 radv_pipeline_get_color_blend_state(pCreateInfo);
3280 if (vkblend) {
3281 for (unsigned i = 0; i < subpass->color_count; i++) {
3282 if (!vkblend->pAttachments[i].colorWriteMask)
3283 continue;
3284
3285 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
3286 continue;
3287
3288 VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;
3289 color_bytes_per_pixel += vk_format_get_blocksize(format);
3290 }
3291
3292 /* MSAA images typically don't use all samples all the time. */
3293 if (effective_samples >= 2 && ps_iter_samples <= 1)
3294 effective_samples = 2;
3295 color_bytes_per_pixel *= effective_samples;
3296 }
3297
3298 const struct radv_bin_size_entry *color_entry = color_size_table[log_num_rb_per_se][log_num_se];
3299 while(color_entry[1].bpp <= color_bytes_per_pixel)
3300 ++color_entry;
3301
3302 extent = color_entry->extent;
3303
3304 if (subpass->depth_stencil_attachment) {
3305 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;
3306
3307 /* Coefficients taken from AMDVLK */
3308 unsigned depth_coeff = vk_format_is_depth(attachment->format) ? 5 : 0;
3309 unsigned stencil_coeff = vk_format_is_stencil(attachment->format) ? 1 : 0;
3310 unsigned ds_bytes_per_pixel = 4 * (depth_coeff + stencil_coeff) * total_samples;
3311
3312 const struct radv_bin_size_entry *ds_entry = ds_size_table[log_num_rb_per_se][log_num_se];
3313 while(ds_entry[1].bpp <= ds_bytes_per_pixel)
3314 ++ds_entry;
3315
3316 if (ds_entry->extent.width * ds_entry->extent.height < extent.width * extent.height)
3317 extent = ds_entry->extent;
3318 }
3319
3320 return extent;
3321 }
3322
3323 static VkExtent2D
3324 radv_gfx10_compute_bin_size(struct radv_pipeline *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo)
3325 {
3326 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
3327 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
3328 VkExtent2D extent = {512, 512};
3329
3330 const unsigned db_tag_size = 64;
3331 const unsigned db_tag_count = 312;
3332 const unsigned color_tag_size = 1024;
3333 const unsigned color_tag_count = 31;
3334 const unsigned fmask_tag_size = 256;
3335 const unsigned fmask_tag_count = 44;
3336
3337 const unsigned rb_count = pipeline->device->physical_device->rad_info.num_render_backends;
3338 const unsigned pipe_count = MAX2(rb_count, pipeline->device->physical_device->rad_info.num_sdp_interfaces);
3339
3340 const unsigned db_tag_part = (db_tag_count * rb_count / pipe_count) * db_tag_size * pipe_count;
3341 const unsigned color_tag_part = (color_tag_count * rb_count / pipe_count) * color_tag_size * pipe_count;
3342 const unsigned fmask_tag_part = (fmask_tag_count * rb_count / pipe_count) * fmask_tag_size * pipe_count;
3343
3344 const unsigned total_samples = 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline->graphics.ms.pa_sc_aa_config);
3345 const unsigned samples_log = util_logbase2_ceil(total_samples);
3346
3347 unsigned color_bytes_per_pixel = 0;
3348 unsigned fmask_bytes_per_pixel = 0;
3349
3350 const VkPipelineColorBlendStateCreateInfo *vkblend =
3351 radv_pipeline_get_color_blend_state(pCreateInfo);
3352 if (vkblend) {
3353 for (unsigned i = 0; i < subpass->color_count; i++) {
3354 if (!vkblend->pAttachments[i].colorWriteMask)
3355 continue;
3356
3357 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
3358 continue;
3359
3360 VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;
3361 color_bytes_per_pixel += vk_format_get_blocksize(format);
3362
3363 if (total_samples > 1) {
3364 assert(samples_log <= 3);
3365 const unsigned fmask_array[] = {0, 1, 1, 4};
3366 fmask_bytes_per_pixel += fmask_array[samples_log];
3367 }
3368 }
3369
3370 color_bytes_per_pixel *= total_samples;
3371 }
3372 color_bytes_per_pixel = MAX2(color_bytes_per_pixel, 1);
3373
3374 const unsigned color_pixel_count_log = util_logbase2(color_tag_part / color_bytes_per_pixel);
3375 extent.width = 1ull << ((color_pixel_count_log + 1) / 2);
3376 extent.height = 1ull << (color_pixel_count_log / 2);
3377
3378 if (fmask_bytes_per_pixel) {
3379 const unsigned fmask_pixel_count_log = util_logbase2(fmask_tag_part / fmask_bytes_per_pixel);
3380
3381 const VkExtent2D fmask_extent = (VkExtent2D){
3382 .width = 1ull << ((fmask_pixel_count_log + 1) / 2),
3383 .height = 1ull << (color_pixel_count_log / 2)
3384 };
3385
3386 if (fmask_extent.width * fmask_extent.height < extent.width * extent.height)
3387 extent = fmask_extent;
3388 }
3389
3390 if (subpass->depth_stencil_attachment) {
3391 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;
3392
3393 /* Coefficients taken from AMDVLK */
3394 unsigned depth_coeff = vk_format_is_depth(attachment->format) ? 5 : 0;
3395 unsigned stencil_coeff = vk_format_is_stencil(attachment->format) ? 1 : 0;
3396 unsigned db_bytes_per_pixel = (depth_coeff + stencil_coeff) * total_samples;
3397
3398 const unsigned db_pixel_count_log = util_logbase2(db_tag_part / db_bytes_per_pixel);
3399
3400 const VkExtent2D db_extent = (VkExtent2D){
3401 .width = 1ull << ((db_pixel_count_log + 1) / 2),
3402 .height = 1ull << (color_pixel_count_log / 2)
3403 };
3404
3405 if (db_extent.width * db_extent.height < extent.width * extent.height)
3406 extent = db_extent;
3407 }
3408
3409 extent.width = MAX2(extent.width, 128);
3410 extent.height = MAX2(extent.width, 64);
3411
3412 return extent;
3413 }
3414
3415 static void
3416 radv_pipeline_generate_disabled_binning_state(struct radeon_cmdbuf *ctx_cs,
3417 struct radv_pipeline *pipeline,
3418 const VkGraphicsPipelineCreateInfo *pCreateInfo)
3419 {
3420 uint32_t pa_sc_binner_cntl_0 =
3421 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC) |
3422 S_028C44_DISABLE_START_OF_PRIM(1);
3423 uint32_t db_dfsm_control = S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF);
3424
3425 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3426 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
3427 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
3428 const VkPipelineColorBlendStateCreateInfo *vkblend =
3429 radv_pipeline_get_color_blend_state(pCreateInfo);
3430 unsigned min_bytes_per_pixel = 0;
3431
3432 if (vkblend) {
3433 for (unsigned i = 0; i < subpass->color_count; i++) {
3434 if (!vkblend->pAttachments[i].colorWriteMask)
3435 continue;
3436
3437 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
3438 continue;
3439
3440 VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;
3441 unsigned bytes = vk_format_get_blocksize(format);
3442 if (!min_bytes_per_pixel || bytes < min_bytes_per_pixel)
3443 min_bytes_per_pixel = bytes;
3444 }
3445 }
3446
3447 pa_sc_binner_cntl_0 =
3448 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_NEW_SC) |
3449 S_028C44_BIN_SIZE_X(0) |
3450 S_028C44_BIN_SIZE_Y(0) |
3451 S_028C44_BIN_SIZE_X_EXTEND(2) | /* 128 */
3452 S_028C44_BIN_SIZE_Y_EXTEND(min_bytes_per_pixel <= 4 ? 2 : 1) | /* 128 or 64 */
3453 S_028C44_DISABLE_START_OF_PRIM(1);
3454 }
3455
3456 pipeline->graphics.binning.pa_sc_binner_cntl_0 = pa_sc_binner_cntl_0;
3457 pipeline->graphics.binning.db_dfsm_control = db_dfsm_control;
3458 }
3459
3460 struct radv_binning_settings
3461 radv_get_binning_settings(const struct radv_physical_device *pdev)
3462 {
3463 struct radv_binning_settings settings;
3464 if (pdev->rad_info.has_dedicated_vram) {
3465 settings.context_states_per_bin = 1;
3466 settings.persistent_states_per_bin = 1;
3467 settings.fpovs_per_batch = 63;
3468 } else {
3469 /* The context states are affected by the scissor bug. */
3470 settings.context_states_per_bin = 6;
3471 /* 32 causes hangs for RAVEN. */
3472 settings.persistent_states_per_bin = 16;
3473 settings.fpovs_per_batch = 63;
3474 }
3475
3476 if (pdev->rad_info.has_gfx9_scissor_bug)
3477 settings.context_states_per_bin = 1;
3478
3479 return settings;
3480 }
3481
3482 static void
3483 radv_pipeline_generate_binning_state(struct radeon_cmdbuf *ctx_cs,
3484 struct radv_pipeline *pipeline,
3485 const VkGraphicsPipelineCreateInfo *pCreateInfo,
3486 const struct radv_blend_state *blend)
3487 {
3488 if (pipeline->device->physical_device->rad_info.chip_class < GFX9)
3489 return;
3490
3491 VkExtent2D bin_size;
3492 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3493 bin_size = radv_gfx10_compute_bin_size(pipeline, pCreateInfo);
3494 } else if (pipeline->device->physical_device->rad_info.chip_class == GFX9) {
3495 bin_size = radv_gfx9_compute_bin_size(pipeline, pCreateInfo);
3496 } else
3497 unreachable("Unhandled generation for binning bin size calculation");
3498
3499 if (pipeline->device->pbb_allowed && bin_size.width && bin_size.height) {
3500 struct radv_binning_settings settings =
3501 radv_get_binning_settings(pipeline->device->physical_device);
3502
3503 bool disable_start_of_prim = true;
3504 uint32_t db_dfsm_control = S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF);
3505
3506 const struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
3507
3508 if (pipeline->device->dfsm_allowed && ps &&
3509 !ps->info.ps.can_discard &&
3510 !ps->info.ps.writes_memory &&
3511 blend->cb_target_enabled_4bit) {
3512 db_dfsm_control = S_028060_PUNCHOUT_MODE(V_028060_AUTO);
3513 disable_start_of_prim = (blend->blend_enable_4bit & blend->cb_target_enabled_4bit) != 0;
3514 }
3515
3516 const uint32_t pa_sc_binner_cntl_0 =
3517 S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED) |
3518 S_028C44_BIN_SIZE_X(bin_size.width == 16) |
3519 S_028C44_BIN_SIZE_Y(bin_size.height == 16) |
3520 S_028C44_BIN_SIZE_X_EXTEND(util_logbase2(MAX2(bin_size.width, 32)) - 5) |
3521 S_028C44_BIN_SIZE_Y_EXTEND(util_logbase2(MAX2(bin_size.height, 32)) - 5) |
3522 S_028C44_CONTEXT_STATES_PER_BIN(settings.context_states_per_bin - 1) |
3523 S_028C44_PERSISTENT_STATES_PER_BIN(settings.persistent_states_per_bin - 1) |
3524 S_028C44_DISABLE_START_OF_PRIM(disable_start_of_prim) |
3525 S_028C44_FPOVS_PER_BATCH(settings.fpovs_per_batch) |
3526 S_028C44_OPTIMAL_BIN_SELECTION(1);
3527
3528 pipeline->graphics.binning.pa_sc_binner_cntl_0 = pa_sc_binner_cntl_0;
3529 pipeline->graphics.binning.db_dfsm_control = db_dfsm_control;
3530 } else
3531 radv_pipeline_generate_disabled_binning_state(ctx_cs, pipeline, pCreateInfo);
3532 }
3533
3534
3535 static void
3536 radv_pipeline_generate_depth_stencil_state(struct radeon_cmdbuf *ctx_cs,
3537 struct radv_pipeline *pipeline,
3538 const VkGraphicsPipelineCreateInfo *pCreateInfo,
3539 const struct radv_graphics_pipeline_create_info *extra)
3540 {
3541 const VkPipelineDepthStencilStateCreateInfo *vkds = radv_pipeline_get_depth_stencil_state(pCreateInfo);
3542 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
3543 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
3544 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
3545 struct radv_render_pass_attachment *attachment = NULL;
3546 uint32_t db_depth_control = 0, db_stencil_control = 0;
3547 uint32_t db_render_control = 0, db_render_override2 = 0;
3548 uint32_t db_render_override = 0;
3549
3550 if (subpass->depth_stencil_attachment)
3551 attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;
3552
3553 bool has_depth_attachment = attachment && vk_format_is_depth(attachment->format);
3554 bool has_stencil_attachment = attachment && vk_format_is_stencil(attachment->format);
3555
3556 if (vkds && has_depth_attachment) {
3557 db_depth_control = S_028800_Z_ENABLE(vkds->depthTestEnable ? 1 : 0) |
3558 S_028800_Z_WRITE_ENABLE(vkds->depthWriteEnable ? 1 : 0) |
3559 S_028800_ZFUNC(vkds->depthCompareOp) |
3560 S_028800_DEPTH_BOUNDS_ENABLE(vkds->depthBoundsTestEnable ? 1 : 0);
3561
3562 /* from amdvlk: For 4xAA and 8xAA need to decompress on flush for better performance */
3563 db_render_override2 |= S_028010_DECOMPRESS_Z_ON_FLUSH(attachment->samples > 2);
3564 }
3565
3566 if (has_stencil_attachment && vkds && vkds->stencilTestEnable) {
3567 db_depth_control |= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);
3568 db_depth_control |= S_028800_STENCILFUNC(vkds->front.compareOp);
3569 db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(vkds->front.failOp));
3570 db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(vkds->front.passOp));
3571 db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(vkds->front.depthFailOp));
3572
3573 db_depth_control |= S_028800_STENCILFUNC_BF(vkds->back.compareOp);
3574 db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(vkds->back.failOp));
3575 db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(vkds->back.passOp));
3576 db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(vkds->back.depthFailOp));
3577 }
3578
3579 if (attachment && extra) {
3580 db_render_control |= S_028000_DEPTH_CLEAR_ENABLE(extra->db_depth_clear);
3581 db_render_control |= S_028000_STENCIL_CLEAR_ENABLE(extra->db_stencil_clear);
3582
3583 db_render_control |= S_028000_RESUMMARIZE_ENABLE(extra->db_resummarize);
3584 db_render_control |= S_028000_DEPTH_COMPRESS_DISABLE(extra->db_flush_depth_inplace);
3585 db_render_control |= S_028000_STENCIL_COMPRESS_DISABLE(extra->db_flush_stencil_inplace);
3586 db_render_override2 |= S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra->db_depth_disable_expclear);
3587 db_render_override2 |= S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra->db_stencil_disable_expclear);
3588 }
3589
3590 db_render_override |= S_02800C_FORCE_HIS_ENABLE0(V_02800C_FORCE_DISABLE) |
3591 S_02800C_FORCE_HIS_ENABLE1(V_02800C_FORCE_DISABLE);
3592
3593 if (!pCreateInfo->pRasterizationState->depthClampEnable &&
3594 ps->info.ps.writes_z) {
3595 /* From VK_EXT_depth_range_unrestricted spec:
3596 *
3597 * "The behavior described in Primitive Clipping still applies.
3598 * If depth clamping is disabled the depth values are still
3599 * clipped to 0 ≤ zc ≤ wc before the viewport transform. If
3600 * depth clamping is enabled the above equation is ignored and
3601 * the depth values are instead clamped to the VkViewport
3602 * minDepth and maxDepth values, which in the case of this
3603 * extension can be outside of the 0.0 to 1.0 range."
3604 */
3605 db_render_override |= S_02800C_DISABLE_VIEWPORT_CLAMP(1);
3606 }
3607
3608 radeon_set_context_reg(ctx_cs, R_028800_DB_DEPTH_CONTROL, db_depth_control);
3609 radeon_set_context_reg(ctx_cs, R_02842C_DB_STENCIL_CONTROL, db_stencil_control);
3610
3611 radeon_set_context_reg(ctx_cs, R_028000_DB_RENDER_CONTROL, db_render_control);
3612 radeon_set_context_reg(ctx_cs, R_02800C_DB_RENDER_OVERRIDE, db_render_override);
3613 radeon_set_context_reg(ctx_cs, R_028010_DB_RENDER_OVERRIDE2, db_render_override2);
3614 }
3615
3616 static void
3617 radv_pipeline_generate_blend_state(struct radeon_cmdbuf *ctx_cs,
3618 struct radv_pipeline *pipeline,
3619 const struct radv_blend_state *blend)
3620 {
3621 radeon_set_context_reg_seq(ctx_cs, R_028780_CB_BLEND0_CONTROL, 8);
3622 radeon_emit_array(ctx_cs, blend->cb_blend_control,
3623 8);
3624 radeon_set_context_reg(ctx_cs, R_028808_CB_COLOR_CONTROL, blend->cb_color_control);
3625 radeon_set_context_reg(ctx_cs, R_028B70_DB_ALPHA_TO_MASK, blend->db_alpha_to_mask);
3626
3627 if (pipeline->device->physical_device->rad_info.has_rbplus) {
3628
3629 radeon_set_context_reg_seq(ctx_cs, R_028760_SX_MRT0_BLEND_OPT, 8);
3630 radeon_emit_array(ctx_cs, blend->sx_mrt_blend_opt, 8);
3631 }
3632
3633 radeon_set_context_reg(ctx_cs, R_028714_SPI_SHADER_COL_FORMAT, blend->spi_shader_col_format);
3634
3635 radeon_set_context_reg(ctx_cs, R_028238_CB_TARGET_MASK, blend->cb_target_mask);
3636 radeon_set_context_reg(ctx_cs, R_02823C_CB_SHADER_MASK, blend->cb_shader_mask);
3637
3638 pipeline->graphics.col_format = blend->spi_shader_col_format;
3639 pipeline->graphics.cb_target_mask = blend->cb_target_mask;
3640 }
3641
3642 static const VkConservativeRasterizationModeEXT
3643 radv_get_conservative_raster_mode(const VkPipelineRasterizationStateCreateInfo *pCreateInfo)
3644 {
3645 const VkPipelineRasterizationConservativeStateCreateInfoEXT *conservative_raster =
3646 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_RASTERIZATION_CONSERVATIVE_STATE_CREATE_INFO_EXT);
3647
3648 if (!conservative_raster)
3649 return VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT;
3650 return conservative_raster->conservativeRasterizationMode;
3651 }
3652
3653 static void
3654 radv_pipeline_generate_raster_state(struct radeon_cmdbuf *ctx_cs,
3655 struct radv_pipeline *pipeline,
3656 const VkGraphicsPipelineCreateInfo *pCreateInfo)
3657 {
3658 const VkPipelineRasterizationStateCreateInfo *vkraster = pCreateInfo->pRasterizationState;
3659 const VkConservativeRasterizationModeEXT mode =
3660 radv_get_conservative_raster_mode(vkraster);
3661 uint32_t pa_sc_conservative_rast = S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1);
3662 bool depth_clip_disable = vkraster->depthClampEnable;
3663
3664 const VkPipelineRasterizationDepthClipStateCreateInfoEXT *depth_clip_state =
3665 vk_find_struct_const(vkraster->pNext, PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT);
3666 if (depth_clip_state) {
3667 depth_clip_disable = !depth_clip_state->depthClipEnable;
3668 }
3669
3670 radeon_set_context_reg(ctx_cs, R_028810_PA_CL_CLIP_CNTL,
3671 S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.
3672 S_028810_ZCLIP_NEAR_DISABLE(depth_clip_disable ? 1 : 0) |
3673 S_028810_ZCLIP_FAR_DISABLE(depth_clip_disable ? 1 : 0) |
3674 S_028810_DX_RASTERIZATION_KILL(vkraster->rasterizerDiscardEnable ? 1 : 0) |
3675 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1));
3676
3677 radeon_set_context_reg(ctx_cs, R_0286D4_SPI_INTERP_CONTROL_0,
3678 S_0286D4_FLAT_SHADE_ENA(1) |
3679 S_0286D4_PNT_SPRITE_ENA(1) |
3680 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
3681 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
3682 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
3683 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
3684 S_0286D4_PNT_SPRITE_TOP_1(0)); /* vulkan is top to bottom - 1.0 at bottom */
3685
3686 radeon_set_context_reg(ctx_cs, R_028BE4_PA_SU_VTX_CNTL,
3687 S_028BE4_PIX_CENTER(1) | // TODO verify
3688 S_028BE4_ROUND_MODE(V_028BE4_X_ROUND_TO_EVEN) |
3689 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH));
3690
3691 radeon_set_context_reg(ctx_cs, R_028814_PA_SU_SC_MODE_CNTL,
3692 S_028814_FACE(vkraster->frontFace) |
3693 S_028814_CULL_FRONT(!!(vkraster->cullMode & VK_CULL_MODE_FRONT_BIT)) |
3694 S_028814_CULL_BACK(!!(vkraster->cullMode & VK_CULL_MODE_BACK_BIT)) |
3695 S_028814_POLY_MODE(vkraster->polygonMode != VK_POLYGON_MODE_FILL) |
3696 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(vkraster->polygonMode)) |
3697 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(vkraster->polygonMode)) |
3698 S_028814_POLY_OFFSET_FRONT_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
3699 S_028814_POLY_OFFSET_BACK_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
3700 S_028814_POLY_OFFSET_PARA_ENABLE(vkraster->depthBiasEnable ? 1 : 0));
3701
3702 /* Conservative rasterization. */
3703 if (mode != VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT) {
3704 struct radv_multisample_state *ms = &pipeline->graphics.ms;
3705
3706 ms->pa_sc_aa_config |= S_028BE0_AA_MASK_CENTROID_DTMN(1);
3707 ms->db_eqaa |= S_028804_ENABLE_POSTZ_OVERRASTERIZATION(1) |
3708 S_028804_OVERRASTERIZATION_AMOUNT(4);
3709
3710 pa_sc_conservative_rast = S_028C4C_PREZ_AA_MASK_ENABLE(1) |
3711 S_028C4C_POSTZ_AA_MASK_ENABLE(1) |
3712 S_028C4C_CENTROID_SAMPLE_OVERRIDE(1);
3713
3714 if (mode == VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT) {
3715 pa_sc_conservative_rast |=
3716 S_028C4C_OVER_RAST_ENABLE(1) |
3717 S_028C4C_OVER_RAST_SAMPLE_SELECT(0) |
3718 S_028C4C_UNDER_RAST_ENABLE(0) |
3719 S_028C4C_UNDER_RAST_SAMPLE_SELECT(1) |
3720 S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(1);
3721 } else {
3722 assert(mode == VK_CONSERVATIVE_RASTERIZATION_MODE_UNDERESTIMATE_EXT);
3723 pa_sc_conservative_rast |=
3724 S_028C4C_OVER_RAST_ENABLE(0) |
3725 S_028C4C_OVER_RAST_SAMPLE_SELECT(1) |
3726 S_028C4C_UNDER_RAST_ENABLE(1) |
3727 S_028C4C_UNDER_RAST_SAMPLE_SELECT(0) |
3728 S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(0);
3729 }
3730 }
3731
3732 radeon_set_context_reg(ctx_cs, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL,
3733 pa_sc_conservative_rast);
3734 }
3735
3736
3737 static void
3738 radv_pipeline_generate_multisample_state(struct radeon_cmdbuf *ctx_cs,
3739 struct radv_pipeline *pipeline)
3740 {
3741 struct radv_multisample_state *ms = &pipeline->graphics.ms;
3742
3743 radeon_set_context_reg_seq(ctx_cs, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0, 2);
3744 radeon_emit(ctx_cs, ms->pa_sc_aa_mask[0]);
3745 radeon_emit(ctx_cs, ms->pa_sc_aa_mask[1]);
3746
3747 radeon_set_context_reg(ctx_cs, R_028804_DB_EQAA, ms->db_eqaa);
3748 radeon_set_context_reg(ctx_cs, R_028A48_PA_SC_MODE_CNTL_0, ms->pa_sc_mode_cntl_0);
3749 radeon_set_context_reg(ctx_cs, R_028A4C_PA_SC_MODE_CNTL_1, ms->pa_sc_mode_cntl_1);
3750 radeon_set_context_reg(ctx_cs, R_028BDC_PA_SC_LINE_CNTL, ms->pa_sc_line_cntl);
3751 radeon_set_context_reg(ctx_cs, R_028BE0_PA_SC_AA_CONFIG, ms->pa_sc_aa_config);
3752
3753 /* The exclusion bits can be set to improve rasterization efficiency
3754 * if no sample lies on the pixel boundary (-8 sample offset). It's
3755 * currently always TRUE because the driver doesn't support 16 samples.
3756 */
3757 bool exclusion = pipeline->device->physical_device->rad_info.chip_class >= GFX7;
3758 radeon_set_context_reg(ctx_cs, R_02882C_PA_SU_PRIM_FILTER_CNTL,
3759 S_02882C_XMAX_RIGHT_EXCLUSION(exclusion) |
3760 S_02882C_YMAX_BOTTOM_EXCLUSION(exclusion));
3761
3762 /* GFX9: Flush DFSM when the AA mode changes. */
3763 if (pipeline->device->dfsm_allowed) {
3764 radeon_emit(ctx_cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
3765 radeon_emit(ctx_cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
3766 }
3767 }
3768
3769 static void
3770 radv_pipeline_generate_vgt_gs_mode(struct radeon_cmdbuf *ctx_cs,
3771 struct radv_pipeline *pipeline)
3772 {
3773 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3774 const struct radv_shader_variant *vs =
3775 pipeline->shaders[MESA_SHADER_TESS_EVAL] ?
3776 pipeline->shaders[MESA_SHADER_TESS_EVAL] :
3777 pipeline->shaders[MESA_SHADER_VERTEX];
3778 unsigned vgt_primitiveid_en = 0;
3779 uint32_t vgt_gs_mode = 0;
3780
3781 if (radv_pipeline_has_ngg(pipeline))
3782 return;
3783
3784 if (radv_pipeline_has_gs(pipeline)) {
3785 const struct radv_shader_variant *gs =
3786 pipeline->shaders[MESA_SHADER_GEOMETRY];
3787
3788 vgt_gs_mode = ac_vgt_gs_mode(gs->info.gs.vertices_out,
3789 pipeline->device->physical_device->rad_info.chip_class);
3790 } else if (outinfo->export_prim_id || vs->info.uses_prim_id) {
3791 vgt_gs_mode = S_028A40_MODE(V_028A40_GS_SCENARIO_A);
3792 vgt_primitiveid_en |= S_028A84_PRIMITIVEID_EN(1);
3793 }
3794
3795 radeon_set_context_reg(ctx_cs, R_028A84_VGT_PRIMITIVEID_EN, vgt_primitiveid_en);
3796 radeon_set_context_reg(ctx_cs, R_028A40_VGT_GS_MODE, vgt_gs_mode);
3797 }
3798
3799 static void
3800 radv_pipeline_generate_hw_vs(struct radeon_cmdbuf *ctx_cs,
3801 struct radeon_cmdbuf *cs,
3802 struct radv_pipeline *pipeline,
3803 struct radv_shader_variant *shader)
3804 {
3805 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3806
3807 radeon_set_sh_reg_seq(cs, R_00B120_SPI_SHADER_PGM_LO_VS, 4);
3808 radeon_emit(cs, va >> 8);
3809 radeon_emit(cs, S_00B124_MEM_BASE(va >> 40));
3810 radeon_emit(cs, shader->config.rsrc1);
3811 radeon_emit(cs, shader->config.rsrc2);
3812
3813 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3814 unsigned clip_dist_mask, cull_dist_mask, total_mask;
3815 clip_dist_mask = outinfo->clip_dist_mask;
3816 cull_dist_mask = outinfo->cull_dist_mask;
3817 total_mask = clip_dist_mask | cull_dist_mask;
3818 bool misc_vec_ena = outinfo->writes_pointsize ||
3819 outinfo->writes_layer ||
3820 outinfo->writes_viewport_index;
3821 unsigned spi_vs_out_config, nparams;
3822
3823 /* VS is required to export at least one param. */
3824 nparams = MAX2(outinfo->param_exports, 1);
3825 spi_vs_out_config = S_0286C4_VS_EXPORT_COUNT(nparams - 1);
3826
3827 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3828 spi_vs_out_config |= S_0286C4_NO_PC_EXPORT(outinfo->param_exports == 0);
3829 }
3830
3831 radeon_set_context_reg(ctx_cs, R_0286C4_SPI_VS_OUT_CONFIG, spi_vs_out_config);
3832
3833 radeon_set_context_reg(ctx_cs, R_02870C_SPI_SHADER_POS_FORMAT,
3834 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
3835 S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ?
3836 V_02870C_SPI_SHADER_4COMP :
3837 V_02870C_SPI_SHADER_NONE) |
3838 S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ?
3839 V_02870C_SPI_SHADER_4COMP :
3840 V_02870C_SPI_SHADER_NONE) |
3841 S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ?
3842 V_02870C_SPI_SHADER_4COMP :
3843 V_02870C_SPI_SHADER_NONE));
3844
3845 radeon_set_context_reg(ctx_cs, R_028818_PA_CL_VTE_CNTL,
3846 S_028818_VTX_W0_FMT(1) |
3847 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
3848 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
3849 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
3850
3851 radeon_set_context_reg(ctx_cs, R_02881C_PA_CL_VS_OUT_CNTL,
3852 S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |
3853 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |
3854 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |
3855 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |
3856 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |
3857 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |
3858 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |
3859 cull_dist_mask << 8 |
3860 clip_dist_mask);
3861
3862 if (pipeline->device->physical_device->rad_info.chip_class <= GFX8)
3863 radeon_set_context_reg(ctx_cs, R_028AB4_VGT_REUSE_OFF,
3864 outinfo->writes_viewport_index);
3865 }
3866
3867 static void
3868 radv_pipeline_generate_hw_es(struct radeon_cmdbuf *cs,
3869 struct radv_pipeline *pipeline,
3870 struct radv_shader_variant *shader)
3871 {
3872 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3873
3874 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 4);
3875 radeon_emit(cs, va >> 8);
3876 radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));
3877 radeon_emit(cs, shader->config.rsrc1);
3878 radeon_emit(cs, shader->config.rsrc2);
3879 }
3880
3881 static void
3882 radv_pipeline_generate_hw_ls(struct radeon_cmdbuf *cs,
3883 struct radv_pipeline *pipeline,
3884 struct radv_shader_variant *shader,
3885 const struct radv_tessellation_state *tess)
3886 {
3887 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3888 uint32_t rsrc2 = shader->config.rsrc2;
3889
3890 radeon_set_sh_reg_seq(cs, R_00B520_SPI_SHADER_PGM_LO_LS, 2);
3891 radeon_emit(cs, va >> 8);
3892 radeon_emit(cs, S_00B524_MEM_BASE(va >> 40));
3893
3894 rsrc2 |= S_00B52C_LDS_SIZE(tess->lds_size);
3895 if (pipeline->device->physical_device->rad_info.chip_class == GFX7 &&
3896 pipeline->device->physical_device->rad_info.family != CHIP_HAWAII)
3897 radeon_set_sh_reg(cs, R_00B52C_SPI_SHADER_PGM_RSRC2_LS, rsrc2);
3898
3899 radeon_set_sh_reg_seq(cs, R_00B528_SPI_SHADER_PGM_RSRC1_LS, 2);
3900 radeon_emit(cs, shader->config.rsrc1);
3901 radeon_emit(cs, rsrc2);
3902 }
3903
3904 static void
3905 radv_pipeline_generate_hw_ngg(struct radeon_cmdbuf *ctx_cs,
3906 struct radeon_cmdbuf *cs,
3907 struct radv_pipeline *pipeline,
3908 struct radv_shader_variant *shader)
3909 {
3910 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3911 gl_shader_stage es_type =
3912 radv_pipeline_has_tess(pipeline) ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
3913 struct radv_shader_variant *es =
3914 es_type == MESA_SHADER_TESS_EVAL ? pipeline->shaders[MESA_SHADER_TESS_EVAL] : pipeline->shaders[MESA_SHADER_VERTEX];
3915 const struct gfx10_ngg_info *ngg_state = &shader->info.ngg_info;
3916
3917 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 2);
3918 radeon_emit(cs, va >> 8);
3919 radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));
3920 radeon_set_sh_reg_seq(cs, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 2);
3921 radeon_emit(cs, shader->config.rsrc1);
3922 radeon_emit(cs, shader->config.rsrc2);
3923
3924 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3925 unsigned clip_dist_mask, cull_dist_mask, total_mask;
3926 clip_dist_mask = outinfo->clip_dist_mask;
3927 cull_dist_mask = outinfo->cull_dist_mask;
3928 total_mask = clip_dist_mask | cull_dist_mask;
3929 bool misc_vec_ena = outinfo->writes_pointsize ||
3930 outinfo->writes_layer ||
3931 outinfo->writes_viewport_index;
3932 bool es_enable_prim_id = outinfo->export_prim_id ||
3933 (es && es->info.uses_prim_id);
3934 bool break_wave_at_eoi = false;
3935 unsigned ge_cntl;
3936 unsigned nparams;
3937
3938 if (es_type == MESA_SHADER_TESS_EVAL) {
3939 struct radv_shader_variant *gs =
3940 pipeline->shaders[MESA_SHADER_GEOMETRY];
3941
3942 if (es_enable_prim_id || (gs && gs->info.uses_prim_id))
3943 break_wave_at_eoi = true;
3944 }
3945
3946 nparams = MAX2(outinfo->param_exports, 1);
3947 radeon_set_context_reg(ctx_cs, R_0286C4_SPI_VS_OUT_CONFIG,
3948 S_0286C4_VS_EXPORT_COUNT(nparams - 1) |
3949 S_0286C4_NO_PC_EXPORT(outinfo->param_exports == 0));
3950
3951 radeon_set_context_reg(ctx_cs, R_028708_SPI_SHADER_IDX_FORMAT,
3952 S_028708_IDX0_EXPORT_FORMAT(V_028708_SPI_SHADER_1COMP));
3953 radeon_set_context_reg(ctx_cs, R_02870C_SPI_SHADER_POS_FORMAT,
3954 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
3955 S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ?
3956 V_02870C_SPI_SHADER_4COMP :
3957 V_02870C_SPI_SHADER_NONE) |
3958 S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ?
3959 V_02870C_SPI_SHADER_4COMP :
3960 V_02870C_SPI_SHADER_NONE) |
3961 S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ?
3962 V_02870C_SPI_SHADER_4COMP :
3963 V_02870C_SPI_SHADER_NONE));
3964
3965 radeon_set_context_reg(ctx_cs, R_028818_PA_CL_VTE_CNTL,
3966 S_028818_VTX_W0_FMT(1) |
3967 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
3968 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
3969 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
3970 radeon_set_context_reg(ctx_cs, R_02881C_PA_CL_VS_OUT_CNTL,
3971 S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |
3972 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |
3973 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |
3974 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |
3975 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |
3976 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |
3977 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |
3978 cull_dist_mask << 8 |
3979 clip_dist_mask);
3980
3981 radeon_set_context_reg(ctx_cs, R_028A84_VGT_PRIMITIVEID_EN,
3982 S_028A84_PRIMITIVEID_EN(es_enable_prim_id) |
3983 S_028A84_NGG_DISABLE_PROVOK_REUSE(outinfo->export_prim_id));
3984
3985 radeon_set_context_reg(ctx_cs, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
3986 ngg_state->vgt_esgs_ring_itemsize);
3987
3988 /* NGG specific registers. */
3989 struct radv_shader_variant *gs = pipeline->shaders[MESA_SHADER_GEOMETRY];
3990 uint32_t gs_num_invocations = gs ? gs->info.gs.invocations : 1;
3991
3992 radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL,
3993 S_028A44_ES_VERTS_PER_SUBGRP(ngg_state->hw_max_esverts) |
3994 S_028A44_GS_PRIMS_PER_SUBGRP(ngg_state->max_gsprims) |
3995 S_028A44_GS_INST_PRIMS_IN_SUBGRP(ngg_state->max_gsprims * gs_num_invocations));
3996 radeon_set_context_reg(ctx_cs, R_0287FC_GE_MAX_OUTPUT_PER_SUBGROUP,
3997 S_0287FC_MAX_VERTS_PER_SUBGROUP(ngg_state->max_out_verts));
3998 radeon_set_context_reg(ctx_cs, R_028B4C_GE_NGG_SUBGRP_CNTL,
3999 S_028B4C_PRIM_AMP_FACTOR(ngg_state->prim_amp_factor) |
4000 S_028B4C_THDS_PER_SUBGRP(0)); /* for fast launch */
4001 radeon_set_context_reg(ctx_cs, R_028B90_VGT_GS_INSTANCE_CNT,
4002 S_028B90_CNT(gs_num_invocations) |
4003 S_028B90_ENABLE(gs_num_invocations > 1) |
4004 S_028B90_EN_MAX_VERT_OUT_PER_GS_INSTANCE(ngg_state->max_vert_out_per_gs_instance));
4005
4006 /* User edge flags are set by the pos exports. If user edge flags are
4007 * not used, we must use hw-generated edge flags and pass them via
4008 * the prim export to prevent drawing lines on internal edges of
4009 * decomposed primitives (such as quads) with polygon mode = lines.
4010 *
4011 * TODO: We should combine hw-generated edge flags with user edge
4012 * flags in the shader.
4013 */
4014 radeon_set_context_reg(ctx_cs, R_028838_PA_CL_NGG_CNTL,
4015 S_028838_INDEX_BUF_EDGE_FLAG_ENA(!radv_pipeline_has_tess(pipeline) &&
4016 !radv_pipeline_has_gs(pipeline)));
4017
4018 ge_cntl = S_03096C_PRIM_GRP_SIZE(ngg_state->max_gsprims) |
4019 S_03096C_VERT_GRP_SIZE(256) | /* 256 = disable vertex grouping */
4020 S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi);
4021
4022 /* Bug workaround for a possible hang with non-tessellation cases.
4023 * Tessellation always sets GE_CNTL.VERT_GRP_SIZE = 0
4024 *
4025 * Requirement: GE_CNTL.VERT_GRP_SIZE = VGT_GS_ONCHIP_CNTL.ES_VERTS_PER_SUBGRP - 5
4026 */
4027 if ((pipeline->device->physical_device->rad_info.family == CHIP_NAVI10 ||
4028 pipeline->device->physical_device->rad_info.family == CHIP_NAVI12 ||
4029 pipeline->device->physical_device->rad_info.family == CHIP_NAVI14) &&
4030 !radv_pipeline_has_tess(pipeline) &&
4031 ngg_state->hw_max_esverts != 256) {
4032 ge_cntl &= C_03096C_VERT_GRP_SIZE;
4033
4034 if (ngg_state->hw_max_esverts > 5) {
4035 ge_cntl |= S_03096C_VERT_GRP_SIZE(ngg_state->hw_max_esverts - 5);
4036 }
4037 }
4038
4039 radeon_set_uconfig_reg(ctx_cs, R_03096C_GE_CNTL, ge_cntl);
4040 }
4041
4042 static void
4043 radv_pipeline_generate_hw_hs(struct radeon_cmdbuf *cs,
4044 struct radv_pipeline *pipeline,
4045 struct radv_shader_variant *shader,
4046 const struct radv_tessellation_state *tess)
4047 {
4048 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
4049
4050 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {
4051 unsigned hs_rsrc2 = shader->config.rsrc2;
4052
4053 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
4054 hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX10(tess->lds_size);
4055 } else {
4056 hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX9(tess->lds_size);
4057 }
4058
4059 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
4060 radeon_set_sh_reg_seq(cs, R_00B520_SPI_SHADER_PGM_LO_LS, 2);
4061 radeon_emit(cs, va >> 8);
4062 radeon_emit(cs, S_00B524_MEM_BASE(va >> 40));
4063 } else {
4064 radeon_set_sh_reg_seq(cs, R_00B410_SPI_SHADER_PGM_LO_LS, 2);
4065 radeon_emit(cs, va >> 8);
4066 radeon_emit(cs, S_00B414_MEM_BASE(va >> 40));
4067 }
4068
4069 radeon_set_sh_reg_seq(cs, R_00B428_SPI_SHADER_PGM_RSRC1_HS, 2);
4070 radeon_emit(cs, shader->config.rsrc1);
4071 radeon_emit(cs, hs_rsrc2);
4072 } else {
4073 radeon_set_sh_reg_seq(cs, R_00B420_SPI_SHADER_PGM_LO_HS, 4);
4074 radeon_emit(cs, va >> 8);
4075 radeon_emit(cs, S_00B424_MEM_BASE(va >> 40));
4076 radeon_emit(cs, shader->config.rsrc1);
4077 radeon_emit(cs, shader->config.rsrc2);
4078 }
4079 }
4080
4081 static void
4082 radv_pipeline_generate_vertex_shader(struct radeon_cmdbuf *ctx_cs,
4083 struct radeon_cmdbuf *cs,
4084 struct radv_pipeline *pipeline,
4085 const struct radv_tessellation_state *tess)
4086 {
4087 struct radv_shader_variant *vs;
4088
4089 /* Skip shaders merged into HS/GS */
4090 vs = pipeline->shaders[MESA_SHADER_VERTEX];
4091 if (!vs)
4092 return;
4093
4094 if (vs->info.vs.as_ls)
4095 radv_pipeline_generate_hw_ls(cs, pipeline, vs, tess);
4096 else if (vs->info.vs.as_es)
4097 radv_pipeline_generate_hw_es(cs, pipeline, vs);
4098 else if (vs->info.is_ngg)
4099 radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, vs);
4100 else
4101 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, vs);
4102 }
4103
4104 static void
4105 radv_pipeline_generate_tess_shaders(struct radeon_cmdbuf *ctx_cs,
4106 struct radeon_cmdbuf *cs,
4107 struct radv_pipeline *pipeline,
4108 const struct radv_tessellation_state *tess)
4109 {
4110 if (!radv_pipeline_has_tess(pipeline))
4111 return;
4112
4113 struct radv_shader_variant *tes, *tcs;
4114
4115 tcs = pipeline->shaders[MESA_SHADER_TESS_CTRL];
4116 tes = pipeline->shaders[MESA_SHADER_TESS_EVAL];
4117
4118 if (tes) {
4119 if (tes->info.is_ngg) {
4120 radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, tes);
4121 } else if (tes->info.tes.as_es)
4122 radv_pipeline_generate_hw_es(cs, pipeline, tes);
4123 else
4124 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, tes);
4125 }
4126
4127 radv_pipeline_generate_hw_hs(cs, pipeline, tcs, tess);
4128
4129 radeon_set_context_reg(ctx_cs, R_028B6C_VGT_TF_PARAM,
4130 tess->tf_param);
4131
4132 if (pipeline->device->physical_device->rad_info.chip_class >= GFX7)
4133 radeon_set_context_reg_idx(ctx_cs, R_028B58_VGT_LS_HS_CONFIG, 2,
4134 tess->ls_hs_config);
4135 else
4136 radeon_set_context_reg(ctx_cs, R_028B58_VGT_LS_HS_CONFIG,
4137 tess->ls_hs_config);
4138
4139 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10 &&
4140 !radv_pipeline_has_gs(pipeline) && !radv_pipeline_has_ngg(pipeline)) {
4141 radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL,
4142 S_028A44_ES_VERTS_PER_SUBGRP(250) |
4143 S_028A44_GS_PRIMS_PER_SUBGRP(126) |
4144 S_028A44_GS_INST_PRIMS_IN_SUBGRP(126));
4145 }
4146 }
4147
4148 static void
4149 radv_pipeline_generate_hw_gs(struct radeon_cmdbuf *ctx_cs,
4150 struct radeon_cmdbuf *cs,
4151 struct radv_pipeline *pipeline,
4152 struct radv_shader_variant *gs)
4153 {
4154 const struct gfx9_gs_info *gs_state = &gs->info.gs_ring_info;
4155 unsigned gs_max_out_vertices;
4156 uint8_t *num_components;
4157 uint8_t max_stream;
4158 unsigned offset;
4159 uint64_t va;
4160
4161 gs_max_out_vertices = gs->info.gs.vertices_out;
4162 max_stream = gs->info.gs.max_stream;
4163 num_components = gs->info.gs.num_stream_output_components;
4164
4165 offset = num_components[0] * gs_max_out_vertices;
4166
4167 radeon_set_context_reg_seq(ctx_cs, R_028A60_VGT_GSVS_RING_OFFSET_1, 3);
4168 radeon_emit(ctx_cs, offset);
4169 if (max_stream >= 1)
4170 offset += num_components[1] * gs_max_out_vertices;
4171 radeon_emit(ctx_cs, offset);
4172 if (max_stream >= 2)
4173 offset += num_components[2] * gs_max_out_vertices;
4174 radeon_emit(ctx_cs, offset);
4175 if (max_stream >= 3)
4176 offset += num_components[3] * gs_max_out_vertices;
4177 radeon_set_context_reg(ctx_cs, R_028AB0_VGT_GSVS_RING_ITEMSIZE, offset);
4178
4179 radeon_set_context_reg_seq(ctx_cs, R_028B5C_VGT_GS_VERT_ITEMSIZE, 4);
4180 radeon_emit(ctx_cs, num_components[0]);
4181 radeon_emit(ctx_cs, (max_stream >= 1) ? num_components[1] : 0);
4182 radeon_emit(ctx_cs, (max_stream >= 2) ? num_components[2] : 0);
4183 radeon_emit(ctx_cs, (max_stream >= 3) ? num_components[3] : 0);
4184
4185 uint32_t gs_num_invocations = gs->info.gs.invocations;
4186 radeon_set_context_reg(ctx_cs, R_028B90_VGT_GS_INSTANCE_CNT,
4187 S_028B90_CNT(MIN2(gs_num_invocations, 127)) |
4188 S_028B90_ENABLE(gs_num_invocations > 0));
4189
4190 radeon_set_context_reg(ctx_cs, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
4191 gs_state->vgt_esgs_ring_itemsize);
4192
4193 va = radv_buffer_get_va(gs->bo) + gs->bo_offset;
4194
4195 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {
4196 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
4197 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 2);
4198 radeon_emit(cs, va >> 8);
4199 radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));
4200 } else {
4201 radeon_set_sh_reg_seq(cs, R_00B210_SPI_SHADER_PGM_LO_ES, 2);
4202 radeon_emit(cs, va >> 8);
4203 radeon_emit(cs, S_00B214_MEM_BASE(va >> 40));
4204 }
4205
4206 radeon_set_sh_reg_seq(cs, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 2);
4207 radeon_emit(cs, gs->config.rsrc1);
4208 radeon_emit(cs, gs->config.rsrc2 | S_00B22C_LDS_SIZE(gs_state->lds_size));
4209
4210 radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL, gs_state->vgt_gs_onchip_cntl);
4211 radeon_set_context_reg(ctx_cs, R_028A94_VGT_GS_MAX_PRIMS_PER_SUBGROUP, gs_state->vgt_gs_max_prims_per_subgroup);
4212 } else {
4213 radeon_set_sh_reg_seq(cs, R_00B220_SPI_SHADER_PGM_LO_GS, 4);
4214 radeon_emit(cs, va >> 8);
4215 radeon_emit(cs, S_00B224_MEM_BASE(va >> 40));
4216 radeon_emit(cs, gs->config.rsrc1);
4217 radeon_emit(cs, gs->config.rsrc2);
4218 }
4219
4220 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, pipeline->gs_copy_shader);
4221 }
4222
4223 static void
4224 radv_pipeline_generate_geometry_shader(struct radeon_cmdbuf *ctx_cs,
4225 struct radeon_cmdbuf *cs,
4226 struct radv_pipeline *pipeline)
4227 {
4228 struct radv_shader_variant *gs;
4229
4230 gs = pipeline->shaders[MESA_SHADER_GEOMETRY];
4231 if (!gs)
4232 return;
4233
4234 if (gs->info.is_ngg)
4235 radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, gs);
4236 else
4237 radv_pipeline_generate_hw_gs(ctx_cs, cs, pipeline, gs);
4238
4239 radeon_set_context_reg(ctx_cs, R_028B38_VGT_GS_MAX_VERT_OUT,
4240 gs->info.gs.vertices_out);
4241 }
4242
4243 static uint32_t offset_to_ps_input(uint32_t offset, bool flat_shade,
4244 bool explicit, bool float16)
4245 {
4246 uint32_t ps_input_cntl;
4247 if (offset <= AC_EXP_PARAM_OFFSET_31) {
4248 ps_input_cntl = S_028644_OFFSET(offset);
4249 if (flat_shade || explicit)
4250 ps_input_cntl |= S_028644_FLAT_SHADE(1);
4251 if (explicit) {
4252 /* Force parameter cache to be read in passthrough
4253 * mode.
4254 */
4255 ps_input_cntl |= S_028644_OFFSET(1 << 5);
4256 }
4257 if (float16) {
4258 ps_input_cntl |= S_028644_FP16_INTERP_MODE(1) |
4259 S_028644_ATTR0_VALID(1);
4260 }
4261 } else {
4262 /* The input is a DEFAULT_VAL constant. */
4263 assert(offset >= AC_EXP_PARAM_DEFAULT_VAL_0000 &&
4264 offset <= AC_EXP_PARAM_DEFAULT_VAL_1111);
4265 offset -= AC_EXP_PARAM_DEFAULT_VAL_0000;
4266 ps_input_cntl = S_028644_OFFSET(0x20) |
4267 S_028644_DEFAULT_VAL(offset);
4268 }
4269 return ps_input_cntl;
4270 }
4271
4272 static void
4273 radv_pipeline_generate_ps_inputs(struct radeon_cmdbuf *ctx_cs,
4274 struct radv_pipeline *pipeline)
4275 {
4276 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
4277 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
4278 uint32_t ps_input_cntl[32];
4279
4280 unsigned ps_offset = 0;
4281
4282 if (ps->info.ps.prim_id_input) {
4283 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID];
4284 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
4285 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false, false);
4286 ++ps_offset;
4287 }
4288 }
4289
4290 if (ps->info.ps.layer_input ||
4291 ps->info.needs_multiview_view_index) {
4292 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_LAYER];
4293 if (vs_offset != AC_EXP_PARAM_UNDEFINED)
4294 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false, false);
4295 else
4296 ps_input_cntl[ps_offset] = offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000, true, false, false);
4297 ++ps_offset;
4298 }
4299
4300 if (ps->info.ps.has_pcoord) {
4301 unsigned val;
4302 val = S_028644_PT_SPRITE_TEX(1) | S_028644_OFFSET(0x20);
4303 ps_input_cntl[ps_offset] = val;
4304 ps_offset++;
4305 }
4306
4307 if (ps->info.ps.num_input_clips_culls) {
4308 unsigned vs_offset;
4309
4310 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_CLIP_DIST0];
4311 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
4312 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, false, false, false);
4313 ++ps_offset;
4314 }
4315
4316 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_CLIP_DIST1];
4317 if (vs_offset != AC_EXP_PARAM_UNDEFINED &&
4318 ps->info.ps.num_input_clips_culls > 4) {
4319 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, false, false, false);
4320 ++ps_offset;
4321 }
4322 }
4323
4324 for (unsigned i = 0; i < 32 && (1u << i) <= ps->info.ps.input_mask; ++i) {
4325 unsigned vs_offset;
4326 bool flat_shade;
4327 bool explicit;
4328 bool float16;
4329 if (!(ps->info.ps.input_mask & (1u << i)))
4330 continue;
4331
4332 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_VAR0 + i];
4333 if (vs_offset == AC_EXP_PARAM_UNDEFINED) {
4334 ps_input_cntl[ps_offset] = S_028644_OFFSET(0x20);
4335 ++ps_offset;
4336 continue;
4337 }
4338
4339 flat_shade = !!(ps->info.ps.flat_shaded_mask & (1u << ps_offset));
4340 explicit = !!(ps->info.ps.explicit_shaded_mask & (1u << ps_offset));
4341 float16 = !!(ps->info.ps.float16_shaded_mask & (1u << ps_offset));
4342
4343 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, flat_shade, explicit, float16);
4344 ++ps_offset;
4345 }
4346
4347 if (ps_offset) {
4348 radeon_set_context_reg_seq(ctx_cs, R_028644_SPI_PS_INPUT_CNTL_0, ps_offset);
4349 for (unsigned i = 0; i < ps_offset; i++) {
4350 radeon_emit(ctx_cs, ps_input_cntl[i]);
4351 }
4352 }
4353 }
4354
4355 static uint32_t
4356 radv_compute_db_shader_control(const struct radv_device *device,
4357 const struct radv_pipeline *pipeline,
4358 const struct radv_shader_variant *ps)
4359 {
4360 unsigned z_order;
4361 if (ps->info.ps.early_fragment_test || !ps->info.ps.writes_memory)
4362 z_order = V_02880C_EARLY_Z_THEN_LATE_Z;
4363 else
4364 z_order = V_02880C_LATE_Z;
4365
4366 bool disable_rbplus = device->physical_device->rad_info.has_rbplus &&
4367 !device->physical_device->rad_info.rbplus_allowed;
4368
4369 /* It shouldn't be needed to export gl_SampleMask when MSAA is disabled
4370 * but this appears to break Project Cars (DXVK). See
4371 * https://bugs.freedesktop.org/show_bug.cgi?id=109401
4372 */
4373 bool mask_export_enable = ps->info.ps.writes_sample_mask;
4374
4375 return S_02880C_Z_EXPORT_ENABLE(ps->info.ps.writes_z) |
4376 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps->info.ps.writes_stencil) |
4377 S_02880C_KILL_ENABLE(!!ps->info.ps.can_discard) |
4378 S_02880C_MASK_EXPORT_ENABLE(mask_export_enable) |
4379 S_02880C_Z_ORDER(z_order) |
4380 S_02880C_DEPTH_BEFORE_SHADER(ps->info.ps.early_fragment_test) |
4381 S_02880C_PRE_SHADER_DEPTH_COVERAGE_ENABLE(ps->info.ps.post_depth_coverage) |
4382 S_02880C_EXEC_ON_HIER_FAIL(ps->info.ps.writes_memory) |
4383 S_02880C_EXEC_ON_NOOP(ps->info.ps.writes_memory) |
4384 S_02880C_DUAL_QUAD_DISABLE(disable_rbplus);
4385 }
4386
4387 static void
4388 radv_pipeline_generate_fragment_shader(struct radeon_cmdbuf *ctx_cs,
4389 struct radeon_cmdbuf *cs,
4390 struct radv_pipeline *pipeline)
4391 {
4392 struct radv_shader_variant *ps;
4393 uint64_t va;
4394 assert (pipeline->shaders[MESA_SHADER_FRAGMENT]);
4395
4396 ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
4397 va = radv_buffer_get_va(ps->bo) + ps->bo_offset;
4398
4399 radeon_set_sh_reg_seq(cs, R_00B020_SPI_SHADER_PGM_LO_PS, 4);
4400 radeon_emit(cs, va >> 8);
4401 radeon_emit(cs, S_00B024_MEM_BASE(va >> 40));
4402 radeon_emit(cs, ps->config.rsrc1);
4403 radeon_emit(cs, ps->config.rsrc2);
4404
4405 radeon_set_context_reg(ctx_cs, R_02880C_DB_SHADER_CONTROL,
4406 radv_compute_db_shader_control(pipeline->device,
4407 pipeline, ps));
4408
4409 radeon_set_context_reg(ctx_cs, R_0286CC_SPI_PS_INPUT_ENA,
4410 ps->config.spi_ps_input_ena);
4411
4412 radeon_set_context_reg(ctx_cs, R_0286D0_SPI_PS_INPUT_ADDR,
4413 ps->config.spi_ps_input_addr);
4414
4415 radeon_set_context_reg(ctx_cs, R_0286D8_SPI_PS_IN_CONTROL,
4416 S_0286D8_NUM_INTERP(ps->info.ps.num_interp) |
4417 S_0286D8_PS_W32_EN(ps->info.wave_size == 32));
4418
4419 radeon_set_context_reg(ctx_cs, R_0286E0_SPI_BARYC_CNTL, pipeline->graphics.spi_baryc_cntl);
4420
4421 radeon_set_context_reg(ctx_cs, R_028710_SPI_SHADER_Z_FORMAT,
4422 ac_get_spi_shader_z_format(ps->info.ps.writes_z,
4423 ps->info.ps.writes_stencil,
4424 ps->info.ps.writes_sample_mask));
4425
4426 if (pipeline->device->dfsm_allowed) {
4427 /* optimise this? */
4428 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
4429 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
4430 }
4431 }
4432
4433 static void
4434 radv_pipeline_generate_vgt_vertex_reuse(struct radeon_cmdbuf *ctx_cs,
4435 struct radv_pipeline *pipeline)
4436 {
4437 if (pipeline->device->physical_device->rad_info.family < CHIP_POLARIS10 ||
4438 pipeline->device->physical_device->rad_info.chip_class >= GFX10)
4439 return;
4440
4441 unsigned vtx_reuse_depth = 30;
4442 if (radv_pipeline_has_tess(pipeline) &&
4443 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.tes.spacing == TESS_SPACING_FRACTIONAL_ODD) {
4444 vtx_reuse_depth = 14;
4445 }
4446 radeon_set_context_reg(ctx_cs, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL,
4447 S_028C58_VTX_REUSE_DEPTH(vtx_reuse_depth));
4448 }
4449
4450 static uint32_t
4451 radv_compute_vgt_shader_stages_en(const struct radv_pipeline *pipeline)
4452 {
4453 uint32_t stages = 0;
4454 if (radv_pipeline_has_tess(pipeline)) {
4455 stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) |
4456 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
4457
4458 if (radv_pipeline_has_gs(pipeline))
4459 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) |
4460 S_028B54_GS_EN(1);
4461 else if (radv_pipeline_has_ngg(pipeline))
4462 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS);
4463 else
4464 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS);
4465 } else if (radv_pipeline_has_gs(pipeline)) {
4466 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) |
4467 S_028B54_GS_EN(1);
4468 } else if (radv_pipeline_has_ngg(pipeline)) {
4469 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL);
4470 }
4471
4472 if (radv_pipeline_has_ngg(pipeline)) {
4473 stages |= S_028B54_PRIMGEN_EN(1);
4474 if (pipeline->streamout_shader)
4475 stages |= S_028B54_NGG_WAVE_ID_EN(1);
4476 if (radv_pipeline_has_ngg_passthrough(pipeline))
4477 stages |= S_028B54_PRIMGEN_PASSTHRU_EN(1);
4478 } else if (radv_pipeline_has_gs(pipeline)) {
4479 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
4480 }
4481
4482 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9)
4483 stages |= S_028B54_MAX_PRIMGRP_IN_WAVE(2);
4484
4485 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
4486 uint8_t hs_size = 64, gs_size = 64, vs_size = 64;
4487
4488 if (radv_pipeline_has_tess(pipeline))
4489 hs_size = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.wave_size;
4490
4491 if (pipeline->shaders[MESA_SHADER_GEOMETRY]) {
4492 vs_size = gs_size = pipeline->shaders[MESA_SHADER_GEOMETRY]->info.wave_size;
4493 if (pipeline->gs_copy_shader)
4494 vs_size = pipeline->gs_copy_shader->info.wave_size;
4495 } else if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
4496 vs_size = pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.wave_size;
4497 else if (pipeline->shaders[MESA_SHADER_VERTEX])
4498 vs_size = pipeline->shaders[MESA_SHADER_VERTEX]->info.wave_size;
4499
4500 if (radv_pipeline_has_ngg(pipeline))
4501 gs_size = vs_size;
4502
4503 /* legacy GS only supports Wave64 */
4504 stages |= S_028B54_HS_W32_EN(hs_size == 32 ? 1 : 0) |
4505 S_028B54_GS_W32_EN(gs_size == 32 ? 1 : 0) |
4506 S_028B54_VS_W32_EN(vs_size == 32 ? 1 : 0);
4507 }
4508
4509 return stages;
4510 }
4511
4512 static uint32_t
4513 radv_compute_cliprect_rule(const VkGraphicsPipelineCreateInfo *pCreateInfo)
4514 {
4515 const VkPipelineDiscardRectangleStateCreateInfoEXT *discard_rectangle_info =
4516 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);
4517
4518 if (!discard_rectangle_info)
4519 return 0xffff;
4520
4521 unsigned mask = 0;
4522
4523 for (unsigned i = 0; i < (1u << MAX_DISCARD_RECTANGLES); ++i) {
4524 /* Interpret i as a bitmask, and then set the bit in the mask if
4525 * that combination of rectangles in which the pixel is contained
4526 * should pass the cliprect test. */
4527 unsigned relevant_subset = i & ((1u << discard_rectangle_info->discardRectangleCount) - 1);
4528
4529 if (discard_rectangle_info->discardRectangleMode == VK_DISCARD_RECTANGLE_MODE_INCLUSIVE_EXT &&
4530 !relevant_subset)
4531 continue;
4532
4533 if (discard_rectangle_info->discardRectangleMode == VK_DISCARD_RECTANGLE_MODE_EXCLUSIVE_EXT &&
4534 relevant_subset)
4535 continue;
4536
4537 mask |= 1u << i;
4538 }
4539
4540 return mask;
4541 }
4542
4543 static void
4544 gfx10_pipeline_generate_ge_cntl(struct radeon_cmdbuf *ctx_cs,
4545 struct radv_pipeline *pipeline,
4546 const struct radv_tessellation_state *tess)
4547 {
4548 bool break_wave_at_eoi = false;
4549 unsigned primgroup_size;
4550 unsigned vertgroup_size = 256; /* 256 = disable vertex grouping */
4551
4552 if (radv_pipeline_has_tess(pipeline)) {
4553 primgroup_size = tess->num_patches; /* must be a multiple of NUM_PATCHES */
4554 } else if (radv_pipeline_has_gs(pipeline)) {
4555 const struct gfx9_gs_info *gs_state =
4556 &pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs_ring_info;
4557 unsigned vgt_gs_onchip_cntl = gs_state->vgt_gs_onchip_cntl;
4558 primgroup_size = G_028A44_GS_PRIMS_PER_SUBGRP(vgt_gs_onchip_cntl);
4559 } else {
4560 primgroup_size = 128; /* recommended without a GS and tess */
4561 }
4562
4563 if (radv_pipeline_has_tess(pipeline)) {
4564 if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.uses_prim_id ||
4565 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.uses_prim_id)
4566 break_wave_at_eoi = true;
4567 }
4568
4569 radeon_set_uconfig_reg(ctx_cs, R_03096C_GE_CNTL,
4570 S_03096C_PRIM_GRP_SIZE(primgroup_size) |
4571 S_03096C_VERT_GRP_SIZE(vertgroup_size) |
4572 S_03096C_PACKET_TO_ONE_PA(0) /* line stipple */ |
4573 S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi));
4574 }
4575
4576 static void
4577 radv_pipeline_generate_pm4(struct radv_pipeline *pipeline,
4578 const VkGraphicsPipelineCreateInfo *pCreateInfo,
4579 const struct radv_graphics_pipeline_create_info *extra,
4580 const struct radv_blend_state *blend,
4581 const struct radv_tessellation_state *tess,
4582 unsigned prim, unsigned gs_out)
4583 {
4584 struct radeon_cmdbuf *ctx_cs = &pipeline->ctx_cs;
4585 struct radeon_cmdbuf *cs = &pipeline->cs;
4586
4587 cs->max_dw = 64;
4588 ctx_cs->max_dw = 256;
4589 cs->buf = malloc(4 * (cs->max_dw + ctx_cs->max_dw));
4590 ctx_cs->buf = cs->buf + cs->max_dw;
4591
4592 radv_pipeline_generate_depth_stencil_state(ctx_cs, pipeline, pCreateInfo, extra);
4593 radv_pipeline_generate_blend_state(ctx_cs, pipeline, blend);
4594 radv_pipeline_generate_raster_state(ctx_cs, pipeline, pCreateInfo);
4595 radv_pipeline_generate_multisample_state(ctx_cs, pipeline);
4596 radv_pipeline_generate_vgt_gs_mode(ctx_cs, pipeline);
4597 radv_pipeline_generate_vertex_shader(ctx_cs, cs, pipeline, tess);
4598 radv_pipeline_generate_tess_shaders(ctx_cs, cs, pipeline, tess);
4599 radv_pipeline_generate_geometry_shader(ctx_cs, cs, pipeline);
4600 radv_pipeline_generate_fragment_shader(ctx_cs, cs, pipeline);
4601 radv_pipeline_generate_ps_inputs(ctx_cs, pipeline);
4602 radv_pipeline_generate_vgt_vertex_reuse(ctx_cs, pipeline);
4603 radv_pipeline_generate_binning_state(ctx_cs, pipeline, pCreateInfo, blend);
4604
4605 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10 && !radv_pipeline_has_ngg(pipeline))
4606 gfx10_pipeline_generate_ge_cntl(ctx_cs, pipeline, tess);
4607
4608 radeon_set_context_reg(ctx_cs, R_028B54_VGT_SHADER_STAGES_EN, radv_compute_vgt_shader_stages_en(pipeline));
4609
4610 if (pipeline->device->physical_device->rad_info.chip_class >= GFX7) {
4611 radeon_set_uconfig_reg_idx(pipeline->device->physical_device,
4612 cs, R_030908_VGT_PRIMITIVE_TYPE, 1, prim);
4613 } else {
4614 radeon_set_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE, prim);
4615 }
4616 radeon_set_context_reg(ctx_cs, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out);
4617
4618 radeon_set_context_reg(ctx_cs, R_02820C_PA_SC_CLIPRECT_RULE, radv_compute_cliprect_rule(pCreateInfo));
4619
4620 pipeline->ctx_cs_hash = _mesa_hash_data(ctx_cs->buf, ctx_cs->cdw * 4);
4621
4622 assert(ctx_cs->cdw <= ctx_cs->max_dw);
4623 assert(cs->cdw <= cs->max_dw);
4624 }
4625
4626 static struct radv_ia_multi_vgt_param_helpers
4627 radv_compute_ia_multi_vgt_param_helpers(struct radv_pipeline *pipeline,
4628 const struct radv_tessellation_state *tess,
4629 uint32_t prim)
4630 {
4631 struct radv_ia_multi_vgt_param_helpers ia_multi_vgt_param = {0};
4632 const struct radv_device *device = pipeline->device;
4633
4634 if (radv_pipeline_has_tess(pipeline))
4635 ia_multi_vgt_param.primgroup_size = tess->num_patches;
4636 else if (radv_pipeline_has_gs(pipeline))
4637 ia_multi_vgt_param.primgroup_size = 64;
4638 else
4639 ia_multi_vgt_param.primgroup_size = 128; /* recommended without a GS */
4640
4641 /* GS requirement. */
4642 ia_multi_vgt_param.partial_es_wave = false;
4643 if (radv_pipeline_has_gs(pipeline) && device->physical_device->rad_info.chip_class <= GFX8)
4644 if (SI_GS_PER_ES / ia_multi_vgt_param.primgroup_size >= pipeline->device->gs_table_depth - 3)
4645 ia_multi_vgt_param.partial_es_wave = true;
4646
4647 ia_multi_vgt_param.wd_switch_on_eop = false;
4648 if (device->physical_device->rad_info.chip_class >= GFX7) {
4649 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
4650 * 4 shader engines. Set 1 to pass the assertion below.
4651 * The other cases are hardware requirements. */
4652 if (device->physical_device->rad_info.max_se < 4 ||
4653 prim == V_008958_DI_PT_POLYGON ||
4654 prim == V_008958_DI_PT_LINELOOP ||
4655 prim == V_008958_DI_PT_TRIFAN ||
4656 prim == V_008958_DI_PT_TRISTRIP_ADJ ||
4657 (pipeline->graphics.prim_restart_enable &&
4658 (device->physical_device->rad_info.family < CHIP_POLARIS10 ||
4659 (prim != V_008958_DI_PT_POINTLIST &&
4660 prim != V_008958_DI_PT_LINESTRIP))))
4661 ia_multi_vgt_param.wd_switch_on_eop = true;
4662 }
4663
4664 ia_multi_vgt_param.ia_switch_on_eoi = false;
4665 if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.ps.prim_id_input)
4666 ia_multi_vgt_param.ia_switch_on_eoi = true;
4667 if (radv_pipeline_has_gs(pipeline) &&
4668 pipeline->shaders[MESA_SHADER_GEOMETRY]->info.uses_prim_id)
4669 ia_multi_vgt_param.ia_switch_on_eoi = true;
4670 if (radv_pipeline_has_tess(pipeline)) {
4671 /* SWITCH_ON_EOI must be set if PrimID is used. */
4672 if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.uses_prim_id ||
4673 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.uses_prim_id)
4674 ia_multi_vgt_param.ia_switch_on_eoi = true;
4675 }
4676
4677 ia_multi_vgt_param.partial_vs_wave = false;
4678 if (radv_pipeline_has_tess(pipeline)) {
4679 /* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
4680 if ((device->physical_device->rad_info.family == CHIP_TAHITI ||
4681 device->physical_device->rad_info.family == CHIP_PITCAIRN ||
4682 device->physical_device->rad_info.family == CHIP_BONAIRE) &&
4683 radv_pipeline_has_gs(pipeline))
4684 ia_multi_vgt_param.partial_vs_wave = true;
4685 /* Needed for 028B6C_DISTRIBUTION_MODE != 0 */
4686 if (device->physical_device->rad_info.has_distributed_tess) {
4687 if (radv_pipeline_has_gs(pipeline)) {
4688 if (device->physical_device->rad_info.chip_class <= GFX8)
4689 ia_multi_vgt_param.partial_es_wave = true;
4690 } else {
4691 ia_multi_vgt_param.partial_vs_wave = true;
4692 }
4693 }
4694 }
4695
4696 /* Workaround for a VGT hang when strip primitive types are used with
4697 * primitive restart.
4698 */
4699 if (pipeline->graphics.prim_restart_enable &&
4700 (prim == V_008958_DI_PT_LINESTRIP ||
4701 prim == V_008958_DI_PT_TRISTRIP ||
4702 prim == V_008958_DI_PT_LINESTRIP_ADJ ||
4703 prim == V_008958_DI_PT_TRISTRIP_ADJ)) {
4704 ia_multi_vgt_param.partial_vs_wave = true;
4705 }
4706
4707 if (radv_pipeline_has_gs(pipeline)) {
4708 /* On these chips there is the possibility of a hang if the
4709 * pipeline uses a GS and partial_vs_wave is not set.
4710 *
4711 * This mostly does not hit 4-SE chips, as those typically set
4712 * ia_switch_on_eoi and then partial_vs_wave is set for pipelines
4713 * with GS due to another workaround.
4714 *
4715 * Reproducer: https://bugs.freedesktop.org/show_bug.cgi?id=109242
4716 */
4717 if (device->physical_device->rad_info.family == CHIP_TONGA ||
4718 device->physical_device->rad_info.family == CHIP_FIJI ||
4719 device->physical_device->rad_info.family == CHIP_POLARIS10 ||
4720 device->physical_device->rad_info.family == CHIP_POLARIS11 ||
4721 device->physical_device->rad_info.family == CHIP_POLARIS12 ||
4722 device->physical_device->rad_info.family == CHIP_VEGAM) {
4723 ia_multi_vgt_param.partial_vs_wave = true;
4724 }
4725 }
4726
4727 ia_multi_vgt_param.base =
4728 S_028AA8_PRIMGROUP_SIZE(ia_multi_vgt_param.primgroup_size - 1) |
4729 /* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */
4730 S_028AA8_MAX_PRIMGRP_IN_WAVE(device->physical_device->rad_info.chip_class == GFX8 ? 2 : 0) |
4731 S_030960_EN_INST_OPT_BASIC(device->physical_device->rad_info.chip_class >= GFX9) |
4732 S_030960_EN_INST_OPT_ADV(device->physical_device->rad_info.chip_class >= GFX9);
4733
4734 return ia_multi_vgt_param;
4735 }
4736
4737
4738 static void
4739 radv_compute_vertex_input_state(struct radv_pipeline *pipeline,
4740 const VkGraphicsPipelineCreateInfo *pCreateInfo)
4741 {
4742 const VkPipelineVertexInputStateCreateInfo *vi_info =
4743 pCreateInfo->pVertexInputState;
4744 struct radv_vertex_elements_info *velems = &pipeline->vertex_elements;
4745
4746 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
4747 const VkVertexInputAttributeDescription *desc =
4748 &vi_info->pVertexAttributeDescriptions[i];
4749 unsigned loc = desc->location;
4750 const struct vk_format_description *format_desc;
4751
4752 format_desc = vk_format_description(desc->format);
4753
4754 velems->format_size[loc] = format_desc->block.bits / 8;
4755 }
4756
4757 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
4758 const VkVertexInputBindingDescription *desc =
4759 &vi_info->pVertexBindingDescriptions[i];
4760
4761 pipeline->binding_stride[desc->binding] = desc->stride;
4762 pipeline->num_vertex_bindings =
4763 MAX2(pipeline->num_vertex_bindings, desc->binding + 1);
4764 }
4765 }
4766
4767 static struct radv_shader_variant *
4768 radv_pipeline_get_streamout_shader(struct radv_pipeline *pipeline)
4769 {
4770 int i;
4771
4772 for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
4773 struct radv_shader_variant *shader =
4774 radv_get_shader(pipeline, i);
4775
4776 if (shader && shader->info.so.num_outputs > 0)
4777 return shader;
4778 }
4779
4780 return NULL;
4781 }
4782
4783 static VkResult
4784 radv_secure_compile(struct radv_pipeline *pipeline,
4785 struct radv_device *device,
4786 const struct radv_pipeline_key *key,
4787 const VkPipelineShaderStageCreateInfo **pStages,
4788 const VkPipelineCreateFlags flags,
4789 unsigned num_stages)
4790 {
4791 uint8_t allowed_pipeline_hashes[2][20];
4792 radv_hash_shaders(allowed_pipeline_hashes[0], pStages,
4793 pipeline->layout, key, get_hash_flags(device));
4794
4795 /* Generate the GC copy hash */
4796 memcpy(allowed_pipeline_hashes[1], allowed_pipeline_hashes[0], 20);
4797 allowed_pipeline_hashes[1][0] ^= 1;
4798
4799 uint8_t allowed_hashes[2][20];
4800 for (unsigned i = 0; i < 2; ++i) {
4801 disk_cache_compute_key(device->physical_device->disk_cache,
4802 allowed_pipeline_hashes[i], 20,
4803 allowed_hashes[i]);
4804 }
4805
4806 /* Do an early exit if all cache entries are already there. */
4807 bool may_need_copy_shader = pStages[MESA_SHADER_GEOMETRY];
4808 void *main_entry = disk_cache_get(device->physical_device->disk_cache, allowed_hashes[0], NULL);
4809 void *copy_entry = NULL;
4810 if (may_need_copy_shader)
4811 copy_entry = disk_cache_get(device->physical_device->disk_cache, allowed_hashes[1], NULL);
4812
4813 bool has_all_cache_entries = main_entry && (!may_need_copy_shader || copy_entry);
4814 free(main_entry);
4815 free(copy_entry);
4816
4817 if(has_all_cache_entries)
4818 return VK_SUCCESS;
4819
4820 unsigned process = 0;
4821 uint8_t sc_threads = device->instance->num_sc_threads;
4822 while (true) {
4823 mtx_lock(&device->sc_state->secure_compile_mutex);
4824 if (device->sc_state->secure_compile_thread_counter < sc_threads) {
4825 device->sc_state->secure_compile_thread_counter++;
4826 for (unsigned i = 0; i < sc_threads; i++) {
4827 if (!device->sc_state->secure_compile_processes[i].in_use) {
4828 device->sc_state->secure_compile_processes[i].in_use = true;
4829 process = i;
4830 break;
4831 }
4832 }
4833 mtx_unlock(&device->sc_state->secure_compile_mutex);
4834 break;
4835 }
4836 mtx_unlock(&device->sc_state->secure_compile_mutex);
4837 }
4838
4839 int fd_secure_input = device->sc_state->secure_compile_processes[process].fd_secure_input;
4840 int fd_secure_output = device->sc_state->secure_compile_processes[process].fd_secure_output;
4841
4842 /* Fork a copy of the slim untainted secure compile process */
4843 enum radv_secure_compile_type sc_type = RADV_SC_TYPE_FORK_DEVICE;
4844 write(fd_secure_input, &sc_type, sizeof(sc_type));
4845
4846 if (!radv_sc_read(fd_secure_output, &sc_type, sizeof(sc_type), true) ||
4847 sc_type != RADV_SC_TYPE_INIT_SUCCESS)
4848 return VK_ERROR_DEVICE_LOST;
4849
4850 fd_secure_input = device->sc_state->secure_compile_processes[process].fd_server;
4851 fd_secure_output = device->sc_state->secure_compile_processes[process].fd_client;
4852
4853 /* Write pipeline / shader module out to secure process via pipe */
4854 sc_type = RADV_SC_TYPE_COMPILE_PIPELINE;
4855 write(fd_secure_input, &sc_type, sizeof(sc_type));
4856
4857 /* Write pipeline layout out to secure process */
4858 struct radv_pipeline_layout *layout = pipeline->layout;
4859 write(fd_secure_input, layout, sizeof(struct radv_pipeline_layout));
4860 write(fd_secure_input, &layout->num_sets, sizeof(uint32_t));
4861 for (uint32_t set = 0; set < layout->num_sets; set++) {
4862 write(fd_secure_input, &layout->set[set].layout->layout_size, sizeof(uint32_t));
4863 write(fd_secure_input, layout->set[set].layout, layout->set[set].layout->layout_size);
4864 }
4865
4866 /* Write pipeline key out to secure process */
4867 write(fd_secure_input, key, sizeof(struct radv_pipeline_key));
4868
4869 /* Write pipeline create flags out to secure process */
4870 write(fd_secure_input, &flags, sizeof(VkPipelineCreateFlags));
4871
4872 /* Write stage and shader information out to secure process */
4873 write(fd_secure_input, &num_stages, sizeof(uint32_t));
4874 for (uint32_t i = 0; i < MESA_SHADER_STAGES; i++) {
4875 if (!pStages[i])
4876 continue;
4877
4878 /* Write stage out to secure process */
4879 gl_shader_stage stage = ffs(pStages[i]->stage) - 1;
4880 write(fd_secure_input, &stage, sizeof(gl_shader_stage));
4881
4882 /* Write entry point name out to secure process */
4883 size_t name_size = strlen(pStages[i]->pName) + 1;
4884 write(fd_secure_input, &name_size, sizeof(size_t));
4885 write(fd_secure_input, pStages[i]->pName, name_size);
4886
4887 /* Write shader module out to secure process */
4888 struct radv_shader_module *module = radv_shader_module_from_handle(pStages[i]->module);
4889 assert(!module->nir);
4890 size_t module_size = sizeof(struct radv_shader_module) + module->size;
4891 write(fd_secure_input, &module_size, sizeof(size_t));
4892 write(fd_secure_input, module, module_size);
4893
4894 /* Write specialization info out to secure process */
4895 const VkSpecializationInfo *specInfo = pStages[i]->pSpecializationInfo;
4896 bool has_spec_info = specInfo ? true : false;
4897 write(fd_secure_input, &has_spec_info, sizeof(bool));
4898 if (specInfo) {
4899 write(fd_secure_input, &specInfo->dataSize, sizeof(size_t));
4900 write(fd_secure_input, specInfo->pData, specInfo->dataSize);
4901
4902 write(fd_secure_input, &specInfo->mapEntryCount, sizeof(uint32_t));
4903 for (uint32_t j = 0; j < specInfo->mapEntryCount; j++)
4904 write(fd_secure_input, &specInfo->pMapEntries[j], sizeof(VkSpecializationMapEntry));
4905 }
4906 }
4907
4908 /* Read the data returned from the secure process */
4909 while (sc_type != RADV_SC_TYPE_COMPILE_PIPELINE_FINISHED) {
4910 if (!radv_sc_read(fd_secure_output, &sc_type, sizeof(sc_type), true))
4911 return VK_ERROR_DEVICE_LOST;
4912
4913 if (sc_type == RADV_SC_TYPE_WRITE_DISK_CACHE) {
4914 assert(device->physical_device->disk_cache);
4915
4916 uint8_t disk_sha1[20];
4917 if (!radv_sc_read(fd_secure_output, disk_sha1, sizeof(uint8_t) * 20, true))
4918 return VK_ERROR_DEVICE_LOST;
4919
4920 if (memcmp(disk_sha1, allowed_hashes[0], 20) &&
4921 memcmp(disk_sha1, allowed_hashes[1], 20))
4922 return VK_ERROR_DEVICE_LOST;
4923
4924 uint32_t entry_size;
4925 if (!radv_sc_read(fd_secure_output, &entry_size, sizeof(uint32_t), true))
4926 return VK_ERROR_DEVICE_LOST;
4927
4928 struct cache_entry *entry = malloc(entry_size);
4929 if (!radv_sc_read(fd_secure_output, entry, entry_size, true))
4930 return VK_ERROR_DEVICE_LOST;
4931
4932 disk_cache_put(device->physical_device->disk_cache,
4933 disk_sha1, entry, entry_size,
4934 NULL);
4935
4936 free(entry);
4937 } else if (sc_type == RADV_SC_TYPE_READ_DISK_CACHE) {
4938 uint8_t disk_sha1[20];
4939 if (!radv_sc_read(fd_secure_output, disk_sha1, sizeof(uint8_t) * 20, true))
4940 return VK_ERROR_DEVICE_LOST;
4941
4942 if (memcmp(disk_sha1, allowed_hashes[0], 20) &&
4943 memcmp(disk_sha1, allowed_hashes[1], 20))
4944 return VK_ERROR_DEVICE_LOST;
4945
4946 size_t size;
4947 struct cache_entry *entry = (struct cache_entry *)
4948 disk_cache_get(device->physical_device->disk_cache,
4949 disk_sha1, &size);
4950
4951 uint8_t found = entry ? 1 : 0;
4952 write(fd_secure_input, &found, sizeof(uint8_t));
4953
4954 if (found) {
4955 write(fd_secure_input, &size, sizeof(size_t));
4956 write(fd_secure_input, entry, size);
4957 }
4958
4959 free(entry);
4960 }
4961 }
4962
4963 sc_type = RADV_SC_TYPE_DESTROY_DEVICE;
4964 write(fd_secure_input, &sc_type, sizeof(sc_type));
4965
4966 mtx_lock(&device->sc_state->secure_compile_mutex);
4967 device->sc_state->secure_compile_thread_counter--;
4968 device->sc_state->secure_compile_processes[process].in_use = false;
4969 mtx_unlock(&device->sc_state->secure_compile_mutex);
4970
4971 return VK_SUCCESS;
4972 }
4973
4974 static VkResult
4975 radv_pipeline_init(struct radv_pipeline *pipeline,
4976 struct radv_device *device,
4977 struct radv_pipeline_cache *cache,
4978 const VkGraphicsPipelineCreateInfo *pCreateInfo,
4979 const struct radv_graphics_pipeline_create_info *extra)
4980 {
4981 VkResult result;
4982 bool has_view_index = false;
4983
4984 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
4985 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
4986 if (subpass->view_mask)
4987 has_view_index = true;
4988
4989 pipeline->device = device;
4990 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
4991 assert(pipeline->layout);
4992
4993 struct radv_blend_state blend = radv_pipeline_init_blend_state(pipeline, pCreateInfo, extra);
4994
4995 const VkPipelineCreationFeedbackCreateInfoEXT *creation_feedback =
4996 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
4997 radv_init_feedback(creation_feedback);
4998
4999 VkPipelineCreationFeedbackEXT *pipeline_feedback = creation_feedback ? creation_feedback->pPipelineCreationFeedback : NULL;
5000
5001 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
5002 VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = { 0 };
5003 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
5004 gl_shader_stage stage = ffs(pCreateInfo->pStages[i].stage) - 1;
5005 pStages[stage] = &pCreateInfo->pStages[i];
5006 if(creation_feedback)
5007 stage_feedbacks[stage] = &creation_feedback->pPipelineStageCreationFeedbacks[i];
5008 }
5009
5010 struct radv_pipeline_key key = radv_generate_graphics_pipeline_key(pipeline, pCreateInfo, &blend, has_view_index);
5011 if (radv_device_use_secure_compile(device->instance)) {
5012 return radv_secure_compile(pipeline, device, &key, pStages, pCreateInfo->flags, pCreateInfo->stageCount);
5013 } else {
5014 radv_create_shaders(pipeline, device, cache, &key, pStages, pCreateInfo->flags, pipeline_feedback, stage_feedbacks);
5015 }
5016
5017 pipeline->graphics.spi_baryc_cntl = S_0286E0_FRONT_FACE_ALL_BITS(1);
5018 radv_pipeline_init_multisample_state(pipeline, &blend, pCreateInfo);
5019 uint32_t gs_out;
5020 uint32_t prim = si_translate_prim(pCreateInfo->pInputAssemblyState->topology);
5021
5022 pipeline->graphics.can_use_guardband = radv_prim_can_use_guardband(pCreateInfo->pInputAssemblyState->topology);
5023
5024 if (radv_pipeline_has_gs(pipeline)) {
5025 gs_out = si_conv_gl_prim_to_gs_out(pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs.output_prim);
5026 pipeline->graphics.can_use_guardband = gs_out == V_028A6C_OUTPRIM_TYPE_TRISTRIP;
5027 } else if (radv_pipeline_has_tess(pipeline)) {
5028 if (pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.point_mode)
5029 gs_out = V_028A6C_OUTPRIM_TYPE_POINTLIST;
5030 else
5031 gs_out = si_conv_gl_prim_to_gs_out(pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.primitive_mode);
5032 pipeline->graphics.can_use_guardband = gs_out == V_028A6C_OUTPRIM_TYPE_TRISTRIP;
5033 } else {
5034 gs_out = si_conv_prim_to_gs_out(pCreateInfo->pInputAssemblyState->topology);
5035 }
5036 if (extra && extra->use_rectlist) {
5037 prim = V_008958_DI_PT_RECTLIST;
5038 gs_out = V_028A6C_OUTPRIM_TYPE_TRISTRIP;
5039 pipeline->graphics.can_use_guardband = true;
5040 if (radv_pipeline_has_ngg(pipeline))
5041 gs_out = V_028A6C_VGT_OUT_RECT_V0;
5042 }
5043 pipeline->graphics.prim_restart_enable = !!pCreateInfo->pInputAssemblyState->primitiveRestartEnable;
5044 /* prim vertex count will need TESS changes */
5045 pipeline->graphics.prim_vertex_count = prim_size_table[prim];
5046
5047 radv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
5048
5049 /* Ensure that some export memory is always allocated, for two reasons:
5050 *
5051 * 1) Correctness: The hardware ignores the EXEC mask if no export
5052 * memory is allocated, so KILL and alpha test do not work correctly
5053 * without this.
5054 * 2) Performance: Every shader needs at least a NULL export, even when
5055 * it writes no color/depth output. The NULL export instruction
5056 * stalls without this setting.
5057 *
5058 * Don't add this to CB_SHADER_MASK.
5059 *
5060 * GFX10 supports pixel shaders without exports by setting both the
5061 * color and Z formats to SPI_SHADER_ZERO. The hw will skip export
5062 * instructions if any are present.
5063 */
5064 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
5065 if ((pipeline->device->physical_device->rad_info.chip_class <= GFX9 ||
5066 ps->info.ps.can_discard) &&
5067 !blend.spi_shader_col_format) {
5068 if (!ps->info.ps.writes_z &&
5069 !ps->info.ps.writes_stencil &&
5070 !ps->info.ps.writes_sample_mask)
5071 blend.spi_shader_col_format = V_028714_SPI_SHADER_32_R;
5072 }
5073
5074 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
5075 if (pipeline->shaders[i]) {
5076 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[i]->info.need_indirect_descriptor_sets;
5077 }
5078 }
5079
5080 if (radv_pipeline_has_gs(pipeline) && !radv_pipeline_has_ngg(pipeline)) {
5081 struct radv_shader_variant *gs =
5082 pipeline->shaders[MESA_SHADER_GEOMETRY];
5083
5084 calculate_gs_ring_sizes(pipeline, &gs->info.gs_ring_info);
5085 }
5086
5087 struct radv_tessellation_state tess = {0};
5088 if (radv_pipeline_has_tess(pipeline)) {
5089 if (prim == V_008958_DI_PT_PATCH) {
5090 pipeline->graphics.prim_vertex_count.min = pCreateInfo->pTessellationState->patchControlPoints;
5091 pipeline->graphics.prim_vertex_count.incr = 1;
5092 }
5093 tess = calculate_tess_state(pipeline, pCreateInfo);
5094 }
5095
5096 pipeline->graphics.ia_multi_vgt_param = radv_compute_ia_multi_vgt_param_helpers(pipeline, &tess, prim);
5097
5098 radv_compute_vertex_input_state(pipeline, pCreateInfo);
5099
5100 for (uint32_t i = 0; i < MESA_SHADER_STAGES; i++)
5101 pipeline->user_data_0[i] = radv_pipeline_stage_to_user_data_0(pipeline, i, device->physical_device->rad_info.chip_class);
5102
5103 struct radv_userdata_info *loc = radv_lookup_user_sgpr(pipeline, MESA_SHADER_VERTEX,
5104 AC_UD_VS_BASE_VERTEX_START_INSTANCE);
5105 if (loc->sgpr_idx != -1) {
5106 pipeline->graphics.vtx_base_sgpr = pipeline->user_data_0[MESA_SHADER_VERTEX];
5107 pipeline->graphics.vtx_base_sgpr += loc->sgpr_idx * 4;
5108 if (radv_get_shader(pipeline, MESA_SHADER_VERTEX)->info.vs.needs_draw_id)
5109 pipeline->graphics.vtx_emit_num = 3;
5110 else
5111 pipeline->graphics.vtx_emit_num = 2;
5112 }
5113
5114 /* Find the last vertex shader stage that eventually uses streamout. */
5115 pipeline->streamout_shader = radv_pipeline_get_streamout_shader(pipeline);
5116
5117 result = radv_pipeline_scratch_init(device, pipeline);
5118 radv_pipeline_generate_pm4(pipeline, pCreateInfo, extra, &blend, &tess, prim, gs_out);
5119
5120 return result;
5121 }
5122
5123 VkResult
5124 radv_graphics_pipeline_create(
5125 VkDevice _device,
5126 VkPipelineCache _cache,
5127 const VkGraphicsPipelineCreateInfo *pCreateInfo,
5128 const struct radv_graphics_pipeline_create_info *extra,
5129 const VkAllocationCallbacks *pAllocator,
5130 VkPipeline *pPipeline)
5131 {
5132 RADV_FROM_HANDLE(radv_device, device, _device);
5133 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
5134 struct radv_pipeline *pipeline;
5135 VkResult result;
5136
5137 pipeline = vk_zalloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
5138 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
5139 if (pipeline == NULL)
5140 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
5141
5142 result = radv_pipeline_init(pipeline, device, cache,
5143 pCreateInfo, extra);
5144 if (result != VK_SUCCESS) {
5145 radv_pipeline_destroy(device, pipeline, pAllocator);
5146 return result;
5147 }
5148
5149 *pPipeline = radv_pipeline_to_handle(pipeline);
5150
5151 return VK_SUCCESS;
5152 }
5153
5154 VkResult radv_CreateGraphicsPipelines(
5155 VkDevice _device,
5156 VkPipelineCache pipelineCache,
5157 uint32_t count,
5158 const VkGraphicsPipelineCreateInfo* pCreateInfos,
5159 const VkAllocationCallbacks* pAllocator,
5160 VkPipeline* pPipelines)
5161 {
5162 VkResult result = VK_SUCCESS;
5163 unsigned i = 0;
5164
5165 for (; i < count; i++) {
5166 VkResult r;
5167 r = radv_graphics_pipeline_create(_device,
5168 pipelineCache,
5169 &pCreateInfos[i],
5170 NULL, pAllocator, &pPipelines[i]);
5171 if (r != VK_SUCCESS) {
5172 result = r;
5173 pPipelines[i] = VK_NULL_HANDLE;
5174 }
5175 }
5176
5177 return result;
5178 }
5179
5180
5181 static void
5182 radv_compute_generate_pm4(struct radv_pipeline *pipeline)
5183 {
5184 struct radv_shader_variant *compute_shader;
5185 struct radv_device *device = pipeline->device;
5186 unsigned threads_per_threadgroup;
5187 unsigned threadgroups_per_cu = 1;
5188 unsigned waves_per_threadgroup;
5189 unsigned max_waves_per_sh = 0;
5190 uint64_t va;
5191
5192 pipeline->cs.max_dw = device->physical_device->rad_info.chip_class >= GFX10 ? 22 : 20;
5193 pipeline->cs.buf = malloc(pipeline->cs.max_dw * 4);
5194
5195 compute_shader = pipeline->shaders[MESA_SHADER_COMPUTE];
5196 va = radv_buffer_get_va(compute_shader->bo) + compute_shader->bo_offset;
5197
5198 radeon_set_sh_reg_seq(&pipeline->cs, R_00B830_COMPUTE_PGM_LO, 2);
5199 radeon_emit(&pipeline->cs, va >> 8);
5200 radeon_emit(&pipeline->cs, S_00B834_DATA(va >> 40));
5201
5202 radeon_set_sh_reg_seq(&pipeline->cs, R_00B848_COMPUTE_PGM_RSRC1, 2);
5203 radeon_emit(&pipeline->cs, compute_shader->config.rsrc1);
5204 radeon_emit(&pipeline->cs, compute_shader->config.rsrc2);
5205 if (device->physical_device->rad_info.chip_class >= GFX10) {
5206 radeon_set_sh_reg(&pipeline->cs, R_00B8A0_COMPUTE_PGM_RSRC3, compute_shader->config.rsrc3);
5207 }
5208
5209 /* Calculate best compute resource limits. */
5210 threads_per_threadgroup = compute_shader->info.cs.block_size[0] *
5211 compute_shader->info.cs.block_size[1] *
5212 compute_shader->info.cs.block_size[2];
5213 waves_per_threadgroup = DIV_ROUND_UP(threads_per_threadgroup,
5214 compute_shader->info.wave_size);
5215
5216 if (device->physical_device->rad_info.chip_class >= GFX10 &&
5217 waves_per_threadgroup == 1)
5218 threadgroups_per_cu = 2;
5219
5220 radeon_set_sh_reg(&pipeline->cs, R_00B854_COMPUTE_RESOURCE_LIMITS,
5221 ac_get_compute_resource_limits(&device->physical_device->rad_info,
5222 waves_per_threadgroup,
5223 max_waves_per_sh,
5224 threadgroups_per_cu));
5225
5226 radeon_set_sh_reg_seq(&pipeline->cs, R_00B81C_COMPUTE_NUM_THREAD_X, 3);
5227 radeon_emit(&pipeline->cs,
5228 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[0]));
5229 radeon_emit(&pipeline->cs,
5230 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[1]));
5231 radeon_emit(&pipeline->cs,
5232 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[2]));
5233
5234 assert(pipeline->cs.cdw <= pipeline->cs.max_dw);
5235 }
5236
5237 static struct radv_pipeline_key
5238 radv_generate_compute_pipeline_key(struct radv_pipeline *pipeline,
5239 const VkComputePipelineCreateInfo *pCreateInfo)
5240 {
5241 const VkPipelineShaderStageCreateInfo *stage = &pCreateInfo->stage;
5242 struct radv_pipeline_key key;
5243 memset(&key, 0, sizeof(key));
5244
5245 if (pCreateInfo->flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT)
5246 key.optimisations_disabled = 1;
5247
5248 const VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT *subgroup_size =
5249 vk_find_struct_const(stage->pNext,
5250 PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO_EXT);
5251
5252 if (subgroup_size) {
5253 assert(subgroup_size->requiredSubgroupSize == 32 ||
5254 subgroup_size->requiredSubgroupSize == 64);
5255 key.compute_subgroup_size = subgroup_size->requiredSubgroupSize;
5256 }
5257
5258 return key;
5259 }
5260
5261 static VkResult radv_compute_pipeline_create(
5262 VkDevice _device,
5263 VkPipelineCache _cache,
5264 const VkComputePipelineCreateInfo* pCreateInfo,
5265 const VkAllocationCallbacks* pAllocator,
5266 VkPipeline* pPipeline)
5267 {
5268 RADV_FROM_HANDLE(radv_device, device, _device);
5269 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
5270 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
5271 VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = { 0 };
5272 struct radv_pipeline *pipeline;
5273 VkResult result;
5274
5275 pipeline = vk_zalloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
5276 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
5277 if (pipeline == NULL)
5278 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
5279
5280 pipeline->device = device;
5281 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
5282 assert(pipeline->layout);
5283
5284 const VkPipelineCreationFeedbackCreateInfoEXT *creation_feedback =
5285 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
5286 radv_init_feedback(creation_feedback);
5287
5288 VkPipelineCreationFeedbackEXT *pipeline_feedback = creation_feedback ? creation_feedback->pPipelineCreationFeedback : NULL;
5289 if (creation_feedback)
5290 stage_feedbacks[MESA_SHADER_COMPUTE] = &creation_feedback->pPipelineStageCreationFeedbacks[0];
5291
5292 pStages[MESA_SHADER_COMPUTE] = &pCreateInfo->stage;
5293
5294 struct radv_pipeline_key key =
5295 radv_generate_compute_pipeline_key(pipeline, pCreateInfo);
5296
5297 if (radv_device_use_secure_compile(device->instance)) {
5298 result = radv_secure_compile(pipeline, device, &key, pStages, pCreateInfo->flags, 1);
5299 *pPipeline = radv_pipeline_to_handle(pipeline);
5300
5301 return result;
5302 } else {
5303 radv_create_shaders(pipeline, device, cache, &key, pStages, pCreateInfo->flags, pipeline_feedback, stage_feedbacks);
5304 }
5305
5306 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);
5307 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[MESA_SHADER_COMPUTE]->info.need_indirect_descriptor_sets;
5308 result = radv_pipeline_scratch_init(device, pipeline);
5309 if (result != VK_SUCCESS) {
5310 radv_pipeline_destroy(device, pipeline, pAllocator);
5311 return result;
5312 }
5313
5314 radv_compute_generate_pm4(pipeline);
5315
5316 *pPipeline = radv_pipeline_to_handle(pipeline);
5317
5318 return VK_SUCCESS;
5319 }
5320
5321 VkResult radv_CreateComputePipelines(
5322 VkDevice _device,
5323 VkPipelineCache pipelineCache,
5324 uint32_t count,
5325 const VkComputePipelineCreateInfo* pCreateInfos,
5326 const VkAllocationCallbacks* pAllocator,
5327 VkPipeline* pPipelines)
5328 {
5329 VkResult result = VK_SUCCESS;
5330
5331 unsigned i = 0;
5332 for (; i < count; i++) {
5333 VkResult r;
5334 r = radv_compute_pipeline_create(_device, pipelineCache,
5335 &pCreateInfos[i],
5336 pAllocator, &pPipelines[i]);
5337 if (r != VK_SUCCESS) {
5338 result = r;
5339 pPipelines[i] = VK_NULL_HANDLE;
5340 }
5341 }
5342
5343 return result;
5344 }
5345
5346
5347 static uint32_t radv_get_executable_count(const struct radv_pipeline *pipeline)
5348 {
5349 uint32_t ret = 0;
5350 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
5351 if (!pipeline->shaders[i])
5352 continue;
5353
5354 if (i == MESA_SHADER_GEOMETRY &&
5355 !radv_pipeline_has_ngg(pipeline)) {
5356 ret += 2u;
5357 } else {
5358 ret += 1u;
5359 }
5360
5361 }
5362 return ret;
5363 }
5364
5365 static struct radv_shader_variant *
5366 radv_get_shader_from_executable_index(const struct radv_pipeline *pipeline, int index, gl_shader_stage *stage)
5367 {
5368 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
5369 if (!pipeline->shaders[i])
5370 continue;
5371 if (!index) {
5372 *stage = i;
5373 return pipeline->shaders[i];
5374 }
5375
5376 --index;
5377
5378 if (i == MESA_SHADER_GEOMETRY &&
5379 !radv_pipeline_has_ngg(pipeline)) {
5380 if (!index) {
5381 *stage = i;
5382 return pipeline->gs_copy_shader;
5383 }
5384 --index;
5385 }
5386 }
5387
5388 *stage = -1;
5389 return NULL;
5390 }
5391
5392 /* Basically strlcpy (which does not exist on linux) specialized for
5393 * descriptions. */
5394 static void desc_copy(char *desc, const char *src) {
5395 int len = strlen(src);
5396 assert(len < VK_MAX_DESCRIPTION_SIZE);
5397 memcpy(desc, src, len);
5398 memset(desc + len, 0, VK_MAX_DESCRIPTION_SIZE - len);
5399 }
5400
5401 VkResult radv_GetPipelineExecutablePropertiesKHR(
5402 VkDevice _device,
5403 const VkPipelineInfoKHR* pPipelineInfo,
5404 uint32_t* pExecutableCount,
5405 VkPipelineExecutablePropertiesKHR* pProperties)
5406 {
5407 RADV_FROM_HANDLE(radv_pipeline, pipeline, pPipelineInfo->pipeline);
5408 const uint32_t total_count = radv_get_executable_count(pipeline);
5409
5410 if (!pProperties) {
5411 *pExecutableCount = total_count;
5412 return VK_SUCCESS;
5413 }
5414
5415 const uint32_t count = MIN2(total_count, *pExecutableCount);
5416 for (unsigned i = 0, executable_idx = 0;
5417 i < MESA_SHADER_STAGES && executable_idx < count; ++i) {
5418 if (!pipeline->shaders[i])
5419 continue;
5420 pProperties[executable_idx].stages = mesa_to_vk_shader_stage(i);
5421 const char *name = NULL;
5422 const char *description = NULL;
5423 switch(i) {
5424 case MESA_SHADER_VERTEX:
5425 name = "Vertex Shader";
5426 description = "Vulkan Vertex Shader";
5427 break;
5428 case MESA_SHADER_TESS_CTRL:
5429 if (!pipeline->shaders[MESA_SHADER_VERTEX]) {
5430 pProperties[executable_idx].stages |= VK_SHADER_STAGE_VERTEX_BIT;
5431 name = "Vertex + Tessellation Control Shaders";
5432 description = "Combined Vulkan Vertex and Tessellation Control Shaders";
5433 } else {
5434 name = "Tessellation Control Shader";
5435 description = "Vulkan Tessellation Control Shader";
5436 }
5437 break;
5438 case MESA_SHADER_TESS_EVAL:
5439 name = "Tessellation Evaluation Shader";
5440 description = "Vulkan Tessellation Evaluation Shader";
5441 break;
5442 case MESA_SHADER_GEOMETRY:
5443 if (radv_pipeline_has_tess(pipeline) && !pipeline->shaders[MESA_SHADER_TESS_EVAL]) {
5444 pProperties[executable_idx].stages |= VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
5445 name = "Tessellation Evaluation + Geometry Shaders";
5446 description = "Combined Vulkan Tessellation Evaluation and Geometry Shaders";
5447 } else if (!radv_pipeline_has_tess(pipeline) && !pipeline->shaders[MESA_SHADER_VERTEX]) {
5448 pProperties[executable_idx].stages |= VK_SHADER_STAGE_VERTEX_BIT;
5449 name = "Vertex + Geometry Shader";
5450 description = "Combined Vulkan Vertex and Geometry Shaders";
5451 } else {
5452 name = "Geometry Shader";
5453 description = "Vulkan Geometry Shader";
5454 }
5455 break;
5456 case MESA_SHADER_FRAGMENT:
5457 name = "Fragment Shader";
5458 description = "Vulkan Fragment Shader";
5459 break;
5460 case MESA_SHADER_COMPUTE:
5461 name = "Compute Shader";
5462 description = "Vulkan Compute Shader";
5463 break;
5464 }
5465
5466 pProperties[executable_idx].subgroupSize = pipeline->shaders[i]->info.wave_size;
5467 desc_copy(pProperties[executable_idx].name, name);
5468 desc_copy(pProperties[executable_idx].description, description);
5469
5470 ++executable_idx;
5471 if (i == MESA_SHADER_GEOMETRY &&
5472 !radv_pipeline_has_ngg(pipeline)) {
5473 assert(pipeline->gs_copy_shader);
5474 if (executable_idx >= count)
5475 break;
5476
5477 pProperties[executable_idx].stages = VK_SHADER_STAGE_GEOMETRY_BIT;
5478 pProperties[executable_idx].subgroupSize = 64;
5479 desc_copy(pProperties[executable_idx].name, "GS Copy Shader");
5480 desc_copy(pProperties[executable_idx].description,
5481 "Extra shader stage that loads the GS output ringbuffer into the rasterizer");
5482
5483 ++executable_idx;
5484 }
5485 }
5486
5487 VkResult result = *pExecutableCount < total_count ? VK_INCOMPLETE : VK_SUCCESS;
5488 *pExecutableCount = count;
5489 return result;
5490 }
5491
5492 VkResult radv_GetPipelineExecutableStatisticsKHR(
5493 VkDevice _device,
5494 const VkPipelineExecutableInfoKHR* pExecutableInfo,
5495 uint32_t* pStatisticCount,
5496 VkPipelineExecutableStatisticKHR* pStatistics)
5497 {
5498 RADV_FROM_HANDLE(radv_device, device, _device);
5499 RADV_FROM_HANDLE(radv_pipeline, pipeline, pExecutableInfo->pipeline);
5500 gl_shader_stage stage;
5501 struct radv_shader_variant *shader = radv_get_shader_from_executable_index(pipeline, pExecutableInfo->executableIndex, &stage);
5502
5503 enum chip_class chip_class = device->physical_device->rad_info.chip_class;
5504 unsigned lds_increment = chip_class >= GFX7 ? 512 : 256;
5505 unsigned max_waves = radv_get_max_waves(device, shader, stage);
5506
5507 VkPipelineExecutableStatisticKHR *s = pStatistics;
5508 VkPipelineExecutableStatisticKHR *end = s + (pStatistics ? *pStatisticCount : 0);
5509 VkResult result = VK_SUCCESS;
5510
5511 if (s < end) {
5512 desc_copy(s->name, "SGPRs");
5513 desc_copy(s->description, "Number of SGPR registers allocated per subgroup");
5514 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5515 s->value.u64 = shader->config.num_sgprs;
5516 }
5517 ++s;
5518
5519 if (s < end) {
5520 desc_copy(s->name, "VGPRs");
5521 desc_copy(s->description, "Number of VGPR registers allocated per subgroup");
5522 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5523 s->value.u64 = shader->config.num_vgprs;
5524 }
5525 ++s;
5526
5527 if (s < end) {
5528 desc_copy(s->name, "Spilled SGPRs");
5529 desc_copy(s->description, "Number of SGPR registers spilled per subgroup");
5530 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5531 s->value.u64 = shader->config.spilled_sgprs;
5532 }
5533 ++s;
5534
5535 if (s < end) {
5536 desc_copy(s->name, "Spilled VGPRs");
5537 desc_copy(s->description, "Number of VGPR registers spilled per subgroup");
5538 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5539 s->value.u64 = shader->config.spilled_vgprs;
5540 }
5541 ++s;
5542
5543 if (s < end) {
5544 desc_copy(s->name, "PrivMem VGPRs");
5545 desc_copy(s->description, "Number of VGPRs stored in private memory per subgroup");
5546 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5547 s->value.u64 = shader->info.private_mem_vgprs;
5548 }
5549 ++s;
5550
5551 if (s < end) {
5552 desc_copy(s->name, "Code size");
5553 desc_copy(s->description, "Code size in bytes");
5554 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5555 s->value.u64 = shader->exec_size;
5556 }
5557 ++s;
5558
5559 if (s < end) {
5560 desc_copy(s->name, "LDS size");
5561 desc_copy(s->description, "LDS size in bytes per workgroup");
5562 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5563 s->value.u64 = shader->config.lds_size * lds_increment;
5564 }
5565 ++s;
5566
5567 if (s < end) {
5568 desc_copy(s->name, "Scratch size");
5569 desc_copy(s->description, "Private memory in bytes per subgroup");
5570 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5571 s->value.u64 = shader->config.scratch_bytes_per_wave;
5572 }
5573 ++s;
5574
5575 if (s < end) {
5576 desc_copy(s->name, "Subgroups per SIMD");
5577 desc_copy(s->description, "The maximum number of subgroups in flight on a SIMD unit");
5578 s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
5579 s->value.u64 = max_waves;
5580 }
5581 ++s;
5582
5583 if (!pStatistics)
5584 *pStatisticCount = s - pStatistics;
5585 else if (s > end) {
5586 *pStatisticCount = end - pStatistics;
5587 result = VK_INCOMPLETE;
5588 } else {
5589 *pStatisticCount = s - pStatistics;
5590 }
5591
5592 return result;
5593 }
5594
5595 static VkResult radv_copy_representation(void *data, size_t *data_size, const char *src)
5596 {
5597 size_t total_size = strlen(src) + 1;
5598
5599 if (!data) {
5600 *data_size = total_size;
5601 return VK_SUCCESS;
5602 }
5603
5604 size_t size = MIN2(total_size, *data_size);
5605
5606 memcpy(data, src, size);
5607 if (size)
5608 *((char*)data + size - 1) = 0;
5609 return size < total_size ? VK_INCOMPLETE : VK_SUCCESS;
5610 }
5611
5612 VkResult radv_GetPipelineExecutableInternalRepresentationsKHR(
5613 VkDevice device,
5614 const VkPipelineExecutableInfoKHR* pExecutableInfo,
5615 uint32_t* pInternalRepresentationCount,
5616 VkPipelineExecutableInternalRepresentationKHR* pInternalRepresentations)
5617 {
5618 RADV_FROM_HANDLE(radv_pipeline, pipeline, pExecutableInfo->pipeline);
5619 gl_shader_stage stage;
5620 struct radv_shader_variant *shader = radv_get_shader_from_executable_index(pipeline, pExecutableInfo->executableIndex, &stage);
5621
5622 VkPipelineExecutableInternalRepresentationKHR *p = pInternalRepresentations;
5623 VkPipelineExecutableInternalRepresentationKHR *end = p + (pInternalRepresentations ? *pInternalRepresentationCount : 0);
5624 VkResult result = VK_SUCCESS;
5625 /* optimized NIR */
5626 if (p < end) {
5627 p->isText = true;
5628 desc_copy(p->name, "NIR Shader(s)");
5629 desc_copy(p->description, "The optimized NIR shader(s)");
5630 if (radv_copy_representation(p->pData, &p->dataSize, shader->nir_string) != VK_SUCCESS)
5631 result = VK_INCOMPLETE;
5632 }
5633 ++p;
5634
5635 /* backend IR */
5636 if (p < end) {
5637 p->isText = true;
5638 if (shader->aco_used) {
5639 desc_copy(p->name, "ACO IR");
5640 desc_copy(p->description, "The ACO IR after some optimizations");
5641 } else {
5642 desc_copy(p->name, "LLVM IR");
5643 desc_copy(p->description, "The LLVM IR after some optimizations");
5644 }
5645 if (radv_copy_representation(p->pData, &p->dataSize, shader->ir_string) != VK_SUCCESS)
5646 result = VK_INCOMPLETE;
5647 }
5648 ++p;
5649
5650 /* Disassembler */
5651 if (p < end) {
5652 p->isText = true;
5653 desc_copy(p->name, "Assembly");
5654 desc_copy(p->description, "Final Assembly");
5655 if (radv_copy_representation(p->pData, &p->dataSize, shader->disasm_string) != VK_SUCCESS)
5656 result = VK_INCOMPLETE;
5657 }
5658 ++p;
5659
5660 if (!pInternalRepresentations)
5661 *pInternalRepresentationCount = p - pInternalRepresentations;
5662 else if(p > end) {
5663 result = VK_INCOMPLETE;
5664 *pInternalRepresentationCount = end - pInternalRepresentations;
5665 } else {
5666 *pInternalRepresentationCount = p - pInternalRepresentations;
5667 }
5668
5669 return result;
5670 }