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 frame refused for a lost device starts empty and collects nothing, so its staging no longer grows until an allocation stops the module
yhhb07c43ae7995
diff --git a/src/esengine/bindings/RendererBindings.cpp b/src/esengine/bindings/RendererBindings.cppindex e07a42447..c83b25bf3 100644--- a/src/esengine/bindings/RendererBindings.cpp+++ b/src/esengine/bindings/RendererBindings.cpp@@ -851,7 +851,8 @@ void renderer_endScreenOverlay(u32 targetHandle) {}void renderer_submitAll(ecs::Registry& registry, i32 vpX, i32 vpY, i32 vpW, i32 vpH) {- if (!g_renderFrame) return;+ // A frame refused for a lost device draws nothing, so nothing is collected for it.+ if (!g_renderFrame || !g_renderFrame->inFrame()) return;ensureTransformsUpdated(registry);// What this camera must NOT draw. Set on every collect rather than once a// frame: the set is rebuilt by each layout pass, and a camera holding thediff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cppindex 09fec1510..5ecc379a4 100644--- a/src/esengine/renderer/frame/RenderFrame.cpp+++ b/src/esengine/renderer/frame/RenderFrame.cpp@@ -421,6 +421,13 @@ void RenderFrame::begin(const glm::mat4& view_projection, RenderTargetManager::Hvoid RenderFrame::begin(const glm::mat4& view_projection, RenderTargetManager::Handle target,const PassClear& clear) {+ // Emptied before the device is asked, so a refused frame starts empty too: the host still submits text and skinned batches into+ // it, and while a lost device waits for its content no end() ever ran to empty them, so the staging grew every frame until an+ // allocation of gigabytes failed.+ pool_.beginFrame();+ draw_list_.clear();+ clip_state_.clear();+// A lost device makes the whole frame meaningless, so it is refused here// rather than discovered by each pass and readback failing separately.// in_frame_ stays false, which makes end() a no-op too: no half-frame.@@ -439,10 +446,6 @@ void RenderFrame::begin(const glm::mat4& view_projection, RenderTargetManager::Hin_frame_ = true;frame_capture_.beginPass(CapturePass::Scene);- pool_.beginFrame();- draw_list_.clear();- clip_state_.clear();-openPass(clear, target);// The rect this camera draws into, taken where the camera STATES it — a zero sizediff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hppindex 0217accdb..6024aebd3 100644--- a/src/esengine/renderer/frame/RenderFrame.hpp+++ b/src/esengine/renderer/frame/RenderFrame.hpp@@ -511,6 +511,10 @@ public:* entity of it need exist. */RenderPrewarmResult prewarmDocument(const MeshDocumentRecord* rows, u32 count);void collectAll(ecs::Registry& registry, bool sceneGBuffer = true);+ /** @brief Whether begin() opened a frame; false when it refused one for a lost device. */+ bool inFrame() const { return in_frame_; }+ /** @brief The vertex bytes staged for @p layout since the last begin(). */+ u32 stagedVertexBytes(LayoutId layout) const { return pool_.vertexBytesUsed(layout); }/*** @brief Whether the camera being rendered right now would draw something ofdiff --git a/tests/renderer/test_frame_lifecycle.cpp b/tests/renderer/test_frame_lifecycle.cppindex a41921b4c..f5efd4577 100644--- a/tests/renderer/test_frame_lifecycle.cpp+++ b/tests/renderer/test_frame_lifecycle.cpp@@ -5,6 +5,7 @@#include "MockGfxDevice.hpp"#include "esengine/renderer/frame/RenderFrame.hpp"#include "esengine/renderer/frame/RenderContext.hpp"+#include "esengine/renderer/draw/TextVertexFormat.hpp"#include "esengine/resource/ResourceManager.hpp"#include "esengine/ecs/Registry.hpp"@@ -73,7 +74,28 @@ int main() {frame.endFrame();CHECK(device.endFrameCalls == 3, "an empty frame closes once too");- // No shutdown() either: nothing was initialised.+ // Last, because it loses the device, and initialised only here, because a text batch needs the frame's programs and staging. A lost+ // device refuses every frame until its content is back, and the host still submits text into them. What one refused frame staged+ // is gone by the next: a staging that kept growing reached gigabytes on a large scene and aborted the module.+ resources.init(device);+ frame.init(kW, kH);+ device.notifyDeviceLost(GfxDeviceLostReason::ContextLost, "gone");+ f32 glyphs[TEXT_VERTEX_FLOATS * 4] = {};+ const u16 quad[6] = {0, 1, 2, 0, 2, 3};+ const glm::mat4 identity(1.0f);+ u32 afterFirst = 0;+ for (int i = 0; i < 3; ++i) {+ frame.beginFrame();+ frame.begin(identity);+ frame.submitTextBatch(glyphs, 4, quad, 6, 0, &identity[0][0], Entity{}, 0, 0.0f, TextGlyphMode::Color, 0);+ if (i == 0) afterFirst = frame.stagedVertexBytes(LayoutId::Batch);+ frame.end();+ frame.endFrame();+ }+ CHECK(!frame.inFrame(), "a lost device's frame is refused");+ CHECK(afterFirst > 0 && frame.stagedVertexBytes(LayoutId::Batch) == afterFirst,+ "a refused frame starts empty, so what the host submits into it does not pile up");+ resources.shutdown();std::printf(g_failures ? "\n%d check(s) failed\n" : "\nall checks passed\n", g_failures);return g_failures == 0 ? 0 : 1;