diff --git a/Assets/Fsr3UpscalerAssets.asset b/Assets/Fsr3UpscalerAssets.asset new file mode 100644 index 0000000..f3f30ea --- /dev/null +++ b/Assets/Fsr3UpscalerAssets.asset @@ -0,0 +1,23 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: db26e15a33db6ab42a38daab0ba2712f, type: 3} + m_Name: Fsr3UpscalerAssets + m_EditorClassIdentifier: + shaders: + computeLuminancePyramidPass: {fileID: 7200000, guid: d253be05abcdc80428503d3e4cce3a36, type: 3} + reconstructPreviousDepthPass: {fileID: 7200000, guid: 4f59e5b9179d74844ae06a30ae1e0629, type: 3} + depthClipPass: {fileID: 7200000, guid: 20e44016ed34b0d4b8de499d1b566c69, type: 3} + lockPass: {fileID: 7200000, guid: a135306e6d1857e43a86ef20db2a47fe, type: 3} + accumulatePass: {fileID: 7200000, guid: c9b45f0ae7673694ba57a4aadfe212e9, type: 3} + sharpenPass: {fileID: 7200000, guid: 7aaf5cfff022de2499e9b0412f947f6c, type: 3} + autoGenReactivePass: {fileID: 7200000, guid: 5716b91fdaa4e9e439df6b96a796fe6e, type: 3} + tcrAutoGenPass: {fileID: 7200000, guid: 75cdc6ef23f08ed498d4da511923fcea, type: 3} diff --git a/Assets/Shaders/FSR3/Fsr3UpscalerShaders.asset.meta b/Assets/Fsr3UpscalerAssets.asset.meta similarity index 100% rename from Assets/Shaders/FSR3/Fsr3UpscalerShaders.asset.meta rename to Assets/Fsr3UpscalerAssets.asset.meta diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 00d0521..265428e 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -337,7 +337,7 @@ MonoBehaviour: autoTcScale: 1 autoReactiveScale: 5 autoReactiveMax: 0.9 - shaders: {fileID: 11400000, guid: 4151befafd86e8740ab09463b4f1bdbb, type: 2} + assets: {fileID: 11400000, guid: 4151befafd86e8740ab09463b4f1bdbb, type: 2} --- !u!114 &963194230 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Core/Fsr3UpscalerAssets.cs b/Assets/Scripts/Core/Fsr3UpscalerAssets.cs new file mode 100644 index 0000000..3e4e24f --- /dev/null +++ b/Assets/Scripts/Core/Fsr3UpscalerAssets.cs @@ -0,0 +1,151 @@ +// Copyright (c) 2023 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 +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using UnityEngine; + +namespace FidelityFX +{ + /// + /// Scriptable object containing all shader resources required by FidelityFX Super Resolution 3 (FSR3) Upscaler. + /// These can be stored in an asset file and referenced from a scene or prefab, avoiding the need to load the shaders from a Resources folder. + /// + public class Fsr3UpscalerAssets : ScriptableObject + { + public Fsr3UpscalerShaders shaders; + +#if UNITY_EDITOR + private void Reset() + { + shaders = new Fsr3UpscalerShaders + { + computeLuminancePyramidPass = FindComputeShader("ffx_fsr3upscaler_compute_luminance_pyramid_pass"), + reconstructPreviousDepthPass = FindComputeShader("ffx_fsr3upscaler_reconstruct_previous_depth_pass"), + depthClipPass = FindComputeShader("ffx_fsr3upscaler_depth_clip_pass"), + lockPass = FindComputeShader("ffx_fsr3upscaler_lock_pass"), + accumulatePass = FindComputeShader("ffx_fsr3upscaler_accumulate_pass"), + sharpenPass = FindComputeShader("ffx_fsr3upscaler_rcas_pass"), + autoGenReactivePass = FindComputeShader("ffx_fsr3upscaler_autogen_reactive_pass"), + tcrAutoGenPass = FindComputeShader("ffx_fsr3upscaler_tcr_autogen_pass"), + }; + } + + private static ComputeShader FindComputeShader(string name) + { + string[] assetGuids = UnityEditor.AssetDatabase.FindAssets($"t:ComputeShader {name}"); + if (assetGuids == null || assetGuids.Length == 0) + return null; + + string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(assetGuids[0]); + return UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath); + } +#endif + } + + /// + /// All the compute shaders used by the FSR3 Upscaler. + /// + [System.Serializable] + public class Fsr3UpscalerShaders + { + /// + /// The compute shader used by the luminance pyramid computation pass. + /// + public ComputeShader computeLuminancePyramidPass; + + /// + /// The compute shader used by the previous depth reconstruction pass. + /// + public ComputeShader reconstructPreviousDepthPass; + + /// + /// The compute shader used by the depth clip pass. + /// + public ComputeShader depthClipPass; + + /// + /// The compute shader used by the lock pass. + /// + public ComputeShader lockPass; + + /// + /// The compute shader used by the accumulation pass. + /// + public ComputeShader accumulatePass; + + /// + /// The compute shader used by the RCAS sharpening pass. + /// + public ComputeShader sharpenPass; + + /// + /// The compute shader used to auto-generate a reactive mask. + /// + public ComputeShader autoGenReactivePass; + + /// + /// The compute shader used to auto-generate a transparency & composition mask. + /// + public ComputeShader tcrAutoGenPass; + + /// + /// Returns a copy of this class and its contents. + /// + public Fsr3UpscalerShaders Clone() + { + return (Fsr3UpscalerShaders)MemberwiseClone(); + } + + /// + /// Returns a copy of this class with clones of all its shaders. + /// This can be useful if you're running multiple FSR3 Upscaler instances with different shader configurations. + /// Be sure to clean up these clones through Dispose once you're done with them. + /// + public Fsr3UpscalerShaders DeepCopy() + { + return new Fsr3UpscalerShaders + { + computeLuminancePyramidPass = Object.Instantiate(computeLuminancePyramidPass), + reconstructPreviousDepthPass = Object.Instantiate(reconstructPreviousDepthPass), + depthClipPass = Object.Instantiate(depthClipPass), + lockPass = Object.Instantiate(lockPass), + accumulatePass = Object.Instantiate(accumulatePass), + sharpenPass = Object.Instantiate(sharpenPass), + autoGenReactivePass = Object.Instantiate(autoGenReactivePass), + tcrAutoGenPass = Object.Instantiate(tcrAutoGenPass), + }; + } + + /// + /// Destroy all the shaders within this instance. + /// Use this only on clones created through DeepCopy. + /// + public void Dispose() + { + Object.Destroy(computeLuminancePyramidPass); + Object.Destroy(reconstructPreviousDepthPass); + Object.Destroy(depthClipPass); + Object.Destroy(lockPass); + Object.Destroy(accumulatePass); + Object.Destroy(sharpenPass); + Object.Destroy(autoGenReactivePass); + Object.Destroy(tcrAutoGenPass); + } + } +} diff --git a/Assets/Scripts/Core/Fsr3UpscalerShaders.cs.meta b/Assets/Scripts/Core/Fsr3UpscalerAssets.cs.meta similarity index 100% rename from Assets/Scripts/Core/Fsr3UpscalerShaders.cs.meta rename to Assets/Scripts/Core/Fsr3UpscalerAssets.cs.meta diff --git a/Assets/Scripts/Core/Fsr3UpscalerShaders.cs b/Assets/Scripts/Core/Fsr3UpscalerShaders.cs deleted file mode 100644 index 6e85b95..0000000 --- a/Assets/Scripts/Core/Fsr3UpscalerShaders.cs +++ /dev/null @@ -1,119 +0,0 @@ -using System; -using UnityEngine; - -namespace FidelityFX -{ - /// - /// All the compute shaders used by the FidelityFX Super Resolution 3 (FSR3) Upscaler. - /// - [Serializable] - public class Fsr3UpscalerShaders: ScriptableObject - { - /// - /// The compute shader used by the luminance pyramid computation pass. - /// - public ComputeShader computeLuminancePyramidPass; - - /// - /// The compute shader used by the previous depth reconstruction pass. - /// - public ComputeShader reconstructPreviousDepthPass; - - /// - /// The compute shader used by the depth clip pass. - /// - public ComputeShader depthClipPass; - - /// - /// The compute shader used by the lock pass. - /// - public ComputeShader lockPass; - - /// - /// The compute shader used by the accumulation pass. - /// - public ComputeShader accumulatePass; - - /// - /// The compute shader used by the RCAS sharpening pass. - /// - public ComputeShader sharpenPass; - - /// - /// The compute shader used to auto-generate a reactive mask. - /// - public ComputeShader autoGenReactivePass; - - /// - /// The compute shader used to auto-generate a transparency & composition mask. - /// - public ComputeShader tcrAutoGenPass; - - /// - /// Returns a copy of this class and its contents. - /// - public Fsr3UpscalerShaders Clone() - { - return (Fsr3UpscalerShaders)MemberwiseClone(); - } - - /// - /// Returns a copy of this class with clones of all its shaders. - /// This can be useful if you're running multiple FSR3 Upscaler instances with different shader configurations. - /// Be sure to clean up these clones through Dispose once you're done with them. - /// - public Fsr3UpscalerShaders DeepCopy() - { - Fsr3UpscalerShaders copy = CreateInstance(); - copy.computeLuminancePyramidPass = Instantiate(computeLuminancePyramidPass); - copy.reconstructPreviousDepthPass = Instantiate(reconstructPreviousDepthPass); - copy.depthClipPass = Instantiate(depthClipPass); - copy.lockPass = Instantiate(lockPass); - copy.accumulatePass = Instantiate(accumulatePass); - copy.sharpenPass = Instantiate(sharpenPass); - copy.autoGenReactivePass = Instantiate(autoGenReactivePass); - copy.tcrAutoGenPass = Instantiate(tcrAutoGenPass); - return copy; - } - - /// - /// Destroy all the shaders within this instance. - /// Use this only on clones created through DeepCopy. - /// - public void Dispose() - { - Destroy(computeLuminancePyramidPass); - Destroy(reconstructPreviousDepthPass); - Destroy(depthClipPass); - Destroy(lockPass); - Destroy(accumulatePass); - Destroy(sharpenPass); - Destroy(autoGenReactivePass); - Destroy(tcrAutoGenPass); - } - - #if UNITY_EDITOR - private void Reset() - { - computeLuminancePyramidPass = FindComputeShader("ffx_fsr3upscaler_compute_luminance_pyramid_pass"); - reconstructPreviousDepthPass = FindComputeShader("ffx_fsr3upscaler_reconstruct_previous_depth_pass"); - depthClipPass = FindComputeShader("ffx_fsr3upscaler_depth_clip_pass"); - lockPass = FindComputeShader("ffx_fsr3upscaler_lock_pass"); - accumulatePass = FindComputeShader("ffx_fsr3upscaler_accumulate_pass"); - sharpenPass = FindComputeShader("ffx_fsr3upscaler_rcas_pass"); - autoGenReactivePass = FindComputeShader("ffx_fsr3upscaler_autogen_reactive_pass"); - tcrAutoGenPass = FindComputeShader("ffx_fsr3upscaler_tcr_autogen_pass"); - } - - private static ComputeShader FindComputeShader(string name) - { - string[] assetGuids = UnityEditor.AssetDatabase.FindAssets($"t:ComputeShader {name}"); - if (assetGuids == null || assetGuids.Length == 0) - return null; - - string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(assetGuids[0]); - return UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath); - } - #endif - } -} diff --git a/Assets/Scripts/Fsr3UpscalerImageEffect.cs b/Assets/Scripts/Fsr3UpscalerImageEffect.cs index 5b0deda..a5591a5 100644 --- a/Assets/Scripts/Fsr3UpscalerImageEffect.cs +++ b/Assets/Scripts/Fsr3UpscalerImageEffect.cs @@ -19,7 +19,6 @@ // THE SOFTWARE. using System; -using System.Collections; using UnityEngine; using UnityEngine.Experimental.Rendering; using UnityEngine.Rendering; @@ -99,7 +98,7 @@ namespace FidelityFX } [SerializeField] - private Fsr3UpscalerShaders shaders; + private Fsr3UpscalerAssets assets; private Fsr3UpscalerContext _context; private Vector2Int _maxRenderSize; @@ -147,9 +146,9 @@ namespace FidelityFX return; } - if (shaders == null) + if (assets == null) { - Debug.LogError($"FSR3 Upscaler shaders are not assigned! Please ensure an {nameof(Fsr3UpscalerShaders)} asset is assigned to the Shaders property of this component."); + Debug.LogError($"FSR3 Upscaler assets are not assigned! Please ensure an {nameof(Fsr3UpscalerAssets)} asset is assigned to the Assets property of this component."); enabled = false; return; } @@ -193,7 +192,7 @@ namespace FidelityFX if (enableAutoExposure) flags |= Fsr3Upscaler.InitializationFlags.EnableAutoExposure; if (UsingDynamicResolution()) flags |= Fsr3Upscaler.InitializationFlags.EnableDynamicResolution; - _context = Fsr3Upscaler.CreateContext(_displaySize, _maxRenderSize, shaders, flags); + _context = Fsr3Upscaler.CreateContext(_displaySize, _maxRenderSize, assets.shaders, flags); _prevDisplaySize = _displaySize; _prevQualityMode = qualityMode; @@ -470,15 +469,15 @@ namespace FidelityFX #if UNITY_EDITOR private void Reset() { - if (shaders != null) + if (assets != null) return; - string[] assetGuids = UnityEditor.AssetDatabase.FindAssets($"t:{nameof(Fsr3UpscalerShaders)}"); + string[] assetGuids = UnityEditor.AssetDatabase.FindAssets($"t:{nameof(Fsr3UpscalerAssets)}"); if (assetGuids == null || assetGuids.Length == 0) return; string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(assetGuids[0]); - shaders = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath); + assets = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath); } #endif } diff --git a/Assets/Shaders/FSR3/Fsr3UpscalerShaders.asset b/Assets/Shaders/FSR3/Fsr3UpscalerShaders.asset deleted file mode 100644 index 50daff0..0000000 --- a/Assets/Shaders/FSR3/Fsr3UpscalerShaders.asset +++ /dev/null @@ -1,22 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: db26e15a33db6ab42a38daab0ba2712f, type: 3} - m_Name: Fsr3UpscalerShaders - m_EditorClassIdentifier: - computeLuminancePyramidPass: {fileID: 7200000, guid: d253be05abcdc80428503d3e4cce3a36, type: 3} - reconstructPreviousDepthPass: {fileID: 7200000, guid: 4f59e5b9179d74844ae06a30ae1e0629, type: 3} - depthClipPass: {fileID: 7200000, guid: 20e44016ed34b0d4b8de499d1b566c69, type: 3} - lockPass: {fileID: 7200000, guid: a135306e6d1857e43a86ef20db2a47fe, type: 3} - accumulatePass: {fileID: 7200000, guid: c9b45f0ae7673694ba57a4aadfe212e9, type: 3} - sharpenPass: {fileID: 7200000, guid: 7aaf5cfff022de2499e9b0412f947f6c, type: 3} - autoGenReactivePass: {fileID: 7200000, guid: 5716b91fdaa4e9e439df6b96a796fe6e, type: 3} - tcrAutoGenPass: {fileID: 7200000, guid: 75cdc6ef23f08ed498d4da511923fcea, type: 3}