Fixed and tested pack/unpackSnorm4x8 functions
This commit is contained in:
parent
0ab221458f
commit
321f89ff7a
@ -85,15 +85,6 @@ GLM_FUNC_QUALIFIER detail::uint32 packUnorm4x8(detail::tvec4<detail::float32> co
|
||||
return detail::uint32((D << 24) | (C << 16) | (B << 8) | A);
|
||||
}
|
||||
|
||||
GLM_FUNC_QUALIFIER detail::uint32 packSnorm4x8(detail::tvec4<detail::float32> 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);
|
||||
}
|
||||
|
||||
GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> unpackUnorm4x8(detail::uint32 const & p)
|
||||
{
|
||||
detail::uint8 A(detail::uint8(p >> 0));
|
||||
@ -107,17 +98,39 @@ GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> unpackUnorm4x8(detail::uint32
|
||||
D * 1.0f / 255.0f);
|
||||
}
|
||||
|
||||
GLM_FUNC_QUALIFIER detail::uint32 packSnorm4x8(detail::tvec4<detail::float32> const & v)
|
||||
{
|
||||
union iu
|
||||
{
|
||||
detail::int8 i;
|
||||
detail::uint8 u;
|
||||
} A, B, C, D;
|
||||
|
||||
detail::tvec4<detail::float32> 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<detail::float32> 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<detail::float32>(
|
||||
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<detail::uint32> const & v)
|
||||
|
@ -55,6 +55,46 @@ int test_packSnorm2x16()
|
||||
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::equalEpsilon(B, D, 0.0001f)) ? 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::equalEpsilon(B, D, 0.0001f)) ? 0 : 1;
|
||||
//assert(!Error);
|
||||
}
|
||||
|
||||
return Error;
|
||||
}
|
||||
|
||||
int test_packHalf2x16()
|
||||
{
|
||||
int Error = 0;
|
||||
@ -99,6 +139,8 @@ int main()
|
||||
{
|
||||
int Error = 0;
|
||||
|
||||
Error += test_packSnorm4x8();
|
||||
Error += test_packUnorm4x8();
|
||||
Error += test_packSnorm2x16();
|
||||
Error += test_packUnorm2x16();
|
||||
Error += test_packHalf2x16();
|
||||
|
Loading…
x
Reference in New Issue
Block a user