Add glDebugGroup support

pull/593/head
AnyOldName3 4 years ago
parent cc2ce9fa3e
commit 0e4e8eb0f3

@ -38,8 +38,11 @@ either expressed or implied, of the FreeBSD Project.
// OpenGL constants not provided by OSG: // OpenGL constants not provided by OSG:
#include <SDL_opengl_glext.h> #include <SDL_opengl_glext.h>
void debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam) namespace Debug
{ {
void debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
{
#ifdef GL_DEBUG_OUTPUT #ifdef GL_DEBUG_OUTPUT
std::string srcStr; std::string srcStr;
switch (source) switch (source)
@ -69,12 +72,12 @@ void debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsiz
std::string typeStr; std::string typeStr;
Debug::Level logSeverity = Debug::Warning; Level logSeverity = Warning;
switch (type) switch (type)
{ {
case GL_DEBUG_TYPE_ERROR: case GL_DEBUG_TYPE_ERROR:
typeStr = "ERROR"; typeStr = "ERROR";
logSeverity = Debug::Error; logSeverity = Error;
break; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
typeStr = "DEPRECATED_BEHAVIOR"; typeStr = "DEPRECATED_BEHAVIOR";
@ -98,11 +101,36 @@ void debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsiz
Log(logSeverity) << "OpenGL " << typeStr << " [" << srcStr << "]: " << message; Log(logSeverity) << "OpenGL " << typeStr << " [" << srcStr << "]: " << message;
#endif #endif
} }
void enableGLDebugExtension(unsigned int contextID) class PushDebugGroup
{ {
public:
static std::unique_ptr<PushDebugGroup> sInstance;
void (GL_APIENTRY * glPushDebugGroup) (GLenum source, GLuint id, GLsizei length, const GLchar * message);
void (GL_APIENTRY * glPopDebugGroup) (void);
bool valid()
{
return glPushDebugGroup && glPopDebugGroup;
}
};
std::unique_ptr<PushDebugGroup> PushDebugGroup::sInstance{ std::make_unique<PushDebugGroup>() };
EnableGLDebugOperation::EnableGLDebugOperation() : osg::GraphicsOperation("EnableGLDebugOperation", false)
{
}
void EnableGLDebugOperation::operator()(osg::GraphicsContext* graphicsContext)
{
#ifdef GL_DEBUG_OUTPUT #ifdef GL_DEBUG_OUTPUT
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(mMutex);
unsigned int contextID = graphicsContext->getState()->getContextID();
typedef void (GL_APIENTRY *DEBUGPROC)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam); typedef void (GL_APIENTRY *DEBUGPROC)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam);
typedef void (GL_APIENTRY *GLDebugMessageControlFunction)(GLenum source, GLenum type, GLenum severity, GLsizei count, const GLuint *ids, GLboolean enabled); typedef void (GL_APIENTRY *GLDebugMessageControlFunction)(GLenum source, GLenum type, GLenum severity, GLsizei count, const GLuint *ids, GLboolean enabled);
typedef void (GL_APIENTRY *GLDebugMessageCallbackFunction)(DEBUGPROC, const void* userParam); typedef void (GL_APIENTRY *GLDebugMessageCallbackFunction)(DEBUGPROC, const void* userParam);
@ -114,6 +142,8 @@ void enableGLDebugExtension(unsigned int contextID)
{ {
osg::setGLExtensionFuncPtr(glDebugMessageCallback, "glDebugMessageCallback"); osg::setGLExtensionFuncPtr(glDebugMessageCallback, "glDebugMessageCallback");
osg::setGLExtensionFuncPtr(glDebugMessageControl, "glDebugMessageControl"); osg::setGLExtensionFuncPtr(glDebugMessageControl, "glDebugMessageControl");
osg::setGLExtensionFuncPtr(PushDebugGroup::sInstance->glPushDebugGroup, "glPushDebugGroup");
osg::setGLExtensionFuncPtr(PushDebugGroup::sInstance->glPopDebugGroup, "glPopDebugGroup");
} }
else if (osg::isGLExtensionSupported(contextID, "GL_ARB_debug_output")) else if (osg::isGLExtensionSupported(contextID, "GL_ARB_debug_output"))
{ {
@ -132,27 +162,15 @@ void enableGLDebugExtension(unsigned int contextID)
glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR, GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr, true); glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR, GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr, true);
glDebugMessageCallback(debugCallback, nullptr); glDebugMessageCallback(debugCallback, nullptr);
Log(Debug::Info) << "OpenGL debug callback attached."; Log(Info) << "OpenGL debug callback attached.";
} }
else else
#endif #endif
Log(Debug::Error) << "Unable to attach OpenGL debug callback."; Log(Error) << "Unable to attach OpenGL debug callback.";
} }
Debug::EnableGLDebugOperation::EnableGLDebugOperation() : osg::GraphicsOperation("EnableGLDebugOperation", false)
{
}
void Debug::EnableGLDebugOperation::operator()(osg::GraphicsContext* graphicsContext)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(mMutex);
unsigned int contextID = graphicsContext->getState()->getContextID();
enableGLDebugExtension(contextID);
}
bool Debug::shouldDebugOpenGL() bool shouldDebugOpenGL()
{ {
const char* env = std::getenv("OPENMW_DEBUG_OPENGL"); const char* env = std::getenv("OPENMW_DEBUG_OPENGL");
if (!env) if (!env)
return false; return false;
@ -161,4 +179,94 @@ bool Debug::shouldDebugOpenGL()
return true; return true;
return str.find("OFF") == std::string::npos && str.find("0") == std::string::npos && str.find("NO") == std::string::npos; return str.find("OFF") == std::string::npos && str.find("0") == std::string::npos && str.find("NO") == std::string::npos;
}
DebugGroup::DebugGroup(const std::string & message, GLuint id)
: mSource(GL_DEBUG_SOURCE_APPLICATION)
, mId(id)
, mMessage(message)
{
}
DebugGroup::DebugGroup(const DebugGroup & debugGroup, const osg::CopyOp & copyop)
: osg::StateAttribute(debugGroup, copyop)
, mSource(debugGroup.mSource)
, mId(debugGroup.mId)
, mMessage(debugGroup.mMessage)
{
}
void DebugGroup::apply(osg::State & state) const
{
if (!PushDebugGroup::sInstance->valid())
{
Log(Error) << "OpenGL debug groups not supported on this system, or OPENMW_DEBUG_OPENGL environment variable not set.";
return;
}
auto& attributeVec = state.getAttributeVec(this);
auto& lastAppliedStack = sLastAppliedStack[state.getContextID()];
size_t firstNonMatch = 0;
while (firstNonMatch < lastAppliedStack.size()
&& ((firstNonMatch < attributeVec.size() && lastAppliedStack[firstNonMatch] == attributeVec[firstNonMatch].first)
|| lastAppliedStack[firstNonMatch] == this))
firstNonMatch++;
for (size_t i = lastAppliedStack.size(); i > firstNonMatch; --i)
lastAppliedStack[i - 1]->pop(state);
lastAppliedStack.resize(firstNonMatch);
lastAppliedStack.reserve(attributeVec.size());
for (size_t i = firstNonMatch; i < attributeVec.size(); ++i)
{
const DebugGroup* group = static_cast<const DebugGroup*>(attributeVec[i].first);
group->push(state);
lastAppliedStack.push_back(group);
}
if (!(lastAppliedStack.back() == this))
{
push(state);
lastAppliedStack.push_back(this);
}
}
int DebugGroup::compare(const StateAttribute & sa) const
{
COMPARE_StateAttribute_Types(DebugGroup, sa);
COMPARE_StateAttribute_Parameter(mSource);
COMPARE_StateAttribute_Parameter(mId);
COMPARE_StateAttribute_Parameter(mMessage);
return 0;
}
void DebugGroup::releaseGLObjects(osg::State * state) const
{
if (state)
sLastAppliedStack.erase(state->getContextID());
else
sLastAppliedStack.clear();
}
bool DebugGroup::isValid() const
{
return mSource || mId || mMessage.length();
}
void DebugGroup::push(osg::State & state) const
{
if (isValid())
PushDebugGroup::sInstance->glPushDebugGroup(mSource, mId, mMessage.size(), mMessage.c_str());
}
void DebugGroup::pop(osg::State & state) const
{
if (isValid())
PushDebugGroup::sInstance->glPopDebugGroup();
}
std::map<unsigned int, std::vector<const DebugGroup *>> DebugGroup::sLastAppliedStack{};
} }

