@margoengine/stats 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +413 -2
- package/dist/beacon.d.ts +80 -0
- package/dist/beacon.js +85 -0
- package/dist/compile.d.ts +112 -0
- package/dist/compile.js +141 -0
- package/dist/geometry.d.ts +64 -0
- package/dist/geometry.js +134 -0
- package/dist/gl.d.ts +78 -0
- package/dist/gl.js +174 -0
- package/dist/index.d.ts +78 -0
- package/dist/index.js +1 -0
- package/dist/leaks.d.ts +86 -0
- package/dist/leaks.js +119 -0
- package/dist/panel-gpu.d.ts +14 -0
- package/dist/panels.d.ts +83 -0
- package/dist/panels.js +177 -0
- package/dist/pass-prototypes.d.ts +26 -0
- package/dist/phases.d.ts +78 -0
- package/dist/phases.js +114 -0
- package/dist/profiler.d.ts +28 -0
- package/dist/report.d.ts +56 -0
- package/dist/report.js +125 -0
- package/dist/src-DtZiAj6x.js +368 -0
- package/package.json +80 -4
package/dist/beacon.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
/** The JSON body of one beacon. It holds these numbers and the app's `context`, nothing else. */
|
|
5
|
+
export interface FrameBeaconReport {
|
|
6
|
+
/** Frames in this report. */
|
|
7
|
+
frames: number;
|
|
8
|
+
/** Their total time. */
|
|
9
|
+
seconds: number;
|
|
10
|
+
/** Frame time percentiles in milliseconds; each is at most 4.6% above the true value. */
|
|
11
|
+
p50: number;
|
|
12
|
+
p90: number;
|
|
13
|
+
p95: number;
|
|
14
|
+
p99: number;
|
|
15
|
+
/** The longest frame, exact. */
|
|
16
|
+
max: number;
|
|
17
|
+
context: Record<string, string | number | boolean>;
|
|
18
|
+
}
|
|
19
|
+
/** Options for {@link FrameBeacon}. */
|
|
20
|
+
export interface FrameBeaconOptions {
|
|
21
|
+
/** Where reports are POSTed: an http or https URL, relative to the page if relative. Required. */
|
|
22
|
+
endpoint: string | URL;
|
|
23
|
+
/** Time between reports, or 0 to send only when the page hides and on `flush()`. Default 30000. */
|
|
24
|
+
intervalMilliseconds?: number;
|
|
25
|
+
/** A report with fewer frames waits for more. Default 60. */
|
|
26
|
+
minFrames?: number;
|
|
27
|
+
/** Share of sessions that report, 0..1, decided once at construction. Default 1. */
|
|
28
|
+
sampleRate?: number;
|
|
29
|
+
/** Sent with every report: a build number, a scene name, a quality tier. Read at each send. Default `{}`. */
|
|
30
|
+
context?: Record<string, string | number | boolean>;
|
|
31
|
+
/** Stops reporting when aborted, as `dispose()` does. */
|
|
32
|
+
signal?: AbortSignal;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Reports frame time percentiles from real sessions to an endpoint the app chooses. The app decides whether to create
|
|
36
|
+
* it; it sends only frame numbers and `context`, with no identifier. It reports on an interval, when the page hides and
|
|
37
|
+
* on `dispose()`, through `navigator.sendBeacon`, or `fetch` with `keepalive` where that is missing or refuses. The body
|
|
38
|
+
* is a JSON string sent as `text/plain`, so a cross-origin endpoint needs no preflight.
|
|
39
|
+
*/
|
|
40
|
+
export declare class FrameBeacon implements EnginePlugin {
|
|
41
|
+
readonly name = "frame-beacon";
|
|
42
|
+
/** False when `sampleRate` left this session out: it then does nothing at all. */
|
|
43
|
+
readonly active: boolean;
|
|
44
|
+
/** Reports handed to the browser. */
|
|
45
|
+
sent: number;
|
|
46
|
+
/** Reports the browser could not send; `lastError` holds why. Nothing is retried. */
|
|
47
|
+
failed: number;
|
|
48
|
+
lastError: unknown;
|
|
49
|
+
private readonly url;
|
|
50
|
+
private readonly bins;
|
|
51
|
+
private readonly minFrames;
|
|
52
|
+
private readonly context;
|
|
53
|
+
private frames;
|
|
54
|
+
private total;
|
|
55
|
+
private longest;
|
|
56
|
+
private timer?;
|
|
57
|
+
private engine?;
|
|
58
|
+
private removeFrameListener?;
|
|
59
|
+
private last;
|
|
60
|
+
private disposed;
|
|
61
|
+
private readonly signal?;
|
|
62
|
+
private readonly hidden;
|
|
63
|
+
private readonly leave;
|
|
64
|
+
private readonly abort;
|
|
65
|
+
/** Adds it to an engine and samples the engine's frames. */
|
|
66
|
+
static attach(engine: EngineKernel, options: FrameBeaconOptions): FrameBeacon;
|
|
67
|
+
constructor({ endpoint, intervalMilliseconds, minFrames, sampleRate, context, signal, }: FrameBeaconOptions);
|
|
68
|
+
/** Adds one frame. Numbers only: nothing is allocated. */
|
|
69
|
+
frame(milliseconds: number): void;
|
|
70
|
+
/** The frame time at or under which share `p` (0..1) of the frames lie, by the upper edge of its bin. */
|
|
71
|
+
private percentile;
|
|
72
|
+
/** What the next report would hold. */
|
|
73
|
+
toJSON(): FrameBeaconReport;
|
|
74
|
+
/** Sends the frames so far and starts counting again. Returns false, keeping them, under `minFrames`. */
|
|
75
|
+
flush(): boolean;
|
|
76
|
+
setup(engine: Engine): void;
|
|
77
|
+
/** Sends what it has, if that is enough, and stops. */
|
|
78
|
+
dispose(): void;
|
|
79
|
+
[Symbol.dispose]: () => void;
|
|
80
|
+
}
|
package/dist/beacon.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
5
|
+
//#region src/beacon.ts
|
|
6
|
+
var t = 256, n = .1, r = Math.log(1e5) / t, i = (e) => Math.round(e * 100) / 100, a = class a {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
7
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
8
|
+
let n = new a(t);
|
|
9
|
+
try {
|
|
10
|
+
return e.use(n), n;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
throw n.dispose(), e;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
constructor({ endpoint: e, intervalMilliseconds: n = 3e4, minFrames: r = 60, sampleRate: i = 1, context: a = {}, signal: o }) {
|
|
16
|
+
if (this.name = "frame-beacon", this.sent = 0, this.failed = 0, this.lastError = null, this.bins = new Float64Array(t), this.frames = 0, this.total = 0, this.longest = 0, this.last = 0, this.disposed = !1, this.hidden = () => {
|
|
17
|
+
globalThis.document?.visibilityState === "hidden" && this.flush();
|
|
18
|
+
}, this.leave = () => {
|
|
19
|
+
this.flush();
|
|
20
|
+
}, this.abort = () => {
|
|
21
|
+
this.dispose();
|
|
22
|
+
}, e === void 0) throw TypeError("FrameBeacon needs an endpoint; pass its URL.");
|
|
23
|
+
let s = new URL(e, globalThis.location?.href);
|
|
24
|
+
if (s.protocol !== "https:" && s.protocol !== "http:") throw TypeError("endpoint must be http or https");
|
|
25
|
+
if (!Number.isFinite(n) || n < 0) throw RangeError("intervalMilliseconds must be 0 or above");
|
|
26
|
+
if (!Number.isInteger(r) || r < 1) throw RangeError("minFrames must be 1 or above");
|
|
27
|
+
if (!(i >= 0 && i <= 1)) throw RangeError("sampleRate must be 0..1");
|
|
28
|
+
this.url = s.href, this.minFrames = r, this.context = a, this.signal = o, this.active = !o?.aborted && Math.random() < i, this.active && (o?.addEventListener("abort", this.abort), n && (this.timer = setInterval(this.leave, n)), globalThis.document?.addEventListener("visibilitychange", this.hidden), globalThis.addEventListener?.("pagehide", this.leave));
|
|
29
|
+
}
|
|
30
|
+
frame(i) {
|
|
31
|
+
if (this.disposed) throw new e("disposed", "FrameBeacon is disposed; create a new one to sample frames.");
|
|
32
|
+
if (!Number.isFinite(i) || i <= 0) throw RangeError("Frame time must be above 0");
|
|
33
|
+
if (!this.active) return;
|
|
34
|
+
let a = Math.floor(Math.log(i / n) / r);
|
|
35
|
+
this.bins[a < 0 ? 0 : a >= t ? 255 : a]++, this.frames++, this.total += i, i > this.longest && (this.longest = i);
|
|
36
|
+
}
|
|
37
|
+
percentile(e) {
|
|
38
|
+
let i = Math.ceil(e * this.frames), a = 0;
|
|
39
|
+
for (let e = 0; e < t; e++) if (a += this.bins[e], a >= i) return Math.min(n * Math.exp((e + 1) * r), this.longest);
|
|
40
|
+
return this.longest;
|
|
41
|
+
}
|
|
42
|
+
toJSON() {
|
|
43
|
+
return {
|
|
44
|
+
frames: this.frames,
|
|
45
|
+
seconds: i(this.total / 1e3),
|
|
46
|
+
p50: i(this.percentile(.5)),
|
|
47
|
+
p90: i(this.percentile(.9)),
|
|
48
|
+
p95: i(this.percentile(.95)),
|
|
49
|
+
p99: i(this.percentile(.99)),
|
|
50
|
+
max: i(this.longest),
|
|
51
|
+
context: { ...this.context }
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
flush() {
|
|
55
|
+
if (!this.active || this.disposed || this.frames < this.minFrames) return !1;
|
|
56
|
+
let e = JSON.stringify(this);
|
|
57
|
+
return this.bins.fill(0), this.frames = this.total = this.longest = 0, globalThis.navigator?.sendBeacon?.(this.url, e) ? (this.sent++, !0) : (fetch(this.url, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
body: e,
|
|
60
|
+
keepalive: !0,
|
|
61
|
+
mode: "no-cors",
|
|
62
|
+
credentials: "omit"
|
|
63
|
+
}).then(() => {
|
|
64
|
+
this.sent++;
|
|
65
|
+
}, (e) => {
|
|
66
|
+
this.failed++, this.lastError = e;
|
|
67
|
+
}), !0);
|
|
68
|
+
}
|
|
69
|
+
setup(t) {
|
|
70
|
+
if (this.disposed) throw new e("disposed", "FrameBeacon is disposed; create a new one and add it to the engine.");
|
|
71
|
+
if (this.engine) throw new e("attached", "FrameBeacon is already attached to an engine; create one per engine.");
|
|
72
|
+
this.engine = t, this.removeFrameListener = t.onFrame((e, t) => {
|
|
73
|
+
let n = performance.now();
|
|
74
|
+
t !== 0 && this.last && this.frame(n - this.last), this.last = n;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
dispose() {
|
|
78
|
+
this.disposed || (this.flush(), this.disposed = !0, this.removeFrameListener?.(), this.removeFrameListener = void 0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0, clearInterval(this.timer), this.signal?.removeEventListener("abort", this.abort), globalThis.document?.removeEventListener("visibilitychange", this.hidden), globalThis.removeEventListener?.("pagehide", this.leave));
|
|
79
|
+
}
|
|
80
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
81
|
+
this.dispose();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
//#endregion
|
|
85
|
+
export { a as FrameBeacon };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import type { Stats } from './index.js';
|
|
5
|
+
/** One `createShaderModule` or pipeline create call. */
|
|
6
|
+
export interface CompileRecord {
|
|
7
|
+
kind: 'shader' | 'render' | 'compute';
|
|
8
|
+
/** The descriptor's label, else its shader module's label, else `''`. */
|
|
9
|
+
label: string;
|
|
10
|
+
/** True for a call that held the caller until it returned; false for the `...Async` pipeline calls. */
|
|
11
|
+
blocking: boolean;
|
|
12
|
+
/** `performance.now()` when the call started. */
|
|
13
|
+
startedAt: number;
|
|
14
|
+
/** How many frames had ended when it started. */
|
|
15
|
+
frame: number;
|
|
16
|
+
/** Time until the call returned or its promise settled; null while an async call runs. */
|
|
17
|
+
milliseconds: number | null;
|
|
18
|
+
/** True when the call threw or its promise rejected. */
|
|
19
|
+
failed: boolean;
|
|
20
|
+
/** Where the call came from, with the `stacks` option. */
|
|
21
|
+
stack?: string;
|
|
22
|
+
}
|
|
23
|
+
/** A slow frame and the compiles that ran during it or the frame before. */
|
|
24
|
+
export interface CompileHitch {
|
|
25
|
+
/** Frame number, counted from attach. */
|
|
26
|
+
frame: number;
|
|
27
|
+
/** `performance.now()` when the frame ended. */
|
|
28
|
+
at: number;
|
|
29
|
+
milliseconds: number;
|
|
30
|
+
/** Blocking calls first, then longest first. */
|
|
31
|
+
compiles: CompileRecord[];
|
|
32
|
+
}
|
|
33
|
+
/** What {@link PipelineCompiles.toJSON} returns; safe for `JSON.stringify`. */
|
|
34
|
+
export interface PipelineCompilesReport {
|
|
35
|
+
frames: number;
|
|
36
|
+
/** Every compile seen since attach, the dropped ones included. */
|
|
37
|
+
count: number;
|
|
38
|
+
/** Time spent inside blocking calls since attach. */
|
|
39
|
+
blockingMilliseconds: number;
|
|
40
|
+
/** The newest records, oldest first. */
|
|
41
|
+
compiles: CompileRecord[];
|
|
42
|
+
/** The newest hitches, oldest first. */
|
|
43
|
+
hitches: CompileHitch[];
|
|
44
|
+
}
|
|
45
|
+
/** Options for {@link PipelineCompiles}. */
|
|
46
|
+
export interface PipelineCompilesOptions {
|
|
47
|
+
/** A frame longer than this, in milliseconds, is a hitch when compiles ran near it. Default 33.4. */
|
|
48
|
+
hitchMilliseconds?: number;
|
|
49
|
+
/** Records and hitches kept, 1..65536 of each; the oldest go first. Default 256. */
|
|
50
|
+
capacity?: number;
|
|
51
|
+
/** Keep a stack trace on each record, to find unlabelled compiles. Default false. */
|
|
52
|
+
stacks?: boolean;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Records every shader and pipeline compile on a device and links them to slow frames. `PipelineCompiles.attach(engine)`
|
|
56
|
+
* follows the engine's device and frames; a custom loop calls `track(device)` and `frame(milliseconds)`. Compiles made
|
|
57
|
+
* before attach are not seen. A hitch says a compile ran near a slow frame; that is evidence, not proof of the cause.
|
|
58
|
+
*/
|
|
59
|
+
export declare class PipelineCompiles implements EnginePlugin {
|
|
60
|
+
readonly name = "pipeline-compiles";
|
|
61
|
+
/** Frames seen since attach. */
|
|
62
|
+
frames: number;
|
|
63
|
+
/** Compiles seen since attach, the dropped ones included. */
|
|
64
|
+
count: number;
|
|
65
|
+
/** Time spent inside blocking calls since attach. */
|
|
66
|
+
blockingMilliseconds: number;
|
|
67
|
+
private readonly records;
|
|
68
|
+
private readonly hitches;
|
|
69
|
+
private readonly listeners;
|
|
70
|
+
private readonly hitchMilliseconds;
|
|
71
|
+
private readonly capacity;
|
|
72
|
+
private readonly stacks;
|
|
73
|
+
private restore?;
|
|
74
|
+
private engine?;
|
|
75
|
+
private removeFrameListener?;
|
|
76
|
+
private tracked?;
|
|
77
|
+
private untrackNext?;
|
|
78
|
+
private last;
|
|
79
|
+
private previous;
|
|
80
|
+
private timer?;
|
|
81
|
+
private row?;
|
|
82
|
+
private disposed;
|
|
83
|
+
/** Adds it to an engine: compiles on `engine.gpu` and the engine's frames are followed until `dispose()`. */
|
|
84
|
+
static attach(engine: EngineKernel, options?: PipelineCompilesOptions): PipelineCompiles;
|
|
85
|
+
constructor({ hitchMilliseconds, capacity, stacks }?: PipelineCompilesOptions);
|
|
86
|
+
/**
|
|
87
|
+
* Catches the next device made on this page, so the engine's startup pipelines are seen too. Call it before
|
|
88
|
+
* `Engine.create`, then add this tracker with `engine.use(compiles)`. Only `GPUAdapter.prototype.requestDevice` is
|
|
89
|
+
* wrapped, and only until that device arrives or `dispose()`.
|
|
90
|
+
* @throws MargoError `unsupported` where the page has no `GPUAdapter`.
|
|
91
|
+
*/
|
|
92
|
+
trackNext(): this;
|
|
93
|
+
/** Wraps a device's compile calls until `dispose()`, or until another `track`. */
|
|
94
|
+
track(device: GPUDevice): void;
|
|
95
|
+
private timed;
|
|
96
|
+
/**
|
|
97
|
+
* Ends a frame of this length, ending at `at`, and returns the hitch it makes, or null. Records are searched only
|
|
98
|
+
* for a slow frame, so a normal frame allocates nothing.
|
|
99
|
+
*/
|
|
100
|
+
frame(milliseconds: number, at?: number): CompileHitch | null;
|
|
101
|
+
/** Calls `listener` for each hitch; returns its remover. */
|
|
102
|
+
onHitch(listener: (hitch: CompileHitch) => void): () => void;
|
|
103
|
+
/** A copy of what was seen. */
|
|
104
|
+
toJSON(): PipelineCompilesReport;
|
|
105
|
+
/** Shows the totals and the newest hitch in a row of the Stats panel, refreshed twice a second until `dispose()`. */
|
|
106
|
+
view(stats: Stats): this;
|
|
107
|
+
setup(engine: Engine): void;
|
|
108
|
+
onDeviceRestored(engine: Engine): void;
|
|
109
|
+
/** Stops watching and removes the view; the device goes back to its own calls. */
|
|
110
|
+
dispose(): void;
|
|
111
|
+
[Symbol.dispose]: () => void;
|
|
112
|
+
}
|
package/dist/compile.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
5
|
+
//#region src/compile.ts
|
|
6
|
+
var t = [
|
|
7
|
+
"createShaderModule",
|
|
8
|
+
"createRenderPipeline",
|
|
9
|
+
"createComputePipeline",
|
|
10
|
+
"createRenderPipelineAsync",
|
|
11
|
+
"createComputePipelineAsync"
|
|
12
|
+
], n = {
|
|
13
|
+
createShaderModule: "shader",
|
|
14
|
+
createRenderPipeline: "render",
|
|
15
|
+
createComputePipeline: "compute",
|
|
16
|
+
createRenderPipelineAsync: "render",
|
|
17
|
+
createComputePipelineAsync: "compute"
|
|
18
|
+
}, r = (e, t) => e.milliseconds ?? t - e.startedAt, i = class i {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
19
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
20
|
+
let n = new i(t);
|
|
21
|
+
return e.use(n), n;
|
|
22
|
+
}
|
|
23
|
+
constructor({ hitchMilliseconds: e = 33.4, capacity: t = 256, stacks: n = !1 } = {}) {
|
|
24
|
+
if (this.name = "pipeline-compiles", this.frames = 0, this.count = 0, this.blockingMilliseconds = 0, this.records = [], this.hitches = [], this.listeners = /* @__PURE__ */ new Set(), this.last = 0, this.previous = 0, this.disposed = !1, !Number.isFinite(e) || e <= 0) throw RangeError("hitchMilliseconds must be above 0");
|
|
25
|
+
if (!Number.isInteger(t) || t < 1 || t > 65536) throw RangeError("capacity must be 1..65536");
|
|
26
|
+
if (typeof n != "boolean") throw TypeError("stacks must be boolean");
|
|
27
|
+
this.hitchMilliseconds = e, this.capacity = t, this.stacks = n;
|
|
28
|
+
}
|
|
29
|
+
trackNext() {
|
|
30
|
+
if (this.disposed) throw new e("disposed", "PipelineCompiles is disposed; create a new one to track a device.");
|
|
31
|
+
let t = globalThis.GPUAdapter?.prototype;
|
|
32
|
+
if (!t) throw new e("unsupported", "trackNext needs WebGPU; this page has no GPUAdapter");
|
|
33
|
+
let n = t.requestDevice, r = this, i = () => {
|
|
34
|
+
t.requestDevice === a && (t.requestDevice = n);
|
|
35
|
+
}, a = async function(e) {
|
|
36
|
+
i();
|
|
37
|
+
let t = await n.call(this, e);
|
|
38
|
+
return r.disposed || r.track(t), t;
|
|
39
|
+
};
|
|
40
|
+
return t.requestDevice = a, this.untrackNext = i, this;
|
|
41
|
+
}
|
|
42
|
+
track(r) {
|
|
43
|
+
if (this.disposed) throw new e("disposed", "PipelineCompiles is disposed; create a new one to track a device.");
|
|
44
|
+
this.restore?.(), this.tracked = r;
|
|
45
|
+
let i = r, a = t.map((e) => i[e]), o = t.map((e, t) => {
|
|
46
|
+
let i = a[t], o = !e.endsWith("Async");
|
|
47
|
+
return (t) => this.timed(r, i, t, n[e], o);
|
|
48
|
+
});
|
|
49
|
+
t.forEach((e, t) => {
|
|
50
|
+
i[e] = o[t];
|
|
51
|
+
}), this.restore = () => {
|
|
52
|
+
t.forEach((e, t) => {
|
|
53
|
+
i[e] === o[t] && (i[e] = a[t]);
|
|
54
|
+
}), this.restore = void 0;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
timed(e, t, n, r, i) {
|
|
58
|
+
let a = {
|
|
59
|
+
kind: r,
|
|
60
|
+
label: n.label || n.vertex?.module?.label || n.compute?.module?.label || "",
|
|
61
|
+
blocking: i,
|
|
62
|
+
startedAt: performance.now(),
|
|
63
|
+
frame: this.frames,
|
|
64
|
+
milliseconds: null,
|
|
65
|
+
failed: !1,
|
|
66
|
+
...this.stacks && { stack: (/* @__PURE__ */ Error()).stack?.split("\n").slice(3).join("\n") }
|
|
67
|
+
};
|
|
68
|
+
this.count++, this.records.push(a), this.records.length > this.capacity && this.records.shift();
|
|
69
|
+
let o = (e) => {
|
|
70
|
+
a.milliseconds = performance.now() - a.startedAt, a.failed = e, i && (this.blockingMilliseconds += a.milliseconds);
|
|
71
|
+
}, s;
|
|
72
|
+
try {
|
|
73
|
+
s = t.call(e, n);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
throw o(!0), e;
|
|
76
|
+
}
|
|
77
|
+
return i ? (o(!1), s) : s.then((e) => (o(!1), e), (e) => {
|
|
78
|
+
throw o(!0), e;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
frame(t, n = performance.now()) {
|
|
82
|
+
if (this.disposed) throw new e("disposed", "PipelineCompiles is disposed; create a new one to sample frames.");
|
|
83
|
+
if (!Number.isFinite(t) || t <= 0 || !Number.isFinite(n)) throw RangeError("Invalid frame; pass milliseconds above 0 and a finite time.");
|
|
84
|
+
let i = n - t - this.previous, a = ++this.frames;
|
|
85
|
+
if (this.previous = t, t <= this.hitchMilliseconds) return null;
|
|
86
|
+
let o = this.records.filter((e) => e.startedAt <= n && e.startedAt + r(e, n) >= i);
|
|
87
|
+
if (!o.length) return null;
|
|
88
|
+
o.sort((e, t) => +t.blocking - e.blocking || r(t, n) - r(e, n));
|
|
89
|
+
let s = {
|
|
90
|
+
frame: a,
|
|
91
|
+
at: n,
|
|
92
|
+
milliseconds: t,
|
|
93
|
+
compiles: o
|
|
94
|
+
};
|
|
95
|
+
return this.hitches.push(s), this.hitches.length > this.capacity && this.hitches.shift(), this.listeners.forEach((e) => e(s)), s;
|
|
96
|
+
}
|
|
97
|
+
onHitch(e) {
|
|
98
|
+
return this.listeners.add(e), () => {
|
|
99
|
+
this.listeners.delete(e);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
toJSON() {
|
|
103
|
+
return {
|
|
104
|
+
frames: this.frames,
|
|
105
|
+
count: this.count,
|
|
106
|
+
blockingMilliseconds: this.blockingMilliseconds,
|
|
107
|
+
compiles: this.records.map((e) => ({ ...e })),
|
|
108
|
+
hitches: this.hitches.map((e) => ({
|
|
109
|
+
...e,
|
|
110
|
+
compiles: e.compiles.map((e) => ({ ...e }))
|
|
111
|
+
}))
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
view(e) {
|
|
115
|
+
if (this.row) return this;
|
|
116
|
+
let t = this.row = e.element.ownerDocument.createElement("div"), n = () => {
|
|
117
|
+
let e = this.hitches.at(-1), n = e?.compiles[0];
|
|
118
|
+
t.textContent = `Compiles ${this.count}, ${this.hitches.length} hitches` + (e ? `\nLast ${e.milliseconds.toFixed(1)} ms: ${n.label || n.kind}` : "");
|
|
119
|
+
};
|
|
120
|
+
return t.style.whiteSpace = "pre-line", e.element.append(t), n(), this.timer = setInterval(n, 500), this;
|
|
121
|
+
}
|
|
122
|
+
setup(t) {
|
|
123
|
+
if (this.disposed) throw new e("disposed", "PipelineCompiles is disposed; create a new one and add it to the engine.");
|
|
124
|
+
if (this.engine) throw new e("attached", "PipelineCompiles is already attached to an engine; create one per engine.");
|
|
125
|
+
this.engine = t, t.gpu && t.gpu !== this.tracked && this.track(t.gpu), this.removeFrameListener = t.onFrame((e, t) => {
|
|
126
|
+
let n = performance.now();
|
|
127
|
+
t === 0 ? this.previous = 0 : this.last && this.frame(n - this.last, n), this.last = n;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
onDeviceRestored(e) {
|
|
131
|
+
this.restore?.(), this.last = this.previous = 0, e.gpu && this.track(e.gpu);
|
|
132
|
+
}
|
|
133
|
+
dispose() {
|
|
134
|
+
this.disposed || (this.disposed = !0, this.removeFrameListener?.(), this.removeFrameListener = void 0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0, this.untrackNext?.(), this.restore?.(), clearInterval(this.timer), this.row?.remove(), this.row = void 0, this.listeners.clear());
|
|
135
|
+
}
|
|
136
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
137
|
+
this.dispose();
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
//#endregion
|
|
141
|
+
export { i as PipelineCompiles };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin, FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import type { Stats } from './index.js';
|
|
5
|
+
/** Submitted geometry, including repeated indexed vertices and every instance. */
|
|
6
|
+
export interface GeometryReport {
|
|
7
|
+
triangles: number;
|
|
8
|
+
vertices: number;
|
|
9
|
+
instances: number;
|
|
10
|
+
drawCalls: number;
|
|
11
|
+
/** Indirect commands whose arguments were not supplied by the application. */
|
|
12
|
+
unknownDraws: number;
|
|
13
|
+
}
|
|
14
|
+
/** Optional CPU metadata for indirect commands; read at draw time, never cached. */
|
|
15
|
+
export interface GeometryOptions {
|
|
16
|
+
indirect?: (buffer: GPUBuffer, offset: number, indexed: boolean) => {
|
|
17
|
+
count: number;
|
|
18
|
+
instances: number;
|
|
19
|
+
} | undefined;
|
|
20
|
+
}
|
|
21
|
+
/** Draw-argument counters for WebGPU and WebGL2, without reading back geometry. */
|
|
22
|
+
export declare class GeometryStats implements EnginePlugin {
|
|
23
|
+
private readonly options;
|
|
24
|
+
readonly name = "stats-geometry";
|
|
25
|
+
triangles: number;
|
|
26
|
+
vertices: number;
|
|
27
|
+
instances: number;
|
|
28
|
+
drawCalls: number;
|
|
29
|
+
unknownDraws: number;
|
|
30
|
+
private engine?;
|
|
31
|
+
private disposed;
|
|
32
|
+
private frameOpen;
|
|
33
|
+
private readonly pipelines;
|
|
34
|
+
private readonly encoders;
|
|
35
|
+
private readonly devices;
|
|
36
|
+
private readonly contexts;
|
|
37
|
+
private readonly restores;
|
|
38
|
+
private row?;
|
|
39
|
+
private timer?;
|
|
40
|
+
constructor(options?: GeometryOptions);
|
|
41
|
+
/** Attach to an engine; all its views in one task contribute to one frame. */
|
|
42
|
+
static attach(engine: EngineKernel, options?: GeometryOptions): GeometryStats;
|
|
43
|
+
private alive;
|
|
44
|
+
/** Reset the counters once before recording a custom frame. */
|
|
45
|
+
beginFrame(): void;
|
|
46
|
+
private count;
|
|
47
|
+
/** Describe a pipeline made before tracking began. Unregistered pipelines default to triangle-list. */
|
|
48
|
+
pipeline(pipeline: GPURenderPipeline, topology: GPUPrimitiveTopology): this;
|
|
49
|
+
/** Capture topology of future pipelines, including asynchronous pipeline creation. */
|
|
50
|
+
trackDevice(device: GPUDevice): void;
|
|
51
|
+
/** Wrap render passes on a custom encoder. Existing Stats wrappers compose in either order. */
|
|
52
|
+
track(encoder: GPUCommandEncoder): void;
|
|
53
|
+
/** Count native WebGL2 arrays, elements, range-elements and instanced commands. */
|
|
54
|
+
trackGL(gl: WebGL2RenderingContext): void;
|
|
55
|
+
setup(engine: Engine): void;
|
|
56
|
+
registerPasses(graph: FrameGraph): void;
|
|
57
|
+
onDeviceRestored(engine: Engine): void;
|
|
58
|
+
/** Copy the current frame into a JSON-compatible report. */
|
|
59
|
+
toJSON(): GeometryReport;
|
|
60
|
+
/** Show geometry in an existing Stats overlay. */
|
|
61
|
+
view(stats: Stats): this;
|
|
62
|
+
dispose(): void;
|
|
63
|
+
[Symbol.dispose]: () => void;
|
|
64
|
+
}
|
package/dist/geometry.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
5
|
+
//#region src/geometry.ts
|
|
6
|
+
var t = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
7
|
+
constructor(e = {}) {
|
|
8
|
+
this.options = e, this.name = "stats-geometry", this.triangles = 0, this.vertices = 0, this.instances = 0, this.drawCalls = 0, this.unknownDraws = 0, this.disposed = !1, this.frameOpen = !1, this.pipelines = /* @__PURE__ */ new WeakMap(), this.encoders = /* @__PURE__ */ new WeakSet(), this.devices = /* @__PURE__ */ new WeakSet(), this.contexts = /* @__PURE__ */ new WeakSet(), this.restores = [];
|
|
9
|
+
}
|
|
10
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
11
|
+
let r = new t(n);
|
|
12
|
+
return e.use(r), r;
|
|
13
|
+
}
|
|
14
|
+
alive() {
|
|
15
|
+
if (this.disposed) throw new e("disposed", "GeometryStats is disposed; create a new counter.");
|
|
16
|
+
}
|
|
17
|
+
beginFrame() {
|
|
18
|
+
this.alive(), this.triangles = this.vertices = this.instances = this.drawCalls = this.unknownDraws = 0;
|
|
19
|
+
}
|
|
20
|
+
count(e, t, n) {
|
|
21
|
+
this.disposed || (this.drawCalls++, e !== 0 && t !== 0 && (this.vertices += e * t, this.instances += t, (n === "triangle-list" || n === 4) && (this.triangles += Math.floor(e / 3) * t), (n === "triangle-strip" || n === 5 || n === 6) && (this.triangles += Math.max(0, e - 2) * t)));
|
|
22
|
+
}
|
|
23
|
+
pipeline(e, t) {
|
|
24
|
+
return this.alive(), this.pipelines.set(e, t), this;
|
|
25
|
+
}
|
|
26
|
+
trackDevice(e) {
|
|
27
|
+
if (this.alive(), this.devices.has(e)) return;
|
|
28
|
+
this.devices.add(e);
|
|
29
|
+
let t = e.createRenderPipeline, n = e.createRenderPipelineAsync, r = (n) => {
|
|
30
|
+
let r = t.call(e, n);
|
|
31
|
+
return this.pipelines.set(r, n.primitive?.topology ?? "triangle-list"), r;
|
|
32
|
+
}, i = async (t) => {
|
|
33
|
+
let r = await n.call(e, t);
|
|
34
|
+
return this.pipelines.set(r, t.primitive?.topology ?? "triangle-list"), r;
|
|
35
|
+
};
|
|
36
|
+
e.createRenderPipeline = r, e.createRenderPipelineAsync = i, this.restores.push(() => {
|
|
37
|
+
e.createRenderPipeline === r && (e.createRenderPipeline = t), e.createRenderPipelineAsync === i && (e.createRenderPipelineAsync = n);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
track(e) {
|
|
41
|
+
if (this.alive(), this.encoders.has(e)) return;
|
|
42
|
+
this.encoders.add(e);
|
|
43
|
+
let t = e.beginRenderPass;
|
|
44
|
+
e.beginRenderPass = (n) => {
|
|
45
|
+
let r = t.call(e, n), i = "triangle-list", a = r.setPipeline, o = r.draw, s = r.drawIndexed, c = r.drawIndirect, l = r.drawIndexedIndirect;
|
|
46
|
+
r.setPipeline = (e) => {
|
|
47
|
+
a.call(r, e), i = this.pipelines.get(e) ?? "triangle-list";
|
|
48
|
+
}, r.draw = (e, t = 1, n = 0, a = 0) => {
|
|
49
|
+
o.call(r, e, t, n, a), this.count(e, t, i);
|
|
50
|
+
}, r.drawIndexed = (e, t = 1, n = 0, a = 0, o = 0) => {
|
|
51
|
+
s.call(r, e, t, n, a, o), this.count(e, t, i);
|
|
52
|
+
};
|
|
53
|
+
let u = (e, t, n) => {
|
|
54
|
+
if (this.disposed) return;
|
|
55
|
+
let r = this.options.indirect?.(e, t, n);
|
|
56
|
+
r ? this.count(r.count, r.instances, i) : (this.drawCalls++, this.unknownDraws++);
|
|
57
|
+
};
|
|
58
|
+
r.drawIndirect = (e, t) => {
|
|
59
|
+
c.call(r, e, t), u(e, t, !1);
|
|
60
|
+
}, r.drawIndexedIndirect = (e, t) => {
|
|
61
|
+
l.call(r, e, t), u(e, t, !0);
|
|
62
|
+
};
|
|
63
|
+
let d = r.executeBundles;
|
|
64
|
+
return d && (r.executeBundles = (e) => {
|
|
65
|
+
let t = Array.from(e);
|
|
66
|
+
d.call(r, t), this.disposed || (this.unknownDraws += t.length);
|
|
67
|
+
}), r;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
trackGL(e) {
|
|
71
|
+
if (this.alive(), this.contexts.has(e)) return;
|
|
72
|
+
this.contexts.add(e);
|
|
73
|
+
let t = e.drawArrays, n = e.drawElements, r = e.drawRangeElements, i = e.drawArraysInstanced, a = e.drawElementsInstanced, o = (n, r, i) => {
|
|
74
|
+
t.call(e, n, r, i), this.count(i, 1, n);
|
|
75
|
+
}, s = (t, r, i, a) => {
|
|
76
|
+
n.call(e, t, r, i, a), this.count(r, 1, t);
|
|
77
|
+
}, c = (t, n, i, a, o, s) => {
|
|
78
|
+
r.call(e, t, n, i, a, o, s), this.count(a, 1, t);
|
|
79
|
+
}, l = (t, n, r, a) => {
|
|
80
|
+
i.call(e, t, n, r, a), this.count(r, a, t);
|
|
81
|
+
}, u = (t, n, r, i, o) => {
|
|
82
|
+
a.call(e, t, n, r, i, o), this.count(n, o, t);
|
|
83
|
+
};
|
|
84
|
+
e.drawArrays = o, e.drawElements = s, e.drawRangeElements = c, e.drawArraysInstanced = l, e.drawElementsInstanced = u, this.restores.push(() => {
|
|
85
|
+
e.drawArrays === o && (e.drawArrays = t), e.drawElements === s && (e.drawElements = n), e.drawRangeElements === c && (e.drawRangeElements = r), e.drawArraysInstanced === l && (e.drawArraysInstanced = i), e.drawElementsInstanced === u && (e.drawElementsInstanced = a);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
setup(t) {
|
|
89
|
+
if (this.alive(), this.engine) throw new e("attached", "GeometryStats is attached; create one per engine.");
|
|
90
|
+
this.engine = t, t.gpu && this.trackDevice(t.gpu);
|
|
91
|
+
}
|
|
92
|
+
registerPasses(e) {
|
|
93
|
+
e.add({
|
|
94
|
+
name: "stats-geometry:begin",
|
|
95
|
+
order: -Number.MAX_VALUE,
|
|
96
|
+
execute: (e) => {
|
|
97
|
+
this.frameOpen || (this.beginFrame(), this.frameOpen = !0, queueMicrotask(() => {
|
|
98
|
+
this.frameOpen = !1;
|
|
99
|
+
})), e.backend === "webgpu" ? this.track(e.encoder) : this.trackGL(e.gl);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
onDeviceRestored(e) {
|
|
104
|
+
e.gpu && this.trackDevice(e.gpu), this.beginFrame();
|
|
105
|
+
}
|
|
106
|
+
toJSON() {
|
|
107
|
+
return {
|
|
108
|
+
triangles: this.triangles,
|
|
109
|
+
vertices: this.vertices,
|
|
110
|
+
instances: this.instances,
|
|
111
|
+
drawCalls: this.drawCalls,
|
|
112
|
+
unknownDraws: this.unknownDraws
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
view(e) {
|
|
116
|
+
if (this.alive(), this.row) return this;
|
|
117
|
+
let t = this.row = e.element.ownerDocument.createElement("div"), n = () => {
|
|
118
|
+
t.textContent = `${this.triangles} triangles ${this.vertices} vertices ${this.instances} instances${this.unknownDraws ? ` (${this.unknownDraws} unknown draws/bundles)` : ""}`;
|
|
119
|
+
};
|
|
120
|
+
return e.element.append(t), n(), this.timer = setInterval(n, 150), this;
|
|
121
|
+
}
|
|
122
|
+
dispose() {
|
|
123
|
+
if (!this.disposed) {
|
|
124
|
+
this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0;
|
|
125
|
+
for (let e of this.restores.splice(0).reverse()) e();
|
|
126
|
+
clearInterval(this.timer), this.row?.remove();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
130
|
+
this.dispose();
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
//#endregion
|
|
134
|
+
export { t as GeometryStats };
|