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
5 changes: 1 addition & 4 deletions src/framework/asset/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,7 @@ class Asset extends EventHandler {

// destroy resources
for (let i = 0; i < old.length; ++i) {
const resource = old[i];
if (resource && resource.destroy) {
resource.destroy();
}
old[i]?.destroy?.();
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/framework/parsers/sogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ class SogsParser {

const textureAssets = subs.map(sub => textures[sub]).flat();

// When the parent gsplat asset unloads, remove and unload child texture assets
asset.once('unload', () => {
textureAssets.forEach((t) => {
// remove from registry
assets.remove(t);

// destroys resource
t.unload();
});
});

combineProgress(asset, textureAssets);

textureAssets.forEach(t => assets.load(t));
Expand All @@ -157,10 +168,14 @@ class SogsParser {
const decompress = asset.data?.decompress;

if (!decompress) {
if (!this.app?.graphicsDevice || this.app?.graphicsDevice?._destroyed) return;

// no need to prepare gpu data if decompressing
await data.prepareGpuData();
}

if (!this.app?.graphicsDevice || this.app?.graphicsDevice?._destroyed) return;

const resource = decompress ?
new GSplatResource(this.app.graphicsDevice, await data.decompress()) :
new GSplatSogsResource(this.app.graphicsDevice, data);
Expand Down
8 changes: 8 additions & 0 deletions src/platform/graphics/graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ class GraphicsDevice extends EventHandler {
*/
gpuProfiler;

/**
* @type {boolean}
* @ignore
*/
_destroyed = false;

defaultClearOptions = {
color: [0, 0, 0, 1],
depth: 1,
Expand Down Expand Up @@ -539,6 +545,8 @@ class GraphicsDevice extends EventHandler {

this.gpuProfiler?.destroy();
this.gpuProfiler = null;

this._destroyed = true;
}

onDestroyShader(shader) {
Expand Down
3 changes: 3 additions & 0 deletions src/platform/graphics/webgl/webgl-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,9 @@ class WebglGraphicsDevice extends GraphicsDevice {
return new Promise((resolve, reject) => {
this.readPixelsAsync(x, y, width, height, data).then((data) => {

// return if the device was destroyed
if (this._destroyed) return;

// destroy RT if we created it
if (!options.renderTarget) {
renderTarget.destroy();
Expand Down
16 changes: 15 additions & 1 deletion src/scene/gsplat/gsplat-sogs-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ class GSplatSogsData {

packedShN;

destroy() {
// Marked when resource is destroyed, to abort any in-flight async preparation
destroyed = false;

_destroyGpuResources() {
this.means_l?.destroy();
this.means_u?.destroy();
this.quats?.destroy();
Expand All @@ -180,6 +183,11 @@ class GSplatSogsData {
this.packedShN?.destroy();
}

destroy() {
this.destroyed = true;
this._destroyGpuResources();
}

createIter(p, r, s, c, sh) {
return new GSplatSogsIterator(this, p, r, s, c, sh);
}
Expand Down Expand Up @@ -438,9 +446,13 @@ class GSplatSogsData {
const { device, height, width } = this.means_l;

// copy back means_l and means_u data so cpu reorder has access to it
if (this.destroyed || device._destroyed) return; // skip the rest if the resource was destroyed
this.means_l._levels[0] = await readImageDataAsync(this.means_l);

if (this.destroyed || device._destroyed) return; // skip the rest if the resource was destroyed
this.means_u._levels[0] = await readImageDataAsync(this.means_u);

if (this.destroyed || device._destroyed) return; // skip the rest if the resource was destroyed
this.packedTexture = new Texture(device, {
name: 'sogsPackedTexture',
width,
Expand Down Expand Up @@ -472,8 +484,10 @@ class GSplatSogsData {
}
});

if (this.destroyed || device._destroyed) return; // skip the rest if the resource was destroyed
this.packGpuMemory();
if (this.packedShN) {
if (this.destroyed || device._destroyed) return; // skip the rest if the resource was destroyed
this.packShMemory();
}
}
Expand Down