First implementation of the new swapchain loader

This commit is contained in:
maik klein 2016-12-23 00:00:50 +01:00
parent 7f8acd4db3
commit d2df2e8ac3
3 changed files with 97 additions and 74 deletions

View file

@ -2,6 +2,7 @@
use prelude::*; use prelude::*;
use std::ptr; use std::ptr;
use std::mem; use std::mem;
use instance::Instance;
use vk; use vk;
unsafe impl Sync for Device {} unsafe impl Sync for Device {}
@ -13,10 +14,85 @@ pub struct Device {
} }
pub struct Swapchain { pub struct Swapchain {
handle: vk::Device,
swapchain_fn: vk::SwapchainFn,
}
impl Swapchain {
pub fn destroy_swapchain_khr(&self, swapchain: vk::SwapchainKHR) {
unsafe {
self.swapchain_fn.destroy_swapchain_khr(self.handle, swapchain, ptr::null());
}
}
pub fn acquire_next_image_khr(&self,
swapchain: vk::SwapchainKHR,
timeout: vk::uint64_t,
semaphore: vk::Semaphore,
fence: vk::Fence)
-> VkResult<vk::uint32_t> {
unsafe {
let mut index = mem::uninitialized();
let err_code = self.swapchain_fn
.acquire_next_image_khr(self.handle,
swapchain,
timeout,
semaphore,
fence,
&mut index);
match err_code {
vk::Result::Success => Ok(index),
_ => Err(err_code),
}
}
}
pub fn create_swapchain_khr(&self,
create_info: &vk::SwapchainCreateInfoKHR)
-> VkResult<vk::SwapchainKHR> {
unsafe {
let mut swapchain = mem::uninitialized();
let err_code = self.swapchain_fn
.create_swapchain_khr(self.handle, create_info, ptr::null(), &mut swapchain);
match err_code {
vk::Result::Success => Ok(swapchain),
_ => Err(err_code),
}
}
}
pub fn get_swapchain_images_khr(&self,
swapchain: vk::SwapchainKHR)
-> VkResult<Vec<vk::Image>> {
unsafe {
let mut count = 0;
self.swapchain_fn
.get_swapchain_images_khr(self.handle, swapchain, &mut count, ptr::null_mut());
let mut v = Vec::with_capacity(count as vk::size_t);
let err_code = self.swapchain_fn
.get_swapchain_images_khr(self.handle, swapchain, &mut count, v.as_mut_ptr());
v.set_len(count as vk::size_t);
match err_code {
vk::Result::Success => Ok(v),
_ => Err(err_code),
}
}
}
} }
impl Device { impl Device {
pub fn load_swapchain(&self, instance: &Instance) -> Swapchain {
let swapchain_fn = vk::SwapchainFn::load(|name| {
unsafe {
mem::transmute(instance.instance_fn
.get_device_proc_addr(self.handle, name.as_ptr()))
}
})
.unwrap();
Swapchain {
handle: self.handle,
swapchain_fn: swapchain_fn,
}
}
pub unsafe fn from_raw(handle: vk::Device, device_fn: vk::DeviceFn) -> Self { pub unsafe fn from_raw(handle: vk::Device, device_fn: vk::DeviceFn) -> Self {
Device { Device {
handle: handle, handle: handle,
@ -60,11 +136,6 @@ impl Device {
} }
} }
pub fn destroy_swapchain_khr(&self, swapchain: vk::SwapchainKHR) {
unsafe {
self.device_fn.destroy_swapchain_khr(self.handle, swapchain, ptr::null());
}
}
pub fn destroy_image_view(&self, image_view: vk::ImageView) { pub fn destroy_image_view(&self, image_view: vk::ImageView) {
unsafe { unsafe {
@ -404,27 +475,6 @@ impl Device {
} }
} }
pub fn acquire_next_image_khr(&self,
swapchain: vk::SwapchainKHR,
timeout: vk::uint64_t,
semaphore: vk::Semaphore,
fence: vk::Fence)
-> VkResult<vk::uint32_t> {
unsafe {
let mut index = mem::uninitialized();
let err_code = self.device_fn
.acquire_next_image_khr(self.handle,
swapchain,
timeout,
semaphore,
fence,
&mut index);
match err_code {
vk::Result::Success => Ok(index),
_ => Err(err_code),
}
}
}
pub fn create_semaphore(&self, pub fn create_semaphore(&self,
create_info: &vk::SemaphoreCreateInfo) create_info: &vk::SemaphoreCreateInfo)
@ -679,24 +729,6 @@ impl Device {
} }
} }
pub fn get_swapchain_images_khr(&self,
swapchain: vk::SwapchainKHR)
-> VkResult<Vec<vk::Image>> {
unsafe {
let mut count = 0;
self.device_fn
.get_swapchain_images_khr(self.handle, swapchain, &mut count, ptr::null_mut());
let mut v = Vec::with_capacity(count as vk::size_t);
let err_code = self.device_fn
.get_swapchain_images_khr(self.handle, swapchain, &mut count, v.as_mut_ptr());
v.set_len(count as vk::size_t);
match err_code {
vk::Result::Success => Ok(v),
_ => Err(err_code),
}
}
}
pub fn allocate_command_buffers(&self, pub fn allocate_command_buffers(&self,
create_info: &vk::CommandBufferAllocateInfo) create_info: &vk::CommandBufferAllocateInfo)
@ -727,19 +759,6 @@ impl Device {
} }
} }
pub fn create_swapchain_khr(&self,
create_info: &vk::SwapchainCreateInfoKHR)
-> VkResult<vk::SwapchainKHR> {
unsafe {
let mut swapchain = mem::uninitialized();
let err_code = self.device_fn
.create_swapchain_khr(self.handle, create_info, ptr::null(), &mut swapchain);
match err_code {
vk::Result::Success => Ok(swapchain),
_ => Err(err_code),
}
}
}
pub fn create_image(&self, create_info: &vk::ImageCreateInfo) -> VkResult<vk::Image> { pub fn create_image(&self, create_info: &vk::ImageCreateInfo) -> VkResult<vk::Image> {
unsafe { unsafe {

View file

@ -14,8 +14,8 @@ pub enum DeviceError {
#[derive(Debug)] #[derive(Debug)]
pub struct Instance { pub struct Instance {
handle: vk::Instance, pub handle: vk::Instance,
instance_fn: vk::InstanceFn, pub instance_fn: vk::InstanceFn,
} }
impl Instance { impl Instance {

View file

@ -3939,13 +3939,6 @@ vk_functions!{
"vkCreateSharedSwapchainsKHR", create_shared_swapchains_khr(
device: Device,
swapchain_count: uint32_t,
p_create_infos: *const SwapchainCreateInfoKHR,
p_allocator: *const AllocationCallbacks,
p_swapchains: *mut SwapchainKHR,
) -> Result;
"vkGetPhysicalDeviceDisplayPropertiesKHR", get_physical_device_display_properties_khr( "vkGetPhysicalDeviceDisplayPropertiesKHR", get_physical_device_display_properties_khr(
physical_device: PhysicalDevice, physical_device: PhysicalDevice,
@ -4849,6 +4842,22 @@ vk_functions!{
command_buffer_count: uint32_t, command_buffer_count: uint32_t,
p_command_buffers: *const CommandBuffer, p_command_buffers: *const CommandBuffer,
) -> (); ) -> ();
"vkQueuePresentKHR", queue_present_khr(
queue: Queue,
p_present_info: *const PresentInfoKHR,
) -> Result;
}
vk_functions!{
SwapchainFn,
"vkCreateSharedSwapchainsKHR", create_shared_swapchains_khr(
device: Device,
swapchain_count: uint32_t,
p_create_infos: *const SwapchainCreateInfoKHR,
p_allocator: *const AllocationCallbacks,
p_swapchains: *mut SwapchainKHR,
) -> Result;
"vkCreateSwapchainKHR", create_swapchain_khr( "vkCreateSwapchainKHR", create_swapchain_khr(
device: Device, device: Device,
p_create_info: *const SwapchainCreateInfoKHR, p_create_info: *const SwapchainCreateInfoKHR,
@ -4877,10 +4886,5 @@ vk_functions!{
fence: Fence, fence: Fence,
p_image_index: *mut uint32_t, p_image_index: *mut uint32_t,
) -> Result; ) -> Result;
"vkQueuePresentKHR", queue_present_khr(
queue: Queue,
p_present_info: *const PresentInfoKHR,
) -> Result;
} }
} }