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 *const_out
= nir_src_as_uint(instr
->arr
.index
);
66 nir_deref_path_init(&path
, instr
, NULL
);
68 uint32_t const_offset
= 0;
70 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
71 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
72 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
73 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
75 for (unsigned i
= 0; i
< index
; i
++) {
76 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
77 const_offset
+= glsl_count_attribute_slots(ft
, false);
79 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
80 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, false);
81 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
))
82 const_offset
+= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
) * size
;
84 unreachable("Uhandled deref type in get_deref_instr_offset");
87 *const_out
= const_offset
;
89 nir_deref_path_finish(&path
);
93 gather_intrinsic_load_deref_info(const nir_shader
*nir
,
94 const nir_intrinsic_instr
*instr
,
95 struct radv_shader_info
*info
)
97 switch (nir
->info
.stage
) {
98 case MESA_SHADER_VERTEX
: {
99 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
101 if (var
&& var
->data
.mode
== nir_var_shader_in
) {
102 unsigned idx
= var
->data
.location
;
103 uint8_t mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
105 info
->vs
.input_usage_mask
[idx
] |=
106 mask
<< var
->data
.location_frac
;
116 widen_writemask(uint32_t wrmask
)
118 uint32_t new_wrmask
= 0;
119 for(unsigned i
= 0; i
< 4; i
++)
120 new_wrmask
|= (wrmask
& (1 << i
) ? 0x3 : 0x0) << (i
* 2);
125 set_output_usage_mask(const nir_shader
*nir
, const nir_intrinsic_instr
*instr
,
126 uint8_t *output_usage_mask
)
128 nir_deref_instr
*deref_instr
=
129 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
130 nir_variable
*var
= nir_deref_instr_get_variable(deref_instr
);
131 unsigned attrib_count
= glsl_count_attribute_slots(deref_instr
->type
, false);
132 unsigned idx
= var
->data
.location
;
133 unsigned comp
= var
->data
.location_frac
;
134 unsigned const_offset
= 0;
136 get_deref_offset(deref_instr
, &const_offset
);
138 if (var
->data
.compact
) {
139 assert(!glsl_type_is_64bit(deref_instr
->type
));
140 const_offset
+= comp
;
141 output_usage_mask
[idx
+ const_offset
/ 4] |= 1 << (const_offset
% 4);
145 uint32_t wrmask
= nir_intrinsic_write_mask(instr
);
146 if (glsl_type_is_64bit(deref_instr
->type
))
147 wrmask
= widen_writemask(wrmask
);
149 for (unsigned i
= 0; i
< attrib_count
; i
++)
150 output_usage_mask
[idx
+ i
+ const_offset
] |=
151 ((wrmask
>> (i
* 4)) & 0xf) << comp
;
155 set_writes_memory(const nir_shader
*nir
, struct radv_shader_info
*info
)
157 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
158 info
->ps
.writes_memory
= true;
159 else if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
)
160 info
->gs
.writes_memory
= true;
164 gather_intrinsic_store_deref_info(const nir_shader
*nir
,
165 const nir_intrinsic_instr
*instr
,
166 struct radv_shader_info
*info
)
168 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
170 if (var
&& var
->data
.mode
== nir_var_shader_out
) {
171 unsigned idx
= var
->data
.location
;
173 switch (nir
->info
.stage
) {
174 case MESA_SHADER_VERTEX
:
175 set_output_usage_mask(nir
, instr
,
176 info
->vs
.output_usage_mask
);
178 case MESA_SHADER_GEOMETRY
:
179 set_output_usage_mask(nir
, instr
,
180 info
->gs
.output_usage_mask
);
182 case MESA_SHADER_TESS_EVAL
:
183 set_output_usage_mask(nir
, instr
,
184 info
->tes
.output_usage_mask
);
186 case MESA_SHADER_TESS_CTRL
: {
187 unsigned param
= shader_io_get_unique_index(idx
);
188 const struct glsl_type
*type
= var
->type
;
190 if (!var
->data
.patch
)
191 type
= glsl_get_array_element(var
->type
);
194 var
->data
.compact
? DIV_ROUND_UP(var
->data
.location_frac
+ glsl_get_length(type
), 4)
195 : glsl_count_attribute_slots(type
, false);
197 mark_tess_output(info
, var
->data
.patch
, param
, slots
);
207 gather_push_constant_info(const nir_shader
*nir
,
208 const nir_intrinsic_instr
*instr
,
209 struct radv_shader_info
*info
)
211 int base
= nir_intrinsic_base(instr
);
213 if (!nir_src_is_const(instr
->src
[0])) {
214 info
->has_indirect_push_constants
= true;
216 uint32_t min
= base
+ nir_src_as_uint(instr
->src
[0]);
217 uint32_t max
= min
+ instr
->num_components
* 4;
219 info
->max_push_constant_used
=
220 MAX2(max
, info
->max_push_constant_used
);
221 info
->min_push_constant_used
=
222 MIN2(min
, info
->min_push_constant_used
);
225 if (instr
->dest
.ssa
.bit_size
!= 32)
226 info
->has_only_32bit_push_constants
= false;
228 info
->loads_push_constants
= true;
232 gather_intrinsic_info(const nir_shader
*nir
, const nir_intrinsic_instr
*instr
,
233 struct radv_shader_info
*info
)
235 switch (instr
->intrinsic
) {
236 case nir_intrinsic_load_barycentric_at_sample
:
237 info
->ps
.needs_sample_positions
= true;
239 case nir_intrinsic_load_draw_id
:
240 info
->vs
.needs_draw_id
= true;
242 case nir_intrinsic_load_instance_id
:
243 info
->vs
.needs_instance_id
= true;
245 case nir_intrinsic_load_num_work_groups
:
246 info
->cs
.uses_grid_size
= true;
248 case nir_intrinsic_load_local_invocation_id
:
249 case nir_intrinsic_load_work_group_id
: {
250 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
252 unsigned i
= u_bit_scan(&mask
);
254 if (instr
->intrinsic
== nir_intrinsic_load_work_group_id
)
255 info
->cs
.uses_block_id
[i
] = true;
257 info
->cs
.uses_thread_id
[i
] = true;
261 case nir_intrinsic_load_local_invocation_index
:
262 case nir_intrinsic_load_subgroup_id
:
263 case nir_intrinsic_load_num_subgroups
:
264 info
->cs
.uses_local_invocation_idx
= true;
266 case nir_intrinsic_load_sample_id
:
267 info
->ps
.force_persample
= true;
269 case nir_intrinsic_load_sample_pos
:
270 info
->ps
.force_persample
= true;
272 case nir_intrinsic_load_view_index
:
273 info
->needs_multiview_view_index
= true;
274 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
275 info
->ps
.layer_input
= true;
277 case nir_intrinsic_load_layer_id
:
278 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
279 info
->ps
.layer_input
= true;
281 case nir_intrinsic_load_invocation_id
:
282 info
->uses_invocation_id
= true;
284 case nir_intrinsic_load_primitive_id
:
285 info
->uses_prim_id
= true;
287 case nir_intrinsic_load_push_constant
:
288 gather_push_constant_info(nir
, instr
, info
);
290 case nir_intrinsic_vulkan_resource_index
:
291 info
->desc_set_used_mask
|= (1 << nir_intrinsic_desc_set(instr
));
293 case nir_intrinsic_image_deref_load
:
294 case nir_intrinsic_image_deref_store
:
295 case nir_intrinsic_image_deref_atomic_add
:
296 case nir_intrinsic_image_deref_atomic_imin
:
297 case nir_intrinsic_image_deref_atomic_umin
:
298 case nir_intrinsic_image_deref_atomic_imax
:
299 case nir_intrinsic_image_deref_atomic_umax
:
300 case nir_intrinsic_image_deref_atomic_and
:
301 case nir_intrinsic_image_deref_atomic_or
:
302 case nir_intrinsic_image_deref_atomic_xor
:
303 case nir_intrinsic_image_deref_atomic_exchange
:
304 case nir_intrinsic_image_deref_atomic_comp_swap
:
305 case nir_intrinsic_image_deref_size
: {
306 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
307 mark_sampler_desc(var
, info
);
309 if (instr
->intrinsic
== nir_intrinsic_image_deref_store
||
310 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_add
||
311 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_imin
||
312 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_umin
||
313 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_imax
||
314 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_umax
||
315 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_and
||
316 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_or
||
317 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_xor
||
318 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_exchange
||
319 instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
) {
320 set_writes_memory(nir
, info
);
324 case nir_intrinsic_store_ssbo
:
325 case nir_intrinsic_ssbo_atomic_add
:
326 case nir_intrinsic_ssbo_atomic_imin
:
327 case nir_intrinsic_ssbo_atomic_umin
:
328 case nir_intrinsic_ssbo_atomic_imax
:
329 case nir_intrinsic_ssbo_atomic_umax
:
330 case nir_intrinsic_ssbo_atomic_and
:
331 case nir_intrinsic_ssbo_atomic_or
:
332 case nir_intrinsic_ssbo_atomic_xor
:
333 case nir_intrinsic_ssbo_atomic_exchange
:
334 case nir_intrinsic_ssbo_atomic_comp_swap
:
335 set_writes_memory(nir
, info
);
337 case nir_intrinsic_load_deref
:
338 gather_intrinsic_load_deref_info(nir
, instr
, info
);
340 case nir_intrinsic_store_deref
:
341 gather_intrinsic_store_deref_info(nir
, instr
, info
);
343 case nir_intrinsic_deref_atomic_add
:
344 case nir_intrinsic_deref_atomic_imin
:
345 case nir_intrinsic_deref_atomic_umin
:
346 case nir_intrinsic_deref_atomic_imax
:
347 case nir_intrinsic_deref_atomic_umax
:
348 case nir_intrinsic_deref_atomic_and
:
349 case nir_intrinsic_deref_atomic_or
:
350 case nir_intrinsic_deref_atomic_xor
:
351 case nir_intrinsic_deref_atomic_exchange
:
352 case nir_intrinsic_deref_atomic_comp_swap
: {
353 if (nir_src_as_deref(instr
->src
[0])->mode
& (nir_var_mem_global
| nir_var_mem_ssbo
))
354 set_writes_memory(nir
, info
);
363 gather_tex_info(const nir_shader
*nir
, const nir_tex_instr
*instr
,
364 struct radv_shader_info
*info
)
366 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
367 switch (instr
->src
[i
].src_type
) {
368 case nir_tex_src_texture_deref
:
369 mark_sampler_desc(nir_deref_instr_get_variable(nir_src_as_deref(instr
->src
[i
].src
)), info
);
371 case nir_tex_src_sampler_deref
:
372 mark_sampler_desc(nir_deref_instr_get_variable(nir_src_as_deref(instr
->src
[i
].src
)), info
);
381 gather_info_block(const nir_shader
*nir
, const nir_block
*block
,
382 struct radv_shader_info
*info
)
384 nir_foreach_instr(instr
, block
) {
385 switch (instr
->type
) {
386 case nir_instr_type_intrinsic
:
387 gather_intrinsic_info(nir
, nir_instr_as_intrinsic(instr
), info
);
389 case nir_instr_type_tex
:
390 gather_tex_info(nir
, nir_instr_as_tex(instr
), info
);
399 gather_info_input_decl_vs(const nir_shader
*nir
, const nir_variable
*var
,
400 struct radv_shader_info
*info
,
401 const struct radv_shader_variant_key
*key
)
403 unsigned attrib_count
= glsl_count_attribute_slots(var
->type
, true);
404 int idx
= var
->data
.location
;
406 if (idx
>= VERT_ATTRIB_GENERIC0
&& idx
<= VERT_ATTRIB_GENERIC15
)
407 info
->vs
.has_vertex_buffers
= true;
409 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
410 unsigned attrib_index
= var
->data
.location
+ i
- VERT_ATTRIB_GENERIC0
;
412 if (key
->vs
.instance_rate_inputs
& (1u << attrib_index
))
413 info
->vs
.needs_instance_id
= true;
418 mark_16bit_ps_input(struct radv_shader_info
*info
, const struct glsl_type
*type
,
421 if (glsl_type_is_scalar(type
) || glsl_type_is_vector(type
) || glsl_type_is_matrix(type
)) {
422 unsigned attrib_count
= glsl_count_attribute_slots(type
, false);
423 if (glsl_type_is_16bit(type
)) {
424 info
->ps
.float16_shaded_mask
|= ((1ull << attrib_count
) - 1) << location
;
426 } else if (glsl_type_is_array(type
)) {
427 unsigned stride
= glsl_count_attribute_slots(glsl_get_array_element(type
), false);
428 for (unsigned i
= 0; i
< glsl_get_length(type
); ++i
) {
429 mark_16bit_ps_input(info
, glsl_get_array_element(type
), location
+ i
* stride
);
432 assert(glsl_type_is_struct_or_ifc(type
));
433 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
434 mark_16bit_ps_input(info
, glsl_get_struct_field(type
, i
), location
);
435 location
+= glsl_count_attribute_slots(glsl_get_struct_field(type
, i
), false);
440 gather_info_input_decl_ps(const nir_shader
*nir
, const nir_variable
*var
,
441 struct radv_shader_info
*info
)
443 unsigned attrib_count
= glsl_count_attribute_slots(var
->type
, false);
444 const struct glsl_type
*type
= glsl_without_array(var
->type
);
445 int idx
= var
->data
.location
;
448 case VARYING_SLOT_PNTC
:
449 info
->ps
.has_pcoord
= true;
451 case VARYING_SLOT_PRIMITIVE_ID
:
452 info
->ps
.prim_id_input
= true;
454 case VARYING_SLOT_LAYER
:
455 info
->ps
.layer_input
= true;
457 case VARYING_SLOT_CLIP_DIST0
:
458 case VARYING_SLOT_CLIP_DIST1
:
459 info
->ps
.num_input_clips_culls
+= attrib_count
;
461 case VARYING_SLOT_VIEWPORT
:
462 info
->ps
.viewport_index_input
= true;
468 if (glsl_get_base_type(type
) == GLSL_TYPE_FLOAT
) {
469 if (var
->data
.sample
)
470 info
->ps
.force_persample
= true;
473 if (var
->data
.compact
) {
474 unsigned component_count
= var
->data
.location_frac
+
475 glsl_get_length(var
->type
);
476 attrib_count
= (component_count
+ 3) / 4;
478 mark_16bit_ps_input(info
, var
->type
, var
->data
.driver_location
);
481 uint64_t mask
= ((1ull << attrib_count
) - 1);
483 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
484 info
->ps
.flat_shaded_mask
|= mask
<< var
->data
.driver_location
;
485 if (var
->data
.interpolation
== INTERP_MODE_EXPLICIT
)
486 info
->ps
.explicit_shaded_mask
|= mask
<< var
->data
.driver_location
;
488 if (var
->data
.location
>= VARYING_SLOT_VAR0
)
489 info
->ps
.input_mask
|= mask
<< (var
->data
.location
- VARYING_SLOT_VAR0
);
493 gather_info_input_decl(const nir_shader
*nir
, const nir_variable
*var
,
494 struct radv_shader_info
*info
,
495 const struct radv_shader_variant_key
*key
)
497 switch (nir
->info
.stage
) {
498 case MESA_SHADER_VERTEX
:
499 gather_info_input_decl_vs(nir
, var
, info
, key
);
501 case MESA_SHADER_FRAGMENT
:
502 gather_info_input_decl_ps(nir
, var
, info
);
510 gather_info_output_decl_ls(const nir_shader
*nir
, const nir_variable
*var
,
511 struct radv_shader_info
*info
)
513 int idx
= var
->data
.location
;
514 unsigned param
= shader_io_get_unique_index(idx
);
515 int num_slots
= glsl_count_attribute_slots(var
->type
, false);
516 if (var
->data
.compact
)
517 num_slots
= DIV_ROUND_UP(var
->data
.location_frac
+ glsl_get_length(var
->type
), 4);
518 mark_ls_output(info
, param
, num_slots
);
522 gather_info_output_decl_ps(const nir_shader
*nir
, const nir_variable
*var
,
523 struct radv_shader_info
*info
)
525 int idx
= var
->data
.location
;
528 case FRAG_RESULT_DEPTH
:
529 info
->ps
.writes_z
= true;
531 case FRAG_RESULT_STENCIL
:
532 info
->ps
.writes_stencil
= true;
534 case FRAG_RESULT_SAMPLE_MASK
:
535 info
->ps
.writes_sample_mask
= true;
543 gather_info_output_decl_gs(const nir_shader
*nir
, const nir_variable
*var
,
544 struct radv_shader_info
*info
)
546 unsigned num_components
= glsl_get_component_slots(var
->type
);
547 unsigned stream
= var
->data
.stream
;
548 unsigned idx
= var
->data
.location
;
552 info
->gs
.max_stream
= MAX2(info
->gs
.max_stream
, stream
);
553 info
->gs
.num_stream_output_components
[stream
] += num_components
;
554 info
->gs
.output_streams
[idx
] = stream
;
558 gather_info_output_decl(const nir_shader
*nir
, const nir_variable
*var
,
559 struct radv_shader_info
*info
,
560 const struct radv_shader_variant_key
*key
)
562 struct radv_vs_output_info
*vs_info
= NULL
;
564 switch (nir
->info
.stage
) {
565 case MESA_SHADER_FRAGMENT
:
566 gather_info_output_decl_ps(nir
, var
, info
);
568 case MESA_SHADER_VERTEX
:
569 if (!key
->vs_common_out
.as_ls
&&
570 !key
->vs_common_out
.as_es
)
571 vs_info
= &info
->vs
.outinfo
;
573 if (key
->vs_common_out
.as_ls
)
574 gather_info_output_decl_ls(nir
, var
, info
);
575 else if (key
->vs_common_out
.as_ngg
)
576 gather_info_output_decl_gs(nir
, var
, info
);
578 case MESA_SHADER_GEOMETRY
:
579 vs_info
= &info
->vs
.outinfo
;
580 gather_info_output_decl_gs(nir
, var
, info
);
582 case MESA_SHADER_TESS_EVAL
:
583 if (!key
->vs_common_out
.as_es
)
584 vs_info
= &info
->tes
.outinfo
;
591 switch (var
->data
.location
) {
592 case VARYING_SLOT_CLIP_DIST0
:
593 vs_info
->clip_dist_mask
=
594 (1 << nir
->info
.clip_distance_array_size
) - 1;
595 vs_info
->cull_dist_mask
=
596 (1 << nir
->info
.cull_distance_array_size
) - 1;
597 vs_info
->cull_dist_mask
<<= nir
->info
.clip_distance_array_size
;
599 case VARYING_SLOT_PSIZ
:
600 vs_info
->writes_pointsize
= true;
602 case VARYING_SLOT_VIEWPORT
:
603 vs_info
->writes_viewport_index
= true;
605 case VARYING_SLOT_LAYER
:
606 vs_info
->writes_layer
= true;
615 gather_xfb_info(const nir_shader
*nir
, struct radv_shader_info
*info
)
617 nir_xfb_info
*xfb
= nir_gather_xfb_info(nir
, NULL
);
618 struct radv_streamout_info
*so
= &info
->so
;
623 assert(xfb
->output_count
< MAX_SO_OUTPUTS
);
624 so
->num_outputs
= xfb
->output_count
;
626 for (unsigned i
= 0; i
< xfb
->output_count
; i
++) {
627 struct radv_stream_output
*output
= &so
->outputs
[i
];
629 output
->buffer
= xfb
->outputs
[i
].buffer
;
630 output
->stream
= xfb
->buffer_to_stream
[xfb
->outputs
[i
].buffer
];
631 output
->offset
= xfb
->outputs
[i
].offset
;
632 output
->location
= xfb
->outputs
[i
].location
;
633 output
->component_mask
= xfb
->outputs
[i
].component_mask
;
635 so
->enabled_stream_buffers_mask
|=
636 (1 << output
->buffer
) << (output
->stream
* 4);
640 for (unsigned i
= 0; i
< NIR_MAX_XFB_BUFFERS
; i
++) {
641 so
->strides
[i
] = xfb
->buffers
[i
].stride
/ 4;
648 radv_nir_shader_info_init(struct radv_shader_info
*info
)
650 /* Assume that shaders only have 32-bit push constants by default. */
651 info
->min_push_constant_used
= UINT8_MAX
;
652 info
->has_only_32bit_push_constants
= true;
656 radv_nir_shader_info_pass(const struct nir_shader
*nir
,
657 const struct radv_pipeline_layout
*layout
,
658 const struct radv_shader_variant_key
*key
,
659 struct radv_shader_info
*info
)
661 struct nir_function
*func
=
662 (struct nir_function
*)exec_list_get_head_const(&nir
->functions
);
664 if (layout
&& layout
->dynamic_offset_count
&&
665 (layout
->dynamic_shader_stages
& mesa_to_vk_shader_stage(nir
->info
.stage
))) {
666 info
->loads_push_constants
= true;
667 info
->loads_dynamic_offsets
= true;
670 nir_foreach_variable(variable
, &nir
->inputs
)
671 gather_info_input_decl(nir
, variable
, info
, key
);
673 nir_foreach_block(block
, func
->impl
) {
674 gather_info_block(nir
, block
, info
);
677 nir_foreach_variable(variable
, &nir
->outputs
)
678 gather_info_output_decl(nir
, variable
, info
, key
);
680 if (nir
->info
.stage
== MESA_SHADER_VERTEX
||
681 nir
->info
.stage
== MESA_SHADER_TESS_EVAL
||
682 nir
->info
.stage
== MESA_SHADER_GEOMETRY
)
683 gather_xfb_info(nir
, info
);
685 /* Make sure to export the LayerID if the fragment shader needs it. */
686 if (key
->vs_common_out
.export_layer_id
) {
687 switch (nir
->info
.stage
) {
688 case MESA_SHADER_VERTEX
:
689 info
->vs
.output_usage_mask
[VARYING_SLOT_LAYER
] |= 0x1;
691 case MESA_SHADER_TESS_EVAL
:
692 info
->tes
.output_usage_mask
[VARYING_SLOT_LAYER
] |= 0x1;
694 case MESA_SHADER_GEOMETRY
:
695 info
->gs
.output_usage_mask
[VARYING_SLOT_LAYER
] |= 0x1;
702 /* Make sure to export the LayerID if the subpass has multiviews. */
703 if (key
->has_multiview_view_index
) {
704 switch (nir
->info
.stage
) {
705 case MESA_SHADER_VERTEX
:
706 info
->vs
.outinfo
.writes_layer
= true;
708 case MESA_SHADER_TESS_EVAL
:
709 info
->tes
.outinfo
.writes_layer
= true;
711 case MESA_SHADER_GEOMETRY
:
712 info
->vs
.outinfo
.writes_layer
= true;
719 /* Make sure to export the PrimitiveID if the fragment shader needs it. */
720 if (key
->vs_common_out
.export_prim_id
) {
721 switch (nir
->info
.stage
) {
722 case MESA_SHADER_VERTEX
:
723 info
->vs
.outinfo
.export_prim_id
= true;
725 case MESA_SHADER_TESS_EVAL
:
726 info
->tes
.outinfo
.export_prim_id
= true;
728 case MESA_SHADER_GEOMETRY
:
729 info
->vs
.outinfo
.export_prim_id
= true;
736 /* Make sure to export the ViewportIndex if the fragment shader needs it. */
737 if (key
->vs_common_out
.export_viewport_index
) {
738 switch (nir
->info
.stage
) {
739 case MESA_SHADER_VERTEX
:
740 info
->vs
.output_usage_mask
[VARYING_SLOT_VIEWPORT
] |= 0x1;
742 case MESA_SHADER_TESS_EVAL
:
743 info
->tes
.output_usage_mask
[VARYING_SLOT_VIEWPORT
] |= 0x1;
745 case MESA_SHADER_GEOMETRY
:
746 info
->gs
.output_usage_mask
[VARYING_SLOT_VIEWPORT
] |= 0x1;
753 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
754 info
->ps
.num_interp
= nir
->num_inputs
;
756 switch (nir
->info
.stage
) {
757 case MESA_SHADER_COMPUTE
:
758 for (int i
= 0; i
< 3; ++i
)
759 info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
761 case MESA_SHADER_FRAGMENT
:
762 info
->ps
.can_discard
= nir
->info
.fs
.uses_discard
;
763 info
->ps
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
764 info
->ps
.post_depth_coverage
= nir
->info
.fs
.post_depth_coverage
;
766 case MESA_SHADER_GEOMETRY
:
767 info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
768 info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
769 info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
770 info
->gs
.invocations
= nir
->info
.gs
.invocations
;
772 case MESA_SHADER_TESS_EVAL
:
773 info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
774 info
->tes
.spacing
= nir
->info
.tess
.spacing
;
775 info
->tes
.ccw
= nir
->info
.tess
.ccw
;
776 info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
777 info
->tes
.as_es
= key
->vs_common_out
.as_es
;
778 info
->tes
.export_prim_id
= key
->vs_common_out
.export_prim_id
;
779 info
->is_ngg
= key
->vs_common_out
.as_ngg
;
780 info
->is_ngg_passthrough
= key
->vs_common_out
.as_ngg_passthrough
;
782 case MESA_SHADER_TESS_CTRL
:
783 info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
785 case MESA_SHADER_VERTEX
:
786 info
->vs
.as_es
= key
->vs_common_out
.as_es
;
787 info
->vs
.as_ls
= key
->vs_common_out
.as_ls
;
788 info
->vs
.export_prim_id
= key
->vs_common_out
.export_prim_id
;
789 info
->is_ngg
= key
->vs_common_out
.as_ngg
;
790 info
->is_ngg_passthrough
= key
->vs_common_out
.as_ngg_passthrough
;
796 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
797 unsigned add_clip
= nir
->info
.clip_distance_array_size
+
798 nir
->info
.cull_distance_array_size
> 4;
799 info
->gs
.gsvs_vertex_size
=
800 (util_bitcount64(nir
->info
.outputs_written
) + add_clip
) * 16;
801 info
->gs
.max_gsvs_emit_size
=
802 info
->gs
.gsvs_vertex_size
* nir
->info
.gs
.vertices_out
;
805 /* Compute the ESGS item size for VS or TES as ES. */
806 if ((nir
->info
.stage
== MESA_SHADER_VERTEX
||
807 nir
->info
.stage
== MESA_SHADER_TESS_EVAL
) &&
808 key
->vs_common_out
.as_es
) {
809 struct radv_es_output_info
*es_info
=
810 nir
->info
.stage
== MESA_SHADER_VERTEX
? &info
->vs
.es_info
: &info
->tes
.es_info
;
811 uint32_t max_output_written
= 0;
813 uint64_t output_mask
= nir
->info
.outputs_written
;
814 while (output_mask
) {
815 const int i
= u_bit_scan64(&output_mask
);
816 unsigned param_index
= shader_io_get_unique_index(i
);
818 max_output_written
= MAX2(param_index
, max_output_written
);
821 es_info
->esgs_itemsize
= (max_output_written
+ 1) * 16;
824 info
->float_controls_mode
= nir
->info
.float_controls_execution_mode
;