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
// 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 syn::Ident;
use proc_macro2::{Span, TokenStream};

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

pub fn write_entry_point(doc: &Spirv, instruction: &Instruction) -> (TokenStream, TokenStream) {
    let (execution, id, ep_name, interface) = match instruction {
        &Instruction::EntryPoint { ref execution, id, ref name, ref interface, .. } =>
            (execution, id, name, interface),
        _ => unreachable!(),
    };

    let capitalized_ep_name: String = ep_name
        .chars()
        .take(1)
        .flat_map(|c| c.to_uppercase())
        .chain(ep_name.chars().skip(1))
        .collect();

    let ignore_first_array_in = match *execution {
        ExecutionModel::ExecutionModelTessellationControl => true,
        ExecutionModel::ExecutionModelTessellationEvaluation => true,
        ExecutionModel::ExecutionModelGeometry => true,
        _ => false,
    };
    let ignore_first_array_out = match *execution {
        ExecutionModel::ExecutionModelTessellationControl => true,
        _ => false,
    };

    let interface_structs = write_interface_structs(
        doc,
        &capitalized_ep_name,
        interface,
        ignore_first_array_in,
        ignore_first_array_out
    );

    let spec_consts_struct = if crate::spec_consts::has_specialization_constants(doc) {
        quote!{ SpecializationConstants }
    } else {
        quote!{ () }
    };

    let (ty, f_call) = {
        if let ExecutionModel::ExecutionModelGLCompute = *execution {
            (
                quote!{ ::vulkano::pipeline::shader::ComputeEntryPoint<#spec_consts_struct, Layout> },
                quote!{ compute_entry_point(
                    ::std::ffi::CStr::from_ptr(NAME.as_ptr() as *const _),
                    Layout(ShaderStages { compute: true, .. ShaderStages::none() })
                )}
            )
        } else {
            let entry_ty = match *execution {
                ExecutionModel::ExecutionModelVertex =>
                    quote!{ ::vulkano::pipeline::shader::GraphicsShaderType::Vertex },

                ExecutionModel::ExecutionModelTessellationControl =>
                    quote!{ ::vulkano::pipeline::shader::GraphicsShaderType::TessellationControl },

                ExecutionModel::ExecutionModelTessellationEvaluation =>
                    quote!{ ::vulkano::pipeline::shader::GraphicsShaderType::TessellationEvaluation },

                ExecutionModel::ExecutionModelGeometry => {
                    let mut execution_mode = None;

                    for instruction in doc.instructions.iter() {
                        if let &Instruction::ExecutionMode { target_id, ref mode, .. } = instruction {
                            if target_id == id {
                                execution_mode = match mode {
                                    &ExecutionMode::ExecutionModeInputPoints => Some(quote!{ Points }),
                                    &ExecutionMode::ExecutionModeInputLines => Some(quote!{ Lines }),
                                    &ExecutionMode::ExecutionModeInputLinesAdjacency =>
                                        Some(quote!{ LinesWithAdjacency }),
                                    &ExecutionMode::ExecutionModeTriangles => Some(quote!{ Triangles }),
                                    &ExecutionMode::ExecutionModeInputTrianglesAdjacency =>
                                        Some(quote!{ TrianglesWithAdjacency }),
                                    _ => continue,
                                };
                                break;
                            }
                        }
                    }

                    quote!{
                        ::vulkano::pipeline::shader::GraphicsShaderType::Geometry(
                            ::vulkano::pipeline::shader::GeometryShaderExecutionMode::#execution_mode
                        )
                    }
                }

                ExecutionModel::ExecutionModelFragment =>
                    quote!{ ::vulkano::pipeline::shader::GraphicsShaderType::Fragment },

                ExecutionModel::ExecutionModelGLCompute => unreachable!(),

                ExecutionModel::ExecutionModelKernel => panic!("Kernels are not supported"),
            };

            let stage = match *execution {
                ExecutionModel::ExecutionModelVertex =>
                    quote!{ ShaderStages { vertex: true, .. ShaderStages::none() } },

                ExecutionModel::ExecutionModelTessellationControl =>
                    quote!{ ShaderStages { tessellation_control: true, .. ShaderStages::none() } },

                ExecutionModel::ExecutionModelTessellationEvaluation =>
                    quote!{ ShaderStages { tessellation_evaluation: true, .. ShaderStages::none() } },

                ExecutionModel::ExecutionModelGeometry =>
                    quote!{ ShaderStages { geometry: true, .. ShaderStages::none() } },

                ExecutionModel::ExecutionModelFragment =>
                    quote!{ ShaderStages { fragment: true, .. ShaderStages::none() } },

                ExecutionModel::ExecutionModelGLCompute => unreachable!(),
                ExecutionModel::ExecutionModelKernel => unreachable!(),
            };

            let mut capitalized_ep_name_input = capitalized_ep_name.clone();
            capitalized_ep_name_input.push_str("Input");
            let capitalized_ep_name_input = Ident::new(&capitalized_ep_name_input, Span::call_site());

            let mut capitalized_ep_name_output = capitalized_ep_name.clone();
            capitalized_ep_name_output.push_str("Output");
            let capitalized_ep_name_output = Ident::new(&capitalized_ep_name_output, Span::call_site());

            let ty = quote!{
                ::vulkano::pipeline::shader::GraphicsEntryPoint<
                    #spec_consts_struct,
                    #capitalized_ep_name_input,
                    #capitalized_ep_name_output,
                    Layout>
            };
            let f_call = quote!{
                graphics_entry_point(
                    ::std::ffi::CStr::from_ptr(NAME.as_ptr() as *const _),
                    #capitalized_ep_name_input,
                    #capitalized_ep_name_output,
                    Layout(#stage),
                    #entry_ty
                )
            };

            (ty, f_call)
        }
    };

    let mut method_name = ep_name.clone();
    method_name.push_str("_entry_point");
    let method_ident = Ident::new(&method_name, Span::call_site());

    let ep_name_lenp1 = ep_name.chars().count() + 1;
    let encoded_ep_name = ep_name.chars().map(|c| (c as u8)).collect::<Vec<_>>();

    let entry_point = quote!{
        /// Returns a logical struct describing the entry point named `{ep_name}`.
        #[inline]
        #[allow(unsafe_code)]
        pub fn #method_ident(&self) -> #ty {
            unsafe {
                #[allow(dead_code)]
                static NAME: [u8; #ep_name_lenp1] = [ #( #encoded_ep_name ),* , 0];
                self.shader.#f_call
            }
        }
    };

    (interface_structs, entry_point)
}

struct Element {
    location: u32,
    name: String,
    format: String,
    location_len: usize,
}

fn write_interface_structs(doc: &Spirv, capitalized_ep_name: &str, interface: &[u32],
                           ignore_first_array_in: bool, ignore_first_array_out: bool)
                           -> TokenStream {
    let mut input_elements = vec!();
    let mut output_elements = vec!();

    // Filling `input_elements` and `output_elements`.
    for interface in interface.iter() {
        for i in doc.instructions.iter() {
            match i {
                &Instruction::Variable {
                    result_type_id,
                    result_id,
                    ref storage_class,
                    ..
                } if &result_id == interface => {
                    if spirv_search::is_builtin(doc, result_id) {
                        continue;
                    }

                    let (to_write, ignore_first_array) = match storage_class {
                        &StorageClass::StorageClassInput =>
                            (&mut input_elements, ignore_first_array_in),
                        &StorageClass::StorageClassOutput =>
                            (&mut output_elements, ignore_first_array_out),
                        _ => continue,
                    };

                    let name = spirv_search::name_from_id(doc, result_id);
                    if name == "__unnamed" {
                        continue;
                    } // FIXME: hack

                    let location = match doc.get_decoration_params(result_id, Decoration::DecorationLocation) {
                        Some(l) => l[0],
                        None => panic!("Attribute `{}` (id {}) is missing a location", name, result_id),
                    };

                    let (format, location_len) = spirv_search::format_from_id(doc, result_type_id, ignore_first_array);
                    to_write.push(Element { location, name, format, location_len });
                },
                _ => (),
            }
        }
    }

    let input: TokenStream = write_interface_struct(&format!("{}Input", capitalized_ep_name), &input_elements);
    let output: TokenStream = write_interface_struct(&format!("{}Output", capitalized_ep_name), &output_elements);
    quote!{ #input #output }
}

fn write_interface_struct(struct_name_str: &str, attributes: &[Element]) -> TokenStream {
    // Checking for overlapping elements.
    for (offset, element1) in attributes.iter().enumerate() {
        for element2 in attributes.iter().skip(offset + 1) {
            if element1.location == element2.location ||
                (element1.location < element2.location && element1.location + element1.location_len as u32 > element2.location) ||
                (element2.location < element1.location && element2.location + element2.location_len as u32 > element1.location)
            {
                panic!("The locations of attributes `{}` (start={}, size={}) \
                    and `{}` (start={}, size={}) overlap",
                    element1.name,
                    element1.location,
                    element1.location_len,
                    element2.name,
                    element2.location,
                    element2.location_len);
            }
        }
    }

    let body = attributes
        .iter()
        .enumerate()
        .map(|(num, element)| {
            assert!(element.location_len >= 1);
            let loc = element.location;
            let loc_end = element.location + element.location_len as u32;
            let format = Ident::new(&element.format, Span::call_site());
            let name = &element.name;
            let num = num as u16;

            quote!{
                if self.num == #num {
                    self.num += 1;

                    return Some(::vulkano::pipeline::shader::ShaderInterfaceDefEntry {
                        location: #loc .. #loc_end,
                        format: ::vulkano::format::Format::#format,
                        name: Some(::std::borrow::Cow::Borrowed(#name))
                    });
                }
            }
        })
        .collect::<Vec<_>>();

    let struct_name = Ident::new(struct_name_str, Span::call_site());

    let mut iter_name = struct_name.to_string();
    iter_name.push_str("Iter");
    let iter_name = Ident::new(&iter_name, Span::call_site());

    let len = attributes.len();

    quote!{
        #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
        pub struct #struct_name;

        #[allow(unsafe_code)]
        unsafe impl ::vulkano::pipeline::shader::ShaderInterfaceDef for #struct_name {
            type Iter = #iter_name;
            fn elements(&self) -> #iter_name {
                 #iter_name { num: 0 }
            }
        }

        #[derive(Debug, Copy, Clone)]
        pub struct #iter_name { num: u16 }

        impl Iterator for #iter_name {
            type Item = ::vulkano::pipeline::shader::ShaderInterfaceDefEntry;

            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                #( #body )*
                None
            }

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                let len = #len - self.num as usize;
                (len, Some(len))
            }
         }

        impl ExactSizeIterator for #iter_name {}
    }
}