@playcanvas/inspector 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.
Files changed (77) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +110 -0
  3. package/package.json +62 -0
  4. package/src/asset-usage.js +108 -0
  5. package/src/asset-view.js +370 -0
  6. package/src/camera-view.js +128 -0
  7. package/src/debug.js +31 -0
  8. package/src/describe.js +302 -0
  9. package/src/entity-inspector-script.js +130 -0
  10. package/src/frame-graph-view.js +767 -0
  11. package/src/hierarchy-view.js +475 -0
  12. package/src/index.js +4 -0
  13. package/src/input/fly-controller.js +116 -0
  14. package/src/input/input.js +299 -0
  15. package/src/input/math.js +8 -0
  16. package/src/input/orbit-controller.js +158 -0
  17. package/src/input/pose.js +207 -0
  18. package/src/inspector.js +3293 -0
  19. package/src/instance-survey.js +50 -0
  20. package/src/list-view.js +410 -0
  21. package/src/material-view.js +197 -0
  22. package/src/memory-view.js +344 -0
  23. package/src/mesh-view.js +235 -0
  24. package/src/model.js +170 -0
  25. package/src/node-model.js +404 -0
  26. package/src/physics-debug.js +527 -0
  27. package/src/physics-view.js +314 -0
  28. package/src/picker/picker.js +545 -0
  29. package/src/picker/render-pass-picker.js +206 -0
  30. package/src/property-view.js +628 -0
  31. package/src/render-target-view.js +288 -0
  32. package/src/renderers/texture-renderer-shaders.js +139 -0
  33. package/src/renderers/texture-renderer.js +430 -0
  34. package/src/renderers/wire-renderer.js +954 -0
  35. package/src/script-view.js +288 -0
  36. package/src/shader-view.js +287 -0
  37. package/src/styles.js +918 -0
  38. package/src/texture-view.js +186 -0
  39. package/src/tooltip.js +72 -0
  40. package/src/viewport-tools.js +695 -0
  41. package/types/asset-usage.d.ts +23 -0
  42. package/types/asset-view.d.ts +94 -0
  43. package/types/camera-view.d.ts +45 -0
  44. package/types/debug.d.ts +14 -0
  45. package/types/describe.d.ts +43 -0
  46. package/types/entity-inspector-script.d.ts +87 -0
  47. package/types/frame-graph-view.d.ts +155 -0
  48. package/types/hierarchy-view.d.ts +172 -0
  49. package/types/index.d.ts +3 -0
  50. package/types/input/fly-controller.d.ts +42 -0
  51. package/types/input/input.d.ts +192 -0
  52. package/types/input/math.d.ts +1 -0
  53. package/types/input/orbit-controller.d.ts +63 -0
  54. package/types/input/pose.d.ts +126 -0
  55. package/types/inspector.d.ts +1304 -0
  56. package/types/instance-survey.d.ts +35 -0
  57. package/types/list-view.d.ts +151 -0
  58. package/types/material-view.d.ts +63 -0
  59. package/types/memory-view.d.ts +95 -0
  60. package/types/mesh-view.d.ts +89 -0
  61. package/types/model.d.ts +104 -0
  62. package/types/node-model.d.ts +54 -0
  63. package/types/physics-debug.d.ts +254 -0
  64. package/types/physics-view.d.ts +81 -0
  65. package/types/picker/picker.d.ts +255 -0
  66. package/types/picker/render-pass-picker.d.ts +47 -0
  67. package/types/property-view.d.ts +189 -0
  68. package/types/render-target-view.d.ts +98 -0
  69. package/types/renderers/texture-renderer-shaders.d.ts +9 -0
  70. package/types/renderers/texture-renderer.d.ts +231 -0
  71. package/types/renderers/wire-renderer.d.ts +347 -0
  72. package/types/script-view.d.ts +56 -0
  73. package/types/shader-view.d.ts +46 -0
  74. package/types/styles.d.ts +22 -0
  75. package/types/texture-view.d.ts +30 -0
  76. package/types/tooltip.d.ts +17 -0
  77. package/types/viewport-tools.d.ts +237 -0
