@ -1,6 +1,8 @@
using System ;
using System.Runtime.InteropServices ;
using UnityEngine.Experimental.Rendering ;
#if UNITY_PS5
using WW1.PlayStation ;
#endif
namespace UnityEngine.Rendering.PostProcessing
{
@ -12,8 +14,8 @@ namespace UnityEngine.Rendering.PostProcessing
private static readonly bool PluginInitialized ;
private static uint _currentContext ;
private PSSRPlugin . DispatchParams _dispatchParams ;
private IntPtr _dispatchParamsBuffer ;
private readonly PSSRPlugin . NativeData < PSSRPlugin . DispatchParams > _dispatchParams = new ( ) ;
private readonly PSSRPlugin . NativeData < PSSRPlugin . DestroyParams > _destroyParams = new ( ) ;
private RenderTexture _inputColor ;
private readonly RenderTexture [ ] _inputDepth = new RenderTexture [ 2 ] ;
@ -27,7 +29,7 @@ namespace UnityEngine.Rendering.PostProcessing
{
try
{
if ( PSSRPlugin . InitPssr ( ) < 0 )
if ( PSSRPlugin . Init ( ) < 0 )
{
Debug . LogError ( "Failed to initialize PSSR plugin!" ) ;
PluginInitialized = false ;
@ -65,12 +67,13 @@ namespace UnityEngine.Rendering.PostProcessing
CreateRenderTextureArray ( _inputDepth , "PSSR Input Depth" , config . MaxRenderSize , GraphicsFormat . R32_SFloat , true ) ;
CreateRenderTextureArray ( _inputMotionVectors , "PSSR Input Motion Vectors" , config . MaxRenderSize , GraphicsFormat . R16G16_SFloat , true ) ;
if ( PSSRPlugin . CreatePssr Context ( ref initParams , out IntPtr outputColorTexturePtr ) > = 0 & & outputColorTexturePtr ! = IntPtr . Zero )
if ( PSSRPlugin . CreateContext ( ref initParams , out IntPtr outputColorTexturePtr ) > = 0 & & outputColorTexturePtr ! = IntPtr . Zero )
{
// PSSR requires an output color texture in a very particular format (k11_11_10Float with kStandard256B tile mode and a specific alignment) that Unity cannot create directly.
// So instead we let the plugin create that texture and then import it into Unity as a generic 32bpp texture from a native pointer.
_outputColor = Texture2D . CreateExternalTexture ( config . UpscaleSize . x , config . UpscaleSize . y , TextureFormat . RGBA32 , false , true , outputColorTexturePtr ) ;
_dispatchParamsBuffer = Marshal . AllocHGlobal ( Marshal . SizeOf < PSSRPlugin . DispatchParams > ( ) ) ;
_dispatchParams . Initialize ( ) ;
_destroyParams . Initialize ( ) ;
_contextInitialized = true ;
}
}
@ -85,7 +88,11 @@ namespace UnityEngine.Rendering.PostProcessing
uint previousContext = _currentContext ;
_currentContext = ( _currentContext + 1 ) % PSSRPlugin . MaxNumContexts ;
PSSRPlugin . DestroyPssrContext ( previousContext ) ;
CommandBuffer cmd = new ( ) ;
_destroyParams . Value . contextIndex = previousContext ;
PSSRPlugin . IssuePluginEvent ( cmd , PSSRPlugin . Event . Destroy , _destroyParams ) ;
Graphics . ExecuteCommandBuffer ( cmd ) ;
cmd . Release ( ) ;
_contextInitialized = false ;
}
@ -100,11 +107,8 @@ namespace UnityEngine.Rendering.PostProcessing
DestroyRenderTextureArray ( _inputDepth ) ;
DestroyRenderTexture ( ref _inputColor ) ;
if ( _dispatchParamsBuffer ! = IntPtr . Zero )
{
Marshal . FreeHGlobal ( _dispatchParamsBuffer ) ;
_dispatchParamsBuffer = IntPtr . Zero ;
}
_destroyParams . Destroy ( ) ;
_dispatchParams . Destroy ( ) ;
}
public override void Render ( PostProcessRenderContext context , Upscaling config )
@ -145,33 +149,33 @@ namespace UnityEngine.Rendering.PostProcessing
if ( SystemInfo . usesReversedZBuffer ) flags | = PSSRPlugin . OptionFlags . ReverseDepth ;
if ( config . exposureSource = = Upscaling . ExposureSource . Auto ) flags | = PSSRPlugin . OptionFlags . AutoExposure ;
_dispatchParams . contextIndex = _currentContext ;
_dispatchParams . color = ToNativePtr ( _inputColor ) ;
_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
ref var dispatchParams = ref _dispatchParams . Value ;
dispatchParams . contextIndex = _currentContext ;
dispatchParams . color = ToNativePtr ( _inputColor ) ;
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
{
Upscaling . ExposureSource . Manual when config . exposure ! = null = > config . exposure ,
Upscaling . ExposureSource . Unity = > context . autoExposureTexture ,
_ = > null ,
} ) ;
_ dispatchParams. reactiveMask = ToNativePtr ( reactiveMask ) ;
_ dispatchParams. outputColor = ToNativePtr ( _outputColor ) ;
dispatchParams . reactiveMask = ToNativePtr ( reactiveMask ) ;
dispatchParams . outputColor = ToNativePtr ( _outputColor ) ;
var scaledRenderSize = config . GetScaledRenderSize ( context . camera ) ;
_ dispatchParams. renderWidth = ( uint ) scaledRenderSize . x ;
_ dispatchParams. renderHeight = ( uint ) scaledRenderSize . y ;
_ dispatchParams. jitter = config . JitterOffset ;
_ dispatchParams. motionVectorScale = new Vector2 ( - scaledRenderSize . x , - scaledRenderSize . y ) ;
_ dispatchParams. FromCamera ( context . camera ) ;
_ dispatchParams. preExposure = config . preExposure ;
_ dispatchParams. resetHistory = config . Reset ? 1 u : 0 u ;
_ dispatchParams. flags = flags ;
dispatchParams . renderWidth = ( uint ) scaledRenderSize . x ;
dispatchParams . renderHeight = ( uint ) scaledRenderSize . y ;
dispatchParams . jitter = config . JitterOffset ;
dispatchParams . motionVectorScale = new Vector2 ( - scaledRenderSize . x , - scaledRenderSize . y ) ;
dispatchParams . FromCamera ( context . camera ) ;
dispatchParams . preExposure = config . preExposure ;
dispatchParams . resetHistory = config . Reset ? 1 u : 0 u ;
dispatchParams . flags = flags ;
Marshal . StructureToPtr ( _dispatchParams , _dispatchParamsBuffer , false ) ;
cmd . IssuePluginEventAndData ( PSSRPlugin . GetRenderEventAndDataFunc ( ) , 1 , _dispatchParamsBuffer ) ;
PSSRPlugin . IssuePluginEvent ( cmd , PSSRPlugin . Event . Dispatch , _dispatchParams ) ;
if ( config . performSharpenPass )
{
@ -191,98 +195,6 @@ namespace UnityEngine.Rendering.PostProcessing
{
return texture ! = null ? texture . GetNativeTexturePtr ( ) : IntPtr . Zero ;
}
private static class PSSRPlugin
{
public static readonly uint MaxNumContexts = 4 ;
private const string LibraryName = "PSSRPlugin.prx" ;
[DllImport(LibraryName)]
public static extern IntPtr GetRenderEventAndDataFunc ( ) ;
[DllImport(LibraryName)]
public static extern int InitPssr ( ) ;
[DllImport(LibraryName)]
public static extern void ReleasePssr ( ) ;
[DllImport(LibraryName)]
public static extern int CreatePssrContext ( ref InitParams initParams , out IntPtr outputColorTexturePtr ) ;
[DllImport(LibraryName)]
public static extern void DestroyPssrContext ( uint contextIndex ) ;
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct InitParams
{
public uint contextIndex ;
public uint displayWidth ;
public uint displayHeight ;
public uint maxRenderWidth ;
public uint maxRenderHeight ;
public uint autoKeepCopies ;
}
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct DispatchParams
{
public uint contextIndex ;
public IntPtr color ;
public IntPtr depth ;
public IntPtr prevDepth ;
public IntPtr motionVectors ;
public IntPtr prevMotionVectors ;
public IntPtr exposure ;
public IntPtr reactiveMask ;
public IntPtr outputColor ;
public uint renderWidth ;
public uint renderHeight ;
public Vector2 jitter ;
public Vector2 motionVectorScale ;
public Matrix4x4 camProjectionNoJitter ;
public Vector3 camForward ;
public Vector3 camUp ;
public Vector3 camRight ;
public double camPositionX ;
public double camPositionY ;
public double camPositionZ ;
public float camNear ;
public float camFar ;
public float preExposure ;
public uint resetHistory ;
public OptionFlags flags ;
public void FromCamera ( Camera cam )
{
camProjectionNoJitter = cam . nonJitteredProjectionMatrix ;
camForward = cam . transform . forward ;
camUp = cam . transform . up ;
camRight = cam . transform . right ;
camPositionX = cam . transform . position . x ;
camPositionY = cam . transform . position . y ;
camPositionZ = cam . transform . position . z ;
camNear = cam . nearClipPlane ;
camFar = cam . farClipPlane ;
}
}
[Flags]
public enum OptionFlags : uint
{
None = 0 u ,
ViewSpaceDepth = 1 u < < 1 4 ,
ReverseDepth = 1 u < < 1 5 ,
AutoExposure = 1 u < < 1 6 ,
PassThrough = 1 u < < 3 1 ,
}
}
#else
public static bool IsSupported = > false ;