From 1c030377c0c7e1b1df500dee42d4a1a7b0bd1f2c Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 18 Mar 2023 14:23:50 +0100 Subject: [PATCH] Reshufled some code so that if the initialization process fails for some reason, we don't leave the scene behind in a messed up state. --- Assets/Scripts/Fsr2ImageEffect.cs | 38 +++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/Assets/Scripts/Fsr2ImageEffect.cs b/Assets/Scripts/Fsr2ImageEffect.cs index 2e7ea37..7077e4f 100644 --- a/Assets/Scripts/Fsr2ImageEffect.cs +++ b/Assets/Scripts/Fsr2ImageEffect.cs @@ -69,6 +69,22 @@ namespace FidelityFX private void OnEnable() { + // Set up the original camera to output all of the required FSR2 input resources at the desired resolution + _renderCamera = GetComponent(); + _originalRenderTarget = _renderCamera.targetTexture; + _originalDepthTextureMode = _renderCamera.depthTextureMode; + _renderCamera.targetTexture = null; // Clear the camera's target texture so we can fully control how the output gets written + _renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors; + + // Determine the desired rendering and display resolutions + _displaySize = new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight); + Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, _displaySize.x, _displaySize.y, qualityMode); + _renderSize = new Vector2Int(renderWidth, renderHeight); + + // Apply a mipmap bias so that textures retain their sharpness + float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _displaySize.x); + Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset); + if (!SystemInfo.supportsComputeShaders) { Debug.LogError("FSR2 requires compute shader support!"); @@ -78,22 +94,12 @@ namespace FidelityFX _helper = GetComponent(); - // Set up the original camera to output all of the required FSR2 input resources at the desired resolution - _renderCamera = GetComponent(); - _originalRenderTarget = _renderCamera.targetTexture; - _originalDepthTextureMode = _renderCamera.depthTextureMode; - _renderCamera.targetTexture = null; // Clear the camera's target texture so we can fully control how the output gets written - _renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors; - // Initialize FSR2 context Fsr2.InitializationFlags flags = 0; if (_renderCamera.allowHDR) flags |= Fsr2.InitializationFlags.EnableHighDynamicRange; if (enableFP16) flags |= Fsr2.InitializationFlags.EnableFP16Usage; if (enableAutoExposure) flags |= Fsr2.InitializationFlags.EnableAutoExposure; - _displaySize = new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight); - Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, _displaySize.x, _displaySize.y, qualityMode); - _renderSize = new Vector2Int(renderWidth, renderHeight); _context = Fsr2.CreateContext(_displaySize, _renderSize, flags); _dispatchCommandBuffer = new CommandBuffer { name = "FSR2 Dispatch" }; @@ -108,10 +114,6 @@ namespace FidelityFX _renderCamera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer); } - // Apply a mipmap bias so that textures retain their sharpness - float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _displaySize.x); - Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset); - _prevDisplaySize = _displaySize; _prevQualityMode = qualityMode; _prevGenReactiveMask = autoGenerateReactiveMask; @@ -119,8 +121,13 @@ namespace FidelityFX private void OnDisable() { + // Undo the current mipmap bias offset float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _prevDisplaySize.x); Fsr2.GlobalCallbacks.ApplyMipmapBias(-biasOffset); + + // Restore the camera's original state + _renderCamera.depthTextureMode = _originalDepthTextureMode; + _renderCamera.targetTexture = _originalRenderTarget; if (_opaqueInputCommandBuffer != null) { @@ -135,9 +142,6 @@ namespace FidelityFX _dispatchCommandBuffer = null; } - _renderCamera.depthTextureMode = _originalDepthTextureMode; - _renderCamera.targetTexture = _originalRenderTarget; - if (_context != null) { _context.Destroy();