diff --git a/glm/core/func_packing.inl b/glm/core/func_packing.inl index 69c4abdd..5acbe96e 100644 --- a/glm/core/func_packing.inl +++ b/glm/core/func_packing.inl @@ -78,24 +78,15 @@ GLM_FUNC_QUALIFIER detail::tvec2 unpackSnorm2x16(detail::uint32 GLM_FUNC_QUALIFIER detail::uint32 packUnorm4x8(detail::tvec4 const & v) { - detail::uint8 A((detail::uint8)round(clamp(v.x, 0.0f, 1.0f) * 255.0f)); - detail::uint8 B((detail::uint8)round(clamp(v.y, 0.0f, 1.0f) * 255.0f)); - detail::uint8 C((detail::uint8)round(clamp(v.z, 0.0f, 1.0f) * 255.0f)); - detail::uint8 D((detail::uint8)round(clamp(v.w, 0.0f, 1.0f) * 255.0f)); - return detail::uint32((D << 24) | (C << 16) | (B << 8) | A); -} - -GLM_FUNC_QUALIFIER detail::uint32 packSnorm4x8(detail::tvec4 const & v) -{ - detail::uint8 A((detail::uint8)round(clamp(v.x,-1.0f, 1.0f) * 255.0f)); - detail::uint8 B((detail::uint8)round(clamp(v.y,-1.0f, 1.0f) * 255.0f)); - detail::uint8 C((detail::uint8)round(clamp(v.z,-1.0f, 1.0f) * 255.0f)); - detail::uint8 D((detail::uint8)round(clamp(v.w,-1.0f, 1.0f) * 255.0f)); - return detail::uint32((D << 24) | (C << 16) | (B << 8) | A); + detail::uint8 A((detail::uint8)round(clamp(v.x, 0.0f, 1.0f) * 255.0f)); + detail::uint8 B((detail::uint8)round(clamp(v.y, 0.0f, 1.0f) * 255.0f)); + detail::uint8 C((detail::uint8)round(clamp(v.z, 0.0f, 1.0f) * 255.0f)); + detail::uint8 D((detail::uint8)round(clamp(v.w, 0.0f, 1.0f) * 255.0f)); + return detail::uint32((D << 24) | (C << 16) | (B << 8) | A); } GLM_FUNC_QUALIFIER detail::tvec4 unpackUnorm4x8(detail::uint32 const & p) -{ +{ detail::uint8 A(detail::uint8(p >> 0)); detail::uint8 B(detail::uint8(p >> 8)); detail::uint8 C(detail::uint8(p >> 16)); @@ -106,18 +97,40 @@ GLM_FUNC_QUALIFIER detail::tvec4 unpackUnorm4x8(detail::uint32 C * 1.0f / 255.0f, D * 1.0f / 255.0f); } + +GLM_FUNC_QUALIFIER detail::uint32 packSnorm4x8(detail::tvec4 const & v) +{ + union iu + { + detail::int8 i; + detail::uint8 u; + } A, B, C, D; + + detail::tvec4 Unpack = clamp(v ,-1.0f, 1.0f) * 127.0f; + A.i = detail::int8(round(Unpack.x)); + B.i = detail::int8(round(Unpack.y)); + C.i = detail::int8(round(Unpack.z)); + D.i = detail::int8(round(Unpack.w)); + detail::uint32 Pack = (detail::uint32(D.u) << 24) | (detail::uint32(C.u) << 16) | (detail::uint32(B.u) << 8) | (detail::uint32(A.u) << 0); + return Pack; +} GLM_FUNC_QUALIFIER detail::tvec4 unpackSnorm4x8(detail::uint32 const & p) { - detail::uint8 A(detail::uint8(p >> 0)); - detail::uint8 B(detail::uint8(p >> 8)); - detail::uint8 C(detail::uint8(p >> 16)); - detail::uint8 D(detail::uint8(p >> 24)); - return clamp(detail::tvec4( - A * 1.0f / 127.0f, - B * 1.0f / 127.0f, - C * 1.0f / 127.0f, - D * 1.0f / 127.0f), -1.0f, 1.0f); + union iu + { + detail::int8 i; + detail::uint8 u; + } A, B, C, D; + + detail::uint32 Mask8((1 << 8) - 1); + A.u = detail::uint8((p >> 0) & Mask8); + B.u = detail::uint8((p >> 8) & Mask8); + C.u = detail::uint8((p >> 16) & Mask8); + D.u = detail::uint8((p >> 24) & Mask8); + vec4 Pack(A.i, B.i, C.i, D.i); + + return clamp(Pack, -1.0f, 1.0f); } GLM_FUNC_QUALIFIER double packDouble2x32(detail::tvec2 const & v) diff --git a/test/core/core_func_packing.cpp b/test/core/core_func_packing.cpp index 8b227249..19a1f4b4 100644 --- a/test/core/core_func_packing.cpp +++ b/test/core/core_func_packing.cpp @@ -55,6 +55,46 @@ int test_packSnorm2x16() 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::equalEpsilon(B, D, 0.0001f)) ? 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::equalEpsilon(B, D, 0.0001f)) ? 0 : 1; + //assert(!Error); + } + + return Error; +} + int test_packHalf2x16() { int Error = 0; @@ -99,11 +139,13 @@ int main() { int Error = 0; + Error += test_packSnorm4x8(); + Error += test_packUnorm4x8(); Error += test_packSnorm2x16(); Error += test_packUnorm2x16(); Error += test_packHalf2x16(); Error += test_packDouble2x32(); - + return Error; }