rayzee 5.6.0 → 5.6.1

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/dist/rayzee.es.js CHANGED
@@ -8983,7 +8983,7 @@ var rc = 128, ic = class {
8983
8983
  this.deltaTime.value = e;
8984
8984
  }
8985
8985
  dispose() {
8986
- this._screenSpaceComputeNode?.dispose(), this._worldSpaceComputeNode?.dispose(), this._screenSpaceStorageTex?.dispose(), this._worldSpaceStorageTex?.dispose(), this.screenSpaceTarget?.dispose(), this.worldSpaceTarget?.dispose();
8986
+ this._screenSpaceComputeNode?.dispose(), this._worldSpaceComputeNode?.dispose(), this._screenSpaceStorageTex?.dispose(), this._worldSpaceStorageTex?.dispose(), this.screenSpaceTarget?.dispose(), this.worldSpaceTarget?.dispose(), this._normalDepthTexNode?.dispose();
8987
8987
  }
8988
8988
  };
8989
8989
  //#endregion
@@ -9331,7 +9331,7 @@ var _c = class extends Sn {
9331
9331
  }
9332
9332
  reset() {}
9333
9333
  dispose() {
9334
- this._gradientNode?.dispose(), this._temporalNodeA?.dispose(), this._temporalNodeB?.dispose(), this._temporalTexA?.dispose(), this._temporalTexB?.dispose(), this._prevNDTexA?.dispose(), this._prevNDTexB?.dispose(), this._gradientStorageTex?.dispose(), this._heatmapComputeNode?.dispose(), this._heatmapStorageTex?.dispose(), this.heatmapTarget?.dispose(), this.heatmapHelper?.dispose();
9334
+ this._gradientNode?.dispose(), this._temporalNodeA?.dispose(), this._temporalNodeB?.dispose(), this._temporalTexA?.dispose(), this._temporalTexB?.dispose(), this._prevNDTexA?.dispose(), this._prevNDTexB?.dispose(), this._gradientStorageTex?.dispose(), this._heatmapComputeNode?.dispose(), this._heatmapStorageTex?.dispose(), this.heatmapTarget?.dispose(), this._colorTexNode?.dispose(), this._normalDepthTexNode?.dispose(), this._motionTexNode?.dispose(), this._readTemporalTexNode?.dispose(), this._readPrevNDTexNode?.dispose(), this._gradientReadTexNode?.dispose(), this._heatmapRawColorTexNode?.dispose(), this._heatmapColorTexNode?.dispose(), this._heatmapTemporalTexNode?.dispose(), this._heatmapNDTexNode?.dispose(), this._heatmapMotionTexNode?.dispose(), this._heatmapGradientTexNode?.dispose(), this.heatmapHelper?.dispose();
9335
9335
  }
