A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(render): SDF batch-shader variant + variant-program cache (REARCH_GUI P1.2)
esenginebe10aad0fcfa
diff --git a/src/esengine/data/shaders/batch.esshader b/src/esengine/data/shaders/batch.esshaderindex 8fde7644c..3055d8121 100644--- a/src/esengine/data/shaders/batch.esshader+++ b/src/esengine/data/shaders/batch.esshader@@ -1,6 +1,11 @@#pragma shader "Batch"#pragma version 300 es+// Compile-time variant: when enabled the fragment stage treats the sampled+// alpha as a signed distance field (runtime glyph atlas, REARCH_GUI P1) and+// derives crisp, resolution-independent coverage instead of sampling RGBA.+#pragma feature SDF+#pragma vertexlayout(location = 0) in vec2 a_position;layout(location = 1) in vec4 a_color;@@ -47,6 +52,16 @@ void main() {else if (v_texIndex == 5) texColor = texture(u_textures[5], v_texCoord);else if (v_texIndex == 6) texColor = texture(u_textures[6], v_texCoord);else texColor = texture(u_textures[7], v_texCoord);+#ifdef SDF+ // The glyph atlas stores a signed distance in the alpha channel (RGB = 1).+ // Recover ~1px screen-space coverage with a smoothstep around the 0.5 edge;+ // fwidth tracks the on-screen scale so glyphs stay crisp at any size.+ float dist = texColor.a;+ float aa = fwidth(dist);+ float coverage = smoothstep(0.5 - aa, 0.5 + aa, dist);+ fragColor = vec4(v_color.rgb, v_color.a * coverage);+#elsefragColor = texColor * v_color;+#endif}#pragma enddiff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cppindex 0f698d008..b50894198 100644--- a/src/esengine/renderer/RenderFrame.cpp+++ b/src/esengine/renderer/RenderFrame.cpp@@ -360,12 +360,27 @@ void RenderFrame::collectAll(ecs::Registry& registry, u32 skipFlags) {}u32 RenderFrame::initBatchShader() {+ // The default (featureless) batch program; seeds the variant cache.+ return batchProgram({});+}++u32 RenderFrame::batchProgram(const std::vector<std::string>& features) {+ const std::string key = resource::ShaderParser::variantKey(features);+ auto it = batch_variants_.find(key);+ if (it != batch_variants_.end()) return it->second;+ const u32 prog = compileBatchVariant(features);+ batch_variants_.emplace(key, prog);+ return prog;+}++u32 RenderFrame::compileBatchVariant(const std::vector<std::string>& features) {// The batch shader is authored as a single .esshader, embedded for the web build.- // Parse it and assemble the two GLSL ES 3.00 stages (single source of truth).+ // Parse it and assemble the two GLSL ES 3.00 stages (single source of truth),+ // injecting the requested feature #defines (e.g. SDF).auto parsed = resource::ShaderParser::parse(ShaderEmbeds::BATCH);resource::ShaderHandle handle = resource_manager_.createShaderWithBindings(- resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex),- resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment),+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex, "", features),+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", features),{{0, "a_position"}, {1, "a_color"}, {2, "a_texCoord"}});@@ -382,7 +397,8 @@ u32 RenderFrame::initBatchShader() {return prog;}- ES_LOG_ERROR("Failed to create batch shader");+ const std::string vk = resource::ShaderParser::variantKey(features);+ ES_LOG_ERROR("Failed to create batch shader variant '{}'", vk.empty() ? "default" : vk.c_str());return 0;}diff --git a/src/esengine/renderer/RenderFrame.hpp b/src/esengine/renderer/RenderFrame.hppindex 7d301eb00..a91343585 100644--- a/src/esengine/renderer/RenderFrame.hpp+++ b/src/esengine/renderer/RenderFrame.hpp@@ -18,6 +18,7 @@#include <glm/glm.hpp>#include <vector>+#include <string>#include <memory>#include <unordered_map>@@ -114,6 +115,14 @@ public:void addPlugin(std::unique_ptr<RenderTypePlugin> plugin);void collectAll(ecs::Registry& registry, u32 skipFlags = 0);+ /**+ * Compiled program id for a batch-shader feature variant, compiled+cached on+ * first use (REARCH_GUI P1.2). `{}` is the default batch program; `{"SDF"}`+ * is the glyph-atlas SDF text variant. Same vertex layout + sampler/UBO setup+ * as the default, so quads of any variant share the batch vertex format.+ */+ u32 batchProgram(const std::vector<std::string>& features);+static constexpr u32 STAGE_COUNT = 4;private:@@ -149,6 +158,9 @@ private:std::unordered_map<u32, EntityStencilInfo> stencil_masks_;u32 batch_shader_id_ = 0;+ // Compiled batch-shader variants keyed by ShaderParser::variantKey(features)+ // (REARCH_GUI P1.2). {} → default, {"SDF"} → glyph-atlas text.+ std::unordered_map<std::string, u32> batch_variants_;TransientBufferPool pool_;DrawList draw_list_;ClipState clip_state_;@@ -157,6 +169,7 @@ private:RenderFrameContext makeContext();void buildClipState();u32 initBatchShader();+ u32 compileBatchVariant(const std::vector<std::string>& features);};} // namespace esengine