Skip to content

Commit 3dd10e0

Browse files
committed
node_macro: add shader_nodes option
1 parent a1a0b76 commit 3dd10e0

7 files changed

Lines changed: 49 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ tinyvec = { version = "1", features = ["std"] }
154154
criterion = { version = "0.5", features = ["html_reports"] }
155155
iai-callgrind = { version = "0.12.3" }
156156
ndarray = "0.16.1"
157+
strum = { version = "0.26.3", features = ["derive"] }
157158

158159
[profile.dev]
159160
opt-level = 1

node-graph/node-macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ syn = { workspace = true }
1919
proc-macro2 = { workspace = true }
2020
quote = { workspace = true }
2121
convert_case = { workspace = true }
22+
strum = { workspace = true }
2223

2324
indoc = "2.0.5"
2425
proc-macro-crate = "3.1.0"

node-graph/node-macro/src/codegen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
345345

346346
let properties = &attributes.properties_string.as_ref().map(|value| quote!(Some(#value))).unwrap_or(quote!(None));
347347

348-
let cfg = if let Some(cfg) = &attributes.cfg { quote!(#[cfg(#cfg)]) } else { quote!() };
348+
let cfg = crate::shader_nodes::modify_cfg(&attributes);
349349
let node_input_accessor = generate_node_input_references(parsed, fn_generics, &field_idents, &graphene_core, &identifier, &cfg);
350350
Ok(quote! {
351351
/// Underlying implementation for [#struct_name]

node-graph/node-macro/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use syn::GenericParam;
55
mod codegen;
66
mod derive_choice_type;
77
mod parsing;
8+
mod shader_nodes;
89
mod validation;
910

1011
/// Used to create a node definition.

node-graph/node-macro/src/parsing.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use syn::{
1212
};
1313

1414
use crate::codegen::generate_node_code;
15+
use crate::shader_nodes::ShaderNodeType;
1516

1617
#[derive(Debug)]
1718
pub(crate) struct Implementation {
@@ -47,6 +48,8 @@ pub(crate) struct NodeFnAttributes {
4748
pub(crate) properties_string: Option<LitStr>,
4849
/// whether to `#[cfg]` gate the node implementation, defaults to None
4950
pub(crate) cfg: Option<TokenStream2>,
51+
/// if this node should get a gpu implementation, defaults to None
52+
pub(crate) shader_node: Option<ShaderNodeType>,
5053
// Add more attributes as needed
5154
}
5255

@@ -187,6 +190,7 @@ impl Parse for NodeFnAttributes {
187190
let mut skip_impl = false;
188191
let mut properties_string = None;
189192
let mut cfg = None;
193+
let mut shader_node = None;
190194

191195
let content = input;
192196
// let content;
@@ -249,6 +253,13 @@ impl Parse for NodeFnAttributes {
249253
let meta = meta.require_list()?;
250254
cfg = Some(meta.tokens.clone());
251255
}
256+
"shader_node" => {
257+
if shader_node.is_some() {
258+
return Err(Error::new_spanned(path, "Multiple 'feature' attributes are not allowed"));
259+
}
260+
let meta = meta.require_list()?;
261+
shader_node = Some(syn::parse2(meta.tokens.to_token_stream())?);
262+
}
252263
_ => {
253264
return Err(Error::new_spanned(
254265
meta,
@@ -273,6 +284,7 @@ impl Parse for NodeFnAttributes {
273284
skip_impl,
274285
properties_string,
275286
cfg,
287+
shader_node,
276288
})
277289
}
278290
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::parsing::NodeFnAttributes;
2+
use proc_macro2::{Ident, TokenStream};
3+
use quote::quote;
4+
use strum::{EnumString, VariantNames};
5+
use syn::Error;
6+
use syn::parse::{Parse, ParseStream};
7+
8+
pub const STD_FEATURE_GATE: &str = "std";
9+
10+
pub fn modify_cfg(attributes: &NodeFnAttributes) -> TokenStream {
11+
match (&attributes.cfg, &attributes.shader_node) {
12+
(Some(cfg), Some(_)) => quote!(#[cfg(all(#cfg, feature = #STD_FEATURE_GATE))]),
13+
(Some(cfg), None) => quote!(#[cfg(#cfg)]),
14+
(None, Some(_)) => quote!(#[cfg(feature = #STD_FEATURE_GATE)]),
15+
(None, None) => quote!(),
16+
}
17+
}
18+
19+
#[derive(Debug, EnumString, VariantNames)]
20+
pub(crate) enum ShaderNodeType {
21+
PerPixelAdjust,
22+
}
23+
24+
impl Parse for ShaderNodeType {
25+
fn parse(input: ParseStream) -> syn::Result<Self> {
26+
let ident: Ident = input.parse()?;
27+
Ok(match ident.to_string().as_str() {
28+
"PerPixelAdjust" => ShaderNodeType::PerPixelAdjust,
29+
_ => return Err(Error::new_spanned(&ident, format!("attr 'shader_node' must be one of {:?}", Self::VARIANTS))),
30+
})
31+
}
32+
}

0 commit comments

Comments
 (0)