From 8af89292b6b516ddbf5266164addde3a186fd518 Mon Sep 17 00:00:00 2001 From: Serkyo Date: Sun, 10 May 2026 22:56:14 +0200 Subject: [PATCH] feat(renderer): integrate graphics pipeline and initialize gpu allocator with centralized frames in flight --- crates/renderer/src/lib.rs | 69 +++++++++++++++++++++++++-------- crates/renderer/src/renderer.rs | 6 +-- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/crates/renderer/src/lib.rs b/crates/renderer/src/lib.rs index 4ec90a9..7929feb 100644 --- a/crates/renderer/src/lib.rs +++ b/crates/renderer/src/lib.rs @@ -1,17 +1,21 @@ +mod device; pub mod error; mod instance; -mod device; +mod mesh; +mod pipeline; +mod renderer; mod surface; mod swapchain; mod sync; -mod renderer; -use std::ffi::c_char; +pub const MAX_FRAMES_IN_FLIGHT: usize = 3; + use ash::{Entry, vk}; use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; +use std::ffi::c_char; pub use error::RendererError; -pub use renderer::{Renderer, MAX_FRAMES_IN_FLIGHT}; +pub use renderer::Renderer; impl Renderer { /// Initializes the Vulkan renderer. @@ -28,31 +32,42 @@ impl Renderer { let entry = unsafe { Entry::load() }?; // 1. Instance and Debug Messenger - let (instance, debug_utils, debug_messenger) = + let (instance, debug_utils, debug_messenger) = instance::create_instance(&entry, required_extensions)?; // 2. Surface - let (surface_loader, surface) = + let (surface_loader, surface) = surface::create_surface(&entry, &instance, display_handle, window_handle)?; // 3. Physical Device (GPU) - let physical_device = - device::pick_physical_device(&instance, &surface_loader, surface)?; + let physical_device = device::pick_physical_device(&instance, &surface_loader, surface)?; // 4. Graphics Queue Index - let graphics_queue_index = - device::find_graphics_queue_family(&instance, physical_device, &surface_loader, surface)?; - + let graphics_queue_index = device::find_graphics_queue_family( + &instance, + physical_device, + &surface_loader, + surface, + )?; + // 5. Logical Device and Queue - let (device, graphics_queue) = + let (device, graphics_queue) = device::create_logical_device(&instance, physical_device, graphics_queue_index)?; // 6. Swapchain - let (swapchain_loader, swapchain, swapchain_images, swapchain_format, swapchain_extent) = - swapchain::create_swapchain(&instance, physical_device, &device, &surface_loader, surface, width, height)?; + let (swapchain_loader, swapchain, swapchain_images, swapchain_format, swapchain_extent) = + swapchain::create_swapchain( + &instance, + physical_device, + &device, + &surface_loader, + surface, + width, + height, + )?; // 7. Image Views - let image_views = + let image_views = swapchain::create_image_views(&device, &swapchain_images, swapchain_format)?; // 8. Command Pool @@ -71,7 +86,24 @@ impl Renderer { let command_buffers = unsafe { device.allocate_command_buffers(&alloc_info)? }; // 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 { _entry: entry, @@ -92,8 +124,11 @@ impl Renderer { swapchain_image_views: image_views, command_pool, command_buffers, + allocator, + pipeline_layout, + graphics_pipeline, sync, current_frame: 0, }) } -} \ No newline at end of file +} diff --git a/crates/renderer/src/renderer.rs b/crates/renderer/src/renderer.rs index 985d7e6..3fc771e 100644 --- a/crates/renderer/src/renderer.rs +++ b/crates/renderer/src/renderer.rs @@ -3,7 +3,7 @@ use crate::sync::SyncPrimitives; use ash::{Device, Instance, khr, vk}; use gpu_allocator::vulkan::Allocator; -pub const MAX_FRAMES_IN_FLIGHT: usize = 3; + /// The core renderer structure holding the Vulkan resources. pub struct Renderer { @@ -51,7 +51,7 @@ pub struct Renderer { pub(crate) allocator: Allocator, /// Synchronization primitives for frame-by-frame execution. 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, } @@ -187,7 +187,7 @@ impl Renderer { } // 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(()) }