Skip to content

Commit c90d260

Browse files
committed
vk_video: Generate low-level structs with bindgen
1 parent c302ebc commit c90d260

File tree

7 files changed

+7716
-13
lines changed

7 files changed

+7716
-13
lines changed

ash/src/vk.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ mod features;
2626
pub use features::*;
2727
mod platform_types;
2828
pub use platform_types::*;
29+
#[allow(nonstandard_style, dead_code, clippy::redundant_static_lifetimes)]
30+
pub mod video;
2931
#[doc = r" Iterates through the pointer chain. Includes the item that is passed into the function."]
3032
#[doc = r" Stops at the last `BaseOutStructure` that has a null `p_next` field."]
3133
pub(crate) unsafe fn ptr_chain_iter<T>(ptr: &mut T) -> impl Iterator<Item = *mut BaseOutStructure> {

ash/src/vk/definitions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::vk::bitflags::*;
33
use crate::vk::constants::*;
44
use crate::vk::enums::*;
55
use crate::vk::platform_types::*;
6+
use crate::vk::video::*;
67
use crate::vk::{ptr_chain_iter, Handle};
78
use std::fmt;
89
use std::os::raw::*;

ash/src/vk/video.rs

Lines changed: 7664 additions & 0 deletions
Large diffs are not rendered by default.

generator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Maik Klein <maikklein@googlemail.com>"]
55
edition = "2018"
66

77
[dependencies]
8+
bindgen = "0.58"
89
heck = "0.3"
910
itertools = "0.10"
1011
nom = "6.0"

generator/src/bin/generator.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ use std::path::Path;
44
fn main() {
55
let cwd = std::env::current_dir().unwrap();
66
if cwd.ends_with("generator") {
7-
write_source_code(Path::new("Vulkan-Headers/registry/vk.xml"), "../ash/src");
7+
write_source_code(Path::new("Vulkan-Headers"), "../ash/src");
88
} else {
9-
write_source_code(
10-
Path::new("generator/Vulkan-Headers/registry/vk.xml"),
11-
"ash/src",
12-
);
9+
write_source_code(Path::new("generator/Vulkan-Headers"), "ash/src");
1310
}
1411
}

generator/src/lib.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,12 +2493,22 @@ pub fn generate_const_debugs(const_values: &BTreeMap<Ident, ConstantTypeInfo>) -
24932493
pub fn generate_aliases_of_types(
24942494
types: &vk_parse::Types,
24952495
ty_cache: &mut HashSet<Ident, impl BuildHasher>,
2496+
include_headers: &mut Vec<String>,
24962497
) -> TokenStream {
24972498
let aliases = types
24982499
.children
24992500
.iter()
25002501
.filter_map(|child| match child {
2501-
vk_parse::TypesChild::Type(ty) => Some((ty.name.as_ref()?, ty.alias.as_ref()?)),
2502+
vk_parse::TypesChild::Type(ty) => {
2503+
if ty.category.as_deref() == Some("include") {
2504+
include_headers.push(
2505+
ty.name
2506+
.clone()
2507+
.expect("Include type must provide header name"),
2508+
);
2509+
}
2510+
Some((ty.name.as_ref()?, ty.alias.as_ref()?))
2511+
}
25022512
_ => None,
25032513
})
25042514
.filter_map(|(name, alias)| {
@@ -2516,10 +2526,11 @@ pub fn generate_aliases_of_types(
25162526
#(#aliases)*
25172527
}
25182528
}
2519-
pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
2529+
pub fn write_source_code<P: AsRef<Path>>(vk_headers_dir: &Path, src_dir: P) {
2530+
let vk_xml = vk_headers_dir.join("registry/vk.xml");
25202531
use std::fs::File;
25212532
use std::io::Write;
2522-
let (spec2, _errors) = vk_parse::parse_file(vk_xml).expect("Invalid xml file");
2533+
let (spec2, _errors) = vk_parse::parse_file(&vk_xml).expect("Invalid xml file");
25232534
let extensions: &Vec<vk_parse::Extension> = spec2
25242535
.0
25252536
.iter()
@@ -2530,18 +2541,21 @@ pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
25302541
.next()
25312542
.expect("extension");
25322543
let mut ty_cache = HashSet::new();
2544+
let mut header_includes = vec![];
25332545
let aliases: Vec<_> = spec2
25342546
.0
25352547
.iter()
25362548
.filter_map(|item| match item {
2537-
vk_parse::RegistryChild::Types(ref ty) => {
2538-
Some(generate_aliases_of_types(ty, &mut ty_cache))
2539-
}
2549+
vk_parse::RegistryChild::Types(ref ty) => Some(generate_aliases_of_types(
2550+
ty,
2551+
&mut ty_cache,
2552+
&mut header_includes,
2553+
)),
25402554
_ => None,
25412555
})
25422556
.collect();
25432557

2544-
let spec = vk_parse::parse_file_as_vkxml(vk_xml).expect("Invalid xml file.");
2558+
let spec = vk_parse::parse_file_as_vkxml(&vk_xml).expect("Invalid xml file.");
25452559
let cmd_aliases: HashMap<String, String> = spec2
25462560
.0
25472561
.iter()
@@ -2745,6 +2759,7 @@ pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
27452759
use crate::vk::bitflags::*;
27462760
use crate::vk::constants::*;
27472761
use crate::vk::enums::*;
2762+
use crate::vk::video::*;
27482763
#(#definition_code)*
27492764
};
27502765

@@ -2824,6 +2839,8 @@ pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
28242839
pub use features::*;
28252840
mod platform_types;
28262841
pub use platform_types::*;
2842+
#[allow(nonstandard_style, dead_code, clippy::redundant_static_lifetimes)]
2843+
pub mod video;
28272844

28282845
#ptr_chain_code
28292846

@@ -2854,4 +2871,25 @@ pub fn write_source_code<P: AsRef<Path>>(vk_xml: &Path, src_dir: P) {
28542871
write!(&mut vk_aliases_file, "{}", aliases).expect("Unable to write vk/aliases.rs");
28552872
write!(&mut vk_rs_file, "{} {}", vk_rs_clippy_lints, vk_rs_code)
28562873
.expect("Unable to write vk.rs");
2874+
2875+
let vk_include = vk_headers_dir.join("include");
2876+
2877+
let mut bindings = bindgen::Builder::default()
2878+
.clang_arg(format!(
2879+
"-I{}",
2880+
vk_include.to_str().expect("Valid UTF8 string")
2881+
))
2882+
.header("stdint.h");
2883+
2884+
for h in header_includes {
2885+
if h.starts_with("vk_video") {
2886+
bindings = bindings.header(vk_include.join(h).to_str().expect("Valid UTF8 string"));
2887+
}
2888+
}
2889+
2890+
bindings
2891+
.generate()
2892+
.expect("Unable to generate bindings")
2893+
.write_to_file(vk_dir.join("video.rs"))
2894+
.expect("Couldn't write bindings!");
28572895
}

0 commit comments

Comments
 (0)