[Mesa-dev] [PATCH 7/7] tests/glsl: Add test for uniform initialization by the linker
Ian Romanick
idr at freedesktop.org
Thu Apr 12 17:25:43 PDT 2012
From: Ian Romanick <ian.d.romanick at intel.com>
Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
Makefile | 1 +
configure.ac | 1 +
tests/Makefile.am | 2 +-
tests/glsl/Makefile.am | 22 +
tests/glsl/copy_constant_to_storage_tests.cpp | 294 +++++++++++++
tests/glsl/set_uniform_initializer_tests.cpp | 587 +++++++++++++++++++++++++
tests/glsl/uniform_initializer_utils.cpp | 233 ++++++++++
tests/glsl/uniform_initializer_utils.h | 47 ++
8 files changed, 1186 insertions(+), 1 deletions(-)
create mode 100644 tests/glsl/Makefile.am
create mode 100644 tests/glsl/copy_constant_to_storage_tests.cpp
create mode 100644 tests/glsl/set_uniform_initializer_tests.cpp
create mode 100644 tests/glsl/uniform_initializer_utils.cpp
create mode 100644 tests/glsl/uniform_initializer_utils.h
diff --git a/Makefile b/Makefile
index 1368b44..55d4eff 100644
--- a/Makefile
+++ b/Makefile
@@ -192,6 +192,7 @@ EXTRA_FILES = \
aclocal.m4 \
configure \
tests/Makefile.in \
+ tests/glsl/Makefile.in \
tests/glx/Makefile.in \
src/glsl/glsl_parser.cpp \
src/glsl/glsl_parser.h \
diff --git a/configure.ac b/configure.ac
index 65d358e..a40bd2b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1998,6 +1998,7 @@ AC_CONFIG_FILES([configs/autoconf
src/mesa/drivers/dri/radeon/Makefile
src/mesa/drivers/dri/swrast/Makefile
tests/Makefile
+ tests/glsl/Makefile
tests/glx/Makefile])
dnl Replace the configs/current symlink
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4079bb9..eb0f79c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1 +1 @@
-SUBDIRS=glx
+SUBDIRS=glx glsl
diff --git a/tests/glsl/Makefile.am b/tests/glsl/Makefile.am
new file mode 100644
index 0000000..58c7b1a
--- /dev/null
+++ b/tests/glsl/Makefile.am
@@ -0,0 +1,22 @@
+INC = \
+ -I$(top_builddir)/src/gtest/include \
+ -I$(top_builddir)/src/mesa \
+ -I$(top_builddir)/src/mapi \
+ -I$(top_builddir)/src/glsl
+
+AM_CFLAGS = $(INC)
+AM_CXXFLAGS = $(INC)
+
+TESTS = glsl_unittest
+check_PROGRAMS = glsl_unittest
+
+glsl_unittest_SOURCES = \
+ copy_constant_to_storage_tests.cpp \
+ set_uniform_initializer_tests.cpp \
+ uniform_initializer_utils.cpp
+
+glsl_unittest_LDADD = \
+ $(top_builddir)/src/glsl/libglsl.a \
+ $(top_builddir)/src/mesa/libmesa.a \
+ $(top_builddir)/src/gtest/libgtest.la \
+ -lpthread
diff --git a/tests/glsl/copy_constant_to_storage_tests.cpp b/tests/glsl/copy_constant_to_storage_tests.cpp
new file mode 100644
index 0000000..6ab2084
--- /dev/null
+++ b/tests/glsl/copy_constant_to_storage_tests.cpp
@@ -0,0 +1,294 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <gtest/gtest.h>
+#include "main/compiler.h"
+#include "main/mtypes.h"
+#include "main/macros.h"
+#include "ralloc.h"
+#include "uniform_initializer_utils.h"
+
+namespace linker {
+extern void
+copy_constant_to_storage(union gl_constant_value *storage,
+ const ir_constant *val,
+ const enum glsl_base_type base_type,
+ const unsigned int elements);
+}
+
+class copy_constant_to_storage : public ::testing::Test {
+public:
+ void int_test(unsigned rows);
+ void uint_test(unsigned rows);
+ void bool_test(unsigned rows);
+ void sampler_test();
+ void float_test(unsigned columns, unsigned rows);
+
+ virtual void SetUp();
+ virtual void TearDown();
+
+ gl_constant_value storage[17];
+ void *mem_ctx;
+};
+
+void
+copy_constant_to_storage::SetUp()
+{
+ this->mem_ctx = ralloc_context(NULL);
+}
+
+void
+copy_constant_to_storage::TearDown()
+{
+ ralloc_free(this->mem_ctx);
+ this->mem_ctx = NULL;
+}
+
+void
+copy_constant_to_storage::int_test(unsigned rows)
+{
+ ir_constant *val;
+ generate_data(mem_ctx, GLSL_TYPE_INT, 1, rows, val);
+
+ const unsigned red_zone_size = Elements(storage) - val->type->components();
+ fill_storage_array_with_sentinels(storage,
+ val->type->components(),
+ red_zone_size);
+
+ linker::copy_constant_to_storage(storage,
+ val,
+ val->type->base_type,
+ val->type->components());
+
+ verify_data(storage, 0, val, red_zone_size);
+}
+
+void
+copy_constant_to_storage::uint_test(unsigned rows)
+{
+ ir_constant *val;
+ generate_data(mem_ctx, GLSL_TYPE_UINT, 1, rows, val);
+
+ const unsigned red_zone_size = Elements(storage) - val->type->components();
+ fill_storage_array_with_sentinels(storage,
+ val->type->components(),
+ red_zone_size);
+
+ linker::copy_constant_to_storage(storage,
+ val,
+ val->type->base_type,
+ val->type->components());
+
+ verify_data(storage, 0, val, red_zone_size);
+}
+
+void
+copy_constant_to_storage::float_test(unsigned columns, unsigned rows)
+{
+ ir_constant *val;
+ generate_data(mem_ctx, GLSL_TYPE_FLOAT, columns, rows, val);
+
+ const unsigned red_zone_size = Elements(storage) - val->type->components();
+ fill_storage_array_with_sentinels(storage,
+ val->type->components(),
+ red_zone_size);
+
+ linker::copy_constant_to_storage(storage,
+ val,
+ val->type->base_type,
+ val->type->components());
+
+ verify_data(storage, 0, val, red_zone_size);
+}
+
+void
+copy_constant_to_storage::bool_test(unsigned rows)
+{
+ ir_constant *val;
+ generate_data(mem_ctx, GLSL_TYPE_BOOL, 1, rows, val);
+
+ const unsigned red_zone_size = Elements(storage) - val->type->components();
+ fill_storage_array_with_sentinels(storage,
+ val->type->components(),
+ red_zone_size);
+
+ linker::copy_constant_to_storage(storage,
+ val,
+ val->type->base_type,
+ val->type->components());
+
+ verify_data(storage, 0, val, red_zone_size);
+}
+
+/**
+ * The only difference between this test and int_test is that the base type
+ * passed to \c linker::copy_constant_to_storage is hard-coded to \c
+ * GLSL_TYPE_SAMPLER instead of using the base type from the constant.
+ */
+void
+copy_constant_to_storage::sampler_test(void)
+{
+ ir_constant *val;
+ generate_data(mem_ctx, GLSL_TYPE_INT, 1, 1, val);
+
+ const unsigned red_zone_size = Elements(storage) - val->type->components();
+ fill_storage_array_with_sentinels(storage,
+ val->type->components(),
+ red_zone_size);
+
+ linker::copy_constant_to_storage(storage,
+ val,
+ GLSL_TYPE_SAMPLER,
+ val->type->components());
+
+ verify_data(storage, 0, val, red_zone_size);
+}
+
+TEST_F(copy_constant_to_storage, bool_uniform)
+{
+ bool_test(1);
+}
+
+TEST_F(copy_constant_to_storage, bvec2_uniform)
+{
+ bool_test(2);
+}
+
+TEST_F(copy_constant_to_storage, bvec3_uniform)
+{
+ bool_test(3);
+}
+
+TEST_F(copy_constant_to_storage, bvec4_uniform)
+{
+ bool_test(4);
+}
+
+TEST_F(copy_constant_to_storage, int_uniform)
+{
+ int_test(1);
+}
+
+TEST_F(copy_constant_to_storage, ivec2_uniform)
+{
+ int_test(2);
+}
+
+TEST_F(copy_constant_to_storage, ivec3_uniform)
+{
+ int_test(3);
+}
+
+TEST_F(copy_constant_to_storage, ivec4_uniform)
+{
+ int_test(4);
+}
+
+TEST_F(copy_constant_to_storage, uint_uniform)
+{
+ uint_test(1);
+}
+
+TEST_F(copy_constant_to_storage, uvec2_uniform)
+{
+ uint_test(2);
+}
+
+TEST_F(copy_constant_to_storage, uvec3_uniform)
+{
+ uint_test(3);
+}
+
+TEST_F(copy_constant_to_storage, uvec4_uniform)
+{
+ uint_test(4);
+}
+
+TEST_F(copy_constant_to_storage, float_uniform)
+{
+ float_test(1, 1);
+}
+
+TEST_F(copy_constant_to_storage, vec2_uniform)
+{
+ float_test(1, 2);
+}
+
+TEST_F(copy_constant_to_storage, vec3_uniform)
+{
+ float_test(1, 3);
+}
+
+TEST_F(copy_constant_to_storage, vec4_uniform)
+{
+ float_test(1, 4);
+}
+
+TEST_F(copy_constant_to_storage, mat2x2_uniform)
+{
+ float_test(2, 2);
+}
+
+TEST_F(copy_constant_to_storage, mat2x3_uniform)
+{
+ float_test(2, 3);
+}
+
+TEST_F(copy_constant_to_storage, mat2x4_uniform)
+{
+ float_test(2, 4);
+}
+
+TEST_F(copy_constant_to_storage, mat3x2_uniform)
+{
+ float_test(3, 2);
+}
+
+TEST_F(copy_constant_to_storage, mat3x3_uniform)
+{
+ float_test(3, 3);
+}
+
+TEST_F(copy_constant_to_storage, mat3x4_uniform)
+{
+ float_test(3, 4);
+}
+
+TEST_F(copy_constant_to_storage, mat4x2_uniform)
+{
+ float_test(4, 2);
+}
+
+TEST_F(copy_constant_to_storage, mat4x3_uniform)
+{
+ float_test(4, 3);
+}
+
+TEST_F(copy_constant_to_storage, mat4x4_uniform)
+{
+ float_test(4, 4);
+}
+
+TEST_F(copy_constant_to_storage, sampler_uniform)
+{
+ sampler_test();
+}
diff --git a/tests/glsl/set_uniform_initializer_tests.cpp b/tests/glsl/set_uniform_initializer_tests.cpp
new file mode 100644
index 0000000..55831f9
--- /dev/null
+++ b/tests/glsl/set_uniform_initializer_tests.cpp
@@ -0,0 +1,587 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <gtest/gtest.h>
+#include "main/compiler.h"
+#include "main/mtypes.h"
+#include "main/macros.h"
+#include "ralloc.h"
+#include "uniform_initializer_utils.h"
+
+namespace linker {
+extern void
+set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
+ const char *name, const glsl_type *type,
+ ir_constant *val);
+}
+
+class set_uniform_initializer : public ::testing::Test {
+public:
+ virtual void SetUp();
+ virtual void TearDown();
+
+ /**
+ * Index of the uniform to be tested.
+ *
+ * All of the \c set_uniform_initializer tests create several slots for
+ * unifroms. All but one of the slots is fake. This field holds the index
+ * of the slot for the uniform being tested.
+ */
+ unsigned actual_index;
+
+ /**
+ * Name of the uniform to be tested.
+ */
+ const char *name;
+
+ /**
+ * Shader program used in the test.
+ */
+ struct gl_shader_program *prog;
+
+ /**
+ * Ralloc memory context used for all temporary allocations.
+ */
+ void *mem_ctx;
+};
+
+void
+set_uniform_initializer::SetUp()
+{
+ this->mem_ctx = ralloc_context(NULL);
+ this->prog = rzalloc(NULL, struct gl_shader_program);
+
+ /* Set default values used by the test cases.
+ */
+ this->actual_index = 1;
+ this->name = "i";
+}
+
+void
+set_uniform_initializer::TearDown()
+{
+ ralloc_free(this->mem_ctx);
+ this->mem_ctx = NULL;
+
+ ralloc_free(this->prog);
+ this->prog = NULL;
+}
+
+/**
+ * Create some uniform storage for a program.
+ *
+ * \param prog Program to get some storage
+ * \param num_storage Total number of storage slots
+ * \param index_to_set Storage slot that will actually get a value
+ * \param name Name for the actual storage slot
+ * \param type Type for the elements of the actual storage slot
+ * \param array_size Size for the array of the actual storage slot. This
+ * should be zero for non-arrays.
+ */
+static unsigned
+establish_uniform_storage(struct gl_shader_program *prog, unsigned num_storage,
+ unsigned index_to_set, const char *name,
+ const glsl_type *type, unsigned array_size)
+{
+ const unsigned elements = MAX2(1, array_size);
+ const unsigned data_components = elements * type->components();
+ const unsigned total_components = MAX2(17, (data_components
+ + type->components()));
+ const unsigned red_zone_components = total_components - data_components;
+
+ prog->UniformStorage = rzalloc_array(prog, struct gl_uniform_storage,
+ num_storage);
+ prog->NumUserUniformStorage = num_storage;
+
+ prog->UniformStorage[index_to_set].name = (char *) name;
+ prog->UniformStorage[index_to_set].type = type;
+ prog->UniformStorage[index_to_set].array_elements = array_size;
+ prog->UniformStorage[index_to_set].initialized = false;
+ prog->UniformStorage[index_to_set].sampler = ~0;
+ prog->UniformStorage[index_to_set].num_driver_storage = 0;
+ prog->UniformStorage[index_to_set].driver_storage = NULL;
+ prog->UniformStorage[index_to_set].storage =
+ rzalloc_array(prog, union gl_constant_value, total_components);
+
+ fill_storage_array_with_sentinels(prog->UniformStorage[index_to_set].storage,
+ data_components,
+ red_zone_components);
+
+ for (unsigned i = 0; i < num_storage; i++) {
+ if (i == index_to_set)
+ continue;
+
+ prog->UniformStorage[i].name = (char *) "invalid slot";
+ prog->UniformStorage[i].type = glsl_type::void_type;
+ prog->UniformStorage[i].array_elements = 0;
+ prog->UniformStorage[i].initialized = false;
+ prog->UniformStorage[i].sampler = ~0;
+ prog->UniformStorage[i].num_driver_storage = 0;
+ prog->UniformStorage[i].driver_storage = NULL;
+ prog->UniformStorage[i].storage = NULL;
+ }
+
+ return red_zone_components;
+}
+
+/**
+ * Verify that the correct uniform is marked as having been initialized.
+ */
+static void
+verify_initialization(struct gl_shader_program *prog, unsigned actual_index)
+{
+ for (unsigned i = 0; i < prog->NumUserUniformStorage; i++) {
+ if (i == actual_index) {
+ EXPECT_TRUE(prog->UniformStorage[actual_index].initialized);
+ } else {
+ EXPECT_FALSE(prog->UniformStorage[i].initialized);
+ }
+ }
+}
+
+static void
+non_array_test(void *mem_ctx, struct gl_shader_program *prog,
+ unsigned actual_index, const char *name,
+ enum glsl_base_type base_type,
+ unsigned columns, unsigned rows)
+{
+ const glsl_type *const type =
+ glsl_type::get_instance(base_type, rows, columns);
+
+ unsigned red_zone_components =
+ establish_uniform_storage(prog, 3, actual_index, name, type, 0);
+
+ ir_constant *val;
+ generate_data(mem_ctx, base_type, columns, rows, val);
+
+ linker::set_uniform_initializer(mem_ctx, prog, name, type, val);
+
+ verify_initialization(prog, actual_index);
+ verify_data(prog->UniformStorage[actual_index].storage, 0, val,
+ red_zone_components);
+}
+
+TEST_F(set_uniform_initializer, int_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 1);
+}
+
+TEST_F(set_uniform_initializer, ivec2_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 2);
+}
+
+TEST_F(set_uniform_initializer, ivec3_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 3);
+}
+
+TEST_F(set_uniform_initializer, ivec4_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 4);
+}
+
+TEST_F(set_uniform_initializer, uint_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 1);
+}
+
+TEST_F(set_uniform_initializer, uvec2_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 2);
+}
+
+TEST_F(set_uniform_initializer, uvec3_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 3);
+}
+
+TEST_F(set_uniform_initializer, uvec4_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 4);
+}
+
+TEST_F(set_uniform_initializer, bool_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 1);
+}
+
+TEST_F(set_uniform_initializer, bvec2_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 2);
+}
+
+TEST_F(set_uniform_initializer, bvec3_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 3);
+}
+
+TEST_F(set_uniform_initializer, bvec4_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 4);
+}
+
+TEST_F(set_uniform_initializer, float_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 2);
+}
+
+TEST_F(set_uniform_initializer, vec2_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 2);
+}
+
+TEST_F(set_uniform_initializer, vec3_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 3);
+}
+
+TEST_F(set_uniform_initializer, vec4_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 4);
+}
+
+TEST_F(set_uniform_initializer, mat2x2_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 2);
+}
+
+TEST_F(set_uniform_initializer, mat2x3_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 3);
+}
+
+TEST_F(set_uniform_initializer, mat2x4_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 4);
+}
+
+TEST_F(set_uniform_initializer, mat3x2_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 2);
+}
+
+TEST_F(set_uniform_initializer, mat3x3_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 3);
+}
+
+TEST_F(set_uniform_initializer, mat3x4_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 4);
+}
+
+TEST_F(set_uniform_initializer, mat4x2_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 2);
+}
+
+TEST_F(set_uniform_initializer, mat4x3_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 3);
+}
+
+TEST_F(set_uniform_initializer, mat4x4_uniform)
+{
+ non_array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 4);
+}
+
+static void
+array_test(void *mem_ctx, struct gl_shader_program *prog,
+ unsigned actual_index, const char *name,
+ enum glsl_base_type base_type,
+ unsigned columns, unsigned rows, unsigned array_size,
+ unsigned excess_data_size)
+{
+ const glsl_type *const element_type =
+ glsl_type::get_instance(base_type, rows, columns);
+
+ const unsigned red_zone_components =
+ establish_uniform_storage(prog, 3, actual_index, name, element_type,
+ array_size);
+
+ /* The constant value generated may have more array elements than the
+ * uniform that it initializes. In the real compiler and linker this can
+ * happen when a uniform array is compacted because some of the tail
+ * elements are not used. In this case, the type of the uniform will be
+ * modified, but the initializer will not.
+ */
+ ir_constant *val;
+ generate_array_data(mem_ctx, base_type, columns, rows,
+ array_size + excess_data_size, val);
+
+ linker::set_uniform_initializer(mem_ctx, prog, name, element_type, val);
+
+ verify_initialization(prog, actual_index);
+ verify_data(prog->UniformStorage[actual_index].storage, array_size,
+ val, red_zone_components);
+}
+
+TEST_F(set_uniform_initializer, int_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 1, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, ivec2_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 2, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, ivec3_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 3, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, ivec4_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 4, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, uint_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 1, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, uvec2_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 2, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, uvec3_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 3, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, uvec4_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 4, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, bool_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 1, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, bvec2_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 2, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, bvec3_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 3, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, bvec4_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 4, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, float_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 1, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, vec2_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 2, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, vec3_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 3, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, vec4_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 4, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat2x2_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 2, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat2x3_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 3, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat2x4_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 4, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat3x2_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 2, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat3x3_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 3, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat3x4_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 4, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat4x2_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 2, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat4x3_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 3, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, mat4x4_array_uniform)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 4, 4, 0);
+}
+
+TEST_F(set_uniform_initializer, int_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 1, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, ivec2_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 2, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, ivec3_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 3, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, ivec4_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_INT, 1, 4, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, uint_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 1, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, uvec2_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 2, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, uvec3_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 3, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, uvec4_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_UINT, 1, 4, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, bool_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 1, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, bvec2_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 2, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, bvec3_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 3, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, bvec4_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_BOOL, 1, 4, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, float_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 1, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, vec2_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 2, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, vec3_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 3, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, vec4_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 1, 4, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat2x2_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 2, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat2x3_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 3, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat2x4_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 2, 4, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat3x2_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 2, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat3x3_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 3, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat3x4_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 3, 4, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat4x2_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 2, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat4x3_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 3, 4, 5);
+}
+
+TEST_F(set_uniform_initializer, mat4x4_array_uniform_excess_initializer)
+{
+ array_test(mem_ctx, prog, actual_index, name, GLSL_TYPE_FLOAT, 4, 4, 4, 5);
+}
diff --git a/tests/glsl/uniform_initializer_utils.cpp b/tests/glsl/uniform_initializer_utils.cpp
new file mode 100644
index 0000000..4794169
--- /dev/null
+++ b/tests/glsl/uniform_initializer_utils.cpp
@@ -0,0 +1,233 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <gtest/gtest.h>
+#include "main/mtypes.h"
+#include "main/macros.h"
+#include "ralloc.h"
+#include "uniform_initializer_utils.h"
+#include <stdio.h>
+
+void
+fill_storage_array_with_sentinels(gl_constant_value *storage,
+ unsigned data_size,
+ unsigned red_zone_size)
+{
+ for (unsigned i = 0; i < data_size; i++)
+ storage[i].u = 0xDEADBEEF;
+
+ for (unsigned i = 0; i < red_zone_size; i++)
+ storage[data_size + i].u = 0xBADDC0DE;
+}
+
+/**
+ * Verfiy that markers past the end of the real uniform are unmodified
+ */
+static ::testing::AssertionResult
+red_zone_is_intact(gl_constant_value *storage,
+ unsigned data_size,
+ unsigned red_zone_size)
+{
+ for (unsigned i = 0; i < red_zone_size; i++) {
+ const unsigned idx = data_size + i;
+
+ if (storage[idx].u != 0xBADDC0DE)
+ return ::testing::AssertionFailure()
+ << "storage[" << idx << "].u = " << storage[idx].u
+ << ", exepected data values = " << data_size
+ << ", red-zone size = " << red_zone_size;
+ }
+
+ return ::testing::AssertionSuccess();
+}
+
+static const int values[] = {
+ 2, 0, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
+};
+
+/**
+ * Generate a single data element.
+ *
+ * This is by both \c generate_data and \c generate_array_data to create the
+ * data.
+ */
+static void
+generate_data_element(void *mem_ctx, const glsl_type *type,
+ ir_constant *&val, unsigned data_index_base)
+{
+ /* Set the initial data values for the generated constant.
+ */
+ ir_constant_data data;
+ memset(&data, 0, sizeof(data));
+ for (unsigned i = 0; i < type->components(); i++) {
+ const unsigned idx = (i + data_index_base) % Elements(values);
+ switch (type->base_type) {
+ case GLSL_TYPE_UINT:
+ case GLSL_TYPE_INT:
+ case GLSL_TYPE_SAMPLER:
+ data.i[i] = values[idx];
+ break;
+ case GLSL_TYPE_FLOAT:
+ data.f[i] = float(values[idx]);
+ break;
+ case GLSL_TYPE_BOOL:
+ data.b[i] = bool(values[idx]);
+ break;
+ case GLSL_TYPE_STRUCT:
+ case GLSL_TYPE_ARRAY:
+ case GLSL_TYPE_VOID:
+ case GLSL_TYPE_ERROR:
+ ASSERT_TRUE(false);
+ break;
+ }
+ }
+
+ /* Generate and verify the constant.
+ */
+ val = new(mem_ctx) ir_constant(type, &data);
+
+ for (unsigned i = 0; i < type->components(); i++) {
+ switch (type->base_type) {
+ case GLSL_TYPE_UINT:
+ case GLSL_TYPE_INT:
+ case GLSL_TYPE_SAMPLER:
+ ASSERT_EQ(data.i[i], val->value.i[i]);
+ break;
+ case GLSL_TYPE_FLOAT:
+ ASSERT_EQ(data.f[i], val->value.f[i]);
+ break;
+ case GLSL_TYPE_BOOL:
+ ASSERT_EQ(data.b[i], val->value.b[i]);
+ break;
+ case GLSL_TYPE_STRUCT:
+ case GLSL_TYPE_ARRAY:
+ case GLSL_TYPE_VOID:
+ case GLSL_TYPE_ERROR:
+ ASSERT_TRUE(false);
+ break;
+ }
+ }
+}
+
+void
+generate_data(void *mem_ctx, enum glsl_base_type base_type,
+ unsigned columns, unsigned rows,
+ ir_constant *&val)
+{
+ /* Determine what the type of the generated constant should be.
+ */
+ const glsl_type *const type =
+ glsl_type::get_instance(base_type, rows, columns);
+ ASSERT_FALSE(type->is_error());
+
+ generate_data_element(mem_ctx, type, val, 0);
+}
+
+void
+generate_array_data(void *mem_ctx, enum glsl_base_type base_type,
+ unsigned columns, unsigned rows, unsigned array_size,
+ ir_constant *&val)
+{
+ /* Determine what the type of the generated constant should be.
+ */
+ const glsl_type *const element_type =
+ glsl_type::get_instance(base_type, rows, columns);
+ ASSERT_FALSE(element_type->is_error());
+
+ const glsl_type *const array_type =
+ glsl_type::get_array_instance(element_type, array_size);
+ ASSERT_FALSE(array_type->is_error());
+
+ /* Set the initial data values for the generated constant.
+ */
+ exec_list values_for_array;
+ for (unsigned i = 0; i < array_size; i++) {
+ ir_constant *element;
+
+ generate_data_element(mem_ctx, element_type, element, i);
+ values_for_array.push_tail(element);
+ }
+
+ val = new(mem_ctx) ir_constant(array_type, &values_for_array);
+}
+
+/**
+ * Verify that the data stored for the uniform matches the initializer
+ *
+ * \param storage Backing storage for the uniform
+ * \param storage_array_size Array size of the backing storage. This must be
+ * less than or equal to the array size of the type
+ * of \c val. If \c val is not an array, this must
+ * be zero.
+ * \param val Value of the initializer for the unifrom.
+ * \param red_zone
+ */
+void
+verify_data(gl_constant_value *storage, unsigned storage_array_size,
+ ir_constant *val, unsigned red_zone_size)
+{
+ if (val->type->base_type == GLSL_TYPE_ARRAY) {
+ const glsl_type *const element_type = val->array_elements[0]->type;
+
+ for (unsigned i = 0; i < storage_array_size; i++) {
+ verify_data(storage + (i * element_type->components()), 0,
+ val->array_elements[i], 0);
+ }
+
+ const unsigned components = element_type->components();
+
+ if (red_zone_size > 0) {
+ EXPECT_TRUE(red_zone_is_intact(storage,
+ storage_array_size * components,
+ red_zone_size));
+ }
+ } else {
+ ASSERT_EQ(0, storage_array_size);
+ for (unsigned i = 0; i < val->type->components(); i++) {
+ switch (val->type->base_type) {
+ case GLSL_TYPE_UINT:
+ case GLSL_TYPE_INT:
+ case GLSL_TYPE_SAMPLER:
+ EXPECT_EQ(val->value.i[i], storage[i].i);
+ break;
+ case GLSL_TYPE_FLOAT:
+ EXPECT_EQ(val->value.f[i], storage[i].f);
+ break;
+ case GLSL_TYPE_BOOL:
+ EXPECT_EQ(int(val->value.b[i]), storage[i].i);
+ break;
+ case GLSL_TYPE_STRUCT:
+ case GLSL_TYPE_ARRAY:
+ case GLSL_TYPE_VOID:
+ case GLSL_TYPE_ERROR:
+ ASSERT_TRUE(false);
+ break;
+ }
+ }
+
+ if (red_zone_size > 0) {
+ EXPECT_TRUE(red_zone_is_intact(storage,
+ val->type->components(),
+ red_zone_size));
+ }
+ }
+}
diff --git a/tests/glsl/uniform_initializer_utils.h b/tests/glsl/uniform_initializer_utils.h
new file mode 100644
index 0000000..f8c06d2
--- /dev/null
+++ b/tests/glsl/uniform_initializer_utils.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "program/prog_parameter.h"
+#include "ir.h"
+#include "ir_uniform.h"
+
+extern void
+fill_storage_array_with_sentinels(gl_constant_value *storage,
+ unsigned data_size,
+ unsigned red_zone_size);
+
+extern void
+generate_data(void *mem_ctx, enum glsl_base_type base_type,
+ unsigned columns, unsigned rows,
+ ir_constant *&val);
+
+extern void
+generate_array_data(void *mem_ctx, enum glsl_base_type base_type,
+ unsigned columns, unsigned rows, unsigned array_size,
+ ir_constant *&val);
+
+extern void
+verify_data(gl_constant_value *storage, unsigned storage_array_size,
+ ir_constant *val, unsigned red_zone_size);
--
1.7.6.5
More information about the mesa-dev
mailing list