From fe6cb8a976b328b87ec66357c97614f67865e1d7 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bruyère Date: Tue, 7 Dec 2021 20:18:00 +0100 Subject: move sample.Utils to main lib and Vulkan.Utils --- samples/Directory.Build.props | 2 +- samples/create_device/Program.cs | 17 +++++---- samples/select_physical_dev/Program.cs | 5 +-- vulkan/LoadingUtils.cs | 32 +++++++++++++++++ vulkan/Utils.cs | 63 ++++++++++++++++++++++------------ 5 files changed, 86 insertions(+), 33 deletions(-) create mode 100644 vulkan/LoadingUtils.cs diff --git a/samples/Directory.Build.props b/samples/Directory.Build.props index 136cee2..24621ae 100644 --- a/samples/Directory.Build.props +++ b/samples/Directory.Build.props @@ -13,7 +13,7 @@ - + $(DefineConstants);AUTO_SET_STYPE diff --git a/samples/create_device/Program.cs b/samples/create_device/Program.cs index ef7339d..3daca81 100644 --- a/samples/create_device/Program.cs +++ b/samples/create_device/Program.cs @@ -1,7 +1,7 @@ using System; using Vulkan; using static Vulkan.Vk; -using static samples.Utils; +using static Vulkan.Utils; using System.Linq; namespace tests @@ -32,12 +32,15 @@ namespace tests } vkGetPhysicalDeviceQueueFamilyProperties (phy, out uint queueFamilyCount, IntPtr.Zero); - VkQueueFamilyProperties[] phys = new VkQueueFamilyProperties[queueFamilyCount]; - vkGetPhysicalDeviceQueueFamilyProperties (phy, out queueFamilyCount, phys.Pin()); - phys.Unpin(); - - uint qFamIndex = (uint)phys.Select((phy, index) => (phy, index)) - .First (qfp=>qfp.phy.queueFlags.HasFlag (VkQueueFlags.Graphics)).index; + VkQueueFamilyProperties[] qFamProps = new VkQueueFamilyProperties[queueFamilyCount]; + vkGetPhysicalDeviceQueueFamilyProperties (phy, out queueFamilyCount, qFamProps.Pin()); + qFamProps.Unpin(); + Console.WriteLine ($"Queues:"); + for (int i = 0; i < queueFamilyCount; i++) + Console.WriteLine ($"\t{i}: {qFamProps[i].queueFlags,-60} ({qFamProps[i].queueCount})"); + + uint qFamIndex = (uint)qFamProps.Select((qFam, index) => (qFam, index)) + .First (qfp=>qfp.qFam.queueFlags.HasFlag (VkQueueFlags.Graphics)).index; VkDevice dev = default; float[] priorities = {0}; diff --git a/samples/select_physical_dev/Program.cs b/samples/select_physical_dev/Program.cs index 3258489..e20193b 100644 --- a/samples/select_physical_dev/Program.cs +++ b/samples/select_physical_dev/Program.cs @@ -1,10 +1,7 @@ using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Text; using Vulkan; using static Vulkan.Vk; -using static samples.Utils; +using static Vulkan.Utils; using Version = Vulkan.Version; namespace tests diff --git a/vulkan/LoadingUtils.cs b/vulkan/LoadingUtils.cs new file mode 100644 index 0000000..b91a47b --- /dev/null +++ b/vulkan/LoadingUtils.cs @@ -0,0 +1,32 @@ +// Copyright (c) 2019 Jean-Philippe Bruyère +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +using System; +using System.Runtime.InteropServices; + +namespace Vulkan { + internal static class LoadingUtils { + public static IntPtr GetDelegate (VkInstance inst, string name) { + byte[] n = System.Text.Encoding.UTF8.GetBytes (name + '\0'); + GCHandle hnd = GCHandle.Alloc (n, GCHandleType.Pinned); + IntPtr del = Vk.vkGetInstanceProcAddr (inst, hnd.AddrOfPinnedObject ()); + if (del == IntPtr.Zero) + System.Diagnostics.Debug.WriteLine ("instance function pointer not found for " + name); + hnd.Free (); + return del; + } + /// + /// try to get device function handle if available + /// + public static void GetDelegate (VkDevice dev, string name, ref IntPtr ptr) { + byte[] n = System.Text.Encoding.UTF8.GetBytes (name + '\0'); + GCHandle hnd = GCHandle.Alloc (n, GCHandleType.Pinned); + IntPtr del = Vk.vkGetDeviceProcAddr (dev, hnd.AddrOfPinnedObject ()); + if (del == IntPtr.Zero) + System.Diagnostics.Debug.WriteLine ("device function pointer not found for " + name); + else + ptr = del; + hnd.Free (); + } + } +} diff --git a/vulkan/Utils.cs b/vulkan/Utils.cs index b91a47b..bf629ed 100644 --- a/vulkan/Utils.cs +++ b/vulkan/Utils.cs @@ -3,30 +3,51 @@ // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; +using Vulkan; +using static Vulkan.Vk; namespace Vulkan { - internal static class LoadingUtils { - public static IntPtr GetDelegate (VkInstance inst, string name) { - byte[] n = System.Text.Encoding.UTF8.GetBytes (name + '\0'); - GCHandle hnd = GCHandle.Alloc (n, GCHandleType.Pinned); - IntPtr del = Vk.vkGetInstanceProcAddr (inst, hnd.AddrOfPinnedObject ()); - if (del == IntPtr.Zero) - System.Diagnostics.Debug.WriteLine ("instance function pointer not found for " + name); - hnd.Free (); - return del; + public static class Utils { + public static void CheckResult (VkResult result, string msg = "Call failed", + [CallerMemberName] string caller = null, + [CallerFilePath] string sourceFilePath = "", + [CallerLineNumber] int sourceLineNumber = 0) { + if (result != VkResult.Success) + throw new InvalidOperationException ($"[{sourceFilePath}:{sourceLineNumber}->{caller}]{msg}: {result}"); } - /// - /// try to get device function handle if available - /// - public static void GetDelegate (VkDevice dev, string name, ref IntPtr ptr) { - byte[] n = System.Text.Encoding.UTF8.GetBytes (name + '\0'); - GCHandle hnd = GCHandle.Alloc (n, GCHandleType.Pinned); - IntPtr del = Vk.vkGetDeviceProcAddr (dev, hnd.AddrOfPinnedObject ()); - if (del == IntPtr.Zero) - System.Diagnostics.Debug.WriteLine ("device function pointer not found for " + name); - else - ptr = del; - hnd.Free (); + public static bool TryGetPhysicalDevice (VkInstance inst, VkPhysicalDeviceType deviceType, out VkPhysicalDevice phy) { + CheckResult (vkEnumeratePhysicalDevices (inst, out uint phyCount, IntPtr.Zero)); + + VkPhysicalDevice[] phys = new VkPhysicalDevice[phyCount]; + + CheckResult (vkEnumeratePhysicalDevices (inst, out phyCount, phys.Pin())); + + for (int i = 0; i < phys.Length; i++) + { + phy = phys[i]; + vkGetPhysicalDeviceProperties (phy, out VkPhysicalDeviceProperties props); + if (props.deviceType == deviceType) + return true; + } + phy = default; + return false; + } + public static VkPhysicalDeviceToolPropertiesEXT[] GetToolProperties (VkPhysicalDevice phy) { + CheckResult (vkGetPhysicalDeviceToolPropertiesEXT (phy , out uint count, IntPtr.Zero)); + int sizeStruct = Marshal.SizeOf (); + IntPtr ptrTools = Marshal.AllocHGlobal (sizeStruct * (int)count); + CheckResult (vkGetPhysicalDeviceToolPropertiesEXT (phy , out count, ptrTools)); + + VkPhysicalDeviceToolPropertiesEXT[] result = new VkPhysicalDeviceToolPropertiesEXT[count]; + IntPtr tmp = ptrTools; + for (int i = 0; i < count; i++) { + result[i] = Marshal.PtrToStructure (tmp); + tmp += sizeStruct; + } + + Marshal.FreeHGlobal (ptrTools); + return result; } } } -- cgit v1.3.1-4-g156e