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

Unable to create WebGL2 context using GLFW, and I can't find any example code for doing this with GLFW with Emscripten #20363

Open
dennisferron opened this issue Oct 1, 2023 · 2 comments · May be fixed by #21472

Comments

@dennisferron
Copy link

I'm pulling my hair out trying to get a basic "HelloTriangle" shader example working with WebGL2 and GLFW. The code works natively (OpenGL ES 3), but when I try to run the Emscripten version I get errors about "storage qualifier supported in GLSL ES 3.00 and above only" and "Floating-point suffix unsupported prior to GLSL ES 3.00" which leads me to believe I'm still getting a WebGL 1 context.

I did get a similar thing to work using the webgl_sample_query.cpp file from the Emscripten tests; the problem is that that sample doesn't use GLFW to create the context, and the GLFW boilerplate is different enough to that sample's boilerplate that I can't easily see how to merge the two. I did try copying some code over (you'll see in main.cpp), but it has no effect.

Based on all I've read it should (?) "just work" using the GLFW boilerplate, but this doesn't (apparently) result in a WebGL2 context. I thought, maybe I could just read the source code and see what needs to be passed to make GLFW hit the happy path of creating a WebGL2 enabled context, but I can't figure out where this source code file is. Because presumably when Emscripten is passed "-s USE_GLFW=3" that is using some custom version of GLFW built in to Emscripten. (That's also why I'm reporting this as a bug on the Emscripten repository and not against the GLFW project.)

The "Optimizing WebGL" page in the Emscripten docs gives a lot of different options based on different compatibility scenarios, but I'd really appreciate a succinct "here's all the compile options" and "here's all the link options" one should be using for new code for WebGL2.

Moreover if Emscripten supports GLFW, and Emscripten supports WebGL2, and GLFW supports OpenGL ES3, does Emscripten support WebGL2 using GLFW? If the answer is no, you might want to put a warning about that gotcha in the documentation. If the answer is yes, where's the example code?

Thanks.

I've attached the code I'm using (it's not very long):
CMakeLists.txt
main.cpp.txt
webgl_sample_query.cpp.txt

Output of emcc -v:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.22 (a9981ae)
clang version 16.0.0 (https://github.com/llvm/llvm-project 8491d01cc385d08b8b4f5dd097239ea0009ddc63)

@sbc100
Copy link
Collaborator

sbc100 commented Oct 3, 2023

I don't have a quick answer for you but I can give you some pointers that might help.

Emscipten's GLFW implementation lives in JavaScript here: https://github.com/emscripten-core/emscripten/blob/main/src/library_glfw.js.

Specifically the code that create the GL context is here:

emscripten/src/library_glfw.js

Lines 1030 to 1040 in 668703b

var contextAttributes = {
antialias: (GLFW.hints[0x0002100D] > 1), // GLFW_SAMPLES
depth: (GLFW.hints[0x00021005] > 0), // GLFW_DEPTH_BITS
stencil: (GLFW.hints[0x00021006] > 0), // GLFW_STENCIL_BITS
alpha: (GLFW.hints[0x00021004] > 0) // GLFW_ALPHA_BITS
}
#if OFFSCREEN_FRAMEBUFFER
// TODO: Make GLFW explicitly aware of whether it is being proxied or not, and set these to true only when proxying is being performed.
GL.enableOffscreenFramebufferAttributes(contextAttributes);
#endif
Module.ctx = Browser.createContext(Module['canvas'], true, true, contextAttributes);

It doesn't look like that code is capable to creating a context with different GL versions, so perhaps it needs updating?

@sbc100
Copy link
Collaborator

sbc100 commented Oct 3, 2023

However is does look like Browser.createContext will honor the -sMIN_WEBGL_VERSION=2 setting:

var contextAttributes = {
antialias: false,
alpha: false,
#if MIN_WEBGL_VERSION >= 2
majorVersion: 2,
#elif MAX_WEBGL_VERSION >= 2 // library_browser.js defaults: use the WebGL version chosen at compile time (unless overridden below)
majorVersion: (typeof WebGL2RenderingContext != 'undefined') ? 2 : 1,
#else
majorVersion: 1,
#endif
};

Can you try building with -sMIN_WEBGL_VERSION=2?

@pmp-p pmp-p linked a pull request Mar 5, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants