Skip to content

Conversation

faern
Copy link

@faern faern commented Mar 27, 2021

Thanks a lot for your tutorial!

gfx-hal 0.7 has been released and I would like to learn the newest API. I could not find any tutorial dealing with the 0.7 branch specifically. So I tried to upgrade your tutorial code.

Warning: ! This is mostly a bunch of monkey patching and me not knowing what I'm doing ! I just experimented and took inspiration from the examples in the gfx-hal repository. This compiles and runs on Linux (vulkan backend). But it segfaults on exit, so the upgrade is definitely not correct!

I don't think this should be merged as is. Or maybe never. But it can help others who come here also looking for gfx-hal 0.7 code. And if you would like to upgrade your tutorial, this might be a start(?). Also, if you want to, I'd love feedback on where I'm screwing up the upgrade :)

@faern faern changed the title Upgrade dependencies Upgrade to gfx-hal 0.7 Mar 27, 2021
@AngelicosPhosphoros
Copy link

AngelicosPhosphoros commented Apr 14, 2021

You need to change drop implementation here.

I hacked this tutorial a bit ago and finished with this destructor which doesn't segfaults.

impl<B: gfx_hal::Backend> Drop for ResourceHolder<B> {
    fn drop(&mut self) {
        use gfx_hal::device::Device;
        use gfx_hal::window::PresentationSurface;
        use gfx_hal::Instance;

        // Saving instance because double free or something like this
        // occurs if it dropped before other fields.
        let _instance = unsafe {
            // We are moving the `Resources` out of the struct...
            let Resources {
                instance,
                mut surface,
                device,
                command_pool,
                render_passes,
                pipeline_layouts,
                pipelines,
                submission_complete_fence,
                rendering_complete_semaphore,
                // This things dropped automatically but PRIOR to instance.
                command_buffer: _command_buffer,
                adapter: _adapter,
                queue_group: _queue_group,
                surface_color_format: _surface_color_format,
                surface_extent: _surface_extent,
            } = ManuallyDrop::take(&mut self.res);

            // ... and destroying them individually:
            device.destroy_semaphore(rendering_complete_semaphore);
            device.destroy_fence(submission_complete_fence);
            for pipeline in pipelines {
                device.destroy_graphics_pipeline(pipeline);
            }
            for pipeline_layout in pipeline_layouts {
                device.destroy_pipeline_layout(pipeline_layout);
            }
            for render_pass in render_passes {
                device.destroy_render_pass(render_pass);
            }
            device.destroy_command_pool(command_pool);
            surface.unconfigure_swapchain(&device);
            instance.destroy_surface(surface);
            instance
        };
    }
}

As you see, you need drop everything prior to instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants