Moved test "demo" to separate directory

This commit is contained in:
Emmanuel BENOîT 2017-12-03 10:23:37 +01:00
parent dcd149eee5
commit 64629560cc
21 changed files with 9 additions and 0 deletions

9
test/build.srd Normal file
View file

@ -0,0 +1,9 @@
(cfg "Default"
(resolutions
(1280 720)
(1920 1080)
)
(optimizer on
(constant-folding on)
)
)

559
test/demo.srd Normal file
View file

@ -0,0 +1,559 @@
(init
# Compute viewport size
(locals aspect-ratio)
(set aspect-ratio (div 16 9))
(if (cmp-gt (div $width $aspect-ratio) $height)
(
(set vp-height $height)
(set vp-width (mul $height $aspect-ratio))
)(
(set vp-width $width)
(set vp-height (div $width $aspect-ratio))
)
)
# Viewport location
(set vp-x (div (sub $width $vp-width) 2))
(set vp-y (div (sub $height $vp-height) 2))
(program prg-fullscreen "fullscreen.v.glsl")
(call scene-init)
(call dof-init)
(call bloom-init)
(call combine-init)
(call fxaa-init)
)
(frame
(profiling "Frame render"
(call scene-render)
(call dof-render tx-scene-output tx-scene-depth)
(call bloom-render tx-scene-output)
(call combine-render tx-dof-pass2 tx-bloom1)
(call fxaa-render tx-combined)
)
)
################################################################################
# Scene
(fn scene-init ()
(texture tx-scene-output rgb-f16 $vp-width $vp-height)
(texture tx-scene-depth r-f16 $vp-width $vp-height)
(framebuffer rt-scene
(color tx-scene-output)
(color tx-scene-depth))
(program prg-scene-p1 "scene.f.glsl")
(pipeline pl-scene-p1 prg-fullscreen prg-scene-p1)
(odbg tx-scene-output hdr "Scene output")
(odbg tx-scene-depth depth "Scene depth")
(input camera-pos-x 0)
(input camera-pos-y 0)
(input camera-pos-z 30)
(input camera-lookat-x 0)
(input camera-lookat-y 0)
(input camera-lookat-z 0)
(input camera-up-x 0)
(input camera-up-y 1)
(input camera-up-z 0)
(input camera-nearplane 2)
(input raymarcher-iterations 256)
(input raymarcher-step 1.2)
(input raymarcher-epsilon .00001)
(input raymarcher-max-dist 250)
(input raymarcher-correction 10)
(input fog .00015)
(ui-overrides
(section "Scenes"
(section "Raymarcher"
(int "Iterations" raymarcher-iterations
(min 1) (max 2048) (step .05))
(float "Step size" raymarcher-step
(min 0) (max 2) (slider))
(float "Epsilon" raymarcher-epsilon
(min 1e-10) (max 1) (power 1)
(decimals 12) (step 1e-8))
(float "Maximal distance" raymarcher-max-dist
(min 1e-2) (max 1000000) (decimals 0)
(step .05))
(int "Correction steps" raymarcher-correction
(min 0) (max 100) (slider))
)
(camera "Camera"
(near-plane camera-nearplane)
(position camera-pos-x camera-pos-y camera-pos-z)
(up camera-up-x camera-up-y camera-up-z)
(look-at camera-lookat-x camera-lookat-y camera-lookat-z))
(float "Fog" fog
(min 0) (max 1) (step .000005) (decimals 5))
)
)
)
(fn scene-render ()
(profiling "Scene render"
(uniforms prg-scene-p1 1 $vp-width $vp-height)
(uniforms prg-scene-p1 0 $time)
(uniforms prg-scene-p1 2
(get-input camera-pos-x)
(get-input camera-pos-y)
(get-input camera-pos-z)
)
(uniforms prg-scene-p1 3
(get-input camera-lookat-x)
(get-input camera-lookat-y)
(get-input camera-lookat-z)
)
(uniforms prg-scene-p1 4
(get-input camera-up-x)
(get-input camera-up-y)
(get-input camera-up-z)
)
(uniforms prg-scene-p1 5
(get-input camera-nearplane)
)
(uniforms prg-scene-p1 6
(get-input raymarcher-iterations)
(get-input raymarcher-step)
(get-input raymarcher-epsilon)
(get-input raymarcher-max-dist)
)
(uniforms prg-scene-p1 7 (get-input fog))
(uniforms-i prg-scene-p1 8 (get-input raymarcher-correction))
(use-pipeline pl-scene-p1)
(use-framebuffer rt-scene)
(viewport 0 0 $vp-width $vp-height)
(fullscreen)
)
)
################################################################################
# Depth of Field
(fn dof-init ()
# Sampler used for the inputs
(sampler smp-dof
(mipmaps no)
(wrapping clamp-edge)
(sampling linear)
(lod 0 0)
)
# 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)
# Output debugging
(odbg tx-dof-pass1 hdr "DoF - First pass")
(odbg tx-dof-pass2 hdr "DoF - Output")
# 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)
# 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"
(use-texture 1 in-depth smp-dof)
(uniforms-i prg-dof-pass1 0 0)
(uniforms-i prg-dof-pass1 1 1)
(uniforms-i prg-dof-pass2 0 0)
(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)
(use-framebuffer rt-dof-pass1)
(fullscreen)
# Second pass
(call dof-set-uniforms prg-dof-pass2)
(use-texture 0 tx-dof-pass1 smp-dof)
(use-pipeline pl-dof-pass2)
(use-framebuffer rt-dof-pass2)
(viewport 0 0 $vp-width $vp-height)
(fullscreen)
)
)
################################################################################
# Bloom
(fn bloom-init ()
# Samplers
(sampler smp-bloom-blur
(sampling linear)
(mipmaps linear)
(lod 0 100)
(wrapping clamp-edge)
)
(sampler smp-bloom-input
(sampling nearest)
)
# First ping/pong texture & targets
(texture tx-bloom1 rgb-f16 $vp-width $vp-height 6)
(framebuffer rt-bloom1-lod0 (color tx-bloom1 0))
(framebuffer rt-bloom1-lod1 (color tx-bloom1 1))
(framebuffer rt-bloom1-lod2 (color tx-bloom1 2))
(framebuffer rt-bloom1-lod3 (color tx-bloom1 3))
(framebuffer rt-bloom1-lod4 (color tx-bloom1 4))
(framebuffer rt-bloom1-lod5 (color tx-bloom1 5))
(odbg tx-bloom1 hdr "Bloom")
# Second ping/pong texture & targets
(texture tx-bloom2 rgb-f16 $vp-width $vp-height 6)
(framebuffer rt-bloom2-lod0 (color tx-bloom2 0))
(framebuffer rt-bloom2-lod1 (color tx-bloom2 1))
(framebuffer rt-bloom2-lod2 (color tx-bloom2 2))
(framebuffer rt-bloom2-lod3 (color tx-bloom2 3))
(framebuffer rt-bloom2-lod4 (color tx-bloom2 4))
(framebuffer rt-bloom2-lod5 (color tx-bloom2 5))
(odbg tx-bloom2 hdr "Bloom (partial)")
# High pass program
(program prg-bloom-highpass "bloom-highpass.f.glsl")
(pipeline pl-bloom-highpass prg-fullscreen prg-bloom-highpass)
# Downsampling program
(program prg-bloom-downsample "downsample.f.glsl")
(pipeline pl-bloom-downsample prg-fullscreen prg-bloom-downsample)
# Blur pass
(program prg-bloom-blur "blur-pass.f.glsl")
(pipeline pl-bloom-blur prg-fullscreen prg-bloom-blur)
# Inputs that control the high pass filter
(input bloom-power 1.6)
(input bloom-factor 1.2)
(input bloom-output-factor 1.05)
# Inputs that control the blur weights
(input bloom-bw0 .324)
(input bloom-bw1 .232)
(input bloom-bw2 .0855)
(input bloom-bw3 .0205)
# Blur size
(input bloom-blur-size 1.3)
# Input overrides
(ui-overrides
(section "Post-processing"
(section "Bloom"
(section "Input filter"
(float "Input power" bloom-power
(min .5) (max 4) (slider))
(float "Input factor" bloom-factor
(min .5) (max 4) (slider))
(float "Output factor" bloom-output-factor
(min .5) (max 4) (slider))
)
(section "Blur"
(float "Size" bloom-blur-size
(min .5) (max 4) (slider))
(float4 "Weights"
bloom-bw0 bloom-bw1 bloom-bw2 bloom-bw3
(min 0) (max 1) (step .00005))
)
)
)
)
)
(fn bloom-render (in-scene)
(profiling "BLOOOOM!"
(uniforms-i prg-bloom-highpass 0 0)
(uniforms-i prg-bloom-highpass 1 0)
(uniforms prg-bloom-highpass 2 $vp-width $vp-height)
(uniforms-i prg-bloom-downsample 0 0)
(uniforms-i prg-bloom-blur 0 0)
# High pass
(use-texture 0 in-scene smp-bloom-input)
(uniforms prg-bloom-highpass 3
(get-input bloom-power)
(get-input bloom-factor)
(get-input bloom-output-factor)
)
(use-framebuffer rt-bloom1-lod0)
(use-pipeline pl-bloom-highpass)
(viewport 0 0 $vp-width $vp-height)
(fullscreen)
# Set up blur weights
(uniforms prg-bloom-blur 4
(get-input bloom-bw0)
(get-input bloom-bw1)
(get-input bloom-bw2)
(get-input bloom-bw3))
# Blur & downsample
(uniforms prg-bloom-blur 1 $vp-width $vp-height)
(call bloom-blur-pass 0 rt-bloom1-lod0 rt-bloom2-lod0)
(call bloom-downsample rt-bloom1-lod1 1)
(call bloom-blur-pass 1 rt-bloom1-lod1 rt-bloom2-lod1)
(call bloom-downsample rt-bloom1-lod2 2)
(call bloom-blur-pass 2 rt-bloom1-lod2 rt-bloom2-lod2)
(call bloom-downsample rt-bloom1-lod3 3)
(call bloom-blur-pass 3 rt-bloom1-lod3 rt-bloom2-lod3)
(call bloom-downsample rt-bloom1-lod4 4)
(call bloom-blur-pass 4 rt-bloom1-lod4 rt-bloom2-lod4)
(call bloom-downsample rt-bloom1-lod5 5)
(call bloom-blur-pass 5 rt-bloom1-lod5 rt-bloom2-lod5)
)
)
(fn bloom-downsample (target level)
(locals m w h)
(set m (inv (pow 2 $level)))
(set w (mul $vp-width $m))
(set h (mul $vp-height $m))
(use-pipeline pl-bloom-downsample)
(use-framebuffer target)
(use-texture 0 tx-bloom1 smp-bloom-blur)
(uniforms prg-bloom-downsample 1 $w $h)
(uniforms-i prg-bloom-downsample 2 (sub $level 1))
(viewport 0 0 $w $h)
(fullscreen)
(uniforms prg-bloom-blur 1 $w $h)
)
(fn bloom-blur-pass (lod fb1 fb2)
# Common stuff for both passes
(use-pipeline pl-bloom-blur)
(uniforms-i prg-bloom-blur 2 $lod)
# Pass 1
(use-framebuffer fb2)
(use-texture 0 tx-bloom1 smp-bloom-blur)
(uniforms prg-bloom-blur 3
(get-input bloom-blur-size) 0)
(fullscreen)
# Pass 2
(use-framebuffer fb1)
(use-texture 0 tx-bloom2 smp-bloom-blur)
(uniforms prg-bloom-blur 3
0 (get-input bloom-blur-size))
(fullscreen)
)
################################################################################
# Combine
(fn combine-init ()
# Assets
(sampler smp-combine-input (sampling nearest))
(sampler smp-combine-bloom
(sampling linear)
(mipmaps nearest)
(lod 0 100))
(texture tx-combined rgba-nu8 $vp-width $vp-height)
(odbg tx-combined ldr-alpha "Combined output")
(framebuffer rt-combined tx-combined)
(program prg-combine "combine.f.glsl")
(pipeline pl-combine prg-fullscreen prg-combine)
# Bloom parameters
(input bloom-strength .45)
(input bloom-attenuation .3)
# Vignette effect
(input vignette-shape-m 1)
(input vignette-shape-p 8)
(input vignette-shape-r 1)
(input vignette-aspect-ratio .5)
(input vignette-apply-base 0)
(input vignette-apply-max 1)
# Color grading
(input cg-lift-r 0)
(input cg-lift-g 0)
(input cg-lift-b 0)
(input cg-gain-r 1)
(input cg-gain-g 1)
(input cg-gain-b 1)
(input cg-gamma-r 0)
(input cg-gamma-g 0)
(input cg-gamma-b 0)
# Input overrides
(ui-overrides
(section "Post-processing"
(section "Bloom"
(section "Output"
(float "Strength" bloom-strength
(min 0) (max 1) (slider))
(float "Attenuation / level" bloom-attenuation
(min 0) (max 1) (slider))
)
)
(section "Color grading"
(color-grading "Lift"
cg-lift-r cg-lift-g cg-lift-b
(base -.1) (unit .1))
(color-grading "Gain"
cg-gain-r cg-gain-g cg-gain-b
(base 0) (unit 1))
(color-grading "Gamma"
cg-gamma-r cg-gamma-g cg-gamma-b
(base -.9) (unit .9))
)
)
# FIXME: overrides for vignette
)
)
(fn combine-render (in-main in-bloom)
(profiling "Combine"
(uniforms-i prg-combine 0 0)
(uniforms-i prg-combine 1 1)
(uniforms prg-combine 2 $vp-width $vp-height)
(use-pipeline pl-combine)
(use-framebuffer rt-combined)
(use-texture 0 in-main smp-combine-input)
(use-texture 1 in-bloom smp-combine-bloom)
(uniforms prg-combine 3
(get-input bloom-strength)
(get-input bloom-attenuation))
(uniforms prg-combine 4
(get-input vignette-shape-m)
(get-input vignette-shape-p)
(get-input vignette-aspect-ratio)
(get-input vignette-shape-r))
(uniforms prg-combine 5
(get-input vignette-apply-base)
(get-input vignette-apply-max))
(uniforms prg-combine 6
(get-input cg-lift-r)
(get-input cg-lift-g)
(get-input cg-lift-b))
(uniforms prg-combine 7
(get-input cg-gain-r)
(get-input cg-gain-g)
(get-input cg-gain-b))
(uniforms prg-combine 8
(get-input cg-gamma-r)
(get-input cg-gamma-g)
(get-input cg-gamma-b))
(viewport 0 0 $vp-width $vp-height)
(fullscreen)
)
)
################################################################################
# FXAA
(fn fxaa-init ()
(program prg-fxaa "fxaa.f.glsl")
(pipeline pl-fxaa prg-fullscreen prg-fxaa)
(sampler smp-fxaa
(sampling linear)
(wrapping clamp-border))
(input fxaa-subpixel .5)
(input fxaa-et .166)
(input fxaa-et-min .0833)
(ui-overrides
(section "Post-processing"
(section "FXAA"
(float "Sub-pixel quality" fxaa-subpixel
(min 0) (max 1) (slider))
(float "Edge threshold" fxaa-et
(min .063) (max .333) (slider))
(float "Edge threshold min." fxaa-et-min
(min .0312) (max .0833) (decimals 4)
(slider))
)
)
)
)
(fn fxaa-render (in-image)
(profiling "FXAA"
(uniforms-i prg-fxaa 0 0)
(uniforms prg-fxaa 1
$vp-x $vp-y $vp-width $vp-height)
(main-output)
(viewport 0 0 $width $height)
(clear 0 0 0 0)
(use-pipeline pl-fxaa)
(use-texture 0 in-image smp-fxaa)
(uniforms prg-fxaa 2
(get-input fxaa-subpixel)
(get-input fxaa-et)
(get-input fxaa-et-min))
(viewport $vp-x $vp-y $vp-width $vp-height)
(fullscreen)
)
)
# vim: syntax=demo-srd

