[Piglit] [PATCH 2/2] glsl-fs-fragcoord-zw-ortho/perspecive: tests of gl_FragCoord.zw components

Brian Paul brianp at vmware.com
Mon Oct 10 11:32:32 PDT 2011


Test that gl_FragCoord's Z and W components have the expected values
for orthgraphic and perspective projection drawing.

Both tests are derived from Eric's glsl-fs-fragcoord.c test.
---
 tests/all.tests                                  |    2 +
 tests/shaders/CMakeLists.gl.txt                  |    2 +
 tests/shaders/glsl-fs-fragcoord-zw-ortho.c       |  134 ++++++++++++++++++
 tests/shaders/glsl-fs-fragcoord-zw-perspective.c |  159 ++++++++++++++++++++++
 tests/shaders/glsl-fs-fragcoord-zw.frag          |    4 +
 5 files changed, 301 insertions(+), 0 deletions(-)
 create mode 100644 tests/shaders/glsl-fs-fragcoord-zw-ortho.c
 create mode 100644 tests/shaders/glsl-fs-fragcoord-zw-perspective.c
 create mode 100644 tests/shaders/glsl-fs-fragcoord-zw.frag

diff --git a/tests/all.tests b/tests/all.tests
index 38d292d..c7f40a6 100644
--- a/tests/all.tests
+++ b/tests/all.tests
@@ -382,6 +382,8 @@ add_plain_test(shaders, 'glsl-fs-exp2')
 add_plain_test(shaders, 'glsl-fs-flat-color')
 add_plain_test(shaders, 'glsl-fs-fogcolor-statechange')
 add_plain_test(shaders, 'glsl-fs-fragcoord')
+add_plain_test(shaders, 'glsl-fs-fragcoord-zw-ortho')
+add_plain_test(shaders, 'glsl-fs-fragcoord-zw-perspective')
 add_plain_test(shaders, 'glsl-fs-loop')
 add_plain_test(shaders, 'glsl-fs-loop-nested')
 add_plain_test(shaders, 'glsl-fs-mix')
diff --git a/tests/shaders/CMakeLists.gl.txt b/tests/shaders/CMakeLists.gl.txt
index d0e3005..3dce256 100644
--- a/tests/shaders/CMakeLists.gl.txt
+++ b/tests/shaders/CMakeLists.gl.txt
@@ -76,6 +76,8 @@ add_executable (glsl-fs-exp2 glsl-fs-exp2.c)
 add_executable (glsl-fs-flat-color glsl-fs-flat-color.c)
 add_executable (glsl-fs-fogcolor-statechange glsl-fs-fogcolor-statechange.c)
 add_executable (glsl-fs-fragcoord glsl-fs-fragcoord.c)
+add_executable (glsl-fs-fragcoord-zw-ortho glsl-fs-fragcoord-zw-ortho.c)
+add_executable (glsl-fs-fragcoord-zw-perspective glsl-fs-fragcoord-zw-perspective.c)
 add_executable (glsl-fs-loop glsl-fs-loop.c)
 add_executable (glsl-fs-loop-nested glsl-fs-loop-nested.c)
 add_executable (glsl-fs-mix glsl-fs-mix.c)
