Skip to content

Commit da7f9dd

Browse files
committed
Fix luminance overflow
1 parent cc75fac commit da7f9dd

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,6 +3102,7 @@ GLShader_cameraEffects::GLShader_cameraEffects() :
31023102
u_Tonemap( this ),
31033103
u_TonemapAdaptiveExposure( this ),
31043104
u_TonemapParms( this ),
3105+
u_TonemapParms2( this ),
31053106
u_TonemapExposure( this ),
31063107
u_InverseGamma( this ) {
31073108
}

src/engine/renderer/gl_shader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4591,6 +4591,7 @@ class GLShader_cameraEffects :
45914591
public u_Tonemap,
45924592
public u_TonemapAdaptiveExposure,
45934593
public u_TonemapParms,
4594+
public u_TonemapParms2,
45944595
public u_TonemapExposure,
45954596
public u_InverseGamma
45964597
{

src/engine/renderer/glsl_source/cameraEffects_fp.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ y: highlightsCompressionSpeed
4444
z: shoulderClip
4545
w: highlightsCompression */
4646
uniform vec4 u_TonemapParms;
47+
uniform vec4 u_TonemapParms2;
4748
uniform float u_TonemapExposure;
4849

4950
vec3 TonemapLottes( vec3 color ) {
@@ -59,7 +60,7 @@ vec3 TonemapLottes( vec3 color ) {
5960
#endif
6061

6162
float GetAverageLuminance( const in uint luminance ) {
62-
return float( luminanceU ) / ( 256.0f * u_ViewWidth * u_ViewHeight );
63+
return float( luminanceU ) / ( u_TonemapParms2[1] * u_ViewWidth * u_ViewHeight );
6364
}
6465
#endif
6566

src/engine/renderer/glsl_source/luminanceReduction_cp.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ float ColorToLuminance( const in vec3 color ) {
6363
}
6464

6565
uint FloatLuminanceToUint( const in float luminance ) {
66-
return uint( luminance * 256 );
66+
return uint( luminance * u_TonemapParms2[1] );
6767
}
6868

6969
void main() {

src/engine/renderer/tr_backend.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,8 +3221,8 @@ static void AdaptiveLightingReduction() {
32213221

32223222
gl_luminanceReductionShader->BindProgram( 0 );
32233223

3224-
const int width = tr.currentRenderImage[backEnd.currentMainFBO]->width;
3225-
const int height = tr.currentRenderImage[backEnd.currentMainFBO]->height;
3224+
const int width = glConfig.vidWidth;
3225+
const int height = glConfig.vidHeight;
32263226

32273227
uint32_t globalWorkgroupX = ( width + 7 ) / 8;
32283228
uint32_t globalWorkgroupY = ( height + 7 ) / 8;
@@ -3232,6 +3232,7 @@ static void AdaptiveLightingReduction() {
32323232
gl_luminanceReductionShader->SetUniform_ViewWidth( width );
32333233
gl_luminanceReductionShader->SetUniform_ViewHeight( height );
32343234
vec4_t parms { log2f( r_toneMappingHDRMax.Get() ) };
3235+
parms[1] = UINT32_MAX / ( width * height * ( uint32_t( parms[0] ) + 8 ) );
32353236
gl_luminanceReductionShader->SetUniform_TonemapParms2( parms );
32363237

32373238
glMemoryBarrier( GL_ATOMIC_COUNTER_BARRIER_BIT );
@@ -3299,6 +3300,10 @@ void RB_CameraPostFX() {
32993300
r_toneMappingDarkAreaPointHDR.Get(), r_toneMappingDarkAreaPointLDR.Get(), tonemapParms[2], tonemapParms[3] );
33003301
gl_cameraEffectsShader->SetUniform_TonemapParms( tonemapParms );
33013302

3303+
vec4_t parms{ log2f( r_toneMappingHDRMax.Get() ) };
3304+
parms[1] = UINT32_MAX / ( glConfig.vidWidth * glConfig.vidHeight * ( uint32_t( parms[0] ) + 8 ) );
3305+
gl_cameraEffectsShader->SetUniform_TonemapParms2( parms );
3306+
33023307
gl_cameraEffectsShader->SetUniform_TonemapAdaptiveExposure(
33033308
glConfig2.adaptiveExposureAvailable && r_toneMappingAdaptiveExposure.Get() );
33043309
gl_cameraEffectsShader->SetUniform_TonemapExposure( r_toneMappingExposure.Get() );

0 commit comments

Comments
 (0)