View file

@ -0,0 +1,20 @@
#version 450 core
//! type fragment
layout( location = 0 ) uniform sampler2D u_Input;
layout( location = 1 ) uniform int u_LOD;
layout( location = 2 ) uniform vec2 u_InputSize;
layout( location = 3 ) uniform vec3 u_FilterParams;
layout( location = 0 ) out vec3 color;
void main( void )
{
const vec3 c = textureLod( u_Input ,
gl_FragCoord.xy / u_InputSize.xy , u_LOD ).xyz;
color = max( vec3( 0 ) ,
( pow( c , vec3( u_FilterParams.x ) ) * u_FilterParams.y - c )
* u_FilterParams.z );
}

View file

@ -0,0 +1,29 @@
#version 450 core
//! type fragment
layout( location = 0 ) uniform sampler2D u_InputTexture;
layout( location = 1 ) uniform vec2 u_OutputSize;
layout( location = 2 ) uniform int u_SourceLOD;
layout( location = 3 ) uniform vec2 u_Direction;
layout( location = 4 ) uniform vec4 u_Weights;
layout( location = 0 ) out vec3 color;
void main( void )
{
vec2 tmp = gl_FragCoord.xy / u_OutputSize;
vec2 disp = u_Direction / u_OutputSize;
color = u_Weights.x * textureLod( u_InputTexture , tmp , u_SourceLOD ).xyz
+ u_Weights.y * (
textureLod( u_InputTexture , tmp + disp , u_SourceLOD ).xyz
+ textureLod( u_InputTexture , tmp - disp , u_SourceLOD ).xyz )
+ u_Weights.z * (
textureLod( u_InputTexture , tmp + disp * 2 , u_SourceLOD ).xyz
+ textureLod( u_InputTexture , tmp - disp * 2 , u_SourceLOD ).xyz )
+ u_Weights.w * (
textureLod( u_InputTexture , tmp + disp * 3 , u_SourceLOD ).xyz
+ textureLod( u_InputTexture , tmp - disp * 3 , u_SourceLOD ).xyz )
;
color /= u_Weights.x + 2 * ( u_Weights.y + u_Weights.z + u_Weights.w );
}

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

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,16 @@
//! type chunk
layout( location = 0 ) uniform float u_Time;
layout( location = 1 ) uniform vec2 u_Resolution;
layout( location = 2 ) uniform vec3 u_CamPos;
layout( location = 3 ) uniform vec3 u_LookAt;
layout( location = 4 ) uniform vec3 u_CamUp;
layout( location = 5 ) uniform float u_NearPlane;
layout( location = 6 ) uniform vec4 u_Render;
layout( location = 7 ) uniform float u_FogAttenuation;
layout( location = 8 ) uniform int u_Correction;
layout( location = 0 ) out vec3 o_Color;
layout( location = 1 ) out float o_Z;
//! include lib/raymarching.glsl

