feat(renderer): create synchronization primitives

This commit is contained in:
Serkyo 2026-05-09 21:03:34 +02:00
parent d0fa203743
commit e005bf1c75

View file

@ -45,6 +45,12 @@ pub struct Renderer {
command_pool: vk::CommandPool,
/// The buffer used to record GPU commands.
command_buffer: vk::CommandBuffer,
/// Signaled when the swapchain has provided an image to render into.
image_available_semaphore: vk::Semaphore,
/// Signaled when rendering is complete and the image is ready for presentation.
render_finished_semaphore: vk::Semaphore,
/// Signaled when the GPU has finished executing the command buffer.
in_flight_fence: vk::Fence,
}
impl Renderer {
@ -116,7 +122,7 @@ impl Renderer {
};
let surface_loader = khr::surface::Instance::new(&entry, &instance);
// Pick a physical device that supports our surface
// Pick a physical device that supports the surface
let physical_device = Self::pick_physical_device(&instance, &surface_loader, surface)?;
// Find the graphics queue family (which must also support presentation)
@ -135,7 +141,7 @@ impl Renderer {
// Create the command pool
let pool_create_info = vk::CommandPoolCreateInfo::default()
.queue_family_index(graphics_queue_index)
// Allows us to reuse the command buffer every frame by resetting it
// Allows reusing the command buffer every frame by resetting it
.flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER);
let command_pool = unsafe { device.create_command_pool(&pool_create_info, None)? };
@ -148,6 +154,15 @@ impl Renderer {
let command_buffer = unsafe { device.allocate_command_buffers(&alloc_info)?[0] };
// Create semaphores and fence for synchronization
let semaphore_info = vk::SemaphoreCreateInfo::default();
let fence_info = vk::FenceCreateInfo::default()
.flags(vk::FenceCreateFlags::SIGNALED);
let image_available_semaphore = unsafe { device.create_semaphore(&semaphore_info, None)? };
let render_finished_semaphore = unsafe { device.create_semaphore(&semaphore_info, None)? };
let in_flight_fence = unsafe { device.create_fence(&fence_info, None)? };
Ok(Self {
_entry: entry,
instance,
@ -167,6 +182,9 @@ impl Renderer {
swapchain_image_views: image_views,
command_pool,
command_buffer,
image_available_semaphore,
render_finished_semaphore,
in_flight_fence,
})
}
@ -379,6 +397,11 @@ impl Drop for Renderer {
unsafe {
self.device.destroy_command_pool(self.command_pool, None);
// Destroy synchronization primitives
self.device.destroy_semaphore(self.image_available_semaphore, None);
self.device.destroy_semaphore(self.render_finished_semaphore, None);
self.device.destroy_fence(self.in_flight_fence, None);
// Destroy the swapchain
self.swapchain_loader.destroy_swapchain(self.swapchain, None);