st/nine: Fix CND implementation
[mesa.git] / src / glsl / lower_const_arrays_to_uniforms.cpp
1 /*
2 * Copyright © 2014 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 lower_const_arrays_to_uniforms.cpp
26 *
27 * Lower constant arrays to uniform arrays.
28 *
29 * Some driver backends (such as i965 and nouveau) don't handle constant arrays
30 * gracefully, instead treating them as ordinary writable temporary arrays.
31 * Since arrays can be large, this often means spilling them to scratch memory,
32 * which usually involves a large number of instructions.
33 *
34 * This must be called prior to link_set_uniform_initializers(); we need the
35 * linker to process our new uniform's constant initializer.
36 *
37 * This should be called after optimizations, since those can result in
38 * splitting and removing arrays that are indexed by constant expressions.
39 */
40 #include "ir.h"
41 #include "ir_visitor.h"
42 #include "ir_rvalue_visitor.h"
43 #include "glsl_types.h"
44
45 namespace {
46 class lower_const_array_visitor : public ir_rvalue_visitor {
47 public:
48 lower_const_array_visitor(exec_list *insts)
49 {
50 instructions = insts;
51 progress = false;
52 index = 0;
53 }
54
55 bool run()
56 {
57 visit_list_elements(this, instructions);
58 return progress;
59 }
60
61 void handle_rvalue(ir_rvalue **rvalue);
62
63 private:
64 exec_list *instructions;
65 bool progress;
66 unsigned index;
67 };
68
69 void
70 lower_const_array_visitor::handle_rvalue(ir_rvalue **rvalue)
71 {
72 if (!*rvalue)
73 return;
74
75 ir_dereference_array *dra = (*rvalue)->as_dereference_array();
76 if (!dra)
77 return;
78
79 ir_constant *con = dra->array->as_constant();
80 if (!con || !con->type->is_array())
81 return;
82
83 void *mem_ctx = ralloc_parent(con);
84
85 char *uniform_name = ralloc_asprintf(mem_ctx, "constarray__%d", index++);
86
87 ir_variable *uni =
88 new(mem_ctx) ir_variable(con->type, uniform_name, ir_var_uniform);
89 uni->constant_initializer = con;
90 uni->constant_value = con;
91 uni->data.has_initializer = true;
92 uni->data.how_declared = ir_var_hidden;
93 uni->data.read_only = true;
94 /* Assume the whole thing is accessed. */
95 uni->data.max_array_access = uni->type->length - 1;
96 instructions->push_head(uni);
97
98 ir_dereference_variable *varref = new(mem_ctx) ir_dereference_variable(uni);
99 *rvalue = new(mem_ctx) ir_dereference_array(varref, dra->array_index);
100
101 progress = true;
102 }
103
104 } /* anonymous namespace */
105
106 bool
107 lower_const_arrays_to_uniforms(exec_list *instructions)
108 {
109 lower_const_array_visitor v(instructions);
110 return v.run();
111 }