| 1 | // Test the legacy GLX server and client together in the Vinix guest. |
| 2 | module main |
| 3 | |
| 4 | #include <X11/Xlib.h> |
| 5 | #include <X11/Xutil.h> |
| 6 | #include <GL/glx.h> |
| 7 | #flag -lGL |
| 8 | #flag -lX11 |
| 9 | |
| 10 | @[typedef] |
| 11 | struct C.XVisualInfo { |
| 12 | depth i32 |
| 13 | } |
| 14 | |
| 15 | fn C.XOpenDisplay(&char) voidptr |
| 16 | fn C.XDefaultScreen(voidptr) i32 |
| 17 | fn C.XRootWindow(voidptr, i32) u64 |
| 18 | fn C.XCreatePixmap(voidptr, u64, u32, u32, u32) u64 |
| 19 | fn C.XFreePixmap(voidptr, u64) i32 |
| 20 | fn C.XFree(voidptr) i32 |
| 21 | fn C.XCloseDisplay(voidptr) i32 |
| 22 | fn C.glXChooseVisual(voidptr, i32, &i32) &C.XVisualInfo |
| 23 | fn C.glXCreateGLXPixmap(voidptr, &C.XVisualInfo, u64) u64 |
| 24 | fn C.glXDestroyGLXPixmap(voidptr, u64) |
| 25 | fn C.glXCreateContext(voidptr, &C.XVisualInfo, voidptr, i32) voidptr |
| 26 | fn C.glXMakeCurrent(voidptr, u64, voidptr) i32 |
| 27 | fn C.glXIsDirect(voidptr, voidptr) i32 |
| 28 | fn C.glXDestroyContext(voidptr, voidptr) |
| 29 | fn C.glClearColor(f32, f32, f32, f32) |
| 30 | fn C.glClear(u32) |
| 31 | fn C.glReadBuffer(u32) |
| 32 | fn C.glReadPixels(i32, i32, i32, i32, u32, u32, voidptr) |
| 33 | fn C.glGetError() u32 |
| 34 | |
| 35 | fn main() { |
| 36 | eprintln('GLX probe: open display') |
| 37 | display := C.XOpenDisplay(unsafe { nil }) |
| 38 | assert display != unsafe { nil } |
| 39 | defer { C.XCloseDisplay(display) } |
| 40 | screen := C.XDefaultScreen(display) |
| 41 | mut attributes := [i32(C.GLX_RGBA), C.GLX_RED_SIZE, 8, C.GLX_GREEN_SIZE, 8, C.GLX_BLUE_SIZE, |
| 42 | 8, 0]! |
| 43 | eprintln('GLX probe: select a visual') |
| 44 | visual := C.glXChooseVisual(display, screen, &attributes[0]) |
| 45 | assert visual != unsafe { nil } |
| 46 | defer { C.XFree(visual) } |
| 47 | pixmap := C.XCreatePixmap(display, C.XRootWindow(display, screen), 1, 1, u32(visual.depth)) |
| 48 | assert pixmap != 0 |
| 49 | defer { C.XFreePixmap(display, pixmap) } |
| 50 | drawable := C.glXCreateGLXPixmap(display, visual, pixmap) |
| 51 | assert drawable != 0 |
| 52 | defer { C.glXDestroyGLXPixmap(display, drawable) } |
| 53 | eprintln('GLX probe: create indirect context') |
| 54 | context := C.glXCreateContext(display, visual, unsafe { nil }, C.False) |
| 55 | assert context != unsafe { nil } |
| 56 | defer { C.glXDestroyContext(display, context) } |
| 57 | assert C.glXIsDirect(display, context) == C.False |
| 58 | eprintln('GLX probe: bind indirect context') |
| 59 | assert C.glXMakeCurrent(display, drawable, context) != C.False |
| 60 | defer { C.glXMakeCurrent(display, 0, unsafe { nil }) } |
| 61 | C.glClearColor(0, 1, 0, 1) |
| 62 | C.glClear(C.GL_COLOR_BUFFER_BIT) |
| 63 | C.glReadBuffer(C.GL_FRONT) |
| 64 | mut pixel := [4]u8{} |
| 65 | eprintln('GLX probe: read pixels') |
| 66 | C.glReadPixels(0, 0, 1, 1, C.GL_RGBA, C.GL_UNSIGNED_BYTE, &pixel[0]) |
| 67 | assert C.glGetError() == C.GL_NO_ERROR |
| 68 | assert pixel[0] == 0 && pixel[1] == 255 && pixel[2] == 0 |
| 69 | println('Indirect GLX rendered a green pixel in Xvfb.') |
| 70 | } |
| 71 | |