Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create a WebGL2 context when using SDL #4724

Open
pthom opened this issue Apr 27, 2024 · 3 comments
Open

Cannot create a WebGL2 context when using SDL #4724

pthom opened this issue Apr 27, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@pthom
Copy link

pthom commented Apr 27, 2024

🐛 Bug

I'm trying to run an application that uses SDL and OpenGL. I can get something to work with WebGL1 but not WebGL2

Javascript

var context = canvasElement.getContext("webgl2", contextAttributes);

C++

    // Require OpenGL ES with major version 3
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
    auto window = SDL_CreateWindow("my window", 0, 0, 600, 400, window_flags);
    auto glContext = SDL_GL_CreateContext((SDL_Window *)window);
    if (glContext == nullptr)
    {
        std::string msg = std::string("Impl_CreateGlContext error: ") + SDL_GetError();
        printf("%s\n", msg.c_str());
    }

will fail with the message: "Could not create EGL context (call to eglCreateContext failed, reporting an error of EGL_BAD_CONFIG)"

However, using Webgl 1 does work (using canvasElement.getContext("webgl")and not setting SDL_GL_CONTEXT_MAJOR_VERSION in C++).

To Reproduce

Use the attached zip file, and unzip it into pyodide/packages.

daft_lib.zip

daft_lib
├── daft_lib_src
│   ├── CMakeLists.txt             # uses pybind11, links with -sUSE_SDL=2 sMAX_WEBGL_VERSION=2 sMIN_WEBGL_VERSION=2
│   ├── _daft_lib_test.html        # A simple test page, copy it into dist/
│   ├── _daft_lib_webgl_test.html  # A simple standalone test that ensures webgl2 is available (outside of pyiodide)
│   ├── pyproject.toml
│   └── src
│       ├── DaftLib
│       │   ├── DaftLib.cpp        # provides `dummy_sdl_call` (will create a window and its opengl context)
│       │   └── DaftLib.h
│       └── python_bindings
│           ├── daft_lib
│           │   └── __init__.py
│           └── module.cpp         # provides a binding for `dummy_sdl_call`
└── meta.yaml                      #  repeats the flags -SUSE_SDL=2 -sMAX_WEBGL_VERSION=2 -sMIN_WEBGL_VERSION=2

Build it "in tree" with:

PYODIDE_PACKAGES="daft_lib" make

The HTML and C++ files are heavily commented and will produce lots of logs in order to help the diagnostic.

  • Compile, and copy (or link) _daft_lib_test.html into pyodide/dist
  • Launch a web server, and open _daft_lib_test.html with a browser
  • Look at the JavaScript console.

Note: it is possible to switch to WebGL 1, by setting USE_WEBGL2=false in _daft_lib_test.html, and commenting out the #define USE_WEBGL2 in _daft_lib_test.html

Expected behavior

I hope it would be possible to use WebGL 2.

Environment

  • Pyodide Version:
    main branch, last commit:
    commit d32e376 (origin/main, origin/HEAD)
    Author: Hood Chatham roberthoodchatham@gmail.com
    Date: Tue Apr 23 15:01:33 2024 +0200

  • Browser version:
    Fails with Firefox and Chromium on Linux.
    Fails with Firefox and Safari on Mac OS

  • Any other relevant information:
    I already compiled numerous C++ / SDL programs to emscripten, using those same parameters, and it worked without issue.
    I guess there might be something in the way emscripten is configured inside pyodide.

Additional context

I'm trying to port a library I created to pyodide: https://pthom.github.io/imgui_bundle/

Live emscripten demo here: https://traineq.org/ImGuiBundle/emscripten/bin/demo_imgui_bundle.html

@pthom pthom added the bug Something isn't working label Apr 27, 2024
@ryanking13
Copy link
Member

Thanks for opening the issue! Unfortunately I'm not an SDL expert, so it's not clear to me exactly what's going on.

I did some quick search for the error in the Emscripten repository, and found that Emscripten only allows hard coded default config (code pointer, document). Maybe this can be related somehow, but I am not very sure...

@pthom
Copy link
Author

pthom commented Apr 28, 2024

Hello, thank you for your answer!

I think the information you gave me can hopefully point us in a better direction.

As far as I can understand, by reading the emscripten documentation, I think that OpenGL and EGL are two different graphics rendering targets.

  • The page about EGL Is someone honest about the fact that the support for this API is lacking (and that in fact it is not common):

Somewhat disappointingly, EGL is not a self-sufficient complete solution for initializing GLES2 graphics rendering (on any platform, not just Emscripten) and overseeing various associated tasks. The specification is limited in its scope and lacks some features.

So the question becomes why does the application try to call eglCreateContext. I'll try to have a look at the call stack, to see if I can gather more information.

@pthom
Copy link
Author

pthom commented Apr 29, 2024

I opened a discussion about this subject in the Emscripten repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants