gfx-hal-tutorials icon indicating copy to clipboard operation
gfx-hal-tutorials copied to clipboard

Upgrade to gfx-hal 0.7

Open faern opened this issue 3 years ago • 1 comments

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 avatar Mar 27 '21 17:03 faern

You need to change drop implementation here. https://github.com/mistodon/gfx-hal-tutorials/blob/7ad3c5436cf1cc51fa0a07fbbcb43c9248bd1ee6/src/bin/part-2-push-constants.rs#L297

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.

AngelicosPhosphoros avatar Apr 14 '21 23:04 AngelicosPhosphoros