rayzee 7.31.1 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +164 -0
  2. package/dist/rayzee.es.js +6728 -4735
  3. package/dist/rayzee.umd.js +121 -73
  4. package/package.json +2 -2
  5. package/src/EngineDefaults.js +168 -13
  6. package/src/EngineIssues.js +5 -0
  7. package/src/PathTracerApp.js +151 -25
  8. package/src/Processor/ArchiveReader.js +538 -0
  9. package/src/Processor/AssetLoader.js +265 -23
  10. package/src/Processor/BVHBuilder.js +48 -32
  11. package/src/Processor/BVHRefitter.js +244 -99
  12. package/src/Processor/ChunkedRecords.js +302 -0
  13. package/src/Processor/EmissiveTriangleBuilder.js +48 -21
  14. package/src/Processor/GeometryExtractor.js +580 -93
  15. package/src/Processor/HostMemory.js +222 -0
  16. package/src/Processor/InstanceTable.js +548 -97
  17. package/src/Processor/PBRT/EqualAreaOctahedral.js +32 -8
  18. package/src/Processor/PBRT/PBRTCurves.js +249 -0
  19. package/src/Processor/PBRT/PBRTMath.js +19 -0
  20. package/src/Processor/PBRT/PBRTParser.js +187 -41
  21. package/src/Processor/PBRT/PBRTSceneBuilder.js +586 -42
  22. package/src/Processor/PBRT/PBRTTokenizer.js +205 -65
  23. package/src/Processor/PBRT/index.js +257 -50
  24. package/src/Processor/PackedRayBuffer.js +29 -18
  25. package/src/Processor/ParallelBVHBuilder.js +9 -8
  26. package/src/Processor/QueueManager.js +5 -3
  27. package/src/Processor/SceneProcessor.js +1046 -365
  28. package/src/Processor/TLASBuilder.js +546 -201
  29. package/src/Processor/TextureCreator.js +11 -9
  30. package/src/Processor/VRAMTracker.js +12 -3
  31. package/src/Processor/Workers/BVHRefitWorker.js +16 -11
  32. package/src/Processor/Workers/BVHWorker.js +12 -12
  33. package/src/Processor/Workers/TLASWorker.js +37 -0
  34. package/src/Processor/utils.js +8 -20
  35. package/src/Stages/NormalDepth.js +6 -2
  36. package/src/Stages/PathTracer.js +5 -5
  37. package/src/Stages/PathTracerStage.js +163 -41
  38. package/src/TSL/BVHTraversal.js +208 -90
  39. package/src/TSL/Common.js +135 -2
  40. package/src/TSL/Displacement.js +21 -11
  41. package/src/TSL/EmissiveSampling.js +49 -25
  42. package/src/TSL/ExtendKernel.js +3 -2
  43. package/src/TSL/LightBVHSampling.js +7 -4
  44. package/src/TSL/LightsDirect.js +30 -29
  45. package/src/TSL/ShadeKernel.js +8 -6
  46. package/src/TSL/SortGlobalKernels.js +3 -4
  47. package/src/TSL/Struct.js +3 -0
  48. package/src/TSL/TextureSampling.js +24 -9
  49. package/src/TSL/patches.js +149 -84
  50. package/src/index.js +3 -0
  51. package/src/managers/AnimationManager.js +86 -76
  52. package/src/managers/MaterialDataManager.js +29 -23
  53. package/src/managers/TransformManager.js +9 -192
@@ -1,5 +1,5 @@
1
1
  import { DataArrayTexture, RGBAFormat, LinearFilter, UnsignedByteType, SRGBColorSpace, LinearSRGBColorSpace, RepeatWrapping } from "three";
2
- import { alignBucketWidth, TEXTURE_CONSTANTS, MEMORY_CONSTANTS, DEFAULT_TEXTURE_MATRIX, MATERIAL_DATA_LAYOUT, normalizeAttenuationDistance } from '../EngineDefaults.js';
2
+ import { alignBucketWidth, TEXTURE_CONSTANTS, MEMORY_CONSTANTS, DEFAULT_TEXTURE_MATRIX, MATERIAL_DATA_LAYOUT, normalizeAttenuationDistance, BVH_LEAF_MARKERS, assertBVHIndexFits, bvhIndexView } from '../EngineDefaults.js';
3
3
  import TexturesWorker from './Workers/TexturesWorker.js?worker&inline';
4
4
  import { ISSUE_CODES } from '../EngineIssues.js';
5
5
  import { linearToSRGB } from './ToneMapCPU.js';
@@ -1148,12 +1148,15 @@ export class TextureCreator {
1148
1148
 
1149
1149
  flattenBVH( bvhRoot );
1150
1150
 
1151
- // Layout: 4 vec4 per node (16 floats)
1151
+ // Layout: 4 vec4 per node (16 floats). Bounds are floats; every index field is a u32
1152
+ // bit pattern (see EngineDefaults.BVH_LEAF_MARKERS).
1152
1153
  // Inner: [leftMin.xyz, leftChild] [leftMax.xyz, rightChild] [rightMin.xyz, 0] [rightMax.xyz, 0]
1153
- // Leaf: [triOffset, triCount, 0, -1] [0,0,0,0] [0,0,0,0] [0,0,0,0]
1154
+ // Leaf: [triOffset, triCount, 0, TRIANGLE_LEAF] [0,0,0,0] [0,0,0,0] [0,0,0,0]
1154
1155
  const floatsPerNode = TEXTURE_CONSTANTS.VEC4_PER_BVH_NODE * TEXTURE_CONSTANTS.FLOATS_PER_VEC4;
1156
+ assertBVHIndexFits( nodes.length, 'BVH node count' );
1155
1157
  const size = nodes.length * floatsPerNode;
1156
1158
  const data = new Float32Array( size );
1159
+ const idx = bvhIndexView( data ); // index fields are u32 bit patterns
1157
1160
 
1158
1161
  for ( let i = 0; i < nodes.length; i ++ ) {
1159
1162
 
@@ -1171,12 +1174,12 @@ export class TextureCreator {
1171
1174
  data[ stride ] = left.boundsMin.x;
1172
1175
  data[ stride + 1 ] = left.boundsMin.y;
1173
1176
  data[ stride + 2 ] = left.boundsMin.z;
1174
- data[ stride + 3 ] = leftIdx;
1177
+ idx[ stride + 3 ] = leftIdx;
1175
1178
 
1176
1179
  data[ stride + 4 ] = left.boundsMax.x;
1177
1180
  data[ stride + 5 ] = left.boundsMax.y;
1178
1181
  data[ stride + 6 ] = left.boundsMax.z;
1179
- data[ stride + 7 ] = rightIdx;
1182
+ idx[ stride + 7 ] = rightIdx;
1180
1183
 
1181
1184
  data[ stride + 8 ] = right.boundsMin.x;
1182
1185
  data[ stride + 9 ] = right.boundsMin.y;
@@ -1188,10 +1191,9 @@ export class TextureCreator {
1188
1191
 
1189
1192
  } else {
1190
1193
 
1191
- // Leaf node
1192
- data[ stride ] = node.triangleOffset;
1193
- data[ stride + 1 ] = node.triangleCount;
1194
- data[ stride + 3 ] = - 1; // Leaf marker
1194
+ idx[ stride ] = node.triangleOffset;
1195
+ idx[ stride + 1 ] = node.triangleCount;
1196
+ idx[ stride + 3 ] = BVH_LEAF_MARKERS.TRIANGLE_LEAF;
1195
1197
 
1196
1198
  }
1197
1199
 
@@ -32,10 +32,10 @@ function texelBytes( tex ) {
32
32
 
33
33
  }
34
34
 
35
- /** Exact byte size of a storage/buffer attribute's backing typed array. */
35
+ /** Exact GPU byte size of a buffer attribute. GPU-only ones carry no backing array. */
36
36
  export function bufferBytes( attr ) {
37
37
 
38
- return attr?.array?.byteLength || 0;
38
+ return attr?.gpuByteLength ?? attr?.array?.byteLength ?? 0;
39
39
 
40
40
  }
41
41
 
@@ -141,7 +141,16 @@ export class VRAMTracker {
141
141
  // synthetic { bytes } (e.g. attributeArray-backed histograms)
142
142
  if ( typeof r.bytes === 'number' && ! r.isTexture && ! r.isRenderTarget ) return r.bytes;
143
143
 
144
- // buffer attribute — dedupe by backing array (rw/ro nodes share one buffer)
144
+ // buffer attribute — dedupe by backing array, or by the attribute itself when it is
145
+ // GPU-only and has none (rw/ro nodes share one buffer either way)
146
+ if ( r.gpuByteLength != null ) {
147
+
148
+ if ( seen.has( r ) ) return 0;
149
+ seen.add( r );
150
+ return r.gpuByteLength;
151
+
152
+ }
153
+
145
154
  if ( r.array && r.array.byteLength != null ) {
146
155
 
147
156
  if ( seen.has( r.array ) ) return 0;
@@ -2,20 +2,21 @@
2
2
  * BVHRefitWorker — Off-main-thread BVH refit using SharedArrayBuffer.
3
3
  *
4
4
  * Protocol:
5
- * 'init' → receives SharedArrayBuffers + index map (once per scene)
6
- * 'refit' → reads shared positions, writes shared bvh/tri data (per frame)
5
+ * 'init' → receives the shared triangle and node buffers (once per scene)
6
+ * 'refit' → recomputes every node's AABB from the triangles already written there
7
+ *
8
+ * Positions never reach here: the main thread scatters each mesh into the shared triangle
9
+ * records as it reads it, so no buffer the size of the scene exists on either side.
7
10
  */
8
11
 
9
12
  import { BVHRefitter } from '../BVHRefitter.js';
13
+ import { ChunkedRecords } from '../ChunkedRecords.js';
10
14
 
11
- const FLOATS_PER_NODE = 16;
12
15
  const refitter = new BVHRefitter();
13
16
 
14
17
  // Cached shared memory views (set once on 'init', reused every frame)
15
18
  let bvhData = null;
16
19
  let triData = null;
17
- let posData = null;
18
- let bvhToOriginal = null;
19
20
  let nodeCount = 0;
20
21
 
21
22
  self.onmessage = function ( e ) {
@@ -24,11 +25,16 @@ self.onmessage = function ( e ) {
24
25
 
25
26
  if ( type === 'init' ) {
26
27
 
27
- bvhData = new Float32Array( e.data.sharedBvhBuf );
28
- triData = new Float32Array( e.data.sharedTriBuf );
29
- posData = new Float32Array( e.data.sharedPosBuf );
30
- bvhToOriginal = e.data.bvhToOriginal; // transferred Uint32Array
31
- nodeCount = bvhData.length / FLOATS_PER_NODE;
28
+ bvhData = ChunkedRecords.adopt(
29
+ e.data.sharedBvhBufs.map( buf => new Float32Array( buf ) ),
30
+ e.data.bvhRecordCount, 16, e.data.bvhRecordsPerChunk
31
+ );
32
+ // Triangles arrive as one shared buffer per chunk; a single-chunk scene is the usual case.
33
+ triData = ChunkedRecords.adopt(
34
+ e.data.sharedTriBufs.map( buf => new Uint32Array( buf ) ),
35
+ e.data.triRecordCount, e.data.triLanesPerRecord, e.data.triRecordsPerChunk
36
+ );
37
+ nodeCount = bvhData.recordCount;
32
38
  return;
33
39
 
34
40
  }
@@ -39,7 +45,6 @@ self.onmessage = function ( e ) {
39
45
 
40
46
  const startTime = performance.now();
41
47
 
42
- refitter.updateTrianglePositions( triData, posData, bvhToOriginal );
43
48
  refitter.refit( bvhData, triData, nodeCount );
44
49
 
45
50
  self.postMessage( {
@@ -3,7 +3,7 @@ import { createLogger, fmt, applyWorkerLogLevel } from '../../utils/Logger.js';
3
3
 
4
4
  const log = createLogger( 'bvh' );
5
5
 
6
- const FPT = 32; // FLOATS_PER_TRIANGLE
6
+ const FPT = 20; // FLOATS_PER_TRIANGLE — the record is uint lanes, not floats
7
7
 
8
8
  // --- Message dispatcher ---
9
9
 
@@ -60,7 +60,7 @@ function handlePhase1( data ) {
60
60
  } : null;
61
61
 
62
62
  // Attach shared buffer views
63
- builder.triangles = new Float32Array( sharedTriangleData );
63
+ builder.triangles = new Uint32Array( sharedTriangleData );
64
64
  builder.centroids = new Float32Array( sharedCentroids );
65
65
  builder.bMin = new Float32Array( sharedBMin );
66
66
  builder.bMax = new Float32Array( sharedBMax );
@@ -148,8 +148,8 @@ function handleAssemble( data ) {
148
148
 
149
149
  // Reorder triangles using final indices from SharedArrayBuffer
150
150
  const indices = new Uint32Array( sharedIndices );
151
- const src = new Float32Array( sharedTriangleData );
152
- const dst = new Float32Array( sharedReorderBuffer );
151
+ const src = new Uint32Array( sharedTriangleData );
152
+ const dst = new Uint32Array( sharedReorderBuffer );
153
153
 
154
154
  for ( let i = 0; i < triangleCount; i ++ ) {
155
155
 
@@ -219,11 +219,11 @@ function handleFullBuild( data ) {
219
219
  } : null;
220
220
 
221
221
  const inputTriangles = triangleByteOffset !== undefined
222
- ? new Float32Array( triangleData, triangleByteOffset, triangleByteLength / 4 )
223
- : new Float32Array( triangleData );
222
+ ? new Uint32Array( triangleData, triangleByteOffset, triangleByteLength / 4 )
223
+ : new Uint32Array( triangleData );
224
224
 
225
225
  const reorderTarget = sharedReorderBuffer
226
- ? new Float32Array( sharedReorderBuffer )
226
+ ? new Uint32Array( sharedReorderBuffer )
227
227
  : null;
228
228
 
229
229
  const bvhRoot = builder.buildSync( inputTriangles, depth, progressCallback, reorderTarget );
@@ -243,21 +243,21 @@ function handleFullBuild( data ) {
243
243
  self.postMessage( {
244
244
  bvhData,
245
245
  originalToBvh,
246
- triangleCount: inputTriangles.length / 32,
246
+ triangleCount: inputTriangles.length / FPT,
247
247
  treeletStats: builder.splitStats
248
248
  }, transferables );
249
249
 
250
250
  } else {
251
251
 
252
- const reorderedFloat32Array = builder.reorderedTriangleData;
253
- const triangleCount = reorderedFloat32Array.byteLength / ( 32 * 4 );
252
+ const reordered = builder.reorderedTriangleData;
253
+ const triangleCount = reordered.byteLength / ( FPT * 4 );
254
254
 
255
- const transferables = [ bvhData.buffer, reorderedFloat32Array.buffer ];
255
+ const transferables = [ bvhData.buffer, reordered.buffer ];
256
256
  if ( originalToBvh ) transferables.push( originalToBvh.buffer );
257
257
 
258
258
  self.postMessage( {
259
259
  bvhData,
260
- triangles: reorderedFloat32Array,
260
+ triangles: reordered,
261
261
  originalToBvh,
262
262
  triangleCount,
263
263
  treeletStats: builder.splitStats
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Builds the TLAS off the main thread.
3
+ *
4
+ * Only the SAH tree runs here — it is the O(n log n) half, and at 6M placements it pinned the
5
+ * main thread for 25 s. The leaf payloads (BLAS pointer, visibility, world-to-object matrix)
6
+ * are filled in by the caller afterwards: that pass is O(n) and cheap, and keeping it out of
7
+ * the message avoids shipping a 16-float matrix per instance across the boundary.
8
+ */
9
+
10
+ import { TLASBuilder } from '../TLASBuilder.js';
11
+ import { createLogger, fmt, applyWorkerLogLevel } from '../../utils/Logger.js';
12
+
13
+ const log = createLogger( 'bvh' );
14
+
15
+ self.onmessage = function ( e ) {
16
+
17
+ const { aabbs, count, logLevel } = e.data;
18
+ applyWorkerLogLevel( logLevel );
19
+
20
+ try {
21
+
22
+ const start = performance.now();
23
+ // A fresh builder per message sizes its buffer exactly, so the result's ArrayBuffer can
24
+ // be transferred whole rather than copied out of a reused, oversized one.
25
+ const { data, nodeCount } = new TLASBuilder().buildStructure( new Float64Array( aabbs ), count );
26
+ log.debug( `TLAS ${fmt.n( nodeCount )} nodes in ${fmt.ms( performance.now() - start )}` );
27
+
28
+ self.postMessage( { tlasData: data, nodeCount }, [ data.buffer ] );
29
+
30
+ } catch ( error ) {
31
+
32
+ log.error( 'TLAS build failed:', error );
33
+ self.postMessage( { error: error.message } );
34
+
35
+ }
36
+
37
+ };
@@ -1,10 +1,6 @@
1
1
  import { SphereGeometry, Mesh, MeshPhysicalMaterial, Group, Object3D, Color, Vector3, ShaderMaterial, Vector2, Matrix4, GLSL3 } from 'three';
2
- import { QuadMesh } from 'three/webgpu';
3
2
  import { EngineEvents } from '../EngineEvents.js';
4
3
 
5
- /** Every QuadMesh in the realm shares this one geometry (module-level in three). */
6
- const _sharedQuadGeometry = /*@__PURE__*/ new QuadMesh( null ).geometry;
7
-
8
4
  let _statusCallback = null;
9
5
 
10
6
  /**
@@ -103,17 +99,18 @@ export function distroyBuffers( { material, geometry, children } ) {
103
99
  /**
104
100
  * Disposes a renderer without leaking it.
105
101
  *
106
- * Three.js 0.184–0.185: `Renderer.dispose()` does not remove the 'resize' listener
102
+ * Three.js 0.184–0.186: `Renderer.dispose()` does not remove the 'resize' listener
107
103
  * it installs on `_canvasTarget`. The bound handler closes over the renderer,
108
104
  * pinning the entire WebGPU graph (Backend, Nodes, Bindings, Pipelines, GPUDevice,
109
105
  * every TSL node) alive indefinitely — confirmed via heap-snapshot retainer
110
- * analysis. See three/src/renderers/common/Renderer.js:292 (attach) and :2503
111
- * (dispose — missing removal).
106
+ * analysis. See `Renderer.js` (attach in the constructor, still absent from
107
+ * `dispose()` in r186). Re-check on every upgrade; drop this when upstream fixes it.
112
108
  *
113
109
  * Renderers constructed with an external `device` do not destroy it here; three
114
110
  * only destroys a device it created itself.
115
111
  *
116
112
  * @param {import('three/webgpu').WebGPURenderer} renderer
113
+ * @returns {?Promise<void>} Settles once the backend is torn down.
117
114
  */
118
115
  export function disposeRenderer( renderer ) {
119
116
 
@@ -125,20 +122,11 @@ export function disposeRenderer( renderer ) {
125
122
 
126
123
  }
127
124
 
128
- renderer.dispose();
125
+ // r186+ `dispose()` is async and settles once the backend is torn down; anything that
126
+ // measures memory straight afterwards has to await the returned promise.
127
+ const disposed = renderer.dispose();
129
128
  renderer._canvasTarget = null;
130
-
131
- // Three.js 0.185: `RenderObjects.dispose()` drops its chain maps without disposing the
132
- // render objects, so each one leaves behind the 'dispose' listener it registered on its
133
- // geometry. Full-screen passes render a QuadMesh, and every QuadMesh in the realm shares
134
- // one module-level geometry — so that listener list grows by one entry per renderer and
135
- // keeps RenderObject → RenderObjects → Bindings → Backend → GPUDevice reachable for the
136
- // page's lifetime. The chain maps are WeakMaps, so this list is the only handle on them.
137
- //
138
- // Clearing it also drops entries belonging to live renderers, which is safe: the
139
- // singleton geometry is never disposed, so none of these listeners can ever fire.
140
- const quadDisposeListeners = _sharedQuadGeometry._listeners?.dispose;
141
- if ( quadDisposeListeners ) quadDisposeListeners.length = 0;
129
+ return disposed;
142
130
 
143
131
  }
144
132
 
@@ -167,8 +167,9 @@ export class NormalDepth extends RenderStage {
167
167
 
168
168
  if ( pt.triangleStorageAttr && ! this._triStorageNode ) {
169
169
 
170
+ // uvec4, matching PathTracerStage: packed lanes must keep their exact bit pattern.
170
171
  this._triStorageNode = storage(
171
- pt.triangleStorageAttr, 'vec4', pt.triangleStorageAttr.count
172
+ pt.triangleStorageAttr, 'uvec4', pt.triangleStorageAttr.count
172
173
  ).toReadOnly();
173
174
 
174
175
  }
@@ -270,7 +271,10 @@ export class NormalDepth extends RenderStage {
270
271
  const uvTangent = vec4( 0.0 ).toVar();
271
272
  If( material.normalMapIndex.greaterThanEqual( int( 0 ) ), () => {
272
273
 
273
- uvTangent.assign( triangleUVTangent( triStorage, hit.triangleIndex, hit.normal, material.normalTransform ) );
274
+ uvTangent.assign( triangleUVTangent(
275
+ triStorage, hit.triangleIndex, hit.normal, material.normalTransform,
276
+ bvhStorage, hit.instanceLeaf
277
+ ) );
274
278
 
275
279
  } );
276
280
 
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import { uniform, texture, storage } from 'three/tsl';
8
- import { StorageInstancedBufferAttribute } from 'three/webgpu';
8
+ import { gpuOnlyStorageAttribute } from '../TSL/patches.js';
9
9
  import { PathTracerStage } from './PathTracerStage.js';
10
10
  import { PackedRayBuffer, GBUFFER_STRIDE, RAY_STRIDE, HIT_STRIDE, freeStorageAttribute } from '../Processor/PackedRayBuffer.js';
11
11
  import { QueueManager, COUNTER, ENERGY_SCALE } from '../Processor/QueueManager.js';
@@ -993,7 +993,7 @@ export class PathTracer extends PathTracerStage {
993
993
  // canonicalize; u32 stores the bits verbatim.
994
994
  const gBufferVec4s = B * GBUFFER_STRIDE;
995
995
  freeStorageAttribute( this.renderer, this._gBufferAttr );
996
- this._gBufferAttr = new StorageInstancedBufferAttribute( new Uint32Array( gBufferVec4s * 4 ), 4 );
996
+ this._gBufferAttr = gpuOnlyStorageAttribute( gBufferVec4s, 4, Uint32Array );
997
997
  const gBufferRW = storage( this._gBufferAttr, 'uvec4' );
998
998
  const gBufferRO = storage( this._gBufferAttr, 'uvec4' ).toReadOnly();
999
999
 
@@ -1009,15 +1009,15 @@ export class PathTracer extends PathTracerStage {
1009
1009
 
1010
1010
  // Tier-1 convergence: per-pixel running mean of luminance² (Welford second moment), read+written by FinalWrite.
1011
1011
  freeStorageAttribute( this.renderer, this._m2Attr );
1012
- this._m2Attr = new StorageInstancedBufferAttribute( new Float32Array( maxPixels ), 1 );
1012
+ this._m2Attr = gpuOnlyStorageAttribute( maxPixels, 1 );
1013
1013
 
1014
1014
  // Tier-2: per-pixel freeze-candidate streak. FinalWrite writes (RW), buildActivePixels reads (RO).
1015
1015
  freeStorageAttribute( this.renderer, this._streakAttr );
1016
- this._streakAttr = new StorageInstancedBufferAttribute( new Uint32Array( maxPixels ), 1 );
1016
+ this._streakAttr = gpuOnlyStorageAttribute( maxPixels, 1, Uint32Array );
1017
1017
 
1018
1018
  // Tier-2: dilated frozen mask (1 = skip). buildActivePixels writes; active-list + FinalWrite read → race-free.
1019
1019
  freeStorageAttribute( this.renderer, this._frozenMaskAttr );
1020
- this._frozenMaskAttr = new StorageInstancedBufferAttribute( new Uint32Array( maxPixels ), 1 );
1020
+ this._frozenMaskAttr = gpuOnlyStorageAttribute( maxPixels, 1, Uint32Array );
1021
1021
 
1022
1022
  }
1023
1023