@playcanvas/inspector 0.2.0 → 0.3.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/src/list-view.js CHANGED
@@ -36,6 +36,15 @@ const GUTTER_GAP = 2;
36
36
  * it is hovered.
37
37
  * @property {boolean} [dim] - Whether the row is shown dimmed.
38
38
  * @property {string} [title] - A tooltip for the whole row.
39
+ * @property {boolean} [header] - Whether the row heads a group: the rows after it, up to the next
40
+ * header, belong to it and are shown only while it is open. Clicking a header opens or closes it
41
+ * instead of selecting it.
42
+ * @property {boolean} [open] - Whether a header starts out open. Once it is clicked, the list
43
+ * remembers its state across refreshes.
44
+ * @property {boolean} [inert] - Whether the row only shows information: clicking it selects
45
+ * nothing.
46
+ * @property {() => object[]} [contents] - For a header whose group does not list what it counts,
47
+ * the objects a copy of the group holds, such as the textures on the device.
39
48
  */
40
49
 
41
50
  /**
@@ -94,9 +103,10 @@ function guideStyle(guides) {
94
103
  }
95
104
 
96
105
  /**
97
- * A flat, selectable list of rows with cells. Rows are keyed and reconciled in place, so a list
98
- * rebuilt from scratch a couple of times a second keeps its selection and scroll position and only
99
- * touches the text that changed.
106
+ * A flat, selectable list of rows with cells, optionally in groups under headers that open and
107
+ * close them. Rows are keyed and reconciled in place, so a list rebuilt from scratch a couple of
108
+ * times a second keeps its selection, open groups and scroll position and only touches the text
109
+ * that changed.
100
110
  *
101
111
  * @ignore
102
112
  */
