[Mesa-dev] [PATCH] glsl-fs-normalmatrix: New test program for gl_NormalMatrix.

Eric Anholt eric at anholt.net
Tue Oct 18 17:33:28 PDT 2011


From: tom fogal <tfogal at sci.utah.edu>

v2: lots of hacking by anholt to make it look more like a normal
    piglit test and make all results visible at once.
---
 tests/all.tests                      |    1 +
 tests/shaders/CMakeLists.gl.txt      |    1 +
 tests/shaders/glsl-fs-normalmatrix.c |  166 ++++++++++++++++++++++++++++++++++
 3 files changed, 168 insertions(+), 0 deletions(-)
 create mode 100644 tests/shaders/glsl-fs-normalmatrix.c

diff --git a/tests/all.tests b/tests/all.tests
index 0248164..89bd03d 100644
--- a/tests/all.tests
+++ b/tests/all.tests
@@ -396,6 +396,7 @@ add_plain_test(shaders, 'glsl-fs-loop')
 add_plain_test(shaders, 'glsl-fs-loop-nested')
 add_plain_test(shaders, 'glsl-fs-mix')
 add_plain_test(shaders, 'glsl-fs-mix-constant')
+add_concurrent_test(shaders, 'glsl-fs-normalmatrix')
 add_plain_test(shaders, 'glsl-fs-pointcoord')
 add_plain_test(shaders, 'glsl-fs-raytrace-bug27060')
 add_plain_test(shaders, 'glsl-fs-sampler-numbering')
diff --git a/tests/shaders/CMakeLists.gl.txt b/tests/shaders/CMakeLists.gl.txt
index 3dce256..ed72b21 100644
--- a/tests/shaders/CMakeLists.gl.txt
+++ b/tests/shaders/CMakeLists.gl.txt
@@ -82,6 +82,7 @@ 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)
 add_executable (glsl-fs-mix-constant glsl-fs-mix-constant.c)
+add_executable (glsl-fs-normalmatrix glsl-fs-normalmatrix.c)
 IF (NOT MSVC)
 	add_executable (glsl-fs-raytrace-bug27060 glsl-fs-raytrace-bug27060.c)
 ENDIF (NOT MSVC)
diff --git a/tests/shaders/glsl-fs-normalmatrix.c b/tests/shaders/glsl-fs-normalmatrix.c
new file mode 100644
index 0000000..b55a80a
--- /dev/null
+++ b/tests/shaders/glsl-fs-normalmatrix.c
@@ -0,0 +1,166 @@
+/*
+ * 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:
+ *    Tom Fogal
+ *
+ */
+
+/** @file glsl-fs-normalmatrix.c
+ *
+ * Tests gl_NormalMatrix for appropriate initial values.
+ */
+
+#include "piglit-util.h"
+
+int piglit_width = 30, piglit_height = 30;
+int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
+
+static const char vert[] = {
+	"void main()\n"
+	"{\n"
+	"	gl_Position = ftransform();\n"
+	"}\n"
+};
+
+/* Creates a fragment shader which colors everything green if
+ *   gl_NormalMatrix[col].row
+ * is between 'low' and 'high', otherwise everything is red.
+ * The returned string is dynamically allocated and must be free'd by the
+ * caller.
+ */
+static char *
+generate_fs(int row, int col)
+{
+	static const char *fs_template =
+		"void main()\n"
+		"{\n"
+		"	if (%f <= gl_NormalMatrix[%u].%c &&\n"
+		"	    gl_NormalMatrix[%u].%c <= %f)\n"
+		"		gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
+		"	else\n"
+		"		gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);\n"
+		"}\n";
+	char *result;
+	char row_char = "xyz"[row];
+	float expected_matrix[] = {
+		1, 0, 0,
+		0, 1, 0,
+		0, 0, 1,
+	};
+	float expected = expected_matrix[row * 3 + col];
+	float low = expected - .01;
+	float high = expected + .01;
+
+	if (0) {
+		printf("test: %g <= gl_NormalMatrix[%u].%c <= %g\n",
+		       low, col, row_char, high);
+	}
+
+	result = calloc(1, strlen(fs_template) + 100);
+	sprintf(result, fs_template, low, col, row_char, col, row_char, high);
+
+	return result;
+}
+
+static bool
+test(int row, int col)
+{
+	GLint vs, fs, prog;
+	float green[] = {0.0, 1.0, 0.0, 0.0};
+	char *fs_source;
+	bool pass;
+	int w = piglit_width / 3;
+	int h = piglit_height / 3;
+	int x = col * w;
+	int y = (2 - row) * h;
+
+	fs_source = generate_fs(row, col);
+	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vert);
+	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_source);
+	prog = piglit_link_simple_program(vs, fs);
+	glUseProgram(prog);
+
+	if (!fs || !vs || !prog) {
+		printf("Failed to compile with fragment shader:\n%s\n",
+		       fs_source);
+		return false;
+	}
+
+	piglit_draw_rect(x, y, w, h);
+	pass = piglit_probe_rect_rgb(x, y, w, h, green);
+
+	/* clean up shaders. */
+	free(fs_source);
+	glUseProgram(0);
+	glDeleteShader(vs);
+	glDeleteShader(fs);
+	glDeleteProgram(prog);
+
+	return pass;
+}
+
+enum piglit_result
+piglit_display(void)
+{
+	GLboolean pass = GL_TRUE;
+	int row, col;
+
+	/* Set up projection matrix so we can just draw using window
+	 * coordinates.
+	 */
+	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
+
+	/* Set the modelview to identity, which means normalmatrix
+	 * should also be identity.
+	 */
+	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
+
+	for (row = 0; row < 3; row++) {
+		for (col = 0; col < 3; col++) {
+			pass = test(row, col) && pass;
+		}
+	}
+
+	piglit_present_results();
+
+	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
+}
+
+void
+piglit_init(int argc, char **argv)
+{
+	if (!GLEW_VERSION_2_0) {
+		printf("Requires OpenGL 2.0\n");
+		piglit_report_result(PIGLIT_SKIP);
+	}
+	piglit_require_GLSL();
+
+	printf("gl_NormalMatrix access results are displayed as:\n");
+	printf("       x y z\n");
+	printf("      +-+-+-+\n");
+	printf("row 0 |1|0|0|\n");
+	printf("      +-+-+-+\n");
+	printf("row 1 |0|1|0|\n");
+	printf("      +-+-+-+\n");
+	printf("row 2 |0|0|1|\n");
+	printf("      +-+-+-+\n");
+}
-- 
1.7.7



More information about the mesa-dev mailing list