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, starting from the shader's next SSBO slot
40 lower_instr(nir_intrinsic_instr
*instr
, unsigned ssbo_offset
, nir_builder
*b
)
44 b
->cursor
= nir_before_instr(&instr
->instr
);
46 switch (instr
->intrinsic
) {
47 case nir_intrinsic_memory_barrier_atomic_counter
:
48 /* Atomic counters are now SSBOs so memoryBarrierAtomicCounter() is now
49 * memoryBarrierBuffer().
51 instr
->intrinsic
= nir_intrinsic_memory_barrier_buffer
;
54 case nir_intrinsic_atomic_counter_inc
:
55 case nir_intrinsic_atomic_counter_add
:
56 case nir_intrinsic_atomic_counter_pre_dec
:
57 case nir_intrinsic_atomic_counter_post_dec
:
58 /* inc and dec get remapped to add: */
59 op
= nir_intrinsic_ssbo_atomic_add
;
61 case nir_intrinsic_atomic_counter_read
:
62 op
= nir_intrinsic_load_ssbo
;
64 case nir_intrinsic_atomic_counter_min
:
65 op
= nir_intrinsic_ssbo_atomic_umin
;
67 case nir_intrinsic_atomic_counter_max
:
68 op
= nir_intrinsic_ssbo_atomic_umax
;
70 case nir_intrinsic_atomic_counter_and
:
71 op
= nir_intrinsic_ssbo_atomic_and
;
73 case nir_intrinsic_atomic_counter_or
:
74 op
= nir_intrinsic_ssbo_atomic_or
;
76 case nir_intrinsic_atomic_counter_xor
:
77 op
= nir_intrinsic_ssbo_atomic_xor
;
79 case nir_intrinsic_atomic_counter_exchange
:
80 op
= nir_intrinsic_ssbo_atomic_exchange
;
82 case nir_intrinsic_atomic_counter_comp_swap
:
83 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
89 nir_ssa_def
*buffer
= nir_imm_int(b
, ssbo_offset
+ nir_intrinsic_base(instr
));
90 nir_ssa_def
*temp
= NULL
;
91 nir_intrinsic_instr
*new_instr
=
92 nir_intrinsic_instr_create(ralloc_parent(instr
), op
);
94 /* a couple instructions need special handling since they don't map
95 * 1:1 with ssbo atomics
97 switch (instr
->intrinsic
) {
98 case nir_intrinsic_atomic_counter_inc
:
99 /* remapped to ssbo_atomic_add: { buffer_idx, offset, +1 } */
100 temp
= nir_imm_int(b
, +1);
101 new_instr
->src
[0] = nir_src_for_ssa(buffer
);
102 nir_src_copy(&new_instr
->src
[1], &instr
->src
[0], new_instr
);
103 new_instr
->src
[2] = nir_src_for_ssa(temp
);
105 case nir_intrinsic_atomic_counter_pre_dec
:
106 case nir_intrinsic_atomic_counter_post_dec
:
107 /* remapped to ssbo_atomic_add: { buffer_idx, offset, -1 } */
108 /* NOTE semantic difference so we adjust the return value below */
109 temp
= nir_imm_int(b
, -1);
110 new_instr
->src
[0] = nir_src_for_ssa(buffer
);
111 nir_src_copy(&new_instr
->src
[1], &instr
->src
[0], new_instr
);
112 new_instr
->src
[2] = nir_src_for_ssa(temp
);
114 case nir_intrinsic_atomic_counter_read
:
115 /* remapped to load_ssbo: { buffer_idx, offset } */
116 new_instr
->src
[0] = nir_src_for_ssa(buffer
);
117 nir_src_copy(&new_instr
->src
[1], &instr
->src
[0], new_instr
);
120 /* remapped to ssbo_atomic_x: { buffer_idx, offset, data, (compare)? } */
121 new_instr
->src
[0] = nir_src_for_ssa(buffer
);
122 nir_src_copy(&new_instr
->src
[1], &instr
->src
[0], new_instr
);
123 nir_src_copy(&new_instr
->src
[2], &instr
->src
[1], new_instr
);
124 if (op
== nir_intrinsic_ssbo_atomic_comp_swap
||
125 op
== nir_intrinsic_ssbo_atomic_fcomp_swap
)
126 nir_src_copy(&new_instr
->src
[3], &instr
->src
[2], new_instr
);
130 if (new_instr
->intrinsic
== nir_intrinsic_load_ssbo
||
131 new_instr
->intrinsic
== nir_intrinsic_store_ssbo
)
132 nir_intrinsic_set_align(new_instr
, 4, 0);
134 nir_ssa_dest_init(&new_instr
->instr
, &new_instr
->dest
,
135 instr
->dest
.ssa
.num_components
,
136 instr
->dest
.ssa
.bit_size
, NULL
);
137 nir_instr_insert_before(&instr
->instr
, &new_instr
->instr
);
138 nir_instr_remove(&instr
->instr
);
140 if (instr
->intrinsic
== nir_intrinsic_atomic_counter_pre_dec
) {
141 b
->cursor
= nir_after_instr(&new_instr
->instr
);
142 nir_ssa_def
*result
= nir_iadd(b
, &new_instr
->dest
.ssa
, temp
);
143 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
, nir_src_for_ssa(result
));
145 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
, nir_src_for_ssa(&new_instr
->dest
.ssa
));
148 /* we could be replacing an intrinsic with fixed # of dest num_components
149 * with one that has variable number. So best to take this from the dest:
151 new_instr
->num_components
= instr
->dest
.ssa
.num_components
;
157 is_atomic_uint(const struct glsl_type
*type
)
159 if (glsl_get_base_type(type
) == GLSL_TYPE_ARRAY
)
160 return is_atomic_uint(glsl_get_array_element(type
));
161 return glsl_get_base_type(type
) == GLSL_TYPE_ATOMIC_UINT
;
165 nir_lower_atomics_to_ssbo(nir_shader
*shader
)
167 unsigned ssbo_offset
= shader
->info
.num_ssbos
;
168 bool progress
= false;
170 nir_foreach_function(function
, shader
) {
171 if (function
->impl
) {
173 nir_builder_init(&builder
, function
->impl
);
174 nir_foreach_block(block
, function
->impl
) {
175 nir_foreach_instr_safe(instr
, block
) {
176 if (instr
->type
== nir_instr_type_intrinsic
)
177 progress
|= lower_instr(nir_instr_as_intrinsic(instr
),
178 ssbo_offset
, &builder
);
182 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
183 nir_metadata_dominance
);
188 /* replace atomic_uint uniforms with ssbo's: */
189 unsigned replaced
= 0;
190 nir_foreach_variable_safe(var
, &shader
->uniforms
) {
191 if (is_atomic_uint(var
->type
)) {
192 exec_node_remove(&var
->node
);
194 if (replaced
& (1 << var
->data
.binding
))
200 /* A length of 0 is used to denote unsized arrays */
201 const struct glsl_type
*type
= glsl_array_type(glsl_uint_type(), 0, 0);
203 snprintf(name
, sizeof(name
), "counter%d", var
->data
.binding
);
205 ssbo
= nir_variable_create(shader
, nir_var_mem_ssbo
, type
, name
);
206 ssbo
->data
.binding
= ssbo_offset
+ var
->data
.binding
;
208 /* We can't use num_abos, because it only represents the number of
209 * active atomic counters, and currently unlike SSBO's they aren't
210 * compacted so num_abos actually isn't a bound on the index passed
211 * to nir_intrinsic_atomic_counter_*. e.g. if we have a single atomic
212 * counter declared like:
214 * layout(binding=1) atomic_uint counter0;
216 * then when we lower accesses to it the atomic_counter_* intrinsics
217 * will have 1 as the index but num_abos will still be 1.
219 shader
->info
.num_ssbos
= MAX2(shader
->info
.num_ssbos
,
220 ssbo
->data
.binding
+ 1);
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
);
236 shader
->info
.num_abos
= 0;