Add Vulkan support for Mac (via MoltenVK)

This commit is contained in:
Wojtek Figat
2022-01-05 10:25:46 +01:00
parent 8874a325f7
commit 7471a819e1
24 changed files with 183 additions and 11 deletions

View File

@@ -524,6 +524,14 @@ bool ProcessShaderBase(CookAssetsStep::AssetCookData& data, ShaderAssetBase* ass
COMPILE_PROFILE(PS5, SHADER_FILE_CHUNK_INTERNAL_GENERIC_CACHE);
break;
}
#endif
#if PLATFORM_TOOLS_MAC
case BuildPlatform::MacOSx64:
{
const char* platformDefineName = "PLATFORM_MAC";
COMPILE_PROFILE(Vulkan_SM5, SHADER_FILE_CHUNK_INTERNAL_VULKAN_SM5_CACHE);
break;
}
#endif
default:
{

View File

@@ -9,6 +9,7 @@
#define GPU_VENDOR_ID_INTEL 0x8086
#define GPU_VENDOR_ID_NVIDIA 0x10DE
#define GPU_VENDOR_ID_MICROSOFT 0x1414
#define GPU_VENDOR_ID_APPLE 0x106B
/// <summary>
/// Interface for GPU device adapter.

View File

@@ -82,7 +82,8 @@ public class Graphics : EngineModule
options.PrivateDependencies.Add("GraphicsDeviceVulkan");
break;
case TargetPlatform.Mac:
options.PrivateDependencies.Add("GraphicsDeviceNull"); // TODO: Graphics support on Mac
options.PrivateDependencies.Add("GraphicsDeviceNull");
options.PrivateDependencies.Add("GraphicsDeviceVulkan");
break;
default: throw new InvalidPlatformException(options.Platform.Target);
}

View File

@@ -14,7 +14,7 @@
const Char* ShaderProfileCacheDirNames[] =
{
// @formatter:off
TEXT("EARTH_IS_NOT_FLAT_XD"), // Unknown
TEXT(""), // Unknown
TEXT("DX_SM4"), // DirectX_SM4
TEXT("DX_SM5"), // DirectX_SM5
TEXT("GLSL_410"), // GLSL_410

View File

@@ -25,6 +25,7 @@ public sealed class VulkanSdk : Sdk
{
TargetPlatform.Windows,
TargetPlatform.Linux,
TargetPlatform.Mac,
};
}
}
@@ -34,10 +35,26 @@ public sealed class VulkanSdk : Sdk
/// </summary>
public VulkanSdk()
{
if (!Platforms.Contains(Flax.Build.Platform.BuildTargetPlatform))
var platform = Flax.Build.Platform.BuildTargetPlatform;
if (!Platforms.Contains(platform))
return;
var vulkanSdk = Environment.GetEnvironmentVariable("VULKAN_SDK");
if (vulkanSdk == null && platform == TargetPlatform.Mac)
{
// Try to guess install location for the current user
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "VulkanSDK");
if (Directory.Exists(path))
{
var subDirs = Directory.GetDirectories(path);
if (subDirs.Length != 0)
{
path = Path.Combine(subDirs[0], "macOS");
if (Directory.Exists(path))
vulkanSdk = path;
}
}
}
if (vulkanSdk != null)
{
if (Directory.Exists(vulkanSdk))
@@ -110,13 +127,14 @@ public class GraphicsDeviceVulkan : GraphicsDeviceBaseModule
options.PrivateDependencies.Add("VulkanMemoryAllocator");
if (options.Platform.Target == TargetPlatform.Switch)
switch (options.Platform.Target)
{
case TargetPlatform.Switch:
options.SourcePaths.Add(Path.Combine(Globals.EngineRoot, "Source", "Platforms", "Switch", "Engine", "GraphicsDevice", "Vulkan"));
}
else
{
break;
default:
options.PrivateDependencies.Add("volk");
break;
}
}
}

View File

@@ -0,0 +1,23 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
#if GRAPHICS_API_VULKAN && PLATFORM_MAC
#include "MacVulkanPlatform.h"
#include "../RenderToolsVulkan.h"
#include <Cocoa/Cocoa.h>
void MacVulkanPlatform::GetInstanceExtensions(Array<const char*>& extensions, Array<const char*>& layers)
{
extensions.Add(VK_MVK_MACOS_SURFACE_EXTENSION_NAME);
}
void MacVulkanPlatform::CreateSurface(void* windowHandle, VkInstance instance, VkSurfaceKHR* surface)
{
NSWindow* window = (NSWindow*)windowHandle;
VkMacOSSurfaceCreateInfoMVK surfaceCreateInfo;
RenderToolsVulkan::ZeroStruct(surfaceCreateInfo, VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK);
surfaceCreateInfo.pView = (void*)window.contentView;
VALIDATE_VULKAN_RESULT(vkCreateMacOSSurfaceMVK(instance, &surfaceCreateInfo, nullptr, surface));
}
#endif

