|
1 | 1 | use js_sys::{Float32Array, Uint32Array, Uint8Array};
|
2 | 2 | use wasm_bindgen::prelude::*;
|
3 | 3 |
|
4 |
| -#[wasm_bindgen] |
5 |
| -pub fn radix_sort_gaussians_attrs( |
6 |
| - positions: &Float32Array, |
7 |
| - scales: &Float32Array, |
8 |
| - rotations: &Float32Array, |
9 |
| - colors: &Uint8Array, |
10 |
| - model_view: &Float32Array, |
11 |
| - count: usize, |
12 |
| -) -> Result<js_sys::Array, JsValue> { |
13 |
| - if positions.length() as usize != count * 3 |
14 |
| - || scales.length() as usize != count * 3 |
15 |
| - || rotations.length() as usize != count * 4 |
16 |
| - || colors.length() as usize != count * 4 |
17 |
| - || model_view.length() != 16 |
18 |
| - { |
19 |
| - return Err(JsValue::from_str("Invalid array lengths")); |
20 |
| - } |
21 |
| - |
22 |
| - //set capacity first |
23 |
| - let positions_vec = positions.to_vec(); |
24 |
| - let model_view_vec = model_view.to_vec(); |
25 |
| - |
26 |
| - let mut depth_values = vec![0i32; count]; |
27 |
| - let mut max_depth = f32::NEG_INFINITY; |
28 |
| - let mut min_depth = f32::INFINITY; |
29 |
| - |
30 |
| - for i in 0..count { |
31 |
| - let depth = positions_vec[i * 3] * model_view_vec[2] |
32 |
| - + positions_vec[i * 3 + 1] * model_view_vec[6] |
33 |
| - + positions_vec[i * 3 + 2] * model_view_vec[10]; |
34 |
| - |
35 |
| - let depth_int = (depth * 4096.0) as i32; |
36 |
| - depth_values[i] = depth_int; |
37 |
| - max_depth = max_depth.max(depth_int as f32); |
38 |
| - min_depth = min_depth.min(depth_int as f32); |
39 |
| - } |
40 |
| - |
41 |
| - let depth_offset = (-min_depth) as i32; |
42 |
| - for depth in depth_values.iter_mut() { |
43 |
| - *depth += depth_offset; |
44 |
| - } |
45 |
| - |
46 |
| - let mut indices: Vec<u32> = (0..count as u32).collect(); |
47 |
| - let mut temp_depths = vec![0i32; count]; |
48 |
| - let mut temp_indices = vec![0u32; count]; |
49 |
| - |
50 |
| - for shift in (0..32).step_by(8) { |
51 |
| - let mut counts = [0u32; 256]; |
52 |
| - |
53 |
| - for &depth in depth_values.iter() { |
54 |
| - let byte = ((depth >> shift) & 0xFF) as usize; |
55 |
| - counts[byte] += 1; |
56 |
| - } |
57 |
| - |
58 |
| - let mut total = 0; |
59 |
| - for count in counts.iter_mut() { |
60 |
| - let current = *count; |
61 |
| - *count = total; |
62 |
| - total += current; |
63 |
| - } |
64 |
| - |
65 |
| - for i in 0..count { |
66 |
| - let byte = ((depth_values[i] >> shift) & 0xFF) as usize; |
67 |
| - let pos = counts[byte] as usize; |
68 |
| - counts[byte] += 1; |
69 |
| - |
70 |
| - temp_depths[pos] = depth_values[i]; |
71 |
| - temp_indices[pos] = indices[i]; |
72 |
| - } |
73 |
| - |
74 |
| - depth_values.copy_from_slice(&temp_depths); |
75 |
| - indices.copy_from_slice(&temp_indices); |
76 |
| - } |
77 |
| - |
78 |
| - let mut new_positions: Vec<f32> = vec![0.0; count * 3]; |
79 |
| - let mut new_scales: Vec<f32> = vec![0.0; count * 3]; |
80 |
| - let mut new_rotations: Vec<f32> = vec![0.0; count * 4]; |
81 |
| - let mut new_colors: Vec<u8> = vec![0; count * 4]; |
82 |
| - |
83 |
| - let scales_vec = scales.to_vec(); |
84 |
| - let rotations_vec = rotations.to_vec(); |
85 |
| - let colors_vec = colors.to_vec(); |
86 |
| - |
87 |
| - for i in 0..count { |
88 |
| - let j = indices[i] as usize; |
89 |
| - |
90 |
| - new_positions[i * 3] = positions_vec[j * 3]; |
91 |
| - new_positions[i * 3 + 1] = positions_vec[j * 3 + 1]; |
92 |
| - new_positions[i * 3 + 2] = positions_vec[j * 3 + 2]; |
93 |
| - |
94 |
| - new_scales[i * 3] = scales_vec[j * 3]; |
95 |
| - new_scales[i * 3 + 1] = scales_vec[j * 3 + 1]; |
96 |
| - new_scales[i * 3 + 2] = scales_vec[j * 3 + 2]; |
97 |
| - |
98 |
| - new_rotations[i * 4] = rotations_vec[j * 4]; |
99 |
| - new_rotations[i * 4 + 1] = rotations_vec[j * 4 + 1]; |
100 |
| - new_rotations[i * 4 + 2] = rotations_vec[j * 4 + 2]; |
101 |
| - new_rotations[i * 4 + 3] = rotations_vec[j * 4 + 3]; |
102 |
| - |
103 |
| - new_colors[i * 4] = colors_vec[j * 4]; |
104 |
| - new_colors[i * 4 + 1] = colors_vec[j * 4 + 1]; |
105 |
| - new_colors[i * 4 + 2] = colors_vec[j * 4 + 2]; |
106 |
| - new_colors[i * 4 + 3] = colors_vec[j * 4 + 3]; |
107 |
| - } |
108 |
| - |
109 |
| - let new_positions_array = Float32Array::new_with_length(count as u32 * 3); |
110 |
| - new_positions_array.copy_from(&new_positions[..]); |
111 |
| - |
112 |
| - let new_scales_array = Float32Array::new_with_length(count as u32 * 3); |
113 |
| - new_scales_array.copy_from(&new_scales[..]); |
114 |
| - |
115 |
| - let new_rotations_array = Float32Array::new_with_length(count as u32 * 4); |
116 |
| - new_rotations_array.copy_from(&new_rotations[..]); |
117 |
| - |
118 |
| - let new_colors_array = Uint8Array::new_with_length(count as u32 * 4); |
119 |
| - new_colors_array.copy_from(&new_colors[..]); |
120 |
| - |
121 |
| - let result = js_sys::Array::new(); |
122 |
| - result.push(&new_positions_array); |
123 |
| - result.push(&new_scales_array); |
124 |
| - result.push(&new_rotations_array); |
125 |
| - result.push(&new_colors_array); |
126 |
| - |
127 |
| - Ok(result) |
128 |
| -} |
129 |
| - |
130 | 4 | #[wasm_bindgen]
|
131 | 5 | pub fn radix_sort_gaussians_indexes(
|
132 | 6 | positions: &Float32Array,
|
|
0 commit comments