@@ -0,0 +1,288 @@
1
+ import { Script, ScriptType } from 'playcanvas';
2
+
3
+ import { describeValue } from './describe.js';
4
+ import { nodeLabel } from './memory-view.js';
5
+ import { makeSection, push } from './model.js';
6
+ import { scriptInstanceRows } from './node-model.js';
7
+
8
+ /** @import { AppBase } from 'playcanvas' */
9
+ /** @import { Asset } from 'playcanvas' */
10
+ /** @import { Entity } from 'playcanvas' */
11
+ /** @import { ListRow } from './list-view.js' */
12
+ /** @import { PropertySection } from './model.js' */
13
+
14
+ /**
15
+ * A script class found, with where it was found and the instances of it.
16
+ *
17
+ * @typedef {object} ScriptEntry
18
+ * @property {string} name - The name the script is known by.
19
+ * @property {boolean} registered - Whether the app's script registry holds it.
20
+ * @property {{ script: Script|ScriptType, entity: Entity }[]} instances - Its instances, with the
21
+ * entities they are on.
22
+ * @ignore
23
+ */
24
+
25
+ // most instances a script lists in the property view
26
+ const MAX_INSTANCES = 100;
27
+
28
+ // the lifecycle methods a script can define, in the order the engine calls them
29
+ const LIFECYCLE = ['initialize', 'postInitialize', 'update', 'postUpdate', 'swap'];
30
+
31
+ /**
32
+ * @param {Function} cls - A class.
33
+ * @returns {boolean} Whether it is a script class, of either kind.
34
+ */
35
+ function isScriptClass(cls) {
36
+ return typeof cls === 'function' && cls.prototype instanceof Script;
37
+ }
38
+
39
+ /**
40
+ * @param {Function} cls - A script class.
41
+ * @returns {boolean} Whether it is a classic script, declared with `createScript` or extending
42
+ * ScriptType, which itself extends Script.
43
+ */
44
+ function isClassic(cls) {
45
+ return cls.prototype instanceof ScriptType;
46
+ }
47
+
48
+ /**
49
+ * @param {Function} cls - A script class.
50
+ * @returns {string} The name it is known by: the name it was created or registered under, its
51
+ * static `scriptName`, or its class name.
52
+ */
53
+ function scriptName(cls) {
54
+ const any = /** @type {any} */ (cls);
55
+ if (any.__name) return any.__name;
56
+ // only a scriptName the class declares itself, not one inherited from a base script
57
+ if (Object.prototype.hasOwnProperty.call(cls, 'scriptName') && any.scriptName) return any.scriptName;
58
+ return cls.name || '(unnamed)';
59
+ }
60
+
61
+ /**
62
+ * Finds the script classes of the app: those the script registry holds, which are the ones loaded
63
+ * from script assets or declared with `createScript`, and the classes of the script instances on
64
+ * the entities of the hierarchy, which add the classes created on an entity straight from their
65
+ * class and never registered.
66
+ *
67
+ * @param {AppBase} app - The app.
68
+ * @returns {Map<Function, ScriptEntry>} The script classes, the registered ones first.
69
+ */
70
+ function surveyScripts(app) {
71
+ /** @type {Map<Function, ScriptEntry>} */
72
+ const scripts = new Map();
73
+ const entryOf = (cls, registered) => {
74
+ let entry = scripts.get(cls);
75
+ if (!entry) {
76
+ entry = { name: scriptName(cls), registered, instances: [] };
77
+ scripts.set(cls, entry);
78
+ }
79
+ return entry;
80
+ };
81
+
82
+ for (const cls of app.scripts?.list() ?? []) entryOf(cls, true);
83
+
84
+ app.root?.forEach((node) => {
85
+ const component = /** @type {any} */ (node).c?.script;
86
+ for (const script of component?.scripts ?? []) {
87
+ const cls = script.constructor;
88
+ if (isScriptClass(cls)) entryOf(cls, false).instances.push({ script, entity: /** @type {Entity} */ (node) });
89
+ }
90
+ });
91
+
92
+ return scripts;
93
+ }
94
+
95
+ /**
96
+ * @param {Script|ScriptType} script - A script instance.
97
+ * @param {Entity} entity - The entity it is on.
98
+ * @returns {boolean} Whether it runs: enabled itself, on an enabled component of an entity enabled
99
+ * in the hierarchy.
100
+ */
101
+ function isRunning(script, entity) {
102
+ const component = /** @type {any} */ (entity).c?.script;
103
+ return !!(script.enabled && component?.enabled && entity.enabled);
104
+ }
105
+
106
+ /**
107
+ * @param {Function} cls - A script class.
108
+ * @returns {string[]} The lifecycle methods it defines, on itself or a base class of its own.
109
+ */
110
+ function lifecycleMethods(cls) {
111
+ const proto = cls.prototype;
112
+ return LIFECYCLE.filter(name => typeof proto[name] === 'function' &&
113
+ proto[name] !== /** @type {any} */ (Script.prototype)[name] && proto[name] !== /** @type {any} */ (ScriptType.prototype)[name]);
114
+ }
115
+
116
+ /**
117
+ * The attributes a script declares, with their types: those of a `createScript` script, which it
118
+ * declares in code, or those its script asset describes.
119
+ *
120
+ * @param {Function} cls - A script class.
121
+ * @param {string} name - The name it is known by.
122
+ * @param {AppBase} app - The app, whose registry keeps the attribute schemas of script assets.
123
+ * @returns {[string, string][]} The attribute names and types.
124
+ */
125
+ function declaredAttributes(cls, name, app) {
126
+ const index = isClassic(cls) ? /** @type {any} */ (cls).attributes?.index : null;
127
+ const attributes = index ?? app.scripts?.getSchema?.(name)?.attributes ?? {};
128
+ return Object.entries(attributes).map(([attribute, info]) => [attribute, String(info?.type ?? '')]);
129
+ }
130
+
131
+ /**
132
+ * @param {Function} cls - A script class.
133
+ * @param {string} name - The name it is known by.
134
+ * @param {AppBase} app - The app.
135
+ * @returns {Asset|null} The script asset it was loaded from, if found: a classic asset holds its
136
+ * classes, and a module asset describes its scripts by name.
137
+ */
138
+ function scriptAsset(cls, name, app) {
139
+ for (const asset of app.assets?.list() ?? []) {
140
+ if (asset.type !== 'script') continue;
141
+ const resource = asset.resource;
142
+ if (resource && typeof resource === 'object' && Object.values(resource).includes(cls)) return asset;
143
+ if (asset.data?.scripts?.[name]) return asset;
144
+ }
145
+ return null;
146
+ }
147
+
148
+ /**
149
+ * The source of a script, as the browser keeps it for every function. A script written as a class
150
+ * is its whole class. A `createScript` script is a function whose methods are set on its prototype
151
+ * one by one, so its source is put back together from those methods. The text is what the browser
152
+ * loaded: minified when the app ships minified.
153
+ *
154
+ * @param {Function} cls - A script class.
155
+ * @param {string} name - The name it is known by.
156
+ * @returns {string} The source.
157
+ */
158
+ function scriptSource(cls, name) {
159
+ const text = Function.prototype.toString.call(cls);
160
+ if (/^class\b/.test(text)) return text;
161
+
162
+ const variable = /^[a-z_$][\w$]*$/i.test(cls.name) && cls.name !== 'scriptType' ? cls.name : 'script';
163
+ const lines = [`const ${variable} = pc.createScript('${name}');`];
164
+ for (const key of Object.getOwnPropertyNames(cls.prototype)) {
165
+ if (key === 'constructor') continue;
166
+ const descriptor = Object.getOwnPropertyDescriptor(cls.prototype, key);
167
+ if (typeof descriptor?.value === 'function') {
168
+ lines.push('', `${variable}.prototype.${key} = ${Function.prototype.toString.call(descriptor.value)};`);
169
+ }
170
+ }
171
+ return lines.join('\n');
172
+ }
173
+
174
+ /** @type {WeakMap<Function, number>} */
175
+ const ids = new WeakMap();
176
+ let nextId = 1;
177
+
178
+ /**
179
+ * @param {Function} cls - A script class.
180
+ * @returns {string} A row key that stays the same for the lifetime of the class, as two classes can
181
+ * share a name when only one of them is registered.
182
+ */
183
+ function keyOf(cls) {
184
+ let id = ids.get(cls);
185
+ if (id === undefined) {
186
+ id = nextId++;
187
+ ids.set(cls, id);
188
+ }
189
+ return `script${id}`;
190
+ }
191
+
192
+ /**
193
+ * One row per script class, by name, dimmed when no entity uses it.
194
+ *
195
+ * @param {Map<Function, ScriptEntry>} survey - The script classes found.
196
+ * @returns {ListRow[]} The rows.
197
+ */
198
+ function scriptListRows(survey) {
199
+ const classes = [...survey.keys()].sort((a, b) => survey.get(a).name.localeCompare(survey.get(b).name));
200
+ return classes.map((cls) => {
201
+ const { name, registered, instances } = survey.get(cls);
202
+ const running = instances.filter(({ script, entity }) => isRunning(script, entity)).length;
203
+ const cells = [{ text: name, cls: 'pci-cell-name' }];
204
+ cells.push({ text: isClassic(cls) ? 'classic' : 'esm', cls: 'pci-cell-tag pci-cell-tag-info' });
205
+ cells.push({
206
+ text: `${instances.length} instance${instances.length === 1 ? '' : 's'}${instances.length ? ` · ${running} running` : ''}`,
207
+ cls: 'pci-cell-info'
208
+ });
209
+ const title = `${name} (${isClassic(cls) ? 'ScriptType' : 'ESM Script'})\n` +
210
+ `${instances.length} instance${instances.length === 1 ? '' : 's'}, ${running} running` +
211
+ `${registered ? '' : '\nnot in the script registry: created on its entities from its class'}`;
212
+ return {
213
+ key: keyOf(cls),
214
+ item: cls,
215
+ name,
216
+ // found by the entities using it too
217
+ matches: filter => name.toLowerCase().includes(filter) ||
218
+ instances.some(({ entity }) => entity.name.toLowerCase().includes(filter)),
219
+ dim: instances.length === 0,
220
+ title,
221
+ cells
222
+ };
223
+ });
224
+ }
225
+
226
+ /**
227
+ * Everything the property view shows for a script class: what it is and declares, the entities
228
+ * using it, each instance opening in place into its attributes, and its source.
229
+ *
230
+ * @param {Function} cls - The script class.
231
+ * @param {{ app: AppBase }} ctx - The app.
232
+ * @returns {PropertySection[]} The sections.
233
+ */
234
+ function buildScriptModel(cls, ctx) {
235
+ const { app } = ctx;
236
+ const entry = surveyScripts(app).get(cls) ?? { name: scriptName(cls), registered: false, instances: [] };
237
+ const { name, registered, instances } = entry;
238
+ const sections = [];
239
+
240
+ const general = makeSection('script', `Script "${name}"`);
241
+ push(general, 'name', describeValue(name));
242
+ // createScript names every class it makes the same, so only a class written by hand says anything
243
+ if (cls.name && cls.name !== 'scriptType') push(general, 'class', describeValue(cls.name));
244
+ push(general, 'kind', { text: isClassic(cls) ? 'ScriptType (classic)' : 'Script (ESM)', cls: 'obj' });
245
+ push(general, 'registered', registered ? describeValue(true) :
246
+ { text: 'false, created on its entities from its class', cls: 'bool' });
247
+ const asset = scriptAsset(cls, name, app);
248
+ if (asset) push(general, 'from asset', describeValue(asset));
249
+ const methods = lifecycleMethods(cls);
250
+ push(general, 'methods', { text: methods.length ? methods.join(', ') : 'none', cls: methods.length ? 'obj' : 'null' });
251
+ const attributes = declaredAttributes(cls, name, app);
252
+ if (attributes.length) {
253
+ push(general, 'attributes', {
254
+ text: `${attributes.length} attribute${attributes.length === 1 ? '' : 's'}`,
255
+ cls: 'obj',
256
+ items: attributes.map(([attribute, type]) => ({ label: attribute, text: type || '-', cls: 'obj' }))
257
+ });
258
+ }
259
+ sections.push(general);
260
+
261
+ const usage = makeSection('instances', 'Used by');
262
+ const running = instances.filter(({ script, entity }) => isRunning(script, entity)).length;
263
+ push(usage, 'instances', {
264
+ text: instances.length ? `${instances.length} instance${instances.length === 1 ? '' : 's'}, ${running} running` : 'none',
265
+ cls: instances.length ? 'obj' : 'null',
266
+ items: instances.slice(0, MAX_INSTANCES).map(({ script, entity }, index) => ({
267
+ label: `[${index}]`,
268
+ text: `${nodeLabel(entity)}${isRunning(script, entity) ? '' : ' (not running)'}`,
269
+ cls: 'ref',
270
+ target: entity,
271
+ expand: () => scriptInstanceRows(script)
272
+ }))
273
+ });
274
+ if (instances.length > MAX_INSTANCES) {
275
+ usage.rows.push({ key: 'more', label: '', value: { text: `… ${instances.length - MAX_INSTANCES} more`, cls: 'null' }, depth: 1 });
276
+ }
277
+ sections.push(usage);
278
+
279
+ const source = scriptSource(cls, name);
280
+ const code = makeSection('source', 'Source');
281
+ const lines = source.split('\n').length;
282
+ push(code, 'source', { text: `${lines} line${lines === 1 ? '' : 's'}`, cls: 'num', code: source });
283
+ sections.push(code);
284
+
285
+ return sections;
286
+ }
287
+
288
+ export { buildScriptModel, isScriptClass, scriptListRows, surveyScripts };
@@ -0,0 +1,287 @@
1
+ import {
2
+ bindGroupNames, pixelFormatInfo, SAMPLETYPE_DEPTH, SAMPLETYPE_FLOAT, SAMPLETYPE_INT, SAMPLETYPE_UINT, SAMPLETYPE_UNFILTERABLE_FLOAT, SHADERLANGUAGE_WGSL, SHADERSTAGE_COMPUTE, SHADERSTAGE_FRAGMENT, SHADERSTAGE_VERTEX, uniformTypeToName
3
+ } from 'playcanvas';
4
+
5
+ import { describeValue } from './describe.js';
6
+ import { makeSection, push, reflectRows } from './model.js';
7
+
8
+ /** @import { BindGroupFormat } from 'playcanvas' */
9
+ /** @import { GraphicsDevice } from 'playcanvas' */
10
+ /** @import { Shader } from 'playcanvas' */
11
+ /** @import { UniformBufferFormat } from 'playcanvas' */
12
+ /** @import { Described } from './describe.js' */
13
+ /** @import { ListRow } from './list-view.js' */
14
+ /** @import { PropertySection } from './model.js' */
15
+
16
+ // shader properties the model shows explicitly, plus plumbing and the source strings themselves
17
+ const SKIP_SHADER = [
18
+ 'id', 'name', 'label', 'ready', 'failed', 'definition', 'device', 'impl', 'attributes',
19
+ 'vUnmodified', 'fUnmodified', 'cUnmodified', 'meshUniformBufferFormat', 'meshBindGroupFormat'
20
+ ];
21
+
22
+ const SAMPLE_TYPE_NAMES = new Map([
23
+ [SAMPLETYPE_FLOAT, 'float'], [SAMPLETYPE_UNFILTERABLE_FLOAT, 'unfilterable-float'], [SAMPLETYPE_DEPTH, 'depth'],
24
+ [SAMPLETYPE_INT, 'int'], [SAMPLETYPE_UINT, 'uint']
25
+ ]);
26
+
27
+ /**
28
+ * @param {Shader} shader - A shader.
29
+ * @returns {string} The language of its source.
30
+ */
31
+ function languageName(shader) {
32
+ return shader.definition.shaderLanguage === SHADERLANGUAGE_WGSL ? 'WGSL' : 'GLSL';
33
+ }
34
+
35
+ /**
36
+ * @param {Shader} shader - A shader.
37
+ * @returns {string} The compilation state: 'failed', 'ready' or 'compiling'.
38
+ */
39
+ function stateName(shader) {
40
+ return shader.failed ? 'failed' : shader.ready ? 'ready' : 'compiling';
41
+ }
42
+
43
+ /**
44
+ * @param {string} source - Shader source code.
45
+ * @returns {string} Its length in lines.
46
+ */
47
+ function sourceLines(source) {
48
+ const lines = source.split('\n').length;
49
+ return `${lines} line${lines === 1 ? '' : 's'}`;
50
+ }
51
+
52
+ /**
53
+ * @param {string} label - The version label.
54
+ * @param {string|null|undefined} source - The source, if kept.
55
+ * @returns {Described} A code row, or a note that the version is not available.
56
+ */
57
+ function sourceRow(label, source) {
58
+ if (typeof source !== 'string' || source === '') return { label, text: 'not kept', cls: 'null' };
59
+ return { label, text: sourceLines(source), cls: 'num', code: source };
60
+ }
61
+
62
+ /**
63
+ * The versions of one shader stage: the source as given, after the preprocessor resolved includes
64
+ * and defines, and the text finally handed to the graphics API when the device rewrote it again,
65
+ * for example to add uniform buffers or translate GLSL to WGSL.
66
+ *
67
+ * @param {Shader} shader - The shader.
68
+ * @param {'v'|'f'|'c'} stage - The stage prefix.
69
+ * @returns {Described} A row summarizing the stage, with one expandable item per version.
70
+ */
71
+ function sourceVersions(shader, stage) {
72
+ const definition = shader.definition;
73
+ const impl = /** @type {any} */ (shader.impl);
74
+ const preprocessed = definition[`${stage}shader`];
75
+ const original = shader[`${stage}Unmodified`];
76
+ const compiled = stage === 'c' ? impl?._computeCode :
77
+ stage === 'v' ? (impl?._vertexCode ?? impl?._vsource) : (impl?._fragmentCode ?? impl?._fsource);
78
+
79
+ const items = [];
80
+ // the unmodified source is only kept in debug builds
81
+ items.push(sourceRow('original', original));
82
+ items.push(sourceRow('preprocessed', preprocessed));
83
+ if (typeof compiled === 'string' && compiled !== preprocessed) items.push(sourceRow('compiled', compiled));
84
+
85
+ const kept = items.filter(item => item.code).length;
86
+ return { text: `${kept} version${kept === 1 ? '' : 's'}`, cls: kept ? 'obj' : 'null', items };
87
+ }
88
+
89
+ /**
90
+ * @param {number} visibility - SHADERSTAGE_* flags.
91
+ * @returns {string} The stages, comma separated.
92
+ */
93
+ function stagesText(visibility) {
94
+ const stages = [];
95
+ if (visibility & SHADERSTAGE_VERTEX) stages.push('vertex');
96
+ if (visibility & SHADERSTAGE_FRAGMENT) stages.push('fragment');
97
+ if (visibility & SHADERSTAGE_COMPUTE) stages.push('compute');
98
+ return stages.join(', ') || 'none';
99
+ }
100
+
101
+ /**
102
+ * @param {string[]} rows - Rows of cells joined by tabs.
103
+ * @returns {string} The rows with each column padded to its widest cell.
104
+ */
105
+ function table(rows) {
106
+ const cells = rows.map(row => row.split('\t'));
107
+ const widths = [];
108
+ for (const row of cells) {
109
+ row.forEach((cell, i) => {
110
+ widths[i] = Math.max(widths[i] ?? 0, cell.length);
111
+ });
112
+ }
113
+ return cells.map(row => row.map((cell, i) => (i === row.length - 1 ? cell : cell.padEnd(widths[i]))).join(' ').trimEnd()).join('\n');
114
+ }
115
+
116
+ /**
117
+ * Lays out a uniform buffer format as text: the std140 offsets, sizes and types of its uniforms.
118
+ *
119
+ * @param {UniformBufferFormat} format - The format.
120
+ * @returns {string} The layout.
121
+ */
122
+ function formatUniformBuffer(format) {
123
+ const rows = ['offset\tsize\ttype\tname'];
124
+ for (const uniform of format.uniforms) {
125
+ const type = `${uniformTypeToName[uniform.type] ?? uniform.type}${uniform.count ? `[${uniform.count}]` : ''}`;
126
+ rows.push(`${uniform.offset * 4}\t${uniform.byteSize}\t${type}\t${uniform.name}`);
127
+ }
128
+ return `uniform buffer, ${format.byteSize} bytes, ${format.uniforms.length} uniform${format.uniforms.length === 1 ? '' : 's'}\n${table(rows)}`;
129
+ }
130
+
131
+ /**
132
+ * Lays out a bind group format as text: its uniform buffers, textures and storage resources with
133
+ * their slots and the stages that can see them.
134
+ *
135
+ * @param {BindGroupFormat} format - The format.
136
+ * @returns {string} The layout.
137
+ */
138
+ function formatBindGroup(format) {
139
+ const rows = ['slot\tkind\tname\tdetails\tstages'];
140
+ for (const ub of format.uniformBufferFormats) {
141
+ rows.push(`${ub.slot}\tuniform buffer\t${ub.name}\t\t${stagesText(ub.visibility)}`);
142
+ }
143
+ for (const texture of format.textureFormats) {
144
+ const details = `${texture.textureDimension} ${SAMPLE_TYPE_NAMES.get(texture.sampleType) ?? texture.sampleType}` +
145
+ `${texture.multisampled ? ' multisampled' : ''}${texture.hasSampler ? ` + sampler ${texture.samplerName ?? ''}`.trimEnd() : ''}`;
146
+ rows.push(`${texture.slot}\ttexture\t${texture.name}\t${details}\t${stagesText(texture.visibility)}`);
147
+ }
148
+ for (const buffer of format.storageBufferFormats) {
149
+ rows.push(`${buffer.slot}\tstorage buffer\t${buffer.name}\t${buffer.readOnly ? 'read-only' : 'read-write'}\t${stagesText(buffer.visibility)}`);
150
+ }
151
+ for (const texture of format.storageTextureFormats) {
152
+ const access = texture.write && texture.read ? 'read-write' : texture.write ? 'write-only' : 'read-only';
153
+ rows.push(`${texture.slot}\tstorage texture\t${texture.name}\t${texture.textureDimension} ${pixelFormatInfo.get(texture.format)?.name ?? texture.format} ${access}\t${stagesText(texture.visibility)}`);
154
+ }
155
+ const count = rows.length - 1;
156
+ return `bind group, ${count} binding${count === 1 ? '' : 's'}\n${table(rows)}`;
157
+ }
158
+
159
+ /**
160
+ * @param {string} label - What the layout describes.
161
+ * @param {UniformBufferFormat|BindGroupFormat|null|undefined} format - The format.
162
+ * @param {(format: any) => string} render - The text layout of the format.
163
+ * @returns {Described} A code row, or a note that there is no format.
164
+ */
165
+ function formatRow(label, format, render) {
166
+ if (!format) return { label, text: 'none', cls: 'null' };
167
+ let text;
168
+ if (label.includes('uniform')) {
169
+ text = `${format.byteSize} bytes`;
170
+ } else {
171
+ const count = format.uniformBufferFormats.length + format.textureFormats.length + format.storageBufferFormats.length + format.storageTextureFormats.length;
172
+ text = `${count} binding${count === 1 ? '' : 's'}`;
173
+ }
174
+ return { label, text, cls: 'num', code: render(format) };
175
+ }
176
+
177
+ /**
178
+ * The bind groups a shader is processed against: the view and material groups from its processing
179
+ * options, and the mesh groups the device generated from its uniforms.
180
+ *
181
+ * @param {Shader} shader - The shader.
182
+ * @returns {PropertySection} The section, one row per group.
183
+ */
184
+ function bindGroupSection(shader) {
185
+ const section = makeSection('groups', 'Bind groups');
186
+ const options = shader.definition.processingOptions;
187
+ const impl = /** @type {any} */ (shader.impl);
188
+
189
+ if (!options && !shader.meshUniformBufferFormat && !shader.meshBindGroupFormat && !impl?.computeBindGroupFormat) {
190
+ push(section, 'layout', { text: 'not processed against bind groups', cls: 'null' });
191
+ return section;
192
+ }
193
+
194
+ bindGroupNames.forEach((name, index) => {
195
+ const uniformFormat = index === 3 ? shader.meshUniformBufferFormat : options?.uniformFormats?.[index];
196
+ const bindGroupFormat = index === 2 ? shader.meshBindGroupFormat : options?.bindGroupFormats?.[index];
197
+ if (!uniformFormat && !bindGroupFormat) return;
198
+ const items = [];
199
+ if (uniformFormat) items.push(formatRow('uniform buffer', uniformFormat, formatUniformBuffer));
200
+ if (bindGroupFormat) items.push(formatRow('bindings', bindGroupFormat, formatBindGroup));
201
+ push(section, `group ${index} · ${name}`, { text: items.map(item => item.text).join(', '), cls: 'obj', items });
202
+ });
203
+
204
+ if (impl?.computeBindGroupFormat) {
205
+ const items = [formatRow('bindings', impl.computeBindGroupFormat, formatBindGroup)];
206
+ if (impl.computeReflectedUniformBufferFormat) items.push(formatRow('uniform buffer', impl.computeReflectedUniformBufferFormat, formatUniformBuffer));
207
+ push(section, 'compute', { text: items.map(item => item.text).join(', '), cls: 'obj', items });
208
+ }
209
+
210
+ return section;
211
+ }
212
+
213
+ /**
214
+ * Every shader alive on the device, in creation order. A shader whose definition had no source
215
+ * never reached the device and is not listed; one that failed to compile is.
216
+ *
217
+ * @param {GraphicsDevice} device - The device.
218
+ * @returns {Shader[]} The shaders.
219
+ */
220
+ function collectShaders(device) {
221
+ return [...device.shaders ?? []];
222
+ }
223
+
224
+ /**
225
+ * One row per shader on the device, tagged with its language, compute shaders and failures.
226
+ *
227
+ * @param {GraphicsDevice} device - The device.
228
+ * @returns {ListRow[]} The rows.
229
+ */
230
+ function shaderRows(device) {
231
+ return collectShaders(device).map((shader) => {
232
+ const name = shader.name;
233
+ const compute = !!shader.definition.cshader;
234
+ const cells = [{ text: name, cls: 'pci-cell-name' }];
235
+ cells.push({ text: languageName(shader), cls: 'pci-cell-tag pci-cell-tag-info' });
236
+ if (compute) cells.push({ text: 'compute', cls: 'pci-cell-tag pci-cell-tag-info' });
237
+ if (shader.failed) cells.push({ text: 'failed', cls: 'pci-cell-tag' });
238
+ cells.push({ text: `#${shader.id}`, cls: 'pci-cell-info pci-cell-right' });
239
+ return { key: `shader${shader.id}`, item: shader, name, dim: !shader.ready, title: `${shader.label}\n${stateName(shader)}`, cells };
240
+ });
241
+ }
242
+
243
+ /**
244
+ * Everything the property view shows for a shader: identity and state, every kept version of its
245
+ * sources, the bind group layouts it was processed against, its vertex attributes, and every
246
+ * other public property.
247
+ *
248
+ * @param {Shader} shader - The shader.
249
+ * @returns {PropertySection[]} The sections.
250
+ */
251
+ function buildShaderModel(shader) {
252
+ const sections = [];
253
+ const definition = shader.definition;
254
+
255
+ const general = makeSection('shader', 'Shader');
256
+ push(general, 'name', describeValue(shader.name));
257
+ push(general, 'id', describeValue(shader.id));
258
+ push(general, 'language', { text: languageName(shader), cls: 'obj' });
259
+ push(general, 'state', { text: stateName(shader), cls: shader.failed ? 'err' : shader.ready ? 'bool' : 'null' });
260
+ if (definition.useTransformFeedback) push(general, 'transform feedback', describeValue(definition.feedbackVaryings ?? true));
261
+ sections.push(general);
262
+
263
+ // each version of each stage expands into a code block
264
+ const sources = makeSection('sources', 'Sources');
265
+ if (definition.cshader) {
266
+ push(sources, 'compute', sourceVersions(shader, 'c'));
267
+ } else {
268
+ push(sources, 'vertex', sourceVersions(shader, 'v'));
269
+ push(sources, 'fragment', sourceVersions(shader, 'f'));
270
+ }
271
+ sections.push(sources);
272
+
273
+ sections.push(bindGroupSection(shader));
274
+
275
+ const attributes = makeSection('attributes', 'Vertex attributes');
276
+ push(attributes, 'declared', describeValue(definition.attributes ?? null));
277
+ push(attributes, 'used', describeValue(shader.attributes));
278
+ sections.push(attributes);
279
+
280
+ const rest = makeSection('props', 'Properties');
281
+ reflectRows(rest, shader, [], SKIP_SHADER);
282
+ if (rest.rows.length) sections.push(rest);
283
+
284
+ return sections;
285
+ }
286
+
287
+ export { buildShaderModel, collectShaders, formatBindGroup, formatUniformBuffer, shaderRows, stateName };