glsl: Lower UBO and SSBO access in glsl linker
[mesa.git] / src / mesa / drivers / dri / i965 / test_vec4_copy_propagation.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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <gtest/gtest.h>
25 #include "brw_vec4.h"
26 #include "brw_vs.h"
27
28 using namespace brw;
29
30 int ret = 0;
31
32 class copy_propagation_test : public ::testing::Test {
33 virtual void SetUp();
34
35 public:
36 struct brw_compiler *compiler;
37 struct brw_device_info *devinfo;
38 struct gl_context *ctx;
39 struct gl_shader_program *shader_prog;
40 struct brw_vertex_program *vp;
41 vec4_visitor *v;
42 };
43
44 class copy_propagation_vec4_visitor : public vec4_visitor
45 {
46 public:
47 copy_propagation_vec4_visitor(struct brw_compiler *compiler,
48 nir_shader *shader)
49 : vec4_visitor(compiler, NULL, NULL, NULL, shader, NULL,
50 false /* no_spills */, -1)
51 {
52 }
53
54 protected:
55 virtual dst_reg *make_reg_for_system_value(int location,
56 const glsl_type *type)
57 {
58 unreachable("Not reached");
59 }
60
61 virtual void setup_payload()
62 {
63 unreachable("Not reached");
64 }
65
66 virtual void emit_prolog()
67 {
68 unreachable("Not reached");
69 }
70
71 virtual void emit_thread_end()
72 {
73 unreachable("Not reached");
74 }
75
76 virtual void emit_urb_write_header(int mrf)
77 {
78 unreachable("Not reached");
79 }
80
81 virtual vec4_instruction *emit_urb_write_opcode(bool complete)
82 {
83 unreachable("Not reached");
84 }
85 };
86
87
88 void copy_propagation_test::SetUp()
89 {
90 ctx = (struct gl_context *)calloc(1, sizeof(*ctx));
91 compiler = (struct brw_compiler *)calloc(1, sizeof(*compiler));
92 devinfo = (struct brw_device_info *)calloc(1, sizeof(*devinfo));
93 compiler->devinfo = devinfo;
94
95 vp = ralloc(NULL, struct brw_vertex_program);
96
97 nir_shader *shader = nir_shader_create(NULL, MESA_SHADER_VERTEX, NULL);
98
99 v = new copy_propagation_vec4_visitor(compiler, shader);
100
101 _mesa_init_gl_program(&vp->program.Base, GL_VERTEX_SHADER, 0);
102
103 devinfo->gen = 4;
104 }
105
106 static void
107 copy_propagation(vec4_visitor *v)
108 {
109 bool print = false;
110
111 if (print) {
112 fprintf(stderr, "instructions before:\n");
113 v->dump_instructions();
114 }
115
116 v->calculate_cfg();
117 v->opt_copy_propagation();
118
119 if (print) {
120 fprintf(stderr, "instructions after:\n");
121 v->dump_instructions();
122 }
123 }
124
125 TEST_F(copy_propagation_test, test_swizzle_swizzle)
126 {
127 dst_reg a = dst_reg(v, glsl_type::vec4_type);
128 dst_reg b = dst_reg(v, glsl_type::vec4_type);
129 dst_reg c = dst_reg(v, glsl_type::vec4_type);
130
131 v->emit(v->ADD(a, src_reg(a), src_reg(a)));
132
133 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_Y,
134 SWIZZLE_Z,
135 SWIZZLE_W,
136 SWIZZLE_X))));
137
138 vec4_instruction *test_mov =
139 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_Y,
140 SWIZZLE_Z,
141 SWIZZLE_W,
142 SWIZZLE_X)));
143 v->emit(test_mov);
144
145 copy_propagation(v);
146
147 EXPECT_EQ(test_mov->src[0].reg, a.reg);
148 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_Z,
149 SWIZZLE_W,
150 SWIZZLE_X,
151 SWIZZLE_Y));
152 }
153
154 TEST_F(copy_propagation_test, test_swizzle_writemask)
155 {
156 dst_reg a = dst_reg(v, glsl_type::vec4_type);
157 dst_reg b = dst_reg(v, glsl_type::vec4_type);
158 dst_reg c = dst_reg(v, glsl_type::vec4_type);
159
160 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_X,
161 SWIZZLE_Y,
162 SWIZZLE_X,
163 SWIZZLE_Z))));
164
165 v->emit(v->MOV(writemask(a, WRITEMASK_XYZ), src_reg(1.0f)));
166
167 vec4_instruction *test_mov =
168 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_W,
169 SWIZZLE_W,
170 SWIZZLE_W,
171 SWIZZLE_W)));
172 v->emit(test_mov);
173
174 copy_propagation(v);
175
176 /* should not copy propagate */
177 EXPECT_EQ(test_mov->src[0].reg, b.reg);
178 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_W,
179 SWIZZLE_W,
180 SWIZZLE_W,
181 SWIZZLE_W));
182 }