Exemple #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;
            }
        }
Exemple #2
0
        public static unsafe DebugReportCallbackEXT CreateDebugReportCallback(Instance instance, DebugReportCallbackDelegate callback)
        {
            var name      = "vkCreateDebugReportCallbackEXT";
            var nameBytes = Encoding.ASCII.GetBytes(name);
            var procAddr  = Vk.GetInstanceProcAddr(instance, name);

            if (procAddr == IntPtr.Zero)
            {
                throw new NullReferenceException($"Didn't find InstanceProcAddr {nameBytes}");
            }

            debugDelegate = callback;
            GC.KeepAlive(debugDelegate);

            callbackHolder = Marshal.GetFunctionPointerForDelegate(callback);
            GC.KeepAlive(callbackHolder);

            var createDelegate = (CreateDebugReportCallbackEXT_Delegate)Marshal.GetDelegateForFunctionPointer(procAddr, typeof(CreateDebugReportCallbackEXT_Delegate));
            var createInfo     = new DebugReportCallbackCreateInfoEXT
            {
                Flags    = (DebugReportFlagsEXT)0x1F,//DebugReportFlagsEXT.Error | DebugReportFlagsEXT.Warning | DebugReportFlagsEXT.PerformanceWarning,
                Callback = callbackHolder,
            };

            var debugReportCallbackEXT = new DebugReportCallbackEXT();

            fixed(UInt64 *ptr = &debugReportCallbackEXT.NativePointer)
            {
                var result = createDelegate(instance.NativePointer, createInfo.NativePointer, null, ptr);

                if (result != Result.Success)
                {
                    throw new VulkanResultException("vkCreateDebugReportCallbackEXT", result);
                }
            }

            createInfo.Dispose();
            return(debugReportCallbackEXT);
        }
        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();
            }
        }
Exemple #4
0
 public abstract Result CreateDebugReportCallback([Count(Count = 0)] Instance instance, [Count(Count = 0), Flow(FlowDirection.In)] ref DebugReportCallbackCreateInfoEXT pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] ref AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out DebugReportCallbackEXT pCallback);
Exemple #5
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);
        }