Skip to content

Commit 3aaf2cc

Browse files
committed
Add SPIRV cache to VkShaderCache
1 parent 41a345d commit 3aaf2cc

File tree

6 files changed

+235
-100
lines changed

6 files changed

+235
-100
lines changed

src/common/rendering/vulkan/shaders/vk_shader.cpp

+34-67
Original file line numberDiff line numberDiff line change
@@ -34,51 +34,49 @@
3434

3535
VkShaderManager::VkShaderManager(VulkanRenderDevice* fb) : fb(fb)
3636
{
37-
ShaderCache = std::make_unique<VkShaderCache>(fb);
38-
3937
ZMinMax.vert = ShaderBuilder()
40-
.Code(GLSLCompiler()
38+
.Code(CachedGLSLCompiler()
4139
.Type(ShaderType::Vertex)
4240
.AddSource("VersionBlock", GetVersionBlock().GetChars())
4341
.AddSource("shaders/scene/vert_zminmax.glsl", LoadPrivateShaderLump("shaders/scene/vert_zminmax.glsl").GetChars())
44-
.Compile(fb->GetDevice()))
42+
.Compile(fb))
4543
.DebugName("ZMinMax.vert")
4644
.Create("ZMinMax.vert", fb->GetDevice());
4745

4846
ZMinMax.frag[0] = ShaderBuilder()
49-
.Code(GLSLCompiler()
47+
.Code(CachedGLSLCompiler()
5048
.Type(ShaderType::Fragment)
5149
.AddSource("VersionBlock", GetVersionBlock().GetChars())
5250
.AddSource("shaders/scene/frag_zminmax0.glsl", LoadPrivateShaderLump("shaders/scene/frag_zminmax0.glsl").GetChars())
53-
.Compile(fb->GetDevice()))
51+
.Compile(fb))
5452
.DebugName("ZMinMax0.frag")
5553
.Create("ZMinMax0.frag", fb->GetDevice());
5654

5755
ZMinMax.frag[1] = ShaderBuilder()
58-
.Code(GLSLCompiler()
56+
.Code(CachedGLSLCompiler()
5957
.Type(ShaderType::Fragment)
6058
.AddSource("VersionBlock", GetVersionBlock().GetChars())
6159
.AddSource("DefinesBlock", "#define MULTISAMPLE\n")
6260
.AddSource("shaders/scene/frag_zminmax0.glsl", LoadPrivateShaderLump("shaders/scene/frag_zminmax0.glsl").GetChars())
63-
.Compile(fb->GetDevice()))
61+
.Compile(fb))
6462
.DebugName("ZMinMax0.frag")
6563
.Create("ZMinMax0.frag", fb->GetDevice());
6664

6765
ZMinMax.frag[2] = ShaderBuilder()
68-
.Code(GLSLCompiler()
66+
.Code(CachedGLSLCompiler()
6967
.Type(ShaderType::Fragment)
7068
.AddSource("VersionBlock", GetVersionBlock().GetChars())
7169
.AddSource("shaders/scene/frag_zminmax1.glsl", LoadPrivateShaderLump("shaders/scene/frag_zminmax1.glsl").GetChars())
72-
.Compile(fb->GetDevice()))
70+
.Compile(fb))
7371
.DebugName("ZMinMax1.frag")
7472
.Create("ZMinMax1.frag", fb->GetDevice());
7573

7674
LightTiles = ShaderBuilder()
77-
.Code(GLSLCompiler()
75+
.Code(CachedGLSLCompiler()
7876
.Type(ShaderType::Compute)
7977
.AddSource("VersionBlock", GetVersionBlock().GetChars())
8078
.AddSource("shaders/scene/comp_lighttiles.glsl", LoadPrivateShaderLump("shaders/scene/comp_lighttiles.glsl").GetChars())
81-
.Compile(fb->GetDevice()))
79+
.Compile(fb))
8280
.DebugName("LightTiles.comp")
8381
.Create("LightTiles.comp", fb->GetDevice());
8482
}
@@ -383,7 +381,7 @@ void VkShaderManager::BuildDefinesBlock(FString &definesBlock, const char *defin
383381
{
384382
//ugh EffectState also controls layout, because specular/pbr/etc defines switch texture indices around for normal/specular/etc
385383

386-
definesBlock << LoadPrivateShaderLump("shaders/shaderkey.glsl").GetChars() << "\n";
384+
definesBlock << SubstituteDefines(LoadPrivateShaderLump("shaders/shaderkey.glsl")).GetChars() << "\n";
387385

388386
definesBlock << "#define UBERSHADERS\n";
389387

@@ -570,28 +568,27 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader(FString shadername
570568
BuildLayoutBlock(layoutBlock, false, key, shader);
571569

572570
FString codeBlock;
573-
codeBlock << LoadPrivateShaderLump(vert_lump).GetChars() << "\n";
571+
codeBlock << SubstituteDefines(LoadPrivateShaderLump(vert_lump)).GetChars() << "\n";
574572
if(vert_lump_custom)
575573
{
576574
codeBlock << "\n#line 1\n";
577-
codeBlock << LoadPublicShaderLump(vert_lump_custom).GetChars() << "\n";
575+
codeBlock << SubstituteDefines(LoadPublicShaderLump(vert_lump_custom)).GetChars() << "\n";
578576
}
579577
else
580578
{
581-
codeBlock << LoadPrivateShaderLump("shaders/scene/vert_nocustom.glsl").GetChars() << "\n";
579+
codeBlock << SubstituteDefines(LoadPrivateShaderLump("shaders/scene/vert_nocustom.glsl")).GetChars() << "\n";
582580
}
583581

584582
return ShaderBuilder()
585-
.Code(GLSLCompiler()
583+
.Code(CachedGLSLCompiler()
586584
.Type(ShaderType::Vertex)
587585
.AddSource("VersionBlock", GetVersionBlock().GetChars())
588586
.AddSource("DefinesBlock", definesBlock.GetChars())
589587
.AddSource("LayoutBlock", layoutBlock.GetChars())
590-
.AddSource("shaders/scene/layout_shared.glsl", LoadPrivateShaderLump("shaders/scene/layout_shared.glsl").GetChars())
588+
.AddSource("shaders/scene/layout_shared.glsl", SubstituteDefines(LoadPrivateShaderLump("shaders/scene/layout_shared.glsl")).GetChars())
591589
.AddSource(vert_lump_custom ? vert_lump_custom : vert_lump, codeBlock.GetChars())
592-
.OnIncludeLocal([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, false); })
593-
.OnIncludeSystem([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, true); })
594-
.Compile(fb->GetDevice()))
590+
.IncludeFilter([](FString s) { return SubstituteDefines(std::move(s), false); })
591+
.Compile(fb))
595592
.DebugName(shadername.GetChars())
596593
.Create(shadername.GetChars(), fb->GetDevice());
597594
}
@@ -605,7 +602,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
605602
BuildLayoutBlock(layoutBlock, true, key, shader);
606603

