Remove FunctionPointers from Entry/Device/Instance

This commit is contained in:
Maik Klein 2018-11-11 10:40:09 +01:00
parent 7b052ade5c
commit 03665e5555
3 changed files with 139 additions and 137 deletions

View file

@ -1717,46 +1717,48 @@ pub trait DeviceV1_0 {
}
#[derive(Clone)]
pub struct Device<V: FunctionPointers> {
pub struct Device {
handle: vk::Device,
device_fn: V::DeviceFp,
device_fn_1_0: vk::DeviceFnV1_0,
device_fn_1_1: vk::DeviceFnV1_1,
}
impl DeviceV1_0 for Device<V1_0> {
fn handle(&self) -> vk::Device {
self.handle
}
fn fp_v1_0(&self) -> &vk::DeviceFnV1_0 {
&self.device_fn.device_fn
}
}
impl DeviceV1_0 for Device<V1_1> {
fn handle(&self) -> vk::Device {
self.handle
}
fn fp_v1_0(&self) -> &vk::DeviceFnV1_0 {
&self.device_fn.device_fn_1_0
}
}
impl DeviceV1_1 for Device<V1_1> {
fn fp_v1_1(&self) -> &vk::DeviceFnV1_1 {
&self.device_fn.device_fn_1_1
}
}
impl<V: FunctionPointers> Device<V> {
pub fn handle(&self) -> vk::Device {
self.handle
}
pub unsafe fn from_raw(handle: vk::Device, device_fn: V::DeviceFp) -> Self {
impl Device {
pub unsafe fn load(
instance_fn: &vk::InstanceFnV1_0,
device: vk::Device,
) -> Self {
let device_fn_1_0 = vk::DeviceFnV1_0::load(|name| {
mem::transmute(instance_fn.get_device_proc_addr(device, name.as_ptr()))
});
let device_fn_1_1 = vk::DeviceFnV1_1::load(|name| {
mem::transmute(instance_fn.get_device_proc_addr(device, name.as_ptr()))
});
Device {
handle: handle,
device_fn: device_fn,
handle: device,
device_fn_1_0,
device_fn_1_1,
}
}
}
impl DeviceV1_0 for Device {
fn handle(&self) -> vk::Device {
self.handle
}
fn fp_v1_0(&self) -> &vk::DeviceFnV1_0 {
&self.device_fn_1_0
}
}
impl DeviceV1_1 for Device {
fn fp_v1_1(&self) -> &vk::DeviceFnV1_1 {
&self.device_fn_1_1
}
}
impl Device {
pub fn handle(&self) -> vk::Device {
self.handle
}
}

View file

@ -14,7 +14,18 @@ use RawPtr;
#[cfg(windows)]
const LIB_PATH: &'static str = "vulkan-1.dll";
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios", target_os = "android"))))]
#[cfg(
all(
unix,
not(
any(
target_os = "macos",
target_os = "ios",
target_os = "android"
)
)
)
)]
const LIB_PATH: &'static str = "libvulkan.so.1";
#[cfg(target_os = "android")]
@ -29,9 +40,10 @@ lazy_static! {
}
#[derive(Clone)]
pub struct Entry<V: FunctionPointers> {
pub struct Entry {
static_fn: vk::StaticFn,
entry_fn: V::EntryFp,
entry_fn_1_0: vk::EntryFnV1_0,
entry_fn_1_1: vk::EntryFnV1_1,
}
#[derive(Debug)]
@ -66,29 +78,14 @@ impl Error for InstanceError {
#[allow(non_camel_case_types)]
pub trait EntryV1_0 {
type Fp: FunctionPointers;
type Instance;
fn fp_v1_0(&self) -> &vk::EntryFnV1_0;
fn static_fn(&self) -> &vk::StaticFn;
unsafe fn create_instance(
&self,
create_info: &vk::InstanceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> Result<Instance<Self::Fp>, InstanceError> {
let mut instance: vk::Instance = mem::uninitialized();
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));
}
let instance_fp =
<Self::Fp as FunctionPointers>::InstanceFp::load(&self.static_fn(), instance);
Ok(Instance::from_raw(instance, instance_fp))
}
) -> Result<Self::Instance, InstanceError>;
fn enumerate_instance_layer_properties(&self) -> VkResult<Vec<vk::LayerProperties>> {
unsafe {
let mut num = 0;
@ -106,7 +103,6 @@ pub trait EntryV1_0 {
}
}
}
fn enumerate_instance_extension_properties(&self) -> VkResult<Vec<vk::ExtensionProperties>> {
unsafe {
let mut num = 0;
@ -138,33 +134,33 @@ pub trait EntryV1_0 {
}
}
impl EntryV1_0 for Entry<V1_0> {
type Fp = V1_0;
impl EntryV1_0 for Entry {
type Instance = Instance;
unsafe fn create_instance(
&self,
create_info: &vk::InstanceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> Result<Self::Instance, InstanceError> {
let mut instance: vk::Instance = mem::uninitialized();
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.fp_v1_0()
&self.entry_fn_1_0
}
fn static_fn(&self) -> &vk::StaticFn {
&self.static_fn
}
}
impl EntryV1_0 for Entry<V1_1> {
type Fp = V1_1;
fn fp_v1_0(&self) -> &vk::EntryFnV1_0 {
self.entry_fn.fp_v1_0()
}
fn static_fn(&self) -> &vk::StaticFn {
&self.static_fn
}
}
impl EntryV1_1 for Entry<V1_1> {
fn fp_v1_1(&self) -> &vk::EntryFnV1_1 {
&self.entry_fn.entry_fn_1_1
}
}
impl<V: FunctionPointers> Entry<V> {
impl Entry {
pub fn new() -> Result<Self, LoadingError> {
let lib = VK_LIB
.as_ref()
@ -175,12 +171,18 @@ impl<V: FunctionPointers> Entry<V> {
.unwrap_or(ptr::null_mut())
});
let entry_fn =
unsafe { V::EntryFp::load(&static_fn) };
let entry_fn_1_0 = vk::EntryFnV1_0::load(|name| unsafe {
mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr()))
});
let entry_fn_1_1 = vk::EntryFnV1_1::load(|name| unsafe {
mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr()))
});
Ok(Entry {
static_fn,
entry_fn,
entry_fn_1_0,
entry_fn_1_1,
})
}
}

