Skip to content

Commit 2f0be3b

Browse files
committed
Cleaned up what we expose via bindgen.
1 parent d1d86f2 commit 2f0be3b

File tree

4 files changed

+63
-71
lines changed

4 files changed

+63
-71
lines changed

wasm-splats/src/lib.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
1+
pub mod models;
12
pub mod radix;
23
pub mod texture_gen;
34

4-
use js_sys::{Float32Array, Object, Uint32Array, Uint8Array};
5+
use js_sys::{Float32Array, Uint32Array, Uint8Array};
56
use wasm_bindgen::prelude::*;
7+
use crate::models::TextureData;
68

79
/// Generate a splat texture from the given attributes.
810
///
911
/// Wraps the [`texture_gen::generate_texture_from_attrs`] function for access from JavaScript.
1012
#[wasm_bindgen]
11-
pub fn generate_splat_texture_from_attrs(
13+
pub fn generate_splat_texture(
1214
positions: &Float32Array,
1315
scales: &Float32Array,
1416
rotations: &Float32Array,
1517
colors: &Uint8Array,
1618
count: usize,
17-
) -> Result<Object, JsValue> {
18-
let texture_data =
19-
texture_gen::generate_texture_from_attrs(positions, scales, rotations, colors, count)?;
20-
21-
let js_data = Uint32Array::new_with_length(texture_data.width() * texture_data.height() * 4);
22-
js_data.copy_from(&texture_data.data());
23-
24-
let result = Object::new();
25-
js_sys::Reflect::set(&result, &"data".into(), &js_data)?;
26-
js_sys::Reflect::set(
27-
&result,
28-
&"width".into(),
29-
&(texture_data.width() as f64).into(),
30-
)?;
31-
js_sys::Reflect::set(
32-
&result,
33-
&"height".into(),
34-
&(texture_data.height() as f64).into(),
35-
)?;
19+
) -> Result<TextureData, JsValue> {
20+
texture_gen::generate_texture_from_attrs(positions, scales, rotations, colors, count)
21+
}
3622

37-
Ok(result)
23+
/// Sorts the Gaussian Splats by depth using a radix sort.
24+
///
25+
/// Wraps the [`radix::radix_sort_gaussians_indexes`] function for access from JavaScript.
26+
#[wasm_bindgen]
27+
pub fn radix_sort_gaussians_indexes(
28+
positions: &Float32Array,
29+
model_view: &Float32Array,
30+
count: usize,
31+
) -> Result<Uint32Array, JsValue> {
32+
radix::radix_sort_gaussians_indexes(positions, model_view, count)
3833
}

wasm-splats/src/models.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Module encapsulating the data models exposed through WebAssembly.
2+
3+
use wasm_bindgen::prelude::wasm_bindgen;
4+
5+
/// A structure representing texture data. This is used to pass the texture data from generation in [`texture_gen`] to the JavaScript side.
6+
#[wasm_bindgen]
7+
pub struct TextureData {
8+
/// The texture data.
9+
data: Vec<u32>,
10+
/// Width of the texture in pixels.
11+
width: u32,
12+
/// Height of the texture in pixels.
13+
height: u32,
14+
}
15+
16+
#[wasm_bindgen]
17+
impl TextureData {
18+
/// Getter for the underlying texture data. Always returns a copy.
19+
#[wasm_bindgen(getter)]
20+
pub fn data(&self) -> Vec<u32> {
21+
self.data.clone()
22+
}
23+
24+
/// Getter for the width of the texture in pixels.
25+
#[wasm_bindgen(getter)]
26+
pub fn width(&self) -> u32 {
27+
self.width
28+
}
29+
30+
/// Getter for the height of the texture in pixels.
31+
#[wasm_bindgen(getter)]
32+
pub fn height(&self) -> u32 {
33+
self.height
34+
}
35+
36+
/// Creates a new texture data object with the underlying data, width, and height.
37+
pub fn new(data: Vec<u32>, width: u32, height: u32) -> Self {
38+
TextureData {
39+
data,
40+
width,
41+
height,
42+
}
43+
}
44+
}

wasm-splats/src/radix.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use js_sys::{Float32Array, Uint32Array};
22
use wasm_bindgen::prelude::*;
33

44
/// Sorts the Gaussian Splats by depth using a radix sort.
5-
#[wasm_bindgen]
65
pub fn radix_sort_gaussians_indexes(
76
positions: &Float32Array,
87
model_view: &Float32Array,
9-
_texture_width: u32, // TODO: FIGURE OUT IF THIS IS NEEDED.
108
count: usize,
119
) -> Result<Uint32Array, JsValue> {
1210
if positions.length() as usize != count * 3 {

wasm-splats/src/texture_gen.rs

+2-47
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,6 @@
11
use js_sys::{Float32Array, Uint8Array};
22
use wasm_bindgen::prelude::*;
3-
4-
/// Represents a texture data object.
5-
#[wasm_bindgen]
6-
pub struct TextureData {
7-
/// The texture data.
8-
data: Vec<u32>,
9-
/// Width of the texture in pixels.
10-
width: u32,
11-
/// Height of the texture in pixels.
12-
height: u32,
13-
}
14-
15-
#[wasm_bindgen]
16-
impl TextureData {
17-
/// Getter for the underlying texture data. Always returns a copy.
18-
#[wasm_bindgen(getter)]
19-
pub fn data(&self) -> Vec<u32> {
20-
self.data.clone()
21-
}
22-
23-
/// Getter for the width of the texture in pixels.
24-
#[wasm_bindgen(getter)]
25-
pub fn width(&self) -> u32 {
26-
self.width
27-
}
28-
29-
/// Getter for the height of the texture in pixels.
30-
#[wasm_bindgen(getter)]
31-
pub fn height(&self) -> u32 {
32-
self.height
33-
}
34-
35-
/// Creates a new texture data object with the underlying data, width, and height.
36-
pub fn new(data: Vec<u32>, width: u32, height: u32) -> Self {
37-
TextureData {
38-
data,
39-
width,
40-
height,
41-
}
42-
}
43-
}
3+
use crate::models::TextureData;
444

455
/// Converts a 32-bit float to a 16-bit integer.
466
fn float_to_half(f: f32) -> i16 {
@@ -88,7 +48,6 @@ fn float_to_half(f: f32) -> i16 {
8848
}
8949

9050
/// Generates a texture from the given attributes.
91-
#[wasm_bindgen]
9251
pub fn generate_texture_from_attrs(
9352
positions: &Float32Array,
9453
scales: &Float32Array,
@@ -172,9 +131,5 @@ pub fn generate_texture_from_attrs(
172131
| ((float_to_half(4.0 * sigma[5]) as u32 & 0xFFFF) << 16);
173132
}
174133

175-
Ok(TextureData {
176-
data: tex_data,
177-
width: tex_width,
178-
height: tex_height,
179-
})
134+
Ok(TextureData::new(tex_data, tex_width, tex_height))
180135
}

0 commit comments

Comments
 (0)