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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

use std::mem;

use syn::Ident;
use proc_macro2::{Span, TokenStream};

use crate::parse::{Instruction, Spirv};
use crate::enums::Decoration;
use crate::spirv_search;

/// Translates all the structs that are contained in the SPIR-V document as Rust structs.
pub fn write_structs(doc: &Spirv) -> TokenStream {
    let mut structs = vec!();
    for instruction in &doc.instructions {
        match *instruction {
            Instruction::TypeStruct { result_id, ref member_types } =>
                structs.push(write_struct(doc, result_id, member_types).0),
            _ => ()
        }
    }

    quote!{
        #( #structs )*
    }
}

/// Analyzes a single struct, returns a string containing its Rust definition, plus its size.
fn write_struct(doc: &Spirv, struct_id: u32, members: &[u32]) -> (TokenStream, Option<usize>) {
    let name = Ident::new(&spirv_search::name_from_id(doc, struct_id), Span::call_site());

    // The members of this struct.
    struct Member {
        pub name: Ident,
        pub ty: TokenStream,
    }
    let mut rust_members = Vec::with_capacity(members.len());

    // Padding structs will be named `_paddingN` where `N` is determined by this variable.
    let mut next_padding_num = 0;

    // Contains the offset of the next field.
    // Equals to `None` if there's a runtime-sized field in there.
    let mut current_rust_offset = Some(0);

    for (num, &member) in members.iter().enumerate() {
        // Compute infos about the member.
        let (ty, rust_size, rust_align) = type_from_id(doc, member);
        let member_name = spirv_search::member_name_from_id(doc, struct_id, num as u32);

        // Ignore the whole struct is a member is built in, which includes
        // `gl_Position` for example.
        if doc.get_member_decoration_params(struct_id, num as u32, Decoration::DecorationBuiltIn).is_some() {
            return (quote!{}, None); // TODO: is this correct? shouldn't it return a correct struct but with a flag or something?
        }

        // Finding offset of the current member, as requested by the SPIR-V code.
        let spirv_offset = doc.get_member_decoration_params(struct_id, num as u32, Decoration::DecorationOffset)
            .map(|x| x[0]);

        // Some structs don't have `Offset` decorations, in the case they are used as local
        // variables only. Ignoring these.
        let spirv_offset = match spirv_offset {
            Some(o) => o as usize,
            None => return (quote!{}, None), // TODO: shouldn't we return and let the caller ignore it instead?
        };

        // We need to add a dummy field if necessary.
        {
            let current_rust_offset =
                current_rust_offset
                    .as_mut()
                    .expect("Found runtime-sized member in non-final position");

            // Updating current_rust_offset to take the alignment of the next field into account
            *current_rust_offset = if *current_rust_offset == 0 {
                0
            } else {
                (1 + (*current_rust_offset - 1) / rust_align) * rust_align
            };

            if spirv_offset != *current_rust_offset {
                let diff = spirv_offset.checked_sub(*current_rust_offset).unwrap();
                let padding_num = next_padding_num;
                next_padding_num += 1;
                rust_members.push(Member {
                    name: Ident::new(&format!("_dummy{}", padding_num), Span::call_site()),
                    ty: quote!{ [u8; #diff] },
                });
                *current_rust_offset += diff;
            }
        }

        // Updating `current_rust_offset`.
        if let Some(s) = rust_size {
            *current_rust_offset.as_mut().unwrap() += s;
        } else {
            current_rust_offset = None;
        }

        rust_members.push(Member {
            name: Ident::new(&member_name, Span::call_site()),
            ty,
        });
    }

    // Try determine the total size of the struct in order to add padding at the end of the struct.
    let mut spirv_req_total_size = None;
    for inst in doc.instructions.iter() {
        match *inst {
            Instruction::TypeArray { result_id, type_id, .. } if type_id == struct_id => {
                if let Some(params) = doc.get_decoration_params(result_id, Decoration::DecorationArrayStride) {
                    spirv_req_total_size = Some(params[0]);
                }
            }
            Instruction::TypeRuntimeArray { result_id, type_id } if type_id == struct_id => {
                if let Some(params) = doc.get_decoration_params(result_id, Decoration::DecorationArrayStride) {
                    spirv_req_total_size = Some(params[0]);
                }
            }
            _ => ()
        }
    }

    // Adding the final padding members.
    if let (Some(cur_size), Some(req_size)) = (current_rust_offset, spirv_req_total_size) {
        let diff = req_size.checked_sub(cur_size as u32).unwrap();
        if diff >= 1 {
            rust_members.push(Member {
                name: Ident::new(&format!("_dummy{}", next_padding_num), Span::call_site()),
                ty: quote!{ [u8; #diff as usize] },
            });
        }
    }

    // We can only implement Clone if there's no unsized member in the struct.
    let (clone_impl, copy_derive) = if current_rust_offset.is_some() {
        let mut copies = vec!();
        for member in &rust_members {
            let name = &member.name;
            copies.push(quote!{ #name: self.#name, });
        }
        (
            // Clone is implemented manually because members can be large arrays
            // that do not implement Clone, but do implement Copy
            quote!{
                impl Clone for #name {
                    fn clone(&self) -> Self {
                        #name {
                            #( #copies )*
                        }
                    }
                }
            },
            quote!{ #[derive(Copy)] }
        )
    } else {
        (quote!{}, quote!{})
    };

    let mut members = vec!();
    for member in &rust_members {
        let name = &member.name;
        let ty = &member.ty;
        members.push(quote!(pub #name: #ty,));
    }

    let ast = quote! {
        #[repr(C)]
        #copy_derive
        #[allow(non_snake_case)]
        pub struct #name {
            #( #members )*
        }
        #clone_impl
    };

    (ast, spirv_req_total_size.map(|sz| sz as usize).or(current_rust_offset))
}

/// Returns the type name to put in the Rust struct, and its size and alignment.
///
/// The size can be `None` if it's only known at runtime.
pub fn type_from_id(doc: &Spirv, searched: u32) -> (TokenStream, Option<usize>, usize) {
    for instruction in doc.instructions.iter() {
        match instruction {
            &Instruction::TypeBool { result_id } if result_id == searched => {
                panic!("Can't put booleans in structs")
            }
            &Instruction::TypeInt { result_id, width, signedness } if result_id == searched => {
                match (width, signedness) {
                    (8, true) => {
                        #[repr(C)]
                        struct Foo {
                            data: i8,
                            after: u8,
                        }
                        return (quote!{i8}, Some(std::mem::size_of::<i8>()), mem::align_of::<Foo>());
                    },
                    (8, false) => {
                        #[repr(C)]
                        struct Foo {
                            data: u8,
                            after: u8,
                        }
                        return (quote!{u8}, Some(std::mem::size_of::<u8>()), mem::align_of::<Foo>());
                    },
                    (16, true) => {
                        #[repr(C)]
                        struct Foo {
                            data: i16,
                            after: u8,
                        }
                        return (quote!{i16}, Some(std::mem::size_of::<i16>()), mem::align_of::<Foo>());
                    },
                    (16, false) => {
                        #[repr(C)]
                        struct Foo {
                            data: u16,
                            after: u8,
                        }
                        return (quote!{u16}, Some(std::mem::size_of::<u16>()), mem::align_of::<Foo>());
                    },
                    (32, true) => {
                        #[repr(C)]
                        struct Foo {
                            data: i32,
                            after: u8,
                        }
                        return (quote!{i32}, Some(std::mem::size_of::<i32>()), mem::align_of::<Foo>());
                    },
                    (32, false) => {
                        #[repr(C)]
                        struct Foo {
                            data: u32,
                            after: u8,
                        }
                        return (quote!{u32}, Some(std::mem::size_of::<u32>()), mem::align_of::<Foo>());
                    },
                    (64, true) => {
                        #[repr(C)]
                        struct Foo {
                            data: i64,
                            after: u8,
                        }
                        return (quote!{i64}, Some(std::mem::size_of::<i64>()), mem::align_of::<Foo>());
                    },
                    (64, false) => {
                        #[repr(C)]
                        struct Foo {
                            data: u64,
                            after: u8,
                        }
                        return (quote!{u64}, Some(std::mem::size_of::<u64>()), mem::align_of::<Foo>());
                    },
                    _ => panic!("No Rust equivalent for an integer of width {}", width),
                }
            }
            &Instruction::TypeFloat { result_id, width } if result_id == searched => {
                match width {
                    32 => {
                        #[repr(C)]
                        struct Foo {
                            data: f32,
                            after: u8,
                        }
                        return (quote!{f32}, Some(std::mem::size_of::<f32>()), mem::align_of::<Foo>());
                    },
                    64 => {
                        #[repr(C)]
                        struct Foo {
                            data: f64,
                            after: u8,
                        }
                        return (quote!{f64}, Some(std::mem::size_of::<f64>()), mem::align_of::<Foo>());
                    },
                    _ => panic!("No Rust equivalent for a floating-point of width {}", width),
                }
            }
            &Instruction::TypeVector {
                result_id,
                component_id,
                count,
            } if result_id == searched => {
                debug_assert_eq!(mem::align_of::<[u32; 3]>(), mem::align_of::<u32>());
                let (ty, t_size, t_align) = type_from_id(doc, component_id);
                let array_length = count as usize;
                let size = t_size.map(|s| s * count as usize);
                return (quote!{ [#ty; #array_length] }, size, t_align);
            }
            &Instruction::TypeMatrix {
                result_id,
                column_type_id,
                column_count,
            } if result_id == searched => {
                // FIXME: row-major or column-major
                debug_assert_eq!(mem::align_of::<[u32; 3]>(), mem::align_of::<u32>());
                let (ty, t_size, t_align) = type_from_id(doc, column_type_id);
                let array_length = column_count as usize;
                let size = t_size.map(|s| s * column_count as usize);
                return (quote!{ [#ty; #array_length] }, size, t_align);
            }
            &Instruction::TypeArray {
                result_id,
                type_id,
                length_id,
            } if result_id == searched => {
                debug_assert_eq!(mem::align_of::<[u32; 3]>(), mem::align_of::<u32>());
                let (ty, t_size, t_align) = type_from_id(doc, type_id);
                let t_size = t_size.expect("array components must be sized");
                let len = doc.instructions
                    .iter()
                    .filter_map(|e| match e {
                        &Instruction::Constant { result_id, ref data, .. }
                            if result_id == length_id => Some(data.clone()),
                        _ => None,
                    })
                    .next()
                    .expect("failed to find array length");
                let len = len.iter().rev().fold(0u64, |a, &b| (a << 32) | b as u64);
                let stride = doc.get_decoration_params(searched, Decoration::DecorationArrayStride).unwrap()[0];
                if stride as usize > t_size {
                    panic!("Not possible to generate a rust array with the correct alignment since the SPIR-V \
                            ArrayStride is larger than the size of the array element in rust. Try wrapping \
                            the array element in a struct or rounding up the size of a vector or matrix \
                            (e.g. increase a vec3 to a vec4)")
                }
                let array_length = len as usize;
                let size = Some(t_size * len as usize);
                return (quote!{ [#ty; #array_length] }, size, t_align);
            }
            &Instruction::TypeRuntimeArray { result_id, type_id }
                if result_id == searched => {
                debug_assert_eq!(mem::align_of::<[u32; 3]>(), mem::align_of::<u32>());
                let (ty, _, t_align) = type_from_id(doc, type_id);
                return (quote!{ [#ty] }, None, t_align);
            }
            &Instruction::TypeStruct {
                result_id,
                ref member_types,
            } if result_id == searched => {
                // TODO: take the Offset member decorate into account?
                let name = Ident::new(&spirv_search::name_from_id(doc, result_id), Span::call_site());
                let ty = quote!{ #name };
                let (_, size) = write_struct(doc, result_id, member_types);
                let align = member_types
                    .iter()
                    .map(|&t| type_from_id(doc, t).2)
                    .max()
                    .unwrap_or(1);
                return (ty, size, align);
            },
            _ => (),
        }
    }

    panic!("Type #{} not found", searched)
}