wgpu-3dgs-core 0.7.0

A 3D Gaussian splatting library written in Rust using wgpu.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use glam::*;

use wgpu::util::DeviceExt;

use crate::{
    BufferWrapper, DownloadBufferError, Gaussian, GaussianCov3dConfig, GaussianCov3dHalfConfig,
    GaussianCov3dRotScaleConfig, GaussianCov3dSingleConfig, GaussianShConfig, GaussianShHalfConfig,
    GaussianShNoneConfig, GaussianShNorm8Config, GaussianShSingleConfig,
    GaussiansBufferTryFromBufferError, GaussiansBufferUpdateError, GaussiansBufferUpdateRangeError,
    IterGaussian,
};

/// The Gaussians storage buffer.
///
/// This buffer holds an array of Gaussians represented by the specified [`GaussianPod`].
#[derive(Debug, Clone)]
pub struct GaussiansBuffer<G: GaussianPod>(wgpu::Buffer, std::marker::PhantomData<G>);

impl<G: GaussianPod> GaussiansBuffer<G> {
    /// Create a new Gaussians buffer.
    pub fn new(device: &wgpu::Device, gaussians: &impl IterGaussian) -> Self {
        Self::new_with_pods(
            device,
            gaussians
                .iter_gaussian()
                .map(|g| G::from_gaussian(&g))
                .collect::<Vec<_>>()
                .as_slice(),
        )
    }

    /// Create a new Gaussians buffer with the specified size with [`wgpu::BufferUsages`].
    pub fn new_with_usage(
        device: &wgpu::Device,
        gaussians: &impl IterGaussian,
        usage: wgpu::BufferUsages,
    ) -> Self {
        Self::new_with_pods_and_usage(
            device,
            gaussians
                .iter_gaussian()
                .map(|g| G::from_gaussian(&g))
                .collect::<Vec<_>>()
                .as_slice(),
            usage,
        )
    }

    /// Create a new Gaussians buffer with [`GaussianPod`].
    pub fn new_with_pods(device: &wgpu::Device, gaussians: &[G]) -> Self {
        Self::new_with_pods_and_usage(device, gaussians, Self::DEFAULT_USAGES)
    }

    /// Create a new Gaussians buffer with [`GaussianPod`] and the specified size and
    /// [`wgpu::BufferUsages`].
    pub fn new_with_pods_and_usage(
        device: &wgpu::Device,
        gaussians: &[G],
        usage: wgpu::BufferUsages,
    ) -> Self {
        let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("Gaussians Buffer"),
            contents: bytemuck::cast_slice(gaussians),
            usage,
        });

        Self(buffer, std::marker::PhantomData)
    }

    /// Create a new Gaussians buffer with the specified size.
    pub fn new_empty(device: &wgpu::Device, len: usize) -> Self {
        Self::new_empty_with_usage(device, len, Self::DEFAULT_USAGES)
    }

    /// Create a new Gaussians buffer with the specified size and [`wgpu::BufferUsages`].
    pub fn new_empty_with_usage(
        device: &wgpu::Device,
        len: usize,
        usage: wgpu::BufferUsages,
    ) -> Self {
        let buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Gaussians Buffer"),
            size: (len * std::mem::size_of::<G>()) as wgpu::BufferAddress,
            usage,
            mapped_at_creation: false,
        });

        Self(buffer, std::marker::PhantomData)
    }

    /// Get the number of Gaussians.
    pub fn len(&self) -> usize {
        self.0.size() as usize / std::mem::size_of::<G>()
    }

    /// Check if the buffer is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Update the buffer.
    ///
    /// `gaussians` should have the same number of Gaussians as the buffer.
    pub fn update(
        &self,
        queue: &wgpu::Queue,
        gaussians: &impl IterGaussian,
    ) -> Result<(), GaussiansBufferUpdateError> {
        self.update_with_pod(
            queue,
            gaussians
                .iter_gaussian()
                .map(|g| G::from_gaussian(&g))
                .collect::<Vec<_>>()
                .as_slice(),
        )
    }

    /// Update the buffer with [`GaussianPod`].
    ///
    /// `pods` should have the same number of Gaussians as the buffer.
    pub fn update_with_pod(
        &self,
        queue: &wgpu::Queue,
        pods: &[G],
    ) -> Result<(), GaussiansBufferUpdateError> {
        if pods.len() != self.len() {
            return Err(GaussiansBufferUpdateError::CountMismatch {
                count: pods.len(),
                expected_count: self.len(),
            });
        }

        queue.write_buffer(&self.0, 0, bytemuck::cast_slice(pods));

        Ok(())
    }

    /// Update a range of the buffer.
    ///
    /// `gaussians` should fit in the buffer starting from `start`.
    pub fn update_range(
        &self,
        queue: &wgpu::Queue,
        start: usize,
        gaussians: &[Gaussian],
    ) -> Result<(), GaussiansBufferUpdateRangeError> {
        self.update_range_with_pod(
            queue,
            start,
            gaussians
                .iter()
                .map(G::from_gaussian)
                .collect::<Vec<_>>()
                .as_slice(),
        )
    }

    /// Update a range of the buffer with [`GaussianPod`].
    ///
    /// `pods` should fit in the buffer starting from `start`.
    pub fn update_range_with_pod(
        &self,
        queue: &wgpu::Queue,
        start: usize,
        pods: &[G],
    ) -> Result<(), GaussiansBufferUpdateRangeError> {
        if start + pods.len() > self.len() {
            return Err(GaussiansBufferUpdateRangeError::CountMismatch {
                count: pods.len(),
                start,
                expected_count: self.len(),
            });
        }

        queue.write_buffer(
            &self.0,
            (start * std::mem::size_of::<G>()) as wgpu::BufferAddress,
            bytemuck::cast_slice(pods),
        );

        Ok(())
    }

    /// Download the buffer data into a [`Vec`] of [`Gaussian`].
    pub async fn download_gaussians(
        &self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
    ) -> Result<Vec<Gaussian>, DownloadBufferError> {
        self.download::<G>(device, queue)
            .await
            .map(|pods| pods.into_iter().map(Into::into).collect::<Vec<_>>())
    }
}

