SDL icon indicating copy to clipboard operation
SDL copied to clipboard

[SDL2] Set EGL surface for foreign X11 windows

Open CasualPokePlayer opened this issue 1 year ago • 3 comments

Ensures the EGL surface is set for foreign X11 windows.

Description

Moves the EGL surface creation code over to SetupWindowData so it works for foreign X11 windows too.

Also slightly changes up the cleanup code for SetupWindowData, to avoid copy pasting.

Existing Issue(s)

#10611

CasualPokePlayer avatar Aug 31 '24 01:08 CasualPokePlayer

Thanks for the patch! What’s the use case for this?

slouken avatar Aug 31 '24 06:08 slouken

My use case is with the BizHawk project, which uses SDL2 for OpenGL functionality. A WinForms handle is wrapped using SDL_CreateWindowFrom, and SDL_HINT_VIDEO_FOREIGN_WINDOW_OPENGL is set so this foreign window can use SDL's OpenGL API.

On Mono, this WinForms handle will just be an X11 window id. As such, SDL would normally end up using GLX for OpenGL. However, this seems to end up being problematic in one very dumb case: Nvidia proprietary drivers under XWayland, which end up always getting a BadMatch error once glXMakeCurrent is called (done at the end of context creation by SDL, so SDL's GL context creation function ends up reporting a failure).

This error is even stranger as cursory googling indicates this typically happens with Nvidia's proprietary drivers if you request a pixel format with alpha present, and one without alpha works fine. However, BizHawk already does specify 0 alpha bits, so I would assume this is just some other driver bug. Also of course, this error is XWayland specific, it does not occur under a pure X11 DE.

I have found out I can end up working around this bug by simply using EGL rather than GLX. While I can easily do this with SDL2 at a glance with SDL_HINT_VIDEO_X11_FORCE_EGL, it simply doesn't work due to the foreign window usage, as the EGL surface ends up only created with SDL created windows. With this patch, foreign X11 windows can use EGL, and I've had users affected by the driver bug report BizHawk working fine applying this workaround.

CasualPokePlayer avatar Aug 31 '24 07:08 CasualPokePlayer

This looks reasonable; I'll need to update this for SDL3.

icculus avatar Dec 04 '24 19:12 icculus

Merging this for SDL2, and I'll open a bug to remember to cherry-pick this into SDL3 (but I don't want to do that before 3.2.0 ships).

icculus avatar Jan 16 '25 15:01 icculus