Vulkan driver version check

Signed-off-by: Fabian Sauter <sauter.fabian@mailbox.org>
This commit is contained in:
Fabian Sauter 2022-05-10 16:30:48 +02:00
parent ffc9e9acf8
commit 5dae50c8d9
3 changed files with 117 additions and 31 deletions

View file

@ -15,39 +15,60 @@ option(KOMPUTE_OPT_BUILD_DOCS "Enable if you want to build documentation" OFF)
option(KOMPUTE_OPT_BUILD_SHADERS "Enable if you want to re-build all shader files" OFF)
option(KOMPUTE_OPT_BUILD_SINGLE_HEADER "Enable if you want to build the single header file" OFF)
option(KOMPUTE_OPT_INSTALL "Enable if you want to enable installation" OFF)
# Build options
option(KOMPUTE_OPT_BUILD_PYTHON "Enable if you want to build python bindings" OFF)
option(KOMPUTE_OPT_ENABLE_SPDLOG "Enable to compile with spdlog as the internal logging framework" OFF)
option(KOMPUTE_OPT_ANDROID_BUILD "Enable android compilation flags required" OFF)
option(KOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS "Explicitly disable debug layers even on debug" OFF)
option(KOMPUTE_OPT_DEPENDENCIES_SHARED_LIBS "Whether to use shared libraries for dependencies for install" OFF)
option(KOMPUTE_OPT_BUILD_AS_SHARED_LIB "Whether to build kompute as shared library" OFF)
option(KOMPUTE_OPT_DISABLE_VULKAN_VERSION_CHECK "Whether to check if your driver supports the Vulkan-Header version you are linking against." OFF)
# External komponents
option(KOMPUTE_OPT_USE_BUILD_IN_SPDLOG "Use the build in version of Spdlog" ON)
option(KOMPUTE_OPT_USE_BUILD_IN_FMT "Use the build in version of fmt" ON)
option(KOMPUTE_OPT_USE_BUILD_IN_GOOGLE_TEST "Use the build in version of GoogleTest" ON)
option(KOMPUTE_OPT_USE_BUILD_IN_PYBIND11 "Use the build in version of pybind11" ON)
option(KOMPUTE_OPT_USE_BUILD_IN_VULKAN_HEADER "Use the build in version of Vulkan-Headers. This could be helpful in case your system Vulkan-Headers are to new for your driver. If you set this to false, please make sure your system Vulkan-Header are supported by your driver." ON)
option(KOMPUTE_OPT_ANDROID_BUILD "Enable android compilation flags required" OFF)
option(KOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS "Explicitly disable debug layers even on debug" OFF)
option(KOMPUTE_OPT_DEPENDENCIES_SHARED_LIBS "Whether to use shared libraries for dependencies for install" OFF)
option(KOMPUTE_OPT_BUILD_AS_SHARED_LIB "Whether to build kompute as shared library" OFF)
# Build flags
set(KOMPUTE_EXTRA_CXX_FLAGS "" CACHE STRING "Extra compile flags for Kompute, see docs for full list")
#####################################################
#################### Deprecated Options #############
#####################################################
if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD)
message(FATAL_ERROR "'KOMPUTE_OPT_REPO_SUBMODULE_BUILD' got replaced by 'KOMPUTE_OPT_USE_BUILD_IN_SPDLOG', 'KOMPUTE_OPT_USE_BUILD_IN_FMT', 'KOMPUTE_OPT_USE_BUILD_IN_GOOGLE_TEST', 'KOMPUTE_OPT_USE_BUILD_IN_PYBIND11' and 'KOMPUTE_OPT_USE_BUILD_IN_VULKAN_HEADER'. Please use them instead.")
endif()
#####################################################
#################### Dependencies ###################
#####################################################
include(FetchContent)
include(cmake/check_vulkan_version.cmake)
# Vulkan
# Vulkan Header
# We don't import Vulkan library if Android build as its build dynamically
# Otherwise it is expected that the Vulkan SDK and dependencies are installed
if(NOT KOMPUTE_OPT_ANDROID_BUILD)
find_package(Vulkan REQUIRED)
endif()
# Vulkan Header
FetchContent_Declare(vulkan_header GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Headers.git
GIT_TAG v1.2.203) # Source: https://github.com/KhronosGroup/Vulkan-Headers/tags
FetchContent_MakeAvailable(vulkan_header)
if(KOMPUTE_OPT_USE_BUILD_IN_VULKAN_HEADER)
FetchContent_Declare(vulkan_header GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Headers.git
GIT_TAG v1.2.203) # Source: https://github.com/KhronosGroup/Vulkan-Headers/tags
FetchContent_MakeAvailable(vulkan_header)
if(NOT KOMPUTE_OPT_DISABLE_VULKAN_VERSION_CHECK)
# Ensure the driver supports this Vulkan version
check_vulkan_version(INCLUDE_DIR "${vulkan_header_SOURCE_DIR}/include")
endif()
else()
if(NOT KOMPUTE_OPT_DISABLE_VULKAN_VERSION_CHECK)
# Ensure the driver supports this Vulkan version
check_vulkan_version(INCLUDE_DIR ${Vulkan_INCLUDE_DIR})
endif()
endif()
# Spdlog
if(KOMPUTE_OPT_ENABLE_SPDLOG)

