Skip to content

Commit

Permalink
lib: Provide entrypoint through Ash loader w.o. implementing Entry
Browse files Browse the repository at this point in the history
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]: ash-rs/ash#412
  • Loading branch information
MarijnS95 committed Aug 31, 2021
1 parent 61ec5fd commit 5877ba1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 3 additions & 6 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,19 @@
// 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()
.application_name(&app_name)
.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");
Expand Down
74 changes: 21 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<MoltenEntry, ash::LoadingError> {
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 = "<https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCreateInstance.html>"]
unsafe fn create_instance(
&self,
create_info: &vk::InstanceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> Result<Self::Instance, InstanceError> {
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
}
}

0 comments on commit 5877ba1

Please sign in to comment.