34
34
35
35
VkShaderManager::VkShaderManager (VulkanRenderDevice* fb) : fb(fb)
36
36
{
37
- ShaderCache = std::make_unique<VkShaderCache>(fb);
38
-
39
37
ZMinMax.vert = ShaderBuilder ()
40
- .Code (GLSLCompiler ()
38
+ .Code (CachedGLSLCompiler ()
41
39
.Type (ShaderType::Vertex)
42
40
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
43
41
.AddSource (" shaders/scene/vert_zminmax.glsl" , LoadPrivateShaderLump (" shaders/scene/vert_zminmax.glsl" ).GetChars ())
44
- .Compile (fb-> GetDevice () ))
42
+ .Compile (fb))
45
43
.DebugName (" ZMinMax.vert" )
46
44
.Create (" ZMinMax.vert" , fb->GetDevice ());
47
45
48
46
ZMinMax.frag [0 ] = ShaderBuilder ()
49
- .Code (GLSLCompiler ()
47
+ .Code (CachedGLSLCompiler ()
50
48
.Type (ShaderType::Fragment)
51
49
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
52
50
.AddSource (" shaders/scene/frag_zminmax0.glsl" , LoadPrivateShaderLump (" shaders/scene/frag_zminmax0.glsl" ).GetChars ())
53
- .Compile (fb-> GetDevice () ))
51
+ .Compile (fb))
54
52
.DebugName (" ZMinMax0.frag" )
55
53
.Create (" ZMinMax0.frag" , fb->GetDevice ());
56
54
57
55
ZMinMax.frag [1 ] = ShaderBuilder ()
58
- .Code (GLSLCompiler ()
56
+ .Code (CachedGLSLCompiler ()
59
57
.Type (ShaderType::Fragment)
60
58
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
61
59
.AddSource (" DefinesBlock" , " #define MULTISAMPLE\n " )
62
60
.AddSource (" shaders/scene/frag_zminmax0.glsl" , LoadPrivateShaderLump (" shaders/scene/frag_zminmax0.glsl" ).GetChars ())
63
- .Compile (fb-> GetDevice () ))
61
+ .Compile (fb))
64
62
.DebugName (" ZMinMax0.frag" )
65
63
.Create (" ZMinMax0.frag" , fb->GetDevice ());
66
64
67
65
ZMinMax.frag [2 ] = ShaderBuilder ()
68
- .Code (GLSLCompiler ()
66
+ .Code (CachedGLSLCompiler ()
69
67
.Type (ShaderType::Fragment)
70
68
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
71
69
.AddSource (" shaders/scene/frag_zminmax1.glsl" , LoadPrivateShaderLump (" shaders/scene/frag_zminmax1.glsl" ).GetChars ())
72
- .Compile (fb-> GetDevice () ))
70
+ .Compile (fb))
73
71
.DebugName (" ZMinMax1.frag" )
74
72
.Create (" ZMinMax1.frag" , fb->GetDevice ());
75
73
76
74
LightTiles = ShaderBuilder ()
77
- .Code (GLSLCompiler ()
75
+ .Code (CachedGLSLCompiler ()
78
76
.Type (ShaderType::Compute)
79
77
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
80
78
.AddSource (" shaders/scene/comp_lighttiles.glsl" , LoadPrivateShaderLump (" shaders/scene/comp_lighttiles.glsl" ).GetChars ())
81
- .Compile (fb-> GetDevice () ))
79
+ .Compile (fb))
82
80
.DebugName (" LightTiles.comp" )
83
81
.Create (" LightTiles.comp" , fb->GetDevice ());
84
82
}
@@ -383,7 +381,7 @@ void VkShaderManager::BuildDefinesBlock(FString &definesBlock, const char *defin
383
381
{
384
382
// ugh EffectState also controls layout, because specular/pbr/etc defines switch texture indices around for normal/specular/etc
385
383
386
- definesBlock << LoadPrivateShaderLump (" shaders/shaderkey.glsl" ).GetChars () << " \n " ;
384
+ definesBlock << SubstituteDefines ( LoadPrivateShaderLump (" shaders/shaderkey.glsl" ) ).GetChars () << " \n " ;
387
385
388
386
definesBlock << " #define UBERSHADERS\n " ;
389
387
@@ -570,28 +568,27 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader(FString shadername
570
568
BuildLayoutBlock (layoutBlock, false , key, shader);
571
569
572
570
FString codeBlock;
573
- codeBlock << LoadPrivateShaderLump (vert_lump).GetChars () << " \n " ;
571
+ codeBlock << SubstituteDefines ( LoadPrivateShaderLump (vert_lump) ).GetChars () << " \n " ;
574
572
if (vert_lump_custom)
575
573
{
576
574
codeBlock << " \n #line 1\n " ;
577
- codeBlock << LoadPublicShaderLump (vert_lump_custom).GetChars () << " \n " ;
575
+ codeBlock << SubstituteDefines ( LoadPublicShaderLump (vert_lump_custom) ).GetChars () << " \n " ;
578
576
}
579
577
else
580
578
{
581
- codeBlock << LoadPrivateShaderLump (" shaders/scene/vert_nocustom.glsl" ).GetChars () << " \n " ;
579
+ codeBlock << SubstituteDefines ( LoadPrivateShaderLump (" shaders/scene/vert_nocustom.glsl" ) ).GetChars () << " \n " ;
582
580
}
583
581
584
582
return ShaderBuilder ()
585
- .Code (GLSLCompiler ()
583
+ .Code (CachedGLSLCompiler ()
586
584
.Type (ShaderType::Vertex)
587
585
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
588
586
.AddSource (" DefinesBlock" , definesBlock.GetChars ())
589
587
.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 ())
591
589
.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))
595
592
.DebugName (shadername.GetChars ())
596
593
.Create (shadername.GetChars (), fb->GetDevice ());
597
594
}
@@ -605,7 +602,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
605
602
BuildLayoutBlock (layoutBlock, true , key, shader);
606
603
607
604
FString codeBlock;
608
- codeBlock << LoadPrivateShaderLump (frag_lump).GetChars () << " \n " ;
605
+ codeBlock << SubstituteDefines ( LoadPrivateShaderLump (frag_lump) ).GetChars () << " \n " ;
609
606
610
607
FString materialname = " MaterialBlock" ;
611
608
FString materialBlock;
@@ -617,7 +614,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
617
614
if (material_lump)
618
615
{
619
616
materialname = material_lump;
620
- materialBlock = LoadPublicShaderLump (material_lump);
617
+ materialBlock = SubstituteDefines ( LoadPublicShaderLump (material_lump) );
621
618
622
619
// Attempt to fix old custom shaders:
623
620
@@ -636,15 +633,15 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
636
633
FString code;
637
634
if (materialBlock.IndexOf (" ProcessTexel" ) >= 0 )
638
635
{
639
- code = LoadPrivateShaderLump (" shaders/scene/material_legacy_ptexel.glsl" );
636
+ code = SubstituteDefines ( LoadPrivateShaderLump (" shaders/scene/material_legacy_ptexel.glsl" ) );
640
637
}
641
638
else if (materialBlock.IndexOf (" Process" ) >= 0 )
642
639
{
643
- code = LoadPrivateShaderLump (" shaders/scene/material_legacy_process.glsl" );
640
+ code = SubstituteDefines ( LoadPrivateShaderLump (" shaders/scene/material_legacy_process.glsl" ) );
644
641
}
645
642
else
646
643
{
647
- code = LoadPrivateShaderLump (" shaders/scene/material_default.glsl" );
644
+ code = SubstituteDefines ( LoadPrivateShaderLump (" shaders/scene/material_default.glsl" ) );
648
645
}
649
646
code << " \n #line 1\n " ;
650
647
@@ -656,7 +653,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
656
653
657
654
definesBlock << " #define LEGACY_USER_SHADER\n " ;
658
655
659
- FString code = LoadPrivateShaderLump (" shaders/scene/material_legacy_pmaterial.glsl" );
656
+ FString code = SubstituteDefines ( LoadPrivateShaderLump (" shaders/scene/material_legacy_pmaterial.glsl" ) );
660
657
code << " \n #line 1\n " ;
661
658
662
659
materialBlock = code + materialBlock;
@@ -669,34 +666,33 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
669
666
670
667
if (light_lump_shared)
671
668
{
672
- lightBlock << LoadPrivateShaderLump (light_lump_shared).GetChars ();
669
+ lightBlock << SubstituteDefines ( LoadPrivateShaderLump (light_lump_shared) ).GetChars ();
673
670
}
674
671
675
- lightBlock << LoadPrivateShaderLump (light_lump).GetChars ();
672
+ lightBlock << SubstituteDefines ( LoadPrivateShaderLump (light_lump) ).GetChars ();
676
673
677
674
}
678
675
679
676
if (mateffect_lump && mateffectBlock.IsEmpty ())
680
677
{
681
678
mateffectname = mateffect_lump;
682
- mateffectBlock << LoadPrivateShaderLump (mateffect_lump).GetChars ();
679
+ mateffectBlock << SubstituteDefines ( LoadPrivateShaderLump (mateffect_lump) ).GetChars ();
683
680
}
684
681
685
682
return ShaderBuilder ()
686
- .Code (GLSLCompiler ()
683
+ .Code (CachedGLSLCompiler ()
687
684
.Type (ShaderType::Fragment)
688
685
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
689
686
.AddSource (" DefinesBlock" , definesBlock.GetChars ())
690
687
.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 ())
693
690
.AddSource (mateffectname.GetChars (), mateffectBlock.GetChars ())
694
691
.AddSource (materialname.GetChars (), materialBlock.GetChars ())
695
692
.AddSource (lightname.GetChars (), lightBlock.GetChars ())
696
693
.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))
700
696
.DebugName (shadername.GetChars ())
701
697
.Create (shadername.GetChars (), fb->GetDevice ());
702
698
}
@@ -725,43 +721,14 @@ FString VkShaderManager::GetVersionBlock()
725
721
return versionBlock;
726
722
}
727
723
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
-
752
724
FString VkShaderManager::LoadPublicShaderLump (const char * lumpname)
753
725
{
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 ;
758
727
}
759
728
760
729
FString VkShaderManager::LoadPrivateShaderLump (const char * lumpname)
761
730
{
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 ;
765
732
}
766
733
767
734
FString VkShaderManager::SubstituteDefines (FString str, bool isUberShader)
0 commit comments