Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support graphics cards without ARB_half_float_vertex #1179

Closed
wants to merge 6 commits into from
Closed
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
128 changes: 89 additions & 39 deletions src/engine/renderer/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4615,10 +4615,18 @@ void DebugDrawVertex(const vec3_t pos, unsigned int color, const vec2_t uv) {
tess.verts[ tess.numVertexes ].xyz[ 1 ] = pos[ 1 ];
tess.verts[ tess.numVertexes ].xyz[ 2 ] = pos[ 2 ];
tess.verts[ tess.numVertexes ].color = colors;

if( uv ) {
tess.verts[ tess.numVertexes ].texCoords[ 0 ] = floatToHalf( uv[ 0 ] );
tess.verts[ tess.numVertexes ].texCoords[ 1 ] = floatToHalf( uv[ 1 ] );
if ( glConfig2.halfFloatVertexAvailable )
{
floatToHalf2( uv, tess.verts[ tess.numVertexes ].f16TexCoords );
}
else
{
Vector2Copy( uv, tess.verts[ tess.numVertexes ].texCoords );
}
}

tess.indexes[ tess.numIndexes ] = tess.numVertexes;
tess.numVertexes++;
tess.numIndexes++;
Expand Down Expand Up @@ -5006,32 +5014,42 @@ const RenderCommand *StretchPicCommand::ExecuteSelf( ) const
tess.verts[ numVerts ].xyz[ 2 ] = 0.0f;
tess.verts[ numVerts + 0 ].color = backEnd.color2D;

tess.verts[ numVerts ].texCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts ].texCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 1 ].xyz[ 0 ] = x + w;
tess.verts[ numVerts + 1 ].xyz[ 1 ] = y;
tess.verts[ numVerts + 1 ].xyz[ 2 ] = 0.0f;
tess.verts[ numVerts + 1 ].color = backEnd.color2D;

tess.verts[ numVerts + 1 ].texCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 1 ].texCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 2 ].xyz[ 0 ] = x + w;
tess.verts[ numVerts + 2 ].xyz[ 1 ] = y + h;
tess.verts[ numVerts + 2 ].xyz[ 2 ] = 0.0f;
tess.verts[ numVerts + 2 ].color = backEnd.color2D;

tess.verts[ numVerts + 2 ].texCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 2 ].texCoords[ 1 ] = floatToHalf( t2 );

tess.verts[ numVerts + 3 ].xyz[ 0 ] = x;
tess.verts[ numVerts + 3 ].xyz[ 1 ] = y + h;
tess.verts[ numVerts + 3 ].xyz[ 2 ] = 0.0f;
tess.verts[ numVerts + 3 ].color = backEnd.color2D;

tess.verts[ numVerts + 3 ].texCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts + 3 ].texCoords[ 1 ] = floatToHalf( t2 );
if ( glConfig2.halfFloatVertexAvailable )
{
tess.verts[ numVerts ].f16TexCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts ].f16TexCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 1 ].f16TexCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 1 ].f16TexCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 2 ].f16TexCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 2 ].f16TexCoords[ 1 ] = floatToHalf( t2 );

tess.verts[ numVerts + 3 ].f16TexCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts + 3 ].f16TexCoords[ 1 ] = floatToHalf( t2 );
}
else
{
Vector2Set( tess.verts[ numVerts ].texCoords, s1, t1 );
Vector2Set( tess.verts[ numVerts + 1 ].texCoords, s2, t1 );
Vector2Set( tess.verts[ numVerts + 2 ].texCoords, s2, t2 );
Vector2Set( tess.verts[ numVerts + 3 ].texCoords, s1, t2 );
}

return this + 1;
}
Expand Down Expand Up @@ -5085,8 +5103,14 @@ const RenderCommand *Poly2dCommand::ExecuteSelf( ) const
tess.verts[ tess.numVertexes ].xyz[ 1 ] = verts[ i ].xyz[ 1 ];
tess.verts[ tess.numVertexes ].xyz[ 2 ] = 0.0f;

tess.verts[ tess.numVertexes ].texCoords[ 0 ] = floatToHalf( verts[ i ].st[ 0 ] );
tess.verts[ tess.numVertexes ].texCoords[ 1 ] = floatToHalf( verts[ i ].st[ 1 ] );
if ( glConfig2.halfFloatVertexAvailable )
{
floatToHalf2( verts[ i ].st, tess.verts[ tess.numVertexes ].f16TexCoords );
}
else
{
Vector2Copy( verts[ i ].st, tess.verts[ tess.numVertexes ].texCoords );
}

