ir_to_mesa: Support texture rectangle targets
[mesa/nouveau-pmpeg.git] / src / glsl / ir_set_program_inouts.cpp
blob658637775a44bf6fdd7e77a4a6d50b5fc0b7931e
1 /*
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
13 * Software.
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.
24 /**
25 * \file ir_set_program_inouts.cpp
27 * Sets the InputsRead and OutputsWritten of Mesa programs.
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.
37 extern "C" {
38 #include "main/mtypes.h"
39 #include "program/hash_table.h"
41 #include "ir.h"
42 #include "ir_visitor.h"
43 #include "glsl_types.h"
45 class ir_set_program_inouts_visitor : public ir_hierarchical_visitor {
46 public:
47 ir_set_program_inouts_visitor(struct gl_program *prog)
49 this->prog = prog;
50 this->ht = hash_table_ctor(0,
51 hash_table_pointer_hash,
52 hash_table_pointer_compare);
54 ir_set_program_inouts_visitor()
56 hash_table_dtor(this->ht);
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 *);
64 struct gl_program *prog;
65 struct hash_table *ht;
68 static void
69 mark(struct gl_program *prog, ir_variable *var, int index)
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.
78 const glsl_type *element_type;
79 int element_size;
81 if (var->type->is_array())
82 element_type = var->type->fields.array;
83 else
84 element_type = var->type;
86 if (element_type->is_matrix())
87 element_size = element_type->matrix_columns;
88 else
89 element_size = 1;
91 index *= element_size;
92 for (int i = 0; i < element_size; i++) {
93 if (var->mode == ir_var_in)
94 prog->InputsRead |= BITFIELD64_BIT(var->location + index + i);
95 else
96 prog->OutputsWritten |= BITFIELD64_BIT(var->location + index + i);
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)
104 if (hash_table_find(this->ht, ir->var) == NULL)
105 return visit_continue;
107 if (ir->type->is_array()) {
108 for (unsigned int i = 0; i < ir->type->length; i++) {
109 mark(this->prog, ir->var, i);
111 } else {
112 mark(this->prog, ir->var, 0);
115 return visit_continue;
118 ir_visitor_status
119 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
121 ir_dereference_variable *deref_var;
122 ir_constant *index = ir->array_index->as_constant();
123 deref_var = ir->array->as_dereference_variable();
124 ir_variable *var = NULL;
126 /* Check that we're dereferencing a shader in or out */
127 if (deref_var)
128 var = (ir_variable *)hash_table_find(this->ht, deref_var->var);
130 if (index && var) {
131 mark(this->prog, var, index->value.i[0]);
132 return visit_continue_with_parent;
135 return visit_continue;
138 ir_visitor_status
139 ir_set_program_inouts_visitor::visit(ir_variable *ir)
141 if (ir->mode == ir_var_in ||
142 ir->mode == ir_var_out) {
143 hash_table_insert(this->ht, ir, ir);
146 return visit_continue;
149 ir_visitor_status
150 ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
152 /* We don't want to descend into the function parameters and
153 * consider them as shader inputs or outputs.
155 visit_list_elements(this, &ir->body);
156 return visit_continue_with_parent;
159 void
160 do_set_program_inouts(exec_list *instructions, struct gl_program *prog)
162 ir_set_program_inouts_visitor v(prog);
164 prog->InputsRead = 0;
165 prog->OutputsWritten = 0;
166 visit_list_elements(&v, instructions);