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"
26 #include "nir/nir_deref.h"
27 #include "nir/nir_xfb_info.h"
29 static void mark_sampler_desc(const nir_variable
*var
,
30 struct radv_shader_info
*info
)
32 info
->desc_set_used_mask
|= (1 << var
->data
.descriptor_set
);
35 static void mark_ls_output(struct radv_shader_info
*info
,
36 uint32_t param
, int num_slots
)
38 uint64_t mask
= (1ull << num_slots
) - 1ull;
39 info
->vs
.ls_outputs_written
|= (mask
<< param
);
42 static void mark_tess_output(struct radv_shader_info
*info
,
43 bool is_patch
, uint32_t param
, int num_slots
)
45 uint64_t mask
= (1ull << num_slots
) - 1ull;
47 info
->tcs
.patch_outputs_written
|= (mask
<< param
);
49 info
->tcs
.outputs_written
|= (mask
<< param
);
53 get_deref_offset(nir_deref_instr
*instr
,
56 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
60 if (var
->data
.compact
) {
61 assert(instr
->deref_type
== nir_deref_type_array
);
62 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
64 *const_out
= v
->u32
[0];
68 nir_deref_path_init(&path
, instr
, NULL
);
70 uint32_t const_offset
= 0;
72 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
73 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
74 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
75 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
77 for (unsigned i
= 0; i
< index
; i
++) {
78 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
79 const_offset
+= glsl_count_attribute_slots(ft
, false);
81 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
82 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, false);
83 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
85 const_offset
+= v
->u32
[0] * size
;
87 unreachable("Uhandled deref type in get_deref_instr_offset");
90 *const_out
= const_offset
;
92 nir_deref_path_finish(&path
);
96 gather_intrinsic_load_deref_info(const nir_shader
*nir
,
97 const nir_intrinsic_instr
*instr
,
98 struct radv_shader_info
*info
)
100 switch (nir
->info
.stage
) {
101 case MESA_SHADER_VERTEX
: {
102 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
104 if (var
->data
.mode
== nir_var_shader_in
) {
105 unsigned idx
= var
->data
.location
;
106 uint8_t mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
108 info
->vs
.input_usage_mask
[idx
] |=
109 mask
<< var
->data
.location_frac
;
119 set_output_usage_mask(const nir_shader
*nir
, const nir_intrinsic_instr
*instr
,
120 uint8_t *output_usage_mask
)
122 nir_deref_instr
*deref_instr
=
123 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
124 nir_variable
*var
= nir_deref_instr_get_variable(deref_instr
);
125 unsigned attrib_count
= glsl_count_attribute_slots(var
->type
, false);
126 unsigned idx
= var
->data
.location
;
127 unsigned comp
= var
->data
.location_frac
;
128 unsigned const_offset
= 0;
130 get_deref_offset(deref_instr
, &const_offset
);
132 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
133 /* Special case for clip/cull distances because there are
134 * combined into a single array that contains both.
136 output_usage_mask
[idx
] |= 1 << const_offset
;
140 for (unsigned i
= 0; i
< attrib_count
; i
++) {
141 output_usage_mask
[idx
+ i
+ const_offset
] |=
142 instr
->const_index
[0] << comp
;
147 gather_intrinsic_store_deref_info(const nir_shader
*nir
,
148 const nir_intrinsic_instr
*instr
,
149 struct radv_shader_info
*info
)
151 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
153 if (var
->data
.mode
== nir_var_shader_out
) {
154 unsigned idx
= var
->data
.location
;
156 switch (nir
->info
.stage
) {
157 case MESA_SHADER_VERTEX
:
158 set_output_usage_mask(nir
, instr
,
159 info
->vs
.output_usage_mask
);
161 case MESA_SHADER_GEOMETRY
:
162 set_output_usage_mask(nir
, instr
,
163 info
->gs
.output_usage_mask
);
165 case MESA_SHADER_TESS_EVAL
:
166 set_output_usage_mask(nir
, instr
,
167 info
->tes
.output_usage_mask
);
169 case MESA_SHADER_TESS_CTRL
: {
170 unsigned param
= shader_io_get_unique_index(idx
);
171 const struct glsl_type
*type
= var
->type
;
173 if (!var
->data
.patch
)
174 type
= glsl_get_array_element(var
->type
);
177 var
->data
.compact
? DIV_ROUND_UP(glsl_get_length(type
), 4)
178 : glsl_count_attribute_slots(type
, false);
180 if (idx
== VARYING_SLOT_CLIP_DIST0
)
181 slots
= (nir
->info
.clip_distance_array_size
+
182 nir
->info
.cull_distance_array_size
> 4) ? 2 : 1;
184 mark_tess_output(info
, var
->data
.patch
, param
, slots
);
194 gather_intrinsic_info(const nir_shader
*nir
, const nir_intrinsic_instr
*instr
,
195 struct radv_shader_info
*info
)
197 switch (instr
->intrinsic
) {
198 case nir_intrinsic_interp_deref_at_sample
:
199 info
->ps
.needs_sample_positions
= true;
201 case nir_intrinsic_load_draw_id
:
202 info
->vs
.needs_draw_id
= true;
204 case nir_intrinsic_load_instance_id
:
205 info
->vs
.needs_instance_id
= true;
207 case nir_intrinsic_load_num_work_groups
:
208 info
->cs
.uses_grid_size
= true;
210 case nir_intrinsic_load_local_invocation_id
:
211 case nir_intrinsic_load_work_group_id
: {
212 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
214 unsigned i
= u_bit_scan(&mask
);
216 if (instr
->intrinsic
== nir_intrinsic_load_work_group_id
)
217 info
->cs
.uses_block_id
[i
] = true;
219 info
->cs
.uses_thread_id
[i
] = true;
223 case nir_intrinsic_load_local_invocation_index
:
224 case nir_intrinsic_load_subgroup_id
:
225 case nir_intrinsic_load_num_subgroups
:
226 info
->cs
.uses_local_invocation_idx
= true;
228 case nir_intrinsic_load_sample_id
:
229 info
->ps
.force_persample
= true;
231 case nir_intrinsic_load_sample_pos
:
232 info
->ps
.force_persample
= true;
234 case nir_intrinsic_load_view_index
:
235 info
->needs_multiview_view_index
= true;
236 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
237 info
->ps
.layer_input
= true;
239 case nir_intrinsic_load_invocation_id
:
240 info
->uses_invocation_id
= true;
242 case nir_intrinsic_load_primitive_id
:
243 info
->uses_prim_id
= true;
245 case nir_intrinsic_load_push_constant
:
246 info
->loads_push_constants
= true;
248 case nir_intrinsic_vulkan_resource_index
:
249 info
->desc_set_used_mask
|= (1 << nir_intrinsic_desc_set(instr
));
251 case nir_intrinsic_image_deref_load
:
252 case nir_intrinsic_image_deref_store
:
253 case nir_intrinsic_image_deref_atomic_add
:
254 case nir_intrinsic_image_deref_atomic_min
:
255 case nir_intrinsic_image_deref_atomic_max
:
256 case nir_intrinsic_image_deref_atomic_and
:
257 case nir_intrinsic_image_deref_atomic_or
:
258 case nir_intrinsic_image_deref_atomic_xor
:
259 case nir_intrinsic_image_deref_atomic_exchange
:
260 case nir_intrinsic_image_deref_atomic_comp_swap
:
261 case nir_intrinsic_image_deref_size
: {
262 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
263 const struct glsl_type
*type
= glsl_without_array(var
->type
);
265 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
266 if (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
267 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) {
268 info
->ps
.layer_input
= true;
269 info
->ps
.uses_input_attachments
= true;
271 mark_sampler_desc(var
, info
);
273 if (instr
->intrinsic
== nir_intrinsic_image_deref_store
||
274 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_add
||
275 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_min
||
276 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_max
||
277 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_and
||
278 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_or
||
279 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_xor
||
280 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_exchange
||
281 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
) {
282 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
283 info
->ps
.writes_memory
= true;
287 case nir_intrinsic_store_ssbo
:
288 case nir_intrinsic_ssbo_atomic_add
:
289 case nir_intrinsic_ssbo_atomic_imin
:
290 case nir_intrinsic_ssbo_atomic_umin
:
291 case nir_intrinsic_ssbo_atomic_imax
:
292 case nir_intrinsic_ssbo_atomic_umax
:
293 case nir_intrinsic_ssbo_atomic_and
:
294 case nir_intrinsic_ssbo_atomic_or
:
295 case nir_intrinsic_ssbo_atomic_xor
:
296 case nir_intrinsic_ssbo_atomic_exchange
:
297 case nir_intrinsic_ssbo_atomic_comp_swap
:
298 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
299 info
->ps
.writes_memory
= true;
301 case nir_intrinsic_load_deref
:
302 gather_intrinsic_load_deref_info(nir
, instr
, info
);
304 case nir_intrinsic_store_deref
:
305 gather_intrinsic_store_deref_info(nir
, instr
, info
);
313 gather_tex_info(const nir_shader
*nir
, const nir_tex_instr
*instr
,
314 struct radv_shader_info
*info
)
316 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
317 switch (instr
->src
[i
].src_type
) {
318 case nir_tex_src_texture_deref
:
319 mark_sampler_desc(nir_deref_instr_get_variable(nir_src_as_deref(instr
->src
[i
].src
)), info
);
321 case nir_tex_src_sampler_deref
:
322 mark_sampler_desc(nir_deref_instr_get_variable(nir_src_as_deref(instr
->src
[i
].src
)), info
);
331 gather_info_block(const nir_shader
*nir
, const nir_block
*block
,
332 struct radv_shader_info
*info
)
334 nir_foreach_instr(instr
, block
) {
335 switch (instr
->type
) {
336 case nir_instr_type_intrinsic
:
337 gather_intrinsic_info(nir
, nir_instr_as_intrinsic(instr
), info
);
339 case nir_instr_type_tex
:
340 gather_tex_info(nir
, nir_instr_as_tex(instr
), info
);
349 gather_info_input_decl_vs(const nir_shader
*nir
, const nir_variable
*var
,
350 struct radv_shader_info
*info
)
352 int idx
= var
->data
.location
;
354 if (idx
>= VERT_ATTRIB_GENERIC0
&& idx
<= VERT_ATTRIB_GENERIC15
)
355 info
->vs
.has_vertex_buffers
= true;
359 gather_info_input_decl_ps(const nir_shader
*nir
, const nir_variable
*var
,
360 struct radv_shader_info
*info
)
362 unsigned attrib_count
= glsl_count_attribute_slots(var
->type
, false);
363 const struct glsl_type
*type
= glsl_without_array(var
->type
);
364 int idx
= var
->data
.location
;
367 case VARYING_SLOT_PNTC
:
368 info
->ps
.has_pcoord
= true;
370 case VARYING_SLOT_PRIMITIVE_ID
:
371 info
->ps
.prim_id_input
= true;
373 case VARYING_SLOT_LAYER
:
374 info
->ps
.layer_input
= true;
376 case VARYING_SLOT_CLIP_DIST0
:
377 info
->ps
.num_input_clips_culls
= attrib_count
;
383 if (glsl_get_base_type(type
) == GLSL_TYPE_FLOAT
) {
384 if (var
->data
.sample
)
385 info
->ps
.force_persample
= true;
390 gather_info_input_decl(const nir_shader
*nir
, const nir_variable
*var
,
391 struct radv_shader_info
*info
)
393 switch (nir
->info
.stage
) {
394 case MESA_SHADER_VERTEX
:
395 gather_info_input_decl_vs(nir
, var
, info
);
397 case MESA_SHADER_FRAGMENT
:
398 gather_info_input_decl_ps(nir
, var
, info
);
406 gather_info_output_decl_ls(const nir_shader
*nir
, const nir_variable
*var
,
407 struct radv_shader_info
*info
)
409 int idx
= var
->data
.location
;
410 unsigned param
= shader_io_get_unique_index(idx
);
411 int num_slots
= glsl_count_attribute_slots(var
->type
, false);
412 if (idx
== VARYING_SLOT_CLIP_DIST0
)
413 num_slots
= (nir
->info
.clip_distance_array_size
+ nir
->info
.cull_distance_array_size
> 4) ? 2 : 1;
414 mark_ls_output(info
, param
, num_slots
);
418 gather_info_output_decl_ps(const nir_shader
*nir
, const nir_variable
*var
,
419 struct radv_shader_info
*info
)
421 int idx
= var
->data
.location
;
424 case FRAG_RESULT_DEPTH
:
425 info
->ps
.writes_z
= true;
427 case FRAG_RESULT_STENCIL
:
428 info
->ps
.writes_stencil
= true;
430 case FRAG_RESULT_SAMPLE_MASK
:
431 info
->ps
.writes_sample_mask
= true;
439 gather_info_output_decl_gs(const nir_shader
*nir
, const nir_variable
*var
,
440 struct radv_shader_info
*info
)
442 unsigned num_components
= glsl_get_component_slots(var
->type
);
443 unsigned stream
= var
->data
.stream
;
444 unsigned idx
= var
->data
.location
;
448 info
->gs
.max_stream
= MAX2(info
->gs
.max_stream
, stream
);
449 info
->gs
.num_stream_output_components
[stream
] += num_components
;
450 info
->gs
.output_streams
[idx
] = stream
;
454 gather_info_output_decl(const nir_shader
*nir
, const nir_variable
*var
,
455 struct radv_shader_info
*info
,
456 const struct radv_nir_compiler_options
*options
)
458 switch (nir
->info
.stage
) {
459 case MESA_SHADER_FRAGMENT
:
460 gather_info_output_decl_ps(nir
, var
, info
);
462 case MESA_SHADER_VERTEX
:
463 if (options
->key
.vs
.as_ls
)
464 gather_info_output_decl_ls(nir
, var
, info
);
466 case MESA_SHADER_GEOMETRY
:
467 gather_info_output_decl_gs(nir
, var
, info
);
475 gather_xfb_info(const nir_shader
*nir
, struct radv_shader_info
*info
)
477 nir_xfb_info
*xfb
= nir_gather_xfb_info(nir
, NULL
);
478 struct radv_streamout_info
*so
= &info
->so
;
483 assert(xfb
->output_count
< MAX_SO_OUTPUTS
);
484 so
->num_outputs
= xfb
->output_count
;
486 for (unsigned i
= 0; i
< xfb
->output_count
; i
++) {
487 struct radv_stream_output
*output
= &so
->outputs
[i
];
489 output
->buffer
= xfb
->outputs
[i
].buffer
;
490 output
->stream
= xfb
->buffer_to_stream
[xfb
->outputs
[i
].buffer
];
491 output
->offset
= xfb
->outputs
[i
].offset
;
492 output
->location
= xfb
->outputs
[i
].location
;
493 output
->component_mask
= xfb
->outputs
[i
].component_mask
;
495 so
->enabled_stream_buffers_mask
|=
496 (1 << output
->buffer
) << (output
->stream
* 4);
500 for (unsigned i
= 0; i
< NIR_MAX_XFB_BUFFERS
; i
++) {
501 so
->strides
[i
] = xfb
->strides
[i
] / 4;
508 radv_nir_shader_info_pass(const struct nir_shader
*nir
,
509 const struct radv_nir_compiler_options
*options
,
510 struct radv_shader_info
*info
)
512 struct nir_function
*func
=
513 (struct nir_function
*)exec_list_get_head_const(&nir
->functions
);
515 if (options
->layout
&& options
->layout
->dynamic_offset_count
&&
516 (options
->layout
->dynamic_shader_stages
& mesa_to_vk_shader_stage(nir
->info
.stage
))) {
517 info
->loads_push_constants
= true;
520 nir_foreach_variable(variable
, &nir
->inputs
)
521 gather_info_input_decl(nir
, variable
, info
);
523 nir_foreach_block(block
, func
->impl
) {
524 gather_info_block(nir
, block
, info
);
527 nir_foreach_variable(variable
, &nir
->outputs
)
528 gather_info_output_decl(nir
, variable
, info
, options
);
530 if (nir
->info
.stage
== MESA_SHADER_VERTEX
||
531 nir
->info
.stage
== MESA_SHADER_TESS_EVAL
||
532 nir
->info
.stage
== MESA_SHADER_GEOMETRY
)
533 gather_xfb_info(nir
, info
);