bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Use safe window handles and `wgpu::Instance::create_surface`

Open ids1024 opened this issue 2 months ago • 10 comments

Objective

I noticed that https://github.com/bevyengine/bevy/pull/12978 introduces a reference counted wrapper around windows, but still uses RawWindowHandle and RawDisplayHandle, with wgpu::Instance::create_surface_unsafe. This can be changed easily enough to use wgpu::Instance::create_surface, and not use the raw handles anywhere.

Solution

Instead of containing an Arc<dyn _> along with raw handles, RawHandleWrapper now contains just an Arc<dyn _>, and ThreadLockedRawWindowHandleWrapper implements HasWindowHandle/HasDisplayHandle by calling those those methods on the trait object.

bevy_render uses wgpu::Instance::create_surface with the ThreadLockedRawWindowHandleWrapper. So this increments the reference count, but guarantees the window will outlive the wgpu::Instance.

Apparently we can just cast a Arc<W> to an Arc<dyn Trait>, so that makes WindowWrapper a little simpler too.

Testing

I'm not that familiar with Bevy, but examples seem to work, and this shouldn't break anything.

Migration Guide

RawHandleWrapper no longer has window_handle and display_handle fields. Instead, the get_handle() method has to be used.

ids1024 avatar May 02 '24 02:05 ids1024