diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx index 558de84..3ae43a6 100644 Binary files a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx and b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx differ diff --git a/Tools/PSSRPlugin/pssrplugin.cpp b/Tools/PSSRPlugin/pssrplugin.cpp index a8e460d..aaf6e5a 100644 --- a/Tools/PSSRPlugin/pssrplugin.cpp +++ b/Tools/PSSRPlugin/pssrplugin.cpp @@ -175,15 +175,6 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr() createMfsrSharedResources(&s_mfsrSharedResources, &mfsrSharedResourcesInitParameters); } - // Set up command buffer for NGGC - if (s_RendererType == kUnityGfxRendererPS5NGGC && s_GraphicsAgcPS5 != nullptr) - { - const size_t agcContextSize = 128 * 1024; - s_AgcContext.m_dcb.init(s_GraphicsAgcPS5->AllocateGPUMemory(agcContextSize, Alignment::kCommandBuffer), agcContextSize); - s_AgcContext.m_bdr.init(&s_AgcContext.m_dcb, &s_AgcContext.m_dcb); - s_AgcContext.m_sb.init(256, &s_AgcContext.m_dcb, &s_AgcContext.m_dcb); - } - s_mfsrInitialized = true; UNITY_LOG(s_Log, "PSSR plugin initialized"); @@ -194,12 +185,6 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API ReleasePssr() { s_mfsrInitialized = false; - if (s_GraphicsAgcPS5 != nullptr && s_AgcContext.m_dcb.m_bottom != nullptr) - { - s_GraphicsAgcPS5->ReleaseGPUMemory(s_AgcContext.m_dcb.m_bottom); - s_AgcContext.m_dcb.clear(); - } - if (s_mfsrSharedResources != nullptr) { releaseMfsrSharedResources(s_mfsrSharedResources); @@ -336,6 +321,15 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreatePssrContext( *outputColorTexture = &s_outputColorTexture; } + // Set up command buffer for NGGC + if (s_RendererType == kUnityGfxRendererPS5NGGC && s_GraphicsAgcPS5 != nullptr) + { + const size_t agcContextSize = 128 * 1024; + s_AgcContext.m_dcb.init(s_GraphicsAgcPS5->AllocateGPUMemory(agcContextSize, Alignment::kCommandBuffer), agcContextSize); + s_AgcContext.m_bdr.init(&s_AgcContext.m_dcb, &s_AgcContext.m_dcb); + s_AgcContext.m_sb.init(256, &s_AgcContext.m_dcb, &s_AgcContext.m_dcb); + } + // Finally, create the actual MFSR context int res = createMfsrContext(&s_mfsrContext, &initParams); if (res != SCE_OK) @@ -352,6 +346,25 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreatePssrContext( extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyPssrContext() { + // TODO: this still crashes often due to race conditions between dispatch above and release here + // The main cause will be that the MFSR context is released before the already dispatched commands are executed on the GPU + // Just adding mutexes everywhere is not going to fix that + // A cheap but slightly dirty solution would be to run this code only at EndOfFrame from Unity + // + // Another issue is that we reuse the same static s_mfsrContext pointer when recreating the MFSR context, which can lead to situations where dispatch uses the wrong context object + // Multi-buffering those pointers will help with that + if (s_mfsrContext != nullptr) + { + releaseMfsrContext(s_mfsrContext); + s_mfsrContext = nullptr; + } + + if (s_GraphicsAgcPS5 != nullptr && s_AgcContext.m_dcb.m_bottom != nullptr) + { + s_GraphicsAgcPS5->ReleaseGPUMemory(s_AgcContext.m_dcb.m_bottom); + s_AgcContext.m_dcb.clear(); + } + if (s_outputColorTexture.getDataAddress()) { if (s_GraphicsPS5 != nullptr) @@ -366,19 +379,6 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyPssrContext() s_outputColorTexture.init(); } - // TODO: this still crashes often due to race conditions between dispatch above and release here - // The main cause will be that the MFSR context is released before the already dispatched commands are executed on the GPU - // Just adding mutexes everywhere is not going to fix that - // A cheap but slightly dirty solution would be to run this code only at EndOfFrame from Unity - // - // Another issue is that we reuse the same static s_mfsrContext pointer when recreating the MFSR context, which can lead to situations where dispatch uses the wrong context object - // Multi-buffering those pointers will help with that - if (s_mfsrContext != nullptr) - { - releaseMfsrContext(s_mfsrContext); - s_mfsrContext = nullptr; - } - if (s_mfsrMemoryBlocks != nullptr) { delete[] s_mfsrMemoryBlocks; @@ -403,7 +403,7 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) // User rendering code switch (eventID) { - case 0: // Initialize PSSR + case 0: // Create PSSR context { auto* params = (pssr_init_params_t*)data; CreatePssrContext(params, nullptr);