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