2 * Copyright © 2012 Intel Corporation
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:
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
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.
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"
31 fill_storage_array_with_sentinels(gl_constant_value
*storage
,
33 unsigned red_zone_size
)
35 for (unsigned i
= 0; i
< data_size
; i
++)
36 storage
[i
].u
= 0xDEADBEEF;
38 for (unsigned i
= 0; i
< red_zone_size
; i
++)
39 storage
[data_size
+ i
].u
= 0xBADDC0DE;
43 * Verfiy that markers past the end of the real uniform are unmodified
45 static ::testing::AssertionResult
46 red_zone_is_intact(gl_constant_value
*storage
,
48 unsigned red_zone_size
)
50 for (unsigned i
= 0; i
< red_zone_size
; i
++) {
51 const unsigned idx
= data_size
+ i
;
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
;
60 return ::testing::AssertionSuccess();
63 static const int values
[] = {
64 2, 0, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
68 * Generate a single data element.
70 * This is by both \c generate_data and \c generate_array_data to create the
74 generate_data_element(void *mem_ctx
, const glsl_type
*type
,
75 ir_constant
*&val
, unsigned data_index_base
)
77 /* Set the initial data values for the generated constant.
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
) {
86 case GLSL_TYPE_SAMPLER
:
88 data
.i
[i
] = values
[idx
];
91 data
.f
[i
] = float(values
[idx
]);
94 data
.b
[i
] = bool(values
[idx
]);
96 case GLSL_TYPE_DOUBLE
:
97 data
.d
[i
] = double(values
[idx
]);
99 case GLSL_TYPE_ATOMIC_UINT
:
100 case GLSL_TYPE_STRUCT
:
101 case GLSL_TYPE_ARRAY
:
103 case GLSL_TYPE_ERROR
:
104 case GLSL_TYPE_INTERFACE
:
105 case GLSL_TYPE_SUBROUTINE
:
111 /* Generate and verify the constant.
113 val
= new(mem_ctx
) ir_constant(type
, &data
);
115 for (unsigned i
= 0; i
< type
->components(); i
++) {
116 switch (type
->base_type
) {
119 case GLSL_TYPE_SAMPLER
:
120 case GLSL_TYPE_IMAGE
:
121 ASSERT_EQ(data
.i
[i
], val
->value
.i
[i
]);
123 case GLSL_TYPE_FLOAT
:
124 ASSERT_EQ(data
.f
[i
], val
->value
.f
[i
]);
127 ASSERT_EQ(data
.b
[i
], val
->value
.b
[i
]);
129 case GLSL_TYPE_DOUBLE
:
130 ASSERT_EQ(data
.d
[i
], val
->value
.d
[i
]);
132 case GLSL_TYPE_ATOMIC_UINT
:
133 case GLSL_TYPE_STRUCT
:
134 case GLSL_TYPE_ARRAY
:
136 case GLSL_TYPE_ERROR
:
137 case GLSL_TYPE_INTERFACE
:
138 case GLSL_TYPE_SUBROUTINE
:
146 generate_data(void *mem_ctx
, enum glsl_base_type base_type
,
147 unsigned columns
, unsigned rows
,
150 /* Determine what the type of the generated constant should be.
152 const glsl_type
*const type
=
153 glsl_type::get_instance(base_type
, rows
, columns
);
154 ASSERT_FALSE(type
->is_error());
156 generate_data_element(mem_ctx
, type
, val
, 0);
160 generate_array_data(void *mem_ctx
, enum glsl_base_type base_type
,
161 unsigned columns
, unsigned rows
, unsigned array_size
,
164 /* Determine what the type of the generated constant should be.
166 const glsl_type
*const element_type
=
167 glsl_type::get_instance(base_type
, rows
, columns
);
168 ASSERT_FALSE(element_type
->is_error());
170 const glsl_type
*const array_type
=
171 glsl_type::get_array_instance(element_type
, array_size
);
172 ASSERT_FALSE(array_type
->is_error());
174 /* Set the initial data values for the generated constant.
176 exec_list values_for_array
;
177 for (unsigned i
= 0; i
< array_size
; i
++) {
178 ir_constant
*element
;
180 generate_data_element(mem_ctx
, element_type
, element
, i
);
181 values_for_array
.push_tail(element
);
184 val
= new(mem_ctx
) ir_constant(array_type
, &values_for_array
);
188 * Verify that the data stored for the uniform matches the initializer
190 * \param storage Backing storage for the uniform
191 * \param storage_array_size Array size of the backing storage. This must be
192 * less than or equal to the array size of the type
193 * of \c val. If \c val is not an array, this must
195 * \param val Value of the initializer for the unifrom.
199 verify_data(gl_constant_value
*storage
, unsigned storage_array_size
,
200 ir_constant
*val
, unsigned red_zone_size
,
201 unsigned int boolean_true
)
203 if (val
->type
->base_type
== GLSL_TYPE_ARRAY
) {
204 const glsl_type
*const element_type
= val
->array_elements
[0]->type
;
206 for (unsigned i
= 0; i
< storage_array_size
; i
++) {
207 verify_data(storage
+ (i
* element_type
->components()), 0,
208 val
->array_elements
[i
], 0, boolean_true
);
211 const unsigned components
= element_type
->components();
213 if (red_zone_size
> 0) {
214 EXPECT_TRUE(red_zone_is_intact(storage
,
215 storage_array_size
* components
,
219 ASSERT_EQ(0u, storage_array_size
);
220 for (unsigned i
= 0; i
< val
->type
->components(); i
++) {
221 switch (val
->type
->base_type
) {
224 case GLSL_TYPE_SAMPLER
:
225 case GLSL_TYPE_IMAGE
:
226 EXPECT_EQ(val
->value
.i
[i
], storage
[i
].i
);
228 case GLSL_TYPE_FLOAT
:
229 EXPECT_EQ(val
->value
.f
[i
], storage
[i
].f
);
232 EXPECT_EQ(val
->value
.b
[i
] ? boolean_true
: 0, storage
[i
].i
);
234 case GLSL_TYPE_DOUBLE
:
235 EXPECT_EQ(val
->value
.d
[i
], *(double *)&storage
[i
*2].i
);
237 case GLSL_TYPE_ATOMIC_UINT
:
238 case GLSL_TYPE_STRUCT
:
239 case GLSL_TYPE_ARRAY
:
241 case GLSL_TYPE_ERROR
:
242 case GLSL_TYPE_INTERFACE
:
243 case GLSL_TYPE_SUBROUTINE
:
249 if (red_zone_size
> 0) {
250 EXPECT_TRUE(red_zone_is_intact(storage
,
251 val
->type
->components(),