-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_view.rs
184 lines (165 loc) · 6.05 KB
/
get_view.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![allow(deprecated)]
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::parse::{Parse, ParseStream, Result};
use syn::{parse_macro_input, Token};
struct GetView {
dimensions: syn::LitInt,
}
impl Parse for GetView {
fn parse(input: ParseStream) -> Result<Self> {
Ok(GetView {
dimensions: input.parse()?,
})
}
}
fn create_path(n: u32, idx: u32, letters: &Vec<&str>) -> syn::Type {
let mut bound_path = syn::punctuated::Punctuated::new();
let mut multisample_path = syn::punctuated::Punctuated::new();
multisample_path.push(syn::PathSegment {
ident: format_ident!("MULTISAMPLE"),
arguments: syn::PathArguments::None,
});
let mut sampletype_path = syn::punctuated::Punctuated::new();
sampletype_path.push(syn::PathSegment {
ident: format_ident!("SAMPLETYPE"),
arguments: syn::PathArguments::None,
});
let mut viewdimension_path = syn::punctuated::Punctuated::new();
viewdimension_path.push(syn::PathSegment {
ident: format_ident!("VIEWDIMENSION"),
arguments: syn::PathArguments::None,
});
let mut bracked_type = syn::punctuated::Punctuated::new();
bracked_type.push(syn::GenericArgument::Lifetime(syn::Lifetime::new(
"'a",
proc_macro2::Span::call_site(),
)));
bracked_type.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: multisample_path,
},
})));
bracked_type.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: sampletype_path,
},
})));
bracked_type.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: viewdimension_path,
},
})));
let mut bind_group_generic_types = syn::punctuated::Punctuated::new();
for i in 0..n {
if i == idx {
let mut texture_type: syn::punctuated::Punctuated<
syn::PathSegment,
syn::token::Colon2,
> = syn::punctuated::Punctuated::new();
texture_type.push(syn::PathSegment {
ident: format_ident!("TextureData"),
arguments: syn::PathArguments::AngleBracketed(
syn::AngleBracketedGenericArguments {
args: bracked_type.clone(),
colon2_token: None,
lt_token: Token!(<)(proc_macro2::Span::call_site()),
gt_token: Token!(>)(proc_macro2::Span::call_site()),
},
),
});
bind_group_generic_types.push(syn::GenericArgument::Type(syn::Type::Path(
syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: texture_type,
},
},
)));
}
if i < letters.len() as u32 {
let mut letter_type = syn::punctuated::Punctuated::new();
letter_type.push(syn::PathSegment {
ident: format_ident!("{}", letters[i as usize]),
arguments: syn::PathArguments::None,
});
bind_group_generic_types.push(syn::GenericArgument::Type(syn::Type::Path(
syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: letter_type,
},
},
)));
}
}
bound_path.push(syn::PathSegment {
ident: format_ident!("BindGroup{}", n),
arguments: syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
args: bind_group_generic_types,
colon2_token: None,
lt_token: Token!(<)(proc_macro2::Span::call_site()),
gt_token: Token!(>)(proc_macro2::Span::call_site()),
}),
});
syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: bound_path,
},
})
}
pub fn sub_module_get_view_func(input: TokenStream) -> TokenStream {
let get_view_params = parse_macro_input!(input as GetView);
let idx = get_view_params.dimensions.base10_parse::<u32>().unwrap();
let mut all_expanded = Vec::new();
let other_letters: Vec<&str> = vec![
"A", "B", "C", "D", "E", "F", "G", "H", "I", "G", "K", "L", "M", "N", "O", "P",
]
.into_iter()
.take((idx - 1) as usize)
.collect();
for i in 0..idx {
//BindGroup3<TextureData<'a, MULTISAMPLE, SAMPLETYPE, VIEWDIMENSION>, C, D>
let struct_type = create_path(idx, i, &other_letters);
let function_name = format_ident!("get_view_{}", i);
let index = i as usize;
let generic_letters: Vec<syn::Ident> = other_letters
.iter()
.map(|x| format_ident!("{}", x))
.collect();
all_expanded.push(quote! {
impl<'a, const MULTISAMPLE: TextureMultisampled, const SAMPLETYPE: wgpu::TextureSampleType,
const VIEWDIMENSION: wgpu::TextureViewDimension, #(#generic_letters : WgpuType),*>
#struct_type
{
pub fn #function_name(&self, desc: &wgpu::TextureViewDescriptor) -> wgpu::TextureView {
match self.data.get(#index).unwrap() {
BoundData::Texture { data, .. } => data.create_view(desc),
_ => panic!("not a texture"),
}
}
}
});
}
let mut collapsed_expanded = quote! {};
for i in all_expanded.into_iter() {
collapsed_expanded = quote! {
#collapsed_expanded
#i
}
}
println!("{}", collapsed_expanded);
// Hand the output tokens back to the compiler
TokenStream::from(collapsed_expanded)
}