Browse Source

Playing around with resetting and recreating FSR context upon resolution or quality mode change. Seems to be working quite well.

mac-autoexp
Nico de Poel 3 years ago
parent
commit
e08a8f9c22
  1. 2
      Assets/Scripts/Fsr2.cs
  2. 22
      Assets/Scripts/Fsr2Controller.cs
  3. 27
      Assets/Scripts/SubsampleTest.cs

2
Assets/Scripts/Fsr2.cs

@ -20,6 +20,8 @@ namespace FidelityFX
else else
flags &= ~InitializationFlags.EnableDepthInverted; flags &= ~InitializationFlags.EnableDepthInverted;
Debug.Log($"Setting up FSR2 with render size: {maxRenderSize.x}x{maxRenderSize.y}, display size: {displaySize.x}x{displaySize.y}, flags: {flags}");
var contextDescription = new ContextDescription var contextDescription = new ContextDescription
{ {
Flags = flags, Flags = flags,

22
Assets/Scripts/Fsr2Controller.cs

@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using FidelityFX; using FidelityFX;
using UnityEngine; using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering; using UnityEngine.Rendering;
/// <summary> /// <summary>
@ -20,6 +19,9 @@ public class Fsr2Controller : MonoBehaviour
[SerializeField, Range(0, 1)] [SerializeField, Range(0, 1)]
private float sharpness = 0.8f; private float sharpness = 0.8f;
[SerializeField]
private bool reset;
[HideInInspector] [HideInInspector]
public Camera gameCamera; public Camera gameCamera;
@ -37,7 +39,7 @@ public class Fsr2Controller : MonoBehaviour
private Fsr2Context _context; private Fsr2Context _context;
private readonly Fsr2.DispatchDescription _dispatchDescription = new Fsr2.DispatchDescription(); private readonly Fsr2.DispatchDescription _dispatchDescription = new Fsr2.DispatchDescription();
private RenderTexture _outputRT;
private RenderTexture _upscaledOutput;
private Material _copyMotionMat; private Material _copyMotionMat;
private Material CopyMotionVectorsMaterial private Material CopyMotionVectorsMaterial
@ -73,16 +75,16 @@ public class Fsr2Controller : MonoBehaviour
// TODO: do we need a depth buffer for the output? We will need depth & motion vectors for subsequent post-FX. How should FSR2 output these? // TODO: do we need a depth buffer for the output? We will need depth & motion vectors for subsequent post-FX. How should FSR2 output these?
// TODO: can probably be a temporary RT // TODO: can probably be a temporary RT
_outputRT = new RenderTexture(DisplaySize.x, DisplaySize.y, 24, RenderTextureFormat.ARGBHalf) { name = "FSR2 Upscaled Output", enableRandomWrite = true };
_outputRT.Create();
_upscaledOutput = new RenderTexture(DisplaySize.x, DisplaySize.y, 24, RenderTextureFormat.ARGBHalf) { name = "FSR2 Upscaled Output", enableRandomWrite = true };
_upscaledOutput.Create();
} }
private void OnDisable() private void OnDisable()
{ {
if (_outputRT != null)
if (_upscaledOutput != null)
{ {
_outputRT.Release();
_outputRT = null;
_upscaledOutput.Release();
_upscaledOutput = null;
} }
if (_context != null) if (_context != null)
@ -116,7 +118,7 @@ public class Fsr2Controller : MonoBehaviour
_dispatchDescription.ColorDepth = renderBuffer; _dispatchDescription.ColorDepth = renderBuffer;
_dispatchDescription.MotionVectors = motionVectors; _dispatchDescription.MotionVectors = motionVectors;
_dispatchDescription.Output = _outputRT;
_dispatchDescription.Output = _upscaledOutput;
_dispatchDescription.Exposure = null; _dispatchDescription.Exposure = null;
_dispatchDescription.Reactive = null; _dispatchDescription.Reactive = null;
_dispatchDescription.PreExposure = 0; _dispatchDescription.PreExposure = 0;
@ -130,6 +132,8 @@ public class Fsr2Controller : MonoBehaviour
_dispatchDescription.CameraFar = gameCamera.farClipPlane; _dispatchDescription.CameraFar = gameCamera.farClipPlane;
_dispatchDescription.CameraFovAngleVertical = gameCamera.fieldOfView * Mathf.Deg2Rad; _dispatchDescription.CameraFovAngleVertical = gameCamera.fieldOfView * Mathf.Deg2Rad;
_dispatchDescription.ViewSpaceToMetersFactor = 1.0f; // 1 unit is 1 meter in Unity _dispatchDescription.ViewSpaceToMetersFactor = 1.0f; // 1 unit is 1 meter in Unity
_dispatchDescription.Reset = reset;
reset = false;
_context.Dispatch(_dispatchDescription); _context.Dispatch(_dispatchDescription);
@ -137,6 +141,6 @@ public class Fsr2Controller : MonoBehaviour
// Output upscaled image to screen // Output upscaled image to screen
// TODO: we should probably use a shader to include depth & motion vectors into the output // TODO: we should probably use a shader to include depth & motion vectors into the output
Graphics.Blit(_outputRT, dest);
Graphics.Blit(_upscaledOutput, dest);
} }
} }

