Re-organising shaders

This commit is contained in:
Emmanuel BENOîT 2017-10-04 18:40:15 +02:00
parent d64e9f9a2e
commit b75cee8638
7 changed files with 82 additions and 65 deletions

23
shaders/dof-common.i.glsl Normal file
View file

@ -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

View file

@ -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;
}

View file

@ -1,7 +1,7 @@
#version 450 core #version 450 core
//! type fragment //! type fragment
//! include dof-common.l.glsl //! include dof-common.i.glsl
void main() void main()
{ {
@ -9,5 +9,5 @@ void main()
vec2 blurvec = vec2( 0 , 1 ) / uResolution; vec2 blurvec = vec2( 0 , 1 ) / uResolution;
float z = texture( u_Depth , uv ).x; 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 );
} }

View file

@ -1,7 +1,7 @@
#version 450 core #version 450 core
//! type fragment //! type fragment
//! include dof-common.l.glsl //! include dof-common.i.glsl
void main() void main()
{ {
@ -10,11 +10,11 @@ void main()
vec2 blurdir = vec2( 1.0 , 0.577350269189626 ); vec2 blurdir = vec2( 1.0 , 0.577350269189626 );
vec2 blurvec = normalize( blurdir ) / uResolution; 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; blurdir.x = -1;
blurvec = normalize( blurdir ) / uResolution; 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 ); o_Color = min( color0 , color1 );
} }

43
shaders/lib/dof.glsl Normal file
View file

@ -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;
}

11
shaders/lib/utils.glsl Normal file
View file

@ -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);
}