编译ncnn+vulkan
·
编译ncnn
git clone https://github.com/Tencent/ncnn.git
修改cmake install
找if(NCNN_INSTALL_SDK),添加:
if(NCNN_INSTALL_SDK)
include(GNUInstallDirs)
install(TARGETS ncnn EXPORT ncnn
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(FILES
allocator.h
benchmark.h
blob.h
c_api.h
command.h
cpu.h
datareader.h
gpu.h
layer.h
layer_shader_type.h
layer_type.h
mat.h
modelbin.h
net.h
option.h
paramdict.h
pipeline.h
pipelinecache.h
simpleocv.h
simpleomp.h
simplestl.h
vulkan_header_fix.h
${CMAKE_CURRENT_BINARY_DIR}/ncnn_export.h
${CMAKE_CURRENT_BINARY_DIR}/layer_shader_type_enum.h
${CMAKE_CURRENT_BINARY_DIR}/layer_type_enum.h
${CMAKE_CURRENT_BINARY_DIR}/platform.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ncnn
)
install(EXPORT ncnn DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ncnn)
configure_file(${CMAKE_CURRENT_LIST_DIR}/../cmake/ncnnConfig.cmake.in ncnnConfig.cmake @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ncnnConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ncnn)
# pkgconfig
configure_file(ncnn.pc.in ${CMAKE_CURRENT_BINARY_DIR}/ncnn.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ncnn.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
# 安装Vulkan的库文件
if(TARGET Vulkan::Vulkan)
# 获取Vulkan库路径
get_target_property(VULKAN_LIB Vulkan::Vulkan IMPORTED_LOCATION)
if(VULKAN_LIB)
# 安装动态库
install(FILES ${VULKAN_LIB} DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
# Windows下可能需要额外处理DLL
if(WIN32)
# 查找vulkan-1.dll
get_target_property(VULKAN_BIN Vulkan::Vulkan IMPORTED_LOCATION)
if(VULKAN_BIN AND EXISTS "${VULKAN_BIN}")
get_filename_component(VULKAN_DLL "${VULKAN_BIN}" NAME)
install(FILES "${VULKAN_BIN}" DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
endif()
endif()
endif()
1.配置cmake
cmake -B build -DNCNN_SHARED_LIB=ON -DNCNN_INSTALL_SDK=ON -DNCNN_PIXEL_ROTATE=OFF -DNCNN_PIXEL_AFFINE=OFF -DNCNN_PIXEL_DRAWING=OFF -DNCNN_VULKAN=ON -DNCNN_VULKAN_ONLINE_SPIRV=ON -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_DISABLE_RTTI=ON -DNCNN_DISABLE_EXCEPTION=ON -DNCNN_INT8=OFF -DWITH_LAYER_absval=OFF -DWITH_LAYER_argmax=OFF -DWITH_LAYER_batchnorm=OFF -DWITH_LAYER_bias=OFF -DWITH_LAYER_bnll=OFF -DWITH_LAYER_concat=ON -DWITH_LAYER_convolution=ON -DWITH_LAYER_crop=ON -DWITH_LAYER_deconvolution=ON -DWITH_LAYER_dropout=OFF -DWITH_LAYER_eltwise=ON -DWITH_LAYER_elu=OFF -DWITH_LAYER_embed=OFF -DWITH_LAYER_exp=OFF -DWITH_LAYER_flatten=ON -DWITH_LAYER_innerproduct=ON -DWITH_LAYER_input=ON -DWITH_LAYER_log=OFF -DWITH_LAYER_lrn=OFF -DWITH_LAYER_memorydata=OFF -DWITH_LAYER_mvn=OFF -DWITH_LAYER_pooling=ON -DWITH_LAYER_power=OFF -DWITH_LAYER_prelu=ON -DWITH_LAYER_proposal=OFF -DWITH_LAYER_reduction=OFF -DWITH_LAYER_relu=ON -DWITH_LAYER_reshape=OFF -DWITH_LAYER_roipooling=OFF -DWITH_LAYER_scale=OFF -DWITH_LAYER_sigmoid=ON -DWITH_LAYER_slice=ON -DWITH_LAYER_softmax=OFF -DWITH_LAYER_split=ON -DWITH_LAYER_spp=OFF -DWITH_LAYER_tanh=OFF -DWITH_LAYER_threshold=OFF -DWITH_LAYER_tile=OFF -DWITH_LAYER_rnn=OFF -DWITH_LAYER_lstm=OFF -DWITH_LAYER_binaryop=ON -DWITH_LAYER_unaryop=ON -DWITH_LAYER_convolutiondepthwise=OFF -DWITH_LAYER_padding=ON -DWITH_LAYER_squeeze=OFF -DWITH_LAYER_expanddims=OFF -DWITH_LAYER_normalize=OFF -DWITH_LAYER_permute=OFF -DWITH_LAYER_priorbox=OFF -DWITH_LAYER_detectionoutput=OFF -DWITH_LAYER_interp=ON -DWITH_LAYER_deconvolutiondepthwise=OFF -DWITH_LAYER_shufflechannel=OFF -DWITH_LAYER_instancenorm=OFF -DWITH_LAYER_clip=ON -DWITH_LAYER_reorg=OFF -DWITH_LAYER_yolodetectionoutput=OFF -DWITH_LAYER_quantize=OFF -DWITH_LAYER_dequantize=OFF -DWITH_LAYER_yolov3detectionoutput=OFF -DWITH_LAYER_psroipooling=OFF -DWITH_LAYER_roialign=OFF -DWITH_LAYER_packing=ON -DWITH_LAYER_requantize=OFF -DWITH_LAYER_cast=ON -DWITH_LAYER_hardsigmoid=OFF -DWITH_LAYER_selu=OFF -DWITH_LAYER_hardswish=OFF -DWITH_LAYER_noop=OFF -DWITH_LAYER_pixelshuffle=ON -DWITH_LAYER_deepcopy=OFF -DWITH_LAYER_mish=OFF -DWITH_LAYER_statisticspooling=OFF -DWITH_LAYER_swish=OFF -DWITH_LAYER_gemm=OFF -DWITH_LAYER_groupnorm=OFF -DWITH_LAYER_layernorm=OFF -DWITH_LAYER_softplus=OFF -DWITH_LAYER_gru=OFF -DWITH_LAYER_multiheadattention=OFF -DWITH_LAYER_gelu=OFF -DWITH_LAYER_convolution1d=OFF -DWITH_LAYER_pooling1d=OFF -DWITH_LAYER_convolutiondepthwise1d=OFF -DWITH_LAYER_convolution3d=OFF -DWITH_LAYER_convolutiondepthwise3d=OFF -DWITH_LAYER_pooling3d=OFF -DWITH_LAYER_matmul=OFF -DWITH_LAYER_deconvolution1d=OFF -DWITH_LAYER_deconvolutiondepthwise1d=OFF -DWITH_LAYER_deconvolution3d=OFF -DWITH_LAYER_deconvolutiondepthwise3d=OFF -DWITH_LAYER_einsum=OFF -DWITH_LAYER_deformableconv2d=OFF
2. 编译
cmake --build build --config debug
cmake --build build --config release
3. 安装
cmake --install build
4. cmake导入
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/install" ${CMAKE_PREFIX_PATH})
find_package(ncnn REQUIRED)
5. 编译原始项目
cmake -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build build --config RelWithDebInfo
cmake --install build --config RelWithDebInfo
编译ncnn+vulkan
修改cmake即可
cmake_policy(SET CMP0091 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0091 NEW)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
project(rife-ncnn-vulkan)
cmake_minimum_required(VERSION 3.9)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE release CACHE STRING "Choose the type of build" FORCE)
endif()
option(USE_SYSTEM_NCNN "build with system libncnn" OFF)
option(USE_SYSTEM_WEBP "build with system libwebp" OFF)
option(USE_STATIC_MOLTENVK "link moltenvk static library" OFF)
find_package(Threads)
find_package(OpenMP)
find_package(Vulkan REQUIRED)
macro(rife_add_shader SHADER_SRC)
get_filename_component(SHADER_SRC_NAME_WE ${SHADER_SRC} NAME_WE)
set(SHADER_COMP_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${SHADER_SRC_NAME_WE}.comp.hex.h)
add_custom_command(
OUTPUT ${SHADER_COMP_HEADER}
COMMAND ${CMAKE_COMMAND} -DSHADER_SRC=${CMAKE_CURRENT_SOURCE_DIR}/${SHADER_SRC} -DSHADER_COMP_HEADER=${SHADER_COMP_HEADER} -P "${CMAKE_CURRENT_SOURCE_DIR}/generate_shader_comp_header.cmake"
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER_SRC}
COMMENT "Preprocessing shader source ${SHADER_SRC_NAME_WE}.comp"
VERBATIM
)
set_source_files_properties(${SHADER_COMP_HEADER} PROPERTIES GENERATED TRUE)
list(APPEND SHADER_SPV_HEX_FILES ${SHADER_COMP_HEADER})
endmacro()
include_directories(${CMAKE_CURRENT_BINARY_DIR})
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
# enable global link time optimization
cmake_policy(SET CMP0069 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_supported_output)
if(ipo_supported)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO is not supported: ${ipo_supported_output}")
endif()
if(USE_SYSTEM_NCNN)
set(GLSLANG_TARGET_DIR "GLSLANG-NOTFOUND" CACHE PATH "Absolute path to glslangTargets.cmake directory")
if(NOT GLSLANG_TARGET_DIR AND NOT DEFINED ENV{GLSLANG_TARGET_DIR})
message(WARNING "GLSLANG_TARGET_DIR must be defined! USE_SYSTEM_NCNN will be turned off.")
set(USE_SYSTEM_NCNN OFF)
else()
message(STATUS "Using glslang install located at ${GLSLANG_TARGET_DIR}")
find_package(Threads)
include("${GLSLANG_TARGET_DIR}/OSDependentTargets.cmake")
include("${GLSLANG_TARGET_DIR}/OGLCompilerTargets.cmake")
if(EXISTS "${GLSLANG_TARGET_DIR}/HLSLTargets.cmake")
# hlsl support can be optional
include("${GLSLANG_TARGET_DIR}/HLSLTargets.cmake")
endif()
include("${GLSLANG_TARGET_DIR}/glslangTargets.cmake")
include("${GLSLANG_TARGET_DIR}/SPIRVTargets.cmake")
if (NOT TARGET glslang OR NOT TARGET SPIRV)
message(WARNING "glslang or SPIRV target not found! USE_SYSTEM_NCNN will be turned off.")
set(USE_SYSTEM_NCNN OFF)
endif()
endif()
endif()
if(USE_SYSTEM_NCNN)
find_package(ncnn)
if(NOT TARGET ncnn)
message(WARNING "ncnn target not found! USE_SYSTEM_NCNN will be turned off.")
set(USE_SYSTEM_NCNN OFF)
endif()
endif()
if(NOT USE_SYSTEM_NCNN)
# build ncnn library
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ncnn/CMakeLists.txt")
message(FATAL_ERROR "The submodules were not downloaded! Please update submodules with \"git submodule update --init --recursive\" and try again.")
endif()
option(NCNN_INSTALL_SDK "" ON)
option(NCNN_PIXEL_ROTATE "" OFF)
option(NCNN_PIXEL_AFFINE "" OFF)
option(NCNN_PIXEL_DRAWING "" OFF)
option(NCNN_VULKAN "" OFF)
option(NCNN_VULKAN_ONLINE_SPIRV "" OFF)
option(NCNN_BUILD_BENCHMARK "" OFF)
option(NCNN_BUILD_TESTS "" OFF)
option(NCNN_BUILD_TOOLS "" OFF)
option(NCNN_BUILD_EXAMPLES "" OFF)
option(NCNN_DISABLE_RTTI "" ON)
option(NCNN_DISABLE_EXCEPTION "" ON)
option(NCNN_INT8 "" OFF)
option(WITH_LAYER_absval "" OFF)
option(WITH_LAYER_argmax "" OFF)
option(WITH_LAYER_batchnorm "" OFF)
option(WITH_LAYER_bias "" OFF)
option(WITH_LAYER_bnll "" OFF)
option(WITH_LAYER_concat "" ON)
option(WITH_LAYER_convolution "" ON)
option(WITH_LAYER_crop "" ON)
option(WITH_LAYER_deconvolution "" ON)
option(WITH_LAYER_dropout "" OFF)
option(WITH_LAYER_eltwise "" ON)
option(WITH_LAYER_elu "" OFF)
option(WITH_LAYER_embed "" OFF)
option(WITH_LAYER_exp "" OFF)
option(WITH_LAYER_flatten "" ON)
option(WITH_LAYER_innerproduct "" ON)
option(WITH_LAYER_input "" ON)
option(WITH_LAYER_log "" OFF)
option(WITH_LAYER_lrn "" OFF)
option(WITH_LAYER_memorydata "" OFF)
option(WITH_LAYER_mvn "" OFF)
option(WITH_LAYER_pooling "" ON)
option(WITH_LAYER_power "" OFF)
option(WITH_LAYER_prelu "" ON)
option(WITH_LAYER_proposal "" OFF)
option(WITH_LAYER_reduction "" OFF)
option(WITH_LAYER_relu "" ON)
option(WITH_LAYER_reshape "" OFF)
option(WITH_LAYER_roipooling "" OFF)
option(WITH_LAYER_scale "" OFF)
option(WITH_LAYER_sigmoid "" ON)
option(WITH_LAYER_slice "" ON)
option(WITH_LAYER_softmax "" OFF)
option(WITH_LAYER_split "" ON)
option(WITH_LAYER_spp "" OFF)
option(WITH_LAYER_tanh "" OFF)
option(WITH_LAYER_threshold "" OFF)
option(WITH_LAYER_tile "" OFF)
option(WITH_LAYER_rnn "" OFF)
option(WITH_LAYER_lstm "" OFF)
option(WITH_LAYER_binaryop "" ON)
option(WITH_LAYER_unaryop "" ON)
option(WITH_LAYER_convolutiondepthwise "" OFF)
option(WITH_LAYER_padding "" ON)
option(WITH_LAYER_squeeze "" OFF)
option(WITH_LAYER_expanddims "" OFF)
option(WITH_LAYER_normalize "" OFF)
option(WITH_LAYER_permute "" OFF)
option(WITH_LAYER_priorbox "" OFF)
option(WITH_LAYER_detectionoutput "" OFF)
option(WITH_LAYER_interp "" ON)
option(WITH_LAYER_deconvolutiondepthwise "" OFF)
option(WITH_LAYER_shufflechannel "" OFF)
option(WITH_LAYER_instancenorm "" OFF)
option(WITH_LAYER_clip "" ON)
option(WITH_LAYER_reorg "" OFF)
option(WITH_LAYER_yolodetectionoutput "" OFF)
option(WITH_LAYER_quantize "" OFF)
option(WITH_LAYER_dequantize "" OFF)
option(WITH_LAYER_yolov3detectionoutput "" OFF)
option(WITH_LAYER_psroipooling "" OFF)
option(WITH_LAYER_roialign "" OFF)
option(WITH_LAYER_packing "" ON)
option(WITH_LAYER_requantize "" OFF)
option(WITH_LAYER_cast "" ON)
option(WITH_LAYER_hardsigmoid "" OFF)
option(WITH_LAYER_selu "" OFF)
option(WITH_LAYER_hardswish "" OFF)
option(WITH_LAYER_noop "" OFF)
option(WITH_LAYER_pixelshuffle "" ON)
option(WITH_LAYER_deepcopy "" OFF)
option(WITH_LAYER_mish "" OFF)
option(WITH_LAYER_statisticspooling "" OFF)
option(WITH_LAYER_swish "" OFF)
option(WITH_LAYER_gemm "" OFF)
option(WITH_LAYER_groupnorm "" OFF)
option(WITH_LAYER_layernorm "" OFF)
option(WITH_LAYER_softplus "" OFF)
option(WITH_LAYER_gru "" OFF)
option(WITH_LAYER_multiheadattention "" OFF)
option(WITH_LAYER_gelu "" OFF)
option(WITH_LAYER_convolution1d "" OFF)
option(WITH_LAYER_pooling1d "" OFF)
option(WITH_LAYER_convolutiondepthwise1d "" OFF)
option(WITH_LAYER_convolution3d "" OFF)
option(WITH_LAYER_convolutiondepthwise3d "" OFF)
option(WITH_LAYER_pooling3d "" OFF)
option(WITH_LAYER_matmul "" OFF)
option(WITH_LAYER_deconvolution1d "" OFF)
option(WITH_LAYER_deconvolutiondepthwise1d "" OFF)
option(WITH_LAYER_deconvolution3d "" OFF)
option(WITH_LAYER_deconvolutiondepthwise3d "" OFF)
option(WITH_LAYER_einsum "" OFF)
option(WITH_LAYER_deformableconv2d "" OFF)
add_subdirectory(ncnn)
endif()
if(USE_SYSTEM_WEBP)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
find_package(WebP)
if(NOT TARGET webp)
message(WARNING "webp target not found! USE_SYSTEM_WEBP will be turned off.")
set(USE_SYSTEM_WEBP OFF)
endif()
endif()
if(NOT USE_SYSTEM_WEBP)
# build libwebp library
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/libwebp/CMakeLists.txt")
message(FATAL_ERROR "The submodules were not downloaded! Please update submodules with \"git submodule update --init --recursive\" and try again.")
endif()
option(WEBP_ENABLE_SIMD "" ON)
option(WEBP_BUILD_ANIM_UTILS "" OFF)
option(WEBP_BUILD_CWEBP "" OFF)
option(WEBP_BUILD_DWEBP "" OFF)
option(WEBP_BUILD_GIF2WEBP "" OFF)
option(WEBP_BUILD_IMG2WEBP "" OFF)
option(WEBP_BUILD_VWEBP "" OFF)
option(WEBP_BUILD_WEBPINFO "" OFF)
option(WEBP_BUILD_WEBPMUX "" OFF)
option(WEBP_BUILD_EXTRAS "" OFF)
option(WEBP_BUILD_WEBP_JS "" OFF)
option(WEBP_NEAR_LOSSLESS "" OFF)
option(WEBP_ENABLE_SWAP_16BIT_CSP "" OFF)
add_subdirectory(libwebp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libwebp/src)
endif()
# rife_add_shader(rife_preproc.comp)
# rife_add_shader(rife_postproc.comp)
# rife_add_shader(rife_preproc_tta.comp)
# rife_add_shader(rife_postproc_tta.comp)
# rife_add_shader(rife_flow_tta_avg.comp)
# rife_add_shader(rife_v2_flow_tta_avg.comp)
# rife_add_shader(rife_v4_flow_tta_avg.comp)
# rife_add_shader(rife_flow_tta_temporal_avg.comp)
# rife_add_shader(rife_v2_flow_tta_temporal_avg.comp)
# rife_add_shader(rife_v4_flow_tta_temporal_avg.comp)
# rife_add_shader(rife_out_tta_temporal_avg.comp)
# rife_add_shader(rife_v4_timestep.comp)
# rife_add_shader(rife_v4_timestep_tta.comp)
# rife_add_shader(warp.comp)
# rife_add_shader(warp_pack4.comp)
# rife_add_shader(warp_pack8.comp)
# add_custom_target(generate-spirv DEPENDS ${SHADER_SPV_HEX_FILES})
# add_executable(rife-ncnn-vulkan
# main.cpp
# rife.cpp
# warp.cpp
# )
# add_dependencies(rife-ncnn-vulkan generate-spirv)
# set(RIFE_LINK_LIBRARIES ncnn webp ${Vulkan_LIBRARY})
# if(USE_STATIC_MOLTENVK)
# find_library(CoreFoundation NAMES CoreFoundation)
# find_library(Foundation NAMES Foundation)
# find_library(Metal NAMES Metal)
# find_library(QuartzCore NAMES QuartzCore)
# find_library(CoreGraphics NAMES CoreGraphics)
# find_library(Cocoa NAMES Cocoa)
# find_library(IOKit NAMES IOKit)
# find_library(IOSurface NAMES IOSurface)
# list(APPEND RIFE_LINK_LIBRARIES
# ${Metal}
# ${QuartzCore}
# ${CoreGraphics}
# ${Cocoa}
# ${IOKit}
# ${IOSurface}
# ${Foundation}
# ${CoreFoundation}
# )
# endif()
# target_link_libraries(rife-ncnn-vulkan ${RIFE_LINK_LIBRARIES})
add_library(rife_dynamic SHARED
rife.cpp
# warp.cpp
rife_interface.cpp
)
set(my_headers ${CMAKE_CURRENT_SOURCE_DIR}/rife_interface.h)
target_sources(rife_dynamic PUBLIC
FILE_SET headers_set
TYPE HEADERS
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/
FILES ${my_headers}
)
target_link_libraries(rife_dynamic PRIVATE
ncnn
)
install(
TARGETS rife_dynamic
EXPORT rife_dynamic_export
FILE_SET headers_set
LIBRARY DESTINATION lib # 动态库安装到 lib 目录
ARCHIVE DESTINATION lib # 静态库安装到 lib 目录(如果有)
INCLUDES DESTINATION include # 头文件安装到 include 目录
RUNTIME DESTINATION bin # 新增这一行,针对Windows的DLL
)
install(EXPORT rife_dynamic_export
DESTINATION bin/cmake/${TS_INSTALL_ARCH}
NAMESPACE rife_dynamic::
)
更多推荐
所有评论(0)