[Piglit] [PATCH] gl-2.1: Verify values of GLSL built-in constants that are visible to the GL API
Ian Romanick
idr at freedesktop.org
Wed Feb 1 14:41:36 PST 2012
From: Ian Romanick <ian.d.romanick at intel.com>
Values are verified by attempting to compile a shader that sizes an
array based on a comparison between the built-in and its expected
value. If the values do not match, the array will have a size of -1
and will cause a compilation error. If the values do match, the array
will have a size of 1.
For safety, the shaders are checked for compilation errors and for
linking errors. This catched implementations that only generate
errors during linking. This means that the array is also accessed by
the shader body so that it is not eliminated (thereby eliminating the
error).
Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
If this looks right, I can generate a similar test for gl-3.0 and
gles-2.0. I intentionally chose to keep these separate because it
will make it easier to port to other APIs. For example, the shaders
for gl-3.0 and gles-2.0 will look different
tests/all.tests | 1 +
tests/spec/gl-2.1/CMakeLists.gl.txt | 1 +
tests/spec/gl-2.1/built-in-constants.c | 174 ++++++++++++++++++++++++++++++++
3 files changed, 176 insertions(+), 0 deletions(-)
create mode 100644 tests/spec/gl-2.1/built-in-constants.c
diff --git a/tests/all.tests b/tests/all.tests
index 2312ee0..862d63f 100644
--- a/tests/all.tests
+++ b/tests/all.tests
@@ -830,6 +830,7 @@ add_concurrent_test(gl20, 'vertex-program-two-side')
gl21 = Group()
spec['!OpenGL 2.1'] = gl21
gl21['minmax'] = concurrent_test('gl-2.1-minmax')
+gl21['GLSL built-in constant values'] = concurrent_test('gl-2.1-built-in-constants')
gl30 = Group()
spec['!OpenGL 3.0'] = gl30
diff --git a/tests/spec/gl-2.1/CMakeLists.gl.txt b/tests/spec/gl-2.1/CMakeLists.gl.txt
index 9a4afbe..4d604bb 100644
--- a/tests/spec/gl-2.1/CMakeLists.gl.txt
+++ b/tests/spec/gl-2.1/CMakeLists.gl.txt
@@ -14,3 +14,4 @@ link_libraries (
)
add_executable (gl-2.1-minmax minmax.c)
+add_executable (gl-2.1-built-in-constants built-in-constants.c)
diff --git a/tests/spec/gl-2.1/built-in-constants.c b/tests/spec/gl-2.1/built-in-constants.c
new file mode 100644
index 0000000..a23fb29
--- /dev/null
+++ b/tests/spec/gl-2.1/built-in-constants.c
@@ -0,0 +1,174 @@
+/* Copyright © 2012 Intel Corporation
+ *
+ * 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.
+ */
+
+/**
+ * \file gl-2.1-built-in-constants.c
+ * Verify values of GLSL built-in constants that are visible to the GL API.
+ *
+ * Values are verified by attempting to compile a shader that sizes an array
+ * based on a comparison between the built-in and its expected value. If the
+ * values do not match, the array will have a size of -1 and will cause a
+ * compilation error. If the values do match, the array will have a size of
+ * 1.
+ *
+ * For safety, the shaders are checked for compilation errors and for linking
+ * errors. This catched implementations that only generate errors during
+ * linking. This means that the array is also accessed by the shader body so
+ * that it is not eliminated (thereby eliminating the error).
+ */
+
+#include "piglit-util.h"
+
+static const char *fs_template =
+ "#version 120\n"
+ "uniform vec4 a[%s == %d ? 1 : -1];\n"
+ "void main() {\n"
+ " gl_FragColor = a[0];\n"
+ "}\n"
+ ;
+
+static const char *vs_template =
+ "#version 120\n"
+ "uniform vec4 a[%s == %d ? 1 : -1];\n"
+ "void main() {\n"
+ " gl_Position = a[0];\n"
+ "}\n"
+ ;
+
+static const struct {
+ const char *name;
+ GLenum pname;
+} built_ins[] = {
+ {
+ "gl_MaxLights",
+ GL_MAX_LIGHTS
+ },
+ {
+ "gl_MaxClipPlanes",
+ GL_MAX_CLIP_PLANES
+ },
+ {
+ "gl_MaxTextureUnits",
+ GL_MAX_TEXTURE_UNITS
+ },
+ {
+ "gl_MaxTextureCoords",
+ GL_MAX_TEXTURE_COORDS
+ },
+ {
+ "gl_MaxVertexAttribs",
+ GL_MAX_VERTEX_ATTRIBS
+ },
+ {
+ "gl_MaxVertexUniformComponents",
+ GL_MAX_VERTEX_UNIFORM_COMPONENTS
+ },
+ {
+ "gl_MaxVaryingFloats",
+ GL_MAX_VARYING_FLOATS
+ },
+ {
+ "gl_MaxVertexTextureImageUnits",
+ GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS
+ },
+ {
+ "gl_MaxCombinedTextureImageUnits",
+ GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
+ },
+ {
+ "gl_MaxTextureImageUnits",
+ GL_MAX_TEXTURE_IMAGE_UNITS
+ },
+ {
+ "gl_MaxFragmentUniformComponents",
+ GL_MAX_FRAGMENT_UNIFORM_COMPONENTS
+ },
+ {
+ "gl_MaxDrawBuffers",
+ GL_MAX_DRAW_BUFFERS
+ }
+};
+
+int piglit_width = 10, piglit_height = 10;
+int piglit_window_mode = GLUT_RGB;
+
+enum piglit_result
+piglit_display()
+{
+ /* UNREACHED */
+ return PIGLIT_FAIL;
+}
+
+void
+piglit_init(int argc, char **argv)
+{
+ bool pass = true;
+ char buf[512];
+ unsigned i;
+
+ for (i = 0; i < ARRAY_SIZE(built_ins); i++) {
+ int value;
+
+ glGetIntegerv(built_ins[i].pname, &value);
+ if (!piglit_check_gl_error(GL_NO_ERROR)) {
+ fprintf(stderr, "Query of 0x%04 generated an error.\n",
+ built_ins[i].name);
+ pass = false;
+ } else {
+ GLuint fs, vs, prog;
+
+ snprintf(buf, sizeof(buf), fs_template,
+ built_ins[i].name, value);
+ fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER,
+ buf);
+ if (fs == 0) {
+ fprintf(stderr,
+ "Check of %s failed in fragment "
+ "shader\n",
+ built_ins[i].name);
+ pass = false;
+ }
+
+ snprintf(buf, sizeof(buf), vs_template,
+ built_ins[i].name, value);
+ vs = piglit_compile_shader_text(GL_VERTEX_SHADER,
+ buf);
+ if (vs == 0) {
+ fprintf(stderr,
+ "Check of %s failed in vertex "
+ "shader\n",
+ built_ins[i].name);
+ pass = false;
+ }
+
+ prog = piglit_link_simple_program(fs, vs);
+ if (prog == 0) {
+ fprintf(stderr,
+ "Check of %s failed to link\n",
+ built_ins[i].name);
+ pass = false;
+ }
+ }
+ }
+
+ piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
+}
--
1.7.6.4
More information about the Piglit
mailing list