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(editor): fold C++ CPU scopes into the profiler; label idle present

esenginec99a4d8c00c0
diff --git a/desktop/src/components/PerfRealmBridge.tsx b/desktop/src/components/PerfRealmBridge.tsx
index c49406310..148dc6420 100644
--- a/desktop/src/components/PerfRealmBridge.tsx
+++ b/desktop/src/components/PerfRealmBridge.tsx
@@ -32,7 +32,7 @@ export function PerfRealmBridge() {
const s = await PlayRealm.stats();
if (!alive) return;
cache = s
- ? { phaseMs: s.phases, systemMs: s.systems, drawCalls: s.drawCalls, triangles: s.triangles, sprites: s.sprites, entities: s.entities, gpuMs: s.gpuMs }
+ ? { phaseMs: s.phases, systemMs: s.systems, drawCalls: s.drawCalls, triangles: s.triangles, sprites: s.sprites, entities: s.entities, gpuMs: s.gpuMs, cppScopes: s.cppScopes }
: null;
};
void poll();
diff --git a/desktop/src/engine/EngineHost.ts b/desktop/src/engine/EngineHost.ts
index 4bc9916c8..2981abd6d 100644
--- a/desktop/src/engine/EngineHost.ts
+++ b/desktop/src/engine/EngineHost.ts
@@ -122,12 +122,18 @@ class EngineHostImpl {
sprites: number;
entities: number;
gpuMs: number;
+ cppScopes: Record<string, number>;
} | null {
const app = this.app_;
if (!app) return null;
const m = this.module_;
const phases = app.getPhaseTimings();
const systems = app.getSystemTimings();
+ let cppScopes: Record<string, number> = {};
+ const scopesJson = m?.engine_getCpuScopes?.();
+ if (scopesJson) {
+ try { cppScopes = JSON.parse(scopesJson) as Record<string, number>; } catch { /* ignore */ }
+ }
return {
phaseMs: phases ? Object.fromEntries(phases) : {},
systemMs: systems ? Object.fromEntries(systems) : {},
@@ -136,6 +142,7 @@ class EngineHostImpl {
sprites: m?.renderer_getSprites?.() ?? 0,
entities: this.world?.getAllEntities().length ?? 0,
gpuMs: m?.renderer_getGpuTimeMs?.() ?? -1,
+ cppScopes,
};
}
@@ -429,6 +436,8 @@ class EngineHostImpl {
// (PerfMonitor reads it each frame). Cheap — a couple performance.now() per
// system — so left on in edit; the Perf overlay decides what to surface.
app.enableStats();
+ // Per-frame C++ CPU scope profiling (render passes → `cpp.*` in the breakdown).
+ module.engine_setCpuProfiling?.(true);
// Subsystem observability: phase changes push immediately; the sampler
// refreshes derived liveness (stepping↔idle) a couple of times a second.
diff --git a/desktop/src/engine/PerfMonitor.ts b/desktop/src/engine/PerfMonitor.ts
index 1c2d1638a..89379031d 100644
--- a/desktop/src/engine/PerfMonitor.ts
+++ b/desktop/src/engine/PerfMonitor.ts
@@ -62,6 +62,8 @@ export interface EngineFrame {
entities: number;
/** GPU time (ms) via EXT_disjoint_timer_query, or -1 when unavailable. */
gpuMs: number;
+ /** Per-frame C++ CPU scopes (render passes etc.), name → ms — the `cpp.*` rows. */
+ cppScopes: Record<string, number>;
}
/**
@@ -81,6 +83,8 @@ export interface FrameSample {
gpuMs: number;
editorPhases: Record<string, number>;
enginePhases: Record<string, number>;
+ /** C++ CPU scopes for this frame (render.collect/submit/…), name → ms. */
+ cppScopes: Record<string, number>;
systems: Array<{ name: string; ms: number }>;
drawCalls: number;
triangles: number;
@@ -335,9 +339,11 @@ class PerfMonitorImpl {
const ef = this.engineSource?.();
let engineFrameMs = 0;
let enginePhases: Record<string, number> = {};
+ let cppScopes: Record<string, number> = {};
let systemsFrame: Array<{ name: string; ms: number }> = [];
if (ef) {
enginePhases = ef.phaseMs;
+ cppScopes = ef.cppScopes;
engineFrameMs = sumMs(enginePhases);
this.engineSum += engineFrameMs;
for (const k in ef.systemMs) {
@@ -359,6 +365,7 @@ class PerfMonitorImpl {
gpuMs: this.counters.gpuMs >= 0 ? r1(this.counters.gpuMs) : -1,
editorPhases: { ...editorPhases },
enginePhases: { ...enginePhases },
+ cppScopes: { ...cppScopes },
systems: systemsFrame,
drawCalls: this.counters.drawCalls,
triangles: this.counters.triangles,
diff --git a/desktop/src/engine/playProtocol.ts b/desktop/src/engine/playProtocol.ts
index 6cd617f07..7dde6a03b 100644
--- a/desktop/src/engine/playProtocol.ts
+++ b/desktop/src/engine/playProtocol.ts
@@ -65,6 +65,7 @@ export interface PlayStatsReply {
sprites: number;
entities: number;
gpuMs: number;
+ cppScopes: Record<string, number>;
}
/** editor → realm. Discriminated by `type`. */
diff --git a/desktop/src/panels/ProfilerPanel.tsx b/desktop/src/panels/ProfilerPanel.tsx
index 3dc4e1a2f..b3bcedfe2 100644
--- a/desktop/src/panels/ProfilerPanel.tsx
+++ b/desktop/src/panels/ProfilerPanel.tsx
@@ -130,6 +130,10 @@ export function ProfilerPanel() {
const frame = Math.max(v.frameMs, v.engineMs + v.editorMs);
const other = Math.max(0, frame - v.engineMs - v.editorMs);
const p99Bad = s.p99 >= BUDGET_MS * 1.5;
+ // A near-budget frame's leftover is just the browser idle-waiting for vsync — not
+ // a hot spot. Only on a spike is the leftover genuinely unattributed work.
+ const isIdle = v.frameMs <= BUDGET_MS * 1.25;
+ const presentLabel = isIdle ? 'idle' : 'present';
// Pinned-frame attribution (PP7): the long tasks that hit this frame (the usual
// cause of an un-instrumented spike) + every measured phase, ranked. `longTaskRev`
@@ -141,6 +145,7 @@ export function ProfilerPanel() {
? [
...Object.entries(pinned.editorPhases),
...Object.entries(pinned.enginePhases).map(([n, m]) => [`engine.${n}`, m] as [string, number]),
+ ...Object.entries(pinned.cppScopes).map(([n, m]) => [`cpp.${n}`, m] as [string, number]),
]
.filter(([, ms]) => ms >= 0.1)
.sort((a, b) => b[1] - a[1])
@@ -203,7 +208,7 @@ export function ProfilerPanel() {
<h4>Unit <span className="prof-realm">· {s.realm}</span></h4>
<Seg label="engine" ms={v.engineMs} frame={frame} color="var(--run, #46a04a)" />
<Seg label="editor" ms={v.editorMs} frame={frame} color="var(--star, #2f88d6)" />
- <Seg label="present" ms={other} frame={frame} color="var(--text-mute, #888)" />
+ <Seg label={presentLabel} ms={other} frame={frame} color="var(--text-mute, #888)" />
{v.gpuMs >= 0 ? (
<Seg label="gpu" ms={v.gpuMs} frame={frame} color="#3fb2b2" />
) : (
diff --git a/desktop/src/playHost.ts b/desktop/src/playHost.ts
index e3bda4feb..a0c52069f 100644
--- a/desktop/src/playHost.ts
+++ b/desktop/src/playHost.ts
@@ -155,9 +155,17 @@ async function ensureEngine(): Promise<void> {
}) as WebGL2RenderingContext | null;
if (!gl) throw new Error('WebGL2 is not available in this realm.');
glHandle = module.GL.registerContext(gl, { majorVersion: 2, minorVersion: 0, enableExtensionsByDefault: true });
+ module.engine_setCpuProfiling?.(true); // C++ scope profiling for the editor profiler
engineModule = module;
}
+/** The running game's last-frame C++ CPU scopes (render passes → `cpp.*`). */
+function parseCppScopes(m: ESEngineModule | null): Record<string, number> {
+ const json = m?.engine_getCpuScopes?.();
+ if (!json) return {};
+ try { return JSON.parse(json) as Record<string, number>; } catch { return {}; }
+}
+
/** Build a fresh App on the (preserved) module + GL and run `msg`'s scene. The
* caller imports the project bundle BEFORE this, so its components/systems are
* already in the registry initPlayRealmRuntime drains. createWebApp's
@@ -339,6 +347,7 @@ window.addEventListener('message', (e: MessageEvent) => {
sprites: m?.renderer_getSprites?.() ?? 0,
entities: app?.world.getAllEntities().length ?? 0,
gpuMs: m?.renderer_getGpuTimeMs?.() ?? -1,
+ cppScopes: parseCppScopes(m),
},
});
} else if (data.kind === 'subsystems') {