From 45936ae6bbff56460f6401bc3e451fb2c08eddbb Mon Sep 17 00:00:00 2001 From: maik klein Date: Fri, 30 Dec 2016 05:42:34 +0100 Subject: [PATCH] Swtich device to the new extension version format --- examples/src/lib.rs | 9 +- src/device.rs | 729 ++++++++++++++++---------------- src/extensions/swapchain.rs | 2 +- src/extensions/win32_surface.rs | 1 + src/instance.rs | 72 ++-- 5 files changed, 419 insertions(+), 394 deletions(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 2853b06..5b92542 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -16,7 +16,8 @@ use ash::device::Device; use std::ptr; use std::ffi::{CStr, CString}; use std::ops::Drop; -use ash::instance::{V1_0, InstanceV1_0}; +pub use ash::instance::{V1_0, InstanceV1_0}; +pub use ash::device::{DeviceV1_0}; // Simple offset_of macro akin to C++ offsetof #[macro_export] @@ -32,7 +33,7 @@ macro_rules! offset_of{ } } -pub fn record_submit_commandbuffer(device: &Device, +pub fn record_submit_commandbuffer, vk::CommandBuffer)>(device: &Device, command_buffer: vk::CommandBuffer, submit_queue: vk::Queue, wait_mask: &[vk::PipelineStageFlags], @@ -188,7 +189,7 @@ fn resize_callback(width: u32, height: u32) { pub struct ExampleBase { pub entry: Entry, pub instance: Instance, - pub device: Device, + pub device: Device, pub surface_loader: Surface, pub swapchain_loader: Swapchain, pub debug_report_loader: DebugReport, @@ -334,7 +335,7 @@ impl ExampleBase { pp_enabled_extension_names: device_extension_names_raw.as_ptr(), p_enabled_features: &features, }; - let device: Device = instance.create_device(pdevice, &device_create_info, None) + let device: Device = instance.create_device(pdevice, &device_create_info, None) .unwrap(); let present_queue = device.get_device_queue(queue_family_index as u32, 0); diff --git a/src/device.rs b/src/device.rs index bddd982..42d16d6 100644 --- a/src/device.rs +++ b/src/device.rs @@ -4,153 +4,149 @@ use std::mem; use vk; use ::RawPtr; -unsafe impl Sync for Device {} -unsafe impl Send for Device {} +use instance::{VkVersion, V1_0}; -#[derive(Clone)] -pub struct Device { - handle: vk::Device, - device_fn: vk::DeviceFn, +// unsafe impl Sync for Device {} +// unsafe impl Send for Device {} + +pub struct DeviceFpV1_0 { + pub device_fn: vk::DeviceFn, } -impl Device { - pub fn handle(&self) -> vk::Device { - self.handle +pub trait DeviceV1_0 { + fn handle(&self) -> vk::Device; + fn fp_v1_0(&self) -> &vk::DeviceFn; + unsafe fn destroy_device(&self, allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0().destroy_device(self.handle(), allocation_callbacks.as_raw_ptr()); } - pub unsafe fn from_raw(handle: vk::Device, device_fn: vk::DeviceFn) -> Self { - Device { - handle: handle, - device_fn: device_fn, - } - } - - pub unsafe fn destroy_device(&self, allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_device(self.handle, allocation_callbacks.as_raw_ptr()); - } - - pub unsafe fn destroy_sampler(&self, - sampler: vk::Sampler, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_sampler(self.handle, sampler, allocation_callbacks.as_raw_ptr()); - } - - pub unsafe fn free_memory(&self, - memory: vk::DeviceMemory, + unsafe fn destroy_sampler(&self, + sampler: vk::Sampler, allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.free_memory(self.handle, memory, allocation_callbacks.as_raw_ptr()); + self.fp_v1_0().destroy_sampler(self.handle(), sampler, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_fence(&self, - fence: vk::Fence, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_fence(self.handle, fence, allocation_callbacks.as_raw_ptr()); + unsafe fn free_memory(&self, + memory: vk::DeviceMemory, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0().free_memory(self.handle(), memory, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_image(&self, - image: vk::Image, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_image(self.handle, image, allocation_callbacks.as_raw_ptr()); + unsafe fn destroy_fence(&self, + fence: vk::Fence, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0().destroy_fence(self.handle(), fence, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_command_pool(&self, - pool: vk::CommandPool, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_command_pool(self.handle, pool, allocation_callbacks.as_raw_ptr()); + unsafe fn destroy_image(&self, + image: vk::Image, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0().destroy_image(self.handle(), image, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_image_view(&self, - image_view: vk::ImageView, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn - .destroy_image_view(self.handle, image_view, allocation_callbacks.as_raw_ptr()); + unsafe fn destroy_command_pool(&self, + pool: vk::CommandPool, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0().destroy_command_pool(self.handle(), pool, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_render_pass(&self, - renderpass: vk::RenderPass, + unsafe fn destroy_image_view(&self, + image_view: vk::ImageView, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0() + .destroy_image_view(self.handle(), image_view, allocation_callbacks.as_raw_ptr()); + } + + unsafe fn destroy_render_pass(&self, + renderpass: vk::RenderPass, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0() + .destroy_render_pass(self.handle(), renderpass, allocation_callbacks.as_raw_ptr()); + } + + unsafe fn destroy_framebuffer(&self, + framebuffer: vk::Framebuffer, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0() + .destroy_framebuffer(self.handle(), + framebuffer, + allocation_callbacks.as_raw_ptr()); + } + + unsafe fn destroy_pipeline_layout(&self, + pipeline_layout: vk::PipelineLayout, allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn - .destroy_render_pass(self.handle, renderpass, allocation_callbacks.as_raw_ptr()); - } - - pub unsafe fn destroy_framebuffer(&self, - framebuffer: vk::Framebuffer, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn - .destroy_framebuffer(self.handle, framebuffer, allocation_callbacks.as_raw_ptr()); - } - - pub unsafe fn destroy_pipeline_layout(&self, - pipeline_layout: vk::PipelineLayout, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_pipeline_layout(self.handle, + self.fp_v1_0().destroy_pipeline_layout(self.handle(), pipeline_layout, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_buffer(&self, - buffer: vk::Buffer, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_buffer(self.handle, buffer, allocation_callbacks.as_raw_ptr()); + unsafe fn destroy_buffer(&self, + buffer: vk::Buffer, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0().destroy_buffer(self.handle(), buffer, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_shader_module(&self, - shader: vk::ShaderModule, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn - .destroy_shader_module(self.handle, shader, allocation_callbacks.as_raw_ptr()); - } - - pub unsafe fn destroy_pipeline(&self, - pipeline: vk::Pipeline, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_pipeline(self.handle, pipeline, allocation_callbacks.as_raw_ptr()); - } - - pub unsafe fn destroy_semaphore(&self, - semaphore: vk::Semaphore, + unsafe fn destroy_shader_module(&self, + shader: vk::ShaderModule, allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn.destroy_semaphore(self.handle, semaphore, allocation_callbacks.as_raw_ptr()); + self.fp_v1_0() + .destroy_shader_module(self.handle(), shader, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_descriptor_pool(&self, - pool: vk::DescriptorPool, - allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn - .destroy_descriptor_pool(self.handle, pool, allocation_callbacks.as_raw_ptr()); + unsafe fn destroy_pipeline(&self, + pipeline: vk::Pipeline, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0().destroy_pipeline(self.handle(), pipeline, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_descriptor_set_layout(&self, layout: vk::DescriptorSetLayout, allocation_callbacks: Option<&vk::AllocationCallbacks>) { - self.device_fn - .destroy_descriptor_set_layout(self.handle, layout, allocation_callbacks.as_raw_ptr()); + unsafe fn destroy_semaphore(&self, + semaphore: vk::Semaphore, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0() + .destroy_semaphore(self.handle(), semaphore, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn free_descriptor_sets(&self, - pool: vk::DescriptorPool, - descriptor_sets: &[vk::DescriptorSet]) { - self.device_fn.free_descriptor_sets(self.handle, + unsafe fn destroy_descriptor_pool(&self, + pool: vk::DescriptorPool, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0() + .destroy_descriptor_pool(self.handle(), pool, allocation_callbacks.as_raw_ptr()); + } + + unsafe fn destroy_descriptor_set_layout(&self, layout: vk::DescriptorSetLayout, allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.fp_v1_0() + .destroy_descriptor_set_layout(self.handle(), + layout, + allocation_callbacks.as_raw_ptr()); + } + + unsafe fn free_descriptor_sets(&self, + pool: vk::DescriptorPool, + descriptor_sets: &[vk::DescriptorSet]) { + self.fp_v1_0().free_descriptor_sets(self.handle(), pool, descriptor_sets.len() as u32, descriptor_sets.as_ptr()); } - pub unsafe fn update_descriptor_sets(&self, - descriptor_writes: &[vk::WriteDescriptorSet], - descriptor_copies: &[vk::CopyDescriptorSet]) { - self.device_fn.update_descriptor_sets(self.handle, + unsafe fn update_descriptor_sets(&self, + descriptor_writes: &[vk::WriteDescriptorSet], + descriptor_copies: &[vk::CopyDescriptorSet]) { + self.fp_v1_0().update_descriptor_sets(self.handle(), descriptor_writes.len() as u32, descriptor_writes.as_ptr(), descriptor_copies.len() as u32, descriptor_copies.as_ptr()); } - pub unsafe fn create_sampler(&self, - create_info: &vk::SamplerCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_sampler(&self, + create_info: &vk::SamplerCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut sampler = mem::uninitialized(); - let err_code = self.device_fn - .create_sampler(self.handle, + let err_code = self.fp_v1_0() + .create_sampler(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut sampler); @@ -160,26 +156,26 @@ impl Device { } } - pub unsafe fn cmd_copy_buffer(&self, - command_buffer: vk::CommandBuffer, - src_buffer: vk::Buffer, - dst_buffer: vk::Buffer, - regions: &[vk::BufferCopy]) { + unsafe fn cmd_copy_buffer(&self, + command_buffer: vk::CommandBuffer, + src_buffer: vk::Buffer, + dst_buffer: vk::Buffer, + regions: &[vk::BufferCopy]) { - self.device_fn.cmd_copy_buffer(command_buffer, + self.fp_v1_0().cmd_copy_buffer(command_buffer, src_buffer, dst_buffer, regions.len() as u32, regions.as_ptr()); } - pub unsafe fn cmd_copy_buffer_to_image(&self, - command_buffer: vk::CommandBuffer, - src_buffer: vk::Buffer, - dst_image: vk::Image, - dst_image_layout: vk::ImageLayout, - regions: &[vk::BufferImageCopy]) { - self.device_fn.cmd_copy_buffer_to_image(command_buffer, + unsafe fn cmd_copy_buffer_to_image(&self, + command_buffer: vk::CommandBuffer, + src_buffer: vk::Buffer, + dst_image: vk::Image, + dst_image_layout: vk::ImageLayout, + regions: &[vk::BufferImageCopy]) { + self.fp_v1_0().cmd_copy_buffer_to_image(command_buffer, src_buffer, dst_image, dst_image_layout, @@ -187,14 +183,14 @@ impl Device { regions.as_ptr()); } - pub unsafe fn cmd_copy_image(&self, - command_buffer: vk::CommandBuffer, - src_image: vk::Image, - src_image_layout: vk::ImageLayout, - dst_image: vk::Image, - dst_image_layout: vk::ImageLayout, - regions: &[vk::ImageCopy]) { - self.device_fn.cmd_copy_image(command_buffer, + unsafe fn cmd_copy_image(&self, + command_buffer: vk::CommandBuffer, + src_image: vk::Image, + src_image_layout: vk::ImageLayout, + dst_image: vk::Image, + dst_image_layout: vk::ImageLayout, + regions: &[vk::ImageCopy]) { + self.fp_v1_0().cmd_copy_image(command_buffer, src_image, src_image_layout, dst_image, @@ -203,12 +199,12 @@ impl Device { regions.as_ptr()); } - pub unsafe fn allocate_descriptor_sets(&self, - create_info: &vk::DescriptorSetAllocateInfo) - -> VkResult> { + unsafe fn allocate_descriptor_sets(&self, + create_info: &vk::DescriptorSetAllocateInfo) + -> VkResult> { let mut desc_set = Vec::with_capacity(create_info.descriptor_set_count as usize); - let err_code = self.device_fn - .allocate_descriptor_sets(self.handle, create_info, desc_set.as_mut_ptr()); + let err_code = self.fp_v1_0() + .allocate_descriptor_sets(self.handle(), create_info, desc_set.as_mut_ptr()); desc_set.set_len(create_info.descriptor_set_count as usize); match err_code { @@ -217,13 +213,13 @@ impl Device { } } - pub unsafe fn create_descriptor_set_layout(&self, - create_info: &vk::DescriptorSetLayoutCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_descriptor_set_layout(&self, + create_info: &vk::DescriptorSetLayoutCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut layout = mem::uninitialized(); - let err_code = self.device_fn - .create_descriptor_set_layout(self.handle, + let err_code = self.fp_v1_0() + .create_descriptor_set_layout(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut layout); @@ -233,9 +229,9 @@ impl Device { } } - pub fn device_wait_idle(&self) -> VkResult<()> { + fn device_wait_idle(&self) -> VkResult<()> { unsafe { - let err_code = self.device_fn.device_wait_idle(self.handle); + let err_code = self.fp_v1_0().device_wait_idle(self.handle()); match err_code { vk::Result::Success => Ok(()), _ => Err(err_code), @@ -243,13 +239,13 @@ impl Device { } } - pub unsafe fn create_descriptor_pool(&self, - create_info: &vk::DescriptorPoolCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_descriptor_pool(&self, + create_info: &vk::DescriptorPoolCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut pool = mem::uninitialized(); - let err_code = self.device_fn - .create_descriptor_pool(self.handle, + let err_code = self.fp_v1_0() + .create_descriptor_pool(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut pool); @@ -259,11 +255,11 @@ impl Device { } } - pub unsafe fn reset_command_buffer(&self, - command_buffer: vk::CommandBuffer, - flags: vk::CommandBufferResetFlags) - -> VkResult<()> { - let err_code = self.device_fn + unsafe fn reset_command_buffer(&self, + command_buffer: vk::CommandBuffer, + flags: vk::CommandBufferResetFlags) + -> VkResult<()> { + let err_code = self.fp_v1_0() .reset_command_buffer(command_buffer, flags); match err_code { vk::Result::Success => Ok(()), @@ -271,32 +267,32 @@ impl Device { } } - pub unsafe fn reset_fences(&self, fences: &[vk::Fence]) -> VkResult<()> { - let err_code = self.device_fn - .reset_fences(self.handle, fences.len() as vk::uint32_t, fences.as_ptr()); + unsafe fn reset_fences(&self, fences: &[vk::Fence]) -> VkResult<()> { + let err_code = self.fp_v1_0() + .reset_fences(self.handle(), fences.len() as vk::uint32_t, fences.as_ptr()); match err_code { vk::Result::Success => Ok(()), _ => Err(err_code), } } - pub unsafe fn cmd_bind_index_buffer(&self, - command_buffer: vk::CommandBuffer, - buffer: vk::Buffer, - offset: vk::DeviceSize, - index_type: vk::IndexType) { - self.device_fn.cmd_bind_index_buffer(command_buffer, buffer, offset, index_type); + unsafe fn cmd_bind_index_buffer(&self, + command_buffer: vk::CommandBuffer, + buffer: vk::Buffer, + offset: vk::DeviceSize, + index_type: vk::IndexType) { + self.fp_v1_0().cmd_bind_index_buffer(command_buffer, buffer, offset, index_type); } - pub unsafe fn cmd_draw_indexed(&self, - command_buffer: vk::CommandBuffer, - index_count: vk::uint32_t, - instance_count: vk::uint32_t, - first_index: vk::uint32_t, - vertex_offset: vk::int32_t, - first_instance: vk::uint32_t) { + unsafe fn cmd_draw_indexed(&self, + command_buffer: vk::CommandBuffer, + index_count: vk::uint32_t, + instance_count: vk::uint32_t, + first_index: vk::uint32_t, + vertex_offset: vk::int32_t, + first_instance: vk::uint32_t) { - self.device_fn.cmd_draw_indexed(command_buffer, + self.fp_v1_0().cmd_draw_indexed(command_buffer, index_count, instance_count, first_index, @@ -304,14 +300,14 @@ impl Device { first_instance); } - pub unsafe fn cmd_bind_descriptor_sets(&self, - command_buffer: vk::CommandBuffer, - pipeline_bind_point: vk::PipelineBindPoint, - layout: vk::PipelineLayout, - first_set: vk::uint32_t, - descriptor_sets: &[vk::DescriptorSet], - dynamic_offsets: &[vk::uint32_t]) { - self.device_fn.cmd_bind_descriptor_sets(command_buffer, + unsafe fn cmd_bind_descriptor_sets(&self, + command_buffer: vk::CommandBuffer, + pipeline_bind_point: vk::PipelineBindPoint, + layout: vk::PipelineLayout, + first_set: vk::uint32_t, + descriptor_sets: &[vk::DescriptorSet], + dynamic_offsets: &[vk::uint32_t]) { + self.fp_v1_0().cmd_bind_descriptor_sets(command_buffer, pipeline_bind_point, layout, first_set, @@ -321,74 +317,72 @@ impl Device { dynamic_offsets.as_ptr()) } - pub unsafe fn cmd_begin_render_pass(&self, - command_buffer: vk::CommandBuffer, - create_info: &vk::RenderPassBeginInfo, - contents: vk::SubpassContents) { - self.device_fn.cmd_begin_render_pass(command_buffer, create_info, contents); - } - - pub unsafe fn cmd_bind_pipeline(&self, + unsafe fn cmd_begin_render_pass(&self, command_buffer: vk::CommandBuffer, - pipeline_bind_point: vk::PipelineBindPoint, - pipeline: vk::Pipeline) { - self.device_fn.cmd_bind_pipeline(command_buffer, pipeline_bind_point, pipeline); + create_info: &vk::RenderPassBeginInfo, + contents: vk::SubpassContents) { + self.fp_v1_0().cmd_begin_render_pass(command_buffer, create_info, contents); } - pub unsafe fn cmd_set_scissor(&self, - command_buffer: vk::CommandBuffer, - scissors: &[vk::Rect2D]) { - self.device_fn + unsafe fn cmd_bind_pipeline(&self, + command_buffer: vk::CommandBuffer, + pipeline_bind_point: vk::PipelineBindPoint, + pipeline: vk::Pipeline) { + self.fp_v1_0().cmd_bind_pipeline(command_buffer, pipeline_bind_point, pipeline); + } + + unsafe fn cmd_set_scissor(&self, command_buffer: vk::CommandBuffer, scissors: &[vk::Rect2D]) { + self.fp_v1_0() .cmd_set_scissor(command_buffer, 0, scissors.len() as vk::uint32_t, scissors.as_ptr()); } - pub unsafe fn cmd_bind_vertex_buffers(&self, - command_buffer: vk::CommandBuffer, - buffers: &[vk::Buffer], - offsets: &vk::DeviceSize) { - self.device_fn.cmd_bind_vertex_buffers(command_buffer, + unsafe fn cmd_bind_vertex_buffers(&self, + command_buffer: vk::CommandBuffer, + buffers: &[vk::Buffer], + offsets: &vk::DeviceSize) { + self.fp_v1_0().cmd_bind_vertex_buffers(command_buffer, 0, buffers.len() as vk::uint32_t, buffers.as_ptr(), offsets); } - pub unsafe fn cmd_end_render_pass(&self, command_buffer: vk::CommandBuffer) { - self.device_fn.cmd_end_render_pass(command_buffer); + unsafe fn cmd_end_render_pass(&self, command_buffer: vk::CommandBuffer) { + self.fp_v1_0().cmd_end_render_pass(command_buffer); } - pub unsafe fn cmd_draw(&self, - command_buffer: vk::CommandBuffer, - vertex_count: vk::uint32_t, - instance_count: vk::uint32_t, - first_vertex: vk::uint32_t, - first_instance: vk::uint32_t) { - self.device_fn.cmd_draw(command_buffer, + unsafe fn cmd_draw(&self, + command_buffer: vk::CommandBuffer, + vertex_count: vk::uint32_t, + instance_count: vk::uint32_t, + first_vertex: vk::uint32_t, + first_instance: vk::uint32_t) { + self.fp_v1_0().cmd_draw(command_buffer, vertex_count, instance_count, first_vertex, first_instance); } - pub unsafe fn cmd_set_viewport(&self, - command_buffer: vk::CommandBuffer, - viewports: &[vk::Viewport]) { - self.device_fn.cmd_set_viewport(command_buffer, + unsafe fn cmd_set_viewport(&self, + command_buffer: vk::CommandBuffer, + viewports: &[vk::Viewport]) { + self.fp_v1_0().cmd_set_viewport(command_buffer, 0, viewports.len() as vk::uint32_t, viewports.as_ptr()); } - pub unsafe fn create_semaphore(&self, - create_info: &vk::SemaphoreCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_semaphore(&self, + create_info: &vk::SemaphoreCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut semaphore = mem::uninitialized(); - let err_code = self.device_fn - .create_semaphore(self.handle, + let err_code = self.fp_v1_0() + .create_semaphore(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut semaphore); @@ -398,14 +392,14 @@ impl Device { } } - pub unsafe fn create_graphics_pipelines(&self, - pipeline_cache: vk::PipelineCache, - create_infos: &[vk::GraphicsPipelineCreateInfo], - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult> { + unsafe fn create_graphics_pipelines(&self, + pipeline_cache: vk::PipelineCache, + create_infos: &[vk::GraphicsPipelineCreateInfo], + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult> { let mut pipelines = Vec::with_capacity(create_infos.len()); - let err_code = self.device_fn - .create_graphics_pipelines(self.handle, + let err_code = self.fp_v1_0() + .create_graphics_pipelines(self.handle(), pipeline_cache, create_infos.len() as vk::uint32_t, create_infos.as_ptr(), @@ -418,13 +412,13 @@ impl Device { } } - pub unsafe fn create_buffer(&self, - create_info: &vk::BufferCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_buffer(&self, + create_info: &vk::BufferCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut buffer = mem::uninitialized(); - let err_code = self.device_fn - .create_buffer(self.handle, + let err_code = self.fp_v1_0() + .create_buffer(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut buffer); @@ -434,14 +428,14 @@ impl Device { } } - pub fn create_pipeline_layout(&self, - create_info: &vk::PipelineLayoutCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + fn create_pipeline_layout(&self, + create_info: &vk::PipelineLayoutCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { unsafe { let mut pipeline_layout = mem::uninitialized(); - let err_code = self.device_fn - .create_pipeline_layout(self.handle, + let err_code = self.fp_v1_0() + .create_pipeline_layout(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut pipeline_layout); @@ -452,16 +446,16 @@ impl Device { } } - pub unsafe fn map_memory(&self, - memory: vk::DeviceMemory, - offset: vk::DeviceSize, - size: vk::DeviceSize, - flags: vk::MemoryMapFlags) - -> VkResult<&mut [T]> { + unsafe fn map_memory(&self, + memory: vk::DeviceMemory, + offset: vk::DeviceSize, + size: vk::DeviceSize, + flags: vk::MemoryMapFlags) + -> VkResult<&mut [T]> { let mut data: *mut () = mem::uninitialized(); - let err_code = self.device_fn - .map_memory(self.handle, memory, offset, size, flags, &mut data); + let err_code = self.fp_v1_0() + .map_memory(self.handle(), memory, offset, size, flags, &mut data); let x: *mut T = data as *mut T; match err_code { vk::Result::Success => { @@ -471,18 +465,18 @@ impl Device { } } - pub unsafe fn unmap_memory(&self, memory: vk::DeviceMemory) { - self.device_fn.unmap_memory(self.handle, memory); + unsafe fn unmap_memory(&self, memory: vk::DeviceMemory) { + self.fp_v1_0().unmap_memory(self.handle(), memory); } - pub fn create_framebuffer(&self, - create_info: &vk::FramebufferCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + fn create_framebuffer(&self, + create_info: &vk::FramebufferCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { unsafe { let mut framebuffer = mem::uninitialized(); - let err_code = self.device_fn - .create_framebuffer(self.handle, + let err_code = self.fp_v1_0() + .create_framebuffer(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut framebuffer); @@ -493,25 +487,25 @@ impl Device { } } - pub unsafe fn get_device_queue(&self, - queue_family_index: vk::uint32_t, - queue_index: vk::uint32_t) - -> vk::Queue { + unsafe fn get_device_queue(&self, + queue_family_index: vk::uint32_t, + queue_index: vk::uint32_t) + -> vk::Queue { let mut queue = mem::uninitialized(); - self.device_fn - .get_device_queue(self.handle, queue_family_index, queue_index, &mut queue); + self.fp_v1_0() + .get_device_queue(self.handle(), queue_family_index, queue_index, &mut queue); queue } - pub unsafe fn cmd_pipeline_barrier(&self, - command_buffer: vk::CommandBuffer, - src_stage_mask: vk::PipelineStageFlags, - dst_stage_mask: vk::PipelineStageFlags, - dependency_flags: vk::DependencyFlags, - memory_barriers: &[vk::MemoryBarrier], - buffer_memory_barriers: &[vk::BufferMemoryBarrier], - image_memory_barriers: &[vk::ImageMemoryBarrier]) { - self.device_fn.cmd_pipeline_barrier(command_buffer, + unsafe fn cmd_pipeline_barrier(&self, + command_buffer: vk::CommandBuffer, + src_stage_mask: vk::PipelineStageFlags, + dst_stage_mask: vk::PipelineStageFlags, + dependency_flags: vk::DependencyFlags, + memory_barriers: &[vk::MemoryBarrier], + buffer_memory_barriers: &[vk::BufferMemoryBarrier], + image_memory_barriers: &[vk::ImageMemoryBarrier]) { + self.fp_v1_0().cmd_pipeline_barrier(command_buffer, src_stage_mask, dst_stage_mask, dependency_flags, @@ -523,13 +517,13 @@ impl Device { image_memory_barriers.as_ptr()); } - pub unsafe fn create_render_pass(&self, - create_info: &vk::RenderPassCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_render_pass(&self, + create_info: &vk::RenderPassCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut renderpass = mem::uninitialized(); - let err_code = self.device_fn - .create_render_pass(self.handle, + let err_code = self.fp_v1_0() + .create_render_pass(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut renderpass); @@ -539,11 +533,11 @@ impl Device { } } - pub unsafe fn begin_command_buffer(&self, - command_buffer: vk::CommandBuffer, - create_info: &vk::CommandBufferBeginInfo) - -> VkResult<()> { - let err_code = self.device_fn + unsafe fn begin_command_buffer(&self, + command_buffer: vk::CommandBuffer, + create_info: &vk::CommandBufferBeginInfo) + -> VkResult<()> { + let err_code = self.fp_v1_0() .begin_command_buffer(command_buffer, create_info); match err_code { vk::Result::Success => Ok(()), @@ -551,8 +545,8 @@ impl Device { } } - pub unsafe fn end_command_buffer(&self, command_buffer: vk::CommandBuffer) -> VkResult<()> { - let err_code = self.device_fn + unsafe fn end_command_buffer(&self, command_buffer: vk::CommandBuffer) -> VkResult<()> { + let err_code = self.fp_v1_0() .end_command_buffer(command_buffer); match err_code { vk::Result::Success => Ok(()), @@ -560,13 +554,13 @@ impl Device { } } - pub unsafe fn wait_for_fences(&self, - fences: &[vk::Fence], - wait_all: bool, - timeout: vk::uint64_t) - -> VkResult<()> { - let err_code = self.device_fn - .wait_for_fences(self.handle, + unsafe fn wait_for_fences(&self, + fences: &[vk::Fence], + wait_all: bool, + timeout: vk::uint64_t) + -> VkResult<()> { + let err_code = self.fp_v1_0() + .wait_for_fences(self.handle(), fences.len() as vk::uint32_t, fences.as_ptr(), wait_all as vk::uint32_t, @@ -577,20 +571,20 @@ impl Device { } } - pub unsafe fn queue_wait_idle(&self, queue: vk::Queue) -> VkResult<()> { - let err_code = self.device_fn.queue_wait_idle(queue); + unsafe fn queue_wait_idle(&self, queue: vk::Queue) -> VkResult<()> { + let err_code = self.fp_v1_0().queue_wait_idle(queue); match err_code { vk::Result::Success => Ok(()), _ => Err(err_code), } } - pub unsafe fn queue_submit(&self, - queue: vk::Queue, - submits: &[vk::SubmitInfo], - fence: vk::Fence) - -> VkResult<()> { - let err_code = self.device_fn + unsafe fn queue_submit(&self, + queue: vk::Queue, + submits: &[vk::SubmitInfo], + fence: vk::Fence) + -> VkResult<()> { + let err_code = self.fp_v1_0() .queue_submit(queue, submits.len() as vk::uint32_t, submits.as_ptr(), @@ -601,13 +595,13 @@ impl Device { } } - pub unsafe fn create_image_view(&self, - create_info: &vk::ImageViewCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_image_view(&self, + create_info: &vk::ImageViewCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut image_view = mem::uninitialized(); - let err_code = self.device_fn - .create_image_view(self.handle, + let err_code = self.fp_v1_0() + .create_image_view(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut image_view); @@ -618,12 +612,12 @@ impl Device { } - pub unsafe fn allocate_command_buffers(&self, - create_info: &vk::CommandBufferAllocateInfo) - -> VkResult> { + unsafe fn allocate_command_buffers(&self, + create_info: &vk::CommandBufferAllocateInfo) + -> VkResult> { 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()); + let err_code = self.fp_v1_0() + .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), @@ -631,13 +625,13 @@ impl Device { } } - pub unsafe fn create_command_pool(&self, - create_info: &vk::CommandPoolCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_command_pool(&self, + create_info: &vk::CommandPoolCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut pool = mem::uninitialized(); - let err_code = self.device_fn - .create_command_pool(self.handle, + let err_code = self.fp_v1_0() + .create_command_pool(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut pool); @@ -647,13 +641,13 @@ impl Device { } } - pub unsafe fn create_image(&self, - create_info: &vk::ImageCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_image(&self, + create_info: &vk::ImageCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut image = mem::uninitialized(); - let err_code = self.device_fn - .create_image(self.handle, + let err_code = self.fp_v1_0() + .create_image(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut image); @@ -663,31 +657,31 @@ impl Device { } } - pub fn get_image_memory_requirements(&self, image: vk::Image) -> vk::MemoryRequirements { + fn get_image_memory_requirements(&self, image: vk::Image) -> vk::MemoryRequirements { unsafe { let mut mem_req = mem::uninitialized(); - self.device_fn - .get_image_memory_requirements(self.handle, image, &mut mem_req); + self.fp_v1_0() + .get_image_memory_requirements(self.handle(), image, &mut mem_req); mem_req } } - pub fn get_buffer_memory_requirements(&self, buffer: vk::Buffer) -> vk::MemoryRequirements { + fn get_buffer_memory_requirements(&self, buffer: vk::Buffer) -> vk::MemoryRequirements { unsafe { let mut mem_req = mem::uninitialized(); - self.device_fn - .get_buffer_memory_requirements(self.handle, buffer, &mut mem_req); + self.fp_v1_0() + .get_buffer_memory_requirements(self.handle(), buffer, &mut mem_req); mem_req } } - pub unsafe fn allocate_memory(&self, - create_info: &vk::MemoryAllocateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn allocate_memory(&self, + create_info: &vk::MemoryAllocateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut memory = mem::uninitialized(); - let err_code = self.device_fn - .allocate_memory(self.handle, + let err_code = self.fp_v1_0() + .allocate_memory(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut memory); @@ -697,13 +691,13 @@ impl Device { } } - pub unsafe fn create_shader_module(&self, - create_info: &vk::ShaderModuleCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_shader_module(&self, + create_info: &vk::ShaderModuleCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut shader = mem::uninitialized(); - let err_code = self.device_fn - .create_shader_module(self.handle, + let err_code = self.fp_v1_0() + .create_shader_module(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut shader); @@ -713,13 +707,13 @@ impl Device { } } - pub unsafe fn create_fence(&self, - create_info: &vk::FenceCreateInfo, - allocation_callbacks: Option<&vk::AllocationCallbacks>) - -> VkResult { + unsafe fn create_fence(&self, + create_info: &vk::FenceCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { let mut fence = mem::uninitialized(); - let err_code = self.device_fn - .create_fence(self.handle, + let err_code = self.fp_v1_0() + .create_fence(self.handle(), create_info, allocation_callbacks.as_raw_ptr(), &mut fence); @@ -729,29 +723,58 @@ impl Device { } } - pub unsafe fn bind_buffer_memory(&self, - buffer: vk::Buffer, - device_memory: vk::DeviceMemory, - offset: vk::DeviceSize) - -> VkResult<()> { - let err_code = self.device_fn - .bind_buffer_memory(self.handle, buffer, device_memory, offset); + unsafe fn bind_buffer_memory(&self, + buffer: vk::Buffer, + device_memory: vk::DeviceMemory, + offset: vk::DeviceSize) + -> VkResult<()> { + let err_code = self.fp_v1_0() + .bind_buffer_memory(self.handle(), buffer, device_memory, offset); match err_code { vk::Result::Success => Ok(()), _ => Err(err_code), } } - pub unsafe fn bind_image_memory(&self, - image: vk::Image, - device_memory: vk::DeviceMemory, - offset: vk::DeviceSize) - -> VkResult<()> { - let err_code = self.device_fn - .bind_image_memory(self.handle, image, device_memory, offset); + unsafe fn bind_image_memory(&self, + image: vk::Image, + device_memory: vk::DeviceMemory, + offset: vk::DeviceSize) + -> VkResult<()> { + let err_code = self.fp_v1_0() + .bind_image_memory(self.handle(), image, device_memory, offset); match err_code { vk::Result::Success => Ok(()), _ => Err(err_code), } } } + +#[derive(Clone)] +pub struct Device { + handle: vk::Device, + device_fn: V::DeviceFp, +} + +impl DeviceV1_0 for Device { + fn handle(&self) -> vk::Device { + self.handle + } + + fn fp_v1_0(&self) -> &vk::DeviceFn { + &self.device_fn.device_fn + } +} + +impl Device { + pub fn handle(&self) -> vk::Device { + self.handle + } + + pub unsafe fn from_raw(handle: vk::Device, device_fn: V::DeviceFp) -> Self { + Device { + handle: handle, + device_fn: device_fn, + } + } +} diff --git a/src/extensions/swapchain.rs b/src/extensions/swapchain.rs index 763543a..dc182e1 100644 --- a/src/extensions/swapchain.rs +++ b/src/extensions/swapchain.rs @@ -15,7 +15,7 @@ pub struct Swapchain { } impl Swapchain { - pub fn new(instance: &Instance, device: &Device) -> Result> { + pub fn new(instance: &Instance, device: &Device) -> Result> { let swapchain_fn = vk::SwapchainFn::load(|name| { unsafe { mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) } })?; diff --git a/src/extensions/win32_surface.rs b/src/extensions/win32_surface.rs index 0ba8ae0..0081f5b 100644 --- a/src/extensions/win32_surface.rs +++ b/src/extensions/win32_surface.rs @@ -14,6 +14,7 @@ pub struct Win32Surface { win32_surface_fn: vk::Win32SurfaceFn, } + impl Win32Surface { pub fn new(entry: &Entry, instance: &Instance) -> Result> { let surface_fn = vk::Win32SurfaceFn::load(|name| { diff --git a/src/instance.rs b/src/instance.rs index 3ff95ba..5a5e8ae 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -3,7 +3,7 @@ use prelude::*; use std::ptr; use std::mem; use vk; -use device::Device; +use device::{Device, DeviceFpV1_0}; use ::RawPtr; #[derive(Debug)] @@ -12,36 +12,35 @@ pub enum DeviceError { VkError(vk::Result), } -pub trait VkVersion{ +pub trait VkVersion { type InstanceFp; type DeviceFp; } - #[warn(non_camel_case_types)] pub struct V1_0; -impl VkVersion for V1_0{ +impl VkVersion for V1_0 { type InstanceFp = InstanceFpV1_0; - type DeviceFp = (); + type DeviceFp = DeviceFpV1_0; } #[warn(non_camel_case_types)] -pub struct InstanceFpV1_0{ - pub instance_fn: vk::InstanceFn +pub struct InstanceFpV1_0 { + pub instance_fn: vk::InstanceFn, } #[derive(Clone)] pub struct Instance { handle: vk::Instance, - instance_fp: V::InstanceFp + instance_fp: V::InstanceFp, } -impl InstanceV1_0 for Instance{ - fn handle(&self) -> vk::Instance{ +impl InstanceV1_0 for Instance { + fn handle(&self) -> vk::Instance { self.handle } - fn fp_v1_0(&self) -> &vk::InstanceFn{ + fn fp_v1_0(&self) -> &vk::InstanceFn { &self.instance_fp.instance_fn } } @@ -53,21 +52,17 @@ impl Instance { pub fn from_raw(handle: vk::Instance, version: V::InstanceFp) -> Self { Instance { handle: handle, - instance_fp: version + instance_fp: version, } } - } -#[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 { +impl Instance { + pub unsafe fn create_device(&self, + physical_device: vk::PhysicalDevice, + create_info: &vk::DeviceCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> Result, DeviceError> { let mut device: vk::Device = mem::uninitialized(); let err_code = self.fp_v1_0() .create_device(physical_device, @@ -80,12 +75,18 @@ pub trait InstanceV1_0 { let device_fn = vk::DeviceFn::load(|name| { mem::transmute(self.fp_v1_0().get_device_proc_addr(device, name.as_ptr())) }).map_err(|err| DeviceError::LoadError(err))?; - Ok(Device::from_raw(device, device_fn)) + Ok(Device::from_raw(device, DeviceFpV1_0 { device_fn: device_fn })) } +} + +#[warn(non_camel_case_types)] +pub trait InstanceV1_0 { + fn handle(&self) -> vk::Instance; + fn fp_v1_0(&self) -> &vk::InstanceFn; fn get_device_proc_addr(&self, - device: vk::Device, - p_name: *const vk::c_char) - -> vk::PFN_vkVoidFunction { + device: vk::Device, + p_name: *const vk::c_char) + -> vk::PFN_vkVoidFunction { unsafe { self.fp_v1_0().get_device_proc_addr(device, p_name) } } @@ -94,9 +95,9 @@ pub trait InstanceV1_0 { } fn get_physical_device_format_properties(&self, - physical_device: vk::PhysicalDevice, - format: vk::Format) - -> vk::FormatProperties { + physical_device: vk::PhysicalDevice, + format: vk::Format) + -> vk::FormatProperties { unsafe { let mut format_prop = mem::uninitialized(); self.fp_v1_0() @@ -105,8 +106,8 @@ pub trait InstanceV1_0 { } } fn get_physical_device_memory_properties(&self, - physical_device: vk::PhysicalDevice) - -> vk::PhysicalDeviceMemoryProperties { + physical_device: vk::PhysicalDevice) + -> vk::PhysicalDeviceMemoryProperties { unsafe { let mut memory_prop = mem::uninitialized(); self.fp_v1_0() @@ -116,8 +117,8 @@ pub trait InstanceV1_0 { } fn get_physical_device_queue_family_properties(&self, - physical_device: vk::PhysicalDevice) - -> Vec { + physical_device: vk::PhysicalDevice) + -> Vec { unsafe { let mut queue_count = 0; self.fp_v1_0() @@ -176,6 +177,5 @@ pub trait InstanceV1_0 { } } -//pub trait InstanceMajor1Minor1: InstanceMajor1Minor0 {} -//pub trait InstanceMajor1Minor2: InstanceMajor1Minor1 {} - +// pub trait InstanceMajor1Minor1: InstanceMajor1Minor0 {} +// pub trait InstanceMajor1Minor2: InstanceMajor1Minor1 {}