From 9162faea77a883f594385a37840d78b175da425c Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 18 Nov 2024 12:20:02 +0100 Subject: [PATCH] Updated PSSR upscaler plugin with modernizations and convenience stuff made for Isonzo --- .../Runtime/Effects/Upscaling/PSSRUpscaler.cs | 158 ++++-------------- .../Unity.Postprocessing.Runtime.asmdef | 3 +- 2 files changed, 37 insertions(+), 124 deletions(-) diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PSSRUpscaler.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PSSRUpscaler.cs index 1508794..168da4d 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PSSRUpscaler.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PSSRUpscaler.cs @@ -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 _dispatchParams = new(); + private readonly PSSRPlugin.NativeData _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.CreatePssrContext(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()); + _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 ? 1u : 0u; - _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 ? 1u : 0u; + 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 = 0u, - ViewSpaceDepth = 1u << 14, - ReverseDepth = 1u << 15, - AutoExposure = 1u << 16, - PassThrough = 1u << 31, - } - } #else public static bool IsSupported => false; diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Unity.Postprocessing.Runtime.asmdef b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Unity.Postprocessing.Runtime.asmdef index 8220b4a..64e2885 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Unity.Postprocessing.Runtime.asmdef +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Unity.Postprocessing.Runtime.asmdef @@ -2,7 +2,8 @@ "name": "Unity.Postprocessing.Runtime", "rootNamespace": "", "references": [ - "FidelityFX.FSR" + "FidelityFX.FSR", + "PSSRPlugin.Runtime" ], "includePlatforms": [], "excludePlatforms": [],