607604
FString codeBlock;
608-
codeBlock << LoadPrivateShaderLump(frag_lump).GetChars() << "\n";
605+
codeBlock << SubstituteDefines(LoadPrivateShaderLump(frag_lump)).GetChars() << "\n";
609606

610607
FString materialname = "MaterialBlock";
611608
FString materialBlock;
@@ -617,7 +614,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
617614
if (material_lump)
618615
{
619616
materialname = material_lump;
620-
materialBlock = LoadPublicShaderLump(material_lump);
617+
materialBlock = SubstituteDefines(LoadPublicShaderLump(material_lump));
621618

622619
// Attempt to fix old custom shaders:
623620

@@ -636,15 +633,15 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
636633
FString code;
637634
if (materialBlock.IndexOf("ProcessTexel") >= 0)
638635
{
639-
code = LoadPrivateShaderLump("shaders/scene/material_legacy_ptexel.glsl");
636+
code = SubstituteDefines(LoadPrivateShaderLump("shaders/scene/material_legacy_ptexel.glsl"));
640637
}
641638
else if (materialBlock.IndexOf("Process") >= 0)
642639
{
643-
code = LoadPrivateShaderLump("shaders/scene/material_legacy_process.glsl");
640+
code = SubstituteDefines(LoadPrivateShaderLump("shaders/scene/material_legacy_process.glsl"));
644641
}
645642
else
646643
{
647-
code = LoadPrivateShaderLump("shaders/scene/material_default.glsl");
644+
code = SubstituteDefines(LoadPrivateShaderLump("shaders/scene/material_default.glsl"));
648645
}
649646
code << "\n#line 1\n";
650647

@@ -656,7 +653,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
656653

657654
definesBlock << "#define LEGACY_USER_SHADER\n";
658655

659-
FString code = LoadPrivateShaderLump("shaders/scene/material_legacy_pmaterial.glsl");
656+
FString code = SubstituteDefines(LoadPrivateShaderLump("shaders/scene/material_legacy_pmaterial.glsl"));
660657
code << "\n#line 1\n";
661658

662659
materialBlock = code + materialBlock;
@@ -669,34 +666,33 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
669666

670667
if(light_lump_shared)
671668
{
672-
lightBlock << LoadPrivateShaderLump(light_lump_shared).GetChars();
669+
lightBlock << SubstituteDefines(LoadPrivateShaderLump(light_lump_shared)).GetChars();
673670
}
674671

675-
lightBlock << LoadPrivateShaderLump(light_lump).GetChars();
672+
lightBlock << SubstituteDefines(LoadPrivateShaderLump(light_lump)).GetChars();
676673

677674
}
678675

