From efd7240f948e89f4b024abf77e7a2655633d1bb9 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Fri, 8 Nov 2024 13:00:04 +0100 Subject: [PATCH] Rotate between PSSR contexts to further reduce risk of race conditions between destroy & create --- .../Runtime/Effects/Upscaling/PSSRUpscaler.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 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 f163ba9..1b3d0d2 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 @@ -10,6 +10,7 @@ namespace UnityEngine.Rendering.PostProcessing public static bool IsSupported => UnityEngine.PS5.Utility.isTrinityMode && PluginInitialized; private static readonly bool PluginInitialized; + private static uint _currentContext; private PSSRPlugin.DispatchParams _dispatchParams; private IntPtr _dispatchParamsBuffer; @@ -32,7 +33,8 @@ namespace UnityEngine.Rendering.PostProcessing } PluginInitialized = true; - } + _currentContext = 0; + } public override void CreateContext(PostProcessRenderContext context, Upscaling config) { @@ -43,7 +45,7 @@ namespace UnityEngine.Rendering.PostProcessing } PSSRPlugin.InitParams initParams; - initParams.contextIndex = 0; + initParams.contextIndex = _currentContext; initParams.displayWidth = (uint)config.UpscaleSize.x; initParams.displayHeight = (uint)config.UpscaleSize.y; initParams.maxRenderWidth = (uint)config.MaxRenderSize.x; @@ -71,8 +73,10 @@ namespace UnityEngine.Rendering.PostProcessing if (_contextInitialized) { - _dispatchParams.contextIndex = 0; + _dispatchParams.contextIndex = _currentContext; Marshal.StructureToPtr(_dispatchParams, _dispatchParamsBuffer, false); + + _currentContext = (_currentContext + 1) % PSSRPlugin.MaxNumContexts; // Destroying the context from the render thread reduces the risk of race conditions causing crashes var cmd = new CommandBuffer(); @@ -139,7 +143,7 @@ namespace UnityEngine.Rendering.PostProcessing if (SystemInfo.usesReversedZBuffer) flags |= PSSRPlugin.OptionFlags.ReverseDepth; if (config.exposureSource == Upscaling.ExposureSource.Auto) flags |= PSSRPlugin.OptionFlags.AutoExposure; - _dispatchParams.contextIndex = 0; + _dispatchParams.contextIndex = _currentContext; _dispatchParams.color = ToNativePtr(_inputColor); _dispatchParams.depth = ToNativePtr(_inputDepth[frameIndex]); _dispatchParams.prevDepth = ToNativePtr(_inputDepth[frameIndex ^ 1]); @@ -191,6 +195,8 @@ namespace UnityEngine.Rendering.PostProcessing private static class PSSRPlugin { + public static readonly uint MaxNumContexts = 4; + private const string LibraryName = "PSSRPlugin.prx"; [DllImport(LibraryName)]