View File

@@ -0,0 +1,21 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
#pragma once
#include "../VulkanPlatformBase.h"
#if GRAPHICS_API_VULKAN && PLATFORM_MAC
/// <summary>
/// The implementation for the Vulkan API support for Mac platform.
/// </summary>
class MacVulkanPlatform : public VulkanPlatformBase
{
public:
static void GetInstanceExtensions(Array<const char*>& extensions, Array<const char*>& layers);
static void CreateSurface(void* windowHandle, VkInstance instance, VkSurfaceKHR* outSurface);
};
typedef MacVulkanPlatform VulkanPlatform;
#endif

View File

@@ -10,4 +10,6 @@
#include "Android/AndroidVulkanPlatform.h"
#elif PLATFORM_SWITCH
#include "Platforms/Switch/Engine/GraphicsDevice/Vulkan/SwitchVulkanPlatform.h"
#elif PLATFORM_MAC
#include "Mac/MacVulkanPlatform.h"
#endif

View File

@@ -6,6 +6,7 @@
#include "MacUtils.h"
#include "Engine/Graphics/RenderTask.h"
#include <Cocoa/Cocoa.h>
#include <QuartzCore/CAMetalLayer.h>
@interface MacWindowImpl : NSWindow <NSWindowDelegate>
{
@@ -31,6 +32,29 @@
@end
@interface MacViewImpl : NSView
{
}
- (CALayer*)makeBackingLayer;
- (BOOL)wantsUpdateLayer;
@end
@implementation MacViewImpl
- (CALayer*)makeBackingLayer
{
return [[CAMetalLayer class] layer];
}
- (BOOL)wantsUpdateLayer
{
return YES;
}
@end
MacWindow::MacWindow(const CreateWindowSettings& settings)
: WindowBase(settings)
{
@@ -60,12 +84,16 @@ MacWindow::MacWindow(const CreateWindowSettings& settings)
styleMask:(styleMask)
backing:NSBackingStoreBuffered
defer:NO];
MacViewImpl* view = [[MacViewImpl alloc] init];
view.wantsLayer = YES;
window.title = (__bridge NSString*)MacUtils::ToString(settings.Title);
[window setWindow:this];
[window setReleasedWhenClosed:NO];
[window setMinSize:NSMakeSize(settings.MinimumSize.X, settings.MinimumSize.Y)];
[window setMaxSize:NSMakeSize(settings.MaximumSize.X, settings.MaximumSize.Y)];
[window setOpaque:!settings.SupportsTransparency];
[window setContentView:view];
[window setAcceptsMouseMovedEvents:YES];
[window setDelegate:window];
_window = window;

View File

@@ -54,6 +54,7 @@ public class ShadersCompilation : EngineModule
options.PrivateDependencies.Add("ShaderCompilerVulkan");
break;
case TargetPlatform.Mac:
options.PrivateDependencies.Add("ShaderCompilerVulkan");
break;
default: throw new InvalidPlatformException(options.Platform.Target);
}

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libGenericCodeGen.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libHLSL.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libMachineIndependent.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libOGLCompiler.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libOSDependent.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libSPIRV-Tools-opt.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libSPIRV-Tools.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libSPIRV.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
Source/Platforms/Mac/Binaries/ThirdParty/x64/libglslang.a (Stored with Git LFS) vendored Normal file

Binary file not shown.

View File

@@ -39,6 +39,7 @@ public class glslang : DepsModule
options.OutputFiles.Add(Path.Combine(depsRoot, "SPIRV.lib"));
break;
case TargetPlatform.Linux:
case TargetPlatform.Mac:
options.OutputFiles.Add(Path.Combine(depsRoot, "libGenericCodeGen.a"));
options.OutputFiles.Add(Path.Combine(depsRoot, "libglslang.a"));
options.OutputFiles.Add(Path.Combine(depsRoot, "libHLSL.a"));

View File

@@ -35,6 +35,7 @@ public class spirv_tools : DepsModule
options.OutputFiles.Add(Path.Combine(depsRoot, "SPIRV-Tools-opt.lib"));
break;
case TargetPlatform.Linux:
case TargetPlatform.Mac:
options.OutputFiles.Add(Path.Combine(depsRoot, "libSPIRV-Tools.a"));
options.OutputFiles.Add(Path.Combine(depsRoot, "libSPIRV-Tools-opt.a"));
break;

