Skip to content

Commit

Permalink
Merge pull request #5679 from stenzek/no-host-state-in-uids
Browse files Browse the repository at this point in the history
ShaderGen: Decouple host state from shader UIDs
  • Loading branch information
stenzek committed Jul 27, 2017
2 parents 10db1bc + 3ea9d86 commit a11bde7
Show file tree
Hide file tree
Showing 33 changed files with 581 additions and 347 deletions.
31 changes: 19 additions & 12 deletions Source/Core/VideoBackends/D3D/GeometryShaderCache.cpp
Expand Up @@ -158,18 +158,23 @@ void GeometryShaderCache::Init()
Clear();

if (g_ActiveConfig.bShaderCache)
{
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));

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);
}
LoadShaderCache();
}

last_entry = nullptr;
void GeometryShaderCache::LoadShaderCache()
{
GeometryShaderCacheInserter inserter;
g_gs_disk_cache.OpenAndRead(GetDiskShaderCacheFileName(APIType::D3D, "GS", true, true), 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.
Expand All @@ -180,6 +185,7 @@ void GeometryShaderCache::Clear()
GeometryShaders.clear();

last_entry = nullptr;
last_uid = {};
}

void GeometryShaderCache::Shutdown()
Expand Down Expand Up @@ -230,7 +236,8 @@ bool GeometryShaderCache::SetShader(u32 primitive_type)
}

// Need to compile a new shader
ShaderCode code = GenerateGeometryShaderCode(APIType::D3D, uid.GetUidData());
ShaderCode code =
GenerateGeometryShaderCode(APIType::D3D, ShaderHostConfig::GetCurrent(), uid.GetUidData());

D3DBlob* pbytecode;
if (!D3D::CompileGeometryShader(code.GetBuffer(), &pbytecode))
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/VideoBackends/D3D/GeometryShaderCache.h
Expand Up @@ -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
Expand All @@ -38,6 +39,8 @@ class GeometryShaderCache

typedef std::map<GeometryShaderUid, GSCacheEntry> GSCache;

static void LoadShaderCache();

static GSCache GeometryShaders;
static const GSCacheEntry* last_entry;
static GeometryShaderUid last_uid;
Expand Down
31 changes: 19 additions & 12 deletions Source/Core/VideoBackends/D3D/PixelShaderCache.cpp
Expand Up @@ -498,18 +498,23 @@ 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));

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);
}
LoadShaderCache();
}

last_entry = nullptr;
void PixelShaderCache::LoadShaderCache()
{
PixelShaderCacheInserter inserter;
g_ps_disk_cache.OpenAndRead(GetDiskShaderCacheFileName(APIType::D3D, "PS", true, true), 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.
Expand All @@ -520,6 +525,7 @@ void PixelShaderCache::Clear()
PixelShaders.clear();

last_entry = nullptr;
last_uid = {};
}

// Used in Swap() when AA mode has changed
Expand Down Expand Up @@ -583,7 +589,8 @@ bool PixelShaderCache::SetShader()
}

// Need to compile a new shader
ShaderCode code = GeneratePixelShaderCode(APIType::D3D, uid.GetUidData());
ShaderCode code =
GeneratePixelShaderCode(APIType::D3D, ShaderHostConfig::GetCurrent(), uid.GetUidData());

D3DBlob* pbytecode;
if (!D3D::CompilePixelShader(code.GetBuffer(), &pbytecode))
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/VideoBackends/D3D/PixelShaderCache.h
Expand Up @@ -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
Expand Down Expand Up @@ -46,6 +47,8 @@ class PixelShaderCache

typedef std::map<PixelShaderUid, PSCacheEntry> PSCache;

static void LoadShaderCache();

static PSCache PixelShaders;
static const PSCacheEntry* last_entry;
static PixelShaderUid last_uid;
Expand Down
7 changes: 7 additions & 0 deletions Source/Core/VideoBackends/D3D/Render.cpp
Expand Up @@ -894,6 +894,13 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
D3D11_CLEAR_DEPTH, 0.f, 0);
}

if (CheckForHostConfigChanges())
{
VertexShaderCache::Reload();
GeometryShaderCache::Reload();
PixelShaderCache::Reload();
}

// begin next frame
RestoreAPIState();
D3D::BeginFrame();
Expand Down
31 changes: 19 additions & 12 deletions Source/Core/VideoBackends/D3D/VertexShaderCache.cpp
Expand Up @@ -159,18 +159,23 @@ 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));

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);
}
LoadShaderCache();
}

last_entry = nullptr;
void VertexShaderCache::LoadShaderCache()
{
VertexShaderCacheInserter inserter;
g_vs_disk_cache.OpenAndRead(GetDiskShaderCacheFileName(APIType::D3D, "VS", true, true), inserter);
}

void VertexShaderCache::Reload()
{
g_vs_disk_cache.Sync();
g_vs_disk_cache.Close();
Clear();

if (g_ActiveConfig.bShaderCache)
LoadShaderCache();
}

void VertexShaderCache::Clear()
Expand All @@ -180,6 +185,7 @@ void VertexShaderCache::Clear()
vshaders.clear();

last_entry = nullptr;
last_uid = {};
}

void VertexShaderCache::Shutdown()
Expand Down Expand Up @@ -222,7 +228,8 @@ bool VertexShaderCache::SetShader()
return (entry.shader != nullptr);
}

ShaderCode code = GenerateVertexShaderCode(APIType::D3D, uid.GetUidData());
ShaderCode code =
GenerateVertexShaderCode(APIType::D3D, ShaderHostConfig::GetCurrent(), uid.GetUidData());

D3DBlob* pbytecode = nullptr;
D3D::CompileVertexShader(code.GetBuffer(), &pbytecode);
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/VideoBackends/D3D/VertexShaderCache.h
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +54,8 @@ class VertexShaderCache
};
typedef std::map<VertexShaderUid, VSCacheEntry> VSCache;

static void LoadShaderCache();

static VSCache vshaders;
static const VSCacheEntry* last_entry;
static VertexShaderUid last_uid;
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/VideoBackends/Null/ShaderCache.h
Expand Up @@ -46,7 +46,7 @@ class VertexShaderCache : public ShaderCache<VertexShaderUid>
}
ShaderCode GenerateCode(APIType api_type, VertexShaderUid uid) override
{
return GenerateVertexShaderCode(api_type, uid.GetUidData());
return GenerateVertexShaderCode(api_type, ShaderHostConfig::GetCurrent(), uid.GetUidData());
}
};

Expand All @@ -62,7 +62,7 @@ class GeometryShaderCache : public ShaderCache<GeometryShaderUid>
}
ShaderCode GenerateCode(APIType api_type, GeometryShaderUid uid) override
{
return GenerateGeometryShaderCode(api_type, uid.GetUidData());
return GenerateGeometryShaderCode(api_type, ShaderHostConfig::GetCurrent(), uid.GetUidData());
}
};

Expand All @@ -78,7 +78,7 @@ class PixelShaderCache : public ShaderCache<PixelShaderUid>
}
ShaderCode GenerateCode(APIType api_type, PixelShaderUid uid) override
{
return GeneratePixelShaderCode(api_type, uid.GetUidData());
return GeneratePixelShaderCode(api_type, ShaderHostConfig::GetCurrent(), uid.GetUidData());
}
};

Expand Down

0 comments on commit a11bde7

Please sign in to comment.