Skip to content

Commit

Permalink
no ARB_half_float_vertex
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Jun 8, 2024
1 parent 6779ec8 commit 08bd535
Show file tree
Hide file tree
Showing 11 changed files with 500 additions and 135 deletions.
155 changes: 116 additions & 39 deletions src/engine/renderer/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4693,10 +4693,20 @@ 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 )
{
tess.verts[ tess.numVertexes ].f16TexCoords[ 0 ] = floatToHalf( uv[ 0 ] );
tess.verts[ tess.numVertexes ].f16TexCoords[ 1 ] = floatToHalf( uv[ 1 ] );
}
else
{
tess.verts[ tess.numVertexes ].texCoords[ 0 ] = uv[ 0 ];
tess.verts[ tess.numVertexes ].texCoords[ 1 ] = uv[ 1 ];
}
}

tess.indexes[ tess.numIndexes ] = tess.numVertexes;
tess.numVertexes++;
tess.numIndexes++;
Expand Down Expand Up @@ -5066,32 +5076,49 @@ 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( s1 );
tess.verts[ numVerts + 1 ].f16TexCoords[ 1 ] = floatToHalf( t1 );

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

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

tess.verts[ numVerts + 1 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 1 ].texCoords[ 1 ] = t1;

tess.verts[ numVerts + 2 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 2 ].texCoords[ 1 ] = t1;

tess.verts[ numVerts + 3 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 3 ].texCoords[ 1 ] = t1;
}

tess.attribsSet |= ATTR_POSITION | ATTR_COLOR | ATTR_TEXCOORD;

Expand Down Expand Up @@ -5147,8 +5174,16 @@ 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 )
{
tess.verts[ tess.numVertexes ].f16TexCoords[ 0 ] = floatToHalf( verts[ i ].st[ 0 ] );
tess.verts[ tess.numVertexes ].f16TexCoords[ 1 ] = floatToHalf( verts[ i ].st[ 1 ] );
}
else
{
tess.verts[ tess.numVertexes ].texCoords[ 0 ] = verts[ i ].st[ 0 ];
tess.verts[ tess.numVertexes ].texCoords[ 1 ] = verts[ i ].st[ 1 ];
}

tess.verts[ tess.numVertexes ].color = Color::Adapt( verts[ i ].modulate );
tess.numVertexes++;
Expand Down Expand Up @@ -5205,8 +5240,16 @@ 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 )
{
tess.verts[ tess.numVertexes ].f16TexCoords[ 0 ] = floatToHalf( verts[ i ].st[ 0 ] );
tess.verts[ tess.numVertexes ].f16TexCoords[ 1 ] = floatToHalf( verts[ i ].st[ 1 ] );
}
else
{
tess.verts[ tess.numVertexes ].texCoords[ 0 ] = verts[ i ].st[ 0 ];
tess.verts[ tess.numVertexes ].texCoords[ 1 ] = verts[ i ].st[ 1 ];
}

tess.verts[ tess.numVertexes ].color = Color::Adapt( verts[ i ].modulate );
tess.numVertexes++;
Expand Down Expand Up @@ -5310,32 +5353,49 @@ 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( s1 );
tess.verts[ numVerts + 1 ].f16TexCoords[ 1 ] = floatToHalf( t1 );

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

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

tess.verts[ numVerts + 1 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 1 ].texCoords[ 1 ] = t1;

tess.verts[ numVerts + 2 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 2 ].texCoords[ 1 ] = t1;

tess.verts[ numVerts + 3 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 3 ].texCoords[ 1 ] = t1;
}

tess.attribsSet |= ATTR_POSITION | ATTR_TEXCOORD | ATTR_COLOR;

Expand Down Expand Up @@ -5396,29 +5456,46 @@ 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( s1 );
tess.verts[ numVerts + 1 ].f16TexCoords[ 1 ] = floatToHalf( t1 );

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

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

tess.verts[ numVerts + 1 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 1 ].texCoords[ 1 ] = t1;

tess.verts[ numVerts + 2 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 2 ].texCoords[ 1 ] = t1;

tess.verts[ numVerts + 3 ].texCoords[ 0 ] = s1;
tess.verts[ numVerts + 3 ].texCoords[ 1 ] = t1;
}

