Skip to content

Commit

Permalink
Add possibility to use custom MoltenVK
Browse files Browse the repository at this point in the history
  • Loading branch information
rokuz committed Oct 13, 2023
1 parent 607c209 commit d10b2e2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
46 changes: 33 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@

cmake_minimum_required(VERSION 3.16)

include(CMakeDependentOption)

project("LVK" CXX C)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# cmake-format: off
option(LVK_DEPLOY_DEPS "Deploy dependencies via CMake" ON)
option(LVK_WITH_GLFW "Enable GLFW" ON)
option(LVK_WITH_SAMPLES "Enable sample demo apps" ON)
option(LVK_WITH_TRACY "Enable Tracy profiler" ON)
option(LVK_WITH_WAYLAND "Enable Wayland" OFF)
option(LVK_CUSTOM_MOLTENVK_PATH "Path to custom MoltenVK" "")
option(LVK_DEPLOY_DEPS "Deploy dependencies via CMake" ON)
option(LVK_WITH_GLFW "Enable GLFW" ON)
option(LVK_WITH_SAMPLES "Enable sample demo apps" ON)
option(LVK_WITH_TRACY "Enable Tracy profiler" ON)
option(LVK_WITH_WAYLAND "Enable Wayland" OFF)

cmake_dependent_option(LVK_WITH_VULKAN_PORTABILITY "Enable portability extension" ON "APPLE" OFF)
# cmake-format: on

if(LVK_WITH_SAMPLES AND NOT LVK_WITH_GLFW)
message(STATUS "WARNING: LVK_WITH_SAMPLES=ON leads to forcing LVK_WITH_GLFW=ON")
set(LVK_WITH_GLFW ON)
endif()

if (ANDROID)
if(ANDROID)
message(STATUS "WARNING: LVK_WITH_GLFW and LVK_WITH_SAMPLES were set to OFF for Android")
set(LVK_WITH_GLFW OFF)
set(LVK_WITH_SAMPLES OFF)
Expand All @@ -32,9 +37,21 @@ if(LVK_WITH_WAYLAND AND (ANDROID OR APPLE OR WIN32))
message(FATAL_ERROR "LVK_WITH_WAYLAND=ON can be set only on Linux")
endif()

if(APPLE)
if(EXISTS ${LVK_CUSTOM_MOLTENVK_PATH})
message("Custom MoltenVK found at ${LVK_CUSTOM_MOLTENVK_PATH}")
set(LVK_USE_CUSTOM_MOLTENVK ON)
else()
if(NOT LVK_WITH_VULKAN_PORTABILITY)
message(FATAL_ERROR "LVK_WITH_VULKAN_PORTABILITY=ON must be set on macOS if Vulkan SDK is used")
endif()
set(LVK_USE_CUSTOM_MOLTENVK OFF)
endif()
endif()

include(cmake/CommonMacros.txt)

if(DEFINED ENV{VULKAN_SDK})
if(NOT LVK_USE_CUSTOM_MOLTENVK AND DEFINED ENV{VULKAN_SDK})
message(STATUS "VULKAN_SDK=$ENV{VULKAN_SDK}")
if(NOT EXISTS $ENV{VULKAN_SDK})
message(FATAL_ERROR "$ENV{VULKAN_SDK} does not exist.")
Expand All @@ -51,11 +68,12 @@ function(lvk_set_cxxstd target cpp_version)
endfunction()

