diff --git a/src/application/sdl-app.cpp b/src/application/sdl-app.cpp index 29311a70..5330c1a8 100644 --- a/src/application/sdl-app.cpp +++ b/src/application/sdl-app.cpp @@ -48,10 +48,18 @@ sdl::app_display::app_display(char const *title, ivec2 res) } } - /* This seems to fix the swap context bug. - * However, perfs warning have been may occur. */ + // This seems to fix a bug we used to have at context swap. Maybe remove one day. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); +#if 0 + // Ask for GL 3.2 at least + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); +#endif +#if LOL_BUILD_DEBUG + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); +#endif + int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; if (window_size == ivec2(0)) flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; diff --git a/src/gpu/debug.cpp b/src/gpu/debug.cpp index ffe6a5b8..7e59767f 100644 --- a/src/gpu/debug.cpp +++ b/src/gpu/debug.cpp @@ -17,6 +17,94 @@ namespace lol { +static std::map gl_dbg_source_to_str +{ + { GL_DEBUG_SOURCE_API, "API" }, + { GL_DEBUG_SOURCE_WINDOW_SYSTEM, "Window System" }, + { GL_DEBUG_SOURCE_SHADER_COMPILER, "Shader Compiler" }, + { GL_DEBUG_SOURCE_THIRD_PARTY, "Third Party" }, + { GL_DEBUG_SOURCE_APPLICATION, "Application" }, + { GL_DEBUG_SOURCE_OTHER, "Other" }, +}; + +static std::map gl_dbg_type_to_str +{ + { GL_DEBUG_TYPE_ERROR, "Error" }, + { GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, "Deprecated Behaviour" }, + { GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR, "Undefined Behaviour" }, + { GL_DEBUG_TYPE_PORTABILITY, "Portability" }, + { GL_DEBUG_TYPE_PERFORMANCE, "Performance" }, + { GL_DEBUG_TYPE_MARKER, "Marker" }, + { GL_DEBUG_TYPE_PUSH_GROUP, "Push Group" }, + { GL_DEBUG_TYPE_POP_GROUP, "Pop Group" }, + { GL_DEBUG_TYPE_OTHER, "Other" }, +}; + +static std::map gl_dbg_severity_to_str +{ + { GL_DEBUG_SEVERITY_HIGH, "High" }, + { GL_DEBUG_SEVERITY_MEDIUM, "Medium" }, + { GL_DEBUG_SEVERITY_LOW, "Low" }, + { GL_DEBUG_SEVERITY_NOTIFICATION, "Notification" }, +}; + +static void gl_debug(GLenum source, GLenum type, GLuint id, + GLenum severity, GLsizei length, + const GLchar *message, const void *user_param) +{ + switch (id) + { + default: + msg::debug("GL debug: %s\n", message); + if (gl_dbg_source_to_str.count(source) == 1) + msg::debug(" source: %s\n", gl_dbg_source_to_str[source]); + if (gl_dbg_type_to_str.count(type) == 1) + msg::debug(" type: %s\n", gl_dbg_type_to_str[type]); + msg::debug(" ID: %d\n", id); + if (gl_dbg_severity_to_str.count(severity) == 1) + msg::debug(" severity: %s\n", gl_dbg_severity_to_str[severity]); + break; + // Some debug messages are just annoying informational messages + case 131185: // glBufferData + case 131169: // glFramebufferRenderbuffer + break; + } +} + +static void gl_debug_amd(GLuint id, GLenum category, GLenum severity, + GLsizei length, const GLchar* message, + GLvoid* user_param) +{ + gl_debug(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, id, + severity, length, message, user_param); +} + +void gpu::setup_debug() +{ +#if LOL_BUILD_DEBUG + GLint glflags; + glGetIntegerv(GL_CONTEXT_FLAGS, &glflags); + + if (glflags & GL_CONTEXT_FLAG_DEBUG_BIT) + { + // Debug Output available + + glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); +#if defined LOL_USE_GLEW && !defined __APPLE__ + if (GLEW_ARB_debug_output) + glDebugMessageCallbackARB(gl_debug, NULL); + else if (GLEW_AMD_debug_output) + glDebugMessageCallbackAMD(gl_debug_amd, NULL); + else if (GLEW_KHR_debug) +#endif + glDebugMessageCallback(gl_debug, NULL); + + glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); + } +#endif +} + void gpu::error(char const *msg, int error) { msg::error("%s: GL error 0x%04x\n", msg, error); diff --git a/src/gpu/renderer.cpp b/src/gpu/renderer.cpp index 12cdbe4a..3bbef5e0 100644 --- a/src/gpu/renderer.cpp +++ b/src/gpu/renderer.cpp @@ -62,7 +62,8 @@ Renderer::Renderer(ivec2 size) : m_data(new RendererData()) { #if defined LOL_USE_GLEW && !defined __APPLE__ - /* Initialise GLEW if necessary */ + // Initialise GLEW if necessary + // FIXME: move this somewhere else GLenum glerr = glewInit(); if (glerr != GLEW_OK) { @@ -71,6 +72,8 @@ Renderer::Renderer(ivec2 size) } #endif + gpu::setup_debug(); + /* Initialise rendering states */ m_data->m_viewport = ibox2(0, 0, 0, 0); SetViewport(ibox2(ivec2::zero, size)); @@ -109,7 +112,7 @@ Renderer::Renderer(ivec2 size) SetPolygonMode(PolygonMode::Fill); /* Add some rendering states that we don't export to the user */ -#if defined HAVE_GL_2X && !defined __APPLE__ +#if defined HAVE_GL_2X && !defined GL_VERSION_3_1 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); #endif } @@ -218,6 +221,8 @@ void Renderer::SetAlphaFunc(AlphaFunc func, float alpha) #if defined HAVE_GLES_2X /* not supported */ +#elif defined GL_VERSION_3_1 + /* deprecated */ #elif defined GL_VERSION_1_1 switch (func) { diff --git a/src/lol/gpu/debug.h b/src/lol/gpu/debug.h index ab840071..33e53c3b 100644 --- a/src/lol/gpu/debug.h +++ b/src/lol/gpu/debug.h @@ -18,6 +18,7 @@ namespace lol namespace gpu { +void setup_debug(); void error(char const *msg, int error); } // namespace gpu