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
24 * Rob Clark <robclark@freedesktop.org>
28 #include "nir_builder.h"
30 #if defined(_WIN32) && !defined(snprintf)
31 #define snprintf _snprintf
35 * Remap atomic counters to SSBOs. Atomic counters get remapped to
36 * SSBO binding points [0..ssbo_offset) and the original SSBOs are
37 * remapped to [ssbo_offset..n) (mostly to align with what mesa/st
42 lower_instr(nir_intrinsic_instr
*instr
, unsigned ssbo_offset
, nir_builder
*b
)
47 b
->cursor
= nir_before_instr(&instr
->instr
);
49 switch (instr
->intrinsic
) {
50 case nir_intrinsic_ssbo_atomic_add
:
51 case nir_intrinsic_ssbo_atomic_imin
:
52 case nir_intrinsic_ssbo_atomic_umin
:
53 case nir_intrinsic_ssbo_atomic_imax
:
54 case nir_intrinsic_ssbo_atomic_umax
:
55 case nir_intrinsic_ssbo_atomic_and
:
56 case nir_intrinsic_ssbo_atomic_or
:
57 case nir_intrinsic_ssbo_atomic_xor
:
58 case nir_intrinsic_ssbo_atomic_exchange
:
59 case nir_intrinsic_ssbo_atomic_comp_swap
:
60 case nir_intrinsic_ssbo_atomic_fadd
:
61 case nir_intrinsic_store_ssbo
:
62 case nir_intrinsic_load_ssbo
:
63 case nir_intrinsic_get_buffer_size
:
64 /* easy case, keep same opcode and just remap SSBO buffer index: */
65 op
= instr
->intrinsic
;
66 idx_src
= (op
== nir_intrinsic_store_ssbo
) ? 1 : 0;
67 nir_ssa_def
*old_idx
= nir_ssa_for_src(b
, instr
->src
[idx_src
], 1);
68 nir_ssa_def
*new_idx
= nir_iadd(b
, old_idx
, nir_imm_int(b
, ssbo_offset
));
69 nir_instr_rewrite_src(&instr
->instr
,
71 nir_src_for_ssa(new_idx
));
73 case nir_intrinsic_atomic_counter_inc
:
74 case nir_intrinsic_atomic_counter_add
:
75 case nir_intrinsic_atomic_counter_pre_dec
:
76 case nir_intrinsic_atomic_counter_post_dec
:
77 /* inc and dec get remapped to add: */
78 op
= nir_intrinsic_ssbo_atomic_add
;
80 case nir_intrinsic_atomic_counter_read
:
81 op
= nir_intrinsic_load_ssbo
;
83 case nir_intrinsic_atomic_counter_min
:
84 op
= nir_intrinsic_ssbo_atomic_umin
;
86 case nir_intrinsic_atomic_counter_max
:
87 op
= nir_intrinsic_ssbo_atomic_umax
;
89 case nir_intrinsic_atomic_counter_and
:
90 op
= nir_intrinsic_ssbo_atomic_and
;
92 case nir_intrinsic_atomic_counter_or
:
93 op
= nir_intrinsic_ssbo_atomic_or
;
95 case nir_intrinsic_atomic_counter_xor
:
96 op
= nir_intrinsic_ssbo_atomic_xor
;
98 case nir_intrinsic_atomic_counter_exchange
:
99 op
= nir_intrinsic_ssbo_atomic_exchange
;
101 case nir_intrinsic_atomic_counter_comp_swap
:
102 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
108 nir_ssa_def
*buffer
= nir_imm_int(b
, nir_intrinsic_base(instr
));
109 nir_ssa_def
*temp
= NULL
;
110 nir_intrinsic_instr
*new_instr
=
111 nir_intrinsic_instr_create(ralloc_parent(instr
), op
);
113 /* a couple instructions need special handling since they don't map
114 * 1:1 with ssbo atomics
116 switch (instr
->intrinsic
) {
117 case nir_intrinsic_atomic_counter_inc
:
118 /* remapped to ssbo_atomic_add: { buffer_idx, offset, +1 } */
119 temp
= nir_imm_int(b
, +1);
120 new_instr
->src
[0] = nir_src_for_ssa(buffer
);
121 nir_src_copy(&new_instr
->src
[1], &instr
->src
[0], new_instr
);
122 new_instr
->src
[2] = nir_src_for_ssa(temp
);
124 case nir_intrinsic_atomic_counter_pre_dec
:
125 case nir_intrinsic_atomic_counter_post_dec
:
126 /* remapped to ssbo_atomic_add: { buffer_idx, offset, -1 } */
127 /* NOTE semantic difference so we adjust the return value below */
128 temp
= nir_imm_int(b
, -1);
129 new_instr
->src
[0] = nir_src_for_ssa(buffer
);
130 nir_src_copy(&new_instr
->src
[1], &instr
->src
[0], new_instr
);
131 new_instr
->src
[2] = nir_src_for_ssa(temp
);
133 case nir_intrinsic_atomic_counter_read
:
134 /* remapped to load_ssbo: { buffer_idx, offset } */
135 new_instr
->src
[0] = nir_src_for_ssa(buffer
);
136 nir_src_copy(&new_instr
->src
[1], &instr
->src
[0], new_instr
);
139 /* remapped to ssbo_atomic_x: { buffer_idx, offset, data, (compare)? } */
140 new_instr
->src
[0] = nir_src_for_ssa(buffer
);
141 nir_src_copy(&new_instr
->src
[1], &instr
->src
[0], new_instr
);
142 nir_src_copy(&new_instr
->src
[2], &instr
->src
[1], new_instr
);
143 if (op
== nir_intrinsic_ssbo_atomic_comp_swap
)
144 nir_src_copy(&new_instr
->src
[3], &instr
->src
[2], new_instr
);
148 nir_ssa_dest_init(&new_instr
->instr
, &new_instr
->dest
,
149 instr
->dest
.ssa
.num_components
,
150 instr
->dest
.ssa
.bit_size
, NULL
);
151 nir_instr_insert_before(&instr
->instr
, &new_instr
->instr
);
152 nir_instr_remove(&instr
->instr
);
154 if (instr
->intrinsic
== nir_intrinsic_atomic_counter_pre_dec
) {
155 b
->cursor
= nir_after_instr(&new_instr
->instr
);
156 nir_ssa_def
*result
= nir_iadd(b
, &new_instr
->dest
.ssa
, temp
);
157 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
, nir_src_for_ssa(result
));
159 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
, nir_src_for_ssa(&new_instr
->dest
.ssa
));
162 /* we could be replacing an intrinsic with fixed # of dest num_components
163 * with one that has variable number. So best to take this from the dest:
165 new_instr
->num_components
= instr
->dest
.ssa
.num_components
;
171 is_atomic_uint(const struct glsl_type
*type
)
173 if (glsl_get_base_type(type
) == GLSL_TYPE_ARRAY
)
174 return is_atomic_uint(glsl_get_array_element(type
));
175 return glsl_get_base_type(type
) == GLSL_TYPE_ATOMIC_UINT
;
179 nir_lower_atomics_to_ssbo(nir_shader
*shader
, unsigned ssbo_offset
)
181 bool progress
= false;
183 nir_foreach_function(function
, shader
) {
184 if (function
->impl
) {
186 nir_builder_init(&builder
, function
->impl
);
187 nir_foreach_block(block
, function
->impl
) {
188 nir_foreach_instr_safe(instr
, block
) {
189 if (instr
->type
== nir_instr_type_intrinsic
)
190 progress
|= lower_instr(nir_instr_as_intrinsic(instr
),
191 ssbo_offset
, &builder
);
195 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
196 nir_metadata_dominance
);
201 /* replace atomic_uint uniforms with ssbo's: */
202 unsigned replaced
= 0;
203 nir_foreach_variable_safe(var
, &shader
->uniforms
) {
204 if (is_atomic_uint(var
->type
)) {
205 exec_node_remove(&var
->node
);
207 if (replaced
& (1 << var
->data
.binding
))
213 /* A length of 0 is used to denote unsized arrays */
214 const struct glsl_type
*type
= glsl_array_type(glsl_uint_type(), 0);
216 snprintf(name
, sizeof(name
), "counter%d", var
->data
.binding
);
218 ssbo
= nir_variable_create(shader
, nir_var_shader_storage
,
220 ssbo
->data
.binding
= var
->data
.binding
;
222 struct glsl_struct_field field
= {
228 ssbo
->interface_type
=
229 glsl_interface_type(&field
, 1, GLSL_INTERFACE_PACKING_STD430
,
232 replaced
|= (1 << var
->data
.binding
);