From 5ac01db5f7fbdfe70c00756bbe3e3ca0e44d949e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 13 Jul 2025 06:00:00 +0000 Subject: [PATCH] Remove separate NCNN_WEBGPU option, enable automatically with emscripten + vulkan Co-authored-by: nihui <171016+nihui@users.noreply.github.com> --- CMakeLists.txt | 14 +++----------- cmake/ncnn_add_shader.cmake | 2 +- docs/webgpu-implementation-summary.md | 12 ++++++------ docs/webgpu-native-support.md | 19 ++++++++++--------- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 72bb55f7f..bb0e306a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/cmake/ncnn_add_shader.cmake b/cmake/ncnn_add_shader.cmake index 80be2dcb4..f983b8082 100644 --- a/cmake/ncnn_add_shader.cmake +++ b/cmake/ncnn_add_shader.cmake @@ -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} diff --git a/docs/webgpu-implementation-summary.md b/docs/webgpu-implementation-summary.md index 0b547ba4b..17efa1250 100644 --- a/docs/webgpu-implementation-summary.md +++ b/docs/webgpu-implementation-summary.md @@ -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. \ No newline at end of file diff --git a/docs/webgpu-native-support.md b/docs/webgpu-native-support.md index 8c55b0e7c..e556d2adf 100644 --- a/docs/webgpu-native-support.md +++ b/docs/webgpu-native-support.md @@ -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. \ No newline at end of file +All 300+ compute shaders will be automatically transformed during the build process when targeting emscripten with vulkan enabled. \ No newline at end of file