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
|
/*
* Copyright © 2015 Intel Corporation
*
* 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 "main/mtypes.h"
#include "nir.h"
static void
set_io_mask(nir_shader *shader, nir_variable *var, int offset, int len)
{
for (int i = 0; i < len; i++) {
assert(var->data.location != -1);
int idx = var->data.location + offset + i;
bool is_patch_generic = var->data.patch &&
idx != VARYING_SLOT_TESS_LEVEL_INNER &&
idx != VARYING_SLOT_TESS_LEVEL_OUTER &&
idx != VARYING_SLOT_BOUNDING_BOX0 &&
idx != VARYING_SLOT_BOUNDING_BOX1;
uint64_t bitfield;
if (is_patch_generic) {
assert(idx >= VARYING_SLOT_PATCH0 && idx < VARYING_SLOT_TESS_MAX);
bitfield = BITFIELD64_BIT(idx - VARYING_SLOT_PATCH0);
}
else {
assert(idx < VARYING_SLOT_MAX);
bitfield = BITFIELD64_BIT(idx);
}
if (var->data.mode == nir_var_shader_in) {
if (is_patch_generic)
shader->info->patch_inputs_read |= bitfield;
else
shader->info->inputs_read |= bitfield;
/* double inputs read is only for vertex inputs */
if (shader->stage == MESA_SHADER_VERTEX &&
glsl_type_is_dual_slot(glsl_without_array(var->type)))
shader->info->double_inputs_read |= bitfield;
if (shader->stage == MESA_SHADER_FRAGMENT) {
shader->info->fs.uses_sample_qualifier |= var->data.sample;
}
} else {
assert(var->data.mode == nir_var_shader_out);
if (is_patch_generic) {
shader->info->patch_outputs_written |= bitfield;
} else if (!var->data.read_only) {
shader->info->outputs_written |= bitfield;
}
if (var->data.fb_fetch_output)
shader->info->outputs_read |= bitfield;
}
}
}
/**
* Mark an entire variable as used. Caller must ensure that the variable
* represents a shader input or output.
*/
static void
mark_whole_variable(nir_shader *shader, nir_variable *var)
{
const struct glsl_type *type = var->type;
bool is_vertex_input = false;
if (nir_is_per_vertex_io(var, shader->stage)) {
assert(glsl_type_is_array(type));
type = glsl_get_array_element(type);
}
if (shader->stage == MESA_SHADER_VERTEX &&
var->data.mode == nir_var_shader_in)
is_vertex_input = true;
set_io_mask(shader, var, 0,
glsl_count_attribute_slots(type, is_vertex_input));
}
static unsigned
get_io_offset(nir_deref_var *deref, bool is_vertex_input)
{
unsigned offset = 0;
nir_deref *tail = &deref->deref;
while (tail->child != NULL) {
tail = tail->child;
if (tail->deref_type == nir_deref_type_array) {
nir_deref_array *deref_array = nir_deref_as_array(tail);
if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
return -1;
}
offset += glsl_count_attribute_slots(tail->type, is_vertex_input) *
deref_array->base_offset;
}
/* TODO: we can get the offset for structs here see nir_lower_io() */
}
return offset;
}
/**
* Try to mark a portion of the given varying as used. Caller must ensure
* that the variable represents a shader input or output.
*
* If the index can't be interpreted as a constant, or some other problem
* occurs, then nothing will be marked and false will be returned.
*/
static bool
try_mask_partial_io(nir_shader *shader, nir_deref_var *deref)
{
nir_variable *var = deref->var;
const struct glsl_type *type = var->type;
if (nir_is_per_vertex_io(var, shader->stage)) {
assert(glsl_type_is_array(type));
type = glsl_get_array_element(type);
}
/* The code below only handles:
*
* - Indexing into matrices
* - Indexing into arrays of (arrays, matrices, vectors, or scalars)
*
* For now, we just give up if we see varying structs and arrays of structs
* here marking the entire variable as used.
*/
if (!(glsl_type_is_matrix(type) ||
(glsl_type_is_array(type) &&
(glsl_type_is_numeric(glsl_without_array(type)) ||
glsl_type_is_boolean(glsl_without_array(type)))))) {
/* If we don't know how to handle this case, give up and let the
* caller mark the whole variable as used.
*/
return false;
}
bool is_vertex_input = false;
if (shader->stage == MESA_SHADER_VERTEX &&
var->data.mode == nir_var_shader_in)
is_vertex_input = true;
unsigned offset = get_io_offset(deref, is_vertex_input);
if (offset == -1)
return false;
unsigned num_elems;
unsigned elem_width = 1;
unsigned mat_cols = 1;
if (glsl_type_is_array(type)) {
num_elems = glsl_get_aoa_size(type);
if (glsl_type_is_matrix(glsl_without_array(type)))
mat_cols = glsl_get_matrix_columns(glsl_without_array(type));
} else {
num_elems = glsl_get_matrix_columns(type);
}
/* double element width for double types that takes two slots */
if (!is_vertex_input &&
glsl_type_is_dual_slot(glsl_without_array(type))) {
elem_width *= 2;
}
if (offset >= num_elems * elem_width * mat_cols) {
/* Constant index outside the bounds of the matrix/array. This could
* arise as a result of constant folding of a legal GLSL program.
*
* Even though the spec says that indexing outside the bounds of a
* matrix/array results in undefined behaviour, we don't want to pass
* out-of-range values to set_io_mask() (since this could result in
* slots that don't exist being marked as used), so just let the caller
* mark the whole variable as used.
*/
return false;
}
set_io_mask(shader, var, offset, elem_width);
return true;
}
static void
gather_intrinsic_info(nir_intrinsic_instr *instr, nir_shader *shader)
{
switch (instr->intrinsic) {
case nir_intrinsic_discard:
case nir_intrinsic_discard_if:
assert(shader->stage == MESA_SHADER_FRAGMENT);
shader->info->fs.uses_discard = true;
break;
case nir_intrinsic_interp_var_at_centroid:
case nir_intrinsic_interp_var_at_sample:
case nir_intrinsic_interp_var_at_offset:
case nir_intrinsic_load_var:
case nir_intrinsic_store_var:
if (instr->variables[0]->var->data.mode == nir_var_shader_in ||
instr->variables[0]->var->data.mode == nir_var_shader_out) {
if (!try_mask_partial_io(shader, instr->variables[0]))
mark_whole_variable(shader, instr->variables[0]->var);
}
break;
case nir_intrinsic_load_draw_id:
case nir_intrinsic_load_front_face:
case nir_intrinsic_load_vertex_id:
case nir_intrinsic_load_vertex_id_zero_base:
case nir_intrinsic_load_base_vertex:
case nir_intrinsic_load_base_instance:
case nir_intrinsic_load_instance_id:
case nir_intrinsic_load_sample_id:
case nir_intrinsic_load_sample_pos:
case nir_intrinsic_load_sample_mask_in:
case nir_intrinsic_load_primitive_id:
case nir_intrinsic_load_invocation_id:
case nir_intrinsic_load_local_invocation_id:
case nir_intrinsic_load_local_invocation_index:
case nir_intrinsic_load_work_group_id:
case nir_intrinsic_load_num_work_groups:
case nir_intrinsic_load_tess_coord:
case nir_intrinsic_load_tess_level_outer:
case nir_intrinsic_load_tess_level_inner:
shader->info->system_values_read |=
(1 << nir_system_value_from_intrinsic(instr->intrinsic));
break;
case nir_intrinsic_end_primitive:
case nir_intrinsic_end_primitive_with_counter:
assert(shader->stage == MESA_SHADER_GEOMETRY);
shader->info->gs.uses_end_primitive = 1;
break;
default:
break;
}
}
static void
gather_tex_info(nir_tex_instr *instr, nir_shader *shader)
{
if (instr->op == nir_texop_tg4)
shader->info->uses_texture_gather = true;
}
static void
gather_info_block(nir_block *block, nir_shader *shader)
{
nir_foreach_instr(instr, block) {
switch (instr->type) {
case nir_instr_type_intrinsic:
gather_intrinsic_info(nir_instr_as_intrinsic(instr), shader);
break;
case nir_instr_type_tex:
gather_tex_info(nir_instr_as_tex(instr), shader);
break;
case nir_instr_type_call:
assert(!"nir_shader_gather_info only works if functions are inlined");
break;
default:
break;
}
}
}
void
nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint)
{
shader->info->num_textures = 0;
shader->info->num_images = 0;
nir_foreach_variable(var, &shader->uniforms) {
const struct glsl_type *type = var->type;
unsigned count = 1;
if (glsl_type_is_array(type)) {
count = glsl_get_length(type);
type = glsl_get_array_element(type);
}
if (glsl_type_is_image(type)) {
shader->info->num_images += count;
} else if (glsl_type_is_sampler(type)) {
shader->info->num_textures += count;
}
}
shader->info->inputs_read = 0;
shader->info->outputs_written = 0;
shader->info->outputs_read = 0;
shader->info->double_inputs_read = 0;
shader->info->patch_inputs_read = 0;
shader->info->patch_outputs_written = 0;
shader->info->system_values_read = 0;
if (shader->stage == MESA_SHADER_FRAGMENT) {
shader->info->fs.uses_sample_qualifier = false;
}
nir_foreach_block(block, entrypoint) {
gather_info_block(block, shader);
}
}
|