diff --git a/ash/src/device.rs b/ash/src/device.rs index 842e685..67bd43a 100644 --- a/ash/src/device.rs +++ b/ash/src/device.rs @@ -126,7 +126,7 @@ impl<'r> Device<'r> { pub fn reset_fences(&self, fences: &[vk::Fence]) -> VkResult<()> { unsafe { let err_code = self.device_fn - .reset_fences(self.handle, fences.len() as u32, fences.as_ptr()); + .reset_fences(self.handle, fences.len() as vk::uint32_t, fences.as_ptr()); match err_code { vk::Result::Success => Ok(()), _ => Err(err_code), @@ -155,7 +155,10 @@ impl<'r> Device<'r> { pub fn cmd_set_scissor(&self, command_buffer: vk::CommandBuffer, scissors: &[vk::Rect2D]) { unsafe { self.device_fn - .cmd_set_scissor(command_buffer, 0, scissors.len() as u32, scissors.as_ptr()); + .cmd_set_scissor(command_buffer, + 0, + scissors.len() as vk::uint32_t, + scissors.as_ptr()); } } @@ -166,7 +169,7 @@ impl<'r> Device<'r> { unsafe { self.device_fn.cmd_bind_vertex_buffers(command_buffer, 0, - buffers.len() as u32, + buffers.len() as vk::uint32_t, buffers.as_ptr(), offsets); } @@ -179,10 +182,10 @@ impl<'r> Device<'r> { } pub fn cmd_draw(&self, command_buffer: vk::CommandBuffer, - vertex_count: u32, - instance_count: u32, - first_vertex: u32, - first_instance: u32) { + vertex_count: vk::uint32_t, + instance_count: vk::uint32_t, + first_vertex: vk::uint32_t, + first_instance: vk::uint32_t) { unsafe { self.device_fn.cmd_draw(command_buffer, vertex_count, @@ -196,16 +199,16 @@ impl<'r> Device<'r> { unsafe { self.device_fn.cmd_set_viewport(command_buffer, 0, - viewports.len() as u32, + viewports.len() as vk::uint32_t, viewports.as_ptr()); } } pub fn acquire_next_image_khr(&self, swapchain: vk::SwapchainKHR, - timeout: u64, + timeout: vk::uint64_t, semaphore: vk::Semaphore, fence: vk::Fence) - -> VkResult { + -> VkResult { unsafe { let mut index = mem::uninitialized(); let err_code = self.device_fn @@ -244,7 +247,7 @@ impl<'r> Device<'r> { let err_code = self.device_fn .create_graphics_pipelines(self.handle, pipeline_cache, - create_infos.len() as u32, + create_infos.len() as vk::uint32_t, create_infos.as_ptr(), ptr::null(), pipelines.as_mut_ptr()); @@ -297,7 +300,8 @@ impl<'r> Device<'r> { let x: *mut T = data as *mut T; match err_code { vk::Result::Success => { - Ok(::std::slice::from_raw_parts_mut(x, size as usize / mem::size_of::())) + Ok(::std::slice::from_raw_parts_mut(x, + size as vk::size_t / mem::size_of::())) } _ => Err(err_code), } @@ -324,7 +328,10 @@ impl<'r> Device<'r> { } } - pub fn get_device_queue(&self, queue_family_index: u32, queue_index: u32) -> vk::Queue { + pub fn get_device_queue(&self, + queue_family_index: vk::uint32_t, + queue_index: vk::uint32_t) + -> vk::Queue { unsafe { let mut queue = mem::uninitialized(); self.device_fn @@ -346,11 +353,11 @@ impl<'r> Device<'r> { src_stage_mask, dst_stage_mask, dependency_flags, - memory_barriers.len() as u32, + memory_barriers.len() as vk::uint32_t, memory_barriers.as_ptr(), - buffer_memory_barriers.len() as u32, + buffer_memory_barriers.len() as vk::uint32_t, buffer_memory_barriers.as_ptr(), - image_memory_barriers.len() as u32, + image_memory_barriers.len() as vk::uint32_t, image_memory_barriers.as_ptr()); } } @@ -397,14 +404,14 @@ impl<'r> Device<'r> { pub fn wait_for_fences(&self, fences: &[vk::Fence], wait_all: bool, - timeout: u64) + timeout: vk::uint64_t) -> VkResult<()> { unsafe { let err_code = self.device_fn .wait_for_fences(self.handle, - fences.len() as u32, + fences.len() as vk::uint32_t, fences.as_ptr(), - wait_all as u32, + wait_all as vk::uint32_t, timeout); match err_code { vk::Result::Success => Ok(()), @@ -443,7 +450,10 @@ impl<'r> Device<'r> { -> VkResult<()> { unsafe { let err_code = self.device_fn - .queue_submit(queue, submits.len() as u32, submits.as_ptr(), fence); + .queue_submit(queue, + submits.len() as vk::uint32_t, + submits.as_ptr(), + fence); match err_code { vk::Result::Success => Ok(()), _ => Err(err_code), @@ -472,10 +482,10 @@ impl<'r> Device<'r> { self.device_fn .get_swapchain_images_khr(self.handle, swapchain, &mut count, ptr::null_mut()); - let mut v = Vec::with_capacity(count as usize); + 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 usize); + v.set_len(count as vk::size_t); match err_code { vk::Result::Success => Ok(v), _ => Err(err_code), @@ -483,45 +493,40 @@ impl<'r> Device<'r> { } } - pub fn allocate_command_buffers> - (&self, - i: I) - -> VkResult> { - let create_info = i.into(); + pub fn allocate_command_buffers(&self, + create_info: &vk::CommandBufferAllocateInfo) + -> VkResult> { unsafe { - let mut buffers = Vec::with_capacity(create_info.command_buffer_count as usize); + let mut buffers = Vec::with_capacity(create_info.command_buffer_count as vk::size_t); let err_code = self.device_fn - .allocate_command_buffers(self.handle, &create_info, buffers.as_mut_ptr()); - buffers.set_len(create_info.command_buffer_count as usize); + .allocate_command_buffers(self.handle, create_info, buffers.as_mut_ptr()); + buffers.set_len(create_info.command_buffer_count as vk::size_t); match err_code { vk::Result::Success => Ok(buffers), _ => Err(err_code), } } } - pub fn create_command_pool>(&self, - i: I) - -> VkResult { - let create_info = i.into(); + pub fn create_command_pool(&self, + create_info: &vk::CommandPoolCreateInfo) + -> VkResult { unsafe { let mut pool = mem::uninitialized(); let err_code = self.device_fn - .create_command_pool(self.handle, &create_info, ptr::null(), &mut pool); + .create_command_pool(self.handle, create_info, ptr::null(), &mut pool); match err_code { vk::Result::Success => Ok(pool), _ => Err(err_code), } } } - pub fn create_swapchain_khr> - (&self, - i: I) - -> VkResult { - let create_info = i.into(); + pub fn create_swapchain_khr(&self, + create_info: &vk::SwapchainCreateInfoKHR) + -> VkResult { unsafe { let mut swapchain = mem::uninitialized(); let err_code = self.device_fn - .create_swapchain_khr(self.handle, &create_info, ptr::null(), &mut swapchain); + .create_swapchain_khr(self.handle, create_info, ptr::null(), &mut swapchain); match err_code { vk::Result::Success => Ok(swapchain), _ => Err(err_code), diff --git a/ash/src/entry.rs b/ash/src/entry.rs new file mode 100644 index 0000000..80b88b2 --- /dev/null +++ b/ash/src/entry.rs @@ -0,0 +1,116 @@ +use std::mem; +use std::ptr; +use vk_loader2 as vk; +use instance::Instance; +use shared_library::dynamic_library::DynamicLibrary; +use std::path::Path; +type VkResult = Result; +#[cfg(windows)] +fn get_path() -> &'static Path { + Path::new("vulkan-1.dll") +} + +#[cfg(all(unix, not(target_os = "android")))] +fn get_path() -> &'static Path { + Path::new("libvulkan.so.1") +} + +#[cfg(target_os = "android")] +fn get_path() -> &'static Path { + Path::new("libvulkan.so") +} + + +pub struct Entry { + lib: DynamicLibrary, + static_fn: vk::Static, + entry_fn: vk::EntryFn, +} + +#[derive(Debug)] +pub enum LoadingError { + LibraryLoadFailure(String), + StaticLoadError(String), + EntryLoadError(String), +} + +impl Entry { + pub fn load_vulkan_path(path: &Path) -> Result { + let lib = try!(DynamicLibrary::open(Some(path)) + .map_err(|err| LoadingError::LibraryLoadFailure(err))); + let static_fn = try!(vk::Static::load(|name| unsafe { + let name = name.to_str().unwrap(); + let f = match lib.symbol(name) { + Ok(s) => s, + Err(_) => ptr::null(), + }; + f + }) + .map_err(|err| LoadingError::StaticLoadError(err))); + let entry_fn = try!(vk::EntryFn::load(|name| unsafe { + mem::transmute(static_fn.get_instance_proc_addr(ptr::null_mut(), name.as_ptr())) + }) + .map_err(|err| LoadingError::EntryLoadError(err))); + Ok(Entry { + lib: lib, + static_fn: static_fn, + entry_fn: entry_fn, + }) + } + pub fn load_vulkan() -> Result { + Entry::load_vulkan_path(get_path()) + } + + + pub fn create_instance(&self, + create_info: &vk::InstanceCreateInfo) + -> Result { + unsafe { + let mut instance: vk::Instance = mem::uninitialized(); + let err_code = self.entry_fn.create_instance(create_info, ptr::null(), &mut instance); + if err_code != vk::Result::Success { + return Err(err_code); + } + let instance_fn = vk::InstanceFn::load(|name| unsafe { + mem::transmute(self.static_fn.get_instance_proc_addr(instance, name.as_ptr())) + }) + .unwrap(); + Ok(Instance::from_raw(instance, instance_fn)) + } + } + + pub fn enumerate_instance_layer_properties(&self) + -> Result, vk::Result> { + unsafe { + let mut num = 0; + self.entry_fn.enumerate_instance_layer_properties(&mut num, ptr::null_mut()); + + let mut v = Vec::with_capacity(num as usize); + let err_code = self.entry_fn + .enumerate_instance_layer_properties(&mut num, v.as_mut_ptr()); + v.set_len(num as usize); + match err_code { + vk::Result::Success => Ok(v), + _ => Err(err_code), + } + } + } + + pub fn enumerate_instance_extension_properties + (&self) + -> Result, vk::Result> { + unsafe { + let mut num = 0; + self.entry_fn + .enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut()); + let mut data = Vec::with_capacity(num as usize); + let err_code = self.entry_fn + .enumerate_instance_extension_properties(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), + } + } + } +} diff --git a/ash/src/instance.rs b/ash/src/instance.rs index 619879a..65c672b 100644 --- a/ash/src/instance.rs +++ b/ash/src/instance.rs @@ -9,120 +9,6 @@ use shared_library::dynamic_library::DynamicLibrary; type VkResult = Result; -#[cfg(windows)] -fn get_path() -> &'static Path { - Path::new("vulkan-1.dll") -} - -#[cfg(all(unix, not(target_os = "android")))] -fn get_path() -> &'static Path { - Path::new("libvulkan.so.1") -} - -#[cfg(target_os = "android")] -fn get_path() -> &'static Path { - Path::new("libvulkan.so") -} - - -pub struct Entry { - lib: DynamicLibrary, - static_fn: vk::Static, - entry_fn: vk::EntryFn, -} - -#[derive(Debug)] -pub enum LoadingError { - LibraryLoadFailure(String), - StaticLoadError(String), - EntryLoadError(String), -} - -impl Entry { - pub fn load_vulkan_path(path: &Path) -> Result { - let lib = try!(DynamicLibrary::open(Some(path)) - .map_err(|err| LoadingError::LibraryLoadFailure(err))); - let static_fn = try!(vk::Static::load(|name| unsafe { - let name = name.to_str().unwrap(); - let f = match lib.symbol(name) { - Ok(s) => s, - Err(_) => ptr::null(), - }; - f - }) - .map_err(|err| LoadingError::StaticLoadError(err))); - let entry_fn = try!(vk::EntryFn::load(|name| unsafe { - mem::transmute(static_fn.get_instance_proc_addr(ptr::null_mut(), name.as_ptr())) - }) - .map_err(|err| LoadingError::EntryLoadError(err))); - Ok(Entry { - lib: lib, - static_fn: static_fn, - entry_fn: entry_fn, - }) - } - pub fn load_vulkan() -> Result { - Entry::load_vulkan_path(get_path()) - } - - - pub fn create_instance>(&self, - i: I) - -> Result { - let create_info = i.into(); - unsafe { - let mut instance: vk::Instance = mem::uninitialized(); - let err_code = self.entry_fn.create_instance(&create_info, ptr::null(), &mut instance); - if err_code != vk::Result::Success { - return Err(err_code); - } - let instance_fn = vk::InstanceFn::load(|name| unsafe { - mem::transmute(self.static_fn.get_instance_proc_addr(instance, name.as_ptr())) - }) - .unwrap(); - Ok(Instance { - handle: instance, - instance_fn: instance_fn, - _lifetime: ::std::marker::PhantomData, - }) - } - } - - pub fn enumerate_instance_layer_properties(&self) - -> Result, vk::Result> { - unsafe { - let mut num = 0; - self.entry_fn.enumerate_instance_layer_properties(&mut num, ptr::null_mut()); - - let mut v = Vec::with_capacity(num as usize); - let err_code = self.entry_fn - .enumerate_instance_layer_properties(&mut num, v.as_mut_ptr()); - v.set_len(num as usize); - match err_code { - vk::Result::Success => Ok(v), - _ => Err(err_code), - } - } - } - - pub fn enumerate_instance_extension_properties - (&self) - -> Result, vk::Result> { - unsafe { - let mut num = 0; - self.entry_fn - .enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut()); - let mut data = Vec::with_capacity(num as usize); - let err_code = self.entry_fn - .enumerate_instance_extension_properties(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), - } - } - } -} #[derive(Debug)] pub struct Instance<'r> { @@ -131,6 +17,13 @@ pub struct Instance<'r> { _lifetime: ::std::marker::PhantomData<&'r ()>, } impl<'r> Instance<'r> { + pub unsafe fn from_raw(handle: vk::Instance, instance_fn: vk::InstanceFn) -> Self { + Instance { + handle: handle, + instance_fn: instance_fn, + _lifetime: ::std::marker::PhantomData, + } + } pub fn destroy_instance(&self) { unsafe { self.instance_fn.destroy_instance(self.handle, ptr::null()); @@ -238,15 +131,13 @@ impl<'r> Instance<'r> { self.instance_fn.destroy_surface_khr(self.handle, surface, ptr::null()); } } - pub fn create_xlib_surface_khr> - (&self, - i: I) - -> VkResult { - let create_info = i.into(); + pub fn create_xlib_surface_khr(&self, + create_info: &vk::XlibSurfaceCreateInfoKHR) + -> VkResult { unsafe { let mut surface = mem::uninitialized(); let err_code = self.instance_fn - .create_xlib_surface_khr(self.handle, &create_info, ptr::null(), &mut surface); + .create_xlib_surface_khr(self.handle, create_info, ptr::null(), &mut surface); match err_code { vk::Result::Success => Ok(surface), _ => Err(err_code), @@ -256,7 +147,7 @@ impl<'r> Instance<'r> { } pub fn get_physical_device_surface_support_khr(&self, physical_device: vk::PhysicalDevice, - queue_index: u32, + queue_index: vk::uint32_t, surface: vk::SurfaceKHR) -> bool { unsafe { @@ -289,15 +180,14 @@ impl<'r> Instance<'r> { } } - pub fn create_device>(&self, - physical_device: vk::PhysicalDevice, - i: I) - -> VkResult { - let create_info = i.into(); + pub fn create_device(&self, + physical_device: vk::PhysicalDevice, + create_info: &vk::DeviceCreateInfo) + -> VkResult { unsafe { let mut device = mem::uninitialized(); let err_code = self.instance_fn - .create_device(physical_device, &create_info, ptr::null(), &mut device); + .create_device(physical_device, create_info, ptr::null(), &mut device); if err_code != vk::Result::Success { return Err(err_code); } diff --git a/ash/src/lib.rs b/ash/src/lib.rs index 3e20a15..6b6b10b 100644 --- a/ash/src/lib.rs +++ b/ash/src/lib.rs @@ -11,6 +11,7 @@ extern crate glfw; pub mod instance; //pub mod feature; pub mod device; +pub mod entry; //pub mod commandpool; //pub mod fence; //pub mod buffer; diff --git a/examples/src/main.rs b/examples/src/main.rs index 09d94ec..3849336 100644 --- a/examples/src/main.rs +++ b/examples/src/main.rs @@ -9,7 +9,8 @@ extern crate time; use std::default::Default; use glfw::*; -use ash::instance::{Entry, Instance}; +use ash::entry::Entry; +use ash::instance::Instance; use ash::device::Device; use std::ptr; use std::ffi::{CStr, CString}; @@ -17,7 +18,6 @@ use std::mem; use std::path::Path; use std::fs::File; use std::io::Read; -use std::os::raw::c_void; unsafe extern "system" fn vulkan_debug_callback(flags: vk::DebugReportFlagsEXT, obj_type: vk::DebugReportObjectTypeEXT, @@ -28,8 +28,7 @@ unsafe extern "system" fn vulkan_debug_callback(flags: vk::DebugReportFlagsEXT, p_message: *const i8, data: *mut ()) -> u32 { - let s = CStr::from_ptr(p_message); - println!("{:?}", s); + println!("{:?}", CStr::from_ptr(p_message)); 1 } fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { @@ -107,7 +106,7 @@ fn main() { pp_enabled_extension_names: extension_names_raw.as_ptr(), enabled_extension_count: extension_names_raw.len() as u32, }; - let instance: Instance = entry.create_instance(create_info).expect("Instance creation error"); + let instance: Instance = entry.create_instance(&create_info).expect("Instance creation error"); let debug_info = vk::DebugReportCallbackCreateInfoEXT { s_type: vk::StructureType::DebugReportCallbackCreateInfoExt, p_next: ptr::null(), @@ -126,7 +125,7 @@ fn main() { window: x11_window as vk::Window, dpy: x11_display as *mut vk::Display, }; - let surface = instance.create_xlib_surface_khr(x11_create_info).unwrap(); + let surface = instance.create_xlib_surface_khr(&x11_create_info).unwrap(); let pdevices = instance.enumerate_physical_devices().expect("Physical device error"); let (pdevice, queue_family_index) = pdevices.iter() .map(|pdevice| { @@ -176,7 +175,7 @@ fn main() { pp_enabled_extension_names: device_extension_names_raw.as_ptr(), p_enabled_features: &features, }; - let device: Device = instance.create_device(pdevice, device_create_info) + let device: Device = instance.create_device(pdevice, &device_create_info) .unwrap(); let present_queue = device.get_device_queue(queue_family_index as u32, 0); @@ -245,15 +244,14 @@ fn main() { p_queue_family_indices: ptr::null(), queue_family_index_count: 0, }; - let swapchain = device.create_swapchain_khr(swapchain_create_info).unwrap(); + let swapchain = device.create_swapchain_khr(&swapchain_create_info).unwrap(); let pool_create_info = vk::CommandPoolCreateInfo { s_type: vk::StructureType::CommandPoolCreateInfo, p_next: ptr::null(), flags: vk::COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, queue_family_index: queue_family_index, }; - let pool = device.create_command_pool(pool_create_info).unwrap(); - + let pool = device.create_command_pool(&pool_create_info).unwrap(); let command_buffer_allocate_info = vk::CommandBufferAllocateInfo { s_type: vk::StructureType::CommandBufferAllocateInfo, p_next: ptr::null(), @@ -261,9 +259,7 @@ fn main() { command_pool: pool, level: vk::CommandBufferLevel::Primary, }; - // let draw_command_buffers = device.allocate_command_buffers(command_buffer_allocate_info).unwrap(); - - let command_buffers = device.allocate_command_buffers(command_buffer_allocate_info).unwrap(); + let command_buffers = device.allocate_command_buffers(&command_buffer_allocate_info).unwrap(); let setup_command_buffer = command_buffers[0]; let draw_command_buffer = command_buffers[1];