Skip to content

Commit

Permalink
Remove HDR, keep bloom.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcy committed Sep 21, 2016
1 parent df50fdf commit f915b9e
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 193 deletions.
4 changes: 2 additions & 2 deletions code/renderer_bgfx/LightBaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ static void EncodeLightmaps()
for (int i = 0; i < lightmapSize.x * lightmapSize.y; i++)
{
// Colors are floats, but in 0-255+ range.
lightmap.encodedPassColor[i] = util::EncodeRGBM(util::OverbrightenColor(lightmap.passColor[i] / 255.0f));
lightmap.encodedAccumulatedColor[i] = util::EncodeRGBM(util::OverbrightenColor(lightmap.accumulatedColor[i] / 255.0f));
lightmap.encodedPassColor[i] = vec4b(vec4(util::OverbrightenColor(lightmap.passColor[i] / 255.0f), 1));
lightmap.encodedAccumulatedColor[i] = vec4b(vec4(util::OverbrightenColor(lightmap.accumulatedColor[i] / 255.0f), 1));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions code/renderer_bgfx/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct FogShaderProgramVariant
enum
{
None = 0,
HDR = 1 << 0, // Fragment
Bloom = 1 << 0, // Fragment
DepthRange = 1 << 1, // Vertex
Num = 1 << 2
};
Expand All @@ -99,8 +99,8 @@ struct GenericShaderProgramVariant

// Fragment
AlphaTest = 1 << 0,
DynamicLights = 1 << 1,
HDR = 1 << 2,
Bloom = 1 << 1,
DynamicLights = 1 << 2,
SoftSprite = 1 << 3,

// Vertex
Expand All @@ -114,6 +114,7 @@ struct ShaderProgramId
{
enum Enum
{
Bloom,
Color,
Depth,
Fog = Depth + DepthShaderProgramVariant::Num,
Expand All @@ -128,7 +129,6 @@ struct ShaderProgramId
Texture,
TextureColor,
TextureDebug,
ToneMap,
Num
};
};
Expand Down
46 changes: 18 additions & 28 deletions code/renderer_bgfx/Main_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,10 +1100,10 @@ static void RenderCamera(const RenderCameraArgs &args)

int shaderVariant = GenericShaderProgramVariant::DepthRange;

if (g_cvars.hdr.getBool())
if (g_cvars.bloom.getBool())
{
shaderVariant |= GenericShaderProgramVariant::HDR;
s_main->uniforms->bloomEnabled.set(vec4::empty);
shaderVariant |= GenericShaderProgramVariant::Bloom;
s_main->uniforms->bloom_Write_Scale.set(vec4::empty);
}

bgfx::submit(mainViewId, s_main->shaderPrograms[ShaderProgramId::Generic + shaderVariant].handle);
Expand Down Expand Up @@ -1219,10 +1219,10 @@ static void RenderCamera(const RenderCameraArgs &args)
shaderVariant |= GenericShaderProgramVariant::DepthRange;
}

if (g_cvars.hdr.getBool())
if (g_cvars.bloom.getBool())
{
shaderVariant |= GenericShaderProgramVariant::HDR;
s_main->uniforms->bloomEnabled.set(vec4(stage.bloom ? 1.0f : 0.0f, 0.0f, 0.0f, 0.0f));
shaderVariant |= GenericShaderProgramVariant::Bloom;
s_main->uniforms->bloom_Write_Scale.set(vec4(stage.bloom ? 1.0f : 0.0f, 0, 0, 0));
}

bgfx::setState(state);
Expand Down Expand Up @@ -1277,9 +1277,9 @@ static void RenderCamera(const RenderCameraArgs &args)
shaderVariant |= FogShaderProgramVariant::DepthRange;
}

if (g_cvars.hdr.getBool())
if (g_cvars.bloom.getBool())
{
shaderVariant |= FogShaderProgramVariant::HDR;
shaderVariant |= FogShaderProgramVariant::Bloom;
}

bgfx::submit(mainViewId, s_main->shaderPrograms[ShaderProgramId::Fog + shaderVariant].handle);
Expand Down Expand Up @@ -1447,43 +1447,34 @@ void RenderScene(const SceneDefinition &scene)

