Skip to content

Commit 255bea9

Browse files
authored
Wgsl GS chunk updates(#7664)
1 parent c1e865f commit 255bea9

File tree

4 files changed

+86
-70
lines changed

4 files changed

+86
-70
lines changed

src/scene/shader-lib/chunks-wgsl/gsplat/vert/gsplatCommon.js

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ struct SplatCorner {
2525
#endif
2626
}
2727
28+
fn quatToMat3(R: vec4<f32>) -> mat3x3<f32> {
29+
let R2: vec4<f32> = R + R;
30+
let X: f32 = R2.x * R.w;
31+
let Y: vec4<f32> = R2.y * R;
32+
let Z: vec4<f32> = R2.z * R;
33+
let W: f32 = R2.w * R.w;
34+
35+
return mat3x3<f32>(
36+
1.0 - Z.z - W, Y.z + X, Y.w - Z.x,
37+
Y.z - X, 1.0 - Y.y - W, Z.w + Y.x,
38+
Y.w + Z.x, Z.w - Y.x, 1.0 - Y.y - Z.z
39+
);
40+
}
41+
2842
#if GSPLAT_COMPRESSED_DATA == true
2943
#include "gsplatCompressedDataVS"
3044
#include "gsplatCompressedSHVS"

src/scene/shader-lib/chunks-wgsl/gsplat/vert/gsplatCompressedData.js

-21
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,6 @@ fn unpackRotation(bits: u32) -> vec4f {
3838
return vec4f(a, b, c, m);
3939
}
4040
41-
fn quatToMat3(R: vec4f) -> mat3x3f {
42-
let qw_scalar = R.x;
43-
let qx_vec = R.y;
44-
let qy_vec = R.z;
45-
let qz_vec = R.w;
46-
47-
return mat3x3f(
48-
vec3f(1.0 - 2.0 * (qy_vec * qy_vec + qz_vec * qz_vec),
49-
2.0 * (qx_vec * qy_vec + qw_scalar * qz_vec),
50-
2.0 * (qx_vec * qz_vec - qw_scalar * qy_vec)),
51-
52-
vec3f(2.0 * (qx_vec * qy_vec - qw_scalar * qz_vec),
53-
1.0 - 2.0 * (qx_vec * qx_vec + qz_vec * qz_vec),
54-
2.0 * (qy_vec * qz_vec + qw_scalar * qx_vec)),
55-
56-
vec3f(2.0 * (qx_vec * qz_vec + qw_scalar * qy_vec),
57-
2.0 * (qy_vec * qz_vec - qw_scalar * qx_vec),
58-
1.0 - 2.0 * (qx_vec * qx_vec + qy_vec * qy_vec))
59-
);
60-
}
61-
6241
// read center
6342
fn readCenter(source: ptr<function, SplatSource>) -> vec3f {
6443
let tex_size_u = textureDimensions(chunkTexture, 0);

src/scene/shader-lib/chunks-wgsl/gsplat/vert/gsplatData.js

+1-29
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,6 @@ fn readCenter(source: ptr<function, SplatSource>) -> vec3f {
1313
return bitcast<vec3f>(tA.xyz);
1414
}
1515
16-
fn quatToMat3(R: vec4f) -> mat3x3f {
17-
let x: f32 = R.w;
18-
let y: f32 = R.x;
19-
let z: f32 = R.y;
20-
let w: f32 = R.z;
21-
22-
// pre-calculate squares and products
23-
let xx: f32 = x * x; let yy: f32 = y * y; let zz: f32 = z * z; let ww: f32 = w * w;
24-
let xy: f32 = x * y; let xz: f32 = x * z; let xw: f32 = x * w;
25-
let yz: f32 = y * z; let yw: f32 = y * w; let zw: f32 = z * w;
26-
27-
return mat3x3f(
28-
vec3f(
29-
1.0 - 2.0 * (zz + ww),
30-
2.0 * (yz + xw),
31-
2.0 * (yw - xz)
32-
), vec3f(
33-
2.0 * (yz - xw),
34-
1.0 - 2.0 * (yy + ww),
35-
2.0 * (zw + xy)
36-
), vec3f(
37-
2.0 * (yw + xz),
38-
2.0 * (zw - xy),
39-
1.0 - 2.0 * (yy + zz)
40-
)
41-
);
42-
}
43-
4416
fn unpackRotation(packed: vec3f) -> vec4f {
4517
return vec4f(packed.xyz, sqrt(max(0.0, 1.0 - dot(packed, packed))));
4618
}
@@ -49,7 +21,7 @@ fn unpackRotation(packed: vec3f) -> vec4f {
4921
fn readCovariance(source: ptr<function, SplatSource>, covA_ptr: ptr<function, vec3f>, covB_ptr: ptr<function, vec3f>) {
5022
let tB: vec4f = textureLoad(transformB, source.uv, 0);
5123
52-
let rot: mat3x3f = quatToMat3(unpackRotation(vec3f(unpack2x16float(bitcast<u32>(tAw)), tB.w)));
24+
let rot: mat3x3f = quatToMat3(unpackRotation(vec3f(unpack2x16float(bitcast<u32>(tAw)), tB.w)).wxyz);
5325
let scale: vec3f = tB.xyz;
5426
5527
// M = S * R

src/scene/shader-lib/chunks-wgsl/gsplat/vert/gsplatSH.js

+71-20
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,73 @@ fn unpack111011s(bits: u32) -> vec3f {
77
return (vec3f((vec3<u32>(bits) >> vec3<u32>(21u, 11u, 0u)) & vec3<u32>(0x7ffu, 0x3ffu, 0x7ffu)) / vec3f(2047.0, 1023.0, 2047.0)) * 2.0 - 1.0;
88
}
99
10+
struct ScaleAndSH {
11+
scale: f32,
12+
a: vec3f,
13+
b: vec3f,
14+
c: vec3f
15+
};
16+
1017
// fetch quantized spherical harmonic coefficients
11-
fn fetchScale(t_in: vec4<u32>, scale: ptr<function, f32>, a: ptr<function, vec3f>, b: ptr<function, vec3f>, c: ptr<function, vec3f>) {
12-
*scale = bitcast<f32>(t_in.x);
13-
*a = unpack111011s(t_in.y);
14-
*b = unpack111011s(t_in.z);
15-
*c = unpack111011s(t_in.w);
18+
fn fetchScale(t_in: vec4<u32>) -> ScaleAndSH {
19+
var result: ScaleAndSH;
20+
result.scale = bitcast<f32>(t_in.x);
21+
result.a = unpack111011s(t_in.y);
22+
result.b = unpack111011s(t_in.z);
23+
result.c = unpack111011s(t_in.w);
24+
return result;
1625
}
1726
27+
struct SH {
28+
a: vec3f,
29+
b: vec3f,
30+
c: vec3f,
31+
d: vec3f
32+
};
33+
1834
// fetch quantized spherical harmonic coefficients
19-
fn fetch4(t_in: vec4<u32>, a: ptr<function, vec3f>, b: ptr<function, vec3f>, c: ptr<function, vec3f>, d: ptr<function, vec3f>) {
20-
*a = unpack111011s(t_in.x);
21-
*b = unpack111011s(t_in.y);
22-
*c = unpack111011s(t_in.z);
23-
*d = unpack111011s(t_in.w);
35+
fn fetch4(t_in: vec4<u32>) -> SH {
36+
var result: SH;
37+
result.a = unpack111011s(t_in.x);
38+
result.b = unpack111011s(t_in.y);
39+
result.c = unpack111011s(t_in.z);
40+
result.d = unpack111011s(t_in.w);
41+
return result;
2442
}
2543
26-
fn fetch1(t_in: u32, a: ptr<function, vec3f>) {
27-
*a = unpack111011s(t_in);
44+
fn fetch1(t_in: u32) -> vec3f {
45+
return unpack111011s(t_in);
2846
}
2947
3048
#if SH_BANDS == 1
3149
var splatSH_1to3: texture_2d<u32>;
3250
3351
fn readSHData(source: ptr<function, SplatSource>, sh: ptr<function, array<vec3f, 3>>, scale: ptr<function, f32>) {
34-
fetchScale(textureLoad(splatSH_1to3, source.uv, 0), scale, &sh[0], &sh[1], &sh[2]);
52+
let result = fetchScale(textureLoad(splatSH_1to3, source.uv, 0));
53+
*scale = result.scale;
54+
sh[0] = result.a;
55+
sh[1] = result.b;
56+
sh[2] = result.c;
3557
}
3658
#elif SH_BANDS == 2
3759
var splatSH_1to3: texture_2d<u32>;
3860
var splatSH_4to7: texture_2d<u32>;
3961
var splatSH_8to11: texture_2d<u32>;
4062
4163
fn readSHData(source: ptr<function, SplatSource>, sh: ptr<function, array<vec3f, 8>>, scale: ptr<function, f32>) {
42-
fetchScale(textureLoad(splatSH_1to3, source.uv, 0), scale, &sh[0], &sh[1], &sh[2]);
43-
fetch4(textureLoad(splatSH_4to7, source.uv, 0), &sh[3], &sh[4], &sh[5], &sh[6]);
44-
fetch1(textureLoad(splatSH_8to11, source.uv, 0).x, &sh[7]);
64+
let first: ScaleAndSH = fetchScale(textureLoad(splatSH_1to3, source.uv, 0));
65+
*scale = first.scale;
66+
sh[0] = first.a;
67+
sh[1] = first.b;
68+
sh[2] = first.c;
69+
70+
let second: SH = fetch4(textureLoad(splatSH_4to7, source.uv, 0));
71+
sh[3] = second.a;
72+
sh[4] = second.b;
73+
sh[5] = second.c;
74+
sh[6] = second.d;
75+
76+
sh[7] = fetch1(textureLoad(splatSH_8to11, source.uv, 0).x);
4577
}
4678
#else
4779
var splatSH_1to3: texture_2d<u32>;
@@ -50,10 +82,29 @@ fn fetch1(t_in: u32, a: ptr<function, vec3f>) {
5082
var splatSH_12to15: texture_2d<u32>;
5183
5284
fn readSHData(source: ptr<function, SplatSource>, sh: ptr<function, array<vec3f, 15>>, scale: ptr<function, f32>) {
53-
fetchScale(textureLoad(splatSH_1to3, source.uv, 0), scale, &sh[0], &sh[1], &sh[2]);
54-
fetch4(textureLoad(splatSH_4to7, source.uv, 0), &sh[3], &sh[4], &sh[5], &sh[6]);
55-
fetch4(textureLoad(splatSH_8to11, source.uv, 0), &sh[7], &sh[8], &sh[9], &sh[10]);
56-
fetch4(textureLoad(splatSH_12to15, source.uv, 0), &sh[11], &sh[12], &sh[13], &sh[14]);
85+
let first: ScaleAndSH = fetchScale(textureLoad(splatSH_1to3, source.uv, 0));
86+
*scale = first.scale;
87+
sh[0] = first.a;
88+
sh[1] = first.b;
89+
sh[2] = first.c;
90+
91+
let second: SH = fetch4(textureLoad(splatSH_4to7, source.uv, 0));
92+
sh[3] = second.a;
93+
sh[4] = second.b;
94+
sh[5] = second.c;
95+
sh[6] = second.d;
96+
97+
let third: SH = fetch4(textureLoad(splatSH_8to11, source.uv, 0));
98+
sh[7] = third.a;
99+
sh[8] = third.b;
100+
sh[9] = third.c;
101+
sh[10] = third.d;
102+
103+
let fourth: SH = fetch4(textureLoad(splatSH_12to15, source.uv, 0));
104+
sh[11] = fourth.a;
105+
sh[12] = fourth.b;
106+
sh[13] = fourth.c;
107+
sh[14] = fourth.d;
57108
}
58109
#endif
59110

0 commit comments

Comments
 (0)