Example #1
0
        internal unsafe static void CreateDebugCallbacks(
            Vk api,
            GraphicsDebugLevel logLevel,
            Instance instance,
            out ExtDebugReport debugReport,
            out DebugReportCallbackEXT debugReportCallback)
        {
            debugReport = default;

            if (logLevel != GraphicsDebugLevel.None)
            {
                if (!api.TryGetInstanceExtension(instance, out debugReport))
                {
                    debugReportCallback = default;
                    return;
                }

                var flags = logLevel switch
                {
                    GraphicsDebugLevel.Error => DebugReportFlagsEXT.DebugReportErrorBitExt,
                    GraphicsDebugLevel.Slowdowns => DebugReportFlagsEXT.DebugReportErrorBitExt | DebugReportFlagsEXT.DebugReportPerformanceWarningBitExt,
                    GraphicsDebugLevel.All => DebugReportFlagsEXT.DebugReportInformationBitExt |
                    DebugReportFlagsEXT.DebugReportWarningBitExt |
                    DebugReportFlagsEXT.DebugReportPerformanceWarningBitExt |
                    DebugReportFlagsEXT.DebugReportErrorBitExt |
                    DebugReportFlagsEXT.DebugReportDebugBitExt,
                    _ => throw new ArgumentException($"Invalid log level \"{logLevel}\".")
                };
                var debugReportCallbackCreateInfo = new DebugReportCallbackCreateInfoEXT()
                {
                    SType       = StructureType.DebugReportCallbackCreateInfoExt,
                    Flags       = flags,
                    PfnCallback = new PfnDebugReportCallbackEXT(DebugReport)
                };

                debugReport.CreateDebugReportCallback(instance, in debugReportCallbackCreateInfo, null, out debugReportCallback).ThrowOnError();
            }
            else
            {
                debugReportCallback = default;
            }
        }
Example #2
0
        void LoadInstanceLevelEntryPoints()
        {
            var instLoad = new VulkanInstanceHandleResolver(vk, vulkan.Instance);

            khrSurface    = new KhrSurface(instLoad);
            khrW32surface = new KhrWin32Surface(instLoad);
            if (supportDebugReport)
            {
                extDebugReport = new ExtDebugReport(instLoad);

                debugReportCallback = OnDebugReport;

                var inf = new DebugReportCallbackCreateInfoEXT()
                {
                    sType = StructureType.DebugReportCallbackCreateInfoExt,
                    pNext = IntPtr.Zero,
                    flags = DebugReportFlagBitsEXT.InformationBitExt | DebugReportFlagBitsEXT.WarningBitExt
                            | DebugReportFlagBitsEXT.PerformanceWarningBitExt | DebugReportFlagBitsEXT.ErrorExt | DebugReportFlagBitsEXT.DebugBitExt,
                    pfnCallback = Marshal.GetFunctionPointerForDelegate(debugReportCallback),
                    pUserData   = IntPtr.Zero,
                };
                extDebugReport.CreateDebugReportCallbackEXT(vulkan.Instance, ref inf, (AllocationCallbacks *)0, out debugReportHandle).CheckError();
            }
        }
