@margoengine/stats 0.0.0-stage → 0.1.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 +414 -3
- 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 +82 -6
package/dist/panels.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { n as e, t } from "./src-DtZiAj6x.js";
|
|
5
|
+
import { FramePhases as n } from "./phases.js";
|
|
6
|
+
import { GPUMemory as r } from "./report.js";
|
|
7
|
+
import { GLStats as i } from "./gl.js";
|
|
8
|
+
import { MargoError as a } from "@margoengine/math/errors";
|
|
9
|
+
//#region src/panel-gpu.ts
|
|
10
|
+
var o = class {
|
|
11
|
+
constructor(t, n, r) {
|
|
12
|
+
this.complete = n, this.error = r, this.tracked = /* @__PURE__ */ new WeakSet(), this.frame = 0, this.disposed = !1, this.profiler = new e(t);
|
|
13
|
+
}
|
|
14
|
+
get supported() {
|
|
15
|
+
return this.profiler.supported;
|
|
16
|
+
}
|
|
17
|
+
track(e) {
|
|
18
|
+
if (this.disposed || this.tracked.has(e) || (this.tracked.add(e), ++this.frame % 15 != 0 || !this.profiler.beginFrame())) return;
|
|
19
|
+
let t = [], n = this.profiler, r = e.beginRenderPass, i = e.beginComputePass, a = e.finish;
|
|
20
|
+
e.beginRenderPass = (i) => {
|
|
21
|
+
let a = i.timestampWrites ? void 0 : n.timestampWrites();
|
|
22
|
+
return a && t.push(i.label || `render ${t.length + 1}`), r.call(e, a ? {
|
|
23
|
+
...i,
|
|
24
|
+
timestampWrites: a
|
|
25
|
+
} : i);
|
|
26
|
+
}, e.beginComputePass = (r = {}) => {
|
|
27
|
+
let a = r.timestampWrites ? void 0 : n.timestampWrites();
|
|
28
|
+
return a && t.push(r.label || `compute ${t.length + 1}`), i.call(e, a ? {
|
|
29
|
+
...r,
|
|
30
|
+
timestampWrites: a
|
|
31
|
+
} : r);
|
|
32
|
+
}, e.finish = (r) => {
|
|
33
|
+
let i = n.resolve(e), o = a.call(e, r);
|
|
34
|
+
return i ? (queueMicrotask(() => {
|
|
35
|
+
n.read().then((e) => {
|
|
36
|
+
this.disposed || this.complete(e, e === null ? [] : t.slice(0, n.passCount).map((e, t) => ({
|
|
37
|
+
name: e,
|
|
38
|
+
milliseconds: n.passMilliseconds[t]
|
|
39
|
+
})));
|
|
40
|
+
}, (e) => {
|
|
41
|
+
this.disposed || (this.complete(null, []), this.error(e));
|
|
42
|
+
});
|
|
43
|
+
}), o) : (n.milliseconds === null && this.complete(null, []), o);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
dispose() {
|
|
47
|
+
this.disposed = !0, this.profiler.dispose();
|
|
48
|
+
}
|
|
49
|
+
}, s = [
|
|
50
|
+
"fps",
|
|
51
|
+
"cpuMilliseconds",
|
|
52
|
+
"gpuMilliseconds",
|
|
53
|
+
"memoryBytes"
|
|
54
|
+
], c = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
55
|
+
constructor(e = {}) {
|
|
56
|
+
this.options = e, this.name = "stats-panels", this.sampleCount = 0, this.sampleIndex = 0, this.phases = new n(), this.gpuTime = null, this.gpuPasses = [], this.latestPasses = [], this.queued = !1, this.disposed = !1, this.memoryLabel = "Memory MiB", this.version = 0, this.shown = -1, this.collect = () => {
|
|
57
|
+
if (this.queued = !1, this.disposed) return;
|
|
58
|
+
let e = this.gl;
|
|
59
|
+
e?.poll();
|
|
60
|
+
let t = performance.memory?.usedJSHeapSize, n = this.options.memory ? this.options.memory() : this.memory ? this.memory.bytes : t ?? null, r = this.options.memory ? "Memory MiB" : this.memory ? "Tracked GPU MiB" : "JS heap MiB";
|
|
61
|
+
r !== this.memoryLabel && this.version++, this.memoryLabel = r, this.sample({
|
|
62
|
+
fps: this.stats.fps,
|
|
63
|
+
cpuMilliseconds: this.phases.last.total,
|
|
64
|
+
gpuMilliseconds: e ? e.frameMilliseconds : this.gpuTime,
|
|
65
|
+
memoryBytes: n,
|
|
66
|
+
passes: e ? e.passes : this.gpuPasses
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
let { samples: r = 120, refreshMilliseconds: i = 150 } = e;
|
|
70
|
+
if (!Number.isInteger(r) || r < 2 || r > 4096) throw RangeError("Invalid samples; use an integer from 2 to 4096.");
|
|
71
|
+
if (!Number.isFinite(i) || i < 16 || i > 6e4) throw RangeError("Invalid refreshMilliseconds; use 16 to 60000 milliseconds.");
|
|
72
|
+
this.ownStats = !e.stats, this.stats = e.stats ?? new t({
|
|
73
|
+
parent: e.parent,
|
|
74
|
+
samples: r
|
|
75
|
+
}), this.graphDisplay = this.stats.graph.style.display, this.stats.graph.style.display = "none";
|
|
76
|
+
let a = this.stats.element.ownerDocument;
|
|
77
|
+
this.element = a.createElement("div"), this.element.dataset.margoStatsPanels = "", this.element.style.cssText = "max-height:calc(100vh - 120px);overflow:auto;pointer-events:auto";
|
|
78
|
+
let o = {}, c = {}, l = {}, u = {};
|
|
79
|
+
for (let e of s) {
|
|
80
|
+
let t = u[e] = a.createElement("div"), n = o[e] = a.createElement("canvas");
|
|
81
|
+
n.width = r, n.height = 40, n.style.cssText = "display:block;width:100%;height:40px;margin:2px 0 6px", n.setAttribute("role", "img"), l[e] = n.getContext("2d"), c[e] = new Float64Array(r).fill(NaN), this.element.append(t, n);
|
|
82
|
+
}
|
|
83
|
+
this.graphs = o, this.series = c, this.contexts = l, this.labels = u, this.list = a.createElement("div"), this.list.style.whiteSpace = "pre-line", this.list.setAttribute("aria-label", "Named GPU pass durations"), this.element.append(this.list), this.ownStats ? (this.stats.element.style.width = "360px", this.stats.element.replaceChildren(this.element)) : this.stats.element.append(this.element), this.refresh(), this.timer = setInterval(() => {
|
|
84
|
+
this.version !== this.shown && this.refresh();
|
|
85
|
+
}, i);
|
|
86
|
+
}
|
|
87
|
+
static attach(t, n = {}) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
88
|
+
let r = new e(n);
|
|
89
|
+
try {
|
|
90
|
+
return t.use(r), r;
|
|
91
|
+
} catch (e) {
|
|
92
|
+
throw r.dispose(), e;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
sample(e) {
|
|
96
|
+
if (this.disposed) throw new a("disposed", "StatsPanels is disposed; create a new overlay.");
|
|
97
|
+
for (let t of s) {
|
|
98
|
+
let n = e[t];
|
|
99
|
+
if ((n !== null || t !== "gpuMilliseconds" && t !== "memoryBytes") && (typeof n != "number" || !Number.isFinite(n) || n < 0)) throw RangeError("Invalid panel sample; use non-negative finite values or null for unavailable GPU/memory.");
|
|
100
|
+
}
|
|
101
|
+
for (let t of e.passes ?? []) if (typeof t.name != "string" || !t.name.trim() || !Number.isFinite(t.milliseconds) || t.milliseconds < 0) throw RangeError("Invalid GPU pass; use a non-empty name and non-negative finite milliseconds.");
|
|
102
|
+
for (let t of s) this.series[t][this.sampleIndex] = e[t] ?? NaN;
|
|
103
|
+
this.latestPasses = e.gpuMilliseconds === null ? [] : (e.passes ?? []).map((e) => ({ ...e })), this.sampleCount = Math.min(this.sampleCount + 1, this.series.fps.length), this.sampleIndex = (this.sampleIndex + 1) % this.series.fps.length, this.version++;
|
|
104
|
+
}
|
|
105
|
+
setup(e) {
|
|
106
|
+
if (this.disposed) throw new a("disposed", "StatsPanels is disposed; create a new overlay.");
|
|
107
|
+
if (this.engine) throw new a("attached", "StatsPanels is attached; create one per engine.");
|
|
108
|
+
this.engine = e, this.ownStats && this.stats.setup(e), this.phases.setup(e), this.bindGPU(e), e.gpu || (this.gl = new i(), this.gl.setup(e));
|
|
109
|
+
}
|
|
110
|
+
bindGPU(e) {
|
|
111
|
+
this.memory?.dispose(), this.gpu?.dispose(), this.gpuTime = null, this.gpuPasses = [], this.memory = void 0, this.gpu = void 0, e.gpu && (this.memory = r.attach(e.gpu), this.gpu = new o(e.gpu, (e, t) => {
|
|
112
|
+
this.gpuTime = e, this.gpuPasses = t;
|
|
113
|
+
}, (t) => e.onError(t)));
|
|
114
|
+
}
|
|
115
|
+
registerPasses(e) {
|
|
116
|
+
this.ownStats && this.stats.registerPasses(e), this.phases.registerPasses(e), this.gl?.registerPasses(e), e.add({
|
|
117
|
+
name: "stats-panels:sample",
|
|
118
|
+
order: -Number.MAX_VALUE,
|
|
119
|
+
execute: (e) => {
|
|
120
|
+
e.backend === "webgpu" && this.gpu?.track(e.encoder), this.queued || (this.queued = !0, queueMicrotask(this.collect));
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
onPreUpdate() {
|
|
125
|
+
this.phases.onPreUpdate();
|
|
126
|
+
}
|
|
127
|
+
onDeviceRestored(e) {
|
|
128
|
+
this.ownStats && this.stats.onDeviceRestored(e), this.phases.onDeviceRestored(e), this.bindGPU(e);
|
|
129
|
+
}
|
|
130
|
+
refresh() {
|
|
131
|
+
if (!this.disposed) {
|
|
132
|
+
this.shown = this.version;
|
|
133
|
+
for (let e of s) {
|
|
134
|
+
let t = this.series[e], n = this.graphs[e], r = this.contexts[e], i = e === "fps" ? "FPS" : e === "cpuMilliseconds" ? "CPU ms" : e === "gpuMilliseconds" ? "GPU ms" : this.memoryLabel, a = e === "memoryBytes" ? 1 / 1048576 : 1, o = this.sampleCount ? t[(this.sampleIndex + t.length - 1) % t.length] : NaN, s = 0, c = 0, l = 0;
|
|
135
|
+
for (let e of t) Number.isFinite(e) && (s += e, c++, l = Math.max(l, e));
|
|
136
|
+
let u = c ? `${i}: ${Number.isFinite(o) ? (o * a).toFixed(2) : "unavailable"} avg ${(s / c * a).toFixed(2)} peak ${(l * a).toFixed(2)}` : `${i}: unavailable`;
|
|
137
|
+
if (this.labels[e].textContent = u, n.setAttribute("aria-label", u), !r) continue;
|
|
138
|
+
r.fillStyle = "#151719", r.fillRect(0, 0, n.width, n.height), r.strokeStyle = e === "fps" ? "#63d9b0" : e === "cpuMilliseconds" ? "#e6c76a" : e === "gpuMilliseconds" ? "#82b9ef" : "#d3a0e8", r.beginPath();
|
|
139
|
+
let d = !1;
|
|
140
|
+
for (let e = 0; e < this.sampleCount; e++) {
|
|
141
|
+
let i = t[(this.sampleIndex - this.sampleCount + e + t.length) % t.length];
|
|
142
|
+
if (!Number.isFinite(i)) {
|
|
143
|
+
d = !1;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
let a = n.height - 1 - i / Math.max(l, 1) * (n.height - 2);
|
|
147
|
+
d ? r.lineTo(e, a) : r.moveTo(e, a), d = !0;
|
|
148
|
+
}
|
|
149
|
+
r.stroke();
|
|
150
|
+
}
|
|
151
|
+
this.list.textContent = this.latestPasses.length ? "GPU passes\n" + this.latestPasses.map((e) => `${e.name}: ${e.milliseconds.toFixed(2)} ms`).join("\n") : "GPU passes unavailable";
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
toJSON() {
|
|
155
|
+
let e = {};
|
|
156
|
+
for (let t of s) {
|
|
157
|
+
let n = this.series[t];
|
|
158
|
+
e[t] = [];
|
|
159
|
+
for (let r = 0; r < this.sampleCount; r++) {
|
|
160
|
+
let i = n[(this.sampleIndex - this.sampleCount + r + n.length) % n.length];
|
|
161
|
+
e[t].push(Number.isFinite(i) ? i : null);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
...e,
|
|
166
|
+
passes: this.latestPasses.map((e) => ({ ...e }))
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
dispose() {
|
|
170
|
+
this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0, clearInterval(this.timer), this.phases.dispose(), this.gpu?.dispose(), this.gl?.dispose(), this.memory?.dispose(), this.element.remove(), this.stats.graph.style.display = this.graphDisplay, this.ownStats && this.stats.dispose());
|
|
171
|
+
}
|
|
172
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
173
|
+
this.dispose();
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
//#endregion
|
|
177
|
+
export { c as StatsPanels };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
type Counters = {
|
|
2
|
+
drawCalls: number;
|
|
3
|
+
dispatches: number;
|
|
4
|
+
};
|
|
5
|
+
/** Internal opt-in layout; ordinary Stats never constructs this helper. */
|
|
6
|
+
export declare class PassPrototypes {
|
|
7
|
+
private readonly owner;
|
|
8
|
+
private readonly getPrototype;
|
|
9
|
+
private readonly setPrototype;
|
|
10
|
+
private readonly reflectSet;
|
|
11
|
+
private readonly own;
|
|
12
|
+
private readonly extensible;
|
|
13
|
+
private readonly lookupGetter;
|
|
14
|
+
private readonly lookupSetter;
|
|
15
|
+
private readonly source;
|
|
16
|
+
private readonly encoderPrototype?;
|
|
17
|
+
private readonly renderKind?;
|
|
18
|
+
private readonly computeKind?;
|
|
19
|
+
constructor(owner: symbol, stats: Counters, renderWrappers: readonly Function[], computeWrappers: readonly Function[]);
|
|
20
|
+
private method;
|
|
21
|
+
private capture;
|
|
22
|
+
private wrap;
|
|
23
|
+
render(pass: GPURenderPassEncoder, encoder: GPUCommandEncoder, factory: GPUCommandEncoder['beginRenderPass']): boolean;
|
|
24
|
+
compute(pass: GPUComputePassEncoder, encoder: GPUCommandEncoder, factory: GPUCommandEncoder['beginComputePass']): boolean;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
package/dist/phases.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
/** CPU milliseconds of one frame by phase. The phases add up to `total`. */
|
|
6
|
+
export interface FramePhaseTimes {
|
|
7
|
+
/** `onFrame` callbacks, plugin updates, resize and camera, up to the kernel tick. */
|
|
8
|
+
update: number;
|
|
9
|
+
/** The kernel tick: world matrices and the frustum test, one Wasm call. */
|
|
10
|
+
tick: number;
|
|
11
|
+
/** Collecting the visible instances into batches and preparing the frame, up to the command encoder. */
|
|
12
|
+
cull: number;
|
|
13
|
+
/** Recording commands: every frame graph pass, then `encoder.finish()`. */
|
|
14
|
+
encode: number;
|
|
15
|
+
/** `queue.submit`. */
|
|
16
|
+
submit: number;
|
|
17
|
+
/** Anything else inside the frame. */
|
|
18
|
+
other: number;
|
|
19
|
+
/** The first phase's start to the end of the frame's task. */
|
|
20
|
+
total: number;
|
|
21
|
+
}
|
|
22
|
+
/** What {@link FramePhases.toJSON} returns; safe for `JSON.stringify`. */
|
|
23
|
+
export interface FramePhasesReport {
|
|
24
|
+
frames: number;
|
|
25
|
+
last: FramePhaseTimes;
|
|
26
|
+
average: FramePhaseTimes;
|
|
27
|
+
}
|
|
28
|
+
/** Options for {@link FramePhases}. */
|
|
29
|
+
export interface FramePhasesOptions {
|
|
30
|
+
/** Frames in the rolling average, 2..4096. Default 120. */
|
|
31
|
+
samples?: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Times the CPU side of each frame in phases, with no change to core: it wraps `store.tick`, `createCommandEncoder`
|
|
35
|
+
* and `queue.submit` and adds two frame graph passes. Nothing is allocated per frame. A frame ends when its task does,
|
|
36
|
+
* so read the numbers from the next frame or later. Views in one frame add up into that frame.
|
|
37
|
+
*/
|
|
38
|
+
export declare class FramePhases implements EnginePlugin {
|
|
39
|
+
readonly name = "frame-phases";
|
|
40
|
+
/** Frames finished since attach. */
|
|
41
|
+
frames: number;
|
|
42
|
+
/** The newest finished frame. One object, updated in place. */
|
|
43
|
+
readonly last: FramePhaseTimes;
|
|
44
|
+
private readonly ring;
|
|
45
|
+
private readonly size;
|
|
46
|
+
private readonly open;
|
|
47
|
+
private phase;
|
|
48
|
+
private mark;
|
|
49
|
+
private started;
|
|
50
|
+
private ended;
|
|
51
|
+
private engine?;
|
|
52
|
+
private removeFrameListener?;
|
|
53
|
+
private restoreDevice?;
|
|
54
|
+
private restoreStore?;
|
|
55
|
+
private timer?;
|
|
56
|
+
private row?;
|
|
57
|
+
private disposed;
|
|
58
|
+
/** Adds it to an engine; `dispose()` takes it off. */
|
|
59
|
+
static attach(engine: EngineKernel, options?: FramePhasesOptions): FramePhases;
|
|
60
|
+
constructor({ samples }?: FramePhasesOptions);
|
|
61
|
+
private enter;
|
|
62
|
+
private readonly begin;
|
|
63
|
+
private readonly close;
|
|
64
|
+
/** The mean of each phase over the frames kept. */
|
|
65
|
+
average(): FramePhaseTimes;
|
|
66
|
+
/** A copy of the numbers. */
|
|
67
|
+
toJSON(): FramePhasesReport;
|
|
68
|
+
/** Shows the averages in a row of the Stats panel, refreshed twice a second until `dispose()`. */
|
|
69
|
+
view(stats: Stats): this;
|
|
70
|
+
private wrapDevice;
|
|
71
|
+
setup(engine: Engine): void;
|
|
72
|
+
onPreUpdate(): void;
|
|
73
|
+
registerPasses(graph: FrameGraph): void;
|
|
74
|
+
onDeviceRestored(engine: Engine): void;
|
|
75
|
+
/** Stops timing and removes the view; the engine goes back to its own calls. */
|
|
76
|
+
dispose(): void;
|
|
77
|
+
[Symbol.dispose]: () => void;
|
|
78
|
+
}
|
package/dist/phases.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// @margo/core-built 0.1.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/phases.ts
|
|
6
|
+
var t = [
|
|
7
|
+
"update",
|
|
8
|
+
"tick",
|
|
9
|
+
"cull",
|
|
10
|
+
"encode",
|
|
11
|
+
"submit",
|
|
12
|
+
"other",
|
|
13
|
+
"total"
|
|
14
|
+
], n = 0, r = 1, i = 2, a = 3, o = 4, s = 5, c = 6, l = -1, u = () => ({
|
|
15
|
+
update: 0,
|
|
16
|
+
tick: 0,
|
|
17
|
+
cull: 0,
|
|
18
|
+
encode: 0,
|
|
19
|
+
submit: 0,
|
|
20
|
+
other: 0,
|
|
21
|
+
total: 0
|
|
22
|
+
}), d = class d {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
23
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
24
|
+
let n = new d(t);
|
|
25
|
+
return e.use(n), n;
|
|
26
|
+
}
|
|
27
|
+
constructor({ samples: e = 120 } = {}) {
|
|
28
|
+
if (this.name = "frame-phases", this.frames = 0, this.last = u(), this.open = /* @__PURE__ */ new Float64Array(6), this.phase = l, this.mark = 0, this.started = 0, this.ended = !1, this.disposed = !1, this.begin = () => {
|
|
29
|
+
this.phase !== l || this.disposed || (this.open.fill(0), this.ended = !1, this.started = this.mark = performance.now(), this.phase = n, queueMicrotask(this.close));
|
|
30
|
+
}, this.close = () => {
|
|
31
|
+
if (this.disposed) return;
|
|
32
|
+
this.enter(l);
|
|
33
|
+
let e = this.frames++ % this.size * t.length, { last: n, open: r, ring: i } = this;
|
|
34
|
+
for (let a = 0; a < c; a++) n[t[a]] = i[e + a] = r[a];
|
|
35
|
+
n.total = i[e + c] = this.mark - this.started;
|
|
36
|
+
}, !Number.isInteger(e) || e < 2 || e > 4096) throw RangeError("Sample count must be 2..4096");
|
|
37
|
+
this.size = e, this.ring = new Float64Array(e * t.length);
|
|
38
|
+
}
|
|
39
|
+
enter(e) {
|
|
40
|
+
let t = performance.now();
|
|
41
|
+
this.open[this.phase] += t - this.mark, this.mark = t, this.phase = e;
|
|
42
|
+
}
|
|
43
|
+
average() {
|
|
44
|
+
let e = u(), n = Math.min(this.frames, this.size);
|
|
45
|
+
for (let r = 0; r < n; r++) for (let i = 0; i <= c; i++) e[t[i]] += this.ring[r * t.length + i] / n;
|
|
46
|
+
return e;
|
|
47
|
+
}
|
|
48
|
+
toJSON() {
|
|
49
|
+
return {
|
|
50
|
+
frames: this.frames,
|
|
51
|
+
last: { ...this.last },
|
|
52
|
+
average: this.average()
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
view(e) {
|
|
56
|
+
if (this.row) return this;
|
|
57
|
+
let t = this.row = e.element.ownerDocument.createElement("div"), n = () => {
|
|
58
|
+
let e = this.average(), n = (e) => e.toFixed(2);
|
|
59
|
+
t.textContent = `CPU ${n(e.total)} ms\nupdate ${n(e.update)} tick ${n(e.tick)} cull ${n(e.cull)}\nencode ${n(e.encode)} submit ${n(e.submit)}`;
|
|
60
|
+
};
|
|
61
|
+
return t.style.whiteSpace = "pre-line", e.element.append(t), n(), this.timer = setInterval(n, 500), this;
|
|
62
|
+
}
|
|
63
|
+
wrapDevice(e) {
|
|
64
|
+
let { queue: t } = e, { createCommandEncoder: n } = e, { submit: r } = t, c = (t) => (this.phase === i && this.enter(a), n.call(e, t)), u = (e) => {
|
|
65
|
+
let n = this.phase;
|
|
66
|
+
if (n === l) return r.call(t, e);
|
|
67
|
+
this.enter(o), r.call(t, e), this.enter(this.ended ? s : n);
|
|
68
|
+
};
|
|
69
|
+
e.createCommandEncoder = c, t.submit = u, this.restoreDevice = () => {
|
|
70
|
+
e.createCommandEncoder === c && (e.createCommandEncoder = n), t.submit === u && (t.submit = r), this.restoreDevice = void 0;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
setup(t) {
|
|
74
|
+
if (this.disposed) throw new e("disposed", "FramePhases is disposed; create a new one and add it to the engine.");
|
|
75
|
+
if (this.engine) throw new e("attached", "FramePhases is already attached to an engine; create one per engine.");
|
|
76
|
+
this.engine = t, this.removeFrameListener = t.onFrame(this.begin);
|
|
77
|
+
let { store: n } = t, { tick: a } = n, o = (e) => {
|
|
78
|
+
if (this.phase === l) return a.call(n, e);
|
|
79
|
+
this.enter(r), a.call(n, e), this.enter(i);
|
|
80
|
+
};
|
|
81
|
+
n.tick = o, this.restoreStore = () => {
|
|
82
|
+
n.tick === o && (n.tick = a);
|
|
83
|
+
}, t.gpu && this.wrapDevice(t.gpu);
|
|
84
|
+
}
|
|
85
|
+
onPreUpdate() {
|
|
86
|
+
this.begin();
|
|
87
|
+
}
|
|
88
|
+
registerPasses(e) {
|
|
89
|
+
e.add({
|
|
90
|
+
name: "frame-phases:begin",
|
|
91
|
+
order: -Number.MAX_VALUE,
|
|
92
|
+
execute: () => {
|
|
93
|
+
this.ended = !1, this.phase === i && this.enter(a);
|
|
94
|
+
}
|
|
95
|
+
}), e.add({
|
|
96
|
+
name: "frame-phases:end",
|
|
97
|
+
order: Number.MAX_VALUE,
|
|
98
|
+
execute: (e) => {
|
|
99
|
+
this.ended = !0, e.backend !== "webgpu" && this.phase === a && this.enter(s);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
onDeviceRestored(e) {
|
|
104
|
+
this.restoreDevice?.(), e.gpu && this.wrapDevice(e.gpu);
|
|
105
|
+
}
|
|
106
|
+
dispose() {
|
|
107
|
+
this.disposed || (this.disposed = !0, this.removeFrameListener?.(), this.removeFrameListener = void 0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0, this.restoreDevice?.(), this.restoreStore?.(), clearInterval(this.timer), this.row?.remove(), this.row = void 0);
|
|
108
|
+
}
|
|
109
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
110
|
+
this.dispose();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
//#endregion
|
|
114
|
+
export { d as FramePhases };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
/** Sums pass durations in milliseconds; one asynchronous readback can be in flight. */
|
|
3
|
+
export declare class GPUProfiler {
|
|
4
|
+
readonly supported: boolean;
|
|
5
|
+
milliseconds: number | null;
|
|
6
|
+
/** Reused storage for the latest complete sample, in timestampWrites() order. */
|
|
7
|
+
readonly passMilliseconds: Float64Array<ArrayBuffer>;
|
|
8
|
+
passCount: number;
|
|
9
|
+
/** First pass beginning to last pass end, including intervening commands and gaps. */
|
|
10
|
+
elapsedMilliseconds: number | null;
|
|
11
|
+
private query?;
|
|
12
|
+
private resolveBuffer?;
|
|
13
|
+
private readBuffer?;
|
|
14
|
+
private readonly descriptors;
|
|
15
|
+
private count;
|
|
16
|
+
private overflow;
|
|
17
|
+
private state;
|
|
18
|
+
private disposed;
|
|
19
|
+
constructor(device: GPUDevice);
|
|
20
|
+
beginFrame(): boolean;
|
|
21
|
+
timestampWrites(): GPURenderPassTimestampWrites | undefined;
|
|
22
|
+
resolve(encoder: GPUCommandEncoder): boolean;
|
|
23
|
+
/** Call after submitting the command buffer containing resolve(). */
|
|
24
|
+
read(): Promise<number | null>;
|
|
25
|
+
private clearSample;
|
|
26
|
+
dispose(): void;
|
|
27
|
+
[Symbol.dispose]: () => void;
|
|
28
|
+
}
|
package/dist/report.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Stats } from './index.js';
|
|
3
|
+
import type { GeometryReport, GeometryStats } from './geometry.js';
|
|
4
|
+
/** One group of live GPU objects. */
|
|
5
|
+
export interface GPUMemoryGroup {
|
|
6
|
+
count: number;
|
|
7
|
+
bytes: number;
|
|
8
|
+
}
|
|
9
|
+
/** What {@link GPUMemory.toJSON} returns. */
|
|
10
|
+
export interface GPUMemoryReport {
|
|
11
|
+
buffers: GPUMemoryGroup;
|
|
12
|
+
textures: GPUMemoryGroup;
|
|
13
|
+
bytes: number;
|
|
14
|
+
/** The same per object label; objects without one are under `''`. */
|
|
15
|
+
labels: Record<string, GPUMemoryGroup>;
|
|
16
|
+
}
|
|
17
|
+
/** What {@link statsReport} returns; safe for `JSON.stringify`. */
|
|
18
|
+
export interface StatsReport {
|
|
19
|
+
fps: number;
|
|
20
|
+
frameTime: number;
|
|
21
|
+
drawCalls: number;
|
|
22
|
+
dispatches: number;
|
|
23
|
+
gpuTime: number | null;
|
|
24
|
+
/** Frame times in milliseconds, oldest first. */
|
|
25
|
+
frames: number[];
|
|
26
|
+
memory?: GPUMemoryReport;
|
|
27
|
+
geometry?: GeometryReport;
|
|
28
|
+
}
|
|
29
|
+
/** The bytes a texture holds, every mip and sample; drivers may pad it. */
|
|
30
|
+
export declare function textureBytes(descriptor: GPUTextureDescriptor): number;
|
|
31
|
+
/**
|
|
32
|
+
* Counts the GPU memory a device's buffers and textures hold, from when it is attached: objects made earlier are not
|
|
33
|
+
* seen. It wraps the device's `createBuffer` and `createTexture` until `dispose()`. Sizes are what the descriptors ask
|
|
34
|
+
* for; drivers may pad them, and depth24plus counts as 4 bytes. `view` shows the totals in a {@link Stats} panel.
|
|
35
|
+
*/
|
|
36
|
+
export declare class GPUMemory {
|
|
37
|
+
private readonly groups;
|
|
38
|
+
private readonly labels;
|
|
39
|
+
private readonly restore;
|
|
40
|
+
private timer?;
|
|
41
|
+
private row?;
|
|
42
|
+
static attach(device: GPUDevice): GPUMemory;
|
|
43
|
+
constructor(device: GPUDevice);
|
|
44
|
+
private count;
|
|
45
|
+
/** Live bytes in all buffers and textures. */
|
|
46
|
+
get bytes(): number;
|
|
47
|
+
/** A copy of the current totals. */
|
|
48
|
+
toJSON(): GPUMemoryReport;
|
|
49
|
+
/** Shows the totals in a row of the Stats panel, refreshed twice a second until `dispose()`. */
|
|
50
|
+
view(stats: Stats): this;
|
|
51
|
+
/** Stops counting and removes the view; objects keep working. */
|
|
52
|
+
dispose(): void;
|
|
53
|
+
[Symbol.dispose]: () => void;
|
|
54
|
+
}
|
|
55
|
+
/** The numbers of a `Stats`, and optionally a {@link GPUMemory}, as a plain object: `JSON.stringify(statsReport(stats))`. */
|
|
56
|
+
export declare function statsReport(stats: Stats, memory?: GPUMemory, geometry?: GeometryStats): StatsReport;
|
package/dist/report.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
//#region src/report.ts
|
|
5
|
+
var e = [
|
|
6
|
+
[/^(r8|stencil8)/, 1],
|
|
7
|
+
[/^(r16|rg8|depth16)/, 2],
|
|
8
|
+
[/^(rgba16|rg32|depth32float-stencil8)/, 8],
|
|
9
|
+
[/^rgba32/, 16]
|
|
10
|
+
];
|
|
11
|
+
function t(t) {
|
|
12
|
+
let n = /^astc-(\d+)x(\d+)/.exec(t);
|
|
13
|
+
return n ? {
|
|
14
|
+
size: 16,
|
|
15
|
+
width: +n[1],
|
|
16
|
+
height: +n[2]
|
|
17
|
+
} : /^(bc1|bc4|etc2-rgb8|eac-r11)/.test(t) ? {
|
|
18
|
+
size: 8,
|
|
19
|
+
width: 4,
|
|
20
|
+
height: 4
|
|
21
|
+
} : /^(bc|etc2|eac)/.test(t) ? {
|
|
22
|
+
size: 16,
|
|
23
|
+
width: 4,
|
|
24
|
+
height: 4
|
|
25
|
+
} : {
|
|
26
|
+
size: e.find(([e]) => e.test(t))?.[1] ?? 4,
|
|
27
|
+
width: 1,
|
|
28
|
+
height: 1
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function n(e) {
|
|
32
|
+
let n = e.size, [r, i = 1, a = 1] = Array.isArray(n) ? n : [
|
|
33
|
+
n.width,
|
|
34
|
+
n.height,
|
|
35
|
+
n.depthOrArrayLayers
|
|
36
|
+
], { size: o, width: s, height: c } = t(e.format), l = e.dimension === "3d", u = 0;
|
|
37
|
+
for (let t = 0; t < (e.mipLevelCount ?? 1); t++) {
|
|
38
|
+
let e = Math.max(r >> t, 1), n = Math.max(i >> t, 1), d = l ? Math.max(a >> t, 1) : a;
|
|
39
|
+
u += Math.ceil(e / s) * Math.ceil(n / c) * d * o;
|
|
40
|
+
}
|
|
41
|
+
return u * (e.sampleCount ?? 1);
|
|
42
|
+
}
|
|
43
|
+
var r = class e {
|
|
44
|
+
static attach(t) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
45
|
+
return new e(t);
|
|
46
|
+
}
|
|
47
|
+
constructor(e) {
|
|
48
|
+
this.groups = {
|
|
49
|
+
buffers: {
|
|
50
|
+
count: 0,
|
|
51
|
+
bytes: 0
|
|
52
|
+
},
|
|
53
|
+
textures: {
|
|
54
|
+
count: 0,
|
|
55
|
+
bytes: 0
|
|
56
|
+
}
|
|
57
|
+
}, this.labels = /* @__PURE__ */ new Map();
|
|
58
|
+
let t = e.createBuffer, r = e.createTexture, i = !0, a = (n) => {
|
|
59
|
+
let r = t.call(e, n);
|
|
60
|
+
return i ? this.count(r, this.groups.buffers, n.size, n.label) : r;
|
|
61
|
+
}, o = (t) => {
|
|
62
|
+
let a = r.call(e, t);
|
|
63
|
+
return i ? this.count(a, this.groups.textures, n(t), t.label) : a;
|
|
64
|
+
};
|
|
65
|
+
e.createBuffer = a, e.createTexture = o, this.restore = () => {
|
|
66
|
+
i = !1, e.createBuffer === a && (e.createBuffer = t), e.createTexture === o && (e.createTexture = r);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
count(e, t, n, r = "") {
|
|
70
|
+
let i = this.labels.get(r);
|
|
71
|
+
i || this.labels.set(r, i = {
|
|
72
|
+
count: 0,
|
|
73
|
+
bytes: 0
|
|
74
|
+
});
|
|
75
|
+
let a = (e) => {
|
|
76
|
+
t.count += e, t.bytes += e * n, i.count += e, i.bytes += e * n;
|
|
77
|
+
};
|
|
78
|
+
a(1);
|
|
79
|
+
let o = e.destroy, s = !0;
|
|
80
|
+
return e.destroy = () => {
|
|
81
|
+
s && a(-1), s = !1, o.call(e);
|
|
82
|
+
}, e;
|
|
83
|
+
}
|
|
84
|
+
get bytes() {
|
|
85
|
+
return this.groups.buffers.bytes + this.groups.textures.bytes;
|
|
86
|
+
}
|
|
87
|
+
toJSON() {
|
|
88
|
+
return {
|
|
89
|
+
buffers: { ...this.groups.buffers },
|
|
90
|
+
textures: { ...this.groups.textures },
|
|
91
|
+
bytes: this.bytes,
|
|
92
|
+
labels: Object.fromEntries([...this.labels].map(([e, t]) => [e, { ...t }]))
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
view(e) {
|
|
96
|
+
if (this.row) return this;
|
|
97
|
+
let t = this.row = e.element.ownerDocument.createElement("div"), n = () => {
|
|
98
|
+
let { buffers: e, textures: n } = this.groups;
|
|
99
|
+
t.textContent = `GPU ${(this.bytes / 1048576).toFixed(1)} MB, ${e.count} buffers, ${n.count} textures`;
|
|
100
|
+
};
|
|
101
|
+
return e.element.append(t), n(), this.timer = setInterval(n, 500), this;
|
|
102
|
+
}
|
|
103
|
+
dispose() {
|
|
104
|
+
clearInterval(this.timer), this.row?.remove(), this.row = void 0, this.restore();
|
|
105
|
+
}
|
|
106
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
107
|
+
this.dispose();
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
function i(e, t, n) {
|
|
111
|
+
let r = [], i = e.samples.length;
|
|
112
|
+
for (let t = 0; t < e.sampleCount; t++) r.push(e.samples[(e.sampleIndex - e.sampleCount + t + i) % i]);
|
|
113
|
+
return {
|
|
114
|
+
fps: e.fps,
|
|
115
|
+
frameTime: e.frameTime,
|
|
116
|
+
drawCalls: e.drawCalls,
|
|
117
|
+
dispatches: e.dispatches,
|
|
118
|
+
gpuTime: e.gpuTime,
|
|
119
|
+
frames: r,
|
|
120
|
+
...t && { memory: t.toJSON() },
|
|
121
|
+
...n && { geometry: n.toJSON() }
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
//#endregion
|
|
125
|
+
export { r as GPUMemory, i as statsReport, n as textureBytes };
|