Skip to content

Commit

Permalink
Do lighting calculations and HDR in linear space, but not blending.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcy committed Aug 21, 2016
1 parent 8c926c0 commit b06828d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion code/renderer_bgfx/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ void Main::renderScene(const SceneDefinition &scene)
(
Clamped(g_cvars.brightness.getFloat() - 1.0f, -0.8f, 0.8f),
Clamped(g_cvars.contrast.getFloat(), 0.5f, 3.0f),
Clamped(g_cvars.gamma.getFloat(), 0.5f, 3.0f),
Clamped(g_cvars.hdrGamma.getFloat(), 0.5f, 3.0f),
Clamped(g_cvars.saturation.getFloat(), 0.0f, 3.0f)
));

Expand Down
3 changes: 2 additions & 1 deletion code/renderer_bgfx/Main_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ void ConsoleVariables::initialize()
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.7", 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 Down
1 change: 1 addition & 0 deletions code/renderer_bgfx/Precompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ struct ConsoleVariables
ConsoleVariable hdr;
ConsoleVariable hdrBloomScale;
ConsoleVariable hdrExposure;
ConsoleVariable hdrGamma;
ConsoleVariable lerpTextureAnimation;
ConsoleVariable maxAnisotropy;
ConsoleVariable picmip;
Expand Down
10 changes: 0 additions & 10 deletions code/renderer_bgfx/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,16 +562,10 @@ int Vsnprintf(char *str, size_t size, const char *format, va_list ap)
#endif
}

//#define USE_SRGB

vec3 ToGamma(vec3 color)
{
#ifdef USE_SRGB
const float invGamma = 1.0f / 2.2f;
return vec3(pow(color.r, invGamma), pow(color.g, invGamma), pow(color.b, invGamma));
#else
return color;
#endif
}

vec4 ToGamma(vec4 color)
Expand All @@ -581,12 +575,8 @@ vec4 ToGamma(vec4 color)

vec3 ToLinear(vec3 color)
{
#ifdef USE_SRGB
const float gamma = 2.2f;
return vec3(pow(color.r, gamma), pow(color.g, gamma), pow(color.b, gamma));
#else
return color;
#endif
}

vec4 ToLinear(vec4 color)
Expand Down
5 changes: 3 additions & 2 deletions shaders/Generic_fragment.sc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ void main()
diffuse = mix(diffuse, diffuse2, u_Animation_Enabled_Fraction.y);
}

diffuse.rgb = ToLinear(diffuse.rgb);
float alpha;

if (u_AlphaGen == AGEN_WATER)
Expand Down Expand Up @@ -215,7 +216,7 @@ void main()

if (lightType == LIGHT_MAP)
{
diffuseLight = DecodeRGBM(texture2D(u_LightSampler, v_texcoord1));
diffuseLight = ToLinear(DecodeRGBM(texture2D(u_LightSampler, v_texcoord1)));
}
else if (lightType == LIGHT_VECTOR)
{
Expand Down Expand Up @@ -286,7 +287,7 @@ void main()
}
#endif // USE_DYNAMIC_LIGHTS

vec4 fragColor = vec4(diffuse.rgb * vertexColor * diffuseLight, alpha);
vec4 fragColor = vec4(ToGamma(diffuse.rgb * vertexColor * diffuseLight), alpha);

#if defined(USE_HDR)
gl_FragData[0] = fragColor;
Expand Down
8 changes: 5 additions & 3 deletions shaders/ToneMap_fragment.sc
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ vec3 ACESFilm(vec3 x)

void main()
{
vec3 color = texture2D(u_TextureSampler, v_texcoord0).rgb;
vec3 color = ToLinear(texture2D(u_TextureSampler, v_texcoord0).rgb);

// bloom
color += texture2D(u_BloomSampler, v_texcoord0).rgb * u_Hdr_BloomScale_Exposure.x;
color += ToLinear(texture2D(u_BloomSampler, v_texcoord0).rgb) * u_Hdr_BloomScale_Exposure.x;

// tone map
color = ACESFilm(color * u_Hdr_BloomScale_Exposure.y);

color = ToGamma(color);

// contrast and brightness
color = (color - 0.5) * contrast + 0.5 + brightness;

Expand All @@ -44,7 +46,7 @@ void main()
color = mix(intensity, color, saturation);

// gamma
//color = pow(color, vec3_splat(1.0 / gamma));
color = pow(color, vec3_splat(1.0 / gamma));

gl_FragColor = vec4(color, 1.0);
}

0 comments on commit b06828d

Please sign in to comment.