diff options
Diffstat (limited to 'source/blender/draw/engines/eevee/shaders/effect_dof_frag.glsl')
-rw-r--r-- | source/blender/draw/engines/eevee/shaders/effect_dof_frag.glsl | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/source/blender/draw/engines/eevee/shaders/effect_dof_frag.glsl b/source/blender/draw/engines/eevee/shaders/effect_dof_frag.glsl new file mode 100644 index 00000000000..34d92fab2c2 --- /dev/null +++ b/source/blender/draw/engines/eevee/shaders/effect_dof_frag.glsl @@ -0,0 +1,222 @@ + +uniform mat4 ProjectionMatrix; + +uniform sampler2D colorBuffer; +uniform sampler2D depthBuffer; + +uniform vec3 dofParams; + +#define dof_aperturesize dofParams.x +#define dof_distance dofParams.y +#define dof_invsensorsize dofParams.z + +uniform vec4 bokehParams[2]; + +#define bokeh_rotation bokehParams[0].x +#define bokeh_ratio bokehParams[0].y +#define bokeh_maxsize bokehParams[0].z +#define bokeh_sides bokehParams[1] /* Polygon Bokeh shape number of sides (with precomputed vars) */ + +uniform vec2 nearFar; /* Near & far view depths values */ + +#define M_PI 3.1415926535897932384626433832795 +#define M_2PI 6.2831853071795864769252868 + +/* -------------- Utils ------------- */ + +/* divide by sensor size to get the normalized size */ +#define calculate_coc(zdepth) (dof_aperturesize * (dof_distance / zdepth - 1.0) * dof_invsensorsize) + +#define linear_depth(z) ((ProjectionMatrix[3][3] == 0.0) \ + ? (nearFar.x * nearFar.y) / (z * (nearFar.x - nearFar.y) + nearFar.y) \ + : (z * 2.0 - 1.0) * nearFar.y) + +#define weighted_sum(a, b, c, d, e) (a * e.x + b * e.y + c * e.z + d * e.w) + +float max_v4(vec4 v) { return max(max(v.x, v.y), max(v.z, v.w)); } + +#define THRESHOLD 0.0 + +#ifdef STEP_DOWNSAMPLE + +layout(location = 0) out vec4 nearColor; +layout(location = 1) out vec4 farColor; +layout(location = 2) out vec2 cocData; + +/* Downsample the color buffer to half resolution. + * Weight color samples by + * Compute maximum CoC for near and far blur. */ +void main(void) +{ + ivec4 uvs = ivec4(gl_FragCoord.xyxy) * 2 + ivec4(0, 0, 1, 1); + + /* custom downsampling */ + vec4 color1 = texelFetch(colorBuffer, uvs.xy, 0); + vec4 color2 = texelFetch(colorBuffer, uvs.zw, 0); + vec4 color3 = texelFetch(colorBuffer, uvs.zy, 0); + vec4 color4 = texelFetch(colorBuffer, uvs.xw, 0); + + /* Leverage SIMD by combining 4 depth samples into a vec4 */ + vec4 depth; + depth.r = texelFetch(depthBuffer, uvs.xy, 0).r; + depth.g = texelFetch(depthBuffer, uvs.zw, 0).r; + depth.b = texelFetch(depthBuffer, uvs.zy, 0).r; + depth.a = texelFetch(depthBuffer, uvs.xw, 0).r; + + vec4 zdepth = linear_depth(depth); + + /* Compute signed CoC for each depth samples */ + vec4 coc_near = calculate_coc(zdepth); + vec4 coc_far = -coc_near; + + /* now we need to write the near-far fields premultiplied by the coc */ + vec4 near_weights = step(THRESHOLD, coc_near); + vec4 far_weights = step(THRESHOLD, coc_far); + + /* now write output to weighted buffers. */ + nearColor = weighted_sum(color1, color2, color3, color4, near_weights); + farColor = weighted_sum(color1, color2, color3, color4, far_weights); + + /* Normalize the color (don't divide by 0.0) */ + nearColor /= max(1e-6, dot(near_weights, near_weights)); + farColor /= max(1e-6, dot(far_weights, far_weights)); + + float max_near_coc = max(max_v4(coc_near), 0.0); + float max_far_coc = max(max_v4(coc_far), 0.0); + + cocData = vec2(max_near_coc, max_far_coc); +} + +#elif defined(STEP_SCATTER) + +flat in vec4 color; +flat in float smoothFac; +flat in ivec2 edge; +/* coordinate used for calculating radius */ +in vec2 particlecoord; + +out vec4 fragColor; + +/* accumulate color in the near/far blur buffers */ +void main(void) +{ + /* Discard to avoid bleeding onto the next layer */ + if (int(gl_FragCoord.x) * edge.x + edge.y > 0) + discard; + + /* Circle Dof */ + float dist = length(particlecoord); + + /* Ouside of bokeh shape */ + if (dist > 1.0) + discard; + + /* Regular Polygon Dof */ + if (bokeh_sides.x > 0.0) { + /* Circle parametrization */ + float theta = atan(particlecoord.y, particlecoord.x) + bokeh_rotation; + + /* Optimized version of : + * float denom = theta - (M_2PI / bokeh_sides) * floor((bokeh_sides * theta + M_PI) / M_2PI); + * float r = cos(M_PI / bokeh_sides) / cos(denom); */ + float denom = theta - bokeh_sides.y * floor(bokeh_sides.z * theta + 0.5); + float r = bokeh_sides.w / cos(denom); + + /* Divide circle radial coord by the shape radius for angle theta. + * Giving us the new linear radius to the shape edge. */ + dist /= r; + + /* Ouside of bokeh shape */ + if (dist > 1.0) + discard; + } + + fragColor = color; + + /* Smooth the edges a bit. This effectively reduce the bokeh shape + * but does fade out the undersampling artifacts. */ + if (smoothFac < 1.0) { + fragColor *= smoothstep(1.0, smoothFac, dist); + } +} + +#elif defined(STEP_RESOLVE) + +#define MERGE_THRESHOLD 4.0 + +uniform sampler2D scatterBuffer; + +in vec4 uvcoordsvar; +out vec4 fragColor; + +vec4 upsample_filter(sampler2D tex, vec2 uv, vec2 texelSize) +{ + /* TODO FIXME: Clamp the sample position + * depending on the layer to avoid bleeding. + * This is not really noticeable so leaving it as is for now. */ + +#if 1 /* 9-tap bilinear upsampler (tent filter) */ + vec4 d = texelSize.xyxy * vec4(1, 1, -1, 0); + + vec4 s; + s = textureLod(tex, uv - d.xy, 0.0); + s += textureLod(tex, uv - d.wy, 0.0) * 2; + s += textureLod(tex, uv - d.zy, 0.0); + + s += textureLod(tex, uv + d.zw, 0.0) * 2; + s += textureLod(tex, uv , 0.0) * 4; + s += textureLod(tex, uv + d.xw, 0.0) * 2; + + s += textureLod(tex, uv + d.zy, 0.0); + s += textureLod(tex, uv + d.wy, 0.0) * 2; + s += textureLod(tex, uv + d.xy, 0.0); + + return s * (1.0 / 16.0); +#else + /* 4-tap bilinear upsampler */ + vec4 d = texelSize.xyxy * vec4(-1, -1, +1, +1) * 0.5; + + vec4 s; + s = textureLod(tex, uv + d.xy, 0.0); + s += textureLod(tex, uv + d.zy, 0.0); + s += textureLod(tex, uv + d.xw, 0.0); + s += textureLod(tex, uv + d.zw, 0.0); + + return s * (1.0 / 4.0); +#endif +} + +/* Combine the Far and Near color buffers */ +void main(void) +{ + vec2 uv = uvcoordsvar.xy; + /* Recompute Near / Far CoC per pixel */ + float depth = textureLod(depthBuffer, uv, 0.0).r; + float zdepth = linear_depth(depth); + float coc_signed = calculate_coc(zdepth); + float coc_far = max(-coc_signed, 0.0); + float coc_near = max(coc_signed, 0.0); + + vec2 texelSize = vec2(0.5, 1.0) / vec2(textureSize(scatterBuffer, 0)); + vec4 srccolor = textureLod(colorBuffer, uv, 0.0); + + vec2 near_uv = uv * vec2(0.5, 1.0); + vec2 far_uv = near_uv + vec2(0.5, 0.0); + vec4 farcolor = upsample_filter(scatterBuffer, far_uv, texelSize); + vec4 nearcolor = upsample_filter(scatterBuffer, near_uv, texelSize); + + float farweight = farcolor.a; + float nearweight = nearcolor.a; + + if (farcolor.a > 0.0) farcolor /= farcolor.a; + if (nearcolor.a > 0.0) nearcolor /= nearcolor.a; + + float mixfac = smoothstep(1.0, MERGE_THRESHOLD, abs(coc_signed)); + + float totalweight = nearweight + farweight; + farcolor = mix(srccolor, farcolor, mixfac); + nearcolor = mix(srccolor, nearcolor, mixfac); + fragColor = mix(farcolor, nearcolor, nearweight / max(1e-6, totalweight)); +} + +#endif |