@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
|
@@ -0,0 +1,368 @@
|
|
|
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/profiler.ts
|
|
6
|
+
var t = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1 ? 0 : 1, n = 1 - t, r = 4294967296, i = class {
|
|
7
|
+
constructor(e) {
|
|
8
|
+
if (this.milliseconds = null, this.passMilliseconds = /* @__PURE__ */ new Float64Array(128), this.passCount = 0, this.elapsedMilliseconds = null, this.descriptors = [], this.count = 0, this.overflow = !1, this.state = "idle", this.disposed = !1, this.supported = e.features.has("timestamp-query"), this.supported) try {
|
|
9
|
+
this.query = e.createQuerySet({
|
|
10
|
+
type: "timestamp",
|
|
11
|
+
count: 256
|
|
12
|
+
}), this.resolveBuffer = e.createBuffer({
|
|
13
|
+
size: 2048,
|
|
14
|
+
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC
|
|
15
|
+
}), this.readBuffer = e.createBuffer({
|
|
16
|
+
size: 2048,
|
|
17
|
+
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
|
|
18
|
+
});
|
|
19
|
+
for (let e = 0; e < 128; e++) this.descriptors.push({
|
|
20
|
+
querySet: this.query,
|
|
21
|
+
beginningOfPassWriteIndex: e * 2,
|
|
22
|
+
endOfPassWriteIndex: e * 2 + 1
|
|
23
|
+
});
|
|
24
|
+
} catch (e) {
|
|
25
|
+
throw this.dispose(), e;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
beginFrame() {
|
|
29
|
+
if (this.state === "disposed") throw new e("disposed", "GPU profiler is disposed; create a new GPUProfiler to time GPU passes.");
|
|
30
|
+
return !this.supported || this.state !== "idle" ? !1 : (this.count = 0, this.overflow = !1, this.state = "recording", !0);
|
|
31
|
+
}
|
|
32
|
+
timestampWrites() {
|
|
33
|
+
if (this.state === "recording") {
|
|
34
|
+
if (this.count === this.descriptors.length) this.overflow = !0;
|
|
35
|
+
else return this.descriptors[this.count++];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
resolve(e) {
|
|
39
|
+
return this.state === "recording" ? !this.count || this.overflow ? (this.clearSample(), this.state = "idle", !1) : (e.resolveQuerySet(this.query, 0, this.count * 2, this.resolveBuffer, 0), e.copyBufferToBuffer(this.resolveBuffer, 0, this.readBuffer, 0, this.count * 16), this.state = "resolved", !0) : !1;
|
|
40
|
+
}
|
|
41
|
+
async read() {
|
|
42
|
+
if (this.state !== "resolved") return null;
|
|
43
|
+
this.state = "reading";
|
|
44
|
+
let i = this.readBuffer;
|
|
45
|
+
try {
|
|
46
|
+
if (await i.mapAsync(GPUMapMode.READ, 0, this.count * 16), this.disposed) return null;
|
|
47
|
+
let a = new Uint32Array(i.getMappedRange(0, this.count * 16)), o = 0, s = 0, c = 0, l = 0, u = 0, d = 0, f = a[t], p = a[n];
|
|
48
|
+
for (let i = 0; i < a.length; i += 4) {
|
|
49
|
+
let m = a[i + t], h = a[i + n], g = a[i + 2 + t], _ = a[i + 2 + n];
|
|
50
|
+
if (_ < h || _ === h && g < m || i > 0 && (_ < l || _ === l && g < c)) throw new e("failed", "Invalid GPU timestamps (end before start or time went backwards); the sample is discarded, check the device timestamp support.");
|
|
51
|
+
let v = g - m, y = _ - h;
|
|
52
|
+
v < 0 && (v += r, y--), o += v, s += y, o >= r && (o -= r, s++), c = m, l = h, (h < p || h === p && m < f) && (f = m, p = h), (_ > d || _ === d && g > u) && (u = g, d = _);
|
|
53
|
+
}
|
|
54
|
+
for (let e = 0; e < this.count; e++) {
|
|
55
|
+
let i = a[e * 4 + 2 + t] - a[e * 4 + t], o = a[e * 4 + 2 + n] - a[e * 4 + n];
|
|
56
|
+
i < 0 && (i += r, o--), this.passMilliseconds[e] = (o * r + i) / 1e6;
|
|
57
|
+
}
|
|
58
|
+
this.passMilliseconds.fill(0, this.count), this.passCount = this.count;
|
|
59
|
+
let m = u - f, h = d - p;
|
|
60
|
+
return m < 0 && (m += r, h--), this.elapsedMilliseconds = (h * r + m) / 1e6, this.milliseconds = (s * r + o) / 1e6;
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (this.clearSample(), !this.disposed) throw e;
|
|
63
|
+
return null;
|
|
64
|
+
} finally {
|
|
65
|
+
this.disposed || (i.unmap(), this.state = "idle");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
clearSample() {
|
|
69
|
+
this.milliseconds = this.elapsedMilliseconds = null, this.passCount = 0, this.passMilliseconds.fill(0);
|
|
70
|
+
}
|
|
71
|
+
dispose() {
|
|
72
|
+
this.state !== "disposed" && (this.disposed = !0, this.state = "disposed", this.query?.destroy(), this.resolveBuffer?.destroy(), this.readBuffer?.destroy(), this.query = this.resolveBuffer = this.readBuffer = void 0, this.clearSample());
|
|
73
|
+
}
|
|
74
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
75
|
+
this.dispose();
|
|
76
|
+
}
|
|
77
|
+
}, a = class {
|
|
78
|
+
constructor(e, t, n, r) {
|
|
79
|
+
this.owner = e, this.getPrototype = Object.getPrototypeOf, this.setPrototype = Reflect.setPrototypeOf, this.reflectSet = Reflect.set, this.own = Object.hasOwn, this.extensible = Object.isExtensible, this.lookupGetter = Reflect.get(Object.prototype, "__lookupGetter__"), this.lookupSetter = Reflect.get(Object.prototype, "__lookupSetter__"), this.source = Function.prototype.toString;
|
|
80
|
+
try {
|
|
81
|
+
if (typeof GPUCommandEncoder != "function" || typeof this.lookupGetter != "function" || typeof this.lookupSetter != "function") return;
|
|
82
|
+
let i = GPUCommandEncoder.prototype;
|
|
83
|
+
if (this.encoderPrototype = i, typeof GPURenderPassEncoder == "function") {
|
|
84
|
+
let r = GPURenderPassEncoder.prototype, a = this.method(r, "draw"), o = this.method(r, "drawIndexed"), s = this.method(r, "drawIndirect"), c = this.method(r, "drawIndexedIndirect");
|
|
85
|
+
a && o && s && c && (this.renderKind = this.capture(r, i, "beginRenderPass", e, {
|
|
86
|
+
stats: t,
|
|
87
|
+
draw: a,
|
|
88
|
+
indexed: o,
|
|
89
|
+
indirect: s,
|
|
90
|
+
indexedIndirect: c
|
|
91
|
+
}, [
|
|
92
|
+
"draw",
|
|
93
|
+
"drawIndexed",
|
|
94
|
+
"drawIndirect",
|
|
95
|
+
"drawIndexedIndirect"
|
|
96
|
+
], n));
|
|
97
|
+
}
|
|
98
|
+
if (typeof GPUComputePassEncoder == "function") {
|
|
99
|
+
let n = GPUComputePassEncoder.prototype, a = this.method(n, "dispatchWorkgroups"), o = this.method(n, "dispatchWorkgroupsIndirect");
|
|
100
|
+
a && o && (this.computeKind = this.capture(n, i, "beginComputePass", e, {
|
|
101
|
+
stats: t,
|
|
102
|
+
dispatch: a,
|
|
103
|
+
indirect: o
|
|
104
|
+
}, ["dispatchWorkgroups", "dispatchWorkgroupsIndirect"], r));
|
|
105
|
+
}
|
|
106
|
+
} catch {}
|
|
107
|
+
}
|
|
108
|
+
method(e, t) {
|
|
109
|
+
let n = Object.getOwnPropertyDescriptor(e, t)?.value;
|
|
110
|
+
return typeof n == "function" && this.source.call(n).replace(/\s/g, "") === `function${t}(){[nativecode]}` ? n : void 0;
|
|
111
|
+
}
|
|
112
|
+
capture(e, t, n, r, i, a, o) {
|
|
113
|
+
let s = this.method(t, n);
|
|
114
|
+
if (!s) return;
|
|
115
|
+
let c = [], l = [], u = Object.create(null);
|
|
116
|
+
for (let t of Object.getOwnPropertyNames(e)) {
|
|
117
|
+
let n = Object.getOwnPropertyDescriptor(e, t);
|
|
118
|
+
if (t === "constructor" || typeof n.value != "function") continue;
|
|
119
|
+
let r = this.method(e, t);
|
|
120
|
+
if (!r) return;
|
|
121
|
+
c.push(t), l.push(r), Object.defineProperty(u, t, {
|
|
122
|
+
value: r,
|
|
123
|
+
writable: !0
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
let d = Object.create(e), f = (e, t) => {
|
|
127
|
+
Object.defineProperty(d, e, {
|
|
128
|
+
enumerable: !0,
|
|
129
|
+
get() {
|
|
130
|
+
return t;
|
|
131
|
+
},
|
|
132
|
+
set(t) {
|
|
133
|
+
if (this === d) throw TypeError("Shared Stats pass prototype is immutable");
|
|
134
|
+
Object.defineProperty(this, e, {
|
|
135
|
+
value: t,
|
|
136
|
+
writable: !0,
|
|
137
|
+
enumerable: !0,
|
|
138
|
+
configurable: !0
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
f(r, i);
|
|
144
|
+
for (let e = 0; e < a.length; e++) f(a[e], o[e]);
|
|
145
|
+
return {
|
|
146
|
+
factory: s,
|
|
147
|
+
prototype: e,
|
|
148
|
+
parent: this.getPrototype(e),
|
|
149
|
+
names: c,
|
|
150
|
+
originals: l,
|
|
151
|
+
probe: u,
|
|
152
|
+
derived: Object.freeze(d)
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
wrap(e, t, n, r) {
|
|
156
|
+
if (!r || n !== r.factory || this.getPrototype(t) !== this.encoderPrototype || this.getPrototype(e) !== r.prototype || !this.extensible(e) || this.owner in e || this.getPrototype(r.prototype) !== r.parent) return !1;
|
|
157
|
+
for (let t = 0; t < r.names.length; t++) {
|
|
158
|
+
let n = r.names[t], i = r.originals[t];
|
|
159
|
+
if (this.own(e, n) || !this.own(r.prototype, n) || this.lookupGetter.call(r.prototype, n) || this.lookupSetter.call(r.prototype, n) || r.prototype[n] !== i || !this.reflectSet(r.prototype, n, i, r.probe)) return !1;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
return this.setPrototype(e, r.derived);
|
|
163
|
+
} catch {
|
|
164
|
+
return !1;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
render(e, t, n) {
|
|
168
|
+
return this.wrap(e, t, n, this.renderKind);
|
|
169
|
+
}
|
|
170
|
+
compute(e, t, n) {
|
|
171
|
+
return this.wrap(e, t, n, this.computeKind);
|
|
172
|
+
}
|
|
173
|
+
}, o = Symbol("stats owner");
|
|
174
|
+
function s(e, t = 1, n = 0, r = 0) {
|
|
175
|
+
let i = this[o];
|
|
176
|
+
i.stats.drawCalls++, i.draw.call(this, e, t, n, r);
|
|
177
|
+
}
|
|
178
|
+
function c(e, t = 1, n = 0, r = 0, i = 0) {
|
|
179
|
+
let a = this[o];
|
|
180
|
+
a.stats.drawCalls++, a.indexed.call(this, e, t, n, r, i);
|
|
181
|
+
}
|
|
182
|
+
function l(e, t) {
|
|
183
|
+
let n = this[o];
|
|
184
|
+
n.stats.drawCalls++, n.indirect.call(this, e, t);
|
|
185
|
+
}
|
|
186
|
+
function u(e, t) {
|
|
187
|
+
let n = this[o];
|
|
188
|
+
n.stats.drawCalls++, n.indexedIndirect.call(this, e, t);
|
|
189
|
+
}
|
|
190
|
+
function d(e, t = 1, n = 1) {
|
|
191
|
+
let r = this[o];
|
|
192
|
+
r.stats.dispatches++, r.dispatch.call(this, e, t, n);
|
|
193
|
+
}
|
|
194
|
+
function f(e, t) {
|
|
195
|
+
let n = this[o];
|
|
196
|
+
n.stats.dispatches++, n.indirect.call(this, e, t);
|
|
197
|
+
}
|
|
198
|
+
var p = {}, m = Symbol("render pass"), h = Symbol("compute pass"), g = Symbol("finish"), _ = Symbol("frame profiler"), v = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
199
|
+
get gpuSampleId() {
|
|
200
|
+
return this.completedGPU;
|
|
201
|
+
}
|
|
202
|
+
get gpuSampleEncodedAt() {
|
|
203
|
+
return this.disposed ? null : this.encodedGPUAt;
|
|
204
|
+
}
|
|
205
|
+
get gpuSampleReadAt() {
|
|
206
|
+
return this.disposed ? null : this.readGPUAt;
|
|
207
|
+
}
|
|
208
|
+
get gpuPassMilliseconds() {
|
|
209
|
+
return !this.disposed && this.profiler?.supported ? this.profiler.passMilliseconds : null;
|
|
210
|
+
}
|
|
211
|
+
get gpuPassCount() {
|
|
212
|
+
return this.profiler?.passCount ?? 0;
|
|
213
|
+
}
|
|
214
|
+
get gpuElapsedMilliseconds() {
|
|
215
|
+
return this.profiler?.elapsedMilliseconds ?? null;
|
|
216
|
+
}
|
|
217
|
+
constructor({ parent: e = document.body, samples: t = 120, profiling: n = !1, lowAllocationPasses: r = !1 } = {}) {
|
|
218
|
+
if (this.name = "stats", this.fps = 0, this.frameTime = 0, this.drawCalls = 0, this.dispatches = 0, this.gpuTime = null, this.sampleCount = 0, this.sampleIndex = 0, this.total = 0, this.last = 0, this.tracked = /* @__PURE__ */ new WeakSet(), this.renderMethods = /* @__PURE__ */ new WeakMap(), this.computeMethods = /* @__PURE__ */ new WeakMap(), this.disposed = !1, this.profileFrame = 0, this.completedGPU = 0, this.encodedGPUAt = null, this.readGPUAt = null, this.ticks = 0, this.shown = "", !Number.isInteger(t) || t < 2 || t > 4096) throw RangeError("Sample count must be 2..4096");
|
|
219
|
+
if (typeof n != "boolean") throw TypeError("profiling must be boolean");
|
|
220
|
+
if (typeof r != "boolean") throw TypeError("lowAllocationPasses must be boolean");
|
|
221
|
+
this.profiling = n, this.lowAllocationPasses = r, this.samples = new Float64Array(t);
|
|
222
|
+
let i = e.ownerDocument;
|
|
223
|
+
this.element = i.createElement("div"), this.element.dataset.margoStats = "", this.element.style.cssText = "position:fixed;top:12px;left:12px;width:224px;max-width:calc(100vw - 24px);padding:10px;box-sizing:border-box;background:#202226;color:#f5f7fa;border:1px solid #4b5058;border-radius:4px;font:12px/1.5 monospace;z-index:10000;pointer-events:none", this.text = i.createElement("div"), this.text.style.whiteSpace = "pre-line", this.graph = i.createElement("canvas"), this.graph.width = t, this.graph.height = 52, this.graph.style.cssText = "display:block;width:100%;height:52px;margin-top:6px", this.graph.setAttribute("role", "img"), this.graph.setAttribute("aria-label", "Rolling frame time in milliseconds"), this.context = this.graph.getContext("2d"), this.element.append(this.text, this.graph), e.append(this.element), this.refresh(), this.timer = setInterval(() => {
|
|
224
|
+
this.key() !== this.shown && this.refresh();
|
|
225
|
+
}, 150);
|
|
226
|
+
}
|
|
227
|
+
key() {
|
|
228
|
+
return `${this.ticks} ${this.fps} ${this.frameTime} ${this.drawCalls} ${this.dispatches} ${this.gpuTime} ${this.gpuElapsedMilliseconds} ${this.profiler?.supported} ${!this.profiler} ${this.profiling}`;
|
|
229
|
+
}
|
|
230
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
231
|
+
let r = new t(n);
|
|
232
|
+
try {
|
|
233
|
+
return e.use(r), r;
|
|
234
|
+
} catch (e) {
|
|
235
|
+
throw r.dispose(), e;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
tick(t, n = this.drawCalls, r = this.dispatches) {
|
|
239
|
+
if (this.disposed) throw new e("disposed", "Stats is disposed; create a new Stats to sample frames.");
|
|
240
|
+
if (!Number.isFinite(t) || t <= 0 || !Number.isSafeInteger(n) || n < 0 || !Number.isSafeInteger(r) || r < 0) throw RangeError("Invalid frame sample or counters; pass milliseconds above 0 and non-negative integer drawCalls and dispatches.");
|
|
241
|
+
let i = this.sampleIndex;
|
|
242
|
+
this.total += t - this.samples[i], this.samples[i] = t, this.sampleCount < this.samples.length && this.sampleCount++, this.sampleIndex = (i + 1) % this.samples.length, this.frameTime = this.total / this.sampleCount, this.fps = 1e3 / this.frameTime, this.drawCalls = n, this.dispatches = r, this.ticks++;
|
|
243
|
+
}
|
|
244
|
+
enableGPUProfiling(t) {
|
|
245
|
+
if (this.disposed) throw new e("disposed", "Stats is disposed; create a new Stats to enable GPU profiling.");
|
|
246
|
+
return this.profiler?.dispose(), this.profiler = new i(t), this.gpuTime = null, this.encodedGPUAt = this.readGPUAt = null, this.profileFrame = 0, this.profiler.supported;
|
|
247
|
+
}
|
|
248
|
+
track(n) {
|
|
249
|
+
if (this.disposed) throw new e("disposed", "Stats is disposed; create a new Stats to track encoders.");
|
|
250
|
+
if (this.tracked.has(n)) return;
|
|
251
|
+
this.tracked.add(n), this.drawCalls = this.dispatches = 0, this.profileFrame = (this.profileFrame + 1) % 15;
|
|
252
|
+
let r = this.profileFrame === 0 && this.profiler?.beginFrame() ? this.profiler : void 0, i = n;
|
|
253
|
+
i[o] = this, i[_] = r, i[m] = n.beginRenderPass, i[h] = n.beginComputePass, i[g] = n.finish, n.beginRenderPass = t.beginRender, n.beginComputePass = t.beginCompute, n.finish = t.finish;
|
|
254
|
+
}
|
|
255
|
+
static beginRender(e) {
|
|
256
|
+
let t = e.timestampWrites ? void 0 : this[_]?.timestampWrites(), n = this[m].call(this, t ? {
|
|
257
|
+
...e,
|
|
258
|
+
timestampWrites: t
|
|
259
|
+
} : e), r = this[o];
|
|
260
|
+
if (r.lowAllocationPasses && (r.passPrototypes ??= new a(o, r, [
|
|
261
|
+
s,
|
|
262
|
+
c,
|
|
263
|
+
l,
|
|
264
|
+
u
|
|
265
|
+
], [d, f])).render(n, this, this[m])) return n;
|
|
266
|
+
let i = n[o];
|
|
267
|
+
if (!i || i.stats !== r) {
|
|
268
|
+
let e = i ? i.draw : n.draw, t = i ? i.indexed : n.drawIndexed, a = i ? i.indirect : n.drawIndirect, s = i ? i.indexedIndirect : n.drawIndexedIndirect, c = r.renderMethods.get(e);
|
|
269
|
+
(!c || c.indexed !== t || c.indirect !== a || c.indexedIndirect !== s) && (c = {
|
|
270
|
+
stats: r,
|
|
271
|
+
draw: e,
|
|
272
|
+
indexed: t,
|
|
273
|
+
indirect: a,
|
|
274
|
+
indexedIndirect: s
|
|
275
|
+
}, typeof e == "function" && r.renderMethods.set(e, c)), n[o] = c;
|
|
276
|
+
}
|
|
277
|
+
return n.draw = s, n.drawIndexed = c, n.drawIndirect = l, n.drawIndexedIndirect = u, n;
|
|
278
|
+
}
|
|
279
|
+
static beginCompute(e = p) {
|
|
280
|
+
let t = e.timestampWrites ? void 0 : this[_]?.timestampWrites(), n = this[h].call(this, t ? {
|
|
281
|
+
...e,
|
|
282
|
+
timestampWrites: t
|
|
283
|
+
} : e), r = this[o];
|
|
284
|
+
if (r.lowAllocationPasses && (r.passPrototypes ??= new a(o, r, [
|
|
285
|
+
s,
|
|
286
|
+
c,
|
|
287
|
+
l,
|
|
288
|
+
u
|
|
289
|
+
], [d, f])).compute(n, this, this[h])) return n;
|
|
290
|
+
let i = n[o];
|
|
291
|
+
if (!i || i.stats !== r) {
|
|
292
|
+
let e = i ? i.dispatch : n.dispatchWorkgroups, t = i ? i.indirect : n.dispatchWorkgroupsIndirect, a = r.computeMethods.get(e);
|
|
293
|
+
(!a || a.indirect !== t) && (a = {
|
|
294
|
+
stats: r,
|
|
295
|
+
dispatch: e,
|
|
296
|
+
indirect: t
|
|
297
|
+
}, typeof e == "function" && r.computeMethods.set(e, a)), n[o] = a;
|
|
298
|
+
}
|
|
299
|
+
return n.dispatchWorkgroups = d, n.dispatchWorkgroupsIndirect = f, n;
|
|
300
|
+
}
|
|
301
|
+
static finish(e) {
|
|
302
|
+
let n = this[o], r = this[_], i = r?.resolve(this), a = this[g].call(this, e), s = i ? performance.now() : null;
|
|
303
|
+
return r && n.profiler === r && !i && r.milliseconds === null && (n.gpuTime = null, n.encodedGPUAt = n.readGPUAt = null), i && t.readback(n, r, s), a;
|
|
304
|
+
}
|
|
305
|
+
static readback(e, t, n) {
|
|
306
|
+
queueMicrotask(() => {
|
|
307
|
+
t.read().then((r) => {
|
|
308
|
+
!e.disposed && e.profiler === t && (e.gpuTime = r, r === null ? e.encodedGPUAt = e.readGPUAt = null : (e.completedGPU++, e.encodedGPUAt = n, e.readGPUAt = performance.now()));
|
|
309
|
+
}, (n) => {
|
|
310
|
+
!e.disposed && e.profiler === t && (e.gpuTime = null, e.encodedGPUAt = e.readGPUAt = null), e.engine?.onError(n);
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
setup(t) {
|
|
315
|
+
if (this.disposed) throw new e("disposed", "Stats is disposed; create a new Stats and add it to the engine.");
|
|
316
|
+
if (this.engine) throw new e("attached", "Stats is already attached to an engine; create one Stats per engine.");
|
|
317
|
+
this.engine = t, this.removeFrameListener = t.onFrame?.((e, t) => {
|
|
318
|
+
t === 0 && (this.last = 0);
|
|
319
|
+
}), this.profiling && t.gpu && this.enableGPUProfiling(t.gpu);
|
|
320
|
+
}
|
|
321
|
+
registerPasses(e) {
|
|
322
|
+
e.add({
|
|
323
|
+
name: "stats:begin",
|
|
324
|
+
order: -Number.MAX_VALUE,
|
|
325
|
+
execute: (e) => {
|
|
326
|
+
e.backend === "webgpu" && this.track(e.encoder);
|
|
327
|
+
}
|
|
328
|
+
}), e.add({
|
|
329
|
+
name: "stats:sample",
|
|
330
|
+
order: Number.MAX_VALUE,
|
|
331
|
+
execute: (e) => {
|
|
332
|
+
if (e.delta <= 0) return;
|
|
333
|
+
let t = performance.now(), n = this.last ? t - this.last : e.delta * 1e3;
|
|
334
|
+
this.last = t, n > 0 && this.tick(n, e.backend === "webgpu" ? this.drawCalls : this.engine.stats.drawCalls, this.dispatches);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
refresh() {
|
|
339
|
+
if (this.disposed) return;
|
|
340
|
+
this.shown = this.key();
|
|
341
|
+
let e = this.gpuTime === null ? this.profiler?.supported ? "warming up" : this.profiling || this.profiler ? "unavailable" : "off" : this.gpuTime.toFixed(2) + " ms passes";
|
|
342
|
+
this.text.textContent = `${this.fps.toFixed(0)} FPS ${this.frameTime.toFixed(2)} ms\n${this.drawCalls} draws ${this.dispatches} dispatches\nGPU timing ${e}${this.gpuElapsedMilliseconds === null ? "" : "\nGPU span " + this.gpuElapsedMilliseconds.toFixed(2) + " ms"}`;
|
|
343
|
+
let t = this.context;
|
|
344
|
+
if (!t) return;
|
|
345
|
+
let n = this.graph.width, r = this.graph.height;
|
|
346
|
+
t.fillStyle = "#151719", t.fillRect(0, 0, n, r);
|
|
347
|
+
let i = 33.3;
|
|
348
|
+
for (let e = 0; e < this.sampleCount; e++) i = Math.max(i, this.samples[e]);
|
|
349
|
+
t.strokeStyle = "#6c757d", t.beginPath(), t.moveTo(0, r * (1 - 16.67 / i)), t.lineTo(n, r * (1 - 16.67 / i)), t.stroke(), t.strokeStyle = "#63d9b0", t.beginPath();
|
|
350
|
+
let a = this.sampleCount === n ? this.sampleIndex : 0;
|
|
351
|
+
for (let e = 0; e < this.sampleCount; e++) {
|
|
352
|
+
let o = r - 1 - this.samples[(a + e) % n] / i * (r - 2);
|
|
353
|
+
e === 0 ? t.moveTo(e, o) : t.lineTo(e, o);
|
|
354
|
+
}
|
|
355
|
+
t.stroke();
|
|
356
|
+
}
|
|
357
|
+
onDeviceRestored(e) {
|
|
358
|
+
this.profiler?.dispose(), this.profiler = void 0, this.gpuTime = null, this.encodedGPUAt = this.readGPUAt = null, this.last = 0, this.profiling && e.gpu && this.enableGPUProfiling(e.gpu);
|
|
359
|
+
}
|
|
360
|
+
dispose() {
|
|
361
|
+
this.disposed || (this.disposed = !0, this.removeFrameListener?.(), this.removeFrameListener = void 0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0, clearInterval(this.timer), this.profiler?.dispose(), this.element.remove());
|
|
362
|
+
}
|
|
363
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
364
|
+
this.dispose();
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
//#endregion
|
|
368
|
+
export { i as n, v as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,82 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@margoengine/stats",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Allocation-free frame samples, DOM performance overlay, optional WebGPU timestamp profiling, pipeline compile tracking, a CPU phase breakdown, leak alerts and field telemetry.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"margoengine",
|
|
7
|
+
"webgpu",
|
|
8
|
+
"3d",
|
|
9
|
+
"stats"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://margoengine.com",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/scottcampbelldata/margoengine/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/scottcampbelldata/margoengine.git",
|
|
18
|
+
"directory": "packages/stats"
|
|
19
|
+
},
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"author": "Scott Campbell",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./beacon": {
|
|
30
|
+
"types": "./dist/beacon.d.ts",
|
|
31
|
+
"import": "./dist/beacon.js"
|
|
32
|
+
},
|
|
33
|
+
"./compile": {
|
|
34
|
+
"types": "./dist/compile.d.ts",
|
|
35
|
+
"import": "./dist/compile.js"
|
|
36
|
+
},
|
|
37
|
+
"./leaks": {
|
|
38
|
+
"types": "./dist/leaks.d.ts",
|
|
39
|
+
"import": "./dist/leaks.js"
|
|
40
|
+
},
|
|
41
|
+
"./phases": {
|
|
42
|
+
"types": "./dist/phases.d.ts",
|
|
43
|
+
"import": "./dist/phases.js"
|
|
44
|
+
},
|
|
45
|
+
"./report": {
|
|
46
|
+
"types": "./dist/report.d.ts",
|
|
47
|
+
"import": "./dist/report.js"
|
|
48
|
+
},
|
|
49
|
+
"./geometry": {
|
|
50
|
+
"types": "./dist/geometry.d.ts",
|
|
51
|
+
"import": "./dist/geometry.js"
|
|
52
|
+
},
|
|
53
|
+
"./gl": {
|
|
54
|
+
"types": "./dist/gl.d.ts",
|
|
55
|
+
"import": "./dist/gl.js"
|
|
56
|
+
},
|
|
57
|
+
"./panels": {
|
|
58
|
+
"types": "./dist/panels.d.ts",
|
|
59
|
+
"import": "./dist/panels.js"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"files": [
|
|
63
|
+
"dist",
|
|
64
|
+
"LICENSE",
|
|
65
|
+
"NOTICE"
|
|
66
|
+
],
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public"
|
|
69
|
+
},
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"@margoengine/core": "^1.0.0"
|
|
72
|
+
},
|
|
73
|
+
"dependencies": {
|
|
74
|
+
"@margoengine/math": "^1.0.0"
|
|
75
|
+
},
|
|
76
|
+
"scripts": {
|
|
77
|
+
"build": "node ../../scripts/build-optional.js stats",
|
|
78
|
+
"test": "npm run build && node --test test/*.test.js",
|
|
79
|
+
"test:browser": "npm run build && node ../../scripts/test-stats-browser.mjs",
|
|
80
|
+
"prepack": "npm run build"
|
|
81
|
+
}
|
|
82
|
+
}
|