[Piglit] [PATCH] texture-float-formats: a simple GL_ARB_texture_float test

brianp at vmware.com brianp at vmware.com
Mon Apr 25 17:53:58 PDT 2011


From: Brian Paul <brianp at vmware.com>

---
 tests/all.tests                                    |    1 +
 tests/spec/CMakeLists.txt                          |    1 +
 tests/spec/arb_texture_float/CMakeLists.gl.txt     |   17 +
 tests/spec/arb_texture_float/CMakeLists.txt        |    1 +
 .../spec/arb_texture_float/texture-float-formats.c |  381 ++++++++++++++++++++
 5 files changed, 401 insertions(+), 0 deletions(-)
 create mode 100644 tests/spec/arb_texture_float/CMakeLists.gl.txt
 create mode 100644 tests/spec/arb_texture_float/CMakeLists.txt
 create mode 100644 tests/spec/arb_texture_float/texture-float-formats.c

diff --git a/tests/all.tests b/tests/all.tests
index 985e9e8..de709df 100644
--- a/tests/all.tests
+++ b/tests/all.tests
@@ -890,6 +890,7 @@ add_texwrap_test2(arb_texture_float, '2D', 'GL_LUMINANCE_ALPHA32F')
 add_texwrap_test2(arb_texture_float, '2D', 'GL_INTENSITY32F')
 add_texwrap_test2(arb_texture_float, '2D', 'GL_RGB32F')
 add_texwrap_test2(arb_texture_float, '2D', 'GL_RGBA32F')
+add_plain_test(arb_texture_float, 'arb_texture_float-texture-float-formats')
 
 ext_texture_integer = Group()
 spec['EXT_texture_integer'] = ext_texture_integer
diff --git a/tests/spec/CMakeLists.txt b/tests/spec/CMakeLists.txt
index 333b051..8ca213b 100644
--- a/tests/spec/CMakeLists.txt
+++ b/tests/spec/CMakeLists.txt
@@ -5,6 +5,7 @@ add_subdirectory (arb_robustness)
 add_subdirectory (arb_sampler_objects)
 add_subdirectory (arb_shader_texture_lod)
 add_subdirectory (ati_draw_buffers)
+add_subdirectory (arb_texture_float)
 add_subdirectory (ati_envmap_bumpmap)
 add_subdirectory (ext_fog_coord)
 add_subdirectory (nv_conditional_render)
