Skip to content

Add support for preMainLoop/postMainLoop functions - #22621

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:eventloop_register
Sep 26, 2024
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:eventloop_register

Conversation

@sbc100

@sbc100 sbc100 commented Sep 25, 2024

Copy link
Copy Markdown
Collaborator

Rather than having code from e.g SDL, etc, in event loop code use a callback registration system.

@sbc100
sbc100 requested a review from kripken September 25, 2024 20:57
Comment thread src/library_eventloop.js
clearInterval(id);
},

$registerPostMainLoop: (f) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe add a comment that as an optimization, this does not include MainLoop but instead checks for it at runtime and does nothing?

Separately, I wonder if we can do better than this. We could in theory add a "weak linking" mechanism, that would add some code only if MainLoop was actually included. Though if it's complicated it might not be worth it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sadly I don't think we have such a weak linking mechanism today. The way I did it here I think is the closest thing we have to weak linking in JS code.

Rather than having code from e.g SDL, etc, in event loop code use a
callback registration system.
Comment thread src/library_eventloop.js

$registerPostMainLoop: (f) => {
// Does nothing unless $MainLoop is included/used.
typeof MainLoop != 'undefined' && MainLoop.postMainLoop.push(f);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
typeof MainLoop != 'undefined' && MainLoop.postMainLoop.push(f);
MainLoop?.postMainLoop.push(f);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sadly I don't think the ? operator works like that (i.e. it doesn't work on top level things):

$ node
Welcome to Node.js v18.20.3.
Type ".help" for more information.
> a?.foo
Uncaught ReferenceError: a is not defined
> 

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Indeed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining:

Optional chaining cannot be used on a non-declared root object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh sorry. That is unfortunate, I wonder why it has that restriction.

@sbc100
sbc100 merged commit f508b43 into emscripten-core:main Sep 26, 2024
@sbc100
sbc100 deleted the eventloop_register branch September 26, 2024 23:52
juj added a commit to juj/emscripten that referenced this pull request Nov 30, 2026
With -sOFFSCREEN_FRAMEBUFFER, -pthread and
-sGL_SUPPORT_EXPLICIT_SWAP_CONTROL, a WebGL context that is proxied to
the main thread and created with explicitSwapControl=false is supposed to
be presented by the runtime at each main loop iteration. Three earlier
changes each broke a part of this:

- emscripten-core#22621 moved the swap out of the main loop runner into a hook on
  _emscripten_proxied_gl_context_activated_from_main_browser_thread, but
  attached it with a `__postjs` suffix. That is not a JS library
  decorator, so the hook was never emitted. The key was instead treated
  as a library symbol of its own, which showed up in builds that include
  the whole JS library (-sINCLUDE_FULL_LIBRARY, -sMAIN_MODULE=1). Use
  `__postset`.
- emscripten-core#22621 left the dependency on the native emscripten_webgl_commit_frame()
  behind on $setMainLoop, and emscripten-core#23855 then removed it from there as
  unused. Since then nothing has exported that function to JS for the
  hook to call. Make the hook depend on it. Also drop the hook's
  `$registerPreMainLoop` dependency: since emscripten-core#27995 it is detected
  automatically, and listing it is reported as redundant.
- emscripten-core#22157 narrowed the write of the explicitSwapControl flag in the
  context handle from i32 to i8 along with the EM_BOOL struct fields.
  The handle field is not an EM_BOOL: the hook reads it as an i32, and
  the WebGLContextHandle struct in webgl1.c declares it as uint32_t. The
  handle is malloc()ed, so the upper bytes were left uninitialized. Write
  it as an i32 again.

No test passed -sGL_SUPPORT_EXPLICIT_SWAP_CONTROL, and the one test of a
proxied implicit-swap context (test_emscripten_animate_canvas_element_size)
only checks the exit code. Add a browser test that reads back the canvas
to check that the frame was presented.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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