Add support for using #include in glsl files and improve glsl compile errors

This commit is contained in:
Magnus Norddahl 2023-03-22 00:06:13 +01:00 committed by Christoph Oelckers
parent 8682361194
commit a267cf737e
16 changed files with 159 additions and 80 deletions

View file

@ -275,30 +275,30 @@ FString RemoveLayoutLocationDecl(FString code, const char *inoutkeyword)
// Note: the MaterialShaderIndex enum in gl_shader.h needs to be updated whenever this array is modified.
const FDefaultShader defaultshaders[] =
{
{"Default", "shaders/glsl/func_normal.fp", "shaders/glsl/material_normal.fp", ""},
{"Warp 1", "shaders/glsl/func_warp1.fp", "shaders/glsl/material_normal.fp", ""},
{"Warp 2", "shaders/glsl/func_warp2.fp", "shaders/glsl/material_normal.fp", ""},
{"Specular", "shaders/glsl/func_spec.fp", "shaders/glsl/material_specular.fp", "#define SPECULAR\n#define NORMALMAP\n"},
{"PBR","shaders/glsl/func_pbr.fp", "shaders/glsl/material_pbr.fp", "#define PBR\n#define NORMALMAP\n"},
{"Paletted", "shaders/glsl/func_paletted.fp", "shaders/glsl/material_nolight.fp", "#define PALETTE_EMULATION\n"},
{"No Texture", "shaders/glsl/func_notexture.fp", "shaders/glsl/material_normal.fp", "#define NO_LAYERS\n"},
{"Basic Fuzz", "shaders/glsl/fuzz_standard.fp", "shaders/glsl/material_normal.fp", ""},
{"Smooth Fuzz", "shaders/glsl/fuzz_smooth.fp", "shaders/glsl/material_normal.fp", ""},
{"Swirly Fuzz", "shaders/glsl/fuzz_swirly.fp", "shaders/glsl/material_normal.fp", ""},
{"Translucent Fuzz", "shaders/glsl/fuzz_smoothtranslucent.fp", "shaders/glsl/material_normal.fp", ""},
{"Jagged Fuzz", "shaders/glsl/fuzz_jagged.fp", "shaders/glsl/material_normal.fp", ""},
{"Noise Fuzz", "shaders/glsl/fuzz_noise.fp", "shaders/glsl/material_normal.fp", ""},
{"Smooth Noise Fuzz", "shaders/glsl/fuzz_smoothnoise.fp", "shaders/glsl/material_normal.fp", ""},
{"Software Fuzz", "shaders/glsl/fuzz_software.fp", "shaders/glsl/material_normal.fp", ""},
{"Default", "shaders/scene/material_default.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Warp 1", "shaders/scene/material_default.glsl", /*"shaders/scene/mateffect_warp1.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Warp 2", "shaders/scene/material_default.glsl", /*"shaders/scene/mateffect_warp2.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Specular", "shaders/scene/material_spec.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_specular.glsl", "#define SPECULAR\n#define NORMALMAP\n"},
{"PBR", "shaders/scene/material_pbr.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_pbr.glsl", "#define PBR\n#define NORMALMAP\n"},
{"Paletted", "shaders/scene/material_paletted.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_nolights.glsl", "#define PALETTE_EMULATION\n"},
{"No Texture", "shaders/scene/material_notexture.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", "#define NO_LAYERS\n"},
{"Basic Fuzz", "shaders/scene/material_fuzz_standard.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Smooth Fuzz", "shaders/scene/material_fuzz_smooth.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Swirly Fuzz", "shaders/scene/material_fuzz_swirly.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Translucent Fuzz", "shaders/scene/material_fuzz_smoothtranslucent.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Jagged Fuzz", "shaders/scene/material_fuzz_jagged.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Noise Fuzz", "shaders/scene/material_fuzz_noise.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Smooth Noise Fuzz", "shaders/scene/material_fuzz_smoothnoise.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{"Software Fuzz", "shaders/scene/material_fuzz_software.glsl", /*"shaders/scene/mateffect_default.glsl",*/ "shaders/scene/lightmodel_normal.glsl", ""},
{nullptr,nullptr,nullptr,nullptr}
};
const FEffectShader effectshaders[] =
{
{ "fogboundary", "shaders/glsl/main.vp", "shaders/glsl/fogboundary.fp", nullptr, nullptr, "#define NO_ALPHATEST\n" },
{ "spheremap", "shaders/glsl/main.vp", "shaders/glsl/main.fp", "shaders/glsl/func_normal.fp", "shaders/glsl/material_normal.fp", "#define SPHEREMAP\n#define NO_ALPHATEST\n" },
{ "burn", "shaders/glsl/main.vp", "shaders/glsl/burn.fp", nullptr, nullptr, "#define SIMPLE\n#define NO_ALPHATEST\n" },
{ "stencil", "shaders/glsl/main.vp", "shaders/glsl/stencil.fp", nullptr, nullptr, "#define SIMPLE\n#define NO_ALPHATEST\n" },
{ "fogboundary", "shaders/scene/vert_main.glsl", "shaders/scene/frag_fogboundary.glsl", nullptr, nullptr, "#define NO_ALPHATEST\n" },
{ "spheremap", "shaders/scene/vert_main.glsl", "shaders/scene/frag_surface.glsl", "shaders/scene/material_default.glsl", "shaders/scene/lightmodel_normal.glsl", "#define SPHEREMAP\n#define NO_ALPHATEST\n" },
{ "burn", "shaders/scene/vert_main.glsl", "shaders/scene/frag_burn.glsl", nullptr, nullptr, "#define SIMPLE\n#define NO_ALPHATEST\n" },
{ "stencil", "shaders/scene/vert_main.glsl", "shaders/scene/frag_stencil.glsl", nullptr, nullptr, "#define SIMPLE\n#define NO_ALPHATEST\n" },
};
int DFrameBuffer::GetShaderCount()

