Skip to content

Commit 4d26619

Browse files
committed
Fix reference counting for SplatAccumulator during updates
1 parent 589b6bc commit 4d26619

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/PackedSplats.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ export class PackedSplats {
464464
let maxSplats = 0;
465465
const mapping = splatCounts.map((numSplats) => {
466466
const base = maxSplats;
467-
// Generation happens in horizonal row chunks, so round up to full width
467+
// Generation happens in horizontal row chunks, so round up to full width
468468
const rounded = Math.ceil(numSplats / SPLAT_TEX_WIDTH) * SPLAT_TEX_WIDTH;
469469
maxSplats += rounded;
470470
return { base, count: numSplats };

src/SparkRenderer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ export class SparkRenderer extends THREE.Mesh {
314314
this.splatEncoding = options.splatEncoding ?? { ...DEFAULT_SPLAT_ENCODING };
315315

316316
this.active = new SplatAccumulator();
317+
this.active.refCount = 1;
317318
this.accumulatorCount = 1;
318319
this.freeAccumulators = [];
319320
// Start with the minimum of 2 total accumulators
@@ -765,7 +766,7 @@ export class SparkRenderer extends THREE.Mesh {
765766
// minimum co-orientation (dot product of quaternions)
766767
const originChanged = !withinCoorientDist({
767768
matrix1: originToWorld,
768-
matrix2: this.active.toWorld,
769+
matrix2: accumulator.toWorld,
769770
maxDistance: 0.00001,
770771
minCoorient: 0.99999,
771772
});

src/SparkViewpoint.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,15 @@ export class SparkViewpoint {
479479
// Splat mapping has not changed, so reuse the existing sorted
480480
// geometry to show updates faster. We will still fire off
481481
// a re-sort if necessary. First release old accumulator.
482+
accumulator.refCount += 1;
482483
this.spark.releaseAccumulator(this.display.accumulator);
483484
this.display.accumulator = accumulator;
485+
this.display.viewToWorld.copy(this.viewToWorld);
484486
displayed = true;
487+
488+
if (this.spark.viewpoint === this) {
489+
this.spark.prepareViewpoint(this);
490+
}
485491
}
486492
}
487493

@@ -509,15 +515,12 @@ export class SparkViewpoint {
509515
}
510516

511517
if (accumulator) {
512-
// Hold a reference to the accumulator so it isn't released
518+
// Hold a reference to the accumulator for sorting
513519
accumulator.refCount += 1;
514520
}
515521

516-
if (
517-
accumulator &&
518-
this.pending?.accumulator &&
519-
this.pending.accumulator !== this.display?.accumulator
520-
) {
522+
if (this.pending?.accumulator) {
523+
// Release the reference of the pending accumulator
521524
this.spark.releaseAccumulator(this.pending.accumulator);
522525
}
523526
this.pending = { accumulator, viewToWorld: this.viewToWorld, displayed };
@@ -533,9 +536,10 @@ export class SparkViewpoint {
533536
}
534537

535538
const { viewToWorld, displayed } = this.pending;
536-
let accumulator = this.pending.accumulator ?? this.display?.accumulator;
539+
let accumulator = this.pending.accumulator;
537540
if (!accumulator) {
538-
accumulator = this.spark.active;
541+
// Hold a reference to the accumulator while sorting
542+
accumulator = this.display?.accumulator ?? this.spark.active;
539543
accumulator.refCount += 1;
540544
}
541545
this.pending = null;
@@ -546,6 +550,10 @@ export class SparkViewpoint {
546550
this.sorting = { viewToWorld };
547551
await this.sortUpdate({ accumulator, viewToWorld, displayed });
548552
this.sorting = null;
553+
554+
// Release the reference to the accumulator
555+
this.spark.releaseAccumulator(accumulator);
556+
549557
// Continue in loop with any queued sort
550558
}
551559
}
@@ -668,13 +676,18 @@ export class SparkViewpoint {
668676
displayed?: boolean;
669677
}) {
670678
if (!this.display) {
679+
// Hold a reference to the accumulator while part of display
680+
accumulator.refCount += 1;
671681
this.display = {
672682
accumulator,
673683
viewToWorld,
674684
geometry: new SplatGeometry(ordering, activeSplats),
675685
};
676686
} else {
677687
if (!displayed && accumulator !== this.display.accumulator) {
688+
// Hold a reference to the new accumulator being displayed
689+
accumulator.refCount += 1;
690+
// Release the reference to the previously displayed accumulator
678691
this.spark.releaseAccumulator(this.display.accumulator);
679692
this.display.accumulator = accumulator;
680693
}

0 commit comments

Comments
 (0)