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/compiler.h"
25 #include "main/mtypes.h"
26 #include "main/macros.h"
28 #include "uniform_initializer_utils.h"
32 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
33 const char *name
, const glsl_type
*type
,
37 class set_uniform_initializer
: public ::testing::Test
{
40 virtual void TearDown();
43 * Index of the uniform to be tested.
45 * All of the \c set_uniform_initializer tests create several slots for
46 * unifroms. All but one of the slots is fake. This field holds the index
47 * of the slot for the uniform being tested.
49 unsigned actual_index
;
52 * Name of the uniform to be tested.
57 * Shader program used in the test.
59 struct gl_shader_program
*prog
;
62 * Ralloc memory context used for all temporary allocations.
68 set_uniform_initializer::SetUp()
70 this->mem_ctx
= ralloc_context(NULL
);
71 this->prog
= rzalloc(NULL
, struct gl_shader_program
);
73 /* Set default values used by the test cases.
75 this->actual_index
= 1;
80 set_uniform_initializer::TearDown()
82 ralloc_free(this->mem_ctx
);
85 ralloc_free(this->prog
);
90 * Create some uniform storage for a program.
92 * \param prog Program to get some storage
93 * \param num_storage Total number of storage slots
94 * \param index_to_set Storage slot that will actually get a value
95 * \param name Name for the actual storage slot
96 * \param type Type for the elements of the actual storage slot
97 * \param array_size Size for the array of the actual storage slot. This
98 * should be zero for non-arrays.
101 establish_uniform_storage(struct gl_shader_program
*prog
, unsigned num_storage
,
102 unsigned index_to_set
, const char *name
,
103 const glsl_type
*type
, unsigned array_size
)
105 const unsigned elements
= MAX2(1, array_size
);
106 const unsigned data_components
= elements
* type
->components();
107 const unsigned total_components
= MAX2(17, (data_components
108 + type
->components()));
109 const unsigned red_zone_components
= total_components
- data_components
;
111 prog
->UniformStorage
= rzalloc_array(prog
, struct gl_uniform_storage
,
113 prog
->NumUserUniformStorage
= num_storage
;
115 prog
->UniformStorage
[index_to_set
].name
= (char *) name
;
116 prog
->UniformStorage
[index_to_set
].type
= type
;
117 prog
->UniformStorage
[index_to_set
].array_elements
= array_size
;
118 prog
->UniformStorage
[index_to_set
].initialized
= false;
119 prog
->UniformStorage
[index_to_set
].sampler
= ~0;
120 prog
->UniformStorage
[index_to_set
].num_driver_storage
= 0;
121 prog
->UniformStorage
[index_to_set
].driver_storage
= NULL
;
122 prog
->UniformStorage
[index_to_set
].storage
=
123 rzalloc_array(prog
, union gl_constant_value
, total_components
);
125 fill_storage_array_with_sentinels(prog
->UniformStorage
[index_to_set
].storage
,
127 red_zone_components
);
129 for (unsigned i
= 0; i
< num_storage
; i
++) {
130 if (i
== index_to_set
)
133 prog
->UniformStorage
[i
].name
= (char *) "invalid slot";
134 prog
->UniformStorage
[i
].type
= glsl_type::void_type
;
135 prog
->UniformStorage
[i
].array_elements
= 0;
136 prog
->UniformStorage
[i
].initialized
= false;
137 prog
->UniformStorage
[i
].sampler
= ~0;
138 prog
->UniformStorage
[i
].num_driver_storage
= 0;
139 prog
->UniformStorage
[i
].driver_storage
= NULL
;
140 prog
->UniformStorage
[i
].storage
= NULL
;
143 return red_zone_components
;
147 * Verify that the correct uniform is marked as having been initialized.
150 verify_initialization(struct gl_shader_program
*prog
, unsigned actual_index
)
152 for (unsigned i
= 0; i
< prog
->NumUserUniformStorage
; i
++) {
153 if (i
== actual_index
) {
154 EXPECT_TRUE(prog
->UniformStorage
[actual_index
].initialized
);
156 EXPECT_FALSE(prog
->UniformStorage
[i
].initialized
);
162 non_array_test(void *mem_ctx
, struct gl_shader_program
*prog
,
163 unsigned actual_index
, const char *name
,
164 enum glsl_base_type base_type
,
165 unsigned columns
, unsigned rows
)
167 const glsl_type
*const type
=
168 glsl_type::get_instance(base_type
, rows
, columns
);
170 unsigned red_zone_components
=
171 establish_uniform_storage(prog
, 3, actual_index
, name
, type
, 0);
174 generate_data(mem_ctx
, base_type
, columns
, rows
, val
);
176 linker::set_uniform_initializer(mem_ctx
, prog
, name
, type
, val
);
178 verify_initialization(prog
, actual_index
);
179 verify_data(prog
->UniformStorage
[actual_index
].storage
, 0, val
,
180 red_zone_components
);
183 TEST_F(set_uniform_initializer
, int_uniform
)
185 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1);
188 TEST_F(set_uniform_initializer
, ivec2_uniform
)
190 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2);
193 TEST_F(set_uniform_initializer
, ivec3_uniform
)
195 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3);
198 TEST_F(set_uniform_initializer
, ivec4_uniform
)
200 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4);
203 TEST_F(set_uniform_initializer
, uint_uniform
)
205 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1);
208 TEST_F(set_uniform_initializer
, uvec2_uniform
)
210 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2);
213 TEST_F(set_uniform_initializer
, uvec3_uniform
)
215 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3);
218 TEST_F(set_uniform_initializer
, uvec4_uniform
)
220 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4);
223 TEST_F(set_uniform_initializer
, bool_uniform
)
225 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1);
228 TEST_F(set_uniform_initializer
, bvec2_uniform
)
230 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2);
233 TEST_F(set_uniform_initializer
, bvec3_uniform
)
235 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3);
238 TEST_F(set_uniform_initializer
, bvec4_uniform
)
240 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4);
243 TEST_F(set_uniform_initializer
, float_uniform
)
245 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2);
248 TEST_F(set_uniform_initializer
, vec2_uniform
)
250 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2);
253 TEST_F(set_uniform_initializer
, vec3_uniform
)
255 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3);
258 TEST_F(set_uniform_initializer
, vec4_uniform
)
260 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4);
263 TEST_F(set_uniform_initializer
, mat2x2_uniform
)
265 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2);
268 TEST_F(set_uniform_initializer
, mat2x3_uniform
)
270 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3);
273 TEST_F(set_uniform_initializer
, mat2x4_uniform
)
275 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4);
278 TEST_F(set_uniform_initializer
, mat3x2_uniform
)
280 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2);
283 TEST_F(set_uniform_initializer
, mat3x3_uniform
)
285 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3);
288 TEST_F(set_uniform_initializer
, mat3x4_uniform
)
290 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4);
293 TEST_F(set_uniform_initializer
, mat4x2_uniform
)
295 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2);
298 TEST_F(set_uniform_initializer
, mat4x3_uniform
)
300 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3);
303 TEST_F(set_uniform_initializer
, mat4x4_uniform
)
305 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4);
309 array_test(void *mem_ctx
, struct gl_shader_program
*prog
,
310 unsigned actual_index
, const char *name
,
311 enum glsl_base_type base_type
,
312 unsigned columns
, unsigned rows
, unsigned array_size
,
313 unsigned excess_data_size
)
315 const glsl_type
*const element_type
=
316 glsl_type::get_instance(base_type
, rows
, columns
);
318 const unsigned red_zone_components
=
319 establish_uniform_storage(prog
, 3, actual_index
, name
, element_type
,
322 /* The constant value generated may have more array elements than the
323 * uniform that it initializes. In the real compiler and linker this can
324 * happen when a uniform array is compacted because some of the tail
325 * elements are not used. In this case, the type of the uniform will be
326 * modified, but the initializer will not.
329 generate_array_data(mem_ctx
, base_type
, columns
, rows
,
330 array_size
+ excess_data_size
, val
);
332 linker::set_uniform_initializer(mem_ctx
, prog
, name
, element_type
, val
);
334 verify_initialization(prog
, actual_index
);
335 verify_data(prog
->UniformStorage
[actual_index
].storage
, array_size
,
336 val
, red_zone_components
);
339 TEST_F(set_uniform_initializer
, int_array_uniform
)
341 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1, 4, 0);
344 TEST_F(set_uniform_initializer
, ivec2_array_uniform
)
346 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2, 4, 0);
349 TEST_F(set_uniform_initializer
, ivec3_array_uniform
)
351 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3, 4, 0);
354 TEST_F(set_uniform_initializer
, ivec4_array_uniform
)
356 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4, 4, 0);
359 TEST_F(set_uniform_initializer
, uint_array_uniform
)
361 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1, 4, 0);
364 TEST_F(set_uniform_initializer
, uvec2_array_uniform
)
366 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2, 4, 0);
369 TEST_F(set_uniform_initializer
, uvec3_array_uniform
)
371 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3, 4, 0);
374 TEST_F(set_uniform_initializer
, uvec4_array_uniform
)
376 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4, 4, 0);
379 TEST_F(set_uniform_initializer
, bool_array_uniform
)
381 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1, 4, 0);
384 TEST_F(set_uniform_initializer
, bvec2_array_uniform
)
386 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2, 4, 0);
389 TEST_F(set_uniform_initializer
, bvec3_array_uniform
)
391 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3, 4, 0);
394 TEST_F(set_uniform_initializer
, bvec4_array_uniform
)
396 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4, 4, 0);
399 TEST_F(set_uniform_initializer
, float_array_uniform
)
401 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 1, 4, 0);
404 TEST_F(set_uniform_initializer
, vec2_array_uniform
)
406 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2, 4, 0);
409 TEST_F(set_uniform_initializer
, vec3_array_uniform
)
411 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3, 4, 0);
414 TEST_F(set_uniform_initializer
, vec4_array_uniform
)
416 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4, 4, 0);
419 TEST_F(set_uniform_initializer
, mat2x2_array_uniform
)
421 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2, 4, 0);
424 TEST_F(set_uniform_initializer
, mat2x3_array_uniform
)
426 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3, 4, 0);
429 TEST_F(set_uniform_initializer
, mat2x4_array_uniform
)
431 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4, 4, 0);
434 TEST_F(set_uniform_initializer
, mat3x2_array_uniform
)
436 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2, 4, 0);
439 TEST_F(set_uniform_initializer
, mat3x3_array_uniform
)
441 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3, 4, 0);
444 TEST_F(set_uniform_initializer
, mat3x4_array_uniform
)
446 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4, 4, 0);
449 TEST_F(set_uniform_initializer
, mat4x2_array_uniform
)
451 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2, 4, 0);
454 TEST_F(set_uniform_initializer
, mat4x3_array_uniform
)
456 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3, 4, 0);
459 TEST_F(set_uniform_initializer
, mat4x4_array_uniform
)
461 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4, 4, 0);
464 TEST_F(set_uniform_initializer
, int_array_uniform_excess_initializer
)
466 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1, 4, 5);
469 TEST_F(set_uniform_initializer
, ivec2_array_uniform_excess_initializer
)
471 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2, 4, 5);
474 TEST_F(set_uniform_initializer
, ivec3_array_uniform_excess_initializer
)
476 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3, 4, 5);
479 TEST_F(set_uniform_initializer
, ivec4_array_uniform_excess_initializer
)
481 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4, 4, 5);
484 TEST_F(set_uniform_initializer
, uint_array_uniform_excess_initializer
)
486 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1, 4, 5);
489 TEST_F(set_uniform_initializer
, uvec2_array_uniform_excess_initializer
)
491 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2, 4, 5);
494 TEST_F(set_uniform_initializer
, uvec3_array_uniform_excess_initializer
)
496 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3, 4, 5);
499 TEST_F(set_uniform_initializer
, uvec4_array_uniform_excess_initializer
)
501 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4, 4, 5);
504 TEST_F(set_uniform_initializer
, bool_array_uniform_excess_initializer
)
506 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1, 4, 5);
509 TEST_F(set_uniform_initializer
, bvec2_array_uniform_excess_initializer
)
511 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2, 4, 5);
514 TEST_F(set_uniform_initializer
, bvec3_array_uniform_excess_initializer
)
516 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3, 4, 5);
519 TEST_F(set_uniform_initializer
, bvec4_array_uniform_excess_initializer
)
521 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4, 4, 5);
524 TEST_F(set_uniform_initializer
, float_array_uniform_excess_initializer
)
526 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 1, 4, 5);
529 TEST_F(set_uniform_initializer
, vec2_array_uniform_excess_initializer
)
531 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2, 4, 5);
534 TEST_F(set_uniform_initializer
, vec3_array_uniform_excess_initializer
)
536 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3, 4, 5);
539 TEST_F(set_uniform_initializer
, vec4_array_uniform_excess_initializer
)
541 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4, 4, 5);
544 TEST_F(set_uniform_initializer
, mat2x2_array_uniform_excess_initializer
)
546 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2, 4, 5);
549 TEST_F(set_uniform_initializer
, mat2x3_array_uniform_excess_initializer
)
551 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3, 4, 5);
554 TEST_F(set_uniform_initializer
, mat2x4_array_uniform_excess_initializer
)
556 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4, 4, 5);
559 TEST_F(set_uniform_initializer
, mat3x2_array_uniform_excess_initializer
)
561 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2, 4, 5);
564 TEST_F(set_uniform_initializer
, mat3x3_array_uniform_excess_initializer
)
566 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3, 4, 5);
569 TEST_F(set_uniform_initializer
, mat3x4_array_uniform_excess_initializer
)
571 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4, 4, 5);
574 TEST_F(set_uniform_initializer
, mat4x2_array_uniform_excess_initializer
)
576 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2, 4, 5);
579 TEST_F(set_uniform_initializer
, mat4x3_array_uniform_excess_initializer
)
581 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3, 4, 5);
584 TEST_F(set_uniform_initializer
, mat4x4_array_uniform_excess_initializer
)
586 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4, 4, 5);