# cmake-format: off
message(STATUS "LVK_DEPLOY_DEPS = ${LVK_DEPLOY_DEPS}")
message(STATUS "LVK_WITH_GLFW = ${LVK_WITH_GLFW}")
message(STATUS "LVK_WITH_SAMPLES = ${LVK_WITH_SAMPLES}")
message(STATUS "LVK_WITH_TRACY = ${LVK_WITH_TRACY}")
message(STATUS "LVK_WITH_WAYLAND = ${LVK_WITH_WAYLAND}")
message(STATUS "LVK_DEPLOY_DEPS = ${LVK_DEPLOY_DEPS}")
message(STATUS "LVK_WITH_GLFW = ${LVK_WITH_GLFW}")
message(STATUS "LVK_WITH_SAMPLES = ${LVK_WITH_SAMPLES}")
message(STATUS "LVK_WITH_TRACY = ${LVK_WITH_TRACY}")
message(STATUS "LVK_WITH_VULKAN_PORTABILITY = ${LVK_WITH_VULKAN_PORTABILITY}")
message(STATUS "LVK_WITH_WAYLAND = ${LVK_WITH_WAYLAND}")
# cmake-format: on

if(NOT DEFINED CMAKE_BUILD_TYPE)
Expand Down Expand Up @@ -103,7 +121,9 @@ if(LVK_WITH_TRACY)
endif()

# temporary
find_package(Vulkan REQUIRED)
if(NOT LVK_USE_CUSTOM_MOLTENVK)
find_package(Vulkan REQUIRED)
endif()

include_directories(.)
include_directories(src)
Expand Down
15 changes: 13 additions & 2 deletions lvk/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ lvk_set_folder(SPIRV "third-party/glslang")
lvk_set_folder(glslang-default-resource-limits "third-party/glslang")
# cmake-format: on

find_package(Vulkan REQUIRED)
if(NOT LVK_USE_CUSTOM_MOLTENVK)
find_package(Vulkan REQUIRED)
endif()

if(LVK_WITH_VULKAN_PORTABILITY)
add_definitions("-DLVK_WITH_VULKAN_PORTABILITY")
endif()

target_link_libraries(LVKVulkan PRIVATE LVKLibrary)
target_link_libraries(LVKVulkan PRIVATE glslang SPIRV glslang-default-resource-limits)
target_link_libraries(LVKVulkan PUBLIC Vulkan::Vulkan)
if(LVK_USE_CUSTOM_MOLTENVK)
target_include_directories(LVKVulkan PUBLIC "${LVK_CUSTOM_MOLTENVK_PATH}/include")
target_link_libraries(LVKVulkan PUBLIC "${LVK_CUSTOM_MOLTENVK_PATH}/dylib/macOS/libMoltenVK.dylib")
else()
target_link_libraries(LVKVulkan PUBLIC Vulkan::Vulkan)
endif()

target_include_directories(LVKVulkan PUBLIC "${LVK_ROOT_DIR}/third-party/deps/src/volk")
target_include_directories(LVKVulkan PUBLIC "${LVK_ROOT_DIR}/third-party/deps/src/vma/include")
Expand Down
6 changes: 5 additions & 1 deletion lvk/vulkan/VulkanClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4045,6 +4045,8 @@ void lvk::VulkanContext::createInstance() {
#endif
#elif defined(__APPLE__)
VK_MVK_MACOS_SURFACE_EXTENSION_NAME,
#endif
#if defined(LVK_WITH_VULKAN_PORTABILITY)
VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME,
#endif
VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME // enabled only for validation
Expand Down Expand Up @@ -4088,7 +4090,7 @@ void lvk::VulkanContext::createInstance() {
};

VkInstanceCreateFlags flags = 0;
#if defined(__APPLE__)
#if defined(LVK_WITH_VULKAN_PORTABILITY)
flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
#endif
const VkInstanceCreateInfo ci = {
Expand Down Expand Up @@ -4323,6 +4325,8 @@ lvk::Result lvk::VulkanContext::initContext(const HWDeviceDesc& desc) {
VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME,
VK_EXT_TEXEL_BUFFER_ALIGNMENT_EXTENSION_NAME,
VK_EXT_TEXTURE_COMPRESSION_ASTC_HDR_EXTENSION_NAME,
#endif
#if defined(LVK_WITH_VULKAN_PORTABILITY)
"VK_KHR_portability_subset",
#endif
};
Expand Down

0 comments on commit d10b2e2

Please sign in to comment.