View file

@ -0,0 +1,70 @@
function(check_vulkan_version)
message(STATUS "Ensuring the currently installed driver supports the Vulkan version requested by the Vulkan-Header.")
cmake_parse_arguments(VULKAN_CHECK_VERSION "" "INCLUDE_DIR" "" ${ARGN})
# Get the current Vulkan-Header version (e.g. 1.2.189).
# This snippet is based on: https://gitlab.kitware.com/cmake/cmake/-/blob/v3.23.1/Modules/FindVulkan.cmake#L140-156
if(VULKAN_CHECK_VERSION_INCLUDE_DIR)
set(VULKAN_CORE_H ${VULKAN_CHECK_VERSION_INCLUDE_DIR}/vulkan/vulkan_core.h)
if(EXISTS ${VULKAN_CORE_H})
file(STRINGS ${VULKAN_CORE_H} VulkanHeaderVersionLine REGEX "^#define VK_HEADER_VERSION ")
string(REGEX MATCHALL "[0-9]+" VulkanHeaderVersion "${VulkanHeaderVersionLine}")
file(STRINGS ${VULKAN_CORE_H} VulkanHeaderVersionLine2 REGEX "^#define VK_HEADER_VERSION_COMPLETE ")
string(REGEX MATCHALL "[0-9]+" VulkanHeaderVersion2 "${VulkanHeaderVersionLine2}")
list(LENGTH VulkanHeaderVersion2 _len)
# Versions >= 1.2.175 have an additional numbers in front of e.g. '0, 1, 2' instead of '1, 2'
if(_len EQUAL 3)
list(REMOVE_AT VulkanHeaderVersion2 0)
endif()
list(APPEND VulkanHeaderVersion2 ${VulkanHeaderVersion})
list(JOIN VulkanHeaderVersion2 "." VULKAN_HEADER_VERSION)
else()
message(FATAL_ERROR "'${VULKAN_CORE_H}' does not exist. Try calling 'find_package(Vulkan REQUIRED)' before you call this function or set 'Vulkan_INCLUDE_DIR' manually!")
return()
endif()
else()
message(FATAL_ERROR "Invalid Vulkan include directory given. Try calling 'find_package(Vulkan REQUIRED)' before you call this function or set 'Vulkan_INCLUDE_DIR' manually!")
return()
endif()
message(STATUS "Found Vulkan Header version ${VULKAN_HEADER_VERSION}.")
# Get Vulkan version supported by driver
find_program(VULKAN_INFO_PATH NAMES vulkaninfo)
if(${VULKAN_INFO_PATH})
message(FATAL_ERROR "vulkaninfo not found. The Vulkan SDK might not be installed properly.")
return()
endif()
execute_process(COMMAND "vulkaninfo"
OUTPUT_VARIABLE VULKAN_INFO_OUTPUT)
if(${VULKAN_INFO_OUTPUT} MATCHES ".*Vulkan version ([0-9]+.[0-9]+.[0-9]+).*")
set(VULKAN_DRIVER_VERSION ${CMAKE_MATCH_1})
message(STATUS "vulkaninfo reported supported version ${VULKAN_DRIVER_VERSION}")
else()
message(FATAL_ERROR "No valid version found in vulkaninfo. Does your GPU (driver) support Vulkan?")
return()
endif()
# Compare driver and header version
if(${VULKAN_DRIVER_VERSION} VERSION_LESS ${VULKAN_HEADER_VERSION})
# Version missmatch. Let us check if the major version is the same.
if(${VULKAN_DRIVER_VERSION} MATCHES "[0-9]+.([0-9]+).[0-9]+")
set(VULKAN_DRIVER_MAJOR_VERSION ${CMAKE_MATCH_1})
else()
message(FATAL_ERROR "Invalid Vulkan driver version '${VULKAN_DRIVER_VERSION}' found. Expected version in the following format: '[0-9]+.[0-9]+.[0-9]+'")
endif()
if(${VULKAN_HEADER_VERSION} MATCHES "[0-9]+.([0-9]+).[0-9]+")
set(VULKAN_HEADER_MAJOR_VERSION ${CMAKE_MATCH_1})
else()
message(FATAL_ERROR "Invalid Vulkan-Header version '${VULKAN_HEADER_VERSION}' found. Expected version in the following format: '[0-9]+.[0-9]+.[0-9]+'")
endif()
if(${VULKAN_DRIVER_MAJOR_VERSION} EQUAL ${VULKAN_HEADER_MAJOR_VERSION})
message(WARNING "Your GPU driver does not support Vulkan > ${VULKAN_DRIVER_VERSION}, but you try to use Vulkan-Header ${VULKAN_HEADER_VERSION}. At least your driver supports the same major version (${VULKAN_DRIVER_MAJOR_VERSION}), so this should be fine but keep it in mind if encounter some strange behavior.")
else()
message(FATAL_ERROR "Your GPU driver does not support Vulkan > ${VULKAN_DRIVER_VERSION}, but you try to use Vulkan-Header ${VULKAN_HEADER_VERSION}.")
endif()
else()
message(STATUS "Vulkan version check successful!")
endif()
endfunction()

