diff --git a/glm/gtc/packing.hpp b/glm/gtc/packing.hpp index 16b8a02f..f44a48b0 100644 --- a/glm/gtc/packing.hpp +++ b/glm/gtc/packing.hpp @@ -494,6 +494,27 @@ namespace glm /// @see uint32 packF3x9_E1x5(vec3 const & v) GLM_FUNC_DECL vec3 unpackF3x9_E1x5(uint32 p); + /// Returns an unsigned integer vector obtained by converting the components of a floating-point vector + /// to the 16-bit floating-point representation found in the OpenGL Specification. + /// The first vector component specifies the 16 least-significant bits of the result; + /// the forth component specifies the 16 most-significant bits. + /// + /// @see gtc_packing + /// @see vecType unpackHalf(vecType const & p) + /// @see GLSL 4.20.8 specification, section 8.4 Floating-Point Pack and Unpack Functions + template class vecType> + GLM_FUNC_DECL vecType packHalf(vecType const & v); + + /// Returns a floating-point vector with components obtained by reinterpreting an integer vector as 16-bit floating-point numbers and converting them to 32-bit floating-point values. + /// The first component of the vector is obtained from the 16 least-significant bits of v; + /// the forth component is obtained from the 16 most-significant bits of v. + /// + /// @see gtc_packing + /// @see vecType packHalf(vecType const & v) + /// @see GLSL 4.20.8 specification, section 8.4 Floating-Point Pack and Unpack Functions + template class vecType> + GLM_FUNC_DECL vecType unpackHalf(vecType const & p); + /// @} }// namespace glm diff --git a/glm/gtc/packing.inl b/glm/gtc/packing.inl index 97a148a7..e390fffc 100644 --- a/glm/gtc/packing.inl +++ b/glm/gtc/packing.inl @@ -260,6 +260,85 @@ namespace detail uint32 pack; }; + template class vecType> + struct compute_half + {}; + + template + struct compute_half + { + GLM_FUNC_QUALIFIER static tvec1 pack(tvec1 const & v) + { + int16 const Topack(detail::toFloat16(v.x)); + return tvec1(reinterpret_cast(Topack)); + } + + GLM_FUNC_QUALIFIER static tvec1 unpack(tvec1 const & v) + { + return tvec1(detail::toFloat32(reinterpret_cast(v.x))); + } + }; + + template + struct compute_half + { + GLM_FUNC_QUALIFIER static tvec2 pack(tvec2 const & v) + { + return tvec2( + reinterpret_cast(detail::toFloat16(v.x)), + reinterpret_cast(detail::toFloat16(v.x))); + } + + GLM_FUNC_QUALIFIER static tvec2 unpack(tvec2 const & v) + { + return tvec2( + detail::toFloat32(reinterpret_cast(v.x)), + detail::toFloat32(reinterpret_cast(v.y))); + } + }; + + template + struct compute_half + { + GLM_FUNC_QUALIFIER static tvec3 pack(tvec3 const & v) + { + return tvec3( + reinterpret_cast(detail::toFloat16(v.x)), + reinterpret_cast(detail::toFloat16(v.y)), + reinterpret_cast(detail::toFloat16(v.z))); + } + + GLM_FUNC_QUALIFIER static tvec3 unpack(tvec3 const & v) + { + return tvec3( + detail::toFloat32(reinterpret_cast(v.x)), + detail::toFloat32(reinterpret_cast(v.y)), + detail::toFloat32(reinterpret_cast(v.z))); + } + }; + + template + struct compute_half + { + GLM_FUNC_QUALIFIER static tvec4 pack(tvec4 const & v) + { + tvec4 unpacked(detail::toFloat16(v.x), detail::toFloat16(v.y), detail::toFloat16(v.z), detail::toFloat16(v.w)); + return tvec4( + reinterpret_cast(unpacked.x), + reinterpret_cast(unpacked.y), + reinterpret_cast(unpacked.z), + reinterpret_cast(unpacked.w)); + } + + GLM_FUNC_QUALIFIER static tvec4 unpack(tvec4 const & v) + { + return tvec4( + detail::toFloat32(reinterpret_cast(v.x)), + detail::toFloat32(reinterpret_cast(v.y)), + detail::toFloat32(reinterpret_cast(v.z)), + detail::toFloat32(reinterpret_cast(v.w))); + } + }; }//namespace detail GLM_FUNC_QUALIFIER uint8 packUnorm1x8(float v) @@ -525,4 +604,16 @@ namespace detail return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * pow(2.0f, Unpack.data.w - 15.f - 9.f); } + + template class vecType> + GLM_FUNC_QUALIFIER vecType packHalf(vecType const & v) + { + return detail::compute_half::pack(v); + } + + template class vecType> + GLM_FUNC_QUALIFIER vecType unpackHalf(vecType const & v) + { + return detail::compute_half::unpack(v); + } }//namespace glm diff --git a/readme.md b/readme.md index 8ee56f88..1bb3505e 100644 --- a/readme.md +++ b/readme.md @@ -55,6 +55,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate) ##### Features: - Added compNormalize and compScale functions to GTX_component_wise - Added packF3x9_E1x5 and unpackF3x9_E1x5 to GTC_packing for RGB9E5 #416 +- Added packHalf and unpackHalf to GTC_packing ##### Improvements: - Improved GTC_random linearRand documentation diff --git a/test/gtc/gtc_packing.cpp b/test/gtc/gtc_packing.cpp index 000eafc5..11bf457c 100644 --- a/test/gtc/gtc_packing.cpp +++ b/test/gtc/gtc_packing.cpp @@ -128,7 +128,11 @@ int test_Half4x16() glm::vec4 v0 = glm::unpackHalf4x16(p0); glm::uint64 p1 = glm::packHalf4x16(v0); glm::vec4 v1 = glm::unpackHalf4x16(p1); + glm::u16vec4 p2 = glm::packHalf(v0); + glm::vec4 v2 = glm::unpackHalf(p2); + Error += glm::all(glm::equal(v0, v1)) ? 0 : 1; + Error += glm::all(glm::equal(v0, v2)) ? 0 : 1; } return Error; @@ -552,7 +556,7 @@ int main() Error += test_I3x10_1x2(); Error += test_U3x10_1x2(); Error += test_Half1x16(); - Error += test_U3x10_1x2(); + Error += test_Half4x16(); return Error; }