[Piglit] [PATCH] arb_uniform_buffer_object: add new test exercising an array in a UBO

Brian Paul brianp at vmware.com
Thu Feb 25 14:54:10 UTC 2016


This test puts the pos, size, rotation and color info for four objects
in an array which is stored in a UBO.  For each drawing command, index
into the array to get the object parameters.
---
 tests/all.py                                       |   2 +
 .../arb_uniform_buffer_object/CMakeLists.gl.txt    |   1 +
 .../arb_uniform_buffer_object/rendering-array.c    | 209 +++++++++++++++++++++
 3 files changed, 212 insertions(+)
 create mode 100644 tests/spec/arb_uniform_buffer_object/rendering-array.c

diff --git a/tests/all.py b/tests/all.py
index 7f02653..f854604 100644
--- a/tests/all.py
+++ b/tests/all.py
@@ -3614,6 +3614,8 @@ with profile.group_manager(
       'referenced-by-shader')
     g(['arb_uniform_buffer_object-rendering'], 'rendering')
     g(['arb_uniform_buffer_object-rendering', 'offset'], 'rendering-offset')
+    g(['arb_uniform_buffer_object-rendering-array'], 'rendering-array')
+    g(['arb_uniform_buffer_object-rendering-array', 'offset'], 'rendering-array-offset')
     g(['arb_uniform_buffer_object-rendering-dsa'], 'rendering-dsa')
     g(['arb_uniform_buffer_object-rendering-dsa', 'offset'], 'rendering-dsa-offset')
     g(['arb_uniform_buffer_object-row-major'], 'row-major')
