diff --git a/Tools/PSSRPlugin/pssrplugin.cpp b/Tools/PSSRPlugin/pssrplugin.cpp index 2384dda..7c5e2e1 100644 --- a/Tools/PSSRPlugin/pssrplugin.cpp +++ b/Tools/PSSRPlugin/pssrplugin.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -27,9 +26,6 @@ static bool s_mfsrInitialized = false; static MfsrPhysicalMemoryBlock* s_mfsrSharedResourcesMemoryBlocks = nullptr; static MfsrSharedResources s_mfsrSharedResources = nullptr; -// "Multiple contexts (up to 4) can be generated to perform multiple MFSR processes within an application." -static const int MaxNumContexts = 4; - struct PssrContext { Core::BasicContext agcContext; @@ -37,6 +33,9 @@ struct PssrContext MfsrContext mfsrContext = nullptr; Core::Texture outputColorTexture; }; + +// "Multiple contexts (up to 4) can be generated to perform multiple MFSR processes within an application." +static const int MaxNumContexts = 4; static PssrContext s_contexts[MaxNumContexts]; static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType); @@ -99,12 +98,10 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev } case kUnityGfxDeviceEventBeforeReset: { - //TODO: user Direct3D 9 code break; } case kUnityGfxDeviceEventAfterReset: { - //TODO: user Direct3D 9 code break; } }; @@ -190,12 +187,14 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API ReleasePssr() { s_mfsrInitialized = false; + // Destroy the MFSR shared resources if (s_mfsrSharedResources != nullptr) { releaseMfsrSharedResources(s_mfsrSharedResources); s_mfsrSharedResources = nullptr; } + // Free the memory used for MFSR shared resource data if (s_mfsrSharedResourcesMemoryBlocks != nullptr) { delete[] s_mfsrSharedResourcesMemoryBlocks; @@ -375,25 +374,21 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyPssrContext(ui PssrContext& context = s_contexts[contextIndex]; - // 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 + // Destroy the MFSR context if (context.mfsrContext != nullptr) { releaseMfsrContext(context.mfsrContext); context.mfsrContext = nullptr; } + // Destroy the NGGC command buffer if (s_GraphicsAgcPS5 != nullptr && context.agcContext.m_dcb.m_bottom != nullptr) { s_GraphicsAgcPS5->ReleaseGPUMemory(context.agcContext.m_dcb.m_bottom); context.agcContext.m_dcb.clear(); } + // Destroy the output color texture if (context.outputColorTexture.getDataAddress()) { if (s_GraphicsPS5 != nullptr) @@ -408,6 +403,7 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyPssrContext(ui context.outputColorTexture.init(); } + // Free the memory used for MFSR context data if (context.mfsrMemoryBlocks != nullptr) { delete[] context.mfsrMemoryBlocks;