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(core): the engine can put its renderer back together
esengined8d10ee091db
diff --git a/src/esengine/core/EstellaContext.cpp b/src/esengine/core/EstellaContext.cppindex 18845066f..3bbad8788 100644--- a/src/esengine/core/EstellaContext.cpp+++ b/src/esengine/core/EstellaContext.cpp@@ -113,6 +113,34 @@ bool EstellaContext::init(Unique<GfxDevice> device) {return true;}+bool EstellaContext::recoverDevice() {+ auto* device = services_.getService<GfxDevice>();+ if (!device || !device->recoverDevice()) return false;++ // The order is the substance. Shaders first: every program id cached+ // downstream is read back from these handles. RenderContext next, because+ // its white texture is the placeholder the textures get swept onto.+ auto* resources = services_.getService<resource::ResourceManager>();+ auto* renderContext = services_.getService<RenderContext>();+ if (!resources || !renderContext) return false;++ resources->recreateGpuShaders();+ renderContext->recreateGpuResources();+ renderContext->materials().refreshShaderPrograms(*resources);++ if (auto* immediateDraw = services_.getService<ImmediateDraw>()) {+ immediateDraw->recreateGpuResources();+ }+ if (auto* renderFrame = services_.getService<RenderFrame>()) {+ renderFrame->recreateGpuResources();+ }++ // Last: the placeholder now exists to park them on. Their CONTENT is still+ // missing, which is why this leaves the device Recovering, not Live.+ resources->invalidateGpuTextures(renderContext->getWhiteTexture());+ return true;+}+void EstellaContext::initSubsystems(Unique<GfxDevice> gfxDevice) {// The device arrives first: ResourceManager (the GPU-resource factory) and// every other renderer subsystem borrow this single device. Which backenddiff --git a/src/esengine/core/EstellaContext.hpp b/src/esengine/core/EstellaContext.hppindex 433b1d1d2..3a67d2957 100644--- a/src/esengine/core/EstellaContext.hpp+++ b/src/esengine/core/EstellaContext.hpp@@ -47,6 +47,16 @@ public:*/bool init(Unique<GfxDevice> device);+ /**+ * @brief Brings the renderer back after a device loss.+ * @details Rebuilds the device, then every subsystem that owns GPU objects,+ * in the one order that works (see the definition). Leaves the+ * device Recovering: it draws, but its textures are placeholders+ * until the asset layer re-uploads them.+ * @return True when the renderer is usable again.+ */+ bool recoverDevice();+/*** @brief Shut down all subsystems and release resources*/diff --git a/src/esengine/renderer/draw/ImmediateDraw.cpp b/src/esengine/renderer/draw/ImmediateDraw.cppindex af4a5cc2d..d8ea7264a 100644--- a/src/esengine/renderer/draw/ImmediateDraw.cpp+++ b/src/esengine/renderer/draw/ImmediateDraw.cpp@@ -57,6 +57,18 @@ ImmediateDraw::~ImmediateDraw() {}}+void ImmediateDraw::recreateGpuResources() {+ if (!initialized_) return;+ pool_.recreateGpuResources();+ white_texture_id_ = context_.getWhiteTextureId();+ // NOT re-created: the manager re-compiled it behind the same handle, so the+ // only stale thing here is the program id cached from it.+ batch_shader_ = ShaderHandle::Invalid;+ if (Shader* shader = resource_manager_.getShader(batch_shader_ref_)) {+ if (shader->isValid()) batch_shader_ = shader->handle();+ }+}+void ImmediateDraw::init() {if (initialized_) return;@@ -71,6 +83,7 @@ void ImmediateDraw::init() {resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", {}, target),{{0, "a_position"}, {1, "a_color"}, {2, "a_texCoord"}},resource_manager_.preferredShaderLanguage());+ batch_shader_ref_ = handle;Shader* shader = resource_manager_.getShader(handle);if (shader && shader->isValid()) {batch_shader_ = shader->handle();diff --git a/src/esengine/renderer/draw/ImmediateDraw.hpp b/src/esengine/renderer/draw/ImmediateDraw.hppindex 04c8f887b..f916a140f 100644--- a/src/esengine/renderer/draw/ImmediateDraw.hpp+++ b/src/esengine/renderer/draw/ImmediateDraw.hpp@@ -20,6 +20,7 @@#include "../../core/Types.hpp"#include "../../math/Math.hpp"+#include "../../resource/Handle.hpp"#include "../rhi/PipelineState.hpp"#include "../rhi/TransientBufferPool.hpp"@@ -74,6 +75,10 @@ public:// =========================================================================void init();++ /// Re-creates the pool's buffers and re-reads the batch program id after a+ /// device loss. The shader itself is rebuilt by the manager, behind its handle.+ void recreateGpuResources();void shutdown();// =========================================================================@@ -180,6 +185,9 @@ private:resource::ResourceManager& resource_manager_;TransientBufferPool pool_;+ /// The handle is the identity; batch_shader_ caches the program id it+ /// currently resolves to, re-read when the device rebuilds the program.+ resource::ShaderHandle batch_shader_ref_;ShaderHandle batch_shader_ = ShaderHandle::Invalid;PipelineDesc base_desc_{};PipelineDesc current_desc_{};diff --git a/src/esengine/renderer/frame/PostProcessPipeline.cpp b/src/esengine/renderer/frame/PostProcessPipeline.cppindex 8790002d2..1df1ed6a7 100644--- a/src/esengine/renderer/frame/PostProcessPipeline.cpp+++ b/src/esengine/renderer/frame/PostProcessPipeline.cpp@@ -161,6 +161,28 @@ void PostProcessPipeline::shutdown() {ES_LOG_INFO("PostProcessPipeline shutdown");}+void PostProcessPipeline::recreateGpuResources() {+ if (!initialized_) return;+ // Handles dropped, not deleted; the framebuffers are Unique<> and simply let+ // go of objects that no longer exist. ensureFBOs re-creates them at the+ // current size on the next frame, exactly as a resize does.+ screen_quad_vbo_ = BufferHandle::Invalid;+ screen_quad_layout_ = VertexLayoutHandle::Invalid;+ sceneTexture_ = TextureHandle::Invalid;+ for (auto& pass : passes_) { pass.paramUbo = BufferHandle::Invalid; pass.paramDirty = true; }+ for (auto& pass : screenPasses_) { pass.paramUbo = BufferHandle::Invalid; pass.paramDirty = true; }++ fboA_.reset();+ fboB_.reset();+ fboOriginal_.reset();+ screenFBO_.reset();+ fbosCreated_ = false;+ fboOriginalCreated_ = false;+ screenFBOCreated_ = false;+ screenCaptureActive_ = false;+ ensureFBOs();+}+void PostProcessPipeline::resize(u32 width, u32 height) {if (!initialized_) return;if (width == width_ && height == height_) return;diff --git a/src/esengine/renderer/frame/PostProcessPipeline.hpp b/src/esengine/renderer/frame/PostProcessPipeline.hppindex 090256429..3afd0c1cb 100644--- a/src/esengine/renderer/frame/PostProcessPipeline.hpp+++ b/src/esengine/renderer/frame/PostProcessPipeline.hpp@@ -84,6 +84,10 @@ public:*/void resize(u32 width, u32 height);+ /// Rebuilds the intermediates and per-pass buffers after a device loss. The+ /// blit shader is not re-created — the manager rebuilt it behind its handle.+ void recreateGpuResources();+/*** @brief Adds a post-processing pass* @param name Unique name for the passdiff --git a/src/esengine/renderer/frame/RenderContext.hpp b/src/esengine/renderer/frame/RenderContext.hppindex fbfadc0f0..7b10e2308 100644--- a/src/esengine/renderer/frame/RenderContext.hpp+++ b/src/esengine/renderer/frame/RenderContext.hpp@@ -146,6 +146,10 @@ public:*/u32 getWhiteTextureId() const { return static_cast<u32>(whiteTexture_); }+ /// The white 1x1 as a typed handle — what a lost texture is parked on until+ /// its content is re-uploaded.+ TextureHandle getWhiteTexture() const { return whiteTexture_; }+/*** @brief Uploads the per-frame view-projection into the shared FrameConstants UBO.diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cppindex 4e0866ed6..04da440aa 100644--- a/src/esengine/renderer/frame/RenderFrame.cpp+++ b/src/esengine/renderer/frame/RenderFrame.cpp@@ -178,6 +178,16 @@ void RenderFrame::shutdown() {ES_LOG_INFO("RenderFrame shutdown");}+void RenderFrame::recreateGpuResources() {+ pool_.recreateGpuResources();+ target_manager_.recreateGpuResources();+ // The variant handles are unchanged; this re-reads the program ids behind them.+ batch_shader_id_ = initBatchShader();+#ifdef ES_ENABLE_POSTPROCESS+ if (post_process_) post_process_->recreateGpuResources();+#endif+}+void RenderFrame::resize(u32 width, u32 height) {width_ = width;height_ = height;@@ -713,13 +723,16 @@ u32 RenderFrame::initBatchShader() {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;+ if (it == batch_variants_.end()) {+ it = batch_variants_.emplace(key, compileBatchVariant(features)).first;+ }+ // Resolved per lookup rather than cached: the program id is what the device+ // replaces on a rebuild, and the handle is what survives it.+ Shader* shader = resource_manager_.getShader(it->second);+ return shader ? shader->getProgramId() : 0;}-u32 RenderFrame::compileBatchVariant(const std::vector<std::string>& features) {+resource::ShaderHandle RenderFrame::compileBatchVariant(const std::vector<std::string>& features) {// The batch shader is authored as a single .esshader with its WGSL twin,// embedded for the web build. Parse it and assemble the two stages for the// preferred target (single source of truth), injecting the requested@@ -748,12 +761,12 @@ u32 RenderFrame::compileBatchVariant(const std::vector<std::string>& features) {if (loc >= 0) device_.setUniform1i(loc, i);}shader->unbind();- return shader->getProgramId();+ return handle;}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;+ return {};}} // namespace esenginediff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hppindex ab42cb2f8..1f1a49429 100644--- a/src/esengine/renderer/frame/RenderFrame.hpp+++ b/src/esengine/renderer/frame/RenderFrame.hpp@@ -88,6 +88,10 @@ public:void shutdown();void resize(u32 width, u32 height);+ /// Rebuilds everything this frame owns after a device loss: the transient+ /// pool, the render targets, the batch program ids and the post-process chain.+ void recreateGpuResources();+/** @brief The frame's main-pass load-op: which attachments to clear, the color,* and an optional region (w == 0 = full target — per-camera flows clear* only their viewport on the shared default target). */@@ -273,7 +277,9 @@ private:u32 batch_shader_id_ = 0;// Compiled batch-shader variants keyed by ShaderParser::variantKey(features).// {} → default, {"SDF"} → glyph-atlas text.- std::unordered_map<std::string, u32> batch_variants_;+ /// Variant key -> the shader HANDLE. Not the program id: rebuilding the+ /// device changes every id, and the handle is what stays true across it.+ std::unordered_map<std::string, resource::ShaderHandle> batch_variants_;TransientBufferPool pool_;DrawList draw_list_;bool linear_color_ = false;@@ -287,7 +293,7 @@ private:/// flush() uploads + binds the result so Lit2D material shaders read it.void collectLights(ecs::Registry& registry);u32 initBatchShader();- u32 compileBatchVariant(const std::vector<std::string>& features);+ resource::ShaderHandle compileBatchVariant(const std::vector<std::string>& features);std::vector<GpuLight2D> light_scratch_; // reused across frames; collectLights only