[Piglit] [PATCH] vertex-alignment: test drawing arrays with unusual offsets and strides
Brian Paul
brianp at vmware.com
Tue May 1 07:23:09 PDT 2012
Test unusual vertex array offsets and strides to check that unaligned
accesses, etc. are handled properly. Both user-space and VBO arrays
are tested.
This test does everything that the older 'array-stride' test does and
more so we can probably remove the old test.
---
tests/all.tests | 1 +
tests/general/CMakeLists.gl.txt | 1 +
tests/general/vertex-alignment.c | 220 ++++++++++++++++++++++++++++++++++++++
3 files changed, 222 insertions(+), 0 deletions(-)
create mode 100644 tests/general/vertex-alignment.c
diff --git a/tests/all.tests b/tests/all.tests
index bd16c42..7965421 100644
--- a/tests/all.tests
+++ b/tests/all.tests
@@ -324,6 +324,7 @@ add_plain_test(general, 'vao-01')
add_plain_test(general, 'vao-02')
add_concurrent_test(general, 'vao-element-array-buffer')
add_plain_test(general, 'varray-disabled')
+add_concurrent_test(general, 'vertex-alignment')
add_plain_test(general, 'vbo-bufferdata')
add_plain_test(general, 'vbo-map-remap')
add_plain_test(general, 'vbo-subdata-sync')
diff --git a/tests/general/CMakeLists.gl.txt b/tests/general/CMakeLists.gl.txt
index 562a634..b3e29e7 100644
--- a/tests/general/CMakeLists.gl.txt
+++ b/tests/general/CMakeLists.gl.txt
@@ -118,6 +118,7 @@ piglit_add_executable (vbo-map-remap vbo-map-remap.c)
piglit_add_executable (vbo-bufferdata vbo-bufferdata.c)
piglit_add_executable (vbo-subdata-zero vbo-subdata-zero.c)
piglit_add_executable (vbo-subdata-sync vbo-subdata-sync.c)
+piglit_add_executable (vertex-alignment vertex-alignment.c)
piglit_add_executable (windowoverlap windowoverlap.c)
piglit_add_executable (object_purgeable-api-pbo object_purgeable-api-pbo.c object_purgeable.c)
piglit_add_executable (object_purgeable-api-texture object_purgeable-api-texture.c object_purgeable.c)
diff --git a/tests/general/vertex-alignment.c b/tests/general/vertex-alignment.c
new file mode 100644
index 0000000..5ba7f14
--- /dev/null
+++ b/tests/general/vertex-alignment.c
@@ -0,0 +1,220 @@
+/*
+ * Copyright (c) 2012 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.
+ */
+
+/*
+ * Test unusual vertex array offsets/strides/alignments.
+ * Both VBOs and user-space arrays are tested.
+ * Brian Paul
+ */
+
+#include "piglit-util.h"
+#include "piglit-framework.h"
+
+int piglit_width = 100;
+int piglit_height = 100;
+int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
+
+static const char *TestName = "vertex-alignment";
+
+static const GLfloat Verts[4][2] = {
+ { 0, 0 },
+ { 1, 0 },
+ { 1, 1 },
+ { 0, 1 }
+};
+
+static const GLubyte Colors[4][3] = {
+ { 255, 0, 0 },
+ { 0, 255, 0 },
+ { 255, 255, 255 },
+ { 0, 0, 255 }
+};
+
+#define VERTEX_SIZE (sizeof(Verts[0]) + sizeof(Colors[0]))
+
+#define BUF_SIZE 1000
+
+
+/**
+ * Cast wrapper for passing an integer to glVertex/ColorPointer's
+ * 'pointer' parameter.
+ */
+static void *
+int_to_ptr(int k)
+{
+ return (void *) (long) k;
+}
+
+
+/**
+ * Place vertex data in the given memory buffer using offset and stride.
+ */
+static void
+setup_array_data(GLubyte *buffer, int offset, int stride)
+{
+ int pos, i;
+
+ pos = offset;
+
+ for (i = 0; i < 4; i++) {
+ /* place vertex data in VBO */
+ memcpy(buffer + pos , Verts[i], sizeof(Verts[i]));
+ /* place color data in VBO */
+ memcpy(buffer + pos + sizeof(Verts[i]), Colors[i],
+ sizeof(Colors[i]));
+ /* advance to next vertex position */
+ pos += stride;
+ }
+}
+
+
+/**
+ * Set up user-space vertex arrays. Interleaved position and color.
+ */
+static void
+setup_user_arrays(GLubyte *buffer, int offset, int stride)
+{
+ setup_array_data(buffer, offset, stride);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glVertexPointer(2, GL_FLOAT, stride, buffer + offset);
+ glColorPointer(3, GL_UNSIGNED_BYTE, stride,
+ buffer + offset + sizeof(Verts[0]));
+}
+
+
+/**
+ * Allocate a VBO and put the vertex position/color data into it.
+ * Vertex position and color will be interleaved.
+ * \param offset the offset (in bytes) of the first vertex from the start
+ * of the VBO.
+ * \param stride stride (in bytes) between vertices in the VBO. This should
+ * be at least as large as VERTEX_SIZE.
+ */
+static GLuint
+setup_vbo(int offset, int stride)
+{
+ GLuint vbo;
+ GLubyte *map;
+
+ assert(stride >= VERTEX_SIZE);
+
+ glGenBuffers(1, &vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferData(GL_ARRAY_BUFFER, BUF_SIZE, NULL, GL_STATIC_DRAW);
+
+ map = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
+ setup_array_data(map, offset, stride);
+ glUnmapBuffer(GL_ARRAY_BUFFER);
+
+ glVertexPointer(2, GL_FLOAT, stride, int_to_ptr(offset));
+ glColorPointer(3, GL_UNSIGNED_BYTE, stride,
+ int_to_ptr(offset + sizeof(Verts[0])));
+
+ return vbo;
+}
+
+
+enum piglit_result
+piglit_display(void)
+{
+ enum piglit_result result = PIGLIT_PASS;
+ int use_vbo, offset, stride;
+ GLubyte *user_buffer;
+ GLubyte *ref, *buf;
+ GLuint vbo;
+
+ ref = malloc(piglit_width * piglit_height * 4);
+ buf = malloc(piglit_width * piglit_height * 4);
+ user_buffer = malloc(BUF_SIZE);
+
+ /*printf("vertex size = %d bytes\n", (int) VERTEX_SIZE);*/
+
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glEnableClientState(GL_COLOR_ARRAY);
+
+ /* draw reference image with ordinary arrays */
+ glClear(GL_COLOR_BUFFER_BIT);
+ glVertexPointer(2, GL_FLOAT, 0, Verts);
+ glColorPointer(3, GL_UNSIGNED_BYTE, 0, Colors);
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ glReadPixels(0, 0, piglit_width, piglit_height,
+ GL_RGBA, GL_UNSIGNED_BYTE, ref);
+
+ piglit_present_results();
+
+ /* test both VBOs and user-space arrays */
+ for (use_vbo = 0; use_vbo < 2; use_vbo++) {
+ /* do tests for offsets in [0,7] and strides up to 16 bytes */
+ for (offset = 0; offset < 7; offset++) {
+ for (stride = VERTEX_SIZE; stride < 16; stride++) {
+
+ /* setup vertex data */
+ if (use_vbo) {
+ vbo = setup_vbo(offset, stride);
+ }
+ else {
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ setup_user_arrays(user_buffer, offset, stride);
+ }
+
+ /* draw */
+ glClear(GL_COLOR_BUFFER_BIT);
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ glReadPixels(0, 0, piglit_width, piglit_height,
+ GL_RGBA, GL_UNSIGNED_BYTE, buf);
+ piglit_present_results();
+
+ /* image compare */
+ if (memcmp(ref, buf, piglit_width * piglit_height * 4)) {
+ printf("%s: failed for offset = %d, "
+ "stride = %d (%s)\n",
+ TestName, offset, stride,
+ (use_vbo ? "VBO" : "client memory")
+ );
+ result = PIGLIT_FAIL;
+ }
+
+ /* clean up */
+ if (use_vbo) {
+ glDeleteBuffers(1, &vbo);
+ }
+ }
+ }
+ }
+
+ free(ref);
+ free(buf);
+
+ return result;
+}
+
+
+void
+piglit_init(int argc, char **argv)
+{
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(-.5, 1.5, -.5, 1.5, -1.0, 1.0);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+}
--
1.7.3.4
More information about the Piglit
mailing list