2016-08-14 09:13:39 +10:00
|
|
|
#![allow(dead_code)]
|
2016-12-10 05:25:48 +11:00
|
|
|
use prelude::*;
|
2016-08-14 09:13:39 +10:00
|
|
|
use std::ptr;
|
|
|
|
use std::mem;
|
2016-12-10 06:15:59 +11:00
|
|
|
use vk;
|
2017-01-01 18:09:51 +11:00
|
|
|
use device::Device;
|
2016-12-28 19:07:27 +11:00
|
|
|
use ::RawPtr;
|
2017-01-01 18:09:51 +11:00
|
|
|
use version::{FunctionPointers, V1_0, InstanceFpV1_0, DeviceFpV1_0};
|
|
|
|
use version::{InstanceLoader, DeviceLoader};
|
|
|
|
|
2016-12-10 05:25:48 +11:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum DeviceError {
|
2016-12-29 15:03:12 +11:00
|
|
|
LoadError(Vec<&'static str>),
|
2016-12-10 05:25:48 +11:00
|
|
|
VkError(vk::Result),
|
|
|
|
}
|
2016-12-04 10:56:58 +11:00
|
|
|
|
2016-12-29 15:03:12 +11:00
|
|
|
#[derive(Clone)]
|
2017-01-01 18:09:51 +11:00
|
|
|
pub struct Instance<V: FunctionPointers> {
|
2016-12-24 15:57:44 +11:00
|
|
|
handle: vk::Instance,
|
2016-12-30 15:42:34 +11:00
|
|
|
instance_fp: V::InstanceFp,
|
2016-12-04 10:56:58 +11:00
|
|
|
}
|
2016-12-10 05:25:48 +11:00
|
|
|
|
2016-12-30 15:42:34 +11:00
|
|
|
impl InstanceV1_0 for Instance<V1_0> {
|
2017-01-05 01:21:01 +11:00
|
|
|
type Fp = V1_0;
|
2016-12-30 15:42:34 +11:00
|
|
|
fn handle(&self) -> vk::Instance {
|
2016-12-30 13:34:14 +11:00
|
|
|
self.handle
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:43:37 +11:00
|
|
|
fn fp_v1_0(&self) -> &vk::InstanceFnV1_0 {
|
2016-12-30 13:34:14 +11:00
|
|
|
&self.instance_fp.instance_fn
|
|
|
|
}
|
|
|
|
}
|
2017-01-01 18:09:51 +11:00
|
|
|
impl<V: FunctionPointers> Instance<V> {
|
2016-12-26 10:24:06 +11:00
|
|
|
pub fn handle(&self) -> vk::Instance {
|
2016-12-24 15:57:44 +11:00
|
|
|
self.handle
|
|
|
|
}
|
2016-12-26 10:24:06 +11:00
|
|
|
|
2016-12-30 13:34:14 +11:00
|
|
|
pub fn from_raw(handle: vk::Instance, version: V::InstanceFp) -> Self {
|
2016-12-09 11:55:29 +11:00
|
|
|
Instance {
|
|
|
|
handle: handle,
|
2016-12-30 15:42:34 +11:00
|
|
|
instance_fp: version,
|
2016-12-09 11:55:29 +11:00
|
|
|
}
|
|
|
|
}
|
2016-12-30 13:34:14 +11:00
|
|
|
}
|
|
|
|
|
2017-01-05 01:21:01 +11:00
|
|
|
#[warn(non_camel_case_types)]
|
|
|
|
pub trait InstanceV1_0 {
|
|
|
|
type Fp: FunctionPointers;
|
|
|
|
fn handle(&self) -> vk::Instance;
|
|
|
|
fn fp_v1_0(&self) -> &vk::InstanceFnV1_0;
|
|
|
|
unsafe fn create_device(&self,
|
|
|
|
physical_device: vk::PhysicalDevice,
|
|
|
|
create_info: &vk::DeviceCreateInfo,
|
|
|
|
allocation_callbacks: Option<&vk::AllocationCallbacks>)
|
|
|
|
-> Result<Device<Self::Fp>, DeviceError> {
|
2016-12-29 14:17:46 +11:00
|
|
|
let mut device: vk::Device = mem::uninitialized();
|
2017-01-05 01:21:01 +11:00
|
|
|
let err_code = self.fp_v1_0()
|
2016-12-29 14:17:46 +11:00
|
|
|
.create_device(physical_device,
|
|
|
|
create_info,
|
|
|
|
allocation_callbacks.as_raw_ptr(),
|
|
|
|
&mut device);
|
|
|
|
if err_code != vk::Result::Success {
|
|
|
|
return Err(DeviceError::VkError(err_code));
|
2016-12-10 05:25:48 +11:00
|
|
|
}
|
2017-01-05 01:21:01 +11:00
|
|
|
let device_fn =
|
|
|
|
<<Self as InstanceV1_0>::Fp as FunctionPointers>::DeviceFp::load(self.fp_v1_0(), device).map_err(|err| DeviceError::LoadError(err))?;
|
2017-01-01 18:09:51 +11:00
|
|
|
Ok(Device::from_raw(device, device_fn))
|
2016-12-10 05:25:48 +11:00
|
|
|
}
|
2016-12-30 15:42:34 +11:00
|
|
|
|
2016-12-30 13:34:14 +11:00
|
|
|
fn get_device_proc_addr(&self,
|
2016-12-30 15:42:34 +11:00
|
|
|
device: vk::Device,
|
|
|
|
p_name: *const vk::c_char)
|
|
|
|
-> vk::PFN_vkVoidFunction {
|
2016-12-30 13:34:14 +11:00
|
|
|
unsafe { self.fp_v1_0().get_device_proc_addr(device, p_name) }
|
2016-12-24 15:07:21 +11:00
|
|
|
}
|
|
|
|
|
2016-12-30 13:34:14 +11:00
|
|
|
unsafe fn destroy_instance(&self, allocation_callbacks: Option<&vk::AllocationCallbacks>) {
|
|
|
|
self.fp_v1_0().destroy_instance(self.handle(), allocation_callbacks.as_raw_ptr());
|
2016-12-04 10:56:58 +11:00
|
|
|
}
|
2016-12-09 11:58:24 +11:00
|
|
|
|
2016-12-30 13:34:14 +11:00
|
|
|
fn get_physical_device_format_properties(&self,
|
2016-12-30 15:42:34 +11:00
|
|
|
physical_device: vk::PhysicalDevice,
|
|
|
|
format: vk::Format)
|
|
|
|
-> vk::FormatProperties {
|
2016-12-12 11:08:12 +11:00
|
|
|
unsafe {
|
|
|
|
let mut format_prop = mem::uninitialized();
|
2016-12-30 13:34:14 +11:00
|
|
|
self.fp_v1_0()
|
2016-12-12 11:08:12 +11:00
|
|
|
.get_physical_device_format_properties(physical_device, format, &mut format_prop);
|
|
|
|
format_prop
|
|
|
|
}
|
|
|
|
}
|
2017-01-05 01:21:01 +11:00
|
|
|
|
2016-12-30 13:34:14 +11:00
|
|
|
fn get_physical_device_memory_properties(&self,
|
2016-12-30 15:42:34 +11:00
|
|
|
physical_device: vk::PhysicalDevice)
|
|
|
|
-> vk::PhysicalDeviceMemoryProperties {
|
2016-12-05 12:18:13 +11:00
|
|
|
unsafe {
|
|
|
|
let mut memory_prop = mem::uninitialized();
|
2016-12-30 13:34:14 +11:00
|
|
|
self.fp_v1_0()
|
2016-12-05 12:18:13 +11:00
|
|
|
.get_physical_device_memory_properties(physical_device, &mut memory_prop);
|
|
|
|
memory_prop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 13:34:14 +11:00
|
|
|
fn get_physical_device_queue_family_properties(&self,
|
2016-12-30 15:42:34 +11:00
|
|
|
physical_device: vk::PhysicalDevice)
|
|
|
|
-> Vec<vk::QueueFamilyProperties> {
|
2016-12-04 10:56:58 +11:00
|
|
|
unsafe {
|
|
|
|
let mut queue_count = 0;
|
2016-12-30 13:34:14 +11:00
|
|
|
self.fp_v1_0()
|
2016-12-04 10:56:58 +11:00
|
|
|
.get_physical_device_queue_family_properties(physical_device,
|
|
|
|
&mut queue_count,
|
|
|
|
ptr::null_mut());
|
|
|
|
let mut queue_families_vec = Vec::with_capacity(queue_count as usize);
|
2016-12-30 13:34:14 +11:00
|
|
|
self.fp_v1_0()
|
2016-12-04 10:56:58 +11:00
|
|
|
.get_physical_device_queue_family_properties(physical_device,
|
|
|
|
&mut queue_count,
|
|
|
|
queue_families_vec.as_mut_ptr());
|
|
|
|
queue_families_vec.set_len(queue_count as usize);
|
|
|
|
queue_families_vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 13:34:14 +11:00
|
|
|
fn enumerate_physical_devices(&self) -> VkResult<Vec<vk::PhysicalDevice>> {
|
2016-11-29 07:16:35 +11:00
|
|
|
unsafe {
|
|
|
|
let mut num = mem::uninitialized();
|
2016-12-30 13:34:14 +11:00
|
|
|
self.fp_v1_0()
|
|
|
|
.enumerate_physical_devices(self.handle(), &mut num, ptr::null_mut());
|
2016-11-29 07:16:35 +11:00
|
|
|
let mut physical_devices = Vec::<vk::PhysicalDevice>::with_capacity(num as usize);
|
2016-12-30 13:34:14 +11:00
|
|
|
let err_code = self.fp_v1_0()
|
|
|
|
.enumerate_physical_devices(self.handle(), &mut num, physical_devices.as_mut_ptr());
|
2016-11-29 07:16:35 +11:00
|
|
|
physical_devices.set_len(num as usize);
|
|
|
|
match err_code {
|
|
|
|
vk::Result::Success => Ok(physical_devices),
|
|
|
|
_ => Err(err_code),
|
|
|
|
}
|
2016-08-14 09:13:39 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 13:34:14 +11:00
|
|
|
fn enumerate_device_extension_properties
|
2016-11-29 07:16:35 +11:00
|
|
|
(&self,
|
|
|
|
device: vk::PhysicalDevice)
|
|
|
|
-> Result<Vec<vk::ExtensionProperties>, vk::Result> {
|
2016-08-14 09:13:39 +10:00
|
|
|
unsafe {
|
|
|
|
let mut num = 0;
|
2016-12-30 13:34:14 +11:00
|
|
|
self.fp_v1_0()
|
2016-11-29 07:16:35 +11:00
|
|
|
.enumerate_device_extension_properties(device,
|
|
|
|
ptr::null(),
|
|
|
|
&mut num,
|
|
|
|
ptr::null_mut());
|
|
|
|
let mut data = Vec::with_capacity(num as usize);
|
2016-12-30 13:34:14 +11:00
|
|
|
let err_code = self.fp_v1_0()
|
2016-11-29 07:16:35 +11:00
|
|
|
.enumerate_device_extension_properties(device,
|
|
|
|
ptr::null(),
|
|
|
|
&mut num,
|
|
|
|
data.as_mut_ptr());
|
|
|
|
data.set_len(num as usize);
|
|
|
|
match err_code {
|
|
|
|
vk::Result::Success => Ok(data),
|
|
|
|
_ => Err(err_code),
|
|
|
|
}
|
2016-08-14 09:13:39 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-30 13:34:14 +11:00
|
|
|
|
2016-12-30 15:42:34 +11:00
|
|
|
// pub trait InstanceMajor1Minor1: InstanceMajor1Minor0 {}
|
|
|
|
// pub trait InstanceMajor1Minor2: InstanceMajor1Minor1 {}
|