415b541c3efd9be396ac62d3f9cd991cc5e46729
[mesa.git] / src / 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 "program/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 class output_read_remover : public ir_hierarchical_visitor {
41 protected:
42 /**
43 * A hash table mapping from the original ir_variable shader outputs
44 * (ir_var_out mode) to the new temporaries to be used instead.
45 */
46 hash_table *replacements;
47
48 void *mem_ctx;
49 public:
50 output_read_remover();
51 ~output_read_remover();
52 virtual ir_visitor_status visit(class ir_dereference_variable *);
53 virtual ir_visitor_status visit_leave(class ir_return *);
54 virtual ir_visitor_status visit_leave(class ir_function_signature *);
55 };
56
57 output_read_remover::output_read_remover()
58 {
59 mem_ctx = ralloc_context(NULL);
60 replacements =
61 hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
62 }
63
64 output_read_remover::~output_read_remover()
65 {
66 hash_table_dtor(replacements);
67 ralloc_free(mem_ctx);
68 }
69
70 ir_visitor_status
71 output_read_remover::visit(ir_dereference_variable *ir)
72 {
73 if (ir->var->mode != ir_var_out)
74 return visit_continue;
75
76 ir_variable *temp = (ir_variable *) hash_table_find(replacements, ir->var);
77
78 /* If we don't have an existing temporary, create one. */
79 if (temp == NULL) {
80 void *var_ctx = ralloc_parent(ir->var);
81 temp = new(var_ctx) ir_variable(ir->var->type, ir->var->name,
82 ir_var_temporary);
83 hash_table_insert(replacements, temp, ir->var);
84 }
85
86 /* Update the dereference to use the temporary */
87 ir->var = temp;
88
89 return visit_continue;
90 }
91
92 /**
93 * Create an assignment to copy a temporary value back to the actual output.
94 */
95 static ir_assignment *
96 copy(void *ctx, ir_variable *output, ir_variable *temp)
97 {
98 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(output);
99 ir_dereference_variable *rhs = new(ctx) ir_dereference_variable(temp);
100 return new(ctx) ir_assignment(lhs, rhs);
101 }
102
103 /** Insert a copy-back assignment before a "return" statement */
104 static void
105 emit_return_copy(const void *key, void *data, void *closure)
106 {
107 ir_return *ir = (ir_return *) closure;
108 ir->insert_before(copy(ir, (ir_variable *) key, (ir_variable *) data));
109 }
110
111 /** Insert a copy-back assignment at the end of the main() function */
112 static void
113 emit_main_copy(const void *key, void *data, void *closure)
114 {
115 ir_function_signature *sig = (ir_function_signature *) closure;
116 sig->body.push_tail(copy(sig, (ir_variable *) key, (ir_variable *) data));
117 }
118
119 ir_visitor_status
120 output_read_remover::visit_leave(ir_return *ir)
121 {
122 hash_table_call_foreach(replacements, emit_return_copy, ir);
123 return visit_continue;
124 }
125
126 ir_visitor_status
127 output_read_remover::visit_leave(ir_function_signature *sig)
128 {
129 if (strcmp(sig->function_name(), "main") != 0)
130 return visit_continue;
131
132 hash_table_call_foreach(replacements, emit_main_copy, sig);
133 return visit_continue;
134 }
135
136 void
137 lower_output_reads(exec_list *instructions)
138 {
139 output_read_remover v;
140 visit_list_elements(&v, instructions);
141 }