Home | History | Annotate | Download | only in tests
      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 "ralloc.h"
     27 #include "uniform_initializer_utils.h"
     28 #include <stdio.h>
     29 
     30 void
     31 fill_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  */
     45 static ::testing::AssertionResult
     46 red_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 
     63 static 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  */
     73 static void
     74 generate_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) % Elements(values);
     83       switch (type->base_type) {
     84       case GLSL_TYPE_UINT:
     85       case GLSL_TYPE_INT:
     86       case GLSL_TYPE_SAMPLER:
     87 	 data.i[i] = values[idx];
     88 	 break;
     89       case GLSL_TYPE_FLOAT:
     90 	 data.f[i] = float(values[idx]);
     91 	 break;
     92       case GLSL_TYPE_BOOL:
     93 	 data.b[i] = bool(values[idx]);
     94 	 break;
     95       case GLSL_TYPE_STRUCT:
     96       case GLSL_TYPE_ARRAY:
     97       case GLSL_TYPE_VOID:
     98       case GLSL_TYPE_ERROR:
     99 	 ASSERT_TRUE(false);
    100 	 break;
    101       }
    102    }
    103 
    104    /* Generate and verify the constant.
    105     */
    106    val = new(mem_ctx) ir_constant(type, &data);
    107 
    108    for (unsigned i = 0; i < type->components(); i++) {
    109       switch (type->base_type) {
    110       case GLSL_TYPE_UINT:
    111       case GLSL_TYPE_INT:
    112       case GLSL_TYPE_SAMPLER:
    113 	 ASSERT_EQ(data.i[i], val->value.i[i]);
    114 	 break;
    115       case GLSL_TYPE_FLOAT:
    116 	 ASSERT_EQ(data.f[i], val->value.f[i]);
    117 	 break;
    118       case GLSL_TYPE_BOOL:
    119 	 ASSERT_EQ(data.b[i], val->value.b[i]);
    120 	 break;
    121       case GLSL_TYPE_STRUCT:
    122       case GLSL_TYPE_ARRAY:
    123       case GLSL_TYPE_VOID:
    124       case GLSL_TYPE_ERROR:
    125 	 ASSERT_TRUE(false);
    126 	 break;
    127       }
    128    }
    129 }
    130 
    131 void
    132 generate_data(void *mem_ctx, enum glsl_base_type base_type,
    133 	      unsigned columns, unsigned rows,
    134 	      ir_constant *&val)
    135 {
    136    /* Determine what the type of the generated constant should be.
    137     */
    138    const glsl_type *const type =
    139       glsl_type::get_instance(base_type, rows, columns);
    140    ASSERT_FALSE(type->is_error());
    141 
    142    generate_data_element(mem_ctx, type, val, 0);
    143 }
    144 
    145 void
    146 generate_array_data(void *mem_ctx, enum glsl_base_type base_type,
    147 		    unsigned columns, unsigned rows, unsigned array_size,
    148 		    ir_constant *&val)
    149 {
    150    /* Determine what the type of the generated constant should be.
    151     */
    152    const glsl_type *const element_type =
    153       glsl_type::get_instance(base_type, rows, columns);
    154    ASSERT_FALSE(element_type->is_error());
    155 
    156    const glsl_type *const array_type =
    157       glsl_type::get_array_instance(element_type, array_size);
    158    ASSERT_FALSE(array_type->is_error());
    159 
    160    /* Set the initial data values for the generated constant.
    161     */
    162    exec_list values_for_array;
    163    for (unsigned i = 0; i < array_size; i++) {
    164       ir_constant *element;
    165 
    166       generate_data_element(mem_ctx, element_type, element, i);
    167       values_for_array.push_tail(element);
    168    }
    169 
    170    val = new(mem_ctx) ir_constant(array_type, &values_for_array);
    171 }
    172 
    173 /**
    174  * Verify that the data stored for the uniform matches the initializer
    175  *
    176  * \param storage              Backing storage for the uniform
    177  * \param storage_array_size  Array size of the backing storage.  This must be
    178  *                            less than or equal to the array size of the type
    179  *                            of \c val.  If \c val is not an array, this must
    180  *                            be zero.
    181  * \param val                 Value of the initializer for the unifrom.
    182  * \param red_zone
    183  */
    184 void
    185 verify_data(gl_constant_value *storage, unsigned storage_array_size,
    186 	    ir_constant *val, unsigned red_zone_size)
    187 {
    188    if (val->type->base_type == GLSL_TYPE_ARRAY) {
    189       const glsl_type *const element_type = val->array_elements[0]->type;
    190 
    191       for (unsigned i = 0; i < storage_array_size; i++) {
    192 	 verify_data(storage + (i * element_type->components()), 0,
    193 		     val->array_elements[i], 0);
    194       }
    195 
    196       const unsigned components = element_type->components();
    197 
    198       if (red_zone_size > 0) {
    199 	 EXPECT_TRUE(red_zone_is_intact(storage,
    200 					storage_array_size * components,
    201 					red_zone_size));
    202       }
    203    } else {
    204       ASSERT_EQ(0u, storage_array_size);
    205       for (unsigned i = 0; i < val->type->components(); i++) {
    206 	 switch (val->type->base_type) {
    207 	 case GLSL_TYPE_UINT:
    208 	 case GLSL_TYPE_INT:
    209 	 case GLSL_TYPE_SAMPLER:
    210 	    EXPECT_EQ(val->value.i[i], storage[i].i);
    211 	    break;
    212 	 case GLSL_TYPE_FLOAT:
    213 	    EXPECT_EQ(val->value.f[i], storage[i].f);
    214 	    break;
    215 	 case GLSL_TYPE_BOOL:
    216 	    EXPECT_EQ(int(val->value.b[i]), storage[i].i);
    217 	    break;
    218 	 case GLSL_TYPE_STRUCT:
    219 	 case GLSL_TYPE_ARRAY:
    220 	 case GLSL_TYPE_VOID:
    221 	 case GLSL_TYPE_ERROR:
    222 	    ASSERT_TRUE(false);
    223 	    break;
    224 	 }
    225       }
    226 
    227       if (red_zone_size > 0) {
    228 	 EXPECT_TRUE(red_zone_is_intact(storage,
    229 					val->type->components(),
    230 					red_zone_size));
    231       }
    232    }
    233 }
    234