Beispiel #1
0
        private VkInstance CreateVulkanInstance()
        {
            NativeString name   = App.Name;
            NativeString engine = "Foster.Framework";

            // create the App Info
            var appInfo = new VkApplicationInfo
            {
                sType              = VkStructureType.ApplicationInfo,
                pApplicationName   = name,
                applicationVersion = VK.MAKE_VERSION(1, 0, 0),
                pEngineName        = engine,
                engineVersion      = VK.MAKE_VERSION(App.Version),
                apiVersion         = VK.MAKE_VERSION(1, 0, 0),
            };

            var createInfo = new VkInstanceCreateInfo
            {
                sType            = VkStructureType.InstanceCreateInfo,
                pApplicationInfo = &appInfo,
            };

            // required validation layers
            using var validationLayerNames = new NativeStringArray(validationLayers);
            if (HasValidationLayers)
            {
                createInfo.enabledLayerCount   = validationLayerNames.Length;
                createInfo.ppEnabledLayerNames = validationLayerNames;
            }

            // get the required Vulkan Extensions
            var exts = System.GetVKExtensions();

            if (HasValidationLayers)
            {
                exts.Add(VkConst.EXT_DEBUG_UTILS_EXTENSION_NAME);
            }

            using var extensions               = new NativeStringArray(exts);
            createInfo.enabledExtensionCount   = extensions.Length;
            createInfo.ppEnabledExtensionNames = extensions;

            // create instance
            var result = VK.CreateInstance(&createInfo, null, out var instance);

            if (result != VkResult.Success)
            {
                throw new Exception($"Failed to create Vulkan Instance, {result}");
            }

            return(instance);
        }
Beispiel #2
0
        protected override void FirstWindowCreated()
        {
            VK = new VK(this);

            // Debug Callback
            if (HasValidationLayers)
            {
                DebugMessenger = CreateDebugMessenger((messageSeverity, messageTypes, pCallbackData, pUserData) =>
                {
                    var message = VK.STRING(pCallbackData->pMessage);

                    if (messageSeverity.HasFlag(VkDebugUtilsMessageSeverityFlagsEXT.Error))
                    {
                        Log.Error(Name, message);
                    }
                    else if (messageSeverity.HasFlag(VkDebugUtilsMessageSeverityFlagsEXT.Warning))
                    {
                        Log.Warning(Name, message);
                    }
                    else
                    {
                        Log.Message(Name, message);
                    }

                    return(VkConst.FALSE);
                });
            }

            // Pick a Physical Device
            PhysicalDevice = PickPhysicalDevice();

            // get the API version
            {
                VkPhysicalDeviceProperties properties;
                VK.GetPhysicalDeviceProperties(PhysicalDevice, &properties);
                ApiVersion = VK.UNMAKE_VERSION(properties.apiVersion);

                int length = 0;
                while (length < VkConst.MAX_PHYSICAL_DEVICE_NAME_SIZE && properties.deviceName[length] != 0)
                {
                    length++;
                }
                DeviceName = Encoding.UTF8.GetString(properties.deviceName, length);
            }

            // Create the Device
            {
                TryGetQueueFamilyIndex(PhysicalDevice, VkQueueFlags.GraphicsBit, out uint graphicsFamilyIndex);

                // Graphics Family Queue
                var priority        = 1.0f;
                var queueCreateInfo = new VkDeviceQueueCreateInfo
                {
                    sType            = VkStructureType.DeviceQueueCreateInfo,
                    queueFamilyIndex = graphicsFamilyIndex,
                    queueCount       = 1,
                    pQueuePriorities = &priority
                };

                // Device Features
                var deviceFeatures = new VkPhysicalDeviceFeatures();

                var createInfo = new VkDeviceCreateInfo
                {
                    sType                = VkStructureType.DeviceCreateInfo,
                    pQueueCreateInfos    = &queueCreateInfo,
                    queueCreateInfoCount = 1,
                    pEnabledFeatures     = &deviceFeatures,
                };

                // Device Extensions
                using var deviceExtensionNames     = new NativeStringArray(deviceExtensions);
                createInfo.enabledExtensionCount   = deviceExtensionNames.Length;
                createInfo.ppEnabledExtensionNames = deviceExtensionNames;

                var result = VK.CreateDevice(PhysicalDevice, &createInfo, null, out Device);
                if (result != VkResult.Success)
                {
                    throw new Exception($"Failed to create Vulkan Logical Device, {result}");
                }

                // Get the Graphics Queue
                VK.GetDeviceQueue(Device, graphicsFamilyIndex, 0, out GraphicsQueue);
            }
        }