Skip to content

Commit

Permalink
Use derivative pyramid because no warp right now
Browse files Browse the repository at this point in the history
  • Loading branch information
brimson committed Jul 10, 2022
1 parent 49f753b commit 7e08a50
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 44 deletions.
33 changes: 20 additions & 13 deletions shaders/cMotionBlur.fx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace OpticalFlowLK
Position = float4(TexCoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}

void Derivatives_VS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float4 TexCoords[2] : TEXCOORD0)
void Derivatives_Spatial_VS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float4 TexCoords[2] : TEXCOORD0)
{
float2 TexCoordVS = 0.0;
Basic_VS(ID, Position, TexCoordVS);
Expand Down Expand Up @@ -305,7 +305,14 @@ namespace OpticalFlowLK
Gaussian_Blur(Sample_Common_1_B, TexCoords, true, OutputColor0);
}

void Derivatives_PS(in float4 Position : SV_POSITION, in float4 TexCoords[2] : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
void Derivatives_Temporal_PS(in float4 Position : SV_POSITION, in float2 TexCoord : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
{
float Current = tex2D(Sample_Common_1_A, TexCoord).x;
float Previous = tex2D(Sample_Common_1_C, TexCoord).x;
OutputColor0 = Current - Previous;
}

void Derivatives_Spatial_PS(in float4 Position : SV_POSITION, in float4 TexCoords[2] : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
{
// Bilinear 5x5 Sobel by CeeJayDK
// B1 B2
Expand All @@ -332,10 +339,10 @@ namespace OpticalFlowLK
// [TexCoord.xw TexCoord.zw]
// [TexCoord.xy TexCoord.zy]
float2 S[4];
S[0] = tex2D(Sample_Common_1_B, TexCoord.xw).xy;
S[1] = tex2D(Sample_Common_1_B, TexCoord.zw).xy;
S[2] = tex2D(Sample_Common_1_B, TexCoord.xy).xy;
S[3] = tex2D(Sample_Common_1_B, TexCoord.zy).xy;
S[0] = tex2D(Sample_Common_1_C, TexCoord.xw).xy;
S[1] = tex2D(Sample_Common_1_C, TexCoord.zw).xy;
S[2] = tex2D(Sample_Common_1_C, TexCoord.xy).xy;
S[3] = tex2D(Sample_Common_1_C, TexCoord.zy).xy;

// A.x = Ix^2 (A11); A.y = Iy^2 (A22); A.z = IxIy (A12)
float3 A = float3(FP16_MINIMUM, FP16_MINIMUM, 0.0);
Expand All @@ -347,10 +354,10 @@ namespace OpticalFlowLK

// Temporal derivative window in 4 bilinear fetches
float T[4];
T[0] = tex2D(Sample_Common_1_A, TexCoord.xw).x - tex2D(Sample_Common_1_C, TexCoord.xw).x;
T[1] = tex2D(Sample_Common_1_A, TexCoord.zw).x - tex2D(Sample_Common_1_C, TexCoord.zw).x;
T[2] = tex2D(Sample_Common_1_A, TexCoord.xy).x - tex2D(Sample_Common_1_C, TexCoord.xy).x;
T[3] = tex2D(Sample_Common_1_A, TexCoord.zy).x - tex2D(Sample_Common_1_C, TexCoord.zy).x;
T[0] = tex2D(Sample_Common_1_B, TexCoord.xw).x;
T[1] = tex2D(Sample_Common_1_B, TexCoord.zw).x;
T[2] = tex2D(Sample_Common_1_B, TexCoord.xy).x;
T[3] = tex2D(Sample_Common_1_B, TexCoord.zy).x;

// B.x = IxIt (Q1); B.y = IyIt (Q2)
float2 B = 0.0;
Expand All @@ -362,7 +369,6 @@ namespace OpticalFlowLK

// Determinant
float D = (A.z * A.z - A.x * A.y);

return ((A.yx * B.xy - A.zz * B.yx) / D) + Vectors;
}

Expand Down Expand Up @@ -447,8 +453,9 @@ namespace OpticalFlowLK
CREATE_PASS(Pre_Blur_0_VS, Pre_Blur_0_PS, Render_Common_1_B)
CREATE_PASS(Pre_Blur_1_VS, Pre_Blur_1_PS, Render_Common_1_A) // Save this to store later

// Calculate spatial derivative pyramid
CREATE_PASS(Derivatives_VS, Derivatives_PS, Render_Common_1_B)
// Calculate derivative pyramids
CREATE_PASS(Basic_VS, Derivatives_Temporal_PS, Render_Common_1_B)
CREATE_PASS(Derivatives_Spatial_VS, Derivatives_Spatial_PS, Render_Common_1_C)

// Bilinear Lucas-Kanade Optical Flow
CREATE_PASS(LK_Level_6_VS, LK_Level_6_PS, Render_Common_6)
Expand Down
43 changes: 25 additions & 18 deletions shaders/cOpticalFlowLK.fx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace OpticalFlowLK
Position = float4(TexCoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}

void Derivatives_VS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float4 TexCoords[2] : TEXCOORD0)
void Derivatives_Spatial_VS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float4 TexCoords[2] : TEXCOORD0)
{
float2 TexCoordVS = 0.0;
Basic_VS(ID, Position, TexCoordVS);
Expand Down Expand Up @@ -298,7 +298,14 @@ namespace OpticalFlowLK
Gaussian_Blur(Sample_Common_1_B, TexCoords, true, OutputColor0);
}

void Derivatives_PS(in float4 Position : SV_POSITION, in float4 TexCoords[2] : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
void Derivatives_Temporal_PS(in float4 Position : SV_POSITION, in float2 TexCoord : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
{
float Current = tex2D(Sample_Common_1_A, TexCoord).x;
float Previous = tex2D(Sample_Common_1_C, TexCoord).x;
OutputColor0 = Current - Previous;
}

void Derivatives_Spatial_PS(in float4 Position : SV_POSITION, in float4 TexCoords[2] : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
{
// Bilinear 5x5 Sobel by CeeJayDK
// B1 B2
Expand All @@ -325,10 +332,10 @@ namespace OpticalFlowLK
// [TexCoord.xw TexCoord.zw]
// [TexCoord.xy TexCoord.zy]
float2 S[4];
S[0] = tex2D(Sample_Common_1_B, TexCoord.xw).xy;
S[1] = tex2D(Sample_Common_1_B, TexCoord.zw).xy;
S[2] = tex2D(Sample_Common_1_B, TexCoord.xy).xy;
S[3] = tex2D(Sample_Common_1_B, TexCoord.zy).xy;
S[0] = tex2D(Sample_Common_1_C, TexCoord.xw).xy;
S[1] = tex2D(Sample_Common_1_C, TexCoord.zw).xy;
S[2] = tex2D(Sample_Common_1_C, TexCoord.xy).xy;
S[3] = tex2D(Sample_Common_1_C, TexCoord.zy).xy;

// A.x = Ix^2 (A11); A.y = Iy^2 (A22); A.z = IxIy (A12)
float3 A = float3(FP16_MINIMUM, FP16_MINIMUM, 0.0);
Expand All @@ -340,10 +347,10 @@ namespace OpticalFlowLK

// Temporal derivative window in 4 bilinear fetches
float T[4];
T[0] = tex2D(Sample_Common_1_A, TexCoord.xw).x - tex2D(Sample_Common_1_C, TexCoord.xw).x;
T[1] = tex2D(Sample_Common_1_A, TexCoord.zw).x - tex2D(Sample_Common_1_C, TexCoord.zw).x;
T[2] = tex2D(Sample_Common_1_A, TexCoord.xy).x - tex2D(Sample_Common_1_C, TexCoord.xy).x;
T[3] = tex2D(Sample_Common_1_A, TexCoord.zy).x - tex2D(Sample_Common_1_C, TexCoord.zy).x;
T[0] = tex2D(Sample_Common_1_B, TexCoord.xw).x;
T[1] = tex2D(Sample_Common_1_B, TexCoord.zw).x;
T[2] = tex2D(Sample_Common_1_B, TexCoord.xy).x;
T[3] = tex2D(Sample_Common_1_B, TexCoord.zy).x;

// B.x = IxIt (Q1); B.y = IyIt (Q2)
float2 B = 0.0;
Expand All @@ -355,7 +362,6 @@ namespace OpticalFlowLK

// Determinant
float D = (A.z * A.z - A.x * A.y);

return ((A.yx * B.xy - A.zz * B.yx) / D) + Vectors;
}

Expand Down Expand Up @@ -400,8 +406,11 @@ namespace OpticalFlowLK
void Display_PS(in float4 Position : SV_POSITION, in float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_Target)
{
OutputColor0 = 0.0;
OutputColor0 = tex2Dlod(Sample_Common_3_A, float4(TexCoord, 0.0, _MipBias));
OutputColor0.ba = 0.0;
float2 Velocity = tex2Dlod(Sample_Common_3_A, float4(TexCoord, 0.0, _MipBias)).xy;
float VelocityLength = saturate(rsqrt(dot(Velocity, Velocity)));
OutputColor0.rg = (Velocity * VelocityLength) * 0.5 + 0.5;
OutputColor0.b = -dot(OutputColor0.rg, 1.0) * 0.5 + 1.0;
OutputColor0.rgb /= max(max(OutputColor0.x, OutputColor0.y), OutputColor0.z);
}

#define CREATE_PASS(VERTEX_SHADER, PIXEL_SHADER, RENDER_TARGET) \
Expand All @@ -424,8 +433,9 @@ namespace OpticalFlowLK
CREATE_PASS(Pre_Blur_0_VS, Pre_Blur_0_PS, Render_Common_1_B)
CREATE_PASS(Pre_Blur_1_VS, Pre_Blur_1_PS, Render_Common_1_A) // Save this to store later

// Calculate spatial derivative pyramid
CREATE_PASS(Derivatives_VS, Derivatives_PS, Render_Common_1_B)
// Calculate derivative pyramids
CREATE_PASS(Basic_VS, Derivatives_Temporal_PS, Render_Common_1_B)
CREATE_PASS(Derivatives_Spatial_VS, Derivatives_Spatial_PS, Render_Common_1_C)

// Bilinear Lucas-Kanade Optical Flow
CREATE_PASS(LK_Level_6_VS, LK_Level_6_PS, Render_Common_6)
Expand Down Expand Up @@ -455,9 +465,6 @@ namespace OpticalFlowLK
{
VertexShader = Basic_VS;
PixelShader = Display_PS;
#if BUFFER_COLOR_BIT_DEPTH == 8
SRGBWriteEnable = TRUE;
#endif
}

// Copy current convolved frame for next frame
Expand Down
33 changes: 20 additions & 13 deletions shaders/kDatamosh.fx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ namespace Datamosh
CREATE_LEVEL_VS(LK_Level_5_VS, BUFFER_SIZE_5)
CREATE_LEVEL_VS(LK_Level_6_VS, BUFFER_SIZE_6)

void Derivatives_VS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float4 TexCoords[2] : TEXCOORD0)
void Derivatives_Spatial_VS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float4 TexCoords[2] : TEXCOORD0)
{
float2 CoordVS = 0.0;
Basic_VS(ID, Position, CoordVS);
Expand Down Expand Up @@ -374,7 +374,14 @@ namespace Datamosh
Gaussian_Blur(Sample_Common_1_B, TexCoords, true, OutputColor0);
}

void Derivatives_PS(in float4 Position : SV_POSITION, in float4 TexCoords[2] : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
void Derivatives_Temporal_PS(in float4 Position : SV_POSITION, in float2 TexCoord : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
{
float Current = tex2D(Sample_Common_1_A, TexCoord).x;
float Previous = tex2D(Sample_Common_1_C, TexCoord).x;
OutputColor0 = Current - Previous;
}

void Derivatives_Spatial_PS(in float4 Position : SV_POSITION, in float4 TexCoords[2] : TEXCOORD0, out float2 OutputColor0 : SV_TARGET0)
{
// Bilinear 5x5 Sobel by CeeJayDK
// B1 B2
Expand All @@ -401,10 +408,10 @@ namespace Datamosh
// [TexCoord.xw TexCoord.zw]
// [TexCoord.xy TexCoord.zy]
float2 S[4];
S[0] = tex2D(Sample_Common_1_B, TexCoord.xw).xy;
S[1] = tex2D(Sample_Common_1_B, TexCoord.zw).xy;
S[2] = tex2D(Sample_Common_1_B, TexCoord.xy).xy;
S[3] = tex2D(Sample_Common_1_B, TexCoord.zy).xy;
S[0] = tex2D(Sample_Common_1_C, TexCoord.xw).xy;
S[1] = tex2D(Sample_Common_1_C, TexCoord.zw).xy;
S[2] = tex2D(Sample_Common_1_C, TexCoord.xy).xy;
S[3] = tex2D(Sample_Common_1_C, TexCoord.zy).xy;

// A.x = Ix^2 (A11); A.y = Iy^2 (A22); A.z = IxIy (A12)
float3 A = float3(FP16_MINIMUM, FP16_MINIMUM, 0.0);
Expand All @@ -416,10 +423,10 @@ namespace Datamosh

// Temporal derivative window in 4 bilinear fetches
float T[4];
T[0] = tex2D(Sample_Common_1_A, TexCoord.xw).x - tex2D(Sample_Common_1_C, TexCoord.xw).x;
T[1] = tex2D(Sample_Common_1_A, TexCoord.zw).x - tex2D(Sample_Common_1_C, TexCoord.zw).x;
T[2] = tex2D(Sample_Common_1_A, TexCoord.xy).x - tex2D(Sample_Common_1_C, TexCoord.xy).x;
T[3] = tex2D(Sample_Common_1_A, TexCoord.zy).x - tex2D(Sample_Common_1_C, TexCoord.zy).x;
T[0] = tex2D(Sample_Common_1_B, TexCoord.xw).x;
T[1] = tex2D(Sample_Common_1_B, TexCoord.zw).x;
T[2] = tex2D(Sample_Common_1_B, TexCoord.xy).x;
T[3] = tex2D(Sample_Common_1_B, TexCoord.zy).x;

// B.x = IxIt (Q1); B.y = IyIt (Q2)
float2 B = 0.0;
Expand All @@ -431,7 +438,6 @@ namespace Datamosh

// Determinant
float D = (A.z * A.z - A.x * A.y);

return ((A.yx * B.xy - A.zz * B.yx) / D) + Vectors;
}

Expand Down Expand Up @@ -583,8 +589,9 @@ namespace Datamosh
CREATE_PASS(Pre_Blur_0_VS, Pre_Blur_0_PS, Render_Common_1_B)
CREATE_PASS(Pre_Blur_1_VS, Pre_Blur_1_PS, Render_Common_1_A) // Save this to store later

// Calculate spatial derivative pyramid
CREATE_PASS(Derivatives_VS, Derivatives_PS, Render_Common_1_B)
// Calculate derivative pyramids
CREATE_PASS(Basic_VS, Derivatives_Temporal_PS, Render_Common_1_B)
CREATE_PASS(Derivatives_Spatial_VS, Derivatives_Spatial_PS, Render_Common_1_C)

// Bilinear Optical Flow
CREATE_PASS(LK_Level_6_VS, LK_Level_6_PS, Render_Common_6)
Expand Down

0 comments on commit 7e08a50

Please sign in to comment.