X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=glwrap.c;h=a90ef8c4707b9f67f13de51495534aa6d6d5258a;hb=e7e5c4643bbf59b0df88356ef5c511ebe764ff58;hp=3a789fc131e0c693c398386b8f6f152038cfda35;hpb=b8b9a79e315bf3a030c3b0840ad1490746b5a8e0;p=fips diff --git a/glwrap.c b/glwrap.c index 3a789fc..a90ef8c 100644 --- a/glwrap.c +++ b/glwrap.c @@ -23,71 +23,32 @@ #include "glwrap.h" +#include "metrics.h" + +/* The prototypes for some OpenGL functions changed at one point from: + * + * const void* *indices + * to: + * const void* const coist *indices + * + * This makes it difficult for us to provide an implementation of + * these functions that is consistent with the locally-available gl.h + * since we don't know if the extra const will be present or not. + * + * To workaround this problem, we simply #define away const altogether + * before including gl.h. + * + * Kudos to Keith Packard for suggesting this kludge. + */ +#define const + #define GL_GLEXT_PROTOTYPES #include -#include - #include "dlwrap.h" -typedef struct counter -{ - unsigned id; - unsigned program; - struct counter *next; -} counter_t; - -typedef struct program_metrics -{ - unsigned id; - double ticks; -} program_metrics_t; - -typedef struct context -{ - unsigned int program; - - counter_t *counter_head; - counter_t *counter_tail; - - unsigned num_program_metrics; - program_metrics_t *program_metrics; -} context_t; - -/* FIXME: Need a map from integers to context objects and track the - * current context with glXMakeContextCurrent, eglMakeCurrent, etc. */ - -context_t current_context; - -static unsigned -add_counter (void) -{ - counter_t *counter; - - counter = malloc (sizeof(counter_t)); - if (counter == NULL) { - fprintf (stderr, "Out of memory\n"); - exit (1); - } - - glGenQueries (1, &counter->id); - - counter->program = current_context.program; - counter->next = NULL; - - if (current_context.counter_tail) { - current_context.counter_tail->next = counter; - current_context.counter_tail = counter; - } else { - current_context.counter_tail = counter; - current_context.counter_head = counter; - } - - return counter->id; -} - -static void * -lookup (const char *name) +void * +glwrap_lookup (char *name) { const char *libgl_filename = "libGL.so.1"; static void *libgl_handle = NULL; @@ -104,20 +65,12 @@ lookup (const char *name) return dlwrap_real_dlsym (libgl_handle, name); } -/* Defer to the underlying, ''real'' function to do the real work. */ -#define DEFER(function,...) do { \ - static typeof(&function) real_ ## function; \ - if (! real_ ## function) \ - real_ ## function = lookup (#function); \ - real_ ## function(__VA_ARGS__); \ -} while (0); - /* Execute a glBegineQuery/glEndQuery pair around an OpenGL call. */ #define TIMED_DEFER(function,...) do { \ unsigned counter; \ - counter = add_counter (); \ + counter = metrics_add_counter (); \ glBeginQuery (GL_TIME_ELAPSED, counter); \ - DEFER(function, __VA_ARGS__); \ + GLWRAP_DEFER(function, __VA_ARGS__); \ glEndQuery (GL_TIME_ELAPSED); \ } while (0); @@ -379,10 +332,30 @@ glClear (GLbitfield mask) TIMED_DEFER (glClear, mask); } +/* We can't just use TIMED_DEFER for glBegin/glEnd since the + * glBeginQuery/glEndQuery calls must both be outside + * glBegin/glEnd. */ +void +glBegin (GLenum mode) +{ + if (! inside_new_list) + { + unsigned counter; + counter = metrics_add_counter (); + glBeginQuery (GL_TIME_ELAPSED, counter); + } + + GLWRAP_DEFER (glBegin, mode); +} + void glEnd (void) { - TIMED_DEFER (glEnd,); + GLWRAP_DEFER (glEnd); + + if (! inside_new_list) { + glEndQuery (GL_TIME_ELAPSED); + } } void @@ -417,105 +390,15 @@ glBlitFramebufferEXT (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, void glUseProgram (GLuint program) { - current_context.program = program; + metrics_set_current_program (program); - DEFER(glUseProgram, program); + GLWRAP_DEFER(glUseProgram, program); } void glUseProgramObjectARB (GLhandleARB programObj) { - current_context.program = programObj; - - DEFER(glUseProgramObjectARB, programObj); -} - -static void -accumulate_program_ticks (unsigned program_id, unsigned ticks) -{ - context_t *ctx = ¤t_context; - unsigned i; - - if (program_id >= ctx->num_program_metrics) { - ctx->program_metrics = realloc (ctx->program_metrics, - (program_id + 1) * sizeof (program_metrics_t)); - for (i = ctx->num_program_metrics; i < program_id + 1; i++) { - ctx->program_metrics[i].id = i; - ctx->program_metrics[i].ticks = 0.0; - } - - ctx->num_program_metrics = program_id + 1; - } - - ctx->program_metrics[program_id].ticks += ticks; -} - -/* FIXME: Should sort the metrics, print out percentages, etc. */ -static void -print_program_metrics (void) -{ - context_t *ctx = ¤t_context; - unsigned i; - - for (i = 0; i < ctx->num_program_metrics; i++) { - if (ctx->program_metrics[i].ticks == 0.0) - continue; - printf ("Program %d:\t%7.2f mega-ticks\n", - i, ctx->program_metrics[i].ticks / 1e6); - } -} - -void -glwrap_end_frame (void) -{ - static int initialized = 0; - static int frames; - static struct timeval tv_start, tv_now; - - if (! initialized) { - frames = 0; - gettimeofday (&tv_start, NULL); - initialized = 1; - } - - - frames++; - - if (frames % 60 == 0) { - double fps; - gettimeofday (&tv_now, NULL); - - fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec + - (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6); - - printf("FPS: %.3f\n", fps); - - print_program_metrics (); - } - - /* Consume all counters that are ready. */ - counter_t *counter = current_context.counter_head; - - while (counter) { - GLint available; - GLuint elapsed; - - glGetQueryObjectiv (counter->id, GL_QUERY_RESULT_AVAILABLE, - &available); - if (! available) - break; - - glGetQueryObjectuiv (counter->id, GL_QUERY_RESULT, &elapsed); + metrics_set_current_program (programObj); - accumulate_program_ticks (counter->program, elapsed); - - current_context.counter_head = counter->next; - if (current_context.counter_head == NULL) - current_context.counter_tail = NULL; - - glDeleteQueries (1, &counter->id); - - free (counter); - counter = current_context.counter_head; - } + GLWRAP_DEFER(glUseProgramObjectARB, programObj); }