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): expose resident texture VRAM; trim profiler comments

esengine64faed0dce99
diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts
index c69b31577..70d900228 100644
--- a/sdk/src/wasm.ts
+++ b/sdk/src/wasm.ts
@@ -294,6 +294,8 @@ export interface ESEngineModule {
engine_setCpuProfiling?(on: boolean): void;
/** Last frame's CPU scopes as a JSON object {"render.submit": ms, …}. */
engine_getCpuScopes?(): string;
+ /** Resident texture VRAM (RGBA8 estimate, bytes) for the profiler's memory pillar. */
+ renderer_getTextureBytes?(): number;
renderer_setClearColor(r: number, g: number, b: number, a: number): void;
renderer_setViewport(x: number, y: number, w: number, h: number): void;
renderer_setScissor(x: number, y: number, w: number, h: number, enable: boolean): void;
diff --git a/src/esengine/bindings/RendererBindings.cpp b/src/esengine/bindings/RendererBindings.cpp
index cfdac46c5..693da7488 100644
--- a/src/esengine/bindings/RendererBindings.cpp
+++ b/src/esengine/bindings/RendererBindings.cpp
@@ -419,8 +419,6 @@ f32 renderer_getGpuTimeMs() {
return g_renderFrame->stats().gpu_time_ms;
}
-// Per-frame CPU scope profiling (FrameProfiler): the editor turns it on and reads
-// the last frame's `cpp.*` scopes as a JSON object to fold into its breakdown.
void engine_setCpuProfiling(bool on) {
FrameProfiler::get().setEnabled(on);
}
@@ -429,6 +427,13 @@ std::string engine_getCpuScopes() {
return FrameProfiler::get().lastJson();
}
+f64 renderer_getTextureBytes() {
+ if (auto* rm = ctx().tryGet<resource::ResourceManager>()) {
+ return static_cast<f64>(rm->getStats().textureBytes);
+ }
+ return 0.0;
+}
+
void renderer_setDeltaTime(f32 dt) {
ctx().state().delta_time = dt;
}
diff --git a/src/esengine/bindings/RendererBindings.hpp b/src/esengine/bindings/RendererBindings.hpp
index 4275f10db..151758d1a 100644
--- a/src/esengine/bindings/RendererBindings.hpp
+++ b/src/esengine/bindings/RendererBindings.hpp
@@ -109,6 +109,7 @@ u32 renderer_getCulled();
f32 renderer_getGpuTimeMs();
void engine_setCpuProfiling(bool on);
std::string engine_getCpuScopes();
+f64 renderer_getTextureBytes();
void renderer_setDeltaTime(f32 dt);
void renderer_setClearColor(f32 r, f32 g, f32 b, f32 a);
void renderer_setViewport(i32 x, i32 y, i32 w, i32 h);
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index 31cc5aab0..ea6159b54 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -521,6 +521,7 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {
emscripten::function("renderer_getGpuTimeMs", &esengine::renderer_getGpuTimeMs);
emscripten::function("engine_setCpuProfiling", &esengine::engine_setCpuProfiling);
emscripten::function("engine_getCpuScopes", &esengine::engine_getCpuScopes);
+ emscripten::function("renderer_getTextureBytes", &esengine::renderer_getTextureBytes);
emscripten::function("renderer_setDeltaTime", &esengine::renderer_setDeltaTime);
emscripten::function("renderer_setClearColor", &esengine::renderer_setClearColor);
emscripten::function("renderer_setViewport", &esengine::renderer_setViewport);
diff --git a/src/esengine/core/FrameProfiler.hpp b/src/esengine/core/FrameProfiler.hpp
index 1e4983288..4aa773a72 100644
--- a/src/esengine/core/FrameProfiler.hpp
+++ b/src/esengine/core/FrameProfiler.hpp
@@ -3,18 +3,7 @@
#pragma once
/**
* @file FrameProfiler.hpp
- * @brief Per-frame CPU scope timing for the editor profiler (C++ side).
- *
- * `ES_PROFILE_SCOPE("render.submit")` times its block and accumulates the ms into
- * a per-frame table — the C++ counterpart of the editor's TS `PerfMonitor.measure`
- * and the analog of UE's SCOPE_CYCLE_COUNTER / TRACE_CPUPROFILER_EVENT_SCOPE. Both
- * feed ONE per-frame breakdown: the TS layer reads these scopes over a binding and
- * shows them as `cpp.*` rows alongside its own zones, so a spike inside the engine
- * (culling vs draw submission vs a physics step) is named, not a black box.
- *
- * `commit()` snapshots the frame (called once at the end of the render) into a
- * stable buffer the binding reads. Header-only + near-free when disabled (the
- * scope short-circuits), so it can live on in the editor with no measurable cost.
+ * @brief Per-frame CPU scope timing (ES_PROFILE_SCOPE) read by the editor profiler.
*/
#include <vector>
#include <string>
@@ -26,22 +15,14 @@
namespace esengine {
-/** High-resolution wall clock (ms). performance.now() under emscripten. */
inline double es_profile_now_ms() {
#ifdef __EMSCRIPTEN__
return emscripten_get_now();
#else
- return 0.0; // the editor profiler is web-only; native builds don't time scopes
+ return 0.0;
#endif
}
-/**
- * Frame-scoped CPU scope collector (singleton). Accumulates inclusive time per
- * named scope for the frame in progress, then `commit()` swaps it to `last_` —
- * the last COMPLETE frame the binding serializes. Scope names are string literals
- * (stable for the program's life), so entries store the pointer and dedupe by
- * strcmp (a handful of scopes per frame — negligible).
- */
class FrameProfiler {
public:
static FrameProfiler& get() {
@@ -59,13 +40,11 @@ public:
cur_.push_back({name, ms});
}
- /** Close the frame: the accumulated scopes become the readable snapshot. */
void commit() {
last_.swap(cur_);
cur_.clear();
}
- /** The last complete frame's scopes as a JSON object {"name": ms, …}. */
std::string lastJson() const {
std::string s = "{";
for (std::size_t i = 0; i < last_.size(); ++i) {
@@ -88,7 +67,6 @@ private:
bool enabled_ = false;
};
-/** RAII scope: times its block into the FrameProfiler when profiling is enabled. */
class ScopeTimer {
public:
explicit ScopeTimer(const char* name)
@@ -109,7 +87,6 @@ private:
#define ES_PROFILE_CONCAT_(a, b) a##b
#define ES_PROFILE_CONCAT(a, b) ES_PROFILE_CONCAT_(a, b)
-/** Time the enclosing block as a named CPU scope (a `cpp.<name>` profiler row). */
#define ES_PROFILE_SCOPE(name) ::esengine::ScopeTimer ES_PROFILE_CONCAT(es_scope_, __LINE__)(name)
} // namespace esengine
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index b84d59528..7ea62faaf 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -34,8 +34,8 @@ namespace esengine {
void GpuTimer::ensureInit() {
if (state_ != 0) return;
#ifdef __EMSCRIPTEN__
- // The extension must be ENABLED (not just present) for emscripten to route the
- // TIME_ELAPSED query entry points; enable returns false when it's unavailable.
+ // Must ENABLE the extension (not just check presence) so emscripten routes the
+ // TIME_ELAPSED query entry points.
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_get_current_context();
if (ctx && emscripten_webgl_enable_extension(ctx, "EXT_disjoint_timer_query_webgl2")) {
glGenQueries(kRing, queries_);
@@ -43,12 +43,12 @@ void GpuTimer::ensureInit() {
return;
}
#endif
- state_ = 2; // unavailable — stays -1ms
+ state_ = 2;
}
void GpuTimer::begin() {
ensureInit();
- if (state_ != 1 || inflight_[write_]) return; // unavailable or ring full → skip
+ if (state_ != 1 || inflight_[write_]) return;
glBeginQuery(GL_TIME_ELAPSED_EXT, queries_[write_]);
active_ = true;
}
@@ -65,7 +65,7 @@ void GpuTimer::poll() {
if (state_ != 1) return;
GLint disjoint = 0;
glGetIntegerv(GL_GPU_DISJOINT_EXT, &disjoint);
- if (disjoint) { // GPU timing was disturbed this window — drop everything in flight
+ if (disjoint) { // timing disturbed — drop everything in flight
for (int i = 0; i < kRing; ++i) inflight_[i] = false;
read_ = write_;
return;
@@ -73,7 +73,7 @@ void GpuTimer::poll() {
while (inflight_[read_]) {
GLuint avail = 0;
glGetQueryObjectuiv(queries_[read_], GL_QUERY_RESULT_AVAILABLE, &avail);
- if (!avail) break; // oldest not ready yet — try again next frame
+ if (!avail) break;
GLuint ns = 0;
glGetQueryObjectuiv(queries_[read_], GL_QUERY_RESULT, &ns);
last_ms_ = static_cast<f32>(ns) / 1.0e6f;
@@ -258,10 +258,9 @@ void RenderFrame::flush() {
context_.updateFrameConstants(view_projection_);
context_.lights().uploadAndBind();
- // GPU time for the scene draw: read back prior frames, then bracket this one.
gpu_timer_.poll();
{
- ES_PROFILE_SCOPE("render.submit"); // CPU cost of issuing the draw calls
+ ES_PROFILE_SCOPE("render.submit");
gpu_timer_.begin();
draw_list_.execute(device_, pool_, context_.materials(), &frame_capture_);
gpu_timer_.end();
@@ -322,8 +321,7 @@ void RenderFrame::end() {
in_frame_ = false;
flushed_ = false;
- // Render is the last C++ work in a frame; physics/animation/… scopes ran
- // earlier this frame and are already accumulated. Snapshot them all now.
+ // Render is the frame's last C++ work: earlier scopes are already accumulated.
FrameProfiler::get().commit();
}
@@ -559,7 +557,7 @@ void RenderFrame::collectLights(ecs::Registry& registry) {
}
void RenderFrame::collectAll(ecs::Registry& registry, u32 skipFlags) {
- ES_PROFILE_SCOPE("render.collect"); // cull + build the draw list from the ECS
+ ES_PROFILE_SCOPE("render.collect");
buildClipState();
collectLights(registry);
diff --git a/src/esengine/renderer/RenderFrame.hpp b/src/esengine/renderer/RenderFrame.hpp
index 6973d3255..35e750840 100644
--- a/src/esengine/renderer/RenderFrame.hpp
+++ b/src/esengine/renderer/RenderFrame.hpp
@@ -38,16 +38,13 @@ struct Frustum {
bool intersectsAABB(const glm::vec3& center, const glm::vec3& halfExtents) const;
};
-// Per-frame GPU timer over EXT_disjoint_timer_query_webgl2 (GL_TIME_ELAPSED). A
-// small ring of query objects so readback never stalls the CPU; reports the most
-// recent completed frame's GPU ms, or -1 when unavailable. Only POD state lives
-// here — the methods (which touch GL / emscripten) are defined in RenderFrame.cpp
-// so no GL headers leak through this widely-included header.
+// GPU frame timer (EXT_disjoint_timer_query). POD state only; the GL methods live
+// in RenderFrame.cpp so no GL headers leak through this header.
class GpuTimer {
public:
- void begin(); // bracket the frame's draw submission (no-op if unavailable)
+ void begin();
void end();
- void poll(); // read back finished queries → last_ms_
+ void poll();
f32 lastMs() const { return last_ms_; }
private:
@@ -76,9 +73,7 @@ public:
u32 particles = 0;
u32 shapes = 0;
u32 culled = 0;
- // Last completed frame's GPU time (ms) via EXT_disjoint_timer_query, or -1
- // when the timer isn't available (extension missing / driver disjoint).
- f32 gpu_time_ms = -1.0f;
+ f32 gpu_time_ms = -1.0f; // -1 when the timer is unavailable
};
RenderFrame(GfxDevice& device, RenderContext& context,