mirror of
https://github.com/Tencent/ncnn.git
synced 2026-10-23 00:59:08 +08:00
5ac01db5f7
Co-authored-by: nihui <171016+nihui@users.noreply.github.com>
2.2 KiB
2.2 KiB
WebGPU Native Support for NCNN
This implementation adds WebGPU native support to NCNN, allowing the reuse of existing Vulkan compute shaders with automatic conversion to WebGPU-compatible format.
Key Features
- Push Constant to Uniform Binding Conversion: Automatically transforms Vulkan push constants to WebGPU uniform bindings
- Modified psc Macro: Updated to use
float(x)==0for WebGPU compatibility instead ofx==0 - Seamless Integration: Reuses existing Vulkan shader infrastructure with minimal changes
Usage
WebGPU 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:
- Enable WebGPU when targeting emscripten + vulkan
- Transform all ~300+ compute shaders for WebGPU compatibility
- Apply the correct psc macro definition
Shader Transformation Example
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; };
Implementation Details
The implementation addresses the SPIR-V compilation issues mentioned in the GitHub issue:
- Error:
unknown SPIR-V storage class: 9- Fixed by converting push constants to uniform bindings - Error:
unhandled expression for ID 33- Fixed by changing psc macro to usefloat(x)==0
Files Modified
CMakeLists.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
Building
# 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.