From 4406d9d697b0adf07b699f8211beef1606766474 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 9 Nov 2024 15:50:58 +0100 Subject: [PATCH] Destroy context by directly calling into the library instead of using a plugin event. Seems perfectly stable in testing and fixes an issue where occasionally the wrong context index would come through in native code. --- .../Runtime/Effects/Upscaling/PSSRUpscaler.cs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 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 1428a40..2df8cb1 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 @@ -71,16 +71,11 @@ namespace UnityEngine.Rendering.PostProcessing if (_contextInitialized) { - _dispatchParams.contextIndex = _currentContext; - Marshal.StructureToPtr(_dispatchParams, _dispatchParamsBuffer, false); - + // Rotate between contexts to reduce the risk of race conditions between old and new contexts + uint previousContext = _currentContext; _currentContext = (_currentContext + 1) % PSSRPlugin.MaxNumContexts; - // Destroying the context from the render thread reduces the risk of race conditions causing crashes - var cmd = new CommandBuffer(); - cmd.IssuePluginEventAndData(PSSRPlugin.GetRenderEventAndDataFunc(), 2, _dispatchParamsBuffer); - Graphics.ExecuteCommandBuffer(cmd); - cmd.Release(); + PSSRPlugin.DestroyPssrContext(previousContext); _contextInitialized = false; } @@ -206,7 +201,7 @@ namespace UnityEngine.Rendering.PostProcessing public static extern int CreatePssrContext(ref InitParams initParams, out IntPtr outputColorTexturePtr); [DllImport(LibraryName)] - public static extern void DestroyPssrContext(); + public static extern void DestroyPssrContext(uint contextIndex); [Serializable, StructLayout(LayoutKind.Sequential)] public struct InitParams