View file

@ -32,8 +32,8 @@
bool VkShaderManager::CompileNextShader()
{
const char *mainvp = "shaders/glsl/vert_scene.vp";
const char *mainfp = "shaders/glsl/frag_surface.fp";
const char *mainvp = "shaders/scene/vert_main.glsl";
const char *mainfp = "shaders/scene/frag_surface.glsl";
int i = compileIndex;
if (compileState == 0)
@ -155,127 +155,189 @@ VkShaderProgram *VkShaderManager::Get(unsigned int eff, bool alphateston, EPassT
std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader(FString shadername, const char *vert_lump, const char *defines)
{
FString code = GetTargetGlslVersion();
code << defines;
code << "\n#define MAX_STREAM_DATA " << std::to_string(MAX_STREAM_DATA).c_str() << "\n";
FString definesBlock;
definesBlock << defines << "\n";
definesBlock << "#define MAX_STREAM_DATA " << std::to_string(MAX_STREAM_DATA).c_str() << "\n";
#ifdef NPOT_EMULATION
code << "#define NPOT_EMULATION\n";
definesBlock << "#define NPOT_EMULATION\n";
#endif
code << LoadPrivateShaderLump("shaders/scene/layout_shared.glsl");
if (!fb->device->EnabledFeatures.Features.shaderClipDistance) code << "#define NO_CLIPDISTANCE_SUPPORT\n";
code << "#line 1\n";
code << LoadPrivateShaderLump(vert_lump).GetChars() << "\n";
if (!fb->device->EnabledFeatures.Features.shaderClipDistance)
{
definesBlock << "#define NO_CLIPDISTANCE_SUPPORT\n";
}
FString layoutBlock;
layoutBlock << LoadPrivateShaderLump("shaders/scene/layout_shared.glsl").GetChars() << "\n";
layoutBlock << LoadPrivateShaderLump("shaders/scene/layout_vert.glsl").GetChars() << "\n";
FString codeBlock;
codeBlock << LoadPrivateShaderLump(vert_lump).GetChars() << "\n";
return ShaderBuilder()
.Type(ShaderType::Vertex)
.AddSource(shadername.GetChars(), code.GetChars())
.DebugName(shadername.GetChars())
.AddSource("VersionBlock", GetVersionBlock().GetChars())
.AddSource("DefinesBlock", definesBlock.GetChars())
.AddSource("LayoutBlock", layoutBlock.GetChars())
.AddSource(vert_lump, codeBlock.GetChars())
.OnIncludeLocal([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, false); })
.OnIncludeSystem([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, true); })
.Create(shadername.GetChars(), fb->device.get());
}
std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername, const char *frag_lump, const char *material_lump, const char *light_lump, const char *defines, bool alphatest, bool gbufferpass)
{
FString code = GetTargetGlslVersion();
if (fb->device->SupportsExtension(VK_KHR_RAY_QUERY_EXTENSION_NAME))
code << "\n#define SUPPORTS_RAYTRACING\n";
code << defines;
code << "\n$placeholder$"; // here the code can later add more needed #defines.
code << "\n#define MAX_STREAM_DATA " << std::to_string(MAX_STREAM_DATA).c_str() << "\n";
FString definesBlock;
definesBlock << defines << "\n";
if (fb->device->SupportsExtension(VK_KHR_RAY_QUERY_EXTENSION_NAME)) definesBlock << "\n#define SUPPORTS_RAYTRACING\n";
definesBlock << defines;
definesBlock << "\n#define MAX_STREAM_DATA " << std::to_string(MAX_STREAM_DATA).c_str() << "\n";
#ifdef NPOT_EMULATION
code << "#define NPOT_EMULATION\n";
definesBlock << "#define NPOT_EMULATION\n";
#endif
code << LoadPrivateShaderLump("shaders/scene/layout_shared.glsl");
FString placeholder = "\n";
if (!fb->device->EnabledFeatures.Features.shaderClipDistance) definesBlock << "#define NO_CLIPDISTANCE_SUPPORT\n";
if (!alphatest) definesBlock << "#define NO_ALPHATEST\n";
if (gbufferpass) definesBlock << "#define GBUFFER_PASS\n";
if (!fb->device->EnabledFeatures.Features.shaderClipDistance) code << "#define NO_CLIPDISTANCE_SUPPORT\n";
if (!alphatest) code << "#define NO_ALPHATEST\n";
if (gbufferpass) code << "#define GBUFFER_PASS\n";
FString layoutBlock;
layoutBlock << LoadPrivateShaderLump("shaders/scene/layout_shared.glsl").GetChars() << "\n";
layoutBlock << LoadPrivateShaderLump("shaders/scene/layout_frag.glsl").GetChars() << "\n";
code << "\n#line 1\n";
code << LoadPrivateShaderLump(frag_lump).GetChars() << "\n";
FString codeBlock;
codeBlock << LoadPrivateShaderLump(frag_lump).GetChars() << "\n";
FString materialname = "MaterialBlock";
FString materialBlock;
if (material_lump)
{
if (material_lump[0] != '#')
{
materialname = material_lump;
FString pp_code = LoadPublicShaderLump(material_lump);
if (pp_code.IndexOf("ProcessMaterial") < 0 && pp_code.IndexOf("SetupMaterial") < 0)
{
// this looks like an old custom hardware shader.
// add ProcessMaterial function that calls the older ProcessTexel function
if (pp_code.IndexOf("GetTexCoord") >= 0)
{
code << "\n" << LoadPrivateShaderLump("shaders/glsl/func_defaultmat2.fp").GetChars() << "\n";
materialBlock << LoadPrivateShaderLump("shaders/scene/material_default.glsl").GetChars() << "\n";
}
else
{
code << "\n" << LoadPrivateShaderLump("shaders/glsl/func_defaultmat.fp").GetChars() << "\n";
if (pp_code.IndexOf("ProcessTexel") < 0)
{
// this looks like an even older custom hardware shader.
// We need to replace the ProcessTexel call to make it work.
code.Substitute("material.Base = ProcessTexel();", "material.Base = Process(vec4(1.0));");
materialBlock << LoadPrivateShaderLump("shaders/scene/material_legacy_process.glsl").GetChars() << "\n";
}
else
{
materialBlock << LoadPrivateShaderLump("shaders/scene/material_legacy_ptexel.glsl").GetChars() << "\n";
}
}
if (pp_code.IndexOf("ProcessLight") >= 0)
{
// The ProcessLight signatured changed. Forward to the old one.
code << "\nvec4 ProcessLight(vec4 color);\n";
code << "\nvec4 ProcessLight(Material material, vec4 color) { return ProcessLight(color); }\n";
materialBlock << "\nvec4 ProcessLight(vec4 color);\n";
materialBlock << "\nvec4 ProcessLight(Material material, vec4 color) { return ProcessLight(color); }\n";
}
}
code << "\n#line 1\n";
code << RemoveLegacyUserUniforms(pp_code).GetChars();
code.Substitute("gl_TexCoord[0]", "vTexCoord"); // fix old custom shaders.
materialBlock << "\n#line 1\n";
materialBlock << RemoveLegacyUserUniforms(pp_code).GetChars();
materialBlock.Substitute("gl_TexCoord[0]", "vTexCoord"); // fix old custom shaders.
if (pp_code.IndexOf("ProcessLight") < 0)
{
code << "\n" << LoadPrivateShaderLump("shaders/glsl/func_defaultlight.fp").GetChars() << "\n";
materialBlock << "\n" << LoadPrivateShaderLump("shaders/scene/lighteffect_default.glsl").GetChars() << "\n";
}
// ProcessMaterial must be considered broken because it requires the user to fill in data they possibly cannot know all about.
if (pp_code.IndexOf("ProcessMaterial") >= 0 && pp_code.IndexOf("SetupMaterial") < 0)
{
// This reactivates the old logic and disables all features that cannot be supported with that method.
placeholder << "#define LEGACY_USER_SHADER\n";
definesBlock << "#define LEGACY_USER_SHADER\n";
}
}
else
{
// material_lump is not a lump name but the source itself (from generated shaders)
code << (material_lump + 1) << "\n";
materialBlock << (material_lump + 1) << "\n";
}
}
code.Substitute("$placeholder$", placeholder);
FString lightname = "LightBlock";
FString lightBlock;
if (light_lump)
{
code << "\n#line 1\n";
code << LoadPrivateShaderLump(light_lump).GetChars();
lightname = light_lump;
lightBlock << LoadPrivateShaderLump(light_lump).GetChars();
}
return ShaderBuilder()
.Type(ShaderType::Fragment)
.AddSource(shadername.GetChars(), code.GetChars())
.DebugName(shadername.GetChars())
.AddSource("VersionBlock", GetVersionBlock().GetChars())
.AddSource("DefinesBlock", definesBlock.GetChars())
.AddSource("LayoutBlock", layoutBlock.GetChars())
.AddSource("shaders/scene/includes.glsl", LoadPrivateShaderLump("shaders/scene/includes.glsl").GetChars())
.AddSource("shaders/scene/mateffect_default.glsl", LoadPrivateShaderLump("shaders/scene/mateffect_default.glsl").GetChars())
//.AddSource("shaders/scene/lighteffect_default.glsl", LoadPrivateShaderLump("shaders/scene/lighteffect_default.glsl").GetChars())
.AddSource(materialname.GetChars(), materialBlock.GetChars())
.AddSource(lightname.GetChars(), lightBlock.GetChars())
.AddSource(frag_lump, codeBlock.GetChars())
.OnIncludeLocal([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, false); })
.OnIncludeSystem([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, true); })
.Create(shadername.GetChars(), fb->device.get());
}
FString VkShaderManager::GetTargetGlslVersion()
FString VkShaderManager::GetVersionBlock()
{
if (fb->device->Instance->ApiVersion == VK_API_VERSION_1_2)
FString versionBlock;
if (fb->device->Instance->ApiVersion >= VK_API_VERSION_1_2)
{
return "#version 460\n#extension GL_EXT_ray_query : enable\n";
versionBlock << "#version 460 core\n";
}
else
{
return "#version 450 core\n";
versionBlock << "#version 450 core\n";
}
versionBlock << "#extension GL_GOOGLE_include_directive : enable\n";
if (fb->device->SupportsExtension(VK_KHR_RAY_QUERY_EXTENSION_NAME))
{
versionBlock << "#extension GL_EXT_ray_query : enable\n";
}
return versionBlock;
}
ShaderIncludeResult VkShaderManager::OnInclude(FString headerName, FString includerName, size_t depth, bool system)
{
if (depth > 8)
I_Error("Too much include recursion!");
FString includeguardname;
includeguardname << "_HEADERGUARD_" << headerName.GetChars();
includeguardname.ReplaceChars("/\\.", '_');
FString code;
code << "#ifndef " << includeguardname.GetChars() << "\n";
code << "#define " << includeguardname.GetChars() << "\n";
code << "#line 1\n";
if (system)
code << LoadPrivateShaderLump(headerName.GetChars()).GetChars() << "\n";
else
code << LoadPublicShaderLump(headerName.GetChars()).GetChars() << "\n";
code << "#endif\n";
return ShaderIncludeResult(headerName.GetChars(), code.GetChars());
}
FString VkShaderManager::LoadPublicShaderLump(const char *lumpname)

