Browse Source

Split the FSR3 shaders aggregate class from the scriptable object part. This allows the shader references to be serialized as a single asset still, but also for them to be embedded inline inside of a different scriptable object (for PPV2)

fsr3
Nico de Poel 2 years ago
parent
commit
8e879726d7
  1. 23
      Assets/Fsr3UpscalerAssets.asset
  2. 0
      Assets/Fsr3UpscalerAssets.asset.meta
  3. 2
      Assets/Scenes/SampleScene.unity
  4. 151
      Assets/Scripts/Core/Fsr3UpscalerAssets.cs
  5. 0
      Assets/Scripts/Core/Fsr3UpscalerAssets.cs.meta
  6. 119
      Assets/Scripts/Core/Fsr3UpscalerShaders.cs
  7. 15
      Assets/Scripts/Fsr3UpscalerImageEffect.cs
  8. 22
      Assets/Shaders/FSR3/Fsr3UpscalerShaders.asset

23
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}

0
Assets/Shaders/FSR3/Fsr3UpscalerShaders.asset.meta → Assets/Fsr3UpscalerAssets.asset.meta

2
Assets/Scenes/SampleScene.unity

@ -337,7 +337,7 @@ MonoBehaviour:
autoTcScale: 1 autoTcScale: 1
autoReactiveScale: 5 autoReactiveScale: 5
autoReactiveMax: 0.9 autoReactiveMax: 0.9
shaders: {fileID: 11400000, guid: 4151befafd86e8740ab09463b4f1bdbb, type: 2}
assets: {fileID: 11400000, guid: 4151befafd86e8740ab09463b4f1bdbb, type: 2}
--- !u!114 &963194230 --- !u!114 &963194230
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

151
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
{
/// <summary>
/// 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.
/// </summary>
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<ComputeShader>(assetPath);
}
#endif
}
/// <summary>
/// All the compute shaders used by the FSR3 Upscaler.
/// </summary>
[System.Serializable]
public class Fsr3UpscalerShaders
{
/// <summary>
/// The compute shader used by the luminance pyramid computation pass.
/// </summary>
public ComputeShader computeLuminancePyramidPass;
/// <summary>
/// The compute shader used by the previous depth reconstruction pass.
/// </summary>
public ComputeShader reconstructPreviousDepthPass;
/// <summary>
/// The compute shader used by the depth clip pass.
/// </summary>
public ComputeShader depthClipPass;
/// <summary>
/// The compute shader used by the lock pass.
/// </summary>
public ComputeShader lockPass;
/// <summary>
/// The compute shader used by the accumulation pass.
/// </summary>
public ComputeShader accumulatePass;
/// <summary>
/// The compute shader used by the RCAS sharpening pass.
/// </summary>
public ComputeShader sharpenPass;
/// <summary>
/// The compute shader used to auto-generate a reactive mask.
/// </summary>
public ComputeShader autoGenReactivePass;
/// <summary>
/// The compute shader used to auto-generate a transparency & composition mask.
/// </summary>
public ComputeShader tcrAutoGenPass;
/// <summary>
/// Returns a copy of this class and its contents.
/// </summary>
public Fsr3UpscalerShaders Clone()
{
return (Fsr3UpscalerShaders)MemberwiseClone();
}
/// <summary>
/// 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.
/// </summary>
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),
};
}
/// <summary>
/// Destroy all the shaders within this instance.
/// Use this only on clones created through DeepCopy.
/// </summary>
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);
}
}
}

0
Assets/Scripts/Core/Fsr3UpscalerShaders.cs.meta → Assets/Scripts/Core/Fsr3UpscalerAssets.cs.meta

119
Assets/Scripts/Core/Fsr3UpscalerShaders.cs

@ -1,119 +0,0 @@
using System;
using UnityEngine;
namespace FidelityFX
{
/// <summary>
/// All the compute shaders used by the FidelityFX Super Resolution 3 (FSR3) Upscaler.
/// </summary>
[Serializable]
public class Fsr3UpscalerShaders: ScriptableObject
{
/// <summary>
/// The compute shader used by the luminance pyramid computation pass.
/// </summary>
public ComputeShader computeLuminancePyramidPass;
/// <summary>
/// The compute shader used by the previous depth reconstruction pass.
/// </summary>
public ComputeShader reconstructPreviousDepthPass;
/// <summary>
/// The compute shader used by the depth clip pass.
/// </summary>
public ComputeShader depthClipPass;
/// <summary>
/// The compute shader used by the lock pass.
/// </summary>
public ComputeShader lockPass;
/// <summary>
/// The compute shader used by the accumulation pass.
/// </summary>
public ComputeShader accumulatePass;
/// <summary>
/// The compute shader used by the RCAS sharpening pass.
/// </summary>
public ComputeShader sharpenPass;
/// <summary>
/// The compute shader used to auto-generate a reactive mask.
/// </summary>
public ComputeShader autoGenReactivePass;
/// <summary>
/// The compute shader used to auto-generate a transparency & composition mask.
/// </summary>
public ComputeShader tcrAutoGenPass;
/// <summary>
/// Returns a copy of this class and its contents.
/// </summary>
public Fsr3UpscalerShaders Clone()
{
return (Fsr3UpscalerShaders)MemberwiseClone();
}
/// <summary>
/// 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.
/// </summary>
public Fsr3UpscalerShaders DeepCopy()
{
Fsr3UpscalerShaders copy = CreateInstance<Fsr3UpscalerShaders>();
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;
}
/// <summary>
/// Destroy all the shaders within this instance.
/// Use this only on clones created through DeepCopy.
/// </summary>
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<ComputeShader>(assetPath);
}
#endif
}
}

15
Assets/Scripts/Fsr3UpscalerImageEffect.cs

@ -19,7 +19,6 @@
// THE SOFTWARE. // THE SOFTWARE.
using System; using System;
using System.Collections;
using UnityEngine; using UnityEngine;
using UnityEngine.Experimental.Rendering; using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering; using UnityEngine.Rendering;
@ -99,7 +98,7 @@ namespace FidelityFX
} }
[SerializeField] [SerializeField]
private Fsr3UpscalerShaders shaders;
private Fsr3UpscalerAssets assets;
private Fsr3UpscalerContext _context; private Fsr3UpscalerContext _context;
private Vector2Int _maxRenderSize; private Vector2Int _maxRenderSize;
@ -147,9 +146,9 @@ namespace FidelityFX
return; 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; enabled = false;
return; return;
} }
@ -193,7 +192,7 @@ namespace FidelityFX
if (enableAutoExposure) flags |= Fsr3Upscaler.InitializationFlags.EnableAutoExposure; if (enableAutoExposure) flags |= Fsr3Upscaler.InitializationFlags.EnableAutoExposure;
if (UsingDynamicResolution()) flags |= Fsr3Upscaler.InitializationFlags.EnableDynamicResolution; if (UsingDynamicResolution()) flags |= Fsr3Upscaler.InitializationFlags.EnableDynamicResolution;
_context = Fsr3Upscaler.CreateContext(_displaySize, _maxRenderSize, shaders, flags);
_context = Fsr3Upscaler.CreateContext(_displaySize, _maxRenderSize, assets.shaders, flags);
_prevDisplaySize = _displaySize; _prevDisplaySize = _displaySize;
_prevQualityMode = qualityMode; _prevQualityMode = qualityMode;
@ -470,15 +469,15 @@ namespace FidelityFX
#if UNITY_EDITOR #if UNITY_EDITOR
private void Reset() private void Reset()
{ {
if (shaders != null)
if (assets != null)
return; return;
string[] assetGuids = UnityEditor.AssetDatabase.FindAssets($"t:{nameof(Fsr3UpscalerShaders)}");
string[] assetGuids = UnityEditor.AssetDatabase.FindAssets($"t:{nameof(Fsr3UpscalerAssets)}");
if (assetGuids == null || assetGuids.Length == 0) if (assetGuids == null || assetGuids.Length == 0)
return; return;
string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(assetGuids[0]); string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(assetGuids[0]);
shaders = UnityEditor.AssetDatabase.LoadAssetAtPath<Fsr3UpscalerShaders>(assetPath);
assets = UnityEditor.AssetDatabase.LoadAssetAtPath<Fsr3UpscalerAssets>(assetPath);
} }
#endif #endif
} }

22
Assets/Shaders/FSR3/Fsr3UpscalerShaders.asset

@ -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}
Loading…
Cancel
Save