2 * Copyright © 2017 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 #include "radv_private.h"
24 #include "radv_shader.h"
27 static void mark_sampler_desc(const nir_variable
*var
,
28 struct radv_shader_info
*info
)
30 info
->desc_set_used_mask
|= (1 << var
->data
.descriptor_set
);
33 static void mark_ls_output(struct radv_shader_info
*info
,
34 uint32_t param
, int num_slots
)
36 uint64_t mask
= (1ull << num_slots
) - 1ull;
37 info
->vs
.ls_outputs_written
|= (mask
<< param
);
40 static void mark_tess_output(struct radv_shader_info
*info
,
41 bool is_patch
, uint32_t param
, int num_slots
)
43 uint64_t mask
= (1ull << num_slots
) - 1ull;
45 info
->tcs
.patch_outputs_written
|= (mask
<< param
);
47 info
->tcs
.outputs_written
|= (mask
<< param
);
50 static void get_deref_offset(nir_deref_var
*deref
, unsigned *const_out
)
52 nir_deref
*tail
= &deref
->deref
;
53 unsigned const_offset
= 0;
55 if (deref
->var
->data
.compact
) {
56 assert(tail
->child
->deref_type
== nir_deref_type_array
);
57 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
59 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
60 /* We always lower indirect dereferences for "compact" array vars. */
61 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
63 *const_out
= deref_array
->base_offset
;
67 while (tail
->child
!= NULL
) {
68 const struct glsl_type
*parent_type
= tail
->type
;
71 if (tail
->deref_type
== nir_deref_type_array
) {
72 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
73 unsigned size
= glsl_count_attribute_slots(tail
->type
, false);
75 const_offset
+= size
* deref_array
->base_offset
;
76 } else if (tail
->deref_type
== nir_deref_type_struct
) {
77 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
79 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
80 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
81 const_offset
+= glsl_count_attribute_slots(ft
, false);
84 unreachable("unsupported deref type");
87 *const_out
= const_offset
;
91 gather_intrinsic_load_var_info(const nir_shader
*nir
,
92 const nir_intrinsic_instr
*instr
,
93 struct radv_shader_info
*info
)
95 switch (nir
->info
.stage
) {
96 case MESA_SHADER_VERTEX
: {
97 nir_deref_var
*dvar
= instr
->variables
[0];
98 nir_variable
*var
= dvar
->var
;
100 if (var
->data
.mode
== nir_var_shader_in
) {
101 unsigned idx
= var
->data
.location
;
102 uint8_t mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
104 info
->vs
.input_usage_mask
[idx
] |=
105 mask
<< var
->data
.location_frac
;
115 gather_intrinsic_store_var_info(const nir_shader
*nir
,
116 const nir_intrinsic_instr
*instr
,
117 struct radv_shader_info
*info
)
119 nir_deref_var
*dvar
= instr
->variables
[0];
120 nir_variable
*var
= dvar
->var
;
122 if (var
->data
.mode
== nir_var_shader_out
) {
123 unsigned attrib_count
= glsl_count_attribute_slots(var
->type
, false);
124 unsigned idx
= var
->data
.location
;
125 unsigned comp
= var
->data
.location_frac
;
126 unsigned const_offset
= 0;
128 get_deref_offset(dvar
, &const_offset
);
130 switch (nir
->info
.stage
) {
131 case MESA_SHADER_VERTEX
:
132 for (unsigned i
= 0; i
< attrib_count
; i
++) {
133 info
->vs
.output_usage_mask
[idx
+ i
+ const_offset
] |=
134 instr
->const_index
[0] << comp
;
137 case MESA_SHADER_TESS_EVAL
:
138 for (unsigned i
= 0; i
< attrib_count
; i
++) {
139 info
->tes
.output_usage_mask
[idx
+ i
+ const_offset
] |=
140 instr
->const_index
[0] << comp
;
143 case MESA_SHADER_TESS_CTRL
: {
144 unsigned param
= shader_io_get_unique_index(idx
);
145 const struct glsl_type
*type
= var
->type
;
147 if (!var
->data
.patch
)
148 type
= glsl_get_array_element(var
->type
);
151 var
->data
.compact
? DIV_ROUND_UP(glsl_get_length(type
), 4)
152 : glsl_count_attribute_slots(type
, false);
154 if (idx
== VARYING_SLOT_CLIP_DIST0
)
155 slots
= (nir
->info
.clip_distance_array_size
+
156 nir
->info
.cull_distance_array_size
> 4) ? 2 : 1;
158 mark_tess_output(info
, var
->data
.patch
, param
, slots
);
168 gather_intrinsic_info(const nir_shader
*nir
, const nir_intrinsic_instr
*instr
,
169 struct radv_shader_info
*info
)
171 switch (instr
->intrinsic
) {
172 case nir_intrinsic_interp_var_at_sample
:
173 info
->ps
.needs_sample_positions
= true;
175 case nir_intrinsic_load_draw_id
:
176 info
->vs
.needs_draw_id
= true;
178 case nir_intrinsic_load_instance_id
:
179 info
->vs
.needs_instance_id
= true;
181 case nir_intrinsic_load_num_work_groups
:
182 info
->cs
.uses_grid_size
= true;
184 case nir_intrinsic_load_local_invocation_id
:
185 case nir_intrinsic_load_work_group_id
: {
186 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
188 unsigned i
= u_bit_scan(&mask
);
190 if (instr
->intrinsic
== nir_intrinsic_load_work_group_id
)
191 info
->cs
.uses_block_id
[i
] = true;
193 info
->cs
.uses_thread_id
[i
] = true;
197 case nir_intrinsic_load_local_invocation_index
:
198 case nir_intrinsic_load_subgroup_id
:
199 case nir_intrinsic_load_num_subgroups
:
200 info
->cs
.uses_local_invocation_idx
= true;
202 case nir_intrinsic_load_sample_id
:
203 info
->ps
.force_persample
= true;
205 case nir_intrinsic_load_sample_pos
:
206 info
->ps
.force_persample
= true;
208 case nir_intrinsic_load_view_index
:
209 info
->needs_multiview_view_index
= true;
210 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
211 info
->ps
.layer_input
= true;
213 case nir_intrinsic_load_invocation_id
:
214 info
->uses_invocation_id
= true;
216 case nir_intrinsic_load_primitive_id
:
217 info
->uses_prim_id
= true;
219 case nir_intrinsic_load_push_constant
:
220 info
->loads_push_constants
= true;
222 case nir_intrinsic_vulkan_resource_index
:
223 info
->desc_set_used_mask
|= (1 << nir_intrinsic_desc_set(instr
));
225 case nir_intrinsic_image_var_load
:
226 case nir_intrinsic_image_var_store
:
227 case nir_intrinsic_image_var_atomic_add
:
228 case nir_intrinsic_image_var_atomic_min
:
229 case nir_intrinsic_image_var_atomic_max
:
230 case nir_intrinsic_image_var_atomic_and
:
231 case nir_intrinsic_image_var_atomic_or
:
232 case nir_intrinsic_image_var_atomic_xor
:
233 case nir_intrinsic_image_var_atomic_exchange
:
234 case nir_intrinsic_image_var_atomic_comp_swap
:
235 case nir_intrinsic_image_var_size
: {
236 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
237 if(instr
->variables
[0]->deref
.child
)
238 type
= instr
->variables
[0]->deref
.child
->type
;
240 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
241 if (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
242 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) {
243 info
->ps
.layer_input
= true;
244 info
->ps
.uses_input_attachments
= true;
246 mark_sampler_desc(instr
->variables
[0]->var
, info
);
248 if (nir_intrinsic_image_var_store
||
249 nir_intrinsic_image_var_atomic_add
||
250 nir_intrinsic_image_var_atomic_min
||
251 nir_intrinsic_image_var_atomic_max
||
252 nir_intrinsic_image_var_atomic_and
||
253 nir_intrinsic_image_var_atomic_or
||
254 nir_intrinsic_image_var_atomic_xor
||
255 nir_intrinsic_image_var_atomic_exchange
||
256 nir_intrinsic_image_var_atomic_comp_swap
) {
257 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
258 info
->ps
.writes_memory
= true;
262 case nir_intrinsic_store_ssbo
:
263 case nir_intrinsic_ssbo_atomic_add
:
264 case nir_intrinsic_ssbo_atomic_imin
:
265 case nir_intrinsic_ssbo_atomic_umin
:
266 case nir_intrinsic_ssbo_atomic_imax
:
267 case nir_intrinsic_ssbo_atomic_umax
:
268 case nir_intrinsic_ssbo_atomic_and
:
269 case nir_intrinsic_ssbo_atomic_or
:
270 case nir_intrinsic_ssbo_atomic_xor
:
271 case nir_intrinsic_ssbo_atomic_exchange
:
272 case nir_intrinsic_ssbo_atomic_comp_swap
:
273 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
274 info
->ps
.writes_memory
= true;
276 case nir_intrinsic_load_var
:
277 gather_intrinsic_load_var_info(nir
, instr
, info
);
279 case nir_intrinsic_store_var
:
280 gather_intrinsic_store_var_info(nir
, instr
, info
);
288 gather_tex_info(const nir_shader
*nir
, const nir_tex_instr
*instr
,
289 struct radv_shader_info
*info
)
292 mark_sampler_desc(instr
->sampler
->var
, info
);
294 mark_sampler_desc(instr
->texture
->var
, info
);
298 gather_info_block(const nir_shader
*nir
, const nir_block
*block
,
299 struct radv_shader_info
*info
)
301 nir_foreach_instr(instr
, block
) {
302 switch (instr
->type
) {
303 case nir_instr_type_intrinsic
:
304 gather_intrinsic_info(nir
, nir_instr_as_intrinsic(instr
), info
);
306 case nir_instr_type_tex
:
307 gather_tex_info(nir
, nir_instr_as_tex(instr
), info
);
316 gather_info_input_decl_vs(const nir_shader
*nir
, const nir_variable
*var
,
317 struct radv_shader_info
*info
)
319 int idx
= var
->data
.location
;
321 if (idx
>= VERT_ATTRIB_GENERIC0
&& idx
<= VERT_ATTRIB_GENERIC15
)
322 info
->vs
.has_vertex_buffers
= true;
326 gather_info_input_decl_ps(const nir_shader
*nir
, const nir_variable
*var
,
327 struct radv_shader_info
*info
)
329 const struct glsl_type
*type
= glsl_without_array(var
->type
);
330 int idx
= var
->data
.location
;
333 case VARYING_SLOT_PNTC
:
334 info
->ps
.has_pcoord
= true;
336 case VARYING_SLOT_PRIMITIVE_ID
:
337 info
->ps
.prim_id_input
= true;
339 case VARYING_SLOT_LAYER
:
340 info
->ps
.layer_input
= true;
346 if (glsl_get_base_type(type
) == GLSL_TYPE_FLOAT
) {
347 if (var
->data
.sample
)
348 info
->ps
.force_persample
= true;
353 gather_info_input_decl(const nir_shader
*nir
, const nir_variable
*var
,
354 struct radv_shader_info
*info
)
356 switch (nir
->info
.stage
) {
357 case MESA_SHADER_VERTEX
:
358 gather_info_input_decl_vs(nir
, var
, info
);
360 case MESA_SHADER_FRAGMENT
:
361 gather_info_input_decl_ps(nir
, var
, info
);
369 gather_info_output_decl_ls(const nir_shader
*nir
, const nir_variable
*var
,
370 struct radv_shader_info
*info
)
372 int idx
= var
->data
.location
;
373 unsigned param
= shader_io_get_unique_index(idx
);
374 int num_slots
= glsl_count_attribute_slots(var
->type
, false);
375 if (idx
== VARYING_SLOT_CLIP_DIST0
)
376 num_slots
= (nir
->info
.clip_distance_array_size
+ nir
->info
.cull_distance_array_size
> 4) ? 2 : 1;
377 mark_ls_output(info
, param
, num_slots
);
381 gather_info_output_decl_ps(const nir_shader
*nir
, const nir_variable
*var
,
382 struct radv_shader_info
*info
)
384 int idx
= var
->data
.location
;
387 case FRAG_RESULT_DEPTH
:
388 info
->ps
.writes_z
= true;
390 case FRAG_RESULT_STENCIL
:
391 info
->ps
.writes_stencil
= true;
393 case FRAG_RESULT_SAMPLE_MASK
:
394 info
->ps
.writes_sample_mask
= true;
402 gather_info_output_decl(const nir_shader
*nir
, const nir_variable
*var
,
403 struct radv_shader_info
*info
,
404 const struct radv_nir_compiler_options
*options
)
406 switch (nir
->info
.stage
) {
407 case MESA_SHADER_FRAGMENT
:
408 gather_info_output_decl_ps(nir
, var
, info
);
410 case MESA_SHADER_VERTEX
:
411 if (options
->key
.vs
.as_ls
)
412 gather_info_output_decl_ls(nir
, var
, info
);
420 radv_nir_shader_info_pass(const struct nir_shader
*nir
,
421 const struct radv_nir_compiler_options
*options
,
422 struct radv_shader_info
*info
)
424 struct nir_function
*func
=
425 (struct nir_function
*)exec_list_get_head_const(&nir
->functions
);
427 if (options
->layout
&& options
->layout
->dynamic_offset_count
)
428 info
->loads_push_constants
= true;
430 nir_foreach_variable(variable
, &nir
->inputs
)
431 gather_info_input_decl(nir
, variable
, info
);
433 nir_foreach_block(block
, func
->impl
) {
434 gather_info_block(nir
, block
, info
);
437 nir_foreach_variable(variable
, &nir
->outputs
)
438 gather_info_output_decl(nir
, variable
, info
, options
);