View file

@ -0,0 +1,59 @@
#version 450 core
//! type fragment
//! include lib/utils.glsl
layout( location = 0 ) uniform sampler2D u_MainInput;
layout( location = 1 ) uniform sampler2D u_BlurInput;
layout( location = 2 ) uniform vec2 u_OutputSize;
layout( location = 3 ) uniform vec2 u_Bloom;
layout( location = 4 ) uniform vec4 u_Vignette1;
layout( location = 5 ) uniform vec2 u_Vignette2;
layout( location = 6 ) uniform vec3 u_ColorLift;
layout( location = 7 ) uniform vec3 u_ColorGain;
layout( location = 8 ) uniform vec3 u_ColorGamma;
#define uVigShapeMul (u_Vignette1.x)
#define uVigShapePow (u_Vignette1.y)
#define uVigAspect (u_Vignette1.z)
#define uVigShapeReverse (u_Vignette1.w)
#define uVigApplyBase (u_Vignette2.x)
#define uVigApplyMax (u_Vignette2.y)
layout( location = 0 ) out vec4 o_Color;
void main( void )
{
vec2 pos = gl_FragCoord.xy / u_OutputSize;
float f = u_Bloom.x;
vec3 color = textureLod( u_MainInput , pos , 0 ).rgb;
// Bloom
vec3 bloom = vec3( 0 );
for ( int i = 0 ; i < 6 ; i ++ ) {
bloom += f * textureLod( u_BlurInput , pos , i ).rgb;
f = pow( f , u_Bloom.y + 1 );
}
color = color + bloom / 2.2;
// Color grading
color = ( color * ( u_ColorGain - u_ColorLift ) ) + u_ColorLift;
// Vignette
vec2 vShape = pow( abs( pos * 2 - 1 ) * uVigShapeMul ,
vec2( uVigShapePow ) );
float vignette = 1 - pow(
2 * ( uVigAspect * vShape.x + ( 1 - uVigAspect) * vShape.y ) ,
1 / ( uVigShapePow * uVigShapeReverse ) );
color *= uVigApplyBase + smoothstep(
uVigApplyBase , uVigApplyBase + uVigApplyMax , vignette );
// Reinhart
color = color / ( color + 1. );
// Gamma
color = pow( color , vec3( 1 ) / ( u_ColorGamma + 2.2 ) );
o_Color = vec4( color , M_Luminosity( color ) );
}

View file

@ -0,0 +1,13 @@
#version 450 core
//! type fragment
//! include chunks/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,20 @@
#version 450 core
//! type fragment
//! include chunks/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 );
}

View file

@ -0,0 +1,20 @@
#version 450 core
//! type fragment
layout( location = 0 ) uniform sampler2D u_InputTexture;
layout( location = 1 ) uniform vec2 u_OutputSize;
layout( location = 2 ) uniform int u_SourceLOD;
layout( location = 0 ) out vec3 color;
void main( void )
{
const vec2 c = gl_FragCoord.xy;
color = ( .25 * (
textureLod( u_InputTexture , ( c + vec2( .5 , .5 ) ) / u_OutputSize , u_SourceLOD )
+ textureLod( u_InputTexture , ( c + vec2(-.5 , .5 ) ) / u_OutputSize , u_SourceLOD )
+ textureLod( u_InputTexture , ( c + vec2( .5 ,-.5 ) ) / u_OutputSize , u_SourceLOD )
+ textureLod( u_InputTexture , ( c + vec2(-.5 ,-.5 ) ) / u_OutputSize , u_SourceLOD )
) ).rgb;
}

View file

@ -0,0 +1,14 @@
#version 450 core
//! type vertex
out gl_PerVertex {
vec4 gl_Position;
};
void main( void )
{
gl_Position = vec4(
vec2( gl_VertexID >> 1 , gl_VertexID & 1 ) * 2 - 1 ,
0 , 1 );
}

44
test/shaders/fxaa.f.glsl Normal file
View file

@ -0,0 +1,44 @@
#version 450 core
//! type fragment
#define FXAA_PC 1
#define FXAA_GLSL_130 1
#define FXAA_FAST_PIXEL_OFFSET 1
#define FXAA_GATHER4_ALPHA 1
#define FXAA_QUALITY__PRESET 39
//! include chunks/fxaa-3.11.glsl
layout( location = 0 ) uniform sampler2D u_Input;
layout( location = 1 ) uniform vec4 u_Viewport;
layout( location = 2 ) uniform vec3 u_Parameters;
#define uSPQuality (u_Parameters.x)
#define uEdgeThreshold (u_Parameters.y)
#define uEdgeThresholdMin (u_Parameters.z)
layout( location = 0 ) out vec4 o_Color;
void main( void )
{
vec2 coords = gl_FragCoord.xy - u_Viewport.xy;
o_Color = FxaaPixelShader(
( coords / u_Viewport.zw ) , // pos (vec2)
vec4( 0 ) , // unused (vec4)
u_Input , // input texture
u_Input , // (unused) input texture
u_Input , // (unused) input texture
vec2( 1 ) / u_Viewport.zw , // 1/resolution (vec2)
vec4( 0 ) , // unused (vec4)
vec4( 0 ) , // unused (vec4)
vec4( 0 ) , // unused (vec4)
uSPQuality , // sub-pixel quality (float in [0;1])
uEdgeThreshold , // edge threshold (float, [.063;.333], lower = better)
uEdgeThresholdMin , // edge threshold min (float, [0.0312;0.0833],
// higher = better)
0 , // unused (float)
0 , // unused (float)
0 , // unused (float)
vec4( 0 ) ); // unused (vec4)
}

43
test/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;
}

10
test/shaders/lib/fog.glsl Normal file
View file

@ -0,0 +1,10 @@
//! type library
vec3 FOG_Apply(
in vec3 color ,
in float dist ,
in float attenuation ,
in vec3 background )
{
return mix( background , color , exp( - dist * dist * attenuation ) );
}

View file

