Fixed and tested pack/unpackSnorm4x8 functions
This commit is contained in:
parent
0ab221458f
commit
321f89ff7a
@ -78,24 +78,15 @@ GLM_FUNC_QUALIFIER detail::tvec2<detail::float32> unpackSnorm2x16(detail::uint32
|
|||||||
|
|
||||||
GLM_FUNC_QUALIFIER detail::uint32 packUnorm4x8(detail::tvec4<detail::float32> const & v)
|
GLM_FUNC_QUALIFIER detail::uint32 packUnorm4x8(detail::tvec4<detail::float32> const & v)
|
||||||
{
|
{
|
||||||
detail::uint8 A((detail::uint8)round(clamp(v.x, 0.0f, 1.0f) * 255.0f));
|
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 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 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));
|
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);
|
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)
|
GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> unpackUnorm4x8(detail::uint32 const & p)
|
||||||
{
|
{
|
||||||
detail::uint8 A(detail::uint8(p >> 0));
|
detail::uint8 A(detail::uint8(p >> 0));
|
||||||
detail::uint8 B(detail::uint8(p >> 8));
|
detail::uint8 B(detail::uint8(p >> 8));
|
||||||
detail::uint8 C(detail::uint8(p >> 16));
|
detail::uint8 C(detail::uint8(p >> 16));
|
||||||
@ -106,18 +97,40 @@ GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> unpackUnorm4x8(detail::uint32
|
|||||||
C * 1.0f / 255.0f,
|
C * 1.0f / 255.0f,
|
||||||
D * 1.0f / 255.0f);
|
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)
|
GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> unpackSnorm4x8(detail::uint32 const & p)
|
||||||
{
|
{
|
||||||
detail::uint8 A(detail::uint8(p >> 0));
|
union iu
|
||||||
detail::uint8 B(detail::uint8(p >> 8));
|
{
|
||||||
detail::uint8 C(detail::uint8(p >> 16));
|
detail::int8 i;
|
||||||
detail::uint8 D(detail::uint8(p >> 24));
|
detail::uint8 u;
|
||||||
return clamp(detail::tvec4<detail::float32>(
|
} A, B, C, D;
|
||||||
A * 1.0f / 127.0f,
|
|
||||||
B * 1.0f / 127.0f,
|
detail::uint32 Mask8((1 << 8) - 1);
|
||||||
C * 1.0f / 127.0f,
|
A.u = detail::uint8((p >> 0) & Mask8);
|
||||||
D * 1.0f / 127.0f), -1.0f, 1.0f);
|
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)
|
GLM_FUNC_QUALIFIER double packDouble2x32(detail::tvec2<detail::uint32> const & v)
|
||||||
|
@ -55,6 +55,46 @@ int test_packSnorm2x16()
|
|||||||
return Error;
|
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 test_packHalf2x16()
|
||||||
{
|
{
|
||||||
int Error = 0;
|
int Error = 0;
|
||||||
@ -99,11 +139,13 @@ int main()
|
|||||||
{
|
{
|
||||||
int Error = 0;
|
int Error = 0;
|
||||||
|
|
||||||
|
Error += test_packSnorm4x8();
|
||||||
|
Error += test_packUnorm4x8();
|
||||||
Error += test_packSnorm2x16();
|
Error += test_packSnorm2x16();
|
||||||
Error += test_packUnorm2x16();
|
Error += test_packUnorm2x16();
|
||||||
Error += test_packHalf2x16();
|
Error += test_packHalf2x16();
|
||||||
Error += test_packDouble2x32();
|
Error += test_packDouble2x32();
|
||||||
|
|
||||||
return Error;
|
return Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user