Skip to content

Commit dfe9de2

Browse files
lhogsprunk
andauthored
Allow adding debug annotations to OpenGL objects. (#1930)
* Allow adding debug annotations to OpenGL objects, as well as defining GPU zones to achieve functionality very similar to Tracy, but on the GPU. V2 --------- Co-authored-by: sprunk <[email protected]>
1 parent d91fb0b commit dfe9de2

File tree

7 files changed

+128
-2
lines changed

7 files changed

+128
-2
lines changed

rts/Lua/LuaConstGL.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,37 @@ bool LuaConstGL::PushEntries(lua_State* L)
704704
PUSH_GL(STENCIL_ATTACHMENT_EXT);
705705

706706
return true;
707+
708+
/******************************************************************************
709+
* OpenGL Object Types
710+
* @section objecttypes
711+
******************************************************************************/
712+
713+
/// @field GL_BUFFER 0x82E0
714+
PUSH_GL(BUFFER);
715+
/// @field GL_SHADER 0x82E1
716+
PUSH_GL(SHADER);
717+
/// @field GL_PROGRAM 0x82E2
718+
PUSH_GL(PROGRAM);
719+
/// @field GL_VERTEX_ARRAY 0x8074
720+
PUSH_GL(VERTEX_ARRAY);
721+
/// @field GL_QUERY 0x82E3
722+
PUSH_GL(QUERY);
723+
/// @field GL_PROGRAM_PIPELINE 0x82E4
724+
PUSH_GL(PROGRAM_PIPELINE);
725+
/// @field GL_TRANSFORM_FEEDBACK 0x8E22
726+
PUSH_GL(TRANSFORM_FEEDBACK);
727+
/// @field GL_RENDERBUFFER 0x8D41
728+
PUSH_GL(RENDERBUFFER);
729+
/// @field GL_FRAMEBUFFER 0x8D40
730+
PUSH_GL(FRAMEBUFFER);
731+
732+
return true;
707733
}
708734

709735

736+
737+
710738
/******************************************************************************
711739
* Not included, but useful texture Formats
712740
* @section textureformats

rts/Lua/LuaOpenGL.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ bool LuaOpenGL::PushEntries(lua_State* L)
470470
REGISTER_LUA_CFUNC(GetWaterRendering);
471471
REGISTER_LUA_CFUNC(GetMapRendering);
472472

473+
if (GLAD_GL_KHR_debug) {
474+
REGISTER_LUA_CFUNC(ObjectLabel);
475+
REGISTER_LUA_CFUNC(PushDebugGroup);
476+
REGISTER_LUA_CFUNC(PopDebugGroup);
477+
}
478+
473479
if (canUseShaders)
474480
LuaShaders::PushEntries(L);
475481

@@ -5667,5 +5673,78 @@ int LuaOpenGL::GetMapRendering(lua_State* L)
56675673
return 0;
56685674
}
56695675

5676+
/**
5677+
* @function gl.ObjectLabel labels an object for use with debugging tools
5678+
* @param objectTypeIdentifier GLenum Specifies the type of object being labeled.
5679+
* @param objectID GLuint Specifies the name or ID of the object to label.
5680+
* @param label string A string containing the label to be assigned to the object.
5681+
* @return nil
5682+
*/
5683+
int LuaOpenGL::ObjectLabel(lua_State* L) {
5684+
const auto identifier = static_cast<GLenum>(luaL_checkinteger(L, 1));
5685+
5686+
switch (identifier) {
5687+
case GL_BUFFER: [[fallthrough]];
5688+
case GL_SHADER: [[fallthrough]];
5689+
case GL_PROGRAM: [[fallthrough]];
5690+
case GL_VERTEX_ARRAY: [[fallthrough]];
5691+
case GL_QUERY: [[fallthrough]];
5692+
case GL_PROGRAM_PIPELINE: [[fallthrough]];
5693+
case GL_TRANSFORM_FEEDBACK: [[fallthrough]];
5694+
case GL_TEXTURE: [[fallthrough]];
5695+
case GL_RENDERBUFFER: [[fallthrough]];
5696+
case GL_FRAMEBUFFER:
5697+
break;
5698+
default: { // something else
5699+
LOG_L(L_ERROR, "gl.%s: invalid identifier (%u)", __func__, identifier);
5700+
return 0;
5701+
}
5702+
}
5703+
5704+
const auto objectID = static_cast<GLuint>(luaL_checkinteger(L, 2));
5705+
const auto* label = luaL_checkstring(L, 3);
5706+
glObjectLabel(identifier, objectID, -1, label);
5707+
5708+
return 0;
5709+
}
5710+
5711+
// https://registry.khronos.org/OpenGL-Refpages/gl4/html/glPushDebugGroup.xhtml
5712+
/**
5713+
* @function gl.PushDebugGroup pushes a debug marker for nVidia nSight 2024.04, does not seem to work when FBO's are raw bound
5714+
* @param id GLuint A numeric identifier for the group.
5715+
* @param message string A human-readable string describing the debug group.
5716+
* @param sourceIsThirdParty boolean Set the source tag, true for GL_DEBUG_SOURCE_THIRD_PARTY, false for GL_DEBUG_SOURCE_APPLICATION. default false
5717+
* @return nil
5718+
*/
5719+
int LuaOpenGL::PushDebugGroup(lua_State* L) {
5720+
const auto id = static_cast<GLuint>(luaL_checkinteger(L, 1));
5721+
std::string message = luaL_checkstring(L, 2);
5722+
const bool sourceIsThirdParty = luaL_optboolean(L, 3, false);
5723+
5724+
GLint maxLength = 0;
5725+
glGetIntegerv(GL_MAX_DEBUG_MESSAGE_LENGTH, &maxLength);
5726+
if (maxLength <= 0)
5727+
return 0;
5728+
5729+
if (message.length() >= maxLength) {
5730+
static constexpr std::string_view TRIM = "(...)";
5731+
message.resize(maxLength - TRIM.length() - 1);
5732+
message += TRIM;
5733+
assert(message.length() < maxLength);
5734+
}
5735+
5736+
glPushDebugGroup((sourceIsThirdParty ? GL_DEBUG_SOURCE_THIRD_PARTY : GL_DEBUG_SOURCE_APPLICATION), id, -1, message.c_str());
5737+
return 0;
5738+
}
5739+
5740+
/**
5741+
* @function gl.PopDebugGroup
5742+
* @return nil
5743+
*/
5744+
int LuaOpenGL::PopDebugGroup(lua_State* L) {
5745+
glPopDebugGroup();
5746+
return 0;
5747+
}
5748+
56705749
/******************************************************************************/
56715750
/******************************************************************************/

