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