From d9b4264cb848b61975fe7213183356961f0e0759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?MACHIZAUD=20Andr=C3=A9a?= Date: Mon, 23 May 2022 00:16:32 +0200 Subject: FindVulkan: Add component for `MoltenVK` --- Modules/FindVulkan.cmake | 87 +++++++++++++++++++++++++++++++++ Tests/FindVulkan/Test/CMakeLists.txt | 19 +++++-- Tests/FindVulkan/Test/main-MoltenVK.cxx | 16 ++++++ 3 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 Tests/FindVulkan/Test/main-MoltenVK.cxx diff --git a/Modules/FindVulkan.cmake b/Modules/FindVulkan.cmake index 7a32c2f..8b322ed 100644 --- a/Modules/FindVulkan.cmake +++ b/Modules/FindVulkan.cmake @@ -17,6 +17,7 @@ Optional COMPONENTS This module respects several optional COMPONENTS: ``glslc``, ``glslangValidator``, ``glslang``, ``shaderc_combined`` and ``SPIRV-Tools``. +On macOS, an additional component ``MoltenVK`` is available. There are corresponding import targets for each of these flags. IMPORTED Targets @@ -63,6 +64,12 @@ This module defines :prop_tgt:`IMPORTED` targets if Vulkan has been found: Defined if SDK has the Khronos library to process SPIR-V modules (SPIRV-Tools). +``Vulkan::MoltenVK`` + .. versionadded:: 3.24 + + Defined if SDK has the Khronos library which implement a subset of Vulkan API + over Apple Metal graphics framework. (MoltenVK). + Result Variables ^^^^^^^^^^^^^^^^ @@ -98,6 +105,10 @@ This module defines the following variables: .. versionadded:: 3.24 True, if the SDK has the SPIRV-Tools library. +``Vulkan_MoltenVK_FOUND`` + .. versionadded:: 3.24 + + True, if the SDK has the MoltenVK library. The module will also defines these cache variables: @@ -121,6 +132,10 @@ The module will also defines these cache variables: .. versionadded:: 3.24 Path to the SPIRV-Tools library. +``Vulkan_MoltenVK_LIBRARY`` + .. versionadded:: 3.24 + + Path to the MoltenVK library. Hints ^^^^^ @@ -181,6 +196,28 @@ else() "$ENV{VULKAN_SDK}/lib" ) endif() +if(APPLE AND DEFINED ENV{VULKAN_SDK}) + cmake_path(SET _MoltenVK_path NORMALIZE "$ENV{VULKAN_SDK}/../MoltenVK") + if(EXISTS "${_MoltenVK_path}") + list(APPEND _Vulkan_hint_include_search_paths + "${_MoltenVK_path}/include" + ) + if(CMAKE_SYSTEM_NAME STREQUAL "iOS") + list(APPEND _Vulkan_hint_library_search_paths + "${_MoltenVK_path}/dylib/iOS" + ) + elseif(CMAKE_SYSTEM_NAME STREQUAL "tvOS") + list(APPEND _Vulkan_hint_library_search_paths + "${_MoltenVK_path}/dylib/tvOS" + ) + else() + list(APPEND _Vulkan_hint_library_search_paths + "${_MoltenVK_path}/dylib/macOS" + ) + endif() + endif() + unset(_MoltenVK_path) +endif() find_path(Vulkan_INCLUDE_DIR NAMES vulkan/vulkan.h @@ -323,6 +360,20 @@ if(SPIRV-Tools IN_LIST Vulkan_FIND_COMPONENTS) ${_Vulkan_hint_library_search_paths}) mark_as_advanced(Vulkan_SPIRV-Tools_DEBUG_LIBRARY) endif() +if(MoltenVK IN_LIST Vulkan_FIND_COMPONENTS) + find_library(Vulkan_MoltenVK_LIBRARY + NAMES MoltenVK + HINTS + ${_Vulkan_hint_library_search_paths}) + mark_as_advanced(Vulkan_MoltenVK_LIBRARY) + + find_path(Vulkan_MoltenVK_INCLUDE_DIR + NAMES MoltenVK/mvk_vulkan.h + HINTS + ${_Vulkan_hint_include_search_paths} + ) + mark_as_advanced(Vulkan_MoltenVK_INCLUDE_DIR) +endif() if(Vulkan_GLSLC_EXECUTABLE) set(Vulkan_glslc_FOUND TRUE) @@ -387,6 +438,12 @@ _Vulkan_set_library_component_found(glslang _Vulkan_set_library_component_found(shaderc_combined) _Vulkan_set_library_component_found(SPIRV-Tools) +if(Vulkan_MoltenVK_INCLUDE_DIR AND Vulkan_MoltenVK_LIBRARY) + set(Vulkan_MoltenVK_FOUND TRUE) +else() + set(Vulkan_MoltenVK_FOUND FALSE) +endif() + set(Vulkan_LIBRARIES ${Vulkan_LIBRARY}) set(Vulkan_INCLUDE_DIRS ${Vulkan_INCLUDE_DIR}) @@ -409,6 +466,25 @@ if(Vulkan_INCLUDE_DIR) endif() endif() +if(Vulkan_MoltenVK_FOUND) + set(Vulkan_MoltenVK_VERSION "") + if(Vulkan_MoltenVK_INCLUDE_DIR) + set(VK_MVK_MOLTENVK_H ${Vulkan_MoltenVK_INCLUDE_DIR}/MoltenVK/vk_mvk_moltenvk.h) + if(EXISTS ${VK_MVK_MOLTENVK_H}) + file(STRINGS ${VK_MVK_MOLTENVK_H} _Vulkan_MoltenVK_VERSION_MAJOR REGEX "^#define MVK_VERSION_MAJOR ") + string(REGEX MATCHALL "[0-9]+" _Vulkan_MoltenVK_VERSION_MAJOR "${_Vulkan_MoltenVK_VERSION_MAJOR}") + file(STRINGS ${VK_MVK_MOLTENVK_H} _Vulkan_MoltenVK_VERSION_MINOR REGEX "^#define MVK_VERSION_MINOR ") + string(REGEX MATCHALL "[0-9]+" _Vulkan_MoltenVK_VERSION_MINOR "${_Vulkan_MoltenVK_VERSION_MINOR}") + file(STRINGS ${VK_MVK_MOLTENVK_H} _Vulkan_MoltenVK_VERSION_PATCH REGEX "^#define MVK_VERSION_PATCH ") + string(REGEX MATCHALL "[0-9]+" _Vulkan_MoltenVK_VERSION_PATCH "${_Vulkan_MoltenVK_VERSION_PATCH}") + set(Vulkan_MoltenVK_VERSION "${_Vulkan_MoltenVK_VERSION_MAJOR}.${_Vulkan_MoltenVK_VERSION_MINOR}.${_Vulkan_MoltenVK_VERSION_PATCH}") + unset(_Vulkan_MoltenVK_VERSION_MAJOR) + unset(_Vulkan_MoltenVK_VERSION_MINOR) + unset(_Vulkan_MoltenVK_VERSION_PATCH) + endif() + endif() +endif() + include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) find_package_handle_standard_args(Vulkan REQUIRED_VARS @@ -649,6 +725,17 @@ if(Vulkan_FOUND) endif() endif() +if(Vulkan_MoltenVK_FOUND) + if(Vulkan_MoltenVK_LIBRARY AND NOT TARGET Vulkan::MoltenVK) + add_library(Vulkan::MoltenVK SHARED IMPORTED) + set_target_properties(Vulkan::MoltenVK + PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Vulkan_MoltenVK_INCLUDE_DIR}" + IMPORTED_LOCATION "${Vulkan_MoltenVK_LIBRARY}" + ) + endif() +endif() + unset(_Vulkan_library_name) unset(_Vulkan_hint_include_search_paths) unset(_Vulkan_hint_executable_search_paths) diff --git a/Tests/FindVulkan/Test/CMakeLists.txt b/Tests/FindVulkan/Test/CMakeLists.txt index e0c358f..42543ac 100644 --- a/Tests/FindVulkan/Test/CMakeLists.txt +++ b/Tests/FindVulkan/Test/CMakeLists.txt @@ -3,11 +3,16 @@ cmake_policy(SET CMP0091 NEW) project(TestFindVulkan C CXX) include(CTest) +set(components + glslang + shaderc_combined + SPIRV-Tools +) +if(APPLE) + list(APPEND components MoltenVK) +endif() find_package(Vulkan REQUIRED - COMPONENTS - glslang - shaderc_combined - SPIRV-Tools) + COMPONENTS ${components}) add_executable(test_tgt main.c) target_link_libraries(test_tgt Vulkan::Vulkan) @@ -64,6 +69,12 @@ if(NOT SPIRV-Tools_debug_location) MSVC_RUNTIME_LIBRARY "MultiThreadedDLL") endif() +if(APPLE) + add_executable(test_tgt_MoltenVK main-MoltenVK.cxx) + target_link_libraries(test_tgt_MoltenVK Vulkan::MoltenVK) + add_test(NAME test_tgt_MoltenVK COMMAND test_tgt_MoltenVK) +endif() + if(Vulkan_GLSLC_EXECUTABLE) add_test(NAME test_glslc COMMAND ${CMAKE_COMMAND} diff --git a/Tests/FindVulkan/Test/main-MoltenVK.cxx b/Tests/FindVulkan/Test/main-MoltenVK.cxx new file mode 100644 index 0000000..3156979 --- /dev/null +++ b/Tests/FindVulkan/Test/main-MoltenVK.cxx @@ -0,0 +1,16 @@ +#include + +#include + +int main() +{ + char mvk_version[256]; + char vk_version[256]; + vkGetVersionStringsMVK(mvk_version, sizeof(mvk_version), vk_version, + sizeof(vk_version)); + + std::cout << "MoltenVK version: " << mvk_version << std::endl; + std::cout << "Vulkan version: " << vk_version << std::endl; + + return 0; +} -- cgit v0.12