Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ using i16vec4_t = int16_t[4];
using u16vec4_t = uint16_t[4];
using i16vec2_t = int16_t[2];
using u16vec2_t = uint16_t[2];
using f16vec4_t = int16_t[4]; // half float vector

// The struct has the same memory layout as a half-float
struct f16_t
{
uint16_t bits;
};

using f16vec2_t = f16_t[2]; // half float vector
using f16vec4_t = f16_t[4]; // half float vector

// GL conversion helpers
static inline float unorm8ToFloat(byte unorm8) {
Expand Down Expand Up @@ -114,13 +122,13 @@ static inline void snorm16ToFloat( const i16vec4_t in, vec4_t out )
out[ 3 ] = snorm16ToFloat( in[ 3 ] );
}

static inline int16_t floatToHalf( float in ) {
static inline f16_t floatToHalf( float in ) {
static float scale = powf(2.0f, 15 - 127);
floatint_t fi;

fi.f = in * scale;

return (int16_t)(((fi.ui & 0x80000000) >> 16) | ((fi.ui & 0x0fffe000) >> 13));
return { uint16_t(((fi.ui & 0x80000000) >> 16) | ((fi.ui & 0x0fffe000) >> 13)) };
}
static inline void floatToHalf( const vec4_t in, f16vec4_t out )
{
Expand All @@ -129,11 +137,11 @@ static inline void floatToHalf( const vec4_t in, f16vec4_t out )
out[ 2 ] = floatToHalf( in[ 2 ] );
out[ 3 ] = floatToHalf( in[ 3 ] );
}
static inline float halfToFloat( int16_t in ) {
static inline float halfToFloat( f16_t in ) {
static float scale = powf(2.0f, 127 - 15);
floatint_t fi;

fi.ui = (((unsigned int)in & 0x8000) << 16) | (((unsigned int)in & 0x7fff) << 13);
fi.ui = (((unsigned int)in.bits & 0x8000) << 16) | (((unsigned int)in.bits & 0x7fff) << 13);
return fi.f * scale;
}
static inline void halfToFloat( const f16vec4_t in, vec4_t out )
Expand Down Expand Up @@ -813,10 +821,10 @@ static inline void glFboSetExt()
vec3_t *xyz;
i16vec4_t *qtangent;
u8vec4_t *color;
union { i16vec2_t *st; i16vec4_t *stpq; vec2_t *stf; };
union { f16vec2_t *st; f16vec4_t *stpq; vec2_t *stf; };
int (*boneIndexes)[ 4 ];
vec4_t *boneWeights;
vec4_t *spriteOrientation;
f16vec4_t *spriteOrientation;

int numFrames;
int numVerts;
Expand Down Expand Up @@ -2264,7 +2272,7 @@ static inline void glFboSetExt()
vec4_t tangent;
vec4_t binormal;
vec4_t normal;
vec2_t texCoords;
vec2_t texCoordsF;

uint32_t firstWeight;
uint32_t numWeights;
Expand Down Expand Up @@ -2394,10 +2402,10 @@ static inline void glFboSetExt()

// vertex data
float *positions;
int16_t *texcoords;
float *normals;
float *tangents;
float *bitangents;
f16_t *texcoords;
byte *blendIndexes;
byte *blendWeights;
byte *colors;
Expand Down Expand Up @@ -3181,7 +3189,7 @@ inline bool checkGLErrors()

void R_CalcTangents( vec3_t tangent, vec3_t binormal,
const vec3_t v0, const vec3_t v1, const vec3_t v2,
const i16vec2_t t0, const i16vec2_t t1, const i16vec2_t t2 );
const f16vec2_t t0, const f16vec2_t t1, const f16vec2_t t2 );

/*
* QTangent representation of tangentspace:
Expand Down Expand Up @@ -3398,7 +3406,7 @@ inline bool checkGLErrors()
i16vec4_t qtangents;
f16vec4_t spriteOrientation;
};
i16vec4_t texCoords;
f16vec4_t texCoords;
};

#ifdef GL_ARB_sync
Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/tr_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void R_CalcTangents( vec3_t tangent, vec3_t binormal,

void R_CalcTangents( vec3_t tangent, vec3_t binormal,
const vec3_t v0, const vec3_t v1, const vec3_t v2,
const i16vec2_t t0, const i16vec2_t t1, const i16vec2_t t2 )
const f16vec2_t t0, const f16vec2_t t1, const f16vec2_t t2 )
{
vec2_t t0f, t1f, t2f;

Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderer/tr_model_iqm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ bool R_LoadIQModel( model_t *mod, void *buffer, int filesize,
size += header->num_vertexes * 3 * sizeof(float); // normals
size += header->num_vertexes * 3 * sizeof(float); // tangents
size += header->num_vertexes * 3 * sizeof(float); // bitangents
size += header->num_vertexes * 2 * sizeof(int16_t); // texcoords
size += header->num_vertexes * 2 * sizeof(f16_t); // texcoords
size += header->num_vertexes * 4 * sizeof(byte); // blendIndexes
size += header->num_vertexes * 4 * sizeof(byte); // blendWeights
size += header->num_vertexes * 4 * sizeof(byte); // colors
Expand Down Expand Up @@ -540,7 +540,7 @@ bool R_LoadIQModel( model_t *mod, void *buffer, int filesize,
IQModel->bitangents = (float *)ptr;
ptr = IQModel->bitangents + 3 * header->num_vertexes;

IQModel->texcoords = (int16_t *)ptr;
IQModel->texcoords = (f16_t *)ptr;
ptr = IQModel->texcoords + 2 * header->num_vertexes;

IQModel->blendIndexes = (byte *)ptr;
Expand Down Expand Up @@ -803,7 +803,7 @@ bool R_LoadIQModel( model_t *mod, void *buffer, int filesize,
vboData.qtangent = qtangentbuf;
vboData.numFrames = 0;
vboData.color = (u8vec4_t *)IQModel->colors;
vboData.st = (i16vec2_t *)IQModel->texcoords;
vboData.st = (f16vec2_t *)IQModel->texcoords;
vboData.noLightCoords = true;
vboData.boneIndexes = (int (*)[4])indexbuf;
vboData.boneWeights = (vec4_t *)weightbuf;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/tr_model_md3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ bool R_LoadMD3( model_t *mod, int lod, void *buffer, const char *modName )
data.xyz = ( vec3_t * ) ri.Hunk_AllocateTempMemory( sizeof( *data.xyz ) * mdvModel->numFrames * surf->numVerts );
data.qtangent = ( i16vec4_t * ) ri.Hunk_AllocateTempMemory( sizeof( i16vec4_t ) * mdvModel->numFrames * surf->numVerts );
data.numFrames = mdvModel->numFrames;
data.st = ( i16vec2_t * ) ri.Hunk_AllocateTempMemory( sizeof( i16vec2_t ) * surf->numVerts );
data.st = ( f16vec2_t * ) ri.Hunk_AllocateTempMemory( sizeof( f16vec2_t ) * surf->numVerts );
data.noLightCoords = true;
data.numVerts = surf->numVerts;

Expand Down
8 changes: 4 additions & 4 deletions src/engine/renderer/tr_model_md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ bool R_LoadMD5( model_t *mod, void *buffer, const char *modName )
for (unsigned k = 0; k < 2; k++ )
{
token = COM_ParseExt2( &buf_p, false );
v->texCoords[ k ] = atof( token );
v->texCoordsF[ k ] = atof( token );
}

// skip )
Expand Down Expand Up @@ -518,9 +518,9 @@ bool R_LoadMD5( model_t *mod, void *buffer, const char *modName )
v1 = surf->verts[ tri->indexes[ 1 ] ].position;
v2 = surf->verts[ tri->indexes[ 2 ] ].position;

t0 = surf->verts[ tri->indexes[ 0 ] ].texCoords;
t1 = surf->verts[ tri->indexes[ 1 ] ].texCoords;
t2 = surf->verts[ tri->indexes[ 2 ] ].texCoords;
t0 = surf->verts[ tri->indexes[ 0 ] ].texCoordsF;
t1 = surf->verts[ tri->indexes[ 1 ] ].texCoordsF;
t2 = surf->verts[ tri->indexes[ 2 ] ].texCoordsF;

R_CalcFaceNormal( normal, v0, v1, v2 );
R_CalcTangents( tangent, binormal, v0, v1, v2, t0, t1, t2 );
Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderer/tr_model_skel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void AddSurfaceToVBOSurfacesList( growList_t *vboSurfaces, growList_t *vboTriang
data.qtangent = ( i16vec4_t * ) ri.Hunk_AllocateTempMemory( sizeof( i16vec4_t ) * vertexesNum );
data.boneIndexes = ( int (*)[ 4 ] ) ri.Hunk_AllocateTempMemory( sizeof( *data.boneIndexes ) * vertexesNum );
data.boneWeights = ( vec4_t * ) ri.Hunk_AllocateTempMemory( sizeof( *data.boneWeights ) * vertexesNum );
data.st = ( i16vec2_t * ) ri.Hunk_AllocateTempMemory( sizeof( i16vec2_t ) * vertexesNum );
data.st = ( f16vec2_t * ) ri.Hunk_AllocateTempMemory( sizeof( f16vec2_t ) * vertexesNum );
data.noLightCoords = true;
data.numVerts = vertexesNum;

Expand Down Expand Up @@ -168,8 +168,8 @@ void AddSurfaceToVBOSurfacesList( growList_t *vboSurfaces, growList_t *vboTriang
R_TBNtoQtangents( surf->verts[ j ].tangent, surf->verts[ j ].binormal,
surf->verts[ j ].normal, data.qtangent[ j ] );

data.st[ j ][ 0 ] = floatToHalf( surf->verts[ j ].texCoords[ 0 ] );
data.st[ j ][ 1 ] = floatToHalf( surf->verts[ j ].texCoords[ 1 ] );
data.st[ j ][ 0 ] = floatToHalf( surf->verts[ j ].texCoordsF[ 0 ] );
data.st[ j ][ 1 ] = floatToHalf( surf->verts[ j ].texCoordsF[ 1 ] );

for (unsigned k = 0; k < MAX_WEIGHTS; k++ )
{
Expand Down
8 changes: 6 additions & 2 deletions src/engine/renderer/tr_shade_calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,10 @@ static void AutospriteDeform( int firstVertex, int numVertexes, int numIndexes )
for ( j = 0; j < 4; j++ ) {
VectorCopy( mid, v[ j ].xyz );
Vector4Set( v[ j ].spriteOrientation,
0, 0, 0, floatToHalf( radius ) );
floatToHalf( 0 ),
floatToHalf( 0 ),
floatToHalf( 0 ),
floatToHalf( radius ) );
}
}
}
Expand Down Expand Up @@ -664,7 +667,8 @@ static void Autosprite2Deform( int firstVertex, int numVertexes, int numIndexes
k = 1;

VectorSubtract( v1->xyz, mid[ k ], minor );
if ( ( DotProduct( cross, minor ) * v1->texCoords[ 3 ] ) < 0 ) {
// I guess this works, since the sign bit is the MSB for both floating point and integers
if ( ( DotProduct( cross, minor ) * static_cast<int16_t>(v1->texCoords[ 3 ].bits) ) < 0 ) {
VectorNegate( major, orientation );
} else {
VectorCopy( major, orientation );
Expand Down
10 changes: 3 additions & 7 deletions src/engine/renderer/tr_surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,9 +1147,7 @@ static void Tess_SurfaceMD5( md5Surface_t *srf )

VectorCopy( position, tessVertex->xyz );

Vector2Set( tessVertex->texCoords,
floatToHalf( surfaceVertex->texCoords[ 0 ] ),
floatToHalf( surfaceVertex->texCoords[ 1 ] ) );
floatToHalf( surfaceVertex->texCoordsF, tessVertex->texCoords );
}
}
else
Expand Down Expand Up @@ -1192,9 +1190,7 @@ static void Tess_SurfaceMD5( md5Surface_t *srf )

R_TBNtoQtangents( tangent, binormal, normal, tessVertex->qtangents );

Vector2Set( tessVertex->texCoords,
floatToHalf( surfaceVertex->texCoords[ 0 ] ),
floatToHalf( surfaceVertex->texCoords[ 1 ] ) );
floatToHalf( surfaceVertex->texCoordsF, tessVertex->texCoords );
}
}

Expand Down Expand Up @@ -1317,7 +1313,7 @@ void Tess_SurfaceIQM( srfIQModel_t *surf ) {
float *modelNormal = model->normals + 3 * firstVertex;
float *modelTangent = model->tangents + 3 * firstVertex;
float *modelBitangent = model->bitangents + 3 * firstVertex;
int16_t *modelTexcoord = model->texcoords + 2 * firstVertex;
f16_t *modelTexcoord = model->texcoords + 2 * firstVertex;
shaderVertex_t *tessVertex = tess.verts + tess.numVertexes;
shaderVertex_t *lastVertex = tessVertex + surf->num_vertexes;

Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/tr_vbo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ struct fmtVertexAnim1 {
const GLsizei sizeVertexAnim1 = sizeof( struct fmtVertexAnim1 );
// interleaved texcoords and colour in part 2
struct fmtVertexAnim2 {
i16vec2_t texcoord;
f16vec2_t texcoord;
Color::Color32Bit colour;
};
const GLsizei sizeVertexAnim2 = sizeof( struct fmtVertexAnim2 );

// interleaved data: position, texcoord, colour, qtangent, bonefactors
struct fmtSkeletal {
i16vec4_t position;
i16vec2_t texcoord;
f16vec2_t texcoord;
Color::Color32Bit colour;
i16vec4_t qtangents;
u16vec4_t boneFactors;
Expand Down