diff --git a/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt b/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
index 541a100..6bdcde8 100644
--- a/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
+++ b/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
@@ -38,6 +38,7 @@ piglit_add_executable (arb_uniform_buffer_object-negative-getactiveuniformblocki
 piglit_add_executable (arb_uniform_buffer_object-negative-getactiveuniformsiv negative-getactiveuniformsiv.c)
 piglit_add_executable (arb_uniform_buffer_object-referenced-by-shader referenced-by-shader.c)
 piglit_add_executable (arb_uniform_buffer_object-rendering rendering.c)
+piglit_add_executable (arb_uniform_buffer_object-rendering-array rendering-array.c)
 piglit_add_executable (arb_uniform_buffer_object-rendering-dsa rendering-dsa.c)
 piglit_add_executable (arb_uniform_buffer_object-row-major row-major.c)
 piglit_add_executable (arb_uniform_buffer_object-uniformblockbinding uniformblockbinding.c)
diff --git a/tests/spec/arb_uniform_buffer_object/rendering-array.c b/tests/spec/arb_uniform_buffer_object/rendering-array.c
new file mode 100644
index 0000000..1535e92
--- /dev/null
+++ b/tests/spec/arb_uniform_buffer_object/rendering-array.c
@@ -0,0 +1,209 @@
+/*
+ * Copyright (c) 2016 VMware, Inc.
+ *
+ * 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 rendering-array.c
+ *
+ * Test rendering with a UBO containing an array of structs.
+ * We draw four squares with different positions, sizes, rotations and colors
+ * where those parameters come from an array in a UBO.  Each draw command
+ * indexes into a different element of that array.
+ */
+
+#include "piglit-util-gl.h"
+
+PIGLIT_GL_TEST_CONFIG_BEGIN
+	config.supports_gl_compat_version = 20;
+	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
+PIGLIT_GL_TEST_CONFIG_END
+
+
+static const char vert_shader_text[] =
+	"#extension GL_ARB_uniform_buffer_object : require\n"
+	"\n"
+	"layout(std140) uniform;\n"
+	"uniform ub_info { \n"
+	"   struct { \n"
+	"      vec2 pos; \n"
+	"      float size; \n"
+	"      float rotation; \n"
+	"      vec4 color; \n"
+	"   } info [4];\n"
+	"};\n"
+	"\n"
+	"uniform int j; \n"
+	"varying vec4 color; \n"
+	"\n"
+	"void main()\n"
+	"{\n"
+	"   int i; \n"
+	"   mat2 m;\n"
+	"   for (i = 0; i < 4; i++) { \n"
+	"      if (i == j) { \n"
+	"         m[0][0] = m[1][1] = cos(info[i].rotation); \n"
+	"         m[0][1] = sin(info[i].rotation); \n"
+	"         m[1][0] = -m[0][1]; \n"
+	"         gl_Position.xy = m * gl_Vertex.xy * vec2(info[i].size) + info[i].pos;\n"
+	"         gl_Position.zw = vec2(0, 1);\n"
+	"         color = info[i].color; \n"
+	"      } \n"
+	"   } \n"
+	"}\n";
+
+static const char frag_shader_text[] =
+	"#extension GL_ARB_uniform_buffer_object : require\n"
+	"\n"
+	"varying vec4 color; \n"
+	"\n"
+	"layout(std140) uniform;\n"
+	"\n"
+	"void main()\n"
+	"{\n"
+	"   gl_FragColor = color;\n"
+	"}\n";
+
+#define NUM_SQUARES 4
+
+static GLuint prog;
+static GLuint ubo_buffer;
+static GLint alignment;
+static bool test_buffer_offset = false;
+static int uniform_j;
+
+struct object_info {
+	float pos[2];
+	float size, rotation;
+	float color[4];
+};
+
+/* This data are copied into the UBO */
+static const struct object_info obj_info[NUM_SQUARES] = {
+	{ {-0.5, -0.5}, 0.1, 0.0, {1.0, 0.0, 0.0, 1.0} },
+	{ { 0.5, -0.5}, 0.2, 0.1, {0.0, 1.0, 0.0, 1.0} },
+	{ {-0.5,  0.5}, 0.3, 0.2, {0.0, 0.0, 1.0, 1.0} },
+	{ { 0.5,  0.5}, 0.4, 0.3, {1.0, 1.0, 1.0, 1.0} }
+};
+
+
+static void
+setup_ubos(void)
+{
+	static const char *ubo_name = "ub_info";
+	GLint ubo_index, ubo_size;
+
+	glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment);
+	printf("GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT = %d\n", alignment);
+
+	if (test_buffer_offset) {
+		printf("Testing buffer offset %d\n", alignment);
+	}
+	else {
+		/* we use alignment as the offset */
+		alignment = 0;
+	}
+
+	glGenBuffers(1, &ubo_buffer);
+
+	/* query UBO index */
+	ubo_index = glGetUniformBlockIndex(prog, ubo_name);
+
+	/* query UBO size */
+	glGetActiveUniformBlockiv(prog, ubo_index,
+				  GL_UNIFORM_BLOCK_DATA_SIZE, &ubo_size);
+
+	printf("UBO %s: index = %d, size = %d\n",
+	       ubo_name, ubo_index, ubo_size);
+
+	assert(ubo_size == sizeof(obj_info));
+
+	/* Allocate UBO and put object info into it */
+	glBindBuffer(GL_UNIFORM_BUFFER, ubo_buffer);
+	glBufferData(GL_UNIFORM_BUFFER, alignment + sizeof(obj_info),
+		     NULL, GL_STATIC_DRAW);
+	glBufferSubData(GL_UNIFORM_BUFFER, alignment,
+			sizeof(obj_info), obj_info);
+
+	/* Attach UBO */
+	glBindBufferRange(GL_UNIFORM_BUFFER, 0, ubo_buffer,
+			  alignment,  /* offset */
+			  ubo_size);
+	glUniformBlockBinding(prog, ubo_index, 0);
+
+	if (!piglit_check_gl_error(GL_NO_ERROR))
+		piglit_report_result(PIGLIT_FAIL);
+}
+
+
+void
+piglit_init(int argc, char **argv)
+{
+	piglit_require_extension("GL_ARB_uniform_buffer_object");
+
+	if (argc > 1 && strcmp(argv[1], "offset") == 0) {
+		test_buffer_offset = true;
+	}
+
+	prog = piglit_build_simple_program(vert_shader_text, frag_shader_text);
+	assert(prog);
+	glUseProgram(prog);
+
+	uniform_j = glGetUniformLocation(prog, "j");
+
+	setup_ubos();
+
+	glClearColor(0.2, 0.2, 0.2, 0.2);
+}
+
+
+enum piglit_result
+piglit_display(void)
+{
+	bool pass = true;
+	int x0 = piglit_width / 4;
+	int x1 = piglit_width * 3 / 4;
+	int y0 = piglit_height / 4;
+	int y1 = piglit_height * 3 / 4;
+	int i;
+
+	glViewport(0, 0, piglit_width, piglit_height);
+
+	glClear(GL_COLOR_BUFFER_BIT);
+
+	for (i = 0; i < NUM_SQUARES; i++) {
+		/* Take object parameters from array position [i] */
+		glUniform1i(uniform_j, i);
+
+		if (!piglit_check_gl_error(GL_NO_ERROR))
+			return PIGLIT_FAIL;
+
+		piglit_draw_rect(-1, -1, 2, 2);
+	}
+
+	pass = piglit_probe_pixel_rgba(x0, y0, obj_info[0].color) && pass;
+	pass = piglit_probe_pixel_rgba(x1, y0, obj_info[1].color) && pass;
+	pass = piglit_probe_pixel_rgba(x0, y1, obj_info[2].color) && pass;
+	pass = piglit_probe_pixel_rgba(x1, y1, obj_info[3].color) && pass;
+
+	piglit_present_results();
+
+	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
+}
-- 
1.9.1



More information about the Piglit mailing list