Skip to content

Commit

Permalink
fix: sh0 padding
Browse files Browse the repository at this point in the history
  • Loading branch information
mosure committed Dec 31, 2023
1 parent 49606d5 commit 7acb01a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 24 deletions.
27 changes: 20 additions & 7 deletions src/gaussian/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
packed::Gaussian,
},
material::spherical_harmonics::{
HALF_SH_COEFF_COUNT,
SH_COEFF_COUNT,
SphericalHarmonicCoefficients,
},
Expand Down Expand Up @@ -211,6 +212,23 @@ impl GaussianCloud {
pub fn spherical_harmonic_mut(&mut self, index: usize) -> &mut SphericalHarmonicCoefficients {
&mut self.spherical_harmonic[index]
}

pub fn resize_to_square(&mut self) {
#[cfg(all(feature = "buffer_texture", feature = "f16"))]
{
self.position_visibility.resize(self.square_len(), PositionVisibility::default());
self.spherical_harmonic.resize(self.square_len(), SphericalHarmonicCoefficients::default());
self.rotation_scale_opacity_packed128.resize(self.square_len(), RotationScaleOpacityPacked128::default());
}

#[cfg(all(feature = "buffer_texture", feature = "f32"))]
{
self.position_visibility.resize(self.square_len(), PositionVisibility::default());
self.spherical_harmonic.resize(self.square_len(), SphericalHarmonicCoefficients::default());
self.rotation.resize(self.square_len(), Rotation::default());
self.scale_opacity.resize(self.square_len(), ScaleOpacity::default());
}
}
}


Expand Down Expand Up @@ -290,12 +308,7 @@ impl GaussianCloud {
rotation_scale_opacity_packed128,
};

#[cfg(feature = "buffer_texture")]
{
cloud.position_visibility.resize(cloud.square_len(), PositionVisibility::default());
cloud.spherical_harmonic.resize(cloud.square_len(), SphericalHarmonicCoefficients::default());
cloud.rotation_scale_opacity_packed128.resize(cloud.square_len(), RotationScaleOpacityPacked128::default());
}
cloud.resize_to_square();

