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

vulkano-shaders: support glam as a linear algebra backend #2479

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 7 additions & 1 deletion vulkano-shaders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
//!
//! - `std`
//! - `cgmath`
//! - `glam`
//! - `nalgebra`
//!
//! The default is `std`, which uses arrays to represent vectors and matrices. Note that if the
Expand Down Expand Up @@ -681,8 +682,12 @@ impl Parse for MacroInput {
linalg_type = Some(match lit.value().as_str() {
"std" => LinAlgType::Std,
"cgmath" => LinAlgType::CgMath,
"glam" => LinAlgType::Glam,
"nalgebra" => LinAlgType::Nalgebra,
ty => bail!(lit, "expected `std`, `cgmath` or `nalgebra`, found `{ty}`"),
ty => bail!(
lit,
"expected `std`, `cgmath`, `glam` or `nalgebra`, found `{ty}`"
),
});
}
"dump" => {
Expand Down Expand Up @@ -749,6 +754,7 @@ enum LinAlgType {
#[default]
Std,
CgMath,
Glam,
Nalgebra,
}

Expand Down
54 changes: 54 additions & 0 deletions vulkano-shaders/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,44 @@ impl ToTokens for Serializer<'_, TypeVector> {
let vector = format_ident!("Vector{}", component_count);
tokens.extend(quote! { ::cgmath::#vector<#component_type> });
}
LinAlgType::Glam => {
// jmi2k: ugly
let ty = match component_type {
TypeScalar::Float(TypeFloat {
width: FloatWidth::W32,
}) => format_ident!("Vec{}", component_count),
TypeScalar::Float(TypeFloat {
width: FloatWidth::W64,
}) => format_ident!("DVec{}", component_count),
TypeScalar::Int(TypeInt {
width: IntWidth::W16,
signed: true,
}) => format_ident!("I16Vec{}", component_count),
TypeScalar::Int(TypeInt {
width: IntWidth::W32,
signed: true,
}) => format_ident!("IVec{}", component_count),
TypeScalar::Int(TypeInt {
width: IntWidth::W64,
signed: true,
}) => format_ident!("I64Vec{}", component_count),
TypeScalar::Int(TypeInt {
width: IntWidth::W16,
signed: false,
}) => format_ident!("U16Vec{}", component_count),
TypeScalar::Int(TypeInt {
width: IntWidth::W32,
signed: false,
}) => format_ident!("UVec{}", component_count),
TypeScalar::Int(TypeInt {
width: IntWidth::W64,
signed: false,
}) => format_ident!("U64Vec{}", component_count),
// jmi2k: is this ok?
_ => unreachable!(),
};
tokens.extend(quote! { ::glam::#ty });
}
LinAlgType::Nalgebra => {
tokens.extend(quote! {
::nalgebra::base::SVector<#component_type, #component_count>
Expand Down Expand Up @@ -936,6 +974,22 @@ impl ToTokens for Serializer<'_, TypeMatrix> {
let matrix = format_ident!("Matrix{}", component_count);
tokens.extend(quote! { ::cgmath::#matrix<#component_type> });
}
// glam only has column-major matrices. It also only has square matrices, and its 3x3
// matrix is not padded right. Fall back to std for anything else.
// jmi2k: is this ok?
LinAlgType::Glam
if majorness == MatrixMajorness::ColumnMajor
&& padding == 0
&& vector_count == component_count =>
{
let ty = match component_type.width {
FloatWidth::W32 => format_ident!("Mat{}", component_count),
FloatWidth::W64 => format_ident!("DMat{}", component_count),
// jmi2k: is this ok?
_ => unreachable!(),
};
tokens.extend(quote! { ::glam::#ty });
}
// nalgebra only has column-major matrices, and its 3xN matrices are not padded right.
// Fall back to std for anything else.
LinAlgType::Nalgebra if majorness == MatrixMajorness::ColumnMajor && padding == 0 => {
Expand Down
Loading