ash/src/instance.rs

145 lines
5.6 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 {
LoadError(String),
VkError(vk::Result),
}
2016-12-04 10:56:58 +11:00
2016-12-08 10:01:16 +11:00
#[derive(Debug)]
2016-12-23 09:31:37 +11:00
pub struct Instance {
2016-12-24 15:57:44 +11:00
handle: vk::Instance,
instance_fn: vk::InstanceFn,
2016-12-04 10:56:58 +11:00
}
2016-12-10 05:25:48 +11:00
2016-12-23 09:31:37 +11:00
impl Instance {
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
pub fn from_raw(handle: vk::Instance, instance_fn: vk::InstanceFn) -> Self {
2016-12-09 11:55:29 +11:00
Instance {
handle: handle,
instance_fn: instance_fn,
}
}
2016-12-09 11:58:24 +11:00
2016-12-10 05:25:48 +11:00
pub fn create_device(&self,
physical_device: vk::PhysicalDevice,
2016-12-28 19:07:27 +11:00
create_info: &vk::DeviceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>)
2016-12-10 05:25:48 +11:00
-> Result<Device, DeviceError> {
unsafe {
let mut device: vk::Device = mem::uninitialized();
2016-12-10 05:25:48 +11:00
let err_code = self.instance_fn
2016-12-28 19:07:27 +11:00
.create_device(physical_device, create_info, allocation_callbacks.as_raw_ptr(), &mut device);
2016-12-10 05:25:48 +11:00
if err_code != vk::Result::Success {
return Err(DeviceError::VkError(err_code));
}
2016-12-23 09:31:37 +11:00
let device_fn = vk::DeviceFn::load(|name| {
2016-12-10 05:25:48 +11:00
mem::transmute(self.instance_fn.get_device_proc_addr(device, name.as_ptr()))
}).map_err(|err| DeviceError::LoadError(err))?;
Ok(Device::from_raw(device, device_fn))
}
}
pub fn get_device_proc_addr(&self,
device: vk::Device,
p_name: *const vk::c_char)
-> vk::PFN_vkVoidFunction {
unsafe { self.instance_fn.get_device_proc_addr(device, p_name) }
}
2016-12-24 10:22:37 +11:00
pub unsafe fn destroy_instance(&self) {
2016-12-26 10:24:06 +11:00
self.instance_fn.destroy_instance(self.handle, ptr::null());
2016-12-04 10:56:58 +11:00
}
2016-12-09 11:58:24 +11:00
2016-12-12 11:08:12 +11:00
pub fn get_physical_device_format_properties(&self,
physical_device: vk::PhysicalDevice,
format: vk::Format)
-> vk::FormatProperties {
unsafe {
let mut format_prop = mem::uninitialized();
self.instance_fn
.get_physical_device_format_properties(physical_device, format, &mut format_prop);
format_prop
}
}
2016-12-05 12:18:13 +11:00
pub fn get_physical_device_memory_properties(&self,
physical_device: vk::PhysicalDevice)
-> vk::PhysicalDeviceMemoryProperties {
unsafe {
let mut memory_prop = mem::uninitialized();
self.instance_fn
.get_physical_device_memory_properties(physical_device, &mut memory_prop);
memory_prop
}
}
2016-12-04 10:56:58 +11:00
pub fn get_physical_device_queue_family_properties(&self,
physical_device: vk::PhysicalDevice)
-> Vec<vk::QueueFamilyProperties> {
unsafe {
let mut queue_count = 0;
self.instance_fn
.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-23 09:31:37 +11:00
self.instance_fn
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
}
}
pub fn enumerate_physical_devices(&self) -> VkResult<Vec<vk::PhysicalDevice>> {
2016-11-29 07:16:35 +11:00
unsafe {
let mut num = mem::uninitialized();
self.instance_fn
.enumerate_physical_devices(self.handle, &mut num, ptr::null_mut());
let mut physical_devices = Vec::<vk::PhysicalDevice>::with_capacity(num as usize);
let err_code = self.instance_fn
.enumerate_physical_devices(self.handle, &mut num, physical_devices.as_mut_ptr());
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-11-29 07:16:35 +11:00
pub fn enumerate_device_extension_properties
(&self,
device: vk::PhysicalDevice)
-> Result<Vec<vk::ExtensionProperties>, vk::Result> {
2016-08-14 09:13:39 +10:00
unsafe {
let mut num = 0;
2016-11-29 07:16:35 +11:00
self.instance_fn
.enumerate_device_extension_properties(device,
ptr::null(),
&mut num,
ptr::null_mut());
let mut data = Vec::with_capacity(num as usize);
let err_code = self.instance_fn
.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
}
}
}