emscripten-core / emscripten-core/emscripten
Sometimes operating on the same WebGLContext between two separate canvases
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
**Version of emscripten/emsdk:**
```shell
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.20-git
clang version 16.0.0 (https://github.com/llvm/llvm-project.git 75767a0f9a926641edbef08e31ec2148ff45da67)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/bin
```
**Failing command line in full:**
```cmake
project(emsdk)
set(EMSDK_LINK_OPTIONS
-lembind
-pthread
-sPROXY_TO_PTHREAD
-sALLOW_MEMORY_GROWTH=1
-sUSE_ZLIB=1
-sFULL_ES3
-sOFFSCREEN_FRAMEBUFFER
-sLLD_REPORT_UNDEFINED
-sWASM_BIGINT
-sENVIRONMENT=webview,web,worker
-sMODULARIZE
-sEXPORT_NAME=loadChronosModule
)
# Let emcc prebuild before generating cmake project.
execute_process(COMMAND emcc ${PROJECT_SOURCE_DIR}/emsdk.cc
--no-entry
-o ${PROJECT_BINARY_DIR}/emsdk.js
${EMSDK_LINK_OPTIONS}
)
add_library(emsdk INTERFACE)
target_compile_options(emsdk INTERFACE -pthread)
target_link_options(emsdk INTERFACE ${EMSDK_LINK_OPTIONS})
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(ENV{EMCC_DEBUG} 2)
target_compile_options(emsdk INTERFACE -v -g -fdebug-compilation-dir='${CMAKE_SOURCE_DIR}')
target_link_options(emsdk INTERFACE
-v
-sSAFE_HEAP=1
-sSAFE_HEAP_LOG
-sDEMANGLE_SUPPORT=1
-sASSERTIONS=2
-sTRACE_WEBGL_CALLS
-sGL_DEBUG
)
endif()
```
**Full link command and output with `-v` appended:**
```shell
[main] Configuring folder: wasm_playground
[driver] Removing /Users/luao/Desktop/wasm_playground/build/CMakeCache.txt
[driver] Removing /Users/luao/Desktop/wasm_playground/build/CMakeFiles
[proc] Executing command: /usr/local/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_TOOLCHAIN_FILE:FILEPATH=/usr/local/opt/emscripten/libexec/cmake/Modules/Platform/Emscripten.cmake -S/Users/luao/Desktop/wasm_playground -B/Users/luao/Desktop/wasm_playground/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] emcc: warning: USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code slowly, see https://github.com/WebAssembly/design/issues/1271 [-Wpthreads-mem-growth]
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /Users/luao/Desktop/wasm_playground/build
[main] Building folder: wasm_playground
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/luao/Desktop/wasm_playground/build --config Debug --target all --
[build] [2/3 33% :: 0.957] Building CXX object CMakeFiles/wasm-playground.dir/src/gl_context_manager.cc.o
[build] "/usr/local/Cellar/emscripten/3.1.20/libexec/llvm/bin/clang++" -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -D__EMSCRIPTEN_SHARED_MEMORY__=1 -DEMSCRIPTEN -D__EMSCRIPTEN_major__=3 -D__EMSCRIPTEN_minor__=1 -D__EMSCRIPTEN_tiny__=20 -I/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/SDL --sysroot=/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot -Xclang -iwithsysroot/include/compat -g3 -pthread -v -g3 -fdebug-compilation-dir='/Users/luao/Desktop/wasm_playground' -std=gnu++17 -MD -MT CMakeFiles/wasm-playground.dir/src/gl_context_manager.cc.o -MF CMakeFiles/wasm-playground.dir/src/gl_context_manager.cc.o.d -c -matomics -mbulk-memory /Users/luao/Desktop/wasm_playground/src/gl_context_manager.cc -o CMakeFiles/wasm-playground.dir/src/gl_context_manager.cc.o
[build] clang version 16.0.0 (https://github.com/llvm/llvm-project.git 75767a0f9a926641edbef08e31ec2148ff45da67)
[build] Target: wasm32-unknown-emscripten
[build] Thread model: posix
[build] InstalledDir: /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/bin
[build] (in-process)
[build] "/usr/local/Cellar/emscripten/3.1.20/libexec/llvm/bin/clang-16" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name gl_context_manager.cc -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-feature +atomics -target-feature +bulk-memory -target-feature +mutable-globals -target-feature +sign-ext -target-cpu generic -target-feature +atomics -target-feature +bulk-memory -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -target-linker-version 764 -v -fcoverage-compilation-dir=/Users/luao/Desktop/wasm_playground/build -resource-dir /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/lib/clang/16.0.0 -dependency-file CMakeFiles/wasm-playground.dir/src/gl_context_manager.cc.o.d -MT CMakeFiles/wasm-playground.dir/src/gl_context_manager.cc.o -sys-header-deps -D __EMSCRIPTEN_SHARED_MEMORY__=1 -D EMSCRIPTEN -D __EMSCRIPTEN_major__=3 -D __EMSCRIPTEN_minor__=1 -D __EMSCRIPTEN_tiny__=20 -I /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/SDL -isysroot /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/c++/v1 -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/lib/clang/16.0.0/include -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/wasm32-emscripten -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir='/Users/luao/Desktop/wasm_playground' -ferror-limit 19 -fvisibility default -pthread -fgnuc-version=4.2.1 -fcxx-exceptions -fignore-exceptions -fexceptions -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o CMakeFiles/wasm-playground.dir/src/gl_context_manager.cc.o -x c++ /Users/luao/Desktop/wasm_playground/src/gl_context_manager.cc
[build] clang -cc1 version 16.0.0 based upon LLVM 16.0.0git default target x86_64-apple-darwin21.5.0
[build] ignoring nonexistent directory "/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/wasm32-emscripten/c++/v1"
[build] ignoring nonexistent directory "/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/wasm32-emscripten"
[build] #include "..." search starts here:
[build] #include <...> search starts here:
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/SDL
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/compat
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/c++/v1
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/lib/clang/16.0.0/include
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include
[build] End of search list.
[build] [2/3 66% :: 1.048] Building CXX object CMakeFiles/wasm-playground.dir/src/main.cc.o
[build] "/usr/local/Cellar/emscripten/3.1.20/libexec/llvm/bin/clang++" -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -D__EMSCRIPTEN_SHARED_MEMORY__=1 -DEMSCRIPTEN -D__EMSCRIPTEN_major__=3 -D__EMSCRIPTEN_minor__=1 -D__EMSCRIPTEN_tiny__=20 -I/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/SDL --sysroot=/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot -Xclang -iwithsysroot/include/compat -g3 -pthread -v -g3 -fdebug-compilation-dir='/Users/luao/Desktop/wasm_playground' -std=gnu++17 -MD -MT CMakeFiles/wasm-playground.dir/src/main.cc.o -MF CMakeFiles/wasm-playground.dir/src/main.cc.o.d -c -matomics -mbulk-memory /Users/luao/Desktop/wasm_playground/src/main.cc -o CMakeFiles/wasm-playground.dir/src/main.cc.o
[build] clang version 16.0.0 (https://github.com/llvm/llvm-project.git 75767a0f9a926641edbef08e31ec2148ff45da67)
[build] Target: wasm32-unknown-emscripten
[build] Thread model: posix
[build] InstalledDir: /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/bin
[build] (in-process)
[build] "/usr/local/Cellar/emscripten/3.1.20/libexec/llvm/bin/clang-16" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cc -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-feature +atomics -target-feature +bulk-memory -target-feature +mutable-globals -target-feature +sign-ext -target-cpu generic -target-feature +atomics -target-feature +bulk-memory -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -target-linker-version 764 -v -fcoverage-compilation-dir=/Users/luao/Desktop/wasm_playground/build -resource-dir /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/lib/clang/16.0.0 -dependency-file CMakeFiles/wasm-playground.dir/src/main.cc.o.d -MT CMakeFiles/wasm-playground.dir/src/main.cc.o -sys-header-deps -D __EMSCRIPTEN_SHARED_MEMORY__=1 -D EMSCRIPTEN -D __EMSCRIPTEN_major__=3 -D __EMSCRIPTEN_minor__=1 -D __EMSCRIPTEN_tiny__=20 -I /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/SDL -isysroot /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/c++/v1 -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/lib/clang/16.0.0/include -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/wasm32-emscripten -internal-isystem /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir='/Users/luao/Desktop/wasm_playground' -ferror-limit 19 -fvisibility default -pthread -fgnuc-version=4.2.1 -fcxx-exceptions -fignore-exceptions -fexceptions -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o CMakeFiles/wasm-playground.dir/src/main.cc.o -x c++ /Users/luao/Desktop/wasm_playground/src/main.cc
[build] clang -cc1 version 16.0.0 based upon LLVM 16.0.0git default target x86_64-apple-darwin21.5.0
[build] ignoring nonexistent directory "/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/wasm32-emscripten/c++/v1"
[build] ignoring nonexistent directory "/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/wasm32-emscripten"
[build] #include "..." search starts here:
[build] #include <...> search starts here:
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/SDL
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/compat
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include/c++/v1
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/llvm/lib/clang/16.0.0/include
[build] /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/include
[build] End of search list.
[build] [3/3 100% :: 3.188] Linking CXX executable wasm-playground.js
[build] em++: warning: USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code slowly, see https://github.com/WebAssembly/design/issues/1271 [-Wpthreads-mem-growth]
[build] "/Users/luao/.nvm/versions/node/v14.20.0/bin/node" /usr/local/Cellar/emscripten/3.1.20/libexec/src/compiler.js /var/folders/02/zyf_1nz556xdjk1qysf892b00000gn/T/tmpy_j3n9nt.json
[build] "/usr/local/Cellar/emscripten/3.1.20/libexec/llvm/bin/wasm-ld" -o wasm-playground.wasm -L/usr/local/opt/qt@5/lib CMakeFiles/wasm-playground.dir/src/gl_context_manager.cc.o CMakeFiles/wasm-playground.dir/src/main.cc.o -L/usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/lib/wasm32-emscripten /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/lib/wasm32-emscripten/libz.a /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/lib/wasm32-emscripten/crtbegin.o /usr/local/Cellar/emscripten/3.1.20/libexec/cache/sysroot/lib/wasm32-emscripten/crt1_proxy_main.o --whole-archive -lembind-rtti --no-whole-archive -lGL-mt-webgl2-ofb-full_es3 -lal -lhtml5 -lstubs-debug -lnoexit -lc-mt-debug -ldlmalloc-mt-debug -lcompiler_rt-mt -lc++-mt-noexcept -lc++abi-mt-noexcept -lsockets-mt -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --allow-undefined-file=/var/folders/02/zyf_1nz556xdjk1qysf892b00000gn/T/tmpsprwqokc.undefined --import-memory --shared-memory --export-if-defined=main --export-if-defined=_emscripten_thread_init --export-if-defined=_emscripten_thread_exit --export-if-defined=_emscripten_thread_crashed --export-if-defined=_emscripten_tls_init --export-if-defined=pthread_self --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=sbrk --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=__cxa_demangle --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=emscripten_get_sbrk_ptr --export=emscripten_dispatch_to_thread_ --export=_emscripten_thread_free_data --export=emscripten_main_browser_thread_id --export=emscripten_main_thread_process_queued_calls --export=emscripten_run_in_main_runtime_thread_js --export=emscripten_stack_set_limits --export=malloc --export=free --export=emscripten_webgl_make_context_current --export=emscripten_webgl_get_current_context --export-table -z stack-size=5242880 --initial-memory=16777216 --entry=_emscripten_proxy_main --max-memory=2147483648 --global-base=1024
[build] "/usr/local/Cellar/emscripten/3.1.20/libexec/binaryen/bin/wasm-emscripten-finalize" -g --bigint --no-dyncalls --no-legalize-javascript-ffi --check-stack-overflow --dwarf wasm-playground.wasm -o wasm-playground.wasm --detect-features
[build] "/Users/luao/.nvm/versions/node/v14.20.0/bin/node" /usr/local/Cellar/emscripten/3.1.20/libexec/src/compiler.js /var/folders/02/zyf_1nz556xdjk1qysf892b00000gn/T/tmphdm0u2em.json
[build] em++: warning: running limited binaryen optimizations because DWARF info requested (or indirectly required) [-Wlimited-postlink-optimizations]
[build] "/usr/local/Cellar/emscripten/3.1.20/libexec/binaryen/bin/wasm-opt" --safe-heap --strip-producers wasm-playground.wasm -o wasm-playground.wasm -g --mvp-features --enable-threads --enable-bulk-memory --enable-mutable-globals --enable-sign-ext
[build] "/Users/luao/.nvm/versions/node/v14.20.0/bin/node" /usr/local/Cellar/emscripten/3.1.20/libexec/tools/acorn-optimizer.js /var/folders/02/zyf_1nz556xdjk1qysf892b00000gn/T/emscripten_temp_3wjbc5ea/wasm-playground.js growableHeap
[build] "/Users/luao/.nvm/versions/node/v14.20.0/bin/node" /usr/local/Cellar/emscripten/3.1.20/libexec/tools/acorn-optimizer.js /var/folders/02/zyf_1nz556xdjk1qysf892b00000gn/T/emscripten_temp_3wjbc5ea/wasm-playground.js.pgrow.js safeHeap
[build] "/Users/luao/.nvm/versions/node/v14.20.0/bin/node" /usr/local/Cellar/emscripten/3.1.20/libexec/tools/preprocessor.js /var/folders/02/zyf_1nz556xdjk1qysf892b00000gn/T/emscripten_temp_3wjbc5ea/settings.js worker.js --expandMacros
[build] Build finished with exit code 0
```
**Repro steps:**
1. Create two canvases in one web page.
2. Create WebGLContext for each canvas on their `RenderingThread` (with PROXY_TO_PTHREAD, FULL_ES3, OFFSCREEN_FRAMEBUFFER, explicitSwapControl=EM_TRUE, renderViaOffscreenBackBuffer=EM_TRUE, proxyContextToMainThread=EMSCRIPTEN_WEBGL_CONTEXT_PROXY_ALWAYS).
3. Calling GL functions on each`RenderingThread`.
4. Repro repo: https://github.com/Autokaka/emscripten_issue17758_minimal_repro
**Expected Behavior:**
Two canvases would draw separately with their own WebGLContext.
**Actual Behavior:**
Sometimes the Expected Behavior would happen, but sometimes one canvas would draw nothing and throw many WebGL warnings in console.
**Extra Information:**
I tried to turn GL_DEBUG and TRACE_WEBGL_CALLS on and found that sometimes GL functions are called in one GLContext, but sometimes not. Seems that it is due to some potential threading issues that I don't know...


Contributor guide
Assessment
This issue has not been assessed yet.