Example #3
0
        public static Instance CreateInstance(Vk api, GraphicsDebugLevel logLevel, string[] requiredExtensions, out ExtDebugReport debugReport, out DebugReportCallbackEXT debugReportCallback)
        {
            var enabledLayers = new List <string>();

            void AddAvailableLayer(string layerName)
            {
                uint layerPropertiesCount;

                api.EnumerateInstanceLayerProperties(&layerPropertiesCount, null).ThrowOnError();

                LayerProperties[] layerProperties = new LayerProperties[layerPropertiesCount];

                fixed(LayerProperties *pLayerProperties = layerProperties)
                {
                    api.EnumerateInstanceLayerProperties(&layerPropertiesCount, layerProperties).ThrowOnError();

                    for (int i = 0; i < layerPropertiesCount; i++)
                    {
                        string currentLayerName = Marshal.PtrToStringAnsi((IntPtr)pLayerProperties[i].LayerName);

                        if (currentLayerName == layerName)
                        {
                            enabledLayers.Add(layerName);
                            return;
                        }
                    }
                }

                Logger.Warning?.Print(LogClass.Gpu, $"Missing layer {layerName}");
            }

            if (logLevel == GraphicsDebugLevel.Slowdowns || logLevel == GraphicsDebugLevel.All)
            {
                AddAvailableLayer("VK_LAYER_KHRONOS_validation");
            }

            var enabledExtensions = requiredExtensions.Append(ExtDebugReport.ExtensionName).ToArray();

            var appName = Marshal.StringToHGlobalAnsi(AppName);

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

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

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

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

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

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

            Marshal.FreeHGlobal(appName);

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

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

            if (!api.TryGetInstanceExtension(instance, out debugReport))
            {
                throw new Exception();
                // TODO: Exception.
            }

            if (logLevel != GraphicsDebugLevel.None)
            {
                var flags = logLevel switch
                {
                    GraphicsDebugLevel.Error => DebugReportFlagsEXT.DebugReportErrorBitExt,
                    GraphicsDebugLevel.Slowdowns => DebugReportFlagsEXT.DebugReportErrorBitExt | DebugReportFlagsEXT.DebugReportPerformanceWarningBitExt,
                    GraphicsDebugLevel.All => DebugReportFlagsEXT.DebugReportInformationBitExt |
                    DebugReportFlagsEXT.DebugReportWarningBitExt |
                    DebugReportFlagsEXT.DebugReportPerformanceWarningBitExt |
                    DebugReportFlagsEXT.DebugReportErrorBitExt |
                    DebugReportFlagsEXT.DebugReportDebugBitExt,
                    _ => throw new NotSupportedException()
                };
                var debugReportCallbackCreateInfo = new DebugReportCallbackCreateInfoEXT()
                {
                    SType       = StructureType.DebugReportCallbackCreateInfoExt,
                    Flags       = flags,
                    PfnCallback = new PfnDebugReportCallbackEXT(DebugReport)
                };

                debugReport.CreateDebugReportCallback(instance, in debugReportCallbackCreateInfo, null, out debugReportCallback).ThrowOnError();
            }
            else
            {
                debugReportCallback = default;
            }

            return(instance);
        }
Example #4
0
        internal static Instance CreateInstance(Vk api, GraphicsDebugLevel logLevel, string[] requiredExtensions, out ExtDebugReport debugReport, out DebugReportCallbackEXT debugReportCallback)
        {
            var enabledLayers = new List <string>();

            void AddAvailableLayer(string layerName)
            {
                uint layerPropertiesCount;

                api.EnumerateInstanceLayerProperties(&layerPropertiesCount, null).ThrowOnError();

                LayerProperties[] layerProperties = new LayerProperties[layerPropertiesCount];

                fixed(LayerProperties *pLayerProperties = layerProperties)
                {
                    api.EnumerateInstanceLayerProperties(&layerPropertiesCount, layerProperties).ThrowOnError();

                    for (int i = 0; i < layerPropertiesCount; i++)
                    {
                        string currentLayerName = Marshal.PtrToStringAnsi((IntPtr)pLayerProperties[i].LayerName);

                        if (currentLayerName == layerName)
                        {
                            enabledLayers.Add(layerName);
                            return;
                        }
                    }
                }

                Logger.Warning?.Print(LogClass.Gpu, $"Missing layer {layerName}");
            }

            if (logLevel != GraphicsDebugLevel.None)
            {
                AddAvailableLayer("VK_LAYER_KHRONOS_validation");
            }

            var enabledExtensions = requiredExtensions.Append(ExtDebugReport.ExtensionName).ToArray();

            var appName = Marshal.StringToHGlobalAnsi(AppName);

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

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

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

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

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

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

            Marshal.FreeHGlobal(appName);

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

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

            CreateDebugCallbacks(api, logLevel, instance, out debugReport, out debugReportCallback);

            return(instance);
        }