From bfa0309c97000520edeabc0f90b32b322517a2cc Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sat, 12 Dec 2020 20:56:43 +0100 Subject: [PATCH] Preliminary cleanup pass (#339) * Fix clippy::manual_strip * Fix clippy::comparison_to_empty * Fix clippy::needless_lifetimes * generator: Drop unnecessary edgecase for charptr array in builder * generator: Make array type handling more linear * prelude: Provide Result -> VkResult<()> conversion * Add #[must_use] to Result type * Fix all unchecked vk::Result cases * generator: Fix typos * generator: Assert char ptrs have `"null-terminated"` len attribute * prelude: Provide result_with_success helper for Result -> VKresult * Cleanup match{success} blocks with SSR {let $p = $f; match $q { vk::Result::SUCCESS => Ok($r), _ => Err($z), }} ==>> {$f.result_with_success($r)} * 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) * Simplify intermediate error return let err_code =\s*(.*\((\n|[^;])*?\));\s*if err_code != vk::Result::SUCCESS \{\s*return Err\(err_code\);\s*\} $1.result()?; * Simplify error checking with .result()? and .result_with_success() * 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 * Suggestion: Allocate data in get_ray_tracing_shader_group_handles * generator: Update nom to 6.0 * generator: Generalize C expression conversion to TokenStream * generator: Simplify ReferenceType::to_tokens with quote! * generator: Refactor to not parse our own stringified token stream * generator: Deal with pointers to static-sized arrays * generator: Apply static-sized array pointer to size containing `*` * generator: setter: Interpret all references as ptr, not just p_/pp_ * generator: quote::* is already imported * generator: Replace manual fn to_tokens with quote::ToTokens trait impl * generator: Return str reference from constant_name * 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: setters: Use safe mutable references instead of raw ptrs --- ash/src/device.rs | 668 +++++++----------- ash/src/entry.rs | 53 +- ash/src/extensions/ext/debug_marker.rs | 10 +- ash/src/extensions/ext/debug_report.rs | 18 +- ash/src/extensions/ext/debug_utils.rs | 38 +- ash/src/extensions/ext/metal_surface.rs | 18 +- ash/src/extensions/ext/tooling_info.rs | 8 +- ash/src/extensions/khr/android_surface.rs | 18 +- ash/src/extensions/khr/display.rs | 117 ++- ash/src/extensions/khr/display_swapchain.rs | 5 +- ash/src/extensions/khr/external_memory_fd.rs | 24 +- .../khr/pipeline_executable_properties.rs | 72 +- ash/src/extensions/khr/ray_tracing.rs | 112 ++- ash/src/extensions/khr/surface.rs | 51 +- ash/src/extensions/khr/swapchain.rs | 32 +- ash/src/extensions/khr/timeline_semaphore.rs | 33 +- 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 | 106 ++- ash/src/instance.rs | 111 ++- ash/src/prelude.rs | 19 + ash/src/vk.rs | 6 +- ash/src/vk/definitions.rs | 29 +- ash/src/vk/enums.rs | 1 + generator/Cargo.toml | 2 +- generator/src/lib.rs | 474 +++++++------ 30 files changed, 939 insertions(+), 1194 deletions(-) diff --git a/ash/src/device.rs b/ash/src/device.rs index 84ef40b..2f05f29 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 = ""] @@ -137,22 +131,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 +174,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 = ""] @@ -322,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 = ""] @@ -354,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 = ""] @@ -456,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 @@ -482,20 +452,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( @@ -736,13 +698,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 = ""] @@ -767,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 = ""] @@ -942,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 = ""] @@ -955,25 +914,19 @@ 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 = ""] 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 = ""] @@ -983,16 +936,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 = ""] @@ -1001,13 +952,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 +963,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 +974,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 = ""] @@ -1456,21 +1393,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 = ""] @@ -1527,16 +1461,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 = ""] @@ -1592,16 +1524,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 = ""] @@ -1611,16 +1541,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 = ""] @@ -1630,17 +1558,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 = ""] @@ -1649,15 +1574,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(), @@ -1666,10 +1590,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 = ""] @@ -1681,13 +1602,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 = ""] @@ -1700,28 +1617,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 = ""] @@ -1731,16 +1636,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 = ""] @@ -1783,16 +1686,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 = ""] @@ -1801,22 +1702,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 = ""] @@ -1826,17 +1719,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 = ""] @@ -1851,11 +1742,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 = ""] @@ -1865,13 +1752,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 = ""] @@ -1881,16 +1764,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 = ""] @@ -1913,16 +1794,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 = ""] @@ -1937,10 +1816,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 = ""] @@ -1950,16 +1826,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 = ""] @@ -1969,16 +1843,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 = ""] @@ -1988,16 +1860,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 = ""] @@ -2039,16 +1909,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 = ""] @@ -2058,16 +1926,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 = ""] @@ -2077,16 +1943,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 = ""] @@ -2096,13 +1960,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 = ""] @@ -2112,13 +1972,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/entry.rs b/ash/src/entry.rs index d812341..3a4d689 100644 --- a/ash/src/entry.rs +++ b/ash/src/entry.rs @@ -58,17 +58,14 @@ pub trait EntryV1_0 { unsafe { let mut num = 0; self.fp_v1_0() - .enumerate_instance_layer_properties(&mut num, ptr::null_mut()); - + .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) } } @@ -76,11 +73,9 @@ pub trait EntryV1_0 { fn enumerate_instance_extension_properties(&self) -> VkResult> { unsafe { let mut num = 0; - self.fp_v1_0().enumerate_instance_extension_properties( - ptr::null(), - &mut num, - ptr::null_mut(), - ); + 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(), @@ -88,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) } } @@ -119,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 { @@ -146,11 +138,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) } } } @@ -228,11 +218,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) } diff --git a/ash/src/extensions/ext/debug_marker.rs b/ash/src/extensions/ext/debug_marker.rs index 626f23a..5218d56 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_report.rs b/ash/src/extensions/ext/debug_report.rs index 56dce80..18440f1 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 fdafd3d..b2fadc4 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 = ""] @@ -114,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 e3ff554..eae91a8 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/ext/tooling_info.rs b/ash/src/extensions/ext/tooling_info.rs index 4212c88..fc40533 100644 --- a/ash/src/extensions/ext/tooling_info.rs +++ b/ash/src/extensions/ext/tooling_info.rs @@ -34,16 +34,14 @@ impl ToolingInfo { ) -> VkResult> { let mut count = 0; self.tooling_info_fn - .get_physical_device_tool_properties_ext(physical_device, &mut count, ptr::null_mut()); + .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/android_surface.rs b/ash/src/extensions/khr/android_surface.rs index 0a22d47..606e4ea 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 8ea5e62..b40d33e 100755 --- a/ash/src/extensions/khr/display.rs +++ b/ash/src/extensions/khr/display.rs @@ -33,11 +33,13 @@ impl Display { physical_device: vk::PhysicalDevice, ) -> VkResult> { let mut count = 0; - self.display_fn.get_physical_device_display_properties_khr( - physical_device, - &mut count, - ptr::null_mut(), - ); + 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, @@ -45,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 = ""] @@ -57,12 +56,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 @@ -72,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 = ""] @@ -85,12 +85,14 @@ impl Display { plane_index: u32, ) -> VkResult> { let mut count = 0; - self.display_fn.get_display_plane_supported_displays_khr( - physical_device, - plane_index, - &mut count, - ptr::null_mut(), - ); + 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, @@ -99,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 = ""] @@ -112,12 +111,9 @@ impl Display { display: vk::DisplayKHR, ) -> VkResult> { let mut count = 0; - self.display_fn.get_display_mode_properties_khr( - physical_device, - display, - &mut count, - ptr::null_mut(), - ); + 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, @@ -126,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 = ""] @@ -141,17 +134,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 = ""] @@ -162,16 +153,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 = ""] @@ -181,16 +170,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/display_swapchain.rs b/ash/src/extensions/khr/display_swapchain.rs index bb4cfe2..a805c85 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 070b33a..ad6d6a5 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 = ""] @@ -44,16 +41,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/pipeline_executable_properties.rs b/ash/src/extensions/khr/pipeline_executable_properties.rs index 493be7f..133c988 100644 --- a/ash/src/extensions/khr/pipeline_executable_properties.rs +++ b/ash/src/extensions/khr/pipeline_executable_properties.rs @@ -39,26 +39,26 @@ impl PipelineExecutableProperties { executable_info: &vk::PipelineExecutableInfoKHR, ) -> VkResult> { let mut count = 0; - self.pipeline_executable_properties_fn - .get_pipeline_executable_internal_representations_khr( - device, - executable_info, - &mut count, - ptr::null_mut(), - ); - let mut v: Vec<_> = vec![Default::default(); count as usize]; let err_code = self .pipeline_executable_properties_fn .get_pipeline_executable_internal_representations_khr( device, executable_info, &mut count, - v.as_mut_ptr(), + ptr::null_mut(), ); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), + if err_code != vk::Result::SUCCESS { + return Err(err_code); } + let mut v: Vec<_> = vec![Default::default(); count as usize]; + self.pipeline_executable_properties_fn + .get_pipeline_executable_internal_representations_khr( + device, + executable_info, + &mut count, + v.as_mut_ptr(), + ) + .result_with_success(v) } #[doc = "https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPipelineExecutablePropertiesKHR.html"] @@ -68,26 +68,26 @@ impl PipelineExecutableProperties { pipeline_info: &vk::PipelineInfoKHR, ) -> VkResult> { let mut count = 0; - self.pipeline_executable_properties_fn - .get_pipeline_executable_properties_khr( - device, - pipeline_info, - &mut count, - ptr::null_mut(), - ); - let mut v: Vec<_> = vec![Default::default(); count as usize]; let err_code = self .pipeline_executable_properties_fn .get_pipeline_executable_properties_khr( device, pipeline_info, &mut count, - v.as_mut_ptr(), + ptr::null_mut(), ); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), + if err_code != vk::Result::SUCCESS { + return Err(err_code); } + let mut v: Vec<_> = vec![Default::default(); count as usize]; + self.pipeline_executable_properties_fn + .get_pipeline_executable_properties_khr( + device, + pipeline_info, + &mut count, + v.as_mut_ptr(), + ) + .result_with_success(v) } #[doc = "https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPipelineExecutableStatisticsKHR.html"] @@ -97,26 +97,26 @@ impl PipelineExecutableProperties { executable_info: &vk::PipelineExecutableInfoKHR, ) -> VkResult> { let mut count = 0; - self.pipeline_executable_properties_fn - .get_pipeline_executable_statistics_khr( - device, - executable_info, - &mut count, - ptr::null_mut(), - ); - let mut v: Vec<_> = vec![Default::default(); count as usize]; let err_code = self .pipeline_executable_properties_fn .get_pipeline_executable_statistics_khr( device, executable_info, &mut count, - v.as_mut_ptr(), + ptr::null_mut(), ); - match err_code { - vk::Result::SUCCESS => Ok(v), - _ => Err(err_code), + if err_code != vk::Result::SUCCESS { + return Err(err_code); } + let mut v: Vec<_> = vec![Default::default(); count as usize]; + self.pipeline_executable_properties_fn + .get_pipeline_executable_statistics_khr( + device, + executable_info, + &mut count, + v.as_mut_ptr(), + ) + .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 76a3011..3257473 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 = ""] @@ -87,15 +85,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 = ""] @@ -160,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 = ""] @@ -180,8 +174,9 @@ impl RayTracing { pipeline: vk::Pipeline, first_group: u32, group_count: u32, - data: &mut [u8], - ) -> VkResult<()> { + 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( @@ -189,13 +184,11 @@ impl RayTracing { pipeline, first_group, group_count, - data.len(), + data_size, data.as_mut_ptr() as *mut std::ffi::c_void, ); - match err_code { - vk::Result::SUCCESS => Ok(()), - _ => Err(err_code), - } + data.set_len(data_size); + err_code.result_with_success(data) } #[doc = ""] @@ -250,13 +243,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 +253,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( @@ -302,8 +286,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, @@ -311,12 +294,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( @@ -345,14 +324,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/surface.rs b/ash/src/extensions/khr/surface.rs index 7cde5aa..f702796 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 = ""] @@ -62,7 +54,8 @@ impl Surface { surface, &mut count, ptr::null_mut(), - ); + ) + .result()?; let mut v = Vec::with_capacity(count as usize); let err_code = self .surface_fn @@ -73,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 = ""] @@ -86,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 = ""] @@ -106,12 +92,14 @@ impl Surface { surface: vk::SurfaceKHR, ) -> VkResult> { let mut count = 0; - self.surface_fn.get_physical_device_surface_formats_khr( - physical_device, - surface, - &mut count, - ptr::null_mut(), - ); + 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, @@ -120,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 266b987..9a65273 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. @@ -106,12 +104,9 @@ impl Swapchain { swapchain: vk::SwapchainKHR, ) -> VkResult> { let mut count = 0; - self.swapchain_fn.get_swapchain_images_khr( - self.handle, - swapchain, - &mut count, - ptr::null_mut(), - ); + 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( @@ -121,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 57ab92f..bb34878 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 = ""] @@ -51,14 +46,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 +57,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/khr/wayland_surface.rs b/ash/src/extensions/khr/wayland_surface.rs index d705f38..2091928 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 6f2aea6..5944351 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 af55f6b..cf638a9 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 = ""] @@ -87,15 +85,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 = ""] @@ -182,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 = ""] @@ -204,18 +198,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 = ""] @@ -225,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 = ""] @@ -259,13 +249,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 669eed4..e36f4df 100644 --- a/ash/src/instance.rs +++ b/ash/src/instance.rs @@ -49,17 +49,16 @@ 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, - 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 { @@ -92,14 +91,11 @@ 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( - self.handle(), - &mut group_count, - ptr::null_mut(), - ); - group_count as usize + self.fp_v1_1() + .enumerate_physical_device_groups(self.handle(), &mut group_count, ptr::null_mut()) + .result_with_success(group_count as usize) } #[doc = ""] @@ -109,16 +105,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 +149,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( @@ -304,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( @@ -347,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 = ""] @@ -421,7 +404,8 @@ pub trait InstanceV1_0 { 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()); + .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(), @@ -429,24 +413,18 @@ 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(), - ); + 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, @@ -455,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) } } diff --git a/ash/src/prelude.rs b/ash/src/prelude.rs index b4a888c..112b0d4 100644 --- a/ash/src/prelude.rs +++ b/ash/src/prelude.rs @@ -1,2 +1,21 @@ use crate::vk; pub type VkResult = Result; + +impl From for VkResult<()> { + fn from(err_code: vk::Result) -> Self { + 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), + } + } +} diff --git a/ash/src/vk.rs b/ash/src/vk.rs index b6f4ef4..9308f08 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::*; diff --git a/ash/src/vk/definitions.rs b/ash/src/vk/definitions.rs index a853493..d4b5be2 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"] @@ -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( @@ -20903,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 } @@ -20983,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 } @@ -21063,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 } @@ -33205,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 @@ -37488,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 @@ -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/ash/src/vk/enums.rs b/ash/src/vk/enums.rs index 8d8a1f0..f473274 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/Cargo.toml b/generator/Cargo.toml index 3785254..6d93432 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 1420351..13a657e 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -5,8 +5,7 @@ use quote::*; use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; use itertools::Itertools; -use proc_macro2::{Literal, Term}; -use quote::Tokens; +use proc_macro2::{Literal, Term, TokenNode, TokenStream, TokenTree}; use std::collections::{BTreeMap, HashMap, HashSet}; use std::fmt::Display; use std::hash::BuildHasher; @@ -23,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"), @@ -32,7 +31,7 @@ impl CType { CType::Float => Term::intern("f32"), CType::Bool32 => Term::intern("Bool32"), }; - quote! {#term} + term.to_tokens(tokens); } } @@ -78,7 +77,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 { @@ -328,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; @@ -359,7 +372,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; @@ -386,7 +399,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>; } @@ -397,9 +409,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() } @@ -412,9 +421,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() } @@ -430,6 +436,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 { @@ -476,39 +512,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())); @@ -547,6 +550,7 @@ impl FeatureExt for vkxml::Feature { if version.len() == 1 { version = format!("{}_0", version) } + version.replace(".", "_") } } @@ -602,10 +606,17 @@ 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; + /// 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. @@ -615,29 +626,33 @@ 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 { - 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), + } + } + + 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), } } } @@ -658,27 +673,52 @@ 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()) } -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! {}); - 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)` +/// - `2*VK_UUID_SIZE` -> `2 * UUID_SIZE` +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 } + fn param_ident(&self) -> Ident { let name = self.name.as_deref().unwrap_or("field"); let name_corrected = match name { @@ -688,18 +728,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() @@ -707,22 +770,19 @@ 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 { - 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) + } + } } } @@ -943,9 +1003,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 } @@ -971,6 +1028,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)) @@ -1150,7 +1208,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() @@ -1162,11 +1220,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 proc_macro2::TokenStream; - 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!() @@ -1176,12 +1231,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 = name_to_tokens(&typedef.name); + let typedef_ty = name_to_tokens(&typedef.basetype); + let khronos_link = khronos_link(&typedef.name); + quote! { + #[doc = #khronos_link] + pub type #typedef_name = #typedef_ty; + } } } pub fn generate_bitmask( @@ -1261,11 +1321,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) }) @@ -1351,11 +1410,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) } @@ -1363,13 +1429,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 @@ -1443,9 +1502,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| { @@ -1602,19 +1661,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", @@ -1650,21 +1699,17 @@ 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_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" { 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() { @@ -1695,81 +1740,68 @@ 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.reference.is_some() { + 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 + } }); } - 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 mut slice_param_ty_tokens = field.safe_type_tokens(quote!('a)); - 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 mut ptr = if field.is_const { + quote!(.as_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_mut_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 - } - }); - } + // Interpret void array as byte array + if field.basetype == "void" { + let mutable = if field.is_const { quote!(const) } else { quote!(mut) }; + + slice_param_ty_tokens = quote!([u8]); + ptr = quote!(#ptr as *#mutable c_void); + }; + + let mutable = if field.is_const { quote!() } else { quote!(mut) }; + + // 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(); + + 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> { + #set_size_stmt + self.inner.#param_ident = #param_ident_short#ptr; + self + } + }); } } } - - if param_ty_string.starts_with("*const ") { - let slice_param_ty_tokens = "&'a ".to_string() + ¶m_ty_string[7..]; - 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> { - self.inner.#param_ident = #param_ident_short; - self - } - }); - } } - 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(); @@ -1778,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; @@ -1983,7 +2022,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); @@ -2026,7 +2065,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); @@ -2169,8 +2208,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>( @@ -2180,16 +2220,14 @@ 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 value = c.to_tokens(); + let ident = Ident::from(name); 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; } } @@ -2287,8 +2325,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