Skip to content

Commit

Permalink
Simplify overbright implementation
Browse files Browse the repository at this point in the history
Multiply lightmaps and light grid by the light factor right when they
are used, instead of the elaborate canceling system. In a few places
there can be issues with intermediate color buffer values exceeding 1.0,
but this is worked around (on reasonably good hardware) by the previous
commit setting up an RGBA16F framebuffer. Vertex light multiplication is
handled via the white image lightmap which is treated as 4.0.
  • Loading branch information
slipher committed Nov 5, 2024
1 parent 0915a83 commit bcab067
Show file tree
Hide file tree
Showing 21 changed files with 40 additions and 251 deletions.
18 changes: 1 addition & 17 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,6 @@ void UpdateSurfaceDataGeneric3D( uint32_t* materials, Material& material, drawSu
// u_AlphaThreshold
gl_genericShaderMaterial->SetUniform_AlphaTest( pStage->stateBits );

// u_InverseLightFactor
float inverseLightFactor = pStage->cancelOverBright ? tr.mapInverseLightFactor : 1.0f;
gl_genericShaderMaterial->SetUniform_InverseLightFactor( inverseLightFactor );

// u_ColorModulate
colorGen_t rgbGen = SetRgbGen( pStage );
alphaGen_t alphaGen = SetAlphaGen( pStage );
Expand Down Expand Up @@ -296,13 +292,6 @@ void UpdateSurfaceDataLightMapping( uint32_t* materials, Material& material, dra
bool enableGridLighting = ( lightMode == lightMode_t::GRID );
bool enableGridDeluxeMapping = ( deluxeMode == deluxeMode_t::GRID );

// u_InverseLightFactor
/* HACK: use sign to know if there is a light or not, and
then if it will receive overbright multiplication or not. */
bool cancelOverBright = pStage->cancelOverBright || lightMode == lightMode_t::FULLBRIGHT;
float inverseLightFactor = cancelOverBright ? tr.mapInverseLightFactor : -tr.mapInverseLightFactor;
gl_lightMappingShaderMaterial->SetUniform_InverseLightFactor( inverseLightFactor );

// u_ColorModulate
gl_lightMappingShaderMaterial->SetUniform_ColorModulate( rgbGen, alphaGen );

Expand Down Expand Up @@ -475,9 +464,6 @@ void UpdateSurfaceDataSkybox( uint32_t* materials, Material& material, drawSurf_
// u_AlphaThreshold
gl_skyboxShaderMaterial->SetUniform_AlphaTest( GLS_ATEST_NONE );

// u_InverseLightFactor
gl_skyboxShaderMaterial->SetUniform_InverseLightFactor( tr.mapInverseLightFactor );

gl_skyboxShaderMaterial->WriteUniformsToBuffer( materials );
}

Expand Down Expand Up @@ -631,9 +617,6 @@ void UpdateSurfaceDataFog( uint32_t* materials, Material& material, drawSurf_t*

const fog_t* fog = material.fog;

// u_InverseLightFactor
gl_fogQuake3ShaderMaterial->SetUniform_InverseLightFactor( tr.mapInverseLightFactor );

// u_Color
gl_fogQuake3ShaderMaterial->SetUniform_Color( fog->color );

Expand Down Expand Up @@ -1059,6 +1042,7 @@ void BindShaderLightMapping( Material* material ) {
if ( tr.world ) {
gl_lightMappingShaderMaterial->SetUniform_LightGridOrigin( tr.world->lightGridGLOrigin );
gl_lightMappingShaderMaterial->SetUniform_LightGridScale( tr.world->lightGridGLScale );
gl_lightMappingShaderMaterial->SetUniform_LightFactor( tr.mapLightFactor );
}
// FIXME: else

Expand Down
31 changes: 11 additions & 20 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2210,7 +2210,6 @@ GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
u_AlphaThreshold( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_ColorModulate( this ),
u_Color( this ),
u_Bones( this ),
Expand Down Expand Up @@ -2243,7 +2242,6 @@ GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
u_AlphaThreshold( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_ColorModulate( this ),
u_Color( this ),
u_DepthScale( this ),
Expand Down Expand Up @@ -2285,7 +2283,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
u_ViewOrigin( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_LightFactor( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
u_ReliefDepthScale( this ),
Expand Down Expand Up @@ -2354,7 +2352,7 @@ GLShader_lightMappingMaterial::GLShader_lightMappingMaterial( GLShaderManager* m
u_ViewOrigin( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_LightFactor( this ),
u_ReliefDepthScale( this ),
u_ReliefOffsetBias( this ),
u_NormalScale( this ),
Expand Down Expand Up @@ -2413,7 +2411,7 @@ GLShader_forwardLighting_omniXYZ::GLShader_forwardLighting_omniXYZ( GLShaderMana
u_ViewOrigin( this ),
u_LightOrigin( this ),
u_LightColor( this ),
u_InverseLightFactor( this ),
u_LightFactor( this ),
u_LightRadius( this ),
u_LightScale( this ),
u_LightAttenuationMatrix( this ),
Expand Down Expand Up @@ -2467,7 +2465,7 @@ GLShader_forwardLighting_projXYZ::GLShader_forwardLighting_projXYZ( GLShaderMana
u_ViewOrigin( this ),
u_LightOrigin( this ),
u_LightColor( this ),
u_InverseLightFactor( this ),
u_LightFactor( this ),
u_LightRadius( this ),
u_LightScale( this ),
u_LightAttenuationMatrix( this ),
Expand Down Expand Up @@ -2532,7 +2530,7 @@ GLShader_forwardLighting_directionalSun::GLShader_forwardLighting_directionalSun
u_ViewOrigin( this ),
u_LightDir( this ),
u_LightColor( this ),
u_InverseLightFactor( this ),
u_LightFactor( this ),
u_LightRadius( this ),
u_LightScale( this ),
u_LightAttenuationMatrix( this ),
Expand Down Expand Up @@ -2622,7 +2620,7 @@ GLShader_reflection::GLShader_reflection( GLShaderManager *manager ):
u_NormalScale( this ),
u_VertexInterpolation( this ),
u_CameraPosition( this ),
u_InverseLightFactor( this ),
u_LightFactor( this ),
GLDeformStage( this ),
GLCompileMacro_USE_VERTEX_SKINNING( this ),
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
Expand Down Expand Up @@ -2651,7 +2649,7 @@ GLShader_reflectionMaterial::GLShader_reflectionMaterial( GLShaderManager* manag
u_ReliefOffsetBias( this ),
u_NormalScale( this ),
u_CameraPosition( this ),
u_InverseLightFactor( this ),
u_LightFactor( this ),
GLDeformStage( this ),
GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP( this ),
GLCompileMacro_USE_RELIEF_MAPPING( this ) {
Expand All @@ -2673,8 +2671,7 @@ GLShader_skybox::GLShader_skybox( GLShaderManager *manager ) :
u_UseCloudMap( this ),
u_AlphaThreshold( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this )
u_ModelViewProjectionMatrix( this )
{
}

Expand All @@ -2694,9 +2691,8 @@ GLShader_skyboxMaterial::GLShader_skyboxMaterial( GLShaderManager* manager ) :
u_UseCloudMap( this ),
u_AlphaThreshold( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ) {
}
u_ModelViewProjectionMatrix( this )
{}

void GLShader_skyboxMaterial::SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) {
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
Expand All @@ -2708,7 +2704,6 @@ GLShader_fogQuake3::GLShader_fogQuake3( GLShaderManager *manager ) :
u_FogMap( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_Color( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
Expand All @@ -2731,7 +2726,6 @@ GLShader_fogQuake3Material::GLShader_fogQuake3Material( GLShaderManager* manager
u_FogMap( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this ),
u_Color( this ),
u_FogDistanceVector( this ),
u_FogDepthVector( this ),
Expand All @@ -2751,7 +2745,6 @@ GLShader_fogGlobal::GLShader_fogGlobal( GLShaderManager *manager ) :
u_ViewMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_UnprojectMatrix( this ),
u_InverseLightFactor( this ),
u_Color( this ),
u_FogDistanceVector( this ),
u_FogDepthVector( this )
Expand Down Expand Up @@ -2861,8 +2854,7 @@ void GLShader_portal::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
GLShader_contrast::GLShader_contrast( GLShaderManager *manager ) :
GLShader( "contrast", ATTR_POSITION, manager ),
u_ColorMap( this ),
u_ModelViewProjectionMatrix( this ),
u_InverseLightFactor( this )
u_ModelViewProjectionMatrix( this )
{
}

Expand All @@ -2878,7 +2870,6 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
u_ColorModulate( this ),
u_TextureMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_LightFactor( this ),
u_DeformMagnitude( this ),
u_InverseGamma( this )
{
Expand Down
46 changes: 11 additions & 35 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ class u_LightFactor :
{
public:
u_LightFactor( GLShader *shader ) :
GLUniform1f( shader, "u_LightFactor" )
GLUniform1f( shader, "u_LightFactor", true )
{
}

Expand All @@ -2150,21 +2150,6 @@ class u_LightFactor :
}
};

class u_InverseLightFactor :
GLUniform1f
{
public:
u_InverseLightFactor( GLShader *shader ) :
GLUniform1f( shader, "u_InverseLightFactor" )
{
}

void SetUniform_InverseLightFactor( const float inverseLightFactor )
{
this->SetValue( inverseLightFactor );
}
};

class u_ColorMap :
GLUniformSampler2D {
public:
Expand Down Expand Up @@ -3946,7 +3931,6 @@ class GLShader_generic :
public u_AlphaThreshold,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_ColorModulate,
public u_Color,
public u_Bones,
Expand Down Expand Up @@ -3976,7 +3960,6 @@ class GLShader_genericMaterial :
public u_AlphaThreshold,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_ColorModulate,
public u_Color,
public u_DepthScale,
Expand Down Expand Up @@ -4015,7 +3998,7 @@ class GLShader_lightMapping :
public u_ViewOrigin,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_LightFactor,
public u_Bones,
public u_VertexInterpolation,
public u_ReliefDepthScale,
Expand Down Expand Up @@ -4067,7 +4050,7 @@ class GLShader_lightMappingMaterial :
public u_ViewOrigin,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_LightFactor,
public u_ReliefDepthScale,
public u_ReliefOffsetBias,
public u_NormalScale,
Expand Down Expand Up @@ -4113,7 +4096,7 @@ class GLShader_forwardLighting_omniXYZ :
public u_ViewOrigin,
public u_LightOrigin,
public u_LightColor,
public u_InverseLightFactor,
public u_LightFactor,
public u_LightRadius,
public u_LightScale,
public u_LightAttenuationMatrix,
Expand Down Expand Up @@ -4157,7 +4140,7 @@ class GLShader_forwardLighting_projXYZ :
public u_ViewOrigin,
public u_LightOrigin,
public u_LightColor,
public u_InverseLightFactor,
public u_LightFactor,
public u_LightRadius,
public u_LightScale,
public u_LightAttenuationMatrix,
Expand Down Expand Up @@ -4208,7 +4191,7 @@ class GLShader_forwardLighting_directionalSun :
public u_ViewOrigin,
public u_LightDir,
public u_LightColor,
public u_InverseLightFactor,
public u_LightFactor,
public u_LightRadius,
public u_LightScale,
public u_LightAttenuationMatrix,
Expand Down Expand Up @@ -4275,7 +4258,7 @@ class GLShader_reflection :
public u_NormalScale,
public u_VertexInterpolation,
public u_CameraPosition,
public u_InverseLightFactor,
public u_LightFactor,
public GLDeformStage,
public GLCompileMacro_USE_VERTEX_SKINNING,
public GLCompileMacro_USE_VERTEX_ANIMATION,
Expand All @@ -4300,7 +4283,7 @@ class GLShader_reflectionMaterial :
public u_ReliefOffsetBias,
public u_NormalScale,
public u_CameraPosition,
public u_InverseLightFactor,
public u_LightFactor,
public GLDeformStage,
public GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP,
public GLCompileMacro_USE_RELIEF_MAPPING {
Expand All @@ -4319,8 +4302,7 @@ class GLShader_skybox :
public u_UseCloudMap,
public u_AlphaThreshold,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor
public u_ModelViewProjectionMatrix
{
public:
GLShader_skybox( GLShaderManager *manager );
Expand All @@ -4337,8 +4319,7 @@ class GLShader_skyboxMaterial :
public u_UseCloudMap,
public u_AlphaThreshold,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor {
public u_ModelViewProjectionMatrix {
public:
GLShader_skyboxMaterial( GLShaderManager* manager );
void SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) override;
Expand All @@ -4349,7 +4330,6 @@ class GLShader_fogQuake3 :
public u_FogMap,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_Color,
public u_Bones,
public u_VertexInterpolation,
Expand All @@ -4370,7 +4350,6 @@ class GLShader_fogQuake3Material :
public u_FogMap,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor,
public u_Color,
public u_FogDistanceVector,
public u_FogDepthVector,
Expand All @@ -4389,7 +4368,6 @@ class GLShader_fogGlobal :
public u_ViewMatrix,
public u_ModelViewProjectionMatrix,
public u_UnprojectMatrix,
public u_InverseLightFactor,
public u_Color,
public u_FogDistanceVector,
public u_FogDepthVector
Expand Down Expand Up @@ -4484,8 +4462,7 @@ class GLShader_portal :
class GLShader_contrast :
public GLShader,
public u_ColorMap,
public u_ModelViewProjectionMatrix,
public u_InverseLightFactor
public u_ModelViewProjectionMatrix
{
public:
GLShader_contrast( GLShaderManager *manager );
Expand All @@ -4499,7 +4476,6 @@ class GLShader_cameraEffects :
public u_ColorModulate,
public u_TextureMatrix,
public u_ModelViewProjectionMatrix,
public u_LightFactor,
public u_DeformMagnitude,
public u_InverseGamma
{
Expand Down
4 changes: 0 additions & 4 deletions src/engine/renderer/glsl_source/cameraEffects_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ uniform sampler2D u_CurrentMap;
uniform sampler3D u_ColorMap3D;
#endif

uniform float u_LightFactor;

uniform vec4 u_ColorModulate;
uniform float u_InverseGamma;

Expand All @@ -44,8 +42,6 @@ void main()

vec4 color = texture2D(u_CurrentMap, st);

color.rgb *= u_LightFactor;

color = clamp(color, 0.0, 1.0);

#if defined(r_colorGrading)
Expand Down
Loading

0 comments on commit bcab067

Please sign in to comment.