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 initial arch module #394

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
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
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.

13 changes: 13 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,16 @@ 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,9 +43,13 @@
#[macro_use]
pub extern crate spirv_std_macros;

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

pub use glam;
pub use num_traits;
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 {}
XAMPPRocky marked this conversation as resolved.
Show resolved Hide resolved
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 {}