vulkano-rs/vulkano

Lifetime issue when attempting to build ``AutoCommandBufferBuilder``

Closed this issue · 4 comments

Template

If you dont understand something just leave it.
If you can provide more detailed information than the template allows for, please ignore the template and present all of your findings.

  • Version of vulkano: 0.34.1
  • OS: NixOS 24
  • GPU (the selected PhysicalDevice): AMD Radeon RX 7800 XT (RADV NAVI32)
  • GPU Driver: amdgpu/mesa

Issue

I am attempting to use an AutoCommandBufferBuilder to construct an AutoCommandBuffer but run into a lifetime issue when calling the .build() function on the builder. The compiler gives the following error:

error[E0507]: cannot move out of `*auto_command_buffer_builder` which is behind a mutable reference
   --> src/vk_utils/command_utils.rs:62:16
    |
62  |         return auto_command_buffer_builder.build().unwrap_or_else(|err| {
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ------- `*auto_command_buffer_builder` moved due to this method call
    |                |
    |                move occurs because `*auto_command_buffer_builder` has type `AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>`, which does not implement the `Copy` trait
    |
note: `AutoCommandBufferBuilder::<PrimaryAutoCommandBuffer<A>, A>::build` takes ownership of the receiver `self`, which moves `*auto_command_buffer_builder`
   --> /builder.rs:344:18
    |
344 |     pub fn build(self) -> Result<Arc<PrimaryAutoCommandBuffer<A>>, Validated<VulkanError>> {
    |                  ^^^^

I do not quite understand how I am supposed to use the Builder instead. My code is as follows:

let mut builder = AutoCommandBufferBuilder::primary(
    &allocator,
    graphics_qf_index,
    CommandBufferUsage::OneTimeSubmit,
)
.unwrap_or_else(|err| {
    println!(
        "[create_command_buffer] Validation error when creating command buffer builder!"
    );
    panic!("{}", err.unwrap());
});

let auto_command_buffer_builder = builder
    .bind_pipeline_graphics(pipeline.clone())
    .unwrap()
    .begin_render_pass(
        RenderPassBeginInfo {
            clear_values: vec![Some([0.0, 0.0, 0.0, 1.0].into())],
            ..RenderPassBeginInfo::framebuffer(buf.clone())
        },
        SubpassBeginInfo {
            contents: SubpassContents::Inline,
            ..Default::default()
        },
    )
    .unwrap()
    .draw(3, 1, 0, 0)
    .unwrap()
    .end_render_pass(Default::default())
    .unwrap();

return auto_command_buffer_builder.build().unwrap_or_else(|err| {
    println!("[create_command_buffer] Validation error:");
    panic!("{}", err.unwrap());
});

I am attempting to follow This hopelessly outdated tutorial and This chapter from the vulkano book. In the book the build method is also just called, obtaining a command_buffer.
Any help would be appreciated!

-return auto_command_buffer_builder.build().unwrap_or_else(|err| {
+return builder.build().unwrap_or_else(|err| {

If you want to learn Rust, the book is an amazing resource. As for vulkano, we also have a book.

Wow. Thanks -_-

Hmm, did I say something wrong? :(

No absolutely not, thank you! I just realized that I should probably be sleeping instead of writing code since this is a really minor easy to fix error that was bugging me for hours now and I couldn't figure it out ^^