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 06c66c3..9c0f5af 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 @@ -21,10 +21,12 @@ namespace UnityEngine.Rendering.PostProcessing private bool _pluginInitialized; private bool _contextInitialized; private uint _frameCount; + + // TODO: InitPssr from class constructor public override void CreateContext(PostProcessRenderContext context, Upscaling config) { - if (!PSSRPlugin.InitPssr()) + if (PSSRPlugin.InitPssr() < 0) { Debug.LogError("Failed to initialize PSSR plugin!"); _pluginInitialized = false; @@ -70,6 +72,12 @@ namespace UnityEngine.Rendering.PostProcessing _contextInitialized = false; } + if (_outputIntermediate != null) + { + Object.Destroy(_outputIntermediate); + _outputIntermediate = null; + } + DestroyRenderTexture(ref _outputColor); DestroyRenderTextureArray(_inputMotionVectors); DestroyRenderTextureArray(_inputDepth); @@ -175,7 +183,7 @@ namespace UnityEngine.Rendering.PostProcessing public static extern IntPtr GetRenderEventAndDataFunc(); [DllImport(LibraryName)] - public static extern bool InitPssr(); + public static extern int InitPssr(); [DllImport(LibraryName)] public static extern void ReleasePssr(); @@ -183,6 +191,9 @@ namespace UnityEngine.Rendering.PostProcessing [DllImport(LibraryName)] public static extern int CreatePssrContext(ref InitParams initParams, out IntPtr outputColorTexturePtr); + [DllImport(LibraryName)] + public static extern void DestroyPssrContext(); + [Serializable, StructLayout(LayoutKind.Sequential)] public struct InitParams { diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins.meta b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins.meta new file mode 100644 index 0000000..ee29cd0 --- /dev/null +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5fe0a1d4497ee1c43bca193c0efc0b4f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PS5.meta b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5.meta similarity index 100% rename from Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PS5.meta rename to Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5.meta diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PS5/PSSRPlugin.prx b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx similarity index 53% rename from Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PS5/PSSRPlugin.prx rename to Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx index 0e86960..558de84 100644 Binary files a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PS5/PSSRPlugin.prx and b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx differ diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PS5/PSSRPlugin.prx.meta b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx.meta similarity index 100% rename from Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PS5/PSSRPlugin.prx.meta rename to Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx.meta diff --git a/Tools/PSSRPlugin/pssrplugin.cpp b/Tools/PSSRPlugin/pssrplugin.cpp index a9bd3dc..a8e460d 100644 --- a/Tools/PSSRPlugin/pssrplugin.cpp +++ b/Tools/PSSRPlugin/pssrplugin.cpp @@ -83,18 +83,6 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev return; } - // Yeah just don't do the stuff below. It makes Unity act weirdly. - //UnityD3D12PluginEventConfig config; - //config.graphicsQueueAccess = kUnityD3D12GraphicsQueueAccess_DontCare; - //config.flags = kUnityD3D12EventConfigFlag_ModifiesCommandBuffersState; - //config.ensureActiveRenderTextureIsBound = true; // TODO: not entirely sure if this is necessary - //s_GraphicsD3D12->ConfigureEvent(1, &config); - - //config.graphicsQueueAccess = kUnityD3D12GraphicsQueueAccess_DontCare; - //config.flags = 0; - //config.ensureActiveRenderTextureIsBound = false; - //s_GraphicsD3D12->ConfigureEvent(2, &config); - break; } case kUnityGfxDeviceEventShutdown: @@ -117,13 +105,13 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev }; } -extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr() +extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr() { // Check if we're running on PS5 Pro if (!sceKernelIsTrinityMode()) { UNITY_LOG_ERROR(s_Log, "Kernel is not running in Trinity mode, PSML is not supported!"); - return false; + return -1; } // Load PSML module @@ -135,7 +123,7 @@ extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr() std::stringstream msg; msg << "Failed to load PSML sysmodule, error code = " << res; UNITY_LOG_ERROR(s_Log, msg.str().c_str()); - return false; + return res; } } @@ -146,10 +134,13 @@ extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr() std::stringstream msg; msg << "Failed to initialize MFSR, error code = " << res; UNITY_LOG_ERROR(s_Log, msg.str().c_str()); - return false; + return res; } - // TODO: do some idempotence here, make sure we don't create shared resources twice + if (s_mfsrInitialized) + { + return SCE_OK; + } // Set up MFSR shared resources { @@ -184,9 +175,7 @@ extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr() createMfsrSharedResources(&s_mfsrSharedResources, &mfsrSharedResourcesInitParameters); } - s_mfsrInitialized = true; - - // Set up command buffer + // Set up command buffer for NGGC if (s_RendererType == kUnityGfxRendererPS5NGGC && s_GraphicsAgcPS5 != nullptr) { const size_t agcContextSize = 128 * 1024; @@ -195,8 +184,10 @@ extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr() s_AgcContext.m_sb.init(256, &s_AgcContext.m_dcb, &s_AgcContext.m_dcb); } + s_mfsrInitialized = true; + UNITY_LOG(s_Log, "PSSR plugin initialized"); - return true; + return SCE_OK; } extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API ReleasePssr() @@ -313,6 +304,7 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreatePssrContext( } // Set up output color texture with the correct specifications (k11_11_10Float with tile mode kStandard256B) + if (outputColorTexture != nullptr) { Core::TextureSpec texSpec; texSpec.init(); @@ -341,10 +333,7 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreatePssrContext( return res; } - if (outputColorTexture != nullptr) - { - *outputColorTexture = &s_outputColorTexture; - } + *outputColorTexture = &s_outputColorTexture; } // Finally, create the actual MFSR context @@ -361,6 +350,44 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreatePssrContext( return SCE_OK; } +extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyPssrContext() +{ + if (s_outputColorTexture.getDataAddress()) + { + if (s_GraphicsPS5 != nullptr) + { + s_GraphicsPS5->ReleaseGPUMemory(s_outputColorTexture.getDataAddress()); + } + else if (s_GraphicsAgcPS5 != nullptr) + { + s_GraphicsAgcPS5->ReleaseGPUMemory(s_outputColorTexture.getDataAddress()); + } + + 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; + s_mfsrMemoryBlocks = nullptr; + } + + UNITY_LOG(s_Log, "Destroyed PSSR context"); +} + // Plugin function to handle a specific rendering event static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) { @@ -387,10 +414,9 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) if (s_mfsrContext == nullptr) break; - // TODO: how do get command buffer from PS5 graphics interface? :thinking: - // I guess we're hoping that s_GraphicsAgcPS5->SubmitGraphics is smart enough to add the submit pointer to the currently recording command list (which seems to be the case, phew!) auto* params = (pssr_dispatch_params_t*)data; + // How we obtain a command buffer to dispatch to depends on which graphics API we're using in Unity sce::Agc::DrawCommandBuffer* cmd = nullptr; if (s_RendererType == kUnityGfxRendererPS5) { @@ -443,28 +469,7 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) } case 2: // Destroy PSSR context { - // TODO: release color input texture - - // 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; - s_mfsrMemoryBlocks = nullptr; - } - - UNITY_LOG(s_Log, "Destroyed PSSR context"); + DestroyPssrContext(); break; } }