@ -13,13 +13,14 @@ namespace UnityEngine.Rendering.PostProcessing
private IntPtr _dispatchParamsBuffer ;
private IntPtr _dispatchParamsBuffer ;
private RenderTexture _inputColor ;
private RenderTexture _inputColor ;
private RenderTexture _inputDepth ;
private RenderTexture _inputMotionVectors ;
private readonly RenderTexture [ ] _inputDepth = new RenderTexture [ 2 ] ;
private readonly RenderTexture [ ] _inputMotionVectors = new RenderTexture [ 2 ] ;
private RenderTexture _outputColor ;
private RenderTexture _outputColor ;
private Texture2D _outputIntermediate ;
private Texture2D _outputIntermediate ;
private bool _pluginInitialized ;
private bool _pluginInitialized ;
private bool _contextInitialized ;
private bool _contextInitialized ;
private uint _frameCount ;
public override void CreateContext ( PostProcessRenderContext context , Upscaling config )
public override void CreateContext ( PostProcessRenderContext context , Upscaling config )
{
{
@ -37,10 +38,11 @@ namespace UnityEngine.Rendering.PostProcessing
initParams . displayHeight = ( uint ) config . UpscaleSize . y ;
initParams . displayHeight = ( uint ) config . UpscaleSize . y ;
initParams . maxRenderWidth = ( uint ) config . MaxRenderSize . x ;
initParams . maxRenderWidth = ( uint ) config . MaxRenderSize . x ;
initParams . maxRenderHeight = ( uint ) config . MaxRenderSize . y ;
initParams . maxRenderHeight = ( uint ) config . MaxRenderSize . y ;
initParams . autoKeepCopies = 0 u ; // We use double buffered depth and motion vector copies created by Unity
CreateRenderTexture ( ref _inputColor , "PSSR Input Color" , config . MaxRenderSize , context . sourceFormat , true ) ;
CreateRenderTexture ( ref _inputColor , "PSSR Input Color" , config . MaxRenderSize , context . sourceFormat , true ) ;
CreateRenderTexture ( ref _inputDepth , "PSSR Input Depth" , config . MaxRenderSize , GraphicsFormat . R32_SFloat , true ) ; // TODO: this texture won't have tile mode kDepth, meaning PSSR doesn't detect it correctly. Is this a problem?
CreateRenderTexture ( ref _inputMotionVectors , "PSSR Input Motion Vectors" , config . MaxRenderSize , GraphicsFormat . R16G16_SFloat , true ) ;
CreateRenderTextureArray ( _inputDepth , "PSSR Input Depth" , config . MaxRenderSize , GraphicsFormat . R32_SFloat , true ) ;
CreateRenderTextureArray ( _inputMotionVectors , "PSSR Input Motion Vectors" , config . MaxRenderSize , GraphicsFormat . R16G16_SFloat , true ) ;
CreateRenderTexture ( ref _outputColor , "PSSR Output Color" , config . UpscaleSize , RenderTextureFormat . RGB111110Float ) ;
CreateRenderTexture ( ref _outputColor , "PSSR Output Color" , config . UpscaleSize , RenderTextureFormat . RGB111110Float ) ;
if ( PSSRPlugin . CreatePssrContext ( ref initParams , out IntPtr outputColorTexturePtr ) > = 0 & & outputColorTexturePtr ! = IntPtr . Zero )
if ( PSSRPlugin . CreatePssrContext ( ref initParams , out IntPtr outputColorTexturePtr ) > = 0 & & outputColorTexturePtr ! = IntPtr . Zero )
@ -69,7 +71,8 @@ namespace UnityEngine.Rendering.PostProcessing
}
}
DestroyRenderTexture ( ref _outputColor ) ;
DestroyRenderTexture ( ref _outputColor ) ;
DestroyRenderTexture ( ref _inputDepth ) ;
DestroyRenderTextureArray ( _inputMotionVectors ) ;
DestroyRenderTextureArray ( _inputDepth ) ;
DestroyRenderTexture ( ref _inputColor ) ;
DestroyRenderTexture ( ref _inputColor ) ;
if ( _dispatchParamsBuffer ! = IntPtr . Zero )
if ( _dispatchParamsBuffer ! = IntPtr . Zero )
@ -95,24 +98,46 @@ namespace UnityEngine.Rendering.PostProcessing
}
}
cmd . BeginSample ( "PSSR" ) ;
cmd . BeginSample ( "PSSR" ) ;
if ( config . Reset | | _frameCount = = 0 )
{
cmd . SetRenderTarget ( _inputDepth [ 0 ] ) ;
cmd . ClearRenderTarget ( false , true , Color . clear ) ;
cmd . SetRenderTarget ( _inputDepth [ 1 ] ) ;
cmd . ClearRenderTarget ( false , true , Color . clear ) ;
cmd . SetRenderTarget ( _inputMotionVectors [ 0 ] ) ;
cmd . ClearRenderTarget ( false , true , Color . clear ) ;
cmd . SetRenderTarget ( _inputMotionVectors [ 1 ] ) ;
cmd . ClearRenderTarget ( false , true , Color . clear ) ;
}
int frameIndex = ( int ) ( _frameCount + + % 2 ) ;
// TODO: if PSSR needs a copy of the previous depth and motion vectors anyway, why not just double-buffer those resources ourselves and make good use of these otherwise dumb copies?
PrepareInputs ( cmd , context , config , _inputColor , _inputDepth , _inputMotionVectors ) ;
// We need to provide copies of the previous depth and motion vector buffers anyway, so we can turn this otherwise wasteful copying into a benefit
PrepareInputs ( cmd , context , config , _inputColor , _inputDepth [ frameIndex ] , _inputMotionVectors [ frameIndex ] ) ;
Texture reactiveMask = config . reactiveMask ;
if ( config . autoGenerateReactiveMask | | config . autoGenerateTransparencyAndComposition )
{
reactiveMask = GenerateReactiveMask ( cmd , context , config ) ;
}
var flags = PSSRPlugin . OptionFlags . None ;
var flags = PSSRPlugin . OptionFlags . None ;
if ( SystemInfo . usesReversedZBuffer ) flags | = PSSRPlugin . OptionFlags . ReverseDepth ;
if ( SystemInfo . usesReversedZBuffer ) flags | = PSSRPlugin . OptionFlags . ReverseDepth ;
if ( config . exposureSource = = Upscaling . ExposureSource . Auto ) flags | = PSSRPlugin . OptionFlags . AutoExposure ;
if ( config . exposureSource = = Upscaling . ExposureSource . Auto ) flags | = PSSRPlugin . OptionFlags . AutoExposure ;
_dispatchParams . color = ToNativePtr ( _inputColor ) ;
_dispatchParams . color = ToNativePtr ( _inputColor ) ;
_dispatchParams . depth = ToNativePtr ( _inputDepth ) ;
_dispatchParams . motionVectors = ToNativePtr ( _inputMotionVectors ) ;
_dispatchParams . depth = ToNativePtr ( _inputDepth [ frameIndex ] ) ;
_dispatchParams . prevDepth = ToNativePtr ( _inputDepth [ frameIndex ^ 1 ] ) ;
_dispatchParams . motionVectors = ToNativePtr ( _inputMotionVectors [ frameIndex ] ) ;
_dispatchParams . prevMotionVectors = ToNativePtr ( _inputMotionVectors [ frameIndex ^ 1 ] ) ;
_dispatchParams . exposure = ToNativePtr ( config . exposureSource switch
_dispatchParams . exposure = ToNativePtr ( config . exposureSource switch
{
{
Upscaling . ExposureSource . Manual when config . exposure ! = null = > config . exposure ,
Upscaling . ExposureSource . Manual when config . exposure ! = null = > config . exposure ,
Upscaling . ExposureSource . Unity = > context . autoExposureTexture ,
Upscaling . ExposureSource . Unity = > context . autoExposureTexture ,
_ = > null ,
_ = > null ,
} ) ;
} ) ;
_dispatchParams . reactiveMask = ToNativePtr ( config . reactiveMask ) ;
_dispatchParams . reactiveMask = ToNativePtr ( reactiveMask ) ;
_dispatchParams . outputColor = ToNativePtr ( _outputIntermediate ) ;
_dispatchParams . outputColor = ToNativePtr ( _outputIntermediate ) ;
var scaledRenderSize = config . GetScaledRenderSize ( context . camera ) ;
var scaledRenderSize = config . GetScaledRenderSize ( context . camera ) ;
@ -165,6 +190,8 @@ namespace UnityEngine.Rendering.PostProcessing
public uint displayHeight ;
public uint displayHeight ;
public uint maxRenderWidth ;
public uint maxRenderWidth ;
public uint maxRenderHeight ;
public uint maxRenderHeight ;
public uint autoKeepCopies ;
}
}
[Serializable, StructLayout(LayoutKind.Sequential)]
[Serializable, StructLayout(LayoutKind.Sequential)]
@ -172,7 +199,9 @@ namespace UnityEngine.Rendering.PostProcessing
{
{
public IntPtr color ;
public IntPtr color ;
public IntPtr depth ;
public IntPtr depth ;
public IntPtr prevDepth ;
public IntPtr motionVectors ;
public IntPtr motionVectors ;
public IntPtr prevMotionVectors ;
public IntPtr exposure ;
public IntPtr exposure ;
public IntPtr reactiveMask ;
public IntPtr reactiveMask ;
public IntPtr outputColor ;
public IntPtr outputColor ;