Browse Source

Remove separate NCNN_WEBGPU option, enable automatically with emscripten + vulkan

Co-authored-by: nihui <171016+nihui@users.noreply.github.com>
copilot/fix-5974
copilot-swe-agent[bot] 11 months ago
parent
commit
5ac01db5f7
4 changed files with 20 additions and 27 deletions
  1. +3
    -11
      CMakeLists.txt
  2. +1
    -1
      cmake/ncnn_add_shader.cmake
  3. +6
    -6
      docs/webgpu-implementation-summary.md
  4. +10
    -9
      docs/webgpu-native-support.md

+ 3
- 11
CMakeLists.txt View File

@@ -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)


+ 1
- 1
cmake/ncnn_add_shader.cmake View File

@@ -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}


+ 6
- 6
docs/webgpu-implementation-summary.md View File

@@ -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
- 9
docs/webgpu-native-support.md View File

@@ -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.

Loading…
Cancel
Save