1 /*
2  * Copyright © 2012 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 "uniform_initializer_utils.h"
29 
30 namespace linker {
31 extern void
32 copy_constant_to_storage(union gl_constant_value *storage,
33 			 const ir_constant *val,
34 			 const enum glsl_base_type base_type,
35                          const unsigned int elements,
36                          unsigned int boolean_true);
37 }
38 
39 class copy_constant_to_storage : public ::testing::Test {
40 public:
41    void int_test(unsigned rows);
42    void uint_test(unsigned rows);
43    void bool_test(unsigned rows);
44    void sampler_test();
45    void float_test(unsigned columns, unsigned rows);
46 
47    virtual void SetUp();
48    virtual void TearDown();
49 
50    gl_constant_value storage[17];
51    void *mem_ctx;
52 };
53 
54 void
SetUp()55 copy_constant_to_storage::SetUp()
56 {
57    this->mem_ctx = ralloc_context(NULL);
58 }
59 
60 void
TearDown()61 copy_constant_to_storage::TearDown()
62 {
63    ralloc_free(this->mem_ctx);
64    this->mem_ctx = NULL;
65 }
66 
67 void
int_test(unsigned rows)68 copy_constant_to_storage::int_test(unsigned rows)
69 {
70    ir_constant *val;
71    generate_data(mem_ctx, GLSL_TYPE_INT, 1, rows, val);
72 
73    const unsigned red_zone_size = ARRAY_SIZE(storage) - val->type->components();
74    fill_storage_array_with_sentinels(storage,
75 				     val->type->components(),
76 				     red_zone_size);
77 
78    linker::copy_constant_to_storage(storage,
79 				    val,
80 				    val->type->base_type,
81                                     val->type->components(),
82                                     0xF00F);
83 
84    verify_data(storage, 0, val, red_zone_size, 0xF00F);
85 }
86 
87 void
uint_test(unsigned rows)88 copy_constant_to_storage::uint_test(unsigned rows)
89 {
90    ir_constant *val;
91    generate_data(mem_ctx, GLSL_TYPE_UINT, 1, rows, val);
92 
93    const unsigned red_zone_size = ARRAY_SIZE(storage) - val->type->components();
94    fill_storage_array_with_sentinels(storage,
95 				     val->type->components(),
96 				     red_zone_size);
97 
98    linker::copy_constant_to_storage(storage,
99 				    val,
100 				    val->type->base_type,
101                                     val->type->components(),
102                                     0xF00F);
103 
104    verify_data(storage, 0, val, red_zone_size, 0xF00F);
105 }
106 
107 void
float_test(unsigned columns,unsigned rows)108 copy_constant_to_storage::float_test(unsigned columns, unsigned rows)
109 {
110    ir_constant *val;
111    generate_data(mem_ctx, GLSL_TYPE_FLOAT, columns, rows, val);
112 
113    const unsigned red_zone_size = ARRAY_SIZE(storage) - val->type->components();
114    fill_storage_array_with_sentinels(storage,
115 				     val->type->components(),
116 				     red_zone_size);
117 
118    linker::copy_constant_to_storage(storage,
119 				    val,
120 				    val->type->base_type,
121                                     val->type->components(),
122                                     0xF00F);
123 
124    verify_data(storage, 0, val, red_zone_size, 0xF00F);
125 }
126 
127 void
bool_test(unsigned rows)128 copy_constant_to_storage::bool_test(unsigned rows)
129 {
130    ir_constant *val;
131    generate_data(mem_ctx, GLSL_TYPE_BOOL, 1, rows, val);
132 
133    const unsigned red_zone_size = ARRAY_SIZE(storage) - val->type->components();
134    fill_storage_array_with_sentinels(storage,
135 				     val->type->components(),
136 				     red_zone_size);
137 
138    linker::copy_constant_to_storage(storage,
139 				    val,
140 				    val->type->base_type,
141                                     val->type->components(),
142                                     0xF00F);
143 
144    verify_data(storage, 0, val, red_zone_size, 0xF00F);
145 }
146 
147 /**
148  * The only difference between this test and int_test is that the base type
149  * passed to \c linker::copy_constant_to_storage is hard-coded to \c
150  * GLSL_TYPE_SAMPLER instead of using the base type from the constant.
151  */
152 void
sampler_test(void)153 copy_constant_to_storage::sampler_test(void)
154 {
155    ir_constant *val;
156    generate_data(mem_ctx, GLSL_TYPE_INT, 1, 1, val);
157 
158    const unsigned red_zone_size = ARRAY_SIZE(storage) - val->type->components();
159    fill_storage_array_with_sentinels(storage,
160 				     val->type->components(),
161 				     red_zone_size);
162 
163    linker::copy_constant_to_storage(storage,
164 				    val,
165 				    GLSL_TYPE_SAMPLER,
166                                     val->type->components(),
167                                     0xF00F);
168 
169    verify_data(storage, 0, val, red_zone_size, 0xF00F);
170 }
171 
TEST_F(copy_constant_to_storage,bool_uniform)172 TEST_F(copy_constant_to_storage, bool_uniform)
173 {
174    bool_test(1);
175 }
176 
TEST_F(copy_constant_to_storage,bvec2_uniform)177 TEST_F(copy_constant_to_storage, bvec2_uniform)
178 {
179    bool_test(2);
180 }
181 
TEST_F(copy_constant_to_storage,bvec3_uniform)182 TEST_F(copy_constant_to_storage, bvec3_uniform)
183 {
184    bool_test(3);
185 }
186 
TEST_F(copy_constant_to_storage,bvec4_uniform)187 TEST_F(copy_constant_to_storage, bvec4_uniform)
188 {
189    bool_test(4);
190 }
191 
TEST_F(copy_constant_to_storage,int_uniform)192 TEST_F(copy_constant_to_storage, int_uniform)
193 {
194    int_test(1);
195 }
196 
TEST_F(copy_constant_to_storage,ivec2_uniform)197 TEST_F(copy_constant_to_storage, ivec2_uniform)
198 {
199    int_test(2);
200 }
201 
TEST_F(copy_constant_to_storage,ivec3_uniform)202 TEST_F(copy_constant_to_storage, ivec3_uniform)
203 {
204    int_test(3);
205 }
206 
TEST_F(copy_constant_to_storage,ivec4_uniform)207 TEST_F(copy_constant_to_storage, ivec4_uniform)
208 {
209    int_test(4);
210 }
211 
TEST_F(copy_constant_to_storage,uint_uniform)212 TEST_F(copy_constant_to_storage, uint_uniform)
213 {
214    uint_test(1);
215 }
216 
TEST_F(copy_constant_to_storage,uvec2_uniform)217 TEST_F(copy_constant_to_storage, uvec2_uniform)
218 {
219    uint_test(2);
220 }
221 
TEST_F(copy_constant_to_storage,uvec3_uniform)222 TEST_F(copy_constant_to_storage, uvec3_uniform)
223 {
224    uint_test(3);
225 }
226 
TEST_F(copy_constant_to_storage,uvec4_uniform)227 TEST_F(copy_constant_to_storage, uvec4_uniform)
228 {
229    uint_test(4);
230 }
231 
TEST_F(copy_constant_to_storage,float_uniform)232 TEST_F(copy_constant_to_storage, float_uniform)
233 {
234    float_test(1, 1);
235 }
236 
TEST_F(copy_constant_to_storage,vec2_uniform)237 TEST_F(copy_constant_to_storage, vec2_uniform)
238 {
239    float_test(1, 2);
240 }
241 
TEST_F(copy_constant_to_storage,vec3_uniform)242 TEST_F(copy_constant_to_storage, vec3_uniform)
243 {
244    float_test(1, 3);
245 }
246 
TEST_F(copy_constant_to_storage,vec4_uniform)247 TEST_F(copy_constant_to_storage, vec4_uniform)
248 {
249    float_test(1, 4);
250 }
251 
TEST_F(copy_constant_to_storage,mat2x2_uniform)252 TEST_F(copy_constant_to_storage, mat2x2_uniform)
253 {
254    float_test(2, 2);
255 }
256 
TEST_F(copy_constant_to_storage,mat2x3_uniform)257 TEST_F(copy_constant_to_storage, mat2x3_uniform)
258 {
259    float_test(2, 3);
260 }
261 
TEST_F(copy_constant_to_storage,mat2x4_uniform)262 TEST_F(copy_constant_to_storage, mat2x4_uniform)
263 {
264    float_test(2, 4);
265 }
266 
TEST_F(copy_constant_to_storage,mat3x2_uniform)267 TEST_F(copy_constant_to_storage, mat3x2_uniform)
268 {
269    float_test(3, 2);
270 }
271 
TEST_F(copy_constant_to_storage,mat3x3_uniform)272 TEST_F(copy_constant_to_storage, mat3x3_uniform)
273 {
274    float_test(3, 3);
275 }
276 
TEST_F(copy_constant_to_storage,mat3x4_uniform)277 TEST_F(copy_constant_to_storage, mat3x4_uniform)
278 {
279    float_test(3, 4);
280 }
281 
TEST_F(copy_constant_to_storage,mat4x2_uniform)282 TEST_F(copy_constant_to_storage, mat4x2_uniform)
283 {
284    float_test(4, 2);
285 }
286 
TEST_F(copy_constant_to_storage,mat4x3_uniform)287 TEST_F(copy_constant_to_storage, mat4x3_uniform)
288 {
289    float_test(4, 3);
290 }
291 
TEST_F(copy_constant_to_storage,mat4x4_uniform)292 TEST_F(copy_constant_to_storage, mat4x4_uniform)
293 {
294    float_test(4, 4);
295 }
296 
TEST_F(copy_constant_to_storage,sampler_uniform)297 TEST_F(copy_constant_to_storage, sampler_uniform)
298 {
299    sampler_test();
300 }
301