1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*
* Copyright © 2019 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors (Collabora):
* Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
*/
#include "nir.h"
#include "nir_builder.h"
/*
* Lowers SSBOs to globals, for hardware that lack native SSBO support. When
* lowering, *_ssbo_* instructions will become *_global_* instructions,
* augmented with load_ssbo_address.
*
* DOES NOT PERFORM BOUNDS CHECKING. DO NOT USE IN PRODUCTION ON UNTRUSTED
* CONTEXTS INCLUDING WEBGL 2.
*/
static nir_intrinsic_op
lower_ssbo_op(nir_intrinsic_op op)
{
switch (op) {
case nir_intrinsic_load_ssbo:
return nir_intrinsic_load_global;
case nir_intrinsic_store_ssbo:
return nir_intrinsic_store_global;
case nir_intrinsic_ssbo_atomic_add:
return nir_intrinsic_global_atomic_add;
case nir_intrinsic_ssbo_atomic_imin:
return nir_intrinsic_global_atomic_imin;
case nir_intrinsic_ssbo_atomic_umin:
return nir_intrinsic_global_atomic_umin;
case nir_intrinsic_ssbo_atomic_imax:
return nir_intrinsic_global_atomic_imax;
case nir_intrinsic_ssbo_atomic_umax:
return nir_intrinsic_global_atomic_umax;
case nir_intrinsic_ssbo_atomic_and:
return nir_intrinsic_global_atomic_and;
case nir_intrinsic_ssbo_atomic_or:
return nir_intrinsic_global_atomic_or;
case nir_intrinsic_ssbo_atomic_xor:
return nir_intrinsic_global_atomic_xor;
case nir_intrinsic_ssbo_atomic_exchange:
return nir_intrinsic_global_atomic_exchange;
case nir_intrinsic_ssbo_atomic_comp_swap:
return nir_intrinsic_global_atomic_comp_swap;
case nir_intrinsic_ssbo_atomic_fadd:
return nir_intrinsic_global_atomic_fadd;
case nir_intrinsic_ssbo_atomic_fmin:
return nir_intrinsic_global_atomic_fmin;
case nir_intrinsic_ssbo_atomic_fmax:
return nir_intrinsic_global_atomic_fmax;
case nir_intrinsic_ssbo_atomic_fcomp_swap:
return nir_intrinsic_global_atomic_fcomp_swap;
default:
unreachable("Invalid SSBO op");
}
}
/* Like SSBO property sysvals, though SSBO index may be indirect. C.f.
* nir_load_system_value */
static inline nir_ssa_def *
nir_load_ssbo_prop(nir_builder *b, nir_intrinsic_op op,
nir_src *idx, unsigned bitsize)
{
nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
load->num_components = 1;
nir_src_copy(&load->src[0], idx, load);
nir_ssa_dest_init(&load->instr, &load->dest, 1, bitsize, NULL);
nir_builder_instr_insert(b, &load->instr);
return &load->dest.ssa;
}
#define nir_ssbo_prop(b, prop, index, bitsize) \
nir_load_ssbo_prop(b, nir_intrinsic_##prop, index, bitsize)
static nir_ssa_def *
lower_ssbo_instr(nir_builder *b, nir_intrinsic_instr *intr)
{
nir_intrinsic_op op = lower_ssbo_op(intr->intrinsic);
bool is_store = op == nir_intrinsic_store_global;
bool is_atomic = !is_store && op != nir_intrinsic_load_global;
/* We have to calculate the address:
*
* &(SSBO[offset]) = &SSBO + offset
*/
nir_src index = intr->src[is_store ? 1 : 0];
nir_src *offset_src = nir_get_io_offset_src(intr);
nir_ssa_def *offset = nir_ssa_for_src(b, *offset_src, 1);
nir_ssa_def *address =
nir_iadd(b,
nir_ssbo_prop(b, load_ssbo_address, &index, 64),
nir_u2u64(b, offset));
/* Create the replacement intrinsic */
nir_intrinsic_instr *global =
nir_intrinsic_instr_create(b->shader, op);
global->num_components = intr->num_components;
global->src[is_store ? 1 : 0] = nir_src_for_ssa(address);
if (is_store) {
nir_src_copy(&global->src[0], &intr->src[0], global);
nir_intrinsic_set_write_mask(global, nir_intrinsic_write_mask(intr));
} else {
nir_ssa_dest_init(&global->instr, &global->dest,
intr->dest.ssa.num_components,
intr->dest.ssa.bit_size, NULL);
if (is_atomic) {
nir_src_copy(&global->src[1], &intr->src[2], global);
if (nir_intrinsic_infos[op].num_srcs > 2)
nir_src_copy(&global->src[2], &intr->src[3], global);
}
}
nir_builder_instr_insert(b, &global->instr);
return is_store ? NULL : &global->dest.ssa;
}
static bool
should_lower_ssbo_instr(const nir_instr *instr)
{
if (instr->type != nir_instr_type_intrinsic)
return false;
const nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
switch (intr->intrinsic) {
case nir_intrinsic_load_ssbo:
case nir_intrinsic_store_ssbo:
case nir_intrinsic_ssbo_atomic_add:
case nir_intrinsic_ssbo_atomic_imin:
case nir_intrinsic_ssbo_atomic_umin:
case nir_intrinsic_ssbo_atomic_imax:
case nir_intrinsic_ssbo_atomic_umax:
case nir_intrinsic_ssbo_atomic_and:
case nir_intrinsic_ssbo_atomic_or:
case nir_intrinsic_ssbo_atomic_xor:
case nir_intrinsic_ssbo_atomic_exchange:
case nir_intrinsic_ssbo_atomic_comp_swap:
case nir_intrinsic_ssbo_atomic_fadd:
case nir_intrinsic_ssbo_atomic_fmin:
case nir_intrinsic_ssbo_atomic_fmax:
case nir_intrinsic_ssbo_atomic_fcomp_swap:
return true;
default:
return false;
}
return false;
}
bool
nir_lower_ssbo(nir_shader *shader)
{
bool progress = false;
nir_foreach_function(function, shader) {
nir_function_impl *impl = function->impl;
nir_builder b;
nir_builder_init(&b, impl);
nir_foreach_block(block, impl) {
nir_foreach_instr_safe(instr, block) {
if (!should_lower_ssbo_instr(instr)) continue;
progress = true;
b.cursor = nir_before_instr(instr);
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
nir_ssa_def *replace = lower_ssbo_instr(&b, intr);
if (replace) {
nir_ssa_def_rewrite_uses(&intr->dest.ssa,
nir_src_for_ssa(replace));
}
nir_instr_remove(instr);
}
}
}
return progress;
}
|