• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mtypes.h"
25 #include "main/macros.h"
26 #include "util/ralloc.h"
27 #include "uniform_initializer_utils.h"
28 #include <stdio.h>
29 
30 void
fill_storage_array_with_sentinels(gl_constant_value * storage,unsigned data_size,unsigned red_zone_size)31 fill_storage_array_with_sentinels(gl_constant_value *storage,
32 				  unsigned data_size,
33 				  unsigned red_zone_size)
34 {
35    for (unsigned i = 0; i < data_size; i++)
36       storage[i].u = 0xDEADBEEF;
37 
38    for (unsigned i = 0; i < red_zone_size; i++)
39       storage[data_size + i].u = 0xBADDC0DE;
40 }
41 
42 /**
43  * Verfiy that markers past the end of the real uniform are unmodified
44  */
45 static ::testing::AssertionResult
red_zone_is_intact(gl_constant_value * storage,unsigned data_size,unsigned red_zone_size)46 red_zone_is_intact(gl_constant_value *storage,
47 		   unsigned data_size,
48 		   unsigned red_zone_size)
49 {
50    for (unsigned i = 0; i < red_zone_size; i++) {
51       const unsigned idx = data_size + i;
52 
53       if (storage[idx].u != 0xBADDC0DE)
54 	 return ::testing::AssertionFailure()
55 	    << "storage[" << idx << "].u = "  << storage[idx].u
56 	    << ", exepected data values = " << data_size
57 	    << ", red-zone size = " << red_zone_size;
58    }
59 
60    return ::testing::AssertionSuccess();
61 }
62 
63 static const int values[] = {
64    2, 0, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
65 };
66 
67 /**
68  * Generate a single data element.
69  *
70  * This is by both \c generate_data and \c generate_array_data to create the
71  * data.
72  */
73 static void
generate_data_element(void * mem_ctx,const glsl_type * type,ir_constant * & val,unsigned data_index_base)74 generate_data_element(void *mem_ctx, const glsl_type *type,
75 		      ir_constant *&val, unsigned data_index_base)
76 {
77    /* Set the initial data values for the generated constant.
78     */
79    ir_constant_data data;
80    memset(&data, 0, sizeof(data));
81    for (unsigned i = 0; i < type->components(); i++) {
82       const unsigned idx = (i + data_index_base) % ARRAY_SIZE(values);
83       switch (type->base_type) {
84       case GLSL_TYPE_UINT:
85       case GLSL_TYPE_INT:
86       case GLSL_TYPE_SAMPLER:
87       case GLSL_TYPE_IMAGE:
88 	 data.i[i] = values[idx];
89 	 break;
90       case GLSL_TYPE_FLOAT:
91 	 data.f[i] = float(values[idx]);
92 	 break;
93       case GLSL_TYPE_BOOL:
94 	 data.b[i] = bool(values[idx]);
95 	 break;
96       case GLSL_TYPE_DOUBLE:
97 	 data.d[i] = double(values[idx]);
98 	 break;
99       case GLSL_TYPE_UINT64:
100 	 data.u64[i] = (uint64_t) values[idx];
101 	 break;
102       case GLSL_TYPE_INT64:
103 	 data.i64[i] = (int64_t) values[idx];
104 	 break;
105       case GLSL_TYPE_ATOMIC_UINT:
106       case GLSL_TYPE_STRUCT:
107       case GLSL_TYPE_ARRAY:
108       case GLSL_TYPE_VOID:
109       case GLSL_TYPE_ERROR:
110       case GLSL_TYPE_INTERFACE:
111       case GLSL_TYPE_SUBROUTINE:
112       case GLSL_TYPE_FUNCTION:
113       case GLSL_TYPE_FLOAT16:
114       case GLSL_TYPE_UINT16:
115       case GLSL_TYPE_INT16:
116 	 ASSERT_TRUE(false);
117 	 break;
118       }
119    }
120 
121    /* Generate and verify the constant.
122     */
123    val = new(mem_ctx) ir_constant(type, &data);
124 
125    for (unsigned i = 0; i < type->components(); i++) {
126       switch (type->base_type) {
127       case GLSL_TYPE_UINT:
128       case GLSL_TYPE_INT:
129       case GLSL_TYPE_SAMPLER:
130       case GLSL_TYPE_IMAGE:
131 	 ASSERT_EQ(data.i[i], val->value.i[i]);
132 	 break;
133       case GLSL_TYPE_FLOAT:
134 	 ASSERT_EQ(data.f[i], val->value.f[i]);
135 	 break;
136       case GLSL_TYPE_BOOL:
137 	 ASSERT_EQ(data.b[i], val->value.b[i]);
138 	 break;
139       case GLSL_TYPE_DOUBLE:
140 	 ASSERT_EQ(data.d[i], val->value.d[i]);
141 	 break;
142       case GLSL_TYPE_UINT64:
143 	 ASSERT_EQ(data.u64[i], val->value.u64[i]);
144 	 break;
145       case GLSL_TYPE_INT64:
146 	 ASSERT_EQ(data.i64[i], val->value.i64[i]);
147 	 break;
148       case GLSL_TYPE_ATOMIC_UINT:
149       case GLSL_TYPE_STRUCT:
150       case GLSL_TYPE_ARRAY:
151       case GLSL_TYPE_VOID:
152       case GLSL_TYPE_ERROR:
153       case GLSL_TYPE_INTERFACE:
154       case GLSL_TYPE_SUBROUTINE:
155       case GLSL_TYPE_FUNCTION:
156       case GLSL_TYPE_FLOAT16:
157       case GLSL_TYPE_UINT16:
158       case GLSL_TYPE_INT16:
159 	 ASSERT_TRUE(false);
160 	 break;
161       }
162    }
163 }
164 
165 void
generate_data(void * mem_ctx,enum glsl_base_type base_type,unsigned columns,unsigned rows,ir_constant * & val)166 generate_data(void *mem_ctx, enum glsl_base_type base_type,
167 	      unsigned columns, unsigned rows,
168 	      ir_constant *&val)
169 {
170    /* Determine what the type of the generated constant should be.
171     */
172    const glsl_type *const type =
173       glsl_type::get_instance(base_type, rows, columns);
174    ASSERT_FALSE(type->is_error());
175 
176    generate_data_element(mem_ctx, type, val, 0);
177 }
178 
179 void
generate_array_data(void * mem_ctx,enum glsl_base_type base_type,unsigned columns,unsigned rows,unsigned array_size,ir_constant * & val)180 generate_array_data(void *mem_ctx, enum glsl_base_type base_type,
181 		    unsigned columns, unsigned rows, unsigned array_size,
182 		    ir_constant *&val)
183 {
184    /* Determine what the type of the generated constant should be.
185     */
186    const glsl_type *const element_type =
187       glsl_type::get_instance(base_type, rows, columns);
188    ASSERT_FALSE(element_type->is_error());
189 
190    const glsl_type *const array_type =
191       glsl_type::get_array_instance(element_type, array_size);
192    ASSERT_FALSE(array_type->is_error());
193 
194    /* Set the initial data values for the generated constant.
195     */
196    exec_list values_for_array;
197    for (unsigned i = 0; i < array_size; i++) {
198       ir_constant *element;
199 
200       generate_data_element(mem_ctx, element_type, element, i);
201       values_for_array.push_tail(element);
202    }
203 
204    val = new(mem_ctx) ir_constant(array_type, &values_for_array);
205 }
206 
207 static uint64_t
uint64_storage(union gl_constant_value * storage)208 uint64_storage(union gl_constant_value *storage)
209 {
210    uint64_t val;
211    memcpy(&val, &storage->i, sizeof(uint64_t));
212    return val;
213 }
214 
215 static uint64_t
double_storage(union gl_constant_value * storage)216 double_storage(union gl_constant_value *storage)
217 {
218    double val;
219    memcpy(&val, &storage->i, sizeof(double));
220    return val;
221 }
222 
223 /**
224  * Verify that the data stored for the uniform matches the initializer
225  *
226  * \param storage              Backing storage for the uniform
227  * \param storage_array_size  Array size of the backing storage.  This must be
228  *                            less than or equal to the array size of the type
229  *                            of \c val.  If \c val is not an array, this must
230  *                            be zero.
231  * \param val                 Value of the initializer for the unifrom.
232  * \param red_zone
233  */
234 void
verify_data(gl_constant_value * storage,unsigned storage_array_size,ir_constant * val,unsigned red_zone_size,unsigned int boolean_true)235 verify_data(gl_constant_value *storage, unsigned storage_array_size,
236             ir_constant *val, unsigned red_zone_size,
237             unsigned int boolean_true)
238 {
239    if (val->type->is_array()) {
240       const glsl_type *const element_type = val->const_elements[0]->type;
241 
242       for (unsigned i = 0; i < storage_array_size; i++) {
243 	 verify_data(storage + (i * element_type->components()), 0,
244                      val->const_elements[i], 0, boolean_true);
245       }
246 
247       const unsigned components = element_type->components();
248 
249       if (red_zone_size > 0) {
250 	 EXPECT_TRUE(red_zone_is_intact(storage,
251 					storage_array_size * components,
252 					red_zone_size));
253       }
254    } else {
255       ASSERT_EQ(0u, storage_array_size);
256       for (unsigned i = 0; i < val->type->components(); i++) {
257 	 switch (val->type->base_type) {
258 	 case GLSL_TYPE_UINT:
259 	 case GLSL_TYPE_INT:
260 	 case GLSL_TYPE_SAMPLER:
261 	 case GLSL_TYPE_IMAGE:
262 	    EXPECT_EQ(val->value.i[i], storage[i].i);
263 	    break;
264 	 case GLSL_TYPE_FLOAT:
265 	    EXPECT_EQ(val->value.f[i], storage[i].f);
266 	    break;
267 	 case GLSL_TYPE_BOOL:
268 	    EXPECT_EQ(val->value.b[i] ? boolean_true : 0, storage[i].i);
269 	    break;
270 	 case GLSL_TYPE_DOUBLE:
271 	    EXPECT_EQ(val->value.d[i], double_storage(&storage[i*2]));
272 	    break;
273 	 case GLSL_TYPE_UINT64:
274             EXPECT_EQ(val->value.u64[i], uint64_storage(&storage[i*2]));
275 	    break;
276 	 case GLSL_TYPE_INT64:
277 	    EXPECT_EQ(val->value.i64[i], uint64_storage(&storage[i*2]));
278 	    break;
279          case GLSL_TYPE_ATOMIC_UINT:
280 	 case GLSL_TYPE_STRUCT:
281 	 case GLSL_TYPE_ARRAY:
282 	 case GLSL_TYPE_VOID:
283 	 case GLSL_TYPE_ERROR:
284 	 case GLSL_TYPE_INTERFACE:
285 	 case GLSL_TYPE_SUBROUTINE:
286          case GLSL_TYPE_FUNCTION:
287          case GLSL_TYPE_FLOAT16:
288          case GLSL_TYPE_UINT16:
289          case GLSL_TYPE_INT16:
290 	    ASSERT_TRUE(false);
291 	    break;
292 	 }
293       }
294 
295       if (red_zone_size > 0) {
296 	 EXPECT_TRUE(red_zone_is_intact(storage,
297 					val->type->components(),
298 					red_zone_size));
299       }
300    }
301 }
302