Added GTC_packing implementation

This commit is contained in:
Christophe Riccio 2013-08-11 02:16:47 +02:00
parent cf14790806
commit a105876133
6 changed files with 333 additions and 74 deletions

View File

@ -40,9 +40,7 @@ namespace glm
uint32 Mask16((1 << 16) - 1); uint32 Mask16((1 << 16) - 1);
uint32 A((p >> 0) & Mask16); uint32 A((p >> 0) & Mask16);
uint32 B((p >> 16) & Mask16); uint32 B((p >> 16) & Mask16);
return vec2( return vec2(A, B) * float(1.5259021896696421759365224689097e-5); // 1.0 / 65535.0
A * 1.0f / 65535.0f,
B * 1.0f / 65535.0f);
} }
GLM_FUNC_QUALIFIER uint32 packSnorm2x16(vec2 const & v) GLM_FUNC_QUALIFIER uint32 packSnorm2x16(vec2 const & v)
@ -53,9 +51,9 @@ namespace glm
uint16 u; uint16 u;
} A, B; } A, B;
vec2 Unpack = clamp(v ,-1.0f, 1.0f) * 32767.0f; vec2 Unpack = round(clamp(v ,-1.0f, 1.0f) * 32767.0f);
A.i = detail::int16(round(Unpack.x)); A.i = detail::int16(Unpack.x);
B.i = detail::int16(round(Unpack.y)); B.i = detail::int16(Unpack.y);
uint32 Pack = (uint32(B.u) << 16) | (uint32(A.u) << 0); uint32 Pack = (uint32(B.u) << 16) | (uint32(A.u) << 0);
return Pack; return Pack;
} }

View File

@ -52,8 +52,9 @@ namespace glm
/// ///
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/lessThan.xml">GLSL lessThan man page</a> /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/lessThan.xml">GLSL lessThan man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.7 Vector Relational Functions</a> /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.7 Vector Relational Functions</a>
template <typename T, precision P, template <typename, precision> class vecType> // TODO: Mismatched
GLM_FUNC_DECL typename vecType<T, P>::bool_type lessThan(vecType<T, P> const & x, vecType<T, P> const & y); //template <typename T, precision P, template <typename, precision> class vecType>
//GLM_FUNC_DECL typename vecType<T, P>::bool_type lessThan(vecType<T, P> const & x, vecType<T, P> const & y);
/// Returns the component-wise comparison of result x <= y. /// Returns the component-wise comparison of result x <= y.
/// ///
@ -88,8 +89,9 @@ namespace glm
/// ///
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/equal.xml">GLSL equal man page</a> /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/equal.xml">GLSL equal man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.7 Vector Relational Functions</a> /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.7 Vector Relational Functions</a>
template <typename T, precision P, template <typename, precision> class vecType> //TODO: conflicts with definision
GLM_FUNC_DECL typename vecType<T, P>::bool_type equal(vecType<T, P> const & x, vecType<T, P> const & y); //template <typename T, precision P, template <typename, precision> class vecType>
//GLM_FUNC_DECL typename vecType<T, P>::bool_type equal(vecType<T, P> const & x, vecType<T, P> const & y);
/// Returns the component-wise comparison of result x != y. /// Returns the component-wise comparison of result x != y.
/// ///

View File

