-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Add proxy_to_pthread
option to platform=web
#79711
Add proxy_to_pthread
option to platform=web
#79711
Conversation
14fe417
to
4f94666
Compare
Woah! That's really amazing 💙 , I've been wanting to do this for a looooooong time! |
Thanks so much for taking this on!
There's some Javascript code elsewhere in Godot's code base, for example in these modules:
If you haven't checked already, it'd be good to double check that they don't also need |
Websocket and WebRTC changes are already in my PR. And for WebXR, a certain @dsnopek already added the |
Ack, sorry, not sure how I glossed over that :-) |
b165a84
to
e7a0e02
Compare
4d63e95
to
fe4b9c4
Compare
I added a demo in the PR description that shows my PR load successfully the editor and runs a game without blocking the main Javascript thread. |
5e3821f
to
7030c0c
Compare
That looks really cool, thanks for working on it! Some minor comments:
|
Once again, this is awesome 😻 . With
|
c27b2a9
to
d1863ed
Compare
025cb21
to
aae0592
Compare
7a35924
to
e17bd9c
Compare
01563fc
to
79b7f4f
Compare
da255d3
to
ab851c7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work 🚀 🥇 !
It seems that dlink + proxy_to_pthread is broken (needs further investigation), I suggest disabling it in that case but leave it on by default:
diff --git a/platform/web/detect.py b/platform/web/detect.py
index 4ad66d0b25..d306869889 100644
--- a/platform/web/detect.py
+++ b/platform/web/detect.py
@@ -216,6 +216,10 @@ def configure(env: "Environment"):
env.Append(LINKFLAGS=["-Wl,-u,scalbnf"])
if env["dlink_enabled"]:
+ if env["proxy_to_pthread"]:
+ print("GDExtension support requires proxy_to_pthread=no, disabling")
+ env["proxy_to_pthread"] = False
+
if cc_semver < (3, 1, 14):
print("GDExtension support requires emscripten >= 3.1.14, detected: %s.%s.%s" % cc_semver)
sys.exit(255)
I'm approving because all my other tests (including testing the 2d and 3d platformer demos) worked well.
Co-authored-by: Fabio Alessandrelli <[email protected]>
ab851c7
to
78c2a08
Compare
Thanks! |
This PR adds the option
proxy_to_pthread
to theweb
platform (withproxy_to_pthread=yes
as default value).The emscripten library compiles C/C++ apps to WASM. Unfortunately, WASM, when ran in the main thread, runs synchronously, making it block the main thread. That makes it so that if the application is in a computing intensive task, it will block the page main thread, affecting the UI and the usability of that page.
When compiled with this PR, users can use the web page (like entering text in a form) without any noticeable issues. Currently, it is impossible to write such text while the engine is loading. See that Firefox profiler as an example.
Emscripten offers a linker option to run the application in a web worker, effectively fixing the issue mentioned above. Web workers are the equivalent of threads for the web platform.
With my PR, if the user add the
do_no_block_main_thread=yes
option to it'splatform=web
scons build, the builder adds-s PROXY_TO_PTHREAD
to the linker options to create a stubmain()
function that will act as a proxy to the newly created main web worker.The Javascript C API ("platform/web/js/libs") had to be updated to specify the
__proxy
setting (now set tosync
). This is because otherwise, the web worker will use it's defaultModule
values that were initialized with the web worker. But all the data set before the initialization still reside in the main thread.Setting the value
sync
to each function__proxy
option makes it so that emscripten knows that the C API Javascript function must be run on the main thread instead of in the main web worker.Here's a demo. Don't mind the non-fluid mouse movement, it's related to the screen capture, it was super smooth while recording. But notice how the HTML UI react even if either the editor blocks the thread when loading (as usual) or while the game blocks the thread each frame for a whole second.
Capture.video.du.2023-07-29.14-31-18.webm
TL;DR Your game will still freeze under heavy processing on the main thread. But, the web page hosting the game will not freeze, as the Godot game will run in it's own thread instead of running on the web page's main thread.