Skip to content

Commit

Permalink
Core update (DOF shader).
Browse files Browse the repository at this point in the history
  • Loading branch information
fra committed Aug 14, 2023
1 parent da92f5d commit 1dab563
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
53 changes: 53 additions & 0 deletions resources/core/shader/dof_coc_fs.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// spiral sampling from https://blog.voxagon.se/2018/05/04/bokeh-depth-of-field-in-single-pass.html

#include <forward_pipeline.sh>

SAMPLER2D(u_color, 0);
SAMPLER2D(u_attr0, 1);

uniform vec4 u_params; // x: focus point, y: focus_length

#define GOLDEN_ANGLE 2.39996323
#define MAX_BLUR_SIZE 20.0
#define RAD_SCALE 0.75 // Smaller = nicer blur, larger = faster

float ComputeCoC(float depth) {
return clamp(abs(depth - u_params.x) / u_params.y, 0.0, 1.0) * MAX_BLUR_SIZE;
}

void main() {
// reference pixel
vec2 uv = gl_FragCoord.xy / uResolution.xy;
vec4 ref_color = texture2D(u_color, uv);
float ref_depth = texture2D(u_attr0, uv).w;

float ref_coc = ComputeCoC(ref_depth);

// sample CoC
float smp_count = 1.0;
vec4 dof_out = ref_color;

float radius = RAD_SCALE;

for (float a = 0.0; radius < MAX_BLUR_SIZE; a += GOLDEN_ANGLE) {
vec2 smp_uv = (gl_FragCoord.xy + vec2(cos(a) * radius, sin(a) * radius)) / uResolution.xy;

float smp_depth = texture2D(u_attr0, smp_uv).w;
vec4 smp_color = texture2D(u_color, smp_uv);

float coc = ref_coc;

if (smp_depth <= ref_depth) {
float smp_coc = ComputeCoC(smp_depth);
coc = min(ref_coc, smp_coc);
}

float k = smoothstep(radius - 0.5, radius + 0.5, coc);
dof_out += mix(dof_out / smp_count, smp_color, k);

smp_count += 1.0;
radius += RAD_SCALE / radius;
}

gl_FragColor = dof_out / smp_count;
}
4 changes: 4 additions & 0 deletions resources/core/shader/dof_coc_varying.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vec2 v_texcoord0 : TEXCOORD0;

vec3 a_position : POSITION;
vec2 a_texcoord0 : TEXCOORD0;
8 changes: 8 additions & 0 deletions resources/core/shader/dof_coc_vs.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$input a_position, a_texcoord0

// HARFANG(R) Copyright (C) 2022 Emmanuel Julien, NWNC HARFANG. Released under GPL/LGPL/Commercial Licence, see licence.txt for details.
#include <forward_pipeline.sh>

void main() {
gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0));
}

0 comments on commit 1dab563

Please sign in to comment.