aboutsummaryrefslogtreecommitdiffhomepage
path: root/optix_base/src/lib.rs
blob: 9c43664f18209dd3322e6a730295de6428361e69 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span};
use quote::{format_ident, quote, ToTokens};
use rustc_hash::{FxHashMap, FxHashSet};
use syn::spanned::Spanned;
use syn::Fields;
use syn::{
    bracketed,
    parse::{Parse, ParseStream},
    parse_macro_input,
    punctuated::Punctuated,
    visit::Visit,
    File, ImplItem, Item, Path, Token, Type, TypePath,
};

const OPTIX_RS: &'static str = include_str! {"optix.rs"};
const OPTIX6_RS: &'static str = include_str! {"optix6.rs"};

// This macro copies optix.rs as-is with some changes:
// * All function declarations are filtered out
// * All CUDA types are skipped
#[proc_macro]
pub fn optix_type_declarations(_: TokenStream) -> TokenStream {
    let mut optix_module = syn::parse_str::<File>(OPTIX_RS).unwrap();
    optix_module.items = optix_module
        .items
        .into_iter()
        .filter_map(|item| match item {
            Item::ForeignMod(_) => None,
            Item::Type(type_) => {
                if type_.ident.to_string().starts_with("CU") {
                    None
                } else {
                    Some(Item::Type(type_))
                }
            }
            Item::Struct(struct_) => {
                if struct_.ident.to_string().starts_with("CU") {
                    None
                } else {
                    Some(Item::Struct(struct_))
                }
            }
            i => Some(i),
        })
        .collect::<Vec<_>>();
    optix_module.into_token_stream().into()
}

// This macro accepts another macro which accepts a semicolon separated
// list of OptiX functions
//   macro_foo!(
//      "C" fn optixDeviceContextCreate(
//          fromContext: CUcontext,
//          options: *const OptixDeviceContextOptions,
//          context: *mut OptixDeviceContext,
//      ) -> OptixResult;
//      "C" fn optixGetErrorName(result: OptixResult) -> *const ::std::os::raw::c_char
//   )
#[proc_macro]
pub fn optix_function_declarations(tokens: TokenStream) -> TokenStream {
    let macro_ = parse_macro_input!(tokens as Path);
    let optix_module = syn::parse_str::<File>(OPTIX_RS).unwrap();
    let mut all_fns = FxHashMap::default();
    let mut ordered_exports = Vec::new();
    for item in optix_module.items.iter() {
        match item {
            Item::ForeignMod(extern_) => {
                for item in extern_.items.iter() {
                    if let syn::ForeignItem::Fn(fn_) = item {
                        all_fns.insert(&fn_.sig.ident, &fn_.sig);
                    }
                }
            }
            Item::Struct(struct_) if struct_.ident == "OptixFunctionTable" => {
                for fn_ in struct_.fields.iter() {
                    ordered_exports.push(fn_.ident.as_ref().unwrap());
                }
            }
            _ => {}
        }
    }
    let fns_ = ordered_exports
        .into_iter()
        .fold(Vec::new(), |mut vec, export| {
            if export.to_string().starts_with("reserved") {
                vec.push(quote! {
                    "C" fn #export() -> ()
                });
            } else {
                let fn_signature = all_fns[export];
                let name = &fn_signature.ident;
                let inputs = &fn_signature.inputs;
                let output = &fn_signature.output;
                vec.push(quote! {
                    "C" fn #name(#inputs) #output
                });
            }
            vec
        });
    quote! {
        #macro_ ! ( #(#fns_);* );
    }
    .into()
}

// This macro copies optix6.rs as-is with some changes:
// * All function declarations are filtered out
// * For selected enums we generate a safe version of the enum type and a conversion function
#[proc_macro]
pub fn optix6_type_declarations(tokens: TokenStream) -> TokenStream {
    let type_decl = parse_macro_input!(tokens as TypeDeclInput);
    let mut optix_module = syn::parse_str::<File>(OPTIX6_RS).unwrap();
    optix_module.items = optix_module
        .items
        .into_iter()
        .filter_map(|item| match item {
            Item::ForeignMod(_) => None,
            i => Some(i),
        })
        .collect::<Vec<_>>();

    let mut safe_enum_gen = GenerateSafeEnums::new(type_decl.enums.iter());
    syn::visit::visit_file(&mut safe_enum_gen, &optix_module);
    let safe_enums = safe_enum_gen.generate();
    let mut token_stream = optix_module.into_token_stream();
    token_stream.extend(safe_enums);
    token_stream.into()
}

struct GenerateSafeEnums<'ast> {
    enums: FxHashMap<&'ast Ident, (Option<&'ast Type>, Vec<&'ast Ident>)>,
}

impl<'ast> GenerateSafeEnums<'ast> {
    fn new(enums: impl Iterator<Item = &'ast Ident>) -> Self {
        let enums = enums.map(|e| (e, (None, Vec::new()))).collect();
        Self { enums }
    }

