2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
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:
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
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
28 #include "util/mesa-sha1.h"
29 #include "radv_private.h"
31 #include "nir/nir_builder.h"
32 #include "spirv/nir_spirv.h"
34 #include <llvm-c/Core.h>
35 #include <llvm-c/TargetMachine.h>
38 #include "r600d_common.h"
39 #include "ac_binary.h"
40 #include "ac_llvm_util.h"
41 #include "ac_nir_to_llvm.h"
42 #include "vk_format.h"
43 #include "util/debug.h"
45 void radv_shader_variant_destroy(struct radv_device
*device
,
46 struct radv_shader_variant
*variant
);
48 static const struct nir_shader_compiler_options nir_options
= {
49 .vertex_id_zero_based
= true,
53 .lower_pack_snorm_2x16
= true,
54 .lower_pack_snorm_4x8
= true,
55 .lower_pack_unorm_2x16
= true,
56 .lower_pack_unorm_4x8
= true,
57 .lower_unpack_snorm_2x16
= true,
58 .lower_unpack_snorm_4x8
= true,
59 .lower_unpack_unorm_2x16
= true,
60 .lower_unpack_unorm_4x8
= true,
61 .lower_extract_byte
= true,
62 .lower_extract_word
= true,
65 VkResult
radv_CreateShaderModule(
67 const VkShaderModuleCreateInfo
* pCreateInfo
,
68 const VkAllocationCallbacks
* pAllocator
,
69 VkShaderModule
* pShaderModule
)
71 RADV_FROM_HANDLE(radv_device
, device
, _device
);
72 struct radv_shader_module
*module
;
74 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
75 assert(pCreateInfo
->flags
== 0);
77 module
= vk_alloc2(&device
->alloc
, pAllocator
,
78 sizeof(*module
) + pCreateInfo
->codeSize
, 8,
79 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
81 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
84 module
->size
= pCreateInfo
->codeSize
;
85 memcpy(module
->data
, pCreateInfo
->pCode
, module
->size
);
87 _mesa_sha1_compute(module
->data
, module
->size
, module
->sha1
);
89 *pShaderModule
= radv_shader_module_to_handle(module
);
94 void radv_DestroyShaderModule(
96 VkShaderModule _module
,
97 const VkAllocationCallbacks
* pAllocator
)
99 RADV_FROM_HANDLE(radv_device
, device
, _device
);
100 RADV_FROM_HANDLE(radv_shader_module
, module
, _module
);
105 vk_free2(&device
->alloc
, pAllocator
, module
);
110 radv_pipeline_destroy(struct radv_device
*device
,
111 struct radv_pipeline
*pipeline
,
112 const VkAllocationCallbacks
* allocator
)
114 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
115 if (pipeline
->shaders
[i
])
116 radv_shader_variant_destroy(device
, pipeline
->shaders
[i
]);
118 if (pipeline
->gs_copy_shader
)
119 radv_shader_variant_destroy(device
, pipeline
->gs_copy_shader
);
121 vk_free2(&device
->alloc
, allocator
, pipeline
);
124 void radv_DestroyPipeline(
126 VkPipeline _pipeline
,
127 const VkAllocationCallbacks
* pAllocator
)
129 RADV_FROM_HANDLE(radv_device
, device
, _device
);
130 RADV_FROM_HANDLE(radv_pipeline
, pipeline
, _pipeline
);
135 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
140 radv_optimize_nir(struct nir_shader
*shader
)
147 NIR_PASS_V(shader
, nir_lower_vars_to_ssa
);
148 NIR_PASS_V(shader
, nir_lower_alu_to_scalar
);
149 NIR_PASS_V(shader
, nir_lower_phis_to_scalar
);
151 NIR_PASS(progress
, shader
, nir_copy_prop
);
152 NIR_PASS(progress
, shader
, nir_opt_remove_phis
);
153 NIR_PASS(progress
, shader
, nir_opt_dce
);
154 NIR_PASS(progress
, shader
, nir_opt_dead_cf
);
155 NIR_PASS(progress
, shader
, nir_opt_cse
);
156 NIR_PASS(progress
, shader
, nir_opt_peephole_select
, 8);
157 NIR_PASS(progress
, shader
, nir_opt_algebraic
);
158 NIR_PASS(progress
, shader
, nir_opt_constant_folding
);
159 NIR_PASS(progress
, shader
, nir_opt_undef
);
160 NIR_PASS(progress
, shader
, nir_opt_conditional_discard
);
165 radv_shader_compile_to_nir(struct radv_device
*device
,
166 struct radv_shader_module
*module
,
167 const char *entrypoint_name
,
168 gl_shader_stage stage
,
169 const VkSpecializationInfo
*spec_info
,
172 if (strcmp(entrypoint_name
, "main") != 0) {
173 radv_finishme("Multiple shaders per module not really supported");
177 nir_function
*entry_point
;
179 /* Some things such as our meta clear/blit code will give us a NIR
180 * shader directly. In that case, we just ignore the SPIR-V entirely
181 * and just use the NIR shader */
183 nir
->options
= &nir_options
;
184 nir_validate_shader(nir
);
186 assert(exec_list_length(&nir
->functions
) == 1);
187 struct exec_node
*node
= exec_list_get_head(&nir
->functions
);
188 entry_point
= exec_node_data(nir_function
, node
, node
);
190 uint32_t *spirv
= (uint32_t *) module
->data
;
191 assert(module
->size
% 4 == 0);
193 uint32_t num_spec_entries
= 0;
194 struct nir_spirv_specialization
*spec_entries
= NULL
;
195 if (spec_info
&& spec_info
->mapEntryCount
> 0) {
196 num_spec_entries
= spec_info
->mapEntryCount
;
197 spec_entries
= malloc(num_spec_entries
* sizeof(*spec_entries
));
198 for (uint32_t i
= 0; i
< num_spec_entries
; i
++) {
199 VkSpecializationMapEntry entry
= spec_info
->pMapEntries
[i
];
200 const void *data
= spec_info
->pData
+ entry
.offset
;
201 assert(data
+ entry
.size
<= spec_info
->pData
+ spec_info
->dataSize
);
203 spec_entries
[i
].id
= spec_info
->pMapEntries
[i
].constantID
;
204 if (spec_info
->dataSize
== 8)
205 spec_entries
[i
].data64
= *(const uint64_t *)data
;
207 spec_entries
[i
].data32
= *(const uint32_t *)data
;
210 const struct nir_spirv_supported_extensions supported_ext
= {
211 .draw_parameters
= true,
213 .image_read_without_format
= true,
214 .image_write_without_format
= true,
216 entry_point
= spirv_to_nir(spirv
, module
->size
/ 4,
217 spec_entries
, num_spec_entries
,
218 stage
, entrypoint_name
, &supported_ext
, &nir_options
);
219 nir
= entry_point
->shader
;
220 assert(nir
->stage
== stage
);
221 nir_validate_shader(nir
);
225 /* We have to lower away local constant initializers right before we
226 * inline functions. That way they get properly initialized at the top
227 * of the function and not at the top of its caller.
229 NIR_PASS_V(nir
, nir_lower_constant_initializers
, nir_var_local
);
230 NIR_PASS_V(nir
, nir_lower_returns
);
231 NIR_PASS_V(nir
, nir_inline_functions
);
233 /* Pick off the single entrypoint that we want */
234 foreach_list_typed_safe(nir_function
, func
, node
, &nir
->functions
) {
235 if (func
!= entry_point
)
236 exec_node_remove(&func
->node
);
238 assert(exec_list_length(&nir
->functions
) == 1);
239 entry_point
->name
= ralloc_strdup(entry_point
, "main");
241 NIR_PASS_V(nir
, nir_remove_dead_variables
,
242 nir_var_shader_in
| nir_var_shader_out
| nir_var_system_value
);
244 /* Now that we've deleted all but the main function, we can go ahead and
245 * lower the rest of the constant initializers.
247 NIR_PASS_V(nir
, nir_lower_constant_initializers
, ~0);
248 NIR_PASS_V(nir
, nir_lower_system_values
);
251 /* Vulkan uses the separate-shader linking model */
252 nir
->info
->separate_shader
= true;
254 nir_shader_gather_info(nir
, entry_point
->impl
);
256 nir_variable_mode indirect_mask
= 0;
257 indirect_mask
|= nir_var_shader_in
;
258 indirect_mask
|= nir_var_local
;
260 nir_lower_indirect_derefs(nir
, indirect_mask
);
262 static const nir_lower_tex_options tex_options
= {
266 nir_lower_tex(nir
, &tex_options
);
268 nir_lower_vars_to_ssa(nir
);
269 nir_lower_var_copies(nir
);
270 nir_lower_global_vars_to_local(nir
);
271 nir_remove_dead_variables(nir
, nir_var_local
);
272 radv_optimize_nir(nir
);
275 nir_print_shader(nir
, stderr
);
280 static const char *radv_get_shader_name(struct radv_shader_variant
*var
,
281 gl_shader_stage stage
)
284 case MESA_SHADER_VERTEX
: return var
->info
.vs
.as_es
? "Vertex Shader as ES" : "Vertex Shader as VS";
285 case MESA_SHADER_GEOMETRY
: return "Geometry Shader";
286 case MESA_SHADER_FRAGMENT
: return "Pixel Shader";
287 case MESA_SHADER_COMPUTE
: return "Compute Shader";
289 return "Unknown shader";
293 static void radv_dump_pipeline_stats(struct radv_device
*device
, struct radv_pipeline
*pipeline
)
295 unsigned lds_increment
= device
->physical_device
->rad_info
.chip_class
>= CIK
? 512 : 256;
296 struct radv_shader_variant
*var
;
297 struct ac_shader_config
*conf
;
300 unsigned max_simd_waves
= 10;
301 unsigned lds_per_wave
= 0;
303 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
304 if (!pipeline
->shaders
[i
])
306 var
= pipeline
->shaders
[i
];
310 if (i
== MESA_SHADER_FRAGMENT
) {
311 lds_per_wave
= conf
->lds_size
* lds_increment
+
312 align(var
->info
.fs
.num_interp
* 48, lds_increment
);
315 if (conf
->num_sgprs
) {
316 if (device
->physical_device
->rad_info
.chip_class
>= VI
)
317 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
319 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
323 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
325 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
329 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
331 fprintf(file
, "\n%s:\n",
332 radv_get_shader_name(var
, i
));
333 if (i
== MESA_SHADER_FRAGMENT
) {
334 fprintf(file
, "*** SHADER CONFIG ***\n"
335 "SPI_PS_INPUT_ADDR = 0x%04x\n"
336 "SPI_PS_INPUT_ENA = 0x%04x\n",
337 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
339 fprintf(file
, "*** SHADER STATS ***\n"
342 "Spilled SGPRs: %d\n"
343 "Spilled VGPRs: %d\n"
344 "Code Size: %d bytes\n"
346 "Scratch: %d bytes per wave\n"
348 "********************\n\n\n",
349 conf
->num_sgprs
, conf
->num_vgprs
,
350 conf
->spilled_sgprs
, conf
->spilled_vgprs
, var
->code_size
,
351 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
356 void radv_shader_variant_destroy(struct radv_device
*device
,
357 struct radv_shader_variant
*variant
)
359 if (__sync_fetch_and_sub(&variant
->ref_count
, 1) != 1)
362 device
->ws
->buffer_destroy(variant
->bo
);
366 static void radv_fill_shader_variant(struct radv_device
*device
,
367 struct radv_shader_variant
*variant
,
368 struct ac_shader_binary
*binary
,
369 gl_shader_stage stage
)
371 bool scratch_enabled
= variant
->config
.scratch_bytes_per_wave
> 0;
372 unsigned vgpr_comp_cnt
= 0;
374 if (scratch_enabled
&& !device
->llvm_supports_spill
)
375 radv_finishme("shader scratch support only available with LLVM 4.0");
377 variant
->code_size
= binary
->code_size
;
380 case MESA_SHADER_VERTEX
:
381 case MESA_SHADER_GEOMETRY
:
382 variant
->rsrc2
= S_00B12C_USER_SGPR(variant
->info
.num_user_sgprs
) |
383 S_00B12C_SCRATCH_EN(scratch_enabled
);
384 vgpr_comp_cnt
= variant
->info
.vs
.vgpr_comp_cnt
;
386 case MESA_SHADER_FRAGMENT
:
387 variant
->rsrc2
= S_00B12C_USER_SGPR(variant
->info
.num_user_sgprs
) |
388 S_00B12C_SCRATCH_EN(scratch_enabled
);
390 case MESA_SHADER_COMPUTE
:
391 variant
->rsrc2
= S_00B84C_USER_SGPR(variant
->info
.num_user_sgprs
) |
392 S_00B84C_SCRATCH_EN(scratch_enabled
) |
393 S_00B84C_TGID_X_EN(1) | S_00B84C_TGID_Y_EN(1) |
394 S_00B84C_TGID_Z_EN(1) | S_00B84C_TIDIG_COMP_CNT(2) |
395 S_00B84C_TG_SIZE_EN(1) |
396 S_00B84C_LDS_SIZE(variant
->config
.lds_size
);
399 unreachable("unsupported shader type");
403 variant
->rsrc1
= S_00B848_VGPRS((variant
->config
.num_vgprs
- 1) / 4) |
404 S_00B848_SGPRS((variant
->config
.num_sgprs
- 1) / 8) |
405 S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt
) |
406 S_00B848_DX10_CLAMP(1) |
407 S_00B848_FLOAT_MODE(variant
->config
.float_mode
);
409 variant
->bo
= device
->ws
->buffer_create(device
->ws
, binary
->code_size
, 256,
410 RADEON_DOMAIN_VRAM
, RADEON_FLAG_CPU_ACCESS
);
412 void *ptr
= device
->ws
->buffer_map(variant
->bo
);
413 memcpy(ptr
, binary
->code
, binary
->code_size
);
414 device
->ws
->buffer_unmap(variant
->bo
);
419 static struct radv_shader_variant
*radv_shader_variant_create(struct radv_device
*device
,
420 struct nir_shader
*shader
,
421 struct radv_pipeline_layout
*layout
,
422 const union ac_shader_variant_key
*key
,
424 unsigned *code_size_out
,
427 struct radv_shader_variant
*variant
= calloc(1, sizeof(struct radv_shader_variant
));
428 enum radeon_family chip_family
= device
->physical_device
->rad_info
.family
;
429 LLVMTargetMachineRef tm
;
433 struct ac_nir_compiler_options options
= {0};
434 options
.layout
= layout
;
438 struct ac_shader_binary binary
;
440 options
.unsafe_math
= !!(device
->debug_flags
& RADV_DEBUG_UNSAFE_MATH
);
441 options
.family
= chip_family
;
442 options
.chip_class
= device
->physical_device
->rad_info
.chip_class
;
443 options
.supports_spill
= device
->llvm_supports_spill
;
444 tm
= ac_create_target_machine(chip_family
, options
.supports_spill
);
445 ac_compile_nir_shader(tm
, &binary
, &variant
->config
,
446 &variant
->info
, shader
, &options
, dump
);
447 LLVMDisposeTargetMachine(tm
);
449 radv_fill_shader_variant(device
, variant
, &binary
, shader
->stage
);
452 *code_out
= binary
.code
;
453 *code_size_out
= binary
.code_size
;
458 free(binary
.global_symbol_offsets
);
460 free(binary
.disasm_string
);
461 variant
->ref_count
= 1;
465 static struct radv_shader_variant
*
466 radv_pipeline_create_gs_copy_shader(struct radv_pipeline
*pipeline
,
467 struct nir_shader
*nir
,
469 unsigned *code_size_out
,
472 struct radv_shader_variant
*variant
= calloc(1, sizeof(struct radv_shader_variant
));
473 enum radeon_family chip_family
= pipeline
->device
->physical_device
->rad_info
.family
;
474 LLVMTargetMachineRef tm
;
478 struct ac_nir_compiler_options options
= {0};
479 struct ac_shader_binary binary
;
480 options
.family
= chip_family
;
481 options
.chip_class
= pipeline
->device
->physical_device
->rad_info
.chip_class
;
482 options
.supports_spill
= pipeline
->device
->llvm_supports_spill
;
483 tm
= ac_create_target_machine(chip_family
, options
.supports_spill
);
484 ac_create_gs_copy_shader(tm
, nir
, &binary
, &variant
->config
, &variant
->info
, &options
, dump_shader
);
485 LLVMDisposeTargetMachine(tm
);
487 radv_fill_shader_variant(pipeline
->device
, variant
, &binary
, MESA_SHADER_VERTEX
);
490 *code_out
= binary
.code
;
491 *code_size_out
= binary
.code_size
;
496 free(binary
.global_symbol_offsets
);
498 free(binary
.disasm_string
);
499 variant
->ref_count
= 1;
503 static struct radv_shader_variant
*
504 radv_pipeline_compile(struct radv_pipeline
*pipeline
,
505 struct radv_pipeline_cache
*cache
,
506 struct radv_shader_module
*module
,
507 const char *entrypoint
,
508 gl_shader_stage stage
,
509 const VkSpecializationInfo
*spec_info
,
510 struct radv_pipeline_layout
*layout
,
511 const union ac_shader_variant_key
*key
)
513 unsigned char sha1
[20];
514 unsigned char gs_copy_sha1
[20];
515 struct radv_shader_variant
*variant
;
518 unsigned code_size
= 0;
519 bool dump
= (pipeline
->device
->debug_flags
& RADV_DEBUG_DUMP_SHADERS
);
522 _mesa_sha1_compute(module
->nir
->info
->name
,
523 strlen(module
->nir
->info
->name
),
526 radv_hash_shader(sha1
, module
, entrypoint
, spec_info
, layout
, key
, 0);
527 if (stage
== MESA_SHADER_GEOMETRY
)
528 radv_hash_shader(gs_copy_sha1
, module
, entrypoint
, spec_info
,
531 variant
= radv_create_shader_variant_from_pipeline_cache(pipeline
->device
,
535 if (stage
== MESA_SHADER_GEOMETRY
) {
536 pipeline
->gs_copy_shader
=
537 radv_create_shader_variant_from_pipeline_cache(
544 (stage
!= MESA_SHADER_GEOMETRY
|| pipeline
->gs_copy_shader
))
547 nir
= radv_shader_compile_to_nir(pipeline
->device
,
548 module
, entrypoint
, stage
,
554 variant
= radv_shader_variant_create(pipeline
->device
, nir
,
559 if (stage
== MESA_SHADER_GEOMETRY
&& !pipeline
->gs_copy_shader
) {
560 void *gs_copy_code
= NULL
;
561 unsigned gs_copy_code_size
= 0;
562 pipeline
->gs_copy_shader
= radv_pipeline_create_gs_copy_shader(
563 pipeline
, nir
, &gs_copy_code
, &gs_copy_code_size
, dump
);
565 if (pipeline
->gs_copy_shader
) {
566 pipeline
->gs_copy_shader
=
567 radv_pipeline_cache_insert_shader(cache
,
569 pipeline
->gs_copy_shader
,
578 variant
= radv_pipeline_cache_insert_shader(cache
, sha1
, variant
,
587 radv_pipeline_scratch_init(struct radv_device
*device
,
588 struct radv_pipeline
*pipeline
)
590 unsigned scratch_bytes_per_wave
= 0;
591 unsigned max_waves
= 0;
592 unsigned min_waves
= 1;
594 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
595 if (pipeline
->shaders
[i
]) {
596 unsigned max_stage_waves
= device
->scratch_waves
;
598 scratch_bytes_per_wave
= MAX2(scratch_bytes_per_wave
,
599 pipeline
->shaders
[i
]->config
.scratch_bytes_per_wave
);
601 max_stage_waves
= MIN2(max_stage_waves
,
602 4 * device
->physical_device
->rad_info
.num_good_compute_units
*
603 (256 / pipeline
->shaders
[i
]->config
.num_vgprs
));
604 max_waves
= MAX2(max_waves
, max_stage_waves
);
608 if (pipeline
->shaders
[MESA_SHADER_COMPUTE
]) {
609 unsigned group_size
= pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[0] *
610 pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[1] *
611 pipeline
->shaders
[MESA_SHADER_COMPUTE
]->info
.cs
.block_size
[2];
612 min_waves
= MAX2(min_waves
, round_up_u32(group_size
, 64));
615 if (scratch_bytes_per_wave
)
616 max_waves
= MIN2(max_waves
, 0xffffffffu
/ scratch_bytes_per_wave
);
618 if (scratch_bytes_per_wave
&& max_waves
< min_waves
) {
619 /* Not really true at this moment, but will be true on first
620 * execution. Avoid having hanging shaders. */
621 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
623 pipeline
->scratch_bytes_per_wave
= scratch_bytes_per_wave
;
624 pipeline
->max_waves
= max_waves
;
628 static uint32_t si_translate_blend_function(VkBlendOp op
)
631 case VK_BLEND_OP_ADD
:
632 return V_028780_COMB_DST_PLUS_SRC
;
633 case VK_BLEND_OP_SUBTRACT
:
634 return V_028780_COMB_SRC_MINUS_DST
;
635 case VK_BLEND_OP_REVERSE_SUBTRACT
:
636 return V_028780_COMB_DST_MINUS_SRC
;
637 case VK_BLEND_OP_MIN
:
638 return V_028780_COMB_MIN_DST_SRC
;
639 case VK_BLEND_OP_MAX
:
640 return V_028780_COMB_MAX_DST_SRC
;
646 static uint32_t si_translate_blend_factor(VkBlendFactor factor
)
649 case VK_BLEND_FACTOR_ZERO
:
650 return V_028780_BLEND_ZERO
;
651 case VK_BLEND_FACTOR_ONE
:
652 return V_028780_BLEND_ONE
;
653 case VK_BLEND_FACTOR_SRC_COLOR
:
654 return V_028780_BLEND_SRC_COLOR
;
655 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR
:
656 return V_028780_BLEND_ONE_MINUS_SRC_COLOR
;
657 case VK_BLEND_FACTOR_DST_COLOR
:
658 return V_028780_BLEND_DST_COLOR
;
659 case VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR
:
660 return V_028780_BLEND_ONE_MINUS_DST_COLOR
;
661 case VK_BLEND_FACTOR_SRC_ALPHA
:
662 return V_028780_BLEND_SRC_ALPHA
;
663 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
:
664 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA
;
665 case VK_BLEND_FACTOR_DST_ALPHA
:
666 return V_028780_BLEND_DST_ALPHA
;
667 case VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA
:
668 return V_028780_BLEND_ONE_MINUS_DST_ALPHA
;
669 case VK_BLEND_FACTOR_CONSTANT_COLOR
:
670 return V_028780_BLEND_CONSTANT_COLOR
;
671 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
:
672 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR
;
673 case VK_BLEND_FACTOR_CONSTANT_ALPHA
:
674 return V_028780_BLEND_CONSTANT_ALPHA
;
675 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
:
676 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA
;
677 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
:
678 return V_028780_BLEND_SRC_ALPHA_SATURATE
;
679 case VK_BLEND_FACTOR_SRC1_COLOR
:
680 return V_028780_BLEND_SRC1_COLOR
;
681 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR
:
682 return V_028780_BLEND_INV_SRC1_COLOR
;
683 case VK_BLEND_FACTOR_SRC1_ALPHA
:
684 return V_028780_BLEND_SRC1_ALPHA
;
685 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
:
686 return V_028780_BLEND_INV_SRC1_ALPHA
;
692 static bool is_dual_src(VkBlendFactor factor
)
695 case VK_BLEND_FACTOR_SRC1_COLOR
:
696 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR
:
697 case VK_BLEND_FACTOR_SRC1_ALPHA
:
698 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
:
705 static unsigned si_choose_spi_color_format(VkFormat vk_format
,
707 bool blend_need_alpha
)
709 const struct vk_format_description
*desc
= vk_format_description(vk_format
);
710 unsigned format
, ntype
, swap
;
712 /* Alpha is needed for alpha-to-coverage.
713 * Blending may be with or without alpha.
715 unsigned normal
= 0; /* most optimal, may not support blending or export alpha */
716 unsigned alpha
= 0; /* exports alpha, but may not support blending */
717 unsigned blend
= 0; /* supports blending, but may not export alpha */
718 unsigned blend_alpha
= 0; /* least optimal, supports blending and exports alpha */
720 format
= radv_translate_colorformat(vk_format
);
721 ntype
= radv_translate_color_numformat(vk_format
, desc
,
722 vk_format_get_first_non_void_channel(vk_format
));
723 swap
= radv_translate_colorswap(vk_format
, false);
725 /* Choose the SPI color formats. These are required values for Stoney/RB+.
726 * Other chips have multiple choices, though they are not necessarily better.
729 case V_028C70_COLOR_5_6_5
:
730 case V_028C70_COLOR_1_5_5_5
:
731 case V_028C70_COLOR_5_5_5_1
:
732 case V_028C70_COLOR_4_4_4_4
:
733 case V_028C70_COLOR_10_11_11
:
734 case V_028C70_COLOR_11_11_10
:
735 case V_028C70_COLOR_8
:
736 case V_028C70_COLOR_8_8
:
737 case V_028C70_COLOR_8_8_8_8
:
738 case V_028C70_COLOR_10_10_10_2
:
739 case V_028C70_COLOR_2_10_10_10
:
740 if (ntype
== V_028C70_NUMBER_UINT
)
741 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_UINT16_ABGR
;
742 else if (ntype
== V_028C70_NUMBER_SINT
)
743 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_SINT16_ABGR
;
745 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_FP16_ABGR
;
748 case V_028C70_COLOR_16
:
749 case V_028C70_COLOR_16_16
:
750 case V_028C70_COLOR_16_16_16_16
:
751 if (ntype
== V_028C70_NUMBER_UNORM
||
752 ntype
== V_028C70_NUMBER_SNORM
) {
753 /* UNORM16 and SNORM16 don't support blending */
754 if (ntype
== V_028C70_NUMBER_UNORM
)
755 normal
= alpha
= V_028714_SPI_SHADER_UNORM16_ABGR
;
757 normal
= alpha
= V_028714_SPI_SHADER_SNORM16_ABGR
;
759 /* Use 32 bits per channel for blending. */
760 if (format
== V_028C70_COLOR_16
) {
761 if (swap
== V_028C70_SWAP_STD
) { /* R */
762 blend
= V_028714_SPI_SHADER_32_R
;
763 blend_alpha
= V_028714_SPI_SHADER_32_AR
;
764 } else if (swap
== V_028C70_SWAP_ALT_REV
) /* A */
765 blend
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
768 } else if (format
== V_028C70_COLOR_16_16
) {
769 if (swap
== V_028C70_SWAP_STD
) { /* RG */
770 blend
= V_028714_SPI_SHADER_32_GR
;
771 blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
772 } else if (swap
== V_028C70_SWAP_ALT
) /* RA */
773 blend
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
776 } else /* 16_16_16_16 */
777 blend
= blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
778 } else if (ntype
== V_028C70_NUMBER_UINT
)
779 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_UINT16_ABGR
;
780 else if (ntype
== V_028C70_NUMBER_SINT
)
781 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_SINT16_ABGR
;
782 else if (ntype
== V_028C70_NUMBER_FLOAT
)
783 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_FP16_ABGR
;
788 case V_028C70_COLOR_32
:
789 if (swap
== V_028C70_SWAP_STD
) { /* R */
790 blend
= normal
= V_028714_SPI_SHADER_32_R
;
791 alpha
= blend_alpha
= V_028714_SPI_SHADER_32_AR
;
792 } else if (swap
== V_028C70_SWAP_ALT_REV
) /* A */
793 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_AR
;
798 case V_028C70_COLOR_32_32
:
799 if (swap
== V_028C70_SWAP_STD
) { /* RG */
800 blend
= normal
= V_028714_SPI_SHADER_32_GR
;
801 alpha
= blend_alpha
= V_028714_SPI_SHADER_32_ABGR
;
802 } else if (swap
== V_028C70_SWAP_ALT
) /* RA */
803 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_AR
;
808 case V_028C70_COLOR_32_32_32_32
:
809 case V_028C70_COLOR_8_24
:
810 case V_028C70_COLOR_24_8
:
811 case V_028C70_COLOR_X24_8_32_FLOAT
:
812 alpha
= blend
= blend_alpha
= normal
= V_028714_SPI_SHADER_32_ABGR
;
816 unreachable("unhandled blend format");
819 if (blend_enable
&& blend_need_alpha
)
821 else if(blend_need_alpha
)
823 else if(blend_enable
)
829 static unsigned si_get_cb_shader_mask(unsigned spi_shader_col_format
)
831 unsigned i
, cb_shader_mask
= 0;
833 for (i
= 0; i
< 8; i
++) {
834 switch ((spi_shader_col_format
>> (i
* 4)) & 0xf) {
835 case V_028714_SPI_SHADER_ZERO
:
837 case V_028714_SPI_SHADER_32_R
:
838 cb_shader_mask
|= 0x1 << (i
* 4);
840 case V_028714_SPI_SHADER_32_GR
:
841 cb_shader_mask
|= 0x3 << (i
* 4);
843 case V_028714_SPI_SHADER_32_AR
:
844 cb_shader_mask
|= 0x9 << (i
* 4);
846 case V_028714_SPI_SHADER_FP16_ABGR
:
847 case V_028714_SPI_SHADER_UNORM16_ABGR
:
848 case V_028714_SPI_SHADER_SNORM16_ABGR
:
849 case V_028714_SPI_SHADER_UINT16_ABGR
:
850 case V_028714_SPI_SHADER_SINT16_ABGR
:
851 case V_028714_SPI_SHADER_32_ABGR
:
852 cb_shader_mask
|= 0xf << (i
* 4);
858 return cb_shader_mask
;
862 radv_pipeline_compute_spi_color_formats(struct radv_pipeline
*pipeline
,
863 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
864 uint32_t blend_enable
,
865 uint32_t blend_need_alpha
,
866 bool single_cb_enable
,
867 bool blend_mrt0_is_dual_src
)
869 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
870 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
871 struct radv_blend_state
*blend
= &pipeline
->graphics
.blend
;
872 unsigned col_format
= 0;
874 for (unsigned i
= 0; i
< (single_cb_enable
? 1 : subpass
->color_count
); ++i
) {
875 struct radv_render_pass_attachment
*attachment
;
878 attachment
= pass
->attachments
+ subpass
->color_attachments
[i
].attachment
;
880 cf
= si_choose_spi_color_format(attachment
->format
,
881 blend_enable
& (1 << i
),
882 blend_need_alpha
& (1 << i
));
884 col_format
|= cf
<< (4 * i
);
887 blend
->cb_shader_mask
= si_get_cb_shader_mask(col_format
);
889 if (blend_mrt0_is_dual_src
)
890 col_format
|= (col_format
& 0xf) << 4;
892 col_format
|= V_028714_SPI_SHADER_32_R
;
893 blend
->spi_shader_col_format
= col_format
;
897 format_is_int8(VkFormat format
)
899 const struct vk_format_description
*desc
= vk_format_description(format
);
900 int channel
= vk_format_get_first_non_void_channel(format
);
902 return channel
>= 0 && desc
->channel
[channel
].pure_integer
&&
903 desc
->channel
[channel
].size
== 8;
906 unsigned radv_format_meta_fs_key(VkFormat format
)
908 unsigned col_format
= si_choose_spi_color_format(format
, false, false) - 1;
909 bool is_int8
= format_is_int8(format
);
911 return col_format
+ (is_int8
? 3 : 0);
915 radv_pipeline_compute_is_int8(const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
917 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
918 struct radv_subpass
*subpass
= pass
->subpasses
+ pCreateInfo
->subpass
;
919 unsigned is_int8
= 0;
921 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
922 struct radv_render_pass_attachment
*attachment
;
924 attachment
= pass
->attachments
+ subpass
->color_attachments
[i
].attachment
;
926 if (format_is_int8(attachment
->format
))
934 radv_pipeline_init_blend_state(struct radv_pipeline
*pipeline
,
935 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
936 const struct radv_graphics_pipeline_create_info
*extra
)
938 const VkPipelineColorBlendStateCreateInfo
*vkblend
= pCreateInfo
->pColorBlendState
;
939 struct radv_blend_state
*blend
= &pipeline
->graphics
.blend
;
940 unsigned mode
= V_028808_CB_NORMAL
;
941 uint32_t blend_enable
= 0, blend_need_alpha
= 0;
942 bool blend_mrt0_is_dual_src
= false;
944 bool single_cb_enable
= false;
949 if (extra
&& extra
->custom_blend_mode
) {
950 single_cb_enable
= true;
951 mode
= extra
->custom_blend_mode
;
953 blend
->cb_color_control
= 0;
954 if (vkblend
->logicOpEnable
)
955 blend
->cb_color_control
|= S_028808_ROP3(vkblend
->logicOp
| (vkblend
->logicOp
<< 4));
957 blend
->cb_color_control
|= S_028808_ROP3(0xcc);
959 blend
->db_alpha_to_mask
= S_028B70_ALPHA_TO_MASK_OFFSET0(2) |
960 S_028B70_ALPHA_TO_MASK_OFFSET1(2) |
961 S_028B70_ALPHA_TO_MASK_OFFSET2(2) |
962 S_028B70_ALPHA_TO_MASK_OFFSET3(2);
964 blend
->cb_target_mask
= 0;
965 for (i
= 0; i
< vkblend
->attachmentCount
; i
++) {
966 const VkPipelineColorBlendAttachmentState
*att
= &vkblend
->pAttachments
[i
];
967 unsigned blend_cntl
= 0;
968 VkBlendOp eqRGB
= att
->colorBlendOp
;
969 VkBlendFactor srcRGB
= att
->srcColorBlendFactor
;
970 VkBlendFactor dstRGB
= att
->dstColorBlendFactor
;
971 VkBlendOp eqA
= att
->alphaBlendOp
;
972 VkBlendFactor srcA
= att
->srcAlphaBlendFactor
;
973 VkBlendFactor dstA
= att
->dstAlphaBlendFactor
;
975 blend
->sx_mrt0_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
);
977 if (!att
->colorWriteMask
)
980 blend
->cb_target_mask
|= (unsigned)att
->colorWriteMask
<< (4 * i
);
981 if (!att
->blendEnable
) {
982 blend
->cb_blend_control
[i
] = blend_cntl
;
986 if (is_dual_src(srcRGB
) || is_dual_src(dstRGB
) || is_dual_src(srcA
) || is_dual_src(dstA
))
988 blend_mrt0_is_dual_src
= true;
990 if (eqRGB
== VK_BLEND_OP_MIN
|| eqRGB
== VK_BLEND_OP_MAX
) {
991 srcRGB
= VK_BLEND_FACTOR_ONE
;
992 dstRGB
= VK_BLEND_FACTOR_ONE
;
994 if (eqA
== VK_BLEND_OP_MIN
|| eqA
== VK_BLEND_OP_MAX
) {
995 srcA
= VK_BLEND_FACTOR_ONE
;
996 dstA
= VK_BLEND_FACTOR_ONE
;
999 blend_cntl
|= S_028780_ENABLE(1);
1001 blend_cntl
|= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB
));
1002 blend_cntl
|= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB
));
1003 blend_cntl
|= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB
));
1004 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
1005 blend_cntl
|= S_028780_SEPARATE_ALPHA_BLEND(1);
1006 blend_cntl
|= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA
));
1007 blend_cntl
|= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA
));
1008 blend_cntl
|= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA
));
1010 blend
->cb_blend_control
[i
] = blend_cntl
;
1012 blend_enable
|= 1 << i
;
1014 if (srcRGB
== VK_BLEND_FACTOR_SRC_ALPHA
||
1015 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA
||
1016 srcRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
||
1017 dstRGB
== VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
||
1018 srcRGB
== VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
||
1019 dstRGB
== VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
)
1020 blend_need_alpha
|= 1 << i
;
1022 for (i
= vkblend
->attachmentCount
; i
< 8; i
++)
1023 blend
->cb_blend_control
[i
] = 0;
1025 if (blend
->cb_target_mask
)
1026 blend
->cb_color_control
|= S_028808_MODE(mode
);
1028 blend
->cb_color_control
|= S_028808_MODE(V_028808_CB_DISABLE
);
1030 radv_pipeline_compute_spi_color_formats(pipeline
, pCreateInfo
,
1031 blend_enable
, blend_need_alpha
, single_cb_enable
, blend_mrt0_is_dual_src
);
1034 static uint32_t si_translate_stencil_op(enum VkStencilOp op
)
1037 case VK_STENCIL_OP_KEEP
:
1038 return V_02842C_STENCIL_KEEP
;
1039 case VK_STENCIL_OP_ZERO
:
1040 return V_02842C_STENCIL_ZERO
;
1041 case VK_STENCIL_OP_REPLACE
:
1042 return V_02842C_STENCIL_REPLACE_TEST
;
1043 case VK_STENCIL_OP_INCREMENT_AND_CLAMP
:
1044 return V_02842C_STENCIL_ADD_CLAMP
;
1045 case VK_STENCIL_OP_DECREMENT_AND_CLAMP
:
1046 return V_02842C_STENCIL_SUB_CLAMP
;
1047 case VK_STENCIL_OP_INVERT
:
1048 return V_02842C_STENCIL_INVERT
;
1049 case VK_STENCIL_OP_INCREMENT_AND_WRAP
:
1050 return V_02842C_STENCIL_ADD_WRAP
;
1051 case VK_STENCIL_OP_DECREMENT_AND_WRAP
:
1052 return V_02842C_STENCIL_SUB_WRAP
;
1058 radv_pipeline_init_depth_stencil_state(struct radv_pipeline
*pipeline
,
1059 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1060 const struct radv_graphics_pipeline_create_info
*extra
)
1062 const VkPipelineDepthStencilStateCreateInfo
*vkds
= pCreateInfo
->pDepthStencilState
;
1063 struct radv_depth_stencil_state
*ds
= &pipeline
->graphics
.ds
;
1065 memset(ds
, 0, sizeof(*ds
));
1068 ds
->db_depth_control
= S_028800_Z_ENABLE(vkds
->depthTestEnable
? 1 : 0) |
1069 S_028800_Z_WRITE_ENABLE(vkds
->depthWriteEnable
? 1 : 0) |
1070 S_028800_ZFUNC(vkds
->depthCompareOp
) |
1071 S_028800_DEPTH_BOUNDS_ENABLE(vkds
->depthBoundsTestEnable
? 1 : 0);
1073 if (vkds
->stencilTestEnable
) {
1074 ds
->db_depth_control
|= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);
1075 ds
->db_depth_control
|= S_028800_STENCILFUNC(vkds
->front
.compareOp
);
1076 ds
->db_stencil_control
|= S_02842C_STENCILFAIL(si_translate_stencil_op(vkds
->front
.failOp
));
1077 ds
->db_stencil_control
|= S_02842C_STENCILZPASS(si_translate_stencil_op(vkds
->front
.passOp
));
1078 ds
->db_stencil_control
|= S_02842C_STENCILZFAIL(si_translate_stencil_op(vkds
->front
.depthFailOp
));
1080 ds
->db_depth_control
|= S_028800_STENCILFUNC_BF(vkds
->back
.compareOp
);
1081 ds
->db_stencil_control
|= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(vkds
->back
.failOp
));
1082 ds
->db_stencil_control
|= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(vkds
->back
.passOp
));
1083 ds
->db_stencil_control
|= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(vkds
->back
.depthFailOp
));
1088 ds
->db_render_control
|= S_028000_DEPTH_CLEAR_ENABLE(extra
->db_depth_clear
);
1089 ds
->db_render_control
|= S_028000_STENCIL_CLEAR_ENABLE(extra
->db_stencil_clear
);
1091 ds
->db_render_control
|= S_028000_RESUMMARIZE_ENABLE(extra
->db_resummarize
);
1092 ds
->db_render_control
|= S_028000_DEPTH_COMPRESS_DISABLE(extra
->db_flush_depth_inplace
);
1093 ds
->db_render_control
|= S_028000_STENCIL_COMPRESS_DISABLE(extra
->db_flush_stencil_inplace
);
1094 ds
->db_render_override2
|= S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra
->db_depth_disable_expclear
);
1095 ds
->db_render_override2
|= S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra
->db_stencil_disable_expclear
);
1099 static uint32_t si_translate_fill(VkPolygonMode func
)
1102 case VK_POLYGON_MODE_FILL
:
1103 return V_028814_X_DRAW_TRIANGLES
;
1104 case VK_POLYGON_MODE_LINE
:
1105 return V_028814_X_DRAW_LINES
;
1106 case VK_POLYGON_MODE_POINT
:
1107 return V_028814_X_DRAW_POINTS
;
1110 return V_028814_X_DRAW_POINTS
;
1114 radv_pipeline_init_raster_state(struct radv_pipeline
*pipeline
,
1115 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1117 const VkPipelineRasterizationStateCreateInfo
*vkraster
= pCreateInfo
->pRasterizationState
;
1118 struct radv_raster_state
*raster
= &pipeline
->graphics
.raster
;
1120 memset(raster
, 0, sizeof(*raster
));
1122 raster
->spi_interp_control
=
1123 S_0286D4_FLAT_SHADE_ENA(1) |
1124 S_0286D4_PNT_SPRITE_ENA(1) |
1125 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S
) |
1126 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T
) |
1127 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0
) |
1128 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1
) |
1129 S_0286D4_PNT_SPRITE_TOP_1(0); // vulkan is top to bottom - 1.0 at bottom
1131 raster
->pa_cl_vs_out_cntl
= S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(1);
1132 raster
->pa_cl_clip_cntl
= S_028810_PS_UCP_MODE(3) |
1133 S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.
1134 S_028810_ZCLIP_NEAR_DISABLE(vkraster
->depthClampEnable
? 1 : 0) |
1135 S_028810_ZCLIP_FAR_DISABLE(vkraster
->depthClampEnable
? 1 : 0) |
1136 S_028810_DX_RASTERIZATION_KILL(vkraster
->rasterizerDiscardEnable
? 1 : 0) |
1137 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
1139 raster
->pa_su_vtx_cntl
=
1140 S_028BE4_PIX_CENTER(1) | // TODO verify
1141 S_028BE4_ROUND_MODE(V_028BE4_X_ROUND_TO_EVEN
) |
1142 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH
);
1144 raster
->pa_su_sc_mode_cntl
=
1145 S_028814_FACE(vkraster
->frontFace
) |
1146 S_028814_CULL_FRONT(!!(vkraster
->cullMode
& VK_CULL_MODE_FRONT_BIT
)) |
1147 S_028814_CULL_BACK(!!(vkraster
->cullMode
& VK_CULL_MODE_BACK_BIT
)) |
1148 S_028814_POLY_MODE(vkraster
->polygonMode
!= VK_POLYGON_MODE_FILL
) |
1149 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(vkraster
->polygonMode
)) |
1150 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(vkraster
->polygonMode
)) |
1151 S_028814_POLY_OFFSET_FRONT_ENABLE(vkraster
->depthBiasEnable
? 1 : 0) |
1152 S_028814_POLY_OFFSET_BACK_ENABLE(vkraster
->depthBiasEnable
? 1 : 0) |
1153 S_028814_POLY_OFFSET_PARA_ENABLE(vkraster
->depthBiasEnable
? 1 : 0);
1158 radv_pipeline_init_multisample_state(struct radv_pipeline
*pipeline
,
1159 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1161 const VkPipelineMultisampleStateCreateInfo
*vkms
= pCreateInfo
->pMultisampleState
;
1162 struct radv_blend_state
*blend
= &pipeline
->graphics
.blend
;
1163 struct radv_multisample_state
*ms
= &pipeline
->graphics
.ms
;
1164 unsigned num_tile_pipes
= pipeline
->device
->physical_device
->rad_info
.num_tile_pipes
;
1165 int ps_iter_samples
= 1;
1166 uint32_t mask
= 0xffff;
1168 ms
->num_samples
= vkms
->rasterizationSamples
;
1170 if (pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.fs
.force_persample
) {
1171 ps_iter_samples
= vkms
->rasterizationSamples
;
1174 ms
->pa_sc_line_cntl
= S_028BDC_DX10_DIAMOND_TEST_ENA(1);
1175 ms
->pa_sc_aa_config
= 0;
1176 ms
->db_eqaa
= S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
1177 S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
1178 ms
->pa_sc_mode_cntl_1
=
1179 S_028A4C_WALK_FENCE_ENABLE(1) | //TODO linear dst fixes
1180 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes
== 2 ? 2 : 3) |
1182 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
1183 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
1184 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
1185 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
1186 EG_S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
1187 EG_S_028A4C_FORCE_EOV_REZ_ENABLE(1);
1189 if (vkms
->rasterizationSamples
> 1) {
1190 unsigned log_samples
= util_logbase2(vkms
->rasterizationSamples
);
1191 unsigned log_ps_iter_samples
= util_logbase2(util_next_power_of_two(ps_iter_samples
));
1192 ms
->pa_sc_mode_cntl_0
= S_028A48_MSAA_ENABLE(1);
1193 ms
->pa_sc_line_cntl
|= S_028BDC_EXPAND_LINE_WIDTH(1); /* CM_R_028BDC_PA_SC_LINE_CNTL */
1194 ms
->db_eqaa
|= S_028804_MAX_ANCHOR_SAMPLES(log_samples
) |
1195 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples
) |
1196 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples
) |
1197 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples
);
1198 ms
->pa_sc_aa_config
|= S_028BE0_MSAA_NUM_SAMPLES(log_samples
) |
1199 S_028BE0_MAX_SAMPLE_DIST(radv_cayman_get_maxdist(log_samples
)) |
1200 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples
); /* CM_R_028BE0_PA_SC_AA_CONFIG */
1201 ms
->pa_sc_mode_cntl_1
|= EG_S_028A4C_PS_ITER_SAMPLE(ps_iter_samples
> 1);
1204 if (vkms
->alphaToCoverageEnable
)
1205 blend
->db_alpha_to_mask
|= S_028B70_ALPHA_TO_MASK_ENABLE(1);
1207 if (vkms
->pSampleMask
) {
1208 mask
= vkms
->pSampleMask
[0] & 0xffff;
1211 ms
->pa_sc_aa_mask
[0] = mask
| (mask
<< 16);
1212 ms
->pa_sc_aa_mask
[1] = mask
| (mask
<< 16);
1216 si_translate_prim(enum VkPrimitiveTopology topology
)
1219 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST
:
1220 return V_008958_DI_PT_POINTLIST
;
1221 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST
:
1222 return V_008958_DI_PT_LINELIST
;
1223 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
:
1224 return V_008958_DI_PT_LINESTRIP
;
1225 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
:
1226 return V_008958_DI_PT_TRILIST
;
1227 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
:
1228 return V_008958_DI_PT_TRISTRIP
;
1229 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
:
1230 return V_008958_DI_PT_TRIFAN
;
1231 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
1232 return V_008958_DI_PT_LINELIST_ADJ
;
1233 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
1234 return V_008958_DI_PT_LINESTRIP_ADJ
;
1235 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
1236 return V_008958_DI_PT_TRILIST_ADJ
;
1237 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
1238 return V_008958_DI_PT_TRISTRIP_ADJ
;
1239 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
:
1240 return V_008958_DI_PT_PATCH
;
1248 si_conv_gl_prim_to_gs_out(unsigned gl_prim
)
1251 case 0: /* GL_POINTS */
1252 return V_028A6C_OUTPRIM_TYPE_POINTLIST
;
1253 case 1: /* GL_LINES */
1254 case 3: /* GL_LINE_STRIP */
1255 case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */
1256 case 0x8E7A: /* GL_ISOLINES */
1257 return V_028A6C_OUTPRIM_TYPE_LINESTRIP
;
1259 case 4: /* GL_TRIANGLES */
1260 case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */
1261 case 5: /* GL_TRIANGLE_STRIP */
1262 case 7: /* GL_QUADS */
1263 return V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
1271 si_conv_prim_to_gs_out(enum VkPrimitiveTopology topology
)
1274 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST
:
1275 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
:
1276 return V_028A6C_OUTPRIM_TYPE_POINTLIST
;
1277 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST
:
1278 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
:
1279 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
:
1280 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
:
1281 return V_028A6C_OUTPRIM_TYPE_LINESTRIP
;
1282 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
:
1283 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
:
1284 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
:
1285 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
:
1286 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
:
1287 return V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
1294 static unsigned si_map_swizzle(unsigned swizzle
)
1298 return V_008F0C_SQ_SEL_Y
;
1300 return V_008F0C_SQ_SEL_Z
;
1302 return V_008F0C_SQ_SEL_W
;
1304 return V_008F0C_SQ_SEL_0
;
1306 return V_008F0C_SQ_SEL_1
;
1307 default: /* VK_SWIZZLE_X */
1308 return V_008F0C_SQ_SEL_X
;
1313 radv_pipeline_init_dynamic_state(struct radv_pipeline
*pipeline
,
1314 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1316 radv_cmd_dirty_mask_t states
= RADV_CMD_DIRTY_DYNAMIC_ALL
;
1317 RADV_FROM_HANDLE(radv_render_pass
, pass
, pCreateInfo
->renderPass
);
1318 struct radv_subpass
*subpass
= &pass
->subpasses
[pCreateInfo
->subpass
];
1320 pipeline
->dynamic_state
= default_dynamic_state
;
1322 if (pCreateInfo
->pDynamicState
) {
1323 /* Remove all of the states that are marked as dynamic */
1324 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
1325 for (uint32_t s
= 0; s
< count
; s
++)
1326 states
&= ~(1 << pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
1329 struct radv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
1331 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1333 * pViewportState is [...] NULL if the pipeline
1334 * has rasterization disabled.
1336 if (!pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
) {
1337 assert(pCreateInfo
->pViewportState
);
1339 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
1340 if (states
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
1341 typed_memcpy(dynamic
->viewport
.viewports
,
1342 pCreateInfo
->pViewportState
->pViewports
,
1343 pCreateInfo
->pViewportState
->viewportCount
);
1346 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
1347 if (states
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
1348 typed_memcpy(dynamic
->scissor
.scissors
,
1349 pCreateInfo
->pViewportState
->pScissors
,
1350 pCreateInfo
->pViewportState
->scissorCount
);
1354 if (states
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
)) {
1355 assert(pCreateInfo
->pRasterizationState
);
1356 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
1359 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
)) {
1360 assert(pCreateInfo
->pRasterizationState
);
1361 dynamic
->depth_bias
.bias
=
1362 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
1363 dynamic
->depth_bias
.clamp
=
1364 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
1365 dynamic
->depth_bias
.slope
=
1366 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
1369 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1371 * pColorBlendState is [...] NULL if the pipeline has rasterization
1372 * disabled or if the subpass of the render pass the pipeline is
1373 * created against does not use any color attachments.
1375 bool uses_color_att
= false;
1376 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
1377 if (subpass
->color_attachments
[i
].attachment
!= VK_ATTACHMENT_UNUSED
) {
1378 uses_color_att
= true;
1383 if (uses_color_att
&& states
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
)) {
1384 assert(pCreateInfo
->pColorBlendState
);
1385 typed_memcpy(dynamic
->blend_constants
,
1386 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
1389 /* If there is no depthstencil attachment, then don't read
1390 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1391 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1392 * no need to override the depthstencil defaults in
1393 * radv_pipeline::dynamic_state when there is no depthstencil attachment.
1395 * Section 9.2 of the Vulkan 1.0.15 spec says:
1397 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1398 * disabled or if the subpass of the render pass the pipeline is created
1399 * against does not use a depth/stencil attachment.
1401 if (!pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
&&
1402 subpass
->depth_stencil_attachment
.attachment
!= VK_ATTACHMENT_UNUSED
) {
1403 assert(pCreateInfo
->pDepthStencilState
);
1405 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
)) {
1406 dynamic
->depth_bounds
.min
=
1407 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
1408 dynamic
->depth_bounds
.max
=
1409 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
1412 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
)) {
1413 dynamic
->stencil_compare_mask
.front
=
1414 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
1415 dynamic
->stencil_compare_mask
.back
=
1416 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
1419 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
)) {
1420 dynamic
->stencil_write_mask
.front
=
1421 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
1422 dynamic
->stencil_write_mask
.back
=
1423 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
1426 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
)) {
1427 dynamic
->stencil_reference
.front
=
1428 pCreateInfo
->pDepthStencilState
->front
.reference
;
1429 dynamic
->stencil_reference
.back
=
1430 pCreateInfo
->pDepthStencilState
->back
.reference
;
1434 pipeline
->dynamic_state_mask
= states
;
1437 static union ac_shader_variant_key
1438 radv_compute_vs_key(const VkGraphicsPipelineCreateInfo
*pCreateInfo
, bool as_es
)
1440 union ac_shader_variant_key key
;
1441 const VkPipelineVertexInputStateCreateInfo
*input_state
=
1442 pCreateInfo
->pVertexInputState
;
1444 memset(&key
, 0, sizeof(key
));
1445 key
.vs
.instance_rate_inputs
= 0;
1446 key
.vs
.as_es
= as_es
;
1448 for (unsigned i
= 0; i
< input_state
->vertexAttributeDescriptionCount
; ++i
) {
1450 binding
= input_state
->pVertexAttributeDescriptions
[i
].binding
;
1451 if (input_state
->pVertexBindingDescriptions
[binding
].inputRate
)
1452 key
.vs
.instance_rate_inputs
|= 1u << input_state
->pVertexAttributeDescriptions
[i
].location
;
1458 calculate_gs_ring_sizes(struct radv_pipeline
*pipeline
)
1460 struct radv_device
*device
= pipeline
->device
;
1461 unsigned num_se
= device
->physical_device
->rad_info
.max_se
;
1462 unsigned wave_size
= 64;
1463 unsigned max_gs_waves
= 32 * num_se
; /* max 32 per SE on GCN */
1464 unsigned gs_vertex_reuse
= 16 * num_se
; /* GS_VERTEX_REUSE register (per SE) */
1465 unsigned alignment
= 256 * num_se
;
1466 /* The maximum size is 63.999 MB per SE. */
1467 unsigned max_size
= ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se
;
1469 struct ac_shader_variant_info
*gs_info
= &pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
;
1470 struct ac_shader_variant_info
*es_info
= &pipeline
->shaders
[MESA_SHADER_VERTEX
]->info
;
1471 /* Calculate the minimum size. */
1472 unsigned min_esgs_ring_size
= align(es_info
->vs
.esgs_itemsize
* gs_vertex_reuse
*
1473 wave_size
, alignment
);
1474 /* These are recommended sizes, not minimum sizes. */
1475 unsigned esgs_ring_size
= max_gs_waves
* 2 * wave_size
*
1476 es_info
->vs
.esgs_itemsize
* gs_info
->gs
.vertices_in
;
1477 unsigned gsvs_ring_size
= max_gs_waves
* 2 * wave_size
*
1478 gs_info
->gs
.max_gsvs_emit_size
* 1; // no streams in VK (gs->max_gs_stream + 1);
1480 min_esgs_ring_size
= align(min_esgs_ring_size
, alignment
);
1481 esgs_ring_size
= align(esgs_ring_size
, alignment
);
1482 gsvs_ring_size
= align(gsvs_ring_size
, alignment
);
1484 pipeline
->graphics
.esgs_ring_size
= CLAMP(esgs_ring_size
, min_esgs_ring_size
, max_size
);
1485 pipeline
->graphics
.gsvs_ring_size
= MIN2(gsvs_ring_size
, max_size
);
1488 static const struct radv_prim_vertex_count prim_size_table
[] = {
1489 [V_008958_DI_PT_NONE
] = {0, 0},
1490 [V_008958_DI_PT_POINTLIST
] = {1, 1},
1491 [V_008958_DI_PT_LINELIST
] = {2, 2},
1492 [V_008958_DI_PT_LINESTRIP
] = {2, 1},
1493 [V_008958_DI_PT_TRILIST
] = {3, 3},
1494 [V_008958_DI_PT_TRIFAN
] = {3, 1},
1495 [V_008958_DI_PT_TRISTRIP
] = {3, 1},
1496 [V_008958_DI_PT_LINELIST_ADJ
] = {4, 4},
1497 [V_008958_DI_PT_LINESTRIP_ADJ
] = {4, 1},
1498 [V_008958_DI_PT_TRILIST_ADJ
] = {6, 6},
1499 [V_008958_DI_PT_TRISTRIP_ADJ
] = {6, 2},
1500 [V_008958_DI_PT_RECTLIST
] = {3, 3},
1501 [V_008958_DI_PT_LINELOOP
] = {2, 1},
1502 [V_008958_DI_PT_POLYGON
] = {3, 1},
1503 [V_008958_DI_PT_2D_TRI_STRIP
] = {0, 0},
1507 radv_pipeline_init(struct radv_pipeline
*pipeline
,
1508 struct radv_device
*device
,
1509 struct radv_pipeline_cache
*cache
,
1510 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1511 const struct radv_graphics_pipeline_create_info
*extra
,
1512 const VkAllocationCallbacks
*alloc
)
1514 struct radv_shader_module fs_m
= {0};
1518 alloc
= &device
->alloc
;
1520 pipeline
->device
= device
;
1521 pipeline
->layout
= radv_pipeline_layout_from_handle(pCreateInfo
->layout
);
1523 radv_pipeline_init_dynamic_state(pipeline
, pCreateInfo
);
1524 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = { 0, };
1525 struct radv_shader_module
*modules
[MESA_SHADER_STAGES
] = { 0, };
1526 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
1527 gl_shader_stage stage
= ffs(pCreateInfo
->pStages
[i
].stage
) - 1;
1528 pStages
[stage
] = &pCreateInfo
->pStages
[i
];
1529 modules
[stage
] = radv_shader_module_from_handle(pStages
[stage
]->module
);
1532 radv_pipeline_init_blend_state(pipeline
, pCreateInfo
, extra
);
1534 if (modules
[MESA_SHADER_VERTEX
]) {
1535 bool as_es
= modules
[MESA_SHADER_GEOMETRY
] != NULL
;
1536 union ac_shader_variant_key key
= radv_compute_vs_key(pCreateInfo
, as_es
);
1538 pipeline
->shaders
[MESA_SHADER_VERTEX
] =
1539 radv_pipeline_compile(pipeline
, cache
, modules
[MESA_SHADER_VERTEX
],
1540 pStages
[MESA_SHADER_VERTEX
]->pName
,
1542 pStages
[MESA_SHADER_VERTEX
]->pSpecializationInfo
,
1543 pipeline
->layout
, &key
);
1545 pipeline
->active_stages
|= mesa_to_vk_shader_stage(MESA_SHADER_VERTEX
);
1548 if (modules
[MESA_SHADER_GEOMETRY
]) {
1549 union ac_shader_variant_key key
= radv_compute_vs_key(pCreateInfo
, false);
1551 pipeline
->shaders
[MESA_SHADER_GEOMETRY
] =
1552 radv_pipeline_compile(pipeline
, cache
, modules
[MESA_SHADER_GEOMETRY
],
1553 pStages
[MESA_SHADER_GEOMETRY
]->pName
,
1554 MESA_SHADER_GEOMETRY
,
1555 pStages
[MESA_SHADER_GEOMETRY
]->pSpecializationInfo
,
1556 pipeline
->layout
, &key
);
1558 pipeline
->active_stages
|= mesa_to_vk_shader_stage(MESA_SHADER_GEOMETRY
);
1559 calculate_gs_ring_sizes(pipeline
);
1562 if (!modules
[MESA_SHADER_FRAGMENT
]) {
1564 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
1565 fs_b
.shader
->info
->name
= ralloc_strdup(fs_b
.shader
, "noop_fs");
1566 fs_m
.nir
= fs_b
.shader
;
1567 modules
[MESA_SHADER_FRAGMENT
] = &fs_m
;
1570 if (modules
[MESA_SHADER_FRAGMENT
]) {
1571 union ac_shader_variant_key key
;
1572 key
.fs
.col_format
= pipeline
->graphics
.blend
.spi_shader_col_format
;
1573 key
.fs
.is_int8
= radv_pipeline_compute_is_int8(pCreateInfo
);
1575 const VkPipelineShaderStageCreateInfo
*stage
= pStages
[MESA_SHADER_FRAGMENT
];
1577 pipeline
->shaders
[MESA_SHADER_FRAGMENT
] =
1578 radv_pipeline_compile(pipeline
, cache
, modules
[MESA_SHADER_FRAGMENT
],
1579 stage
? stage
->pName
: "main",
1580 MESA_SHADER_FRAGMENT
,
1581 stage
? stage
->pSpecializationInfo
: NULL
,
1582 pipeline
->layout
, &key
);
1583 pipeline
->active_stages
|= mesa_to_vk_shader_stage(MESA_SHADER_FRAGMENT
);
1587 ralloc_free(fs_m
.nir
);
1589 radv_pipeline_init_depth_stencil_state(pipeline
, pCreateInfo
, extra
);
1590 radv_pipeline_init_raster_state(pipeline
, pCreateInfo
);
1591 radv_pipeline_init_multisample_state(pipeline
, pCreateInfo
);
1592 pipeline
->graphics
.prim
= si_translate_prim(pCreateInfo
->pInputAssemblyState
->topology
);
1593 if (radv_pipeline_has_gs(pipeline
)) {
1594 pipeline
->graphics
.gs_out
= si_conv_gl_prim_to_gs_out(pipeline
->shaders
[MESA_SHADER_GEOMETRY
]->info
.gs
.output_prim
);
1596 pipeline
->graphics
.gs_out
= si_conv_prim_to_gs_out(pCreateInfo
->pInputAssemblyState
->topology
);
1598 if (extra
&& extra
->use_rectlist
) {
1599 pipeline
->graphics
.prim
= V_008958_DI_PT_RECTLIST
;
1600 pipeline
->graphics
.gs_out
= V_028A6C_OUTPRIM_TYPE_TRISTRIP
;
1602 pipeline
->graphics
.prim_restart_enable
= !!pCreateInfo
->pInputAssemblyState
->primitiveRestartEnable
;
1603 /* prim vertex count will need TESS changes */
1604 pipeline
->graphics
.prim_vertex_count
= prim_size_table
[pipeline
->graphics
.prim
];
1606 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
1607 pCreateInfo
->pVertexInputState
;
1608 for (uint32_t i
= 0; i
< vi_info
->vertexAttributeDescriptionCount
; i
++) {
1609 const VkVertexInputAttributeDescription
*desc
=
1610 &vi_info
->pVertexAttributeDescriptions
[i
];
1611 unsigned loc
= desc
->location
;
1612 const struct vk_format_description
*format_desc
;
1614 uint32_t num_format
, data_format
;
1615 format_desc
= vk_format_description(desc
->format
);
1616 first_non_void
= vk_format_get_first_non_void_channel(desc
->format
);
1618 num_format
= radv_translate_buffer_numformat(format_desc
, first_non_void
);
1619 data_format
= radv_translate_buffer_dataformat(format_desc
, first_non_void
);
1621 pipeline
->va_rsrc_word3
[loc
] = S_008F0C_DST_SEL_X(si_map_swizzle(format_desc
->swizzle
[0])) |
1622 S_008F0C_DST_SEL_Y(si_map_swizzle(format_desc
->swizzle
[1])) |
1623 S_008F0C_DST_SEL_Z(si_map_swizzle(format_desc
->swizzle
[2])) |
1624 S_008F0C_DST_SEL_W(si_map_swizzle(format_desc
->swizzle
[3])) |
1625 S_008F0C_NUM_FORMAT(num_format
) |
1626 S_008F0C_DATA_FORMAT(data_format
);
1627 pipeline
->va_format_size
[loc
] = format_desc
->block
.bits
/ 8;
1628 pipeline
->va_offset
[loc
] = desc
->offset
;
1629 pipeline
->va_binding
[loc
] = desc
->binding
;
1630 pipeline
->num_vertex_attribs
= MAX2(pipeline
->num_vertex_attribs
, loc
+ 1);
1633 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
1634 const VkVertexInputBindingDescription
*desc
=
1635 &vi_info
->pVertexBindingDescriptions
[i
];
1637 pipeline
->binding_stride
[desc
->binding
] = desc
->stride
;
1640 if (device
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
) {
1641 radv_dump_pipeline_stats(device
, pipeline
);
1644 result
= radv_pipeline_scratch_init(device
, pipeline
);
1649 radv_graphics_pipeline_create(
1651 VkPipelineCache _cache
,
1652 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1653 const struct radv_graphics_pipeline_create_info
*extra
,
1654 const VkAllocationCallbacks
*pAllocator
,
1655 VkPipeline
*pPipeline
)
1657 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1658 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
1659 struct radv_pipeline
*pipeline
;
1662 pipeline
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
1663 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1664 if (pipeline
== NULL
)
1665 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1667 memset(pipeline
, 0, sizeof(*pipeline
));
1668 result
= radv_pipeline_init(pipeline
, device
, cache
,
1669 pCreateInfo
, extra
, pAllocator
);
1670 if (result
!= VK_SUCCESS
) {
1671 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
1675 *pPipeline
= radv_pipeline_to_handle(pipeline
);
1680 VkResult
radv_CreateGraphicsPipelines(
1682 VkPipelineCache pipelineCache
,
1684 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
1685 const VkAllocationCallbacks
* pAllocator
,
1686 VkPipeline
* pPipelines
)
1688 VkResult result
= VK_SUCCESS
;
1691 for (; i
< count
; i
++) {
1693 r
= radv_graphics_pipeline_create(_device
,
1696 NULL
, pAllocator
, &pPipelines
[i
]);
1697 if (r
!= VK_SUCCESS
) {
1699 pPipelines
[i
] = VK_NULL_HANDLE
;
1706 static VkResult
radv_compute_pipeline_create(
1708 VkPipelineCache _cache
,
1709 const VkComputePipelineCreateInfo
* pCreateInfo
,
1710 const VkAllocationCallbacks
* pAllocator
,
1711 VkPipeline
* pPipeline
)
1713 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1714 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
1715 RADV_FROM_HANDLE(radv_shader_module
, module
, pCreateInfo
->stage
.module
);
1716 struct radv_pipeline
*pipeline
;
1719 pipeline
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
1720 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1721 if (pipeline
== NULL
)
1722 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1724 memset(pipeline
, 0, sizeof(*pipeline
));
1725 pipeline
->device
= device
;
1726 pipeline
->layout
= radv_pipeline_layout_from_handle(pCreateInfo
->layout
);
1728 pipeline
->shaders
[MESA_SHADER_COMPUTE
] =
1729 radv_pipeline_compile(pipeline
, cache
, module
,
1730 pCreateInfo
->stage
.pName
,
1731 MESA_SHADER_COMPUTE
,
1732 pCreateInfo
->stage
.pSpecializationInfo
,
1733 pipeline
->layout
, NULL
);
1736 result
= radv_pipeline_scratch_init(device
, pipeline
);
1737 if (result
!= VK_SUCCESS
) {
1738 radv_pipeline_destroy(device
, pipeline
, pAllocator
);
1742 *pPipeline
= radv_pipeline_to_handle(pipeline
);
1744 if (device
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
) {
1745 radv_dump_pipeline_stats(device
, pipeline
);
1749 VkResult
radv_CreateComputePipelines(
1751 VkPipelineCache pipelineCache
,
1753 const VkComputePipelineCreateInfo
* pCreateInfos
,
1754 const VkAllocationCallbacks
* pAllocator
,
1755 VkPipeline
* pPipelines
)
1757 VkResult result
= VK_SUCCESS
;
1760 for (; i
< count
; i
++) {
1762 r
= radv_compute_pipeline_create(_device
, pipelineCache
,
1764 pAllocator
, &pPipelines
[i
]);
1765 if (r
!= VK_SUCCESS
) {
1767 pPipelines
[i
] = VK_NULL_HANDLE
;