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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* Copyright © 2016 Broadcom
*
* 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.
*/
#include "nir.h"
#include "nir_builder.h"
#include "nir_deref.h"
/** @file nir_lower_io_to_scalar.c
*
* Replaces nir_load_input/nir_store_output operations with num_components !=
* 1 with individual per-channel operations.
*/
static void
lower_load_input_to_scalar(nir_builder *b, nir_intrinsic_instr *intr)
{
b->cursor = nir_before_instr(&intr->instr);
assert(intr->dest.is_ssa);
nir_ssa_def *loads[NIR_MAX_VEC_COMPONENTS];
for (unsigned i = 0; i < intr->num_components; i++) {
nir_intrinsic_instr *chan_intr =
nir_intrinsic_instr_create(b->shader, intr->intrinsic);
nir_ssa_dest_init(&chan_intr->instr, &chan_intr->dest,
1, intr->dest.ssa.bit_size, NULL);
chan_intr->num_components = 1;
nir_intrinsic_set_base(chan_intr, nir_intrinsic_base(intr));
nir_intrinsic_set_component(chan_intr, nir_intrinsic_component(intr) + i);
/* offset */
nir_src_copy(&chan_intr->src[0], &intr->src[0], chan_intr);
nir_builder_instr_insert(b, &chan_intr->instr);
loads[i] = &chan_intr->dest.ssa;
}
nir_ssa_def_rewrite_uses(&intr->dest.ssa,
nir_src_for_ssa(nir_vec(b, loads,
intr->num_components)));
nir_instr_remove(&intr->instr);
}
static void
lower_store_output_to_scalar(nir_builder *b, nir_intrinsic_instr *intr)
{
b->cursor = nir_before_instr(&intr->instr);
nir_ssa_def *value = nir_ssa_for_src(b, intr->src[0], intr->num_components);
for (unsigned i = 0; i < intr->num_components; i++) {
if (!(nir_intrinsic_write_mask(intr) & (1 << i)))
continue;
nir_intrinsic_instr *chan_intr =
nir_intrinsic_instr_create(b->shader, intr->intrinsic);
chan_intr->num_components = 1;
nir_intrinsic_set_base(chan_intr, nir_intrinsic_base(intr));
nir_intrinsic_set_write_mask(chan_intr, 0x1);
nir_intrinsic_set_component(chan_intr, nir_intrinsic_component(intr) + i);
/* value */
chan_intr->src[0] = nir_src_for_ssa(nir_channel(b, value, i));
/* offset */
nir_src_copy(&chan_intr->src[1], &intr->src[1], chan_intr);
nir_builder_instr_insert(b, &chan_intr->instr);
}
nir_instr_remove(&intr->instr);
}
void
nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask)
{
nir_foreach_function(function, shader) {
if (function->impl) {
nir_builder b;
nir_builder_init(&b, function->impl);
nir_foreach_block(block, function->impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_intrinsic)
continue;
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
if (intr->num_components == 1)
continue;
switch (intr->intrinsic) {
case nir_intrinsic_load_input:
if (mask & nir_var_shader_in)
lower_load_input_to_scalar(&b, intr);
break;
case nir_intrinsic_store_output:
if (mask & nir_var_shader_out)
lower_store_output_to_scalar(&b, intr);
break;
default:
break;
}
}
}
}
}
}
static nir_variable **
get_channel_variables(struct hash_table *ht, nir_variable *var)
{
nir_variable **chan_vars;
struct hash_entry *entry = _mesa_hash_table_search(ht, var);
if (!entry) {
chan_vars = (nir_variable **) calloc(4, sizeof(nir_variable *));
_mesa_hash_table_insert(ht, var, chan_vars);
} else {
chan_vars = (nir_variable **) entry->data;
}
return chan_vars;
}
/*
* Note that the src deref that we are cloning is the head of the
* chain of deref instructions from the original intrinsic, but
* the dst we are cloning to is the tail (because chains of deref
* instructions are created back to front)
*/
static nir_deref_instr *
clone_deref_array(nir_builder *b, nir_deref_instr *dst_tail,
const nir_deref_instr *src_head)
{
const nir_deref_instr *parent = nir_deref_instr_parent(src_head);
if (!parent)
return dst_tail;
assert(src_head->deref_type == nir_deref_type_array);
dst_tail = clone_deref_array(b, dst_tail, parent);
return nir_build_deref_array(b, dst_tail,
nir_ssa_for_src(b, src_head->arr.index, 1));
}
static void
lower_load_to_scalar_early(nir_builder *b, nir_intrinsic_instr *intr,
nir_variable *var, struct hash_table *split_inputs,
struct hash_table *split_outputs)
{
b->cursor = nir_before_instr(&intr->instr);
assert(intr->dest.is_ssa);
nir_ssa_def *loads[NIR_MAX_VEC_COMPONENTS];
nir_variable **chan_vars;
if (var->data.mode == nir_var_shader_in) {
chan_vars = get_channel_variables(split_inputs, var);
} else {
chan_vars = get_channel_variables(split_outputs, var);
}
for (unsigned i = 0; i < intr->num_components; i++) {
nir_variable *chan_var = chan_vars[var->data.location_frac + i];
if (!chan_vars[var->data.location_frac + i]) {
chan_var = nir_variable_clone(var, b->shader);
chan_var->data.location_frac = var->data.location_frac + i;
chan_var->type = glsl_channel_type(chan_var->type);
chan_vars[var->data.location_frac + i] = chan_var;
nir_shader_add_variable(b->shader, chan_var);
}
nir_intrinsic_instr *chan_intr =
nir_intrinsic_instr_create(b->shader, intr->intrinsic);
nir_ssa_dest_init(&chan_intr->instr, &chan_intr->dest,
1, intr->dest.ssa.bit_size, NULL);
chan_intr->num_components = 1;
nir_deref_instr *deref = nir_build_deref_var(b, chan_var);
deref = clone_deref_array(b, deref, nir_src_as_deref(intr->src[0]));
chan_intr->src[0] = nir_src_for_ssa(&deref->dest.ssa);
if (intr->intrinsic == nir_intrinsic_interp_deref_at_offset ||
intr->intrinsic == nir_intrinsic_interp_deref_at_sample)
nir_src_copy(&chan_intr->src[1], &intr->src[1], &chan_intr->instr);
nir_builder_instr_insert(b, &chan_intr->instr);
loads[i] = &chan_intr->dest.ssa;
}
nir_ssa_def_rewrite_uses(&intr->dest.ssa,
nir_src_for_ssa(nir_vec(b, loads,
intr->num_components)));
/* Remove the old load intrinsic */
nir_instr_remove(&intr->instr);
}
static void
lower_store_output_to_scalar_early(nir_builder *b, nir_intrinsic_instr *intr,
nir_variable *var,
struct hash_table *split_outputs)
{
b->cursor = nir_before_instr(&intr->instr);
nir_ssa_def *value = nir_ssa_for_src(b, intr->src[1], intr->num_components);
nir_variable **chan_vars = get_channel_variables(split_outputs, var);
for (unsigned i = 0; i < intr->num_components; i++) {
if (!(nir_intrinsic_write_mask(intr) & (1 << i)))
continue;
nir_variable *chan_var = chan_vars[var->data.location_frac + i];
if (!chan_vars[var->data.location_frac + i]) {
chan_var = nir_variable_clone(var, b->shader);
chan_var->data.location_frac = var->data.location_frac + i;
chan_var->type = glsl_channel_type(chan_var->type);
chan_vars[var->data.location_frac + i] = chan_var;
nir_shader_add_variable(b->shader, chan_var);
}
nir_intrinsic_instr *chan_intr =
nir_intrinsic_instr_create(b->shader, intr->intrinsic);
chan_intr->num_components = 1;
nir_intrinsic_set_write_mask(chan_intr, 0x1);
nir_deref_instr *deref = nir_build_deref_var(b, chan_var);
deref = clone_deref_array(b, deref, nir_src_as_deref(intr->src[0]));
chan_intr->src[0] = nir_src_for_ssa(&deref->dest.ssa);
chan_intr->src[1] = nir_src_for_ssa(nir_channel(b, value, i));
nir_builder_instr_insert(b, &chan_intr->instr);
}
/* Remove the old store intrinsic */
nir_instr_remove(&intr->instr);
}
/*
* This function is intended to be called earlier than nir_lower_io_to_scalar()
* i.e. before nir_lower_io() is called.
*/
void
nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask)
{
struct hash_table *split_inputs = _mesa_pointer_hash_table_create(NULL);
struct hash_table *split_outputs = _mesa_pointer_hash_table_create(NULL);
nir_foreach_function(function, shader) {
if (function->impl) {
nir_builder b;
nir_builder_init(&b, function->impl);
nir_foreach_block(block, function->impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_intrinsic)
continue;
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
if (intr->num_components == 1)
continue;
if (intr->intrinsic != nir_intrinsic_load_deref &&
intr->intrinsic != nir_intrinsic_store_deref &&
intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
intr->intrinsic != nir_intrinsic_interp_deref_at_offset)
continue;
nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
nir_variable_mode mode = deref->mode;
if (!(mode & mask))
continue;
nir_variable *var = nir_deref_instr_get_variable(deref);
/* TODO: add patch support */
if (var->data.patch)
continue;
/* TODO: add doubles support */
if (glsl_type_is_64bit(glsl_without_array(var->type)))
continue;
if (!(shader->info.stage == MESA_SHADER_VERTEX &&
mode == nir_var_shader_in) &&
var->data.location < VARYING_SLOT_VAR0 &&
var->data.location >= 0)
continue;
/* Don't bother splitting if we can't opt away any unused
* components.
*/
if (var->data.always_active_io)
continue;
/* Skip types we cannot split */
if (glsl_type_is_matrix(glsl_without_array(var->type)) ||
glsl_type_is_struct(glsl_without_array(var->type)))
continue;
switch (intr->intrinsic) {
case nir_intrinsic_interp_deref_at_centroid:
case nir_intrinsic_interp_deref_at_sample:
case nir_intrinsic_interp_deref_at_offset:
case nir_intrinsic_load_deref:
if ((mask & nir_var_shader_in && mode == nir_var_shader_in) ||
(mask & nir_var_shader_out && mode == nir_var_shader_out))
lower_load_to_scalar_early(&b, intr, var, split_inputs,
split_outputs);
break;
case nir_intrinsic_store_deref:
if (mask & nir_var_shader_out &&
mode == nir_var_shader_out)
lower_store_output_to_scalar_early(&b, intr, var,
split_outputs);
break;
default:
break;
}
}
}
}
}
/* Remove old input from the shaders inputs list */
hash_table_foreach(split_inputs, entry) {
nir_variable *var = (nir_variable *) entry->key;
exec_node_remove(&var->node);
free(entry->data);
}
/* Remove old output from the shaders outputs list */
hash_table_foreach(split_outputs, entry) {
nir_variable *var = (nir_variable *) entry->key;
exec_node_remove(&var->node);
free(entry->data);
}
_mesa_hash_table_destroy(split_inputs, NULL);
_mesa_hash_table_destroy(split_outputs, NULL);
nir_remove_dead_derefs(shader);
}
|