@ -0,0 +1,724 @@
////////////////////////////////////////////////////////////////
//
// HG_SDF
//
// GLSL LIBRARY FOR BUILDING SIGNED DISTANCE BOUNDS
//
// version 2016-01-10
//
// Check http://mercury.sexy/hg_sdf for updates
// and usage examples. Send feedback to spheretracing@mercury.sexy.
//
// Brought to you by MERCURY http://mercury.sexy
//
//
//
// Released as Creative Commons Attribution-NonCommercial (CC BY-NC)
//
////////////////////////////////////////////////////////////////
//
// How to use this:
//
// 1. Build some system to #include glsl files in each other.
// Include this one at the very start. Or just paste everywhere.
// 2. Build a sphere tracer. See those papers:
// * "Sphere Tracing" http://graphics.cs.illinois.edu/sites/default/files/zeno.pdf
// * "Enhanced Sphere Tracing" http://lgdv.cs.fau.de/get/2234
// The Raymnarching Toolbox Thread on pouet can be helpful as well
// http://www.pouet.net/topic.php?which=7931&page=1
// and contains links to many more resources.
// 3. Use the tools in this library to build your distance bound f().
// 4. ???
// 5. Win a compo.
//
// (6. Buy us a beer or a good vodka or something, if you like.)
//
////////////////////////////////////////////////////////////////
//
// Table of Contents:
//
// * Helper functions and macros
// * Collection of some primitive objects
// * Domain Manipulation operators
// * Object combination operators
//
////////////////////////////////////////////////////////////////
//
// Why use this?
//
// The point of this lib is that everything is structured according
// to patterns that we ended up using when building geometry.
// It makes it more easy to write code that is reusable and that somebody
// else can actually understand. Especially code on Shadertoy (which seems
// to be what everybody else is looking at for "inspiration") tends to be
// really ugly. So we were forced to do something about the situation and
// release this lib ;)
//
// Everything in here can probably be done in some better way.
// Please experiment. We'd love some feedback, especially if you
// use it in a scene production.
//
// The main patterns for building geometry this way are:
// * Stay Lipschitz continuous. That means: don't have any distance
// gradient larger than 1. Try to be as close to 1 as possible -
// Distances are euclidean distances, don't fudge around.
// Underestimating distances will happen. That's why calling
// it a "distance bound" is more correct. Don't ever multiply
// distances by some value to "fix" a Lipschitz continuity
// violation. The invariant is: each fSomething() function returns
// a correct distance bound.
// * Use very few primitives and combine them as building blocks
// using combine opertors that preserve the invariant.
// * Multiply objects by repeating the domain (space).
// If you are using a loop inside your distance function, you are
// probably doing it wrong (or you are building boring fractals).
// * At right-angle intersections between objects, build a new local
// coordinate system from the two distances to combine them in
// interesting ways.
// * As usual, there are always times when it is best to not follow
// specific patterns.
//
////////////////////////////////////////////////////////////////
//
// FAQ
//
// Q: Why is there no sphere tracing code in this lib?
// A: Because our system is way too complex and always changing.
// This is the constant part. Also we'd like everyone to
// explore for themselves.
//
// Q: This does not work when I paste it into Shadertoy!!!!
// A: Yes. It is GLSL, not GLSL ES. We like real OpenGL
// because it has way more features and is more likely
// to work compared to browser-based WebGL. We recommend
// you consider using OpenGL for your productions. Most
// of this can be ported easily though.
//
// Q: How do I material?
// A: We recommend something like this:
// Write a material ID, the distance and the local coordinate
// p into some global variables whenever an object's distance is
// smaller than the stored distance. Then, at the end, evaluate
// the material to get color, roughness, etc., and do the shading.
//
// Q: I found an error. Or I made some function that would fit in
// in this lib. Or I have some suggestion.
// A: Awesome! Drop us a mail at spheretracing@mercury.sexy.
//
// Q: Why is this not on github?
// A: Because we were too lazy. If we get bugged about it enough,
// we'll do it.
//
// Q: Your license sucks for me.
// A: Oh. What should we change it to?
//
// Q: I have trouble understanding what is going on with my distances.
// A: Some visualization of the distance field helps. Try drawing a
// plane that you can sweep through your scene with some color
// representation of the distance field at each point and/or iso
// lines at regular intervals. Visualizing the length of the
// gradient (or better: how much it deviates from being equal to 1)
// is immensely helpful for understanding which parts of the
// distance field are broken.
//
////////////////////////////////////////////////////////////////
//! type library
//! include lib/utils.glsl
////////////////////////////////////////////////////////////////
//
// PRIMITIVE DISTANCE FUNCTIONS
//
////////////////////////////////////////////////////////////////
//
// Conventions:
//
// Everything that is a distance function is called fSomething.
// The first argument is always a point in 2 or 3-space called <p>.
// Unless otherwise noted, (if the object has an intrinsic "up"
// side or direction) the y axis is "up" and the object is
// centered at the origin.
//
////////////////////////////////////////////////////////////////
float HG_fSphere(vec3 p, float r) {
return length(p) - r;
}
// Plane with normal n (n is normalized) at some distance from the origin
float HG_fPlane(vec3 p, vec3 n, float distanceFromOrigin) {
return dot(p, n) + distanceFromOrigin;
}
// Cheap Box: distance to corners is overestimated
float HG_fBoxCheap(vec3 p, vec3 b) { //cheap box
return M_VecMax(abs(p) - b);
}
// Box: correct distance to corners
float HG_fBox(vec3 p, vec3 b) {
vec3 d = abs(p) - b;
return length(max(d, vec3(0))) + M_VecMax(min(d, vec3(0)));
}
// Same as above, but in two dimensions (an endless box)
float HG_fBox2Cheap(vec2 p, vec2 b) {
return M_VecMax(abs(p)-b);
}
float HG_fBox2(vec2 p, vec2 b) {
vec2 d = abs(p) - b;
return length(max(d, vec2(0))) + M_VecMax(min(d, vec2(0)));
}
// Endless "corner"
float HG_fCorner (vec2 p) {
return length(max(p, vec2(0))) + M_VecMax(min(p, vec2(0)));
}
// Blobby ball object. You've probably seen it somewhere. This is not a correct distance bound, beware.
float HG_fBlob(vec3 p) {
p = abs(p);
if (p.x < max(p.y, p.z)) p = p.yzx;
if (p.x < max(p.y, p.z)) p = p.yzx;
float b = max(max(max(
dot(p, normalize(vec3(1, 1, 1))),
dot(p.xz, normalize(vec2(PHI+1, 1)))),
dot(p.yx, normalize(vec2(1, PHI)))),
dot(p.xz, normalize(vec2(1, PHI))));
float l = length(p);
return l - 1.5 - 0.2 * (1.5 / 2)* cos(min(sqrt(1.01 - b / l)*(PI / 0.25), PI));
}
// Cylinder standing upright on the xz plane
float HG_fCylinder(vec3 p, float r, float height) {
float d = length(p.xz) - r;
d = max(d, abs(p.y) - height);
return d;
}
// Capsule: A Cylinder with round caps on both sides
float HG_fCapsule(vec3 p, float r, float c) {
return mix(length(p.xz) - r, length(vec3(p.x, abs(p.y) - c, p.z)) - r, step(c, abs(p.y)));
}
// Distance to line segment between <a> and <b>, used for fCapsule() version 2below
float HG_fLineSegment(vec3 p, vec3 a, vec3 b) {
vec3 ab = b - a;
float t = M_Saturate(dot(p - a, ab) / dot(ab, ab));
return length((ab*t + a) - p);
}
// Capsule version 2: between two end points <a> and <b> with radius r
float HG_fCapsule(vec3 p, vec3 a, vec3 b, float r) {
return HG_fLineSegment(p, a, b) - r;
}
// Torus in the XZ-plane
float HG_fTorus(vec3 p, float smallRadius, float largeRadius) {
return length(vec2(length(p.xz) - largeRadius, p.y)) - smallRadius;
}
// A circle line. Can also be used to make a torus by subtracting the smaller radius of the torus.
float HG_fCircle(vec3 p, float r) {
float l = length(p.xz) - r;
return length(vec2(p.y, l));
}
// A circular disc with no thickness (i.e. a cylinder with no height).
// Subtract some value to make a flat disc with rounded edge.
float HG_fDisc(vec3 p, float r) {
float l = length(p.xz) - r;
return l < 0 ? abs(p.y) : length(vec2(p.y, l));
}
// Hexagonal prism, circumcircle variant
float HG_fHexagonCircumcircle(vec3 p, vec2 h) {
vec3 q = abs(p);
return max(q.y - h.y, max(q.x*sqrt(3)*0.5 + q.z*0.5, q.z) - h.x);
//this is mathematically equivalent to this line, but less efficient:
//return max(q.y - h.y, max(dot(vec2(cos(PI/3), sin(PI/3)), q.zx), q.z) - h.x);
}
// Hexagonal prism, incircle variant
float HG_fHexagonIncircle(vec3 p, vec2 h) {
return HG_fHexagonCircumcircle(p, vec2(h.x*sqrt(3)*0.5, h.y));
}
// Cone with correct distances to tip and base circle. Y is up, 0 is in the middle of the base.
float HG_fCone(vec3 p, float radius, float height) {
vec2 q = vec2(length(p.xz), p.y);
vec2 tip = q - vec2(0, height);
vec2 mantleDir = normalize(vec2(height, radius));
float mantle = dot(tip, mantleDir);
float d = max(mantle, -q.y);
float projected = dot(tip, vec2(mantleDir.y, -mantleDir.x));
// distance to tip
if ((q.y > height) && (projected < 0)) {
d = max(d, length(tip));
}
// distance to base ring
if ((q.x > radius) && (projected > length(vec2(height, radius)))) {
d = max(d, length(q - vec2(radius, 0)));
}
return d;
}
//
// "Generalized Distance Functions" by Akleman and Chen.
// see the Paper at https://www.viz.tamu.edu/faculty/ergun/research/implicitmodeling/papers/sm99.pdf
//
// This set of constants is used to construct a large variety of geometric primitives.
// Indices are shifted by 1 compared to the paper because we start counting at Zero.
// Some of those are slow whenever a driver decides to not unroll the loop,
// which seems to happen for fIcosahedron und fTruncatedIcosahedron on nvidia 350.12 at least.
// Specialized implementations can well be faster in all cases.
//
const vec3 HG_GDFVectors[19] = vec3[](
normalize(vec3(1, 0, 0)),
normalize(vec3(0, 1, 0)),
normalize(vec3(0, 0, 1)),
normalize(vec3(1, 1, 1 )),
normalize(vec3(-1, 1, 1)),
normalize(vec3(1, -1, 1)),
normalize(vec3(1, 1, -1)),
normalize(vec3(0, 1, PHI+1)),
normalize(vec3(0, -1, PHI+1)),
normalize(vec3(PHI+1, 0, 1)),
normalize(vec3(-PHI-1, 0, 1)),
normalize(vec3(1, PHI+1, 0)),
normalize(vec3(-1, PHI+1, 0)),
normalize(vec3(0, PHI, 1)),
normalize(vec3(0, -PHI, 1)),
normalize(vec3(1, 0, PHI)),
normalize(vec3(-1, 0, PHI)),
normalize(vec3(PHI, 1, 0)),
normalize(vec3(-PHI, 1, 0))
);
// Version with variable exponent.
// This is slow and does not produce correct distances, but allows for bulging of objects.
float HG_fGDF(vec3 p, float r, float e, int begin, int end) {
float d = 0;
for (int i = begin; i <= end; ++i)
d += pow(abs(dot(p, HG_GDFVectors[i])), e);
return pow(d, 1/e) - r;
}
// Version with without exponent, creates objects with sharp edges and flat faces
float HG_fGDF(vec3 p, float r, int begin, int end) {
float d = 0;
for (int i = begin; i <= end; ++i)
d = max(d, abs(dot(p, HG_GDFVectors[i])));
return d - r;
}
// Primitives follow:
float HG_fOctahedron(vec3 p, float r, float e) {
return HG_fGDF(p, r, e, 3, 6);
}
float HG_fDodecahedron(vec3 p, float r, float e) {
return HG_fGDF(p, r, e, 13, 18);
}
float HG_fIcosahedron(vec3 p, float r, float e) {
return HG_fGDF(p, r, e, 3, 12);
}
float HG_fTruncatedOctahedron(vec3 p, float r, float e) {
return HG_fGDF(p, r, e, 0, 6);
}
float HG_fTruncatedIcosahedron(vec3 p, float r, float e) {
return HG_fGDF(p, r, e, 3, 18);
}
float HG_fOctahedron(vec3 p, float r) {
return HG_fGDF(p, r, 3, 6);
}
float HG_fDodecahedron(vec3 p, float r) {
return HG_fGDF(p, r, 13, 18);
}
float HG_fIcosahedron(vec3 p, float r) {
return HG_fGDF(p, r, 3, 12);
}
float HG_fTruncatedOctahedron(vec3 p, float r) {
return HG_fGDF(p, r, 0, 6);
}
float HG_fTruncatedIcosahedron(vec3 p, float r) {
return HG_fGDF(p, r, 3, 18);
}
////////////////////////////////////////////////////////////////
//
// DOMAIN MANIPULATION OPERATORS
//
////////////////////////////////////////////////////////////////
//
// Conventions:
//
// Everything that modifies the domain is named pSomething.
//
// Many operate only on a subset of the three dimensions. For those,
// you must choose the dimensions that you want manipulated
// by supplying e.g. <p.x> or <p.zx>
//
// <inout p> is always the first argument and modified in place.
//
// Many of the operators partition space into cells. An identifier
// or cell index is returned, if possible. This return value is
// intended to be optionally used e.g. as a random seed to change
// parameters of the distance functions inside the cells.
//
// Unless stated otherwise, for cell index 0, <p> is unchanged and cells
// are centered on the origin so objects don't have to be moved to fit.
//
//
////////////////////////////////////////////////////////////////
// Rotate around a coordinate axis (i.e. in a plane perpendicular to that axis) by angle <a>.
// Read like this: R(p.xz, a) rotates "x towards z".
// This is fast if <a> is a compile-time constant and slower (but still practical) if not.
void HG_pR(inout vec2 p, float a) {
p = cos(a)*p + sin(a)*vec2(p.y, -p.x);
}
// Shortcut for 45-degrees rotation
void HG_pR45(inout vec2 p) {
p = (p + vec2(p.y, -p.x))*sqrt(0.5);
}
// Repeat space along one axis. Use like this to repeat along the x axis:
// <float cell = HG_pMod1(p.x,5);> - using the return value is optional.
float HG_pMod1(inout float p, float size) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = mod(p + halfsize, size) - halfsize;
return c;
}
// Same, but mirror every second cell so they match at the boundaries
float HG_pModMirror1(inout float p, float size) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = mod(p + halfsize,size) - halfsize;
p *= mod(c, 2.0)*2 - 1;
return c;
}
// Repeat the domain only in positive direction. Everything in the negative half-space is unchanged.
float HG_pModSingle1(inout float p, float size) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
if (p >= 0)
p = mod(p + halfsize, size) - halfsize;
return c;
}
// Repeat only a few times: from indices <start> to <stop> (similar to above, but more flexible)
float HG_pModInterval1(inout float p, float size, float start, float stop) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = mod(p+halfsize, size) - halfsize;
if (c > stop) { //yes, this might not be the best thing numerically.
p += size*(c - stop);
c = stop;
}
if (c <start) {
p += size*(c - start);
c = start;
}
return c;
}
// Repeat around the origin by a fixed angle.
// For easier use, num of repetitions is use to specify the angle.
float HG_pModPolar(inout vec2 p, float repetitions) {
float angle = 2*PI/repetitions;
float a = atan(p.y, p.x) + angle/2.;
float r = length(p);
float c = floor(a/angle);
a = mod(a,angle) - angle/2.;
p = vec2(cos(a), sin(a))*r;
// For an odd number of repetitions, fix cell index of the cell in -x direction
// (cell index would be e.g. -5 and 5 in the two halves of the cell):
if (abs(c) >= (repetitions/2)) c = abs(c);
return c;
}
// Repeat in two dimensions
vec2 HG_pMod2(inout vec2 p, vec2 size) {
vec2 c = floor((p + size*0.5)/size);
p = mod(p + size*0.5,size) - size*0.5;
return c;
}
// Same, but mirror every second cell so all boundaries match
vec2 HG_pModMirror2(inout vec2 p, vec2 size) {
vec2 halfsize = size*0.5;
vec2 c = floor((p + halfsize)/size);
p = mod(p + halfsize, size) - halfsize;
p *= mod(c,vec2(2))*2 - vec2(1);
return c;
}
// Same, but mirror every second cell at the diagonal as well
vec2 HG_pModGrid2(inout vec2 p, vec2 size) {
vec2 c = floor((p + size*0.5)/size);
p = mod(p + size*0.5, size) - size*0.5;
p *= mod(c,vec2(2))*2 - vec2(1);
p -= size/2;
if (p.x > p.y) p.xy = p.yx;
return floor(c/2);
}
// Repeat in three dimensions
vec3 HG_pMod3(inout vec3 p, vec3 size) {
vec3 c = floor((p + size*0.5)/size);
p = mod(p + size*0.5, size) - size*0.5;
return c;
}
// Mirror at an axis-aligned plane which is at a specified distance <dist> from the origin.
float HG_pMirror (inout float p, float dist) {
float s = M_Sign(p);
p = abs(p)-dist;
return s;
}
// Mirror in both dimensions and at the diagonal, yielding one eighth of the space.
// translate by dist before mirroring.
vec2 HG_pMirrorOctant (inout vec2 p, vec2 dist) {
vec2 s = M_Sign(p);
HG_pMirror(p.x, dist.x);
HG_pMirror(p.y, dist.y);
if (p.y > p.x)
p.xy = p.yx;
return s;
}
// Reflect space at a plane
float HG_pReflect(inout vec3 p, vec3 planeNormal, float offset) {
float t = dot(p, planeNormal)+offset;
if (t < 0) {
p = p - (2*t)*planeNormal;
}
return M_Sign(t);
}
////////////////////////////////////////////////////////////////
//
// OBJECT COMBINATION OPERATORS
//
////////////////////////////////////////////////////////////////
//
// We usually need the following boolean operators to combine two objects:
// Union: OR(a,b)
// Intersection: AND(a,b)
// Difference: AND(a,!b)
// (a and b being the distances to the objects).
//
// The trivial implementations are min(a,b) for union, max(a,b) for intersection
// and max(a,-b) for difference. To combine objects in more interesting ways to
// produce rounded edges, chamfers, stairs, etc. instead of plain sharp edges we
// can use combination operators. It is common to use some kind of "smooth minimum"
// instead of min(), but we don't like that because it does not preserve Lipschitz
// continuity in many cases.
//
// Naming convention: since they return a distance, they are called fOpSomething.
// The different flavours usually implement all the boolean operators above
// and are called fOpUnionRound, fOpIntersectionRound, etc.
//
// The basic idea: Assume the object surfaces intersect at a right angle. The two
// distances <a> and <b> constitute a new local two-dimensional coordinate system
// with the actual intersection as the origin. In this coordinate system, we can
// evaluate any 2D distance function we want in order to shape the edge.
//
// The operators below are just those that we found useful or interesting and should
// be seen as examples. There are infinitely more possible operators.
//
// They are designed to actually produce correct distances or distance bounds, unlike
// popular "smooth minimum" operators, on the condition that the gradients of the two
// SDFs are at right angles. When they are off by more than 30 degrees or so, the
// Lipschitz condition will no longer hold (i.e. you might get artifacts). The worst
// case is parallel surfaces that are close to each other.
//
// Most have a float argument <r> to specify the radius of the feature they represent.
// This should be much smaller than the object size.
//
// Some of them have checks like "if ((-a < r) && (-b < r))" that restrict
// their influence (and computation cost) to a certain area. You might
// want to lift that restriction or enforce it. We have left it as comments
// in some cases.
//
// usage example:
//
// float fTwoBoxes(vec3 p) {
// float box0 = fBox(p, vec3(1));
// float box1 = fBox(p-vec3(1), vec3(1));
// return fOpUnionChamfer(box0, box1, 0.2);
// }
//
////////////////////////////////////////////////////////////////
// The "Chamfer" flavour makes a 45-degree chamfered edge (the diagonal of a square of size <r>):
float HG_fOpUnionChamfer(float a, float b, float r) {
return min(min(a, b), (a - r + b)*sqrt(0.5));
}
// Intersection has to deal with what is normally the inside of the resulting object
// when using union, which we normally don't care about too much. Thus, intersection
// implementations sometimes differ from union implementations.
float HG_fOpIntersectionChamfer(float a, float b, float r) {
return max(max(a, b), (a + r + b)*sqrt(0.5));
}
// Difference can be built from Intersection or Union:
float HG_fOpDifferenceChamfer (float a, float b, float r) {
return HG_fOpIntersectionChamfer(a, -b, r);
}
// The "Round" variant uses a quarter-circle to join the two objects smoothly:
float HG_fOpUnionRound(float a, float b, float r) {
vec2 u = max(vec2(r - a,r - b), vec2(0));
return max(r, min (a, b)) - length(u);
}
float HG_fOpIntersectionRound(float a, float b, float r) {
vec2 u = max(vec2(r + a,r + b), vec2(0));
return min(-r, max (a, b)) + length(u);
}
float HG_fOpDifferenceRound (float a, float b, float r) {
return HG_fOpIntersectionRound(a, -b, r);
}
// The "Columns" flavour makes n-1 circular columns at a 45 degree angle:
float HG_fOpUnionColumns(float a, float b, float r, float n) {
if ((a < r) && (b < r)) {
vec2 p = vec2(a, b);
float columnradius = r*sqrt(2)/((n-1)*2+sqrt(2));
HG_pR45(p);
p.x -= sqrt(2)/2*r;
p.x += columnradius*sqrt(2);
if (mod(n,2) == 1) {
p.y += columnradius;
}
// At this point, we have turned 45 degrees and moved at a point on the
// diagonal that we want to place the columns on.
// Now, repeat the domain along this direction and place a circle.
HG_pMod1(p.y, columnradius*2);
float result = length(p) - columnradius;
result = min(result, p.x);
result = min(result, a);
return min(result, b);
} else {
return min(a, b);
}
}
float HG_fOpDifferenceColumns(float a, float b, float r, float n) {
a = -a;
float m = min(a, b);
//avoid the expensive computation where not needed (produces discontinuity though)
if ((a < r) && (b < r)) {
vec2 p = vec2(a, b);
float columnradius = r*sqrt(2)/n/2.0;
columnradius = r*sqrt(2)/((n-1)*2+sqrt(2));
HG_pR45(p);
p.y += columnradius;
p.x -= sqrt(2)/2*r;
p.x += -columnradius*sqrt(2)/2;
if (mod(n,2) == 1) {
p.y += columnradius;
}
HG_pMod1(p.y,columnradius*2);
float result = -length(p) + columnradius;
result = max(result, p.x);
result = min(result, a);
return -min(result, b);
} else {
return -m;
}
}
float fOpIntersectionColumns(float a, float b, float r, float n) {
return HG_fOpDifferenceColumns(a,-b,r, n);
}
// The "Stairs" flavour produces n-1 steps of a staircase:
// much less stupid version by paniq
float HG_fOpUnionStairs(float a, float b, float r, float n) {
float s = r/n;
float u = b-r;
return min(min(a,b), 0.5 * (u + a + abs ((mod (u - a + s, 2 * s)) - s)));
}
// We can just call Union since stairs are symmetric.
float fOpIntersectionStairs(float a, float b, float r, float n) {
return -HG_fOpUnionStairs(-a, -b, r, n);
}
float fOpDifferenceStairs(float a, float b, float r, float n) {
return -HG_fOpUnionStairs(-a, b, r, n);
}
// Similar to fOpUnionRound, but more lipschitz-y at acute angles
// (and less so at 90 degrees). Useful when fudging around too much
// by MediaMolecule, from Alex Evans' siggraph slides
float HG_fOpUnionSoft(float a, float b, float r) {
float e = max(r - abs(a - b), 0);
return min(a, b) - e*e*0.25/r;
}
// produces a cylindical pipe that runs along the intersection.
// No objects remain, only the pipe. This is not a boolean operator.
float HG_fOpPipe(float a, float b, float r) {
return length(vec2(a, b)) - r;
}
// first object gets a v-shaped engraving where it intersect the second
float HG_fOpEngrave(float a, float b, float r) {
return max(a, (a + r - abs(b))*sqrt(0.5));
}
// first object gets a capenter-style groove cut out
float HG_fOpGroove(float a, float b, float ra, float rb) {
return max(a, min(a + ra, rb - abs(b)));
}
// first object gets a capenter-style tongue attached
float HG_fOpTongue(float a, float b, float ra, float rb) {
return min(a, max(a - ra, abs(b) - rb));
}

