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(renderer): allocate transient stream storage on first use
yhh7b1caad493dd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 118149b32..58702c259 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,6 +22,8 @@ published separately; it ships inside the editor.### Fixed+- **Unused transient streams no longer reserve GPU buffers.** Renderer pools initialize pipeline layouts without reserving vertex/index buffers or CPU staging for every stream. Each stream allocates storage on its first non-empty write, preserving buffer reuse, growth and recovery while avoiding unused particle, shape and sky allocations in 2D scenes.+- **GPU memory estimates include buffers and render targets.** Runtime reports and editor captures count live buffer capacity, texture mip chains, compressed blocks and target attachments without double-counting framebuffer references. WebGPU also includes private fallback, surface and readback resources. Lost or destroyed resources stop contributing; the profiler labels this as an estimate that excludes driver overhead, shader binaries and host-owned swapchain images.- **Play-mode performance captures include every recorded frame.** The editor's `profile_frames` command captures the running world's frame-end stream instead of its 250 ms telemetry snapshots, so frame counts, tail latency and system costs come from actual frames. Captures discard data if the play world is replaced, keep editor overhead visible, and can observe existing instrumentation without resetting timings or toggling another recorder's profiling switches.diff --git a/src/esengine/renderer/rhi/TransientBufferPool.cpp b/src/esengine/renderer/rhi/TransientBufferPool.cppindex b08f05884..98a8d27de 100644--- a/src/esengine/renderer/rhi/TransientBufferPool.cpp+++ b/src/esengine/renderer/rhi/TransientBufferPool.cpp@@ -111,6 +111,7 @@ void TransientBufferPool::uploadInstanceRows(Stream& s, u32& grows, u32& writes)u32 TransientBufferPool::allocVertices(LayoutId layout, u32 byteSize) {Stream& s = stream(layout);+ if (byteSize) ensureStorage(layout);u32 offset = s.vertex_write_pos;u32 newPos = s.vertex_write_pos + byteSize;if (newPos > static_cast<u32>(s.vertex_staging.size())) {@@ -122,6 +123,7 @@ u32 TransientBufferPool::allocVertices(LayoutId layout, u32 byteSize) {u32 TransientBufferPool::allocIndices(LayoutId layout, u32 count) {Stream& s = stream(layout);+ if (count) ensureStorage(layout);u32 offset = s.index_write_pos;u32 newPos = s.index_write_pos + count;if (newPos > static_cast<u32>(s.index_staging.size())) {@@ -248,19 +250,9 @@ u32 TransientBufferPool::vertexStride(LayoutId layout) const {return stream(layout).vertex_stride;}-void TransientBufferPool::setupStream(LayoutId layout) {+void TransientBufferPool::ensureStorage(LayoutId layout) {Stream& s = stream(layout);-- if (layout == LayoutId::MeshInstance) {- // Only a stream, and not a vertex one: the frame's per-object records go- // in a texture, so a shader can reach any record by index rather than only- // the one the hardware happens to be on.- s.vertex_stride = MESH_INSTANCE_STRIDE;- s.vertex_staging.resize(instanceRowBytes());- s.texture_rows = 0;- return;- }-+ if (layout == LayoutId::MeshInstance || s.vbo != BufferHandle::Invalid) return;if (layout == LayoutId::ParticleInstance) {// Per-instance (per-particle) stream: dynamic, streamed each frame.s.vertex_staging.resize(initial_vertex_bytes_);@@ -286,6 +278,33 @@ void TransientBufferPool::setupStream(LayoutId layout) {{GfxBufferUsage::Index, static_cast<u32>(sizeof(quadIdx)), /*dynamic=*/false},GfxContent::retained(), quadIdx);+ return;+ }+ s.vertex_staging.resize(initial_vertex_bytes_);+ s.index_staging.resize(initial_index_count_);+ s.vbo_capacity = initial_vertex_bytes_;+ s.ebo_capacity = initial_index_count_;++ s.vbo = device_.createBuffer({GfxBufferUsage::Vertex, s.vbo_capacity, /*dynamic=*/true},+ GfxContent::transient(), nullptr);+ s.ebo = device_.createBuffer(+ {GfxBufferUsage::Index, static_cast<u32>(s.ebo_capacity * sizeof(u32)), /*dynamic=*/true},+ GfxContent::transient(), nullptr);+}++void TransientBufferPool::setupStream(LayoutId layout) {+ Stream& s = stream(layout);++ if (layout == LayoutId::MeshInstance) {+ // Only a stream, and not a vertex one: the frame's per-object records go+ // in a texture, so a shader can reach any record by index rather than only+ // the one the hardware happens to be on.+ s.vertex_stride = MESH_INSTANCE_STRIDE;+ s.texture_rows = 0;+ return;+ }++ if (layout == LayoutId::ParticleInstance) {// Slot 0: the static quad (per vertex). Slot 1: the instance stream (per instance),// rebased per draw in bindInstanceLayout.VertexLayoutDesc desc;@@ -308,17 +327,6 @@ void TransientBufferPool::setupStream(LayoutId layout) {return;}- s.vertex_staging.resize(initial_vertex_bytes_);- s.index_staging.resize(initial_index_count_);- s.vbo_capacity = initial_vertex_bytes_;- s.ebo_capacity = initial_index_count_;-- s.vbo = device_.createBuffer({GfxBufferUsage::Vertex, s.vbo_capacity, /*dynamic=*/true},- GfxContent::transient(), nullptr);- s.ebo = device_.createBuffer(- {GfxBufferUsage::Index, static_cast<u32>(s.ebo_capacity * sizeof(u32)), /*dynamic=*/true},- GfxContent::transient(), nullptr);-VertexLayoutDesc desc;switch (layout) {case LayoutId::Batch:diff --git a/src/esengine/renderer/rhi/TransientBufferPool.hpp b/src/esengine/renderer/rhi/TransientBufferPool.hppindex b5c5decab..62be2c1b2 100644--- a/src/esengine/renderer/rhi/TransientBufferPool.hpp+++ b/src/esengine/renderer/rhi/TransientBufferPool.hpp@@ -28,6 +28,7 @@ class TransientBufferPool {public:explicit TransientBufferPool(GfxDevice& device);+ /** Initialize layouts; stream storage is allocated on its first non-empty write. */void init(u32 initialVertexBytes = 2 * 1024 * 1024,u32 initialIndexCount = 256 * 1024);void shutdown();@@ -96,6 +97,7 @@ private:};void setupStream(LayoutId layout);+ void ensureStorage(LayoutId layout);void uploadInstanceRows(Stream& s, u32& grows, u32& writes);static constexpr u32 instanceRowBytes() { return MESH_INSTANCE_TEXTURE_WIDTH * 16; }void growVertexStaging(Stream& s, u32 requiredBytes);diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cppindex 5f5ffa5f7..ec450903f 100644--- a/tests/renderer/test_batch_builder.cpp+++ b/tests/renderer/test_batch_builder.cpp@@ -1061,6 +1061,55 @@ TEST_CASE("transparent: hundredths of depth separate; thousandths share a bucket// 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("storage: idle streams allocate nothing and used streams survive recovery") {+ MockGfxDevice device;+ TransientBufferPool pool{device};+ pool.init(4096, 1024);+ CHECK(device.createBufferCalls == 0);+ CHECK(device.estimatedGpuBytes() == 0);+ for (auto layout : {LayoutId::Batch, LayoutId::Shape, LayoutId::Sky, LayoutId::ParticleInstance}) {+ CHECK(pool.layoutHandle(layout) != VertexLayoutHandle::Invalid);+ CHECK(pool.vertexStride(layout) > 0);+ CHECK(pool.vertexBuffer(layout) == BufferHandle::Invalid);+ }+ pool.allocVertices(LayoutId::Batch, 0);+ pool.allocIndices(LayoutId::Shape, 0);+ pool.upload();+ CHECK(device.createBufferCalls == 0);+ BatchVertex vertices[4] = {};+ const u32 indices[6] = {0, 1, 2, 2, 3, 0};+ pool.appendVertices(LayoutId::Batch, vertices, sizeof(vertices));+ pool.appendIndices(LayoutId::Batch, indices, 6);+ pool.upload();+ CHECK(device.createBufferCalls == 2);+ CHECK(device.estimatedGpuBytes() == 8192);+ const auto batch = pool.vertexBuffer(LayoutId::Batch);+ pool.beginFrame();+ pool.appendVertices(LayoutId::Batch, vertices, sizeof(vertices));+ pool.upload();+ CHECK(device.createBufferCalls == 2);+ CHECK(pool.vertexBuffer(LayoutId::Batch) == batch);+ std::vector<u8> particle(44);+ pool.appendVertices(LayoutId::ParticleInstance, particle.data(), 44);+ pool.upload();+ CHECK(device.createBufferCalls == 5);+ CHECK(device.estimatedGpuBytes() == 8192 + 4096 + 64 + 24);+ CHECK(pool.vertexBuffer(LayoutId::Sky) == BufferHandle::Invalid);+ CHECK(pool.vertexBuffer(LayoutId::Shape) == BufferHandle::Invalid);+ const auto storage = device.estimatedGpuBytes();+ device.notifyDeviceLost(GfxDeviceLostReason::ContextLost, "test");+ CHECK(device.estimatedGpuBytes() == 0);+ REQUIRE(device.recoverDevice());+ CHECK(device.estimatedGpuBytes() == storage);+ CHECK(pool.vertexBuffer(LayoutId::Batch) == batch);+ pool.upload();+ pool.shutdown();+ CHECK(device.estimatedGpuBytes() == 0);+ pool.init(4096, 1024);+ CHECK(device.estimatedGpuBytes() == 0);+ pool.shutdown();+}+TEST_CASE("growth: a slowly rising workload reallocates a handful of times, not per frame") {MockGfxDevice device;TransientBufferPool pool{device};diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 4fe9b5038..0e2264a91 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'perf(renderer): allocate transient stream storage on first use':+ { note: 'Unused transient streams no longer reserve GPU buffers.' },'fix(profiler): label live gpu storage estimates':{ note: 'GPU memory estimates include buffers and render targets.' },'fix(profiler): account for live gpu resource storage':