From 80e1db07a7b388edb7ecf21267e25c3d87326e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 24 Oct 2022 17:41:41 +0200 Subject: [PATCH] Minor ShaderWriter refactor, prep for later changes --- Common/GPU/Shader.h | 1 + Common/GPU/ShaderWriter.cpp | 28 +++++++++++++++++--------- Common/GPU/ShaderWriter.h | 9 +++++++-- GPU/Common/DepalettizeShaderCommon.cpp | 4 ++-- GPU/Common/Draw2D.cpp | 2 +- GPU/Common/ReinterpretFramebuffer.cpp | 2 +- GPU/Common/StencilCommon.cpp | 2 +- GPU/Common/TextureShaderCommon.cpp | 4 ++-- 8 files changed, 34 insertions(+), 18 deletions(-) diff --git a/Common/GPU/Shader.h b/Common/GPU/Shader.h index 75e1a9b2e9..4020a7b253 100644 --- a/Common/GPU/Shader.h +++ b/Common/GPU/Shader.h @@ -92,6 +92,7 @@ struct UniformDef { }; struct SamplerDef { + int binding; // Might only be used by some backends. const char *name; // TODO: Might need unsigned samplers, 3d samplers, or other types in the future. }; diff --git a/Common/GPU/ShaderWriter.cpp b/Common/GPU/ShaderWriter.cpp index 5399c6241a..e00a468e35 100644 --- a/Common/GPU/ShaderWriter.cpp +++ b/Common/GPU/ShaderWriter.cpp @@ -456,34 +456,35 @@ void ShaderWriter::ConstFloat(const char *name, float value) { void ShaderWriter::DeclareSamplers(Slice samplers) { for (int i = 0; i < (int)samplers.size(); i++) { - DeclareTexture2D(samplers[i].name, i); - DeclareSampler2D(samplers[i].name, i); + DeclareTexture2D(samplers[i]); + DeclareSampler2D(samplers[i]); } + samplerDefs_ = samplers; } -void ShaderWriter::DeclareTexture2D(const char *name, int binding) { +void ShaderWriter::DeclareTexture2D(const SamplerDef &def) { switch (lang_.shaderLanguage) { case HLSL_D3D11: - F("Texture2D %s : register(t%d);\n", name, binding); + F("Texture2D %s : register(t%d);\n", def.name, def.binding); break; case HLSL_D3D9: - F("sampler %s: register(s%d);\n", name, binding); + F("sampler %s: register(s%d);\n", def.name, def.binding); break; case GLSL_VULKAN: // In the thin3d descriptor set layout, textures start at 1 in set 0. Hence the +1. - F("layout(set = 0, binding = %d) uniform sampler2D %s;\n", binding + 1, name); + F("layout(set = 0, binding = %d) uniform sampler2D %s;\n", def.binding + texBindingBase_, def.name); break; default: - F("uniform sampler2D %s;\n", name); + F("uniform sampler2D %s;\n", def.name); break; } } -void ShaderWriter::DeclareSampler2D(const char *name, int binding) { +void ShaderWriter::DeclareSampler2D(const SamplerDef &def) { // We only use separate samplers in HLSL D3D11, where we have no choice. switch (lang_.shaderLanguage) { case HLSL_D3D11: - F("SamplerState %sSamp : register(s%d);\n", name, binding); + F("SamplerState %sSamp : register(s%d);\n", def.name, def.binding); break; default: break; @@ -555,3 +556,12 @@ ShaderWriter &ShaderWriter::GetTextureSize(const char *szVariable, const char *t } return *this; } + +const SamplerDef *ShaderWriter::GetSamplerDef(const char *name) const { + for (int i = 0; i < (int)samplerDefs_.size(); i++) { + if (!strcmp(samplerDefs_[i].name, name)) { + return &samplerDefs_[i]; + } + } + return nullptr; +} diff --git a/Common/GPU/ShaderWriter.h b/Common/GPU/ShaderWriter.h index bcfc2f8263..6eb42719f0 100644 --- a/Common/GPU/ShaderWriter.h +++ b/Common/GPU/ShaderWriter.h @@ -75,10 +75,12 @@ public: void HighPrecisionFloat(); void LowPrecisionFloat(); + // NOTE: samplers must live for the rest of ShaderWriter's lifetime. No way to express that in C++ though :( void DeclareSamplers(Slice samplers); void ConstFloat(const char *name, float value); void SetFlags(ShaderWriterFlags flags) { flags_ |= flags; } + void SetTexBindingBase(int base) { texBindingBase_ = base; } ShaderWriter &SampleTexture2D(const char *texName, const char *uv); ShaderWriter &SampleTexture2DOffset(const char *texName, const char *uv, int offX, int offY); @@ -110,8 +112,9 @@ public: private: // Several of the shader languages ignore samplers, beware of that. - void DeclareSampler2D(const char *name, int binding); - void DeclareTexture2D(const char *name, int binding); + void DeclareSampler2D(const SamplerDef &def); + void DeclareTexture2D(const SamplerDef &def); + const SamplerDef *GetSamplerDef(const char *name) const; void Preamble(Slice extensions); @@ -119,4 +122,6 @@ private: const ShaderLanguageDesc &lang_; const ShaderStage stage_; ShaderWriterFlags flags_ = ShaderWriterFlags::NONE; + Slice samplerDefs_; + int texBindingBase_ = 1; }; diff --git a/GPU/Common/DepalettizeShaderCommon.cpp b/GPU/Common/DepalettizeShaderCommon.cpp index 7ff6c8b587..a784d22377 100644 --- a/GPU/Common/DepalettizeShaderCommon.cpp +++ b/GPU/Common/DepalettizeShaderCommon.cpp @@ -35,8 +35,8 @@ static const InputDef vsInputs[2] = { // TODO: Deduplicate with TextureShaderCommon.cpp static const SamplerDef samplers[2] = { - { "tex" }, - { "pal" }, + { 0, "tex" }, + { 1, "pal" }, }; static const VaryingDef varyings[1] = { diff --git a/GPU/Common/Draw2D.cpp b/GPU/Common/Draw2D.cpp index 93d542bafb..9b342e05bc 100644 --- a/GPU/Common/Draw2D.cpp +++ b/GPU/Common/Draw2D.cpp @@ -37,7 +37,7 @@ static const VaryingDef varyings[1] = { }; static const SamplerDef samplers[1] = { - { "tex" }, + { 0, "tex" }, }; const UniformDef g_draw2Duniforms[2] = { diff --git a/GPU/Common/ReinterpretFramebuffer.cpp b/GPU/Common/ReinterpretFramebuffer.cpp index ea80fa8b3d..b14e324d0d 100644 --- a/GPU/Common/ReinterpretFramebuffer.cpp +++ b/GPU/Common/ReinterpretFramebuffer.cpp @@ -14,7 +14,7 @@ static const VaryingDef varyings[1] = { }; static const SamplerDef samplers[1] = { - { "tex" } + { 0, "tex" } }; // Requires full size integer math. It would be possible to make a floating point-only version with lots of diff --git a/GPU/Common/StencilCommon.cpp b/GPU/Common/StencilCommon.cpp index c829e40f03..682d8a1f0c 100644 --- a/GPU/Common/StencilCommon.cpp +++ b/GPU/Common/StencilCommon.cpp @@ -80,7 +80,7 @@ static const VaryingDef varyings[1] = { }; static const SamplerDef samplers[1] = { - { "tex" }, + { 0, "tex" }, }; void GenerateStencilFs(char *buffer, const ShaderLanguageDesc &lang, const Draw::Bugs &bugs) { diff --git a/GPU/Common/TextureShaderCommon.cpp b/GPU/Common/TextureShaderCommon.cpp index 2860389fc2..5919d5442e 100644 --- a/GPU/Common/TextureShaderCommon.cpp +++ b/GPU/Common/TextureShaderCommon.cpp @@ -34,8 +34,8 @@ static const VaryingDef varyings[1] = { }; static const SamplerDef samplers[2] = { - { "tex" }, - { "pal" }, + { 0, "tex" }, + { 1, "pal" }, }; TextureShaderCache::TextureShaderCache(Draw::DrawContext *draw, Draw2D *draw2D) : draw_(draw), draw2D_(draw2D) { }