View file

@ -0,0 +1,146 @@
//! type library
//! include lib/utils.glsl
vec2 RM_Map( in vec3 pos );
// -----------------------------------------------------------------------------
vec3 RM_GetNormal(
in vec3 pos )
{
const vec2 e = vec2( 1 , -1 ) * .0001;
const vec4 f = vec4(
RM_Map( pos + e.xyy ).x ,
RM_Map( pos + e.yyx ).x ,
RM_Map( pos + e.yxy ).x ,
RM_Map( pos + e.xxx ).x );
return normalize( f.x * e.xyy
+ f.y * e.yyx
+ f.z * e.yxy
+ f.w * e.xxx );
}
// -----------------------------------------------------------------------------
vec3 RM_Basic(
in vec3 origin ,
in vec3 direction ,
in int steps ,
in float factor ,
in float epsilon ,
in float dMin ,
in float dMax )
{
int i = 0;
float dist = dMin , mat = -1;
for ( ; i < steps ; ++ i ) {
vec2 res = RM_Map( origin + direction * dist );
if ( abs( res.x ) < epsilon || dist > dMax ) {
break;
}
dist += res.x * factor;
mat = res.y;
}
return vec3( dist , dist >= dMax ? -1 : mat , i );
}
vec3 RM_Advanced(
in vec3 origin ,
in vec3 direction ,
in int steps ,
in float factor ,
in float epsilon ,
in float dMin ,
in float dMax )
{
int i = 0;
float dist = dMin ,
omega = factor ,
cError = 1 / 0. ,
cDist = dMin ,
pRad = 0 ,
sLen = 0;
for ( ; i < steps ; ++ i ) {
vec2 res = RM_Map( origin + direction * dist );
float rad = abs( res.x );
bool sorFail = omega > 1 && ( rad + pRad ) < sLen;
if ( sorFail ) {
sLen -= omega * sLen;
omega = 1;
} else {
sLen = res.x * omega;
}
pRad = rad;
float error = rad / dist;
if ( !sorFail && error < cError ) {
cError = error;
cDist = dist;
}
if ( !sorFail && error < epsilon || dist > dMax ) {
break;
}
dist += sLen;
}
if ( dist <= dMax && cError <= epsilon ) {
return vec3( cDist , RM_Map( origin + direction * cDist ).y , i );
}
return vec3( cDist , -1 , steps );
}
float RM_TanPhi(
in vec2 uv ,
in float pixelRadius )
{
// 1 / tan( FOV * PI / 360 )
// u_NearPlane = 2 * tan( PI * ( 180 - FOV ) / 360 );
// atan( u_NearPlane / 2 ) = PI * ( 180 - FOV ) / 360
// = PI / 2 - PI * FOV / 360
// atan( u_NearPlane / 2 ) - PI / 2 = - PI * FOV / 360
// PI / 2 - atan( u_NearPlane / 2 ) = PI * FOV / 360
// ...
// ...
// or we could just pass it as a fucking uniform
const float hfov = ( PI * .5 - atan( u_NearPlane * .5 ) ) * .5;
vec2 e = ( abs( uv.x ) > abs( uv.y ) )
? vec2( uv.x < 0 ? -1 : 1 , 0 )
: vec2( 0 , uv.y < 0 ? -1 : 1 );
vec3 d = normalize( vec3( uv , -1 / tan( hfov ) ) ) ,
a = vec3( ( e + e.yx ) * pixelRadius + uv ,
-1 / tan( hfov ) ) ,
b = vec3( ( e - e.yx ) * pixelRadius + uv ,
-1 / tan( hfov ) ) ,
p2ca = a / dot( a , d ) ,
p2cb = a / dot( b , d ) ,
diff = p2cb - p2ca ,
de = mix( p2ca , p2cb , M_Saturate(
dot( d - p2ca , diff ) / M_LengthSqr( diff ) ) );
float sinPhiSquared = M_LengthSqr( cross( d , normalize( de ) ) );
return sqrt( sinPhiSquared / ( 1 - sinPhiSquared ) );
}
vec3 RM_ReduceDiscontinuity(
in vec3 origin ,
in vec3 rayDir ,
in float hitDist ,
in float tanPhi ,
in int iterations )
{
vec3 hitPos = origin + hitDist * rayDir;
for ( int i = 0 ; i < iterations ; i ++ ) {
hitPos = hitPos + rayDir * (
RM_Map( hitPos ).x - tanPhi * length( origin - hitPos )
);
}
return hitPos;
}

