@displayxr/inline3d 1.47.4 → 1.47.6

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.
@@ -0,0 +1,270 @@
1
+ # Content lifecycle rules: release what a swap replaces
2
+
3
+ Companion to [`authoring-inline-3d.md`](authoring-inline-3d.md) and
4
+ [`woven-canvas-rules.md`](woven-canvas-rules.md). Those pages keep a woven canvas alive and
5
+ joined. This one covers what happens to the GPU over the life of that canvas, when the page swaps
6
+ content in and out of it: a product carousel, a catalogue, a gallery that loads glTF after glTF.
7
+
8
+ The symptom is slow and silent. Nothing on the page looks wrong. The browser's **GPU process**
9
+ grows by a few megabytes a minute for as long as the page is open, until the machine runs short
10
+ of memory. The rules below come from a production page that grew about **8 MB/min** this way on a
11
+ Linux laptop. They were then checked against the PlayCanvas 2.22.3 and three.js 0.180 sources, and
12
+ measured against this SDK's own swap paths (§5).
13
+
14
+ ---
15
+
16
+ ## 1. What leaks, and why it costs so much
17
+
18
+ Every distinct material set a page draws makes the engine **link a WebGL program**: one per
19
+ material variant (lighting, skinning, maps, pass). A linked program is not just a handle. The
20
+ driver keeps its compiled form for as long as the program exists. On one Linux driver (Mesa, integrated
21
+ graphics) that was **about 5 MB per live program**, measured with an allocation tracer on the
22
+ browser's GPU process: the outstanding allocations were the linked program's IR, freed only by
23
+ `glDeleteProgram`. The production page linked about four new programs per product and deleted
24
+ none, so each product it showed left 15–25 MB behind for the life of the page.
25
+
26
+ What the page saw as "swapping a product" was, to the GPU, *adding* one. The page destroyed the
27
+ entity and its materials, and the engine kept every program those materials had linked.
28
+
29
+ ## 2. Engine facts
30
+
31
+ ### PlayCanvas (2.22.3)
32
+
33
+ - **`ProgramLibrary` caches every program for the device's life.** Its cache is keyed by the
34
+ variant's options. An entry leaves only when that `Shader` is destroyed (`Shader.destroy()`),
35
+ or when the device is.
36
+ - **`Material.destroy()` destroys no program.** It empties `material.variants` and clears each
37
+ mesh instance's per-pass cache (its bind groups and uniform buffers), and that is all. Once it
38
+ has run, the material no longer even says which programs it had.
39
+ - **`Asset.unload()` on a glTF container** destroys the container's textures, meshes and
40
+ materials (each material through `Material.destroy()`, so no program again). **The registry
41
+ shares one container per URL** between every caller that loads it.
42
+ - **One `Shader` per key, shared.** Two materials with the same options get the same `Shader`
43
+ object from the library. Destroying it under one of them breaks the other.
44
+ - **The compiled shaders outlive their program.** `WebglShader.destroy()` deletes the program and
45
+ nothing else. The vertex and fragment shaders it was linked from stay in a module-private
46
+ `CompiledShaderCache` (keyed by source) until the device is destroyed. So even a page that
47
+ destroys its programs keeps two compiled shaders per program it ever linked.
48
+ - **`app.destroy()` frees everything.** It unloads every asset, clears the program library
49
+ (destroying each program) and destroys the device, which deletes the cached shaders.
50
+
51
+ ### three.js (0.180)
52
+
53
+ - **Programs are reference-counted per material.** The renderer listens for each material's
54
+ `dispose` event. `material.dispose()` releases that material's programs
55
+ (`WebGLPrograms.releaseProgram`), and the last material out deletes the program
56
+ (`gl.deleteProgram`). Materials that compile to the same program share it.
57
+ - **There is no compiled-shader cache to leak.** `WebGLProgram` deletes its two shaders right
58
+ after linking.
59
+ - **Nothing is freed by taking an object out of the scene,** or by dropping the last reference to
60
+ it. Call `geometry.dispose()`, `material.dispose()` and `texture.dispose()` for each texture.
61
+ `material.dispose()` does not dispose the textures it uses.
62
+ - **`renderer.info.programs.length`** is the live program count: the pass test of §4, for free.
63
+
64
+ ## 3. The rules
65
+
66
+ ### 1. Release everything a swap replaces, not just the entity
67
+
68
+ A swap replaces five things: the **entity**, its **materials**, the **programs** those materials
69
+ linked, their **textures**, and the **container** they came from. Destroying the entity releases
70
+ the first. Unloading the container releases the textures, meshes and materials. The engine
71
+ releases the programs for nobody.
72
+
73
+ **On a PlayCanvas tile the SDK created, it releases the programs for you** (from the release
74
+ after 1.47.5): when the page destroys a material, directly or through the container's
75
+ `asset.unload()`, the SDK destroys the programs that material linked one frame later, when
76
+ nothing still in the app uses them (rules 2 and 3, done automatically; see §6). So the page's
77
+ part is the engine's own teardown: **destroy the entity, and destroy or unload what it drew
78
+ with.** `entity.destroy()` alone is not enough. A container that stays loaded keeps its
79
+ materials alive, and a live material keeps its programs on purpose, because the page may show it
80
+ again.
81
+
82
+ On a PlayCanvas tile, content you add under `handle.engine.root` is released in one call:
83
+
84
+ ```js
85
+ const prev = shown; // { entity, asset } of the outgoing product
86
+ const { programs, container } = handle.releaseEntity(prev.entity, { asset: prev.asset });
87
+ ```
88
+
89
+ `releaseEntity` (on `addSplat(…, { engine: 'playcanvas' })` and PlayCanvas `addModel`
90
+ handles) does rules 2 to 4 in that call, including the container's last-user check of rule 4,
91
+ and the SDK does rule 5 on its own devices. Without it, the page unloads the container itself
92
+ (rule 4) and the automatic release above covers the programs. On a three.js scene, traverse and dispose (§2, three.js).
93
+
94
+ ### 2. Collect the programs before you destroy anything
95
+
96
+ `Material.destroy()` empties `material.variants`, and the container's unload destroys its
97
+ materials. Read `variants` of every material the entity's mesh instances use (and each mesh
98
+ instance's own shader cache), and of the container's materials, **first**. Then destroy the entity,
99
+ then unload the container, then destroy the programs.
100
+
101
+ ### 3. Destroy only a program nothing else still draws with
102
+
103
+ Because the library hands one `Shader` to every material with the same key, a program you collected
104
+ may also belong to content that stays. Before `shader.destroy()`, look at what is still in the app:
105
+ every mesh instance under `app.root` (enabled or not), every layer's mesh instances, and every
106
+ material asset still in the registry. Destroy only what none of them holds. `Shader.destroy()` is
107
+ the engine's own release: it deletes the program and takes it out of the library, so the next ask
108
+ for that variant compiles it again rather than returning a dead one.
109
+
110
+ ### 4. Unload a container with its last user
111
+
112
+ The registry shares one container per URL, so two products loaded from the same file share
113
+ textures and materials. Unload it (`app.assets.remove(asset); asset.unload()`) only when nothing
114
+ left in the scene draws from it: refcount your holders, or check that no remaining mesh instance
115
+ uses one of its meshes or materials (what `releaseEntity` does: it reports `container: 'in-use'`
116
+ and leaves it loaded).
117
+
118
+ ### 5. Let the engine give compiled shaders back
119
+
120
+ Rule 3 deletes the programs. The compiled shaders behind them stay in the engine's
121
+ `CompiledShaderCache` (§2), so a page that follows rules 1–4 still keeps two compiled shaders for
122
+ every program it ever linked (measured in §5: shaders 10 → 91 over 24 swaps, programs bounded).
123
+ The fix counts, per cached shader, the programs that use it, and deletes the shader when the last
124
+ one is destroyed. **The SDK applies it to every PlayCanvas device it creates** (see §6). A page that
125
+ runs its own PlayCanvas app outside the SDK has to patch the engine itself: the change is about 20
126
+ lines in `src/platform/graphics/webgl/webgl-shader.js` (a count in `_compileShaderSource`, a
127
+ release in `WebglShader.destroy`, cleared on context loss). `js/inline3d-pc-shader-cache.js` is the
128
+ reference.
129
+
130
+ ### 6. On three.js, dispose explicitly, and textures separately
131
+
132
+ Removing a mesh from the scene frees nothing on the GPU. For each object a swap replaces, call
133
+ `geometry.dispose()`, `material.dispose()`, and `dispose()` on every texture the material uses
134
+ (`map`, `normalMap`, …). The renderer then deletes a program when its last material is disposed.
135
+ Do not dispose a geometry, material or texture that content which stays still uses.
136
+
137
+ ### 7. Fewer variants is cheaper than releasing them
138
+
139
+ Each distinct option set is a program. A catalogue whose items share one material set (the same
140
+ maps, the same lighting) links once and swaps meshes and textures. A catalogue of hand-made
141
+ materials links per item. Releasing keeps the second kind bounded, but every swap then compiles
142
+ again, which costs a frame or more of main-thread time on first draw: warm the incoming content
143
+ before you show it (`handle.warmEntity`, [PlayCanvas adapter § warmEntity](playcanvas-adapter.md#warmentity--a-gltfs-first-draw-without-the-freeze)).
144
+
145
+ ### 8. Removing the tile is not a content swap
146
+
147
+ `handle.remove()` on a PlayCanvas tile destroys the app, and with it every program, shader,
148
+ texture and asset (measured in §5: nothing left). But a new tile is a new canvas for the browser's
149
+ compositor to join, with the raw side-by-side window that brings
150
+ ([woven-canvas rules 2 and 3](woven-canvas-rules.md#2-never-remount-a-woven-canvas-inside-a-screen)).
151
+ Keep the tile and swap what is in it, with rules 1–5.
152
+
153
+ ## 4. The pass test: hook, swap, plateau
154
+
155
+ Count program and shader creation on the context, swap content about ten times, and check that the
156
+ live count (`created − deleted`) **plateaus**. A page that leaks climbs with every new item. A page
157
+ that releases goes up and down around what is on screen.
158
+
159
+ ```js
160
+ // Run before the page creates its WebGL context (inject at document start, e.g. with the
161
+ // DevTools protocol's Page.addScriptToEvaluateOnNewDocument, then reload).
162
+ const live = (globalThis.__glLive = { programs: 0, shaders: 0 });
163
+ for (const Ctx of [WebGLRenderingContext, WebGL2RenderingContext]) {
164
+ const p = Ctx.prototype;
165
+ const count = (name, key, step) => {
166
+ const orig = p[name];
167
+ p[name] = function (...args) {
168
+ const r = orig.apply(this, args);
169
+ if (step > 0 ? r : args[0]) live[key] += step; // a create that returned an object; a delete of one
170
+ return r;
171
+ };
172
+ };
173
+ count('createProgram', 'programs', +1);
174
+ count('deleteProgram', 'programs', -1);
175
+ count('createShader', 'shaders', +1);
176
+ count('deleteShader', 'shaders', -1);
177
+ }
178
+ // Swap content ten times, reading __glLive after each swap: it must level off.
179
+ ```
180
+
181
+ Two things to know when reading it:
182
+ - **Content that repeats plateaus anyway.** The engine reuses a cached program for a variant it has
183
+ already linked, so a carousel of eight products stops growing after the eighth even without a
184
+ release. Test with more distinct items than the page will show at once, or with real catalogue
185
+ data.
186
+ - **The SDK's own transitions compile once.** The first `setSource` with each transition kind
187
+ links its programs, and later swaps reuse them: a few rises, then flat.
188
+
189
+ **`?dxrdiag` does this for you on a PlayCanvas tile.** The diagnostics overlay shows
190
+ `gl programs N live (+created −deleted) shaders N live (…) cache patched`, and the dump
191
+ (`copy(__dxrDiag.dump())`) carries the same under `glObjects`. At every content swap the SDK can
192
+ see (a `setSource`, a glTF container loaded into the tile's registry, a `releaseEntity`), it samples
193
+ the live program count. When the count has not dropped across the last 10 swaps of one kind and
194
+ rose at more than 60% of them, it warns once in the console. It costs nothing when diag is off.
195
+ `?dxrdiag=keepshaders` turns rule 5 off for an A/B (page-wide; set it on the URL before the first
196
+ tile loads). The overlay also shows `release patched (−N)`: programs the automatic release of
197
+ rule 1 destroyed. `?dxrdiag=keepprograms` turns that off for an A/B (per tile). Details: [PlayCanvas adapter § Diagnosing transition stalls](playcanvas-adapter.md#diagnosing-transition-stalls-diag--dxrdiag-36).
198
+
199
+ ## 5. Measured: the SDK's own paths
200
+
201
+ DisplayXR Browser on Linux (Chromium 156, Mesa, integrated graphics), PlayCanvas 2.22.3, the hook of §4,
202
+ Khronos glTF sample models. "Live" is `created − deleted` after each step.
203
+
204
+ | path | before this release | with this release |
205
+ |---|---|---|
206
+ | `addModel` → `remove()` → `addModel` on the same canvas, 10–12 times over 4 models | live 0 programs / 0 shaders after every `remove()` (60 created, 60 deleted) | same: nothing leaks |
207
+ | glTF swapped under `handle.engine.root`, page destroys the entity and unloads the container (rule 1 without the programs), 24 distinct models | programs 5 → 47, shaders 10 → 91; `deleteProgram` never called | same: the page never destroys a program, so nothing the SDK does can free it. `?dxrdiag` warns at the 10th swap |
208
+ | the same swaps with `handle.releaseEntity(entity, { asset })` | n/a | programs between 5 and 16 (the model on screen), shaders exactly twice that; back to 5 / 10 after the last release |
209
+ | the same, `?dxrdiag=keepshaders` (rule 5 off) | n/a | programs bounded (5–16), shaders 10 → 91: the engine's compiled-shader cache alone |
210
+ | `setSource` × 12–16 on one splat (cut, crossfade, wavefront, flip in turn) | +6 programs over the first pass of the four transitions, then flat | same |
211
+ | `setVideo(src)` / `setVideo(null)` × 6 | +1 program on the first, then flat | same |
212
+
213
+ **The automatic program release (rule 1), measured.** DisplayXR Browser on macOS (Chromium 156,
214
+ Apple silicon), PlayCanvas 2.22.3, the hook of §4. A PlayCanvas `addModel` tile; each swap adds a
215
+ product with its own material options (a distinct program) under `handle.engine.root`, and
216
+ destroys the outgoing entity and its material: the engine's own teardown, no `releaseEntity`.
217
+
218
+ | 24 swaps | live programs (created) | live shaders |
219
+ |---|---|---|
220
+ | automatic release (default) | 2 (25 created, 23 deleted) | 4 |
221
+ | `?dxrdiag=keepprograms` (the engine as it ships) | 25 (25 created, 0 deleted) | 50 |
222
+
223
+ The same pattern in a production kiosk app (one persistent PlayCanvas stage, content swapped per
224
+ screen, entities, materials and textures destroyed, no program ever released) went from 0 to 170
225
+ live programs over 28 screen changes before this release.
226
+
227
+ So the SDK's own swaps (`setSource`, `setVideo`, `prepareSource().dispose()`, `remove()`) release
228
+ what they replace, and content a page adds under `handle.engine.root` is the page's, with
229
+ `releaseEntity` to do it. On the production page, releasing the outgoing product's programs and
230
+ container plus the shader-cache fix took the GPU process from **+9.1 MB/min to flat** over a soak,
231
+ with live programs and shaders steady and every product still rendering when it came round again.
232
+
233
+ ## 6. What the SDK does for you, and what the page still owns
234
+
235
+ | | SDK | page |
236
+ |---|---|---|
237
+ | `handle.remove()` (PlayCanvas `./splat`, `./model`) | destroys the app: every entity, asset, program, shader and texture on the tile | nothing |
238
+ | `handle.remove()` (three.js `./model`) | disposes the model's geometries, materials and textures | anything the page added to the scene |
239
+ | `setSource` | the outgoing asset (entity destroyed, asset unloaded a few frames later); its own transition programs are compiled once and reused | nothing |
240
+ | `setVideo(null)`, `prepareSource().dispose()` | the plane / the prepared asset | nothing |
241
+ | compiled shaders (rule 5) | on every PlayCanvas device the SDK creates | a PlayCanvas app the page creates itself |
242
+ | programs of a material the page destroys (rule 1) | on every PlayCanvas app the SDK creates, a frame after the destroy, when nothing else uses them | a PlayCanvas app the page creates itself (rules 2–3 by hand) |
243
+ | content under `handle.engine.root` | `handle.releaseEntity(entity, { asset })` when the page calls it | destroying the entity and destroying or unloading what it drew with (or calling `releaseEntity`) |
244
+ | `addScene` (three.js or raw WebGL) | nothing: the renderer is the page's | rule 6 |
245
+
246
+ **How the shader-cache fix rides along with the engine.** PlayCanvas is the page's own peer
247
+ dependency (an import map, a bundler, a CDN build), never a file this package ships, so the SDK
248
+ cannot patch it on disk. `js/inline3d-pc-shader-cache.js` replaces two methods on the engine's
249
+ `WebglShader` prototype at run time, once, through the first shader the device creates. It
250
+ installs only on the 2.22.x shape (an `_compileShaderSource(device, src, isVertexShader)` reading a
251
+ cache keyed by source, and a `destroy` that does not delete shaders), and otherwise steps aside and
252
+ reports why in `?dxrdiag` (`cache skipped: …`). On an engine bump: re-read `webgl-shader.js`; if it
253
+ still never deletes a compiled shader before device destroy, keep the fix and update its shape
254
+ check and `test/pc-shader-cache.test.mjs` (a copy of the 2.22.3 class shape). If upstream releases
255
+ shaders itself, the check makes the fix a no-op, and it can be deleted.
256
+
257
+ **How the program release rides along with the engine.** `js/inline3d-pc-auto-release.js` wraps
258
+ two methods on the engine's `Material` prototype, once: `getShaderVariant` (the one place a
259
+ material links a program) records the material weakly on its device, and `destroy` collects the
260
+ material's programs before the engine empties `variants`. Both act only on devices the SDK
261
+ created; a PlayCanvas app the page runs itself is untouched. At the app's next `frameend` (a
262
+ timer backs that up for a stopped app) it destroys each collected program that no mesh instance
263
+ in the app, no material asset in the registry and **no still-alive material** holds. The last
264
+ condition is what makes it safe: the library hands one `Shader` to every material with the same
265
+ key, and a material the page keeps off-scene to show again still has it in its `variants`.
266
+ `Shader.destroy()` also drops the program from the library, so content shown again recompiles on
267
+ its first draw (`handle.warmEntity` hides that). It installs only on the 2.22.x shape (a
268
+ `getShaderVariant` and a `destroy` that clears `variants`) and otherwise reports
269
+ `release skipped: …` in `?dxrdiag`. On an engine bump: re-read `material.js` and
270
+ `mesh-instance.js`'s `getShaderInstance`; keep `test/pc-auto-release.test.mjs` in step.
@@ -10,10 +10,11 @@
10
10
  // fails with NotReadableError ("Device in use"). That device is skipped silently — a call must
11
11
  // never fail because an optional camera is busy.
12
12
  // - Labels are empty until one getUserMedia has succeeded. Without them, permission is primed on
13
- // an environment-facing camera where there is one (a phone's front lens is the head tracker's,
14
- // and opening it while the tracker holds it can reset the whole camera stack), then the list
15
- // is read again and a stereo device is opened by id BEFORE the default. The default is the
16
- // fallback.
13
+ // the DEFAULT camera with no constraints: in the DisplayXR Browser the runtime's 3D camera is
14
+ // listed first, so the prime opens the pair itself and no phone lens is opened at all (a plain
15
+ // open of one lens of the pair, or its teardown, crashes some phones' camera HAL; any facing or
16
+ // size constraint lets the engine pick a lens instead). Then the list is read again and a
17
+ // stereo device is opened by id BEFORE falling back. The primed default is the fallback.
17
18
  //
18
19
  // RECTIFICATION is not done here (P2): it needs the camera's calibration (intrinsics/extrinsics
19
20
  // for the ACTIVE device), which comes from a plug-in or the runtime. P1 sends the raw pair with
@@ -104,6 +105,17 @@ export async function openCamera(want = 'auto', o = {}) {
104
105
  log('camera-skip', { label, error: err.name });
105
106
  };
106
107
  const stop = (r) => r && r.stream.getTracks().forEach((t) => t.stop());
108
+ // Ask an open camera for another mode without a second start; keep it as it is if it refuses.
109
+ const withMode = async (r, constraints) => {
110
+ try {
111
+ await r.track.applyConstraints(constraints);
112
+ const st = r.track.getSettings();
113
+ if (st.width > 0 && st.height > 0) return { ...r, width: st.width, height: st.height };
114
+ } catch {
115
+ /* keep the mode it opened in */
116
+ }
117
+ return r;
118
+ };
107
119
  // A track that declares itself a side-by-side pair IS one, whatever its aspect (a phone's portrait
108
120
  // eyes make a 1440x1280 pair). Shape is only the guess for an unknown device, e.g. a USB pair.
109
121
  const isPair = (r) => !!r && (!!r.stereo || looksSbs(r.width, r.height));
@@ -170,15 +182,23 @@ export async function openCamera(want = 'auto', o = {}) {
170
182
  };
171
183
  let mono = null;
172
184
  if (want !== 'mono') {
173
- const listed = await listCameras();
174
- const pair = await tryNamedPairs(listed);
185
+ let listed = await listCameras();
186
+ let pair = await tryNamedPairs(listed);
175
187
  if (pair) return result(pair, 'sbs');
176
- // No labels yet (no permission). Prime it on an environment-facing camera, so a phone does not
177
- // open its front lens (the head tracker's) just to learn the names; then open a pair by id.
188
+ // Labels visible but no stereo-named device: a runtime 3D camera can join the list late (after
189
+ // a runtime restart), so read it once more before falling back.
190
+ if (listed.some((d) => d.label) && !listed.some((d) => stereoLabelHint(d.label))) {
191
+ listed = await listCameras();
192
+ pair = await tryNamedPairs(listed);
193
+ if (pair) return result(pair, 'sbs');
194
+ }
195
+ // No labels yet (no permission). Prime it on the default camera with NO constraints: in the
196
+ // DisplayXR Browser that is the 3D camera (listed first), so no phone lens is opened; a facing
197
+ // or size constraint would let the engine pick a lens instead. Then open a pair by id.
178
198
  if (listed.length && listed.every((d) => !d.label)) {
179
199
  let primed = null;
180
200
  try {
181
- primed = await open({ ...MONO_CONSTRAINTS, facingMode: { ideal: 'environment' } });
201
+ primed = await open(true);
182
202
  } catch (err) {
183
203
  skip('default', err);
184
204
  }
@@ -188,10 +208,8 @@ export async function openCamera(want = 'auto', o = {}) {
188
208
  stop(primed);
189
209
  return result(named, 'sbs');
190
210
  }
191
- // A camera that faces the user (or does not say: a desktop webcam) is the fallback as is; a
192
- // rear one is not what a self view falls back to, so the default is opened below instead.
193
- if (primed && primed.track?.getSettings?.().facingMode !== 'environment') mono = primed;
194
- else stop(primed);
211
+ // The primed default is the fallback as is (one camera start); ask it for the usual mode.
212
+ if (primed) mono = await withMode(primed, MONO_CONSTRAINTS);
195
213
  }
196
214
  }
197
215
 
@@ -153,6 +153,9 @@ function addModelUnguarded(wall, canvas, src, opts = {}) {
153
153
  depthFactor: 1,
154
154
  // Before the backend lands: once it has, the backend's own (PlayCanvas); three has none (0).
155
155
  warmEntity: (entity, o) => out.ready.then(() => (out.warmEntity !== stubWarm && typeof out.warmEntity === 'function' ? out.warmEntity(entity, o) : 0)),
156
+ // PlayCanvas replaces this once it has loaded; before that (and on three) there is nothing of
157
+ // the engine's to release.
158
+ releaseEntity: () => ({ programs: 0, container: null }),
156
159
  };
157
160
  const stubWarm = out.warmEntity;
158
161
  out.firstWoven = new Promise((resolve) => {
@@ -40,6 +40,7 @@
40
40
  import { PlayCanvasSplatViewer, pageRigTRS, pageViewAxis, pageViewRig } from './inline3d-splat-playcanvas.js';
41
41
  import { resolveControls, normalizeCameraPose, now, MAX_DT_S, declareViewRig, attachModelStereo, attachComfortDepth, resolveDepthOption, framedSubjectBounds } from './inline3d-splat-shared.js';
42
42
  import { displayRig } from './inline3d-three.js';
43
+ import { resolveDiag, DiagRecorder, startDiagLoop, registerDiag, watchContent } from './inline3d-splat-diag.js';
43
44
  import {
44
45
  NEUTRAL_STUDIO,
45
46
  neutralStudioRadiance,
@@ -479,6 +480,11 @@ export function attachPlayCanvasModel(out, wall, canvas, src, opts, pending = []
479
480
  let handle = null;
480
481
  let removed = false;
481
482
  let lastPagePose = null;
483
+ // `?dxrdiag` (or `diag`): the splat handle's recorder, for what a model tile can show — frame
484
+ // cadence, and the live GL objects + content-swap leak watch (./inline3d-splat-diag.js).
485
+ const diagCfg = resolveDiag(opts.diag);
486
+ const diag = diagCfg.on ? new DiagRecorder({ switches: diagCfg.switches, label: 'model' }) : null;
487
+ let diagLoop = null;
482
488
 
483
489
  Object.assign(out, {
484
490
  backend: 'playcanvas',
@@ -514,6 +520,8 @@ export function attachPlayCanvasModel(out, wall, canvas, src, opts, pending = []
514
520
  },
515
521
  remove() {
516
522
  removed = true;
523
+ diagLoop?.remove();
524
+ diag?.dispose();
517
525
  depth.dispose();
518
526
  handle?.remove();
519
527
  viewer.dispose(); // app.destroy(): the model, its textures and any page entities go with it
@@ -528,6 +536,14 @@ export function attachPlayCanvasModel(out, wall, canvas, src, opts, pending = []
528
536
  await out.ready?.catch?.(() => null);
529
537
  return removed ? 0 : viewer.warmEntity(entity ?? out.model, o);
530
538
  },
539
+ /**
540
+ * Destroy `entity` (content a page added under handle.engine.root) and release the programs
541
+ * its materials linked, and its container `asset` when nothing else draws from it —
542
+ * PlayCanvasSplatViewer.releaseEntity. Returns { programs, container }.
543
+ */
544
+ releaseEntity(entity, o) {
545
+ return removed ? { programs: 0, container: null } : viewer.releaseEntity(entity, o);
546
+ },
531
547
  });
532
548
 
533
549
  // The per-eye buffer scale in force (the request times the device-limit clamp,
@@ -550,8 +566,15 @@ export function attachPlayCanvasModel(out, wall, canvas, src, opts, pending = []
550
566
  });
551
567
  if (pageMode) depth.ignore("controls:'page' (the page's camera rig)");
552
568
 
569
+ const onFrame = diag
570
+ ? (views, layer, frame) => {
571
+ const t = performance.now();
572
+ viewer.onFrame(views, layer, frame);
573
+ diag.frame(views, t);
574
+ }
575
+ : viewer.onFrame;
553
576
  if (wall && wall.supported) {
554
- handle = wall.addScene(canvas, viewer.onFrame, {
577
+ handle = wall.addScene(canvas, onFrame, {
555
578
  ...(pageMode
556
579
  ? { viewRig: pageViewRig({ verticalFovDeg: viewer.page.fov, convergence: 2, comfortDepth: ctl.comfortDepth }) }
557
580
  : { virtualDisplayHeight }),
@@ -575,6 +598,12 @@ export function attachPlayCanvasModel(out, wall, canvas, src, opts, pending = []
575
598
  delete out._resolveFirstWoven;
576
599
  }
577
600
 
601
+ if (diag) {
602
+ diagLoop = startDiagLoop(diag, { overlay: !diag.has('nooverlay') && !!handle });
603
+ if (diagLoop?.el) handle?.exclude?.(diagLoop.el);
604
+ registerDiag(diag);
605
+ }
606
+
578
607
  for (const [name, args] of pending) {
579
608
  if (name === 'remove') {
580
609
  out.remove();
@@ -637,8 +666,13 @@ export function attachPlayCanvasModel(out, wall, canvas, src, opts, pending = []
637
666
  }
638
667
  const wanted = PC_DECODER_KINDS.filter((k) => (k === 'meshopt' && meshoptDecoder) || (declared ? declared.has(PC_DECODERS[k].ext) : false));
639
668
 
640
- const app = await viewer.attachEngine(pc, { preserveDrawingBuffer, antialias, patchSplats: false });
669
+ // `?dxrdiag=keepshaders`: the engine's shader cache as it ships (A/B; see ./inline3d-pc-shader-cache.js).
670
+ const shaderCacheRelease = !diag?.has('keepshaders');
671
+ // `?dxrdiag=keepprograms`: destroyed materials keep their programs (A/B; ./inline3d-pc-auto-release.js).
672
+ const programRelease = !diag?.has('keepprograms');
673
+ const app = await viewer.attachEngine(pc, { preserveDrawingBuffer, antialias, patchSplats: false, shaderCacheRelease, programRelease });
641
674
  if (!app || removed) return out;
675
+ if (diag) watchContent(diag, app, viewer);
642
676
  out.engine = Object.freeze({ app, root: viewer.content, camera: viewer.eye || null });
643
677
 
644
678
  // Lighting before the model, so the first frame the model is in is lit.
@@ -0,0 +1,191 @@
1
+ // inline3d-pc-auto-release.js — when a page destroys a material on one of the SDK's PlayCanvas
2
+ // devices, release the programs it linked, without the page asking.
3
+ //
4
+ // Internal. PlayCanvasSplatViewer.attachEngine (./inline3d-splat-playcanvas.js) installs it on
5
+ // every app it creates, for ./splat (engine: 'playcanvas') and ./model. docs/content-lifecycle-rules.md
6
+ // § rule 1 has the why; `?dxrdiag=keepprograms` turns it off (A/B).
7
+ //
8
+ // WHY. `Material.destroy()` empties `material.variants` and destroys no program, and the engine's
9
+ // ProgramLibrary keeps every program it linked for the device's life. A page that swaps content
10
+ // under one live app (`entity.destroy()`, `asset.unload()`, which destroys the container's
11
+ // materials) therefore keeps every program it ever linked: measured 0 → 170 live programs over 28
12
+ // screen changes on a kiosk app, none deleted. `handle.releaseEntity` releases them on request;
13
+ // this does the same for every material the page destroys, so a page that follows the engine's
14
+ // own teardown is bounded too.
15
+ //
16
+ // HOW. Two methods on the engine's Material prototype, wrapped once (the prototype is shared by
17
+ // every app on the page, so both wrappers act only on devices registered here):
18
+ // - getShaderVariant: the one place a material links a program. Records the material (weakly)
19
+ // on its device, so a live material that holds a program keeps it (step 3);
20
+ // - destroy: BEFORE the engine empties `variants`, collects the programs the material and its
21
+ // mesh instances hold, and marks the material destroyed.
22
+ // The sweep runs once per frame that had a destroy, at the app's `frameend` (after render, so
23
+ // content added in the same frame has drawn and holds its programs), with a timer as backup for a
24
+ // stopped app. It destroys each collected program that nothing still uses:
25
+ // 1. no mesh instance under `app.root` or in a layer, and no material asset in the registry
26
+ // (the keep set of ./inline3d-pc-release.js);
27
+ // 2. still registered on the device (not already destroyed, e.g. by releaseEntity);
28
+ // 3. no material that linked it is still alive. ProgramLibrary hands one Shader to every material
29
+ // with the same key, and a material a page keeps off-scene to show again still has it in its
30
+ // `variants`; destroying it under that material would leave a dead program there.
31
+ // `Shader.destroy()` also takes the program out of the library, so a later ask for that variant
32
+ // compiles it again. Cost: content shown again after release recompiles on its first draw
33
+ // (`handle.warmEntity` hides that).
34
+
35
+ import { liveShaders } from './inline3d-pc-release.js';
36
+
37
+ const states = new WeakMap(); // device -> { app, materials:Set<WeakRef>, pending:Set, scheduled, stats }
38
+ let patchedProto = null;
39
+
40
+ const deref = (r) => (typeof r?.deref === 'function' ? r.deref() : r);
41
+ const weak = (x) => (typeof WeakRef === 'function' ? new WeakRef(x) : x);
42
+
43
+ function collect(material) {
44
+ const into = (s) => {
45
+ const st = s && states.get(s.device);
46
+ if (!st) return;
47
+ st.pending.add(s);
48
+ schedule(st);
49
+ };
50
+ material.variants?.forEach?.(into);
51
+ material.meshInstances?.forEach?.((mi) => mi?._shaderCache?.forEach?.((si) => into(si?.shader)));
52
+ }
53
+
54
+ /** Programs some still-alive material on `st`'s device has linked. Prunes dead entries. */
55
+ function heldByLiveMaterials(st) {
56
+ const held = new Set();
57
+ for (const r of st.materials) {
58
+ const m = deref(r);
59
+ if (!m || m.__dxrDestroyed) {
60
+ st.materials.delete(r);
61
+ continue;
62
+ }
63
+ m.variants?.forEach?.((s) => s && held.add(s));
64
+ }
65
+ return held;
66
+ }
67
+
68
+ function schedule(st) {
69
+ if (st.scheduled) return;
70
+ st.scheduled = true;
71
+ const run = (st.run ||= () => sweep(st));
72
+ try {
73
+ st.app.once?.('frameend', run);
74
+ } catch {
75
+ /* no event system: the timer below */
76
+ }
77
+ st.timer = setTimeout(run, 1000);
78
+ }
79
+
80
+ /** Destroy every collected program nothing still uses. Returns how many. Exported for tests. */
81
+ export function sweep(st) {
82
+ if (!st || !st.scheduled) return 0;
83
+ st.scheduled = false;
84
+ clearTimeout(st.timer);
85
+ if (st.run) st.app.off?.('frameend', st.run); // only ours: the page may listen to frameend too
86
+ if (!st.pending.size || st.off) {
87
+ st.pending.clear();
88
+ return 0;
89
+ }
90
+ let keep;
91
+ try {
92
+ keep = liveShaders(st.app);
93
+ } catch {
94
+ st.pending.clear(); // an app mid-teardown: it frees everything itself
95
+ return 0;
96
+ }
97
+ const held = heldByLiveMaterials(st);
98
+ let n = 0;
99
+ for (const s of st.pending) {
100
+ if (keep.has(s) || held.has(s) || typeof s.destroy !== 'function') continue;
101
+ const registered = s.device?.shaders;
102
+ if (Array.isArray(registered) && !registered.includes(s)) continue; // already destroyed
103
+ try {
104
+ s.destroy();
105
+ n++;
106
+ } catch {
107
+ /* a program the engine already tore down */
108
+ }
109
+ }
110
+ st.pending.clear();
111
+ st.stats.released += n;
112
+ st.stats.sweeps++;
113
+ st.onSweep?.(n);
114
+ return n;
115
+ }
116
+
117
+ /**
118
+ * The prototype has the shape this relies on (2.22.x): `getShaderVariant(params)` with
119
+ * `params.device`, and a `destroy()` that clears `variants`. Returns a reason string, or null.
120
+ */
121
+ export function materialShapeProblem(proto) {
122
+ if (!proto || typeof proto.getShaderVariant !== 'function' || typeof proto.destroy !== 'function') return 'no Material.getShaderVariant/destroy';
123
+ const d = Function.prototype.toString.call(proto.destroy);
124
+ if (!/variants/.test(d)) return 'Material.destroy no longer clears variants';
125
+ return null;
126
+ }
127
+
128
+ /** Wrap the engine's Material prototype once. Returns 'patched' | 'already' | 'skipped: …'. */
129
+ export function patchMaterialPrototype(proto) {
130
+ if (patchedProto === proto || proto?.__dxrAutoRelease) return 'already';
131
+ const problem = materialShapeProblem(proto);
132
+ if (problem) return `skipped: ${problem}`;
133
+ const getShaderVariant = proto.getShaderVariant;
134
+ const destroy = proto.destroy;
135
+ proto.getShaderVariant = function (params) {
136
+ const s = getShaderVariant.apply(this, arguments);
137
+ const st = states.get(params?.device ?? s?.device);
138
+ if (st && !this.__dxrTracked) {
139
+ this.__dxrTracked = true;
140
+ st.materials.add(weak(this));
141
+ }
142
+ return s;
143
+ };
144
+ proto.destroy = function () {
145
+ try {
146
+ collect(this);
147
+ } catch {
148
+ /* never let the release break the engine's own teardown */
149
+ }
150
+ this.__dxrDestroyed = true;
151
+ return destroy.apply(this, arguments);
152
+ };
153
+ proto.__dxrAutoRelease = true;
154
+ patchedProto = proto;
155
+ return 'patched';
156
+ }
157
+
158
+ /**
159
+ * Release the programs of destroyed materials on `app`'s device. `Material` is the engine's
160
+ * Material class (the one the page's own `import … from 'playcanvas'` also resolves to).
161
+ * The result is kept on `device.__dxrAutoRelease`.
162
+ */
163
+ export function installAutoProgramRelease(app, Material) {
164
+ const device = app?.graphicsDevice;
165
+ if (!device) return null;
166
+ if (states.has(device)) return device.__dxrAutoRelease;
167
+ const patched = patchMaterialPrototype(Material?.prototype);
168
+ const result = patched === 'already' ? 'patched' : patched; // one prototype for every app on the page
169
+ device.__dxrAutoRelease = result;
170
+ if (result.startsWith('skipped')) return result;
171
+ const st = { app, materials: new Set(), pending: new Set(), scheduled: false, timer: 0, off: false, stats: { released: 0, sweeps: 0 } };
172
+ states.set(device, st);
173
+ app.once?.('destroy', () => {
174
+ st.off = true;
175
+ clearTimeout(st.timer);
176
+ st.pending.clear();
177
+ st.materials.clear();
178
+ });
179
+ return result;
180
+ }
181
+
182
+ /** Programs released on `app`'s device so far (diagnostics), or null. */
183
+ export function autoReleaseStats(app) {
184
+ const st = app?.graphicsDevice && states.get(app.graphicsDevice);
185
+ return st ? { ...st.stats, pending: st.pending.size, materials: st.materials.size } : null;
186
+ }
187
+
188
+ /** The state for `app` (tests, and diag's per-sweep hook). */
189
+ export function autoReleaseState(app) {
190
+ return (app?.graphicsDevice && states.get(app.graphicsDevice)) || null;
191
+ }