feat(renderer): integrate graphics pipeline and initialize gpu allocator with centralized frames in flight

This commit is contained in:
Serkyo 2026-05-10 22:56:14 +02:00
parent c7fc5de690
commit 8af89292b6
2 changed files with 55 additions and 20 deletions

View file

@ -1,17 +1,21 @@
mod device;
pub mod error; pub mod error;
mod instance; mod instance;
mod device; mod mesh;
mod pipeline;
mod renderer;
mod surface; mod surface;
mod swapchain; mod swapchain;
mod sync; mod sync;
mod renderer;
use std::ffi::c_char; pub const MAX_FRAMES_IN_FLIGHT: usize = 3;
use ash::{Entry, vk}; use ash::{Entry, vk};
use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use std::ffi::c_char;
pub use error::RendererError; pub use error::RendererError;
pub use renderer::{Renderer, MAX_FRAMES_IN_FLIGHT}; pub use renderer::Renderer;
impl Renderer { impl Renderer {
/// Initializes the Vulkan renderer. /// Initializes the Vulkan renderer.
@ -36,12 +40,15 @@ impl Renderer {
surface::create_surface(&entry, &instance, display_handle, window_handle)?; surface::create_surface(&entry, &instance, display_handle, window_handle)?;
// 3. Physical Device (GPU) // 3. Physical Device (GPU)
let physical_device = let physical_device = device::pick_physical_device(&instance, &surface_loader, surface)?;
device::pick_physical_device(&instance, &surface_loader, surface)?;
// 4. Graphics Queue Index // 4. Graphics Queue Index
let graphics_queue_index = let graphics_queue_index = device::find_graphics_queue_family(
device::find_graphics_queue_family(&instance, physical_device, &surface_loader, surface)?; &instance,
physical_device,
&surface_loader,
surface,
)?;
// 5. Logical Device and Queue // 5. Logical Device and Queue
let (device, graphics_queue) = let (device, graphics_queue) =
@ -49,7 +56,15 @@ impl Renderer {
// 6. Swapchain // 6. Swapchain
let (swapchain_loader, swapchain, swapchain_images, swapchain_format, swapchain_extent) = let (swapchain_loader, swapchain, swapchain_images, swapchain_format, swapchain_extent) =
swapchain::create_swapchain(&instance, physical_device, &device, &surface_loader, surface, width, height)?; swapchain::create_swapchain(
&instance,
physical_device,
&device,
&surface_loader,
surface,
width,
height,
)?;
// 7. Image Views // 7. Image Views
let image_views = let image_views =
@ -71,7 +86,24 @@ impl Renderer {
let command_buffers = unsafe { device.allocate_command_buffers(&alloc_info)? }; let command_buffers = unsafe { device.allocate_command_buffers(&alloc_info)? };
// 10. Synchronization Primitives // 10. Synchronization Primitives
let sync = sync::create_sync_primitives(&device, MAX_FRAMES_IN_FLIGHT, swapchain_images.len())?; let sync =
sync::create_sync_primitives(&device, MAX_FRAMES_IN_FLIGHT, swapchain_images.len())?;
let allocator_create_info = gpu_allocator::vulkan::AllocatorCreateDesc {
instance: instance.clone(),
device: device.clone(),
physical_device,
debug_settings: Default::default(),
buffer_device_address: false,
allocation_sizes: Default::default(),
};
let allocator = gpu_allocator::vulkan::Allocator::new(&allocator_create_info)
.expect("Failed to create Vulkan allocator");
let pipeline_layout = pipeline::create_pipeline_layout(&device);
let graphics_pipeline =
pipeline::create_graphics_pipeline(&device, pipeline_layout, swapchain_format);
Ok(Self { Ok(Self {
_entry: entry, _entry: entry,
@ -92,6 +124,9 @@ impl Renderer {
swapchain_image_views: image_views, swapchain_image_views: image_views,
command_pool, command_pool,
command_buffers, command_buffers,
allocator,
pipeline_layout,
graphics_pipeline,
sync, sync,
current_frame: 0, current_frame: 0,
}) })

View file

@ -3,7 +3,7 @@ use crate::sync::SyncPrimitives;
use ash::{Device, Instance, khr, vk}; use ash::{Device, Instance, khr, vk};
use gpu_allocator::vulkan::Allocator; use gpu_allocator::vulkan::Allocator;
pub const MAX_FRAMES_IN_FLIGHT: usize = 3;
/// The core renderer structure holding the Vulkan resources. /// The core renderer structure holding the Vulkan resources.
pub struct Renderer { pub struct Renderer {
@ -51,7 +51,7 @@ pub struct Renderer {
pub(crate) allocator: Allocator, pub(crate) allocator: Allocator,
/// Synchronization primitives for frame-by-frame execution. /// Synchronization primitives for frame-by-frame execution.
pub(crate) sync: SyncPrimitives, pub(crate) sync: SyncPrimitives,
/// Index of the current frame being processed (0 to MAX_FRAMES_IN_FLIGHT - 1). /// Index of the current frame being processed (0 to crate::MAX_FRAMES_IN_FLIGHT - 1).
pub(crate) current_frame: usize, pub(crate) current_frame: usize,
} }
@ -187,7 +187,7 @@ impl Renderer {
} }
// Advance the frame index for the next call // Advance the frame index for the next call
self.current_frame = (self.current_frame + 1) % MAX_FRAMES_IN_FLIGHT; self.current_frame = (self.current_frame + 1) % crate::MAX_FRAMES_IN_FLIGHT;
Ok(()) Ok(())
} }