ash/src/instance.rs

182 lines
6.4 KiB
Rust
Raw Normal View History

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;
2016-12-08 10:01:16 +11:00
use device::Device;
2016-12-28 19:07:27 +11:00
use ::RawPtr;
2016-12-08 10:01:16 +11:00
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-30 13:34:14 +11:00
pub trait VkVersion{
type InstanceFp;
type DeviceFp;
}
2016-12-30 13:34:14 +11:00
#[warn(non_camel_case_types)]
pub struct V1_0;
impl VkVersion for V1_0{
type InstanceFp = InstanceFpV1_0;
type DeviceFp = ();
}
#[warn(non_camel_case_types)]
pub struct InstanceFpV1_0{
pub instance_fn: vk::InstanceFn
}
2016-12-29 15:03:12 +11:00
#[derive(Clone)]
2016-12-30 13:34:14 +11:00
pub struct Instance<V: VkVersion> {
2016-12-24 15:57:44 +11:00
handle: vk::Instance,
2016-12-30 13:34:14 +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 13:34:14 +11:00
impl InstanceV1_0 for Instance<V1_0>{
fn handle(&self) -> vk::Instance{
self.handle
}
fn fp_v1_0(&self) -> &vk::InstanceFn{
&self.instance_fp.instance_fn
}
}
impl<V: VkVersion> 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 13:34:14 +11:00
instance_fp: version
2016-12-09 11:55:29 +11:00
}
}
2016-12-09 11:58:24 +11:00
2016-12-30 13:34:14 +11:00
}
#[warn(non_camel_case_types)]
pub trait InstanceV1_0 {
fn handle(&self) -> vk::Instance;
fn fp_v1_0(&self) -> &vk::InstanceFn;
unsafe fn create_device(&self,
physical_device: vk::PhysicalDevice,
create_info: &vk::DeviceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>)
-> Result<Device, DeviceError> {
2016-12-29 14:17:46 +11:00
let mut device: vk::Device = mem::uninitialized();
2016-12-30 13:34:14 +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
}
2016-12-29 14:17:46 +11:00
let device_fn = vk::DeviceFn::load(|name| {
2016-12-30 13:34:14 +11:00
mem::transmute(self.fp_v1_0().get_device_proc_addr(device, name.as_ptr()))
2016-12-29 14:17:46 +11:00
}).map_err(|err| DeviceError::LoadError(err))?;
Ok(Device::from_raw(device, device_fn))
2016-12-10 05:25:48 +11:00
}
2016-12-30 13:34:14 +11:00
fn get_device_proc_addr(&self,
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-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-12 11:08:12 +11:00
physical_device: vk::PhysicalDevice,
format: vk::Format)
-> vk::FormatProperties {
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
}
}
2016-12-30 13:34:14 +11:00
fn get_physical_device_memory_properties(&self,
2016-12-05 12:18:13 +11:00
physical_device: vk::PhysicalDevice)
-> vk::PhysicalDeviceMemoryProperties {
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-04 10:56:58 +11:00
physical_device: vk::PhysicalDevice)
-> Vec<vk::QueueFamilyProperties> {
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
//pub trait InstanceMajor1Minor1: InstanceMajor1Minor0 {}
//pub trait InstanceMajor1Minor2: InstanceMajor1Minor1 {}