Added error handling for optional extension loading

This commit is contained in:
maik klein 2016-12-23 21:43:41 +01:00
parent e706df027f
commit f39baa7e34
6 changed files with 39 additions and 43 deletions

View file

@ -42,7 +42,7 @@ fn create_surface(instance: &Instance,
window: x11_window as vk::Window, window: x11_window as vk::Window,
dpy: x11_display as *mut vk::Display, dpy: x11_display as *mut vk::Display,
}; };
let xlib_surface_loader = XlibSurface::new(&entry, &instance); let xlib_surface_loader = XlibSurface::new(&entry, &instance).expect("Unable to load xlib surface");
xlib_surface_loader.create_xlib_surface_khr(&x11_create_info) xlib_surface_loader.create_xlib_surface_khr(&x11_create_info)
} }
@ -154,12 +154,13 @@ fn main() {
pfn_callback: vulkan_debug_callback, pfn_callback: vulkan_debug_callback,
p_user_data: ptr::null_mut(), p_user_data: ptr::null_mut(),
}; };
let debug_report_loader = DebugReport::new(&entry, &instance); let debug_report_loader = DebugReport::new(&entry, &instance).expect("Unable to load debug report");
let debug_call_back = debug_report_loader.create_debug_report_callback_ext(&debug_info) let debug_call_back = debug_report_loader.create_debug_report_callback_ext(&debug_info)
.unwrap(); .unwrap();
let surface = create_surface(&instance, &entry, &window).unwrap(); let surface = create_surface(&instance, &entry, &window).unwrap();
let pdevices = instance.enumerate_physical_devices().expect("Physical device error"); let pdevices = instance.enumerate_physical_devices().expect("Physical device error");
let surface_loader = Surface::new(&entry, &instance); let surface_loader = Surface::new(&entry, &instance)
.expect("Unable to load the Surface extension");
let (pdevice, queue_family_index) = pdevices.iter() let (pdevice, queue_family_index) = pdevices.iter()
.map(|pdevice| { .map(|pdevice| {
instance.get_physical_device_queue_family_properties(*pdevice) instance.get_physical_device_queue_family_properties(*pdevice)
@ -257,7 +258,7 @@ fn main() {
.cloned() .cloned()
.find(|&mode| mode == vk::PresentModeKHR::Mailbox) .find(|&mode| mode == vk::PresentModeKHR::Mailbox)
.unwrap_or(vk::PresentModeKHR::Fifo); .unwrap_or(vk::PresentModeKHR::Fifo);
let swapchain_loader = Swapchain::new(&instance, &device); let swapchain_loader = Swapchain::new(&instance, &device).expect("Unable to load swapchain");
let swapchain_create_info = vk::SwapchainCreateInfoKHR { let swapchain_create_info = vk::SwapchainCreateInfoKHR {
s_type: vk::StructureType::SwapchainCreateInfoKhr, s_type: vk::StructureType::SwapchainCreateInfoKhr,
p_next: ptr::null(), p_next: ptr::null(),

View file

@ -12,18 +12,17 @@ pub struct DebugReport {
} }
impl DebugReport { impl DebugReport {
pub fn new(entry: &Entry, instance: &Instance) -> DebugReport { pub fn new(entry: &Entry, instance: &Instance) -> Result<DebugReport, String> {
let debug_report_fn = vk::DebugReportFn::load(|name| { let debug_report_fn = vk::DebugReportFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.static_fn mem::transmute(entry.static_fn
.get_instance_proc_addr(instance.handle, name.as_ptr())) .get_instance_proc_addr(instance.handle, name.as_ptr()))
} }
}) })?;
.unwrap(); Ok(DebugReport {
DebugReport {
handle: instance.handle, handle: instance.handle,
debug_report_fn: debug_report_fn, debug_report_fn: debug_report_fn,
} })
} }
pub fn destroy_debug_report_callback_ext(&self, debug: vk::DebugReportCallbackEXT) { pub fn destroy_debug_report_callback_ext(&self, debug: vk::DebugReportCallbackEXT) {

View file

@ -11,18 +11,17 @@ pub struct Surface {
} }
impl Surface { impl Surface {
pub fn new(entry: &Entry, instance: &Instance) -> Surface { pub fn new(entry: &Entry, instance: &Instance) -> Result<Surface, String> {
let surface_fn = vk::SurfaceFn::load(|name| { let surface_fn = vk::SurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.static_fn mem::transmute(entry.static_fn
.get_instance_proc_addr(instance.handle, name.as_ptr())) .get_instance_proc_addr(instance.handle, name.as_ptr()))
} }
}) })?;
.unwrap(); Ok(Surface {
Surface {
handle: instance.handle, handle: instance.handle,
surface_fn: surface_fn, surface_fn: surface_fn,
} })
} }
pub fn get_physical_device_surface_support_khr(&self, pub fn get_physical_device_surface_support_khr(&self,
@ -111,4 +110,3 @@ impl Surface {
} }
} }
} }

