Explorar o código

Fixed GTC_packing *pack*norm*x* build and added tests #292

Christophe Riccio %!s(int64=11) %!d(string=hai) anos
pai
achega
d9290d7887
Modificáronse 6 ficheiros con 306 adicións e 56 borrados
  1. 8 10
      glm/detail/func_packing.inl
  2. 1 1
      glm/gtc/packing.hpp
  3. 31 39
      glm/gtc/packing.inl
  4. 1 0
      readme.txt
  5. 1 6
      test/core/core_func_packing.cpp
  6. 264 0
      test/gtc/gtc_packing.cpp

+ 8 - 10
glm/detail/func_packing.inl

@@ -40,8 +40,8 @@ namespace glm
 	{
 		u16vec2 Topack(round(clamp(v, 0.0f, 1.0f) * 65535.0f));
 		// return reinterpret_cast<uint&>(Topack);
-    uint* ptr(reinterpret_cast<uint*>(&Topack));
-    return *ptr;
+		uint* ptr(reinterpret_cast<uint*>(&Topack));
+		return *ptr;
 	}
 
 	GLM_FUNC_QUALIFIER vec2 unpackUnorm2x16(uint const & p)
@@ -52,15 +52,13 @@ namespace glm
 
 	GLM_FUNC_QUALIFIER uint packSnorm2x16(vec2 const & v)
 	{
-		i16vec2 Topack(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
-		// return reinterpret_cast<uint32&>(Topack);
-    uint* ptr(reinterpret_cast<uint*>(&Topack));
-    return *ptr;
+		i16vec2 const Topack(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
+		return reinterpret_cast<uint const &>(Topack);
 	}
 
 	GLM_FUNC_QUALIFIER vec2 unpackSnorm2x16(uint const & p)
 	{
-		vec2 Unpack(reinterpret_cast<i16vec2 const &>(p));
+		vec2 const Unpack(reinterpret_cast<i16vec2 const &>(p));
 		return clamp(
 			Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f,
 			-1.0f, 1.0f);
@@ -68,13 +66,13 @@ namespace glm
 
 	GLM_FUNC_QUALIFIER uint packUnorm4x8(vec4 const & v)
 	{
-		u8vec4 Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f));
-		return reinterpret_cast<uint&>(Topack);
+		u8vec4 const Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f));
+		return reinterpret_cast<uint const &>(Topack);
 	}
 
 	GLM_FUNC_QUALIFIER vec4 unpackUnorm4x8(uint const & p)
 	{
-		vec4 Unpack(reinterpret_cast<u8vec4 const&>(p));
+		vec4 const Unpack(reinterpret_cast<u8vec4 const&>(p));
 		return Unpack * float(0.0039215686274509803921568627451); // 1 / 255
 	}
 	

+ 1 - 1
glm/gtc/packing.hpp

@@ -283,7 +283,7 @@ namespace glm
 	/// @see vec2 unpackSnorm2x16(uint32 p)
 	/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/unpackSnorm2x16.xml">GLSL unpackSnorm4x8 man page</a>
 	/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.4 Floating-Point Pack and Unpack Functions</a>
-	GLM_FUNC_DECL vec4 unpackSnorm4x16(uint64 const & p);
+	GLM_FUNC_DECL vec4 unpackSnorm4x16(uint64 p);
 	
 	/// Returns an unsigned integer obtained by converting the components of a floating-point scalar
 	/// to the 16-bit floating-point representation found in the OpenGL Specification,

+ 31 - 39
glm/gtc/packing.inl

@@ -257,33 +257,31 @@ namespace detail
 	
 	GLM_FUNC_QUALIFIER float unpackUnorm1x8(uint8 p)
 	{
-		float Unpack(static_cast<float>(p));
+		float const Unpack(p);
 		return Unpack * static_cast<float>(0.0039215686274509803921568627451); // 1 / 255
 	}
 	
 	GLM_FUNC_QUALIFIER uint16 packUnorm2x8(vec2 const & v)
 	{
 		u8vec2 Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f));
