From 3011d242f4e52e198785eaf58b97f263e81ec6d0 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 9 Nov 2020 09:46:00 +0100 Subject: [PATCH 01/29] Fix clippy::manual_strip --- generator/src/lib.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 14203514b..1df3eb07c 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -658,13 +658,7 @@ fn name_to_tokens(type_name: &str) -> Ident { "float" => "f32", "double" => "f64", "long" => "c_ulong", - _ => { - if type_name.starts_with("Vk") { - &type_name[2..] - } else { - type_name - } - } + _ => type_name.strip_prefix("Vk").unwrap_or(type_name), }; let new_name = new_name.replace("FlagBits", "Flags"); Ident::from(new_name.as_str()) @@ -1658,13 +1652,10 @@ pub fn derive_setters( return None; } - let mut param_ident_short = param_ident_string.as_str(); - if param_ident_string.starts_with("p_") { - param_ident_short = ¶m_ident_string[2..]; - }; - if param_ident_string.starts_with("pp_") { - param_ident_short = ¶m_ident_string[3..]; - }; + 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()); let param_ident_short = Term::intern(¶m_ident_short); if let Some(name) = field.name.as_ref() { @@ -1757,8 +1748,8 @@ pub fn derive_setters( } } - if param_ty_string.starts_with("*const ") { - let slice_param_ty_tokens = "&'a ".to_string() + ¶m_ty_string[7..]; + if let Some(param_ty_string) = param_ty_string.strip_prefix("*const ") { + let slice_param_ty_tokens = "&'a ".to_string() + param_ty_string; let slice_param_ty_tokens = Term::intern(&slice_param_ty_tokens); return Some(quote!{ pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { From d035f82c8d78a29716aea6bead7d34308318ee72 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 9 Nov 2020 09:55:02 +0100 Subject: [PATCH 02/29] Fix clippy::comparison_to_empty --- generator/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 1df3eb07c..ffced5822 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -1974,7 +1974,7 @@ pub fn generate_struct( } pub fn generate_handle(handle: &vkxml::Handle) -> Option { - if handle.name == "" { + if handle.name.is_empty() { return None; } let khronos_link = khronos_link(&handle.name); From f7c94510fabc365a2768d14e9270751f9b03d275 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 9 Nov 2020 09:55:41 +0100 Subject: [PATCH 03/29] Fix clippy::needless_lifetimes --- generator/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index ffced5822..7ae8f9aa0 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -2278,8 +2278,8 @@ pub fn generate_const_debugs(const_values: &BTreeMap( - types: &'a vk_parse::Types, +pub fn generate_aliases_of_types( + types: &vk_parse::Types, ty_cache: &mut HashSet, ) -> Tokens { let aliases = types From f078547d463d64940634e24c6b5a31b6da82d06c Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 24 Aug 2020 14:27:40 +0200 Subject: [PATCH 04/29] generator: Drop unnecessary edgecase for charptr array in builder --- ash/src/vk/definitions.rs | 8 ++++---- generator/src/lib.rs | 29 +++++++++-------------------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/ash/src/vk/definitions.rs b/ash/src/vk/definitions.rs index a8534936a..e855bf869 100644 --- a/ash/src/vk/definitions.rs +++ b/ash/src/vk/definitions.rs @@ -1543,16 +1543,16 @@ impl<'a> DeviceCreateInfoBuilder<'a> { mut self, enabled_layer_names: &'a [*const c_char], ) -> DeviceCreateInfoBuilder<'a> { - self.inner.pp_enabled_layer_names = enabled_layer_names.as_ptr(); self.inner.enabled_layer_count = enabled_layer_names.len() as _; + self.inner.pp_enabled_layer_names = enabled_layer_names.as_ptr(); self } pub fn enabled_extension_names( mut self, enabled_extension_names: &'a [*const c_char], ) -> DeviceCreateInfoBuilder<'a> { - self.inner.pp_enabled_extension_names = enabled_extension_names.as_ptr(); self.inner.enabled_extension_count = enabled_extension_names.len() as _; + self.inner.pp_enabled_extension_names = enabled_extension_names.as_ptr(); self } pub fn enabled_features( @@ -1654,16 +1654,16 @@ impl<'a> InstanceCreateInfoBuilder<'a> { mut self, enabled_layer_names: &'a [*const c_char], ) -> InstanceCreateInfoBuilder<'a> { - self.inner.pp_enabled_layer_names = enabled_layer_names.as_ptr(); self.inner.enabled_layer_count = enabled_layer_names.len() as _; + self.inner.pp_enabled_layer_names = enabled_layer_names.as_ptr(); self } pub fn enabled_extension_names( mut self, enabled_extension_names: &'a [*const c_char], ) -> InstanceCreateInfoBuilder<'a> { - self.inner.pp_enabled_extension_names = enabled_extension_names.as_ptr(); self.inner.enabled_extension_count = enabled_extension_names.len() as _; + self.inner.pp_enabled_extension_names = enabled_extension_names.as_ptr(); self } #[doc = r" Prepends the given extension struct between the root and the first pointer. This"] diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 7ae8f9aa0..84766840c 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -965,6 +965,7 @@ pub fn generate_extension_constants<'a>( if const_cache.contains(_enum.name.as_str()) { return None; } + let (constant, extends, is_alias) = match &_enum.spec { EnumSpec::Bitpos { bitpos, extends } => { Some((Constant::BitPos(*bitpos as u32), extends.clone(), false)) @@ -1696,20 +1697,10 @@ pub fn derive_setters( }); } - if let Some(ref array_type) = field.array { + if matches!(field.array, Some(vkxml::ArrayType::Dynamic)) { if let Some(ref array_size) = field.size { if !array_size.starts_with("latexmath") { let array_size_ident = Ident::from(array_size.to_snake_case().as_str()); - if param_ty_string == "*const *const c_char" { - return Some(quote!{ - pub fn #param_ident_short(mut self, #param_ident_short: &'a [*const c_char]) -> #name_builder<'a> { - self.inner.#param_ident = #param_ident_short.as_ptr(); - self.inner.#array_size_ident = #param_ident_short.len() as _; - self - } - }); - } - let slice_param_ty_tokens; let ptr; if param_ty_string.starts_with("*const ") { @@ -1735,15 +1726,13 @@ pub fn derive_setters( let slice_param_ty_tokens = Term::intern(&slice_param_ty_tokens); let ptr = Term::intern(ptr); - if let vkxml::ArrayType::Dynamic = array_type { - return Some(quote!{ - pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { - self.inner.#array_size_ident = #param_ident_short.len() as _; - self.inner.#param_ident = #param_ident_short#ptr; - self - } - }); - } + return Some(quote!{ + pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { + self.inner.#array_size_ident = #param_ident_short.len() as _; + self.inner.#param_ident = #param_ident_short#ptr; + self + } + }); } } } From abd30520bc8be657e67f3a258bafd78dcdb50a29 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 24 Aug 2020 15:05:31 +0200 Subject: [PATCH 05/29] generator: Make array type handling more linear --- generator/src/lib.rs | 66 +++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 84766840c..5aec367dd 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -5,7 +5,7 @@ use quote::*; use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; use itertools::Itertools; -use proc_macro2::{Literal, Term}; +use proc_macro2::{Literal, Term, TokenStream}; use quote::Tokens; use std::collections::{BTreeMap, HashMap, HashSet}; use std::fmt::Display; @@ -547,6 +547,7 @@ impl FeatureExt for vkxml::Feature { if version.len() == 1 { version = format!("{}_0", version) } + version.replace(".", "_") } } @@ -1159,7 +1160,6 @@ pub fn generate_define(define: &vkxml::Define) -> Tokens { if define.defref.contains(&"VK_MAKE_VERSION".to_string()) { let c_expr = c_expr.replace("VK_", ""); let c_expr = c_expr.replace("MAKE_VERSION", "crate::vk::make_version"); - use proc_macro2::TokenStream; use std::str::FromStr; let c_expr = TokenStream::from_str(&c_expr).unwrap(); quote!(pub const #ident: u32 = #c_expr;) @@ -1701,32 +1701,34 @@ pub fn derive_setters( if let Some(ref array_size) = field.size { if !array_size.starts_with("latexmath") { let array_size_ident = Ident::from(array_size.to_snake_case().as_str()); - let slice_param_ty_tokens; - let ptr; - if param_ty_string.starts_with("*const ") { - let slice_type = ¶m_ty_string[7..]; - if slice_type == "c_void" { - slice_param_ty_tokens = "&'a [u8]".to_string(); - ptr = ".as_ptr() as *const c_void"; - } else { - slice_param_ty_tokens = "&'a [".to_string() + slice_type + "]"; - ptr = ".as_ptr()"; - } + + let param_ty = param_ty_string.splitn(2, ' ').collect_vec(); + assert_eq!(param_ty.len(), 2); + let mutable = match param_ty[0] { + "*const" => false, + "*mut" => true, + _ => unreachable!("Unexpected const-ness type: {}", param_ty[0]), + }; + let mut slice_type = param_ty[1].parse::().unwrap(); + + let mut ptr = if mutable { + quote!(.as_mut_ptr()) } else { - // *mut - let slice_type = ¶m_ty_string[5..]; - if slice_type == "c_void" { - slice_param_ty_tokens = "&'a mut [u8]".to_string(); - ptr = ".as_mut_ptr() as *mut c_void"; - } else { - slice_param_ty_tokens = "&'a mut [".to_string() + slice_type + "]"; - ptr = ".as_mut_ptr()"; - } - } - let slice_param_ty_tokens = Term::intern(&slice_param_ty_tokens); - let ptr = Term::intern(ptr); + quote!(.as_ptr()) + }; - return Some(quote!{ + // Interpret void array as byte array + if param_ty[1] == "c_void" { + let mutable = if mutable { quote!(mut) } else { quote!(const) }; + + slice_type = quote!(u8).into(); + ptr = quote!(#ptr as *#mutable c_void); + }; + + let mutable = if mutable { quote!(mut) } else { quote!() }; + let slice_param_ty_tokens = quote!(&'a #mutable [#slice_type]); + + return Some(quote! { pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { self.inner.#array_size_ident = #param_ident_short.len() as _; self.inner.#param_ident = #param_ident_short#ptr; @@ -1738,13 +1740,13 @@ pub fn derive_setters( } if let Some(param_ty_string) = param_ty_string.strip_prefix("*const ") { - let slice_param_ty_tokens = "&'a ".to_string() + param_ty_string; - let slice_param_ty_tokens = Term::intern(&slice_param_ty_tokens); + let slice_param_ty_tokens = param_ty_string.parse::().unwrap(); + let slice_param_ty_tokens = quote!(&'a #slice_param_ty_tokens); return Some(quote!{ - pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { - self.inner.#param_ident = #param_ident_short; - self - } + pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { + self.inner.#param_ident = #param_ident_short; + self + } }); } } From 88035705d1f7ddafc3973c35eecee3123e68e7d3 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 24 Aug 2020 17:28:13 +0200 Subject: [PATCH 06/29] prelude: Provide Result -> VkResult<()> conversion --- ash/src/device.rs | 223 ++++++------------- ash/src/extensions/ext/debug_marker.rs | 10 +- ash/src/extensions/ext/debug_utils.rs | 20 +- ash/src/extensions/khr/ray_tracing.rs | 58 ++--- ash/src/extensions/khr/timeline_semaphore.rs | 22 +- ash/src/extensions/nv/ray_tracing.rs | 48 ++-- ash/src/instance.rs | 32 +-- ash/src/prelude.rs | 9 + 8 files changed, 143 insertions(+), 279 deletions(-) diff --git a/ash/src/device.rs b/ash/src/device.rs index fce4c1536..9006c3cb9 100644 --- a/ash/src/device.rs +++ b/ash/src/device.rs @@ -137,22 +137,16 @@ pub trait DeviceV1_2: DeviceV1_1 { wait_info: &vk::SemaphoreWaitInfo, timeout: u64, ) -> VkResult<()> { - let err_code = self - .fp_v1_2() - .wait_semaphores(self.handle(), wait_info, timeout); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_2() + .wait_semaphores(self.handle(), wait_info, timeout) + .into() } #[doc = ""] unsafe fn signal_semaphore(&self, signal_info: &vk::SemaphoreSignalInfo) -> VkResult<()> { - let err_code = self.fp_v1_2().signal_semaphore(self.handle(), signal_info); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_2() + .signal_semaphore(self.handle(), signal_info) + .into() } #[doc = ""] @@ -186,28 +180,16 @@ pub trait DeviceV1_1: DeviceV1_0 { #[doc = ""] unsafe fn bind_buffer_memory2(&self, bind_infos: &[vk::BindBufferMemoryInfo]) -> VkResult<()> { - let err_code = self.fp_v1_1().bind_buffer_memory2( - self.handle(), - bind_infos.len() as _, - bind_infos.as_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_1() + .bind_buffer_memory2(self.handle(), bind_infos.len() as _, bind_infos.as_ptr()) + .into() } #[doc = ""] unsafe fn bind_image_memory2(&self, bind_infos: &[vk::BindImageMemoryInfo]) -> VkResult<()> { - let err_code = self.fp_v1_1().bind_image_memory2( - self.handle(), - bind_infos.len() as _, - bind_infos.as_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_1() + .bind_image_memory2(self.handle(), bind_infos.len() as _, bind_infos.as_ptr()) + .into() } #[doc = ""] @@ -482,20 +464,12 @@ pub trait DeviceV1_0 { #[doc = ""] unsafe fn set_event(&self, event: vk::Event) -> VkResult<()> { - let err_code = self.fp_v1_0().set_event(self.handle(), event); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0().set_event(self.handle(), event).into() } #[doc = ""] unsafe fn reset_event(&self, event: vk::Event) -> VkResult<()> { - let err_code = self.fp_v1_0().reset_event(self.handle(), event); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0().reset_event(self.handle(), event).into() } #[doc = ""] unsafe fn cmd_set_event( @@ -969,11 +943,7 @@ pub trait DeviceV1_0 { #[doc = ""] unsafe fn device_wait_idle(&self) -> VkResult<()> { - let err_code = self.fp_v1_0().device_wait_idle(self.handle()); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0().device_wait_idle(self.handle()).into() } #[doc = ""] @@ -1001,13 +971,9 @@ pub trait DeviceV1_0 { pool: vk::DescriptorPool, flags: vk::DescriptorPoolResetFlags, ) -> VkResult<()> { - let err_code = self - .fp_v1_0() - .reset_descriptor_pool(self.handle(), pool, flags); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .reset_descriptor_pool(self.handle(), pool, flags) + .into() } #[doc = ""] @@ -1016,13 +982,9 @@ pub trait DeviceV1_0 { command_pool: vk::CommandPool, flags: vk::CommandPoolResetFlags, ) -> VkResult<()> { - let err_code = self - .fp_v1_0() - .reset_command_pool(self.handle(), command_pool, flags); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .reset_command_pool(self.handle(), command_pool, flags) + .into() } #[doc = ""] @@ -1031,22 +993,16 @@ pub trait DeviceV1_0 { command_buffer: vk::CommandBuffer, flags: vk::CommandBufferResetFlags, ) -> VkResult<()> { - let err_code = self.fp_v1_0().reset_command_buffer(command_buffer, flags); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .reset_command_buffer(command_buffer, flags) + .into() } #[doc = ""] unsafe fn reset_fences(&self, fences: &[vk::Fence]) -> VkResult<()> { - let err_code = - self.fp_v1_0() - .reset_fences(self.handle(), fences.len() as u32, fences.as_ptr()); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .reset_fences(self.handle(), fences.len() as u32, fences.as_ptr()) + .into() } #[doc = ""] @@ -1460,21 +1416,18 @@ pub trait DeviceV1_0 { "query_count was higher than the length of the slice" ); let data_size = mem::size_of::() * data_length; - let err_code = self.fp_v1_0().get_query_pool_results( - self.handle(), - query_pool, - first_query, - query_count, - data_size, - data.as_mut_ptr() as *mut _, - mem::size_of::() as _, - flags, - ); - - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .get_query_pool_results( + self.handle(), + query_pool, + first_query, + query_count, + data_size, + data.as_mut_ptr() as *mut _, + mem::size_of::() as _, + flags, + ) + .into() } #[doc = ""] @@ -1661,7 +1614,7 @@ pub trait DeviceV1_0 { ); if err_code != vk::Result::SUCCESS { return Err(err_code); - }; + } let mut data: Vec = Vec::with_capacity(data_size); let err_code = self.fp_v1_0().get_pipeline_cache_data( self.handle(), @@ -1704,28 +1657,16 @@ pub trait DeviceV1_0 { &self, ranges: &[vk::MappedMemoryRange], ) -> VkResult<()> { - let err_code = self.fp_v1_0().invalidate_mapped_memory_ranges( - self.handle(), - ranges.len() as u32, - ranges.as_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .invalidate_mapped_memory_ranges(self.handle(), ranges.len() as u32, ranges.as_ptr()) + .into() } #[doc = ""] unsafe fn flush_mapped_memory_ranges(&self, ranges: &[vk::MappedMemoryRange]) -> VkResult<()> { - let err_code = self.fp_v1_0().flush_mapped_memory_ranges( - self.handle(), - ranges.len() as u32, - ranges.as_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .flush_mapped_memory_ranges(self.handle(), ranges.len() as u32, ranges.as_ptr()) + .into() } #[doc = ""] @@ -1805,22 +1746,14 @@ pub trait DeviceV1_0 { command_buffer: vk::CommandBuffer, begin_info: &vk::CommandBufferBeginInfo, ) -> VkResult<()> { - let err_code = self - .fp_v1_0() - .begin_command_buffer(command_buffer, begin_info); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .begin_command_buffer(command_buffer, begin_info) + .into() } #[doc = ""] unsafe fn end_command_buffer(&self, command_buffer: vk::CommandBuffer) -> VkResult<()> { - let err_code = self.fp_v1_0().end_command_buffer(command_buffer); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0().end_command_buffer(command_buffer).into() } #[doc = ""] @@ -1830,17 +1763,15 @@ pub trait DeviceV1_0 { wait_all: bool, timeout: u64, ) -> VkResult<()> { - let err_code = self.fp_v1_0().wait_for_fences( - self.handle(), - fences.len() as u32, - fences.as_ptr(), - wait_all as u32, - timeout, - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .wait_for_fences( + self.handle(), + fences.len() as u32, + fences.as_ptr(), + wait_all as u32, + timeout, + ) + .into() } #[doc = ""] @@ -1855,11 +1786,7 @@ pub trait DeviceV1_0 { #[doc = ""] unsafe fn queue_wait_idle(&self, queue: vk::Queue) -> VkResult<()> { - let err_code = self.fp_v1_0().queue_wait_idle(queue); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0().queue_wait_idle(queue).into() } #[doc = ""] @@ -1869,13 +1796,9 @@ pub trait DeviceV1_0 { submits: &[vk::SubmitInfo], fence: vk::Fence, ) -> VkResult<()> { - let err_code = - self.fp_v1_0() - .queue_submit(queue, submits.len() as u32, submits.as_ptr(), fence); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .queue_submit(queue, submits.len() as u32, submits.as_ptr(), fence) + .into() } #[doc = ""] @@ -2100,13 +2023,9 @@ pub trait DeviceV1_0 { device_memory: vk::DeviceMemory, offset: vk::DeviceSize, ) -> VkResult<()> { - let err_code = - self.fp_v1_0() - .bind_buffer_memory(self.handle(), buffer, device_memory, offset); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .bind_buffer_memory(self.handle(), buffer, device_memory, offset) + .into() } #[doc = ""] @@ -2116,13 +2035,9 @@ pub trait DeviceV1_0 { device_memory: vk::DeviceMemory, offset: vk::DeviceSize, ) -> VkResult<()> { - let err_code = - self.fp_v1_0() - .bind_image_memory(self.handle(), image, device_memory, offset); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.fp_v1_0() + .bind_image_memory(self.handle(), image, device_memory, offset) + .into() } } diff --git a/ash/src/extensions/ext/debug_marker.rs b/ash/src/extensions/ext/debug_marker.rs index 626f23ab5..5218d567c 100755 --- a/ash/src/extensions/ext/debug_marker.rs +++ b/ash/src/extensions/ext/debug_marker.rs @@ -28,13 +28,9 @@ impl DebugMarker { device: vk::Device, name_info: &vk::DebugMarkerObjectNameInfoEXT, ) -> VkResult<()> { - let err_code = self - .debug_marker_fn - .debug_marker_set_object_name_ext(device, name_info); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.debug_marker_fn + .debug_marker_set_object_name_ext(device, name_info) + .into() } #[doc = ""] diff --git a/ash/src/extensions/ext/debug_utils.rs b/ash/src/extensions/ext/debug_utils.rs index fdafd3d2f..0e7137780 100755 --- a/ash/src/extensions/ext/debug_utils.rs +++ b/ash/src/extensions/ext/debug_utils.rs @@ -32,13 +32,9 @@ impl DebugUtils { device: vk::Device, name_info: &vk::DebugUtilsObjectNameInfoEXT, ) -> VkResult<()> { - let err_code = self - .debug_utils_fn - .set_debug_utils_object_name_ext(device, name_info); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.debug_utils_fn + .set_debug_utils_object_name_ext(device, name_info) + .into() } #[doc = ""] @@ -47,13 +43,9 @@ impl DebugUtils { device: vk::Device, tag_info: &vk::DebugUtilsObjectTagInfoEXT, ) -> VkResult<()> { - let err_code = self - .debug_utils_fn - .set_debug_utils_object_tag_ext(device, tag_info); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.debug_utils_fn + .set_debug_utils_object_tag_ext(device, tag_info) + .into() } #[doc = ""] diff --git a/ash/src/extensions/khr/ray_tracing.rs b/ash/src/extensions/khr/ray_tracing.rs index 76a301128..ca6ab0cf3 100644 --- a/ash/src/extensions/khr/ray_tracing.rs +++ b/ash/src/extensions/khr/ray_tracing.rs @@ -87,15 +87,13 @@ impl RayTracing { &self, bind_info: &[vk::BindAccelerationStructureMemoryInfoKHR], ) -> VkResult<()> { - let err_code = self.ray_tracing_fn.bind_acceleration_structure_memory_khr( - self.handle, - bind_info.len() as u32, - bind_info.as_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.ray_tracing_fn + .bind_acceleration_structure_memory_khr( + self.handle, + bind_info.len() as u32, + bind_info.as_ptr(), + ) + .into() } #[doc = ""] @@ -182,8 +180,7 @@ impl RayTracing { group_count: u32, data: &mut [u8], ) -> VkResult<()> { - let err_code = self - .ray_tracing_fn + self.ray_tracing_fn .get_ray_tracing_shader_group_handles_khr( self.handle, pipeline, @@ -191,11 +188,8 @@ impl RayTracing { group_count, data.len(), data.as_mut_ptr() as *mut std::ffi::c_void, - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + ) + .into() } #[doc = ""] @@ -250,13 +244,9 @@ impl RayTracing { device: vk::Device, info: &vk::CopyAccelerationStructureToMemoryInfoKHR, ) -> VkResult<()> { - let err_code = self - .ray_tracing_fn - .copy_acceleration_structure_to_memory_khr(device, info); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.ray_tracing_fn + .copy_acceleration_structure_to_memory_khr(device, info) + .into() } pub unsafe fn copy_memory_to_acceleration_structure( @@ -264,14 +254,9 @@ impl RayTracing { device: vk::Device, info: &vk::CopyMemoryToAccelerationStructureInfoKHR, ) -> VkResult<()> { - let err_code = self - .ray_tracing_fn - .copy_memory_to_acceleration_structure_khr(device, info); - - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.ray_tracing_fn + .copy_memory_to_acceleration_structure_khr(device, info) + .into() } pub unsafe fn cmd_copy_acceleration_structure_to_memory( @@ -345,14 +330,9 @@ impl RayTracing { device: vk::Device, version: &vk::AccelerationStructureVersionKHR, ) -> VkResult<()> { - let err_code = self - .ray_tracing_fn - .get_device_acceleration_structure_compatibility_khr(device, version); - - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.ray_tracing_fn + .get_device_acceleration_structure_compatibility_khr(device, version) + .into() } pub fn name() -> &'static CStr { diff --git a/ash/src/extensions/khr/timeline_semaphore.rs b/ash/src/extensions/khr/timeline_semaphore.rs index 57ab92fe7..7a3ea80e5 100644 --- a/ash/src/extensions/khr/timeline_semaphore.rs +++ b/ash/src/extensions/khr/timeline_semaphore.rs @@ -51,14 +51,9 @@ impl TimelineSemaphore { wait_info: &vk::SemaphoreWaitInfo, timeout: u64, ) -> VkResult<()> { - let err_code = self - .timeline_semaphore_fn - .wait_semaphores_khr(device, wait_info, timeout); - - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.timeline_semaphore_fn + .wait_semaphores_khr(device, wait_info, timeout) + .into() } #[doc = ""] @@ -67,14 +62,9 @@ impl TimelineSemaphore { device: vk::Device, signal_info: &vk::SemaphoreSignalInfo, ) -> VkResult<()> { - let err_code = self - .timeline_semaphore_fn - .signal_semaphore_khr(device, signal_info); - - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.timeline_semaphore_fn + .signal_semaphore_khr(device, signal_info) + .into() } pub fn fp(&self) -> &vk::KhrTimelineSemaphoreFn { diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index af55f6bed..c1d748a91 100755 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -87,15 +87,13 @@ impl RayTracing { &self, bind_info: &[vk::BindAccelerationStructureMemoryInfoNV], ) -> VkResult<()> { - let err_code = self.ray_tracing_fn.bind_acceleration_structure_memory_nv( - self.handle, - bind_info.len() as u32, - bind_info.as_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.ray_tracing_fn + .bind_acceleration_structure_memory_nv( + self.handle, + bind_info.len() as u32, + bind_info.as_ptr(), + ) + .into() } #[doc = ""] @@ -204,18 +202,16 @@ impl RayTracing { group_count: u32, data: &mut [u8], ) -> VkResult<()> { - let err_code = self.ray_tracing_fn.get_ray_tracing_shader_group_handles_nv( - self.handle, - pipeline, - first_group, - group_count, - data.len(), - data.as_mut_ptr() as *mut std::ffi::c_void, - ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.ray_tracing_fn + .get_ray_tracing_shader_group_handles_nv( + self.handle, + pipeline, + first_group, + group_count, + data.len(), + data.as_mut_ptr() as *mut std::ffi::c_void, + ) + .into() } #[doc = ""] @@ -259,13 +255,9 @@ impl RayTracing { #[doc = ""] pub unsafe fn compile_deferred(&self, pipeline: vk::Pipeline, shader: u32) -> VkResult<()> { - let err_code = self - .ray_tracing_fn - .compile_deferred_nv(self.handle, pipeline, shader); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + self.ray_tracing_fn + .compile_deferred_nv(self.handle, pipeline, shader) + .into() } pub fn name() -> &'static CStr { diff --git a/ash/src/instance.rs b/ash/src/instance.rs index 669eed47b..07e4acba7 100644 --- a/ash/src/instance.rs +++ b/ash/src/instance.rs @@ -49,7 +49,7 @@ impl InstanceV1_0 for Instance { physical_device: vk::PhysicalDevice, create_info: &vk::DeviceCreateInfo, allocation_callbacks: Option<&vk::AllocationCallbacks>, - ) -> Result { + ) -> VkResult { let mut device: vk::Device = mem::zeroed(); let err_code = self.fp_v1_0().create_device( physical_device, @@ -109,16 +109,9 @@ pub trait InstanceV1_1: InstanceV1_0 { ) -> VkResult<()> { unsafe { let mut group_count = out.len() as u32; - let err_code = self.fp_v1_1().enumerate_physical_device_groups( - self.handle(), - &mut group_count, - out.as_mut_ptr(), - ); - if err_code == vk::Result::SUCCESS { - Ok(()) - } else { - Err(err_code) - } + self.fp_v1_1() + .enumerate_physical_device_groups(self.handle(), &mut group_count, out.as_mut_ptr()) + .into() } } @@ -160,16 +153,13 @@ pub trait InstanceV1_1: InstanceV1_0 { format_info: &vk::PhysicalDeviceImageFormatInfo2, image_format_prop: &mut vk::ImageFormatProperties2, ) -> VkResult<()> { - let err_code = self.fp_v1_1().get_physical_device_image_format_properties2( - physical_device, - format_info, - image_format_prop, - ); - if err_code == vk::Result::SUCCESS { - Ok(()) - } else { - Err(err_code) - } + self.fp_v1_1() + .get_physical_device_image_format_properties2( + physical_device, + format_info, + image_format_prop, + ) + .into() } unsafe fn get_physical_device_queue_family_properties2_len( diff --git a/ash/src/prelude.rs b/ash/src/prelude.rs index b4a888cd5..c794ed8c1 100644 --- a/ash/src/prelude.rs +++ b/ash/src/prelude.rs @@ -1,2 +1,11 @@ use crate::vk; pub type VkResult = Result; + +impl From for VkResult<()> { + fn from(err_code: vk::Result) -> Self { + match err_code { + vk::Result::SUCCESS => Ok(()), + _ => Err(err_code), + } + } +} From 6110c23af3437ab28eeb3052ac80e34c84b5fe28 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 24 Aug 2020 17:28:55 +0200 Subject: [PATCH 07/29] Add #[must_use] to Result type --- ash/src/vk/enums.rs | 1 + generator/src/lib.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ash/src/vk/enums.rs b/ash/src/vk/enums.rs index 8d8a1f06e..f473274f6 100644 --- a/ash/src/vk/enums.rs +++ b/ash/src/vk/enums.rs @@ -869,6 +869,7 @@ impl SubpassContents { #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] #[repr(transparent)] #[doc = ""] +#[must_use] pub struct Result(pub(crate) i32); impl Result { pub const fn from_raw(x: i32) -> Self { diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 5aec367dd..8e0ba9f16 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -1346,11 +1346,18 @@ pub fn generate_enum<'a>( EnumType::Bitflags(q) } } else { + let (struct_attribute, special_quote) = match _name.as_str() { + //"StructureType" => generate_structure_type(&_name, _enum, create_info_constants), + "Result" => (quote!(#[must_use]), generate_result(ident, _enum)), + _ => (quote!(), quote!()), + }; + let impl_block = bitflags_impl_block(ident, &_enum.name, &constants); let enum_quote = quote! { #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] #[repr(transparent)] #[doc = #khronos_link] + #struct_attribute pub struct #ident(pub(crate) i32); impl #ident { pub const fn from_raw(x: i32) -> Self { #ident(x) } @@ -1358,13 +1365,6 @@ pub fn generate_enum<'a>( } #impl_block }; - let special_quote = match _name.as_str() { - //"StructureType" => generate_structure_type(&_name, _enum, create_info_constants), - "Result" => generate_result(ident, _enum), - _ => { - quote! {} - } - }; let q = quote! { #enum_quote #special_quote From 7e2e938d1ddd63d69bc1d946c32de3e086611cca Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 25 Aug 2020 11:28:46 +0200 Subject: [PATCH 08/29] Fix all unchecked vk::Result cases --- ash/src/device.rs | 16 ++++++++------ ash/src/entry.rs | 12 +++++++--- ash/src/extensions/ext/tooling_info.rs | 6 ++++- ash/src/extensions/khr/display.rs | 21 ++++++++++++++---- .../khr/pipeline_executable_properties.rs | 18 ++++++++++++--- ash/src/extensions/khr/surface.rs | 11 ++++++++-- ash/src/extensions/khr/swapchain.rs | 5 ++++- ash/src/instance.rs | 22 ++++++++++++++----- 8 files changed, 84 insertions(+), 27 deletions(-) diff --git a/ash/src/device.rs b/ash/src/device.rs index 9006c3cb9..53886e066 100644 --- a/ash/src/device.rs +++ b/ash/src/device.rs @@ -710,13 +710,15 @@ pub trait DeviceV1_0 { &self, pool: vk::DescriptorPool, descriptor_sets: &[vk::DescriptorSet], - ) { - self.fp_v1_0().free_descriptor_sets( - self.handle(), - pool, - descriptor_sets.len() as u32, - descriptor_sets.as_ptr(), - ); + ) -> VkResult<()> { + self.fp_v1_0() + .free_descriptor_sets( + self.handle(), + pool, + descriptor_sets.len() as u32, + descriptor_sets.as_ptr(), + ) + .into() } #[doc = ""] diff --git a/ash/src/entry.rs b/ash/src/entry.rs index d81234142..261a2e7b6 100644 --- a/ash/src/entry.rs +++ b/ash/src/entry.rs @@ -57,9 +57,12 @@ pub trait EntryV1_0 { fn enumerate_instance_layer_properties(&self) -> VkResult> { unsafe { let mut num = 0; - self.fp_v1_0() + let err_code = self + .fp_v1_0() .enumerate_instance_layer_properties(&mut num, ptr::null_mut()); - + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(num as usize); let err_code = self .fp_v1_0() @@ -76,11 +79,14 @@ pub trait EntryV1_0 { fn enumerate_instance_extension_properties(&self) -> VkResult> { unsafe { let mut num = 0; - self.fp_v1_0().enumerate_instance_extension_properties( + let err_code = self.fp_v1_0().enumerate_instance_extension_properties( ptr::null(), &mut num, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut data = Vec::with_capacity(num as usize); let err_code = self.fp_v1_0().enumerate_instance_extension_properties( ptr::null(), diff --git a/ash/src/extensions/ext/tooling_info.rs b/ash/src/extensions/ext/tooling_info.rs index 4212c8899..e42e30f08 100644 --- a/ash/src/extensions/ext/tooling_info.rs +++ b/ash/src/extensions/ext/tooling_info.rs @@ -33,8 +33,12 @@ impl ToolingInfo { physical_device: vk::PhysicalDevice, ) -> VkResult> { let mut count = 0; - self.tooling_info_fn + let err_code = self + .tooling_info_fn .get_physical_device_tool_properties_ext(physical_device, &mut count, ptr::null_mut()); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(count as usize); let err_code = self .tooling_info_fn diff --git a/ash/src/extensions/khr/display.rs b/ash/src/extensions/khr/display.rs index 8ea5e6257..be4db5d4d 100755 --- a/ash/src/extensions/khr/display.rs +++ b/ash/src/extensions/khr/display.rs @@ -33,11 +33,14 @@ impl Display { physical_device: vk::PhysicalDevice, ) -> VkResult> { let mut count = 0; - self.display_fn.get_physical_device_display_properties_khr( + let err_code = self.display_fn.get_physical_device_display_properties_khr( physical_device, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(count as usize); let err_code = self.display_fn.get_physical_device_display_properties_khr( physical_device, @@ -57,12 +60,16 @@ impl Display { physical_device: vk::PhysicalDevice, ) -> VkResult> { let mut count = 0; - self.display_fn + let err_code = self + .display_fn .get_physical_device_display_plane_properties_khr( physical_device, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(count as usize); let err_code = self .display_fn @@ -85,12 +92,15 @@ impl Display { plane_index: u32, ) -> VkResult> { let mut count = 0; - self.display_fn.get_display_plane_supported_displays_khr( + let err_code = self.display_fn.get_display_plane_supported_displays_khr( physical_device, plane_index, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(count as usize); let err_code = self.display_fn.get_display_plane_supported_displays_khr( physical_device, @@ -112,12 +122,15 @@ impl Display { display: vk::DisplayKHR, ) -> VkResult> { let mut count = 0; - self.display_fn.get_display_mode_properties_khr( + let err_code = self.display_fn.get_display_mode_properties_khr( physical_device, display, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(count as usize); let err_code = self.display_fn.get_display_mode_properties_khr( physical_device, diff --git a/ash/src/extensions/khr/pipeline_executable_properties.rs b/ash/src/extensions/khr/pipeline_executable_properties.rs index 493be7f03..65c723b8a 100644 --- a/ash/src/extensions/khr/pipeline_executable_properties.rs +++ b/ash/src/extensions/khr/pipeline_executable_properties.rs @@ -39,13 +39,17 @@ impl PipelineExecutableProperties { executable_info: &vk::PipelineExecutableInfoKHR, ) -> VkResult> { let mut count = 0; - self.pipeline_executable_properties_fn + let err_code = self + .pipeline_executable_properties_fn .get_pipeline_executable_internal_representations_khr( device, executable_info, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v: Vec<_> = vec![Default::default(); count as usize]; let err_code = self .pipeline_executable_properties_fn @@ -68,13 +72,17 @@ impl PipelineExecutableProperties { pipeline_info: &vk::PipelineInfoKHR, ) -> VkResult> { let mut count = 0; - self.pipeline_executable_properties_fn + let err_code = self + .pipeline_executable_properties_fn .get_pipeline_executable_properties_khr( device, pipeline_info, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v: Vec<_> = vec![Default::default(); count as usize]; let err_code = self .pipeline_executable_properties_fn @@ -97,13 +105,17 @@ impl PipelineExecutableProperties { executable_info: &vk::PipelineExecutableInfoKHR, ) -> VkResult> { let mut count = 0; - self.pipeline_executable_properties_fn + let err_code = self + .pipeline_executable_properties_fn .get_pipeline_executable_statistics_khr( device, executable_info, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v: Vec<_> = vec![Default::default(); count as usize]; let err_code = self .pipeline_executable_properties_fn diff --git a/ash/src/extensions/khr/surface.rs b/ash/src/extensions/khr/surface.rs index 7cde5aa9f..66118ca51 100755 --- a/ash/src/extensions/khr/surface.rs +++ b/ash/src/extensions/khr/surface.rs @@ -56,13 +56,17 @@ impl Surface { surface: vk::SurfaceKHR, ) -> VkResult> { let mut count = 0; - self.surface_fn + let err_code = self + .surface_fn .get_physical_device_surface_present_modes_khr( physical_device, surface, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(count as usize); let err_code = self .surface_fn @@ -106,12 +110,15 @@ impl Surface { surface: vk::SurfaceKHR, ) -> VkResult> { let mut count = 0; - self.surface_fn.get_physical_device_surface_formats_khr( + let err_code = self.surface_fn.get_physical_device_surface_formats_khr( physical_device, surface, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(count as usize); let err_code = self.surface_fn.get_physical_device_surface_formats_khr( physical_device, diff --git a/ash/src/extensions/khr/swapchain.rs b/ash/src/extensions/khr/swapchain.rs index 266b9875e..35e153edb 100755 --- a/ash/src/extensions/khr/swapchain.rs +++ b/ash/src/extensions/khr/swapchain.rs @@ -106,12 +106,15 @@ impl Swapchain { swapchain: vk::SwapchainKHR, ) -> VkResult> { let mut count = 0; - self.swapchain_fn.get_swapchain_images_khr( + let err_code = self.swapchain_fn.get_swapchain_images_khr( self.handle, swapchain, &mut count, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut v = Vec::with_capacity(count as usize); let err_code = self.swapchain_fn.get_swapchain_images_khr( diff --git a/ash/src/instance.rs b/ash/src/instance.rs index 07e4acba7..fc64e5d4e 100644 --- a/ash/src/instance.rs +++ b/ash/src/instance.rs @@ -92,14 +92,17 @@ pub trait InstanceV1_2: InstanceV1_1 { pub trait InstanceV1_1: InstanceV1_0 { fn fp_v1_1(&self) -> &vk::InstanceFnV1_1; - unsafe fn enumerate_physical_device_groups_len(&self) -> usize { + unsafe fn enumerate_physical_device_groups_len(&self) -> VkResult { let mut group_count = mem::zeroed(); - self.fp_v1_1().enumerate_physical_device_groups( + let err_code = self.fp_v1_1().enumerate_physical_device_groups( self.handle(), &mut group_count, ptr::null_mut(), ); - group_count as usize + match err_code { + vk::Result::SUCCESS => Ok(group_count as usize), + _ => Err(err_code), + } } #[doc = ""] @@ -410,8 +413,12 @@ pub trait InstanceV1_0 { #[doc = ""] unsafe fn enumerate_physical_devices(&self) -> VkResult> { let mut num = mem::zeroed(); - self.fp_v1_0() - .enumerate_physical_devices(self.handle(), &mut num, ptr::null_mut()); + let err_code = + self.fp_v1_0() + .enumerate_physical_devices(self.handle(), &mut num, ptr::null_mut()); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut physical_devices = Vec::::with_capacity(num as usize); let err_code = self.fp_v1_0().enumerate_physical_devices( self.handle(), @@ -431,12 +438,15 @@ pub trait InstanceV1_0 { device: vk::PhysicalDevice, ) -> Result, vk::Result> { let mut num = 0; - self.fp_v1_0().enumerate_device_extension_properties( + let err_code = self.fp_v1_0().enumerate_device_extension_properties( device, ptr::null(), &mut num, ptr::null_mut(), ); + if err_code != vk::Result::SUCCESS { + return Err(err_code); + } let mut data = Vec::with_capacity(num as usize); let err_code = self.fp_v1_0().enumerate_device_extension_properties( device, From 5a98daa88cc3b849f2cc57812e231648506723ae Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 24 Aug 2020 16:11:34 +0200 Subject: [PATCH 09/29] generator: Fix typos --- generator/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 8e0ba9f16..bda41465e 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -359,7 +359,7 @@ pub fn platform_specific_types() -> Tokens { // typedefs are only here so that the code compiles for now #[allow(non_camel_case_types)] pub type SECURITY_ATTRIBUTES = (); - // Opage types + // Opaque types pub type ANativeWindow = c_void; pub type AHardwareBuffer = c_void; pub type CAMetalLayer = c_void; @@ -603,7 +603,7 @@ impl CommandExt for vkxml::Command { } pub trait FieldExt { - /// Returns the name of the paramter that doesn't clash with Rusts resevered + /// Returns the name of the parameter that doesn't clash with Rusts reserved /// keywords fn param_ident(&self) -> Ident; @@ -1438,9 +1438,9 @@ pub fn derive_default(_struct: &vkxml::Struct) -> Option { // also doesn't mark them as pointers let handles = ["LPCWSTR", "HANDLE", "HINSTANCE", "HWND", "HMONITOR"]; let contains_ptr = members.clone().any(|field| field.reference.is_some()); - let contains_strucutre_type = members.clone().any(is_structure_type); + let contains_structure_type = members.clone().any(is_structure_type); let contains_static_array = members.clone().any(is_static_array); - if !(contains_ptr || contains_strucutre_type || contains_static_array) { + if !(contains_ptr || contains_structure_type || contains_static_array) { return None; }; let default_fields = members.clone().map(|field| { From f0f012ded4d85bca603bf043a4da0f748b0483fc Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 24 Aug 2020 16:01:26 +0200 Subject: [PATCH 10/29] generator: Assert char ptrs have `"null-terminated"` len attribute --- generator/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index bda41465e..36f10a199 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -1689,6 +1689,8 @@ pub fn derive_setters( // TODO: Improve in future when https://github.com/rust-lang/rust/issues/53667 is merged id:6 if param_ident_string.starts_with("p_") || param_ident_string.starts_with("pp_") { if param_ty_string == "*const c_char" { + assert!(field.null_terminate); + assert_eq!(field.size, None); return Some(quote!{ pub fn #param_ident_short(mut self, #param_ident_short: &'a ::std::ffi::CStr) -> #name_builder<'a> { self.inner.#param_ident = #param_ident_short.as_ptr(); From e8305d41785ed6d66abdf6a79bae368af45246ba Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 16 Nov 2020 14:50:11 +0100 Subject: [PATCH 11/29] prelude: Provide result_with_success helper for Result -> VKresult --- ash/src/prelude.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ash/src/prelude.rs b/ash/src/prelude.rs index c794ed8c1..112b0d418 100644 --- a/ash/src/prelude.rs +++ b/ash/src/prelude.rs @@ -3,9 +3,19 @@ pub type VkResult = Result; impl From for VkResult<()> { fn from(err_code: vk::Result) -> Self { - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), + err_code.result() + } +} + +impl vk::Result { + pub fn result(self) -> VkResult<()> { + self.result_with_success(()) + } + + pub fn result_with_success(self, v: T) -> VkResult { + match self { + vk::Result::SUCCESS => Ok(v), + _ => Err(self), } } } From 62961b4a23b9c9848d269ab0fd9a51460bf14596 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 16 Nov 2020 20:08:11 +0100 Subject: [PATCH 12/29] Cleanup match{success} blocks with SSR {let $p = $f; match $q { vk::Result::SUCCESS => Ok($r), _ => Err($z), }} ==>> {$f.result_with_success($r)} --- ash/src/entry.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ash/src/entry.rs b/ash/src/entry.rs index 261a2e7b6..b640ae058 100644 --- a/ash/src/entry.rs +++ b/ash/src/entry.rs @@ -234,11 +234,8 @@ impl EntryCustom { ) }; if let Some(enumerate_instance_version) = enumerate_instance_version { - let err_code = (enumerate_instance_version)(&mut api_version); - match err_code { - vk::Result::SUCCESS => Ok(Some(api_version)), - _ => Err(err_code), - } + (enumerate_instance_version)(&mut api_version) + .result_with_success(Some(api_version)) } else { Ok(None) } From d619ecbae2c425bb07b029485eb639270d9934c8 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 20 Nov 2020 12:18:56 +0100 Subject: [PATCH 13/29] Simplify matching on Result::SUCCESS Using the following regex replacement: let err_code =\s*(.*\((\n|[^;])*?\));\s*match err_code \{\s*vk::Result::SUCCESS => Ok\((.*)\),\s*_ => Err\(err_code\),\s*\} $1.result_with_success($3) --- ash/src/device.rs | 399 ++++++++----------- ash/src/entry.rs | 8 +- ash/src/extensions/ext/debug_report.rs | 18 +- ash/src/extensions/ext/debug_utils.rs | 18 +- ash/src/extensions/ext/metal_surface.rs | 18 +- ash/src/extensions/khr/android_surface.rs | 18 +- ash/src/extensions/khr/display.rs | 56 ++- ash/src/extensions/khr/external_memory_fd.rs | 13 +- ash/src/extensions/khr/ray_tracing.rs | 40 +- ash/src/extensions/khr/surface.rs | 14 +- ash/src/extensions/khr/swapchain.rs | 18 +- ash/src/extensions/khr/wayland_surface.rs | 18 +- ash/src/extensions/khr/win32_surface.rs | 18 +- ash/src/extensions/khr/xcb_surface.rs | 18 +- ash/src/extensions/khr/xlib_surface.rs | 18 +- ash/src/extensions/mvk/ios_surface.rs | 18 +- ash/src/extensions/mvk/macos_surface.rs | 18 +- ash/src/extensions/nv/ray_tracing.rs | 58 ++- ash/src/instance.rs | 12 +- ash/src/vk.rs | 6 +- 20 files changed, 348 insertions(+), 456 deletions(-) diff --git a/ash/src/device.rs b/ash/src/device.rs index 53886e066..e089c5c47 100644 --- a/ash/src/device.rs +++ b/ash/src/device.rs @@ -61,16 +61,14 @@ pub trait DeviceV1_2: DeviceV1_1 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut renderpass = mem::zeroed(); - let err_code = self.fp_v1_2().create_render_pass2( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut renderpass, - ); - match err_code { - vk::Result::SUCCESS => Ok(renderpass), - _ => Err(err_code), - } + self.fp_v1_2() + .create_render_pass2( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut renderpass, + ) + .result_with_success(renderpass) } #[doc = ""] @@ -122,13 +120,9 @@ pub trait DeviceV1_2: DeviceV1_1 { #[doc = ""] unsafe fn get_semaphore_counter_value(&self, semaphore: vk::Semaphore) -> VkResult { let mut value = 0; - let err_code = - self.fp_v1_2() - .get_semaphore_counter_value(self.handle(), semaphore, &mut value); - match err_code { - vk::Result::SUCCESS => Ok(value), - _ => Err(err_code), - } + self.fp_v1_2() + .get_semaphore_counter_value(self.handle(), semaphore, &mut value) + .result_with_success(value) } #[doc = ""] @@ -304,16 +298,14 @@ pub trait DeviceV1_1: DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut ycbcr_conversion = mem::zeroed(); - let err_code = self.fp_v1_1().create_sampler_ycbcr_conversion( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut ycbcr_conversion, - ); - match err_code { - vk::Result::SUCCESS => Ok(ycbcr_conversion), - _ => Err(err_code), - } + self.fp_v1_1() + .create_sampler_ycbcr_conversion( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut ycbcr_conversion, + ) + .result_with_success(ycbcr_conversion) } #[doc = ""] @@ -336,16 +328,14 @@ pub trait DeviceV1_1: DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut descriptor_update_template = mem::zeroed(); - let err_code = self.fp_v1_1().create_descriptor_update_template( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut descriptor_update_template, - ); - match err_code { - vk::Result::SUCCESS => Ok(descriptor_update_template), - _ => Err(err_code), - } + self.fp_v1_1() + .create_descriptor_update_template( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut descriptor_update_template, + ) + .result_with_success(descriptor_update_template) } #[doc = ""] @@ -438,16 +428,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut event = mem::zeroed(); - let err_code = self.fp_v1_0().create_event( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut event, - ); - match err_code { - vk::Result::SUCCESS => Ok(event), - _ => Err(err_code), - } + self.fp_v1_0() + .create_event( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut event, + ) + .result_with_success(event) } /// Returns true if the event was set, and false if the event was reset, otherwise it will @@ -743,16 +731,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut sampler = mem::zeroed(); - let err_code = self.fp_v1_0().create_sampler( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut sampler, - ); - match err_code { - vk::Result::SUCCESS => Ok(sampler), - _ => Err(err_code), - } + self.fp_v1_0() + .create_sampler( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut sampler, + ) + .result_with_success(sampler) } #[doc = ""] @@ -931,16 +917,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut layout = mem::zeroed(); - let err_code = self.fp_v1_0().create_descriptor_set_layout( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut layout, - ); - match err_code { - vk::Result::SUCCESS => Ok(layout), - _ => Err(err_code), - } + self.fp_v1_0() + .create_descriptor_set_layout( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut layout, + ) + .result_with_success(layout) } #[doc = ""] @@ -955,16 +939,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut pool = mem::zeroed(); - let err_code = self.fp_v1_0().create_descriptor_pool( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut pool, - ); - match err_code { - vk::Result::SUCCESS => Ok(pool), - _ => Err(err_code), - } + self.fp_v1_0() + .create_descriptor_pool( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut pool, + ) + .result_with_success(pool) } #[doc = ""] @@ -1486,16 +1468,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut semaphore = mem::zeroed(); - let err_code = self.fp_v1_0().create_semaphore( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut semaphore, - ); - match err_code { - vk::Result::SUCCESS => Ok(semaphore), - _ => Err(err_code), - } + self.fp_v1_0() + .create_semaphore( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut semaphore, + ) + .result_with_success(semaphore) } #[doc = ""] @@ -1551,16 +1531,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut buffer = mem::zeroed(); - let err_code = self.fp_v1_0().create_buffer( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut buffer, - ); - match err_code { - vk::Result::SUCCESS => Ok(buffer), - _ => Err(err_code), - } + self.fp_v1_0() + .create_buffer( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut buffer, + ) + .result_with_success(buffer) } #[doc = ""] @@ -1570,16 +1548,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut pipeline_layout = mem::zeroed(); - let err_code = self.fp_v1_0().create_pipeline_layout( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut pipeline_layout, - ); - match err_code { - vk::Result::SUCCESS => Ok(pipeline_layout), - _ => Err(err_code), - } + self.fp_v1_0() + .create_pipeline_layout( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut pipeline_layout, + ) + .result_with_success(pipeline_layout) } #[doc = ""] @@ -1589,17 +1565,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut pipeline_cache = mem::zeroed(); - let err_code = self.fp_v1_0().create_pipeline_cache( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut pipeline_cache, - ); - - match err_code { - vk::Result::SUCCESS => Ok(pipeline_cache), - _ => Err(err_code), - } + self.fp_v1_0() + .create_pipeline_cache( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut pipeline_cache, + ) + .result_with_success(pipeline_cache) } #[doc = ""] @@ -1640,13 +1613,9 @@ pub trait DeviceV1_0 { flags: vk::MemoryMapFlags, ) -> VkResult<*mut c_void> { let mut data: *mut c_void = ptr::null_mut(); - let err_code = - self.fp_v1_0() - .map_memory(self.handle(), memory, offset, size, flags, &mut data); - match err_code { - vk::Result::SUCCESS => Ok(data), - _ => Err(err_code), - } + self.fp_v1_0() + .map_memory(self.handle(), memory, offset, size, flags, &mut data) + .result_with_success(data) } #[doc = ""] @@ -1678,16 +1647,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut framebuffer = mem::zeroed(); - let err_code = self.fp_v1_0().create_framebuffer( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut framebuffer, - ); - match err_code { - vk::Result::SUCCESS => Ok(framebuffer), - _ => Err(err_code), - } + self.fp_v1_0() + .create_framebuffer( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut framebuffer, + ) + .result_with_success(framebuffer) } #[doc = ""] @@ -1730,16 +1697,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut renderpass = mem::zeroed(); - let err_code = self.fp_v1_0().create_render_pass( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut renderpass, - ); - match err_code { - vk::Result::SUCCESS => Ok(renderpass), - _ => Err(err_code), - } + self.fp_v1_0() + .create_render_pass( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut renderpass, + ) + .result_with_success(renderpass) } #[doc = ""] @@ -1810,16 +1775,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut buffer_view = mem::zeroed(); - let err_code = self.fp_v1_0().create_buffer_view( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut buffer_view, - ); - match err_code { - vk::Result::SUCCESS => Ok(buffer_view), - _ => Err(err_code), - } + self.fp_v1_0() + .create_buffer_view( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut buffer_view, + ) + .result_with_success(buffer_view) } #[doc = ""] @@ -1842,16 +1805,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut image_view = mem::zeroed(); - let err_code = self.fp_v1_0().create_image_view( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut image_view, - ); - match err_code { - vk::Result::SUCCESS => Ok(image_view), - _ => Err(err_code), - } + self.fp_v1_0() + .create_image_view( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut image_view, + ) + .result_with_success(image_view) } #[doc = ""] @@ -1879,16 +1840,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut pool = mem::zeroed(); - let err_code = self.fp_v1_0().create_command_pool( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut pool, - ); - match err_code { - vk::Result::SUCCESS => Ok(pool), - _ => Err(err_code), - } + self.fp_v1_0() + .create_command_pool( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut pool, + ) + .result_with_success(pool) } #[doc = ""] @@ -1898,16 +1857,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut pool = mem::zeroed(); - let err_code = self.fp_v1_0().create_query_pool( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut pool, - ); - match err_code { - vk::Result::SUCCESS => Ok(pool), - _ => Err(err_code), - } + self.fp_v1_0() + .create_query_pool( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut pool, + ) + .result_with_success(pool) } #[doc = ""] @@ -1917,16 +1874,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut image = mem::zeroed(); - let err_code = self.fp_v1_0().create_image( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut image, - ); - match err_code { - vk::Result::SUCCESS => Ok(image), - _ => Err(err_code), - } + self.fp_v1_0() + .create_image( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut image, + ) + .result_with_success(image) } #[doc = ""] @@ -1968,16 +1923,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut memory = mem::zeroed(); - let err_code = self.fp_v1_0().allocate_memory( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut memory, - ); - match err_code { - vk::Result::SUCCESS => Ok(memory), - _ => Err(err_code), - } + self.fp_v1_0() + .allocate_memory( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut memory, + ) + .result_with_success(memory) } #[doc = ""] @@ -1987,16 +1940,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut shader = mem::zeroed(); - let err_code = self.fp_v1_0().create_shader_module( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut shader, - ); - match err_code { - vk::Result::SUCCESS => Ok(shader), - _ => Err(err_code), - } + self.fp_v1_0() + .create_shader_module( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut shader, + ) + .result_with_success(shader) } #[doc = ""] @@ -2006,16 +1957,14 @@ pub trait DeviceV1_0 { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut fence = mem::zeroed(); - let err_code = self.fp_v1_0().create_fence( - self.handle(), - create_info, - allocation_callbacks.as_raw_ptr(), - &mut fence, - ); - match err_code { - vk::Result::SUCCESS => Ok(fence), - _ => Err(err_code), - } + self.fp_v1_0() + .create_fence( + self.handle(), + create_info, + allocation_callbacks.as_raw_ptr(), + &mut fence, + ) + .result_with_success(fence) } #[doc = ""] diff --git a/ash/src/entry.rs b/ash/src/entry.rs index b640ae058..8f02078e6 100644 --- a/ash/src/entry.rs +++ b/ash/src/entry.rs @@ -152,11 +152,9 @@ pub trait EntryV1_1: EntryV1_0 { fn enumerate_instance_version(&self) -> VkResult { unsafe { let mut api_version = 0; - let err_code = self.fp_v1_1().enumerate_instance_version(&mut api_version); - match err_code { - vk::Result::SUCCESS => Ok(api_version), - _ => Err(err_code), - } + self.fp_v1_1() + .enumerate_instance_version(&mut api_version) + .result_with_success(api_version) } } } diff --git a/ash/src/extensions/ext/debug_report.rs b/ash/src/extensions/ext/debug_report.rs index 56dce8055..18440f13d 100755 --- a/ash/src/extensions/ext/debug_report.rs +++ b/ash/src/extensions/ext/debug_report.rs @@ -47,16 +47,14 @@ impl DebugReport { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut debug_cb = mem::zeroed(); - let err_code = self.debug_report_fn.create_debug_report_callback_ext( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut debug_cb, - ); - match err_code { - vk::Result::SUCCESS => Ok(debug_cb), - _ => Err(err_code), - } + self.debug_report_fn + .create_debug_report_callback_ext( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut debug_cb, + ) + .result_with_success(debug_cb) } pub fn fp(&self) -> &vk::ExtDebugReportFn { diff --git a/ash/src/extensions/ext/debug_utils.rs b/ash/src/extensions/ext/debug_utils.rs index 0e7137780..b2fadc497 100755 --- a/ash/src/extensions/ext/debug_utils.rs +++ b/ash/src/extensions/ext/debug_utils.rs @@ -106,16 +106,14 @@ impl DebugUtils { allocator: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut messenger = mem::zeroed(); - let err_code = self.debug_utils_fn.create_debug_utils_messenger_ext( - self.handle, - create_info, - allocator.as_raw_ptr(), - &mut messenger, - ); - match err_code { - vk::Result::SUCCESS => Ok(messenger), - _ => Err(err_code), - } + self.debug_utils_fn + .create_debug_utils_messenger_ext( + self.handle, + create_info, + allocator.as_raw_ptr(), + &mut messenger, + ) + .result_with_success(messenger) } #[doc = ""] diff --git a/ash/src/extensions/ext/metal_surface.rs b/ash/src/extensions/ext/metal_surface.rs index e3ff5542e..eae91a8a2 100644 --- a/ash/src/extensions/ext/metal_surface.rs +++ b/ash/src/extensions/ext/metal_surface.rs @@ -34,16 +34,14 @@ impl MetalSurface { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut surface = mem::zeroed(); - let err_code = self.metal_surface_fn.create_metal_surface_ext( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut surface, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface), - _ => Err(err_code), - } + self.metal_surface_fn + .create_metal_surface_ext( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut surface, + ) + .result_with_success(surface) } pub fn fp(&self) -> &vk::ExtMetalSurfaceFn { diff --git a/ash/src/extensions/khr/android_surface.rs b/ash/src/extensions/khr/android_surface.rs index 0a22d47d5..606e4eaf9 100755 --- a/ash/src/extensions/khr/android_surface.rs +++ b/ash/src/extensions/khr/android_surface.rs @@ -34,16 +34,14 @@ impl AndroidSurface { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut surface = mem::zeroed(); - let err_code = self.android_surface_fn.create_android_surface_khr( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut surface, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface), - _ => Err(err_code), - } + self.android_surface_fn + .create_android_surface_khr( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut surface, + ) + .result_with_success(surface) } pub fn fp(&self) -> &vk::KhrAndroidSurfaceFn { diff --git a/ash/src/extensions/khr/display.rs b/ash/src/extensions/khr/display.rs index be4db5d4d..28aab7bfc 100755 --- a/ash/src/extensions/khr/display.rs +++ b/ash/src/extensions/khr/display.rs @@ -154,17 +154,15 @@ impl Display { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut display_mode = mem::MaybeUninit::zeroed(); - let err_code = self.display_fn.create_display_mode_khr( - physical_device, - display, - create_info, - allocation_callbacks.as_raw_ptr(), - display_mode.as_mut_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(display_mode.assume_init()), - _ => Err(err_code), - } + self.display_fn + .create_display_mode_khr( + physical_device, + display, + create_info, + allocation_callbacks.as_raw_ptr(), + display_mode.as_mut_ptr(), + ) + .result_with_success(display_mode.assume_init()) } #[doc = ""] @@ -175,16 +173,14 @@ impl Display { plane_index: u32, ) -> VkResult { let mut display_plane_capabilities = mem::MaybeUninit::zeroed(); - let err_code = self.display_fn.get_display_plane_capabilities_khr( - physical_device, - mode, - plane_index, - display_plane_capabilities.as_mut_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(display_plane_capabilities.assume_init()), - _ => Err(err_code), - } + self.display_fn + .get_display_plane_capabilities_khr( + physical_device, + mode, + plane_index, + display_plane_capabilities.as_mut_ptr(), + ) + .result_with_success(display_plane_capabilities.assume_init()) } #[doc = ""] @@ -194,16 +190,14 @@ impl Display { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut surface = mem::MaybeUninit::zeroed(); - let err_code = self.display_fn.create_display_plane_surface_khr( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - surface.as_mut_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(surface.assume_init()), - _ => Err(err_code), - } + self.display_fn + .create_display_plane_surface_khr( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + surface.as_mut_ptr(), + ) + .result_with_success(surface.assume_init()) } pub fn fp(&self) -> &vk::KhrDisplayFn { diff --git a/ash/src/extensions/khr/external_memory_fd.rs b/ash/src/extensions/khr/external_memory_fd.rs index 070b33a19..593ba614f 100644 --- a/ash/src/extensions/khr/external_memory_fd.rs +++ b/ash/src/extensions/khr/external_memory_fd.rs @@ -44,16 +44,9 @@ impl ExternalMemoryFd { fd: i32, ) -> VkResult { let mut memory_fd_properties = mem::zeroed(); - let err_code = self.external_memory_fd_fn.get_memory_fd_properties_khr( - self.handle, - handle_type, - fd, - &mut memory_fd_properties, - ); - match err_code { - vk::Result::SUCCESS => Ok(memory_fd_properties), - _ => Err(err_code), - } + self.external_memory_fd_fn + .get_memory_fd_properties_khr(self.handle, handle_type, fd, &mut memory_fd_properties) + .result_with_success(memory_fd_properties) } pub fn fp(&self) -> &vk::KhrExternalMemoryFdFn { diff --git a/ash/src/extensions/khr/ray_tracing.rs b/ash/src/extensions/khr/ray_tracing.rs index ca6ab0cf3..6380cc645 100644 --- a/ash/src/extensions/khr/ray_tracing.rs +++ b/ash/src/extensions/khr/ray_tracing.rs @@ -42,16 +42,14 @@ impl RayTracing { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut accel_struct = mem::zeroed(); - let err_code = self.ray_tracing_fn.create_acceleration_structure_khr( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut accel_struct, - ); - match err_code { - vk::Result::SUCCESS => Ok(accel_struct), - _ => Err(err_code), - } + self.ray_tracing_fn + .create_acceleration_structure_khr( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut accel_struct, + ) + .result_with_success(accel_struct) } #[doc = ""] @@ -158,18 +156,16 @@ impl RayTracing { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult> { let mut pipelines = vec![mem::zeroed(); create_info.len()]; - let err_code = self.ray_tracing_fn.create_ray_tracing_pipelines_khr( - self.handle, - pipeline_cache, - create_info.len() as u32, - create_info.as_ptr(), - allocation_callbacks.as_raw_ptr(), - pipelines.as_mut_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(pipelines), - _ => Err(err_code), - } + self.ray_tracing_fn + .create_ray_tracing_pipelines_khr( + self.handle, + pipeline_cache, + create_info.len() as u32, + create_info.as_ptr(), + allocation_callbacks.as_raw_ptr(), + pipelines.as_mut_ptr(), + ) + .result_with_success(pipelines) } #[doc = ""] diff --git a/ash/src/extensions/khr/surface.rs b/ash/src/extensions/khr/surface.rs index 66118ca51..e2b9c4a32 100755 --- a/ash/src/extensions/khr/surface.rs +++ b/ash/src/extensions/khr/surface.rs @@ -36,17 +36,9 @@ impl Surface { surface: vk::SurfaceKHR, ) -> VkResult { let mut b = mem::zeroed(); - let err_code = self.surface_fn.get_physical_device_surface_support_khr( - physical_device, - queue_index, - surface, - &mut b, - ); - - match err_code { - vk::Result::SUCCESS => Ok(b > 0), - _ => Err(err_code), - } + self.surface_fn + .get_physical_device_surface_support_khr(physical_device, queue_index, surface, &mut b) + .result_with_success(b > 0) } #[doc = ""] diff --git a/ash/src/extensions/khr/swapchain.rs b/ash/src/extensions/khr/swapchain.rs index 35e153edb..b39375789 100755 --- a/ash/src/extensions/khr/swapchain.rs +++ b/ash/src/extensions/khr/swapchain.rs @@ -73,16 +73,14 @@ impl Swapchain { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut swapchain = mem::zeroed(); - let err_code = self.swapchain_fn.create_swapchain_khr( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut swapchain, - ); - match err_code { - vk::Result::SUCCESS => Ok(swapchain), - _ => Err(err_code), - } + self.swapchain_fn + .create_swapchain_khr( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut swapchain, + ) + .result_with_success(swapchain) } /// On success, returns whether the swapchain is suboptimal for the surface. diff --git a/ash/src/extensions/khr/wayland_surface.rs b/ash/src/extensions/khr/wayland_surface.rs index d705f3803..20919286d 100755 --- a/ash/src/extensions/khr/wayland_surface.rs +++ b/ash/src/extensions/khr/wayland_surface.rs @@ -34,16 +34,14 @@ impl WaylandSurface { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut surface = mem::zeroed(); - let err_code = self.wayland_surface_fn.create_wayland_surface_khr( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut surface, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface), - _ => Err(err_code), - } + self.wayland_surface_fn + .create_wayland_surface_khr( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut surface, + ) + .result_with_success(surface) } #[doc = ", ) -> VkResult { let mut surface = mem::zeroed(); - let err_code = self.win32_surface_fn.create_win32_surface_khr( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut surface, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface), - _ => Err(err_code), - } + self.win32_surface_fn + .create_win32_surface_khr( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut surface, + ) + .result_with_success(surface) } #[doc = ", ) -> VkResult { let mut surface = mem::zeroed(); - let err_code = self.xcb_surface_fn.create_xcb_surface_khr( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut surface, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface), - _ => Err(err_code), - } + self.xcb_surface_fn + .create_xcb_surface_khr( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut surface, + ) + .result_with_success(surface) } #[doc = ", ) -> VkResult { let mut surface = mem::zeroed(); - let err_code = self.xlib_surface_fn.create_xlib_surface_khr( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut surface, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface), - _ => Err(err_code), - } + self.xlib_surface_fn + .create_xlib_surface_khr( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut surface, + ) + .result_with_success(surface) } #[doc = ", ) -> VkResult { let mut surface = mem::zeroed(); - let err_code = self.ios_surface_fn.create_ios_surface_mvk( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut surface, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface), - _ => Err(err_code), - } + self.ios_surface_fn + .create_ios_surface_mvk( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut surface, + ) + .result_with_success(surface) } pub fn fp(&self) -> &vk::MvkIosSurfaceFn { diff --git a/ash/src/extensions/mvk/macos_surface.rs b/ash/src/extensions/mvk/macos_surface.rs index 6f2aea696..5944351b4 100755 --- a/ash/src/extensions/mvk/macos_surface.rs +++ b/ash/src/extensions/mvk/macos_surface.rs @@ -34,16 +34,14 @@ impl MacOSSurface { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut surface = mem::zeroed(); - let err_code = self.macos_surface_fn.create_mac_os_surface_mvk( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut surface, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface), - _ => Err(err_code), - } + self.macos_surface_fn + .create_mac_os_surface_mvk( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut surface, + ) + .result_with_success(surface) } pub fn fp(&self) -> &vk::MvkMacosSurfaceFn { diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index c1d748a91..cf638a935 100755 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -42,16 +42,14 @@ impl RayTracing { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut accel_struct = mem::zeroed(); - let err_code = self.ray_tracing_fn.create_acceleration_structure_nv( - self.handle, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut accel_struct, - ); - match err_code { - vk::Result::SUCCESS => Ok(accel_struct), - _ => Err(err_code), - } + self.ray_tracing_fn + .create_acceleration_structure_nv( + self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut accel_struct, + ) + .result_with_success(accel_struct) } #[doc = ""] @@ -180,18 +178,16 @@ impl RayTracing { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult> { let mut pipelines = vec![mem::zeroed(); create_info.len()]; - let err_code = self.ray_tracing_fn.create_ray_tracing_pipelines_nv( - self.handle, - pipeline_cache, - create_info.len() as u32, - create_info.as_ptr(), - allocation_callbacks.as_raw_ptr(), - pipelines.as_mut_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(pipelines), - _ => Err(err_code), - } + self.ray_tracing_fn + .create_ray_tracing_pipelines_nv( + self.handle, + pipeline_cache, + create_info.len() as u32, + create_info.as_ptr(), + allocation_callbacks.as_raw_ptr(), + pipelines.as_mut_ptr(), + ) + .result_with_success(pipelines) } #[doc = ""] @@ -221,16 +217,14 @@ impl RayTracing { ) -> VkResult { let mut handle: u64 = 0; let handle_ptr: *mut u64 = &mut handle; - let err_code = self.ray_tracing_fn.get_acceleration_structure_handle_nv( - self.handle, - accel_struct, - std::mem::size_of::(), - handle_ptr as *mut std::ffi::c_void, - ); - match err_code { - vk::Result::SUCCESS => Ok(handle), - _ => Err(err_code), - } + self.ray_tracing_fn + .get_acceleration_structure_handle_nv( + self.handle, + accel_struct, + std::mem::size_of::(), + handle_ptr as *mut std::ffi::c_void, + ) + .result_with_success(handle) } #[doc = ""] diff --git a/ash/src/instance.rs b/ash/src/instance.rs index fc64e5d4e..e00e013d1 100644 --- a/ash/src/instance.rs +++ b/ash/src/instance.rs @@ -94,15 +94,9 @@ pub trait InstanceV1_1: InstanceV1_0 { unsafe fn enumerate_physical_device_groups_len(&self) -> VkResult { let mut group_count = mem::zeroed(); - let err_code = self.fp_v1_1().enumerate_physical_device_groups( - self.handle(), - &mut group_count, - ptr::null_mut(), - ); - match err_code { - vk::Result::SUCCESS => Ok(group_count as usize), - _ => Err(err_code), - } + self.fp_v1_1() + .enumerate_physical_device_groups(self.handle(), &mut group_count, ptr::null_mut()) + .result_with_success(group_count as usize) } #[doc = ""] diff --git a/ash/src/vk.rs b/ash/src/vk.rs index b6f4ef4fe..9308f081f 100644 --- a/ash/src/vk.rs +++ b/ash/src/vk.rs @@ -1,4 +1,8 @@ -#![allow(clippy::too_many_arguments, clippy::cognitive_complexity, clippy::wrong_self_convention)] +#![allow( + clippy::too_many_arguments, + clippy::cognitive_complexity, + clippy::wrong_self_convention +)] #[macro_use] mod macros; pub use macros::*; From e3f9aaac3992064ef545072a7e47285ca0593994 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 20 Nov 2020 12:33:34 +0100 Subject: [PATCH 14/29] Simplify intermediate error return let err_code =\s*(.*\((\n|[^;])*?\));\s*if err_code != vk::Result::SUCCESS \{\s*return Err\(err_code\);\s*\} $1.result()?; --- ash/src/device.rs | 17 ++++++----- ash/src/entry.rs | 11 ++------ ash/src/extensions/khr/display.rs | 44 ++++++++++++----------------- ash/src/extensions/khr/surface.rs | 17 ++++++----- ash/src/extensions/khr/swapchain.rs | 12 ++------ ash/src/instance.rs | 38 +++++++++---------------- 6 files changed, 54 insertions(+), 85 deletions(-) diff --git a/ash/src/device.rs b/ash/src/device.rs index e089c5c47..92f891e9d 100644 --- a/ash/src/device.rs +++ b/ash/src/device.rs @@ -1581,15 +1581,14 @@ pub trait DeviceV1_0 { pipeline_cache: vk::PipelineCache, ) -> VkResult> { let mut data_size: usize = 0; - let err_code = self.fp_v1_0().get_pipeline_cache_data( - self.handle(), - pipeline_cache, - &mut data_size, - ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.fp_v1_0() + .get_pipeline_cache_data( + self.handle(), + pipeline_cache, + &mut data_size, + ptr::null_mut(), + ) + .result()?; let mut data: Vec = Vec::with_capacity(data_size); let err_code = self.fp_v1_0().get_pipeline_cache_data( self.handle(), diff --git a/ash/src/entry.rs b/ash/src/entry.rs index 8f02078e6..c1e6791f2 100644 --- a/ash/src/entry.rs +++ b/ash/src/entry.rs @@ -79,14 +79,9 @@ pub trait EntryV1_0 { fn enumerate_instance_extension_properties(&self) -> VkResult> { unsafe { let mut num = 0; - let err_code = self.fp_v1_0().enumerate_instance_extension_properties( - ptr::null(), - &mut num, - ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.fp_v1_0() + .enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut()) + .result()?; let mut data = Vec::with_capacity(num as usize); let err_code = self.fp_v1_0().enumerate_instance_extension_properties( ptr::null(), diff --git a/ash/src/extensions/khr/display.rs b/ash/src/extensions/khr/display.rs index 28aab7bfc..0ac7defbd 100755 --- a/ash/src/extensions/khr/display.rs +++ b/ash/src/extensions/khr/display.rs @@ -33,14 +33,13 @@ impl Display { physical_device: vk::PhysicalDevice, ) -> VkResult> { let mut count = 0; - let err_code = self.display_fn.get_physical_device_display_properties_khr( - physical_device, - &mut count, - ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.display_fn + .get_physical_device_display_properties_khr( + physical_device, + &mut count, + ptr::null_mut(), + ) + .result()?; let mut v = Vec::with_capacity(count as usize); let err_code = self.display_fn.get_physical_device_display_properties_khr( physical_device, @@ -92,15 +91,14 @@ impl Display { plane_index: u32, ) -> VkResult> { let mut count = 0; - let err_code = self.display_fn.get_display_plane_supported_displays_khr( - physical_device, - plane_index, - &mut count, - ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.display_fn + .get_display_plane_supported_displays_khr( + physical_device, + plane_index, + &mut count, + ptr::null_mut(), + ) + .result()?; let mut v = Vec::with_capacity(count as usize); let err_code = self.display_fn.get_display_plane_supported_displays_khr( physical_device, @@ -122,15 +120,9 @@ impl Display { display: vk::DisplayKHR, ) -> VkResult> { let mut count = 0; - let err_code = self.display_fn.get_display_mode_properties_khr( - physical_device, - display, - &mut count, - ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.display_fn + .get_display_mode_properties_khr(physical_device, display, &mut count, ptr::null_mut()) + .result()?; let mut v = Vec::with_capacity(count as usize); let err_code = self.display_fn.get_display_mode_properties_khr( physical_device, diff --git a/ash/src/extensions/khr/surface.rs b/ash/src/extensions/khr/surface.rs index e2b9c4a32..4abf83951 100755 --- a/ash/src/extensions/khr/surface.rs +++ b/ash/src/extensions/khr/surface.rs @@ -102,15 +102,14 @@ impl Surface { surface: vk::SurfaceKHR, ) -> VkResult> { let mut count = 0; - let err_code = self.surface_fn.get_physical_device_surface_formats_khr( - physical_device, - surface, - &mut count, - ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.surface_fn + .get_physical_device_surface_formats_khr( + physical_device, + surface, + &mut count, + ptr::null_mut(), + ) + .result()?; let mut v = Vec::with_capacity(count as usize); let err_code = self.surface_fn.get_physical_device_surface_formats_khr( physical_device, diff --git a/ash/src/extensions/khr/swapchain.rs b/ash/src/extensions/khr/swapchain.rs index b39375789..fe5e50f1d 100755 --- a/ash/src/extensions/khr/swapchain.rs +++ b/ash/src/extensions/khr/swapchain.rs @@ -104,15 +104,9 @@ impl Swapchain { swapchain: vk::SwapchainKHR, ) -> VkResult> { let mut count = 0; - let err_code = self.swapchain_fn.get_swapchain_images_khr( - self.handle, - swapchain, - &mut count, - ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.swapchain_fn + .get_swapchain_images_khr(self.handle, swapchain, &mut count, ptr::null_mut()) + .result()?; let mut v = Vec::with_capacity(count as usize); let err_code = self.swapchain_fn.get_swapchain_images_khr( diff --git a/ash/src/instance.rs b/ash/src/instance.rs index e00e013d1..b2004b3ec 100644 --- a/ash/src/instance.rs +++ b/ash/src/instance.rs @@ -51,15 +51,14 @@ impl InstanceV1_0 for Instance { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut device: vk::Device = mem::zeroed(); - let err_code = self.fp_v1_0().create_device( - physical_device, - create_info, - allocation_callbacks.as_raw_ptr(), - &mut device, - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.fp_v1_0() + .create_device( + physical_device, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut device, + ) + .result()?; Ok(Device::load(&self.instance_fn_1_0, device)) } fn handle(&self) -> vk::Instance { @@ -407,12 +406,9 @@ pub trait InstanceV1_0 { #[doc = ""] unsafe fn enumerate_physical_devices(&self) -> VkResult> { let mut num = mem::zeroed(); - let err_code = - self.fp_v1_0() - .enumerate_physical_devices(self.handle(), &mut num, ptr::null_mut()); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.fp_v1_0() + .enumerate_physical_devices(self.handle(), &mut num, ptr::null_mut()) + .result()?; let mut physical_devices = Vec::::with_capacity(num as usize); let err_code = self.fp_v1_0().enumerate_physical_devices( self.handle(), @@ -432,15 +428,9 @@ pub trait InstanceV1_0 { device: vk::PhysicalDevice, ) -> Result, vk::Result> { let mut num = 0; - let err_code = self.fp_v1_0().enumerate_device_extension_properties( - device, - ptr::null(), - &mut num, - ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.fp_v1_0() + .enumerate_device_extension_properties(device, ptr::null(), &mut num, ptr::null_mut()) + .result()?; let mut data = Vec::with_capacity(num as usize); let err_code = self.fp_v1_0().enumerate_device_extension_properties( device, From e23e4967456f81202ad5d96deef5bb8c3a18b66a Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 20 Nov 2020 12:31:12 +0100 Subject: [PATCH 15/29] Simplify error checking with .result()? and .result_with_success() --- ash/src/device.rs | 15 ++----- ash/src/entry.rs | 35 +++++++---------- ash/src/extensions/ext/tooling_info.rs | 14 ++----- ash/src/extensions/khr/display.rs | 20 ++-------- ash/src/extensions/khr/display_swapchain.rs | 5 +-- ash/src/extensions/khr/external_memory_fd.rs | 11 ++---- .../khr/pipeline_executable_properties.rs | 30 +++++--------- ash/src/extensions/khr/ray_tracing.rs | 11 ++---- ash/src/extensions/khr/surface.rs | 29 ++++---------- ash/src/extensions/khr/swapchain.rs | 5 +-- ash/src/extensions/khr/timeline_semaphore.rs | 11 ++---- ash/src/instance.rs | 39 +++++++------------ 12 files changed, 68 insertions(+), 157 deletions(-) diff --git a/ash/src/device.rs b/ash/src/device.rs index 92f891e9d..bcee6b93c 100644 --- a/ash/src/device.rs +++ b/ash/src/device.rs @@ -904,10 +904,7 @@ pub trait DeviceV1_0 { ); desc_set.set_len(create_info.descriptor_set_count as usize); - match err_code { - vk::Result::SUCCESS => Ok(desc_set), - _ => Err(err_code), - } + err_code.result_with_success(desc_set) } #[doc = ""] @@ -1597,10 +1594,7 @@ pub trait DeviceV1_0 { data.as_mut_ptr() as _, ); data.set_len(data_size); - match err_code { - vk::Result::SUCCESS => Ok(data), - _ => Err(err_code), - } + err_code.result_with_success(data) } #[doc = ""] @@ -1826,10 +1820,7 @@ pub trait DeviceV1_0 { buffers.as_mut_ptr(), ); buffers.set_len(create_info.command_buffer_count as usize); - match err_code { - vk::Result::SUCCESS => Ok(buffers), - _ => Err(err_code), - } + err_code.result_with_success(buffers) } #[doc = ""] diff --git a/ash/src/entry.rs b/ash/src/entry.rs index c1e6791f2..3a4d689df 100644 --- a/ash/src/entry.rs +++ b/ash/src/entry.rs @@ -57,21 +57,15 @@ pub trait EntryV1_0 { fn enumerate_instance_layer_properties(&self) -> VkResult> { unsafe { let mut num = 0; - let err_code = self - .fp_v1_0() - .enumerate_instance_layer_properties(&mut num, ptr::null_mut()); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.fp_v1_0() + .enumerate_instance_layer_properties(&mut num, ptr::null_mut()) + .result()?; let mut v = Vec::with_capacity(num as usize); let err_code = self .fp_v1_0() .enumerate_instance_layer_properties(&mut num, v.as_mut_ptr()); v.set_len(num as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } } @@ -89,10 +83,7 @@ pub trait EntryV1_0 { data.as_mut_ptr(), ); data.set_len(num as usize); - match err_code { - vk::Result::SUCCESS => Ok(data), - _ => Err(err_code), - } + err_code.result_with_success(data) } } @@ -120,14 +111,14 @@ impl EntryV1_0 for EntryCustom { allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> Result { let mut instance: vk::Instance = mem::zeroed(); - let err_code = self.fp_v1_0().create_instance( - create_info, - allocation_callbacks.as_raw_ptr(), - &mut instance, - ); - if err_code != vk::Result::SUCCESS { - return Err(InstanceError::VkError(err_code)); - } + self.fp_v1_0() + .create_instance( + create_info, + allocation_callbacks.as_raw_ptr(), + &mut instance, + ) + .result() + .map_err(InstanceError::VkError)?; Ok(Instance::load(&self.static_fn, instance)) } fn fp_v1_0(&self) -> &vk::EntryFnV1_0 { diff --git a/ash/src/extensions/ext/tooling_info.rs b/ash/src/extensions/ext/tooling_info.rs index e42e30f08..fc405339d 100644 --- a/ash/src/extensions/ext/tooling_info.rs +++ b/ash/src/extensions/ext/tooling_info.rs @@ -33,21 +33,15 @@ impl ToolingInfo { physical_device: vk::PhysicalDevice, ) -> VkResult> { let mut count = 0; - let err_code = self - .tooling_info_fn - .get_physical_device_tool_properties_ext(physical_device, &mut count, ptr::null_mut()); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + self.tooling_info_fn + .get_physical_device_tool_properties_ext(physical_device, &mut count, ptr::null_mut()) + .result()?; let mut v = Vec::with_capacity(count as usize); let err_code = self .tooling_info_fn .get_physical_device_tool_properties_ext(physical_device, &mut count, v.as_mut_ptr()); v.set_len(count as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } pub fn fp(&self) -> &vk::ExtToolingInfoFn { diff --git a/ash/src/extensions/khr/display.rs b/ash/src/extensions/khr/display.rs index 0ac7defbd..b40d33ea6 100755 --- a/ash/src/extensions/khr/display.rs +++ b/ash/src/extensions/khr/display.rs @@ -47,10 +47,7 @@ impl Display { v.as_mut_ptr(), ); v.set_len(count as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } #[doc = ""] @@ -78,10 +75,7 @@ impl Display { v.as_mut_ptr(), ); v.set_len(count as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } #[doc = ""] @@ -107,10 +101,7 @@ impl Display { v.as_mut_ptr(), ); v.set_len(count as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } #[doc = ""] @@ -131,10 +122,7 @@ impl Display { v.as_mut_ptr(), ); v.set_len(count as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } #[doc = ""] diff --git a/ash/src/extensions/khr/display_swapchain.rs b/ash/src/extensions/khr/display_swapchain.rs index bb4cfe289..a805c852b 100755 --- a/ash/src/extensions/khr/display_swapchain.rs +++ b/ash/src/extensions/khr/display_swapchain.rs @@ -42,10 +42,7 @@ impl DisplaySwapchain { swapchains.as_mut_ptr(), ); swapchains.set_len(create_infos.len()); - match err_code { - vk::Result::SUCCESS => Ok(swapchains), - _ => Err(err_code), - } + err_code.result_with_success(swapchains) } pub fn fp(&self) -> &vk::KhrDisplaySwapchainFn { diff --git a/ash/src/extensions/khr/external_memory_fd.rs b/ash/src/extensions/khr/external_memory_fd.rs index 593ba614f..ad6d6a534 100644 --- a/ash/src/extensions/khr/external_memory_fd.rs +++ b/ash/src/extensions/khr/external_memory_fd.rs @@ -28,13 +28,10 @@ impl ExternalMemoryFd { #[doc = ""] pub unsafe fn get_memory_fd(&self, create_info: &vk::MemoryGetFdInfoKHR) -> VkResult { let mut fd = -1; - let err_code = - self.external_memory_fd_fn - .get_memory_fd_khr(self.handle, create_info, &mut fd); - match err_code { - vk::Result::SUCCESS => Ok(fd), - _ => Err(err_code), - } + + self.external_memory_fd_fn + .get_memory_fd_khr(self.handle, create_info, &mut fd) + .result_with_success(fd) } #[doc = ""] diff --git a/ash/src/extensions/khr/pipeline_executable_properties.rs b/ash/src/extensions/khr/pipeline_executable_properties.rs index 65c723b8a..133c9880d 100644 --- a/ash/src/extensions/khr/pipeline_executable_properties.rs +++ b/ash/src/extensions/khr/pipeline_executable_properties.rs @@ -51,18 +51,14 @@ impl PipelineExecutableProperties { return Err(err_code); } let mut v: Vec<_> = vec![Default::default(); count as usize]; - let err_code = self - .pipeline_executable_properties_fn + self.pipeline_executable_properties_fn .get_pipeline_executable_internal_representations_khr( device, executable_info, &mut count, v.as_mut_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + ) + .result_with_success(v) } #[doc = "https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPipelineExecutablePropertiesKHR.html"] @@ -84,18 +80,14 @@ impl PipelineExecutableProperties { return Err(err_code); } let mut v: Vec<_> = vec![Default::default(); count as usize]; - let err_code = self - .pipeline_executable_properties_fn + self.pipeline_executable_properties_fn .get_pipeline_executable_properties_khr( device, pipeline_info, &mut count, v.as_mut_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + ) + .result_with_success(v) } #[doc = "https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPipelineExecutableStatisticsKHR.html"] @@ -117,18 +109,14 @@ impl PipelineExecutableProperties { return Err(err_code); } let mut v: Vec<_> = vec![Default::default(); count as usize]; - let err_code = self - .pipeline_executable_properties_fn + self.pipeline_executable_properties_fn .get_pipeline_executable_statistics_khr( device, executable_info, &mut count, v.as_mut_ptr(), - ); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + ) + .result_with_success(v) } pub fn fp(&self) -> &vk::KhrPipelineExecutablePropertiesFn { diff --git a/ash/src/extensions/khr/ray_tracing.rs b/ash/src/extensions/khr/ray_tracing.rs index 6380cc645..dad56371c 100644 --- a/ash/src/extensions/khr/ray_tracing.rs +++ b/ash/src/extensions/khr/ray_tracing.rs @@ -283,8 +283,7 @@ impl RayTracing { ) -> VkResult> { let mut data: Vec = Vec::with_capacity(data_size); - let err_code = self - .ray_tracing_fn + self.ray_tracing_fn .get_ray_tracing_capture_replay_shader_group_handles_khr( device, pipeline, @@ -292,12 +291,8 @@ impl RayTracing { group_count, data_size, data.as_mut_ptr() as *mut _, - ); - - match err_code { - vk::Result::SUCCESS => Ok(data), - _ => Err(err_code), - } + ) + .result_with_success(data) } pub unsafe fn cmd_trace_rays_indirect( diff --git a/ash/src/extensions/khr/surface.rs b/ash/src/extensions/khr/surface.rs index 4abf83951..f70279671 100755 --- a/ash/src/extensions/khr/surface.rs +++ b/ash/src/extensions/khr/surface.rs @@ -48,17 +48,14 @@ impl Surface { surface: vk::SurfaceKHR, ) -> VkResult> { let mut count = 0; - let err_code = self - .surface_fn + self.surface_fn .get_physical_device_surface_present_modes_khr( physical_device, surface, &mut count, ptr::null_mut(), - ); - if err_code != vk::Result::SUCCESS { - return Err(err_code); - } + ) + .result()?; let mut v = Vec::with_capacity(count as usize); let err_code = self .surface_fn @@ -69,10 +66,7 @@ impl Surface { v.as_mut_ptr(), ); v.set_len(count as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } #[doc = ""] @@ -82,17 +76,13 @@ impl Surface { surface: vk::SurfaceKHR, ) -> VkResult { let mut surface_capabilities = mem::zeroed(); - let err_code = self - .surface_fn + self.surface_fn .get_physical_device_surface_capabilities_khr( physical_device, surface, &mut surface_capabilities, - ); - match err_code { - vk::Result::SUCCESS => Ok(surface_capabilities), - _ => Err(err_code), - } + ) + .result_with_success(surface_capabilities) } #[doc = ""] @@ -118,10 +108,7 @@ impl Surface { v.as_mut_ptr(), ); v.set_len(count as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } #[doc = ""] diff --git a/ash/src/extensions/khr/swapchain.rs b/ash/src/extensions/khr/swapchain.rs index fe5e50f1d..9a6527391 100755 --- a/ash/src/extensions/khr/swapchain.rs +++ b/ash/src/extensions/khr/swapchain.rs @@ -116,10 +116,7 @@ impl Swapchain { v.as_mut_ptr(), ); v.set_len(count as usize); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), - } + err_code.result_with_success(v) } pub fn fp(&self) -> &vk::KhrSwapchainFn { diff --git a/ash/src/extensions/khr/timeline_semaphore.rs b/ash/src/extensions/khr/timeline_semaphore.rs index 7a3ea80e5..bb3487863 100644 --- a/ash/src/extensions/khr/timeline_semaphore.rs +++ b/ash/src/extensions/khr/timeline_semaphore.rs @@ -34,14 +34,9 @@ impl TimelineSemaphore { semaphore: vk::Semaphore, ) -> VkResult { let mut value = 0; - let err_code = self - .timeline_semaphore_fn - .get_semaphore_counter_value_khr(device, semaphore, &mut value); - - match err_code { - vk::Result::SUCCESS => Ok(value), - _ => Err(err_code), - } + self.timeline_semaphore_fn + .get_semaphore_counter_value_khr(device, semaphore, &mut value) + .result_with_success(value) } #[doc = ""] diff --git a/ash/src/instance.rs b/ash/src/instance.rs index b2004b3ec..e36f4df01 100644 --- a/ash/src/instance.rs +++ b/ash/src/instance.rs @@ -290,7 +290,7 @@ pub trait InstanceV1_0 { physical_device: vk::PhysicalDevice, create_info: &vk::DeviceCreateInfo, allocation_callbacks: Option<&vk::AllocationCallbacks>, - ) -> Result; + ) -> VkResult; #[doc = ""] unsafe fn get_device_proc_addr( @@ -333,20 +333,17 @@ pub trait InstanceV1_0 { flags: vk::ImageCreateFlags, ) -> VkResult { let mut image_format_prop = mem::zeroed(); - let err_code = self.fp_v1_0().get_physical_device_image_format_properties( - physical_device, - format, - typ, - tiling, - usage, - flags, - &mut image_format_prop, - ); - if err_code == vk::Result::SUCCESS { - Ok(image_format_prop) - } else { - Err(err_code) - } + self.fp_v1_0() + .get_physical_device_image_format_properties( + physical_device, + format, + typ, + tiling, + usage, + flags, + &mut image_format_prop, + ) + .result_with_success(image_format_prop) } #[doc = ""] @@ -416,17 +413,14 @@ pub trait InstanceV1_0 { physical_devices.as_mut_ptr(), ); physical_devices.set_len(num as usize); - match err_code { - vk::Result::SUCCESS => Ok(physical_devices), - _ => Err(err_code), - } + err_code.result_with_success(physical_devices) } #[doc = ""] unsafe fn enumerate_device_extension_properties( &self, device: vk::PhysicalDevice, - ) -> Result, vk::Result> { + ) -> VkResult> { let mut num = 0; self.fp_v1_0() .enumerate_device_extension_properties(device, ptr::null(), &mut num, ptr::null_mut()) @@ -439,9 +433,6 @@ pub trait InstanceV1_0 { data.as_mut_ptr(), ); data.set_len(num as usize); - match err_code { - vk::Result::SUCCESS => Ok(data), - _ => Err(err_code), - } + err_code.result_with_success(data) } } From d64286e66111f478a668a942a47898620d9abada Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 24 Aug 2020 12:34:49 +0200 Subject: [PATCH 16/29] generator: Ignore empty basetype in typedef (forward declaration) ANativeWindow and AHardwareBuffer were [recently changed] to the basetype category, but without an actual basetype, consequently failing the Ident constructor with an empty name. Since these types are already dealt with in platform_types, and there doesn't seem to be anything sensical to define them to right now, just ignore these cases. [recently changed]: https://github.com/KhronosGroup/Vulkan-Headers/commit/0c5351f5e9114d3e9033aeae51d036a3d201c082#diff-0ff049f722e55de41ee15b2c91ef380fL179-R180 --- generator/src/lib.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 36f10a199..2a2d58f8e 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -1171,12 +1171,17 @@ pub fn generate_define(define: &vkxml::Define) -> Tokens { } } pub fn generate_typedef(typedef: &vkxml::Typedef) -> Tokens { - let typedef_name = to_type_tokens(&typedef.name, None); - let typedef_ty = to_type_tokens(&typedef.basetype, None); - let khronos_link = khronos_link(&typedef.name); - quote! { - #[doc = #khronos_link] - pub type #typedef_name = #typedef_ty; + if typedef.basetype.is_empty() { + // Ignore forward declarations + quote! {} + } else { + let typedef_name = to_type_tokens(&typedef.name, None); + let typedef_ty = to_type_tokens(&typedef.basetype, None); + let khronos_link = khronos_link(&typedef.name); + quote! { + #[doc = #khronos_link] + pub type #typedef_name = #typedef_ty; + } } } pub fn generate_bitmask( From 7e1a6013caf4e391cb93b131b19768d6a00d0db4 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 28 Aug 2020 21:33:01 +0200 Subject: [PATCH 17/29] Suggestion: Allocate data in get_ray_tracing_shader_group_handles --- ash/src/extensions/khr/ray_tracing.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ash/src/extensions/khr/ray_tracing.rs b/ash/src/extensions/khr/ray_tracing.rs index dad56371c..325747399 100644 --- a/ash/src/extensions/khr/ray_tracing.rs +++ b/ash/src/extensions/khr/ray_tracing.rs @@ -174,18 +174,21 @@ impl RayTracing { pipeline: vk::Pipeline, first_group: u32, group_count: u32, - data: &mut [u8], - ) -> VkResult<()> { - self.ray_tracing_fn + data_size: usize, + ) -> VkResult> { + let mut data = Vec::::with_capacity(data_size); + let err_code = self + .ray_tracing_fn .get_ray_tracing_shader_group_handles_khr( self.handle, pipeline, first_group, group_count, - data.len(), + data_size, data.as_mut_ptr() as *mut std::ffi::c_void, - ) - .into() + ); + data.set_len(data_size); + err_code.result_with_success(data) } #[doc = ""] From 05dcdbf35e0854867b714f3e09ce5a1b5d192a3d Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 16 Nov 2020 20:20:57 +0100 Subject: [PATCH 18/29] generator: Update nom to 6.0 --- generator/Cargo.toml | 2 +- generator/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/generator/Cargo.toml b/generator/Cargo.toml index 378525424..6d934320a 100644 --- a/generator/Cargo.toml +++ b/generator/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] vk-parse = { version = "0.5.0", features = ["vkxml-convert"] } vkxml = "0.3" -nom = "4.0" +nom = "6.0" heck = "0.3" proc-macro2 = "0.2" itertools = "0.9" diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 2a2d58f8e..3061e32c4 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -78,7 +78,7 @@ named!(inverse_number<&str, (CType, String)>, ); named!(cfloat<&str, f32>, - terminated!(nom::float, char!('f')) + terminated!(nom::number::complete::float, char!('f')) ); fn khronos_link(name: &S) -> Literal { From 79be32d179bfd843ce8b97d2d2f1b9044501cb8c Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 20 Nov 2020 11:44:33 +0100 Subject: [PATCH 19/29] generator: Generalize C expression conversion to TokenStream --- generator/src/lib.rs | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 3061e32c4..3bb922d1a 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -5,7 +5,7 @@ use quote::*; use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; use itertools::Itertools; -use proc_macro2::{Literal, Term, TokenStream}; +use proc_macro2::{Literal, Term, TokenNode, TokenStream, TokenTree}; use quote::Tokens; use std::collections::{BTreeMap, HashMap, HashSet}; use std::fmt::Display; @@ -670,6 +670,40 @@ fn to_type_tokens(type_name: &str, reference: Option<&vkxml::ReferenceType>) -> quote! {#ptr_name #new_name} } +fn map_identifier_to_rust(term: Term) -> Term { + match term.as_str() { + "VK_MAKE_VERSION" => Term::intern("crate::vk::make_version"), + s => Term::intern(&constant_name(&s)), + } +} + +/// Parse and yield a C expression that is valid to write in Rust +/// Identifiers are replaced with their Rust vk equivalent. +/// +/// Examples: +/// - `VK_MAKE_VERSION(1, 2, VK_HEADER_VERSION)` -> `crate::vk::make_version(1, 2, HEADER_VERSION)` +fn convert_c_expression(c_expr: &str) -> TokenStream { + fn rewrite_token_stream(stream: TokenStream) -> TokenStream { + stream + .into_iter() + .map(|tok| TokenTree { + kind: rewrite_token_node(tok.kind), + ..tok + }) + .collect::() + } + fn rewrite_token_node(node: TokenNode) -> TokenNode { + match node { + TokenNode::Group(d, stream) => TokenNode::Group(d, rewrite_token_stream(stream)), + TokenNode::Term(term) => TokenNode::Term(map_identifier_to_rust(term)), + _ => node, + } + } + + let c_expr = c_expr.parse().unwrap(); + rewrite_token_stream(c_expr) +} + impl FieldExt for vkxml::Field { fn is_clone(&self) -> bool { true @@ -1158,10 +1192,8 @@ pub fn generate_define(define: &vkxml::Define) -> Tokens { str::parse::(value).map_or(quote!(), |v| quote!(pub const #ident: u32 = #v;)) } else if let Some(c_expr) = &define.c_expression { if define.defref.contains(&"VK_MAKE_VERSION".to_string()) { - let c_expr = c_expr.replace("VK_", ""); - let c_expr = c_expr.replace("MAKE_VERSION", "crate::vk::make_version"); - use std::str::FromStr; - let c_expr = TokenStream::from_str(&c_expr).unwrap(); + let c_expr = convert_c_expression(c_expr); + quote!(pub const #ident: u32 = #c_expr;) } else { quote!() From 76a19df59d6699f43ff59a23ca1eeda18a2a7faf Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 16 Nov 2020 20:59:02 +0100 Subject: [PATCH 20/29] generator: Simplify ReferenceType::to_tokens with quote! --- generator/src/lib.rs | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 3bb922d1a..144e0a9c9 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -619,26 +619,15 @@ pub trait ToTokens { } impl ToTokens for vkxml::ReferenceType { fn to_tokens(&self, is_const: bool) -> Tokens { - let ptr_name = match self { - vkxml::ReferenceType::Pointer => { - if is_const { - "*const" - } else { - "*mut" - } - } - vkxml::ReferenceType::PointerToPointer => "*mut *mut", - vkxml::ReferenceType::PointerToConstPointer => { - if is_const { - "*const *const" - } else { - "*mut *const" - } - } + let r = if is_const { + quote!(*const) + } else { + quote!(*mut) }; - let ident = Term::intern(ptr_name); - quote! { - #ident + match self { + vkxml::ReferenceType::Pointer => quote!(#r), + vkxml::ReferenceType::PointerToPointer => quote!(#r *mut), + vkxml::ReferenceType::PointerToConstPointer => quote!(#r *const), } } } From 17f3010931165974e217b74c7ea56936a6e25ed7 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 23 Nov 2020 14:40:21 +0100 Subject: [PATCH 21/29] generator: Refactor to not parse our own stringified token stream --- generator/src/lib.rs | 142 +++++++++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 59 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 144e0a9c9..486ed5719 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -607,6 +607,13 @@ pub trait FieldExt { /// keywords fn param_ident(&self) -> Ident; + /// The inner type of this field, with one level of pointers removed + fn inner_type_tokens(&self) -> Tokens; + + /// Returns reference-types wrapped in their safe variant. (Dynamic) arrays become + /// slices, pointers become Rust references. + fn safe_type_tokens(&self, lifetime: Tokens) -> Tokens; + /// 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. @@ -616,6 +623,8 @@ pub trait FieldExt { pub trait ToTokens { fn to_tokens(&self, is_const: bool) -> Tokens; + /// Returns the topmost pointer as safe reference + fn to_safe_tokens(&self, is_const: bool, lifetime: Tokens) -> Tokens; } impl ToTokens for vkxml::ReferenceType { fn to_tokens(&self, is_const: bool) -> Tokens { @@ -630,6 +639,19 @@ impl ToTokens for vkxml::ReferenceType { vkxml::ReferenceType::PointerToConstPointer => quote!(#r *const), } } + + fn to_safe_tokens(&self, is_const: bool, lifetime: Tokens) -> Tokens { + 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), + } + } } fn name_to_tokens(type_name: &str) -> Ident { let new_name = match type_name { @@ -655,7 +677,7 @@ fn name_to_tokens(type_name: &str) -> Ident { } fn to_type_tokens(type_name: &str, reference: Option<&vkxml::ReferenceType>) -> Tokens { let new_name = name_to_tokens(type_name); - let ptr_name = reference.map(|r| r.to_tokens(false)).unwrap_or(quote! {}); + let ptr_name = reference.map(|r| r.to_tokens(false)); quote! {#ptr_name #new_name} } @@ -697,6 +719,7 @@ impl FieldExt for vkxml::Field { fn is_clone(&self) -> bool { true } + fn param_ident(&self) -> Ident { let name = self.name.as_deref().unwrap_or("field"); let name_corrected = match name { @@ -706,18 +729,41 @@ impl FieldExt for vkxml::Field { Ident::from(name_corrected.to_snake_case().as_str()) } + fn inner_type_tokens(&self) -> Tokens { + 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), + } + } + + fn safe_type_tokens(&self, lifetime: Tokens) -> Tokens { + 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) + } + } + } + fn type_tokens(&self, is_ffi_param: bool) -> Tokens { let ty = name_to_tokens(&self.basetype); - let pointer = self - .reference - .as_ref() - .map(|r| r.to_tokens(self.is_const)) - .unwrap_or(quote! {}); - let pointer_ty = quote! { - #pointer #ty - }; - let array = self.array.as_ref().and_then(|arraytype| match arraytype { - vkxml::ArrayType::Static => { + + match self.array { + Some(vkxml::ArrayType::Static) => { + assert!(self.reference.is_none()); let size = self .size .as_ref() @@ -729,18 +775,16 @@ impl FieldExt for vkxml::Field { let size = Term::intern(&size); // arrays in c are always passed as a pointer if is_ffi_param { - Some(quote! { - &[#ty; #size] - }) + quote!(&[#ty; #size]) } else { - Some(quote! { - [#ty; #size] - }) + quote!([#ty; #size]) } } - _ => None, - }); - array.unwrap_or(pointer_ty) + _ => { + let pointer = self.reference.as_ref().map(|r| r.to_tokens(self.is_const)); + quote!(#pointer #ty) + } + } } } @@ -1623,19 +1667,9 @@ pub fn derive_setters( _ => None, }); - let (has_next, _is_next_const) = match members + let has_next = members .clone() - .find(|field| field.param_ident().as_ref() == "p_next") - { - Some(p_next) => { - if p_next.type_tokens(false).to_string().starts_with("*const") { - (true, true) - } else { - (true, false) - } - } - None => (false, false), - }; + .any(|field| field.param_ident().as_ref() == "p_next"); let nofilter_count_members = [ "VkPipelineViewportStateCreateInfo.pViewports", @@ -1672,7 +1706,6 @@ pub fn derive_setters( let setters = members.clone().filter_map(|field| { let param_ident = field.param_ident(); let param_ty_tokens = field.type_tokens(false); - let param_ty_string = param_ty_tokens.to_string(); let param_ident_string = param_ident.to_string(); if param_ident_string == "s_type" || param_ident_string == "p_next" { @@ -1714,14 +1747,14 @@ pub fn derive_setters( // TODO: Improve in future when https://github.com/rust-lang/rust/issues/53667 is merged id:6 if param_ident_string.starts_with("p_") || param_ident_string.starts_with("pp_") { - if param_ty_string == "*const c_char" { + if field.basetype == "char" && matches!(field.reference, Some(vkxml::ReferenceType::Pointer)) { assert!(field.null_terminate); assert_eq!(field.size, None); return Some(quote!{ - pub fn #param_ident_short(mut self, #param_ident_short: &'a ::std::ffi::CStr) -> #name_builder<'a> { - self.inner.#param_ident = #param_ident_short.as_ptr(); - self - } + pub fn #param_ident_short(mut self, #param_ident_short: &'a ::std::ffi::CStr) -> #name_builder<'a> { + self.inner.#param_ident = #param_ident_short.as_ptr(); + self + } }); } @@ -1730,34 +1763,26 @@ pub fn derive_setters( if !array_size.starts_with("latexmath") { let array_size_ident = Ident::from(array_size.to_snake_case().as_str()); - let param_ty = param_ty_string.splitn(2, ' ').collect_vec(); - assert_eq!(param_ty.len(), 2); - let mutable = match param_ty[0] { - "*const" => false, - "*mut" => true, - _ => unreachable!("Unexpected const-ness type: {}", param_ty[0]), - }; - let mut slice_type = param_ty[1].parse::().unwrap(); + let mut slice_param_ty_tokens = field.safe_type_tokens(quote!('a)); - let mut ptr = if mutable { - quote!(.as_mut_ptr()) - } else { + let mut ptr = if field.is_const { quote!(.as_ptr()) + } else { + quote!(.as_mut_ptr()) }; // Interpret void array as byte array - if param_ty[1] == "c_void" { - let mutable = if mutable { quote!(mut) } else { quote!(const) }; + if field.basetype == "void" { + let mutable = if field.is_const { quote!(const) } else { quote!(mut) }; - slice_type = quote!(u8).into(); + slice_param_ty_tokens = quote!([u8]); ptr = quote!(#ptr as *#mutable c_void); }; - let mutable = if mutable { quote!(mut) } else { quote!() }; - let slice_param_ty_tokens = quote!(&'a #mutable [#slice_type]); + let mutable = if field.is_const { quote!() } else { quote!(mut) }; return Some(quote! { - pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { + pub fn #param_ident_short(mut self, #param_ident_short: &'a #mutable #slice_param_ty_tokens) -> #name_builder<'a> { self.inner.#array_size_ident = #param_ident_short.len() as _; self.inner.#param_ident = #param_ident_short#ptr; self @@ -1767,9 +1792,8 @@ pub fn derive_setters( } } - if let Some(param_ty_string) = param_ty_string.strip_prefix("*const ") { - let slice_param_ty_tokens = param_ty_string.parse::().unwrap(); - let slice_param_ty_tokens = quote!(&'a #slice_param_ty_tokens); + if field.is_const { + let slice_param_ty_tokens = field.safe_type_tokens(quote!('a)); return Some(quote!{ pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { self.inner.#param_ident = #param_ident_short; @@ -1779,7 +1803,7 @@ pub fn derive_setters( } } - if param_ty_string == "Bool32" { + if field.basetype == "VkBool32" { return Some(quote!{ pub fn #param_ident_short(mut self, #param_ident_short: bool) -> #name_builder<'a> { self.inner.#param_ident = #param_ident_short.into(); From 392f943aae1fc18766f374881ec838ce1bdec006 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 24 Aug 2020 15:21:15 +0200 Subject: [PATCH 22/29] generator: Deal with pointers to static-sized arrays --- generator/src/lib.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 486ed5719..e9e2863f6 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -693,6 +693,7 @@ fn map_identifier_to_rust(term: Term) -> Term { /// /// Examples: /// - `VK_MAKE_VERSION(1, 2, VK_HEADER_VERSION)` -> `crate::vk::make_version(1, 2, HEADER_VERSION)` +/// - `2*VK_UUID_SIZE` -> `2 * UUID_SIZE` fn convert_c_expression(c_expr: &str) -> TokenStream { fn rewrite_token_stream(stream: TokenStream) -> TokenStream { stream @@ -1761,8 +1762,6 @@ pub fn derive_setters( if matches!(field.array, Some(vkxml::ArrayType::Dynamic)) { if let Some(ref array_size) = field.size { if !array_size.starts_with("latexmath") { - let array_size_ident = Ident::from(array_size.to_snake_case().as_str()); - let mut slice_param_ty_tokens = field.safe_type_tokens(quote!('a)); let mut ptr = if field.is_const { @@ -1781,9 +1780,23 @@ pub fn derive_setters( let mutable = if field.is_const { quote!() } else { quote!(mut) }; + let set_size_stmt = if array_size.contains("ename:") { + // Should contain the same minus `ename:`-prefixed identifiers + let array_size = field.c_size.as_ref().unwrap(); + let c_size = convert_c_expression(array_size); + let inner_type = field.inner_type_tokens(); + + slice_param_ty_tokens = quote!([#inner_type; #c_size]); + + quote!() + } else { + let array_size_ident = Ident::from(array_size.to_snake_case().as_str()); + quote!(self.inner.#array_size_ident = #param_ident_short.len() as _;) + }; + return Some(quote! { pub fn #param_ident_short(mut self, #param_ident_short: &'a #mutable #slice_param_ty_tokens) -> #name_builder<'a> { - self.inner.#array_size_ident = #param_ident_short.len() as _; + #set_size_stmt self.inner.#param_ident = #param_ident_short#ptr; self } From f4adefcc84fe2bc57bc4e93780f05bf5f5f872f9 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 23 Nov 2020 15:37:36 +0100 Subject: [PATCH 23/29] generator: Apply static-sized array pointer to size containing `*` --- generator/src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index e9e2863f6..21ab1de2e 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -1780,9 +1780,11 @@ pub fn derive_setters( let mutable = if field.is_const { quote!() } else { quote!(mut) }; - let set_size_stmt = if array_size.contains("ename:") { - // Should contain the same minus `ename:`-prefixed identifiers - let array_size = field.c_size.as_ref().unwrap(); + // Apply some heuristics to determine whether the size is an expression. + // If so, this is a pointer to a piece of memory with statically known size. + let set_size_stmt = if array_size.contains("ename:") || array_size.contains('*') { + // c_size should contain the same minus `ename:`-prefixed identifiers + let array_size = field.c_size.as_ref().unwrap_or(array_size); let c_size = convert_c_expression(array_size); let inner_type = field.inner_type_tokens(); From 18fce5b20193958b91ca2f786839fc31941684f1 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 23 Nov 2020 15:43:36 +0100 Subject: [PATCH 24/29] generator: setter: Interpret all references as ptr, not just p_/pp_ --- ash/src/vk/definitions.rs | 13 ++++++++----- generator/src/lib.rs | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ash/src/vk/definitions.rs b/ash/src/vk/definitions.rs index e855bf869..e2215dc1c 100644 --- a/ash/src/vk/definitions.rs +++ b/ash/src/vk/definitions.rs @@ -10527,8 +10527,11 @@ impl<'a> DisplayPropertiesKHRBuilder<'a> { self.inner.display = display; self } - pub fn display_name(mut self, display_name: *const c_char) -> DisplayPropertiesKHRBuilder<'a> { - self.inner.display_name = display_name; + pub fn display_name( + mut self, + display_name: &'a ::std::ffi::CStr, + ) -> DisplayPropertiesKHRBuilder<'a> { + self.inner.display_name = display_name.as_ptr(); self } pub fn physical_dimensions( @@ -26270,7 +26273,7 @@ impl<'a> ::std::ops::DerefMut for NativeBufferANDROIDBuilder<'a> { } } impl<'a> NativeBufferANDROIDBuilder<'a> { - pub fn handle(mut self, handle: *const c_void) -> NativeBufferANDROIDBuilder<'a> { + pub fn handle(mut self, handle: &'a c_void) -> NativeBufferANDROIDBuilder<'a> { self.inner.handle = handle; self } @@ -44090,9 +44093,9 @@ impl<'a> ::std::ops::DerefMut for AccelerationStructureVersionKHRBuilder<'a> { impl<'a> AccelerationStructureVersionKHRBuilder<'a> { pub fn version_data( mut self, - version_data: *const u8, + version_data: &'a [u8; 2 * UUID_SIZE], ) -> AccelerationStructureVersionKHRBuilder<'a> { - self.inner.version_data = version_data; + self.inner.version_data = version_data.as_ptr(); self } #[doc = r" Prepends the given extension struct between the root and the first pointer. This"] diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 21ab1de2e..464b208c8 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -1747,7 +1747,7 @@ pub fn derive_setters( } // TODO: Improve in future when https://github.com/rust-lang/rust/issues/53667 is merged id:6 - if param_ident_string.starts_with("p_") || param_ident_string.starts_with("pp_") { + 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); From 6d04568356e8d7d917832bc627c165674bc5525e Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 24 Nov 2020 17:54:27 +0100 Subject: [PATCH 25/29] generator: quote::* is already imported --- generator/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 464b208c8..f3a711ee9 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -6,7 +6,6 @@ use quote::*; use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; use itertools::Itertools; use proc_macro2::{Literal, Term, TokenNode, TokenStream, TokenTree}; -use quote::Tokens; use std::collections::{BTreeMap, HashMap, HashSet}; use std::fmt::Display; use std::hash::BuildHasher; From 162c861657cab3e7cf7b42d99fcebb95deef4c8d Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 24 Nov 2020 18:10:19 +0100 Subject: [PATCH 26/29] generator: Replace manual fn to_tokens with quote::ToTokens trait impl --- generator/src/lib.rs | 88 ++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 52 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index f3a711ee9..52092d10a 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -22,8 +22,8 @@ pub enum CType { Bool32, } -impl CType { - fn to_tokens(self) -> Tokens { +impl quote::ToTokens for CType { + fn to_tokens(&self, tokens: &mut Tokens) { let term = match self { CType::USize => Term::intern("usize"), CType::U32 => Term::intern("u32"), @@ -31,7 +31,7 @@ impl CType { CType::Float => Term::intern("f32"), CType::Bool32 => Term::intern("Bool32"), }; - quote! {#term} + term.to_tokens(tokens); } } @@ -385,7 +385,6 @@ impl ConstVal { pub trait ConstantExt { fn constant(&self) -> Constant; fn variant_ident(&self, enum_name: &str) -> Ident; - fn to_tokens(&self) -> Tokens; fn notation(&self) -> Option<&str>; } @@ -396,9 +395,6 @@ impl ConstantExt for vkxml::ExtensionEnum { fn variant_ident(&self, enum_name: &str) -> Ident { variant_ident(enum_name, &self.name) } - fn to_tokens(&self) -> Tokens { - Constant::from_extension_enum(self).expect("").to_tokens() - } fn notation(&self) -> Option<&str> { self.notation.as_deref() } @@ -411,9 +407,6 @@ impl ConstantExt for vkxml::Constant { fn variant_ident(&self, enum_name: &str) -> Ident { variant_ident(enum_name, &self.name) } - fn to_tokens(&self) -> Tokens { - Constant::from_constant(self).to_tokens() - } fn notation(&self) -> Option<&str> { self.notation.as_deref() } @@ -429,6 +422,36 @@ pub enum Constant { Alias(Ident, Ident), } +impl quote::ToTokens for Constant { + fn to_tokens(&self, tokens: &mut Tokens) { + match *self { + Constant::Number(n) => { + let number = interleave_number('_', 3, &n.to_string()); + let term = Term::intern(&number); + term.to_tokens(tokens); + } + Constant::Hex(ref s) => { + let number = interleave_number('_', 4, s); + let term = Term::intern(&format!("0x{}", number)); + term.to_tokens(tokens); + } + Constant::Text(ref text) => text.to_tokens(tokens), + Constant::CExpr(ref expr) => { + let (_, (_, rexpr)) = cexpr(expr).expect("Unable to parse cexpr"); + tokens.append_all(rexpr.parse::()); + } + Constant::BitPos(pos) => { + let value = 1 << pos; + let bit_string = format!("{:b}", value); + let bit_string = interleave_number('_', 4, &bit_string); + let term = Term::intern(&format!("0b{}", bit_string)); + term.to_tokens(tokens); + } + Constant::Alias(ref base, ref value) => tokens.append_all(quote!(#base::#value)), + } + } +} + impl quote::ToTokens for ConstVal { fn to_tokens(&self, tokens: &mut Tokens) { match self { @@ -475,39 +498,6 @@ impl Constant { } } - pub fn to_tokens(&self) -> Tokens { - match *self { - Constant::Number(n) => { - let number = interleave_number('_', 3, &n.to_string()); - let term = Term::intern(&number); - quote! {#term} - } - Constant::Hex(ref s) => { - let number = interleave_number('_', 4, s); - let term = Term::intern(&format!("0x{}", number)); - quote! {#term} - } - Constant::Text(ref text) => { - quote! {#text} - } - Constant::CExpr(ref expr) => { - let (_, (_, rexpr)) = cexpr(expr).expect("Unable to parse cexpr"); - let term = Term::intern(rexpr.as_str()); - quote! {#term} - } - Constant::BitPos(pos) => { - let value = 1 << pos; - let bit_string = format!("{:b}", value); - let bit_string = interleave_number('_', 4, &bit_string); - let term = Term::intern(&format!("0b{}", bit_string)); - quote! {#term} - } - Constant::Alias(ref base, ref value) => { - quote! {#base::#value} - } - } - } - pub fn from_extension_enum(constant: &vkxml::ExtensionEnum) -> Option { let number = constant.number.map(Constant::Number); let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone())); @@ -1005,9 +995,6 @@ impl<'a> ConstantExt for ExtensionConstant<'a> { fn variant_ident(&self, enum_name: &str) -> Ident { variant_ident(enum_name, self.name) } - fn to_tokens(&self) -> Tokens { - self.constant.to_tokens() - } fn notation(&self) -> Option<&str> { None } @@ -1326,11 +1313,10 @@ pub fn bitflags_impl_block( .map(|constant| { let variant_ident = constant.variant_ident(enum_name); let constant = constant.constant(); - let tokens = constant.to_tokens(); let tokens = if let Constant::Alias(_, _) = &constant { - tokens + quote!(#constant) } else { - quote!(Self(#tokens)) + quote!(Self(#constant)) }; (variant_ident, tokens) }) @@ -2229,15 +2215,13 @@ pub fn generate_constant<'a>( let c = Constant::from_constant(constant); let name = constant_name(&constant.name); let ident = Ident::from(name.as_str()); - let value = c.to_tokens(); let ty = if name == "TRUE" || name == "FALSE" { CType::Bool32 } else { c.ty() }; - let ty = ty.to_tokens(); quote! { - pub const #ident: #ty = #value; + pub const #ident: #ty = #c; } } From d366983e2848eae59fcc1e678a32320c4504dc74 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 24 Nov 2020 18:30:45 +0100 Subject: [PATCH 27/29] generator: Return str reference from constant_name --- generator/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 52092d10a..f15b333a8 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -673,7 +673,7 @@ fn to_type_tokens(type_name: &str, reference: Option<&vkxml::ReferenceType>) -> fn map_identifier_to_rust(term: Term) -> Term { match term.as_str() { "VK_MAKE_VERSION" => Term::intern("crate::vk::make_version"), - s => Term::intern(&constant_name(&s)), + s => Term::intern(constant_name(s)), } } @@ -761,8 +761,7 @@ impl FieldExt for vkxml::Field { .expect("Should have size"); // Make sure we also rename the constant, that is // used inside the static array - let size = constant_name(size); - let size = Term::intern(&size); + let size = Term::intern(constant_name(size)); // arrays in c are always passed as a pointer if is_ffi_param { quote!(&[#ty; #size]) @@ -1200,7 +1199,7 @@ pub fn generate_extension<'a>( } pub fn generate_define(define: &vkxml::Define) -> Tokens { let name = constant_name(&define.name); - let ident = Ident::from(name.as_str()); + let ident = Ident::from(name); let deprecated = define .comment .as_ref() @@ -2203,8 +2202,9 @@ pub fn generate_feature<'a>( #device } } -pub fn constant_name(name: &str) -> String { - name.replace("VK_", "") + +pub fn constant_name(name: &str) -> &str { + name.strip_prefix("VK_").unwrap_or(name) } pub fn generate_constant<'a>( @@ -2214,7 +2214,7 @@ pub fn generate_constant<'a>( cache.insert(constant.name.as_str()); let c = Constant::from_constant(constant); let name = constant_name(&constant.name); - let ident = Ident::from(name.as_str()); + let ident = Ident::from(name); let ty = if name == "TRUE" || name == "FALSE" { CType::Bool32 } else { From a83882fbd6807d0df4e9473f42cec8bee04cf108 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sun, 6 Dec 2020 21:32:06 +0100 Subject: [PATCH 28/29] generator: Replace unused to_type_tokens with name_to_tokens The reference argument was always None; replace it with a direct call to name_to_tokens. --- generator/src/lib.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index f15b333a8..88be73877 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -664,11 +664,6 @@ fn name_to_tokens(type_name: &str) -> Ident { let new_name = new_name.replace("FlagBits", "Flags"); Ident::from(new_name.as_str()) } -fn to_type_tokens(type_name: &str, reference: Option<&vkxml::ReferenceType>) -> Tokens { - let new_name = name_to_tokens(type_name); - let ptr_name = reference.map(|r| r.to_tokens(false)); - quote! {#ptr_name #new_name} -} fn map_identifier_to_rust(term: Term) -> Term { match term.as_str() { @@ -1226,8 +1221,8 @@ pub fn generate_typedef(typedef: &vkxml::Typedef) -> Tokens { // Ignore forward declarations quote! {} } else { - let typedef_name = to_type_tokens(&typedef.name, None); - let typedef_ty = to_type_tokens(&typedef.basetype, None); + 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] @@ -2059,7 +2054,7 @@ fn generate_funcptr(fnptr: &vkxml::FunctionPointer) -> Tokens { } fn generate_union(union: &vkxml::Union) -> Tokens { - let name = to_type_tokens(&union.name, None); + let name = name_to_tokens(&union.name); let fields = union.elements.iter().map(|field| { let name = field.param_ident(); let ty = field.type_tokens(false); From 5ae5c17e7b0690b576315f2ce8d061b606d1aa59 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 23 Nov 2020 15:53:26 +0100 Subject: [PATCH 29/29] generator: setters: Use safe mutable references instead of raw ptrs --- ash/src/vk/definitions.rs | 12 ++++++------ generator/src/lib.rs | 33 ++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/ash/src/vk/definitions.rs b/ash/src/vk/definitions.rs index e2215dc1c..d4b5be2e1 100644 --- a/ash/src/vk/definitions.rs +++ b/ash/src/vk/definitions.rs @@ -20906,7 +20906,7 @@ impl<'a> IOSSurfaceCreateInfoMVKBuilder<'a> { self.inner.flags = flags; self } - pub fn view(mut self, view: &'a c_void) -> IOSSurfaceCreateInfoMVKBuilder<'a> { + pub fn view(mut self, view: *const c_void) -> IOSSurfaceCreateInfoMVKBuilder<'a> { self.inner.p_view = view; self } @@ -20986,7 +20986,7 @@ impl<'a> MacOSSurfaceCreateInfoMVKBuilder<'a> { self.inner.flags = flags; self } - pub fn view(mut self, view: &'a c_void) -> MacOSSurfaceCreateInfoMVKBuilder<'a> { + pub fn view(mut self, view: *const c_void) -> MacOSSurfaceCreateInfoMVKBuilder<'a> { self.inner.p_view = view; self } @@ -21066,7 +21066,7 @@ impl<'a> MetalSurfaceCreateInfoEXTBuilder<'a> { self.inner.flags = flags; self } - pub fn layer(mut self, layer: &'a CAMetalLayer) -> MetalSurfaceCreateInfoEXTBuilder<'a> { + pub fn layer(mut self, layer: *const CAMetalLayer) -> MetalSurfaceCreateInfoEXTBuilder<'a> { self.inner.p_layer = layer; self } @@ -26273,7 +26273,7 @@ impl<'a> ::std::ops::DerefMut for NativeBufferANDROIDBuilder<'a> { } } impl<'a> NativeBufferANDROIDBuilder<'a> { - pub fn handle(mut self, handle: &'a c_void) -> NativeBufferANDROIDBuilder<'a> { + pub fn handle(mut self, handle: *const c_void) -> NativeBufferANDROIDBuilder<'a> { self.inner.handle = handle; self } @@ -33208,7 +33208,7 @@ impl<'a> RayTracingShaderGroupCreateInfoKHRBuilder<'a> { } pub fn shader_group_capture_replay_handle( mut self, - shader_group_capture_replay_handle: &'a c_void, + shader_group_capture_replay_handle: *const c_void, ) -> RayTracingShaderGroupCreateInfoKHRBuilder<'a> { self.inner.p_shader_group_capture_replay_handle = shader_group_capture_replay_handle; self @@ -37491,7 +37491,7 @@ impl<'a> ::std::ops::DerefMut for PipelineCreationFeedbackCreateInfoEXTBuilder<' impl<'a> PipelineCreationFeedbackCreateInfoEXTBuilder<'a> { pub fn pipeline_creation_feedback( mut self, - pipeline_creation_feedback: *mut PipelineCreationFeedbackEXT, + pipeline_creation_feedback: &'a mut PipelineCreationFeedbackEXT, ) -> PipelineCreationFeedbackCreateInfoEXTBuilder<'a> { self.inner.p_pipeline_creation_feedback = pipeline_creation_feedback; self diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 88be73877..13a657e99 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -327,6 +327,20 @@ pub fn vk_bitflags_wrapped_macro() -> Tokens { } } +fn is_opaque_type(ty: &str) -> bool { + matches!( + ty, + "void" + | "wl_display" + | "wl_surface" + | "Display" + | "xcb_connection_t" + | "ANativeWindow" + | "AHardwareBuffer" + | "CAMetalLayer" + ) +} + pub fn platform_specific_types() -> Tokens { quote! { pub type RROutput = c_ulong; @@ -1685,7 +1699,7 @@ pub fn derive_setters( let setters = members.clone().filter_map(|field| { let param_ident = field.param_ident(); - let param_ty_tokens = field.type_tokens(false); + let param_ty_tokens = field.safe_type_tokens(quote!('a)); let param_ident_string = param_ident.to_string(); if param_ident_string == "s_type" || param_ident_string == "p_next" { @@ -1785,16 +1799,6 @@ pub fn derive_setters( } } } - - if field.is_const { - let slice_param_ty_tokens = field.safe_type_tokens(quote!('a)); - return Some(quote!{ - pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> { - self.inner.#param_ident = #param_ident_short; - self - } - }); - } } if field.basetype == "VkBool32" { @@ -1806,6 +1810,13 @@ pub fn derive_setters( }); } + 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 + }; + Some(quote!{ pub fn #param_ident_short(mut self, #param_ident_short: #param_ty_tokens) -> #name_builder<'a> { self.inner.#param_ident = #param_ident_short;