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
fix(render): a device rebuild does not leave its shaders behind
esenginead3b9bb8abe1
diff --git a/desktop/scripts/headless-verify.mjs b/desktop/scripts/headless-verify.mjsindex 5616d8be3..3a6905994 100644--- a/desktop/scripts/headless-verify.mjs+++ b/desktop/scripts/headless-verify.mjs@@ -119,12 +119,17 @@ function finish(result, server) {&& dl.recovered === true && dl.statusAfterRecover === 2 && dl.fullRecovered === true);// Objects the dead context minted that nobody released — visible only across// rounds. From the SECOND, so the first recovery's one-off costs are not read- // as a slope. Per table per round; unheld, buffers alone grew by 18.+ // as a slope. What the ENGINE owns comes back to the same size, full stop: a+ // program it re-creates on every rebuild without releasing the last is a leak+ // the round count makes obvious and a single round never can.const rounds = dl?.rounds ?? [];- const growthOk = rounds.length < 3 || ((first, last, spans) =>- ['programs', 'buffers', 'textures', 'vaos', 'framebuffers']- .every((k) => (last.tables[k] - first.tables[k]) <= spans * 6)- )(rounds[1], rounds[rounds.length - 1], rounds.length - 2);+ const growthOk = rounds.length < 3 || ((first, last, spans) => {+ const owned = ['programs', 'textures', 'vaos', 'framebuffers']+ .every((k) => last.tables[k] <= first.tables[k]);+ // Buffers get one allowance per rebuild: the host mints an internal buffer+ // for each WebGL context it creates, and no engine call names it.+ return owned && (last.tables.buffers - first.tables.buffers) <= spans;+ })(rounds[1], rounds[rounds.length - 1], rounds.length - 2);const deviceLossOk = lossSeen && cameBack && drivenSteps && growthOk;// Freezing nothing would pass every pixel assertion by not having changed the// scene — the shape of a check that cannot fail.diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cppindex f56de92cd..54b221e2b 100644--- a/src/esengine/renderer/plugins/MeshPlugin.cpp+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp@@ -43,7 +43,13 @@ u32 meshVariant(bool normals, bool lit, bool normalMapped) {void MeshPlugin::init(RenderFrameContext& ctx) {// A rebuild after a lost device runs this again, and every program minted// against the dead context is gone — so the cache empties here rather than- // handing out ids that name nothing.+ // handing out ids that name nothing. The RESOURCES are released, not just+ // forgotten: a shader nobody holds still sits in the pool, gets recompiled+ // on the next loss, and keeps a host program object alive for good.+ for (auto& shader : mesh_shaders_) {+ if (shader.isValid()) ctx.resources.releaseShader(shader);+ shader = {};+ }mesh_compiled_.fill(false);mesh_programs_.fill(0);// The base variant now, so a broken shader is a boot-time failure rather than@@ -75,7 +81,8 @@ u32 MeshPlugin::meshProgram(RenderFrameContext& ctx, bool normals, bool lit, booauto compile = [&](std::vector<std::string> features) -> u32 {const bool normalMapped = std::find(features.begin(), features.end(), "NORMAL_MAP")!= features.end();- resource::ShaderHandle handle = ctx.resources.createShaderWithBindings(+ resource::ShaderHandle& handle = mesh_shaders_[variant];+ handle = ctx.resources.createShaderWithBindings(resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex, "", features, target),resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", features, target),{}, ctx.resources.preferredShaderLanguage());diff --git a/src/esengine/renderer/plugins/MeshPlugin.hpp b/src/esengine/renderer/plugins/MeshPlugin.hppindex 0238461b8..11f33cf3a 100644--- a/src/esengine/renderer/plugins/MeshPlugin.hpp+++ b/src/esengine/renderer/plugins/MeshPlugin.hpp@@ -5,6 +5,7 @@#include <array>#include "../RenderTypePlugin.hpp"#include "../draw/BatchVertex.hpp"+#include "../../resource/Handle.hpp"#include <vector>@@ -29,6 +30,10 @@ private:/// questions. Compiled on first use — a scene draws one or two of the five.std::array<u32, 8> mesh_programs_{};std::array<bool, 8> mesh_compiled_{};+ /// The shader RESOURCES behind those ids, kept because init() runs again+ /// after a device rebuild: with no handle to release, each rebuild left a+ /// dead shader in the pool and one more program the host never freed.+ std::array<resource::ShaderHandle, 8> mesh_shaders_{};u32 meshProgram(RenderFrameContext& ctx, bool normals, bool lit, bool normalMapped);bool warned_material_ = false; ///< A material on resident geometry is said once.};diff --git a/src/esengine/renderer/plugins/ParticlePlugin.cpp b/src/esengine/renderer/plugins/ParticlePlugin.cppindex 8d3f4c502..eab08a2e9 100644--- a/src/esengine/renderer/plugins/ParticlePlugin.cpp+++ b/src/esengine/renderer/plugins/ParticlePlugin.cpp@@ -38,9 +38,11 @@ void ParticlePlugin::init(RenderFrameContext& ctx) {// Particle instancing shader, authored as particle.esshader (single source,// WGSL twin included) and embedded for the web build. Attribute locations// are explicit, so no name bindings.+ if (particle_shader_handle_.isValid()) ctx.resources.releaseShader(particle_shader_handle_);const auto target = ctx.resources.preferredShaderTarget();auto parsed = resource::ShaderParser::parse(ShaderEmbeds::PARTICLE);- resource::ShaderHandle handle = ctx.resources.createShaderWithBindings(+ resource::ShaderHandle& handle = particle_shader_handle_;+ handle = ctx.resources.createShaderWithBindings(resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex, "", {}, target),resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", {}, target),{}, ctx.resources.preferredShaderLanguage());diff --git a/src/esengine/renderer/plugins/ParticlePlugin.hpp b/src/esengine/renderer/plugins/ParticlePlugin.hppindex 738a6ed4b..0980a5e20 100644--- a/src/esengine/renderer/plugins/ParticlePlugin.hpp+++ b/src/esengine/renderer/plugins/ParticlePlugin.hpp@@ -4,6 +4,7 @@#include "../RenderTypePlugin.hpp"#include "../draw/BatchVertex.hpp"+#include "../../resource/Handle.hpp"#include <vector>@@ -25,6 +26,9 @@ public:private:particle::ParticleSystem* particle_system_ = nullptr;u32 particle_shader_id_ = 0; // PARTICLE_INSTANCE program (GPU instancing)+ /// The shader resource behind it — held so a device rebuild, which runs+ /// init() again, can release the one the dead context owned.+ resource::ShaderHandle particle_shader_handle_;// Reused per-frame scratch for per-particle trail ribbons.std::vector<glm::vec2> trail_center_; ///< Centerline (world), oldest→head.diff --git a/src/esengine/renderer/plugins/ShapePlugin.cpp b/src/esengine/renderer/plugins/ShapePlugin.cppindex c580097b4..48cf36e2f 100644--- a/src/esengine/renderer/plugins/ShapePlugin.cpp+++ b/src/esengine/renderer/plugins/ShapePlugin.cpp@@ -18,6 +18,9 @@ void ShapePlugin::init(RenderFrameContext& ctx) {// One path for both backends: the WGSL twin rides shape.esshader, so the// stages assemble for the preferred target. Attribute name bindings are a// GL link-time concept; the WGSL twin fixes its locations in-source.+ // A device rebuild runs init() again; releasing the previous handle is what+ // keeps the pool from filling with shaders of dead contexts.+ if (shape_shader_handle_.isValid()) ctx.resources.releaseShader(shape_shader_handle_);const auto target = ctx.resources.preferredShaderTarget();auto shapeParsed = resource::ShaderParser::parse(ShaderEmbeds::SHAPE);shape_shader_handle_ = ctx.resources.createShaderWithBindings(