[Mesa-dev] [PATCH 4/4] ARB_Uniform_Buffer_Object: Draw several ubos test

Vincent Lejeune vljn at ovi.com
Sun Dec 25 09:35:50 PST 2011


---
 tests/all.tests                                    |    1 +
 .../arb_uniform_buffer_object/CMakeLists.gl.txt    |    1 +
 .../draw_several_ubo_test.c                        |  291 ++++++++++++++++++++
 3 files changed, 293 insertions(+), 0 deletions(-)
 create mode 100644 tests/spec/arb_uniform_buffer_object/draw_several_ubo_test.c

diff --git a/tests/all.tests b/tests/all.tests
index 8ad3d89..2cf5e7d 100644
--- a/tests/all.tests
+++ b/tests/all.tests
@@ -1392,6 +1392,7 @@ spec['ARB_uniform_buffer_object'] = arb_uniform_buffer_object
 arb_uniform_buffer_object['standard_layout'] = concurrent_test('arb_uniform_buffer_object_standard_layout')
 arb_uniform_buffer_object['types'] = concurrent_test('arb_uniform_buffer_object_types')
 arb_uniform_buffer_object['draw_test'] = PlainExecTest('arb_uniform_buffer_object_draw_test')
+arb_uniform_buffer_object['draw_several_ubo_test'] = PlainExecTest('arb_uniform_buffer_object_draw_several_ubo_test')
 
 ati_draw_buffers = Group()
 spec['ATI_draw_buffers'] = ati_draw_buffers