impl<G: GaussianPod> BufferWrapper for GaussiansBuffer<G> {
    const DEFAULT_USAGES: wgpu::BufferUsages = wgpu::BufferUsages::from_bits_retain(
        wgpu::BufferUsages::STORAGE.bits() | wgpu::BufferUsages::COPY_DST.bits(),
    );

    fn buffer(&self) -> &wgpu::Buffer {
        &self.0
    }
}

impl<G: GaussianPod> From<GaussiansBuffer<G>> for wgpu::Buffer {
    fn from(wrapper: GaussiansBuffer<G>) -> Self {
        wrapper.0
    }
}

impl<G: GaussianPod> TryFrom<wgpu::Buffer> for GaussiansBuffer<G> {
    type Error = GaussiansBufferTryFromBufferError;

    fn try_from(buffer: wgpu::Buffer) -> Result<Self, Self::Error> {
        if !buffer
            .size()
            .is_multiple_of(std::mem::size_of::<G>() as wgpu::BufferAddress)
        {
            return Err(GaussiansBufferTryFromBufferError::BufferSizeNotMultiple {
                buffer_size: buffer.size(),
                expected_multiple_size: std::mem::size_of::<G>() as wgpu::BufferAddress,
            });
        }

        Ok(Self(buffer, std::marker::PhantomData))
    }
}

