Implementation:Tencent Ncnn CMake Vulkan Enable
| Knowledge Sources | |
|---|---|
| Domains | GPU_Computing, Build_System |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
External tool documentation for enabling Vulkan GPU compute in the ncnn CMake build system.
Description
This is an External Tool Doc documenting the CMake configuration required to build ncnn with Vulkan support. The primary option is NCNN_VULKAN=ON in the root CMakeLists.txt. This triggers:
- Detection of Vulkan SDK or fallback to the built-in simplevk loader
- Compilation of GLSL compute shaders to SPIR-V via bundled glslang
- Inclusion of Vulkan-specific source files (gpu.cpp, command.cpp, pipeline.cpp, etc.)
- Linking against the Vulkan driver library
The NCNN_SIMPLEVK option (enabled by default when Vulkan SDK is not found) uses ncnn's built-in Vulkan function loader, eliminating the Vulkan SDK dependency for deployment.
Usage
Pass -DNCNN_VULKAN=ON when configuring ncnn with CMake. The build system handles Vulkan SDK detection, shader compilation, and linking automatically.
Code Reference
Source Location
- Repository: ncnn
- File: CMakeLists.txt (root), src/CMakeLists.txt (library build)
- Lines: CMakeLists.txt:L1-892 (root with NCNN_VULKAN option), src/CMakeLists.txt:L1-806 (Vulkan shader compilation)
Signature
# Basic Vulkan build
mkdir build && cd build
cmake -DNCNN_VULKAN=ON ..
make -j$(nproc)
# With explicit Vulkan SDK path
cmake -DNCNN_VULKAN=ON \
-DVulkan_INCLUDE_DIR=/path/to/vulkan/include \
-DVulkan_LIBRARY=/path/to/libvulkan.so \
..
# Using built-in simplevk loader (no SDK needed)
cmake -DNCNN_VULKAN=ON -DNCNN_SIMPLEVK=ON ..
Import
# CMake — no code import needed
# The NCNN_VULKAN preprocessor macro is defined automatically
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| NCNN_VULKAN | CMake BOOL | Yes | ON to enable Vulkan GPU compute |
| Vulkan_INCLUDE_DIR | CMake PATH | No | Vulkan SDK headers (auto-detected) |
| Vulkan_LIBRARY | CMake PATH | No | Vulkan driver library (auto-detected) |
| NCNN_SIMPLEVK | CMake BOOL | No | Use built-in Vulkan loader (auto-enabled) |
Outputs
| Name | Type | Description |
|---|---|---|
| libncnn.a / libncnn.so | Library | ncnn library with Vulkan compute support |
| SPIR-V shaders | Embedded | Compiled GPU shaders embedded in the library |
Usage Examples
Linux Desktop Build
git clone https://github.com/Tencent/ncnn.git
cd ncnn
mkdir build && cd build
cmake -DNCNN_VULKAN=ON \
-DCMAKE_BUILD_TYPE=Release \
..
make -j$(nproc)
Android Cross-Compilation
cmake -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=android-24 \
-DNCNN_VULKAN=ON \
..
make -j$(nproc)