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 glsl_type_singleton_init_or_ref();
73 this->mem_ctx
= ralloc_context(NULL
);
74 this->prog
= rzalloc(NULL
, struct gl_shader_program
);
75 this->prog
->data
= rzalloc(this->prog
, struct gl_shader_program_data
);
77 /* Set default values used by the test cases.
79 this->actual_index
= 1;
84 set_uniform_initializer::TearDown()
86 ralloc_free(this->mem_ctx
);
89 ralloc_free(this->prog
);
92 glsl_type_singleton_decref();
96 * Create some uniform storage for a program.
98 * \param prog Program to get some storage
99 * \param num_storage Total number of storage slots
100 * \param index_to_set Storage slot that will actually get a value
101 * \param name Name for the actual storage slot
102 * \param type Type for the elements of the actual storage slot
103 * \param array_size Size for the array of the actual storage slot. This
104 * should be zero for non-arrays.
107 establish_uniform_storage(struct gl_shader_program
*prog
, unsigned num_storage
,
108 unsigned index_to_set
, const char *name
,
109 const glsl_type
*type
, unsigned array_size
)
111 const unsigned elements
= MAX2(1, array_size
);
112 const unsigned data_components
= elements
* type
->components();
113 const unsigned total_components
= MAX2(17, (data_components
114 + type
->components()));
115 const unsigned red_zone_components
= total_components
- data_components
;
117 prog
->UniformHash
= new string_to_uint_map
;
118 prog
->data
->UniformStorage
= rzalloc_array(prog
, struct gl_uniform_storage
,
120 prog
->data
->NumUniformStorage
= num_storage
;
122 prog
->data
->UniformStorage
[index_to_set
].name
= (char *) name
;
123 prog
->data
->UniformStorage
[index_to_set
].type
= type
;
124 prog
->data
->UniformStorage
[index_to_set
].array_elements
= array_size
;
125 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
126 prog
->data
->UniformStorage
[index_to_set
].opaque
[sh
].index
= ~0;
127 prog
->data
->UniformStorage
[index_to_set
].opaque
[sh
].active
= false;
129 prog
->data
->UniformStorage
[index_to_set
].num_driver_storage
= 0;
130 prog
->data
->UniformStorage
[index_to_set
].driver_storage
= NULL
;
131 prog
->data
->UniformStorage
[index_to_set
].storage
=
132 rzalloc_array(prog
, union gl_constant_value
, total_components
);
134 fill_storage_array_with_sentinels(prog
->data
->UniformStorage
[index_to_set
].storage
,
136 red_zone_components
);
138 prog
->UniformHash
->put(index_to_set
,
139 prog
->data
->UniformStorage
[index_to_set
].name
);
141 for (unsigned i
= 0; i
< num_storage
; i
++) {
142 if (i
== index_to_set
)
145 prog
->data
->UniformStorage
[i
].name
= (char *) "invalid slot";
146 prog
->data
->UniformStorage
[i
].type
= glsl_type::void_type
;
147 prog
->data
->UniformStorage
[i
].array_elements
= 0;
148 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
149 prog
->data
->UniformStorage
[i
].opaque
[sh
].index
= ~0;
150 prog
->data
->UniformStorage
[i
].opaque
[sh
].active
= false;
152 prog
->data
->UniformStorage
[i
].num_driver_storage
= 0;
153 prog
->data
->UniformStorage
[i
].driver_storage
= NULL
;
154 prog
->data
->UniformStorage
[i
].storage
= NULL
;
157 return red_zone_components
;
161 non_array_test(void *mem_ctx
, struct gl_shader_program
*prog
,
162 unsigned actual_index
, const char *name
,
163 enum glsl_base_type base_type
,
164 unsigned columns
, unsigned rows
)
166 const glsl_type
*const type
=
167 glsl_type::get_instance(base_type
, rows
, columns
);
169 unsigned red_zone_components
=
170 establish_uniform_storage(prog
, 3, actual_index
, name
, type
, 0);
173 generate_data(mem_ctx
, base_type
, columns
, rows
, val
);
175 linker::set_uniform_initializer(mem_ctx
, prog
, name
, type
, val
, 0xF00F);
177 verify_data(prog
->data
->UniformStorage
[actual_index
].storage
, 0, val
,
178 red_zone_components
, 0xF00F);
181 TEST_F(set_uniform_initializer
, int_uniform
)
183 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1);
186 TEST_F(set_uniform_initializer
, ivec2_uniform
)
188 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2);
191 TEST_F(set_uniform_initializer
, ivec3_uniform
)
193 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3);
196 TEST_F(set_uniform_initializer
, ivec4_uniform
)
198 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4);
201 TEST_F(set_uniform_initializer
, uint_uniform
)
203 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1);
206 TEST_F(set_uniform_initializer
, uvec2_uniform
)
208 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2);
211 TEST_F(set_uniform_initializer
, uvec3_uniform
)
213 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3);
216 TEST_F(set_uniform_initializer
, uvec4_uniform
)
218 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4);
221 TEST_F(set_uniform_initializer
, bool_uniform
)
223 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1);
226 TEST_F(set_uniform_initializer
, bvec2_uniform
)
228 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2);
231 TEST_F(set_uniform_initializer
, bvec3_uniform
)
233 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3);
236 TEST_F(set_uniform_initializer
, bvec4_uniform
)
238 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4);
241 TEST_F(set_uniform_initializer
, float_uniform
)
243 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2);
246 TEST_F(set_uniform_initializer
, vec2_uniform
)
248 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2);
251 TEST_F(set_uniform_initializer
, vec3_uniform
)
253 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3);
256 TEST_F(set_uniform_initializer
, vec4_uniform
)
258 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4);
261 TEST_F(set_uniform_initializer
, mat2x2_uniform
)
263 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2);
266 TEST_F(set_uniform_initializer
, mat2x3_uniform
)
268 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3);
271 TEST_F(set_uniform_initializer
, mat2x4_uniform
)
273 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4);
276 TEST_F(set_uniform_initializer
, mat3x2_uniform
)
278 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2);
281 TEST_F(set_uniform_initializer
, mat3x3_uniform
)
283 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3);
286 TEST_F(set_uniform_initializer
, mat3x4_uniform
)
288 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4);
291 TEST_F(set_uniform_initializer
, mat4x2_uniform
)
293 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2);
296 TEST_F(set_uniform_initializer
, mat4x3_uniform
)
298 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3);
301 TEST_F(set_uniform_initializer
, mat4x4_uniform
)
303 non_array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4);
307 array_test(void *mem_ctx
, struct gl_shader_program
*prog
,
308 unsigned actual_index
, const char *name
,
309 enum glsl_base_type base_type
,
310 unsigned columns
, unsigned rows
, unsigned array_size
,
311 unsigned excess_data_size
)
313 const glsl_type
*const element_type
=
314 glsl_type::get_instance(base_type
, rows
, columns
);
316 const unsigned red_zone_components
=
317 establish_uniform_storage(prog
, 3, actual_index
, name
, element_type
,
320 /* The constant value generated may have more array elements than the
321 * uniform that it initializes. In the real compiler and linker this can
322 * happen when a uniform array is compacted because some of the tail
323 * elements are not used. In this case, the type of the uniform will be
324 * modified, but the initializer will not.
327 generate_array_data(mem_ctx
, base_type
, columns
, rows
,
328 array_size
+ excess_data_size
, val
);
330 linker::set_uniform_initializer(mem_ctx
, prog
, name
, element_type
, val
,
333 verify_data(prog
->data
->UniformStorage
[actual_index
].storage
, array_size
,
334 val
, red_zone_components
, 0xF00F);
337 TEST_F(set_uniform_initializer
, int_array_uniform
)
339 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1, 4, 0);
342 TEST_F(set_uniform_initializer
, ivec2_array_uniform
)
344 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2, 4, 0);
347 TEST_F(set_uniform_initializer
, ivec3_array_uniform
)
349 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3, 4, 0);
352 TEST_F(set_uniform_initializer
, ivec4_array_uniform
)
354 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4, 4, 0);
357 TEST_F(set_uniform_initializer
, uint_array_uniform
)
359 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1, 4, 0);
362 TEST_F(set_uniform_initializer
, uvec2_array_uniform
)
364 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2, 4, 0);
367 TEST_F(set_uniform_initializer
, uvec3_array_uniform
)
369 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3, 4, 0);
372 TEST_F(set_uniform_initializer
, uvec4_array_uniform
)
374 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4, 4, 0);
377 TEST_F(set_uniform_initializer
, bool_array_uniform
)
379 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1, 4, 0);
382 TEST_F(set_uniform_initializer
, bvec2_array_uniform
)
384 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2, 4, 0);
387 TEST_F(set_uniform_initializer
, bvec3_array_uniform
)
389 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3, 4, 0);
392 TEST_F(set_uniform_initializer
, bvec4_array_uniform
)
394 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4, 4, 0);
397 TEST_F(set_uniform_initializer
, float_array_uniform
)
399 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 1, 4, 0);
402 TEST_F(set_uniform_initializer
, vec2_array_uniform
)
404 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2, 4, 0);
407 TEST_F(set_uniform_initializer
, vec3_array_uniform
)
409 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3, 4, 0);
412 TEST_F(set_uniform_initializer
, vec4_array_uniform
)
414 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4, 4, 0);
417 TEST_F(set_uniform_initializer
, mat2x2_array_uniform
)
419 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2, 4, 0);
422 TEST_F(set_uniform_initializer
, mat2x3_array_uniform
)
424 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3, 4, 0);
427 TEST_F(set_uniform_initializer
, mat2x4_array_uniform
)
429 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4, 4, 0);
432 TEST_F(set_uniform_initializer
, mat3x2_array_uniform
)
434 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2, 4, 0);
437 TEST_F(set_uniform_initializer
, mat3x3_array_uniform
)
439 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3, 4, 0);
442 TEST_F(set_uniform_initializer
, mat3x4_array_uniform
)
444 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4, 4, 0);
447 TEST_F(set_uniform_initializer
, mat4x2_array_uniform
)
449 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2, 4, 0);
452 TEST_F(set_uniform_initializer
, mat4x3_array_uniform
)
454 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3, 4, 0);
457 TEST_F(set_uniform_initializer
, mat4x4_array_uniform
)
459 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4, 4, 0);
462 TEST_F(set_uniform_initializer
, int_array_uniform_excess_initializer
)
464 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 1, 4, 5);
467 TEST_F(set_uniform_initializer
, ivec2_array_uniform_excess_initializer
)
469 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 2, 4, 5);
472 TEST_F(set_uniform_initializer
, ivec3_array_uniform_excess_initializer
)
474 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 3, 4, 5);
477 TEST_F(set_uniform_initializer
, ivec4_array_uniform_excess_initializer
)
479 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_INT
, 1, 4, 4, 5);
482 TEST_F(set_uniform_initializer
, uint_array_uniform_excess_initializer
)
484 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 1, 4, 5);
487 TEST_F(set_uniform_initializer
, uvec2_array_uniform_excess_initializer
)
489 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 2, 4, 5);
492 TEST_F(set_uniform_initializer
, uvec3_array_uniform_excess_initializer
)
494 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 3, 4, 5);
497 TEST_F(set_uniform_initializer
, uvec4_array_uniform_excess_initializer
)
499 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_UINT
, 1, 4, 4, 5);
502 TEST_F(set_uniform_initializer
, bool_array_uniform_excess_initializer
)
504 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 1, 4, 5);
507 TEST_F(set_uniform_initializer
, bvec2_array_uniform_excess_initializer
)
509 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 2, 4, 5);
512 TEST_F(set_uniform_initializer
, bvec3_array_uniform_excess_initializer
)
514 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 3, 4, 5);
517 TEST_F(set_uniform_initializer
, bvec4_array_uniform_excess_initializer
)
519 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_BOOL
, 1, 4, 4, 5);
522 TEST_F(set_uniform_initializer
, float_array_uniform_excess_initializer
)
524 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 1, 4, 5);
527 TEST_F(set_uniform_initializer
, vec2_array_uniform_excess_initializer
)
529 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 2, 4, 5);
532 TEST_F(set_uniform_initializer
, vec3_array_uniform_excess_initializer
)
534 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 3, 4, 5);
537 TEST_F(set_uniform_initializer
, vec4_array_uniform_excess_initializer
)
539 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 1, 4, 4, 5);
542 TEST_F(set_uniform_initializer
, mat2x2_array_uniform_excess_initializer
)
544 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 2, 4, 5);
547 TEST_F(set_uniform_initializer
, mat2x3_array_uniform_excess_initializer
)
549 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 3, 4, 5);
552 TEST_F(set_uniform_initializer
, mat2x4_array_uniform_excess_initializer
)
554 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 2, 4, 4, 5);
557 TEST_F(set_uniform_initializer
, mat3x2_array_uniform_excess_initializer
)
559 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 2, 4, 5);
562 TEST_F(set_uniform_initializer
, mat3x3_array_uniform_excess_initializer
)
564 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 3, 4, 5);
567 TEST_F(set_uniform_initializer
, mat3x4_array_uniform_excess_initializer
)
569 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 3, 4, 4, 5);
572 TEST_F(set_uniform_initializer
, mat4x2_array_uniform_excess_initializer
)
574 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 2, 4, 5);
577 TEST_F(set_uniform_initializer
, mat4x3_array_uniform_excess_initializer
)
579 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 3, 4, 5);
582 TEST_F(set_uniform_initializer
, mat4x4_array_uniform_excess_initializer
)
584 array_test(mem_ctx
, prog
, actual_index
, name
, GLSL_TYPE_FLOAT
, 4, 4, 4, 5);