diff options
-rw-r--r-- | main.c | 89 |
1 files changed, 72 insertions, 17 deletions
@@ -11,6 +11,16 @@ #define SS(v) #v #define S(v) SS(v) +// default log level +#define DEFAULT_LOG_PRIORITY SDL_LOG_PRIORITY_VERBOSE + +// logging macros +#define LOG_C(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) +#define LOG_W(...) SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) +#define LOG_V(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) +#define LOG_I(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) +#define LOG_D(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) + // number of time records to keep (must be power of two - 1) #define MAX_TIMES 0xFF @@ -20,8 +30,11 @@ // use compute shader to update particles? #define USE_COMPUTE_SHADER false +// enable gl debug +#define USE_GL_DEBUG false + // number of particles -#define NUM_PARTICLES 1000 +#define NUM_PARTICLES 1024 // number of compute shader workgroups #define WORKGROUP_SIZE 1 @@ -65,6 +78,7 @@ verts[] = { static const char cs_src[] = "#version 320 es\n" + // "#pragma debug(on)\n" "\n" "layout(local_size_x = " S(WORKGROUP_SIZE) ") in;\n" "\n" @@ -118,6 +132,26 @@ RENDER_SHADERS[] = { { GL_FRAGMENT_SHADER, fs_src }, }; +static void +debug_cb( + GLenum source, + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar *message, + const void *arg +) { + UNUSED(source); + UNUSED(type); + UNUSED(id); + UNUSED(severity); + UNUSED(length); + UNUSED(arg); + + LOG_D("GL: %s", message); +} + static GLuint compile_shader( const GLenum type, @@ -136,7 +170,7 @@ compile_shader( if (!ok) { GLchar log[1024]; glGetShaderInfoLog(r, sizeof(log), NULL, log); - SDL_Log("glCompileShader() failed: %s", log); + LOG_C("glCompileShader() failed: %s", log); exit(EXIT_FAILURE); } @@ -174,7 +208,7 @@ link_program( if (!ok) { GLchar log[1024]; glGetProgramInfoLog(r, sizeof(log), NULL, log); - SDL_Log("glLinkProgram() failed: %s", log); + LOG_C("glLinkProgram() failed: %s", log); exit(EXIT_FAILURE); } @@ -210,7 +244,7 @@ init_particles(void) { // generate seed data uint8_t seeds[5 * NUM_PARTICLES]; if (!RAND_bytes(seeds, 5 * NUM_PARTICLES)) { - SDL_Log("RAND_bytes() failed"); + LOG_C("RAND_bytes() failed"); exit(EXIT_FAILURE); } @@ -221,7 +255,7 @@ init_particles(void) { float x = 2.0 * seeds[5 * i + 0] / 0xFF - 1.0, y = 2.0 * seeds[5 * i + 1] / 0xFF - 1.0; - // SDL_Log("%f,%f", x, y); + // LOG_D("%f,%f", x, y); // populate position data[4 * i + 0] = x; @@ -261,6 +295,7 @@ update_particles( glUseProgram(compute_prog); glDispatchCompute(NUM_PARTICLES / WORKGROUP_SIZE, 1, 1); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); + // glMemoryBarrier(GL_ALL_BARRIER_BITS); } else { UNUSED(compute_prog); @@ -316,9 +351,12 @@ update_viewport( static void ctx_init(context_t * const ctx) { + // set log level + SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, DEFAULT_LOG_PRIORITY); + // init sdl if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { - SDL_Log("SDL_Init() failed: %s", SDL_GetError()); + LOG_C("SDL_Init() failed: %s", SDL_GetError()); exit(EXIT_FAILURE); } @@ -334,7 +372,7 @@ ctx_init(context_t * const ctx) { // check for error if (!ctx->win) { - SDL_Log("SDL_CreateWindow() failed: %s", SDL_GetError()); + LOG_C("SDL_CreateWindow() failed: %s", SDL_GetError()); exit(EXIT_FAILURE); } @@ -343,26 +381,26 @@ ctx_init(context_t * const ctx) { SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES ) < 0) { - SDL_Log("SDL_GL_SetAttribute() failed: %s", SDL_GetError()); + LOG_C("SDL_GL_SetAttribute() failed: %s", SDL_GetError()); exit(EXIT_FAILURE); } // set gl context majory version hint if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) { - SDL_Log("SDL_GL_SetAttribute() failed: %s", SDL_GetError()); + LOG_C("SDL_GL_SetAttribute() failed: %s", SDL_GetError()); exit(EXIT_FAILURE); } // set gl context minor version hint if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2) < 0) { - SDL_Log("SDL_GL_SetAttribute() failed: %s", SDL_GetError()); + LOG_C("SDL_GL_SetAttribute() failed: %s", SDL_GetError()); exit(EXIT_FAILURE); } // create GL context SDL_GLContext context = SDL_GL_CreateContext(ctx->win); if (!context) { - SDL_Log("SDL_GL_CreateContext() failed: %s", SDL_GetError()); + LOG_C("SDL_GL_CreateContext() failed: %s", SDL_GetError()); exit(EXIT_FAILURE); } @@ -371,11 +409,28 @@ ctx_init(context_t * const ctx) { { GLenum err = glewInit(); if (err != GLEW_OK) { - SDL_Log("glewInit() failed: %s", glewGetErrorString(err)); + LOG_C("glewInit() failed: %s", glewGetErrorString(err)); exit(EXIT_FAILURE); } } + if (USE_GL_DEBUG) { + // enable debugging + glEnable(GL_DEBUG_OUTPUT); + glDebugMessageCallback(debug_cb, ctx); + } + + { + int sizes[3]; + + for (int i = 0; i < 3; i++) { + glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, i, sizes + i); + } + + // log workgroup sizes + LOG_I("workgroup_sizes = [%d, %d, %d]", sizes[0], sizes[1], sizes[2]); + } + // set viewport size // (after GLEW init) update_viewport(ctx->win); @@ -383,7 +438,7 @@ ctx_init(context_t * const ctx) { // set swap interval // (after context init) if (SDL_GL_SetSwapInterval(1) < 0) { - SDL_Log("SDL_GL_SetSwapInterval() failed: %s", SDL_GetError()); + LOG_C("SDL_GL_SetSwapInterval() failed: %s", SDL_GetError()); exit(EXIT_FAILURE); } @@ -423,7 +478,7 @@ handle_events( ctx->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0 ) < 0) { // log warning - SDL_Log("SDL_SetWindowFullscreen() failed: %s", SDL_GetError()); + LOG_W("SDL_SetWindowFullscreen() failed: %s", SDL_GetError()); ctx->fullscreen = !ctx->fullscreen; } @@ -442,7 +497,7 @@ handle_events( // update viewport update_viewport(ctx->win); } else { - SDL_Log("ignoring SIZE_CHANGED event from unknown window"); + LOG_W("ignoring SIZE_CHANGED event from unknown window"); } } @@ -460,7 +515,7 @@ handle_events( } // print fps - SDL_Log("fps: %f", sum * 1.0 / MAX_TIMES); + LOG_I("fps: %f", 1.0 * sum / MAX_TIMES); } break; @@ -567,7 +622,7 @@ int main(int argc, char *argv[]) { // remove timer if (SDL_RemoveTimer(timer_id) == SDL_FALSE) { - SDL_Log("SDL_RemoveTimer() failed"); + LOG_W("SDL_RemoveTimer() failed"); } // delete GL context |