diff --git a/assets/shaders/fsr2_accumulate.comp.glsl b/assets/shaders/fsr2_accumulate.comp.glsl index 7fb0cb27..44e7c2ad 100644 --- a/assets/shaders/fsr2_accumulate.comp.glsl +++ b/assets/shaders/fsr2_accumulate.comp.glsl @@ -29,20 +29,84 @@ vec3 yCoCgToRgb(vec3 ycocg) { return vec3(y + co - cg, y + cg, y - co - cg); } +// Catmull-Rom bicubic (9 bilinear taps) with anti-ringing clamp. +// Sharper than bilinear; anti-ringing prevents edge halos that shift with jitter. +vec3 sampleBicubic(sampler2D tex, vec2 uv, vec2 texSize) { + vec2 invTexSize = 1.0 / texSize; + vec2 iTc = uv * texSize; + vec2 tc = floor(iTc - 0.5) + 0.5; + vec2 f = iTc - tc; + + // Catmull-Rom weights + vec2 w0 = f * (-0.5 + f * (1.0 - 0.5 * f)); + vec2 w1 = 1.0 + f * f * (-2.5 + 1.5 * f); + vec2 w2 = f * (0.5 + f * (2.0 - 1.5 * f)); + vec2 w3 = f * f * (-0.5 + 0.5 * f); + + vec2 s12 = w1 + w2; + vec2 offset12 = w2 / s12; + + vec2 tc0 = (tc - 1.0) * invTexSize; + vec2 tc3 = (tc + 2.0) * invTexSize; + vec2 tc12 = (tc + offset12) * invTexSize; + + // 3x3 bilinear taps covering 4x4 texel support + vec3 result = + (texture(tex, vec2(tc0.x, tc0.y)).rgb * w0.x + + texture(tex, vec2(tc12.x, tc0.y)).rgb * s12.x + + texture(tex, vec2(tc3.x, tc0.y)).rgb * w3.x) * w0.y + + (texture(tex, vec2(tc0.x, tc12.y)).rgb * w0.x + + texture(tex, vec2(tc12.x, tc12.y)).rgb * s12.x + + texture(tex, vec2(tc3.x, tc12.y)).rgb * w3.x) * s12.y + + (texture(tex, vec2(tc0.x, tc3.y)).rgb * w0.x + + texture(tex, vec2(tc12.x, tc3.y)).rgb * s12.x + + texture(tex, vec2(tc3.x, tc3.y)).rgb * w3.x) * w3.y; + + // Anti-ringing: clamp to range of the 4 nearest texels. + // Prevents Catmull-Rom negative lobe overshoots at high-contrast edges. + vec2 tcNear = tc * invTexSize; + vec3 t00 = texture(tex, tcNear).rgb; + vec3 t10 = texture(tex, tcNear + vec2(invTexSize.x, 0.0)).rgb; + vec3 t01 = texture(tex, tcNear + vec2(0.0, invTexSize.y)).rgb; + vec3 t11 = texture(tex, tcNear + invTexSize).rgb; + vec3 minC = min(min(t00, t10), min(t01, t11)); + vec3 maxC = max(max(t00, t10), max(t01, t11)); + return clamp(result, minC, maxC); +} + void main() { ivec2 outPixel = ivec2(gl_GlobalInvocationID.xy); ivec2 outSize = ivec2(pc.displaySize.xy); if (outPixel.x >= outSize.x || outPixel.y >= outSize.y) return; vec2 outUV = (vec2(outPixel) + 0.5) * pc.displaySize.zw; - vec3 currentColor = texture(sceneColor, outUV).rgb; + + // Bicubic upsampling with anti-ringing: sharp without edge halos + vec3 currentColor = sampleBicubic(sceneColor, outUV, pc.internalSize.xy); if (pc.params.x > 0.5) { imageStore(historyOutput, outPixel, vec4(currentColor, 1.0)); return; } - vec2 motion = texture(motionVectors, outUV).rg; + // Depth-dilated motion vector: pick the MV from the nearest-to-camera + // pixel in a 3x3 neighborhood. Prevents background MVs from bleeding + // over foreground edges. + vec2 texelSize = pc.internalSize.zw; + float closestDepth = texture(depthBuffer, outUV).r; + vec2 closestOffset = vec2(0.0); + for (int y = -1; y <= 1; y++) { + for (int x = -1; x <= 1; x++) { + vec2 off = vec2(float(x), float(y)) * texelSize; + float d = texture(depthBuffer, outUV + off).r; + if (d < closestDepth) { + closestDepth = d; + closestOffset = off; + } + } + } + vec2 motion = texture(motionVectors, outUV + closestOffset).rg; + vec2 historyUV = outUV + motion; float historyValid = (historyUV.x >= 0.0 && historyUV.x <= 1.0 && @@ -50,8 +114,9 @@ void main() { vec3 historyColor = texture(historyInput, historyUV).rgb; - // Neighborhood clamping in YCoCg space - vec2 texelSize = pc.internalSize.zw; + // Neighborhood clamping in YCoCg space with wide gamma. + // Wide gamma (3.0) prevents jitter-chasing: the clamp box only catches + // truly stale history (disocclusion), not normal jitter variation. vec3 s0 = rgbToYCoCg(currentColor); vec3 s1 = rgbToYCoCg(texture(sceneColor, outUV + vec2(-texelSize.x, 0.0)).rgb); vec3 s2 = rgbToYCoCg(texture(sceneColor, outUV + vec2( texelSize.x, 0.0)).rgb); @@ -68,7 +133,7 @@ void main() { vec3 variance = max(m2 / 9.0 - mean * mean, vec3(0.0)); vec3 stddev = sqrt(variance); - float gamma = 1.5; + float gamma = 3.0; vec3 boxMin = mean - gamma * stddev; vec3 boxMax = mean + gamma * stddev; @@ -77,7 +142,19 @@ void main() { historyColor = yCoCgToRgb(clampedHistory); float clampDist = length(historyYCoCg - clampedHistory); - float blendFactor = mix(0.05, 0.30, clamp(clampDist * 2.0, 0.0, 1.0)); + + // Uniform 5% blend: ~45 frames for 90% convergence. + // Simpler than edge-aware; the anti-ringing bicubic handles edge stability. + float blendFactor = 0.05; + + // Disocclusion: large clamp distance → rapidly replace stale history + blendFactor = mix(blendFactor, 0.60, clamp(clampDist * 5.0, 0.0, 1.0)); + + // Velocity: higher blend during motion reduces ghosting + float motionMag = length(motion * pc.displaySize.xy); + blendFactor = max(blendFactor, clamp(motionMag * 0.15, 0.0, 0.35)); + + // Full current frame when history is out of bounds blendFactor = mix(blendFactor, 1.0, 1.0 - historyValid); vec3 result = mix(historyColor, currentColor, blendFactor); diff --git a/assets/shaders/fsr2_accumulate.comp.spv b/assets/shaders/fsr2_accumulate.comp.spv index 47529d75..f69e0f3b 100644 Binary files a/assets/shaders/fsr2_accumulate.comp.spv and b/assets/shaders/fsr2_accumulate.comp.spv differ diff --git a/assets/shaders/fsr2_motion.comp.glsl b/assets/shaders/fsr2_motion.comp.glsl index b0b39375..9c4e8fab 100644 --- a/assets/shaders/fsr2_motion.comp.glsl +++ b/assets/shaders/fsr2_motion.comp.glsl @@ -8,6 +8,7 @@ layout(set = 0, binding = 1, rg16f) uniform writeonly image2D motionVectors; layout(push_constant) uniform PushConstants { mat4 reprojMatrix; // prevUnjitteredVP * inverse(currentUnjitteredVP) vec4 resolution; // xy = internal size, zw = 1/internal size + vec4 jitterOffset; // xy = current jitter (NDC), zw = unused } pc; void main() { @@ -15,21 +16,30 @@ void main() { ivec2 imgSize = ivec2(pc.resolution.xy); if (pixelCoord.x >= imgSize.x || pixelCoord.y >= imgSize.y) return; - // Sample depth (Vulkan: 0 = near, 1 = far) float depth = texelFetch(depthBuffer, pixelCoord, 0).r; - // Pixel center in UV [0,1] and NDC [-1,1] + // Pixel center UV and NDC vec2 uv = (vec2(pixelCoord) + 0.5) * pc.resolution.zw; vec2 ndc = uv * 2.0 - 1.0; - // Clip-to-clip reprojection: current unjittered clip → previous unjittered clip - vec4 clipPos = vec4(ndc, depth, 1.0); + // Unjitter the NDC: the scene was rendered with jitter applied to + // projection[2][0/1]. For RH perspective (P[2][3]=-1, clip.w=-vz): + // jittered_ndc = unjittered_ndc - jitter + // unjittered_ndc = ndc + jitter + vec2 unjitteredNDC = ndc + pc.jitterOffset.xy; + + // Reproject to previous frame via unjittered VP matrices + vec4 clipPos = vec4(unjitteredNDC, depth, 1.0); vec4 prevClip = pc.reprojMatrix * clipPos; vec2 prevNdc = prevClip.xy / prevClip.w; vec2 prevUV = prevNdc * 0.5 + 0.5; - // Motion = previous position - current position (both unjittered, in UV space) - vec2 motion = prevUV - uv; + // Current unjittered UV for this pixel's world content + vec2 currentUnjitteredUV = unjitteredNDC * 0.5 + 0.5; + + // Motion between unjittered positions — jitter-free. + // For a static scene (identity reprojMatrix), this is exactly zero. + vec2 motion = prevUV - currentUnjitteredUV; imageStore(motionVectors, pixelCoord, vec4(motion, 0.0, 0.0)); } diff --git a/assets/shaders/fsr2_motion.comp.spv b/assets/shaders/fsr2_motion.comp.spv index faa3d836..8a50542d 100644 Binary files a/assets/shaders/fsr2_motion.comp.spv and b/assets/shaders/fsr2_motion.comp.spv differ diff --git a/src/rendering/renderer.cpp b/src/rendering/renderer.cpp index 063bae9a..c3449f93 100644 --- a/src/rendering/renderer.cpp +++ b/src/rendering/renderer.cpp @@ -3785,7 +3785,7 @@ bool Renderer::initFSR2Resources() { VkPushConstantRange pc{}; pc.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; pc.offset = 0; - pc.size = sizeof(glm::mat4) + sizeof(glm::vec4); // 80 bytes + pc.size = sizeof(glm::mat4) + 2 * sizeof(glm::vec4); // 96 bytes VkPipelineLayoutCreateInfo plCI{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; plCI.setLayoutCount = 1; @@ -4086,17 +4086,20 @@ void Renderer::dispatchMotionVectors() { vkCmdBindDescriptorSets(currentCmd, VK_PIPELINE_BIND_POINT_COMPUTE, fsr2_.motionVecPipelineLayout, 0, 1, &fsr2_.motionVecDescSet, 0, nullptr); - // Single reprojection matrix: prevUnjitteredVP * inv(currentUnjitteredVP) - // Both matrices are unjittered — jitter only affects sub-pixel sampling, - // not motion vector computation. This avoids numerical instability from - // jitter amplification through large world coordinates. + // Reprojection: prevUnjitteredVP * inv(currentUnjitteredVP) + // Using unjittered VPs avoids numerical instability from jitter amplification + // through large world coordinates. The shader corrects NDC by subtracting + // current jitter before reprojection (depth was rendered at jittered position). struct { - glm::mat4 reprojMatrix; // prevUnjitteredVP * inv(currentUnjitteredVP) + glm::mat4 reprojMatrix; glm::vec4 resolution; + glm::vec4 jitterOffset; // xy = current jitter (NDC), zw = unused } pc; glm::mat4 currentUnjitteredVP = camera->getUnjitteredViewProjectionMatrix(); pc.reprojMatrix = fsr2_.prevViewProjection * glm::inverse(currentUnjitteredVP); + glm::vec2 jitter = camera->getJitter(); + pc.jitterOffset = glm::vec4(jitter.x, jitter.y, 0.0f, 0.0f); pc.resolution = glm::vec4( static_cast(fsr2_.internalWidth), static_cast(fsr2_.internalHeight),