Skip to content

Commit

Permalink
Fix clamping artifacts when hardware gamma is disabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcy committed Sep 21, 2016
1 parent 46bfad7 commit a65c7ea
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion code/renderer_bgfx/Interface_ioq3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ static void RE_BeginRegistration(glconfig_t *config)
main::Initialize();
const bgfx::Caps *caps = bgfx::getCaps();
config->maxTextureSize = caps->maxTextureSize;
config->deviceSupportsGamma = qtrue;
config->deviceSupportsGamma = window::IsFullscreen() ? qtrue : qfalse;
config->vidWidth = window::GetWidth();
config->vidHeight = window::GetHeight();
config->windowAspect = window::GetAspectRatio();
Expand Down
2 changes: 1 addition & 1 deletion code/renderer_bgfx/Interface_iortcw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ static void RE_BeginRegistration(glconfig_t *config)
main::Initialize();
const bgfx::Caps *caps = bgfx::getCaps();
config->maxTextureSize = caps->maxTextureSize;
config->deviceSupportsGamma = qtrue;
config->deviceSupportsGamma = window::IsFullscreen() ? qtrue : qfalse;
config->vidWidth = window::GetWidth();
config->vidHeight = window::GetHeight();
config->windowAspect = window::GetAspectRatio();
Expand Down
2 changes: 1 addition & 1 deletion code/renderer_bgfx/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ void SetWindowGamma()
value = int(255 * pow(i / 255.0f, 1.0f / gamma) + 0.5f);
}

g_gammaTable[i] = math::Clamped(value, 0, 255);
g_gammaTable[i] = math::Clamped(value * (int)g_overbrightFactor, 0, 255);
}

window::SetGamma(g_gammaTable, g_gammaTable, g_gammaTable);
Expand Down
19 changes: 14 additions & 5 deletions code/renderer_bgfx/Main_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,16 @@ void DrawStretchRaw(int x, int y, int w, int h, int cols, int rows, const uint8_
bgfx::setVertexBuffer(&tvb);
bgfx::setIndexBuffer(&tib);
bgfx::setTexture(0, s_main->uniforms->textureSampler.handle, Texture::getScratch(size_t(client))->getHandle());
s_main->matStageUniforms->color.set(vec4::white);

if (g_hardwareGammaEnabled)
{
s_main->matStageUniforms->color.set(vec4(g_identityLight, g_identityLight, g_identityLight, 1));
}
else
{
s_main->matStageUniforms->color.set(vec4::white);
}

bgfx::setState(BGFX_STATE_RGB_WRITE);
const uint8_t viewId = PushView(s_main->defaultFb, BGFX_CLEAR_NONE, mat4::identity, mat4::orthographicProjection(0, 1, 0, 1, -1, 1), Rect(x, y, w, h), PushViewFlags::Sequential);
bgfx::submit(viewId, s_main->shaderPrograms[ShaderProgramId::TextureColor].handle);
Expand Down Expand Up @@ -320,16 +329,16 @@ static void SetupEntityLighting(Entity *entity)
entity->ambientLight += vec3(g_identityLight * 32);
}

// Clamp ambient.
for (int i = 0; i < 3; i++)
entity->ambientLight[i] = std::min(entity->ambientLight[i], g_identityLight * 255);

// Modify the light by dynamic lights.
if (!s_main->isWorldCamera)
{
s_main->dlightManager->contribute(s_main->frameNo, lightPosition, &entity->directedLight, &entity->lightDir);
}

// Clamp ambient.
for (int i = 0; i < 3; i++)
entity->ambientLight[i] = std::min(entity->ambientLight[i], g_identityLight * 255);

entity->lightDir.normalize();
}

Expand Down
27 changes: 21 additions & 6 deletions code/renderer_bgfx/Material_calculate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,28 @@ void MaterialStage::calculateColors(vec4 *baseColor, vec4 *vertColor) const
break;
}

// Multiply color by overbrightbits if this isn't a blend.
if (g_overbrightFactor > 1
&& blendSrc != BGFX_STATE_BLEND_DST_COLOR
&& blendSrc != BGFX_STATE_BLEND_INV_DST_COLOR
&& blendDst != BGFX_STATE_BLEND_SRC_COLOR
&& blendDst != BGFX_STATE_BLEND_INV_SRC_COLOR)
// Multiply color by overbright factor.
// The GL1 renderer does this to texture color at load time instead.
if (!g_hardwareGammaEnabled && g_overbrightFactor > 1)
{
const bool isBlend = (blendSrc == BGFX_STATE_BLEND_DST_COLOR || blendSrc == BGFX_STATE_BLEND_INV_DST_COLOR || blendDst == BGFX_STATE_BLEND_SRC_COLOR || blendDst == BGFX_STATE_BLEND_INV_SRC_COLOR);

// Hack around materials with lightmap only stages (white diffuse * lightmap) like textures/base_wall/bluemetal1b_shiny (q3dm12).
// These will have a lightmap only first stage with the second stage doing multiply blend.
// Normally, the first stage will be multiplied by overbright factor, but not the second stage, resulting in ugly clamping artifacts.
// Fix this by swapping which stage gets the overbright factor multiply.
if (material->stages[0].bundles[MaterialTextureBundleIndex::DiffuseMap].textures[0] == Texture::getWhite() && material->stages[0].bundles[MaterialTextureBundleIndex::Lightmap].isLightmap)
{
// First stage is lightmap only.
// If this is the first stage, don't multiply by overbright factor. Otherwise, multiply even if it's a blend.
if (this == &material->stages[0])
return;
}
else if (isBlend)
{
return;
}

(*baseColor) = vec4(baseColor->xyz() * g_overbrightFactor, baseColor->a);
(*vertColor) = vec4(vertColor->xyz() * g_overbrightFactor, vertColor->a);
}
Expand Down
3 changes: 0 additions & 3 deletions code/renderer_bgfx/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,6 @@ vec3 MirroredVector(const vec3 in, const Transform &surface, const Transform &ca

vec3 OverbrightenColor(vec3 color)
{
assert(in);
assert(out);

color *= g_overbrightFactor;

// Normalize by color instead of saturating to white.
Expand Down

0 comments on commit a65c7ea

Please sign in to comment.