View file

@ -0,0 +1,23 @@
//! type library
struct T_BPMaterial
{
vec3 cAlbedo, cSpecular;
float specPower , ambient;
};
vec3 BP_Shade(
in T_BPMaterial material ,
in vec3 rayDir ,
in vec3 normal ,
in vec3 lightDir )
{
const vec3 halfVec = normalize( rayDir + lightDir );
const float nDotL = dot( normal , lightDir ) ,
nDotH = dot( normal , halfVec ) ,
si = pow( clamp( nDotH , 0 , 1 ) , material.specPower ) ,
di = material.ambient + ( 1 - material.ambient )
* clamp( nDotL , 0 , 1 );
return mix( material.cAlbedo * di , material.cSpecular , si );
}

View file

@ -0,0 +1,144 @@
//! type library
//! include lib/utils.glsl
struct T_PBRMaterial
{
vec3 cAlbedo;
float roughness;
float metallic;
float subsurface;
float anisotropy;
float specular; // Specular strengh for non-metals
float specularTint; // Albedo color% in specular tint (non-metals)
};
struct T_PBRPrecomputed
{
float nDotV , ax , ay , fv , vgs;
vec3 tangent , bitangent , cSpecular;
};
// -----------------------------------------------------------------------------
float PBR_SchlickFresnel(
in float u )
{
const float m = 1 - u , m2 = M_Sqr( m );
return M_Sqr( m2 ) * m;
}
float PBR_GTR2Aniso(
in float nDotH ,
in float hDotX ,
in float hDotY ,
in float ax ,
in float ay )
{
float x = hDotX / ax , y = hDotY / ay ,
p = x * x + y * y + nDotH * nDotH;
return 1 / ( ax * ay * p * p );
}
float PBR_SmithGGXAniso(
in float nDotV ,
in float vDotX ,
in float vDotY ,
in float ax ,
in float ay )
{
float x = vDotX * ax , y = vDotY * ay;
return 1 / ( nDotV + sqrt( x * x + y * y + nDotV * nDotV ) );
}
T_PBRPrecomputed PBR_Precompute(
in T_PBRMaterial material ,
in vec3 camDir ,
in vec3 normal )
{
T_PBRPrecomputed rv;
rv.nDotV = abs( dot( normal , camDir ) ) + 1e-5;
rv.fv = PBR_SchlickFresnel( rv.nDotV );
float tDir = step( .99 , abs( normal.z ) ) ,
aspect = sqrt( 1 - material.anisotropy * .9 ) ,
rsqr = M_Sqr( material.roughness );
vec3 tUp = mix( vec3( 0 , 0 , 1 ) , vec3( 1 , 0 , 0 ) , tDir ) ,
tint = M_NormalizeColor( material.cAlbedo );
rv.tangent = normalize( cross( tUp , normal ) );
rv.bitangent = normalize( cross( normal , rv.tangent ) );
rv.cSpecular = mix(
material.specular * .08 * mix(
vec3( 1 ) , tint , material.specularTint ) ,
material.cAlbedo , material.metallic );
rv.ax = max( .001, rsqr / aspect );
rv.ay = max( .001, rsqr * aspect );
rv.vgs = PBR_SmithGGXAniso( rv.nDotV ,
dot( camDir , rv.tangent ) , dot( camDir , rv.bitangent ) ,
rv.ax , rv.ay );
return rv;
}
vec3 PBR_Shade(
in T_PBRMaterial material ,
in T_PBRPrecomputed pre ,
in vec3 camDir ,
in vec3 normal ,
in vec3 lightDir )
{
float nDotL = dot( normal , lightDir );
if ( nDotL < 0 ) {
return vec3( 0 );
}
vec3 halfVec = normalize( lightDir + camDir ) ;
float nDotH = M_Saturate( dot( normal , halfVec ) ) ,
lDotH = M_Saturate( dot( lightDir , halfVec ) ) ,
// Diffuse fresnel - go from 1 at normal incidence to .5 at grazing
// and mix in diffuse retro-reflection based on roughness
FL = PBR_SchlickFresnel( nDotL ) ,
Fd90 = ( 0.5 + 2 * lDotH * lDotH ) * material.roughness ,
Fd = ( 1 + ( Fd90 - 1 ) * FL )
* ( 1 + ( Fd90 - 1 ) * pre.fv )
* ( 1 - 0.51 * material.roughness / 1.51 ) ,
// Based on Hanrahan-Krueger brdf approximation of isotropic bssrdf
// 1.25 scale is used to (roughly) preserve albedo
// Fss90 used to "flatten" retroreflection based on roughness
Fss90 = lDotH * lDotH * material.roughness ,
Fss = mix( 1 , Fss90 , FL ) * mix( 1 , Fss90 , pre.fv ) ,
ss = 1.25 * ( Fss * ( 1 / ( nDotL + pre.nDotV ) - .5 ) + .5 ) ,
// Specular
Ds = PBR_GTR2Aniso( nDotH , dot( halfVec , pre.tangent ) ,
dot( halfVec , pre.bitangent ) ,
pre.ax , pre.ay ) ,
FH = PBR_SchlickFresnel( lDotH ) ,
Gs = PBR_SmithGGXAniso( nDotL , dot( lightDir , pre.tangent ) ,
dot( lightDir , pre.bitangent ) ,
pre.ax , pre.ay ) * pre.vgs;
vec3 Fs = mix( pre.cSpecular , vec3(1) , FH );
return nDotL * ( mix( Fd , ss , material.subsurface )
* material.cAlbedo
* ( 1 - material.metallic )
+ Gs * Fs * Ds ) / PI;
}
vec3 PBR_Shade(
in T_PBRMaterial material ,
in vec3 camDir ,
in vec3 normal ,
in vec3 lightDir )
{
return PBR_Shade(
material ,
PBR_Precompute( material , camDir , normal ) ,
camDir , normal , lightDir );
}