View file

@ -4,24 +4,24 @@ use std::mem;
use instance::Instance; use instance::Instance;
use device::Device; use device::Device;
use vk; use vk;
pub struct Swapchain { pub struct Swapchain {
handle: vk::Device, handle: vk::Device,
swapchain_fn: vk::SwapchainFn, swapchain_fn: vk::SwapchainFn,
} }
impl Swapchain { impl Swapchain {
pub fn new(instance: &Instance, device: &Device) -> Swapchain { pub fn new(instance: &Instance, device: &Device) -> Result<Swapchain, String> {
let swapchain_fn = vk::SwapchainFn::load(|name| { let swapchain_fn = vk::SwapchainFn::load(|name| {
unsafe { unsafe {
mem::transmute(instance.instance_fn mem::transmute(instance.instance_fn
.get_device_proc_addr(device.handle, name.as_ptr())) .get_device_proc_addr(device.handle, name.as_ptr()))
} }
}) })?;
.unwrap(); Ok(Swapchain {
Swapchain {
handle: device.handle, handle: device.handle,
swapchain_fn: swapchain_fn, swapchain_fn: swapchain_fn,
} })
} }
pub fn destroy_swapchain_khr(&self, swapchain: vk::SwapchainKHR) { pub fn destroy_swapchain_khr(&self, swapchain: vk::SwapchainKHR) {

View file

@ -11,21 +11,20 @@ pub struct XlibSurface {
} }
impl XlibSurface { impl XlibSurface {
pub fn new(entry: &Entry, instance: &Instance) -> XlibSurface { pub fn new(entry: &Entry, instance: &Instance) -> Result<XlibSurface, String> {
let surface_fn = vk::XlibSurfaceFn::load(|name| { let surface_fn = vk::XlibSurfaceFn::load(|name| {
unsafe { unsafe {
mem::transmute(entry.static_fn mem::transmute(entry.static_fn
.get_instance_proc_addr(instance.handle, name.as_ptr())) .get_instance_proc_addr(instance.handle, name.as_ptr()))
} }
}) })?;
.unwrap(); Ok(XlibSurface {
XlibSurface {
handle: instance.handle, handle: instance.handle,
xlib_surface_fn: surface_fn, xlib_surface_fn: surface_fn,
} })
} }
pub fn create_xlib_surface_khr(&self, pub fn create_xlib_surface_khr(&self,
create_info: &vk::XlibSurfaceCreateInfoKHR) create_info: &vk::XlibSurfaceCreateInfoKHR)
-> VkResult<vk::SurfaceKHR> { -> VkResult<vk::SurfaceKHR> {
unsafe { unsafe {

View file

@ -19,7 +19,6 @@ pub struct Instance {
pub instance_fn: vk::InstanceFn, pub instance_fn: vk::InstanceFn,
} }
impl Instance { impl Instance {
pub unsafe fn from_raw(handle: vk::Instance, instance_fn: vk::InstanceFn) -> Self { pub unsafe fn from_raw(handle: vk::Instance, instance_fn: vk::InstanceFn) -> Self {
Instance { Instance {