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