Skip to content

Commit

Permalink
vk_video: Generate low-level structs with bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed May 23, 2021
1 parent 684fda8 commit f3a501d
Show file tree
Hide file tree
Showing 7 changed files with 7,716 additions and 13 deletions.
2 changes: 2 additions & 0 deletions ash/src/vk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ mod features;
pub use features::*;
mod platform_types;
pub use platform_types::*;
#[allow(nonstandard_style, dead_code, clippy::redundant_static_lifetimes)]
pub mod video;
#[doc = r" Iterates through the pointer chain. Includes the item that is passed into the function."]
#[doc = r" Stops at the last `BaseOutStructure` that has a null `p_next` field."]
pub(crate) unsafe fn ptr_chain_iter<T>(ptr: &mut T) -> impl Iterator<Item = *mut BaseOutStructure> {
Expand Down
1 change: 1 addition & 0 deletions ash/src/vk/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::vk::bitflags::*;
use crate::vk::constants::*;
use crate::vk::enums::*;
use crate::vk::platform_types::*;
use crate::vk::video::*;
use crate::vk::{ptr_chain_iter, Handle};
use std::fmt;
use std::os::raw::*;
Expand Down
7,664 changes: 7,664 additions & 0 deletions ash/src/vk/video.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Maik Klein <maikklein@googlemail.com>"]
edition = "2018"

[dependencies]
bindgen = "0.58"
heck = "0.3"
itertools = "0.10"
nom = "6.0"
Expand Down
7 changes: 2 additions & 5 deletions generator/src/bin/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ use std::path::Path;
fn main() {
let cwd = std::env::current_dir().unwrap();
if cwd.ends_with("generator") {
write_source_code(Path::new("Vulkan-Headers/registry/vk.xml"), "../ash/src");
write_source_code(Path::new("Vulkan-Headers"), "../ash/src");
} else {
write_source_code(
Path::new("generator/Vulkan-Headers/registry/vk.xml"),
"ash/src",
);
write_source_code(Path::new("generator/Vulkan-Headers"), "ash/src");
}
}
52 changes: 45 additions & 7 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2496,12 +2496,22 @@ pub fn generate_const_debugs(const_values: &BTreeMap<Ident, ConstantTypeInfo>) -
pub fn generate_aliases_of_types(
types: &vk_parse::Types,
ty_cache: &mut HashSet<Ident, impl BuildHasher>,
include_headers: &mut Vec<String>,
) -> TokenStream {
let aliases = types
.children
.iter()
.filter_map(|child| match child {
vk_parse::TypesChild::Type(ty) => Some((ty.name.as_ref()?, ty.alias.as_ref()?)),
vk_parse::TypesChild::Type(ty) => {
if ty.category.as_deref() == Some("include") {
include_headers.push(
ty.name
.clone()
.expect("Include type must provide header name"),
);
}
Some((ty.name.as_ref()?, ty.alias.as_ref()?))
}
_ => None,
})
.filter_map(|(name, alias)| {
Expand All @@ -2519,10 +2529,11 @@ pub fn generate_aliases_of_types(
#(#aliases)*
}
}
pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
pub fn write_source_code<P: AsRef<Path>>(vk_headers_dir: &Path, src_dir: P) {
let vk_xml = vk_headers_dir.join("registry/vk.xml");
use std::fs::File;
use std::io::Write;
let (spec2, _errors) = vk_parse::parse_file(vk_xml).expect("Invalid xml file");
let (spec2, _errors) = vk_parse::parse_file(&vk_xml).expect("Invalid xml file");
let extensions: &Vec<vk_parse::Extension> = spec2
.0
.iter()
Expand All @@ -2533,18 +2544,21 @@ pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
.next()
.expect("extension");
let mut ty_cache = HashSet::new();
let mut header_includes = vec![];
let aliases: Vec<_> = spec2
.0
.iter()
.filter_map(|item| match item {
vk_parse::RegistryChild::Types(ref ty) => {
Some(generate_aliases_of_types(ty, &mut ty_cache))
}
vk_parse::RegistryChild::Types(ref ty) => Some(generate_aliases_of_types(
ty,
&mut ty_cache,
&mut header_includes,
)),
_ => None,
})
.collect();

let spec = vk_parse::parse_file_as_vkxml(vk_xml).expect("Invalid xml file.");
let spec = vk_parse::parse_file_as_vkxml(&vk_xml).expect("Invalid xml file.");
let cmd_aliases: HashMap<String, String> = spec2
.0
.iter()
Expand Down Expand Up @@ -2748,6 +2762,7 @@ pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
use crate::vk::bitflags::*;
use crate::vk::constants::*;
use crate::vk::enums::*;
use crate::vk::video::*;
#(#definition_code)*
};

Expand Down Expand Up @@ -2827,6 +2842,8 @@ pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
pub use features::*;
mod platform_types;
pub use platform_types::*;
#[allow(nonstandard_style, dead_code, clippy::redundant_static_lifetimes)]
pub mod video;

#ptr_chain_code

Expand Down Expand Up @@ -2857,4 +2874,25 @@ pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
write!(&mut vk_aliases_file, "{}", aliases).expect("Unable to write vk/aliases.rs");
write!(&mut vk_rs_file, "{} {}", vk_rs_clippy_lints, vk_rs_code)
.expect("Unable to write vk.rs");

let vk_include = vk_headers_dir.join("include");

let mut bindings = bindgen::Builder::default()
.clang_arg(format!(
"-I{}",
vk_include.to_str().expect("Valid UTF8 string")
))
.header("stdint.h");

for h in header_includes {
if h.starts_with("vk_video") {
bindings = bindings.header(vk_include.join(h).to_str().expect("Valid UTF8 string"));
}
}

bindings
.generate()
.expect("Unable to generate bindings")
.write_to_file(vk_dir.join("video.rs"))
.expect("Couldn't write bindings!");
}

0 comments on commit f3a501d

Please sign in to comment.