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 30void 31fill_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 */ 45static ::testing::AssertionResult 46red_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 63static 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 */ 73static void 74generate_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_UINT64: 100 data.u64[i] = (uint64_t) values[idx]; 101 break; 102 case GLSL_TYPE_INT64: 103 data.i64[i] = (int64_t) values[idx]; 104 break; 105 case GLSL_TYPE_ATOMIC_UINT: 106 case GLSL_TYPE_STRUCT: 107 case GLSL_TYPE_ARRAY: 108 case GLSL_TYPE_VOID: 109 case GLSL_TYPE_ERROR: 110 case GLSL_TYPE_INTERFACE: 111 case GLSL_TYPE_SUBROUTINE: 112 case GLSL_TYPE_FUNCTION: 113 case GLSL_TYPE_FLOAT16: 114 case GLSL_TYPE_UINT16: 115 case GLSL_TYPE_INT16: 116 case GLSL_TYPE_UINT8: 117 case GLSL_TYPE_INT8: 118 ASSERT_TRUE(false); 119 break; 120 } 121 } 122 123 /* Generate and verify the constant. 124 */ 125 val = new(mem_ctx) ir_constant(type, &data); 126 127 for (unsigned i = 0; i < type->components(); i++) { 128 switch (type->base_type) { 129 case GLSL_TYPE_UINT: 130 case GLSL_TYPE_INT: 131 case GLSL_TYPE_SAMPLER: 132 case GLSL_TYPE_IMAGE: 133 ASSERT_EQ(data.i[i], val->value.i[i]); 134 break; 135 case GLSL_TYPE_FLOAT: 136 ASSERT_EQ(data.f[i], val->value.f[i]); 137 break; 138 case GLSL_TYPE_BOOL: 139 ASSERT_EQ(data.b[i], val->value.b[i]); 140 break; 141 case GLSL_TYPE_DOUBLE: 142 ASSERT_EQ(data.d[i], val->value.d[i]); 143 break; 144 case GLSL_TYPE_UINT64: 145 ASSERT_EQ(data.u64[i], val->value.u64[i]); 146 break; 147 case GLSL_TYPE_INT64: 148 ASSERT_EQ(data.i64[i], val->value.i64[i]); 149 break; 150 case GLSL_TYPE_ATOMIC_UINT: 151 case GLSL_TYPE_STRUCT: 152 case GLSL_TYPE_ARRAY: 153 case GLSL_TYPE_VOID: 154 case GLSL_TYPE_ERROR: 155 case GLSL_TYPE_INTERFACE: 156 case GLSL_TYPE_SUBROUTINE: 157 case GLSL_TYPE_FUNCTION: 158 case GLSL_TYPE_FLOAT16: 159 case GLSL_TYPE_UINT16: 160 case GLSL_TYPE_INT16: 161 case GLSL_TYPE_UINT8: 162 case GLSL_TYPE_INT8: 163 ASSERT_TRUE(false); 164 break; 165 } 166 } 167} 168 169void 170generate_data(void *mem_ctx, enum glsl_base_type base_type, 171 unsigned columns, unsigned rows, 172 ir_constant *&val) 173{ 174 /* Determine what the type of the generated constant should be. 175 */ 176 const glsl_type *const type = 177 glsl_type::get_instance(base_type, rows, columns); 178 ASSERT_FALSE(type->is_error()); 179 180 generate_data_element(mem_ctx, type, val, 0); 181} 182 183void 184generate_array_data(void *mem_ctx, enum glsl_base_type base_type, 185 unsigned columns, unsigned rows, unsigned array_size, 186 ir_constant *&val) 187{ 188 /* Determine what the type of the generated constant should be. 189 */ 190 const glsl_type *const element_type = 191 glsl_type::get_instance(base_type, rows, columns); 192 ASSERT_FALSE(element_type->is_error()); 193 194 const glsl_type *const array_type = 195 glsl_type::get_array_instance(element_type, array_size); 196 ASSERT_FALSE(array_type->is_error()); 197 198 /* Set the initial data values for the generated constant. 199 */ 200 exec_list values_for_array; 201 for (unsigned i = 0; i < array_size; i++) { 202 ir_constant *element; 203 204 generate_data_element(mem_ctx, element_type, element, i); 205 values_for_array.push_tail(element); 206 } 207 208 val = new(mem_ctx) ir_constant(array_type, &values_for_array); 209} 210 211static uint64_t 212uint64_storage(union gl_constant_value *storage) 213{ 214 uint64_t val; 215 memcpy(&val, &storage->i, sizeof(uint64_t)); 216 return val; 217} 218 219static uint64_t 220double_storage(union gl_constant_value *storage) 221{ 222 double val; 223 memcpy(&val, &storage->i, sizeof(double)); 224 return val; 225} 226 227/** 228 * Verify that the data stored for the uniform matches the initializer 229 * 230 * \param storage Backing storage for the uniform 231 * \param storage_array_size Array size of the backing storage. This must be 232 * less than or equal to the array size of the type 233 * of \c val. If \c val is not an array, this must 234 * be zero. 235 * \param val Value of the initializer for the unifrom. 236 * \param red_zone 237 */ 238void 239verify_data(gl_constant_value *storage, unsigned storage_array_size, 240 ir_constant *val, unsigned red_zone_size, 241 unsigned int boolean_true) 242{ 243 if (val->type->is_array()) { 244 const glsl_type *const element_type = val->const_elements[0]->type; 245 246 for (unsigned i = 0; i < storage_array_size; i++) { 247 verify_data(storage + (i * element_type->components()), 0, 248 val->const_elements[i], 0, boolean_true); 249 } 250 251 const unsigned components = element_type->components(); 252 253 if (red_zone_size > 0) { 254 EXPECT_TRUE(red_zone_is_intact(storage, 255 storage_array_size * components, 256 red_zone_size)); 257 } 258 } else { 259 ASSERT_EQ(0u, storage_array_size); 260 for (unsigned i = 0; i < val->type->components(); i++) { 261 switch (val->type->base_type) { 262 case GLSL_TYPE_UINT: 263 case GLSL_TYPE_INT: 264 case GLSL_TYPE_SAMPLER: 265 case GLSL_TYPE_IMAGE: 266 EXPECT_EQ(val->value.i[i], storage[i].i); 267 break; 268 case GLSL_TYPE_FLOAT: 269 EXPECT_EQ(val->value.f[i], storage[i].f); 270 break; 271 case GLSL_TYPE_BOOL: 272 EXPECT_EQ(val->value.b[i] ? boolean_true : 0, storage[i].i); 273 break; 274 case GLSL_TYPE_DOUBLE: 275 EXPECT_EQ(val->value.d[i], double_storage(&storage[i*2])); 276 break; 277 case GLSL_TYPE_UINT64: 278 EXPECT_EQ(val->value.u64[i], uint64_storage(&storage[i*2])); 279 break; 280 case GLSL_TYPE_INT64: 281 EXPECT_EQ(val->value.i64[i], uint64_storage(&storage[i*2])); 282 break; 283 case GLSL_TYPE_ATOMIC_UINT: 284 case GLSL_TYPE_STRUCT: 285 case GLSL_TYPE_ARRAY: 286 case GLSL_TYPE_VOID: 287 case GLSL_TYPE_ERROR: 288 case GLSL_TYPE_INTERFACE: 289 case GLSL_TYPE_SUBROUTINE: 290 case GLSL_TYPE_FUNCTION: 291 case GLSL_TYPE_FLOAT16: 292 case GLSL_TYPE_UINT16: 293 case GLSL_TYPE_INT16: 294 case GLSL_TYPE_UINT8: 295 case GLSL_TYPE_INT8: 296 ASSERT_TRUE(false); 297 break; 298 } 299 } 300 301 if (red_zone_size > 0) { 302 EXPECT_TRUE(red_zone_is_intact(storage, 303 val->type->components(), 304 red_zone_size)); 305 } 306 } 307} 308