tess.verts[ tess.numVertexes ].color = Color::Adapt( verts[ i ].modulate );
tess.numVertexes++;
Expand Down Expand Up @@ -5142,8 +5166,14 @@ const RenderCommand *Poly2dIndexedCommand::ExecuteSelf( ) const
tess.verts[ tess.numVertexes ].xyz[ 1 ] = verts[ i ].xyz[ 1 ] + translation[ 1 ];
tess.verts[ tess.numVertexes ].xyz[ 2 ] = 0.0f;

tess.verts[ tess.numVertexes ].texCoords[ 0 ] = floatToHalf( verts[ i ].st[ 0 ] );
tess.verts[ tess.numVertexes ].texCoords[ 1 ] = floatToHalf( verts[ i ].st[ 1 ] );
if ( glConfig2.halfFloatVertexAvailable )
{
floatToHalf2( verts[ i ].st, tess.verts[ tess.numVertexes ].f16TexCoords );
}
else
{
Vector2Copy( verts[ i ].st, tess.verts[ tess.numVertexes ].texCoords );
}

tess.verts[ tess.numVertexes ].color = Color::Adapt( verts[ i ].modulate );
tess.numVertexes++;
Expand Down Expand Up @@ -5245,32 +5275,42 @@ const RenderCommand *RotatedPicCommand::ExecuteSelf( ) const
tess.verts[ numVerts ].xyz[ 2 ] = 0.0f;
tess.verts[ numVerts + 0 ].color = backEnd.color2D;

tess.verts[ numVerts ].texCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts ].texCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 1 ].xyz[ 0 ] = mx + cw - sh;
tess.verts[ numVerts + 1 ].xyz[ 1 ] = my - sw - ch;
tess.verts[ numVerts + 1 ].xyz[ 2 ] = 0.0f;
tess.verts[ numVerts + 1 ].color = backEnd.color2D;

tess.verts[ numVerts + 1 ].texCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 1 ].texCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 2 ].xyz[ 0 ] = mx + cw + sh;
tess.verts[ numVerts + 2 ].xyz[ 1 ] = my - sw + ch;
tess.verts[ numVerts + 2 ].xyz[ 2 ] = 0.0f;
tess.verts[ numVerts + 2 ].color = backEnd.color2D;

tess.verts[ numVerts + 2 ].texCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 2 ].texCoords[ 1 ] = floatToHalf( t2 );

tess.verts[ numVerts + 3 ].xyz[ 0 ] = mx - cw + sh;
tess.verts[ numVerts + 3 ].xyz[ 1 ] = my + sw + ch;
tess.verts[ numVerts + 3 ].xyz[ 2 ] = 0.0f;
tess.verts[ numVerts + 3 ].color = backEnd.color2D;

tess.verts[ numVerts + 3 ].texCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts + 3 ].texCoords[ 1 ] = floatToHalf( t2 );
if ( glConfig2.halfFloatVertexAvailable )
{
tess.verts[ numVerts ].f16TexCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts ].f16TexCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 1 ].f16TexCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 1 ].f16TexCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 2 ].f16TexCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 2 ].f16TexCoords[ 1 ] = floatToHalf( t2 );

tess.verts[ numVerts + 3 ].f16TexCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts + 3 ].f16TexCoords[ 1 ] = floatToHalf( t2 );
}
else
{
Vector2Set( tess.verts[ numVerts ].texCoords, s1, t1 );
Vector2Set( tess.verts[ numVerts + 1 ].texCoords, s2, t1 );
Vector2Set( tess.verts[ numVerts + 2 ].texCoords, s2, t2 );
Vector2Set( tess.verts[ numVerts + 3 ].texCoords, s1, t2 );
}

return this + 1;
}
Expand Down Expand Up @@ -5329,29 +5369,39 @@ const RenderCommand *GradientPicCommand::ExecuteSelf( ) const
tess.verts[ numVerts ].xyz[ 1 ] = y;
tess.verts[ numVerts ].xyz[ 2 ] = 0.0f;

