2020-07-10 09:53:31 +00:00
|
|
|
use crate::modules::{get_modules, get_path};
|
2020-06-08 02:12:41 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::{format_ident, quote};
|
2020-07-10 09:53:31 +00:00
|
|
|
use syn::{
|
|
|
|
parse::ParseStream, parse_macro_input, Data, DataStruct, DeriveInput, Field, Fields, Path,
|
|
|
|
};
|
2020-06-08 02:12:41 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
2020-07-10 09:53:31 +00:00
|
|
|
struct RenderResourceFieldAttributes {
|
2020-06-08 02:12:41 +00:00
|
|
|
pub ignore: bool,
|
|
|
|
pub buffer: bool,
|
|
|
|
}
|
|
|
|
|
2020-07-10 09:53:31 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct RenderResourceAttributes {
|
|
|
|
pub from_self: bool,
|
2020-06-08 02:12:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 07:30:04 +00:00
|
|
|
static RENDER_RESOURCE_ATTRIBUTE_NAME: &str = "render_resources";
|
2020-06-08 02:12:41 +00:00
|
|
|
|
|
|
|
pub fn derive_render_resources(input: TokenStream) -> TokenStream {
|
|
|
|
let ast = parse_macro_input!(input as DeriveInput);
|
2020-07-14 23:16:01 +00:00
|
|
|
let modules = get_modules(&ast.attrs);
|
2020-06-08 02:12:41 +00:00
|
|
|
|
|
|
|
let bevy_render_path: Path = get_path(&modules.bevy_render);
|
2020-07-10 09:53:31 +00:00
|
|
|
let attributes = ast
|
|
|
|
.attrs
|
|
|
|
.iter()
|
2020-08-16 07:30:04 +00:00
|
|
|
.find(|a| *a.path.get_ident().as_ref().unwrap() == RENDER_RESOURCE_ATTRIBUTE_NAME)
|
|
|
|
.map_or_else(RenderResourceAttributes::default, |a| {
|
|
|
|
syn::custom_keyword!(from_self);
|
|
|
|
let mut attributes = RenderResourceAttributes::default();
|
|
|
|
a.parse_args_with(|input: ParseStream| {
|
|
|
|
if input.parse::<Option<from_self>>()?.is_some() {
|
|
|
|
attributes.from_self = true;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("invalid 'render_resources' attribute format");
|
2020-07-10 09:53:31 +00:00
|
|
|
|
2020-08-16 07:30:04 +00:00
|
|
|
attributes
|
|
|
|
});
|
2020-06-08 02:12:41 +00:00
|
|
|
let struct_name = &ast.ident;
|
|
|
|
let struct_name_string = struct_name.to_string();
|
|
|
|
|
|
|
|
if attributes.from_self {
|
|
|
|
TokenStream::from(quote! {
|
2020-07-17 01:26:21 +00:00
|
|
|
impl #bevy_render_path::renderer::RenderResources for #struct_name {
|
2020-06-08 02:12:41 +00:00
|
|
|
fn render_resources_len(&self) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2020-07-17 01:26:21 +00:00
|
|
|
fn get_render_resource(&self, index: usize) -> Option<&dyn #bevy_render_path::renderer::RenderResource> {
|
2020-06-08 02:12:41 +00:00
|
|
|
if index == 0 {
|
|
|
|
Some(self)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_render_resource_name(&self, index: usize) -> Option<&str> {
|
|
|
|
if index == 0 {
|
|
|
|
Some(#struct_name_string)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:03:55 +00:00
|
|
|
fn iter(&self) -> #bevy_render_path::renderer::RenderResourceIterator {
|
2020-07-17 01:26:21 +00:00
|
|
|
#bevy_render_path::renderer::RenderResourceIterator::new(self)
|
2020-06-08 02:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2020-07-10 09:53:31 +00:00
|
|
|
let fields = match &ast.data {
|
|
|
|
Data::Struct(DataStruct {
|
|
|
|
fields: Fields::Named(fields),
|
|
|
|
..
|
|
|
|
}) => &fields.named,
|
|
|
|
_ => panic!("expected a struct with named fields"),
|
|
|
|
};
|
|
|
|
let field_attributes = fields
|
|
|
|
.iter()
|
|
|
|
.map(|field| {
|
|
|
|
(
|
|
|
|
field,
|
|
|
|
field
|
|
|
|
.attrs
|
|
|
|
.iter()
|
|
|
|
.find(|a| {
|
2020-08-16 07:30:04 +00:00
|
|
|
*a.path.get_ident().as_ref().unwrap() == RENDER_RESOURCE_ATTRIBUTE_NAME
|
2020-07-10 09:53:31 +00:00
|
|
|
})
|
2020-08-16 07:30:04 +00:00
|
|
|
.map_or_else(RenderResourceFieldAttributes::default, |a| {
|
|
|
|
syn::custom_keyword!(ignore);
|
|
|
|
syn::custom_keyword!(buffer);
|
|
|
|
let mut attributes = RenderResourceFieldAttributes::default();
|
|
|
|
a.parse_args_with(|input: ParseStream| {
|
|
|
|
if input.parse::<Option<ignore>>()?.is_some() {
|
|
|
|
attributes.ignore = true;
|
|
|
|
} else if input.parse::<Option<buffer>>()?.is_some() {
|
|
|
|
attributes.buffer = true;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("invalid 'render_resources' attribute format");
|
2020-06-08 02:12:41 +00:00
|
|
|
|
2020-08-16 07:30:04 +00:00
|
|
|
attributes
|
|
|
|
}),
|
2020-07-10 09:53:31 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<(&Field, RenderResourceFieldAttributes)>>();
|
2020-06-08 02:12:41 +00:00
|
|
|
let mut render_resource_names = Vec::new();
|
|
|
|
let mut render_resource_fields = Vec::new();
|
|
|
|
let mut render_resource_hints = Vec::new();
|
|
|
|
for (field, attrs) in field_attributes.iter() {
|
|
|
|
if attrs.ignore {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let field_ident = field.ident.as_ref().unwrap();
|
|
|
|
let field_name = field_ident.to_string();
|
|
|
|
render_resource_fields.push(field_ident);
|
|
|
|
render_resource_names.push(format!("{}_{}", struct_name, field_name));
|
|
|
|
if attrs.buffer {
|
2020-07-17 01:26:21 +00:00
|
|
|
render_resource_hints
|
|
|
|
.push(quote! {Some(#bevy_render_path::renderer::RenderResourceHints::BUFFER)})
|
2020-06-08 02:12:41 +00:00
|
|
|
} else {
|
|
|
|
render_resource_hints.push(quote! {None})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let render_resource_count = render_resource_names.len();
|
|
|
|
let render_resource_indices = 0..render_resource_count;
|
|
|
|
|
|
|
|
let struct_name_uppercase = struct_name_string.to_uppercase();
|
|
|
|
let render_resource_names_ident =
|
|
|
|
format_ident!("{}_RENDER_RESOURCE_NAMES", struct_name_uppercase);
|
|
|
|
let render_resource_hints_ident =
|
|
|
|
format_ident!("{}_RENDER_RESOURCE_HINTS", struct_name_uppercase);
|
|
|
|
|
|
|
|
TokenStream::from(quote! {
|
|
|
|
static #render_resource_names_ident: &[&str] = &[
|
|
|
|
#(#render_resource_names,)*
|
|
|
|
];
|
|
|
|
|
2020-07-17 01:26:21 +00:00
|
|
|
static #render_resource_hints_ident: &[Option<#bevy_render_path::renderer::RenderResourceHints>] = &[
|
2020-06-08 02:12:41 +00:00
|
|
|
#(#render_resource_hints,)*
|
|
|
|
];
|
|
|
|
|
2020-07-17 01:26:21 +00:00
|
|
|
impl #bevy_render_path::renderer::RenderResources for #struct_name {
|
2020-06-08 02:12:41 +00:00
|
|
|
fn render_resources_len(&self) -> usize {
|
|
|
|
#render_resource_count
|
|
|
|
}
|
|
|
|
|
2020-07-17 01:26:21 +00:00
|
|
|
fn get_render_resource(&self, index: usize) -> Option<&dyn #bevy_render_path::renderer::RenderResource> {
|
2020-06-08 02:12:41 +00:00
|
|
|
match index {
|
|
|
|
#(#render_resource_indices => Some(&self.#render_resource_fields),)*
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_render_resource_name(&self, index: usize) -> Option<&str> {
|
2020-11-30 21:33:52 +00:00
|
|
|
#render_resource_names_ident.get(index).copied()
|
2020-06-08 02:12:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 01:26:21 +00:00
|
|
|
fn get_render_resource_hints(&self, index: usize) -> Option<#bevy_render_path::renderer::RenderResourceHints> {
|
2020-11-30 21:33:52 +00:00
|
|
|
#render_resource_hints_ident.get(index).and_then(|o| *o)
|
2020-06-08 02:12:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 06:03:55 +00:00
|
|
|
fn iter(&self) -> #bevy_render_path::renderer::RenderResourceIterator {
|
2020-07-17 01:26:21 +00:00
|
|
|
#bevy_render_path::renderer::RenderResourceIterator::new(self)
|
2020-06-08 02:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|