This implementation successfully adds WebGPU native support to NCNN by reusing existing Vulkan compute shader infrastructure with automatic transformations for WebGPU compatibility.
The original issue identified two critical problems when trying to compile Vulkan shaders for WebGPU:
SPIR-V Storage Class Error: unknown SPIR-V storage class: 9
layout (push_constant) uniform to layout (binding = N) uniformSpecialization Constant Expression Error: unhandled expression for ID 33
float(x)==0 instead of x==0 in the psc macroCMakeLists.txt:
CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NCNN_VULKAN conditionNCNN_WEBGPU=1 preprocessor definecmake/ncnn_add_shader.cmake:
ncnn_generate_webgpu_shader_header.cmake for transformationcmake/ncnn_generate_webgpu_shader_header.cmake:
layout (push_constant) uniform X { ... } Y; → struct X { ... }; layout (binding = 1) uniform X_blob { X Y; };src/gpu.cpp:
(float(x)==0?p.x:x) instead of (x==0?p.x:x)✅ All shader transformations working: 300+ compute shaders successfully transformed
✅ Push constant conversion: Correctly converts to uniform bindings
✅ psc macro compatibility: Uses float casting for WebGPU
✅ Automated testing: Created verification script that passes all checks
Before (Vulkan):
layout (push_constant) uniform parameter {
int dims, w, h, c, cstep;
} p;
if (gx >= psc(w)) return; // psc(w) = (w==0?p.w:w)
After (WebGPU):
struct parameter {
int dims, w, h, c, cstep;
};
layout (binding = 1) uniform parameter_blob { parameter p; };
if (gx >= psc(w)) return; // psc(w) = (float(w)==0?p.w:w)
# Enable WebGPU native support with emscripten
emcmake cmake .. -DNCNN_VULKAN=ON
emmake make -j$(nproc)
This implementation provides a solid foundation for WebGPU native support while maintaining compatibility with existing Vulkan infrastructure.