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
#![recursion_limit="128"]

extern crate proc_macro;
extern crate proc_macro2;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro2::Span;

#[proc_macro_derive(AsStd140)]
pub fn as_std140(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = proc_macro2::TokenStream::from(input);

    let ast = syn::parse2(input).unwrap();

    proc_macro::TokenStream::from(impl_as_std140(&ast))
}

fn impl_as_std140(ast: &syn::DeriveInput) -> proc_macro2::TokenStream {
    let name = &ast.ident;

    let rname = format_ident!("LayoutStd140{}", name);
    
    let fields = match &ast.data {
        syn::Data::Struct(syn::DataStruct {
            fields: syn::Fields::Named(syn::FieldsNamed {
                named,
                ..
            }),
            ..
        }) => named,
        _ => panic!(),
    };

    let aligned_fields = fields.iter().flat_map(|field| {
        let (a, f) = aligned_field(field);
        vec![a, f]
    });

    let field_names = fields.iter().map(|field| field.ident.as_ref().unwrap());
    let field_names2 = fields.iter().map(|field| field.ident.as_ref().unwrap());

    let dummy = format_ident!("_GLSL_LAYOUT_{}", name);

    quote! {
        #[allow(bad_style)]
        const #dummy: () = {
            extern crate glsl_layout as _glsl_layout;

            #[repr(C, align(16))]
            #[derive(Clone, Copy, Debug, Default)]
            pub struct #rname {#(
                #aligned_fields,
            )*}

            unsafe impl _glsl_layout::Std140 for #rname {}

            unsafe impl _glsl_layout::AsStd140 for #rname {
                type Align = _glsl_layout::align::Align16;
                type Std140 = #rname;

                fn std140(&self) -> #rname {
                    self.clone()
                }
            }

            unsafe impl _glsl_layout::AsStd140 for #name {
                type Align = _glsl_layout::align::Align16;
                type Std140 = #rname;

                fn std140(&self) -> #rname {
                    #rname {
                        #(#field_names: self.#field_names2.std140(),)*
                        ..Default::default()
                    }
                }
            }
        };
    }
}

fn aligned_field(field: &syn::Field) -> (syn::Field, syn::Field) {
    let name = field.ident.as_ref().unwrap();
    let align = syn::Field {
        ty: syn::Type::Path(align_type_for(&field.ty)),
        ident: Some(format_ident!("_align_{}", name)),
        attrs: Vec::new(),
        vis: syn::Visibility::Inherited,
        colon_token: Some(Default::default()),
    };

    let std140 = syn::Field {
        ty: syn::Type::Path(std140_type_for(&field.ty)),
        ..field.clone()
    };

    (align, std140)
}

fn align_type_for(aligned: &syn::Type) -> syn::TypePath {
    use std::iter::once;
    syn::TypePath {
        qself: Some(syn::QSelf {
            lt_token: Default::default(),
            ty: Box::new(aligned.clone()),
            position: 2,
            as_token: Some(Default::default()),
            gt_token: Default::default(),
        }),
        path: syn::Path {
            leading_colon: None,
            segments: once(syn::PathSegment::from(syn::Ident::new("_glsl_layout", Span::call_site())))
                .chain(once(syn::Ident::new("AsStd140", Span::call_site()).into()))
                .chain(once(syn::Ident::new("Align", Span::call_site()).into()))
                .collect(),
        }
    }
}

fn std140_type_for(aligned: &syn::Type) -> syn::TypePath {
    use std::iter::once;
    syn::TypePath {
        qself: Some(syn::QSelf {
            lt_token: Default::default(),
            ty: Box::new(aligned.clone()),
            position: 2,
            as_token: Some(Default::default()),
            gt_token: Default::default(),
        }),
        path: syn::Path {
            leading_colon: None,
            segments: once(syn::PathSegment::from(syn::Ident::new("_glsl_layout", Span::call_site())))
                .chain(once(syn::Ident::new("AsStd140".into(), Span::call_site()).into()))
                .chain(once(syn::Ident::new("Std140".into(), Span::call_site()).into()))
                .collect(),
        }
    }
}