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
31 #include "ac_binary.h"
32 #include "amd_family.h"
33 #include "radv_constants.h"
36 #include "vulkan/vulkan.h"
40 struct radv_shader_module
{
41 struct nir_shader
*nir
;
42 unsigned char sha1
[20];
48 RADV_ALPHA_ADJUST_NONE
= 0,
49 RADV_ALPHA_ADJUST_SNORM
= 1,
50 RADV_ALPHA_ADJUST_SINT
= 2,
51 RADV_ALPHA_ADJUST_SSCALED
= 3,
54 struct radv_vs_out_key
{
58 uint32_t as_ngg_passthrough
:1;
59 uint32_t export_prim_id
:1;
60 uint32_t export_layer_id
:1;
61 uint32_t export_clip_dists
:1;
64 struct radv_vs_variant_key
{
65 struct radv_vs_out_key out
;
67 uint32_t instance_rate_inputs
;
68 uint32_t instance_rate_divisors
[MAX_VERTEX_ATTRIBS
];
69 uint8_t vertex_attribute_formats
[MAX_VERTEX_ATTRIBS
];
70 uint32_t vertex_attribute_bindings
[MAX_VERTEX_ATTRIBS
];
71 uint32_t vertex_attribute_offsets
[MAX_VERTEX_ATTRIBS
];
72 uint32_t vertex_attribute_strides
[MAX_VERTEX_ATTRIBS
];
74 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
75 * so we may need to fix it up. */
76 uint64_t alpha_adjust
;
78 /* For some formats the channels have to be shuffled. */
79 uint32_t post_shuffle
;
81 /* Output primitive type. */
85 struct radv_tes_variant_key
{
86 struct radv_vs_out_key out
;
89 uint8_t tcs_num_outputs
;
92 struct radv_tcs_variant_key
{
93 struct radv_vs_variant_key vs_key
;
94 unsigned primitive_mode
;
95 unsigned input_vertices
;
97 uint32_t tes_reads_tess_factors
:1;
100 struct radv_fs_variant_key
{
102 uint8_t log2_ps_iter_samples
;
108 struct radv_cs_variant_key
{
109 uint8_t subgroup_size
;
112 struct radv_shader_variant_key
{
114 struct radv_vs_variant_key vs
;
115 struct radv_fs_variant_key fs
;
116 struct radv_tes_variant_key tes
;
117 struct radv_tcs_variant_key tcs
;
118 struct radv_cs_variant_key cs
;
120 /* A common prefix of the vs and tes keys. */
121 struct radv_vs_out_key vs_common_out
;
123 bool has_multiview_view_index
;
126 struct radv_nir_compiler_options
{
127 struct radv_pipeline_layout
*layout
;
128 struct radv_shader_variant_key key
;
129 bool explicit_scratch_args
;
130 bool clamp_shadow_reference
;
131 bool robust_buffer_access
;
137 bool has_ls_vgpr_init_bug
;
138 bool use_ngg_streamout
;
139 enum radeon_family family
;
140 enum chip_class chip_class
;
141 uint32_t tess_offchip_block_dw_size
;
142 uint32_t address32_hi
;
146 AC_UD_SCRATCH_RING_OFFSETS
= 0,
147 AC_UD_PUSH_CONSTANTS
= 1,
148 AC_UD_INLINE_PUSH_CONSTANTS
= 2,
149 AC_UD_INDIRECT_DESCRIPTOR_SETS
= 3,
150 AC_UD_VIEW_INDEX
= 4,
151 AC_UD_STREAMOUT_BUFFERS
= 5,
152 AC_UD_NGG_GS_STATE
= 6,
153 AC_UD_SHADER_START
= 7,
154 AC_UD_VS_VERTEX_BUFFERS
= AC_UD_SHADER_START
,
155 AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
158 AC_UD_CS_GRID_SIZE
= AC_UD_SHADER_START
,
163 AC_UD_MAX_UD
= AC_UD_TCS_MAX_UD
,
166 struct radv_stream_output
{
170 uint8_t component_mask
;
174 struct radv_streamout_info
{
175 uint16_t num_outputs
;
176 struct radv_stream_output outputs
[MAX_SO_OUTPUTS
];
177 uint16_t strides
[MAX_SO_BUFFERS
];
178 uint32_t enabled_stream_buffers_mask
;
181 struct radv_userdata_info
{
186 struct radv_userdata_locations
{
187 struct radv_userdata_info descriptor_sets
[MAX_SETS
];
188 struct radv_userdata_info shader_data
[AC_UD_MAX_UD
];
189 uint32_t descriptor_sets_enabled
;
192 struct radv_vs_output_info
{
193 uint8_t vs_output_param_offset
[VARYING_SLOT_MAX
];
194 uint8_t clip_dist_mask
;
195 uint8_t cull_dist_mask
;
196 uint8_t param_exports
;
197 bool writes_pointsize
;
199 bool writes_viewport_index
;
201 unsigned pos_exports
;
204 struct radv_es_output_info
{
205 uint32_t esgs_itemsize
;
208 struct gfx9_gs_info
{
209 uint32_t vgt_gs_onchip_cntl
;
210 uint32_t vgt_gs_max_prims_per_subgroup
;
211 uint32_t vgt_esgs_ring_itemsize
;
215 struct gfx10_ngg_info
{
216 uint16_t ngg_emit_size
; /* in dwords */
217 uint32_t hw_max_esverts
;
218 uint32_t max_gsprims
;
219 uint32_t max_out_verts
;
220 uint32_t prim_amp_factor
;
221 uint32_t vgt_esgs_ring_itemsize
;
222 uint32_t esgs_ring_size
;
223 bool max_vert_out_per_gs_instance
;
226 struct radv_shader_info
{
227 bool loads_push_constants
;
228 bool loads_dynamic_offsets
;
229 uint8_t min_push_constant_used
;
230 uint8_t max_push_constant_used
;
231 bool has_only_32bit_push_constants
;
232 bool has_indirect_push_constants
;
233 uint8_t num_inline_push_consts
;
234 uint8_t base_inline_push_consts
;
235 uint32_t desc_set_used_mask
;
236 bool needs_multiview_view_index
;
237 bool uses_invocation_id
;
240 uint8_t ballot_bit_size
;
241 struct radv_userdata_locations user_sgprs_locs
;
242 unsigned num_user_sgprs
;
243 unsigned num_input_sgprs
;
244 unsigned num_input_vgprs
;
245 unsigned private_mem_vgprs
;
246 bool need_indirect_descriptor_sets
;
248 bool is_ngg_passthrough
;
250 uint64_t ls_outputs_written
;
251 uint8_t input_usage_mask
[VERT_ATTRIB_MAX
];
252 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
253 bool has_vertex_buffers
; /* needs vertex buffers and base/start */
255 bool needs_instance_id
;
256 struct radv_vs_output_info outinfo
;
257 struct radv_es_output_info es_info
;
263 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
264 uint8_t num_stream_output_components
[4];
265 uint8_t output_streams
[VARYING_SLOT_VAR31
+ 1];
268 unsigned gsvs_vertex_size
;
269 unsigned max_gsvs_emit_size
;
270 unsigned vertices_in
;
271 unsigned vertices_out
;
272 unsigned output_prim
;
273 unsigned invocations
;
274 unsigned es_type
; /* GFX9: VS or TES */
277 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
278 struct radv_vs_output_info outinfo
;
279 struct radv_es_output_info es_info
;
281 unsigned primitive_mode
;
282 enum gl_tess_spacing spacing
;
288 bool force_persample
;
289 bool needs_sample_positions
;
293 bool writes_sample_mask
;
297 uint8_t num_input_clips_culls
;
299 uint32_t flat_shaded_mask
;
300 uint32_t explicit_shaded_mask
;
301 uint32_t float16_shaded_mask
;
304 bool early_fragment_test
;
305 bool post_depth_coverage
;
309 bool uses_block_id
[3];
310 bool uses_thread_id
[3];
311 bool uses_local_invocation_idx
;
312 unsigned block_size
[3];
315 uint64_t outputs_written
;
316 uint64_t patch_outputs_written
;
317 unsigned tcs_vertices_out
;
318 uint32_t num_patches
;
322 struct radv_streamout_info so
;
324 struct gfx9_gs_info gs_ring_info
;
325 struct gfx10_ngg_info ngg_info
;
327 unsigned float_controls_mode
;
330 enum radv_shader_binary_type
{
331 RADV_BINARY_TYPE_LEGACY
,
332 RADV_BINARY_TYPE_RTLD
335 struct radv_shader_binary
{
336 enum radv_shader_binary_type type
;
337 gl_shader_stage stage
;
338 bool is_gs_copy_shader
;
340 struct radv_shader_info info
;
342 /* Self-referential size so we avoid consistency issues. */
346 struct radv_shader_binary_legacy
{
347 struct radv_shader_binary base
;
348 struct ac_shader_config config
;
352 unsigned disasm_size
;
355 /* data has size of stats_size + code_size + ir_size + disasm_size + 2,
356 * where the +2 is for 0 of the ir strings. */
360 struct radv_shader_binary_rtld
{
361 struct radv_shader_binary base
;
363 unsigned llvm_ir_size
;
367 struct radv_compiler_statistic_info
{
372 struct radv_compiler_statistics
{
374 struct radv_compiler_statistic_info
*infos
;
378 struct radv_shader_variant
{
381 struct radeon_winsys_bo
*bo
;
383 struct ac_shader_config config
;
386 struct radv_shader_info info
;
394 struct radv_compiler_statistics
*statistics
;
396 struct list_head slab_list
;
399 struct radv_shader_slab
{
400 struct list_head slabs
;
401 struct list_head shaders
;
402 struct radeon_winsys_bo
*bo
;
408 radv_optimize_nir(struct nir_shader
*shader
, bool optimize_conservatively
,
411 radv_nir_lower_ycbcr_textures(nir_shader
*shader
,
412 const struct radv_pipeline_layout
*layout
);
415 radv_shader_compile_to_nir(struct radv_device
*device
,
416 struct radv_shader_module
*module
,
417 const char *entrypoint_name
,
418 gl_shader_stage stage
,
419 const VkSpecializationInfo
*spec_info
,
420 const VkPipelineCreateFlags flags
,
421 const struct radv_pipeline_layout
*layout
,
422 unsigned subgroup_size
, unsigned ballot_bit_size
);
425 radv_alloc_shader_memory(struct radv_device
*device
,
426 struct radv_shader_variant
*shader
);
429 radv_destroy_shader_slabs(struct radv_device
*device
);
432 radv_create_shaders(struct radv_pipeline
*pipeline
,
433 struct radv_device
*device
,
434 struct radv_pipeline_cache
*cache
,
435 const struct radv_pipeline_key
*key
,
436 const VkPipelineShaderStageCreateInfo
**pStages
,
437 const VkPipelineCreateFlags flags
,
438 VkPipelineCreationFeedbackEXT
*pipeline_feedback
,
439 VkPipelineCreationFeedbackEXT
**stage_feedbacks
);
441 struct radv_shader_variant
*
442 radv_shader_variant_create(struct radv_device
*device
,
443 const struct radv_shader_binary
*binary
,
444 bool keep_shader_info
);
445 struct radv_shader_variant
*
446 radv_shader_variant_compile(struct radv_device
*device
,
447 struct radv_shader_module
*module
,
448 struct nir_shader
*const *shaders
,
450 struct radv_pipeline_layout
*layout
,
451 const struct radv_shader_variant_key
*key
,
452 struct radv_shader_info
*info
,
453 bool keep_shader_info
, bool keep_statistic_info
,
454 struct radv_shader_binary
**binary_out
);
456 struct radv_shader_variant
*
457 radv_create_gs_copy_shader(struct radv_device
*device
, struct nir_shader
*nir
,
458 struct radv_shader_info
*info
,
459 struct radv_shader_binary
**binary_out
,
460 bool multiview
, bool keep_shader_info
,
461 bool keep_statistic_info
);
464 radv_shader_variant_destroy(struct radv_device
*device
,
465 struct radv_shader_variant
*variant
);
469 radv_get_max_waves(struct radv_device
*device
,
470 struct radv_shader_variant
*variant
,
471 gl_shader_stage stage
);
474 radv_get_max_workgroup_size(enum chip_class chip_class
,
475 gl_shader_stage stage
,
476 const unsigned *sizes
);
479 radv_get_shader_name(struct radv_shader_info
*info
,
480 gl_shader_stage stage
);
483 radv_shader_dump_stats(struct radv_device
*device
,
484 struct radv_shader_variant
*variant
,
485 gl_shader_stage stage
,
489 radv_can_dump_shader(struct radv_device
*device
,
490 struct radv_shader_module
*module
,
491 bool is_gs_copy_shader
);
494 radv_can_dump_shader_stats(struct radv_device
*device
,
495 struct radv_shader_module
*module
);
497 static inline unsigned
498 shader_io_get_unique_index(gl_varying_slot slot
)
500 /* handle patch indices separate */
501 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
503 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
505 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
506 return 2 + (slot
- VARYING_SLOT_PATCH0
);
507 if (slot
== VARYING_SLOT_POS
)
509 if (slot
== VARYING_SLOT_PSIZ
)
511 if (slot
== VARYING_SLOT_CLIP_DIST0
)
513 if (slot
== VARYING_SLOT_CLIP_DIST1
)
515 /* 3 is reserved for clip dist as well */
516 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
517 return 4 + (slot
- VARYING_SLOT_VAR0
);
518 unreachable("illegal slot in get unique index\n");
521 static inline unsigned
522 calculate_tess_lds_size(unsigned tcs_num_input_vertices
,
523 unsigned tcs_num_output_vertices
,
524 unsigned tcs_num_inputs
,
525 unsigned tcs_num_patches
,
526 unsigned tcs_outputs_written
,
527 unsigned tcs_per_patch_outputs_written
)
529 unsigned num_tcs_outputs
= util_last_bit64(tcs_outputs_written
);
530 unsigned num_tcs_patch_outputs
= util_last_bit64(tcs_per_patch_outputs_written
);
532 unsigned input_vertex_size
= tcs_num_inputs
* 16;
533 unsigned output_vertex_size
= num_tcs_outputs
* 16;
535 unsigned input_patch_size
= tcs_num_input_vertices
* input_vertex_size
;
537 unsigned pervertex_output_patch_size
= tcs_num_output_vertices
* output_vertex_size
;
538 unsigned output_patch_size
= pervertex_output_patch_size
+ num_tcs_patch_outputs
* 16;
540 unsigned output_patch0_offset
= input_patch_size
* tcs_num_patches
;
542 return output_patch0_offset
+ output_patch_size
* tcs_num_patches
;
545 static inline unsigned
546 get_tcs_num_patches(unsigned tcs_num_input_vertices
,
547 unsigned tcs_num_output_vertices
,
548 unsigned tcs_num_inputs
,
549 unsigned tcs_outputs_written
,
550 unsigned tcs_per_patch_outputs_written
,
551 unsigned tess_offchip_block_dw_size
,
552 enum chip_class chip_class
,
553 enum radeon_family family
)
555 uint32_t input_vertex_size
= tcs_num_inputs
* 16;
556 uint32_t input_patch_size
= tcs_num_input_vertices
* input_vertex_size
;
557 uint32_t num_tcs_outputs
= util_last_bit64(tcs_outputs_written
);
558 uint32_t num_tcs_patch_outputs
= util_last_bit64(tcs_per_patch_outputs_written
);
559 uint32_t output_vertex_size
= num_tcs_outputs
* 16;
560 uint32_t pervertex_output_patch_size
= tcs_num_output_vertices
* output_vertex_size
;
561 uint32_t output_patch_size
= pervertex_output_patch_size
+ num_tcs_patch_outputs
* 16;
563 /* Ensure that we only need one wave per SIMD so we don't need to check
564 * resource usage. Also ensures that the number of tcs in and out
565 * vertices per threadgroup are at most 256.
567 unsigned num_patches
= 64 / MAX2(tcs_num_input_vertices
, tcs_num_output_vertices
) * 4;
568 /* Make sure that the data fits in LDS. This assumes the shaders only
569 * use LDS for the inputs and outputs.
571 unsigned hardware_lds_size
= 32768;
573 /* Looks like STONEY hangs if we use more than 32 KiB LDS in a single
574 * threadgroup, even though there is more than 32 KiB LDS.
576 * Test: dEQP-VK.tessellation.shader_input_output.barrier
578 if (chip_class
>= GFX7
&& family
!= CHIP_STONEY
)
579 hardware_lds_size
= 65536;
581 num_patches
= MIN2(num_patches
, hardware_lds_size
/ (input_patch_size
+ output_patch_size
));
582 /* Make sure the output data fits in the offchip buffer */
583 num_patches
= MIN2(num_patches
, (tess_offchip_block_dw_size
* 4) / output_patch_size
);
584 /* Not necessary for correctness, but improves performance. The
585 * specific value is taken from the proprietary driver.
587 num_patches
= MIN2(num_patches
, 40);
589 /* GFX6 bug workaround - limit LS-HS threadgroups to only one wave. */
590 if (chip_class
== GFX6
) {
591 unsigned one_wave
= 64 / MAX2(tcs_num_input_vertices
, tcs_num_output_vertices
);
592 num_patches
= MIN2(num_patches
, one_wave
);
598 radv_lower_fs_io(nir_shader
*nir
);