From cb0812aaf31f573dc0724240fa306d57bdfcec70 Mon Sep 17 00:00:00 2001 From: CAD97 Date: Mon, 28 Jun 2021 20:36:28 -0500 Subject: [PATCH 1/3] Add bytemuck impls to geometry types --- src/geometry/dual_quaternion.rs | 6 ++++++ src/geometry/orthographic.rs | 6 ++++++ src/geometry/perspective.rs | 6 ++++++ src/geometry/rotation.rs | 16 ++++++++++++++++ src/geometry/transform.rs | 18 ++++++++++++++++++ src/geometry/translation.rs | 16 ++++++++++++++++ 6 files changed, 68 insertions(+) diff --git a/src/geometry/dual_quaternion.rs b/src/geometry/dual_quaternion.rs index 63148836..d745f1d3 100644 --- a/src/geometry/dual_quaternion.rs +++ b/src/geometry/dual_quaternion.rs @@ -241,6 +241,12 @@ where } } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for DualQuaternion where Quaternion: bytemuck::Zeroable {} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for DualQuaternion where Quaternion: bytemuck::Pod {} + #[cfg(feature = "serde-serialize-no-std")] impl Serialize for DualQuaternion where diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 17a5b969..ade84092 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -44,6 +44,12 @@ impl PartialEq for Orthographic3 { } } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for Orthographic3 where Matrix4: bytemuck::Zeroable {} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for Orthographic3 where Matrix4: bytemuck::Pod {} + #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Orthographic3 { fn serialize(&self, serializer: S) -> Result diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index 6ad9707f..c7dd8b3e 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -45,6 +45,12 @@ impl PartialEq for Perspective3 { } } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for Perspective3 where Matrix4: bytemuck::Zeroable {} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for Perspective3 where Matrix4: bytemuck::Pod {} + #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Perspective3 { fn serialize(&self, serializer: S) -> Result diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index f3127fb9..89bc082f 100755 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -83,6 +83,22 @@ where } } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for Rotation +where + T: Scalar, + SMatrix: bytemuck::Zeroable, +{ +} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for Rotation +where + T: Scalar, + SMatrix: bytemuck::Pod, +{ +} + #[cfg(feature = "abomonation-serialize")] impl Abomonation for Rotation where diff --git a/src/geometry/transform.rs b/src/geometry/transform.rs index 682d2bd6..051fd3d5 100755 --- a/src/geometry/transform.rs +++ b/src/geometry/transform.rs @@ -194,6 +194,24 @@ where } } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for Transform +where + Const: DimNameAdd, + DefaultAllocator: Allocator, U1>, DimNameSum, U1>>, + OMatrix, U1>, DimNameSum, U1>>: bytemuck::Zeroable, +{ +} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for Transform +where + Const: DimNameAdd, + DefaultAllocator: Allocator, U1>, DimNameSum, U1>>, + OMatrix, U1>, DimNameSum, U1>>: bytemuck::Pod, +{ +} + #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Transform where diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 18fa7e04..758a21ca 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -50,6 +50,22 @@ where } } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for Translation +where + T: Scalar, + SVector: bytemuck::Zeroable, +{ +} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for Translation +where + T: Scalar, + SVector: bytemuck::Pod, +{ +} + #[cfg(feature = "abomonation-serialize")] impl Abomonation for Translation where From 396d3d661512284f147b3ba3500beb3e3c9a5751 Mon Sep 17 00:00:00 2001 From: CAD97 Date: Mon, 28 Jun 2021 20:49:57 -0500 Subject: [PATCH 2/3] Add missing repr(C) on bytemuckable geometry types --- src/geometry/orthographic.rs | 1 + src/geometry/perspective.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index ade84092..836edc93 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -18,6 +18,7 @@ use crate::base::{Matrix4, Vector, Vector3}; use crate::geometry::{Point3, Projective3}; /// A 3D orthographic projection stored as a homogeneous 4x4 matrix. +#[repr(C)] pub struct Orthographic3 { matrix: Matrix4, } diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index c7dd8b3e..08b2608b 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -19,6 +19,7 @@ use crate::base::{Matrix4, Scalar, Vector, Vector3}; use crate::geometry::{Point3, Projective3}; /// A 3D perspective projection stored as a homogeneous 4x4 matrix. +#[repr(C)] pub struct Perspective3 { matrix: Matrix4, } From 22ba88353a78420e6f523b8293f7e521a22b6b1d Mon Sep 17 00:00:00 2001 From: CAD97 Date: Fri, 9 Jul 2021 15:23:08 -0500 Subject: [PATCH 3/3] Fix bounds for bytemuck impls --- src/geometry/dual_quaternion.rs | 14 ++++++++++++-- src/geometry/orthographic.rs | 14 ++++++++++++-- src/geometry/perspective.rs | 14 ++++++++++++-- src/geometry/rotation.rs | 4 ++-- src/geometry/transform.rs | 7 +++++-- src/geometry/translation.rs | 4 ++-- 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/geometry/dual_quaternion.rs b/src/geometry/dual_quaternion.rs index d745f1d3..376c5fbb 100644 --- a/src/geometry/dual_quaternion.rs +++ b/src/geometry/dual_quaternion.rs @@ -242,10 +242,20 @@ where } #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Zeroable for DualQuaternion where Quaternion: bytemuck::Zeroable {} +unsafe impl bytemuck::Zeroable for DualQuaternion +where + T: Scalar + bytemuck::Zeroable, + Quaternion: bytemuck::Zeroable, +{ +} #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Pod for DualQuaternion where Quaternion: bytemuck::Pod {} +unsafe impl bytemuck::Pod for DualQuaternion +where + T: Scalar + bytemuck::Pod, + Quaternion: bytemuck::Pod, +{ +} #[cfg(feature = "serde-serialize-no-std")] impl Serialize for DualQuaternion diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 836edc93..4f7b909b 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -46,10 +46,20 @@ impl PartialEq for Orthographic3 { } #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Zeroable for Orthographic3 where Matrix4: bytemuck::Zeroable {} +unsafe impl bytemuck::Zeroable for Orthographic3 +where + T: RealField + bytemuck::Zeroable, + Matrix4: bytemuck::Zeroable, +{ +} #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Pod for Orthographic3 where Matrix4: bytemuck::Pod {} +unsafe impl bytemuck::Pod for Orthographic3 +where + T: RealField + bytemuck::Pod, + Matrix4: bytemuck::Pod, +{ +} #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Orthographic3 { diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index 08b2608b..90cf95d8 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -47,10 +47,20 @@ impl PartialEq for Perspective3 { } #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Zeroable for Perspective3 where Matrix4: bytemuck::Zeroable {} +unsafe impl bytemuck::Zeroable for Perspective3 +where + T: RealField + bytemuck::Zeroable, + Matrix4: bytemuck::Zeroable, +{ +} #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Pod for Perspective3 where Matrix4: bytemuck::Pod {} +unsafe impl bytemuck::Pod for Perspective3 +where + T: RealField + bytemuck::Pod, + Matrix4: bytemuck::Pod, +{ +} #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Perspective3 { diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 89bc082f..6fd9e803 100755 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -86,7 +86,7 @@ where #[cfg(feature = "bytemuck")] unsafe impl bytemuck::Zeroable for Rotation where - T: Scalar, + T: Scalar + bytemuck::Zeroable, SMatrix: bytemuck::Zeroable, { } @@ -94,7 +94,7 @@ where #[cfg(feature = "bytemuck")] unsafe impl bytemuck::Pod for Rotation where - T: Scalar, + T: Scalar + bytemuck::Pod, SMatrix: bytemuck::Pod, { } diff --git a/src/geometry/transform.rs b/src/geometry/transform.rs index 051fd3d5..51a8f64d 100755 --- a/src/geometry/transform.rs +++ b/src/geometry/transform.rs @@ -195,8 +195,9 @@ where } #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Zeroable for Transform +unsafe impl bytemuck::Zeroable for Transform where + T: RealField + bytemuck::Zeroable, Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>>, OMatrix, U1>, DimNameSum, U1>>: bytemuck::Zeroable, @@ -204,11 +205,13 @@ where } #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Pod for Transform +unsafe impl bytemuck::Pod for Transform where + T: RealField + bytemuck::Pod, Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>>, OMatrix, U1>, DimNameSum, U1>>: bytemuck::Pod, + Owned, U1>, DimNameSum, U1>>: Copy, { } diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 758a21ca..7de9bb04 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -53,7 +53,7 @@ where #[cfg(feature = "bytemuck")] unsafe impl bytemuck::Zeroable for Translation where - T: Scalar, + T: Scalar + bytemuck::Zeroable, SVector: bytemuck::Zeroable, { } @@ -61,7 +61,7 @@ where #[cfg(feature = "bytemuck")] unsafe impl bytemuck::Pod for Translation where - T: Scalar, + T: Scalar + bytemuck::Pod, SVector: bytemuck::Pod, { }