vulkano-rs / vulkano

Safe and rich Rust wrapper around the Vulkan API
Apache License 2.0
4.48k stars 433 forks source link

Creating frame buffers for VulkanoWindowRenderer #2471

Closed coolcatcoder closed 6 months ago

coolcatcoder commented 6 months ago

How would I create frame buffers when the swapchain images stored as final_views in VulkanoWindowRenderer are private? Usually in my code, when the window size changed, I would recreate my frame buffers by looping through the swapchain images and create a frame buffer for every one. Something like this:

let framebuffers = swapchain_images
      .iter()
      .map(|swapchain_image| {
          let swapchain_image_view = ImageView::new_default(swapchain_image.clone()).unwrap();
          Framebuffer::new(
              render_pass.clone(),
              FramebufferCreateInfo {
                  attachments: vec![swapchain_image_view, depth_buffer_view.clone()],
                  ..Default::default()
              },
          )
          .unwrap()
      })
      .collect::<Vec<_>>();

(Fairly certain I stole this code from one of the examples.) How would I adapt the code to work with VulkanoWindowRenderer?

Edit: Thinking about this further, how would I even access the new images at all? Resize() only sets recreate_swapchain. Perhaps we could give some sort of closure for it to call when it recreates the swapchain?

Rua commented 6 months ago

You can look at some of Vulkano's examples that use vulkano-utils. Like this one: https://github.com/vulkano-rs/vulkano/blob/7b17d590a7908ed2ba08d8e64f70b75b528b5d23/examples/multi-window-game-of-life/main.rs#L105-L109

coolcatcoder commented 6 months ago

From looking at that specific example (and others), it seems to create a frame buffer every frame, if I understand correctly. How badly does this affect performance, if at all?

Rua commented 6 months ago

I agree that that is a bit of a silly thing to do. We should probably update the examples with something more sensible.

coolcatcoder commented 6 months ago

What would be a more sensible course of action, so that I can implement it in my code?

Rua commented 6 months ago

It should be created whenever the swapchain is (re)created, and then kept and reused.

coolcatcoder commented 6 months ago

Is it possible to find out when a window renderer recreates its swapchain? I couldn't figure out how to do that, if that is even possible.

coolcatcoder commented 6 months ago

Okay by ctrl-f-ing my way through the vulkano window renderer code, I think it only sets recreate_swapchain on .resize() and .set_present_mode(), then it only actually recreates the swapchain on acquisition of the future. So now I just need to somehow access final_views so I can recreate my framebuffers.

This seems like it could be simplified by perhaps storing a closure that gets passed final_views, that gets called on recreation of the swapchain.

Edit: This seems like a fairly simple change, and I've never contributed to anything before, so I'll try to create some code, and then I'll chuck it into a pull request.