diff options
author | Marek Olšák <maraeo@gmail.com> | 2011-04-25 15:58:21 +0200 |
---|---|---|
committer | Marek Olšák <maraeo@gmail.com> | 2011-04-26 02:42:28 +0200 |
commit | 7692d4e562081c8c317edf0c780b7147572a7739 (patch) | |
tree | 934ecf9ce982a09769bbe62003cb7ff00b6e410c | |
parent | c5b3845359d67c8958683ec77cb029d19b00eda4 (diff) |
glean/pixelFormats: test EXT_texture_shared_exponent
-rw-r--r-- | tests/glean/CMakeLists.gl.txt | 1 | ||||
-rw-r--r-- | tests/glean/tpixelformats.cpp | 34 | ||||
-rw-r--r-- | tests/glean/tpixelformats.h | 1 | ||||
-rw-r--r-- | tests/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/util/rgb9e5.c | 177 | ||||
-rw-r--r-- | tests/util/rgb9e5.h | 38 |
6 files changed, 251 insertions, 1 deletions
diff --git a/tests/glean/CMakeLists.gl.txt b/tests/glean/CMakeLists.gl.txt index df9041627..04081a9dc 100644 --- a/tests/glean/CMakeLists.gl.txt +++ b/tests/glean/CMakeLists.gl.txt @@ -84,6 +84,7 @@ add_executable (glean basic.cpp lex.cpp timer.cpp + ../util/rgb9e5.c ) target_link_libraries (glean diff --git a/tests/glean/tpixelformats.cpp b/tests/glean/tpixelformats.cpp index 50283774b..3518ed1d8 100644 --- a/tests/glean/tpixelformats.cpp +++ b/tests/glean/tpixelformats.cpp @@ -31,6 +31,7 @@ #include <cassert> #include <cmath> #include "tpixelformats.h" +#include "../util/rgb9e5.h" // Set to 1 to help debug test failures: @@ -78,6 +79,8 @@ static const NameTokenComps Types[] = { "GL_UNSIGNED_SHORT_5_6_5_REV", GL_UNSIGNED_SHORT_5_6_5_REV, 3 }, { "GL_UNSIGNED_BYTE_3_3_2", GL_UNSIGNED_BYTE_3_3_2, 3 }, { "GL_UNSIGNED_BYTE_2_3_3_REV", GL_UNSIGNED_BYTE_2_3_3_REV, 3 }, + + { "GL_UNSIGNED_INT_5_9_9_9_REV", GL_UNSIGNED_INT_5_9_9_9_REV, 3 } }; #define NUM_TYPES (sizeof(Types) / sizeof(Types[0])) @@ -200,6 +203,8 @@ static const NameTokenComps InternalFormats[] = { "GL_LUMINANCE16_ALPHA16_SNORM", GL_LUMINANCE16_ALPHA16_SNORM, 2 }, { "GL_INTENSITY16_SNORM", GL_INTENSITY16_SNORM, 1 }, + { "GL_RGB9_E5", GL_RGB9_E5, 3 } + // XXX maybe add compressed formats too... }; @@ -227,6 +232,7 @@ ComponentMasks(GLenum datatype, GLuint masks[4]) case GL_INT: case GL_FLOAT: case GL_HALF_FLOAT_ARB: + case GL_UNSIGNED_INT_5_9_9_9_REV: /* special case, handled separately */ masks[0] = masks[1] = masks[2] = @@ -475,6 +481,7 @@ BaseTextureFormat(GLint intFormat) case GL_RGB_SNORM: case GL_RGB8_SNORM: case GL_RGB16_SNORM: + case GL_RGB9_E5: return GL_RGB; case 4: case GL_RGBA: @@ -562,6 +569,7 @@ SizeofType(GLenum datatype) case GL_UNSIGNED_INT_2_10_10_10_REV: case GL_UNSIGNED_INT_8_8_8_8: case GL_UNSIGNED_INT_8_8_8_8_REV: + case GL_UNSIGNED_INT_5_9_9_9_REV: case GL_UNSIGNED_INT: case GL_INT: case GL_FLOAT: @@ -607,6 +615,9 @@ PixelFormatsTest::CompatibleFormatAndType(GLenum format, GLenum datatype) const if (format == GL_RG && !haveRG) return false; + if (datatype == GL_UNSIGNED_INT_5_9_9_9_REV && !haveTexSharedExp) + return false; + const int formatComps = NumberOfComponentsInFormat(format); const int typeComps = NumberOfComponentsInPackedType(datatype); return formatComps == typeComps || typeComps == 0; @@ -663,6 +674,8 @@ PixelFormatsTest::SupportedIntFormat(GLint intFormat) const case GL_INTENSITY8_SNORM: case GL_INTENSITY16_SNORM: return haveSnorm; + case GL_RGB9_E5: + return haveTexSharedExp; default: return true; @@ -694,7 +707,25 @@ MakeImage(int width, int height, GLenum format, GLenum type, { assert(fillComponent < 4); - if (IsPackedType(type)) { + if (type == GL_UNSIGNED_INT_5_9_9_9_REV) { + GLubyte *image = new GLubyte [width * height * 4]; + int i; + + assert(format == GL_RGB); + + GLuint *ui = (GLuint *) image; + for (i = 0; i < width * height; i++) { + float p[3] = {0, 0, 0}; + + if (!IsUpperRight(i, width, height)) + p[fillComponent] = 1; + + ui[i] = float3_to_rgb9e5(p); + } + + return image; + } + else if (IsPackedType(type)) { const int bpp = SizeofType(type); GLubyte *image = new GLubyte [width * height * bpp]; GLuint masks[4]; @@ -1482,6 +1513,7 @@ PixelFormatsTest::setup(void) haveRG = GLUtils::haveExtensions("GL_ARB_texture_rg"); haveFloat = GLUtils::haveExtensions("GL_ARB_texture_float"); haveSnorm = GLUtils::haveExtensions("GL_EXT_texture_snorm"); + haveTexSharedExp = GLUtils::haveExtensions("GL_EXT_texture_shared_exponent"); glGetIntegerv(GL_ALPHA_BITS, &alphaBits); diff --git a/tests/glean/tpixelformats.h b/tests/glean/tpixelformats.h index ec6878df0..20641fe31 100644 --- a/tests/glean/tpixelformats.h +++ b/tests/glean/tpixelformats.h @@ -66,6 +66,7 @@ private: bool haveRG; bool haveFloat; bool haveSnorm; + bool haveTexSharedExp; bool CheckError(const char *where) const; diff --git a/tests/util/CMakeLists.txt b/tests/util/CMakeLists.txt index c3d1109e3..3daccea1c 100644 --- a/tests/util/CMakeLists.txt +++ b/tests/util/CMakeLists.txt @@ -14,6 +14,7 @@ set(UTIL_SOURCES piglit-util.c shader-load.c piglit-framework.c + rgb9e5.c ) piglit_include_target_api() diff --git a/tests/util/rgb9e5.c b/tests/util/rgb9e5.c new file mode 100644 index 000000000..27b38c9c8 --- /dev/null +++ b/tests/util/rgb9e5.c @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com> + * + * 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. + */ + +/* Copied from EXT_texture_shared_exponent */ + +#include "rgb9e5.h" +#include <math.h> +#include <assert.h> + +#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) + +/* XXX assuming little endian */ + +#define RGB9E5_EXPONENT_BITS 5 +#define RGB9E5_MANTISSA_BITS 9 +#define RGB9E5_EXP_BIAS 15 +#define RGB9E5_MAX_VALID_BIASED_EXP 31 + +#define MAX_RGB9E5_EXP (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS) +#define RGB9E5_MANTISSA_VALUES (1<<RGB9E5_MANTISSA_BITS) +#define MAX_RGB9E5_MANTISSA (RGB9E5_MANTISSA_VALUES-1) +#define MAX_RGB9E5 (((float)MAX_RGB9E5_MANTISSA)/RGB9E5_MANTISSA_VALUES * (1<<MAX_RGB9E5_EXP)) +#define EPSILON_RGB9E5 ((1.0/RGB9E5_MANTISSA_VALUES) / (1<<RGB9E5_EXP_BIAS)) + +typedef struct { +#ifdef MESA_BIG_ENDIAN + unsigned int negative:1; + unsigned int biasedexponent:8; + unsigned int mantissa:23; +#else + unsigned int mantissa:23; + unsigned int biasedexponent:8; + unsigned int negative:1; +#endif +} BitsOfIEEE754; + +typedef union { + unsigned int raw; + float value; + BitsOfIEEE754 field; +} float754; + +typedef struct { +#ifdef MESA_BIG_ENDIAN + unsigned int biasedexponent:RGB9E5_EXPONENT_BITS; + unsigned int b:RGB9E5_MANTISSA_BITS; + unsigned int g:RGB9E5_MANTISSA_BITS; + unsigned int r:RGB9E5_MANTISSA_BITS; +#else + unsigned int r:RGB9E5_MANTISSA_BITS; + unsigned int g:RGB9E5_MANTISSA_BITS; + unsigned int b:RGB9E5_MANTISSA_BITS; + unsigned int biasedexponent:RGB9E5_EXPONENT_BITS; +#endif +} BitsOfRGB9E5; + +typedef union { + unsigned int raw; + BitsOfRGB9E5 field; +} rgb9e5; + +static float ClampRange_for_rgb9e5(float x) +{ + if (x > 0.0) { + if (x >= MAX_RGB9E5) { + return MAX_RGB9E5; + } else { + return x; + } + } else { + /* NaN gets here too since comparisons with NaN always fail! */ + return 0.0; + } +} + +static float MaxOf3(float x, float y, float z) +{ + if (x > y) { + return MAX2(x, z); + } else { + return MAX2(y, z); + } +} + +/* Ok, FloorLog2 is not correct for the denorm and zero values, but we + are going to do a max of this value with the minimum rgb9e5 exponent + that will hide these problem cases. */ +static int FloorLog2(float x) +{ + float754 f; + + f.value = x; + return (f.field.biasedexponent - 127); +} + +unsigned float3_to_rgb9e5(const float rgb[3]) +{ + rgb9e5 retval; + float maxrgb; + int rm, gm, bm; + float rc, gc, bc; + int exp_shared, maxm; + double denom; + + rc = ClampRange_for_rgb9e5(rgb[0]); + gc = ClampRange_for_rgb9e5(rgb[1]); + bc = ClampRange_for_rgb9e5(rgb[2]); + + maxrgb = MaxOf3(rc, gc, bc); + exp_shared = MAX2(-RGB9E5_EXP_BIAS-1, FloorLog2(maxrgb)) + 1 + RGB9E5_EXP_BIAS; + assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP); + assert(exp_shared >= 0); + /* This pow function could be replaced by a table. */ + denom = pow(2, exp_shared - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS); + + maxm = (int) floor(maxrgb / denom + 0.5); + if (maxm == MAX_RGB9E5_MANTISSA+1) { + denom *= 2; + exp_shared += 1; + assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP); + } else { + assert(maxm <= MAX_RGB9E5_MANTISSA); + } + + rm = (int) floor(rc / denom + 0.5); + gm = (int) floor(gc / denom + 0.5); + bm = (int) floor(bc / denom + 0.5); + + assert(rm <= MAX_RGB9E5_MANTISSA); + assert(gm <= MAX_RGB9E5_MANTISSA); + assert(bm <= MAX_RGB9E5_MANTISSA); + assert(rm >= 0); + assert(gm >= 0); + assert(bm >= 0); + + retval.field.r = rm; + retval.field.g = gm; + retval.field.b = bm; + retval.field.biasedexponent = exp_shared; + + return retval.raw; +} + +void rgb9e5_to_float3(unsigned rgb, float retval[3]) +{ + rgb9e5 v; + int exponent; + float scale; + + v.raw = rgb; + exponent = v.field.biasedexponent - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS; + scale = (float) pow(2, exponent); + + retval[0] = v.field.r * scale; + retval[1] = v.field.g * scale; + retval[2] = v.field.b * scale; +} diff --git a/tests/util/rgb9e5.h b/tests/util/rgb9e5.h new file mode 100644 index 000000000..55b323af8 --- /dev/null +++ b/tests/util/rgb9e5.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com> + * + * 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. + */ + +#ifndef RGB9E5_H +#define RGB9E5_H + +#ifdef __cplusplus +extern "C" { +#endif + +void rgb9e5_to_float3(unsigned rgb, float retval[3]); +unsigned float3_to_rgb9e5(const float rgb[3]); + +#ifdef __cplusplus +} +#endif + +#endif |