From 09efe2d6a04ed92557ef83aa15e316c860257e40 Mon Sep 17 00:00:00 2001 From: Aiden Date: Thu, 21 May 2026 15:08:36 +1000 Subject: [PATCH] Changed defaults --- src/render/runtime/RuntimeShaderParams.cpp | 4 ++++ src/render/runtime/RuntimeShaderRenderer.cpp | 4 ++-- .../runtime/RuntimeTextTextureCache.cpp | 20 +++++++++++++++---- src/render/runtime/RuntimeTextTextureCache.h | 5 +++-- src/runtime/RuntimeShaderArtifact.h | 1 + src/shader/ShaderCompiler.cpp | 5 ++++- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/render/runtime/RuntimeShaderParams.cpp b/src/render/runtime/RuntimeShaderParams.cpp index 622153a..b88eabc 100644 --- a/src/render/runtime/RuntimeShaderParams.cpp +++ b/src/render/runtime/RuntimeShaderParams.cpp @@ -110,7 +110,11 @@ std::vector BuildRuntimeShaderGlobalParamsStd140( AppendStd140Int(buffer, EnumIndexForDefault(definition, value)); break; case ShaderParameterType::Text: + { + const auto scaleIt = artifact.textTextureWidthScales.find(definition.id); + AppendStd140Float(buffer, scaleIt == artifact.textTextureWidthScales.end() ? 1.0f : scaleIt->second); break; + } case ShaderParameterType::Trigger: AppendStd140Int(buffer, value.numberValues.empty() ? 0 : static_cast(value.numberValues[0])); AppendStd140Float(buffer, value.numberValues.size() > 1 ? static_cast(value.numberValues[1]) : -1000000.0f); diff --git a/src/render/runtime/RuntimeShaderRenderer.cpp b/src/render/runtime/RuntimeShaderRenderer.cpp index ccb16cc..5fa8203 100644 --- a/src/render/runtime/RuntimeShaderRenderer.cpp +++ b/src/render/runtime/RuntimeShaderRenderer.cpp @@ -61,7 +61,7 @@ bool RuntimeShaderRenderer::CommitPreparedProgram(RuntimePreparedShaderProgram& DestroyProgram(); return false; } - mTextTextures.RefreshTextTextures(); + mTextTextures.RefreshTextTextures(&mArtifact); return true; } @@ -71,7 +71,7 @@ void RuntimeShaderRenderer::UpdateArtifactState(const RuntimeShaderArtifact& art mArtifact.parameterValues = artifact.parameterValues; mArtifact.message = artifact.message; mTextTextures.UpdateArtifactState(artifact); - mTextTextures.RefreshTextTextures(); + mTextTextures.RefreshTextTextures(&mArtifact); } bool RuntimeShaderRenderer::BuildPreparedProgram( diff --git a/src/render/runtime/RuntimeTextTextureCache.cpp b/src/render/runtime/RuntimeTextTextureCache.cpp index 8557e09..cce3603 100644 --- a/src/render/runtime/RuntimeTextTextureCache.cpp +++ b/src/render/runtime/RuntimeTextTextureCache.cpp @@ -102,11 +102,20 @@ void RuntimeTextTextureCache::UpdateArtifactState(const RuntimeShaderArtifact& a mArtifact.parameterValues = artifact.parameterValues; } -void RuntimeTextTextureCache::RefreshTextTextures() +void RuntimeTextTextureCache::RefreshTextTextures(RuntimeShaderArtifact* artifactState) { + if (artifactState) + artifactState->textTextureWidthScales.clear(); for (TextTexture& textTexture : mTextTextures) { EnsureTextTexture(textTexture); + if (artifactState) + { + const float scale = textTexture.width == 0 + ? 1.0f + : static_cast(textTexture.liveWidth) / static_cast(textTexture.width); + artifactState->textTextureWidthScales[textTexture.parameterId] = scale; + } } } @@ -326,7 +335,8 @@ bool RuntimeTextTextureCache::EnsureTextTexture(TextTexture& texture) unsigned width = 0; unsigned height = 0; - std::vector pixels = ComposeTextMask(*atlas, texture, text, width, height); + unsigned liveWidth = 1; + std::vector pixels = ComposeTextMask(*atlas, texture, text, width, height, liveWidth); if (pixels.empty() || width == 0 || height == 0) return false; @@ -356,10 +366,11 @@ bool RuntimeTextTextureCache::EnsureTextTexture(TextTexture& texture) texture.cachedText = text; texture.width = width; texture.height = height; + texture.liveWidth = liveWidth; return texture.texture != 0; } -std::vector RuntimeTextTextureCache::ComposeTextMask(const Atlas& atlas, const TextTexture& texture, const std::string& text, unsigned& width, unsigned& height) const +std::vector RuntimeTextTextureCache::ComposeTextMask(const Atlas& atlas, const TextTexture& texture, const std::string& text, unsigned& width, unsigned& height, unsigned& liveWidth) const { double advance = 0.0; for (unsigned char character : text) @@ -370,7 +381,8 @@ std::vector RuntimeTextTextureCache::ComposeTextMask(const Atlas& } const unsigned fixedWidth = static_cast(std::ceil(static_cast(texture.maxLength) * kFontPixelsPerEm * 0.9)) + kTextTexturePadding * 2u; - width = (std::max)(fixedWidth, static_cast(std::ceil(advance * kFontPixelsPerEm)) + kTextTexturePadding * 2u); + liveWidth = (std::max)(1u, static_cast(std::ceil(advance * kFontPixelsPerEm)) + kTextTexturePadding * 2u); + width = (std::max)(fixedWidth, liveWidth); height = kTextTextureHeight; std::vector mask(static_cast(width) * height, 0); diff --git a/src/render/runtime/RuntimeTextTextureCache.h b/src/render/runtime/RuntimeTextTextureCache.h index 446f5fd..e1b75ae 100644 --- a/src/render/runtime/RuntimeTextTextureCache.h +++ b/src/render/runtime/RuntimeTextTextureCache.h @@ -17,7 +17,7 @@ public: bool Configure(const RuntimeShaderArtifact& artifact, std::string& error); void UpdateArtifactState(const RuntimeShaderArtifact& artifact); - void RefreshTextTextures(); + void RefreshTextTextures(RuntimeShaderArtifact* artifactState = nullptr); void BindTextTextures(GLuint program); void ShutdownGl(); @@ -58,6 +58,7 @@ private: GLuint texture = 0; unsigned width = 0; unsigned height = 0; + unsigned liveWidth = 1; unsigned maxLength = 64; }; @@ -65,7 +66,7 @@ private: bool LoadAtlasJson(const RenderCadenceCompositor::FontAtlasBuildOutput& output, Atlas& atlas, std::string& error) const; bool LoadAtlasImage(const RenderCadenceCompositor::FontAtlasBuildOutput& output, Atlas& atlas, std::string& error) const; bool EnsureTextTexture(TextTexture& texture); - std::vector ComposeTextMask(const Atlas& atlas, const TextTexture& texture, const std::string& text, unsigned& width, unsigned& height) const; + std::vector ComposeTextMask(const Atlas& atlas, const TextTexture& texture, const std::string& text, unsigned& width, unsigned& height, unsigned& liveWidth) const; const Atlas* FindAtlas(const std::string& fontId) const; static const ShaderParameterValue* FindParameterValue(const RuntimeShaderArtifact& artifact, const std::string& parameterId); static std::string DefaultTextValue(const RuntimeShaderArtifact& artifact, const std::string& parameterId); diff --git a/src/runtime/RuntimeShaderArtifact.h b/src/runtime/RuntimeShaderArtifact.h index 81a8d10..65a8bb2 100644 --- a/src/runtime/RuntimeShaderArtifact.h +++ b/src/runtime/RuntimeShaderArtifact.h @@ -25,5 +25,6 @@ struct RuntimeShaderArtifact std::string message; std::vector parameterDefinitions; std::map parameterValues; + std::map textTextureWidthScales; std::vector fontAtlases; }; diff --git a/src/shader/ShaderCompiler.cpp b/src/shader/ShaderCompiler.cpp index 2314db9..4152b34 100644 --- a/src/shader/ShaderCompiler.cpp +++ b/src/shader/ShaderCompiler.cpp @@ -52,7 +52,10 @@ std::string BuildParameterUniforms(const std::vector& for (const ShaderParameterDefinition& definition : parameters) { if (definition.type == ShaderParameterType::Text) + { + source << "\tfloat " << definition.id << "TextureWidthScale;\n"; continue; + } if (definition.type == ShaderParameterType::Trigger) { source << "\tint " << definition.id << ";\n"; @@ -109,7 +112,7 @@ std::string BuildTextHelpers(const std::vector& param << "{\n" << "\tif (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)\n" << "\t\treturn 0.0;\n" - << "\treturn " << definition.id << "Texture.Sample(uv).r;\n" + << "\treturn " << definition.id << "Texture.Sample(float2(uv.x * " << definition.id << "TextureWidthScale, uv.y)).r;\n" << "}\n\n" << "float4 draw" << suffix << "(float2 uv, float4 fillColor)\n" << "{\n"