Command buffer allocation/freeing

This commit is contained in:
Dzmitry Malyshau 2017-11-06 22:46:57 -05:00
parent 82d4e84e03
commit fcdebb5160
3 changed files with 49 additions and 21 deletions

View file

@ -19,7 +19,7 @@ binding: $(BINDING)
$(BINDING): $(VULKAN_DIR)/vulkan/*.h $(BINDING): $(VULKAN_DIR)/vulkan/*.h
bindgen --no-layout-tests --rustfmt-bindings $(VULKAN_DIR)/vulkan/vulkan.h -o $(BINDING) bindgen --no-layout-tests --rustfmt-bindings $(VULKAN_DIR)/vulkan/vulkan.h -o $(BINDING)
$(LIBRARY): src/*.rs Cargo.toml $(LIBRARY): src/*.rs Cargo.toml Cargo.lock
cargo build cargo build
mkdir -p target/native mkdir -p target/native

View file

@ -12,6 +12,7 @@ int main() {
VkInstance instance; VkInstance instance;
VkResult res; VkResult res;
unsigned int i;
res = vkCreateInstance(&inst_info, NULL, &instance); res = vkCreateInstance(&inst_info, NULL, &instance);
if (res == VK_ERROR_INCOMPATIBLE_DRIVER) { if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
@ -36,7 +37,7 @@ int main() {
assert(queue_family_count); assert(queue_family_count);
int queue_family_index = -1; int queue_family_index = -1;
for (unsigned int i = 0; i < queue_family_count; i++) { for (i = 0; i < queue_family_count; i++) {
if (queue_family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { if (queue_family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
queue_family_index = i; queue_family_index = i;
break; break;

View file

@ -9,6 +9,7 @@ mod handle;
use std::{cmp, slice}; use std::{cmp, slice};
use hal::{Device, Instance, PhysicalDevice, QueueFamily}; // traits only use hal::{Device, Instance, PhysicalDevice, QueueFamily}; // traits only
use hal::pool::RawCommandPool;
use back::Backend as B; use back::Backend as B;
use handle::Handle; use handle::Handle;
@ -462,6 +463,7 @@ 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>; pub type VkCommandPool = Handle<<B as hal::Backend>::CommandPool>;
pub type VkCommandBuffer = <B as hal::Backend>::CommandBuffer;
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@ -477,12 +479,6 @@ pub struct VkSemaphore_T {
pub type VkSemaphore = *mut VkSemaphore_T; pub type VkSemaphore = *mut VkSemaphore_T;
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct VkCommandBuffer_T {
_unused: [u8; 0],
}
pub type VkCommandBuffer = *mut VkCommandBuffer_T;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct VkFence_T { pub struct VkFence_T {
_unused: [u8; 0], _unused: [u8; 0],
} }
@ -4890,22 +4886,53 @@ pub extern fn vkDestroyCommandPool(
gpu.device.destroy_command_pool(*commandPool.unwrap()); gpu.device.destroy_command_pool(*commandPool.unwrap());
} }
extern "C" { #[no_mangle]
pub fn vkResetCommandPool(device: VkDevice, commandPool: VkCommandPool, pub extern fn vkResetCommandPool(
flags: VkCommandPoolResetFlags) -> VkResult; _gpu: VkDevice,
mut commandPool: VkCommandPool,
_flags: VkCommandPoolResetFlags,
) -> VkResult {
commandPool.reset();
VkResult::VK_SUCCESS
} }
extern "C" {
pub fn vkAllocateCommandBuffers(device: VkDevice, #[no_mangle]
pAllocateInfo: pub extern fn vkAllocateCommandBuffers(
*const VkCommandBufferAllocateInfo, _gpu: VkDevice,
pCommandBuffers: *mut VkCommandBuffer) pAllocateInfo: *const VkCommandBufferAllocateInfo,
-> VkResult; pCommandBuffers: *mut VkCommandBuffer,
) -> VkResult {
let info = unsafe { &mut *(pAllocateInfo as *mut VkCommandBufferAllocateInfo) };
assert_eq!(info.level, VkCommandBufferLevel::VK_COMMAND_BUFFER_LEVEL_PRIMARY); //TODO
let count = info.commandBufferCount as usize;
let cmd_bufs = info.commandPool.allocate(count);
let output = unsafe {
slice::from_raw_parts_mut(pCommandBuffers, count)
};
for (out, cmd_buf) in output.iter_mut().zip(cmd_bufs) {
*out = cmd_buf;
} }
extern "C" {
pub fn vkFreeCommandBuffers(device: VkDevice, commandPool: VkCommandPool, VkResult::VK_SUCCESS
}
#[no_mangle]
pub extern fn vkFreeCommandBuffers(
_gpu: VkDevice,
mut commandPool: VkCommandPool,
commandBufferCount: u32, commandBufferCount: u32,
pCommandBuffers: *const VkCommandBuffer); pCommandBuffers: *const VkCommandBuffer,
) {
let buffer_slice = unsafe {
slice::from_raw_parts(pCommandBuffers, commandBufferCount as _)
};
let buffers = buffer_slice.to_vec();
unsafe { commandPool.free(buffers) };
} }
extern "C" { extern "C" {
pub fn vkBeginCommandBuffer(commandBuffer: VkCommandBuffer, pub fn vkBeginCommandBuffer(commandBuffer: VkCommandBuffer,
pBeginInfo: *const VkCommandBufferBeginInfo) pBeginInfo: *const VkCommandBufferBeginInfo)