Skip to content

Commit

Permalink
Pass timeout to acquire (#2503)
Browse files Browse the repository at this point in the history
* Pass timeout to acquire

* Changelog

* Fix examples

* Switch input order
  • Loading branch information
hakolao authored Mar 27, 2024
1 parent 93b6e94 commit 15f60f0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Changes to vulkano-shaders:

Changes to vulkano-util:
- `VulkanoWindowRenderer::acquire` now takes in an `FnOnce(&[Arc<ImageView>])`. This means that a closure can be called when the swapchain gets recreated.
- `VulkanoWindowRenderer::acquire` now also takes in `Option<Duration>` for the swapchain acquire timeout.

### Additions

Expand Down
23 changes: 12 additions & 11 deletions examples/interactive-fractal/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// - A simple `InputState` to interact with the application.

use crate::app::FractalApp;
use std::error::Error;
use std::{error::Error, time::Duration};
use vulkano::{image::ImageUsage, swapchain::PresentMode, sync::GpuFuture};
use vulkano_util::{
context::{VulkanoConfig, VulkanoContext},
Expand Down Expand Up @@ -145,16 +145,17 @@ fn compute_then_render(
target_image_id: usize,
) {
// Start the frame.
let before_pipeline_future = match renderer.acquire(|swapchain_image_views| {
app.place_over_frame
.recreate_framebuffers(swapchain_image_views)
}) {
Err(e) => {
println!("{e}");
return;
}
Ok(future) => future,
};
let before_pipeline_future =
match renderer.acquire(Some(Duration::from_millis(1)), |swapchain_image_views| {
app.place_over_frame
.recreate_framebuffers(swapchain_image_views)
}) {
Err(e) => {
println!("{e}");
return;
}
Ok(future) => future,
};

// Retrieve the target image.
let image = renderer.get_additional_image_view(target_image_id);
Expand Down
28 changes: 16 additions & 12 deletions examples/multi-window-game-of-life/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ mod render_pass;

use crate::app::{App, RenderPipeline};
use glam::{f32::Vec2, IVec2};
use std::{error::Error, time::Instant};
use std::{
error::Error,
time::{Duration, Instant},
};
use vulkano_util::renderer::VulkanoWindowRenderer;
use winit::{
event::{ElementState, Event, MouseButton, WindowEvent},
Expand Down Expand Up @@ -194,17 +197,18 @@ fn compute_then_render(
}

// Start the frame.
let before_pipeline_future = match window_renderer.acquire(|swapchain_image_views| {
pipeline
.place_over_frame
.recreate_framebuffers(swapchain_image_views)
}) {
Err(e) => {
println!("{e}");
return;
}
Ok(future) => future,
};
let before_pipeline_future =
match window_renderer.acquire(Some(Duration::from_millis(1)), |swapchain_image_views| {
pipeline
.place_over_frame
.recreate_framebuffers(swapchain_image_views)
}) {
Err(e) => {
println!("{e}");
return;
}
Ok(future) => future,
};

// Compute.
let after_compute = pipeline
Expand Down
12 changes: 6 additions & 6 deletions examples/triangle-util/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// that you want to learn Vulkan. This means that for example it won't go into details about what a
// vertex or a shader is.

use std::{error::Error, sync::Arc};
use std::{error::Error, sync::Arc, time::Duration};
use vulkano::{
buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage},
command_buffer::{
Expand Down Expand Up @@ -331,11 +331,11 @@ fn main() -> Result<(), impl Error> {

// Begin rendering by acquiring the gpu future from the window renderer.
let previous_frame_end = window_renderer
.acquire(|swapchain_images| {
// Whenever the window resizes we need to recreate everything dependent on
// the window size. In this example that includes
// the swapchain, the framebuffers and the dynamic
// state viewport.
.acquire(Some(Duration::from_millis(1)), |swapchain_images| {
// Whenever the window resizes we need to recreate everything dependent
// on the window size. In this example that
// includes the swapchain, the framebuffers
// and the dynamic state viewport.
framebuffers = window_size_dependent_setup(
swapchain_images,
render_pass.clone(),
Expand Down
5 changes: 3 additions & 2 deletions vulkano-util/src/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{context::VulkanoContext, window::WindowDescriptor};
use ahash::HashMap;
use std::sync::Arc;
use std::{sync::Arc, time::Duration};
use vulkano::{
device::{Device, Queue},
format::Format,
Expand Down Expand Up @@ -263,6 +263,7 @@ impl VulkanoWindowRenderer {
#[inline]
pub fn acquire(
&mut self,
timeout: Option<Duration>,
on_recreate_swapchain: impl FnOnce(&[Arc<ImageView>]),
) -> Result<Box<dyn GpuFuture>, VulkanError> {
// Recreate swap chain if needed (when resizing of window occurs or swapchain is outdated)
Expand All @@ -274,7 +275,7 @@ impl VulkanoWindowRenderer {

// Acquire next image in the swapchain
let (image_index, suboptimal, acquire_future) =
match swapchain::acquire_next_image(self.swapchain.clone(), None)
match swapchain::acquire_next_image(self.swapchain.clone(), timeout)
.map_err(Validated::unwrap)
{
Ok(r) => r,
Expand Down

0 comments on commit 15f60f0

Please sign in to comment.