@@ -3,49 +3,60 @@ use crate::shader_nodes::per_pixel_adjust::PerPixelAdjust;
33use proc_macro2:: { Ident , TokenStream } ;
44use quote:: quote;
55use strum:: VariantNames ;
6- use syn:: Error ;
76use syn:: parse:: { Parse , ParseStream } ;
7+ use syn:: punctuated:: Punctuated ;
8+ use syn:: { Error , Token } ;
89
910pub mod per_pixel_adjust;
1011
1112pub const STD_FEATURE_GATE : & str = "std" ;
13+ pub const SHADER_NODES_FEATURE_GATE : & str = "shader-nodes" ;
1214
1315pub fn modify_cfg ( attributes : & NodeFnAttributes ) -> TokenStream {
14- match ( & attributes. cfg , & attributes. shader_node ) {
15- ( Some ( cfg) , Some ( _) ) => quote ! ( #[ cfg( all( #cfg, feature = #STD_FEATURE_GATE ) ) ] ) ,
16- ( Some ( cfg) , None ) => quote ! ( #[ cfg( #cfg) ] ) ,
17- ( None , Some ( _) ) => quote ! ( #[ cfg( feature = #STD_FEATURE_GATE ) ] ) ,
18- ( None , None ) => quote ! ( ) ,
19- }
16+ let feature_gate = match & attributes. shader_node {
17+ // shader node cfg is done on the mod
18+ Some ( ShaderNodeType :: ShaderNode ) => quote ! ( ) ,
19+ Some ( _) => quote ! ( feature = #STD_FEATURE_GATE ) ,
20+ None => quote ! ( ) ,
21+ } ;
22+ let cfgs: Punctuated < _ , Token ! [ , ] > = match & attributes. cfg {
23+ None => [ & feature_gate] . into_iter ( ) . collect ( ) ,
24+ Some ( cfg) => [ cfg, & feature_gate] . into_iter ( ) . collect ( ) ,
25+ } ;
26+ quote ! ( #[ cfg( all( #cfgs) ) ] )
2027}
2128
2229#[ derive( Debug , Clone , VariantNames ) ]
2330pub ( crate ) enum ShaderNodeType {
31+ /// Marker for this node being in a gpu node crate, but not having a gpu implementation. This is distinct from not
32+ /// declaring `shader_node` at all, as it will wrap the CPU node with a `#[cfg(feature = "std")]` feature gate.
33+ None ,
2434 /// Marker for this node being a generated gpu node implementation, that should not emit anything to prevent
2535 /// recursively generating more gpu nodes. But it still counts as a gpu node and will get the
2636 /// `#[cfg(feature = "std")]` feature gate around it's impl.
27- GpuNode ,
37+ ShaderNode ,
2838 PerPixelAdjust ( PerPixelAdjust ) ,
2939}
3040
3141impl Parse for ShaderNodeType {
3242 fn parse ( input : ParseStream ) -> syn:: Result < Self > {
3343 let ident: Ident = input. parse ( ) ?;
3444 Ok ( match ident. to_string ( ) . as_str ( ) {
45+ "None" => ShaderNodeType :: None ,
3546 "PerPixelAdjust" => ShaderNodeType :: PerPixelAdjust ( PerPixelAdjust :: parse ( input) ?) ,
3647 _ => return Err ( Error :: new_spanned ( & ident, format ! ( "attr 'shader_node' must be one of {:?}" , Self :: VARIANTS ) ) ) ,
3748 } )
3849 }
3950}
4051
4152pub trait ShaderCodegen {
42- fn codegen ( & self , parsed : & ParsedNodeFn , node_cfg : & TokenStream ) -> syn:: Result < ShaderTokens > ;
53+ fn codegen ( & self , parsed : & ParsedNodeFn ) -> syn:: Result < ShaderTokens > ;
4354}
4455
4556impl ShaderCodegen for ShaderNodeType {
46- fn codegen ( & self , parsed : & ParsedNodeFn , node_cfg : & TokenStream ) -> syn:: Result < ShaderTokens > {
57+ fn codegen ( & self , parsed : & ParsedNodeFn ) -> syn:: Result < ShaderTokens > {
4758 match self {
48- ShaderNodeType :: GpuNode => ( ) ,
59+ ShaderNodeType :: None | ShaderNodeType :: ShaderNode => ( ) ,
4960 _ => {
5061 if parsed. is_async {
5162 return Err ( Error :: new_spanned ( & parsed. fn_name , "Shader nodes must not be async" ) ) ;
@@ -54,8 +65,8 @@ impl ShaderCodegen for ShaderNodeType {
5465 }
5566
5667 match self {
57- ShaderNodeType :: GpuNode => Ok ( ShaderTokens :: default ( ) ) ,
58- ShaderNodeType :: PerPixelAdjust ( x) => x. codegen ( parsed, node_cfg ) ,
68+ ShaderNodeType :: None | ShaderNodeType :: ShaderNode => Ok ( ShaderTokens :: default ( ) ) ,
69+ ShaderNodeType :: PerPixelAdjust ( x) => x. codegen ( parsed) ,
5970 }
6071 }
6172}
0 commit comments