Browse Source

Fixed a memory leak on repeated destroy/create, as it turns out MFSR doesn't take ownership of the memory you allocate for it. You have to keep track of this and release it yourself.

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

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

34
Tools/PSSRPlugin/pssrplugin.cpp

@ -21,13 +21,19 @@ static IUnityGraphicsAgcPS5* s_GraphicsAgcPS5 = nullptr; // New Agc-based gra
static UnityGfxRenderer s_RendererType = kUnityGfxRendererNull;
static bool s_mfsrInitialized = false;
static size_t s_mfsrSharedResourcesMemorySize = 0;
static off_t s_mfsrSharedResourcesMemoryAddress = 0;
static MfsrPhysicalMemoryBlock* s_mfsrSharedResourcesMemoryBlocks = nullptr;
static MfsrSharedResources s_mfsrSharedResources = nullptr;
struct PssrContext
{
Core::BasicContext agcContext;
size_t mfsrMemorySize = 0;
off_t mfsrMemoryPhysicalAddress = 0;
MfsrPhysicalMemoryBlock* mfsrMemoryBlocks = nullptr;
MfsrContext mfsrContext = nullptr;
Core::Texture outputColorTexture;
};
@ -158,14 +164,13 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API InitPssr()
size_t blockSize = mfsrSharedResourcesInitRequirement.m_physicalMemoryBlockSize;
size_t blockAlign = mfsrSharedResourcesInitRequirement.m_physicalMemoryBlockAlignment;
size_t m_mfsrSharedResourcesMemorySize = mfsrSharedResourcesInitRequirement.m_physicalMemoryBlockCount * blockSize;
off_t m_mfsrSharedResourcesMemoryAddress;
sceKernelAllocateMainDirectMemory(m_mfsrSharedResourcesMemorySize, blockAlign, SCE_KERNEL_MTYPE_C_SHARED, &m_mfsrSharedResourcesMemoryAddress);
s_mfsrSharedResourcesMemorySize = mfsrSharedResourcesInitRequirement.m_physicalMemoryBlockCount * blockSize;
sceKernelAllocateMainDirectMemory(s_mfsrSharedResourcesMemorySize, blockAlign, SCE_KERNEL_MTYPE_C_SHARED, &s_mfsrSharedResourcesMemoryAddress);
for (uint32_t i = 0; i < mfsrSharedResourcesInitRequirement.m_physicalMemoryBlockCount; i++)
{
s_mfsrSharedResourcesMemoryBlocks[i].m_size = blockSize;
s_mfsrSharedResourcesMemoryBlocks[i].m_offset = m_mfsrSharedResourcesMemoryAddress + blockSize * i;
s_mfsrSharedResourcesMemoryBlocks[i].m_offset = s_mfsrSharedResourcesMemoryAddress + blockSize * i;
}
mfsrSharedResourcesInitParameters.m_physicalMemoryBlocks = s_mfsrSharedResourcesMemoryBlocks;
@ -193,6 +198,13 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API ReleasePssr()
}
// Free the memory used for MFSR shared resource data
if (s_mfsrSharedResourcesMemoryAddress != 0)
{
sceKernelReleaseDirectMemory(s_mfsrSharedResourcesMemoryAddress, s_mfsrSharedResourcesMemorySize);
s_mfsrSharedResourcesMemoryAddress = 0;
s_mfsrSharedResourcesMemorySize = 0;
}
if (s_mfsrSharedResourcesMemoryBlocks != nullptr)
{
delete[] s_mfsrSharedResourcesMemoryBlocks;
@ -289,14 +301,13 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreatePssrContext(
size_t size = mfsrInitRequirement.m_physicalMemoryBlockSize;
size_t align = mfsrInitRequirement.m_physicalMemoryBlockAlignment;
size_t m_mfsrMemorySize = mfsrInitRequirement.m_physicalMemoryBlockCount * size;
off_t m_mfsrMemoryPhysicalAddress;
sceKernelAllocateMainDirectMemory(m_mfsrMemorySize, align, SCE_KERNEL_MTYPE_C_SHARED, &m_mfsrMemoryPhysicalAddress);
context.mfsrMemorySize = mfsrInitRequirement.m_physicalMemoryBlockCount * size;
sceKernelAllocateMainDirectMemory(context.mfsrMemorySize, align, SCE_KERNEL_MTYPE_C_SHARED, &context.mfsrMemoryPhysicalAddress);
for (uint32_t i = 0; i < mfsrInitRequirement.m_physicalMemoryBlockCount; i++)
{
context.mfsrMemoryBlocks[i].m_size = size;
context.mfsrMemoryBlocks[i].m_offset = m_mfsrMemoryPhysicalAddress + mfsrInitRequirement.m_physicalMemoryBlockSize * i;
context.mfsrMemoryBlocks[i].m_offset = context.mfsrMemoryPhysicalAddress + mfsrInitRequirement.m_physicalMemoryBlockSize * i;
}
initParams.m_sharedResources = s_mfsrSharedResources;
@ -402,6 +413,13 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyPssrContext(ui
}
// Free the memory used for MFSR context data
if (context.mfsrMemoryPhysicalAddress != 0)
{
sceKernelReleaseDirectMemory(context.mfsrMemoryPhysicalAddress, context.mfsrMemorySize);
context.mfsrMemoryPhysicalAddress = 0;
context.mfsrMemorySize = 0;
}
if (context.mfsrMemoryBlocks != nullptr)
{
delete[] context.mfsrMemoryBlocks;

Loading…
Cancel
Save