Browse Source

Cleaned up Fsr2Controller ahead of major refactor

mac-autoexp
Nico de Poel 3 years ago
parent
commit
3de40f6fe6
  1. 78
      Assets/Scripts/Fsr2Controller.cs
  2. 3
      Assets/Scripts/SubsampleTest.cs

78
Assets/Scripts/Fsr2Controller.cs

@ -1,7 +1,5 @@
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using FidelityFX; using FidelityFX;
using UnityEngine; using UnityEngine;
using UnityEngine.Experimental.Rendering; using UnityEngine.Experimental.Rendering;
@ -24,10 +22,7 @@ public class Fsr2Controller : MonoBehaviour // TODO: rename this to Fsr2Disp
private bool reset; private bool reset;
[HideInInspector] [HideInInspector]
public Camera gameCamera;
[HideInInspector]
public Camera outputCamera;
public Camera renderCamera;
[HideInInspector] [HideInInspector]
public float renderScale; public float renderScale;
@ -39,25 +34,7 @@ public class Fsr2Controller : MonoBehaviour // TODO: rename this to Fsr2Disp
private Fsr2Context _context; private Fsr2Context _context;
private readonly Fsr2.DispatchDescription _dispatchDescription = new Fsr2.DispatchDescription(); private readonly Fsr2.DispatchDescription _dispatchDescription = new Fsr2.DispatchDescription();
public Fsr2Context Context => _context;
private RenderTexture _upscaledOutput;
private Material _copyMotionMat;
private Material CopyMotionVectorsMaterial
{
get
{
if (_copyMotionMat == null)
{
var copyMotionShader = Fsr2.GlobalCallbacks.LoadShader("Shaders/FSR2_CopyMotionVectors");
_copyMotionMat = new Material(copyMotionShader);
}
return _copyMotionMat;
}
}
private CommandBuffer _commandBuffer;
private void Start() private void Start()
{ {
@ -72,21 +49,17 @@ public class Fsr2Controller : MonoBehaviour // TODO: rename this to Fsr2Disp
return; return;
_context = Fsr2.CreateContext(DisplaySize, RenderSize, Fsr2.InitializationFlags.EnableMotionVectorsJitterCancellation); _context = Fsr2.CreateContext(DisplaySize, RenderSize, Fsr2.InitializationFlags.EnableMotionVectorsJitterCancellation);
// 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
_upscaledOutput = new RenderTexture(DisplaySize.x, DisplaySize.y, 0, RenderTextureFormat.ARGBHalf) { name = "FSR2 Upscaled Output", enableRandomWrite = true };
_upscaledOutput.Create();
_commandBuffer = new CommandBuffer { name = "FSR2 Dispatch" };
} }
private void OnDisable() private void OnDisable()
{ {
if (_upscaledOutput != null)
if (_commandBuffer != null)
{ {
_upscaledOutput.Release();
_upscaledOutput = null;
_commandBuffer.Release();
_commandBuffer = null;
} }
if (_context != null) if (_context != null)
{ {
_context.Destroy(); _context.Destroy();
@ -102,28 +75,18 @@ public class Fsr2Controller : MonoBehaviour // TODO: rename this to Fsr2Disp
// For legacy built-in render pipeline // For legacy built-in render pipeline
private void OnRenderImage(RenderTexture src, RenderTexture dest) private void OnRenderImage(RenderTexture src, RenderTexture dest)
{ {
var renderBuffer = gameCamera.targetTexture;
var commandBuffer = new CommandBuffer { name = "FSR2 Main" };
// int motionVectorsId = Shader.PropertyToID("r_input_motion_vectors");
int upscaledOutputId = Shader.PropertyToID("rw_upscaled_output");
// I hate having to allocate extra RTs just to duplicate already existing Unity render buffers, but AFAIK there is no way to directly address motion vectors from code
// TODO: or can we? Look at RenderTargetIdentifier.MotionVectors with SetGlobalTexture!! (Possibly in gameCamera OnRenderImage)
// commandBuffer.GetTemporaryRT(motionVectorsId, renderBuffer.width, renderBuffer.height, 0, default, RenderTextureFormat.RGHalf);
// commandBuffer.Blit(renderBuffer, motionVectorsId, CopyMotionVectorsMaterial);
_commandBuffer.Clear();
if (dest != null) if (dest != null)
{ {
// We have more image effects lined up after this, so FSR2 can output straight to the intermediate render texture // We have more image effects lined up after this, so FSR2 can output straight to the intermediate render texture
// 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
commandBuffer.SetGlobalTexture(upscaledOutputId, dest);
_commandBuffer.SetGlobalTexture(Fsr2Pipeline.UavUpscaledOutput, dest);
} }
else else
{ {
// We are rendering to the backbuffer, so we need a temporary render texture for FSR2 to output to // We are rendering to the backbuffer, so we need a temporary render texture for FSR2 to output to
commandBuffer.GetTemporaryRT(upscaledOutputId, DisplaySize.x, DisplaySize.y, 0, default, GraphicsFormat.R16G16B16A16_SFloat, 1, true);
_commandBuffer.GetTemporaryRT(Fsr2Pipeline.UavUpscaledOutput, DisplaySize.x, DisplaySize.y, 0, default, GraphicsFormat.R16G16B16A16_SFloat, 1, true);
} }
_dispatchDescription.Color = null; _dispatchDescription.Color = null;
@ -135,31 +98,28 @@ public class Fsr2Controller : MonoBehaviour // TODO: rename this to Fsr2Disp
_dispatchDescription.PreExposure = 0; _dispatchDescription.PreExposure = 0;
_dispatchDescription.EnableSharpening = performSharpenPass; _dispatchDescription.EnableSharpening = performSharpenPass;
_dispatchDescription.Sharpness = sharpness; _dispatchDescription.Sharpness = sharpness;
_dispatchDescription.MotionVectorScale.x = -gameCamera.pixelWidth;
_dispatchDescription.MotionVectorScale.y = -gameCamera.pixelHeight;
_dispatchDescription.MotionVectorScale.x = -renderCamera.pixelWidth;
_dispatchDescription.MotionVectorScale.y = -renderCamera.pixelHeight;
_dispatchDescription.RenderSize = RenderSize; _dispatchDescription.RenderSize = RenderSize;
_dispatchDescription.FrameTimeDelta = Time.unscaledDeltaTime; _dispatchDescription.FrameTimeDelta = Time.unscaledDeltaTime;
_dispatchDescription.CameraNear = gameCamera.nearClipPlane;
_dispatchDescription.CameraFar = gameCamera.farClipPlane;
_dispatchDescription.CameraFovAngleVertical = gameCamera.fieldOfView * Mathf.Deg2Rad;
_dispatchDescription.CameraNear = renderCamera.nearClipPlane;
_dispatchDescription.CameraFar = renderCamera.farClipPlane;
_dispatchDescription.CameraFovAngleVertical = renderCamera.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; _dispatchDescription.Reset = reset;
reset = false; reset = false;
_context.Dispatch(_dispatchDescription, commandBuffer);
_context.Dispatch(_dispatchDescription, _commandBuffer);
// Output upscaled image to screen // Output upscaled image to screen
// TODO: if `dest` is null, we likely don't care about the depth & motion vectors anymore // TODO: if `dest` is null, we likely don't care about the depth & motion vectors anymore
if (dest == null) if (dest == null)
{ {
commandBuffer.Blit(upscaledOutputId, dest);
commandBuffer.ReleaseTemporaryRT(upscaledOutputId);
_commandBuffer.Blit(Fsr2Pipeline.UavUpscaledOutput, dest);
_commandBuffer.ReleaseTemporaryRT(Fsr2Pipeline.UavUpscaledOutput);
} }
// commandBuffer.ReleaseTemporaryRT(motionVectorsId);
Graphics.ExecuteCommandBuffer(commandBuffer);
commandBuffer.Release();
Graphics.ExecuteCommandBuffer(_commandBuffer);
// Shut up the Unity warning about not writing to the destination texture // Shut up the Unity warning about not writing to the destination texture
Graphics.SetRenderTarget(dest); Graphics.SetRenderTarget(dest);

3
Assets/Scripts/SubsampleTest.cs

@ -49,8 +49,7 @@ public class SubsampleTest : MonoBehaviour // TODO: rename this to Fsr2Controll
_fsr2Controller = _displayCameraObject.AddComponent<Fsr2Controller>(); _fsr2Controller = _displayCameraObject.AddComponent<Fsr2Controller>();
} }
_fsr2Controller.gameCamera = _renderCamera;
_fsr2Controller.outputCamera = _displayCamera;
_fsr2Controller.renderCamera = _renderCamera;
_fsr2Controller.renderScale = _renderScale; _fsr2Controller.renderScale = _renderScale;
_fsr2Controller.enabled = true; _fsr2Controller.enabled = true;

Loading…
Cancel
Save