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 "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    this->mem_ctx = ralloc_context(NULL);
50    this->ir.make_empty();
51 }
52 
53 void
TearDown()54 invalidate_locations::TearDown()
55 {
56    ralloc_free(this->mem_ctx);
57    this->mem_ctx = NULL;
58 }
59 
TEST_F(invalidate_locations,simple_vertex_in_generic)60 TEST_F(invalidate_locations, simple_vertex_in_generic)
61 {
62    ir_variable *const var =
63       new(mem_ctx) ir_variable(glsl_type::vec(4),
64                                "a",
65                                ir_var_shader_in);
66 
67    EXPECT_FALSE(var->data.explicit_location);
68    EXPECT_EQ(-1, var->data.location);
69 
70    var->data.location = VERT_ATTRIB_GENERIC0;
71    var->data.location_frac = 2;
72 
73    ir.push_tail(var);
74 
75    link_invalidate_variable_locations(&ir);
76 
77    EXPECT_EQ(-1, var->data.location);
78    EXPECT_EQ(0u, var->data.location_frac);
79    EXPECT_FALSE(var->data.explicit_location);
80    EXPECT_TRUE(var->data.is_unmatched_generic_inout);
81 }
82 
TEST_F(invalidate_locations,explicit_location_vertex_in_generic)83 TEST_F(invalidate_locations, explicit_location_vertex_in_generic)
84 {
85    ir_variable *const var =
86       new(mem_ctx) ir_variable(glsl_type::vec(4),
87                                "a",
88                                ir_var_shader_in);
89 
90    EXPECT_FALSE(var->data.explicit_location);
91    EXPECT_EQ(-1, var->data.location);
92 
93    var->data.location = VERT_ATTRIB_GENERIC0;
94    var->data.explicit_location = true;
95 
96    ir.push_tail(var);
97 
98    link_invalidate_variable_locations(&ir);
99 
100    EXPECT_EQ(VERT_ATTRIB_GENERIC0, var->data.location);
101    EXPECT_EQ(0u, var->data.location_frac);
102    EXPECT_TRUE(var->data.explicit_location);
103    EXPECT_FALSE(var->data.is_unmatched_generic_inout);
104 }
105 
TEST_F(invalidate_locations,explicit_location_frac_vertex_in_generic)106 TEST_F(invalidate_locations, explicit_location_frac_vertex_in_generic)
107 {
108    ir_variable *const var =
109       new(mem_ctx) ir_variable(glsl_type::vec(4),
110                                "a",
111                                ir_var_shader_in);
112 
113    EXPECT_FALSE(var->data.explicit_location);
114    EXPECT_EQ(-1, var->data.location);
115 
116    var->data.location = VERT_ATTRIB_GENERIC0;
117    var->data.location_frac = 2;
118    var->data.explicit_location = true;
119 
120    ir.push_tail(var);
121 
122    link_invalidate_variable_locations(&ir);
123 
124    EXPECT_EQ(VERT_ATTRIB_GENERIC0, var->data.location);
125    EXPECT_EQ(2u, var->data.location_frac);
126    EXPECT_TRUE(var->data.explicit_location);
127    EXPECT_FALSE(var->data.is_unmatched_generic_inout);
128 }
129 
TEST_F(invalidate_locations,vertex_in_builtin)130 TEST_F(invalidate_locations, vertex_in_builtin)
131 {
132    ir_variable *const var =
133       new(mem_ctx) ir_variable(glsl_type::vec(4),
134                                "gl_Vertex",
135                                ir_var_shader_in);
136 
137    EXPECT_FALSE(var->data.explicit_location);
138    EXPECT_EQ(-1, var->data.location);
139 
140    var->data.location = VERT_ATTRIB_POS;
141    var->data.explicit_location = true;
142 
143    ir.push_tail(var);
144 
145    link_invalidate_variable_locations(&ir);
146 
147    EXPECT_EQ(VERT_ATTRIB_POS, var->data.location);
148    EXPECT_EQ(0u, var->data.location_frac);
149    EXPECT_TRUE(var->data.explicit_location);
150    EXPECT_FALSE(var->data.is_unmatched_generic_inout);
151 }
152 
TEST_F(invalidate_locations,simple_vertex_out_generic)153 TEST_F(invalidate_locations, simple_vertex_out_generic)
154 {
155    ir_variable *const var =
156       new(mem_ctx) ir_variable(glsl_type::vec(4),
157                                "a",
158                                ir_var_shader_out);
159 
160    EXPECT_FALSE(var->data.explicit_location);
161    EXPECT_EQ(-1, var->data.location);
162 
163    var->data.location = VARYING_SLOT_VAR0;
164 
165    ir.push_tail(var);
166 
167    link_invalidate_variable_locations(&ir);
168 
169    EXPECT_EQ(-1, var->data.location);
170    EXPECT_EQ(0u, var->data.location_frac);
171    EXPECT_FALSE(var->data.explicit_location);
172    EXPECT_TRUE(var->data.is_unmatched_generic_inout);
173 }
174 
TEST_F(invalidate_locations,vertex_out_builtin)175 TEST_F(invalidate_locations, vertex_out_builtin)
176 {
177    ir_variable *const var =
178       new(mem_ctx) ir_variable(glsl_type::vec(4),
179                                "gl_FrontColor",
180                                ir_var_shader_out);
181 
182    EXPECT_FALSE(var->data.explicit_location);
183    EXPECT_EQ(-1, var->data.location);
184 
185    var->data.location = VARYING_SLOT_COL0;
186    var->data.explicit_location = true;
187 
188    ir.push_tail(var);
189 
190    link_invalidate_variable_locations(&ir);
191 
192    EXPECT_EQ(VARYING_SLOT_COL0, var->data.location);
193    EXPECT_EQ(0u, var->data.location_frac);
194    EXPECT_TRUE(var->data.explicit_location);
195    EXPECT_FALSE(var->data.is_unmatched_generic_inout);
196 }
197