GLdc/GL/quantize.inl

103 lines
2.7 KiB
C++

GLushort _quantize( GLfloat v ) {
union { GLfloat f; GLuint ui; } u = {v};
GLuint ui = u.ui;
int s = (ui >> 16) & 0x8000;
int em = ui & 0x7fffffff;
int h = (em - (112 << 23) + (1 << 12)) >> 13;
h = (em < (113 << 23)) ? 0 : h;
h = (em >= (143 << 23)) ? 0x7c00 : h;
h = (em > (255 << 23)) ? 0x7e00 : h;
return (GLushort)(s | h);
}
GLfloat _dequantize( GLushort h ) {
GLuint s = (GLuint) (h & 0x8000) << 16;
int em = h & 0x7fff;
int r = (em + (112 << 10)) << 13;
r = (em < (1 << 10)) ? 0 : r;
r += (em >= (31 << 10)) ? (112 << 23) : 0;
union { GLfloat f; GLuint ui; } u;
u.ui = s | r;
return u.f;
}
// dequantize to float
static void _readPosition3usq3f(const GLubyte* in, GLubyte* out) {
const GLushort* input = (const GLushort*) in;
Vertex* it = (Vertex*)out;
float x = _dequantize(input[0]);
float y = _dequantize(input[1]);
float z = _dequantize(input[2]);
float w = 1.0f;
TransformVertex(x, y, z, w, it->xyz, &it->w);
}
static void _readPosition2usq3f(const GLubyte* in, GLubyte* out) {
const GLushort* input = (const GLushort*) in;
Vertex* it = (Vertex*)out;
float x = _dequantize(input[0]);
float y = _dequantize(input[1]);
float z = 0.0f;
float w = 1.0f;
TransformVertex(x, y, z, w, it->xyz, &it->w);
}
// convert from float16 to float32
static void _readPosition3f16_3f(const GLubyte* in, GLubyte* out) {
const GLhalf* input = (const GLhalf*) in;
Vertex* it = (Vertex*)out;
float x = (float) input[0];
float y = (float) input[1];
float z = (float) input[2];
float w = 1.0f;
TransformVertex(x, y, z, w, it->xyz, &it->w);
}
static void _readPosition2f16_3f(const GLubyte* in, GLubyte* out) {
const GLhalf* input = (const GLhalf*) in;
Vertex* it = (Vertex*)out;
float x = (float) input[0];
float y = (float) input[1];
float z = 0.0f;
float w = 1.0f;
TransformVertex(x, y, z, w, it->xyz, &it->w);
}
// dequantize to float
static void _readTexcoord2usq2f(const GLubyte* in, GLubyte* out) {
const GLushort* input = (const GLushort*) in;
float* output = (float*) out;
output[0] = _dequantize(input[0]);
output[1] = _dequantize(input[1]);
}
// convert from float16 to float32
static void _readTexcoord2f16_2f(const GLubyte* in, GLubyte* out) {
const GLhalf* input = (const GLhalf*) in;
float* output = (float*) out;
output[0] = input[0];
output[1] = input[1];
}
//
static void _readNormal3usq3f(const GLubyte* in, GLubyte* out) {
const GLushort* input = (const GLushort*) in;
float* output = (float*) out;
output[0] = _dequantize(input[0]);
output[1] = _dequantize(input[1]);
output[2] = _dequantize(input[2]);
}
static void _readNormal3f16_3f(const GLubyte* in, GLubyte* out) {
const GLhalf* input = (const GLhalf*) in;
float* output = (float*) out;
output[0] = input[0];
output[1] = input[1];
output[2] = input[2];
}