if (isWorldScene)
{
// HDR.
if (g_cvars.hdr.getBool())
if (g_cvars.bloom.getBool())
{
// Bloom.
// Blit to quarter size framebuffer.
const Rect bloomRect(0, 0, window::GetWidth() / 4, window::GetHeight() / 4);
bgfx::setTexture(0, s_main->uniforms->textureSampler.handle, s_main->sceneFb.handle, s_main->sceneBloomAttachment);
RenderScreenSpaceQuad(s_main->bloomFb[0], ShaderProgramId::Texture, BGFX_STATE_RGB_WRITE, BGFX_CLEAR_NONE, s_main->isTextureOriginBottomLeft, bloomRect);

// Ping-pong guassian blur in quarter size framebuffers
for (int i = 0; i < 2; i++)
{
s_main->uniforms->guassianBlurDirection.set(i == 0 ? vec4(1, 0, 0, 0) : vec4(0, 1, 0, 0));
bgfx::setTexture(0, s_main->uniforms->textureSampler.handle, s_main->bloomFb[i].handle);
RenderScreenSpaceQuad(s_main->bloomFb[!i], ShaderProgramId::GaussianBlur, BGFX_STATE_RGB_WRITE, BGFX_CLEAR_NONE, s_main->isTextureOriginBottomLeft, bloomRect);
}

// Tonemap.
// Clamp to sane values.
s_main->uniforms->brightnessContrastGammaSaturation.set(vec4
(
Clamped(g_cvars.brightness.getFloat() - 1.0f, -0.8f, 0.8f),
Clamped(g_cvars.contrast.getFloat(), 0.5f, 3.0f),
Clamped(g_cvars.hdrGamma.getFloat(), 0.5f, 3.0f),
Clamped(g_cvars.saturation.getFloat(), 0.0f, 3.0f)
));

s_main->uniforms->hdr_BloomScale_Exposure.set(vec4(g_cvars.hdrBloomScale.getFloat(), g_cvars.hdrExposure.getFloat(), 0, 0));
// 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));
bgfx::setTexture(0, s_main->uniforms->textureSampler.handle, s_main->sceneFb.handle);
bgfx::setTexture(1, s_main->uniforms->bloomSampler.handle, s_main->bloomFb[0].handle);
RenderScreenSpaceQuad(s_main->aa == AntiAliasing::None ? s_main->defaultFb : s_main->sceneTempFb, ShaderProgramId::ToneMap, BGFX_STATE_RGB_WRITE, BGFX_CLEAR_NONE, s_main->isTextureOriginBottomLeft);
RenderScreenSpaceQuad(s_main->aa == AntiAliasing::SMAA ? s_main->sceneTempFb : s_main->defaultFb, ShaderProgramId::Bloom, BGFX_STATE_RGB_WRITE, BGFX_CLEAR_NONE, s_main->isTextureOriginBottomLeft);
}

if (s_main->aa == AntiAliasing::SMAA)
{
s_main->uniforms->smaaMetrics.set(vec4(1.0f / rect.w, 1.0f / rect.h, (float)rect.w, (float)rect.h));

// Edge detection.
if (g_cvars.hdr.getBool())
if (g_cvars.bloom.getBool())
{
bgfx::setTexture(0, s_main->uniforms->smaaColorSampler.handle, s_main->sceneTempFb.handle);
}
Expand All @@ -1501,7 +1492,7 @@ void RenderScene(const SceneDefinition &scene)
RenderScreenSpaceQuad(s_main->smaaBlendFb, ShaderProgramId::SMAABlendingWeightCalculation, BGFX_STATE_RGB_WRITE | BGFX_STATE_ALPHA_WRITE, BGFX_CLEAR_COLOR, s_main->isTextureOriginBottomLeft);

// Neighborhood blending.
if (g_cvars.hdr.getBool())
if (g_cvars.bloom.getBool())
{
bgfx::setTexture(0, s_main->uniforms->smaaColorSampler.handle, s_main->sceneTempFb.handle);
}
Expand All @@ -1513,7 +1504,7 @@ void RenderScene(const SceneDefinition &scene)
bgfx::setTexture(1, s_main->uniforms->smaaBlendSampler.handle, s_main->smaaBlendFb.handle);
RenderScreenSpaceQuad(s_main->defaultFb, ShaderProgramId::SMAANeighborhoodBlending, BGFX_STATE_RGB_WRITE, BGFX_CLEAR_NONE, s_main->isTextureOriginBottomLeft);
}
else
else if (!g_cvars.bloom.getBool())
{
// Blit scene.
bgfx::setTexture(0, s_main->uniforms->textureSampler.handle, s_main->sceneFb.handle);
Expand Down Expand Up @@ -1634,8 +1625,7 @@ void EndFrame()
{
for (int i = 0; i < world::GetNumLightmaps(); i++)
{
s_main->uniforms->textureDebug.set(vec4(TEXTURE_DEBUG_RGBM, 0, 0, 0));
RenderDebugDraw(world::GetLightmap(i)->getHandle(), i, 0, ShaderProgramId::TextureDebug);
RenderDebugDraw(world::GetLightmap(i)->getHandle(), i, 0, ShaderProgramId::Texture);
}
}
else if (s_main->debugDraw == DebugDraw::Reflection)
Expand Down
85 changes: 34 additions & 51 deletions code/renderer_bgfx/Main_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ void ConsoleVariables::initialize()
}

bgfx_stats = interface::Cvar_Get("r_bgfx_stats", "0", ConsoleVariableFlags::Cheat);
bloom = interface::Cvar_Get("r_bloom", "1", ConsoleVariableFlags::Archive | ConsoleVariableFlags::Latch);
bloomScale = interface::Cvar_Get("r_bloomScale", "1.0", ConsoleVariableFlags::Archive);
debug = interface::Cvar_Get("r_debug", "", 0);
debugDraw = interface::Cvar_Get("r_debugDraw", "", 0);
debugDraw.setDescription(
"<empty> None\n"
"bloom HDR bloom\n"
"bloom Bloom\n"
"depth Linear depth\n"
"dlight Dynamic light data\n"
"lightmap Lightmaps\n"
Expand All @@ -106,10 +108,6 @@ void ConsoleVariables::initialize()
debugDrawSize = interface::Cvar_Get("r_debugDrawSize", "256", ConsoleVariableFlags::Archive);
dynamicLightIntensity = interface::Cvar_Get("r_dynamicLightIntensity", "1", ConsoleVariableFlags::Archive);
dynamicLightScale = interface::Cvar_Get("r_dynamicLightScale", "0.7", ConsoleVariableFlags::Archive);
hdr = interface::Cvar_Get("r_hdr", "0", ConsoleVariableFlags::Archive | ConsoleVariableFlags::Latch);
hdrBloomScale = interface::Cvar_Get("r_hdrBloomScale", "1.0", ConsoleVariableFlags::Archive);
hdrExposure = interface::Cvar_Get("r_hdrExposure", "0.5", ConsoleVariableFlags::Archive);
hdrGamma = interface::Cvar_Get("r_hdrGamma", "1.3", ConsoleVariableFlags::Archive);
lerpTextureAnimation = interface::Cvar_Get("r_lerpTextureAnimation", "1", ConsoleVariableFlags::Archive | ConsoleVariableFlags::Latch);
maxAnisotropy = interface::Cvar_Get("r_maxAnisotropy", "0", ConsoleVariableFlags::Archive | ConsoleVariableFlags::Latch);
picmip = interface::Cvar_Get("r_picmip", "0", ConsoleVariableFlags::Archive | ConsoleVariableFlags::Latch);
Expand All @@ -122,12 +120,9 @@ void ConsoleVariables::initialize()
waterReflections = interface::Cvar_Get("r_waterReflections", "0", ConsoleVariableFlags::Archive | ConsoleVariableFlags::Latch);
wireframe = interface::Cvar_Get("r_wireframe", "0", ConsoleVariableFlags::Cheat);

// Screen
brightness = interface::Cvar_Get("r_brightness", "1", ConsoleVariableFlags::Archive);
contrast = interface::Cvar_Get("r_contrast", "1", ConsoleVariableFlags::Archive);
// Gamma
gamma = interface::Cvar_Get("r_gamma", "1", ConsoleVariableFlags::Archive);
ignoreHardwareGamma = interface::Cvar_Get("r_ignorehwgamma", "0", ConsoleVariableFlags::Archive | ConsoleVariableFlags::Latch);
saturation = interface::Cvar_Get("r_saturation", "1", ConsoleVariableFlags::Archive);

// Window
allowResize = interface::Cvar_Get("r_allowResize", "0", ConsoleVariableFlags::Archive | ConsoleVariableFlags::Latch);
Expand Down Expand Up @@ -268,11 +263,6 @@ void Initialize()
s_main = std::make_unique<Main>();
g_cvars.initialize();
s_main->aa = AntiAliasingFromString(g_cvars.aa.getString());

// Don't allow MSAA if HDR is enabled.
if (g_cvars.hdr.getBool() && s_main->aa >= AntiAliasing::MSAA2x && s_main->aa <= AntiAliasing::MSAA16x)
s_main->aa = AntiAliasing::None;

s_main->aaHud = AntiAliasingFromString(g_cvars.aa_hud.getString());

// Non-world/HUD scenes can only use MSAA.
Expand Down Expand Up @@ -429,8 +419,9 @@ void Initialize()

// Map shader programs to their vertex and fragment shaders.
std::array<ShaderProgramIdMap, ShaderProgramId::Num> programMap;
programMap[ShaderProgramId::Color] = { FragmentShaderId::Color, VertexShaderId::Color };
programMap[ShaderProgramId::Depth] = { FragmentShaderId::Depth, VertexShaderId::Depth };
programMap[ShaderProgramId::Bloom] = { FragmentShaderId::Bloom, VertexShaderId::Texture };
programMap[ShaderProgramId::Color] = { FragmentShaderId::Color, VertexShaderId::Color };
programMap[ShaderProgramId::Depth] = { FragmentShaderId::Depth, VertexShaderId::Depth };

programMap[ShaderProgramId::Depth + DepthShaderProgramVariant::AlphaTest] =
{
Expand All @@ -452,9 +443,9 @@ void Initialize()

programMap[ShaderProgramId::Fog] = { FragmentShaderId::Fog, VertexShaderId::Fog };

programMap[ShaderProgramId::Fog + FogShaderProgramVariant::HDR] =
programMap[ShaderProgramId::Fog + FogShaderProgramVariant::Bloom] =
{
FragmentShaderId::Fog_HDR,
FragmentShaderId::Fog_Bloom,
VertexShaderId::Fog
};

Expand All @@ -464,9 +455,9 @@ void Initialize()
VertexShaderId::Fog_DepthRange
};

programMap[ShaderProgramId::Fog + (FogShaderProgramVariant::HDR | FogShaderProgramVariant::DepthRange)] =
programMap[ShaderProgramId::Fog + (FogShaderProgramVariant::Bloom | FogShaderProgramVariant::DepthRange)] =
{
FragmentShaderId::Fog_HDR,
FragmentShaderId::Fog_Bloom,
VertexShaderId::Fog_DepthRange
};

Expand All @@ -492,7 +483,6 @@ void Initialize()
programMap[ShaderProgramId::Texture] = { FragmentShaderId::Texture, VertexShaderId::Texture };
programMap[ShaderProgramId::TextureColor] = { FragmentShaderId::TextureColor, VertexShaderId::Texture };
programMap[ShaderProgramId::TextureDebug] = { FragmentShaderId::TextureDebug, VertexShaderId::Texture };
programMap[ShaderProgramId::ToneMap] = { FragmentShaderId::ToneMap, VertexShaderId::Texture };

// Create shader programs.
for (size_t i = 0; i < ShaderProgramId::Num; i++)
Expand All @@ -501,7 +491,7 @@ void Initialize()
if (s_main->aa != AntiAliasing::SMAA && (i == ShaderProgramId::SMAABlendingWeightCalculation || i == ShaderProgramId::SMAAEdgeDetection || i == ShaderProgramId::SMAANeighborhoodBlending))
continue;

if (g_cvars.hdr.getBool() == 0 && (i == ShaderProgramId::GaussianBlur || i == ShaderProgramId::ToneMap))
if (g_cvars.bloom.getBool() == 0 && (i == ShaderProgramId::Bloom || i == ShaderProgramId::GaussianBlur))
continue;

Shader &fragment = s_main->fragmentShaders[programMap[i].frag];
Expand Down Expand Up @@ -539,46 +529,39 @@ void LoadWorld(const char *name)
}

// Create frame buffers first.
uint32_t aaFlags = 0;

if (s_main->aa >= AntiAliasing::MSAA2x && s_main->aa <= AntiAliasing::MSAA16x)
{
aaFlags |= (1 + (int)s_main->aa - (int)AntiAliasing::MSAA2x) << BGFX_TEXTURE_RT_MSAA_SHIFT;
}

const uint32_t rtClampFlags = BGFX_TEXTURE_RT | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP;
s_main->linearDepthFb.handle = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Equal, bgfx::TextureFormat::R16F);
bgfx::TextureHandle reflectionTexture;

