Skip to content

Commit

Permalink
Comment on downsample scaling
Browse files Browse the repository at this point in the history
brimson committed Jul 16, 2022
1 parent f6c87fc commit b196ca6
Showing 3 changed files with 21 additions and 14 deletions.
19 changes: 11 additions & 8 deletions shaders/cMotionBlur.fx
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ namespace OpticalFlowLK
ui_max = MAXIMUM; \
> = DEFAULT;

OPTION(float, _MipBias, "slider", "Optical flow", "Optical flow mipmap bias", 0.0, 6.0, 0.0)
OPTION(float, _MipBias, "slider", "Optical flow", "Optical flow mipmap bias", 0.0, 6.0, 0.5)
OPTION(float, _BlendFactor, "slider", "Optical flow", "Temporal blending factor", 0.0, 0.9, 0.2)

OPTION(float, _Scale, "slider", "Main", "Blur scale", 0.0, 1.0, 0.5)
@@ -62,7 +62,7 @@ namespace OpticalFlowLK
[Macros for resolution sizes and scaling]
*/

#define FP16_MINIMUM float((1.0 / float(1 << 14)) * (0.0 + (1.0 / 1024.0)))
#define FP16_MINIMUM float((1.0 / float(1 << 14)) * (0.0 + (1.0 / 1024.0)))

#define RCP_HEIGHT (1.0 / BUFFER_HEIGHT)
#define ASPECT_RATIO (BUFFER_WIDTH * RCP_HEIGHT)
@@ -357,7 +357,7 @@ namespace OpticalFlowLK
A /= 4.0;

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

// Temporal derivative window in 4 bilinear fetches
float T[4];
@@ -374,7 +374,7 @@ namespace OpticalFlowLK
B += (S[3] * T[3]);
B /= 4.0;

float2 UV = (D != 0.0) ? ((A.yx * B.xy - A.zz * B.yx) / D) + Vectors : 0.0;
float2 UV = (D != 0.0) ? ((A.yx * B.xy - A.zz * B.yx) / D) + (Vectors * 2.0) : 0.0;
return UV;
}

@@ -425,14 +425,17 @@ namespace OpticalFlowLK
float FrameRate = 1e+3 / _FrameTime;
float FrameTimeRatio = _TargetFrameRate / FrameRate;

float2 Velocity = tex2Dlod(Sample_Common_3_A, float4(TexCoord, 0.0, _MipBias)).xy;
float2 ScreenSize = float2(BUFFER_WIDTH, BUFFER_HEIGHT);
float2 ScreenCoord = TexCoord.xy * ScreenSize;

// DownsampledRatio = Texel ratio between the finest optical flow level and the buffer we're warping
// We need this because the velocity between the downsampled finest level is not to-scale with the current buffer
float2 DownsampledRatio = BUFFER_SIZE_3 / ScreenSize;
float2 Velocity = tex2Dlod(Sample_Common_3_A, float4(TexCoord, 0.0, _MipBias)).xy * DownsampledRatio;

float2 ScaledVelocity = Velocity * _Scale;
ScaledVelocity = (_FrameRateScaling) ? ScaledVelocity / FrameTimeRatio : ScaledVelocity;

float2 ScreenSize = float2(BUFFER_WIDTH, BUFFER_HEIGHT);
float2 ScreenCoord = TexCoord.xy * ScreenSize;

for(int k = 0; k < Samples; ++k)
{
float2 Offset = ScaledVelocity * (Noise + k);
4 changes: 2 additions & 2 deletions shaders/cOpticalFlowLK.fx
Original file line number Diff line number Diff line change
@@ -350,7 +350,7 @@ namespace OpticalFlowLK
A /= 4.0;

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

// Temporal derivative window in 4 bilinear fetches
float T[4];
@@ -367,7 +367,7 @@ namespace OpticalFlowLK
B += (S[3] * T[3]);
B /= 4.0;

float2 UV = (D != 0.0) ? ((A.yx * B.xy - A.zz * B.yx) / D) + Vectors : 0.0;
float2 UV = (D != 0.0) ? ((A.yx * B.xy - A.zz * B.yx) / D) + (Vectors * 2.0) : 0.0;
return UV;
}

12 changes: 8 additions & 4 deletions shaders/kDatamosh.fx
Original file line number Diff line number Diff line change
@@ -422,7 +422,7 @@ namespace Datamosh
A /= 4.0;

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

// Temporal derivative window in 4 bilinear fetches
float T[4];
@@ -439,7 +439,7 @@ namespace Datamosh
B += (S[3] * T[3]);
B /= 4.0;

float2 UV = (D != 0.0) ? ((A.yx * B.xy - A.zz * B.yx) / D) + Vectors : 0.0;
float2 UV = (D != 0.0) ? ((A.yx * B.xy - A.zz * B.yx) / D) + (Vectors * 2.0) : 0.0;
return UV;
}

@@ -522,7 +522,8 @@ namespace Datamosh

void Datamosh_PS(in float4 Position : SV_POSITION, in float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
const float2 DisplacementTexel = 1.0 / float2(BUFFER_WIDTH, BUFFER_HEIGHT);
float2 ScreenSize = float2(BUFFER_WIDTH, BUFFER_HEIGHT);
const float2 DisplacementTexel = 1.0 / ScreenSize;
const float Quality = 1.0 - _Entropy;

// Random numbers
@@ -532,7 +533,10 @@ namespace Datamosh
Random.y = RandomNoise(TexCoord.xy + Time.yx);
Random.z = RandomNoise(TexCoord.yx - Time.xx);

float2 MotionVectors = tex2Dlod(Sample_Optical_Flow_Post, float4(TexCoord, 0.0, _MipBias)).xy;
// DownsampledRatio = Texel ratio between the finest optical flow level and the buffer we're warping
// We need this because the velocity between the downsampled finest level is not to-scale with the current buffer
float2 DownsampledRatio = BUFFER_SIZE_3 / ScreenSize;
float2 MotionVectors = tex2Dlod(Sample_Optical_Flow_Post, float4(TexCoord, 0.0, _MipBias)).xy * DownsampledRatio;
MotionVectors *= _Scale;

float4 Source = tex2D(Sample_Color, TexCoord); // Color from the original image

0 comments on commit b196ca6

Please sign in to comment.