@ -51,23 +51,23 @@ namespace glm
/// @addtogroup gtc_packing /// @addtogroup gtc_packing
/// @{ /// @{
uint16 packUnorm1x16(float v); GLM_FUNC_DECL uint16 packUnorm1x16(float v);
float unpackUnorm1x16(uint16 v); GLM_FUNC_DECL float unpackUnorm1x16(uint16 v);
uint64 packUnorm4x16(vec4 const & v); GLM_FUNC_DECL uint64 packUnorm4x16(vec4 const & v);
vec4 unpackUnorm4x16(uint64 const & v); GLM_FUNC_DECL vec4 unpackUnorm4x16(uint64 const & v);
uint16 packSnorm1x16(float v); GLM_FUNC_DECL uint16 packSnorm1x16(float v);
float unpackSnorm1x16(uint16 v); GLM_FUNC_DECL float unpackSnorm1x16(uint16 v);
uint64 packSnorm4x16(vec4 const & v); GLM_FUNC_DECL uint64 packSnorm4x16(vec4 const & v);
vec4 unpackSnorm4x16(uint64 const & v); GLM_FUNC_DECL vec4 unpackSnorm4x16(uint64 const & v);
uint16 packUnorm2x8(vec2 const & v); GLM_FUNC_DECL uint16 packUnorm2x8(vec2 const & v);
vec2 unpackUnorm2x8(uint16 v); GLM_FUNC_DECL vec2 unpackUnorm2x8(uint16 v);
uint16 packSnorm2x8(vec2 const & v); GLM_FUNC_DECL uint16 packSnorm2x8(vec2 const & v);
vec2 unpackSnorm2x8(uint16 v); GLM_FUNC_DECL vec2 unpackSnorm2x8(uint16 v);
GLM_FUNC_DECL uint16 packHalf1x16(float const & v); GLM_FUNC_DECL uint16 packHalf1x16(float const & v);
GLM_FUNC_DECL float unpackHalf1x16(uint16 const & v); GLM_FUNC_DECL float unpackHalf1x16(uint16 const & v);
@ -94,15 +94,15 @@ namespace glm
/// @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> /// @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 unpackHalf4x16(uint64 const & v); GLM_FUNC_DECL vec4 unpackHalf4x16(uint64 const & v);
GLM_FUNC_DECL uint32 packSnorm3x10Snorm1x2(vec4 const & v); GLM_FUNC_DECL uint32 packI10I10I10I2(ivec4 const & v);
GLM_FUNC_DECL vec4 unpackSnorm3x10Snorm1x2(uint32 const & v); GLM_FUNC_DECL ivec4 unpackI10I10I10I2(uint32 const & v);
GLM_FUNC_DECL uint32 packI3x10I1x2(ivec4 const & v);
GLM_FUNC_DECL ivec4 unpackI3x10I1x2(uint32 const & v);
GLM_FUNC_DECL uint32 packU10U10U10U2(uvec4 const & v); GLM_FUNC_DECL uint32 packU10U10U10U2(uvec4 const & v);
GLM_FUNC_DECL uvec4 unpackU10U10U10U2(uint32 const & v); GLM_FUNC_DECL uvec4 unpackU10U10U10U2(uint32 const & v);
GLM_FUNC_DECL uint32 packSnorm3x10_1x2(vec4 const & v);
GLM_FUNC_DECL vec4 unpackSnorm3x10_1x2(uint32 const & v);
GLM_FUNC_DECL uint32 packF11F11F10(vec3 const & v); GLM_FUNC_DECL uint32 packF11F11F10(vec3 const & v);
GLM_FUNC_DECL vec3 unpackF11F11F10(uint32 const & v); GLM_FUNC_DECL vec3 unpackF11F11F10(uint32 const & v);

View File