View file

@ -11,6 +11,7 @@
#define SHADER_MIN_REQUIRED_TEXTURE_LAYERS 11
class ShaderIncludeResult;
class VulkanRenderDevice;
class VulkanDevice;
class VulkanShader;
@ -85,7 +86,9 @@ private:
std::unique_ptr<VulkanShader> LoadVertShader(FString shadername, const char *vert_lump, const char *defines);
std::unique_ptr<VulkanShader> LoadFragShader(FString shadername, const char *frag_lump, const char *material_lump, const char *light_lump, const char *defines, bool alphatest, bool gbufferpass);
FString GetTargetGlslVersion();
ShaderIncludeResult OnInclude(FString headerName, FString includerName, size_t depth, bool system);
FString GetVersionBlock();
FString LoadPublicShaderLump(const char *lumpname);
FString LoadPrivateShaderLump(const char *lumpname);

View file

@ -1,5 +1,5 @@
#include "grayscale.glsl"
#include "shaders/scene/grayscale.glsl"
// Desaturate a color

View file

@ -1,6 +1,5 @@
#include "layout_frag_scene.glsl"
#include "material.glsl"
#include "shaders/scene/material.glsl"
void main()
{

View file

@ -1,6 +1,5 @@
#include "layout_frag_scene.glsl"
#include "material.glsl"
#include "shaders/scene/material.glsl"
void main()
{

View file

@ -1,6 +1,5 @@
#include "layout_frag_scene.glsl"
#include "material.glsl"
#include "shaders/scene/material.glsl"
void main()
{

View file

@ -1,6 +1,5 @@
#include "layout_frag_scene.glsl"
#include "material.glsl"
#include "shaders/scene/material.glsl"
void main()
{

View file

@ -0,0 +1,5 @@
#include "shaders/scene/material.glsl"
#include "shaders/scene/lightmodel.glsl"
#include "shaders/scene/light_shadow.glsl"
#include "shaders/scene/light_spot.glsl"

View file

@ -1,6 +1,6 @@
#include "lightmodel_software.glsl"
#include "material.glsl"
#include "shaders/scene/lightmodel_software.glsl"
#include "shaders/scene/material.glsl"
vec4 ProcessLight(Material mat, vec4 color);

View file

@ -1,4 +1,7 @@
#include "shaders/scene/material_getTexel.glsl"
#include "shaders/scene/material_normalmap.glsl"
struct Material
{
vec4 Base;

View file

@ -1,5 +1,5 @@
#include "desaturate.glsl"
#include "shaders/scene/desaturate.glsl"
vec4 ApplyTextureManipulation(vec4 texel, int blendflags);
@ -26,6 +26,7 @@ vec4 getTexel(vec2 st)
}
#elif (uTextureMode & 0xffff) == 6 // TM_OPAQUEINVERSE
texel = vec4(1.0-texel.r, 1.0-texel.b, 1.0-texel.g, 1.0);
#endif
#if (uTextureMode & 0x80000) == 0x80000 // TEXF_ClampY
if (st.t < 0.0 || st.t > 1.0)

View file

@ -0,0 +1,9 @@
vec4 Process(vec4);
void SetupMaterial(inout Material material)
{
material.Base = Process(vec4(1.0));
material.Normal = ApplyNormalMap(vTexCoord.st);
material.Bright = texture(brighttexture, vTexCoord.st);
}

View file

@ -1,5 +1,5 @@
#include "bones.glsl"
#include "shaders/scene/bones.glsl"
void main()
{