View file

@ -10,52 +10,66 @@ use vk;
use RawPtr;
#[derive(Clone)]
pub struct Instance<V: FunctionPointers> {
pub struct Instance {
handle: vk::Instance,
instance_fp: V::InstanceFp,
instance_fn_1_0: vk::InstanceFnV1_0,
instance_fn_1_1: vk::InstanceFnV1_1,
}
impl Instance {
pub unsafe fn load(
static_fn: &vk::StaticFn,
instance: vk::Instance,
) -> Self {
let instance_fn_1_0 = vk::InstanceFnV1_0::load(|name| {
mem::transmute(static_fn.get_instance_proc_addr(instance, name.as_ptr()))
});
let instance_fn_1_1 = vk::InstanceFnV1_1::load(|name| {
mem::transmute(static_fn.get_instance_proc_addr(instance, name.as_ptr()))
});
impl InstanceV1_0 for Instance<V1_0> {
type Fp = V1_0;
fn handle(&self) -> vk::Instance {
self.handle
}
fn fp_v1_0(&self) -> &vk::InstanceFnV1_0 {
&self.instance_fp.instance_fn
}
}
impl InstanceV1_0 for Instance<V1_1> {
type Fp = V1_1;
fn handle(&self) -> vk::Instance {
self.handle
}
fn fp_v1_0(&self) -> &vk::InstanceFnV1_0 {
&self.instance_fp.instance_fn_1_0
}
}
impl InstanceV1_1 for Instance<V1_1> {
fn fp_v1_1(&self) -> &vk::InstanceFnV1_1 {
&self.instance_fp.instance_fn_1_1
}
}
impl<V: FunctionPointers> Instance<V> {
pub fn handle(&self) -> vk::Instance {
self.handle
}
pub fn from_raw(handle: vk::Instance, version: V::InstanceFp) -> Self {
Instance {
handle: handle,
instance_fp: version,
handle: instance,
instance_fn_1_0,
instance_fn_1_1,
}
}
}
impl InstanceV1_0 for Instance {
type Device = Device;
unsafe fn create_device(
&self,
physical_device: vk::PhysicalDevice,
create_info: &vk::DeviceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> Result<Self::Device, vk::Result> {
let mut device: vk::Device = mem::uninitialized();
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);
}
Ok(Device::load(&self.instance_fn_1_0, device))
}
fn handle(&self) -> vk::Instance {
self.handle
}
fn fp_v1_0(&self) -> &vk::InstanceFnV1_0 {
&self.instance_fn_1_0
}
}
impl InstanceV1_1 for Instance {
fn fp_v1_1(&self) -> &vk::InstanceFnV1_1 {
&self.instance_fn_1_1
}
}
#[allow(non_camel_case_types)]
pub trait InstanceV1_1: InstanceV1_0 {
fn fp_v1_1(&self) -> &vk::InstanceFnV1_1;
@ -232,7 +246,7 @@ pub trait InstanceV1_1: InstanceV1_0 {
#[allow(non_camel_case_types)]
pub trait InstanceV1_0 {
type Fp: FunctionPointers;
type Device;
fn handle(&self) -> vk::Instance;
fn fp_v1_0(&self) -> &vk::InstanceFnV1_0;
unsafe fn create_device(
@ -240,23 +254,7 @@ pub trait InstanceV1_0 {
physical_device: vk::PhysicalDevice,
create_info: &vk::DeviceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> Result<Device<Self::Fp>, vk::Result> {
let mut device: vk::Device = mem::uninitialized();
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);
}
let device_fn = <<Self as InstanceV1_0>::Fp as FunctionPointers>::DeviceFp::load(
self.fp_v1_0(),
device,
);
Ok(Device::from_raw(device, device_fn))
}
) -> Result<Self::Device, vk::Result>;
unsafe fn get_device_proc_addr(
&self,