Skip to content

Commit

Permalink
Change bloom shader variant to a uniform conditional.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcy committed Aug 28, 2017
1 parent e8ef538 commit 189f2da
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 77 deletions.
21 changes: 5 additions & 16 deletions code/renderer_bgfx/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
};
};

Expand All @@ -114,7 +103,7 @@ struct ShaderProgramId
Color,
Depth,
Fog = Depth + DepthShaderProgramVariant::Num,
GaussianBlur = Fog + FogShaderProgramVariant::Num,
GaussianBlur,
Generic,
HemicubeDownsample = Generic + GenericShaderProgramVariant::Num,
HemicubeWeightedDownsample,
Expand Down
54 changes: 30 additions & 24 deletions code/renderer_bgfx/Main_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions code/renderer_bgfx/Main_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions code/renderer_bgfx/Precompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -182,7 +176,7 @@ newaction
{ "Bloom" },
{ "Color" },
{ "Depth", depthFragmentVariants },
{ "Fog", fogFragmentVariants },
{ "Fog" },
{ "GaussianBlur" },
{ "Generic", genericFragmentVariants },
{ "HemicubeDownsample" },
Expand Down
5 changes: 3 additions & 2 deletions shaders/Bloom_fragment.sc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 4 additions & 6 deletions shaders/Fog_fragment.sc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ $input v_position, v_texcoord0

#include <bgfx_shader.sh>

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
Expand All @@ -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);
}
25 changes: 12 additions & 13 deletions shaders/Generic_fragment.sc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 189f2da

Please sign in to comment.