Skip to content

Commit b8e1026

Browse files
committed
renderer: rework tone mapper and ssao enablement
1 parent 4ee12e1 commit b8e1026

File tree

8 files changed

+75
-46
lines changed

8 files changed

+75
-46
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,11 @@ static std::string GenEngineConstants() {
843843
AddDefine( str, "r_colorGrading", 1 );
844844
}
845845

846+
if ( glConfig2.toneMapping )
847+
{
848+
AddDefine( str, "r_toneMapping", 1 );
849+
}
850+
846851
return str;
847852
}
848853

@@ -2900,7 +2905,6 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
29002905
u_ColorModulate( this ),
29012906
u_TextureMatrix( this ),
29022907
u_ModelViewProjectionMatrix( this ),
2903-
u_Tonemap( this ),
29042908
u_TonemapParms( this ),
29052909
u_TonemapExposure( this ),
29062910
u_InverseGamma( this )

src/engine/renderer/gl_shader.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,18 +3794,6 @@ class u_InverseGamma :
37943794
}
37953795
};
37963796

3797-
class u_Tonemap :
3798-
GLUniform1Bool {
3799-
public:
3800-
u_Tonemap( GLShader* shader ) :
3801-
GLUniform1Bool( shader, "u_Tonemap", true ) {
3802-
}
3803-
3804-
void SetUniform_Tonemap( bool tonemap ) {
3805-
this->SetValue( tonemap );
3806-
}
3807-
};
3808-
38093797
class u_TonemapParms :
38103798
GLUniform4f {
38113799
public:
@@ -4462,7 +4450,6 @@ class GLShader_cameraEffects :
44624450
public u_ColorModulate,
44634451
public u_TextureMatrix,
44644452
public u_ModelViewProjectionMatrix,
4465-
public u_Tonemap,
44664453
public u_TonemapParms,
44674454
public u_TonemapExposure,
44684455
public u_InverseGamma

src/engine/renderer/glsl_source/cameraEffects_fp.glsl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ IN(smooth) vec2 var_TexCoords;
3535

3636
DECLARE_OUTPUT(vec4)
3737

38+
#if defined(r_toneMapping)
3839
/* x: contrast
3940
y: highlightsCompressionSpeed
4041
z: shoulderClip
@@ -48,6 +49,7 @@ vec3 TonemapLottes( vec3 color ) {
4849
return pow( color, vec3( u_TonemapParms[0] ) )
4950
/ ( pow( color, vec3( u_TonemapParms[0] * u_TonemapParms[1] ) ) * u_TonemapParms[2] + u_TonemapParms[3] );
5051
}
52+
#endif
5153

5254
void main()
5355
{
@@ -56,9 +58,10 @@ void main()
5658

5759
vec4 color = texture2D(u_CurrentMap, st);
5860

59-
if( u_Tonemap ) {
60-
color.rgb = TonemapLottes( color.rgb * u_TonemapExposure );
61-
}
61+
#if defined(r_toneMapping)
62+
color.rgb = TonemapLottes( color.rgb * u_TonemapExposure );
63+
#endif
64+
6265
color.rgb = clamp( color.rgb, vec3( 0.0f ), vec3( 1.0f ) );
6366

6467
#if defined(r_colorGrading)

src/engine/renderer/tr_backend.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ void GL_BindNullProgram()
151151

152152
void GL_SelectTexture( int unit )
153153
{
154-
155154
if ( glState.currenttmu == unit )
156155
{
157156
return;
@@ -3192,7 +3191,8 @@ void RB_RenderSSAO()
31923191
{
31933192
GLimp_LogComment( "--- RB_RenderSSAO ---\n" );
31943193

3195-
if ( !glConfig2.textureGatherAvailable ) {
3194+
if ( !glConfig2.ssao )
3195+
{
31963196
return;
31973197
}
31983198

@@ -3205,7 +3205,7 @@ void RB_RenderSSAO()
32053205
GL_State( GLS_DEPTHTEST_DISABLE | GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO );
32063206
GL_Cull( cullType_t::CT_TWO_SIDED );
32073207

3208-
if ( r_ssao->integer < 0 ) {
3208+
if ( glConfig2.ssao && r_ssao.Get() == Util::ordinal( ssaoMode::SHOW ) ) {
32093209
// clear the screen to show only SSAO
32103210
GL_ClearColor( 1.0, 1.0, 1.0, 1.0 );
32113211
glClear( GL_COLOR_BUFFER_BIT );
@@ -3354,15 +3354,13 @@ void RB_CameraPostFX()
33543354

33553355
gl_cameraEffectsShader->SetUniform_InverseGamma( 1.0 / r_gamma->value );
33563356

3357-
const bool tonemap = r_tonemap.Get() && r_highPrecisionRendering.Get() && glConfig2.textureFloatAvailable;
3358-
if ( tonemap ) {
3357+
if ( glConfig2.toneMapping ) {
33593358
vec4_t tonemapParms { r_tonemapContrast.Get(), r_tonemapHighlightsCompressionSpeed.Get() };
33603359
ComputeTonemapParams( tonemapParms[0], tonemapParms[1], r_tonemapHDRMax.Get(),
33613360
r_tonemapDarkAreaPointHDR.Get(), r_tonemapDarkAreaPointLDR.Get(), tonemapParms[2], tonemapParms[3] );
33623361
gl_cameraEffectsShader->SetUniform_TonemapParms( tonemapParms );
33633362
gl_cameraEffectsShader->SetUniform_TonemapExposure( r_tonemapExposure.Get() );
33643363
}
3365-
gl_cameraEffectsShader->SetUniform_Tonemap( tonemap );
33663364

33673365
// This shader is run last, so let it render to screen instead of
33683366
// tr.mainFBO
@@ -4725,9 +4723,7 @@ static void RB_RenderView( bool depthPass )
47254723
RB_RenderDrawSurfaces( shaderSort_t::SS_ENVIRONMENT_FOG, shaderSort_t::SS_OPAQUE, DRAWSURFACES_ALL );
47264724
}
47274725

4728-
if ( r_ssao->integer ) {
4729-
RB_RenderSSAO();
4730-
}
4726+
RB_RenderSSAO();
47314727

47324728
if ( r_speeds->integer == Util::ordinal(renderSpeeds_t::RSPEEDS_SHADING_TIMES) )
47334729
{

src/engine/renderer/tr_init.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
205205

206206
cvar_t *r_gamma;
207207

208-
Cvar::Cvar<bool> r_tonemap( "r_tonemap", "Use HDR->LDR tonemapping", Cvar::NONE, true );
208+
Cvar::Cvar<bool> r_toneMapping( "r_toneMapping", "Use HDR->LDR tonemapping", Cvar::NONE, true );
209209
Cvar::Cvar<float> r_tonemapExposure( "r_tonemapExposure", "Tonemap exposure", Cvar::NONE, 1.0f );
210210
Cvar::Range<Cvar::Cvar<float>> r_tonemapContrast( "r_tonemapContrast", "Makes dark areas light up faster",
211211
Cvar::NONE, 1.6f, 1.0f, 10.0f );
@@ -312,7 +312,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
312312
Cvar::Cvar<float> r_bloomBlur( "r_bloomBlur", "Bloom strength", Cvar::NONE, 1.0 );
313313
Cvar::Cvar<int> r_bloomPasses( "r_bloomPasses", "Amount of bloom passes in each direction", Cvar::NONE, 2 );
314314
cvar_t *r_FXAA;
315-
cvar_t *r_ssao;
315+
Cvar::Range<Cvar::Cvar<int>> r_ssao( "r_ssao",
316+
"Screen space ambient occlusion: "
317+
"-1: show, 0: disabled, 1: enabled",
318+
Cvar::NONE,
319+
Util::ordinal( ssaoMode::DISABLED ),
320+
Util::ordinal( ssaoMode::SHOW ),
321+
Util::ordinal( ssaoMode::ENABLED ) );
316322

317323
cvar_t *r_evsmPostProcess;
318324

@@ -1190,6 +1196,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
11901196
Cvar::Latch( r_lightMode );
11911197
Cvar::Latch( r_colorGrading );
11921198
Cvar::Latch( r_drawSky );
1199+
Cvar::Latch( r_toneMapping );
11931200
r_lightStyles = Cvar_Get( "r_lightStyles", "1", CVAR_LATCH | CVAR_ARCHIVE );
11941201
r_exportTextures = Cvar_Get( "r_exportTextures", "0", 0 );
11951202
r_heatHaze = Cvar_Get( "r_heatHaze", "1", CVAR_LATCH | CVAR_ARCHIVE );
@@ -1241,7 +1248,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
12411248

12421249
Cvar::Latch( r_bloom );
12431250
r_FXAA = Cvar_Get( "r_FXAA", "0", CVAR_LATCH | CVAR_ARCHIVE );
1244-
r_ssao = Cvar_Get( "r_ssao", "0", CVAR_LATCH | CVAR_ARCHIVE );
1251+
Cvar::Latch( r_ssao );
12451252

12461253
// temporary variables that can change at any time
12471254
r_showImages = Cvar_Get( "r_showImages", "0", CVAR_TEMP );

src/engine/renderer/tr_local.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ enum class shaderProfilerRenderSubGroupsMode {
321321
FS_ALL
322322
};
323323

324+
enum class ssaoMode {
325+
SHOW = -1,
326+
DISABLED,
327+
ENABLED,
328+
};
329+
324330
enum class renderSpeeds_t
325331
{
326332
RSPEEDS_GENERAL = 1,
@@ -2969,7 +2975,7 @@ enum class shaderProfilerRenderSubGroupsMode {
29692975
extern cvar_t *r_mode; // video mode
29702976
extern cvar_t *r_gamma;
29712977

2972-
extern Cvar::Cvar<bool> r_tonemap;
2978+
extern Cvar::Cvar<bool> r_toneMapping;
29732979
extern Cvar::Cvar<float> r_tonemapExposure;
29742980
extern Cvar::Range<Cvar::Cvar<float>> r_tonemapContrast;
29752981
extern Cvar::Range<Cvar::Cvar<float>> r_tonemapHighlightsCompressionSpeed;
@@ -3114,7 +3120,7 @@ enum class shaderProfilerRenderSubGroupsMode {
31143120
extern Cvar::Cvar<float> r_bloomBlur;
31153121
extern Cvar::Cvar<int> r_bloomPasses;
31163122
extern cvar_t *r_FXAA;
3117-
extern cvar_t *r_ssao;
3123+
extern Cvar::Range<Cvar::Cvar<int>> r_ssao;
31183124

31193125
extern cvar_t *r_evsmPostProcess;
31203126

src/engine/renderer/tr_public.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ struct glconfig2_t
152152
bool reflectionMappingAvailable;
153153
bool reflectionMapping;
154154
bool bloom;
155+
bool ssao;
155156
bool motionBlur;
157+
bool toneMapping;
156158
};
157159

158160
//

src/engine/renderer/tr_shade.cpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void EnableAvailableFeatures()
7373

7474
if ( glConfig2.realtimeLightLayers > glConfig2.max3DTextureSize ) {
7575
glConfig2.realtimeLightLayers = glConfig2.max3DTextureSize;
76-
Log::Notice( "r_realtimeLightLayers exceeds maximum 3D texture size, using %i instead", glConfig2.max3DTextureSize );
76+
Log::Notice( "r_realtimeLightLayers exceeds maximum 3D texture size, using %i instead.", glConfig2.max3DTextureSize );
7777
}
7878

7979
Log::Notice( "Using %i dynamic light layers, %i dynamic lights available per tile", glConfig2.realtimeLightLayers,
@@ -94,12 +94,28 @@ static void EnableAvailableFeatures()
9494
glConfig2.shadowingMode = shadowingMode_t( r_shadows.Get() );
9595
glConfig2.shadowMapping = glConfig2.shadowingMode >= shadowingMode_t::SHADOWING_ESM16;
9696

97-
if ( glConfig2.shadowMapping )
97+
glConfig2.toneMapping = r_toneMapping.Get();
98+
99+
if ( !r_highPrecisionRendering.Get() )
98100
{
99-
if ( !glConfig2.textureFloatAvailable )
101+
Log::Warn( "Tone mapping disabled because high precision rendering is disabled." );
102+
glConfig2.toneMapping = false;
103+
}
104+
105+
if ( !glConfig2.textureFloatAvailable )
106+
{
107+
static const std::pair<bool*, std::string> textureFloatFeatures[] = {
108+
{ &glConfig2.shadowMapping, "Shadow mapping" },
109+
{ &glConfig2.toneMapping, "Tone mapping" },
110+
};
111+
112+
for ( auto& f : textureFloatFeatures )
100113
{
101-
Log::Warn( "Shadow mapping disabled because ARB_texture_float is not available" );
102-
glConfig2.shadowMapping = false;
114+
if ( *f.first )
115+
{
116+
Log::Warn( "%s disabled because ARB_texture_float is not available.", f.second, "ARB_texture_float" );
117+
*f.first = false;
118+
}
103119
}
104120
}
105121

@@ -136,14 +152,29 @@ static void EnableAvailableFeatures()
136152
}
137153
}
138154

139-
140155
// Disable features that require deluxe mapping to be enabled.
141156
glConfig2.normalMapping = glConfig2.deluxeMapping && glConfig2.normalMapping;
142157
glConfig2.specularMapping = glConfig2.deluxeMapping && glConfig2.specularMapping;
143158
glConfig2.physicalMapping = glConfig2.deluxeMapping && glConfig2.physicalMapping;
144159

145160
glConfig2.bloom = r_bloom.Get();
146161

162+
glConfig2.ssao = r_ssao.Get() != Util::ordinal( ssaoMode::DISABLED );
163+
164+
static const std::pair<bool*, std::string> ssaoRequiredExtensions[] = {
165+
{ &glConfig2.textureGatherAvailable, "ARB_texture_gather" },
166+
{ &glConfig2.gpuShader4Available, "EXT_gpu_shader4" },
167+
};
168+
169+
for ( auto& e: ssaoRequiredExtensions )
170+
{
171+
if ( *e.first )
172+
{
173+
Log::Warn( "SSAO disabled because %s is not available.", e.second );
174+
glConfig2.ssao = false;
175+
}
176+
}
177+
147178
/* Motion blur is enabled by cg_motionblur which is a client cvar so we have to build it in all cases,
148179
unless unsupported by the hardware which is the only condition when the engine knows it is not used. */
149180
glConfig2.motionBlur = true;
@@ -369,16 +400,9 @@ static void GLSL_InitGPUShadersOrError()
369400
gl_shaderManager.load( gl_motionblurShader );
370401
}
371402

372-
if ( r_ssao->integer )
403+
if ( glConfig2.ssao )
373404
{
374-
if ( glConfig2.textureGatherAvailable )
375-
{
376-
gl_shaderManager.load( gl_ssaoShader );
377-
}
378-
else
379-
{
380-
Log::Warn("SSAO not used because GL_ARB_texture_gather is not available.");
381-
}
405+
gl_shaderManager.load( gl_ssaoShader );
382406
}
383407

384408
if ( r_FXAA->integer != 0 )

0 commit comments

Comments
 (0)