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(profiler): account for live gpu resource storage
yhhac03b9c566d4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5227a3319..118149b32 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,6 +22,8 @@ published separately; it ships inside the editor.### Fixed+- **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.- **Inactive 2D bodies retain pending collider and body edits.** Physics reconciliation tracks the last synchronization of each body, so changes made while a parent or entity is inactive are applied on reactivation instead of being skipped by another body's newer global synchronization. Collider enable flags and rigid-body properties update in place, preserving the body and its existing simulation state.diff --git a/desktop b/desktopindex 3edce7a5a..7c0ad54bb 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit 3edce7a5a6a0c44d39fcf36e6bce0aee668935b1+Subproject commit 7c0ad54bbd26730808c71212ce1e135c749f92bcdiff --git a/sdk/etc/wasm.api.md b/sdk/etc/wasm.api.mdindex 6516ac549..97ff4064a 100644--- a/sdk/etc/wasm.api.md+++ b/sdk/etc/wasm.api.md@@ -427,6 +427,7 @@ renderer_getCapturedTextureCount: () => numberrenderer_getCapturedTextures: () => numberrenderer_getCulled: () => numberrenderer_getDrawCalls: () => number+renderer_getEstimatedGpuBytes: (() => number) | undefinedrenderer_getGpuTimeMs: (() => number) | undefinedrenderer_getLiveObjects: (() => GfxLiveObjects | null) | undefinedrenderer_getMeshes: () => numberdiff --git a/sdk/src/app/profileRecorder.ts b/sdk/src/app/profileRecorder.tsindex 38e1dcf7c..2e61b1527 100644--- a/sdk/src/app/profileRecorder.ts+++ b/sdk/src/app/profileRecorder.ts@@ -139,7 +139,7 @@ export class ProfileRecorder {memory: {wasmBytes: this.app_.wasmModule?.HEAPU8?.byteLength ?? 0,jsHeapBytes: jsHeapBytes(),- vramBytes: m?.renderer_getTextureBytes?.() ?? 0,+ vramBytes: m?.renderer_getEstimatedGpuBytes?.() ?? 0,},};this.frames_.push(frame);diff --git a/sdk/src/ecs/bridge/nativeEngineApi.generated.ts b/sdk/src/ecs/bridge/nativeEngineApi.generated.tsindex a9704f6df..4687c4d89 100644--- a/sdk/src/ecs/bridge/nativeEngineApi.generated.ts+++ b/sdk/src/ecs/bridge/nativeEngineApi.generated.ts@@ -147,6 +147,7 @@ export interface NativeEngineApi {renderer_getCapturedTextureCount?(): number;renderer_getCulled?(): number;renderer_getDrawCalls?(): number;+ renderer_getEstimatedGpuBytes?(): number;renderer_getGpuTimeMs?(): number;renderer_getMeshes?(): number;renderer_getPreviewHeight?(): number;@@ -431,6 +432,7 @@ export function createNativeEngineApi(bind('renderer_getCapturedTextureCount', 'es_renderer_getCapturedTextureCount', false);bind('renderer_getCulled', 'es_renderer_getCulled', false);bind('renderer_getDrawCalls', 'es_renderer_getDrawCalls', false);+ bind('renderer_getEstimatedGpuBytes', 'es_renderer_getEstimatedGpuBytes', false);bind('renderer_getGpuTimeMs', 'es_renderer_getGpuTimeMs', false);bind('renderer_getMeshes', 'es_renderer_getMeshes', false);bind('renderer_getPreviewHeight', 'es_renderer_getPreviewHeight', false);diff --git a/sdk/src/runtime/frameStats.ts b/sdk/src/runtime/frameStats.tsindex b90c027d3..00e81a4fc 100644--- a/sdk/src/runtime/frameStats.ts+++ b/sdk/src/runtime/frameStats.ts@@ -50,7 +50,7 @@ export function frameStatsReport(app: App): FrameStatsReport {cppCounters: jsonMap(m?.engine_getCounters?.()),gpuScopes: jsonMap(m?.engine_getGpuScopes?.()),wasmBytes: app.wasmModule?.HEAPU8?.byteLength ?? 0,- vramBytes: m?.renderer_getTextureBytes?.() ?? 0,+ vramBytes: m?.renderer_getEstimatedGpuBytes?.() ?? 0,quality: app.hasResource(Quality) ? app.getResource(Quality)?.report() : undefined,};}diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.tsindex 13736b511..b97be8198 100644--- a/sdk/src/wasm.ts+++ b/sdk/src/wasm.ts@@ -651,6 +651,8 @@ export interface ESEngineModule {engine_getGpuScopes?(): string;/** Resident texture VRAM (RGBA8 estimate, bytes) for the profiler's memory pillar. */renderer_getTextureBytes?(): number;+ /** Nominal live GPU buffer/texture storage, including target attachments; excludes driver overhead. */+ renderer_getEstimatedGpuBytes?(): number;renderer_setClearColor(r: number, g: number, b: number, a: number): void;renderer_setViewport(x: number, y: number, w: number, h: number): void;/** Bitmask of layers 0..31 that y-sort within the layer (top-down occlusion). */diff --git a/sdk/tests/native-frame-stats.test.ts b/sdk/tests/native-frame-stats.test.tsindex 636043108..1e05b4edc 100644--- a/sdk/tests/native-frame-stats.test.ts+++ b/sdk/tests/native-frame-stats.test.ts@@ -22,6 +22,7 @@ function nativeEngine() {renderer_getSprites: () => 7,renderer_getGpuTimeMs: () => 1.5,renderer_getTextureBytes: () => 4096,+ renderer_getEstimatedGpuBytes: () => 12288,engine_getCpuScopes: () => JSON.stringify({ 'render.submit': 0.8 }),engine_getCounters: () => JSON.stringify({ batches: 3 }),engine_getGpuScopes: () => JSON.stringify({ main: 1.2 }),@@ -46,7 +47,7 @@ describe('frame stats on a device with no wasm module', () => {const report = frameStatsReport(App.new());expect(report).toMatchObject({- drawCalls: 10, triangles: 36, sprites: 7, gpuMs: 1.5, vramBytes: 4096,+ drawCalls: 10, triangles: 36, sprites: 7, gpuMs: 1.5, vramBytes: 12288,cppScopes: { 'render.submit': 0.8 },cppCounters: { batches: 3 },gpuScopes: { main: 1.2 },@@ -68,7 +69,7 @@ describe('frame stats on a device with no wasm module', () => {expect(engine.engine_setCpuProfiling.mock.calls).toEqual([[true], [false]]);const frame = rec.take().frames[0];expect(frame.gpuMs).toBe(1.5);- expect(frame.memory?.vramBytes).toBe(4096);+ expect(frame.memory?.vramBytes).toBe(12288);expect(frame.counters).toEqual({ batches: 3 });});});diff --git a/src/esengine/bindings/RendererBindings.cpp b/src/esengine/bindings/RendererBindings.cppindex 1f0259bf5..993f03279 100644--- a/src/esengine/bindings/RendererBindings.cpp+++ b/src/esengine/bindings/RendererBindings.cpp@@ -996,6 +996,10 @@ f64 renderer_getTextureBytes() {return 0.0;}+f64 renderer_getEstimatedGpuBytes() {+ return g_device ? static_cast<f64>(g_device->estimatedGpuBytes()) : 0.0;+}+// f64 rather than u32: the epoch is a counter nothing resets, and a session that// outlives 4 billion invalidations should not wrap into a stamp that matches.f64 renderer_programEpoch() {diff --git a/src/esengine/bindings/RendererBindings.hpp b/src/esengine/bindings/RendererBindings.hppindex 5d178df5d..b63712ad1 100644--- a/src/esengine/bindings/RendererBindings.hpp+++ b/src/esengine/bindings/RendererBindings.hpp@@ -177,6 +177,7 @@ std::string engine_getCpuScopes();std::string engine_getCounters();std::string engine_getGpuScopes();f64 renderer_getTextureBytes();+f64 renderer_getEstimatedGpuBytes();/** @brief The renderer's current program-readiness epoch. A readiness record* stamped with an older one names programs that may be gone. */diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cppindex 627fc59fc..d5e0f5a79 100644--- a/src/esengine/bindings/WebSDKEntry.cpp+++ b/src/esengine/bindings/WebSDKEntry.cpp@@ -744,6 +744,7 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {emscripten::function("engine_getCounters", &esengine::engine_getCounters);emscripten::function("engine_getGpuScopes", &esengine::engine_getGpuScopes);emscripten::function("renderer_getTextureBytes", &esengine::renderer_getTextureBytes);+ emscripten::function("renderer_getEstimatedGpuBytes", &esengine::renderer_getEstimatedGpuBytes);emscripten::function("renderer_programEpoch", &esengine::renderer_programEpoch);emscripten::function("renderer_deviceGeneration", &esengine::renderer_deviceGeneration);emscripten::function("engine_prepareMeshPrograms", &esengine::engine_prepareMeshPrograms);diff --git a/src/esengine/renderer/rhi/GfxDevice.cpp b/src/esengine/renderer/rhi/GfxDevice.cppindex 8b1455eb9..252c20136 100644--- a/src/esengine/renderer/rhi/GfxDevice.cpp+++ b/src/esengine/renderer/rhi/GfxDevice.cpp@@ -791,6 +791,35 @@ u32 GfxDevice::createTimerQuery() {return id;}+u64 GfxDevice::estimatedGpuBytes() const {+ if (!isDeviceUsable()) return 0;+ u64 bytes = 0;+ for (const auto& [id, buffer] : registry_.buffers.all()) {+ if (buffer.realized) bytes += buffer.desc.size;+ }+ // Framebuffers reference these textures; counting attachments again doubles them.+ for (const auto& [id, texture] : registry_.textures.all()) {+ if (!texture.realized) continue;+ u32 width = texture.desc.width, height = texture.desc.height;+ if (!width || !height) continue;+ const auto block = gfxCompressedBlockInfo(texture.compressedFormat);+ const u32 levels = texture.compressed ? texture.mipLevels+ : texture.desc.mipmaps && texture.desc.samples <= 1 && !isDepthFormat(texture.desc.format) ? 32 : 1;+ for (u32 level = 0; level < levels; ++level) {+ if (texture.compressed) {+ bytes += ((static_cast<u64>(width) + block.blockWidth - 1) / block.blockWidth)+ * ((static_cast<u64>(height) + block.blockHeight - 1) / block.blockHeight) * block.bytesPerBlock;+ } else {+ bytes += static_cast<u64>(width) * height * gfxBytesPerPixel(texture.desc.format)+ * std::max(1u, texture.desc.samples);+ }+ if (width == 1 && height == 1) break;+ width = std::max(1u, width / 2); height = std::max(1u, height / 2);+ }+ }+ return bytes;+}+GfxLiveObjects GfxDevice::liveObjects() const {return GfxLiveObjects{registry_.buffers.size(),diff --git a/src/esengine/renderer/rhi/GfxDevice.hpp b/src/esengine/renderer/rhi/GfxDevice.hppindex ff93d0488..4c7f16ebc 100644--- a/src/esengine/renderer/rhi/GfxDevice.hpp+++ b/src/esengine/renderer/rhi/GfxDevice.hpp@@ -723,6 +723,10 @@ public:/** @brief GPU objects alive right now (see {@link GfxLiveObjects}), read off the registry. */GfxLiveObjects liveObjects() const;+ /** Estimated live buffer/texture storage, including target attachments.+ * Excludes driver overhead, shader binaries and host-owned swapchain images. */+ virtual u64 estimatedGpuBytes() const;+/*** @brief The backend's own name for the object behind `texture`, or 0.* @details For a host that writes the contents itself — a JS video frame ordiff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cppindex 58b31307f..90fcefa8b 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp@@ -674,6 +674,41 @@ void WebGPUDevice::setScissor(i32 x, i32 y, i32 w, i32 h) {// Buffers// =============================================================================+u64 WebGPUDevice::estimatedGpuBytes() const {+ if (!device_ || !isDeviceUsable()) return 0;+ u64 bytes = 0;+ const auto bufferBytes = [](WGPUBuffer buffer) -> u64 { return buffer ? wgpuBufferGetSize(buffer) : 0; };+ for (const auto& [id, buffer] : buffers_) bytes += bufferBytes(buffer.buffer);+ bytes += bufferBytes(dummy_ubo_.buffer) + bufferBytes(clear_color_buffer_) + bufferBytes(timestamp_resolve_);+ for (const auto& slot : gpu_time_ring_) bytes += bufferBytes(slot.buf);+ for (const auto& [id, readback] : readbacks_) bytes += bufferBytes(readback.buffer);+ const auto textureBytes = [](WGPUTexture texture) -> u64 {+ if (!texture) return 0;+ u32 width = wgpuTextureGetWidth(texture), height = wgpuTextureGetHeight(texture);+ const auto format = wgpuTextureGetFormat(texture);+ const u32 pixelBytes = format == WGPUTextureFormat_RGBA32Float ? 16 : format == WGPUTextureFormat_RGBA16Float ? 8 : 4;+ GfxBlockInfo block{1, 1, pixelBytes};+ for (const auto compressed : {GfxCompressedFormat::ETC2_RGB8, GfxCompressedFormat::ETC2_RGBA8,+ GfxCompressedFormat::ASTC_4x4, GfxCompressedFormat::ASTC_8x8, GfxCompressedFormat::S3TC_DXT1,+ GfxCompressedFormat::S3TC_DXT5, GfxCompressedFormat::ETC2_RGBA8_SRGB,+ GfxCompressedFormat::ASTC_4x4_SRGB, GfxCompressedFormat::S3TC_DXT5_SRGB}) {+ if (format == toWGPUCompressedFormat(compressed)) { block = gfxCompressedBlockInfo(compressed); break; }+ }+ u64 size = 0;+ for (u32 level = 0; level < wgpuTextureGetMipLevelCount(texture); ++level) {+ size += ((static_cast<u64>(width) + block.blockWidth - 1) / block.blockWidth)+ * ((static_cast<u64>(height) + block.blockHeight - 1) / block.blockHeight) * block.bytesPerBlock+ * wgpuTextureGetSampleCount(texture);+ width = std::max(1u, width / 2); height = std::max(1u, height / 2);+ }+ return size * wgpuTextureGetDepthOrArrayLayers(texture);+ };+ for (const auto& [id, texture] : textures_) bytes += textureBytes(texture.texture);+ bytes += textureBytes(dummy_texture_.texture) + textureBytes(dummy_depth_texture_.texture);+ bytes += textureBytes(surface_depth_texture_) + textureBytes(surface_msaa_texture_);+ return bytes;+}+WGPUBuffer WebGPUDevice::makeBuffer(GfxBufferUsage usage, u32 size, const void* data) {if (!device_) {ES_LOG_ERROR("WebGPUDevice: no device to create a buffer on");diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hppindex 33dc2cc99..c83de3e7a 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp@@ -43,6 +43,7 @@ namespace esengine {class WebGPUDevice final : public GfxDevice {public:+ u64 estimatedGpuBytes() const override;/** @brief Depth-stencil attachment shape of a pass — one WGPURenderPipeline* per shape, since WebGPU validates a pipeline's depthStencil state* against the pass it draws into (GL has no such coupling). */diff --git a/tests/renderer/test_buffer_device.cpp b/tests/renderer/test_buffer_device.cppindex e7f171136..95d3b686d 100644--- a/tests/renderer/test_buffer_device.cpp+++ b/tests/renderer/test_buffer_device.cpp@@ -91,6 +91,44 @@ int main() {CHECK(d.setVertexBufferCalls == 0, "bind on an empty-layout geometry is a no-op");}+ // Storage follows allocations, not uploads or framebuffer references.+ {+ MockGfxDevice d;+ const auto buffer = d.createBuffer({GfxBufferUsage::Vertex, 32, true}, GfxContent::transient(), nullptr);+ TextureDesc image; image.width = 8; image.height = 4; image.mipmaps = true;+ const auto texture = d.createTexture(image, GfxContent::transient(), nullptr);+ CHECK(d.estimatedGpuBytes() == 32 + 172, "estimate includes buffer capacity and the whole mip chain");+ d.resizeBuffer(buffer, 128, nullptr);+ CHECK(d.estimatedGpuBytes() == 128 + 172, "buffer growth updates its allocated storage");+ TextureDesc target; target.width = 16; target.height = 8; target.samples = 4;+ const auto color = d.createTexture(target, GfxContent::transient(), nullptr);+ target.format = GfxPixelFormat::Depth24Stencil8;+ const auto depth = d.createTexture(target, GfxContent::transient(), nullptr);+ FramebufferDesc fb; fb.color0 = color; fb.depthStencil = depth;+ const auto first = d.createFramebuffer(fb), second = d.createFramebuffer(fb);+ CHECK(d.estimatedGpuBytes() == 128 + 172 + 4096, "shared MSAA/depth attachments count once");+ d.deleteFramebuffer(first); d.deleteFramebuffer(second);+ CHECK(d.estimatedGpuBytes() == 128 + 172 + 4096, "dropping framebuffer references keeps attachment storage");+ d.deleteTexture(color); d.deleteTexture(depth); d.deleteTexture(texture); d.deleteBuffer(buffer);+ CHECK(d.estimatedGpuBytes() == 0, "destroyed allocations leave no estimated bytes");+ TextureDesc compressed; compressed.width = 10; compressed.height = 9;+ const auto packed = d.createCompressedTexture(compressed, GfxContent::transient(),+ GfxCompressedFormat::S3TC_DXT1, nullptr, 0, 3);+ CHECK(d.estimatedGpuBytes() == 72 + 16 + 8, "compressed edge blocks and supplied mip levels use block storage");+ d.deleteTexture(packed);+ d.createBufferSucceeds = false;+ d.createBuffer({GfxBufferUsage::Vertex, 4096, false}, GfxContent::transient(), nullptr);+ CHECK(d.estimatedGpuBytes() == 0, "failed allocations do not inflate memory");+ d.createBufferSucceeds = true;+ const std::vector<u8> retainedBytes(256, 7);+ const auto retained = d.createBuffer({GfxBufferUsage::Uniform, 256, false}, GfxContent::retained(), retainedBytes.data());+ d.notifyDeviceLost(GfxDeviceLostReason::ContextLost, "test");+ CHECK(d.estimatedGpuBytes() == 0, "a lost device has no live GPU storage");+ CHECK(d.recoverDevice(), "device recovery succeeds");+ CHECK(d.estimatedGpuBytes() == 256, "recovery restores storage without counting CPU retention twice");+ d.deleteBuffer(retained);+ }+if (g_failures == 0) {std::printf("\nALL BUFFER/GEOMETRY TESTS PASSED\n");return 0;diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 0cfc0ef75..4fe9b5038 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,10 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(profiler): label live gpu storage estimates':+ { note: 'GPU memory estimates include buffers and render targets.' },+ 'fix(profiler): account for live gpu resource storage':+ { note: 'GPU memory estimates include buffers and render targets.' },'fix(profiler): capture every frame from the play realm':{ note: 'Play-mode performance captures include every recorded frame.' },'fix(physics): retain edits made to inactive 2d bodies':