From d7ec9710ab48f567f2b413cc64f1110ba67e28dd Mon Sep 17 00:00:00 2001 From: Vinicius Miguel Date: Mon, 2 Jan 2023 19:12:04 +0100 Subject: [PATCH 1/7] Fix to ImGuiVulkan Experiment to run on MacOSX From ae86787b01edb7660072ade10bd50cddfb1f72bd Mon Sep 17 00:00:00 2001 From: Vinicius Miguel Date: Mon, 2 Jan 2023 19:25:15 +0100 Subject: [PATCH 2/7] Pointing to the correct lib on OSX The MoltenVK library (dylib) is considered as an ICD (Installable Client Driver) by the Vulkan loader. The Vulkan loader dynamically loads ICDs at execution time. When using the Vulkan loader, ICDs are not linked directly to the application. Similarly, the Vulkan loader dynamically loads the layers at execution time. Layers are not linked directly to the application. The MoltenVK library is a little unusual because it is built in such a way that an application can link to it directly and run MoltenVK (Vulkan subset) applications without using the loader and layers. But if you want to use the loader and layers, you don't link MoltenVK and the layers to your application. You link only the loader. The loader then dynamically loads the MoltenVK lib as an ICD and dynamically loads the layers if requested. --- src/Vulkan/Silk.NET.Vulkan/VulkanLibraryNameContainer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Vulkan/Silk.NET.Vulkan/VulkanLibraryNameContainer.cs b/src/Vulkan/Silk.NET.Vulkan/VulkanLibraryNameContainer.cs index 9fef2d8698..b1f8a530e2 100644 --- a/src/Vulkan/Silk.NET.Vulkan/VulkanLibraryNameContainer.cs +++ b/src/Vulkan/Silk.NET.Vulkan/VulkanLibraryNameContainer.cs @@ -14,7 +14,7 @@ internal class VulkanLibraryNameContainer : SearchPathContainer public override string Linux => "libvulkan.so.1"; /// - public override string MacOS => "libMoltenVK.dylib"; + public override string MacOS => "libvulkan.dylib"; /// public override string Android => "libvulkan.so"; From 035a48222323af5cabd206ad99391fc7cafc559b Mon Sep 17 00:00:00 2001 From: Vinicius Miguel Date: Mon, 2 Jan 2023 19:32:38 +0100 Subject: [PATCH 3/7] Updating ImGuiVulkan project file Updating to net6.0 Including Constants for different OSes --- .../Experiments/ImGuiVulkan/ImGuiVulkan.csproj | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj index 33cb241d5c..eb9e5f3bea 100644 --- a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj +++ b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj @@ -1,12 +1,22 @@  - Exe - netcoreapp3.1 + net6.0 true preview - + + OS_WINDOWS + + + OS_LINUX + + + OS_FREEBSD + + + OS_MAC + From ccd70292f7be0b347596d7da97b4198205b867db Mon Sep 17 00:00:00 2001 From: Vinicius Miguel Date: Mon, 2 Jan 2023 19:42:20 +0100 Subject: [PATCH 4/7] Update ImGuiVulkan.csproj Rolling back OS Constants --- src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj index eb9e5f3bea..c80509cc73 100644 --- a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj +++ b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj @@ -5,18 +5,6 @@ true preview - - OS_WINDOWS - - - OS_LINUX - - - OS_FREEBSD - - - OS_MAC - From 2c75dfec03f9c618197665a6c0c5f89c5f4e1a20 Mon Sep 17 00:00:00 2001 From: Vinicius Miguel Date: Mon, 2 Jan 2023 19:43:54 +0100 Subject: [PATCH 5/7] Updating ImGui.Net Package --- src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj index c80509cc73..74754b3378 100644 --- a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj +++ b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj @@ -13,7 +13,7 @@ - + From a74d6ddcad82736642ef208707f7f01066d60af1 Mon Sep 17 00:00:00 2001 From: Vinicius Miguel Date: Mon, 2 Jan 2023 19:57:12 +0100 Subject: [PATCH 6/7] Update ImGuiVulkanApplication.cs Included KHR portability subset to work on MoltenVK. Bit set into InstanceCreateInfo for Enumerate Portability. The project works on MacOS 13.1 now on Apple Silicon. --- .../ImGuiVulkan/ImGuiVulkanApplication.cs | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs index 45283cad4c..475a61de2e 100644 --- a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs +++ b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -75,8 +76,8 @@ public void Run() private KhrSwapchain _vkSwapchain; private ExtDebugUtils _debugUtils; private string[] _validationLayers = { "VK_LAYER_KHRONOS_validation" }; - private string[] _instanceExtensions = { ExtDebugUtils.ExtensionName }; - private string[] _deviceExtensions = { KhrSwapchain.ExtensionName }; + private List _instanceExtensions = new () { ExtDebugUtils.ExtensionName }; + private List _deviceExtensions = new () { KhrSwapchain.ExtensionName }; private void InitWindow() { @@ -320,12 +321,20 @@ private unsafe void CreateInstance() { _vk = Vk.GetApi(); + var isMacOs = Environment.OSVersion.Platform is PlatformID.Unix or PlatformID.MacOSX; + + if (isMacOs) + { + _instanceExtensions.Add("VK_KHR_portability_enumeration"); + _deviceExtensions.Add("VK_KHR_portability_subset"); + } + if (EnableValidationLayers && !CheckValidationLayerSupport()) { throw new NotSupportedException("Validation layers requested, but not available!"); } - var appInfo = new ApplicationInfo + var appInfo = new ApplicationInfo { SType = StructureType.ApplicationInfo, PApplicationName = (byte*)Marshal.StringToHGlobalAnsi("Hello Triangle"), @@ -340,22 +349,27 @@ private unsafe void CreateInstance() SType = StructureType.InstanceCreateInfo, PApplicationInfo = &appInfo }; - + + if (isMacOs) + { + createInfo.Flags = InstanceCreateFlags.EnumeratePortabilityBitKhr; + } + var extensions = _window.VkSurface!.GetRequiredExtensions(out var extCount); // TODO Review that this count doesn't realistically exceed 1k (recommended max for stackalloc) // Should probably be allocated on heap anyway as this isn't super performance critical. - var newExtensions = stackalloc byte*[(int)(extCount + _instanceExtensions.Length)]; + var newExtensions = stackalloc byte*[(int)(extCount + _instanceExtensions.Count)]; for (var i = 0; i < extCount; i++) { newExtensions[i] = extensions[i]; } - for (var i = 0; i < _instanceExtensions.Length; i++) + for (var i = 0; i < _instanceExtensions.Count; i++) { newExtensions[extCount + i] = (byte*)SilkMarshal.StringToPtr(_instanceExtensions[i]); } - extCount += (uint)_instanceExtensions.Length; + extCount += (uint)_instanceExtensions.Count; createInfo.EnabledExtensionCount = extCount; createInfo.PpEnabledExtensionNames = newExtensions; @@ -615,7 +629,7 @@ private unsafe void CreateLogicalDevice() createInfo.QueueCreateInfoCount = (uint)uniqueQueueFamilies.Length; createInfo.PQueueCreateInfos = queueCreateInfos; createInfo.PEnabledFeatures = &deviceFeatures; - createInfo.EnabledExtensionCount = (uint)_deviceExtensions.Length; + createInfo.EnabledExtensionCount = (uint)_deviceExtensions.Count; var enabledExtensionNames = SilkMarshal.StringArrayToPtr(_deviceExtensions); createInfo.PpEnabledExtensionNames = (byte**)enabledExtensionNames; From 053e5bc07c7afe770cacfc543a9c50584975df4f Mon Sep 17 00:00:00 2001 From: Vinicius Miguel Date: Tue, 3 Jan 2023 15:21:39 +0100 Subject: [PATCH 7/7] Fix MacOs Platform Detection Using a different approach proposed by the corefx team. --- src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs index 475a61de2e..8a5959b668 100644 --- a/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs +++ b/src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs @@ -320,8 +320,8 @@ private unsafe void CleanupSwapchain() private unsafe void CreateInstance() { _vk = Vk.GetApi(); - - var isMacOs = Environment.OSVersion.Platform is PlatformID.Unix or PlatformID.MacOSX; + + var isMacOs = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); if (isMacOs) {