1 /*
2 * Copyright © 2013 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 #include <gtest/gtest.h>
24 #include "util/compiler.h"
25 #include "main/mtypes.h"
26 #include "main/macros.h"
27 #include "util/ralloc.h"
28 #include "ir.h"
29 #include "linker.h"
30
31 /**
32 * \file varyings_test.cpp
33 *
34 * Test various aspects of linking shader stage inputs and outputs.
35 */
36
37 class invalidate_locations : public ::testing::Test {
38 public:
39 virtual void SetUp();
40 virtual void TearDown();
41
42 void *mem_ctx;
43 exec_list ir;
44 };
45
46 void
SetUp()47 invalidate_locations::SetUp()
48 {
49 glsl_type_singleton_init_or_ref();
50
51 this->mem_ctx = ralloc_context(NULL);
52 this->ir.make_empty();
53 }
54
55 void
TearDown()56 invalidate_locations::TearDown()
57 {
58 ralloc_free(this->mem_ctx);
59 this->mem_ctx = NULL;
60
61 glsl_type_singleton_decref();
62 }
63
TEST_F(invalidate_locations,simple_vertex_in_generic)64 TEST_F(invalidate_locations, simple_vertex_in_generic)
65 {
66 ir_variable *const var =
67 new(mem_ctx) ir_variable(glsl_type::vec(4),
68 "a",
69 ir_var_shader_in);
70
71 EXPECT_FALSE(var->data.explicit_location);
72 EXPECT_EQ(-1, var->data.location);
73
74 var->data.location = VERT_ATTRIB_GENERIC0;
75 var->data.location_frac = 2;
76
77 ir.push_tail(var);
78
79 link_invalidate_variable_locations(&ir);
80
81 EXPECT_EQ(-1, var->data.location);
82 EXPECT_EQ(0u, var->data.location_frac);
83 EXPECT_FALSE(var->data.explicit_location);
84 EXPECT_TRUE(var->data.is_unmatched_generic_inout);
85 }
86
TEST_F(invalidate_locations,explicit_location_vertex_in_generic)87 TEST_F(invalidate_locations, explicit_location_vertex_in_generic)
88 {
89 ir_variable *const var =
90 new(mem_ctx) ir_variable(glsl_type::vec(4),
91 "a",
92 ir_var_shader_in);
93
94 EXPECT_FALSE(var->data.explicit_location);
95 EXPECT_EQ(-1, var->data.location);
96
97 var->data.location = VERT_ATTRIB_GENERIC0;
98 var->data.explicit_location = true;
99
100 ir.push_tail(var);
101
102 link_invalidate_variable_locations(&ir);
103
104 EXPECT_EQ(VERT_ATTRIB_GENERIC0, var->data.location);
105 EXPECT_EQ(0u, var->data.location_frac);
106 EXPECT_TRUE(var->data.explicit_location);
107 EXPECT_FALSE(var->data.is_unmatched_generic_inout);
108 }
109
TEST_F(invalidate_locations,explicit_location_frac_vertex_in_generic)110 TEST_F(invalidate_locations, explicit_location_frac_vertex_in_generic)
111 {
112 ir_variable *const var =
113 new(mem_ctx) ir_variable(glsl_type::vec(4),
114 "a",
115 ir_var_shader_in);
116
117 EXPECT_FALSE(var->data.explicit_location);
118 EXPECT_EQ(-1, var->data.location);
119
120 var->data.location = VERT_ATTRIB_GENERIC0;
121 var->data.location_frac = 2;
122 var->data.explicit_location = true;
123
124 ir.push_tail(var);
125
126 link_invalidate_variable_locations(&ir);
127
128 EXPECT_EQ(VERT_ATTRIB_GENERIC0, var->data.location);
129 EXPECT_EQ(2u, var->data.location_frac);
130 EXPECT_TRUE(var->data.explicit_location);
131 EXPECT_FALSE(var->data.is_unmatched_generic_inout);
132 }
133
TEST_F(invalidate_locations,vertex_in_builtin)134 TEST_F(invalidate_locations, vertex_in_builtin)
135 {
136 ir_variable *const var =
137 new(mem_ctx) ir_variable(glsl_type::vec(4),
138 "gl_Vertex",
139 ir_var_shader_in);
140
141 EXPECT_FALSE(var->data.explicit_location);
142 EXPECT_EQ(-1, var->data.location);
143
144 var->data.location = VERT_ATTRIB_POS;
145 var->data.explicit_location = true;
146
147 ir.push_tail(var);
148
149 link_invalidate_variable_locations(&ir);
150
151 EXPECT_EQ(VERT_ATTRIB_POS, var->data.location);
152 EXPECT_EQ(0u, var->data.location_frac);
153 EXPECT_TRUE(var->data.explicit_location);
154 EXPECT_FALSE(var->data.is_unmatched_generic_inout);
155 }
156
TEST_F(invalidate_locations,simple_vertex_out_generic)157 TEST_F(invalidate_locations, simple_vertex_out_generic)
158 {
159 ir_variable *const var =
160 new(mem_ctx) ir_variable(glsl_type::vec(4),
161 "a",
162 ir_var_shader_out);
163
164 EXPECT_FALSE(var->data.explicit_location);
165 EXPECT_EQ(-1, var->data.location);
166
167 var->data.location = VARYING_SLOT_VAR0;
168
169 ir.push_tail(var);
170
171 link_invalidate_variable_locations(&ir);
172
173 EXPECT_EQ(-1, var->data.location);
174 EXPECT_EQ(0u, var->data.location_frac);
175 EXPECT_FALSE(var->data.explicit_location);
176 EXPECT_TRUE(var->data.is_unmatched_generic_inout);
177 }
178
TEST_F(invalidate_locations,vertex_out_builtin)179 TEST_F(invalidate_locations, vertex_out_builtin)
180 {
181 ir_variable *const var =
182 new(mem_ctx) ir_variable(glsl_type::vec(4),
183 "gl_FrontColor",
184 ir_var_shader_out);
185
186 EXPECT_FALSE(var->data.explicit_location);
187 EXPECT_EQ(-1, var->data.location);
188
189 var->data.location = VARYING_SLOT_COL0;
190 var->data.explicit_location = true;
191
192 ir.push_tail(var);
193
194 link_invalidate_variable_locations(&ir);
195
196 EXPECT_EQ(VARYING_SLOT_COL0, var->data.location);
197 EXPECT_EQ(0u, var->data.location_frac);
198 EXPECT_TRUE(var->data.explicit_location);
199 EXPECT_FALSE(var->data.is_unmatched_generic_inout);
200 }
201