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 /* A common prefix of the vs and tes keys. */
123 struct radv_vs_out_key vs_common_out
;
125 bool has_multiview_view_index
;
128 struct radv_nir_compiler_options
{
129 struct radv_pipeline_layout
*layout
;
130 struct radv_shader_variant_key key
;
133 bool clamp_shadow_reference
;
138 enum radeon_family family
;
139 enum chip_class chip_class
;
140 uint32_t tess_offchip_block_dw_size
;
141 uint32_t address32_hi
;
145 AC_UD_SCRATCH_RING_OFFSETS
= 0,
146 AC_UD_PUSH_CONSTANTS
= 1,
147 AC_UD_INLINE_PUSH_CONSTANTS
= 2,
148 AC_UD_INDIRECT_DESCRIPTOR_SETS
= 3,
149 AC_UD_VIEW_INDEX
= 4,
150 AC_UD_STREAMOUT_BUFFERS
= 5,
151 AC_UD_SHADER_START
= 6,
152 AC_UD_VS_VERTEX_BUFFERS
= AC_UD_SHADER_START
,
153 AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
156 AC_UD_CS_GRID_SIZE
= AC_UD_SHADER_START
,
161 AC_UD_MAX_UD
= AC_UD_TCS_MAX_UD
,
164 struct radv_stream_output
{
168 uint8_t component_mask
;
172 struct radv_streamout_info
{
173 uint16_t num_outputs
;
174 struct radv_stream_output outputs
[MAX_SO_OUTPUTS
];
175 uint16_t strides
[MAX_SO_BUFFERS
];
176 uint32_t enabled_stream_buffers_mask
;
179 struct radv_shader_info
{
180 bool loads_push_constants
;
181 bool loads_dynamic_offsets
;
182 uint8_t min_push_constant_used
;
183 uint8_t max_push_constant_used
;
184 bool has_only_32bit_push_constants
;
185 bool has_indirect_push_constants
;
186 uint8_t num_inline_push_consts
;
187 uint8_t base_inline_push_consts
;
188 uint32_t desc_set_used_mask
;
189 bool needs_multiview_view_index
;
190 bool uses_invocation_id
;
193 uint64_t ls_outputs_written
;
194 uint8_t input_usage_mask
[VERT_ATTRIB_MAX
];
195 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
196 bool has_vertex_buffers
; /* needs vertex buffers and base/start */
198 bool needs_instance_id
;
201 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
202 uint8_t num_stream_output_components
[4];
203 uint8_t output_streams
[VARYING_SLOT_VAR31
+ 1];
207 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
210 bool force_persample
;
211 bool needs_sample_positions
;
215 bool writes_sample_mask
;
219 uint8_t num_input_clips_culls
;
223 bool uses_block_id
[3];
224 bool uses_thread_id
[3];
225 bool uses_local_invocation_idx
;
228 uint64_t outputs_written
;
229 uint64_t patch_outputs_written
;
232 struct radv_streamout_info so
;
235 struct radv_userdata_info
{
240 struct radv_userdata_locations
{
241 struct radv_userdata_info descriptor_sets
[RADV_UD_MAX_SETS
];
242 struct radv_userdata_info shader_data
[AC_UD_MAX_UD
];
243 uint32_t descriptor_sets_enabled
;
246 struct radv_vs_output_info
{
247 uint8_t vs_output_param_offset
[VARYING_SLOT_MAX
];
248 uint8_t clip_dist_mask
;
249 uint8_t cull_dist_mask
;
250 uint8_t param_exports
;
251 bool writes_pointsize
;
253 bool writes_viewport_index
;
255 unsigned pos_exports
;
258 struct radv_es_output_info
{
259 uint32_t esgs_itemsize
;
262 struct radv_shader_variant_info
{
263 struct radv_userdata_locations user_sgprs_locs
;
264 struct radv_shader_info info
;
265 unsigned num_user_sgprs
;
266 unsigned num_input_sgprs
;
267 unsigned num_input_vgprs
;
268 unsigned private_mem_vgprs
;
269 bool need_indirect_descriptor_sets
;
273 struct radv_vs_output_info outinfo
;
274 struct radv_es_output_info es_info
;
282 uint32_t flat_shaded_mask
;
283 uint32_t float16_shaded_mask
;
285 bool early_fragment_test
;
288 unsigned block_size
[3];
291 unsigned vertices_in
;
292 unsigned vertices_out
;
293 unsigned output_prim
;
294 unsigned invocations
;
295 unsigned gsvs_vertex_size
;
296 unsigned max_gsvs_emit_size
;
297 unsigned es_type
; /* GFX9: VS or TES */
300 unsigned tcs_vertices_out
;
301 uint32_t num_patches
;
305 struct radv_vs_output_info outinfo
;
306 struct radv_es_output_info es_info
;
308 unsigned primitive_mode
;
309 enum gl_tess_spacing spacing
;
317 enum radv_shader_binary_type
{
318 RADV_BINARY_TYPE_LEGACY
,
319 RADV_BINARY_TYPE_RTLD
322 struct radv_shader_binary
{
323 enum radv_shader_binary_type type
;
324 gl_shader_stage stage
;
325 bool is_gs_copy_shader
;
327 struct radv_shader_variant_info variant_info
;
329 /* Self-referential size so we avoid consistency issues. */
333 struct radv_shader_binary_legacy
{
334 struct radv_shader_binary base
;
335 struct ac_shader_config config
;
337 unsigned llvm_ir_size
;
338 unsigned disasm_size
;
340 /* data has size of code_size + llvm_ir_size + disasm_size + 2, where
341 * the +2 is for 0 of the ir strings. */
345 struct radv_shader_binary_rtld
{
346 struct radv_shader_binary base
;
348 unsigned llvm_ir_size
;
352 struct radv_shader_variant
{
355 struct radeon_winsys_bo
*bo
;
357 struct ac_shader_config config
;
359 struct radv_shader_variant_info info
;
364 struct nir_shader
*nir
;
366 char *llvm_ir_string
;
368 struct list_head slab_list
;
371 struct radv_shader_slab
{
372 struct list_head slabs
;
373 struct list_head shaders
;
374 struct radeon_winsys_bo
*bo
;
380 radv_optimize_nir(struct nir_shader
*shader
, bool optimize_conservatively
,
383 radv_nir_lower_ycbcr_textures(nir_shader
*shader
,
384 const struct radv_pipeline_layout
*layout
);
387 radv_shader_compile_to_nir(struct radv_device
*device
,
388 struct radv_shader_module
*module
,
389 const char *entrypoint_name
,
390 gl_shader_stage stage
,
391 const VkSpecializationInfo
*spec_info
,
392 const VkPipelineCreateFlags flags
,
393 const struct radv_pipeline_layout
*layout
);
396 radv_alloc_shader_memory(struct radv_device
*device
,
397 struct radv_shader_variant
*shader
);
400 radv_destroy_shader_slabs(struct radv_device
*device
);
402 struct radv_shader_variant
*
403 radv_shader_variant_create(struct radv_device
*device
,
404 const struct radv_shader_binary
*binary
);
405 struct radv_shader_variant
*
406 radv_shader_variant_compile(struct radv_device
*device
,
407 struct radv_shader_module
*module
,
408 struct nir_shader
*const *shaders
,
410 struct radv_pipeline_layout
*layout
,
411 const struct radv_shader_variant_key
*key
,
412 struct radv_shader_binary
**binary_out
);
414 struct radv_shader_variant
*
415 radv_create_gs_copy_shader(struct radv_device
*device
, struct nir_shader
*nir
,
416 struct radv_shader_binary
**binary_out
,
420 radv_shader_variant_destroy(struct radv_device
*device
,
421 struct radv_shader_variant
*variant
);
424 radv_get_shader_name(struct radv_shader_variant_info
*info
,
425 gl_shader_stage stage
);
428 radv_shader_dump_stats(struct radv_device
*device
,
429 struct radv_shader_variant
*variant
,
430 gl_shader_stage stage
,
434 radv_can_dump_shader(struct radv_device
*device
,
435 struct radv_shader_module
*module
,
436 bool is_gs_copy_shader
)
438 if (!(device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADERS
))
441 /* Only dump non-meta shaders, useful for debugging purposes. */
442 return (module
&& !module
->nir
) || is_gs_copy_shader
;
446 radv_can_dump_shader_stats(struct radv_device
*device
,
447 struct radv_shader_module
*module
)
449 /* Only dump non-meta shader stats. */
450 return device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
&&
451 module
&& !module
->nir
;
454 static inline unsigned shader_io_get_unique_index(gl_varying_slot slot
)
456 /* handle patch indices separate */
457 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
459 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
461 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
462 return 2 + (slot
- VARYING_SLOT_PATCH0
);
463 if (slot
== VARYING_SLOT_POS
)
465 if (slot
== VARYING_SLOT_PSIZ
)
467 if (slot
== VARYING_SLOT_CLIP_DIST0
)
469 if (slot
== VARYING_SLOT_CLIP_DIST1
)
471 /* 3 is reserved for clip dist as well */
472 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
473 return 4 + (slot
- VARYING_SLOT_VAR0
);
474 unreachable("illegal slot in get unique index\n");