@threenative/core 0.3.10 → 0.3.12

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 (158) hide show
  1. package/capabilities.json +1096 -26
  2. package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
  3. package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-CWuFRT7G.d.ts} +1 -1
  4. package/dist/chunk-2Z44XSI2.js +35 -0
  5. package/dist/chunk-5O73CDMR.js +1849 -0
  6. package/dist/chunk-6KLNISRJ.js +153 -0
  7. package/dist/chunk-7UUT3TY5.js +133 -0
  8. package/dist/chunk-CFGTPFUH.js +231 -0
  9. package/dist/chunk-DBELTTTM.js +615 -0
  10. package/dist/chunk-GMHATRPW.js +7701 -0
  11. package/dist/chunk-GXY7B3JG.js +46 -0
  12. package/dist/chunk-KIRUKTBQ.js +187 -0
  13. package/dist/chunk-N5LRP4BR.js +220 -0
  14. package/dist/chunk-OKXUCASC.js +2238 -0
  15. package/dist/chunk-QWJEMUUT.js +94 -0
  16. package/dist/chunk-VAZM36TV.js +497 -0
  17. package/dist/{game-DzMcitDO.d.ts → game-D7pAzZUY.d.ts} +15 -3
  18. package/dist/hot.d.ts +3 -3
  19. package/dist/hot.js +2 -46
  20. package/dist/index.d.ts +329 -302
  21. package/dist/index.js +10565 -22533
  22. package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-B6BMqS1r.d.ts} +33 -3
  23. package/dist/playtest.d.ts +3 -3
  24. package/dist/playtest.js +24 -75
  25. package/dist/react-css.js +1 -215
  26. package/dist/react.d.ts +2 -2
  27. package/dist/{renderer-rZm0hVqq.d.ts → renderer-DvFVoAgj.d.ts} +13 -4
  28. package/dist/spells/index.d.ts +643 -0
  29. package/dist/spells/index.js +1558 -0
  30. package/dist/spells/ultra/index.d.ts +1600 -0
  31. package/dist/spells/ultra/index.js +6804 -0
  32. package/dist/terrain-jobs-worker.js +1 -123
  33. package/dist/two-sided-foliage-W523J3UM.js +1 -0
  34. package/dist/ui-layer.js +5 -384
  35. package/dist/worker.js +4 -41
  36. package/dist/world.d.ts +725 -259
  37. package/dist/world.js +16714 -18284
  38. package/mcp/animation-server.mjs +1 -1
  39. package/mcp/animation.mjs +2 -3
  40. package/mcp/engine-server.mjs +142 -47
  41. package/mcp/servers.mjs +9 -2
  42. package/mcp/terrain.mjs +8 -0
  43. package/package.json +14 -6
  44. package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
  45. package/patches/three@0.187.0.patch +2004 -407
  46. package/src/assets.ts +9 -2
  47. package/src/card-lod.ts +152 -0
  48. package/src/compressed-texture-preparation.ts +1008 -0
  49. package/src/frame-budget.ts +31 -20
  50. package/src/game-dev-tools.ts +93 -0
  51. package/src/game-frame-loop.ts +995 -0
  52. package/src/game-renderer-metrics.ts +77 -0
  53. package/src/game-startup.ts +1001 -0
  54. package/src/game-validate.ts +70 -0
  55. package/src/game.ts +253 -1935
  56. package/src/index.ts +27 -2
  57. package/src/launch-diagnostics.ts +22 -12
  58. package/src/look/ao-admission.ts +24 -0
  59. package/src/look/filmic-tone-mapping.ts +15 -13
  60. package/src/look/look-diagnostics.ts +3 -3
  61. package/src/look/quality.ts +10 -5
  62. package/src/look/world-environment.ts +76 -9
  63. package/src/loop.ts +29 -0
  64. package/src/model-lod.ts +42 -3
  65. package/src/particle-kinds.ts +515 -0
  66. package/src/picking.ts +17 -3
  67. package/src/playtest.ts +27 -1
  68. package/src/projection-apply.ts +4 -1
  69. package/src/projection-plan.ts +9 -6
  70. package/src/render/mesh-pool.ts +1 -1
  71. package/src/render/resolution-subrect.ts +2 -0
  72. package/src/render/rough-diffuse.ts +46 -12
  73. package/src/render/soft-shadow.ts +27 -12
  74. package/src/render/virtual-shadow-adaptive.ts +55 -0
  75. package/src/render/virtual-shadow-casters.ts +175 -0
  76. package/src/render/virtual-shadow-layers.ts +76 -0
  77. package/src/render/virtual-shadow-level.ts +224 -0
  78. package/src/render/virtual-shadow-markers.ts +193 -0
  79. package/src/render/virtual-shadow-options.ts +370 -0
  80. package/src/render/virtual-shadow-pages.ts +5 -0
  81. package/src/render/virtual-shadow.ts +213 -992
  82. package/src/render/world-impostor-surface.ts +293 -34
  83. package/src/render-camera-cull.ts +32 -1
  84. package/src/render-pass-budget.ts +34 -7
  85. package/src/renderProjection.ts +22 -0
  86. package/src/renderer.ts +92 -37
  87. package/src/snow-field.ts +1 -1
  88. package/src/spells/cast-pool.ts +361 -0
  89. package/src/spells/index.ts +177 -0
  90. package/src/spells/mechanisms.ts +295 -0
  91. package/src/spells/reaction-field.ts +415 -0
  92. package/src/spells/spell-lights.ts +139 -0
  93. package/src/spells/spell-world.ts +187 -0
  94. package/src/spells/spell.ts +397 -0
  95. package/src/spells/spells.ts +283 -0
  96. package/src/spells/ultra/engine/camera.ts +184 -0
  97. package/src/spells/ultra/engine/quality.ts +96 -0
  98. package/src/spells/ultra/index.ts +52 -0
  99. package/src/spells/ultra/main.ts +305 -0
  100. package/src/spells/ultra/scene/arenaMaterial.ts +325 -0
  101. package/src/spells/ultra/spells/cast.ts +233 -0
  102. package/src/spells/ultra/spells/fire.ts +1267 -0
  103. package/src/spells/ultra/vfx/FluidVolume.ts +816 -0
  104. package/src/spells/ultra/vfx/FluidVolume.tsl.ts +855 -0
  105. package/src/spells/ultra/vfx/FxTarget.ts +61 -0
  106. package/src/spells/ultra/vfx/GroundField.ts +1399 -0
  107. package/src/spells/ultra/vfx/Particles.ts +1475 -0
  108. package/src/spells/ultra/vfx/Ribbons.ts +645 -0
  109. package/src/spells/ultra/vfx/Shockwaves.ts +286 -0
  110. package/src/spells/ultra/vfx/Volumes.ts +489 -0
  111. package/src/spells/ultra/vfx/common.ts +1195 -0
  112. package/src/spells/ultra/vfx/noise.ts +441 -0
  113. package/src/startup-readiness.ts +128 -34
  114. package/src/version.ts +1 -1
  115. package/src/warmup.ts +56 -8
  116. package/src/water-surface.ts +9 -3
  117. package/src/world-bundle-safety.ts +401 -0
  118. package/src/world-cell-batches.ts +1216 -0
  119. package/src/world-cell-chunks.ts +1132 -0
  120. package/src/world-cell-data.ts +995 -0
  121. package/src/world-cell-impostors.ts +1449 -0
  122. package/src/world-cell-lod-bias.ts +151 -0
  123. package/src/world-cell-models.ts +1074 -0
  124. package/src/world-cell-options.ts +189 -0
  125. package/src/world-cell-placements.ts +694 -0
  126. package/src/world-cell-prewarm.ts +557 -0
  127. package/src/world-cell-readiness.ts +357 -0
  128. package/src/world-cell-render-bundles.ts +459 -0
  129. package/src/world-cell-render-gpu-dress.ts +714 -0
  130. package/src/world-cell-render-main-cull.ts +484 -0
  131. package/src/world-cell-render-shadow-claims.ts +198 -0
  132. package/src/world-cell-render-util.ts +246 -0
  133. package/src/world-cell-render.ts +1493 -0
  134. package/src/world-cell-residency.ts +745 -0
  135. package/src/world-cells.ts +1318 -10418
  136. package/src/world-chunk-shadow.ts +308 -0
  137. package/src/world-collar.ts +315 -0
  138. package/src/world-gpu-flags.ts +163 -0
  139. package/src/world-gpu-readback.ts +881 -0
  140. package/src/world-gpu-reference.ts +510 -0
  141. package/src/world-gpu-scene.ts +214 -2264
  142. package/src/world-gpu-shadow.ts +190 -0
  143. package/src/world-gpu-types.ts +381 -0
  144. package/src/world-gpu-validation.ts +518 -0
  145. package/src/world-heightfield.ts +802 -0
  146. package/src/world-load-limiter.ts +106 -0
  147. package/src/world-noise.ts +241 -0
  148. package/src/world-package.ts +42 -0
  149. package/src/world-region.ts +1 -1
  150. package/src/world-terrain-splat.ts +365 -149
  151. package/src/world-tile-budget.ts +256 -0
  152. package/src/world-tile-geometry.ts +469 -0
  153. package/src/world-tile-lod.ts +408 -0
  154. package/src/world-tile-observation.ts +566 -0
  155. package/src/world-tile-stitch.ts +166 -0
  156. package/src/world-tiles.ts +170 -1745
  157. package/src/world-water-bodies.ts +359 -0
  158. package/src/world.ts +35 -741
