Exemple #1
0
        private unsafe void CreateInstance(IVkSurface vkSurface, string[] requestedExtensions, string[]?validationLayers, out Instance instance)
        {
            nint applicationName = Info.ApplicationName.Marshal();
            nint engineName      = Info.EngineName.Marshal();

            ApplicationInfo applicationInfo = new ApplicationInfo
            {
                SType              = StructureType.ApplicationInfo,
                PApplicationName   = (byte *)applicationName,
                ApplicationVersion = Info.ApplicationVersion,
                PEngineName        = (byte *)engineName,
                EngineVersion      = Info.EngineVersion,
                ApiVersion         = Info.APIVersion
            };

            string[] requiredExtensions        = VKAPI.GetRequiredExtensions(vkSurface, requestedExtensions);
            nint     requiredExtensionsPointer = SilkMarshal.StringArrayToPtr(requiredExtensions);
            nint     enabledLayerNames         = 0x0;
            int      enabledLayerCount         = 0;

            InstanceCreateInfo createInfo = new InstanceCreateInfo
            {
                SType                   = StructureType.InstanceCreateInfo,
                PApplicationInfo        = &applicationInfo,
                PpEnabledExtensionNames = (byte **)requiredExtensionsPointer,
                EnabledExtensionCount   = (uint)requiredExtensions.Length,
                PpEnabledLayerNames     = (byte **)null !,
                EnabledLayerCount       = 0,
                PNext                   = (void *)null !
            };

            if (validationLayers is not null)
            {
                VKAPI.VerifyValidationLayerSupport(VK, validationLayers);

                enabledLayerNames = SilkMarshal.StringArrayToPtr(validationLayers);
                enabledLayerCount = validationLayers.Length;
                createInfo.PpEnabledLayerNames = (byte **)enabledLayerNames;
                createInfo.EnabledLayerCount   = (uint)enabledLayerCount;
            }

            Log.Information(string.Format(FormatHelper.DEFAULT_LOGGING, nameof(VulkanInstance), "allocating instance."));
            Result result = VK.CreateInstance(&createInfo, (AllocationCallbacks *)null !, out instance);

            if (result is not Result.Success)
            {
                throw new VulkanException(result, "Failed to create Vulkan instance.");
            }

            SilkMarshal.FreeString(applicationName);
            SilkMarshal.FreeString(engineName);
            SilkMarshal.FreeString(requiredExtensionsPointer);

            if (enabledLayerNames is not 0x0 && enabledLayerCount is not 0)
            {
                SilkMarshal.FreeString(enabledLayerNames);
            }
        }
        void CreateInstance()
        {
            var appName = new InteropString("Hello Triangle");

            var appInfo = new VkApplicationInfo();

            appInfo.sType              = CSGL.Vulkan.VkStructureType.ApplicationInfo;
            appInfo.pApplicationName   = appName.Address;
            appInfo.applicationVersion = new CSGL.Vulkan.VkVersion(1, 0, 0);
            appInfo.engineVersion      = new CSGL.Vulkan.VkVersion(0, 0, 1);
            appInfo.apiVersion         = new CSGL.Vulkan.VkVersion(1, 0, 0);

            var appInfoNative = new Native <VkApplicationInfo>(appInfo);

            var info = new VkInstanceCreateInfo();

            info.sType            = CSGL.Vulkan.VkStructureType.InstanceCreateInfo;
            info.pApplicationInfo = appInfoNative.Address;

            var extensions       = GLFW.GetRequiredInstanceExceptions();
            var extensionsNative = new NativeStringArray(extensions);

            info.ppEnabledExtensionNames = extensionsNative.Address;
            info.enabledExtensionCount   = (uint)extensions.Count;

            var layersNative = new NativeStringArray(layers);

            info.ppEnabledLayerNames = layersNative.Address;
            info.enabledLayerCount   = (uint)layers.Length;

            var result = VK.CreateInstance(ref info, alloc, out instance);

            appName.Dispose();
            appInfoNative.Dispose();
            extensionsNative.Dispose();
            layersNative.Dispose();
        }