tess.attribsSet |= ATTR_POSITION | ATTR_TEXCOORD | ATTR_COLOR;
return this + 1;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/renderer/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
cvar_t *r_arb_texture_gather;
cvar_t *r_arb_gpu_shader5;
Cvar::Cvar<bool> r_arb_depth_clamp("r_arb_depth_clamp", "Use GL_ARB_depth_clamp if available", Cvar::NONE, true);
Cvar::Cvar<bool> r_arb_half_float_vertex("r_arb_half_float_vertex", "Use GL_ARB_half_float_vertex if available", Cvar::NONE, true);

cvar_t *r_checkGLErrors;
cvar_t *r_logFile;
Expand Down Expand Up @@ -1091,6 +1092,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
r_arb_texture_gather = Cvar_Get( "r_arb_texture_gather", "1", CVAR_CHEAT | CVAR_LATCH );
r_arb_gpu_shader5 = Cvar_Get( "r_arb_gpu_shader5", "1", CVAR_CHEAT | CVAR_LATCH );
Cvar::Latch(r_arb_depth_clamp);
Cvar::Latch(r_arb_half_float_vertex);

r_picMip = Cvar_Get( "r_picMip", "0", CVAR_LATCH | CVAR_ARCHIVE );
r_imageMaxDimension = Cvar_Get( "r_imageMaxDimension", "0", CVAR_LATCH | CVAR_ARCHIVE );
Expand Down
37 changes: 32 additions & 5 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,34 @@ static inline f16_t floatToHalf( float in ) {

return { uint16_t(((ui & 0x80000000) >> 16) | ((ui & 0x0fffe000) >> 13)) };
}

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

static inline void floatToHalf( 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 halfToFloat2( const f16vec2_t in, vec2_t out )
{
out[ 0 ] = halfToFloat( in[ 0 ] );
out[ 1 ] = halfToFloat( in[ 1 ] );
}

static inline void halfToFloat( const f16vec4_t in, vec4_t out )
{
out[ 0 ] = halfToFloat( in[ 0 ] );
Expand Down Expand Up @@ -200,6 +215,14 @@ 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 glShim_t
{
// ARB_half_float_vertex
GLint VERTEX_FLOAT_FORMAT;
};

extern glShim_t GL_shim;

struct glFboShim_t
{
/* Functions with same signature and similar purpose can be provided by:
Expand Down Expand Up @@ -736,7 +759,7 @@ enum class dynamicLightRenderer_t { LEGACY, TILED };
union { f16vec2_t *st; f16vec4_t *stpq; vec2_t *stf; };
int (*boneIndexes)[ 4 ];
vec4_t *boneWeights;
f16vec4_t *spriteOrientation;
f16vec4_t *f16SpriteOrientation;

int numFrames;
int numVerts;
Expand Down Expand Up @@ -2188,7 +2211,7 @@ enum class dynamicLightRenderer_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 @@ -2322,7 +2345,7 @@ enum class dynamicLightRenderer_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 @@ -2902,6 +2925,7 @@ enum class dynamicLightRenderer_t { LEGACY, TILED };
extern cvar_t *r_arb_texture_gather;
extern cvar_t *r_arb_gpu_shader5;
extern Cvar::Cvar<bool> r_arb_depth_clamp;
extern Cvar::Cvar<bool> r_arb_half_float_vertex;

extern cvar_t *r_nobind; // turns off binding to appropriate textures
extern cvar_t *r_singleShader; // make most world faces use default shader
Expand Down Expand Up @@ -3325,9 +3349,12 @@ inline bool checkGLErrors()
Color::Color32Bit color;
union {
i16vec4_t qtangents;
f16vec4_t spriteOrientation;
f16vec4_t f16SpriteOrientation;
};
f16vec4_t texCoords;
f16vec4_t f16TexCoords;

vec4_t spriteOrientation;
vec4_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 @@ -486,7 +486,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 @@ -546,8 +546,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 @@ -728,7 +728,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 @@ -811,7 +811,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.st = (f16vec2_t *)IQModel->f16TexCoords;
vboData.noLightCoords = true;
vboData.boneIndexes = (int (*)[4])indexbuf;
vboData.boneWeights = (vec4_t *)weightbuf;
Expand Down
Loading

0 comments on commit 08bd535

Please sign in to comment.