From b035691579616cf73cb55dfcf299d8363f3e7774 Mon Sep 17 00:00:00 2001 From: rokuz Date: Thu, 12 Oct 2023 22:54:59 +0100 Subject: [PATCH 1/3] Add feature to use atomics in fragment shader --- lvk/vulkan/VulkanClasses.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lvk/vulkan/VulkanClasses.cpp b/lvk/vulkan/VulkanClasses.cpp index fa59360fc6..620c278013 100644 --- a/lvk/vulkan/VulkanClasses.cpp +++ b/lvk/vulkan/VulkanClasses.cpp @@ -4324,6 +4324,7 @@ lvk::Result lvk::VulkanContext::initContext(const HWDeviceDesc& desc) { .fillModeNonSolid = VK_TRUE, .samplerAnisotropy = VK_TRUE, .textureCompressionBC = VK_TRUE, + .fragmentStoresAndAtomics = VK_TRUE, }; VkPhysicalDeviceVulkan11Features deviceFeatures11 = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES, From 607c209c3f4417eea5b3df48bbc106459da37b6f Mon Sep 17 00:00:00 2001 From: rokuz Date: Thu, 12 Oct 2023 22:57:09 +0100 Subject: [PATCH 2/3] Disable shader validation for MoltenVK --- lvk/vulkan/VulkanClasses.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lvk/vulkan/VulkanClasses.cpp b/lvk/vulkan/VulkanClasses.cpp index 620c278013..97e0630324 100644 --- a/lvk/vulkan/VulkanClasses.cpp +++ b/lvk/vulkan/VulkanClasses.cpp @@ -4056,12 +4056,25 @@ void lvk::VulkanContext::createInstance() { const VkValidationFeatureEnableEXT validationFeaturesEnabled[] = { VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT, }; + +#if defined(__APPLE__) + // Shader validation doesn't work in MoltenVK for SPIR-V 1.6 under Vulkan 1.3: + // "Invalid SPIR-V binary version 1.6 for target environment SPIR-V 1.5 (under Vulkan 1.2 semantics)." + const VkValidationFeatureDisableEXT validationFeaturesDisabled[] = { + VK_VALIDATION_FEATURE_DISABLE_SHADERS_EXT, + VK_VALIDATION_FEATURE_DISABLE_SHADER_VALIDATION_CACHE_EXT, + }; +#endif const VkValidationFeaturesEXT features = { .sType = VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT, .pNext = nullptr, .enabledValidationFeatureCount = config_.enableValidation ? (uint32_t)LVK_ARRAY_NUM_ELEMENTS(validationFeaturesEnabled) : 0u, .pEnabledValidationFeatures = config_.enableValidation ? validationFeaturesEnabled : nullptr, +#if defined(__APPLE__) + .disabledValidationFeatureCount = config_.enableValidation ? (uint32_t)LVK_ARRAY_NUM_ELEMENTS(validationFeaturesDisabled) : 0u, + .pDisabledValidationFeatures = config_.enableValidation ? validationFeaturesDisabled : nullptr, +#endif }; const VkApplicationInfo appInfo = { From d10b2e2578b12ef842937b9dc84061bf8fe6bc75 Mon Sep 17 00:00:00 2001 From: rokuz Date: Thu, 12 Oct 2023 22:58:47 +0100 Subject: [PATCH 3/3] Add possibility to use custom MoltenVK --- CMakeLists.txt | 46 ++++++++++++++++++++++++++---------- lvk/vulkan/CMakeLists.txt | 15 ++++++++++-- lvk/vulkan/VulkanClasses.cpp | 6 ++++- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 95c6bfb052..117f8232ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,16 +5,21 @@ 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) @@ -22,7 +27,7 @@ if(LVK_WITH_SAMPLES AND NOT LVK_WITH_GLFW) 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) @@ -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.") @@ -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) @@ -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) diff --git a/lvk/vulkan/CMakeLists.txt b/lvk/vulkan/CMakeLists.txt index f070fcffb0..9fe575ca00 100644 --- a/lvk/vulkan/CMakeLists.txt +++ b/lvk/vulkan/CMakeLists.txt @@ -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") diff --git a/lvk/vulkan/VulkanClasses.cpp b/lvk/vulkan/VulkanClasses.cpp index 97e0630324..ee58d9cfc2 100644 --- a/lvk/vulkan/VulkanClasses.cpp +++ b/lvk/vulkan/VulkanClasses.cpp @@ -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 @@ -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 = { @@ -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 };