mirror of
https://github.com/cemu-project/Cemu.git
synced 2025-01-08 08:00:44 +01:00
refactor the way pipelines are stored
This commit is contained in:
parent
665eb23e4a
commit
bca32c43d0
@ -15,6 +15,7 @@
|
|||||||
#include "HW/Latte/ISA/LatteReg.h"
|
#include "HW/Latte/ISA/LatteReg.h"
|
||||||
#include "HW/Latte/Renderer/Metal/LatteToMtl.h"
|
#include "HW/Latte/Renderer/Metal/LatteToMtl.h"
|
||||||
#include "HW/Latte/Renderer/Metal/MetalAttachmentsInfo.h"
|
#include "HW/Latte/Renderer/Metal/MetalAttachmentsInfo.h"
|
||||||
|
#include "HW/Latte/Renderer/Metal/MetalPipelineCompiler.h"
|
||||||
#include "Metal/MTLRenderPipeline.hpp"
|
#include "Metal/MTLRenderPipeline.hpp"
|
||||||
#include "util/helpers/helpers.h"
|
#include "util/helpers/helpers.h"
|
||||||
#include "config/ActiveSettings.h"
|
#include "config/ActiveSettings.h"
|
||||||
@ -34,32 +35,32 @@ MetalPipelineCache::MetalPipelineCache(class MetalRenderer* metalRenderer) : m_m
|
|||||||
|
|
||||||
MetalPipelineCache::~MetalPipelineCache()
|
MetalPipelineCache::~MetalPipelineCache()
|
||||||
{
|
{
|
||||||
for (auto& [key, value] : m_pipelineCache)
|
for (auto& [key, pipelineObj] : m_pipelineCache)
|
||||||
value->release();
|
{
|
||||||
|
pipelineObj->m_pipeline->release();
|
||||||
|
delete pipelineObj;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MTL::RenderPipelineState* MetalPipelineCache::GetRenderPipelineState(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const LatteDecompilerShader* geometryShader, const LatteDecompilerShader* pixelShader, const MetalAttachmentsInfo& lastUsedAttachmentsInfo, const MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr)
|
PipelineObject* MetalPipelineCache::GetRenderPipelineState(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const LatteDecompilerShader* geometryShader, const LatteDecompilerShader* pixelShader, const MetalAttachmentsInfo& lastUsedAttachmentsInfo, const MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr)
|
||||||
{
|
{
|
||||||
uint64 hash = CalculatePipelineHash(fetchShader, vertexShader, geometryShader, pixelShader, lastUsedAttachmentsInfo, activeAttachmentsInfo, lcr);
|
uint64 hash = CalculatePipelineHash(fetchShader, vertexShader, geometryShader, pixelShader, lastUsedAttachmentsInfo, activeAttachmentsInfo, lcr);
|
||||||
auto it = m_pipelineCache.find(hash);
|
PipelineObject*& pipelineObj = m_pipelineCache[hash];
|
||||||
if (it != m_pipelineCache.end())
|
if (pipelineObj)
|
||||||
return it->second;
|
return pipelineObj;
|
||||||
|
|
||||||
MetalPipelineCompiler compiler(m_mtlr);
|
pipelineObj = new PipelineObject();
|
||||||
|
|
||||||
|
MetalPipelineCompiler compiler(m_mtlr, *pipelineObj);
|
||||||
bool fbosMatch;
|
bool fbosMatch;
|
||||||
compiler.InitFromState(fetchShader, vertexShader, geometryShader, pixelShader, lastUsedAttachmentsInfo, activeAttachmentsInfo, lcr, fbosMatch);
|
compiler.InitFromState(fetchShader, vertexShader, geometryShader, pixelShader, lastUsedAttachmentsInfo, activeAttachmentsInfo, lcr, fbosMatch);
|
||||||
bool attemptedCompilation = false;
|
compiler.Compile(false, true, true);
|
||||||
MTL::RenderPipelineState* pipeline = compiler.Compile(false, true, true, attemptedCompilation);
|
|
||||||
|
|
||||||
// If FBOs don't match, it wouldn't be possible to reconstruct the pipeline from the cache
|
// If FBOs don't match, it wouldn't be possible to reconstruct the pipeline from the cache
|
||||||
if (pipeline && fbosMatch)
|
if (fbosMatch)
|
||||||
AddCurrentStateToCache(hash);
|
AddCurrentStateToCache(hash);
|
||||||
|
|
||||||
// Place the pipeline to the cache if the compilation was at least attempted
|
return pipelineObj;
|
||||||
if (attemptedCompilation)
|
|
||||||
m_pipelineCache.insert({hash, pipeline});
|
|
||||||
|
|
||||||
return pipeline;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64 MetalPipelineCache::CalculatePipelineHash(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const LatteDecompilerShader* geometryShader, const LatteDecompilerShader* pixelShader, const MetalAttachmentsInfo& lastUsedAttachmentsInfo, const MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr)
|
uint64 MetalPipelineCache::CalculatePipelineHash(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const LatteDecompilerShader* geometryShader, const LatteDecompilerShader* pixelShader, const MetalAttachmentsInfo& lastUsedAttachmentsInfo, const MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr)
|
||||||
@ -358,32 +359,24 @@ void MetalPipelineCache::LoadPipelineFromCache(std::span<uint8> fileData)
|
|||||||
|
|
||||||
MetalAttachmentsInfo attachmentsInfo(*lcr, pixelShader);
|
MetalAttachmentsInfo attachmentsInfo(*lcr, pixelShader);
|
||||||
|
|
||||||
MTL::RenderPipelineState* pipeline = nullptr;
|
PipelineObject* pipelineObject = new PipelineObject();
|
||||||
|
|
||||||
// compile
|
// compile
|
||||||
{
|
{
|
||||||
MetalPipelineCompiler pp(m_mtlr);
|
MetalPipelineCompiler pp(m_mtlr, *pipelineObject);
|
||||||
bool fbosMatch;
|
bool fbosMatch;
|
||||||
pp.InitFromState(vertexShader->compatibleFetchShader, vertexShader, geometryShader, pixelShader, attachmentsInfo, attachmentsInfo, *lcr, fbosMatch);
|
pp.InitFromState(vertexShader->compatibleFetchShader, vertexShader, geometryShader, pixelShader, attachmentsInfo, attachmentsInfo, *lcr, fbosMatch);
|
||||||
cemu_assert_debug(fbosMatch);
|
cemu_assert_debug(fbosMatch);
|
||||||
//{
|
pp.Compile(true, true, false);
|
||||||
// s_spinlockSharedInternal.lock();
|
|
||||||
// delete lcr;
|
|
||||||
// delete cachedPipeline;
|
|
||||||
// s_spinlockSharedInternal.unlock();
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
bool attemptedCompilation = false;
|
|
||||||
pipeline = pp.Compile(true, true, false, attemptedCompilation);
|
|
||||||
cemu_assert_debug(attemptedCompilation);
|
|
||||||
// destroy pp early
|
// destroy pp early
|
||||||
}
|
}
|
||||||
|
|
||||||
// on success, calculate pipeline hash and flag as present in cache
|
// on success, cache the pipeline
|
||||||
if (pipeline)
|
if (pipelineObject->m_pipeline)
|
||||||
{
|
{
|
||||||
uint64 pipelineStateHash = CalculatePipelineHash(vertexShader->compatibleFetchShader, vertexShader, geometryShader, pixelShader, attachmentsInfo, attachmentsInfo, *lcr);
|
uint64 pipelineStateHash = CalculatePipelineHash(vertexShader->compatibleFetchShader, vertexShader, geometryShader, pixelShader, attachmentsInfo, attachmentsInfo, *lcr);
|
||||||
m_pipelineCacheLock.lock();
|
m_pipelineCacheLock.lock();
|
||||||
m_pipelineCache[pipelineStateHash] = pipeline;
|
m_pipelineCache[pipelineStateHash] = pipelineObject;
|
||||||
m_pipelineCacheLock.unlock();
|
m_pipelineCacheLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ public:
|
|||||||
MetalPipelineCache(class MetalRenderer* metalRenderer);
|
MetalPipelineCache(class MetalRenderer* metalRenderer);
|
||||||
~MetalPipelineCache();
|
~MetalPipelineCache();
|
||||||
|
|
||||||
MTL::RenderPipelineState* GetRenderPipelineState(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const LatteDecompilerShader* geometryShader, const LatteDecompilerShader* pixelShader, const class MetalAttachmentsInfo& lastUsedAttachmentsInfo, const class MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr);
|
PipelineObject* GetRenderPipelineState(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const LatteDecompilerShader* geometryShader, const LatteDecompilerShader* pixelShader, const class MetalAttachmentsInfo& lastUsedAttachmentsInfo, const class MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr);
|
||||||
|
|
||||||
// Cache loading
|
// Cache loading
|
||||||
uint32 BeginLoading(uint64 cacheTitleId); // returns count of pipelines stored in cache
|
uint32 BeginLoading(uint64 cacheTitleId); // returns count of pipelines stored in cache
|
||||||
@ -27,7 +27,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
class MetalRenderer* m_mtlr;
|
class MetalRenderer* m_mtlr;
|
||||||
|
|
||||||
std::map<uint64, MTL::RenderPipelineState*> m_pipelineCache;
|
std::map<uint64, PipelineObject*> m_pipelineCache;
|
||||||
FSpinlock m_pipelineCacheLock;
|
FSpinlock m_pipelineCacheLock;
|
||||||
|
|
||||||
std::thread* m_pipelineCacheStoreThread;
|
std::thread* m_pipelineCacheStoreThread;
|
||||||
|
@ -327,7 +327,7 @@ void MetalPipelineCompiler::InitFromState(const LatteFetchShader* fetchShader, c
|
|||||||
InitFromStateRender(fetchShader, vertexShader, lastUsedAttachmentsInfo, activeAttachmentsInfo, lcr, fbosMatch);
|
InitFromStateRender(fetchShader, vertexShader, lastUsedAttachmentsInfo, activeAttachmentsInfo, lcr, fbosMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
MTL::RenderPipelineState* MetalPipelineCompiler::Compile(bool forceCompile, bool isRenderThread, bool showInOverlay, bool& attemptedCompilation)
|
bool MetalPipelineCompiler::Compile(bool forceCompile, bool isRenderThread, bool showInOverlay)
|
||||||
{
|
{
|
||||||
if (forceCompile)
|
if (forceCompile)
|
||||||
{
|
{
|
||||||
@ -343,11 +343,11 @@ MTL::RenderPipelineState* MetalPipelineCompiler::Compile(bool forceCompile, bool
|
|||||||
{
|
{
|
||||||
// fail early if some shader stages are not compiled
|
// fail early if some shader stages are not compiled
|
||||||
if (m_vertexShaderMtl && !m_vertexShaderMtl->IsCompiled())
|
if (m_vertexShaderMtl && !m_vertexShaderMtl->IsCompiled())
|
||||||
return nullptr;
|
return false;
|
||||||
if (m_geometryShaderMtl && !m_geometryShaderMtl->IsCompiled())
|
if (m_geometryShaderMtl && !m_geometryShaderMtl->IsCompiled())
|
||||||
return nullptr;
|
return false;
|
||||||
if (m_pixelShaderMtl && !m_pixelShaderMtl->IsCompiled())
|
if (m_pixelShaderMtl && !m_pixelShaderMtl->IsCompiled())
|
||||||
return nullptr;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile
|
// Compile
|
||||||
@ -403,10 +403,9 @@ MTL::RenderPipelineState* MetalPipelineCompiler::Compile(bool forceCompile, bool
|
|||||||
g_compiling_pipelines++;
|
g_compiling_pipelines++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inform the pipeline cache that compilation was at least attempted
|
m_pipelineObj.m_pipeline = pipeline;
|
||||||
attemptedCompilation = true;
|
|
||||||
|
|
||||||
return pipeline;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MetalPipelineCompiler::InitFromStateRender(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const MetalAttachmentsInfo& lastUsedAttachmentsInfo, const MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr, bool& fbosMatch)
|
void MetalPipelineCompiler::InitFromStateRender(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const MetalAttachmentsInfo& lastUsedAttachmentsInfo, const MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr, bool& fbosMatch)
|
||||||
|
@ -5,18 +5,24 @@
|
|||||||
#include "Cafe/HW/Latte/ISA/LatteReg.h"
|
#include "Cafe/HW/Latte/ISA/LatteReg.h"
|
||||||
#include "Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompiler.h"
|
#include "Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompiler.h"
|
||||||
|
|
||||||
|
struct PipelineObject
|
||||||
|
{
|
||||||
|
MTL::RenderPipelineState* m_pipeline = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
class MetalPipelineCompiler
|
class MetalPipelineCompiler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MetalPipelineCompiler(class MetalRenderer* metalRenderer) : m_mtlr{metalRenderer} {}
|
MetalPipelineCompiler(class MetalRenderer* metalRenderer, PipelineObject& pipelineObj) : m_mtlr{metalRenderer}, m_pipelineObj{pipelineObj} {}
|
||||||
~MetalPipelineCompiler();
|
~MetalPipelineCompiler();
|
||||||
|
|
||||||
void InitFromState(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const LatteDecompilerShader* geometryShader, const LatteDecompilerShader* pixelShader, const class MetalAttachmentsInfo& lastUsedAttachmentsInfo, const class MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr, bool& fbosMatch);
|
void InitFromState(const LatteFetchShader* fetchShader, const LatteDecompilerShader* vertexShader, const LatteDecompilerShader* geometryShader, const LatteDecompilerShader* pixelShader, const class MetalAttachmentsInfo& lastUsedAttachmentsInfo, const class MetalAttachmentsInfo& activeAttachmentsInfo, const LatteContextRegister& lcr, bool& fbosMatch);
|
||||||
|
|
||||||
MTL::RenderPipelineState* Compile(bool forceCompile, bool isRenderThread, bool showInOverlay, bool& attemptedCompilation);
|
bool Compile(bool forceCompile, bool isRenderThread, bool showInOverlay);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class MetalRenderer* m_mtlr;
|
class MetalRenderer* m_mtlr;
|
||||||
|
PipelineObject& m_pipelineObj;
|
||||||
|
|
||||||
class RendererShaderMtl* m_vertexShaderMtl;
|
class RendererShaderMtl* m_vertexShaderMtl;
|
||||||
class RendererShaderMtl* m_geometryShaderMtl;
|
class RendererShaderMtl* m_geometryShaderMtl;
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
#include "Cafe/HW/Latte/Renderer/Metal/MetalCommon.h"
|
#include "Cafe/HW/Latte/Renderer/Metal/MetalCommon.h"
|
||||||
#include "Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.h"
|
#include "Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.h"
|
||||||
#include "Cafe/HW/Latte/Renderer/Renderer.h"
|
#include "Cafe/HW/Latte/Renderer/Renderer.h"
|
||||||
|
#include "HW/Latte/Renderer/Metal/MetalPipelineCompiler.h"
|
||||||
#include "config/CemuConfig.h"
|
#include "config/CemuConfig.h"
|
||||||
|
|
||||||
#define IMGUI_IMPL_METAL_CPP
|
#define IMGUI_IMPL_METAL_CPP
|
||||||
@ -1000,14 +1001,14 @@ void MetalRenderer::draw_execute(uint32 baseVertex, uint32 baseInstance, uint32
|
|||||||
auto renderCommandEncoder = GetRenderCommandEncoder();
|
auto renderCommandEncoder = GetRenderCommandEncoder();
|
||||||
|
|
||||||
// Render pipeline state
|
// Render pipeline state
|
||||||
MTL::RenderPipelineState* renderPipelineState = m_pipelineCache->GetRenderPipelineState(fetchShader, vertexShader, geometryShader, pixelShader, m_state.m_lastUsedFBO.m_attachmentsInfo, m_state.m_activeFBO.m_attachmentsInfo, LatteGPUState.contextNew);
|
PipelineObject* pipelineObj = m_pipelineCache->GetRenderPipelineState(fetchShader, vertexShader, geometryShader, pixelShader, m_state.m_lastUsedFBO.m_attachmentsInfo, m_state.m_activeFBO.m_attachmentsInfo, LatteGPUState.contextNew);
|
||||||
if (!renderPipelineState)
|
if (!pipelineObj->m_pipeline)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (renderPipelineState != encoderState.m_renderPipelineState)
|
if (pipelineObj->m_pipeline != encoderState.m_renderPipelineState)
|
||||||
{
|
{
|
||||||
renderCommandEncoder->setRenderPipelineState(renderPipelineState);
|
renderCommandEncoder->setRenderPipelineState(pipelineObj->m_pipeline);
|
||||||
encoderState.m_renderPipelineState = renderPipelineState;
|
encoderState.m_renderPipelineState = pipelineObj->m_pipeline;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Depth stencil state
|
// Depth stencil state
|
||||||
|
Loading…
Reference in New Issue
Block a user