radv: Use correct bindings for inputRate in key generation.
[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_shader.h"
33 #include "nir/nir.h"
34 #include "nir/nir_builder.h"
35 #include "spirv/nir_spirv.h"
36 #include "vk_util.h"
37
38 #include <llvm-c/Core.h>
39 #include <llvm-c/TargetMachine.h>
40
41 #include "sid.h"
42 #include "gfx9d.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
51 static void
52 radv_pipeline_destroy(struct radv_device *device,
53 struct radv_pipeline *pipeline,
54 const VkAllocationCallbacks* allocator)
55 {
56 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i)
57 if (pipeline->shaders[i])
58 radv_shader_variant_destroy(device, pipeline->shaders[i]);
59
60 if (pipeline->gs_copy_shader)
61 radv_shader_variant_destroy(device, pipeline->gs_copy_shader);
62
63 vk_free2(&device->alloc, allocator, pipeline);
64 }
65
66 void radv_DestroyPipeline(
67 VkDevice _device,
68 VkPipeline _pipeline,
69 const VkAllocationCallbacks* pAllocator)
70 {
71 RADV_FROM_HANDLE(radv_device, device, _device);
72 RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline);
73
74 if (!_pipeline)
75 return;
76
77 radv_pipeline_destroy(device, pipeline, pAllocator);
78 }
79
80 static void radv_dump_pipeline_stats(struct radv_device *device, struct radv_pipeline *pipeline)
81 {
82 int i;
83
84 for (i = 0; i < MESA_SHADER_STAGES; i++) {
85 if (!pipeline->shaders[i])
86 continue;
87
88 radv_shader_dump_stats(device, pipeline->shaders[i], i, stderr);
89 }
90 }
91
92 static uint32_t get_hash_flags(struct radv_device *device)
93 {
94 uint32_t hash_flags = 0;
95
96 if (device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH)
97 hash_flags |= RADV_HASH_SHADER_UNSAFE_MATH;
98 if (device->instance->perftest_flags & RADV_PERFTEST_SISCHED)
99 hash_flags |= RADV_HASH_SHADER_SISCHED;
100 return hash_flags;
101 }
102
103 static VkResult
104 radv_pipeline_scratch_init(struct radv_device *device,
105 struct radv_pipeline *pipeline)
106 {
107 unsigned scratch_bytes_per_wave = 0;
108 unsigned max_waves = 0;
109 unsigned min_waves = 1;
110
111 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
112 if (pipeline->shaders[i]) {
113 unsigned max_stage_waves = device->scratch_waves;
114
115 scratch_bytes_per_wave = MAX2(scratch_bytes_per_wave,
116 pipeline->shaders[i]->config.scratch_bytes_per_wave);
117
118 max_stage_waves = MIN2(max_stage_waves,
119 4 * device->physical_device->rad_info.num_good_compute_units *
120 (256 / pipeline->shaders[i]->config.num_vgprs));
121 max_waves = MAX2(max_waves, max_stage_waves);
122 }
123 }
124
125 if (pipeline->shaders[MESA_SHADER_COMPUTE]) {
126 unsigned group_size = pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[0] *
127 pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[1] *
128 pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[2];
129 min_waves = MAX2(min_waves, round_up_u32(group_size, 64));
130 }
131
132 if (scratch_bytes_per_wave)
133 max_waves = MIN2(max_waves, 0xffffffffu / scratch_bytes_per_wave);
134
135 if (scratch_bytes_per_wave && max_waves < min_waves) {
136 /* Not really true at this moment, but will be true on first
137 * execution. Avoid having hanging shaders. */
138 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
139 }
140 pipeline->scratch_bytes_per_wave = scratch_bytes_per_wave;
141 pipeline->max_waves = max_waves;
142 return VK_SUCCESS;
143 }
144
145 static uint32_t si_translate_blend_function(VkBlendOp op)
146 {
147 switch (op) {
148 case VK_BLEND_OP_ADD:
149 return V_028780_COMB_DST_PLUS_SRC;
150 case VK_BLEND_OP_SUBTRACT:
151 return V_028780_COMB_SRC_MINUS_DST;
152 case VK_BLEND_OP_REVERSE_SUBTRACT:
153 return V_028780_COMB_DST_MINUS_SRC;
154 case VK_BLEND_OP_MIN:
155 return V_028780_COMB_MIN_DST_SRC;
156 case VK_BLEND_OP_MAX:
157 return V_028780_COMB_MAX_DST_SRC;
158 default:
159 return 0;
160 }
161 }
162
163 static uint32_t si_translate_blend_factor(VkBlendFactor factor)
164 {
165 switch (factor) {
166 case VK_BLEND_FACTOR_ZERO:
167 return V_028780_BLEND_ZERO;
168 case VK_BLEND_FACTOR_ONE:
169 return V_028780_BLEND_ONE;
170 case VK_BLEND_FACTOR_SRC_COLOR:
171 return V_028780_BLEND_SRC_COLOR;
172 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:
173 return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
174 case VK_BLEND_FACTOR_DST_COLOR:
175 return V_028780_BLEND_DST_COLOR;
176 case VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR:
177 return V_028780_BLEND_ONE_MINUS_DST_COLOR;
178 case VK_BLEND_FACTOR_SRC_ALPHA:
179 return V_028780_BLEND_SRC_ALPHA;
180 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:
181 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
182 case VK_BLEND_FACTOR_DST_ALPHA:
183 return V_028780_BLEND_DST_ALPHA;
184 case VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA:
185 return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
186 case VK_BLEND_FACTOR_CONSTANT_COLOR:
187 return V_028780_BLEND_CONSTANT_COLOR;
188 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR:
189 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
190 case VK_BLEND_FACTOR_CONSTANT_ALPHA:
191 return V_028780_BLEND_CONSTANT_ALPHA;
192 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA:
193 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
194 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:
195 return V_028780_BLEND_SRC_ALPHA_SATURATE;
196 case VK_BLEND_FACTOR_SRC1_COLOR:
197 return V_028780_BLEND_SRC1_COLOR;
198 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:
199 return V_028780_BLEND_INV_SRC1_COLOR;
200 case VK_BLEND_FACTOR_SRC1_ALPHA:
201 return V_028780_BLEND_SRC1_ALPHA;
202 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:
203 return V_028780_BLEND_INV_SRC1_ALPHA;
204 default:
205 return 0;
206 }
207 }
208
209 static uint32_t si_translate_blend_opt_function(VkBlendOp op)
210 {
211 switch (op) {
212 case VK_BLEND_OP_ADD:
213 return V_028760_OPT_COMB_ADD;
214 case VK_BLEND_OP_SUBTRACT:
215 return V_028760_OPT_COMB_SUBTRACT;
216 case VK_BLEND_OP_REVERSE_SUBTRACT:
217 return V_028760_OPT_COMB_REVSUBTRACT;
218 case VK_BLEND_OP_MIN:
219 return V_028760_OPT_COMB_MIN;
220 case VK_BLEND_OP_MAX:
221 return V_028760_OPT_COMB_MAX;
222 default:
223 return V_028760_OPT_COMB_BLEND_DISABLED;
224 }
225 }
226
227 static uint32_t si_translate_blend_opt_factor(VkBlendFactor factor, bool is_alpha)
228 {
229 switch (factor) {
230 case VK_BLEND_FACTOR_ZERO:
231 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;
232 case VK_BLEND_FACTOR_ONE:
233 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;
234 case VK_BLEND_FACTOR_SRC_COLOR:
235 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
236 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;
237 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:
238 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
239 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;
240 case VK_BLEND_FACTOR_SRC_ALPHA:
241 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;
242 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:
243 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;
244 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:
245 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
246 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
247 default:
248 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
249 }
250 }
251
252 /**
253 * Get rid of DST in the blend factors by commuting the operands:
254 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
255 */
256 static void si_blend_remove_dst(unsigned *func, unsigned *src_factor,
257 unsigned *dst_factor, unsigned expected_dst,
258 unsigned replacement_src)
259 {
260 if (*src_factor == expected_dst &&
261 *dst_factor == VK_BLEND_FACTOR_ZERO) {
262 *src_factor = VK_BLEND_FACTOR_ZERO;
263 *dst_factor = replacement_src;
264
265 /* Commuting the operands requires reversing subtractions. */
266 if (*func == VK_BLEND_OP_SUBTRACT)
267 *func = VK_BLEND_OP_REVERSE_SUBTRACT;
268 else if (*func == VK_BLEND_OP_REVERSE_SUBTRACT)
269 *func = VK_BLEND_OP_SUBTRACT;
270 }
271 }
272
273 static bool si_blend_factor_uses_dst(unsigned factor)
274 {
275 return factor == VK_BLEND_FACTOR_DST_COLOR ||
276 factor == VK_BLEND_FACTOR_DST_ALPHA ||
277 factor == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
278 factor == VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA ||
279 factor == VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;
280 }
281
282 static bool is_dual_src(VkBlendFactor factor)
283 {
284 switch (factor) {
285 case VK_BLEND_FACTOR_SRC1_COLOR:
286 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:
287 case VK_BLEND_FACTOR_SRC1_ALPHA:
288 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:
289 return true;
290 default:
291 return false;
292 }
293 }
294
295 static unsigned si_choose_spi_color_format(VkFormat vk_format,
296 bool blend_enable,
297 bool blend_need_alpha)
298 {
299 const struct vk_format_description *desc = vk_format_description(vk_format);
300 unsigned format, ntype, swap;
301
302 /* Alpha is needed for alpha-to-coverage.
303 * Blending may be with or without alpha.
304 */
305 unsigned normal = 0; /* most optimal, may not support blending or export alpha */
306 unsigned alpha = 0; /* exports alpha, but may not support blending */
307 unsigned blend = 0; /* supports blending, but may not export alpha */
308 unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */
309
310 format = radv_translate_colorformat(vk_format);
311 ntype = radv_translate_color_numformat(vk_format, desc,
312 vk_format_get_first_non_void_channel(vk_format));
313 swap = radv_translate_colorswap(vk_format, false);
314
315 /* Choose the SPI color formats. These are required values for Stoney/RB+.
316 * Other chips have multiple choices, though they are not necessarily better.
317 */
318 switch (format) {
319 case V_028C70_COLOR_5_6_5:
320 case V_028C70_COLOR_1_5_5_5:
321 case V_028C70_COLOR_5_5_5_1:
322 case V_028C70_COLOR_4_4_4_4:
323 case V_028C70_COLOR_10_11_11:
324 case V_028C70_COLOR_11_11_10:
325 case V_028C70_COLOR_8:
326 case V_028C70_COLOR_8_8:
327 case V_028C70_COLOR_8_8_8_8:
328 case V_028C70_COLOR_10_10_10_2:
329 case V_028C70_COLOR_2_10_10_10:
330 if (ntype == V_028C70_NUMBER_UINT)
331 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
332 else if (ntype == V_028C70_NUMBER_SINT)
333 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
334 else
335 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
336 break;
337
338 case V_028C70_COLOR_16:
339 case V_028C70_COLOR_16_16:
340 case V_028C70_COLOR_16_16_16_16:
341 if (ntype == V_028C70_NUMBER_UNORM ||
342 ntype == V_028C70_NUMBER_SNORM) {
343 /* UNORM16 and SNORM16 don't support blending */
344 if (ntype == V_028C70_NUMBER_UNORM)
345 normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;
346 else
347 normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;
348
349 /* Use 32 bits per channel for blending. */
350 if (format == V_028C70_COLOR_16) {
351 if (swap == V_028C70_SWAP_STD) { /* R */
352 blend = V_028714_SPI_SHADER_32_R;
353 blend_alpha = V_028714_SPI_SHADER_32_AR;
354 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
355 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
356 else
357 assert(0);
358 } else if (format == V_028C70_COLOR_16_16) {
359 if (swap == V_028C70_SWAP_STD) { /* RG */
360 blend = V_028714_SPI_SHADER_32_GR;
361 blend_alpha = V_028714_SPI_SHADER_32_ABGR;
362 } else if (swap == V_028C70_SWAP_ALT) /* RA */
363 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
364 else
365 assert(0);
366 } else /* 16_16_16_16 */
367 blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
368 } else if (ntype == V_028C70_NUMBER_UINT)
369 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
370 else if (ntype == V_028C70_NUMBER_SINT)
371 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
372 else if (ntype == V_028C70_NUMBER_FLOAT)
373 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
374 else
375 assert(0);
376 break;
377
378 case V_028C70_COLOR_32:
379 if (swap == V_028C70_SWAP_STD) { /* R */
380 blend = normal = V_028714_SPI_SHADER_32_R;
381 alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;
382 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
383 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
384 else
385 assert(0);
386 break;
387
388 case V_028C70_COLOR_32_32:
389 if (swap == V_028C70_SWAP_STD) { /* RG */
390 blend = normal = V_028714_SPI_SHADER_32_GR;
391 alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
392 } else if (swap == V_028C70_SWAP_ALT) /* RA */
393 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
394 else
395 assert(0);
396 break;
397
398 case V_028C70_COLOR_32_32_32_32:
399 case V_028C70_COLOR_8_24:
400 case V_028C70_COLOR_24_8:
401 case V_028C70_COLOR_X24_8_32_FLOAT:
402 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
403 break;
404
405 default:
406 unreachable("unhandled blend format");
407 }
408
409 if (blend_enable && blend_need_alpha)
410 return blend_alpha;
411 else if(blend_need_alpha)
412 return alpha;
413 else if(blend_enable)
414 return blend;
415 else
416 return normal;
417 }
418
419 static void
420 radv_pipeline_compute_spi_color_formats(struct radv_pipeline *pipeline,
421 const VkGraphicsPipelineCreateInfo *pCreateInfo,
422 uint32_t blend_enable,
423 uint32_t blend_need_alpha,
424 bool single_cb_enable,
425 bool blend_mrt0_is_dual_src)
426 {
427 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
428 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
429 struct radv_blend_state *blend = &pipeline->graphics.blend;
430 unsigned col_format = 0;
431
432 for (unsigned i = 0; i < (single_cb_enable ? 1 : subpass->color_count); ++i) {
433 unsigned cf;
434
435 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED) {
436 cf = V_028714_SPI_SHADER_ZERO;
437 } else {
438 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->color_attachments[i].attachment;
439
440 cf = si_choose_spi_color_format(attachment->format,
441 blend_enable & (1 << i),
442 blend_need_alpha & (1 << i));
443 }
444
445 col_format |= cf << (4 * i);
446 }
447
448 blend->cb_shader_mask = ac_get_cb_shader_mask(col_format);
449
450 if (blend_mrt0_is_dual_src)
451 col_format |= (col_format & 0xf) << 4;
452 blend->spi_shader_col_format = col_format;
453 }
454
455 static bool
456 format_is_int8(VkFormat format)
457 {
458 const struct vk_format_description *desc = vk_format_description(format);
459 int channel = vk_format_get_first_non_void_channel(format);
460
461 return channel >= 0 && desc->channel[channel].pure_integer &&
462 desc->channel[channel].size == 8;
463 }
464
465 static bool
466 format_is_int10(VkFormat format)
467 {
468 const struct vk_format_description *desc = vk_format_description(format);
469
470 if (desc->nr_channels != 4)
471 return false;
472 for (unsigned i = 0; i < 4; i++) {
473 if (desc->channel[i].pure_integer && desc->channel[i].size == 10)
474 return true;
475 }
476 return false;
477 }
478
479 unsigned radv_format_meta_fs_key(VkFormat format)
480 {
481 unsigned col_format = si_choose_spi_color_format(format, false, false) - 1;
482 bool is_int8 = format_is_int8(format);
483 bool is_int10 = format_is_int10(format);
484
485 return col_format + (is_int8 ? 3 : is_int10 ? 5 : 0);
486 }
487
488 static void
489 radv_pipeline_compute_get_int_clamp(const VkGraphicsPipelineCreateInfo *pCreateInfo,
490 unsigned *is_int8, unsigned *is_int10)
491 {
492 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
493 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
494 *is_int8 = 0;
495 *is_int10 = 0;
496
497 for (unsigned i = 0; i < subpass->color_count; ++i) {
498 struct radv_render_pass_attachment *attachment;
499
500 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
501 continue;
502
503 attachment = pass->attachments + subpass->color_attachments[i].attachment;
504
505 if (format_is_int8(attachment->format))
506 *is_int8 |= 1 << i;
507 if (format_is_int10(attachment->format))
508 *is_int10 |= 1 << i;
509 }
510 }
511
512 static void
513 radv_pipeline_init_blend_state(struct radv_pipeline *pipeline,
514 const VkGraphicsPipelineCreateInfo *pCreateInfo,
515 const struct radv_graphics_pipeline_create_info *extra)
516 {
517 const VkPipelineColorBlendStateCreateInfo *vkblend = pCreateInfo->pColorBlendState;
518 const VkPipelineMultisampleStateCreateInfo *vkms = pCreateInfo->pMultisampleState;
519 struct radv_blend_state *blend = &pipeline->graphics.blend;
520 unsigned mode = V_028808_CB_NORMAL;
521 uint32_t blend_enable = 0, blend_need_alpha = 0;
522 bool blend_mrt0_is_dual_src = false;
523 int i;
524 bool single_cb_enable = false;
525
526 if (!vkblend)
527 return;
528
529 if (extra && extra->custom_blend_mode) {
530 single_cb_enable = true;
531 mode = extra->custom_blend_mode;
532 }
533 blend->cb_color_control = 0;
534 if (vkblend->logicOpEnable)
535 blend->cb_color_control |= S_028808_ROP3(vkblend->logicOp | (vkblend->logicOp << 4));
536 else
537 blend->cb_color_control |= S_028808_ROP3(0xcc);
538
539 blend->db_alpha_to_mask = S_028B70_ALPHA_TO_MASK_OFFSET0(2) |
540 S_028B70_ALPHA_TO_MASK_OFFSET1(2) |
541 S_028B70_ALPHA_TO_MASK_OFFSET2(2) |
542 S_028B70_ALPHA_TO_MASK_OFFSET3(2);
543
544 if (vkms && vkms->alphaToCoverageEnable) {
545 blend->db_alpha_to_mask |= S_028B70_ALPHA_TO_MASK_ENABLE(1);
546 }
547
548 blend->cb_target_mask = 0;
549 for (i = 0; i < vkblend->attachmentCount; i++) {
550 const VkPipelineColorBlendAttachmentState *att = &vkblend->pAttachments[i];
551 unsigned blend_cntl = 0;
552 unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;
553 VkBlendOp eqRGB = att->colorBlendOp;
554 VkBlendFactor srcRGB = att->srcColorBlendFactor;
555 VkBlendFactor dstRGB = att->dstColorBlendFactor;
556 VkBlendOp eqA = att->alphaBlendOp;
557 VkBlendFactor srcA = att->srcAlphaBlendFactor;
558 VkBlendFactor dstA = att->dstAlphaBlendFactor;
559
560 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);
561
562 if (!att->colorWriteMask)
563 continue;
564
565 blend->cb_target_mask |= (unsigned)att->colorWriteMask << (4 * i);
566 if (!att->blendEnable) {
567 blend->cb_blend_control[i] = blend_cntl;
568 continue;
569 }
570
571 if (is_dual_src(srcRGB) || is_dual_src(dstRGB) || is_dual_src(srcA) || is_dual_src(dstA))
572 if (i == 0)
573 blend_mrt0_is_dual_src = true;
574
575 if (eqRGB == VK_BLEND_OP_MIN || eqRGB == VK_BLEND_OP_MAX) {
576 srcRGB = VK_BLEND_FACTOR_ONE;
577 dstRGB = VK_BLEND_FACTOR_ONE;
578 }
579 if (eqA == VK_BLEND_OP_MIN || eqA == VK_BLEND_OP_MAX) {
580 srcA = VK_BLEND_FACTOR_ONE;
581 dstA = VK_BLEND_FACTOR_ONE;
582 }
583
584 /* Blending optimizations for RB+.
585 * These transformations don't change the behavior.
586 *
587 * First, get rid of DST in the blend factors:
588 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
589 */
590 si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB,
591 VK_BLEND_FACTOR_DST_COLOR,
592 VK_BLEND_FACTOR_SRC_COLOR);
593
594 si_blend_remove_dst(&eqA, &srcA, &dstA,
595 VK_BLEND_FACTOR_DST_COLOR,
596 VK_BLEND_FACTOR_SRC_COLOR);
597
598 si_blend_remove_dst(&eqA, &srcA, &dstA,
599 VK_BLEND_FACTOR_DST_ALPHA,
600 VK_BLEND_FACTOR_SRC_ALPHA);
601
602 /* Look up the ideal settings from tables. */
603 srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);
604 dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);
605 srcA_opt = si_translate_blend_opt_factor(srcA, true);
606 dstA_opt = si_translate_blend_opt_factor(dstA, true);
607
608 /* Handle interdependencies. */
609 if (si_blend_factor_uses_dst(srcRGB))
610 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
611 if (si_blend_factor_uses_dst(srcA))
612 dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
613
614 if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE &&
615 (dstRGB == VK_BLEND_FACTOR_ZERO ||
616 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
617 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE))
618 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
619
620 /* Set the final value. */
621 blend->sx_mrt_blend_opt[i] =
622 S_028760_COLOR_SRC_OPT(srcRGB_opt) |
623 S_028760_COLOR_DST_OPT(dstRGB_opt) |
624 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |
625 S_028760_ALPHA_SRC_OPT(srcA_opt) |
626 S_028760_ALPHA_DST_OPT(dstA_opt) |
627 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));
628 blend_cntl |= S_028780_ENABLE(1);
629
630 blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
631 blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
632 blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
633 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
634 blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
635 blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
636 blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
637 blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
638 }
639 blend->cb_blend_control[i] = blend_cntl;
640
641 blend_enable |= 1 << i;
642
643 if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
644 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
645 srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
646 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
647 srcRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA ||
648 dstRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA)
649 blend_need_alpha |= 1 << i;
650 }
651 for (i = vkblend->attachmentCount; i < 8; i++) {
652 blend->cb_blend_control[i] = 0;
653 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);
654 }
655
656 /* disable RB+ for now */
657 if (pipeline->device->physical_device->has_rbplus)
658 blend->cb_color_control |= S_028808_DISABLE_DUAL_QUAD(1);
659
660 if (blend->cb_target_mask)
661 blend->cb_color_control |= S_028808_MODE(mode);
662 else
663 blend->cb_color_control |= S_028808_MODE(V_028808_CB_DISABLE);
664
665 radv_pipeline_compute_spi_color_formats(pipeline, pCreateInfo,
666 blend_enable, blend_need_alpha, single_cb_enable, blend_mrt0_is_dual_src);
667 }
668
669 static uint32_t si_translate_stencil_op(enum VkStencilOp op)
670 {
671 switch (op) {
672 case VK_STENCIL_OP_KEEP:
673 return V_02842C_STENCIL_KEEP;
674 case VK_STENCIL_OP_ZERO:
675 return V_02842C_STENCIL_ZERO;
676 case VK_STENCIL_OP_REPLACE:
677 return V_02842C_STENCIL_REPLACE_TEST;
678 case VK_STENCIL_OP_INCREMENT_AND_CLAMP:
679 return V_02842C_STENCIL_ADD_CLAMP;
680 case VK_STENCIL_OP_DECREMENT_AND_CLAMP:
681 return V_02842C_STENCIL_SUB_CLAMP;
682 case VK_STENCIL_OP_INVERT:
683 return V_02842C_STENCIL_INVERT;
684 case VK_STENCIL_OP_INCREMENT_AND_WRAP:
685 return V_02842C_STENCIL_ADD_WRAP;
686 case VK_STENCIL_OP_DECREMENT_AND_WRAP:
687 return V_02842C_STENCIL_SUB_WRAP;
688 default:
689 return 0;
690 }
691 }
692 static void
693 radv_pipeline_init_depth_stencil_state(struct radv_pipeline *pipeline,
694 const VkGraphicsPipelineCreateInfo *pCreateInfo,
695 const struct radv_graphics_pipeline_create_info *extra)
696 {
697 const VkPipelineDepthStencilStateCreateInfo *vkds = pCreateInfo->pDepthStencilState;
698 struct radv_depth_stencil_state *ds = &pipeline->graphics.ds;
699
700 if (!vkds)
701 return;
702
703 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
704 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
705 if (subpass->depth_stencil_attachment.attachment == VK_ATTACHMENT_UNUSED)
706 return;
707
708 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->depth_stencil_attachment.attachment;
709 bool has_depth_attachment = vk_format_is_depth(attachment->format);
710 bool has_stencil_attachment = vk_format_is_stencil(attachment->format);
711
712 if (has_depth_attachment) {
713 ds->db_depth_control = S_028800_Z_ENABLE(vkds->depthTestEnable ? 1 : 0) |
714 S_028800_Z_WRITE_ENABLE(vkds->depthWriteEnable ? 1 : 0) |
715 S_028800_ZFUNC(vkds->depthCompareOp) |
716 S_028800_DEPTH_BOUNDS_ENABLE(vkds->depthBoundsTestEnable ? 1 : 0);
717 }
718
719 if (has_stencil_attachment && vkds->stencilTestEnable) {
720 ds->db_depth_control |= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);
721 ds->db_depth_control |= S_028800_STENCILFUNC(vkds->front.compareOp);
722 ds->db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(vkds->front.failOp));
723 ds->db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(vkds->front.passOp));
724 ds->db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(vkds->front.depthFailOp));
725
726 ds->db_depth_control |= S_028800_STENCILFUNC_BF(vkds->back.compareOp);
727 ds->db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(vkds->back.failOp));
728 ds->db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(vkds->back.passOp));
729 ds->db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(vkds->back.depthFailOp));
730 }
731
732 if (extra) {
733
734 ds->db_render_control |= S_028000_DEPTH_CLEAR_ENABLE(extra->db_depth_clear);
735 ds->db_render_control |= S_028000_STENCIL_CLEAR_ENABLE(extra->db_stencil_clear);
736
737 ds->db_render_control |= S_028000_RESUMMARIZE_ENABLE(extra->db_resummarize);
738 ds->db_render_control |= S_028000_DEPTH_COMPRESS_DISABLE(extra->db_flush_depth_inplace);
739 ds->db_render_control |= S_028000_STENCIL_COMPRESS_DISABLE(extra->db_flush_stencil_inplace);
740 ds->db_render_override2 |= S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra->db_depth_disable_expclear);
741 ds->db_render_override2 |= S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra->db_stencil_disable_expclear);
742 }
743 }
744
745 static uint32_t si_translate_fill(VkPolygonMode func)
746 {
747 switch(func) {
748 case VK_POLYGON_MODE_FILL:
749 return V_028814_X_DRAW_TRIANGLES;
750 case VK_POLYGON_MODE_LINE:
751 return V_028814_X_DRAW_LINES;
752 case VK_POLYGON_MODE_POINT:
753 return V_028814_X_DRAW_POINTS;
754 default:
755 assert(0);
756 return V_028814_X_DRAW_POINTS;
757 }
758 }
759 static void
760 radv_pipeline_init_raster_state(struct radv_pipeline *pipeline,
761 const VkGraphicsPipelineCreateInfo *pCreateInfo)
762 {
763 const VkPipelineRasterizationStateCreateInfo *vkraster = pCreateInfo->pRasterizationState;
764 struct radv_raster_state *raster = &pipeline->graphics.raster;
765
766 raster->spi_interp_control =
767 S_0286D4_FLAT_SHADE_ENA(1) |
768 S_0286D4_PNT_SPRITE_ENA(1) |
769 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
770 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
771 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
772 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
773 S_0286D4_PNT_SPRITE_TOP_1(0); // vulkan is top to bottom - 1.0 at bottom
774
775
776 raster->pa_cl_clip_cntl = S_028810_PS_UCP_MODE(3) |
777 S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.
778 S_028810_ZCLIP_NEAR_DISABLE(vkraster->depthClampEnable ? 1 : 0) |
779 S_028810_ZCLIP_FAR_DISABLE(vkraster->depthClampEnable ? 1 : 0) |
780 S_028810_DX_RASTERIZATION_KILL(vkraster->rasterizerDiscardEnable ? 1 : 0) |
781 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
782
783 raster->pa_su_vtx_cntl =
784 S_028BE4_PIX_CENTER(1) | // TODO verify
785 S_028BE4_ROUND_MODE(V_028BE4_X_ROUND_TO_EVEN) |
786 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH);
787
788 raster->pa_su_sc_mode_cntl =
789 S_028814_FACE(vkraster->frontFace) |
790 S_028814_CULL_FRONT(!!(vkraster->cullMode & VK_CULL_MODE_FRONT_BIT)) |
791 S_028814_CULL_BACK(!!(vkraster->cullMode & VK_CULL_MODE_BACK_BIT)) |
792 S_028814_POLY_MODE(vkraster->polygonMode != VK_POLYGON_MODE_FILL) |
793 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(vkraster->polygonMode)) |
794 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(vkraster->polygonMode)) |
795 S_028814_POLY_OFFSET_FRONT_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
796 S_028814_POLY_OFFSET_BACK_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
797 S_028814_POLY_OFFSET_PARA_ENABLE(vkraster->depthBiasEnable ? 1 : 0);
798
799 }
800
801 static void
802 radv_pipeline_init_multisample_state(struct radv_pipeline *pipeline,
803 const VkGraphicsPipelineCreateInfo *pCreateInfo)
804 {
805 const VkPipelineMultisampleStateCreateInfo *vkms = pCreateInfo->pMultisampleState;
806 struct radv_multisample_state *ms = &pipeline->graphics.ms;
807 unsigned num_tile_pipes = pipeline->device->physical_device->rad_info.num_tile_pipes;
808 int ps_iter_samples = 1;
809 uint32_t mask = 0xffff;
810
811 if (vkms)
812 ms->num_samples = vkms->rasterizationSamples;
813 else
814 ms->num_samples = 1;
815
816 if (vkms && vkms->sampleShadingEnable) {
817 ps_iter_samples = ceil(vkms->minSampleShading * ms->num_samples);
818 } else if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.info.ps.force_persample) {
819 ps_iter_samples = ms->num_samples;
820 }
821
822 ms->pa_sc_line_cntl = S_028BDC_DX10_DIAMOND_TEST_ENA(1);
823 ms->pa_sc_aa_config = 0;
824 ms->db_eqaa = S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
825 S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
826 ms->pa_sc_mode_cntl_1 =
827 S_028A4C_WALK_FENCE_ENABLE(1) | //TODO linear dst fixes
828 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |
829 /* always 1: */
830 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
831 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
832 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
833 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
834 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
835 S_028A4C_FORCE_EOV_REZ_ENABLE(1);
836 ms->pa_sc_mode_cntl_0 = S_028A48_ALTERNATE_RBS_PER_TILE(pipeline->device->physical_device->rad_info.chip_class >= GFX9);
837
838 if (ms->num_samples > 1) {
839 unsigned log_samples = util_logbase2(ms->num_samples);
840 unsigned log_ps_iter_samples = util_logbase2(util_next_power_of_two(ps_iter_samples));
841 ms->pa_sc_mode_cntl_0 |= S_028A48_MSAA_ENABLE(1);
842 ms->pa_sc_line_cntl |= S_028BDC_EXPAND_LINE_WIDTH(1); /* CM_R_028BDC_PA_SC_LINE_CNTL */
843 ms->db_eqaa |= S_028804_MAX_ANCHOR_SAMPLES(log_samples) |
844 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |
845 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |
846 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples);
847 ms->pa_sc_aa_config |= S_028BE0_MSAA_NUM_SAMPLES(log_samples) |
848 S_028BE0_MAX_SAMPLE_DIST(radv_cayman_get_maxdist(log_samples)) |
849 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples); /* CM_R_028BE0_PA_SC_AA_CONFIG */
850 ms->pa_sc_mode_cntl_1 |= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1);
851 }
852
853 const struct VkPipelineRasterizationStateRasterizationOrderAMD *raster_order =
854 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext, PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD);
855 if (raster_order && raster_order->rasterizationOrder == VK_RASTERIZATION_ORDER_RELAXED_AMD) {
856 ms->pa_sc_mode_cntl_1 |= S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(1) |
857 S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7);
858 }
859
860 if (vkms && vkms->pSampleMask) {
861 mask = vkms->pSampleMask[0] & 0xffff;
862 }
863
864 ms->pa_sc_aa_mask[0] = mask | (mask << 16);
865 ms->pa_sc_aa_mask[1] = mask | (mask << 16);
866 }
867
868 static bool
869 radv_prim_can_use_guardband(enum VkPrimitiveTopology topology)
870 {
871 switch (topology) {
872 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
873 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
874 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
875 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
876 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
877 return false;
878 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
879 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
880 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
881 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
882 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
883 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
884 return true;
885 default:
886 unreachable("unhandled primitive type");
887 }
888 }
889
890 static uint32_t
891 si_translate_prim(enum VkPrimitiveTopology topology)
892 {
893 switch (topology) {
894 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
895 return V_008958_DI_PT_POINTLIST;
896 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
897 return V_008958_DI_PT_LINELIST;
898 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
899 return V_008958_DI_PT_LINESTRIP;
900 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
901 return V_008958_DI_PT_TRILIST;
902 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
903 return V_008958_DI_PT_TRISTRIP;
904 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
905 return V_008958_DI_PT_TRIFAN;
906 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
907 return V_008958_DI_PT_LINELIST_ADJ;
908 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
909 return V_008958_DI_PT_LINESTRIP_ADJ;
910 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
911 return V_008958_DI_PT_TRILIST_ADJ;
912 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
913 return V_008958_DI_PT_TRISTRIP_ADJ;
914 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
915 return V_008958_DI_PT_PATCH;
916 default:
917 assert(0);
918 return 0;
919 }
920 }
921
922 static uint32_t
923 si_conv_gl_prim_to_gs_out(unsigned gl_prim)
924 {
925 switch (gl_prim) {
926 case 0: /* GL_POINTS */
927 return V_028A6C_OUTPRIM_TYPE_POINTLIST;
928 case 1: /* GL_LINES */
929 case 3: /* GL_LINE_STRIP */
930 case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */
931 case 0x8E7A: /* GL_ISOLINES */
932 return V_028A6C_OUTPRIM_TYPE_LINESTRIP;
933
934 case 4: /* GL_TRIANGLES */
935 case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */
936 case 5: /* GL_TRIANGLE_STRIP */
937 case 7: /* GL_QUADS */
938 return V_028A6C_OUTPRIM_TYPE_TRISTRIP;
939 default:
940 assert(0);
941 return 0;
942 }
943 }
944
945 static uint32_t
946 si_conv_prim_to_gs_out(enum VkPrimitiveTopology topology)
947 {
948 switch (topology) {
949 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
950 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
951 return V_028A6C_OUTPRIM_TYPE_POINTLIST;
952 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
953 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
954 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
955 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
956 return V_028A6C_OUTPRIM_TYPE_LINESTRIP;
957 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
958 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
959 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
960 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
961 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
962 return V_028A6C_OUTPRIM_TYPE_TRISTRIP;
963 default:
964 assert(0);
965 return 0;
966 }
967 }
968
969 static unsigned si_map_swizzle(unsigned swizzle)
970 {
971 switch (swizzle) {
972 case VK_SWIZZLE_Y:
973 return V_008F0C_SQ_SEL_Y;
974 case VK_SWIZZLE_Z:
975 return V_008F0C_SQ_SEL_Z;
976 case VK_SWIZZLE_W:
977 return V_008F0C_SQ_SEL_W;
978 case VK_SWIZZLE_0:
979 return V_008F0C_SQ_SEL_0;
980 case VK_SWIZZLE_1:
981 return V_008F0C_SQ_SEL_1;
982 default: /* VK_SWIZZLE_X */
983 return V_008F0C_SQ_SEL_X;
984 }
985 }
986
987
988 static unsigned radv_dynamic_state_mask(VkDynamicState state)
989 {
990 switch(state) {
991 case VK_DYNAMIC_STATE_VIEWPORT:
992 return RADV_DYNAMIC_VIEWPORT;
993 case VK_DYNAMIC_STATE_SCISSOR:
994 return RADV_DYNAMIC_SCISSOR;
995 case VK_DYNAMIC_STATE_LINE_WIDTH:
996 return RADV_DYNAMIC_LINE_WIDTH;
997 case VK_DYNAMIC_STATE_DEPTH_BIAS:
998 return RADV_DYNAMIC_DEPTH_BIAS;
999 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
1000 return RADV_DYNAMIC_BLEND_CONSTANTS;
1001 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
1002 return RADV_DYNAMIC_DEPTH_BOUNDS;
1003 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
1004 return RADV_DYNAMIC_STENCIL_COMPARE_MASK;
1005 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
1006 return RADV_DYNAMIC_STENCIL_WRITE_MASK;
1007 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
1008 return RADV_DYNAMIC_STENCIL_REFERENCE;
1009 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT:
1010 return RADV_DYNAMIC_DISCARD_RECTANGLE;
1011 default:
1012 unreachable("Unhandled dynamic state");
1013 }
1014 }
1015
1016 static void
1017 radv_pipeline_init_dynamic_state(struct radv_pipeline *pipeline,
1018 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1019 {
1020 uint32_t states = RADV_DYNAMIC_ALL;
1021 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1022 struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
1023
1024 pipeline->dynamic_state = default_dynamic_state;
1025
1026 if (pCreateInfo->pDynamicState) {
1027 /* Remove all of the states that are marked as dynamic */
1028 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
1029 for (uint32_t s = 0; s < count; s++)
1030 states &= ~radv_dynamic_state_mask(pCreateInfo->pDynamicState->pDynamicStates[s]);
1031 }
1032
1033 struct radv_dynamic_state *dynamic = &pipeline->dynamic_state;
1034
1035 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1036 *
1037 * pViewportState is [...] NULL if the pipeline
1038 * has rasterization disabled.
1039 */
1040 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1041 assert(pCreateInfo->pViewportState);
1042
1043 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
1044 if (states & RADV_DYNAMIC_VIEWPORT) {
1045 typed_memcpy(dynamic->viewport.viewports,
1046 pCreateInfo->pViewportState->pViewports,
1047 pCreateInfo->pViewportState->viewportCount);
1048 }
1049
1050 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
1051 if (states & RADV_DYNAMIC_SCISSOR) {
1052 typed_memcpy(dynamic->scissor.scissors,
1053 pCreateInfo->pViewportState->pScissors,
1054 pCreateInfo->pViewportState->scissorCount);
1055 }
1056 }
1057
1058 if (states & RADV_DYNAMIC_LINE_WIDTH) {
1059 assert(pCreateInfo->pRasterizationState);
1060 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
1061 }
1062
1063 if (states & RADV_DYNAMIC_DEPTH_BIAS) {
1064 assert(pCreateInfo->pRasterizationState);
1065 dynamic->depth_bias.bias =
1066 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
1067 dynamic->depth_bias.clamp =
1068 pCreateInfo->pRasterizationState->depthBiasClamp;
1069 dynamic->depth_bias.slope =
1070 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
1071 }
1072
1073 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1074 *
1075 * pColorBlendState is [...] NULL if the pipeline has rasterization
1076 * disabled or if the subpass of the render pass the pipeline is
1077 * created against does not use any color attachments.
1078 */
1079 bool uses_color_att = false;
1080 for (unsigned i = 0; i < subpass->color_count; ++i) {
1081 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED) {
1082 uses_color_att = true;
1083 break;
1084 }
1085 }
1086
1087 if (uses_color_att && states & RADV_DYNAMIC_BLEND_CONSTANTS) {
1088 assert(pCreateInfo->pColorBlendState);
1089 typed_memcpy(dynamic->blend_constants,
1090 pCreateInfo->pColorBlendState->blendConstants, 4);
1091 }
1092
1093 /* If there is no depthstencil attachment, then don't read
1094 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1095 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1096 * no need to override the depthstencil defaults in
1097 * radv_pipeline::dynamic_state when there is no depthstencil attachment.
1098 *
1099 * Section 9.2 of the Vulkan 1.0.15 spec says:
1100 *
1101 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1102 * disabled or if the subpass of the render pass the pipeline is created
1103 * against does not use a depth/stencil attachment.
1104 */
1105 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
1106 subpass->depth_stencil_attachment.attachment != VK_ATTACHMENT_UNUSED) {
1107 assert(pCreateInfo->pDepthStencilState);
1108
1109 if (states & RADV_DYNAMIC_DEPTH_BOUNDS) {
1110 dynamic->depth_bounds.min =
1111 pCreateInfo->pDepthStencilState->minDepthBounds;
1112 dynamic->depth_bounds.max =
1113 pCreateInfo->pDepthStencilState->maxDepthBounds;
1114 }
1115
1116 if (states & RADV_DYNAMIC_STENCIL_COMPARE_MASK) {
1117 dynamic->stencil_compare_mask.front =
1118 pCreateInfo->pDepthStencilState->front.compareMask;
1119 dynamic->stencil_compare_mask.back =
1120 pCreateInfo->pDepthStencilState->back.compareMask;
1121 }
1122
1123 if (states & RADV_DYNAMIC_STENCIL_WRITE_MASK) {
1124 dynamic->stencil_write_mask.front =
1125 pCreateInfo->pDepthStencilState->front.writeMask;
1126 dynamic->stencil_write_mask.back =
1127 pCreateInfo->pDepthStencilState->back.writeMask;
1128 }
1129
1130 if (states & RADV_DYNAMIC_STENCIL_REFERENCE) {
1131 dynamic->stencil_reference.front =
1132 pCreateInfo->pDepthStencilState->front.reference;
1133 dynamic->stencil_reference.back =
1134 pCreateInfo->pDepthStencilState->back.reference;
1135 }
1136 }
1137
1138 const VkPipelineDiscardRectangleStateCreateInfoEXT *discard_rectangle_info =
1139 vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);
1140 if (discard_rectangle_info) {
1141 dynamic->discard_rectangle.count = discard_rectangle_info->discardRectangleCount;
1142 typed_memcpy(dynamic->discard_rectangle.rectangles,
1143 discard_rectangle_info->pDiscardRectangles,
1144 discard_rectangle_info->discardRectangleCount);
1145
1146 unsigned mask = 0;
1147
1148 for (unsigned i = 0; i < (1u << MAX_DISCARD_RECTANGLES); ++i) {
1149 /* Interpret i as a bitmask, and then set the bit in the mask if
1150 * that combination of rectangles in which the pixel is contained
1151 * should pass the cliprect test. */
1152 unsigned relevant_subset = i & ((1u << discard_rectangle_info->discardRectangleCount) - 1);
1153
1154 if (discard_rectangle_info->discardRectangleMode == VK_DISCARD_RECTANGLE_MODE_INCLUSIVE_EXT &&
1155 !relevant_subset)
1156 continue;
1157
1158 if (discard_rectangle_info->discardRectangleMode == VK_DISCARD_RECTANGLE_MODE_EXCLUSIVE_EXT &&
1159 relevant_subset)
1160 continue;
1161
1162 mask |= 1u << i;
1163 }
1164 pipeline->graphics.pa_sc_cliprect_rule = mask;
1165 } else {
1166 states &= ~RADV_DYNAMIC_DISCARD_RECTANGLE;
1167
1168 /* Allow from all rectangle combinations */
1169 pipeline->graphics.pa_sc_cliprect_rule = 0xffff;
1170 }
1171 pipeline->dynamic_state.mask = states;
1172 }
1173
1174 static void calculate_gfx9_gs_info(const VkGraphicsPipelineCreateInfo *pCreateInfo,
1175 struct radv_pipeline *pipeline)
1176 {
1177 struct ac_shader_variant_info *gs_info = &pipeline->shaders[MESA_SHADER_GEOMETRY]->info;
1178 struct ac_es_output_info *es_info = radv_pipeline_has_tess(pipeline) ?
1179 &gs_info->tes.es_info : &gs_info->vs.es_info;
1180 unsigned gs_num_invocations = MAX2(gs_info->gs.invocations, 1);
1181 bool uses_adjacency;
1182 switch(pCreateInfo->pInputAssemblyState->topology) {
1183 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1184 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1185 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1186 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1187 uses_adjacency = true;
1188 break;
1189 default:
1190 uses_adjacency = false;
1191 break;
1192 }
1193
1194 /* All these are in dwords: */
1195 /* We can't allow using the whole LDS, because GS waves compete with
1196 * other shader stages for LDS space. */
1197 const unsigned max_lds_size = 8 * 1024;
1198 const unsigned esgs_itemsize = es_info->esgs_itemsize / 4;
1199 unsigned esgs_lds_size;
1200
1201 /* All these are per subgroup: */
1202 const unsigned max_out_prims = 32 * 1024;
1203 const unsigned max_es_verts = 255;
1204 const unsigned ideal_gs_prims = 64;
1205 unsigned max_gs_prims, gs_prims;
1206 unsigned min_es_verts, es_verts, worst_case_es_verts;
1207
1208 if (uses_adjacency || gs_num_invocations > 1)
1209 max_gs_prims = 127 / gs_num_invocations;
1210 else
1211 max_gs_prims = 255;
1212
1213 /* MAX_PRIMS_PER_SUBGROUP = gs_prims * max_vert_out * gs_invocations.
1214 * Make sure we don't go over the maximum value.
1215 */
1216 if (gs_info->gs.vertices_out > 0) {
1217 max_gs_prims = MIN2(max_gs_prims,
1218 max_out_prims /
1219 (gs_info->gs.vertices_out * gs_num_invocations));
1220 }
1221 assert(max_gs_prims > 0);
1222
1223 /* If the primitive has adjacency, halve the number of vertices
1224 * that will be reused in multiple primitives.
1225 */
1226 min_es_verts = gs_info->gs.vertices_in / (uses_adjacency ? 2 : 1);
1227
1228 gs_prims = MIN2(ideal_gs_prims, max_gs_prims);
1229 worst_case_es_verts = MIN2(min_es_verts * gs_prims, max_es_verts);
1230
1231 /* Compute ESGS LDS size based on the worst case number of ES vertices
1232 * needed to create the target number of GS prims per subgroup.
1233 */
1234 esgs_lds_size = esgs_itemsize * worst_case_es_verts;
1235
1236 /* If total LDS usage is too big, refactor partitions based on ratio
1237 * of ESGS item sizes.
1238 */
1239 if (esgs_lds_size > max_lds_size) {
1240 /* Our target GS Prims Per Subgroup was too large. Calculate
1241 * the maximum number of GS Prims Per Subgroup that will fit
1242 * into LDS, capped by the maximum that the hardware can support.
1243 */
1244 gs_prims = MIN2((max_lds_size / (esgs_itemsize * min_es_verts)),
1245 max_gs_prims);
1246 assert(gs_prims > 0);
1247 worst_case_es_verts = MIN2(min_es_verts * gs_prims,
1248 max_es_verts);
1249
1250 esgs_lds_size = esgs_itemsize * worst_case_es_verts;
1251 assert(esgs_lds_size <= max_lds_size);
1252 }
1253
1254 /* Now calculate remaining ESGS information. */
1255 if (esgs_lds_size)
1256 es_verts = MIN2(esgs_lds_size / esgs_itemsize, max_es_verts);
1257 else
1258 es_verts = max_es_verts;
1259
1260 /* Vertices for adjacency primitives are not always reused, so restore
1261 * it for ES_VERTS_PER_SUBGRP.
1262 */
1263 min_es_verts = gs_info->gs.vertices_in;
1264
1265 /* For normal primitives, the VGT only checks if they are past the ES
1266 * verts per subgroup after allocating a full GS primitive and if they
1267 * are, kick off a new subgroup. But if those additional ES verts are
1268 * unique (e.g. not reused) we need to make sure there is enough LDS
1269 * space to account for those ES verts beyond ES_VERTS_PER_SUBGRP.
1270 */
1271 es_verts -= min_es_verts - 1;
1272
1273 uint32_t es_verts_per_subgroup = es_verts;
1274 uint32_t gs_prims_per_subgroup = gs_prims;
1275 uint32_t gs_inst_prims_in_subgroup = gs_prims * gs_num_invocations;
1276 uint32_t max_prims_per_subgroup = gs_inst_prims_in_subgroup * gs_info->gs.vertices_out;
1277 pipeline->graphics.gs.lds_size = align(esgs_lds_size, 128) / 128;
1278 pipeline->graphics.gs.vgt_gs_onchip_cntl =
1279 S_028A44_ES_VERTS_PER_SUBGRP(es_verts_per_subgroup) |
1280 S_028A44_GS_PRIMS_PER_SUBGRP(gs_prims_per_subgroup) |
1281 S_028A44_GS_INST_PRIMS_IN_SUBGRP(gs_inst_prims_in_subgroup);
1282 pipeline->graphics.gs.vgt_gs_max_prims_per_subgroup =
1283 S_028A94_MAX_PRIMS_PER_SUBGROUP(max_prims_per_subgroup);
1284 pipeline->graphics.gs.vgt_esgs_ring_itemsize = esgs_itemsize;
1285 assert(max_prims_per_subgroup <= max_out_prims);
1286 }
1287
1288 static void
1289 calculate_gs_ring_sizes(struct radv_pipeline *pipeline)
1290 {
1291 struct radv_device *device = pipeline->device;
1292 unsigned num_se = device->physical_device->rad_info.max_se;
1293 unsigned wave_size = 64;
1294 unsigned max_gs_waves = 32 * num_se; /* max 32 per SE on GCN */
1295 unsigned gs_vertex_reuse = 16 * num_se; /* GS_VERTEX_REUSE register (per SE) */
1296 unsigned alignment = 256 * num_se;
1297 /* The maximum size is 63.999 MB per SE. */
1298 unsigned max_size = ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se;
1299 struct ac_shader_variant_info *gs_info = &pipeline->shaders[MESA_SHADER_GEOMETRY]->info;
1300 struct ac_es_output_info *es_info;
1301 if (pipeline->device->physical_device->rad_info.chip_class >= GFX9)
1302 es_info = radv_pipeline_has_tess(pipeline) ? &gs_info->tes.es_info : &gs_info->vs.es_info;
1303 else
1304 es_info = radv_pipeline_has_tess(pipeline) ?
1305 &pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.es_info :
1306 &pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.es_info;
1307
1308 /* Calculate the minimum size. */
1309 unsigned min_esgs_ring_size = align(es_info->esgs_itemsize * gs_vertex_reuse *
1310 wave_size, alignment);
1311 /* These are recommended sizes, not minimum sizes. */
1312 unsigned esgs_ring_size = max_gs_waves * 2 * wave_size *
1313 es_info->esgs_itemsize * gs_info->gs.vertices_in;
1314 unsigned gsvs_ring_size = max_gs_waves * 2 * wave_size *
1315 gs_info->gs.max_gsvs_emit_size * 1; // no streams in VK (gs->max_gs_stream + 1);
1316
1317 min_esgs_ring_size = align(min_esgs_ring_size, alignment);
1318 esgs_ring_size = align(esgs_ring_size, alignment);
1319 gsvs_ring_size = align(gsvs_ring_size, alignment);
1320
1321 if (pipeline->device->physical_device->rad_info.chip_class <= VI)
1322 pipeline->graphics.esgs_ring_size = CLAMP(esgs_ring_size, min_esgs_ring_size, max_size);
1323
1324 pipeline->graphics.gs.vgt_esgs_ring_itemsize = es_info->esgs_itemsize / 4;
1325 pipeline->graphics.gsvs_ring_size = MIN2(gsvs_ring_size, max_size);
1326 }
1327
1328 static void si_multiwave_lds_size_workaround(struct radv_device *device,
1329 unsigned *lds_size)
1330 {
1331 /* SPI barrier management bug:
1332 * Make sure we have at least 4k of LDS in use to avoid the bug.
1333 * It applies to workgroup sizes of more than one wavefront.
1334 */
1335 if (device->physical_device->rad_info.family == CHIP_BONAIRE ||
1336 device->physical_device->rad_info.family == CHIP_KABINI ||
1337 device->physical_device->rad_info.family == CHIP_MULLINS)
1338 *lds_size = MAX2(*lds_size, 8);
1339 }
1340
1341 struct radv_shader_variant *
1342 radv_get_vertex_shader(struct radv_pipeline *pipeline)
1343 {
1344 if (pipeline->shaders[MESA_SHADER_VERTEX])
1345 return pipeline->shaders[MESA_SHADER_VERTEX];
1346 if (pipeline->shaders[MESA_SHADER_TESS_CTRL])
1347 return pipeline->shaders[MESA_SHADER_TESS_CTRL];
1348 return pipeline->shaders[MESA_SHADER_GEOMETRY];
1349 }
1350
1351 static struct radv_shader_variant *
1352 radv_get_tess_eval_shader(struct radv_pipeline *pipeline)
1353 {
1354 if (pipeline->shaders[MESA_SHADER_TESS_EVAL])
1355 return pipeline->shaders[MESA_SHADER_TESS_EVAL];
1356 return pipeline->shaders[MESA_SHADER_GEOMETRY];
1357 }
1358
1359 static void
1360 calculate_tess_state(struct radv_pipeline *pipeline,
1361 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1362 {
1363 unsigned num_tcs_input_cp = pCreateInfo->pTessellationState->patchControlPoints;
1364 unsigned num_tcs_output_cp, num_tcs_inputs, num_tcs_outputs;
1365 unsigned num_tcs_patch_outputs;
1366 unsigned input_vertex_size, output_vertex_size, pervertex_output_patch_size;
1367 unsigned input_patch_size, output_patch_size, output_patch0_offset;
1368 unsigned lds_size, hardware_lds_size;
1369 unsigned perpatch_output_offset;
1370 unsigned num_patches;
1371 struct radv_tessellation_state *tess = &pipeline->graphics.tess;
1372
1373 /* This calculates how shader inputs and outputs among VS, TCS, and TES
1374 * are laid out in LDS. */
1375 num_tcs_inputs = util_last_bit64(radv_get_vertex_shader(pipeline)->info.vs.outputs_written);
1376
1377 num_tcs_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.outputs_written); //tcs->outputs_written
1378 num_tcs_output_cp = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.tcs_vertices_out; //TCS VERTICES OUT
1379 num_tcs_patch_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.patch_outputs_written);
1380
1381 /* Ensure that we only need one wave per SIMD so we don't need to check
1382 * resource usage. Also ensures that the number of tcs in and out
1383 * vertices per threadgroup are at most 256.
1384 */
1385 input_vertex_size = num_tcs_inputs * 16;
1386 output_vertex_size = num_tcs_outputs * 16;
1387
1388 input_patch_size = num_tcs_input_cp * input_vertex_size;
1389
1390 pervertex_output_patch_size = num_tcs_output_cp * output_vertex_size;
1391 output_patch_size = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
1392 /* Ensure that we only need one wave per SIMD so we don't need to check
1393 * resource usage. Also ensures that the number of tcs in and out
1394 * vertices per threadgroup are at most 256.
1395 */
1396 num_patches = 64 / MAX2(num_tcs_input_cp, num_tcs_output_cp) * 4;
1397
1398 /* Make sure that the data fits in LDS. This assumes the shaders only
1399 * use LDS for the inputs and outputs.
1400 */
1401 hardware_lds_size = pipeline->device->physical_device->rad_info.chip_class >= CIK ? 65536 : 32768;
1402 num_patches = MIN2(num_patches, hardware_lds_size / (input_patch_size + output_patch_size));
1403
1404 /* Make sure the output data fits in the offchip buffer */
1405 num_patches = MIN2(num_patches,
1406 (pipeline->device->tess_offchip_block_dw_size * 4) /
1407 output_patch_size);
1408
1409 /* Not necessary for correctness, but improves performance. The
1410 * specific value is taken from the proprietary driver.
1411 */
1412 num_patches = MIN2(num_patches, 40);
1413
1414 /* SI bug workaround - limit LS-HS threadgroups to only one wave. */
1415 if (pipeline->device->physical_device->rad_info.chip_class == SI) {
1416 unsigned one_wave = 64 / MAX2(num_tcs_input_cp, num_tcs_output_cp);
1417 num_patches = MIN2(num_patches, one_wave);
1418 }
1419
1420 output_patch0_offset = input_patch_size * num_patches;
1421 perpatch_output_offset = output_patch0_offset + pervertex_output_patch_size;
1422
1423 lds_size = output_patch0_offset + output_patch_size * num_patches;
1424
1425 if (pipeline->device->physical_device->rad_info.chip_class >= CIK) {
1426 assert(lds_size <= 65536);
1427 lds_size = align(lds_size, 512) / 512;
1428 } else {
1429 assert(lds_size <= 32768);
1430 lds_size = align(lds_size, 256) / 256;
1431 }
1432 si_multiwave_lds_size_workaround(pipeline->device, &lds_size);
1433
1434 tess->lds_size = lds_size;
1435
1436 tess->tcs_in_layout = (input_patch_size / 4) |
1437 ((input_vertex_size / 4) << 13);
1438 tess->tcs_out_layout = (output_patch_size / 4) |
1439 ((output_vertex_size / 4) << 13);
1440 tess->tcs_out_offsets = (output_patch0_offset / 16) |
1441 ((perpatch_output_offset / 16) << 16);
1442 tess->offchip_layout = (pervertex_output_patch_size * num_patches << 16) |
1443 (num_tcs_output_cp << 9) | num_patches;
1444
1445 tess->ls_hs_config = S_028B58_NUM_PATCHES(num_patches) |
1446 S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp) |
1447 S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp);
1448 tess->num_patches = num_patches;
1449 tess->num_tcs_input_cp = num_tcs_input_cp;
1450
1451 struct radv_shader_variant *tes = radv_get_tess_eval_shader(pipeline);
1452 unsigned type = 0, partitioning = 0, topology = 0, distribution_mode = 0;
1453
1454 switch (tes->info.tes.primitive_mode) {
1455 case GL_TRIANGLES:
1456 type = V_028B6C_TESS_TRIANGLE;
1457 break;
1458 case GL_QUADS:
1459 type = V_028B6C_TESS_QUAD;
1460 break;
1461 case GL_ISOLINES:
1462 type = V_028B6C_TESS_ISOLINE;
1463 break;
1464 }
1465
1466 switch (tes->info.tes.spacing) {
1467 case TESS_SPACING_EQUAL:
1468 partitioning = V_028B6C_PART_INTEGER;
1469 break;
1470 case TESS_SPACING_FRACTIONAL_ODD:
1471 partitioning = V_028B6C_PART_FRAC_ODD;
1472 break;
1473 case TESS_SPACING_FRACTIONAL_EVEN:
1474 partitioning = V_028B6C_PART_FRAC_EVEN;
1475 break;
1476 default:
1477 break;
1478 }
1479
1480 bool ccw = tes->info.tes.ccw;
1481 const VkPipelineTessellationDomainOriginStateCreateInfoKHR *domain_origin_state =
1482 vk_find_struct_const(pCreateInfo->pTessellationState,
1483 PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO_KHR);
1484
1485 if (domain_origin_state && domain_origin_state->domainOrigin != VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT_KHR)
1486 ccw = !ccw;
1487
1488 if (tes->info.tes.point_mode)
1489 topology = V_028B6C_OUTPUT_POINT;
1490 else if (tes->info.tes.primitive_mode == GL_ISOLINES)
1491 topology = V_028B6C_OUTPUT_LINE;
1492 else if (ccw)
1493 topology = V_028B6C_OUTPUT_TRIANGLE_CCW;
1494 else
1495 topology = V_028B6C_OUTPUT_TRIANGLE_CW;
1496
1497 if (pipeline->device->has_distributed_tess) {
1498 if (pipeline->device->physical_device->rad_info.family == CHIP_FIJI ||
1499 pipeline->device->physical_device->rad_info.family >= CHIP_POLARIS10)
1500 distribution_mode = V_028B6C_DISTRIBUTION_MODE_TRAPEZOIDS;
1501 else
1502 distribution_mode = V_028B6C_DISTRIBUTION_MODE_DONUTS;
1503 } else
1504 distribution_mode = V_028B6C_DISTRIBUTION_MODE_NO_DIST;
1505
1506 tess->tf_param = S_028B6C_TYPE(type) |
1507 S_028B6C_PARTITIONING(partitioning) |
1508 S_028B6C_TOPOLOGY(topology) |
1509 S_028B6C_DISTRIBUTION_MODE(distribution_mode);
1510 }
1511
1512 static const struct radv_prim_vertex_count prim_size_table[] = {
1513 [V_008958_DI_PT_NONE] = {0, 0},
1514 [V_008958_DI_PT_POINTLIST] = {1, 1},
1515 [V_008958_DI_PT_LINELIST] = {2, 2},
1516 [V_008958_DI_PT_LINESTRIP] = {2, 1},
1517 [V_008958_DI_PT_TRILIST] = {3, 3},
1518 [V_008958_DI_PT_TRIFAN] = {3, 1},
1519 [V_008958_DI_PT_TRISTRIP] = {3, 1},
1520 [V_008958_DI_PT_LINELIST_ADJ] = {4, 4},
1521 [V_008958_DI_PT_LINESTRIP_ADJ] = {4, 1},
1522 [V_008958_DI_PT_TRILIST_ADJ] = {6, 6},
1523 [V_008958_DI_PT_TRISTRIP_ADJ] = {6, 2},
1524 [V_008958_DI_PT_RECTLIST] = {3, 3},
1525 [V_008958_DI_PT_LINELOOP] = {2, 1},
1526 [V_008958_DI_PT_POLYGON] = {3, 1},
1527 [V_008958_DI_PT_2D_TRI_STRIP] = {0, 0},
1528 };
1529
1530 static struct ac_vs_output_info *get_vs_output_info(struct radv_pipeline *pipeline)
1531 {
1532 if (radv_pipeline_has_gs(pipeline))
1533 return &pipeline->gs_copy_shader->info.vs.outinfo;
1534 else if (radv_pipeline_has_tess(pipeline))
1535 return &pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.outinfo;
1536 else
1537 return &pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.outinfo;
1538 }
1539
1540 static void calculate_vgt_gs_mode(struct radv_pipeline *pipeline)
1541 {
1542 struct ac_vs_output_info *outinfo = get_vs_output_info(pipeline);
1543
1544 pipeline->graphics.vgt_primitiveid_en = false;
1545 pipeline->graphics.vgt_gs_mode = 0;
1546
1547 if (radv_pipeline_has_gs(pipeline)) {
1548 struct radv_shader_variant *gs =
1549 pipeline->shaders[MESA_SHADER_GEOMETRY];
1550
1551 pipeline->graphics.vgt_gs_mode =
1552 ac_vgt_gs_mode(gs->info.gs.vertices_out,
1553 pipeline->device->physical_device->rad_info.chip_class);
1554 } else if (outinfo->export_prim_id) {
1555 pipeline->graphics.vgt_gs_mode = S_028A40_MODE(V_028A40_GS_SCENARIO_A);
1556 pipeline->graphics.vgt_primitiveid_en = true;
1557 }
1558 }
1559
1560 static void calculate_vs_outinfo(struct radv_pipeline *pipeline)
1561 {
1562 struct ac_vs_output_info *outinfo = get_vs_output_info(pipeline);
1563
1564 unsigned clip_dist_mask, cull_dist_mask, total_mask;
1565 clip_dist_mask = outinfo->clip_dist_mask;
1566 cull_dist_mask = outinfo->cull_dist_mask;
1567 total_mask = clip_dist_mask | cull_dist_mask;
1568
1569 bool misc_vec_ena = outinfo->writes_pointsize ||
1570 outinfo->writes_layer ||
1571 outinfo->writes_viewport_index;
1572 pipeline->graphics.vs.pa_cl_vs_out_cntl =
1573 S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |
1574 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |
1575 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |
1576 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |
1577 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |
1578 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |
1579 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |
1580 cull_dist_mask << 8 |
1581 clip_dist_mask;
1582
1583 pipeline->graphics.vs.spi_shader_pos_format =
1584 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
1585 S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ?
1586 V_02870C_SPI_SHADER_4COMP :
1587 V_02870C_SPI_SHADER_NONE) |
1588 S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ?
1589 V_02870C_SPI_SHADER_4COMP :
1590 V_02870C_SPI_SHADER_NONE) |
1591 S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ?
1592 V_02870C_SPI_SHADER_4COMP :
1593 V_02870C_SPI_SHADER_NONE);
1594
1595 pipeline->graphics.vs.spi_vs_out_config = S_0286C4_VS_EXPORT_COUNT(MAX2(1, outinfo->param_exports) - 1);
1596 /* only emitted on pre-VI */
1597 pipeline->graphics.vs.vgt_reuse_off = S_028AB4_REUSE_OFF(outinfo->writes_viewport_index);
1598 }
1599
1600 static uint32_t offset_to_ps_input(uint32_t offset, bool flat_shade)
1601 {
1602 uint32_t ps_input_cntl;
1603 if (offset <= AC_EXP_PARAM_OFFSET_31) {
1604 ps_input_cntl = S_028644_OFFSET(offset);
1605 if (flat_shade)
1606 ps_input_cntl |= S_028644_FLAT_SHADE(1);
1607 } else {
1608 /* The input is a DEFAULT_VAL constant. */
1609 assert(offset >= AC_EXP_PARAM_DEFAULT_VAL_0000 &&
1610 offset <= AC_EXP_PARAM_DEFAULT_VAL_1111);
1611 offset -= AC_EXP_PARAM_DEFAULT_VAL_0000;
1612 ps_input_cntl = S_028644_OFFSET(0x20) |
1613 S_028644_DEFAULT_VAL(offset);
1614 }
1615 return ps_input_cntl;
1616 }
1617
1618 static void calculate_ps_inputs(struct radv_pipeline *pipeline)
1619 {
1620 struct radv_shader_variant *ps;
1621 struct ac_vs_output_info *outinfo = get_vs_output_info(pipeline);
1622
1623 ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
1624
1625 unsigned ps_offset = 0;
1626
1627 if (ps->info.fs.prim_id_input) {
1628 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID];
1629 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
1630 pipeline->graphics.ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true);
1631 ++ps_offset;
1632 }
1633 }
1634
1635 if (ps->info.fs.layer_input) {
1636 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_LAYER];
1637 if (vs_offset != AC_EXP_PARAM_UNDEFINED)
1638 pipeline->graphics.ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true);
1639 else
1640 pipeline->graphics.ps_input_cntl[ps_offset] = offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000, true);
1641 ++ps_offset;
1642 }
1643
1644 if (ps->info.fs.has_pcoord) {
1645 unsigned val;
1646 val = S_028644_PT_SPRITE_TEX(1) | S_028644_OFFSET(0x20);
1647 pipeline->graphics.ps_input_cntl[ps_offset] = val;
1648 ps_offset++;
1649 }
1650
1651 for (unsigned i = 0; i < 32 && (1u << i) <= ps->info.fs.input_mask; ++i) {
1652 unsigned vs_offset;
1653 bool flat_shade;
1654 if (!(ps->info.fs.input_mask & (1u << i)))
1655 continue;
1656
1657 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_VAR0 + i];
1658 if (vs_offset == AC_EXP_PARAM_UNDEFINED) {
1659 pipeline->graphics.ps_input_cntl[ps_offset] = S_028644_OFFSET(0x20);
1660 ++ps_offset;
1661 continue;
1662 }
1663
1664 flat_shade = !!(ps->info.fs.flat_shaded_mask & (1u << ps_offset));
1665
1666 pipeline->graphics.ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, flat_shade);
1667 ++ps_offset;
1668 }
1669
1670 pipeline->graphics.ps_input_cntl_num = ps_offset;
1671 }
1672
1673 static void
1674 radv_link_shaders(struct radv_pipeline *pipeline, nir_shader **shaders)
1675 {
1676 nir_shader* ordered_shaders[MESA_SHADER_STAGES];
1677 int shader_count = 0;
1678
1679 if(shaders[MESA_SHADER_FRAGMENT]) {
1680 ordered_shaders[shader_count++] = shaders[MESA_SHADER_FRAGMENT];
1681 }
1682 if(shaders[MESA_SHADER_GEOMETRY]) {
1683 ordered_shaders[shader_count++] = shaders[MESA_SHADER_GEOMETRY];
1684 }
1685 if(shaders[MESA_SHADER_TESS_EVAL]) {
1686 ordered_shaders[shader_count++] = shaders[MESA_SHADER_TESS_EVAL];
1687 }
1688 if(shaders[MESA_SHADER_TESS_CTRL]) {
1689 ordered_shaders[shader_count++] = shaders[MESA_SHADER_TESS_CTRL];
1690 }
1691 if(shaders[MESA_SHADER_VERTEX]) {
1692 ordered_shaders[shader_count++] = shaders[MESA_SHADER_VERTEX];
1693 }
1694
1695 for (int i = 1; i < shader_count; ++i) {
1696 nir_lower_io_arrays_to_elements(ordered_shaders[i],
1697 ordered_shaders[i - 1]);
1698
1699 nir_remove_dead_variables(ordered_shaders[i],
1700 nir_var_shader_out);
1701 nir_remove_dead_variables(ordered_shaders[i - 1],
1702 nir_var_shader_in);
1703
1704 bool progress = nir_remove_unused_varyings(ordered_shaders[i],
1705 ordered_shaders[i - 1]);
1706
1707 if (progress) {
1708 nir_lower_global_vars_to_local(ordered_shaders[i]);
1709 radv_optimize_nir(ordered_shaders[i]);
1710 nir_lower_global_vars_to_local(ordered_shaders[i - 1]);
1711 radv_optimize_nir(ordered_shaders[i - 1]);
1712 }
1713 }
1714 }
1715
1716
1717 static struct radv_pipeline_key
1718 radv_generate_graphics_pipeline_key(struct radv_pipeline *pipeline,
1719 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1720 bool has_view_index)
1721 {
1722 const VkPipelineVertexInputStateCreateInfo *input_state =
1723 pCreateInfo->pVertexInputState;
1724 struct radv_pipeline_key key;
1725 memset(&key, 0, sizeof(key));
1726
1727 key.has_multiview_view_index = has_view_index;
1728
1729 uint32_t binding_input_rate = 0;
1730 for (unsigned i = 0; i < input_state->vertexBindingDescriptionCount; ++i) {
1731 if (input_state->pVertexBindingDescriptions[i].inputRate)
1732 binding_input_rate |= 1u << input_state->pVertexBindingDescriptions[i].binding;
1733 }
1734
1735 for (unsigned i = 0; i < input_state->vertexAttributeDescriptionCount; ++i) {
1736 unsigned binding;
1737 binding = input_state->pVertexAttributeDescriptions[i].binding;
1738 if (binding_input_rate & (1u << binding))
1739 key.instance_rate_inputs |= 1u << input_state->pVertexAttributeDescriptions[i].location;
1740 }
1741
1742 if (pCreateInfo->pTessellationState)
1743 key.tess_input_vertices = pCreateInfo->pTessellationState->patchControlPoints;
1744
1745
1746 if (pCreateInfo->pMultisampleState &&
1747 pCreateInfo->pMultisampleState->rasterizationSamples > 1)
1748 key.multisample = true;
1749
1750 key.col_format = pipeline->graphics.blend.spi_shader_col_format;
1751 if (pipeline->device->physical_device->rad_info.chip_class < VI)
1752 radv_pipeline_compute_get_int_clamp(pCreateInfo, &key.is_int8, &key.is_int10);
1753
1754 return key;
1755 }
1756
1757 static void
1758 radv_fill_shader_keys(struct ac_shader_variant_key *keys,
1759 const struct radv_pipeline_key *key,
1760 nir_shader **nir)
1761 {
1762 keys[MESA_SHADER_VERTEX].vs.instance_rate_inputs = key->instance_rate_inputs;
1763
1764 if (nir[MESA_SHADER_TESS_CTRL]) {
1765 keys[MESA_SHADER_VERTEX].vs.as_ls = true;
1766 keys[MESA_SHADER_TESS_CTRL].tcs.input_vertices = key->tess_input_vertices;
1767 keys[MESA_SHADER_TESS_CTRL].tcs.primitive_mode = nir[MESA_SHADER_TESS_EVAL]->info.tess.primitive_mode;
1768
1769 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));
1770 }
1771
1772 if (nir[MESA_SHADER_GEOMETRY]) {
1773 if (nir[MESA_SHADER_TESS_CTRL])
1774 keys[MESA_SHADER_TESS_EVAL].tes.as_es = true;
1775 else
1776 keys[MESA_SHADER_VERTEX].vs.as_es = true;
1777 }
1778
1779 for(int i = 0; i < MESA_SHADER_STAGES; ++i)
1780 keys[i].has_multiview_view_index = key->has_multiview_view_index;
1781
1782 keys[MESA_SHADER_FRAGMENT].fs.multisample = key->multisample;
1783 keys[MESA_SHADER_FRAGMENT].fs.col_format = key->col_format;
1784 keys[MESA_SHADER_FRAGMENT].fs.is_int8 = key->is_int8;
1785 keys[MESA_SHADER_FRAGMENT].fs.is_int10 = key->is_int10;
1786 }
1787
1788 static void
1789 merge_tess_info(struct shader_info *tes_info,
1790 const struct shader_info *tcs_info)
1791 {
1792 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
1793 *
1794 * "PointMode. Controls generation of points rather than triangles
1795 * or lines. This functionality defaults to disabled, and is
1796 * enabled if either shader stage includes the execution mode.
1797 *
1798 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
1799 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
1800 * and OutputVertices, it says:
1801 *
1802 * "One mode must be set in at least one of the tessellation
1803 * shader stages."
1804 *
1805 * So, the fields can be set in either the TCS or TES, but they must
1806 * agree if set in both. Our backend looks at TES, so bitwise-or in
1807 * the values from the TCS.
1808 */
1809 assert(tcs_info->tess.tcs_vertices_out == 0 ||
1810 tes_info->tess.tcs_vertices_out == 0 ||
1811 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
1812 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
1813
1814 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
1815 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
1816 tcs_info->tess.spacing == tes_info->tess.spacing);
1817 tes_info->tess.spacing |= tcs_info->tess.spacing;
1818
1819 assert(tcs_info->tess.primitive_mode == 0 ||
1820 tes_info->tess.primitive_mode == 0 ||
1821 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
1822 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
1823 tes_info->tess.ccw |= tcs_info->tess.ccw;
1824 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
1825 }
1826
1827 static
1828 void radv_create_shaders(struct radv_pipeline *pipeline,
1829 struct radv_device *device,
1830 struct radv_pipeline_cache *cache,
1831 struct radv_pipeline_key key,
1832 const VkPipelineShaderStageCreateInfo **pStages)
1833 {
1834 struct radv_shader_module fs_m = {0};
1835 struct radv_shader_module *modules[MESA_SHADER_STAGES] = { 0, };
1836 nir_shader *nir[MESA_SHADER_STAGES] = {0};
1837 void *codes[MESA_SHADER_STAGES] = {0};
1838 unsigned code_sizes[MESA_SHADER_STAGES] = {0};
1839 struct ac_shader_variant_key keys[MESA_SHADER_STAGES] = {{{{0}}}};
1840 unsigned char hash[20], gs_copy_hash[20];
1841
1842 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
1843 if (pStages[i]) {
1844 modules[i] = radv_shader_module_from_handle(pStages[i]->module);
1845 if (modules[i]->nir)
1846 _mesa_sha1_compute(modules[i]->nir->info.name,
1847 strlen(modules[i]->nir->info.name),
1848 modules[i]->sha1);
1849 }
1850 }
1851
1852 radv_hash_shaders(hash, pStages, pipeline->layout, &key, get_hash_flags(device));
1853 memcpy(gs_copy_hash, hash, 20);
1854 gs_copy_hash[0] ^= 1;
1855
1856 if (modules[MESA_SHADER_GEOMETRY]) {
1857 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
1858 radv_create_shader_variants_from_pipeline_cache(device, cache, gs_copy_hash, variants);
1859 pipeline->gs_copy_shader = variants[MESA_SHADER_GEOMETRY];
1860 }
1861
1862 if (radv_create_shader_variants_from_pipeline_cache(device, cache, hash, pipeline->shaders) &&
1863 (!modules[MESA_SHADER_GEOMETRY] || pipeline->gs_copy_shader)) {
1864 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
1865 if (pipeline->shaders[i])
1866 pipeline->active_stages |= mesa_to_vk_shader_stage(i);
1867 }
1868 return;
1869 }
1870
1871 if (!modules[MESA_SHADER_FRAGMENT] && !modules[MESA_SHADER_COMPUTE]) {
1872 nir_builder fs_b;
1873 nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
1874 fs_b.shader->info.name = ralloc_strdup(fs_b.shader, "noop_fs");
1875 fs_m.nir = fs_b.shader;
1876 modules[MESA_SHADER_FRAGMENT] = &fs_m;
1877 }
1878
1879 /* Determine first and last stage. */
1880 unsigned first = MESA_SHADER_STAGES;
1881 unsigned last = 0;
1882 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1883 if (!pStages[i])
1884 continue;
1885 if (first == MESA_SHADER_STAGES)
1886 first = i;
1887 last = i;
1888 }
1889
1890 int prev = -1;
1891 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
1892 const VkPipelineShaderStageCreateInfo *stage = pStages[i];
1893
1894 if (!modules[i])
1895 continue;
1896
1897 nir[i] = radv_shader_compile_to_nir(device, modules[i],
1898 stage ? stage->pName : "main", i,
1899 stage ? stage->pSpecializationInfo : NULL);
1900 pipeline->active_stages |= mesa_to_vk_shader_stage(i);
1901
1902 /* We don't want to alter meta shaders IR directly so clone it
1903 * first.
1904 */
1905 if (nir[i]->info.name) {
1906 nir[i] = nir_shader_clone(NULL, nir[i]);
1907 }
1908
1909 if (first != last) {
1910 nir_variable_mode mask = 0;
1911
1912 if (i != first)
1913 mask = mask | nir_var_shader_in;
1914
1915 if (i != last)
1916 mask = mask | nir_var_shader_out;
1917
1918 nir_lower_io_to_scalar_early(nir[i], mask);
1919 radv_optimize_nir(nir[i]);
1920 }
1921
1922 if (prev != -1) {
1923 nir_compact_varyings(nir[prev], nir[i], true);
1924 }
1925 prev = i;
1926 }
1927
1928 if (nir[MESA_SHADER_TESS_CTRL]) {
1929 nir_lower_tes_patch_vertices(nir[MESA_SHADER_TESS_EVAL], nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out);
1930 merge_tess_info(&nir[MESA_SHADER_TESS_EVAL]->info, &nir[MESA_SHADER_TESS_CTRL]->info);
1931 }
1932
1933 radv_link_shaders(pipeline, nir);
1934
1935 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
1936 if (modules[i] && radv_can_dump_shader(device, modules[i]))
1937 nir_print_shader(nir[i], stderr);
1938 }
1939
1940 radv_fill_shader_keys(keys, &key, nir);
1941
1942 if (nir[MESA_SHADER_FRAGMENT]) {
1943 if (!pipeline->shaders[MESA_SHADER_FRAGMENT]) {
1944 pipeline->shaders[MESA_SHADER_FRAGMENT] =
1945 radv_shader_variant_create(device, modules[MESA_SHADER_FRAGMENT], &nir[MESA_SHADER_FRAGMENT], 1,
1946 pipeline->layout, keys + MESA_SHADER_FRAGMENT,
1947 &codes[MESA_SHADER_FRAGMENT], &code_sizes[MESA_SHADER_FRAGMENT]);
1948 }
1949
1950 /* TODO: These are no longer used as keys we should refactor this */
1951 keys[MESA_SHADER_VERTEX].vs.export_prim_id =
1952 pipeline->shaders[MESA_SHADER_FRAGMENT]->info.fs.prim_id_input;
1953 keys[MESA_SHADER_TESS_EVAL].tes.export_prim_id =
1954 pipeline->shaders[MESA_SHADER_FRAGMENT]->info.fs.prim_id_input;
1955 }
1956
1957 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_TESS_CTRL]) {
1958 if (!pipeline->shaders[MESA_SHADER_TESS_CTRL]) {
1959 struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};
1960 struct ac_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];
1961 key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;
1962 pipeline->shaders[MESA_SHADER_TESS_CTRL] = radv_shader_variant_create(device, modules[MESA_SHADER_TESS_CTRL], combined_nir, 2,
1963 pipeline->layout,
1964 &key, &codes[MESA_SHADER_TESS_CTRL],
1965 &code_sizes[MESA_SHADER_TESS_CTRL]);
1966 }
1967 modules[MESA_SHADER_VERTEX] = NULL;
1968 }
1969
1970 if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_GEOMETRY]) {
1971 gl_shader_stage pre_stage = modules[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
1972 if (!pipeline->shaders[MESA_SHADER_GEOMETRY]) {
1973 struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};
1974 pipeline->shaders[MESA_SHADER_GEOMETRY] = radv_shader_variant_create(device, modules[MESA_SHADER_GEOMETRY], combined_nir, 2,
1975 pipeline->layout,
1976 &keys[pre_stage] , &codes[MESA_SHADER_GEOMETRY],
1977 &code_sizes[MESA_SHADER_GEOMETRY]);
1978 }
1979 modules[pre_stage] = NULL;
1980 }
1981
1982 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
1983 if(modules[i] && !pipeline->shaders[i]) {
1984 pipeline->shaders[i] = radv_shader_variant_create(device, modules[i], &nir[i], 1,
1985 pipeline->layout,
1986 keys + i, &codes[i],
1987 &code_sizes[i]);
1988 }
1989 }
1990
1991 if(modules[MESA_SHADER_GEOMETRY]) {
1992 void *gs_copy_code = NULL;
1993 unsigned gs_copy_code_size = 0;
1994 if (!pipeline->gs_copy_shader) {
1995 pipeline->gs_copy_shader = radv_create_gs_copy_shader(
1996 device, nir[MESA_SHADER_GEOMETRY], &gs_copy_code,
1997 &gs_copy_code_size,
1998 keys[MESA_SHADER_GEOMETRY].has_multiview_view_index);
1999 }
2000
2001 if (pipeline->gs_copy_shader) {
2002 void *code[MESA_SHADER_STAGES] = {0};
2003 unsigned code_size[MESA_SHADER_STAGES] = {0};
2004 struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};
2005
2006 code[MESA_SHADER_GEOMETRY] = gs_copy_code;
2007 code_size[MESA_SHADER_GEOMETRY] = gs_copy_code_size;
2008 variants[MESA_SHADER_GEOMETRY] = pipeline->gs_copy_shader;
2009
2010 radv_pipeline_cache_insert_shaders(device, cache,
2011 gs_copy_hash,
2012 variants,
2013 (const void**)code,
2014 code_size);
2015 }
2016 free(gs_copy_code);
2017 }
2018
2019 radv_pipeline_cache_insert_shaders(device, cache, hash, pipeline->shaders,
2020 (const void**)codes, code_sizes);
2021
2022 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
2023 free(codes[i]);
2024 if (modules[i] && !pipeline->device->keep_shader_info)
2025 ralloc_free(nir[i]);
2026 }
2027
2028 if (fs_m.nir)
2029 ralloc_free(fs_m.nir);
2030 }
2031
2032 static uint32_t
2033 radv_pipeline_stage_to_user_data_0(struct radv_pipeline *pipeline,
2034 gl_shader_stage stage, enum chip_class chip_class)
2035 {
2036 bool has_gs = radv_pipeline_has_gs(pipeline);
2037 bool has_tess = radv_pipeline_has_tess(pipeline);
2038 switch (stage) {
2039 case MESA_SHADER_FRAGMENT:
2040 return R_00B030_SPI_SHADER_USER_DATA_PS_0;
2041 case MESA_SHADER_VERTEX:
2042 if (chip_class >= GFX9) {
2043 return has_tess ? R_00B430_SPI_SHADER_USER_DATA_LS_0 :
2044 has_gs ? R_00B330_SPI_SHADER_USER_DATA_ES_0 :
2045 R_00B130_SPI_SHADER_USER_DATA_VS_0;
2046 }
2047 if (has_tess)
2048 return R_00B530_SPI_SHADER_USER_DATA_LS_0;
2049 else
2050 return has_gs ? R_00B330_SPI_SHADER_USER_DATA_ES_0 : R_00B130_SPI_SHADER_USER_DATA_VS_0;
2051 case MESA_SHADER_GEOMETRY:
2052 return chip_class >= GFX9 ? R_00B330_SPI_SHADER_USER_DATA_ES_0 :
2053 R_00B230_SPI_SHADER_USER_DATA_GS_0;
2054 case MESA_SHADER_COMPUTE:
2055 return R_00B900_COMPUTE_USER_DATA_0;
2056 case MESA_SHADER_TESS_CTRL:
2057 return chip_class >= GFX9 ? R_00B430_SPI_SHADER_USER_DATA_LS_0 :
2058 R_00B430_SPI_SHADER_USER_DATA_HS_0;
2059 case MESA_SHADER_TESS_EVAL:
2060 if (chip_class >= GFX9) {
2061 return has_gs ? R_00B330_SPI_SHADER_USER_DATA_ES_0 :
2062 R_00B130_SPI_SHADER_USER_DATA_VS_0;
2063 }
2064 if (has_gs)
2065 return R_00B330_SPI_SHADER_USER_DATA_ES_0;
2066 else
2067 return R_00B130_SPI_SHADER_USER_DATA_VS_0;
2068 default:
2069 unreachable("unknown shader");
2070 }
2071 }
2072
2073 struct radv_bin_size_entry {
2074 unsigned bpp;
2075 VkExtent2D extent;
2076 };
2077
2078 static VkExtent2D
2079 radv_compute_bin_size(struct radv_pipeline *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo)
2080 {
2081 static const struct radv_bin_size_entry color_size_table[][3][9] = {
2082 {
2083 /* One RB / SE */
2084 {
2085 /* One shader engine */
2086 { 0, {128, 128}},
2087 { 1, { 64, 128}},
2088 { 2, { 32, 128}},
2089 { 3, { 16, 128}},
2090 { 17, { 0, 0}},
2091 { UINT_MAX, { 0, 0}},
2092 },
2093 {
2094 /* Two shader engines */
2095 { 0, {128, 128}},
2096 { 2, { 64, 128}},
2097 { 3, { 32, 128}},
2098 { 5, { 16, 128}},
2099 { 17, { 0, 0}},
2100 { UINT_MAX, { 0, 0}},
2101 },
2102 {
2103 /* Four shader engines */
2104 { 0, {128, 128}},
2105 { 3, { 64, 128}},
2106 { 5, { 16, 128}},
2107 { 17, { 0, 0}},
2108 { UINT_MAX, { 0, 0}},
2109 },
2110 },
2111 {
2112 /* Two RB / SE */
2113 {
2114 /* One shader engine */
2115 { 0, {128, 128}},
2116 { 2, { 64, 128}},
2117 { 3, { 32, 128}},
2118 { 5, { 16, 128}},
2119 { 33, { 0, 0}},
2120 { UINT_MAX, { 0, 0}},
2121 },
2122 {
2123 /* Two shader engines */
2124 { 0, {128, 128}},
2125 { 3, { 64, 128}},
2126 { 5, { 32, 128}},
2127 { 9, { 16, 128}},
2128 { 33, { 0, 0}},
2129 { UINT_MAX, { 0, 0}},
2130 },
2131 {
2132 /* Four shader engines */
2133 { 0, {256, 256}},
2134 { 2, {128, 256}},
2135 { 3, {128, 128}},
2136 { 5, { 64, 128}},
2137 { 9, { 16, 128}},
2138 { 33, { 0, 0}},
2139 { UINT_MAX, { 0, 0}},
2140 },
2141 },
2142 {
2143 /* Four RB / SE */
2144 {
2145 /* One shader engine */
2146 { 0, {128, 256}},
2147 { 2, {128, 128}},
2148 { 3, { 64, 128}},
2149 { 5, { 32, 128}},
2150 { 9, { 16, 128}},
2151 { 33, { 0, 0}},
2152 { UINT_MAX, { 0, 0}},
2153 },
2154 {
2155 /* Two shader engines */
2156 { 0, {256, 256}},
2157 { 2, {128, 256}},
2158 { 3, {128, 128}},
2159 { 5, { 64, 128}},
2160 { 9, { 32, 128}},
2161 { 17, { 16, 128}},
2162 { 33, { 0, 0}},
2163 { UINT_MAX, { 0, 0}},
2164 },
2165 {
2166 /* Four shader engines */
2167 { 0, {256, 512}},
2168 { 2, {256, 256}},
2169 { 3, {128, 256}},
2170 { 5, {128, 128}},
2171 { 9, { 64, 128}},
2172 { 17, { 16, 128}},
2173 { 33, { 0, 0}},
2174 { UINT_MAX, { 0, 0}},
2175 },
2176 },
2177 };
2178 static const struct radv_bin_size_entry ds_size_table[][3][9] = {
2179 {
2180 // One RB / SE
2181 {
2182 // One shader engine
2183 { 0, {128, 256}},
2184 { 2, {128, 128}},
2185 { 4, { 64, 128}},
2186 { 7, { 32, 128}},
2187 { 13, { 16, 128}},
2188 { 49, { 0, 0}},
2189 { UINT_MAX, { 0, 0}},
2190 },
2191 {
2192 // Two shader engines
2193 { 0, {256, 256}},
2194 { 2, {128, 256}},
2195 { 4, {128, 128}},
2196 { 7, { 64, 128}},
2197 { 13, { 32, 128}},
2198 { 25, { 16, 128}},
2199 { 49, { 0, 0}},
2200 { UINT_MAX, { 0, 0}},
2201 },
2202 {
2203 // Four shader engines
2204 { 0, {256, 512}},
2205 { 2, {256, 256}},
2206 { 4, {128, 256}},
2207 { 7, {128, 128}},
2208 { 13, { 64, 128}},
2209 { 25, { 16, 128}},
2210 { 49, { 0, 0}},
2211 { UINT_MAX, { 0, 0}},
2212 },
2213 },
2214 {
2215 // Two RB / SE
2216 {
2217 // One shader engine
2218 { 0, {256, 256}},
2219 { 2, {128, 256}},
2220 { 4, {128, 128}},
2221 { 7, { 64, 128}},
2222 { 13, { 32, 128}},
2223 { 25, { 16, 128}},
2224 { 97, { 0, 0}},
2225 { UINT_MAX, { 0, 0}},
2226 },
2227 {
2228 // Two shader engines
2229 { 0, {256, 512}},
2230 { 2, {256, 256}},
2231 { 4, {128, 256}},
2232 { 7, {128, 128}},
2233 { 13, { 64, 128}},
2234 { 25, { 32, 128}},
2235 { 49, { 16, 128}},
2236 { 97, { 0, 0}},
2237 { UINT_MAX, { 0, 0}},
2238 },
2239 {
2240 // Four shader engines
2241 { 0, {512, 512}},
2242 { 2, {256, 512}},
2243 { 4, {256, 256}},
2244 { 7, {128, 256}},
2245 { 13, {128, 128}},
2246 { 25, { 64, 128}},
2247 { 49, { 16, 128}},
2248 { 97, { 0, 0}},
2249 { UINT_MAX, { 0, 0}},
2250 },
2251 },
2252 {
2253 // Four RB / SE
2254 {
2255 // One shader engine
2256 { 0, {256, 512}},
2257 { 2, {256, 256}},
2258 { 4, {128, 256}},
2259 { 7, {128, 128}},
2260 { 13, { 64, 128}},
2261 { 25, { 32, 128}},
2262 { 49, { 16, 128}},
2263 { UINT_MAX, { 0, 0}},
2264 },
2265 {
2266 // Two shader engines
2267 { 0, {512, 512}},
2268 { 2, {256, 512}},
2269 { 4, {256, 256}},
2270 { 7, {128, 256}},
2271 { 13, {128, 128}},
2272 { 25, { 64, 128}},
2273 { 49, { 32, 128}},
2274 { 97, { 16, 128}},
2275 { UINT_MAX, { 0, 0}},
2276 },
2277 {
2278 // Four shader engines
2279 { 0, {512, 512}},
2280 { 4, {256, 512}},
2281 { 7, {256, 256}},
2282 { 13, {128, 256}},
2283 { 25, {128, 128}},
2284 { 49, { 64, 128}},
2285 { 97, { 16, 128}},
2286 { UINT_MAX, { 0, 0}},
2287 },
2288 },
2289 };
2290
2291 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
2292 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
2293 VkExtent2D extent = {512, 512};
2294
2295 unsigned log_num_rb_per_se =
2296 util_logbase2_ceil(pipeline->device->physical_device->rad_info.num_render_backends /
2297 pipeline->device->physical_device->rad_info.max_se);
2298 unsigned log_num_se = util_logbase2_ceil(pipeline->device->physical_device->rad_info.max_se);
2299
2300 unsigned total_samples = 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline->graphics.ms.pa_sc_mode_cntl_1);
2301 unsigned ps_iter_samples = 1u << G_028804_PS_ITER_SAMPLES(pipeline->graphics.ms.db_eqaa);
2302 unsigned effective_samples = total_samples;
2303 unsigned cb_target_mask = pipeline->graphics.blend.cb_target_mask;
2304 unsigned color_bytes_per_pixel = 0;
2305
2306 for (unsigned i = 0; i < subpass->color_count; i++) {
2307 if (!(cb_target_mask & (0xf << (i * 4))))
2308 continue;
2309
2310 if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)
2311 continue;
2312
2313 VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;
2314 color_bytes_per_pixel += vk_format_get_blocksize(format);
2315 }
2316
2317 /* MSAA images typically don't use all samples all the time. */
2318 if (effective_samples >= 2 && ps_iter_samples <= 1)
2319 effective_samples = 2;
2320 color_bytes_per_pixel *= effective_samples;
2321
2322 const struct radv_bin_size_entry *color_entry = color_size_table[log_num_rb_per_se][log_num_se];
2323 while(color_entry->bpp <= color_bytes_per_pixel)
2324 ++color_entry;
2325
2326 extent = color_entry->extent;
2327
2328 if (subpass->depth_stencil_attachment.attachment != VK_ATTACHMENT_UNUSED) {
2329 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->depth_stencil_attachment.attachment;
2330
2331 /* Coefficients taken from AMDVLK */
2332 unsigned depth_coeff = vk_format_is_depth(attachment->format) ? 5 : 0;
2333 unsigned stencil_coeff = vk_format_is_stencil(attachment->format) ? 1 : 0;
2334 unsigned ds_bytes_per_pixel = 4 * (depth_coeff + stencil_coeff) * total_samples;
2335
2336 const struct radv_bin_size_entry *ds_entry = ds_size_table[log_num_rb_per_se][log_num_se];
2337 while(ds_entry->bpp <= ds_bytes_per_pixel)
2338 ++ds_entry;
2339
2340 extent.width = MIN2(extent.width, ds_entry->extent.width);
2341 extent.height = MIN2(extent.height, ds_entry->extent.height);
2342 }
2343
2344 return extent;
2345 }
2346
2347 static void
2348 radv_compute_binning_state(struct radv_pipeline *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo)
2349 {
2350 pipeline->graphics.bin.pa_sc_binner_cntl_0 =
2351 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC) |
2352 S_028C44_DISABLE_START_OF_PRIM(1);
2353 pipeline->graphics.bin.db_dfsm_control = S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF);
2354
2355 if (!pipeline->device->pbb_allowed)
2356 return;
2357
2358 VkExtent2D bin_size = radv_compute_bin_size(pipeline, pCreateInfo);
2359 if (!bin_size.width || !bin_size.height)
2360 return;
2361
2362 unsigned context_states_per_bin; /* allowed range: [1, 6] */
2363 unsigned persistent_states_per_bin; /* allowed range: [1, 32] */
2364 unsigned fpovs_per_batch; /* allowed range: [0, 255], 0 = unlimited */
2365
2366 switch (pipeline->device->physical_device->rad_info.family) {
2367 case CHIP_VEGA10:
2368 context_states_per_bin = 1;
2369 persistent_states_per_bin = 1;
2370 fpovs_per_batch = 63;
2371 break;
2372 case CHIP_RAVEN:
2373 context_states_per_bin = 6;
2374 persistent_states_per_bin = 32;
2375 fpovs_per_batch = 63;
2376 break;
2377 default:
2378 unreachable("unhandled family while determining binning state.");
2379 }
2380
2381 pipeline->graphics.bin.pa_sc_binner_cntl_0 =
2382 S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED) |
2383 S_028C44_BIN_SIZE_X(bin_size.width == 16) |
2384 S_028C44_BIN_SIZE_Y(bin_size.height == 16) |
2385 S_028C44_BIN_SIZE_X_EXTEND(util_logbase2(MAX2(bin_size.width, 32)) - 5) |
2386 S_028C44_BIN_SIZE_Y_EXTEND(util_logbase2(MAX2(bin_size.height, 32)) - 5) |
2387 S_028C44_CONTEXT_STATES_PER_BIN(context_states_per_bin - 1) |
2388 S_028C44_PERSISTENT_STATES_PER_BIN(persistent_states_per_bin - 1) |
2389 S_028C44_DISABLE_START_OF_PRIM(1) |
2390 S_028C44_FPOVS_PER_BATCH(fpovs_per_batch) |
2391 S_028C44_OPTIMAL_BIN_SELECTION(1);
2392
2393 /* DFSM is not implemented yet */
2394 assert(!pipeline->device->dfsm_allowed);
2395 }
2396
2397 static VkResult
2398 radv_pipeline_init(struct radv_pipeline *pipeline,
2399 struct radv_device *device,
2400 struct radv_pipeline_cache *cache,
2401 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2402 const struct radv_graphics_pipeline_create_info *extra,
2403 const VkAllocationCallbacks *alloc)
2404 {
2405 VkResult result;
2406 bool has_view_index = false;
2407
2408 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
2409 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
2410 if (subpass->view_mask)
2411 has_view_index = true;
2412 if (alloc == NULL)
2413 alloc = &device->alloc;
2414
2415 pipeline->device = device;
2416 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
2417 assert(pipeline->layout);
2418
2419 radv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
2420 radv_pipeline_init_blend_state(pipeline, pCreateInfo, extra);
2421
2422 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
2423 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2424 gl_shader_stage stage = ffs(pCreateInfo->pStages[i].stage) - 1;
2425 pStages[stage] = &pCreateInfo->pStages[i];
2426 }
2427
2428 radv_create_shaders(pipeline, device, cache,
2429 radv_generate_graphics_pipeline_key(pipeline, pCreateInfo, has_view_index),
2430 pStages);
2431
2432 radv_pipeline_init_depth_stencil_state(pipeline, pCreateInfo, extra);
2433 radv_pipeline_init_raster_state(pipeline, pCreateInfo);
2434 radv_pipeline_init_multisample_state(pipeline, pCreateInfo);
2435 pipeline->graphics.prim = si_translate_prim(pCreateInfo->pInputAssemblyState->topology);
2436 pipeline->graphics.can_use_guardband = radv_prim_can_use_guardband(pCreateInfo->pInputAssemblyState->topology);
2437
2438 if (radv_pipeline_has_gs(pipeline)) {
2439 pipeline->graphics.gs_out = si_conv_gl_prim_to_gs_out(pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs.output_prim);
2440 pipeline->graphics.can_use_guardband = pipeline->graphics.gs_out == V_028A6C_OUTPRIM_TYPE_TRISTRIP;
2441 } else {
2442 pipeline->graphics.gs_out = si_conv_prim_to_gs_out(pCreateInfo->pInputAssemblyState->topology);
2443 }
2444 if (extra && extra->use_rectlist) {
2445 pipeline->graphics.prim = V_008958_DI_PT_RECTLIST;
2446 pipeline->graphics.gs_out = V_028A6C_OUTPRIM_TYPE_TRISTRIP;
2447 pipeline->graphics.can_use_guardband = true;
2448 }
2449 pipeline->graphics.prim_restart_enable = !!pCreateInfo->pInputAssemblyState->primitiveRestartEnable;
2450 /* prim vertex count will need TESS changes */
2451 pipeline->graphics.prim_vertex_count = prim_size_table[pipeline->graphics.prim];
2452
2453 /* Ensure that some export memory is always allocated, for two reasons:
2454 *
2455 * 1) Correctness: The hardware ignores the EXEC mask if no export
2456 * memory is allocated, so KILL and alpha test do not work correctly
2457 * without this.
2458 * 2) Performance: Every shader needs at least a NULL export, even when
2459 * it writes no color/depth output. The NULL export instruction
2460 * stalls without this setting.
2461 *
2462 * Don't add this to CB_SHADER_MASK.
2463 */
2464 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
2465 if (!pipeline->graphics.blend.spi_shader_col_format) {
2466 if (!ps->info.fs.writes_z &&
2467 !ps->info.fs.writes_stencil &&
2468 !ps->info.fs.writes_sample_mask)
2469 pipeline->graphics.blend.spi_shader_col_format = V_028714_SPI_SHADER_32_R;
2470 }
2471
2472 unsigned z_order;
2473 pipeline->graphics.db_shader_control = 0;
2474 if (ps->info.fs.early_fragment_test || !ps->info.fs.writes_memory)
2475 z_order = V_02880C_EARLY_Z_THEN_LATE_Z;
2476 else
2477 z_order = V_02880C_LATE_Z;
2478
2479 pipeline->graphics.db_shader_control =
2480 S_02880C_Z_EXPORT_ENABLE(ps->info.fs.writes_z) |
2481 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps->info.fs.writes_stencil) |
2482 S_02880C_KILL_ENABLE(!!ps->info.fs.can_discard) |
2483 S_02880C_MASK_EXPORT_ENABLE(ps->info.fs.writes_sample_mask) |
2484 S_02880C_Z_ORDER(z_order) |
2485 S_02880C_DEPTH_BEFORE_SHADER(ps->info.fs.early_fragment_test) |
2486 S_02880C_EXEC_ON_HIER_FAIL(ps->info.fs.writes_memory) |
2487 S_02880C_EXEC_ON_NOOP(ps->info.fs.writes_memory);
2488
2489 if (pipeline->device->physical_device->has_rbplus)
2490 pipeline->graphics.db_shader_control |= S_02880C_DUAL_QUAD_DISABLE(1);
2491
2492 unsigned shader_z_format =
2493 ac_get_spi_shader_z_format(ps->info.fs.writes_z,
2494 ps->info.fs.writes_stencil,
2495 ps->info.fs.writes_sample_mask);
2496 pipeline->graphics.shader_z_format = shader_z_format;
2497
2498 calculate_vgt_gs_mode(pipeline);
2499 calculate_vs_outinfo(pipeline);
2500 calculate_ps_inputs(pipeline);
2501
2502 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2503 if (pipeline->shaders[i]) {
2504 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[i]->info.need_indirect_descriptor_sets;
2505 }
2506 }
2507
2508 uint32_t stages = 0;
2509 if (radv_pipeline_has_tess(pipeline)) {
2510 stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) |
2511 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
2512
2513 if (radv_pipeline_has_gs(pipeline))
2514 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) |
2515 S_028B54_GS_EN(1) |
2516 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
2517 else
2518 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS);
2519
2520 } else if (radv_pipeline_has_gs(pipeline))
2521 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) |
2522 S_028B54_GS_EN(1) |
2523 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
2524
2525 if (device->physical_device->rad_info.chip_class >= GFX9)
2526 stages |= S_028B54_MAX_PRIMGRP_IN_WAVE(2);
2527
2528 pipeline->graphics.vgt_shader_stages_en = stages;
2529
2530 if (radv_pipeline_has_gs(pipeline)) {
2531 calculate_gs_ring_sizes(pipeline);
2532 if (device->physical_device->rad_info.chip_class >= GFX9)
2533 calculate_gfx9_gs_info(pCreateInfo, pipeline);
2534 }
2535
2536 if (radv_pipeline_has_tess(pipeline)) {
2537 if (pipeline->graphics.prim == V_008958_DI_PT_PATCH) {
2538 pipeline->graphics.prim_vertex_count.min = pCreateInfo->pTessellationState->patchControlPoints;
2539 pipeline->graphics.prim_vertex_count.incr = 1;
2540 }
2541 calculate_tess_state(pipeline, pCreateInfo);
2542 }
2543
2544 if (radv_pipeline_has_tess(pipeline))
2545 pipeline->graphics.primgroup_size = pipeline->graphics.tess.num_patches;
2546 else if (radv_pipeline_has_gs(pipeline))
2547 pipeline->graphics.primgroup_size = 64;
2548 else
2549 pipeline->graphics.primgroup_size = 128; /* recommended without a GS */
2550
2551 pipeline->graphics.partial_es_wave = false;
2552 if (pipeline->device->has_distributed_tess) {
2553 if (radv_pipeline_has_gs(pipeline)) {
2554 if (device->physical_device->rad_info.chip_class <= VI)
2555 pipeline->graphics.partial_es_wave = true;
2556 }
2557 }
2558 /* GS requirement. */
2559 if (SI_GS_PER_ES / pipeline->graphics.primgroup_size >= pipeline->device->gs_table_depth - 3)
2560 pipeline->graphics.partial_es_wave = true;
2561
2562 pipeline->graphics.wd_switch_on_eop = false;
2563 if (device->physical_device->rad_info.chip_class >= CIK) {
2564 unsigned prim = pipeline->graphics.prim;
2565 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
2566 * 4 shader engines. Set 1 to pass the assertion below.
2567 * The other cases are hardware requirements. */
2568 if (device->physical_device->rad_info.max_se < 4 ||
2569 prim == V_008958_DI_PT_POLYGON ||
2570 prim == V_008958_DI_PT_LINELOOP ||
2571 prim == V_008958_DI_PT_TRIFAN ||
2572 prim == V_008958_DI_PT_TRISTRIP_ADJ ||
2573 (pipeline->graphics.prim_restart_enable &&
2574 (device->physical_device->rad_info.family < CHIP_POLARIS10 ||
2575 (prim != V_008958_DI_PT_POINTLIST &&
2576 prim != V_008958_DI_PT_LINESTRIP &&
2577 prim != V_008958_DI_PT_TRISTRIP))))
2578 pipeline->graphics.wd_switch_on_eop = true;
2579 }
2580
2581 pipeline->graphics.ia_switch_on_eoi = false;
2582 if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.fs.prim_id_input)
2583 pipeline->graphics.ia_switch_on_eoi = true;
2584 if (radv_pipeline_has_gs(pipeline) &&
2585 pipeline->shaders[MESA_SHADER_GEOMETRY]->info.info.uses_prim_id)
2586 pipeline->graphics.ia_switch_on_eoi = true;
2587 if (radv_pipeline_has_tess(pipeline)) {
2588 /* SWITCH_ON_EOI must be set if PrimID is used. */
2589 if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.info.uses_prim_id ||
2590 radv_get_tess_eval_shader(pipeline)->info.info.uses_prim_id)
2591 pipeline->graphics.ia_switch_on_eoi = true;
2592 }
2593
2594 pipeline->graphics.partial_vs_wave = false;
2595 if (radv_pipeline_has_tess(pipeline)) {
2596 /* Bug with tessellation and GS on Bonaire and older 2 SE chips. */
2597 if ((device->physical_device->rad_info.family == CHIP_TAHITI ||
2598 device->physical_device->rad_info.family == CHIP_PITCAIRN ||
2599 device->physical_device->rad_info.family == CHIP_BONAIRE) &&
2600 radv_pipeline_has_gs(pipeline))
2601 pipeline->graphics.partial_vs_wave = true;
2602 /* Needed for 028B6C_DISTRIBUTION_MODE != 0 */
2603 if (device->has_distributed_tess) {
2604 if (radv_pipeline_has_gs(pipeline)) {
2605 if (device->physical_device->rad_info.family == CHIP_TONGA ||
2606 device->physical_device->rad_info.family == CHIP_FIJI ||
2607 device->physical_device->rad_info.family == CHIP_POLARIS10 ||
2608 device->physical_device->rad_info.family == CHIP_POLARIS11 ||
2609 device->physical_device->rad_info.family == CHIP_POLARIS12)
2610 pipeline->graphics.partial_vs_wave = true;
2611 } else {
2612 pipeline->graphics.partial_vs_wave = true;
2613 }
2614 }
2615 }
2616
2617 pipeline->graphics.base_ia_multi_vgt_param =
2618 S_028AA8_PRIMGROUP_SIZE(pipeline->graphics.primgroup_size - 1) |
2619 /* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */
2620 S_028AA8_MAX_PRIMGRP_IN_WAVE(device->physical_device->rad_info.chip_class == VI ? 2 : 0) |
2621 S_030960_EN_INST_OPT_BASIC(device->physical_device->rad_info.chip_class >= GFX9) |
2622 S_030960_EN_INST_OPT_ADV(device->physical_device->rad_info.chip_class >= GFX9);
2623
2624 const VkPipelineVertexInputStateCreateInfo *vi_info =
2625 pCreateInfo->pVertexInputState;
2626 struct radv_vertex_elements_info *velems = &pipeline->vertex_elements;
2627
2628 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
2629 const VkVertexInputAttributeDescription *desc =
2630 &vi_info->pVertexAttributeDescriptions[i];
2631 unsigned loc = desc->location;
2632 const struct vk_format_description *format_desc;
2633 int first_non_void;
2634 uint32_t num_format, data_format;
2635 format_desc = vk_format_description(desc->format);
2636 first_non_void = vk_format_get_first_non_void_channel(desc->format);
2637
2638 num_format = radv_translate_buffer_numformat(format_desc, first_non_void);
2639 data_format = radv_translate_buffer_dataformat(format_desc, first_non_void);
2640
2641 velems->rsrc_word3[loc] = S_008F0C_DST_SEL_X(si_map_swizzle(format_desc->swizzle[0])) |
2642 S_008F0C_DST_SEL_Y(si_map_swizzle(format_desc->swizzle[1])) |
2643 S_008F0C_DST_SEL_Z(si_map_swizzle(format_desc->swizzle[2])) |
2644 S_008F0C_DST_SEL_W(si_map_swizzle(format_desc->swizzle[3])) |
2645 S_008F0C_NUM_FORMAT(num_format) |
2646 S_008F0C_DATA_FORMAT(data_format);
2647 velems->format_size[loc] = format_desc->block.bits / 8;
2648 velems->offset[loc] = desc->offset;
2649 velems->binding[loc] = desc->binding;
2650 velems->count = MAX2(velems->count, loc + 1);
2651 }
2652
2653 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
2654 const VkVertexInputBindingDescription *desc =
2655 &vi_info->pVertexBindingDescriptions[i];
2656
2657 pipeline->binding_stride[desc->binding] = desc->stride;
2658 }
2659
2660 for (uint32_t i = 0; i < MESA_SHADER_STAGES; i++)
2661 pipeline->user_data_0[i] = radv_pipeline_stage_to_user_data_0(pipeline, i, device->physical_device->rad_info.chip_class);
2662
2663 struct ac_userdata_info *loc = radv_lookup_user_sgpr(pipeline, MESA_SHADER_VERTEX,
2664 AC_UD_VS_BASE_VERTEX_START_INSTANCE);
2665 if (loc->sgpr_idx != -1) {
2666 pipeline->graphics.vtx_base_sgpr = pipeline->user_data_0[MESA_SHADER_VERTEX];
2667 pipeline->graphics.vtx_base_sgpr += loc->sgpr_idx * 4;
2668 if (radv_get_vertex_shader(pipeline)->info.info.vs.needs_draw_id)
2669 pipeline->graphics.vtx_emit_num = 3;
2670 else
2671 pipeline->graphics.vtx_emit_num = 2;
2672 }
2673
2674 pipeline->graphics.vtx_reuse_depth = 30;
2675 if (radv_pipeline_has_tess(pipeline) &&
2676 radv_get_tess_eval_shader(pipeline)->info.tes.spacing == TESS_SPACING_FRACTIONAL_ODD) {
2677 pipeline->graphics.vtx_reuse_depth = 14;
2678 }
2679
2680 if (device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS) {
2681 radv_dump_pipeline_stats(device, pipeline);
2682 }
2683
2684 radv_compute_binning_state(pipeline, pCreateInfo);
2685
2686 result = radv_pipeline_scratch_init(device, pipeline);
2687 return result;
2688 }
2689
2690 VkResult
2691 radv_graphics_pipeline_create(
2692 VkDevice _device,
2693 VkPipelineCache _cache,
2694 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2695 const struct radv_graphics_pipeline_create_info *extra,
2696 const VkAllocationCallbacks *pAllocator,
2697 VkPipeline *pPipeline)
2698 {
2699 RADV_FROM_HANDLE(radv_device, device, _device);
2700 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
2701 struct radv_pipeline *pipeline;
2702 VkResult result;
2703
2704 pipeline = vk_zalloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
2705 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2706 if (pipeline == NULL)
2707 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2708
2709 result = radv_pipeline_init(pipeline, device, cache,
2710 pCreateInfo, extra, pAllocator);
2711 if (result != VK_SUCCESS) {
2712 radv_pipeline_destroy(device, pipeline, pAllocator);
2713 return result;
2714 }
2715
2716 *pPipeline = radv_pipeline_to_handle(pipeline);
2717
2718 return VK_SUCCESS;
2719 }
2720
2721 VkResult radv_CreateGraphicsPipelines(
2722 VkDevice _device,
2723 VkPipelineCache pipelineCache,
2724 uint32_t count,
2725 const VkGraphicsPipelineCreateInfo* pCreateInfos,
2726 const VkAllocationCallbacks* pAllocator,
2727 VkPipeline* pPipelines)
2728 {
2729 VkResult result = VK_SUCCESS;
2730 unsigned i = 0;
2731
2732 for (; i < count; i++) {
2733 VkResult r;
2734 r = radv_graphics_pipeline_create(_device,
2735 pipelineCache,
2736 &pCreateInfos[i],
2737 NULL, pAllocator, &pPipelines[i]);
2738 if (r != VK_SUCCESS) {
2739 result = r;
2740 pPipelines[i] = VK_NULL_HANDLE;
2741 }
2742 }
2743
2744 return result;
2745 }
2746
2747 static VkResult radv_compute_pipeline_create(
2748 VkDevice _device,
2749 VkPipelineCache _cache,
2750 const VkComputePipelineCreateInfo* pCreateInfo,
2751 const VkAllocationCallbacks* pAllocator,
2752 VkPipeline* pPipeline)
2753 {
2754 RADV_FROM_HANDLE(radv_device, device, _device);
2755 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
2756 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
2757 struct radv_pipeline *pipeline;
2758 VkResult result;
2759
2760 pipeline = vk_zalloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
2761 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2762 if (pipeline == NULL)
2763 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2764
2765 pipeline->device = device;
2766 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
2767 assert(pipeline->layout);
2768
2769 pStages[MESA_SHADER_COMPUTE] = &pCreateInfo->stage;
2770 radv_create_shaders(pipeline, device, cache, (struct radv_pipeline_key) {0}, pStages);
2771
2772 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);
2773 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[MESA_SHADER_COMPUTE]->info.need_indirect_descriptor_sets;
2774 result = radv_pipeline_scratch_init(device, pipeline);
2775 if (result != VK_SUCCESS) {
2776 radv_pipeline_destroy(device, pipeline, pAllocator);
2777 return result;
2778 }
2779
2780 *pPipeline = radv_pipeline_to_handle(pipeline);
2781
2782 if (device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS) {
2783 radv_dump_pipeline_stats(device, pipeline);
2784 }
2785 return VK_SUCCESS;
2786 }
2787 VkResult radv_CreateComputePipelines(
2788 VkDevice _device,
2789 VkPipelineCache pipelineCache,
2790 uint32_t count,
2791 const VkComputePipelineCreateInfo* pCreateInfos,
2792 const VkAllocationCallbacks* pAllocator,
2793 VkPipeline* pPipelines)
2794 {
2795 VkResult result = VK_SUCCESS;
2796
2797 unsigned i = 0;
2798 for (; i < count; i++) {
2799 VkResult r;
2800 r = radv_compute_pipeline_create(_device, pipelineCache,
2801 &pCreateInfos[i],
2802 pAllocator, &pPipelines[i]);
2803 if (r != VK_SUCCESS) {
2804 result = r;
2805 pPipelines[i] = VK_NULL_HANDLE;
2806 }
2807 }
2808
2809 return result;
2810 }