679676
if (mateffect_lump && mateffectBlock.IsEmpty())
680677
{
681678
mateffectname = mateffect_lump;
682-
mateffectBlock << LoadPrivateShaderLump(mateffect_lump).GetChars();
679+
mateffectBlock << SubstituteDefines(LoadPrivateShaderLump(mateffect_lump)).GetChars();
683680
}
684681

685682
return ShaderBuilder()
686-
.Code(GLSLCompiler()
683+
.Code(CachedGLSLCompiler()
687684
.Type(ShaderType::Fragment)
688685
.AddSource("VersionBlock", GetVersionBlock().GetChars())
689686
.AddSource("DefinesBlock", definesBlock.GetChars())
690687
.AddSource("LayoutBlock", layoutBlock.GetChars())
691-
.AddSource("shaders/scene/layout_shared.glsl", LoadPrivateShaderLump("shaders/scene/layout_shared.glsl").GetChars())
692-
.AddSource("shaders/scene/includes.glsl", LoadPrivateShaderLump("shaders/scene/includes.glsl").GetChars())
688+
.AddSource("shaders/scene/layout_shared.glsl", SubstituteDefines(LoadPrivateShaderLump("shaders/scene/layout_shared.glsl")).GetChars())
689+
.AddSource("shaders/scene/includes.glsl", SubstituteDefines(LoadPrivateShaderLump("shaders/scene/includes.glsl")).GetChars())
693690
.AddSource(mateffectname.GetChars(), mateffectBlock.GetChars())
694691
.AddSource(materialname.GetChars(), materialBlock.GetChars())
695692
.AddSource(lightname.GetChars(), lightBlock.GetChars())
696693
.AddSource(frag_lump, codeBlock.GetChars())
697-
.OnIncludeLocal([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, false); })
698-
.OnIncludeSystem([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, true); })
699-
.Compile(fb->GetDevice()))
694+
.IncludeFilter([](FString s) { return SubstituteDefines(std::move(s), false); })
695+
.Compile(fb))
700696
.DebugName(shadername.GetChars())
701697
.Create(shadername.GetChars(), fb->GetDevice());
702698
}
@@ -725,43 +721,14 @@ FString VkShaderManager::GetVersionBlock()
725721
return versionBlock;
726722
}
727723

