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"
27 #include "util/ralloc.h"
28 #include "string_to_uint_map.h"
29 #include "uniform_initializer_utils.h"
33 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
34 const char *name
, const glsl_type
*type
,
35 ir_constant
*val
, unsigned int boolean_true
);
38 class set_uniform_initializer
: public ::testing::Test
{
41 virtual void TearDown();
44 * Index of the uniform to be tested.
46 * All of the \c set_uniform_initializer tests create several slots for
47 * unifroms. All but one of the slots is fake. This field holds the index
48 * of the slot for the uniform being tested.
50 unsigned actual_index
;
53 * Name of the uniform to be tested.
58 * Shader program used in the test.
60 struct gl_shader_program
*prog
;
63 * Ralloc memory context used for all temporary allocations.
69 set_uniform_initializer::SetUp()
71 this->mem_ctx
= ralloc_context(NULL
);
72 this->prog
= rzalloc(NULL
, struct gl_shader_program
);
73 this->prog
->data
= rzalloc(this->prog
, struct gl_shader_program_data
);
75 /* Set default values used by the test cases.
77 this->actual_index
= 1;
82 set_uniform_initializer::TearDown()
84 ralloc_free(this->mem_ctx
);
87 ralloc_free(this->prog
);
92 * Create some uniform storage for a program.
94 * \param prog Program to get some storage
95 * \param num_storage Total number of storage slots
96 * \param index_to_set Storage slot that will actually get a value
97 * \param name Name for the actual storage slot
98 * \param type Type for the elements of the actual storage slot
99 * \param array_size Size for the array of the actual storage slot. This
100 * should be zero for non-arrays.
103 establish_uniform_storage(struct gl_shader_program
*prog
, unsigned num_storage
,
104 unsigned index_to_set
, const char *name
,
105 const glsl_type
*type
, unsigned array_size
)
107 const unsigned elements
= MAX2(1, array_size
);
108 const unsigned data_components
= elements
* type
->components();
109 const unsigned total_components
= MAX2(17, (data_components
110 + type
->components()));
111 const unsigned red_zone_components
= total_components
- data_components
;
113 prog
->UniformHash
= new string_to_uint_map
;
114 prog
->data
->UniformStorage
= rzalloc_array(prog
, struct gl_uniform_storage
,
116 prog
->data
->NumUniformStorage
= num_storage
;
118 prog
->data
->UniformStorage
[index_to_set
].name
= (char *) name
;
119 prog
->data
->UniformStorage
[index_to_set
].type
= type
;
120 prog
->data
->UniformStorage
[index_to_set
].array_elements
= array_size
;
121 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
122 prog
->data
->UniformStorage
[index_to_set
].opaque
[sh
].index
= ~0;
123 prog
->data
->UniformStorage
[index_to_set
].opaque
[sh
].active
= false;
125 prog
->data
->UniformStorage
[index_to_set
].num_driver_storage
= 0;
126 prog
->data
->UniformStorage
[index_to_set
].driver_storage
= NULL
;
127 prog
->data
->UniformStorage
[index_to_set
].storage
=
128 rzalloc_array(prog
, union gl_constant_value
, total_components
);
130 fill_storage_array_with_sentinels(prog
->data
->UniformStorage
[index_to_set
].storage
,
132 red_zone_components
);
134 prog
->UniformHash
->put(index_to_set
,
135 prog
->data
->UniformStorage
[index_to_set
].name
);
137 for (unsigned i
= 0; i
< num_storage
; i
++) {
138 if (i
== index_to_set
)
141 prog
->data
->UniformStorage
[i
].name
= (char *) "invalid slot";
142 prog
->data
->UniformStorage
[i
].type
= glsl_type::void_type
;
143 prog
->data
->UniformStorage
[i
].array_elements
= 0;
144 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
145 prog
->data
->UniformStorage
[i
].opaque
[sh
].index
= ~0;
146 prog
->data
->UniformStorage
[i
].opaque
[sh
].active
= false;
148 prog
->data
->UniformStorage
[i
].num_driver_storage
= 0;
149 prog
->data
->UniformStorage
[i
].driver_storage
= NULL
;
150 prog
->data
->UniformStorage
[i
].storage
= NULL
;
153 return red_zone_components
;
157 non_array_test(void *mem_ctx
, struct gl_shader_program
*prog
,
158 unsigned actual_index
, const char *name
,
159 enum glsl_base_type base_type
,
160 unsigned columns
, unsigned rows
)
162 const glsl_type
*const type
=
163 glsl_type::get_instance(base_type
, rows
, columns
);
165 unsigned red_zone_components
=
166 establish_uniform_storage(prog
, 3, actual_index
, name
, type
, 0);
169 generate_data(mem_ctx
, base_type
, columns
, rows
, val
);
171 linker::set_uniform_initializer(mem_ctx
, prog
, name
, type
, val
, 0xF00F);
173 verify_data(prog
->data
->UniformStorage
[actual_index
].storage
, 0, val
,
174 red_zone_components
, 0xF00F);
177 TEST_F(set_uniform_initializer
, int_uniform
)
179 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1);
182 TEST_F(set_uniform_initializer
, ivec2_uniform
)
184 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2);
187 TEST_F(set_uniform_initializer
, ivec3_uniform
)
189 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3);
192 TEST_F(set_uniform_initializer
, ivec4_uniform
)
194 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4);
197 TEST_F(set_uniform_initializer
, uint_uniform
)
199 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1);
202 TEST_F(set_uniform_initializer
, uvec2_uniform
)
204 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2);
207 TEST_F(set_uniform_initializer
, uvec3_uniform
)
209 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3);
212 TEST_F(set_uniform_initializer
, uvec4_uniform
)
214 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4);
217 TEST_F(set_uniform_initializer
, bool_uniform
)
219 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1);
222 TEST_F(set_uniform_initializer
, bvec2_uniform
)
224 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2);
227 TEST_F(set_uniform_initializer
, bvec3_uniform
)
229 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3);
232 TEST_F(set_uniform_initializer
, bvec4_uniform
)
234 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4);
237 TEST_F(set_uniform_initializer
, float_uniform
)
239 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2);
242 TEST_F(set_uniform_initializer
, vec2_uniform
)
244 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2);
247 TEST_F(set_uniform_initializer
, vec3_uniform
)
249 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3);
252 TEST_F(set_uniform_initializer
, vec4_uniform
)
254 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4);
257 TEST_F(set_uniform_initializer
, mat2x2_uniform
)
259 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2);
262 TEST_F(set_uniform_initializer
, mat2x3_uniform
)
264 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3);
267 TEST_F(set_uniform_initializer
, mat2x4_uniform
)
269 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4);
272 TEST_F(set_uniform_initializer
, mat3x2_uniform
)
274 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2);
277 TEST_F(set_uniform_initializer
, mat3x3_uniform
)
279 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3);
282 TEST_F(set_uniform_initializer
, mat3x4_uniform
)
284 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4);
287 TEST_F(set_uniform_initializer
, mat4x2_uniform
)
289 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2);
292 TEST_F(set_uniform_initializer
, mat4x3_uniform
)
294 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3);
297 TEST_F(set_uniform_initializer
, mat4x4_uniform
)
299 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4);
303 array_test(void *mem_ctx
, struct gl_shader_program
*prog
,
304 unsigned actual_index
, const char *name
,
305 enum glsl_base_type base_type
,
306 unsigned columns
, unsigned rows
, unsigned array_size
,
307 unsigned excess_data_size
)
309 const glsl_type
*const element_type
=
310 glsl_type::get_instance(base_type
, rows
, columns
);
312 const unsigned red_zone_components
=
313 establish_uniform_storage(prog
, 3, actual_index
, name
, element_type
,
316 /* The constant value generated may have more array elements than the
317 * uniform that it initializes. In the real compiler and linker this can
318 * happen when a uniform array is compacted because some of the tail
319 * elements are not used. In this case, the type of the uniform will be
320 * modified, but the initializer will not.
323 generate_array_data(mem_ctx
, base_type
, columns
, rows
,
324 array_size
+ excess_data_size
, val
);
326 linker::set_uniform_initializer(mem_ctx
, prog
, name
, element_type
, val
,
329 verify_data(prog
->data
->UniformStorage
[actual_index
].storage
, array_size
,
330 val
, red_zone_components
, 0xF00F);
333 TEST_F(set_uniform_initializer
, int_array_uniform
)
335 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1, 4, 0);
338 TEST_F(set_uniform_initializer
, ivec2_array_uniform
)
340 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2, 4, 0);
343 TEST_F(set_uniform_initializer
, ivec3_array_uniform
)
345 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3, 4, 0);
348 TEST_F(set_uniform_initializer
, ivec4_array_uniform
)
350 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4, 4, 0);
353 TEST_F(set_uniform_initializer
, uint_array_uniform
)
355 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1, 4, 0);
358 TEST_F(set_uniform_initializer
, uvec2_array_uniform
)
360 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2, 4, 0);
363 TEST_F(set_uniform_initializer
, uvec3_array_uniform
)
365 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3, 4, 0);
368 TEST_F(set_uniform_initializer
, uvec4_array_uniform
)
370 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4, 4, 0);
373 TEST_F(set_uniform_initializer
, bool_array_uniform
)
375 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1, 4, 0);
378 TEST_F(set_uniform_initializer
, bvec2_array_uniform
)
380 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2, 4, 0);
383 TEST_F(set_uniform_initializer
, bvec3_array_uniform
)
385 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3, 4, 0);
388 TEST_F(set_uniform_initializer
, bvec4_array_uniform
)
390 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4, 4, 0);
393 TEST_F(set_uniform_initializer
, float_array_uniform
)
395 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 1, 4, 0);
398 TEST_F(set_uniform_initializer
, vec2_array_uniform
)
400 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2, 4, 0);
403 TEST_F(set_uniform_initializer
, vec3_array_uniform
)
405 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3, 4, 0);
408 TEST_F(set_uniform_initializer
, vec4_array_uniform
)
410 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4, 4, 0);
413 TEST_F(set_uniform_initializer
, mat2x2_array_uniform
)
415 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2, 4, 0);
418 TEST_F(set_uniform_initializer
, mat2x3_array_uniform
)
420 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3, 4, 0);
423 TEST_F(set_uniform_initializer
, mat2x4_array_uniform
)
425 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4, 4, 0);
428 TEST_F(set_uniform_initializer
, mat3x2_array_uniform
)
430 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2, 4, 0);
433 TEST_F(set_uniform_initializer
, mat3x3_array_uniform
)
435 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3, 4, 0);
438 TEST_F(set_uniform_initializer
, mat3x4_array_uniform
)
440 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4, 4, 0);
443 TEST_F(set_uniform_initializer
, mat4x2_array_uniform
)
445 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2, 4, 0);
448 TEST_F(set_uniform_initializer
, mat4x3_array_uniform
)
450 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3, 4, 0);
453 TEST_F(set_uniform_initializer
, mat4x4_array_uniform
)
455 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4, 4, 0);
458 TEST_F(set_uniform_initializer
, int_array_uniform_excess_initializer
)
460 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1, 4, 5);
463 TEST_F(set_uniform_initializer
, ivec2_array_uniform_excess_initializer
)
465 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2, 4, 5);
468 TEST_F(set_uniform_initializer
, ivec3_array_uniform_excess_initializer
)
470 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3, 4, 5);
473 TEST_F(set_uniform_initializer
, ivec4_array_uniform_excess_initializer
)
475 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4, 4, 5);
478 TEST_F(set_uniform_initializer
, uint_array_uniform_excess_initializer
)
480 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1, 4, 5);
483 TEST_F(set_uniform_initializer
, uvec2_array_uniform_excess_initializer
)
485 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2, 4, 5);
488 TEST_F(set_uniform_initializer
, uvec3_array_uniform_excess_initializer
)
490 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3, 4, 5);
493 TEST_F(set_uniform_initializer
, uvec4_array_uniform_excess_initializer
)
495 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4, 4, 5);
498 TEST_F(set_uniform_initializer
, bool_array_uniform_excess_initializer
)
500 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1, 4, 5);
503 TEST_F(set_uniform_initializer
, bvec2_array_uniform_excess_initializer
)
505 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2, 4, 5);
508 TEST_F(set_uniform_initializer
, bvec3_array_uniform_excess_initializer
)
510 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3, 4, 5);
513 TEST_F(set_uniform_initializer
, bvec4_array_uniform_excess_initializer
)
515 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4, 4, 5);
518 TEST_F(set_uniform_initializer
, float_array_uniform_excess_initializer
)
520 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 1, 4, 5);
523 TEST_F(set_uniform_initializer
, vec2_array_uniform_excess_initializer
)
525 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2, 4, 5);
528 TEST_F(set_uniform_initializer
, vec3_array_uniform_excess_initializer
)
530 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3, 4, 5);
533 TEST_F(set_uniform_initializer
, vec4_array_uniform_excess_initializer
)
535 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4, 4, 5);
538 TEST_F(set_uniform_initializer
, mat2x2_array_uniform_excess_initializer
)
540 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2, 4, 5);
543 TEST_F(set_uniform_initializer
, mat2x3_array_uniform_excess_initializer
)
545 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3, 4, 5);
548 TEST_F(set_uniform_initializer
, mat2x4_array_uniform_excess_initializer
)
550 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4, 4, 5);
553 TEST_F(set_uniform_initializer
, mat3x2_array_uniform_excess_initializer
)
555 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2, 4, 5);
558 TEST_F(set_uniform_initializer
, mat3x3_array_uniform_excess_initializer
)
560 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3, 4, 5);
563 TEST_F(set_uniform_initializer
, mat3x4_array_uniform_excess_initializer
)
565 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4, 4, 5);
568 TEST_F(set_uniform_initializer
, mat4x2_array_uniform_excess_initializer
)
570 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2, 4, 5);
573 TEST_F(set_uniform_initializer
, mat4x3_array_uniform_excess_initializer
)
575 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3, 4, 5);
578 TEST_F(set_uniform_initializer
, mat4x4_array_uniform_excess_initializer
)
580 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4, 4, 5);