Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/lib/libhtml5_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,22 @@ var LibraryHtml5WebGL = {
// an integer pointer as a token value to represent the GL context activation from another thread. (when this function is called, the main browser thread
// has already accepted the GL context activation for our pthread, so that side is good)
#if GL_SUPPORT_EXPLICIT_SWAP_CONTROL
_emscripten_proxied_gl_context_activated_from_main_browser_thread__deps: ['$registerPreMainLoop'],
_emscripten_proxied_gl_context_activated_from_main_browser_thread__postjs: `
_emscripten_proxied_gl_context_activated_from_main_browser_thread__deps: ['emscripten_webgl_commit_frame'],
_emscripten_proxied_gl_context_activated_from_main_browser_thread__postset: `
// If the current GL context is a proxied regular WebGL context, and was
// initialized with implicit swap mode on the main thread, and we are on the
// parent thread, perform the swap on behalf of the user.
registerPreMainLoop(() => {
if (GL.currentContext && GL.currentContextIsProxied) {
var explicitSwapControl = {{{ makeGetValue('GL.currentContext', 0, 'i32') }}};
// For a proxied context, GL.currentContext is not a JS context object
// but the WebGLContextHandle pointer (see webgl1.c), whose first field
// is the explicitSwapControl flag.
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.

if (!explicitSwapControl) _emscripten_webgl_commit_frame();
}
});`,
#endif
_emscripten_proxied_gl_context_activated_from_main_browser_thread__sig: 'vp',
_emscripten_proxied_gl_context_activated_from_main_browser_thread: (contextHandle) => {
GLctx = Module['ctx'] = GL.currentContext = contextHandle;
GL.currentContextIsProxied = true;
Expand Down
60 changes: 60 additions & 0 deletions test/browser/webgl_proxied_implicit_swap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2026 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <GLES2/gl2.h>
#include <emscripten/em_asm.h>
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>

// Tests that a WebGL context that is proxied to the main thread and uses
// implicit swap (explicitSwapControl=false) gets presented on behalf of the
// application at each main loop iteration.
//
// A proxied context renders to an offscreen framebuffer, so nothing reaches
// the canvas unless the runtime calls emscripten_webgl_commit_frame() for us.

int frame = 0;

void tick(void) {
if (frame++ == 0) {
glClearColor(0, 0, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
return;
}

// The frame rendered above has been implicitly presented before this
// iteration started. Read back what is visible on the canvas.
MAIN_THREAD_EM_ASM({
var canvas = Module['canvas'];
var copy = document.createElement('canvas');
copy.width = canvas.width;
copy.height = canvas.height;
var ctx = copy.getContext('2d');
ctx.drawImage(canvas, 0, 0);
var pixel = ctx.getImageData(canvas.width >> 1, canvas.height >> 1, 1, 1).data;
out('canvas pixel: ' + Array.from(pixel));
assert(pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 255 && pixel[3] == 255,
'the frame should have been presented without an explicit commit');
});

emscripten_cancel_main_loop();
exit(0);
}

int main(void) {
EmscriptenWebGLContextAttributes attr;
emscripten_webgl_init_context_attributes(&attr);
assert(!attr.explicitSwapControl);
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
assert(ctx);
EMSCRIPTEN_RESULT res = emscripten_webgl_make_context_current(ctx);
assert(res == EMSCRIPTEN_RESULT_SUCCESS);

emscripten_set_main_loop(tick, 0, false);
return 99;
}
6 changes: 6 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4344,6 +4344,12 @@ def test_webgl_offscreen_framebuffer(self, version, threads):
args = ['-lGL', '-sOFFSCREEN_FRAMEBUFFER', '-DEXPLICIT_SWAP=1'] + threads + version
self.btest_exit('webgl_draw_triangle.c', cflags=args)

# Tests that a WebGL context that is proxied to the main thread and uses
# implicit swap gets presented at each main loop iteration.
@requires_graphics_hardware
def test_webgl_proxied_implicit_swap(self):
self.btest_exit('webgl_proxied_implicit_swap.c', cflags=['-lGL', '-pthread', '-sPROXY_TO_PTHREAD', '-sOFFSCREEN_FRAMEBUFFER', '-sGL_SUPPORT_EXPLICIT_SWAP_CONTROL'])

# Tests that VAOs can be used even if WebGL enableExtensionsByDefault is set to 0.
@requires_graphics_hardware
def test_webgl_vao_without_automatic_extensions(self):
Expand Down
Loading