Skip to content

Commit

Permalink
Done with glDebugOutput/logging for now
Browse files Browse the repository at this point in the history
  • Loading branch information
rswinkle committed Jun 4, 2024
1 parent 97a90cb commit 3a1ae46
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define HEIGHT 480

#ifndef FPS_EVERY_N_SECS
#define FPS_EVERY_N_SECS 1
#define FPS_EVERY_N_SECS 2
#endif

#define FPS_DELAY (FPS_EVERY_N_SECS*1000)
Expand Down Expand Up @@ -78,7 +78,7 @@ int main(int argc, char** argv)
counter++;
ms = new_time - old_time;
if (ms >= FPS_DELAY) {
printf("%d %f FPS\n", ms, counter*1000.0f/ms);
printf("%d %.0f FPS\n", ms, counter*1000.0f/ms);
old_time = new_time;
counter = 0;
}
Expand Down
99 changes: 97 additions & 2 deletions portablegl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,33 @@ enum
GL_MAX_3D_TEXTURE_SIZE,
GL_MAX_ARRAY_TEXTURE_LAYERS,

// glDebugOutput
GL_DEBUG_OUTPUT,

GL_DEBUG_SOURCE_API,
GL_DEBUG_SOURCE_SHADER_COMPILER,
GL_DEBUG_SOURCE_WINDOW_SYSTEM,
GL_DEBUG_SOURCE_THIRD_PARTY,
GL_DEBUG_SOURCE_APPLICATION,
GL_DEBUG_SOURCE_OTHER,

GL_DEBUG_TYPE_ERROR,
GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
GL_DEBUG_TYPE_PERFORMANCE,
GL_DEBUG_TYPE_PORTABILITY,
GL_DEBUG_TYPE_MARKER,
GL_DEBUG_TYPE_PUSH_GROUP,
GL_DEBUG_TYPE_POP_GROUP,
GL_DEBUG_TYPE_OTHER,

GL_DEBUG_SEVERITY_HIGH,
GL_DEBUG_SEVERITY_MEDIUM,
GL_DEBUG_SEVERITY_LOW,
GL_DEBUG_SEVERITY_NOTIFICATION,

GL_MAX_DEBUG_MESSAGE_LENGTH,

//shader types etc. not used, just here for compatibility add what you
//need so you can use your OpenGL code with PortableGL with minimal changes
GL_COMPUTE_SHADER,
Expand Down Expand Up @@ -2446,6 +2473,7 @@ enum
#define PGL_MAX_TEXTURE_SIZE 16384
#define PGL_MAX_3D_TEXTURE_SIZE 8192
#define PGL_MAX_ARRAY_TEXTURE_LAYERS 8192
#define PGL_MAX_DEBUG_MESSAGE_LENGTH 256

// TODO for now I only support smooth AA lines width 1, so granularity is meaningless
#define PGL_MAX_SMOOTH_WIDTH 1.0f
Expand Down Expand Up @@ -2501,6 +2529,8 @@ typedef struct Shader_Builtins
typedef void (*vert_func)(float* vs_output, vec4* vertex_attribs, Shader_Builtins* builtins, void* uniforms);
typedef void (*frag_func)(float* fs_input, Shader_Builtins* builtins, void* uniforms);

typedef void (*GLDEBUGPROC)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam);

typedef struct glProgram
{
vert_func vertex_shader;
Expand Down Expand Up @@ -2874,6 +2904,10 @@ typedef struct glContext
GLuint cur_program;

GLenum error;
GLDEBUGPROC dbg_callback;
GLchar dbg_msg_buf[PGL_MAX_DEBUG_MESSAGE_LENGTH];
void* dbg_userparam;
GLboolean dbg_output;

// TODO make some or all of these locals, measure performance
// impact. Would be necessary in the long term if I ever
Expand Down Expand Up @@ -3062,6 +3096,7 @@ GLboolean pglResizeFramebuffer(GLsizei w, GLsizei h);

void glViewport(GLint x, GLint y, GLsizei width, GLsizei height);

void glDebugMessageCallback(GLDEBUGPROC callback, void* userParam);

GLubyte* glGetString(GLenum name);
GLenum glGetError(void);
Expand Down Expand Up @@ -7567,18 +7602,32 @@ static void draw_pixel(vec4 cf, int x, int y, float z, int do_frag_processing)
// for CHAR_BIT
#include <limits.h>

// TODO always return from PGL_SET_ERR() ?
// TODO different name? NO_ERROR_CHECKING? LOOK_MA_NO_HANDS?
#ifdef PGL_UNSAFE
#define PGL_SET_ERR(err)
#define PGL_ERR(check, err)
#define PGL_ERR_RET_VAL(check, err, ret)
#define PGL_LOG(err)
#else
#define PGL_SET_ERR(err) do { if (!c->error) c->error = err; } while (0)
#define PGL_LOG(err) \
do { \
if (c->dbg_output && c->dbg_callback) { \
int len = snprintf(c->dbg_msg_buf, PGL_MAX_DEBUG_MESSAGE_LENGTH, "%s in %s()", pgl_err_strs[err-GL_NO_ERROR], __func__); \
c->dbg_callback(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, len, c->dbg_msg_buf, c->dbg_userparam); \
} \
} while (0)

#define PGL_SET_ERR(err) \
do { \
if (!c->error) c->error = err; \
PGL_LOG(err); \
} while (0)

#define PGL_ERR(check, err) \
do { \
if (check) { \
if (!c->error) c->error = err; \
PGL_LOG(err); \
return; \
} \
} while (0)
Expand All @@ -7587,6 +7636,7 @@ static void draw_pixel(vec4 cf, int x, int y, float z, int do_frag_processing)
do { \
if (check) { \
if (!c->error) c->error = err; \
PGL_LOG(err); \
return ret; \
} \
} while (0)
Expand Down Expand Up @@ -7642,6 +7692,31 @@ void default_fs(float* fs_input, Shader_Builtins* builtins, void* uniforms)
fragcolor->w = 1.0f;
}

// TODO Where to put this and what to name it?
#ifndef PGL_UNSAFE
static const char* pgl_err_strs[] =
{
"GL_NO_ERROR",
"GL_INVALID_ENUM",
"GL_INVALID_VALUE",
"GL_INVALID_OPERATION",
"GL_INVALID_FRAMEBUFFER_OPERATION",
"GL_OUT_OF_MEMORY"
};

void dflt_dbg_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
PGL_UNUSED(source);
PGL_UNUSED(type);
PGL_UNUSED(id);
PGL_UNUSED(severity);
PGL_UNUSED(length);
PGL_UNUSED(userParam);

printf("%s\n", message);
}
#endif


void init_glVertex_Array(glVertex_Array* v)
{
Expand Down Expand Up @@ -7800,6 +7875,13 @@ GLboolean init_glContext(glContext* context, u32** back, GLsizei w, GLsizei h, G
c->draw_triangle_back = draw_triangle_fill;

c->error = GL_NO_ERROR;
#ifndef PGL_UNSAFE
c->dbg_callback = dflt_dbg_callback;
c->dbg_output = GL_TRUE;
#else
c->dbg_callback = NULL;
c->dbg_output = GL_FALSE;
#endif

//program 0 is supposed to be undefined but not invalid so I'll
//just make it default, no transform, just draws things red
Expand Down Expand Up @@ -8849,6 +8931,11 @@ void glDrawElementsInstancedBaseInstance(GLenum mode, GLsizei count, GLenum type
}
}

void glDebugMessageCallback(GLDEBUGPROC callback, void* userParam)
{
c->dbg_callback = callback;
c->dbg_userparam = userParam;
}

void glViewport(int x, int y, GLsizei width, GLsizei height)
{
Expand Down Expand Up @@ -9056,6 +9143,9 @@ void glEnable(GLenum cap)
case GL_STENCIL_TEST:
c->stencil_test = GL_TRUE;
break;
case GL_DEBUG_OUTPUT:
c->dbg_output = GL_TRUE;
break;
default:
PGL_SET_ERR(GL_INVALID_ENUM);
}
Expand Down Expand Up @@ -9101,6 +9191,9 @@ void glDisable(GLenum cap)
case GL_STENCIL_TEST:
c->stencil_test = GL_FALSE;
break;
case GL_DEBUG_OUTPUT:
c->dbg_output = GL_FALSE;
break;
default:
PGL_SET_ERR(GL_INVALID_ENUM);
}
Expand Down Expand Up @@ -9233,6 +9326,8 @@ void glGetIntegerv(GLenum pname, GLint* data)
case GL_MAX_3D_TEXTURE_SIZE: data[0] = PGL_MAX_3D_TEXTURE_SIZE; break;
case GL_MAX_ARRAY_TEXTURE_LAYERS: data[0] = PGL_MAX_ARRAY_TEXTURE_LAYERS; break;

case GL_MAX_DEBUG_MESSAGE_LENGTH: data[0] = PGL_MAX_DEBUG_MESSAGE_LENGTH; break;

case GL_POLYGON_MODE:
data[0] = c->poly_mode_front;
data[1] = c->poly_mode_back;
Expand Down
64 changes: 62 additions & 2 deletions src/gl_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,32 @@
// for CHAR_BIT
#include <limits.h>

