Beispiel #1
0
        private static VulkanPlatformInterface TryCreate()
        {
            _options = AvaloniaLocator.Current.GetService <VulkanOptions>() ?? new VulkanOptions();

            var instance = VulkanInstance.Create(_options);

            return(new VulkanPlatformInterface(instance));
        }
Beispiel #2
0
        internal static unsafe VulkanInstance Create(VulkanOptions options)
        {
            var api               = Vk.GetApi();
            var applicationName   = Marshal.StringToHGlobalAnsi(options.ApplicationName);
            var engineName        = Marshal.StringToHGlobalAnsi(EngineName);
            var enabledExtensions = new List <string>(options.InstanceExtensions);

            enabledExtensions.AddRange(RequiredInstanceExtensions);

            var applicationInfo = new ApplicationInfo
            {
                PApplicationName   = (byte *)applicationName,
                ApiVersion         = Vk.Version12.Value,
                PEngineName        = (byte *)engineName,
                EngineVersion      = new Version32(1, 0, 0),
                ApplicationVersion = new Version32(1, 0, 0)
            };

            var enabledLayers = new HashSet <string>();

            if (options.UseDebug)
            {
                enabledExtensions.Add(ExtDebugUtils.ExtensionName);
                enabledExtensions.Add(ExtDebugReport.ExtensionName);
                if (IsLayerAvailable(api, "VK_LAYER_KHRONOS_validation"))
                {
                    enabledLayers.Add("VK_LAYER_KHRONOS_validation");
                }
            }

            foreach (var layer in options.EnabledLayers)
            {
                enabledLayers.Add(layer);
            }

            var ppEnabledExtensions = stackalloc IntPtr[enabledExtensions.Count];
            var ppEnabledLayers     = stackalloc IntPtr[enabledLayers.Count];

            for (var i = 0; i < enabledExtensions.Count; i++)
            {
                ppEnabledExtensions[i] = Marshal.StringToHGlobalAnsi(enabledExtensions[i]);
            }

            var layers = enabledLayers.ToList();

            for (var i = 0; i < enabledLayers.Count; i++)
            {
                ppEnabledLayers[i] = Marshal.StringToHGlobalAnsi(layers[i]);
            }

            var instanceCreateInfo = new InstanceCreateInfo
            {
                SType                   = StructureType.InstanceCreateInfo,
                PApplicationInfo        = &applicationInfo,
                PpEnabledExtensionNames = (byte **)ppEnabledExtensions,
                PpEnabledLayerNames     = (byte **)ppEnabledLayers,
                EnabledExtensionCount   = (uint)enabledExtensions.Count,
                EnabledLayerCount       = (uint)enabledLayers.Count
            };

            api.CreateInstance(in instanceCreateInfo, null, out var instance).ThrowOnError();

            Marshal.FreeHGlobal(applicationName);
            Marshal.FreeHGlobal(engineName);

            for (var i = 0; i < enabledExtensions.Count; i++)
            {
                Marshal.FreeHGlobal(ppEnabledExtensions[i]);
            }

            for (var i = 0; i < enabledLayers.Count; i++)
            {
                Marshal.FreeHGlobal(ppEnabledLayers[i]);
            }

            return(new VulkanInstance(instance, api));
        }