libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.53k stars 1.77k forks source link

Why does SDL try to make reference to emscripten webgl if I've disabled all renderers? #10326

Open Please-just-dont opened 2 months ago

Please-just-dont commented 2 months ago

I've set everything offf:

set(SDL_RENDER FALSE CACHE BOOL "" FORCE) set(SDL_OPENGL FALSE CACHE BOOL "" FORCE) set(SDL_OPENGLES FALSE CACHE BOOL "" FORCE) set(SDL_RENDER_VULKAN FALSE CACHE BOOL "" FORCE) set(SDL_RENDER_METAL FALSE CACHE BOOL "" FORCE) set(SDL_RENDER_D3D FALSE CACHE BOOL "" FORCE)

And when I compile with Emscripten I can't there are undefined symbols such as emscripten_webgl_create_context and emscripten_webgl_destroy_context. I have added compile and link flags in emscripten to every OpenGL I could but it made no difference.

D:\Apps\emsdk\emsdk\upstream\emscripten\em++.bat -sUSE_WEBGPU -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFULL_ES2 -sASYNCIFY -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFULL_ES2 -sENVIRONMENT=web -sMIN_FIREFOX_VERSION=87 -sMIN_SAFARI_VERSION=140000 -sMIN_CHROME_VERSION=90 --js-library C:/Users/Moe/Desktop/Programming_Source_Code/ExternalDeps/Emscripten/lib/lib_webgpu.js -sALLOW_MEMORY_GROWTH=1 -sUSE_SDL=0 -sFILESYSTEM=1 -sAUTO_JS_LIBRARIES=0 -lmath.js -lhtml5.js -lint53.js @CMakeFiles\myapp.dir\objects1.rsp -o myapp.html @CMakeFiles\myapp.dir\linkLibs.rsp wasm-ld: error: ExternalDeps/SDL/libSDL3.a(SDL_emscriptenopengles.c.o): undefined symbol: emscripten_webgl_create_context wasm-ld: error: ExternalDeps/SDL/libSDL3.a(SDL_emscriptenopengles.c.o): undefined symbol: emscripten_webgl_make_context_current wasm-ld: error: ExternalDeps/SDL/libSDL3.a(SDL_emscriptenopengles.c.o): undefined symbol: emscripten_webgl_destroy_context wasm-ld: error: ExternalDeps/SDL/libSDL3.a(SDL_emscriptenopengles.c.o): undefined symbol: emscripten_webgl_destroy_context wasm-ld: error: ExternalDeps/SDL/libSDL3.a(SDL_emscriptenopengles.c.o): undefined symbol: emscripten_webgl_make_context_current

I've noticed that in the main SDL CMakeLists.txt it checks if SDL_VIDEO is set (which it kind of has to be to use it at all), and if so it sets SDL_VIDEO_DRIVER_EMSCRIPTEN

if(SDL_VIDEO) set(SDL_VIDEO_DRIVER_EMSCRIPTEN 1) sdl_glob_sources("${SDL3_SOURCE_DIR}/src/video/emscripten/*.c")

Then it adds everything in video/emscripten as source. In SDL_emscriptenopengles.c it makes all these calls to emscripten_webgl, which it then can't link to because the linker can't find them. Why are opengl calls being made if it's all been disabled? All the calls to SDL_emscriptenopengles.c are made in SDL_emscriptenvideo.c. I'm trying to use WebGPU, not opengles.

icculus commented 2 months ago

I think this is probably an assumption that one is using WebGL in emscripten, but afaict it doesn't rely on it (Emscripten_CreateWindow has an #ifdef SDL_VIDEO_OPENGL_EGL in there, so you can create an SDL Window without it creating a GL context.

I think we probably just need some preprocessor checks in SDL_emscriptenopengles.c to fix this, assuming we don't need any extra specific magic for WebGPU support on top of this.

Please-just-dont commented 2 months ago

Do we even need to create an SDL window to use Emscripten with the canvas? I mean last time I checked the event system doesn't work without initialising the video subsystem so I think I have to create a window.

icculus commented 2 months ago

You have to create an SDL window if you want to get SDL events and other things, but I think the canvas exists on the page already, so you can probably just mess with it via JavaScript (and probably have to at least a little to set up WebGPU, I presume).

Anyhow, the build issue I will fix, but my initial look at the code suggests SDL won't actually try to initialize WebGL unless you use SDL_WINDOW_OPENGL when creating a window, so this is strictly a linking issue that needs to be resolved.

(And in the short term, it's safe to just link that part of Emscripten in, it just won't actually be used.)

Please-just-dont commented 2 months ago

If someone comes across this problem, you need give these linker flags to get rid of a bunch of linker errors to OpenGL functions:

append_compiler_flags("-sMIN_WEBGL_VERSION=2") append_compiler_flags("-sMAX_WEBGL_VERSION=2")

Then you need to link the Javascript html5_webgl.js library, or -sAUTO_JS_LIBRARIES=1.

This is what you need if you call SDL_createwindow.

This took me an entire day to figure out, working with this sort of stuff without documentation is so hard.