From 70dc9e29d4b6ad78342f532e239623acdb4f9ac5 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 18 Feb 2023 17:23:28 +0100 Subject: [PATCH] Use a CommandBuffer to queue up commands and dispatch them all in one go --- Assets/Scripts/Fsr2Context.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Fsr2Context.cs b/Assets/Scripts/Fsr2Context.cs index a7deb95..c46128d 100644 --- a/Assets/Scripts/Fsr2Context.cs +++ b/Assets/Scripts/Fsr2Context.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; +using UnityEngine.Rendering; namespace FidelityFX { @@ -10,6 +11,7 @@ namespace FidelityFX private const int MaxQueuedFrames = 16; private Fsr2.ContextDescription _contextDescription; + private CommandBuffer _commandBuffer; private ComputeShader _prepareInputColorShader; private ComputeShader _depthClipShader; @@ -44,6 +46,7 @@ namespace FidelityFX public void Create(Fsr2.ContextDescription contextDescription) { _contextDescription = contextDescription; + _commandBuffer = new CommandBuffer { name = "FSR2" }; _fsr2ConstantsBuffer = CreateConstantBuffer(); _spdConstantsBuffer = CreateConstantBuffer(); @@ -88,6 +91,12 @@ namespace FidelityFX public void Dispatch(Fsr2.DispatchDescription dispatchParams) { + _commandBuffer.Clear(); + + // TODO: Should probably use a CommandBuffer here, to queue up all of the commands and dispatch them in one go + // Use SetRandomWriteTarget to declare UAVs + // Use ClearRandomWriteTargets to clear all UAVs at once + if (_firstExecution) { // TODO: clear values @@ -112,6 +121,7 @@ namespace FidelityFX if (resetAccumulation) { // TODO: clear reconstructed depth for max depth store + } // Auto exposure @@ -133,15 +143,17 @@ namespace FidelityFX int threadGroupsX = (Screen.width + threadGroupWorkRegionDimRcas - 1) / threadGroupWorkRegionDimRcas; int threadGroupsY = (Screen.height + threadGroupWorkRegionDimRcas - 1) / threadGroupWorkRegionDimRcas; - _rcasShader.Dispatch(rcasKernel, threadGroupsX, threadGroupsY, 1); + _commandBuffer.DispatchCompute(_rcasShader, rcasKernel, threadGroupsX, threadGroupsY, 1); } else { - Graphics.Blit(dispatchParams.Input, dispatchParams.Output); + _commandBuffer.Blit(dispatchParams.Input, dispatchParams.Output); } _resourceFrameIndex = (_resourceFrameIndex + 1) % MaxQueuedFrames; + Graphics.ExecuteCommandBuffer(_commandBuffer); + // TODO Unregister resources: release temp RT's } @@ -313,6 +325,9 @@ namespace FidelityFX DestroyComputeShader(ref _prepareInputColorShader); DestroyComputeShader(ref _rcasShader); DestroyComputeShader(ref _computeLuminancePyramidShader); + + _commandBuffer.Dispose(); + _commandBuffer = null; } [Serializable, StructLayout(LayoutKind.Sequential)]