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 mark(this->prog
, var
, 0, var
->type
->count_attribute_slots(),
118 this->shader_type
== GL_FRAGMENT_SHADER
);
121 /* Default handler: Mark all the locations in the variable as used. */
123 ir_set_program_inouts_visitor::visit(ir_dereference_variable
*ir
)
125 if (!is_shader_inout(ir
->var
))
126 return visit_continue
;
128 mark_whole_variable(ir
->var
);
130 return visit_continue
;
134 * Try to mark a portion of the given variable as used. Caller must ensure
135 * that the variable represents a shader input or output which can be indexed
136 * into in array fashion (an array or matrix).
138 * If the index can't be interpreted as a constant, or some other problem
139 * occurs, then nothing will be marked and false will be returned.
142 ir_set_program_inouts_visitor::try_mark_partial_variable(ir_variable
*var
,
145 const glsl_type
*type
= var
->type
;
147 /* The code below only handles:
149 * - Indexing into matrices
150 * - Indexing into arrays of (matrices, vectors, or scalars)
152 * All other possibilities are either prohibited by GLSL (vertex inputs and
153 * fragment outputs can't be structs) or should have been eliminated by
154 * lowering passes (do_vec_index_to_swizzle() gets rid of indexing into
155 * vectors, and lower_packed_varyings() gets rid of structs that occur in
158 if (!(type
->is_matrix() ||
160 (type
->fields
.array
->is_numeric() ||
161 type
->fields
.array
->is_boolean())))) {
162 assert(!"Unexpected indexing in ir_set_program_inouts");
164 /* For safety in release builds, in case we ever encounter unexpected
165 * indexing, give up and let the caller mark the whole variable as used.
170 ir_constant
*index_as_constant
= index
->as_constant();
171 if (!index_as_constant
)
176 if (type
->is_array()) {
177 num_elems
= type
->length
;
178 if (type
->fields
.array
->is_matrix())
179 elem_width
= type
->fields
.array
->matrix_columns
;
183 num_elems
= type
->matrix_columns
;
187 if (index_as_constant
->value
.u
[0] >= num_elems
) {
188 /* Constant index outside the bounds of the matrix/array. This could
189 * arise as a result of constant folding of a legal GLSL program.
191 * Even though the spec says that indexing outside the bounds of a
192 * matrix/array results in undefined behaviour, we don't want to pass
193 * out-of-range values to mark() (since this could result in slots that
194 * don't exist being marked as used), so just let the caller mark the
195 * whole variable as used.
200 mark(this->prog
, var
, index_as_constant
->value
.u
[0] * elem_width
,
201 elem_width
, this->shader_type
== GL_FRAGMENT_SHADER
);
206 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array
*ir
)
208 ir_dereference_variable
*deref_var
;
209 deref_var
= ir
->array
->as_dereference_variable();
210 ir_variable
*var
= deref_var
? deref_var
->var
: NULL
;
212 /* Check that we're dereferencing a shader in or out */
213 if (!var
|| !is_shader_inout(var
))
214 return visit_continue
;
216 if (try_mark_partial_variable(var
, ir
->array_index
))
217 return visit_continue_with_parent
;
219 return visit_continue
;
223 ir_set_program_inouts_visitor::visit_enter(ir_function_signature
*ir
)
225 /* We don't want to descend into the function parameters and
226 * consider them as shader inputs or outputs.
228 visit_list_elements(this, &ir
->body
);
229 return visit_continue_with_parent
;
233 ir_set_program_inouts_visitor::visit_enter(ir_expression
*ir
)
235 if (this->shader_type
== GL_FRAGMENT_SHADER
&&
236 ir
->operation
== ir_unop_dFdy
) {
237 gl_fragment_program
*fprog
= (gl_fragment_program
*) prog
;
238 fprog
->UsesDFdy
= true;
240 return visit_continue
;
244 ir_set_program_inouts_visitor::visit_enter(ir_discard
*)
246 /* discards are only allowed in fragment shaders. */
247 assert(this->shader_type
== GL_FRAGMENT_SHADER
);
249 gl_fragment_program
*fprog
= (gl_fragment_program
*) prog
;
250 fprog
->UsesKill
= true;
252 return visit_continue
;
256 do_set_program_inouts(exec_list
*instructions
, struct gl_program
*prog
,
259 ir_set_program_inouts_visitor
v(prog
, shader_type
);
261 prog
->InputsRead
= 0;
262 prog
->OutputsWritten
= 0;
263 prog
->SystemValuesRead
= 0;
264 if (shader_type
== GL_FRAGMENT_SHADER
) {
265 gl_fragment_program
*fprog
= (gl_fragment_program
*) prog
;
266 memset(fprog
->InterpQualifier
, 0, sizeof(fprog
->InterpQualifier
));
267 fprog
->IsCentroid
= 0;
268 fprog
->UsesDFdy
= false;
269 fprog
->UsesKill
= false;
271 visit_list_elements(&v
, instructions
);