Skip to content

Conversation

MarijnS95
Copy link

Depends on: #20357
Depends on: gfx-rs/wgpu#8012

Objective

Solution

Multiple community members have asked me to look into persistent BadDisplay crashes on wgpu when using its EGL backend in conjunction with Wayland. Besides realizing that this backend is quite literally the definition of Undefined Behaviour itself, various hacks to patch the EGL context - which is compositor-specific - inside the Instance/Adapter with the actual wl_display handle as soon as a surface is created will not only void any previously created GL resources, only the Instance handles are patched leaving any previously queried Adapters to reference destroyed EGL objects causing those BadDisplay errors.

Solving that handle lifetime/ownership problem has yet to be done (and is why crates like glutin exist...), but at the very least we might as well create an EGLDisplay and EGLContext which is compatible with the current compositor from the get-go, which is what gfx-rs/wgpu#8012 allows callers to do. This is one of the reasons why raw-display-handle split out a RawDisplayHandle type and related enums: to know up-front what compositor is used (telling X11 and Wayland apart, if both could be used concurrently instead of letting wgpu "guess") and to use the same wl_display compositor connection as winit.

There are alternatives available, which is why this and the related wgpu PR is a draft to shake out some feedback. Anything that's involving more complexity inside wgpu's EGL backend should be attempted incredibly cautiously if it weren't for a full rewrite on top of a higher-level EGL abstraction.

Testing

  • WGPU_BACKEND=gl cargo r --example 3d_bloom -Fwayland -Fbevy_render/decoupled_naga on a Wayland compositor.

@MarijnS95 MarijnS95 changed the title Egl wayland bevy_render: Pass DisplayHandle to wgpu for compositor-aware Instance creation Jul 31, 2025
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen A-Windowing Platform-agnostic interface layer to run your app in D-Complex Quite challenging from either a design or technical perspective. Ask for help! S-Needs-Design This issue requires design work to think about how it would best be accomplished labels Aug 3, 2025
@MarijnS95
Copy link
Author

MarijnS95 commented Aug 6, 2025

@alice-i-cecile just curious why this PR, which is effectively a one-line change of code on top of dependent PR #20357 is marked D-Complex Quite challenging from either a design or technical perspective. Ask for help! , while the large/controversial refactor in that PR is D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes . Shouldn't that be tagged the opposite way around?

Edit: this likely needs O-OpenGL Specific to the OpenGL render API too since it addresses a crash specific to using wgpu's GLES backend on Wayland.

@alice-i-cecile alice-i-cecile added D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes O-OpenGL Specific to the OpenGL render API and removed D-Complex Quite challenging from either a design or technical perspective. Ask for help! labels Aug 6, 2025
@alice-i-cecile
Copy link
Member

Very reasonable; I've amended the labels :)

Long ago `raw-window-handle` and `winit` split out a `RawDisplayHandle`
type and respective traits for dealing with the connection - or at the
very least type - of a compositor, and typically implement this for a
`Window` directly.

`wgpu` and `bevy` seem to have caught on to the latter and folded
the two traits/types together because `Window` provides it, but miss
a critical goal here: that `(Raw)DisplayHandle` is important for
initialization of certain graphics APIs. In the case of Wayland all
resources are unique per connection, and in general for others
it's important to distinguish between Wayland and X11 if `winit` chose
one over the other, even if both are available; currently that's just
guesswork inside `wgpu`.

Newer APIs like Vulkan don't suffer from this, but older graphics APIs
like OpenGL and specifically the EGL backend (or GLX for X11) set up
their entire state based on the compositor connection (alternatives are
available) even if it's ultimately "only" important for the surface that
is going to be rendered into.

Note that I haven't yet checked all the safety constraints carefully
in this PR; some existing messages seem flawed but I need to perform a
clean audit from scratch to denote what limitations should apply to the
newly proposed `RawDisplayHandleWrapper` as well.
…tance` creation

Multiple community members have asked me to look into persistent
`BadDisplay` crashes on `wgpu` when using its EGL backend in
conjunction with Wayland.  Besides realizing that this backend is
quite literally the definition of Undefined Behaviour itself, various
hacks to patch the EGL context - which is compositor-specific - inside
the `Instance`/`Adapter` with the actual `wl_display` handle as soon
as a `surface` is created will not only void any previously created
GL resources, only the `Instance` handles are patched leaving any
previously queried `Adapter`s to reference destroyed EGL objects causing
those `BadDisplay` errors.

Solving that handle lifetime/ownership problem has yet to be done
(and is why crates like `glutin` exist...), but at the very least
we might as well create an `EGLDisplay` and `EGLContext` which is
compatible with the current compositor from the get-go, which is
what gfx-rs/wgpu#8012 allows callers to
do.  This is one of the reasons why `raw-display-handle` split out
a `RawDisplayHandle` type and related enums: to know up-front what
compositor is used (telling X11 and Wayland apart, if both could be
used concurrently instead of letting `wgpu` "guess") and to use the same
`wl_display` compositor connection as `winit`.

There are alternatives available, which is why this and the related
`wgpu` PR is a draft to shake out some feedback.  Anything that's
involving more complexity inside `wgpu`'s EGL backend should be
attempted incredibly cautiously if it weren't for a full rewrite on top
of a higher-level EGL abstraction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen A-Windowing Platform-agnostic interface layer to run your app in D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes O-OpenGL Specific to the OpenGL render API S-Needs-Design This issue requires design work to think about how it would best be accomplished
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Crash due to wgpu during startup on Wayland REGRESSION: wgpu panic BadDisplay after winit 0.30 on Wayland OpenGL
2 participants