From f3fcfec801e0528f7e2c000263489a36215d6bf7 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Wed, 2 Apr 2025 19:53:49 +0200 Subject: [PATCH] Backported safety fixes from Isonzo --- .../Runtime/Effects/Upscaling/PSSRUpscaler.cs | 3 +-- Packages/com.ww1gameseries.pssr/Runtime/PSSRPlugin.cs | 5 +++++ .../com.ww1gameseries.pssr/Source~/pssrplugin.cpp | 11 +++++++++-- 3 files changed, 15 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 168da4d..451bc01 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 @@ -89,8 +89,7 @@ namespace UnityEngine.Rendering.PostProcessing _currentContext = (_currentContext + 1) % PSSRPlugin.MaxNumContexts; CommandBuffer cmd = new(); - _destroyParams.Value.contextIndex = previousContext; - PSSRPlugin.IssuePluginEvent(cmd, PSSRPlugin.Event.Destroy, _destroyParams); + PSSRPlugin.IssuePluginEvent(cmd, PSSRPlugin.Event.Destroy, new IntPtr(previousContext)); Graphics.ExecuteCommandBuffer(cmd); cmd.Release(); diff --git a/Packages/com.ww1gameseries.pssr/Runtime/PSSRPlugin.cs b/Packages/com.ww1gameseries.pssr/Runtime/PSSRPlugin.cs index bd09e8f..4873c73 100644 --- a/Packages/com.ww1gameseries.pssr/Runtime/PSSRPlugin.cs +++ b/Packages/com.ww1gameseries.pssr/Runtime/PSSRPlugin.cs @@ -21,6 +21,11 @@ namespace WW1.PlayStation { cmd.IssuePluginEventAndData(GetRenderEventAndDataFunc(), (int)pluginEvent, data.GetPointer()); } + + public static void IssuePluginEvent(CommandBuffer cmd, Event pluginEvent, IntPtr data) + { + cmd.IssuePluginEventAndData(GetRenderEventAndDataFunc(), (int)pluginEvent, data); + } [DllImport(LibraryInfo.LibraryName)] public static extern IntPtr GetRenderEventAndDataFunc(); diff --git a/Packages/com.ww1gameseries.pssr/Source~/pssrplugin.cpp b/Packages/com.ww1gameseries.pssr/Source~/pssrplugin.cpp index 037a124..630c5c5 100644 --- a/Packages/com.ww1gameseries.pssr/Source~/pssrplugin.cpp +++ b/Packages/com.ww1gameseries.pssr/Source~/pssrplugin.cpp @@ -288,6 +288,8 @@ static bool IsInitialized() return true; } +extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API PSSR_DestroyContext(uint32_t contextIndex); + extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API PSSR_CreateContext(const pssr_init_params_t* params, Core::Texture** outputColorTexture) { if (!IsInitialized()) @@ -305,6 +307,11 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API PSSR_CreateContext } PssrContext& context = s_contexts[params->contextIndex]; + if (context.mfsrContext != nullptr) + { + // If for some reason this context wasn't properly destroyed previously, make sure we do that now + PSSR_DestroyContext(params->contextIndex); + } MfsrContextInitParameters initParams = {}; initParams.init(); @@ -592,8 +599,8 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) } case 2: // Destroy PSSR context { - auto* params = (pssr_destroy_params_t*)data; - PSSR_DestroyContext(params->contextIndex); + uint32_t contextIndex = (uint32_t)(intptr_t)data; + PSSR_DestroyContext(contextIndex); break; } case 3: // Request PSSR capture