// 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.FrameGen { /// /// 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. /// [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(assetPath); } #endif } /// /// All the compute shaders used by Optical Flow. /// [System.Serializable] public class OpticalFlowShaders { /// /// The compute shader used by the generate optical flow input pass. /// public ComputeShader generateOpticalFlowInputPyramid; /// /// The compute shader used by the prepare luma pass. /// public ComputeShader prepareLuma; /// /// The compute shader used by the generate scene change detection histogram pass. /// public ComputeShader generateScdHistogram; /// /// The compute shader used by the compute scene change detection divergence pass. /// public ComputeShader computeScdDivergence; /// /// The compute shader used by the compute optical flow pass. /// public ComputeShader computeOpticalFlow; /// /// The compute shader used by the filter optical flow pass. /// public ComputeShader filterOpticalFlow; /// /// The compute shader used by the scale optical flow mask. /// public ComputeShader scaleOpticalFlow; /// /// Returns a copy of this class and its contents. /// public OpticalFlowShaders Clone() { return (OpticalFlowShaders)MemberwiseClone(); } /// /// 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. /// 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), }; } /// /// Destroy all the shaders within this instance. /// Use this only on clones created through DeepCopy. /// public void Dispose() { Object.Destroy(scaleOpticalFlow); Object.Destroy(filterOpticalFlow); Object.Destroy(computeOpticalFlow); Object.Destroy(computeScdDivergence); Object.Destroy(generateScdHistogram); Object.Destroy(prepareLuma); Object.Destroy(generateOpticalFlowInputPyramid); } } }