Command pool creation/destruction

This commit is contained in:
Dzmitry Malyshau 2017-11-06 22:33:36 -05:00
parent 1828304cbd
commit 82d4e84e03
2 changed files with 73 additions and 22 deletions

View file

@ -1,3 +1,5 @@
/// Sample code adopted from https://github.com/LunarG/VulkanSamples
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
@ -61,6 +63,33 @@ int main() {
//TODO //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); vkDestroyDevice(device, NULL);
vkDestroyInstance(instance, NULL); vkDestroyInstance(instance, NULL);

View file

@ -8,7 +8,7 @@ extern crate gfx_backend_vulkan as back;
mod handle; mod handle;
use std::{cmp, slice}; 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 back::Backend as B;
use handle::Handle; use handle::Handle;
@ -461,6 +461,7 @@ pub type VkSampleMask = u32;
pub type VkInstance = Handle<back::Instance>; pub type VkInstance = Handle<back::Instance>;
pub type VkPhysicalDevice = Handle<hal::Adapter<B>>; pub type VkPhysicalDevice = Handle<hal::Adapter<B>>;
pub type VkDevice = Handle<hal::Gpu<B>>; pub type VkDevice = Handle<hal::Gpu<B>>;
pub type VkCommandPool = Handle<<B as hal::Backend>::CommandPool>;
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@ -588,12 +589,7 @@ pub struct VkFramebuffer_T {
_unused: [u8; 0], _unused: [u8; 0],
} }
pub type VkFramebuffer = *mut VkFramebuffer_T; 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: pub const VkPipelineCacheHeaderVersion_VK_PIPELINE_CACHE_HEADER_VERSION_BEGIN_RANGE:
VkPipelineCacheHeaderVersion = VkPipelineCacheHeaderVersion =
VkPipelineCacheHeaderVersion::VK_PIPELINE_CACHE_HEADER_VERSION_ONE; VkPipelineCacheHeaderVersion::VK_PIPELINE_CACHE_HEADER_VERSION_ONE;
@ -4370,14 +4366,14 @@ extern "C" {
#[no_mangle] #[no_mangle]
pub extern fn vkGetPhysicalDeviceQueueFamilyProperties( pub extern fn vkGetPhysicalDeviceQueueFamilyProperties(
physicalDevice: VkPhysicalDevice, adapter: VkPhysicalDevice,
pQueueFamilyPropertyCount: *mut u32, pQueueFamilyPropertyCount: *mut u32,
pQueueFamilyProperties: *mut VkQueueFamilyProperties, pQueueFamilyProperties: *mut VkQueueFamilyProperties,
) { ) {
let output = unsafe { let output = unsafe {
slice::from_raw_parts_mut(pQueueFamilyProperties, *pQueueFamilyPropertyCount as _) slice::from_raw_parts_mut(pQueueFamilyProperties, *pQueueFamilyPropertyCount as _)
}; };
let families = &physicalDevice.queue_families; let families = &adapter.queue_families;
if output.len() > families.len() { if output.len() > families.len() {
unsafe { *pQueueFamilyPropertyCount = families.len() as _ }; unsafe { *pQueueFamilyPropertyCount = families.len() as _ };
} }
@ -4415,7 +4411,7 @@ extern "C" {
#[no_mangle] #[no_mangle]
pub extern fn vkCreateDevice( pub extern fn vkCreateDevice(
physicalDevice: VkPhysicalDevice, adapter: VkPhysicalDevice,
pCreateInfo: *const VkDeviceCreateInfo, pCreateInfo: *const VkDeviceCreateInfo,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
pDevice: *mut VkDevice, pDevice: *mut VkDevice,
@ -4425,13 +4421,13 @@ pub extern fn vkCreateDevice(
slice::from_raw_parts(dev_info.pQueueCreateInfos, dev_info.queueCreateInfoCount as _) slice::from_raw_parts(dev_info.pQueueCreateInfos, dev_info.queueCreateInfoCount as _)
}; };
let request_infos = queue_infos.iter().map(|info| { let request_infos = queue_infos.iter().map(|info| {
let family = physicalDevice let family = adapter
.queue_families[info.queueFamilyIndex as usize] .queue_families[info.queueFamilyIndex as usize]
.clone(); .clone();
(family, vec![1.0; info.queueCount as usize]) (family, vec![1.0; info.queueCount as usize])
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
let gpu = physicalDevice.physical_device.clone().open(request_infos); let gpu = adapter.physical_device.clone().open(request_infos);
unsafe { *pDevice = Handle::new(gpu) }; unsafe { *pDevice = Handle::new(gpu) };
VkResult::VK_SUCCESS VkResult::VK_SUCCESS
@ -4858,16 +4854,42 @@ extern "C" {
renderPass: VkRenderPass, renderPass: VkRenderPass,
pGranularity: *mut VkExtent2D); pGranularity: *mut VkExtent2D);
} }
extern "C" {
pub fn vkCreateCommandPool(device: VkDevice, #[no_mangle]
pCreateInfo: *const VkCommandPoolCreateInfo, pub extern fn vkCreateCommandPool(
pAllocator: *const VkAllocationCallbacks, gpu: VkDevice,
pCommandPool: *mut VkCommandPool) -> VkResult; 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, #[no_mangle]
pAllocator: *const VkAllocationCallbacks); pub extern fn vkDestroyCommandPool(
gpu: VkDevice,
commandPool: VkCommandPool,
_pAllocator: *const VkAllocationCallbacks,
) {
gpu.device.destroy_command_pool(*commandPool.unwrap());
} }
extern "C" { extern "C" {
pub fn vkResetCommandPool(device: VkDevice, commandPool: VkCommandPool, pub fn vkResetCommandPool(device: VkDevice, commandPool: VkCommandPool,
flags: VkCommandPoolResetFlags) -> VkResult; flags: VkCommandPoolResetFlags) -> VkResult;