if (g_cvars.hdr.getBool())
if (g_cvars.bloom.getBool())
{
if (g_cvars.waterReflections.getBool())
reflectionTexture = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::RGBA16F, rtClampFlags);

if (s_main->aa != AntiAliasing::None)
if (s_main->aa == AntiAliasing::SMAA)
{
// HDR needs a temp BGRA8 destination for AA.
// Bloom needs a temp BGRA8 destination for SMAA.
s_main->sceneTempFb.handle = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Equal, bgfx::TextureFormat::BGRA8, rtClampFlags);
}

}
bgfx::TextureHandle sceneTextures[3];
sceneTextures[0] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::RGBA16F, rtClampFlags);
sceneTextures[1] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, rtClampFlags);
sceneTextures[2] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT);
sceneTextures[0] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, rtClampFlags | aaFlags);
sceneTextures[1] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, rtClampFlags | aaFlags);
sceneTextures[2] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | aaFlags);
s_main->sceneFb.handle = bgfx::createFrameBuffer(3, sceneTextures, true);
s_main->sceneBloomAttachment = 1;
s_main->sceneDepthAttachment = 2;

for (size_t i = 0; i < s_main->nBloomFrameBuffers; i++)
{
s_main->bloomFb[i].handle = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Quarter, bgfx::TextureFormat::BGRA8, rtClampFlags);
s_main->bloomFb[i].handle = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Quarter, bgfx::TextureFormat::BGRA8, rtClampFlags | aaFlags);
}
}
else
{
uint32_t aaFlags = 0;

if (s_main->aa >= AntiAliasing::MSAA2x && s_main->aa <= AntiAliasing::MSAA16x)
{
aaFlags |= (1 + (int)s_main->aa - (int)AntiAliasing::MSAA2x) << BGFX_TEXTURE_RT_MSAA_SHIFT;
}

if (g_cvars.waterReflections.getBool())
reflectionTexture = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, rtClampFlags | aaFlags);

