Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable pthreads on Emscripten #17586

Merged
merged 26 commits into from
Feb 19, 2025

Conversation

JoeOsborn
Copy link
Contributor

@JoeOsborn JoeOsborn commented Feb 17, 2025

This patch eliminates the need for asyncify and uses modern filesystem APIs instead of the deprecated, unmaintained BrowserFS. I also added a new emscripten graphics backend for using webgl directly instead of (buggy) egl emulation.

Until this Emscripten patch lands in a release, the Makefile should be built with the default HAVE_EGL=1 (and PROXY_TO_PTHREAD=0). Once it's merged and we can update our emscripten version, HAVE_EGL=0 should be set and PROXY_TO_PTHREAD may be enabled (and ASYNC disabled).

There is a slowdown of maybe 20-30% in PROXY_TO_PTHREAD compared to ASYNC, which is due to proxied calls in openal.c and webgl_ctx. I'll submit fixes to those in future PRs; I've diagnosed the problem and have eliminated the slowdown in local testing by dummying out the relevant calls, but I'll have a real fix in the future.

The biggest issue is that local storage of users who were using the old version of the webplayer will be gone when they switch to the new webplayer. I don't have a good story for converting the old BrowserFS
IDBFS contents into the new OPFS filesystem (the move is worth doing because OPFS supports seeking and reading only bits of a file, and because BrowserFS is unmaintained). I think @BinBashBanana is looking at that.

I've kept around the old libretro webplayer under pkg/emscripten/libretro, and with the default make flags you can build a non-workerized RA that uses asyncify to sleep as before:

emmake make -f Makefile.emscripten LIBRETRO=$CORE 

The new player should be built for the pkg/emscripten/libretro-pthread directory:

emmake make -f Makefile.emscripten LIBRETRO=$CORE HAVE_WORKER=1 HAVE_WASMFS=1 PTHREAD=4 PROXY_TO_PTHREAD=1 HAVE_EGL=0 ASYNC=0

I think @BinBashBanana has some other quality of life fixes in development on top of this branch, but I think it's probably best to try and land this PR as-is now.

I've tested both make commands above and they produce working builds on Linux Firefox, Linux Chrome, and recent desktop Safari. I haven't tested other browsers or combinations.

This patch eliminates the need for asyncify and uses modern filesystem
APIs instead of the deprecated, unmaintained BrowserFS.

This is a WIP patch because it won't fully work until these two
Emscripten PRs land and are released:

emscripten-core/emscripten#23518
emscripten-core/emscripten#23021

The former fixes an offscreen canvas context recreation bug, and the
latter adds an equivalent to BrowserFS's XHR filesystem (but without
the hazardous running-XHR-on-the-main-thread problem).

The biggest issue is that local storage of users who were using the
old version of the webplayer will be gone when they switch to the new
webplayer.  I don't have a good story for converting the old BrowserFS
IDBFS contents into the new OPFS filesystem (the move is worth doing
because OPFS supports seeking and reading only bits of a file, and
because BrowserFS is dead).

I've kept around the old libretro webplayer under
pkg/emscripten/libretro-classic, and with these make flags you can
build a non-workerized RA that uses asyncify to sleep as before:

make -f Makefile.emscripten libretro=$CORE HAVE_WORKER=0 HAVE_WASMFS=0 PTHREAD=0 HAVE_AL=1

I also moved the default directory for core content on emscripten to
not be a subdirectory of the local filesystem mount, because it's
confusing to have a subdirectory that's lazily fetched and not
mirrored to the local storage.  I think it won't impact existing users
of the classic web player because they already have a retroarch.cfg in
place.
- Add http transfer support for emscripten
  - At the task_http level, not the net_http level --- so no netplay
    or webdav.
- Change default paths to be more like other platforms
- Gives us smaller bundles and a faster boot time
- Had to work around a task queue bug on Emscripten
  - I made the smallest possible change to do it, but it may be better
    to fix in rthread.c
If no ozone assets are present, load a libretro_minimal package
created using Emscripten's built-in file packager.
1. make PROXY_TO_PTHREAD=1 (slower)
2. make PROXY_TO_PTHREAD=0 (bad audio, because doesn't sleep in
openal.c)
also make input_driver check existence of drv->axis, drv->button
before calling them.
@JoeOsborn JoeOsborn marked this pull request as draft February 17, 2025 23:39
@JoeOsborn JoeOsborn marked this pull request as ready for review February 19, 2025 22:43
Copy link
Contributor

@BinBashBanana BinBashBanana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do a follow-up on this, but everything here right now looks good

@LibretroAdmin LibretroAdmin merged commit 56014a2 into libretro:master Feb 19, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants