Development repository for FSR2 integration into Unity Post-Processing Stack V2.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.8 KiB

using UnityEngine;
using UnityEngine.Rendering;
namespace ArmASR
{
/// <summary>
/// Scriptable object containing all shader resources required by Arm Accuracy Super Resolution (ASR).
/// 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>
[CreateAssetMenu(fileName = "ASR Assets", menuName = "ARM/ASR Assets", order = 1102)]
public class AsrAssets : ScriptableObject
{
public AsrShaderBundle shaderBundle;
#if UNITY_EDITOR
private void Reset()
{
shaderBundle = new AsrShaderBundle
{
legacyShaders = new AsrShaders
{
fragmentShader = FindFragmentShader("ffxm_fsr2_fragment_legacy"),
computeLuminancePyramidPass = FindComputeShader("ffxm_fsr2_luma_pyramid_legacy"),
lockPass = FindComputeShader("ffxm_fsr2_lock_legacy"),
},
modernShaders = new AsrShaders
{
fragmentShader = FindFragmentShader("ffxm_fsr2_fragment_modern"),
computeLuminancePyramidPass = FindComputeShader("ffxm_fsr2_luma_pyramid_modern"),
lockPass = FindComputeShader("ffxm_fsr2_lock_modern"),
},
};
}
private static Shader FindFragmentShader(string name)
{
string[] assetGuids = UnityEditor.AssetDatabase.FindAssets($"t:Shader {name}");
if (assetGuids == null || assetGuids.Length == 0)
return null;
string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(assetGuids[0]);
return UnityEditor.AssetDatabase.LoadAssetAtPath<Shader>(assetPath);
}
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
}
[System.Serializable]
public class AsrShaderBundle
{
public AsrShaders legacyShaders;
public AsrShaders modernShaders;
public AsrShaders GetShadersForCurrentPlatform()
{
switch (SystemInfo.graphicsDeviceType)
{
case GraphicsDeviceType.Direct3D12:
case GraphicsDeviceType.Vulkan:
case GraphicsDeviceType.Metal:
case GraphicsDeviceType.PlayStation5:
case GraphicsDeviceType.PlayStation5NGGC:
case GraphicsDeviceType.GameCoreXboxSeries:
return modernShaders;
default:
return legacyShaders;
}
}
}
/// <summary>
/// All the compute shaders used by ASR.
/// </summary>
[System.Serializable]
public class AsrShaders
{
/// <summary>
/// Combined shader file containing all non-compute passes.
/// </summary>
public Shader fragmentShader;
/// <summary>
/// The compute shader used by the luminance pyramid computation pass.
/// </summary>
public ComputeShader computeLuminancePyramidPass;
/// <summary>
/// The compute shader used by the lock pass.
/// </summary>
public ComputeShader lockPass;
/// <summary>
/// Returns a copy of this class and its contents.
/// </summary>
public AsrShaders Clone()
{
return (AsrShaders)MemberwiseClone();
}
/// <summary>
/// Returns a copy of this class with clones of all its shaders.
/// This can be useful if you're running multiple ASR instances with different shader configurations.
/// Be sure to clean up these clones through Dispose once you're done with them.
/// </summary>
public AsrShaders DeepCopy()
{
return new AsrShaders
{
fragmentShader = Object.Instantiate(fragmentShader),
computeLuminancePyramidPass = Object.Instantiate(computeLuminancePyramidPass),
lockPass = Object.Instantiate(lockPass),
};
}
/// <summary>
/// Destroy all the shaders within this instance.
/// Use this only on clones created through DeepCopy.
/// </summary>
public void Dispose()
{
Object.Destroy(fragmentShader);
Object.Destroy(computeLuminancePyramidPass);
Object.Destroy(lockPass);
}
}
}