From 9f06f24943237566c1d259058e6eb5ed63147304 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 3 Aug 2024 14:46:07 +0200 Subject: [PATCH] Moved a number of shared static helper methods to a new FfxUtils class --- Runtime/Common/FfxUtils.cs | 52 +++++++++++++++++++++++++++ Runtime/Common/FfxUtils.cs.meta | 11 ++++++ Runtime/FSR2/Fsr2.cs | 45 ----------------------- Runtime/FSR2/Fsr2Context.cs | 2 +- Runtime/FSR2/Fsr2Resources.cs | 2 +- Runtime/FSR3/Fsr3Upscaler.cs | 45 ----------------------- Runtime/FSR3/Fsr3UpscalerContext.cs | 2 +- Runtime/FSR3/Fsr3UpscalerResources.cs | 2 +- 8 files changed, 67 insertions(+), 94 deletions(-) create mode 100644 Runtime/Common/FfxUtils.cs create mode 100644 Runtime/Common/FfxUtils.cs.meta diff --git a/Runtime/Common/FfxUtils.cs b/Runtime/Common/FfxUtils.cs new file mode 100644 index 0000000..4d442da --- /dev/null +++ b/Runtime/Common/FfxUtils.cs @@ -0,0 +1,52 @@ +using UnityEngine; + +namespace FidelityFX +{ + public static class FfxUtils + { + public static float GetMipmapBiasOffset(int renderWidth, int displayWidth) + { + return Mathf.Log((float)renderWidth / displayWidth, 2.0f) - 1.0f; + } + + public static int GetJitterPhaseCount(int renderWidth, int displayWidth) + { + const float basePhaseCount = 8.0f; + int jitterPhaseCount = (int)(basePhaseCount * Mathf.Pow((float)displayWidth / renderWidth, 2.0f)); + return jitterPhaseCount; + } + + public static void GetJitterOffset(out float outX, out float outY, int index, int phaseCount) + { + outX = Halton((index % phaseCount) + 1, 2) - 0.5f; + outY = Halton((index % phaseCount) + 1, 3) - 0.5f; + } + + // Calculate halton number for index and base. + private static float Halton(int index, int @base) + { + float f = 1.0f, result = 0.0f; + + for (int currentIndex = index; currentIndex > 0;) { + + f /= @base; + result += f * (currentIndex % @base); + currentIndex = (int)Mathf.Floor((float)currentIndex / @base); + } + + return result; + } + + public static float Lanczos2(float value) + { + return Mathf.Abs(value) < Mathf.Epsilon ? 1.0f : Mathf.Sin(Mathf.PI * value) / (Mathf.PI * value) * (Mathf.Sin(0.5f * Mathf.PI * value) / (0.5f * Mathf.PI * value)); + } + +#if !UNITY_2021_1_OR_NEWER + internal static void SetBufferData(this CommandBuffer commandBuffer, ComputeBuffer computeBuffer, Array data) + { + commandBuffer.SetComputeBufferData(computeBuffer, data); + } +#endif + } +} \ No newline at end of file diff --git a/Runtime/Common/FfxUtils.cs.meta b/Runtime/Common/FfxUtils.cs.meta new file mode 100644 index 0000000..8e2e1ff --- /dev/null +++ b/Runtime/Common/FfxUtils.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9c5c0f0b987cca64c8dfd8051e09962e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/FSR2/Fsr2.cs b/Runtime/FSR2/Fsr2.cs index 4632b96..85dccf4 100644 --- a/Runtime/FSR2/Fsr2.cs +++ b/Runtime/FSR2/Fsr2.cs @@ -89,51 +89,6 @@ namespace FidelityFX.FSR2 renderHeight = Mathf.RoundToInt(displayHeight / ratio); } - public static float GetMipmapBiasOffset(int renderWidth, int displayWidth) - { - return Mathf.Log((float)renderWidth / displayWidth, 2.0f) - 1.0f; - } - - public static int GetJitterPhaseCount(int renderWidth, int displayWidth) - { - const float basePhaseCount = 8.0f; - int jitterPhaseCount = (int)(basePhaseCount * Mathf.Pow((float)displayWidth / renderWidth, 2.0f)); - return jitterPhaseCount; - } - - public static void GetJitterOffset(out float outX, out float outY, int index, int phaseCount) - { - outX = Halton((index % phaseCount) + 1, 2) - 0.5f; - outY = Halton((index % phaseCount) + 1, 3) - 0.5f; - } - - // Calculate halton number for index and base. - private static float Halton(int index, int @base) - { - float f = 1.0f, result = 0.0f; - - for (int currentIndex = index; currentIndex > 0;) { - - f /= @base; - result += f * (currentIndex % @base); - currentIndex = (int)Mathf.Floor((float)currentIndex / @base); - } - - return result; - } - - public static float Lanczos2(float value) - { - return Mathf.Abs(value) < Mathf.Epsilon ? 1.0f : Mathf.Sin(Mathf.PI * value) / (Mathf.PI * value) * (Mathf.Sin(0.5f * Mathf.PI * value) / (0.5f * Mathf.PI * value)); - } - -#if !UNITY_2021_1_OR_NEWER - internal static void SetBufferData(this CommandBuffer commandBuffer, ComputeBuffer computeBuffer, Array data) - { - commandBuffer.SetComputeBufferData(computeBuffer, data); - } -#endif - public enum QualityMode { NativeAA = 0, diff --git a/Runtime/FSR2/Fsr2Context.cs b/Runtime/FSR2/Fsr2Context.cs index 38d380b..9603e6b 100644 --- a/Runtime/FSR2/Fsr2Context.cs +++ b/Runtime/FSR2/Fsr2Context.cs @@ -345,7 +345,7 @@ namespace FidelityFX.FSR2 _previousJitterOffset = constants.jitterOffset; } - int jitterPhaseCount = Fsr2.GetJitterPhaseCount(dispatchParams.RenderSize.x, _contextDescription.DisplaySize.x); + int jitterPhaseCount = FfxUtils.GetJitterPhaseCount(dispatchParams.RenderSize.x, _contextDescription.DisplaySize.x); if (resetAccumulation || constants.jitterPhaseCount == 0) { constants.jitterPhaseCount = jitterPhaseCount; diff --git a/Runtime/FSR2/Fsr2Resources.cs b/Runtime/FSR2/Fsr2Resources.cs index 4c1c50e..0314a45 100644 --- a/Runtime/FSR2/Fsr2Resources.cs +++ b/Runtime/FSR2/Fsr2Resources.cs @@ -55,7 +55,7 @@ namespace FidelityFX.FSR2 for (int currentLanczosWidthIndex = 0; currentLanczosWidthIndex < lanczos2LutWidth; ++currentLanczosWidthIndex) { float x = 2.0f * currentLanczosWidthIndex / (lanczos2LutWidth - 1); - float y = Fsr2.Lanczos2(x); + float y = FfxUtils.Lanczos2(x); lanczos2Weights[currentLanczosWidthIndex] = y; } diff --git a/Runtime/FSR3/Fsr3Upscaler.cs b/Runtime/FSR3/Fsr3Upscaler.cs index d69aa29..6547305 100644 --- a/Runtime/FSR3/Fsr3Upscaler.cs +++ b/Runtime/FSR3/Fsr3Upscaler.cs @@ -89,51 +89,6 @@ namespace FidelityFX.FSR3 renderHeight = Mathf.RoundToInt(displayHeight / ratio); } - public static float GetMipmapBiasOffset(int renderWidth, int displayWidth) - { - return Mathf.Log((float)renderWidth / displayWidth, 2.0f) - 1.0f; - } - - public static int GetJitterPhaseCount(int renderWidth, int displayWidth) - { - const float basePhaseCount = 8.0f; - int jitterPhaseCount = (int)(basePhaseCount * Mathf.Pow((float)displayWidth / renderWidth, 2.0f)); - return jitterPhaseCount; - } - - public static void GetJitterOffset(out float outX, out float outY, int index, int phaseCount) - { - outX = Halton((index % phaseCount) + 1, 2) - 0.5f; - outY = Halton((index % phaseCount) + 1, 3) - 0.5f; - } - - // Calculate halton number for index and base. - private static float Halton(int index, int @base) - { - float f = 1.0f, result = 0.0f; - - for (int currentIndex = index; currentIndex > 0;) { - - f /= @base; - result += f * (currentIndex % @base); - currentIndex = (int)Mathf.Floor((float)currentIndex / @base); - } - - return result; - } - - public static float Lanczos2(float value) - { - return Mathf.Abs(value) < Mathf.Epsilon ? 1.0f : Mathf.Sin(Mathf.PI * value) / (Mathf.PI * value) * (Mathf.Sin(0.5f * Mathf.PI * value) / (0.5f * Mathf.PI * value)); - } - -#if !UNITY_2021_1_OR_NEWER - internal static void SetBufferData(this CommandBuffer commandBuffer, ComputeBuffer computeBuffer, Array data) - { - commandBuffer.SetComputeBufferData(computeBuffer, data); - } -#endif - public enum QualityMode { NativeAA = 0, diff --git a/Runtime/FSR3/Fsr3UpscalerContext.cs b/Runtime/FSR3/Fsr3UpscalerContext.cs index 2b7f2ef..18fbced 100644 --- a/Runtime/FSR3/Fsr3UpscalerContext.cs +++ b/Runtime/FSR3/Fsr3UpscalerContext.cs @@ -386,7 +386,7 @@ namespace FidelityFX.FSR3 _previousJitterOffset = constants.jitterOffset; } - int jitterPhaseCount = Fsr3Upscaler.GetJitterPhaseCount(dispatchParams.RenderSize.x, _contextDescription.MaxUpscaleSize.x); + int jitterPhaseCount = FfxUtils.GetJitterPhaseCount(dispatchParams.RenderSize.x, _contextDescription.MaxUpscaleSize.x); if (resetAccumulation || constants.jitterPhaseCount == 0) { constants.jitterPhaseCount = jitterPhaseCount; diff --git a/Runtime/FSR3/Fsr3UpscalerResources.cs b/Runtime/FSR3/Fsr3UpscalerResources.cs index 112b98d..d591215 100644 --- a/Runtime/FSR3/Fsr3UpscalerResources.cs +++ b/Runtime/FSR3/Fsr3UpscalerResources.cs @@ -59,7 +59,7 @@ namespace FidelityFX.FSR3 for (int currentLanczosWidthIndex = 0; currentLanczosWidthIndex < lanczos2LutWidth; ++currentLanczosWidthIndex) { float x = 2.0f * currentLanczosWidthIndex / (lanczos2LutWidth - 1); - float y = Fsr3Upscaler.Lanczos2(x); + float y = FfxUtils.Lanczos2(x); lanczos2Weights[currentLanczosWidthIndex] = y; }