radv: Lower input attachments in NIR.
[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 keys[MESA_SHADER_VERTEX].vs.out.as_ngg = true;
2258 }
2259
2260 for(int i = 0; i < MESA_SHADER_STAGES; ++i)
2261 keys[i].has_multiview_view_index = key->has_multiview_view_index;
2262
2263 keys[MESA_SHADER_FRAGMENT].fs.col_format = key->col_format;
2264 keys[MESA_SHADER_FRAGMENT].fs.is_int8 = key->is_int8;
2265 keys[MESA_SHADER_FRAGMENT].fs.is_int10 = key->is_int10;
2266 keys[MESA_SHADER_FRAGMENT].fs.log2_ps_iter_samples = key->log2_ps_iter_samples;
2267 keys[MESA_SHADER_FRAGMENT].fs.num_samples = key->num_samples;
2268 }
2269
2270 static void
2271 merge_tess_info(struct shader_info *tes_info,
2272 const struct shader_info *tcs_info)
2273 {
2274 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
2275 *
2276 * "PointMode. Controls generation of points rather than triangles
2277 * or lines. This functionality defaults to disabled, and is
2278 * enabled if either shader stage includes the execution mode.
2279 *
2280 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
2281 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
2282 * and OutputVertices, it says:
2283 *
2284 * "One mode must be set in at least one of the tessellation
2285 * shader stages."
2286 *
2287 * So, the fields can be set in either the TCS or TES, but they must
2288 * agree if set in both. Our backend looks at TES, so bitwise-or in
2289 * the values from the TCS.
2290 */
2291 assert(tcs_info->tess.tcs_vertices_out == 0 ||
2292 tes_info->tess.tcs_vertices_out == 0 ||
2293 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
2294 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
2295
2296 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
2297 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
2298 tcs_info->tess.spacing == tes_info->tess.spacing);
2299 tes_info->tess.spacing |= tcs_info->tess.spacing;
2300
2301 assert(tcs_info->tess.primitive_mode == 0 ||
2302 tes_info->tess.primitive_mode == 0 ||
2303 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
2304 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
2305 tes_info->tess.ccw |= tcs_info->tess.ccw;
2306 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
2307 }
2308
2309 static
2310 void radv_init_feedback(const VkPipelineCreationFeedbackCreateInfoEXT *ext)
2311 {
2312 if (!ext)
2313 return;
2314
2315 if (ext->pPipelineCreationFeedback) {
2316 ext->pPipelineCreationFeedback->flags = 0;
2317 ext->pPipelineCreationFeedback->duration = 0;
2318 }
2319
2320 for (unsigned i = 0; i < ext->pipelineStageCreationFeedbackCount; ++i) {
2321 ext->pPipelineStageCreationFeedbacks[i].flags = 0;
2322 ext->pPipelineStageCreationFeedbacks[i].duration = 0;
2323 }
2324 }
2325
2326 static
2327 void radv_start_feedback(VkPipelineCreationFeedbackEXT *feedback)
2328 {
2329 if (!feedback)
2330 return;
2331
2332 feedback->duration -= radv_get_current_time();
2333 feedback ->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT;
2334 }
2335
2336 static
2337 void radv_stop_feedback(VkPipelineCreationFeedbackEXT *feedback, bool cache_hit)
2338 {
2339 if (!feedback)
2340 return;
2341
2342 feedback->duration += radv_get_current_time();
2343 feedback ->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT |
2344 (cache_hit ? VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT : 0);
2345 }
2346
2347 static
2348 void radv_create_shaders(struct radv_pipeline *pipeline,
2349 struct radv_device *device,
2350 struct radv_pipeline_cache *cache,
2351 const struct radv_pipeline_key *key,
2352 const VkPipelineShaderStageCreateInfo **pStages,
2353 const VkPipelineCreateFlags flags,
2354 VkPipelineCreationFeedbackEXT *pipeline_feedback,
2355 VkPipelineCreationFeedbackEXT **stage_feedbacks)
2356 {
2357 struct radv_shader_module fs_m = {0};
2358 struct radv_shader_module *modules[MESA_SHADER_STAGES] = { 0, };
2359 nir_shader *nir[MESA_SHADER_STAGES] = {0};
2360 struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};
2361 struct radv_shader_variant_key keys[MESA_SHADER_STAGES] = {{{{{0}}}}};
2362 unsigned char hash[20], gs_copy_hash[20];
2363
2364 radv_start_feedback(pipeline_feedback);
2365
2366 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
2367 if (pStages[i]) {
2368 modules[i] = radv_shader_module_from_handle(pStages[i]->module);
2369 if (modules[i]->nir)
2370 _mesa_sha1_compute(modules[i]->nir->info.name,
2371 strlen(modules[i]->nir->info.name),
2372 modules[i]->sha1);
2373
2374 pipeline->active_stages |= mesa_to_vk_shader_stage(i);
2375 }
2376 }
2377
2378 radv_hash_shaders(hash, pStages, pipeline->layout, key, get_hash_flags(device));
2379 memcpy(gs_copy_hash, hash, 20);
2380 gs_copy_hash[0] ^= 1;
2381
2382 bool found_in_application_cache = true;
2383 if (modules[MESA_SHADER_GEOMETRY]) {
2384 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2385 radv_create_shader_variants_from_pipeline_cache(device, cache, gs_copy_hash, variants,
2386 &found_in_application_cache);
2387 pipeline->gs_copy_shader = variants[MESA_SHADER_GEOMETRY];
2388 }
2389
2390 if (radv_create_shader_variants_from_pipeline_cache(device, cache, hash, pipeline->shaders,
2391 &found_in_application_cache) &&
2392 (!modules[MESA_SHADER_GEOMETRY] || pipeline->gs_copy_shader)) {
2393 radv_stop_feedback(pipeline_feedback, found_in_application_cache);
2394 return;
2395 }
2396
2397 if (!modules[MESA_SHADER_FRAGMENT] && !modules[MESA_SHADER_COMPUTE]) {
2398 nir_builder fs_b;
2399 nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
2400 fs_b.shader->info.name = ralloc_strdup(fs_b.shader, "noop_fs");
2401 fs_m.nir = fs_b.shader;
2402 modules[MESA_SHADER_FRAGMENT] = &fs_m;
2403 }
2404
2405 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
2406 const VkPipelineShaderStageCreateInfo *stage = pStages[i];
2407
2408 if (!modules[i])
2409 continue;
2410
2411 radv_start_feedback(stage_feedbacks[i]);
2412
2413 nir[i] = radv_shader_compile_to_nir(device, modules[i],
2414 stage ? stage->pName : "main", i,
2415 stage ? stage->pSpecializationInfo : NULL,
2416 flags, pipeline->layout);
2417
2418 /* We don't want to alter meta shaders IR directly so clone it
2419 * first.
2420 */
2421 if (nir[i]->info.name) {
2422 nir[i] = nir_shader_clone(NULL, nir[i]);
2423 }
2424
2425 radv_stop_feedback(stage_feedbacks[i], false);
2426 }
2427
2428 if (nir[MESA_SHADER_TESS_CTRL]) {
2429 nir_lower_patch_vertices(nir[MESA_SHADER_TESS_EVAL], nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out, NULL);
2430 merge_tess_info(&nir[MESA_SHADER_TESS_EVAL]->info, &nir[MESA_SHADER_TESS_CTRL]->info);
2431 }
2432
2433 if (!(flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT))
2434 radv_link_shaders(pipeline, nir);
2435
2436 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2437 if (nir[i]) {
2438 NIR_PASS_V(nir[i], nir_lower_bool_to_int32);
2439 NIR_PASS_V(nir[i], nir_lower_non_uniform_access,
2440 nir_lower_non_uniform_ubo_access |
2441 nir_lower_non_uniform_ssbo_access |
2442 nir_lower_non_uniform_texture_access |
2443 nir_lower_non_uniform_image_access);
2444 }
2445
2446 if (radv_can_dump_shader(device, modules[i], false))
2447 nir_print_shader(nir[i], stderr);
2448 }
2449
2450 radv_fill_shader_keys(device, keys, key, nir);
2451
2452 if (nir[MESA_SHADER_FRAGMENT]) {
2453 if (!pipeline->shaders[MESA_SHADER_FRAGMENT]) {
2454 radv_start_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT]);
2455
2456 pipeline->shaders[MESA_SHADER_FRAGMENT] =
2457 radv_shader_variant_compile(device, modules[MESA_SHADER_FRAGMENT], &nir[MESA_SHADER_FRAGMENT], 1,
2458 pipeline->layout, keys + MESA_SHADER_FRAGMENT,
2459 &binaries[MESA_SHADER_FRAGMENT]);
2460
2461 radv_stop_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT], false);
2462 }
2463
2464 /* TODO: These are no longer used as keys we should refactor this */
2465 keys[MESA_SHADER_VERTEX].vs.out.export_prim_id =
2466 pipeline->shaders[MESA_SHADER_FRAGMENT]->info.info.ps.prim_id_input;
2467 keys[MESA_SHADER_VERTEX].vs.out.export_layer_id =
2468 pipeline->shaders[MESA_SHADER_FRAGMENT]->info.info.ps.layer_input;
2469 keys[MESA_SHADER_VERTEX].vs.out.export_clip_dists =
2470 !!pipeline->shaders[MESA_SHADER_FRAGMENT]->info.info.ps.num_input_clips_culls;
2471 keys[MESA_SHADER_TESS_EVAL].tes.out.export_prim_id =
2472 pipeline->shaders[MESA_SHADER_FRAGMENT]->info.info.ps.prim_id_input;
2473 keys[MESA_SHADER_TESS_EVAL].tes.out.export_layer_id =
2474 pipeline->shaders[MESA_SHADER_FRAGMENT]->info.info.ps.layer_input;
2475 keys[MESA_SHADER_TESS_EVAL].tes.out.export_clip_dists =
2476 !!pipeline->shaders[MESA_SHADER_FRAGMENT]->info.info.ps.num_input_clips_culls;
2477 }
2478
2479 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_TESS_CTRL]) {
2480 if (!pipeline->shaders[MESA_SHADER_TESS_CTRL]) {
2481 struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};
2482 struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];
2483 key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;
2484
2485 radv_start_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL]);
2486
2487 pipeline->shaders[MESA_SHADER_TESS_CTRL] = radv_shader_variant_compile(device, modules[MESA_SHADER_TESS_CTRL], combined_nir, 2,
2488 pipeline->layout,
2489 &key, &binaries[MESA_SHADER_TESS_CTRL]);
2490
2491 radv_stop_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL], false);
2492 }
2493 modules[MESA_SHADER_VERTEX] = NULL;
2494 keys[MESA_SHADER_TESS_EVAL].tes.num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2495 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.info.tcs.outputs_written);
2496 }
2497
2498 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_GEOMETRY]) {
2499 gl_shader_stage pre_stage = modules[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2500 if (!pipeline->shaders[MESA_SHADER_GEOMETRY]) {
2501 struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};
2502
2503 radv_start_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY]);
2504
2505 pipeline->shaders[MESA_SHADER_GEOMETRY] = radv_shader_variant_compile(device, modules[MESA_SHADER_GEOMETRY], combined_nir, 2,
2506 pipeline->layout,
2507 &keys[pre_stage] , &binaries[MESA_SHADER_GEOMETRY]);
2508
2509 radv_stop_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY], false);
2510 }
2511 modules[pre_stage] = NULL;
2512 }
2513
2514 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2515 if(modules[i] && !pipeline->shaders[i]) {
2516 if (i == MESA_SHADER_TESS_CTRL) {
2517 keys[MESA_SHADER_TESS_CTRL].tcs.num_inputs = util_last_bit64(pipeline->shaders[MESA_SHADER_VERTEX]->info.info.vs.ls_outputs_written);
2518 }
2519 if (i == MESA_SHADER_TESS_EVAL) {
2520 keys[MESA_SHADER_TESS_EVAL].tes.num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_patches;
2521 keys[MESA_SHADER_TESS_EVAL].tes.tcs_num_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.info.tcs.outputs_written);
2522 }
2523
2524 radv_start_feedback(stage_feedbacks[i]);
2525
2526 pipeline->shaders[i] = radv_shader_variant_compile(device, modules[i], &nir[i], 1,
2527 pipeline->layout,
2528 keys + i, &binaries[i]);
2529
2530 radv_stop_feedback(stage_feedbacks[i], false);
2531 }
2532 }
2533
2534 if(modules[MESA_SHADER_GEOMETRY]) {
2535 struct radv_shader_binary *gs_copy_binary = NULL;
2536 if (!pipeline->gs_copy_shader) {
2537 pipeline->gs_copy_shader = radv_create_gs_copy_shader(
2538 device, nir[MESA_SHADER_GEOMETRY], &gs_copy_binary,
2539 keys[MESA_SHADER_GEOMETRY].has_multiview_view_index);
2540 }
2541
2542 if (pipeline->gs_copy_shader) {
2543 struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};
2544 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2545
2546 binaries[MESA_SHADER_GEOMETRY] = gs_copy_binary;
2547 variants[MESA_SHADER_GEOMETRY] = pipeline->gs_copy_shader;
2548
2549 radv_pipeline_cache_insert_shaders(device, cache,
2550 gs_copy_hash,
2551 variants,
2552 binaries);
2553 }
2554 free(gs_copy_binary);
2555 }
2556
2557 radv_pipeline_cache_insert_shaders(device, cache, hash, pipeline->shaders,
2558 binaries);
2559
2560 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2561 free(binaries[i]);
2562 if (nir[i]) {
2563 if (!pipeline->device->keep_shader_info)
2564 ralloc_free(nir[i]);
2565
2566 if (radv_can_dump_shader_stats(device, modules[i]))
2567 radv_shader_dump_stats(device,
2568 pipeline->shaders[i],
2569 i, stderr);
2570 }
2571 }
2572
2573 if (fs_m.nir)
2574 ralloc_free(fs_m.nir);
2575
2576 radv_stop_feedback(pipeline_feedback, false);
2577 }
2578
2579 static uint32_t
2580 radv_pipeline_stage_to_user_data_0(struct radv_pipeline *pipeline,
2581 gl_shader_stage stage, enum chip_class chip_class)
2582 {
2583 bool has_gs = radv_pipeline_has_gs(pipeline);
2584 bool has_tess = radv_pipeline_has_tess(pipeline);
2585 bool has_ngg = radv_pipeline_has_ngg(pipeline);
2586
2587 switch (stage) {
2588 case MESA_SHADER_FRAGMENT:
2589 return R_00B030_SPI_SHADER_USER_DATA_PS_0;
2590 case MESA_SHADER_VERTEX:
2591 if (has_tess) {
2592 if (chip_class >= GFX10) {
2593 return R_00B430_SPI_SHADER_USER_DATA_HS_0;
2594 } else if (chip_class == GFX9) {
2595 return R_00B430_SPI_SHADER_USER_DATA_LS_0;
2596 } else {
2597 return R_00B530_SPI_SHADER_USER_DATA_LS_0;
2598 }
2599
2600 }
2601
2602 if (has_gs) {
2603 if (chip_class >= GFX10) {
2604 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
2605 } else {
2606 return R_00B330_SPI_SHADER_USER_DATA_ES_0;
2607 }
2608 }
2609
2610 if (has_ngg)
2611 return R_00B230_SPI_SHADER_USER_DATA_GS_0;
2612
2613 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
2614 case MESA_SHADER_GEOMETRY:
2615 return chip_class == GFX9 ? R_00B330_SPI_SHADER_USER_DATA_ES_0 :
2616 R_00B230_SPI_SHADER_USER_DATA_GS_0;
2617 case MESA_SHADER_COMPUTE:
2618 return R_00B900_COMPUTE_USER_DATA_0;
2619 case MESA_SHADER_TESS_CTRL:
2620 return chip_class == GFX9 ? R_00B430_SPI_SHADER_USER_DATA_LS_0 :
2621 R_00B430_SPI_SHADER_USER_DATA_HS_0;
2622 case MESA_SHADER_TESS_EVAL:
2623 if (has_gs) {
2624 return chip_class >= GFX10 ? R_00B230_SPI_SHADER_USER_DATA_GS_0 :
2625 R_00B330_SPI_SHADER_USER_DATA_ES_0;
2626 } else {
2627 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
2628 }
2629 default:
2630 unreachable("unknown shader");
2631 }
2632 }
2633
2634 struct radv_bin_size_entry {
2635 unsigned bpp;
2636 VkExtent2D extent;
2637 };
2638
2639 static VkExtent2D
2640 radv_compute_bin_size(struct radv_pipeline *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo)
2641 {
2642 static const struct radv_bin_size_entry color_size_table[][3][9] = {
2643 {
2644 /* One RB / SE */
2645 {
2646 /* One shader engine */
2647 { 0, {128, 128}},
2648 { 1, { 64, 128}},
2649 { 2, { 32, 128}},
2650 { 3, { 16, 128}},
2651 { 17, { 0, 0}},
2652 { UINT_MAX, { 0, 0}},
2653 },
2654 {
2655 /* Two shader engines */
2656 { 0, {128, 128}},
2657 { 2, { 64, 128}},
2658 { 3, { 32, 128}},
2659 { 5, { 16, 128}},
2660 { 17, { 0, 0}},
2661 { UINT_MAX, { 0, 0}},
2662 },
2663 {
2664 /* Four shader engines */
2665 { 0, {128, 128}},
2666 { 3, { 64, 128}},
2667 { 5, { 16, 128}},
2668 { 17, { 0, 0}},
2669 { UINT_MAX, { 0, 0}},
2670 },
2671 },
2672 {
2673 /* Two RB / SE */
2674 {
2675 /* One shader engine */
2676 { 0, {128, 128}},
2677 { 2, { 64, 128}},
2678 { 3, { 32, 128}},
2679 { 5, { 16, 128}},
2680 { 33, { 0, 0}},
2681 { UINT_MAX, { 0, 0}},
2682 },
2683 {
2684 /* Two shader engines */
2685 { 0, {128, 128}},
2686 { 3, { 64, 128}},
2687 { 5, { 32, 128}},
2688 { 9, { 16, 128}},
2689 { 33, { 0, 0}},
2690 { UINT_MAX, { 0, 0}},
2691 },
2692 {
2693 /* Four shader engines */
2694 { 0, {256, 256}},
2695 { 2, {128, 256}},
2696 { 3, {128, 128}},
2697 { 5, { 64, 128}},
2698 { 9, { 16, 128}},
2699 { 33, { 0, 0}},
2700 { UINT_MAX, { 0, 0}},
2701 },
2702 },
2703 {
2704 /* Four RB / SE */
2705 {
2706 /* One shader engine */
2707 { 0, {128, 256}},
2708 { 2, {128, 128}},
2709 { 3, { 64, 128}},
2710 { 5, { 32, 128}},
2711 { 9, { 16, 128}},
2712 { 33, { 0, 0}},
2713 { UINT_MAX, { 0, 0}},
2714 },
2715 {
2716 /* Two shader engines */
2717 { 0, {256, 256}},
2718 { 2, {128, 256}},
2719 { 3, {128, 128}},
2720 { 5, { 64, 128}},
2721 { 9, { 32, 128}},
2722 { 17, { 16, 128}},
2723 { 33, { 0, 0}},
2724 { UINT_MAX, { 0, 0}},
2725 },
2726 {
2727 /* Four shader engines */
2728 { 0, {256, 512}},
2729 { 2, {256, 256}},
2730 { 3, {128, 256}},
2731 { 5, {128, 128}},
2732 { 9, { 64, 128}},
2733 { 17, { 16, 128}},
2734 { 33, { 0, 0}},
2735 { UINT_MAX, { 0, 0}},
2736 },
2737 },
2738 };
2739 static const struct radv_bin_size_entry ds_size_table[][3][9] = {
2740 {
2741 // One RB / SE
2742 {
2743 // One shader engine
2744 { 0, {128, 256}},
2745 { 2, {128, 128}},
2746 { 4, { 64, 128}},
2747 { 7, { 32, 128}},
2748 { 13, { 16, 128}},
2749 { 49, { 0, 0}},
2750 { UINT_MAX, { 0, 0}},
2751 },
2752 {
2753 // Two shader engines
2754 { 0, {256, 256}},
2755 { 2, {128, 256}},
2756 { 4, {128, 128}},
2757 { 7, { 64, 128}},
2758 { 13, { 32, 128}},
2759 { 25, { 16, 128}},
2760 { 49, { 0, 0}},
2761 { UINT_MAX, { 0, 0}},
2762 },
2763 {
2764 // Four shader engines
2765 { 0, {256, 512}},
2766 { 2, {256, 256}},
2767 { 4, {128, 256}},
2768 { 7, {128, 128}},
2769 { 13, { 64, 128}},
2770 { 25, { 16, 128}},
2771 { 49, { 0, 0}},
2772 { UINT_MAX, { 0, 0}},
2773 },
2774 },
2775 {
2776 // Two RB / SE
2777 {
2778 // One shader engine
2779 { 0, {256, 256}},
2780 { 2, {128, 256}},
2781 { 4, {128, 128}},
2782 { 7, { 64, 128}},
2783 { 13, { 32, 128}},
2784 { 25, { 16, 128}},
2785 { 97, { 0, 0}},
2786 { UINT_MAX, { 0, 0}},
2787 },
2788 {
2789 // Two shader engines
2790 { 0, {256, 512}},
2791 { 2, {256, 256}},
2792 { 4, {128, 256}},
2793 { 7, {128, 128}},
2794 { 13, { 64, 128}},
2795 { 25, { 32, 128}},
2796 { 49, { 16, 128}},
2797 { 97, { 0, 0}},
2798 { UINT_MAX, { 0, 0}},
2799 },
2800 {
2801 // Four shader engines
2802 { 0, {512, 512}},
2803 { 2, {256, 512}},
2804 { 4, {256, 256}},
2805 { 7, {128, 256}},
2806 { 13, {128, 128}},
2807 { 25, { 64, 128}},
2808 { 49, { 16, 128}},
2809 { 97, { 0, 0}},
2810 { UINT_MAX, { 0, 0}},
2811 },
2812 },
2813 {
2814 // Four RB / SE
2815 {
2816 // One shader engine
2817 { 0, {256, 512}},
2818 { 2, {256, 256}},
2819 { 4, {128, 256}},
2820 { 7, {128, 128}},
2821 { 13, { 64, 128}},
2822 { 25, { 32, 128}},
2823 { 49, { 16, 128}},
2824 { UINT_MAX, { 0, 0}},
2825 },
2826 {
2827 // Two shader engines
2828 { 0, {512, 512}},
2829 { 2, {256, 512}},
2830 { 4, {256, 256}},
2831 { 7, {128, 256}},
2832 { 13, {128, 128}},
2833 { 25, { 64, 128}},
2834 { 49, { 32, 128}},
2835 { 97, { 16, 128}},
2836 { UINT_MAX, { 0, 0}},
2837 },
2838 {
2839 // Four shader engines
2840 { 0, {512, 512}},
2841 { 4, {256, 512}},
2842 { 7, {256, 256}},
2843 { 13, {128, 256}},
2844 { 25, {128, 128}},
2845 { 49, { 64, 128}},
2846 { 97, { 16, 128}},
2847 { UINT_MAX, { 0, 0}},
2848 },
2849 },
2850 };
2851
2852 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
2853 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
2854 VkExtent2D extent = {512, 512};
2855
2856 unsigned log_num_rb_per_se =
2857 util_logbase2_ceil(pipeline->device->physical_device->rad_info.num_render_backends /
2858 pipeline->device->physical_device->rad_info.max_se);
2859 unsigned log_num_se = util_logbase2_ceil(pipeline->device->physical_device->rad_info.max_se);
2860
2861 unsigned total_samples = 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline->graphics.ms.pa_sc_aa_config);
2862 unsigned ps_iter_samples = 1u << G_028804_PS_ITER_SAMPLES(pipeline->graphics.ms.db_eqaa);
2863 unsigned effective_samples = total_samples;
2864 unsigned color_bytes_per_pixel = 0;
2865
2866 const VkPipelineColorBlendStateCreateInfo *vkblend = pCreateInfo->pColorBlendState;
2867 if (vkblend) {
2868 for (unsigned i = 0; i < subpass->color_count; i++) {
2869 if (!vkblend->pAttachments[i].colorWriteMask)
2870 continue;
2871
2872 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
2873 continue;
2874
2875 VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;
2876 color_bytes_per_pixel += vk_format_get_blocksize(format);
2877 }
2878
2879 /* MSAA images typically don't use all samples all the time. */
2880 if (effective_samples >= 2 && ps_iter_samples <= 1)
2881 effective_samples = 2;
2882 color_bytes_per_pixel *= effective_samples;
2883 }
2884
2885 const struct radv_bin_size_entry *color_entry = color_size_table[log_num_rb_per_se][log_num_se];
2886 while(color_entry[1].bpp <= color_bytes_per_pixel)
2887 ++color_entry;
2888
2889 extent = color_entry->extent;
2890
2891 if (subpass->depth_stencil_attachment) {
2892 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;
2893
2894 /* Coefficients taken from AMDVLK */
2895 unsigned depth_coeff = vk_format_is_depth(attachment->format) ? 5 : 0;
2896 unsigned stencil_coeff = vk_format_is_stencil(attachment->format) ? 1 : 0;
2897 unsigned ds_bytes_per_pixel = 4 * (depth_coeff + stencil_coeff) * total_samples;
2898
2899 const struct radv_bin_size_entry *ds_entry = ds_size_table[log_num_rb_per_se][log_num_se];
2900 while(ds_entry[1].bpp <= ds_bytes_per_pixel)
2901 ++ds_entry;
2902
2903 extent.width = MIN2(extent.width, ds_entry->extent.width);
2904 extent.height = MIN2(extent.height, ds_entry->extent.height);
2905 }
2906
2907 return extent;
2908 }
2909
2910 static void
2911 radv_pipeline_generate_binning_state(struct radeon_cmdbuf *ctx_cs,
2912 struct radv_pipeline *pipeline,
2913 const VkGraphicsPipelineCreateInfo *pCreateInfo)
2914 {
2915 if (pipeline->device->physical_device->rad_info.chip_class < GFX9)
2916 return;
2917
2918 uint32_t pa_sc_binner_cntl_0 =
2919 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC) |
2920 S_028C44_DISABLE_START_OF_PRIM(1);
2921 uint32_t db_dfsm_control = S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF);
2922
2923 VkExtent2D bin_size = radv_compute_bin_size(pipeline, pCreateInfo);
2924
2925 if (pipeline->device->pbb_allowed && bin_size.width && bin_size.height) {
2926 unsigned context_states_per_bin; /* allowed range: [1, 6] */
2927 unsigned persistent_states_per_bin; /* allowed range: [1, 32] */
2928 unsigned fpovs_per_batch; /* allowed range: [0, 255], 0 = unlimited */
2929
2930 switch (pipeline->device->physical_device->rad_info.family) {
2931 case CHIP_VEGA10:
2932 case CHIP_VEGA12:
2933 case CHIP_VEGA20:
2934 context_states_per_bin = 1;
2935 persistent_states_per_bin = 1;
2936 fpovs_per_batch = 63;
2937 break;
2938 case CHIP_RAVEN:
2939 case CHIP_RAVEN2:
2940 context_states_per_bin = 6;
2941 persistent_states_per_bin = 32;
2942 fpovs_per_batch = 63;
2943 break;
2944 default:
2945 unreachable("unhandled family while determining binning state.");
2946 }
2947
2948 pa_sc_binner_cntl_0 =
2949 S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED) |
2950 S_028C44_BIN_SIZE_X(bin_size.width == 16) |
2951 S_028C44_BIN_SIZE_Y(bin_size.height == 16) |
2952 S_028C44_BIN_SIZE_X_EXTEND(util_logbase2(MAX2(bin_size.width, 32)) - 5) |
2953 S_028C44_BIN_SIZE_Y_EXTEND(util_logbase2(MAX2(bin_size.height, 32)) - 5) |
2954 S_028C44_CONTEXT_STATES_PER_BIN(context_states_per_bin - 1) |
2955 S_028C44_PERSISTENT_STATES_PER_BIN(persistent_states_per_bin - 1) |
2956 S_028C44_DISABLE_START_OF_PRIM(1) |
2957 S_028C44_FPOVS_PER_BATCH(fpovs_per_batch) |
2958 S_028C44_OPTIMAL_BIN_SELECTION(1);
2959 }
2960
2961 radeon_set_context_reg(ctx_cs, R_028C44_PA_SC_BINNER_CNTL_0,
2962 pa_sc_binner_cntl_0);
2963
2964 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
2965 radeon_set_context_reg(ctx_cs, R_028038_DB_DFSM_CONTROL,
2966 db_dfsm_control);
2967 } else {
2968 radeon_set_context_reg(ctx_cs, R_028060_DB_DFSM_CONTROL,
2969 db_dfsm_control);
2970 }
2971 }
2972
2973
2974 static void
2975 radv_pipeline_generate_depth_stencil_state(struct radeon_cmdbuf *ctx_cs,
2976 struct radv_pipeline *pipeline,
2977 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2978 const struct radv_graphics_pipeline_create_info *extra)
2979 {
2980 const VkPipelineDepthStencilStateCreateInfo *vkds = pCreateInfo->pDepthStencilState;
2981 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
2982 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
2983 struct radv_render_pass_attachment *attachment = NULL;
2984 uint32_t db_depth_control = 0, db_stencil_control = 0;
2985 uint32_t db_render_control = 0, db_render_override2 = 0;
2986 uint32_t db_render_override = 0;
2987
2988 if (subpass->depth_stencil_attachment)
2989 attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;
2990
2991 bool has_depth_attachment = attachment && vk_format_is_depth(attachment->format);
2992 bool has_stencil_attachment = attachment && vk_format_is_stencil(attachment->format);
2993
2994 if (vkds && has_depth_attachment) {
2995 db_depth_control = S_028800_Z_ENABLE(vkds->depthTestEnable ? 1 : 0) |
2996 S_028800_Z_WRITE_ENABLE(vkds->depthWriteEnable ? 1 : 0) |
2997 S_028800_ZFUNC(vkds->depthCompareOp) |
2998 S_028800_DEPTH_BOUNDS_ENABLE(vkds->depthBoundsTestEnable ? 1 : 0);
2999
3000 /* from amdvlk: For 4xAA and 8xAA need to decompress on flush for better performance */
3001 db_render_override2 |= S_028010_DECOMPRESS_Z_ON_FLUSH(attachment->samples > 2);
3002 }
3003
3004 if (has_stencil_attachment && vkds && vkds->stencilTestEnable) {
3005 db_depth_control |= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);
3006 db_depth_control |= S_028800_STENCILFUNC(vkds->front.compareOp);
3007 db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(vkds->front.failOp));
3008 db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(vkds->front.passOp));
3009 db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(vkds->front.depthFailOp));
3010
3011 db_depth_control |= S_028800_STENCILFUNC_BF(vkds->back.compareOp);
3012 db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(vkds->back.failOp));
3013 db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(vkds->back.passOp));
3014 db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(vkds->back.depthFailOp));
3015 }
3016
3017 if (attachment && extra) {
3018 db_render_control |= S_028000_DEPTH_CLEAR_ENABLE(extra->db_depth_clear);
3019 db_render_control |= S_028000_STENCIL_CLEAR_ENABLE(extra->db_stencil_clear);
3020
3021 db_render_control |= S_028000_RESUMMARIZE_ENABLE(extra->db_resummarize);
3022 db_render_control |= S_028000_DEPTH_COMPRESS_DISABLE(extra->db_flush_depth_inplace);
3023 db_render_control |= S_028000_STENCIL_COMPRESS_DISABLE(extra->db_flush_stencil_inplace);
3024 db_render_override2 |= S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra->db_depth_disable_expclear);
3025 db_render_override2 |= S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra->db_stencil_disable_expclear);
3026 }
3027
3028 db_render_override |= S_02800C_FORCE_HIS_ENABLE0(V_02800C_FORCE_DISABLE) |
3029 S_02800C_FORCE_HIS_ENABLE1(V_02800C_FORCE_DISABLE);
3030
3031 if (!pCreateInfo->pRasterizationState->depthClampEnable) {
3032 /* From VK_EXT_depth_range_unrestricted spec:
3033 *
3034 * "The behavior described in Primitive Clipping still applies.
3035 * If depth clamping is disabled the depth values are still
3036 * clipped to 0 ≤ zc ≤ wc before the viewport transform. If
3037 * depth clamping is enabled the above equation is ignored and
3038 * the depth values are instead clamped to the VkViewport
3039 * minDepth and maxDepth values, which in the case of this
3040 * extension can be outside of the 0.0 to 1.0 range."
3041 */
3042 db_render_override |= S_02800C_DISABLE_VIEWPORT_CLAMP(1);
3043 }
3044
3045 radeon_set_context_reg(ctx_cs, R_028800_DB_DEPTH_CONTROL, db_depth_control);
3046 radeon_set_context_reg(ctx_cs, R_02842C_DB_STENCIL_CONTROL, db_stencil_control);
3047
3048 radeon_set_context_reg(ctx_cs, R_028000_DB_RENDER_CONTROL, db_render_control);
3049 radeon_set_context_reg(ctx_cs, R_02800C_DB_RENDER_OVERRIDE, db_render_override);
3050 radeon_set_context_reg(ctx_cs, R_028010_DB_RENDER_OVERRIDE2, db_render_override2);
3051 }
3052
3053 static void
3054 radv_pipeline_generate_blend_state(struct radeon_cmdbuf *ctx_cs,
3055 struct radv_pipeline *pipeline,
3056 const struct radv_blend_state *blend)
3057 {
3058 radeon_set_context_reg_seq(ctx_cs, R_028780_CB_BLEND0_CONTROL, 8);
3059 radeon_emit_array(ctx_cs, blend->cb_blend_control,
3060 8);
3061 radeon_set_context_reg(ctx_cs, R_028808_CB_COLOR_CONTROL, blend->cb_color_control);
3062 radeon_set_context_reg(ctx_cs, R_028B70_DB_ALPHA_TO_MASK, blend->db_alpha_to_mask);
3063
3064 if (pipeline->device->physical_device->has_rbplus) {
3065
3066 radeon_set_context_reg_seq(ctx_cs, R_028760_SX_MRT0_BLEND_OPT, 8);
3067 radeon_emit_array(ctx_cs, blend->sx_mrt_blend_opt, 8);
3068 }
3069
3070 radeon_set_context_reg(ctx_cs, R_028714_SPI_SHADER_COL_FORMAT, blend->spi_shader_col_format);
3071
3072 radeon_set_context_reg(ctx_cs, R_028238_CB_TARGET_MASK, blend->cb_target_mask);
3073 radeon_set_context_reg(ctx_cs, R_02823C_CB_SHADER_MASK, blend->cb_shader_mask);
3074
3075 pipeline->graphics.col_format = blend->spi_shader_col_format;
3076 pipeline->graphics.cb_target_mask = blend->cb_target_mask;
3077 }
3078
3079 static const VkConservativeRasterizationModeEXT
3080 radv_get_conservative_raster_mode(const VkPipelineRasterizationStateCreateInfo *pCreateInfo)
3081 {
3082 const VkPipelineRasterizationConservativeStateCreateInfoEXT *conservative_raster =
3083 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_RASTERIZATION_CONSERVATIVE_STATE_CREATE_INFO_EXT);
3084
3085 if (!conservative_raster)
3086 return VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT;
3087 return conservative_raster->conservativeRasterizationMode;
3088 }
3089
3090 static void
3091 radv_pipeline_generate_raster_state(struct radeon_cmdbuf *ctx_cs,
3092 struct radv_pipeline *pipeline,
3093 const VkGraphicsPipelineCreateInfo *pCreateInfo)
3094 {
3095 const VkPipelineRasterizationStateCreateInfo *vkraster = pCreateInfo->pRasterizationState;
3096 const VkConservativeRasterizationModeEXT mode =
3097 radv_get_conservative_raster_mode(vkraster);
3098 uint32_t pa_sc_conservative_rast = S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1);
3099 bool depth_clip_disable = vkraster->depthClampEnable;
3100
3101 const VkPipelineRasterizationDepthClipStateCreateInfoEXT *depth_clip_state =
3102 vk_find_struct_const(vkraster->pNext, PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT);
3103 if (depth_clip_state) {
3104 depth_clip_disable = !depth_clip_state->depthClipEnable;
3105 }
3106
3107 radeon_set_context_reg(ctx_cs, R_028810_PA_CL_CLIP_CNTL,
3108 S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.
3109 S_028810_ZCLIP_NEAR_DISABLE(depth_clip_disable ? 1 : 0) |
3110 S_028810_ZCLIP_FAR_DISABLE(depth_clip_disable ? 1 : 0) |
3111 S_028810_DX_RASTERIZATION_KILL(vkraster->rasterizerDiscardEnable ? 1 : 0) |
3112 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1));
3113
3114 radeon_set_context_reg(ctx_cs, R_0286D4_SPI_INTERP_CONTROL_0,
3115 S_0286D4_FLAT_SHADE_ENA(1) |
3116 S_0286D4_PNT_SPRITE_ENA(1) |
3117 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
3118 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
3119 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
3120 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
3121 S_0286D4_PNT_SPRITE_TOP_1(0)); /* vulkan is top to bottom - 1.0 at bottom */
3122
3123 radeon_set_context_reg(ctx_cs, R_028BE4_PA_SU_VTX_CNTL,
3124 S_028BE4_PIX_CENTER(1) | // TODO verify
3125 S_028BE4_ROUND_MODE(V_028BE4_X_ROUND_TO_EVEN) |
3126 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH));
3127
3128 radeon_set_context_reg(ctx_cs, R_028814_PA_SU_SC_MODE_CNTL,
3129 S_028814_FACE(vkraster->frontFace) |
3130 S_028814_CULL_FRONT(!!(vkraster->cullMode & VK_CULL_MODE_FRONT_BIT)) |
3131 S_028814_CULL_BACK(!!(vkraster->cullMode & VK_CULL_MODE_BACK_BIT)) |
3132 S_028814_POLY_MODE(vkraster->polygonMode != VK_POLYGON_MODE_FILL) |
3133 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(vkraster->polygonMode)) |
3134 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(vkraster->polygonMode)) |
3135 S_028814_POLY_OFFSET_FRONT_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
3136 S_028814_POLY_OFFSET_BACK_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
3137 S_028814_POLY_OFFSET_PARA_ENABLE(vkraster->depthBiasEnable ? 1 : 0));
3138
3139 /* Conservative rasterization. */
3140 if (mode != VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT) {
3141 struct radv_multisample_state *ms = &pipeline->graphics.ms;
3142
3143 ms->pa_sc_aa_config |= S_028BE0_AA_MASK_CENTROID_DTMN(1);
3144 ms->db_eqaa |= S_028804_ENABLE_POSTZ_OVERRASTERIZATION(1) |
3145 S_028804_OVERRASTERIZATION_AMOUNT(4);
3146
3147 pa_sc_conservative_rast = S_028C4C_PREZ_AA_MASK_ENABLE(1) |
3148 S_028C4C_POSTZ_AA_MASK_ENABLE(1) |
3149 S_028C4C_CENTROID_SAMPLE_OVERRIDE(1);
3150
3151 if (mode == VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT) {
3152 pa_sc_conservative_rast |=
3153 S_028C4C_OVER_RAST_ENABLE(1) |
3154 S_028C4C_OVER_RAST_SAMPLE_SELECT(0) |
3155 S_028C4C_UNDER_RAST_ENABLE(0) |
3156 S_028C4C_UNDER_RAST_SAMPLE_SELECT(1) |
3157 S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(1);
3158 } else {
3159 assert(mode == VK_CONSERVATIVE_RASTERIZATION_MODE_UNDERESTIMATE_EXT);
3160 pa_sc_conservative_rast |=
3161 S_028C4C_OVER_RAST_ENABLE(0) |
3162 S_028C4C_OVER_RAST_SAMPLE_SELECT(1) |
3163 S_028C4C_UNDER_RAST_ENABLE(1) |
3164 S_028C4C_UNDER_RAST_SAMPLE_SELECT(0) |
3165 S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(0);
3166 }
3167 }
3168
3169 radeon_set_context_reg(ctx_cs, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL,
3170 pa_sc_conservative_rast);
3171 }
3172
3173
3174 static void
3175 radv_pipeline_generate_multisample_state(struct radeon_cmdbuf *ctx_cs,
3176 struct radv_pipeline *pipeline)
3177 {
3178 struct radv_multisample_state *ms = &pipeline->graphics.ms;
3179
3180 radeon_set_context_reg_seq(ctx_cs, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0, 2);
3181 radeon_emit(ctx_cs, ms->pa_sc_aa_mask[0]);
3182 radeon_emit(ctx_cs, ms->pa_sc_aa_mask[1]);
3183
3184 radeon_set_context_reg(ctx_cs, R_028804_DB_EQAA, ms->db_eqaa);
3185 radeon_set_context_reg(ctx_cs, R_028A4C_PA_SC_MODE_CNTL_1, ms->pa_sc_mode_cntl_1);
3186
3187 /* The exclusion bits can be set to improve rasterization efficiency
3188 * if no sample lies on the pixel boundary (-8 sample offset). It's
3189 * currently always TRUE because the driver doesn't support 16 samples.
3190 */
3191 bool exclusion = pipeline->device->physical_device->rad_info.chip_class >= GFX7;
3192 radeon_set_context_reg(ctx_cs, R_02882C_PA_SU_PRIM_FILTER_CNTL,
3193 S_02882C_XMAX_RIGHT_EXCLUSION(exclusion) |
3194 S_02882C_YMAX_BOTTOM_EXCLUSION(exclusion));
3195 }
3196
3197 static void
3198 radv_pipeline_generate_vgt_gs_mode(struct radeon_cmdbuf *ctx_cs,
3199 struct radv_pipeline *pipeline)
3200 {
3201 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3202 unsigned vgt_primitiveid_en = 0;
3203 uint32_t vgt_gs_mode = 0;
3204
3205 if (radv_pipeline_has_gs(pipeline)) {
3206 const struct radv_shader_variant *gs =
3207 pipeline->shaders[MESA_SHADER_GEOMETRY];
3208
3209 vgt_gs_mode = ac_vgt_gs_mode(gs->info.gs.vertices_out,
3210 pipeline->device->physical_device->rad_info.chip_class);
3211 } else if (radv_pipeline_has_ngg(pipeline)) {
3212 const struct radv_shader_variant *vs =
3213 pipeline->shaders[MESA_SHADER_VERTEX];
3214 bool enable_prim_id =
3215 outinfo->export_prim_id || vs->info.info.uses_prim_id;
3216
3217 vgt_primitiveid_en |= S_028A84_PRIMITIVEID_EN(enable_prim_id) |
3218 S_028A84_NGG_DISABLE_PROVOK_REUSE(enable_prim_id);
3219 } else if (outinfo->export_prim_id) {
3220 vgt_gs_mode = S_028A40_MODE(V_028A40_GS_SCENARIO_A);
3221 vgt_primitiveid_en |= S_028A84_PRIMITIVEID_EN(1);
3222 }
3223
3224 radeon_set_context_reg(ctx_cs, R_028A84_VGT_PRIMITIVEID_EN, vgt_primitiveid_en);
3225 radeon_set_context_reg(ctx_cs, R_028A40_VGT_GS_MODE, vgt_gs_mode);
3226 }
3227
3228 static void
3229 radv_pipeline_generate_hw_vs(struct radeon_cmdbuf *ctx_cs,
3230 struct radeon_cmdbuf *cs,
3231 struct radv_pipeline *pipeline,
3232 struct radv_shader_variant *shader)
3233 {
3234 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3235
3236 radeon_set_sh_reg_seq(cs, R_00B120_SPI_SHADER_PGM_LO_VS, 4);
3237 radeon_emit(cs, va >> 8);
3238 radeon_emit(cs, S_00B124_MEM_BASE(va >> 40));
3239 radeon_emit(cs, shader->config.rsrc1);
3240 radeon_emit(cs, shader->config.rsrc2);
3241
3242 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3243 unsigned clip_dist_mask, cull_dist_mask, total_mask;
3244 clip_dist_mask = outinfo->clip_dist_mask;
3245 cull_dist_mask = outinfo->cull_dist_mask;
3246 total_mask = clip_dist_mask | cull_dist_mask;
3247 bool misc_vec_ena = outinfo->writes_pointsize ||
3248 outinfo->writes_layer ||
3249 outinfo->writes_viewport_index;
3250
3251 radeon_set_context_reg(ctx_cs, R_0286C4_SPI_VS_OUT_CONFIG,
3252 S_0286C4_VS_EXPORT_COUNT(MAX2(1, outinfo->param_exports) - 1));
3253
3254 radeon_set_context_reg(ctx_cs, R_02870C_SPI_SHADER_POS_FORMAT,
3255 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
3256 S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ?
3257 V_02870C_SPI_SHADER_4COMP :
3258 V_02870C_SPI_SHADER_NONE) |
3259 S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ?
3260 V_02870C_SPI_SHADER_4COMP :
3261 V_02870C_SPI_SHADER_NONE) |
3262 S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ?
3263 V_02870C_SPI_SHADER_4COMP :
3264 V_02870C_SPI_SHADER_NONE));
3265
3266 radeon_set_context_reg(ctx_cs, R_028818_PA_CL_VTE_CNTL,
3267 S_028818_VTX_W0_FMT(1) |
3268 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
3269 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
3270 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
3271
3272 radeon_set_context_reg(ctx_cs, R_02881C_PA_CL_VS_OUT_CNTL,
3273 S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |
3274 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |
3275 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |
3276 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |
3277 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |
3278 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |
3279 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |
3280 cull_dist_mask << 8 |
3281 clip_dist_mask);
3282
3283 if (pipeline->device->physical_device->rad_info.chip_class <= GFX8)
3284 radeon_set_context_reg(ctx_cs, R_028AB4_VGT_REUSE_OFF,
3285 outinfo->writes_viewport_index);
3286 }
3287
3288 static void
3289 radv_pipeline_generate_hw_es(struct radeon_cmdbuf *cs,
3290 struct radv_pipeline *pipeline,
3291 struct radv_shader_variant *shader)
3292 {
3293 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3294
3295 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 4);
3296 radeon_emit(cs, va >> 8);
3297 radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));
3298 radeon_emit(cs, shader->config.rsrc1);
3299 radeon_emit(cs, shader->config.rsrc2);
3300 }
3301
3302 static void
3303 radv_pipeline_generate_hw_ls(struct radeon_cmdbuf *cs,
3304 struct radv_pipeline *pipeline,
3305 struct radv_shader_variant *shader,
3306 const struct radv_tessellation_state *tess)
3307 {
3308 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3309 uint32_t rsrc2 = shader->config.rsrc2;
3310
3311 radeon_set_sh_reg_seq(cs, R_00B520_SPI_SHADER_PGM_LO_LS, 2);
3312 radeon_emit(cs, va >> 8);
3313 radeon_emit(cs, S_00B524_MEM_BASE(va >> 40));
3314
3315 rsrc2 |= S_00B52C_LDS_SIZE(tess->lds_size);
3316 if (pipeline->device->physical_device->rad_info.chip_class == GFX7 &&
3317 pipeline->device->physical_device->rad_info.family != CHIP_HAWAII)
3318 radeon_set_sh_reg(cs, R_00B52C_SPI_SHADER_PGM_RSRC2_LS, rsrc2);
3319
3320 radeon_set_sh_reg_seq(cs, R_00B528_SPI_SHADER_PGM_RSRC1_LS, 2);
3321 radeon_emit(cs, shader->config.rsrc1);
3322 radeon_emit(cs, rsrc2);
3323 }
3324
3325 static void
3326 radv_pipeline_generate_hw_ngg(struct radeon_cmdbuf *ctx_cs,
3327 struct radeon_cmdbuf *cs,
3328 struct radv_pipeline *pipeline,
3329 struct radv_shader_variant *shader,
3330 const struct radv_ngg_state *ngg_state)
3331 {
3332 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3333
3334 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 2);
3335 radeon_emit(cs, va >> 8);
3336 radeon_emit(cs, va >> 40);
3337 radeon_set_sh_reg_seq(cs, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 2);
3338 radeon_emit(cs, shader->config.rsrc1);
3339 radeon_emit(cs, shader->config.rsrc2);
3340
3341 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3342 unsigned clip_dist_mask, cull_dist_mask, total_mask;
3343 clip_dist_mask = outinfo->clip_dist_mask;
3344 cull_dist_mask = outinfo->cull_dist_mask;
3345 total_mask = clip_dist_mask | cull_dist_mask;
3346 bool misc_vec_ena = outinfo->writes_pointsize ||
3347 outinfo->writes_layer ||
3348 outinfo->writes_viewport_index;
3349 bool break_wave_at_eoi = false;
3350
3351 radeon_set_context_reg(ctx_cs, R_0286C4_SPI_VS_OUT_CONFIG,
3352 S_0286C4_VS_EXPORT_COUNT(MAX2(1, outinfo->param_exports) - 1));
3353 radeon_set_context_reg(ctx_cs, R_028708_SPI_SHADER_IDX_FORMAT,
3354 S_028708_IDX0_EXPORT_FORMAT(V_028708_SPI_SHADER_1COMP));
3355 radeon_set_context_reg(ctx_cs, R_02870C_SPI_SHADER_POS_FORMAT,
3356 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
3357 S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ?
3358 V_02870C_SPI_SHADER_4COMP :
3359 V_02870C_SPI_SHADER_NONE) |
3360 S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ?
3361 V_02870C_SPI_SHADER_4COMP :
3362 V_02870C_SPI_SHADER_NONE) |
3363 S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ?
3364 V_02870C_SPI_SHADER_4COMP :
3365 V_02870C_SPI_SHADER_NONE));
3366
3367 radeon_set_context_reg(ctx_cs, R_028818_PA_CL_VTE_CNTL,
3368 S_028818_VTX_W0_FMT(1) |
3369 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
3370 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
3371 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
3372 radeon_set_context_reg(ctx_cs, R_02881C_PA_CL_VS_OUT_CNTL,
3373 S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |
3374 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |
3375 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |
3376 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |
3377 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |
3378 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |
3379 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |
3380 cull_dist_mask << 8 |
3381 clip_dist_mask);
3382
3383 /* TODO: Correctly set REUSE_OFF */
3384 radeon_set_context_reg(ctx_cs, R_028AB4_VGT_REUSE_OFF,
3385 S_028AB4_REUSE_OFF(0));
3386 radeon_set_context_reg(ctx_cs, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
3387 ngg_state->vgt_esgs_ring_itemsize);
3388
3389 /* NGG specific registers. */
3390 struct radv_shader_variant *gs = pipeline->shaders[MESA_SHADER_GEOMETRY];
3391 uint32_t gs_num_invocations = gs ? gs->info.gs.invocations : 1;
3392
3393 radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL,
3394 S_028A44_ES_VERTS_PER_SUBGRP(ngg_state->hw_max_esverts) |
3395 S_028A44_GS_PRIMS_PER_SUBGRP(ngg_state->max_gsprims) |
3396 S_028A44_GS_INST_PRIMS_IN_SUBGRP(ngg_state->max_gsprims * gs_num_invocations));
3397 radeon_set_context_reg(ctx_cs, R_0287FC_GE_MAX_OUTPUT_PER_SUBGROUP,
3398 S_0287FC_MAX_VERTS_PER_SUBGROUP(ngg_state->max_out_verts));
3399 radeon_set_context_reg(ctx_cs, R_028B4C_GE_NGG_SUBGRP_CNTL,
3400 S_028B4C_PRIM_AMP_FACTOR(ngg_state->prim_amp_factor) |
3401 S_028B4C_THDS_PER_SUBGRP(0)); /* for fast launch */
3402 radeon_set_context_reg(ctx_cs, R_028B90_VGT_GS_INSTANCE_CNT,
3403 S_028B90_CNT(gs_num_invocations) |
3404 S_028B90_ENABLE(gs_num_invocations > 1) |
3405 S_028B90_EN_MAX_VERT_OUT_PER_GS_INSTANCE(ngg_state->max_vert_out_per_gs_instance));
3406
3407 /* User edge flags are set by the pos exports. If user edge flags are
3408 * not used, we must use hw-generated edge flags and pass them via
3409 * the prim export to prevent drawing lines on internal edges of
3410 * decomposed primitives (such as quads) with polygon mode = lines.
3411 *
3412 * TODO: We should combine hw-generated edge flags with user edge
3413 * flags in the shader.
3414 */
3415 radeon_set_context_reg(ctx_cs, R_028838_PA_CL_NGG_CNTL,
3416 S_028838_INDEX_BUF_EDGE_FLAG_ENA(1));
3417
3418 radeon_set_context_reg(ctx_cs, R_03096C_GE_CNTL,
3419 S_03096C_PRIM_GRP_SIZE(ngg_state->max_gsprims) |
3420 S_03096C_VERT_GRP_SIZE(ngg_state->hw_max_esverts) |
3421 S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi));
3422 }
3423
3424 static void
3425 radv_pipeline_generate_hw_hs(struct radeon_cmdbuf *cs,
3426 struct radv_pipeline *pipeline,
3427 struct radv_shader_variant *shader,
3428 const struct radv_tessellation_state *tess)
3429 {
3430 uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;
3431
3432 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {
3433 unsigned hs_rsrc2 = shader->config.rsrc2;
3434
3435 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3436 hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX10(tess->lds_size);
3437 } else {
3438 hs_rsrc2 |= S_00B42C_LDS_SIZE_GFX9(tess->lds_size);
3439 }
3440
3441 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3442 radeon_set_sh_reg_seq(cs, R_00B520_SPI_SHADER_PGM_LO_LS, 2);
3443 radeon_emit(cs, va >> 8);
3444 radeon_emit(cs, S_00B524_MEM_BASE(va >> 40));
3445 } else {
3446 radeon_set_sh_reg_seq(cs, R_00B410_SPI_SHADER_PGM_LO_LS, 2);
3447 radeon_emit(cs, va >> 8);
3448 radeon_emit(cs, S_00B414_MEM_BASE(va >> 40));
3449 }
3450
3451 radeon_set_sh_reg_seq(cs, R_00B428_SPI_SHADER_PGM_RSRC1_HS, 2);
3452 radeon_emit(cs, shader->config.rsrc1);
3453 radeon_emit(cs, hs_rsrc2);
3454 } else {
3455 radeon_set_sh_reg_seq(cs, R_00B420_SPI_SHADER_PGM_LO_HS, 4);
3456 radeon_emit(cs, va >> 8);
3457 radeon_emit(cs, S_00B424_MEM_BASE(va >> 40));
3458 radeon_emit(cs, shader->config.rsrc1);
3459 radeon_emit(cs, shader->config.rsrc2);
3460 }
3461 }
3462
3463 static void
3464 radv_pipeline_generate_vertex_shader(struct radeon_cmdbuf *ctx_cs,
3465 struct radeon_cmdbuf *cs,
3466 struct radv_pipeline *pipeline,
3467 const struct radv_tessellation_state *tess,
3468 const struct radv_ngg_state *ngg)
3469 {
3470 struct radv_shader_variant *vs;
3471
3472 /* Skip shaders merged into HS/GS */
3473 vs = pipeline->shaders[MESA_SHADER_VERTEX];
3474 if (!vs)
3475 return;
3476
3477 if (vs->info.vs.as_ls)
3478 radv_pipeline_generate_hw_ls(cs, pipeline, vs, tess);
3479 else if (vs->info.vs.as_es)
3480 radv_pipeline_generate_hw_es(cs, pipeline, vs);
3481 else if (vs->info.is_ngg)
3482 radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, vs, ngg);
3483 else
3484 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, vs);
3485 }
3486
3487 static void
3488 radv_pipeline_generate_tess_shaders(struct radeon_cmdbuf *ctx_cs,
3489 struct radeon_cmdbuf *cs,
3490 struct radv_pipeline *pipeline,
3491 const struct radv_tessellation_state *tess)
3492 {
3493 if (!radv_pipeline_has_tess(pipeline))
3494 return;
3495
3496 struct radv_shader_variant *tes, *tcs;
3497
3498 tcs = pipeline->shaders[MESA_SHADER_TESS_CTRL];
3499 tes = pipeline->shaders[MESA_SHADER_TESS_EVAL];
3500
3501 if (tes) {
3502 if (tes->info.tes.as_es)
3503 radv_pipeline_generate_hw_es(cs, pipeline, tes);
3504 else
3505 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, tes);
3506 }
3507
3508 radv_pipeline_generate_hw_hs(cs, pipeline, tcs, tess);
3509
3510 radeon_set_context_reg(ctx_cs, R_028B6C_VGT_TF_PARAM,
3511 tess->tf_param);
3512
3513 if (pipeline->device->physical_device->rad_info.chip_class >= GFX7)
3514 radeon_set_context_reg_idx(ctx_cs, R_028B58_VGT_LS_HS_CONFIG, 2,
3515 tess->ls_hs_config);
3516 else
3517 radeon_set_context_reg(ctx_cs, R_028B58_VGT_LS_HS_CONFIG,
3518 tess->ls_hs_config);
3519 }
3520
3521 static void
3522 radv_pipeline_generate_geometry_shader(struct radeon_cmdbuf *ctx_cs,
3523 struct radeon_cmdbuf *cs,
3524 struct radv_pipeline *pipeline,
3525 const struct radv_gs_state *gs_state)
3526 {
3527 struct radv_shader_variant *gs;
3528 unsigned gs_max_out_vertices;
3529 uint8_t *num_components;
3530 uint8_t max_stream;
3531 unsigned offset;
3532 uint64_t va;
3533
3534 gs = pipeline->shaders[MESA_SHADER_GEOMETRY];
3535 if (!gs)
3536 return;
3537
3538 gs_max_out_vertices = gs->info.gs.vertices_out;
3539 max_stream = gs->info.info.gs.max_stream;
3540 num_components = gs->info.info.gs.num_stream_output_components;
3541
3542 offset = num_components[0] * gs_max_out_vertices;
3543
3544 radeon_set_context_reg_seq(ctx_cs, R_028A60_VGT_GSVS_RING_OFFSET_1, 3);
3545 radeon_emit(ctx_cs, offset);
3546 if (max_stream >= 1)
3547 offset += num_components[1] * gs_max_out_vertices;
3548 radeon_emit(ctx_cs, offset);
3549 if (max_stream >= 2)
3550 offset += num_components[2] * gs_max_out_vertices;
3551 radeon_emit(ctx_cs, offset);
3552 if (max_stream >= 3)
3553 offset += num_components[3] * gs_max_out_vertices;
3554 radeon_set_context_reg(ctx_cs, R_028AB0_VGT_GSVS_RING_ITEMSIZE, offset);
3555
3556 radeon_set_context_reg(ctx_cs, R_028B38_VGT_GS_MAX_VERT_OUT, gs->info.gs.vertices_out);
3557
3558 radeon_set_context_reg_seq(ctx_cs, R_028B5C_VGT_GS_VERT_ITEMSIZE, 4);
3559 radeon_emit(ctx_cs, num_components[0]);
3560 radeon_emit(ctx_cs, (max_stream >= 1) ? num_components[1] : 0);
3561 radeon_emit(ctx_cs, (max_stream >= 2) ? num_components[2] : 0);
3562 radeon_emit(ctx_cs, (max_stream >= 3) ? num_components[3] : 0);
3563
3564 uint32_t gs_num_invocations = gs->info.gs.invocations;
3565 radeon_set_context_reg(ctx_cs, R_028B90_VGT_GS_INSTANCE_CNT,
3566 S_028B90_CNT(MIN2(gs_num_invocations, 127)) |
3567 S_028B90_ENABLE(gs_num_invocations > 0));
3568
3569 radeon_set_context_reg(ctx_cs, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
3570 gs_state->vgt_esgs_ring_itemsize);
3571
3572 va = radv_buffer_get_va(gs->bo) + gs->bo_offset;
3573
3574 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {
3575 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {
3576 radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 2);
3577 radeon_emit(cs, va >> 8);
3578 radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));
3579 } else {
3580 radeon_set_sh_reg_seq(cs, R_00B210_SPI_SHADER_PGM_LO_ES, 2);
3581 radeon_emit(cs, va >> 8);
3582 radeon_emit(cs, S_00B214_MEM_BASE(va >> 40));
3583 }
3584
3585 radeon_set_sh_reg_seq(cs, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 2);
3586 radeon_emit(cs, gs->config.rsrc1);
3587 radeon_emit(cs, gs->config.rsrc2 | S_00B22C_LDS_SIZE(gs_state->lds_size));
3588
3589 radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL, gs_state->vgt_gs_onchip_cntl);
3590 radeon_set_context_reg(ctx_cs, R_028A94_VGT_GS_MAX_PRIMS_PER_SUBGROUP, gs_state->vgt_gs_max_prims_per_subgroup);
3591 } else {
3592 radeon_set_sh_reg_seq(cs, R_00B220_SPI_SHADER_PGM_LO_GS, 4);
3593 radeon_emit(cs, va >> 8);
3594 radeon_emit(cs, S_00B224_MEM_BASE(va >> 40));
3595 radeon_emit(cs, gs->config.rsrc1);
3596 radeon_emit(cs, gs->config.rsrc2);
3597 }
3598
3599 radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, pipeline->gs_copy_shader);
3600 }
3601
3602 static uint32_t offset_to_ps_input(uint32_t offset, bool flat_shade, bool float16)
3603 {
3604 uint32_t ps_input_cntl;
3605 if (offset <= AC_EXP_PARAM_OFFSET_31) {
3606 ps_input_cntl = S_028644_OFFSET(offset);
3607 if (flat_shade)
3608 ps_input_cntl |= S_028644_FLAT_SHADE(1);
3609 if (float16) {
3610 ps_input_cntl |= S_028644_FP16_INTERP_MODE(1) |
3611 S_028644_ATTR0_VALID(1);
3612 }
3613 } else {
3614 /* The input is a DEFAULT_VAL constant. */
3615 assert(offset >= AC_EXP_PARAM_DEFAULT_VAL_0000 &&
3616 offset <= AC_EXP_PARAM_DEFAULT_VAL_1111);
3617 offset -= AC_EXP_PARAM_DEFAULT_VAL_0000;
3618 ps_input_cntl = S_028644_OFFSET(0x20) |
3619 S_028644_DEFAULT_VAL(offset);
3620 }
3621 return ps_input_cntl;
3622 }
3623
3624 static void
3625 radv_pipeline_generate_ps_inputs(struct radeon_cmdbuf *ctx_cs,
3626 struct radv_pipeline *pipeline)
3627 {
3628 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
3629 const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);
3630 uint32_t ps_input_cntl[32];
3631
3632 unsigned ps_offset = 0;
3633
3634 if (ps->info.info.ps.prim_id_input) {
3635 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID];
3636 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
3637 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false);
3638 ++ps_offset;
3639 }
3640 }
3641
3642 if (ps->info.info.ps.layer_input ||
3643 ps->info.info.needs_multiview_view_index) {
3644 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_LAYER];
3645 if (vs_offset != AC_EXP_PARAM_UNDEFINED)
3646 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false);
3647 else
3648 ps_input_cntl[ps_offset] = offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000, true, false);
3649 ++ps_offset;
3650 }
3651
3652 if (ps->info.info.ps.has_pcoord) {
3653 unsigned val;
3654 val = S_028644_PT_SPRITE_TEX(1) | S_028644_OFFSET(0x20);
3655 ps_input_cntl[ps_offset] = val;
3656 ps_offset++;
3657 }
3658
3659 if (ps->info.info.ps.num_input_clips_culls) {
3660 unsigned vs_offset;
3661
3662 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_CLIP_DIST0];
3663 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
3664 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, false, false);
3665 ++ps_offset;
3666 }
3667
3668 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_CLIP_DIST1];
3669 if (vs_offset != AC_EXP_PARAM_UNDEFINED &&
3670 ps->info.info.ps.num_input_clips_culls > 4) {
3671 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, false, false);
3672 ++ps_offset;
3673 }
3674 }
3675
3676 for (unsigned i = 0; i < 32 && (1u << i) <= ps->info.fs.input_mask; ++i) {
3677 unsigned vs_offset;
3678 bool flat_shade;
3679 bool float16;
3680 if (!(ps->info.fs.input_mask & (1u << i)))
3681 continue;
3682
3683 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_VAR0 + i];
3684 if (vs_offset == AC_EXP_PARAM_UNDEFINED) {
3685 ps_input_cntl[ps_offset] = S_028644_OFFSET(0x20);
3686 ++ps_offset;
3687 continue;
3688 }
3689
3690 flat_shade = !!(ps->info.fs.flat_shaded_mask & (1u << ps_offset));
3691 float16 = !!(ps->info.fs.float16_shaded_mask & (1u << ps_offset));
3692
3693 ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, flat_shade, float16);
3694 ++ps_offset;
3695 }
3696
3697 if (ps_offset) {
3698 radeon_set_context_reg_seq(ctx_cs, R_028644_SPI_PS_INPUT_CNTL_0, ps_offset);
3699 for (unsigned i = 0; i < ps_offset; i++) {
3700 radeon_emit(ctx_cs, ps_input_cntl[i]);
3701 }
3702 }
3703 }
3704
3705 static uint32_t
3706 radv_compute_db_shader_control(const struct radv_device *device,
3707 const struct radv_pipeline *pipeline,
3708 const struct radv_shader_variant *ps)
3709 {
3710 unsigned z_order;
3711 if (ps->info.fs.early_fragment_test || !ps->info.info.ps.writes_memory)
3712 z_order = V_02880C_EARLY_Z_THEN_LATE_Z;
3713 else
3714 z_order = V_02880C_LATE_Z;
3715
3716 bool disable_rbplus = device->physical_device->has_rbplus &&
3717 !device->physical_device->rbplus_allowed;
3718
3719 /* It shouldn't be needed to export gl_SampleMask when MSAA is disabled
3720 * but this appears to break Project Cars (DXVK). See
3721 * https://bugs.freedesktop.org/show_bug.cgi?id=109401
3722 */
3723 bool mask_export_enable = ps->info.info.ps.writes_sample_mask;
3724
3725 return S_02880C_Z_EXPORT_ENABLE(ps->info.info.ps.writes_z) |
3726 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps->info.info.ps.writes_stencil) |
3727 S_02880C_KILL_ENABLE(!!ps->info.fs.can_discard) |
3728 S_02880C_MASK_EXPORT_ENABLE(mask_export_enable) |
3729 S_02880C_Z_ORDER(z_order) |
3730 S_02880C_DEPTH_BEFORE_SHADER(ps->info.fs.early_fragment_test) |
3731 S_02880C_EXEC_ON_HIER_FAIL(ps->info.info.ps.writes_memory) |
3732 S_02880C_EXEC_ON_NOOP(ps->info.info.ps.writes_memory) |
3733 S_02880C_DUAL_QUAD_DISABLE(disable_rbplus);
3734 }
3735
3736 static void
3737 radv_pipeline_generate_fragment_shader(struct radeon_cmdbuf *ctx_cs,
3738 struct radeon_cmdbuf *cs,
3739 struct radv_pipeline *pipeline)
3740 {
3741 struct radv_shader_variant *ps;
3742 uint64_t va;
3743 assert (pipeline->shaders[MESA_SHADER_FRAGMENT]);
3744
3745 ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
3746 va = radv_buffer_get_va(ps->bo) + ps->bo_offset;
3747
3748 radeon_set_sh_reg_seq(cs, R_00B020_SPI_SHADER_PGM_LO_PS, 4);
3749 radeon_emit(cs, va >> 8);
3750 radeon_emit(cs, S_00B024_MEM_BASE(va >> 40));
3751 radeon_emit(cs, ps->config.rsrc1);
3752 radeon_emit(cs, ps->config.rsrc2);
3753
3754 radeon_set_context_reg(ctx_cs, R_02880C_DB_SHADER_CONTROL,
3755 radv_compute_db_shader_control(pipeline->device,
3756 pipeline, ps));
3757
3758 radeon_set_context_reg(ctx_cs, R_0286CC_SPI_PS_INPUT_ENA,
3759 ps->config.spi_ps_input_ena);
3760
3761 radeon_set_context_reg(ctx_cs, R_0286D0_SPI_PS_INPUT_ADDR,
3762 ps->config.spi_ps_input_addr);
3763
3764 radeon_set_context_reg(ctx_cs, R_0286D8_SPI_PS_IN_CONTROL,
3765 S_0286D8_NUM_INTERP(ps->info.fs.num_interp));
3766
3767 radeon_set_context_reg(ctx_cs, R_0286E0_SPI_BARYC_CNTL, pipeline->graphics.spi_baryc_cntl);
3768
3769 radeon_set_context_reg(ctx_cs, R_028710_SPI_SHADER_Z_FORMAT,
3770 ac_get_spi_shader_z_format(ps->info.info.ps.writes_z,
3771 ps->info.info.ps.writes_stencil,
3772 ps->info.info.ps.writes_sample_mask));
3773
3774 if (pipeline->device->dfsm_allowed) {
3775 /* optimise this? */
3776 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
3777 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
3778 }
3779 }
3780
3781 static void
3782 radv_pipeline_generate_vgt_vertex_reuse(struct radeon_cmdbuf *ctx_cs,
3783 struct radv_pipeline *pipeline)
3784 {
3785 if (pipeline->device->physical_device->rad_info.family < CHIP_POLARIS10 ||
3786 pipeline->device->physical_device->rad_info.chip_class >= GFX10)
3787 return;
3788
3789 unsigned vtx_reuse_depth = 30;
3790 if (radv_pipeline_has_tess(pipeline) &&
3791 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.tes.spacing == TESS_SPACING_FRACTIONAL_ODD) {
3792 vtx_reuse_depth = 14;
3793 }
3794 radeon_set_context_reg(ctx_cs, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL,
3795 S_028C58_VTX_REUSE_DEPTH(vtx_reuse_depth));
3796 }
3797
3798 static uint32_t
3799 radv_compute_vgt_shader_stages_en(const struct radv_pipeline *pipeline)
3800 {
3801 uint32_t stages = 0;
3802 if (radv_pipeline_has_tess(pipeline)) {
3803 stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) |
3804 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
3805
3806 if (radv_pipeline_has_gs(pipeline))
3807 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) |
3808 S_028B54_GS_EN(1) |
3809 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
3810 else if (radv_pipeline_has_ngg(pipeline))
3811 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) |
3812 S_028B54_PRIMGEN_EN(1);
3813 else
3814 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS);
3815
3816 } else if (radv_pipeline_has_gs(pipeline)) {
3817 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) |
3818 S_028B54_GS_EN(1) |
3819 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
3820 } else if (radv_pipeline_has_ngg(pipeline)) {
3821 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) |
3822 S_028B54_PRIMGEN_EN(1);
3823 }
3824
3825 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9)
3826 stages |= S_028B54_MAX_PRIMGRP_IN_WAVE(2);
3827
3828 return stages;
3829 }
3830
3831 static uint32_t
3832 radv_compute_cliprect_rule(const VkGraphicsPipelineCreateInfo *pCreateInfo)
3833 {
3834 const VkPipelineDiscardRectangleStateCreateInfoEXT *discard_rectangle_info =
3835 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);
3836
3837 if (!discard_rectangle_info)
3838 return 0xffff;
3839
3840 unsigned mask = 0;
3841
3842 for (unsigned i = 0; i < (1u << MAX_DISCARD_RECTANGLES); ++i) {
3843 /* Interpret i as a bitmask, and then set the bit in the mask if
3844 * that combination of rectangles in which the pixel is contained
3845 * should pass the cliprect test. */
3846 unsigned relevant_subset = i & ((1u << discard_rectangle_info->discardRectangleCount) - 1);
3847
3848 if (discard_rectangle_info->discardRectangleMode == VK_DISCARD_RECTANGLE_MODE_INCLUSIVE_EXT &&
3849 !relevant_subset)
3850 continue;
3851
3852 if (discard_rectangle_info->discardRectangleMode == VK_DISCARD_RECTANGLE_MODE_EXCLUSIVE_EXT &&
3853 relevant_subset)
3854 continue;
3855
3856 mask |= 1u << i;
3857 }
3858
3859 return mask;
3860 }
3861
3862 static void
3863 gfx10_pipeline_generate_ge_cntl(struct radeon_cmdbuf *ctx_cs,
3864 struct radv_pipeline *pipeline,
3865 const struct radv_tessellation_state *tess,
3866 const struct radv_gs_state *gs_state)
3867 {
3868 bool break_wave_at_eoi = false;
3869 unsigned primgroup_size;
3870 unsigned vertgroup_size;
3871
3872 if (radv_pipeline_has_tess(pipeline)) {
3873 primgroup_size = tess->num_patches; /* must be a multiple of NUM_PATCHES */
3874 vertgroup_size = 0;
3875 } else if (radv_pipeline_has_gs(pipeline)) {
3876 unsigned vgt_gs_onchip_cntl = gs_state->vgt_gs_onchip_cntl;
3877 primgroup_size = G_028A44_GS_PRIMS_PER_SUBGRP(vgt_gs_onchip_cntl);
3878 vertgroup_size = G_028A44_ES_VERTS_PER_SUBGRP(vgt_gs_onchip_cntl);
3879 } else {
3880 primgroup_size = 128; /* recommended without a GS and tess */
3881 vertgroup_size = 0;
3882 }
3883
3884 if (radv_pipeline_has_tess(pipeline)) {
3885 if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.info.uses_prim_id ||
3886 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.info.uses_prim_id)
3887 break_wave_at_eoi = true;
3888 }
3889
3890 radeon_set_uconfig_reg(ctx_cs, R_03096C_GE_CNTL,
3891 S_03096C_PRIM_GRP_SIZE(primgroup_size) |
3892 S_03096C_VERT_GRP_SIZE(vertgroup_size) |
3893 S_03096C_PACKET_TO_ONE_PA(0) /* line stipple */ |
3894 S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi));
3895 }
3896
3897 static void
3898 radv_pipeline_generate_pm4(struct radv_pipeline *pipeline,
3899 const VkGraphicsPipelineCreateInfo *pCreateInfo,
3900 const struct radv_graphics_pipeline_create_info *extra,
3901 const struct radv_blend_state *blend,
3902 const struct radv_tessellation_state *tess,
3903 const struct radv_gs_state *gs,
3904 const struct radv_ngg_state *ngg,
3905 unsigned prim, unsigned gs_out)
3906 {
3907 struct radeon_cmdbuf *ctx_cs = &pipeline->ctx_cs;
3908 struct radeon_cmdbuf *cs = &pipeline->cs;
3909
3910 cs->max_dw = 64;
3911 ctx_cs->max_dw = 256;
3912 cs->buf = malloc(4 * (cs->max_dw + ctx_cs->max_dw));
3913 ctx_cs->buf = cs->buf + cs->max_dw;
3914
3915 radv_pipeline_generate_depth_stencil_state(ctx_cs, pipeline, pCreateInfo, extra);
3916 radv_pipeline_generate_blend_state(ctx_cs, pipeline, blend);
3917 radv_pipeline_generate_raster_state(ctx_cs, pipeline, pCreateInfo);
3918 radv_pipeline_generate_multisample_state(ctx_cs, pipeline);
3919 radv_pipeline_generate_vgt_gs_mode(ctx_cs, pipeline);
3920 radv_pipeline_generate_vertex_shader(ctx_cs, cs, pipeline, tess, ngg);
3921 radv_pipeline_generate_tess_shaders(ctx_cs, cs, pipeline, tess);
3922 radv_pipeline_generate_geometry_shader(ctx_cs, cs, pipeline, gs);
3923 radv_pipeline_generate_fragment_shader(ctx_cs, cs, pipeline);
3924 radv_pipeline_generate_ps_inputs(ctx_cs, pipeline);
3925 radv_pipeline_generate_vgt_vertex_reuse(ctx_cs, pipeline);
3926 radv_pipeline_generate_binning_state(ctx_cs, pipeline, pCreateInfo);
3927
3928 if (pipeline->device->physical_device->rad_info.chip_class >= GFX10 && !radv_pipeline_has_ngg(pipeline))
3929 gfx10_pipeline_generate_ge_cntl(ctx_cs, pipeline, tess, gs);
3930
3931 radeon_set_context_reg(ctx_cs, R_0286E8_SPI_TMPRING_SIZE,
3932 S_0286E8_WAVES(pipeline->max_waves) |
3933 S_0286E8_WAVESIZE(pipeline->scratch_bytes_per_wave >> 10));
3934
3935 radeon_set_context_reg(ctx_cs, R_028B54_VGT_SHADER_STAGES_EN, radv_compute_vgt_shader_stages_en(pipeline));
3936
3937 if (pipeline->device->physical_device->rad_info.chip_class >= GFX7) {
3938 radeon_set_uconfig_reg_idx(pipeline->device->physical_device,
3939 cs, R_030908_VGT_PRIMITIVE_TYPE, 1, prim);
3940 } else {
3941 radeon_set_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE, prim);
3942 }
3943 radeon_set_context_reg(ctx_cs, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out);
3944
3945 radeon_set_context_reg(ctx_cs, R_02820C_PA_SC_CLIPRECT_RULE, radv_compute_cliprect_rule(pCreateInfo));
3946
3947 pipeline->ctx_cs_hash = _mesa_hash_data(ctx_cs->buf, ctx_cs->cdw * 4);
3948
3949 assert(ctx_cs->cdw <= ctx_cs->max_dw);
3950 assert(cs->cdw <= cs->max_dw);
3951 }
3952
3953 static struct radv_ia_multi_vgt_param_helpers
3954 radv_compute_ia_multi_vgt_param_helpers(struct radv_pipeline *pipeline,
3955 const struct radv_tessellation_state *tess,
3956 uint32_t prim)
3957 {
3958 struct radv_ia_multi_vgt_param_helpers ia_multi_vgt_param = {0};
3959 const struct radv_device *device = pipeline->device;
3960
3961 if (radv_pipeline_has_tess(pipeline))
3962 ia_multi_vgt_param.primgroup_size = tess->num_patches;
3963 else if (radv_pipeline_has_gs(pipeline))
3964 ia_multi_vgt_param.primgroup_size = 64;
3965 else
3966 ia_multi_vgt_param.primgroup_size = 128; /* recommended without a GS */
3967
3968 /* GS requirement. */
3969 ia_multi_vgt_param.partial_es_wave = false;
3970 if (radv_pipeline_has_gs(pipeline) && device->physical_device->rad_info.chip_class <= GFX8)
3971 if (SI_GS_PER_ES / ia_multi_vgt_param.primgroup_size >= pipeline->device->gs_table_depth - 3)
3972 ia_multi_vgt_param.partial_es_wave = true;
3973
3974 ia_multi_vgt_param.wd_switch_on_eop = false;
3975 if (device->physical_device->rad_info.chip_class >= GFX7) {
3976 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
3977 * 4 shader engines. Set 1 to pass the assertion below.
3978 * The other cases are hardware requirements. */
3979 if (device->physical_device->rad_info.max_se < 4 ||
3980 prim == V_008958_DI_PT_POLYGON ||
3981 prim == V_008958_DI_PT_LINELOOP ||
3982 prim == V_008958_DI_PT_TRIFAN ||
3983 prim == V_008958_DI_PT_TRISTRIP_ADJ ||
3984 (pipeline->graphics.prim_restart_enable &&
3985 (device->physical_device->rad_info.family < CHIP_POLARIS10 ||
3986 (prim != V_008958_DI_PT_POINTLIST &&
3987 prim != V_008958_DI_PT_LINESTRIP))))
3988 ia_multi_vgt_param.wd_switch_on_eop = true;
3989 }
3990
3991 ia_multi_vgt_param.ia_switch_on_eoi = false;
3992 if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.info.ps.prim_id_input)
3993 ia_multi_vgt_param.ia_switch_on_eoi = true;
3994 if (radv_pipeline_has_gs(pipeline) &&
3995 pipeline->shaders[MESA_SHADER_GEOMETRY]->info.info.uses_prim_id)
3996 ia_multi_vgt_param.ia_switch_on_eoi = true;
3997 if (radv_pipeline_has_tess(pipeline)) {
3998 /* SWITCH_ON_EOI must be set if PrimID is used. */
3999 if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.info.uses_prim_id ||
4000 radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.info.uses_prim_id)
4001 ia_multi_vgt_param.ia_switch_on_eoi = true;
4002 }
4003
4004 ia_multi_vgt_param.partial_vs_wave = false;
4005 if (radv_pipeline_has_tess(pipeline)) {
4006 /* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
4007 if ((device->physical_device->rad_info.family == CHIP_TAHITI ||
4008 device->physical_device->rad_info.family == CHIP_PITCAIRN ||
4009 device->physical_device->rad_info.family == CHIP_BONAIRE) &&
4010 radv_pipeline_has_gs(pipeline))
4011 ia_multi_vgt_param.partial_vs_wave = true;
4012 /* Needed for 028B6C_DISTRIBUTION_MODE != 0 */
4013 if (device->has_distributed_tess) {
4014 if (radv_pipeline_has_gs(pipeline)) {
4015 if (device->physical_device->rad_info.chip_class <= GFX8)
4016 ia_multi_vgt_param.partial_es_wave = true;
4017 } else {
4018 ia_multi_vgt_param.partial_vs_wave = true;
4019 }
4020 }
4021 }
4022
4023 /* Workaround for a VGT hang when strip primitive types are used with
4024 * primitive restart.
4025 */
4026 if (pipeline->graphics.prim_restart_enable &&
4027 (prim == V_008958_DI_PT_LINESTRIP ||
4028 prim == V_008958_DI_PT_TRISTRIP ||
4029 prim == V_008958_DI_PT_LINESTRIP_ADJ ||
4030 prim == V_008958_DI_PT_TRISTRIP_ADJ)) {
4031 ia_multi_vgt_param.partial_vs_wave = true;
4032 }
4033
4034 if (radv_pipeline_has_gs(pipeline)) {
4035 /* On these chips there is the possibility of a hang if the
4036 * pipeline uses a GS and partial_vs_wave is not set.
4037 *
4038 * This mostly does not hit 4-SE chips, as those typically set
4039 * ia_switch_on_eoi and then partial_vs_wave is set for pipelines
4040 * with GS due to another workaround.
4041 *
4042 * Reproducer: https://bugs.freedesktop.org/show_bug.cgi?id=109242
4043 */
4044 if (device->physical_device->rad_info.family == CHIP_TONGA ||
4045 device->physical_device->rad_info.family == CHIP_FIJI ||
4046 device->physical_device->rad_info.family == CHIP_POLARIS10 ||
4047 device->physical_device->rad_info.family == CHIP_POLARIS11 ||
4048 device->physical_device->rad_info.family == CHIP_POLARIS12 ||
4049 device->physical_device->rad_info.family == CHIP_VEGAM) {
4050 ia_multi_vgt_param.partial_vs_wave = true;
4051 }
4052 }
4053
4054 ia_multi_vgt_param.base =
4055 S_028AA8_PRIMGROUP_SIZE(ia_multi_vgt_param.primgroup_size - 1) |
4056 /* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */
4057 S_028AA8_MAX_PRIMGRP_IN_WAVE(device->physical_device->rad_info.chip_class == GFX8 ? 2 : 0) |
4058 S_030960_EN_INST_OPT_BASIC(device->physical_device->rad_info.chip_class >= GFX9) |
4059 S_030960_EN_INST_OPT_ADV(device->physical_device->rad_info.chip_class >= GFX9);
4060
4061 return ia_multi_vgt_param;
4062 }
4063
4064
4065 static void
4066 radv_compute_vertex_input_state(struct radv_pipeline *pipeline,
4067 const VkGraphicsPipelineCreateInfo *pCreateInfo)
4068 {
4069 const VkPipelineVertexInputStateCreateInfo *vi_info =
4070 pCreateInfo->pVertexInputState;
4071 struct radv_vertex_elements_info *velems = &pipeline->vertex_elements;
4072
4073 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
4074 const VkVertexInputAttributeDescription *desc =
4075 &vi_info->pVertexAttributeDescriptions[i];
4076 unsigned loc = desc->location;
4077 const struct vk_format_description *format_desc;
4078
4079 format_desc = vk_format_description(desc->format);
4080
4081 velems->format_size[loc] = format_desc->block.bits / 8;
4082 }
4083
4084 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
4085 const VkVertexInputBindingDescription *desc =
4086 &vi_info->pVertexBindingDescriptions[i];
4087
4088 pipeline->binding_stride[desc->binding] = desc->stride;
4089 pipeline->num_vertex_bindings =
4090 MAX2(pipeline->num_vertex_bindings, desc->binding + 1);
4091 }
4092 }
4093
4094 static struct radv_shader_variant *
4095 radv_pipeline_get_streamout_shader(struct radv_pipeline *pipeline)
4096 {
4097 int i;
4098
4099 for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
4100 struct radv_shader_variant *shader =
4101 radv_get_shader(pipeline, i);
4102
4103 if (shader && shader->info.info.so.num_outputs > 0)
4104 return shader;
4105 }
4106
4107 return NULL;
4108 }
4109
4110 static VkResult
4111 radv_pipeline_init(struct radv_pipeline *pipeline,
4112 struct radv_device *device,
4113 struct radv_pipeline_cache *cache,
4114 const VkGraphicsPipelineCreateInfo *pCreateInfo,
4115 const struct radv_graphics_pipeline_create_info *extra)
4116 {
4117 VkResult result;
4118 bool has_view_index = false;
4119
4120 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
4121 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
4122 if (subpass->view_mask)
4123 has_view_index = true;
4124
4125 pipeline->device = device;
4126 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
4127 assert(pipeline->layout);
4128
4129 struct radv_blend_state blend = radv_pipeline_init_blend_state(pipeline, pCreateInfo, extra);
4130
4131 const VkPipelineCreationFeedbackCreateInfoEXT *creation_feedback =
4132 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
4133 radv_init_feedback(creation_feedback);
4134
4135 VkPipelineCreationFeedbackEXT *pipeline_feedback = creation_feedback ? creation_feedback->pPipelineCreationFeedback : NULL;
4136
4137 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
4138 VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = { 0 };
4139 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
4140 gl_shader_stage stage = ffs(pCreateInfo->pStages[i].stage) - 1;
4141 pStages[stage] = &pCreateInfo->pStages[i];
4142 if(creation_feedback)
4143 stage_feedbacks[stage] = &creation_feedback->pPipelineStageCreationFeedbacks[i];
4144 }
4145
4146 struct radv_pipeline_key key = radv_generate_graphics_pipeline_key(pipeline, pCreateInfo, &blend, has_view_index);
4147 radv_create_shaders(pipeline, device, cache, &key, pStages, pCreateInfo->flags, pipeline_feedback, stage_feedbacks);
4148
4149 pipeline->graphics.spi_baryc_cntl = S_0286E0_FRONT_FACE_ALL_BITS(1);
4150 radv_pipeline_init_multisample_state(pipeline, &blend, pCreateInfo);
4151 uint32_t gs_out;
4152 uint32_t prim = si_translate_prim(pCreateInfo->pInputAssemblyState->topology);
4153
4154 pipeline->graphics.can_use_guardband = radv_prim_can_use_guardband(pCreateInfo->pInputAssemblyState->topology);
4155
4156 if (radv_pipeline_has_gs(pipeline)) {
4157 gs_out = si_conv_gl_prim_to_gs_out(pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs.output_prim);
4158 pipeline->graphics.can_use_guardband = gs_out == V_028A6C_OUTPRIM_TYPE_TRISTRIP;
4159 } else {
4160 gs_out = si_conv_prim_to_gs_out(pCreateInfo->pInputAssemblyState->topology);
4161 }
4162 if (extra && extra->use_rectlist) {
4163 prim = V_008958_DI_PT_RECTLIST;
4164 gs_out = V_028A6C_OUTPRIM_TYPE_TRISTRIP;
4165 pipeline->graphics.can_use_guardband = true;
4166 if (radv_pipeline_has_ngg(pipeline))
4167 gs_out = V_028A6C_VGT_OUT_RECT_V0;
4168 }
4169 pipeline->graphics.prim_restart_enable = !!pCreateInfo->pInputAssemblyState->primitiveRestartEnable;
4170 /* prim vertex count will need TESS changes */
4171 pipeline->graphics.prim_vertex_count = prim_size_table[prim];
4172
4173 radv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
4174
4175 /* Ensure that some export memory is always allocated, for two reasons:
4176 *
4177 * 1) Correctness: The hardware ignores the EXEC mask if no export
4178 * memory is allocated, so KILL and alpha test do not work correctly
4179 * without this.
4180 * 2) Performance: Every shader needs at least a NULL export, even when
4181 * it writes no color/depth output. The NULL export instruction
4182 * stalls without this setting.
4183 *
4184 * Don't add this to CB_SHADER_MASK.
4185 */
4186 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
4187 if (!blend.spi_shader_col_format) {
4188 if (!ps->info.info.ps.writes_z &&
4189 !ps->info.info.ps.writes_stencil &&
4190 !ps->info.info.ps.writes_sample_mask)
4191 blend.spi_shader_col_format = V_028714_SPI_SHADER_32_R;
4192 }
4193
4194 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
4195 if (pipeline->shaders[i]) {
4196 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[i]->info.need_indirect_descriptor_sets;
4197 }
4198 }
4199
4200 struct radv_ngg_state ngg = {0};
4201 struct radv_gs_state gs = {0};
4202
4203 if (radv_pipeline_has_ngg(pipeline)) {
4204 ngg = calculate_ngg_info(pCreateInfo, pipeline);
4205 } else if (radv_pipeline_has_gs(pipeline)) {
4206 gs = calculate_gs_info(pCreateInfo, pipeline);
4207 calculate_gs_ring_sizes(pipeline, &gs);
4208 }
4209
4210 struct radv_tessellation_state tess = {0};
4211 if (radv_pipeline_has_tess(pipeline)) {
4212 if (prim == V_008958_DI_PT_PATCH) {
4213 pipeline->graphics.prim_vertex_count.min = pCreateInfo->pTessellationState->patchControlPoints;
4214 pipeline->graphics.prim_vertex_count.incr = 1;
4215 }
4216 tess = calculate_tess_state(pipeline, pCreateInfo);
4217 }
4218
4219 pipeline->graphics.ia_multi_vgt_param = radv_compute_ia_multi_vgt_param_helpers(pipeline, &tess, prim);
4220
4221 radv_compute_vertex_input_state(pipeline, pCreateInfo);
4222
4223 for (uint32_t i = 0; i < MESA_SHADER_STAGES; i++)
4224 pipeline->user_data_0[i] = radv_pipeline_stage_to_user_data_0(pipeline, i, device->physical_device->rad_info.chip_class);
4225
4226 struct radv_userdata_info *loc = radv_lookup_user_sgpr(pipeline, MESA_SHADER_VERTEX,
4227 AC_UD_VS_BASE_VERTEX_START_INSTANCE);
4228 if (loc->sgpr_idx != -1) {
4229 pipeline->graphics.vtx_base_sgpr = pipeline->user_data_0[MESA_SHADER_VERTEX];
4230 pipeline->graphics.vtx_base_sgpr += loc->sgpr_idx * 4;
4231 if (radv_get_shader(pipeline, MESA_SHADER_VERTEX)->info.info.vs.needs_draw_id)
4232 pipeline->graphics.vtx_emit_num = 3;
4233 else
4234 pipeline->graphics.vtx_emit_num = 2;
4235 }
4236
4237 /* Find the last vertex shader stage that eventually uses streamout. */
4238 pipeline->streamout_shader = radv_pipeline_get_streamout_shader(pipeline);
4239
4240 result = radv_pipeline_scratch_init(device, pipeline);
4241 radv_pipeline_generate_pm4(pipeline, pCreateInfo, extra, &blend, &tess, &gs, &ngg, prim, gs_out);
4242
4243 return result;
4244 }
4245
4246 VkResult
4247 radv_graphics_pipeline_create(
4248 VkDevice _device,
4249 VkPipelineCache _cache,
4250 const VkGraphicsPipelineCreateInfo *pCreateInfo,
4251 const struct radv_graphics_pipeline_create_info *extra,
4252 const VkAllocationCallbacks *pAllocator,
4253 VkPipeline *pPipeline)
4254 {
4255 RADV_FROM_HANDLE(radv_device, device, _device);
4256 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
4257 struct radv_pipeline *pipeline;
4258 VkResult result;
4259
4260 pipeline = vk_zalloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
4261 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4262 if (pipeline == NULL)
4263 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
4264
4265 result = radv_pipeline_init(pipeline, device, cache,
4266 pCreateInfo, extra);
4267 if (result != VK_SUCCESS) {
4268 radv_pipeline_destroy(device, pipeline, pAllocator);
4269 return result;
4270 }
4271
4272 *pPipeline = radv_pipeline_to_handle(pipeline);
4273
4274 return VK_SUCCESS;
4275 }
4276
4277 VkResult radv_CreateGraphicsPipelines(
4278 VkDevice _device,
4279 VkPipelineCache pipelineCache,
4280 uint32_t count,
4281 const VkGraphicsPipelineCreateInfo* pCreateInfos,
4282 const VkAllocationCallbacks* pAllocator,
4283 VkPipeline* pPipelines)
4284 {
4285 VkResult result = VK_SUCCESS;
4286 unsigned i = 0;
4287
4288 for (; i < count; i++) {
4289 VkResult r;
4290 r = radv_graphics_pipeline_create(_device,
4291 pipelineCache,
4292 &pCreateInfos[i],
4293 NULL, pAllocator, &pPipelines[i]);
4294 if (r != VK_SUCCESS) {
4295 result = r;
4296 pPipelines[i] = VK_NULL_HANDLE;
4297 }
4298 }
4299
4300 return result;
4301 }
4302
4303
4304 static void
4305 radv_compute_generate_pm4(struct radv_pipeline *pipeline)
4306 {
4307 struct radv_shader_variant *compute_shader;
4308 struct radv_device *device = pipeline->device;
4309 unsigned compute_resource_limits;
4310 unsigned waves_per_threadgroup;
4311 uint64_t va;
4312
4313 pipeline->cs.buf = malloc(20 * 4);
4314 pipeline->cs.max_dw = 20;
4315
4316 compute_shader = pipeline->shaders[MESA_SHADER_COMPUTE];
4317 va = radv_buffer_get_va(compute_shader->bo) + compute_shader->bo_offset;
4318
4319 radeon_set_sh_reg_seq(&pipeline->cs, R_00B830_COMPUTE_PGM_LO, 2);
4320 radeon_emit(&pipeline->cs, va >> 8);
4321 radeon_emit(&pipeline->cs, S_00B834_DATA(va >> 40));
4322
4323 radeon_set_sh_reg_seq(&pipeline->cs, R_00B848_COMPUTE_PGM_RSRC1, 2);
4324 radeon_emit(&pipeline->cs, compute_shader->config.rsrc1);
4325 radeon_emit(&pipeline->cs, compute_shader->config.rsrc2);
4326
4327 radeon_set_sh_reg(&pipeline->cs, R_00B860_COMPUTE_TMPRING_SIZE,
4328 S_00B860_WAVES(pipeline->max_waves) |
4329 S_00B860_WAVESIZE(pipeline->scratch_bytes_per_wave >> 10));
4330
4331 /* Calculate best compute resource limits. */
4332 waves_per_threadgroup =
4333 DIV_ROUND_UP(compute_shader->info.cs.block_size[0] *
4334 compute_shader->info.cs.block_size[1] *
4335 compute_shader->info.cs.block_size[2], 64);
4336 compute_resource_limits =
4337 S_00B854_SIMD_DEST_CNTL(waves_per_threadgroup % 4 == 0);
4338
4339 if (device->physical_device->rad_info.chip_class >= GFX7) {
4340 unsigned num_cu_per_se =
4341 device->physical_device->rad_info.num_good_compute_units /
4342 device->physical_device->rad_info.max_se;
4343
4344 /* Force even distribution on all SIMDs in CU if the workgroup
4345 * size is 64. This has shown some good improvements if # of
4346 * CUs per SE is not a multiple of 4.
4347 */
4348 if (num_cu_per_se % 4 && waves_per_threadgroup == 1)
4349 compute_resource_limits |= S_00B854_FORCE_SIMD_DIST(1);
4350 }
4351
4352 radeon_set_sh_reg(&pipeline->cs, R_00B854_COMPUTE_RESOURCE_LIMITS,
4353 compute_resource_limits);
4354
4355 radeon_set_sh_reg_seq(&pipeline->cs, R_00B81C_COMPUTE_NUM_THREAD_X, 3);
4356 radeon_emit(&pipeline->cs,
4357 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[0]));
4358 radeon_emit(&pipeline->cs,
4359 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[1]));
4360 radeon_emit(&pipeline->cs,
4361 S_00B81C_NUM_THREAD_FULL(compute_shader->info.cs.block_size[2]));
4362
4363 assert(pipeline->cs.cdw <= pipeline->cs.max_dw);
4364 }
4365
4366 static VkResult radv_compute_pipeline_create(
4367 VkDevice _device,
4368 VkPipelineCache _cache,
4369 const VkComputePipelineCreateInfo* pCreateInfo,
4370 const VkAllocationCallbacks* pAllocator,
4371 VkPipeline* pPipeline)
4372 {
4373 RADV_FROM_HANDLE(radv_device, device, _device);
4374 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
4375 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
4376 VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = { 0 };
4377 struct radv_pipeline *pipeline;
4378 VkResult result;
4379
4380 pipeline = vk_zalloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
4381 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4382 if (pipeline == NULL)
4383 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
4384
4385 pipeline->device = device;
4386 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
4387 assert(pipeline->layout);
4388
4389 const VkPipelineCreationFeedbackCreateInfoEXT *creation_feedback =
4390 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
4391 radv_init_feedback(creation_feedback);
4392
4393 VkPipelineCreationFeedbackEXT *pipeline_feedback = creation_feedback ? creation_feedback->pPipelineCreationFeedback : NULL;
4394 if (creation_feedback)
4395 stage_feedbacks[MESA_SHADER_COMPUTE] = &creation_feedback->pPipelineStageCreationFeedbacks[0];
4396
4397 pStages[MESA_SHADER_COMPUTE] = &pCreateInfo->stage;
4398 radv_create_shaders(pipeline, device, cache, &(struct radv_pipeline_key) {0}, pStages, pCreateInfo->flags, pipeline_feedback, stage_feedbacks);
4399
4400 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);
4401 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[MESA_SHADER_COMPUTE]->info.need_indirect_descriptor_sets;
4402 result = radv_pipeline_scratch_init(device, pipeline);
4403 if (result != VK_SUCCESS) {
4404 radv_pipeline_destroy(device, pipeline, pAllocator);
4405 return result;
4406 }
4407
4408 radv_compute_generate_pm4(pipeline);
4409
4410 *pPipeline = radv_pipeline_to_handle(pipeline);
4411
4412 return VK_SUCCESS;
4413 }
4414
4415 VkResult radv_CreateComputePipelines(
4416 VkDevice _device,
4417 VkPipelineCache pipelineCache,
4418 uint32_t count,
4419 const VkComputePipelineCreateInfo* pCreateInfos,
4420 const VkAllocationCallbacks* pAllocator,
4421 VkPipeline* pPipelines)
4422 {
4423 VkResult result = VK_SUCCESS;
4424
4425 unsigned i = 0;
4426 for (; i < count; i++) {
4427 VkResult r;
4428 r = radv_compute_pipeline_create(_device, pipelineCache,
4429 &pCreateInfos[i],
4430 pAllocator, &pPipelines[i]);
4431 if (r != VK_SUCCESS) {
4432 result = r;
4433 pPipelines[i] = VK_NULL_HANDLE;
4434 }
4435 }
4436
4437 return result;
4438 }