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