Pārlūkot izejas kodu

Added (un)packUnorm and (un)packSnorm to GTC_packing

Christophe Riccio 10 gadi atpakaļ
vecāks
revīzija
04c8f05a34
3 mainītis faili ar 110 papildinājumiem un 1 dzēšanām
  1. 28 0
      glm/gtc/packing.hpp
  2. 38 1
      glm/gtc/packing.inl
  3. 44 0
      test/gtc/gtc_packing.cpp

+ 28 - 0
glm/gtc/packing.hpp

@@ -515,6 +515,34 @@ namespace glm
 	template <precision P, template <typename, precision> class vecType>
 	template <precision P, template <typename, precision> class vecType>
 	GLM_FUNC_DECL vecType<float, P> unpackHalf(vecType<uint16, P> const & p);
 	GLM_FUNC_DECL vecType<float, P> unpackHalf(vecType<uint16, P> const & p);
 
 
+	/// Convert each component of the normalized floating-point vector into unsigned integer values.
+	///
+	/// @see gtc_packing
+	/// @see vecType<floatType, P> unpackUnorm(vecType<intType, P> const & p);
+	template <typename uintType, typename floatType, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_DECL vecType<uintType, P> packUnorm(vecType<floatType, P> const & v);
+
+	/// Convert each unsigned integer components of a vector to normalized floating-point values.
+	/// 
+	/// @see gtc_packing
+	/// @see vecType<intType, P> packUnorm(vecType<floatType, P> const & v)
+	template <typename uintType, typename floatType, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_DECL vecType<floatType, P> unpackUnorm(vecType<uintType, P> const & v);
+
+	/// Convert each component of the normalized floating-point vector into signed integer values.
+	///
+	/// @see gtc_packing
+	/// @see vecType<floatType, P> unpackSnorm(vecType<intType, P> const & p);
+	template <typename intType, typename floatType, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_DECL vecType<intType, P> packSnorm(vecType<floatType, P> const & v);
+
+	/// Convert each signed integer components of a vector to normalized floating-point values.
+	/// 
+	/// @see gtc_packing
+	/// @see vecType<intType, P> packSnorm(vecType<floatType, P> const & v)
+	template <typename intType, typename floatType, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_DECL vecType<floatType, P> unpackSnorm(vecType<intType, P> const & v);
+
 	/// @}
 	/// @}
 }// namespace glm
 }// namespace glm
 
 

+ 38 - 1
glm/gtc/packing.inl

@@ -36,6 +36,7 @@
 #include "../vec4.hpp"
 #include "../vec4.hpp"
 #include "../detail/type_half.hpp"
 #include "../detail/type_half.hpp"
 #include <cstring>
 #include <cstring>
+#include <limits>
 
 
 namespace glm{
 namespace glm{
 namespace detail
 namespace detail
@@ -391,7 +392,7 @@ namespace detail
 			Unpack * 0.00787401574803149606299212598425f, // 1.0f / 127.0f
 			Unpack * 0.00787401574803149606299212598425f, // 1.0f / 127.0f
 			-1.0f, 1.0f);
 			-1.0f, 1.0f);
 	}
 	}
-	
+
 	GLM_FUNC_QUALIFIER uint16 packUnorm1x16(float s)
 	GLM_FUNC_QUALIFIER uint16 packUnorm1x16(float s)
 	{
 	{
 		return static_cast<uint16>(round(clamp(s, 0.0f, 1.0f) * 65535.0f));
 		return static_cast<uint16>(round(clamp(s, 0.0f, 1.0f) * 65535.0f));
@@ -616,4 +617,40 @@ namespace detail
 	{
 	{
 		return detail::compute_half<P, vecType>::unpack(v);
 		return detail::compute_half<P, vecType>::unpack(v);
 	}
 	}
+
+	template <typename uintType, typename floatType, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<uintType, P> packUnorm(vecType<floatType, P> const & v)
+	{
+		static_assert(std::numeric_limits<uintType>::is_integer, "uintType must be an integer type");
+		static_assert(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
+
+		return vecType<uintType, P>(round(clamp(v, static_cast<floatType>(0), static_cast<floatType>(1)) * static_cast<floatType>(std::numeric_limits<uintType>::max())));
+	}
+
+	template <typename uintType, typename floatType, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<floatType, P> unpackUnorm(vecType<uintType, P> const & v)
+	{
+		static_assert(std::numeric_limits<uintType>::is_integer, "uintType must be an integer type");
+		static_assert(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
+
+		return vecType<float, P>(v) * (static_cast<floatType>(1) / static_cast<floatType>(std::numeric_limits<uintType>::max()));
+	}
+
+	template <typename intType, typename floatType, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<intType, P> packSnorm(vecType<floatType, P> const & v)
+	{
+		static_assert(std::numeric_limits<intType>::is_integer, "uintType must be an integer type");
+		static_assert(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
+
+		return vecType<intType, P>(round(clamp(v , static_cast<floatType>(-1), static_cast<floatType>(1)) * static_cast<floatType>(std::numeric_limits<intType>::max())));
+	}
+
+	template <typename intType, typename floatType, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<floatType, P> unpackSnorm(vecType<intType, P> const & v)
+	{
+		static_assert(std::numeric_limits<intType>::is_integer, "uintType must be an integer type");
+		static_assert(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
+
+		return clamp(vecType<floatType, P>(v) * (static_cast<floatType>(1) / static_cast<floatType>(std::numeric_limits<intType>::max())), static_cast<floatType>(-1), static_cast<floatType>(1));
+	}
 }//namespace glm
 }//namespace glm

+ 44 - 0
test/gtc/gtc_packing.cpp

@@ -529,10 +529,54 @@ int test_packSnorm4x8()
 	return Error;
 	return Error;
 }
 }
 
 
+int test_packUnorm()
+{
+	int Error = 0;
+
+	std::vector<glm::vec2> 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::u16vec2 C = glm::packUnorm<glm::uint16>(B);
+		glm::vec2 D = glm::unpackUnorm<glm::uint16, float>(C);
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1;
+		assert(!Error);
+	}
+
+	return Error;
+}
+
+int test_packSnorm()
+{
+	int Error = 0;
+
+	std::vector<glm::vec2> 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::i16vec2 C = glm::packSnorm<glm::int16>(B);
+		glm::vec2 D = glm::unpackSnorm<glm::int16, float>(C);
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1;
+		assert(!Error);
+	}
+
+	return Error;
+}
+
 int main()
 int main()
 {
 {
 	int Error(0);
 	int Error(0);
 
 
+	Error += test_packUnorm();
+	Error += test_packSnorm();
+
 	Error += test_packSnorm1x16();
 	Error += test_packSnorm1x16();
 	Error += test_packSnorm2x16();
 	Error += test_packSnorm2x16();
 	Error += test_packSnorm4x16();
 	Error += test_packSnorm4x16();