Skip to content

Commit 344932f

Browse files
slipherillwieckz
andcommitted
Simplify full-range overbright implementation
Multiply lightmaps and light grid by the light factor right when they are used, instead of the elaborate canceling system. Vertex light multiplication in lightmapped stages is handled by the scaling of the white image lightmap (treated as 4.0). Various bugs will be fixed, since the canceling approach relied on detecting an enormous amount of special cases and not all of them were handled. 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. When limited to RGBA8 framebuffers, as can be tried with r_highPrecisionRendering 0, this rewrite is a mixed bag. In some places it leads to clamping in the color buffer (example: Pulse viewpos 4822 -1312 -1314 98 13), while in others it improves results due to the effective 2 extra bits of precision in the framebuffer (example: Procyon viewpos -955 3443 276 94 7). There are a few cases where lightmap/lightgrid scaling doesn't apply that required special handling: - For intermediate stages of light styles, the already-existing detection of STYLELIGHTMAP and STYLECOLORMAP is used. For these stage types u_Color is scaled by the light factor. Example: metro-b1-2 viewpos -1172 627 107 94 -21. - For non-lightmapped vertex-lit surfaces, u_ColorModulate is scaled by the light factor. Example: station15 small plants. - For fullbright lightmapped stages, the light factor is set to 1. Example: Procyon star chart. Some bugs (in non-clamped overbright mode) that are fixed by this commit: - Non-lightmapped vertex-lit surfaces (like the station15 plants) are now scaled by the light factor as they should be. - Procyon star chart is no longer too dark with material system. - Glowmap-like stage implemented with q3 syntax in a control panel shader are no longer wrongly overbright-scaled, causing visual noise. zittrig-arena viewpos 1201 -301 -338 87 0 Co-authored-by: Thomas Debesse <dev@illwieckz.net>
1 parent e5a4caf commit 344932f

21 files changed

Lines changed: 50 additions & 264 deletions

