From 178d9131af86ab2bd78e958323ecd24597c93168 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Thu, 9 Mar 2023 15:59:04 +0100 Subject: [PATCH] Implemented most of the FSR2 dispatch. It seems to be doing the right things in the background. Now its output just needs to be hacked into the PPV2 pipeline correctly. --- Assets/Scripts/Fsr2PostProcessEffect.cs | 64 +++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Fsr2PostProcessEffect.cs b/Assets/Scripts/Fsr2PostProcessEffect.cs index 19b9259..cc20c54 100644 --- a/Assets/Scripts/Fsr2PostProcessEffect.cs +++ b/Assets/Scripts/Fsr2PostProcessEffect.cs @@ -3,6 +3,7 @@ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.Rendering; using UnityEngine.Rendering.PostProcessing; namespace FidelityFX @@ -63,6 +64,9 @@ namespace FidelityFX private ref Vector2Int DisplaySize => ref _helper.DisplaySize; private ref Vector2Int RenderSize => ref _helper.RenderSize; + private readonly Fsr2.DispatchDescription _dispatchDescription = new Fsr2.DispatchDescription(); + private readonly Fsr2.GenerateReactiveDescription _genReactiveDescription = new Fsr2.GenerateReactiveDescription(); + private Fsr2.QualityMode _prevQualityMode; private Vector2Int _prevDisplaySize; @@ -119,11 +123,26 @@ namespace FidelityFX _prevQualityMode = settings.qualityMode; } - //Debug.Log("[FSR2] Render, where am I being called from?"); // In OnPreCull... OH - // TODO: executing in OnPreCull means we can do jittering in here, at least. Rect manipulation should still happen before PPV2 entirely. + // Effects rendering happens in OnPreCull, so this is the right place to apply camera jittering ApplyJitter(context.camera); - cmd.BlitFullscreenTriangle(context.source, context.destination); + cmd.SetGlobalTexture(Fsr2Pipeline.SrvInputColor, context.source, RenderTextureSubElement.Color); + cmd.SetGlobalTexture(Fsr2Pipeline.SrvInputDepth, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); + cmd.SetGlobalTexture(Fsr2Pipeline.SrvInputMotionVectors, BuiltinRenderTextureType.MotionVectors); + + SetupDispatchDescription(context); + + if (settings.autoGenerateReactiveMask) + { + // TODO: auto-generate reactive mask + } + + cmd.GetTemporaryRT(Fsr2Pipeline.UavUpscaledOutput, DisplaySize.x, DisplaySize.y, 0, default, context.sourceFormat, default, 1, true); + + _fsrContext.Dispatch(_dispatchDescription, cmd); + + cmd.BlitFullscreenTriangle(Fsr2Pipeline.UavUpscaledOutput, context.destination); + cmd.ReleaseTemporaryRT(Fsr2Pipeline.UavUpscaledOutput); } /// @@ -189,6 +208,45 @@ namespace FidelityFX camera.projectionMatrix = jitterTranslationMatrix * camera.nonJitteredProjectionMatrix; camera.useJitteredProjectionMatrixForTransparentRendering = true; } + + private void SetupDispatchDescription(PostProcessRenderContext context) + { + var camera = context.camera; + + // Set up the main FSR2 dispatch parameters + // The input and output textures are left blank here, as they are already being bound elsewhere in this source file + _dispatchDescription.Color = null; + _dispatchDescription.Depth = null; + _dispatchDescription.MotionVectors = null; + _dispatchDescription.Exposure = null; + _dispatchDescription.Reactive = null; + _dispatchDescription.TransparencyAndComposition = null; + + if (!settings.enableAutoExposure && settings.exposure.value != null) _dispatchDescription.Exposure = settings.exposure.value; + if (settings.reactiveMask.value != null) _dispatchDescription.Reactive = settings.reactiveMask.value; + if (settings.transparencyAndCompositionMask.value != null) _dispatchDescription.TransparencyAndComposition = settings.transparencyAndCompositionMask.value; + + _dispatchDescription.Output = null; + _dispatchDescription.PreExposure = settings.preExposure; + _dispatchDescription.EnableSharpening = settings.performSharpenPass; + _dispatchDescription.Sharpness = settings.sharpness; + _dispatchDescription.MotionVectorScale.x = -camera.pixelWidth; + _dispatchDescription.MotionVectorScale.y = -camera.pixelHeight; + _dispatchDescription.RenderSize = RenderSize; + _dispatchDescription.InputResourceSize = new Vector2Int(context.width, context.height); + _dispatchDescription.FrameTimeDelta = Time.unscaledDeltaTime; + _dispatchDescription.CameraNear = camera.nearClipPlane; + _dispatchDescription.CameraFar = camera.farClipPlane; + _dispatchDescription.CameraFovAngleVertical = camera.fieldOfView * Mathf.Deg2Rad; + _dispatchDescription.ViewSpaceToMetersFactor = 1.0f; // 1 unit is 1 meter in Unity + _dispatchDescription.Reset = m_ResetHistory; + + if (SystemInfo.usesReversedZBuffer) + { + // Swap the near and far clip plane distances as FSR2 expects this when using inverted depth + (_dispatchDescription.CameraNear, _dispatchDescription.CameraFar) = (_dispatchDescription.CameraFar, _dispatchDescription.CameraNear); + } + } } internal class Fsr2PostProcessHelper : MonoBehaviour