glsl: Convert output read lowering to the util hash table
[mesa.git] / src / compiler / glsl / lower_output_reads.cpp
1 /*
2 * Copyright © 2012 Vincent Lejeune
3 * Copyright © 2012 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "ir.h"
26 #include "util/hash_table.h"
27
28 /**
29 * \file lower_output_reads.cpp
30 *
31 * In GLSL, shader output variables (such as varyings) can be both read and
32 * written. However, on some hardware, reading an output register causes
33 * trouble.
34 *
35 * This pass creates temporary shadow copies of every (used) shader output,
36 * and replaces all accesses to use those instead. It also adds code to the
37 * main() function to copy the final values to the actual shader outputs.
38 */
39
40 namespace {
41
42 class output_read_remover : public ir_hierarchical_visitor {
43 protected:
44 /**
45 * A hash table mapping from the original ir_variable shader outputs
46 * (ir_var_shader_out mode) to the new temporaries to be used instead.
47 */
48 hash_table *replacements;
49
50 void *mem_ctx;
51
52 unsigned stage;
53 public:
54 output_read_remover(unsigned stage);
55 ~output_read_remover();
56 virtual ir_visitor_status visit(class ir_dereference_variable *);
57 virtual ir_visitor_status visit_leave(class ir_emit_vertex *);
58 virtual ir_visitor_status visit_leave(class ir_return *);
59 virtual ir_visitor_status visit_leave(class ir_function_signature *);
60 };
61
62 } /* anonymous namespace */
63
64 /**
65 * Hash function for the output variables - computes the hash of the name.
66 * NOTE: We're using the name string to ensure that the hash doesn't depend
67 * on any random factors, otherwise the output_read_remover could produce
68 * the random order of the assignments.
69 *
70 * NOTE: If you want to reuse this function please take into account that
71 * generally the names of the variables are non-unique.
72 */
73 static unsigned
74 hash_table_var_hash(const void *key)
75 {
76 const ir_variable * var = static_cast<const ir_variable *>(key);
77 return _mesa_key_hash_string(var->name);
78 }
79
80 output_read_remover::output_read_remover(unsigned stage)
81 {
82 this->stage = stage;
83 mem_ctx = ralloc_context(NULL);
84 replacements = _mesa_hash_table_create(NULL, hash_table_var_hash,
85 _mesa_key_pointer_equal);
86 }
87
88 output_read_remover::~output_read_remover()
89 {
90 _mesa_hash_table_destroy(replacements, NULL);
91 ralloc_free(mem_ctx);
92 }
93
94 ir_visitor_status
95 output_read_remover::visit(ir_dereference_variable *ir)
96 {
97 if (ir->var->data.mode != ir_var_shader_out)
98 return visit_continue;
99 if (stage == MESA_SHADER_TESS_CTRL)
100 return visit_continue;
101
102 hash_entry *entry = _mesa_hash_table_search(replacements, ir->var);
103 ir_variable *temp = entry ? (ir_variable *) entry->data : NULL;
104
105 /* If we don't have an existing temporary, create one. */
106 if (temp == NULL) {
107 void *var_ctx = ralloc_parent(ir->var);
108 temp = new(var_ctx) ir_variable(ir->var->type, ir->var->name,
109 ir_var_temporary);
110 _mesa_hash_table_insert(replacements, ir->var, temp);
111 ir->var->insert_after(temp);
112 }
113
114 /* Update the dereference to use the temporary */
115 ir->var = temp;
116
117 return visit_continue;
118 }
119
120 /**
121 * Create an assignment to copy a temporary value back to the actual output.
122 */
123 static ir_assignment *
124 copy(void *ctx, ir_variable *output, ir_variable *temp)
125 {
126 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(output);
127 ir_dereference_variable *rhs = new(ctx) ir_dereference_variable(temp);
128 return new(ctx) ir_assignment(lhs, rhs);
129 }
130
131 /** Insert a copy-back assignment before a "return" statement or a call to
132 * EmitVertex().
133 */
134 static void
135 emit_return_copy(const void *key, void *data, void *closure)
136 {
137 ir_return *ir = (ir_return *) closure;
138 ir->insert_before(copy(ir, (ir_variable *) key, (ir_variable *) data));
139 }
140
141 /** Insert a copy-back assignment at the end of the main() function */
142 static void
143 emit_main_copy(const void *key, void *data, void *closure)
144 {
145 ir_function_signature *sig = (ir_function_signature *) closure;
146 sig->body.push_tail(copy(sig, (ir_variable *) key, (ir_variable *) data));
147 }
148
149 ir_visitor_status
150 output_read_remover::visit_leave(ir_return *ir)
151 {
152 hash_table_call_foreach(replacements, emit_return_copy, ir);
153 return visit_continue;
154 }
155
156 ir_visitor_status
157 output_read_remover::visit_leave(ir_emit_vertex *ir)
158 {
159 hash_table_call_foreach(replacements, emit_return_copy, ir);
160 _mesa_hash_table_clear(replacements, NULL);
161 return visit_continue;
162 }
163
164 ir_visitor_status
165 output_read_remover::visit_leave(ir_function_signature *sig)
166 {
167 if (strcmp(sig->function_name(), "main") != 0)
168 return visit_continue;
169
170 hash_table_call_foreach(replacements, emit_main_copy, sig);
171 return visit_continue;
172 }
173
174 void
175 lower_output_reads(unsigned stage, exec_list *instructions)
176 {
177 output_read_remover v(stage);
178 visit_list_elements(&v, instructions);
179 }