From 82d4e84e0363f0827724a6d634869c2ba69aba11 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 6 Nov 2017 22:33:36 -0500 Subject: [PATCH] Command pool creation/destruction --- native/test.c | 29 ++++++++++++++++++++++ src/lib.rs | 66 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/native/test.c b/native/test.c index 6abe528..b10ac3a 100644 --- a/native/test.c +++ b/native/test.c @@ -1,3 +1,5 @@ +/// Sample code adopted from https://github.com/LunarG/VulkanSamples + #include #include #include @@ -61,6 +63,33 @@ int main() { //TODO + VkCommandPool cmd_pool = 0; + VkCommandPoolCreateInfo cmd_pool_info = {}; + cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; + cmd_pool_info.pNext = NULL; + cmd_pool_info.queueFamilyIndex = queue_family_index; + cmd_pool_info.flags = 0; + + res = vkCreateCommandPool(device, &cmd_pool_info, NULL, &cmd_pool); + printf("\tvkCreateCommandPool: res=%d\n", res); + assert(!res); + + VkCommandBuffer cmd_buffer = 0; + VkCommandBufferAllocateInfo cmd_alloc_info; + cmd_alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + cmd_alloc_info.pNext = NULL; + cmd_alloc_info.commandPool = cmd_pool; + cmd_alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + cmd_alloc_info.commandBufferCount = 1; + + res = vkAllocateCommandBuffers(device, &cmd_alloc_info, &cmd_buffer); + printf("\tvkAllocateCommandBuffers: res=%d\n", res); + assert(!res); + + // Some work... + + vkFreeCommandBuffers(device, cmd_pool, 1, &cmd_buffer); + vkDestroyCommandPool(device, cmd_pool, NULL); vkDestroyDevice(device, NULL); vkDestroyInstance(instance, NULL); diff --git a/src/lib.rs b/src/lib.rs index 45c154f..e3d8807 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ extern crate gfx_backend_vulkan as back; mod handle; use std::{cmp, slice}; -use hal::{Instance, PhysicalDevice, QueueFamily}; // traits only +use hal::{Device, Instance, PhysicalDevice, QueueFamily}; // traits only use back::Backend as B; use handle::Handle; @@ -461,6 +461,7 @@ pub type VkSampleMask = u32; pub type VkInstance = Handle; pub type VkPhysicalDevice = Handle>; pub type VkDevice = Handle>; +pub type VkCommandPool = Handle<::CommandPool>; #[repr(C)] #[derive(Debug, Copy, Clone)] @@ -588,12 +589,7 @@ pub struct VkFramebuffer_T { _unused: [u8; 0], } pub type VkFramebuffer = *mut VkFramebuffer_T; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct VkCommandPool_T { - _unused: [u8; 0], -} -pub type VkCommandPool = *mut VkCommandPool_T; + pub const VkPipelineCacheHeaderVersion_VK_PIPELINE_CACHE_HEADER_VERSION_BEGIN_RANGE: VkPipelineCacheHeaderVersion = VkPipelineCacheHeaderVersion::VK_PIPELINE_CACHE_HEADER_VERSION_ONE; @@ -4370,14 +4366,14 @@ extern "C" { #[no_mangle] pub extern fn vkGetPhysicalDeviceQueueFamilyProperties( - physicalDevice: VkPhysicalDevice, + adapter: VkPhysicalDevice, pQueueFamilyPropertyCount: *mut u32, pQueueFamilyProperties: *mut VkQueueFamilyProperties, ) { let output = unsafe { slice::from_raw_parts_mut(pQueueFamilyProperties, *pQueueFamilyPropertyCount as _) }; - let families = &physicalDevice.queue_families; + let families = &adapter.queue_families; if output.len() > families.len() { unsafe { *pQueueFamilyPropertyCount = families.len() as _ }; } @@ -4415,7 +4411,7 @@ extern "C" { #[no_mangle] pub extern fn vkCreateDevice( - physicalDevice: VkPhysicalDevice, + adapter: VkPhysicalDevice, pCreateInfo: *const VkDeviceCreateInfo, _pAllocator: *const VkAllocationCallbacks, pDevice: *mut VkDevice, @@ -4425,13 +4421,13 @@ pub extern fn vkCreateDevice( slice::from_raw_parts(dev_info.pQueueCreateInfos, dev_info.queueCreateInfoCount as _) }; let request_infos = queue_infos.iter().map(|info| { - let family = physicalDevice - .queue_families[info.queueFamilyIndex as usize] - .clone(); + let family = adapter + .queue_families[info.queueFamilyIndex as usize] + .clone(); (family, vec![1.0; info.queueCount as usize]) }).collect::>(); - let gpu = physicalDevice.physical_device.clone().open(request_infos); + let gpu = adapter.physical_device.clone().open(request_infos); unsafe { *pDevice = Handle::new(gpu) }; VkResult::VK_SUCCESS @@ -4858,16 +4854,42 @@ extern "C" { renderPass: VkRenderPass, pGranularity: *mut VkExtent2D); } -extern "C" { - pub fn vkCreateCommandPool(device: VkDevice, - pCreateInfo: *const VkCommandPoolCreateInfo, - pAllocator: *const VkAllocationCallbacks, - pCommandPool: *mut VkCommandPool) -> VkResult; + +#[no_mangle] +pub extern fn vkCreateCommandPool( + gpu: VkDevice, + pCreateInfo: *const VkCommandPoolCreateInfo, + _pAllocator: *const VkAllocationCallbacks, + pCommandPool: *mut VkCommandPool, +) -> VkResult { + use hal::pool::CommandPoolCreateFlags; + + let info = unsafe { &*pCreateInfo }; + assert_eq!(info.queueFamilyIndex, 0); //TODO + let family = gpu.queue_groups[0].family(); + + let mut flags = CommandPoolCreateFlags::empty(); + if info.flags & VkCommandPoolCreateFlagBits::VK_COMMAND_POOL_CREATE_TRANSIENT_BIT as u32 != 0 { + flags |= CommandPoolCreateFlags::TRANSIENT; + } + if info.flags & VkCommandPoolCreateFlagBits::VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT as u32 != 0 { + flags |= CommandPoolCreateFlags::RESET_INDIVIDUAL; + } + + let pool = gpu.device.create_command_pool(family, flags); + unsafe { *pCommandPool = Handle::new(pool) }; + VkResult::VK_SUCCESS } -extern "C" { - pub fn vkDestroyCommandPool(device: VkDevice, commandPool: VkCommandPool, - pAllocator: *const VkAllocationCallbacks); + +#[no_mangle] +pub extern fn vkDestroyCommandPool( + gpu: VkDevice, + commandPool: VkCommandPool, + _pAllocator: *const VkAllocationCallbacks, +) { + gpu.device.destroy_command_pool(*commandPool.unwrap()); } + extern "C" { pub fn vkResetCommandPool(device: VkDevice, commandPool: VkCommandPool, flags: VkCommandPoolResetFlags) -> VkResult;