@ -88,17 +88,7 @@ public class Fsr2Controller : MonoBehaviour
// TODO: enable motion vector jitter cancellation or not?
// TODO: enable motion vector jitter cancellation or not?
_context = Fsr2 . CreateContext ( DisplaySize , RenderSize ) ;
_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 , 2 4 , RenderTextureFormat . ARGBHalf ) { enableRandomWrite = true } ; // TODO: does this need to be a UAV?
_outputRT = new RenderTexture ( DisplaySize . x , DisplaySize . y , 2 4 , RenderTextureFormat . ARGBHalf ) { enableRandomWrite = true } ;
_outputRT . Create ( ) ;
_outputRT . Create ( ) ;
_exposure = new Texture2D ( 1 , 1 ) { name = "FSR2 Exposure" } ;
_exposure = new Texture2D ( 1 , 1 ) { name = "FSR2 Exposure" } ;
@ -120,24 +110,6 @@ public class Fsr2Controller : MonoBehaviour
_outputRT = null ;
_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 )
if ( _context ! = null )
{
{
_context . Destroy ( ) ;
_context . Destroy ( ) ;
@ -149,6 +121,12 @@ public class Fsr2Controller : MonoBehaviour
public void UpdateInputResources ( RenderTexture src )
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 , _colorRT ) ;
Graphics . Blit ( src , _depthRT , CopyDepthMaterial ) ;
Graphics . Blit ( src , _depthRT , CopyDepthMaterial ) ;
Graphics . Blit ( src , _motionVectorsRT , CopyMotionVectorsMaterial ) ;
Graphics . Blit ( src , _motionVectorsRT , CopyMotionVectorsMaterial ) ;
@ -163,6 +141,10 @@ public class Fsr2Controller : MonoBehaviour
// 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 )
{
{
// Ensure the input resources were updated correctly before upscaling
if ( _colorRT = = null | | _depthRT = = null | | _motionVectorsRT = = null )
return ;
_dispatchDescription . Color = _colorRT ;
_dispatchDescription . Color = _colorRT ;
_dispatchDescription . Depth = _depthRT ;
_dispatchDescription . Depth = _depthRT ;
_dispatchDescription . MotionVectors = _motionVectorsRT ;
_dispatchDescription . MotionVectors = _motionVectorsRT ;
@ -181,7 +163,12 @@ public class Fsr2Controller : MonoBehaviour
_context . Dispatch ( _dispatchDescription ) ;
_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 ) ;
Graphics . Blit ( _outputRT , dest ) ;
}
}
}
}