Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ScalarOrVector<S> trait #1030

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions crates/spirv-std/src/vector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Traits related to vectors.

use crate::scalar::Scalar;

/// Abstract trait representing a SPIR-V vector type.
///
/// # Safety
Expand All @@ -23,3 +25,54 @@ unsafe impl Vector<u32, 4> for glam::UVec4 {}
unsafe impl Vector<i32, 2> for glam::IVec2 {}
unsafe impl Vector<i32, 3> for glam::IVec3 {}
unsafe impl Vector<i32, 4> for glam::IVec4 {}

/// Abstract trait representing a SPIR-V scalar or vector type.
///
/// # Safety
/// Implementing this trait on non-simd-vector/scalar types breaks assumptions of other unsafe code, and
/// should not be done.
pub unsafe trait ScalarOrVector {
/// The scalar type
type Scalar;

/// The dimension of the vector, or 1 if it is a scalar
const DIM: usize;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use std::num::NonZeroUsize here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, theoretically you could do it, but I am not sure if it is currently supported in Rust-Gpu.

}

unsafe impl<S: Scalar> ScalarOrVector for S {
type Scalar = S;
const DIM: usize = 1;
}

macro_rules! impl_vector_or_scalar {
($(for $scalar_ty:ty, $dim: literal: unsafe impl ScalarOrVector for $typ:ty;)+) => {
$(
unsafe impl ScalarOrVector for $typ {
type Scalar = $scalar_ty;
const DIM: usize = $dim;
}
)+
}
}

impl_vector_or_scalar! {
for bool, 2: unsafe impl ScalarOrVector for glam::BVec2;
for bool, 3: unsafe impl ScalarOrVector for glam::BVec3;
for bool, 4: unsafe impl ScalarOrVector for glam::BVec4;

for f32, 2: unsafe impl ScalarOrVector for glam::Vec2;
for f32, 3: unsafe impl ScalarOrVector for glam::Vec3;
for f32, 4: unsafe impl ScalarOrVector for glam::Vec4;

for f64, 2: unsafe impl ScalarOrVector for glam::DVec2;
for f64, 3: unsafe impl ScalarOrVector for glam::DVec3;
for f64, 4: unsafe impl ScalarOrVector for glam::DVec4;

for u32, 2: unsafe impl ScalarOrVector for glam::UVec2;
for u32, 3: unsafe impl ScalarOrVector for glam::UVec3;
for u32, 4: unsafe impl ScalarOrVector for glam::UVec4;

for i32, 2: unsafe impl ScalarOrVector for glam::IVec2;
for i32, 3: unsafe impl ScalarOrVector for glam::IVec3;
for i32, 4: unsafe impl ScalarOrVector for glam::IVec4;
}
Comment on lines +58 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nicer to make this one per line, like i32: IVec2 => 2, IVec3 => 3, IVec4 => 4;
Also, you can make the macro implement both Vector and ScalarOrVector at the same time.