private VkResult CreateInstance(bool enableValidation) { Settings.Validation = enableValidation; VkApplicationInfo appInfo = new VkApplicationInfo() { sType = VkStructureType.ApplicationInfo, apiVersion = new Version(1, 0, 0), pApplicationName = Name, pEngineName = Name, }; NativeList <IntPtr> instanceExtensions = new NativeList <IntPtr>(2); 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_XLIB_SURFACE_EXTENSION_NAME); } else { throw new PlatformNotSupportedException(); } VkInstanceCreateInfo instanceCreateInfo = VkInstanceCreateInfo.New(); instanceCreateInfo.pApplicationInfo = &appInfo; if (instanceExtensions.Count > 0) { if (enableValidation) { instanceExtensions.Add(Strings.VK_EXT_DEBUG_REPORT_EXTENSION_NAME); } instanceCreateInfo.enabledExtensionCount = instanceExtensions.Count; instanceCreateInfo.ppEnabledExtensionNames = (byte **)instanceExtensions.Data; } if (enableValidation) { NativeList <IntPtr> enabledLayerNames = new NativeList <IntPtr>(1); enabledLayerNames.Add(Strings.StandardValidationLayeName); instanceCreateInfo.enabledLayerCount = enabledLayerNames.Count; instanceCreateInfo.ppEnabledLayerNames = (byte **)enabledLayerNames.Data; } VkInstance instance; VkResult result = vkCreateInstance(&instanceCreateInfo, null, &instance); Instance = instance; return(result); }
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); } }
private void CreateInstance(bool debug) { HashSet <string> availableInstanceLayers = new HashSet <string>(EnumerateInstanceLayers()); HashSet <string> availableInstanceExtensions = new HashSet <string>(EnumerateInstanceExtensions()); VkInstanceCreateInfo instanceCI = VkInstanceCreateInfo.New(); VkApplicationInfo applicationInfo = new VkApplicationInfo(); applicationInfo.apiVersion = new VkVersion(1, 0, 0); applicationInfo.applicationVersion = new VkVersion(1, 0, 0); applicationInfo.engineVersion = new VkVersion(1, 0, 0); applicationInfo.pApplicationName = s_name; applicationInfo.pEngineName = s_name; instanceCI.pApplicationInfo = &applicationInfo; StackList <IntPtr, Size64Bytes> instanceExtensions = new StackList <IntPtr, Size64Bytes>(); StackList <IntPtr, Size64Bytes> instanceLayers = new StackList <IntPtr, Size64Bytes>(); if (!availableInstanceExtensions.Contains(CommonStrings.VK_KHR_SURFACE_EXTENSION_NAME)) { throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_SURFACE_EXTENSION_NAME}"); } instanceExtensions.Add(CommonStrings.VK_KHR_SURFACE_EXTENSION_NAME); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!availableInstanceExtensions.Contains(CommonStrings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME)) { throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME}"); } instanceExtensions.Add(CommonStrings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { if (!availableInstanceExtensions.Contains(CommonStrings.VK_KHR_XLIB_SURFACE_EXTENSION_NAME)) { throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_XLIB_SURFACE_EXTENSION_NAME}"); } instanceExtensions.Add(CommonStrings.VK_KHR_XLIB_SURFACE_EXTENSION_NAME); } else { throw new NotSupportedException("This platform does not support Vulkan."); } bool debugReportExtensionAvailable = false; if (debug) { if (availableInstanceExtensions.Contains(CommonStrings.VK_EXT_DEBUG_REPORT_EXTENSION_NAME)) { debugReportExtensionAvailable = true; instanceExtensions.Add(CommonStrings.VK_EXT_DEBUG_REPORT_EXTENSION_NAME); } if (availableInstanceLayers.Contains(CommonStrings.StandardValidationLayerName)) { instanceLayers.Add(CommonStrings.StandardValidationLayerName); } } instanceCI.enabledExtensionCount = instanceExtensions.Count; instanceCI.ppEnabledExtensionNames = (byte **)instanceExtensions.Data; instanceCI.enabledLayerCount = instanceLayers.Count; instanceCI.ppEnabledLayerNames = (byte **)instanceLayers.Data; VkResult result = vkCreateInstance(ref instanceCI, null, out _instance); CheckResult(result); if (debug && debugReportExtensionAvailable) { EnableDebugCallback(); } }
private VkInstance CreateInstance() { List <GCHandle> handles = new List <GCHandle>(); bool debug = false; string[] debugOnlyLayers = new string[] { //"VK_LAYER_GOOGLE_threading", //"VK_LAYER_LUNARG_parameter_validation", //"VK_LAYER_LUNARG_device_limits", Not Present? //"VK_LAYER_LUNARG_object_tracker", //"VK_LAYER_LUNARG_image", Not Present? "VK_LAYER_LUNARG_core_validation", //"VK_LAYER_LUNARG_swapchain", //"VK_LAYER_GOOGLE_unique_objects", }; List <string> layerList = new List <string>(); if (debug) { layerList.AddRange(debugOnlyLayers); } string[] layers = layerList.ToArray(); byte[][] pDebugLayers = new byte[layers.Length][]; byte *[] ppDebugLayerArray = new byte *[pDebugLayers.Length]; if (!debug) { ppDebugLayerArray = new byte *[1];//this is to give a null ptr to use later } for (int i = 0; i < pDebugLayers.Length; i++) { pDebugLayers[i] = Encoding.UTF8.GetBytes(layers[i] + char.MinValue); GCHandle handle = GCHandle.Alloc(pDebugLayers[i]); handles.Add(handle); fixed(byte *p = &(((byte[])handle.Target)[0])) { ppDebugLayerArray[i] = p; } } List <string> requiredExtensions = new List <string>(); requiredExtensions.Add("VK_EXT_debug_report"); requiredExtensions.AddRange(GLFW.Vulkan.GetRequiredInstanceExtensions()); string[] extensionNames = requiredExtensions.ToArray(); byte[][] pExtensionNames = new byte[extensionNames.Length][]; byte *[] ppExtensionNamesArray = new byte *[extensionNames.Length]; for (int i = 0; i < pExtensionNames.Length; i++) { pExtensionNames[i] = Encoding.UTF8.GetBytes(extensionNames[i] + char.MinValue); GCHandle handle = GCHandle.Alloc(pExtensionNames[i]); handles.Add(handle); fixed(byte *p = &(((byte[])handle.Target)[0])) { ppExtensionNamesArray[i] = p; } } VkInstance instance = new VkInstance(); fixed(byte **layersptr = &ppDebugLayerArray[0]) { fixed(byte **extensions = &ppExtensionNamesArray[0]) { VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.New(); pCreateInfo.ppEnabledLayerNames = layersptr; pCreateInfo.ppEnabledExtensionNames = extensions; pCreateInfo.enabledLayerCount = (uint)pDebugLayers.Length; pCreateInfo.enabledExtensionCount = (uint)extensionNames.Length; Assert(vkCreateInstance(&pCreateInfo, null, &instance)); } } foreach (var handle in handles) { handle.Free(); } DebugDelegate debugDelegate = new DebugDelegate(DebugCallback); //PFN_vkDebugReportCallbackEXT _debugCallbackFunc =(PFN_vkDebugReportCallbackEXT) debugDelegate; IntPtr debugFunctionPtr = Marshal.GetFunctionPointerForDelegate(debugDelegate); debugDelegateHandle = GCHandle.Alloc(debugDelegate); VkDebugReportCallbackCreateInfoEXT createInfoEXT = VkDebugReportCallbackCreateInfoEXT.New(); createInfoEXT.pfnCallback = debugFunctionPtr; createInfoEXT.flags = //VkDebugReportFlagsEXT.DebugEXT | VkDebugReportFlagsEXT.ErrorEXT | VkDebugReportFlagsEXT.WarningEXT | (VkDebugReportFlagsEXT)int.MaxValue; byte[] debugExtFnName = Encoding.UTF8.GetBytes("vkCreateDebugReportCallbackEXT" + char.MinValue); IntPtr createFnPtr; fixed(byte *namePtr = &(debugExtFnName[0])) { createFnPtr = vkGetInstanceProcAddr(instance, namePtr); } vkCreateDebugReportCallbackEXT_d createDelegate = Marshal.GetDelegateForFunctionPointer <vkCreateDebugReportCallbackEXT_d>(createFnPtr); VkDebugReportCallbackCreateInfoEXT *createInfoPtr = (&createInfoEXT); fixed(ulong *ptr = &(debugReport.Handle)) { Assert(createDelegate(instance, createInfoPtr, IntPtr.Zero, out debugReport)); } return(instance); }