From b75cee863814e36c78c3242694cc948aead5541b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Wed, 4 Oct 2017 18:40:15 +0200 Subject: [PATCH] Re-organising shaders --- shaders/dof-common.i.glsl | 23 +++++++++++++ shaders/dof-common.l.glsl | 60 ---------------------------------- shaders/dof-pass1.f.glsl | 4 +-- shaders/dof-pass2.f.glsl | 6 ++-- shaders/lib/dof.glsl | 43 ++++++++++++++++++++++++ shaders/lib/utils.glsl | 11 +++++++ shaders/{ => unused}/copy.glsl | 0 7 files changed, 82 insertions(+), 65 deletions(-) create mode 100644 shaders/dof-common.i.glsl delete mode 100644 shaders/dof-common.l.glsl create mode 100644 shaders/lib/dof.glsl create mode 100644 shaders/lib/utils.glsl rename shaders/{ => unused}/copy.glsl (100%) diff --git a/shaders/dof-common.i.glsl b/shaders/dof-common.i.glsl new file mode 100644 index 0000000..e18b7aa --- /dev/null +++ b/shaders/dof-common.i.glsl @@ -0,0 +1,23 @@ +//! type chunk + + +//#define DOF_USE_RANDOM + +layout( location = 0 ) uniform sampler2D u_Input; +layout( location = 1 ) uniform sampler2D u_Depth; +layout( location = 2 ) uniform vec4 u_Parameters; +layout( location = 3 ) uniform float u_Samples; +layout( location = 4 ) uniform vec3 u_ResolutionTime; + +#define uSharpDist (u_Parameters.x) +#define uSharpRange (u_Parameters.y) +#define uBlurFalloff (u_Parameters.z) +#define uMaxBlur (u_Parameters.w) + +#define uResolution (u_ResolutionTime.xy) +#define uTime (u_ResolutionTime.z) + +layout( location = 0 ) out vec3 o_Color; + + +//!include lib/dof.glsl diff --git a/shaders/dof-common.l.glsl b/shaders/dof-common.l.glsl deleted file mode 100644 index 7d6f985..0000000 --- a/shaders/dof-common.l.glsl +++ /dev/null @@ -1,60 +0,0 @@ -//! type chunk -//#define USE_RANDOM - -layout( location = 0 ) uniform sampler2D u_Input; -layout( location = 1 ) uniform sampler2D u_Depth; -layout( location = 2 ) uniform vec4 u_Parameters; -layout( location = 3 ) uniform float u_Samples; -layout( location = 4 ) uniform vec3 u_ResolutionTime; - -#define uSharpDist (u_Parameters.x) -#define uSharpRange (u_Parameters.y) -#define uBlurFalloff (u_Parameters.z) -#define uMaxBlur (u_Parameters.w) - -#define uResolution (u_ResolutionTime.xy) -#define uTime (u_ResolutionTime.z) - -float CoC( float z ) -{ - return uMaxBlur * min( 1 , - max( 0 , abs( z - uSharpDist ) - uSharpRange ) / uBlurFalloff ); -} - -layout( location = 0 ) out vec3 o_Color; - -float hash1( vec2 p ) -{ - p = fract(p * vec2(5.3987, 5.4421)); - p += dot(p.yx, p.xy + vec2(21.5351, 14.3137)); - return fract(p.x * p.y * 95.4307); -} - -// z: z at UV -// coc: blur radius at UV -// uv: initial coordinate -// blurvec: smudge direction -vec3 depthDirectionalBlur( float z , float coc , vec2 uv , vec2 blurvec ) -{ - vec3 sumcol = vec3( 0. ); - for ( int i = 0 ; i < u_Samples ; i++ ) { - float r = i; -#ifdef USE_RANDOM - r += hash1( uv + float( i + uTime ) ) - .5; -#endif - r = r / float( u_Samples - 1 ) - .5; - vec2 p = uv + r * coc * blurvec; - vec3 smpl = texture( u_Input , p ).xyz; - float sz = texture( u_Depth , p ).x; - if ( sz < z ) { - // if sample is closer consider it's CoC - p = uv + r * min( coc , CoC( sz ) ) * blurvec; - p = uv + r * CoC( sz ) * blurvec; - smpl = texture( u_Input , p ).xyz; - } - sumcol += smpl; - } - sumcol /= float( u_Samples ); - sumcol = max( sumcol , 0. ); - return sumcol; -} diff --git a/shaders/dof-pass1.f.glsl b/shaders/dof-pass1.f.glsl index ce8d91f..1ce53e1 100644 --- a/shaders/dof-pass1.f.glsl +++ b/shaders/dof-pass1.f.glsl @@ -1,7 +1,7 @@ #version 450 core //! type fragment -//! include dof-common.l.glsl +//! include dof-common.i.glsl void main() { @@ -9,5 +9,5 @@ void main() vec2 blurvec = vec2( 0 , 1 ) / uResolution; float z = texture( u_Depth , uv ).x; - o_Color = depthDirectionalBlur( z , CoC( z ) , uv , blurvec ); + o_Color = DOF_Blur( z , DOF_CoC( z ) , uv , blurvec ); } diff --git a/shaders/dof-pass2.f.glsl b/shaders/dof-pass2.f.glsl index 3c85929..2ab820a 100644 --- a/shaders/dof-pass2.f.glsl +++ b/shaders/dof-pass2.f.glsl @@ -1,7 +1,7 @@ #version 450 core //! type fragment -//! include dof-common.l.glsl +//! include dof-common.i.glsl void main() { @@ -10,11 +10,11 @@ void main() vec2 blurdir = vec2( 1.0 , 0.577350269189626 ); vec2 blurvec = normalize( blurdir ) / uResolution; - vec3 color0 = depthDirectionalBlur( z , CoC( z ) , uv , blurvec ); + vec3 color0 = DOF_Blur( z , DOF_CoC( z ) , uv , blurvec ); blurdir.x = -1; blurvec = normalize( blurdir ) / uResolution; - vec3 color1 = depthDirectionalBlur( z , CoC( z ) , uv , blurvec ); + vec3 color1 = DOF_Blur( z , DOF_CoC( z ) , uv , blurvec ); o_Color = min( color0 , color1 ); } diff --git a/shaders/lib/dof.glsl b/shaders/lib/dof.glsl new file mode 100644 index 0000000..5fd17c5 --- /dev/null +++ b/shaders/lib/dof.glsl @@ -0,0 +1,43 @@ +//!type library +//!include lib/utils.glsl + +float DOF_CoC( + in float z ) +{ + return uMaxBlur * min( 1 , + max( 0 , abs( z - uSharpDist ) - uSharpRange ) / uBlurFalloff ); +} + +// z: z at UV +// coc: blur radius at UV +// uv: initial coordinate +// blurvec: smudge direction +vec3 DOF_Blur( + in float z , + in float coc , + in vec2 uv , + in vec2 blurvec ) +{ + vec3 sumcol = vec3( 0. ); + for ( int i = 0 ; i < u_Samples ; i++ ) { + float r = i; +#ifdef DOF_USE_RANDOM + r += M_Hash( uv + float( i + uTime ) ) - .5; +#endif + r = r / float( u_Samples - 1 ) - .5; + vec2 p = uv + r * coc * blurvec; + vec3 smpl = texture( u_Input , p ).xyz; + float sz = texture( u_Depth , p ).x; + if ( sz < z ) { + // if sample is closer consider it's CoC + p = uv + r * min( coc , DOF_CoC( sz ) ) * blurvec; + p = uv + r * DOF_CoC( sz ) * blurvec; + smpl = texture( u_Input , p ).xyz; + } + sumcol += smpl; + } + sumcol /= float( u_Samples ); + sumcol = max( sumcol , 0. ); + return sumcol; +} + diff --git a/shaders/lib/utils.glsl b/shaders/lib/utils.glsl new file mode 100644 index 0000000..4511a13 --- /dev/null +++ b/shaders/lib/utils.glsl @@ -0,0 +1,11 @@ +//! type library + +// ----------------------------------------------------------------------------- + +float M_Hash( in vec2 p ) +{ + p = fract(p * vec2(5.3987, 5.4421)); + p += dot(p.yx, p.xy + vec2(21.5351, 14.3137)); + return fract(p.x * p.y * 95.4307); +} + diff --git a/shaders/copy.glsl b/shaders/unused/copy.glsl similarity index 100% rename from shaders/copy.glsl rename to shaders/unused/copy.glsl