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