Pārlūkot izejas kodu

Merge topic 'findvulkan-moltenvk'

d9b4264cb8 FindVulkan: Add component for `MoltenVK`
10a6bb16bb FindVulkan: Mark test target with `cxx_std_11` to avoid AppleClang warnings

Acked-by: Kitware Robot <[email protected]>
Merge-request: !7286
Brad King 3 gadi atpakaļ
vecāks
revīzija
f791cd23d4

+ 87 - 0
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)

+ 19 - 4
Tests/FindVulkan/Test/CMakeLists.txt

@@ -3,28 +3,37 @@ 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)
+target_compile_features(test_tgt PRIVATE cxx_std_11)
 add_test(NAME test_tgt COMMAND test_tgt)
 
 add_executable(test_var main.c)
 target_include_directories(test_var PRIVATE ${Vulkan_INCLUDE_DIRS})
 target_link_libraries(test_var PRIVATE ${Vulkan_LIBRARIES})
+target_compile_features(test_var PRIVATE cxx_std_11)
 add_test(NAME test_var COMMAND test_var)
 
 add_executable(test_tgt_dl main-dynamicVulkanLoading.cpp)
 target_link_libraries(test_tgt_dl Vulkan::Headers ${CMAKE_DL_LIBS})
+target_compile_features(test_tgt_dl PRIVATE cxx_std_11)
 add_test(NAME test_tgt_dl COMMAND test_tgt_dl)
 
 add_executable(test_var_dl main-dynamicVulkanLoading.cpp)
 target_include_directories(test_var_dl PRIVATE ${Vulkan_INCLUDE_DIRS})
 target_link_libraries(test_var_dl ${CMAKE_DL_LIBS})
+target_compile_features(test_var_dl PRIVATE cxx_std_11)
 add_test(NAME test_var_dl COMMAND test_var_dl)
 
 add_executable(test_tgt_glslang main-glslang.cxx)
@@ -60,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}

+ 16 - 0
Tests/FindVulkan/Test/main-MoltenVK.cxx

@@ -0,0 +1,16 @@
+#include <iostream>
+
+#include <MoltenVK/vk_mvk_moltenvk.h>
+
+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;
+}