Shaders - Relative paths

Shaders are no longer found under /shaders in the project; they can be
anywhere. In addition, paths for both (program) instructions in the
script and include directives in shader source code are relative to the
file which contains them.
This commit is contained in:
Emmanuel BENOîT 2017-12-29 11:33:15 +01:00
parent 3344f96af0
commit 68d01ca42e
41 changed files with 198 additions and 132 deletions

62
test/fx-dof/dof-cs.glsl Normal file
View file

@ -0,0 +1,62 @@
//! type chunk
//#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;
}

43
test/fx-dof/dof-lib.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;
}

View file

@ -0,0 +1,21 @@
#version 450 core
//! type compute
layout(
local_size_x = 8 ,
local_size_y = 64
) in;
//! include 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 ) );
}

View file

@ -0,0 +1,13 @@
#version 450 core
//! type fragment
//! include dof.glsl
void main()
{
vec2 uv = gl_FragCoord.xy / uResolution;
vec2 blurvec = vec2( 0 , 1 ) / uResolution;
float z = texture( u_Depth , uv ).x;
o_Color = DOF_Blur( z , DOF_CoC( z ) , uv , blurvec );
}

View file

@ -0,0 +1,27 @@
#version 450 core
//! type compute
layout(
local_size_x = 8 ,
local_size_y = 20
) in;
//! include 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. ) );
}

View file

@ -0,0 +1,20 @@
#version 450 core
//! type fragment
//! include dof.glsl
void main()
{
vec2 uv = gl_FragCoord.xy / uResolution;
float z = texture( u_Depth , uv ).x;
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 );
o_Color = min( color0 , color1 );
}

23
test/fx-dof/dof.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 dof-lib.glsl

125
test/fx-dof/fx-dof.srd Normal file
View file

@ -0,0 +1,125 @@
################################################################################
# Depth of Field
(fn dof-init ()
# Sampler used for the inputs
(sampler smp-dof
(mipmaps no)
(wrapping clamp-edge)
(sampling linear)
(lod 0 0)
)
(if $use-compute (
# Textures for both passes
(texture tx-dof-pass1 rgba-f16 $vp-width $vp-height)
(texture tx-dof-pass2 rgba-f16 $vp-width $vp-height)
# Programs
(program prg-dof-pass1 "dof-pass1.c.glsl")
(program prg-dof-pass2 "dof-pass2.c.glsl")
# Pipelines
(pipeline pl-dof-pass1 prg-dof-pass1)
(pipeline pl-dof-pass2 prg-dof-pass2)
)(
# Texture & RT for pass 1
(texture tx-dof-pass1 rgb-f16 $vp-width $vp-height)
(framebuffer rt-dof-pass1 tx-dof-pass1)
# Texture & RT for pass 2
(texture tx-dof-pass2 rgb-f16 $vp-width $vp-height)
(framebuffer rt-dof-pass2 tx-dof-pass2)
# TODO MAYBE ? (alias tx-dof-output tx-dof-pass2)
# Programs
(program prg-dof-pass1 "dof-pass1.f.glsl")
(program prg-dof-pass2 "dof-pass2.f.glsl")
# Pipelines
(pipeline pl-dof-pass1 prg-fullscreen prg-dof-pass1)
(pipeline pl-dof-pass2 prg-fullscreen prg-dof-pass2)
))
# Output debugging
(odbg tx-dof-pass1 hdr "DoF - First pass")
(odbg tx-dof-pass2 hdr "DoF - Output")
# Inputs
(input dof-sharp-distance 0)
(input dof-sharp-range 50)
(input dof-falloff 50)
(input dof-max-blur 16)
(input dof-samples 16)
# Input overrides
(ui-overrides
(section "Post-processing"
(section "Depth of Field"
(float "Sharp distance" dof-sharp-distance
(min 0) (max 50000) (step .1))
(float "Sharp range" dof-sharp-range
(min 0) (max 50000) (step .1))
(float "Falloff" dof-falloff
(min 0) (max 50000) (step .1))
(float "Maximum blur" dof-max-blur
(min 1) (max 64) (slider))
(int "Samples" dof-samples
(min 1) (max 64) (slider))
)
)
)
)
(fn dof-set-uniforms (prog)
(uniforms prog 2
(get-input dof-sharp-distance)
(get-input dof-sharp-range)
(get-input dof-falloff)
(get-input dof-max-blur)
)
(uniforms prog 3 (get-input dof-samples))
(uniforms prog 4 $vp-width $vp-height $time)
)
(fn dof-render (in-image in-depth)
(profiling "Depth of Field"
(uniforms-i prg-dof-pass1 0 0)
(uniforms-i prg-dof-pass2 0 0)
(if (not $use-compute) (
(use-texture 1 in-depth smp-dof)
(uniforms-i prg-dof-pass1 1 1)
(uniforms-i prg-dof-pass2 1 1)
))
# First pass
(call dof-set-uniforms prg-dof-pass1)
(use-texture 0 in-image smp-dof)
(use-pipeline pl-dof-pass1)
(if $use-compute (
(image 0 tx-dof-pass1 0)
(compute $vp-width $vp-height 1)
)(
(use-framebuffer rt-dof-pass1)
(viewport 0 0 $vp-width $vp-height)
(fullscreen)
))
# Second pass
(call dof-set-uniforms prg-dof-pass2)
(use-texture 0 tx-dof-pass1 smp-dof)
(use-pipeline pl-dof-pass2)
(if $use-compute (
(image 0 tx-dof-pass2 0)
(compute $vp-width $vp-height 1)
)(
(use-framebuffer rt-dof-pass2)
(viewport 0 0 $vp-width $vp-height)
(fullscreen)
))
)
)
# vim: syntax=demo-srd