D3D11: Reload shader cache when relevant config changes

This commit is contained in:
Stenzek 2017-06-24 19:29:03 +10:00
parent 62a901508b
commit 228ddb8aba
8 changed files with 88 additions and 30 deletions

View File

@ -158,18 +158,29 @@ void GeometryShaderCache::Init()
Clear();
if (g_ActiveConfig.bShaderCache)
{
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
LoadShaderCache();
}
std::string cache_filename =
StringFromFormat("%sdx11-%s-gs.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(),
SConfig::GetInstance().GetGameID().c_str());
GeometryShaderCacheInserter inserter;
g_gs_disk_cache.OpenAndRead(cache_filename, inserter);
}
void GeometryShaderCache::LoadShaderCache()
{
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
last_entry = nullptr;
std::string cache_filename = StringFromFormat(
"%sdx11-%s-%s-gs.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(),
SConfig::GetInstance().GetGameID().c_str(), g_ActiveConfig.GetHostConfigFilename().c_str());
GeometryShaderCacheInserter inserter;
g_gs_disk_cache.OpenAndRead(cache_filename, inserter);
}
void GeometryShaderCache::Reload()
{
g_gs_disk_cache.Sync();
g_gs_disk_cache.Close();
Clear();
if (g_ActiveConfig.bShaderCache)
LoadShaderCache();
}
// ONLY to be used during shutdown.
@ -180,6 +191,7 @@ void GeometryShaderCache::Clear()
GeometryShaders.clear();
last_entry = nullptr;
last_uid = {};
}
void GeometryShaderCache::Shutdown()

View File

@ -15,6 +15,7 @@ class GeometryShaderCache
{
public:
static void Init();
static void Reload();
static void Clear();
static void Shutdown();
static bool SetShader(u32 primitive_type); // TODO: Should be renamed to LoadShader
@ -38,6 +39,8 @@ private:
typedef std::map<GeometryShaderUid, GSCacheEntry> GSCache;
static void LoadShaderCache();
static GSCache GeometryShaders;
static const GSCacheEntry* last_entry;
static GeometryShaderUid last_uid;

View File

@ -498,18 +498,29 @@ void PixelShaderCache::Init()
SETSTAT(stats.numPixelShadersAlive, 0);
if (g_ActiveConfig.bShaderCache)
{
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
LoadShaderCache();
}
std::string cache_filename =
StringFromFormat("%sdx11-%s-ps.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(),
SConfig::GetInstance().GetGameID().c_str());
PixelShaderCacheInserter inserter;
g_ps_disk_cache.OpenAndRead(cache_filename, inserter);
}
void PixelShaderCache::LoadShaderCache()
{
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
last_entry = nullptr;
std::string cache_filename = StringFromFormat(
"%sdx11-%s-%s-ps.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(),
SConfig::GetInstance().GetGameID().c_str(), g_ActiveConfig.GetHostConfigFilename().c_str());
PixelShaderCacheInserter inserter;
g_ps_disk_cache.OpenAndRead(cache_filename, inserter);
}
void PixelShaderCache::Reload()
{
g_ps_disk_cache.Sync();
g_ps_disk_cache.Close();
Clear();
if (g_ActiveConfig.bShaderCache)
LoadShaderCache();
}
// ONLY to be used during shutdown.
@ -520,6 +531,7 @@ void PixelShaderCache::Clear()
PixelShaders.clear();
last_entry = nullptr;
last_uid = {};
}
// Used in Swap() when AA mode has changed

View File

@ -15,6 +15,7 @@ class PixelShaderCache
{
public:
static void Init();
static void Reload();
static void Clear();
static void Shutdown();
static bool SetShader(); // TODO: Should be renamed to LoadShader
@ -46,6 +47,8 @@ private:
typedef std::map<PixelShaderUid, PSCacheEntry> PSCache;
static void LoadShaderCache();
static PSCache PixelShaders;
static const PSCacheEntry* last_entry;
static PixelShaderUid last_uid;

View File

@ -242,6 +242,7 @@ Renderer::Renderer() : ::Renderer(D3D::GetBackBufferWidth(), D3D::GetBackBufferH
s_last_stereo_mode = g_ActiveConfig.iStereoMode > 0;
s_last_xfb_mode = g_ActiveConfig.bUseRealXFB;
s_last_fullscreen_mode = D3D::GetFullscreenState();
m_last_host_config_bits = g_ActiveConfig.GetHostConfigBits();
g_framebuffer_manager = std::make_unique<FramebufferManager>(m_target_width, m_target_height);
SetupDeviceObjects();
@ -894,6 +895,16 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
D3D11_CLEAR_DEPTH, 0.f, 0);
}
u32 new_host_config_bits = g_ActiveConfig.GetHostConfigBits();
if (new_host_config_bits != m_last_host_config_bits)
{
OSD::AddMessage("Video config changed, reloading shaders.", OSD::Duration::NORMAL);
VertexShaderCache::Reload();
GeometryShaderCache::Reload();
PixelShaderCache::Reload();
m_last_host_config_bits = new_host_config_bits;
}
// begin next frame
RestoreAPIState();
D3D::BeginFrame();

View File

@ -64,5 +64,7 @@ public:
private:
void BlitScreen(TargetRectangle src, TargetRectangle dst, D3DTexture2D* src_texture,
u32 src_width, u32 src_height, float Gamma);
u32 m_last_host_config_bits = 0;
};
}

View File

@ -159,18 +159,29 @@ void VertexShaderCache::Init()
SETSTAT(stats.numVertexShadersAlive, 0);
if (g_ActiveConfig.bShaderCache)
{
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
LoadShaderCache();
}
std::string cache_filename =
StringFromFormat("%sdx11-%s-vs.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(),
SConfig::GetInstance().GetGameID().c_str());
VertexShaderCacheInserter inserter;
g_vs_disk_cache.OpenAndRead(cache_filename, inserter);
}
void VertexShaderCache::LoadShaderCache()
{
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
last_entry = nullptr;
std::string cache_filename = StringFromFormat(
"%sdx11-%s-%s-vs.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(),
SConfig::GetInstance().GetGameID().c_str(), g_ActiveConfig.GetHostConfigFilename().c_str());
VertexShaderCacheInserter inserter;
g_vs_disk_cache.OpenAndRead(cache_filename, inserter);
}
void VertexShaderCache::Reload()
{
g_vs_disk_cache.Sync();
g_vs_disk_cache.Close();
Clear();
if (g_ActiveConfig.bShaderCache)
LoadShaderCache();
}
void VertexShaderCache::Clear()
@ -180,6 +191,7 @@ void VertexShaderCache::Clear()
vshaders.clear();
last_entry = nullptr;
last_uid = {};
}
void VertexShaderCache::Shutdown()

View File

@ -17,6 +17,7 @@ class VertexShaderCache
{
public:
static void Init();
static void Reload();
static void Clear();
static void Shutdown();
static bool SetShader(); // TODO: Should be renamed to LoadShader
@ -53,6 +54,8 @@ private:
};
typedef std::map<VertexShaderUid, VSCacheEntry> VSCache;
static void LoadShaderCache();
static VSCache vshaders;
static const VSCacheEntry* last_entry;
static VertexShaderUid last_uid;