Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding debug annotations to OpenGL objects. #1930

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions rts/Lua/LuaConstGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,37 @@ bool LuaConstGL::PushEntries(lua_State* L)
PUSH_GL(STENCIL_ATTACHMENT_EXT);

return true;

/******************************************************************************
* OpenGL Object Types
* @section objecttypes
******************************************************************************/

/// @field GL_BUFFER 0x82E0
PUSH_GL(BUFFER);
/// @field GL_SHADER 0x82E1
PUSH_GL(SHADER);
/// @field GL_PROGRAM 0x82E2
PUSH_GL(PROGRAM);
/// @field GL_VERTEX_ARRAY 0x8074
PUSH_GL(VERTEX_ARRAY);
/// @field GL_QUERY 0x82E3
PUSH_GL(QUERY);
/// @field GL_PROGRAM_PIPELINE 0x82E4
PUSH_GL(PROGRAM_PIPELINE);
/// @field GL_TRANSFORM_FEEDBACK 0x8E22
PUSH_GL(TRANSFORM_FEEDBACK);
/// @field GL_RENDERBUFFER 0x8D41
PUSH_GL(RENDERBUFFER);
/// @field GL_FRAMEBUFFER 0x8D40
PUSH_GL(FRAMEBUFFER);

return true;
}




/******************************************************************************
* Not included, but useful texture Formats
* @section textureformats
Expand Down
79 changes: 79 additions & 0 deletions rts/Lua/LuaOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ bool LuaOpenGL::PushEntries(lua_State* L)
REGISTER_LUA_CFUNC(GetWaterRendering);
REGISTER_LUA_CFUNC(GetMapRendering);

if (GLAD_GL_KHR_debug) {
sprunk marked this conversation as resolved.
Show resolved Hide resolved
REGISTER_LUA_CFUNC(ObjectLabel);
REGISTER_LUA_CFUNC(PushDebugGroup);
REGISTER_LUA_CFUNC(PopDebugGroup);
}

if (canUseShaders)
LuaShaders::PushEntries(L);

Expand Down Expand Up @@ -5667,5 +5673,78 @@ int LuaOpenGL::GetMapRendering(lua_State* L)
return 0;
}

/**
* @function gl.ObjectLabel labels an object for use with debugging tools
* @param objectTypeIdentifier GLenum Specifies the type of object being labeled.
* @param objectID GLuint Specifies the name or ID of the object to label.
* @param label string A string containing the label to be assigned to the object.
* @treturn nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return here (or just don't document a nil return?)

*/
int LuaOpenGL::ObjectLabel(lua_State* L) {
const auto identifier = static_cast<GLenum>(luaL_checkinteger(L, 1));

switch (identifier) {
case GL_BUFFER: [[fallthrough]];
case GL_SHADER: [[fallthrough]];
case GL_PROGRAM: [[fallthrough]];
case GL_VERTEX_ARRAY: [[fallthrough]];
case GL_QUERY: [[fallthrough]];
case GL_PROGRAM_PIPELINE: [[fallthrough]];
case GL_TRANSFORM_FEEDBACK: [[fallthrough]];
case GL_TEXTURE: [[fallthrough]];
case GL_RENDERBUFFER: [[fallthrough]];
sprunk marked this conversation as resolved.
Show resolved Hide resolved
case GL_FRAMEBUFFER:
break;
default: { // something else
LOG_L(L_ERROR, "gl.%s: invalid identifier (%u)", __func__, identifier);
return 0;
}
}

const auto objectID = static_cast<GLuint>(luaL_checkinteger(L, 2));
const auto* label = luaL_checkstring(L, 3);
glObjectLabel(identifier, objectID, -1, label);

return 0;
}

// https://registry.khronos.org/OpenGL-Refpages/gl4/html/glPushDebugGroup.xhtml
/**
* @function gl.PushDebugGroup pushes a debug marker for nVidia nSight 2024.04, does not seem to work when FBO's are raw bound
* @param id GLuint A numeric identifier for the group.
* @param message string A human-readable string describing the debug group.
* @param source boolean true for GL_DEBUG_SOURCE_APPLICATION, false for GL_DEBUG_SOURCE_THIRD_PARTY. default false
sprunk marked this conversation as resolved.
Show resolved Hide resolved
* @treturn nil
*/
int LuaOpenGL::PushDebugGroup(lua_State* L) {
const auto id = static_cast<GLuint>(luaL_checkinteger(L, 1));
std::string message = luaL_checkstring(L, 2);
const bool source = luaL_optboolean(L, 3, false);

GLint maxLength = 0;
glGetIntegerv(GL_MAX_DEBUG_MESSAGE_LENGTH, &maxLength);
if (maxLength <= 0)
return 0;

if (message.length() >= maxLength) {
static constexpr std::string_view TRIM = "(...)";
message.resize(maxLength - TRIM.length() - 1);
message += TRIM;
assert(message.length() < maxLength);
}

glPushDebugGroup((source ? GL_DEBUG_SOURCE_APPLICATION : GL_DEBUG_SOURCE_THIRD_PARTY), id, -1, message.c_str());
return 0;
}

