From dacb0ee367a6f3885ed214808e6f7fa2fde9c020 Mon Sep 17 00:00:00 2001 From: hunterk Date: Tue, 6 Aug 2019 12:57:27 -0500 Subject: [PATCH] add motion_interpolation shaders and preset --- .../motion_interpolation.slangp | 8 ++ .../motion_interpolation_pass0.slang | 83 +++++++++++++++++++ .../motion_interpolation_pass1.slang | 57 +++++++++++++ .../motion_interpolation_pass2.slang | 57 +++++++++++++ .../motion_interpolation_pass3.slang | 69 +++++++++++++++ 5 files changed, 274 insertions(+) create mode 100644 motion-interpolation/motion_interpolation.slangp create mode 100644 motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass0.slang create mode 100644 motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass1.slang create mode 100644 motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass2.slang create mode 100644 motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass3.slang diff --git a/motion-interpolation/motion_interpolation.slangp b/motion-interpolation/motion_interpolation.slangp new file mode 100644 index 0000000..7f34af3 --- /dev/null +++ b/motion-interpolation/motion_interpolation.slangp @@ -0,0 +1,8 @@ +shaders = 2 + +shader0 = shaders/motion_interpolation/motion_interpolation_pass0.slang +alias0 = Pass0 +filter_linear0 = true + +shader1 = shaders/motion_interpolation/motion_interpolation_pass3.slang +filter_linear1 = true diff --git a/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass0.slang b/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass0.slang new file mode 100644 index 0000000..f3c6382 --- /dev/null +++ b/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass0.slang @@ -0,0 +1,83 @@ +#version 450 + +// Video Motion Interpolation +// based on Drudgerist's shadertoy: +// https://www.shadertoy.com/view/MtVfRz +// which is, in turn, based on bodhid's V+ +// https://github.com/bodhid/Vplus +// This pass generates motion vectors based on the current and previous frames + +layout(push_constant) uniform Push +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; +} params; + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; +layout(set = 0, binding = 3) uniform sampler2D OriginalHistory1; + +#define iChannel0 OriginalHistory1 +#define iChannel1 Source +#pragma name Pass0 + +float PixV(sampler2D Target, vec2 Shift, vec2 iuv, vec2 texelSize) +{ + vec4 c = texture(Target,iuv); + c += texture(Target, iuv + Shift+ (vec2(1, 0) * texelSize)); + c += texture(Target, iuv + Shift + (vec2(0, 1) * texelSize)); + c += texture(Target, iuv + Shift + (vec2(-1, 0) * texelSize)); + c += texture(Target, iuv + Shift + (vec2(0, -1) * texelSize)); + return c.r + c.g + c.b; +} + +void main() +{ + const float magic = 6.28; + vec2 uv = vTexCoord.xy; + vec2 texelSize = params.SourceSize.zw; + float CheckValue, FirstValue, PrevValue, LastMatch; + vec2 FinalUvShift,CheckUvShift; + + PrevValue = PixV(iChannel0, vec2(0,0), uv, texelSize); + LastMatch = abs(PrevValue - PixV(iChannel1, vec2(0,0), uv, texelSize)); + + for(int Circle = 1; Circle < 4; ++Circle) + { + for(int j = 0; j < 4 * Circle; ++j) + { + float circleF = float(Circle); + float jF = float(j); + + FirstValue = magic / (4.0 * circleF); + CheckUvShift = vec2(sin(FirstValue * jF), cos(FirstValue * jF)); + float currentValue = PixV(iChannel1, CheckUvShift * texelSize * circleF, uv, texelSize); + if ((CheckValue = abs(PrevValue - currentValue)) < LastMatch) + { + LastMatch = CheckValue; + FinalUvShift = CheckUvShift; + } + } + } + FragColor = vec4(FinalUvShift.xy, 0, 1); +} \ No newline at end of file diff --git a/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass1.slang b/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass1.slang new file mode 100644 index 0000000..6fe5417 --- /dev/null +++ b/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass1.slang @@ -0,0 +1,57 @@ +#version 450 + +layout(push_constant) uniform Push +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; +} params; + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +vec4 blur(vec2 uv, vec2 TexelSize, vec2 Direction) +{ + vec4 c = vec4(0.0); + + c += texture(Source, uv + (TexelSize*Direction*.5))*0.49; + c += texture(Source, uv + (TexelSize*Direction*1.5))*0.33; + c += texture(Source, uv + (TexelSize*Direction*2.5))*0.14; + c += texture(Source, uv + (TexelSize*Direction*3.5))*9.0; + c += texture(Source, uv + (TexelSize*Direction*4.5))*0.01; + c += texture(Source, uv - (TexelSize*Direction*.5))*0.49; + c += texture(Source, uv - (TexelSize*Direction*1.5))*0.33; + c += texture(Source, uv - (TexelSize*Direction*2.5))*0.14; + c += texture(Source, uv - (TexelSize*Direction*3.5))*0.04; + c += texture(Source, uv - (TexelSize*Direction*4.5))*0.01; + + return c/2.0; +} + +void main() +{ + vec2 uv = vTexCoord.xy; + vec2 TexelSize = params.SourceSize.zw; + vec2 DirectionH = vec2(1.0, 0.0); + vec4 cH = blur(uv, TexelSize, DirectionH); + FragColor = cH; +} \ No newline at end of file diff --git a/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass2.slang b/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass2.slang new file mode 100644 index 0000000..7ef9cc4 --- /dev/null +++ b/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass2.slang @@ -0,0 +1,57 @@ +#version 450 + +layout(push_constant) uniform Push +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; +} params; + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +vec4 blur(vec2 uv, vec2 TexelSize, vec2 Direction) +{ + vec4 c = vec4(0.0); + + c += texture(Source, uv + (TexelSize*Direction*.5))*0.49; + c += texture(Source, uv + (TexelSize*Direction*1.5))*0.33; + c += texture(Source, uv + (TexelSize*Direction*2.5))*0.14; + c += texture(Source, uv + (TexelSize*Direction*3.5))*9.0; + c += texture(Source, uv + (TexelSize*Direction*4.5))*0.01; + c += texture(Source, uv - (TexelSize*Direction*.5))*0.49; + c += texture(Source, uv - (TexelSize*Direction*1.5))*0.33; + c += texture(Source, uv - (TexelSize*Direction*2.5))*0.14; + c += texture(Source, uv - (TexelSize*Direction*3.5))*0.04; + c += texture(Source, uv - (TexelSize*Direction*4.5))*0.01; + + return c/2.0; +} + +void main() +{ + vec2 uv = vTexCoord.xy; + vec2 TexelSize = params.SourceSize.zw; + vec2 DirectionV = vec2(0.0, 1.0); + vec4 cV = blur(uv, TexelSize, DirectionV); + FragColor = cV; +} \ No newline at end of file diff --git a/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass3.slang b/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass3.slang new file mode 100644 index 0000000..78fed23 --- /dev/null +++ b/motion-interpolation/shaders/motion_interpolation/motion_interpolation_pass3.slang @@ -0,0 +1,69 @@ +#version 450 + +// Video Motion Interpolation +// based on Drudgerist's shadertoy: +// https://www.shadertoy.com/view/MtVfRz +// which is, in turn, based on bodhid's V+ +// https://github.com/bodhid/Vplus +// This pass uses the motion vectors to make an in-between frame + +layout(push_constant) uniform Push +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + float MOTION_SCALE_FACTOR; + float MOTION_MIX_FACTOR; +} params; + +#pragma parameter MOTION_SCALE_FACTOR "Motion Interp Factor" 300.0 100.0 1000.0 50.0 +#pragma parameter MOTION_MIX_FACTOR "Motion Blur Factor" 0.5 0.0 1.0 0.01 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; +layout(set = 0, binding = 3) uniform sampler2D OriginalHistory1; +layout(set = 0, binding = 4) uniform sampler2D Pass0; +layout(set = 0, binding = 5) uniform sampler2D Original; + +// uncomment the next line to see the motion vectors for debugging purposes +//#define OUTPUT_MOTION_VECTOR +#define iChannel3 Source +#define iChannel2 Pass0 +#define iChannel1 Original +#define iChannel0 OriginalHistory1 + +void main() +{ + vec2 uv = vTexCoord.xy; + vec4 col = texture(iChannel3, uv); +#ifdef OUTPUT_MOTION_VECTOR + FragColor = (col * 0.5) + 0.5; + return; +#endif + + float testShift = params.MOTION_MIX_FACTOR;//iMouse.x / iResolution.x; + vec2 FinalUvShift = texture(iChannel2, uv).rg / params.MOTION_SCALE_FACTOR; + vec4 previousFrame = texture(iChannel0, uv - (FinalUvShift * 0.5)); + vec4 nextFrame = texture(iChannel1, uv - (FinalUvShift * (0.5))); + + FragColor = mix(previousFrame, nextFrame, testShift); +} \ No newline at end of file