@ -130,12 +130,270 @@ namespace detail
{ {
return ((floatTo11bit(x) & ((1 << 11) - 1)) << 0) | ((floatTo11bit(y) & ((1 << 11) - 1)) << 11) | ((floatTo10bit(z) & ((1 << 10) - 1)) << 22); return ((floatTo11bit(x) & ((1 << 11) - 1)) << 0) | ((floatTo11bit(y) & ((1 << 11) - 1)) << 11) | ((floatTo10bit(z) & ((1 << 10) - 1)) << 22);
} }
union u10u10u10u2
{
struct
{
uint x : 10;
uint y : 10;
uint z : 10;
uint w : 2;
} data;
uint32 pack;
};
union i10i10i10i2
{
struct
{
int x : 10;
int y : 10;
int z : 10;
int w : 2;
} data;
uint32 pack;
};
union unorm4x16
{
struct
{
uint16 x;
uint16 y;
uint16 z;
uint16 w;
} data;
uint64 pack;
};
union snorm4x16
{
struct
{
int16 x;
int16 y;
int16 z;
int16 w;
} data;
uint64 pack;
};
union snorm1x16
{
int16 data;
uint16 pack;
};
union half1x16
{
hdata data;
uint16 pack;
};
union unorm2x8
{
struct
{
uint8 x;
uint8 y;
} data;
uint16 pack;
};
union snorm2x8
{
struct
{
int8 x;
int8 y;
} data;
uint16 pack;
};
}//namespace detail }//namespace detail
GLM_FUNC_QUALIFIER uint16 packUnorm1x16(float s)
{
return uint16(round(clamp(s, 0.0f, 1.0f) * 65535.0f));
}
GLM_FUNC_QUALIFIER float unpackUnorm1x16(uint16 p)
{
return float(p) * 1.0f / 65535.0f;
}
GLM_FUNC_QUALIFIER uint64 packUnorm4x16(vec4 const & v)
{
i16vec4 Scaled(round(clamp(v, 0.0f, 1.0f) * 65535.0f));
detail::unorm4x16 Packing;
Packing.data.x = Scaled[0];
Packing.data.y = Scaled[1];
Packing.data.z = Scaled[2];
Packing.data.w = Scaled[3];
return Packing.pack;
}
GLM_FUNC_QUALIFIER vec4 unpackUnorm4x16(uint64 const & p)
{
detail::unorm4x16 Packing;
vec4 Result(
float(Packing.data.x),
float(Packing.data.y),
float(Packing.data.z),
float(Packing.data.w));
Result *= float(1.5259021896696421759365224689097e-5); // 1.0 / 65535.0
}
GLM_FUNC_QUALIFIER uint16 packSnorm1x16(float v)
{
float Scaled = clamp(v ,-1.0f, 1.0f) * 32767.0f;
detail::snorm1x16 Packing;
Packing.data = detail::int16(Scaled);
return Packing.pack;
}
GLM_FUNC_QUALIFIER float unpackSnorm1x16(uint16 p)
{
detail::snorm1x16 Packing;
Packing.pack = p;
return clamp(float(Packing.data) * float(3.0518509475997192297128208258309e-5), -1.0f, 1.0f); //1.0f / 32767.0f
}
GLM_FUNC_QUALIFIER uint64 packSnorm4x16(vec4 const & v)
{
i16vec4 Scaled(clamp(v ,-1.0f, 1.0f) * 32767.0f);
detail::snorm4x16 Packing;
Packing.data.x = Scaled.x;
Packing.data.y = Scaled.y;
Packing.data.z = Scaled.z;
Packing.data.w = Scaled.w;
return Packing.pack;
}
GLM_FUNC_QUALIFIER vec4 unpackSnorm4x16(uint64 const & p)
{
detail::snorm4x16 Packing;
Packing.pack = p;
vec4 Unpacked(Packing.data.x, Packing.data.y, Packing.data.z, Packing.data.w);
return clamp(Unpacked * float(3.0518509475997192297128208258309e-5), -1.0f, 1.0f); //1.0f / 32767.0f
}
GLM_FUNC_QUALIFIER uint16 packUnorm2x8(vec2 const & v)
{
i8vec2 Scaled(round(clamp(v ,-1.0f, 1.0f) * 255.0f));
detail::unorm2x8 Packing;
Packing.data.x = Scaled.x;
Packing.data.y = Scaled.y;
return Packing.pack;
}
GLM_FUNC_QUALIFIER vec2 unpackUnorm2x8(uint16 p)
{
detail::unorm2x8 Packing;
Packing.pack = p;
vec2 Unpacked(Packing.data.x, Packing.data.y);
return Unpacked * float(0.0039215686274509803921568627451);
}
GLM_FUNC_QUALIFIER uint16 packSnorm2x8(vec2 const & v)
{
glm::i8vec2 Scaled(round(clamp(v ,-1.0f, 1.0f) * 127.0f));
detail::snorm2x8 Packing;
Packing.data.x = Scaled.x;
Packing.data.y = Scaled.y;
return Packing.pack;
}
GLM_FUNC_QUALIFIER vec2 unpackSnorm2x8(uint16 p)
{
detail::snorm2x8 Packing;
Packing.pack = p;
vec2 Unpacked(Packing.data.x, Packing.data.y);
return clamp(Unpacked * float(0.00787401574803149606299212598425), -1.0f, 1.0f);
}
GLM_FUNC_DECL uint16 packHalf1x16(float const & v)
{
detail::half1x16 Packing;
Packing.data = detail::toFloat16(v);
return Packing.pack;
}
GLM_FUNC_DECL float unpackHalf1x16(uint16 const & v)
{
detail::half1x16 Packing;
Packing.pack = v;
return detail::toFloat32(Packing.data);
}
GLM_FUNC_QUALIFIER uint32 packI10I10I10I2(ivec4 const & v)
{
detail::i10i10i10i2 Result;
Result.data.x = v.x;
Result.data.y = v.y;
Result.data.z = v.z;
Result.data.w = v.w;
return Result.pack;
}
GLM_FUNC_QUALIFIER ivec4 unpackI10I10I10I2(uint32 const & v)
{
detail::i10i10i10i2 Unpack;
Unpack.pack = v;
return ivec4(
Unpack.data.x,
Unpack.data.y,
Unpack.data.z,
Unpack.data.w);
}
GLM_FUNC_QUALIFIER uint32 packU10U10U10U2(uvec4 const & v)
{
detail::u10u10u10u2 Result;
Result.data.x = v.x;
Result.data.y = v.y;
Result.data.z = v.z;
Result.data.w = v.w;
return Result.pack;
}
GLM_FUNC_QUALIFIER uvec4 unpackU10U10U10U2(uint32 const & v)
{
detail::u10u10u10u2 Unpack;
Unpack.pack = v;
return uvec4(
Unpack.data.x,
Unpack.data.y,
Unpack.data.z,
Unpack.data.w);
}
GLM_FUNC_QUALIFIER uint32 packSnorm3x10_1x2(vec4 const & v)
{
detail::i10i10i10i2 Result;
Result.data.x = int(v.x * 511.f);
Result.data.y = int(v.y * 511.f);
Result.data.z = int(v.z * 511.f);
Result.data.w = int(v.w * 1.f);
return Result.pack;
}
GLM_FUNC_QUALIFIER vec4 unpackSnorm3x10_1x2(uint32 const & v)
{
detail::i10i10i10i2 Unpack;
Unpack.pack = v;
vec4 Result;
Result.x = float(Unpack.data.x) / 511.f;
Result.y = float(Unpack.data.y) / 511.f;
Result.z = float(Unpack.data.z) / 511.f;
Result.w = float(Unpack.data.w) / 1.f;
return Result;
}
GLM_FUNC_QUALIFIER uint32 packF11F11F10(vec3 const & v) GLM_FUNC_QUALIFIER uint32 packF11F11F10(vec3 const & v)
{ {
return return
((detail::floatTo11bit(v.x) & ((1 << 11) - 1)) << 0) | ((detail::floatTo11bit(v.x) & ((1 << 11) - 1)) << 0) |
((detail::floatTo11bit(v.y) & ((1 << 11) - 1)) << 11) | ((detail::floatTo11bit(v.y) & ((1 << 11) - 1)) << 11) |
((detail::floatTo10bit(v.z) & ((1 << 10) - 1)) << 22); ((detail::floatTo10bit(v.z) & ((1 << 10) - 1)) << 22);
} }

