private void CreateInstance()
        {
#if DEBUG
            if (!this.CheckValidationLayerSupport())
            {
                throw new Exception("Validation layers requested, but not available!");
            }
#endif
            VkApplicationInfo appInfo = new VkApplicationInfo()
            {
                sType              = VkStructureType.VK_STRUCTURE_TYPE_APPLICATION_INFO,
                pApplicationName   = "Hello Triangle".ToPointer(),
                applicationVersion = Helpers.Version(1, 0, 0),
                pEngineName        = "No Engine".ToPointer(),
                engineVersion      = Helpers.Version(1, 0, 0),
                apiVersion         = Helpers.Version(1, 2, 0),
            };

            VkInstanceCreateInfo createInfo = default;
            createInfo.sType            = VkStructureType.VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
            createInfo.pApplicationInfo = &appInfo;

            // Extensions
            this.GetAllInstanceExtensionsAvailables();

            IntPtr *extensionsToBytesArray = stackalloc IntPtr[extensions.Length];
            for (int i = 0; i < extensions.Length; i++)
            {
                extensionsToBytesArray[i] = Marshal.StringToHGlobalAnsi(extensions[i]);
            }
            createInfo.enabledExtensionCount   = (uint)extensions.Length;
            createInfo.ppEnabledExtensionNames = (byte **)extensionsToBytesArray;

            // Validation layers
#if DEBUG
            IntPtr *layersToBytesArray = stackalloc IntPtr[validationLayers.Length];
            for (int i = 0; i < validationLayers.Length; i++)
            {
                layersToBytesArray[i] = Marshal.StringToHGlobalAnsi(validationLayers[i]);
            }

            createInfo.enabledLayerCount   = (uint)validationLayers.Length;
            createInfo.ppEnabledLayerNames = (byte **)layersToBytesArray;
#else
            createInfo.enabledLayerCount = 0;
            createInfo.pNext             = null;
#endif

            fixed(VkInstance *instancePtr = &instance)
            {
                Helpers.CheckErrors(VulkanNative.vkCreateInstance(&createInfo, null, instancePtr));
                VulkanNative.LoadFuncionPointers(instance);
            }
        }
Exemple #2
0
        private void CreateInstance()
        {
            ////var availableExtensions = EnumerateInstanceExtensions();
            ////var availableLayers = EnumerateInstanceLayers();

            VkApplicationInfo appInfo = new VkApplicationInfo()
            {
                sType            = VkStructureType.VK_STRUCTURE_TYPE_APPLICATION_INFO,
                pApplicationName = "Hello Triangle".ToPointer(),
                pEngineName      = "WaveEngine".ToPointer(),
                engineVersion    = Helpers.Version(3, 0, 0),
            };

            string[] extensions = new string[]
            {
                "VK_KHR_surface",
                "VK_KHR_win32_surface",
                "VK_EXT_debug_report",
            };

            IntPtr *extensionsToEnableArray = stackalloc IntPtr[extensions.Length];

            for (int i = 0; i < extensions.Length; i++)
            {
                string extension = extensions[i];
                extensionsToEnableArray[i] = Marshal.StringToHGlobalAnsi(extension);
            }

            string[] layers = new string[]
            {
                "VK_LAYER_LUNARG_standard_validation"
            };

            IntPtr *layersToEnableArray = stackalloc IntPtr[layers.Length];

            for (int i = 0; i < layers.Length; i++)
            {
                string layer = layers[i];
                layersToEnableArray[i] = Marshal.StringToHGlobalAnsi(layer);
            }

            VkInstanceCreateInfo createInfo = new VkInstanceCreateInfo()
            {
                sType = VkStructureType.VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
                ppEnabledExtensionNames = (byte **)extensionsToEnableArray,
                enabledExtensionCount   = (uint)extensions.Length,
                enabledLayerCount       = (uint)layers.Length,
                ppEnabledLayerNames     = (byte **)layersToEnableArray,
                pApplicationInfo        = &appInfo,
            };

            VkInstance newInstance;
            var        result = VulkanNative.vkCreateInstance(&createInfo, null, &newInstance);

            this.vkInstance = newInstance;
            Helpers.CheckErrors(result);

            for (int i = 0; i < extensions.Length; i++)
            {
                Marshal.FreeHGlobal(extensionsToEnableArray[i]);
            }

            for (int i = 0; i < layers.Length; i++)
            {
                Marshal.FreeHGlobal(layersToEnableArray[i]);
            }

            EnableDebugCallback();
        }
