Skip to content

CAN_ADDRESS_2GB vs emscripten_webgl_create_context #21278

Closed

Description

@sbc100

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:

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.

See https://emscripten.org/docs/api_reference/html5.h.html#c.emscripten_webgl_create_context

Possible solutions:

  1. Update emscripten_webgl_create_context such that all error return 0 and change the type of EMSCRIPTEN_WEBGL_CONTEXT_HANDLE to be unsigned.
  2. Create a new API and deprecate emscripten_webgl_create_context
  3. Always return a small integer handle from emscripten_webgl_create_context and 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:

  1. 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.
  2. 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 < 0 could now get a compiler warning.. but such code would already be broken since it wouldn't handle the exist 0 error return codes.

Activity

  1. sbc100 commented on Feb 6, 2024

    @sbc100
    CollaboratorAuthor
  2. added 3 commits that reference this issue on Feb 6, 2024
    da55077
    befa7f5
    982b108
  3. sbc100 commented on Feb 7, 2024

    @sbc100
    CollaboratorAuthor

    #21281 implements (1)

  4. juj commented on Feb 7, 2024

    @juj
    Collaborator

    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.

  5. sbc100 commented on Feb 7, 2024

    @sbc100
    CollaboratorAuthor

    LGTM - though with Wasm64 we still have the problem that we would need to return an int64_t...

    The size the type was already widened to intptr_t in 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.

  6. added a commit that references this issue on Feb 7, 2024
    38f9ad8
  7. added a commit that references this issue on Apr 12, 2024
    baa07c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions