ec64be18cb36ddabffa5ee96a0fd76d1272c4729
[mesa.git] / src / compiler / glsl / tests / uniform_initializer_utils.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 "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
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
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
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_ATOMIC_UINT:
100 case GLSL_TYPE_STRUCT:
101 case GLSL_TYPE_ARRAY:
102 case GLSL_TYPE_VOID:
103 case GLSL_TYPE_ERROR:
104 case GLSL_TYPE_INTERFACE:
105 case GLSL_TYPE_SUBROUTINE:
106 case GLSL_TYPE_FUNCTION:
107 ASSERT_TRUE(false);
108 break;
109 }
110 }
111
112 /* Generate and verify the constant.
113 */
114 val = new(mem_ctx) ir_constant(type, &data);
115
116 for (unsigned i = 0; i < type->components(); i++) {
117 switch (type->base_type) {
118 case GLSL_TYPE_UINT:
119 case GLSL_TYPE_INT:
120 case GLSL_TYPE_SAMPLER:
121 case GLSL_TYPE_IMAGE:
122 ASSERT_EQ(data.i[i], val->value.i[i]);
123 break;
124 case GLSL_TYPE_FLOAT:
125 ASSERT_EQ(data.f[i], val->value.f[i]);
126 break;
127 case GLSL_TYPE_BOOL:
128 ASSERT_EQ(data.b[i], val->value.b[i]);
129 break;
130 case GLSL_TYPE_DOUBLE:
131 ASSERT_EQ(data.d[i], val->value.d[i]);
132 break;
133 case GLSL_TYPE_ATOMIC_UINT:
134 case GLSL_TYPE_STRUCT:
135 case GLSL_TYPE_ARRAY:
136 case GLSL_TYPE_VOID:
137 case GLSL_TYPE_ERROR:
138 case GLSL_TYPE_INTERFACE:
139 case GLSL_TYPE_SUBROUTINE:
140 case GLSL_TYPE_FUNCTION:
141 ASSERT_TRUE(false);
142 break;
143 }
144 }
145 }
146
147 void
148 generate_data(void *mem_ctx, enum glsl_base_type base_type,
149 unsigned columns, unsigned rows,
150 ir_constant *&val)
151 {
152 /* Determine what the type of the generated constant should be.
153 */
154 const glsl_type *const type =
155 glsl_type::get_instance(base_type, rows, columns);
156 ASSERT_FALSE(type->is_error());
157
158 generate_data_element(mem_ctx, type, val, 0);
159 }
160
161 void
162 generate_array_data(void *mem_ctx, enum glsl_base_type base_type,
163 unsigned columns, unsigned rows, unsigned array_size,
164 ir_constant *&val)
165 {
166 /* Determine what the type of the generated constant should be.
167 */
168 const glsl_type *const element_type =
169 glsl_type::get_instance(base_type, rows, columns);
170 ASSERT_FALSE(element_type->is_error());
171
172 const glsl_type *const array_type =
173 glsl_type::get_array_instance(element_type, array_size);
174 ASSERT_FALSE(array_type->is_error());
175
176 /* Set the initial data values for the generated constant.
177 */
178 exec_list values_for_array;
179 for (unsigned i = 0; i < array_size; i++) {
180 ir_constant *element;
181
182 generate_data_element(mem_ctx, element_type, element, i);
183 values_for_array.push_tail(element);
184 }
185
186 val = new(mem_ctx) ir_constant(array_type, &values_for_array);
187 }
188
189 /**
190 * Verify that the data stored for the uniform matches the initializer
191 *
192 * \param storage Backing storage for the uniform
193 * \param storage_array_size Array size of the backing storage. This must be
194 * less than or equal to the array size of the type
195 * of \c val. If \c val is not an array, this must
196 * be zero.
197 * \param val Value of the initializer for the unifrom.
198 * \param red_zone
199 */
200 void
201 verify_data(gl_constant_value *storage, unsigned storage_array_size,
202 ir_constant *val, unsigned red_zone_size,
203 unsigned int boolean_true)
204 {
205 if (val->type->base_type == GLSL_TYPE_ARRAY) {
206 const glsl_type *const element_type = val->array_elements[0]->type;
207
208 for (unsigned i = 0; i < storage_array_size; i++) {
209 verify_data(storage + (i * element_type->components()), 0,
210 val->array_elements[i], 0, boolean_true);
211 }
212
213 const unsigned components = element_type->components();
214
215 if (red_zone_size > 0) {
216 EXPECT_TRUE(red_zone_is_intact(storage,
217 storage_array_size * components,
218 red_zone_size));
219 }
220 } else {
221 ASSERT_EQ(0u, storage_array_size);
222 for (unsigned i = 0; i < val->type->components(); i++) {
223 switch (val->type->base_type) {
224 case GLSL_TYPE_UINT:
225 case GLSL_TYPE_INT:
226 case GLSL_TYPE_SAMPLER:
227 case GLSL_TYPE_IMAGE:
228 EXPECT_EQ(val->value.i[i], storage[i].i);
229 break;
230 case GLSL_TYPE_FLOAT:
231 EXPECT_EQ(val->value.f[i], storage[i].f);
232 break;
233 case GLSL_TYPE_BOOL:
234 EXPECT_EQ(val->value.b[i] ? boolean_true : 0, storage[i].i);
235 break;
236 case GLSL_TYPE_DOUBLE:
237 EXPECT_EQ(val->value.d[i], *(double *)&storage[i*2].i);
238 break;
239 case GLSL_TYPE_ATOMIC_UINT:
240 case GLSL_TYPE_STRUCT:
241 case GLSL_TYPE_ARRAY:
242 case GLSL_TYPE_VOID:
243 case GLSL_TYPE_ERROR:
244 case GLSL_TYPE_INTERFACE:
245 case GLSL_TYPE_SUBROUTINE:
246 case GLSL_TYPE_FUNCTION:
247 ASSERT_TRUE(false);
248 break;
249 }
250 }
251
252 if (red_zone_size > 0) {
253 EXPECT_TRUE(red_zone_is_intact(storage,
254 val->type->components(),
255 red_zone_size));
256 }
257 }
258 }