1b45052b3f761deb980bcca405f25e543892a93eeIan Romanick/* 2b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * Copyright © 2012 Intel Corporation 3b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * 4b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * Permission is hereby granted, free of charge, to any person obtaining a 5b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * copy of this software and associated documentation files (the "Software"), 6b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * to deal in the Software without restriction, including without limitation 7b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * and/or sell copies of the Software, and to permit persons to whom the 9b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * Software is furnished to do so, subject to the following conditions: 10b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * 11b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * The above copyright notice and this permission notice (including the next 12b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * paragraph) shall be included in all copies or substantial portions of the 13b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * Software. 14b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * 15b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * DEALINGS IN THE SOFTWARE. 22b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 23b45052b3f761deb980bcca405f25e543892a93eeIan Romanick#include <gtest/gtest.h> 24b45052b3f761deb980bcca405f25e543892a93eeIan Romanick#include "main/mtypes.h" 25b45052b3f761deb980bcca405f25e543892a93eeIan Romanick#include "main/macros.h" 26b45052b3f761deb980bcca405f25e543892a93eeIan Romanick#include "ralloc.h" 27b45052b3f761deb980bcca405f25e543892a93eeIan Romanick#include "uniform_initializer_utils.h" 28b45052b3f761deb980bcca405f25e543892a93eeIan Romanick#include <stdio.h> 29b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 30b45052b3f761deb980bcca405f25e543892a93eeIan Romanickvoid 31b45052b3f761deb980bcca405f25e543892a93eeIan Romanickfill_storage_array_with_sentinels(gl_constant_value *storage, 32b45052b3f761deb980bcca405f25e543892a93eeIan Romanick unsigned data_size, 33b45052b3f761deb980bcca405f25e543892a93eeIan Romanick unsigned red_zone_size) 34b45052b3f761deb980bcca405f25e543892a93eeIan Romanick{ 35b45052b3f761deb980bcca405f25e543892a93eeIan Romanick for (unsigned i = 0; i < data_size; i++) 36b45052b3f761deb980bcca405f25e543892a93eeIan Romanick storage[i].u = 0xDEADBEEF; 37b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 38b45052b3f761deb980bcca405f25e543892a93eeIan Romanick for (unsigned i = 0; i < red_zone_size; i++) 39b45052b3f761deb980bcca405f25e543892a93eeIan Romanick storage[data_size + i].u = 0xBADDC0DE; 40b45052b3f761deb980bcca405f25e543892a93eeIan Romanick} 41b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 42b45052b3f761deb980bcca405f25e543892a93eeIan Romanick/** 43b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * Verfiy that markers past the end of the real uniform are unmodified 44b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 45b45052b3f761deb980bcca405f25e543892a93eeIan Romanickstatic ::testing::AssertionResult 46b45052b3f761deb980bcca405f25e543892a93eeIan Romanickred_zone_is_intact(gl_constant_value *storage, 47b45052b3f761deb980bcca405f25e543892a93eeIan Romanick unsigned data_size, 48b45052b3f761deb980bcca405f25e543892a93eeIan Romanick unsigned red_zone_size) 49b45052b3f761deb980bcca405f25e543892a93eeIan Romanick{ 50b45052b3f761deb980bcca405f25e543892a93eeIan Romanick for (unsigned i = 0; i < red_zone_size; i++) { 51b45052b3f761deb980bcca405f25e543892a93eeIan Romanick const unsigned idx = data_size + i; 52b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 53b45052b3f761deb980bcca405f25e543892a93eeIan Romanick if (storage[idx].u != 0xBADDC0DE) 54b45052b3f761deb980bcca405f25e543892a93eeIan Romanick return ::testing::AssertionFailure() 55b45052b3f761deb980bcca405f25e543892a93eeIan Romanick << "storage[" << idx << "].u = " << storage[idx].u 56b45052b3f761deb980bcca405f25e543892a93eeIan Romanick << ", exepected data values = " << data_size 57b45052b3f761deb980bcca405f25e543892a93eeIan Romanick << ", red-zone size = " << red_zone_size; 58b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 59b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 60b45052b3f761deb980bcca405f25e543892a93eeIan Romanick return ::testing::AssertionSuccess(); 61b45052b3f761deb980bcca405f25e543892a93eeIan Romanick} 62b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 63b45052b3f761deb980bcca405f25e543892a93eeIan Romanickstatic const int values[] = { 64b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 2, 0, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 65b45052b3f761deb980bcca405f25e543892a93eeIan Romanick}; 66b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 67b45052b3f761deb980bcca405f25e543892a93eeIan Romanick/** 68b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * Generate a single data element. 69b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * 70b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * This is by both \c generate_data and \c generate_array_data to create the 71b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * data. 72b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 73b45052b3f761deb980bcca405f25e543892a93eeIan Romanickstatic void 74b45052b3f761deb980bcca405f25e543892a93eeIan Romanickgenerate_data_element(void *mem_ctx, const glsl_type *type, 75b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ir_constant *&val, unsigned data_index_base) 76b45052b3f761deb980bcca405f25e543892a93eeIan Romanick{ 77b45052b3f761deb980bcca405f25e543892a93eeIan Romanick /* Set the initial data values for the generated constant. 78b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 79b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ir_constant_data data; 80b45052b3f761deb980bcca405f25e543892a93eeIan Romanick memset(&data, 0, sizeof(data)); 81b45052b3f761deb980bcca405f25e543892a93eeIan Romanick for (unsigned i = 0; i < type->components(); i++) { 82b45052b3f761deb980bcca405f25e543892a93eeIan Romanick const unsigned idx = (i + data_index_base) % Elements(values); 83b45052b3f761deb980bcca405f25e543892a93eeIan Romanick switch (type->base_type) { 84b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_UINT: 85b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_INT: 86b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_SAMPLER: 87b45052b3f761deb980bcca405f25e543892a93eeIan Romanick data.i[i] = values[idx]; 88b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 89b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_FLOAT: 90b45052b3f761deb980bcca405f25e543892a93eeIan Romanick data.f[i] = float(values[idx]); 91b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 92b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_BOOL: 93b45052b3f761deb980bcca405f25e543892a93eeIan Romanick data.b[i] = bool(values[idx]); 94b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 95b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_STRUCT: 96b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_ARRAY: 97b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_VOID: 98b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_ERROR: 99b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_TRUE(false); 100b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 101b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 102b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 103b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 104b45052b3f761deb980bcca405f25e543892a93eeIan Romanick /* Generate and verify the constant. 105b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 106b45052b3f761deb980bcca405f25e543892a93eeIan Romanick val = new(mem_ctx) ir_constant(type, &data); 107b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 108b45052b3f761deb980bcca405f25e543892a93eeIan Romanick for (unsigned i = 0; i < type->components(); i++) { 109b45052b3f761deb980bcca405f25e543892a93eeIan Romanick switch (type->base_type) { 110b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_UINT: 111b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_INT: 112b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_SAMPLER: 113b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_EQ(data.i[i], val->value.i[i]); 114b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 115b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_FLOAT: 116b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_EQ(data.f[i], val->value.f[i]); 117b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 118b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_BOOL: 119b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_EQ(data.b[i], val->value.b[i]); 120b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 121b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_STRUCT: 122b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_ARRAY: 123b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_VOID: 124b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_ERROR: 125b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_TRUE(false); 126b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 127b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 128b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 129b45052b3f761deb980bcca405f25e543892a93eeIan Romanick} 130b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 131b45052b3f761deb980bcca405f25e543892a93eeIan Romanickvoid 132b45052b3f761deb980bcca405f25e543892a93eeIan Romanickgenerate_data(void *mem_ctx, enum glsl_base_type base_type, 133b45052b3f761deb980bcca405f25e543892a93eeIan Romanick unsigned columns, unsigned rows, 134b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ir_constant *&val) 135b45052b3f761deb980bcca405f25e543892a93eeIan Romanick{ 136b45052b3f761deb980bcca405f25e543892a93eeIan Romanick /* Determine what the type of the generated constant should be. 137b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 138b45052b3f761deb980bcca405f25e543892a93eeIan Romanick const glsl_type *const type = 139b45052b3f761deb980bcca405f25e543892a93eeIan Romanick glsl_type::get_instance(base_type, rows, columns); 140b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_FALSE(type->is_error()); 141b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 142b45052b3f761deb980bcca405f25e543892a93eeIan Romanick generate_data_element(mem_ctx, type, val, 0); 143b45052b3f761deb980bcca405f25e543892a93eeIan Romanick} 144b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 145b45052b3f761deb980bcca405f25e543892a93eeIan Romanickvoid 146b45052b3f761deb980bcca405f25e543892a93eeIan Romanickgenerate_array_data(void *mem_ctx, enum glsl_base_type base_type, 147b45052b3f761deb980bcca405f25e543892a93eeIan Romanick unsigned columns, unsigned rows, unsigned array_size, 148b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ir_constant *&val) 149b45052b3f761deb980bcca405f25e543892a93eeIan Romanick{ 150b45052b3f761deb980bcca405f25e543892a93eeIan Romanick /* Determine what the type of the generated constant should be. 151b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 152b45052b3f761deb980bcca405f25e543892a93eeIan Romanick const glsl_type *const element_type = 153b45052b3f761deb980bcca405f25e543892a93eeIan Romanick glsl_type::get_instance(base_type, rows, columns); 154b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_FALSE(element_type->is_error()); 155b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 156b45052b3f761deb980bcca405f25e543892a93eeIan Romanick const glsl_type *const array_type = 157b45052b3f761deb980bcca405f25e543892a93eeIan Romanick glsl_type::get_array_instance(element_type, array_size); 158b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_FALSE(array_type->is_error()); 159b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 160b45052b3f761deb980bcca405f25e543892a93eeIan Romanick /* Set the initial data values for the generated constant. 161b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 162b45052b3f761deb980bcca405f25e543892a93eeIan Romanick exec_list values_for_array; 163b45052b3f761deb980bcca405f25e543892a93eeIan Romanick for (unsigned i = 0; i < array_size; i++) { 164b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ir_constant *element; 165b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 166b45052b3f761deb980bcca405f25e543892a93eeIan Romanick generate_data_element(mem_ctx, element_type, element, i); 167b45052b3f761deb980bcca405f25e543892a93eeIan Romanick values_for_array.push_tail(element); 168b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 169b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 170b45052b3f761deb980bcca405f25e543892a93eeIan Romanick val = new(mem_ctx) ir_constant(array_type, &values_for_array); 171b45052b3f761deb980bcca405f25e543892a93eeIan Romanick} 172b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 173b45052b3f761deb980bcca405f25e543892a93eeIan Romanick/** 174b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * Verify that the data stored for the uniform matches the initializer 175b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * 176b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * \param storage Backing storage for the uniform 177b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * \param storage_array_size Array size of the backing storage. This must be 178b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * less than or equal to the array size of the type 179b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * of \c val. If \c val is not an array, this must 180b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * be zero. 181b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * \param val Value of the initializer for the unifrom. 182b45052b3f761deb980bcca405f25e543892a93eeIan Romanick * \param red_zone 183b45052b3f761deb980bcca405f25e543892a93eeIan Romanick */ 184b45052b3f761deb980bcca405f25e543892a93eeIan Romanickvoid 185b45052b3f761deb980bcca405f25e543892a93eeIan Romanickverify_data(gl_constant_value *storage, unsigned storage_array_size, 186b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ir_constant *val, unsigned red_zone_size) 187b45052b3f761deb980bcca405f25e543892a93eeIan Romanick{ 188b45052b3f761deb980bcca405f25e543892a93eeIan Romanick if (val->type->base_type == GLSL_TYPE_ARRAY) { 189b45052b3f761deb980bcca405f25e543892a93eeIan Romanick const glsl_type *const element_type = val->array_elements[0]->type; 190b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 191b45052b3f761deb980bcca405f25e543892a93eeIan Romanick for (unsigned i = 0; i < storage_array_size; i++) { 192b45052b3f761deb980bcca405f25e543892a93eeIan Romanick verify_data(storage + (i * element_type->components()), 0, 193b45052b3f761deb980bcca405f25e543892a93eeIan Romanick val->array_elements[i], 0); 194b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 195b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 196b45052b3f761deb980bcca405f25e543892a93eeIan Romanick const unsigned components = element_type->components(); 197b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 198b45052b3f761deb980bcca405f25e543892a93eeIan Romanick if (red_zone_size > 0) { 199b45052b3f761deb980bcca405f25e543892a93eeIan Romanick EXPECT_TRUE(red_zone_is_intact(storage, 200b45052b3f761deb980bcca405f25e543892a93eeIan Romanick storage_array_size * components, 201b45052b3f761deb980bcca405f25e543892a93eeIan Romanick red_zone_size)); 202b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 203b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } else { 204f2f05e50b1e88b431cb98348bf67e6d5a35d8cf1Paul Berry ASSERT_EQ(0u, storage_array_size); 205b45052b3f761deb980bcca405f25e543892a93eeIan Romanick for (unsigned i = 0; i < val->type->components(); i++) { 206b45052b3f761deb980bcca405f25e543892a93eeIan Romanick switch (val->type->base_type) { 207b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_UINT: 208b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_INT: 209b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_SAMPLER: 210b45052b3f761deb980bcca405f25e543892a93eeIan Romanick EXPECT_EQ(val->value.i[i], storage[i].i); 211b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 212b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_FLOAT: 213b45052b3f761deb980bcca405f25e543892a93eeIan Romanick EXPECT_EQ(val->value.f[i], storage[i].f); 214b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 215b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_BOOL: 216b45052b3f761deb980bcca405f25e543892a93eeIan Romanick EXPECT_EQ(int(val->value.b[i]), storage[i].i); 217b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 218b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_STRUCT: 219b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_ARRAY: 220b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_VOID: 221b45052b3f761deb980bcca405f25e543892a93eeIan Romanick case GLSL_TYPE_ERROR: 222b45052b3f761deb980bcca405f25e543892a93eeIan Romanick ASSERT_TRUE(false); 223b45052b3f761deb980bcca405f25e543892a93eeIan Romanick break; 224b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 225b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 226b45052b3f761deb980bcca405f25e543892a93eeIan Romanick 227b45052b3f761deb980bcca405f25e543892a93eeIan Romanick if (red_zone_size > 0) { 228b45052b3f761deb980bcca405f25e543892a93eeIan Romanick EXPECT_TRUE(red_zone_is_intact(storage, 229b45052b3f761deb980bcca405f25e543892a93eeIan Romanick val->type->components(), 230b45052b3f761deb980bcca405f25e543892a93eeIan Romanick red_zone_size)); 231b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 232b45052b3f761deb980bcca405f25e543892a93eeIan Romanick } 233b45052b3f761deb980bcca405f25e543892a93eeIan Romanick} 234