tess.verts[ numVerts ].texCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts ].texCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 1 ].xyz[ 0 ] = x + w;
tess.verts[ numVerts + 1 ].xyz[ 1 ] = y;
tess.verts[ numVerts + 1 ].xyz[ 2 ] = 0.0f;

tess.verts[ numVerts + 1 ].texCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 1 ].texCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 2 ].xyz[ 0 ] = x + w;
tess.verts[ numVerts + 2 ].xyz[ 1 ] = y + h;
tess.verts[ numVerts + 2 ].xyz[ 2 ] = 0.0f;

tess.verts[ numVerts + 2 ].texCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 2 ].texCoords[ 1 ] = floatToHalf( t2 );

tess.verts[ numVerts + 3 ].xyz[ 0 ] = x;
tess.verts[ numVerts + 3 ].xyz[ 1 ] = y + h;
tess.verts[ numVerts + 3 ].xyz[ 2 ] = 0.0f;

tess.verts[ numVerts + 3 ].texCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts + 3 ].texCoords[ 1 ] = floatToHalf( t2 );
if ( glConfig2.halfFloatVertexAvailable )
{
tess.verts[ numVerts ].f16TexCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts ].f16TexCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 1 ].f16TexCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 1 ].f16TexCoords[ 1 ] = floatToHalf( t1 );

tess.verts[ numVerts + 2 ].f16TexCoords[ 0 ] = floatToHalf( s2 );
tess.verts[ numVerts + 2 ].f16TexCoords[ 1 ] = floatToHalf( t2 );

tess.verts[ numVerts + 3 ].f16TexCoords[ 0 ] = floatToHalf( s1 );
tess.verts[ numVerts + 3 ].f16TexCoords[ 1 ] = floatToHalf( t2 );
}
else
{
Vector2Set( tess.verts[ numVerts ].texCoords, s1, t1 );
Vector2Set( tess.verts[ numVerts + 1 ].texCoords, s2, t1 );
Vector2Set( tess.verts[ numVerts + 2 ].texCoords, s2, t2 );
Vector2Set( tess.verts[ numVerts + 3 ].texCoords, s1, t2 );
}

return this + 1;
}
Expand Down
9 changes: 9 additions & 0 deletions src/engine/renderer/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,15 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
Log::Notice("%sMissing OpenGL extensions: %s", Color::ToString( Color::Red ), glConfig2.glMissingExtensionsString );
}

if ( glConfig2.halfFloatVertexAvailable )
{
Log::Notice("%sUsing half-float vertex format.", Color::ToString( Color::Green ));
}
else
{
Log::Notice("%sMissing half-float vertex format.", Color::ToString( Color::Red ));
}