diff --git a/tests/shaders/glsl-fs-fragcoord-zw-ortho.c b/tests/shaders/glsl-fs-fragcoord-zw-ortho.c
new file mode 100644
index 0000000..d9a92b5
--- /dev/null
+++ b/tests/shaders/glsl-fs-fragcoord-zw-ortho.c
@@ -0,0 +1,134 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ * Copyright © 2011 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.
+ *
+ * Authors:
+ *    Eric Anholt <eric at anholt.net>
+ *    Brian Paul
+ *
+ */
+
+/** @file glsl-fs-fragcoord-zw-ortho.c
+ *
+ * Tests that gl_FragCoord.zw produces the expected output in a fragment shader
+ * with an orthographic projection
+ */
+
+#include "piglit-util.h"
+
+int piglit_width = 256, piglit_height = 256;
+int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH;
+
+
+static void
+draw_quad(float x, float y, float w, float h)
+{
+	float zLeft = -1.0, zRight = 1.0;
+	float verts[4][4];
+
+	verts[0][0] = x;
+	verts[0][1] = y;
+	verts[0][2] = zLeft;
+	verts[0][3] = 1.0;
+	verts[1][0] = x + w;
+	verts[1][1] = y;
+	verts[1][2] = zRight;
+	verts[1][3] = 1.0;
+	verts[2][0] = x + w;
+	verts[2][1] = y + h;
+	verts[2][2] = zRight;
+	verts[2][3] = 1.0;
+	verts[3][0] = x;
+	verts[3][1] = y + h;
+	verts[3][2] = zLeft;
+	verts[3][3] = 1.0;
+
+	glVertexPointer(4, GL_FLOAT, 0, verts);
+	glEnableClientState(GL_VERTEX_ARRAY);
+
+	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+	glDisableClientState(GL_VERTEX_ARRAY);
+}
+
+
+
+enum piglit_result
+piglit_display(void)
+{
+	GLboolean pass = GL_TRUE;
+	int x, y;
+
+	glClearColor(0.5, 0.5, 0.5, 0.5);
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	draw_quad(0, 0, piglit_width, piglit_height);
+
+	/* Test Z values */
+	pass = piglit_probe_pixel_depth(0, 0, 1.0) && pass;
+	pass = piglit_probe_pixel_depth(piglit_width-1, 0, 0.0) && pass;
+	pass = piglit_probe_pixel_depth(piglit_width-1, piglit_height-1, 0.0) && pass;
+	pass = piglit_probe_pixel_depth(0, piglit_height-1, 1.0) && pass;
+
+	/* Test colors */
+	for (y = 0; y < piglit_height && pass; y++) {
+		for (x = 0; x < piglit_width; x++) {
+			float color[3];
+
+			color[0] = 1.0 - x / 256.0;
+			color[1] = 0.5;
+			color[2] = 0;
+
+			pass &= piglit_probe_pixel_rgb(x, y, color);
+			if (!pass)
+				break;
+		}
+	}
+
+	glutSwapBuffers();
+
+	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
+}
+
+void
+piglit_init(int argc, char **argv)
+{
+	GLint vs, fs, prog;
+
+	if (!GLEW_VERSION_2_0) {
+		printf("Requires OpenGL 2.0\n");
+		piglit_report_result(PIGLIT_SKIP);
+	}
+
+	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
+
+	vs = piglit_compile_shader(GL_VERTEX_SHADER,
+				   "shaders/glsl-mvp.vert");
+	fs = piglit_compile_shader(GL_FRAGMENT_SHADER,
+				   "shaders/glsl-fs-fragcoord-zw.frag");
+
+	prog = piglit_link_simple_program(vs, fs);
+
+	glUseProgram(prog);
+
+	glEnable(GL_DEPTH_TEST);
+}
diff --git a/tests/shaders/glsl-fs-fragcoord-zw-perspective.c b/tests/shaders/glsl-fs-fragcoord-zw-perspective.c
new file mode 100644
index 0000000..923ff5b
--- /dev/null
+++ b/tests/shaders/glsl-fs-fragcoord-zw-perspective.c
@@ -0,0 +1,159 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ * Copyright © 2011 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.
+ *
+ * Authors:
+ *    Eric Anholt <eric at anholt.net>
+ *    Brian Paul
+ *
+ */
+
+/** @file glsl-fs-fragcoord-zw-perspective.c
+ *
+ * Tests that gl_FragCoord.zw produces the expected output in a fragment shader
+ * with a perspective projection.
+ */
+
+#include "piglit-util.h"
+
+int piglit_width = 256, piglit_height = 256;
+int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH;
+
+static GLfloat Znear = 1.0, Zfar = 10.0;
+
+
+/**
+ * Draw a quad where the bottom edge is on the near clip plane and the
+ * top edge is on the far clip plane.  Adjust the far coordinates' X and Y
+ * values to fill the window.  So the quad doesn't look like it's drawn in
+ * perspective, but it is (the top is much wider than the bottom).
+ */
+static void
+draw_quad(void)
+{
+	float xBotLeft = -Znear, xBotRight = Znear;
+	float xTopLeft = -Zfar, xTopRight = Zfar;
+	float yBotLeft = -Znear, yBotRight = -Znear;
+	float yTopLeft = Zfar, yTopRight = Zfar;
+	float zBottom = -Znear, zTop = -Zfar;
+	float verts[4][4];
+
+	verts[0][0] = xBotLeft;
+	verts[0][1] = yBotLeft;
+	verts[0][2] = zBottom;
+	verts[0][3] = 1.0;
+	verts[1][0] = xBotRight;
+	verts[1][1] = yBotRight;
+	verts[1][2] = zBottom;
+	verts[1][3] = 1.0;
+	verts[2][0] = xTopRight;
+	verts[2][1] = yTopRight;
+	verts[2][2] = zTop;
+	verts[2][3] = 1.0;
+	verts[3][0] = xTopLeft;
+	verts[3][1] = yTopLeft;
+	verts[3][2] = zTop;
+	verts[3][3] = 1.0;
+
+	glVertexPointer(4, GL_FLOAT, 0, verts);
+	glEnableClientState(GL_VERTEX_ARRAY);
+
+	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+	glDisableClientState(GL_VERTEX_ARRAY);
+}
+
+
+/** Convert t in [0,1] into a 1/w value */
+static float
+t_to_w(float t)
+{
+	float zEye = -(Znear + t * (Zfar - Znear));
+	float wClip = -zEye;
+	return 1.0 / wClip;
+}
+
+
+enum piglit_result
+piglit_display(void)
+{
+	float w0, w1;
+	GLboolean pass = GL_TRUE;
+	int y;
+
+	glClearColor(0.5, 0.5, 0.5, 0.5);
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	draw_quad();
+
+	/* Spot-test Z values */
+	pass = piglit_probe_pixel_depth(0, 0, 0.0) && pass;
+	pass = piglit_probe_pixel_depth(piglit_width-1, 0, 0.0) && pass;
+	pass = piglit_probe_pixel_depth(piglit_width-2, piglit_height-1, 1.0) && pass;
+	pass = piglit_probe_pixel_depth(1, piglit_height-2, 1.0) && pass;
+
+	w0 = t_to_w(0.0);
+	w1 = t_to_w(1.0);
+
+	/* Test a column of pixel colors */
+	for (y = 0; y < piglit_height; y++) {
+		float expected[3];
+		float t = y / (float) (piglit_height - 1);
+		expected[0] = t;
+		expected[1] = w0 + t * (w1 - w0);
+		expected[2] = 0.0;
+
+		pass = piglit_probe_pixel_rgb(piglit_width/2, y, expected) & pass;
+	}
+
+	glutSwapBuffers();
+
+	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
+}
+
+void
+piglit_init(int argc, char **argv)
+{
+	GLint vs, fs, prog;
+
+	if (!GLEW_VERSION_2_0) {
+		printf("Requires OpenGL 2.0\n");
+		piglit_report_result(PIGLIT_SKIP);
+	}
+
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+	glFrustum(-1.0, 1.0, -1.0, 1.0, Znear, Zfar);
+	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
+
+	vs = piglit_compile_shader(GL_VERTEX_SHADER,
+				   "shaders/glsl-mvp.vert");
+	fs = piglit_compile_shader(GL_FRAGMENT_SHADER,
+				   "shaders/glsl-fs-fragcoord-zw.frag");
+
+	prog = piglit_link_simple_program(vs, fs);
+
+	glUseProgram(prog);
+
+	glEnable(GL_DEPTH_TEST);
+}
diff --git a/tests/shaders/glsl-fs-fragcoord-zw.frag b/tests/shaders/glsl-fs-fragcoord-zw.frag
new file mode 100644
index 0000000..10454bc
--- /dev/null
+++ b/tests/shaders/glsl-fs-fragcoord-zw.frag
@@ -0,0 +1,4 @@
+void main()
+{
+	gl_FragColor = vec4(gl_FragCoord.z, gl_FragCoord.w, 0.0, 0.0);
+}
-- 
1.7.3.4



More information about the Piglit mailing list