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