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_variant_key
{
66 uint32_t instance_rate_inputs
;
67 uint32_t instance_rate_divisors
[MAX_VERTEX_ATTRIBS
];
68 uint8_t vertex_attribute_formats
[MAX_VERTEX_ATTRIBS
];
70 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
71 * so we may need to fix it up. */
72 uint64_t alpha_adjust
;
76 uint32_t export_prim_id
:1;
77 uint32_t export_layer_id
:1;
80 struct radv_tes_variant_key
{
82 uint32_t export_prim_id
:1;
83 uint32_t export_layer_id
:1;
85 uint8_t tcs_num_outputs
;
88 struct radv_tcs_variant_key
{
89 struct radv_vs_variant_key vs_key
;
90 unsigned primitive_mode
;
91 unsigned input_vertices
;
93 uint32_t tes_reads_tess_factors
:1;
96 struct radv_fs_variant_key
{
98 uint8_t log2_ps_iter_samples
;
104 struct radv_shader_variant_key
{
106 struct radv_vs_variant_key vs
;
107 struct radv_fs_variant_key fs
;
108 struct radv_tes_variant_key tes
;
109 struct radv_tcs_variant_key tcs
;
111 bool has_multiview_view_index
;
114 struct radv_nir_compiler_options
{
115 struct radv_pipeline_layout
*layout
;
116 struct radv_shader_variant_key key
;
119 bool clamp_shadow_reference
;
124 enum radeon_family family
;
125 enum chip_class chip_class
;
126 uint32_t tess_offchip_block_dw_size
;
127 uint32_t address32_hi
;
131 AC_UD_SCRATCH_RING_OFFSETS
= 0,
132 AC_UD_PUSH_CONSTANTS
= 1,
133 AC_UD_INLINE_PUSH_CONSTANTS
= 2,
134 AC_UD_INDIRECT_DESCRIPTOR_SETS
= 3,
135 AC_UD_VIEW_INDEX
= 4,
136 AC_UD_STREAMOUT_BUFFERS
= 5,
137 AC_UD_SHADER_START
= 6,
138 AC_UD_VS_VERTEX_BUFFERS
= AC_UD_SHADER_START
,
139 AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
142 AC_UD_CS_GRID_SIZE
= AC_UD_SHADER_START
,
147 AC_UD_MAX_UD
= AC_UD_TCS_MAX_UD
,
150 struct radv_stream_output
{
154 uint8_t component_mask
;
158 struct radv_streamout_info
{
159 uint16_t num_outputs
;
160 struct radv_stream_output outputs
[MAX_SO_OUTPUTS
];
161 uint16_t strides
[MAX_SO_BUFFERS
];
162 uint32_t enabled_stream_buffers_mask
;
165 struct radv_shader_info
{
166 bool loads_push_constants
;
167 bool loads_dynamic_offsets
;
168 uint8_t min_push_constant_used
;
169 uint8_t max_push_constant_used
;
170 bool has_only_32bit_push_constants
;
171 bool has_indirect_push_constants
;
172 uint8_t num_inline_push_consts
;
173 uint8_t base_inline_push_consts
;
174 uint32_t desc_set_used_mask
;
175 bool needs_multiview_view_index
;
176 bool uses_invocation_id
;
179 uint64_t ls_outputs_written
;
180 uint8_t input_usage_mask
[VERT_ATTRIB_MAX
];
181 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
182 bool has_vertex_buffers
; /* needs vertex buffers and base/start */
184 bool needs_instance_id
;
187 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
188 uint8_t num_stream_output_components
[4];
189 uint8_t output_streams
[VARYING_SLOT_VAR31
+ 1];
193 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
196 bool force_persample
;
197 bool needs_sample_positions
;
198 bool uses_input_attachments
;
202 bool writes_sample_mask
;
206 uint8_t num_input_clips_culls
;
210 bool uses_block_id
[3];
211 bool uses_thread_id
[3];
212 bool uses_local_invocation_idx
;
215 uint64_t outputs_written
;
216 uint64_t patch_outputs_written
;
219 struct radv_streamout_info so
;
222 struct radv_userdata_info
{
227 struct radv_userdata_locations
{
228 struct radv_userdata_info descriptor_sets
[RADV_UD_MAX_SETS
];
229 struct radv_userdata_info shader_data
[AC_UD_MAX_UD
];
230 uint32_t descriptor_sets_enabled
;
233 struct radv_vs_output_info
{
234 uint8_t vs_output_param_offset
[VARYING_SLOT_MAX
];
235 uint8_t clip_dist_mask
;
236 uint8_t cull_dist_mask
;
237 uint8_t param_exports
;
238 bool writes_pointsize
;
240 bool writes_viewport_index
;
242 unsigned pos_exports
;
245 struct radv_es_output_info
{
246 uint32_t esgs_itemsize
;
249 struct radv_shader_variant_info
{
250 struct radv_userdata_locations user_sgprs_locs
;
251 struct radv_shader_info info
;
252 unsigned num_user_sgprs
;
253 unsigned num_input_sgprs
;
254 unsigned num_input_vgprs
;
255 unsigned private_mem_vgprs
;
256 bool need_indirect_descriptor_sets
;
259 struct radv_vs_output_info outinfo
;
260 struct radv_es_output_info es_info
;
261 unsigned vgpr_comp_cnt
;
268 uint32_t flat_shaded_mask
;
269 uint32_t float16_shaded_mask
;
271 bool early_fragment_test
;
274 unsigned block_size
[3];
277 unsigned vertices_in
;
278 unsigned vertices_out
;
279 unsigned output_prim
;
280 unsigned invocations
;
281 unsigned gsvs_vertex_size
;
282 unsigned max_gsvs_emit_size
;
283 unsigned es_type
; /* GFX9: VS or TES */
286 unsigned tcs_vertices_out
;
287 uint32_t num_patches
;
291 struct radv_vs_output_info outinfo
;
292 struct radv_es_output_info es_info
;
294 unsigned primitive_mode
;
295 enum gl_tess_spacing spacing
;
302 struct radv_shader_variant
{
305 struct radeon_winsys_bo
*bo
;
307 struct ac_shader_config config
;
309 struct radv_shader_variant_info info
;
316 struct nir_shader
*nir
;
318 char *llvm_ir_string
;
320 struct list_head slab_list
;
323 struct radv_shader_slab
{
324 struct list_head slabs
;
325 struct list_head shaders
;
326 struct radeon_winsys_bo
*bo
;
332 radv_optimize_nir(struct nir_shader
*shader
, bool optimize_conservatively
,
336 radv_shader_compile_to_nir(struct radv_device
*device
,
337 struct radv_shader_module
*module
,
338 const char *entrypoint_name
,
339 gl_shader_stage stage
,
340 const VkSpecializationInfo
*spec_info
,
341 const VkPipelineCreateFlags flags
);
344 radv_alloc_shader_memory(struct radv_device
*device
,
345 struct radv_shader_variant
*shader
);
348 radv_destroy_shader_slabs(struct radv_device
*device
);
350 struct radv_shader_variant
*
351 radv_shader_variant_create(struct radv_device
*device
,
352 struct radv_shader_module
*module
,
353 struct nir_shader
*const *shaders
,
355 struct radv_pipeline_layout
*layout
,
356 const struct radv_shader_variant_key
*key
,
358 unsigned *code_size_out
);
360 struct radv_shader_variant
*
361 radv_create_gs_copy_shader(struct radv_device
*device
, struct nir_shader
*nir
,
362 void **code_out
, unsigned *code_size_out
,
366 radv_shader_variant_destroy(struct radv_device
*device
,
367 struct radv_shader_variant
*variant
);
370 radv_get_shader_name(struct radv_shader_variant
*var
, gl_shader_stage stage
);
373 radv_shader_dump_stats(struct radv_device
*device
,
374 struct radv_shader_variant
*variant
,
375 gl_shader_stage stage
,
379 radv_can_dump_shader(struct radv_device
*device
,
380 struct radv_shader_module
*module
,
381 bool is_gs_copy_shader
)
383 if (!(device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADERS
))
386 /* Only dump non-meta shaders, useful for debugging purposes. */
387 return (module
&& !module
->nir
) || is_gs_copy_shader
;
391 radv_can_dump_shader_stats(struct radv_device
*device
,
392 struct radv_shader_module
*module
)
394 /* Only dump non-meta shader stats. */
395 return device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
&&
396 module
&& !module
->nir
;
399 static inline unsigned shader_io_get_unique_index(gl_varying_slot slot
)
401 /* handle patch indices separate */
402 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
404 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
406 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
407 return 2 + (slot
- VARYING_SLOT_PATCH0
);
408 if (slot
== VARYING_SLOT_POS
)
410 if (slot
== VARYING_SLOT_PSIZ
)
412 if (slot
== VARYING_SLOT_CLIP_DIST0
)
414 if (slot
== VARYING_SLOT_CLIP_DIST1
)
416 /* 3 is reserved for clip dist as well */
417 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
418 return 4 + (slot
- VARYING_SLOT_VAR0
);
419 unreachable("illegal slot in get unique index\n");