diff --git a/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt b/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
index f2bf52b..54d35e9 100644
--- a/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
+++ b/tests/spec/arb_uniform_buffer_object/CMakeLists.gl.txt
@@ -15,5 +15,6 @@ link_libraries (
 add_executable (arb_uniform_buffer_object_standard_layout standard-layout.c)
 add_executable (arb_uniform_buffer_object_types types.c)
 add_executable (arb_uniform_buffer_object_draw_test draw_test.c)
+add_executable (arb_uniform_buffer_object_draw_several_ubo_test draw_several_ubo_test.c)
 
 # vim: ft=cmake:
diff --git a/tests/spec/arb_uniform_buffer_object/draw_several_ubo_test.c b/tests/spec/arb_uniform_buffer_object/draw_several_ubo_test.c
new file mode 100644
index 0000000..8b11bbb
--- /dev/null
+++ b/tests/spec/arb_uniform_buffer_object/draw_several_ubo_test.c
@@ -0,0 +1,291 @@
+/*
+ * Copyright © 2011 Marek Olšák <maraeo at gmail.com>
+ * Copyright © 2011 Vincent Lejeune
+ *
+ * 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.
+ */
+
+/**
+ * ARB_uniform_buffer_object test
+ *
+
+ */
+
+#include "piglit-util.h"
+
+int piglit_width = 64;
+int piglit_height = 64;
+int piglit_window_mode = GLUT_DOUBLE | GLUT_RGB | GLUT_ALPHA;
+
+static const char vert_shader_text[] =
+	"#version 130\n"
+	"in vec2 VertexPosition;\n"
+	"invariant gl_Position;\n"
+	"\n"
+	"void main ()\n"
+	"{\n"
+	"   gl_Position = vec4 (VertexPosition, 0.0, 1.0);\n"
+	"}\n"
+;
+
+static const char frag_shader_text[] =
+	"#version 130\n"
+	"#extension GL_ARB_uniform_buffer_object : enable\n "
+	"uniform mode_and_offset {\n"
+	"   int mode1;\n"
+	"   int mode2;\n"
+	"   int index1;\n"
+	"   int index2;\n"
+	"};\n"
+	"\n"
+	"struct colors {\n"
+	"	float first[3];\n"
+	"  float second[3];\n"
+	"  float third[3];\n"
+	"};"
+	"uniform color_information {\n"
+	"   colors colors_array[3];\n"
+	"};\n"
+	"out vec3 fragColor;\n"
+	"\n"
+	"void main()\n"
+	"{\n"
+	"   colors tmp1;"
+	"   colors tmp2;"
+	"   if (mode1 == 0)\n"
+	"   {\n"
+	"      tmp1 = colors(colors_array[index1].first, colors_array[index2].second, colors_array[index1].third);"
+	"      tmp2 = colors_array[index2];"
+	"   }\n"
+	"   else\n"
+	"   {\n"
+	"      tmp1 = colors_array[index1];"
+	"      tmp2 = colors(colors_array[index2].first, colors_array[index1].second, colors_array[index2].third);"
+	"   }\n"
+	"   if (mode2 == 0)"
+   "   {\n"
+	"      float f1 = tmp1.first[0] + tmp2.first[0];"
+	"      float f2 = tmp1.first[1] + tmp2.first[1];"
+	"      float f3 = tmp1.first[2] + tmp2.first[2];"
+	"      fragColor = vec3(f1,f2,f3);"
+	"   }\n"
+	"   else\n"
+	"   {\n"
+	"      float f1 = tmp1.first[0] + tmp2.first[0];"
+	"      float f2 = tmp1.second[1] + tmp2.second[1];"
+	"      float f3 = tmp1.third[2] + tmp2.third[2];"
+	"      fragColor = vec3(f1,f2,f3);"
+	"   }\n"
+	"}\n"
+;
+
+GLuint prog;
+GLuint mode_and_offset_buf;
+GLuint color_information_buf;
+GLuint vertex_buf;
+GLuint positionAttrib;
+
+
+static const float verts[] = {
+	-0.25, -0.25,
+	-0.25, 0.25,
+	0.25, 0.25,
+	0.25, -0.25
+};
+
+const char* mode_and_offset_names[] = {"mode1","mode2","index1","index2"};
+const char* color_information_names[] = {
+	"colors_array[0].first", "colors_array[0].second", "colors_array[0].third",
+	"colors_array[1].first", "colors_array[1].second", "colors_array[1].third",
+	"colors_array[2].first", "colors_array[2].second", "colors_array[2].third",
+	};
+
+static void
+set_uniform_int(const char* name, const int value)
+{
+	GLuint ubo_var_id; 
+	GLint ubo_var_offset; 
+	glGetUniformIndices(prog, 1, &name, &ubo_var_id); 
+	glGetActiveUniformsiv(prog, 1, &ubo_var_id, GL_UNIFORM_OFFSET, &ubo_var_offset); 
+	glBufferSubData(GL_UNIFORM_BUFFER, ubo_var_offset, sizeof(GLint), &value ); 
+	assert(glGetError() == 0);
+}
+
+static void
+set_uniform_array(const char* name,const float value[]) 
+{ 
+	GLuint ubo_var_id; 
+	GLint ubo_var_offset; 
+	GLint ubo_var_size; 
+	GLint ubo_var_stride; 
+	unsigned i; 
+	glGetUniformIndices(prog, 1, &name, &ubo_var_id); 
+	glGetActiveUniformsiv(prog, 1, &ubo_var_id, GL_UNIFORM_OFFSET, &ubo_var_offset); 
+	glGetActiveUniformsiv(prog, 1, &ubo_var_id, GL_UNIFORM_SIZE, &ubo_var_size); 
+	glGetActiveUniformsiv(prog, 1, &ubo_var_id, GL_UNIFORM_ARRAY_STRIDE, &ubo_var_stride); 
+	for (i = 0; i < ubo_var_size; i++) 
+		glBufferSubData(GL_UNIFORM_BUFFER, ubo_var_offset + i * ubo_var_stride , sizeof(float),&value[i] );
+	assert(glGetError() == 0);
+
+}
+
+static void set_ubo_content(void)
+{
+	static GLuint ubo_id;
+	static GLuint ubo_size;
+
+	static const float f1[] = { 0.1, 0.2, 0.3 };
+	static const float f2[] = { 0.15, 0.25, 0.35 };
+	static const float f3[] = { 0.175, 0.275, 0.375 };
+	static const float f4[] = { 0.22, 0.28, 0.30 };
+	static const float f5[] = { 0.28, 0.21, 0.6} ;
+	static const float f6[] = { 0.12, 0.13, 0.14};
+	static const float f7[] = { 0, 0.57, 0.34 };
+	static const float f8[] = { 0.05, 0.11, 0.17 };
+	static const float f9[] = { 0.45, 0.45, 0.45 };
+
+	/* First UBO */
+
+	ubo_id = glGetUniformBlockIndex(prog, "mode_and_offset");
+	glGetActiveUniformBlockiv(prog, ubo_id,GL_UNIFORM_BLOCK_DATA_SIZE, &ubo_size);
+
+	glGenBuffers(1,&mode_and_offset_buf);
+	glBindBuffer(GL_UNIFORM_BUFFER,mode_and_offset_buf);
+	glBufferData(GL_UNIFORM_BUFFER,ubo_size,0,GL_DYNAMIC_DRAW);
+
+
+	glBindBufferBase(GL_UNIFORM_BUFFER, 0, mode_and_offset_buf);
+	glUniformBlockBinding(prog, ubo_id, 0);
+
+	/* Second UBO */
+
+	ubo_id = glGetUniformBlockIndex(prog, "color_information");
+	glGetActiveUniformBlockiv(prog, ubo_id,GL_UNIFORM_BLOCK_DATA_SIZE, &ubo_size);
+
+	glGenBuffers(1,&color_information_buf);
+	glBindBuffer(GL_UNIFORM_BUFFER,color_information_buf);
+	glBufferData(GL_UNIFORM_BUFFER,ubo_size,0,GL_DYNAMIC_DRAW);
+
+	set_uniform_array(color_information_names[0],f1);
+	set_uniform_array(color_information_names[1],f2);
+	set_uniform_array(color_information_names[2],f3);
+	set_uniform_array(color_information_names[3],f4);
+	set_uniform_array(color_information_names[4],f5);
+	set_uniform_array(color_information_names[5],f6);
+	set_uniform_array(color_information_names[6],f7);
+	set_uniform_array(color_information_names[7],f8);
+	set_uniform_array(color_information_names[8],f9);
+
+
+	glBindBufferBase(GL_UNIFORM_BUFFER, 1, color_information_buf);
+	glUniformBlockBinding(prog, ubo_id, 1);
+
+	assert(glGetError() == 0);
+}
+
+void piglit_init(int argc, char **argv)
+{
+	GLuint vs;
+	GLuint fs;
+
+	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
+
+	/* Check the driver. */
+	if (!GLEW_VERSION_1_5) {
+		fprintf(stderr, "OpenGL 1.5 required.\n");
+		piglit_report_result(PIGLIT_SKIP);
+	}
+	piglit_require_GLSL();
+	piglit_require_extension("GL_ARB_uniform_buffer_object");
+
+
+	/* Create shaders. */
+	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vert_shader_text);
+	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag_shader_text);
+	prog = piglit_CreateProgram();
+	piglit_AttachShader(prog,vs);
+	piglit_AttachShader(prog, fs);
+
+	piglit_LinkProgram(prog);
+	if (!piglit_link_check_status(prog)) {
+		piglit_DeleteProgram(prog);
+		piglit_report_result(PIGLIT_FAIL);
+	}
+
+	positionAttrib = glGetAttribLocation(prog,"VertexPosition");
+
+	glGenBuffers(1,&vertex_buf);
+	glBindBuffer(GL_ARRAY_BUFFER,vertex_buf);
+	glBufferData(GL_ARRAY_BUFFER,sizeof(verts),verts,GL_STATIC_DRAW);
+
+	glClearColor(0., 0., 0., 1.0);
+	glEnableClientState(GL_VERTEX_ARRAY);
+
+	set_ubo_content();
+}
+
+static void
+render_mode(GLint mode1, GLint mode2, GLint offset1, GLint offset2)
+{
+	glClear(GL_COLOR_BUFFER_BIT);
+
+	piglit_UseProgram(prog);
+	glLoadIdentity();
+
+	glBindBuffer(GL_UNIFORM_BUFFER,mode_and_offset_buf);
+
+	set_uniform_int(mode_and_offset_names[0],mode1);
+	set_uniform_int(mode_and_offset_names[1],mode2);
+	set_uniform_int(mode_and_offset_names[2],offset1);
+	set_uniform_int(mode_and_offset_names[3],offset2);
+
+	glBindBuffer(GL_ARRAY_BUFFER, vertex_buf);
+	glVertexAttribPointer(positionAttrib,2,GL_FLOAT,GL_FALSE,8,0);
+	glEnableVertexAttribArray(positionAttrib);
+	glDrawArrays(GL_QUADS, 0, 4);
+
+	assert(glGetError() == 0);
+}
+
+enum piglit_result piglit_display(void)
+{
+	static const float result1[] = {0.22,0.85,0.64};
+	static const float result2[] = {0.22,0.85,0.64};
+	static const float result3[] = {0.22,0.22,0.59};
+	static const float result4[] = {0.22,0.42,0.59};
+
+	GLboolean pass = GL_TRUE;
+
+	render_mode(0,0,1,2);
+	pass = piglit_probe_pixel_rgb(32, 32, result1) && pass;
+
+	render_mode(1,0,1,2);
+	pass = piglit_probe_pixel_rgb(32, 32, result2) && pass;
+
+	render_mode(0,1,1,2);
+	pass = piglit_probe_pixel_rgb(32, 32, result3) && pass;
+
+	render_mode(1,1,1,2);
+	pass = piglit_probe_pixel_rgb(32, 32, result4) && pass;
+
+	glutSwapBuffers();
+
+	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
+}
-- 
1.7.7



More information about the mesa-dev mailing list