Browse Source

Moved command buffer creation/destruction to context create/destroy, so that the command buffer is directly associated with the MFSR context.

pssr
Nico de Poel 1 year ago
parent
commit
470c178e4f
  1. BIN
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx
  2. 58
      Tools/PSSRPlugin/pssrplugin.cpp

BIN
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/Plugins/PS5/PSSRPlugin.prx

58
Tools/PSSRPlugin/pssrplugin.cpp

@ -175,15 +175,6 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr()
createMfsrSharedResources(&s_mfsrSharedResources, &mfsrSharedResourcesInitParameters); 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; s_mfsrInitialized = true;
UNITY_LOG(s_Log, "PSSR plugin initialized"); UNITY_LOG(s_Log, "PSSR plugin initialized");
@ -194,12 +185,6 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API ReleasePssr()
{ {
s_mfsrInitialized = false; 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) if (s_mfsrSharedResources != nullptr)
{ {
releaseMfsrSharedResources(s_mfsrSharedResources); releaseMfsrSharedResources(s_mfsrSharedResources);
@ -336,6 +321,15 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreatePssrContext(
*outputColorTexture = &s_outputColorTexture; *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 // Finally, create the actual MFSR context
int res = createMfsrContext(&s_mfsrContext, &initParams); int res = createMfsrContext(&s_mfsrContext, &initParams);
if (res != SCE_OK) 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() 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_outputColorTexture.getDataAddress())
{ {
if (s_GraphicsPS5 != nullptr) if (s_GraphicsPS5 != nullptr)
@ -366,19 +379,6 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyPssrContext()
s_outputColorTexture.init(); 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) if (s_mfsrMemoryBlocks != nullptr)
{ {
delete[] s_mfsrMemoryBlocks; delete[] s_mfsrMemoryBlocks;
@ -403,7 +403,7 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
// User rendering code // User rendering code
switch (eventID) switch (eventID)
{ {
case 0: // Initialize PSSR
case 0: // Create PSSR context
{ {
auto* params = (pssr_init_params_t*)data; auto* params = (pssr_init_params_t*)data;
CreatePssrContext(params, nullptr); CreatePssrContext(params, nullptr);

Loading…
Cancel
Save