@playcanvas/inspector 0.2.0 → 0.4.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.
Files changed (49) hide show
  1. package/README.md +147 -5
  2. package/package.json +1 -1
  3. package/src/asset-view.js +24 -17
  4. package/src/camera-view.js +5 -3
  5. package/src/context-menu.js +231 -0
  6. package/src/copy-json.js +692 -0
  7. package/src/describe.js +20 -1
  8. package/src/entity-inspector-script.js +10 -0
  9. package/src/frame-graph-view.js +41 -9
  10. package/src/hierarchy-view.js +29 -3
  11. package/src/inspector.js +958 -80
  12. package/src/list-view.js +118 -12
  13. package/src/material-view.js +7 -6
  14. package/src/memory-view.js +224 -66
  15. package/src/mesh-view.js +6 -5
  16. package/src/model.js +24 -2
  17. package/src/node-model.js +81 -11
  18. package/src/perf-capture.js +2034 -0
  19. package/src/perf-chart.js +3989 -0
  20. package/src/perf-copy.js +367 -0
  21. package/src/perf-view.js +1224 -0
  22. package/src/property-view.js +162 -5
  23. package/src/render-target-view.js +3 -3
  24. package/src/script-view.js +21 -12
  25. package/src/styles.js +374 -2
  26. package/src/texture-view.js +4 -4
  27. package/src/transform-tool.js +185 -0
  28. package/types/asset-view.d.ts +3 -1
  29. package/types/camera-view.d.ts +4 -0
  30. package/types/context-menu.d.ts +90 -0
  31. package/types/copy-json.d.ts +69 -0
  32. package/types/describe.d.ts +1 -1
  33. package/types/entity-inspector-script.d.ts +8 -0
  34. package/types/frame-graph-view.d.ts +7 -0
  35. package/types/hierarchy-view.d.ts +13 -0
  36. package/types/inspector.d.ts +339 -25
  37. package/types/list-view.d.ts +44 -3
  38. package/types/material-view.d.ts +3 -1
  39. package/types/memory-view.d.ts +24 -25
  40. package/types/mesh-view.d.ts +3 -1
  41. package/types/model.d.ts +16 -1
  42. package/types/node-model.d.ts +17 -1
  43. package/types/perf-capture.d.ts +871 -0
  44. package/types/perf-chart.d.ts +1407 -0
  45. package/types/perf-copy.d.ts +61 -0
  46. package/types/perf-view.d.ts +231 -0
  47. package/types/property-view.d.ts +41 -0
  48. package/types/script-view.d.ts +3 -1
  49. package/types/transform-tool.d.ts +92 -0
