Skip to content

Fix implicit swap of proxied WebGL contexts - #28455

Open
juj wants to merge 6 commits into
emscripten-core:mainfrom
juj:fix_proxied_gl_postset
Open

juj wants to merge 6 commits into
emscripten-core:mainfrom
juj:fix_proxied_gl_postset

Conversation

@juj

@juj juj commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

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:

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.

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>
_emscripten_proxied_gl_context_activated_from_main_browser_thread had no
__sig, so with -sMEMORY64 the context handle was stored in
GL.currentContext as a BigInt. The implicit swap hook, which is now
emitted, then failed with 'Cannot mix BigInt and other types' when
reading the explicitSwapControl field through it. Add a 'vp' signature
so the handle is converted to a Number.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/lib/libwebgl.js Outdated
#endif
#if GL_SUPPORT_EXPLICIT_SWAP_CONTROL
{{{ makeSetValue('handle', 0, 'webGLContextAttributes.explicitSwapControl', 'i8')}}};
{{{ makeSetValue('handle', 0, 'webGLContextAttributes.explicitSwapControl', 'i32')}}};

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.

grep shows other places still using i8 for this?

@kripken

kripken commented Dec 1, 2026

Copy link
Copy Markdown
Member

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.

Hmm, but grep shows

system/include/emscripten/html5_webgl.h:  bool explicitSwapControl;

So it is a bool there?

@juj

juj commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator Author

So it is a bool there?

The story with bool was that EM_BOOL used to be i8, but https://github.com/emscripten-core/emscripten/pull/22157/changes#diff-874f33563125619a7a5cb567ebe523c59258662d69858d78d24d947b275f9c6cR23-R24 changed it to a i32 (which was a breaking ABI change to users interacting with EM_BOOL signatures)

Then #22155 changed Emscripten away from using EM_BOOL into a plain bool, which is a also 4-byte field: #22155 (comment)

The regression to this PR happened at some point in those refactors I think.

@juj

juj commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

Or actually, no, it looks like it is a i8 field still. I'll update to that form.

Comment thread src/lib/libhtml5_webgl.js Outdated
#endif
#if GL_SUPPORT_EXPLICIT_SWAP_CONTROL
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.explicitSwapControl, 'c.attributes.explicitSwapControl', 'i8') }}};
{{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.explicitSwapControl, 'c.attributes.explicitSwapControl', 'i32') }}};

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.

There is also an access in

src/lib/libwebgl.js:      {{{ makeSetValue('handle', 0, 'webGLContextAttributes.explicitSwapControl', 'i8')}}};

which should change to i32.

Comment thread src/lib/libhtml5_webgl.js
registerPreMainLoop(() => {
if (GL.currentContext && GL.currentContextIsProxied) {
var explicitSwapControl = {{{ makeGetValue('GL.currentContext', 0, 'i32') }}};
var explicitSwapControl = {{{ makeGetValue('GL.currentContext', 0, 'i8') }}};

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.

I'm confused. Elsewhere in this file we do things like GL.currentContext.tempVertexBuffers1 which suggests GL.currentContext is an object. Is that not the case?

It looks like it is, as it is written to from GL.contexts entries, which are objects?

@juj juj Dec 1, 2026 •

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.

That is indeed a bit confusing by static reading.

What happens is that when GL.currentContextIsProxied is true, then GL.currentContext is not an actual GL context handle (since the proxy target thread owns it), but a pointer to a control block of that context, so a number. Updated this site with a comment.

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.

Now I see, thanks. But I see this:

typedef struct WebGLContextHandle {
  uint32_t explicit_swap_control;
  pthread_t owning_thread;
} WebGLContextHandle;

So shouldn't this be i32, not i8? From the PR description it sounds like the write is of i8, but if so, can we define the field in C to be an i8 too for consistency?

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.

Also, where is that write? I can't seem to find it by grepping.

ctx.drawImage(canvas, 0, 0);
var pixel = ctx.getImageData(canvas.width >> 1, canvas.height >> 1, 1, 1).data;
out('canvas pixel: ' + Array.from(pixel));
return pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 255 && pixel[3] == 255;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we just assert this line here in JS? Then if it crashes the debugger will show the correct location.

Also we can remove int presented completely.

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.

Updated

juj and others added 2 commits December 1, 2026 20:23
Keep writing the bool explicitSwapControl attribute as i8; writing it as
i32 at its unaligned offset would clobber the neighboring struct fields.

In the test, assert the canvas pixel directly in JS and remove the static
keywords.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ontexts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@juj
juj enabled auto-merge (squash) December 1, 2026 18:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants