void init() { List <IntPtr> instanceExtensions = new List <IntPtr> (); List <IntPtr> enabledLayerNames = new List <IntPtr> (); instanceExtensions.Add(Strings.VK_KHR_SURFACE_EXTENSION_NAME); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { instanceExtensions.Add(Strings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { instanceExtensions.Add(Strings.VK_KHR_XCB_SURFACE_EXTENSION_NAME); } else { throw new PlatformNotSupportedException(); } if (VALIDATION) { enabledLayerNames.Add(Strings.LayerValidation); } if (RENDER_DOC_CAPTURE) { enabledLayerNames.Add(Strings.RenderdocCaptureLayerName); } if (DEBUG_UTILS) { instanceExtensions.Add(Strings.VK_EXT_DEBUG_UTILS_EXTENSION_NAME); instanceExtensions.Add(Strings.VK_EXT_DEBUG_REPORT_EXTENSION_NAME); } VkApplicationInfo appInfo = new VkApplicationInfo() { sType = VkStructureType.ApplicationInfo, apiVersion = new Vulkan.Version(1, 0, 0), pApplicationName = Strings.Name, pEngineName = Strings.Name, }; VkInstanceCreateInfo instanceCreateInfo = VkInstanceCreateInfo.New(); instanceCreateInfo.pApplicationInfo = appInfo.Pin(); if (instanceExtensions.Count > 0) { instanceCreateInfo.enabledExtensionCount = (uint)instanceExtensions.Count; instanceCreateInfo.ppEnabledExtensionNames = instanceExtensions.Pin(); } if (enabledLayerNames.Count > 0) { instanceCreateInfo.enabledLayerCount = (uint)enabledLayerNames.Count; instanceCreateInfo.ppEnabledLayerNames = enabledLayerNames.Pin(); } VkResult result = vkCreateInstance(ref instanceCreateInfo, IntPtr.Zero, out inst); if (result != VkResult.Success) { throw new InvalidOperationException("Could not create Vulkan instance. Error: " + result); } Vk.LoadInstanceFunctionPointers(inst); appInfo.Unpin(); if (instanceExtensions.Count > 0) { instanceExtensions.Unpin(); } if (enabledLayerNames.Count > 0) { enabledLayerNames.Unpin(); } }
/// <summary> /// Create a new vulkan instance with enabled extensions given as argument. /// </summary> /// <param name="extensions">List of extension to enable if supported</param> public Instance(params string[] extensions) { List <IntPtr> instanceExtensions = new List <IntPtr> (); List <IntPtr> enabledLayerNames = new List <IntPtr> (); string[] supportedExts = SupportedExtensions(IntPtr.Zero); using (PinnedObjects pctx = new PinnedObjects()) { for (int i = 0; i < extensions.Length; i++) { if (supportedExts.Contains(extensions [i])) { instanceExtensions.Add(extensions [i].Pin(pctx)); if (extensions [i] == Ext.I.VK_EXT_debug_utils) { debugUtilsEnabled = true; } } else { Console.WriteLine($"Vulkan initialisation: Unsupported extension: {extensions [i]}"); } } if (VALIDATION) { enabledLayerNames.Add(strValidationLayer.Pin(pctx)); } if (RENDER_DOC_CAPTURE) { enabledLayerNames.Add(strRenderDocLayer.Pin(pctx)); } VkApplicationInfo appInfo = new VkApplicationInfo() { sType = VkStructureType.ApplicationInfo, apiVersion = new Vulkan.Version(VK_MAJOR, VK_MINOR, 0), pApplicationName = ENGINE_NAME.Pin(pctx), pEngineName = APPLICATION_NAME.Pin(pctx), }; VkInstanceCreateInfo instanceCreateInfo = VkInstanceCreateInfo.New(); instanceCreateInfo.pApplicationInfo = appInfo.Pin(pctx); if (instanceExtensions.Count > 0) { instanceCreateInfo.enabledExtensionCount = (uint)instanceExtensions.Count; instanceCreateInfo.ppEnabledExtensionNames = instanceExtensions.Pin(pctx); } if (enabledLayerNames.Count > 0) { instanceCreateInfo.enabledLayerCount = (uint)enabledLayerNames.Count; instanceCreateInfo.ppEnabledLayerNames = enabledLayerNames.Pin(pctx); } VkResult result = vkCreateInstance(ref instanceCreateInfo, IntPtr.Zero, out inst); if (result != VkResult.Success) { throw new InvalidOperationException("Could not create Vulkan instance. Error: " + result); } Vk.LoadInstanceFunctionPointers(inst); } }