Skip to content

Commit

Permalink
Support multiple backends in blade-macros
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed Jul 21, 2024
1 parent 622f49c commit 01be6ef
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions blade-graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,6 @@ mod util;

#[path = "../common/traits.rs"]
mod traits;

#[cfg(feature = "derive")]
pub use blade_macros::ShaderDataGles as ShaderData;
2 changes: 1 addition & 1 deletion blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub mod gles;
),
path = "vulkan/mod.rs"
)]
mod hal;
pub mod hal;

Check failure on line 64 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

file not found for module `hal`
mod shader;
pub mod traits;
pub mod util;
Expand Down
3 changes: 3 additions & 0 deletions blade-graphics/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,6 @@ mod util;

#[path = "../common/traits.rs"]
mod traits;

#[cfg(feature = "derive")]
pub use blade_macros::ShaderDataHal as ShaderData;
26 changes: 23 additions & 3 deletions blade-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod shader_data;
mod vertex;

use proc_macro::TokenStream;
use quote::quote;

/// Derive the `ShaderData` trait for a struct
///
Expand All @@ -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(),
};
Expand Down
8 changes: 4 additions & 4 deletions blade-macros/src/shader_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro::TokenStream;
use quote::quote;

pub fn generate(input_stream: TokenStream) -> syn::Result<proc_macro2::TokenStream> {
pub fn generate(input_stream: TokenStream, namespace: proc_macro2::TokenStream) -> syn::Result<proc_macro2::TokenStream> {
let item_struct = syn::parse::<syn::ItemStruct>(input_stream)?;
let fields = match item_struct.fields {
syn::Fields::Named(ref fields) => fields,
Expand Down Expand Up @@ -44,14 +44,14 @@ pub fn generate(input_stream: TokenStream) -> syn::Result<proc_macro2::TokenStre

let struct_name = item_struct.ident;
Ok(quote! {
impl<#(#generics),*> 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)*
}
}
Expand Down

0 comments on commit 01be6ef

Please sign in to comment.