-		uint16* Packed = reinterpret_cast<uint16*>(&Topack);
-		return *Packed;
+		return reinterpret_cast<uint16 const &>(Topack);
 	}
 	
 	GLM_FUNC_QUALIFIER vec2 unpackUnorm2x8(uint16 p)
 	{
-		u8vec2* Unpacked = reinterpret_cast<u8vec2*>(const_cast<uint16*>(&p));
-		return vec2(*Unpacked) * float(0.0039215686274509803921568627451); // 1 / 255
+		vec2 const Unpack(reinterpret_cast<u8vec2 const &>(p));
+		return Unpack * float(0.0039215686274509803921568627451); // 1 / 255
 	}
 
 	GLM_FUNC_QUALIFIER uint8 packSnorm1x8(float v)
 	{
-		int8 Topack(static_cast<int8>(round(clamp(v ,-1.0f, 1.0f) * 127.0f)));
-		uint8* Packed = reinterpret_cast<uint8*>(&Topack);
-		return *Packed;
+		int8 const Topack(static_cast<int8>(round(clamp(v ,-1.0f, 1.0f) * 127.0f)));
+		return reinterpret_cast<uint8 const &>(Topack);
 	}
 	
 	GLM_FUNC_QUALIFIER float unpackSnorm1x8(uint8 p)
 	{
-		float Unpack(static_cast<float>(*const_cast<uint8*>(&p)));
+		float const Unpack(reinterpret_cast<int8 const &>(p));
 		return clamp(
 			Unpack * 0.00787401574803149606299212598425f, // 1.0f / 127.0f
 			-1.0f, 1.0f);
@@ -291,16 +289,15 @@ namespace detail
 	
 	GLM_FUNC_QUALIFIER uint16 packSnorm2x8(vec2 const & v)
 	{
-		i8vec2 Topack(round(clamp(v ,-1.0f, 1.0f) * 127.0f));
-		uint16* Packed = reinterpret_cast<uint16*>(&Topack);
-		return *Packed;
+		i8vec2 const Topack(round(clamp(v, -1.0f, 1.0f) * 127.0f));
+		return reinterpret_cast<uint16 const &>(Topack);
 	}
 	
 	GLM_FUNC_QUALIFIER vec2 unpackSnorm2x8(uint16 p)
 	{
-		i8vec2* Unpack = reinterpret_cast<i8vec2*>(const_cast<uint16*>(&p));
+		vec2 const Unpack(reinterpret_cast<i8vec2 const &>(p));
 		return clamp(
-			vec2(*Unpack) * 0.00787401574803149606299212598425f, // 1.0f / 127.0f
+			Unpack * 0.00787401574803149606299212598425f, // 1.0f / 127.0f
 			-1.0f, 1.0f);
 	}
 	
@@ -311,33 +308,31 @@ namespace detail
 
 	GLM_FUNC_QUALIFIER float unpackUnorm1x16(uint16 p)
 	{
-		float Unpack = static_cast<float>(*const_cast<uint16*>(&p));
+		float const Unpack(p);
 		return Unpack * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0
 	}
 
 	GLM_FUNC_QUALIFIER uint64 packUnorm4x16(vec4 const & v)
 	{
-		u16vec4 Topack(round(clamp(v , 0.0f, 1.0f) * 65535.0f));
-		uint64* Packed = reinterpret_cast<uint64*>(&Topack);
-		return *Packed;
+		u16vec4 const Topack(round(clamp(v , 0.0f, 1.0f) * 65535.0f));
+		return reinterpret_cast<uint64 const &>(Topack);
 	}
 
 	GLM_FUNC_QUALIFIER vec4 unpackUnorm4x16(uint64 p)
 	{
-		u16vec4* Unpack = reinterpret_cast<u16vec4*>(const_cast<uint64*>(&p));
-		return vec4(*Unpack) * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0
+		vec4 const Unpack(reinterpret_cast<u16vec4 const &>(p));
+		return Unpack * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0
 	}
 
 	GLM_FUNC_QUALIFIER uint16 packSnorm1x16(float v)
 	{
-		int16 Topack = static_cast<int16>(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
-		uint16* Packed = reinterpret_cast<uint16*>(&Topack);
-		return *Packed;
+		int16 const Topack = static_cast<int16>(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
+		return reinterpret_cast<uint16 const &>(Topack);
 	}
 
 	GLM_FUNC_QUALIFIER float unpackSnorm1x16(uint16 p)
 	{
-		float Unpack = static_cast<float>(*const_cast<uint16*>(&p));
+		float const Unpack(reinterpret_cast<int16 const &>(p));
 		return clamp(
 			Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, 
 			-1.0f, 1.0f);
@@ -345,16 +340,15 @@ namespace detail
 
 	GLM_FUNC_QUALIFIER uint64 packSnorm4x16(vec4 const & v)
 	{
-		i16vec4 Topack = static_cast<i16vec4>(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
-		uint64* Packed = reinterpret_cast<uint64*>(&Topack);
-		return *Packed;
+		i16vec4 const Topack(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
+		return reinterpret_cast<uint64 const &>(Topack);
 	}
 
 	GLM_FUNC_QUALIFIER vec4 unpackSnorm4x16(uint64 p)
 	{
-		i16vec4* Unpack(reinterpret_cast<i16vec4*>(const_cast<uint64*>(&p)));
+		vec4 const Unpack(reinterpret_cast<i16vec4 const &>(p));
 		return clamp(
-			vec4(*Unpack) * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f,
+			Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f,
 			-1.0f, 1.0f);
 	}
 
@@ -379,19 +373,17 @@ namespace detail
 			detail::toFloat16(v.z),
 			detail::toFloat16(v.w));
 
-		uint64* Packed = reinterpret_cast<uint64*>(&Unpack);
-		return *Packed;
+		return reinterpret_cast<uint64 const &>(Unpack);
 	}
 
 	GLM_FUNC_QUALIFIER glm::vec4 unpackHalf4x16(uint64 v)
 	{
-		i16vec4* p = reinterpret_cast<i16vec4*>(const_cast<uint64*>(&v));
-		i16vec4 Unpack(*p);
+		i16vec4 Unpack(reinterpret_cast<i16vec4 const &>(v));
 	
 		return vec4(
-			detail::toFloat32(Unpack.x), 
-			detail::toFloat32(Unpack.y), 
-			detail::toFloat32(Unpack.z), 
+			detail::toFloat32(Unpack.x),
+			detail::toFloat32(Unpack.y),
+			detail::toFloat32(Unpack.z),
 			detail::toFloat32(Unpack.w));
 	}
 
@@ -483,7 +475,7 @@ namespace detail
 
 	GLM_FUNC_QUALIFIER uint32 packF2x11_1x10(vec3 const & v)
 	{
-		return 
+		return
 			((detail::floatTo11bit(v.x) & ((1 << 11) - 1)) <<  0) |
 			((detail::floatTo11bit(v.y) & ((1 << 11) - 1)) << 11) |
 			((detail::floatTo10bit(v.z) & ((1 << 10) - 1)) << 22);
@@ -492,8 +484,8 @@ namespace detail
 	GLM_FUNC_QUALIFIER vec3 unpackF2x11_1x10(uint32 v)
 	{
 		return vec3(
-			detail::packed11bitToFloat(v >> 0), 
-			detail::packed11bitToFloat(v >> 11), 
+			detail::packed11bitToFloat(v >> 0),
+			detail::packed11bitToFloat(v >> 11),
 			detail::packed10bitToFloat(v >> 22));
 	}
 

+ 1 - 0
readme.txt

@@ -80,6 +80,7 @@ Fixes:
 - Fixed mat4x3 = mat2x3 * mat4x2 operator #297
 - Fixed warnings in F2x11_1x10 packing function in GTC_packing #295
 - Fixed Visual Studio natvis support for vec4 #288
+- Fixed GTC_packing *pack*norm*x* build and added tests #292
 
 ================================================================================
 GLM 0.9.6.1: 2014-12-10

+ 1 - 6
test/core/core_func_packing.cpp

@@ -38,12 +38,7 @@
 int test_packUnorm2x16()
 {
 	int Error = 0;
-/*	
-	std::vector<glm::hvec2> A;
-	A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 0.0f)));
-	A.push_back(glm::hvec2(glm::half( 0.5f), glm::half( 0.7f)));
-	A.push_back(glm::hvec2(glm::half( 0.1f), glm::half( 0.2f)));
-*/
+
 	std::vector<glm::vec2> A;
 	A.push_back(glm::vec2(1.0f, 0.0f));
 	A.push_back(glm::vec2(0.5f, 0.7f));

+ 264 - 0
test/gtc/gtc_packing.cpp

@@ -30,6 +30,7 @@
 ///////////////////////////////////////////////////////////////////////////////////
 
 #include <glm/gtc/packing.hpp>
+#include <glm/gtc/epsilon.hpp>
 #include <cstdio>
 #include <vector>
 
@@ -253,10 +254,273 @@ int test_F2x11_1x10()
 	return Error;
 }
 
+int test_packUnorm1x16()
+{
+	int Error = 0;
+
+	std::vector<glm::vec1> A;
+	A.push_back(glm::vec1(1.0f));
+	A.push_back(glm::vec1(0.5f));
+	A.push_back(glm::vec1(0.1f));
+	A.push_back(glm::vec1(0.0f));
+
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec1 B(A[i]);
+		glm::uint32 C = glm::packUnorm1x16(B.x);
+		glm::vec1 D(glm::unpackUnorm1x16(C));
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 65535.f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
+int test_packSnorm1x16()
+{
+	int Error = 0;
+
+	std::vector<glm::vec1> A;
+	A.push_back(glm::vec1( 1.0f));
+	A.push_back(glm::vec1( 0.0f));
+	A.push_back(glm::vec1(-0.5f));
+	A.push_back(glm::vec1(-0.1f));
+
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec1 B(A[i]);
+		glm::uint32 C = glm::packSnorm1x16(B.x);
+		glm::vec1 D(glm::unpackSnorm1x16(C));
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1;
+	}
+	
+	return Error;
+}
+
+int test_packUnorm2x16()
+{
+	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.2f));
+
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec2 B(A[i]);
+		glm::uint32 C = glm::packUnorm2x16(B);
+		glm::vec2 D = glm::unpackUnorm2x16(C);
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 65535.f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
+int test_packSnorm2x16()
+{
+	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::uint32 C = glm::packSnorm2x16(B);
+		glm::vec2 D = glm::unpackSnorm2x16(C);
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
+int test_packUnorm4x16()
+{
+	int Error = 0;
+
+	std::vector<glm::vec4> A;
+	A.push_back(glm::vec4(1.0f));
+	A.push_back(glm::vec4(0.5f));
+	A.push_back(glm::vec4(0.1f));
+	A.push_back(glm::vec4(0.0f));
+
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec4 B(A[i]);
+		glm::uint64 C = glm::packUnorm4x16(B);
+		glm::vec4 D(glm::unpackUnorm4x16(C));
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 65535.f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
+int test_packSnorm4x16()
+{
+	int Error = 0;
+
+	std::vector<glm::vec4> A;
+	A.push_back(glm::vec4( 1.0f, 0.0f, -0.5f, 0.5f));
+	A.push_back(glm::vec4(-0.3f,-0.7f,  0.3f, 0.7f));
+	A.push_back(glm::vec4(-0.1f, 0.1f, -0.2f, 0.2f));
+
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec4 B(A[i]);
+		glm::uint64 C = glm::packSnorm4x16(B);
+		glm::vec4 D(glm::unpackSnorm4x16(C));
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
+int test_packUnorm1x8()
+{
+	int Error = 0;
+	
+	std::vector<glm::vec1> A;
+	A.push_back(glm::vec1(1.0f));
+	A.push_back(glm::vec1(0.5f));
+	A.push_back(glm::vec1(0.0f));
+	
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec1 B(A[i]);
+		glm::uint16 C = glm::packUnorm1x8(B.x);
+		glm::vec1 D(glm::unpackUnorm1x8(C));
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
+int test_packSnorm1x8()
+{
+	int Error = 0;
+	
+	std::vector<glm::vec1> A;
+	A.push_back(glm::vec1( 1.0f));
+	A.push_back(glm::vec1(-0.7f));
+	
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec1 B(A[i]);
+		glm::uint16 C = glm::packSnorm1x8(B.x);
+		glm::vec1 D(glm::unpackSnorm1x8(C));
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
+	}
+	
+	return Error;
+}
+
+int test_packUnorm2x8()
+{
+	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::uint16 C = glm::packUnorm2x8(B);
+		glm::vec2 D = glm::unpackUnorm2x8(C);
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
+int test_packSnorm2x8()
+{
+	int Error = 0;
+	
+	std::vector<glm::vec2> A;
+	A.push_back(glm::vec2( 1.0f, 0.0f));
+	A.push_back(glm::vec2(-0.7f,-0.1f));
+	
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec2 B(A[i]);
+		glm::uint16 C = glm::packSnorm2x8(B);
+		glm::vec2 D = glm::unpackSnorm2x8(C);
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
+	}
+	
+	return Error;
+}
+
+int test_packUnorm4x8()
+{
+	int Error = 0;
+	
+	std::vector<glm::vec4> 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::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
+int test_packSnorm4x8()
+{
+	int Error = 0;
+	
+	std::vector<glm::vec4> 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::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
+		assert(!Error);
+	}
+	
+	return Error;
+}
+
 int main()
 {
 	int Error(0);
 
+	Error += test_packSnorm1x16();
+	Error += test_packSnorm2x16();
+	Error += test_packSnorm4x16();
+
+	Error += test_packSnorm1x8();
+	Error += test_packSnorm2x8();
+	Error += test_packSnorm4x8();
+
+	Error += test_packUnorm1x16();
+	Error += test_packUnorm2x16();
+	Error += test_packUnorm4x16();
+
+	Error += test_packUnorm1x8();
+	Error += test_packUnorm2x8();
+	Error += test_packUnorm4x8();
+
 	Error += test_F2x11_1x10();
 	Error += test_Snorm3x10_1x2();
 	Error += test_Unorm3x10_1x2();