From 3c51a3eb0b26c1b758714042617303640ca7f771 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Sun, 21 May 2017 19:04:14 +0200 Subject: [PATCH] Update the examples to use the new system --- examples/src/bin/image.rs | 10 ++++------ examples/src/bin/teapot.rs | 12 +++++------- examples/src/bin/triangle.rs | 21 +++++++++++---------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/examples/src/bin/image.rs b/examples/src/bin/image.rs index 3e82bbe4e..931f647e5 100644 --- a/examples/src/bin/image.rs +++ b/examples/src/bin/image.rs @@ -164,12 +164,10 @@ fn main() { vulkano::framebuffer::Framebuffer::new(renderpass.clone(), dimensions, attachments).unwrap() }).collect::>(); - let mut submissions: Vec> = Vec::new(); + let mut previous_frame_end = Box::new(vulkano::sync::now(device.clone())) as Box; loop { - while submissions.len() >= 4 { - submissions.remove(0); - } + previous_frame_end.cleanup_finished(); let (image_num, future) = swapchain.acquire_next_image(Duration::new(10, 0)).unwrap(); @@ -187,11 +185,11 @@ fn main() { .end_render_pass().unwrap() .build().unwrap(); - let future = future + let future = previous_frame_end.join(future) .then_execute(queue.clone(), cb).unwrap() .then_swapchain_present(queue.clone(), swapchain.clone(), image_num) .then_signal_fence_and_flush().unwrap(); - submissions.push(Box::new(future) as Box<_>); + previous_frame_end = Box::new(future) as Box<_>; let mut done = false; events_loop.poll_events(|ev| { diff --git a/examples/src/bin/teapot.rs b/examples/src/bin/teapot.rs index 42422c5c8..0e0222d98 100644 --- a/examples/src/bin/teapot.rs +++ b/examples/src/bin/teapot.rs @@ -161,12 +161,10 @@ fn main() { }).collect::>(); - let mut submissions: Vec> = Vec::new(); + let mut previous_frame = Box::new(vulkano::sync::now(device.clone())) as Box; loop { - while submissions.len() >= 4 { - submissions.remove(0); - } + previous_frame.cleanup_finished(); { // aquiring write lock for the uniform buffer @@ -179,7 +177,7 @@ fn main() { buffer_content.world = cgmath::Matrix4::from(rotation).into(); } - let (image_num, future) = swapchain.acquire_next_image(std::time::Duration::new(1, 0)).unwrap(); + let (image_num, acquire_future) = swapchain.acquire_next_image(std::time::Duration::new(1, 0)).unwrap(); let command_buffer = vulkano::command_buffer::AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap() .begin_render_pass( @@ -193,11 +191,11 @@ fn main() { .end_render_pass().unwrap() .build().unwrap(); - let future = future + let future = previous_frame.join(acquire_future) .then_execute(queue.clone(), command_buffer).unwrap() .then_swapchain_present(queue.clone(), swapchain.clone(), image_num) .then_signal_fence_and_flush().unwrap(); - submissions.push(Box::new(future) as Box<_>); + previous_frame = Box::new(future) as Box<_>; let mut done = false; events_loop.poll_events(|ev| { diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index cdb3885ec..7d07a97aa 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -55,6 +55,7 @@ use vulkano::pipeline::viewport::Viewport; use vulkano::pipeline::viewport::Scissor; use vulkano::swapchain::SurfaceTransform; use vulkano::swapchain::Swapchain; +use vulkano::sync::now; use vulkano::sync::GpuFuture; use std::sync::Arc; @@ -348,15 +349,15 @@ fn main() { // they are in use by the GPU. // // Destroying the `GpuFuture` blocks until the GPU is finished executing it. In order to avoid - // that, we store them in a `Vec` and clean them from time to time. - let mut submissions: Vec> = Vec::new(); + // that, we store the submission of the previous frame here. + let mut previous_frame_end = Box::new(now(device.clone())) as Box; loop { - // Clearing the old submissions by keeping alive only the ones which probably aren't - // finished. - while submissions.len() >= 4 { - submissions.remove(0); - } + // It is important to call this function from time to time, otherwise resources will keep + // accumulating and you will eventually reach an out of memory error. + // Calling this function polls various fences in order to determine what the GPU has + // already processed, and frees the resources that are no longer needed. + previous_frame_end.cleanup_finished(); // Before we can draw on the output, we have to *acquire* an image from the swapchain. If // no image is available (which happens if you submit draw commands too quickly), then the @@ -365,7 +366,7 @@ fn main() { // // This function can block if no image is available. The parameter is a timeout after // which the function call will return an error. - let (image_num, future) = swapchain.acquire_next_image(Duration::new(1, 0)).unwrap(); + let (image_num, acquire_future) = swapchain.acquire_next_image(Duration::new(1, 0)).unwrap(); // In order to draw, we have to build a *command buffer*. The command buffer object holds // the list of commands that are going to be executed. @@ -404,7 +405,7 @@ fn main() { // Finish building the command buffer by calling `build`. .build().unwrap(); - let future = future + let future = previous_frame_end.join(acquire_future) .then_execute(queue.clone(), command_buffer).unwrap() // The color output is now expected to contain our triangle. But in order to show it on @@ -415,7 +416,7 @@ fn main() { // the GPU has finished executing the command buffer that draws the triangle. .then_swapchain_present(queue.clone(), swapchain.clone(), image_num) .then_signal_fence_and_flush().unwrap(); - submissions.push(Box::new(future) as Box<_>); + previous_frame_end = Box::new(future) as Box<_>; // Note that in more complex programs it is likely that one of `acquire_next_image`, // `command_buffer::submit`, or `present` will block for some time. This happens when the