Skip to content

Commit

Permalink
Pack MV
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Mar 27, 2024
1 parent bbbf902 commit 0b1e1b4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion shaders/cBlockMatching.fx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace cBlockMatching
float2 InvTexSize = fwidth(Input.Tex0);

float2 Vectors = tex2Dlod(SampleOFlowTex, float4(Input.Tex0.xy, 0.0, _MipBias)).xy;
Vectors = DecodeVectors(Vectors, InvTexSize);
Vectors = UnnormalizeMotionVectors(UnpackMotionVectors(Vectors), InvTexSize);

float3 NVectors = normalize(float3(Vectors, 1.0));
NVectors = saturate((NVectors * 0.5) + 0.5);
Expand Down
2 changes: 1 addition & 1 deletion shaders/cMotionBlur.fx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace cMotionBlur
float2 ScreenSize = float2(BUFFER_WIDTH, BUFFER_HEIGHT);
float2 ScreenCoord = Input.Tex0.xy;

float2 Velocity = tex2Dlod(SampleTempTex2b, float4(Input.Tex0.xy, 0.0, _MipBias)).xy;
float2 Velocity = UnpackMotionVectors(tex2Dlod(SampleTempTex2b, float4(Input.Tex0.xy, 0.0, _MipBias)).xy);

float2 ScaledVelocity = Velocity * _Scale;
ScaledVelocity = (_FrameRateScaling) ? ScaledVelocity / FrameTimeRatio : ScaledVelocity;
Expand Down
4 changes: 2 additions & 2 deletions shaders/cOpticalFlow.fx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace cOpticalFlow
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 = UnpackMotionVectors(tex2Dlod(SampleTempTex2b, float4(VelocityCoord, 0.0, _MipBias)).xy) / PixelSize;
Output.Velocity.y *= -1.0;

