From 834abd0f0e72a9f71e8185c206463cb87d881493 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Fri, 9 May 2025 20:14:16 +0200 Subject: [PATCH] Ported changes from FidelityFX 1.1.4 --- Runtime/FSR3/Fsr3Upscaler.cs | 8 +++++++ Runtime/FSR3/Fsr3UpscalerContext.cs | 8 +++++++ Shaders/shaders/fsr2/ffx_fsr2_accumulate.h | 4 ++-- .../ffx_fsr3upscaler_accumulate.h | 4 ++-- .../ffx_fsr3upscaler_callbacks_hlsl.h | 22 +++++++++++++++---- .../ffx_fsr3upscaler_prepare_reactivity.h | 19 +++++++++++++--- 6 files changed, 54 insertions(+), 11 deletions(-) diff --git a/Runtime/FSR3/Fsr3Upscaler.cs b/Runtime/FSR3/Fsr3Upscaler.cs index 6b3ad77..971ea28 100644 --- a/Runtime/FSR3/Fsr3Upscaler.cs +++ b/Runtime/FSR3/Fsr3Upscaler.cs @@ -201,6 +201,10 @@ namespace FidelityFX.FSR3 public float CameraFovAngleVertical; public float ViewSpaceToMetersFactor; public float VelocityFactor = 1.0f; + public float ReactivenessScale = 1.0f; + public float ShadingChangeScale = 1.0f; + public float AccumulationAddedPerFrame = 1.0f / 3.0f; + public float MinDisocclusionAccumulation = -1.0f / 3.0f; public DispatchFlags Flags; public bool UseTextureArrays; // Enable texture array bindings, primarily used for HDRP and XR @@ -268,6 +272,10 @@ namespace FidelityFX.FSR3 public float frameIndex; public float velocityFactor; + public float reactivenessScale; + public float shadingChangeScale; + public float accumulationAddedPerFrame; + public float minDisocclusionAccumulation; } [Serializable, StructLayout(LayoutKind.Sequential)] diff --git a/Runtime/FSR3/Fsr3UpscalerContext.cs b/Runtime/FSR3/Fsr3UpscalerContext.cs index 07f1af7..e4a3fd9 100644 --- a/Runtime/FSR3/Fsr3UpscalerContext.cs +++ b/Runtime/FSR3/Fsr3UpscalerContext.cs @@ -97,6 +97,10 @@ namespace FidelityFX.FSR3 UpscalerConsts.maxUpscaleSize = _contextDescription.MaxUpscaleSize; UpscalerConsts.velocityFactor = 1.0f; + UpscalerConsts.reactivenessScale = 1.0f; + UpscalerConsts.shadingChangeScale = 1.0f; + UpscalerConsts.accumulationAddedPerFrame = 1.0f / 3.0f; + UpscalerConsts.minDisocclusionAccumulation = -1.0f / 3.0f; _resources.Create(_contextDescription); CreatePasses(); @@ -410,6 +414,10 @@ namespace FidelityFX.FSR3 constants.frameIndex += 1.0f; constants.velocityFactor = dispatchParams.VelocityFactor; + constants.reactivenessScale = dispatchParams.ReactivenessScale; + constants.shadingChangeScale = dispatchParams.ShadingChangeScale; + constants.accumulationAddedPerFrame = dispatchParams.AccumulationAddedPerFrame; + constants.minDisocclusionAccumulation = dispatchParams.MinDisocclusionAccumulation; } private Vector4 SetupDeviceDepthToViewSpaceDepthParams(Fsr3Upscaler.DispatchDescription dispatchParams) diff --git a/Shaders/shaders/fsr2/ffx_fsr2_accumulate.h b/Shaders/shaders/fsr2/ffx_fsr2_accumulate.h index d87c748..71b9d26 100644 --- a/Shaders/shaders/fsr2/ffx_fsr2_accumulate.h +++ b/Shaders/shaders/fsr2/ffx_fsr2_accumulate.h @@ -66,8 +66,8 @@ void RectifyHistory( { FfxFloat32 fScaleFactorInfluence = ffxMin(20.0f, ffxPow(FfxFloat32(1.0f / length(DownscaleFactor().x * DownscaleFactor().y)), 3.0f)); - const FfxFloat32 fVecolityFactor = ffxSaturate(params.fHrVelocity / 20.0f); - const FfxFloat32 fBoxScaleT = ffxMax(params.fDepthClipFactor, ffxMax(params.fAccumulationMask, fVecolityFactor)); + const FfxFloat32 fHrVelocityFactor = ffxSaturate(params.fHrVelocity / 20.0f); + const FfxFloat32 fBoxScaleT = ffxMax(params.fDepthClipFactor, ffxMax(params.fAccumulationMask, fHrVelocityFactor)); FfxFloat32 fBoxScale = ffxLerp(fScaleFactorInfluence, 1.0f, fBoxScaleT); FfxFloat32x3 fScaledBoxVec = clippingBox.boxVec * fBoxScale; diff --git a/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_accumulate.h b/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_accumulate.h index 084d4d3..1a231aa 100644 --- a/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_accumulate.h +++ b/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_accumulate.h @@ -46,12 +46,12 @@ void RectifyHistory( FFX_PARAMETER_INOUT AccumulationPassData data ) { - const FfxFloat32 fVecolityFactor = ffxSaturate(params.f4KVelocity / 20.0f); + const FfxFloat32 f4kVelocityFactor = ffxSaturate(params.f4KVelocity / 20.0f); const FfxFloat32 fDistanceFactor = ffxSaturate(0.75f - params.fFarthestDepthInMeters / 20.0f); const FfxFloat32 fAccumulationFactor = 1.0f - params.fAccumulation; const FfxFloat32 fReactiveFactor = ffxPow(params.fReactiveMask, 1.0f / 2.0f); const FfxFloat32 fShadingChangeFactor = params.fShadingChange; - const FfxFloat32 fBoxScaleT = ffxMax(fVecolityFactor, ffxMax(fDistanceFactor, ffxMax(fAccumulationFactor, ffxMax(fReactiveFactor, fShadingChangeFactor)))); + const FfxFloat32 fBoxScaleT = ffxMax(f4kVelocityFactor, ffxMax(fDistanceFactor, ffxMax(fAccumulationFactor, ffxMax(fReactiveFactor, fShadingChangeFactor)))); const FfxFloat32 fBoxScale = ffxLerp(3.0f, 1.0f, fBoxScaleT); const FfxFloat32x3 fScaledBoxVec = data.clippingBox.boxVec * FfxFloat32x3(1.7f, 1.0f, 1.0f) * fBoxScale; diff --git a/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_callbacks_hlsl.h b/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_callbacks_hlsl.h index 2b2811e..6f616e3 100644 --- a/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_callbacks_hlsl.h +++ b/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_callbacks_hlsl.h @@ -77,6 +77,10 @@ cbuffer cbFSR3Upscaler : FFX_FSR3UPSCALER_DECLARE_CB(FSR3UPSCALER_BIND_CB_FSR3UP FfxFloat32 fFrameIndex; FfxFloat32 fVelocityFactor; + FfxFloat32 fReactivenessScale; + FfxFloat32 fShadingChangeScale; + FfxFloat32 fAccumulationAddedPerFrame; + FfxFloat32 fMinDisocclusionAccumulation; }; #define FFX_FSR3UPSCALER_CONSTANT_BUFFER_1_SIZE (sizeof(cbFSR3Upscaler) / 4) // Number of 32-bit values. This must be kept in sync with the cbFSR3Upscaler size. @@ -177,6 +181,16 @@ FfxFloat32 VelocityFactor() return fVelocityFactor; } +FfxFloat32 AccumulationAddedPerFrame() +{ + return fAccumulationAddedPerFrame; +} + +FfxFloat32 MinDisocclusionAccumulation() +{ + return fMinDisocclusionAccumulation; +} + #endif // #if defined(FSR3UPSCALER_BIND_CB_FSR3UPSCALER) #define FFX_FSR3UPSCALER_ROOTSIG_STRINGIFY(p) FFX_FSR3UPSCALER_ROOTSIG_STR(p) @@ -384,7 +398,7 @@ UNITY_FSR_TEX2D(FfxFloat32) r_reactive_mask : FFX_FSR3UPSCALER_DECLARE_SRV(FSR3U FfxFloat32 LoadReactiveMask(FfxUInt32x2 iPxPos) { - return r_reactive_mask[UNITY_FSR_POS(iPxPos)]; + return r_reactive_mask[UNITY_FSR_POS(iPxPos)] * fReactivenessScale; } FfxInt32x2 GetReactiveMaskResourceDimensions() @@ -398,7 +412,7 @@ FfxInt32x2 GetReactiveMaskResourceDimensions() FfxFloat32 SampleReactiveMask(FfxFloat32x2 fUV) { - return r_reactive_mask.SampleLevel(s_LinearClamp, UNITY_FSR_UV(fUV), 0).x; + return r_reactive_mask.SampleLevel(s_LinearClamp, UNITY_FSR_UV(fUV), 0).x * fReactivenessScale; } #endif @@ -553,12 +567,12 @@ Texture2D r_shading_change : FFX_FSR3UPSCALER_DECLARE_SRV(FSR3UPSCAL FfxFloat32 LoadShadingChange(FfxUInt32x2 iPxPos) { - return r_shading_change[iPxPos]; + return r_shading_change[iPxPos] * fShadingChangeScale; } FfxFloat32 SampleShadingChange(FfxFloat32x2 fUV) { - return r_shading_change.SampleLevel(s_LinearClamp, fUV, 0); + return r_shading_change.SampleLevel(s_LinearClamp, fUV, 0) * fShadingChangeScale; } #endif diff --git a/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_prepare_reactivity.h b/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_prepare_reactivity.h index fa9571d..ae24545 100644 --- a/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_prepare_reactivity.h +++ b/Shaders/shaders/fsr3upscaler/ffx_fsr3upscaler_prepare_reactivity.h @@ -210,15 +210,28 @@ FfxFloat32 UpdateAccumulation(FfxInt32x2 iPxPos, FfxFloat32x2 fUv, FfxFloat32x2 const FfxFloat32x2 fReprojectedUv_HW = ClampUv(fReprojectedUv, PreviousFrameRenderSize(), MaxRenderSize()); fAccumulation = ffxSaturate(SampleAccumulation(fReprojectedUv_HW)); } + const FfxFloat32 fAccumulationAddedPerFrame= AccumulationAddedPerFrame(); //default is 0.333 + // Assume at frame N+0 fShadingChange is 1.0, and all subsequent frames fShadingChange is 0.0 and fDisocclusion is 0.0. Then, + // frame N+0 fAccumulation will be 0.000 + // frame N+2 fAccumulation will be 0.000 + 0.333 * 1 == 0.333 + // frame N+3 fAccumulation will be 0.000 + 0.333 * 2 == 0.666 + // frame N+4 fAccumulation will be 0.000 + 0.333 * 3 == 0.999 fAccumulation = ffxLerp(fAccumulation, 0.0f, fShadingChange); - fAccumulation = ffxLerp(fAccumulation, ffxMin(fAccumulation, 0.25f), fDisocclusion); + + const FfxFloat32 fMinDisocclusionAccumulation = MinDisocclusionAccumulation(); //default is -0.333 + // Assume at frame N+0 fDisocclusion is 1.0, and all subsequent frames fShadingChange is 0.0 and fDisocclusion is 0.0. Then, + // frame N+0 fAccumulation will be -0.333f (but normalized to store in unorm) + // frame N+1 fAccumulation will be -0.333f + 0.333 * 1 == 0.000 + // frame N+2 fAccumulation will be -0.333f + 0.333 * 2 == 0.333 + // frame N+3 fAccumulation will be -0.333f + 0.333 * 3 == 0.666 + // frame N+4 fAccumulation will be -0.333f + 0.333 * 4 == 0.999 + fAccumulation = ffxLerp(fAccumulation, ffxMin(fMinDisocclusionAccumulation, fAccumulation), fDisocclusion); fAccumulation *= FfxFloat32(round(fAccumulation * 100.0f) > 1.0f); // Update for next frame, normalize to store in unorm - const FfxFloat32 fAccumulatedFramesMax = 3.0f; - const FfxFloat32 fAccumulatedFramesToStore = ffxSaturate(fAccumulation + (1.0f / fAccumulatedFramesMax)); + const FfxFloat32 fAccumulatedFramesToStore = ffxSaturate(fAccumulation + fAccumulationAddedPerFrame); StoreAccumulation(iPxPos, fAccumulatedFramesToStore); return fAccumulation;