Merge pull request #139 from MaikKlein/no_version

Simplify versioning
This commit is contained in:
Maik Klein 2018-11-11 14:16:17 +01:00 committed by GitHub
commit f54832a9fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 145 additions and 328 deletions

View file

@ -3,7 +3,6 @@ use prelude::*;
use std::mem; use std::mem;
use std::os::raw::c_void; use std::os::raw::c_void;
use std::ptr; use std::ptr;
use version::{FunctionPointers, V1_0, V1_1};
use vk; use vk;
use RawPtr; use RawPtr;
@ -1717,46 +1716,48 @@ pub trait DeviceV1_0 {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Device<V: FunctionPointers> { pub struct Device {
handle: vk::Device, handle: vk::Device,
device_fn: V::DeviceFp, device_fn_1_0: vk::DeviceFnV1_0,
device_fn_1_1: vk::DeviceFnV1_1,
}
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: device,
device_fn_1_0,
device_fn_1_1,
}
}
} }
impl DeviceV1_0 for Device<V1_0> { impl DeviceV1_0 for Device {
fn handle(&self) -> vk::Device { fn handle(&self) -> vk::Device {
self.handle self.handle
} }
fn fp_v1_0(&self) -> &vk::DeviceFnV1_0 { fn fp_v1_0(&self) -> &vk::DeviceFnV1_0 {
&self.device_fn.device_fn &self.device_fn_1_0
} }
} }
impl DeviceV1_0 for Device<V1_1> { impl DeviceV1_1 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<V1_1> {
fn fp_v1_1(&self) -> &vk::DeviceFnV1_1 { fn fp_v1_1(&self) -> &vk::DeviceFnV1_1 {
&self.device_fn.device_fn_1_1 &self.device_fn_1_1
} }
} }
impl<V: FunctionPointers> Device<V> { impl Device {
pub fn handle(&self) -> vk::Device { pub fn handle(&self) -> vk::Device {
self.handle self.handle
} }
pub unsafe fn from_raw(handle: vk::Device, device_fn: V::DeviceFp) -> Self {
Device {
handle: handle,
device_fn: device_fn,
}
}
} }

View file

