Created
August 21, 2025 06:46
-
-
Save Bigfoot71/ac652f10e73b364a5fd5a3b99ef590b3 to your computer and use it in GitHub Desktop.
gl-profiling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Get a glad header (4.6 core): https://gen.glad.sh/ | |
| Build with: `gcc prof.c -o prof -lSDL2` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <SDL2/SDL.h> | |
| #define GLAD_GL_IMPLEMENTATION | |
| #include "./gl.h" | |
| #define GPU_PROFILE_BLOCK(Name, NSAMPLES, CodeBlock) \ | |
| do { \ | |
| static GLuint query = 0; \ | |
| static double hist[NSAMPLES] = {0}; \ | |
| static int count = 0, index = 0; \ | |
| \ | |
| if (!query) glGenQueries(1, &query); \ | |
| \ | |
| glBeginQuery(GL_TIME_ELAPSED, query); \ | |
| do { CodeBlock } while(0); \ | |
| glEndQuery(GL_TIME_ELAPSED); \ | |
| \ | |
| GLuint64 ns = 0; \ | |
| glGetQueryObjectui64v(query, GL_QUERY_RESULT, &ns); \ | |
| double ms = ns / 1e6; \ | |
| \ | |
| hist[index] = ms; \ | |
| index = (index + 1) % NSAMPLES; \ | |
| if (count < NSAMPLES) count++; \ | |
| \ | |
| if (count == NSAMPLES) { \ | |
| double sum = 0.0; \ | |
| for (int i = 0; i < NSAMPLES; i++) sum += hist[i]; \ | |
| printf("[GPU] %s avg(%d) = %.3f ms\n", Name, NSAMPLES, sum/NSAMPLES); \ | |
| count = 0; \ | |
| } \ | |
| } while(0) | |
| int main(int argc, char** argv) | |
| { | |
| if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
| SDL_Log("SDL init failed: %s", SDL_GetError()); | |
| return -1; | |
| } | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6); | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
| SDL_Window* win = SDL_CreateWindow( | |
| "GPU Profiler Example", | |
| SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | |
| 800, 600, | |
| SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | |
| ); | |
| if (!win) { | |
| SDL_Log("Window creation failed: %s", SDL_GetError()); | |
| SDL_Quit(); | |
| return -1; | |
| } | |
| SDL_GLContext ctx = SDL_GL_CreateContext(win); | |
| if (!ctx) { | |
| SDL_Log("GL context failed: %s", SDL_GetError()); | |
| SDL_DestroyWindow(win); | |
| SDL_Quit(); | |
| return -1; | |
| } | |
| if (!gladLoadGL((GLADloadfunc)SDL_GL_GetProcAddress)) { | |
| SDL_Log("Failed to init GLAD"); | |
| SDL_DestroyWindow(win); | |
| SDL_Quit(); | |
| return -1; | |
| } | |
| int running = 1; | |
| while (running) { | |
| SDL_Event e; | |
| while (SDL_PollEvent(&e)) { | |
| if (e.type == SDL_QUIT) running = 0; | |
| } | |
| GPU_PROFILE_BLOCK("ClearScreen", 32, { | |
| glClearColor(0.2f, 0.3f, 0.3f, 1.0f); | |
| glClear(GL_COLOR_BUFFER_BIT); | |
| }); | |
| SDL_GL_SwapWindow(win); | |
| } | |
| SDL_GL_DeleteContext(ctx); | |
| SDL_DestroyWindow(win); | |
| SDL_Quit(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment