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 struct radv_shader_module
{
50 struct nir_shader
*nir
;
51 unsigned char sha1
[20];
56 struct radv_vs_variant_key
{
57 uint32_t instance_rate_inputs
;
60 uint32_t export_prim_id
:1;
63 struct radv_tes_variant_key
{
65 uint32_t export_prim_id
:1;
68 struct radv_tcs_variant_key
{
69 struct radv_vs_variant_key vs_key
;
70 unsigned primitive_mode
;
71 unsigned input_vertices
;
72 uint32_t tes_reads_tess_factors
:1;
75 struct radv_fs_variant_key
{
77 uint8_t log2_ps_iter_samples
;
78 uint8_t log2_num_samples
;
81 uint32_t multisample
: 1;
84 struct radv_shader_variant_key
{
86 struct radv_vs_variant_key vs
;
87 struct radv_fs_variant_key fs
;
88 struct radv_tes_variant_key tes
;
89 struct radv_tcs_variant_key tcs
;
91 bool has_multiview_view_index
;
94 struct radv_nir_compiler_options
{
95 struct radv_pipeline_layout
*layout
;
96 struct radv_shader_variant_key key
;
99 bool clamp_shadow_reference
;
103 enum radeon_family family
;
104 enum chip_class chip_class
;
108 AC_UD_SCRATCH_RING_OFFSETS
= 0,
109 AC_UD_PUSH_CONSTANTS
= 1,
110 AC_UD_INDIRECT_DESCRIPTOR_SETS
= 2,
111 AC_UD_VIEW_INDEX
= 3,
112 AC_UD_SHADER_START
= 4,
113 AC_UD_VS_VERTEX_BUFFERS
= AC_UD_SHADER_START
,
114 AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
115 AC_UD_VS_LS_TCS_IN_LAYOUT
,
117 AC_UD_PS_SAMPLE_POS_OFFSET
= AC_UD_SHADER_START
,
119 AC_UD_CS_GRID_SIZE
= AC_UD_SHADER_START
,
121 AC_UD_GS_VS_RING_STRIDE_ENTRIES
= AC_UD_VS_MAX_UD
,
123 AC_UD_TCS_OFFCHIP_LAYOUT
= AC_UD_VS_MAX_UD
,
125 AC_UD_TES_OFFCHIP_LAYOUT
= AC_UD_SHADER_START
,
127 AC_UD_MAX_UD
= AC_UD_TCS_MAX_UD
,
129 struct radv_shader_info
{
130 bool loads_push_constants
;
131 uint32_t desc_set_used_mask
;
132 bool needs_multiview_view_index
;
133 bool uses_invocation_id
;
136 uint64_t ls_outputs_written
;
137 uint8_t input_usage_mask
[VERT_ATTRIB_MAX
];
138 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
139 bool has_vertex_buffers
; /* needs vertex buffers and base/start */
141 bool needs_instance_id
;
144 uint8_t output_usage_mask
[VARYING_SLOT_VAR31
+ 1];
147 bool force_persample
;
148 bool needs_sample_positions
;
149 bool uses_input_attachments
;
153 bool writes_sample_mask
;
160 bool uses_block_id
[3];
161 bool uses_thread_id
[3];
162 bool uses_local_invocation_idx
;
165 uint64_t outputs_written
;
166 uint64_t patch_outputs_written
;
170 struct radv_userdata_info
{
174 uint32_t indirect_offset
;
177 struct radv_userdata_locations
{
178 struct radv_userdata_info descriptor_sets
[RADV_UD_MAX_SETS
];
179 struct radv_userdata_info shader_data
[AC_UD_MAX_UD
];
182 struct radv_vs_output_info
{
183 uint8_t vs_output_param_offset
[VARYING_SLOT_MAX
];
184 uint8_t clip_dist_mask
;
185 uint8_t cull_dist_mask
;
186 uint8_t param_exports
;
187 bool writes_pointsize
;
189 bool writes_viewport_index
;
191 unsigned pos_exports
;
194 struct radv_es_output_info
{
195 uint32_t esgs_itemsize
;
198 struct radv_shader_variant_info
{
199 struct radv_userdata_locations user_sgprs_locs
;
200 struct radv_shader_info info
;
201 unsigned num_user_sgprs
;
202 unsigned num_input_sgprs
;
203 unsigned num_input_vgprs
;
204 unsigned private_mem_vgprs
;
205 bool need_indirect_descriptor_sets
;
208 struct radv_vs_output_info outinfo
;
209 struct radv_es_output_info es_info
;
210 unsigned vgpr_comp_cnt
;
213 uint64_t outputs_written
;
218 uint32_t flat_shaded_mask
;
220 bool early_fragment_test
;
223 unsigned block_size
[3];
226 unsigned vertices_in
;
227 unsigned vertices_out
;
228 unsigned output_prim
;
229 unsigned invocations
;
230 unsigned gsvs_vertex_size
;
231 unsigned max_gsvs_emit_size
;
232 unsigned es_type
; /* GFX9: VS or TES */
235 unsigned tcs_vertices_out
;
236 /* Which outputs are actually written */
237 uint64_t outputs_written
;
238 /* Which patch outputs are actually written */
239 uint32_t patch_outputs_written
;
243 struct radv_vs_output_info outinfo
;
244 struct radv_es_output_info es_info
;
246 unsigned primitive_mode
;
247 enum gl_tess_spacing spacing
;
254 struct radv_shader_variant
{
257 struct radeon_winsys_bo
*bo
;
259 struct ac_shader_config config
;
261 struct radv_shader_variant_info info
;
268 struct nir_shader
*nir
;
270 char *llvm_ir_string
;
272 struct list_head slab_list
;
275 struct radv_shader_slab
{
276 struct list_head slabs
;
277 struct list_head shaders
;
278 struct radeon_winsys_bo
*bo
;
284 radv_optimize_nir(struct nir_shader
*shader
);
287 radv_shader_compile_to_nir(struct radv_device
*device
,
288 struct radv_shader_module
*module
,
289 const char *entrypoint_name
,
290 gl_shader_stage stage
,
291 const VkSpecializationInfo
*spec_info
);
294 radv_alloc_shader_memory(struct radv_device
*device
,
295 struct radv_shader_variant
*shader
);
298 radv_destroy_shader_slabs(struct radv_device
*device
);
300 struct radv_shader_variant
*
301 radv_shader_variant_create(struct radv_device
*device
,
302 struct radv_shader_module
*module
,
303 struct nir_shader
*const *shaders
,
305 struct radv_pipeline_layout
*layout
,
306 const struct radv_shader_variant_key
*key
,
308 unsigned *code_size_out
);
310 struct radv_shader_variant
*
311 radv_create_gs_copy_shader(struct radv_device
*device
, struct nir_shader
*nir
,
312 void **code_out
, unsigned *code_size_out
,
316 radv_shader_variant_destroy(struct radv_device
*device
,
317 struct radv_shader_variant
*variant
);
320 radv_get_shader_name(struct radv_shader_variant
*var
, gl_shader_stage stage
);
323 radv_shader_dump_stats(struct radv_device
*device
,
324 struct radv_shader_variant
*variant
,
325 gl_shader_stage stage
,
329 radv_can_dump_shader(struct radv_device
*device
,
330 struct radv_shader_module
*module
)
332 /* Only dump non-meta shaders, useful for debugging purposes. */
333 return device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADERS
&&
334 module
&& !module
->nir
;
338 radv_can_dump_shader_stats(struct radv_device
*device
,
339 struct radv_shader_module
*module
)
341 /* Only dump non-meta shader stats. */
342 return device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
&&
343 module
&& !module
->nir
;
346 static inline unsigned shader_io_get_unique_index(gl_varying_slot slot
)
348 /* handle patch indices separate */
349 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
351 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
353 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
354 return 2 + (slot
- VARYING_SLOT_PATCH0
);
355 if (slot
== VARYING_SLOT_POS
)
357 if (slot
== VARYING_SLOT_PSIZ
)
359 if (slot
== VARYING_SLOT_CLIP_DIST0
)
361 /* 3 is reserved for clip dist as well */
362 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
363 return 4 + (slot
- VARYING_SLOT_VAR0
);
364 unreachable("illegal slot in get unique index\n");