Unify text shader handling

This commit is contained in:
Georg Hagen
2025-01-04 02:46:24 +01:00
parent bc78af70e8
commit 94025c79c6
7 changed files with 60 additions and 51 deletions

View File

@@ -62,7 +62,6 @@ namespace OpenVulkano
texts.push_back(std::make_pair("\u0410\u0411\u0412\u041F", TextConfig()));
texts.push_back(std::make_pair("Unsupported glyphs \u1E30\u1E31 are coming", TextConfig()));
texts.push_back(std::make_pair("This is first line\nSecond gg line\nThird G line", TextConfig()));
texts[0].second.applyBorder = true;
texts[1].second.backgroundColor.a = 1;
const int N = texts.size();
@@ -99,31 +98,26 @@ namespace OpenVulkano
if (i < texts.size())
{
t = new TextDrawable(m_atlasGenerator.GetAtlasData(), texts[textIdx].second);
t->SetShader(&TextDrawable::GetSdfDefaultShader());
}
else
{
t = new TextDrawable(m_msdfAtlasGenerator.GetAtlasData(), texts[textIdx].second);
t->SetShader(&TextDrawable::GetMsdfDefaultShader());
}
#else
int xOffset = 0;
if (i < N)
{
t = new TextDrawable(sdfMetadataInfo, texts[textIdx].second);
t->SetShader(&TextDrawable::GetSdfDefaultShader());
xOffset = -5;
}
else if (i >= N && i < N * 2)
{
t = new TextDrawable(msdfMetadataInfo, texts[textIdx].second);
t->SetShader(&TextDrawable::GetMsdfDefaultShader());
xOffset = 15;
}
else
{
t = new TextDrawable(bitmapMetadataInfo, texts[textIdx].second);
t->SetShader(&TextDrawable::GetBitmapDefaultShader());
xOffset = 35;
}
// OR use separate texture + metadata file

View File

@@ -38,11 +38,11 @@ namespace OpenVulkano::Scene
shader.cullMode = CullMode::NONE;
return shader;
}
}
Shader TextDrawable::DEFAULT_SHADER_BITMAP = MakeDefaultShader(FontAtlasType::BITMAP);
Shader TextDrawable::DEFAULT_SHADER_SDF = MakeDefaultShader(FontAtlasType::SDF);
Shader TextDrawable::DEFAULT_SHADER_MSDF = MakeDefaultShader(FontAtlasType::MSDF);
Shader DEFAULT_SHADER_BITMAP = MakeDefaultShader(FontAtlasType::BITMAP);
Shader DEFAULT_SHADER_SDF = MakeDefaultShader(FontAtlasType::SDF);
Shader DEFAULT_SHADER_MSDF = MakeDefaultShader(FontAtlasType::MSDF);
}
TextDrawable::TextDrawable(const TextConfig& config) : m_cfg(config)
{
@@ -215,7 +215,17 @@ namespace OpenVulkano::Scene
++i;
}
m_bbox.Init(bmin, bmax);
SimpleDrawable::Init(nullptr, &m_geometry, &m_material, &m_uniBuffer);
Shader* shader = nullptr;
switch (m_atlasData->meta.atlasType)
{
case FontAtlasType::SDF: shader = &DEFAULT_SHADER_SDF; break;
case FontAtlasType::MSDF: shader = &DEFAULT_SHADER_MSDF; break;
default: Logger::RENDER->warn("No default shader for atlas type: {}", m_atlasData->meta.atlasType.GetName());
case FontAtlasType::BITMAP: shader = &DEFAULT_SHADER_BITMAP; break;
}
SimpleDrawable::Init(shader, &m_geometry, &m_material, &m_uniBuffer);
}
void TextDrawable::SetAtlasData(const std::shared_ptr<AtlasData>& atlasData)

View File

