From ce4945a61cd1e7f480530f3003393aeb2cc28a90 Mon Sep 17 00:00:00 2001 From: maik klein Date: Wed, 28 Dec 2016 09:07:27 +0100 Subject: [PATCH] Add allocation callbacks everywhere --- examples/src/lib.rs | 2 +- src/allocator.rs | 30 ++++++ src/device.rs | 223 ++++++++++++++++++++++++++++++++------------ src/entry.rs | 8 +- src/instance.rs | 6 +- src/lib.rs | 13 +++ 6 files changed, 215 insertions(+), 67 deletions(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 39bc9cf..57bea68 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -249,7 +249,7 @@ impl ExampleBase { 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) + let instance: Instance = entry.create_instance(&create_info, None) .expect("Instance creation error"); let debug_info = vk::DebugReportCallbackCreateInfoEXT { s_type: vk::StructureType::DebugReportCallbackCreateInfoExt, diff --git a/src/allocator.rs b/src/allocator.rs index 1cf8b1b..1b0015d 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -36,7 +36,37 @@ pub trait VkAllocation { } pub struct DefaultAllocatorCallback; +pub struct TestAlloc; +impl VkAllocation for TestAlloc { + unsafe extern "system" fn allocation(_: *mut (), + _: vk::size_t, + _: vk::size_t, + _: vk::SystemAllocationScope) + -> *mut () { + ptr::null_mut() + } + + unsafe extern "system" fn reallocation(_: *mut vk::c_void, + _: *mut vk::c_void, + _: vk::size_t, + _: vk::size_t, + _: vk::SystemAllocationScope) + -> *mut vk::c_void { + ptr::null_mut() + } + unsafe extern "system" fn free(_: *mut vk::c_void, _: *mut vk::c_void) {} + unsafe extern "system" fn internal_allocation(_: *mut vk::c_void, + _: vk::size_t, + _: vk::InternalAllocationType, + _: vk::SystemAllocationScope) { + } + unsafe extern "system" fn internal_free(_: *mut vk::c_void, + _: vk::size_t, + _: vk::InternalAllocationType, + _: vk::SystemAllocationScope) { + } +} impl VkAllocation for DefaultAllocatorCallback { unsafe extern "system" fn allocation(_: *mut (), _: vk::size_t, diff --git a/src/device.rs b/src/device.rs index e4eb840..eedfc69 100644 --- a/src/device.rs +++ b/src/device.rs @@ -3,6 +3,7 @@ use prelude::*; use std::ptr; use std::mem; use vk; +use ::RawPtr; unsafe impl Sync for Device {} unsafe impl Send for Device {} @@ -24,69 +25,105 @@ impl Device { } } - pub unsafe fn destroy_device(&self) { - self.device_fn.destroy_device(self.handle, ptr::null()); + 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) { - self.device_fn.destroy_sampler(self.handle, sampler, ptr::null()); + 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) { - self.device_fn.free_memory(self.handle, memory, ptr::null()); + pub unsafe fn free_memory(&self, + memory: vk::DeviceMemory, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.device_fn.free_memory(self.handle, memory, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_fence(&self, fence: vk::Fence) { - self.device_fn.destroy_fence(self.handle, fence, ptr::null()); + 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()); } - pub unsafe fn destroy_image(&self, image: vk::Image) { - self.device_fn.destroy_image(self.handle, image, ptr::null()); + 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()); } - pub unsafe fn destroy_command_pool(&self, pool: vk::CommandPool) { - self.device_fn.destroy_command_pool(self.handle, pool, ptr::null()); + 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()); } - pub unsafe fn destroy_image_view(&self, image_view: vk::ImageView) { - self.device_fn.destroy_image_view(self.handle, image_view, ptr::null()); + 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()); } - pub unsafe fn destroy_render_pass(&self, renderpass: vk::RenderPass) { - self.device_fn.destroy_render_pass(self.handle, renderpass, ptr::null()); + pub unsafe fn destroy_render_pass(&self, + renderpass: vk::RenderPass, + 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) { - self.device_fn.destroy_framebuffer(self.handle, framebuffer, ptr::null()); + 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) { - self.device_fn.destroy_pipeline_layout(self.handle, pipeline_layout, ptr::null()); + pub unsafe fn destroy_pipeline_layout(&self, + pipeline_layout: vk::PipelineLayout, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.device_fn.destroy_pipeline_layout(self.handle, + pipeline_layout, + allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_buffer(&self, buffer: vk::Buffer) { - self.device_fn.destroy_buffer(self.handle, buffer, ptr::null()); + 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()); } - pub unsafe fn destroy_shader_module(&self, shader: vk::ShaderModule) { - self.device_fn.destroy_shader_module(self.handle, shader, ptr::null()); + 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) { - self.device_fn.destroy_pipeline(self.handle, pipeline, ptr::null()); + 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) { - self.device_fn.destroy_semaphore(self.handle, semaphore, ptr::null()); + pub unsafe fn destroy_semaphore(&self, + semaphore: vk::Semaphore, + allocation_callbacks: Option<&vk::AllocationCallbacks>) { + self.device_fn.destroy_semaphore(self.handle, semaphore, allocation_callbacks.as_raw_ptr()); } - pub unsafe fn destroy_descriptor_pool(&self, pool: vk::DescriptorPool) { - self.device_fn.destroy_descriptor_pool(self.handle, pool, ptr::null()); + 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()); } - pub unsafe fn destroy_descriptor_set_layout(&self, layout: vk::DescriptorSetLayout) { - self.device_fn.destroy_descriptor_set_layout(self.handle, layout, ptr::null()); + 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()); } pub unsafe fn free_descriptor_sets(&self, @@ -108,11 +145,17 @@ impl Device { descriptor_copies.as_ptr()); } - pub fn create_sampler(&self, create_info: &vk::SamplerCreateInfo) -> VkResult { + pub fn create_sampler(&self, + create_info: &vk::SamplerCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { unsafe { let mut sampler = mem::uninitialized(); let err_code = self.device_fn - .create_sampler(self.handle, create_info, ptr::null(), &mut sampler); + .create_sampler(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut sampler); match err_code { vk::Result::Success => Ok(sampler), _ => Err(err_code), @@ -176,12 +219,16 @@ impl Device { } } pub fn create_descriptor_set_layout(&self, - create_info: &vk::DescriptorSetLayoutCreateInfo) + create_info: &vk::DescriptorSetLayoutCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> VkResult { unsafe { let mut layout = mem::uninitialized(); let err_code = self.device_fn - .create_descriptor_set_layout(self.handle, create_info, ptr::null(), &mut layout); + .create_descriptor_set_layout(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut layout); match err_code { vk::Result::Success => Ok(layout), _ => Err(err_code), @@ -199,12 +246,16 @@ impl Device { } pub fn create_descriptor_pool(&self, - create_info: &vk::DescriptorPoolCreateInfo) + create_info: &vk::DescriptorPoolCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> VkResult { unsafe { let mut pool = mem::uninitialized(); let err_code = self.device_fn - .create_descriptor_pool(self.handle, create_info, ptr::null(), &mut pool); + .create_descriptor_pool(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut pool); match err_code { vk::Result::Success => Ok(pool), _ => Err(err_code), @@ -337,12 +388,16 @@ impl Device { pub fn create_semaphore(&self, - create_info: &vk::SemaphoreCreateInfo) + create_info: &vk::SemaphoreCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> VkResult { unsafe { let mut semaphore = mem::uninitialized(); let err_code = self.device_fn - .create_semaphore(self.handle, create_info, ptr::null(), &mut semaphore); + .create_semaphore(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut semaphore); match err_code { vk::Result::Success => Ok(semaphore), _ => Err(err_code), @@ -352,7 +407,8 @@ impl Device { pub fn create_graphics_pipelines(&self, pipeline_cache: vk::PipelineCache, - create_infos: &[vk::GraphicsPipelineCreateInfo]) + create_infos: &[vk::GraphicsPipelineCreateInfo], + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> VkResult> { unsafe { let mut pipelines = Vec::with_capacity(create_infos.len()); @@ -361,7 +417,7 @@ impl Device { pipeline_cache, create_infos.len() as vk::uint32_t, create_infos.as_ptr(), - ptr::null(), + allocation_callbacks.as_raw_ptr(), pipelines.as_mut_ptr()); pipelines.set_len(create_infos.len()); match err_code { @@ -371,11 +427,17 @@ impl Device { } } - pub fn create_buffer(&self, create_info: &vk::BufferCreateInfo) -> VkResult { + pub fn create_buffer(&self, + create_info: &vk::BufferCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { unsafe { let mut buffer = mem::uninitialized(); let err_code = self.device_fn - .create_buffer(self.handle, create_info, ptr::null(), &mut buffer); + .create_buffer(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut buffer); match err_code { vk::Result::Success => Ok(buffer), _ => Err(err_code), @@ -384,14 +446,15 @@ impl Device { } pub fn create_pipeline_layout(&self, - create_info: &vk::PipelineLayoutCreateInfo) + 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, create_info, - ptr::null(), + allocation_callbacks.as_raw_ptr(), &mut pipeline_layout); match err_code { vk::Result::Success => Ok(pipeline_layout), @@ -423,12 +486,16 @@ impl Device { } pub fn create_framebuffer(&self, - create_info: &vk::FramebufferCreateInfo) + 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, create_info, ptr::null(), &mut framebuffer); + .create_framebuffer(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut framebuffer); match err_code { vk::Result::Success => Ok(framebuffer), _ => Err(err_code), @@ -469,12 +536,16 @@ impl Device { } pub fn create_render_pass(&self, - create_info: &vk::RenderPassCreateInfo) + create_info: &vk::RenderPassCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> VkResult { unsafe { let mut renderpass = mem::uninitialized(); let err_code = self.device_fn - .create_render_pass(self.handle, create_info, ptr::null(), &mut renderpass); + .create_render_pass(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut renderpass); match err_code { vk::Result::Success => Ok(renderpass), _ => Err(err_code), @@ -561,12 +632,16 @@ impl Device { } pub fn create_image_view(&self, - create_info: &vk::ImageViewCreateInfo) + create_info: &vk::ImageViewCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> VkResult { unsafe { let mut image_view = mem::uninitialized(); let err_code = self.device_fn - .create_image_view(self.handle, create_info, ptr::null(), &mut image_view); + .create_image_view(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut image_view); match err_code { vk::Result::Success => Ok(image_view), _ => Err(err_code), @@ -589,12 +664,16 @@ impl Device { } pub fn create_command_pool(&self, - create_info: &vk::CommandPoolCreateInfo) + create_info: &vk::CommandPoolCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> 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, + allocation_callbacks.as_raw_ptr(), + &mut pool); match err_code { vk::Result::Success => Ok(pool), _ => Err(err_code), @@ -603,11 +682,17 @@ impl Device { } - pub fn create_image(&self, create_info: &vk::ImageCreateInfo) -> VkResult { + pub fn create_image(&self, + create_info: &vk::ImageCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { unsafe { let mut image = mem::uninitialized(); let err_code = self.device_fn - .create_image(self.handle, create_info, ptr::null(), &mut image); + .create_image(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut image); match err_code { vk::Result::Success => Ok(image), _ => Err(err_code), @@ -634,12 +719,16 @@ impl Device { } pub fn allocate_memory(&self, - create_info: &vk::MemoryAllocateInfo) + create_info: &vk::MemoryAllocateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> VkResult { unsafe { let mut memory = mem::uninitialized(); let err_code = self.device_fn - .allocate_memory(self.handle, create_info, ptr::null(), &mut memory); + .allocate_memory(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut memory); match err_code { vk::Result::Success => Ok(memory), _ => Err(err_code), @@ -648,12 +737,16 @@ impl Device { } pub fn create_shader_module(&self, - create_info: &vk::ShaderModuleCreateInfo) + create_info: &vk::ShaderModuleCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> VkResult { unsafe { let mut shader = mem::uninitialized(); let err_code = self.device_fn - .create_shader_module(self.handle, create_info, ptr::null(), &mut shader); + .create_shader_module(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut shader); match err_code { vk::Result::Success => Ok(shader), _ => Err(err_code), @@ -661,11 +754,17 @@ impl Device { } } - pub fn create_fence(&self, create_info: &vk::FenceCreateInfo) -> VkResult { + pub fn create_fence(&self, + create_info: &vk::FenceCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) + -> VkResult { unsafe { let mut fence = mem::uninitialized(); let err_code = self.device_fn - .create_fence(self.handle, create_info, ptr::null(), &mut fence); + .create_fence(self.handle, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut fence); match err_code { vk::Result::Success => Ok(fence), _ => Err(err_code), diff --git a/src/entry.rs b/src/entry.rs index f8a8a99..83e3f03 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -69,11 +69,15 @@ impl Entry { } pub fn create_instance(&self, - create_info: &vk::InstanceCreateInfo) + create_info: &vk::InstanceCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> Result { unsafe { + use ::RawPtr; let mut instance: vk::Instance = mem::uninitialized(); - let err_code = self.entry_fn.create_instance(create_info, ptr::null(), &mut instance); + let err_code = self.entry_fn.create_instance(create_info, + allocation_callbacks.as_raw_ptr(), + &mut instance); if err_code != vk::Result::Success { return Err(InstanceError::VkError(err_code)); } diff --git a/src/instance.rs b/src/instance.rs index e401d1e..eea0a82 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -4,6 +4,7 @@ use std::ptr; use std::mem; use vk; use device::Device; +use ::RawPtr; #[derive(Debug)] pub enum DeviceError { @@ -31,12 +32,13 @@ impl Instance { pub fn create_device(&self, physical_device: vk::PhysicalDevice, - create_info: &vk::DeviceCreateInfo) + create_info: &vk::DeviceCreateInfo, + allocation_callbacks: Option<&vk::AllocationCallbacks>) -> Result { unsafe { let mut device: vk::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, allocation_callbacks.as_raw_ptr(), &mut device); if err_code != vk::Result::Success { return Err(DeviceError::VkError(err_code)); } diff --git a/src/lib.rs b/src/lib.rs index 1d3ec8e..f05b512 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,3 +8,16 @@ pub mod prelude; pub mod vk; pub mod allocator; pub mod extensions; + +pub trait RawPtr{ + fn as_raw_ptr(&self) -> *const T; +} + +impl<'r, T> RawPtr for Option<&'r T>{ + fn as_raw_ptr(&self) -> *const T{ + match self{ + &Some(inner) => inner as *const T, + _ => ::std::ptr::null() + } + } +}