[Piglit] [Review Request 2/2] Added triangle rasterisation overdraw test based off Brian Paul's suggestion
jbenton at vmware.com
jbenton at vmware.com
Fri May 11 09:31:16 PDT 2012
From: James Benton <jbenton at vmware.com>
Fills the window using a triangle fan around a random point,
can be filled exactly or clipped by drawing a circle containing the window.
Ensures all pixels are drawn only once for that screen fill thus testing
that all triangles are rasterised correctly.
---
tests/general/CMakeLists.gl.txt | 1 +
tests/general/triangle-rasterisation-overdraw.cpp | 245 +++++++++++++++++++++
2 files changed, 246 insertions(+)
create mode 100644 tests/general/triangle-rasterisation-overdraw.cpp
diff --git a/tests/general/CMakeLists.gl.txt b/tests/general/CMakeLists.gl.txt
index 96841c6..d17a371 100644
--- a/tests/general/CMakeLists.gl.txt
+++ b/tests/general/CMakeLists.gl.txt
@@ -107,6 +107,7 @@ piglit_add_executable (texgen texgen.c)
piglit_add_executable (texunits texunits.c)
piglit_add_executable (timer_query timer_query.c)
piglit_add_executable (triangle-rasterisation triangle-rasterisation.cpp)
+piglit_add_executable (triangle-rasterisation-overdraw triangle-rasterisation-overdraw.cpp)
piglit_add_executable (two-sided-lighting two-sided-lighting.c)
piglit_add_executable (two-sided-lighting-separate-specular two-sided-lighting-separate-specular.c)
piglit_add_executable (user-clip user-clip.c)
diff --git a/tests/general/triangle-rasterisation-overdraw.cpp b/tests/general/triangle-rasterisation-overdraw.cpp
new file mode 100644
index 0000000..02c52ae
--- /dev/null
+++ b/tests/general/triangle-rasterisation-overdraw.cpp
@@ -0,0 +1,245 @@
+/**************************************************************************
+ *
+ * Copyright 2012 VMware, Inc.
+ * All Rights Reserved.
+ *
+ * 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, sub license, 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 NON-INFRINGEMENT.
+ * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS 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.
+ *
+ **************************************************************************/
+
+/**
+ * Triangle Rasterisation Overdraw Test
+ *
+ * Draws a triangle fan to fill the screen and ensures every pixel was drawn only once
+ * Based on idea from Brian Paul
+ *
+ *
+ * Contains two methods of drawing to cover both clipped and unclipped triangles:
+ *
+ * 1. No-Clip: Picks a random point in the window and walks the perimeter of the window
+ * adding vertices to the triangle fan at non integer steps.
+ *
+ * 2. Clip: Picks a random point in the window and adds vertices to the triangle fan
+ * around a circle that contains the entire window, thus going off screen.
+ */
+
+#include "piglit-util.h"
+#include "mersenne.hpp"
+
+#include <time.h>
+#include <vector>
+#include <algorithm>
+
+/* Data structures */
+struct Vector
+{
+ Vector()
+ {
+ }
+
+ Vector(float x, float y)
+ : x(x), y(y)
+ {
+ }
+
+ float x, y;
+};
+
+/* Command line arguments */
+bool clips = false;
+bool break_on_fail = false;
+int random_test_count = 10;
+
+/* Compile time constants */
+const int float_range = 1000;
+
+/* Piglit variables */
+int piglit_width = 1000;
+int piglit_height = 1000;
+int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
+
+/* Globals */
+int test_id = 0;
+Mersenne mersenne;
+
+/* Generates a random triangle fan filling the whole screen */
+void generate_triangle_fan(std::vector<Vector>& triangle_fan, int width, int height)
+{
+ Vector mid;
+
+ /* Random center point */
+ mid.x = (mersenne.value() % (width * float_range)) * (1.0f / float_range);
+ mid.y = (mersenne.value() % (height * float_range)) * (1.0f / float_range);
+ triangle_fan.push_back(mid);
+
+ /* Setup random step size between 0.5 and 1.5 */
+ double pos = 0.0f;
+ double step = 0.5f + ((mersenne.value() % float_range) / (float)float_range);
+
+ if (!clips) {
+ /* Step around the window perimeter adding triangles */
+ double perimeter = width*2 + height*2;
+
+ for (int i = 0; pos < perimeter; ++i, pos = step * i) {
+ if (pos < width) {
+ /* bottom */
+ triangle_fan.push_back(Vector(pos, 0.0f));
+ } else if (pos < width + height) {
+ /* right */
+ triangle_fan.push_back(Vector(width, pos - width));
+ } else if(pos < width*2 + height) {
+ /* top */
+ triangle_fan.push_back(Vector(width - (pos - (width + height)), height));
+ } else {
+ /* left */
+ triangle_fan.push_back(Vector(0.0f, height - (pos - (width*2 + height))));
+ }
+ }
+ } else {
+ /* Step around a circle that contains the window */
+ double radius = (sqrt(width*width + height*height) / 2.0) + 5.0;
+ double perimeter = 2.0 * M_PI * radius;
+
+ for (int i = 0; pos < perimeter; ++i, pos = step * i) {
+ double theta = pos / radius;
+ float x = cos(theta) * radius;
+ float y = sin(theta) * radius;
+ triangle_fan.push_back(Vector(x + width / 2, y + height / 2));
+ }
+ }
+
+ /* Complete the fan! */
+ triangle_fan.push_back(triangle_fan.at(1));
+
+ ++test_id;
+}
+
+/* Tests a triangle fan */
+bool test_triangle_fan(const std::vector<Vector>& triangle_fan, int width, int height, const float* expected)
+{
+ static uint32_t* buffer = NULL;
+ static int lastW = -1, lastH = -1;
+
+ if (lastW != width || lastH != height) {
+ if (buffer)
+ delete [] buffer;
+
+ buffer = new uint32_t[width * height];
+
+ lastW = width;
+ lastH = height;
+ }
+
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ /* Draw triangle fan */
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glVertexPointer(2, GL_FLOAT, 0, triangle_fan.data());
+ glDrawArrays(GL_TRIANGLE_FAN, 0, triangle_fan.size());
+ glDisableClientState(GL_VERTEX_ARRAY);
+
+ if (!piglit_probe_rect_rgba(0, 0, width, height, expected)) {
+ printf("%d. Triangle Fan with %d triangles around (%f, %f)\n",
+ test_id, (int)triangle_fan.size(), triangle_fan[0].x, triangle_fan[0].y);
+
+ fflush(stdout);
+ return false;
+ }
+
+ return true;
+}
+
+/* Render */
+enum piglit_result
+piglit_display(void)
+{
+ float colour[] = { 127 / 255.0f, 127 / 255.0f, 127 / 255.0f, 127 / 255.0f };
+
+ glViewport(0, 0, piglit_width, piglit_height);
+ piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
+
+ /* Set render state */
+ glColor4fv(colour);
+ glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ glEnable(GL_BLEND);
+ glBlendEquation(GL_FUNC_ADD);
+ glBlendFunc(GL_ONE, GL_ONE);
+
+ /* Perform test */
+ GLboolean pass = GL_TRUE;
+
+ if (piglit_automatic) {
+ int fail_count = 0;
+
+ printf("Running %d random tests\n", random_test_count);
+ fflush(stdout);
+
+ for (int i = 0; i < random_test_count && !(fail_count && break_on_fail); ++i) {
+ std::vector<Vector> fan;
+ generate_triangle_fan(fan, piglit_width, piglit_height);
+
+ if (!test_triangle_fan(fan, piglit_width, piglit_height, colour))
+ fail_count++;
+ }
+
+ printf("Failed %d random tests\n", fail_count);
+ fflush(stdout);
+
+ if (fail_count)
+ pass = GL_FALSE;
+ } else {
+ std::vector<Vector> fan;
+ generate_triangle_fan(fan, piglit_width, piglit_height);
+ pass &= test_triangle_fan(fan, piglit_width, piglit_height, colour);
+
+ glutSwapBuffers();
+ }
+
+ assert(glGetError() == 0);
+ return pass ? PIGLIT_PASS : PIGLIT_FAIL;
+}
+
+/* Read command line arguments! */
+void
+piglit_init(int argc, char **argv)
+{
+ uint32_t seed = 0xfacebeef ^ time(NULL);
+
+ for (int i = 1; i < argc; ++i) {
+ if (strcmp(argv[i], "-break_on_fail") == 0){
+ break_on_fail = true;
+ printf("Execution will stop on first fail\n");
+ } else if (strcmp(argv[i], "-clip") == 0){
+ clips = true;
+ printf("Clipped triangles are being tested\n");
+ } else if (i + 1 < argc) {
+ if (strcmp(argv[i], "-count") == 0) {
+ random_test_count = strtoul(argv[++i], NULL, 0);
+ } else if (strcmp(argv[i], "-seed") == 0) {
+ seed = strtoul(argv[++i], NULL, 0);
+ }
+ }
+ }
+
+ printf("Random seed: 0x%08X\n", seed);
+ mersenne.init(seed);
+}
--
1.7.9.5
More information about the Piglit
mailing list