Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/scene/gsplat/gsplat-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ class GSplatInstance {
const orderTarget = this.orderBuffer ?? this.orderTexture;
this.sorter = new GSplatSorter(device, options.scene);
this.sorter.init(orderTarget, numSplats, centers, chunks);
this.sorter.on('updated', (count) => {
this.meshInstance.instancingCount = Math.ceil(count / GSplatResourceBase.instanceSize);
this.material.setParameter('numSplats', count);
});

this.setHighQualitySH(options.highQualitySH ?? false);
}
Expand Down Expand Up @@ -215,8 +211,12 @@ class GSplatInstance {

update() {

// Apply deferred sort results (at most one upload per frame).
this.sorter?.applyPendingSorted();
// Apply deferred sort results (at most one GPU upload per frame).
const count = this.sorter?.applyPendingSorted() ?? -1;
if (count >= 0) {
this.meshInstance.instancingCount = Math.ceil(count / GSplatResourceBase.instanceSize);
this.material.setParameter('numSplats', count);
}

if (this.cameras.length > 0) {

Expand Down
14 changes: 10 additions & 4 deletions src/scene/gsplat/gsplat-sorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ class GSplatSorter extends EventHandler {
order: oldOrder
}, [oldOrder]);

// Store result for deferred application. Only the latest result is kept,
// Store result for deferred GPU upload. Only the latest result is kept,
// avoiding redundant uploads when multiple worker messages arrive between frames.
this.orderData = newOrder;
this.pendingSorted = {
count: msgData.count,
data: new Uint32Array(newOrder)
};

// Notify immediately so listeners can request a new frame (e.g. renderNextFrame).
this.fire('updated');
};

const workerSource = `(${SortWorker.toString()})()`;
Expand Down Expand Up @@ -118,16 +121,19 @@ class GSplatSorter extends EventHandler {
}

/**
* Applies the most recent pending sorted result (if any), uploading to GPU
* and firing the 'updated' event. Call once per frame from the instance's update().
* Applies the most recent pending sorted result (if any), uploading order data to the GPU.
* Call once per frame from the instance's update().
*
* @returns {number} The splat count from the applied result, or -1 if nothing was pending.
*/
applyPendingSorted() {
if (this.pendingSorted) {
const { count, data } = this.pendingSorted;
this.pendingSorted = null;
this.uploadStream.upload(data, this.target);
this.fire('updated', count);
return count;
}
return -1;
}

setMapping(mapping) {
Expand Down