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