View file

@ -50,18 +50,18 @@ target_include_directories(
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/single_include>
)
if(NOT KOMPUTE_OPT_ANDROID_BUILD)
target_link_libraries(
kompute
Vulkan::Vulkan
Vulkan-Headers
fmt::fmt
)
if(KOMPUTE_OPT_ANDROID_BUILD)
target_link_libraries(kompute
fmt::fmt)
else()
target_link_libraries(
kompute
fmt::fmt
)
target_link_libraries(kompute
Vulkan::Vulkan
fmt::fmt)
endif()
if(KOMPUTE_OPT_USE_BUILD_IN_VULKAN_HEADER)
target_link_libraries(kompute
Vulkan-Headers)
endif()
#####################################################
@ -69,10 +69,7 @@ endif()
#####################################################
if(KOMPUTE_OPT_ENABLE_SPDLOG)
target_link_libraries(
kompute
spdlog::spdlog
)
target_link_libraries(kompute spdlog::spdlog)
endif()
#####################################################
@ -80,12 +77,10 @@ endif()
#####################################################
if(KOMPUTE_OPT_ANDROID_BUILD)
target_link_libraries(
kompute
kompute_vk_ndk_wrapper
log
android
)
target_link_libraries(kompute
kompute_vk_ndk_wrapper
log
android)
endif()
#####################################################