vulkano-rs / vulkano

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

Lifetime issue when attempting to build ``AutoCommandBufferBuilder`` #2520

Closed CDaut closed 4 months ago

CDaut commented 4 months ago

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.

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!

marc0246 commented 4 months ago
-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.

CDaut commented 4 months ago

Wow. Thanks -_-

marc0246 commented 4 months ago

Hmm, did I say something wrong? :(

CDaut commented 4 months ago

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 ^^