@@ -171,6 +181,14 @@ class ListView {
171
181
  */
172
182
  _entries = new Map();
173
183
 
184
+ /**
185
+ * Whether each header the user opened or closed is open, by row key.
186
+ *
187
+ * @type {Map<string, boolean>}
188
+ * @private
189
+ */
190
+ _open = new Map();
191
+
174
192
  /**
175
193
  * @param {HTMLElement} container - The element to render into.
176
194
  * @param {(item: *, key: string|null) => void} onSelect - The selection callback.
@@ -191,13 +209,31 @@ class ListView {
191
209
  return this._rows.length;
192
210
  }
193
211
 
212
+ /**
213
+ * The rows, all of them, whatever the filter shows and whichever groups are open.
214
+ *
215
+ * @type {ListRow[]}
216
+ */
217
+ get rows() {
218
+ return this._rows;
219
+ }
220
+
194
221
  /**
195
222
  * The item of the selected row, or null when nothing is selected or the row is gone.
196
223
  *
197
224
  * @type {*}
198
225
  */
199
226
  get selected() {
200
- return this._rows.find(row => row.key === this.selectedKey)?.item ?? null;
227
+ return this.selectedRow?.item ?? null;
228
+ }
229
+
230
+ /**
231
+ * The selected row, or null when nothing is selected or the row is gone.
232
+ *
233
+ * @type {ListRow|null}
234
+ */
235
+ get selectedRow() {
236
+ return this._rows.find(row => row.key === this.selectedKey) ?? null;
201
237
  }
202
238
 
203
239
  /**
@@ -230,32 +266,60 @@ class ListView {
230
266
  * @returns {boolean} Whether a row was found.
231
267
  */
232
268
  selectItem(item) {
233
- const row = this._rows.find(r => r.item === item);
234
- if (!row) return false;
235
- this.select(row.key);
269
+ const index = this._rows.findIndex(r => !r.header && r.item === item);
270
+ if (index < 0) return false;
271
+ // open the group the row is in, or it would be selected out of sight
272
+ for (let i = index - 1; i >= 0; i--) {
273
+ if (this._rows[i].header) {
274
+ this._open.set(this._rows[i].key, true);
275
+ break;
276
+ }
277
+ }
278
+ this.select(this._rows[index].key);
236
279
  return true;
237
280
  }
238
281
 
282
+ /**
283
+ * @param {Node} node - An element, such as the target of a context menu event.
284
+ * @returns {ListRow|null} The row it is in, a header included, or null when it is in none.
285
+ */
286
+ rowAt(node) {
287
+ for (const entry of this._entries.values()) {
288
+ if (entry.el.contains(node)) return entry.row;
289
+ }
290
+ return null;
291
+ }
292
+
293
+ /**
294
+ * @param {ListRow} row - A header row.
295
+ * @returns {boolean} Whether its group is open.
296
+ * @private
297
+ */
298
+ _isOpen(row) {
299
+ return this._open.get(row.key) ?? !!row.open;
300
+ }
301
+
239
302
  /**
240
303
  * Synchronizes the DOM with the rows.
241
304
  */
242
305
  render() {
243
306
  const filter = this.filter.trim().toLowerCase();
307
+ const shown = this._shownRows(filter);
244
308
  const seen = new Set();
245
309
  let cursor = this.container.firstChild;
246
310
  let visible = 0;
247
311
 
248
- for (const row of this._rows) {
312
+ for (let i = 0; i < this._rows.length; i++) {
313
+ const row = this._rows[i];
249
314
  const entry = this._entries.get(row.key) ?? this._createEntry(row);
250
315
  entry.row = row;
251
316
  seen.add(row.key);
252
317
 
253
318
  this._syncCells(entry, row.cells);
254
319
 
255
- const shown = !filter || (row.matches ? row.matches(filter) : row.name.toLowerCase().includes(filter));
256
- const display = shown ? '' : 'none';
320
+ const display = shown[i] ? '' : 'none';
257
321
  if (entry.el.style.display !== display) entry.el.style.display = display;
258
- if (shown) visible++;
322
+ if (shown[i] && !row.header) visible++;
259
323
 
260
324
  const indent = row.indent ?? 0;
261
325
  const guides = row.guides ? `${row.guides.lanes}|${row.guides.segments.join(',')}|${row.guides.tick}` : '';
@@ -275,6 +339,12 @@ class ListView {
275
339
 
276
340
  entry.el.classList.toggle('pci-selected', row.key === this.selectedKey);
277
341
  entry.el.classList.toggle('pci-dim', !!row.dim);
342
+ entry.el.classList.toggle('pci-lrow-header', !!row.header);
343
+ entry.el.classList.toggle('pci-open', !!row.header && this._isOpen(row));
344
+ // a header with nothing under it has nothing to open
345
+ const next = this._rows[i + 1];
346
+ entry.el.classList.toggle('pci-leaf', !!row.header && (!next || !!next.header));
347
+ entry.el.classList.toggle('pci-inert', !!row.inert);
278
348
 
279
349
  if (entry.el !== cursor) {
280
350
  this.container.insertBefore(entry.el, cursor);
@@ -293,6 +363,34 @@ class ListView {
293
363
  this.visibleCount = visible;
294
364
  }
295
365
 
366
+ /**
367
+ * Decides which rows are shown. Without a filter, the rows of a closed group are hidden. With
368
+ * one, every matching row is shown whether its group is open or not, and a header is shown when
369
+ * any row of its group matches.
370
+ *
371
+ * @param {string} filter - The lower-cased filter, or an empty string for none.
372
+ * @returns {boolean[]} Whether each row is shown.
373
+ * @private
374
+ */
375
+ _shownRows(filter) {
376
+ const shown = [];
377
+ let header = -1;
378
+ for (let i = 0; i < this._rows.length; i++) {
379
+ const row = this._rows[i];
380
+ if (row.header) {
381
+ header = i;
382
+ shown.push(!filter);
383
+ } else if (filter) {
384
+ const match = row.matches ? row.matches(filter) : row.name.toLowerCase().includes(filter);
385
+ if (match && header >= 0) shown[header] = true;
386
+ shown.push(match);
387
+ } else {
388
+ shown.push(header < 0 || this._isOpen(this._rows[header]));
389
+ }
390
+ }
391
+ return shown;
392
+ }
393
+
296
394
  /**
297
395
  * @param {ListEntry} entry - The row entry.
298
396
  * @param {ListCell[]} cells - The cells to show.
@@ -383,9 +481,17 @@ class ListView {
383
481
  _createEntry(row) {
384
482
  const rowEl = document.createElement('div');
385
483
  rowEl.className = 'pci-lrow';
386
- rowEl.addEventListener('click', () => this.select(row.key));
387
484
 
388
485
  const entry = { el: rowEl, cells: [], row, indent: -1, guides: '' };
486
+ rowEl.addEventListener('click', () => {
487
+ const current = entry.row;
488
+ if (current.header) {
489
+ this._open.set(current.key, !this._isOpen(current));
490
+ this.render();
491
+ } else if (!current.inert) {
492
+ this.select(current.key);
493
+ }
494
+ });
389
495
  rowEl.addEventListener('pointerenter', () => this._hoverRow(entry));
390
496
  rowEl.addEventListener('pointerleave', () => this.onHover?.(null, null));
391
497
  this._entries.set(row.key, entry);
@@ -3,7 +3,7 @@ import { Material } from 'playcanvas';
3
3
  import { describeValue } from './describe.js';
4
4
  import { collectMeshInstances } from './instance-survey.js';
5
5
  import { nodeLabel } from './memory-view.js';
6
- import { makeSection, push } from './model.js';
6
+ import { makeSection, makeUsageSection, push } from './model.js';
7
7
  import { materialRows, materialTextures, meshInstanceRows } from './node-model.js';
8
8
 
9
9
  /** @import { AppBase } from 'playcanvas' */
@@ -114,12 +114,11 @@ function materialListRows(survey) {
114
114
  * properties, and what uses it, with each mesh instance opening in place.
115
115
  *
116
116
  * @param {Material} material - The material.
117
- * @param {{ app: AppBase }} ctx - The app, to find the assets and debug lines using the material.
117
+ * @param {{ app: AppBase, backlinks?: boolean }} ctx - The app, to find the assets and debug lines
118
+ * using the material, and whether to find them at all, which a copy has no use for.
118
119
  * @returns {PropertySection[]} The sections.
119
120
  */
120
121
  function buildMaterialModel(material, ctx) {
121
- const sources = surveyMaterials(ctx.app).get(material) ?? { assets: [], lines: [] };
122
-
123
122
  const general = makeSection('material', `${typeOf(material)} "${materialName(material)}"`);
124
123
  push(general, 'id', describeValue(material.id));
125
124
  const textures = [...materialTextures(material)];
@@ -129,8 +128,10 @@ function buildMaterialModel(material, ctx) {
129
128
  items: textures.map((texture, index) => ({ label: `[${index}]`, ...describeValue(texture) }))
130
129
  });
131
130
  general.rows.push(...materialRows(material));
131
+ if (ctx.backlinks === false) return [general];
132
132
 
133
- const usage = makeSection('users', 'Used by');
133
+ const sources = surveyMaterials(ctx.app).get(material) ?? { assets: [], lines: [] };
134
+ const usage = makeUsageSection('users', 'Used by');
134
135
  const instances = [...material.meshInstances];
135
136
  push(usage, 'mesh instances', {
136
137
  text: instances.length ? `${instances.length} mesh instance${instances.length === 1 ? '' : 's'}` : 'none',
@@ -182,7 +183,7 @@ function materialsUsingShader(app, shader) {
182
183
  * @returns {PropertySection} A section listing them, each linking to the Materials tab.
183
184
  */
184
185
  function usedByMaterialsSection(materials) {
185
- const section = makeSection('materials', 'Used by materials');
186
+ const section = makeUsageSection('materials', 'Used by materials');
186
187
  push(section, 'materials', {
187
188
  text: materials.length ? `${materials.length} material${materials.length === 1 ? '' : 's'}` : 'none found',
188
189
  cls: materials.length ? 'obj' : 'null',
@@ -4,9 +4,10 @@ import {
4
4
 
5
5
  import { describeValue, isUniformBuffer } from './describe.js';
6
6
  import { collectMeshInstances } from './instance-survey.js';
7
- import { formatBytes, makeSection, push, read, reflectRows } from './model.js';
7
+ import { formatBytes, makeSection, makeUsageSection, push, read, reflectRows } from './model.js';
8
8
  import { vertexFormatValue } from './node-model.js';
9
9
  import { formatUniformBuffer } from './shader-view.js';
10
+ import { collectTextures } from './texture-view.js';
10
11
 
11
12
  /** @import { AppBase } from 'playcanvas' */
12
13
  /** @import { UniformBuffer } from 'playcanvas' */
@@ -40,13 +41,22 @@ const SKIP_BUFFER = [
40
41
  'id', 'allocation', 'vaoKeyPart'
41
42
  ];
42
43
 
43
- /** What the list can be narrowed to, as selector options. */
44
- const BUFFER_KINDS = [
45
- ['all', 'all'], ['vertex', 'vertex'], ['index', 'index'], ['uniform', 'uniform'], ['storage', 'storage']
44
+ /** The groups the buffers are listed in, in order. */
45
+ const BUFFER_GROUPS = [
46
+ ['vertex', 'Vertex buffers'], ['index', 'Index buffers'], ['uniform', 'Uniform buffers'], ['storage', 'Storage buffers']
46
47
  ];
47
48
 
48
49
  const KIND_NAMES = { vertex: 'VertexBuffer', index: 'IndexBuffer', uniform: 'UniformBuffer', storage: 'StorageBuffer' };
49
50
 
51
+ // what a buffer of each kind usually holds for its owner, which its row leaves unsaid
52
+ const PLAIN_ROLES = { vertex: 'vertices', index: 'indices' };
53
+
54
+ // the device caches that grow with the variety of what is drawn, by their resource count key
55
+ const CACHE_NAMES = [
56
+ ['renderPipelines', 'render pipelines'], ['computePipelines', 'compute pipelines'], ['bindGroups', 'bind groups'],
57
+ ['bindGroupFormats', 'bind group formats'], ['drawCommands', 'draw commands'], ['shaders', 'shaders']
58
+ ];
59
+
50
60
  const USAGE_NAMES = {
51
61
  [BUFFER_STATIC]: 'BUFFER_STATIC',
52
62
  [BUFFER_DYNAMIC]: 'BUFFER_DYNAMIC',
@@ -103,21 +113,33 @@ function bufferBytes(buffer) {
103
113
  return b.numBytes ?? b.byteSize ?? b.format?.byteSize ?? 0;
104
114
  }
105
115
 
116
+ /**
117
+ * @param {number} n - A count.
118
+ * @param {string} one - The word for one.
119
+ * @param {string} [many] - The word for any other count, the singular with an s by default.
120
+ * @returns {string} The count with its word.
121
+ */
122
+ function plural(n, one, many = `${one}s`) {
123
+ return `${n} ${n === 1 ? one : many}`;
124
+ }
125
+
106
126
  /**
107
127
  * @param {Buffer} buffer - A buffer.
108
- * @returns {string} What it holds, in a few words.
128
+ * @returns {string} What it holds, in a few words, or an empty string for a storage buffer, whose
129
+ * contents only its user knows.
109
130
  */
110
131
  function contentText(buffer) {
111
132
  const b = /** @type {any} */ (buffer);
112
133
  switch (bufferKind(buffer)) {
113
134
  case 'vertex':
114
- return `${b.numVertices} vertices, ${b.format?.elements?.length ?? 0} elements${b.format?.instancing ? ', instancing' : ''}`;
135
+ return `${plural(b.numVertices, 'vertex', 'vertices')}, ${plural(b.format?.elements?.length ?? 0, 'element')}` +
136
+ `${b.format?.instancing ? ', instancing' : ''}`;
115
137
  case 'index':
116
- return `${b.numIndices} indices, ${INDEX_FORMAT_NAMES[b.format] ?? b.format}`;
138
+ return `${plural(b.numIndices, 'index', 'indices')}, ${INDEX_FORMAT_NAMES[b.format] ?? b.format}`;
117
139
  case 'uniform':
118
- return `${b.format?.uniforms?.length ?? 0} uniforms`;
140
+ return plural(b.format?.uniforms?.length ?? 0, 'uniform');
119
141
  default:
120
- return 'storage';
142
+ return '';
121
143
  }
122
144
  }
123
145
 
@@ -206,10 +228,10 @@ function bufferOwners(app) {
206
228
 
207
229
  /**
208
230
  * Every buffer the device tracks, largest first. Uniform buffers appear only when persistent, as
209
- * the rest are slices of the per-frame pool.
231
+ * the rest are slices of the per-draw pool.
210
232
  *
211
233
  * @param {GraphicsDevice} device - The device.
212
- * @param {string} [kind] - One of {@link BUFFER_KINDS}.
234
+ * @param {'all'|'vertex'|'index'|'uniform'|'storage'} [kind] - The kind to collect.
213
235
  * @returns {Buffer[]} The buffers.
214
236
  */
215
237
  function collectBuffers(device, kind = 'all') {
@@ -218,72 +240,200 @@ function collectBuffers(device, kind = 'all') {
218
240
  }
219
241
 
220
242
  /**
221
- * @param {Buffer} buffer - A buffer.
222
- * @param {BufferOwner[]|undefined} owners - What it belongs to.
243
+ * @param {BufferOwner[]|undefined} owners - What a buffer belongs to.
223
244
  * @returns {string} A name for it, from its first owner, since buffers carry none of their own.
224
245
  */
225
- function bufferName(buffer, owners) {
226
- if (!owners?.length) return `${KIND_NAMES[bufferKind(buffer)]} #${idOf(buffer)}`;
227
- return owners[0].label;
246
+ function bufferName(owners) {
247
+ return owners?.length ? owners[0].label : '(owner not found)';
228
248
  }
229
249
 
230
250
  /**
231
- * One row per buffer, largest first, named after what uses it and dimmed when nothing was found.
232
- *
233
251
  * @param {GraphicsDevice} device - The device.
234
- * @param {Map<Buffer, BufferOwner[]>} owners - The owners of each buffer.
235
- * @param {string} kind - The kind to list, or 'all'.
236
- * @returns {ListRow[]} The rows.
252
+ * @returns {number} The video memory the device counts for its textures and buffers.
237
253
  */
238
- function bufferRows(device, owners, kind) {
239
- return collectBuffers(device, kind).map((buffer) => {
240
- const users = owners.get(buffer);
241
- const name = bufferName(buffer, users);
242
- const cells = [{ text: name, cls: 'pci-cell-name' }];
243
- cells.push({ text: bufferKind(buffer), cls: 'pci-cell-tag pci-cell-tag-info' });
244
- const extra = users && users.length > 1 ? `, ${users.length} users` : '';
245
- cells.push({ text: `${contentText(buffer)}${users ? ` · ${users[0].role}` : ''}${extra}`, cls: 'pci-cell-info' });
246
- cells.push({ text: formatBytes(bufferBytes(buffer)), cls: 'pci-cell-info pci-cell-right' });
247
- const title = `${KIND_NAMES[bufferKind(buffer)]} #${idOf(buffer)}\n${contentText(buffer)}, ${formatBytes(bufferBytes(buffer))}` +
248
- `${users ? `\n${users[0].role} of ${users[0].label}${extra}` : '\nowner not found'}`;
249
- return { key: `buffer${idOf(buffer)}`, item: buffer, name, dim: !users, title, cells };
250
- });
254
+ function videoMemory(device) {
255
+ const vram = /** @type {any} */ (device)._vram ?? {};
256
+ return (vram.tex ?? 0) + (vram.vb ?? 0) + (vram.ib ?? 0) + (vram.ub ?? 0) + (vram.sb ?? 0);
257
+ }
258
+
259
+ /**
260
+ * @param {string} key - The group's key.
261
+ * @param {string} title - The group's name.
262
+ * @param {string} count - How many it holds, or an empty string.
263
+ * @param {string} size - Their total size, or an empty string.
264
+ * @param {string} tip - What the group holds.
265
+ * @returns {ListRow} The row heading a group of the memory list.
266
+ */
267
+ function headerRow(key, title, count, size, tip) {
268
+ return {
269
+ key: `group:${key}`,
270
+ item: null,
271
+ name: title,
272
+ header: true,
273
+ title: tip,
274
+ cells: [{ text: title, cls: 'pci-cell-name' }, { text: count, cls: 'pci-cell-count' }, { text: size, cls: 'pci-cell-size' }]
275
+ };
251
276
  }
252
277
 
253
278
  /**
254
- * The totals shown above the list: video memory by kind of resource, the per-frame uniform pool,
255
- * and the counts of the device caches that grow with the variety of what is drawn.
279
+ * @param {string} key - A key unique within the list.
280
+ * @param {string} name - What the row counts or measures.
281
+ * @param {object} values - What it shows.
282
+ * @param {string} [values.info] - A few words about it.
283
+ * @param {string} [values.count] - A count, in the column of the group counts.
284
+ * @param {string} [values.size] - A size, in the column of the group sizes.
285
+ * @param {string} [values.tip] - A tooltip.
286
+ * @returns {ListRow} A row of a group that shows figures and has nothing to select.
287
+ */
288
+ function infoRow(key, name, { info = '', count = '', size = '', tip }) {
289
+ return {
290
+ key,
291
+ item: null,
292
+ name,
293
+ indent: 1,
294
+ inert: true,
295
+ title: tip,
296
+ cells: [
297
+ { text: name, cls: 'pci-cell-name' }, { text: info, cls: 'pci-cell-info' },
298
+ { text: count, cls: 'pci-cell-count' }, { text: size, cls: 'pci-cell-size' }
299
+ ]
300
+ };
301
+ }
302
+
303
+ /**
304
+ * @param {Buffer} buffer - A buffer.
305
+ * @param {BufferOwner[]|undefined} users - What it belongs to.
306
+ * @returns {ListRow} Its row, named after what uses it and dimmed when nothing was found.
307
+ */
308
+ function bufferRow(buffer, users) {
309
+ const kind = bufferKind(buffer);
310
+ const name = bufferName(users);
311
+ const content = contentText(buffer);
312
+ const notes = content ? [content] : [];
313
+ const role = users?.[0].role;
314
+ if (role && role !== PLAIN_ROLES[kind]) notes.push(role);
315
+ if (users && users.length > 1) notes.push(`${users.length} users`);
316
+ const size = formatBytes(bufferBytes(buffer));
317
+ const title = [KIND_NAMES[kind], content ? `${content}, ${size}` : size,
318
+ users ? `${users[0].role} of ${users[0].label}${users.length > 1 ? `, ${users.length} users` : ''}` : 'owner not found'].join('\n');
319
+ return {
320
+ key: `buffer${idOf(buffer)}`,
321
+ item: buffer,
322
+ name,
323
+ indent: 1,
324
+ dim: !users,
325
+ title,
326
+ cells: [
327
+ { text: name, cls: 'pci-cell-name' }, { text: notes.join(' · '), cls: 'pci-cell-info' },
328
+ { text: size, cls: 'pci-cell-info pci-cell-right' }
329
+ ]
330
+ };
331
+ }
332
+
333
+ /**
334
+ * The textures on the device, in total, and split by what they are for where the engine tracks
335
+ * that, which only profiler builds do. They are listed one by one on the Textures tab.
256
336
  *
257
337
  * @param {GraphicsDevice} device - The device.
258
- * @returns {string} The totals, one line per group.
338
+ * @returns {ListRow[]} The rows of the group.
259
339
  */
260
- function memorySummary(device) {
340
+ function textureGroup(device) {
261
341
  const vram = /** @type {any} */ (device)._vram ?? {};
262
- const lines = [];
263
- lines.push(`VRAM: textures ${formatBytes(vram.tex ?? 0)} · vertex ${formatBytes(vram.vb ?? 0)} · index ${formatBytes(vram.ib ?? 0)}` +
264
- ` · uniform ${formatBytes(vram.ub ?? 0)} · storage ${formatBytes(vram.sb ?? 0)}`);
265
- // the texture split is only tracked by profiler builds
266
- if (vram.texShadow || vram.texAsset || vram.texLightmap) {
267
- lines.push(`textures: assets ${formatBytes(vram.texAsset ?? 0)} · shadow maps ${formatBytes(vram.texShadow ?? 0)} · lightmaps ${formatBytes(vram.texLightmap ?? 0)}`);
342
+ const total = vram.tex ?? 0;
343
+ const header = headerRow('textures', 'Textures', String(device.textures?.size ?? 0), formatBytes(total),
344
+ 'Every texture on the device, including the render target attachments. The Textures tab lists them');
345
+ // listed on their own tab, but a copy of the group holds them
346
+ header.contents = () => collectTextures(device);
347
+ const rows = [header];
348
+ const split = [['assets', vram.texAsset], ['shadow maps', vram.texShadow], ['lightmaps', vram.texLightmap]];
349
+ if (split.some(([, bytes]) => bytes)) {
350
+ for (const [name, bytes] of split) {
351
+ rows.push(infoRow(`textures:${name}`, name, { size: formatBytes(bytes ?? 0) }));
352
+ }
353
+ const rest = total - split.reduce((sum, [, bytes]) => sum + (bytes ?? 0), 0);
354
+ rows.push(infoRow('textures:other', 'other', { info: 'render targets and textures made at runtime', size: formatBytes(Math.max(0, rest)) }));
268
355
  }
356
+ return rows;
357
+ }
269
358
 
270
- // WebGPU carves draws out of large fixed blocks; WebGL2 keeps whole buffers of each size
271
- // requested, so only the count says anything there
359
+ /**
360
+ * The persistent uniform buffers, and the pool that the uniforms set for each draw are written
361
+ * into every frame. The pool has no buffer object to list, so it is counted from the device: its
362
+ * size is what the device counts for uniform buffers beyond the persistent ones.
363
+ *
364
+ * @param {GraphicsDevice} device - The device.
365
+ * @param {Buffer[]} buffers - The persistent uniform buffers, largest first.
366
+ * @param {Map<Buffer, BufferOwner[]>} owners - The owners of each buffer.
367
+ * @returns {ListRow[]} The rows of the group.
368
+ */
369
+ function uniformGroup(device, buffers, owners) {
370
+ const vram = /** @type {any} */ (device)._vram ?? {};
371
+ const persistent = buffers.reduce((sum, buffer) => sum + bufferBytes(buffer), 0);
372
+ const rows = buffers.map(buffer => bufferRow(buffer, owners.get(buffer)));
373
+
374
+ let count = buffers.length;
375
+ let bytes = persistent;
272
376
  const pool = /** @type {any} */ (device).dynamicBuffers;
273
377
  if (pool?.bufferCount) {
274
- lines.push(pool.bufferSize ?
275
- `per-draw uniform pool: ${pool.bufferCount} × ${formatBytes(pool.bufferSize)}` :
276
- `per-draw uniform pool: ${pool.bufferCount} buffers`);
378
+ const poolBytes = Math.max(0, (vram.ub ?? 0) - persistent);
379
+ count += pool.bufferCount;
380
+ bytes += poolBytes;
381
+ // WebGPU carves the draws out of large fixed blocks, each written through a staging copy;
382
+ // WebGL2 keeps whole buffers of each size requested, so only their count says anything there
383
+ let info = plural(pool.bufferCount, 'buffer');
384
+ if (pool.bufferSize) {
385
+ const staging = pool.stagingBuffers?.length;
386
+ info = `${info}${staging ? ` + ${staging} staging` : ''}, ${formatBytes(pool.bufferSize)} each`;
387
+ }
388
+ rows.unshift(infoRow('uniform:pool', 'per-draw uniform pool', {
389
+ info,
390
+ size: formatBytes(poolBytes),
391
+ tip: 'The buffers the uniforms set for each draw are written into, every frame'
392
+ }));
277
393
  }
394
+ if (!count) return [];
395
+ return [headerRow('uniform', 'Uniform buffers', String(count), formatBytes(bytes),
396
+ 'The persistent uniform buffers, such as those of the materials, and the per-draw pool'), ...rows];
397
+ }
278
398
 
399
+ /**
400
+ * @param {GraphicsDevice} device - The device.
401
+ * @returns {ListRow[]} The rows of the group counting the device caches, which grow with the
402
+ * variety of what is drawn, or none when the device reports none.
403
+ */
404
+ function cacheGroup(device) {
279
405
  const counts = new Map();
280
406
  /** @type {any} */ (device).getResourceCounts?.(counts);
281
- const names = [['renderPipelines', 'render pipelines'], ['computePipelines', 'compute pipelines'], ['bindGroups', 'bind groups'],
282
- ['bindGroupFormats', 'bind group formats'], ['drawCommands', 'draw commands'], ['shaders', 'shaders']];
283
- const parts = names.filter(([key]) => counts.has(key)).map(([key, label]) => `${label} ${counts.get(key)}`);
284
- if (parts.length) lines.push(parts.join(' · '));
407
+ const rows = CACHE_NAMES.filter(([key]) => counts.has(key))
408
+ .map(([key, name]) => infoRow(`caches:${key}`, name, { count: String(counts.get(key)) }));
409
+ if (!rows.length) return [];
410
+ return [headerRow('caches', 'Device caches', '', '', 'The pipelines, bind groups and shaders the device has created and keeps for reuse'), ...rows];
411
+ }
285
412
 
286
- return lines.join('\n');
413
+ /**
414
+ * The rows of the memory list: one group per kind of resource, headed by its count and total size
415
+ * and closed until opened, holding its buffers largest first, named after what uses each. The
416
+ * device caches come last. Kinds with nothing on the device are left out.
417
+ *
418
+ * @param {GraphicsDevice} device - The device.
419
+ * @param {Map<Buffer, BufferOwner[]>} owners - The owners of each buffer.
420
+ * @returns {ListRow[]} The rows.
421
+ */
422
+ function memoryRows(device, owners) {
423
+ const rows = textureGroup(device);
424
+ const buffers = collectBuffers(device);
425
+ for (const [kind, title] of BUFFER_GROUPS) {
426
+ const members = buffers.filter(buffer => bufferKind(buffer) === kind);
427
+ if (kind === 'uniform') {
428
+ rows.push(...uniformGroup(device, members, owners));
429
+ } else if (members.length) {
430
+ const bytes = members.reduce((sum, buffer) => sum + bufferBytes(buffer), 0);
431
+ rows.push(headerRow(kind, title, String(members.length), formatBytes(bytes), `The ${title.toLowerCase()} on the device`));
432
+ for (const buffer of members) rows.push(bufferRow(buffer, owners.get(buffer)));
433
+ }
434
+ }
435
+ rows.push(...cacheGroup(device));
436
+ return rows;
287
437
  }
288
438
 
289
439
  /**
@@ -291,7 +441,8 @@ function memorySummary(device) {
291
441
  * belong to (linked), and every other public property.
292
442
  *
293
443
  * @param {Buffer} buffer - The buffer.
294
- * @param {{ app: AppBase }} ctx - The app, to find the buffer's owners.
444
+ * @param {{ app: AppBase, backlinks?: boolean }} ctx - The app, to find the buffer's owners, and
445
+ * whether to find them at all, which a copy has no use for.
295
446
  * @returns {PropertySection[]} The sections.
296
447
  */
297
448
  function buildBufferModel(buffer, ctx) {
@@ -316,9 +467,23 @@ function buildBufferModel(buffer, ctx) {
316
467
  }
317
468
  if (b.usage !== undefined) push(general, 'usage', { text: USAGE_NAMES[b.usage] ?? String(b.usage), cls: 'obj' });
318
469
  sections.push(general);
470
+ if (ctx.backlinks !== false) sections.push(ownersSection(buffer, ctx.app));
471
+
472
+ const rest = makeSection('props', 'Properties');
473
+ reflectRows(rest, buffer, [], SKIP_BUFFER);
474
+ if (rest.rows.length) sections.push(rest);
319
475
 
320
- const owners = bufferOwners(ctx.app).get(buffer) ?? [];
321
- const usage = makeSection('users', 'Used by');
476
+ return sections;
477
+ }
478
+
479
+ /**
480
+ * @param {Buffer} buffer - The buffer.
481
+ * @param {AppBase} app - The app, to find the buffer's owners.
482
+ * @returns {PropertySection} The section listing what the buffer was found to belong to.
483
+ */
484
+ function ownersSection(buffer, app) {
485
+ const owners = bufferOwners(app).get(buffer) ?? [];
486
+ const usage = makeUsageSection('users', 'Used by');
322
487
  push(usage, 'owners', {
323
488
  text: owners.length ? `${owners.length} user${owners.length === 1 ? '' : 's'}` :
324
489
  'not found: held by something the panel cannot reach, such as a storage buffer',
@@ -329,16 +494,9 @@ function buildBufferModel(buffer, ctx) {
329
494
  }))
330
495
  });
331
496
  if (owners.length > 50) usage.rows.push({ key: 'more', label: '', value: { text: `… ${owners.length - 50} more`, cls: 'null' }, depth: 1 });
332
- sections.push(usage);
333
-
334
- const rest = makeSection('props', 'Properties');
335
- reflectRows(rest, buffer, [], SKIP_BUFFER);
336
- if (rest.rows.length) sections.push(rest);
337
-
338
- return sections;
497
+ return usage;
339
498
  }
340
499
 
341
500
  export {
342
- BUFFER_KINDS, bufferBytes, bufferKind, bufferOwners, bufferRows, buildBufferModel, collectBuffers, idOf,
343
- memorySummary, nodeLabel
501
+ bufferKind, bufferOwners, buildBufferModel, collectBuffers, idOf, KIND_NAMES, memoryRows, nodeLabel, videoMemory
344
502
  };