114
test/shaders/lib/utils.glsl Normal file
View file

@ -0,0 +1,114 @@
//! type library
const float PI = 3.1415926535;
const float TAU = 2 * PI;
const float PHI = sqrt( 5 ) * .5 + .5;
// -----------------------------------------------------------------------------
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);
}
// -----------------------------------------------------------------------------
float M_Luminosity(
in vec3 color )
{
return dot( color , vec3( .299 , .587 , .114 ) );
}
vec3 M_NormalizeColor(
in vec3 color )
{
const float l = M_Luminosity( color );
return l > 0 ? ( color / l ) : vec3( 1 );
}
// -----------------------------------------------------------------------------
float M_Saturate(
in float x )
{
return clamp( x , 0. , 1. );
}
float M_LengthSqr(
in vec3 x )
{
return dot( x , x );
}
float M_Sqr(
in float x )
{
return x * x;
}
vec2 M_Sqr(
in vec2 x )
{
return x * x;
}
vec3 M_Sqr(
in vec3 x )
{
return x * x;
}
float M_Sign(
in float x )
{
return ( x < 0 ) ? -1 : 1;
}
vec2 M_Sign(
in vec2 v )
{
return vec2( M_Sign( v.x ) , M_Sign( v.y ) );
}
float M_VecMax(
in vec2 v )
{
return max( v.x , v.y );
}
float M_VecMax(
in vec3 v )
{
return max( M_VecMax( v.xy ) , v.z );
}
float M_VecMax(
in vec4 v )
{
return max( M_VecMax( v.xy ) , M_VecMax( v.zw ) );
}
float M_VecMin(
in vec2 v )
{
return min( v.x , v.y );
}
float M_VecMin(
in vec3 v )
{
return min( M_VecMin( v.xy ) , v.z );
}
float M_VecMin(
in vec4 v )
{
return min( M_VecMin( v.xy ) , M_VecMin( v.zw ) );
}

