Browse Source

Further cleanup and small details

mac-autoexp
Nico de Poel 3 years ago
parent
commit
8ceffcadd1
  1. 4
      Assets/Scripts/Fsr2.cs
  2. 2
      Assets/Scripts/Fsr2Context.cs
  3. 48
      Assets/Scripts/SubsampleTest.cs

4
Assets/Scripts/Fsr2.cs

@ -61,9 +61,9 @@ namespace FidelityFX
renderHeight = (int)(displayHeight / ratio);
}
public static float GetMipmapBiasOffset(float renderScale)
public static float GetMipmapBiasOffset(int renderWidth, int displayWidth)
{
return Mathf.Log(renderScale, 2.0f) - 1.0f;
return Mathf.Log((float)renderWidth / displayWidth, 2.0f) - 1.0f;
}
public static int GetJitterPhaseCount(int renderWidth, int displayWidth)

2
Assets/Scripts/Fsr2Context.cs

@ -225,6 +225,8 @@ namespace FidelityFX
constants.renderSize = dispatchParams.RenderSize;
constants.maxRenderSize = _contextDescription.MaxRenderSize;
constants.inputColorResourceDimensions = dispatchParams.RenderSize; // We have no way to query the actual width & height of the input resources, so trust that it matches the render size
// TODO: if we want to support dynamic resolution, we may have to differentiate between renderSize and inputColorResourceDimensions again
// TODO: come to think of it, couldn't we use Unity's ScalableBufferManager for dealing with render scale altogether? So we don't have to deal with this nasty double camera business?
// Compute the horizontal FOV for the shader from the vertical one
float aspectRatio = (float)dispatchParams.RenderSize.x / dispatchParams.RenderSize.y;

48
Assets/Scripts/SubsampleTest.cs

@ -6,27 +6,25 @@ using UnityEngine.Rendering;
[RequireComponent(typeof(Camera))]
public class SubsampleTest : MonoBehaviour // TODO: rename this to Fsr2Controller
{
[SerializeField]
private Fsr2.QualityMode qualityMode;
private Camera _renderCamera;
private RenderTexture _originalRenderTarget;
private GameObject _displayCameraObject;
private Camera _displayCamera;
[SerializeField]
private Fsr2.QualityMode qualityMode;
private Fsr2Controller _fsr2Controller;
private float _renderScale;
private Fsr2.QualityMode _prevQualityMode;
private Vector2Int _prevScreenSize;
private CommandBuffer _opaqueOnlyCommandBuffer;
private CommandBuffer _reactiveCommandBuffer;
private CommandBuffer _inputsCommandBuffer;
private void OnEnable()
{
_renderScale = 1.0f / Fsr2.GetUpscaleRatioFromQualityMode(qualityMode);
_renderCamera = GetComponent<Camera>();
if (_displayCameraObject == null)
{
@ -50,36 +48,36 @@ public class SubsampleTest : MonoBehaviour // TODO: rename this to Fsr2Controll
}
_fsr2Controller.renderCamera = _renderCamera;
_fsr2Controller.renderScale = _renderScale;
_fsr2Controller.renderScale = 1.0f / Fsr2.GetUpscaleRatioFromQualityMode(qualityMode);
_fsr2Controller.enabled = true;
Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, Screen.width, Screen.height, qualityMode);
// TODO: check if the camera doesn't already have a target texture
_originalRenderTarget = _renderCamera.targetTexture; // TODO: if this isn't null, could maybe reuse this for the output texture?
_renderCamera.targetTexture = new RenderTexture(
renderWidth, renderHeight,
24, // Can we copy depth value from original camera?
RenderTextureFormat.ARGBHalf); // Can we copy format from original camera? Or renderer quality settings?
_renderCamera.depthTextureMode |= DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
_originalRenderTarget != null ? _originalRenderTarget.depth : 32,
_originalRenderTarget != null ? _originalRenderTarget.format : RenderTextureFormat.ARGBHalf) { name = "FSR2 Input Texture" };
_renderCamera.targetTexture.name = "FSR2 Input Texture";
_renderCamera.targetTexture.Create();
_renderCamera.depthTextureMode |= DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
_opaqueOnlyCommandBuffer = new CommandBuffer { name = "FSR2 Opaque Input" };
// TODO: may need to copy the opaque-only render buffer to a temp RT here, in which case we'll need an additional CommandBuffer to release the temp RT again
_opaqueOnlyCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvOpaqueOnly, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color);
_renderCamera.AddCommandBuffer(CameraEvent.BeforeImageEffectsOpaque, _opaqueOnlyCommandBuffer);
_reactiveCommandBuffer = new CommandBuffer { name = "FSR2 Inputs" };
_reactiveCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputColor, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color);
_reactiveCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputDepth, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth);
_reactiveCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputMotionVectors, BuiltinRenderTextureType.MotionVectors);
_renderCamera.AddCommandBuffer(CameraEvent.BeforeImageEffects, _reactiveCommandBuffer);
_inputsCommandBuffer = new CommandBuffer { name = "FSR2 Inputs" };
_inputsCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputColor, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color);
_inputsCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputDepth, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth);
_inputsCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputMotionVectors, BuiltinRenderTextureType.MotionVectors);
_renderCamera.AddCommandBuffer(CameraEvent.BeforeImageEffects, _inputsCommandBuffer);
// Adjust texture mipmap LOD bias by log2(renderResolution/displayResolution) - 1.0;
// 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(renderWidth, Screen.width);
Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset);
_prevScreenSize = new Vector2Int(Screen.width, Screen.height);
@ -88,19 +86,19 @@ public class SubsampleTest : MonoBehaviour // TODO: rename this to Fsr2Controll
private void OnDisable()
{
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderScale);
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderCamera.targetTexture.width, _prevScreenSize.x);
Fsr2.GlobalCallbacks.ApplyMipmapBias(-biasOffset);
_renderCamera.RemoveCommandBuffer(CameraEvent.BeforeImageEffectsOpaque, _opaqueOnlyCommandBuffer);
_opaqueOnlyCommandBuffer.Release();
_opaqueOnlyCommandBuffer = null;
_renderCamera.RemoveCommandBuffer(CameraEvent.BeforeImageEffects, _reactiveCommandBuffer);
_reactiveCommandBuffer.Release();
_reactiveCommandBuffer = null;
_renderCamera.RemoveCommandBuffer(CameraEvent.BeforeImageEffects, _inputsCommandBuffer);
_inputsCommandBuffer.Release();
_inputsCommandBuffer = null;
_renderCamera.targetTexture.Release();
_renderCamera.targetTexture = null;
_renderCamera.targetTexture = _originalRenderTarget;
_fsr2Controller.enabled = false;
_displayCamera.enabled = false;

Loading…
Cancel
Save