728-
ShaderIncludeResult VkShaderManager::OnInclude(FString headerName, FString includerName, size_t depth, bool system)
729-
{
730-
if (depth > 8)
731-
I_Error("Too much include recursion!");
732-
733-
FString includeguardname;
734-
includeguardname << "_HEADERGUARD_" << headerName.GetChars();
735-
includeguardname.ReplaceChars("/\\.", '_');
736-
737-
FString code;
738-
code << "#ifndef " << includeguardname.GetChars() << "\n";
739-
code << "#define " << includeguardname.GetChars() << "\n";
740-
code << "#line 1\n";
741-
742-
if (system)
743-
code << LoadPrivateShaderLump(headerName.GetChars()).GetChars() << "\n";
744-
else
745-
code << LoadPublicShaderLump(headerName.GetChars()).GetChars() << "\n";
746-
747-
code << "#endif\n";
748-
749-
return ShaderIncludeResult(headerName.GetChars(), code.GetChars());
750-
}
751-
752724
FString VkShaderManager::LoadPublicShaderLump(const char* lumpname)
753725
{
754-
int lump = fileSystem.CheckNumForFullName(lumpname, 0);
755-
if (lump == -1) lump = fileSystem.CheckNumForFullName(lumpname);
756-
if (lump == -1) I_Error("Unable to load '%s'", lumpname);
757-
return GetStringFromLump(lump);
726+
return fb->GetShaderCache()->GetPublicFile(lumpname).Code;
758727
}
759728

760729
FString VkShaderManager::LoadPrivateShaderLump(const char* lumpname)
761730
{
762-
int lump = fileSystem.CheckNumForFullName(lumpname, 0);
763-
if (lump == -1) I_Error("Unable to load '%s'", lumpname);
764-
return GetStringFromLump(lump);
731+
return fb->GetShaderCache()->GetPrivateFile(lumpname).Code;
765732
}
766733

767734
FString VkShaderManager::SubstituteDefines(FString str, bool isUberShader)

src/common/rendering/vulkan/shaders/vk_shader.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class ShaderIncludeResult;
1515
class VulkanRenderDevice;
1616
class VulkanDevice;
1717
class VulkanShader;
18-
class VkShaderCache;
1918
class VkPPShader;
2019
class PPShader;
2120

@@ -162,21 +161,17 @@ class VkShaderManager
162161
std::unique_ptr<VulkanShader> LoadVertShader(FString shadername, const char *vert_lump, const char *vert_lump_custom, const char *defines, const VkShaderKey& key, const UserShaderDesc *shader);
163162
std::unique_ptr<VulkanShader> LoadFragShader(FString shadername, const char *frag_lump, const char *material_lump, const char* mateffect_lump, const char *light_lump_shared, const char *lightmodel_lump, const char *defines, const VkShaderKey& key, const UserShaderDesc *shader);
164163

165-
ShaderIncludeResult OnInclude(FString headerName, FString includerName, size_t depth, bool system);
166-
167164
FString GetVersionBlock();
168165
FString LoadPublicShaderLump(const char *lumpname);
169166
FString LoadPrivateShaderLump(const char *lumpname);
170167

171-
static FString SubstituteDefines(FString code, bool isUberShader);
168+
static FString SubstituteDefines(FString code, bool isUberShader = false);
172169

173170
void BuildLayoutBlock(FString &definesBlock, bool isFrag, const VkShaderKey& key, const UserShaderDesc *shader, bool isUberShader = false);
174171
void BuildDefinesBlock(FString &definesBlock, const char *defines, bool isFrag, const VkShaderKey& key, const UserShaderDesc *shader, bool isUberShader = false);
175172

176173
VulkanRenderDevice* fb = nullptr;
177174

178-
std::unique_ptr<VkShaderCache> ShaderCache;
179-
180175
std::map<VkShaderKey, VkShaderProgram> programs;
181176

182177
std::list<VkPPShader*> PPShaders;

0 commit comments

Comments
 (0)