Skip to content

Commit 474e3b0

Browse files
NathanaelG1Ouroboros
and
Ouroboros
authored
#630 transmute_vec_as_bytes potential fix (#662)
* chore(#630): reformatting transmute_vec_as_bytes to properly handle padding for usize and f32 types * chore(630): added bytemuck to fyrox-core and fyrox-impl . Updated transmute_vec_as_bytes to have the generic T implement pod trait * chore(630): removing commented statement * general cleanup * removed podvector3 struct and enabled bytemuck on half dependency. --------- Co-authored-by: Ouroboros <nathanael@Ouroboross-Mac-Studio.local>
1 parent 31344ef commit 474e3b0

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

Diff for: fyrox-core/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ byteorder = "1.4.3"
2222
rand = "0.8.4"
2323
memoffset = "0.9.0"
2424
lazy_static = "1.4.0"
25-
nalgebra = "0.32.3"
25+
nalgebra = { version = "0.32.3", features = ["bytemuck"] }
2626
arrayvec = "0.7.2"
2727
futures = {version = "0.3.17", features = ["thread-pool"] }
2828
uuid = { version = "1", features = ["v4", "js"] }
@@ -35,6 +35,7 @@ once_cell = "1.17.1"
3535
notify = "6"
3636
serde = { version = "1", features = ["derive"] }
3737
bincode = "1.3.3"
38+
bytemuck = "1.16.1"
3839

3940
[target.'cfg(target_arch = "wasm32")'.dependencies]
4041
web-sys = { version = "0.3.53", features = ["Request", "Window", "Response", "AudioContext", "AudioBuffer", "AudioContextOptions", "AudioNode", "AudioBufferSourceNode", "AudioDestinationNode"] }

Diff for: fyrox-core/src/lib.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::{
3030
path::{Path, PathBuf},
3131
};
3232

33+
use bytemuck::Pod;
3334
pub mod color;
3435
pub mod color_gradient;
3536
pub mod io;
@@ -66,7 +67,6 @@ pub use wasm_bindgen_futures;
6667
pub use web_sys;
6768

6869
pub use type_traits::prelude::*;
69-
7070
/// Defines as_(variant), as_mut_(variant) and is_(variant) methods.
7171
#[macro_export]
7272
macro_rules! define_is_as {
@@ -321,7 +321,7 @@ pub fn value_as_u8_slice<T: Sized>(v: &T) -> &'_ [u8] {
321321
}
322322

323323
/// Takes a vector of trivially-copyable values and turns it into a vector of bytes.
324-
pub fn transmute_vec_as_bytes<T: Copy>(vec: Vec<T>) -> Vec<u8> {
324+
pub fn transmute_vec_as_bytes<T: Pod>(vec: Vec<T>) -> Vec<u8> {
325325
unsafe {
326326
let mut vec = std::mem::ManuallyDrop::new(vec);
327327
Vec::from_raw_parts(
@@ -449,15 +449,15 @@ where
449449
mod test {
450450
use std::path::Path;
451451

452-
use fxhash::FxHashMap;
453-
use uuid::uuid;
454-
455452
use crate::{
456453
append_extension, cmp_strings_case_insensitive, combine_uuids, hash_combine,
457-
make_relative_path,
454+
make_relative_path, transmute_vec_as_bytes,
458455
visitor::{Visit, Visitor},
459456
BiDirHashMap,
460457
};
458+
use fxhash::FxHashMap;
459+
use std::mem::size_of;
460+
use uuid::uuid;
461461

462462
#[test]
463463
fn test_combine_uuids() {
@@ -627,4 +627,20 @@ mod test {
627627
assert!(!cmp_strings_case_insensitive("FooBaz", "FOOBaR"));
628628
assert!(cmp_strings_case_insensitive("foobar", "foobar"));
629629
}
630+
631+
#[test]
632+
fn test_transmute_vec_as_bytes_length_new_f32() {
633+
let vec = vec![1.0f32, 2.0, 3.0];
634+
let byte_vec = transmute_vec_as_bytes(vec.clone());
635+
let expected_length = vec.len() * size_of::<f32>();
636+
assert_eq!(byte_vec.len(), expected_length);
637+
}
638+
639+
#[test]
640+
fn test_transmute_vec_as_bytes_length_new_usize() {
641+
let vec = vec![1usize, 2, 3];
642+
let byte_vec = transmute_vec_as_bytes(vec.clone());
643+
let expected_length = vec.len() * size_of::<usize>();
644+
assert_eq!(byte_vec.len(), expected_length);
645+
}
630646
}

Diff for: fyrox-impl/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ strum = "0.26.1"
4040
strum_macros = "0.26.1"
4141
clap = { version = "4", features = ["derive"] }
4242
winit = { version = "0.29.2", features = ["serde"] }
43-
half = "2.2.1"
43+
half = { version = "2.2.1", features = ["bytemuck"] }
4444
fast_image_resize = "4.0.0"
4545
base64 = "0.22.1"
4646
uvgen = "0.1.0"
4747
lightmap = "0.1.1"
4848
libloading = "0.8.1"
4949
gltf = { version = "1.4.0", optional = true, default-features = false, features = ["names", "utils"] }
50-
50+
bytemuck = { version = "1.16.1", features = ["derive"]}
5151
# These dependencies isn't actually used by the engine, but it is needed to prevent cargo from rebuilding
5252
# the engine lib on different packages.
5353
hashbrown = { version = "0.14.3", features = ["raw"] }
@@ -67,4 +67,4 @@ raw-window-handle = "0.5.0"
6767
serde-wasm-bindgen = "0.6.3"
6868

6969
[target.'cfg(target_os = "android")'.dependencies]
70-
winit = { version = "0.29.2", features = ["android-native-activity"] }
70+
winit = { version = "0.29.2", features = ["android-native-activity"] }

Diff for: fyrox-impl/src/scene/mesh/surface.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::{
3737
},
3838
utils::raw_mesh::{RawMesh, RawMeshBuilder},
3939
};
40+
use bytemuck::{Pod, Zeroable};
4041
use fxhash::{FxHashMap, FxHasher};
4142
use half::f16;
4243
use lazy_static::lazy_static;
@@ -101,7 +102,7 @@ impl BlendShapesContainer {
101102
input_blend_shapes: &[InputBlendShapeData],
102103
) -> Self {
103104
#[repr(C)]
104-
#[derive(Default, Clone, Copy)]
105+
#[derive(Default, Clone, Copy, Pod, Zeroable)]
105106
struct VertexData {
106107
position: Vector3<f16>,
107108
normal: Vector3<f16>,
@@ -167,7 +168,7 @@ impl BlendShapesContainer {
167168
}
168169
}
169170

170-
let bytes = crate::core::transmute_vec_as_bytes(vertex_data);
171+
let bytes = crate::core::transmute_vec_as_bytes::<VertexData>(vertex_data);
171172

172173
assert_eq!(
173174
bytes.len(),

0 commit comments

Comments
 (0)