aboutsummaryrefslogtreecommitdiffstatsci
path: root/samples/create_device/Program.cs
blob: b2c0f5adf98e47082f19bc424d0b9911722e5080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using Vulkan;
using static Vulkan.Vk;
using static Vulkan.Utils;
using System.Linq;
using System.Runtime.InteropServices;

namespace tests
{	
	class Program
	{
		static void Main(string[] args)
		{
			VkInstance inst;

			using (VkApplicationInfo ai = new VkApplicationInfo (
				new Vulkan.Version(1,2,0),
				new Vulkan.Version(1,2,0),
				new Vulkan.Version(1,3,0)
			)) {
				
			using (PinnedObjects po = new PinnedObjects()) {
				IntPtr[] instanceExts = {Ext.I.VK_KHR_get_physical_device_properties2.Pin(po)};
				IntPtr[] layers = {"VK_LAYER_KHRONOS_validation".Pin(po)};
				using (VkInstanceCreateInfo ci = new VkInstanceCreateInfo {
					pApplicationInfo = ai,
					enabledExtensionCount = 1,
					enabledLayerCount = 1,
					ppEnabledExtensionNames = instanceExts.Pin(po),
					ppEnabledLayerNames = layers.Pin(po)}){
						CheckResult (vkCreateInstance (ci, IntPtr.Zero, out inst));
					}
				}
			}

			Vk.LoadInstanceFunctionPointers (inst);

			CheckResult (vkEnumerateInstanceVersion(out uint apiVersion));

			Console.WriteLine ($"Vulkan api version: {(Vulkan.Version)apiVersion})");
		

			if (!TryGetPhysicalDevice (inst, VkPhysicalDeviceType.DiscreteGpu, out VkPhysicalDevice phy))
				if (!TryGetPhysicalDevice (inst, VkPhysicalDeviceType.IntegratedGpu, out phy))
					if (!TryGetPhysicalDevice (inst, VkPhysicalDeviceType.Cpu, out phy))
						throw new Exception ("no suitable physical device found");

			foreach (VkPhysicalDeviceToolProperties toolProp in GetToolProperties(phy)) {
				Console.ForegroundColor = ConsoleColor.DarkYellow;
				Console.WriteLine ($"Enabled Tool: {toolProp.name}({toolProp.version})");
				Console.ResetColor ();
			}
			
			vkGetPhysicalDeviceQueueFamilyProperties (phy, out uint queueFamilyCount, IntPtr.Zero);
			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;

						
			VkPhysicalDeviceProperties2 phyProps2 = VkPhysicalDeviceProperties2.New;
			using (var vk11 = new PNext<VkPhysicalDeviceVulkan11Properties>()) {
				phyProps2.pNext = vk11;
				vkGetPhysicalDeviceProperties2(phy, ref phyProps2);				
				Console.WriteLine($"SupportedStages: {vk11.Val.subgroupSupportedStages}" );
			}

			VkDevice dev = default;
			float[] priorities = {0};
			using (VkDeviceQueueCreateInfo qInfo = new VkDeviceQueueCreateInfo (qFamIndex,1,priorities)) {
				using (VkDeviceCreateInfo deviceCreateInfo = new VkDeviceCreateInfo () {
							pQueueCreateInfos = qInfo
						}) {
					CheckResult (vkCreateDevice (phy, deviceCreateInfo, IntPtr.Zero, out dev));
				}
			}
			
			vkGetDeviceQueue (dev, qFamIndex, 0, out VkQueue gQ);

			vkDestroyDevice (dev, IntPtr.Zero);

			vkDestroyInstance (inst, IntPtr.Zero);
		}
	}
}