From: Carl Worth Date: Fri, 20 Sep 2013 22:29:03 +0000 (-0700) Subject: Initial import of libglfps demo for glaze X-Git-Url: https://git.cworth.org/git?p=glfps;a=commitdiff_plain;h=3b0270c3e44c4a9e0f95ac43069883df9eab6d01 Initial import of libglfps demo for glaze This first implementation is a simple FPS-counter for OpenGL implemented via a library suitable for use with LD_PRELOAD. --- 3b0270c3e44c4a9e0f95ac43069883df9eab6d01 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..916f458 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +libglfps.so diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3e7e390 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CC ?= gcc +CFLAGS ?= -g -Wall -Wextra -Wmissing-declarations + +libglfps.so: glfps.c + $(CC) $(CFLAGS) -fPIC -shared -Wl,-Bsymbolic -o $@ $< + +clean: + rm -f libglfps.so diff --git a/glfps.c b/glfps.c new file mode 100644 index 0000000..68d389a --- /dev/null +++ b/glfps.c @@ -0,0 +1,48 @@ +#define _GNU_SOURCE /* For RTLD_NEXT */ +#include + +#include + +#include +#include +#include + +#include + +/* How many frames between reports. */ +#define REPORT_FREQ 60 + +static void +on_each_frame (void) +{ + static int count = 0; + static struct timeval tv_last; + struct timeval tv_now; + + if ((count % REPORT_FREQ) == 0) { + gettimeofday (&tv_now, NULL); + if (count == 0) { + printf ("glfps: Initializing FPS timer\n"); + } else { + double elapsed = ((tv_now.tv_sec - tv_last.tv_sec) + + (tv_now.tv_usec - tv_last.tv_usec) / 1e6); + printf ("FPS: %.3f\n", ((double) REPORT_FREQ) / elapsed); + } + tv_last = tv_now; + } + + count++; +} + +void +glXSwapBuffers (Display *dpy, GLXDrawable drawable) +{ + static typeof(&glXSwapBuffers) real_glXSwapBuffers = NULL; + + if (real_glXSwapBuffers == NULL) + real_glXSwapBuffers = dlsym (RTLD_NEXT, "glXSwapBuffers"); + + on_each_frame (); + + real_glXSwapBuffers (dpy, drawable); +}