From 644349b16bb31adcea440bfe7194fcb72e44ae6b Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Fri, 24 Mar 2023 14:23:29 +0100 Subject: [PATCH] Respect output target texture's dimensions when determining render size and rescaling camera viewport. --- Assets/Scripts/Fsr2ImageEffect.cs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Fsr2ImageEffect.cs b/Assets/Scripts/Fsr2ImageEffect.cs index 5a79293..1cf586c 100644 --- a/Assets/Scripts/Fsr2ImageEffect.cs +++ b/Assets/Scripts/Fsr2ImageEffect.cs @@ -103,7 +103,7 @@ namespace FidelityFX _renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors; // Determine the desired rendering and display resolutions - _displaySize = new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight); + _displaySize = GetDisplaySize(); Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, _displaySize.x, _displaySize.y, qualityMode); _renderSize = new Vector2Int(renderWidth, renderHeight); @@ -185,7 +185,8 @@ namespace FidelityFX private void Update() { - if (_renderCamera.pixelWidth != _prevDisplaySize.x || _renderCamera.pixelHeight != _prevDisplaySize.y || qualityMode != _prevQualityMode) + var displaySize = GetDisplaySize(); + if (displaySize.x != _prevDisplaySize.x || displaySize.y != _prevDisplaySize.y || qualityMode != _prevQualityMode) { // Force all resources to be destroyed and recreated with the new settings OnDisable(); @@ -219,8 +220,8 @@ namespace FidelityFX if (_helper == null || !_helper.enabled) { // Render to a smaller portion of the screen by manipulating the camera's viewport rect - _renderCamera.aspect = (_renderCamera.pixelWidth * _originalRect.width) / (_renderCamera.pixelHeight * _originalRect.height); - _renderCamera.rect = new Rect(0, 0, _originalRect.width * _renderSize.x / _displaySize.x, _originalRect.height * _renderSize.y / _displaySize.y); + _renderCamera.aspect = (_displaySize.x * _originalRect.width) / (_displaySize.y * _originalRect.height); + _renderCamera.rect = new Rect(0, 0, _originalRect.width * _renderSize.x / _renderCamera.pixelWidth, _originalRect.height * _renderSize.y / _renderCamera.pixelHeight); } // Set up the parameters to auto-generate a reactive mask @@ -322,13 +323,12 @@ namespace FidelityFX _context.Dispatch(_dispatchDescription, _dispatchCommandBuffer); // Output the upscaled image - if (_originalRenderTarget != null || outputColorDepth != null) + var outputTexture = GetOutputTexture(); + if (outputTexture != null) { - var targetTexture = outputColorDepth != null ? outputColorDepth : _originalRenderTarget; - // Output to the camera target texture, passing through depth and motion vectors _dispatchCommandBuffer.SetGlobalTexture("_DepthTex", BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); - _dispatchCommandBuffer.Blit(Fsr2ShaderIDs.UavUpscaledOutput, targetTexture, _copyWithDepthMaterial); + _dispatchCommandBuffer.Blit(Fsr2ShaderIDs.UavUpscaledOutput, outputTexture, _copyWithDepthMaterial); if (outputMotionVectors != null) _dispatchCommandBuffer.Blit(BuiltinRenderTextureType.MotionVectors, outputMotionVectors); } @@ -350,5 +350,16 @@ namespace FidelityFX // Shut up the Unity warning about not writing to the destination texture RenderTexture.active = dest; } + + private RenderTexture GetOutputTexture() => outputColorDepth != null ? outputColorDepth : _originalRenderTarget; + + private Vector2Int GetDisplaySize() + { + var outputTexture = GetOutputTexture(); + if (outputTexture != null) + return new Vector2Int(outputTexture.width, outputTexture.height); + + return new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight); + } } }