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 "radv_debug.h"
32 #include "radv_private.h"
36 /* descriptor index into scratch ring offsets */
37 #define RING_SCRATCH 0
38 #define RING_ESGS_VS 1
39 #define RING_ESGS_GS 2
40 #define RING_GSVS_VS 3
41 #define RING_GSVS_GS 4
42 #define RING_HS_TESS_FACTOR 5
43 #define RING_HS_TESS_OFFCHIP 6
44 #define RING_PS_SAMPLE_POSITIONS 7
46 // Match MAX_SETS from radv_descriptor_set.h
47 #define RADV_UD_MAX_SETS MAX_SETS
49 #define RADV_NUM_PHYSICAL_VGPRS 256
51 struct radv_shader_module
{
52 struct nir_shader
*nir
;
53 unsigned char sha1
[20];
59 RADV_ALPHA_ADJUST_NONE
= 0,
60 RADV_ALPHA_ADJUST_SNORM
= 1,
61 RADV_ALPHA_ADJUST_SINT
= 2,
62 RADV_ALPHA_ADJUST_SSCALED
= 3,
65 struct radv_vs_out_key
{
69 uint32_t export_prim_id
:1;
70 uint32_t export_layer_id
:1;
71 uint32_t export_clip_dists
:1;
74 struct radv_vs_variant_key
{
75 struct radv_vs_out_key out
;
77 uint32_t instance_rate_inputs
;
78 uint32_t instance_rate_divisors
[MAX_VERTEX_ATTRIBS
];
79 uint8_t vertex_attribute_formats
[MAX_VERTEX_ATTRIBS
];
80 uint32_t vertex_attribute_bindings
[MAX_VERTEX_ATTRIBS
];
81 uint32_t vertex_attribute_offsets
[MAX_VERTEX_ATTRIBS
];
82 uint32_t vertex_attribute_strides
[MAX_VERTEX_ATTRIBS
];
84 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
85 * so we may need to fix it up. */
86 uint64_t alpha_adjust
;
88 /* For some formats the channels have to be shuffled. */
89 uint32_t post_shuffle
;
92 struct radv_tes_variant_key
{
93 struct radv_vs_out_key out
;
96 uint8_t tcs_num_outputs
;
99 struct radv_tcs_variant_key
{
100 struct radv_vs_variant_key vs_key
;
101 unsigned primitive_mode
;
102 unsigned input_vertices
;
104 uint32_t tes_reads_tess_factors
:1;
107 struct radv_fs_variant_key
{
109 uint8_t log2_ps_iter_samples
;
115 struct radv_shader_variant_key
{
117 struct radv_vs_variant_key vs
;
118 struct radv_fs_variant_key fs
;
119 struct radv_tes_variant_key tes
;
120 struct radv_tcs_variant_key tcs
;
122 bool has_multiview_view_index
;
125 struct radv_nir_compiler_options
{
126 struct radv_pipeline_layout
*layout
;
127 struct radv_shader_variant_key key
;
130 bool clamp_shadow_reference
;
135 enum radeon_family family
;
136 enum chip_class chip_class
;
137 uint32_t tess_offchip_block_dw_size
;
138 uint32_t address32_hi
;
142 AC_UD_SCRATCH_RING_OFFSETS
= 0,
143 AC_UD_PUSH_CONSTANTS
= 1,
144 AC_UD_INLINE_PUSH_CONSTANTS
= 2,
145 AC_UD_INDIRECT_DESCRIPTOR_SETS
= 3,
146 AC_UD_VIEW_INDEX
= 4,
147 AC_UD_STREAMOUT_BUFFERS
= 5,
148 AC_UD_SHADER_START
= 6,
149 AC_UD_VS_VERTEX_BUFFERS
= AC_UD_SHADER_START
,
150 AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
153 AC_UD_CS_GRID_SIZE
= AC_UD_SHADER_START
,
158 AC_UD_MAX_UD
= AC_UD_TCS_MAX_UD
,
161 struct radv_stream_output
{
165 uint8_t component_mask
;
169 struct radv_streamout_info
{
170 uint16_t num_outputs
;
171 struct radv_stream_output outputs
[MAX_SO_OUTPUTS
];
172 uint16_t strides
[MAX_SO_BUFFERS
];
173 uint32_t enabled_stream_buffers_mask
;
176 struct radv_shader_info
{
177 bool loads_push_constants
;
178 bool loads_dynamic_offsets
;
179 uint8_t min_push_constant_used
;
180 uint8_t max_push_constant_used
;
181 bool has_only_32bit_push_constants
;
182 bool has_indirect_push_constants
;
183 uint8_t num_inline_push_consts
;
184 uint8_t base_inline_push_consts
;
185 uint32_t desc_set_used_mask
;
186 bool needs_multiview_view_index
;
187 bool uses_invocation_id
;
190 uint64_t ls_outputs_written
;
191 uint8_t input_usage_mask
[VERT_ATTRIB_MAX
];
192 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
193 bool has_vertex_buffers
; /* needs vertex buffers and base/start */
195 bool needs_instance_id
;
198 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
199 uint8_t num_stream_output_components
[4];
200 uint8_t output_streams
[VARYING_SLOT_VAR31
+ 1];
204 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
207 bool force_persample
;
208 bool needs_sample_positions
;
212 bool writes_sample_mask
;
216 uint8_t num_input_clips_culls
;
220 bool uses_block_id
[3];
221 bool uses_thread_id
[3];
222 bool uses_local_invocation_idx
;
225 uint64_t outputs_written
;
226 uint64_t patch_outputs_written
;
229 struct radv_streamout_info so
;
232 struct radv_userdata_info
{
237 struct radv_userdata_locations
{
238 struct radv_userdata_info descriptor_sets
[RADV_UD_MAX_SETS
];
239 struct radv_userdata_info shader_data
[AC_UD_MAX_UD
];
240 uint32_t descriptor_sets_enabled
;
243 struct radv_vs_output_info
{
244 uint8_t vs_output_param_offset
[VARYING_SLOT_MAX
];
245 uint8_t clip_dist_mask
;
246 uint8_t cull_dist_mask
;
247 uint8_t param_exports
;
248 bool writes_pointsize
;
250 bool writes_viewport_index
;
252 unsigned pos_exports
;
255 struct radv_es_output_info
{
256 uint32_t esgs_itemsize
;
259 struct radv_shader_variant_info
{
260 struct radv_userdata_locations user_sgprs_locs
;
261 struct radv_shader_info info
;
262 unsigned num_user_sgprs
;
263 unsigned num_input_sgprs
;
264 unsigned num_input_vgprs
;
265 unsigned private_mem_vgprs
;
266 bool need_indirect_descriptor_sets
;
270 struct radv_vs_output_info outinfo
;
271 struct radv_es_output_info es_info
;
279 uint32_t flat_shaded_mask
;
280 uint32_t float16_shaded_mask
;
282 bool early_fragment_test
;
285 unsigned block_size
[3];
288 unsigned vertices_in
;
289 unsigned vertices_out
;
290 unsigned output_prim
;
291 unsigned invocations
;
292 unsigned gsvs_vertex_size
;
293 unsigned max_gsvs_emit_size
;
294 unsigned es_type
; /* GFX9: VS or TES */
297 unsigned tcs_vertices_out
;
298 uint32_t num_patches
;
302 struct radv_vs_output_info outinfo
;
303 struct radv_es_output_info es_info
;
305 unsigned primitive_mode
;
306 enum gl_tess_spacing spacing
;
314 enum radv_shader_binary_type
{
315 RADV_BINARY_TYPE_LEGACY
,
316 RADV_BINARY_TYPE_RTLD
319 struct radv_shader_binary
{
320 enum radv_shader_binary_type type
;
321 gl_shader_stage stage
;
322 bool is_gs_copy_shader
;
324 struct radv_shader_variant_info variant_info
;
326 /* Self-referential size so we avoid consistency issues. */
330 struct radv_shader_binary_legacy
{
331 struct radv_shader_binary base
;
332 struct ac_shader_config config
;
334 unsigned llvm_ir_size
;
335 unsigned disasm_size
;
337 /* data has size of code_size + llvm_ir_size + disasm_size + 2, where
338 * the +2 is for 0 of the ir strings. */
342 struct radv_shader_binary_rtld
{
343 struct radv_shader_binary base
;
345 unsigned llvm_ir_size
;
349 struct radv_shader_variant
{
352 struct radeon_winsys_bo
*bo
;
354 struct ac_shader_config config
;
356 struct radv_shader_variant_info info
;
361 struct nir_shader
*nir
;
363 char *llvm_ir_string
;
365 struct list_head slab_list
;
368 struct radv_shader_slab
{
369 struct list_head slabs
;
370 struct list_head shaders
;
371 struct radeon_winsys_bo
*bo
;
377 radv_optimize_nir(struct nir_shader
*shader
, bool optimize_conservatively
,
380 radv_nir_lower_ycbcr_textures(nir_shader
*shader
,
381 const struct radv_pipeline_layout
*layout
);
384 radv_shader_compile_to_nir(struct radv_device
*device
,
385 struct radv_shader_module
*module
,
386 const char *entrypoint_name
,
387 gl_shader_stage stage
,
388 const VkSpecializationInfo
*spec_info
,
389 const VkPipelineCreateFlags flags
,
390 const struct radv_pipeline_layout
*layout
);
393 radv_alloc_shader_memory(struct radv_device
*device
,
394 struct radv_shader_variant
*shader
);
397 radv_destroy_shader_slabs(struct radv_device
*device
);
399 struct radv_shader_variant
*
400 radv_shader_variant_create(struct radv_device
*device
,
401 const struct radv_shader_binary
*binary
);
402 struct radv_shader_variant
*
403 radv_shader_variant_compile(struct radv_device
*device
,
404 struct radv_shader_module
*module
,
405 struct nir_shader
*const *shaders
,
407 struct radv_pipeline_layout
*layout
,
408 const struct radv_shader_variant_key
*key
,
409 struct radv_shader_binary
**binary_out
);
411 struct radv_shader_variant
*
412 radv_create_gs_copy_shader(struct radv_device
*device
, struct nir_shader
*nir
,
413 struct radv_shader_binary
**binary_out
,
417 radv_shader_variant_destroy(struct radv_device
*device
,
418 struct radv_shader_variant
*variant
);
421 radv_get_shader_name(struct radv_shader_variant
*var
, gl_shader_stage stage
);
424 radv_shader_dump_stats(struct radv_device
*device
,
425 struct radv_shader_variant
*variant
,
426 gl_shader_stage stage
,
430 radv_can_dump_shader(struct radv_device
*device
,
431 struct radv_shader_module
*module
,
432 bool is_gs_copy_shader
)
434 if (!(device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADERS
))
437 /* Only dump non-meta shaders, useful for debugging purposes. */
438 return (module
&& !module
->nir
) || is_gs_copy_shader
;
442 radv_can_dump_shader_stats(struct radv_device
*device
,
443 struct radv_shader_module
*module
)
445 /* Only dump non-meta shader stats. */
446 return device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
&&
447 module
&& !module
->nir
;
450 static inline unsigned shader_io_get_unique_index(gl_varying_slot slot
)
452 /* handle patch indices separate */
453 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
455 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
457 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
458 return 2 + (slot
- VARYING_SLOT_PATCH0
);
459 if (slot
== VARYING_SLOT_POS
)
461 if (slot
== VARYING_SLOT_PSIZ
)
463 if (slot
== VARYING_SLOT_CLIP_DIST0
)
465 if (slot
== VARYING_SLOT_CLIP_DIST1
)
467 /* 3 is reserved for clip dist as well */
468 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
469 return 4 + (slot
- VARYING_SLOT_VAR0
);
470 unreachable("illegal slot in get unique index\n");