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.
 
 
 
 

144 lines
6.1 KiB

// Copyright (c) 2024 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.OpticalFlow
{
/// <summary>
/// Scriptable object containing all shader resources required by FidelityFX Optical Flow.
/// 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 = "Optical Flow Assets", menuName = "FidelityFX/Optical Flow Assets", order = 1111)]
public class OpticalFlowAssets : ScriptableObject
{
public OpticalFlowShaders shaders;
#if UNITY_EDITOR
private void Reset()
{
shaders = new OpticalFlowShaders
{
generateOpticalFlowInputPyramid = FindComputeShader("ffx_opticalflow_compute_luminance_pyramid_pass"),
prepareLuma = FindComputeShader("ffx_opticalflow_prepare_luma_pass"),
generateScdHistogram = FindComputeShader("ffx_opticalflow_generate_scd_histogram_pass"),
computeScdDivergence = FindComputeShader("ffx_opticalflow_compute_scd_divergence_pass"),
computeOpticalFlow = FindComputeShader("ffx_opticalflow_compute_optical_flow_advanced_pass_v5"),
filterOpticalFlow = FindComputeShader("ffx_opticalflow_filter_optical_flow_pass_v5"),
scaleOpticalFlow = FindComputeShader("ffx_opticalflow_scale_optical_flow_advanced_pass_v5"),
};
}
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 Optical Flow.
/// </summary>
[System.Serializable]
public class OpticalFlowShaders
{
/// <summary>
/// The compute shader used by the generate optical flow input pass.
/// </summary>
public ComputeShader generateOpticalFlowInputPyramid;
/// <summary>
/// The compute shader used by the prepare luma pass.
/// </summary>
public ComputeShader prepareLuma;
/// <summary>
/// The compute shader used by the generate scene change detection histogram pass.
/// </summary>
public ComputeShader generateScdHistogram;
/// <summary>
/// The compute shader used by the compute scene change detection divergence pass.
/// </summary>
public ComputeShader computeScdDivergence;
/// <summary>
/// The compute shader used by the compute optical flow pass.
/// </summary>
public ComputeShader computeOpticalFlow;
/// <summary>
/// The compute shader used by the filter optical flow pass.
/// </summary>
public ComputeShader filterOpticalFlow;
/// <summary>
/// The compute shader used by the scale optical flow mask.
/// </summary>
public ComputeShader scaleOpticalFlow;
/// <summary>
/// Returns a copy of this class and its contents.
/// </summary>
public OpticalFlowShaders Clone()
{
return (OpticalFlowShaders)MemberwiseClone();
}
/// <summary>
/// Returns a copy of this class with clones of all its shaders.
/// This can be useful if you're running multiple Optical Flow instances with different shader configurations.
/// Be sure to clean up these clones through Dispose once you're done with them.
/// </summary>
public OpticalFlowShaders DeepCopy()
{
return new OpticalFlowShaders
{
generateOpticalFlowInputPyramid = Object.Instantiate(generateOpticalFlowInputPyramid),
prepareLuma = Object.Instantiate(prepareLuma),
generateScdHistogram = Object.Instantiate(generateScdHistogram),
computeScdDivergence = Object.Instantiate(computeScdDivergence),
computeOpticalFlow = Object.Instantiate(computeOpticalFlow),
filterOpticalFlow = Object.Instantiate(filterOpticalFlow),
scaleOpticalFlow = Object.Instantiate(scaleOpticalFlow),
};
}
/// <summary>
/// Destroy all the shaders within this instance.
/// Use this only on clones created through DeepCopy.
/// </summary>
public void Dispose()
{
Object.Destroy(scaleOpticalFlow);
Object.Destroy(filterOpticalFlow);
Object.Destroy(computeOpticalFlow);
Object.Destroy(computeScdDivergence);
Object.Destroy(generateScdHistogram);
Object.Destroy(prepareLuma);
Object.Destroy(generateOpticalFlowInputPyramid);
}
}
}