Exemple #3
0
        public VkInstance CreateInstance()
        {
            VkApplicationInfo AppInfo = new VkApplicationInfo()
            {
                sType              = VkStructureType.ApplicationInfo,
                apiVersion         = new Version(1, 0, 0),
                applicationVersion = new Version(0, 0, 1),
                engineVersion      = new Version(0, 0, 2),
                pApplicationName   = Interop.String.ToPointer("Zeckoxe Engine"),
                pEngineName        = Interop.String.ToPointer("Zeckoxe"),
            };


            if (Parameters.Settings.Validation)
            {
                // TODO:
            }

            EnumerateInstanceExtensions = Tools.EnumerateInstanceExtensions();

            if (EnumerateInstanceExtensions.Contains("VK_KHR_surface"))
            {
                InstanceExtensions.Add("VK_KHR_surface");
            }


            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if (EnumerateInstanceExtensions.Contains("VK_KHR_win32_surface"))
                {
                    InstanceExtensions.Add("VK_KHR_win32_surface");
                }
            }


            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                if (EnumerateInstanceExtensions.Contains("VK_MVK_macos_surface"))
                {
                    InstanceExtensions.Add("VK_MVK_macos_surface");
                }


                if (EnumerateInstanceExtensions.Contains("VK_MVK_ios_surface"))
                {
                    InstanceExtensions.Add("VK_MVK_ios_surface");
                }
            }


            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                if (EnumerateInstanceExtensions.Contains("VK_KHR_android_surface"))
                {
                    InstanceExtensions.Add("VK_KHR_android_surface");
                }


                if (EnumerateInstanceExtensions.Contains("VK_KHR_xlib_surface"))
                {
                    InstanceExtensions.Add("VK_KHR_xlib_surface");
                }


                if (EnumerateInstanceExtensions.Contains("VK_KHR_wayland_surface"))
                {
                    InstanceExtensions.Add("VK_KHR_wayland_surface");
                }
            }



            VkInstanceCreateInfo instanceCreateInfo = new VkInstanceCreateInfo()
            {
                sType                   = VkStructureType.InstanceCreateInfo,
                pNext                   = null,
                pApplicationInfo        = &AppInfo,
                enabledExtensionCount   = (uint)InstanceExtensions.Count(),
                ppEnabledExtensionNames = Interop.String.AllocToPointers(InstanceExtensions.ToArray()),
            };


            VkInstance instance;

            VulkanNative.vkCreateInstance(&instanceCreateInfo, (VkAllocationCallbacks *)null, &instance);
            return(instance);
        }
        public unsafe GraphicsService()
        {
            //create vulkan info
            var applicationInfo = new VkApplicationInfo
            {
                sType              = VkStructureType.ApplicationInfo,
                apiVersion         = new VkVersion(1, 0, 0),
                applicationVersion = new VkVersion(1, 0, 0),
                engineVersion      = new VkVersion(1, 0, 0),
                pApplicationName   = new FixedUtf8String("Tortuga"),
                pEngineName        = new FixedUtf8String("Tortuga")
            };

            //vulkan extensions
            var instanceExtensions = new NativeList <IntPtr>();

            instanceExtensions.Add(GraphicsApiConstants.VK_KHR_SURFACE_EXTENSION_NAME);
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                instanceExtensions.Add(GraphicsApiConstants.VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                instanceExtensions.Add(GraphicsApiConstants.VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                instanceExtensions.Add(GraphicsApiConstants.VK_MVK_SURFACE_EXTENSION_NAME);
            }
            else
            {
                throw new NotSupportedException("This platform is not supported");
            }

            if (Settings.IsGraphicsApiDebugingEnabled)
            {
                instanceExtensions.Add(GraphicsApiConstants.VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
            }

            //vulkan validation layers
            var validationLayer = new NativeList <IntPtr>();

            if (Settings.IsGraphicsApiDebugingEnabled)
            {
                uint supportedLayersCount;
                VulkanNative.vkEnumerateInstanceLayerProperties(&supportedLayersCount, null);
                var supportedLayers = new NativeList <VkLayerProperties>(supportedLayersCount);
                supportedLayers.Count = supportedLayersCount;
                VulkanNative.vkEnumerateInstanceLayerProperties(
                    &supportedLayersCount,
                    (VkLayerProperties *)supportedLayers.Data.ToPointer()
                    );
                foreach (var vl in supportedLayers)
                {
                    var validationName = Encoding.ASCII.GetString(vl.layerName, 256).Replace("\0", "");
                    Console.WriteLine($"Supported Validation Layer: {validationName}");
                    validationLayer.Add(new IntPtr(vl.layerName));
                }
            }

            var instanceInfo = new VkInstanceCreateInfo
            {
                sType                   = VkStructureType.InstanceCreateInfo,
                pApplicationInfo        = &applicationInfo,
                enabledExtensionCount   = instanceExtensions.Count,
                ppEnabledExtensionNames = (byte **)instanceExtensions.Data,
                enabledLayerCount       = validationLayer.Count,
                ppEnabledLayerNames     = (byte **)validationLayer.Data
            };

            VkInstance instance;

            if (VulkanNative.vkCreateInstance(
                    &instanceInfo,
                    null,
                    &instance
                    ) != VkResult.Success)
            {
                throw new Exception("failed to initialize graphics api");
            }
            _handle = instance;

            if (Settings.IsGraphicsApiDebugingEnabled)
            {
                if (CreateDebugReportCallback(
                        GetVulkanDebugFlags
                        ) != VkResult.Success)
                {
                    throw new Exception("failed to start graphics api debugger");
                }
            }
            //get vulkan physical device list
            uint deviceCount = 0;

            if (VulkanNative.vkEnumeratePhysicalDevices(
                    _handle,
                    ref deviceCount,
                    null
                    ) != VkResult.Success)
            {
                throw new Exception("could not get physical devices");
            }
            var physicalDevices = new NativeList <VkPhysicalDevice>(deviceCount);

            physicalDevices.Count = deviceCount;
            if (VulkanNative.vkEnumeratePhysicalDevices(
                    _handle,
                    ref deviceCount,
                    (VkPhysicalDevice *)physicalDevices.Data.ToPointer()
                    ) != VkResult.Success)
            {
                throw new Exception("could not get physical devices");
            }

            //setup devices
            _devices = new List <Device>();
            for (int i = 0; i < deviceCount; i++)
            {
                _devices.Add(new Device(physicalDevices[i]));
            }

            //sort devices with best to worst
            _devices.Sort((a, b) => b.Score.CompareTo(a.Score));
        }