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
|
/*
* 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, the
* IsCentroid bitfield, and the UsesDFdy flag.
*
* 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 "glsl_types.h"
class ir_set_program_inouts_visitor : public ir_hierarchical_visitor {
public:
ir_set_program_inouts_visitor(struct gl_program *prog, GLenum shader_type)
{
this->prog = prog;
this->shader_type = shader_type;
}
~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_expression *);
virtual ir_visitor_status visit_enter(ir_discard *);
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;
GLenum shader_type;
};
static inline bool
is_shader_inout(ir_variable *var)
{
return var->mode == ir_var_shader_in ||
var->mode == ir_var_shader_out ||
var->mode == ir_var_system_value;
}
static void
mark(struct gl_program *prog, ir_variable *var, int offset, int len,
bool is_fragment_shader)
{
/* 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++) {
GLbitfield64 bitfield = BITFIELD64_BIT(var->location + var->index + offset + i);
if (var->mode == ir_var_shader_in) {
prog->InputsRead |= bitfield;
if (is_fragment_shader) {
gl_fragment_program *fprog = (gl_fragment_program *) prog;
fprog->InterpQualifier[var->location + var->index + offset + i] =
(glsl_interp_qualifier) var->interpolation;
if (var->centroid)
fprog->IsCentroid |= bitfield;
}
} else if (var->mode == ir_var_system_value) {
prog->SystemValuesRead |= bitfield;
} else {
assert(var->mode == ir_var_shader_out);
prog->OutputsWritten |= 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)
{
mark(this->prog, var, 0, var->type->count_attribute_slots(),
this->shader_type == GL_FRAGMENT_SHADER);
}
/* 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).
*
* 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;
ir_constant *index_as_constant = index->as_constant();
if (!index_as_constant)
return false;
int width = 1;
if (type->is_array() && type->fields.array->is_matrix()) {
width = type->fields.array->matrix_columns;
}
mark(this->prog, var, index_as_constant->value.u[0] * width, width,
this->shader_type == GL_FRAGMENT_SHADER);
return true;
}
ir_visitor_status
ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
{
ir_dereference_variable *deref_var;
deref_var = ir->array->as_dereference_variable();
ir_variable *var = deref_var ? deref_var->var : NULL;
/* Check that we're dereferencing a shader in or out */
if (!var || !is_shader_inout(var))
return visit_continue;
if (try_mark_partial_variable(var, ir->array_index))
return visit_continue_with_parent;
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_expression *ir)
{
if (this->shader_type == GL_FRAGMENT_SHADER &&
ir->operation == ir_unop_dFdy) {
gl_fragment_program *fprog = (gl_fragment_program *) prog;
fprog->UsesDFdy = true;
}
return visit_continue;
}
ir_visitor_status
ir_set_program_inouts_visitor::visit_enter(ir_discard *)
{
/* discards are only allowed in fragment shaders. */
assert(this->shader_type == GL_FRAGMENT_SHADER);
gl_fragment_program *fprog = (gl_fragment_program *) prog;
fprog->UsesKill = true;
return visit_continue;
}
void
do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
GLenum shader_type)
{
ir_set_program_inouts_visitor v(prog, shader_type);
prog->InputsRead = 0;
prog->OutputsWritten = 0;
prog->SystemValuesRead = 0;
if (shader_type == GL_FRAGMENT_SHADER) {
gl_fragment_program *fprog = (gl_fragment_program *) prog;
memset(fprog->InterpQualifier, 0, sizeof(fprog->InterpQualifier));
fprog->IsCentroid = 0;
fprog->UsesDFdy = false;
fprog->UsesKill = false;
}
visit_list_elements(&v, instructions);
}
|