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 and
30 * IsCentroid bitfield.
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 */
41 #include "program/hash_table.h"
43 #include "ir_visitor.h"
44 #include "glsl_types.h"
46 class ir_set_program_inouts_visitor
: public ir_hierarchical_visitor
{
48 ir_set_program_inouts_visitor(struct gl_program
*prog
,
49 bool is_fragment_shader
)
52 this->is_fragment_shader
= is_fragment_shader
;
53 this->ht
= hash_table_ctor(0,
54 hash_table_pointer_hash
,
55 hash_table_pointer_compare
);
57 ~ir_set_program_inouts_visitor()
59 hash_table_dtor(this->ht
);
62 virtual ir_visitor_status
visit_enter(ir_dereference_array
*);
63 virtual ir_visitor_status
visit_enter(ir_function_signature
*);
64 virtual ir_visitor_status
visit(ir_dereference_variable
*);
65 virtual ir_visitor_status
visit(ir_variable
*);
67 struct gl_program
*prog
;
68 struct hash_table
*ht
;
69 bool is_fragment_shader
;
73 mark(struct gl_program
*prog
, ir_variable
*var
, int offset
, int len
,
74 bool is_fragment_shader
)
76 /* As of GLSL 1.20, varyings can only be floats, floating-point
77 * vectors or matrices, or arrays of them. For Mesa programs using
78 * InputsRead/OutputsWritten, everything but matrices uses one
79 * slot, while matrices use a slot per column. Presumably
80 * something doing a more clever packing would use something other
81 * than InputsRead/OutputsWritten.
84 for (int i
= 0; i
< len
; i
++) {
85 GLbitfield64 bitfield
= BITFIELD64_BIT(var
->location
+ var
->index
+ offset
+ i
);
86 if (var
->mode
== ir_var_in
) {
87 prog
->InputsRead
|= bitfield
;
88 if (is_fragment_shader
) {
89 gl_fragment_program
*fprog
= (gl_fragment_program
*) prog
;
90 fprog
->InterpQualifier
[var
->location
+ var
->index
+ offset
+ i
] =
91 (glsl_interp_qualifier
) var
->interpolation
;
93 fprog
->IsCentroid
|= bitfield
;
95 } else if (var
->mode
== ir_var_system_value
) {
96 prog
->SystemValuesRead
|= bitfield
;
98 prog
->OutputsWritten
|= bitfield
;
103 /* Default handler: Mark all the locations in the variable as used. */
105 ir_set_program_inouts_visitor::visit(ir_dereference_variable
*ir
)
107 if (hash_table_find(this->ht
, ir
->var
) == NULL
)
108 return visit_continue
;
110 if (ir
->type
->is_array()) {
111 mark(this->prog
, ir
->var
, 0,
112 ir
->type
->length
* ir
->type
->fields
.array
->matrix_columns
,
113 this->is_fragment_shader
);
115 mark(this->prog
, ir
->var
, 0, ir
->type
->matrix_columns
,
116 this->is_fragment_shader
);
119 return visit_continue
;
123 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array
*ir
)
125 ir_dereference_variable
*deref_var
;
126 ir_constant
*index
= ir
->array_index
->as_constant();
127 deref_var
= ir
->array
->as_dereference_variable();
128 ir_variable
*var
= NULL
;
130 /* Check that we're dereferencing a shader in or out */
132 var
= (ir_variable
*)hash_table_find(this->ht
, deref_var
->var
);
137 if (deref_var
->type
->is_array() &&
138 deref_var
->type
->fields
.array
->is_matrix()) {
139 width
= deref_var
->type
->fields
.array
->matrix_columns
;
142 mark(this->prog
, var
, index
->value
.i
[0] * width
, width
,
143 this->is_fragment_shader
);
144 return visit_continue_with_parent
;
147 return visit_continue
;
151 ir_set_program_inouts_visitor::visit(ir_variable
*ir
)
153 if (ir
->mode
== ir_var_in
||
154 ir
->mode
== ir_var_out
||
155 ir
->mode
== ir_var_system_value
) {
156 hash_table_insert(this->ht
, ir
, ir
);
159 return visit_continue
;
163 ir_set_program_inouts_visitor::visit_enter(ir_function_signature
*ir
)
165 /* We don't want to descend into the function parameters and
166 * consider them as shader inputs or outputs.
168 visit_list_elements(this, &ir
->body
);
169 return visit_continue_with_parent
;
173 do_set_program_inouts(exec_list
*instructions
, struct gl_program
*prog
,
174 bool is_fragment_shader
)
176 ir_set_program_inouts_visitor
v(prog
, is_fragment_shader
);
178 prog
->InputsRead
= 0;
179 prog
->OutputsWritten
= 0;
180 prog
->SystemValuesRead
= 0;
181 if (is_fragment_shader
) {
182 memset(((gl_fragment_program
*) prog
)->InterpQualifier
, 0,
183 sizeof(((gl_fragment_program
*) prog
)->InterpQualifier
));
184 ((gl_fragment_program
*) prog
)->IsCentroid
= 0;
186 visit_list_elements(&v
, instructions
);