9336
9336
  }, vc = /* @__PURE__ */ K("\n fn temporalAccumulate(\n lum: f32,\n prevMean: f32,\n prevMeanSq: f32,\n alpha: f32,\n spatialVariance: f32,\n varianceBoost: f32\n ) -> vec4f {\n\n let newMean = prevMean + ( lum - prevMean ) * alpha;\n let newMeanSq = prevMeanSq + ( lum * lum - prevMeanSq ) * alpha;\n let temporalVariance = max( newMeanSq - newMean * newMean, 0.0 );\n\n return vec4f(\n newMean,\n newMeanSq,\n temporalVariance * varianceBoost,\n spatialVariance * varianceBoost\n );\n\n }\n"), yc = class extends Sn {
9337
9337
  constructor(e, t = {}) {
@@ -9406,7 +9406,7 @@ var _c = class extends Sn {
9406
9406
  ];
9407
9407
  }
9408
9408
  dispose() {
9409
- this._computeNodeA?.dispose(), this._computeNodeB?.dispose(), this._storageTexA?.dispose(), this._storageTexB?.dispose();
9409
+ this._computeNodeA?.dispose(), this._computeNodeB?.dispose(), this._storageTexA?.dispose(), this._storageTexB?.dispose(), this._colorTexNode?.dispose(), this._readTexNodeA?.dispose(), this._readTexNodeB?.dispose();
9410
9410
  }
9411
9411
  }, bc = /* @__PURE__ */ K("\n fn bilateralWeight(\n centerLum: f32, sLum: f32,\n centerNormal: vec3f, sNormal: vec3f,\n centerDepth: f32, sDepth: f32,\n centerColor: vec3f, sColor: vec3f,\n kernelW: f32,\n phiLum: f32, phiNorm: f32, phiDep: f32, phiCol: f32\n ) -> f32 {\n\n let lumW = exp( -abs( centerLum - sLum ) * phiLum );\n let normW = pow( max( dot( centerNormal, sNormal ), 0.0 ), phiNorm );\n let depW = exp( -abs( centerDepth - sDepth ) / max( phiDep, 0.001 ) );\n let maxDiff = max( max( abs( centerColor.x - sColor.x ),\n abs( centerColor.y - sColor.y ) ),\n abs( centerColor.z - sColor.z ) );\n let colW = exp( -maxDiff * phiCol );\n return kernelW * lumW * normW * depW * colW;\n\n }\n"), xc = class extends Sn {
9412
9412
  constructor(e, t = {}) {
@@ -9496,7 +9496,7 @@ var _c = class extends Sn {
9496
9496
  }
9497
9497
  reset() {}
9498
9498
  dispose() {
9499
- this._computeNodeA?.dispose(), this._computeNodeB?.dispose(), this._storageTexA?.dispose(), this._storageTexB?.dispose();
9499
+ this._computeNodeA?.dispose(), this._computeNodeB?.dispose(), this._storageTexA?.dispose(), this._storageTexB?.dispose(), this._readTexNode?.dispose(), this._normalDepthTexNode?.dispose();
9500
9500
  }
9501
9501
  }, Sc = /* @__PURE__ */ K("\n fn computeSamplingGuidance(\n temporalVariance: f32,\n spatialVariance: f32,\n meanLuminance: f32,\n threshold: f32,\n frame: i32,\n minFrames: i32,\n convThreshold: f32,\n sensitivity: f32,\n convSpeedScale: f32\n ) -> vec4f {\n\n // The path tracer accumulates via alpha = 1/(frame+1), so temporal variance\n // of the accumulated output shrinks as ~sigma²/(frame+1)². Scale by (frame+1)\n // to get accumulated image quality ~sigma²/N — decreases as image converges.\n let frameScale = f32( frame + 1 );\n let effectiveVariance = temporalVariance * frameScale;\n\n // Normalize by luminance² — converts absolute variance to relative (CV²).\n // Floor of 0.01 prevents noise amplification for near-black pixels\n // (linear luminance < 0.1 → below perceptual visibility threshold).\n let normFactor = max( meanLuminance * meanLuminance, 0.01 );\n let normalizedVariance = effectiveVariance / normFactor;\n\n let varianceRatio = clamp( normalizedVariance / threshold, 0.0, 1.0 );\n\n // Apply sensitivity — higher values assign more samples to noisy pixels\n var normalizedSamples = clamp( varianceRatio * sensitivity, 0.0, 1.0 );\n\n // Small spatial boost for noisy neighbourhoods (un-scaled — provides\n // a minor secondary signal that naturally diminishes as image converges)\n let spatialBoost = clamp( spatialVariance / ( threshold * 4.0 ), 0.0, 0.2 );\n normalizedSamples = clamp( normalizedSamples + spatialBoost, 0.0, 1.0 );\n\n // Warm-up: variance estimates need a few frames to stabilise\n if ( frame < minFrames ) {\n\n let warmupFactor = f32( frame ) / f32( minFrames );\n normalizedSamples = mix( 1.0, normalizedSamples, warmupFactor * warmupFactor );\n\n }\n\n // Convergence: mark pixel only when per-frame noise is truly negligible.\n // convSpeedScale controls aggressiveness: higher = easier to converge\n // (scales the threshold up, so more pixels qualify as converged).\n let scaledConvThreshold = convThreshold * convSpeedScale;\n var converged = 0.0;\n if ( normalizedVariance < scaledConvThreshold && frame > minFrames ) {\n\n converged = 1.0;\n\n }\n\n return vec4f(\n normalizedSamples,\n varianceRatio,\n converged,\n 1.0\n );\n\n }\n"), Cc = /* @__PURE__ */ K("\n fn heatmapGradient( t: f32, normalizedVariance: f32, converged: f32 ) -> vec4f {\n\n let r = clamp( ( t - 0.5 ) * 4.0, 0.0, 1.0 );\n let g = clamp( t * 4.0, 0.0, 1.0 ) - clamp( ( t - 0.75 ) * 4.0, 0.0, 1.0 );\n let b = 1.0 - clamp( ( t - 0.25 ) * 4.0, 0.0, 1.0 );\n\n var color = vec3f( r, g, b );\n\n // Convergence: desaturate converged pixels\n if ( converged > 0.5 ) {\n\n let gray = color.x * 0.299 + color.y * 0.587 + color.z * 0.114;\n color = mix( color, vec3f( gray ), 0.6 );\n\n }\n\n // Brightness modulation\n color *= 0.7 + normalizedVariance * 0.3;\n\n return vec4f( color, 1.0 );\n\n }\n"), wc = class extends Sn {
9502
9502
  constructor(e, t = {}) {
@@ -9604,7 +9604,7 @@ var _c = class extends Sn {
9604
9604
  e.threshold !== void 0 && this.setVarianceThreshold(e.threshold), e.materialBias !== void 0 && this.setMaterialBias(e.materialBias), e.edgeBias !== void 0 && this.setEdgeBias(e.edgeBias), e.convergenceSpeedUp !== void 0 && this.setConvergenceSpeed(e.convergenceSpeedUp), e.adaptiveSamplingMax !== void 0 && this.setAdaptiveSamplingMax(e.adaptiveSamplingMax);
9605
9605
  }
9606
9606
  dispose() {
9607
- this._computeNode?.dispose(), this._heatmapComputeNode?.dispose(), this._heatmapStorageTex?.dispose(), this._outputStorageTex?.dispose(), this.heatmapTarget?.dispose(), this.helper?.dispose(), this._computeNode = null, this._heatmapComputeNode = null, this._heatmapStorageTex = null, this._outputStorageTex = null, this.heatmapTarget = null, this.helper = null;
9607
+ this._computeNode?.dispose(), this._heatmapComputeNode?.dispose(), this._heatmapStorageTex?.dispose(), this._outputStorageTex?.dispose(), this.heatmapTarget?.dispose(), this._varianceTexNode?.dispose(), this.helper?.dispose(), this._computeNode = null, this._heatmapComputeNode = null, this._heatmapStorageTex = null, this._outputStorageTex = null, this.heatmapTarget = null, this._varianceTexNode = null, this.helper = null;
9608
9608
  }
9609
9609
  }, Tc = class extends Sn {
9610
9610
  constructor(e, t = {}) {
@@ -9684,7 +9684,7 @@ var _c = class extends Sn {
9684
9684
  ];
9685
9685
  }
9686
9686
  dispose() {
9687
- this._computeNode?.dispose(), this._outputStorageTex?.dispose(), this.outputTarget?.dispose();
9687
+ this._computeNode?.dispose(), this._outputStorageTex?.dispose(), this.outputTarget?.dispose(), this._inputTexNode?.dispose();
9688
9688
  }
9689
9689
  }, Ec = 256, Dc = -8, Oc = 6 - Dc, kc = Oc / Ec, Ac = 1e4, jc = /* @__PURE__ */ K("\n fn adaptExposure(\n geoMean: f32,\n prevExposure: f32,\n keyValue: f32,\n minExp: f32,\n maxExp: f32,\n speedBright: f32,\n speedDark: f32,\n dt: f32,\n isFirstFrame: f32\n ) -> vec4f {\n\n let targetExp = clamp( keyValue / max( geoMean, 0.001 ), minExp, maxExp );\n var newExposure = targetExp;\n\n // Temporal smoothing (skip on first frame)\n if ( isFirstFrame < 0.5 ) {\n\n // Asymmetric speed: brighter scenes adapt faster\n let speed = select( speedDark, speedBright, targetExp < prevExposure );\n let alpha = 1.0 - exp( -dt * speed );\n newExposure = mix( prevExposure, targetExp, alpha );\n\n }\n\n return vec4f( newExposure, geoMean, targetExp, 1.0 );\n\n }\n"), Mc = class extends Sn {
9690
9690
  constructor(e, t = {}) {
@@ -9838,7 +9838,7 @@ var _c = class extends Sn {
9838
9838
  e.keyValue !== void 0 && (this.keyValueU.value = e.keyValue), e.minExposure !== void 0 && (this.minExposureU.value = e.minExposure), e.maxExposure !== void 0 && (this.maxExposureU.value = e.maxExposure), e.adaptSpeedBright !== void 0 && (this.adaptSpeedBrightU.value = e.adaptSpeedBright), e.adaptSpeedDark !== void 0 && (this.adaptSpeedDarkU.value = e.adaptSpeedDark), e.lowPercentile !== void 0 && (this.lowPercentileU.value = e.lowPercentile), e.highPercentile !== void 0 && (this.highPercentileU.value = e.highPercentile), e.centerWeight !== void 0 && (this.centerWeightU.value = e.centerWeight);
9839
9839
  }
9840
9840
  dispose() {
9841
- this._downsampleComputeNode?.dispose(), this._histogramComputeNode?.dispose(), this._histogramAnalyzeNode?.dispose(), this._adaptationComputeNode?.dispose(), this._downsampleTarget?.dispose(), this._downsampleStorageTex?.dispose(), this._reductionStorageTex?.dispose(), this._reductionReadTarget?.dispose(), this._readbackBuffer?.dispose();
9841
+ this._downsampleComputeNode?.dispose(), this._histogramComputeNode?.dispose(), this._histogramAnalyzeNode?.dispose(), this._adaptationComputeNode?.dispose(), this._downsampleTarget?.dispose(), this._downsampleStorageTex?.dispose(), this._reductionStorageTex?.dispose(), this._reductionReadTarget?.dispose(), this._readbackBuffer?.dispose(), this._inputTexNode?.dispose(), this._reductionReadTexNode?.dispose();
9842
9842
  }
9843
9843
  };
9844
9844
  //#endregion
@@ -9941,7 +9941,7 @@ var Fc = class extends Sn {
9941
9941
  this._pass1NodeA && (this._pass1NodeA.dispatchSize = n), this._pass1NodeB && (this._pass1NodeB.dispatchSize = n), this._pass2Node && (this._pass2Node.dispatchSize = n), this._resetCache();
9942
9942
  }
9943
9943
  dispose() {
9944
- this._pass1NodeA?.dispose(), this._pass1NodeB?.dispose(), this._pass2Node?.dispose(), this._cacheTexA.dispose(), this._cacheTexB.dispose(), this._prevNDTexA.dispose(), this._prevNDTexB.dispose(), this._outputTex.dispose();
9944
+ this._pass1NodeA?.dispose(), this._pass1NodeB?.dispose(), this._pass2Node?.dispose(), this._cacheTexA.dispose(), this._cacheTexB.dispose(), this._prevNDTexA.dispose(), this._prevNDTexB.dispose(), this._outputTex.dispose(), this._colorTexNode?.dispose(), this._ndTexNode?.dispose(), this._motionTexNode?.dispose(), this._readCacheTexNode?.dispose(), this._readPrevNDTexNode?.dispose(), this._readPass1CacheTexNode?.dispose();
9945
9945
  }
9946
9946
  updateParameters(e) {
9947
9947
  e.temporalAlpha !== void 0 && (this.temporalAlpha.value = e.temporalAlpha), e.phiNormal !== void 0 && (this.phiNormal.value = e.phiNormal), e.phiDepth !== void 0 && (this.phiDepth.value = e.phiDepth), e.maxHistory !== void 0 && (this.maxHistory.value = e.maxHistory), e.spatialRadius !== void 0 && (this.spatialRadius.value = e.spatialRadius), e.spatialWeight !== void 0 && (this.spatialWeight.value = e.spatialWeight);
@@ -10025,7 +10025,7 @@ var Fc = class extends Sn {
10025
10025
  this._transparentBackground.value = +!!e;
10026
10026
  }
10027
10027
  dispose() {
10028
- this.displayMaterial?.dispose(), this.displayQuad = null;
10028
+ this._displayTexNode?.dispose(), this.displayMaterial?.dispose(), this.displayQuad = null;
10029
10029
  }
10030
10030
  }, Lc = class {
10031
10031
  constructor() {
@@ -10207,7 +10207,7 @@ var Fc = class extends Sn {
10207
10207
  let t = this.stages.findIndex((t) => t.name === e);
10208
10208
  if (t > -1) {
10209
10209
  let e = this.stages[t];
10210
- return this.stages.splice(t, 1), e.dispose && e.dispose(), !0;
10210
+ return this.stages.splice(t, 1), e.dispose && e.dispose(), this.stats.timings.delete(e.name), !0;
10211
10211
  }
10212
10212
  return !1;
10213
10213
  }
@@ -11934,14 +11934,14 @@ var dl = new Float32Array(3), fl = {
11934
11934
  }, this._gpuInputBufferSize = {
11935
11935
  width: 0,
11936
11936
  height: 0
11937
- }, this._gpuInputPadBuffer = null, this._gpuInputPaddedRowBytes = 0, this._alphaReadbackBuffer = null, this._cachedAlpha = null, this._cachedAlphaWidth = 0, this.config = {
11937
+ }, this._gpuInputPadBuffer = null, this._gpuInputPaddedRowBytes = 0, this._alphaReadbackBuffer = null, this._alphaReadbackMapped = !1, this._cachedAlpha = null, this._cachedAlphaWidth = 0, this.config = {
11938
11938
  ...fl.DEFAULT_OPTIONS,
11939
11939
  ...i
11940
11940
  }, this.enabled = this.config.enableOIDN, this.quality = this.config.oidnQuality, this.debugGbufferMaps = this.config.debugGbufferMaps, this.tileSize = this.config.tileSize, this.state = {
11941
11941
  isDenoising: !1,
11942
11942
  isLoading: !1,
11943
11943
  abortController: null
11944
- }, this.currentTZAUrl = null, this.unet = null, this.debugHelpers = null, this._lastAlbedoTexture = null, this._lastNormalTexture = null, this._initialize().catch((e) => {
11944
+ }, this._pendingStagingBuffers = /* @__PURE__ */ new Set(), this.currentTZAUrl = null, this.unet = null, this.debugHelpers = null, this._lastAlbedoTexture = null, this._lastNormalTexture = null, this._initialize().catch((e) => {
11945
11945
  console.error("Failed to initialize OIDNDenoiser:", e), this.dispatchEvent({
11946
11946
  type: "error",
11947
11947
  error: e
@@ -12129,7 +12129,10 @@ var dl = new Float32Array(3), fl = {
12129
12129
  }), this._gpuInputPaddedRowBytes = c);
12130
12130
  }
12131
12131
  _destroyGPUInputBuffers() {
12132
- this._gpuInputBuffers.color?.destroy(), this._gpuInputBuffers.albedo?.destroy(), this._gpuInputBuffers.normal?.destroy(), this._gpuInputPadBuffer?.destroy(), this._alphaReadbackBuffer?.destroy(), this._gpuInputBuffers = {
12132
+ if (this._gpuInputBuffers.color?.destroy(), this._gpuInputBuffers.albedo?.destroy(), this._gpuInputBuffers.normal?.destroy(), this._gpuInputPadBuffer?.destroy(), this._alphaReadbackMapped && this._alphaReadbackBuffer) try {
12133
+ this._alphaReadbackBuffer.unmap();
12134
+ } catch {}
12135
+ this._alphaReadbackBuffer?.destroy(), this._alphaReadbackMapped = !1, this._gpuInputBuffers = {
12133
12136
  color: null,
12134
12137
  albedo: null,
12135
12138
  normal: null
@@ -12146,10 +12149,16 @@ var dl = new Float32Array(3), fl = {
12146
12149
  usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
12147
12150
  }));
12148
12151
  let i = this._alphaReadbackBuffer, a = e.createCommandEncoder();
12149
- a.copyBufferToBuffer(this._gpuInputBuffers.color, 0, i, 0, r), e.queue.submit([a.finish()]), await i.mapAsync(GPUMapMode.READ);
12152
+ a.copyBufferToBuffer(this._gpuInputBuffers.color, 0, i, 0, r), e.queue.submit([a.finish()]), this._alphaReadbackMapped = !0;
12153
+ try {
12154
+ await i.mapAsync(GPUMapMode.READ);
12155
+ } catch {
12156
+ this._alphaReadbackMapped = !1;
12157
+ return;
12158
+ }
12150
12159
  let o = new Float32Array(i.getMappedRange()), s = t * n, c = new Uint8Array(s);
12151
12160
  for (let e = 0; e < s; e++) c[e] = Math.min(Math.max(o[e * 4 + 3] * 255, 0), 255) | 0;
12152
- i.unmap(), this._cachedAlpha = c, this._cachedAlphaWidth = t;
12161
+ i.unmap(), this._alphaReadbackMapped = !1, this._cachedAlpha = c, this._cachedAlphaWidth = t;
12153
12162
  }
12154
12163
  _executeWithAbortGPU(e) {
12155
12164
  return new Promise((t, n) => {
@@ -12183,7 +12192,9 @@ var dl = new Float32Array(3), fl = {
12183
12192
  let c = o * 16, l = o * s * 16, u = r.createBuffer({
12184
12193
  size: l,
12185
12194
  usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
12186
- }), d = r.createCommandEncoder();
12195
+ });
12196
+ this._pendingStagingBuffers.add(u);
12197
+ let d = r.createCommandEncoder();
12187
12198
  for (let t = 0; t < s; t++) {
12188
12199
  let r = ((n.y + t) * i + n.x) * 16, a = t * c;
12189
12200
  d.copyBufferToBuffer(e.data, r, u, a, c);
@@ -12197,7 +12208,9 @@ var dl = new Float32Array(3), fl = {
12197
12208
  t.data[s + 3] = c[(n.y + r) * l + n.x + e];
12198
12209
  } else t.data[s + 3] = 255;
12199
12210
  }
12200
- u.unmap(), u.destroy(), this.ctx.putImageData(t, n.x, n.y);
12211
+ u.unmap(), u.destroy(), this._pendingStagingBuffers.delete(u), this.ctx.putImageData(t, n.x, n.y);
12212
+ }).catch(() => {
12213
+ u.destroy(), this._pendingStagingBuffers.delete(u);
12201
12214
  });
12202
12215
  }
12203
12216
  });
@@ -12213,17 +12226,22 @@ var dl = new Float32Array(3), fl = {
12213
12226
  label: "oidn-output-staging",
12214
12227
  size: i,
12215
12228
  usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
12216
- }), o = r.createCommandEncoder({ label: "oidn-readback" });
12217
- o.copyBufferToBuffer(e, 0, a, 0, i), r.queue.submit([o.finish()]), await a.mapAsync(GPUMapMode.READ);
12218
- let s = new Float32Array(a.getMappedRange()), c = new ImageData(t, n), l = this.getExposure(), u = this.getSaturation(), d = rl.get(this.getToneMapping()) || rl.get(Ee), f = this._cachedAlpha;
12219
- for (let e = 0, t = s.length; e < t; e += 4) {
12220
- let t = s[e] * l, n = s[e + 1] * l, r = s[e + 2] * l;
12221
- u !== 1 && (dl[0] = t, dl[1] = n, dl[2] = r, cl(dl, u), t = dl[0], n = dl[1], r = dl[2]), d(t, n, r, 1, dl), c.data[e] = dl[0] ** il * 255 | 0, c.data[e + 1] = dl[1] ** il * 255 | 0, c.data[e + 2] = dl[2] ** il * 255 | 0, c.data[e + 3] = f ? f[e >> 2] : 255;
12229
+ });
12230
+ try {
12231
+ let o = r.createCommandEncoder({ label: "oidn-readback" });
12232
+ o.copyBufferToBuffer(e, 0, a, 0, i), r.queue.submit([o.finish()]), await a.mapAsync(GPUMapMode.READ);
12233
+ let s = new Float32Array(a.getMappedRange()), c = new ImageData(t, n), l = this.getExposure(), u = this.getSaturation(), d = rl.get(this.getToneMapping()) || rl.get(Ee), f = this._cachedAlpha;
12234
+ for (let e = 0, t = s.length; e < t; e += 4) {
12235
+ let t = s[e] * l, n = s[e + 1] * l, r = s[e + 2] * l;
12236
+ u !== 1 && (dl[0] = t, dl[1] = n, dl[2] = r, cl(dl, u), t = dl[0], n = dl[1], r = dl[2]), d(t, n, r, 1, dl), c.data[e] = dl[0] ** il * 255 | 0, c.data[e + 1] = dl[1] ** il * 255 | 0, c.data[e + 2] = dl[2] ** il * 255 | 0, c.data[e + 3] = f ? f[e >> 2] : 255;
12237
+ }
12238
+ a.unmap(), this.ctx.putImageData(c, 0, 0);
12239
+ } finally {
12240
+ a.destroy();
12222
12241
  }
12223
- a.unmap(), a.destroy(), this.ctx.putImageData(c, 0, 0);
12224
12242
  }
12225
12243
  abort() {
12226
- !this.enabled || !this.state.isDenoising || (this.state.abortController?.abort(), this.input.style.opacity = "1", this.state.isDenoising = !1, this.dispatchEvent({ type: "end" }), console.log("Denoising aborted"));
12244
+ !this.enabled || !this.state.isDenoising || (this.state.abortController?.abort(), this._destroyPendingStagingBuffers(), this.input.style.opacity = "1", this.state.isDenoising = !1, this.dispatchEvent({ type: "end" }), console.log("Denoising aborted"));
12227
12245
  }
12228
12246
  setSize(e, t) {
12229
12247
  if (e <= 0 || t <= 0) throw Error(`Invalid dimensions: ${e}x${t}`);
@@ -12260,8 +12278,12 @@ var dl = new Float32Array(3), fl = {
12260
12278
  }
12261
12279
  this.debugHelpers.albedo.update(), this.debugHelpers.normal.update();
12262
12280
  }
12281
+ _destroyPendingStagingBuffers() {
12282
+ for (let e of this._pendingStagingBuffers) e.destroy();
12283
+ this._pendingStagingBuffers.clear();
12284
+ }
12263
12285
  dispose() {
12264
- this.abort(), this.unet?.dispose(), this._destroyGPUInputBuffers(), this.debugHelpers &&= (this.debugHelpers.albedo?.dispose(), this.debugHelpers.normal?.dispose(), null), this._lastAlbedoTexture = null, this._lastNormalTexture = null, this.output?.parentNode && this.output.remove(), this.unet = null, this.ctx = null, this.state.abortController = null, this.removeAllListeners?.(), console.log("OIDNDenoiser disposed");
12286
+ this.abort(), this._destroyPendingStagingBuffers(), this.unet?.dispose(), this._destroyGPUInputBuffers(), this.debugHelpers &&= (this.debugHelpers.albedo?.dispose(), this.debugHelpers.normal?.dispose(), null), this._lastAlbedoTexture = null, this._lastNormalTexture = null, this.output?.parentNode && this.output.remove(), this.unet = null, this.ctx = null, this.state.abortController = null, this.removeAllListeners?.(), console.log("OIDNDenoiser disposed");
12265
12287
  }
12266
12288
  }, ml = "" + new URL("assets/AIUpscalerWorker-D58dcMrY.js", import.meta.url).href, hl = "https://huggingface.co/notaneimu/onnx-image-models/resolve/main/", gl = {
12267
12289
  QUALITY_PRESETS: {
@@ -12294,7 +12316,7 @@ var dl = new Float32Array(3), fl = {
12294
12316
  isUpscaling: !1,
12295
12317
  isLoading: !1,
12296
12318
  abortController: null
12297
- }, this._worker = null, this._currentModelUrl = null, this._tileId = 0, this._upscaledAlpha = null, this._upscaledAlphaWidth = 0, this._backupCanvas = null, this._baseWidth = e.width, this._baseHeight = e.height, this._hdrStagingBuffer = null, this._hdrStagingWidth = 0, this._hdrStagingHeight = 0;
12319
+ }, this._worker = null, this._currentModelUrl = null, this._tileId = 0, this._pendingWorkerHandlers = /* @__PURE__ */ new Set(), this._upscaledAlpha = null, this._upscaledAlphaWidth = 0, this._backupCanvas = null, this._baseWidth = e.width, this._baseHeight = e.height, this._hdrStagingBuffer = null, this._hdrStagingWidth = 0, this._hdrStagingHeight = 0;
12298
12320
  }
