Skip to content

Commit

Permalink
[vent-rendering] reset command pools instead command buffers
Browse files Browse the repository at this point in the history
Its a bit faster and should be use instead of resetting command buffers every frame. For details check out https://github.com/KhronosGroup/Vulkan-Samples/tree/main/samples/performance/command_buffer_usage
  • Loading branch information
Snowiiii committed Jul 10, 2024
1 parent 3afcc06 commit 614f4c2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Vent-Engine Platform Support:

| Platform | Runtime | Editor |
| -------- | ------- | ------ |
| Windows | ✅️ | **** |
| Windows | 😬 | **** |
| MacOS | **** | **** |
| Unix | ✅️ | **** |
| Redox | **** | **** |
Expand Down
5 changes: 3 additions & 2 deletions crates/vent-rendering/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ impl VulkanBuffer {

unsafe { staging_buf.upload_data(memory, data, size) };

let command_buffer = begin_single_time_command(&instance.device, instance.command_pool);
let command_buffer =
begin_single_time_command(&instance.device, instance.global_command_pool);

unsafe {
let buffer_info = vk::BufferCopy::default().size(size);
Expand All @@ -89,7 +90,7 @@ impl VulkanBuffer {

end_single_time_command(
&instance.device,
instance.command_pool,
instance.global_command_pool,
instance.graphics_queue,
command_buffer,
);
Expand Down
6 changes: 3 additions & 3 deletions crates/vent-rendering/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl VulkanImage {
instance,
image,
&staging_buffer,
instance.command_pool,
instance.global_command_pool,
image_size,
1,
true,
Expand Down Expand Up @@ -137,7 +137,7 @@ impl VulkanImage {
instance,
image,
&staging_buffer,
instance.command_pool,
instance.global_command_pool,
image_size,
mip_level,
false,
Expand All @@ -147,7 +147,7 @@ impl VulkanImage {
Self::generate_mipmaps(
instance,
image,
instance.command_pool,
instance.global_command_pool,
image_size.width,
image_size.height,
mip_level,
Expand Down
59 changes: 42 additions & 17 deletions crates/vent-rendering/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pub struct VulkanInstance {
pub descriptor_set_layout: vk::DescriptorSetLayout,

pub render_pass: vk::RenderPass,
pub command_pool: vk::CommandPool,
pub global_command_pool: vk::CommandPool,
pub command_pools: Vec<vk::CommandPool>,
pub command_buffers: Vec<vk::CommandBuffer>,

pub image_available_semaphores: Vec<vk::Semaphore>,
Expand Down Expand Up @@ -215,9 +216,13 @@ impl VulkanInstance {
depth_image.image_view,
surface_resolution,
);
let command_pool = Self::create_command_pool(&device, graphics_queue_family_index);
let (global_command_pool, command_pools) = Self::create_command_pools(
&device,
graphics_queue_family_index,
swapchain_images.len(),
);
let command_buffers =
Self::allocate_command_buffers(&device, command_pool, frame_buffers.len() as u32);
Self::allocate_command_buffers(&device, &command_pools, swapchain_images.len());

let (
image_available_semaphores,
Expand Down Expand Up @@ -260,7 +265,8 @@ impl VulkanInstance {
depth_format,
depth_image,
frame_buffers,
command_pool,
global_command_pool,
command_pools,
command_buffers,
image_available_semaphores,
render_finished_semaphores,
Expand Down Expand Up @@ -629,24 +635,41 @@ impl VulkanInstance {
)
}

fn create_command_pool(device: &ash::Device, queue_family_index: u32) -> vk::CommandPool {
fn create_command_pools(
device: &ash::Device,
queue_family_index: u32,
count: usize,
) -> (vk::CommandPool, Vec<vk::CommandPool>) {
let create_info = vk::CommandPoolCreateInfo::default()
.queue_family_index(queue_family_index)
.flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER);
unsafe { device.create_command_pool(&create_info, None) }.unwrap()
.flags(vk::CommandPoolCreateFlags::TRANSIENT);
let global = unsafe { device.create_command_pool(&create_info, None) }.unwrap();

let mut pools: Vec<_> = Vec::with_capacity(count);
for _ in 0..count {
let create_info = vk::CommandPoolCreateInfo::default()
.queue_family_index(queue_family_index)
.flags(vk::CommandPoolCreateFlags::TRANSIENT);
pools.push(unsafe { device.create_command_pool(&create_info, None) }.unwrap());
}
(global, pools)
}

fn allocate_command_buffers(
device: &ash::Device,
command_pool: vk::CommandPool,
count: u32,
command_pools: &[vk::CommandPool],
count: usize,
) -> Vec<vk::CommandBuffer> {
let allocate_info = vk::CommandBufferAllocateInfo::default()
.command_pool(command_pool)
.level(vk::CommandBufferLevel::PRIMARY)
.command_buffer_count(count);

unsafe { device.allocate_command_buffers(&allocate_info) }.unwrap()
let mut buffers = Vec::with_capacity(count);
for pool in command_pools.iter().take(count) {
let allocate_info = vk::CommandBufferAllocateInfo::default()
.command_pool(*pool)
.level(vk::CommandBufferLevel::PRIMARY)
.command_buffer_count(1);

buffers.push(unsafe { device.allocate_command_buffers(&allocate_info) }.unwrap()[0]);
}
buffers
}

fn create_sync_objects(
Expand Down Expand Up @@ -897,9 +920,11 @@ impl Drop for VulkanInstance {
self.device
.destroy_descriptor_pool(self.descriptor_pool, None);

self.command_pools
.iter()
.for_each(|p| self.device.destroy_command_pool(*p, None));
self.device
.free_command_buffers(self.command_pool, &self.command_buffers);
self.device.destroy_command_pool(self.command_pool, None);
.destroy_command_pool(self.global_command_pool, None);

// DEVICE DESTRUCTION
self.device.destroy_device(None);
Expand Down
5 changes: 3 additions & 2 deletions crates/vent-rendering/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ impl Mesh3D {
)
};

let command_buffer = begin_single_time_command(&instance.device, instance.command_pool);
let command_buffer =
begin_single_time_command(&instance.device, instance.global_command_pool);

unsafe {
let buffer_info = vk::BufferCopy::default().size(vertex_size);
Expand All @@ -92,7 +93,7 @@ impl Mesh3D {

end_single_time_command(
&instance.device,
instance.command_pool,
instance.global_command_pool,
instance.graphics_queue,
command_buffer,
);
Expand Down
14 changes: 7 additions & 7 deletions crates/vent-runtime/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ impl RawRuntimeRenderer {

match image {
Ok((image_index, _)) => {
let command_buffer = instance.command_buffers[image_index as usize];
let command_pool = instance.command_pools[image_index as usize];
unsafe {
instance
.device
.reset_command_buffer(
command_buffer,
vk::CommandBufferResetFlags::RELEASE_RESOURCES,
)
.reset_command_pool(command_pool, vk::CommandPoolResetFlags::empty())
.unwrap();

let info = vk::CommandBufferBeginInfo::default();
}
let command_buffer = instance.command_buffers[image_index as usize];
unsafe {
let info = vk::CommandBufferBeginInfo::default()
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);

instance
.device
Expand Down

0 comments on commit 614f4c2

Please sign in to comment.