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): the profiler and the draw-call count cover every camera of the frame
esengine279e98f82eb2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8527647ae..9c031df03 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -33,6 +33,7 @@ published separately; it ships inside the editor.### Fixed+- **With more than one camera, the profiler and the draw-call count cover the whole frame.** Each camera reset the frame's tally and closed the profiler's frame, so a split screen, or a scene with a UI camera, reported only the last camera's draw calls, triangles, merge counts and C++ timings, and the batch-break reasons of the others were lost. The frame now counts every camera and the screen overlay, once, and an asset preview rendered in between counts toward none of them.- **A game started right after the project opened no longer restarts itself about ten seconds in.** Pressing Play while the editor was still warming the game's engine up handed the scene over twice: once when the engine greeted the editor, and again when the warm-up's ten-second wait ran out — so the game restarted, and everything that had happened in it was gone. It now starts once, as soon as the engine is up.- A mini-game whose host's WebGL2 context also offers `OES_vertex_array_object` renders instead of hanging: the WebGL1 extensions WebGL2 made core are withheld from a WebGL2 context, as the spec says, so Emscripten no longer swaps in the host's broken extension path (vivo's).- `estella export` writes a mini-game's appid and project version as the build dialog does — a headless Bilibili package came out with an empty `appId`.diff --git a/src/esengine/renderer/draw/DrawList.cpp b/src/esengine/renderer/draw/DrawList.cppindex c23d4fc84..84d18829d 100644--- a/src/esengine/renderer/draw/DrawList.cpp+++ b/src/esengine/renderer/draw/DrawList.cpp@@ -84,6 +84,8 @@ void DrawList::finalize(TransientBufferPool& pool) {u32 count = static_cast<u32>(commands_.size());if (count == 0) {merged_draw_calls_ = 0;+ merged_away_ = 0;+ std::fill(std::begin(breaks_), std::end(breaks_), 0u);return;}@@ -187,17 +189,8 @@ void DrawList::finalize(TransientBufferPool& pool) {commands_.resize(writeIdx);merged_draw_calls_ = writeIdx;- // What the frame's draw-call count is made of. Emitted per reason and only- // where it happened, so a clean frame publishes nothing rather than a wall- // of zeroes for a caller to read past.- if (FrameProfiler::get().enabled()) {- ES_PROFILE_COUNTER("batch.draws", merged_draw_calls_);- ES_PROFILE_COUNTER("batch.merged", count - merged_draw_calls_);- for (u32 r = 1; r < static_cast<u32>(BatchBreak::Count); ++r) {- if (breaks[r] == 0) continue;- FrameProfiler::get().counter(batchBreakCounter(static_cast<BatchBreak>(r)), breaks[r]);- }- }+ merged_away_ = count - merged_draw_calls_;+ std::copy(std::begin(breaks), std::end(breaks), std::begin(breaks_));}std::span<const Entity> DrawList::runEntities(u32 index) const {diff --git a/src/esengine/renderer/draw/DrawList.hpp b/src/esengine/renderer/draw/DrawList.hppindex 50d7a5630..dd145396a 100644--- a/src/esengine/renderer/draw/DrawList.hpp+++ b/src/esengine/renderer/draw/DrawList.hpp@@ -70,6 +70,10 @@ public:u32 commandCount() const { return static_cast<u32>(commands_.size()); }u32 mergedDrawCallCount() const { return merged_draw_calls_; }+ /** How many of the last finalize's draws started for @p reason. */+ u32 breakCount(BatchBreak reason) const { return breaks_[static_cast<u32>(reason)]; }+ /** How many submitted commands the last finalize folded into another. */+ u32 mergedAwayCount() const { return merged_away_; }/** The entities merged draw @p index was made from, in draw order. */std::span<const Entity> runEntities(u32 index) const;@@ -211,6 +215,8 @@ private:/// @see reserveMask2DSlotsu32 mask_2d_slots_ = 0;u32 merged_draw_calls_ = 0;+ u32 merged_away_ = 0;+ u32 breaks_[static_cast<u32>(BatchBreak::Count)] = {};/// Members of a SortingGroup, by entity id — empty in the common frame.std::unordered_map<u32, GroupIdentity> group_identities_;u32 ysort_mask_ = 0;diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cppindex 666761cd2..01a8f7763 100644--- a/src/esengine/renderer/frame/RenderFrame.cpp+++ b/src/esengine/renderer/frame/RenderFrame.cpp@@ -291,6 +291,11 @@ void RenderFrame::beginFrame() {// them, so a buffer reused between two of them is read by both.context_.beginPerDrawBlocks();frame_capture_.beginFrame();++ // The frame's tally, over every camera and the overlay.+ stats_ = Stats{};+ frame_merged_ = 0;+ std::fill(std::begin(frame_breaks_), std::end(frame_breaks_), 0u);}void RenderFrame::applySceneDepthNeed() {@@ -325,8 +330,6 @@ void RenderFrame::begin(const glm::mat4& view_projection, RenderTargetManager::Hin_frame_ = true;frame_capture_.beginPass(CapturePass::Scene);- stats_ = Stats{};-pool_.beginFrame();draw_list_.clear();clip_state_.clear();@@ -497,6 +500,37 @@ void RenderFrame::flush() {// What the frame's render targets cost. The atlas and every chain// intermediate come out of one pool, so one number answers for all of them —// and a pool that stopped giving targets back is visible in no pixel.+ tallyPass(draw_list_);+}++void RenderFrame::endFrame() {+ frame_capture_.endFrame();+ device_.endFrame();+ publishFrameCounters();+ // Once per frame and not per camera: a commit at each camera's end kept only+ // the last camera's scopes and counters.+ FrameProfiler::get().commit();+}++// What one real pass's merge decided, added to the frame's tally. A preview+// surface finalizes a list too, and is not part of the frame.+void RenderFrame::tallyPass(const DrawList& list) {+ frame_merged_ += list.mergedAwayCount();+ for (u32 r = 1; r < static_cast<u32>(BatchBreak::Count); ++r) {+ frame_breaks_[r] += list.breakCount(static_cast<BatchBreak>(r));+ }+}++void RenderFrame::publishFrameCounters() {+ if (!FrameProfiler::get().enabled()) return;+ ES_PROFILE_COUNTER("batch.draws", stats_.draw_calls);+ ES_PROFILE_COUNTER("batch.merged", frame_merged_);+ // Emitted per reason and only where it happened, so a clean frame publishes+ // nothing rather than a wall of zeroes for a caller to read past.+ for (u32 r = 1; r < static_cast<u32>(BatchBreak::Count); ++r) {+ if (frame_breaks_[r] == 0) continue;+ FrameProfiler::get().counter(batchBreakCounter(static_cast<BatchBreak>(r)), frame_breaks_[r]);+ }ES_PROFILE_COUNTER("render.targets", target_pool_.count());ES_PROFILE_COUNTER("render.targets.bytes", target_pool_.bytes());// Memory on the other side: pixels and buffers the device copies so they@@ -529,11 +563,6 @@ void RenderFrame::flush() {#endif}-void RenderFrame::endFrame() {- frame_capture_.endFrame();- device_.endFrame();-}-void RenderFrame::end() {if (!in_frame_) return;@@ -621,8 +650,6 @@ void RenderFrame::end() {in_frame_ = false;flushed_ = false;- // Render is the frame's last C++ work: earlier scopes are already accumulated.- FrameProfiler::get().commit();}void RenderFrame::replayToDrawCall(i32 drawIndex) {diff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hppindex db6a1e1f2..de14e27e9 100644--- a/src/esengine/renderer/frame/RenderFrame.hpp+++ b/src/esengine/renderer/frame/RenderFrame.hpp@@ -509,6 +509,10 @@ private:i32 replay_draw_ = -1;bool snapshot_matches_ = false;void runPendingReplay(const glm::mat4& projection, u32 w, u32 h);+ void tallyPass(const DrawList& list);+ void publishFrameCounters();+ u32 frame_merged_ = 0;+ u32 frame_breaks_[static_cast<u32>(BatchBreak::Count)] = {};u32 snapshot_w_ = 0;u32 snapshot_h_ = 0;PreviewSurface preview_;diff --git a/src/esengine/renderer/frame/RenderFrameOverlay.cpp b/src/esengine/renderer/frame/RenderFrameOverlay.cppindex 719951f28..93b322e0f 100644--- a/src/esengine/renderer/frame/RenderFrameOverlay.cpp+++ b/src/esengine/renderer/frame/RenderFrameOverlay.cpp@@ -101,6 +101,7 @@ void RenderFrame::endScreenOverlay(RenderTargetManager::Handle target) {pool_.upload();}accumulateStats(draw_list_);+ tallyPass(draw_list_);FramebufferHandle fbo = FramebufferHandle::Default;if (target != RenderTargetManager::INVALID_HANDLE) {diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex bc14e5df6..c441b0341 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -984,6 +984,8 @@ export const NOTED = {{ note: 'A frame can be captured draw call by draw call, and replayed up to any one of them.' },'feat(editor): the Frame Debugger panel lists a frame\'s draws, why each started, and replays up to one':{ note: 'A frame can be captured draw call by draw call, and replayed up to any one of them.' },+ 'fix(render): the profiler and the draw-call count cover every camera of the frame':+ { note: 'With more than one camera, the profiler and the draw-call count cover the whole frame.' },'fix(play): a Play pressed while the engine warms up starts the game once, not again ten seconds later':{ note: 'A game started right after the project opened no longer restarts itself about ten seconds in.' },'fix(play): the game starts once when Play is pressed during the warm-up':diff --git a/tools/render-host/host.ts b/tools/render-host/host.tsindex 059859ef8..8ebdaa7a4 100644--- a/tools/render-host/host.ts+++ b/tools/render-host/host.ts@@ -433,7 +433,7 @@ const api = {*/async frameDebug(): Promise<{draws: number; passes: number; breaks: Record<string, number>;- first: number; last: number; differ: number; matches: boolean;+ first: number; last: number; differ: number | null; matches: boolean;} | null> {if (!module) return null;const next = async () => { await api.step(1, 1 / 60); };@@ -449,10 +449,12 @@ const api = {const b = await replayDraw(module, pass[pass.length - 1].index, next);if (!a || !b) return null;const painted = (px: Uint8ClampedArray) => { let n = 0; for (let i = 3; i < px.length; i += 4) if (px[i] > 0) n++; return n; };- let differ = 0;+ let changed = 0;for (let i = 0; i < a.image.data.length; i += 4) {- if (Math.abs(a.image.data[i] - b.image.data[i]) + Math.abs(a.image.data[i + 3] - b.image.data[i + 3]) > 16) differ++;+ if (Math.abs(a.image.data[i] - b.image.data[i]) + Math.abs(a.image.data[i + 3] - b.image.data[i + 3]) > 16) changed++;}+ // A pass of one draw has no earlier draw to differ from.+ const differ = pass.length > 1 ? changed : null;return {draws: cap.drawCalls.length, passes: cap.passCount, breaks,first: painted(a.image.data), last: painted(b.image.data), differ,diff --git a/tools/render-host/run.mjs b/tools/render-host/run.mjsindex 9cb51b124..a2004672f 100644--- a/tools/render-host/run.mjs+++ b/tools/render-host/run.mjs@@ -717,7 +717,7 @@ app.whenReady().then(async () => {const sameReasons = JSON.stringify(counted) === JSON.stringify(reported);frameDebug = {...got, counted, sameReasons,- ok: Boolean(got) && got.draws > 0 && got.matches && sameReasons && got.last > 0 && got.differ > 0,+ ok: Boolean(got) && got.draws > 0 && got.matches && sameReasons && got.last > 0 && (got.differ === null || got.differ > 0),};}// Optional color/orientation assertion: ESTELLA_VERIFY_EXPECT is a JSON array ofdiff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex c756cc9c6..2131aa6f8 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -77,6 +77,7 @@ export const SCENES = [{ id: "editor-grid-grazing", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-instanced.esscene", ESTELLA_VERIFY_GRID: "64", ESTELLA_VERIFY_GRID_EXPECT: "grazing", ESTELLA_VERIFY_PERSPECTIVE: "1", ESTELLA_VERIFY_ORBIT: "0,4", ESTELLA_VERIFY_STEPS: "3" } },{ id: "frame-debugger-2d", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/sprite-rendering.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/sprite-rendering.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_FRAME_DEBUG: "1" } },{ id: "frame-debugger-3d", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/ssao.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_FRAME_DEBUG: "1" } },+ { id: "frame-debugger-split", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/camera-split.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/camera-split.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_FRAME_DEBUG: "1" } },{ id: "mesh-renderer", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-renderer.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-renderer.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.30,\"y\":0.556,\"rgb\":[255,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.556,\"rgb\":[0,255,0],\"tol\":40},{\"x\":0.30,\"y\":0.40,\"rgb\":[255,0,0],\"tol\":40}]" } },// One mask, both its sides, and a sprite that never asked. A mask that does nothing// paints the frame red; a side that inverted swaps green and red. The blue bar states