Skip to content

Implement adaptive exposure #1559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/crunch
Submodule crunch updated 1 files
+1 −1 CMakeLists.txt
2 changes: 2 additions & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ set(GLSLSOURCELIST
${ENGINE_DIR}/renderer/glsl_source/lighttile_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/computeLight_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/reliefMapping_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/luminanceReduction_cp.glsl
${ENGINE_DIR}/renderer/glsl_source/clearFrameData_cp.glsl

# Common vertex shader libraries
${ENGINE_DIR}/renderer/glsl_source/deformVertexes_vp.glsl
Expand Down
2 changes: 2 additions & 0 deletions src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ enum class BufferBind {
PORTAL_SURFACES = 5,
GEOMETRY_CACHE_INPUT_VBO = 6,
GEOMETRY_CACHE_VBO = 7,
LUMINANCE = 3,
LUMINANCE_STORAGE = 8,
DEBUG = 10,
UNUSED = INT32_MAX
};
Expand Down
50 changes: 45 additions & 5 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ GLShader_fxaa *gl_fxaaShader = nullptr;
GLShader_motionblur *gl_motionblurShader = nullptr;
GLShader_ssao *gl_ssaoShader = nullptr;

GLShader_depthtile1 *gl_depthtile1Shader = nullptr;
GLShader_depthtile2 *gl_depthtile2Shader = nullptr;
GLShader_lighttile *gl_lighttileShader = nullptr;
GLShader_depthtile1* gl_depthtile1Shader = nullptr;
GLShader_depthtile2* gl_depthtile2Shader = nullptr;
GLShader_lighttile* gl_lighttileShader = nullptr;
GLShader_luminanceReduction* gl_luminanceReductionShader = nullptr;
GLShader_clearFrameData* gl_clearFrameDataShader = nullptr;

GLShader_generic *gl_genericShader = nullptr;
GLShader_genericMaterial *gl_genericShaderMaterial = nullptr;
Expand All @@ -83,6 +85,7 @@ GLShader_skybox *gl_skyboxShader = nullptr;
GLShader_skyboxMaterial *gl_skyboxShaderMaterial = nullptr;
GLShader_debugShadowMap *gl_debugShadowMapShader = nullptr;
GLShaderManager gl_shaderManager;
GLBuffer luminanceBuffer( "luminance", Util::ordinal( BufferBind::LUMINANCE ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );

namespace // Implementation details
{
Expand Down Expand Up @@ -432,6 +435,9 @@ static const std::vector<addedExtension_t> fragmentVertexAddedExtensions = {
where the core variables have different names. */
{ glConfig2.shaderDrawParametersAvailable, -1, "ARB_shader_draw_parameters" },
{ glConfig2.SSBOAvailable, 430, "ARB_shader_storage_buffer_object" },
{ glConfig2.shadingLanguage420PackAvailable, 420, "ARB_shading_language_420pack" },
{ glConfig2.explicitUniformLocationAvailable, 430, "ARB_explicit_uniform_location" },
{ glConfig2.shaderAtomicCountersAvailable, 420, "ARB_shader_atomic_counters" },
/* Even though these are part of the GL_KHR_shader_subgroup extension, we need to enable
the individual extensions for each feature.
GL_KHR_shader_subgroup itself can't be used in the shader. */
Expand Down Expand Up @@ -590,6 +596,10 @@ static std::string GenVertexHeader() {
AddDefine( str, "BIND_LIGHTMAP_DATA", Util::ordinal( BufferBind::LIGHTMAP_DATA ) );
}

if ( glConfig2.adaptiveExposureAvailable ) {
AddDefine( str, "BIND_LUMINANCE", Util::ordinal( BufferBind::LUMINANCE ) );
}

return str;
}

Expand Down Expand Up @@ -630,6 +640,11 @@ static std::string GenFragmentHeader() {
AddDefine( str, "BIND_LIGHTMAP_DATA", Util::ordinal( BufferBind::LIGHTMAP_DATA ) );
}

if ( glConfig2.adaptiveExposureAvailable ) {
AddDefine( str, "BIND_LUMINANCE", Util::ordinal( BufferBind::LUMINANCE ) );
AddDefine( str, "ADAPTIVE_EXPOSURE_AVAILABLE" );
}

return str;
}

Expand All @@ -655,6 +670,11 @@ static std::string GenComputeHeader() {
AddDefine( str, "BIND_DEBUG", Util::ordinal( BufferBind::DEBUG ) );
}

if ( glConfig2.adaptiveExposureAvailable ) {
AddDefine( str, "BIND_LUMINANCE", Util::ordinal( BufferBind::LUMINANCE ) );
AddDefine( str, "BIND_LUMINANCE_STORAGE", Util::ordinal( BufferBind::LUMINANCE_STORAGE ) );
}

if ( glConfig2.usingBindlessTextures ) {
str += "layout(bindless_image) uniform;\n";
}
Expand Down Expand Up @@ -2828,6 +2848,23 @@ void GLShader_shadowFill::SetShaderProgramUniforms( ShaderProgramDescriptor *sha
glUniform1i( glGetUniformLocation( shaderProgram->id, "u_ColorMap" ), 0 );
}

GLShader_luminanceReduction::GLShader_luminanceReduction() :
GLShader( "luminanceReduction",
false, "luminanceReduction" ),
u_ViewWidth( this ),
u_ViewHeight( this ),
u_TonemapParms2( this ) {
}

void GLShader_luminanceReduction::SetShaderProgramUniforms( ShaderProgramDescriptor* shaderProgram ) {
glUniform1i( glGetUniformLocation( shaderProgram->id, "initialRenderImage" ), 0 );
}

GLShader_clearFrameData::GLShader_clearFrameData() :
GLShader( "clearFrameData",
false, "clearFrameData" ) {
}

GLShader_reflection::GLShader_reflection():
GLShader( "reflection", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT,
false, "reflection_CB", "reflection_CB" ),
Expand Down Expand Up @@ -3086,11 +3123,14 @@ GLShader_cameraEffects::GLShader_cameraEffects() :
u_CurrentMap( this ),
u_GlobalLightFactor( this ),
u_ColorModulate( this ),
u_ViewWidth( this ),
u_ViewHeight( this ),
u_Tonemap( this ),
u_TonemapAdaptiveExposure( this ),
u_TonemapParms( this ),
u_TonemapParms2( this ),
u_TonemapExposure( this ),
u_InverseGamma( this )
{
u_InverseGamma( this ) {
}

void GLShader_cameraEffects::SetShaderProgramUniforms( ShaderProgramDescriptor *shaderProgram )
Expand Down
53 changes: 50 additions & 3 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3933,6 +3933,18 @@ class u_Tonemap :
}
};

class u_TonemapAdaptiveExposure :
GLUniform1Bool {
public:
u_TonemapAdaptiveExposure( GLShader* shader ) :
GLUniform1Bool( shader, "u_TonemapAdaptiveExposure", true ) {
}

void SetUniform_TonemapAdaptiveExposure( bool tonemapAdaptiveExposure ) {
this->SetValue( tonemapAdaptiveExposure );
}
};

class u_TonemapParms :
GLUniform4f {
public:
Expand All @@ -3945,6 +3957,18 @@ class u_TonemapParms :
}
};

