Skip to content

Commit

Permalink
Update wgpu to 0.14.0, naga to 0.10.0, winit to 0.27.4, `raw-…
Browse files Browse the repository at this point in the history
…window-handle` to 0.5.0, `ndk` to 0.7 (bevyengine#6218)

# Objective

- Update `wgpu` to 0.14.0, `naga` to `0.10.0`, `winit` to 0.27.4, `raw-window-handle` to 0.5.0, `ndk` to 0.7.

## Solution

---

## Changelog

### Changed

- Changed `RawWindowHandleWrapper` to `RawHandleWrapper` which wraps both `RawWindowHandle` and `RawDisplayHandle`, which satisfies the `impl HasRawWindowHandle and HasRawDisplayHandle` that `wgpu` 0.14.0 requires.

- Changed `bevy_window::WindowDescriptor`'s `cursor_locked` to `cursor_grab_mode`, change its type from `bool` to `bevy_window::CursorGrabMode`.

## Migration Guide

- Adjust usage of `bevy_window::WindowDescriptor`'s `cursor_locked` to `cursor_grab_mode`, and adjust its type from `bool` to `bevy_window::CursorGrabMode`.
  • Loading branch information
VitalyAnkh authored and Pietrek14 committed Dec 17, 2022
1 parent efbaa83 commit a1be424
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 134 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_asset/Cargo.toml
Expand Up @@ -40,7 +40,7 @@ wasm-bindgen-futures = "0.4"
js-sys = "0.3"

[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = { version = "0.5" }
ndk-glue = { version = "0.7" }

[dev-dependencies]
futures-lite = "1.4.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_internal/Cargo.toml
Expand Up @@ -100,4 +100,4 @@ bevy_gilrs = { path = "../bevy_gilrs", optional = true, version = "0.9.0-dev" }
[target.'cfg(target_os = "android")'.dependencies]
# This version *must* be the same as the version used by winit,
# or Android will break: https://github.com/rust-windowing/winit#android
ndk-glue = {version = "0.5", features = ["logger"]}
ndk-glue = {version = "0.7", features = ["logger"]}
4 changes: 2 additions & 2 deletions crates/bevy_render/Cargo.toml
Expand Up @@ -49,9 +49,9 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" }
image = { version = "0.24", default-features = false }

# misc
wgpu = { version = "0.13.1", features = ["spirv"] }
wgpu = { version = "0.14.0", features = ["spirv"] }
codespan-reporting = "0.11.0"
naga = { version = "0.9.0", features = ["glsl-in", "spv-in", "spv-out", "wgsl-in", "wgsl-out"] }
naga = { version = "0.10.0", features = ["glsl-in", "spv-in", "spv-out", "wgsl-in", "wgsl-out"] }
serde = { version = "1", features = ["derive"] }
bitflags = "1.2.1"
smallvec = { version = "1.6", features = ["union", "const_generics"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Expand Up @@ -149,7 +149,7 @@ impl Plugin for RenderPlugin {

let surface = windows
.get_primary()
.and_then(|window| window.raw_window_handle())
.and_then(|window| window.raw_handle())
.map(|wrapper| unsafe {
let handle = wrapper.get_handle();
instance.create_surface(&handle)
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_render/src/view/window.rs
Expand Up @@ -6,7 +6,7 @@ use crate::{
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
use bevy_utils::{tracing::debug, HashMap, HashSet};
use bevy_window::{PresentMode, RawWindowHandleWrapper, WindowClosed, WindowId, Windows};
use bevy_window::{PresentMode, RawHandleWrapper, WindowClosed, WindowId, Windows};
use std::ops::{Deref, DerefMut};

/// Token to ensure a system runs on the main thread.
Expand Down Expand Up @@ -38,7 +38,7 @@ impl Plugin for WindowRenderPlugin {

pub struct ExtractedWindow {
pub id: WindowId,
pub raw_window_handle: Option<RawWindowHandleWrapper>,
pub raw_handle: Option<RawHandleWrapper>,
pub physical_width: u32,
pub physical_height: u32,
pub present_mode: PresentMode,
Expand Down Expand Up @@ -83,7 +83,7 @@ fn extract_windows(
.entry(window.id())
.or_insert(ExtractedWindow {
id: window.id(),
raw_window_handle: window.raw_window_handle(),
raw_handle: window.raw_handle(),
physical_width: new_width,
physical_height: new_height,
present_mode: window.present_mode(),
Expand Down Expand Up @@ -164,17 +164,16 @@ pub fn prepare_windows(
for window in windows
.windows
.values_mut()
// value of raw_winndow_handle only None if synthetic test
.filter(|x| x.raw_window_handle.is_some())
// value of raw_handle is only None in synthetic tests
.filter(|x| x.raw_handle.is_some())
{
let window_surfaces = window_surfaces.deref_mut();
let surface = window_surfaces
.surfaces
.entry(window.id)
.or_insert_with(|| unsafe {
// NOTE: On some OSes this MUST be called from the main thread.
render_instance
.create_surface(&window.raw_window_handle.as_ref().unwrap().get_handle())
render_instance.create_surface(&window.raw_handle.as_ref().unwrap().get_handle())
});

let swap_chain_descriptor = wgpu::SurfaceConfiguration {
Expand All @@ -197,6 +196,7 @@ pub fn prepare_windows(
PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync,
PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync,
},
alpha_mode: wgpu::CompositeAlphaMode::Auto,
};

// Do the initial surface configuration if it hasn't been configured yet. Or if size or
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_window/Cargo.toml
Expand Up @@ -21,7 +21,7 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.9.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" }
# Used for close_on_esc
bevy_input = { path = "../bevy_input", version = "0.9.0-dev" }
raw-window-handle = "0.4.2"
raw-window-handle = "0.5"

# other
serde = { version = "1.0", features = ["derive"], optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_window/src/lib.rs
@@ -1,12 +1,12 @@
#[warn(missing_docs)]
mod cursor;
mod event;
mod raw_window_handle;
mod raw_handle;
mod system;
mod window;
mod windows;

pub use crate::raw_window_handle::*;
pub use crate::raw_handle::*;
pub use cursor::*;
pub use event::*;
pub use system::*;
Expand Down
75 changes: 75 additions & 0 deletions crates/bevy_window/src/raw_handle.rs
@@ -0,0 +1,75 @@
use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};

/// A wrapper over [`RawWindowHandle`] and [`RawDisplayHandle`] that allows us to safely pass it across threads.
///
/// Depending on the platform, the underlying pointer-containing handle cannot be used on all threads,
/// and so we cannot simply make it (or any type that has a safe operation to get a [`RawWindowHandle`] or [`RawDisplayHandle`])
/// thread-safe.
#[derive(Debug, Clone)]
pub struct RawHandleWrapper {
pub window_handle: RawWindowHandle,
pub display_handle: RawDisplayHandle,
}

impl RawHandleWrapper {
/// Returns a [`HasRawWindowHandle`] + [`HasRawDisplayHandle`] impl, which exposes [`RawWindowHandle`] and [`RawDisplayHandle`].
///
/// # Safety
///
/// Some platforms have constraints on where/how this handle can be used. For example, some platforms don't support doing window
/// operations off of the main thread. The caller must ensure the [`RawHandleWrapper`] is only used in valid contexts.
pub unsafe fn get_handle(&self) -> ThreadLockedRawWindowHandleWrapper {
ThreadLockedRawWindowHandleWrapper(self.clone())
}

pub fn get_display_handle(&self) -> RawDisplayHandle {
self.display_handle
}

pub fn get_window_handle(&self) -> RawWindowHandle {
self.window_handle
}
}

// SAFETY: [`RawHandleWrapper`] is just a normal "raw pointer", which doesn't impl Send/Sync. However the pointer is only
// exposed via an unsafe method that forces the user to make a call for a given platform. (ex: some platforms don't
// support doing window operations off of the main thread).
// A recommendation for this pattern (and more context) is available here:
// https://github.com/rust-windowing/raw-window-handle/issues/59
unsafe impl Send for RawHandleWrapper {}
unsafe impl Sync for RawHandleWrapper {}

/// A [`RawHandleWrapper`] that cannot be sent across threads.
///
/// This safely exposes [`RawWindowHandle`] and [`RawDisplayHandle`], but care must be taken to ensure that the construction itself is correct.
///
/// This can only be constructed via the [`RawHandleWrapper::get_handle()`] method;
/// be sure to read the safety docs there about platform-specific limitations.
/// In many cases, this should only be constructed on the main thread.
pub struct ThreadLockedRawWindowHandleWrapper(RawHandleWrapper);

// SAFETY: the caller has validated that this is a valid context to get [`RawHandleWrapper`]
// as otherwise an instance of this type could not have been constructed
// NOTE: we cannot simply impl HasRawWindowHandle for RawHandleWrapper,
// as the `raw_window_handle` method is safe. We cannot guarantee that all calls
// of this method are correct (as it may be off the main thread on an incompatible platform),
// and so exposing a safe method to get a [`RawWindowHandle`] directly would be UB.
unsafe impl HasRawWindowHandle for ThreadLockedRawWindowHandleWrapper {
fn raw_window_handle(&self) -> RawWindowHandle {
self.0.get_window_handle()
}
}

// SAFETY: the caller has validated that this is a valid context to get [`RawDisplayHandle`]
// as otherwise an instance of this type could not have been constructed
// NOTE: we cannot simply impl HasRawDisplayHandle for RawHandleWrapper,
// as the `raw_display_handle` method is safe. We cannot guarantee that all calls
// of this method are correct (as it may be off the main thread on an incompatible platform),
// and so exposing a safe method to get a [`RawDisplayHandle`] directly would be UB.
unsafe impl HasRawDisplayHandle for ThreadLockedRawWindowHandleWrapper {
fn raw_display_handle(&self) -> RawDisplayHandle {
self.0.get_display_handle()
}
}
54 changes: 0 additions & 54 deletions crates/bevy_window/src/raw_window_handle.rs

This file was deleted.

0 comments on commit a1be424

Please sign in to comment.