cloud
}
Expand Down Expand Up @@ -349,7 +362,7 @@ impl GaussianCloud {
coefficients: {
#[cfg(feature = "f16")]
{
let mut coefficients = [0 as u32; SH_COEFF_COUNT / 2];
let mut coefficients = [0 as u32; HALF_SH_COEFF_COUNT];

for coefficient in coefficients.iter_mut() {
let upper = rng.gen_range(-1.0..1.0);
Expand Down
4 changes: 2 additions & 2 deletions src/gaussian/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
packed::Gaussian,
},
material::spherical_harmonics::{
SH_COEFF_COUNT,
HALF_SH_COEFF_COUNT,
SphericalHarmonicCoefficients,
},
};
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Distribution<Gaussian> for rand::distributions::Standard {
coefficients: {
#[cfg(feature = "f16")]
{
let mut coefficients: [u32; SH_COEFF_COUNT / 2] = [0; SH_COEFF_COUNT / 2];
let mut coefficients: [u32; HALF_SH_COEFF_COUNT] = [0; HALF_SH_COEFF_COUNT];
for coefficient in coefficients.iter_mut() {
let upper = rng.gen_range(-1.0..1.0);
let lower = rng.gen_range(-1.0..1.0);
Expand Down
21 changes: 11 additions & 10 deletions src/material/spherical_harmonics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ const fn num_sh_coefficients(degree: usize) -> usize {
const SH_DEGREE: usize = 0;

#[cfg(not(feature = "web"))]
const SH_DEGREE: usize = 3;
const SH_DEGREE: usize = 0;

pub const SH_CHANNELS: usize = 3;
pub const SH_COEFF_COUNT_PER_CHANNEL: usize = num_sh_coefficients(SH_DEGREE);
pub const SH_COEFF_COUNT: usize = (SH_COEFF_COUNT_PER_CHANNEL * SH_CHANNELS + 3) & !3;

pub const HALF_SH_COEFF_COUNT: usize = ((SH_COEFF_COUNT + 1) & !1) / 2;
pub const HALF_SH_COEFF_COUNT: usize = SH_COEFF_COUNT / 2;
pub const PADDED_HALF_SH_COEFF_COUNT: usize = (HALF_SH_COEFF_COUNT + 3) & !3;

#[cfg(feature = "f16")]
pub const SH_VEC4_PLANES: usize = ((HALF_SH_COEFF_COUNT + 3) & !3) / 4;
pub const SH_VEC4_PLANES: usize = PADDED_HALF_SH_COEFF_COUNT / 4;
#[cfg(feature = "f32")]
pub const SH_VEC4_PLANES: usize = SH_COEFF_COUNT / 4;

Expand All @@ -82,7 +83,7 @@ pub const SH_VEC4_PLANES: usize = SH_COEFF_COUNT / 4;
pub struct SphericalHarmonicCoefficients {
#[reflect(ignore)]
#[serde(serialize_with = "coefficients_serializer", deserialize_with = "coefficients_deserializer")]
pub coefficients: [u32; SH_COEFF_COUNT / 2],
pub coefficients: [u32; HALF_SH_COEFF_COUNT],
}


Expand Down Expand Up @@ -110,7 +111,7 @@ pub struct SphericalHarmonicCoefficients {
impl Default for SphericalHarmonicCoefficients {
fn default() -> Self {
Self {
coefficients: [0; SH_COEFF_COUNT / 2],
coefficients: [0; HALF_SH_COEFF_COUNT],
}
}
}
Expand Down Expand Up @@ -145,7 +146,7 @@ impl SphericalHarmonicCoefficients {


#[cfg(feature = "f16")]
fn coefficients_serializer<S>(n: &[u32; SH_COEFF_COUNT / 2], s: S) -> Result<S::Ok, S::Error>
fn coefficients_serializer<S>(n: &[u32; HALF_SH_COEFF_COUNT], s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Expand All @@ -158,24 +159,24 @@ where
}

#[cfg(feature = "f16")]
fn coefficients_deserializer<'de, D>(d: D) -> Result<[u32; SH_COEFF_COUNT / 2], D::Error>
fn coefficients_deserializer<'de, D>(d: D) -> Result<[u32; HALF_SH_COEFF_COUNT], D::Error>
where
D: serde::Deserializer<'de>,
{
struct CoefficientsVisitor;

impl<'de> serde::de::Visitor<'de> for CoefficientsVisitor {
type Value = [u32; SH_COEFF_COUNT / 2];
type Value = [u32; HALF_SH_COEFF_COUNT];

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("an array of floats")
}

fn visit_seq<A>(self, mut seq: A) -> Result<[u32; SH_COEFF_COUNT / 2], A::Error>
fn visit_seq<A>(self, mut seq: A) -> Result<[u32; HALF_SH_COEFF_COUNT], A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut coefficients = [0; SH_COEFF_COUNT / 2];
let mut coefficients = [0; HALF_SH_COEFF_COUNT];

for (i, coefficient) in coefficients.iter_mut().enumerate().take(SH_COEFF_COUNT) {
*coefficient = seq
Expand Down
4 changes: 2 additions & 2 deletions src/query/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct SparseSelect {
impl Default for SparseSelect {
fn default() -> Self {
Self {
radius: 0.03,
neighbor_threshold: 5,
radius: 0.05,
neighbor_threshold: 4,
completed: false,
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/render/bindings.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,23 @@ struct Gaussian {
#ifdef PLANAR_TEXTURE_F16
#ifdef READ_WRITE_POINTS
@group(2) @binding(0) var position_visibility: texture_storage_2d<rgba32float, read_write>;

#if SH_VEC4_PLANES == 1
@group(2) @binding(1) var spherical_harmonics: texture_storage_2d<rgba32uint, read_write>;
#else
@group(2) @binding(1) var spherical_harmonics: texture_storage_2d_array<rgba32uint, read_write>;
#endif

@group(2) @binding(2) var rotation_scale_opacity: texture_storage_2d<rgba32uint, read_write>;
#else
@group(2) @binding(0) var position_visibility: texture_storage_2d<rgba32float, read>;

#if SH_VEC4_PLANES == 1
@group(2) @binding(1) var spherical_harmonics: texture_storage_2d<rgba32uint, read>;
#else
@group(2) @binding(1) var spherical_harmonics: texture_storage_2d_array<rgba32uint, read>;
#endif

@group(2) @binding(2) var rotation_scale_opacity: texture_storage_2d<rgba32uint, read>;
#endif
#endif
Expand All @@ -74,11 +86,23 @@ struct Gaussian {
#ifdef PLANAR_TEXTURE_F32
#ifdef READ_WRITE_POINTS
@group(2) @binding(0) var position_visibility: texture_storage_2d<rgba32float, read_write>;

#if SH_VEC4_PLANES == 1
@group(2) @binding(1) var spherical_harmonics: texture_storage_2d<rgba32float, read_write>;
#else
@group(2) @binding(1) var spherical_harmonics: texture_storage_2d_array<rgba32float, read_write>;
#endif

@group(2) @binding(2) var rotation_scale_opacity: texture_storage_2d<rgba32float, read_write>;
#else
@group(2) @binding(0) var position_visibility: texture_storage_2d<rgba32float, read>;

#if SH_VEC4_PLANES == 1
@group(2) @binding(1) var spherical_harmonics: texture_storage_2d<rgba32float, read>;
#else
@group(2) @binding(1) var spherical_harmonics: texture_storage_2d_array<rgba32float, read>;
#endif

@group(2) @binding(2) var rotation_scale_opacity: texture_storage_2d<rgba32float, read>;
#endif
#endif
Expand Down
14 changes: 11 additions & 3 deletions src/render/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use crate::{
GpuGaussianCloud,
},
material::spherical_harmonics::{
HALF_SH_COEFF_COUNT,
SH_COEFF_COUNT,
SH_VEC4_PLANES,
SphericalHarmonicCoefficients,
Expand Down Expand Up @@ -222,7 +221,10 @@ fn queue_textures(
let start_index = plane_index * 4;
let end_index = std::cmp::min(start_index + 4, sh.coefficients.len());

sh.coefficients[start_index..end_index].to_vec()
let mut depthwise = sh.coefficients[start_index..end_index].to_vec();
depthwise.resize(4, 0);

depthwise
})
})
.collect();
Expand Down Expand Up @@ -282,6 +284,12 @@ pub fn get_bind_group_layout(
StorageTextureAccess::ReadWrite
};

let sh_view_dimension = if SH_VEC4_PLANES == 1 {
TextureViewDimension::D2
} else {
TextureViewDimension::D2Array
};

render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("planar_f16_gaussian_cloud_layout"),
entries: &[
Expand All @@ -301,7 +309,7 @@ pub fn get_bind_group_layout(
ty: BindingType::StorageTexture {
access,
format: TextureFormat::Rgba32Uint,
view_dimension: TextureViewDimension::D2Array,
view_dimension: sh_view_dimension,
},
count: None,
},
Expand Down
8 changes: 8 additions & 0 deletions src/render/texture.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ fn get_spherical_harmonics(index: u32) -> array<f32, #{SH_COEFF_COUNT}> {
var coefficients: array<f32, #{SH_COEFF_COUNT}>;

for (var i = 0u; i < #{SH_VEC4_PLANES}u; i = i + 1u) {

#if SH_VEC4_PLANES == 1
let sample = textureLoad(
spherical_harmonics,
location(index),
);
#else
let sample = textureLoad(
spherical_harmonics,
location(index),
i,
);
#endif

let v0 = unpack2x16float(sample.x);
let v1 = unpack2x16float(sample.y);
Expand Down

0 comments on commit 7acb01a

Please sign in to comment.