/**
* @function gl.PopDebugGroup
* @treturn nil
*/
int LuaOpenGL::PopDebugGroup(lua_State* L) {
glPopDebugGroup();
return 0;
}

/******************************************************************************/
/******************************************************************************/
4 changes: 4 additions & 0 deletions rts/Lua/LuaOpenGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ class LuaOpenGL {
static int GetSun(lua_State* L);
static int GetWaterRendering(lua_State* L);
static int GetMapRendering(lua_State* L);

static int ObjectLabel(lua_State* L);
static int PushDebugGroup(lua_State* L);
static int PopDebugGroup(lua_State* L);
};

inline void LuaOpenGL::InitMatrixState(lua_State* L, const char* fn) {
Expand Down
4 changes: 3 additions & 1 deletion rts/Lua/LuaShaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@ int LuaShaders::CreateShader(lua_State* L)

// note: index, not raw ID
lua_pushnumber(L, shaders.AddProgram(p));
return 1;
// also push the program ID
lua_pushnumber(L, prog);
return 2;
}


Expand Down
3 changes: 2 additions & 1 deletion rts/Lua/LuaVBO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ bool LuaVBOs::PushEntries(lua_State* L)
"UnbindBufferRange", &LuaVBOImpl::UnbindBufferRange,

"DumpDefinition", &LuaVBOImpl::DumpDefinition,
"GetBufferSize", &LuaVBOImpl::GetBufferSize
"GetBufferSize", &LuaVBOImpl::GetBufferSize,
"GetID", & LuaVBOImpl::GetID
);

gl.set("VBO", sol::lua_nil); // don't want this to be accessible directly without gl.GetVBO
Expand Down
11 changes: 11 additions & 0 deletions rts/Lua/LuaVBOImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,17 @@ void LuaVBOImpl::DumpDefinition()
LOG("%s", ss.str().c_str());
}

/*** Gets the OpenGL Buffer ID
*
* @function VBO:GetID
* @treturn number buffer ID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be:

@return number bufferID

Because @return <type> <name> [Optional description.].

It's a bit odd that returns get names, but that's the standard. I think it helps in multi-return cases.

*/
uint32_t LuaVBOImpl::GetID() const
{
VBOExistenceCheck(vbo, __func__);
return vbo->GetId();
}

void LuaVBOImpl::AllocGLBuffer(size_t byteSize)
{
if (defTarget == GL_UNIFORM_BUFFER && bufferSizeInBytes > UBO_SAFE_SIZE_BYTES) {
Expand Down
1 change: 1 addition & 0 deletions rts/Lua/LuaVBOImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class LuaVBOImpl {
int UnbindBufferRange(const GLuint index, const sol::optional<int> elemOffsetOpt, const sol::optional<int> elemCountOpt, const sol::optional<GLenum> targetOpt);

void DumpDefinition();
uint32_t GetID() const;
public:
static bool Supported(GLenum target);
private:
Expand Down
2 changes: 2 additions & 0 deletions rts/lib/headlessStubs/gladstub.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ extern "C" {
#define GLAD_GL_ARB_base_instance GL_FALSE
#define GLAD_GL_ARB_sample_shading GL_FALSE

#define GLAD_GL_KHR_debug GL_FALSE

#define GLXEW_SGI_video_sync GL_FALSE

int gladLoadGL(void);
Expand Down
4 changes: 4 additions & 0 deletions rts/lib/headlessStubs/glstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ GLAPI void APIENTRY glDepthRange(GLdouble nearVal, GLdouble farVal) {}
GLAPI void APIENTRY glBeginConditionalRender(GLuint id, GLenum mode) {}
GLAPI void APIENTRY glEndConditionalRender(void) {}

GLAPI void APIENTRY glObjectLabel(GLenum identifier, GLuint name, GLsizei length, const GLchar* label) {}
GLAPI void APIENTRY glPushDebugGroup(GLenum source, GLuint id, GLsizei length, const GLchar* message) {}
GLAPI void APIENTRY glPopDebugGroup() {}

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down