首先安装一些依赖:
sudo install build-essentials git cmake libprotobuf-dev protobuf-compiler libvulkan-dev glslang-tools vulkan-tools libopencv-dev
注意: 只有ubuntu 19.0以上的用户才能通过install 安装glslang-tools vulkan-tools,低于19.0 的版本通过源码安装
一: Vulkan SDK 下载及安装
1. 到vulkan官网下载SDK: https://vulkan.lunarg.com/sdk/home
2. 解压至某一路径下:
tar -xzf vulkansdk-linux-x86_64-1.2.148.1.tar.gz
3. 安装相应的库,否则后面报vulkan的错
sudo apt install vulkan-utils
4. 添加环境变量
sudo gedit ~/.bashrc
写入以下:
export Vulkan_LIBRARY=/home/XXX/vulkan_1.2.148.1/x86_64/lib
export Vulkan_INCLUDE_DIR=$Vulkan_INCLUDE_DIR:/home/XXX/vulkan_1.2.148.1/x86_64/include
export Vulkan_BIN=$Vulkan_BIN:/home/XXX/vulkan_1.2.148.1/x86_64/bin
export PATH=$PATH:$Vulkan_LIBRARY
export PATH=$PATH:$Vulkan_INCLUDE_DIR
export PATH=$PATH:$Vulkan_BIN
5. 检验是否添加成功
vulkaninfo
二: glslang 编译安装
git clone https://github.com/KhronosGroup/glslang.git
cd glslang
目前(2018.12.17)的正式版,最稳定的版本,试过最新的版本,编译部分代码存在问题
git checkout 7.10.2984
# 下载 spirv-tools 部分的功能代码
python update_glslang_sources.py
mkdir build
cd build
cmake ..
make
sudo make install (默认安装在/usr/local/bin)
可以参考这个链接: https://www.mobibrw.com/2019/22463
三: 编译NCNN
cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_VULKAN=ON -DNCNN_SYSTEM_GLSLANG=ON -NCNN_OPENCV=ON -NCNN_BUILD_EXAMPLES=ON ..
若报错: The submodules were not downloaded! Please update submodules with "git submodule update --init" and try again.
则在build 目录外 git submodule update --init 后, 再重新cmake
make -j8
make install
四: 在项目中使用vulkan 时,需要添加以下的库: "-lncnn -lvulkan -lglslang -lOSDependent -lSPIRV -lOGLCompiler",否则报错
过程中报错:
1. 在cmake 时会报错: Could not find a package configuration file provided by "Vulkan" with any of the following names:
VulkanConfig.cmake
vulkan-config.cmake
升级你的cmake 版本即可解决
2. 在cmake 时会报错: The submodels were not download! NCNN_ONLINE_SPIRV will be turned off.
please update submodels with "git submodel update --init" and try again.
可以参考这个解决: https://github.com/Tencent/ncnn/issues/2130