This implementation adds WebGPU native support to NCNN, allowing the reuse of existing Vulkan compute shaders with automatic conversion to WebGPU-compatible format.
float(x)==0 for WebGPU compatibility instead of x==0WebGPU support is automatically enabled when compiling to WebAssembly (wasm) target with emscripten and Vulkan support is enabled:
# Use emscripten toolchain with Vulkan enabled
emcmake cmake .. -DNCNN_VULKAN=ON
This will automatically:
Vulkan (original):
layout (push_constant) uniform parameter
{
int dims;
int w;
int h;
int c;
int cstep;
} p;
WebGPU (transformed):
struct parameter
{
int dims;
int w;
int h;
int c;
int cstep;
};
layout (binding = 1) uniform parameter_blob { parameter p; };
The implementation addresses the SPIR-V compilation issues mentioned in the GitHub issue:
unknown SPIR-V storage class: 9 - Fixed by converting push constants to uniform bindingsunhandled expression for ID 33 - Fixed by changing psc macro to use float(x)==0CMakeLists.txt: Automatic WebGPU detection for emscripten + vulkansrc/gpu.cpp: Updated psc macro for WebGPU compatibilitycmake/ncnn_add_shader.cmake: Added WebGPU shader preprocessing pathcmake/ncnn_generate_webgpu_shader_header.cmake: New shader transformation logic# Standard build with WebGPU support using emscripten
mkdir build && cd build
emcmake cmake .. -DNCNN_VULKAN=ON -DNCNN_BUILD_TESTS=ON
emmake make -j$(nproc)
All 300+ compute shaders will be automatically transformed during the build process when targeting emscripten with vulkan enabled.