From 3a37283c84f59e0af9dbca10c42251e85abe051c Mon Sep 17 00:00:00 2001 From: Lukas Lihotzki Date: Sun, 21 Jul 2024 19:46:58 +0200 Subject: [PATCH] Support multiple backends in blade-macros --- blade-graphics/Cargo.toml | 2 ++ blade-graphics/src/gles/mod.rs | 3 +++ blade-graphics/src/lib.rs | 2 +- blade-graphics/src/vulkan/mod.rs | 3 +++ blade-macros/src/lib.rs | 26 +++++++++++++++++++++++--- blade-macros/src/shader_data.rs | 8 ++++---- 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/blade-graphics/Cargo.toml b/blade-graphics/Cargo.toml index 667261c6..1241c63c 100644 --- a/blade-graphics/Cargo.toml +++ b/blade-graphics/Cargo.toml @@ -12,6 +12,7 @@ repository = "https://github.com/kvark/blade" [features] default = ["gles"] gles = ["dep:glow", "dep:egl", "dep:libloading", "naga/glsl-out"] +derive = ["dep:blade-macros"] [dependencies] bitflags = { workspace = true } @@ -23,6 +24,7 @@ mint = { workspace = true } naga = { workspace = true } raw-window-handle = "0.6" glow = { version = "0.13", optional = true } +blade-macros = { "path" = "../blade-macros", optional = true } [target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] block = "0.1" diff --git a/blade-graphics/src/gles/mod.rs b/blade-graphics/src/gles/mod.rs index a8912646..112848ed 100644 --- a/blade-graphics/src/gles/mod.rs +++ b/blade-graphics/src/gles/mod.rs @@ -576,3 +576,6 @@ mod derive; #[path = "../common/util.rs"] mod util; + +#[cfg(feature = "derive")] +pub use blade_macros::ShaderDataGles as ShaderData; diff --git a/blade-graphics/src/lib.rs b/blade-graphics/src/lib.rs index 8707644a..8ff7a1fc 100644 --- a/blade-graphics/src/lib.rs +++ b/blade-graphics/src/lib.rs @@ -67,7 +67,7 @@ pub mod gles; ), path = "vulkan/mod.rs" )] -mod hal; +pub mod hal; mod shader; pub mod traits; pub mod util; diff --git a/blade-graphics/src/vulkan/mod.rs b/blade-graphics/src/vulkan/mod.rs index cbde515f..7db09d04 100644 --- a/blade-graphics/src/vulkan/mod.rs +++ b/blade-graphics/src/vulkan/mod.rs @@ -686,3 +686,6 @@ mod derive; #[path = "../common/util.rs"] mod util; + +#[cfg(feature = "derive")] +pub use blade_macros::ShaderDataHal as ShaderData; diff --git a/blade-macros/src/lib.rs b/blade-macros/src/lib.rs index b59475ad..863ad892 100644 --- a/blade-macros/src/lib.rs +++ b/blade-macros/src/lib.rs @@ -4,6 +4,7 @@ mod shader_data; mod vertex; use proc_macro::TokenStream; +use quote::quote; /// Derive the `ShaderData` trait for a struct /// @@ -15,9 +16,28 @@ use proc_macro::TokenStream; /// sm: blade_graphics::Sampler, /// } /// ``` -#[proc_macro_derive(ShaderData)] -pub fn shader_data_derive(input: TokenStream) -> TokenStream { - let stream = match shader_data::generate(input) { +#[proc_macro_derive(ShaderDataHal)] +pub fn shader_data_hal_derive(input: TokenStream) -> TokenStream { + let stream = match shader_data::generate(input, quote! { blade_graphics::hal }) { + Ok(tokens) => tokens, + Err(err) => err.into_compile_error(), + }; + stream.into() +} + +/// Derive the `ShaderData` trait for a struct +/// +/// ## Example +/// +/// ```rust +/// #[derive(blade_macros::ShaderData)] +/// struct Test { +/// sm: blade_graphics::Sampler, +/// } +/// ``` +#[proc_macro_derive(ShaderDataGles)] +pub fn shader_data_gles_derive(input: TokenStream) -> TokenStream { + let stream = match shader_data::generate(input, quote! { blade_graphics::gles }) { Ok(tokens) => tokens, Err(err) => err.into_compile_error(), }; diff --git a/blade-macros/src/shader_data.rs b/blade-macros/src/shader_data.rs index 13ffddcb..41350ff6 100644 --- a/blade-macros/src/shader_data.rs +++ b/blade-macros/src/shader_data.rs @@ -1,7 +1,7 @@ use proc_macro::TokenStream; use quote::quote; -pub fn generate(input_stream: TokenStream) -> syn::Result { +pub fn generate(input_stream: TokenStream, namespace: proc_macro2::TokenStream) -> syn::Result { let item_struct = syn::parse::(input_stream)?; let fields = match item_struct.fields { syn::Fields::Named(ref fields) => fields, @@ -44,14 +44,14 @@ pub fn generate(input_stream: TokenStream) -> syn::Result blade_graphics::ShaderData for #struct_name<#(#generics),*> { + impl<#(#generics,)*> #namespace::ShaderData for #struct_name<#(#generics),*> { fn layout() -> blade_graphics::ShaderDataLayout { blade_graphics::ShaderDataLayout { bindings: vec![#(#bindings),*], } } - fn fill(&self, mut ctx: blade_graphics::PipelineContext) { - use blade_graphics::ShaderBindable as _; + fn fill(&self, mut ctx: #namespace::PipelineContext) { + use #namespace::ShaderBindable as _; #(#assignments)* } }