• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2021 The Vulkano developers
2 // Licensed under the Apache License, Version 2.0
3 // <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
6 // at your option. All files in the project carrying such
7 // notice may not be copied, modified, or distributed except
8 // according to those terms.
9 
10 use heck::{CamelCase, SnakeCase};
11 use indexmap::IndexMap;
12 use std::io::Write;
13 use vk_parse::{Extension, ExtensionChild, InterfaceItem};
14 
write<W: Write>(writer: &mut W, extensions: &IndexMap<&str, &Extension>)15 pub fn write<W: Write>(writer: &mut W, extensions: &IndexMap<&str, &Extension>) {
16     write_fns(writer, std::iter::empty(), "Entry");
17     write!(writer, "\n\n").unwrap();
18     write_fns(
19         writer,
20         make_vulkano_extension_fns("instance", &extensions),
21         "Instance",
22     );
23     write!(writer, "\n\n").unwrap();
24     write_fns(
25         writer,
26         make_vulkano_extension_fns("device", &extensions),
27         "Device",
28     );
29 }
30 
31 #[derive(Clone, Debug)]
32 struct VulkanoFns {
33     member: String,
34     fn_struct: String,
35 }
36 
make_vulkano_extension_fns( ty: &str, extensions: &IndexMap<&str, &Extension>, ) -> Vec<VulkanoFns>37 fn make_vulkano_extension_fns(
38     ty: &str,
39     extensions: &IndexMap<&str, &Extension>,
40 ) -> Vec<VulkanoFns> {
41     extensions
42         .values()
43         .filter(|ext| ext.ext_type.as_ref().unwrap() == ty)
44         // Filter only extensions that have functions
45         .filter(|ext| {
46             ext.children.iter().any(|ch| {
47                 if let ExtensionChild::Require { items, .. } = ch {
48                     items
49                         .iter()
50                         .any(|i| matches!(i, InterfaceItem::Command { .. }))
51                 } else {
52                     false
53                 }
54             })
55         })
56         .map(|ext| {
57             let member = ext.name.strip_prefix("VK_").unwrap().to_snake_case();
58             let fn_struct = member.to_camel_case() + "Fn";
59             VulkanoFns { member, fn_struct }
60         })
61         .collect()
62 }
63 
write_fns<W, I>(writer: &mut W, extension_fns: I, ty: &str) where W: Write, I: IntoIterator<Item = VulkanoFns>,64 fn write_fns<W, I>(writer: &mut W, extension_fns: I, ty: &str)
65 where
66     W: Write,
67     I: IntoIterator<Item = VulkanoFns>,
68 {
69     write!(writer, "crate::fns::fns!({}Functions, {{", ty).unwrap();
70 
71     for version in std::array::IntoIter::new(["1_0", "1_1", "1_2"]) {
72         write!(writer, "\n\tv{} => {}FnV{0},", version, ty).unwrap();
73     }
74 
75     for ext in extension_fns {
76         write!(writer, "\n\t{} => {},", ext.member, ext.fn_struct).unwrap();
77     }
78     write!(writer, "\n}});").unwrap();
79 }
80