Skip to content

Commit

Permalink
Add initial arch module
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Jan 27, 2021
1 parent 353bb96 commit 8732b84
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions crates/spirv-builder/src/test/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,17 @@ pub fn create_uninit_and_write() {
#[spirv(fragment)]
pub fn main() {}"#);
}

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

2 changes: 1 addition & 1 deletion crates/spirv-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository = "https://github.com/EmbarkStudios/rust-gpu"
description = "Standard functions and types for SPIR-V"

[dependencies]
glam = { version = "0.11.3", default-features = false, features = ["libm", "scalar-math"] }
glam = { version = "0.12.0", default-features = false, features = ["libm", "scalar-math"] }
num-traits = { version = "0.2.14", default-features = false }
spirv-std-macros = { path = "../spirv-std-macros" }

Expand Down
29 changes: 29 additions & 0 deletions crates/spirv-std/src/arch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! SPIR-V Instrinics
//!
//! This module is intended as a low level abstraction over SPIR-V instructions.
//! These functions will typically map to a single instruction, and will perform
//! no additional safety checks beyond type-checking.
use crate::{scalar::Scalar, vector::Vector};

/// Extract a single, dynamically selected, component of a vector.
///
/// # 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 = "OpVectorExtractDynamic")]
#[inline]
pub unsafe fn vector_extract_dynamic<T: Scalar, V: Vector<T>>(vector: V, index: usize) -> T {
let mut result = T::default();

asm! {
"%vector = OpLoad _ {vector}",
"%element = OpVectorExtractDynamic _ %vector {index}",
"OpStore {element} %element",
vector = in(reg) &vector,
index = in(reg) index,
element = in(reg) &mut result
}

result
}
4 changes: 4 additions & 0 deletions crates/spirv-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@
#[macro_use]
pub extern crate spirv_std_macros;

pub mod arch;
pub mod derivative;
pub mod storage_class;
pub mod scalar;
pub(crate) mod sealed;
pub mod vector;
mod textures;

pub use glam;
Expand Down
7 changes: 7 additions & 0 deletions crates/spirv-std/src/scalar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Abstract trait representing a SPIR-V scalar type.
pub trait Scalar: Copy + Default + crate::sealed::Sealed {}

impl Scalar for bool {}
impl Scalar for f32 {}
impl Scalar for u32 {}
impl Scalar for i32 {}
21 changes: 21 additions & 0 deletions crates/spirv-std/src/sealed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// A marker trait used to prevent other traits from being implemented outside
/// of `spirv-std`.
pub trait Sealed {}

impl Sealed for bool {}
impl Sealed for f32 {}
impl Sealed for u32 {}
impl Sealed for i32 {}
impl Sealed for glam::BVec2 {}
impl Sealed for glam::BVec3 {}
impl Sealed for glam::BVec4 {}
impl Sealed for glam::Vec2 {}
impl Sealed for glam::Vec3 {}
impl Sealed for glam::Vec3A {}
impl Sealed for glam::Vec4 {}
impl Sealed for glam::UVec2 {}
impl Sealed for glam::UVec3 {}
impl Sealed for glam::UVec4 {}
impl Sealed for glam::IVec2 {}
impl Sealed for glam::IVec3 {}
impl Sealed for glam::IVec4 {}
16 changes: 16 additions & 0 deletions crates/spirv-std/src/vector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// Abstract trait representing a SPIR-V vector type.
pub trait Vector<T: crate::scalar::Scalar>: crate::sealed::Sealed {}

impl Vector<bool> for glam::BVec2 {}
impl Vector<bool> for glam::BVec3 {}
impl Vector<bool> for glam::BVec4 {}
impl Vector<f32> for glam::Vec2 {}
impl Vector<f32> for glam::Vec3 {}
impl Vector<f32> for glam::Vec3A {}
impl Vector<f32> for glam::Vec4 {}
impl Vector<u32> for glam::UVec2 {}
impl Vector<u32> for glam::UVec3 {}
impl Vector<u32> for glam::UVec4 {}
impl Vector<i32> for glam::IVec2 {}
impl Vector<i32> for glam::IVec3 {}
impl Vector<i32> for glam::IVec4 {}

0 comments on commit 8732b84

Please sign in to comment.