diff --git a/ash/src/device.rs b/ash/src/device.rs index b21fa7c..8b9cbc0 100644 --- a/ash/src/device.rs +++ b/ash/src/device.rs @@ -3,7 +3,6 @@ use prelude::*; use std::mem; use std::os::raw::c_void; use std::ptr; -use version::{FunctionPointers, V1_0, V1_1}; use vk; use RawPtr; @@ -1717,46 +1716,48 @@ pub trait DeviceV1_0 { } #[derive(Clone)] -pub struct Device { +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 { - 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 { - 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 { - fn fp_v1_1(&self) -> &vk::DeviceFnV1_1 { - &self.device_fn.device_fn_1_1 - } -} - -impl Device { - 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 + } +} diff --git a/ash/src/entry.rs b/ash/src/entry.rs index e75f569..a6955d0 100644 --- a/ash/src/entry.rs +++ b/ash/src/entry.rs @@ -7,14 +7,24 @@ use std::mem; use std::os::raw::c_char; use std::path::Path; use std::ptr; -use version::{EntryLoader, FunctionPointers, InstanceLoader, V1_0, V1_1}; use vk; 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 +39,10 @@ lazy_static! { } #[derive(Clone)] -pub struct Entry { +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 +77,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, 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 = - ::InstanceFp::load(&self.static_fn(), instance); - Ok(Instance::from_raw(instance, instance_fp)) - } - + ) -> Result; fn enumerate_instance_layer_properties(&self) -> VkResult> { unsafe { let mut num = 0; @@ -106,7 +102,6 @@ pub trait EntryV1_0 { } } } - fn enumerate_instance_extension_properties(&self) -> VkResult> { unsafe { let mut num = 0; @@ -138,33 +133,33 @@ pub trait EntryV1_0 { } } -impl EntryV1_0 for Entry { - 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 { + 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 { - 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 { - fn fp_v1_1(&self) -> &vk::EntryFnV1_1 { - &self.entry_fn.entry_fn_1_1 - } -} - -impl Entry { +impl Entry { pub fn new() -> Result { let lib = VK_LIB .as_ref() @@ -175,12 +170,18 @@ impl Entry { .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, }) } } diff --git a/ash/src/instance.rs b/ash/src/instance.rs index c86b9a7..6137c1d 100644 --- a/ash/src/instance.rs +++ b/ash/src/instance.rs @@ -4,58 +4,70 @@ use prelude::*; use std::mem; use std::os::raw::c_char; use std::ptr; -use version::DeviceLoader; -use version::{FunctionPointers, V1_0, V1_1}; use vk; use RawPtr; #[derive(Clone)] -pub struct Instance { +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 { - 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 { - 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 { - fn fp_v1_1(&self) -> &vk::InstanceFnV1_1 { - &self.instance_fp.instance_fn_1_1 - } -} - -impl Instance { - 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 { + 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 +244,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 +252,7 @@ pub trait InstanceV1_0 { physical_device: vk::PhysicalDevice, create_info: &vk::DeviceCreateInfo, allocation_callbacks: Option<&vk::AllocationCallbacks>, - ) -> Result, 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 = <::Fp as FunctionPointers>::DeviceFp::load( - self.fp_v1_0(), - device, - ); - Ok(Device::from_raw(device, device_fn)) - } + ) -> Result; unsafe fn get_device_proc_addr( &self, diff --git a/ash/src/version.rs b/ash/src/version.rs index 4d86da3..922f6bf 100644 --- a/ash/src/version.rs +++ b/ash/src/version.rs @@ -1,182 +1,3 @@ pub use device::{DeviceV1_0, DeviceV1_1}; pub use entry::{EntryV1_0, EntryV1_1}; pub use instance::{InstanceV1_0, InstanceV1_1}; -use std::mem; -use vk; -pub trait FunctionPointers { - type InstanceFp: InstanceLoader + Clone; - type DeviceFp: DeviceLoader + Clone; - type EntryFp: EntryLoader + Clone; -} - -#[allow(non_camel_case_types)] -#[derive(Clone)] -pub struct V1_1; -impl FunctionPointers for V1_1 { - type InstanceFp = InstanceFpV1_1; - type DeviceFp = DeviceFpV1_1; - type EntryFp = EntryFpV1_1; -} - -#[allow(non_camel_case_types)] -#[derive(Clone)] -pub struct V1_0; -impl FunctionPointers for V1_0 { - type InstanceFp = InstanceFpV1_0; - type DeviceFp = DeviceFpV1_0; - type EntryFp = EntryFpV1_0; -} - -#[allow(non_camel_case_types)] -#[derive(Clone)] -pub struct EntryFpV1_0 { - pub entry_fn: vk::EntryFnV1_0, -} - -impl EntryLoader for EntryFpV1_0 { - fn fp_v1_0(&self) -> &vk::EntryFnV1_0 { - &self.entry_fn - } - unsafe fn load(static_fn: &vk::StaticFn) -> Self { - let entry_fn = vk::EntryFnV1_0::load(|name| { - mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr())) - }); - EntryFpV1_0 { entry_fn: entry_fn } - } -} - -pub trait EntryLoader: Sized { - fn fp_v1_0(&self) -> &vk::EntryFnV1_0; - unsafe fn load(static_fn: &vk::StaticFn) -> Self; -} - -#[allow(non_camel_case_types)] -#[derive(Clone)] -pub struct EntryFpV1_1 { - pub entry_fn_1_0: vk::EntryFnV1_0, - pub entry_fn_1_1: vk::EntryFnV1_1, -} - -impl EntryLoader for EntryFpV1_1 { - fn fp_v1_0(&self) -> &vk::EntryFnV1_0 { - &self.entry_fn_1_0 - } - unsafe fn load(static_fn: &vk::StaticFn) -> Self { - let entry_fn_1_0 = vk::EntryFnV1_0::load(|name| { - mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr())) - }); - let entry_fn_1_1 = vk::EntryFnV1_1::load(|name| { - mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr())) - }); - - EntryFpV1_1 { - entry_fn_1_0, - entry_fn_1_1, - } - } -} - -pub trait InstanceLoader: Sized { - unsafe fn load( - static_fn: &vk::StaticFn, - instance: vk::Instance, - ) -> Self; -} - -#[allow(non_camel_case_types)] -#[derive(Clone)] -pub struct InstanceFpV1_0 { - pub instance_fn: vk::InstanceFnV1_0, -} - -impl InstanceLoader for InstanceFpV1_0 { - unsafe fn load( - static_fn: &vk::StaticFn, - instance: vk::Instance, - ) -> Self { - let instance_fn = vk::InstanceFnV1_0::load(|name| { - mem::transmute(static_fn.get_instance_proc_addr(instance, name.as_ptr())) - }); - InstanceFpV1_0 { - instance_fn: instance_fn, - } - } -} - -#[allow(non_camel_case_types)] -#[derive(Clone)] -pub struct InstanceFpV1_1 { - pub instance_fn_1_0: vk::InstanceFnV1_0, - pub instance_fn_1_1: vk::InstanceFnV1_1, -} - -impl InstanceLoader for InstanceFpV1_1 { - 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())) - }); - - InstanceFpV1_1 { - instance_fn_1_0, - instance_fn_1_1, - } - } -} - -pub trait DeviceLoader: Sized { - unsafe fn load( - instance_fn: &vk::InstanceFnV1_0, - device: vk::Device, - ) -> Self; -} - -#[allow(non_camel_case_types)] -#[derive(Clone)] -pub struct DeviceFpV1_0 { - pub device_fn: vk::DeviceFnV1_0, -} - -impl DeviceLoader for DeviceFpV1_0 { - unsafe fn load( - instance_fn: &vk::InstanceFnV1_0, - device: vk::Device, - ) -> Self { - let device_fn = vk::DeviceFnV1_0::load(|name| { - mem::transmute(instance_fn.get_device_proc_addr(device, name.as_ptr())) - }); - DeviceFpV1_0 { - device_fn: device_fn, - } - } -} - -#[allow(non_camel_case_types)] -#[derive(Clone)] -pub struct DeviceFpV1_1 { - pub device_fn_1_0: vk::DeviceFnV1_0, - pub device_fn_1_1: vk::DeviceFnV1_1, -} - -impl DeviceLoader for DeviceFpV1_1 { - 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())) - }); - DeviceFpV1_1 { - device_fn_1_0, - device_fn_1_1, - } - } -} diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index d87df89..5aece4c 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -1,5 +1,4 @@ extern crate ash; -#[macro_use] extern crate examples; extern crate image; diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 12a3b85..c0ba9f1 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -1,5 +1,4 @@ extern crate ash; -#[macro_use] extern crate examples; use ash::util::*; diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 61acd62..6b7fc84 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -9,7 +9,7 @@ use ash::extensions::Win32Surface; #[cfg(not(windows))] use ash::extensions::XlibSurface; use ash::extensions::{DebugReport, Surface, Swapchain}; -pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0, V1_0}; +pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0,}; use ash::vk; use ash::Device; use ash::Entry; @@ -205,9 +205,9 @@ pub fn find_memorytype_index_f, - pub instance: Instance, - pub device: Device, + pub entry: Entry, + pub instance: Instance, + pub device: Device, pub surface_loader: Surface, pub swapchain_loader: Swapchain, pub debug_report_loader: DebugReport, @@ -301,7 +301,7 @@ impl ExampleBase { pp_enabled_extension_names: extension_names_raw.as_ptr(), enabled_extension_count: extension_names_raw.len() as u32, }; - let instance: Instance = entry + let instance: Instance = entry .create_instance(&create_info, None) .expect("Instance creation error"); let debug_info = vk::DebugReportCallbackCreateInfoEXT { @@ -374,7 +374,7 @@ impl ExampleBase { pp_enabled_extension_names: device_extension_names_raw.as_ptr(), p_enabled_features: &features, }; - let device: Device = instance + let device: Device = instance .create_device(pdevice, &device_create_info, None) .unwrap(); let present_queue = device.get_device_queue(queue_family_index as u32, 0);