From 17ad59094f6b7ca80daf887d38c2c216ab2894dc Mon Sep 17 00:00:00 2001 From: ts-korhonen Date: Mon, 5 Jul 2021 23:16:00 +0300 Subject: [PATCH] win_opengl: Add logging --- src/win/win_opengl.c | 33 +++++++++++++++++++--------- src/win/win_opengl_glslp.c | 44 +++++++++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/win/win_opengl.c b/src/win/win_opengl.c index 387c24c67..9d44fc47e 100644 --- a/src/win/win_opengl.c +++ b/src/win/win_opengl.c @@ -451,14 +451,10 @@ static void opengl_fail() _endthread(); } -/* static void __stdcall opengl_debugmsg_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) { - OutputDebugStringA("OpenGL: "); - OutputDebugStringA(message); - OutputDebugStringA("\n"); + pclog("OpenGL: %s\n", message); } -*/ /** * @brief Main OpenGL thread proc. @@ -478,13 +474,19 @@ static void opengl_main(void* param) SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); - //SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG | SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); + + if (GLAD_GL_ARB_debug_output) + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG | SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); + else + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); window = SDL_CreateWindow("86Box OpenGL Renderer", 0, 0, resize_info.width, resize_info.height, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS); if (window == NULL) + { + pclog("OpenGL: failed to create OpenGL window.\n"); opengl_fail(); + } /* Keep track of certain parameters, only changed in this thread to avoid race conditions */ int fullscreen = resize_info.fullscreen, video_width = INIT_WIDTH, video_height = INIT_HEIGHT, @@ -495,7 +497,10 @@ static void opengl_main(void* param) SDL_GetWindowWMInfo(window, &wmi); if (wmi.subsystem != SDL_SYSWM_WINDOWS) + { + pclog("OpenGL: subsystem is not SDL_SYSWM_WINDOWS.\n"); opengl_fail(); + } window_hwnd = wmi.info.win.window; @@ -507,29 +512,37 @@ static void opengl_main(void* param) SDL_GLContext context = SDL_GL_CreateContext(window); if (context == NULL) + { + pclog("OpenGL: failed to create OpenGL context.\n"); opengl_fail(); + } SDL_GL_SetSwapInterval(options.vsync); if (!gladLoadGLLoader(SDL_GL_GetProcAddress)) { + pclog("OpenGL: failed to set OpenGL loader.\n"); SDL_GL_DeleteContext(context); opengl_fail(); } - - /* + if (GLAD_GL_ARB_debug_output) { glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE_ARB, GL_DONT_CARE, 0, 0, GL_FALSE); glDebugMessageCallbackARB(opengl_debugmsg_callback, NULL); } - */ + + pclog("OpenGL vendor: %s\n", glGetString(GL_VENDOR)); + pclog("OpenGL renderer: %s\n", glGetString(GL_RENDERER)); + pclog("OpenGL version: %s\n", glGetString(GL_VERSION)); + pclog("OpenGL shader language version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); gl_identifiers gl = { 0 }; if (!initialize_glcontext(&gl)) { + pclog("OpenGL: failed to initialize.\n"); finalize_glcontext(&gl); SDL_GL_DeleteContext(context); opengl_fail(); diff --git a/src/win/win_opengl_glslp.c b/src/win/win_opengl_glslp.c index fc1d296e5..0dff03be9 100644 --- a/src/win/win_opengl_glslp.c +++ b/src/win/win_opengl_glslp.c @@ -63,6 +63,16 @@ void main() {\n\ color = texture(texsampler, tex);\n\ }\n"; +/** + * @brief OpenGL shader program build targets +*/ +typedef enum +{ + OPENGL_BUILD_TARGET_VERTEX, + OPENGL_BUILD_TARGET_FRAGMENT, + OPENGL_BUILD_TARGET_LINK +} opengl_build_target_t; + /** * @brief Reads a whole file into a null terminated string. * @param Path Path to the file relative to executable path. @@ -98,11 +108,11 @@ static char* read_file_to_string(const char* path) return NULL; } -static int check_status(GLuint id, int is_shader) +static int check_status(GLuint id, opengl_build_target_t build_target, const char* shader_path) { GLint status = GL_FALSE; - if (is_shader) + if (build_target != OPENGL_BUILD_TARGET_LINK) glGetShaderiv(id, GL_COMPILE_STATUS, &status); else glGetProgramiv(id, GL_LINK_STATUS, &status); @@ -111,19 +121,37 @@ static int check_status(GLuint id, int is_shader) { int info_log_length; - if (is_shader) + if (build_target != OPENGL_BUILD_TARGET_LINK) glGetShaderiv(id, GL_INFO_LOG_LENGTH, &info_log_length); else glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_length); GLchar* info_log_text = (GLchar*)malloc(sizeof(GLchar) * info_log_length); - if (is_shader) + if (build_target != OPENGL_BUILD_TARGET_LINK) glGetShaderInfoLog(id, info_log_length, NULL, info_log_text); else glGetProgramInfoLog(id, info_log_length, NULL, info_log_text); - /* TODO: error logging */ + const char* reason = NULL; + + switch (build_target) + { + case OPENGL_BUILD_TARGET_VERTEX: + reason = "compiling vertex shader"; + break; + case OPENGL_BUILD_TARGET_FRAGMENT: + reason = "compiling fragment shader"; + break; + case OPENGL_BUILD_TARGET_LINK: + reason = "linking shader program"; + break; + } + + /* Shader compilation log can be lengthy, mark begin and end */ + const char* line = "--------------------"; + + pclog("OpenGL: Error when %s in %s:\n%sBEGIN%s\n%s\n%s END %s\n", reason, shader_path, line, line, info_log_text, line, line); free(info_log_text); @@ -153,11 +181,11 @@ GLuint load_custom_shaders(const char* path) glShaderSource(vertex_id, 2, vertex_sources, NULL); glCompileShader(vertex_id); - success *= check_status(vertex_id, 1); + success *= check_status(vertex_id, OPENGL_BUILD_TARGET_VERTEX, path); glShaderSource(fragment_id, 2, fragment_sources, NULL); glCompileShader(fragment_id); - success *= check_status(fragment_id, 1); + success *= check_status(fragment_id, OPENGL_BUILD_TARGET_FRAGMENT, path); free(shader); @@ -170,7 +198,7 @@ GLuint load_custom_shaders(const char* path) glAttachShader(prog_id, vertex_id); glAttachShader(prog_id, fragment_id); glLinkProgram(prog_id); - check_status(prog_id, 0); + check_status(prog_id, OPENGL_BUILD_TARGET_LINK, path); glDetachShader(prog_id, vertex_id); glDetachShader(prog_id, fragment_id);