Add extensions

This commit is contained in:
Maik Klein 2018-07-21 12:56:16 +02:00
parent 3fc04b77a6
commit 1a3094d4c2
3 changed files with 138 additions and 43 deletions

View file

@ -382,7 +382,7 @@ impl ExampleBase {
let surface_format = surface_formats let surface_format = surface_formats
.iter() .iter()
.map(|sfmt| match sfmt.format { .map(|sfmt| match sfmt.format {
vk::Format::Undefined => vk::SurfaceFormatKHR { vk::Format:: => vk::SurfaceFormatKHR {
format: vk::Format::B8g8r8Unorm, format: vk::Format::B8g8r8Unorm,
color_space: sfmt.color_space, color_space: sfmt.color_space,
}, },
@ -450,7 +450,7 @@ impl ExampleBase {
let pool_create_info = vk::CommandPoolCreateInfo { let pool_create_info = vk::CommandPoolCreateInfo {
s_type: vk::StructureType::CommandPoolCreateInfo, s_type: vk::StructureType::CommandPoolCreateInfo,
p_next: ptr::null(), p_next: ptr::null(),
flags: vk::COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, flags: vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER_BIT,
queue_family_index: queue_family_index, queue_family_index: queue_family_index,
}; };
let pool = device.create_command_pool(&pool_create_info, None).unwrap(); let pool = device.create_command_pool(&pool_create_info, None).unwrap();

View file

@ -8,6 +8,5 @@ use std::path::Path;
fn main() { fn main() {
//let file = File::open("New-Vulkan-XML-Format/vk_new.xml").expect("vknew"); //let file = File::open("New-Vulkan-XML-Format/vk_new.xml").expect("vknew");
let spec = vk_parse::parse_file_as_vkxml(Path::new("vk.xml")); write_source_code(Path::new("vk.xml"));
write_source_code(&spec);
} }

View file

@ -18,12 +18,9 @@ use itertools::Itertools;
use proc_macro2::{Term, TokenTree}; use proc_macro2::{Term, TokenTree};
use quote::Tokens; use quote::Tokens;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::Path;
use syn::Ident; use syn::Ident;
pub trait ExtensionExt {}
pub struct Foo;
impl Foo {
const FOO: u32 = 1;
}
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum CType { pub enum CType {
USize, USize,
@ -31,6 +28,7 @@ pub enum CType {
U64, U64,
Float, Float,
} }
impl CType { impl CType {
fn to_tokens(&self) -> Tokens { fn to_tokens(&self) -> Tokens {
let term = match self { let term = match self {
@ -371,6 +369,17 @@ pub trait ConstantExt {
fn notation(&self) -> Option<&str>; fn notation(&self) -> Option<&str>;
} }
impl ConstantExt for vkxml::ExtensionEnum {
fn variant_ident(&self, enum_name: &str) -> Ident {
variant_ident(enum_name, &self.name)
}
fn to_tokens(&self) -> Tokens {
Constant::from_extension_enum(self).expect("").to_tokens()
}
fn notation(&self) -> Option<&str> {
self.notation.as_ref().map(|s| s.as_str())
}
}
impl ConstantExt for vkxml::Constant { impl ConstantExt for vkxml::Constant {
fn variant_ident(&self, enum_name: &str) -> Ident { fn variant_ident(&self, enum_name: &str) -> Ident {
variant_ident(enum_name, &self.name) variant_ident(enum_name, &self.name)
@ -382,6 +391,7 @@ impl ConstantExt for vkxml::Constant {
self.notation.as_ref().map(|s| s.as_str()) self.notation.as_ref().map(|s| s.as_str())
} }
} }
pub enum Constant { pub enum Constant {
Number(i32), Number(i32),
Hex(String), Hex(String),
@ -448,7 +458,8 @@ impl Constant {
} }
} }
pub fn from_extension_enum(constant: &vkxml::ExtensionEnum) -> Self { pub fn from_extension_enum(constant: &vkxml::ExtensionEnum) -> Option<Self> {
println!("{:#?}", constant);
let number = constant.number.map(|n| Constant::Number(n)); let number = constant.number.map(|n| Constant::Number(n));
let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone())); let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone()));
let bitpos = constant.bitpos.map(|bit| Constant::BitPos(bit)); let bitpos = constant.bitpos.map(|bit| Constant::BitPos(bit));
@ -456,13 +467,16 @@ impl Constant {
.c_expression .c_expression
.as_ref() .as_ref()
.map(|e| Constant::CExpr(e.clone())); .map(|e| Constant::CExpr(e.clone()));
number.or(hex).or(bitpos).or(expr).expect("") number.or(hex).or(bitpos).or(expr)
} }
pub fn from_extension_constant(constant: &vkxml::ExtensionConstant) -> Self { pub fn from_extension_constant(constant: &vkxml::ExtensionConstant) -> Self {
let number = constant.number.map(|n| Constant::Number(n)); let number = constant.number.map(|n| Constant::Number(n));
let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone())); let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone()));
let bitpos = constant.bitpos.map(|bit| Constant::BitPos(bit)); let bitpos = constant.bitpos.map(|bit| Constant::BitPos(bit));
let text = constant.text.as_ref().map(|bit| Constant::Text(bit.clone())); let text = constant
.text
.as_ref()
.map(|bit| Constant::Text(bit.clone()));
let expr = constant let expr = constant
.c_expression .c_expression
.as_ref() .as_ref()
@ -759,6 +773,45 @@ fn generate_function_pointers(ident: Ident, commands: &[&vkxml::Command]) -> quo
} }
} }
} }
pub fn generate_interface_item(items: &[vk_parse::InterfaceItem]) -> quote::Tokens {
use vk_parse::InterfaceItem::*;
for item in items {
match item {
Type { name,.. } => println!("type {}", name),
Enum(e) => println!("type {}", name),
_ => ()
};
}
quote!{}
}
pub fn generate_extension2(
extension: &vk_parse::Extension,
cmd_map: &CommandMap,
) -> Option<quote::Tokens> {
let _ = extension
.supported
.as_ref()
.filter(|s| s.as_str() == "vulkan")?;
extension
.items
.iter()
.filter_map(|item| match item {
vk_parse::ExtensionItem::Require {
extension: ext,
feature,
items,
..
} => {
//println!("feature {:?} - {:?}", feature, ext);
let t = generate_interface_item(items);
Some(t)
}
_ => None,
})
.collect_vec();
None
}
pub fn generate_extension(extension: &vkxml::Extension, cmd_map: &CommandMap) -> quote::Tokens { pub fn generate_extension(extension: &vkxml::Extension, cmd_map: &CommandMap) -> quote::Tokens {
// Don't generate disabled or reserved extensions // Don't generate disabled or reserved extensions
if extension.disabled { if extension.disabled {
@ -794,13 +847,12 @@ pub fn generate_extension(extension: &vkxml::Extension, cmd_map: &CommandMap) ->
.elements .elements
.iter() .iter()
.flat_map(|ext| { .flat_map(|ext| {
println!("{:?}", ext);
if let &vkxml::ExtensionElement::Require(ref spec) = ext { if let &vkxml::ExtensionElement::Require(ref spec) = ext {
spec.elements spec.elements
.iter() .iter()
.filter_map(|extension_spec| match extension_spec { .filter_map(|extension_spec| match extension_spec {
vkxml::ExtensionSpecificationElement::Enum(ref _enum) => { vkxml::ExtensionSpecificationElement::Enum(ref _enum) => Some(_enum),
Some(_enum)
}
_ => None, _ => None,
}) })
.collect() .collect()
@ -809,16 +861,21 @@ pub fn generate_extension(extension: &vkxml::Extension, cmd_map: &CommandMap) ->
} }
}) })
.collect_vec(); .collect_vec();
let variants = extension_enums.iter().map(|&constant|{ let extend_enums = extension_enums.iter().filter_map(|&constant| {
let c = Constant::from_extension_enum(constant); let c = Constant::from_extension_enum(constant)?;
let variant_ident = variant_ident(&constant.name, &constant.name); let variant_ident = variant_ident(&constant.extends, &constant.name);
(variant_ident, c.to_tokens()) let tokens = c.to_tokens();
let enum_name = name_to_tokens(&constant.extends);
Some(quote! {
impl #enum_name {
pub const #variant_ident: Self = #enum_name(#tokens);
}
})
}); });
println!("{:?}", extension_enums); //println!("{:#?}", extension_enums);
//let enum_tokens = im
quote!{ quote!{
#fp #fp
//#(#enum_tokens)* #(#extend_enums)*
} }
} }
pub fn generate_typedef(typedef: &vkxml::Typedef) -> Tokens { pub fn generate_typedef(typedef: &vkxml::Typedef) -> Tokens {
@ -1220,9 +1277,44 @@ pub fn generate_constant(constant: &vkxml::Constant) -> Tokens {
} }
} }
pub fn write_source_code(spec: &vkxml::Registry) { pub fn write_source_code(path: &Path) {
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
let spec2 = vk_parse::parse_file(path);
let extensions: &Vec<vk_parse::Extension> = spec2
.0
.iter()
.filter_map(|item| match item {
vk_parse::RegistryItem::Extensions { items: ext, .. } => Some(ext),
_ => None,
})
.nth(0)
.expect("extension");
// for ext in extensions {
// println!("{}", ext.name);
// for item in &ext.items {
// match item {
// vk_parse::ExtensionItem::Require { items, .. } => {
// for interface in items {
// match interface {
// vk_parse::InterfaceItem::Enum(ref e) => match &e.spec {
// vk_parse::EnumSpec::Value { value, extends } => {
// print!("{} - ", e.name,);
// println!("{} {:?}", value, extends);
// }
// _ => (),
// },
// _ => (),
// }
// }
// }
// _ => (),
// }
// }
// println!("ext {} {:?}", ext.name, ext.supported);
// }
let spec = vk_parse::parse_file_as_vkxml(path);
let commands: HashMap<vkxml::Identifier, &vkxml::Command> = spec let commands: HashMap<vkxml::Identifier, &vkxml::Command> = spec
.elements .elements
.iter() .iter()
@ -1233,6 +1325,10 @@ pub fn write_source_code(spec: &vkxml::Registry) {
.flat_map(|cmds| cmds.elements.iter().map(|cmd| (cmd.name.clone(), cmd))) .flat_map(|cmds| cmds.elements.iter().map(|cmd| (cmd.name.clone(), cmd)))
.collect(); .collect();
let _ = extensions
.iter()
.filter_map(|ext| generate_extension2(ext, &commands))
.collect_vec();
let features: Vec<&vkxml::Feature> = spec let features: Vec<&vkxml::Feature> = spec
.elements .elements
.iter() .iter()
@ -1243,15 +1339,15 @@ pub fn write_source_code(spec: &vkxml::Registry) {
.flat_map(|features| features.elements.iter().map(|feature| feature)) .flat_map(|features| features.elements.iter().map(|feature| feature))
.collect(); .collect();
let extensions: Vec<&vkxml::Extension> = spec // let extensions: Vec<&vkxml::Extension> = spec
.elements // .elements
.iter() // .iter()
.filter_map(|elem| match elem { // .filter_map(|elem| match elem {
&vkxml::RegistryElement::Extensions(ref extensions) => Some(extensions), // &vkxml::RegistryElement::Extensions(ref extensions) => Some(extensions),
_ => None, // _ => None,
}) // })
.flat_map(|extensions| extensions.elements.iter().map(|extension| extension)) // .flat_map(|extensions| extensions.elements.iter().map(|extension| extension))
.collect(); // .collect();
let definitions: Vec<&vkxml::DefinitionsElement> = spec let definitions: Vec<&vkxml::DefinitionsElement> = spec
.elements .elements
@ -1313,10 +1409,10 @@ pub fn write_source_code(spec: &vkxml::Registry) {
.map(|feature| generate_feature(feature, &commands)) .map(|feature| generate_feature(feature, &commands))
.collect(); .collect();
let extension_code: Vec<_> = extensions // let extension_code: Vec<_> = extensions
.iter() // .iter()
.map(|ext| generate_extension(ext, &commands)) // .map(|ext| generate_extension(ext, &commands))
.collect(); // .collect();
let mut file = File::create("../ash/src/vk.rs").expect("vk"); let mut file = File::create("../ash/src/vk.rs").expect("vk");
let bitflags_macro = vk_bitflags_wrapped_macro(); let bitflags_macro = vk_bitflags_wrapped_macro();
let handle_nondispatchable_macro = handle_nondispatchable_macro(); let handle_nondispatchable_macro = handle_nondispatchable_macro();
@ -1335,7 +1431,7 @@ pub fn write_source_code(spec: &vkxml::Registry) {
#(#enum_code)* #(#enum_code)*
#(#bitflags_code)* #(#bitflags_code)*
#(#constants_code)* #(#constants_code)*
#(#extension_code)* //#(#extension_code)*
}; };
write!(&mut file, "{}", source_code); write!(&mut file, "{}", source_code);
} }