2 * Copyright 2017 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "si_shader_internal.h"
28 #include "ac_nir_to_llvm.h"
30 #include "tgsi/tgsi_from_mesa.h"
32 #include "compiler/nir/nir.h"
33 #include "compiler/nir_types.h"
34 #include "compiler/nir/nir_builder.h"
36 static nir_variable
* tex_get_texture_var(nir_tex_instr
*instr
)
38 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
39 switch (instr
->src
[i
].src_type
) {
40 case nir_tex_src_texture_deref
:
41 return nir_deref_instr_get_variable(nir_src_as_deref(instr
->src
[i
].src
));
50 static nir_variable
* intrinsic_get_var(nir_intrinsic_instr
*instr
)
52 return nir_deref_instr_get_variable(nir_src_as_deref(instr
->src
[0]));
55 static void gather_intrinsic_load_deref_input_info(const nir_shader
*nir
,
56 const nir_intrinsic_instr
*instr
,
58 struct tgsi_shader_info
*info
)
60 assert(var
&& var
->data
.mode
== nir_var_shader_in
);
62 switch (nir
->info
.stage
) {
63 case MESA_SHADER_VERTEX
: {
64 unsigned i
= var
->data
.driver_location
;
65 unsigned attrib_count
= glsl_count_attribute_slots(var
->type
, false);
67 for (unsigned j
= 0; j
< attrib_count
; j
++, i
++) {
68 if (glsl_type_is_64bit(glsl_without_array(var
->type
))) {
69 /* TODO: set usage mask more accurately for doubles */
70 info
->input_usage_mask
[i
] = TGSI_WRITEMASK_XYZW
;
72 uint8_t mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
73 info
->input_usage_mask
[i
] |= mask
<< var
->data
.location_frac
;
78 case MESA_SHADER_FRAGMENT
:
79 if (var
->data
.location
== VARYING_SLOT_COL0
||
80 var
->data
.location
== VARYING_SLOT_COL1
) {
81 unsigned index
= var
->data
.location
== VARYING_SLOT_COL1
;
82 uint8_t mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
83 info
->colors_read
|= mask
<< (index
* 4);
90 static void gather_intrinsic_load_deref_output_info(const nir_shader
*nir
,
91 const nir_intrinsic_instr
*instr
,
93 struct tgsi_shader_info
*info
)
95 assert(var
&& var
->data
.mode
== nir_var_shader_out
);
97 switch (nir
->info
.stage
) {
98 case MESA_SHADER_TESS_CTRL
:
99 if (var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
100 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
101 info
->reads_tessfactor_outputs
= true;
102 else if (var
->data
.patch
)
103 info
->reads_perpatch_outputs
= true;
105 info
->reads_pervertex_outputs
= true;
108 case MESA_SHADER_FRAGMENT
:
109 if (var
->data
.fb_fetch_output
)
110 info
->uses_fbfetch
= true;
116 static void scan_instruction(const struct nir_shader
*nir
,
117 struct tgsi_shader_info
*info
,
120 if (instr
->type
== nir_instr_type_alu
) {
121 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
126 case nir_op_fddx_fine
:
127 case nir_op_fddy_fine
:
128 case nir_op_fddx_coarse
:
129 case nir_op_fddy_coarse
:
130 info
->uses_derivatives
= true;
135 } else if (instr
->type
== nir_instr_type_tex
) {
136 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
137 nir_variable
*texture
= tex_get_texture_var(tex
);
140 info
->samplers_declared
|=
141 u_bit_consecutive(tex
->sampler_index
, 1);
143 if (texture
->data
.bindless
)
144 info
->uses_bindless_samplers
= true;
151 info
->uses_derivatives
= true;
156 } else if (instr
->type
== nir_instr_type_intrinsic
) {
157 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
159 switch (intr
->intrinsic
) {
160 case nir_intrinsic_load_front_face
:
161 info
->uses_frontface
= 1;
163 case nir_intrinsic_load_instance_id
:
164 info
->uses_instanceid
= 1;
166 case nir_intrinsic_load_invocation_id
:
167 info
->uses_invocationid
= true;
169 case nir_intrinsic_load_num_work_groups
:
170 info
->uses_grid_size
= true;
172 case nir_intrinsic_load_local_group_size
:
173 /* The block size is translated to IMM with a fixed block size. */
174 if (info
->properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0)
175 info
->uses_block_size
= true;
177 case nir_intrinsic_load_local_invocation_id
:
178 case nir_intrinsic_load_work_group_id
: {
179 unsigned mask
= nir_ssa_def_components_read(&intr
->dest
.ssa
);
181 unsigned i
= u_bit_scan(&mask
);
183 if (intr
->intrinsic
== nir_intrinsic_load_work_group_id
)
184 info
->uses_block_id
[i
] = true;
186 info
->uses_thread_id
[i
] = true;
190 case nir_intrinsic_load_vertex_id
:
191 info
->uses_vertexid
= 1;
193 case nir_intrinsic_load_vertex_id_zero_base
:
194 info
->uses_vertexid_nobase
= 1;
196 case nir_intrinsic_load_base_vertex
:
197 info
->uses_basevertex
= 1;
199 case nir_intrinsic_load_draw_id
:
200 info
->uses_drawid
= 1;
202 case nir_intrinsic_load_primitive_id
:
203 info
->uses_primid
= 1;
205 case nir_intrinsic_load_sample_mask_in
:
206 info
->reads_samplemask
= true;
208 case nir_intrinsic_load_tess_level_inner
:
209 case nir_intrinsic_load_tess_level_outer
:
210 info
->reads_tess_factors
= true;
212 case nir_intrinsic_bindless_image_load
:
213 info
->uses_bindless_images
= true;
215 if (nir_intrinsic_image_dim(intr
) == GLSL_SAMPLER_DIM_BUF
)
216 info
->uses_bindless_buffer_load
= true;
218 info
->uses_bindless_image_load
= true;
220 case nir_intrinsic_bindless_image_size
:
221 case nir_intrinsic_bindless_image_samples
:
222 info
->uses_bindless_images
= true;
224 case nir_intrinsic_bindless_image_store
:
225 info
->uses_bindless_images
= true;
227 if (nir_intrinsic_image_dim(intr
) == GLSL_SAMPLER_DIM_BUF
)
228 info
->uses_bindless_buffer_store
= true;
230 info
->uses_bindless_image_store
= true;
232 info
->writes_memory
= true;
234 case nir_intrinsic_image_deref_store
:
235 info
->writes_memory
= true;
237 case nir_intrinsic_bindless_image_atomic_add
:
238 case nir_intrinsic_bindless_image_atomic_min
:
239 case nir_intrinsic_bindless_image_atomic_max
:
240 case nir_intrinsic_bindless_image_atomic_and
:
241 case nir_intrinsic_bindless_image_atomic_or
:
242 case nir_intrinsic_bindless_image_atomic_xor
:
243 case nir_intrinsic_bindless_image_atomic_exchange
:
244 case nir_intrinsic_bindless_image_atomic_comp_swap
:
245 info
->uses_bindless_images
= true;
247 if (nir_intrinsic_image_dim(intr
) == GLSL_SAMPLER_DIM_BUF
)
248 info
->uses_bindless_buffer_atomic
= true;
250 info
->uses_bindless_image_atomic
= true;
252 info
->writes_memory
= true;
254 case nir_intrinsic_image_deref_atomic_add
:
255 case nir_intrinsic_image_deref_atomic_min
:
256 case nir_intrinsic_image_deref_atomic_max
:
257 case nir_intrinsic_image_deref_atomic_and
:
258 case nir_intrinsic_image_deref_atomic_or
:
259 case nir_intrinsic_image_deref_atomic_xor
:
260 case nir_intrinsic_image_deref_atomic_exchange
:
261 case nir_intrinsic_image_deref_atomic_comp_swap
:
262 info
->writes_memory
= true;
264 case nir_intrinsic_store_ssbo
:
265 case nir_intrinsic_ssbo_atomic_add
:
266 case nir_intrinsic_ssbo_atomic_imin
:
267 case nir_intrinsic_ssbo_atomic_umin
:
268 case nir_intrinsic_ssbo_atomic_imax
:
269 case nir_intrinsic_ssbo_atomic_umax
:
270 case nir_intrinsic_ssbo_atomic_and
:
271 case nir_intrinsic_ssbo_atomic_or
:
272 case nir_intrinsic_ssbo_atomic_xor
:
273 case nir_intrinsic_ssbo_atomic_exchange
:
274 case nir_intrinsic_ssbo_atomic_comp_swap
:
275 info
->writes_memory
= true;
277 case nir_intrinsic_load_deref
: {
278 nir_variable
*var
= intrinsic_get_var(intr
);
279 nir_variable_mode mode
= var
->data
.mode
;
280 enum glsl_base_type base_type
=
281 glsl_get_base_type(glsl_without_array(var
->type
));
283 if (mode
== nir_var_shader_in
) {
284 gather_intrinsic_load_deref_input_info(nir
, intr
, var
, info
);
286 switch (var
->data
.interpolation
) {
287 case INTERP_MODE_NONE
:
288 if (glsl_base_type_is_integer(base_type
))
292 case INTERP_MODE_SMOOTH
:
293 if (var
->data
.sample
)
294 info
->uses_persp_sample
= true;
295 else if (var
->data
.centroid
)
296 info
->uses_persp_centroid
= true;
298 info
->uses_persp_center
= true;
301 case INTERP_MODE_NOPERSPECTIVE
:
302 if (var
->data
.sample
)
303 info
->uses_linear_sample
= true;
304 else if (var
->data
.centroid
)
305 info
->uses_linear_centroid
= true;
307 info
->uses_linear_center
= true;
310 } else if (mode
== nir_var_shader_out
) {
311 gather_intrinsic_load_deref_output_info(nir
, intr
, var
, info
);
315 case nir_intrinsic_interp_deref_at_centroid
:
316 case nir_intrinsic_interp_deref_at_sample
:
317 case nir_intrinsic_interp_deref_at_offset
: {
318 enum glsl_interp_mode interp
= intrinsic_get_var(intr
)->data
.interpolation
;
320 case INTERP_MODE_SMOOTH
:
321 case INTERP_MODE_NONE
:
322 if (intr
->intrinsic
== nir_intrinsic_interp_deref_at_centroid
)
323 info
->uses_persp_opcode_interp_centroid
= true;
324 else if (intr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
)
325 info
->uses_persp_opcode_interp_sample
= true;
327 info
->uses_persp_opcode_interp_offset
= true;
329 case INTERP_MODE_NOPERSPECTIVE
:
330 if (intr
->intrinsic
== nir_intrinsic_interp_deref_at_centroid
)
331 info
->uses_linear_opcode_interp_centroid
= true;
332 else if (intr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
)
333 info
->uses_linear_opcode_interp_sample
= true;
335 info
->uses_linear_opcode_interp_offset
= true;
337 case INTERP_MODE_FLAT
:
340 unreachable("Unsupported interpoation type");
350 void si_nir_scan_tess_ctrl(const struct nir_shader
*nir
,
351 struct tgsi_tessctrl_info
*out
)
353 memset(out
, 0, sizeof(*out
));
355 if (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
358 out
->tessfactors_are_def_in_all_invocs
=
359 ac_are_tessfactors_def_in_all_invocs(nir
);
362 void si_nir_scan_shader(const struct nir_shader
*nir
,
363 struct tgsi_shader_info
*info
)
368 info
->processor
= pipe_shader_type_from_mesa(nir
->info
.stage
);
369 info
->num_tokens
= 2; /* indicate that the shader is non-empty */
370 info
->num_instructions
= 2;
372 info
->properties
[TGSI_PROPERTY_NEXT_SHADER
] =
373 pipe_shader_type_from_mesa(nir
->info
.next_stage
);
375 if (nir
->info
.stage
== MESA_SHADER_VERTEX
) {
376 info
->properties
[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION
] =
377 nir
->info
.vs
.window_space_position
;
380 if (nir
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
381 info
->properties
[TGSI_PROPERTY_TCS_VERTICES_OUT
] =
382 nir
->info
.tess
.tcs_vertices_out
;
385 if (nir
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
386 if (nir
->info
.tess
.primitive_mode
== GL_ISOLINES
)
387 info
->properties
[TGSI_PROPERTY_TES_PRIM_MODE
] = PIPE_PRIM_LINES
;
389 info
->properties
[TGSI_PROPERTY_TES_PRIM_MODE
] = nir
->info
.tess
.primitive_mode
;
391 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
392 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
393 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
394 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
395 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
397 info
->properties
[TGSI_PROPERTY_TES_SPACING
] = (nir
->info
.tess
.spacing
+ 1) % 3;
398 info
->properties
[TGSI_PROPERTY_TES_VERTEX_ORDER_CW
] = !nir
->info
.tess
.ccw
;
399 info
->properties
[TGSI_PROPERTY_TES_POINT_MODE
] = nir
->info
.tess
.point_mode
;
402 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
403 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
] = nir
->info
.gs
.input_primitive
;
404 info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
] = nir
->info
.gs
.output_primitive
;
405 info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
] = nir
->info
.gs
.vertices_out
;
406 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = nir
->info
.gs
.invocations
;
409 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
410 info
->properties
[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL
] =
411 nir
->info
.fs
.early_fragment_tests
| nir
->info
.fs
.post_depth_coverage
;
412 info
->properties
[TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE
] = nir
->info
.fs
.post_depth_coverage
;
414 if (nir
->info
.fs
.pixel_center_integer
) {
415 info
->properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] =
416 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
;
419 if (nir
->info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
420 switch (nir
->info
.fs
.depth_layout
) {
421 case FRAG_DEPTH_LAYOUT_ANY
:
422 info
->properties
[TGSI_PROPERTY_FS_DEPTH_LAYOUT
] = TGSI_FS_DEPTH_LAYOUT_ANY
;
424 case FRAG_DEPTH_LAYOUT_GREATER
:
425 info
->properties
[TGSI_PROPERTY_FS_DEPTH_LAYOUT
] = TGSI_FS_DEPTH_LAYOUT_GREATER
;
427 case FRAG_DEPTH_LAYOUT_LESS
:
428 info
->properties
[TGSI_PROPERTY_FS_DEPTH_LAYOUT
] = TGSI_FS_DEPTH_LAYOUT_LESS
;
430 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
431 info
->properties
[TGSI_PROPERTY_FS_DEPTH_LAYOUT
] = TGSI_FS_DEPTH_LAYOUT_UNCHANGED
;
434 unreachable("Unknow depth layout");
439 if (gl_shader_stage_is_compute(nir
->info
.stage
)) {
440 info
->properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] = nir
->info
.cs
.local_size
[0];
441 info
->properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] = nir
->info
.cs
.local_size
[1];
442 info
->properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
] = nir
->info
.cs
.local_size
[2];
446 uint64_t processed_inputs
= 0;
447 unsigned num_inputs
= 0;
448 nir_foreach_variable(variable
, &nir
->inputs
) {
449 unsigned semantic_name
, semantic_index
;
451 const struct glsl_type
*type
= variable
->type
;
452 if (nir_is_per_vertex_io(variable
, nir
->info
.stage
)) {
453 assert(glsl_type_is_array(type
));
454 type
= glsl_get_array_element(type
);
457 unsigned attrib_count
= glsl_count_attribute_slots(type
,
458 nir
->info
.stage
== MESA_SHADER_VERTEX
);
460 i
= variable
->data
.driver_location
;
462 /* Vertex shader inputs don't have semantics. The state
463 * tracker has already mapped them to attributes via
464 * variable->data.driver_location.
466 if (nir
->info
.stage
== MESA_SHADER_VERTEX
) {
467 if (glsl_type_is_dual_slot(glsl_without_array(variable
->type
)))
474 for (unsigned j
= 0; j
< attrib_count
; j
++, i
++) {
476 if (processed_inputs
& ((uint64_t)1 << i
))
479 processed_inputs
|= ((uint64_t)1 << i
);
482 tgsi_get_gl_varying_semantic(variable
->data
.location
+ j
, true,
483 &semantic_name
, &semantic_index
);
485 info
->input_semantic_name
[i
] = semantic_name
;
486 info
->input_semantic_index
[i
] = semantic_index
;
488 if (semantic_name
== TGSI_SEMANTIC_PRIMID
)
489 info
->uses_primid
= true;
491 enum glsl_base_type base_type
=
492 glsl_get_base_type(glsl_without_array(variable
->type
));
494 switch (variable
->data
.interpolation
) {
495 case INTERP_MODE_NONE
:
496 if (glsl_base_type_is_integer(base_type
)) {
497 info
->input_interpolate
[i
] = TGSI_INTERPOLATE_CONSTANT
;
501 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
502 info
->input_interpolate
[i
] = TGSI_INTERPOLATE_COLOR
;
507 case INTERP_MODE_SMOOTH
:
508 assert(!glsl_base_type_is_integer(base_type
));
510 info
->input_interpolate
[i
] = TGSI_INTERPOLATE_PERSPECTIVE
;
513 case INTERP_MODE_NOPERSPECTIVE
:
514 assert(!glsl_base_type_is_integer(base_type
));
516 info
->input_interpolate
[i
] = TGSI_INTERPOLATE_LINEAR
;
519 case INTERP_MODE_FLAT
:
520 info
->input_interpolate
[i
] = TGSI_INTERPOLATE_CONSTANT
;
526 info
->num_inputs
= num_inputs
;
529 uint64_t processed_outputs
= 0;
530 unsigned num_outputs
= 0;
531 nir_foreach_variable(variable
, &nir
->outputs
) {
532 unsigned semantic_name
, semantic_index
;
534 i
= variable
->data
.driver_location
;
536 const struct glsl_type
*type
= variable
->type
;
537 if (nir_is_per_vertex_io(variable
, nir
->info
.stage
)) {
538 assert(glsl_type_is_array(type
));
539 type
= glsl_get_array_element(type
);
542 unsigned attrib_count
= glsl_count_attribute_slots(type
, false);
543 for (unsigned k
= 0; k
< attrib_count
; k
++, i
++) {
545 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
546 tgsi_get_gl_frag_result_semantic(variable
->data
.location
+ k
,
547 &semantic_name
, &semantic_index
);
549 /* Adjust for dual source blending */
550 if (variable
->data
.index
> 0) {
554 tgsi_get_gl_varying_semantic(variable
->data
.location
+ k
, true,
555 &semantic_name
, &semantic_index
);
558 unsigned num_components
= 4;
559 unsigned vector_elements
= glsl_get_vector_elements(glsl_without_array(variable
->type
));
561 num_components
= vector_elements
;
563 unsigned component
= variable
->data
.location_frac
;
564 if (glsl_type_is_64bit(glsl_without_array(variable
->type
))) {
565 if (glsl_type_is_dual_slot(glsl_without_array(variable
->type
)) && k
% 2) {
566 num_components
= (num_components
* 2) - 4;
569 num_components
= MIN2(num_components
* 2, 4);
574 for (unsigned j
= component
; j
< num_components
+ component
; j
++) {
577 usagemask
|= TGSI_WRITEMASK_X
;
580 usagemask
|= TGSI_WRITEMASK_Y
;
583 usagemask
|= TGSI_WRITEMASK_Z
;
586 usagemask
|= TGSI_WRITEMASK_W
;
589 unreachable("error calculating component index");
593 unsigned gs_out_streams
;
594 if (variable
->data
.stream
& (1u << 31)) {
595 gs_out_streams
= variable
->data
.stream
& ~(1u << 31);
597 assert(variable
->data
.stream
< 4);
599 for (unsigned j
= 0; j
< num_components
; ++j
)
600 gs_out_streams
|= variable
->data
.stream
<< (2 * (component
+ j
));
603 unsigned streamx
= gs_out_streams
& 3;
604 unsigned streamy
= (gs_out_streams
>> 2) & 3;
605 unsigned streamz
= (gs_out_streams
>> 4) & 3;
606 unsigned streamw
= (gs_out_streams
>> 6) & 3;
608 if (usagemask
& TGSI_WRITEMASK_X
) {
609 info
->output_usagemask
[i
] |= TGSI_WRITEMASK_X
;
610 info
->output_streams
[i
] |= streamx
;
611 info
->num_stream_output_components
[streamx
]++;
613 if (usagemask
& TGSI_WRITEMASK_Y
) {
614 info
->output_usagemask
[i
] |= TGSI_WRITEMASK_Y
;
615 info
->output_streams
[i
] |= streamy
<< 2;
616 info
->num_stream_output_components
[streamy
]++;
618 if (usagemask
& TGSI_WRITEMASK_Z
) {
619 info
->output_usagemask
[i
] |= TGSI_WRITEMASK_Z
;
620 info
->output_streams
[i
] |= streamz
<< 4;
621 info
->num_stream_output_components
[streamz
]++;
623 if (usagemask
& TGSI_WRITEMASK_W
) {
624 info
->output_usagemask
[i
] |= TGSI_WRITEMASK_W
;
625 info
->output_streams
[i
] |= streamw
<< 6;
626 info
->num_stream_output_components
[streamw
]++;
629 /* make sure we only count this location once against
630 * the num_outputs counter.
632 if (processed_outputs
& ((uint64_t)1 << i
))
635 processed_outputs
|= ((uint64_t)1 << i
);
638 info
->output_semantic_name
[i
] = semantic_name
;
639 info
->output_semantic_index
[i
] = semantic_index
;
641 switch (semantic_name
) {
642 case TGSI_SEMANTIC_PRIMID
:
643 info
->writes_primid
= true;
645 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
646 info
->writes_viewport_index
= true;
648 case TGSI_SEMANTIC_LAYER
:
649 info
->writes_layer
= true;
651 case TGSI_SEMANTIC_PSIZE
:
652 info
->writes_psize
= true;
654 case TGSI_SEMANTIC_CLIPVERTEX
:
655 info
->writes_clipvertex
= true;
657 case TGSI_SEMANTIC_COLOR
:
658 info
->colors_written
|= 1 << semantic_index
;
660 case TGSI_SEMANTIC_STENCIL
:
661 info
->writes_stencil
= true;
663 case TGSI_SEMANTIC_SAMPLEMASK
:
664 info
->writes_samplemask
= true;
666 case TGSI_SEMANTIC_EDGEFLAG
:
667 info
->writes_edgeflag
= true;
669 case TGSI_SEMANTIC_POSITION
:
670 if (info
->processor
== PIPE_SHADER_FRAGMENT
)
671 info
->writes_z
= true;
673 info
->writes_position
= true;
678 unsigned loc
= variable
->data
.location
;
679 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
&&
680 loc
== FRAG_RESULT_COLOR
&&
681 nir
->info
.outputs_written
& (1ull << loc
)) {
682 assert(attrib_count
== 1);
683 info
->properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] = true;
687 info
->num_outputs
= num_outputs
;
689 struct set
*ubo_set
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
690 _mesa_key_pointer_equal
);
691 struct set
*ssbo_set
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
692 _mesa_key_pointer_equal
);
694 /* Intialise const_file_max[0] */
695 info
->const_file_max
[0] = -1;
697 /* The first 8 are reserved for atomic counters using ssbo */
698 unsigned ssbo_idx
= 8;
700 unsigned ubo_idx
= 1;
701 nir_foreach_variable(variable
, &nir
->uniforms
) {
702 const struct glsl_type
*type
= variable
->type
;
703 enum glsl_base_type base_type
=
704 glsl_get_base_type(glsl_without_array(type
));
705 unsigned aoa_size
= MAX2(1, glsl_get_aoa_size(type
));
706 unsigned loc
= variable
->data
.driver_location
/ 4;
707 int slot_count
= glsl_count_attribute_slots(type
, false);
708 int max_slot
= MAX2(info
->const_file_max
[0], (int) loc
) + slot_count
;
710 /* Gather buffers declared bitmasks. Note: radeonsi doesn't
711 * really use the mask (other than ubo_idx == 1 for regular
712 * uniforms) its really only used for getting the buffer count
713 * so we don't need to worry about the ordering.
715 if (variable
->interface_type
!= NULL
) {
716 if (variable
->data
.mode
== nir_var_uniform
||
717 variable
->data
.mode
== nir_var_mem_ubo
||
718 variable
->data
.mode
== nir_var_mem_ssbo
) {
720 struct set
*buf_set
= variable
->data
.mode
== nir_var_mem_ssbo
?
723 unsigned block_count
;
724 if (base_type
!= GLSL_TYPE_INTERFACE
) {
725 struct set_entry
*entry
=
726 _mesa_set_search(buf_set
, variable
->interface_type
);
728 /* Check if we have already processed
729 * a member from this ubo.
736 block_count
= aoa_size
;
739 if (variable
->data
.mode
== nir_var_uniform
||
740 variable
->data
.mode
== nir_var_mem_ubo
) {
741 info
->const_buffers_declared
|= u_bit_consecutive(ubo_idx
, block_count
);
742 ubo_idx
+= block_count
;
744 assert(variable
->data
.mode
== nir_var_mem_ssbo
);
746 info
->shader_buffers_declared
|= u_bit_consecutive(ssbo_idx
, block_count
);
747 ssbo_idx
+= block_count
;
750 _mesa_set_add(buf_set
, variable
->interface_type
);
756 /* We rely on the fact that nir_lower_samplers_as_deref has
757 * eliminated struct dereferences.
759 if (base_type
== GLSL_TYPE_SAMPLER
&& !variable
->data
.bindless
) {
760 info
->samplers_declared
|=
761 u_bit_consecutive(variable
->data
.binding
, aoa_size
);
762 } else if (base_type
== GLSL_TYPE_IMAGE
&& !variable
->data
.bindless
) {
763 info
->images_declared
|=
764 u_bit_consecutive(variable
->data
.binding
, aoa_size
);
765 } else if (base_type
!= GLSL_TYPE_ATOMIC_UINT
) {
766 info
->const_buffers_declared
|= 1;
767 info
->const_file_max
[0] = max_slot
;
771 _mesa_set_destroy(ubo_set
, NULL
);
772 _mesa_set_destroy(ssbo_set
, NULL
);
774 info
->num_written_clipdistance
= nir
->info
.clip_distance_array_size
;
775 info
->num_written_culldistance
= nir
->info
.cull_distance_array_size
;
776 info
->clipdist_writemask
= u_bit_consecutive(0, info
->num_written_clipdistance
);
777 info
->culldist_writemask
= u_bit_consecutive(0, info
->num_written_culldistance
);
779 if (info
->processor
== PIPE_SHADER_FRAGMENT
)
780 info
->uses_kill
= nir
->info
.fs
.uses_discard
;
782 func
= (struct nir_function
*)exec_list_get_head_const(&nir
->functions
);
783 nir_foreach_block(block
, func
->impl
) {
784 nir_foreach_instr(instr
, block
)
785 scan_instruction(nir
, info
, instr
);
790 si_nir_opts(struct nir_shader
*nir
)
793 unsigned lower_flrp
=
794 (nir
->options
->lower_flrp16
? 16 : 0) |
795 (nir
->options
->lower_flrp32
? 32 : 0) |
796 (nir
->options
->lower_flrp64
? 64 : 0);
801 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
803 NIR_PASS(progress
, nir
, nir_opt_copy_prop_vars
);
804 NIR_PASS(progress
, nir
, nir_opt_dead_write_vars
);
806 NIR_PASS_V(nir
, nir_lower_alu_to_scalar
, NULL
);
807 NIR_PASS_V(nir
, nir_lower_phis_to_scalar
);
809 /* (Constant) copy propagation is needed for txf with offsets. */
810 NIR_PASS(progress
, nir
, nir_copy_prop
);
811 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
812 NIR_PASS(progress
, nir
, nir_opt_dce
);
813 if (nir_opt_trivial_continues(nir
)) {
815 NIR_PASS(progress
, nir
, nir_copy_prop
);
816 NIR_PASS(progress
, nir
, nir_opt_dce
);
818 NIR_PASS(progress
, nir
, nir_opt_if
, true);
819 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
820 NIR_PASS(progress
, nir
, nir_opt_cse
);
821 NIR_PASS(progress
, nir
, nir_opt_peephole_select
, 8, true, true);
823 /* Needed for algebraic lowering */
824 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
825 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
827 if (lower_flrp
!= 0) {
828 bool lower_flrp_progress
= false;
830 NIR_PASS(lower_flrp_progress
, nir
, nir_lower_flrp
,
832 false /* always_precise */,
833 nir
->options
->lower_ffma
);
834 if (lower_flrp_progress
) {
835 NIR_PASS(progress
, nir
,
836 nir_opt_constant_folding
);
840 /* Nothing should rematerialize any flrps, so we only
841 * need to do this lowering once.
846 NIR_PASS(progress
, nir
, nir_opt_undef
);
847 NIR_PASS(progress
, nir
, nir_opt_conditional_discard
);
848 if (nir
->options
->max_unroll_iterations
) {
849 NIR_PASS(progress
, nir
, nir_opt_loop_unroll
, 0);
855 type_size_vec4(const struct glsl_type
*type
, bool bindless
)
857 return glsl_count_attribute_slots(type
, false);
861 si_nir_lower_color(nir_shader
*nir
)
863 nir_function_impl
*entrypoint
= nir_shader_get_entrypoint(nir
);
866 nir_builder_init(&b
, entrypoint
);
868 nir_foreach_block(block
, entrypoint
) {
869 nir_foreach_instr_safe(instr
, block
) {
870 if (instr
->type
!= nir_instr_type_intrinsic
)
873 nir_intrinsic_instr
*intrin
=
874 nir_instr_as_intrinsic(instr
);
876 if (intrin
->intrinsic
!= nir_intrinsic_load_deref
)
879 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
880 if (deref
->mode
!= nir_var_shader_in
)
883 b
.cursor
= nir_before_instr(instr
);
884 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
887 if (var
->data
.location
== VARYING_SLOT_COL0
) {
888 def
= nir_load_color0(&b
);
889 } else if (var
->data
.location
== VARYING_SLOT_COL1
) {
890 def
= nir_load_color1(&b
);
895 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
, nir_src_for_ssa(def
));
896 nir_instr_remove(instr
);
902 * Perform "lowering" operations on the NIR that are run once when the shader
903 * selector is created.
906 si_lower_nir(struct si_shader_selector
* sel
, unsigned wave_size
)
908 /* Adjust the driver location of inputs and outputs. The state tracker
909 * interprets them as slots, while the ac/nir backend interprets them
910 * as individual components.
912 if (sel
->nir
->info
.stage
!= MESA_SHADER_FRAGMENT
) {
913 nir_foreach_variable(variable
, &sel
->nir
->inputs
)
914 variable
->data
.driver_location
*= 4;
916 NIR_PASS_V(sel
->nir
, nir_lower_io_to_temporaries
,
917 nir_shader_get_entrypoint(sel
->nir
), false, true);
919 /* Since we're doing nir_lower_io_to_temporaries late, we need
920 * to lower all the copy_deref's introduced by
921 * lower_io_to_temporaries before calling nir_lower_io.
923 NIR_PASS_V(sel
->nir
, nir_split_var_copies
);
924 NIR_PASS_V(sel
->nir
, nir_lower_var_copies
);
925 NIR_PASS_V(sel
->nir
, nir_lower_global_vars_to_local
);
927 si_nir_lower_color(sel
->nir
);
928 NIR_PASS_V(sel
->nir
, nir_lower_io
, nir_var_shader_in
, type_size_vec4
, 0);
930 /* This pass needs actual constants */
931 NIR_PASS_V(sel
->nir
, nir_opt_constant_folding
);
932 NIR_PASS_V(sel
->nir
, nir_io_add_const_offset_to_base
,
936 nir_foreach_variable(variable
, &sel
->nir
->outputs
) {
937 variable
->data
.driver_location
*= 4;
939 if (sel
->nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
940 if (variable
->data
.location
== FRAG_RESULT_DEPTH
)
941 variable
->data
.driver_location
+= 2;
942 else if (variable
->data
.location
== FRAG_RESULT_STENCIL
)
943 variable
->data
.driver_location
+= 1;
947 /* Perform lowerings (and optimizations) of code.
949 * Performance considerations aside, we must:
950 * - lower certain ALU operations
951 * - ensure constant offsets for texture instructions are folded
952 * and copy-propagated
955 static const struct nir_lower_tex_options lower_tex_options
= {
958 NIR_PASS_V(sel
->nir
, nir_lower_tex
, &lower_tex_options
);
960 const nir_lower_subgroups_options subgroups_options
= {
961 .subgroup_size
= wave_size
,
962 .ballot_bit_size
= wave_size
,
963 .lower_to_scalar
= true,
964 .lower_subgroup_masks
= true,
965 .lower_vote_trivial
= false,
966 .lower_vote_eq_to_ballot
= true,
968 NIR_PASS_V(sel
->nir
, nir_lower_subgroups
, &subgroups_options
);
970 ac_lower_indirect_derefs(sel
->nir
, sel
->screen
->info
.chip_class
);
972 si_nir_opts(sel
->nir
);
974 NIR_PASS_V(sel
->nir
, nir_lower_bool_to_int32
);
976 /* Strip the resulting shader so that the shader cache is more likely
977 * to hit from other similar shaders.
982 static void declare_nir_input_vs(struct si_shader_context
*ctx
,
983 struct nir_variable
*variable
,
984 unsigned input_index
,
987 si_llvm_load_input_vs(ctx
, input_index
, out
);
991 si_nir_lookup_interp_param(struct ac_shader_abi
*abi
,
992 enum glsl_interp_mode interp
, unsigned location
)
994 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
995 int interp_param_idx
= -1;
998 case INTERP_MODE_FLAT
:
1000 case INTERP_MODE_SMOOTH
:
1001 case INTERP_MODE_NONE
:
1002 if (location
== INTERP_CENTER
)
1003 interp_param_idx
= SI_PARAM_PERSP_CENTER
;
1004 else if (location
== INTERP_CENTROID
)
1005 interp_param_idx
= SI_PARAM_PERSP_CENTROID
;
1006 else if (location
== INTERP_SAMPLE
)
1007 interp_param_idx
= SI_PARAM_PERSP_SAMPLE
;
1009 case INTERP_MODE_NOPERSPECTIVE
:
1010 if (location
== INTERP_CENTER
)
1011 interp_param_idx
= SI_PARAM_LINEAR_CENTER
;
1012 else if (location
== INTERP_CENTROID
)
1013 interp_param_idx
= SI_PARAM_LINEAR_CENTROID
;
1014 else if (location
== INTERP_SAMPLE
)
1015 interp_param_idx
= SI_PARAM_LINEAR_SAMPLE
;
1018 assert(!"Unhandled interpolation mode.");
1022 return interp_param_idx
!= -1 ?
1023 LLVMGetParam(ctx
->main_fn
, interp_param_idx
) : NULL
;
1027 si_nir_load_sampler_desc(struct ac_shader_abi
*abi
,
1028 unsigned descriptor_set
, unsigned base_index
,
1029 unsigned constant_index
, LLVMValueRef dynamic_index
,
1030 enum ac_descriptor_type desc_type
, bool image
,
1031 bool write
, bool bindless
)
1033 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1034 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1035 unsigned const_index
= base_index
+ constant_index
;
1037 assert(!descriptor_set
);
1038 assert(!image
|| desc_type
== AC_DESC_IMAGE
|| desc_type
== AC_DESC_BUFFER
);
1042 LLVMGetParam(ctx
->main_fn
, ctx
->param_bindless_samplers_and_images
);
1044 /* dynamic_index is the bindless handle */
1046 /* For simplicity, bindless image descriptors use fixed
1047 * 16-dword slots for now.
1049 dynamic_index
= LLVMBuildMul(ctx
->ac
.builder
, dynamic_index
,
1050 LLVMConstInt(ctx
->i64
, 2, 0), "");
1052 return si_load_image_desc(ctx
, list
, dynamic_index
, desc_type
,
1056 /* Since bindless handle arithmetic can contain an unsigned integer
1057 * wraparound and si_load_sampler_desc assumes there isn't any,
1058 * use GEP without "inbounds" (inside ac_build_pointer_add)
1059 * to prevent incorrect code generation and hangs.
1061 dynamic_index
= LLVMBuildMul(ctx
->ac
.builder
, dynamic_index
,
1062 LLVMConstInt(ctx
->i64
, 2, 0), "");
1063 list
= ac_build_pointer_add(&ctx
->ac
, list
, dynamic_index
);
1064 return si_load_sampler_desc(ctx
, list
, ctx
->i32_0
, desc_type
);
1067 unsigned num_slots
= image
? ctx
->num_images
: ctx
->num_samplers
;
1068 assert(const_index
< num_slots
);
1070 LLVMValueRef list
= LLVMGetParam(ctx
->main_fn
, ctx
->param_samplers_and_images
);
1071 LLVMValueRef index
= LLVMConstInt(ctx
->ac
.i32
, const_index
, false);
1073 if (dynamic_index
) {
1074 index
= LLVMBuildAdd(builder
, index
, dynamic_index
, "");
1076 /* From the GL_ARB_shader_image_load_store extension spec:
1078 * If a shader performs an image load, store, or atomic
1079 * operation using an image variable declared as an array,
1080 * and if the index used to select an individual element is
1081 * negative or greater than or equal to the size of the
1082 * array, the results of the operation are undefined but may
1083 * not lead to termination.
1085 index
= si_llvm_bound_index(ctx
, index
, num_slots
);
1089 index
= LLVMBuildSub(ctx
->ac
.builder
,
1090 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
- 1, 0),
1092 return si_load_image_desc(ctx
, list
, index
, desc_type
, write
, false);
1095 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
1096 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
/ 2, 0), "");
1097 return si_load_sampler_desc(ctx
, list
, index
, desc_type
);
1100 static void bitcast_inputs(struct si_shader_context
*ctx
,
1101 LLVMValueRef data
[4],
1104 for (unsigned chan
= 0; chan
< 4; chan
++) {
1105 ctx
->inputs
[input_idx
+ chan
] =
1106 LLVMBuildBitCast(ctx
->ac
.builder
, data
[chan
], ctx
->ac
.i32
, "");
1110 bool si_nir_build_llvm(struct si_shader_context
*ctx
, struct nir_shader
*nir
)
1112 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1114 if (nir
->info
.stage
== MESA_SHADER_VERTEX
) {
1115 uint64_t processed_inputs
= 0;
1116 nir_foreach_variable(variable
, &nir
->inputs
) {
1117 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
,
1119 unsigned input_idx
= variable
->data
.driver_location
;
1121 LLVMValueRef data
[4];
1122 unsigned loc
= variable
->data
.location
;
1124 for (unsigned i
= 0; i
< attrib_count
; i
++) {
1125 /* Packed components share the same location so skip
1126 * them if we have already processed the location.
1128 if (processed_inputs
& ((uint64_t)1 << (loc
+ i
))) {
1133 declare_nir_input_vs(ctx
, variable
, input_idx
/ 4, data
);
1134 bitcast_inputs(ctx
, data
, input_idx
);
1135 if (glsl_type_is_dual_slot(variable
->type
)) {
1137 declare_nir_input_vs(ctx
, variable
, input_idx
/ 4, data
);
1138 bitcast_inputs(ctx
, data
, input_idx
);
1141 processed_inputs
|= ((uint64_t)1 << (loc
+ i
));
1145 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1146 unsigned colors_read
=
1147 ctx
->shader
->selector
->info
.colors_read
;
1148 LLVMValueRef main_fn
= ctx
->main_fn
;
1150 LLVMValueRef undef
= LLVMGetUndef(ctx
->f32
);
1152 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1;
1154 if (colors_read
& 0x0f) {
1155 unsigned mask
= colors_read
& 0x0f;
1156 LLVMValueRef values
[4];
1157 values
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1158 values
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1159 values
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1160 values
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1162 ac_to_integer(&ctx
->ac
,
1163 ac_build_gather_values(&ctx
->ac
, values
, 4));
1165 if (colors_read
& 0xf0) {
1166 unsigned mask
= (colors_read
& 0xf0) >> 4;
1167 LLVMValueRef values
[4];
1168 values
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1169 values
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1170 values
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1171 values
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1173 ac_to_integer(&ctx
->ac
,
1174 ac_build_gather_values(&ctx
->ac
, values
, 4));
1178 ctx
->abi
.inputs
= &ctx
->inputs
[0];
1179 ctx
->abi
.load_sampler_desc
= si_nir_load_sampler_desc
;
1180 ctx
->abi
.clamp_shadow_reference
= true;
1182 ctx
->num_samplers
= util_last_bit(info
->samplers_declared
);
1183 ctx
->num_images
= util_last_bit(info
->images_declared
);
1185 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_CS_LOCAL_SIZE
]) {
1186 assert(gl_shader_stage_is_compute(nir
->info
.stage
));
1187 si_declare_compute_memory(ctx
);
1189 ac_nir_translate(&ctx
->ac
, &ctx
->abi
, nir
);