Skip to content

Commit

Permalink
Clean up docs, allow getting shader object ID and buffer object ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Beherith committed Dec 21, 2024
1 parent 1540827 commit 8bd8e30
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 126 deletions.
1 change: 1 addition & 0 deletions rts/Lua/LuaConstPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ bool LuaConstPlatform::PushEntries(lua_State* L)
LuaPushNamedString(L, "hwConfig", Platform::GetHardwareStr());
LuaPushNamedNumber(L, "cpuLogicalCores", Threading::GetLogicalCpuCores());
LuaPushNamedNumber(L, "cpuPhysicalCores", Threading::GetPhysicalCpuCores());
LuaPushNamedString(L, "cpuBrand", Threading::GetCPUBrand());

LuaPushNamedString(L, "sysInfoHash", Platform::GetSysInfoHash());
LuaPushNamedString(L, "macAddrHash", Platform::GetMacAddrHash());
Expand Down
147 changes: 23 additions & 124 deletions rts/Lua/LuaOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ bool LuaOpenGL::PushEntries(lua_State* L)
REGISTER_LUA_CFUNC(GetWaterRendering);
REGISTER_LUA_CFUNC(GetMapRendering);


REGISTER_LUA_CFUNC(ObjectLabel);
REGISTER_LUA_CFUNC(PushDebugGroup);
REGISTER_LUA_CFUNC(PopDebugGroup);

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

Expand Down Expand Up @@ -5662,152 +5667,46 @@ int LuaOpenGL::GetMapRendering(lua_State* L)
}



/*
void glObjectLabel(GLenum identifier, GLuint name, GLsizei length, const GLchar *label);
Parameters
identifier:
Specifies the type of object being labeled. Possible values include:
GL_BUFFER: Buffer object.
GL_SHADER: Shader object.
GL_PROGRAM: Program object.
GL_VERTEX_ARRAY: Vertex array object (VAO).
GL_QUERY: Query object.
GL_PROGRAM_PIPELINE: Program pipeline object.
GL_TRANSFORM_FEEDBACK: Transform feedback object.
GL_TEXTURE: Texture object.
GL_RENDERBUFFER: Renderbuffer object.
GL_FRAMEBUFFER: Framebuffer object.
name:
Specifies the name or ID of the object to label (e.g., the result of glGenBuffers, glCreateShader, etc.).
length:
Specifies the length of the label string. If -1, the function treats the string as null-terminated.
label:
A null-terminated string containing the label to be assigned to the object.
Notes
Length Handling:
If you provide -1 as the length, OpenGL assumes the label is null-terminated.
Otherwise, you must specify the exact length of the label string.
Debugging Tools:
The label appears in OpenGL debug outputs and tools like RenderDoc, Nsight, or apitrace.
This makes it easier to trace operations and diagnose issues related to specific OpenGL objects.
Portability:
Supported in OpenGL 4.3 and later.
Also available with the KHR_debug extension in earlier versions of OpenGL (if supported).
*/
// Ensure that identifier is a valid GLenum
// The length should be passed as -1 because the strings are null terminated
// Check that the GLuint name refers to a valid openGL object
// Check that the label is a valid string
// Write a a docstring as well as a comment
// Write a lua interface for this function:
// void glObjectLabel(GLenum identifier, GLuint name, GLsizei length, const GLchar *label);

/**
* @function gl.ObjectLabel
* @param identifier GLenum Specifies the type of object being labeled.
* @param name GLuint Specifies the name or ID of the object to label.
* @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 null-terminated string containing the label to be assigned to the object.
* @treturn nil
*/
int LuaOpenGL::ObjectLabel(lua_State* L) {
GLenum identifier = (GLenum)luaL_checkinteger(L, 1);
GLuint name = (GLuint)luaL_checkinteger(L, 2);
GLenum identifier = (GLenum)luaL_checkinteger(L, 1);
if (identifier != GL_BUFFER && identifier != GL_SHADER && identifier != GL_PROGRAM &&
identifier != GL_VERTEX_ARRAY && identifier != GL_QUERY && identifier != GL_PROGRAM_PIPELINE &&
identifier != GL_TRANSFORM_FEEDBACK && identifier != GL_TEXTURE && identifier != GL_RENDERBUFFER &&
identifier != GL_FRAMEBUFFER) {
return 0;
}
GLuint objectID = (GLuint)luaL_checkinteger(L, 2);
const char* label = luaL_checkstring(L, 3);

// Ensure that identifier is a valid GLenum
// The length should be passed as -1 because the strings are null terminated
// Check that the GLuint name refers to a valid openGL object
// Check that the label is a valid string
glObjectLabel(identifier, name, -1, label);
glObjectLabel(identifier, objectID, -1, label);

return 0;
}