rts/Lua/LuaOpenGL.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ class LuaOpenGL {
353353
static int GetSun(lua_State* L);
354354
static int GetWaterRendering(lua_State* L);
355355
static int GetMapRendering(lua_State* L);
356+
357+
static int ObjectLabel(lua_State* L);
358+
static int PushDebugGroup(lua_State* L);
359+
static int PopDebugGroup(lua_State* L);
356360
};
357361

358362
inline void LuaOpenGL::InitMatrixState(lua_State* L, const char* fn) {

rts/Lua/LuaShaders.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,9 @@ int LuaShaders::CreateShader(lua_State* L)
780780

781781
// note: index, not raw ID
782782
lua_pushnumber(L, shaders.AddProgram(p));
783-
return 1;
783+
// also push the program ID
784+
lua_pushnumber(L, prog);
785+
return 2;
784786
}
785787

786788

rts/Lua/LuaVBO.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ bool LuaVBOs::PushEntries(lua_State* L)
6363
"UnbindBufferRange", &LuaVBOImpl::UnbindBufferRange,
6464

6565
"DumpDefinition", &LuaVBOImpl::DumpDefinition,
66-
"GetBufferSize", &LuaVBOImpl::GetBufferSize
66+
"GetBufferSize", &LuaVBOImpl::GetBufferSize,
67+
"GetID", & LuaVBOImpl::GetID
6768
);
6869

6970
gl.set("VBO", sol::lua_nil); // don't want this to be accessible directly without gl.GetVBO

rts/Lua/LuaVBOImpl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,17 @@ void LuaVBOImpl::DumpDefinition()
14861486
LOG("%s", ss.str().c_str());
14871487
}
14881488

1489+
/*** Gets the OpenGL Buffer ID
1490+
*
1491+
* @function VBO:GetID
1492+
* @return number bufferID
1493+
*/
1494+
uint32_t LuaVBOImpl::GetID() const
1495+
{
1496+
VBOExistenceCheck(vbo, __func__);
1497+
return vbo->GetId();
1498+
}
1499+
14891500
void LuaVBOImpl::AllocGLBuffer(size_t byteSize)
14901501
{
14911502
if (defTarget == GL_UNIFORM_BUFFER && bufferSizeInBytes > UBO_SAFE_SIZE_BYTES) {

rts/Lua/LuaVBOImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class LuaVBOImpl {
5252
int UnbindBufferRange(const GLuint index, const sol::optional<int> elemOffsetOpt, const sol::optional<int> elemCountOpt, const sol::optional<GLenum> targetOpt);
5353

5454
void DumpDefinition();
55+
uint32_t GetID() const;
5556
public:
5657
static bool Supported(GLenum target);
5758
private:

0 commit comments

Comments
 (0)