// Scale velocity
Expand Down Expand Up @@ -208,7 +208,7 @@ namespace cOpticalFlow
float4 PS_Shading(VS2PS_Quad Input) : SV_TARGET0
{
float2 Vectors = tex2Dlod(SampleTempTex2b, float4(Input.Tex0.xy, 0.0, _MipBias)).xy;
Vectors = DecodeVectors(Vectors, fwidth(Input.Tex0));
Vectors = UnnormalizeMotionVectors(UnpackMotionVectors(Vectors), fwidth(Input.Tex0));
Vectors.y *= -1.0;
float Magnitude = length(float3(Vectors, 1.0));

Expand Down
8 changes: 4 additions & 4 deletions shaders/kDatamosh.fx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ namespace kDatamosh
Random.z = RandUV(Tex.yx - Time.xx);

// Normalized screen space -> Pixel coordinates
MV = DecodeVectors(MV * _Scale, TexSize);
MV = UnnormalizeMotionVectors(MV * _Scale, TexSize);

// Small random displacement (diffusion)
MV += (Random.xy - 0.5) * _Diffusion;
Expand All @@ -218,7 +218,7 @@ namespace kDatamosh
float3 Random = 0.0;

// Motion vectors
float2 MV = tex2Dlod(SampleFilteredFlowTex, float4(Input.Tex0, 0.0, _MipBias)).xy;
float2 MV = UnpackMotionVectors(tex2Dlod(SampleFilteredFlowTex, float4(Input.Tex0, 0.0, _MipBias)).xy);

// Get motion blocks
MV = GetMVBlocks(MV, Input.Tex0, Random);
Expand Down Expand Up @@ -257,7 +257,7 @@ namespace kDatamosh
float3 Random = 0.0;

// Motion vectors
float2 MV = tex2Dlod(SampleFilteredFlowTex, float4(Input.Tex0, 0.0, _MipBias)).xy;
float2 MV = UnpackMotionVectors(tex2Dlod(SampleFilteredFlowTex, float4(Input.Tex0, 0.0, _MipBias)).xy);

// Get motion blocks
MV = GetMVBlocks(MV, Input.Tex0, Random);
Expand All @@ -266,7 +266,7 @@ namespace kDatamosh
float RandomMotion = RandUV(Input.Tex0 + length(MV));

// Pixel coordinates -> Normalized screen space
MV = EncodeVectors(MV, TexSize);
MV = NormalizeMotionVectors(MV, TexSize);

// Color from the original image
float4 Source = tex2D(CShade_SampleColorTex, Input.Tex0);
Expand Down
54 changes: 45 additions & 9 deletions shaders/shared/cVideoProcessing.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,46 @@
[Functions]
*/

float GetHalfMax()
{
// Get the Half format distribution of bits
// Sign Exponent Significand
// 0 00000 000000000
const int SignBit = 0;
const int ExponentBits = 5;
const int SignificandBits = 10;

const int Bias = -15;

const int Exponent = 1 << ExponentBits;
const int Significand = 1 << SignificandBits;

const float MaxExponent = (Exponent - (1 << 1)) + Bias;
const float MaxSignificand = 1.0 + float((Significand - 1.0) / Significand);

return pow(-1, SignBit) * exp2(MaxExponent) * MaxSignificand;
}

// [-Half, Half] -> [-1.0, 1.0]
float2 UnpackMotionVectors(float2 Half2)
{
return clamp(Half2 / GetHalfMax(), -1.0, 1.0);
}

// [-1.0, 1.0] -> [-Half, Half]
float2 PackMotionVectors(float2 Half2)
{
return Half2 * GetHalfMax();
}

// [-1.0, 1.0] -> [Width, Height]
float2 DecodeVectors(float2 Vectors, float2 ImageSize)
float2 UnnormalizeMotionVectors(float2 Vectors, float2 ImageSize)
{
return Vectors / abs(ImageSize);
}

// [Width, Height] -> [-1.0, 1.0]
float2 EncodeVectors(float2 Vectors, float2 ImageSize)
float2 NormalizeMotionVectors(float2 Vectors, float2 ImageSize)
{
return clamp(Vectors * abs(ImageSize), -1.0, 1.0);
}
Expand Down Expand Up @@ -50,17 +82,20 @@
// Get required data to calculate main texel data
const float Pi2 = acos(-1.0) * 2.0;

// Unpack and upscale vectors
Vectors = UnpackMotionVectors(Vectors);

// Calculate main texel data (TexelSize, TexelLOD)
WarpTex = float4(MainTex, MainTex + Vectors);

// Get gradient information
float4 TexIx = ddx(WarpTex);
float4 TexIy = ddy(WarpTex);
float2 PixelSize = abs(TexIx.xy) + abs(TexIy.xy);

// Un-normalize data for processing
WarpTex *= (1.0 / abs(PixelSize.xyxy));
Vectors = DecodeVectors(Vectors, PixelSize);
// Upscale
WarpTex.xy = UnnormalizeMotionVectors(WarpTex.xy, PixelSize);
WarpTex.zw = UnnormalizeMotionVectors(WarpTex.zw, PixelSize);

[loop] for(int i = 1; i < 4; ++i)
{
Expand Down Expand Up @@ -114,7 +149,8 @@
float2 Flow = (D == 0.0) ? 0.0 : mul(B, A);

// Propagate and encode vectors
return EncodeVectors(Vectors + Flow, PixelSize);
Vectors = UnnormalizeMotionVectors(Vectors, PixelSize) + Flow;
return PackMotionVectors(NormalizeMotionVectors(Vectors, PixelSize));
}

/*
Expand Down Expand Up @@ -222,7 +258,7 @@

// Un-normalize data for processing
B.Tex *= (1.0 / B.PixelSize.xyxy);
Vectors = DecodeVectors(Vectors, B.PixelSize);
Vectors = UnpackMotionVectors(Vectors);

// Pre-calculate template
float2 Template[4];
Expand All @@ -231,6 +267,6 @@
// Calculate three-step search
// Propagate and encode vectors
Vectors += SearchArea(SampleImage, B, Template);
return EncodeVectors(Vectors, B.PixelSize);
return PackMotionVectors(Vectors);
}
#endif

0 comments on commit 0b1e1b4

Please sign in to comment.