Co-authored-by: nihui <171016+nihui@users.noreply.github.com>copilot/fix-5974
| @@ -77,7 +77,6 @@ option(NCNN_PIXEL_AFFINE "warp affine image pixel" ON) | |||
| option(NCNN_PIXEL_DRAWING "draw basic figure and text" ON) | |||
| option(NCNN_CMAKE_VERBOSE "print verbose cmake messages" OFF) | |||
| option(NCNN_VULKAN "vulkan compute support" OFF) | |||
| option(NCNN_WEBGPU "webgpu compute support" OFF) | |||
| option(NCNN_SIMPLEVK "minimal in-house vulkan loader" ON) | |||
| option(NCNN_SYSTEM_GLSLANG "use system glslang library" OFF) | |||
| option(NCNN_RUNTIME_CPU "runtime dispatch cpu routines" ON) | |||
| @@ -832,16 +831,9 @@ if(NCNN_VULKAN) | |||
| endif() | |||
| endif() | |||
| if(NCNN_WEBGPU) | |||
| # WebGPU native support reuses Vulkan shader compilation infrastructure | |||
| # but requires different runtime and uniform handling | |||
| message(STATUS "WebGPU native support enabled") | |||
| # Enable Vulkan infrastructure for shader compilation if not already enabled | |||
| if(NOT NCNN_VULKAN) | |||
| message(STATUS "Enabling Vulkan infrastructure for WebGPU shader compilation") | |||
| set(NCNN_VULKAN ON) | |||
| endif() | |||
| # Automatically enable WebGPU when compiling to wasm target with emscripten if Vulkan is enabled | |||
| if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NCNN_VULKAN) | |||
| message(STATUS "WebGPU native support enabled (emscripten + vulkan)") | |||
| # WebGPU specific configuration flags | |||
| add_definitions(-DNCNN_WEBGPU=1) | |||
| @@ -3,7 +3,7 @@ macro(ncnn_add_shader NCNN_SHADER_SRC) | |||
| get_filename_component(NCNN_SHADER_SRC_NAME_WE ${NCNN_SHADER_SRC} NAME_WE) | |||
| set(NCNN_SHADER_COMP_HEADER ${CMAKE_CURRENT_BINARY_DIR}/layer/vulkan/shader/${NCNN_SHADER_SRC_NAME_WE}.comp.hex.h) | |||
| if(NCNN_WEBGPU) | |||
| if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NCNN_VULKAN) | |||
| # Use WebGPU shader transformation for push constant -> uniform binding conversion | |||
| add_custom_command( | |||
| OUTPUT ${NCNN_SHADER_COMP_HEADER} | |||
| @@ -21,14 +21,14 @@ The original issue identified two critical problems when trying to compile Vulka | |||
| ### 1. Build System Changes | |||
| **CMakeLists.txt**: | |||
| - Added `NCNN_WEBGPU` option | |||
| - Automatically enables Vulkan infrastructure when WebGPU is enabled | |||
| - Automatic WebGPU detection when targeting emscripten with Vulkan enabled | |||
| - Uses `CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NCNN_VULKAN` condition | |||
| - Sets `NCNN_WEBGPU=1` preprocessor define | |||
| ### 2. Shader Preprocessing Pipeline | |||
| **cmake/ncnn_add_shader.cmake**: | |||
| - Added conditional logic to use WebGPU shader transformation when `NCNN_WEBGPU=ON` | |||
| - Added conditional logic to use WebGPU shader transformation when targeting emscripten + vulkan | |||
| - Uses `ncnn_generate_webgpu_shader_header.cmake` for transformation | |||
| **cmake/ncnn_generate_webgpu_shader_header.cmake**: | |||
| @@ -72,9 +72,9 @@ if (gx >= psc(w)) return; // psc(w) = (float(w)==0?p.w:w) | |||
| ## Usage | |||
| ```bash | |||
| # Enable WebGPU native support | |||
| cmake .. -DNCNN_WEBGPU=ON | |||
| make -j$(nproc) | |||
| # 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. | |||
| @@ -10,15 +10,16 @@ This implementation adds WebGPU native support to NCNN, allowing the reuse of ex | |||
| ## Usage | |||
| Enable WebGPU support by adding `-DNCNN_WEBGPU=ON` to your CMake configuration: | |||
| WebGPU support is automatically enabled when compiling to WebAssembly (wasm) target with emscripten and Vulkan support is enabled: | |||
| ```bash | |||
| cmake .. -DNCNN_WEBGPU=ON | |||
| # Use emscripten toolchain with Vulkan enabled | |||
| emcmake cmake .. -DNCNN_VULKAN=ON | |||
| ``` | |||
| This will automatically: | |||
| - Enable Vulkan infrastructure for shader compilation | |||
| - Transform all ~300+ compute shaders for WebGPU compatibility | |||
| - Enable WebGPU when targeting emscripten + vulkan | |||
| - Transform all ~300+ compute shaders for WebGPU compatibility | |||
| - Apply the correct psc macro definition | |||
| ## Shader Transformation Example | |||
| @@ -57,7 +58,7 @@ The implementation addresses the SPIR-V compilation issues mentioned in the GitH | |||
| ## Files Modified | |||
| - `CMakeLists.txt`: Added NCNN_WEBGPU option | |||
| - `CMakeLists.txt`: Automatic WebGPU detection for emscripten + vulkan | |||
| - `src/gpu.cpp`: Updated psc macro for WebGPU compatibility | |||
| - `cmake/ncnn_add_shader.cmake`: Added WebGPU shader preprocessing path | |||
| - `cmake/ncnn_generate_webgpu_shader_header.cmake`: New shader transformation logic | |||
| @@ -65,10 +66,10 @@ The implementation addresses the SPIR-V compilation issues mentioned in the GitH | |||
| ## Building | |||
| ```bash | |||
| # Standard build with WebGPU support | |||
| # Standard build with WebGPU support using emscripten | |||
| mkdir build && cd build | |||
| cmake .. -DNCNN_WEBGPU=ON -DNCNN_BUILD_TESTS=ON | |||
| make -j$(nproc) | |||
| 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. | |||
| All 300+ compute shaders will be automatically transformed during the build process when targeting emscripten with vulkan enabled. | |||