glsl: Add an origin pointer in the function signature object.
[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.
30 *
31 * Mesa programs (gl_program, not gl_shader_program) have a set of
32 * flags indicating which varyings are read and written. Computing
33 * which are actually read from some sort of backend code can be
34 * tricky when variable array indexing involved. So this pass
35 * provides support for setting InputsRead and OutputsWritten right
36 * from the GLSL IR.
37 */
38
39 #include "main/core.h" /* for struct gl_program */
40 #include "program/hash_table.h"
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,
48 bool is_fragment_shader)
49 {
50 this->prog = prog;
51 this->is_fragment_shader = is_fragment_shader;
52 this->ht = hash_table_ctor(0,
53 hash_table_pointer_hash,
54 hash_table_pointer_compare);
55 }
56 ~ir_set_program_inouts_visitor()
57 {
58 hash_table_dtor(this->ht);
59 }
60
61 virtual ir_visitor_status visit_enter(ir_dereference_array *);
62 virtual ir_visitor_status visit_enter(ir_function_signature *);
63 virtual ir_visitor_status visit(ir_dereference_variable *);
64 virtual ir_visitor_status visit(ir_variable *);
65
66 struct gl_program *prog;
67 struct hash_table *ht;
68 bool is_fragment_shader;
69 };
70
71 static void
72 mark(struct gl_program *prog, ir_variable *var, int offset, int len,
73 bool is_fragment_shader)
74 {
75 /* As of GLSL 1.20, varyings can only be floats, floating-point
76 * vectors or matrices, or arrays of them. For Mesa programs using
77 * InputsRead/OutputsWritten, everything but matrices uses one
78 * slot, while matrices use a slot per column. Presumably
79 * something doing a more clever packing would use something other
80 * than InputsRead/OutputsWritten.
81 */
82
83 for (int i = 0; i < len; i++) {
84 GLbitfield64 bitfield = BITFIELD64_BIT(var->location + var->index + offset + i);
85 if (var->mode == ir_var_in) {
86 prog->InputsRead |= bitfield;
87 if (is_fragment_shader) {
88 gl_fragment_program *fprog = (gl_fragment_program *) prog;
89 fprog->InterpQualifier[var->location + var->index + offset + i] =
90 (glsl_interp_qualifier) var->interpolation;
91 }
92 } else if (var->mode == ir_var_system_value) {
93 prog->SystemValuesRead |= bitfield;
94 } else {
95 prog->OutputsWritten |= bitfield;
96 }
97 }
98 }
99
100 /* Default handler: Mark all the locations in the variable as used. */
101 ir_visitor_status
102 ir_set_program_inouts_visitor::visit(ir_dereference_variable *ir)
103 {
104 if (hash_table_find(this->ht, ir->var) == NULL)
105 return visit_continue;
106
107 if (ir->type->is_array()) {
108 mark(this->prog, ir->var, 0,
109 ir->type->length * ir->type->fields.array->matrix_columns,
110 this->is_fragment_shader);
111 } else {
112 mark(this->prog, ir->var, 0, ir->type->matrix_columns,
113 this->is_fragment_shader);
114 }
115
116 return visit_continue;
117 }
118
119 ir_visitor_status
120 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
121 {
122 ir_dereference_variable *deref_var;
123 ir_constant *index = ir->array_index->as_constant();
124 deref_var = ir->array->as_dereference_variable();
125 ir_variable *var = NULL;
126
127 /* Check that we're dereferencing a shader in or out */
128 if (deref_var)
129 var = (ir_variable *)hash_table_find(this->ht, deref_var->var);
130
131 if (index && var) {
132 int width = 1;
133
134 if (deref_var->type->is_array() &&
135 deref_var->type->fields.array->is_matrix()) {
136 width = deref_var->type->fields.array->matrix_columns;
137 }
138
139 mark(this->prog, var, index->value.i[0] * width, width,
140 this->is_fragment_shader);
141 return visit_continue_with_parent;
142 }
143
144 return visit_continue;
145 }
146
147 ir_visitor_status
148 ir_set_program_inouts_visitor::visit(ir_variable *ir)
149 {
150 if (ir->mode == ir_var_in ||
151 ir->mode == ir_var_out ||
152 ir->mode == ir_var_system_value) {
153 hash_table_insert(this->ht, ir, ir);
154 }
155
156 return visit_continue;
157 }
158
159 ir_visitor_status
160 ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
161 {
162 /* We don't want to descend into the function parameters and
163 * consider them as shader inputs or outputs.
164 */
165 visit_list_elements(this, &ir->body);
166 return visit_continue_with_parent;
167 }
168
169 void
170 do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
171 bool is_fragment_shader)
172 {
173 ir_set_program_inouts_visitor v(prog, is_fragment_shader);
174
175 prog->InputsRead = 0;
176 prog->OutputsWritten = 0;
177 prog->SystemValuesRead = 0;
178 if (is_fragment_shader) {
179 memset(((gl_fragment_program *) prog)->InterpQualifier, 0,
180 sizeof(((gl_fragment_program *) prog)->InterpQualifier));
181 }
182 visit_list_elements(&v, instructions);
183 }