27
Assets/Scripts/SubsampleTest.cs

@ -22,6 +22,9 @@ public class SubsampleTest : MonoBehaviour
private Fsr2Controller _fsr2Controller; private Fsr2Controller _fsr2Controller;
private float _renderScale; private float _renderScale;
private Fsr2.QualityMode _prevQualityMode;
private Vector2Int _prevScreenSize;
private void OnEnable() private void OnEnable()
{ {
_renderScale = 1.0f / Fsr2.GetUpscaleRatioFromQualityMode(qualityMode); _renderScale = 1.0f / Fsr2.GetUpscaleRatioFromQualityMode(qualityMode);
@ -35,24 +38,19 @@ public class SubsampleTest : MonoBehaviour
//outputCameraObject.transform.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector; //outputCameraObject.transform.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
outputCamera = outputCameraObject.AddComponent<Camera>(); outputCamera = outputCameraObject.AddComponent<Camera>();
//outputCamera.CopyFrom(gameCamera);
outputCamera.eventMask = 0; outputCamera.eventMask = 0;
outputCamera.cullingMask = 0;
outputCamera.clearFlags = CameraClearFlags.Nothing;
_fsr2Controller = outputCameraObject.AddComponent<Fsr2Controller>(); _fsr2Controller = outputCameraObject.AddComponent<Fsr2Controller>();
}
_fsr2Controller.gameCamera = gameCamera; _fsr2Controller.gameCamera = gameCamera;
_fsr2Controller.outputCamera = outputCamera; _fsr2Controller.outputCamera = outputCamera;
_fsr2Controller.renderScale = _renderScale; _fsr2Controller.renderScale = _renderScale;
_fsr2Controller.enabled = true; _fsr2Controller.enabled = true;
//outputCamera.CopyFrom(gameCamera);
outputCamera.cullingMask = 0;
outputCamera.clearFlags = CameraClearFlags.Nothing;
}
else
{
_fsr2Controller.enabled = true;
}
Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, Screen.width, Screen.height, qualityMode); Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, Screen.width, Screen.height, qualityMode);
gameCamera.targetTexture = new RenderTexture( gameCamera.targetTexture = new RenderTexture(
@ -69,6 +67,9 @@ public class SubsampleTest : MonoBehaviour
// May need to leave this to the game dev, as we don't know which textures do and don't belong to the 3D scene // May need to leave this to the game dev, as we don't know which textures do and don't belong to the 3D scene
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderScale); float biasOffset = Fsr2.GetMipmapBiasOffset(_renderScale);
Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset); Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset);
_prevScreenSize = new Vector2Int(Screen.width, Screen.height);
_prevQualityMode = qualityMode;
} }
private void OnDisable() private void OnDisable()
@ -96,6 +97,12 @@ public class SubsampleTest : MonoBehaviour
//outputCamera.CopyFrom(gameCamera); //outputCamera.CopyFrom(gameCamera);
outputCamera.clearFlags = CameraClearFlags.Color; outputCamera.clearFlags = CameraClearFlags.Color;
if (Screen.width != _prevScreenSize.x || Screen.height != _prevScreenSize.y || qualityMode != _prevQualityMode)
{
enabled = false;
enabled = true;
}
} }
private Rect _tempRect; private Rect _tempRect;

Loading…
Cancel
Save