Skip to content

Commit ac7faf6

Browse files
authored
Sogs load tweaks (#7667)
1 parent 255bea9 commit ac7faf6

File tree

3 files changed

+50
-54
lines changed

3 files changed

+50
-54
lines changed

src/framework/parsers/sogs.js

+2-35
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ import { GSplatSogsData } from '../../scene/gsplat/gsplat-sogs-data.js';
88
* @import { ResourceHandlerCallback } from '../handlers/handler.js'
99
*/
1010

11-
const readImageDataAsync = (texture) => {
12-
return texture.read(0, 0, texture.width, texture.height, {
13-
mipLevel: 0,
14-
face: 0,
15-
immediate: true
16-
});
17-
};
18-
1911
class SogsParser {
2012
/** @type {AppBase} */
2113
app;
@@ -66,18 +58,10 @@ class SogsParser {
6658
// wait for all textures to complete loading
6759
await Promise.allSettled(promises);
6860

69-
// sh palette has 64 sh entries per row. use width to calculate number of bands
70-
const widths = {
71-
192: 1, // 64 * 3
72-
512: 2, // 64 * 8
73-
960: 3 // 64 * 15
74-
};
75-
7661
// construct the gsplat resource
7762
const data = new GSplatSogsData();
7863
data.meta = meta;
7964
data.numSplats = meta.means.shape[0];
80-
data.shBands = widths[textures.shN?.[0]?.resource?.width] ?? 0;
8165
data.means_l = textures.means[0].resource;
8266
data.means_u = textures.means[1].resource;
8367
data.quats = textures.quats[0].resource;
@@ -87,27 +71,10 @@ class SogsParser {
8771
data.sh_labels = textures.shN?.[1]?.resource;
8872

8973
if (asset.data?.reorder ?? true) {
90-
// copy back means_l and means_u data from gpu so cpu reorder has access to it
91-
data.means_l._levels[0] = await readImageDataAsync(data.means_l);
92-
data.means_u._levels[0] = await readImageDataAsync(data.means_u);
93-
data.reorderData();
74+
await data.reorderData();
9475
}
9576

96-
let resource;
97-
if (asset.data?.decompress) {
98-
// copy back gpu texture data so cpu iterator has access to it
99-
const { means_l, means_u, quats, scales, sh0, sh_labels, sh_centroids } = data;
100-
means_l._levels[0] = await readImageDataAsync(means_l);
101-
means_u._levels[0] = await readImageDataAsync(means_u);
102-
quats._levels[0] = await readImageDataAsync(quats);
103-
scales._levels[0] = await readImageDataAsync(scales);
104-
sh0._levels[0] = await readImageDataAsync(sh0);
105-
sh_labels._levels[0] = await readImageDataAsync(sh_labels);
106-
sh_centroids._levels[0] = await readImageDataAsync(sh_centroids);
107-
resource = new GSplatResource(this.app, data.decompress(), []);
108-
} else {
109-
resource = new GSplatResource(this.app, data, []);
110-
}
77+
const resource = new GSplatResource(this.app, (asset.data?.decompress) ? (await data.decompress()) : data, []);
11178

11279
callback(null, resource);
11380
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export { Sky } from './scene/skybox/sky.js';
236236
export { GSplatData } from './scene/gsplat/gsplat-data.js';
237237
export { GSplat } from './scene/gsplat/gsplat.js';
238238
export { GSplatInstance } from './scene/gsplat/gsplat-instance.js';
239+
export { GSplatSogsData } from './scene/gsplat/gsplat-sogs-data.js';
239240

240241
// FRAMEWORK
241242
export * from './framework/constants.js';

src/scene/gsplat/gsplat-sogs-data.js

+47-19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ const reorderFS = /* glsl */`
3838
}
3939
`;
4040

41+
const readImageDataAsync = (texture) => {
42+
return texture.read(0, 0, texture.width, texture.height, {
43+
mipLevel: 0,
44+
face: 0,
45+
immediate: true
46+
});
47+
};
48+
4149
const resolve = (scope, values) => {
4250
for (const key in values) {
4351
scope.resolve(key).setValue(values[key]);
@@ -129,8 +137,6 @@ class GSplatSogsData {
129137

130138
numSplats;
131139

132-
shBands;
133-
134140
means_l;
135141

136142
means_u;
@@ -192,7 +198,17 @@ class GSplatSogsData {
192198
return true;
193199
}
194200

195-
decompress() {
201+
get shBands() {
202+
// sh palette has 64 sh entries per row. use width to calculate number of bands
203+
const widths = {
204+
192: 1, // 64 * 3
205+
512: 2, // 64 * 8
206+
960: 3 // 64 * 15
207+
};
208+
return widths[this.sh_centroids?.resource?.width] ?? 0;
209+
}
210+
211+
async decompress() {
196212
const members = [
197213
'x', 'y', 'z',
198214
'f_dc_0', 'f_dc_1', 'f_dc_2', 'opacity',
@@ -202,6 +218,16 @@ class GSplatSogsData {
202218

203219
const { shBands } = this;
204220

221+
// copy back gpu texture data so cpu iterator has access to it
222+
const { means_l, means_u, quats, scales, sh0, sh_labels, sh_centroids } = this;
223+
means_l._levels[0] = await readImageDataAsync(means_l);
224+
means_u._levels[0] = await readImageDataAsync(means_u);
225+
quats._levels[0] = await readImageDataAsync(quats);
226+
scales._levels[0] = await readImageDataAsync(scales);
227+
sh0._levels[0] = await readImageDataAsync(sh0);
228+
sh_labels._levels[0] = await readImageDataAsync(sh_labels);
229+
sh_centroids._levels[0] = await readImageDataAsync(sh_centroids);
230+
205231
// allocate spherical harmonics data
206232
if (shBands > 0) {
207233
const shMembers = [];
@@ -362,23 +388,25 @@ class GSplatSogsData {
362388
return order;
363389
}
364390

365-
reorderData() {
366-
if (!this.orderTexture) {
367-
const { device, height, width } = this.means_l;
368-
369-
this.orderTexture = new Texture(device, {
370-
name: 'orderTexture',
371-
width,
372-
height,
373-
format: PIXELFORMAT_R32U,
374-
mipmaps: false,
375-
levels: [this.calcMortonOrder()]
376-
});
391+
async reorderData() {
392+
const { device, height, width } = this.means_l;
377393

378-
device.on('devicerestored', () => {
379-
this.reorderGpuMemory();
380-
});
381-
}
394+
// copy back means_l and means_u data from gpu so cpu reorder has access to it
395+
this.means_l._levels[0] = await readImageDataAsync(this.means_l);
396+
this.means_u._levels[0] = await readImageDataAsync(this.means_u);
397+
398+
this.orderTexture = new Texture(device, {
399+
name: 'orderTexture',
400+
width,
401+
height,
402+
format: PIXELFORMAT_R32U,
403+
mipmaps: false,
404+
levels: [this.calcMortonOrder()]
405+
});
406+
407+
device.on('devicerestored', () => {
408+
this.reorderGpuMemory();
409+
});
382410

383411
this.reorderGpuMemory();
384412
}

0 commit comments

Comments
 (0)