View File

@@ -1,5 +1,6 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System.IO;
using Flax.Build;
using Flax.Build.NativeCpp;
@@ -36,6 +37,11 @@ public class volk : ThirdPartyModule
case TargetPlatform.Android:
options.PublicDefinitions.Add("VK_USE_PLATFORM_ANDROID_KHR");
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;
default: throw new InvalidPlatformException(options.Platform.Target);
}

View File

@@ -28,6 +28,11 @@ namespace Flax.Deps.Dependencies
{
TargetPlatform.Linux,
};
case TargetPlatform.Mac:
return new[]
{
TargetPlatform.Mac,
};
default: return new TargetPlatform[0];
}
}
@@ -71,10 +76,10 @@ namespace Flax.Deps.Dependencies
// Build for Win64
File.Delete(Path.Combine(buildDir, "CMakeCache.txt"));
RunCmake(buildDir, TargetPlatform.Windows, TargetArchitecture.x64, cmakeArgs);
RunCmake(buildDir, platform, TargetArchitecture.x64, cmakeArgs);
Utilities.Run("cmake", string.Format("--build . --config {0} --target install", configuration), null, buildDir, Utilities.RunOptions.None);
Deploy.VCEnvironment.BuildSolution(solutionPath, configuration, "x64");
var depsFolder = GetThirdPartyFolder(options, TargetPlatform.Windows, TargetArchitecture.x64);
var depsFolder = GetThirdPartyFolder(options, platform, TargetArchitecture.x64);
foreach (var file in outputFiles)
{
Utilities.FileCopy(file, Path.Combine(depsFolder, Path.GetFileName(file)));
@@ -97,10 +102,10 @@ namespace Flax.Deps.Dependencies
};
// Build for Linux
RunCmake(root, TargetPlatform.Linux, TargetArchitecture.x64, cmakeArgs);
RunCmake(root, platform, TargetArchitecture.x64, cmakeArgs);
Utilities.Run("cmake", string.Format("--build . --config {0} --target install", configuration), null, buildDir, Utilities.RunOptions.None);
Utilities.Run("make", null, null, root, Utilities.RunOptions.None);
var depsFolder = GetThirdPartyFolder(options, TargetPlatform.Linux, TargetArchitecture.x64);
var depsFolder = GetThirdPartyFolder(options, platform, TargetArchitecture.x64);
foreach (var file in outputFiles)
{
var dst = Path.Combine(depsFolder, Path.GetFileName(file));
@@ -109,6 +114,34 @@ namespace Flax.Deps.Dependencies
}
break;
}
case TargetPlatform.Mac:
{
var outputFiles = new[]
{
Path.Combine(libsRoot, "libGenericCodeGen.a"),
Path.Combine(libsRoot, "libMachineIndependent.a"),
Path.Combine(libsRoot, "libHLSL.a"),
Path.Combine(libsRoot, "libOSDependent.a"),
Path.Combine(libsRoot, "libOGLCompiler.a"),
Path.Combine(libsRoot, "libSPIRV-Tools-opt.a"),
Path.Combine(libsRoot, "libSPIRV-Tools.a"),
Path.Combine(libsRoot, "libSPIRV.a"),
Path.Combine(libsRoot, "libglslang.a"),
};
// Build for Mac
RunCmake(root, platform, TargetArchitecture.x64, cmakeArgs);
Utilities.Run("cmake", string.Format("--build . --config {0} --target install", configuration), null, buildDir, Utilities.RunOptions.None);
Utilities.Run("make", null, null, root, Utilities.RunOptions.None);
var depsFolder = GetThirdPartyFolder(options, platform, TargetArchitecture.x64);
foreach (var file in outputFiles)
{
var dst = Path.Combine(depsFolder, Path.GetFileName(file));
Utilities.FileCopy(file, dst);
Utilities.Run("strip", string.Format("\"{0}\"", dst), null, null, Utilities.RunOptions.None);
}
break;
}
}
}

View File

@@ -114,6 +114,7 @@ namespace Flax.Build.Platforms
options.LinkEnv.InputLibraries.Add("SystemConfiguration.framework");
options.LinkEnv.InputLibraries.Add("IOKit.framework");
options.LinkEnv.InputLibraries.Add("Cocoa.framework");
options.LinkEnv.InputLibraries.Add("QuartzCore.framework");
}
/// <inheritdoc />