Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

device,instance: Provide load_with() constructor for get_proc_addr closure #846

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `descriptor_count()` setter on `ash::vk::WriteDescriptorSet` (#809)
- Added `*_as_c_str()` getters for `c_char` pointers and `c_char` arrays (#831)
- Added `#[must_use]` to Vulkan structs to make it more clear that they are moved by the builder pattern (#845)
- Added `load_with()` function on `Device` and `Instance` for providing custom `get_xxx_proc_addr()` implementations (#846)
- Added `Send`/`Sync` to all Vulkan structs (#869)

### Changed
Expand Down
28 changes: 18 additions & 10 deletions ash/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use crate::prelude::*;
use crate::vk;
use crate::RawPtr;
use core::ffi;
use std::mem;
use std::os::raw::c_void;
use std::ptr;

/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDevice.html>
Expand All @@ -19,16 +19,24 @@ pub struct Device {

impl Device {
pub unsafe fn load(instance_fn: &vk::InstanceFnV1_0, device: vk::Device) -> Self {
let load_fn = |name: &std::ffi::CStr| {
mem::transmute((instance_fn.get_device_proc_addr)(device, name.as_ptr()))
};
Self::load_with(
|name: &std::ffi::CStr| {
mem::transmute((instance_fn.get_device_proc_addr)(device, name.as_ptr()))
},
device,
)
}

pub unsafe fn load_with(
mut load_fn: impl FnMut(&ffi::CStr) -> *const ffi::c_void,
device: vk::Device,
) -> Self {
Self::from_parts_1_3(
device,
vk::DeviceFnV1_0::load(load_fn),
vk::DeviceFnV1_1::load(load_fn),
vk::DeviceFnV1_2::load(load_fn),
vk::DeviceFnV1_3::load(load_fn),
vk::DeviceFnV1_0::load(&mut load_fn),
vk::DeviceFnV1_1::load(&mut load_fn),
vk::DeviceFnV1_2::load(&mut load_fn),
vk::DeviceFnV1_3::load(&mut load_fn),
)
}

Expand Down Expand Up @@ -951,7 +959,7 @@ impl Device {
&self,
descriptor_set: vk::DescriptorSet,
descriptor_update_template: vk::DescriptorUpdateTemplate,
data: *const c_void,
data: *const ffi::c_void,
) {
(self.device_fn_1_1.update_descriptor_set_with_template)(
self.handle(),
Expand Down Expand Up @@ -2273,7 +2281,7 @@ impl Device {
offset: vk::DeviceSize,
size: vk::DeviceSize,
flags: vk::MemoryMapFlags,
) -> VkResult<*mut c_void> {
) -> VkResult<*mut ffi::c_void> {
let mut data = mem::MaybeUninit::uninit();
(self.device_fn_1_0.map_memory)(
self.handle(),
Expand Down
22 changes: 14 additions & 8 deletions ash/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::device::Device;
use crate::prelude::*;
use crate::vk;
use crate::RawPtr;
use core::ffi;
use std::mem;
use std::os::raw::c_char;
use std::ptr;

/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkInstance.html>
Expand All @@ -20,15 +20,21 @@ pub struct Instance {

impl Instance {
pub unsafe fn load(static_fn: &vk::StaticFn, instance: vk::Instance) -> Self {
let load_fn = |name: &std::ffi::CStr| {
mem::transmute((static_fn.get_instance_proc_addr)(instance, name.as_ptr()))
};
Self::load_with(
|name| mem::transmute((static_fn.get_instance_proc_addr)(instance, name.as_ptr())),
instance,
)
}

pub unsafe fn load_with(
mut load_fn: impl FnMut(&ffi::CStr) -> *const ffi::c_void,
instance: vk::Instance,
) -> Self {
Self::from_parts_1_3(
instance,
vk::InstanceFnV1_0::load(load_fn),
vk::InstanceFnV1_1::load(load_fn),
vk::InstanceFnV1_3::load(load_fn),
vk::InstanceFnV1_0::load(&mut load_fn),
vk::InstanceFnV1_1::load(&mut load_fn),
vk::InstanceFnV1_3::load(&mut load_fn),
)
}

Expand Down Expand Up @@ -374,7 +380,7 @@ impl Instance {
pub unsafe fn get_device_proc_addr(
&self,
device: vk::Device,
p_name: *const c_char,
p_name: *const ffi::c_char,
) -> vk::PFN_vkVoidFunction {
(self.instance_fn_1_0.get_device_proc_addr)(device, p_name)
}
Expand Down