Skip to content

Commit

Permalink
Move emscripten_webgl_init_context_attributes to native code. NFC (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Jan 29, 2024
1 parent 4eb71a2 commit cfdc30e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 50 deletions.
25 changes: 0 additions & 25 deletions src/library_html5_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,6 @@ var LibraryHtml5WebGL = {
return len;
},

// Execute in calling thread without proxying needed.
emscripten_webgl_init_context_attributes: (attributes) => {
#if ASSERTIONS
assert(attributes);
#endif
var a = attributes >> 2;
for (var i = 0; i < ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.__size__ }}}>>2); ++i) {
HEAP32[a+i] = 0;
}

HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.alpha }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.depth }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.antialias }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.premultipliedAlpha }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.majorVersion }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.enableExtensionsByDefault }}}>>2)] = 1;

#if PTHREADS
// Default context initialization state (user can override):
// - if main thread is creating the context, default to the context not being shared between threads - enabling sharing has performance overhead, because it forces the context to be OffscreenCanvas or OffscreenFramebuffer.
// - if a web worker is creating the context, default to using OffscreenCanvas if available, or proxying via Offscreen Framebuffer if not
if (ENVIRONMENT_IS_WORKER) {{{ makeSetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.proxyContextToMainThread, 1/*EMSCRIPTEN_WEBGL_CONTEXT_PROXY_FALLBACK*/, 'i32') }}};
#endif
},

$emscripten_webgl_power_preferences: "['default', 'low-power', 'high-performance']",

#if PTHREADS && OFFSCREEN_FRAMEBUFFER
Expand Down
1 change: 0 additions & 1 deletion src/library_sigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,6 @@ sigs = {
emscripten_webgl_get_vertex_attrib_d__sig: 'dii',
emscripten_webgl_get_vertex_attrib_o__sig: 'iii',
emscripten_webgl_get_vertex_attrib_v__sig: 'iiipii',
emscripten_webgl_init_context_attributes__sig: 'vp',
emscripten_webgl_make_context_current__sig: 'ip',
emscripten_webgpu_export_bind_group__sig: 'ip',
emscripten_webgpu_export_bind_group_layout__sig: 'ip',
Expand Down
22 changes: 22 additions & 0 deletions system/lib/gl/webgl1.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@

#include "webgl_internal.h"

void emscripten_webgl_init_context_attributes(EmscriptenWebGLContextAttributes *attributes) {
memset(attributes, 0, sizeof(*attributes));

attributes->alpha = 1;
attributes->depth = 1;
attributes->antialias = 1;
attributes->premultipliedAlpha = 1;
attributes->majorVersion = 1;
attributes->enableExtensionsByDefault = 1;

// Default context initialization state (user can override):
// - if main thread is creating the context, default to the context not being
// shared between threads - enabling sharing has performance overhead,
// because it forces the context to be OffscreenCanvas or
// OffscreenFramebuffer.
// - if a web worker is creating the context, default to using OffscreenCanvas
// if available, or proxying via Offscreen Framebuffer if not
if (!emscripten_is_main_runtime_thread()) {
attributes->proxyContextToMainThread = EMSCRIPTEN_WEBGL_CONTEXT_PROXY_FALLBACK;
}
}

#if defined(__EMSCRIPTEN_PTHREADS__) && defined(__EMSCRIPTEN_OFFSCREEN_FRAMEBUFFER__)

static pthread_key_t currentActiveWebGLContext;
Expand Down
12 changes: 6 additions & 6 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4681,
"a.js.gz": 2412,
"a.wasm": 10388,
"a.wasm.gz": 6692,
"total": 15638,
"total_gz": 9483
"a.js": 4593,
"a.js.gz": 2372,
"a.wasm": 10451,
"a.wasm.gz": 6724,
"total": 15613,
"total_gz": 9475
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17917,
"a.js.gz": 8089,
"a.js": 17937,
"a.js.gz": 8077,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21607,
"total_gz": 11161
"total": 21627,
"total_gz": 11149
}
12 changes: 6 additions & 6 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4167,
"a.js.gz": 2236,
"a.wasm": 10388,
"a.wasm.gz": 6692,
"total": 15124,
"total_gz": 9307
"a.js": 4080,
"a.js.gz": 2201,
"a.wasm": 10451,
"a.wasm.gz": 6724,
"total": 15100,
"total_gz": 9304
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17395,
"a.js.gz": 7900,
"a.js": 17415,
"a.js.gz": 7894,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21085,
"total_gz": 10972
"total": 21105,
"total_gz": 10966
}
6 changes: 3 additions & 3 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4690,9 +4690,9 @@ def test_webgl_timer_query(self, args):
'threads': (['-pthread', '-sPROXY_TO_PTHREAD'],)
})
@parameterized({
'v1': ([],),
'v2': (['-sFULL_ES2'],),
'v3': (['-sFULL_ES3'],),
'': ([],),
'es2': (['-sFULL_ES2'],),
'es3': (['-sFULL_ES3'],),
})
def test_webgl_offscreen_framebuffer(self, version, threads):
# Tests all the different possible versions of libgl
Expand Down
2 changes: 1 addition & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -10545,7 +10545,7 @@ def test_minimal_runtime_code_size(self, test_name, js, compare_js_output=False)
hello_webgl_sources = [test_file('minimal_webgl/main.c'),
test_file('minimal_webgl/webgl.c'),
'--js-library', test_file('minimal_webgl/library_js.js'),
'-lwebgl.js',
'-lGL',
'-sMODULARIZE']
hello_webgl2_sources = hello_webgl_sources + ['-sMAX_WEBGL_VERSION=2']
hello_wasm_worker_sources = [test_file('wasm_worker/wasm_worker_code_size.c'), '-sWASM_WORKERS', '-sENVIRONMENT=web,worker']
Expand Down

0 comments on commit cfdc30e

Please sign in to comment.