@@ -0,0 +1,367 @@
1
+ import { EVENT_KINDS, captureNorms, eventLines, eventMask, isHitch, rangeStats } from './perf-view.js';
2
+
3
+ /** @import { PerfCapture, PerfLoad } from './perf-capture.js' */
4
+
5
+ /**
6
+ * The series a chart, or a section under the charts, copies: the figures of each frame, by the
7
+ * name they are copied under and the channel they are read from.
8
+ *
9
+ * @type {Record<string, [string, string][]>}
10
+ */
11
+ const SERIES = {
12
+ frame: [['interval', 'interval']],
13
+ cpu: [
14
+ ['total', 'cpu'], ['update', 'update'], ['systems', 'cpuSystems'], ['scripts', 'scripts'], ['physics', 'physics'],
15
+ ['animation', 'anim'], ['postUpdate', 'cpuPost'], ['otherUpdate', 'cpuOther'], ['render', 'render'],
16
+ ['inspector', 'inspector']
17
+ ],
18
+ gpu: [['gpu', 'gpu']],
19
+ draws: [
20
+ ['total', 'draws'], ['forward', 'drawsForward'], ['shadow', 'drawsShadow'], ['other', 'drawsMisc'],
21
+ ['skinned', 'drawsSkinned'], ['primitives', 'primitives'], ['materialSwitches', 'materialSwitches'],
22
+ ['shaderSwitches', 'shaderSwitches'], ['computeDispatches', 'dispatches'], ['computePasses', 'computePasses']
23
+ ],
24
+ memory: [
25
+ ['jsHeap', 'heap'], ['video', 'vram'], ['textures', 'vramTex'], ['vertexBuffers', 'vramVb'],
26
+ ['indexBuffers', 'vramIb'], ['uniformBuffers', 'vramUb'], ['storageBuffers', 'vramSb']
27
+ ],
28
+ resources: [
29
+ ['textures', 'textures'], ['renderTargets', 'targets'], ['shaders', 'shaders'], ['buffers', 'buffers'],
30
+ ['pipelines', 'pipelines']
31
+ ]
32
+ };
33
+
34
+ // the render phases a profiler or debug engine times, by the name they are copied under
35
+ const RENDER_PHASES = [
36
+ ['batching', 'batching'], ['creatingBatches', 'batchCreate'], ['culling', 'cull'], ['sorting', 'sort'], ['skinning', 'skin'], ['morphing', 'morph'],
37
+ ['shadowMaps', 'shadows'], ['forward', 'forward'], ['lightClusters', 'clusters']
38
+ ];
39
+
40
+ /**
41
+ * Sets the figures of a frame on an object, leaving out those the engine did not provide.
42
+ *
43
+ * @param {object} out - The object.
44
+ * @param {PerfCapture} capture - The capture.
45
+ * @param {number} slot - The slot of the frame.
46
+ * @param {[string, string][]} figures - The names to set and the channels to read.
47
+ * @returns {object} The object.
48
+ */
49
+ function put(out, capture, slot, figures) {
50
+ for (const [name, key] of figures) {
51
+ const v = capture.data[key][slot];
52
+ if (Number.isFinite(v)) out[name] = v;
53
+ }
54
+ return out;
55
+ }
56
+
57
+ /**
58
+ * @param {PerfCapture} capture - The capture.
59
+ * @param {number} seq - A kept frame.
60
+ * @returns {Record<string, number>} Its GPU passes by name, those sharing a name summed, empty
61
+ * until its timings resolved.
62
+ */
63
+ function passesOf(capture, seq) {
64
+ /** @type {Record<string, number>} */
65
+ const passes = {};
66
+ for (const pass of capture.passesInOrder(seq)) passes[pass.name] = (passes[pass.name] ?? 0) + pass.time;
67
+ return passes;
68
+ }
69
+
70
+ /**
71
+ * @param {PerfLoad} load - Something that loaded, or a moment.
72
+ * @param {number} tick - When the entry it is in began, which its times count from.
73
+ * @returns {object} It: what it was, when it began and how long it took, its request and its size.
74
+ * A moment has only when it was.
75
+ */
76
+ function loadJson(load, tick) {
77
+ if (load.kind === 'milestone') return { kind: load.kind, name: load.name, at: load.start - tick };
78
+ /** @type {any} */
79
+ const out = { kind: load.kind, name: load.name };
80
+ if (load.type) out.type = load.type;
81
+ if (load.url && load.url !== load.name) out.url = load.url;
82
+ // an asset that began loading before recording did has no start
83
+ if (Number.isFinite(load.start)) {
84
+ out.start = load.start - tick;
85
+ out.duration = load.end - load.start;
86
+ }
87
+ out.end = load.end - tick;
88
+ if (Number.isFinite(load.netEnd)) {
89
+ out.request = {
90
+ start: load.netStart - tick,
91
+ duration: load.netEnd - load.netStart,
92
+ ...(load.firstByte > 0 ? { firstByte: load.firstByte - load.netStart } : {})
93
+ };
94
+ }
95
+ if (load.bytes > 0) out.bytes = load.bytes;
96
+ if (load.cached) out.cached = true;
97
+ if (load.failed) out.failed = true;
98
+ return out;
99
+ }
100
+
101
+ /**
102
+ * @param {PerfCapture} capture - The capture.
103
+ * @param {number} slot - The slot of a frame.
104
+ * @returns {Record<string, number>|null} The counters the app set in the frame, by name, or null
105
+ * where it set none.
106
+ */
107
+ function countersOf(capture, slot) {
108
+ /** @type {Record<string, number>|null} */
109
+ let counters = null;
110
+ capture.counterNames.forEach((name, id) => {
111
+ const v = capture.counterRings[id][slot];
112
+ if (Number.isFinite(v)) (counters ??= {})[name] = v;
113
+ });
114
+ return counters;
115
+ }
116
+
117
+ /**
118
+ * Everything recorded for a frame. Durations are in milliseconds, memory in bytes; a figure the
119
+ * engine did not provide is left out.
120
+ *
121
+ * The start of the app, when the capture began with it, has how long it lasted, what loaded and
122
+ * the moments in it, with times from the page opening.
123
+ *
124
+ * @param {PerfCapture} capture - The capture.
125
+ * @param {number} seq - A kept frame.
126
+ * @returns {object} The frame.
127
+ */
128
+ function frameJson(capture, seq) {
129
+ const d = capture.data;
130
+ const s = capture.slot(seq);
131
+ const first = capture.slot(capture.first);
132
+ /** @type {any} */
133
+ const frame = { frame: capture.frameNumber(seq), ...(capture.isStartup(seq) ? { startup: true, duration: d.width[s] } : {}) };
134
+ put(frame, capture, s, [['appFrame', 'appFrame']]);
135
+ frame.time = (d.tick[s] - d.tick[first]) / 1000;
136
+ frame.rendered = d.version[s] >= 0;
137
+ put(frame, capture, s, SERIES.frame);
138
+ frame.hitch = isHitch(capture, seq, captureNorms(capture).median);
139
+
140
+ frame.cpu = put({}, capture, s, SERIES.cpu);
141
+ if (capture.detailed) frame.cpu.renderPhases = put({}, capture, s, RENDER_PHASES);
142
+ if (d.splatSort[s] > 0) frame.cpu.splatSortOnWorker = d.splatSort[s];
143
+
144
+ if (Number.isFinite(d.gpu[s])) {
145
+ frame.gpu = {
146
+ frame: d.gpu[s],
147
+ // summed by name, and each apart in the order they ran, with the effect running it
148
+ passes: passesOf(capture, seq),
149
+ passList: capture.passesInOrder(seq).map(pass => ({ name: pass.name, time: pass.time, ...(pass.owner ? { effect: pass.owner } : {}) }))
150
+ };
151
+ }
152
+ frame.draws = put({}, capture, s, SERIES.draws);
153
+ frame.memory = put({}, capture, s, SERIES.memory);
154
+ frame.resources = put({}, capture, s, SERIES.resources);
155
+ if (d.compiles[s] > 0) frame.shaderCompiles = { count: d.compiles[s], time: d.compileTime[s] };
156
+ if (capture.scriptFigures(seq)) frame.scripts = scriptsJson(capture, seq);
157
+
158
+ const events = capture.events(seq);
159
+ if (events.length) {
160
+ frame.events = events.map(event => ({
161
+ kind: event.kind,
162
+ name: event.name,
163
+ start: event.start - d.tick[s],
164
+ duration: event.duration,
165
+ ...(event.detail ? { detail: event.detail } : {}),
166
+ ...(event.bake ? { bake: event.bake } : {})
167
+ }));
168
+ }
169
+ const counters = countersOf(capture, s);
170
+ if (counters) frame.counters = counters;
171
+ const loads = capture.loads(seq);
172
+ if (loads.length) frame.loads = loads.map(load => loadJson(load, d.tick[s]));
173
+ return frame;
174
+ }
175
+
176
+ /**
177
+ * @param {PerfCapture} capture - The capture.
178
+ * @param {number} seq - A kept frame scripts were timed in.
179
+ * @returns {object} The scripts' own time in the frame, in all and in the update, post-update and
180
+ * initializing, their calls, the estimated cost of timing them, and each script type's times.
181
+ */
182
+ function scriptsJson(capture, seq) {
183
+ const s = capture.slot(seq);
184
+ const d = capture.data;
185
+ /** @type {Record<string, object>} */
186
+ const types = {};
187
+ for (const type of capture.scripts(seq)) {
188
+ types[type.name] = {
189
+ update: type.update,
190
+ postUpdate: type.post,
191
+ initialize: type.init,
192
+ calls: type.calls,
193
+ updateCalls: type.updateCalls,
194
+ postUpdateCalls: type.postCalls,
195
+ initializeCalls: type.initCalls
196
+ };
197
+ }
198
+ return {
199
+ total: d.scriptsTotal[s],
200
+ update: d.scriptsUpdate[s],
201
+ postUpdate: d.scriptsPost[s],
202
+ initialize: d.scriptsInit[s],
203
+ calls: d.scriptCalls[s],
204
+ timingCost: d.scriptCost[s],
205
+ types
206
+ };
207
+ }
208
+
209
+ /**
210
+ * @param {PerfCapture} capture - The capture.
211
+ * @param {number} from - The first frame.
212
+ * @param {number} to - The last frame, inclusive.
213
+ * @returns {object} What the frames have in common: how many, and the norms they are judged by.
214
+ */
215
+ function header(capture, from, to) {
216
+ const { budget, median } = captureNorms(capture);
217
+ return {
218
+ range: { from: capture.frameNumber(from), to: capture.frameNumber(to), frames: to - from + 1 },
219
+ medianFrame: median,
220
+ displayInterval: budget,
221
+ engineBuild: capture.detailed ? 'profiler or debug' : 'release',
222
+ gpuTimingsExact: capture.gpuExact,
223
+ ...(Number.isFinite(capture.clockStep) ? { clockStep: capture.clockStep } : {})
224
+ };
225
+ }
226
+
227
+ /**
228
+ * The whole capture, or a span of it: a header and every frame.
229
+ *
230
+ * @param {PerfCapture} capture - The capture.
231
+ * @param {number} from - The first frame.
232
+ * @param {number} to - The last frame, inclusive.
233
+ * @param {object} [device] - What to say about the device.
234
+ * @returns {object} The capture.
235
+ */
236
+ function captureJson(capture, from, to, device) {
237
+ const frames = [];
238
+ for (let seq = from; seq <= to; seq++) frames.push(frameJson(capture, seq));
239
+ return { ...header(capture, from, to), ...(device ? { device } : {}), frames };
240
+ }
241
+
242
+ /**
243
+ * The series of a chart or section, a row per frame: the frame and its figures. The GPU series
244
+ * has a column per pass, the scripts series a column per script type and only the frames that
245
+ * timed scripts, the counters series a column per counter and only the frames that set one, the
246
+ * events series lists only the frames that had events, with what happened by kind, and the
247
+ * loading series has a row per asset, request and moment, in the frame it ended in.
248
+ *
249
+ * @param {PerfCapture} capture - The capture.
250
+ * @param {string} key - The chart or section: 'frame', 'cpu', 'gpu', 'draws', 'scripts',
251
+ * 'counters', 'loading', 'memory', 'resources' or 'events'.
252
+ * @param {number} from - The first frame.
253
+ * @param {number} to - The last frame, inclusive.
254
+ * @returns {object[]} The rows.
255
+ */
256
+ function seriesRows(capture, key, from, to) {
257
+ const rows = [];
258
+ for (let seq = from; seq <= to; seq++) {
259
+ const s = capture.slot(seq);
260
+ if (key === 'events') {
261
+ const mask = eventMask(capture, seq);
262
+ if (!mask) continue;
263
+ /** @type {any} */
264
+ const row = { frame: capture.frameNumber(seq) };
265
+ EVENT_KINDS.forEach((kind, i) => {
266
+ if (mask & (1 << i)) row[kind.key] = eventLines(capture, seq, kind.key);
267
+ });
268
+ rows.push(row);
269
+ continue;
270
+ }
271
+ if (key === 'counters') {
272
+ const counters = countersOf(capture, s);
273
+ if (counters) rows.push({ frame: capture.frameNumber(seq), ...counters });
274
+ continue;
275
+ }
276
+ if (key === 'loading') {
277
+ const tick = capture.data.tick[s];
278
+ for (const load of capture.loads(seq)) {
279
+ const { request, ...rest } = /** @type {any} */ (loadJson(load, tick));
280
+ rows.push({
281
+ frame: capture.frameNumber(seq),
282
+ ...rest,
283
+ ...(request ? { requestStart: request.start, requestDuration: request.duration, firstByte: request.firstByte } : {})
284
+ });
285
+ }
286
+ continue;
287
+ }
288
+ if (key === 'scripts') {
289
+ if (!capture.scriptFigures(seq)) continue;
290
+ /** @type {any} */
291
+ const row = { frame: capture.frameNumber(seq), total: capture.data.scriptsTotal[s] };
292
+ for (const type of capture.scripts(seq)) row[type.name] = type.update + type.post + type.init;
293
+ rows.push(row);
294
+ continue;
295
+ }
296
+ /** @type {any} */
297
+ const row = put({ frame: capture.frameNumber(seq) }, capture, s, SERIES[key]);
298
+ if (key === 'cpu' && capture.detailed) put(row, capture, s, RENDER_PHASES);
299
+ if (key === 'gpu') {
300
+ for (const [name, time] of Object.entries(passesOf(capture, seq))) row[`pass ${name}`] = time;
301
+ }
302
+ rows.push(row);
303
+ }
304
+ return rows;
305
+ }
306
+
307
+ /**
308
+ * @param {*} value - A cell.
309
+ * @returns {string} The cell as CSV: numbers at full precision, lists joined, text quoted when it
310
+ * has to be.
311
+ */
312
+ function csvCell(value) {
313
+ if (value === undefined || value === null) return '';
314
+ const text = Array.isArray(value) ? value.join('; ') : String(value);
315
+ return /[",\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
316
+ }
317
+
318
+ /**
319
+ * @param {object[]} rows - Rows of the same kind, as {@link seriesRows} makes them.
320
+ * @returns {string} The rows as CSV, with a header of every column any row has, in the order
321
+ * they first appear.
322
+ */
323
+ function toCsv(rows) {
324
+ const columns = [];
325
+ const seen = new Set();
326
+ for (const row of rows) {
327
+ for (const column of Object.keys(row)) {
328
+ if (!seen.has(column)) {
329
+ seen.add(column);
330
+ columns.push(column);
331
+ }
332
+ }
333
+ }
334
+ const lines = [columns.map(csvCell).join(',')];
335
+ for (const row of rows) lines.push(columns.map(column => csvCell(row[column])).join(','));
336
+ return `${lines.join('\n')}\n`;
337
+ }
338
+
339
+ /**
340
+ * The figures of frames selected together: their averages, 95th percentiles and largest.
341
+ *
342
+ * @param {PerfCapture} capture - The capture.
343
+ * @param {number} from - The first frame.
344
+ * @param {number} to - The last frame, inclusive.
345
+ * @returns {object} The figures.
346
+ */
347
+ function rangeJson(capture, from, to) {
348
+ const { median } = captureNorms(capture);
349
+ let hitches = 0;
350
+ for (let seq = from; seq <= to; seq++) {
351
+ if (isHitch(capture, seq, median)) hitches++;
352
+ }
353
+ const stats = (key) => {
354
+ const { count, avg, p95, max } = rangeStats(capture, key, from, to);
355
+ return count ? { avg, p95, max } : null;
356
+ };
357
+ const interval = stats('interval');
358
+ /** @type {any} */
359
+ const out = { ...header(capture, from, to), fps: interval ? 1000 / interval.avg : null, hitches };
360
+ for (const [name, key] of [['interval', 'interval'], ['cpu', 'cpu'], ['gpu', 'gpu'], ['inspector', 'inspector'], ['scripts', 'scriptsTotal'], ['draws', 'draws'], ['videoMemory', 'vram'], ['jsHeap', 'heap']]) {
361
+ const figure = stats(key);
362
+ if (figure) out[name] = figure;
363
+ }
364
+ return out;
365
+ }
366
+
367
+ export { SERIES, captureJson, frameJson, rangeJson, seriesRows, toCsv };