Skip to content

Commit

Permalink
Update cOpticalFlow.fx
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Dec 15, 2023
1 parent a989a84 commit ece4ec6
Showing 1 changed file with 137 additions and 18 deletions.
155 changes: 137 additions & 18 deletions shaders/cOpticalFlow.fx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
#include "shared/cImageProcessing.fxh"
#include "shared/cVideoProcessing.fxh"

/*
MIT License
Copyright (c) 2016 Thomas Diewald
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

namespace cOpticalFlow
{
/*
Expand All @@ -14,22 +38,103 @@ namespace cOpticalFlow
ui_type = "slider";
ui_min = 0.0;
ui_max = 7.0;
> = 0.0;
> = 3.5;

uniform float _BlendFactor <
ui_label = "Temporal Blending Factor";
ui_type = "slider";
ui_min = 0.0;
ui_max = 0.9;
> = 0.0;
> = 0.45;

#ifndef RENDER_VELOCITY_STREAMS
#define RENDER_VELOCITY_STREAMS 1
#endif

#ifndef VERTEX_SPACING
#define VERTEX_SPACING 10
#endif

#define LINES_X uint(BUFFER_WIDTH / VERTEX_SPACING)
#define LINES_Y uint(BUFFER_HEIGHT / VERTEX_SPACING)
#define NUM_LINES (LINES_X * LINES_Y)
#define SPACE_X (BUFFER_WIDTH / LINES_X)
#define SPACE_Y (BUFFER_HEIGHT / LINES_Y)
#define VELOCITY_SCALE (SPACE_X + SPACE_Y) * 1

/*
[Textures & Samplers]
*/

CREATE_SAMPLER(SampleTempTex1, TempTex1_RG8, LINEAR, MIRROR)
CREATE_SAMPLER(SampleTempTex2a, TempTex2a_RG16F, LINEAR, MIRROR)
CREATE_SAMPLER(SampleTempTex2b, TempTex2b_RG16F, LINEAR, MIRROR)

struct VS2PS_Streaming
{
float4 HPos : SV_POSITION;
float2 Velocity : TEXCOORD0;
};

VS2PS_Streaming VS_Streaming(APP2VS Input)
{
VS2PS_Streaming Output;

int LineID = Input.ID / 2; // Line Index
int VertexID = Input.ID % 2; // Vertex Index within the line (0 = start, 1 = end)

// Get Row (x) and Column (y) position
int Row = LineID / LINES_X;
int Column = LineID - LINES_X * Row;

// Compute origin (line-start)
const float2 Spacing = float2(SPACE_X, SPACE_Y);
float2 Offset = Spacing * 0.5;
float2 Origin = Offset + float2(Column, Row) * Spacing;

// Get velocity from texture at origin location
const float2 PixelSize = float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT);
float2 VelocityCoord;
VelocityCoord.x = Origin.x * PixelSize.x;
VelocityCoord.y = 1.0 - (Origin.y * PixelSize.y);
Output.Velocity = tex2Dlod(SampleTempTex2b, float4(VelocityCoord, 0.0, _MipBias)).xy / PixelSize;
Output.Velocity *= smoothstep(1.0, 2.0, length(Output.Velocity));

// Scale velocity
float2 Direction = Output.Velocity * VELOCITY_SCALE;

float Length = length(Direction + 1e-5);
Direction = Direction / sqrt(Length * 0.1);

// Color for fragmentshader
Output.Velocity = Direction * 0.2;

// Compute current vertex position (based on VertexID)
float2 VertexPosition = 0.0;

// Lines: Velocity direction
VertexPosition = Origin + Direction * VertexID;

// Finish vertex position
float2 VertexPositionNormal = (VertexPosition + 0.5) * PixelSize; // [0, 1]
Output.HPos = float4((VertexPositionNormal * 2.0) - 1.0, 0.0, 1.0); // ndc: [-1, +1]

return Output;
}

float4 PS_Streaming(VS2PS_Streaming Input) : SV_TARGET0
{
float2 Velocity = Input.Velocity;
float Magnitude = saturate(length(Velocity)) + 1e-4;

float3 Display = 0.0;
Display.rg = ((Velocity / Magnitude) * 0.5) + 0.5;
Display.b = 1.0 - dot(Display.rg, 0.5);

return float4(Display, 1.0);
}

CREATE_SAMPLER(SampleTempTex2a, TempTex2a_RG16F, LINEAR, MIRROR)

CREATE_SAMPLER(SampleTempTex3, TempTex3_RG16F, LINEAR, MIRROR)
CREATE_SAMPLER(SampleTempTex4, TempTex4_RG16F, LINEAR, MIRROR)
CREATE_SAMPLER(SampleTempTex5, TempTex5_RG16F, LINEAR, MIRROR)
Expand Down Expand Up @@ -100,17 +205,17 @@ namespace cOpticalFlow
return float4(GetPixelBlur(Input, SampleTempTex2a, false).rg, 0.0, 1.0);
}

float4 PS_Display(VS2PS_Quad Input) : SV_TARGET0
float4 PS_Shading(VS2PS_Quad Input) : SV_TARGET0
{
float2 InvTexSize = fwidth(Input.Tex0);

float2 Vectors = tex2Dlod(SampleTempTex2b, float4(Input.Tex0.xy, 0.0, _MipBias)).xy;
Vectors = DecodeVectors(Vectors, InvTexSize);
Vectors = DecodeVectors(Vectors, fwidth(Input.Tex0));
float Magnitude = length(float3(Vectors, 1.0));

float3 NVectors = normalize(float3(Vectors, 1.0));
NVectors = saturate((NVectors * 0.5) + 0.5);
float3 Display = 0.0;
Display.rg = ((Vectors / Magnitude) * 0.5) + 0.5;
Display.b = 1.0 - dot(Display.rg, 0.5);

return float4(NVectors, 1.0);
return float4(Display, 1.0);
}

#define CREATE_PASS(VERTEX_SHADER, PIXEL_SHADER, RENDER_TARGET) \
Expand Down Expand Up @@ -163,13 +268,27 @@ namespace cOpticalFlow
RenderTarget0 = TempTex2b_RG16F;
}

// Display
pass
{
SRGBWriteEnable = WRITE_SRGB;

VertexShader = VS_Quad;
PixelShader = PS_Display;
}
#if RENDER_VELOCITY_STREAMS
pass
{
PrimitiveTopology = LINELIST;
VertexCount = NUM_LINES * 2;
VertexShader = VS_Streaming;
PixelShader = PS_Streaming;
ClearRenderTargets = FALSE;
BlendEnable = TRUE;
BlendOp = ADD;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
SrcBlendAlpha = ONE;
DestBlendAlpha = ONE;
}
#else
pass
{
VertexShader = VS_Quad;
PixelShader = PS_Shading;
}
#endif
}
}

0 comments on commit ece4ec6

Please sign in to comment.