diff --git a/code/renderer_bgfx/Main.h b/code/renderer_bgfx/Main.h index 961c92f..a2211ac 100644 --- a/code/renderer_bgfx/Main.h +++ b/code/renderer_bgfx/Main.h @@ -78,16 +78,6 @@ struct DepthShaderProgramVariant }; }; -struct FogShaderProgramVariant -{ - enum - { - None = 0, - Bloom = 1 << 0, // Fragment - Num = 1 << 1 - }; -}; - /// @remarks Sync with generated GenericFragmentShaderVariant and GenericVertexShaderVariant. Order matters - fragment first. struct GenericShaderProgramVariant { @@ -97,12 +87,11 @@ struct GenericShaderProgramVariant // Fragment AlphaTest = 1 << 0, - Bloom = 1 << 1, - DynamicLights = 1 << 2, - SoftSprite = 1 << 3, - TextureVariation = 1 << 4, + DynamicLights = 1 << 1, + SoftSprite = 1 << 2, + TextureVariation = 1 << 3, - Num = 1 << 5 + Num = 1 << 4 }; }; @@ -114,7 +103,7 @@ struct ShaderProgramId Color, Depth, Fog = Depth + DepthShaderProgramVariant::Num, - GaussianBlur = Fog + FogShaderProgramVariant::Num, + GaussianBlur, Generic, HemicubeDownsample = Generic + GenericShaderProgramVariant::Num, HemicubeWeightedDownsample, diff --git a/code/renderer_bgfx/Main_frame.cpp b/code/renderer_bgfx/Main_frame.cpp index 87aa253..dc61d1d 100644 --- a/code/renderer_bgfx/Main_frame.cpp +++ b/code/renderer_bgfx/Main_frame.cpp @@ -1076,6 +1076,15 @@ static void RenderCamera(const RenderCameraArgs &args) // Special case for skybox. if (dc.flags & DrawCallFlags::Skybox) { + if (g_cvars.bloom.getBool()) + { + s_main->uniforms->bloom_Enabled_Write_Scale.set(vec4(1, 0, 0, 0)); + } + else + { + s_main->uniforms->bloom_Enabled_Write_Scale.set(vec4::empty); + } + s_main->uniforms->depthRangeEnabled.set(vec4(1, 0, 0, 0)); s_main->uniforms->depthRange.set(vec4(dc.zOffset, dc.zScale, depthRange.x, depthRange.y)); s_main->uniforms->dynamicLight_Num_Intensity.set(vec4::empty); @@ -1100,15 +1109,7 @@ static void RenderCamera(const RenderCameraArgs &args) bgfx::setStencil(stencilTest); } - int shaderVariant = GenericShaderProgramVariant::None; - - if (g_cvars.bloom.getBool()) - { - shaderVariant |= GenericShaderProgramVariant::Bloom; - s_main->uniforms->bloom_Write_Scale.set(vec4::empty); - } - - bgfx::submit(mainViewId, s_main->shaderPrograms[ShaderProgramId::Generic + shaderVariant].handle); + bgfx::submit(mainViewId, s_main->shaderPrograms[ShaderProgramId::Generic].handle); continue; } @@ -1169,6 +1170,15 @@ static void RenderCamera(const RenderCameraArgs &args) if (!stage.active) continue; + if (g_cvars.bloom.getBool()) + { + s_main->uniforms->bloom_Enabled_Write_Scale.set(vec4(1, stage.bloom ? 1.0f : 0.0f, 0, 0)); + } + else + { + s_main->uniforms->bloom_Enabled_Write_Scale.set(vec4::empty); + } + if (mat->polygonOffset || dc.zOffset > 0 || dc.zScale > 0) { s_main->uniforms->depthRangeEnabled.set(vec4(1, 0, 0, 0)); @@ -1225,12 +1235,6 @@ static void RenderCamera(const RenderCameraArgs &args) bgfx::setTexture(TextureUnit::DynamicLights, s_main->matStageUniforms->dynamicLightsSampler.handle, s_main->dlightManager->getLightsTexture()); } - if (g_cvars.bloom.getBool()) - { - shaderVariant |= GenericShaderProgramVariant::Bloom; - s_main->uniforms->bloom_Write_Scale.set(vec4(stage.bloom ? 1.0f : 0.0f, 0, 0, 0)); - } - if (g_cvars.textureVariation.getBool() && stage.textureVariation) { shaderVariant |= GenericShaderProgramVariant::TextureVariation; @@ -1261,6 +1265,15 @@ static void RenderCamera(const RenderCameraArgs &args) // Do fog pass. if (doFogPass) { + if (g_cvars.bloom.getBool()) + { + s_main->uniforms->bloom_Enabled_Write_Scale.set(vec4(1, 0, 0, 0)); + } + else + { + s_main->uniforms->bloom_Enabled_Write_Scale.set(vec4::empty); + } + if (dc.zOffset > 0 || dc.zScale > 0) { s_main->uniforms->depthRangeEnabled.set(vec4(1, 0, 0, 0)); @@ -1291,14 +1304,7 @@ static void RenderCamera(const RenderCameraArgs &args) bgfx::setStencil(stencilTest); } - int shaderVariant = FogShaderProgramVariant::None; - - if (g_cvars.bloom.getBool()) - { - shaderVariant |= FogShaderProgramVariant::Bloom; - } - - bgfx::submit(mainViewId, s_main->shaderPrograms[ShaderProgramId::Fog + shaderVariant].handle); + bgfx::submit(mainViewId, s_main->shaderPrograms[ShaderProgramId::Fog].handle); } s_main->currentEntity = nullptr; @@ -1479,7 +1485,7 @@ void RenderScene(const SceneDefinition &scene) } // Apply bloom. If using SMAA, we need to read color, so blit into the original bloom texture which is no longer used. - s_main->uniforms->bloom_Write_Scale.set(vec4(0, g_cvars.bloomScale.getFloat(), 0, 0)); + s_main->uniforms->bloom_Enabled_Write_Scale.set(vec4(1, 0, g_cvars.bloomScale.getFloat(), 0)); bgfx::setTexture(0, s_main->uniforms->textureSampler.handle, bgfx::getTexture(s_main->sceneFb.handle)); bgfx::setTexture(1, s_main->uniforms->bloomSampler.handle, bgfx::getTexture(s_main->bloomFb[0].handle)); RenderScreenSpaceQuad(s_main->aa == AntiAliasing::SMAA ? s_main->sceneTempFb : s_main->defaultFb, ShaderProgramId::Bloom, BGFX_STATE_RGB_WRITE, BGFX_CLEAR_NONE, s_main->isTextureOriginBottomLeft); diff --git a/code/renderer_bgfx/Main_init.cpp b/code/renderer_bgfx/Main_init.cpp index 808ba21..e1fdc67 100644 --- a/code/renderer_bgfx/Main_init.cpp +++ b/code/renderer_bgfx/Main_init.cpp @@ -432,13 +432,6 @@ void Initialize() }; programMap[ShaderProgramId::Fog] = { FragmentShaderId::Fog, VertexShaderId::Fog }; - - programMap[ShaderProgramId::Fog + FogShaderProgramVariant::Bloom] = - { - FragmentShaderId::Fog_Bloom, - VertexShaderId::Fog - }; - programMap[ShaderProgramId::GaussianBlur] = { FragmentShaderId::GaussianBlur, VertexShaderId::Texture }; // Sync with GenericShaderProgramVariant. diff --git a/code/renderer_bgfx/Precompiled.h b/code/renderer_bgfx/Precompiled.h index 13941a5..4a75dc6 100644 --- a/code/renderer_bgfx/Precompiled.h +++ b/code/renderer_bgfx/Precompiled.h @@ -1452,8 +1452,8 @@ struct Uniforms /// @{ Uniform_vec4 guassianBlurDirection = "u_GuassianBlurDirection"; - /// @remarks Only x and y used. - Uniform_vec4 bloom_Write_Scale = "u_Bloom_Write_Scale"; + /// @remarks w not used. + Uniform_vec4 bloom_Enabled_Write_Scale = "u_Bloom_Enabled_Write_Scale"; /// @} /// @name Texture samplers diff --git a/premake5.lua b/premake5.lua index 13a02b0..9a7e691 100644 --- a/premake5.lua +++ b/premake5.lua @@ -163,15 +163,9 @@ newaction { "AlphaTest", "USE_ALPHA_TEST" } } - local fogFragmentVariants = - { - { "Bloom", "USE_BLOOM" } - } - local genericFragmentVariants = { { "AlphaTest", "USE_ALPHA_TEST" }, - { "Bloom", "USE_BLOOM" }, { "DynamicLights", "USE_DYNAMIC_LIGHTS" }, { "SoftSprite", "USE_SOFT_SPRITE" }, { "TextureVariation", "USE_TEXTURE_VARIATION" } @@ -182,7 +176,7 @@ newaction { "Bloom" }, { "Color" }, { "Depth", depthFragmentVariants }, - { "Fog", fogFragmentVariants }, + { "Fog" }, { "GaussianBlur" }, { "Generic", genericFragmentVariants }, { "HemicubeDownsample" }, diff --git a/shaders/Bloom_fragment.sc b/shaders/Bloom_fragment.sc index ec67ee3..0a46970 100644 --- a/shaders/Bloom_fragment.sc +++ b/shaders/Bloom_fragment.sc @@ -6,10 +6,11 @@ $input v_texcoord0 SAMPLER2D(u_TextureSampler, 0); SAMPLER2D(u_BloomSampler, 1); -uniform vec4 u_Bloom_Write_Scale; +uniform vec4 u_Bloom_Enabled_Write_Scale; +#define u_BloomScale u_Bloom_Enabled_Write_Scale.z void main() { - vec3 color = texture2D(u_TextureSampler, v_texcoord0).rgb + texture2D(u_BloomSampler, v_texcoord0).rgb * u_Bloom_Write_Scale.y; + vec3 color = texture2D(u_TextureSampler, v_texcoord0).rgb + texture2D(u_BloomSampler, v_texcoord0).rgb * u_BloomScale; gl_FragColor = vec4(color, 1.0); } diff --git a/shaders/Fog_fragment.sc b/shaders/Fog_fragment.sc index 1a34e04..d3102f4 100644 --- a/shaders/Fog_fragment.sc +++ b/shaders/Fog_fragment.sc @@ -2,6 +2,8 @@ $input v_position, v_texcoord0 #include +uniform vec4 u_Bloom_Enabled_Write_Scale; +#define u_BloomEnabled int(u_Bloom_Enabled_Write_Scale.x) uniform vec4 u_PortalClip; uniform vec4 u_PortalPlane; #define v_scale v_texcoord0.x @@ -18,11 +20,7 @@ void main() } vec4 fragColor = vec4(u_Color.rgb, sqrt(saturate(v_scale))); - -#if defined(USE_BLOOM) gl_FragData[0] = fragColor; - gl_FragData[1] = vec4(0.0, 0.0, 0.0, fragColor.a); -#else - gl_FragColor = fragColor; -#endif // USE_BLOOM + if (u_BloomEnabled != 0) + gl_FragData[1] = vec4(0.0, 0.0, 0.0, fragColor.a); } diff --git a/shaders/Generic_fragment.sc b/shaders/Generic_fragment.sc index 00b85c6..dedd002 100644 --- a/shaders/Generic_fragment.sc +++ b/shaders/Generic_fragment.sc @@ -31,9 +31,9 @@ uniform vec4 u_DynamicLight_Num_Intensity; // x is the number of dynamic lights, uniform vec4 u_DynamicLightTextureSizes_Cells_Indices_Lights; // w not used #endif -#if defined(USE_BLOOM) -uniform vec4 u_Bloom_Write_Scale; -#endif +uniform vec4 u_Bloom_Enabled_Write_Scale; +#define u_BloomEnabled int(u_Bloom_Enabled_Write_Scale.x) +#define u_BloomWrite int(u_Bloom_Enabled_Write_Scale.y) uniform vec4 u_Animation_Enabled_Fraction; // only x and y used uniform vec4 u_RenderMode; // only x used @@ -344,18 +344,17 @@ void main() fragColor = vec4(texture2D(u_LightSampler, v_texcoord1).rgb, alpha); } -#if defined(USE_BLOOM) gl_FragData[0] = fragColor; - if (int(u_Bloom_Write_Scale.x) != 0) + if (u_BloomEnabled != 0) { - gl_FragData[1] = fragColor; - } - else - { - gl_FragData[1] = vec4(0.0, 0.0, 0.0, fragColor.a); + if (u_BloomWrite != 0) + { + gl_FragData[1] = fragColor; + } + else + { + gl_FragData[1] = vec4(0.0, 0.0, 0.0, fragColor.a); + } } -#else - gl_FragColor = fragColor; -#endif // USE_BLOOM }