glsl: Fix handling of function calls inside nested loops.
[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 and IsSample bitfields, 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 namespace {
46
47 class ir_set_program_inouts_visitor : public ir_hierarchical_visitor {
48 public:
49 ir_set_program_inouts_visitor(struct gl_program *prog, GLenum shader_type)
50 {
51 this->prog = prog;
52 this->shader_type = shader_type;
53 }
54 ~ir_set_program_inouts_visitor()
55 {
56 }
57
58 virtual ir_visitor_status visit_enter(ir_dereference_array *);
59 virtual ir_visitor_status visit_enter(ir_function_signature *);
60 virtual ir_visitor_status visit_enter(ir_expression *);
61 virtual ir_visitor_status visit_enter(ir_discard *);
62 virtual ir_visitor_status visit_enter(ir_texture *);
63 virtual ir_visitor_status visit(ir_dereference_variable *);
64
65 private:
66 void mark_whole_variable(ir_variable *var);
67 bool try_mark_partial_variable(ir_variable *var, ir_rvalue *index);
68
69 struct gl_program *prog;
70 GLenum shader_type;
71 };
72
73 } /* anonymous namespace */
74
75 static inline bool
76 is_shader_inout(ir_variable *var)
77 {
78 return var->mode == ir_var_shader_in ||
79 var->mode == ir_var_shader_out ||
80 var->mode == ir_var_system_value;
81 }
82
83 static void
84 mark(struct gl_program *prog, ir_variable *var, int offset, int len,
85 bool is_fragment_shader)
86 {
87 /* As of GLSL 1.20, varyings can only be floats, floating-point
88 * vectors or matrices, or arrays of them. For Mesa programs using
89 * InputsRead/OutputsWritten, everything but matrices uses one
90 * slot, while matrices use a slot per column. Presumably
91 * something doing a more clever packing would use something other
92 * than InputsRead/OutputsWritten.
93 */
94
95 for (int i = 0; i < len; i++) {
96 GLbitfield64 bitfield = BITFIELD64_BIT(var->location + var->index + offset + i);
97 if (var->mode == ir_var_shader_in) {
98 prog->InputsRead |= bitfield;
99 if (is_fragment_shader) {
100 gl_fragment_program *fprog = (gl_fragment_program *) prog;
101 fprog->InterpQualifier[var->location + var->index + offset + i] =
102 (glsl_interp_qualifier) var->interpolation;
103 if (var->centroid)
104 fprog->IsCentroid |= bitfield;
105 if (var->sample)
106 fprog->IsSample |= bitfield;
107 }
108 } else if (var->mode == ir_var_system_value) {
109 prog->SystemValuesRead |= bitfield;
110 } else {
111 assert(var->mode == ir_var_shader_out);
112 prog->OutputsWritten |= bitfield;
113 }
114 }
115 }
116
117 /**
118 * Mark an entire variable as used. Caller must ensure that the variable
119 * represents a shader input or output.
120 */
121 void
122 ir_set_program_inouts_visitor::mark_whole_variable(ir_variable *var)
123 {
124 const glsl_type *type = var->type;
125 if (this->shader_type == GL_GEOMETRY_SHADER &&
126 var->mode == ir_var_shader_in && type->is_array()) {
127 type = type->fields.array;
128 }
129
130 mark(this->prog, var, 0, type->count_attribute_slots(),
131 this->shader_type == GL_FRAGMENT_SHADER);
132 }
133
134 /* Default handler: Mark all the locations in the variable as used. */
135 ir_visitor_status
136 ir_set_program_inouts_visitor::visit(ir_dereference_variable *ir)
137 {
138 if (!is_shader_inout(ir->var))
139 return visit_continue;
140
141 mark_whole_variable(ir->var);
142
143 return visit_continue;
144 }
145
146 /**
147 * Try to mark a portion of the given variable as used. Caller must ensure
148 * that the variable represents a shader input or output which can be indexed
149 * into in array fashion (an array or matrix). For the purpose of geometry
150 * shader inputs (which are always arrays*), this means that the array element
151 * must be something that can be indexed into in array fashion.
152 *
153 * *Except gl_PrimitiveIDIn, as noted below.
154 *
155 * If the index can't be interpreted as a constant, or some other problem
156 * occurs, then nothing will be marked and false will be returned.
157 */
158 bool
159 ir_set_program_inouts_visitor::try_mark_partial_variable(ir_variable *var,
160 ir_rvalue *index)
161 {
162 const glsl_type *type = var->type;
163
164 if (this->shader_type == GL_GEOMETRY_SHADER &&
165 var->mode == ir_var_shader_in) {
166 /* The only geometry shader input that is not an array is
167 * gl_PrimitiveIDIn, and in that case, this code will never be reached,
168 * because gl_PrimitiveIDIn can't be indexed into in array fashion.
169 */
170 assert(type->is_array());
171 type = type->fields.array;
172 }
173
174 /* The code below only handles:
175 *
176 * - Indexing into matrices
177 * - Indexing into arrays of (matrices, vectors, or scalars)
178 *
179 * All other possibilities are either prohibited by GLSL (vertex inputs and
180 * fragment outputs can't be structs) or should have been eliminated by
181 * lowering passes (do_vec_index_to_swizzle() gets rid of indexing into
182 * vectors, and lower_packed_varyings() gets rid of structs that occur in
183 * varyings).
184 */
185 if (!(type->is_matrix() ||
186 (type->is_array() &&
187 (type->fields.array->is_numeric() ||
188 type->fields.array->is_boolean())))) {
189 assert(!"Unexpected indexing in ir_set_program_inouts");
190
191 /* For safety in release builds, in case we ever encounter unexpected
192 * indexing, give up and let the caller mark the whole variable as used.
193 */
194 return false;
195 }
196
197 ir_constant *index_as_constant = index->as_constant();
198 if (!index_as_constant)
199 return false;
200
201 unsigned elem_width;
202 unsigned num_elems;
203 if (type->is_array()) {
204 num_elems = type->length;
205 if (type->fields.array->is_matrix())
206 elem_width = type->fields.array->matrix_columns;
207 else
208 elem_width = 1;
209 } else {
210 num_elems = type->matrix_columns;
211 elem_width = 1;
212 }
213
214 if (index_as_constant->value.u[0] >= num_elems) {
215 /* Constant index outside the bounds of the matrix/array. This could
216 * arise as a result of constant folding of a legal GLSL program.
217 *
218 * Even though the spec says that indexing outside the bounds of a
219 * matrix/array results in undefined behaviour, we don't want to pass
220 * out-of-range values to mark() (since this could result in slots that
221 * don't exist being marked as used), so just let the caller mark the
222 * whole variable as used.
223 */
224 return false;
225 }
226
227 mark(this->prog, var, index_as_constant->value.u[0] * elem_width,
228 elem_width, this->shader_type == GL_FRAGMENT_SHADER);
229 return true;
230 }
231
232 ir_visitor_status
233 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
234 {
235 /* Note: for geometry shader inputs, lower_named_interface_blocks may
236 * create 2D arrays, so we need to be able to handle those. 2D arrays
237 * shouldn't be able to crop up for any other reason.
238 */
239 if (ir_dereference_array * const inner_array =
240 ir->array->as_dereference_array()) {
241 /* ir => foo[i][j]
242 * inner_array => foo[i]
243 */
244 if (ir_dereference_variable * const deref_var =
245 inner_array->array->as_dereference_variable()) {
246 if (this->shader_type == GL_GEOMETRY_SHADER &&
247 deref_var->var->mode == ir_var_shader_in) {
248 /* foo is a geometry shader input, so i is the vertex, and j the
249 * part of the input we're accessing.
250 */
251 if (try_mark_partial_variable(deref_var->var, ir->array_index))
252 {
253 /* We've now taken care of foo and j, but i might contain a
254 * subexpression that accesses shader inputs. So manually
255 * visit i and then continue with the parent.
256 */
257 inner_array->array_index->accept(this);
258 return visit_continue_with_parent;
259 }
260 }
261 }
262 } else if (ir_dereference_variable * const deref_var =
263 ir->array->as_dereference_variable()) {
264 /* ir => foo[i], where foo is a variable. */
265 if (this->shader_type == GL_GEOMETRY_SHADER &&
266 deref_var->var->mode == ir_var_shader_in) {
267 /* foo is a geometry shader input, so i is the vertex, and we're
268 * accessing the entire input.
269 */
270 mark_whole_variable(deref_var->var);
271 /* We've now taken care of foo, but i might contain a subexpression
272 * that accesses shader inputs. So manually visit i and then
273 * continue with the parent.
274 */
275 ir->array_index->accept(this);
276 return visit_continue_with_parent;
277 } else if (is_shader_inout(deref_var->var)) {
278 /* foo is a shader input/output, but not a geometry shader input,
279 * so i is the part of the input we're accessing.
280 */
281 if (try_mark_partial_variable(deref_var->var, ir->array_index))
282 return visit_continue_with_parent;
283 }
284 }
285
286 /* The expression is something we don't recognize. Just visit its
287 * subexpressions.
288 */
289 return visit_continue;
290 }
291
292 ir_visitor_status
293 ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
294 {
295 /* We don't want to descend into the function parameters and
296 * consider them as shader inputs or outputs.
297 */
298 visit_list_elements(this, &ir->body);
299 return visit_continue_with_parent;
300 }
301
302 ir_visitor_status
303 ir_set_program_inouts_visitor::visit_enter(ir_expression *ir)
304 {
305 if (this->shader_type == GL_FRAGMENT_SHADER &&
306 ir->operation == ir_unop_dFdy) {
307 gl_fragment_program *fprog = (gl_fragment_program *) prog;
308 fprog->UsesDFdy = true;
309 }
310 return visit_continue;
311 }
312
313 ir_visitor_status
314 ir_set_program_inouts_visitor::visit_enter(ir_discard *)
315 {
316 /* discards are only allowed in fragment shaders. */
317 assert(this->shader_type == GL_FRAGMENT_SHADER);
318
319 gl_fragment_program *fprog = (gl_fragment_program *) prog;
320 fprog->UsesKill = true;
321
322 return visit_continue;
323 }
324
325 ir_visitor_status
326 ir_set_program_inouts_visitor::visit_enter(ir_texture *ir)
327 {
328 if (ir->op == ir_tg4)
329 prog->UsesGather = true;
330 return visit_continue;
331 }
332
333 void
334 do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
335 GLenum shader_type)
336 {
337 ir_set_program_inouts_visitor v(prog, shader_type);
338
339 prog->InputsRead = 0;
340 prog->OutputsWritten = 0;
341 prog->SystemValuesRead = 0;
342 if (shader_type == GL_FRAGMENT_SHADER) {
343 gl_fragment_program *fprog = (gl_fragment_program *) prog;
344 memset(fprog->InterpQualifier, 0, sizeof(fprog->InterpQualifier));
345 fprog->IsCentroid = 0;
346 fprog->IsSample = 0;
347 fprog->UsesDFdy = false;
348 fprog->UsesKill = false;
349 }
350 visit_list_elements(&v, instructions);
351 }