From 40163c804a4ebb7fe2f31939833ffbfd40e11ac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Miguel=20S=C3=A1nchez=20Garc=C3=ADa?= Date: Mon, 26 Feb 2024 21:25:23 +0100 Subject: [PATCH] vulkano-shaders: support `glam` as a linear algebra backend --- vulkano-shaders/src/lib.rs | 8 ++++- vulkano-shaders/src/structs.rs | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/vulkano-shaders/src/lib.rs b/vulkano-shaders/src/lib.rs index b78153d78c..377de2d50b 100644 --- a/vulkano-shaders/src/lib.rs +++ b/vulkano-shaders/src/lib.rs @@ -188,6 +188,7 @@ //! //! - `std` //! - `cgmath` +//! - `glam` //! - `nalgebra` //! //! The default is `std`, which uses arrays to represent vectors and matrices. Note that if the @@ -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" => { @@ -749,6 +754,7 @@ enum LinAlgType { #[default] Std, CgMath, + Glam, Nalgebra, } diff --git a/vulkano-shaders/src/structs.rs b/vulkano-shaders/src/structs.rs index 6d44408bd7..b4c8fd5032 100644 --- a/vulkano-shaders/src/structs.rs +++ b/vulkano-shaders/src/structs.rs @@ -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> @@ -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 => {