From 9554e3d43a72a55de06d4f1b9168af29a929600a Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 29 Dec 2024 19:24:33 +0100 Subject: [PATCH] Add support for glObjectLabel to aid debugging --- irr/src/COpenGLCoreTexture.h | 6 ++++++ irr/src/COpenGLExtensionHandler.h | 4 ++++ irr/src/OpenGL/Driver.cpp | 23 +++++++++++++++-------- irr/src/OpenGL/ExtensionHandler.h | 13 +++++++++++++ irr/src/OpenGL/MaterialRenderer.cpp | 7 ++++++- irr/src/OpenGL/MaterialRenderer.h | 5 ++++- irr/src/OpenGL/Renderer2D.cpp | 4 ++-- irr/src/OpenGL3/Driver.cpp | 3 +++ irr/src/OpenGLES2/Driver.cpp | 3 +++ 9 files changed, 56 insertions(+), 12 deletions(-) diff --git a/irr/src/COpenGLCoreTexture.h b/irr/src/COpenGLCoreTexture.h index d8a813d5d..2254076a7 100644 --- a/irr/src/COpenGLCoreTexture.h +++ b/irr/src/COpenGLCoreTexture.h @@ -137,6 +137,9 @@ public: Images.clear(); } + if (!name.empty()) + Driver->irrGlObjectLabel(GL_TEXTURE, TextureName, name.c_str()); + Driver->getCacheHandler()->getTextureCache().set(0, prevTexture); TEST_GL_ERROR(Driver); @@ -247,6 +250,9 @@ public: break; } + if (!name.empty()) + Driver->irrGlObjectLabel(GL_TEXTURE, TextureName, name.c_str()); + Driver->getCacheHandler()->getTextureCache().set(0, prevTexture); if (TEST_GL_ERROR(Driver)) { char msg[256]; diff --git a/irr/src/COpenGLExtensionHandler.h b/irr/src/COpenGLExtensionHandler.h index a1754a328..63c55f26c 100644 --- a/irr/src/COpenGLExtensionHandler.h +++ b/irr/src/COpenGLExtensionHandler.h @@ -1065,6 +1065,10 @@ public: void irrGlCompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *data); + inline void irrGlObjectLabel(GLenum identifier, GLuint name, const char *label) + { + // unimplemented + } // shader programming void extGlGenPrograms(GLsizei n, GLuint *programs); diff --git a/irr/src/OpenGL/Driver.cpp b/irr/src/OpenGL/Driver.cpp index c33b3c9d9..31fb7a177 100644 --- a/irr/src/OpenGL/Driver.cpp +++ b/irr/src/OpenGL/Driver.cpp @@ -164,13 +164,6 @@ COpenGL3DriverBase::COpenGL3DriverBase(const SIrrlichtCreationParameters ¶ms ExposedData = ContextManager->getContext(); ContextManager->activateContext(ExposedData, false); GL.LoadAllProcedures(ContextManager); - if (EnableErrorTest && GL.IsExtensionPresent("GL_KHR_debug")) { - GL.Enable(GL_DEBUG_OUTPUT); - GL.DebugMessageCallback(debugCb, this); - } else if (EnableErrorTest) { - os::Printer::log("GL debug extension not available"); - } - initQuadsIndices(); TEST_GL_ERROR(this); } @@ -248,6 +241,20 @@ bool COpenGL3DriverBase::genericDriverInit(const core::dimension2d &screenS initFeatures(); printTextureFormats(); + if (EnableErrorTest) { + if (KHRDebugSupported) { + GL.Enable(GL_DEBUG_OUTPUT); + GL.DebugMessageCallback(debugCb, this); + } else { + os::Printer::log("GL debug extension not available"); + } + } else { + // don't do debug things if they are not wanted (even if supported) + KHRDebugSupported = false; + } + + initQuadsIndices(); + // reset cache handler delete CacheHandler; CacheHandler = new COpenGL3CacheHandler(this); @@ -1615,7 +1622,7 @@ s32 COpenGL3DriverBase::addHighLevelShaderMaterial( s32 nr = -1; COpenGL3MaterialRenderer *r = new COpenGL3MaterialRenderer( this, nr, vertexShaderProgram, - pixelShaderProgram, + pixelShaderProgram, shaderName, callback, baseMaterial, userData); r->drop(); diff --git a/irr/src/OpenGL/ExtensionHandler.h b/irr/src/OpenGL/ExtensionHandler.h index 0e3a0af2d..209fd415b 100644 --- a/irr/src/OpenGL/ExtensionHandler.h +++ b/irr/src/OpenGL/ExtensionHandler.h @@ -161,10 +161,23 @@ public: GL.BlendEquation(mode); } + inline void irrGlObjectLabel(GLenum identifier, GLuint name, const char *label) + { + if (KHRDebugSupported) { + u32 len = strlen(label); + // Since our texture strings can get quite long we also truncate + // to a hardcoded limit of 82 + len = std::min(len, std::min(MaxLabelLength, 82U)); + GL.ObjectLabel(identifier, name, len, label); + } + } + bool LODBiasSupported = false; bool AnisotropicFilterSupported = false; bool BlendMinMaxSupported = false; bool TextureMultisampleSupported = false; + bool KHRDebugSupported = false; + u32 MaxLabelLength = 0; }; } diff --git a/irr/src/OpenGL/MaterialRenderer.cpp b/irr/src/OpenGL/MaterialRenderer.cpp index 7439dba61..269f28ae9 100644 --- a/irr/src/OpenGL/MaterialRenderer.cpp +++ b/irr/src/OpenGL/MaterialRenderer.cpp @@ -24,6 +24,7 @@ COpenGL3MaterialRenderer::COpenGL3MaterialRenderer(COpenGL3DriverBase *driver, s32 &outMaterialTypeNr, const c8 *vertexShaderProgram, const c8 *pixelShaderProgram, + const c8 *debugName, IShaderConstantSetCallBack *callback, E_MATERIAL_TYPE baseMaterial, s32 userData) : @@ -45,7 +46,7 @@ COpenGL3MaterialRenderer::COpenGL3MaterialRenderer(COpenGL3DriverBase *driver, if (CallBack) CallBack->grab(); - init(outMaterialTypeNr, vertexShaderProgram, pixelShaderProgram); + init(outMaterialTypeNr, vertexShaderProgram, pixelShaderProgram, debugName); } COpenGL3MaterialRenderer::COpenGL3MaterialRenderer(COpenGL3DriverBase *driver, @@ -98,6 +99,7 @@ GLuint COpenGL3MaterialRenderer::getProgram() const void COpenGL3MaterialRenderer::init(s32 &outMaterialTypeNr, const c8 *vertexShaderProgram, const c8 *pixelShaderProgram, + const c8 *debugName, bool addMaterial) { outMaterialTypeNr = -1; @@ -121,6 +123,9 @@ void COpenGL3MaterialRenderer::init(s32 &outMaterialTypeNr, if (!linkProgram()) return; + if (debugName) + Driver->irrGlObjectLabel(GL_PROGRAM, Program, debugName); + if (addMaterial) outMaterialTypeNr = Driver->addMaterialRenderer(this); } diff --git a/irr/src/OpenGL/MaterialRenderer.h b/irr/src/OpenGL/MaterialRenderer.h index fca71478a..0916f1ad5 100644 --- a/irr/src/OpenGL/MaterialRenderer.h +++ b/irr/src/OpenGL/MaterialRenderer.h @@ -28,6 +28,7 @@ public: s32 &outMaterialTypeNr, const c8 *vertexShaderProgram = 0, const c8 *pixelShaderProgram = 0, + const c8 *debugName = nullptr, IShaderConstantSetCallBack *callback = 0, E_MATERIAL_TYPE baseMaterial = EMT_SOLID, s32 userData = 0); @@ -66,7 +67,9 @@ protected: E_MATERIAL_TYPE baseMaterial = EMT_SOLID, s32 userData = 0); - void init(s32 &outMaterialTypeNr, const c8 *vertexShaderProgram, const c8 *pixelShaderProgram, bool addMaterial = true); + void init(s32 &outMaterialTypeNr, const c8 *vertexShaderProgram, + const c8 *pixelShaderProgram, const c8 *debugName = nullptr, + bool addMaterial = true); bool createShader(GLenum shaderType, const char *shader); bool linkProgram(); diff --git a/irr/src/OpenGL/Renderer2D.cpp b/irr/src/OpenGL/Renderer2D.cpp index b7b8edf1b..ec53cc9f9 100644 --- a/irr/src/OpenGL/Renderer2D.cpp +++ b/irr/src/OpenGL/Renderer2D.cpp @@ -23,8 +23,8 @@ COpenGL3Renderer2D::COpenGL3Renderer2D(const c8 *vertexShaderProgram, const c8 * WithTexture(withTexture) { int Temp = 0; - - init(Temp, vertexShaderProgram, pixelShaderProgram, false); + init(Temp, vertexShaderProgram, pixelShaderProgram, + withTexture ? "2DTexture" : "2DNoTexture", false); COpenGL3CacheHandler *cacheHandler = Driver->getCacheHandler(); diff --git a/irr/src/OpenGL3/Driver.cpp b/irr/src/OpenGL3/Driver.cpp index 767ce5992..7a62f4a12 100644 --- a/irr/src/OpenGL3/Driver.cpp +++ b/irr/src/OpenGL3/Driver.cpp @@ -72,6 +72,9 @@ void COpenGL3Driver::initFeatures() LODBiasSupported = true; BlendMinMaxSupported = true; TextureMultisampleSupported = true; + KHRDebugSupported = isVersionAtLeast(4, 6) || queryExtension("GL_KHR_debug"); + if (KHRDebugSupported) + MaxLabelLength = GetInteger(GL.MAX_LABEL_LENGTH); // COGLESCoreExtensionHandler::Feature static_assert(MATERIAL_MAX_TEXTURES <= 16, "Only up to 16 textures are guaranteed"); diff --git a/irr/src/OpenGLES2/Driver.cpp b/irr/src/OpenGLES2/Driver.cpp index 25c4f485d..6b234842a 100644 --- a/irr/src/OpenGLES2/Driver.cpp +++ b/irr/src/OpenGLES2/Driver.cpp @@ -124,6 +124,9 @@ void COpenGLES2Driver::initFeatures() AnisotropicFilterSupported = queryExtension("GL_EXT_texture_filter_anisotropic"); BlendMinMaxSupported = (Version.Major >= 3) || FeatureAvailable[IRR_GL_EXT_blend_minmax]; TextureMultisampleSupported = isVersionAtLeast(3, 1); + KHRDebugSupported = queryExtension("GL_KHR_debug"); + if (KHRDebugSupported) + MaxLabelLength = GetInteger(GL.MAX_LABEL_LENGTH); // COGLESCoreExtensionHandler::Feature static_assert(MATERIAL_MAX_TEXTURES <= 8, "Only up to 8 textures are guaranteed");