@@ -0,0 +1,2102 @@
1
+ diff --git a/build/three.tsl.js b/build/three.tsl.js
2
+ index f23dc1ee0ac72e328e9360ccd1ba2c9f6d9b2d56..b87cf38629b2c52804888a10bda3b814a1493316 100644
3
+ --- a/build/three.tsl.js
4
+ +++ b/build/three.tsl.js
5
+ @@ -24442,8 +24442,11 @@ const instance = /*@__PURE__*/ Fn( ( [ matrices, colors = null ], builder ) => {
6
+
7
+ const { previousInstanceMatrix } = _previousInstanceMatrices.get( object );
8
+
9
+ + if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] === scheduledPrevious ) return;
10
+ +
11
+ previousInstanceMatrix.array.set( scheduledPrevious );
12
+ previousInstanceMatrix.needsUpdate = true;
13
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = scheduledPrevious;
14
+
15
+ } );
16
+
17
+ @@ -24452,6 +24455,8 @@ const instance = /*@__PURE__*/ Fn( ( [ matrices, colors = null ], builder ) => {
18
+
19
+ if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatrices' ) ] !== undefined ) return;
20
+
21
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = undefined;
22
+ +
23
+ const { previousInstanceMatrix } = _previousInstanceMatrices.get( object );
24
+
25
+ previousInstanceMatrix.array.set( matrices.array );
26
+ @@ -24502,8 +24507,11 @@ const instancedMesh = /*@__PURE__*/ Fn( ( [ instancedMesh ], builder ) => {
27
+
28
+ const previous = getPreviousMatrix$1( object );
29
+
30
+ + if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] === scheduledPrevious ) return;
31
+ +
32
+ previous.array.set( scheduledPrevious );
33
+ previous.needsUpdate = true;
34
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = scheduledPrevious;
35
+
36
+ } );
37
+
38
+ @@ -24512,6 +24520,8 @@ const instancedMesh = /*@__PURE__*/ Fn( ( [ instancedMesh ], builder ) => {
39
+
40
+ if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatrices' ) ] !== undefined ) return;
41
+
42
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = undefined;
43
+ +
44
+ const previous = getPreviousMatrix$1( object );
45
+
46
+ previous.array.set( object.instanceMatrix.array );
47
+ @@ -26338,7 +26348,8 @@ class NodeMaterialObserver {
48
+ environmentRotation: scene.environmentRotation.clone(),
49
+ environmentIntensity: scene.environmentIntensity,
50
+ materialVersion: material.version,
51
+ - geometryVersion: geometry.version
52
+ + geometryVersion: geometry.version,
53
+ + pmremSwaps: renderObject.renderer._tnPmremSwaps
54
+ };
55
+
56
+ data.settledState = false;
57
+ @@ -26351,7 +26362,9 @@ class NodeMaterialObserver {
58
+ settled.environmentRotation.equals( scene.environmentRotation ) !== true ||
59
+ settled.environmentIntensity !== scene.environmentIntensity ||
60
+ settled.materialVersion !== material.version ||
61
+ - settled.geometryVersion !== geometry.version ) {
62
+ + settled.geometryVersion !== geometry.version ||
63
+ + // a PMREM finished and swapped the texture its node samples: a settled object would keep the old one
64
+ + settled.pmremSwaps !== renderObject.renderer._tnPmremSwaps ) {
65
+
66
+ // re-arm with the current state, so the object settles again once it stops changing
67
+
68
+ @@ -26360,6 +26373,7 @@ class NodeMaterialObserver {
69
+ settled.environmentIntensity = scene.environmentIntensity;
70
+ settled.materialVersion = material.version;
71
+ settled.geometryVersion = geometry.version;
72
+ + settled.pmremSwaps = renderObject.renderer._tnPmremSwaps;
73
+
74
+ data.settledState = false;
75
+
76
+ @@ -39401,6 +39415,9 @@ class PMREMNode extends Node {
77
+
78
+ this._pmrem = pmrem;
79
+
80
+ + // count the swap, so every settled object that samples this node refreshes once (NodeMaterialObserver)
81
+ + if ( this._texture.value !== pmrem && frame.renderer ) frame.renderer._tnPmremSwaps = ( frame.renderer._tnPmremSwaps || 0 ) + 1;
82
+ +
83
+ this.updateFromTexture( pmrem );
84
+
85
+ }
86
+ diff --git a/build/three.webgpu.js b/build/three.webgpu.js
87
+ index b72762aeb47f25485ce9b7f6beab3cf5d4d882ed..25db376c0930d86abd7e6fe7132869ee66b31118 100644
88
+ --- a/build/three.webgpu.js
89
+ +++ b/build/three.webgpu.js
90
+ @@ -6495,6 +6495,7 @@ class RenderObjects {
91
+ this.pipelines.delete( renderObject );
92
+ this.bindings.deleteForRender( renderObject );
93
+ this.nodes.delete( renderObject );
94
+ + this.geometries.releaseRenderObject( renderObject );
95
+
96
+ chainMap.delete( renderObject.getChainArray() );
97
+
98
+ @@ -6964,6 +6965,9 @@ class Geometries extends DataMap {
99
+ * @type {WeakMap<RenderObject,Map<BufferAttribute,number>>}
100
+ */
101
+ this._settledAttributes = new WeakMap();
102
+ + this._geometryAttributeOwners = new WeakMap();
103
+ + this._sharedAttributeOwners = new WeakMap();
104
+ + this._renderObjectAttributeGeometry = new WeakMap();
105
+
106
+ }
107
+
108
+ @@ -6989,6 +6993,7 @@ class Geometries extends DataMap {
109
+ const geometry = renderObject.geometry;
110
+
111
+ if ( this.has( geometry ) === false ) this.initGeometry( geometry );
112
+ + if ( this.geometryAttributesChanged( renderObject ) ) this.syncGeometryAttributes( renderObject );
113
+
114
+ // A settled static object has no attribute that could have changed since the last refresh,
115
+ // unless a writer bumped one. An InstancedMesh's instance buffer is a vertex attribute of
116
+ @@ -7016,13 +7021,18 @@ class Geometries extends DataMap {
117
+ const index = this.getIndex( renderObject );
118
+ const settled = this._settledAttributes.get( renderObject );
119
+
120
+ - if ( settled !== undefined && settled.size === attributes.length + ( index === null ? 0 : 1 ) ) {
121
+ + if ( settled !== undefined && settled.attributes.length === attributes.length && settled.index === index ) {
122
+
123
+ let changed = false;
124
+
125
+ - for ( const attribute of attributes ) {
126
+ + for ( let i = 0; i < attributes.length; i ++ ) {
127
+ +
128
+ + const attribute = attributes[ i ];
129
+ + const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
130
+ + const previous = settled.attributes[ i ];
131
+
132
+ - if ( settled.get( attribute.isInterleavedBufferAttribute ? attribute.data : attribute ) !== attribute.version ) {
133
+ + // an interleaved wrapper has no version of its own: its buffer's version is the write signal
134
+ + if ( previous.attribute !== attribute || previous.data !== data || previous.version !== data.version ) {
135
+
136
+ changed = true;
137
+ break;
138
+ @@ -7031,25 +7041,122 @@ class Geometries extends DataMap {
139
+
140
+ }
141
+
142
+ - if ( changed === false && ( index === null || settled.get( index ) === index.version ) ) return false;
143
+ + if ( changed === false && ( index === null || settled.indexVersion === index.version ) ) return false;
144
+ +
145
+ + }
146
+ +
147
+ + const versions = attributes.map( ( attribute ) => {
148
+ +
149
+ + const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
150
+ + return { attribute, data, version: data.version };
151
+ +
152
+ + } );
153
+ +
154
+ + this._settledAttributes.set( renderObject, { attributes: versions, index, indexVersion: index === null ? undefined : index.version } );
155
+ +
156
+ + return true;
157
+ +
158
+ + }
159
+ +
160
+ + /** Compare the published attribute identities without allocating on settled frames. */
161
+ + geometryAttributesChanged( renderObject ) {
162
+ +
163
+ + const geometry = renderObject.geometry;
164
+ + const owner = this._geometryAttributeOwners.get( geometry )?.get( renderObject );
165
+ + if ( owner === undefined || owner.index !== geometry.index ) return true;
166
+ +
167
+ + const attributes = renderObject.getAttributes();
168
+ + if ( owner.attributes.length !== attributes.length ) return true;
169
+ + for ( let i = 0; i < attributes.length; i ++ ) {
170
+ + const attribute = attributes[ i ];
171
+ + if ( owner.attributes[ i ] !== ( attribute.isInterleavedBufferAttribute ? attribute.data : attribute ) ) return true;
172
+ + }
173
+ +
174
+ + return false;
175
+ +
176
+ + }
177
+
178
+ + /** Add this render object's current geometry attributes to the renderer's shared-resource owners. */
179
+ + syncGeometryAttributes( renderObject ) {
180
+ +
181
+ + const { geometry } = renderObject;
182
+ + const previousGeometry = this._renderObjectAttributeGeometry.get( renderObject );
183
+ + if ( previousGeometry !== undefined && previousGeometry !== geometry ) this.releaseRenderObject( renderObject );
184
+ + this._renderObjectAttributeGeometry.set( renderObject, geometry );
185
+ + let geometryOwners = this._geometryAttributeOwners.get( geometry );
186
+ +
187
+ + if ( geometryOwners === undefined ) {
188
+ + geometryOwners = new Map();
189
+ + this._geometryAttributeOwners.set( geometry, geometryOwners );
190
+ }
191
+
192
+ - const versions = new Map();
193
+ + const previous = geometryOwners.get( renderObject )?.keys ?? new Map();
194
+ + const current = new Map();
195
+ + const attributes = renderObject.getAttributes();
196
+ + const attributeKeys = [];
197
+
198
+ for ( const attribute of attributes ) {
199
+ + const key = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
200
+ + current.set( key, true );
201
+ + attributeKeys.push( key );
202
+ + }
203
+ + const index = geometry.index;
204
+ + if ( index !== null ) current.set( index, true );
205
+
206
+ - const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
207
+ + for ( const key of current.keys() ) {
208
+ + let record = this._sharedAttributeOwners.get( key );
209
+ + if ( record === undefined ) record = { owners: new Set() };
210
+ + if ( previous.has( key ) === false ) record.owners.add( renderObject );
211
+ + this._sharedAttributeOwners.set( key, record );
212
+ + }
213
+
214
+ - versions.set( data, data.version );
215
+ + for ( const key of previous.keys() ) {
216
+ + if ( current.has( key ) === false ) this.releaseSharedAttribute( key, renderObject );
217
+ + }
218
+ +
219
+ + geometryOwners.set( renderObject, { keys: current, attributes: attributeKeys, index } );
220
+
221
+ + }
222
+ +
223
+ + /** Drop a render object from its geometry before its material-owned node attributes go stale. */
224
+ + releaseRenderObject( renderObject ) {
225
+ +
226
+ + const geometry = this._renderObjectAttributeGeometry.get( renderObject );
227
+ + if ( geometry === undefined ) return;
228
+ + const geometryOwners = this._geometryAttributeOwners.get( geometry );
229
+ + const owned = geometryOwners?.get( renderObject )?.keys;
230
+ + if ( owned !== undefined ) {
231
+ + for ( const key of owned.keys() ) this.releaseSharedAttribute( key, renderObject );
232
+ + geometryOwners.delete( renderObject );
233
+ + if ( geometryOwners.size === 0 ) this._geometryAttributeOwners.delete( geometry );
234
+ }
235
+ + this._renderObjectAttributeGeometry.delete( renderObject );
236
+ +
237
+ + }
238
+
239
+ - if ( index !== null ) versions.set( index, index.version );
240
+ + /** Release a shared GPU resource only after its last live geometry render object lets it go. */
241
+ + releaseSharedAttribute( key, owner ) {
242
+
243
+ - this._settledAttributes.set( renderObject, versions );
244
+ + const record = this._sharedAttributeOwners.get( key );
245
+ + if ( record === undefined ) return;
246
+ + record.owners.delete( owner );
247
+ + if ( record.owners.size !== 0 ) return;
248
+
249
+ - return true;
250
+ + this.attributes.delete( key );
251
+ + this._sharedAttributeOwners.delete( key );
252
+ +
253
+ + }
254
+ +
255
+ + /** Release the recorded sets, not a potentially replaced render object's current attributes. */
256
+ + releaseGeometryAttributes( geometry ) {
257
+ +
258
+ + const geometryOwners = this._geometryAttributeOwners.get( geometry );
259
+ + if ( geometryOwners === undefined ) return;
260
+ + for ( const [ owner, attributes ] of geometryOwners ) {
261
+ + for ( const key of attributes.keys.keys() ) this.releaseSharedAttribute( key, owner );
262
+ + this._renderObjectAttributeGeometry.delete( owner );
263
+ + }
264
+ + this._geometryAttributeOwners.delete( geometry );
265
+
266
+ }
267
+
268
+ @@ -7070,23 +7177,10 @@ class Geometries extends DataMap {
269
+
270
+ this.info.memory.geometries --;
271
+
272
+ - // index
273
+ -
274
+ - const index = geometry.index;
275
+ + // index and geometry attributes: release this geometry's owners, so a buffer another
276
+ + // geometry still draws stays alive until its last owner lets it go
277
+
278
+ - if ( index !== null ) {
279
+ -
280
+ - this.attributes.delete( index );
281
+ -
282
+ - }
283
+ -
284
+ - // geometry attributes
285
+ -
286
+ - for ( const attribute of Object.values( geometry.attributes ) ) {
287
+ -
288
+ - this.attributes.delete( this.backend.getBufferAttribute( attribute ) );
289
+ -
290
+ - }
291
+ + this.releaseGeometryAttributes( geometry );
292
+
293
+ // wireframe attributes
294
+
295
+ @@ -7264,17 +7358,9 @@ class Geometries extends DataMap {
296
+ */
297
+ deleteNodeAttributes( renderObject ) {
298
+
299
+ - const currentAttributes = new Set( Object.values( renderObject.geometry.attributes ) );
300
+ -
301
+ - for ( const attribute of renderObject.getAttributes() ) {
302
+ + // node attributes can be shared with other render objects: release this object's owners
303
+
304
+ - if ( currentAttributes.has( attribute ) === false ) {
305
+ -
306
+ - this.attributes.delete( this.backend.getBufferAttribute( attribute ) );
307
+ -
308
+ - }
309
+ -
310
+ - }
311
+ + this.releaseRenderObject( renderObject );
312
+
313
+ }
314
+
315
+ @@ -8919,6 +9005,21 @@ class Bindings extends DataMap {
316
+
317
+ if ( groupData.usedTimes === 0 ) {
318
+
319
+ + // Stop disposed bind groups from being invalidated by later texture disposal:
320
+ + // untrack every texture the group was ever tracked on, not only its current one.
321
+ + const bindingData = this.backend.get( bindGroup );
322
+ + if ( bindingData.trackedTextures ) {
323
+ +
324
+ + for ( const textureData of bindingData.trackedTextures ) {
325
+ +
326
+ + textureData.bindGroups.delete( bindGroup );
327
+ +
328
+ + }
329
+ +
330
+ + bindingData.trackedTextures.clear();
331
+ +
332
+ + }
333
+ +
334
+ for ( const binding of bindGroup.bindings ) {
335
+
336
+ if ( binding.isUniformBuffer ) {
337
+ @@ -9009,7 +9110,11 @@ class Bindings extends DataMap {
338
+ // a settled object keeps the values it uploaded the last time it refreshed, so an object
339
+ // group it has already written is the one thing in here it may skip
340
+
341
+ - let needsBindingsUpdate = false;
342
+ + // a destroyed texture dropped this group's cached GPU groups: rebuild it even if no binding reports a change
343
+ + const bindingsData = backend.get( bindGroup );
344
+ + const forceBindingsUpdate = bindingsData.forceUpdate === true;
345
+ +
346
+ + let needsBindingsUpdate = forceBindingsUpdate;
347
+ let cacheBindings = true;
348
+ let cacheKeyLength = 0;
349
+ let version = 0;
350
+ @@ -9018,7 +9123,7 @@ class Bindings extends DataMap {
351
+
352
+ for ( const binding of bindGroup.bindings ) {
353
+
354
+ - const updatedGroup = this.nodes.updateGroup( binding, settled );
355
+ + const updatedGroup = forceBindingsUpdate || this.nodes.updateGroup( binding, settled );
356
+
357
+ // every uniforms group is a uniform buffer. So if no update is required,
358
+ // we move one with the next binding. Otherwise the next if block will update the group.
359
+ @@ -9111,12 +9216,20 @@ class Bindings extends DataMap {
360
+
361
+ }
362
+
363
+ - // keep track which bind groups refer to the current texture (this is needed for dispose)
364
+ + }
365
+ +
366
+ + // keep track which bind groups refer to the current texture (this is needed for dispose),
367
+ + // also when the binding's first update reported no change: a destroy must reach this group
368
+
369
+ - texturesTextureData.bindGroups.add( bindGroup );
370
+ + texturesTextureData.bindGroups.add( bindGroup );
371
+ + if ( bindingsData.trackedTextures === undefined ) {
372
+ +
373
+ + bindingsData.trackedTextures = new Set();
374
+
375
+ }
376
+
377
+ + bindingsData.trackedTextures.add( texturesTextureData );
378
+ +
379
+ const textureData = backend.get( texture );
380
+
381
+ if ( textureData.externalTexture !== undefined || texturesTextureData.isDefaultTexture ) {
382
+ @@ -9183,6 +9296,7 @@ class Bindings extends DataMap {
383
+ const cacheKey = cacheBindings ? _cacheKeyIds.join( ',' ) : '';
384
+
385
+ this.backend.updateBindings( bindGroup, bindings, cacheKey, version );
386
+ + bindingsData.forceUpdate = false;
387
+
388
+ }
389
+
390
+ @@ -10517,6 +10631,7 @@ class Textures extends DataMap {
391
+
392
+ bindingsData.groups = undefined;
393
+ bindingsData.versions = undefined;
394
+ + bindingsData.forceUpdate = true;
395
+
396
+ }
397
+
398
+ @@ -11087,6 +11202,7 @@ class Textures extends DataMap {
399
+
400
+ bindingsData.groups = undefined;
401
+ bindingsData.versions = undefined;
402
+ + bindingsData.forceUpdate = true;
403
+
404
+ // go through all bindings and if one points to the destroyed texture, trigger dispose as well
405
+
406
+ @@ -43674,6 +43790,13 @@ class WGSLNodeBuilder extends NodeBuilder {
407
+
408
+ super( object, renderer, new WGSLNodeParser() );
409
+
410
+ + /**
411
+ + * Comparison samplers shared by identical keys, and the WGSL sampler name of each texture
412
+ + * that reads through another texture's sampler.
413
+ + */
414
+ + this.sharedCompareSamplers = new Map();
415
+ + this.samplerNames = new Map();
416
+ +
417
+ /**
418
+ * A dictionary that holds for each shader stage ('vertex', 'fragment', 'compute')
419
+ * another dictionary which manages UBOs per group ('render','frame','object').
420
+ @@ -43750,21 +43873,21 @@ class WGSLNodeBuilder extends NodeBuilder {
421
+
422
+ if ( offsetSnippet ) {
423
+
424
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
425
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
426
+
427
+ }
428
+
429
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet } )`;
430
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet } )`;
431
+
432
+ } else {
433
+
434
+ if ( offsetSnippet ) {
435
+
436
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ offsetSnippet } )`;
437
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ offsetSnippet } )`;
438
+
439
+ }
440
+
441
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet } )`;
442
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet } )`;
443
+
444
+ }
445
+
446
+ @@ -43796,21 +43919,21 @@ class WGSLNodeBuilder extends NodeBuilder {
447
+
448
+ if ( offsetSnippet ) {
449
+
450
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
451
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
452
+
453
+ }
454
+
455
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
456
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
457
+
458
+ } else {
459
+
460
+ if ( offsetSnippet ) {
461
+
462
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
463
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
464
+
465
+ }
466
+
467
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet } )`;
468
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet } )`;
469
+
470
+ }
471
+
472
+ @@ -44073,7 +44196,7 @@ class WGSLNodeBuilder extends NodeBuilder {
473
+ // Depth textures require integer level, regular textures use float
474
+ const levelType = texture.isDepthTexture ? 'u32' : 'f32';
475
+
476
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelType }( ${ levelSnippet } ) )`;
477
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelType }( ${ levelSnippet } ) )`;
478
+
479
+ }
480
+
481
+ @@ -44217,6 +44340,18 @@ class WGSLNodeBuilder extends NodeBuilder {
482
+ * @param {Texture} texture - The texture.
483
+ * @return {boolean} Whether the sampled values of the given texture should be compared against a reference value or not.
484
+ */
485
+ + /**
486
+ + * The WGSL sampler a texture reads through: its own, or a shared comparison sampler.
487
+ + *
488
+ + * @param {string} textureProperty - The texture's WGSL name.
489
+ + * @return {string} The sampler's WGSL name.
490
+ + */
491
+ + getSamplerName( textureProperty ) {
492
+ +
493
+ + return this.samplerNames.get( textureProperty ) ?? `${ textureProperty }_sampler`;
494
+ +
495
+ + }
496
+ +
497
+ isSampleCompare( texture ) {
498
+
499
+ return texture.isDepthTexture === true && texture.compareFunction !== null && this.renderer.hasCompatibility( Compatibility.TEXTURE_COMPARE );
500
+ @@ -44293,21 +44428,21 @@ class WGSLNodeBuilder extends NodeBuilder {
501
+
502
+ if ( offsetSnippet ) {
503
+
504
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
505
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
506
+
507
+ }
508
+
509
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
510
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
511
+
512
+ } else {
513
+
514
+ if ( offsetSnippet ) {
515
+
516
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
517
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
518
+
519
+ }
520
+
521
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
522
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
523
+
524
+ }
525
+
526
+ @@ -44340,21 +44475,21 @@ class WGSLNodeBuilder extends NodeBuilder {
527
+
528
+ if ( offsetSnippet ) {
529
+
530
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
531
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
532
+
533
+ }
534
+
535
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet } )`;
536
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet } )`;
537
+
538
+ }
539
+
540
+ if ( offsetSnippet ) {
541
+
542
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
543
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
544
+
545
+ }
546
+
547
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet } )`;
548
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet } )`;
549
+
550
+ } else {
551
+
552
+ @@ -44419,21 +44554,21 @@ class WGSLNodeBuilder extends NodeBuilder {
553
+
554
+ if ( offsetSnippet ) {
555
+
556
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
557
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
558
+
559
+ }
560
+
561
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet } )`;
562
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet } )`;
563
+
564
+ }
565
+
566
+ if ( offsetSnippet ) {
567
+
568
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ offsetSnippet } )`;
569
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ offsetSnippet } )`;
570
+
571
+ }
572
+
573
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet })`;
574
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet })`;
575
+
576
+ }
577
+
578
+ @@ -44454,21 +44589,21 @@ class WGSLNodeBuilder extends NodeBuilder {
579
+
580
+ if ( offsetSnippet ) {
581
+
582
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
583
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
584
+
585
+ }
586
+
587
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet })`;
588
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet })`;
589
+
590
+ }
591
+
592
+ if ( offsetSnippet ) {
593
+
594
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
595
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
596
+
597
+ }
598
+
599
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet })`;
600
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet })`;
601
+
602
+ }
603
+
604
+ @@ -44491,21 +44626,21 @@ class WGSLNodeBuilder extends NodeBuilder {
605
+
606
+ if ( offsetSnippet ) {
607
+
608
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
609
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
610
+
611
+ }
612
+
613
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
614
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
615
+
616
+ } else {
617
+
618
+ if ( offsetSnippet ) {
619
+
620
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
621
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
622
+
623
+ }
624
+
625
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet } )`;
626
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet } )`;
627
+
628
+ }
629
+
630
+ @@ -44541,21 +44676,21 @@ class WGSLNodeBuilder extends NodeBuilder {
631
+
632
+ if ( offsetSnippet ) {
633
+
634
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
635
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
636
+
637
+ }
638
+
639
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet } )`;
640
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet } )`;
641
+
642
+ } else {
643
+
644
+ if ( offsetSnippet ) {
645
+
646
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
647
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
648
+
649
+ }
650
+
651
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ biasSnippet } )`;
652
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ biasSnippet } )`;
653
+
654
+ }
655
+
656
+ @@ -44799,11 +44934,27 @@ class WGSLNodeBuilder extends NodeBuilder {
657
+ // Also textureGather always need sampler.
658
+ const needsSampler = node.isRawTextureNode !== true && ( node.value.isCubeTexture === true || ( this.isUnfilterable( node.value ) === false && texture.store === false ) || node.gatherNode !== null );
659
+
660
+ - if ( needsSampler ) {
661
+ + // Identical comparison samplers share one binding. Every shadow map of a clipmap compares the
662
+ + // same way, and a sampler per map exhausts maxSamplersPerShaderStage (16 on most adapters).
663
+ + const sharedKey = needsSampler && this.isSampleCompare( node.value ) && node.isSampleCompare() ? `${ groupName }:${ shaderStage }:${ node.value.compareFunction }:${ node.value.wrapS }:${ node.value.wrapT }:${ node.value.wrapR }:${ node.value.minFilter }:${ node.value.magFilter }` : null;
664
+ + const sharedSampler = sharedKey === null ? undefined : this.sharedCompareSamplers.get( sharedKey );
665
+ +
666
+ + if ( sharedSampler !== undefined ) {
667
+ +
668
+ + this.samplerNames.set( uniformNode.name, sharedSampler.name );
669
+ + nodeData.sharedSampler = true;
670
+ +
671
+ + bindings.push( texture );
672
+ +
673
+ + uniformGPU = [ sharedSampler, texture ];
674
+ +
675
+ + } else if ( needsSampler ) {
676
+
677
+ const sampler = new NodeSampler( `${ uniformNode.name }_sampler`, uniformNode.node, group );
678
+ sampler.setVisibility( gpuShaderStageLib[ shaderStage ] );
679
+
680
+ + if ( sharedKey !== null ) this.sharedCompareSamplers.set( sharedKey, sampler );
681
+ +
682
+ bindings.push( sampler, texture );
683
+
684
+ uniformGPU = [ sampler, texture ];
685
+ @@ -45615,7 +45766,8 @@ ${ flowData.code }
686
+ const uniformGPU = nodeData.uniformGPU;
687
+ const needsSampler = Array.isArray( uniformGPU ) && uniformGPU[ 0 ].isSampler === true && uniformGPU[ 0 ].isSampledTexture !== true;
688
+
689
+ - if ( needsSampler ) {
690
+ + // A shared comparison sampler is declared once, by the texture that created it.
691
+ + if ( needsSampler && nodeData.sharedSampler !== true ) {
692
+
693
+ if ( this.isSampleCompare( texture ) && textureNode.isSampleCompare() ) {
694
+
695
+ diff --git a/build/three.webgpu.nodes.js b/build/three.webgpu.nodes.js
696
+ index 5d1c3e9b32d4347ac6605fdf59438fd634d40cd7..790d97c1247564c7fd3aea2ee54ea0a6ffa50c62 100644
697
+ --- a/build/three.webgpu.nodes.js
698
+ +++ b/build/three.webgpu.nodes.js
699
+ @@ -413,7 +413,8 @@ class NodeMaterialObserver {
700
+ environmentRotation: scene.environmentRotation.clone(),
701
+ environmentIntensity: scene.environmentIntensity,
702
+ materialVersion: material.version,
703
+ - geometryVersion: geometry.version
704
+ + geometryVersion: geometry.version,
705
+ + pmremSwaps: renderObject.renderer._tnPmremSwaps
706
+ };
707
+
708
+ data.settledState = false;
709
+ @@ -426,7 +427,9 @@ class NodeMaterialObserver {
710
+ settled.environmentRotation.equals( scene.environmentRotation ) !== true ||
711
+ settled.environmentIntensity !== scene.environmentIntensity ||
712
+ settled.materialVersion !== material.version ||
713
+ - settled.geometryVersion !== geometry.version ) {
714
+ + settled.geometryVersion !== geometry.version ||
715
+ + // a PMREM finished and swapped the texture its node samples: a settled object would keep the old one
716
+ + settled.pmremSwaps !== renderObject.renderer._tnPmremSwaps ) {
717
+
718
+ // re-arm with the current state, so the object settles again once it stops changing
719
+
720
+ @@ -435,6 +438,7 @@ class NodeMaterialObserver {
721
+ settled.environmentIntensity = scene.environmentIntensity;
722
+ settled.materialVersion = material.version;
723
+ settled.geometryVersion = geometry.version;
724
+ + settled.pmremSwaps = renderObject.renderer._tnPmremSwaps;
725
+
726
+ data.settledState = false;
727
+
728
+ @@ -20355,8 +20359,11 @@ const instance = /*@__PURE__*/ Fn( ( [ matrices, colors = null ], builder ) => {
729
+
730
+ const { previousInstanceMatrix } = _previousInstanceMatrices.get( object );
731
+
732
+ + if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] === scheduledPrevious ) return;
733
+ +
734
+ previousInstanceMatrix.array.set( scheduledPrevious );
735
+ previousInstanceMatrix.needsUpdate = true;
736
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = scheduledPrevious;
737
+
738
+ } );
739
+
740
+ @@ -20365,6 +20372,8 @@ const instance = /*@__PURE__*/ Fn( ( [ matrices, colors = null ], builder ) => {
741
+
742
+ if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatrices' ) ] !== undefined ) return;
743
+
744
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = undefined;
745
+ +
746
+ const { previousInstanceMatrix } = _previousInstanceMatrices.get( object );
747
+
748
+ previousInstanceMatrix.array.set( matrices.array );
749
+ @@ -20415,8 +20424,11 @@ const instancedMesh = /*@__PURE__*/ Fn( ( [ instancedMesh ], builder ) => {
750
+
751
+ const previous = getPreviousMatrix$1( object );
752
+
753
+ + if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] === scheduledPrevious ) return;
754
+ +
755
+ previous.array.set( scheduledPrevious );
756
+ previous.needsUpdate = true;
757
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = scheduledPrevious;
758
+
759
+ } );
760
+
761
+ @@ -20425,6 +20437,8 @@ const instancedMesh = /*@__PURE__*/ Fn( ( [ instancedMesh ], builder ) => {
762
+
763
+ if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatrices' ) ] !== undefined ) return;
764
+
765
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = undefined;
766
+ +
767
+ const previous = getPreviousMatrix$1( object );
768
+
769
+ previous.array.set( object.instanceMatrix.array );
770
+ @@ -29633,6 +29647,9 @@ class PMREMNode extends Node {
771
+
772
+ this._pmrem = pmrem;
773
+
774
+ + // count the swap, so every settled object that samples this node refreshes once (NodeMaterialObserver)
775
+ + if ( this._texture.value !== pmrem && frame.renderer ) frame.renderer._tnPmremSwaps = ( frame.renderer._tnPmremSwaps || 0 ) + 1;
776
+ +
777
+ this.updateFromTexture( pmrem );
778
+
779
+ }
780
+ @@ -33621,6 +33638,7 @@ class RenderObjects {
781
+ this.pipelines.delete( renderObject );
782
+ this.bindings.deleteForRender( renderObject );
783
+ this.nodes.delete( renderObject );
784
+ + this.geometries.releaseRenderObject( renderObject );
785
+
786
+ chainMap.delete( renderObject.getChainArray() );
787
+
788
+ @@ -34090,6 +34108,9 @@ class Geometries extends DataMap {
789
+ * @type {WeakMap<RenderObject,Map<BufferAttribute,number>>}
790
+ */
791
+ this._settledAttributes = new WeakMap();
792
+ + this._geometryAttributeOwners = new WeakMap();
793
+ + this._sharedAttributeOwners = new WeakMap();
794
+ + this._renderObjectAttributeGeometry = new WeakMap();
795
+
796
+ }
797
+
798
+ @@ -34115,6 +34136,7 @@ class Geometries extends DataMap {
799
+ const geometry = renderObject.geometry;
800
+
801
+ if ( this.has( geometry ) === false ) this.initGeometry( geometry );
802
+ + if ( this.geometryAttributesChanged( renderObject ) ) this.syncGeometryAttributes( renderObject );
803
+
804
+ // A settled static object has no attribute that could have changed since the last refresh,
805
+ // unless a writer bumped one. An InstancedMesh's instance buffer is a vertex attribute of
806
+ @@ -34142,13 +34164,18 @@ class Geometries extends DataMap {
807
+ const index = this.getIndex( renderObject );
808
+ const settled = this._settledAttributes.get( renderObject );
809
+
810
+ - if ( settled !== undefined && settled.size === attributes.length + ( index === null ? 0 : 1 ) ) {
811
+ + if ( settled !== undefined && settled.attributes.length === attributes.length && settled.index === index ) {
812
+
813
+ let changed = false;
814
+
815
+ - for ( const attribute of attributes ) {
816
+ + for ( let i = 0; i < attributes.length; i ++ ) {
817
+ +
818
+ + const attribute = attributes[ i ];
819
+ + const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
820
+ + const previous = settled.attributes[ i ];
821
+
822
+ - if ( settled.get( attribute.isInterleavedBufferAttribute ? attribute.data : attribute ) !== attribute.version ) {
823
+ + // an interleaved wrapper has no version of its own: its buffer's version is the write signal
824
+ + if ( previous.attribute !== attribute || previous.data !== data || previous.version !== data.version ) {
825
+
826
+ changed = true;
827
+ break;
828
+ @@ -34157,25 +34184,122 @@ class Geometries extends DataMap {
829
+
830
+ }
831
+
832
+ - if ( changed === false && ( index === null || settled.get( index ) === index.version ) ) return false;
833
+ + if ( changed === false && ( index === null || settled.indexVersion === index.version ) ) return false;
834
+
835
+ }
836
+
837
+ - const versions = new Map();
838
+ + const versions = attributes.map( ( attribute ) => {
839
+ +
840
+ + const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
841
+ + return { attribute, data, version: data.version };
842
+ +
843
+ + } );
844
+ +
845
+ + this._settledAttributes.set( renderObject, { attributes: versions, index, indexVersion: index === null ? undefined : index.version } );
846
+ +
847
+ + return true;
848
+ +
849
+ + }
850
+ +
851
+ + /** Compare the published attribute identities without allocating on settled frames. */
852
+ + geometryAttributesChanged( renderObject ) {
853
+ +
854
+ + const geometry = renderObject.geometry;
855
+ + const owner = this._geometryAttributeOwners.get( geometry )?.get( renderObject );
856
+ + if ( owner === undefined || owner.index !== geometry.index ) return true;
857
+ +
858
+ + const attributes = renderObject.getAttributes();
859
+ + if ( owner.attributes.length !== attributes.length ) return true;
860
+ + for ( let i = 0; i < attributes.length; i ++ ) {
861
+ + const attribute = attributes[ i ];
862
+ + if ( owner.attributes[ i ] !== ( attribute.isInterleavedBufferAttribute ? attribute.data : attribute ) ) return true;
863
+ + }
864
+ +
865
+ + return false;
866
+ +
867
+ + }
868
+ +
869
+ + /** Add this render object's current geometry attributes to the renderer's shared-resource owners. */
870
+ + syncGeometryAttributes( renderObject ) {
871
+ +
872
+ + const { geometry } = renderObject;
873
+ + const previousGeometry = this._renderObjectAttributeGeometry.get( renderObject );
874
+ + if ( previousGeometry !== undefined && previousGeometry !== geometry ) this.releaseRenderObject( renderObject );
875
+ + this._renderObjectAttributeGeometry.set( renderObject, geometry );
876
+ + let geometryOwners = this._geometryAttributeOwners.get( geometry );
877
+ +
878
+ + if ( geometryOwners === undefined ) {
879
+ + geometryOwners = new Map();
880
+ + this._geometryAttributeOwners.set( geometry, geometryOwners );
881
+ + }
882
+ +
883
+ + const previous = geometryOwners.get( renderObject )?.keys ?? new Map();
884
+ + const current = new Map();
885
+ + const attributes = renderObject.getAttributes();
886
+ + const attributeKeys = [];
887
+
888
+ for ( const attribute of attributes ) {
889
+ + const key = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
890
+ + current.set( key, true );
891
+ + attributeKeys.push( key );
892
+ + }
893
+ + const index = geometry.index;
894
+ + if ( index !== null ) current.set( index, true );
895
+
896
+ - const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
897
+ + for ( const key of current.keys() ) {
898
+ + let record = this._sharedAttributeOwners.get( key );
899
+ + if ( record === undefined ) record = { owners: new Set() };
900
+ + if ( previous.has( key ) === false ) record.owners.add( renderObject );
901
+ + this._sharedAttributeOwners.set( key, record );
902
+ + }
903
+
904
+ - versions.set( data, data.version );
905
+ + for ( const key of previous.keys() ) {
906
+ + if ( current.has( key ) === false ) this.releaseSharedAttribute( key, renderObject );
907
+ + }
908
+ +
909
+ + geometryOwners.set( renderObject, { keys: current, attributes: attributeKeys, index } );
910
+ +
911
+ + }
912
+ +
913
+ + /** Drop a render object from its geometry before its material-owned node attributes go stale. */
914
+ + releaseRenderObject( renderObject ) {
915
+
916
+ + const geometry = this._renderObjectAttributeGeometry.get( renderObject );
917
+ + if ( geometry === undefined ) return;
918
+ + const geometryOwners = this._geometryAttributeOwners.get( geometry );
919
+ + const owned = geometryOwners?.get( renderObject )?.keys;
920
+ + if ( owned !== undefined ) {
921
+ + for ( const key of owned.keys() ) this.releaseSharedAttribute( key, renderObject );
922
+ + geometryOwners.delete( renderObject );
923
+ + if ( geometryOwners.size === 0 ) this._geometryAttributeOwners.delete( geometry );
924
+ }
925
+ + this._renderObjectAttributeGeometry.delete( renderObject );
926
+ +
927
+ + }
928
+
929
+ - if ( index !== null ) versions.set( index, index.version );
930
+ + /** Release a shared GPU resource only after its last live geometry render object lets it go. */
931
+ + releaseSharedAttribute( key, owner ) {
932
+
933
+ - this._settledAttributes.set( renderObject, versions );
934
+ + const record = this._sharedAttributeOwners.get( key );
935
+ + if ( record === undefined ) return;
936
+ + record.owners.delete( owner );
937
+ + if ( record.owners.size !== 0 ) return;
938
+
939
+ - return true;
940
+ + this.attributes.delete( key );
941
+ + this._sharedAttributeOwners.delete( key );
942
+ +
943
+ + }
944
+ +
945
+ + /** Release the recorded sets, not a potentially replaced render object's current attributes. */
946
+ + releaseGeometryAttributes( geometry ) {
947
+ +
948
+ + const geometryOwners = this._geometryAttributeOwners.get( geometry );
949
+ + if ( geometryOwners === undefined ) return;
950
+ + for ( const [ owner, attributes ] of geometryOwners ) {
951
+ + for ( const key of attributes.keys.keys() ) this.releaseSharedAttribute( key, owner );
952
+ + this._renderObjectAttributeGeometry.delete( owner );
953
+ + }
954
+ + this._geometryAttributeOwners.delete( geometry );
955
+
956
+ }
957
+
958
+ @@ -34196,23 +34320,10 @@ class Geometries extends DataMap {
959
+
960
+ this.info.memory.geometries --;
961
+
962
+ - // index
963
+ -
964
+ - const index = geometry.index;
965
+ -
966
+ - if ( index !== null ) {
967
+ -
968
+ - this.attributes.delete( index );
969
+ -
970
+ - }
971
+ -
972
+ - // geometry attributes
973
+ -
974
+ - for ( const attribute of Object.values( geometry.attributes ) ) {
975
+ + // index and geometry attributes: release this geometry's owners, so a buffer another
976
+ + // geometry still draws stays alive until its last owner lets it go
977
+
978
+ - this.attributes.delete( this.backend.getBufferAttribute( attribute ) );
979
+ -
980
+ - }
981
+ + this.releaseGeometryAttributes( geometry );
982
+
983
+ // wireframe attributes
984
+
985
+ @@ -34390,17 +34501,9 @@ class Geometries extends DataMap {
986
+ */
987
+ deleteNodeAttributes( renderObject ) {
988
+
989
+ - const currentAttributes = new Set( Object.values( renderObject.geometry.attributes ) );
990
+ -
991
+ - for ( const attribute of renderObject.getAttributes() ) {
992
+ + // node attributes can be shared with other render objects: release this object's owners
993
+
994
+ - if ( currentAttributes.has( attribute ) === false ) {
995
+ -
996
+ - this.attributes.delete( this.backend.getBufferAttribute( attribute ) );
997
+ -
998
+ - }
999
+ -
1000
+ - }
1001
+ + this.releaseRenderObject( renderObject );
1002
+
1003
+ }
1004
+
1005
+ @@ -36045,6 +36148,21 @@ class Bindings extends DataMap {
1006
+
1007
+ if ( groupData.usedTimes === 0 ) {
1008
+
1009
+ + // Stop disposed bind groups from being invalidated by later texture disposal:
1010
+ + // untrack every texture the group was ever tracked on, not only its current one.
1011
+ + const bindingData = this.backend.get( bindGroup );
1012
+ + if ( bindingData.trackedTextures ) {
1013
+ +
1014
+ + for ( const textureData of bindingData.trackedTextures ) {
1015
+ +
1016
+ + textureData.bindGroups.delete( bindGroup );
1017
+ +
1018
+ + }
1019
+ +
1020
+ + bindingData.trackedTextures.clear();
1021
+ +
1022
+ + }
1023
+ +
1024
+ for ( const binding of bindGroup.bindings ) {
1025
+
1026
+ if ( binding.isUniformBuffer ) {
1027
+ @@ -36135,7 +36253,11 @@ class Bindings extends DataMap {
1028
+ // a settled object keeps the values it uploaded the last time it refreshed, so an object
1029
+ // group it has already written is the one thing in here it may skip
1030
+
1031
+ - let needsBindingsUpdate = false;
1032
+ + // a destroyed texture dropped this group's cached GPU groups: rebuild it even if no binding reports a change
1033
+ + const bindingsData = backend.get( bindGroup );
1034
+ + const forceBindingsUpdate = bindingsData.forceUpdate === true;
1035
+ +
1036
+ + let needsBindingsUpdate = forceBindingsUpdate;
1037
+ let cacheBindings = true;
1038
+ let cacheKeyLength = 0;
1039
+ let version = 0;
1040
+ @@ -36144,7 +36266,7 @@ class Bindings extends DataMap {
1041
+
1042
+ for ( const binding of bindGroup.bindings ) {
1043
+
1044
+ - const updatedGroup = this.nodes.updateGroup( binding, settled );
1045
+ + const updatedGroup = forceBindingsUpdate || this.nodes.updateGroup( binding, settled );
1046
+
1047
+ // every uniforms group is a uniform buffer. So if no update is required,
1048
+ // we move one with the next binding. Otherwise the next if block will update the group.
1049
+ @@ -36237,12 +36359,20 @@ class Bindings extends DataMap {
1050
+
1051
+ }
1052
+
1053
+ - // keep track which bind groups refer to the current texture (this is needed for dispose)
1054
+ + }
1055
+ +
1056
+ + // keep track which bind groups refer to the current texture (this is needed for dispose),
1057
+ + // also when the binding's first update reported no change: a destroy must reach this group
1058
+ +
1059
+ + texturesTextureData.bindGroups.add( bindGroup );
1060
+ + if ( bindingsData.trackedTextures === undefined ) {
1061
+
1062
+ - texturesTextureData.bindGroups.add( bindGroup );
1063
+ + bindingsData.trackedTextures = new Set();
1064
+
1065
+ }
1066
+
1067
+ + bindingsData.trackedTextures.add( texturesTextureData );
1068
+ +
1069
+ const textureData = backend.get( texture );
1070
+
1071
+ if ( textureData.externalTexture !== undefined || texturesTextureData.isDefaultTexture ) {
1072
+ @@ -36309,6 +36439,7 @@ class Bindings extends DataMap {
1073
+ const cacheKey = cacheBindings ? _cacheKeyIds.join( ',' ) : '';
1074
+
1075
+ this.backend.updateBindings( bindGroup, bindings, cacheKey, version );
1076
+ + bindingsData.forceUpdate = false;
1077
+
1078
+ }
1079
+
1080
+ @@ -37643,6 +37774,7 @@ class Textures extends DataMap {
1081
+
1082
+ bindingsData.groups = undefined;
1083
+ bindingsData.versions = undefined;
1084
+ + bindingsData.forceUpdate = true;
1085
+
1086
+ }
1087
+
1088
+ @@ -38213,6 +38345,7 @@ class Textures extends DataMap {
1089
+
1090
+ bindingsData.groups = undefined;
1091
+ bindingsData.versions = undefined;
1092
+ + bindingsData.forceUpdate = true;
1093
+
1094
+ // go through all bindings and if one points to the destroyed texture, trigger dispose as well
1095
+
1096
+ @@ -85519,6 +85652,13 @@ class WGSLNodeBuilder extends NodeBuilder {
1097
+
1098
+ super( object, renderer, new WGSLNodeParser() );
1099
+
1100
+ + /**
1101
+ + * Comparison samplers shared by identical keys, and the WGSL sampler name of each texture
1102
+ + * that reads through another texture's sampler.
1103
+ + */
1104
+ + this.sharedCompareSamplers = new Map();
1105
+ + this.samplerNames = new Map();
1106
+ +
1107
+ /**
1108
+ * A dictionary that holds for each shader stage ('vertex', 'fragment', 'compute')
1109
+ * another dictionary which manages UBOs per group ('render','frame','object').
1110
+ @@ -85595,21 +85735,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1111
+
1112
+ if ( offsetSnippet ) {
1113
+
1114
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
1115
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
1116
+
1117
+ }
1118
+
1119
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet } )`;
1120
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet } )`;
1121
+
1122
+ } else {
1123
+
1124
+ if ( offsetSnippet ) {
1125
+
1126
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ offsetSnippet } )`;
1127
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ offsetSnippet } )`;
1128
+
1129
+ }
1130
+
1131
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet } )`;
1132
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet } )`;
1133
+
1134
+ }
1135
+
1136
+ @@ -85641,21 +85781,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1137
+
1138
+ if ( offsetSnippet ) {
1139
+
1140
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1141
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1142
+
1143
+ }
1144
+
1145
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
1146
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
1147
+
1148
+ } else {
1149
+
1150
+ if ( offsetSnippet ) {
1151
+
1152
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1153
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1154
+
1155
+ }
1156
+
1157
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet } )`;
1158
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet } )`;
1159
+
1160
+ }
1161
+
1162
+ @@ -85918,7 +86058,7 @@ class WGSLNodeBuilder extends NodeBuilder {
1163
+ // Depth textures require integer level, regular textures use float
1164
+ const levelType = texture.isDepthTexture ? 'u32' : 'f32';
1165
+
1166
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelType }( ${ levelSnippet } ) )`;
1167
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelType }( ${ levelSnippet } ) )`;
1168
+
1169
+ }
1170
+
1171
+ @@ -86062,6 +86202,18 @@ class WGSLNodeBuilder extends NodeBuilder {
1172
+ * @param {Texture} texture - The texture.
1173
+ * @return {boolean} Whether the sampled values of the given texture should be compared against a reference value or not.
1174
+ */
1175
+ + /**
1176
+ + * The WGSL sampler a texture reads through: its own, or a shared comparison sampler.
1177
+ + *
1178
+ + * @param {string} textureProperty - The texture's WGSL name.
1179
+ + * @return {string} The sampler's WGSL name.
1180
+ + */
1181
+ + getSamplerName( textureProperty ) {
1182
+ +
1183
+ + return this.samplerNames.get( textureProperty ) ?? `${ textureProperty }_sampler`;
1184
+ +
1185
+ + }
1186
+ +
1187
+ isSampleCompare( texture ) {
1188
+
1189
+ return texture.isDepthTexture === true && texture.compareFunction !== null && this.renderer.hasCompatibility( Compatibility.TEXTURE_COMPARE );
1190
+ @@ -86138,21 +86290,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1191
+
1192
+ if ( offsetSnippet ) {
1193
+
1194
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
1195
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
1196
+
1197
+ }
1198
+
1199
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
1200
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
1201
+
1202
+ } else {
1203
+
1204
+ if ( offsetSnippet ) {
1205
+
1206
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
1207
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
1208
+
1209
+ }
1210
+
1211
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
1212
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
1213
+
1214
+ }
1215
+
1216
+ @@ -86185,21 +86337,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1217
+
1218
+ if ( offsetSnippet ) {
1219
+
1220
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1221
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1222
+
1223
+ }
1224
+
1225
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet } )`;
1226
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet } )`;
1227
+
1228
+ }
1229
+
1230
+ if ( offsetSnippet ) {
1231
+
1232
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1233
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1234
+
1235
+ }
1236
+
1237
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet } )`;
1238
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet } )`;
1239
+
1240
+ } else {
1241
+
1242
+ @@ -86264,21 +86416,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1243
+
1244
+ if ( offsetSnippet ) {
1245
+
1246
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
1247
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
1248
+
1249
+ }
1250
+
1251
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet } )`;
1252
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet } )`;
1253
+
1254
+ }
1255
+
1256
+ if ( offsetSnippet ) {
1257
+
1258
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ offsetSnippet } )`;
1259
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ offsetSnippet } )`;
1260
+
1261
+ }
1262
+
1263
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet })`;
1264
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet })`;
1265
+
1266
+ }
1267
+
1268
+ @@ -86299,21 +86451,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1269
+
1270
+ if ( offsetSnippet ) {
1271
+
1272
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1273
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1274
+
1275
+ }
1276
+
1277
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet })`;
1278
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet })`;
1279
+
1280
+ }
1281
+
1282
+ if ( offsetSnippet ) {
1283
+
1284
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1285
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1286
+
1287
+ }
1288
+
1289
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet })`;
1290
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet })`;
1291
+
1292
+ }
1293
+
1294
+ @@ -86336,21 +86488,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1295
+
1296
+ if ( offsetSnippet ) {
1297
+
1298
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1299
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1300
+
1301
+ }
1302
+
1303
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
1304
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
1305
+
1306
+ } else {
1307
+
1308
+ if ( offsetSnippet ) {
1309
+
1310
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1311
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1312
+
1313
+ }
1314
+
1315
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet } )`;
1316
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet } )`;
1317
+
1318
+ }
1319
+
1320
+ @@ -86386,21 +86538,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1321
+
1322
+ if ( offsetSnippet ) {
1323
+
1324
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
1325
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
1326
+
1327
+ }
1328
+
1329
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet } )`;
1330
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet } )`;
1331
+
1332
+ } else {
1333
+
1334
+ if ( offsetSnippet ) {
1335
+
1336
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
1337
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
1338
+
1339
+ }
1340
+
1341
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ biasSnippet } )`;
1342
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ biasSnippet } )`;
1343
+
1344
+ }
1345
+
1346
+ @@ -86644,11 +86796,27 @@ class WGSLNodeBuilder extends NodeBuilder {
1347
+ // Also textureGather always need sampler.
1348
+ const needsSampler = node.isRawTextureNode !== true && ( node.value.isCubeTexture === true || ( this.isUnfilterable( node.value ) === false && texture.store === false ) || node.gatherNode !== null );
1349
+
1350
+ - if ( needsSampler ) {
1351
+ + // Identical comparison samplers share one binding. Every shadow map of a clipmap compares the
1352
+ + // same way, and a sampler per map exhausts maxSamplersPerShaderStage (16 on most adapters).
1353
+ + const sharedKey = needsSampler && this.isSampleCompare( node.value ) && node.isSampleCompare() ? `${ groupName }:${ shaderStage }:${ node.value.compareFunction }:${ node.value.wrapS }:${ node.value.wrapT }:${ node.value.wrapR }:${ node.value.minFilter }:${ node.value.magFilter }` : null;
1354
+ + const sharedSampler = sharedKey === null ? undefined : this.sharedCompareSamplers.get( sharedKey );
1355
+ +
1356
+ + if ( sharedSampler !== undefined ) {
1357
+ +
1358
+ + this.samplerNames.set( uniformNode.name, sharedSampler.name );
1359
+ + nodeData.sharedSampler = true;
1360
+ +
1361
+ + bindings.push( texture );
1362
+ +
1363
+ + uniformGPU = [ sharedSampler, texture ];
1364
+ +
1365
+ + } else if ( needsSampler ) {
1366
+
1367
+ const sampler = new NodeSampler( `${ uniformNode.name }_sampler`, uniformNode.node, group );
1368
+ sampler.setVisibility( gpuShaderStageLib[ shaderStage ] );
1369
+
1370
+ + if ( sharedKey !== null ) this.sharedCompareSamplers.set( sharedKey, sampler );
1371
+ +
1372
+ bindings.push( sampler, texture );
1373
+
1374
+ uniformGPU = [ sampler, texture ];
1375
+ @@ -87460,7 +87628,8 @@ ${ flowData.code }
1376
+ const uniformGPU = nodeData.uniformGPU;
1377
+ const needsSampler = Array.isArray( uniformGPU ) && uniformGPU[ 0 ].isSampler === true && uniformGPU[ 0 ].isSampledTexture !== true;
1378
+
1379
+ - if ( needsSampler ) {
1380
+ + // A shared comparison sampler is declared once, by the texture that created it.
1381
+ + if ( needsSampler && nodeData.sharedSampler !== true ) {
1382
+
1383
+ if ( this.isSampleCompare( texture ) && textureNode.isSampleCompare() ) {
1384
+
1385
+ diff --git a/src/materials/nodes/manager/NodeMaterialObserver.js b/src/materials/nodes/manager/NodeMaterialObserver.js
1386
+ index bd94730965dcc321ecb12083980f7da5f1cfbf9b..239556dc65ed767755f10a57d82dbfee0b79bce3 100644
1387
+ --- a/src/materials/nodes/manager/NodeMaterialObserver.js
1388
+ +++ b/src/materials/nodes/manager/NodeMaterialObserver.js
1389
+ @@ -407,7 +407,8 @@ class NodeMaterialObserver {
1390
+ environmentRotation: scene.environmentRotation.clone(),
1391
+ environmentIntensity: scene.environmentIntensity,
1392
+ materialVersion: material.version,
1393
+ - geometryVersion: geometry.version
1394
+ + geometryVersion: geometry.version,
1395
+ + pmremSwaps: renderObject.renderer._tnPmremSwaps
1396
+ };
1397
+
1398
+ data.settledState = false;
1399
+ @@ -420,7 +421,9 @@ class NodeMaterialObserver {
1400
+ settled.environmentRotation.equals( scene.environmentRotation ) !== true ||
1401
+ settled.environmentIntensity !== scene.environmentIntensity ||
1402
+ settled.materialVersion !== material.version ||
1403
+ - settled.geometryVersion !== geometry.version ) {
1404
+ + settled.geometryVersion !== geometry.version ||
1405
+ + // a PMREM finished and swapped the texture its node samples: a settled object would keep the old one
1406
+ + settled.pmremSwaps !== renderObject.renderer._tnPmremSwaps ) {
1407
+
1408
+ // re-arm with the current state, so the object settles again once it stops changing
1409
+
1410
+ @@ -429,6 +432,7 @@ class NodeMaterialObserver {
1411
+ settled.environmentIntensity = scene.environmentIntensity;
1412
+ settled.materialVersion = material.version;
1413
+ settled.geometryVersion = geometry.version;
1414
+ + settled.pmremSwaps = renderObject.renderer._tnPmremSwaps;
1415
+
1416
+ data.settledState = false;
1417
+
1418
+ diff --git a/src/nodes/accessors/Instance.js b/src/nodes/accessors/Instance.js
1419
+ index 57b6ebfcc3e8be56ec503a9d4ac491dee9d607af..d8594793e78a2f0b8ad38cecef9451ed9c8a32aa 100644
1420
+ --- a/src/nodes/accessors/Instance.js
1421
+ +++ b/src/nodes/accessors/Instance.js
1422
+ @@ -304,8 +304,11 @@ export const instance = /*@__PURE__*/ Fn( ( [ matrices, colors = null ], builder
1423
+
1424
+ const { previousInstanceMatrix } = _previousInstanceMatrices.get( object );
1425
+
1426
+ + if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] === scheduledPrevious ) return;
1427
+ +
1428
+ previousInstanceMatrix.array.set( scheduledPrevious );
1429
+ previousInstanceMatrix.needsUpdate = true;
1430
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = scheduledPrevious;
1431
+
1432
+ } );
1433
+
1434
+ @@ -314,6 +317,8 @@ export const instance = /*@__PURE__*/ Fn( ( [ matrices, colors = null ], builder
1435
+
1436
+ if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatrices' ) ] !== undefined ) return;
1437
+
1438
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = undefined;
1439
+ +
1440
+ const { previousInstanceMatrix } = _previousInstanceMatrices.get( object );
1441
+
1442
+ previousInstanceMatrix.array.set( matrices.array );
1443
+ @@ -364,8 +369,11 @@ export const instancedMesh = /*@__PURE__*/ Fn( ( [ instancedMesh ], builder ) =>
1444
+
1445
+ const previous = getPreviousMatrix( object );
1446
+
1447
+ + if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] === scheduledPrevious ) return;
1448
+ +
1449
+ previous.array.set( scheduledPrevious );
1450
+ previous.needsUpdate = true;
1451
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = scheduledPrevious;
1452
+
1453
+ } );
1454
+
1455
+ @@ -374,6 +382,8 @@ export const instancedMesh = /*@__PURE__*/ Fn( ( [ instancedMesh ], builder ) =>
1456
+
1457
+ if ( object[ Symbol.for( 'threenative.velocity.previousInstanceMatrices' ) ] !== undefined ) return;
1458
+
1459
+ + object[ Symbol.for( 'threenative.velocity.previousInstanceMatricesApplied' ) ] = undefined;
1460
+ +
1461
+ const previous = getPreviousMatrix( object );
1462
+
1463
+ previous.array.set( object.instanceMatrix.array );
1464
+ diff --git a/src/nodes/pmrem/PMREMNode.js b/src/nodes/pmrem/PMREMNode.js
1465
+ index 0f7851b615cadc9cb971e0377bfecb5c23c4afef..5dc2f396f729b6eca2d04c4114bb163930fc2e92 100644
1466
+ --- a/src/nodes/pmrem/PMREMNode.js
1467
+ +++ b/src/nodes/pmrem/PMREMNode.js
1468
+ @@ -438,6 +438,9 @@ class PMREMNode extends Node {
1469
+
1470
+ this._pmrem = pmrem;
1471
+
1472
+ + // count the swap, so every settled object that samples this node refreshes once (NodeMaterialObserver)
1473
+ + if ( this._texture.value !== pmrem && frame.renderer ) frame.renderer._tnPmremSwaps = ( frame.renderer._tnPmremSwaps || 0 ) + 1;
1474
+ +
1475
+ this.updateFromTexture( pmrem );
1476
+
1477
+ }
1478
+ diff --git a/src/renderers/common/Bindings.js b/src/renderers/common/Bindings.js
1479
+ index d44e9db6b1b87139f40eb33ab217ac8094ad4ce9..a0423e27dde75aaf072c8b26eb5101051e8b5193 100644
1480
+ --- a/src/renderers/common/Bindings.js
1481
+ +++ b/src/renderers/common/Bindings.js
1482
+ @@ -384,6 +384,21 @@ class Bindings extends DataMap {
1483
+
1484
+ if ( groupData.usedTimes === 0 ) {
1485
+
1486
+ + // Stop disposed bind groups from being invalidated by later texture disposal:
1487
+ + // untrack every texture the group was ever tracked on, not only its current one.
1488
+ + const bindingData = this.backend.get( bindGroup );
1489
+ + if ( bindingData.trackedTextures ) {
1490
+ +
1491
+ + for ( const textureData of bindingData.trackedTextures ) {
1492
+ +
1493
+ + textureData.bindGroups.delete( bindGroup );
1494
+ +
1495
+ + }
1496
+ +
1497
+ + bindingData.trackedTextures.clear();
1498
+ +
1499
+ + }
1500
+ +
1501
+ for ( const binding of bindGroup.bindings ) {
1502
+
1503
+ if ( binding.isUniformBuffer ) {
1504
+ @@ -474,7 +489,11 @@ class Bindings extends DataMap {
1505
+ // a settled object keeps the values it uploaded the last time it refreshed, so an object
1506
+ // group it has already written is the one thing in here it may skip
1507
+
1508
+ - let needsBindingsUpdate = false;
1509
+ + // a destroyed texture dropped this group's cached GPU groups: rebuild it even if no binding reports a change
1510
+ + const bindingsData = backend.get( bindGroup );
1511
+ + const forceBindingsUpdate = bindingsData.forceUpdate === true;
1512
+ +
1513
+ + let needsBindingsUpdate = forceBindingsUpdate;
1514
+ let cacheBindings = true;
1515
+ let cacheKeyLength = 0;
1516
+ let version = 0;
1517
+ @@ -483,7 +502,7 @@ class Bindings extends DataMap {
1518
+
1519
+ for ( const binding of bindGroup.bindings ) {
1520
+
1521
+ - const updatedGroup = this.nodes.updateGroup( binding, settled );
1522
+ + const updatedGroup = forceBindingsUpdate || this.nodes.updateGroup( binding, settled );
1523
+
1524
+ // every uniforms group is a uniform buffer. So if no update is required,
1525
+ // we move one with the next binding. Otherwise the next if block will update the group.
1526
+ @@ -576,12 +595,20 @@ class Bindings extends DataMap {
1527
+
1528
+ }
1529
+
1530
+ - // keep track which bind groups refer to the current texture (this is needed for dispose)
1531
+ + }
1532
+ +
1533
+ + // keep track which bind groups refer to the current texture (this is needed for dispose),
1534
+ + // also when the binding's first update reported no change: a destroy must reach this group
1535
+ +
1536
+ + texturesTextureData.bindGroups.add( bindGroup );
1537
+ + if ( bindingsData.trackedTextures === undefined ) {
1538
+
1539
+ - texturesTextureData.bindGroups.add( bindGroup );
1540
+ + bindingsData.trackedTextures = new Set();
1541
+
1542
+ }
1543
+
1544
+ + bindingsData.trackedTextures.add( texturesTextureData );
1545
+ +
1546
+ const textureData = backend.get( texture );
1547
+
1548
+ if ( textureData.externalTexture !== undefined || texturesTextureData.isDefaultTexture ) {
1549
+ @@ -648,6 +675,7 @@ class Bindings extends DataMap {
1550
+ const cacheKey = cacheBindings ? _cacheKeyIds.join( ',' ) : '';
1551
+
1552
+ this.backend.updateBindings( bindGroup, bindings, cacheKey, version );
1553
+ + bindingsData.forceUpdate = false;
1554
+
1555
+ }
1556
+
1557
+ diff --git a/src/renderers/common/Geometries.js b/src/renderers/common/Geometries.js
1558
+ index 0dbd75a8f0a5cbd9b0ef00d91b426aafc0132a6e..5d9268aecd1189ff355055e120a8993cb1be48cc 100644
1559
+ --- a/src/renderers/common/Geometries.js
1560
+ +++ b/src/renderers/common/Geometries.js
1561
+ @@ -168,6 +168,9 @@ class Geometries extends DataMap {
1562
+ * @type {WeakMap<RenderObject,Map<BufferAttribute,number>>}
1563
+ */
1564
+ this._settledAttributes = new WeakMap();
1565
+ + this._geometryAttributeOwners = new WeakMap();
1566
+ + this._sharedAttributeOwners = new WeakMap();
1567
+ + this._renderObjectAttributeGeometry = new WeakMap();
1568
+
1569
+ }
1570
+
1571
+ @@ -193,6 +196,7 @@ class Geometries extends DataMap {
1572
+ const geometry = renderObject.geometry;
1573
+
1574
+ if ( this.has( geometry ) === false ) this.initGeometry( geometry );
1575
+ + if ( this.geometryAttributesChanged( renderObject ) ) this.syncGeometryAttributes( renderObject );
1576
+
1577
+ // A settled static object has no attribute that could have changed since the last refresh,
1578
+ // unless a writer bumped one. An InstancedMesh's instance buffer is a vertex attribute of
1579
+ @@ -220,13 +224,18 @@ class Geometries extends DataMap {
1580
+ const index = this.getIndex( renderObject );
1581
+ const settled = this._settledAttributes.get( renderObject );
1582
+
1583
+ - if ( settled !== undefined && settled.size === attributes.length + ( index === null ? 0 : 1 ) ) {
1584
+ + if ( settled !== undefined && settled.attributes.length === attributes.length && settled.index === index ) {
1585
+
1586
+ let changed = false;
1587
+
1588
+ - for ( const attribute of attributes ) {
1589
+ + for ( let i = 0; i < attributes.length; i ++ ) {
1590
+
1591
+ - if ( settled.get( attribute.isInterleavedBufferAttribute ? attribute.data : attribute ) !== attribute.version ) {
1592
+ + const attribute = attributes[ i ];
1593
+ + const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
1594
+ + const previous = settled.attributes[ i ];
1595
+ +
1596
+ + // an interleaved wrapper has no version of its own: its buffer's version is the write signal
1597
+ + if ( previous.attribute !== attribute || previous.data !== data || previous.version !== data.version ) {
1598
+
1599
+ changed = true;
1600
+ break;
1601
+ @@ -235,25 +244,122 @@ class Geometries extends DataMap {
1602
+
1603
+ }
1604
+
1605
+ - if ( changed === false && ( index === null || settled.get( index ) === index.version ) ) return false;
1606
+ + if ( changed === false && ( index === null || settled.indexVersion === index.version ) ) return false;
1607
+
1608
+ }
1609
+
1610
+ - const versions = new Map();
1611
+ + const versions = attributes.map( ( attribute ) => {
1612
+ +
1613
+ + const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
1614
+ + return { attribute, data, version: data.version };
1615
+ +
1616
+ + } );
1617
+ +
1618
+ + this._settledAttributes.set( renderObject, { attributes: versions, index, indexVersion: index === null ? undefined : index.version } );
1619
+ +
1620
+ + return true;
1621
+ +
1622
+ + }
1623
+ +
1624
+ + /** Compare the published attribute identities without allocating on settled frames. */
1625
+ + geometryAttributesChanged( renderObject ) {
1626
+ +
1627
+ + const geometry = renderObject.geometry;
1628
+ + const owner = this._geometryAttributeOwners.get( geometry )?.get( renderObject );
1629
+ + if ( owner === undefined || owner.index !== geometry.index ) return true;
1630
+ +
1631
+ + const attributes = renderObject.getAttributes();
1632
+ + if ( owner.attributes.length !== attributes.length ) return true;
1633
+ + for ( let i = 0; i < attributes.length; i ++ ) {
1634
+ + const attribute = attributes[ i ];
1635
+ + if ( owner.attributes[ i ] !== ( attribute.isInterleavedBufferAttribute ? attribute.data : attribute ) ) return true;
1636
+ + }
1637
+ +
1638
+ + return false;
1639
+ +
1640
+ + }
1641
+ +
1642
+ + /** Add this render object's current geometry attributes to the renderer's shared-resource owners. */
1643
+ + syncGeometryAttributes( renderObject ) {
1644
+ +
1645
+ + const { geometry } = renderObject;
1646
+ + const previousGeometry = this._renderObjectAttributeGeometry.get( renderObject );
1647
+ + if ( previousGeometry !== undefined && previousGeometry !== geometry ) this.releaseRenderObject( renderObject );
1648
+ + this._renderObjectAttributeGeometry.set( renderObject, geometry );
1649
+ + let geometryOwners = this._geometryAttributeOwners.get( geometry );
1650
+ +
1651
+ + if ( geometryOwners === undefined ) {
1652
+ + geometryOwners = new Map();
1653
+ + this._geometryAttributeOwners.set( geometry, geometryOwners );
1654
+ + }
1655
+ +
1656
+ + const previous = geometryOwners.get( renderObject )?.keys ?? new Map();
1657
+ + const current = new Map();
1658
+ + const attributes = renderObject.getAttributes();
1659
+ + const attributeKeys = [];
1660
+
1661
+ for ( const attribute of attributes ) {
1662
+ + const key = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
1663
+ + current.set( key, true );
1664
+ + attributeKeys.push( key );
1665
+ + }
1666
+ + const index = geometry.index;
1667
+ + if ( index !== null ) current.set( index, true );
1668
+ +
1669
+ + for ( const key of current.keys() ) {
1670
+ + let record = this._sharedAttributeOwners.get( key );
1671
+ + if ( record === undefined ) record = { owners: new Set() };
1672
+ + if ( previous.has( key ) === false ) record.owners.add( renderObject );
1673
+ + this._sharedAttributeOwners.set( key, record );
1674
+ + }
1675
+
1676
+ - const data = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
1677
+ + for ( const key of previous.keys() ) {
1678
+ + if ( current.has( key ) === false ) this.releaseSharedAttribute( key, renderObject );
1679
+ + }
1680
+ +
1681
+ + geometryOwners.set( renderObject, { keys: current, attributes: attributeKeys, index } );
1682
+
1683
+ - versions.set( data, data.version );
1684
+ + }
1685
+
1686
+ + /** Drop a render object from its geometry before its material-owned node attributes go stale. */
1687
+ + releaseRenderObject( renderObject ) {
1688
+ +
1689
+ + const geometry = this._renderObjectAttributeGeometry.get( renderObject );
1690
+ + if ( geometry === undefined ) return;
1691
+ + const geometryOwners = this._geometryAttributeOwners.get( geometry );
1692
+ + const owned = geometryOwners?.get( renderObject )?.keys;
1693
+ + if ( owned !== undefined ) {
1694
+ + for ( const key of owned.keys() ) this.releaseSharedAttribute( key, renderObject );
1695
+ + geometryOwners.delete( renderObject );
1696
+ + if ( geometryOwners.size === 0 ) this._geometryAttributeOwners.delete( geometry );
1697
+ }
1698
+ + this._renderObjectAttributeGeometry.delete( renderObject );
1699
+
1700
+ - if ( index !== null ) versions.set( index, index.version );
1701
+ + }
1702
+
1703
+ - this._settledAttributes.set( renderObject, versions );
1704
+ + /** Release a shared GPU resource only after its last live geometry render object lets it go. */
1705
+ + releaseSharedAttribute( key, owner ) {
1706
+
1707
+ - return true;
1708
+ + const record = this._sharedAttributeOwners.get( key );
1709
+ + if ( record === undefined ) return;
1710
+ + record.owners.delete( owner );
1711
+ + if ( record.owners.size !== 0 ) return;
1712
+ +
1713
+ + this.attributes.delete( key );
1714
+ + this._sharedAttributeOwners.delete( key );
1715
+ +
1716
+ + }
1717
+ +
1718
+ + /** Release the recorded sets, not a potentially replaced render object's current attributes. */
1719
+ + releaseGeometryAttributes( geometry ) {
1720
+ +
1721
+ + const geometryOwners = this._geometryAttributeOwners.get( geometry );
1722
+ + if ( geometryOwners === undefined ) return;
1723
+ + for ( const [ owner, attributes ] of geometryOwners ) {
1724
+ + for ( const key of attributes.keys.keys() ) this.releaseSharedAttribute( key, owner );
1725
+ + this._renderObjectAttributeGeometry.delete( owner );
1726
+ + }
1727
+ + this._geometryAttributeOwners.delete( geometry );
1728
+
1729
+ }
1730
+
1731
+ @@ -274,23 +380,10 @@ class Geometries extends DataMap {
1732
+
1733
+ this.info.memory.geometries --;
1734
+
1735
+ - // index
1736
+ -
1737
+ - const index = geometry.index;
1738
+ -
1739
+ - if ( index !== null ) {
1740
+ + // index and geometry attributes: release this geometry's owners, so a buffer another
1741
+ + // geometry still draws stays alive until its last owner lets it go
1742
+
1743
+ - this.attributes.delete( index );
1744
+ -
1745
+ - }
1746
+ -
1747
+ - // geometry attributes
1748
+ -
1749
+ - for ( const attribute of Object.values( geometry.attributes ) ) {
1750
+ -
1751
+ - this.attributes.delete( this.backend.getBufferAttribute( attribute ) );
1752
+ -
1753
+ - }
1754
+ + this.releaseGeometryAttributes( geometry );
1755
+
1756
+ // wireframe attributes
1757
+
1758
+ @@ -468,17 +561,9 @@ class Geometries extends DataMap {
1759
+ */
1760
+ deleteNodeAttributes( renderObject ) {
1761
+
1762
+ - const currentAttributes = new Set( Object.values( renderObject.geometry.attributes ) );
1763
+ + // node attributes can be shared with other render objects: release this object's owners
1764
+
1765
+ - for ( const attribute of renderObject.getAttributes() ) {
1766
+ -
1767
+ - if ( currentAttributes.has( attribute ) === false ) {
1768
+ -
1769
+ - this.attributes.delete( this.backend.getBufferAttribute( attribute ) );
1770
+ -
1771
+ - }
1772
+ -
1773
+ - }
1774
+ + this.releaseRenderObject( renderObject );
1775
+
1776
+ }
1777
+
1778
+ diff --git a/src/renderers/common/RenderObjects.js b/src/renderers/common/RenderObjects.js
1779
+ index 882a39dcc5ab9e62236c2e02e20aa282b90ee421..99d89385ccc4eef01c8072f809f976db455a66a0 100644
1780
+ --- a/src/renderers/common/RenderObjects.js
1781
+ +++ b/src/renderers/common/RenderObjects.js
1782
+ @@ -244,6 +244,7 @@ class RenderObjects {
1783
+ this.pipelines.delete( renderObject );
1784
+ this.bindings.deleteForRender( renderObject );
1785
+ this.nodes.delete( renderObject );
1786
+ + this.geometries.releaseRenderObject( renderObject );
1787
+
1788
+ chainMap.delete( renderObject.getChainArray() );
1789
+
1790
+ diff --git a/src/renderers/common/Textures.js b/src/renderers/common/Textures.js
1791
+ index f8e762fcf1c7ca0a9192e38f2afe26926e03edc8..ceea39f57fbe0a885e0dc75781e53d2e8a28104b 100644
1792
+ --- a/src/renderers/common/Textures.js
1793
+ +++ b/src/renderers/common/Textures.js
1794
+ @@ -327,6 +327,7 @@ class Textures extends DataMap {
1795
+
1796
+ bindingsData.groups = undefined;
1797
+ bindingsData.versions = undefined;
1798
+ + bindingsData.forceUpdate = true;
1799
+
1800
+ }
1801
+
1802
+ @@ -897,6 +898,7 @@ class Textures extends DataMap {
1803
+
1804
+ bindingsData.groups = undefined;
1805
+ bindingsData.versions = undefined;
1806
+ + bindingsData.forceUpdate = true;
1807
+
1808
+ // go through all bindings and if one points to the destroyed texture, trigger dispose as well
1809
+
1810
+ diff --git a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js
1811
+ index c662dfaa40d14a04d38e886a0730c314a85a8bd5..48c5779133deee480709402615417c3f6e58cebb 100644
1812
+ --- a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js
1813
+ +++ b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js
1814
+ @@ -306,6 +306,13 @@ class WGSLNodeBuilder extends NodeBuilder {
1815
+
1816
+ super( object, renderer, new WGSLNodeParser() );
1817
+
1818
+ + /**
1819
+ + * Comparison samplers shared by identical keys, and the WGSL sampler name of each texture
1820
+ + * that reads through another texture's sampler.
1821
+ + */
1822
+ + this.sharedCompareSamplers = new Map();
1823
+ + this.samplerNames = new Map();
1824
+ +
1825
+ /**
1826
+ * A dictionary that holds for each shader stage ('vertex', 'fragment', 'compute')
1827
+ * another dictionary which manages UBOs per group ('render','frame','object').
1828
+ @@ -382,21 +389,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1829
+
1830
+ if ( offsetSnippet ) {
1831
+
1832
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
1833
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
1834
+
1835
+ }
1836
+
1837
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet } )`;
1838
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet } )`;
1839
+
1840
+ } else {
1841
+
1842
+ if ( offsetSnippet ) {
1843
+
1844
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ offsetSnippet } )`;
1845
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ offsetSnippet } )`;
1846
+
1847
+ }
1848
+
1849
+ - return `textureSample( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet } )`;
1850
+ + return `textureSample( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet } )`;
1851
+
1852
+ }
1853
+
1854
+ @@ -428,21 +435,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1855
+
1856
+ if ( offsetSnippet ) {
1857
+
1858
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1859
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1860
+
1861
+ }
1862
+
1863
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
1864
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
1865
+
1866
+ } else {
1867
+
1868
+ if ( offsetSnippet ) {
1869
+
1870
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1871
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
1872
+
1873
+ }
1874
+
1875
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet } )`;
1876
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet } )`;
1877
+
1878
+ }
1879
+
1880
+ @@ -705,7 +712,7 @@ class WGSLNodeBuilder extends NodeBuilder {
1881
+ // Depth textures require integer level, regular textures use float
1882
+ const levelType = texture.isDepthTexture ? 'u32' : 'f32';
1883
+
1884
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelType }( ${ levelSnippet } ) )`;
1885
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelType }( ${ levelSnippet } ) )`;
1886
+
1887
+ }
1888
+
1889
+ @@ -849,6 +856,18 @@ class WGSLNodeBuilder extends NodeBuilder {
1890
+ * @param {Texture} texture - The texture.
1891
+ * @return {boolean} Whether the sampled values of the given texture should be compared against a reference value or not.
1892
+ */
1893
+ + /**
1894
+ + * The WGSL sampler a texture reads through: its own, or a shared comparison sampler.
1895
+ + *
1896
+ + * @param {string} textureProperty - The texture's WGSL name.
1897
+ + * @return {string} The sampler's WGSL name.
1898
+ + */
1899
+ + getSamplerName( textureProperty ) {
1900
+ +
1901
+ + return this.samplerNames.get( textureProperty ) ?? `${ textureProperty }_sampler`;
1902
+ +
1903
+ + }
1904
+ +
1905
+ isSampleCompare( texture ) {
1906
+
1907
+ return texture.isDepthTexture === true && texture.compareFunction !== null && this.renderer.hasCompatibility( Compatibility.TEXTURE_COMPARE );
1908
+ @@ -925,21 +944,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1909
+
1910
+ if ( offsetSnippet ) {
1911
+
1912
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
1913
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
1914
+
1915
+ }
1916
+
1917
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
1918
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
1919
+
1920
+ } else {
1921
+
1922
+ if ( offsetSnippet ) {
1923
+
1924
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
1925
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] }, ${ offsetSnippet } )`;
1926
+
1927
+ }
1928
+
1929
+ - return `textureSampleGrad( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
1930
+ + return `textureSampleGrad( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
1931
+
1932
+ }
1933
+
1934
+ @@ -972,21 +991,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1935
+
1936
+ if ( offsetSnippet ) {
1937
+
1938
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1939
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1940
+
1941
+ }
1942
+
1943
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet } )`;
1944
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet } )`;
1945
+
1946
+ }
1947
+
1948
+ if ( offsetSnippet ) {
1949
+
1950
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1951
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1952
+
1953
+ }
1954
+
1955
+ - return `textureSampleCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet } )`;
1956
+ + return `textureSampleCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet } )`;
1957
+
1958
+ } else {
1959
+
1960
+ @@ -1051,21 +1070,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1961
+
1962
+ if ( offsetSnippet ) {
1963
+
1964
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
1965
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ offsetSnippet } )`;
1966
+
1967
+ }
1968
+
1969
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet } )`;
1970
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet } )`;
1971
+
1972
+ }
1973
+
1974
+ if ( offsetSnippet ) {
1975
+
1976
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ offsetSnippet } )`;
1977
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ offsetSnippet } )`;
1978
+
1979
+ }
1980
+
1981
+ - return `textureGather( ${componentSnippet}${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet })`;
1982
+ + return `textureGather( ${componentSnippet}${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet })`;
1983
+
1984
+ }
1985
+
1986
+ @@ -1086,21 +1105,21 @@ class WGSLNodeBuilder extends NodeBuilder {
1987
+
1988
+ if ( offsetSnippet ) {
1989
+
1990
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1991
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
1992
+
1993
+ }
1994
+
1995
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet })`;
1996
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ compareSnippet })`;
1997
+
1998
+ }
1999
+
2000
+ if ( offsetSnippet ) {
2001
+
2002
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
2003
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet }, ${ offsetSnippet } )`;
2004
+
2005
+ }
2006
+
2007
+ - return `textureGatherCompare( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ compareSnippet })`;
2008
+ + return `textureGatherCompare( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ compareSnippet })`;
2009
+
2010
+ }
2011
+
2012
+ @@ -1123,21 +1142,21 @@ class WGSLNodeBuilder extends NodeBuilder {
2013
+
2014
+ if ( offsetSnippet ) {
2015
+
2016
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
2017
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
2018
+
2019
+ }
2020
+
2021
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
2022
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ levelSnippet } )`;
2023
+
2024
+ } else {
2025
+
2026
+ if ( offsetSnippet ) {
2027
+
2028
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
2029
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet }, ${ offsetSnippet } )`;
2030
+
2031
+ }
2032
+
2033
+ - return `textureSampleLevel( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ levelSnippet } )`;
2034
+ + return `textureSampleLevel( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ levelSnippet } )`;
2035
+
2036
+ }
2037
+
2038
+ @@ -1173,21 +1192,21 @@ class WGSLNodeBuilder extends NodeBuilder {
2039
+
2040
+ if ( offsetSnippet ) {
2041
+
2042
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
2043
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
2044
+
2045
+ }
2046
+
2047
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet } )`;
2048
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ depthSnippet }, ${ biasSnippet } )`;
2049
+
2050
+ } else {
2051
+
2052
+ if ( offsetSnippet ) {
2053
+
2054
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
2055
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ biasSnippet }, ${ offsetSnippet } )`;
2056
+
2057
+ }
2058
+
2059
+ - return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ biasSnippet } )`;
2060
+ + return `textureSampleBias( ${ textureProperty }, ${ this.getSamplerName( textureProperty ) }, ${ uvSnippet }, ${ biasSnippet } )`;
2061
+
2062
+ }
2063
+
2064
+ @@ -1431,11 +1450,27 @@ class WGSLNodeBuilder extends NodeBuilder {
2065
+ // Also textureGather always need sampler.
2066
+ const needsSampler = node.isRawTextureNode !== true && ( node.value.isCubeTexture === true || ( this.isUnfilterable( node.value ) === false && texture.store === false ) || node.gatherNode !== null );
2067
+
2068
+ - if ( needsSampler ) {
2069
+ + // Identical comparison samplers share one binding. Every shadow map of a clipmap compares the
2070
+ + // same way, and a sampler per map exhausts maxSamplersPerShaderStage (16 on most adapters).
2071
+ + const sharedKey = needsSampler && this.isSampleCompare( node.value ) && node.isSampleCompare() ? `${ groupName }:${ shaderStage }:${ node.value.compareFunction }:${ node.value.wrapS }:${ node.value.wrapT }:${ node.value.wrapR }:${ node.value.minFilter }:${ node.value.magFilter }` : null;
2072
+ + const sharedSampler = sharedKey === null ? undefined : this.sharedCompareSamplers.get( sharedKey );
2073
+ +
2074
+ + if ( sharedSampler !== undefined ) {
2075
+ +
2076
+ + this.samplerNames.set( uniformNode.name, sharedSampler.name );
2077
+ + nodeData.sharedSampler = true;
2078
+ +
2079
+ + bindings.push( texture );
2080
+ +
2081
+ + uniformGPU = [ sharedSampler, texture ];
2082
+ +
2083
+ + } else if ( needsSampler ) {
2084
+
2085
+ const sampler = new NodeSampler( `${ uniformNode.name }_sampler`, uniformNode.node, group );
2086
+ sampler.setVisibility( gpuShaderStageLib[ shaderStage ] );
2087
+
2088
+ + if ( sharedKey !== null ) this.sharedCompareSamplers.set( sharedKey, sampler );
2089
+ +
2090
+ bindings.push( sampler, texture );
2091
+
2092
+ uniformGPU = [ sampler, texture ];
2093
+ @@ -2247,7 +2282,8 @@ ${ flowData.code }
2094
+ const uniformGPU = nodeData.uniformGPU;
2095
+ const needsSampler = Array.isArray( uniformGPU ) && uniformGPU[ 0 ].isSampler === true && uniformGPU[ 0 ].isSampledTexture !== true;
2096
+
2097
+ - if ( needsSampler ) {
2098
+ + // A shared comparison sampler is declared once, by the texture that created it.
2099
+ + if ( needsSampler && nodeData.sharedSampler !== true ) {
2100
+
2101
+ if ( this.isSampleCompare( texture ) && textureNode.isSampleCompare() ) {
2102
+