Dav1dde / glad

Multi-Language Vulkan/GL/GLES/EGL/GLX/WGL Loader-Generator based on the official specs.
https://glad.dav1d.de/
Other
3.73k stars 440 forks source link

Use glGenQueries failure with Emscripten in Web #396

Closed syby119 closed 1 year ago

syby119 commented 1 year ago

When I use glfw3 + glad2.0 to transpile my C++ code to WebAssembly, everything works well. I choose the gles2 version3.2 core, and I can render a triangle in the newest version of Microsoft Edge browser with WebGL2.

Here are some code snippets for my CMakeLists.txt

# I write the CMakeLists for the glad
target_link_libraries(${PROJECT_NAME} PRIVATE glad)

if (EMSCRIPTEN)
    target_link_libraries(${PROJECT_NAME} PRIVATE "-s USE_GLFW=3")
    target_link_libraries(${PROJECT_NAME} PRIVATE "-s FULL_ES3=1")
    target_link_libraries(${PROJECT_NAME} PRIVATE "-s MIN_WEBGL_VERSION=2")
    target_link_libraries(${PROJECT_NAME} PRIVATE "-s MAX_WEBGL_VERSION=2")
    target_link_libraries(${PROJECT_NAME} PRIVATE "-s WASM=1")
    target_link_libraries(${PROJECT_NAME} PRIVATE "-s NO_DISABLE_EXCEPTION_CATCHING=1")
    target_link_libraries(${PROJECT_NAME} PRIVATE "-s ALLOW_MEMORY_GROWTH=1")
    target_link_libraries(${PROJECT_NAME} PRIVATE "-s GL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS=1")
else()
    target_link_libraries(${PROJECT_NAME} PRIVATE glfw)
endif()

However, when I want to use glGenQueries to test GL_ANY_SAMPLES_PASSED, so as to perform order independent transparency rendering, I encounter a problem.

The browser tells me GLctx.disjointTimerQueryExt.createQueryEXT is not a function.

I search for MDN and it tells me that in WebGL2, the function is changed into GLctx.createQuery. EXT_disjoint_timer_query createQuery

I only include the header from glad, i.e. glad/gles2.h, while not including any OpenGL/ES header file from Emscripten.

Is it a problem with glad or it's a problem with Emscripten?

By the way, I also tried to replace the original glad library with gles2.0 compatible + GL_EXT_disjoint_timer_query. It didn't work either.

Thanks in advance.

syby119 commented 1 year ago

I test glGenQueries with glfw3 + glew from the ArthurSonzongni's project (OpenGL_CMake_Skeleton) in WebAssembly branch, by simply add a call to the function after the OpenGL context has been setup. It works well in the browser.

syby119 commented 1 year ago

Here are my CMakeLists.txt for glad


cmake_minimum_required(VERSION 3.15)

project(glad VERSION 2.0)

if (EMSCRIPTEN OR USE_GLES)
    add_library(glad
        ./include/glad/gles2.h 
        ./include/KHR/khrplatform.h 
        ./src/gles2.c
    )
else()
    add_library(glad
        ./include/glad/gl.h 
        ./include/KHR/khrplatform.h 
        ./src/gl.c
    )
endif()

target_include_directories(glad PUBLIC ./include)
`
Dav1dde commented 1 year ago

Glad only loads the functions from emscripten it does not do anything else, so I assume this is more of a emscripten/GLES2/WebGL issue and I am not familiar enough to properly help you with that. You might find better help on StackOverflow or another platform here.

syby119 commented 1 year ago

Glad only loads the functions from emscripten it does not do anything else, so I assume this is more of a emscripten/GLES2/WebGL issue and I am not familiar enough to properly help you with that. You might find better help on StackOverflow or another platform here.

Thank you for your reply. I've removed glad when compiled with Emscripten currently.

I think the problem is that Emscripten registers the function glGenQuery in a WebGL1.0 deprecated style, while the canvas use WebGL2.0 context.

I'll work out the problem if I have time, since glad is a really convenient library.