173
test/shaders/scene.f.glsl Normal file
View file

@ -0,0 +1,173 @@
#version 450 core
//! type fragment
//! include chunks/raymarcher.glsl
//! include lib/hg_sdf.glsl
//! include lib/shading-pbr.glsl
//! include lib/shading-blinnphong.glsl
//! include lib/fog.glsl
T_BPMaterial BPMaterials[1] = {
{ vec3( .95 , .8 , 1 ) * .2 , vec3( 10 ) , 800 , 0 }
};
T_PBRMaterial PBRMaterials[1] = {
{
// Albedo color
vec3( .9 , 1 , .9 ) ,
// Roughness , metallic , subsurface , anisotropy
.15 , .5 , .9 , .9 ,
// Specular strength / tint%
.5 , .5
}
};
vec3 Glow[1] = {
vec3( .95 , .8 , 1 ) * 5
};
void mapMaterial(
in int matIndex ,
out int type ,
out int tIndex ,
out int glowIndex )
{
if ( matIndex == 0 ) {
type = 1;
glowIndex = -1;
} else {
type = 0;
glowIndex = 0;
}
tIndex = 0;
}
vec2 RM_Map( vec3 pos )
{
vec3 q = pos;
HG_pMod2( q.xy , vec2( 8. ) );
HG_pR( q.xz , 1 );
return vec2( HG_fOpUnionStairs(
HG_fBox( q , vec3( 2.3 ) ) ,
HG_fSphere( q , 2.6 ) ,
1. , 4 ) ,
step( 0. , -HG_fSphere( pos , 3 ) ) );
}
vec3 pointlight(
in T_PBRMaterial material ,
in T_PBRPrecomputed pre ,
in vec3 hitPos ,
in vec3 rayDir ,
in vec3 normal ,
in vec3 lightPos ,
in vec3 lightColor ,
in float lightIntensity )
{
vec3 lightRay = lightPos - hitPos;
float lPwr = lightIntensity / max( .001 , dot( lightRay , lightRay ) );
if ( lPwr < .00005 ) {
return vec3( 0 );
}
return lightColor * lPwr * PBR_Shade( material , pre ,
-rayDir , normal , normalize( lightRay ) );
}
vec3 pointlight(
in T_BPMaterial material ,
in vec3 hitPos ,
in vec3 rayDir ,
in vec3 normal ,
in vec3 lightPos ,
in vec3 lightColor ,
in float lightIntensity )
{
vec3 lightRay = lightPos - hitPos;
float lPwr = lightIntensity / max( .1 , dot( lightRay , lightRay ) );
if ( lPwr < .00005 ) {
return vec3( 0 );
}
return lightColor * lPwr * BP_Shade(
material , -rayDir , normal , lightRay );
}
void main( )
{
vec2 uv = ( gl_FragCoord.xy / u_Resolution ) * 2 - 1;
vec3 camDir = normalize( u_LookAt - u_CamPos );
vec3 side = normalize( cross( u_CamUp , camDir ) );
vec3 up = normalize( cross( camDir , side ) );
vec3 rayDir = normalize( camDir * u_NearPlane
+ uv.x * side * u_Resolution.x / u_Resolution.y
+ uv.y * up );
float tanPhi = RM_TanPhi( uv , u_Render.z );
vec3 r = RM_Advanced( u_CamPos , rayDir ,
int( u_Render.x ) , u_Render.y ,
tanPhi , .01 , u_Render.w );
const vec3 background = vec3( .005 );
vec3 bc = background;
if ( r.y >= 0. ) {
const vec3 hitPos = RM_ReduceDiscontinuity(
u_CamPos , rayDir , r.x ,
tanPhi , u_Correction );
const vec3 normal = RM_GetNormal( hitPos );
const int midx = int( r.y );
const vec3 sunlightDir = normalize( vec3( 0 , 1 , 1 ) );
const vec3 sunlightColor = vec3( 1 , 1 , .99 ) * .05;
const vec3 pl1Pos = vec3( 3 , 3 , 5 );
const vec3 pl1Color = vec3( 1 , .7 , .8 );
const float pl1Power = 20;
const vec3 pl2Pos = vec3( -3 , -3 , 5 );
const vec3 pl2Color = vec3( .6 , 1 , .8 );
const float pl2Power = 20;
// Remap materials through mapMaterials
int mtype , mtidx , glowidx;
mapMaterial( midx , mtype , mtidx , glowidx );
if ( mtype == 0 ) {
T_BPMaterial mat = BPMaterials[ mtidx ];
bc = sunlightColor * BP_Shade( mat ,
-rayDir , normal , sunlightDir );
bc += pointlight( mat ,
hitPos , rayDir , normal ,
pl1Pos , pl1Color , pl1Power );
bc += pointlight( mat ,
hitPos , rayDir , normal ,
pl2Pos , pl2Color , pl2Power );
} else {
T_PBRMaterial mat = PBRMaterials[ mtidx ];
T_PBRPrecomputed pre = PBR_Precompute(
mat , -rayDir , normal );
bc = sunlightColor * PBR_Shade( mat , pre ,
-rayDir , normal , sunlightDir );
bc += pointlight( mat , pre ,
hitPos , rayDir , normal ,
pl1Pos , pl1Color , pl1Power );
bc += pointlight( mat , pre ,
hitPos , rayDir , normal ,
pl2Pos , pl2Color , pl2Power );
}
if ( glowidx >= 0 ) {
bc += Glow[ glowidx ];
}
bc = FOG_Apply( bc , length( u_CamPos - hitPos ) ,
u_FogAttenuation , background );
o_Z = r.x;
} else {
o_Z = u_Render.w;
}
o_Color = bc;
}