Merge pull request #16283 from hrydgard/more-shaderwriter-refactor

Minor ShaderWriter refactor, prep for later changes
This commit is contained in:
Unknown W. Brackets 2022-10-24 10:03:31 -07:00 committed by GitHub
commit 91886be119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 18 deletions

View File

@ -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.
};

View File

@ -456,34 +456,35 @@ void ShaderWriter::ConstFloat(const char *name, float value) {
void ShaderWriter::DeclareSamplers(Slice<SamplerDef> 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<float4> %s : register(t%d);\n", name, binding);
F("Texture2D<float4> %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;
}

View File

@ -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<SamplerDef> 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<const char *> extensions);
@ -119,4 +122,6 @@ private:
const ShaderLanguageDesc &lang_;
const ShaderStage stage_;
ShaderWriterFlags flags_ = ShaderWriterFlags::NONE;
Slice<SamplerDef> samplerDefs_;
int texBindingBase_ = 1;
};

View File

@ -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] = {

View File

@ -37,7 +37,7 @@ static const VaryingDef varyings[1] = {
};
static const SamplerDef samplers[1] = {
{ "tex" },
{ 0, "tex" },
};
const UniformDef g_draw2Duniforms[2] = {

View File

@ -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

View File

@ -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) {

View File

@ -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) { }