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