Issue with Using pthread in WASM

Hello,
I’m trying to compile VTK-9.5.0 and use the resulting build as a library for compiling my own C++ code targeting WebAssembly.

During the VTK build process, I followed the instructions from the official guide (Building using emscripten for WebAssembly - VTK documentation) and enabled the option -DVTK_WEBASSEMBLY_THREADS:BOOL=ON.

When compiling my C++ project with this VTK build, I added the following flags in my CMakeLists.txt:
-pthread and -sPROXY_TO_PTHREAD=1

However, I encountered the following error:

–shared-memory is disallowed by vtkSMPThreadPool.cxx.o because it was not compiled with ‘atomics’ or ‘bulk-memory’ features.

Is there a specific flag or configuration I may have missed when building VTK to support threads in WASM?

Any help would be greatly appreciated!

Can you search the build.ninja of VTK for instances of -pthread? I want confirm that the generated file has that flag for the vtkSMPThreadPool.cxx.o. Sometimes, setting the option after initial configure does not work.

Thank you for your reply! I was previously using MinGW, but after switching to Ninja, the issue was resolved.

Now I have another question related to WebGPU. I’d like to use both multithreading and WebGPU together. Following the discussion here: Problems when compilate VTK build for wasm - #4 by jaswantp, I integrated Demdawnwebgpu_DIR into my build, and then encountered the same error as before, which I suspect is due to the WebGPU build not being compiled with the -pthread flag.

–shared-memory is disallowed by webgpu.cpp.o because it was not compiled with ‘atomics’ or ‘bulk-memory’ features.

Is there a version of the webgpu that supports multithreading? Or is there any source code and guidance on how to build webgpu properly with multithreading support?

Thanks again for your help!

Try a recent commit of vtk master and EMSDK >=4.0.10 We recently updated VTK to use emdawnwebgpu via the --use-port flag which fixes that shared-memory disallowed by webgpu.cpp.o error.

1 Like

it worked, thank you!

1 Like

Apologies for bothering you again, but I have encountered another difficult issue:

I have attempted to compile VTK using the code below

emcmake cmake -S F:\kuaite\VTK-master\VTK-master -B . -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS:BOOL=OFF -DVTK_ENABLE_WEBGPU:BOOL=ON 
cmake --build . 
cmake --install . --prefix .\installRelease

and followed the README instructions to compile the ConeMultiBackend example. During the compilation process, I encountered several warnings and errors:

[1/1] Linking CXX executable ConeMultiBackend.js
FAILED: [code=1] ConeMultiBackend.js
C:\WINDOWS\system32\cmd.exe /C "cd . && F:\kuaite\emscripten\emsdk\upstream\emscripten\em++.bat  -sWASM=1 -sMODULARIZE=1 -sASYNCIFY=1 -sALLOW_MEMORY_GROWTH=1 -sEXPORT_NAME=createConeMultiBackendModule "-sEXPORTED_RUNTIME_METHODS=['ENV']" -fwasm-exceptions -sEXCEPTION_STACK_TRACES=1 -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$getWasmTableEntry --js-library=F:/kuaite/VTK-master/VTK-master/Rendering/UI/vtkWebAssemblyRenderWindowInteractor.js -s FULL_ES3=1 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 --use-port=emdawnwebgpu @CMakeFiles\ConeMultiBackend.rsp -o ConeMultiBackend.js && C:\WINDOWS\system32\cmd.exe /C "cd /D F:\kuaite\VTK-master\VTK-master\Examples\Emscripten\Cxx\ConeMultiBackend\out\build && D:\cmake\bin\cmake.exe -E copy_directory F:/kuaite/VTK-master/VTK-master/Examples/Emscripten/Cxx/ConeMultiBackend/web F:/kuaite/VTK-master/VTK-master/Examples/Emscripten/Cxx/ConeMultiBackend/out/build""
em++: warning: ASYNCIFY=1 is not compatible with -fwasm-exceptions. Parts of the program that mix ASYNCIFY and exceptions will not compile. [-Wemcc]
unexpected expression type
UNREACHABLE executed at C:\b\s\w\ir\cache\builder\emscripten-releases\binaryen\src\passes\Asyncify.cpp:1146!
unexpected expression type
UNREACHABLE executed at em++: error: ''F:/kuaite/emscripten/emsdk/upstream\bin\wasm-opt' --asyncify --pass-arg=asyncify-asserts --pass-arg=asyncify-propagate-addlist '--pass-arg=asyncify-imports@env.invoke_*,env.__asyncjs__*,*.$asyncLoad,*.fd_sync,*.emscripten_promise_await,*.emscripten_async_load_script,*.emscripten_async_wget_data,*.emscripten_idb_load,*.emscripten_idb_store,*.emscripten_idb_delete,*.emscripten_idb_exists,*.emscripten_idb_clear,*.emscripten_idb_load_blob,*.emscripten_idb_store_blob,*.emscripten_sleep,*.emscripten_wget_data,*.emscripten_scan_registers,*._load_secondary_module,*.emscripten_fiber_swap,*.SDL_Delay,*.emwgpuWaitAny' ConeMultiBackend.wasm -o ConeMultiBackend.wasm --mvp-features --enable-bulk-memory --enable-bulk-memory-opt --enable-call-indirect-overlong --enable-exception-handling --enable-multivalue --enable-mutable-globals --enable-nontrapping-float-to-int --enable-reference-types --enable-sign-ext' failed (returned 3221226505)
ninja: build stopped: subcommand failed.

It seems that there is a conflict between ASYNCIFY=1 and -fwasm-exceptions. I attempted to remove the -fwasm-exceptions flag, but VTK requires exception handling during compilation, which necessitates Emscripten support. However, I recall reading somewhere that WebGPU requires ASYNCIFY support (and indeed, removing ASYNCIFY caused new errors).

Are there any compilation examples? Thanks again!

That example needs updated. Replace “-sASYNCIFY=1” with

"-sJSPI=1"
"-sJSPI_EXPORTS=['__wasm_call_ctors']"
1 Like

Note that there is a bug that causes the example to not start correctly. interactor->Start() is now non-blocking and because of that main() exits and interactor goes out of scope, gets destroyed and the vtkWebAssemblyRenderWindowInteractor::ProcessEvents gets called on a non-existent interactor. I will make a fix shortly.

1 Like

Fix for interactor start and example - https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12258

1 Like

Thank you so much for the fix! The example now works smoothly.
It seems the new version introduced some changes, i will try to exploring them