From fcb01b3da1a4bc51482e67b88b7c59dc5c8b7578 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sat, 17 Sep 2022 03:56:12 +0300 Subject: [PATCH] added swap interval control --- Makefile | 2 +- glpixels.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 95 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index bb38ce2..69d29a1 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ obj = glpixels.o bin = glpixels CFLAGS = -pedantic -Wall -g -LDFLAGS = -lGL -lglut -lm +LDFLAGS = -lX11 -lGL -lglut -lm $(bin): $(obj) $(CC) -o $@ $(obj) $(LDFLAGS) diff --git a/glpixels.c b/glpixels.c index e63476b..38ffd2d 100644 --- a/glpixels.c +++ b/glpixels.c @@ -1,10 +1,17 @@ #include #include +#include #include #include -#define GL_GLEXT_PROTOTYPES 1 #include +#if defined(unix) || defined(__unix__) +#include + +#define BUILD_X11 +#define get_proc_addr(s) (void (*)())glXGetProcAddress((unsigned char*)(s)) +#endif + enum { MODE_POINTS, MODE_DRAWPIX, @@ -27,8 +34,8 @@ void change_mode(int m); int win_width, win_height; int max_xscroll, max_yscroll; -#define IMG_W 1024 -#define IMG_H 1024 +#define IMG_W 1400 +#define IMG_H 1200 unsigned int img[IMG_W * IMG_H]; float *varr, *carr; unsigned int vbo_pos, vbo_col; @@ -37,11 +44,25 @@ unsigned int tex, quad, tri; int mode = MODE_POINTS; -int have_vbo = 1; /* TODO */ +int have_vbo; unsigned int start_tm; unsigned int num_frames; +void (*gl_gen_buffers)(GLsizei, GLuint*); +void (*gl_bind_buffer)(GLenum, GLuint); +void (*gl_buffer_data)(GLenum, GLsizeiptr, const void*, GLenum); +void (*gl_buffer_sub_data)(GLenum, GLintptr, GLsizeiptr, const void *data); + +#ifdef BUILD_X11 +static Display *dpy; +static Window win; + +static void (*glx_swap_interval_ext)(Display*, Window, int); +static void (*glx_swap_interval_mesa)(int); +static void (*glx_swap_interval_sgi)(int); +#endif + int main(int argc, char **argv) { @@ -72,6 +93,63 @@ int init(void) int i, j, xor, r, g, b; unsigned int *ptr; float *vptr; + const char *extstr; + + extstr = (char*)glGetString(GL_EXTENSIONS); + + if(strstr(extstr, "ARB_vertex_buffer_object")) { + if(!(gl_gen_buffers = get_proc_addr("glGenBuffers"))) { + gl_gen_buffers = get_proc_addr("glGenBuffersARB"); + } + if(!(gl_bind_buffer = get_proc_addr("glBindBuffer"))) { + gl_bind_buffer = get_proc_addr("glBindBufferARB"); + } + if(!(gl_buffer_data = get_proc_addr("glBufferData"))) { + gl_buffer_data = get_proc_addr("glBufferDataARB"); + } + if(!(gl_buffer_sub_data = get_proc_addr("glBufferSubData"))) { + gl_buffer_sub_data = get_proc_addr("glBufferSubDataARB"); + } + if(gl_gen_buffers && gl_bind_buffer && gl_buffer_data && gl_buffer_sub_data) { + have_vbo = 1; + } + } + if(have_vbo) { + printf("Using vertex buffer objects for the GL_POINTS test\n"); + } else { + printf("No VBO extension, using client-side vertex arrays for the GL_POINTS test\n"); + } + +#ifdef BUILD_X11 + { + int scr; + XWindowAttributes wattr; + + dpy = glXGetCurrentDisplay(); + win = glXGetCurrentDrawable(); + + XGetWindowAttributes(dpy, win, &wattr); + scr = XScreenNumberOfScreen(wattr.screen); + + if(!(extstr = glXQueryExtensionsString(dpy, scr))) { + return -1; + } + } + + if(strstr(extstr, "GLX_EXT_swap_control") && (glx_swap_interval_ext = get_proc_addr("glXSwapIntervalEXT"))) { + printf("using GLX_EXT_swap_control to disable vsync\n"); + glx_swap_interval_ext(dpy, win, 0); + } else if(strstr(extstr, "GLX_MESA_swap_control") && (glx_swap_interval_mesa = get_proc_addr("glXSwapIntervalMESA"))) { + printf("using GLX_MESA_swap_control to disable vsync\n"); + glx_swap_interval_mesa(0); + } else if(strstr(extstr, "GLX_SGI_swap_control") && (glx_swap_interval_sgi = get_proc_addr("glXSwapIntervalSGI"))) { + printf("using GLX_SGI_swap_control to disable vsync\n"); + glx_swap_interval_sgi(0); + } else { + fprintf(stderr, "No vsync extension found, you might need to disable vsync externally\n"); + } +#endif + ptr = img; for(i=0; i 5000) { + if(interv >= 4000) { unsigned int fps = 100000 * num_frames / interv; printf("%s: %.2f fps\n", modestr[mode], fps / 100.0f); num_frames = 0; -- 1.7.10.4