#include "blur.glsllib" const float BlurAmount = 4.0; void vert() { } void frag() // Simple averaging box blur. { gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); } void vert() { } void frag() // Mix the input blur and the depth texture with the sprite { vec4 sourceColor = textureLod(Texture0, TexCoord, 0.0); float depthTap = getDepthValue( texture2D(DepthSampler, TexCoord), CameraClipRange ); float linearDepth = depthValueToLinearDistance( depthTap, CameraClipRange ); float FocusStart = FocusDistance - FocusWidth; float FocusEnd = FocusDistance + FocusWidth; float blur = clamp( abs(linearDepth - FocusDistance) / (FocusEnd-FocusStart), 0.00, 1.0); //float blur = GetDepthMultiplier( TexCoord, DepthSampler, FocusDistance, FocusWidth, FocusWidth ); gl_FragColor = vec4( linearDepth, blur, 0.0, 1.0); // use a 3x3 filter to compute luminance difference vec3 avgColor = vec3(0); avgColor += textureLodOffset(Texture0, TexCoord, 0.0, ivec2(1, 0)).xyz; avgColor += textureLodOffset(Texture0, TexCoord, 0.0, ivec2(0, 1)).xyz; avgColor += textureLodOffset(Texture0, TexCoord, 0.0, ivec2(-1, 0)).xyz; avgColor += textureLodOffset(Texture0, TexCoord, 0.0, ivec2(0, -1)).xyz; avgColor += textureLodOffset(Texture0, TexCoord, 0.0, ivec2(1, 1)).xyz; avgColor += textureLodOffset(Texture0, TexCoord, 0.0, ivec2(1, -1)).xyz; avgColor += textureLodOffset(Texture0, TexCoord, 0.0, ivec2(-1, -1)).xyz; avgColor += textureLodOffset(Texture0, TexCoord, 0.0, ivec2(-1, 1)).xyz; avgColor /= 8.0; float currentLum = dot(vec3(0.2126, 0.7152, 0.0722), sourceColor.xyz); float averageLum = dot(vec3(0.2126, 0.7152, 0.0722), avgColor); //float currentLum = dot(vec3(1.0), sourceColor.xyz); //float averageLum = dot(vec3(1.0), avgColor); float lumDiff = max(currentLum - averageLum, 0.0); float cocSize = blur * MaxCoCRadius; if( lumDiff > MinBokehThreshold && lumDiff < MaxBokehThreshold && cocSize > MinCoCThreshold ) { ivec2 sourceSize, storeCoord; sourceSize = ivec2(textureSize(Texture0, 0).xy); uint add = uint(1); int current = int(atomicAdd( BokehCounter_data[0].y, add)); storeCoord.y = int(floor(float(current/sourceSize.x))); storeCoord.x = current - storeCoord.y*sourceSize.x; imageStore(BokehDetectSampler, ivec2(storeCoord.xy), vec4(TexCoord.x * float(sourceSize.x), TexCoord.y * float(sourceSize.y), linearDepth, blur)); vec3 bokehColor = sourceColor.xyz / (cocSize); imageStore(BokehColorSampler, ivec2(storeCoord.xy), vec4(bokehColor, 1.0)); } } out vec4 varColor; out float varRadius; out float varDepth; void vert() { ivec2 bufSize, coord; bufSize = textureSize(BokehPositionSampler,0).xy; coord.y = int(floor(float(gl_InstanceID / bufSize.x))); coord.x = gl_InstanceID - coord.y * bufSize.x; varColor = texelFetch(BokehSampler, coord, 0); vec4 pos = texelFetch(BokehPositionSampler, coord, 0); varRadius = pos.w * MaxBokehRadius; varDepth = pos.z; // convert to NDC -1, 1 range //pos.xy /= DestSize; //pos.xy = pos.xy * 2.0 - 1.0; vec4 pos1 = vec4( (attr_pos.xy + pos.xy) / DestSize.xy , 0.0, 1.0 ); gl_Position = pos1; } #include "viewProperties.glsllib" in float varRadius[]; in float varDepth[]; in vec4 varColor[]; out vec4 geColor; out float geDepth; out vec2 geUVPos; layout (points) in; layout (triangle_strip, max_vertices = 4) out; void main() { gl_Layer = 0; geColor = varColor[0]; geDepth = varDepth[0]; vec2 rcpSize = 1.0 / DestSize; //emit triangle at pos. vec2 offsetx = vec2(rcpSize.x * varRadius[0], 0.0); vec2 offsety = vec2(0.0, rcpSize.y * varRadius[0]); vec2 offsets = vec2(-1.0, -1.0); // Screen offset // Expand point into a quad gl_Position = vec4(offsets + 2.0*(gl_in[0].gl_Position.xy - offsetx - offsety),0.0 ,1.0); geUVPos = vec2(0,0); EmitVertex(); gl_Position = vec4(offsets + 2.0*(gl_in[0].gl_Position.xy + offsetx - offsety),0.0 ,1.0); geUVPos = vec2(1,0); EmitVertex(); gl_Position = vec4(offsets + 2.0*(gl_in[0].gl_Position.xy - offsetx + offsety),0.0 ,1.0); geUVPos = vec2(0,1); EmitVertex(); gl_Position = vec4(offsets + 2.0*(gl_in[0].gl_Position.xy + offsetx + offsety),0.0 ,1.0); geUVPos = vec2(1,1); EmitVertex(); EndPrimitive(); } in vec4 geColor; in vec2 geUVPos; in float geDepth; void frag() { float alpha = textureLod(BokehShapeSampler, geUVPos, 0.0 ).r; vec2 bd = textureLod(Texture0, gl_FragCoord.xy/vec2(textureSize(Texture0, 0)), 0.0 ).xy; float blur = bd.x; float depth = bd.y; float weight = clamp(depth - geDepth + 1.0, 0.0, 1.0); weight = clamp( weight + blur, 0.0, 1.0); gl_FragColor = vec4( geColor.xyz * alpha * weight, alpha * weight); } void vert() { // reset bokeh counter BokehCounter_data[0].y = uint(0); } void frag() // Mix the input blur and the depth texture with the sprite { vec4 sourceCol = texture2D( SourceSampler, TexCoord ); ivec2 storeCoord; ivec2 sourceSize = textureSize(BokehBufferSampler, 0).xy; storeCoord = ivec2(float(sourceSize.x) * TexCoord.x, float(sourceSize.y) * TexCoord.y); vec4 bokehCol = texelFetch( BokehBufferSampler, storeCoord, 0 ); gl_FragColor = vec4( bokehCol.xyz + sourceCol.xyz, sourceCol.a ); } void vert() { SetupBoxBlurCoords(vec2(Texture0Info.xy)); } void frag() // Simple averaging box blur. { gl_FragColor = BoxDepthBlur(DepthSampler, Texture0, Texture0Info.z, FocusDistance, FocusWidth, FocusWidth); } void vert() { SetupPoissonBlurCoords( BlurAmount, DestSize.xy ); } void frag() // Mix the input blur and the depth texture with the sprite { float centerMultiplier = GetDepthMultiplier( TexCoord, DepthSampler, FocusDistance, FocusWidth, FocusWidth ); if ( DepthDebug ) { gl_FragColor = vec4( centerMultiplier,centerMultiplier,centerMultiplier, 1.0 ); } else { vec4 blurColor = PoissonDepthBlur(Texture0, Texture0Info.z, DepthSampler, FocusDistance, FocusWidth, FocusWidth ); gl_FragColor = mix( texture2D(MergedBufferSampler,TexCoord), blurColor, centerMultiplier ); } }