Skip to content

Commit

Permalink
[wasm64] Fix offscreen canvas and offscreen framebuffer. (emscripten-…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Nov 13, 2023
1 parent 457ea77 commit 40cbc21
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 137 deletions.
2 changes: 2 additions & 0 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,8 @@ def create_pointer_conversion_wrappers(metadata):
'emscripten_proxy_finish': '_p',
'emscripten_proxy_execute_queue': '_p',
'_emval_coro_resume': '_pp',
'emscripten_main_runtime_thread_id': 'p',
'_emscripten_set_offscreencanvas_size_on_thread': '_pp__',
}

for function in settings.SIGNATURE_CONVERSIONS:
Expand Down
2 changes: 1 addition & 1 deletion src/generated_struct_info32.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
"EM_FUNC_SIG_PARAM_F": 2,
"EM_FUNC_SIG_PARAM_F2I": 5,
"EM_FUNC_SIG_PARAM_I": 0,
"EM_FUNC_SIG_PARAM_I64": 1,
"EM_FUNC_SIG_PARAM_J": 1,
"EM_HTML5_LONG_STRING_LEN_BYTES": 128,
"EM_HTML5_MEDIUM_STRING_LEN_BYTES": 64,
"EM_HTML5_SHORT_STRING_LEN_BYTES": 32,
Expand Down
2 changes: 1 addition & 1 deletion src/generated_struct_info64.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
"EM_FUNC_SIG_PARAM_F": 2,
"EM_FUNC_SIG_PARAM_F2I": 5,
"EM_FUNC_SIG_PARAM_I": 0,
"EM_FUNC_SIG_PARAM_I64": 1,
"EM_FUNC_SIG_PARAM_J": 1,
"EM_HTML5_LONG_STRING_LEN_BYTES": 128,
"EM_HTML5_MEDIUM_STRING_LEN_BYTES": 64,
"EM_HTML5_SHORT_STRING_LEN_BYTES": 32,
Expand Down
23 changes: 16 additions & 7 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
#error "EVAL_CTORS is not compatible with pthreads yet (passive segments)"
#endif

{{{
#if MEMORY64
globalThis.MAX_PTR = Number((2n ** 64n) - 1n);
#else
globalThis.MAX_PTR = (2 ** 32) - 1
#endif
}}}

var LibraryPThread = {
$PThread__postset: 'PThread.init();',
$PThread__deps: ['_emscripten_thread_init',
Expand Down Expand Up @@ -763,11 +771,12 @@ var LibraryPThread = {
// Comma-delimited list of CSS selectors that must identify canvases by IDs: "#canvas1, #canvas2, ..."
var transferredCanvasNames = attr ? {{{ makeGetValue('attr', C_STRUCTS.pthread_attr_t._a_transferredcanvases, '*') }}} : 0;
#if OFFSCREENCANVASES_TO_PTHREAD
// Proxied canvases string pointer -1 is used as a special token to fetch
// whatever canvases were passed to build in -s
// OFFSCREENCANVASES_TO_PTHREAD= command line.
if (transferredCanvasNames == (-1 >>> 0)) transferredCanvasNames = '{{{ OFFSCREENCANVASES_TO_PTHREAD }}}';
else
// Proxied canvases string pointer -1/MAX_PTR is used as a special token to
// fetch whatever canvases were passed to build in
// -sOFFSCREENCANVASES_TO_PTHREAD= command line.
if (transferredCanvasNames == {{{ MAX_PTR }}}) {
transferredCanvasNames = '{{{ OFFSCREENCANVASES_TO_PTHREAD }}}';
} else
#endif
if (transferredCanvasNames) transferredCanvasNames = UTF8ToString(transferredCanvasNames).trim();
if (transferredCanvasNames) transferredCanvasNames = transferredCanvasNames.split(',');
Expand Down Expand Up @@ -816,10 +825,10 @@ var LibraryPThread = {
// Create a shared information block in heap so that we can control
// the canvas size from any thread.
if (!canvas.canvasSharedPtr) {
canvas.canvasSharedPtr = _malloc(12);
canvas.canvasSharedPtr = _malloc({{{ 8 + POINTER_SIZE }}});
{{{ makeSetValue('canvas.canvasSharedPtr', 0, 'canvas.width', 'i32') }}};
{{{ makeSetValue('canvas.canvasSharedPtr', 4, 'canvas.height', 'i32') }}};
{{{ makeSetValue('canvas.canvasSharedPtr', 8, 0, 'i32') }}}; // pthread ptr to the thread that owns this canvas, filled in below.
{{{ makeSetValue('canvas.canvasSharedPtr', 8, 0, '*') }}}; // pthread ptr to the thread that owns this canvas, filled in below.
}
offscreenCanvasInfo = {
offscreenCanvas: canvas.transferControlToOffscreen(),
Expand Down
32 changes: 16 additions & 16 deletions src/library_sigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ sigs = {
emscripten_idb_read_from_blob__sig: 'viiip',
emscripten_idb_store__sig: 'vpppip',
emscripten_idb_store_blob__sig: 'vpppip',
emscripten_is_webgl_context_lost__sig: 'ii',
emscripten_is_webgl_context_lost__sig: 'ip',
emscripten_lazy_load_code__sig: 'v',
emscripten_lock_async_acquire__sig: 'vpppd',
emscripten_lock_orientation__sig: 'ii',
Expand Down Expand Up @@ -850,21 +850,21 @@ sigs = {
emscripten_wasm_worker_post_function_viii__sig: 'vipiii',
emscripten_wasm_worker_self_id__sig: 'i',
emscripten_webgl_commit_frame__sig: 'i',
emscripten_webgl_create_context__sig: 'ipp',
emscripten_webgl_destroy_context__sig: 'ii',
emscripten_webgl_create_context__sig: 'ppp',
emscripten_webgl_destroy_context__sig: 'ip',
emscripten_webgl_do_commit_frame__sig: 'i',
emscripten_webgl_do_create_context__sig: 'ipp',
emscripten_webgl_do_get_current_context__sig: 'i',
emscripten_webgl_enable_ANGLE_instanced_arrays__sig: 'ii',
emscripten_webgl_enable_OES_vertex_array_object__sig: 'ii',
emscripten_webgl_enable_WEBGL_draw_buffers__sig: 'ii',
emscripten_webgl_enable_WEBGL_draw_instanced_base_vertex_base_instance__sig: 'ii',
emscripten_webgl_enable_WEBGL_multi_draw__sig: 'ii',
emscripten_webgl_enable_WEBGL_multi_draw_instanced_base_vertex_base_instance__sig: 'ii',
emscripten_webgl_enable_extension__sig: 'iip',
emscripten_webgl_get_context_attributes__sig: 'iip',
emscripten_webgl_get_current_context__sig: 'i',
emscripten_webgl_get_drawing_buffer_size__sig: 'iipp',
emscripten_webgl_do_create_context__sig: 'ppp',
emscripten_webgl_do_get_current_context__sig: 'p',
emscripten_webgl_enable_ANGLE_instanced_arrays__sig: 'ip',
emscripten_webgl_enable_OES_vertex_array_object__sig: 'ip',
emscripten_webgl_enable_WEBGL_draw_buffers__sig: 'ip',
emscripten_webgl_enable_WEBGL_draw_instanced_base_vertex_base_instance__sig: 'ip',
emscripten_webgl_enable_WEBGL_multi_draw__sig: 'ip',
emscripten_webgl_enable_WEBGL_multi_draw_instanced_base_vertex_base_instance__sig: 'ip',
emscripten_webgl_enable_extension__sig: 'ipp',
emscripten_webgl_get_context_attributes__sig: 'ipp',
emscripten_webgl_get_current_context__sig: 'p',
emscripten_webgl_get_drawing_buffer_size__sig: 'ippp',
emscripten_webgl_get_parameter_d__sig: 'di',
emscripten_webgl_get_parameter_i64v__sig: 'vip',
emscripten_webgl_get_parameter_o__sig: 'ii',
Expand All @@ -882,7 +882,7 @@ sigs = {
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: 'ii',
emscripten_webgl_make_context_current__sig: 'ip',
emscripten_webgpu_export_bind_group__sig: 'ip',
emscripten_webgpu_export_bind_group_layout__sig: 'ip',
emscripten_webgpu_export_buffer__sig: 'ip',
Expand Down
13 changes: 7 additions & 6 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,15 +991,16 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};

registerContext: (ctx, webGLContextAttributes) => {
#if PTHREADS
// with pthreads a context is a location in memory with some synchronized data between threads
var handle = _malloc(8);
// with pthreads a context is a location in memory with some synchronized
// data between threads
var handle = _malloc({{{ 2 * POINTER_SIZE }}});
#if GL_ASSERTIONS
assert(handle, 'malloc() failed in GL.registerContext!');
#endif
#if GL_SUPPORT_EXPLICIT_SWAP_CONTROL
{{{ makeSetValue('handle', 0, 'webGLContextAttributes.explicitSwapControl', 'i32')}}}; // explicitSwapControl
{{{ makeSetValue('handle', 0, 'webGLContextAttributes.explicitSwapControl', 'i32')}}};
#endif
{{{ makeSetValue('handle', 4, '_pthread_self()', 'i32')}}}; // the thread pointer of the thread that owns the control of the context
{{{ makeSetValue('handle', POINTER_SIZE, '_pthread_self()', '*')}}}; // the thread pointer of the thread that owns the control of the context
#else // PTHREADS
// without pthreads a context is just an integer ID
var handle = GL.getNewId(GL.contexts);
Expand Down Expand Up @@ -1307,7 +1308,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
break;
case 0x8DF8: // GL_SHADER_BINARY_FORMATS
#if GL_TRACK_ERRORS
if (type != {{{ cDefs.EM_FUNC_SIG_PARAM_I }}} && type != {{{ cDefs.EM_FUNC_SIG_PARAM_I64 }}}) {
if (type != {{{ cDefs.EM_FUNC_SIG_PARAM_I }}} && type != {{{ cDefs.EM_FUNC_SIG_PARAM_J }}}) {
GL.recordError(0x500); // GL_INVALID_ENUM
#if GL_ASSERTIONS
err(`GL_INVALID_ENUM in glGet${type}v(GL_SHADER_BINARY_FORMATS): Invalid parameter type!`);
Expand Down Expand Up @@ -1464,7 +1465,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
}

switch (type) {
case {{{ cDefs.EM_FUNC_SIG_PARAM_I64 }}}: writeI53ToI64(p, ret); break;
case {{{ cDefs.EM_FUNC_SIG_PARAM_J }}}: writeI53ToI64(p, ret); break;
case {{{ cDefs.EM_FUNC_SIG_PARAM_I }}}: {{{ makeSetValue('p', '0', 'ret', 'i32') }}}; break;
case {{{ cDefs.EM_FUNC_SIG_PARAM_F }}}: {{{ makeSetValue('p', '0', 'ret', 'float') }}}; break;
case {{{ cDefs.EM_FUNC_SIG_PARAM_B }}}: {{{ makeSetValue('p', '0', 'ret ? 1 : 0', 'i8') }}}; break;
Expand Down
6 changes: 3 additions & 3 deletions src/library_webgl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var LibraryWebGL2 = {

glGetInteger64v__deps: ['$emscriptenWebGLGet'],
glGetInteger64v: (name_, p) => {
emscriptenWebGLGet(name_, p, {{{ cDefs.EM_FUNC_SIG_PARAM_I64 }}});
emscriptenWebGLGet(name_, p, {{{ cDefs.EM_FUNC_SIG_PARAM_J }}});
},

glGetInternalformativ: (target, internalformat, pname, bufSize, params) => {
Expand Down Expand Up @@ -456,7 +456,7 @@ var LibraryWebGL2 = {
}

switch (type) {
case {{{ cDefs.EM_FUNC_SIG_PARAM_I64 }}}: writeI53ToI64(data, ret); break;
case {{{ cDefs.EM_FUNC_SIG_PARAM_J }}}: writeI53ToI64(data, ret); break;
case {{{ cDefs.EM_FUNC_SIG_PARAM_I }}}: {{{ makeSetValue('data', '0', 'ret', 'i32') }}}; break;
case {{{ cDefs.EM_FUNC_SIG_PARAM_F }}}: {{{ makeSetValue('data', '0', 'ret', 'float') }}}; break;
case {{{ cDefs.EM_FUNC_SIG_PARAM_B }}}: {{{ makeSetValue('data', '0', 'ret ? 1 : 0', 'i8') }}}; break;
Expand All @@ -470,7 +470,7 @@ var LibraryWebGL2 = {

glGetInteger64i_v__deps: ['$emscriptenWebGLGetIndexed'],
glGetInteger64i_v: (target, index, data) =>
emscriptenWebGLGetIndexed(target, index, data, {{{ cDefs.EM_FUNC_SIG_PARAM_I64 }}}),
emscriptenWebGLGetIndexed(target, index, data, {{{ cDefs.EM_FUNC_SIG_PARAM_J }}}),

// Uniform Buffer objects
glBindBufferBase: (target, index, buffer) => {
Expand Down
2 changes: 1 addition & 1 deletion src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@
"file": "emscripten/threading.h",
"defines": [
"EM_FUNC_SIG_PARAM_I",
"EM_FUNC_SIG_PARAM_I64",
"EM_FUNC_SIG_PARAM_J",
"EM_FUNC_SIG_PARAM_F",
"EM_FUNC_SIG_PARAM_D",
"EM_FUNC_SIG_PARAM_B",
Expand Down
21 changes: 11 additions & 10 deletions src/threadprofiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ var emscriptenThreadProfiler = {
var profilerBlock = Atomics.load(HEAPU32, (threadPtr + 8 /* {{{ C_STRUCTS.pthread.profilerBlock }}}*/) >> 2);
var threadName = PThread.getThreadName(threadPtr);
if (threadName) {
threadName = '"' + threadName + '" (' + ptrToString(threadPtr) + ')';
threadName = `"${threadName}" (${ptrToString(threadPtr)})`;
} else {
threadName = '(' + ptrToString(threadPtr) + ')';
threadName = `(${ptrToString(threadPtr)}})`;
}

console.log('Thread ' + threadName + ' now: ' + PThread.threadStatusAsString(threadPtr) + '. ');
console.log(`Thread ${threadName} now: ${PThread.threadStatusAsString(threadPtr)}. `);
}
},

Expand All @@ -72,27 +72,28 @@ var emscriptenThreadProfiler = {
var profilerBlock = Atomics.load(HEAPU32, (threadPtr + 8 /* {{{ C_STRUCTS.pthread.profilerBlock }}}*/) >> 2);
var threadName = PThread.getThreadName(threadPtr);
if (threadName) {
threadName = '"' + threadName + '" (' + ptrToString(threadPtr) + ')';
threadName = `"${threadName}" (${ptrToString(threadPtr)})`;
} else {
threadName = '(' + ptrToString(threadPtr) + ')';
threadName = `(${ptrToString(threadPtr)})`;
}

str += 'Thread ' + threadName + ' now: ' + PThread.threadStatusAsString(threadPtr) + '. ';
str += `Thread ${threadName} now: ${PThread.threadStatusAsString(threadPtr)}. `;

var threadTimesInStatus = [];
var totalTime = 0;
for (var j = 0; j < 7/*EM_THREAD_STATUS_NUMFIELDS*/; ++j) {
threadTimesInStatus.push(HEAPF64[((profilerBlock + 16/*C_STRUCTS.thread_profiler_block.timeSpentInStatus*/) >> 3) + j]);
var offset = profilerBlock + 16/*C_STRUCTS.thread_profiler_block.timeSpentInStatus*/;
for (var j = 0; j < 7/*EM_THREAD_STATUS_NUMFIELDS*/; ++j, offset += 8) {
threadTimesInStatus.push(Number(getValue(offset, 'double')));
totalTime += threadTimesInStatus[j];
HEAPF64[((profilerBlock + 16/*C_STRUCTS.thread_profiler_block.timeSpentInStatus*/) >> 3) + j] = 0;
setValue(offset, 0, 'double');
}
var recent = '';
if (threadTimesInStatus[1] > 0) recent += (threadTimesInStatus[1] / totalTime * 100.0).toFixed(1) + '% running. ';
if (threadTimesInStatus[2] > 0) recent += (threadTimesInStatus[2] / totalTime * 100.0).toFixed(1) + '% sleeping. ';
if (threadTimesInStatus[3] > 0) recent += (threadTimesInStatus[3] / totalTime * 100.0).toFixed(1) + '% waiting for futex. ';
if (threadTimesInStatus[4] > 0) recent += (threadTimesInStatus[4] / totalTime * 100.0).toFixed(1) + '% waiting for mutex. ';
if (threadTimesInStatus[5] > 0) recent += (threadTimesInStatus[5] / totalTime * 100.0).toFixed(1) + '% waiting for proxied ops. ';
if (recent.length > 0) str += 'Recent activity: ' + recent;
if (recent.length > 0) str += `Recent activity: ${recent}`;
str += '<br />';
}
this.threadProfilerDiv.innerHTML = str;
Expand Down
3 changes: 2 additions & 1 deletion system/include/emscripten/html5_webgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

#pragma once

#include <stdint.h>
#include <emscripten/html5.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef int EMSCRIPTEN_WEBGL_CONTEXT_HANDLE;
typedef intptr_t EMSCRIPTEN_WEBGL_CONTEXT_HANDLE;

typedef int EMSCRIPTEN_WEBGL_CONTEXT_PROXY_MODE;
#define EMSCRIPTEN_WEBGL_CONTEXT_PROXY_DISALLOW 0
Expand Down
Loading

0 comments on commit 40cbc21

Please sign in to comment.