12299
12321
  async _ensureSession() {
12300
12322
  let e = gl.QUALITY_PRESETS[this.quality];
@@ -12370,11 +12392,11 @@ var dl = new Float32Array(3), fl = {
12370
12392
  height: this._baseHeight
12371
12393
  })), !1;
12372
12394
  } finally {
12373
- this._capturedSource = null, this._upscaledAlpha = null, this.state.isUpscaling && (this.state.isUpscaling = !1, this.state.abortController = null, this.dispatchEvent({ type: "end" }));
12395
+ this._capturedSource = null, this._upscaledAlpha = null, this._backupCanvas = null, this.state.isUpscaling && (this.state.isUpscaling = !1, this.state.abortController = null, this.dispatchEvent({ type: "end" }));
12374
12396
  }
12375
12397
  }
12376
12398
  abort() {
12377
- this.state.isUpscaling && (this.state.abortController?.abort(), this.input.style.opacity = "1", this._restoreBackup(), this.dispatchEvent({
12399
+ this.state.isUpscaling && (this.state.abortController?.abort(), this._cleanupPendingWorkerHandlers(), this.input.style.opacity = "1", this._restoreBackup(), this.dispatchEvent({
12378
12400
  type: "resolution_changed",
12379
12401
  width: this._baseWidth,
12380
12402
  height: this._baseHeight
@@ -12470,9 +12492,9 @@ var dl = new Float32Array(3), fl = {
12470
12492
  let r = ++this._tileId;
12471
12493
  return new Promise((i, a) => {
12472
12494
  let o = (e) => {
12473
- e.data.id === r && (this._worker.removeEventListener("message", o), e.data.type === "inferred" ? i(e.data.outputData) : e.data.type === "error" && a(Error(e.data.message)));
12495
+ e.data.id === r && (this._worker.removeEventListener("message", o), this._pendingWorkerHandlers.delete(o), e.data.type === "inferred" ? i(e.data.outputData) : e.data.type === "error" && a(Error(e.data.message)));
12474
12496
  };
12475
- this._worker.addEventListener("message", o), this._worker.postMessage({
12497
+ this._pendingWorkerHandlers.add(o), this._worker.addEventListener("message", o), this._worker.postMessage({
12476
12498
  type: "infer",
12477
12499
  tileData: e,
12478
12500
  width: t,
@@ -12550,12 +12572,16 @@ var dl = new Float32Array(3), fl = {
12550
12572
  setBaseSize(e, t) {
12551
12573
  this._baseWidth = e, this._baseHeight = t;
12552
12574
  }
12575
+ _cleanupPendingWorkerHandlers() {
12576
+ if (this._worker) for (let e of this._pendingWorkerHandlers) this._worker.removeEventListener("message", e);
12577
+ this._pendingWorkerHandlers.clear();
12578
+ }
12553
12579
  async dispose() {
12554
- this.abort(), this._worker &&= (this._worker.postMessage({ type: "dispose" }), this._worker.terminate(), null), this._currentModelUrl = null, this._backupCanvas = null, this._upscaledAlpha = null, this.state.abortController = null, this._hdrStagingBuffer?.destroy(), this._hdrStagingBuffer = null, this._hdrStagingWidth = 0, this._hdrStagingHeight = 0, console.log("AIUpscaler disposed");
12580
+ this.abort(), this._cleanupPendingWorkerHandlers(), this._worker &&= (this._worker.postMessage({ type: "dispose" }), this._worker.terminate(), null), this._currentModelUrl = null, this._backupCanvas = null, this._upscaledAlpha = null, this.state.abortController = null, this._hdrStagingBuffer?.destroy(), this._hdrStagingBuffer = null, this._hdrStagingWidth = 0, this._hdrStagingHeight = 0, console.log("AIUpscaler disposed");
12555
12581
  }
12556
12582
  }, vl = class extends We {
12557
12583
  constructor({ renderer: e, mainCanvas: t, scene: n, camera: r, stages: i, pipeline: a, getExposure: o, getSaturation: s, getTransparentBg: c }) {
12558
- super(), this.renderer = e, this.mainCanvas = t, this.denoiserCanvas = this._createDenoiserCanvas(t), this.scene = n, this.camera = r, this.pipeline = a, this._stages = i, this._getExposure = o, this._getSaturation = s, this._getTransparentBg = c, this.denoiser = null, this.upscaler = null, this._lastRenderWidth = 0, this._lastRenderHeight = 0;
12584
+ super(), this.renderer = e, this.mainCanvas = t, this.denoiserCanvas = this._createDenoiserCanvas(t), this.scene = n, this.camera = r, this.pipeline = a, this._stages = i, this._getExposure = o, this._getSaturation = s, this._getTransparentBg = c, this.denoiser = null, this.upscaler = null, this._lastRenderWidth = 0, this._lastRenderHeight = 0, this._pendingStartUpscaler = null, this._denoiserStartHandler = null, this._denoiserEndHandler = null, this._upscalerResChangedHandler = null, this._upscalerStartHandler = null, this._upscalerProgressHandler = null, this._upscalerEndHandler = null;
12559
12585
  }
12560
12586
  _createDenoiserCanvas(e) {
12561
12587
  let t = e.parentNode;
@@ -12594,7 +12620,7 @@ var dl = new Float32Array(3), fl = {
12594
12620
  getSaturation: () => this._getSaturation(),
12595
12621
  getTransparentBackground: () => this._getTransparentBg(),
12596
12622
  getMRTRenderTarget: () => e?.storageTextures?.readTarget ?? null
12597
- }), this.denoiser.enabled = X.enableOIDN, this.denoiser.addEventListener("start", () => this.dispatchEvent({ type: Y.DENOISING_START })), this.denoiser.addEventListener("end", () => this.dispatchEvent({ type: Y.DENOISING_END }));
12623
+ }), this.denoiser.enabled = X.enableOIDN, this._denoiserStartHandler = () => this.dispatchEvent({ type: Y.DENOISING_START }), this._denoiserEndHandler = () => this.dispatchEvent({ type: Y.DENOISING_END }), this.denoiser.addEventListener("start", this._denoiserStartHandler), this.denoiser.addEventListener("end", this._denoiserEndHandler);
12598
12624
  }
12599
12625
  setupUpscaler() {
12600
12626
  if (!this.denoiserCanvas) return;
@@ -12611,14 +12637,14 @@ var dl = new Float32Array(3), fl = {
12611
12637
  getExposure: () => this._getEffectiveExposure(),
12612
12638
  getToneMapping: () => this._getToneMapping(),
12613
12639
  getSaturation: () => this._getSaturation()
12614
- }), this.upscaler.enabled = X.enableUpscaler || !1, this.upscaler.addEventListener("resolution_changed", (e) => this.dispatchEvent({
12640
+ }), this.upscaler.enabled = X.enableUpscaler || !1, this._upscalerResChangedHandler = (e) => this.dispatchEvent({
12615
12641
  type: "resolution_changed",
12616
12642
  width: e.width,
12617
12643
  height: e.height
12618
- })), this.upscaler.addEventListener("start", () => this.dispatchEvent({ type: Y.UPSCALING_START })), this.upscaler.addEventListener("progress", (e) => this.dispatchEvent({
12644
+ }), this._upscalerStartHandler = () => this.dispatchEvent({ type: Y.UPSCALING_START }), this._upscalerProgressHandler = (e) => this.dispatchEvent({
12619
12645
  type: Y.UPSCALING_PROGRESS,
12620
12646
  progress: e.progress
12621
- })), this.upscaler.addEventListener("end", () => this.dispatchEvent({ type: Y.UPSCALING_END }));
12647
+ }), this._upscalerEndHandler = () => this.dispatchEvent({ type: Y.UPSCALING_END }), this.upscaler.addEventListener("resolution_changed", this._upscalerResChangedHandler), this.upscaler.addEventListener("start", this._upscalerStartHandler), this.upscaler.addEventListener("progress", this._upscalerProgressHandler), this.upscaler.addEventListener("end", this._upscalerEndHandler);
12622
12648
  }
12623
12649
  setDenoiserStrategy(e, t) {
12624
12650
  let n = this._stages;
@@ -12649,18 +12675,21 @@ var dl = new Float32Array(3), fl = {
12649
12675
  let t = this._stages;
12650
12676
  t.adaptiveSampling && (t.adaptiveSampling.enabled = e, t.adaptiveSampling.toggleHelper(!1)), e ? t.variance && (t.variance.enabled = !0) : t.asvgf?.enabled || t.variance && (t.variance.enabled = !1), !e && this.pipeline?.context && !t.asvgf?.enabled && this.pipeline.context.removeTexture("variance:output");
12651
12677
  }
12678
+ _cleanupCompletionListener() {
12679
+ this._pendingStartUpscaler && this.denoiser && this.denoiser.removeEventListener("end", this._pendingStartUpscaler), this._pendingStartUpscaler = null;
12680
+ }
12652
12681
  onRenderComplete({ isStillComplete: e, context: t }) {
12653
- (this.denoiser?.enabled || this.upscaler?.enabled) && this.denoiserCanvas && (this.denoiserCanvas.style.display = "block");
12682
+ this._cleanupCompletionListener(), (this.denoiser?.enabled || this.upscaler?.enabled) && this.denoiserCanvas && (this.denoiserCanvas.style.display = "block");
12654
12683
  let n = () => {
12655
- e() && this.upscaler?.enabled && this.upscaler.start();
12684
+ this._pendingStartUpscaler = null, e() && this.upscaler?.enabled && this.upscaler.start();
12656
12685
  };
12657
- this.denoiser?.enabled ? (this.denoiser.addEventListener("end", n, { once: !0 }), this.denoiser.start()) : (this.upscaler?.enabled && this._stages.display && t && this._stages.display.render(t), n());
12686
+ this.denoiser?.enabled ? (this._pendingStartUpscaler = n, this.denoiser.addEventListener("end", n, { once: !0 }), this.denoiser.start()) : (this.upscaler?.enabled && this._stages.display && t && this._stages.display.render(t), n());
12658
12687
  }
12659
12688
  abort(e) {
12660
- e && (e.style.opacity = "1"), this.upscaler && this.upscaler.abort(), this.denoiser && (this.denoiser.enabled && this.denoiser.abort(), this.denoiser.output && (this.denoiser.output.style.display = "none"));
12689
+ this._cleanupCompletionListener(), e && (e.style.opacity = "1"), this.upscaler && this.upscaler.abort(), this.denoiser && (this.denoiser.enabled && this.denoiser.abort(), this.denoiser.output && (this.denoiser.output.style.display = "none"));
12661
12690
  }
12662
12691
  dispose() {
12663
- this.denoiser &&= (this.denoiser.dispose(), null), this.upscaler &&= (this.upscaler.dispose(), null), this.denoiserCanvas?.parentNode && (this.denoiserCanvas.parentNode.removeChild(this.denoiserCanvas), this.denoiserCanvas = null);
12692
+ this._cleanupCompletionListener(), this.denoiser &&= (this._denoiserStartHandler && this.denoiser.removeEventListener("start", this._denoiserStartHandler), this._denoiserEndHandler && this.denoiser.removeEventListener("end", this._denoiserEndHandler), this.denoiser.dispose(), null), this.upscaler &&= (this._upscalerResChangedHandler && this.upscaler.removeEventListener("resolution_changed", this._upscalerResChangedHandler), this._upscalerStartHandler && this.upscaler.removeEventListener("start", this._upscalerStartHandler), this._upscalerProgressHandler && this.upscaler.removeEventListener("progress", this._upscalerProgressHandler), this._upscalerEndHandler && this.upscaler.removeEventListener("end", this._upscalerEndHandler), this.upscaler.dispose(), null), this._denoiserStartHandler = null, this._denoiserEndHandler = null, this._upscalerResChangedHandler = null, this._upscalerStartHandler = null, this._upscalerProgressHandler = null, this._upscalerEndHandler = null, this.denoiserCanvas?.parentNode && (this.denoiserCanvas.parentNode.removeChild(this.denoiserCanvas), this.denoiserCanvas = null);
12664
12693
  }
12665
12694
  setOverlayManager(e) {
12666
12695
  this._overlayManager = e;