class u_TonemapParms2 :
GLUniform4f {
public:
u_TonemapParms2( GLShader* shader ) :
GLUniform4f( shader, "u_TonemapParms2", true ) {
}

void SetUniform_TonemapParms2( vec4_t tonemapParms2 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use a more readable interface. Arguments with named float(s) instead of vec4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps so. I'd just made it as an "extension" to u_TonemapParms that was already there.

this->SetValue( tonemapParms2 );
}
};

class u_TonemapExposure :
GLUniform1f {
public:
Expand Down Expand Up @@ -4386,6 +4410,22 @@ class GLShader_shadowFill :
void SetShaderProgramUniforms( ShaderProgramDescriptor *shaderProgram ) override;
};

class GLShader_luminanceReduction :
public GLShader,
public u_ViewWidth,
public u_ViewHeight,
public u_TonemapParms2 {
public:
GLShader_luminanceReduction();
void SetShaderProgramUniforms( ShaderProgramDescriptor* shaderProgram ) override;
};

class GLShader_clearFrameData :
public GLShader {
public:
GLShader_clearFrameData();
};

class GLShader_reflection :
public GLShader,
public u_ColorMapCube,
Expand Down Expand Up @@ -4598,8 +4638,12 @@ class GLShader_cameraEffects :
public u_CurrentMap,
public u_GlobalLightFactor,
public u_ColorModulate,
public u_ViewWidth,
public u_ViewHeight,
public u_Tonemap,
public u_TonemapAdaptiveExposure,
public u_TonemapParms,
public u_TonemapParms2,
public u_TonemapExposure,
public u_InverseGamma
{
Expand Down Expand Up @@ -4830,9 +4874,11 @@ extern GLShader_fxaa *gl_fxaaShader;
extern GLShader_motionblur *gl_motionblurShader;
extern GLShader_ssao *gl_ssaoShader;

extern GLShader_depthtile1 *gl_depthtile1Shader;
extern GLShader_depthtile2 *gl_depthtile2Shader;
extern GLShader_lighttile *gl_lighttileShader;
extern GLShader_depthtile1* gl_depthtile1Shader;
extern GLShader_depthtile2* gl_depthtile2Shader;
extern GLShader_lighttile* gl_lighttileShader;
extern GLShader_luminanceReduction* gl_luminanceReductionShader;
extern GLShader_clearFrameData* gl_clearFrameDataShader;

extern GLShader_generic *gl_genericShader;
extern GLShader_genericMaterial *gl_genericShaderMaterial;
Expand All @@ -4857,5 +4903,6 @@ extern GLShader_skybox *gl_skyboxShader;
extern GLShader_skyboxMaterial *gl_skyboxShaderMaterial;
extern GLShader_debugShadowMap *gl_debugShadowMapShader;
extern GLShaderManager gl_shaderManager;
extern GLBuffer luminanceBuffer;

#endif // GL_SHADER_H
36 changes: 29 additions & 7 deletions src/engine/renderer/glsl_source/cameraEffects_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,49 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
uniform sampler2D u_CurrentMap;

#if defined(r_colorGrading)
uniform sampler3D u_ColorMap3D;
uniform sampler3D u_ColorMap3D;
#endif

uniform vec4 u_ColorModulate;
uniform float u_GlobalLightFactor; // 1 / tr.identityLight
uniform float u_InverseGamma;
uniform vec4 u_ColorModulate;
uniform float u_GlobalLightFactor; // 1 / tr.identityLight
uniform float u_InverseGamma;

// Tone mapping is not available when high-precision float framebuffer isn't enabled or supported.
#if defined(r_highPrecisionRendering) && defined(HAVE_ARB_texture_float)
uniform uint u_ViewWidth;
uniform uint u_ViewHeight;

uniform bool u_Tonemap;
uniform bool u_TonemapAdaptiveExposure;
/* x: contrast
y: highlightsCompressionSpeed
z: shoulderClip
w: highlightsCompression */
uniform bool u_Tonemap;
uniform vec4 u_TonemapParms;
uniform vec4 u_TonemapParms2;
uniform float u_TonemapExposure;

vec3 TonemapLottes( vec3 color ) {
// Lottes 2016, "Advanced Techniques and Optimization of HDR Color Pipelines"
return pow( color, vec3( u_TonemapParms[0] ) )
/ ( pow( color, vec3( u_TonemapParms[0] * u_TonemapParms[1] ) ) * u_TonemapParms[2] + u_TonemapParms[3] );
}

#if defined(ADAPTIVE_EXPOSURE_AVAILABLE)
layout(std140, binding = BIND_LUMINANCE) uniform ub_LuminanceUBO {
uint luminanceU;
};

float GetAverageLuminance( const in uint luminance ) {
return float( luminanceU ) / ( u_TonemapParms2[1] * u_ViewWidth * u_ViewHeight );
}
#endif

#endif

DECLARE_OUTPUT(vec4)

void main()
{
void main() {
// calculate the screen texcoord in the 0.0 to 1.0 range
vec2 st = gl_FragCoord.st / r_FBufSize;

Expand All @@ -61,6 +76,13 @@ void main()

#if defined(r_highPrecisionRendering) && defined(HAVE_ARB_texture_float)
if( u_Tonemap ) {
#if defined(ADAPTIVE_EXPOSURE_AVAILABLE)
if( u_TonemapAdaptiveExposure ) {
const float l = GetAverageLuminance( luminanceU ) - 8;
color.rgb *= clamp( 0.18f / exp2( l * 0.8f + 0.1f ), 0.0f, 2.0f );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the lower limit shouldn't be allowed to go 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't, the lowest it can go is ~0.0016.

}
#endif

color.rgb = TonemapLottes( color.rgb * u_TonemapExposure );
}
#endif
Expand Down
53 changes: 53 additions & 0 deletions src/engine/renderer/glsl_source/clearFrameData_cp.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
===========================================================================

Daemon BSD Source Code
Copyright (c) 2025 Daemon Developers
All rights reserved.

This file is part of the Daemon BSD Source Code (Daemon Source Code).

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

===========================================================================
*/

/* clearFrameData_cp.glsl */

#insert common_cp

layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

layout(std430, binding = BIND_LUMINANCE_STORAGE) writeonly buffer luminanceBuffer {
uint luminance;
};

uniform uint u_Frame;

void main() {
const uint globalInvocationID = GLOBAL_INVOCATION_ID;
if( globalInvocationID >= 1 ) {
return;
}
luminance = 0;
}
5 changes: 5 additions & 0 deletions src/engine/renderer/glsl_source/common_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ array must be in the form of uvec4 array[] */

/* Macro combinations for subgroup ops */

#if defined(HAVE_KHR_shader_subgroup_basic) && defined(HAVE_KHR_shader_subgroup_arithmetic)\
&& defined(HAVE_ARB_shader_atomic_counter_ops)
#define SUBGROUP_ATOMIC
#endif

#if defined(HAVE_KHR_shader_subgroup_basic) && defined(HAVE_KHR_shader_subgroup_arithmetic)\
&& defined(HAVE_KHR_shader_subgroup_ballot) && defined(HAVE_ARB_shader_atomic_counter_ops)
#define SUBGROUP_STREAM_COMPACTION
Expand Down
Loading