@ -7,14 +7,24 @@ use std::mem;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::path::Path; use std::path::Path;
use std::ptr; use std::ptr;
use version::{EntryLoader, FunctionPointers, InstanceLoader, V1_0, V1_1};
use vk; use vk;
use RawPtr; use RawPtr;
#[cfg(windows)] #[cfg(windows)]
const LIB_PATH: &'static str = "vulkan-1.dll"; 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"; const LIB_PATH: &'static str = "libvulkan.so.1";
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
@ -29,9 +39,10 @@ lazy_static! {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Entry<V: FunctionPointers> { pub struct Entry {
static_fn: vk::StaticFn, static_fn: vk::StaticFn,
entry_fn: V::EntryFp, entry_fn_1_0: vk::EntryFnV1_0,
entry_fn_1_1: vk::EntryFnV1_1,
} }
#[derive(Debug)] #[derive(Debug)]
@ -66,29 +77,14 @@ impl Error for InstanceError {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub trait EntryV1_0 { pub trait EntryV1_0 {
type Fp: FunctionPointers; type Instance;
fn fp_v1_0(&self) -> &vk::EntryFnV1_0; fn fp_v1_0(&self) -> &vk::EntryFnV1_0;
fn static_fn(&self) -> &vk::StaticFn; fn static_fn(&self) -> &vk::StaticFn;
unsafe fn create_instance( unsafe fn create_instance(
&self, &self,
create_info: &vk::InstanceCreateInfo, create_info: &vk::InstanceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>, allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> Result<Instance<Self::Fp>, InstanceError> { ) -> 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));
}
let instance_fp =
<Self::Fp as FunctionPointers>::InstanceFp::load(&self.static_fn(), instance);
Ok(Instance::from_raw(instance, instance_fp))
}
fn enumerate_instance_layer_properties(&self) -> VkResult<Vec<vk::LayerProperties>> { fn enumerate_instance_layer_properties(&self) -> VkResult<Vec<vk::LayerProperties>> {
unsafe { unsafe {
let mut num = 0; let mut num = 0;
@ -106,7 +102,6 @@ pub trait EntryV1_0 {
} }
} }
} }
fn enumerate_instance_extension_properties(&self) -> VkResult<Vec<vk::ExtensionProperties>> { fn enumerate_instance_extension_properties(&self) -> VkResult<Vec<vk::ExtensionProperties>> {
unsafe { unsafe {
let mut num = 0; let mut num = 0;
@ -138,33 +133,33 @@ pub trait EntryV1_0 {
} }
} }
impl EntryV1_0 for Entry<V1_0> { impl EntryV1_0 for Entry {
type Fp = V1_0; 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 { 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 { fn static_fn(&self) -> &vk::StaticFn {
&self.static_fn &self.static_fn
} }
} }
impl EntryV1_0 for Entry<V1_1> { impl 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<V1_1> {
fn fp_v1_1(&self) -> &vk::EntryFnV1_1 {
&self.entry_fn.entry_fn_1_1
}
}
impl<V: FunctionPointers> Entry<V> {
pub fn new() -> Result<Self, LoadingError> { pub fn new() -> Result<Self, LoadingError> {
let lib = VK_LIB let lib = VK_LIB
.as_ref() .as_ref()
@ -175,12 +170,18 @@ impl<V: FunctionPointers> Entry<V> {
.unwrap_or(ptr::null_mut()) .unwrap_or(ptr::null_mut())
}); });
let entry_fn = let entry_fn_1_0 = vk::EntryFnV1_0::load(|name| unsafe {
unsafe { V::EntryFp::load(&static_fn) }; 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 { Ok(Entry {
static_fn, static_fn,
entry_fn, entry_fn_1_0,
entry_fn_1_1,
}) })
} }
} }

View file

@ -4,58 +4,70 @@ use prelude::*;
use std::mem; use std::mem;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::ptr; use std::ptr;
use version::DeviceLoader;
use version::{FunctionPointers, V1_0, V1_1};
use vk; use vk;
use RawPtr; use RawPtr;
#[derive(Clone)] #[derive(Clone)]
pub struct Instance<V: FunctionPointers> { pub struct Instance {
handle: vk::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 { Instance {
handle: handle, handle: instance,
instance_fp: version, 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)] #[allow(non_camel_case_types)]
pub trait InstanceV1_1: InstanceV1_0 { pub trait InstanceV1_1: InstanceV1_0 {
fn fp_v1_1(&self) -> &vk::InstanceFnV1_1; fn fp_v1_1(&self) -> &vk::InstanceFnV1_1;
@ -232,7 +244,7 @@ pub trait InstanceV1_1: InstanceV1_0 {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub trait InstanceV1_0 { pub trait InstanceV1_0 {
type Fp: FunctionPointers; type Device;
fn handle(&self) -> vk::Instance; fn handle(&self) -> vk::Instance;
fn fp_v1_0(&self) -> &vk::InstanceFnV1_0; fn fp_v1_0(&self) -> &vk::InstanceFnV1_0;
unsafe fn create_device( unsafe fn create_device(
@ -240,23 +252,7 @@ pub trait InstanceV1_0 {
physical_device: vk::PhysicalDevice, physical_device: vk::PhysicalDevice,
create_info: &vk::DeviceCreateInfo, create_info: &vk::DeviceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>, allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> Result<Device<Self::Fp>, vk::Result> { ) -> 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);
}
let device_fn = <<Self as InstanceV1_0>::Fp as FunctionPointers>::DeviceFp::load(
self.fp_v1_0(),
device,
);
Ok(Device::from_raw(device, device_fn))
}
unsafe fn get_device_proc_addr( unsafe fn get_device_proc_addr(
&self, &self,

View file

@ -1,182 +1,3 @@
pub use device::{DeviceV1_0, DeviceV1_1}; pub use device::{DeviceV1_0, DeviceV1_1};
pub use entry::{EntryV1_0, EntryV1_1}; pub use entry::{EntryV1_0, EntryV1_1};
pub use instance::{InstanceV1_0, InstanceV1_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,
}
}
}

View file

@ -1,5 +1,4 @@
extern crate ash; extern crate ash;
#[macro_use]
extern crate examples; extern crate examples;
extern crate image; extern crate image;

View file

@ -1,5 +1,4 @@
extern crate ash; extern crate ash;
#[macro_use]
extern crate examples; extern crate examples;
use ash::util::*; use ash::util::*;

View file

@ -9,7 +9,7 @@ use ash::extensions::Win32Surface;
#[cfg(not(windows))] #[cfg(not(windows))]
use ash::extensions::XlibSurface; use ash::extensions::XlibSurface;
use ash::extensions::{DebugReport, Surface, Swapchain}; 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::vk;
use ash::Device; use ash::Device;
use ash::Entry; use ash::Entry;
@ -205,9 +205,9 @@ pub fn find_memorytype_index_f<F: Fn(vk::MemoryPropertyFlags, vk::MemoryProperty
} }
pub struct ExampleBase { pub struct ExampleBase {
pub entry: Entry<V1_0>, pub entry: Entry,
pub instance: Instance<V1_0>, pub instance: Instance,
pub device: Device<V1_0>, pub device: Device,
pub surface_loader: Surface, pub surface_loader: Surface,
pub swapchain_loader: Swapchain, pub swapchain_loader: Swapchain,
pub debug_report_loader: DebugReport, pub debug_report_loader: DebugReport,
@ -301,7 +301,7 @@ impl ExampleBase {
pp_enabled_extension_names: extension_names_raw.as_ptr(), pp_enabled_extension_names: extension_names_raw.as_ptr(),
enabled_extension_count: extension_names_raw.len() as u32, enabled_extension_count: extension_names_raw.len() as u32,
}; };
let instance: Instance<V1_0> = entry let instance: Instance = entry
.create_instance(&create_info, None) .create_instance(&create_info, None)
.expect("Instance creation error"); .expect("Instance creation error");
let debug_info = vk::DebugReportCallbackCreateInfoEXT { let debug_info = vk::DebugReportCallbackCreateInfoEXT {
@ -374,7 +374,7 @@ impl ExampleBase {
pp_enabled_extension_names: device_extension_names_raw.as_ptr(), pp_enabled_extension_names: device_extension_names_raw.as_ptr(),
p_enabled_features: &features, p_enabled_features: &features,
}; };
let device: Device<V1_0> = instance let device: Device = instance
.create_device(pdevice, &device_create_info, None) .create_device(pdevice, &device_create_info, None)
.unwrap(); .unwrap();
let present_queue = device.get_device_queue(queue_family_index as u32, 0); let present_queue = device.get_device_queue(queue_family_index as u32, 0);