emscripten-core / emscripten-core/emscripten
SDL2 and PROXY_TO_PTHREAD fails to get webgl2 context
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
I'm exploring running wasm (with rendering) in a web worker to improve interactivity. I feel like I'm using all the right link and compile options yet I can't seem to find a way forward. I've seen that the unit test `gl_only_in_pthread.c` works great, however it doesn't use SDL2. [VTK](https://github.com/kitware/vtk) uses `SDL2` when compiled to webassembly. Is that causing this problem? We can make VTK use low-level `html5_webgl.h` API instead of SDL2 if that would fix it.
Console shows this error:
```
ConesViewer.js?t=1698358746705:5078 Uncaught (in promise) DOMException: Failed to execute 'getContext' on 'HTMLCanvasElement': Cannot get context from a canvas that has transferred its control to offscreen.
at HTMLCanvasElement.fixedGetContext [as getContext] (http://localhost:3000/src/components/ConesViewer/ConesViewer.js?t=1698358746705:5078:21)
at Object.createContext (http://localhost:3000/src/components/ConesViewer/ConesViewer.js?t=1698358746705:5083:20)
at _eglCreateContext (http://localhost:3000/src/components/ConesViewer/ConesViewer.js?t=1698358746705:6129:19)
at __emscripten_receive_on_main_thread_js (http://localhost:3000/src/components/ConesViewer/ConesViewer.js?t=1698358746705:4743:17)
at ConesViewer.wasm.run_js_func (wasm://wasm/ConesViewer.wasm-04a84d3e:wasm-function[30755]:0x6d45c2)
at ConesViewer.wasm.call_then_finish_task (wasm://wasm/ConesViewer.wasm-04a84d3e:wasm-function[30753]:0x6d447c)
at ConesViewer.wasm.call_with_ctx (wasm://wasm/ConesViewer.wasm-04a84d3e:wasm-function[30748]:0x6d437c)
at ConesViewer.wasm.em_task_queue_execute (wasm://wasm/ConesViewer.wasm-04a84d3e:wasm-function[30724]:0x6d39ff)
at ConesViewer.wasm.receive_notification (wasm://wasm/ConesViewer.wasm-04a84d3e:wasm-function[30733]:0x6d3d21)
at ConesViewer.wasm.em_task_queue_execute (wasm://wasm/ConesViewer.wasm-04a84d3e:wasm-function[30724]:0x6d39ff)
```
**Version of emscripten/emsdk:**
```
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.46 (19607820c447a13fd8d0b7680c56148427d6e1b8)
clang version 18.0.0 (https://github.com/llvm/llvm-project 75501f53624de92aafce2f1da698b249a7293dc7)
Target: wasm32-unknown-emscripten
Thread model: posix
```
**Failing command line in full:**
```
ninja -nv
[1/2] /home/local/KHQ/jaswant.panchumarti/Documents/code/emsdk/upstream/emscripten/em++ -DGLEW_STATIC -Dkiss_fft_scalar=double -DvtkRenderingCore_AUTOINIT_INCLUDE=\"/work/src/vtkWasmBenchmarks/out/CMakeFiles/vtkModuleAutoInit_84c59f7653f53de7d0fb39034b6d7950.h\" -isystem /work/src/vtk/install/include/vtk-9.3 -isystem /work/src/vtk/install/include/vtk-9.3/vtknlohmannjson/include -isystem /home/local/KHQ/jaswant.panchumarti/Documents/code/emsdk/emscripten/main/cache/sysroot/include/webgpu -pthread -O0 -g3 -sUSE_SDL=2 -std=gnu++14 -MD -MT cpp/ConesViewer/CMakeFiles/ConesViewer.dir/ConesViewer.cxx.o -MF cpp/ConesViewer/CMakeFiles/ConesViewer.dir/ConesViewer.cxx.o.d -o cpp/ConesViewer/CMakeFiles/ConesViewer.dir/ConesViewer.cxx.o -c /work/src/vtkWasmBenchmarks/cpp/ConesViewer/ConesViewer.cxx
[2/2] : && /home/local/KHQ/jaswant.panchumarti/Documents/code/emsdk/upstream/emscripten/em++ -sMODULARIZE=1 -sEXPORT_ES6=1 -pthread -sPROXY_TO_PTHREAD=1 -sPTHREAD_POOL_SIZE=2 -sOFFSCREENCANVAS_SUPPORT -sOFFSCREEN_FRAMEBUFFER -sEXPORT_NAME=createConesViewerModule -sENVIRONMENT=web,worker -sALLOW_MEMORY_GROWTH=1 -sSINGLE_FILE=1 -sMAXIMUM_MEMORY=4GB "-sEXPORTED_RUNTIME_METHODS=['ENV']" -sASSERTIONS=1 -sDEMANGLE_SUPPORT=1 -O0 -g3 -sUSE_SDL=2 -s FULL_ES3=1 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 -sUSE_WEBGPU=1 cpp/ConesViewer/CMakeFiles/ConesViewer.dir/ConesViewer.cxx.o -o /work/src/vtkWasmBenchmarks/src/components/ConesViewer/ConesViewer.js /work/src/vtk/install/lib/libvtkFiltersSources-9.3.a /work/src/vtk/install/lib/libvtkInteractionStyle-9.3.a /work/src/vtk/install/lib/libvtkRenderingOpenGL2-9.3.a /work/src/vtk/install/lib/libvtkRenderingWebGPU-9.3.a /work/src/vtk/install/lib/libvtkRenderingUI-9.3.a /work/src/vtk/install/lib/libvtkFiltersExtraction-9.3.a /work/src/vtk/install/lib/libvtkFiltersStatistics-9.3.a /work/src/vtk/install/lib/libvtkParallelDIY-9.3.a /work/src/vtk/install/lib/libvtkParallelCore-9.3.a /work/src/vtk/install/lib/libvtkIOLegacy-9.3.a /work/src/vtk/install/lib/libvtkIOXML-9.3.a /work/src/vtk/install/lib/libvtkIOXMLParser-9.3.a /work/src/vtk/install/lib/libvtkexpat-9.3.a /work/src/vtk/install/lib/libvtkIOCore-9.3.a /work/src/vtk/install/lib/libvtkdoubleconversion-9.3.a /work/src/vtk/install/lib/libvtklz4-9.3.a /work/src/vtk/install/lib/libvtklzma-9.3.a /work/src/vtk/install/lib/libvtkIOImage-9.3.a /work/src/vtk/install/lib/libvtkDICOMParser-9.3.a /work/src/vtk/install/lib/libvtkmetaio-9.3.a /work/src/vtk/install/lib/libvtkpng-9.3.a /work/src/vtk/install/lib/libvtktiff-9.3.a /work/src/vtk/install/lib/libvtkjpeg-9.3.a /work/src/vtk/install/lib/libvtkzlib-9.3.a /work/src/vtk/install/lib/libvtkRenderingHyperTreeGrid-9.3.a /work/src/vtk/install/lib/libvtkFiltersHyperTree-9.3.a /work/src/vtk/install/lib/libvtkFiltersHybrid-9.3.a /work/src/vtk/install/lib/libvtkImagingSources-9.3.a /work/src/vtk/install/lib/libvtkImagingCore-9.3.a /work/src/vtk/install/lib/libvtkglew-9.3.a /work/src/vtk/install/lib/libvtkRenderingCore-9.3.a /work/src/vtk/install/lib/libvtkFiltersSources-9.3.a /work/src/vtk/install/lib/libvtkFiltersGeneral-9.3.a /work/src/vtk/install/lib/libvtkCommonComputationalGeometry-9.3.a /work/src/vtk/install/lib/libvtkFiltersVerdict-9.3.a /work/src/vtk/install/lib/libvtkverdict-9.3.a /work/src/vtk/install/lib/libvtkCommonColor-9.3.a /work/src/vtk/install/lib/libvtkFiltersGeometry-9.3.a /work/src/vtk/install/lib/libvtkFiltersCore-9.3.a /work/src/vtk/install/lib/libvtkCommonExecutionModel-9.3.a /work/src/vtk/install/lib/libvtkCommonDataModel-9.3.a /work/src/vtk/install/lib/libvtkpugixml-9.3.a /work/src/vtk/install/lib/libvtkCommonTransforms-9.3.a /work/src/vtk/install/lib/libvtkCommonMisc-9.3.a /work/src/vtk/install/lib/libvtkCommonMath-9.3.a /work/src/vtk/install/lib/libvtkkissfft-9.3.a -lm /work/src/vtk/install/lib/libvtkCommonSystem-9.3.a /work/src/vtk/install/lib/libvtkCommonCore-9.3.a /work/src/vtk/install/lib/libvtktoken-9.3.a /work/src/vtk/install/lib/libvtksys-9.3.a /work/src/vtk/install/lib/libvtkfmt-9.3.a && :
```
Contributor guide
Assessment
This issue has not been assessed yet.