From 3cd8b412244cf3db99036bd5c0783741e4941b96 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Tue, 9 Jul 2024 19:13:28 +0200 Subject: [PATCH] Updated upscaler constants definition and implemented modifications to the constants setup function. --- Assets/Scripts/Core/Fsr3Upscaler.cs | 25 +++++++----- Assets/Scripts/Core/Fsr3UpscalerContext.cs | 47 ++++++++++++++-------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/Assets/Scripts/Core/Fsr3Upscaler.cs b/Assets/Scripts/Core/Fsr3Upscaler.cs index 14145d1..8d72838 100644 --- a/Assets/Scripts/Core/Fsr3Upscaler.cs +++ b/Assets/Scripts/Core/Fsr3Upscaler.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023 Nico de Poel +// Copyright (c) 2024 Nico de Poel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -240,27 +240,30 @@ namespace FidelityFX internal struct UpscalerConstants { public Vector2Int renderSize; + public Vector2Int previousFrameRenderSize; + + public Vector2Int upscaleSize; + public Vector2Int previousFrameUpscaleSize; + public Vector2Int maxRenderSize; - public Vector2Int displaySize; - public Vector2Int inputColorResourceDimensions; - public Vector2Int lumaMipDimensions; - public int lumaMipLevelToUse; - public int frameIndex; + public Vector2Int maxUpscaleSize; public Vector4 deviceToViewDepth; + public Vector2 jitterOffset; + public Vector2 previousFrameJitterOffset; + public Vector2 motionVectorScale; public Vector2 downscaleFactor; + public Vector2 motionVectorJitterCancellation; - public float preExposure; - public float previousFramePreExposure; public float tanHalfFOV; public float jitterPhaseCount; + public float deltaTime; - public float dynamicResChangeFactor; + public float deltaPreExposure; public float viewSpaceToMetersFactor; - - public int dummy; + public float frameIndex; } [Serializable, StructLayout(LayoutKind.Sequential)] diff --git a/Assets/Scripts/Core/Fsr3UpscalerContext.cs b/Assets/Scripts/Core/Fsr3UpscalerContext.cs index 7dcc1d8..be5955e 100644 --- a/Assets/Scripts/Core/Fsr3UpscalerContext.cs +++ b/Assets/Scripts/Core/Fsr3UpscalerContext.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023 Nico de Poel +// Copyright (c) 2024 Nico de Poel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -70,8 +70,10 @@ namespace FidelityFX private ref Fsr3Upscaler.GenerateReactiveConstants2 TcrAutoGenConsts => ref _tcrAutogenerateConstantsArray[0]; private bool _firstExecution; - private Vector2 _previousJitterOffset; private int _resourceFrameIndex; + private Vector2 _previousJitterOffset; + private float _preExposure; + private float _previousFramePreExposure; public void Create(Fsr3Upscaler.ContextDescription contextDescription) { @@ -88,7 +90,7 @@ namespace FidelityFX _firstExecution = true; _resourceFrameIndex = 0; - UpscalerConsts.displaySize = _contextDescription.MaxUpscaleSize; + UpscalerConsts.maxUpscaleSize = _contextDescription.MaxUpscaleSize; _resources.Create(_contextDescription); CreatePasses(); @@ -309,11 +311,13 @@ namespace FidelityFX private void SetupConstants(Fsr3Upscaler.DispatchDescription dispatchParams, bool resetAccumulation) { ref Fsr3Upscaler.UpscalerConstants constants = ref UpscalerConsts; - + + constants.previousFrameJitterOffset = constants.jitterOffset; constants.jitterOffset = dispatchParams.JitterOffset; + + constants.previousFrameRenderSize = constants.renderSize; constants.renderSize = dispatchParams.RenderSize; constants.maxRenderSize = _contextDescription.MaxRenderSize; - constants.inputColorResourceDimensions = dispatchParams.UpscaleSize; // Compute the horizontal FOV for the shader from the vertical one float aspectRatio = (float)dispatchParams.RenderSize.x / dispatchParams.RenderSize.y; @@ -323,14 +327,32 @@ namespace FidelityFX // Compute params to enable device depth to view space depth computation in shader constants.deviceToViewDepth = SetupDeviceDepthToViewSpaceDepthParams(dispatchParams); + + constants.previousFrameUpscaleSize = constants.upscaleSize; + if (dispatchParams.UpscaleSize.x == 0 && dispatchParams.UpscaleSize.y == 0) + { + constants.upscaleSize = _contextDescription.MaxUpscaleSize; + } + else + { + constants.upscaleSize = dispatchParams.UpscaleSize; + } // To be updated if resource is larger than the actual image size constants.downscaleFactor = new Vector2((float)constants.renderSize.x / _contextDescription.MaxUpscaleSize.x, (float)constants.renderSize.y / _contextDescription.MaxUpscaleSize.y); - constants.previousFramePreExposure = constants.preExposure; - constants.preExposure = (dispatchParams.PreExposure != 0) ? dispatchParams.PreExposure : 1.0f; + + // Calculate pre-exposure relevant factors + constants.deltaPreExposure = 1.0f; + _previousFramePreExposure = _preExposure; + _preExposure = dispatchParams.PreExposure != 0.0f ? dispatchParams.PreExposure : 1.0f; + + if (_previousFramePreExposure > 0.0f) + { + constants.deltaPreExposure = _preExposure / _previousFramePreExposure; + } // Motion vector data - Vector2Int motionVectorsTargetSize = (_contextDescription.Flags & Fsr3Upscaler.InitializationFlags.EnableDisplayResolutionMotionVectors) != 0 ? constants.displaySize : constants.renderSize; + Vector2Int motionVectorsTargetSize = (_contextDescription.Flags & Fsr3Upscaler.InitializationFlags.EnableDisplayResolutionMotionVectors) != 0 ? constants.upscaleSize : constants.renderSize; constants.motionVectorScale = dispatchParams.MotionVectorScale / motionVectorsTargetSize; // Compute jitter cancellation @@ -360,14 +382,7 @@ namespace FidelityFX if (resetAccumulation) constants.frameIndex = 0; else - constants.frameIndex++; - - // Shading change usage of the SPD mip levels - constants.lumaMipLevelToUse = Fsr3UpscalerPass.ShadingChangeMipLevel; - - float mipDiv = 2 << constants.lumaMipLevelToUse; - constants.lumaMipDimensions.x = (int)(constants.maxRenderSize.x / mipDiv); - constants.lumaMipDimensions.y = (int)(constants.maxRenderSize.y / mipDiv); + constants.frameIndex += 1.0f; } private Vector4 SetupDeviceDepthToViewSpaceDepthParams(Fsr3Upscaler.DispatchDescription dispatchParams)