glsl: In ir_set_program_inouts, handle indexing outside array/matrix bounds.
[mesa.git] / src / glsl / ir_set_program_inouts.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file ir_set_program_inouts.cpp
26 *
27 * Sets the InputsRead and OutputsWritten of Mesa programs.
28 *
29 * Additionally, for fragment shaders, sets the InterpQualifier array, the
30 * IsCentroid bitfield, and the UsesDFdy flag.
31 *
32 * Mesa programs (gl_program, not gl_shader_program) have a set of
33 * flags indicating which varyings are read and written. Computing
34 * which are actually read from some sort of backend code can be
35 * tricky when variable array indexing involved. So this pass
36 * provides support for setting InputsRead and OutputsWritten right
37 * from the GLSL IR.
38 */
39
40 #include "main/core.h" /* for struct gl_program */
41 #include "ir.h"
42 #include "ir_visitor.h"
43 #include "glsl_types.h"
44
45 class ir_set_program_inouts_visitor : public ir_hierarchical_visitor {
46 public:
47 ir_set_program_inouts_visitor(struct gl_program *prog, GLenum shader_type)
48 {
49 this->prog = prog;
50 this->shader_type = shader_type;
51 }
52 ~ir_set_program_inouts_visitor()
53 {
54 }
55
56 virtual ir_visitor_status visit_enter(ir_dereference_array *);
57 virtual ir_visitor_status visit_enter(ir_function_signature *);
58 virtual ir_visitor_status visit_enter(ir_expression *);
59 virtual ir_visitor_status visit_enter(ir_discard *);
60 virtual ir_visitor_status visit(ir_dereference_variable *);
61
62 private:
63 void mark_whole_variable(ir_variable *var);
64 bool try_mark_partial_variable(ir_variable *var, ir_rvalue *index);
65
66 struct gl_program *prog;
67 GLenum shader_type;
68 };
69
70 static inline bool
71 is_shader_inout(ir_variable *var)
72 {
73 return var->mode == ir_var_shader_in ||
74 var->mode == ir_var_shader_out ||
75 var->mode == ir_var_system_value;
76 }
77
78 static void
79 mark(struct gl_program *prog, ir_variable *var, int offset, int len,
80 bool is_fragment_shader)
81 {
82 /* As of GLSL 1.20, varyings can only be floats, floating-point
83 * vectors or matrices, or arrays of them. For Mesa programs using
84 * InputsRead/OutputsWritten, everything but matrices uses one
85 * slot, while matrices use a slot per column. Presumably
86 * something doing a more clever packing would use something other
87 * than InputsRead/OutputsWritten.
88 */
89
90 for (int i = 0; i < len; i++) {
91 GLbitfield64 bitfield = BITFIELD64_BIT(var->location + var->index + offset + i);
92 if (var->mode == ir_var_shader_in) {
93 prog->InputsRead |= bitfield;
94 if (is_fragment_shader) {
95 gl_fragment_program *fprog = (gl_fragment_program *) prog;
96 fprog->InterpQualifier[var->location + var->index + offset + i] =
97 (glsl_interp_qualifier) var->interpolation;
98 if (var->centroid)
99 fprog->IsCentroid |= bitfield;
100 }
101 } else if (var->mode == ir_var_system_value) {
102 prog->SystemValuesRead |= bitfield;
103 } else {
104 assert(var->mode == ir_var_shader_out);
105 prog->OutputsWritten |= bitfield;
106 }
107 }
108 }
109
110 /**
111 * Mark an entire variable as used. Caller must ensure that the variable
112 * represents a shader input or output.
113 */
114 void
115 ir_set_program_inouts_visitor::mark_whole_variable(ir_variable *var)
116 {
117 mark(this->prog, var, 0, var->type->count_attribute_slots(),
118 this->shader_type == GL_FRAGMENT_SHADER);
119 }
120
121 /* Default handler: Mark all the locations in the variable as used. */
122 ir_visitor_status
123 ir_set_program_inouts_visitor::visit(ir_dereference_variable *ir)
124 {
125 if (!is_shader_inout(ir->var))
126 return visit_continue;
127
128 mark_whole_variable(ir->var);
129
130 return visit_continue;
131 }
132
133 /**
134 * Try to mark a portion of the given variable as used. Caller must ensure
135 * that the variable represents a shader input or output which can be indexed
136 * into in array fashion (an array or matrix).
137 *
138 * If the index can't be interpreted as a constant, or some other problem
139 * occurs, then nothing will be marked and false will be returned.
140 */
141 bool
142 ir_set_program_inouts_visitor::try_mark_partial_variable(ir_variable *var,
143 ir_rvalue *index)
144 {
145 const glsl_type *type = var->type;
146
147 /* The code below only handles:
148 *
149 * - Indexing into matrices
150 * - Indexing into arrays of (matrices, vectors, or scalars)
151 *
152 * All other possibilities are either prohibited by GLSL (vertex inputs and
153 * fragment outputs can't be structs) or should have been eliminated by
154 * lowering passes (do_vec_index_to_swizzle() gets rid of indexing into
155 * vectors, and lower_packed_varyings() gets rid of structs that occur in
156 * varyings).
157 */
158 if (!(type->is_matrix() ||
159 (type->is_array() &&
160 (type->fields.array->is_numeric() ||
161 type->fields.array->is_boolean())))) {
162 assert(!"Unexpected indexing in ir_set_program_inouts");
163
164 /* For safety in release builds, in case we ever encounter unexpected
165 * indexing, give up and let the caller mark the whole variable as used.
166 */
167 return false;
168 }
169
170 ir_constant *index_as_constant = index->as_constant();
171 if (!index_as_constant)
172 return false;
173
174 unsigned elem_width;
175 unsigned num_elems;
176 if (type->is_array()) {
177 num_elems = type->length;
178 if (type->fields.array->is_matrix())
179 elem_width = type->fields.array->matrix_columns;
180 else
181 elem_width = 1;
182 } else {
183 num_elems = type->matrix_columns;
184 elem_width = 1;
185 }
186
187 if (index_as_constant->value.u[0] >= num_elems) {
188 /* Constant index outside the bounds of the matrix/array. This could
189 * arise as a result of constant folding of a legal GLSL program.
190 *
191 * Even though the spec says that indexing outside the bounds of a
192 * matrix/array results in undefined behaviour, we don't want to pass
193 * out-of-range values to mark() (since this could result in slots that
194 * don't exist being marked as used), so just let the caller mark the
195 * whole variable as used.
196 */
197 return false;
198 }
199
200 mark(this->prog, var, index_as_constant->value.u[0] * elem_width,
201 elem_width, this->shader_type == GL_FRAGMENT_SHADER);
202 return true;
203 }
204
205 ir_visitor_status
206 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
207 {
208 ir_dereference_variable *deref_var;
209 deref_var = ir->array->as_dereference_variable();
210 ir_variable *var = deref_var ? deref_var->var : NULL;
211
212 /* Check that we're dereferencing a shader in or out */
213 if (!var || !is_shader_inout(var))
214 return visit_continue;
215
216 if (try_mark_partial_variable(var, ir->array_index))
217 return visit_continue_with_parent;
218
219 return visit_continue;
220 }
221
222 ir_visitor_status
223 ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
224 {
225 /* We don't want to descend into the function parameters and
226 * consider them as shader inputs or outputs.
227 */
228 visit_list_elements(this, &ir->body);
229 return visit_continue_with_parent;
230 }
231
232 ir_visitor_status
233 ir_set_program_inouts_visitor::visit_enter(ir_expression *ir)
234 {
235 if (this->shader_type == GL_FRAGMENT_SHADER &&
236 ir->operation == ir_unop_dFdy) {
237 gl_fragment_program *fprog = (gl_fragment_program *) prog;
238 fprog->UsesDFdy = true;
239 }
240 return visit_continue;
241 }
242
243 ir_visitor_status
244 ir_set_program_inouts_visitor::visit_enter(ir_discard *)
245 {
246 /* discards are only allowed in fragment shaders. */
247 assert(this->shader_type == GL_FRAGMENT_SHADER);
248
249 gl_fragment_program *fprog = (gl_fragment_program *) prog;
250 fprog->UsesKill = true;
251
252 return visit_continue;
253 }
254
255 void
256 do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
257 GLenum shader_type)
258 {
259 ir_set_program_inouts_visitor v(prog, shader_type);
260
261 prog->InputsRead = 0;
262 prog->OutputsWritten = 0;
263 prog->SystemValuesRead = 0;
264 if (shader_type == GL_FRAGMENT_SHADER) {
265 gl_fragment_program *fprog = (gl_fragment_program *) prog;
266 memset(fprog->InterpQualifier, 0, sizeof(fprog->InterpQualifier));
267 fprog->IsCentroid = 0;
268 fprog->UsesDFdy = false;
269 fprog->UsesKill = false;
270 }
271 visit_list_elements(&v, instructions);
272 }