diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp index 4f27ccf5..2d068884 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp @@ -4,6 +4,7 @@ #include "Cemu/Logging/CemuLogging.h" #include "Common/precompiled.h" +#include "HW/MMU/MMU.h" MetalMemoryManager::~MetalMemoryManager() { @@ -30,15 +31,23 @@ void MetalMemoryManager::InitBufferCache(size_t size) m_bufferCacheType = g_current_game_profile->GetBufferCacheType(); // First, try to import the host memory as a buffer - if (m_bufferCacheType == BufferCacheType::Host && m_mtlr->IsAppleGPU()) + if (m_bufferCacheType == BufferCacheType::Host) { - m_importedMemBaseAddress = 0x10000000; - m_hostAllocationSize = 0x40000000ull; // TODO: get size of allocation - m_bufferCache = m_mtlr->GetDevice()->newBuffer(memory_getPointerFromVirtualOffset(m_importedMemBaseAddress), m_hostAllocationSize, MTL::ResourceStorageModeShared, nullptr); - if (!m_bufferCache) + if (m_mtlr->HasUnifiedMemory()) { - cemuLog_logDebug(LogType::Force, "Failed to import host memory as a buffer"); - m_bufferCacheType = BufferCacheType::DevicePrivate; + m_importedMemBaseAddress = mmuRange_MEM2.getBase(); + m_hostAllocationSize = mmuRange_MEM2.getSize(); + m_bufferCache = m_mtlr->GetDevice()->newBuffer(memory_getPointerFromVirtualOffset(m_importedMemBaseAddress), m_hostAllocationSize, MTL::ResourceStorageModeShared, nullptr); + if (!m_bufferCache) + { + cemuLog_logDebug(LogType::Force, "Failed to import host memory as a buffer, using device shared mode instead"); + m_bufferCacheType = BufferCacheType::DeviceShared; + } + } + else + { + cemuLog_logDebug(LogType::Force, "Host buffer cache mode is only available on unified memory systems, using device shared mode instead"); + m_bufferCacheType = BufferCacheType::DeviceShared; } } diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h index 8f3c4d4c..a35df24c 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h @@ -43,6 +43,11 @@ public: return (m_bufferCacheType == BufferCacheType::Host); } + bool NeedsReducedLatency() const + { + return (m_bufferCacheType == BufferCacheType::DeviceShared || m_bufferCacheType == BufferCacheType::Host); + } + MPTR GetImportedMemBaseAddress() const { return m_importedMemBaseAddress; diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp index b19ef07a..20f8f678 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp @@ -88,8 +88,8 @@ MetalRenderer::MetalRenderer() m_depthStencilCache = new MetalDepthStencilCache(this); m_samplerCache = new MetalSamplerCache(this); - // Lower the commit treshold when host memory is used for cache to reduce latency - if (m_memoryManager->UseHostMemoryForCache()) + // Lower the commit treshold when buffer cache needs reduced latency + if (m_memoryManager->NeedsReducedLatency()) m_defaultCommitTreshlod = 64; else m_defaultCommitTreshlod = 196;