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.

fix(profiler): separate work counters and manual steps from frame timing

yhh000f35420898
diff --git a/sdk/src/app/app.ts b/sdk/src/app/app.ts
index 86680f1a8..3e7e4bef2 100644
--- a/sdk/src/app/app.ts
+++ b/sdk/src/app/app.ts
@@ -252,6 +252,7 @@ export class App {
private frame_paused_ = false;
private user_paused_ = false;
private step_pending_ = false;
+ private steppedFrames_ = 0;
private play_speed_ = 1.0;
private constructor() {
@@ -1009,6 +1010,11 @@ export class App {
return this.frameTiming_;
}
+ /** Frames requested by stepping controls, excluded from wall-clock FPS windows. */
+ getSteppedFrameCount(): number {
+ return this.steppedFrames_;
+ }
+
/**
* Sub-frame CPU scopes recorded this frame via {@link measureFrameScope} —
* the finer breakdown within a single system, keyed by scope name. Null when
@@ -2006,6 +2012,7 @@ export class App {
time.delta = hostPauseOwners.get(this)?.size && !this.step_pending_ ? 0 : delta * scale;
time.elapsed += time.delta;
time.frameCount++;
+ if (this.step_pending_) this.steppedFrames_++;
time.fixedDelta = this.fixedTimestep_;
}
}
diff --git a/sdk/src/app/frameProfile.ts b/sdk/src/app/frameProfile.ts
index d316b6248..460da7cd6 100644
--- a/sdk/src/app/frameProfile.ts
+++ b/sdk/src/app/frameProfile.ts
@@ -157,6 +157,12 @@ function byMsDesc(a: ProfileNode, b: ProfileNode): number {
return b.ms - a.ms;
}
+/** The native mesh collector also publishes work counts in its scope packet.
+ * These are quantities, including in existing captures, never milliseconds. */
+export function isNativeProfileCounter(name: string): boolean {
+ return /^render\.collect\.mesh\.(scene|depth|motion|other)\.(walks|visits)$/.test(name);
+}
+
/**
* Fold one frame's flat measurements into the tree the profiler renders.
*
@@ -166,6 +172,7 @@ function byMsDesc(a: ProfileNode, b: ProfileNode): number {
export function buildFrameProfile(input: FrameProfileInput): FrameProfile {
const nativeByDomain = new Map<string, ScopeCost[]>();
for (const n of input.nativeScopes ?? []) {
+ if (isNativeProfileCounter(n.name)) continue;
const d = scopeDomain(n.name);
const list = nativeByDomain.get(d);
if (list) list.push(n); else nativeByDomain.set(d, [n]);
diff --git a/sdk/src/app/profileCapture.ts b/sdk/src/app/profileCapture.ts
index 0944138f9..62618559e 100644
--- a/sdk/src/app/profileCapture.ts
+++ b/sdk/src/app/profileCapture.ts
@@ -7,6 +7,7 @@ import type { QualityReport } from '../render/quality';
import {
buildFrameProfile,
meanFrameProfile,
+ isNativeProfileCounter,
type FrameProfile,
type ScopeCost,
type SystemCost,
@@ -131,7 +132,13 @@ export function summarizeFrames(frames: readonly CapturedFrame[], budgetMs = 100
const counters: Record<string, number> = {};
for (const f of frames) {
- for (const k in f.counters) counters[k] = (counters[k] ?? 0) + f.counters[k];
+ const quantities = { ...f.counters };
+ for (const scope of f.nativeScopes ?? []) {
+ if (isNativeProfileCounter(scope.name) && quantities[scope.name] === undefined) {
+ quantities[scope.name] = scope.ms;
+ }
+ }
+ for (const k in quantities) counters[k] = (counters[k] ?? 0) + quantities[k];
}
if (frames.length > 0) for (const k in counters) counters[k] /= frames.length;
diff --git a/sdk/src/app/renderOptions.ts b/sdk/src/app/renderOptions.ts
index 82c480073..8b8a48da9 100644
--- a/sdk/src/app/renderOptions.ts
+++ b/sdk/src/app/renderOptions.ts
@@ -9,7 +9,7 @@
* linear values unencoded, far too dark, and its output transform, MSAA, quality and layer sorting never arrived either.
*/
import { setLinearColorSpace } from '../ecs/env';
-import { outputTransformCode, type OutputTransform } from '../postprocess';
+import { outputTransformCode, type OutputTransform } from '../postprocess/PostProcessAPI';
import { installQuality } from '../render/qualityRuntime';
import { ScreenScaling, SCREEN_FIT_OFF, type ScreenScalingData } from '../camera/ScreenScaling';
import { RenderResolution } from '../camera/presentPlan';
diff --git a/sdk/tests/app-tick.test.ts b/sdk/tests/app-tick.test.ts
index 0488850ca..3b723ca5c 100644
--- a/sdk/tests/app-tick.test.ts
+++ b/sdk/tests/app-tick.test.ts
@@ -128,6 +128,20 @@ describe('App.tick()', () => {
});
describe('App.stepFrames()', () => {
+ it('counts explicitly stepped frames separately from the normal clock', async () => {
+ const app = App.new();
+ await app.tick(1 / 60);
+ expect(app.getSteppedFrameCount()).toBe(0);
+ await app.stepFrames(5);
+ expect(app.getSteppedFrameCount()).toBe(5);
+ expect(app.getResource(Time).frameCount).toBe(6);
+ await app.tick(1 / 60);
+ expect(app.getSteppedFrameCount()).toBe(5);
+ const release = app.holdExecution();
+ await app.stepFrames(2);
+ expect(app.getSteppedFrameCount()).toBe(5);
+ release();
+ });
it('waits for an in-flight frame and resumes only one loop', async () => {
const realRaf = globalThis.requestAnimationFrame;
const realCancel = globalThis.cancelAnimationFrame;
diff --git a/sdk/tests/profile-capture.test.ts b/sdk/tests/profile-capture.test.ts
index 9cbc43e8b..90e30b1b9 100644
--- a/sdk/tests/profile-capture.test.ts
+++ b/sdk/tests/profile-capture.test.ts
@@ -74,6 +74,34 @@ describe('summarizeFrames', () => {
expect(s.counters['batch.draws']).toBeCloseTo(2, 4);
});
+ it('reads native mesh visits as quantities instead of thousands of milliseconds', () => {
+ const measured = frame(0, 16.7, {
+ scopes: [{ name: 'render.submit', ms: 2, system: 'RenderSystem', remainder: 'wait' }],
+ nativeScopes: [
+ { name: 'render.collect.mesh.scene.walks', ms: 1, system: '', remainder: 'work' },
+ { name: 'render.collect.mesh.scene.visits', ms: 3083, system: '', remainder: 'work' },
+ { name: 'render.collect.mesh.scene.ms', ms: .5, system: '', remainder: 'work' },
+ ],
+ });
+ const s = summarizeFrames([measured]);
+ expect(s.counters['render.collect.mesh.scene.visits']).toBe(3083);
+ expect(s.counters['render.collect.mesh.scene.walks']).toBe(1);
+ expect(s.mean.cpuMs).toBeCloseTo(.5, 4);
+ expect(s.mean.waitMs).toBeCloseTo(1.5, 4);
+ expect(JSON.stringify(s.mean.domains)).not.toContain('.visits');
+ expect(JSON.stringify(s.mean.domains)).not.toContain('.walks');
+ expect(JSON.stringify(s.mean.domains)).toContain('scene.ms');
+ });
+
+ it('keeps the typed counter when a capture carries both old and new representations', () => {
+ const name = 'render.collect.mesh.depth.visits';
+ const s = summarizeFrames([
+ frame(0, 16, { counters: { [name]: 8 }, nativeScopes: [{ name, ms: 100, system: '', remainder: 'work' }] }),
+ frame(1, 16),
+ ]);
+ expect(s.counters[name]).toBe(4);
+ });
+
it('keeps the mean profile summing like a frame', () => {
const s = summarizeFrames([frame(0, 16.7), frame(1, 16.7)]);
expect(s.mean.cpuMs + s.mean.waitMs + s.mean.idleMs).toBeCloseTo(s.mean.frameMs, 4);