Skip to content

Instantly share code, notes, and snippets.

@yshui
Last active July 4, 2023 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yshui/596721d87cfd1bc263cd3c8ff669e420 to your computer and use it in GitHub Desktop.
Save yshui/596721d87cfd1bc263cd3c8ff669e420 to your computer and use it in GitHub Desktop.
nvidia glXWaitVideoSync test
// Build with: gcc wait_video_sync.c -lGLX -lX11 -lGLU -lGL
// Source: https://www.khronos.org/opengl/wiki/Programming_OpenGL_in_Linux:_GLX_and_Xlib
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <GL/glxext.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Display *dpy;
Window root;
GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;
PFNGLXWAITVIDEOSYNCSGIPROC glXWaitVideoSyncSGI = NULL;
void DrawAQuad() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1., 1., -1., 1., 1., 20.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);
glBegin(GL_QUADS);
glColor3f(1., 0., 0.);
glVertex3f(-.75, -.75, 0.);
glColor3f(0., 1., 0.);
glVertex3f(.75, -.75, 0.);
glColor3f(0., 0., 1.);
glVertex3f(.75, .75, 0.);
glColor3f(1., 1., 0.);
glVertex3f(-.75, .75, 0.);
glEnd();
}
int main(int argc, char *argv[]) {
dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
printf("\n\tcannot connect to X server\n\n");
exit(0);
}
root = DefaultRootWindow(dpy);
vi = glXChooseVisual(dpy, 0, att);
if (vi == NULL) {
printf("\n\tno appropriate visual found\n\n");
exit(0);
} else {
printf("\n\tvisual %p selected\n",
(void *)vi
->visualid); /* %p creates hexadecimal output like in glxinfo */
}
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask;
win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput,
vi->visual, CWColormap | CWEventMask, &swa);
XMapWindow(dpy, win);
XStoreName(dpy, win, "VERY SIMPLE APPLICATION");
const char *ext = glXQueryExtensionsString(dpy, 0);
if (!strstr(ext, "GLX_SGI_video_sync")) {
printf("GLX_SGI_video_sync extension missing\n");
exit(1);
}
glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, glc);
glXWaitVideoSyncSGI = (PFNGLXWAITVIDEOSYNCSGIPROC)glXGetProcAddress(
(const GLubyte *)"glXWaitVideoSyncSGI");
glEnable(GL_DEPTH_TEST);
while (1) {
while (XPending(dpy) > 0) {
XNextEvent(dpy, &xev);
if (xev.type == Expose) {
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
}
else if (xev.type == KeyPress && xev.xkey.keycode == 0x09) { // ESC
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
exit(0);
}
}
DrawAQuad();
glXSwapBuffers(dpy, win);
unsigned int count = 0;
glXWaitVideoSyncSGI(1, 0, &count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment