rayzee 7.22.5 → 7.22.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rayzee",
3
- "version": "7.22.5",
3
+ "version": "7.22.7",
4
4
  "type": "module",
5
5
  "description": "Real-time WebGPU path tracing engine built on Three.js",
6
6
  "main": "dist/rayzee.umd.js",
@@ -442,16 +442,7 @@ export class PathTracerApp extends EventDispatcher {
442
442
 
443
443
  }
444
444
 
445
- // Abort post-processing and restore denoiser canvas resolution
446
- this.denoisingManager?.abort( this.canvas );
447
-
448
- if ( this.denoisingManager?.restoreBaseResolution() ) {
449
-
450
- const w = this.denoisingManager._lastRenderWidth;
451
- const h = this.denoisingManager._lastRenderHeight;
452
- this.dispatchEvent( { type: 'resolution_changed', width: w, height: h } );
453
-
454
- }
445
+ this._abortPostProcess();
455
446
 
456
447
  this.completion.reset();
457
448
  this.wake();
@@ -1524,6 +1515,37 @@ export class PathTracerApp extends EventDispatcher {
1524
1515
 
1525
1516
  }
1526
1517
 
1518
+ // Aborts any in-flight denoise/upscale and puts the denoiser canvas back at base resolution (the
1519
+ // upscaler leaves it enlarged), so the live canvas is what's on screen again.
1520
+ _abortPostProcess() {
1521
+
1522
+ this.denoisingManager?.abort( this.canvas );
1523
+
1524
+ if ( this.denoisingManager?.restoreBaseResolution() ) {
1525
+
1526
+ const w = this.denoisingManager._lastRenderWidth;
1527
+ const h = this.denoisingManager._lastRenderHeight;
1528
+ this.dispatchEvent( { type: 'resolution_changed', width: w, height: h } );
1529
+
1530
+ }
1531
+
1532
+ }
1533
+
1534
+ /**
1535
+ * Re-runs the post-process chain (OIDN → upscaler) against the accumulated image. The chain fires once,
1536
+ * on the frame the render completes, so a denoiser switched on afterwards would otherwise never run.
1537
+ */
1538
+ requestPostProcessRefresh() {
1539
+
1540
+ if ( ! this.stages.pathTracer?.isReady || this._deviceLost ) return;
1541
+
1542
+ this._abortPostProcess();
1543
+
1544
+ this.completion.renderCompleteDispatched = false;
1545
+ this.wake();
1546
+
1547
+ }
1548
+
1527
1549
  // ═══════════════════════════════════════════════════════════════
1528
1550
  // Deterministic / headless control
1529
1551
  // ═══════════════════════════════════════════════════════════════
@@ -2408,6 +2430,7 @@ export class PathTracerApp extends EventDispatcher {
2408
2430
 
2409
2431
  this.denoisingManager.setOverlayManager( this.overlayManager );
2410
2432
  this.denoisingManager.setResetCallback( () => this.reset() );
2433
+ this.denoisingManager.setPostProcessRefreshCallback( () => this.requestPostProcessRefresh() );
2411
2434
  this.denoisingManager.setSettings( this.settings );
2412
2435
 
2413
2436
  // Expose environment manager (lives on pathTracer stage)
@@ -18,6 +18,9 @@ const MISS_THRESHOLD = 6e4;
18
18
  const sanitize1 = ( x ) => select( x.equal( x ), x, float( 0.0 ) ).clamp( 0.0, 1e7 );
19
19
  const sanitizeRGB = ( c ) => vec3( sanitize1( c.x ), sanitize1( c.y ), sanitize1( c.z ) );
20
20
 
21
+ // Frames before the temporal EMA variance is trusted on its own (Schied 2017 §4.2).
22
+ const VARIANCE_HISTORY_MIN = 4;
23
+
21
24
  // 5×5 à-trous kernel weights (B3-spline / Gaussian approx, Σ = 1.0).
22
25
  const ATROUS_KERNEL = [
23
26
  1.0 / 256.0, 4.0 / 256.0, 6.0 / 256.0, 4.0 / 256.0, 1.0 / 256.0,
@@ -66,7 +69,9 @@ export class EdgeFilter extends RenderStage {
66
69
  // phiNormal: normal cone exponent (bigger = tighter, preserves more edges).
67
70
  // phiDepth: RELATIVE depth tolerance (fraction of ray distance, scale-invariant).
68
71
  this.filterStrength = uniform( options.filterStrength ?? 1.0 );
69
- this.phiLuminance = uniform( options.phiLuminance ?? 4.0 );
72
+ // 1.0, not the old 4.0, which was tuned against the non-converging variance seed below
73
+ // and now over-blurs enough to lose to no denoising at all from ~16spp up.
74
+ this.phiLuminance = uniform( options.phiLuminance ?? 1.0 );
70
75
  this.phiNormal = uniform( options.phiNormal ?? 64.0 );
71
76
  this.phiDepth = uniform( options.phiDepth ?? 0.1 );
72
77
  this.iterations = options.atrousIterations ?? options.edgeAtrousIterations ?? 5;
@@ -75,6 +80,7 @@ export class EdgeFilter extends RenderStage {
75
80
  this.isLastIterationU = uniform( 0, 'int' );
76
81
  // 1 on pass 0 → seed variance from variance:output; later passes read the carried value.
77
82
  this.isFirstIterationU = uniform( 1, 'int' );
83
+ this.varianceHistoryU = uniform( 0, 'int' );
78
84
  this.resW = uniform( options.width || 1 );
79
85
  this.resH = uniform( options.height || 1 );
80
86
 
@@ -205,6 +211,7 @@ export class EdgeFilter extends RenderStage {
205
211
  const stepSize = this.stepSizeU;
206
212
  const isLastIterationU = this.isLastIterationU;
207
213
  const isFirstIterationU = this.isFirstIterationU;
214
+ const varianceHistoryU = this.varianceHistoryU;
208
215
  const resW = this.resW;
209
216
  const resH = this.resH;
210
217
 
@@ -233,19 +240,23 @@ export class EdgeFilter extends RenderStage {
233
240
  const demodAlbedo = select( centerIsHitBool, max( centerAlbedo, vec3( ALBEDO_EPS ) ), vec3( 1.0 ) );
234
241
  const centerAlbedoLum = max( luminance( demodAlbedo ), float( ALBEDO_EPS ) );
235
242
 
236
- // Variance-guided luminance σ (SVGF). .z = temporal, .w = spatial variance;
237
- // max() falls back to the spatial estimate as temporal collapses on a
238
- // converging accumulation buffer. Dividing by albedoLum rescales the
239
- // modulated-space variance into demodulated space (std ∝ 1/albedo).
243
+ // Variance-guided luminance σ (SVGF). .z = temporal, .w = spatial variance.
244
+ // Temporal only, once the EMA is warm: .w is the 3×3 contrast of the
245
+ // *accumulated* image, so it plateaus at scene detail instead of decaying with
246
+ // sample count. max()-ing it in pinned sigma_l wide forever, which left the
247
+ // geometric gates — binary reads off the jitter-free G-buffer — to snap every
248
+ // antialiased silhouette pixel to one side, destroying sub-pixel AA.
240
249
  //
241
- // Filtered alongside colour and carried between passes as √variance in the
242
- // ping-pong's spare alpha, so sigma_l tightens as the signal smooths. Stored as
243
- // the root because converged variance (~1e-6) is a half-float subnormal.
250
+ // Dividing by albedoLum rescales the modulated-space variance into demodulated
251
+ // space (std ∝ 1/albedo). Filtered alongside colour and carried between passes
252
+ // as √variance in the ping-pong's spare alpha, so sigma_l tightens as the signal
253
+ // smooths. Stored as the root because converged variance is a half-float subnormal.
244
254
  const isFirst = isFirstIterationU.equal( int( 1 ) );
255
+ const historyShort = varianceHistoryU.lessThan( int( VARIANCE_HISTORY_MIN ) );
245
256
  const seedVariance = ( at ) => {
246
257
 
247
258
  const v = textureLoad( varTex, at );
248
- return max( v.z, v.w );
259
+ return select( historyShort, max( v.z, v.w ), v.z );
249
260
 
250
261
  };
251
262
 
@@ -383,6 +394,8 @@ export class EdgeFilter extends RenderStage {
383
394
 
384
395
  }
385
396
 
397
+ this.varianceHistoryU.value = context.getState( 'accumulatedFrames' ) ?? 0;
398
+
386
399
  this._colorTexNode.value = colorTex;
387
400
  this._ndTexNode.value = ndTex;
388
401
  this._snTexNode.value = snTex;
@@ -68,6 +68,10 @@ export class PathTracer extends PathTracerStage {
68
68
  // of the point-sampled per-frame value that leaks noise. Off for fast/ASVGF (they want the bump normal).
69
69
  this._cleanAuxNormalEnabled = false;
70
70
  this._cleanAuxNormalUniform = uniform( 0, 'uint' );
71
+ // Aux MRT accumulates on an epoch of its own: enabling the denoiser at colour frame N must not leave
72
+ // the stale aux pinned by the colour's 1/(N+1) alpha.
73
+ this._auxSamples = 0;
74
+ this._auxSeedPending = false;
71
75
 
72
76
  // CPU sizes per-bounce kernels from last frame's survivor curve; kernels bound on ENTERING_COUNT so over-sizing is safe. (indirect dispatch not viable — three.js doesn't sync compute-written indirect buffers across submissions)
73
77
  this._useDynamicDispatch = true;
@@ -238,8 +242,17 @@ export class PathTracer extends PathTracerStage {
238
242
 
239
243
  if ( this.isComplete || this.frameCount >= this.completionThreshold || this._isConvergedComplete() ) {
240
244
 
241
- if ( ! this.isComplete ) this.isComplete = true;
242
- return;
245
+ // Denoiser switched on after the render finished — spend one more frame to fill the aux MRT.
246
+ if ( this._auxSeedPending ) {
247
+
248
+ this.isComplete = false;
249
+
250
+ } else {
251
+
252
+ if ( ! this.isComplete ) this.isComplete = true;
253
+ return;
254
+
255
+ }
243
256
 
244
257
  }
245
258
 
@@ -283,6 +296,7 @@ export class PathTracer extends PathTracerStage {
283
296
  }
284
297
 
285
298
  this._updateAccumulationUniforms( frameValue, renderMode );
299
+ const auxMixes = this._updateAuxAccumulationUniforms( frameValue );
286
300
  this.frame.value = frameValue;
287
301
  this.seedFrame.value = this._seedTick ++;
288
302
 
@@ -354,7 +368,8 @@ export class PathTracer extends PathTracerStage {
354
368
 
355
369
  // Tier-2 bounce-0 grid size from the stale settled active-pixel count (single-chunk only; else full).
356
370
  // Monotonic freeze ⇒ a stale count is a safe over-estimate; reset/camera-move → _curveSizingValid=false.
357
- const adaptiveFreeze = this.usePixelFreeze.value > 0;
371
+ // An aux seed frame must trace every pixel: Generate/Shade skip the G-buffer for frozen ones.
372
+ const adaptiveFreeze = this.usePixelFreeze.value > 0 && ! this._auxSeedPending;
358
373
  const activeBounce0 = ( singleChunk && adaptiveFreeze && this._curveSizingValid && this._lastActivePixelCount > 0 )
359
374
  ? this._lastActivePixelCount : maxRays;
360
375
 
@@ -488,6 +503,9 @@ export class PathTracer extends PathTracerStage {
488
503
  // Don't count interaction-mode (1-SPP feedback) frames toward completion (megakernel parity Stages/PathTracer.js:1240) — else a continuous orbit "completes" on noise.
489
504
  if ( ! this.cameraOptimizer?.isInInteractionMode() ) this.frameCount ++;
490
505
 
506
+ if ( this._auxGBufferEnabled ) this._auxSamples = auxMixes ? this._auxSamples + 1 : 1;
507
+ this._auxSeedPending = false;
508
+
491
509
  if ( originalMaxBounces !== null ) this.maxBounces.value = originalMaxBounces;
492
510
 
493
511
  this.performanceMonitor?.end();
@@ -518,6 +536,8 @@ export class PathTracer extends PathTracerStage {
518
536
  this._convergedFraction = 0;
519
537
  this._convergedGeometryFraction = 0;
520
538
  this._lastActivePixelCount = 0;
539
+ this._auxSamples = 0;
540
+ this._auxSeedPending = false;
521
541
 
522
542
  }
523
543
 
@@ -585,28 +605,62 @@ export class PathTracer extends PathTracerStage {
585
605
  }
586
606
 
587
607
  // Aux MRT (normalDepth/albedo) is needed only by the denoiser/OIDN; DenoisingManager calls this to
588
- // turn the wavefront's aux writes on/off. It's a live uniform, so toggling is just a value flip +
589
- // accumulation reset — no kernel rebuild, no UI freeze.
608
+ // turn the wavefront's aux writes on/off. It's a live uniform, so toggling is just a value flip —
609
+ // no kernel rebuild, no UI freeze.
590
610
  setAuxGBufferEnabled( enabled ) {
591
611
 
592
612
  enabled = !! enabled;
593
613
  if ( this._auxGBufferEnabled === enabled ) return;
594
614
  this._auxGBufferEnabled = enabled;
595
615
  this._auxGBufferUniform.value = enabled ? 1 : 0;
596
- this.reset();
616
+ this.restartAuxAccumulation();
597
617
 
598
618
  }
599
619
 
600
620
  // Clean-aux normal: when a clean-aux OIDN model is active (calb_cnrm/high, alb_nrm/balanced), FinalWrite
601
621
  // temporally accumulates + renormalizes the aux normal so the model isn't fed per-frame point-sampled
602
- // noise. DenoisingManager calls this on OIDN enable + quality change. Live uniform → value flip + reset.
622
+ // noise. DenoisingManager calls this on OIDN enable + quality change. The two modes write the normal in
623
+ // incompatible ways, so the aux buffer has to refill.
603
624
  setCleanAuxNormal( enabled ) {
604
625
 
605
626
  enabled = !! enabled;
606
627
  if ( this._cleanAuxNormalEnabled === enabled ) return;
607
628
  this._cleanAuxNormalEnabled = enabled;
608
629
  this._cleanAuxNormalUniform.value = enabled ? 1 : 0;
609
- this.reset();
630
+ this.restartAuxAccumulation();
631
+
632
+ }
633
+
634
+ /** Drops the aux MRT's history, leaving the colour's accumulation intact. */
635
+ restartAuxAccumulation() {
636
+
637
+ this._auxSamples = 0;
638
+ this._auxSeedPending = this._auxGBufferEnabled;
639
+
640
+ }
641
+
642
+ /**
643
+ * @returns {boolean} whether FinalWrite will MIX the aux this frame rather than seed it.
644
+ *
645
+ * colorAccumulates mirrors FinalWrite's outer gate: when that gate is false the aux is written verbatim
646
+ * regardless, which restarts the epoch. A mismatch costs aux quality only — the shader also reads
647
+ * hasPreviousAux, so it can never mix against textures this epoch has not written.
648
+ */
649
+ _updateAuxAccumulationUniforms( frameValue ) {
650
+
651
+ const colorAccumulates = this.accumulationEnabled
652
+ && ! this.cameraIsMoving.value
653
+ && frameValue > 0
654
+ && this.hasPreviousAccumulated.value > 0
655
+ && this.visMode.value !== 11;
656
+
657
+ const mixes = this._auxGBufferEnabled && colorAccumulates
658
+ && ! this._auxSeedPending && this._auxSamples > 0;
659
+
660
+ this.hasPreviousAux.value = mixes ? 1 : 0;
661
+ this.auxAccumulationAlpha.value = mixes ? 1 / ( this._auxSamples + 1 ) : 1.0;
662
+
663
+ return mixes;
610
664
 
611
665
  }
612
666
 
@@ -1483,6 +1537,8 @@ export class PathTracer extends PathTracerStage {
1483
1537
  enableAccumulation: this.enableAccumulation,
1484
1538
  hasPreviousAccumulated: this.hasPreviousAccumulated,
1485
1539
  accumulationAlpha: this.accumulationAlpha,
1540
+ hasPreviousAux: this.hasPreviousAux,
1541
+ auxAccumulationAlpha: this.auxAccumulationAlpha,
1486
1542
  cameraIsMoving: this.cameraIsMoving,
1487
1543
  transparentBackground: this.transparentBackground,
1488
1544
  prevAccumTexture: prevColor,
@@ -37,6 +37,8 @@ export function buildFinalWriteKernel( params ) {
37
37
  writeColorTex, writeNDTex, writeAlbedoTex,
38
38
  resolution, frame,
39
39
  enableAccumulation, hasPreviousAccumulated, accumulationAlpha, cameraIsMoving,
40
+ // Aux MRT accumulates on its own epoch; hasPreviousAux=0 → write it verbatim instead of mixing.
41
+ hasPreviousAux, auxAccumulationAlpha,
40
42
  transparentBackground,
41
43
  prevAccumTexture, prevAlbedoTexture, prevNormalDepthTexture,
42
44
  renderWidth,
@@ -113,10 +115,10 @@ export function buildFinalWriteKernel( params ) {
113
115
 
114
116
  // Frozen pixels pass prev colour through unchanged (stale sample); active pixels accumulate.
115
117
  finalColor.assign( select( wasFrozen, prevAccumSample.xyz, mix( prevAccumSample.xyz, sampleColor.xyz, accumulationAlpha ) ) );
116
- If( auxOn, () => {
118
+ If( auxOn.and( hasPreviousAux ), () => {
117
119
 
118
120
  // Albedo averages cleanly (it's a colour).
119
- finalAlbedo.assign( mix( texture( prevAlbedoTexture, prevUV, 0 ).xyz, finalAlbedo, accumulationAlpha ) );
121
+ finalAlbedo.assign( mix( texture( prevAlbedoTexture, prevUV, 0 ).xyz, finalAlbedo, auxAccumulationAlpha ) );
120
122
 
121
123
  // NORMAL: by default keep this frame's POINT-SAMPLED normal — it varies with the bump,
122
124
  // which fast/ASVGF want to preserve edge detail. But a CLEAN-AUX OIDN model (calb_cnrm/high,
@@ -129,7 +131,7 @@ export function buildFinalWriteKernel( params ) {
129
131
 
130
132
  const prevN = texture( prevNormalDepthTexture, prevUV, 0 ).xyz.mul( 2.0 ).sub( 1.0 );
131
133
  const curN = finalNormalDepth.xyz.mul( 2.0 ).sub( 1.0 ).toVar();
132
- const mixedN = mix( prevN, curN, accumulationAlpha ).toVar();
134
+ const mixedN = mix( prevN, curN, auxAccumulationAlpha ).toVar();
133
135
  const len = length( mixedN );
134
136
  const avgN = select( len.greaterThan( 1e-4 ), mixedN.div( len ), curN );
135
137
  finalNormalDepth.assign( vec4( avgN.mul( 0.5 ).add( 0.5 ), finalNormalDepth.w ) );
@@ -50,6 +50,9 @@ export class DenoisingManager extends EventDispatcher {
50
50
  this.denoiser = null;
51
51
  this.upscaler = null;
52
52
 
53
+ this._onReset = null;
54
+ this._onPostProcessRefresh = null;
55
+
53
56
  // Resolution tracking — used for canvas restoration on reset
54
57
  this._lastRenderWidth = 0;
55
58
  this._lastRenderHeight = 0;
@@ -547,6 +550,13 @@ export class DenoisingManager extends EventDispatcher {
547
550
 
548
551
  }
549
552
 
553
+ /** @param {Function} fn - () => void, re-runs the completion chain on the accumulated image */
554
+ setPostProcessRefreshCallback( fn ) {
555
+
556
+ this._onPostProcessRefresh = fn;
557
+
558
+ }
559
+
550
560
  /** @param {import('../RenderSettings.js').RenderSettings} settings */
551
561
  setSettings( settings ) {
552
562
 
@@ -620,6 +630,7 @@ export class DenoisingManager extends EventDispatcher {
620
630
  if ( this.denoiser ) this.denoiser.enabled = enabled;
621
631
  // OIDN reads the PathTracer aux MRT; re-sync so the wavefront produces it while OIDN is on.
622
632
  this._syncGBufferStages();
633
+ this._onPostProcessRefresh?.();
623
634
 
624
635
  }
625
636
 
@@ -629,6 +640,7 @@ export class DenoisingManager extends EventDispatcher {
629
640
  this.denoiser?.updateQuality( quality );
630
641
  // Clean-aux normal follows the model: balanced/high (clean-aux models) → accumulate; fast → keep bump.
631
642
  this._stages.pathTracer?.setCleanAuxNormal?.( !! this.denoiser?.enabled && quality !== 'fast' );
643
+ this._onPostProcessRefresh?.();
632
644
 
633
645
  }
634
646
 
@@ -196,6 +196,10 @@ export class UniformManager {
196
196
  ub( 'cameraIsMoving', false );
197
197
  ub( 'hasPreviousAccumulated', false );
198
198
 
199
+ // Aux MRT epoch, separate from the colour's so the denoiser can be enabled mid-render.
200
+ u( 'auxAccumulationAlpha', 1.0, 'float' );
201
+ ub( 'hasPreviousAux', false );
202
+
199
203
  // Environment
200
204
  u( 'environmentIntensity', DEFAULT_STATE.environmentIntensity, 'float' );
201
205
  u( 'backgroundIntensity', DEFAULT_STATE.backgroundIntensity, 'float' );