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