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
];
69 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
70 * so we may need to fix it up. */
71 uint64_t alpha_adjust
;
75 uint32_t export_prim_id
:1;
76 uint32_t export_layer_id
:1;
79 struct radv_tes_variant_key
{
81 uint32_t export_prim_id
:1;
82 uint32_t export_layer_id
:1;
84 uint8_t tcs_num_outputs
;
87 struct radv_tcs_variant_key
{
88 struct radv_vs_variant_key vs_key
;
89 unsigned primitive_mode
;
90 unsigned input_vertices
;
92 uint32_t tes_reads_tess_factors
:1;
95 struct radv_fs_variant_key
{
97 uint8_t log2_ps_iter_samples
;
103 struct radv_shader_variant_key
{
105 struct radv_vs_variant_key vs
;
106 struct radv_fs_variant_key fs
;
107 struct radv_tes_variant_key tes
;
108 struct radv_tcs_variant_key tcs
;
110 bool has_multiview_view_index
;
113 struct radv_nir_compiler_options
{
114 struct radv_pipeline_layout
*layout
;
115 struct radv_shader_variant_key key
;
118 bool clamp_shadow_reference
;
123 enum radeon_family family
;
124 enum chip_class chip_class
;
125 uint32_t tess_offchip_block_dw_size
;
126 uint32_t address32_hi
;
130 AC_UD_SCRATCH_RING_OFFSETS
= 0,
131 AC_UD_PUSH_CONSTANTS
= 1,
132 AC_UD_INDIRECT_DESCRIPTOR_SETS
= 2,
133 AC_UD_VIEW_INDEX
= 3,
134 AC_UD_SHADER_START
= 4,
135 AC_UD_VS_VERTEX_BUFFERS
= AC_UD_SHADER_START
,
136 AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
139 AC_UD_CS_GRID_SIZE
= AC_UD_SHADER_START
,
144 AC_UD_MAX_UD
= AC_UD_TCS_MAX_UD
,
146 struct radv_shader_info
{
147 bool loads_push_constants
;
148 uint32_t desc_set_used_mask
;
149 bool needs_multiview_view_index
;
150 bool uses_invocation_id
;
153 uint64_t ls_outputs_written
;
154 uint8_t input_usage_mask
[VERT_ATTRIB_MAX
];
155 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
156 bool has_vertex_buffers
; /* needs vertex buffers and base/start */
158 bool needs_instance_id
;
161 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
164 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
167 bool force_persample
;
168 bool needs_sample_positions
;
169 bool uses_input_attachments
;
173 bool writes_sample_mask
;
177 uint8_t num_input_clips_culls
;
181 bool uses_block_id
[3];
182 bool uses_thread_id
[3];
183 bool uses_local_invocation_idx
;
186 uint64_t outputs_written
;
187 uint64_t patch_outputs_written
;
191 struct radv_userdata_info
{
197 struct radv_userdata_locations
{
198 struct radv_userdata_info descriptor_sets
[RADV_UD_MAX_SETS
];
199 struct radv_userdata_info shader_data
[AC_UD_MAX_UD
];
200 uint32_t descriptor_sets_enabled
;
203 struct radv_vs_output_info
{
204 uint8_t vs_output_param_offset
[VARYING_SLOT_MAX
];
205 uint8_t clip_dist_mask
;
206 uint8_t cull_dist_mask
;
207 uint8_t param_exports
;
208 bool writes_pointsize
;
210 bool writes_viewport_index
;
212 unsigned pos_exports
;
215 struct radv_es_output_info
{
216 uint32_t esgs_itemsize
;
219 struct radv_shader_variant_info
{
220 struct radv_userdata_locations user_sgprs_locs
;
221 struct radv_shader_info info
;
222 unsigned num_user_sgprs
;
223 unsigned num_input_sgprs
;
224 unsigned num_input_vgprs
;
225 unsigned private_mem_vgprs
;
226 bool need_indirect_descriptor_sets
;
229 struct radv_vs_output_info outinfo
;
230 struct radv_es_output_info es_info
;
231 unsigned vgpr_comp_cnt
;
238 uint32_t flat_shaded_mask
;
240 bool early_fragment_test
;
243 unsigned block_size
[3];
246 unsigned vertices_in
;
247 unsigned vertices_out
;
248 unsigned output_prim
;
249 unsigned invocations
;
250 unsigned gsvs_vertex_size
;
251 unsigned max_gsvs_emit_size
;
252 unsigned es_type
; /* GFX9: VS or TES */
255 unsigned tcs_vertices_out
;
256 uint32_t num_patches
;
260 struct radv_vs_output_info outinfo
;
261 struct radv_es_output_info es_info
;
263 unsigned primitive_mode
;
264 enum gl_tess_spacing spacing
;
271 struct radv_shader_variant
{
274 struct radeon_winsys_bo
*bo
;
276 struct ac_shader_config config
;
278 struct radv_shader_variant_info info
;
285 struct nir_shader
*nir
;
287 char *llvm_ir_string
;
289 struct list_head slab_list
;
292 struct radv_shader_slab
{
293 struct list_head slabs
;
294 struct list_head shaders
;
295 struct radeon_winsys_bo
*bo
;
301 radv_optimize_nir(struct nir_shader
*shader
, bool optimize_conservatively
);
304 radv_shader_compile_to_nir(struct radv_device
*device
,
305 struct radv_shader_module
*module
,
306 const char *entrypoint_name
,
307 gl_shader_stage stage
,
308 const VkSpecializationInfo
*spec_info
,
309 const VkPipelineCreateFlags flags
);
312 radv_alloc_shader_memory(struct radv_device
*device
,
313 struct radv_shader_variant
*shader
);
316 radv_destroy_shader_slabs(struct radv_device
*device
);
318 struct radv_shader_variant
*
319 radv_shader_variant_create(struct radv_device
*device
,
320 struct radv_shader_module
*module
,
321 struct nir_shader
*const *shaders
,
323 struct radv_pipeline_layout
*layout
,
324 const struct radv_shader_variant_key
*key
,
326 unsigned *code_size_out
);
328 struct radv_shader_variant
*
329 radv_create_gs_copy_shader(struct radv_device
*device
, struct nir_shader
*nir
,
330 void **code_out
, unsigned *code_size_out
,
334 radv_shader_variant_destroy(struct radv_device
*device
,
335 struct radv_shader_variant
*variant
);
338 radv_get_shader_name(struct radv_shader_variant
*var
, gl_shader_stage stage
);
341 radv_shader_dump_stats(struct radv_device
*device
,
342 struct radv_shader_variant
*variant
,
343 gl_shader_stage stage
,
347 radv_can_dump_shader(struct radv_device
*device
,
348 struct radv_shader_module
*module
,
349 bool is_gs_copy_shader
)
351 if (!(device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADERS
))
354 /* Only dump non-meta shaders, useful for debugging purposes. */
355 return (module
&& !module
->nir
) || is_gs_copy_shader
;
359 radv_can_dump_shader_stats(struct radv_device
*device
,
360 struct radv_shader_module
*module
)
362 /* Only dump non-meta shader stats. */
363 return device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
&&
364 module
&& !module
->nir
;
367 static inline unsigned shader_io_get_unique_index(gl_varying_slot slot
)
369 /* handle patch indices separate */
370 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
372 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
374 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
375 return 2 + (slot
- VARYING_SLOT_PATCH0
);
376 if (slot
== VARYING_SLOT_POS
)
378 if (slot
== VARYING_SLOT_PSIZ
)
380 if (slot
== VARYING_SLOT_CLIP_DIST0
)
382 /* 3 is reserved for clip dist as well */
383 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
384 return 4 + (slot
- VARYING_SLOT_VAR0
);
385 unreachable("illegal slot in get unique index\n");
388 static inline uint32_t
389 radv_get_num_physical_sgprs(struct radv_physical_device
*physical_device
)
391 return physical_device
->rad_info
.chip_class
>= VI
? 800 : 512;