7643e1bbbf917390e0f91e48cd5021e46a29a3be
[mesa.git] / src / compiler / glsl / tests / varyings_test.cpp
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 "main/compiler.h"
25 #include "main/mtypes.h"
26 #include "main/macros.h"
27 #include "util/ralloc.h"
28 #include "ir.h"
29 #include "util/hash_table.h"
30
31 /**
32 * \file varyings_test.cpp
33 *
34 * Test various aspects of linking shader stage inputs and outputs.
35 */
36
37 namespace linker {
38 void
39 populate_consumer_input_sets(void *mem_ctx, exec_list *ir,
40 hash_table *consumer_inputs,
41 hash_table *consumer_interface_inputs,
42 ir_variable *consumer_inputs_with_locations[VARYING_SLOT_MAX]);
43
44 ir_variable *
45 get_matching_input(void *mem_ctx,
46 const ir_variable *output_var,
47 hash_table *consumer_inputs,
48 hash_table *consumer_interface_inputs,
49 ir_variable *consumer_inputs_with_locations[VARYING_SLOT_MAX]);
50 }
51
52 class link_varyings : public ::testing::Test {
53 public:
54 link_varyings();
55
56 virtual void SetUp();
57 virtual void TearDown();
58
59 char *interface_field_name(const glsl_type *iface, unsigned field = 0)
60 {
61 return ralloc_asprintf(mem_ctx,
62 "%s.%s",
63 iface->name,
64 iface->fields.structure[field].name);
65 }
66
67 void *mem_ctx;
68 exec_list ir;
69 hash_table *consumer_inputs;
70 hash_table *consumer_interface_inputs;
71
72 const glsl_type *simple_interface;
73 ir_variable *junk[VARYING_SLOT_TESS_MAX];
74 };
75
76 link_varyings::link_varyings()
77 {
78 static const glsl_struct_field f[] = {
79 glsl_struct_field(glsl_type::vec(4), "v")
80 };
81
82 this->simple_interface =
83 glsl_type::get_interface_instance(f,
84 ARRAY_SIZE(f),
85 GLSL_INTERFACE_PACKING_STD140,
86 "simple_interface");
87 }
88
89 void
90 link_varyings::SetUp()
91 {
92 this->mem_ctx = ralloc_context(NULL);
93 this->ir.make_empty();
94
95 this->consumer_inputs =
96 _mesa_hash_table_create(NULL, _mesa_key_hash_string,
97 _mesa_key_string_equal);
98
99 this->consumer_interface_inputs =
100 _mesa_hash_table_create(NULL, _mesa_key_hash_string,
101 _mesa_key_string_equal);
102 }
103
104 void
105 link_varyings::TearDown()
106 {
107 ralloc_free(this->mem_ctx);
108 this->mem_ctx = NULL;
109
110 _mesa_hash_table_destroy(this->consumer_inputs, NULL);
111 this->consumer_inputs = NULL;
112 _mesa_hash_table_destroy(this->consumer_interface_inputs, NULL);
113 this->consumer_interface_inputs = NULL;
114 }
115
116 TEST_F(link_varyings, single_simple_input)
117 {
118 ir_variable *const v =
119 new(mem_ctx) ir_variable(glsl_type::vec(4),
120 "a",
121 ir_var_shader_in);
122
123
124 ir.push_tail(v);
125
126 linker::populate_consumer_input_sets(mem_ctx,
127 &ir,
128 consumer_inputs,
129 consumer_interface_inputs,
130 junk);
131
132 hash_entry *entry = _mesa_hash_table_search(consumer_inputs, "a");
133 EXPECT_EQ((void *) v, entry->data);
134 EXPECT_EQ(1u, consumer_inputs->entries);
135 EXPECT_TRUE(consumer_interface_inputs->entries == 0);
136 }
137
138 TEST_F(link_varyings, gl_ClipDistance)
139 {
140 const glsl_type *const array_8_of_float =
141 glsl_type::get_array_instance(glsl_type::vec(1), 8);
142
143 ir_variable *const clipdistance =
144 new(mem_ctx) ir_variable(array_8_of_float,
145 "gl_ClipDistance",
146 ir_var_shader_in);
147
148 clipdistance->data.explicit_location = true;
149 clipdistance->data.location = VARYING_SLOT_CLIP_DIST0;
150 clipdistance->data.explicit_index = 0;
151
152 ir.push_tail(clipdistance);
153
154 linker::populate_consumer_input_sets(mem_ctx,
155 &ir,
156 consumer_inputs,
157 consumer_interface_inputs,
158 junk);
159
160 EXPECT_EQ(clipdistance, junk[VARYING_SLOT_CLIP_DIST0]);
161 EXPECT_TRUE(consumer_inputs->entries == 0);
162 EXPECT_TRUE(consumer_interface_inputs->entries == 0);
163 }
164
165 TEST_F(link_varyings, gl_CullDistance)
166 {
167 const glsl_type *const array_8_of_float =
168 glsl_type::get_array_instance(glsl_type::vec(1), 8);
169
170 ir_variable *const culldistance =
171 new(mem_ctx) ir_variable(array_8_of_float,
172 "gl_CullDistance",
173 ir_var_shader_in);
174
175 culldistance->data.explicit_location = true;
176 culldistance->data.location = VARYING_SLOT_CULL_DIST0;
177 culldistance->data.explicit_index = 0;
178
179 ir.push_tail(culldistance);
180
181 linker::populate_consumer_input_sets(mem_ctx,
182 &ir,
183 consumer_inputs,
184 consumer_interface_inputs,
185 junk);
186
187 EXPECT_EQ(culldistance, junk[VARYING_SLOT_CULL_DIST0]);
188 EXPECT_TRUE(consumer_inputs->entries == 0);
189 EXPECT_TRUE(consumer_interface_inputs->entries == 0);
190 }
191
192 TEST_F(link_varyings, single_interface_input)
193 {
194 ir_variable *const v =
195 new(mem_ctx) ir_variable(simple_interface->fields.structure[0].type,
196 simple_interface->fields.structure[0].name,
197 ir_var_shader_in);
198
199 v->init_interface_type(simple_interface);
200
201 ir.push_tail(v);
202
203 linker::populate_consumer_input_sets(mem_ctx,
204 &ir,
205 consumer_inputs,
206 consumer_interface_inputs,
207 junk);
208 char *const full_name = interface_field_name(simple_interface);
209
210 hash_entry *entry = _mesa_hash_table_search(consumer_interface_inputs,
211 full_name);
212 EXPECT_EQ((void *) v, entry->data);
213 EXPECT_EQ(1u, consumer_interface_inputs->entries);
214 EXPECT_TRUE(consumer_inputs->entries == 0);
215 }
216
217 TEST_F(link_varyings, one_interface_and_one_simple_input)
218 {
219 ir_variable *const v =
220 new(mem_ctx) ir_variable(glsl_type::vec(4),
221 "a",
222 ir_var_shader_in);
223
224
225 ir.push_tail(v);
226
227 ir_variable *const iface =
228 new(mem_ctx) ir_variable(simple_interface->fields.structure[0].type,
229 simple_interface->fields.structure[0].name,
230 ir_var_shader_in);
231
232 iface->init_interface_type(simple_interface);
233
234 ir.push_tail(iface);
235
236 linker::populate_consumer_input_sets(mem_ctx,
237 &ir,
238 consumer_inputs,
239 consumer_interface_inputs,
240 junk);
241
242 char *const iface_field_name = interface_field_name(simple_interface);
243
244 hash_entry *entry = _mesa_hash_table_search(consumer_interface_inputs,
245 iface_field_name);
246 EXPECT_EQ((void *) iface, entry->data);
247 EXPECT_EQ(1u, consumer_interface_inputs->entries);
248
249 entry = _mesa_hash_table_search(consumer_inputs, "a");
250 EXPECT_EQ((void *) v, entry->data);
251 EXPECT_EQ(1u, consumer_inputs->entries);
252 }
253
254 TEST_F(link_varyings, interface_field_doesnt_match_noninterface)
255 {
256 char *const iface_field_name = interface_field_name(simple_interface);
257
258 /* The input shader has a single input variable name "a.v"
259 */
260 ir_variable *const in_v =
261 new(mem_ctx) ir_variable(glsl_type::vec(4),
262 iface_field_name,
263 ir_var_shader_in);
264
265 ir.push_tail(in_v);
266
267 linker::populate_consumer_input_sets(mem_ctx,
268 &ir,
269 consumer_inputs,
270 consumer_interface_inputs,
271 junk);
272
273 /* Create an output variable, "v", that is part of an interface block named
274 * "a". They should not match.
275 */
276 ir_variable *const out_v =
277 new(mem_ctx) ir_variable(simple_interface->fields.structure[0].type,
278 simple_interface->fields.structure[0].name,
279 ir_var_shader_in);
280
281 out_v->init_interface_type(simple_interface);
282
283 ir_variable *const match =
284 linker::get_matching_input(mem_ctx,
285 out_v,
286 consumer_inputs,
287 consumer_interface_inputs,
288 junk);
289
290 EXPECT_EQ(NULL, match);
291 }
292
293 TEST_F(link_varyings, interface_field_doesnt_match_noninterface_vice_versa)
294 {
295 char *const iface_field_name = interface_field_name(simple_interface);
296
297 /* In input shader has a single variable, "v", that is part of an interface
298 * block named "a".
299 */
300 ir_variable *const in_v =
301 new(mem_ctx) ir_variable(simple_interface->fields.structure[0].type,
302 simple_interface->fields.structure[0].name,
303 ir_var_shader_in);
304
305 in_v->init_interface_type(simple_interface);
306
307 ir.push_tail(in_v);
308
309 linker::populate_consumer_input_sets(mem_ctx,
310 &ir,
311 consumer_inputs,
312 consumer_interface_inputs,
313 junk);
314
315 /* Create an output variable "a.v". They should not match.
316 */
317 ir_variable *const out_v =
318 new(mem_ctx) ir_variable(glsl_type::vec(4),
319 iface_field_name,
320 ir_var_shader_out);
321
322 ir_variable *const match =
323 linker::get_matching_input(mem_ctx,
324 out_v,
325 consumer_inputs,
326 consumer_interface_inputs,
327 junk);
328
329 EXPECT_EQ(NULL, match);
330 }