View File

@ -791,27 +791,27 @@ int test_half()
{ {
int Error = 0; int Error = 0;
print_value(0.0); print_value(0.0f);
print_value(0.1); print_value(0.1f);
print_value(0.2); print_value(0.2f);
print_value(0.3); print_value(0.3f);
print_value(0.4); print_value(0.4f);
print_value(0.5); print_value(0.5f);
print_value(0.6); print_value(0.6f);
print_value(1.0); print_value(1.0f);
print_value(1.1); print_value(1.1f);
print_value(1.2); print_value(1.2f);
print_value(1.3); print_value(1.3f);
print_value(1.4); print_value(1.4f);
print_value(1.5); print_value(1.5f);
print_value(1.6); print_value(1.6f);
print_value(2.0); print_value(2.0f);
print_value(2.1); print_value(2.1f);
print_value(2.2); print_value(2.2f);
print_value(2.3); print_value(2.3f);
print_value(2.4); print_value(2.4f);
print_value(2.5); print_value(2.5f);
print_value(2.6); print_value(2.6f);
return Error; return Error;
} }

View File

@ -29,6 +29,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/packing.hpp> #include <glm/gtc/packing.hpp>
#include <cstdio>
void print_bits(glm::half const & s) void print_bits(glm::half const & s)
{ {
@ -96,10 +97,10 @@ void print_value(float const & s)
print_bits(s); print_bits(s);
printf(", "); printf(", ");
print_bits(glm::half(s)); print_bits(glm::half(s));
printf(", "); // printf(", ");
print_11bits(detail::floatTo11bit(s)); // print_11bits(detail::floatTo11bit(s));
printf(", "); // printf(", ");
print_10bits(detail::floatTo10bit(s)); // print_10bits(detail::floatTo10bit(s));
printf("\n"); printf("\n");
} }
@ -107,27 +108,27 @@ int test_half()
{ {
int Error = 0; int Error = 0;
print_value(0.0); print_value(0.0f);
print_value(0.1); print_value(0.1f);
print_value(0.2); print_value(0.2f);
print_value(0.3); print_value(0.3f);
print_value(0.4); print_value(0.4f);
print_value(0.5); print_value(0.5f);
print_value(0.6); print_value(0.6f);
print_value(1.0); print_value(1.0f);
print_value(1.1); print_value(1.1f);
print_value(1.2); print_value(1.2f);
print_value(1.3); print_value(1.3f);
print_value(1.4); print_value(1.4f);
print_value(1.5); print_value(1.5f);
print_value(1.6); print_value(1.6f);
print_value(2.0); print_value(2.0f);
print_value(2.1); print_value(2.1f);
print_value(2.2); print_value(2.2f);
print_value(2.3); print_value(2.3f);
print_value(2.4); print_value(2.4f);
print_value(2.5); print_value(2.5f);
print_value(2.6); print_value(2.6f);
return Error; return Error;
} }