From 903c2f71f3bb30e050c5a1fc7e56ac3f514e825f Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 26 Feb 2023 15:12:23 +0100 Subject: [PATCH] Updated code to make it compatible with Unity 2020.1, i.e. the oldest version that's technically usable. --- Assets/Scripts/Fsr2Context.cs | 44 ++++++++++++++++---------------- Assets/Scripts/Fsr2Controller.cs | 10 -------- Assets/Scripts/Fsr2Pipeline.cs | 26 +++++++++++-------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/Assets/Scripts/Fsr2Context.cs b/Assets/Scripts/Fsr2Context.cs index 7e51666..3e5c2c1 100644 --- a/Assets/Scripts/Fsr2Context.cs +++ b/Assets/Scripts/Fsr2Context.cs @@ -353,29 +353,29 @@ namespace FidelityFX /// The FSR2 C++ codebase uses floats bitwise converted to ints to pass sharpness parameters to the RCAS shader. /// This is not possible in C# without enabling unsafe code compilation, so to avoid that we instead use a table of precomputed values. /// - private static readonly List RcasConfigs = new() + private static readonly List RcasConfigs = new List() { - new(1048576000u, 872428544u), - new(1049178080u, 877212745u), - new(1049823372u, 882390168u), - new(1050514979u, 887895276u), - new(1051256227u, 893859143u), - new(1052050675u, 900216232u), - new(1052902144u, 907032080u), - new(1053814727u, 914306687u), - new(1054792807u, 922105590u), - new(1055841087u, 930494326u), - new(1056964608u, 939538432u), - new(1057566688u, 944322633u), - new(1058211980u, 949500056u), - new(1058903587u, 955005164u), - new(1059644835u, 960969031u), - new(1060439283u, 967326120u), - new(1061290752u, 974141968u), - new(1062203335u, 981416575u), - new(1063181415u, 989215478u), - new(1064229695u, 997604214u), - new(1065353216u, 1006648320), + new Fsr2.RcasConstants(1048576000u, 872428544u), + new Fsr2.RcasConstants(1049178080u, 877212745u), + new Fsr2.RcasConstants(1049823372u, 882390168u), + new Fsr2.RcasConstants(1050514979u, 887895276u), + new Fsr2.RcasConstants(1051256227u, 893859143u), + new Fsr2.RcasConstants(1052050675u, 900216232u), + new Fsr2.RcasConstants(1052902144u, 907032080u), + new Fsr2.RcasConstants(1053814727u, 914306687u), + new Fsr2.RcasConstants(1054792807u, 922105590u), + new Fsr2.RcasConstants(1055841087u, 930494326u), + new Fsr2.RcasConstants(1056964608u, 939538432u), + new Fsr2.RcasConstants(1057566688u, 944322633u), + new Fsr2.RcasConstants(1058211980u, 949500056u), + new Fsr2.RcasConstants(1058903587u, 955005164u), + new Fsr2.RcasConstants(1059644835u, 960969031u), + new Fsr2.RcasConstants(1060439283u, 967326120u), + new Fsr2.RcasConstants(1061290752u, 974141968u), + new Fsr2.RcasConstants(1062203335u, 981416575u), + new Fsr2.RcasConstants(1063181415u, 989215478u), + new Fsr2.RcasConstants(1064229695u, 997604214u), + new Fsr2.RcasConstants(1065353216u, 1006648320), }; private static ComputeBuffer CreateConstantBuffer() where TConstants: struct diff --git a/Assets/Scripts/Fsr2Controller.cs b/Assets/Scripts/Fsr2Controller.cs index 0db4bfa..83761dd 100644 --- a/Assets/Scripts/Fsr2Controller.cs +++ b/Assets/Scripts/Fsr2Controller.cs @@ -68,8 +68,6 @@ public class Fsr2Controller : MonoBehaviour if (!_started) return; - RenderPipelineManager.endContextRendering += OnEndContextRendering; - _context = Fsr2.CreateContext(DisplaySize, RenderSize, Fsr2.InitializationFlags.EnableMotionVectorsJitterCancellation | Fsr2.InitializationFlags.EnableFP16Usage); // TODO: do we need a depth buffer for the output? We will need depth & motion vectors for subsequent post-FX. How should FSR2 output these? @@ -91,8 +89,6 @@ public class Fsr2Controller : MonoBehaviour _context.Destroy(); _context = null; } - - RenderPipelineManager.endContextRendering -= OnEndContextRendering; } public void SetJitterOffset(Vector2 jitterOffset) @@ -100,12 +96,6 @@ public class Fsr2Controller : MonoBehaviour _dispatchDescription.JitterOffset = jitterOffset; } - // For scriptable rendering pipeline - private void OnEndContextRendering(ScriptableRenderContext context, List cameras) - { - Debug.Log($"OnEndContentRendering, cameras = {string.Join(", ", cameras.Select(c => c.name))}"); - } - // For legacy built-in render pipeline private void OnRenderImage(RenderTexture src, RenderTexture dest) { diff --git a/Assets/Scripts/Fsr2Pipeline.cs b/Assets/Scripts/Fsr2Pipeline.cs index d7ee0f1..f04bd10 100644 --- a/Assets/Scripts/Fsr2Pipeline.cs +++ b/Assets/Scripts/Fsr2Pipeline.cs @@ -87,25 +87,25 @@ namespace FidelityFX // TODO: we could potentially gather *all* resource binding here, by using CommandBuffer.SetGlobalTexture for everything // Resource FSR2_SpdAtomicCounter: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_UINT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(UavSpdAtomicCount, 1, 1, 0, default(FilterMode), GraphicsFormat.R32_UInt, 1, true); + commandBuffer.GetTemporaryRT(UavSpdAtomicCount, 1, 1, 0, default, GraphicsFormat.R32_UInt, 1, true); // FSR2_ReconstructedPrevNearestDepth: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_UINT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(UavReconstructedPrevNearestDepth, maxRenderSize.x, maxRenderSize.y, 0, default(FilterMode), GraphicsFormat.R32_UInt, 1, true); + commandBuffer.GetTemporaryRT(UavReconstructedPrevNearestDepth, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R32_UInt, 1, true); // FSR2_DilatedDepth: FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(UavDilatedDepth, maxRenderSize.x, maxRenderSize.y, 0, default(FilterMode), GraphicsFormat.R32_SFloat, 1, true); + commandBuffer.GetTemporaryRT(UavDilatedDepth, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R32_SFloat, 1, true); // FSR2_LockInputLuma: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R16_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(UavLockInputLuma, maxRenderSize.x, maxRenderSize.y, 0, default(FilterMode), GraphicsFormat.R16_SFloat, 1, true); + commandBuffer.GetTemporaryRT(UavLockInputLuma, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R16_SFloat, 1, true); // FSR2_DilatedReactiveMasks: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R8G8_UNORM, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(UavDilatedReactiveMasks, maxRenderSize.x, maxRenderSize.y, 0, default(FilterMode), GraphicsFormat.R8G8_UNorm, 1, true); + commandBuffer.GetTemporaryRT(UavDilatedReactiveMasks, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R8G8_UNorm, 1, true); // FSR2_PreparedInputColor: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(UavPreparedInputColor, maxRenderSize.x, maxRenderSize.y, 0, default(FilterMode), GraphicsFormat.R16G16B16A16_SFloat, 1, true); + commandBuffer.GetTemporaryRT(UavPreparedInputColor, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R16G16B16A16_SFloat, 1, true); // FSR2_NewLocks: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R8_UNORM, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(UavNewLocks, displaySize.x, displaySize.y, 0, default(FilterMode), GraphicsFormat.R8_UNorm, 1, true); + commandBuffer.GetTemporaryRT(UavNewLocks, displaySize.x, displaySize.y, 0, default, GraphicsFormat.R8_UNorm, 1, true); } public static void UnregisterResources(CommandBuffer commandBuffer) @@ -132,7 +132,13 @@ namespace FidelityFX kernelIndex = shaderRef.FindKernel("CS"); - bool useLut = (SystemInfo.computeSubGroupSize == 64); + bool useLut = false; +#if UNITY_2020_3_OR_NEWER + if (SystemInfo.computeSubGroupSize == 64) + { + useLut = true; + } +#endif // Allow 16-bit floating point as a configuration option, except on passes that explicitly disable it bool supportedFP16 = ((flags & Fsr2.InitializationFlags.EnableFP16Usage) != 0 && AllowFP16); @@ -300,9 +306,9 @@ namespace FidelityFX // Simply loading the accumulate_pass compute shader will give us the same instance as the non-sharpen pipeline // So we have to clone the shader instance and set the extra keyword on the new copy _shaderCopy = UnityEngine.Object.Instantiate(ComputeShader); - foreach (var keyword in ComputeShader.enabledKeywords) + foreach (var keyword in ComputeShader.shaderKeywords) { - _shaderCopy.EnableKeyword(keyword.name); + _shaderCopy.EnableKeyword(keyword); } _shaderCopy.EnableKeyword("FFX_FSR2_OPTION_APPLY_SHARPENING"); }