From 21d939cfd4dd967d3f3052a1e3842cd80f68e08d Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 5 Mar 2023 22:36:39 +0100 Subject: [PATCH] Use original camera pixel width and height to determine display size, so that cameras with non-standard viewport rects will work too. --- Assets/Scripts/Fsr2Controller.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Assets/Scripts/Fsr2Controller.cs b/Assets/Scripts/Fsr2Controller.cs index a5e93ed..a644cfc 100644 --- a/Assets/Scripts/Fsr2Controller.cs +++ b/Assets/Scripts/Fsr2Controller.cs @@ -60,6 +60,12 @@ 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(); + _originalDepthTextureMode = _renderCamera.depthTextureMode; + _renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors; + + // Initialize FSR2 context Fsr2.InitializationFlags flags = 0; if (enableFP16) flags |= Fsr2.InitializationFlags.EnableFP16Usage; @@ -68,16 +74,11 @@ namespace FidelityFX flags |= Fsr2.InitializationFlags.EnableDebugChecking; #endif - _displaySize = new Vector2Int(Screen.width, Screen.height); + _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); - // Set up the original camera to output all of the required FSR2 input resources at the desired resolution - _renderCamera = GetComponent(); - _originalDepthTextureMode = _renderCamera.depthTextureMode; - _renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors; - _dispatchCommandBuffer = new CommandBuffer { name = "FSR2 Dispatch" }; // Create command buffers to bind the camera's output at the right moments in the render loop @@ -141,7 +142,7 @@ namespace FidelityFX private void Update() { - if (Screen.width != _prevDisplaySize.x || Screen.height != _prevDisplaySize.y || qualityMode != _prevQualityMode) + if (_renderCamera.pixelWidth != _prevDisplaySize.x || _renderCamera.pixelHeight != _prevDisplaySize.y || qualityMode != _prevQualityMode) { // Force all resources to be destroyed and recreated with the new settings OnDisable(); @@ -168,7 +169,7 @@ namespace FidelityFX { // Render to a smaller portion of the screen by manipulating the camera's viewport rect _originalRect = _renderCamera.rect; - _renderCamera.aspect = (Screen.width * _originalRect.width) / (Screen.height * _originalRect.height); + _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); // Set up the parameters to auto-generate a reactive mask @@ -266,7 +267,7 @@ namespace FidelityFX else { // We are rendering to the backbuffer, so we need a temporary render texture for FSR2 to output to - _dispatchCommandBuffer.GetTemporaryRT(Fsr2Pipeline.UavUpscaledOutput, Screen.width, Screen.height, 0, default, GraphicsFormat.R16G16B16A16_SFloat, 1, true); + _dispatchCommandBuffer.GetTemporaryRT(Fsr2Pipeline.UavUpscaledOutput, _displaySize.x, _displaySize.y, 0, default, GraphicsFormat.R16G16B16A16_SFloat, 1, true); } _context.Dispatch(_dispatchDescription, _dispatchCommandBuffer);