Test - Converted DoF to compute shaders
It doesn't work too well, however. Other algorithms need to be checked out.
This commit is contained in:
parent
eb2e83a568
commit
c3ccc9403a
4 changed files with 165 additions and 25 deletions
test/shaders
67
test/shaders/chunks/dof-cs.glsl
Normal file
67
test/shaders/chunks/dof-cs.glsl
Normal file
|
@ -0,0 +1,67 @@
|
|||
//! type chunk
|
||||
|
||||
layout(
|
||||
local_size_x = 64 ,
|
||||
local_size_y = 16
|
||||
) in;
|
||||
|
||||
//#define DOF_USE_RANDOM
|
||||
|
||||
layout( location = 0 ) uniform sampler2D u_Input;
|
||||
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( binding = 0 , rgba16f ) writeonly uniform image2D u_Output;
|
||||
|
||||
//!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;
|
||||
vec4 d = textureLod( u_Input , p , 0 );
|
||||
vec3 smpl = d.xyz;
|
||||
float sz = d.w;
|
||||
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 = textureLod( u_Input , p , 0 ).xyz;
|
||||
}
|
||||
sumcol += smpl;
|
||||
}
|
||||
sumcol /= float( u_Samples );
|
||||
sumcol = max( sumcol , 0. );
|
||||
return sumcol;
|
||||
}
|
||||
|
15
test/shaders/dof-pass1.c.glsl
Normal file
15
test/shaders/dof-pass1.c.glsl
Normal file
|
@ -0,0 +1,15 @@
|
|||
#version 450 core
|
||||
|
||||
//! type compute
|
||||
//! include chunks/dof-cs.glsl
|
||||
|
||||
void main()
|
||||
{
|
||||
ivec2 coords = ivec2( gl_GlobalInvocationID.xy );
|
||||
vec2 uv = vec2( coords ) / uResolution;
|
||||
vec2 blurvec = vec2( 0 , 1 ) / uResolution;
|
||||
|
||||
float z = textureLod( u_Input , uv , 0 ).w;
|
||||
vec3 c = DOF_Blur( z , DOF_CoC( z ) , uv , blurvec );
|
||||
imageStore( u_Output , coords , vec4( c , z ) );
|
||||
}
|
21
test/shaders/dof-pass2.c.glsl
Normal file
21
test/shaders/dof-pass2.c.glsl
Normal file
|
@ -0,0 +1,21 @@
|
|||
#version 450 core
|
||||
|
||||
//! type compute
|
||||
//! include chunks/dof-cs.glsl
|
||||
|
||||
void main()
|
||||
{
|
||||
ivec2 coords = ivec2( gl_GlobalInvocationID.xy );
|
||||
vec2 uv = vec2( coords ) / uResolution;
|
||||
float z = textureLod( u_Input , uv , 0 ).w;
|
||||
|
||||
vec2 blurdir = vec2( 1.0 , 0.577350269189626 );
|
||||
vec2 blurvec = normalize( blurdir ) / uResolution;
|
||||
vec3 color0 = DOF_Blur( z , DOF_CoC( z ) , uv , blurvec );
|
||||
|
||||
blurdir.x = -1;
|
||||
blurvec = normalize( blurdir ) / uResolution;
|
||||
vec3 color1 = DOF_Blur( z , DOF_CoC( z ) , uv , blurvec );
|
||||
|
||||
imageStore( u_Output , coords , vec4( min( color0 , color1 ) , 1. ) );
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue