From a105876133a98c9b396437a4b98527d22bf36b5b Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Sun, 11 Aug 2013 02:16:47 +0200 Subject: [PATCH] Added GTC_packing implementation --- glm/core/func_packing.inl | 10 +- glm/core/func_vector_relational.hpp | 10 +- glm/gtc/packing.hpp | 34 ++-- glm/gtc/packing.inl | 260 +++++++++++++++++++++++++++- test/gtc/gtc_half_float.cpp | 42 ++--- test/gtc/gtc_packing.cpp | 51 +++--- 6 files changed, 333 insertions(+), 74 deletions(-) diff --git a/glm/core/func_packing.inl b/glm/core/func_packing.inl index ce22d4e9..2ee447e1 100644 --- a/glm/core/func_packing.inl +++ b/glm/core/func_packing.inl @@ -40,9 +40,7 @@ namespace glm uint32 Mask16((1 << 16) - 1); uint32 A((p >> 0) & Mask16); uint32 B((p >> 16) & Mask16); - return vec2( - A * 1.0f / 65535.0f, - B * 1.0f / 65535.0f); + return vec2(A, B) * float(1.5259021896696421759365224689097e-5); // 1.0 / 65535.0 } GLM_FUNC_QUALIFIER uint32 packSnorm2x16(vec2 const & v) @@ -53,9 +51,9 @@ namespace glm uint16 u; } A, B; - vec2 Unpack = clamp(v ,-1.0f, 1.0f) * 32767.0f; - A.i = detail::int16(round(Unpack.x)); - B.i = detail::int16(round(Unpack.y)); + vec2 Unpack = round(clamp(v ,-1.0f, 1.0f) * 32767.0f); + A.i = detail::int16(Unpack.x); + B.i = detail::int16(Unpack.y); uint32 Pack = (uint32(B.u) << 16) | (uint32(A.u) << 0); return Pack; } diff --git a/glm/core/func_vector_relational.hpp b/glm/core/func_vector_relational.hpp index b2fe4407..4c7d6813 100644 --- a/glm/core/func_vector_relational.hpp +++ b/glm/core/func_vector_relational.hpp @@ -52,8 +52,9 @@ namespace glm /// /// @see GLSL lessThan man page /// @see GLSL 4.20.8 specification, section 8.7 Vector Relational Functions - template class vecType> - GLM_FUNC_DECL typename vecType::bool_type lessThan(vecType const & x, vecType const & y); + // TODO: Mismatched + //template class vecType> + //GLM_FUNC_DECL typename vecType::bool_type lessThan(vecType const & x, vecType const & y); /// Returns the component-wise comparison of result x <= y. /// @@ -88,8 +89,9 @@ namespace glm /// /// @see GLSL equal man page /// @see GLSL 4.20.8 specification, section 8.7 Vector Relational Functions - template class vecType> - GLM_FUNC_DECL typename vecType::bool_type equal(vecType const & x, vecType const & y); + //TODO: conflicts with definision + //template class vecType> + //GLM_FUNC_DECL typename vecType::bool_type equal(vecType const & x, vecType const & y); /// Returns the component-wise comparison of result x != y. /// diff --git a/glm/gtc/packing.hpp b/glm/gtc/packing.hpp index f77b9f0d..15a2b3e7 100644 --- a/glm/gtc/packing.hpp +++ b/glm/gtc/packing.hpp @@ -51,23 +51,23 @@ namespace glm /// @addtogroup gtc_packing /// @{ - uint16 packUnorm1x16(float v); - float unpackUnorm1x16(uint16 v); + GLM_FUNC_DECL uint16 packUnorm1x16(float v); + GLM_FUNC_DECL float unpackUnorm1x16(uint16 v); - uint64 packUnorm4x16(vec4 const & v); - vec4 unpackUnorm4x16(uint64 const & v); + GLM_FUNC_DECL uint64 packUnorm4x16(vec4 const & v); + GLM_FUNC_DECL vec4 unpackUnorm4x16(uint64 const & v); - uint16 packSnorm1x16(float v); - float unpackSnorm1x16(uint16 v); + GLM_FUNC_DECL uint16 packSnorm1x16(float v); + GLM_FUNC_DECL float unpackSnorm1x16(uint16 v); - uint64 packSnorm4x16(vec4 const & v); - vec4 unpackSnorm4x16(uint64 const & v); + GLM_FUNC_DECL uint64 packSnorm4x16(vec4 const & v); + GLM_FUNC_DECL vec4 unpackSnorm4x16(uint64 const & v); - uint16 packUnorm2x8(vec2 const & v); - vec2 unpackUnorm2x8(uint16 v); + GLM_FUNC_DECL uint16 packUnorm2x8(vec2 const & v); + GLM_FUNC_DECL vec2 unpackUnorm2x8(uint16 v); - uint16 packSnorm2x8(vec2 const & v); - vec2 unpackSnorm2x8(uint16 v); + GLM_FUNC_DECL uint16 packSnorm2x8(vec2 const & v); + GLM_FUNC_DECL vec2 unpackSnorm2x8(uint16 v); GLM_FUNC_DECL uint16 packHalf1x16(float const & v); GLM_FUNC_DECL float unpackHalf1x16(uint16 const & v); @@ -93,16 +93,16 @@ namespace glm /// @see GLSL unpackHalf2x16 man page /// @see GLSL 4.20.8 specification, section 8.4 Floating-Point Pack and Unpack Functions GLM_FUNC_DECL vec4 unpackHalf4x16(uint64 const & v); - - GLM_FUNC_DECL uint32 packSnorm3x10Snorm1x2(vec4 const & v); - GLM_FUNC_DECL vec4 unpackSnorm3x10Snorm1x2(uint32 const & v); - GLM_FUNC_DECL uint32 packI3x10I1x2(ivec4 const & v); - GLM_FUNC_DECL ivec4 unpackI3x10I1x2(uint32 const & v); + GLM_FUNC_DECL uint32 packI10I10I10I2(ivec4 const & v); + GLM_FUNC_DECL ivec4 unpackI10I10I10I2(uint32 const & v); GLM_FUNC_DECL uint32 packU10U10U10U2(uvec4 const & v); GLM_FUNC_DECL uvec4 unpackU10U10U10U2(uint32 const & v); + GLM_FUNC_DECL uint32 packSnorm3x10_1x2(vec4 const & v); + GLM_FUNC_DECL vec4 unpackSnorm3x10_1x2(uint32 const & v); + GLM_FUNC_DECL uint32 packF11F11F10(vec3 const & v); GLM_FUNC_DECL vec3 unpackF11F11F10(uint32 const & v); diff --git a/glm/gtc/packing.inl b/glm/gtc/packing.inl index 2ae8e139..9b078dad 100644 --- a/glm/gtc/packing.inl +++ b/glm/gtc/packing.inl @@ -130,12 +130,270 @@ namespace detail { return ((floatTo11bit(x) & ((1 << 11) - 1)) << 0) | ((floatTo11bit(y) & ((1 << 11) - 1)) << 11) | ((floatTo10bit(z) & ((1 << 10) - 1)) << 22); } + + union u10u10u10u2 + { + struct + { + uint x : 10; + uint y : 10; + uint z : 10; + uint w : 2; + } data; + uint32 pack; + }; + + union i10i10i10i2 + { + struct + { + int x : 10; + int y : 10; + int z : 10; + int w : 2; + } data; + uint32 pack; + }; + + union unorm4x16 + { + struct + { + uint16 x; + uint16 y; + uint16 z; + uint16 w; + } data; + uint64 pack; + }; + + union snorm4x16 + { + struct + { + int16 x; + int16 y; + int16 z; + int16 w; + } data; + uint64 pack; + }; + + union snorm1x16 + { + int16 data; + uint16 pack; + }; + + union half1x16 + { + hdata data; + uint16 pack; + }; + + union unorm2x8 + { + struct + { + uint8 x; + uint8 y; + } data; + uint16 pack; + }; + + union snorm2x8 + { + struct + { + int8 x; + int8 y; + } data; + uint16 pack; + }; }//namespace detail + GLM_FUNC_QUALIFIER uint16 packUnorm1x16(float s) + { + return uint16(round(clamp(s, 0.0f, 1.0f) * 65535.0f)); + } + + GLM_FUNC_QUALIFIER float unpackUnorm1x16(uint16 p) + { + return float(p) * 1.0f / 65535.0f; + } + + GLM_FUNC_QUALIFIER uint64 packUnorm4x16(vec4 const & v) + { + i16vec4 Scaled(round(clamp(v, 0.0f, 1.0f) * 65535.0f)); + detail::unorm4x16 Packing; + Packing.data.x = Scaled[0]; + Packing.data.y = Scaled[1]; + Packing.data.z = Scaled[2]; + Packing.data.w = Scaled[3]; + return Packing.pack; + } + + GLM_FUNC_QUALIFIER vec4 unpackUnorm4x16(uint64 const & p) + { + detail::unorm4x16 Packing; + vec4 Result( + float(Packing.data.x), + float(Packing.data.y), + float(Packing.data.z), + float(Packing.data.w)); + Result *= float(1.5259021896696421759365224689097e-5); // 1.0 / 65535.0 + } + + GLM_FUNC_QUALIFIER uint16 packSnorm1x16(float v) + { + float Scaled = clamp(v ,-1.0f, 1.0f) * 32767.0f; + detail::snorm1x16 Packing; + Packing.data = detail::int16(Scaled); + return Packing.pack; + } + + GLM_FUNC_QUALIFIER float unpackSnorm1x16(uint16 p) + { + detail::snorm1x16 Packing; + Packing.pack = p; + return clamp(float(Packing.data) * float(3.0518509475997192297128208258309e-5), -1.0f, 1.0f); //1.0f / 32767.0f + } + + GLM_FUNC_QUALIFIER uint64 packSnorm4x16(vec4 const & v) + { + i16vec4 Scaled(clamp(v ,-1.0f, 1.0f) * 32767.0f); + detail::snorm4x16 Packing; + Packing.data.x = Scaled.x; + Packing.data.y = Scaled.y; + Packing.data.z = Scaled.z; + Packing.data.w = Scaled.w; + return Packing.pack; + } + + GLM_FUNC_QUALIFIER vec4 unpackSnorm4x16(uint64 const & p) + { + detail::snorm4x16 Packing; + Packing.pack = p; + vec4 Unpacked(Packing.data.x, Packing.data.y, Packing.data.z, Packing.data.w); + return clamp(Unpacked * float(3.0518509475997192297128208258309e-5), -1.0f, 1.0f); //1.0f / 32767.0f + } + + GLM_FUNC_QUALIFIER uint16 packUnorm2x8(vec2 const & v) + { + i8vec2 Scaled(round(clamp(v ,-1.0f, 1.0f) * 255.0f)); + detail::unorm2x8 Packing; + Packing.data.x = Scaled.x; + Packing.data.y = Scaled.y; + return Packing.pack; + } + + GLM_FUNC_QUALIFIER vec2 unpackUnorm2x8(uint16 p) + { + detail::unorm2x8 Packing; + Packing.pack = p; + vec2 Unpacked(Packing.data.x, Packing.data.y); + return Unpacked * float(0.0039215686274509803921568627451); + } + + GLM_FUNC_QUALIFIER uint16 packSnorm2x8(vec2 const & v) + { + glm::i8vec2 Scaled(round(clamp(v ,-1.0f, 1.0f) * 127.0f)); + detail::snorm2x8 Packing; + Packing.data.x = Scaled.x; + Packing.data.y = Scaled.y; + return Packing.pack; + } + + GLM_FUNC_QUALIFIER vec2 unpackSnorm2x8(uint16 p) + { + detail::snorm2x8 Packing; + Packing.pack = p; + vec2 Unpacked(Packing.data.x, Packing.data.y); + return clamp(Unpacked * float(0.00787401574803149606299212598425), -1.0f, 1.0f); + } + + GLM_FUNC_DECL uint16 packHalf1x16(float const & v) + { + detail::half1x16 Packing; + Packing.data = detail::toFloat16(v); + return Packing.pack; + } + + GLM_FUNC_DECL float unpackHalf1x16(uint16 const & v) + { + detail::half1x16 Packing; + Packing.pack = v; + return detail::toFloat32(Packing.data); + } + + GLM_FUNC_QUALIFIER uint32 packI10I10I10I2(ivec4 const & v) + { + detail::i10i10i10i2 Result; + Result.data.x = v.x; + Result.data.y = v.y; + Result.data.z = v.z; + Result.data.w = v.w; + return Result.pack; + } + + GLM_FUNC_QUALIFIER ivec4 unpackI10I10I10I2(uint32 const & v) + { + detail::i10i10i10i2 Unpack; + Unpack.pack = v; + return ivec4( + Unpack.data.x, + Unpack.data.y, + Unpack.data.z, + Unpack.data.w); + } + + GLM_FUNC_QUALIFIER uint32 packU10U10U10U2(uvec4 const & v) + { + detail::u10u10u10u2 Result; + Result.data.x = v.x; + Result.data.y = v.y; + Result.data.z = v.z; + Result.data.w = v.w; + return Result.pack; + } + + GLM_FUNC_QUALIFIER uvec4 unpackU10U10U10U2(uint32 const & v) + { + detail::u10u10u10u2 Unpack; + Unpack.pack = v; + return uvec4( + Unpack.data.x, + Unpack.data.y, + Unpack.data.z, + Unpack.data.w); + } + + GLM_FUNC_QUALIFIER uint32 packSnorm3x10_1x2(vec4 const & v) + { + detail::i10i10i10i2 Result; + Result.data.x = int(v.x * 511.f); + Result.data.y = int(v.y * 511.f); + Result.data.z = int(v.z * 511.f); + Result.data.w = int(v.w * 1.f); + return Result.pack; + } + + GLM_FUNC_QUALIFIER vec4 unpackSnorm3x10_1x2(uint32 const & v) + { + detail::i10i10i10i2 Unpack; + Unpack.pack = v; + vec4 Result; + Result.x = float(Unpack.data.x) / 511.f; + Result.y = float(Unpack.data.y) / 511.f; + Result.z = float(Unpack.data.z) / 511.f; + Result.w = float(Unpack.data.w) / 1.f; + return Result; + } + GLM_FUNC_QUALIFIER uint32 packF11F11F10(vec3 const & v) { return - ((detail::floatTo11bit(v.x) & ((1 << 11) - 1)) << 0) | + ((detail::floatTo11bit(v.x) & ((1 << 11) - 1)) << 0) | ((detail::floatTo11bit(v.y) & ((1 << 11) - 1)) << 11) | ((detail::floatTo10bit(v.z) & ((1 << 10) - 1)) << 22); } diff --git a/test/gtc/gtc_half_float.cpp b/test/gtc/gtc_half_float.cpp index f054f599..41266142 100644 --- a/test/gtc/gtc_half_float.cpp +++ b/test/gtc/gtc_half_float.cpp @@ -791,27 +791,27 @@ int test_half() { int Error = 0; - print_value(0.0); - print_value(0.1); - print_value(0.2); - print_value(0.3); - print_value(0.4); - print_value(0.5); - print_value(0.6); - print_value(1.0); - print_value(1.1); - print_value(1.2); - print_value(1.3); - print_value(1.4); - print_value(1.5); - print_value(1.6); - print_value(2.0); - print_value(2.1); - print_value(2.2); - print_value(2.3); - print_value(2.4); - print_value(2.5); - print_value(2.6); + print_value(0.0f); + print_value(0.1f); + print_value(0.2f); + print_value(0.3f); + print_value(0.4f); + print_value(0.5f); + print_value(0.6f); + print_value(1.0f); + print_value(1.1f); + print_value(1.2f); + print_value(1.3f); + print_value(1.4f); + print_value(1.5f); + print_value(1.6f); + print_value(2.0f); + print_value(2.1f); + print_value(2.2f); + print_value(2.3f); + print_value(2.4f); + print_value(2.5f); + print_value(2.6f); return Error; } diff --git a/test/gtc/gtc_packing.cpp b/test/gtc/gtc_packing.cpp index 7eaf5730..bd401792 100644 --- a/test/gtc/gtc_packing.cpp +++ b/test/gtc/gtc_packing.cpp @@ -29,6 +29,7 @@ #include #include +#include void print_bits(glm::half const & s) { @@ -96,10 +97,10 @@ void print_value(float const & s) print_bits(s); printf(", "); print_bits(glm::half(s)); - printf(", "); - print_11bits(detail::floatTo11bit(s)); - printf(", "); - print_10bits(detail::floatTo10bit(s)); +// printf(", "); +// print_11bits(detail::floatTo11bit(s)); +// printf(", "); +// print_10bits(detail::floatTo10bit(s)); printf("\n"); } @@ -107,27 +108,27 @@ int test_half() { int Error = 0; - print_value(0.0); - print_value(0.1); - print_value(0.2); - print_value(0.3); - print_value(0.4); - print_value(0.5); - print_value(0.6); - print_value(1.0); - print_value(1.1); - print_value(1.2); - print_value(1.3); - print_value(1.4); - print_value(1.5); - print_value(1.6); - print_value(2.0); - print_value(2.1); - print_value(2.2); - print_value(2.3); - print_value(2.4); - print_value(2.5); - print_value(2.6); + print_value(0.0f); + print_value(0.1f); + print_value(0.2f); + print_value(0.3f); + print_value(0.4f); + print_value(0.5f); + print_value(0.6f); + print_value(1.0f); + print_value(1.1f); + print_value(1.2f); + print_value(1.3f); + print_value(1.4f); + print_value(1.5f); + print_value(1.6f); + print_value(2.0f); + print_value(2.1f); + print_value(2.2f); + print_value(2.3f); + print_value(2.4f); + print_value(2.5f); + print_value(2.6f); return Error; }