Skip to content

Instantly share code, notes, and snippets.

@Bananaft
Last active March 11, 2019 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bananaft/ea515040f116d19665ecbf8c3df013a6 to your computer and use it in GitHub Desktop.
Save Bananaft/ea515040f116d19665ecbf8c3df013a6 to your computer and use it in GitHub Desktop.
#include "Uniforms.glsl"
#include "Samplers.glsl"
#include "Transform.glsl"
#include "ScreenPos.glsl"
#include "PostProcess.glsl"
varying vec2 vTexCoord;
varying vec2 vScreenPos;
#ifdef COMPILEPS
uniform float cAutoExposureAdaptRate;
uniform vec2 cAutoExposureLumRange;
uniform float cAutoExposureMiddleGrey;
uniform vec2 cHDR128InvSize;
uniform vec2 cLum64InvSize;
uniform vec2 cLum16InvSize;
uniform vec2 cLum4InvSize;
float GatherAvgLum(sampler2D texSampler, vec2 texCoord, vec2 texelSize)
{
float lumAvg = 0.0;
lumAvg += texture2D(texSampler, texCoord + vec2(1.0, -1.0) * texelSize).r;
lumAvg += texture2D(texSampler, texCoord + vec2(-1.0, 1.0) * texelSize).r;
lumAvg += texture2D(texSampler, texCoord + vec2(1.0, 1.0) * texelSize).r;
lumAvg += texture2D(texSampler, texCoord + vec2(1.0, -1.0) * texelSize).r;
return lumAvg / 4.0;
}
#endif
void VS()
{
mat4 modelMatrix = iModelMatrix;
vec3 worldPos = GetWorldPos(modelMatrix);
gl_Position = GetClipPos(worldPos);
vTexCoord = GetQuadTexCoord(gl_Position);
vScreenPos = GetScreenPosPreDiv(gl_Position);
}
void PS()
{
#ifdef LUMINANCE64
float logLumSum = 0.0;
logLumSum += log(dot(texture2D(sDiffMap, vTexCoord + vec2(-1.0, -1.0) * cHDR128InvSize).rgb, LumWeights) + 1e-5);
logLumSum += log(dot(texture2D(sDiffMap, vTexCoord + vec2(-1.0, 1.0) * cHDR128InvSize).rgb, LumWeights) + 1e-5);
logLumSum += log(dot(texture2D(sDiffMap, vTexCoord + vec2(1.0, 1.0) * cHDR128InvSize).rgb, LumWeights) + 1e-5);
logLumSum += log(dot(texture2D(sDiffMap, vTexCoord + vec2(1.0, -1.0) * cHDR128InvSize).rgb, LumWeights) + 1e-5);
gl_FragColor.r = logLumSum/4.;
#endif
#ifdef LUMINANCE16
gl_FragColor.r = GatherAvgLum(sDiffMap, vTexCoord, cLum64InvSize);
#endif
#ifdef LUMINANCE4
gl_FragColor.r = GatherAvgLum(sDiffMap, vTexCoord, cLum16InvSize);
#endif
#ifdef LUMINANCE1
gl_FragColor.r = exp(GatherAvgLum(sDiffMap, vTexCoord, cLum4InvSize));
#endif
#ifdef ADAPTLUMINANCE
float adaptedLum = texture2D(sDiffMap, vTexCoord).r;
float lum = clamp(texture2D(sNormalMap, vTexCoord).r, cAutoExposureLumRange.x, cAutoExposureLumRange.y);
gl_FragColor.r = adaptedLum + (lum - adaptedLum) * (1.0 - exp(-cDeltaTimePS * cAutoExposureAdaptRate));
#endif
#ifdef EXPOSE
vec3 color = texture2D(sDiffMap, vScreenPos).rgb;
float adaptedLum = texture2D(sNormalMap, vTexCoord).r;
// if (vScreenPos.x < 0.1)
// {
// if (vScreenPos.y < adaptedLum)
// color = vec3(0.1,0.3,0.);
// }
/// Numbers output by FabriceNeyret2 https://www.shadertoy.com/view/4lXSR4
// int x = 28-int(vScreenPos.x * 1000)/3, y = int(vScreenPos.y * 1000)/3,
// c = int( adaptedLum / pow(10.,float(x/4-3)) );
// x-=x/4*4;
// c = ( x<1||y<1||y>5? 0: y>4? 972980223: y>3? 690407533: y>2? 704642687: y>1? 696556137: 972881535 ) / int(exp2(float(x+26+c/10*30-3*c)));
// vec3 num = vec3( max(c-c/2*2 , 0) );
// gl_FragColor = vec4( vec3(color * (cAutoExposureMiddleGrey / adaptedLum) + num*0.2), 1.0);
gl_FragColor = vec4(color * (cAutoExposureMiddleGrey / adaptedLum), 1.0);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment