emscripten-core / emscripten-core/emscripten

OpenGL functions stop working when using -sPROXY_TO_PTHREAD together with -sUSE_SDL=2

Open
#23,842 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**Description**
I'm trying to convert a project that uses SDL with OpenGL and it uses a library that can be dynamically loaded. What I found out is that the OpenGL functions in SDL stop working once I add -sPROXY_TO_PTHREAD to my linking flags. But PROXY_TO_PTHREAD is required when using dlopen to load a library.

If I disable PROXY_TO_PTHREAD, the error I get is
`Error in loading dynamic library libcalc.so: Error: http://127.0.0.1:8000/libcalc.so: file not found, and synchronous loading of external files is not available`
In this case, if I remove the dlopen from the code, everything works and glEnable(GL_DEPTH_TEST); gets executed correctly, but I'm unable to load my dynamic library.

But when I enable PROXY_TO_PTHREAD and put the dlopen back into my code, I get this error:
`main.worker.js:188 worker.js onmessage() captured an uncaught exception: TypeError: Cannot read properties of undefined (reading 'enable')` (referring to glEnable)
There are more functions with the same error, like glCullFace and glViewport

Is there a way to get dynamic loading of libraries and OpenGL in SDL working at the same time?

**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.56 (cf90417346b78455089e64eb909d71d091ecc055)

**Cmake setup**
(only relevant linking and compiling included)
```cmake
add_library(calc SHARED Src/calc.cpp)
set_target_properties(calc PROPERTIES SUFFIX ".so")
set_target_properties(calc PROPERTIES COMPILE_FLAGS "-sSHARED_MEMORY=1 -Wno-experimental -sUSE_SDL=2")
set_target_properties(calc PROPERTIES LINK_FLAGS "-sSIDE_MODULE=1 -sMAIN_MODULE=0 -sEXPORT_ALL=1 -pthread -sUSE_SDL=2")

add_executable(main Src/main.cpp)
set_target_properties(main PROPERTIES SUFFIX ".html")
set_target_properties(main PROPERTIES COMPILE_FLAGS "-sMAIN_MODULE=2 -sSHARED_MEMORY=1 -Wno-experimental -sUSE_SDL=2")
set_target_properties(main PROPERTIES LINK_FLAGS "-sMAIN_MODULE=2 -sAUTOLOAD_DYLIBS=0 -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=4 -pthread -sUSE_SDL=2")

target_link_libraries(main PUBLIC calc)
```

**main.cpp**
```cpp
#include
#include
#include
#include

int main()
{
std::cout << "Loading wasm lib" << '\n';

// Wasm uses either wasm or so files for library, loading an so file here
// RTLD_GLOBAL is required here, otherwise the library won't be accessible by linked functions after loading
void* dllHandle = dlopen("libcalc.so", RTLD_NOW|RTLD_GLOBAL);
if(!dllHandle)
{
std::cout << dlerror() << '\n';
}
else
{
std::cout << "Handle: " << int(dllHandle) << '\n';
}

SDL_Window* window{};
SDL_GLContext gl_context{};
const int WindowSize[2] = { 1280, 720 };

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER);
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, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");

SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
window = SDL_CreateWindow("Modgine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WindowSize[0], WindowSize[1], window_flags);
gl_context = SDL_GL_CreateContext(window);

glEnable(GL_DEPTH_TEST);
}
```

**calc.cpp**
Is an empty file. For this example we just need a cpp file to be there.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.