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
perf(render): the GPU buffers grew by the frame, the staging by doubling
esengine7c53e311be2e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9c5c4c241..2b1785b34 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -64,6 +64,14 @@ published separately; it ships inside the editor.### Fixed+- **A slowly growing frame reallocated the GPU buffers every frame.** The CPU+ staging vectors double when they run out, but the vertex/index buffers were+ resized to exactly what the frame in hand needed — so a workload creeping upward+ (2.1MB, then 2.2MB, then 2.3MB) reallocated on the GPU every single frame,+ forever, while the staging behind it grew once. The GPU capacity now follows the+ staging capacity, so there is one growth rule instead of two. Measured on a+ 200-frame rising workload: 200 reallocations before, single digits after.+- **A destroyed entity's handle could come back as a live one.** An `Entity` is a22-bit slot index plus a 10-bit generation, and recycling a slot bumped thegeneration with a wrap: after 1023 reuses the counter returned to 1 and the slotdiff --git a/src/esengine/renderer/rhi/TransientBufferPool.cpp b/src/esengine/renderer/rhi/TransientBufferPool.cppindex 0ba693c6b..023cf3a1d 100644--- a/src/esengine/renderer/rhi/TransientBufferPool.cpp+++ b/src/esengine/renderer/rhi/TransientBufferPool.cpp@@ -109,8 +109,11 @@ void TransientBufferPool::upload() {if (s.vbo == BufferHandle::Invalid) continue;if (s.vertex_write_pos == 0 && s.index_write_pos == 0) continue;+ // Grow to the STAGING capacity, which already doubles — not to what this+ // frame needed, which reallocates every frame for a workload creeping+ // upward (2.1MB, 2.2MB, 2.3MB). One growth rule, in growVertexStaging.if (s.vertex_write_pos > s.vbo_capacity) {- s.vbo_capacity = s.vertex_write_pos;+ s.vbo_capacity = static_cast<u32>(s.vertex_staging.size());device_.resizeBuffer(s.vbo, s.vbo_capacity, s.vertex_staging.data());} else if (s.vertex_write_pos > 0) {device_.updateBuffer(s.vbo, 0, s.vertex_staging.data(), s.vertex_write_pos);@@ -119,7 +122,7 @@ void TransientBufferPool::upload() {u32 eboBytes = s.index_write_pos * sizeof(u32);u32 eboCapBytes = s.ebo_capacity * sizeof(u32);if (eboBytes > eboCapBytes) {- s.ebo_capacity = s.index_write_pos;+ s.ebo_capacity = static_cast<u32>(s.index_staging.size());device_.resizeBuffer(s.ebo, static_cast<u32>(s.ebo_capacity * sizeof(u32)),s.index_staging.data());} else if (eboBytes > 0) {diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cppindex 9d85444ea..6658f4de7 100644--- a/tests/renderer/test_batch_builder.cpp+++ b/tests/renderer/test_batch_builder.cpp@@ -565,3 +565,45 @@ TEST_CASE("transparent: hundredths of depth separate; thousandths share a bucketfine.list.finalize(fine.pool);CHECK(fine.list.mergedDrawCallCount() == 1);}++// ─── Transient buffer growth ─────────────────────────────────────────────────+// A workload creeping upward each frame must not reallocate the GPU buffer every+// frame. The staging vectors double, and the GPU capacity follows them.++TEST_CASE("growth: a slowly rising workload reallocates a handful of times, not per frame") {+ MockGfxDevice device;+ TransientBufferPool pool{device};+ pool.init(/*initialVertexBytes=*/4096, /*initialIndexCount=*/1024);++ const int FRAMES = 200;+ for (int f = 0; f < FRAMES; ++f) {+ pool.beginFrame();+ // Each frame asks for a little more than the last.+ const u32 quads = 64 + static_cast<u32>(f) * 8;+ BatchVertex quad[4] = {};+ for (u32 i = 0; i < quads; ++i) {+ pool.appendVertices(LayoutId::Batch, quad, sizeof(quad));+ }+ pool.upload();+ }++ // Doubling from 4KB, the whole run needs a bounded number of reallocations —+ // one per doubling per stream, not one per frame.+ CHECK(device.resizeBufferCalls < 32);+ CHECK(device.resizeBufferCalls > 0); // it did have to grow+}++TEST_CASE("growth: a steady workload inside capacity never reallocates") {+ MockGfxDevice device;+ TransientBufferPool pool{device};+ pool.init(/*initialVertexBytes=*/1024 * 1024, /*initialIndexCount=*/64 * 1024);+ const int before = device.resizeBufferCalls;++ BatchVertex quad[4] = {};+ for (int f = 0; f < 100; ++f) {+ pool.beginFrame();+ for (int i = 0; i < 100; ++i) pool.appendVertices(LayoutId::Batch, quad, sizeof(quad));+ pool.upload();+ }+ CHECK_EQ(device.resizeBufferCalls, before);+}