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

glam support #4

Merged
merged 2 commits into from
Sep 16, 2024
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
21 changes: 14 additions & 7 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ To enable all features, including `f16` and vector constant support, use the `fu
```

### `f16` and vector specialization constants support
When querying specialization constants, spirv-cross2 includes optional support for `f16` via [half](https://crates.io/crates/half) and `Vec2`, `Vec3`, `Vec4`, and `Mat4` types
via [gfx-maths](https://crates.io/crates/gfx-maths).
When querying specialization constants, spirv-cross2 includes optional support for `f16` via [half](https://crates.io/crates/half) and vector and matrix types
via [glam](https://crates.io/crates/glam) and [gfx-maths](https://crates.io/crates/gfx-maths).

```toml
[dependencies]
spirv-cross2 = { features = ["f16", "half"] }
spirv-cross2 = { features = ["f16", "gfx-maths-types", "glam-types"] }
```

## License
Expand Down
5 changes: 4 additions & 1 deletion spirv-cross2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ bitflags = "2.6.0"

half = { version = "2.4.1", optional = true }
gfx-maths = { version = "0.2.9", optional = true }
glam = { version = "0.29.0", optional = true }

memchr = "2.7.4"

spirv = "0.3.0"

[features]
default = ["glsl", "hlsl", "msl"]
full = ["gfx-math-types", "f16", "glsl", "hlsl", "msl", "json", "cpp"]
full = ["gfx-math-types", "glam-types", "f16", "glsl", "hlsl", "msl", "json", "cpp"]

f16 = ["dep:half"]
gfx-math-types = ["dep:gfx-maths"]
glam-types = ["dep:glam"]

glsl = ["spirv-cross-sys/glsl"]
hlsl = ["spirv-cross-sys/hlsl"]
Expand Down
6 changes: 3 additions & 3 deletions spirv-cross2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@
//! ```
//!
//! ### `f16` and vector specialization constants support
//! When querying specialization constants, spirv-cross2 includes optional support for `f16` via [half](https://crates.io/crates/half) and `Vec2`, `Vec3`, `Vec4`, and `Mat4` types
//! via [gfx-maths](https://crates.io/crates/gfx-maths).
//! When querying specialization constants, spirv-cross2 includes optional support for `f16` via [half](https://crates.io/crates/half) and vector and matrix types
//! via [glam](https://crates.io/crates/glam) and [gfx-maths](https://crates.io/crates/gfx-maths).
//!
//! ```toml
//! [dependencies]
//! spirv-cross2 = { features = ["f16", "half"] }
//! spirv-cross2 = { features = ["f16", "gfx-maths-types", "glam-types"] }
//! ```
//!
//! ## Usage
Expand Down
146 changes: 34 additions & 112 deletions spirv-cross2/src/reflect/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use crate::iter::impl_iterator;
use crate::{error, Compiler, PhantomCompiler};
use spirv_cross_sys as sys;

mod gfx_maths;
mod half;
mod glam;

/// A marker trait for types that can be represented as a scalar SPIR-V constant.
pub trait ConstantScalar: Default + Sealed + Copy {
#[doc(hidden)]
Expand All @@ -34,6 +38,27 @@ macro_rules! impl_spvc_constant {
};
}

macro_rules! impl_vec_constant {
($vec_ty:ty [$base_ty:ty; $len:literal] for [$($component:ident),*]) => {
impl $crate::sealed::Sealed for $vec_ty {}
impl $crate::reflect::constants::ConstantValue for $vec_ty {
const COLUMNS: usize = 1;
const VECSIZE: usize = $len;
type BaseArrayType = [$base_ty; $len];
type ArrayType = [[$base_ty; $len]; 1];
type BaseType = $base_ty;

fn from_array(value: Self::ArrayType) -> Self {
value[0].into()
}

fn to_array(value: Self) -> Self::ArrayType {
[[$(value.$component),*]]
}
}
};
}

impl_spvc_constant!(spvc_constant_get_scalar_i8 spvc_constant_set_scalar_i8 i8);
impl_spvc_constant!(spvc_constant_get_scalar_i16 spvc_constant_set_scalar_i16 i16);
impl_spvc_constant!(spvc_constant_get_scalar_i32 spvc_constant_set_scalar_i32 i32);
Expand All @@ -47,19 +72,17 @@ impl_spvc_constant!(spvc_constant_get_scalar_u64 spvc_constant_set_scalar_u64 u6
impl_spvc_constant!(spvc_constant_get_scalar_fp32 spvc_constant_set_scalar_fp32 f32);
impl_spvc_constant!(spvc_constant_get_scalar_fp64 spvc_constant_set_scalar_fp64 f64);

#[cfg(feature = "f16")]
impl Sealed for half::f16 {}

#[cfg(feature = "f16")]
#[cfg_attr(docsrs, doc(cfg(feature = "f16")))]
impl ConstantScalar for half::f16 {
// implement manually for bool
impl Sealed for bool {}
impl ConstantScalar for bool {
unsafe fn get(constant: spvc_constant, column: u32, row: u32) -> Self {
let f32 = unsafe { sys::spvc_constant_get_scalar_fp16(constant, column, row) };
half::f16::from_f32(f32)
unsafe {
sys::spvc_constant_get_scalar_u8(constant, column, row) != 0
}
}

unsafe fn set(constant: spvc_constant, column: u32, row: u32, value: Self) {
unsafe { sys::spvc_constant_set_scalar_fp16(constant, column, row, value.to_bits()) }
sys::spvc_constant_set_scalar_u8(constant, column, row, if value { 1 } else { 0 });
}
}

Expand Down Expand Up @@ -347,109 +370,6 @@ impl<T: ConstantScalar> ConstantValue for T {
}
}

#[cfg(feature = "gfx-math-types")]
#[cfg_attr(docsrs, doc(cfg(feature = "gfx-math-types")))]
mod gfx_maths_types {
use crate::reflect::ConstantValue;
use crate::sealed::Sealed;
use gfx_maths::{Mat4, Vec2, Vec3, Vec4};

impl Sealed for Vec2 {}
impl ConstantValue for Vec2 {
const COLUMNS: usize = 1;
const VECSIZE: usize = 2;
type BaseArrayType = [f32; 2];
type ArrayType = [[f32; 2]; 1];
type BaseType = f32;

fn from_array(value: Self::ArrayType) -> Self {
value[0].into()
}

fn to_array(value: Self) -> Self::ArrayType {
[[value.x, value.y]]
}
}

impl Sealed for Vec3 {}
impl ConstantValue for Vec3 {
const COLUMNS: usize = 1;
const VECSIZE: usize = 3;
type BaseArrayType = [f32; 3];
type ArrayType = [[f32; 3]; 1];
type BaseType = f32;

fn from_array(value: Self::ArrayType) -> Self {
value[0].into()
}

fn to_array(value: Self) -> Self::ArrayType {
[[value.x, value.y, value.z]]
}
}

impl Sealed for Vec4 {}
impl ConstantValue for Vec4 {
const COLUMNS: usize = 1;
const VECSIZE: usize = 4;
type BaseArrayType = [f32; 4];
type ArrayType = [[f32; 4]; 1];
type BaseType = f32;

fn from_array(value: Self::ArrayType) -> Self {
value[0].into()
}

fn to_array(value: Self) -> Self::ArrayType {
[[value.x, value.y, value.z, value.w]]
}
}

impl Sealed for Mat4 {}
impl ConstantValue for Mat4 {
const COLUMNS: usize = 4;
const VECSIZE: usize = 4;
type BaseArrayType = [f32; 4];
type ArrayType = [[f32; 4]; 4];
type BaseType = f32;

fn from_array(value: Self::ArrayType) -> Self {
value.into()
}

fn to_array(value: Self) -> Self::ArrayType {
let mut array = [[0f32; 4]; 4];
// gfx-math uses
// so we assign it back in the same order.

// const fn cr(c: usize, r: usize) -> usize {
// r + c * 4
// }
array[0][0] = value[(0, 0)];
array[0][1] = value[(0, 1)];
array[0][2] = value[(0, 2)];
array[0][3] = value[(0, 3)];

array[1][0] = value[(1, 0)];
array[1][1] = value[(1, 1)];
array[1][2] = value[(1, 2)];
array[1][3] = value[(1, 3)];

array[2][0] = value[(2, 0)];
array[2][1] = value[(2, 1)];
array[2][2] = value[(2, 2)];
array[2][3] = value[(2, 3)];

array[3][0] = value[(3, 0)];
array[3][1] = value[(3, 1)];
array[3][2] = value[(3, 2)];
array[3][3] = value[(3, 3)];

array
}
}
}

impl<T> Compiler<T> {
/// Get the value of the specialization value.
///
Expand Down Expand Up @@ -517,3 +437,5 @@ impl<T> Compiler<T> {
Ok(())
}
}

pub(self) use impl_vec_constant;
68 changes: 68 additions & 0 deletions spirv-cross2/src/reflect/constants/gfx_maths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#![cfg(feature = "gfx-math-types")]
#![cfg_attr(docsrs, doc(cfg(feature = "gfx-math-types")))]
use crate::reflect::ConstantValue;
use crate::sealed::Sealed;
use gfx_maths::{Mat4, Vec2, Vec3, Vec4};
use crate::reflect::constants::impl_vec_constant;

impl_vec_constant!(Vec2 [f32; 2] for [x, y]);
impl_vec_constant!(Vec3 [f32; 3] for [x, y, z]);
impl_vec_constant!(Vec4 [f32; 4] for [x, y, z, w]);

impl Sealed for Mat4 {}
impl ConstantValue for Mat4 {
const COLUMNS: usize = 4;
const VECSIZE: usize = 4;
type BaseArrayType = [f32; 4];
type ArrayType = [[f32; 4]; 4];
type BaseType = f32;

fn from_array(value: Self::ArrayType) -> Self {
value.into()
}

fn to_array(value: Self) -> Self::ArrayType {
let mut array = [[0f32; 4]; 4];
// gfx-math uses
// so we assign it back in the same order.

// const fn cr(c: usize, r: usize) -> usize {
// r + c * 4
// }
array[0][0] = value[(0, 0)];
array[0][1] = value[(0, 1)];
array[0][2] = value[(0, 2)];
array[0][3] = value[(0, 3)];

array[1][0] = value[(1, 0)];
array[1][1] = value[(1, 1)];
array[1][2] = value[(1, 2)];
array[1][3] = value[(1, 3)];

array[2][0] = value[(2, 0)];
array[2][1] = value[(2, 1)];
array[2][2] = value[(2, 2)];
array[2][3] = value[(2, 3)];

array[3][0] = value[(3, 0)];
array[3][1] = value[(3, 1)];
array[3][2] = value[(3, 2)];
array[3][3] = value[(3, 3)];

array
}
}

#[cfg(test)]
mod test {
use crate::reflect::ConstantValue;

#[test]
pub fn round_trip_mat4() {
let mat4 = gfx_maths::Mat4::inverse_orthographic_opengl(1.0, 2.0, 3.0, 4.0,5.0, 6.0);
let arr = ConstantValue::to_array(mat4.clone());
let returned = ConstantValue::from_array(arr);

assert_eq!(mat4, returned);
}
}
Loading