From 5877ba1b67f5470220e11bac756cfb59c3d78401 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 30 Apr 2021 18:15:06 +0200 Subject: [PATCH] lib: Provide entrypoint through Ash loader w.o. implementing Entry Ash recently [dropped all traits](1) to simplify `ash::Device` usage, but this also disallows ash-molten from overriding the `EntryV1_0` trait to provide a static entrypoint intead. Fortunately ash-molten can simply pass a library loading closure that returns the static address of `vkGetInstanceProcAddr` to `EntryCustom::new_custom`, getting rid of the copied `fn create_instance` implementation at the same time. [1]: https://github.com/MaikKlein/ash/pull/412 --- Cargo.toml | 2 +- src/bin/main.rs | 9 ++---- src/lib.rs | 74 ++++++++++++++----------------------------------- 3 files changed, 25 insertions(+), 60 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2a5e667..5b73e39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ documentation = "https://docs.rs/ash-molten" build = "build/build.rs" [dependencies] -ash = "0.32" +ash = "0.33" [build-dependencies] anyhow = "1.0" diff --git a/src/bin/main.rs b/src/bin/main.rs index 72916bb..1462396 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -70,14 +70,11 @@ // crate-specific exceptions: #![allow(unsafe_code)] -use ash::{ - version::{EntryV1_0, InstanceV1_0}, - vk, -}; +use ash::vk; use std::ffi::CString; fn main() { unsafe { - let entry = ash_molten::MoltenEntry::load().expect("Unable to load Molten"); + let entry = ash_molten::MoltenEntry::load(); let app_name = CString::new("Hello Static Molten").unwrap(); let appinfo = vk::ApplicationInfo::builder() @@ -85,7 +82,7 @@ fn main() { .application_version(0) .engine_name(&app_name) .engine_version(0) - .api_version(vk::make_version(1, 0, 0)); + .api_version(vk::make_api_version(0, 1, 0, 0)); let create_info = vk::InstanceCreateInfo::builder().application_info(&appinfo); let instance = entry.create_instance(&create_info, None).expect("Instance"); diff --git a/src/lib.rs b/src/lib.rs index eaf802b..08d81d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,7 +70,9 @@ // crate-specific exceptions: #![allow(unsafe_code)] -use ash::{version::EntryV1_0, vk, Instance, InstanceError, RawPtr}; +use std::ops::Deref; + +use ash::{vk, EntryCustom}; extern "system" { fn vkGetInstanceProcAddr( @@ -79,62 +81,28 @@ extern "system" { ) -> vk::PFN_vkVoidFunction; } -extern "system" fn get_instance_proc_addr( - instance: vk::Instance, - p_name: *const std::os::raw::c_char, -) -> vk::PFN_vkVoidFunction { - unsafe { vkGetInstanceProcAddr(instance, p_name) } -} - /// The entry point for the statically linked molten library -pub struct MoltenEntry { - static_fn: vk::StaticFn, - entry_fn_1_0: vk::EntryFnV1_0, -} +pub struct MoltenEntry(EntryCustom<()>); impl MoltenEntry { - /// Fetches the function pointer to `get_instance_proc_addr` which is statically linked. This - /// function can not fail. - pub fn load() -> Result { - let static_fn = vk::StaticFn { - get_instance_proc_addr, - }; - - let entry_fn_1_0 = vk::EntryFnV1_0::load(|name| unsafe { - std::mem::transmute( - static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr()), - ) - }); - - Ok(MoltenEntry { - static_fn, - entry_fn_1_0, - }) + /// Fetches the function pointer to `vkGetInstanceProcAddr` which is statically linked. + pub fn load() -> Self { + Self( + EntryCustom::new_custom((), |(), name| { + assert_eq!(name.to_bytes_with_nul(), b"vkGetInstanceProcAddr\0"); + vkGetInstanceProcAddr as _ + }) + // This can never fail because we always return the address of + // `vkGetInstanceProcAddr` from the closure: + .unwrap(), + ) } } -impl EntryV1_0 for MoltenEntry { - type Instance = Instance; - #[doc = ""] - unsafe fn create_instance( - &self, - create_info: &vk::InstanceCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>, - ) -> Result { - let mut instance: vk::Instance = vk::Instance::null(); - 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)); - } - Ok(Instance::load(&self.static_fn, instance)) - } - fn fp_v1_0(&self) -> &vk::EntryFnV1_0 { - &self.entry_fn_1_0 - } - fn static_fn(&self) -> &vk::StaticFn { - &self.static_fn + +impl Deref for MoltenEntry { + type Target = EntryCustom<()>; + + fn deref(&self) -> &Self::Target { + &self.0 } }