I'm working through implementing TAA in my renderer and I am wondering how I might remove the jitter before I output the velocity to my texture to store. I've shown below how I am adding jitter the projection matrix and also the current calculation for my velocity values.
Would appreciate any insight into how I might remove the jitter. Thanks.
Applying jitter:
static const float Halton23[8][2] = {
{0.0f / 8.0f, 0.0f / 9.0f}, {4.0f / 8.0f, 3.0f / 9.0f},
{2.0f / 8.0f, 6.0f / 9.0f}, {6.0f / 8.0f, 1.0f / 9.0f},
{1.0f / 8.0f, 4.0f / 9.0f}, {5.0f / 8.0f, 7.0f / 9.0f},
{3.0f / 8.0f, 2.0f / 9.0f}, {7.0f / 8.0f, 5.0f / 9.0f}
};
float haltonX = Halton23[FrameIndex][0];
float haltonY = Halton23[FrameIndex][1];
float2 dimensions = float2(1920, 1080);
float2 invDimensions = float2(1.0f / dimensions.x, 1.0f / dimensions.y);
float2 offset = float2(haltonX, haltonY);
offset.x = ((offset.x - 0.5f) / dimensions.x) * 2.0f;
offset.y = ((offset.y - 0.5f) / dimensions.y) * 2.0f;
float4x4 translationMatrix = float4x4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
translationMatrix[3][0] = offset.x;
translationMatrix[3][1] = offset.y;
float4x4 newProjection = mul(translationMatrix, P);
result.position = mul(float4(position, 1), WorldMatrix);
result.position = mul(result.position, V);
result.position = mul(result.position, newProjection);
Calculating velocity (need to remove jitter here)
void CSMain(uint3 DTid : SV_DispatchThreadID)
{
float2 texelSize = float2(1.0f / 1920, 1.0f / 1080);
float2 texCoords = texelSize * (DTid.xy + 0.5);
// Calculate current position world space
float depth = other.SampleLevel(sPointClamp, texCoords, 0).x;
float ndcX = texCoords.x * 2.0 - 1.0;
float ndcY = 1.0f - texCoords.y * 2.0f;
float4 viewpos = mul(float4(ndcX, ndcY, depth, 1.0), InverseProjectionMatrix);
viewpos = viewpos / viewpos.w;
float4 worldspace = mul(float4(viewpos.xyz, 1.0), InverseViewMatrix);
// Reproject using previous view and proj matrix
float4x4 prevViewProj = mul(prevView, prevProj);
float4 previousPosNDC = mul(float4(worldspace.xyz, 1.0), prevView);
previousPosNDC = mul(float4(previousPosNDC.xyz, 1.0), prevProj);
previousPosNDC.xy /= previousPosNDC.w;
float2 prevPositionSS = (previousPosNDC.xy * float2(0.5, -0.5) + float2(0.5, 0.5));
// Calculate velocity: current position in SS minus previous position SS
float4 ssPos = float4(texCoords.x, texCoords.y, depth, 1.0);
float2 velocity = ssPos.xy - prevPositionSS;
// Remove jitter before output
dst[DTid.xy] = float4(velocity, 0.0, 1.0);
}