/// The Gaussian POD trait.
///
/// The number of configurations for this is the combination of all the [`GaussianShConfig`]
/// and [`GaussianCov3dConfig`].
///
/// You can use the corresponding config by using the name in the following format:
/// `GaussianPodWithSh{ShConfig}Cov3d{Cov3dConfig}Configs`, e.g.
/// [`GaussianPodWithShSingleCov3dRotScaleConfigs`].
pub trait GaussianPod:
    for<'a> From<&'a Gaussian>
    + Into<Gaussian>
    + Send
    + Sync
    + std::fmt::Debug
    + Clone
    + Copy
    + PartialEq
    + bytemuck::NoUninit
    + bytemuck::AnyBitPattern
{
    /// The SH configuration.
    type ShConfig: GaussianShConfig;

    /// The covariance 3D configuration.
    type Cov3dConfig: GaussianCov3dConfig;

    /// Convert from POD to Gaussian.
    fn into_gaussian(self) -> Gaussian {
        self.into()
    }

    /// Create a new Gaussian POD from the Gaussian.
    fn from_gaussian(gaussian: &Gaussian) -> Self {
        Self::from(gaussian)
    }

    /// Create the features for [`Wesl`](wesl::Wesl) compilation.
    ///
    /// You may want to use [`GaussianPod::wesl_features`] most of the time instead.
    fn features() -> [(&'static str, bool); 7] {
        [
            GaussianShSingleConfig::FEATURE,
            GaussianShHalfConfig::FEATURE,
            GaussianShNorm8Config::FEATURE,
            GaussianShNoneConfig::FEATURE,
            GaussianCov3dRotScaleConfig::FEATURE,
            GaussianCov3dSingleConfig::FEATURE,
            GaussianCov3dHalfConfig::FEATURE,
        ]
        .map(|name| {
            (
                name,
                name == Self::ShConfig::FEATURE || name == Self::Cov3dConfig::FEATURE,
            )
        })
    }

    /// Create the features for [`Wesl`](wesl::Wesl) compilation as a [`wesl::Features`].
    fn wesl_features() -> wesl::Features {
        wesl::Features {
            flags: Self::features()
                .iter()
                .map(|(name, enabled)| (name.to_string(), (*enabled).into()))
                .collect(),
            ..Default::default()
        }
    }
}

/// Macro to create the POD representation of Gaussian given the configurations.
macro_rules! gaussian_pod {
    (sh = $sh:ident, cov3d = $cov3d:ident, padding_size = $padding:expr) => {
        paste::paste! {
            #[repr(C)]
            #[derive(Debug, Clone, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
            pub struct [< GaussianPodWith Sh $sh Cov3d $cov3d Configs >] {
                pub pos: Vec3,
                pub color: U8Vec4,
                pub sh: <[< GaussianSh $sh Config >] as GaussianShConfig>::Field,
                pub cov3d: <[< GaussianCov3d $cov3d Config >] as GaussianCov3dConfig>::Field,
                pub padding: [f32; $padding],
            }

            impl From<&Gaussian> for [< GaussianPodWith Sh $sh Cov3d $cov3d Configs >] {
                fn from(gaussian: &Gaussian) -> Self {
                    // Covariance
                    let cov3d = <[< GaussianCov3d $cov3d Config >]>::from_rot_scale(
                        gaussian.rot,
                        gaussian.scale,
                    );

                    // Color
                    let color = gaussian.color;

                    // Spherical harmonics
                    let sh = [< GaussianSh $sh Config >]::from_sh(&gaussian.sh);

                    // Position
                    let pos = gaussian.pos;

                    Self {
                        pos,
                        color,
                        sh,
                        cov3d,
                        padding: [0.0; $padding],
                    }
                }
            }

            impl From<[< GaussianPodWith Sh $sh Cov3d $cov3d Configs >]> for Gaussian {
                fn from(pod: [< GaussianPodWith Sh $sh Cov3d $cov3d Configs >]) -> Self {
                    // Position
                    let pos = pod.pos;

                    // Spherical harmonics
                    let sh = [< GaussianSh $sh Config >]::to_sh(&pod.sh);

                    // Color
                    let color = pod.color;

                    // Rotation
                    let (rot, scale) = <[< GaussianCov3d $cov3d Config >]>::to_rot_scale(&pod.cov3d);

                    Self {
                        rot,
                        pos,
                        color,
                        sh,
                        scale,
                    }
                }
            }

            impl GaussianPod for [< GaussianPodWith Sh $sh Cov3d $cov3d Configs >] {
                type ShConfig = [< GaussianSh $sh Config >];
                type Cov3dConfig = [< GaussianCov3d $cov3d Config >];
            }
        }
    };
}

gaussian_pod!(sh = Single, cov3d = RotScale, padding_size = 0);
gaussian_pod!(sh = Single, cov3d = Single, padding_size = 1);
gaussian_pod!(sh = Single, cov3d = Half, padding_size = 0);
gaussian_pod!(sh = Half, cov3d = RotScale, padding_size = 2);
gaussian_pod!(sh = Half, cov3d = Single, padding_size = 3);
gaussian_pod!(sh = Half, cov3d = Half, padding_size = 2);
gaussian_pod!(sh = Norm8, cov3d = RotScale, padding_size = 1);
gaussian_pod!(sh = Norm8, cov3d = Single, padding_size = 2);
gaussian_pod!(sh = Norm8, cov3d = Half, padding_size = 1);
gaussian_pod!(sh = None, cov3d = RotScale, padding_size = 1);
gaussian_pod!(sh = None, cov3d = Single, padding_size = 2);
gaussian_pod!(sh = None, cov3d = Half, padding_size = 1);

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! test_pod_from_gaussian {
        ($name:ident, $pod_type:ty, true) => {
            paste::paste! {
                #[test]
                #[should_panic]
                fn [<test_ $name _into_gaussian_should_panic>]() {
                    let pod = $pod_type::from_gaussian(&Gaussian {
                        rot: Quat::from_xyzw(0.0, 0.0, 0.0, 1.0),
                        pos: Vec3::new(1.0, 2.0, 3.0),
                        color: U8Vec4::new(255, 128, 64, 32),
                        sh: [Vec3::new(0.1, 0.2, 0.3); 15],
                        scale: Vec3::new(1.0, 2.0, 3.0),
                    });

                    pod.into_gaussian();
                }
            }
        };
        ($name:ident, $pod_type:ty, false) => {
            paste::paste! {
                #[test]
                fn [<test_ $name _into_gaussian_should_equal_original_pod>]() {
                    let pod = $pod_type::from_gaussian(&Gaussian {
                        rot: Quat::from_xyzw(0.0, 0.0, 0.0, 1.0),
                        pos: Vec3::new(1.0, 2.0, 3.0),
                        color: U8Vec4::new(255, 128, 64, 32),
                        sh: [Vec3::new(0.1, 0.2, 0.3); 15],
                        scale: Vec3::new(1.0, 2.0, 3.0),
                    });

                    let gaussian = pod.into_gaussian();

                    assert_eq!(pod.pos, gaussian.pos);
                    assert_eq!(pod.color, gaussian.color);
                    assert_eq!(
                        pod.sh,
                        <$pod_type as GaussianPod>::ShConfig::from_sh(&gaussian.sh),
                    );
                    assert_eq!(
                        pod.cov3d,
                        <$pod_type as GaussianPod>::Cov3dConfig::from_rot_scale(
                            gaussian.rot,
                            gaussian.scale
                        ),
                    );
                }
            }
        };
    }

    macro_rules! test_pod {
        ($name:ident, $pod_type:ty, $when_into_gaussian_should_panic:expr) => {
            paste::paste! {
                #[test]
                fn [<test_ $name _from_gaussian_should_equal_original_gaussian>]() {
                    let gaussian = Gaussian {
                        rot: Quat::from_xyzw(0.0, 0.0, 0.0, 1.0),
                        pos: Vec3::new(1.0, 2.0, 3.0),
                        color: U8Vec4::new(255, 128, 64, 32),
                        sh: [Vec3::new(0.1, 0.2, 0.3); 15],
                        scale: Vec3::new(1.0, 2.0, 3.0),
                    };

                    let pod = $pod_type::from_gaussian(&gaussian);

                    assert_eq!(gaussian.pos, pod.pos);
                    assert_eq!(gaussian.color, pod.color);
                    assert_eq!(
                        <$pod_type as GaussianPod>::ShConfig::from_sh(&gaussian.sh),
                        pod.sh,
                    );
                    assert_eq!(
                        <$pod_type as GaussianPod>::Cov3dConfig::from_rot_scale(
                            gaussian.rot,
                            gaussian.scale
                        ),
                        pod.cov3d,
                    );
                }

                test_pod_from_gaussian!($name, $pod_type, $when_into_gaussian_should_panic);

                #[test]
                fn [<test_ $name _features_should_be_correct>]() {
                    let features = <$pod_type as GaussianPod>::features();

                    for (name, enabled) in features {
                        if name == <$pod_type as GaussianPod>::ShConfig::FEATURE
                            || name == <$pod_type as GaussianPod>::Cov3dConfig::FEATURE
                        {
                            assert!(enabled, "Feature {name} should be enabled");
                        } else {
                            assert!(!enabled, "Feature {name} should be disabled");
                        }
                    }
                }

                #[test]
                fn [<test_ $name _wesl_features_should_be_correct>]() {
                    let wesl_features = <$pod_type as GaussianPod>::wesl_features();
                    let features = <$pod_type as GaussianPod>::features();

                    for (name, enabled) in features {
                        let wesl_enabled = wesl_features
                            .flags
                            .get(name)
                            .map(|v| *v == wesl::Feature::Enable)
                            .unwrap_or(false);

                        assert_eq!(
                            enabled, wesl_enabled,
                            "Feature {name} should be {}",
                            if enabled { "enabled" } else { "disabled" }
                        );
                    }
                }
            }
        };
    }

    #[rustfmt::skip]
    mod pod {
        use super::*;

        test_pod!(single_rotscale, GaussianPodWithShSingleCov3dRotScaleConfigs, false);
        test_pod!(single_single, GaussianPodWithShSingleCov3dSingleConfigs, true);
        test_pod!(single_half, GaussianPodWithShSingleCov3dHalfConfigs, true);
        test_pod!(half_rotscale, GaussianPodWithShHalfCov3dRotScaleConfigs, false);
        test_pod!(test_half_single, GaussianPodWithShHalfCov3dSingleConfigs, true);
        test_pod!(test_half_half, GaussianPodWithShHalfCov3dHalfConfigs, true);
        test_pod!(norm8_rotscale, GaussianPodWithShNorm8Cov3dRotScaleConfigs, false);
        test_pod!(norm8_single, GaussianPodWithShNorm8Cov3dSingleConfigs, true);
        test_pod!(norm8_half, GaussianPodWithShNorm8Cov3dHalfConfigs, true);
        test_pod!(none_rotscale, GaussianPodWithShNoneCov3dRotScaleConfigs, true);
        test_pod!(none_single, GaussianPodWithShNoneCov3dSingleConfigs, true);
        test_pod!(none_half, GaussianPodWithShNoneCov3dHalfConfigs, true);
    }
}