if ( glConfig.hardwareType == glHardwareType_t::GLHW_R300 )
{
Log::Notice("%sUsing ATI R300 approximations.", Color::ToString( Color::Red ));
Expand Down
57 changes: 50 additions & 7 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,35 @@ static inline f16_t floatToHalf( float in ) {

return { uint16_t(((ui & 0x80000000) >> 16) | ((ui & 0x0fffe000) >> 13)) };
}
static inline void floatToHalf( const vec4_t in, f16vec4_t out )

static inline void floatToHalf2( const vec2_t in, f16vec2_t out )
{
out[ 0 ] = floatToHalf( in[ 0 ] );
out[ 1 ] = floatToHalf( in[ 1 ] );
}

static inline void floatToHalf4( const vec4_t in, f16vec4_t out )
{
out[ 0 ] = floatToHalf( in[ 0 ] );
out[ 1 ] = floatToHalf( in[ 1 ] );
out[ 2 ] = floatToHalf( in[ 2 ] );
out[ 3 ] = floatToHalf( in[ 3 ] );
}

static inline float halfToFloat( f16_t in ) {
static float scale = powf(2.0f, 127 - 15);

uint32_t ui = (((unsigned int)in.bits & 0x8000) << 16) | (((unsigned int)in.bits & 0x7fff) << 13);
return Util::bit_cast<float>(ui) * scale;
}
static inline void halfToFloat( const f16vec4_t in, vec4_t out )

static inline void halfToFloat2( const f16vec2_t in, vec2_t out )
{
out[ 0 ] = halfToFloat( in[ 0 ] );
out[ 1 ] = halfToFloat( in[ 1 ] );
}

static inline void halfToFloat4( const f16vec4_t in, vec4_t out )
{
out[ 0 ] = halfToFloat( in[ 0 ] );
out[ 1 ] = halfToFloat( in[ 1 ] );
Expand Down Expand Up @@ -201,6 +216,23 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
// max. 16 dynamic lights per plane
#define LIGHT_PLANES ( MAX_REF_LIGHTS / 16 )

struct glVertexShim_t
{
GLenum floatFormat;
};

extern glVertexShim_t GL_vertexShim;

static inline void glVertexSetHalfFloat()
{
GL_vertexShim.floatFormat = GL_HALF_FLOAT;
}

static inline void glVertexSetFloat()
{
GL_vertexShim.floatFormat = GL_FLOAT;
}

struct glFboShim_t
{
/* Functions with same signature and similar purpose can be provided by:
Expand Down Expand Up @@ -723,7 +755,12 @@ enum class realtimeLightingRenderer_t { LEGACY, TILED };
vec3_t *xyz;
i16vec4_t *qtangent;
u8vec4_t *color;
union { f16vec2_t *st; vec2_t *stf; };

union {
f16vec2_t *f16st;
vec2_t *st;
};

int (*boneIndexes)[ 4 ];
vec4_t *boneWeights;

Expand Down Expand Up @@ -2158,7 +2195,7 @@ enum class realtimeLightingRenderer_t { LEGACY, TILED };
vec4_t binormal;
vec4_t normal;

f16vec2_t texCoords;
f16vec2_t f16TexCoords;
char _pad[ 4 ];
uint32_t firstWeight;
uint32_t numWeights;
Expand Down Expand Up @@ -2292,7 +2329,7 @@ enum class realtimeLightingRenderer_t { LEGACY, TILED };
float *normals;
float *tangents;
float *bitangents;
f16_t *texcoords;
f16_t *f16TexCoords;
byte *blendIndexes;
byte *blendWeights;
byte *colors;
Expand Down Expand Up @@ -3275,11 +3312,17 @@ inline bool checkGLErrors()
struct shaderVertex_t {
vec3_t xyz;
Color::Color32Bit color;

union {
i16vec4_t qtangents;
f16vec4_t spriteOrientation;
f16vec4_t f16SpriteOrientation;
vec4_t spriteOrientation;
};

union {
f16vec4_t f16TexCoords;
vec4_t texCoords;
};
f16vec4_t texCoords;
};

#ifdef GL_ARB_sync
Expand Down
10 changes: 5 additions & 5 deletions src/engine/renderer/tr_model_iqm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ bool R_LoadIQModel( model_t *mod, const 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(f16_t); // texcoords
size += header->num_vertexes * 2 * sizeof(f16_t); // f16TexCoords
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 @@ -541,8 +541,8 @@ bool R_LoadIQModel( model_t *mod, const void *buffer, int filesize,
IQModel->bitangents = (float *)ptr;
ptr = IQModel->bitangents + 3 * header->num_vertexes;

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

IQModel->blendIndexes = (byte *)ptr;
ptr = IQModel->blendIndexes + 4 * header->num_vertexes;
Expand Down Expand Up @@ -725,7 +725,7 @@ bool R_LoadIQModel( model_t *mod, const void *buffer, int filesize,
break;
case IQM_TEXCOORD:
for( int j = 0; j < n; j++ ) {
IQModel->texcoords[ j ] = floatToHalf( ((float *)IQMPtr( header, vertexarray->offset ))[ j ] );
IQModel->f16TexCoords[ j ] = floatToHalf( ((float *)IQMPtr( header, vertexarray->offset ))[ j ] );
}
break;
case IQM_BLENDINDEXES:
Expand Down Expand Up @@ -803,7 +803,7 @@ bool R_LoadIQModel( model_t *mod, const void *buffer, int filesize,
vboData.qtangent = qtangentbuf;
vboData.numFrames = 0;
vboData.color = (u8vec4_t *)IQModel->colors;
vboData.st = (f16vec2_t *)IQModel->texcoords;
vboData.f16st = (f16vec2_t *)IQModel->f16TexCoords;
vboData.boneIndexes = (int (*)[4])indexbuf;
vboData.boneWeights = (vec4_t *)weightbuf;
vboData.numVerts = IQModel->num_vertexes;
Expand Down
Loading
Loading