bgfx::TextureHandle sceneTextures[2];
sceneTextures[0] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, rtClampFlags | aaFlags);
sceneTextures[1] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | aaFlags);
Expand All @@ -587,8 +570,14 @@ void LoadWorld(const char *name)
}

if (g_cvars.waterReflections.getBool())
{
bgfx::TextureHandle reflectionTexture = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, rtClampFlags | aaFlags);
s_main->reflectionFb.handle = bgfx::createFrameBuffer(1, &reflectionTexture); // Don't destroy the texture, that will be done by the texture cache.

// Register the reflection texture so it can accessed by materials.
Texture::create("*reflection", reflectionTexture);
}

if (s_main->aa == AntiAliasing::SMAA)
{
s_main->smaaBlendFb.handle = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Equal, bgfx::TextureFormat::BGRA8, rtClampFlags);
Expand All @@ -597,12 +586,6 @@ void LoadWorld(const char *name)
s_main->smaaSearchTex = bgfx::createTexture2D(SEARCHTEX_WIDTH, SEARCHTEX_HEIGHT, false, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP, bgfx::makeRef(searchTexBytes, SEARCHTEX_SIZE));
}

if (g_cvars.waterReflections.getBool())
{
// Register the reflection texture so it can accessed by materials.
Texture::create("*reflection", reflectionTexture);
}

// Load the world.
world::Load(name);
s_main->dlightManager->initializeGrid();
Expand Down
12 changes: 0 additions & 12 deletions code/renderer_bgfx/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,18 +437,6 @@ int Material::collapseStagesToGLSL()
}
}

// Lightmap only stages: decode RGBM
for (i = 0; i < maxStages; i++)
{
MaterialStage *stage = &stages[i];

if (stage->active && stage->bundles[MaterialTextureBundleIndex::DiffuseMap].tcGen == MaterialTexCoordGen::Lightmap)
{
stage->decodeRGBM = true;
}
}


return numStages;
}

Expand Down
1 change: 0 additions & 1 deletion code/renderer_bgfx/Material_calculate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ void MaterialStage::setShaderUniforms(Uniforms_MaterialStage *uniforms, int flag
uniforms->animation_Enabled_Fraction.set(vec4::empty);
}

uniforms->diffuseRGBM.set(vec4(decodeRGBM ? 1.0f : 0.0f, 0, 0, 0));
uniforms->lightType.set(vec4((float)light, 0, 0, 0));
uniforms->normalScale.set(normalScale);
uniforms->specularScale.set(specularScale);
Expand Down
Loading

0 comments on commit f915b9e

Please sign in to comment.