@ -17,5 +17,65 @@ namespace Debug
}; };
bool shouldDebugOpenGL(); bool shouldDebugOpenGL();
/*
Debug groups allow rendering to be annotated, making debugging via APITrace/CodeXL/NSight etc. much clearer.
Because I've not thought of a quick and clean way of doing it without incurring a small performance cost,
there are no uses of this class checked in. For now, add annotations locally when you need them.
To use this class, add it to a StateSet just like any other StateAttribute. Prefer the string-only constructor.
You'll need OPENMW_DEBUG_OPENGL set to true, or shouldDebugOpenGL() redefined to just return true as otherwise
the extension function pointers won't get set up. That can maybe be cleaned up in the future.
Beware that consecutive identical debug groups (i.e. pointers match) won't always get applied due to OSG thinking
it's already applied them. Either avoid nesting the same object, add dummy groups so they're not consecutive, or
ensure the leaf group isn't identical to its parent.
*/
class DebugGroup : public osg::StateAttribute
{
public:
DebugGroup()
: mSource(0)
, mId(0)
, mMessage("")
{}
DebugGroup(GLenum source, GLuint id, const std::string& message)
: mSource(source)
, mId(id)
, mMessage(message)
{}
DebugGroup(const std::string& message, GLuint id = 0);
DebugGroup(const DebugGroup& debugGroup, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
META_StateAttribute(Debug, DebugGroup, osg::StateAttribute::Type(101));
void apply(osg::State& state) const override;
int compare(const StateAttribute& sa) const override;
void releaseGLObjects(osg::State* state = nullptr) const override;
virtual bool isValid() const;
protected:
virtual ~DebugGroup() = default;
virtual void push(osg::State& state) const;
virtual void pop(osg::State& state) const;
GLenum mSource;
GLuint mId;
std::string mMessage;
static std::map<unsigned int, std::vector<const DebugGroup *>> sLastAppliedStack;
friend EnableGLDebugOperation;
};
} }
#endif #endif

Loading…
Cancel
Save