From c74320613bc9b4eeea9bae3e086bb855402c3e77 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Tue, 23 Jul 2024 23:28:13 +0200 Subject: [PATCH] Reworked multi-level double-buffered resources into arrays of arrays of render textures --- Runtime/OpticalFlow/OpticalFlowContext.cs | 19 ++-- Runtime/OpticalFlow/OpticalFlowPass.cs | 20 ++--- Runtime/OpticalFlow/OpticalFlowResources.cs | 98 ++++++++------------- 3 files changed, 53 insertions(+), 84 deletions(-) diff --git a/Runtime/OpticalFlow/OpticalFlowContext.cs b/Runtime/OpticalFlow/OpticalFlowContext.cs index 5ca8a1f..90fdd7c 100644 --- a/Runtime/OpticalFlow/OpticalFlowContext.cs +++ b/Runtime/OpticalFlow/OpticalFlowContext.cs @@ -106,20 +106,11 @@ namespace FidelityFX.OpticalFlow commandBuffer.ClearRenderTarget(false, true, Color.clear); for (int i = 0; i < 2; ++i) { - commandBuffer.SetRenderTarget(_resources.OpticalFlowInput[i]); - commandBuffer.ClearRenderTarget(false, true, Color.clear); - commandBuffer.SetRenderTarget(_resources.OpticalFlowInputLevel1[i]); - commandBuffer.ClearRenderTarget(false, true, Color.clear); - commandBuffer.SetRenderTarget(_resources.OpticalFlowInputLevel2[i]); - commandBuffer.ClearRenderTarget(false, true, Color.clear); - commandBuffer.SetRenderTarget(_resources.OpticalFlowInputLevel3[i]); - commandBuffer.ClearRenderTarget(false, true, Color.clear); - commandBuffer.SetRenderTarget(_resources.OpticalFlowInputLevel4[i]); - commandBuffer.ClearRenderTarget(false, true, Color.clear); - commandBuffer.SetRenderTarget(_resources.OpticalFlowInputLevel5[i]); - commandBuffer.ClearRenderTarget(false, true, Color.clear); - commandBuffer.SetRenderTarget(_resources.OpticalFlowInputLevel6[i]); - commandBuffer.ClearRenderTarget(false, true, Color.clear); + for (int level = 0; level < OpticalFlow.OpticalFlowMaxPyramidLevels; ++level) + { + commandBuffer.SetRenderTarget(_resources.OpticalFlowInputLevels[level][i]); + commandBuffer.ClearRenderTarget(false, true, Color.clear); + } } } diff --git a/Runtime/OpticalFlow/OpticalFlowPass.cs b/Runtime/OpticalFlow/OpticalFlowPass.cs index 952ea72..dcbeb77 100644 --- a/Runtime/OpticalFlow/OpticalFlowPass.cs +++ b/Runtime/OpticalFlow/OpticalFlowPass.cs @@ -63,7 +63,7 @@ namespace FidelityFX.OpticalFlow ref var color = ref dispatchParams.color; commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.SrvInputColor, color.RenderTarget, color.MipLevel, color.SubElement); - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInput, Resources.OpticalFlowInput[frameIndex]); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInput, Resources.OpticalFlowInputLevels[0][frameIndex]); commandBuffer.SetComputeConstantBufferParam(ComputeShader, OpticalFlowShaderIDs.CbOpticalFlow, Constants, 0, Marshal.SizeOf()); @@ -84,13 +84,13 @@ namespace FidelityFX.OpticalFlow protected override void DoScheduleDispatch(CommandBuffer commandBuffer, OpticalFlow.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ) { - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInput, Resources.OpticalFlowInput[frameIndex]); - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel1, Resources.OpticalFlowInputLevel1[frameIndex]); - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel2, Resources.OpticalFlowInputLevel2[frameIndex]); - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel3, Resources.OpticalFlowInputLevel3[frameIndex]); - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel4, Resources.OpticalFlowInputLevel4[frameIndex]); - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel5, Resources.OpticalFlowInputLevel5[frameIndex]); - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel6, Resources.OpticalFlowInputLevel6[frameIndex]); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInput, Resources.OpticalFlowInputLevels[0][frameIndex]); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel1, Resources.OpticalFlowInputLevels[1][frameIndex]); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel2, Resources.OpticalFlowInputLevels[2][frameIndex]); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel3, Resources.OpticalFlowInputLevels[3][frameIndex]); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel4, Resources.OpticalFlowInputLevels[4][frameIndex]); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel5, Resources.OpticalFlowInputLevels[5][frameIndex]); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowInputLevel6, Resources.OpticalFlowInputLevels[6][frameIndex]); commandBuffer.SetComputeConstantBufferParam(ComputeShader, OpticalFlowShaderIDs.CbOpticalFlow, Constants, 0, Marshal.SizeOf()); commandBuffer.SetComputeConstantBufferParam(ComputeShader, OpticalFlowShaderIDs.CbSpd, _spdConstants, 0, Marshal.SizeOf()); @@ -109,8 +109,8 @@ namespace FidelityFX.OpticalFlow protected override void DoScheduleDispatch(CommandBuffer commandBuffer, OpticalFlow.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ) { - // TODO: probably needs to be input from this frame (result from pyramid pass), but double check to be sure - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.SrvOpticalFlowInput, Resources.OpticalFlowInput[frameIndex]); + // TODO: probably needs to be input from this frame (result from previous passes), but double check to be sure + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.SrvOpticalFlowInput, Resources.OpticalFlowInputLevels[0][frameIndex]); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, OpticalFlowShaderIDs.UavOpticalFlowScdHistogram, Resources.OpticalFlowSCDHistogram); commandBuffer.SetComputeConstantBufferParam(ComputeShader, OpticalFlowShaderIDs.CbOpticalFlow, Constants, 0, Marshal.SizeOf()); diff --git a/Runtime/OpticalFlow/OpticalFlowResources.cs b/Runtime/OpticalFlow/OpticalFlowResources.cs index 42c1ebc..17f55a9 100644 --- a/Runtime/OpticalFlow/OpticalFlowResources.cs +++ b/Runtime/OpticalFlow/OpticalFlowResources.cs @@ -5,22 +5,28 @@ namespace FidelityFX.OpticalFlow { internal class OpticalFlowResources { - public readonly RenderTexture[] OpticalFlowInput = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowInputLevel1 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowInputLevel2 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowInputLevel3 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowInputLevel4 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowInputLevel5 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowInputLevel6 = new RenderTexture[2]; + public readonly RenderTexture[][] OpticalFlowInputLevels = + { + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + }; + + public readonly RenderTexture[][] OpticalFlowLevels = + { + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + new RenderTexture[2], + }; - public readonly RenderTexture[] OpticalFlowLevel0 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowLevel1 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowLevel2 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowLevel3 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowLevel4 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowLevel5 = new RenderTexture[2]; - public readonly RenderTexture[] OpticalFlowLevel6 = new RenderTexture[2]; - public RenderTexture OpticalFlowSCDHistogram; public RenderTexture OpticalFlowSCDPreviousHistogram; public RenderTexture OpticalFlowSCDTemp; @@ -31,38 +37,20 @@ namespace FidelityFX.OpticalFlow const int minBlockSize = 8; Vector2Int opticalFlowTextureSize = OpticalFlow.GetOpticalFlowTextureSize(contextDescription.resolution, minBlockSize); - Vector2Int opticalFlowLevel1TextureSize = NextLevelSize(opticalFlowTextureSize); - Vector2Int opticalFlowLevel2TextureSize = NextLevelSize(opticalFlowLevel1TextureSize); - Vector2Int opticalFlowLevel3TextureSize = NextLevelSize(opticalFlowLevel2TextureSize); - Vector2Int opticalFlowLevel4TextureSize = NextLevelSize(opticalFlowLevel3TextureSize); - Vector2Int opticalFlowLevel5TextureSize = NextLevelSize(opticalFlowLevel4TextureSize); - Vector2Int opticalFlowLevel6TextureSize = NextLevelSize(opticalFlowLevel5TextureSize); - + + // TODO: this seems useless, delete unless something comes up Vector2Int opticalFlowHistogramTextureSize = OpticalFlow.GetOpticalFlowHistogramSize(0); Vector2Int globalMotionSearchMaxDispatchSize = OpticalFlow.GetGlobalMotionSearchDispatchSize(0); int globalMotionSearchTextureWidth = 4 + (globalMotionSearchMaxDispatchSize.x * globalMotionSearchMaxDispatchSize.y); - - CreateDoubleBufferedResource(OpticalFlowInput, "OPTICALFLOW_OpticalFlowInput", opticalFlowInputTextureSize, GraphicsFormat.R8_UInt); - opticalFlowInputTextureSize = new Vector2Int(opticalFlowInputTextureSize.x >> 1, opticalFlowInputTextureSize.y >> 1); - CreateDoubleBufferedResource(OpticalFlowInputLevel1, "OPTICALFLOW_OpticalFlowInputLevel1-", opticalFlowInputTextureSize, GraphicsFormat.R8_UInt); - opticalFlowInputTextureSize = new Vector2Int(opticalFlowInputTextureSize.x >> 1, opticalFlowInputTextureSize.y >> 1); - CreateDoubleBufferedResource(OpticalFlowInputLevel2, "OPTICALFLOW_OpticalFlowInputLevel2-", opticalFlowInputTextureSize, GraphicsFormat.R8_UInt); - opticalFlowInputTextureSize = new Vector2Int(opticalFlowInputTextureSize.x >> 1, opticalFlowInputTextureSize.y >> 1); - CreateDoubleBufferedResource(OpticalFlowInputLevel3, "OPTICALFLOW_OpticalFlowInputLevel3-", opticalFlowInputTextureSize, GraphicsFormat.R8_UInt); - opticalFlowInputTextureSize = new Vector2Int(opticalFlowInputTextureSize.x >> 1, opticalFlowInputTextureSize.y >> 1); - CreateDoubleBufferedResource(OpticalFlowInputLevel4, "OPTICALFLOW_OpticalFlowInputLevel4-", opticalFlowInputTextureSize, GraphicsFormat.R8_UInt); - opticalFlowInputTextureSize = new Vector2Int(opticalFlowInputTextureSize.x >> 1, opticalFlowInputTextureSize.y >> 1); - CreateDoubleBufferedResource(OpticalFlowInputLevel5, "OPTICALFLOW_OpticalFlowInputLevel5-", opticalFlowInputTextureSize, GraphicsFormat.R8_UInt); - opticalFlowInputTextureSize = new Vector2Int(opticalFlowInputTextureSize.x >> 1, opticalFlowInputTextureSize.y >> 1); - CreateDoubleBufferedResource(OpticalFlowInputLevel6, "OPTICALFLOW_OpticalFlowInputLevel6-", opticalFlowInputTextureSize, GraphicsFormat.R8_UInt); - - CreateDoubleBufferedResource(OpticalFlowLevel0, "OPTICALFLOW_OpticalFlow", opticalFlowTextureSize, GraphicsFormat.R16G16_SInt); - CreateDoubleBufferedResource(OpticalFlowLevel1, "OPTICALFLOW_OpticalFlowLevel1-", opticalFlowLevel1TextureSize, GraphicsFormat.R16G16_SInt); - CreateDoubleBufferedResource(OpticalFlowLevel2, "OPTICALFLOW_OpticalFlowLevel2-", opticalFlowLevel2TextureSize, GraphicsFormat.R16G16_SInt); - CreateDoubleBufferedResource(OpticalFlowLevel3, "OPTICALFLOW_OpticalFlowLevel3-", opticalFlowLevel3TextureSize, GraphicsFormat.R16G16_SInt); - CreateDoubleBufferedResource(OpticalFlowLevel4, "OPTICALFLOW_OpticalFlowLevel4-", opticalFlowLevel4TextureSize, GraphicsFormat.R16G16_SInt); - CreateDoubleBufferedResource(OpticalFlowLevel5, "OPTICALFLOW_OpticalFlowLevel5-", opticalFlowLevel5TextureSize, GraphicsFormat.R16G16_SInt); - CreateDoubleBufferedResource(OpticalFlowLevel6, "OPTICALFLOW_OpticalFlowLevel6-", opticalFlowLevel6TextureSize, GraphicsFormat.R16G16_SInt); + + for (int level = 0; level < OpticalFlow.OpticalFlowMaxPyramidLevels; ++level) + { + CreateDoubleBufferedResource(OpticalFlowInputLevels[level], $"OPTICALFLOW_OpticalFlowInputLevel{level}-", opticalFlowInputTextureSize, GraphicsFormat.R8_UInt); + CreateDoubleBufferedResource(OpticalFlowLevels[level], $"OPTICALFLOW_OpticalFlowLevel{level}-", opticalFlowTextureSize, GraphicsFormat.R16G16_SInt); + + opticalFlowTextureSize = NextLevelSize(opticalFlowTextureSize); + opticalFlowInputTextureSize = new Vector2Int(opticalFlowInputTextureSize.x >> 1, opticalFlowInputTextureSize.y >> 1); + } OpticalFlowSCDHistogram = CreateResource("OPTICALFLOW_OpticalFlowSCDHistogram", new Vector2Int(OpticalFlow.GetSCDHistogramTextureWidth(), 1), GraphicsFormat.R32_UInt); OpticalFlowSCDPreviousHistogram = CreateResource("OPTICALFLOW_OpticalFlowSCDPreviousHistogram", new Vector2Int(OpticalFlow.GetSCDHistogramTextureWidth(), 1), GraphicsFormat.R32_SFloat); @@ -74,22 +62,12 @@ namespace FidelityFX.OpticalFlow DestroyResource(ref OpticalFlowSCDTemp); DestroyResource(ref OpticalFlowSCDPreviousHistogram); DestroyResource(ref OpticalFlowSCDHistogram); - - DestroyResource(OpticalFlowLevel6); - DestroyResource(OpticalFlowLevel5); - DestroyResource(OpticalFlowLevel4); - DestroyResource(OpticalFlowLevel3); - DestroyResource(OpticalFlowLevel2); - DestroyResource(OpticalFlowLevel1); - DestroyResource(OpticalFlowLevel0); - - DestroyResource(OpticalFlowInputLevel6); - DestroyResource(OpticalFlowInputLevel5); - DestroyResource(OpticalFlowInputLevel4); - DestroyResource(OpticalFlowInputLevel3); - DestroyResource(OpticalFlowInputLevel2); - DestroyResource(OpticalFlowInputLevel1); - DestroyResource(OpticalFlowInput); + + for (int level = 0; level < OpticalFlow.OpticalFlowMaxPyramidLevels; ++level) + { + DestroyResource(OpticalFlowLevels[level]); + DestroyResource(OpticalFlowInputLevels[level]); + } } private static Vector2Int NextLevelSize(Vector2Int size)