diff --git a/tests/spec/arb_texture_float/CMakeLists.gl.txt b/tests/spec/arb_texture_float/CMakeLists.gl.txt
new file mode 100644
index 0000000..c3a4684
--- /dev/null
+++ b/tests/spec/arb_texture_float/CMakeLists.gl.txt
@@ -0,0 +1,17 @@
+include_directories(
+	${GLEXT_INCLUDE_DIR}
+	${OPENGL_INCLUDE_PATH}
+	${GLUT_INCLUDE_DIR}
+	${piglit_SOURCE_DIR}/tests/util
+)
+
+link_libraries (
+	piglitutil
+	${OPENGL_gl_LIBRARY}
+	${OPENGL_glu_LIBRARY}
+	${GLUT_glut_LIBRARY}
+)
+
+add_executable (arb_texture_float-texture-float-formats texture-float-formats.c)
+
+# vim: ft=cmake:
diff --git a/tests/spec/arb_texture_float/CMakeLists.txt b/tests/spec/arb_texture_float/CMakeLists.txt
new file mode 100644
index 0000000..144a306
--- /dev/null
+++ b/tests/spec/arb_texture_float/CMakeLists.txt
@@ -0,0 +1 @@
+piglit_include_target_api()
diff --git a/tests/spec/arb_texture_float/texture-float-formats.c b/tests/spec/arb_texture_float/texture-float-formats.c
new file mode 100644
index 0000000..a4ed1ef
--- /dev/null
+++ b/tests/spec/arb_texture_float/texture-float-formats.c
@@ -0,0 +1,381 @@
+/*
+ * Copyright (c) 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
+ * on 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 THEIR 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.
+ */
+
+/**
+ * Tests GL_ARB_texture_float floating point formats
+ */
+
+#include "piglit-util.h"
+
+#define ELEMENTS(ARRAY)  (sizeof(ARRAY) / sizeof(ARRAY[0]))
+
+int piglit_width = 100, piglit_height = 100;
+int piglit_window_mode = GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE;
+
+static const char *TestName = "texture-float-formats";
+
+static GLint TexWidth = 16, TexHeight = 16;
+
+static GLint BiasUniform = -1, ScaleUniform = -1, TexUniform = -1;
+
+static const float Bias = 0.5;
+static const float Scale = 1.0 / 2000.0;
+
+struct format_info
+{
+   const char *Name;
+   GLenum IntFormat, BaseFormat;
+   GLuint BitsPerChannel;
+};
+
+
+static const struct format_info Formats[] = {
+   { "GL_RGBA32F_ARB", GL_RGBA32F_ARB, GL_RGBA, 32 },
+   { "GL_RGB32F_ARB", GL_RGB32F_ARB, GL_RGB, 32 },
+   { "GL_ALPHA32F_ARB", GL_ALPHA32F_ARB, GL_ALPHA, 32 },
+   { "GL_INTENSITY32F_ARB", GL_INTENSITY32F_ARB, GL_INTENSITY, 32 },
+   { "GL_LUMINANCE32F_ARB", GL_LUMINANCE32F_ARB, GL_LUMINANCE, 32 },
+   { "GL_LUMINANCE_ALPHA32F_ARB", GL_LUMINANCE_ALPHA32F_ARB, GL_LUMINANCE, 32 },
+
+   { "GL_RGBA16F_ARB", GL_RGBA16F_ARB, GL_RGBA, 16 },
+   { "GL_RGB16F_ARB", GL_RGB16F_ARB, GL_RGB, 16 },
+   { "GL_ALPHA16F_ARB", GL_ALPHA16F_ARB, GL_ALPHA, 16 },
+   { "GL_INTENSITY16F_ARB", GL_INTENSITY16F_ARB, GL_INTENSITY, 16 },
+   { "GL_LUMINANCE16F_ARB", GL_LUMINANCE16F_ARB, GL_LUMINANCE, 16 },
+   { "GL_LUMINANCE_ALPHA16F_ARB", GL_LUMINANCE_ALPHA16F_ARB, GL_LUMINANCE, 16 },
+
+   /* XXX could also add tests for RED, RG formats */
+};
+
+#define NUM_FORMATS  (sizeof(Formats) / sizeof(Formats[0]))
+
+
+static const char *FragShaderText =
+   "uniform float bias, scale; \n"
+   "uniform sampler2D tex; \n"
+   "void main() \n"
+   "{ \n"
+   "   vec4 t = vec4(texture2D(tex, gl_TexCoord[0].xy)); \n"
+   "   gl_FragColor = t * scale + bias; \n"
+   "} \n";
+
+
+static GLuint FragShader, Program;
+
+
+
+static int
+num_components(GLenum format)
+{
+   switch (format) {
+   case GL_RGBA:
+      return 4;
+   case GL_RGB:
+      return 3;
+   case GL_ALPHA:
+      return 1;
+   case GL_INTENSITY:
+      return 1;
+   case GL_LUMINANCE:
+      return 1;
+   case GL_LUMINANCE_ALPHA:
+      return 2;
+   case GL_RED:
+      return 1;
+   case GL_RG:
+      return 2;
+   default:
+      assert(0);
+      return 0;
+   }
+}
+
+
+static void
+fill_array(int comps, int texels, void *buf, const float val[4])
+{
+   GLfloat *f = (GLfloat *) buf;
+   int i, j;
+
+   for (i = 0; i < texels; i++) {
+      for (j = 0; j < comps; j++) {
+         f[i * comps + j] = val[j];
+      }
+   }
+}
+
+
+static GLboolean
+check_error(const char *file, int line)
+{
+   GLenum err = glGetError();
+   if (err) {
+      fprintf(stderr, "%s: error 0x%x at %s:%d\n", TestName, err, file, line);
+      return GL_TRUE;
+   }
+   return GL_FALSE;
+}
+
+
+/** Scale a float in [-1000, 1000] to [0, 1] */
+static float
+scale_and_bias(float val)
+{
+   return val * Scale + Bias;
+}
+
+
+/** \return GL_TRUE for pass, GL_FALSE for fail */
+static GLboolean
+test_format(const struct format_info *info)
+{
+   const int comps = num_components(info->BaseFormat);
+   const int texels = TexWidth * TexHeight;
+   const int w = piglit_width / 10;
+   const int h = piglit_height / 10;
+   const float error = 2.0 / 255.0; /* XXX this is a hack */
+   GLfloat expected[4];
+   void *image;
+   float value[4];
+   GLfloat result[4];
+   GLint f, type;
+   GLenum userFormat;
+
+   /*printf("Testing %s\n", info->Name);*/
+
+   /* pick random texture color, comps in [-1000, 1000] */
+   value[0] = rand() % 2001 - 1000;
+   value[1] = rand() % 2001 - 1000;
+   value[2] = rand() % 2001 - 1000;
+   value[3] = rand() % 2001 - 1000;
+   /* debug
+   value[0] = value[1] = value[2] = value[3] = 500;
+   */
+
+   /* alloc, fill texture image */
+   image = malloc(comps * texels * sizeof(GLfloat));
+   fill_array(comps, texels, image, value);
+
+   /* GL_INTENSITY is not a legal src format */
+   userFormat = info->BaseFormat == GL_INTENSITY ? GL_LUMINANCE : info->BaseFormat;
+
+   glTexImage2D(GL_TEXTURE_2D, 0, info->IntFormat, TexWidth, TexHeight, 0,
+                userFormat, GL_FLOAT, image);
+   free(image);
+
+   if (check_error(__FILE__, __LINE__))
+      return GL_FALSE;
+
+   /* check internal format */
+   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &f);
+   if (f != info->IntFormat) {
+      fprintf(stderr, "%s: GL_TEXTURE_INTERNAL_FORMAT query failed for 0x%x\n",
+              TestName,
+              info->IntFormat);
+      return GL_FALSE;
+   }
+
+   /* check texture datatype info */
+   {
+      static const GLenum queries[] = {
+         GL_TEXTURE_RED_TYPE_ARB,
+         GL_TEXTURE_GREEN_TYPE_ARB,
+         GL_TEXTURE_BLUE_TYPE_ARB,
+         GL_TEXTURE_ALPHA_TYPE_ARB,
+         GL_TEXTURE_LUMINANCE_TYPE_ARB,
+         GL_TEXTURE_INTENSITY_TYPE_ARB,
+         GL_TEXTURE_DEPTH_TYPE_ARB
+      };
+      static const char *queryNames[] = {
+         "GL_TEXTURE_RED_TYPE_ARB",
+         "GL_TEXTURE_GREEN_TYPE_ARB",
+         "GL_TEXTURE_BLUE_TYPE_ARB",
+         "GL_TEXTURE_ALPHA_TYPE_ARB",
+         "GL_TEXTURE_LUMINANCE_TYPE_ARB",
+         "GL_TEXTURE_INTENSITY_TYPE_ARB",
+         "GL_TEXTURE_DEPTH_TYPE_ARB"
+      };
+      int i;
+      for (i = 0; i < ELEMENTS(queries); i++) {
+         GLint type = 1;
+         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, queries[i], &type);
+         if (check_error(__FILE__, __LINE__))
+            return GL_FALSE;
+         if (type != GL_NONE && type != GL_FLOAT) {
+            fprintf(stderr, "%s: %s query failed (returned 0x%x)\n",
+                    TestName, queryNames[i], type);
+            return GL_FALSE;
+         }
+      }
+   }
+
+   /* compute expected color */
+   switch (info->BaseFormat) {
+   case GL_RGBA:
+      expected[0] = scale_and_bias(value[0]);
+      expected[1] = scale_and_bias(value[1]);
+      expected[2] = scale_and_bias(value[2]);
+      expected[3] = scale_and_bias(value[3]);
+      break;
+   case GL_RGB:
+      expected[0] = scale_and_bias(value[0]);
+      expected[1] = scale_and_bias(value[1]);
+      expected[2] = scale_and_bias(value[2]);
+      expected[3] = scale_and_bias(1.0);
+      break;
+   case GL_ALPHA:
+      expected[0] =
+      expected[1] =
+      expected[2] = scale_and_bias(0.0);
+      expected[3] = scale_and_bias(value[0]);
+      break;
+   case GL_LUMINANCE:
+      expected[0] =
+      expected[1] =
+      expected[2] = scale_and_bias(value[0]);
+      expected[3] = scale_and_bias(1.0);
+      break;
+   case GL_INTENSITY:
+      expected[0] =
+      expected[1] =
+      expected[2] = 
+      expected[3] = scale_and_bias(value[0]);
+      break;
+   case GL_LUMINANCE_ALPHA:
+      expected[0] =
+      expected[1] =
+      expected[2] = scale_and_bias(value[0]);
+      expected[3] = scale_and_bias(value[1]);
+      break;
+   case GL_RED:
+      expected[0] = scale_and_bias(value[0]);
+      expected[1] = scale_and_bias(0.0);
+      expected[2] = scale_and_bias(0.0);
+      expected[3] = scale_and_bias(1.0);
+      break;
+   case GL_RG:
+      expected[0] = scale_and_bias(value[0]);
+      expected[1] = scale_and_bias(value[1]);
+      expected[2] = scale_and_bias(0.0);
+      expected[3] = scale_and_bias(1.0);
+      break;
+   default:
+      abort();
+   }
+
+   /* draw */
+   glClearColor(0.5, 0.5, 0.5, 0.0);
+   glClear(GL_COLOR_BUFFER_BIT);
+   glBegin(GL_POLYGON);
+   glTexCoord2f(0, 0);   glVertex2f(0, 0);
+   glTexCoord2f(1, 0);   glVertex2f(w, 0);
+   glTexCoord2f(1, 1);   glVertex2f(w, h);
+   glTexCoord2f(0, 1);   glVertex2f(0, h);
+   glEnd();
+
+   if (check_error(__FILE__, __LINE__))
+      return GL_FALSE;
+
+   /* read color */
+   glReadPixels(w/2, h/2, 1, 1, GL_RGBA, GL_FLOAT, result);
+
+   glutSwapBuffers();
+
+   if (check_error(__FILE__, __LINE__))
+      return GL_FALSE;
+
+   /* test */
+   if (fabsf(result[0] - expected[0]) > error ||
+       fabsf(result[1] - expected[1]) > error ||
+       fabsf(result[2] - expected[2]) > error ||
+       fabsf(result[3] - expected[3]) > error) {
+      /* unexpected result */
+      int i;
+
+      fprintf(stderr, "%s: failure with format %s:\n", TestName, info->Name);
+      fprintf(stderr, "  texture color = ");
+      for (i = 0; i < comps; i++) {
+         fprintf(stderr, "%f", value[i]);
+         if (i + 1 < comps)
+            fprintf(stderr, ", ");
+      }
+      fprintf(stderr, "\n");
+      fprintf(stderr, "  expected color = %g, %g, %g, %g\n",
+              expected[0], expected[1], expected[2], expected[3]);
+      fprintf(stderr, "  rendered color = %g, %g, %g, %g\n",
+              result[0], result[1], result[2], result[3]);
+      return GL_FALSE;
+   }
+
+   glutSwapBuffers();
+
+   return GL_TRUE;
+}
+
+
+enum piglit_result
+piglit_display(void)
+{
+   int f;
+
+   for (f = 0; f < NUM_FORMATS; f++)
+      if (!test_format(&Formats[f]))
+         return PIGLIT_FAILURE;
+
+   return PIGLIT_SUCCESS;
+}
+
+
+void
+piglit_init(int argc, char **argv)
+{
+   GLuint Texture;
+
+   piglit_require_extension("GL_ARB_texture_float");
+   piglit_require_extension("GL_ARB_fragment_shader");
+
+   FragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, FragShaderText);
+   assert(FragShader);
+
+   Program = piglit_link_simple_program(0, FragShader);
+
+   glUseProgram(Program);
+
+   BiasUniform = glGetUniformLocation(Program, "bias");
+   ScaleUniform = glGetUniformLocation(Program, "scale");
+   TexUniform = glGetUniformLocation(Program, "tex");
+
+   glUniform1f(BiasUniform, Bias);
+   glUniform1f(ScaleUniform, Scale);
+   glUniform1i(TexUniform, 0);  /* tex unit zero */
+
+   (void) check_error(__FILE__, __LINE__);
+
+   glGenTextures(1, &Texture);
+   glBindTexture(GL_TEXTURE_2D, Texture);
+   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+   piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
+}
-- 
1.7.3.4



More information about the Piglit mailing list