OpenGL2: Support half floats for texcoords and vertex colors.
This commit is contained in:
parent
1ba9e7a45e
commit
943259f6b4
10 changed files with 193 additions and 154 deletions
|
@ -199,42 +199,35 @@ int NextPowerOfTwo(int in)
|
|||
return out;
|
||||
}
|
||||
|
||||
unsigned short FloatToHalf(float in)
|
||||
{
|
||||
unsigned short out;
|
||||
|
||||
union
|
||||
{
|
||||
float f;
|
||||
unsigned int i;
|
||||
} f32;
|
||||
union f32_u {
|
||||
float f;
|
||||
uint32_t i;
|
||||
struct {
|
||||
unsigned int fraction:23;
|
||||
unsigned int exponent:8;
|
||||
unsigned int sign:1;
|
||||
} pack;
|
||||
};
|
||||
|
||||
int sign, inExponent, inFraction;
|
||||
int outExponent, outFraction;
|
||||
union f16_u {
|
||||
uint16_t i;
|
||||
struct {
|
||||
unsigned int fraction:10;
|
||||
unsigned int exponent:5;
|
||||
unsigned int sign:1;
|
||||
} pack;
|
||||
};
|
||||
|
||||
uint16_t FloatToHalf(float in)
|
||||
{
|
||||
union f32_u f32;
|
||||
union f16_u f16;
|
||||
|
||||
f32.f = in;
|
||||
|
||||
sign = (f32.i & 0x80000000) >> 31;
|
||||
inExponent = (f32.i & 0x7F800000) >> 23;
|
||||
inFraction = f32.i & 0x007FFFFF;
|
||||
f16.pack.exponent = CLAMP(f32.pack.exponent - 112, 0, 31);
|
||||
f16.pack.fraction = f32.pack.fraction >> 13;
|
||||
f16.pack.sign = f32.pack.sign;
|
||||
|
||||
outExponent = CLAMP(inExponent - 127, -15, 16) + 15;
|
||||
|
||||
outFraction = 0;
|
||||
if (outExponent == 0x1F)
|
||||
{
|
||||
if (inExponent == 0xFF && inFraction != 0)
|
||||
outFraction = 0x3FF;
|
||||
}
|
||||
else if (outExponent == 0x00)
|
||||
{
|
||||
if (inExponent == 0x00 && inFraction != 0)
|
||||
outFraction = 0x3FF;
|
||||
}
|
||||
else
|
||||
outFraction = inFraction >> 13;
|
||||
|
||||
out = (sign << 15) | (outExponent << 10) | outFraction;
|
||||
|
||||
return out;
|
||||
return f16.i;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue