Skip to content

Commit

Permalink
Fix issue with swapchain helper allocating excessive amount of sync p…
Browse files Browse the repository at this point in the history
…rimitives and returning a rotating image
  • Loading branch information
aclysma committed Apr 10, 2024
1 parent 8eb2f77 commit 295c336
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
2 changes: 0 additions & 2 deletions rafx-api/src/backends/vulkan/swapchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use crate::*;
use ash::vk::Extent2D;
use std::mem::ManuallyDrop;

pub const MAX_FRAMES_IN_FLIGHT: usize = 2;

/// Used to select which PresentMode is preferred. Some of this is hardware/platform dependent and
/// it's a good idea to read the Vulkan spec.
///
Expand Down
24 changes: 8 additions & 16 deletions rafx-api/src/extra/swapchain_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ impl RafxSwapchainHelperSharedState {
device_context: &RafxDeviceContext,
swapchain: Arc<Mutex<RafxSwapchain>>,
) -> RafxResult<Self> {
let image_count = swapchain.lock().unwrap().image_count();
let mut image_available_semaphores = Vec::with_capacity(image_count);
let mut render_finished_semaphores = Vec::with_capacity(image_count);
let mut in_flight_fences = Vec::with_capacity(image_count);
let sync_primitive_count = crate::MAX_FRAMES_IN_FLIGHT + 1;
let mut image_available_semaphores = Vec::with_capacity(sync_primitive_count);
let mut render_finished_semaphores = Vec::with_capacity(sync_primitive_count);
let mut in_flight_fences = Vec::with_capacity(sync_primitive_count);

for _ in 0..image_count {
for _ in 0..sync_primitive_count {
image_available_semaphores.push(device_context.create_semaphore()?);
render_finished_semaphores.push(device_context.create_semaphore()?);
in_flight_fences.push(device_context.create_fence()?);
Expand Down Expand Up @@ -89,9 +89,9 @@ pub struct RafxPresentableFrame {

impl RafxPresentableFrame {
/// An index that starts at 0 on the first present and increments every frame, wrapping back to
/// 0 after each swapchain image has been presented once. (See image_count on
/// RafxSwapchainHelper). WARNING: This is not always the returned swapchain image. Swapchain
/// images may be acquired in any order.
/// 0 after reporting MAX_FRAMES_IN_FLIGHT + 1. This is a convenience feature for cases where
/// a resource needs to be allocated and preserved for each frame in flight. For example,
/// delaying deallocation of memory until it is no longer in use by the GPU.
pub fn rotating_frame_index(&self) -> usize {
// The sync_frame_index can be used as-is for this purpose
self.sync_frame_index
Expand Down Expand Up @@ -224,7 +224,6 @@ pub struct RafxSwapchainHelper {
format: RafxFormat,
color_space: RafxSwapchainColorSpace,
swapchain_def: RafxSwapchainDef,
image_count: usize,

// False initially, set to true when we produce the first presentable frame to indicate that
// future frames need to wait for its result to be sent via the result_tx/result_rx channel
Expand All @@ -239,7 +238,6 @@ impl RafxSwapchainHelper {
) -> RafxResult<Self> {
let format = swapchain.format();
let color_space = swapchain.color_space();
let image_count = swapchain.image_count();
let swapchain_def = swapchain.swapchain_def().clone();

let shared_state = Arc::new(RafxSwapchainHelperSharedState::new(
Expand All @@ -257,7 +255,6 @@ impl RafxSwapchainHelper {
shared_state: Some(shared_state),
format,
color_space,
image_count,
swapchain_def,
expect_result_from_previous_frame: false,
})
Expand Down Expand Up @@ -317,10 +314,6 @@ impl RafxSwapchainHelper {
self.color_space
}

pub fn image_count(&self) -> usize {
self.image_count
}

pub fn swapchain_def(&self) -> &RafxSwapchainDef {
&self.swapchain_def
}
Expand Down Expand Up @@ -530,7 +523,6 @@ impl RafxSwapchainHelper {
}

self.format = swapchain.format();
self.image_count = swapchain.image_count();
self.swapchain_def = swapchain_def;
}

Expand Down
3 changes: 3 additions & 0 deletions rafx-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ pub const MAX_DESCRIPTOR_SET_LAYOUTS: usize = 4;
pub const MAX_RENDER_TARGET_ATTACHMENTS: usize = 8;
// Vulkan guarantees up to 16
pub const MAX_VERTEX_INPUT_BINDINGS: usize = 16;
// The vast, vast majority of usecases will have the GPU processing either one or two frames
// simultaneously
pub const MAX_FRAMES_IN_FLIGHT: usize = 2;

//
// Exported public API
Expand Down
2 changes: 1 addition & 1 deletion rafx-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ pub use rafx_api::RafxResult;

mod shaders;

pub const MAX_FRAMES_IN_FLIGHT: usize = 2;
pub use rafx_api::MAX_FRAMES_IN_FLIGHT;

0 comments on commit 295c336

Please sign in to comment.