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 vector_insert_dynamic #411

Merged
merged 5 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions crates/spirv-builder/src/test/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,33 @@ pub fn main() {
"#);
}

#[test]
fn vector_insert_dynamic() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::Vec2::new(1.0, 2.0);
let expected = glam::Vec2::new(1.0, 3.0);
let new_vector = unsafe { spirv_std::arch::vector_insert_dynamic(vector, 1, 3.0) };
assert!(new_vector == expected);
}
"#);
}

#[test]
fn copy_object() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::Vec2::new(1.0, 2.0);
let new_vector = unsafe { spirv_std::arch::copy_object(vector) };
assert!(new_vector == vector);
}
"#);
}

#[test]
fn mat3_vec3_multiply() {
val(r#"
Expand Down
44 changes: 44 additions & 0 deletions crates/spirv-std/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,47 @@ pub unsafe fn vector_extract_dynamic<T: Scalar, V: Vector<T>>(vector: V, index:

result
}

/// Make a copy of a vector, with a single, variably selected,
/// component modified.
///
/// # Safety
/// Behavior is undefined if `index`’s value is greater than or equal to the
/// number of components in `vector`.
#[spirv_std_macros::gpu_only]
#[doc(alias = "OpVectorInsertDynamic")]
#[inline]
pub unsafe fn vector_insert_dynamic<T: Scalar, V: Vector<T>>(vector: V, index: usize, element: T) -> V {
let mut result = V::default();

asm! {
"%vector = OpLoad _ {vector}",
"%new_vector = OpVectorInsertDynamic _ %vector {element} {index}",
XAMPPRocky marked this conversation as resolved.
Show resolved Hide resolved
"OpStore {result} %new_vector",
vector = in(reg) &vector,
index = in(reg) index,
element = in(reg) &element,
result = in(reg) &mut result,
}

result
}

/// Make a copy of `object`.
///
/// # Safety
/// `T` must be a composite type (vector, array, or matrix).
#[spirv_std_macros::gpu_only]
#[doc(alias = "OpCopyObject")]
#[inline]
pub unsafe fn copy_object<T>(object: T) -> T {
XAMPPRocky marked this conversation as resolved.
Show resolved Hide resolved
let mut result = core::mem::MaybeUninit::uninit();

asm! {
"{copy} = OpCopyObject {object}",
object = in(reg) &object,
copy = in(reg) result.as_mut_ptr(),
}

result.assume_init()
}
2 changes: 1 addition & 1 deletion crates/spirv-std/src/vector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Abstract trait representing a SPIR-V vector type.
pub trait Vector<T: crate::scalar::Scalar>: crate::sealed::Sealed {}
pub trait Vector<T: crate::scalar::Scalar>: crate::sealed::Sealed + Default {}

impl Vector<bool> for glam::BVec2 {}
impl Vector<bool> for glam::BVec3 {}
Expand Down