diff --git a/src/GLDebug.h b/src/GLDebug.h new file mode 100644 index 0000000..b0056a2 --- /dev/null +++ b/src/GLDebug.h @@ -0,0 +1,109 @@ + +// NOTE: get useful debug messages from opengl +// https://www.khronos.org/opengl/wiki/Debug_Output + +#include + +#include "shader.h" + + +void dumpShader(GLuint prog_id); +void openglDebugCallback(GLenum source, + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar* message, + const void* userParam); + + +#ifdef GL_DEBUG_IMPLEMENTATION + +const char* +glEnumToString(GLenum e) +{ + switch (e) { + case GL_FLOAT_MAT4: return "GL_FLOAT_MAT4"; + case GL_FLOAT_VEC3: return "GL_FLOAT_VEC3"; + case GL_FLOAT_VEC4: return "GL_FLOAT_VEC4"; + + case GL_DEBUG_TYPE_ERROR: return "GL_DEBUG_TYPE_ERROR"; + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: return "GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR"; + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: return "GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR"; + case GL_DEBUG_TYPE_PORTABILITY: return "GL_DEBUG_TYPE_PORTABILITY"; + case GL_DEBUG_TYPE_PERFORMANCE: return "GL_DEBUG_TYPE_PERFORMANCE"; + case GL_DEBUG_TYPE_MARKER: return "GL_DEBUG_TYPE_MARKER"; + case GL_DEBUG_TYPE_PUSH_GROUP: return "GL_DEBUG_TYPE_PUSH_GROUP"; + case GL_DEBUG_TYPE_POP_GROUP: return "GL_DEBUG_TYPE_POP_GROUP"; + case GL_DEBUG_TYPE_OTHER: return "GL_DEBUG_TYPE_OTHER"; + + case GL_DEBUG_SEVERITY_HIGH: return "GL_DEBUG_SEVERITY_HIGH"; + case GL_DEBUG_SEVERITY_MEDIUM: return "GL_DEBUG_SEVERITY_MEDIUM"; + case GL_DEBUG_SEVERITY_LOW: return "GL_DEBUG_SEVERITY_LOW"; + case GL_DEBUG_SEVERITY_NOTIFICATION: return "GL_DEBUG_SEVERITY_NOTIFICATION"; + + default: return "???"; + } +} + +void +openglDebugCallback(GLenum source, + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar* message, + const void* userParam) +{ + std::cout << ((type == GL_DEBUG_TYPE_ERROR) ? "Error" : "Debug") + << (type == GL_DEBUG_TYPE_ERROR ? "** GL Error **" : "") + << ", type: " << glEnumToString(type) + << ", severity: " << glEnumToString(severity) + << ", message: " << message << "\n"; +} + +void +dumpShader(GLuint prog_id) +{ + printf("------------------------\n"); + printf("%s(), dumping shader info, program id: %d\n", + __FUNCTION__, prog_id); + + GLint active_uniforms; + GLint active_uniform_blocks; + GLint active_attribs; + + // NOTE: unused uniforms/attributes get optimized away + // https://www.khronos.org/opengl/wiki/Program_Introspection#Attributes + glGetProgramiv(prog_id, GL_ACTIVE_UNIFORMS, &active_uniforms); + glGetProgramiv(prog_id, GL_ACTIVE_UNIFORM_BLOCKS, &active_uniform_blocks); + glGetProgramiv(prog_id, GL_ACTIVE_ATTRIBUTES, &active_attribs); + + printf("active uniforms: %d\n", active_uniforms); + printf("active uniform blocks: %d\n", active_uniform_blocks); + printf("active attributes: %d\n", active_attribs); + + GLchar uni_name[256]; + GLsizei length; + GLint size; + GLenum type; + + for (int i = 0; i < active_uniforms; i++) { + glGetActiveUniform(prog_id, i, sizeof(uni_name), + &length, &size, &type, uni_name); + printf("uniform idx: %d, type: %s, name: %s \n", + i, glEnumToString(type), uni_name); + } + + for (int i = 0; i < active_attribs; i++) { + glGetActiveAttrib(prog_id, i, sizeof(uni_name), + &length, &size, &type, uni_name); + printf("attribute idx: %d, type: %s, name: %s \n", + i, glEnumToString(type), uni_name); + } + + printf("------------------------\n"); +} + +#endif // ifdef GL_DEBUG_IMPLEMENTATION + diff --git a/src/dumpShader.inl b/src/dumpShader.inl deleted file mode 100644 index 0935dac..0000000 --- a/src/dumpShader.inl +++ /dev/null @@ -1,62 +0,0 @@ - -#include - -#include "shader.h" - - -const char* gl_enum_to_string(GLenum e); - - -void -dumpShader(GLuint prog_id) -{ - std::cout << "dumping shader info, program id: " << prog_id << "\n"; - - GLint active_uniforms; - GLint active_uniform_blocks; - GLint active_attribs; - - // NOTE: unused uniforms/attributes get optimized away - // https://www.khronos.org/opengl/wiki/Program_Introspection#Attributes - // https://stackoverflow.com/questions/54811319/how-to-get-from-glgetattriblocation-to-glgetactiveattrib-index - glGetProgramiv(prog_id, GL_ACTIVE_UNIFORMS, &active_uniforms); - glGetProgramiv(prog_id, GL_ACTIVE_UNIFORM_BLOCKS, &active_uniform_blocks); - glGetProgramiv(prog_id, GL_ACTIVE_ATTRIBUTES, &active_attribs); - - printf("active uniforms: %d\n", active_uniforms); - printf("active uniform blocks: %d\n", active_uniform_blocks); - printf("active attributes: %d\n", active_attribs); - - GLchar uni_name[256]; - GLsizei length; - GLint size; - GLenum type; - - for (int i = 0; i < active_uniforms; i++) { - glGetActiveUniform(prog_id, i, sizeof(uni_name), - &length, &size, &type, uni_name); - printf("uniform idx: %d, type: %s, name: %s \n", - i, gl_enum_to_string(type), uni_name); - } - - for (int i = 0; i < active_attribs; i++) { - glGetActiveAttrib(prog_id, i, sizeof(uni_name), - &length, &size, &type, uni_name); - printf("attribute idx: %d, type: %s, name: %s \n", - i, gl_enum_to_string(type), uni_name); - } - -} - -const char* -gl_enum_to_string(GLenum e) -{ - switch (e) { - case 0x8b5c: return "GL_FLOAT_MAT4"; - case 0x8b51: return "GL_FLOAT_VEC3"; - case 0x8B52: return "GL_FLOAT_VEC4"; - - default: return "???"; - } -} - diff --git a/src/main.cpp b/src/main.cpp index 2208f9f..efb58bd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "asset.h" #include "dumbLog.h" +#include "GLDebug.h" #include "shader.h" #include "types.h" @@ -168,22 +169,6 @@ initRenderState(memory_arena* arena) return rs; } -void -openglDebugCallback(GLenum source, - GLenum type, - GLuint id, - GLenum severity, - GLsizei length, - const GLchar* message, - const void* userParam) -{ - std::cout << ((type == GL_DEBUG_TYPE_ERROR) ? "Error" : "Debug") - << (type == GL_DEBUG_TYPE_ERROR ? "** GL Error **" : "") - << ", type: " << type - << ", severity: " << severity - << ", message: " << message << "\n"; -} - bool initGraphics(SDLHandles* handles) { diff --git a/src/shader.cpp b/src/shader.cpp index dcdd116..b2e88d3 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -10,9 +10,10 @@ #include #include "asset.h" +#define GL_DEBUG_IMPLEMENTATION +#include "GLDebug.h" #include "shader.h" -#include "dumpShader.inl" // NOTE: forward declarations diff --git a/src/tiny_gltf.cc b/src/tiny_gltf.cc index ef72029..e5a9eba 100644 --- a/src/tiny_gltf.cc +++ b/src/tiny_gltf.cc @@ -1,3 +1,4 @@ + #define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION #define TINYGLTF_IMPLEMENTATION