Skip to content

Instantly share code, notes, and snippets.

@elect86
Last active May 16, 2017 16:56
Show Gist options
  • Select an option

  • Save elect86/2e51e18ea743675a9cf46f7731bd3c5a to your computer and use it in GitHub Desktop.

Select an option

Save elect86/2e51e18ea743675a9cf46f7731bd3c5a to your computer and use it in GitHub Desktop.
#version 330
#extension GL_ARB_draw_buffers : require
#include semantic.glsl
#include shade-blinnGaussSp.glsl
uniform sampler2D opaqueDepthTex;
layout (location = SUM_COLOR) out vec4 sumColor;
layout (location = SUM_WEIGHT) out float sumWeight;
uniform wbParams
{
vec2 size;
float depthScale;
int samples;
};
void main(void)
{
vec2 uv = gl_FragCoord.xy / size;
float opaqueDepth = texture(opaqueDepthTex, uv).r;
if (gl_FragCoord.z >= opaqueDepth)
discard;
vec4 color = shade();
// Assuming that the projection matrix is a perspective projection
// gl_FragCoord.w returns the inverse of the oPos.w register from the vertex shader
float viewDepth = abs(1.0 / gl_FragCoord.w);
// Tuned to work well with FP16 accumulation buffers and 0.001 < linearDepth < 2.5
// See Equation (9) from http://jcgt.org/published/0002/02/09/
float linearDepth = viewDepth * depthScale;
float weight = clamp(0.03 / (1e-5 + pow(linearDepth, 4.0)), 1e-2, 3e3);
sumColor = vec4(color.rgb * color.a, color.a) * weight;
sumWeight = color.a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment