From 51504d0d9261dc257ce320b9fec9f813706066d7 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 24 Mar 2024 19:39:16 +0100 Subject: [PATCH] Fix building macOS/iOS with the latest Vulkan SDK --- .../Cooker/Platform/iOS/iOSPlatformTools.cpp | 14 ++++- .../Vulkan/GraphicsDeviceVulkan.Build.cs | 53 +++++++++++++++++++ Source/ThirdParty/volk/volk.Build.cs | 5 +- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Source/Editor/Cooker/Platform/iOS/iOSPlatformTools.cpp b/Source/Editor/Cooker/Platform/iOS/iOSPlatformTools.cpp index 74ad99f1e..a54676a64 100644 --- a/Source/Editor/Cooker/Platform/iOS/iOSPlatformTools.cpp +++ b/Source/Editor/Cooker/Platform/iOS/iOSPlatformTools.cpp @@ -202,13 +202,25 @@ bool iOSPlatformTools::OnPostProcess(CookingData& data) if (EditorUtilities::FormatAppPackageName(appIdentifier)) return true; - // Copy fresh Gradle project template + // Copy fresh XCode project template if (FileSystem::CopyDirectory(data.OriginalOutputPath, platformDataPath / TEXT("Project"), true)) { LOG(Error, "Failed to deploy XCode project to {0} from {1}", data.OriginalOutputPath, platformDataPath); return true; } + // Fix MoltenVK lib (copied from VulkanSDK xcframework) + FileSystem::MoveFile(data.DataOutputPath / TEXT("libMoltenVK.dylib"), data.DataOutputPath / TEXT("MoltenVK"), true); + { + // Fix rpath to point into dynamic library (rather than framework location) + CreateProcessSettings procSettings; + procSettings.HiddenWindow = true; + procSettings.WorkingDirectory = data.DataOutputPath; + procSettings.FileName = TEXT("/usr/bin/install_name_tool"); + procSettings.Arguments = TEXT("-id \"@rpath/libMoltenVK.dylib\" libMoltenVK.dylib"); + Platform::CreateProcess(procSettings); + } + // Format project template files Dictionary configReplaceMap; configReplaceMap[TEXT("${AppName}")] = appName; diff --git a/Source/Engine/GraphicsDevice/Vulkan/GraphicsDeviceVulkan.Build.cs b/Source/Engine/GraphicsDevice/Vulkan/GraphicsDeviceVulkan.Build.cs index a76d94ee3..663ff97a0 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GraphicsDeviceVulkan.Build.cs +++ b/Source/Engine/GraphicsDevice/Vulkan/GraphicsDeviceVulkan.Build.cs @@ -133,6 +133,59 @@ public sealed class VulkanSdk : Sdk } return false; } + + /// + /// Adds any runtime dependency files to the build that uses Vulkan SDK. + /// + /// Build options. + public void AddDependencyFiles(BuildOptions options) + { + switch (options.Platform.Target) + { + case TargetPlatform.Mac: + case TargetPlatform.iOS: + { + // MoltenVK + var platformName = options.Platform.Target == TargetPlatform.iOS ? "iOS" : "macOS"; + var location1 = Path.Combine(RootPath, "../MoltenVK/dylib/" + platformName); + if (Directory.Exists(location1)) + { + // Initial location + options.DependencyFiles.Add(Path.Combine(location1, "libMoltenVK.dylib")); + options.DependencyFiles.Add(Path.Combine(location1, "MoltenVK_icd.json")); + return; + } + + // New location from SDK 1.3.275 + if (options.Platform.Target == TargetPlatform.iOS) + { + var location2 = Path.Combine(RootPath, "../iOS/lib/MoltenVK.xcframework/ios-arm64/MoltenVK.framework"); + var location3 = Path.Combine(RootPath, "../iOS/share/vulkan/icd.d"); + if (Directory.Exists(location2) && Directory.Exists(location3)) + { + // iOS + options.DependencyFiles.Add(Path.Combine(location2, "MoltenVK")); + options.DependencyFiles.Add(Path.Combine(location3, "MoltenVK_icd.json")); + return; + } + } + else + { + var location2 = Path.Combine(RootPath, "lib"); + var location3 = Path.Combine(RootPath, "share/vulkan/icd.d"); + if (Directory.Exists(location2) && Directory.Exists(location3)) + { + // macOS + options.DependencyFiles.Add(Path.Combine(location2, "libMoltenVK.dylib")); + options.DependencyFiles.Add(Path.Combine(location3, "MoltenVK_icd.json")); + return; + } + } + Log.Error($"Missing MoltenVK files for {platformName} in VulkanSDK '{RootPath}'"); + break; + } + } + } } /// diff --git a/Source/ThirdParty/volk/volk.Build.cs b/Source/ThirdParty/volk/volk.Build.cs index 667ecf3af..bd6ee1f58 100644 --- a/Source/ThirdParty/volk/volk.Build.cs +++ b/Source/ThirdParty/volk/volk.Build.cs @@ -42,16 +42,13 @@ public class volk : ThirdPartyModule break; case TargetPlatform.Mac: options.PublicDefinitions.Add("VK_USE_PLATFORM_MACOS_MVK"); - options.DependencyFiles.Add(Path.Combine(VulkanSdk.Instance.RootPath, "../MoltenVK/dylib/macOS/libMoltenVK.dylib")); - options.DependencyFiles.Add(Path.Combine(VulkanSdk.Instance.RootPath, "../MoltenVK/dylib/macOS/MoltenVK_icd.json")); break; case TargetPlatform.iOS: options.PublicDefinitions.Add("VK_USE_PLATFORM_IOS_MVK"); - options.DependencyFiles.Add(Path.Combine(VulkanSdk.Instance.RootPath, "../MoltenVK/dylib/iOS/libMoltenVK.dylib")); - options.DependencyFiles.Add(Path.Combine(VulkanSdk.Instance.RootPath, "../MoltenVK/dylib/iOS/MoltenVK_icd.json")); break; default: throw new InvalidPlatformException(options.Platform.Target); } + VulkanSdk.Instance.AddDependencyFiles(options); string includesFolderPath; if (VulkanSdk.Instance.TryGetIncludePath(options.Platform.Target, out includesFolderPath))