From 560dcdbec0b0d55159fe67e9c373c6b5c7c1f5f0 Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Mon, 12 Oct 2015 01:03:01 +0200 Subject: [PATCH] Added 8bit pack and unpack to GTC_packing --- glm/gtc/packing.hpp | 11 +++++++++++ glm/gtc/packing.inl | 29 +++++++++++++++++++++++++++++ readme.md | 1 + test/gtc/gtc_packing.cpp | 21 +++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/glm/gtc/packing.hpp b/glm/gtc/packing.hpp index 1d03aec3..b016eb15 100644 --- a/glm/gtc/packing.hpp +++ b/glm/gtc/packing.hpp @@ -591,6 +591,17 @@ namespace glm /// @see uint16 packUnorm3x5_1x1(vec4 const & v) GLM_FUNC_DECL vec4 unpackUnorm3x5_1x1(uint16 p); + /// Convert each component of the normalized floating-point vector into unsigned integer values. + /// + /// @see gtc_packing + /// @see vec3 unpackUnorm2x3_1x2(uint8 p) + GLM_FUNC_DECL uint8 packUnorm2x3_1x2(vec3 const & v); + + /// Convert each unsigned integer components of a vector to normalized floating-point values. + /// + /// @see gtc_packing + /// @see uint8 packUnorm2x3_1x2(vec3 const & v) + GLM_FUNC_DECL vec3 unpackUnorm2x3_1x2(uint8 p); /// @} }// namespace glm diff --git a/glm/gtc/packing.inl b/glm/gtc/packing.inl index 2aab46fc..5c6667af 100644 --- a/glm/gtc/packing.inl +++ b/glm/gtc/packing.inl @@ -225,6 +225,17 @@ namespace detail // return ((floatTo11bit(x) & ((1 << 11) - 1)) << 0) | ((floatTo11bit(y) & ((1 << 11) - 1)) << 11) | ((floatTo10bit(z) & ((1 << 10) - 1)) << 22); // } + union u3u3u2 + { + struct + { + uint x : 3; + uint y : 3; + uint z : 2; + } data; + uint8 pack; + }; + union u4u4 { struct @@ -773,5 +784,23 @@ namespace detail Unpack.pack = v; return vec4(Unpack.data.x, Unpack.data.y, Unpack.data.z, Unpack.data.w) * ScaleFactor; } + + GLM_FUNC_QUALIFIER uint8 packUnorm2x3_1x2(vec3 const & v) + { + u32vec3 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec3(7.f, 7.f, 3.f))); + detail::u3u3u2 Result; + Result.data.x = Unpack.x; + Result.data.y = Unpack.y; + Result.data.z = Unpack.z; + return Result.pack; + } + + GLM_FUNC_QUALIFIER vec3 unpackUnorm2x3_1x2(uint8 v) + { + vec3 const ScaleFactor(1.f / 7.f, 1.f / 7.f, 1.f / 3.f); + detail::u3u3u2 Unpack; + Unpack.pack = v; + return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * ScaleFactor; + } }//namespace glm diff --git a/readme.md b/readme.md index 46fd15ca..67b4566f 100644 --- a/readme.md +++ b/readme.md @@ -58,6 +58,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate) - Added (un)packHalf to GTC_packing - Added (un)packUnorm and (un)packSnorm to GTC_packing - Added 16bit pack and unpack to GTC_packing +- Added 8bit pack and unpack to GTC_packing ##### Improvements: - Improved GTC_random linearRand documentation diff --git a/test/gtc/gtc_packing.cpp b/test/gtc/gtc_packing.cpp index a3c4250f..2f7a1c3e 100644 --- a/test/gtc/gtc_packing.cpp +++ b/test/gtc/gtc_packing.cpp @@ -650,6 +650,26 @@ int test_packUnorm1x5_1x6_1x5() return Error; } +int test_packUnorm2x3_1x2() +{ + int Error = 0; + + std::vector A; + A.push_back(glm::vec3(1.0f, 0.7f, 0.5f)); + A.push_back(glm::vec3(0.5f, 0.1f, 0.0f)); + + for(std::size_t i = 0; i < A.size(); ++i) + { + glm::vec3 B(A[i]); + glm::uint8 C = glm::packUnorm2x3_1x2(B); + glm::vec3 D = glm::unpackUnorm2x3_1x2(C); + Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 3.f)) ? 0 : 1; + assert(!Error); + } + + return Error; +} + int main() { int Error = 0; @@ -677,6 +697,7 @@ int main() Error += test_packUnorm4x4(); Error += test_packUnorm3x5_1x1(); Error += test_packUnorm1x5_1x6_1x5(); + Error += test_packUnorm2x3_1x2(); Error += test_F2x11_1x10(); Error += test_F3x9_E1x5();