Skip to content

Commit eb8afd5

Browse files
committed
Allow optional RawDisplayHandle in InstanceDescriptor
This allows platforms like GLES with EGL context backend to properly initialize such that their `EGLDisplay` and `EGLContext` are compatible with an incoming `Surface`, which is otherwise impossible if not for hotpatching the context (and loosing all resources created on it) when a surface comes in and that compositor connection becomes known. This hotpatching is known to break quite a few systems and initialization ordering (i.e. the alternative is forcing users to create surfaces before querying adapters, which isn't feasible on platform like Android), and is better avoided or at least made possible by using the `(Raw)DisplayHandle` abstraction provided by `raw_display_handle` in the way it's intended to be used (additionally, removing it from the surface would be a sensible decision). There may however be cases where users want to initialize multiple `Instance`s or perhaps `Adapter`s for independent `RawDisplayHandle`s, and certain backends may be designed specifically to cope with this (i.e. Vulkan). Note that this PR does nothing to address many other soundness problems inside the EGL backend.
1 parent 45e96e5 commit eb8afd5

File tree

15 files changed

+149
-263
lines changed

15 files changed

+149
-263
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pp-rs = "0.2.1"
165165
profiling = { version = "1.0.1", default-features = false }
166166
quote = "1.0.38"
167167
raw-window-handle = { version = "0.6.2", default-features = false }
168-
rwh_05 = { version = "0.5.2", package = "raw-window-handle" } # temporary compatibility for glutin-winit
168+
rwh_05 = { version = "0.5.2", package = "raw-window-handle" } # temporary compatibility for glutin-winit
169169
rayon = "1.3"
170170
regex-lite = "0.1"
171171
renderdoc-sys = "1"

deno_webgpu/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ impl GPU {
162162
gl: wgpu_types::GlBackendOptions::default(),
163163
noop: wgpu_types::NoopBackendOptions::default(),
164164
},
165+
display: None,
165166
},
166167
)));
167168
state.borrow::<Instance>()

examples/features/src/framework.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use wgpu::{Instance, Surface};
3+
use wgpu::{rwh::HasDisplayHandle, Instance, Surface};
44
use winit::{
55
dpi::PhysicalSize,
66
event::{Event, KeyEvent, StartCause, WindowEvent},
@@ -268,7 +268,10 @@ impl ExampleContext {
268268
async fn init_async<E: Example>(surface: &mut SurfaceWrapper, window: Arc<Window>) -> Self {
269269
log::info!("Initializing wgpu...");
270270

271-
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::from_env_or_default());
271+
let mut desc = wgpu::InstanceDescriptor::from_env_or_default();
272+
// XXX: Could also come from EventLoop before Window is created
273+
desc.display = Some(window.display_handle().unwrap().as_raw());
274+
let instance = wgpu::Instance::new(&desc);
272275
surface.pre_adapter(&instance, window);
273276

274277
let adapter = get_adapter_with_capabilities_or_from_env(

tests/src/init.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub fn initialize_instance(backends: wgpu::Backends, params: &TestParameters) ->
6868
// TODO(https://github.com/gfx-rs/wgpu/issues/7119): Enable noop backend?
6969
noop: wgpu::NoopBackendOptions::default(),
7070
},
71+
display: None,
7172
})
7273
}
7374

tests/tests/wgpu-validation/api/instance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod request_adapter_error {
4141
flags: wgpu::InstanceFlags::default(),
4242
memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(),
4343
backend_options: wgpu::BackendOptions::default(),
44+
display: None,
4445
}
4546
}
4647

wgpu-core/src/instance.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use alloc::{
88
};
99

1010
use hashbrown::HashMap;
11+
use raw_window_handle::DisplayHandle;
1112
use thiserror::Error;
1213
use wgt::error::{ErrorType, WebGpuError};
1314

@@ -131,6 +132,10 @@ impl Instance {
131132
flags: self.flags,
132133
memory_budget_thresholds: instance_desc.memory_budget_thresholds,
133134
backend_options: instance_desc.backend_options.clone(),
135+
display: instance_desc
136+
.display
137+
// XXX: This assigns a bogus lifetime. hal::InstanceDescriptor should also take Raw?
138+
.map(|r| unsafe { DisplayHandle::borrow_raw(r) }),
134139
};
135140

136141
use hal::Instance as _;

wgpu-hal/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ gles = [
112112
"dep:hashbrown",
113113
"dep:js-sys",
114114
"dep:khronos-egl",
115+
"dep:wayland-sys",
115116
"dep:libloading",
116117
"dep:log",
117118
"dep:ndk-sys",
@@ -245,6 +246,9 @@ renderdoc-sys = { workspace = true, optional = true }
245246
[target.'cfg(unix)'.dependencies]
246247
# Backend: Vulkan
247248
libc = { workspace = true, optional = true }
249+
# backend: GLES
250+
# XXX: Using crate to copy from glutin. Better yet, use wayland-client or replace entire backend with glutin entirely.
251+
wayland-sys = { version = "0.31", features = ["client", "dlopen", "egl"], optional = true }
248252

249253
#########################
250254
### Platform: Windows ###

wgpu-hal/examples/halmark/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,24 @@ struct Example<A: hal::Api> {
9393

9494
impl<A: hal::Api> Example<A> {
9595
fn init(window: &winit::window::Window) -> Result<Self, Box<dyn std::error::Error>> {
96+
// XXX: Could be from the EventLoop as well
97+
let raw_display_handle = window.display_handle()?;
98+
9699
let instance_desc = hal::InstanceDescriptor {
97100
name: "example",
98101
flags: wgpu_types::InstanceFlags::from_build_config().with_env(),
99102
memory_budget_thresholds: wgpu_types::MemoryBudgetThresholds::default(),
100103
// Can't rely on having DXC available, so use FXC instead
101104
backend_options: wgpu_types::BackendOptions::default(),
105+
display: Some(raw_display_handle),
102106
};
103107
let instance = unsafe { A::Instance::init(&instance_desc)? };
104108
let surface = {
105109
let raw_window_handle = window.window_handle()?.as_raw();
106-
let raw_display_handle = window.display_handle()?.as_raw();
107110

108111
unsafe {
109112
instance
110-
.create_surface(raw_display_handle, raw_window_handle)
113+
.create_surface(raw_display_handle.as_raw(), raw_window_handle)
111114
.unwrap()
112115
}
113116
};

wgpu-hal/examples/ray-traced-triangle/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ impl<A: hal::Api> Example<A> {
235235
log::info!("using index buffer")
236236
}
237237

238+
// XXX: Could be from the EventLoop as well
239+
let raw_display_handle = window.display_handle()?;
240+
238241
let instance_desc = hal::InstanceDescriptor {
239242
name: "example",
240243
flags: wgpu_types::InstanceFlags::default(),
@@ -245,15 +248,16 @@ impl<A: hal::Api> Example<A> {
245248
},
246249
..Default::default()
247250
},
251+
// XXX: Coudla
252+
display: Some(raw_display_handle),
248253
};
249254
let instance = unsafe { A::Instance::init(&instance_desc)? };
250255
let surface = {
251256
let raw_window_handle = window.window_handle()?.as_raw();
252-
let raw_display_handle = window.display_handle()?.as_raw();
253257

254258
unsafe {
255259
instance
256-
.create_surface(raw_display_handle, raw_window_handle)
260+
.create_surface(raw_display_handle.as_raw(), raw_window_handle)
257261
.unwrap()
258262
}
259263
};

0 commit comments

Comments
 (0)