Example #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);
            }
        }
Example #2
0
        public unsafe VulkanInstance(Vk vk, IVkSurface vkSurface, VulkanInstanceInfo info, string[] requestedExtensions, string[]?validationLayers)
        {
            VK   = vk;
            Info = info;

            CreateInstance(vkSurface, requestedExtensions, validationLayers, out _VKInstance);
            SurfaceExtension = GetInstanceExtension <SurfaceExtension>();
            Handle           = _VKInstance.ToHandle();
            Surface          = vkSurface.Create(Handle, (AllocationCallbacks *)null !).ToSurface();

            Log.Debug(string.Format(FormatHelper.DEFAULT_LOGGING, nameof(VulkanInstance), "Created."));
        }
Example #3
0
        public static unsafe Instance CreateInstance(string[] instanceLayers, string[] instanceExtensions, IVkSurface windowSurface)
        {
            ApplicationInfo appInfo = new()
            {
                SType = StructureType.ApplicationInfo,

                PApplicationName   = (byte *)SilkMarshal.StringToPtr("SilkyNvg-Vulkan-Example"),
                ApplicationVersion = Vk.MakeVersion(1, 0, 0),
                PEngineName        = (byte *)SilkMarshal.StringToPtr("SilkyNvg (Renderer: Vulkan)"),
                EngineVersion      = Vk.MakeVersion(1, 0, 0),
                ApiVersion         = Vk.Version12
            };

            byte **windowExtensionsPtr   = windowSurface.GetRequiredExtensions(out uint windowExtensionCount);
            byte **instanceExtensionsPtr = (byte **)SilkMarshal.StringArrayToPtr(instanceExtensions);
            byte **extensions            = stackalloc byte *[(int)windowExtensionCount + instanceExtensions.Length];
            int    i = 0;

            for (; i < windowExtensionCount; i++)
            {
                extensions[i] = windowExtensionsPtr[i];
            }
            for (; i < windowExtensionCount + instanceExtensions.Length; i++)
            {
                extensions[i] = instanceExtensionsPtr[i];
            }
            CheckInstanceExtensionsPresent(extensions, (int)windowExtensionCount + instanceExtensions.Length);

            CheckInstanceLayersPresent(instanceLayers);
            byte **layers = (byte **)SilkMarshal.StringArrayToPtr(instanceLayers);

            InstanceCreateInfo instanceCreateInfo = VkInit.InstanceCreateInfo(appInfo, layers,
                                                                              (uint)instanceLayers.Length, extensions, windowExtensionCount);

            AssertVulkan(Vk.CreateInstance(instanceCreateInfo, null, out Instance instance));
            return(instance);
        }