From 6d9e9e5de83be0ad4cf7cc4c373a5f8e94783aef Mon Sep 17 00:00:00 2001 From: VReaperV Date: Wed, 25 Dec 2024 15:05:44 +0300 Subject: [PATCH] Change u_Color to uint `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`. --- src/engine/renderer/gl_shader.cpp | 4 ++++ src/engine/renderer/gl_shader.h | 6 +++--- src/engine/renderer/glsl_source/fogGlobal_fp.glsl | 4 ++-- src/engine/renderer/glsl_source/fogQuake3_vp.glsl | 4 ++-- src/engine/renderer/glsl_source/forwardLighting_vp.glsl | 4 ++-- src/engine/renderer/glsl_source/generic_vp.glsl | 4 ++-- src/engine/renderer/glsl_source/lightMapping_vp.glsl | 4 ++-- src/engine/renderer/glsl_source/shadowFill_vp.glsl | 4 ++-- src/engine/renderer/tr_local.h | 7 +++++++ 9 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/engine/renderer/gl_shader.cpp b/src/engine/renderer/gl_shader.cpp index 7425f0854a..39d6b22571 100644 --- a/src/engine/renderer/gl_shader.cpp +++ b/src/engine/renderer/gl_shader.cpp @@ -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; } diff --git a/src/engine/renderer/gl_shader.h b/src/engine/renderer/gl_shader.h index a6a8b7773f..a6d089423a 100644 --- a/src/engine/renderer/gl_shader.h +++ b/src/engine/renderer/gl_shader.h @@ -3119,17 +3119,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() ) ); } }; diff --git a/src/engine/renderer/glsl_source/fogGlobal_fp.glsl b/src/engine/renderer/glsl_source/fogGlobal_fp.glsl index da3ac5299d..24076a159b 100644 --- a/src/engine/renderer/glsl_source/fogGlobal_fp.glsl +++ b/src/engine/renderer/glsl_source/fogGlobal_fp.glsl @@ -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 @@ -52,5 +52,5 @@ void main() vec4 color = texture2D(u_ColorMap, st); - outputColor = u_Color * color; + outputColor = unpackUnorm4x8( u_Color ) * color; } diff --git a/src/engine/renderer/glsl_source/fogQuake3_vp.glsl b/src/engine/renderer/glsl_source/fogQuake3_vp.glsl index a8d798ed3b..77eba51fe1 100644 --- a/src/engine/renderer/glsl_source/fogQuake3_vp.glsl +++ b/src/engine/renderer/glsl_source/fogQuake3_vp.glsl @@ -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; @@ -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, diff --git a/src/engine/renderer/glsl_source/forwardLighting_vp.glsl b/src/engine/renderer/glsl_source/forwardLighting_vp.glsl index 221bebe10c..22d247c3c4 100644 --- a/src/engine/renderer/glsl_source/forwardLighting_vp.glsl +++ b/src/engine/renderer/glsl_source/forwardLighting_vp.glsl @@ -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; @@ -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, diff --git a/src/engine/renderer/glsl_source/generic_vp.glsl b/src/engine/renderer/glsl_source/generic_vp.glsl index d62630d649..e8bdce44fc 100644 --- a/src/engine/renderer/glsl_source/generic_vp.glsl +++ b/src/engine/renderer/glsl_source/generic_vp.glsl @@ -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 @@ -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, diff --git a/src/engine/renderer/glsl_source/lightMapping_vp.glsl b/src/engine/renderer/glsl_source/lightMapping_vp.glsl index d69f9dc729..f78d86228c 100644 --- a/src/engine/renderer/glsl_source/lightMapping_vp.glsl +++ b/src/engine/renderer/glsl_source/lightMapping_vp.glsl @@ -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; @@ -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); diff --git a/src/engine/renderer/glsl_source/shadowFill_vp.glsl b/src/engine/renderer/glsl_source/shadowFill_vp.glsl index 4c0afd1e04..5becd198ff 100644 --- a/src/engine/renderer/glsl_source/shadowFill_vp.glsl +++ b/src/engine/renderer/glsl_source/shadowFill_vp.glsl @@ -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; @@ -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 ); } diff --git a/src/engine/renderer/tr_local.h b/src/engine/renderer/tr_local.h index b44fc3e0cc..230c32d89a 100644 --- a/src/engine/renderer/tr_local.h +++ b/src/engine/renderer/tr_local.h @@ -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);