From d9290d78875fa9e8dc9fb054bde744606a89e8b9 Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Fri, 9 Jan 2015 23:14:10 +0100 Subject: [PATCH] Fixed GTC_packing *pack*norm*x* build and added tests #292 --- glm/detail/func_packing.inl | 18 +-- glm/gtc/packing.hpp | 2 +- glm/gtc/packing.inl | 70 ++++----- readme.txt | 1 + test/core/core_func_packing.cpp | 7 +- test/gtc/gtc_packing.cpp | 264 ++++++++++++++++++++++++++++++++ 6 files changed, 306 insertions(+), 56 deletions(-) diff --git a/glm/detail/func_packing.inl b/glm/detail/func_packing.inl index e668c988..e0d476e4 100644 --- a/glm/detail/func_packing.inl +++ b/glm/detail/func_packing.inl @@ -40,8 +40,8 @@ namespace glm { u16vec2 Topack(round(clamp(v, 0.0f, 1.0f) * 65535.0f)); // return reinterpret_cast(Topack); - uint* ptr(reinterpret_cast(&Topack)); - return *ptr; + uint* ptr(reinterpret_cast(&Topack)); + return *ptr; } GLM_FUNC_QUALIFIER vec2 unpackUnorm2x16(uint const & p) @@ -52,15 +52,13 @@ namespace glm GLM_FUNC_QUALIFIER uint packSnorm2x16(vec2 const & v) { - i16vec2 Topack(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); - // return reinterpret_cast(Topack); - uint* ptr(reinterpret_cast(&Topack)); - return *ptr; + i16vec2 const Topack(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); + return reinterpret_cast(Topack); } GLM_FUNC_QUALIFIER vec2 unpackSnorm2x16(uint const & p) { - vec2 Unpack(reinterpret_cast(p)); + vec2 const Unpack(reinterpret_cast(p)); return clamp( Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, -1.0f, 1.0f); @@ -68,13 +66,13 @@ namespace glm GLM_FUNC_QUALIFIER uint packUnorm4x8(vec4 const & v) { - u8vec4 Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f)); - return reinterpret_cast(Topack); + u8vec4 const Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f)); + return reinterpret_cast(Topack); } GLM_FUNC_QUALIFIER vec4 unpackUnorm4x8(uint const & p) { - vec4 Unpack(reinterpret_cast(p)); + vec4 const Unpack(reinterpret_cast(p)); return Unpack * float(0.0039215686274509803921568627451); // 1 / 255 } diff --git a/glm/gtc/packing.hpp b/glm/gtc/packing.hpp index 87a70d4c..8808aa4f 100644 --- a/glm/gtc/packing.hpp +++ b/glm/gtc/packing.hpp @@ -283,7 +283,7 @@ namespace glm /// @see vec2 unpackSnorm2x16(uint32 p) /// @see GLSL unpackSnorm4x8 man page /// @see GLSL 4.20.8 specification, section 8.4 Floating-Point Pack and Unpack Functions - GLM_FUNC_DECL vec4 unpackSnorm4x16(uint64 const & p); + GLM_FUNC_DECL vec4 unpackSnorm4x16(uint64 p); /// Returns an unsigned integer obtained by converting the components of a floating-point scalar /// to the 16-bit floating-point representation found in the OpenGL Specification, diff --git a/glm/gtc/packing.inl b/glm/gtc/packing.inl index 902787fb..5d243977 100644 --- a/glm/gtc/packing.inl +++ b/glm/gtc/packing.inl @@ -257,33 +257,31 @@ namespace detail GLM_FUNC_QUALIFIER float unpackUnorm1x8(uint8 p) { - float Unpack(static_cast(p)); + float const Unpack(p); return Unpack * static_cast(0.0039215686274509803921568627451); // 1 / 255 } GLM_FUNC_QUALIFIER uint16 packUnorm2x8(vec2 const & v) { u8vec2 Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f)); - uint16* Packed = reinterpret_cast(&Topack); - return *Packed; + return reinterpret_cast(Topack); } GLM_FUNC_QUALIFIER vec2 unpackUnorm2x8(uint16 p) { - u8vec2* Unpacked = reinterpret_cast(const_cast(&p)); - return vec2(*Unpacked) * float(0.0039215686274509803921568627451); // 1 / 255 + vec2 const Unpack(reinterpret_cast(p)); + return Unpack * float(0.0039215686274509803921568627451); // 1 / 255 } GLM_FUNC_QUALIFIER uint8 packSnorm1x8(float v) { - int8 Topack(static_cast(round(clamp(v ,-1.0f, 1.0f) * 127.0f))); - uint8* Packed = reinterpret_cast(&Topack); - return *Packed; + int8 const Topack(static_cast(round(clamp(v ,-1.0f, 1.0f) * 127.0f))); + return reinterpret_cast(Topack); } GLM_FUNC_QUALIFIER float unpackSnorm1x8(uint8 p) { - float Unpack(static_cast(*const_cast(&p))); + float const Unpack(reinterpret_cast(p)); return clamp( Unpack * 0.00787401574803149606299212598425f, // 1.0f / 127.0f -1.0f, 1.0f); @@ -291,16 +289,15 @@ namespace detail GLM_FUNC_QUALIFIER uint16 packSnorm2x8(vec2 const & v) { - i8vec2 Topack(round(clamp(v ,-1.0f, 1.0f) * 127.0f)); - uint16* Packed = reinterpret_cast(&Topack); - return *Packed; + i8vec2 const Topack(round(clamp(v, -1.0f, 1.0f) * 127.0f)); + return reinterpret_cast(Topack); } GLM_FUNC_QUALIFIER vec2 unpackSnorm2x8(uint16 p) { - i8vec2* Unpack = reinterpret_cast(const_cast(&p)); + vec2 const Unpack(reinterpret_cast(p)); return clamp( - vec2(*Unpack) * 0.00787401574803149606299212598425f, // 1.0f / 127.0f + Unpack * 0.00787401574803149606299212598425f, // 1.0f / 127.0f -1.0f, 1.0f); } @@ -311,33 +308,31 @@ namespace detail GLM_FUNC_QUALIFIER float unpackUnorm1x16(uint16 p) { - float Unpack = static_cast(*const_cast(&p)); + float const Unpack(p); return Unpack * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0 } GLM_FUNC_QUALIFIER uint64 packUnorm4x16(vec4 const & v) { - u16vec4 Topack(round(clamp(v , 0.0f, 1.0f) * 65535.0f)); - uint64* Packed = reinterpret_cast(&Topack); - return *Packed; + u16vec4 const Topack(round(clamp(v , 0.0f, 1.0f) * 65535.0f)); + return reinterpret_cast(Topack); } GLM_FUNC_QUALIFIER vec4 unpackUnorm4x16(uint64 p) { - u16vec4* Unpack = reinterpret_cast(const_cast(&p)); - return vec4(*Unpack) * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0 + vec4 const Unpack(reinterpret_cast(p)); + return Unpack * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0 } GLM_FUNC_QUALIFIER uint16 packSnorm1x16(float v) { - int16 Topack = static_cast(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); - uint16* Packed = reinterpret_cast(&Topack); - return *Packed; + int16 const Topack = static_cast(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); + return reinterpret_cast(Topack); } GLM_FUNC_QUALIFIER float unpackSnorm1x16(uint16 p) { - float Unpack = static_cast(*const_cast(&p)); + float const Unpack(reinterpret_cast(p)); return clamp( Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, -1.0f, 1.0f); @@ -345,16 +340,15 @@ namespace detail GLM_FUNC_QUALIFIER uint64 packSnorm4x16(vec4 const & v) { - i16vec4 Topack = static_cast(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); - uint64* Packed = reinterpret_cast(&Topack); - return *Packed; + i16vec4 const Topack(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); + return reinterpret_cast(Topack); } GLM_FUNC_QUALIFIER vec4 unpackSnorm4x16(uint64 p) { - i16vec4* Unpack(reinterpret_cast(const_cast(&p))); + vec4 const Unpack(reinterpret_cast(p)); return clamp( - vec4(*Unpack) * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, + Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, -1.0f, 1.0f); } @@ -379,19 +373,17 @@ namespace detail detail::toFloat16(v.z), detail::toFloat16(v.w)); - uint64* Packed = reinterpret_cast(&Unpack); - return *Packed; + return reinterpret_cast(Unpack); } GLM_FUNC_QUALIFIER glm::vec4 unpackHalf4x16(uint64 v) { - i16vec4* p = reinterpret_cast(const_cast(&v)); - i16vec4 Unpack(*p); + i16vec4 Unpack(reinterpret_cast(v)); return vec4( - detail::toFloat32(Unpack.x), - detail::toFloat32(Unpack.y), - detail::toFloat32(Unpack.z), + detail::toFloat32(Unpack.x), + detail::toFloat32(Unpack.y), + detail::toFloat32(Unpack.z), detail::toFloat32(Unpack.w)); } @@ -483,7 +475,7 @@ namespace detail GLM_FUNC_QUALIFIER uint32 packF2x11_1x10(vec3 const & v) { - return + return ((detail::floatTo11bit(v.x) & ((1 << 11) - 1)) << 0) | ((detail::floatTo11bit(v.y) & ((1 << 11) - 1)) << 11) | ((detail::floatTo10bit(v.z) & ((1 << 10) - 1)) << 22); @@ -492,8 +484,8 @@ namespace detail GLM_FUNC_QUALIFIER vec3 unpackF2x11_1x10(uint32 v) { return vec3( - detail::packed11bitToFloat(v >> 0), - detail::packed11bitToFloat(v >> 11), + detail::packed11bitToFloat(v >> 0), + detail::packed11bitToFloat(v >> 11), detail::packed10bitToFloat(v >> 22)); } diff --git a/readme.txt b/readme.txt index 25be0a43..1b990718 100644 --- a/readme.txt +++ b/readme.txt @@ -80,6 +80,7 @@ Fixes: - Fixed mat4x3 = mat2x3 * mat4x2 operator #297 - Fixed warnings in F2x11_1x10 packing function in GTC_packing #295 - Fixed Visual Studio natvis support for vec4 #288 +- Fixed GTC_packing *pack*norm*x* build and added tests #292 ================================================================================ GLM 0.9.6.1: 2014-12-10 diff --git a/test/core/core_func_packing.cpp b/test/core/core_func_packing.cpp index 9d64e4f9..411121bc 100644 --- a/test/core/core_func_packing.cpp +++ b/test/core/core_func_packing.cpp @@ -38,12 +38,7 @@ int test_packUnorm2x16() { int Error = 0; -/* - std::vector A; - A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 0.0f))); - A.push_back(glm::hvec2(glm::half( 0.5f), glm::half( 0.7f))); - A.push_back(glm::hvec2(glm::half( 0.1f), glm::half( 0.2f))); -*/ + std::vector A; A.push_back(glm::vec2(1.0f, 0.0f)); A.push_back(glm::vec2(0.5f, 0.7f)); diff --git a/test/gtc/gtc_packing.cpp b/test/gtc/gtc_packing.cpp index 6a6e358d..fc6f57c2 100644 --- a/test/gtc/gtc_packing.cpp +++ b/test/gtc/gtc_packing.cpp @@ -30,6 +30,7 @@ /////////////////////////////////////////////////////////////////////////////////// #include +#include #include #include @@ -253,10 +254,273 @@ int test_F2x11_1x10() return Error; } +int test_packUnorm1x16() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec1(1.0f)); + A.push_back(glm::vec1(0.5f)); + A.push_back(glm::vec1(0.1f)); + A.push_back(glm::vec1(0.0f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec1 B(A[i]); + glm::uint32 C = glm::packUnorm1x16(B.x); + glm::vec1 D(glm::unpackUnorm1x16(C)); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 65535.f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + +int test_packSnorm1x16() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec1( 1.0f)); + A.push_back(glm::vec1( 0.0f)); + A.push_back(glm::vec1(-0.5f)); + A.push_back(glm::vec1(-0.1f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec1 B(A[i]); + glm::uint32 C = glm::packSnorm1x16(B.x); + glm::vec1 D(glm::unpackSnorm1x16(C)); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1; + } + + return Error; +} + +int test_packUnorm2x16() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec2(1.0f, 0.0f)); + A.push_back(glm::vec2(0.5f, 0.7f)); + A.push_back(glm::vec2(0.1f, 0.2f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec2 B(A[i]); + glm::uint32 C = glm::packUnorm2x16(B); + glm::vec2 D = glm::unpackUnorm2x16(C); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 65535.f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + +int test_packSnorm2x16() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec2( 1.0f, 0.0f)); + A.push_back(glm::vec2(-0.5f,-0.7f)); + A.push_back(glm::vec2(-0.1f, 0.1f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec2 B(A[i]); + glm::uint32 C = glm::packSnorm2x16(B); + glm::vec2 D = glm::unpackSnorm2x16(C); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + +int test_packUnorm4x16() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec4(1.0f)); + A.push_back(glm::vec4(0.5f)); + A.push_back(glm::vec4(0.1f)); + A.push_back(glm::vec4(0.0f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec4 B(A[i]); + glm::uint64 C = glm::packUnorm4x16(B); + glm::vec4 D(glm::unpackUnorm4x16(C)); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 65535.f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + +int test_packSnorm4x16() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec4( 1.0f, 0.0f, -0.5f, 0.5f)); + A.push_back(glm::vec4(-0.3f,-0.7f, 0.3f, 0.7f)); + A.push_back(glm::vec4(-0.1f, 0.1f, -0.2f, 0.2f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec4 B(A[i]); + glm::uint64 C = glm::packSnorm4x16(B); + glm::vec4 D(glm::unpackSnorm4x16(C)); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + +int test_packUnorm1x8() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec1(1.0f)); + A.push_back(glm::vec1(0.5f)); + A.push_back(glm::vec1(0.0f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec1 B(A[i]); + glm::uint16 C = glm::packUnorm1x8(B.x); + glm::vec1 D(glm::unpackUnorm1x8(C)); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + +int test_packSnorm1x8() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec1( 1.0f)); + A.push_back(glm::vec1(-0.7f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec1 B(A[i]); + glm::uint16 C = glm::packSnorm1x8(B.x); + glm::vec1 D(glm::unpackSnorm1x8(C)); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1; + } + + return Error; +} + +int test_packUnorm2x8() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec2(1.0f, 0.7f)); + A.push_back(glm::vec2(0.5f, 0.1f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec2 B(A[i]); + glm::uint16 C = glm::packUnorm2x8(B); + glm::vec2 D = glm::unpackUnorm2x8(C); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + +int test_packSnorm2x8() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec2( 1.0f, 0.0f)); + A.push_back(glm::vec2(-0.7f,-0.1f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec2 B(A[i]); + glm::uint16 C = glm::packSnorm2x8(B); + glm::vec2 D = glm::unpackSnorm2x8(C); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1; + } + + return Error; +} + +int test_packUnorm4x8() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec4(1.0f, 0.7f, 0.3f, 0.0f)); + A.push_back(glm::vec4(0.5f, 0.1f, 0.2f, 0.3f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec4 B(A[i]); + glm::uint32 C = glm::packUnorm4x8(B); + glm::vec4 D = glm::unpackUnorm4x8(C); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + +int test_packSnorm4x8() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec4( 1.0f, 0.0f,-0.5f,-1.0f)); + A.push_back(glm::vec4(-0.7f,-0.1f, 0.1f, 0.7f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec4 B(A[i]); + glm::uint32 C = glm::packSnorm4x8(B); + glm::vec4 D = glm::unpackSnorm4x8(C); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + int main() { int Error(0); + Error += test_packSnorm1x16(); + Error += test_packSnorm2x16(); + Error += test_packSnorm4x16(); + + Error += test_packSnorm1x8(); + Error += test_packSnorm2x8(); + Error += test_packSnorm4x8(); + + Error += test_packUnorm1x16(); + Error += test_packUnorm2x16(); + Error += test_packUnorm4x16(); + + Error += test_packUnorm1x8(); + Error += test_packUnorm2x8(); + Error += test_packUnorm4x8(); + Error += test_F2x11_1x10(); Error += test_Snorm3x10_1x2(); Error += test_Unorm3x10_1x2();