Repository navigation
Description
emscripten_webgl_create_context return EMSCRIPTEN_WEBGL_CONTEXT_HANDLE which is an opaque handle to the context.
Internally, the ends up either being small integer handle or, in multi-threaded build, it ends up being a pointer to small struct that we malloc:
emscripten/src/library_webgl.js
Line 1058 in 288d475
| var handle = _malloc({{{ 2 * POINTER_SIZE }}}); |
The problem is that emscripten_webgl_create_context is currently documented such that any value <= 0 is an error, and specifically that negative values are errrors. In CAN_ADDRESS_2GB mode, all pointers are greater than 2^31 and therefore show up as negative return values.
Possible solutions:
- Update
emscripten_webgl_create_contextsuch that all error return 0 and change the type ofEMSCRIPTEN_WEBGL_CONTEXT_HANDLEto be unsigned. - Create a new API and deprecate
emscripten_webgl_create_context - Always return a small integer handle from
emscripten_webgl_create_contextand then maintain a mapping to this mapped region separately.
(3) is the probably the safest, but comes with added overhead.
Of these options I think (1) is by far the easiest, but it has two possible downsides:
- In means that reason for context creation is no longer communicated via the return value. However, looking that the code we have to it looks like this feature is never used and all error cases return 0 for failure.
- Existing code that checks for the return code of this function might need updating? However I think it should be fine since all the current error paths already return 0, so existing code will already need to be checking for 0 (or < 1). Code that checks
ctn < 0could now get a compiler warning.. but such code would already be broken since it wouldn't handle the exist 0 error return codes.
Activity
sbc100 commentedon Feb 6, 2024
@juj @kripken @dschuff
Update EMSCRIPTEN_WEBGL_CONTEXT_HANDLE to handle full pointer range
Update EMSCRIPTEN_WEBGL_CONTEXT_HANDLE to handle full pointer range
Update EMSCRIPTEN_WEBGL_CONTEXT_HANDLE to handle full pointer range
sbc100 commentedon Feb 7, 2024
#21281 implements (1)
juj commentedon Feb 7, 2024
LGTM - though with Wasm64 we still have the problem that we would need to return an int64_t...
The reason for the dual handle/pointer semantics was that when we created support for a multithreaded WebGL context (either via OffscreenCanvas vs OffscreenFramebuffer), there was a need to have that behave like one could assume in C code for that to do: given a handle to a context, you could e.g. query the front buffer size of that context, or resize the context.
This is something that happened in UE4 at the time: the main thread would get events for browser resize, but a Worker would be doing the rendering, so if resize occurred, the main thread might want to resize the WebGL context backing surface area, but that is then in the other thread's domain. So shared memory was used to be able to reference the sizing in both threads.
Likewise, main thread might get input events and process them, and so it would need to know what the size of the context was. So sharing the memory would allow main thread to query if the rendering thread might have done a resize.
Though probably in Wasm64 we might still keep this to be 32-bits, and just abort at runtime if the malloced pointer is >32bits... Or only in Wasm64 modes we might upgrade the handle to an int64_t.
sbc100 commentedon Feb 7, 2024
The size the type was already widened to
intptr_tin a previous change: #20678. Hopefully that change seems reasonable too?This change just makes it unsigned, preventing the use of negative values having special meaning.
Update EMSCRIPTEN_WEBGL_CONTEXT_HANDLE to handle full pointer range (#…
Merge upstream 3.1.56 (#64)