Browse Source

[GL] allow debug with glDebugMessageCallback (#812)

Yuxiao Mao 2 weeks ago
parent
commit
8b7b26bd86
3 changed files with 36 additions and 2 deletions
  1. 6 2
      libs/sdl/GLImports.h
  2. 25 0
      libs/sdl/gl.c
  3. 5 0
      libs/sdl/sdl/GL.hx

+ 6 - 2
libs/sdl/GLImports.h

@@ -123,12 +123,14 @@ GL_IMPORT(glColorMaski, COLORMASKI);
 GL_IMPORT(glTexStorage2D, TEXSTORAGE2D);
 GL_IMPORT(glTexStorage2D, TEXSTORAGE2D);
 GL_IMPORT(glTexStorage3D, TEXSTORAGE3D);
 GL_IMPORT(glTexStorage3D, TEXSTORAGE3D);
 
 
+GL_IMPORT(glDebugMessageCallback, DEBUGMESSAGECALLBACK);
+GL_IMPORT(glDebugMessageControl, DEBUGMESSAGECONTROL);
+
 #if !defined(HL_MESA)
 #if !defined(HL_MESA)
 GL_IMPORT(glGetQueryObjectui64v, GETQUERYOBJECTUI64V);
 GL_IMPORT(glGetQueryObjectui64v, GETQUERYOBJECTUI64V);
 GL_IMPORT(glQueryCounter, QUERYCOUNTER);
 GL_IMPORT(glQueryCounter, QUERYCOUNTER);
 #endif
 #endif
 
 
-#endif
 #if defined(_WIN32) || defined(HL_NX)
 #if defined(_WIN32) || defined(HL_NX)
 GL_IMPORT(glBlendEquation, BLENDEQUATION);
 GL_IMPORT(glBlendEquation, BLENDEQUATION);
 GL_IMPORT(glActiveTexture, ACTIVETEXTURE);
 GL_IMPORT(glActiveTexture, ACTIVETEXTURE);
@@ -142,4 +144,6 @@ GL_IMPORT(glCompressedTexSubImage3D, COMPRESSEDTEXSUBIMAGE3D);
 
 
 #if defined(HL_NX)
 #if defined(HL_NX)
 GL_IMPORT(glClearDepthf, CLEARDEPTHF);
 GL_IMPORT(glClearDepthf, CLEARDEPTHF);
-#endif
+#endif
+
+#endif

+ 25 - 0
libs/sdl/gl.c

@@ -69,6 +69,14 @@ static int GLLoadAPI() {
 	return 0;
 	return 0;
 }
 }
 
 
+#ifdef GL_VERSION_4_3
+static void APIENTRY debug_message_callback( GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam ) {
+	fprintf(stderr, "GL %s: type = 0x%x, severity = 0x%x, message = %s\n",
+		( type == GL_DEBUG_TYPE_ERROR ? "** ERROR **" : "DEBUG" ),
+		type, severity, message);
+}
+#endif
+
 #define ZIDX(val) ((val)?(val)->v.i:0)
 #define ZIDX(val) ((val)?(val)->v.i:0)
 
 
 // globals
 // globals
@@ -76,6 +84,22 @@ HL_PRIM bool HL_NAME(gl_init)() {
 	return GLLoadAPI() == 0;
 	return GLLoadAPI() == 0;
 }
 }
 
 
+HL_PRIM bool HL_NAME(gl_set_debug)( bool enable ) {
+#ifdef GL_VERSION_4_3
+	if( enable ) {
+		glEnable(GL_DEBUG_OUTPUT);
+		glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE, GL_DONT_CARE, 0, NULL, GL_FALSE);
+		glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_OTHER, GL_DONT_CARE, 0, NULL, GL_FALSE);
+		glDebugMessageCallback(debug_message_callback, 0);
+	} else {
+		glDisable(GL_DEBUG_OUTPUT);
+	}
+	return true;
+#else
+	return false;
+#endif
+}
+
 HL_PRIM bool HL_NAME(gl_is_context_lost)() {
 HL_PRIM bool HL_NAME(gl_is_context_lost)() {
 	// seems like a GL context is rarely lost on desktop
 	// seems like a GL context is rarely lost on desktop
 	// let's look at it again on mobile
 	// let's look at it again on mobile
@@ -716,6 +740,7 @@ HL_PRIM void HL_NAME(gl_shader_storage_block_binding)( vdynamic *p, int index, i
 }
 }
 
 
 DEFINE_PRIM(_BOOL,gl_init,_NO_ARG);
 DEFINE_PRIM(_BOOL,gl_init,_NO_ARG);
+DEFINE_PRIM(_BOOL,gl_set_debug,_BOOL);
 DEFINE_PRIM(_BOOL,gl_is_context_lost,_NO_ARG);
 DEFINE_PRIM(_BOOL,gl_is_context_lost,_NO_ARG);
 DEFINE_PRIM(_VOID,gl_clear,_I32);
 DEFINE_PRIM(_VOID,gl_clear,_I32);
 DEFINE_PRIM(_I32,gl_get_error,_NO_ARG);
 DEFINE_PRIM(_I32,gl_get_error,_NO_ARG);

+ 5 - 0
libs/sdl/sdl/GL.hx

@@ -34,6 +34,11 @@ class GL {
 		return false;
 		return false;
 	}
 	}
 
 
+	@:hlNative("?sdl","gl_set_debug")
+	public static function setDebug( enable : Bool ) : Bool {
+		return false;
+	}
+
 	// non standard
 	// non standard
 	public static function getConfigParameter( v : Int ) : Int {
 	public static function getConfigParameter( v : Int ) : Int {
 		return 0;
 		return 0;