Skip to content

Commit

Permalink
Merge pull request #89 from SlimeYummy/feat/adjust
Browse files Browse the repository at this point in the history
some small adjust
  • Loading branch information
SlimeYummy authored Aug 22, 2024
2 parents c59ad47 + 7405943 commit 8c5e8f4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ozz-animation-rs"
version = "0.9.2"
version = "0.10.0"
authors = ["SlimeYummy <zzzcccnnn@outlook.com>"]
edition = "2021"
rust-version = "1.75"
Expand Down Expand Up @@ -32,6 +32,8 @@ thiserror = "1.0"
wasm-bindgen = { version = "0.2", optional = true }

[dev-dependencies]
miniz_oxide = "0.7"
getrandom = { version = "0.2", features = ["js"] }
miniz_oxide = "0.8"
rand = "0.8"
serde_json = "1.0"
wasm-bindgen-test = "0.3"
13 changes: 11 additions & 2 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub trait ArchiveRead<T> {

/// Reads `Vec<T>` from the archive.
/// * `count` - The number of elements to read.
#[inline]
fn read_vec<R: Read>(archive: &mut Archive<R>, count: usize) -> Result<Vec<T>, OzzError> {
let mut buffer = Vec::with_capacity(count);
for _ in 0..count {
Expand All @@ -132,10 +133,11 @@ pub trait ArchiveRead<T> {
macro_rules! primitive_reader {
($type:ty) => {
impl ArchiveRead<$type> for $type {
#[inline]
fn read<R: Read>(archive: &mut Archive<R>) -> Result<$type, OzzError> {
let val = Default::default();
let mut val = Default::default();
archive.read.read_exact(unsafe {
slice::from_raw_parts_mut(&val as *const $type as *mut u8, mem::size_of::<$type>())
slice::from_raw_parts_mut(&mut val as *const $type as *mut u8, mem::size_of::<$type>())
})?;
if !archive.endian_swap {
return Ok(val);
Expand All @@ -160,6 +162,7 @@ primitive_reader!(f32);
primitive_reader!(f64);

impl ArchiveRead<Vec2> for Vec2 {
#[inline]
fn read<R: Read>(archive: &mut Archive<R>) -> Result<Vec2, OzzError> {
let x = f32::read(archive)?;
let y = f32::read(archive)?;
Expand All @@ -168,6 +171,7 @@ impl ArchiveRead<Vec2> for Vec2 {
}

impl ArchiveRead<Vec3> for Vec3 {
#[inline]
fn read<R: Read>(archive: &mut Archive<R>) -> Result<Vec3, OzzError> {
let x = f32::read(archive)?;
let y = f32::read(archive)?;
Expand All @@ -177,6 +181,7 @@ impl ArchiveRead<Vec3> for Vec3 {
}

impl ArchiveRead<Vec4> for Vec4 {
#[inline]
fn read<R: Read>(archive: &mut Archive<R>) -> Result<Vec4, OzzError> {
let x = f32::read(archive)?;
let y = f32::read(archive)?;
Expand All @@ -187,6 +192,7 @@ impl ArchiveRead<Vec4> for Vec4 {
}

impl ArchiveRead<Quat> for Quat {
#[inline]
fn read<R: Read>(archive: &mut Archive<R>) -> Result<Quat, OzzError> {
let x = f32::read(archive)?;
let y = f32::read(archive)?;
Expand All @@ -197,6 +203,7 @@ impl ArchiveRead<Quat> for Quat {
}

impl ArchiveRead<String> for String {
#[inline]
fn read<R: Read>(archive: &mut Archive<R>) -> Result<String, OzzError> {
let mut buffer = Vec::new();
loop {
Expand All @@ -223,5 +230,7 @@ mod tests {
fn test_archive_new() {
let archive = Archive::from_path("./resource/playback/animation.ozz").unwrap();
assert_eq!(archive.endian_swap, false);
assert_eq!(archive.tag, "ozz-animation");
assert_eq!(archive.version, 7);
}
}
16 changes: 14 additions & 2 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ ozz_index!(usize);
ozz_index!(i32);
ozz_index!(i16);

#[inline(always)]
pub(crate) fn align_usize(size: usize, align: usize) -> usize {
assert!(align.is_power_of_two());
return (size + align - 1) & !(align - 1);
}

#[inline(always)]
pub(crate) fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
assert!(align.is_power_of_two());
return align_usize(ptr as usize, align) as *mut u8;
}

/// Represents a reference to the ozz resource object.
/// `T` usually is `Skeleton` or `Animation`.
///
Expand Down Expand Up @@ -407,7 +419,7 @@ impl<'t, T> DerefMut for ObRwLockWriteGuard<'t, T> {
}
}

/// Shortcuts for Rc<RefCell<T>>.
/// Shortcuts for `Rc<RefCell<T>>`.
pub type OzzRcBuf<T> = Rc<RefCell<Vec<T>>>;

/// Creates a new `Rc<RefCell<Vec<T>>>`.
Expand All @@ -416,7 +428,7 @@ pub fn ozz_rc_buf<T>(v: Vec<T>) -> OzzRcBuf<T> {
return Rc::new(RefCell::new(v));
}

/// Shortcuts for Arc<RwLock<T>>.
/// Shortcuts for `Arc<RwLock<T>>`.
pub type OzzArcBuf<T> = Arc<RwLock<Vec<T>>>;

/// Creates a new `Arc<RwLock<Vec<T>>>`.
Expand Down
15 changes: 5 additions & 10 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,6 @@ impl SoaVec3 {
};
}

#[inline]
pub fn component_mul(&self, other: &SoaVec3) -> SoaVec3 {
return SoaVec3 {
x: self.x * other.x,
y: self.y * other.y,
z: self.z * other.z,
};
}

#[inline]
pub fn mul_num(&self, f: f32x4) -> SoaVec3 {
return SoaVec3 {
Expand Down Expand Up @@ -482,6 +473,7 @@ pub struct SoaTransform {
}

impl ArchiveRead<SoaTransform> for SoaTransform {
#[inline]
fn read<R: Read>(archive: &mut Archive<R>) -> Result<SoaTransform, OzzError> {
const COUNT: usize = mem::size_of::<SoaTransform>() / mem::size_of::<f32>();
let mut buffer = [0f32; COUNT];
Expand All @@ -493,6 +485,7 @@ impl ArchiveRead<SoaTransform> for SoaTransform {
}

impl SoaTransform {
#[inline]
pub fn new(translation: SoaVec3, rotation: SoaQuat, scale: SoaVec3) -> SoaTransform {
return SoaTransform {
translation,
Expand Down Expand Up @@ -849,6 +842,7 @@ impl SoaMat4 {
// functions
//

#[inline]
pub(crate) fn f16_to_f32(n: u16) -> f32 {
let sign = (n & 0x8000) as u32;
let expo = (n & 0x7C00) as u32;
Expand All @@ -872,6 +866,7 @@ pub(crate) fn f16_to_f32(n: u16) -> f32 {
};
}

#[inline]
pub(crate) fn simd_f16_to_f32(half4: [u16; 4]) -> f32x4 {
const MASK_NO_SIGN: i32x4 = i32x4::from_array([0x7FFF; 4]);
const MAGIC: f32x4 = fx4(i32x4::from_array([(254 - 15) << 23; 4]));
Expand Down Expand Up @@ -1528,7 +1523,7 @@ mod tests {
let json = serde_json::to_string(&vec3).unwrap();
let vec3_de: SoaVec3 = serde_json::from_str(&json).unwrap();
assert_eq!(vec3_de, vec3);

let quat = SoaQuat::splat_col([2.0, 3.0, 4.0, 5.0]);
let json = serde_json::to_string(&quat).unwrap();
let quat_de: SoaQuat = serde_json::from_str(&json).unwrap();
Expand Down

0 comments on commit 8c5e8f4

Please sign in to comment.