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