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

Support for worker execution context #22

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

x0k
Copy link

@x0k x0k commented Feb 10, 2024

I initially experimented with OffscreenCanvas to allow compiling examples without modifying the game loop.

But now i realized that the execution of WASM code is treated like a normal function call (not a macro or micro tasks), so the worker thread will never handle user input, which is useless for many apps.
Also currently it is impossible to simply generate frames in a worker with OffscreenCanvas and bitmaprenderer context, as they don't work with 2d context.

So my experiment was failed, but there is a code to run lib in worker or main thread.

@rexim
Copy link
Member

rexim commented Feb 17, 2024

I don't get it. Is this some sort of working solution or just some bits of a failed experiment?

@x0k
Copy link
Author

x0k commented Feb 17, 2024

I don't get it. Is this some sort of working solution or just some bits of a failed experiment?

This is a working solution for running Raylib.js in worker

@rexim
Copy link
Member

rexim commented Feb 18, 2024

But does this allow to use original unmodified examples without the GameFrame() trick?

@x0k
Copy link
Author

x0k commented Feb 18, 2024

But does this allow to use original unmodified examples without the GameFrame() trick?

No, this pull request only about support for worker context.

But i think it is possible to do this in worker context.
I came up with the idea of using SharedArrayBuffer and maybe Atomics to implement the queue for passing events into worker context.
Also, as far as I remember, OffscreenCanvas with the transferToImageBitmap approach does not redraw the canvas in the main thread if the worker thread is blocked. So you need to generate and send the entire frame (possibly in the EndDrawing call) via postMessage.

I can continue to work specifically on this if needed.

@x0k
Copy link
Author

x0k commented Feb 23, 2024

I implemented my idea in a separate branch and here's what I realized:

  • performance degradation is there, at any settings (without GameFrame() trick)
  • different settings work differently on different machines and in different browsers
  • using Atomics.wait and a separate thread, you can load resources (images, fonts, ...) synchronously (relates to this problem)

Here is a demo https://x0k.github.io/raylib.js/ (I don't recommend checking implementations other than game frame on a stream due to the increased load)
Thanks to @AntonPieper for the GitHub pages hack with SharedArrayBuffer.

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.

2 participants