[Piglit] [PATCH 05/10] GL_ARB_ubo: Add new test for more thorough std140 testing.

Eric Anholt eric at anholt.net
Thu Aug 9 09:22:11 PDT 2012


The std140 example in the spec provides quite a bit of coverage,
particularly for structs and arrays, but it failed to catch a bug with
mat2 alignment in Mesa.
---
 tests/all.tests                                    |    1 +
 .../arb_uniform_buffer_object/CMakeLists.gl.txt    |    1 +
 .../layout-std140-base-size-and-alignment.c        |  142 ++++++++++++++++++++
 3 files changed, 144 insertions(+)
 create mode 100644 tests/spec/arb_uniform_buffer_object/layout-std140-base-size-and-alignment.c

diff --git a/tests/all.tests b/tests/all.tests
index 1fb6038..788dd33 100644
--- a/tests/all.tests
+++ b/tests/all.tests
@@ -1912,6 +1912,7 @@ arb_uniform_buffer_object['getuniformblockindex'] = concurrent_test('arb_uniform
 arb_uniform_buffer_object['getuniformindices'] = concurrent_test('arb_uniform_buffer_object-getuniformindices')
 arb_uniform_buffer_object['getuniformlocation'] = concurrent_test('arb_uniform_buffer_object-getuniformlocation')
 arb_uniform_buffer_object['layout-std140'] = concurrent_test('arb_uniform_buffer_object-layout-std140')
+arb_uniform_buffer_object['layout-std140-base-size-and-alignment'] = concurrent_test('arb_uniform_buffer_object-layout-std140-base-size-and-alignment')
 arb_uniform_buffer_object['link-mismatch-blocks'] = concurrent_test('arb_uniform_buffer_object-link-mismatch-blocks')
 arb_uniform_buffer_object['maxblocks'] = concurrent_test('arb_uniform_buffer_object-maxblocks')
 arb_uniform_buffer_object['minmax'] = concurrent_test('arb_uniform_buffer_object-minmax')
diff --git a/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt b/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
index 4f772bf..d989022 100644
--- a/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
+++ b/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
@@ -23,6 +23,7 @@ add_executable (arb_uniform_buffer_object-getuniformblockindex getuniformblockin
 add_executable (arb_uniform_buffer_object-getuniformindices getuniformindices.c)
 add_executable (arb_uniform_buffer_object-getuniformlocation getuniformlocation.c)
 add_executable (arb_uniform_buffer_object-layout-std140 layout-std140.c)
+add_executable (arb_uniform_buffer_object-layout-std140-base-size-and-alignment layout-std140-base-size-and-alignment.c uniform-types.c)
 add_executable (arb_uniform_buffer_object-link-mismatch-blocks link-mismatch-blocks.c)
 add_executable (arb_uniform_buffer_object-maxblocks maxblocks.c)
 add_executable (arb_uniform_buffer_object-minmax minmax.c)
diff --git a/tests/spec/arb_uniform_buffer_object/layout-std140-base-size-and-alignment.c b/tests/spec/arb_uniform_buffer_object/layout-std140-base-size-and-alignment.c
new file mode 100644
index 0000000..5841c03
--- /dev/null
+++ b/tests/spec/arb_uniform_buffer_object/layout-std140-base-size-and-alignment.c
@@ -0,0 +1,142 @@
+/*
+ * 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.
+ */
+
+/** @file layout-std140-base-size-and-alignment.c
+ *
+ * Tests that glGetActiveUniformsiv() returns the correct offset for
+ * any basic type valid in std140, and for a float just following
+ * that, thus testing the size and base alignment for them.
+ */
+
+#define _GNU_SOURCE
+#include "piglit-util-gl-common.h"
+#include "uniform-types.h"
+
+PIGLIT_GL_TEST_MAIN(
+    10 /*window_width*/,
+    10 /*window_height*/,
+    GLUT_DOUBLE | GLUT_RGB | GLUT_ALPHA)
+
+static int
+align(int v, int a)
+{
+	return (v + a - 1) & ~(a - 1);
+}
+
+static bool
+test_format(const struct uniform_type *type, bool row_major)
+{
+	/* Using 140 to get unsigned ints. */
+	const char *fs_template =
+		"#version 140\n"
+		"layout(std140) uniform ubo {\n"
+		"	float pad;\n"
+		"	%s %s u;\n"
+		"	float size_test;\n"
+		"};\n"
+		"\n"
+		"void main() {\n"
+		"	gl_FragColor = vec4(pad);\n"
+		"}\n";
+	char *fs_source;
+	GLuint fs, prog;
+	const char *uniform_names[] = { "u", "size_test" };
+	GLuint uniform_indices[2];
+	GLint offsets[2];
+	int offset, size, expected_offset;
+	const struct uniform_type *transposed_type;
+	bool pass;
+
+	if (row_major)
+		transposed_type = get_transposed_type(type);
+	else
+		transposed_type = type;
+
+	asprintf(&fs_source, fs_template,
+		 row_major ? "layout(row_major) " : "",
+		 type->type);
+	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_source);
+	prog = piglit_link_simple_program(0, fs);
+	if (!fs || !prog) {
+		fprintf(stderr, "Failed to compile shader:\n%s", fs_source);
+		piglit_report_result(PIGLIT_FAIL);
+	}
+	free(fs_source);
+
+	glGetUniformIndices(prog, 2, uniform_names, uniform_indices);
+	glGetActiveUniformsiv(prog, 2, uniform_indices,
+			      GL_UNIFORM_OFFSET, offsets);
+
+	glDeleteShader(fs);
+	glDeleteProgram(prog);
+
+	offset = offsets[0];
+	size = offsets[1] - offsets[0];
+
+	/* "pad" at the start of the UBO is a float, so our test
+	 * uniform would start at byte 4 if not for alignment.
+	 */
+	expected_offset = 4;
+	expected_offset = align(expected_offset, transposed_type->alignment);
+
+	pass = (offset == expected_offset &&
+		size == transposed_type->size);
+
+	printf("%-10s %10s %8d %-16d %8d %-16d%s\n",
+	       type->type,
+	       row_major ? "y" : "n",
+	       offset,
+	       expected_offset,
+	       size,
+	       transposed_type->size,
+	       pass ? "" : " FAIL");
+
+	return pass;
+}
+
+void
+piglit_init(int argc, char **argv)
+{
+	bool pass = true;
+	unsigned int i;
+
+	piglit_require_extension("GL_ARB_uniform_buffer_object");
+	piglit_require_GLSL_version(140);
+
+	printf("%-10s %10s %8s %16s %8s %-16s\n",
+	       "type", "row_major",
+	       "offset", "expected offset", "size", "expected size");
+
+	for (i = 0; uniform_types[i].type; i++) {
+		pass = test_format(&uniform_types[i], false) && pass;
+		pass = test_format(&uniform_types[i], true) && pass;
+	}
+
+	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
+}
+
+enum piglit_result piglit_display(void)
+{
+	/* UNREACHED */
+	return PIGLIT_FAIL;
+}
-- 
1.7.10.4



More information about the Piglit mailing list