Browse Source

Use temporary render textures for storing color, depth and motion vectors copied from the game camera.

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

47
Assets/Scripts/Fsr2Controller.cs

@ -88,17 +88,7 @@ public class Fsr2Controller : MonoBehaviour
// TODO: enable motion vector jitter cancellation or not?
_context = Fsr2.CreateContext(DisplaySize, RenderSize);
// TODO: could make these temporary RTs
_colorRT = new RenderTexture(RenderSize.x, RenderSize.y, 0, RenderTextureFormat.ARGBHalf) { name = "FSR2 Color Input" };
_colorRT.Create();
_depthRT = new RenderTexture(RenderSize.x, RenderSize.y, 0, RenderTextureFormat.RFloat) { name = "FSR2 Depth Input" };
_depthRT.Create();
_motionVectorsRT = new RenderTexture(RenderSize.x, RenderSize.y, 0, RenderTextureFormat.RGHalf) { name = "FSR2 Motion Vectors Input" };
_motionVectorsRT.Create();
_outputRT = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf) { enableRandomWrite = true }; // TODO: does this need to be a UAV?
_outputRT = new RenderTexture(DisplaySize.x, DisplaySize.y, 24, RenderTextureFormat.ARGBHalf) { enableRandomWrite = true };
_outputRT.Create();
_exposure = new Texture2D(1, 1) { name = "FSR2 Exposure" };
@ -120,24 +110,6 @@ public class Fsr2Controller : MonoBehaviour
_outputRT = null;
}
if (_motionVectorsRT != null)
{
_motionVectorsRT.Release();
_motionVectorsRT = null;
}
if (_depthRT != null)
{
_depthRT.Release();
_depthRT = null;
}
if (_colorRT != null)
{
_colorRT.Release();
_colorRT = null;
}
if (_context != null)
{
_context.Destroy();
@ -149,6 +121,12 @@ public class Fsr2Controller : MonoBehaviour
public void UpdateInputResources(RenderTexture src)
{
// I hate having to allocate extra RTs just to duplicate already existing Unity render buffers, but AFAIK there is no way to directly address these buffers individually from code
var renderSize = RenderSize;
_colorRT = RenderTexture.GetTemporary(renderSize.x, renderSize.y, 0, RenderTextureFormat.ARGBHalf);
_depthRT = RenderTexture.GetTemporary(renderSize.x, renderSize.y, 0, RenderTextureFormat.RFloat);
_motionVectorsRT = RenderTexture.GetTemporary(renderSize.x, renderSize.y, 0, RenderTextureFormat.RGHalf);
Graphics.Blit(src, _colorRT);
Graphics.Blit(src, _depthRT, CopyDepthMaterial);
Graphics.Blit(src, _motionVectorsRT, CopyMotionVectorsMaterial);
@ -163,6 +141,10 @@ public class Fsr2Controller : MonoBehaviour
// For legacy built-in render pipeline
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
// Ensure the input resources were updated correctly before upscaling
if (_colorRT == null || _depthRT == null || _motionVectorsRT == null)
return;
_dispatchDescription.Color = _colorRT;
_dispatchDescription.Depth = _depthRT;
_dispatchDescription.MotionVectors = _motionVectorsRT;
@ -181,7 +163,12 @@ public class Fsr2Controller : MonoBehaviour
_context.Dispatch(_dispatchDescription);
// Output sharpened image to screen
RenderTexture.ReleaseTemporary(_colorRT);
RenderTexture.ReleaseTemporary(_depthRT);
RenderTexture.ReleaseTemporary(_motionVectorsRT);
_colorRT = _depthRT = _motionVectorsRT = null;
// Output upscaled image to screen
Graphics.Blit(_outputRT, dest);
}
}

4
Assets/Scripts/SubsampleTest.cs

@ -116,7 +116,11 @@ public class SubsampleTest : MonoBehaviour
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
// We don't actually output any image from this camera; instead we pass its render buffers on to the FSR2 system
_fsr2Controller.UpdateInputResources(src);
// Shut up Unity warning about not writing anything to the destination texture
Graphics.SetRenderTarget(dest);
}
private void OnPostRender()

Loading…
Cancel
Save