// TODO always return from PGL_SET_ERR() ?
// TODO different name? NO_ERROR_CHECKING? LOOK_MA_NO_HANDS?
#ifdef PGL_UNSAFE
#define PGL_SET_ERR(err)
#define PGL_ERR(check, err)
#define PGL_ERR_RET_VAL(check, err, ret)
#define PGL_LOG(err)
#else
#define PGL_SET_ERR(err) do { if (!c->error) c->error = err; } while (0)
#define PGL_LOG(err) \
do { \
if (c->dbg_output && c->dbg_callback) { \
int len = snprintf(c->dbg_msg_buf, PGL_MAX_DEBUG_MESSAGE_LENGTH, "%s in %s()", pgl_err_strs[err-GL_NO_ERROR], __func__); \
c->dbg_callback(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, len, c->dbg_msg_buf, c->dbg_userparam); \
} \
} while (0)

#define PGL_SET_ERR(err) \
do { \
if (!c->error) c->error = err; \
PGL_LOG(err); \
} while (0)

#define PGL_ERR(check, err) \
do { \
if (check) { \
if (!c->error) c->error = err; \
PGL_LOG(err); \
return; \
} \
} while (0)
Expand All @@ -33,6 +47,7 @@
do { \
if (check) { \
if (!c->error) c->error = err; \
PGL_LOG(err); \
return ret; \
} \
} while (0)
Expand Down Expand Up @@ -88,6 +103,31 @@ void default_fs(float* fs_input, Shader_Builtins* builtins, void* uniforms)
fragcolor->w = 1.0f;
}

// TODO Where to put this and what to name it?
#ifndef PGL_UNSAFE
static const char* pgl_err_strs[] =
{
"GL_NO_ERROR",
"GL_INVALID_ENUM",
"GL_INVALID_VALUE",
"GL_INVALID_OPERATION",
"GL_INVALID_FRAMEBUFFER_OPERATION",
"GL_OUT_OF_MEMORY"
};

void dflt_dbg_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
PGL_UNUSED(source);
PGL_UNUSED(type);
PGL_UNUSED(id);
PGL_UNUSED(severity);
PGL_UNUSED(length);
PGL_UNUSED(userParam);

printf("%s\n", message);
}
#endif


void init_glVertex_Array(glVertex_Array* v)
{
Expand Down Expand Up @@ -246,6 +286,13 @@ GLboolean init_glContext(glContext* context, u32** back, GLsizei w, GLsizei h, G
c->draw_triangle_back = draw_triangle_fill;

c->error = GL_NO_ERROR;
#ifndef PGL_UNSAFE
c->dbg_callback = dflt_dbg_callback;
c->dbg_output = GL_TRUE;
#else
c->dbg_callback = NULL;
c->dbg_output = GL_FALSE;
#endif

//program 0 is supposed to be undefined but not invalid so I'll
//just make it default, no transform, just draws things red
Expand Down Expand Up @@ -1295,6 +1342,11 @@ void glDrawElementsInstancedBaseInstance(GLenum mode, GLsizei count, GLenum type
}
}

void glDebugMessageCallback(GLDEBUGPROC callback, void* userParam)
{
c->dbg_callback = callback;
c->dbg_userparam = userParam;
}

void glViewport(int x, int y, GLsizei width, GLsizei height)
{
Expand Down Expand Up @@ -1502,6 +1554,9 @@ void glEnable(GLenum cap)
case GL_STENCIL_TEST:
c->stencil_test = GL_TRUE;
break;
case GL_DEBUG_OUTPUT:
c->dbg_output = GL_TRUE;
break;
default:
PGL_SET_ERR(GL_INVALID_ENUM);
}
Expand Down Expand Up @@ -1547,6 +1602,9 @@ void glDisable(GLenum cap)
case GL_STENCIL_TEST:
c->stencil_test = GL_FALSE;
break;
case GL_DEBUG_OUTPUT:
c->dbg_output = GL_FALSE;
break;
default:
PGL_SET_ERR(GL_INVALID_ENUM);
}
Expand Down Expand Up @@ -1679,6 +1737,8 @@ void glGetIntegerv(GLenum pname, GLint* data)
case GL_MAX_3D_TEXTURE_SIZE: data[0] = PGL_MAX_3D_TEXTURE_SIZE; break;
case GL_MAX_ARRAY_TEXTURE_LAYERS: data[0] = PGL_MAX_ARRAY_TEXTURE_LAYERS; break;

case GL_MAX_DEBUG_MESSAGE_LENGTH: data[0] = PGL_MAX_DEBUG_MESSAGE_LENGTH; break;

case GL_POLYGON_MODE:
data[0] = c->poly_mode_front;
data[1] = c->poly_mode_back;
Expand Down
1 change: 1 addition & 0 deletions src/gl_prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GLboolean pglResizeFramebuffer(GLsizei w, GLsizei h);

void glViewport(GLint x, GLint y, GLsizei width, GLsizei height);

void glDebugMessageCallback(GLDEBUGPROC callback, void* userParam);

GLubyte* glGetString(GLenum name);
GLenum glGetError(void);
Expand Down
Loading

0 comments on commit 3a1ae46

Please sign in to comment.