Skip to content

Commit fb62668

Browse files
committed
Fix luminance overflow
1 parent 9933a16 commit fb62668

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
@@ -3078,6 +3078,7 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
30783078
u_Tonemap( this ),
30793079
u_TonemapAdaptiveExposure( this ),
30803080
u_TonemapParms( this ),
3081+
u_TonemapParms2( this ),
30813082
u_TonemapExposure( this ),
30823083
u_InverseGamma( this )
30833084
{

src/engine/renderer/gl_shader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4630,6 +4630,7 @@ class GLShader_cameraEffects :
46304630
public u_Tonemap,
46314631
public u_TonemapAdaptiveExposure,
46324632
public u_TonemapParms,
4633+
public u_TonemapParms2,
46334634
public u_TonemapExposure,
46344635
public u_InverseGamma
46354636
{

src/engine/renderer/glsl_source/cameraEffects_fp.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ y: highlightsCompressionSpeed
5454
z: shoulderClip
5555
w: highlightsCompression */
5656
uniform vec4 u_TonemapParms;
57+
uniform vec4 u_TonemapParms2;
5758
uniform float u_TonemapExposure;
5859

5960
vec3 TonemapLottes( vec3 color ) {
@@ -64,7 +65,7 @@ vec3 TonemapLottes( vec3 color ) {
6465
#endif
6566

6667
float GetAverageLuminance( const in uint luminance ) {
67-
return float( luminanceU ) / ( 256.0f * u_ViewWidth * u_ViewHeight );
68+
return float( luminanceU ) / ( u_TonemapParms2[1] * u_ViewWidth * u_ViewHeight );
6869
}
6970

7071
void main() {

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
@@ -3305,8 +3305,8 @@ static void AdaptiveLightingReduction() {
33053305

33063306
gl_luminanceReductionShader->BindProgram( 0 );
33073307

3308-
const int width = tr.currentRenderImage[backEnd.currentMainFBO]->width;
3309-
const int height = tr.currentRenderImage[backEnd.currentMainFBO]->height;
3308+
const int width = glConfig.vidWidth;
3309+
const int height = glConfig.vidHeight;
33103310

33113311
uint32_t globalWorkgroupX = ( width + 7 ) / 8;
33123312
uint32_t globalWorkgroupY = ( height + 7 ) / 8;
@@ -3316,6 +3316,7 @@ static void AdaptiveLightingReduction() {
33163316
gl_luminanceReductionShader->SetUniform_ViewWidth( width );
33173317
gl_luminanceReductionShader->SetUniform_ViewHeight( height );
33183318
vec4_t parms { log2f( r_toneMappingHDRMax.Get() ) };
3319+
parms[1] = UINT32_MAX / ( width * height * ( parms[0] + 8.0f ) );
33193320
gl_luminanceReductionShader->SetUniform_TonemapParms2( parms );
33203321

33213322
glMemoryBarrier( GL_ATOMIC_COUNTER_BARRIER_BIT );
@@ -3395,6 +3396,10 @@ void RB_CameraPostFX()
33953396
r_toneMappingDarkAreaPointHDR.Get(), r_toneMappingDarkAreaPointLDR.Get(), tonemapParms[2], tonemapParms[3] );
33963397
gl_cameraEffectsShader->SetUniform_TonemapParms( tonemapParms );
33973398

3399+
vec4_t parms{ log2f( r_toneMappingHDRMax.Get() ) };
3400+
parms[1] = UINT32_MAX / ( glConfig.vidWidth * glConfig.vidHeight * ( parms[0] + 8.0f ) );
3401+
gl_cameraEffectsShader->SetUniform_TonemapParms2( parms );
3402+
33983403
gl_cameraEffectsShader->SetUniform_TonemapAdaptiveExposure(
33993404
glConfig2.adaptiveExposureAvailable && r_toneMappingAdaptiveExposure.Get() );
34003405
gl_cameraEffectsShader->SetUniform_TonemapExposure( r_toneMappingExposure.Get() );

0 commit comments

Comments
 (0)