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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*
* Copyright © 2010 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.
*/
/**
* \file ir_set_program_inouts.cpp
*
* Sets the InputsRead and OutputsWritten of Mesa programs.
*
* Additionally, for fragment shaders, sets the InterpQualifier array, and the
* IsCentroid and IsSample bitfields.
*
* Mesa programs (gl_program, not gl_shader_program) have a set of
* flags indicating which varyings are read and written. Computing
* which are actually read from some sort of backend code can be
* tricky when variable array indexing involved. So this pass
* provides support for setting InputsRead and OutputsWritten right
* from the GLSL IR.
*/
#include "main/core.h" /* for struct gl_program */
#include "ir.h"
#include "ir_visitor.h"
#include "compiler/glsl_types.h"
namespace {
class ir_set_program_inouts_visitor : public ir_hierarchical_visitor {
public:
ir_set_program_inouts_visitor(struct gl_program *prog,
gl_shader_stage shader_stage)
{
this->prog = prog;
this->shader_stage = shader_stage;
}
~ir_set_program_inouts_visitor()
{
}
virtual ir_visitor_status visit_enter(ir_dereference_array *);
virtual ir_visitor_status visit_enter(ir_function_signature *);
virtual ir_visitor_status visit_enter(ir_discard *);
virtual ir_visitor_status visit_enter(ir_texture *);
virtual ir_visitor_status visit(ir_dereference_variable *);
private:
void mark_whole_variable(ir_variable *var);
bool try_mark_partial_variable(ir_variable *var, ir_rvalue *index);
struct gl_program *prog;
gl_shader_stage shader_stage;
};
} /* anonymous namespace */
static inline bool
is_shader_inout(ir_variable *var)
{
return var->data.mode == ir_var_shader_in ||
var->data.mode == ir_var_shader_out ||
var->data.mode == ir_var_system_value;
}
static void
mark(struct gl_program *prog, ir_variable *var, int offset, int len,
gl_shader_stage stage)
{
/* As of GLSL 1.20, varyings can only be floats, floating-point
* vectors or matrices, or arrays of them. For Mesa programs using
* InputsRead/OutputsWritten, everything but matrices uses one
* slot, while matrices use a slot per column. Presumably
* something doing a more clever packing would use something other
* than InputsRead/OutputsWritten.
*/
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;
GLbitfield64 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 == ir_var_shader_in) {
if (is_patch_generic)
prog->PatchInputsRead |= bitfield;
else
prog->InputsRead |= bitfield;
/* double inputs read is only for vertex inputs */
if (stage == MESA_SHADER_VERTEX &&
var->type->without_array()->is_dual_slot())
prog->DoubleInputsRead |= bitfield;
if (stage == MESA_SHADER_FRAGMENT) {
gl_fragment_program *fprog = (gl_fragment_program *) prog;
fprog->InterpQualifier[idx] =
(glsl_interp_mode) var->data.interpolation;
if (var->data.centroid)
fprog->IsCentroid |= bitfield;
if (var->data.sample)
fprog->IsSample |= bitfield;
}
} else if (var->data.mode == ir_var_system_value) {
prog->SystemValuesRead |= bitfield;
} else {
assert(var->data.mode == ir_var_shader_out);
if (is_patch_generic) {
prog->PatchOutputsWritten |= bitfield;
} else if (!var->data.read_only) {
prog->OutputsWritten |= bitfield;
if (var->data.index > 0)
prog->SecondaryOutputsWritten |= bitfield;
}
if (var->data.fb_fetch_output)
prog->OutputsRead |= bitfield;
}
}
}
/**
* Mark an entire variable as used. Caller must ensure that the variable
* represents a shader input or output.
*/
void
ir_set_program_inouts_visitor::mark_whole_variable(ir_variable *var)
{
const glsl_type *type = var->type;
bool is_vertex_input = false;
if (this->shader_stage == MESA_SHADER_GEOMETRY &&
var->data.mode == ir_var_shader_in && type->is_array()) {
type = type->fields.array;
}
if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
var->data.mode == ir_var_shader_in) {
assert(type->is_array());
type = type->fields.array;
}
if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
var->data.mode == ir_var_shader_out && !var->data.patch) {
assert(type->is_array());
type = type->fields.array;
}
if (this->shader_stage == MESA_SHADER_TESS_EVAL &&
var->data.mode == ir_var_shader_in && !var->data.patch) {
assert(type->is_array());
type = type->fields.array;
}
if (this->shader_stage == MESA_SHADER_VERTEX &&
var->data.mode == ir_var_shader_in)
is_vertex_input = true;
mark(this->prog, var, 0, type->count_attribute_slots(is_vertex_input),
this->shader_stage);
}
/* Default handler: Mark all the locations in the variable as used. */
ir_visitor_status
ir_set_program_inouts_visitor::visit(ir_dereference_variable *ir)
{
if (!is_shader_inout(ir->var))
return visit_continue;
mark_whole_variable(ir->var);
return visit_continue;
}
/**
* Try to mark a portion of the given variable as used. Caller must ensure
* that the variable represents a shader input or output which can be indexed
* into in array fashion (an array or matrix). For the purpose of geometry
* shader inputs (which are always arrays*), this means that the array element
* must be something that can be indexed into in array fashion.
*
* *Except gl_PrimitiveIDIn, as noted below.
*
* For tessellation control shaders all inputs and non-patch outputs are
* arrays. For tessellation evaluation shaders non-patch inputs are arrays.
*
* 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.
*/
bool
ir_set_program_inouts_visitor::try_mark_partial_variable(ir_variable *var,
ir_rvalue *index)
{
const glsl_type *type = var->type;
if (this->shader_stage == MESA_SHADER_GEOMETRY &&
var->data.mode == ir_var_shader_in) {
/* The only geometry shader input that is not an array is
* gl_PrimitiveIDIn, and in that case, this code will never be reached,
* because gl_PrimitiveIDIn can't be indexed into in array fashion.
*/
assert(type->is_array());
type = type->fields.array;
}
if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
var->data.mode == ir_var_shader_in) {
assert(type->is_array());
type = type->fields.array;
}
if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
var->data.mode == ir_var_shader_out && !var->data.patch) {
assert(type->is_array());
type = type->fields.array;
}
if (this->shader_stage == MESA_SHADER_TESS_EVAL &&
var->data.mode == ir_var_shader_in && !var->data.patch) {
assert(type->is_array());
type = type->fields.array;
}
/* TODO: implement proper arrays of arrays support
* for now let the caller mark whole variable as used.
*/
if (type->is_array() && type->fields.array->is_array())
return false;
/* The code below only handles:
*
* - Indexing into matrices
* - Indexing into arrays of (matrices, vectors, or scalars)
*
* All other possibilities are either prohibited by GLSL (vertex inputs and
* fragment outputs can't be structs) or should have been eliminated by
* lowering passes (do_vec_index_to_swizzle() gets rid of indexing into
* vectors, and lower_packed_varyings() gets rid of structs that occur in
* varyings).
*
* However, we don't use varying packing in all cases - tessellation
* shaders bypass it. This means we'll see varying structs and arrays
* of structs here. For now, we just give up so the caller marks the
* entire variable as used.
*/
if (!(type->is_matrix() ||
(type->is_array() &&
(type->fields.array->is_numeric() ||
type->fields.array->is_boolean())))) {
/* If we don't know how to handle this case, give up and let the
* caller mark the whole variable as used.
*/
return false;
}
ir_constant *index_as_constant = index->as_constant();
if (!index_as_constant)
return false;
unsigned elem_width;
unsigned num_elems;
if (type->is_array()) {
num_elems = type->length;
if (type->fields.array->is_matrix())
elem_width = type->fields.array->matrix_columns;
else
elem_width = 1;
} else {
num_elems = type->matrix_columns;
elem_width = 1;
}
if (index_as_constant->value.u[0] >= num_elems) {
/* 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 mark() (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;
}
/* double element width for double types that takes two slots */
if (this->shader_stage != MESA_SHADER_VERTEX ||
var->data.mode != ir_var_shader_in) {
if (type->without_array()->is_dual_slot())
elem_width *= 2;
}
mark(this->prog, var, index_as_constant->value.u[0] * elem_width,
elem_width, this->shader_stage);
return true;
}
static bool
is_multiple_vertices(gl_shader_stage stage, ir_variable *var)
{
if (var->data.patch)
return false;
if (var->data.mode == ir_var_shader_in)
return stage == MESA_SHADER_GEOMETRY ||
stage == MESA_SHADER_TESS_CTRL ||
stage == MESA_SHADER_TESS_EVAL;
if (var->data.mode == ir_var_shader_out)
return stage == MESA_SHADER_TESS_CTRL;
return false;
}
ir_visitor_status
ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
{
/* Note: for geometry shader inputs, lower_named_interface_blocks may
* create 2D arrays, so we need to be able to handle those. 2D arrays
* shouldn't be able to crop up for any other reason.
*/
if (ir_dereference_array * const inner_array =
ir->array->as_dereference_array()) {
/* ir => foo[i][j]
* inner_array => foo[i]
*/
if (ir_dereference_variable * const deref_var =
inner_array->array->as_dereference_variable()) {
if (is_multiple_vertices(this->shader_stage, deref_var->var)) {
/* foo is a geometry or tessellation shader input, so i is
* the vertex, and j the part of the input we're accessing.
*/
if (try_mark_partial_variable(deref_var->var, ir->array_index))
{
/* We've now taken care of foo and j, but i might contain a
* subexpression that accesses shader inputs. So manually
* visit i and then continue with the parent.
*/
inner_array->array_index->accept(this);
return visit_continue_with_parent;
}
}
}
} else if (ir_dereference_variable * const deref_var =
ir->array->as_dereference_variable()) {
/* ir => foo[i], where foo is a variable. */
if (is_multiple_vertices(this->shader_stage, deref_var->var)) {
/* foo is a geometry or tessellation shader input, so i is
* the vertex, and we're accessing the entire input.
*/
mark_whole_variable(deref_var->var);
/* We've now taken care of foo, but i might contain a subexpression
* that accesses shader inputs. So manually visit i and then
* continue with the parent.
*/
ir->array_index->accept(this);
return visit_continue_with_parent;
} else if (is_shader_inout(deref_var->var)) {
/* foo is a shader input/output, but not a geometry shader input,
* so i is the part of the input we're accessing.
*/
if (try_mark_partial_variable(deref_var->var, ir->array_index))
return visit_continue_with_parent;
}
}
/* The expression is something we don't recognize. Just visit its
* subexpressions.
*/
return visit_continue;
}
ir_visitor_status
ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
{
/* We don't want to descend into the function parameters and
* consider them as shader inputs or outputs.
*/
visit_list_elements(this, &ir->body);
return visit_continue_with_parent;
}
ir_visitor_status
ir_set_program_inouts_visitor::visit_enter(ir_discard *)
{
/* discards are only allowed in fragment shaders. */
assert(this->shader_stage == MESA_SHADER_FRAGMENT);
gl_fragment_program *fprog = (gl_fragment_program *) prog;
fprog->UsesKill = true;
return visit_continue;
}
ir_visitor_status
ir_set_program_inouts_visitor::visit_enter(ir_texture *ir)
{
if (ir->op == ir_tg4)
prog->UsesGather = true;
return visit_continue;
}
void
do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
gl_shader_stage shader_stage)
{
ir_set_program_inouts_visitor v(prog, shader_stage);
prog->InputsRead = 0;
prog->OutputsWritten = 0;
prog->SecondaryOutputsWritten = 0;
prog->OutputsRead = 0;
prog->PatchInputsRead = 0;
prog->PatchOutputsWritten = 0;
prog->SystemValuesRead = 0;
if (shader_stage == MESA_SHADER_FRAGMENT) {
gl_fragment_program *fprog = (gl_fragment_program *) prog;
memset(fprog->InterpQualifier, 0, sizeof(fprog->InterpQualifier));
fprog->IsCentroid = 0;
fprog->IsSample = 0;
fprog->UsesKill = false;
}
visit_list_elements(&v, instructions);
}
|