/*
`glPushDebugGroup` is a function in the OpenGL API that helps developers debug and profile OpenGL applications. It is part of the `KHR_debug` extension, which is integrated into the core OpenGL since version 4.3.
### Purpose
`glPushDebugGroup` allows you to group a set of OpenGL commands into a named debug group. This helps in identifying and organizing sections of code in the debug output, making it easier to diagnose and analyze rendering issues.
### Function Prototype
```c
void glPushDebugGroup(GLenum source, GLuint id, GLsizei length, const GLchar *message);
```
### Parameters
1. **`source`**: Specifies the source of the debug group. Common values include:
- `GL_DEBUG_SOURCE_APPLICATION`: Indicates that the debug group is defined by the application.
- `GL_DEBUG_SOURCE_THIRD_PARTY`: Indicates that the group is defined by external tools or libraries.
2. **`id`**: A numeric identifier for the group. This can be any value that uniquely identifies the group within the scope of the source.
3. **`length`**: The length of the `message` string. If this is `-1`, the string is assumed to be null-terminated.

4. **`message`**: A human-readable string describing the debug group. This message is used in debugging tools or logs.
### Example Usage
```c
glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 1, -1, "Render Scene");
// OpenGL commands for rendering the scene
glDrawArrays(GL_TRIANGLES, 0, 36);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glPopDebugGroup(); // End of the debug group
```
### Notes
- Each `glPushDebugGroup` must be matched with a corresponding `glPopDebugGroup`.
- The function adds a new debug group to the debug output stack. Groups can be nested for better organization.
- Debug groups are particularly useful when analyzing rendering performance or diagnosing errors in tools like OpenGL Debug Output or profilers.
### Benefits
1. **Organized Debugging**: Clearly marks and groups commands for easier traceability.
2. **Better Profiling**: Tools can provide insights into specific debug groups.
3. **Improved Diagnostics**: Helps isolate and identify problematic sections of code.
### Availability
- OpenGL 4.3 or later.
- Requires the `KHR_debug` extension in earlier versions of OpenGL (if supported).
By using `glPushDebugGroup`, you can make debugging and profiling OpenGL applications much more structured and efficient.
Write the lua interfaces for glPushDebugGroup and glPopDebugGroup
*/

