Skip to content

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.

feat(engine): the GPU objects nobody was counting

esengineac7fa920296f
diff --git a/src/esengine/bindings/RendererBindings.cpp b/src/esengine/bindings/RendererBindings.cpp
index a078cecab..8f347d121 100644
--- a/src/esengine/bindings/RendererBindings.cpp
+++ b/src/esengine/bindings/RendererBindings.cpp
@@ -497,6 +497,25 @@ u32 renderer_getDrawCalls() {
return g_renderFrame->stats().draw_calls;
}
+#ifdef __EMSCRIPTEN__
+emscripten::val renderer_getLiveObjects() {
+ auto* device = g_device;
+ // Null rather than zeros: with no device there is nothing to report, and a
+ // census that recorded 0 live textures would read as "everything was freed".
+ if (!device) return emscripten::val::null();
+ const GfxLiveObjects live = device->liveObjects();
+ auto out = emscripten::val::object();
+ out.set("buffers", static_cast<f64>(live.buffers));
+ out.set("textures", static_cast<f64>(live.textures));
+ out.set("programs", static_cast<f64>(live.programs));
+ out.set("layouts", static_cast<f64>(live.layouts));
+ out.set("pipelines", static_cast<f64>(live.pipelines));
+ out.set("renderTargets", static_cast<f64>(live.renderTargets));
+ out.set("readbacks", static_cast<f64>(live.readbacks));
+ return out;
+}
+#endif // __EMSCRIPTEN__
+
u32 renderer_getTriangles() {
if (!g_renderFrame) return 0;
return g_renderFrame->stats().triangles;
diff --git a/src/esengine/bindings/RendererBindings.hpp b/src/esengine/bindings/RendererBindings.hpp
index 8082584fc..74883f7a6 100644
--- a/src/esengine/bindings/RendererBindings.hpp
+++ b/src/esengine/bindings/RendererBindings.hpp
@@ -92,6 +92,11 @@ u32 renderer_getTargetDepthTexture(u32 handle);
void renderer_releaseTarget(u32 handle);
u32 renderer_getTargetTexture(u32 handle);
u32 renderer_getDrawCalls();
+#ifdef __EMSCRIPTEN__
+/// GPU objects the device has not destroyed, for the resource census. Null when
+/// no device is initialized — absent counters, not zeroed ones.
+emscripten::val renderer_getLiveObjects();
+#endif
u32 renderer_getTriangles();
u32 renderer_getSprites();
#ifdef ES_ENABLE_SPINE
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index 967c819b7..5d68792f8 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -69,6 +69,7 @@
#include <cstdlib>
#include <cstring>
#include <cstddef>
+#include <malloc.h>
#include <sstream>
static_assert(sizeof(void*) == 4, "EM_JS pointer passing assumes wasm32 (4-byte pointers)");
@@ -88,6 +89,14 @@ static EstellaContext& ctx() { return activeCtx(); }
#define g_particleSystem (ctx().tryGet<particle::ParticleSystem>())
#endif
+// Bytes malloc has handed out and not got back, for the resource census. The
+// reserved heap size cannot answer this: emscripten's heap only grows, so a leak
+// hides until it crosses a growth step. This is exact, and it falls on free.
+f64 es_getMallocBytes() {
+ const struct mallinfo mi = mallinfo();
+ return static_cast<f64>(static_cast<u32>(mi.uordblks));
+}
+
// Cook-time introspection: assembles both GLSL stages exactly as the runtime would
// (same parser + injected headers), plus the reflection a GLSL→WGSL converter needs
// (texture params → sampler units). The export cook feeds the assembled stages through
@@ -380,6 +389,7 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {
emscripten::function("material_compileEsshader", &esengine::material_compileEsshader);
emscripten::function("esshader_cookInfo", &esengine::esshader_cookInfo);
+ emscripten::function("es_getMallocBytes", &esengine::es_getMallocBytes);
emscripten::function("material_define", &esengine::material_define);
emscripten::function("material_setUniform", &esengine::material_setUniform);
emscripten::function("material_setTexture", &esengine::material_setTexture);
@@ -512,6 +522,7 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {
emscripten::function("renderer_getTargetTexture", &esengine::renderer_getTargetTexture);
emscripten::function("renderer_getTargetDepthTexture", &esengine::renderer_getTargetDepthTexture);
emscripten::function("renderer_getDrawCalls", &esengine::renderer_getDrawCalls);
+ emscripten::function("renderer_getLiveObjects", &esengine::renderer_getLiveObjects);
emscripten::function("renderer_getTriangles", &esengine::renderer_getTriangles);
emscripten::function("renderer_getSprites", &esengine::renderer_getSprites);
#ifdef ES_ENABLE_SPINE
diff --git a/src/esengine/bindings/modules/physics/PhysicsBindings.hpp b/src/esengine/bindings/modules/physics/PhysicsBindings.hpp
index f469ac71b..63b6da1fe 100644
--- a/src/esengine/bindings/modules/physics/PhysicsBindings.hpp
+++ b/src/esengine/bindings/modules/physics/PhysicsBindings.hpp
@@ -49,6 +49,12 @@ void physics_step(float dt);
void physics_setBodyTransform(uint32_t entityId, float x, float y, float angle);
void physics_setBodyTargetTransform(uint32_t entityId, float x, float y, float angle, float dt);
int physics_getDynamicBodyCount();
+// Resource-census counters: what physics is still holding, including the static
+// and kinematic bodies the dynamic readback cannot see.
+int physics_getBodyCount();
+int physics_getShapeCount();
+int physics_getJointCount();
+int physics_getTrackingRows();
// @heapreturn physics_dynamicBodyTransformsBytes()
uintptr_t physics_getDynamicBodyTransforms();
// Render interpolation. The module keeps the two most recent fixed-step snapshots
diff --git a/src/esengine/bindings/modules/physics/PhysicsModuleEntry.cpp b/src/esengine/bindings/modules/physics/PhysicsModuleEntry.cpp
index 4943fa941..c6497c463 100644
--- a/src/esengine/bindings/modules/physics/PhysicsModuleEntry.cpp
+++ b/src/esengine/bindings/modules/physics/PhysicsModuleEntry.cpp
@@ -255,6 +255,34 @@ int physics_getDynamicBodyCount() {
return static_cast<int>(g_ctx.dynamicBodyEntities.size());
}
+// For the resource census. The dynamic count was the only one reachable, and it
+// is the subset that cannot show the leak worth finding: a static body outliving
+// its entity still collides, and never appears in a dynamic-body readback.
+EMSCRIPTEN_KEEPALIVE
+int physics_getBodyCount() {
+ return static_cast<int>(g_ctx.entityToBody.size());
+}
+
+EMSCRIPTEN_KEEPALIVE
+int physics_getShapeCount() {
+ size_t total = 0;
+ for (const auto& [entityId, shapes] : g_ctx.entityToShapes) total += shapes.size();
+ return static_cast<int>(total);
+}
+
+EMSCRIPTEN_KEEPALIVE
+int physics_getJointCount() {
+ return static_cast<int>(g_ctx.entityToJoint.size());
+}
+
+/// Rows in the side tables keyed by entity. They are invisible to gameplay and
+/// are exactly where a destroy path that forgets one map shows up.
+EMSCRIPTEN_KEEPALIVE
+int physics_getTrackingRows() {
+ return static_cast<int>(g_ctx.entityToShapes.size() + g_ctx.oneWayNormals.size()
+ + g_ctx.posePrevIndex.size() + g_ctx.dynamicBodyEntities.size());
+}
+
EMSCRIPTEN_KEEPALIVE
uintptr_t physics_getDynamicBodyTransforms() {
g_ctx.dynamicTransformBuffer.clear();
diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cpp
index d4d0c66cd..58be42cb1 100644
--- a/src/esengine/renderer/rhi/GLDevice.cpp
+++ b/src/esengine/renderer/rhi/GLDevice.cpp
@@ -486,11 +486,15 @@ ShaderHandle GLDevice::createProgram(const GfxShaderSource& source,
glDeleteShader(fragmentShader);
if (outFailedStage) *outFailedStage = GfxShaderStage::None;
+ ++live_programs_;
return ShaderHandle{program};
}
void GLDevice::deleteProgram(ShaderHandle program) {
- if (program != ShaderHandle::Invalid) glDeleteProgram(static_cast<GLuint>(program));
+ if (program != ShaderHandle::Invalid) {
+ glDeleteProgram(static_cast<GLuint>(program));
+ if (live_programs_ > 0) --live_programs_;
+ }
}
void GLDevice::useProgram(ShaderHandle program) {
@@ -1230,4 +1234,19 @@ bool GLDevice::supportsFloatTargets() {
|| glExtensionPresent("EXT_color_buffer_float");
}
+GfxLiveObjects GLDevice::liveObjects() const {
+ // Buffers and textures need no counter: the per-handle metadata maps are
+ // written on create and erased on delete, so their size IS the live set —
+ // and unlike a parallel counter it cannot drift out of agreement with them.
+ return GfxLiveObjects{
+ static_cast<u32>(buffer_meta_.size()),
+ static_cast<u32>(texture_formats_.size()),
+ live_programs_,
+ static_cast<u32>(layouts_.size()),
+ static_cast<u32>(pipelines_.size()),
+ static_cast<u32>(framebuffer_textures_.size()),
+ static_cast<u32>(readbacks_.size()),
+ };
+}
+
} // namespace esengine
diff --git a/src/esengine/renderer/rhi/GLDevice.hpp b/src/esengine/renderer/rhi/GLDevice.hpp
index e9a5403ae..fc4b8e5f9 100644
--- a/src/esengine/renderer/rhi/GLDevice.hpp
+++ b/src/esengine/renderer/rhi/GLDevice.hpp
@@ -122,6 +122,7 @@ public:
u32 getError() override;
std::string getString(GfxStringName name) override;
i32 getInt(GfxIntParam name) override;
+ GfxLiveObjects liveObjects() const override;
private:
// Clear machinery: backend-internal since RenderPassDesc became the only way
@@ -229,6 +230,11 @@ private:
// 0 = unprobed, 1 = timer queries available, 2 = unavailable.
int timer_query_state_ = 0;
+
+ // Linked programs alive. Unlike buffers and textures, programs have no
+ // per-handle metadata map whose size would answer this, and a program leaked
+ // by shader hot reload is the exact failure the census exists to catch.
+ u32 live_programs_ = 0;
};
} // namespace esengine
diff --git a/src/esengine/renderer/rhi/GfxDevice.hpp b/src/esengine/renderer/rhi/GfxDevice.hpp
index ccee7e982..e1244511e 100644
--- a/src/esengine/renderer/rhi/GfxDevice.hpp
+++ b/src/esengine/renderer/rhi/GfxDevice.hpp
@@ -38,6 +38,24 @@ namespace esengine {
/** @brief Sentinel returned by getUniformBlockIndex when a program has no such block (GL_INVALID_INDEX). */
static constexpr u32 GFX_INVALID_UNIFORM_BLOCK = 0xFFFFFFFFu;
+/**
+ * @brief GPU objects the device has created and not yet destroyed.
+ *
+ * @details ResourceManager reports a strict subset — render targets, the
+ * transient pool and plugin-created textures bypass it.
+ * @note Conserved: buffers, textures, programs, renderTargets. Caches that
+ * plateau: layouts, pipelines. `readbacks` is a queue, empty at rest.
+ */
+struct GfxLiveObjects {
+ u32 buffers = 0;
+ u32 textures = 0;
+ u32 programs = 0;
+ u32 layouts = 0;
+ u32 pipelines = 0;
+ u32 renderTargets = 0;
+ u32 readbacks = 0;
+};
+
/**
* @brief Abstract graphics device interface
*
@@ -426,6 +444,15 @@ public:
/** @brief Queries a backend integer capability/limit */
virtual i32 getInt(GfxIntParam name) = 0;
+
+ /**
+ * @brief GPU objects alive right now (see {@link GfxLiveObjects}).
+ * @details Not pure: a backend that cannot answer reports zeros rather than
+ * forcing every device to carry the bookkeeping. The census treats
+ * an all-zero result as "this backend does not report", which is a
+ * different claim than "nothing is allocated" — see censusProbes.ts.
+ */
+ virtual GfxLiveObjects liveObjects() const { return {}; }
};
} // namespace esengine
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
index 7d1c75a3d..4f8f38acb 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
@@ -202,6 +202,18 @@ public:
usize textureCount() const { return textures_.size(); }
usize layoutCount() const { return layouts_.size(); }
usize pipelineDescCount() const { return pipelines_.size(); }
+
+ GfxLiveObjects liveObjects() const override {
+ return GfxLiveObjects{
+ static_cast<u32>(buffers_.size()),
+ static_cast<u32>(textures_.size()),
+ static_cast<u32>(programs_.size()),
+ static_cast<u32>(layouts_.size()),
+ static_cast<u32>(pipelines_.size()),
+ static_cast<u32>(framebuffers_.size()),
+ static_cast<u32>(readbacks_.size()),
+ };
+ }
const VertexLayoutDesc* layoutDesc(VertexLayoutHandle handle) const;
const PipelineDesc* pipelineDesc(PipelineHandle handle) const;