src/engine/renderer/Material.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,12 @@ void UpdateSurfaceDataGeneric3D( uint32_t* materials, Material& material, drawSu
222222
// u_AlphaThreshold
223223
gl_genericShaderMaterial->SetUniform_AlphaTest( pStage->stateBits );
224224

225-
// u_InverseLightFactor
226-
float inverseLightFactor = pStage->cancelOverBright ? tr.mapInverseLightFactor : 1.0f;
227-
gl_genericShaderMaterial->SetUniform_InverseLightFactor( inverseLightFactor );
228-
229225
// u_ColorModulate
230226
colorGen_t rgbGen = SetRgbGen( pStage );
231227
alphaGen_t alphaGen = SetAlphaGen( pStage );
232228

233-
gl_genericShaderMaterial->SetUniform_ColorModulate( rgbGen, alphaGen );
229+
bool mayUseVertexOverbright = pStage->type == stageType_t::ST_COLORMAP && drawSurf->bspSurface;
230+
gl_genericShaderMaterial->SetUniform_ColorModulate( rgbGen, alphaGen, mayUseVertexOverbright );
234231

235232
Tess_ComputeColor( pStage );
236233
gl_genericShaderMaterial->SetUniform_Color( tess.svars.color );
@@ -291,12 +288,9 @@ void UpdateSurfaceDataLightMapping( uint32_t* materials, Material& material, dra
291288
bool enableGridLighting = ( lightMode == lightMode_t::GRID );
292289
bool enableGridDeluxeMapping = ( deluxeMode == deluxeMode_t::GRID );
293290

294-
// u_InverseLightFactor
295-
/* HACK: use sign to know if there is a light or not, and
296-
then if it will receive overbright multiplication or not. */
297-
bool cancelOverBright = pStage->cancelOverBright || lightMode == lightMode_t::FULLBRIGHT;
298-
float inverseLightFactor = cancelOverBright ? tr.mapInverseLightFactor : -tr.mapInverseLightFactor;
299-
gl_lightMappingShaderMaterial->SetUniform_InverseLightFactor( inverseLightFactor );
291+
// u_LightFactor
292+
gl_lightMappingShaderMaterial->SetUniform_LightFactor(
293+
lightMode == lightMode_t::FULLBRIGHT ? 1.0f : tr.mapLightFactor );
300294

301295
// u_ColorModulate
302296
gl_lightMappingShaderMaterial->SetUniform_ColorModulate( rgbGen, alphaGen );
@@ -470,9 +464,6 @@ void UpdateSurfaceDataSkybox( uint32_t* materials, Material& material, drawSurf_
470464
// u_AlphaThreshold
471465
gl_skyboxShaderMaterial->SetUniform_AlphaTest( GLS_ATEST_NONE );
472466

473-
// u_InverseLightFactor
474-
gl_skyboxShaderMaterial->SetUniform_InverseLightFactor( tr.mapInverseLightFactor );
475-
476467
gl_skyboxShaderMaterial->WriteUniformsToBuffer( materials );
477468
}
478469

@@ -620,9 +611,6 @@ void UpdateSurfaceDataFog( uint32_t* materials, Material& material, drawSurf_t*
620611

621612
const fog_t* fog = material.fog;
622613

623-
// u_InverseLightFactor
624-
gl_fogQuake3ShaderMaterial->SetUniform_InverseLightFactor( tr.mapInverseLightFactor );
625-
626614
// u_Color
627615
gl_fogQuake3ShaderMaterial->SetUniform_Color( fog->color );
628616

src/engine/renderer/gl_shader.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,6 @@ GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
22102210
u_AlphaThreshold( this ),
22112211
u_ModelMatrix( this ),
22122212
u_ModelViewProjectionMatrix( this ),
2213-
u_InverseLightFactor( this ),
22142213
u_ColorModulate( this ),
22152214
u_Color( this ),
22162215
u_Bones( this ),
@@ -2243,7 +2242,6 @@ GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
22432242
u_AlphaThreshold( this ),
22442243
u_ModelMatrix( this ),
22452244
u_ModelViewProjectionMatrix( this ),
2246-
u_InverseLightFactor( this ),
22472245
u_ColorModulate( this ),
22482246
u_Color( this ),
22492247
u_DepthScale( this ),
@@ -2285,7 +2283,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
22852283
u_ViewOrigin( this ),
22862284
u_ModelMatrix( this ),
22872285
u_ModelViewProjectionMatrix( this ),
2288-
u_InverseLightFactor( this ),
2286+
u_LightFactor( this ),
22892287
u_Bones( this ),
22902288
u_VertexInterpolation( this ),
22912289
u_ReliefDepthScale( this ),
@@ -2354,7 +2352,7 @@ GLShader_lightMappingMaterial::GLShader_lightMappingMaterial( GLShaderManager* m
23542352
u_ViewOrigin( this ),
23552353
u_ModelMatrix( this ),
23562354
u_ModelViewProjectionMatrix( this ),
2357-
u_InverseLightFactor( this ),
2355+
u_LightFactor( this ),
23582356
u_ReliefDepthScale( this ),
23592357
u_ReliefOffsetBias( this ),
23602358
u_NormalScale( this ),
@@ -2413,7 +2411,6 @@ GLShader_forwardLighting_omniXYZ::GLShader_forwardLighting_omniXYZ( GLShaderMana
24132411
u_ViewOrigin( this ),
24142412
u_LightOrigin( this ),
24152413
u_LightColor( this ),
2416-
u_InverseLightFactor( this ),
24172414
u_LightRadius( this ),
24182415
u_LightScale( this ),
24192416
u_LightAttenuationMatrix( this ),
@@ -2467,7 +2464,6 @@ GLShader_forwardLighting_projXYZ::GLShader_forwardLighting_projXYZ( GLShaderMana
24672464
u_ViewOrigin( this ),
24682465
u_LightOrigin( this ),
24692466
u_LightColor( this ),
2470-
u_InverseLightFactor( this ),
24712467
u_LightRadius( this ),
24722468
u_LightScale( this ),
24732469
u_LightAttenuationMatrix( this ),
@@ -2532,7 +2528,6 @@ GLShader_forwardLighting_directionalSun::GLShader_forwardLighting_directionalSun
25322528
u_ViewOrigin( this ),
25332529
u_LightDir( this ),
25342530
u_LightColor( this ),
2535-
u_InverseLightFactor( this ),
25362531
u_LightRadius( this ),
25372532
u_LightScale( this ),
25382533
u_LightAttenuationMatrix( this ),
@@ -2622,7 +2617,6 @@ GLShader_reflection::GLShader_reflection( GLShaderManager *manager ):
26222617
u_NormalScale( this ),
26232618
u_VertexInterpolation( this ),
26242619
u_CameraPosition( this ),
2625-
u_InverseLightFactor( this ),
26262620
GLDeformStage( this ),
26272621
GLCompileMacro_USE_VERTEX_SKINNING( this ),
26282622
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
@@ -2651,7 +2645,6 @@ GLShader_reflectionMaterial::GLShader_reflectionMaterial( GLShaderManager* manag
26512645
u_ReliefOffsetBias( this ),
26522646
u_NormalScale( this ),
26532647
u_CameraPosition( this ),
2654-
u_InverseLightFactor( this ),
26552648
GLDeformStage( this ),
26562649
GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP( this ),
26572650
GLCompileMacro_USE_RELIEF_MAPPING( this ) {
@@ -2673,8 +2666,7 @@ GLShader_skybox::GLShader_skybox( GLShaderManager *manager ) :
26732666
u_UseCloudMap( this ),
26742667
u_AlphaThreshold( this ),
26752668
u_ModelMatrix( this ),
2676-
u_ModelViewProjectionMatrix( this ),
2677-
u_InverseLightFactor( this )
2669+
u_ModelViewProjectionMatrix( this )
26782670
{
26792671
}
26802672

@@ -2694,9 +2686,8 @@ GLShader_skyboxMaterial::GLShader_skyboxMaterial( GLShaderManager* manager ) :
26942686
u_UseCloudMap( this ),
26952687
u_AlphaThreshold( this ),
26962688
u_ModelMatrix( this ),
2697-
u_ModelViewProjectionMatrix( this ),
2698-
u_InverseLightFactor( this ) {
2699-
}
2689+
u_ModelViewProjectionMatrix( this )
2690+
{}
27002691

27012692
void GLShader_skyboxMaterial::SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) {
27022693
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
@@ -2708,7 +2699,6 @@ GLShader_fogQuake3::GLShader_fogQuake3( GLShaderManager *manager ) :
27082699
u_FogMap( this ),
27092700
u_ModelMatrix( this ),
27102701
u_ModelViewProjectionMatrix( this ),
2711-
u_InverseLightFactor( this ),
27122702
u_Color( this ),
27132703
u_Bones( this ),
27142704
u_VertexInterpolation( this ),
@@ -2731,7 +2721,6 @@ GLShader_fogQuake3Material::GLShader_fogQuake3Material( GLShaderManager* manager
27312721
u_FogMap( this ),
27322722
u_ModelMatrix( this ),
27332723
u_ModelViewProjectionMatrix( this ),
2734-
u_InverseLightFactor( this ),
27352724
u_Color( this ),
27362725
u_FogDistanceVector( this ),
27372726
u_FogDepthVector( this ),
@@ -2751,7 +2740,6 @@ GLShader_fogGlobal::GLShader_fogGlobal( GLShaderManager *manager ) :
27512740
u_ViewMatrix( this ),
27522741
u_ModelViewProjectionMatrix( this ),
27532742
u_UnprojectMatrix( this ),
2754-
u_InverseLightFactor( this ),
27552743
u_Color( this ),
27562744
u_FogDistanceVector( this ),
27572745
u_FogDepthVector( this )
@@ -2861,8 +2849,7 @@ void GLShader_portal::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
28612849
GLShader_contrast::GLShader_contrast( GLShaderManager *manager ) :
28622850
GLShader( "contrast", ATTR_POSITION, manager ),
28632851
u_ColorMap( this ),
2864-
u_ModelViewProjectionMatrix( this ),
2865-
u_InverseLightFactor( this )
2852+
u_ModelViewProjectionMatrix( this )
28662853
{
28672854
}
28682855

@@ -2878,7 +2865,6 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
28782865
u_ColorModulate( this ),
28792866
u_TextureMatrix( this ),
28802867
u_ModelViewProjectionMatrix( this ),
2881-
u_LightFactor( this ),
28822868
u_DeformMagnitude( this ),
28832869
u_InverseGamma( this )
28842870
{

src/engine/renderer/gl_shader.h

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,21 +2150,6 @@ class u_LightFactor :
21502150
}
21512151
};
21522152

2153-
class u_InverseLightFactor :
2154-
GLUniform1f
2155-
{
2156-
public:
2157-
u_InverseLightFactor( GLShader *shader ) :
2158-
GLUniform1f( shader, "u_InverseLightFactor" )
2159-
{
2160-
}
2161-
2162-
void SetUniform_InverseLightFactor( const float inverseLightFactor )
2163-
{
2164-
this->SetValue( inverseLightFactor );
2165-
}
2166-
};
2167-
21682153
class u_ColorMap :
21692154
GLUniformSampler2D {
21702155
public:
@@ -3617,7 +3602,7 @@ class u_ColorModulate :
36173602
{
36183603
this->SetValue( v );
36193604
}
3620-
void SetUniform_ColorModulate( colorGen_t colorGen, alphaGen_t alphaGen )
3605+
void SetUniform_ColorModulate( colorGen_t colorGen, alphaGen_t alphaGen, bool vertexOverbright = false )
36213606
{
36223607
vec4_t v;
36233608
bool needAttrib = false;
@@ -3631,7 +3616,16 @@ class u_ColorModulate :
36313616
{
36323617
case colorGen_t::CGEN_VERTEX:
36333618
needAttrib = true;
3634-
VectorSet( v, 1, 1, 1 );
3619+
if ( vertexOverbright )
3620+
{
3621+
// vertexOverbright is only needed for non-lightmapped cases. When there is a
3622+
// lightmap, this is done by multiplying with the overbright-scaled white image
3623+
VectorSet( v, tr.mapLightFactor, tr.mapLightFactor, tr.mapLightFactor );
3624+
}
3625+
else
3626+
{
3627+
VectorSet( v, 1, 1, 1 );
3628+
}
36353629
break;
36363630

36373631
case colorGen_t::CGEN_ONE_MINUS_VERTEX:
@@ -3946,7 +3940,6 @@ class GLShader_generic :
39463940
public u_AlphaThreshold,
39473941
public u_ModelMatrix,
39483942
public u_ModelViewProjectionMatrix,
3949-
public u_InverseLightFactor,
39503943
public u_ColorModulate,
39513944
public u_Color,
39523945
public u_Bones,
@@ -3976,7 +3969,6 @@ class GLShader_genericMaterial :
39763969
public u_AlphaThreshold,
39773970
public u_ModelMatrix,
39783971
public u_ModelViewProjectionMatrix,
3979-
public u_InverseLightFactor,
39803972
public u_ColorModulate,
39813973
public u_Color,
39823974
public u_DepthScale,
@@ -4015,7 +4007,7 @@ class GLShader_lightMapping :
40154007
public u_ViewOrigin,
40164008
public u_ModelMatrix,
40174009
public u_ModelViewProjectionMatrix,
4018-
public u_InverseLightFactor,
4010+
public u_LightFactor,
40194011
public u_Bones,
40204012
public u_VertexInterpolation,
40214013
public u_ReliefDepthScale,
@@ -4067,7 +4059,7 @@ class GLShader_lightMappingMaterial :
40674059
public u_ViewOrigin,
40684060
public u_ModelMatrix,
40694061
public u_ModelViewProjectionMatrix,
4070-
public u_InverseLightFactor,
4062+
public u_LightFactor,
40714063
public u_ReliefDepthScale,
40724064
public u_ReliefOffsetBias,
40734065
public u_NormalScale,
@@ -4113,7 +4105,6 @@ class GLShader_forwardLighting_omniXYZ :
41134105
public u_ViewOrigin,
41144106
public u_LightOrigin,
41154107
public u_LightColor,
4116-
public u_InverseLightFactor,
41174108
public u_LightRadius,
41184109
public u_LightScale,
41194110
public u_LightAttenuationMatrix,
@@ -4157,7 +4148,6 @@ class GLShader_forwardLighting_projXYZ :
41574148
public u_ViewOrigin,
41584149
public u_LightOrigin,
41594150
public u_LightColor,
4160-
public u_InverseLightFactor,
41614151
public u_LightRadius,
41624152
public u_LightScale,
41634153
public u_LightAttenuationMatrix,
@@ -4208,7 +4198,6 @@ class GLShader_forwardLighting_directionalSun :
42084198
public u_ViewOrigin,
42094199
public u_LightDir,
42104200
public u_LightColor,
4211-
public u_InverseLightFactor,
42124201
public u_LightRadius,
42134202
public u_LightScale,
42144203
public u_LightAttenuationMatrix,
@@ -4275,7 +4264,6 @@ class GLShader_reflection :
42754264
public u_NormalScale,
42764265
public u_VertexInterpolation,
42774266
public u_CameraPosition,
4278-
public u_InverseLightFactor,
42794267
public GLDeformStage,
42804268
public GLCompileMacro_USE_VERTEX_SKINNING,
42814269
public GLCompileMacro_USE_VERTEX_ANIMATION,
@@ -4300,7 +4288,6 @@ class GLShader_reflectionMaterial :
43004288
public u_ReliefOffsetBias,
43014289
public u_NormalScale,
43024290
public u_CameraPosition,
4303-
public u_InverseLightFactor,
43044291
public GLDeformStage,
43054292
public GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP,
43064293
public GLCompileMacro_USE_RELIEF_MAPPING {
@@ -4319,8 +4306,7 @@ class GLShader_skybox :
43194306
public u_UseCloudMap,
43204307
public u_AlphaThreshold,
43214308
public u_ModelMatrix,
4322-
public u_ModelViewProjectionMatrix,
4323-
public u_InverseLightFactor
4309+
public u_ModelViewProjectionMatrix
43244310
{
43254311
public:
43264312
GLShader_skybox( GLShaderManager *manager );
@@ -4337,8 +4323,7 @@ class GLShader_skyboxMaterial :
43374323
public u_UseCloudMap,
43384324
public u_AlphaThreshold,
43394325
public u_ModelMatrix,
4340-
public u_ModelViewProjectionMatrix,
4341-
public u_InverseLightFactor {
4326+
public u_ModelViewProjectionMatrix {
43424327
public:
43434328
GLShader_skyboxMaterial( GLShaderManager* manager );
43444329
void SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) override;
@@ -4349,7 +4334,6 @@ class GLShader_fogQuake3 :
43494334
public u_FogMap,
43504335
public u_ModelMatrix,
43514336
public u_ModelViewProjectionMatrix,
4352-
public u_InverseLightFactor,
43534337
public u_Color,
43544338
public u_Bones,
43554339
public u_VertexInterpolation,
@@ -4370,7 +4354,6 @@ class GLShader_fogQuake3Material :
43704354
public u_FogMap,
43714355
public u_ModelMatrix,
43724356
public u_ModelViewProjectionMatrix,
4373-
public u_InverseLightFactor,
43744357
public u_Color,
43754358
public u_FogDistanceVector,
43764359
public u_FogDepthVector,
@@ -4389,7 +4372,6 @@ class GLShader_fogGlobal :
43894372
public u_ViewMatrix,
43904373
public u_ModelViewProjectionMatrix,
43914374
public u_UnprojectMatrix,
4392-
public u_InverseLightFactor,
43934375
public u_Color,
43944376
public u_FogDistanceVector,
43954377
public u_FogDepthVector
@@ -4484,8 +4466,7 @@ class GLShader_portal :
44844466
class GLShader_contrast :
44854467
public GLShader,
44864468
public u_ColorMap,
4487-
public u_ModelViewProjectionMatrix,
4488-
public u_InverseLightFactor
4469+
public u_ModelViewProjectionMatrix
44894470
{
44904471
public:
44914472
GLShader_contrast( GLShaderManager *manager );
@@ -4499,7 +4480,6 @@ class GLShader_cameraEffects :
44994480
public u_ColorModulate,
45004481
public u_TextureMatrix,
45014482
public u_ModelViewProjectionMatrix,
4502-
public u_LightFactor,
45034483
public u_DeformMagnitude,
45044484
public u_InverseGamma
45054485
{

src/engine/renderer/glsl_source/cameraEffects_fp.glsl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ uniform sampler2D u_CurrentMap;
2828
uniform sampler3D u_ColorMap3D;
2929
#endif
3030

31-
uniform float u_LightFactor;
32-
3331
uniform vec4 u_ColorModulate;
3432
uniform float u_InverseGamma;
3533

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

4543
vec4 color = texture2D(u_CurrentMap, st);
4644

47-
color.rgb *= u_LightFactor;
48-
4945
color = clamp(color, 0.0, 1.0);
5046

5147
#if defined(r_colorGrading)

0 commit comments

Comments
 (0)