    fn generate(self) -> TokenStream2 {
        let mut token_stream = TokenStream2::new();
        for (enum_, (underlying_type, items)) in self.enums {
            let safe_enum = format_ident!("{}Safe", enum_);
            let underlying_type = Self::to_ident(underlying_type.unwrap());
            token_stream.extend(quote! {
                #[repr(#underlying_type)]
                #[derive(Copy, Clone, PartialEq, Eq)]
                pub enum #safe_enum {
                    #(
                        #items = #enum_ :: #items .0,
                    )*
                }

                impl #safe_enum {
                    pub fn new(e: #enum_) -> Option<Self> {
                        match e {
                            #(
                                #enum_ :: #items => Some(#safe_enum :: #items),
                            )*
                            _ => None
                        }
                    }
                }

                impl From<#safe_enum> for #enum_ {
                    fn from(value: #safe_enum) -> #enum_ {
                        match value {
                            #(
                                #safe_enum :: #items => #enum_ :: #items,
                            )*
                        }
                    }
                }
            });
        }
        token_stream
    }

    fn to_ident(ty: &Type) -> Ident {
        if let Type::Path(ty_path) = ty {
            match &*ty_path.path.segments.last().unwrap().ident.to_string() {
                "c_uint" => Ident::new("u32", ty_path.span()),
                "c_int" => Ident::new("i32", ty_path.span()),
                _ => unimplemented!(),
            }
        } else {
            panic!()
        }
    }
}

impl<'ast> Visit<'ast> for GenerateSafeEnums<'ast> {
    fn visit_item_impl(&mut self, item: &'ast syn::ItemImpl) {
        if let Type::Path(TypePath { path, .. }) = &*item.self_ty {
            if let Some((_, entry)) = self.enums.get_mut(&path.segments[0].ident) {
                for item in item.items.iter() {
                    if let ImplItem::Const(const_) = item {
                        entry.push(&const_.ident)
                    }
                }
            }
        }
    }
    fn visit_item_struct(&mut self, item: &'ast syn::ItemStruct) {
        if let Some((underlying, _)) = self.enums.get_mut(&item.ident) {
            if let Fields::Unnamed(fields) = &item.fields {
                *underlying = Some(&fields.unnamed[0].ty);
            }
        }
    }
}

struct TypeDeclInput {
    enums: Punctuated<Ident, Token![,]>,
}

impl Parse for TypeDeclInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let enums = Punctuated::<Ident, Token![,]>::parse_terminated(input)?;
        Ok(TypeDeclInput { enums })
    }
}

// This macro accepts following arguments:
// * `normal_macro`: ident for a normal macro
// * `override_macro`: ident for an override macro
// * `override_fns`: list of override functions
// Then macro goes through every function in optix6.rs, and for every fn `foo`:
// * if `foo` is contained in `override_fns` then pass it into `override_macro`
// * if `foo` is not contained in `override_fns` pass it to `normal_macro`
// Both macros expected a separated list of OptiX6 functions:
//   macro_foo!(
//      "C" fn rtContextCreate(context: *mut RTcontext) -> RTresult;
//      "C" fn rtContextDestroy(context: RTcontext) -> RTresult
//   )
#[proc_macro]
pub fn optix6_function_declarations(tokens: TokenStream) -> TokenStream {
    let function_decl = parse_macro_input!(tokens as FnDeclInput);
    let override_idents = function_decl
        .override_fns
        .into_iter()
        .collect::<FxHashSet<_>>();
    let optix_module = syn::parse_str::<File>(OPTIX6_RS).unwrap();
    let mut fns = Vec::new();
    let mut override_fns = Vec::new();
    for item in optix_module.items.iter() {
        match item {
            Item::ForeignMod(extern_) => {
                for item in extern_.items.iter() {
                    if let syn::ForeignItem::Fn(fn_) = item {
                        let abi = &extern_.abi.name;
                        let name = &fn_.sig.ident;
                        let inputs = &fn_.sig.inputs;
                        let mut output = &fn_.sig.output;
                        let unit_return = syn::ReturnType::Type(
                            Token![->](Span::call_site()),
                            Box::new(syn::Type::Tuple(syn::TypeTuple {
                                paren_token: syn::token::Paren {
                                    span: Span::call_site(),
                                },
                                elems: Punctuated::new(),
                            })),
                        );
                        if let syn::ReturnType::Default = output {
                            output = &unit_return;
                        }
                        let fn_call = quote! {
                            #abi fn #name(#inputs) #output
                        };
                        let insertion_target = if override_idents.contains(name) {
                            &mut override_fns
                        } else {
                            &mut fns
                        };
                        insertion_target.push(fn_call);
                    }
                }
            }
            _ => {}
        }
    }
    let macro1 = function_decl.normal_macro;
    let macro2 = function_decl.override_macro;
    quote! {
        #macro1 ! ( #(#fns);* );
        #macro2 ! ( #(#override_fns);* );
    }
    .into()
}

struct FnDeclInput {
    normal_macro: Path,
    override_macro: Path,
    override_fns: Punctuated<Ident, Token![,]>,
}

impl Parse for FnDeclInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let normal_macro = input.parse::<Path>()?;
        input.parse::<Token![,]>()?;
        let override_macro = input.parse::<Path>()?;
        input.parse::<Token![,]>()?;
        let override_fns_content;
        bracketed!(override_fns_content in input);
        let override_fns = override_fns_content.parse_terminated(Ident::parse)?;
        Ok(Self {
            normal_macro,
            override_macro,
            override_fns,
        })
    }
}