// Ensure that identifier is a valid GLenum
// The length should be passed as -1 because the strings are null terminated
// Check that the GLuint name refers to a valid openGL object
// Check that the label is a valid string
// Write a a docstring as well as a comment
// Write a lua interface for this function:
// https://registry.khronos.org/OpenGL-Refpages/gl4/html/glPushDebugGroup.xhtml
/**
* @function gl.PushDebugGroup
* @param source GLenum Specifies the source of the debug group.
* @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
* @treturn nil
*/
int LuaOpenGL::PushDebugGroup(lua_State* L) {
GLenum source = (GLenum)luaL_checkinteger(L, 1);
GLuint id = (GLuint)luaL_checkinteger(L, 2);
const char* message = luaL_checkstring(L, 3);

if (source != GL_DEBUG_SOURCE_APPLICATION && source != GL_DEBUG_SOURCE_THIRD_PARTY) {
luaL_error(L, "Invalid source value for gl.PushDebugGroup");
return 0;
}
GLuint id = (GLuint)luaL_checkinteger(L, 1);
const char* message = luaL_checkstring(L, 2);
bool source = luaL_optboolean(L, 3, false);

GLint maxLength;
glGetIntegerv(GL_MAX_DEBUG_MESSAGE_LENGTH, &maxLength);
Expand All @@ -5817,7 +5716,7 @@ int LuaOpenGL::PushDebugGroup(lua_State* L) {
return 0;
}

glPushDebugGroup(source, id, -1, message);
glPushDebugGroup((source ? GL_DEBUG_SOURCE_APPLICATION: GL_DEBUG_SOURCE_THIRD_PARTY) , id, -1, message);

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion rts/Lua/LuaShaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ GLint LuaShaders::GetUniformLocation(LuaShaders::Program* p, const char* name)
* @function gl.CreateShader ( table shaderParams )
* @tparam table shaderParams
* @treturn number shaderID
* @treturn number glProgID
*
* ({[ vertex = "glsl code" ,]
* [ tcs = "glsl code" ,]
Expand Down Expand Up @@ -738,7 +739,8 @@ int LuaShaders::CreateShader(lua_State* L)

// note: index, not raw ID
lua_pushnumber(L, shaders.AddProgram(p));
return 1;
lua_pushnumber(L, (int) 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 @@ -64,7 +64,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 @@ -1445,6 +1445,17 @@ void LuaVBOImpl::DumpDefinition()
LOG("%s", ss.str().c_str());
}

/*** Gets the OpenGL Buffer ID
*
* @function VBO:GetID
* @treturn number buffer ID
*/
uint32_t LuaVBOImpl::GetID()
{
VBOExistenceCheck(vbo, __func__);
return vbo->GetId();
}

void LuaVBOImpl::AllocGLBuffer(size_t byteSize)
{
if (defTarget == GL_UNIFORM_BUFFER && bufferSizeInBytes > UBO_SAFE_SIZE_BYTES) {
Expand Down
2 changes: 2 additions & 0 deletions rts/Lua/LuaVBOImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ 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();

public:
static bool Supported(GLenum target);
private:
Expand Down
1 change: 1 addition & 0 deletions rts/System/Platform/CpuID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ namespace springproc {

for (int group = 0; group < system.num_cpu_types; ++group) {
cpu_id_t cpu_id = system.cpu_types[group];
cpuid_brand = std::string(cpu_id.brand_str);
switch(cpu_id.purpose) {
case PURPOSE_GENERAL:
case PURPOSE_PERFORMANCE:
Expand Down
4 changes: 4 additions & 0 deletions rts/System/Platform/CpuID.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#endif

#include <cstdint>
#include <string>

namespace springproc {
_noinline void ExecCPUID(unsigned int* a, unsigned int* b, unsigned int* c, unsigned int* d);
Expand Down Expand Up @@ -44,6 +45,7 @@ namespace springproc {
cores. */
int GetNumPhysicalCores() const { return numPhysicalCores; }
int GetNumLogicalCores() const { return numLogicalCores; }
std::string GetCPUBrand() const { return cpuid_brand; }

/** Total number of physical processor dies in the system. */
int GetTotalNumPackages() const { return totalNumPackages; }
Expand Down Expand Up @@ -71,6 +73,8 @@ namespace springproc {
uint64_t affinityMaskOfPackages[MAX_PROCESSORS];
uint64_t availableProceesorAffinityMask;

std::string cpuid_brand;

////////////////////////
// Intel specific fields

Expand Down
4 changes: 4 additions & 0 deletions rts/System/Platform/Threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ namespace Threading {
return springproc::CPUID::GetInstance().GetNumPhysicalCores();
}

std::string GetCPUBrand() {
return springproc::CPUID::GetInstance().GetCPUBrand();
}

bool HasHyperThreading() { return (GetLogicalCpuCores() > GetPhysicalCpuCores()); }


Expand Down
2 changes: 2 additions & 0 deletions rts/System/Platform/Threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ namespace Threading {
int GetLogicalCpuCores(); /// physical + hyperthreading
bool HasHyperThreading();

std::string GetCPUBrand();

/**
* Inform the OS kernel that we are a cpu-intensive task
*/
Expand Down

0 comments on commit 8bd8e30

Please sign in to comment.