2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file ir_set_program_inouts.cpp
27 * Sets the InputsRead and OutputsWritten of Mesa programs.
29 * Additionally, for fragment shaders, sets the InterpQualifier array, the
30 * IsCentroid bitfield, and the UsesDFdy flag.
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
40 #include "main/core.h" /* for struct gl_program */
42 #include "ir_visitor.h"
43 #include "glsl_types.h"
45 class ir_set_program_inouts_visitor
: public ir_hierarchical_visitor
{
47 ir_set_program_inouts_visitor(struct gl_program
*prog
, GLenum shader_type
)
50 this->shader_type
= shader_type
;
52 ~ir_set_program_inouts_visitor()
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
*);
63 void mark_whole_variable(ir_variable
*var
);
64 bool try_mark_partial_variable(ir_variable
*var
, ir_rvalue
*index
);
66 struct gl_program
*prog
;
71 is_shader_inout(ir_variable
*var
)
73 return var
->mode
== ir_var_shader_in
||
74 var
->mode
== ir_var_shader_out
||
75 var
->mode
== ir_var_system_value
;
79 mark(struct gl_program
*prog
, ir_variable
*var
, int offset
, int len
,
80 bool is_fragment_shader
)
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.
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
;
99 fprog
->IsCentroid
|= bitfield
;
101 } else if (var
->mode
== ir_var_system_value
) {
102 prog
->SystemValuesRead
|= bitfield
;
104 assert(var
->mode
== ir_var_shader_out
);
105 prog
->OutputsWritten
|= bitfield
;
111 * Mark an entire variable as used. Caller must ensure that the variable
112 * represents a shader input or output.
115 ir_set_program_inouts_visitor::mark_whole_variable(ir_variable
*var
)
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
;
123 mark(this->prog
, var
, 0, type
->count_attribute_slots(),
124 this->shader_type
== GL_FRAGMENT_SHADER
);
127 /* Default handler: Mark all the locations in the variable as used. */
129 ir_set_program_inouts_visitor::visit(ir_dereference_variable
*ir
)
131 if (!is_shader_inout(ir
->var
))
132 return visit_continue
;
134 mark_whole_variable(ir
->var
);
136 return visit_continue
;
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.
146 * *Except gl_PrimitiveIDIn, as noted below.
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.
152 ir_set_program_inouts_visitor::try_mark_partial_variable(ir_variable
*var
,
155 const glsl_type
*type
= var
->type
;
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.
163 assert(type
->is_array());
164 type
= type
->fields
.array
;
167 /* The code below only handles:
169 * - Indexing into matrices
170 * - Indexing into arrays of (matrices, vectors, or scalars)
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
178 if (!(type
->is_matrix() ||
180 (type
->fields
.array
->is_numeric() ||
181 type
->fields
.array
->is_boolean())))) {
182 assert(!"Unexpected indexing in ir_set_program_inouts");
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.
190 ir_constant
*index_as_constant
= index
->as_constant();
191 if (!index_as_constant
)
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
;
203 num_elems
= type
->matrix_columns
;
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.
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.
220 mark(this->prog
, var
, index_as_constant
->value
.u
[0] * elem_width
,
221 elem_width
, this->shader_type
== GL_FRAGMENT_SHADER
);
226 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array
*ir
)
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.
232 if (ir_dereference_array
* const inner_array
=
233 ir
->array
->as_dereference_array()) {
235 * inner_array => foo[i]
237 if (ir_dereference_variable
* const deref_var
=
238 inner_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.
244 if (try_mark_partial_variable(deref_var
->var
, ir
->array_index
))
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.
250 inner_array
->array_index
->accept(this);
251 return visit_continue_with_parent
;
255 } else if (ir_dereference_variable
* const deref_var
=
256 ir
->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.
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.
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.
274 if (try_mark_partial_variable(deref_var
->var
, ir
->array_index
))
275 return visit_continue_with_parent
;
279 /* The expression is something we don't recognize. Just visit its
282 return visit_continue
;
286 ir_set_program_inouts_visitor::visit_enter(ir_function_signature
*ir
)
288 /* We don't want to descend into the function parameters and
289 * consider them as shader inputs or outputs.
291 visit_list_elements(this, &ir
->body
);
292 return visit_continue_with_parent
;
296 ir_set_program_inouts_visitor::visit_enter(ir_expression
*ir
)
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;
303 return visit_continue
;
307 ir_set_program_inouts_visitor::visit_enter(ir_discard
*)
309 /* discards are only allowed in fragment shaders. */
310 assert(this->shader_type
== GL_FRAGMENT_SHADER
);
312 gl_fragment_program
*fprog
= (gl_fragment_program
*) prog
;
313 fprog
->UsesKill
= true;
315 return visit_continue
;
319 do_set_program_inouts(exec_list
*instructions
, struct gl_program
*prog
,
322 ir_set_program_inouts_visitor
v(prog
, shader_type
);
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;
334 visit_list_elements(&v
, instructions
);