Skip to content

Commit

Permalink
Change u_Color to uint
Browse files Browse the repository at this point in the history
`u_Color` was a vec4 that was being set from a `Color` object, which only has 8-bit precision per colour, so using up 4 times more memory than that to transfer it was quite atrocious. Instead, pack it into a uint32_t, then use `unpackUnorm4x8()` to unpack in the shader.

Also adds a macro for unpacking for hardware that doesn't support `GL_ARB_gpu_shader5`.
  • Loading branch information
VReaperV committed Jan 12, 2025
1 parent 6e1a9d6 commit 7453c4c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ static std::string GenCompatHeader() {
str += "float smoothstep(float edge0, float edge1, float x) { float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); return t * t * (3.0 - 2.0 * t); }\n";
}

if ( !glConfig2.gpuShader5Available ) {
str += "#define unpackUnorm4x8( value ) ( ( vec4( value, value >> 8, value >> 16, value >> 24 ) & 0xFF ) / 255.0f )\n";
}

return str;
}

Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3099,17 +3099,17 @@ class u_CloudHeight :
};

class u_Color :
GLUniform4f
GLUniform1ui
{
public:
u_Color( GLShader *shader ) :
GLUniform4f( shader, "u_Color" )
GLUniform1ui( shader, "u_Color" )
{
}

void SetUniform_Color( const Color::Color& color )
{
this->SetValue( color.ToArray() );
this->SetValue( packUnorm4x8( color.ToArray() ) );
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/fogGlobal_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uniform sampler2D u_ColorMap; // fog texture
uniform sampler2D u_DepthMap;

uniform vec4 u_FogDistanceVector;
uniform vec4 u_Color;
uniform uint u_Color;
uniform mat4 u_UnprojectMatrix;

#if __VERSION__ > 120
Expand All @@ -52,5 +52,5 @@ void main()

vec4 color = texture2D(u_ColorMap, st);

outputColor = u_Color * color;
outputColor = unpackUnorm4x8( u_Color ) * color;
}
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/fogQuake3_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
uniform float u_Time;

uniform vec4 u_ColorModulate;
uniform vec4 u_Color;
uniform uint u_Color;
uniform mat4 u_ModelMatrix;
uniform mat4 u_ModelViewProjectionMatrix;

Expand Down Expand Up @@ -58,7 +58,7 @@ void main()

VertexFetch( position, LB, color, texCoord, lmCoord );

color = /* color * u_ColorModulate + */ u_Color;
color = /* color * u_ColorModulate + */ unpackUnorm4x8( u_Color );

DeformVertex( position,
LB.normal,
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/forwardLighting_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ uniform mat4 u_ModelMatrix;
uniform mat4 u_ModelViewProjectionMatrix;

uniform vec4 u_ColorModulate;
uniform vec4 u_Color;
uniform uint u_Color;

uniform float u_Time;

Expand Down Expand Up @@ -63,7 +63,7 @@ void main()
VertexFetch( position, LB, color, texCoord, lmCoord);

// assign color
color = color * u_ColorModulate + u_Color;
color = color * u_ColorModulate + unpackUnorm4x8( u_Color );

DeformVertex( position,
LB.normal,
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/generic_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ uniform vec3 u_ViewOrigin;
uniform float u_Time;

uniform vec4 u_ColorModulate;
uniform vec4 u_Color;
uniform uint u_Color;
#if defined(USE_TCGEN_ENVIRONMENT)
uniform mat4 u_ModelMatrix;
#endif
Expand Down Expand Up @@ -63,7 +63,7 @@ void main()
vec2 texCoord, lmCoord;

VertexFetch( position, LB, color, texCoord, lmCoord );
color = color * u_ColorModulate + u_Color;
color = color * u_ColorModulate + unpackUnorm4x8( u_Color );

DeformVertex( position,
LB.normal,
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/lightMapping_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ uniform mat4 u_ModelViewProjectionMatrix;
uniform float u_Time;

uniform vec4 u_ColorModulate;
uniform vec4 u_Color;
uniform uint u_Color;

OUT(smooth) vec3 var_Position;
OUT(smooth) vec2 var_TexCoords;
Expand All @@ -73,7 +73,7 @@ void main()

VertexFetch(position, LB, color, texCoord, lmCoord);

color = color * u_ColorModulate + u_Color;
color = color * u_ColorModulate + unpackUnorm4x8( u_Color );

DeformVertex(position, LB.normal, texCoord, color, u_Time);

Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/shadowFill_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#insert vertexSkinning_vp
#insert vertexAnimation_vp

uniform vec4 u_Color;
uniform uint u_Color;

uniform mat4 u_TextureMatrix;
uniform mat4 u_ModelMatrix;
Expand Down Expand Up @@ -73,5 +73,5 @@ void main()
var_TexCoords = (u_TextureMatrix * vec4(texCoord, 0.0, 1.0)).st;

// assign color
var_Color = u_Color;
var_Color = unpackUnorm4x8( u_Color );
}
7 changes: 7 additions & 0 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ static inline void snorm16ToFloat( const i16vec4_t in, vec4_t out )
out[ 3 ] = snorm16ToFloat( in[ 3 ] );
}

static inline uint32_t packUnorm4x8( const vec4_t in ) {
return uint32_t( floatToUnorm8( in[0] ) )
| ( uint32_t( floatToUnorm8( in[1] ) ) << 8 )
| ( uint32_t( floatToUnorm8( in[2] ) ) << 16 )
| ( uint32_t( floatToUnorm8( in[3] ) ) << 24 );
}

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

Expand Down

0 comments on commit 7453c4c

Please sign in to comment.