@@ -21,19 +21,13 @@ namespace OpenVulkano::Scene
struct TextConfig
{
Math::Vector4f textColor = { 1, 1, 1, 1 };
Math::Vector4f borderColor = { 1, 0, 0, 1 };
Math::Vector4f backgroundColor = { 0, 1, 0, 0 };
float threshold = 0.4f;
float borderSize = 0.05f;
float smoothing = 1.f/32.f;
uint32_t applyBorder = false;
//bool sdfMultiChannel = false;
};
class TextDrawable : public SimpleDrawable
{
static Shader DEFAULT_SHADER_BITMAP, DEFAULT_SHADER_SDF, DEFAULT_SHADER_MSDF;
Geometry m_geometry;
Material m_material;
UniformBuffer m_uniBuffer;
@@ -45,9 +39,6 @@ namespace OpenVulkano::Scene
uint32_t GetFallbackGlyph() const;
public:
[[nodiscard]] static Shader& GetSdfDefaultShader() { return DEFAULT_SHADER_SDF; }
[[nodiscard]] static Shader& GetMsdfDefaultShader() { return DEFAULT_SHADER_MSDF; }
[[nodiscard]] static Shader& GetBitmapDefaultShader() { return DEFAULT_SHADER_BITMAP; }
TextDrawable(const TextConfig& config = TextConfig());
TextDrawable(const Array<char>& atlasMetadata, const TextConfig& config = TextConfig());
TextDrawable(const std::string& atlasMetadataFile, const TextConfig& config = TextConfig());

View File

@@ -9,12 +9,9 @@ layout(set = 2, binding = 0) uniform sampler2D texSampler;
layout(set = 3, binding = 0) uniform TextConfig
{
vec4 textColor;
vec4 borderColor;
vec4 backgroundColor;
float threshold;
float borderSize;
float smoothing;
bool applyBorder;
} textConfig;
float median(float r, float g, float b) {

View File

@@ -9,28 +9,16 @@ layout(set = 2, binding = 0) uniform sampler2D texSampler;
layout(set = 3, binding = 0) uniform TextConfig
{
vec4 textColor;
vec4 borderColor;
vec4 backgroundColor;
float threshold;
float borderSize;
float smoothing;
bool applyBorder;
} textConfig;
void main()
{
float distance = texture(texSampler, texCoord).r;
float alpha = smoothstep(textConfig.threshold - textConfig.smoothing, textConfig.threshold + textConfig.smoothing, distance);
if (textConfig.applyBorder)
{
float border = smoothstep(textConfig.threshold + textConfig.borderSize - textConfig.smoothing,
textConfig.threshold + textConfig.borderSize + textConfig.smoothing, distance);
outColor = mix(textConfig.borderColor, textConfig.textColor, border) * alpha;
}
else
{
outColor = vec4(textConfig.textColor) * alpha;
}
if (textConfig.backgroundColor.a != 0)
{

View File

@@ -0,0 +1,32 @@
#version 450
layout(location = 1) in vec2 texCoord;
layout(location = 0) out vec4 outColor;
layout(set = 2, binding = 0) uniform sampler2D texSampler;
layout(set = 3, binding = 0) uniform TextConfig
{
vec4 textColor;
vec4 borderColor;
vec4 backgroundColor;
float threshold;
float borderSize;
float smoothing;
} textConfig;
void main()
{
float distance = texture(texSampler, texCoord).r;
float alpha = smoothstep(textConfig.threshold - textConfig.smoothing, textConfig.threshold + textConfig.smoothing, distance);
float border = smoothstep(textConfig.threshold + textConfig.borderSize - textConfig.smoothing,
textConfig.threshold + textConfig.borderSize + textConfig.smoothing, distance);
outColor = mix(textConfig.borderColor, textConfig.textColor, border) * alpha;
if (textConfig.backgroundColor.a != 0)
{
outColor = mix(textConfig.backgroundColor, outColor, alpha);
}
}

View File

@@ -1,6 +1,6 @@
#version 450
layout(location = 0) in vec4 color;
//layout(location = 0) in vec4 color;
layout(location = 1) in vec2 texCoord;
layout(location = 0) out vec4 outColor;
@@ -10,12 +10,9 @@ layout(set = 2, binding = 0) uniform sampler2D texSampler;
layout(set = 3, binding = 0) uniform TextConfig
{
vec4 textColor;
vec4 borderColor;
vec4 backgroundColor;
float threshold;
float borderSize;
float smoothing;
bool applyBorder;
} textConfig;
void main()