Skip to content

Commit 53efb9e

Browse files
committed
prepare for shader cache
1 parent 13834ca commit 53efb9e

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,22 +665,21 @@ void MetalRenderer::draw_beginSequence()
665665

666666
if (!rasterizerEnable == false)
667667
m_state.m_skipDrawSequence = true;
668+
669+
// TODO: is this even needed?
670+
if (!m_state.m_activeFBO)
671+
m_state.m_skipDrawSequence = true;
668672
}
669673

670674
void MetalRenderer::draw_execute(uint32 baseVertex, uint32 baseInstance, uint32 instanceCount, uint32 count, MPTR indexDataMPTR, Latte::LATTE_VGT_DMA_INDEX_TYPE::E_INDEX_TYPE indexType, bool isFirst)
671675
{
672-
//if (m_state.skipDrawSequence)
676+
// TODO: uncomment
677+
//if (m_state.m_skipDrawSequence)
673678
//{
674679
// return;
675680
//}
676681

677682
// Render pass
678-
if (!m_state.m_activeFBO)
679-
{
680-
debug_printf("no active FBO, skipping draw\n");
681-
return;
682-
}
683-
684683
auto renderCommandEncoder = GetRenderCommandEncoder();
685684

686685
// Shaders

src/Cafe/HW/Latte/Renderer/Metal/RendererShaderMtl.cpp

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,40 @@
22
#include "Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h"
33
#include "Cafe/HW/Latte/Renderer/Metal/LatteToMtl.h"
44
#include "Cafe/HW/Latte/Renderer/Metal/MetalCommon.h"
5+
#include "Cemu/FileCache/FileCache.h"
6+
#include "config/ActiveSettings.h"
57

68
#include "Cemu/Logging/CemuLogging.h"
79
#include "Common/precompiled.h"
810

11+
bool s_isLoadingShadersMtl{ false };
12+
class FileCache* s_mslCache{nullptr};
13+
14+
extern std::atomic_int g_compiled_shaders_total;
15+
extern std::atomic_int g_compiled_shaders_async;
16+
917
RendererShaderMtl::RendererShaderMtl(MetalRenderer* mtlRenderer, ShaderType type, uint64 baseHash, uint64 auxHash, bool isGameShader, bool isGfxPackShader, const std::string& mslCode)
1018
: RendererShader(type, baseHash, auxHash, isGameShader, isGfxPackShader), m_mtlr{mtlRenderer}
1119
{
12-
// Fragment functions are compiled just-in-time
20+
if (LoadBinary())
21+
return;
22+
1323
if (m_type == ShaderType::kFragment)
1424
{
25+
// Fragment functions are compiled just-in-time
1526
m_mslCode = mslCode;
1627
}
1728
else
1829
{
1930
Compile(mslCode);
2031
}
32+
33+
// Store the compiled shader in the cache
34+
StoreBinary();
35+
36+
// Count shader compilation
37+
if (!s_isLoadingShadersMtl)
38+
g_compiled_shaders_total++;
2139
}
2240

2341
RendererShaderMtl::~RendererShaderMtl()
@@ -69,17 +87,29 @@ void RendererShaderMtl::CompileFragmentFunction(CachedFBOMtl* activeFBO)
6987

7088
void RendererShaderMtl::ShaderCacheLoading_begin(uint64 cacheTitleId)
7189
{
72-
cemuLog_log(LogType::MetalLogging, "RendererShaderMtl::ShaderCacheLoading_begin not implemented!");
90+
if (s_mslCache)
91+
{
92+
delete s_mslCache;
93+
}
94+
uint32 spirvCacheMagic = GeneratePrecompiledCacheId();
95+
const std::string cacheFilename = fmt::format("{:016x}_msl.bin", cacheTitleId);
96+
const fs::path cachePath = ActiveSettings::GetCachePath("shaderCache/precompiled/{}", cacheFilename);
97+
s_mslCache = FileCache::Open(cachePath, true, spirvCacheMagic);
98+
if (!s_mslCache)
99+
cemuLog_log(LogType::Force, "Unable to open MSL cache {}", cacheFilename);
100+
s_isLoadingShadersMtl = true;
73101
}
74102

75103
void RendererShaderMtl::ShaderCacheLoading_end()
76104
{
77-
cemuLog_log(LogType::MetalLogging, "RendererShaderMtl::ShaderCacheLoading_end not implemented!");
105+
s_isLoadingShadersMtl = false;
78106
}
79107

80108
void RendererShaderMtl::ShaderCacheLoading_Close()
81109
{
82-
cemuLog_log(LogType::MetalLogging, "RendererShaderMtl::ShaderCacheLoading_Close not implemented!");
110+
delete s_mslCache;
111+
g_compiled_shaders_total = 0;
112+
g_compiled_shaders_async = 0;
83113
}
84114

85115
void RendererShaderMtl::Compile(const std::string& mslCode)
@@ -95,3 +125,33 @@ void RendererShaderMtl::Compile(const std::string& mslCode)
95125
m_function = library->newFunction(NS::String::string("main0", NS::ASCIIStringEncoding));
96126
library->release();
97127
}
128+
129+
bool RendererShaderMtl::LoadBinary()
130+
{
131+
// HACK: since fragment functions are compiled just-in-time, we cannot load them from the cache
132+
if (m_type == ShaderType::kFragment)
133+
return false;
134+
135+
uint64 h1, h2;
136+
GenerateShaderPrecompiledCacheFilename(m_type, m_baseHash, m_auxHash, h1, h2);
137+
if (!s_mslCache->GetFile({h1, h2 }, m_binary))
138+
return false;
139+
140+
// TODO: implement
141+
return false;
142+
143+
return true;
144+
}
145+
146+
void RendererShaderMtl::StoreBinary()
147+
{
148+
if (m_binary.size() == 0)
149+
{
150+
// TODO: retrieve the binary from the function
151+
return;
152+
}
153+
154+
uint64 h1, h2;
155+
GenerateShaderPrecompiledCacheFilename(m_type, m_baseHash, m_auxHash, h1, h2);
156+
s_mslCache->AddFileAsync({h1, h2 }, m_binary.data(), m_binary.size());
157+
}

src/Cafe/HW/Latte/Renderer/Metal/RendererShaderMtl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ class RendererShaderMtl : public RendererShader
5858

5959
MTL::Function* m_function = nullptr;
6060

61+
std::vector<uint8> m_binary;
6162
std::string m_mslCode;
6263

6364
void Compile(const std::string& mslCode);
65+
66+
bool LoadBinary();
67+
void StoreBinary();
6468
};

0 commit comments

Comments
 (0)