2018-03-10 20:07:12 +11:00
|
|
|
#![recursion_limit = "256"]
|
2019-05-26 05:34:18 +10:00
|
|
|
|
2018-06-04 19:26:14 +10:00
|
|
|
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
|
2018-07-07 22:49:17 +10:00
|
|
|
use itertools::Itertools;
|
2021-05-11 08:00:55 +10:00
|
|
|
use nom::{
|
2021-06-06 19:00:29 +10:00
|
|
|
alt, char,
|
|
|
|
character::complete::{digit1, hex_digit1, multispace1},
|
|
|
|
complete, delimited, do_parse, many1, map, named, none_of, one_of, opt, pair, preceded, tag,
|
|
|
|
terminated, value,
|
2021-05-11 08:00:55 +10:00
|
|
|
};
|
2021-05-08 20:25:10 +10:00
|
|
|
use once_cell::sync::Lazy;
|
2021-01-03 00:37:10 +11:00
|
|
|
use proc_macro2::{Delimiter, Group, Literal, Span, TokenStream, TokenTree};
|
2021-05-08 20:25:10 +10:00
|
|
|
use quote::*;
|
|
|
|
use regex::Regex;
|
2019-03-15 10:22:52 +11:00
|
|
|
use std::collections::{BTreeMap, HashMap, HashSet};
|
2019-03-17 03:56:27 +11:00
|
|
|
use std::fmt::Display;
|
2019-10-21 02:18:40 +11:00
|
|
|
use std::hash::BuildHasher;
|
2018-07-21 20:56:16 +10:00
|
|
|
use std::path::Path;
|
2018-03-10 07:45:58 +11:00
|
|
|
use syn::Ident;
|
2019-03-17 03:46:26 +11:00
|
|
|
|
2018-07-21 20:56:16 +10:00
|
|
|
pub trait ExtensionExt {}
|
2018-06-24 20:09:37 +10:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum CType {
|
|
|
|
USize,
|
|
|
|
U32,
|
|
|
|
U64,
|
|
|
|
Float,
|
2018-08-03 23:39:25 +10:00
|
|
|
Bool32,
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
2018-07-21 20:56:16 +10:00
|
|
|
|
2021-05-30 21:59:18 +10:00
|
|
|
impl CType {
|
2021-07-09 19:55:20 +10:00
|
|
|
fn to_string(self) -> &'static str {
|
2021-05-30 21:59:18 +10:00
|
|
|
match self {
|
|
|
|
Self::USize => "usize",
|
|
|
|
Self::U32 => "u32",
|
|
|
|
Self::U64 => "u64",
|
|
|
|
Self::Float => "f32",
|
|
|
|
Self::Bool32 => "Bool32",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 06:56:43 +11:00
|
|
|
impl quote::ToTokens for CType {
|
2021-01-03 00:37:10 +11:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2021-05-30 21:59:18 +10:00
|
|
|
format_ident!("{}", self.to_string()).to_tokens(tokens);
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 20:34:14 +10:00
|
|
|
named!(ctype<&str, CType>,
|
2018-06-24 20:09:37 +10:00
|
|
|
alt!(
|
2021-05-30 21:59:18 +10:00
|
|
|
value!(CType::U64, complete!(tag!("ULL"))) |
|
|
|
|
value!(CType::U32, complete!(tag!("U")))
|
2018-06-24 20:09:37 +10:00
|
|
|
)
|
|
|
|
);
|
2021-05-11 08:00:55 +10:00
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
named!(cexpr<&str, (CType, String)>,
|
2021-05-11 08:00:55 +10:00
|
|
|
alt!(
|
|
|
|
map!(cfloat, |f| (CType::Float, format!("{:.2}", f))) |
|
2021-05-30 21:59:18 +10:00
|
|
|
inverse_number |
|
|
|
|
decimal_number |
|
|
|
|
hexadecimal_number
|
2021-05-11 08:00:55 +10:00
|
|
|
)
|
2018-06-24 20:09:37 +10:00
|
|
|
);
|
|
|
|
|
2021-05-11 08:00:55 +10:00
|
|
|
named!(decimal_number<&str, (CType, String)>,
|
2018-06-24 20:09:37 +10:00
|
|
|
do_parse!(
|
2021-05-11 08:00:55 +10:00
|
|
|
num: digit1 >>
|
|
|
|
typ: ctype >>
|
|
|
|
((typ, num.to_string()))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-05-30 21:59:18 +10:00
|
|
|
named!(hexadecimal_number<&str, (CType, String)>,
|
|
|
|
preceded!(
|
|
|
|
alt!(tag!("0x") | tag!("0X")),
|
|
|
|
map!(
|
|
|
|
pair!(hex_digit1, ctype),
|
|
|
|
|(num, typ)| (typ, format!("0x{}{}", num.to_ascii_lowercase(), typ.to_string())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-05-11 08:00:55 +10:00
|
|
|
named!(inverse_number<&str, (CType, String)>,
|
|
|
|
map!(
|
|
|
|
delimited!(
|
|
|
|
tag!("("),
|
|
|
|
pair!(
|
|
|
|
preceded!(tag!("~"), decimal_number),
|
|
|
|
opt!(preceded!(tag!("-"), digit1))
|
|
|
|
),
|
|
|
|
tag!(")")
|
|
|
|
),
|
|
|
|
|((ctyp, num), minus_num)| {
|
|
|
|
let expr = if let Some(minus) = minus_num {
|
|
|
|
format!("!{}-{}", num, minus)
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
format!("!{}", num)
|
|
|
|
};
|
|
|
|
(ctyp, expr)
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
named!(cfloat<&str, f32>,
|
2021-05-11 08:00:55 +10:00
|
|
|
terminated!(nom::number::complete::float, one_of!("fF"))
|
2018-06-24 20:09:37 +10:00
|
|
|
);
|
|
|
|
|
2021-06-06 19:00:29 +10:00
|
|
|
// Like a C string, but does not support quote escaping and expects at least one character.
|
|
|
|
// If needed, use https://github.com/Geal/nom/blob/8e09f0c3029d32421b5b69fb798cef6855d0c8df/tests/json.rs#L61-L81
|
|
|
|
named!(c_include_string<&str, String>,
|
|
|
|
delimited!(
|
|
|
|
char!('"'),
|
|
|
|
map!(
|
|
|
|
many1!(none_of!("\"")),
|
|
|
|
|chars| chars.iter().map(char::to_string).join("")
|
|
|
|
),
|
|
|
|
char!('"')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
named!(c_include<&str, String>,
|
|
|
|
preceded!(tag!("#include"), preceded!(multispace1, c_include_string))
|
|
|
|
);
|
|
|
|
|
2021-03-01 20:05:13 +11:00
|
|
|
fn khronos_link<S: Display + ?Sized>(name: &S) -> Literal {
|
2019-03-17 03:56:27 +11:00
|
|
|
Literal::string(&format!(
|
2020-01-19 19:56:12 +11:00
|
|
|
"<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/{name}.html>",
|
2019-03-17 03:56:27 +11:00
|
|
|
name = name
|
|
|
|
))
|
2019-03-17 03:46:26 +11:00
|
|
|
}
|
2019-03-14 14:51:22 +11:00
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
pub fn define_handle_macro() -> TokenStream {
|
2018-06-24 20:09:37 +10:00
|
|
|
quote! {
|
2018-11-17 03:25:20 +11:00
|
|
|
#[macro_export]
|
2018-06-24 20:09:37 +10:00
|
|
|
macro_rules! define_handle{
|
2019-03-18 03:54:26 +11:00
|
|
|
($name: ident, $ty: ident) => {
|
|
|
|
define_handle!($name, $ty, doc = "");
|
|
|
|
};
|
2019-03-14 14:51:22 +11:00
|
|
|
($name: ident, $ty: ident, $doc_link: meta) => {
|
2018-08-03 20:34:14 +10:00
|
|
|
#[repr(transparent)]
|
2018-09-17 05:15:01 +10:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash)]
|
2019-03-14 14:51:22 +11:00
|
|
|
#[$doc_link]
|
2018-08-02 10:32:16 +10:00
|
|
|
pub struct $name(*mut u8);
|
|
|
|
impl Default for $name {
|
2018-07-31 03:53:12 +10:00
|
|
|
fn default() -> $name {
|
|
|
|
$name::null()
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
2018-08-02 10:32:16 +10:00
|
|
|
impl Handle for $name {
|
|
|
|
const TYPE: ObjectType = ObjectType::$ty;
|
|
|
|
fn as_raw(self) -> u64 { self.0 as u64 }
|
|
|
|
fn from_raw(x: u64) -> Self { $name(x as _) }
|
|
|
|
}
|
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
unsafe impl Send for $name {}
|
|
|
|
unsafe impl Sync for $name {}
|
|
|
|
|
|
|
|
impl $name{
|
2019-05-26 06:00:15 +10:00
|
|
|
pub const fn null() -> Self{
|
2018-08-02 10:32:16 +10:00
|
|
|
$name(::std::ptr::null_mut())
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 05:15:01 +10:00
|
|
|
|
|
|
|
impl fmt::Pointer for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Pointer::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
pub fn handle_nondispatchable_macro() -> TokenStream {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-17 03:25:20 +11:00
|
|
|
#[macro_export]
|
2018-06-24 20:09:37 +10:00
|
|
|
macro_rules! handle_nondispatchable {
|
2018-08-02 10:32:16 +10:00
|
|
|
($name: ident, $ty: ident) => {
|
2019-03-14 14:51:22 +11:00
|
|
|
handle_nondispatchable!($name, $ty, doc = "");
|
|
|
|
};
|
|
|
|
($name: ident, $ty: ident, $doc_link: meta) => {
|
2018-08-03 20:34:14 +10:00
|
|
|
#[repr(transparent)]
|
2018-07-31 03:53:12 +10:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Default)]
|
2019-03-14 14:51:22 +11:00
|
|
|
#[$doc_link]
|
2018-08-25 21:09:44 +10:00
|
|
|
pub struct $name(u64);
|
2018-08-02 10:32:16 +10:00
|
|
|
|
|
|
|
impl Handle for $name {
|
|
|
|
const TYPE: ObjectType = ObjectType::$ty;
|
|
|
|
fn as_raw(self) -> u64 { self.0 as u64 }
|
|
|
|
fn from_raw(x: u64) -> Self { $name(x as _) }
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
impl $name{
|
2019-05-26 06:00:15 +10:00
|
|
|
pub const fn null() -> $name{
|
2018-06-24 20:09:37 +10:00
|
|
|
$name(0)
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 05:15:01 +10:00
|
|
|
|
|
|
|
impl fmt::Pointer for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-06-24 20:09:37 +10:00
|
|
|
write!(f, "0x{:x}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 05:15:01 +10:00
|
|
|
impl fmt::Debug for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-06-24 20:09:37 +10:00
|
|
|
write!(f, "0x{:x}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 00:37:10 +11:00
|
|
|
pub fn vk_bitflags_wrapped_macro() -> TokenStream {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-17 03:25:20 +11:00
|
|
|
#[macro_export]
|
2018-06-24 20:09:37 +10:00
|
|
|
macro_rules! vk_bitflags_wrapped {
|
|
|
|
($name: ident, $all: expr, $flag_type: ty) => {
|
|
|
|
|
|
|
|
impl Default for $name{
|
|
|
|
fn default() -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name(0)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
#[inline]
|
2019-05-26 06:00:15 +10:00
|
|
|
pub const fn empty() -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name(0)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-26 06:00:15 +10:00
|
|
|
pub const fn all() -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name($all)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-26 06:00:15 +10:00
|
|
|
pub const fn from_raw(x: $flag_type) -> Self { $name(x) }
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
#[inline]
|
2019-05-26 06:00:15 +10:00
|
|
|
pub const fn as_raw(self) -> $flag_type { self.0 }
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(self) -> bool {
|
|
|
|
self == $name::empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_all(self) -> bool {
|
|
|
|
self & $name::all() == $name::all()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn intersects(self, other: $name) -> bool {
|
|
|
|
self & other != $name::empty()
|
|
|
|
}
|
|
|
|
|
2018-09-17 04:59:55 +10:00
|
|
|
/// Returns whether `other` is a subset of `self`
|
2018-06-24 20:09:37 +10:00
|
|
|
#[inline]
|
2018-09-17 04:59:55 +10:00
|
|
|
pub fn contains(self, other: $name) -> bool {
|
2018-06-24 20:09:37 +10:00
|
|
|
self & other == other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitOr for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn bitor(self, rhs: $name) -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name (self.0 | rhs.0 )
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitOrAssign for $name {
|
|
|
|
#[inline]
|
|
|
|
fn bitor_assign(&mut self, rhs: $name) {
|
|
|
|
*self = *self | rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitAnd for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn bitand(self, rhs: $name) -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name (self.0 & rhs.0)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitAndAssign for $name {
|
|
|
|
#[inline]
|
|
|
|
fn bitand_assign(&mut self, rhs: $name) {
|
|
|
|
*self = *self & rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitXor for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn bitxor(self, rhs: $name) -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name (self.0 ^ rhs.0 )
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitXorAssign for $name {
|
|
|
|
#[inline]
|
|
|
|
fn bitxor_assign(&mut self, rhs: $name) {
|
|
|
|
*self = *self ^ rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Sub for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, rhs: $name) -> $name {
|
|
|
|
self & !rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::SubAssign for $name {
|
|
|
|
#[inline]
|
|
|
|
fn sub_assign(&mut self, rhs: $name) {
|
|
|
|
*self = *self - rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Not for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn not(self) -> $name {
|
|
|
|
self ^ $name::all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 20:54:31 +10:00
|
|
|
|
2020-12-13 06:56:43 +11:00
|
|
|
fn is_opaque_type(ty: &str) -> bool {
|
|
|
|
matches!(
|
|
|
|
ty,
|
|
|
|
"void"
|
|
|
|
| "wl_display"
|
|
|
|
| "wl_surface"
|
|
|
|
| "Display"
|
|
|
|
| "xcb_connection_t"
|
|
|
|
| "ANativeWindow"
|
|
|
|
| "AHardwareBuffer"
|
|
|
|
| "CAMetalLayer"
|
2021-01-03 00:37:10 +11:00
|
|
|
| "IDirectFB"
|
|
|
|
| "IDirectFBSurface"
|
2020-12-13 06:56:43 +11:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum ConstVal {
|
|
|
|
U32(u32),
|
|
|
|
U64(u64),
|
|
|
|
Float(f32),
|
|
|
|
}
|
|
|
|
impl ConstVal {
|
|
|
|
pub fn bits(&self) -> u64 {
|
|
|
|
match self {
|
|
|
|
ConstVal::U64(n) => *n,
|
|
|
|
_ => panic!("Constval not supported"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 21:18:22 +10:00
|
|
|
pub trait ConstantExt {
|
2021-04-19 05:58:40 +10:00
|
|
|
fn constant(&self, enum_name: &str) -> Constant;
|
2018-07-11 21:18:22 +10:00
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident;
|
|
|
|
fn notation(&self) -> Option<&str>;
|
2021-04-19 05:58:40 +10:00
|
|
|
fn is_alias(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2018-07-11 21:18:22 +10:00
|
|
|
}
|
|
|
|
|
2018-07-21 20:56:16 +10:00
|
|
|
impl ConstantExt for vkxml::ExtensionEnum {
|
2021-04-19 05:58:40 +10:00
|
|
|
fn constant(&self, _enum_name: &str) -> Constant {
|
2020-05-10 21:42:07 +10:00
|
|
|
Constant::from_extension_enum(self).unwrap()
|
|
|
|
}
|
2018-07-21 20:56:16 +10:00
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident {
|
|
|
|
variant_ident(enum_name, &self.name)
|
|
|
|
}
|
|
|
|
fn notation(&self) -> Option<&str> {
|
2020-03-15 10:55:26 +11:00
|
|
|
self.notation.as_deref()
|
2018-07-21 20:56:16 +10:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
|
2021-04-19 05:58:40 +10:00
|
|
|
impl ConstantExt for vk_parse::Enum {
|
|
|
|
fn constant(&self, enum_name: &str) -> Constant {
|
|
|
|
Constant::from_vk_parse_enum_spec(&self.spec, Some(enum_name), None)
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
}
|
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident {
|
|
|
|
variant_ident(enum_name, &self.name)
|
|
|
|
}
|
|
|
|
fn notation(&self) -> Option<&str> {
|
|
|
|
self.comment.as_deref()
|
|
|
|
}
|
|
|
|
fn is_alias(&self) -> bool {
|
|
|
|
matches!(self.spec, vk_parse::EnumSpec::Alias { .. })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 21:18:22 +10:00
|
|
|
impl ConstantExt for vkxml::Constant {
|
2021-04-19 05:58:40 +10:00
|
|
|
fn constant(&self, _enum_name: &str) -> Constant {
|
2020-05-10 21:42:07 +10:00
|
|
|
Constant::from_constant(self)
|
|
|
|
}
|
2018-07-11 21:18:22 +10:00
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident {
|
|
|
|
variant_ident(enum_name, &self.name)
|
|
|
|
}
|
|
|
|
fn notation(&self) -> Option<&str> {
|
2020-03-15 10:55:26 +11:00
|
|
|
self.notation.as_deref()
|
2018-07-11 21:18:22 +10:00
|
|
|
}
|
|
|
|
}
|
2018-07-21 20:56:16 +10:00
|
|
|
|
2020-05-10 21:42:07 +10:00
|
|
|
#[derive(Clone, Debug)]
|
2018-04-01 18:52:21 +10:00
|
|
|
pub enum Constant {
|
|
|
|
Number(i32),
|
|
|
|
Hex(String),
|
|
|
|
BitPos(u32),
|
|
|
|
CExpr(vkxml::CExpression),
|
|
|
|
Text(String),
|
2021-03-27 06:13:16 +11:00
|
|
|
Alias(Ident),
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
2020-03-23 02:05:30 +11:00
|
|
|
|
2020-12-13 06:56:43 +11:00
|
|
|
impl quote::ToTokens for Constant {
|
2021-01-03 00:37:10 +11:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2020-12-13 06:56:43 +11:00
|
|
|
match *self {
|
|
|
|
Constant::Number(n) => {
|
|
|
|
let number = interleave_number('_', 3, &n.to_string());
|
2021-01-03 00:37:10 +11:00
|
|
|
syn::LitInt::new(&number, Span::call_site()).to_tokens(tokens);
|
2020-12-13 06:56:43 +11:00
|
|
|
}
|
|
|
|
Constant::Hex(ref s) => {
|
|
|
|
let number = interleave_number('_', 4, s);
|
2021-01-03 00:37:10 +11:00
|
|
|
syn::LitInt::new(&format!("0x{}", number), Span::call_site()).to_tokens(tokens);
|
2020-12-13 06:56:43 +11:00
|
|
|
}
|
|
|
|
Constant::Text(ref text) => text.to_tokens(tokens),
|
|
|
|
Constant::CExpr(ref expr) => {
|
|
|
|
let (_, (_, rexpr)) = cexpr(expr).expect("Unable to parse cexpr");
|
2021-01-03 00:37:10 +11:00
|
|
|
tokens.extend(rexpr.parse::<TokenStream>());
|
2020-12-13 06:56:43 +11:00
|
|
|
}
|
|
|
|
Constant::BitPos(pos) => {
|
2021-05-08 20:25:10 +10:00
|
|
|
let value = 1u64 << pos;
|
2020-12-13 06:56:43 +11:00
|
|
|
let bit_string = format!("{:b}", value);
|
|
|
|
let bit_string = interleave_number('_', 4, &bit_string);
|
2021-01-03 00:37:10 +11:00
|
|
|
syn::LitInt::new(&format!("0b{}", bit_string), Span::call_site()).to_tokens(tokens);
|
2020-12-13 06:56:43 +11:00
|
|
|
}
|
2021-03-27 06:13:16 +11:00
|
|
|
Constant::Alias(ref value) => tokens.extend(quote!(Self::#value)),
|
2020-12-13 06:56:43 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
impl quote::ToTokens for ConstVal {
|
2021-01-03 00:37:10 +11:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2018-06-24 20:09:37 +10:00
|
|
|
match self {
|
|
|
|
ConstVal::U32(n) => n.to_tokens(tokens),
|
|
|
|
ConstVal::U64(n) => n.to_tokens(tokens),
|
|
|
|
ConstVal::Float(f) => f.to_tokens(tokens),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 02:18:40 +11:00
|
|
|
|
|
|
|
// Interleaves a number, for example 100000 => 100_000. Mostly used to make clippy happy
|
|
|
|
fn interleave_number(symbol: char, count: usize, n: &str) -> String {
|
|
|
|
let number: String = n
|
|
|
|
.chars()
|
|
|
|
.rev()
|
|
|
|
.enumerate()
|
|
|
|
.fold(String::new(), |mut acc, (idx, next)| {
|
|
|
|
if idx != 0 && idx % count == 0 {
|
|
|
|
acc.push(symbol);
|
|
|
|
}
|
|
|
|
acc.push(next);
|
|
|
|
acc
|
|
|
|
});
|
|
|
|
number.chars().rev().collect()
|
|
|
|
}
|
2018-04-01 18:52:21 +10:00
|
|
|
impl Constant {
|
2018-06-24 20:09:37 +10:00
|
|
|
pub fn value(&self) -> Option<ConstVal> {
|
2018-04-01 18:52:21 +10:00
|
|
|
match *self {
|
2018-06-24 20:09:37 +10:00
|
|
|
Constant::Number(n) => Some(ConstVal::U64(n as u64)),
|
2021-07-30 20:01:55 +10:00
|
|
|
Constant::Hex(ref hex) => u64::from_str_radix(hex, 16).ok().map(ConstVal::U64),
|
2021-05-08 20:25:10 +10:00
|
|
|
Constant::BitPos(pos) => Some(ConstVal::U64(1u64 << pos)),
|
2018-04-01 18:52:21 +10:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
pub fn ty(&self) -> CType {
|
|
|
|
match self {
|
|
|
|
Constant::Number(_) | Constant::Hex(_) => CType::USize,
|
|
|
|
Constant::CExpr(expr) => {
|
|
|
|
let (_, (ty, _)) = cexpr(expr).expect("Unable to parse cexpr");
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 20:56:16 +10:00
|
|
|
pub fn from_extension_enum(constant: &vkxml::ExtensionEnum) -> Option<Self> {
|
2018-07-30 16:54:03 +10:00
|
|
|
let number = constant.number.map(Constant::Number);
|
2018-07-11 21:18:22 +10:00
|
|
|
let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone()));
|
2018-07-30 16:54:03 +10:00
|
|
|
let bitpos = constant.bitpos.map(Constant::BitPos);
|
2018-07-11 21:18:22 +10:00
|
|
|
let expr = constant
|
|
|
|
.c_expression
|
|
|
|
.as_ref()
|
|
|
|
.map(|e| Constant::CExpr(e.clone()));
|
2018-07-21 20:56:16 +10:00
|
|
|
number.or(hex).or(bitpos).or(expr)
|
2018-07-11 21:18:22 +10:00
|
|
|
}
|
2018-07-30 16:54:03 +10:00
|
|
|
|
2018-04-01 18:52:21 +10:00
|
|
|
pub fn from_constant(constant: &vkxml::Constant) -> Self {
|
2018-07-30 16:54:03 +10:00
|
|
|
let number = constant.number.map(Constant::Number);
|
2018-04-01 18:52:21 +10:00
|
|
|
let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone()));
|
2018-07-30 16:54:03 +10:00
|
|
|
let bitpos = constant.bitpos.map(Constant::BitPos);
|
2018-04-01 18:52:21 +10:00
|
|
|
let expr = constant
|
|
|
|
.c_expression
|
|
|
|
.as_ref()
|
|
|
|
.map(|e| Constant::CExpr(e.clone()));
|
|
|
|
number.or(hex).or(bitpos).or(expr).expect("")
|
|
|
|
}
|
2021-04-19 05:58:40 +10:00
|
|
|
|
|
|
|
/// Returns (Constant, optional base type, is_alias)
|
|
|
|
pub fn from_vk_parse_enum_spec(
|
|
|
|
spec: &vk_parse::EnumSpec,
|
|
|
|
enum_name: Option<&str>,
|
|
|
|
extension_number: Option<i64>,
|
|
|
|
) -> Option<(Self, Option<String>, bool)> {
|
|
|
|
use vk_parse::EnumSpec;
|
|
|
|
|
|
|
|
match spec {
|
|
|
|
EnumSpec::Bitpos { bitpos, extends } => {
|
|
|
|
Some((Self::BitPos(*bitpos as u32), extends.clone(), false))
|
|
|
|
}
|
|
|
|
EnumSpec::Offset {
|
|
|
|
offset,
|
|
|
|
extends,
|
|
|
|
extnumber,
|
|
|
|
dir: positive,
|
|
|
|
} => {
|
|
|
|
let ext_base = 1_000_000_000;
|
|
|
|
let ext_block_size = 1000;
|
|
|
|
let extnumber = extnumber
|
|
|
|
.or(extension_number)
|
|
|
|
.expect("Need an extension number");
|
|
|
|
let value = ext_base + (extnumber - 1) * ext_block_size + offset;
|
|
|
|
let value = if *positive { value } else { -value };
|
|
|
|
Some((Self::Number(value as i32), Some(extends.clone()), false))
|
|
|
|
}
|
|
|
|
EnumSpec::Value { value, extends } => {
|
|
|
|
let value = value
|
|
|
|
.strip_prefix("0x")
|
|
|
|
.map(|hex| Self::Hex(hex.to_owned()))
|
|
|
|
.or_else(|| value.parse::<i32>().ok().map(Self::Number))?;
|
|
|
|
Some((value, extends.clone(), false))
|
|
|
|
}
|
|
|
|
EnumSpec::Alias { alias, extends } => {
|
|
|
|
let base_type = extends.as_deref().or(enum_name)?;
|
2021-07-30 20:01:55 +10:00
|
|
|
let key = variant_ident(base_type, alias);
|
2021-04-19 05:58:40 +10:00
|
|
|
if key == "DISPATCH_BASE" {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((Self::Alias(key), Some(base_type.to_owned()), true))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
|
|
|
|
2018-03-10 08:43:45 +11:00
|
|
|
pub trait FeatureExt {
|
|
|
|
fn version_string(&self) -> String;
|
2019-10-21 02:18:40 +11:00
|
|
|
fn is_version(&self, major: u32, minor: u32) -> bool;
|
2018-03-10 08:43:45 +11:00
|
|
|
}
|
|
|
|
impl FeatureExt for vkxml::Feature {
|
2019-10-21 02:18:40 +11:00
|
|
|
fn is_version(&self, major: u32, minor: u32) -> bool {
|
|
|
|
let self_major = self.version as u32;
|
2019-10-21 03:11:13 +11:00
|
|
|
let self_minor = (self.version * 10.0) as u32 - self_major * 10;
|
2019-10-21 02:18:40 +11:00
|
|
|
major == self_major && self_minor == minor
|
|
|
|
}
|
2018-03-10 08:43:45 +11:00
|
|
|
fn version_string(&self) -> String {
|
2018-07-07 20:54:31 +10:00
|
|
|
let mut version = format!("{}", self.version);
|
|
|
|
if version.len() == 1 {
|
|
|
|
version = format!("{}_0", version)
|
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2018-03-10 08:43:45 +11:00
|
|
|
version.replace(".", "_")
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 20:54:31 +10:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum FunctionType {
|
|
|
|
Static,
|
|
|
|
Entry,
|
|
|
|
Instance,
|
|
|
|
Device,
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
pub trait CommandExt {
|
|
|
|
/// Returns the ident in snake_case and without the 'vk' prefix.
|
2018-07-07 20:54:31 +10:00
|
|
|
fn function_type(&self) -> FunctionType;
|
2018-03-10 07:45:58 +11:00
|
|
|
///
|
|
|
|
/// Returns true if the command is a device level command. This is indicated by
|
|
|
|
/// the type of the first parameter.
|
|
|
|
fn command_ident(&self) -> Ident;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandExt for vkxml::Command {
|
|
|
|
fn command_ident(&self) -> Ident {
|
2021-05-19 07:30:28 +10:00
|
|
|
format_ident!("{}", self.name.strip_prefix("vk").unwrap().to_snake_case())
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
|
|
|
|
2018-07-07 20:54:31 +10:00
|
|
|
fn function_type(&self) -> FunctionType {
|
2018-11-18 05:05:28 +11:00
|
|
|
let is_first_param_device = self
|
|
|
|
.param
|
2018-07-30 16:54:03 +10:00
|
|
|
.get(0)
|
2020-10-17 15:55:41 +11:00
|
|
|
.map(|field| {
|
|
|
|
matches!(
|
|
|
|
field.basetype.as_str(),
|
|
|
|
"VkDevice" | "VkCommandBuffer" | "VkQueue"
|
|
|
|
)
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.unwrap_or(false);
|
2018-07-07 20:54:31 +10:00
|
|
|
match self.name.as_str() {
|
|
|
|
"vkGetInstanceProcAddr" => FunctionType::Static,
|
|
|
|
"vkCreateInstance"
|
|
|
|
| "vkEnumerateInstanceLayerProperties"
|
2018-08-22 17:24:50 +10:00
|
|
|
| "vkEnumerateInstanceExtensionProperties"
|
|
|
|
| "vkEnumerateInstanceVersion" => FunctionType::Entry,
|
2018-07-07 20:54:31 +10:00
|
|
|
// This is actually not a device level function
|
|
|
|
"vkGetDeviceProcAddr" => FunctionType::Instance,
|
|
|
|
_ => {
|
|
|
|
if is_first_param_device {
|
|
|
|
FunctionType::Device
|
|
|
|
} else {
|
|
|
|
FunctionType::Instance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FieldExt {
|
2020-12-13 06:56:43 +11:00
|
|
|
/// Returns the name of the parameter that doesn't clash with Rusts reserved
|
2018-03-10 07:45:58 +11:00
|
|
|
/// keywords
|
|
|
|
fn param_ident(&self) -> Ident;
|
|
|
|
|
2020-12-13 06:56:43 +11:00
|
|
|
/// The inner type of this field, with one level of pointers removed
|
2021-01-03 00:37:10 +11:00
|
|
|
fn inner_type_tokens(&self) -> TokenStream;
|
2020-12-13 06:56:43 +11:00
|
|
|
|
|
|
|
/// Returns reference-types wrapped in their safe variant. (Dynamic) arrays become
|
|
|
|
/// slices, pointers become Rust references.
|
2021-01-03 00:37:10 +11:00
|
|
|
fn safe_type_tokens(&self, lifetime: TokenStream) -> TokenStream;
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2019-03-14 08:50:04 +11:00
|
|
|
/// Returns the basetype ident and removes the 'Vk' prefix. When `is_ffi_param` is `true`
|
|
|
|
/// array types (e.g. `[f32; 3]`) will be converted to pointer types (e.g. `&[f32; 3]`),
|
|
|
|
/// which is needed for `C` function parameters. Set to `false` for struct definitions.
|
2021-01-03 00:37:10 +11:00
|
|
|
fn type_tokens(&self, is_ffi_param: bool) -> TokenStream;
|
2018-06-24 20:09:37 +10:00
|
|
|
fn is_clone(&self) -> bool;
|
2021-03-01 02:50:24 +11:00
|
|
|
|
|
|
|
/// Whether this is C's `void` type (not to be mistaken with a void _pointer_!)
|
|
|
|
fn is_void(&self) -> bool;
|
2021-09-08 19:26:58 +10:00
|
|
|
|
|
|
|
/// Exceptions for pointers to static-sized arrays,
|
|
|
|
/// `vk.xml` does not annotate this.
|
|
|
|
fn is_pointer_to_static_sized_array(&self) -> bool;
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
pub trait ToTokens {
|
2021-01-03 00:37:10 +11:00
|
|
|
fn to_tokens(&self, is_const: bool) -> TokenStream;
|
2020-12-13 06:56:43 +11:00
|
|
|
/// Returns the topmost pointer as safe reference
|
2021-01-03 00:37:10 +11:00
|
|
|
fn to_safe_tokens(&self, is_const: bool, lifetime: TokenStream) -> TokenStream;
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
impl ToTokens for vkxml::ReferenceType {
|
2021-01-03 00:37:10 +11:00
|
|
|
fn to_tokens(&self, is_const: bool) -> TokenStream {
|
2020-12-13 06:56:43 +11:00
|
|
|
let r = if is_const {
|
|
|
|
quote!(*const)
|
|
|
|
} else {
|
|
|
|
quote!(*mut)
|
2018-06-24 20:09:37 +10:00
|
|
|
};
|
2020-12-13 06:56:43 +11:00
|
|
|
match self {
|
|
|
|
vkxml::ReferenceType::Pointer => quote!(#r),
|
|
|
|
vkxml::ReferenceType::PointerToPointer => quote!(#r *mut),
|
|
|
|
vkxml::ReferenceType::PointerToConstPointer => quote!(#r *const),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
fn to_safe_tokens(&self, is_const: bool, lifetime: TokenStream) -> TokenStream {
|
2020-12-13 06:56:43 +11:00
|
|
|
let r = if is_const {
|
|
|
|
quote!(&#lifetime)
|
|
|
|
} else {
|
|
|
|
quote!(&#lifetime mut)
|
|
|
|
};
|
|
|
|
match self {
|
|
|
|
vkxml::ReferenceType::Pointer => quote!(#r),
|
|
|
|
vkxml::ReferenceType::PointerToPointer => quote!(#r *mut),
|
|
|
|
vkxml::ReferenceType::PointerToConstPointer => quote!(#r *const),
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
fn name_to_tokens(type_name: &str) -> Ident {
|
2018-03-11 02:21:35 +11:00
|
|
|
let new_name = match type_name {
|
2018-08-25 21:09:44 +10:00
|
|
|
"uint8_t" => "u8",
|
|
|
|
"uint16_t" => "u16",
|
|
|
|
"uint32_t" => "u32",
|
|
|
|
"uint64_t" => "u64",
|
|
|
|
"int8_t" => "i8",
|
|
|
|
"int16_t" => "i16",
|
|
|
|
"int32_t" => "i32",
|
|
|
|
"int64_t" => "i64",
|
|
|
|
"size_t" => "usize",
|
2018-06-24 20:09:37 +10:00
|
|
|
"int" => "c_int",
|
2018-03-11 02:21:35 +11:00
|
|
|
"void" => "c_void",
|
|
|
|
"char" => "c_char",
|
2018-09-30 04:21:56 +10:00
|
|
|
"float" => "f32",
|
2020-01-19 19:56:12 +11:00
|
|
|
"double" => "f64",
|
2018-03-11 02:21:35 +11:00
|
|
|
"long" => "c_ulong",
|
2020-12-13 06:56:43 +11:00
|
|
|
_ => type_name.strip_prefix("Vk").unwrap_or(type_name),
|
2018-03-11 02:21:35 +11:00
|
|
|
};
|
2018-06-24 20:09:37 +10:00
|
|
|
let new_name = new_name.replace("FlagBits", "Flags");
|
2021-01-03 00:37:10 +11:00
|
|
|
format_ident!("{}", new_name.as_str())
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-05-30 21:59:18 +10:00
|
|
|
/// Parses and rewrites a C literal into Rust
|
|
|
|
///
|
|
|
|
/// If no special pattern is recognized the original literal is returned.
|
|
|
|
/// Any new conversions need to be added to the [`cexpr()`] [`nom`] parser.
|
|
|
|
///
|
|
|
|
/// Examples:
|
|
|
|
/// - `0x3FFU` -> `0x3ffu32`
|
|
|
|
fn convert_c_literal(lit: Literal) -> Literal {
|
|
|
|
if let Ok((_, (_, rexpr))) = cexpr(&lit.to_string()) {
|
|
|
|
// lit::SynInt uses the same `.parse` method to create hexadecimal
|
|
|
|
// literals because there is no `Literal` constructor for it.
|
|
|
|
let mut stream = rexpr.parse::<TokenStream>().unwrap().into_iter();
|
|
|
|
// If expression rewriting succeeds this should parse into a single literal
|
|
|
|
match (stream.next(), stream.next()) {
|
|
|
|
(Some(TokenTree::Literal(l)), None) => l,
|
|
|
|
x => panic!("Stream must contain a single literal, not {:?}", x),
|
2021-01-03 00:37:10 +11:00
|
|
|
}
|
2021-05-30 21:59:18 +10:00
|
|
|
} else {
|
|
|
|
lit
|
2020-12-13 06:56:43 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse and yield a C expression that is valid to write in Rust
|
|
|
|
/// Identifiers are replaced with their Rust vk equivalent.
|
|
|
|
///
|
|
|
|
/// Examples:
|
2021-05-30 21:59:18 +10:00
|
|
|
/// - `VK_MAKE_VERSION(1, 2, VK_HEADER_VERSION)` -> `make_version(1, 2, HEADER_VERSION)`
|
2020-12-13 06:56:43 +11:00
|
|
|
/// - `2*VK_UUID_SIZE` -> `2 * UUID_SIZE`
|
2021-05-30 21:59:18 +10:00
|
|
|
fn convert_c_expression(c_expr: &str, identifier_renames: &BTreeMap<String, Ident>) -> TokenStream {
|
|
|
|
fn rewrite_token_stream(
|
|
|
|
stream: TokenStream,
|
|
|
|
identifier_renames: &BTreeMap<String, Ident>,
|
|
|
|
) -> TokenStream {
|
2020-12-13 06:56:43 +11:00
|
|
|
stream
|
|
|
|
.into_iter()
|
2021-01-03 00:37:10 +11:00
|
|
|
.map(|tt| match tt {
|
|
|
|
TokenTree::Group(group) => TokenTree::Group(Group::new(
|
|
|
|
group.delimiter(),
|
2021-05-30 21:59:18 +10:00
|
|
|
rewrite_token_stream(group.stream(), identifier_renames),
|
2021-01-03 00:37:10 +11:00
|
|
|
)),
|
2021-05-30 21:59:18 +10:00
|
|
|
TokenTree::Ident(term) => {
|
|
|
|
let name = term.to_string();
|
|
|
|
identifier_renames
|
|
|
|
.get(&name)
|
|
|
|
.cloned()
|
|
|
|
.unwrap_or_else(|| format_ident!("{}", constant_name(&name)))
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
TokenTree::Literal(lit) => TokenTree::Literal(convert_c_literal(lit)),
|
|
|
|
tt => tt,
|
2020-12-13 06:56:43 +11:00
|
|
|
})
|
|
|
|
.collect::<TokenStream>()
|
|
|
|
}
|
2021-05-30 21:59:18 +10:00
|
|
|
let c_expr = c_expr
|
|
|
|
.parse()
|
|
|
|
.unwrap_or_else(|_| panic!("Failed to parse `{}` as Rust", c_expr));
|
|
|
|
rewrite_token_stream(c_expr, identifier_renames)
|
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-05-30 21:59:18 +10:00
|
|
|
fn discard_outmost_delimiter(stream: TokenStream) -> TokenStream {
|
|
|
|
let stream = stream.into_iter().collect_vec();
|
|
|
|
// Discard the delimiter if this stream consists of a single top-most group
|
|
|
|
if let [TokenTree::Group(group)] = stream.as_slice() {
|
|
|
|
TokenTree::Group(Group::new(Delimiter::None, group.stream())).into()
|
|
|
|
} else {
|
|
|
|
stream.into_iter().collect::<TokenStream>()
|
|
|
|
}
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
|
2018-03-10 07:45:58 +11:00
|
|
|
impl FieldExt for vkxml::Field {
|
2018-06-24 20:09:37 +10:00
|
|
|
fn is_clone(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2018-03-10 07:45:58 +11:00
|
|
|
fn param_ident(&self) -> Ident {
|
2020-03-15 10:55:26 +11:00
|
|
|
let name = self.name.as_deref().unwrap_or("field");
|
2018-03-10 07:45:58 +11:00
|
|
|
let name_corrected = match name {
|
|
|
|
"type" => "ty",
|
|
|
|
_ => name,
|
|
|
|
};
|
2021-01-03 00:37:10 +11:00
|
|
|
format_ident!("{}", name_corrected.to_snake_case().as_str())
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
fn inner_type_tokens(&self) -> TokenStream {
|
2021-03-01 02:50:24 +11:00
|
|
|
assert!(!self.is_void());
|
2020-12-13 06:56:43 +11:00
|
|
|
let ty = name_to_tokens(&self.basetype);
|
|
|
|
|
|
|
|
match self.reference {
|
|
|
|
Some(vkxml::ReferenceType::PointerToPointer) => quote!(*mut #ty),
|
|
|
|
Some(vkxml::ReferenceType::PointerToConstPointer) => quote!(*const #ty),
|
|
|
|
_ => quote!(#ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
fn safe_type_tokens(&self, lifetime: TokenStream) -> TokenStream {
|
2021-03-01 02:50:24 +11:00
|
|
|
assert!(!self.is_void());
|
2020-12-13 06:56:43 +11:00
|
|
|
match self.array {
|
|
|
|
// The outer type fn type_tokens() returns is [], which fits our "safe" prescription
|
|
|
|
Some(vkxml::ArrayType::Static) => self.type_tokens(false),
|
|
|
|
Some(vkxml::ArrayType::Dynamic) => {
|
|
|
|
let ty = self.inner_type_tokens();
|
|
|
|
quote!([#ty])
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let ty = name_to_tokens(&self.basetype);
|
|
|
|
let pointer = self
|
|
|
|
.reference
|
|
|
|
.as_ref()
|
|
|
|
.map(|r| r.to_safe_tokens(self.is_const, lifetime));
|
|
|
|
quote!(#pointer #ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
fn type_tokens(&self, is_ffi_param: bool) -> TokenStream {
|
2021-03-01 02:50:24 +11:00
|
|
|
assert!(!self.is_void());
|
2018-06-24 20:09:37 +10:00
|
|
|
let ty = name_to_tokens(&self.basetype);
|
2020-12-13 06:56:43 +11:00
|
|
|
|
|
|
|
match self.array {
|
|
|
|
Some(vkxml::ArrayType::Static) => {
|
|
|
|
assert!(self.reference.is_none());
|
2018-11-18 05:05:28 +11:00
|
|
|
let size = self
|
|
|
|
.size
|
2018-06-24 20:09:37 +10:00
|
|
|
.as_ref()
|
2018-07-30 16:54:03 +10:00
|
|
|
.or_else(|| self.size_enumref.as_ref())
|
2018-06-24 20:09:37 +10:00
|
|
|
.expect("Should have size");
|
2018-08-03 20:34:14 +10:00
|
|
|
// Make sure we also rename the constant, that is
|
2018-08-01 17:22:28 +10:00
|
|
|
// used inside the static array
|
2021-09-08 19:26:58 +10:00
|
|
|
let size = convert_c_expression(size, &BTreeMap::new());
|
2019-03-14 08:50:04 +11:00
|
|
|
// arrays in c are always passed as a pointer
|
|
|
|
if is_ffi_param {
|
2021-01-03 01:08:10 +11:00
|
|
|
quote!(*const [#ty; #size])
|
2019-03-14 08:50:04 +11:00
|
|
|
} else {
|
2020-12-13 06:56:43 +11:00
|
|
|
quote!([#ty; #size])
|
2019-03-14 08:50:04 +11:00
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
_ => {
|
|
|
|
let pointer = self.reference.as_ref().map(|r| r.to_tokens(self.is_const));
|
2021-09-08 19:26:58 +10:00
|
|
|
if self.is_pointer_to_static_sized_array() {
|
|
|
|
let size = self.c_size.as_ref().expect("Should have c_size");
|
|
|
|
let size = convert_c_expression(size, &BTreeMap::new());
|
|
|
|
quote!(#pointer [#ty; #size])
|
|
|
|
} else {
|
|
|
|
quote!(#pointer #ty)
|
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
}
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
2021-03-01 02:50:24 +11:00
|
|
|
|
|
|
|
fn is_void(&self) -> bool {
|
|
|
|
self.basetype == "void" && self.reference.is_none()
|
|
|
|
}
|
2021-09-08 19:26:58 +10:00
|
|
|
|
|
|
|
fn is_pointer_to_static_sized_array(&self) -> bool {
|
|
|
|
matches!(self.array, Some(vkxml::ArrayType::Dynamic))
|
|
|
|
&& self.name.as_deref() == Some("pVersionData")
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
2019-03-15 10:22:52 +11:00
|
|
|
pub type CommandMap<'a> = HashMap<vkxml::Identifier, &'a vkxml::Command>;
|
2018-03-10 20:07:12 +11:00
|
|
|
|
2018-11-17 03:57:08 +11:00
|
|
|
fn generate_function_pointers<'a>(
|
|
|
|
ident: Ident,
|
|
|
|
commands: &[&'a vkxml::Command],
|
2020-03-22 23:56:01 +11:00
|
|
|
aliases: &HashMap<String, String, impl BuildHasher>,
|
2019-10-21 02:18:40 +11:00
|
|
|
fn_cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2020-03-22 23:56:01 +11:00
|
|
|
// Commands can have duplicates inside them because they are declared per features. But we only
|
|
|
|
// really want to generate one function pointer.
|
2021-05-19 07:30:28 +10:00
|
|
|
let commands = commands
|
|
|
|
.iter()
|
|
|
|
.unique_by(|cmd| cmd.name.as_str())
|
|
|
|
.collect::<Vec<_>>();
|
2020-03-22 23:56:01 +11:00
|
|
|
|
2021-05-24 08:13:29 +10:00
|
|
|
struct Command {
|
|
|
|
type_needs_defining: bool,
|
|
|
|
type_name: Ident,
|
|
|
|
function_name_c: String,
|
|
|
|
function_name_rust: Ident,
|
|
|
|
parameter_names: TokenStream,
|
|
|
|
parameters: TokenStream,
|
|
|
|
parameters_unused: TokenStream,
|
|
|
|
returns: TokenStream,
|
|
|
|
}
|
2019-03-14 14:51:22 +11:00
|
|
|
|
2021-05-24 08:13:29 +10:00
|
|
|
let commands = commands
|
2018-03-10 20:07:12 +11:00
|
|
|
.iter()
|
|
|
|
.map(|cmd| {
|
2021-05-24 08:13:29 +10:00
|
|
|
let type_name = format_ident!("PFN_{}", cmd.name);
|
|
|
|
|
|
|
|
let function_name_c = if let Some(alias_name) = aliases.get(&cmd.name) {
|
|
|
|
alias_name.to_string()
|
|
|
|
} else {
|
|
|
|
cmd.name.to_string()
|
|
|
|
};
|
|
|
|
let function_name_rust = format_ident!(
|
|
|
|
"{}",
|
|
|
|
function_name_c
|
|
|
|
.strip_prefix("vk")
|
|
|
|
.unwrap()
|
|
|
|
.to_snake_case()
|
|
|
|
.as_str()
|
|
|
|
);
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let params: Vec<_> = cmd
|
|
|
|
.param
|
2018-03-10 20:07:12 +11:00
|
|
|
.iter()
|
|
|
|
.map(|field| {
|
|
|
|
let name = field.param_ident();
|
2019-03-14 08:50:04 +11:00
|
|
|
let ty = field.type_tokens(true);
|
2018-03-10 20:07:12 +11:00
|
|
|
(name, ty)
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-03-10 20:07:12 +11:00
|
|
|
|
2021-05-24 08:13:29 +10:00
|
|
|
let params_iter = params.iter().map(|(param_name, _)| param_name);
|
|
|
|
let parameter_names = quote!(#(#params_iter,)*);
|
|
|
|
|
|
|
|
let params_iter = params
|
2018-03-10 20:07:12 +11:00
|
|
|
.iter()
|
2021-05-24 08:13:29 +10:00
|
|
|
.map(|(param_name, param_ty)| quote!(#param_name: #param_ty));
|
|
|
|
let parameters = quote!(#(#params_iter,)*);
|
|
|
|
|
|
|
|
let params_iter = params.iter().map(|(param_name, param_ty)| {
|
2021-05-19 07:30:28 +10:00
|
|
|
let unused_name = format_ident!("_{}", param_name);
|
2021-05-24 08:13:29 +10:00
|
|
|
quote!(#unused_name: #param_ty)
|
2018-11-04 09:37:20 +11:00
|
|
|
});
|
2021-05-24 08:13:29 +10:00
|
|
|
let parameters_unused = quote!(#(#params_iter,)*);
|
|
|
|
|
|
|
|
Command {
|
|
|
|
// PFN function pointers are global and can not have duplicates.
|
|
|
|
// This can happen because there are aliases to commands
|
|
|
|
type_needs_defining: fn_cache.insert(cmd.name.as_str()),
|
|
|
|
type_name,
|
|
|
|
function_name_c,
|
|
|
|
function_name_rust,
|
|
|
|
parameter_names,
|
|
|
|
parameters,
|
|
|
|
parameters_unused,
|
|
|
|
returns: if cmd.return_type.is_void() {
|
|
|
|
quote!()
|
|
|
|
} else {
|
|
|
|
let ret_ty_tokens = cmd.return_type.type_tokens(true);
|
|
|
|
quote!(-> #ret_ty_tokens)
|
|
|
|
},
|
2018-11-04 09:37:20 +11:00
|
|
|
}
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
2021-05-24 08:13:29 +10:00
|
|
|
.collect::<Vec<_>>();
|
2018-03-10 20:07:12 +11:00
|
|
|
|
2021-05-24 08:13:29 +10:00
|
|
|
struct CommandToType<'a>(&'a Command);
|
|
|
|
impl<'a> quote::ToTokens for CommandToType<'a> {
|
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
|
|
let type_name = &self.0.type_name;
|
|
|
|
let parameters = &self.0.parameters;
|
|
|
|
let returns = &self.0.returns;
|
|
|
|
quote!(
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type #type_name = unsafe extern "system" fn(#parameters) #returns;
|
|
|
|
)
|
|
|
|
.to_tokens(tokens)
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 03:57:08 +11:00
|
|
|
|
2021-05-24 08:13:29 +10:00
|
|
|
struct CommandToMember<'a>(&'a Command);
|
|
|
|
impl<'a> quote::ToTokens for CommandToMember<'a> {
|
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2021-05-24 08:39:07 +10:00
|
|
|
let type_name = &self.0.type_name;
|
|
|
|
let type_name = if self.0.type_needs_defining {
|
|
|
|
// Type is defined in local scope
|
|
|
|
quote!(#type_name)
|
|
|
|
} else {
|
|
|
|
// Type is usually defined in another module
|
|
|
|
quote!(crate::vk::#type_name)
|
|
|
|
};
|
2021-05-24 08:13:29 +10:00
|
|
|
let function_name_rust = &self.0.function_name_rust;
|
2021-05-24 08:39:07 +10:00
|
|
|
quote!(pub #function_name_rust: #type_name).to_tokens(tokens)
|
2021-05-24 08:13:29 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CommandToLoader<'a>(&'a Command);
|
|
|
|
impl<'a> quote::ToTokens for CommandToLoader<'a> {
|
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
|
|
let function_name_rust = &self.0.function_name_rust;
|
|
|
|
let parameters_unused = &self.0.parameters_unused;
|
|
|
|
let returns = &self.0.returns;
|
|
|
|
|
|
|
|
let byte_function_name =
|
|
|
|
Literal::byte_string(format!("{}\0", self.0.function_name_c).as_bytes());
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#function_name_rust: unsafe {
|
|
|
|
unsafe extern "system" fn #function_name_rust (#parameters_unused) #returns {
|
|
|
|
panic!(concat!("Unable to load ", stringify!(#function_name_rust)))
|
|
|
|
}
|
|
|
|
let cname = ::std::ffi::CStr::from_bytes_with_nul_unchecked(#byte_function_name);
|
|
|
|
let val = _f(cname);
|
|
|
|
if val.is_null() {
|
|
|
|
#function_name_rust
|
|
|
|
} else {
|
|
|
|
::std::mem::transmute(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.to_tokens(tokens)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CommandToBody<'a>(&'a Command);
|
|
|
|
impl<'a> quote::ToTokens for CommandToBody<'a> {
|
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
|
|
let function_name_rust = &self.0.function_name_rust;
|
|
|
|
let parameters = &self.0.parameters;
|
|
|
|
let parameter_names = &self.0.parameter_names;
|
|
|
|
let returns = &self.0.returns;
|
|
|
|
let khronos_link = khronos_link(&self.0.function_name_c);
|
|
|
|
quote!(
|
|
|
|
#[doc = #khronos_link]
|
|
|
|
pub unsafe fn #function_name_rust(&self, #parameters) #returns {
|
|
|
|
(self.#function_name_rust)(#parameter_names)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.to_tokens(tokens)
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 03:57:08 +11:00
|
|
|
|
2021-05-24 08:13:29 +10:00
|
|
|
let pfn_typedefs = commands
|
2018-11-17 03:57:08 +11:00
|
|
|
.iter()
|
2021-05-24 08:13:29 +10:00
|
|
|
.filter(|pfn| pfn.type_needs_defining)
|
|
|
|
.map(|pfn| CommandToType(pfn));
|
|
|
|
let members = commands.iter().map(|pfn| CommandToMember(pfn));
|
|
|
|
let loaders = commands.iter().map(|pfn| CommandToLoader(pfn));
|
|
|
|
let bodies = commands.iter().map(|pfn| CommandToBody(pfn));
|
2018-11-17 03:57:08 +11:00
|
|
|
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2021-05-24 08:13:29 +10:00
|
|
|
#(#pfn_typedefs)*
|
2018-11-17 03:57:08 +11:00
|
|
|
|
2021-05-24 08:13:29 +10:00
|
|
|
#[derive(Clone)]
|
2018-03-10 20:07:12 +11:00
|
|
|
pub struct #ident {
|
2021-05-24 08:13:29 +10:00
|
|
|
#(#members,)*
|
2018-03-10 20:07:12 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for #ident {}
|
|
|
|
unsafe impl Sync for #ident {}
|
|
|
|
|
|
|
|
impl #ident {
|
2018-11-04 09:37:20 +11:00
|
|
|
pub fn load<F>(mut _f: F) -> Self
|
2018-03-10 20:07:12 +11:00
|
|
|
where F: FnMut(&::std::ffi::CStr) -> *const c_void
|
|
|
|
{
|
2018-11-04 09:37:20 +11:00
|
|
|
#ident {
|
2021-05-24 08:13:29 +10:00
|
|
|
#(#loaders,)*
|
2018-03-10 20:07:12 +11:00
|
|
|
}
|
|
|
|
}
|
2021-05-24 08:13:29 +10:00
|
|
|
#(#bodies)*
|
2018-03-10 20:07:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
pub struct ExtensionConstant<'a> {
|
|
|
|
pub name: &'a str,
|
|
|
|
pub constant: Constant,
|
|
|
|
}
|
|
|
|
impl<'a> ConstantExt for ExtensionConstant<'a> {
|
2021-04-19 05:58:40 +10:00
|
|
|
fn constant(&self, _enum_name: &str) -> Constant {
|
2020-05-10 21:42:07 +10:00
|
|
|
self.constant.clone()
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident {
|
|
|
|
variant_ident(enum_name, self.name)
|
|
|
|
}
|
|
|
|
fn notation(&self) -> Option<&str> {
|
|
|
|
None
|
2018-07-21 20:56:16 +10:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
|
|
|
|
pub fn generate_extension_constants<'a>(
|
2018-07-31 03:53:12 +10:00
|
|
|
extension_name: &str,
|
|
|
|
extension_number: i64,
|
2018-11-18 03:27:18 +11:00
|
|
|
extension_items: &'a [vk_parse::ExtensionChild],
|
2019-10-21 02:18:40 +11:00
|
|
|
const_cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-05-08 20:25:10 +10:00
|
|
|
const_values: &mut BTreeMap<Ident, ConstantTypeInfo>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2018-07-31 03:53:12 +10:00
|
|
|
let items = extension_items
|
2018-07-21 20:56:16 +10:00
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
2018-11-18 03:27:18 +11:00
|
|
|
vk_parse::ExtensionChild::Require { items, .. } => Some(items.iter()),
|
2018-07-21 20:56:16 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
2021-07-09 19:55:20 +10:00
|
|
|
.flatten();
|
2018-07-30 06:39:45 +10:00
|
|
|
let enum_tokens = items.filter_map(|item| match item {
|
2021-04-19 05:58:40 +10:00
|
|
|
vk_parse::InterfaceItem::Enum(enum_) => {
|
2021-05-19 07:30:28 +10:00
|
|
|
if !const_cache.insert(enum_.name.as_str()) {
|
2018-07-30 06:39:45 +10:00
|
|
|
return None;
|
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-04-19 05:58:40 +10:00
|
|
|
let (constant, extends, is_alias) =
|
|
|
|
Constant::from_vk_parse_enum_spec(&enum_.spec, None, Some(extension_number))?;
|
2018-07-30 06:39:45 +10:00
|
|
|
let extends = extends?;
|
|
|
|
let ext_constant = ExtensionConstant {
|
2021-04-19 05:58:40 +10:00
|
|
|
name: &enum_.name,
|
2018-07-30 06:39:45 +10:00
|
|
|
constant,
|
|
|
|
};
|
|
|
|
let ident = name_to_tokens(&extends);
|
2018-11-17 03:57:08 +11:00
|
|
|
const_values
|
2019-04-22 09:04:49 +10:00
|
|
|
.get_mut(&ident)
|
|
|
|
.unwrap()
|
2021-05-08 20:25:10 +10:00
|
|
|
.values
|
2020-03-23 02:05:30 +11:00
|
|
|
.push(ConstantMatchInfo {
|
|
|
|
ident: ext_constant.variant_ident(&extends),
|
|
|
|
is_alias,
|
|
|
|
});
|
2018-07-30 16:54:03 +10:00
|
|
|
let impl_block = bitflags_impl_block(ident, &extends, &[&ext_constant]);
|
2018-07-31 03:53:12 +10:00
|
|
|
let doc_string = format!("Generated from '{}'", extension_name);
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-07-30 06:39:45 +10:00
|
|
|
#[doc = #doc_string]
|
|
|
|
#impl_block
|
|
|
|
};
|
2018-07-21 20:56:16 +10:00
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
Some(q)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-30 06:39:45 +10:00
|
|
|
#(#enum_tokens)*
|
2018-07-11 21:18:22 +10:00
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
}
|
2018-11-17 03:57:08 +11:00
|
|
|
pub fn generate_extension_commands<'a>(
|
2018-07-31 03:53:12 +10:00
|
|
|
extension_name: &str,
|
2018-11-18 03:27:18 +11:00
|
|
|
items: &[vk_parse::ExtensionChild],
|
2018-11-17 03:57:08 +11:00
|
|
|
cmd_map: &CommandMap<'a>,
|
2020-03-22 23:56:01 +11:00
|
|
|
cmd_aliases: &HashMap<String, String, impl BuildHasher>,
|
2019-10-21 02:18:40 +11:00
|
|
|
fn_cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2020-03-22 23:56:01 +11:00
|
|
|
let mut commands = Vec::new();
|
|
|
|
let mut aliases = HashMap::new();
|
|
|
|
items
|
2018-03-11 02:21:35 +11:00
|
|
|
.iter()
|
2018-07-30 06:39:45 +10:00
|
|
|
.filter_map(|ext_item| match ext_item {
|
2018-11-18 03:27:18 +11:00
|
|
|
vk_parse::ExtensionChild::Require { items, .. } => {
|
2018-07-30 06:39:45 +10:00
|
|
|
Some(items.iter().filter_map(|item| match item {
|
2020-03-22 23:56:01 +11:00
|
|
|
vk_parse::InterfaceItem::Command { ref name, .. } => Some(name),
|
2018-07-30 06:39:45 +10:00
|
|
|
_ => None,
|
|
|
|
}))
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
2020-03-22 23:56:01 +11:00
|
|
|
.flatten()
|
|
|
|
.for_each(|name| {
|
|
|
|
if let Some(cmd) = cmd_map.get(name).copied() {
|
|
|
|
commands.push(cmd);
|
|
|
|
} else if let Some(cmd) = cmd_aliases
|
|
|
|
.get(name)
|
|
|
|
.and_then(|alias_name| cmd_map.get(alias_name).copied())
|
|
|
|
{
|
|
|
|
aliases.insert(cmd.name.clone(), name.to_string());
|
|
|
|
commands.push(cmd);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-19 07:30:28 +10:00
|
|
|
let ident = format_ident!(
|
|
|
|
"{}Fn",
|
|
|
|
extension_name.to_camel_case().strip_prefix("Vk").unwrap()
|
|
|
|
);
|
2021-01-03 00:37:10 +11:00
|
|
|
let fp = generate_function_pointers(ident.clone(), &commands, &aliases, fn_cache);
|
2019-02-13 01:04:38 +11:00
|
|
|
let byte_name = format!("{}\0", extension_name);
|
|
|
|
|
2020-09-11 04:46:42 +10:00
|
|
|
let spec_version = items
|
|
|
|
.iter()
|
|
|
|
.find_map(|ext_item| match ext_item {
|
|
|
|
vk_parse::ExtensionChild::Require { items, .. } => {
|
|
|
|
items.iter().find_map(|item| match item {
|
|
|
|
vk_parse::InterfaceItem::Enum(ref e) if e.name.contains("SPEC_VERSION") => {
|
|
|
|
Some(e)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.and_then(|e| {
|
|
|
|
if let vk_parse::EnumSpec::Value { value, .. } = &e.spec {
|
|
|
|
let v: u32 = str::parse(value).unwrap();
|
|
|
|
Some(quote!(pub const SPEC_VERSION: u32 = #v;))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
let byte_name_ident = syn::LitByteStr::new(byte_name.as_bytes(), Span::call_site());
|
2019-02-13 01:04:38 +11:00
|
|
|
let extension_cstr = quote! {
|
|
|
|
impl #ident {
|
|
|
|
pub fn name() -> &'static ::std::ffi::CStr {
|
|
|
|
::std::ffi::CStr::from_bytes_with_nul(#byte_name_ident).expect("Wrong extension string")
|
|
|
|
}
|
2020-09-11 04:46:42 +10:00
|
|
|
#spec_version
|
2019-02-13 01:04:38 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
#extension_cstr
|
|
|
|
#fp
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
}
|
|
|
|
pub fn generate_extension<'a>(
|
|
|
|
extension: &'a vk_parse::Extension,
|
2018-11-17 03:57:08 +11:00
|
|
|
cmd_map: &CommandMap<'a>,
|
2019-10-21 02:18:40 +11:00
|
|
|
const_cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-05-08 20:25:10 +10:00
|
|
|
const_values: &mut BTreeMap<Ident, ConstantTypeInfo>,
|
2020-03-22 23:56:01 +11:00
|
|
|
cmd_aliases: &HashMap<String, String, impl BuildHasher>,
|
2019-10-21 02:18:40 +11:00
|
|
|
fn_cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> Option<TokenStream> {
|
2018-07-31 04:23:25 +10:00
|
|
|
// Okay this is a little bit odd. We need to generate all extensions, even disabled ones,
|
|
|
|
// because otherwise some StructureTypes won't get generated. But we don't generate extensions
|
|
|
|
// that are reserved
|
|
|
|
if extension.name.contains("RESERVED") {
|
|
|
|
return None;
|
|
|
|
}
|
2018-07-31 03:53:12 +10:00
|
|
|
let extension_tokens = generate_extension_constants(
|
|
|
|
&extension.name,
|
|
|
|
extension.number.unwrap_or(0),
|
2018-11-18 03:27:18 +11:00
|
|
|
&extension.children,
|
2018-07-31 03:53:12 +10:00
|
|
|
const_cache,
|
2018-08-20 12:40:28 +10:00
|
|
|
const_values,
|
2018-07-31 03:53:12 +10:00
|
|
|
);
|
2020-03-22 23:56:01 +11:00
|
|
|
let fp = generate_extension_commands(
|
|
|
|
&extension.name,
|
|
|
|
&extension.children,
|
|
|
|
cmd_map,
|
|
|
|
cmd_aliases,
|
|
|
|
fn_cache,
|
|
|
|
);
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-07-21 20:56:16 +10:00
|
|
|
#fp
|
2018-07-30 06:39:45 +10:00
|
|
|
#extension_tokens
|
|
|
|
};
|
|
|
|
Some(q)
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
2021-05-30 21:59:18 +10:00
|
|
|
pub fn generate_define(
|
|
|
|
define: &vkxml::Define,
|
|
|
|
identifier_renames: &mut BTreeMap<String, Ident>,
|
|
|
|
) -> TokenStream {
|
2020-09-11 04:46:42 +10:00
|
|
|
let name = constant_name(&define.name);
|
2021-01-03 00:37:10 +11:00
|
|
|
let ident = format_ident!("{}", name);
|
2020-09-11 04:46:42 +10:00
|
|
|
|
2021-05-30 21:59:18 +10:00
|
|
|
if name == "NULL_HANDLE" {
|
2020-09-11 04:46:42 +10:00
|
|
|
quote!()
|
|
|
|
} else if let Some(value) = &define.value {
|
|
|
|
str::parse::<u32>(value).map_or(quote!(), |v| quote!(pub const #ident: u32 = #v;))
|
|
|
|
} else if let Some(c_expr) = &define.c_expression {
|
2021-05-30 21:59:18 +10:00
|
|
|
if define.name.contains(&"VERSION".to_string()) {
|
|
|
|
let link = khronos_link(&define.name);
|
|
|
|
let c_expr = c_expr.trim_start_matches('\\');
|
|
|
|
let c_expr = c_expr.replace("(uint32_t)", "");
|
2021-07-30 20:01:55 +10:00
|
|
|
let c_expr = convert_c_expression(&c_expr, identifier_renames);
|
2021-05-30 21:59:18 +10:00
|
|
|
let c_expr = discard_outmost_delimiter(c_expr);
|
|
|
|
|
|
|
|
let deprecated = define
|
|
|
|
.comment
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|c| c.strip_prefix("DEPRECATED: "))
|
|
|
|
.map(|comment| quote!(#[deprecated = #comment]));
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-05-30 21:59:18 +10:00
|
|
|
let (code, ident) = if define.parameters.is_empty() {
|
|
|
|
(quote!(pub const #ident: u32 = #c_expr;), ident)
|
|
|
|
} else {
|
|
|
|
let params = define
|
|
|
|
.parameters
|
|
|
|
.iter()
|
|
|
|
.map(|param| format_ident!("{}", param))
|
|
|
|
.map(|i| quote!(#i: u32));
|
|
|
|
let ident = format_ident!("{}", name.to_lowercase());
|
|
|
|
(
|
|
|
|
quote!(pub const fn #ident(#(#params),*) -> u32 { #c_expr }),
|
|
|
|
ident,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
identifier_renames.insert(define.name.clone(), ident);
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#deprecated
|
|
|
|
#[doc = #link]
|
|
|
|
#code
|
|
|
|
}
|
2020-09-11 04:46:42 +10:00
|
|
|
} else {
|
|
|
|
quote!()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote!()
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 00:37:10 +11:00
|
|
|
pub fn generate_typedef(typedef: &vkxml::Typedef) -> TokenStream {
|
2020-12-13 06:56:43 +11:00
|
|
|
if typedef.basetype.is_empty() {
|
|
|
|
// Ignore forward declarations
|
|
|
|
quote! {}
|
|
|
|
} else {
|
|
|
|
let typedef_name = name_to_tokens(&typedef.name);
|
|
|
|
let typedef_ty = name_to_tokens(&typedef.basetype);
|
|
|
|
let khronos_link = khronos_link(&typedef.name);
|
|
|
|
quote! {
|
|
|
|
#[doc = #khronos_link]
|
|
|
|
pub type #typedef_name = #typedef_ty;
|
|
|
|
}
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
}
|
2018-11-18 19:29:17 +11:00
|
|
|
pub fn generate_bitmask(
|
|
|
|
bitmask: &vkxml::Bitmask,
|
2019-10-21 02:18:40 +11:00
|
|
|
bitflags_cache: &mut HashSet<Ident, impl BuildHasher>,
|
2021-05-08 20:25:10 +10:00
|
|
|
const_values: &mut BTreeMap<Ident, ConstantTypeInfo>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> Option<TokenStream> {
|
2018-06-04 19:26:14 +10:00
|
|
|
// Workaround for empty bitmask
|
2018-07-30 16:54:03 +10:00
|
|
|
if bitmask.name.is_empty() {
|
2018-06-04 19:26:14 +10:00
|
|
|
return None;
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
// If this enum has constants, then it will generated later in generate_enums.
|
|
|
|
if bitmask.enumref.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-05-19 07:30:28 +10:00
|
|
|
let name = bitmask.name.strip_prefix("Vk").unwrap();
|
2021-01-03 00:37:10 +11:00
|
|
|
let ident = format_ident!("{}", name);
|
2021-05-19 07:30:28 +10:00
|
|
|
if !bitflags_cache.insert(ident.clone()) {
|
2018-11-18 19:29:17 +11:00
|
|
|
return None;
|
|
|
|
};
|
2021-05-08 20:25:10 +10:00
|
|
|
const_values.insert(ident.clone(), Default::default());
|
2019-03-17 03:46:26 +11:00
|
|
|
let khronos_link = khronos_link(&bitmask.name);
|
2021-05-08 20:25:10 +10:00
|
|
|
let type_ = name_to_tokens(&bitmask.basetype);
|
2018-12-07 20:33:36 +11:00
|
|
|
Some(quote! {
|
2018-08-03 20:34:14 +10:00
|
|
|
#[repr(transparent)]
|
2018-07-09 17:23:53 +10:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2019-03-14 14:51:22 +11:00
|
|
|
#[doc = #khronos_link]
|
2021-05-08 20:25:10 +10:00
|
|
|
pub struct #ident(pub(crate) #type_);
|
|
|
|
vk_bitflags_wrapped!(#ident, 0b0, #type_);
|
2018-06-04 19:26:14 +10:00
|
|
|
})
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
2018-07-09 16:49:28 +10:00
|
|
|
|
2018-04-01 18:52:21 +10:00
|
|
|
pub enum EnumType {
|
2021-01-03 00:37:10 +11:00
|
|
|
Bitflags(TokenStream),
|
|
|
|
Enum(TokenStream),
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
|
|
|
|
2021-05-01 19:04:09 +10:00
|
|
|
fn is_enum_variant_with_typo(variant_name: &str) -> bool {
|
|
|
|
// All these names are aliases and make little sense in our
|
|
|
|
// enum structure, they are better omitted entirely.
|
|
|
|
matches!(
|
|
|
|
variant_name,
|
|
|
|
"VK_STENCIL_FRONT_AND_BACK"
|
|
|
|
| "VK_COLORSPACE_SRGB_NONLINEAR"
|
|
|
|
| "VK_QUERY_SCOPE_COMMAND_BUFFER"
|
|
|
|
| "VK_QUERY_SCOPE_RENDER_PASS"
|
|
|
|
| "VK_QUERY_SCOPE_COMMAND"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-08 20:25:10 +10:00
|
|
|
static TRAILING_NUMBER: Lazy<Regex> = Lazy::new(|| Regex::new("(\\d+)$").unwrap());
|
|
|
|
|
2018-07-11 21:18:22 +10:00
|
|
|
pub fn variant_ident(enum_name: &str, variant_name: &str) -> Ident {
|
2021-03-08 02:21:05 +11:00
|
|
|
let variant_name = variant_name.to_uppercase();
|
2018-08-01 16:51:50 +10:00
|
|
|
let _name = enum_name.replace("FlagBits", "");
|
2018-11-21 03:55:06 +11:00
|
|
|
// TODO: Should be read from vk.xml id:2
|
|
|
|
// TODO: Also needs to be more robust, vendor names can be substrings from itself, id:4
|
2018-07-31 21:44:22 +10:00
|
|
|
// like NVX and NV
|
2021-05-01 19:04:09 +10:00
|
|
|
let vendors = [
|
|
|
|
"_NVX", "_KHR", "_EXT", "_NV", "_AMD", "_ANDROID", "_GOOGLE", "_INTEL",
|
|
|
|
];
|
2021-03-06 06:51:11 +11:00
|
|
|
let struct_name = _name.to_shouty_snake_case();
|
2018-08-01 16:51:50 +10:00
|
|
|
let vendor = vendors
|
2019-10-21 02:18:40 +11:00
|
|
|
.iter()
|
2021-03-06 06:51:11 +11:00
|
|
|
.find(|&vendor| struct_name.ends_with(vendor))
|
2018-08-01 16:51:50 +10:00
|
|
|
.cloned()
|
|
|
|
.unwrap_or("");
|
2021-03-06 06:51:11 +11:00
|
|
|
let struct_name = struct_name.strip_suffix(vendor).unwrap();
|
2021-05-08 20:25:10 +10:00
|
|
|
let struct_name = TRAILING_NUMBER.replace(struct_name, "_$1");
|
2021-05-01 19:04:09 +10:00
|
|
|
let variant_name = variant_name
|
|
|
|
.strip_suffix(vendor)
|
|
|
|
.unwrap_or_else(|| variant_name.as_str());
|
|
|
|
|
2021-05-08 20:25:10 +10:00
|
|
|
let new_variant_name = variant_name
|
|
|
|
.strip_prefix(struct_name.as_ref())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
if enum_name == "VkResult" || is_enum_variant_with_typo(variant_name) {
|
|
|
|
variant_name.strip_prefix("VK").unwrap()
|
|
|
|
} else {
|
|
|
|
panic!(
|
|
|
|
"Failed to strip {} prefix from enum variant {}",
|
|
|
|
struct_name, variant_name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
});
|
2021-05-01 19:04:09 +10:00
|
|
|
|
2021-03-08 02:21:05 +11:00
|
|
|
// Both of the above strip_prefix leave a leading `_`:
|
2021-07-30 20:01:55 +10:00
|
|
|
let new_variant_name = new_variant_name.strip_prefix('_').unwrap();
|
2021-03-08 02:21:05 +11:00
|
|
|
// Replace _BIT anywhere in the string, also works when there's a trailing
|
|
|
|
// vendor extension in the variant name that's not in the enum/type name:
|
|
|
|
let new_variant_name = new_variant_name.replace("_BIT", "");
|
2018-07-09 17:23:53 +10:00
|
|
|
let is_digit = new_variant_name
|
|
|
|
.chars()
|
2020-03-15 10:55:26 +11:00
|
|
|
.next()
|
2018-07-09 17:23:53 +10:00
|
|
|
.map(|c| c.is_digit(10))
|
|
|
|
.unwrap_or(false);
|
|
|
|
if is_digit {
|
2021-05-19 07:30:28 +10:00
|
|
|
format_ident!("TYPE_{}", new_variant_name)
|
2018-07-09 17:23:53 +10:00
|
|
|
} else {
|
2021-01-03 00:37:10 +11:00
|
|
|
format_ident!("{}", new_variant_name)
|
2018-07-09 17:23:53 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 21:18:22 +10:00
|
|
|
pub fn bitflags_impl_block(
|
2018-07-30 16:54:03 +10:00
|
|
|
ident: Ident,
|
2018-07-30 06:39:45 +10:00
|
|
|
enum_name: &str,
|
2018-07-21 20:56:16 +10:00
|
|
|
constants: &[&impl ConstantExt],
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2018-07-11 21:18:22 +10:00
|
|
|
let variants = constants
|
|
|
|
.iter()
|
|
|
|
.map(|constant| {
|
2018-07-30 06:39:45 +10:00
|
|
|
let variant_ident = constant.variant_ident(enum_name);
|
2021-04-19 05:58:40 +10:00
|
|
|
let constant = constant.constant(enum_name);
|
2021-03-27 06:13:16 +11:00
|
|
|
let tokens = if let Constant::Alias(_) = &constant {
|
2020-12-13 06:56:43 +11:00
|
|
|
quote!(#constant)
|
2020-05-10 21:42:07 +10:00
|
|
|
} else {
|
2020-12-13 06:56:43 +11:00
|
|
|
quote!(Self(#constant))
|
2020-05-10 21:42:07 +10:00
|
|
|
};
|
2018-07-11 21:18:22 +10:00
|
|
|
(variant_ident, tokens)
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect_vec();
|
2018-07-11 21:18:22 +10:00
|
|
|
|
|
|
|
let notations = constants.iter().map(|constant| {
|
|
|
|
constant.notation().map(|n| {
|
2021-04-19 05:58:40 +10:00
|
|
|
if n.to_lowercase().contains("backwards") {
|
|
|
|
quote!(#[deprecated = #n])
|
|
|
|
} else {
|
|
|
|
quote!(#[doc = #n])
|
2018-07-11 21:18:22 +10:00
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let variants =
|
|
|
|
variants
|
|
|
|
.iter()
|
|
|
|
.zip(notations.clone())
|
|
|
|
.map(|((variant_ident, value), ref notation)| {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-18 05:05:28 +11:00
|
|
|
#notation
|
2020-03-23 02:05:30 +11:00
|
|
|
pub const #variant_ident: Self = #value;
|
2018-11-18 05:05:28 +11:00
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-11 21:18:22 +10:00
|
|
|
impl #ident {
|
|
|
|
#(#variants)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
pub fn generate_enum<'a>(
|
2021-04-19 05:58:40 +10:00
|
|
|
enum_: &'a vk_parse::Enums,
|
2019-10-21 02:18:40 +11:00
|
|
|
const_cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-05-08 20:25:10 +10:00
|
|
|
const_values: &mut BTreeMap<Ident, ConstantTypeInfo>,
|
2019-10-21 02:18:40 +11:00
|
|
|
bitflags_cache: &mut HashSet<Ident, impl BuildHasher>,
|
2018-07-07 22:49:17 +10:00
|
|
|
) -> EnumType {
|
2021-04-19 05:58:40 +10:00
|
|
|
let name = enum_.name.as_ref().unwrap();
|
|
|
|
let clean_name = name.strip_prefix("Vk").unwrap();
|
|
|
|
let _name = clean_name.replace("FlagBits", "Flags");
|
2021-01-03 00:37:10 +11:00
|
|
|
let ident = format_ident!("{}", _name.as_str());
|
2021-04-19 05:58:40 +10:00
|
|
|
let constants = enum_
|
|
|
|
.children
|
2018-07-09 17:23:53 +10:00
|
|
|
.iter()
|
2018-07-11 21:18:22 +10:00
|
|
|
.filter_map(|elem| match *elem {
|
2021-04-19 05:58:40 +10:00
|
|
|
vk_parse::EnumsChild::Enum(ref constant) => Some(constant),
|
2018-07-11 21:18:22 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
2021-04-19 05:58:40 +10:00
|
|
|
.filter(|constant| match &constant.spec {
|
|
|
|
vk_parse::EnumSpec::Alias { alias, .. } => {
|
|
|
|
// Remove any alias whose name is identical after name de-mangling. For example
|
|
|
|
// the XML contains compatibility aliases for variants without _BIT postfix
|
|
|
|
// which are removed by the generator anyway, after which they become identical.
|
|
|
|
let alias_name = constant.variant_ident(name);
|
|
|
|
let aliases_to = variant_ident(name, alias);
|
|
|
|
alias_name != aliases_to
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
})
|
2018-12-07 20:33:36 +11:00
|
|
|
.collect_vec();
|
2021-04-19 05:58:40 +10:00
|
|
|
|
2019-04-22 09:04:49 +10:00
|
|
|
let mut values = Vec::with_capacity(constants.len());
|
2018-07-30 06:39:45 +10:00
|
|
|
for constant in &constants {
|
|
|
|
const_cache.insert(constant.name.as_str());
|
2020-03-23 02:05:30 +11:00
|
|
|
values.push(ConstantMatchInfo {
|
2021-04-19 05:58:40 +10:00
|
|
|
ident: constant.variant_ident(name),
|
|
|
|
is_alias: constant.is_alias(),
|
2020-03-23 02:05:30 +11:00
|
|
|
});
|
2018-07-30 06:39:45 +10:00
|
|
|
}
|
2021-05-08 20:25:10 +10:00
|
|
|
const_values.insert(
|
|
|
|
ident.clone(),
|
|
|
|
ConstantTypeInfo {
|
|
|
|
values,
|
|
|
|
bitwidth: enum_.bitwidth,
|
|
|
|
},
|
|
|
|
);
|
2018-07-11 21:18:22 +10:00
|
|
|
|
2021-04-19 05:58:40 +10:00
|
|
|
let khronos_link = khronos_link(name);
|
2019-03-14 14:51:22 +11:00
|
|
|
|
2021-04-19 05:58:40 +10:00
|
|
|
if clean_name.contains("Bit") {
|
2021-01-03 00:37:10 +11:00
|
|
|
let ident = format_ident!("{}", _name.as_str());
|
2018-07-11 21:18:22 +10:00
|
|
|
let all_bits = constants
|
2018-06-06 00:47:21 +10:00
|
|
|
.iter()
|
2021-04-19 05:58:40 +10:00
|
|
|
.filter_map(|constant| constant.constant(name).value())
|
2018-06-24 20:09:37 +10:00
|
|
|
.fold(0, |acc, next| acc | next.bits());
|
2021-05-08 20:25:10 +10:00
|
|
|
|
|
|
|
let type_ = if enum_.bitwidth == Some(64u32) {
|
|
|
|
quote!(Flags64)
|
|
|
|
} else {
|
|
|
|
quote!(Flags)
|
|
|
|
};
|
2019-10-21 02:18:40 +11:00
|
|
|
let bit_string = format!("{:b}", all_bits);
|
|
|
|
let bit_string = interleave_number('_', 4, &bit_string);
|
2021-01-03 00:37:10 +11:00
|
|
|
let all_bits_term = syn::LitInt::new(&format!("0b{}", bit_string), Span::call_site());
|
2018-06-06 00:47:21 +10:00
|
|
|
|
2021-05-19 07:30:28 +10:00
|
|
|
if !bitflags_cache.insert(ident.clone()) {
|
2018-12-07 20:33:36 +11:00
|
|
|
EnumType::Bitflags(quote! {})
|
2018-11-18 19:29:17 +11:00
|
|
|
} else {
|
2021-04-19 05:58:40 +10:00
|
|
|
let impl_bitflags = bitflags_impl_block(ident.clone(), name, &constants);
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-11-18 19:29:17 +11:00
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2019-03-14 14:51:22 +11:00
|
|
|
#[doc = #khronos_link]
|
2021-05-08 20:25:10 +10:00
|
|
|
pub struct #ident(pub(crate) #type_);
|
|
|
|
vk_bitflags_wrapped!(#ident, #all_bits_term, #type_);
|
2018-11-18 19:29:17 +11:00
|
|
|
#impl_bitflags
|
|
|
|
};
|
|
|
|
EnumType::Bitflags(q)
|
|
|
|
}
|
2018-04-01 18:52:21 +10:00
|
|
|
} else {
|
2020-12-13 06:56:43 +11:00
|
|
|
let (struct_attribute, special_quote) = match _name.as_str() {
|
|
|
|
//"StructureType" => generate_structure_type(&_name, _enum, create_info_constants),
|
2021-04-19 05:58:40 +10:00
|
|
|
"Result" => (quote!(#[must_use]), generate_result(ident.clone(), enum_)),
|
2020-12-13 06:56:43 +11:00
|
|
|
_ => (quote!(), quote!()),
|
|
|
|
};
|
|
|
|
|
2021-04-19 05:58:40 +10:00
|
|
|
let impl_block = bitflags_impl_block(ident.clone(), name, &constants);
|
2018-12-07 20:33:36 +11:00
|
|
|
let enum_quote = quote! {
|
2019-04-22 09:04:49 +10:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
2018-08-03 20:34:14 +10:00
|
|
|
#[repr(transparent)]
|
2019-03-14 14:51:22 +11:00
|
|
|
#[doc = #khronos_link]
|
2020-12-13 06:56:43 +11:00
|
|
|
#struct_attribute
|
2018-07-30 06:39:45 +10:00
|
|
|
pub struct #ident(pub(crate) i32);
|
2018-08-21 16:56:33 +10:00
|
|
|
impl #ident {
|
2020-04-12 04:24:41 +10:00
|
|
|
pub const fn from_raw(x: i32) -> Self { #ident(x) }
|
|
|
|
pub const fn as_raw(self) -> i32 { self.0 }
|
2018-08-21 16:56:33 +10:00
|
|
|
}
|
2018-07-11 21:18:22 +10:00
|
|
|
#impl_block
|
2018-07-09 17:23:53 +10:00
|
|
|
};
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-07-09 17:23:53 +10:00
|
|
|
#enum_quote
|
|
|
|
#special_quote
|
|
|
|
|
|
|
|
};
|
2018-04-01 18:52:21 +10:00
|
|
|
EnumType::Enum(q)
|
|
|
|
}
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
2018-07-09 16:49:28 +10:00
|
|
|
|
2021-04-19 05:58:40 +10:00
|
|
|
pub fn generate_result(ident: Ident, enum_: &vk_parse::Enums) -> TokenStream {
|
|
|
|
let notation = enum_.children.iter().filter_map(|elem| {
|
2019-03-21 12:19:49 +11:00
|
|
|
let (variant_name, notation) = match *elem {
|
2021-04-19 05:58:40 +10:00
|
|
|
vk_parse::EnumsChild::Enum(ref constant) => (
|
2019-03-21 12:19:49 +11:00
|
|
|
constant.name.as_str(),
|
2021-04-19 05:58:40 +10:00
|
|
|
constant.comment.as_deref().unwrap_or(""),
|
2019-03-21 12:19:49 +11:00
|
|
|
),
|
2019-03-16 09:41:33 +11:00
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-30 20:01:55 +10:00
|
|
|
let variant_ident = variant_ident(enum_.name.as_ref().unwrap(), variant_name);
|
2019-03-16 09:41:33 +11:00
|
|
|
Some(quote! {
|
2019-03-21 12:19:49 +11:00
|
|
|
#ident::#variant_ident => Some(#notation)
|
2019-03-16 09:41:33 +11:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2021-04-28 02:54:29 +10:00
|
|
|
impl ::std::error::Error for #ident {}
|
2018-08-20 12:40:28 +10:00
|
|
|
impl fmt::Display for #ident {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let name = match *self {
|
2021-04-28 02:54:29 +10:00
|
|
|
#(#notation),*,
|
2018-08-20 12:40:28 +10:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(x) = name {
|
|
|
|
fmt.write_str(x)
|
|
|
|
} else {
|
2021-04-28 00:48:15 +10:00
|
|
|
// If we don't have a nice message to show, call the generated `Debug` impl
|
|
|
|
// which includes *all* enum variants, including those from extensions.
|
2021-07-30 20:01:55 +10:00
|
|
|
<Self as fmt::Debug>::fmt(self, fmt)
|
2018-07-07 18:43:05 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
|
|
|
|
fn is_static_array(field: &vkxml::Field) -> bool {
|
|
|
|
field
|
|
|
|
.array
|
|
|
|
.as_ref()
|
2020-10-17 15:55:41 +11:00
|
|
|
.map(|ty| matches!(ty, vkxml::ArrayType::Static))
|
2018-12-07 20:33:36 +11:00
|
|
|
.unwrap_or(false)
|
2018-07-30 20:50:51 +10:00
|
|
|
}
|
2021-01-03 00:37:10 +11:00
|
|
|
pub fn derive_default(_struct: &vkxml::Struct) -> Option<TokenStream> {
|
2018-07-31 03:53:12 +10:00
|
|
|
let name = name_to_tokens(&_struct.name);
|
|
|
|
let members = _struct.elements.iter().filter_map(|elem| match *elem {
|
|
|
|
vkxml::StructElement::Member(ref field) => Some(field),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
let is_structure_type = |field: &vkxml::Field| field.basetype == "VkStructureType";
|
|
|
|
|
2018-07-31 04:23:25 +10:00
|
|
|
// This are also pointers, and therefor also don't implement Default. The spec
|
|
|
|
// also doesn't mark them as pointers
|
2019-10-21 03:11:13 +11:00
|
|
|
let handles = ["LPCWSTR", "HANDLE", "HINSTANCE", "HWND", "HMONITOR"];
|
2018-07-31 03:53:12 +10:00
|
|
|
let contains_ptr = members.clone().any(|field| field.reference.is_some());
|
2020-12-13 06:56:43 +11:00
|
|
|
let contains_structure_type = members.clone().any(is_structure_type);
|
2018-07-31 03:53:12 +10:00
|
|
|
let contains_static_array = members.clone().any(is_static_array);
|
2020-12-13 06:56:43 +11:00
|
|
|
if !(contains_ptr || contains_structure_type || contains_static_array) {
|
2018-07-31 03:53:12 +10:00
|
|
|
return None;
|
|
|
|
};
|
|
|
|
let default_fields = members.clone().map(|field| {
|
|
|
|
let param_ident = field.param_ident();
|
|
|
|
if is_structure_type(field) {
|
|
|
|
let ty = field
|
|
|
|
.type_enums
|
|
|
|
.as_ref()
|
2020-03-15 10:55:26 +11:00
|
|
|
.and_then(|ty| ty.split(',').next());
|
2018-07-31 03:53:12 +10:00
|
|
|
if let Some(variant) = ty {
|
|
|
|
let variant_ident = variant_ident("VkStructureType", variant);
|
|
|
|
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#param_ident: StructureType::#variant_ident
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#param_ident: unsafe { ::std::mem::zeroed() }
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 06:53:17 +10:00
|
|
|
} else if let Some(ref reference) = field.reference {
|
|
|
|
match reference {
|
|
|
|
vkxml::ReferenceType::Pointer => {
|
|
|
|
if field.is_const {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null()
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vkxml::ReferenceType::PointerToPointer => {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vkxml::ReferenceType::PointerToConstPointer => {
|
|
|
|
if field.is_const {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null()
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 03:57:08 +11:00
|
|
|
} else if is_static_array(field) || handles.contains(&field.basetype.as_str()) {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#param_ident: unsafe { ::std::mem::zeroed() }
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-14 08:50:04 +11:00
|
|
|
let ty = field.type_tokens(false);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#param_ident: #ty::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
impl ::std::default::Default for #name {
|
|
|
|
fn default() -> #name {
|
|
|
|
#name {
|
|
|
|
#(
|
|
|
|
#default_fields
|
|
|
|
),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(q)
|
|
|
|
}
|
2019-10-21 02:18:40 +11:00
|
|
|
pub fn derive_debug(
|
|
|
|
_struct: &vkxml::Struct,
|
|
|
|
union_types: &HashSet<&str, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> Option<TokenStream> {
|
2018-07-30 20:50:51 +10:00
|
|
|
let name = name_to_tokens(&_struct.name);
|
2018-07-07 20:54:31 +10:00
|
|
|
let members = _struct.elements.iter().filter_map(|elem| match *elem {
|
|
|
|
vkxml::StructElement::Member(ref field) => Some(field),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
let contains_pfn = members.clone().any(|field| {
|
|
|
|
field
|
|
|
|
.name
|
|
|
|
.as_ref()
|
|
|
|
.map(|n| n.contains("pfn"))
|
|
|
|
.unwrap_or(false)
|
|
|
|
});
|
2018-11-17 03:57:08 +11:00
|
|
|
let contains_static_array = members
|
|
|
|
.clone()
|
|
|
|
.any(|x| is_static_array(x) && x.basetype == "char");
|
2018-07-30 20:50:51 +10:00
|
|
|
let contains_union = members
|
|
|
|
.clone()
|
|
|
|
.any(|field| union_types.contains(field.basetype.as_str()));
|
|
|
|
if !(contains_union || contains_static_array || contains_pfn) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let debug_fields = members.clone().map(|field| {
|
|
|
|
let param_ident = field.param_ident();
|
2021-01-03 00:37:10 +11:00
|
|
|
let param_str = param_ident.to_string();
|
2018-10-08 04:17:48 +11:00
|
|
|
let debug_value = if is_static_array(field) && field.basetype == "char" {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-30 20:50:51 +10:00
|
|
|
&unsafe {
|
2019-01-30 06:07:12 +11:00
|
|
|
::std::ffi::CStr::from_ptr(self.#param_ident.as_ptr() as *const c_char)
|
2018-07-30 20:50:51 +10:00
|
|
|
}
|
|
|
|
}
|
2021-01-03 00:37:10 +11:00
|
|
|
} else if param_str.contains("pfn") {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-10-08 04:09:29 +11:00
|
|
|
&(self.#param_ident.map(|x| x as *const ()))
|
2018-07-30 20:50:51 +10:00
|
|
|
}
|
|
|
|
} else if union_types.contains(field.basetype.as_str()) {
|
|
|
|
quote!(&"union")
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-30 20:50:51 +10:00
|
|
|
&self.#param_ident
|
|
|
|
}
|
|
|
|
};
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-30 20:50:51 +10:00
|
|
|
.field(#param_str, #debug_value)
|
2018-07-07 20:54:31 +10:00
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
});
|
2021-01-03 00:37:10 +11:00
|
|
|
let name_str = name.to_string();
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-09-17 05:15:01 +10:00
|
|
|
impl fmt::Debug for #name {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2018-07-30 20:50:51 +10:00
|
|
|
fmt.debug_struct(#name_str)
|
|
|
|
#(#debug_fields)*
|
|
|
|
.finish()
|
|
|
|
}
|
2018-07-07 20:54:31 +10:00
|
|
|
}
|
|
|
|
};
|
2018-07-30 20:50:51 +10:00
|
|
|
Some(q)
|
|
|
|
}
|
2018-08-19 18:10:11 +10:00
|
|
|
|
2019-02-27 06:54:48 +11:00
|
|
|
pub fn derive_setters(
|
2021-09-08 19:26:58 +10:00
|
|
|
struct_: &vkxml::Struct,
|
2021-06-17 07:44:51 +10:00
|
|
|
root_structs: &HashSet<Ident, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> Option<TokenStream> {
|
2021-09-08 19:26:58 +10:00
|
|
|
if &struct_.name == "VkBaseInStructure"
|
|
|
|
|| &struct_.name == "VkBaseOutStructure"
|
|
|
|
|| &struct_.name == "VkTransformMatrixKHR"
|
|
|
|
|| &struct_.name == "VkAccelerationStructureInstanceKHR"
|
2020-03-23 02:05:30 +11:00
|
|
|
{
|
2018-11-19 00:50:37 +11:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
let name = name_to_tokens(&struct_.name);
|
|
|
|
let name_builder = name_to_tokens(&(struct_.name.clone() + "Builder"));
|
2018-10-20 07:16:35 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
let members = struct_.elements.iter().filter_map(|elem| match *elem {
|
2018-10-20 07:16:35 +11:00
|
|
|
vkxml::StructElement::Member(ref field) => Some(field),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
let has_next = members.clone().any(|field| field.param_ident() == "p_next");
|
2018-11-19 00:50:37 +11:00
|
|
|
|
2018-11-13 09:22:16 +11:00
|
|
|
let nofilter_count_members = [
|
|
|
|
"VkPipelineViewportStateCreateInfo.pViewports",
|
|
|
|
"VkPipelineViewportStateCreateInfo.pScissors",
|
|
|
|
"VkDescriptorSetLayoutBinding.pImmutableSamplers",
|
|
|
|
];
|
2018-11-17 03:57:08 +11:00
|
|
|
let filter_members: Vec<String> = members
|
|
|
|
.clone()
|
|
|
|
.filter_map(|field| {
|
|
|
|
let field_name = field.name.as_ref().unwrap();
|
|
|
|
|
|
|
|
// Associated _count members
|
|
|
|
if field.array.is_some() {
|
|
|
|
if let Some(ref array_size) = field.size {
|
2021-09-08 19:26:58 +10:00
|
|
|
if !nofilter_count_members
|
|
|
|
.iter()
|
|
|
|
.any(|&n| n == (struct_.name.clone() + "." + field_name))
|
2018-11-17 03:57:08 +11:00
|
|
|
{
|
|
|
|
return Some((*array_size).clone());
|
|
|
|
}
|
2018-10-24 08:37:28 +11:00
|
|
|
}
|
|
|
|
}
|
2018-10-25 01:00:16 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
// VkShaderModuleCreateInfo requires a custom setter
|
2018-11-17 03:57:08 +11:00
|
|
|
if field_name == "codeSize" {
|
|
|
|
return Some(field_name.clone());
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
|
2018-11-17 03:57:08 +11:00
|
|
|
None
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-10-24 08:37:28 +11:00
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
let setters = members.clone().filter_map(|field| {
|
|
|
|
let param_ident = field.param_ident();
|
2020-12-13 06:56:43 +11:00
|
|
|
let param_ty_tokens = field.safe_type_tokens(quote!('a));
|
2018-10-20 07:16:35 +11:00
|
|
|
|
|
|
|
let param_ident_string = param_ident.to_string();
|
|
|
|
if param_ident_string == "s_type" || param_ident_string == "p_next" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-12-13 06:56:43 +11:00
|
|
|
let param_ident_short = param_ident_string
|
|
|
|
.strip_prefix("p_")
|
|
|
|
.or_else(|| param_ident_string.strip_prefix("pp_"))
|
|
|
|
.unwrap_or_else(|| param_ident_string.as_str());
|
2021-01-03 00:37:10 +11:00
|
|
|
let param_ident_short = format_ident!("{}", ¶m_ident_short);
|
2018-10-20 07:16:35 +11:00
|
|
|
|
2018-11-13 09:22:16 +11:00
|
|
|
if let Some(name) = field.name.as_ref() {
|
2021-09-08 19:26:58 +10:00
|
|
|
// Filter
|
2018-10-25 01:00:16 +11:00
|
|
|
if filter_members.iter().any(|n| *n == *name) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unique cases
|
|
|
|
if name == "pCode" {
|
|
|
|
return Some(quote!{
|
2021-04-18 06:25:06 +10:00
|
|
|
pub fn code(mut self, code: &'a [u32]) -> Self {
|
2020-12-14 04:30:42 +11:00
|
|
|
self.inner.code_size = code.len() * 4;
|
|
|
|
self.inner.p_code = code.as_ptr() as *const u32;
|
|
|
|
self
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "pSampleMask" {
|
|
|
|
return Some(quote!{
|
2021-05-12 05:38:09 +10:00
|
|
|
/// Sets `p_sample_mask` to `null` if the slice is empty. The mask will
|
|
|
|
/// be treated as if it has all bits set to `1`.
|
|
|
|
///
|
|
|
|
/// See <https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkPipelineMultisampleStateCreateInfo.html#_description>
|
|
|
|
/// for more details.
|
2021-04-18 06:25:06 +10:00
|
|
|
pub fn sample_mask(mut self, sample_mask: &'a [SampleMask]) -> Self {
|
2021-05-12 05:38:09 +10:00
|
|
|
self.inner.p_sample_mask = if sample_mask.is_empty() {
|
|
|
|
std::ptr::null()
|
|
|
|
} else {
|
|
|
|
sample_mask.as_ptr() as *const SampleMask
|
|
|
|
};
|
2020-12-14 04:30:42 +11:00
|
|
|
self
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "ppGeometries" {
|
|
|
|
return Some(quote!{
|
2021-04-25 17:58:12 +10:00
|
|
|
pub fn geometries_ptrs(mut self, geometries: &'a [&'a AccelerationStructureGeometryKHR]) -> Self {
|
2020-12-14 04:30:42 +11:00
|
|
|
self.inner.geometry_count = geometries.len() as _;
|
2021-04-25 17:58:12 +10:00
|
|
|
self.inner.pp_geometries = geometries.as_ptr() as *const *const _;
|
2020-12-14 04:30:42 +11:00
|
|
|
self
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
});
|
2018-10-25 01:00:16 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 03:55:06 +11:00
|
|
|
// TODO: Improve in future when https://github.com/rust-lang/rust/issues/53667 is merged id:6
|
2020-12-13 06:56:43 +11:00
|
|
|
if field.reference.is_some() {
|
|
|
|
if field.basetype == "char" && matches!(field.reference, Some(vkxml::ReferenceType::Pointer)) {
|
|
|
|
assert!(field.null_terminate);
|
|
|
|
assert_eq!(field.size, None);
|
2018-10-20 07:16:35 +11:00
|
|
|
return Some(quote!{
|
2021-04-18 06:25:06 +10:00
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: &'a ::std::ffi::CStr) -> Self {
|
2020-12-13 06:56:43 +11:00
|
|
|
self.inner.#param_ident = #param_ident_short.as_ptr();
|
|
|
|
self
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
});
|
2018-10-20 07:16:35 +11:00
|
|
|
}
|
|
|
|
|
2020-12-13 06:56:43 +11:00
|
|
|
if matches!(field.array, Some(vkxml::ArrayType::Dynamic)) {
|
2018-10-20 07:16:35 +11:00
|
|
|
if let Some(ref array_size) = field.size {
|
2021-09-08 19:26:58 +10:00
|
|
|
let mut slice_param_ty_tokens = field.safe_type_tokens(quote!('a));
|
2018-10-20 07:16:35 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
let mut ptr = if field.is_const {
|
|
|
|
quote!(.as_ptr())
|
|
|
|
} else {
|
|
|
|
quote!(.as_mut_ptr())
|
|
|
|
};
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
// Interpret void array as byte array
|
|
|
|
if field.basetype == "void" && matches!(field.reference, Some(vkxml::ReferenceType::Pointer)) {
|
|
|
|
let mutable = if field.is_const { quote!(const) } else { quote!(mut) };
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
slice_param_ty_tokens = quote!([u8]);
|
|
|
|
ptr = quote!(#ptr as *#mutable c_void);
|
|
|
|
};
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
let set_size_stmt = if field.is_pointer_to_static_sized_array() {
|
|
|
|
// this is a pointer to a piece of memory with statically known size.
|
|
|
|
let array_size = field.c_size.as_ref().unwrap();
|
|
|
|
let c_size = convert_c_expression(array_size, &BTreeMap::new());
|
|
|
|
let inner_type = field.inner_type_tokens();
|
|
|
|
let mutable = if field.is_const { quote!(const) } else { quote!(mut) };
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
slice_param_ty_tokens = quote!([#inner_type; #c_size]);
|
|
|
|
ptr = quote!(as *#mutable #slice_param_ty_tokens);
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
quote!()
|
|
|
|
} else {
|
|
|
|
let array_size_ident = format_ident!("{}", array_size.to_snake_case().as_str());
|
|
|
|
quote!(self.inner.#array_size_ident = #param_ident_short.len() as _;)
|
|
|
|
};
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
let mutable = if field.is_const { quote!() } else { quote!(mut) };
|
2020-12-13 06:56:43 +11:00
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
return Some(quote! {
|
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: &'a #mutable #slice_param_ty_tokens) -> Self {
|
|
|
|
#set_size_stmt
|
|
|
|
self.inner.#param_ident = #param_ident_short#ptr;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
});
|
2018-10-20 07:16:35 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 06:56:43 +11:00
|
|
|
if field.basetype == "VkBool32" {
|
2018-11-13 09:22:16 +11:00
|
|
|
return Some(quote!{
|
2021-04-18 06:25:06 +10:00
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: bool) -> Self {
|
2018-11-13 09:22:16 +11:00
|
|
|
self.inner.#param_ident = #param_ident_short.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-13 06:56:43 +11:00
|
|
|
let param_ty_tokens = if is_opaque_type(&field.basetype) {
|
|
|
|
// Use raw pointers for void/opaque types
|
|
|
|
field.type_tokens(false)
|
|
|
|
} else {
|
|
|
|
param_ty_tokens
|
|
|
|
};
|
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
Some(quote!{
|
2021-04-18 06:25:06 +10:00
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: #param_ty_tokens) -> Self {
|
2018-10-20 07:16:35 +11:00
|
|
|
self.inner.#param_ident = #param_ident_short;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-06-06 19:00:29 +10:00
|
|
|
let extends_name = format_ident!("Extends{}", name);
|
2018-11-19 00:50:37 +11:00
|
|
|
|
2021-06-17 07:44:51 +10:00
|
|
|
let is_root_struct = has_next && root_structs.contains(&name);
|
2021-06-06 19:00:29 +10:00
|
|
|
|
2019-03-10 03:34:10 +11:00
|
|
|
// We only implement a next methods for root structs with a `pnext` field.
|
2021-06-06 19:00:29 +10:00
|
|
|
let next_function = if is_root_struct {
|
2019-02-03 22:48:05 +11:00
|
|
|
quote! {
|
2019-02-27 21:10:38 +11:00
|
|
|
/// Prepends the given extension struct between the root and the first pointer. This
|
2019-03-10 03:34:10 +11:00
|
|
|
/// method only exists on structs that can be passed to a function directly. Only
|
2019-02-27 21:10:38 +11:00
|
|
|
/// valid extension structs can be pushed into the chain.
|
|
|
|
/// If the chain looks like `A -> B -> C`, and you call `builder.push_next(&mut D)`, then the
|
|
|
|
/// chain will look like `A -> D -> B -> C`.
|
2021-04-18 06:25:06 +10:00
|
|
|
pub fn push_next<T: #extends_name>(mut self, next: &'a mut T) -> Self {
|
2019-02-03 22:48:05 +11:00
|
|
|
unsafe{
|
2019-03-04 00:33:19 +11:00
|
|
|
let next_ptr = next as *mut T as *mut BaseOutStructure;
|
2019-03-10 04:52:03 +11:00
|
|
|
// `next` here can contain a pointer chain. This means that we must correctly
|
|
|
|
// attach he head to the root and the tail to the rest of the chain
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// next = A -> B
|
|
|
|
// Before: `Root -> C -> D -> E`
|
|
|
|
// After: `Root -> A -> B -> C -> D -> E`
|
|
|
|
// ^^^^^^
|
|
|
|
// next chain
|
|
|
|
let last_next = ptr_chain_iter(next).last().unwrap();
|
|
|
|
(*last_next).p_next = self.inner.p_next as _;
|
2019-02-27 06:54:48 +11:00
|
|
|
self.inner.p_next = next_ptr as _;
|
2018-11-19 00:50:37 +11:00
|
|
|
}
|
2019-02-03 22:48:05 +11:00
|
|
|
self
|
2018-11-19 00:50:37 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-06 19:00:29 +10:00
|
|
|
quote!()
|
2018-11-19 00:50:37 +11:00
|
|
|
};
|
|
|
|
|
2021-06-06 19:00:29 +10:00
|
|
|
// Root structs come with their own trait that structs that extend
|
|
|
|
// this struct will implement
|
|
|
|
let next_trait = if is_root_struct {
|
|
|
|
quote!(pub unsafe trait #extends_name {})
|
2018-12-01 12:52:23 +11:00
|
|
|
} else {
|
2021-06-06 19:00:29 +10:00
|
|
|
quote!()
|
2018-12-01 12:52:23 +11:00
|
|
|
};
|
|
|
|
|
2021-06-17 07:44:51 +10:00
|
|
|
// If the struct extends something we need to implement the traits.
|
2021-09-08 19:26:58 +10:00
|
|
|
let impl_extend_trait = struct_
|
2021-06-17 07:44:51 +10:00
|
|
|
.extends
|
|
|
|
.iter()
|
|
|
|
.flat_map(|extends| extends.split(','))
|
|
|
|
.map(|extends| format_ident!("Extends{}", name_to_tokens(extends)))
|
|
|
|
.map(|extends| {
|
|
|
|
quote! {
|
|
|
|
unsafe impl #extends for #name_builder<'_> {}
|
|
|
|
unsafe impl #extends for #name {}
|
|
|
|
}
|
|
|
|
});
|
2019-02-27 06:54:48 +11:00
|
|
|
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-10-20 07:16:35 +11:00
|
|
|
impl #name {
|
2018-11-19 00:50:37 +11:00
|
|
|
pub fn builder<'a>() -> #name_builder<'a> {
|
2018-10-20 07:16:35 +11:00
|
|
|
#name_builder {
|
|
|
|
inner: #name::default(),
|
|
|
|
marker: ::std::marker::PhantomData,
|
|
|
|
}
|
2018-11-19 00:50:37 +11:00
|
|
|
}
|
|
|
|
}
|
2018-10-20 07:16:35 +11:00
|
|
|
|
2019-03-04 00:33:19 +11:00
|
|
|
#[repr(transparent)]
|
2018-10-20 07:16:35 +11:00
|
|
|
pub struct #name_builder<'a> {
|
|
|
|
inner: #name,
|
|
|
|
marker: ::std::marker::PhantomData<&'a ()>,
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:29:37 +11:00
|
|
|
#(#impl_extend_trait)*
|
2018-12-01 12:52:23 +11:00
|
|
|
#next_trait
|
2018-11-19 00:50:37 +11:00
|
|
|
|
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
impl<'a> ::std::ops::Deref for #name_builder<'a> {
|
|
|
|
type Target = #name;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 22:48:05 +11:00
|
|
|
impl<'a> ::std::ops::DerefMut for #name_builder<'a> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.inner
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 07:16:35 +11:00
|
|
|
|
|
|
|
impl<'a> #name_builder<'a> {
|
|
|
|
#(#setters)*
|
|
|
|
|
2018-11-19 00:50:37 +11:00
|
|
|
#next_function
|
|
|
|
|
2019-03-20 20:45:17 +11:00
|
|
|
/// Calling build will **discard** all the lifetime information. Only call this if
|
|
|
|
/// necessary! Builders implement `Deref` targeting their corresponding Vulkan struct,
|
|
|
|
/// so references to builders can be passed directly to Vulkan functions.
|
2018-10-20 07:16:35 +11:00
|
|
|
pub fn build(self) -> #name {
|
|
|
|
self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(q)
|
|
|
|
}
|
|
|
|
|
2018-08-20 15:30:10 +10:00
|
|
|
/// At the moment `Ash` doesn't properly derive all the necessary drives
|
|
|
|
/// like Eq, Hash etc.
|
|
|
|
/// To Address some cases, you can add the name of the struct that you
|
|
|
|
/// require and add the missing derives yourself.
|
2021-01-03 00:37:10 +11:00
|
|
|
pub fn manual_derives(_struct: &vkxml::Struct) -> TokenStream {
|
2018-08-20 15:30:10 +10:00
|
|
|
match _struct.name.as_str() {
|
2019-11-17 21:49:09 +11:00
|
|
|
"VkClearRect" | "VkExtent2D" | "VkExtent3D" | "VkOffset2D" | "VkOffset3D" | "VkRect2D"
|
|
|
|
| "VkSurfaceFormatKHR" => quote! {PartialEq, Eq, Hash,},
|
2018-12-07 20:33:36 +11:00
|
|
|
_ => quote! {},
|
2018-08-19 18:10:11 +10:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 06:54:48 +11:00
|
|
|
pub fn generate_struct(
|
|
|
|
_struct: &vkxml::Struct,
|
2021-06-17 07:44:51 +10:00
|
|
|
root_structs: &HashSet<Ident, impl BuildHasher>,
|
2019-10-21 02:18:40 +11:00
|
|
|
union_types: &HashSet<&str, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2018-07-30 20:50:51 +10:00
|
|
|
let name = name_to_tokens(&_struct.name);
|
2020-03-23 02:05:30 +11:00
|
|
|
if &_struct.name == "VkTransformMatrixKHR" {
|
|
|
|
return quote! {
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct TransformMatrixKHR {
|
|
|
|
pub matrix: [f32; 12],
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if &_struct.name == "VkAccelerationStructureInstanceKHR" {
|
|
|
|
return quote! {
|
2020-12-14 04:30:42 +11:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub union AccelerationStructureReferenceKHR {
|
|
|
|
pub device_handle: DeviceAddress,
|
|
|
|
pub host_handle: AccelerationStructureKHR,
|
|
|
|
}
|
2020-03-23 02:05:30 +11:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
2021-07-09 19:55:20 +10:00
|
|
|
#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkAccelerationStructureInstanceKHR.html>"]
|
2020-03-23 02:05:30 +11:00
|
|
|
pub struct AccelerationStructureInstanceKHR {
|
|
|
|
pub transform: TransformMatrixKHR,
|
2021-10-02 20:37:52 +10:00
|
|
|
/// Use [`Packed24_8::new(instance_custom_index, mask)`][Packed24_8::new()] to construct this field
|
|
|
|
pub instance_custom_index_and_mask: Packed24_8,
|
|
|
|
/// Use [`Packed24_8::new(instance_shader_binding_table_record_offset, flags)`][Packed24_8::new()] to construct this field
|
|
|
|
pub instance_shader_binding_table_record_offset_and_flags: Packed24_8,
|
2020-12-14 04:30:42 +11:00
|
|
|
pub acceleration_structure_reference: AccelerationStructureReferenceKHR,
|
2020-03-23 02:05:30 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:55:20 +10:00
|
|
|
if &_struct.name == "VkAccelerationStructureSRTMotionInstanceNV" {
|
|
|
|
return quote! {
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkAccelerationStructureSRTMotionInstanceNV.html>"]
|
|
|
|
pub struct AccelerationStructureSRTMotionInstanceNV {
|
|
|
|
pub transform_t0: SRTDataNV,
|
|
|
|
pub transform_t1: SRTDataNV,
|
2021-10-02 20:37:52 +10:00
|
|
|
/// Use [`Packed24_8::new(instance_custom_index, mask)`][Packed24_8::new()] to construct this field
|
|
|
|
pub instance_custom_index_and_mask: Packed24_8,
|
|
|
|
/// Use [`Packed24_8::new(instance_shader_binding_table_record_offset, flags)`][Packed24_8::new()] to construct this field
|
|
|
|
pub instance_shader_binding_table_record_offset_and_flags: Packed24_8,
|
2021-07-09 19:55:20 +10:00
|
|
|
pub acceleration_structure_reference: AccelerationStructureReferenceKHR,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if &_struct.name == "VkAccelerationStructureMatrixMotionInstanceNV" {
|
|
|
|
return quote! {
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/AccelerationStructureMatrixMotionInstanceNV.html>"]
|
|
|
|
pub struct AccelerationStructureMatrixMotionInstanceNV {
|
|
|
|
pub transform_t0: TransformMatrixKHR,
|
|
|
|
pub transform_t1: TransformMatrixKHR,
|
2021-10-02 20:37:52 +10:00
|
|
|
/// Use [`Packed24_8::new(instance_custom_index, mask)`][Packed24_8::new()] to construct this field
|
|
|
|
pub instance_custom_index_and_mask: Packed24_8,
|
|
|
|
/// Use [`Packed24_8::new(instance_shader_binding_table_record_offset, flags)`][Packed24_8::new()] to construct this field
|
|
|
|
pub instance_shader_binding_table_record_offset_and_flags: Packed24_8,
|
2021-07-09 19:55:20 +10:00
|
|
|
pub acceleration_structure_reference: AccelerationStructureReferenceKHR,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-30 20:50:51 +10:00
|
|
|
let members = _struct.elements.iter().filter_map(|elem| match *elem {
|
|
|
|
vkxml::StructElement::Member(ref field) => Some(field),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
|
|
|
let params = members.clone().map(|field| {
|
|
|
|
let param_ident = field.param_ident();
|
2019-03-14 08:50:04 +11:00
|
|
|
let param_ty_tokens = field.type_tokens(false);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {pub #param_ident: #param_ty_tokens}
|
2018-07-30 20:50:51 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
let debug_tokens = derive_debug(_struct, union_types);
|
2018-08-19 18:10:11 +10:00
|
|
|
let default_tokens = derive_default(_struct);
|
2021-06-17 07:44:51 +10:00
|
|
|
let setter_tokens = derive_setters(_struct, root_structs);
|
2018-08-20 15:30:10 +10:00
|
|
|
let manual_derive_tokens = manual_derives(_struct);
|
2018-07-31 03:53:12 +10:00
|
|
|
let dbg_str = if debug_tokens.is_none() {
|
|
|
|
quote!(Debug,)
|
|
|
|
} else {
|
|
|
|
quote!()
|
|
|
|
};
|
|
|
|
let default_str = if default_tokens.is_none() {
|
|
|
|
quote!(Default,)
|
|
|
|
} else {
|
|
|
|
quote!()
|
|
|
|
};
|
2019-03-17 03:46:26 +11:00
|
|
|
let khronos_link = khronos_link(&_struct.name);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-03-11 02:21:35 +11:00
|
|
|
#[repr(C)]
|
2018-08-20 15:30:10 +10:00
|
|
|
#[derive(Copy, Clone, #default_str #dbg_str #manual_derive_tokens)]
|
2019-03-14 14:51:22 +11:00
|
|
|
#[doc = #khronos_link]
|
2018-03-11 02:21:35 +11:00
|
|
|
pub struct #name {
|
|
|
|
#(#params,)*
|
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
#debug_tokens
|
2018-07-31 03:53:12 +10:00
|
|
|
#default_tokens
|
2018-10-20 07:16:35 +11:00
|
|
|
#setter_tokens
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
pub fn generate_handle(handle: &vkxml::Handle) -> Option<TokenStream> {
|
2020-12-13 06:56:43 +11:00
|
|
|
if handle.name.is_empty() {
|
2018-06-24 20:09:37 +10:00
|
|
|
return None;
|
|
|
|
}
|
2019-03-17 03:46:26 +11:00
|
|
|
let khronos_link = khronos_link(&handle.name);
|
2018-06-24 20:09:37 +10:00
|
|
|
let tokens = match handle.ty {
|
|
|
|
vkxml::HandleType::Dispatch => {
|
2021-05-19 07:30:28 +10:00
|
|
|
let name = handle.name.strip_prefix("Vk").unwrap();
|
2021-01-03 00:37:10 +11:00
|
|
|
let ty = format_ident!("{}", name.to_shouty_snake_case());
|
|
|
|
let name = format_ident!("{}", name);
|
2018-06-24 20:09:37 +10:00
|
|
|
quote! {
|
2019-03-14 14:51:22 +11:00
|
|
|
define_handle!(#name, #ty, doc = #khronos_link);
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vkxml::HandleType::NoDispatch => {
|
2021-05-19 07:30:28 +10:00
|
|
|
let name = handle.name.strip_prefix("Vk").unwrap();
|
2021-01-03 00:37:10 +11:00
|
|
|
let ty = format_ident!("{}", name.to_shouty_snake_case());
|
|
|
|
let name = format_ident!("{}", name);
|
2018-06-24 20:09:37 +10:00
|
|
|
quote! {
|
2019-03-14 14:51:22 +11:00
|
|
|
handle_nondispatchable!(#name, #ty, doc = #khronos_link);
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(tokens)
|
|
|
|
}
|
2021-01-03 00:37:10 +11:00
|
|
|
fn generate_funcptr(fnptr: &vkxml::FunctionPointer) -> TokenStream {
|
|
|
|
let name = format_ident!("{}", fnptr.name.as_str());
|
2021-03-01 02:50:24 +11:00
|
|
|
let ret_ty_tokens = if fnptr.return_type.is_void() {
|
|
|
|
quote!()
|
|
|
|
} else {
|
|
|
|
let ret_ty_tokens = fnptr.return_type.type_tokens(true);
|
|
|
|
quote!(-> #ret_ty_tokens)
|
|
|
|
};
|
2018-07-11 21:18:22 +10:00
|
|
|
let params = fnptr.param.iter().map(|field| {
|
|
|
|
let ident = field.param_ident();
|
2019-03-14 08:50:04 +11:00
|
|
|
let type_tokens = field.type_tokens(true);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-11 21:18:22 +10:00
|
|
|
#ident: #type_tokens
|
|
|
|
}
|
|
|
|
});
|
2019-03-17 03:46:26 +11:00
|
|
|
let khronos_link = khronos_link(&fnptr.name);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-07 20:54:31 +10:00
|
|
|
#[allow(non_camel_case_types)]
|
2019-03-14 14:51:22 +11:00
|
|
|
#[doc = #khronos_link]
|
2021-03-01 02:50:24 +11:00
|
|
|
pub type #name = Option<unsafe extern "system" fn(#(#params),*) #ret_ty_tokens>;
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
2018-07-07 18:43:44 +10:00
|
|
|
|
2021-01-03 00:37:10 +11:00
|
|
|
fn generate_union(union: &vkxml::Union) -> TokenStream {
|
2020-12-13 06:56:43 +11:00
|
|
|
let name = name_to_tokens(&union.name);
|
2018-07-07 18:43:44 +10:00
|
|
|
let fields = union.elements.iter().map(|field| {
|
|
|
|
let name = field.param_ident();
|
2019-03-14 08:50:04 +11:00
|
|
|
let ty = field.type_tokens(false);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-07 18:43:44 +10:00
|
|
|
pub #name: #ty
|
|
|
|
}
|
|
|
|
});
|
2019-03-17 03:46:26 +11:00
|
|
|
let khronos_link = khronos_link(&union.name);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-07 18:43:44 +10:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
2019-03-14 14:51:22 +11:00
|
|
|
#[doc = #khronos_link]
|
2018-07-07 18:43:44 +10:00
|
|
|
pub union #name {
|
|
|
|
#(#fields),*
|
|
|
|
}
|
2018-07-31 03:53:12 +10:00
|
|
|
impl ::std::default::Default for #name {
|
|
|
|
fn default() -> #name {
|
|
|
|
unsafe { ::std::mem::zeroed() }
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 18:43:44 +10:00
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
2021-06-17 07:44:51 +10:00
|
|
|
/// Root structs are all structs that are extended by other structs.
|
|
|
|
pub fn root_structs(definitions: &[&vkxml::DefinitionsElement]) -> HashSet<Ident> {
|
|
|
|
let mut root_structs = HashSet::new();
|
|
|
|
// Loop over all structs and collect their extends
|
|
|
|
for definition in definitions {
|
|
|
|
if let vkxml::DefinitionsElement::Struct(ref _struct) = definition {
|
|
|
|
if let Some(extends) = &_struct.extends {
|
|
|
|
root_structs.extend(extends.split(',').map(name_to_tokens));
|
2019-02-27 06:54:48 +11:00
|
|
|
}
|
2021-06-17 07:44:51 +10:00
|
|
|
};
|
|
|
|
}
|
|
|
|
root_structs
|
2019-02-27 06:54:48 +11:00
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
pub fn generate_definition(
|
|
|
|
definition: &vkxml::DefinitionsElement,
|
2019-10-21 02:18:40 +11:00
|
|
|
union_types: &HashSet<&str, impl BuildHasher>,
|
2021-06-17 07:44:51 +10:00
|
|
|
root_structs: &HashSet<Ident, impl BuildHasher>,
|
2019-10-21 02:18:40 +11:00
|
|
|
bitflags_cache: &mut HashSet<Ident, impl BuildHasher>,
|
2021-05-08 20:25:10 +10:00
|
|
|
const_values: &mut BTreeMap<Ident, ConstantTypeInfo>,
|
2021-05-30 21:59:18 +10:00
|
|
|
identifier_renames: &mut BTreeMap<String, Ident>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> Option<TokenStream> {
|
2018-03-11 02:21:35 +11:00
|
|
|
match *definition {
|
2021-05-30 21:59:18 +10:00
|
|
|
vkxml::DefinitionsElement::Define(ref define) => {
|
|
|
|
Some(generate_define(define, identifier_renames))
|
|
|
|
}
|
2018-06-04 19:26:14 +10:00
|
|
|
vkxml::DefinitionsElement::Typedef(ref typedef) => Some(generate_typedef(typedef)),
|
2018-07-30 20:50:51 +10:00
|
|
|
vkxml::DefinitionsElement::Struct(ref _struct) => {
|
2019-03-10 04:52:03 +11:00
|
|
|
Some(generate_struct(_struct, root_structs, union_types))
|
2018-07-30 20:50:51 +10:00
|
|
|
}
|
2019-04-22 09:04:49 +10:00
|
|
|
vkxml::DefinitionsElement::Bitmask(ref mask) => {
|
|
|
|
generate_bitmask(mask, bitflags_cache, const_values)
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
vkxml::DefinitionsElement::Handle(ref handle) => generate_handle(handle),
|
|
|
|
vkxml::DefinitionsElement::FuncPtr(ref fp) => Some(generate_funcptr(fp)),
|
|
|
|
vkxml::DefinitionsElement::Union(ref union) => Some(generate_union(union)),
|
2018-06-04 19:26:14 +10:00
|
|
|
_ => None,
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
}
|
2018-11-17 03:57:08 +11:00
|
|
|
pub fn generate_feature<'a>(
|
|
|
|
feature: &vkxml::Feature,
|
|
|
|
commands: &CommandMap<'a>,
|
2019-10-21 02:18:40 +11:00
|
|
|
fn_cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2018-07-07 20:54:31 +10:00
|
|
|
let (static_commands, entry_commands, device_commands, instance_commands) = feature
|
2018-03-10 07:45:58 +11:00
|
|
|
.elements
|
|
|
|
.iter()
|
|
|
|
.flat_map(|feature| {
|
2018-07-30 16:54:03 +10:00
|
|
|
if let vkxml::FeatureElement::Require(ref spec) = feature {
|
2018-03-10 07:45:58 +11:00
|
|
|
spec.elements
|
|
|
|
.iter()
|
|
|
|
.filter_map(|feature_spec| {
|
2018-07-30 20:50:51 +10:00
|
|
|
if let vkxml::FeatureReference::CommandReference(ref cmd_ref) = feature_spec
|
2018-03-10 07:45:58 +11:00
|
|
|
{
|
|
|
|
Some(cmd_ref)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect()
|
2018-03-10 07:45:58 +11:00
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.filter_map(|cmd_ref| commands.get(&cmd_ref.name))
|
2018-07-07 20:54:31 +10:00
|
|
|
.fold(
|
|
|
|
(Vec::new(), Vec::new(), Vec::new(), Vec::new()),
|
|
|
|
|mut acc, &cmd_ref| {
|
|
|
|
match cmd_ref.function_type() {
|
|
|
|
FunctionType::Static => {
|
|
|
|
acc.0.push(cmd_ref);
|
|
|
|
}
|
|
|
|
FunctionType::Entry => {
|
|
|
|
acc.1.push(cmd_ref);
|
|
|
|
}
|
|
|
|
FunctionType::Device => {
|
|
|
|
acc.2.push(cmd_ref);
|
|
|
|
}
|
|
|
|
FunctionType::Instance => {
|
|
|
|
acc.3.push(cmd_ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
},
|
|
|
|
);
|
2018-03-10 08:43:45 +11:00
|
|
|
let version = feature.version_string();
|
2019-10-21 02:18:40 +11:00
|
|
|
let static_fn = if feature.is_version(1, 0) {
|
2020-03-22 23:56:01 +11:00
|
|
|
generate_function_pointers(
|
2021-01-03 00:37:10 +11:00
|
|
|
format_ident!("{}", "StaticFn"),
|
2020-03-22 23:56:01 +11:00
|
|
|
&static_commands,
|
|
|
|
&HashMap::new(),
|
|
|
|
fn_cache,
|
|
|
|
)
|
2018-07-07 20:54:31 +10:00
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {}
|
2018-07-07 20:54:31 +10:00
|
|
|
};
|
|
|
|
let entry = generate_function_pointers(
|
2021-05-19 07:30:28 +10:00
|
|
|
format_ident!("EntryFnV{}", version),
|
2018-07-07 20:54:31 +10:00
|
|
|
&entry_commands,
|
2020-03-22 23:56:01 +11:00
|
|
|
&HashMap::new(),
|
2018-11-17 03:57:08 +11:00
|
|
|
fn_cache,
|
2018-07-07 20:54:31 +10:00
|
|
|
);
|
2018-03-10 08:43:45 +11:00
|
|
|
let instance = generate_function_pointers(
|
2021-05-19 07:30:28 +10:00
|
|
|
format_ident!("InstanceFnV{}", version),
|
2018-03-10 08:43:45 +11:00
|
|
|
&instance_commands,
|
2020-03-22 23:56:01 +11:00
|
|
|
&HashMap::new(),
|
2018-11-17 03:57:08 +11:00
|
|
|
fn_cache,
|
2018-03-10 08:43:45 +11:00
|
|
|
);
|
|
|
|
let device = generate_function_pointers(
|
2021-05-19 07:30:28 +10:00
|
|
|
format_ident!("DeviceFnV{}", version),
|
2018-03-10 20:07:12 +11:00
|
|
|
&device_commands,
|
2020-03-22 23:56:01 +11:00
|
|
|
&HashMap::new(),
|
2018-11-17 03:57:08 +11:00
|
|
|
fn_cache,
|
2018-03-10 08:43:45 +11:00
|
|
|
);
|
|
|
|
quote! {
|
2018-07-07 20:54:31 +10:00
|
|
|
#static_fn
|
|
|
|
#entry
|
2018-03-10 08:43:45 +11:00
|
|
|
#instance
|
|
|
|
#device
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
|
|
|
|
pub fn constant_name(name: &str) -> &str {
|
|
|
|
name.strip_prefix("VK_").unwrap_or(name)
|
2018-08-01 17:22:28 +10:00
|
|
|
}
|
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
pub fn generate_constant<'a>(
|
|
|
|
constant: &'a vkxml::Constant,
|
2019-10-21 02:18:40 +11:00
|
|
|
cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2018-07-30 06:39:45 +10:00
|
|
|
cache.insert(constant.name.as_str());
|
2018-06-24 20:09:37 +10:00
|
|
|
let c = Constant::from_constant(constant);
|
2018-08-01 17:22:28 +10:00
|
|
|
let name = constant_name(&constant.name);
|
2021-01-03 00:37:10 +11:00
|
|
|
let ident = format_ident!("{}", name);
|
2018-08-03 23:39:25 +10:00
|
|
|
let ty = if name == "TRUE" || name == "FALSE" {
|
|
|
|
CType::Bool32
|
|
|
|
} else {
|
|
|
|
c.ty()
|
|
|
|
};
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2020-12-13 06:56:43 +11:00
|
|
|
pub const #ident: #ty = #c;
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
2018-03-11 02:21:35 +11:00
|
|
|
|
2018-07-31 04:06:00 +10:00
|
|
|
pub fn generate_feature_extension<'a>(
|
|
|
|
registry: &'a vk_parse::Registry,
|
2019-10-21 02:18:40 +11:00
|
|
|
const_cache: &mut HashSet<&'a str, impl BuildHasher>,
|
2021-05-08 20:25:10 +10:00
|
|
|
const_values: &mut BTreeMap<Ident, ConstantTypeInfo>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2018-11-18 06:00:57 +11:00
|
|
|
let constants = registry.0.iter().filter_map(|item| match item {
|
|
|
|
vk_parse::RegistryChild::Feature(feature) => Some(generate_extension_constants(
|
|
|
|
&feature.name,
|
|
|
|
0,
|
|
|
|
&feature.children,
|
|
|
|
const_cache,
|
|
|
|
const_values,
|
|
|
|
)),
|
|
|
|
_ => None,
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 04:06:00 +10:00
|
|
|
#(#constants)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:05:30 +11:00
|
|
|
pub struct ConstantMatchInfo {
|
|
|
|
pub ident: Ident,
|
|
|
|
pub is_alias: bool,
|
|
|
|
}
|
|
|
|
|
2021-05-08 20:25:10 +10:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ConstantTypeInfo {
|
|
|
|
values: Vec<ConstantMatchInfo>,
|
|
|
|
bitwidth: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_const_debugs(const_values: &BTreeMap<Ident, ConstantTypeInfo>) -> TokenStream {
|
2019-04-22 09:04:49 +10:00
|
|
|
let impls = const_values.iter().map(|(ty, values)| {
|
2021-05-08 20:25:10 +10:00
|
|
|
let ConstantTypeInfo { values, bitwidth } = values;
|
2019-04-22 09:04:49 +10:00
|
|
|
if ty.to_string().contains("Flags") {
|
2020-03-23 02:05:30 +11:00
|
|
|
let cases = values.iter().filter_map(|value| {
|
|
|
|
if value.is_alias {
|
|
|
|
None
|
|
|
|
} else {
|
2021-01-03 00:37:10 +11:00
|
|
|
let ident = &value.ident;
|
|
|
|
let name = ident.to_string();
|
2020-03-23 02:05:30 +11:00
|
|
|
Some(quote! { (#ty::#ident.0, #name) })
|
|
|
|
}
|
2019-04-22 09:04:49 +10:00
|
|
|
});
|
2021-05-08 20:25:10 +10:00
|
|
|
|
|
|
|
let type_ = if bitwidth == &Some(64u32) {
|
|
|
|
quote!(Flags64)
|
|
|
|
} else {
|
|
|
|
quote!(Flags)
|
|
|
|
};
|
|
|
|
|
2019-04-22 09:04:49 +10:00
|
|
|
quote! {
|
|
|
|
impl fmt::Debug for #ty {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-05-08 20:25:10 +10:00
|
|
|
const KNOWN: &[(#type_, &str)] = &[#(#cases),*];
|
2019-04-22 09:04:49 +10:00
|
|
|
debug_flags(f, KNOWN, self.0)
|
2018-08-20 12:40:28 +10:00
|
|
|
}
|
|
|
|
}
|
2019-04-22 09:04:49 +10:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-23 02:05:30 +11:00
|
|
|
let cases = values.iter().filter_map(|value| {
|
|
|
|
if value.is_alias {
|
|
|
|
None
|
|
|
|
} else {
|
2021-01-03 00:37:10 +11:00
|
|
|
let ident = &value.ident;
|
|
|
|
let name = ident.to_string();
|
2020-03-23 02:05:30 +11:00
|
|
|
Some(quote! { Self::#ident => Some(#name), })
|
|
|
|
}
|
2019-04-22 09:04:49 +10:00
|
|
|
});
|
|
|
|
quote! {
|
|
|
|
impl fmt::Debug for #ty {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let name = match *self {
|
|
|
|
#(#cases)*
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(x) = name {
|
|
|
|
f.write_str(x)
|
|
|
|
} else {
|
|
|
|
self.0.fmt(f)
|
2018-08-20 12:40:28 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 09:04:49 +10:00
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2021-05-08 20:25:10 +10:00
|
|
|
pub(crate) fn debug_flags<Value: Into<u64> + Copy>(
|
|
|
|
f: &mut fmt::Formatter,
|
|
|
|
known: &[(Value, &'static str)],
|
|
|
|
value: Value,
|
|
|
|
) -> fmt::Result {
|
2018-08-20 12:40:28 +10:00
|
|
|
let mut first = true;
|
2021-05-08 20:25:10 +10:00
|
|
|
let mut accum = value.into();
|
|
|
|
for &(bit, name) in known {
|
|
|
|
let bit = bit.into();
|
|
|
|
if bit != 0 && accum & bit == bit {
|
|
|
|
if !first {
|
|
|
|
f.write_str(" | ")?;
|
|
|
|
}
|
2018-08-20 12:40:28 +10:00
|
|
|
f.write_str(name)?;
|
|
|
|
first = false;
|
|
|
|
accum &= !bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if accum != 0 {
|
2021-05-08 20:25:10 +10:00
|
|
|
if !first {
|
|
|
|
f.write_str(" | ")?;
|
|
|
|
}
|
2018-08-20 12:40:28 +10:00
|
|
|
write!(f, "{:b}", accum)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#(#impls)*
|
|
|
|
}
|
|
|
|
}
|
2021-06-06 19:00:29 +10:00
|
|
|
pub fn extract_native_types(registry: &vk_parse::Registry) -> (Vec<(String, String)>, Vec<String>) {
|
|
|
|
// Not a HashMap so that headers are processed in order of definition:
|
|
|
|
let mut header_includes = vec![];
|
|
|
|
let mut header_types = vec![];
|
|
|
|
|
|
|
|
let types = registry
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
|
|
|
vk_parse::RegistryChild::Types(ref ty) => {
|
|
|
|
Some(ty.children.iter().filter_map(|child| match child {
|
|
|
|
vk_parse::TypesChild::Type(ty) => Some(ty),
|
|
|
|
_ => None,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
|
|
|
for ty in types {
|
|
|
|
match ty.category.as_deref() {
|
|
|
|
Some("include") => {
|
|
|
|
// `category="include"` lacking an `#include` directive are generally "irrelevant" system headers.
|
|
|
|
if let vk_parse::TypeSpec::Code(code) = &ty.spec {
|
|
|
|
let name = ty
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
.expect("Include type must provide header name");
|
|
|
|
assert!(
|
|
|
|
header_includes
|
|
|
|
.iter()
|
|
|
|
.all(|(other_name, _)| other_name != &name),
|
|
|
|
"Header `{}` being redefined",
|
|
|
|
name
|
|
|
|
);
|
|
|
|
|
|
|
|
let (rem, path) = c_include(&code.code)
|
|
|
|
.expect("Failed to parse `#include` from `category=\"include\"` directive");
|
|
|
|
assert!(rem.is_empty());
|
|
|
|
header_includes.push((name, path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => {}
|
|
|
|
None => {
|
|
|
|
if let Some(header_name) = ty.requires.clone() {
|
|
|
|
if header_includes.iter().any(|(name, _)| name == &header_name) {
|
|
|
|
// Omit types from system and other headers
|
|
|
|
header_types.push(ty.name.clone().expect("Type must have a name"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
(header_includes, header_types)
|
|
|
|
}
|
2020-12-13 06:56:43 +11:00
|
|
|
pub fn generate_aliases_of_types(
|
|
|
|
types: &vk_parse::Types,
|
2019-10-21 02:18:40 +11:00
|
|
|
ty_cache: &mut HashSet<Ident, impl BuildHasher>,
|
2021-01-03 00:37:10 +11:00
|
|
|
) -> TokenStream {
|
2018-11-18 19:29:17 +11:00
|
|
|
let aliases = types
|
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.filter_map(|child| match child {
|
|
|
|
vk_parse::TypesChild::Type(ty) => Some((ty.name.as_ref()?, ty.alias.as_ref()?)),
|
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.filter_map(|(name, alias)| {
|
2018-11-18 19:29:17 +11:00
|
|
|
let name_ident = name_to_tokens(name);
|
2021-05-19 07:30:28 +10:00
|
|
|
if !ty_cache.insert(name_ident.clone()) {
|
2018-11-18 19:29:17 +11:00
|
|
|
return None;
|
|
|
|
};
|
|
|
|
let alias_ident = name_to_tokens(alias);
|
2018-12-07 20:33:36 +11:00
|
|
|
let tokens = quote! {
|
2018-11-18 19:29:17 +11:00
|
|
|
pub type #name_ident = #alias_ident;
|
|
|
|
};
|
|
|
|
Some(tokens)
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-18 06:00:57 +11:00
|
|
|
#(#aliases)*
|
|
|
|
}
|
|
|
|
}
|
2021-06-06 19:00:29 +10:00
|
|
|
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");
|
2018-03-11 02:21:35 +11:00
|
|
|
use std::fs::File;
|
2018-06-04 19:26:14 +10:00
|
|
|
use std::io::Write;
|
2021-06-06 19:00:29 +10:00
|
|
|
let (spec2, _errors) = vk_parse::parse_file(&vk_xml).expect("Invalid xml file");
|
2018-07-21 20:56:16 +10:00
|
|
|
let extensions: &Vec<vk_parse::Extension> = spec2
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
2018-11-18 03:27:18 +11:00
|
|
|
vk_parse::RegistryChild::Extensions(ref ext) => Some(&ext.children),
|
2018-07-21 20:56:16 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
2020-03-15 10:55:26 +11:00
|
|
|
.next()
|
2018-07-21 20:56:16 +10:00
|
|
|
.expect("extension");
|
2019-03-15 10:22:52 +11:00
|
|
|
let mut ty_cache = HashSet::new();
|
2018-11-18 06:00:57 +11:00
|
|
|
let aliases: Vec<_> = spec2
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
2018-11-18 19:29:17 +11:00
|
|
|
vk_parse::RegistryChild::Types(ref ty) => {
|
|
|
|
Some(generate_aliases_of_types(ty, &mut ty_cache))
|
|
|
|
}
|
2018-11-18 06:00:57 +11:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-07-21 20:56:16 +10:00
|
|
|
|
2021-06-06 19:00:29 +10:00
|
|
|
let spec = vk_parse::parse_file_as_vkxml(&vk_xml).expect("Invalid xml file.");
|
2020-03-22 23:56:01 +11:00
|
|
|
let cmd_aliases: HashMap<String, String> = spec2
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
|
|
|
vk_parse::RegistryChild::Commands(cmds) => {
|
|
|
|
let cmd_tuple_iter = cmds.children.iter().filter_map(|cmd| match cmd {
|
|
|
|
vk_parse::Command::Alias { name, alias } => {
|
|
|
|
Some((name.to_string(), alias.to_string()))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
Some(cmd_tuple_iter)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect();
|
|
|
|
|
2019-03-15 10:22:52 +11:00
|
|
|
let commands: HashMap<vkxml::Identifier, &vkxml::Command> = spec
|
2018-11-18 05:05:28 +11:00
|
|
|
.elements
|
2018-03-11 02:21:35 +11:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Commands(ref cmds) => Some(cmds),
|
2018-03-11 02:21:35 +11:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|cmds| cmds.elements.iter().map(|cmd| (cmd.name.clone(), cmd)))
|
2018-03-11 02:21:35 +11:00
|
|
|
.collect();
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let features: Vec<&vkxml::Feature> = spec
|
|
|
|
.elements
|
2018-03-11 02:21:35 +11:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Features(ref features) => Some(features),
|
2018-03-11 02:21:35 +11:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|features| features.elements.iter())
|
2018-03-11 02:21:35 +11:00
|
|
|
.collect();
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let definitions: Vec<&vkxml::DefinitionsElement> = spec
|
|
|
|
.elements
|
2018-03-11 02:21:35 +11:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Definitions(ref definitions) => Some(definitions),
|
2018-03-11 02:21:35 +11:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|definitions| definitions.elements.iter())
|
2018-03-11 02:21:35 +11:00
|
|
|
.collect();
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let constants: Vec<&vkxml::Constant> = spec
|
|
|
|
.elements
|
2018-04-01 18:52:21 +10:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Constants(ref constants) => Some(constants),
|
2018-04-01 18:52:21 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|constants| constants.elements.iter())
|
2018-04-01 18:52:21 +10:00
|
|
|
.collect();
|
|
|
|
|
2019-03-15 10:22:52 +11:00
|
|
|
let mut fn_cache = HashSet::new();
|
|
|
|
let mut bitflags_cache = HashSet::new();
|
|
|
|
let mut const_cache = HashSet::new();
|
2018-08-20 12:40:28 +10:00
|
|
|
|
2021-05-08 20:25:10 +10:00
|
|
|
let mut const_values: BTreeMap<Ident, ConstantTypeInfo> = BTreeMap::new();
|
2018-08-20 12:40:28 +10:00
|
|
|
|
2021-04-19 05:58:40 +10:00
|
|
|
let (enum_code, bitflags_code) = spec2
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
|
|
|
vk_parse::RegistryChild::Enums(ref enums) if enums.kind.is_some() => Some(enums),
|
|
|
|
_ => None,
|
|
|
|
})
|
2018-11-18 19:29:17 +11:00
|
|
|
.map(|e| generate_enum(e, &mut const_cache, &mut const_values, &mut bitflags_cache))
|
2018-07-30 06:39:45 +10:00
|
|
|
.fold((Vec::new(), Vec::new()), |mut acc, elem| {
|
2018-04-01 18:52:21 +10:00
|
|
|
match elem {
|
|
|
|
EnumType::Enum(token) => acc.0.push(token),
|
|
|
|
EnumType::Bitflags(token) => acc.1.push(token),
|
|
|
|
};
|
|
|
|
acc
|
2018-07-30 06:39:45 +10:00
|
|
|
});
|
2018-08-23 06:53:17 +10:00
|
|
|
|
2020-03-23 02:05:30 +11:00
|
|
|
let mut constants_code: Vec<_> = constants
|
2018-07-07 22:49:17 +10:00
|
|
|
.iter()
|
2018-07-30 06:39:45 +10:00
|
|
|
.map(|constant| generate_constant(constant, &mut const_cache))
|
2018-07-07 22:49:17 +10:00
|
|
|
.collect();
|
2020-03-23 02:05:30 +11:00
|
|
|
|
|
|
|
constants_code.push(quote! { pub const SHADER_UNUSED_NV : u32 = SHADER_UNUSED_KHR;});
|
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
let extension_code = extensions
|
|
|
|
.iter()
|
2018-11-17 03:57:08 +11:00
|
|
|
.filter_map(|ext| {
|
|
|
|
generate_extension(
|
|
|
|
ext,
|
|
|
|
&commands,
|
|
|
|
&mut const_cache,
|
|
|
|
&mut const_values,
|
2020-03-22 23:56:01 +11:00
|
|
|
&cmd_aliases,
|
2018-11-17 03:57:08 +11:00
|
|
|
&mut fn_cache,
|
|
|
|
)
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect_vec();
|
2018-04-01 18:52:21 +10:00
|
|
|
|
2018-07-30 20:50:51 +10:00
|
|
|
let union_types = definitions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|def| match def {
|
|
|
|
vkxml::DefinitionsElement::Union(ref union) => Some(union.name.as_str()),
|
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
2019-03-15 10:22:52 +11:00
|
|
|
.collect::<HashSet<&str>>();
|
2018-07-30 20:50:51 +10:00
|
|
|
|
2021-05-30 21:59:18 +10:00
|
|
|
let mut identifier_renames = BTreeMap::new();
|
|
|
|
|
2021-06-17 07:44:51 +10:00
|
|
|
let root_structs = root_structs(&definitions);
|
2018-06-06 00:47:21 +10:00
|
|
|
let definition_code: Vec<_> = definitions
|
|
|
|
.into_iter()
|
2019-04-22 09:04:49 +10:00
|
|
|
.filter_map(|def| {
|
|
|
|
generate_definition(
|
|
|
|
def,
|
|
|
|
&union_types,
|
2021-06-17 07:44:51 +10:00
|
|
|
&root_structs,
|
2019-04-22 09:04:49 +10:00
|
|
|
&mut bitflags_cache,
|
|
|
|
&mut const_values,
|
2021-05-30 21:59:18 +10:00
|
|
|
&mut identifier_renames,
|
2019-04-22 09:04:49 +10:00
|
|
|
)
|
|
|
|
})
|
2018-06-06 00:47:21 +10:00
|
|
|
.collect();
|
2018-03-11 02:21:35 +11:00
|
|
|
|
|
|
|
let feature_code: Vec<_> = features
|
|
|
|
.iter()
|
2018-11-17 03:57:08 +11:00
|
|
|
.map(|feature| generate_feature(feature, &commands, &mut fn_cache))
|
2018-03-11 02:21:35 +11:00
|
|
|
.collect();
|
2018-11-17 03:57:08 +11:00
|
|
|
let feature_extensions_code =
|
|
|
|
generate_feature_extension(&spec2, &mut const_cache, &mut const_values);
|
2018-08-20 12:40:28 +10:00
|
|
|
|
2019-04-22 09:04:49 +10:00
|
|
|
let const_debugs = generate_const_debugs(&const_values);
|
2018-03-11 02:21:35 +11:00
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
let bitflags_macro = vk_bitflags_wrapped_macro();
|
|
|
|
let handle_nondispatchable_macro = handle_nondispatchable_macro();
|
|
|
|
let define_handle_macro = define_handle_macro();
|
2020-04-20 03:12:17 +10:00
|
|
|
|
|
|
|
let macros_code = quote! {
|
2018-07-07 20:54:31 +10:00
|
|
|
#bitflags_macro
|
2018-07-09 17:23:53 +10:00
|
|
|
#handle_nondispatchable_macro
|
|
|
|
#define_handle_macro
|
2020-04-20 03:12:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let src_dir = src_dir.as_ref();
|
|
|
|
|
|
|
|
let vk_dir = src_dir.join("vk");
|
|
|
|
std::fs::create_dir_all(&vk_dir).expect("failed to create vk dir");
|
|
|
|
|
|
|
|
let mut vk_macros_file = File::create(vk_dir.join("macros.rs")).expect("vk/macros.rs");
|
|
|
|
let mut vk_features_file = File::create(vk_dir.join("features.rs")).expect("vk/features.rs");
|
|
|
|
let mut vk_definitions_file =
|
|
|
|
File::create(vk_dir.join("definitions.rs")).expect("vk/definitions.rs");
|
|
|
|
let mut vk_enums_file = File::create(vk_dir.join("enums.rs")).expect("vk/enums.rs");
|
|
|
|
let mut vk_bitflags_file = File::create(vk_dir.join("bitflags.rs")).expect("vk/bitflags.rs");
|
|
|
|
let mut vk_constants_file = File::create(vk_dir.join("constants.rs")).expect("vk/constants.rs");
|
|
|
|
let mut vk_extensions_file =
|
|
|
|
File::create(vk_dir.join("extensions.rs")).expect("vk/extensions.rs");
|
|
|
|
let mut vk_feature_extensions_file =
|
|
|
|
File::create(vk_dir.join("feature_extensions.rs")).expect("vk/feature_extensions.rs");
|
|
|
|
let mut vk_const_debugs_file =
|
|
|
|
File::create(vk_dir.join("const_debugs.rs")).expect("vk/const_debugs.rs");
|
|
|
|
let mut vk_aliases_file = File::create(vk_dir.join("aliases.rs")).expect("vk/aliases.rs");
|
|
|
|
|
|
|
|
let feature_code = quote! {
|
|
|
|
use std::os::raw::*;
|
|
|
|
use crate::vk::bitflags::*;
|
|
|
|
use crate::vk::definitions::*;
|
|
|
|
use crate::vk::enums::*;
|
2018-07-09 17:23:53 +10:00
|
|
|
#(#feature_code)*
|
2020-04-20 03:12:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let definition_code = quote! {
|
|
|
|
use std::fmt;
|
|
|
|
use std::os::raw::*;
|
|
|
|
use crate::vk::{Handle, ptr_chain_iter};
|
|
|
|
use crate::vk::aliases::*;
|
|
|
|
use crate::vk::bitflags::*;
|
|
|
|
use crate::vk::constants::*;
|
|
|
|
use crate::vk::enums::*;
|
2021-06-06 19:00:29 +10:00
|
|
|
use crate::vk::native::*;
|
|
|
|
use crate::vk::platform_types::*;
|
2021-10-02 20:37:52 +10:00
|
|
|
use crate::vk::prelude::*;
|
2018-07-09 17:23:53 +10:00
|
|
|
#(#definition_code)*
|
2020-04-20 03:12:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let enum_code = quote! {
|
|
|
|
use std::fmt;
|
2018-07-07 20:54:31 +10:00
|
|
|
#(#enum_code)*
|
2020-04-20 03:12:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let bitflags_code = quote! {
|
|
|
|
use crate::vk::definitions::*;
|
2018-09-30 04:21:56 +10:00
|
|
|
#(#bitflags_code)*
|
2020-04-20 03:12:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let constants_code = quote! {
|
|
|
|
use crate::vk::definitions::*;
|
2018-07-09 17:23:53 +10:00
|
|
|
#(#constants_code)*
|
2020-04-20 03:12:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let extension_code = quote! {
|
|
|
|
use std::os::raw::*;
|
|
|
|
use crate::vk::platform_types::*;
|
|
|
|
use crate::vk::aliases::*;
|
|
|
|
use crate::vk::bitflags::*;
|
|
|
|
use crate::vk::definitions::*;
|
|
|
|
use crate::vk::enums::*;
|
2018-09-30 04:21:56 +10:00
|
|
|
#(#extension_code)*
|
2020-04-20 03:12:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let feature_extensions_code = quote! {
|
|
|
|
use crate::vk::bitflags::*;
|
|
|
|
use crate::vk::enums::*;
|
|
|
|
#feature_extensions_code
|
|
|
|
};
|
|
|
|
|
|
|
|
let const_debugs = quote! {
|
|
|
|
use std::fmt;
|
|
|
|
use crate::vk::bitflags::*;
|
|
|
|
use crate::vk::definitions::*;
|
|
|
|
use crate::vk::enums::*;
|
2019-04-22 09:04:49 +10:00
|
|
|
#const_debugs
|
2020-04-20 03:12:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let aliases = quote! {
|
|
|
|
use crate::vk::bitflags::*;
|
|
|
|
use crate::vk::definitions::*;
|
|
|
|
use crate::vk::enums::*;
|
2018-11-18 06:00:57 +11:00
|
|
|
#(#aliases)*
|
2018-03-11 02:21:35 +11:00
|
|
|
};
|
2020-04-20 03:12:17 +10:00
|
|
|
|
|
|
|
write!(&mut vk_macros_file, "{}", macros_code).expect("Unable to write vk/macros.rs");
|
|
|
|
write!(&mut vk_features_file, "{}", feature_code).expect("Unable to write vk/features.rs");
|
|
|
|
write!(&mut vk_definitions_file, "{}", definition_code)
|
|
|
|
.expect("Unable to write vk/definitions.rs");
|
|
|
|
write!(&mut vk_enums_file, "{}", enum_code).expect("Unable to write vk/enums.rs");
|
|
|
|
write!(&mut vk_bitflags_file, "{}", bitflags_code).expect("Unable to write vk/bitflags.rs");
|
|
|
|
write!(&mut vk_constants_file, "{}", constants_code).expect("Unable to write vk/constants.rs");
|
|
|
|
write!(&mut vk_extensions_file, "{}", extension_code)
|
|
|
|
.expect("Unable to write vk/extensions.rs");
|
|
|
|
write!(
|
|
|
|
&mut vk_feature_extensions_file,
|
|
|
|
"{}",
|
|
|
|
feature_extensions_code
|
|
|
|
)
|
|
|
|
.expect("Unable to write vk/feature_extensions.rs");
|
|
|
|
write!(&mut vk_const_debugs_file, "{}", const_debugs)
|
|
|
|
.expect("Unable to write vk/const_debugs.rs");
|
|
|
|
write!(&mut vk_aliases_file, "{}", aliases).expect("Unable to write vk/aliases.rs");
|
2021-06-06 19:00:29 +10:00
|
|
|
|
|
|
|
let vk_include = vk_headers_dir.join("include");
|
|
|
|
|
2021-09-08 19:26:58 +10:00
|
|
|
let mut bindings = bindgen::Builder::default().clang_arg(format!(
|
|
|
|
"-I{}",
|
|
|
|
vk_include.to_str().expect("Valid UTF8 string")
|
|
|
|
));
|
2021-06-06 19:00:29 +10:00
|
|
|
|
|
|
|
let (header_includes, header_types) = extract_native_types(&spec2);
|
|
|
|
|
|
|
|
for (_name, path) in header_includes {
|
2021-09-08 19:26:58 +10:00
|
|
|
let path = if path == "vk_platform.h" {
|
|
|
|
// Fix broken path, https://github.com/KhronosGroup/Vulkan-Docs/pull/1538
|
|
|
|
// Reintroduced in: https://github.com/KhronosGroup/Vulkan-Docs/issues/1573
|
|
|
|
vk_include.join("vulkan").join(path)
|
|
|
|
} else {
|
|
|
|
vk_include.join(path)
|
|
|
|
};
|
|
|
|
bindings = bindings.header(path.to_str().expect("Valid UTF8 string"));
|
2021-06-06 19:00:29 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
for typ in header_types {
|
|
|
|
bindings = bindings.allowlist_type(typ);
|
|
|
|
}
|
|
|
|
|
|
|
|
bindings
|
|
|
|
.generate()
|
|
|
|
.expect("Unable to generate native bindings")
|
|
|
|
.write_to_file(vk_dir.join("native.rs"))
|
|
|
|
.expect("Couldn't write native bindings!");
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|