-
-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #977 from tychedelia/shader-model
Introduce ShaderModel.
- Loading branch information
Showing
32 changed files
with
1,019 additions
and
989 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "bevy_nannou_derive" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = { version = "1", features = ["full"] } | ||
quote = "1.0" | ||
proc-macro2 = "1.0" | ||
|
||
[dev-dependencies] | ||
bevy = { workspace = true } | ||
bevy_nannou_draw = { path = "../bevy_nannou_draw" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, parse_quote, ItemStruct, Lit, Meta, NestedMeta}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn shader_model(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let mut input = parse_macro_input!(item as ItemStruct); | ||
let attrs = parse_macro_input!(attr as syn::AttributeArgs); | ||
|
||
let (vertex_shader, fragment_shader) = parse_shader_attributes(&attrs); | ||
|
||
let name = &input.ident; | ||
let generics = &input.generics; | ||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); | ||
|
||
let vertex_shader_impl = vertex_shader | ||
.map(|path| quote! { #path.into() }) | ||
.unwrap_or_else(|| quote! { ::nannou::prelude::ShaderRef::Default }); | ||
|
||
let fragment_shader_impl = fragment_shader | ||
.map(|path| quote! { #path.into() }) | ||
.unwrap_or_else(|| quote! { ::nannou::prelude::ShaderRef::Default }); | ||
|
||
// Add derive attributes | ||
input | ||
.attrs | ||
.push(parse_quote!(#[derive(Asset, TypePath, AsBindGroup, Debug, Clone, Default)])); | ||
|
||
let expanded = quote! { | ||
#input | ||
|
||
impl #impl_generics ::nannou::prelude::render::ShaderModel for #name #ty_generics #where_clause { | ||
fn vertex_shader() -> ::nannou::prelude::ShaderRef { | ||
#vertex_shader_impl | ||
} | ||
|
||
fn fragment_shader() -> ::nannou::prelude::ShaderRef { | ||
#fragment_shader_impl | ||
} | ||
} | ||
|
||
impl #impl_generics ::nannou::prelude::Material for #name #ty_generics #where_clause { | ||
fn vertex_shader() -> ::nannou::prelude::ShaderRef { | ||
<Self as ::nannou::prelude::render::ShaderModel>::vertex_shader() | ||
} | ||
|
||
fn fragment_shader() -> ::nannou::prelude::ShaderRef { | ||
<Self as ::nannou::prelude::render::ShaderModel>::fragment_shader() | ||
} | ||
} | ||
}; | ||
|
||
TokenStream::from(expanded) | ||
} | ||
|
||
fn parse_shader_attributes(attrs: &[syn::NestedMeta]) -> (Option<String>, Option<String>) { | ||
let mut vertex_shader = None; | ||
let mut fragment_shader = None; | ||
|
||
for attr in attrs { | ||
if let NestedMeta::Meta(Meta::NameValue(nv)) = attr { | ||
if nv.path.is_ident("vertex") { | ||
if let Lit::Str(lit) = &nv.lit { | ||
vertex_shader = Some(lit.value()); | ||
} | ||
} else if nv.path.is_ident("fragment") { | ||
if let Lit::Str(lit) = &nv.lit { | ||
fragment_shader = Some(lit.value()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
(vertex_shader, fragment_shader) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use bevy::render::render_resource::ShaderRef; | ||
use bevy_nannou_derive::shader_model; | ||
use bevy_nannou_draw::render::ShaderModel; | ||
|
||
#[shader_model] | ||
struct TestMaterial {} | ||
|
||
#[test] | ||
fn test_default_shaders() { | ||
assert!(matches!(TestMaterial::vertex_shader(), ShaderRef::Default)); | ||
assert!(matches!( | ||
TestMaterial::fragment_shader(), | ||
ShaderRef::Default | ||
)); | ||
} | ||
|
||
#[shader_model(vertex = "custom_vertex.wgsl")] | ||
struct TestVertexMaterial {} | ||
|
||
#[test] | ||
fn test_custom_vertex_shader() { | ||
assert!(matches!( | ||
TestVertexMaterial::vertex_shader(), | ||
ShaderRef::Path(_) | ||
)); | ||
assert!(matches!( | ||
TestVertexMaterial::fragment_shader(), | ||
ShaderRef::Default | ||
)); | ||
} | ||
|
||
#[shader_model(fragment = "custom_fragment.wgsl")] | ||
struct TestFragmentMaterial {} | ||
|
||
#[test] | ||
fn test_custom_fragment_shader() { | ||
assert!(matches!( | ||
TestFragmentMaterial::vertex_shader(), | ||
ShaderRef::Default | ||
)); | ||
assert!(matches!( | ||
TestFragmentMaterial::fragment_shader(), | ||
ShaderRef::Path(_) | ||
)); | ||
} | ||
|
||
#[shader_model(vertex = "custom_vertex.wgsl", fragment = "custom_fragment.wgsl")] | ||
struct TestBothMaterial {} | ||
|
||
#[test] | ||
fn test_both_custom_shaders() { | ||
assert!(matches!( | ||
TestBothMaterial::vertex_shader(), | ||
ShaderRef::Path(_) | ||
)); | ||
assert!(matches!( | ||
TestBothMaterial::fragment_shader(), | ||
ShaderRef::Path(_) | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.