using System; using System.Diagnostics; using System.Net.Http; using System.Runtime.InteropServices; using OpenTK.Graphics.OpenGL4; using OpenTK.Platform.Egl; using ErrorCode = OpenTK.Graphics.OpenGL4.ErrorCode; namespace SM.OGL { /// /// Contains everything that is needed to debug OpenGL /// public static class GLDebugging { private static DebugProc _debugProc = DebugCallback; private static GCHandle _debugGcHandle; /// /// A action that is performed, when a OpenGL-error occurs. /// public static Action DebugAction = DefaultDebugAction; [DebuggerStepThrough] private static void DebugCallback(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr message, IntPtr userparam) { string msg = Marshal.PtrToStringAnsi(message, length); DebugAction?.Invoke(source, type, severity, msg); } /// /// Enables the debugging. /// public static void EnableDebugging() { try { _debugGcHandle = GCHandle.Alloc(_debugProc); GL.DebugMessageCallback(_debugProc, IntPtr.Zero); GL.Enable(EnableCap.DebugOutput); GL.Enable(EnableCap.DebugOutputSynchronous); } catch (AccessViolationException) { Console.WriteLine("Enableing proper GLDebugging failed. \n" + "Often it fails, because your hardware doesn't provide proper OpenGL 4 \n" + " or KHR_debug extension support."); } } /// /// Default action for 'DebugAction'. /// /// /// /// /// public static void DefaultDebugAction(DebugSource source, DebugType type, DebugSeverity severity, string msg) { Console.WriteLine($"{severity}, {type}, {source} -> {msg}"); if (type == DebugType.DebugTypeError) throw new Exception(msg); } /// /// A action, that is performed, when find an error. /// public static Action GlErrorAction; /// /// Checks for OpenGL errors. /// public static bool CheckGLErrors() { bool hasError = false; ErrorCode c; while ((c = GL.GetError()) != ErrorCode.NoError) { hasError = true; GlErrorAction?.Invoke(c); } return hasError; } } }