vinix-ports / tests / x11 / glx.v
65 lines · 61 sloc · 2.26 KB · 66d5abf95c8d6b5b55160e281857033d694c6b3b
Raw
1// Test the legacy GLX server and client together in the Vinix guest.
2module 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]
11struct C.XVisualInfo {
12 depth i32
13}
14
15fn C.XOpenDisplay(&char) voidptr
16fn C.XDefaultScreen(voidptr) i32
17fn C.XRootWindow(voidptr, i32) u64
18fn C.XCreatePixmap(voidptr, u64, u32, u32, u32) u64
19fn C.XFreePixmap(voidptr, u64) i32
20fn C.XFree(voidptr) i32
21fn C.XCloseDisplay(voidptr) i32
22fn C.glXChooseVisual(voidptr, i32, &i32) &C.XVisualInfo
23fn C.glXCreateGLXPixmap(voidptr, &C.XVisualInfo, u64) u64
24fn C.glXDestroyGLXPixmap(voidptr, u64)
25fn C.glXCreateContext(voidptr, &C.XVisualInfo, voidptr, i32) voidptr
26fn C.glXMakeCurrent(voidptr, u64, voidptr) i32
27fn C.glXIsDirect(voidptr, voidptr) i32
28fn C.glXDestroyContext(voidptr, voidptr)
29fn C.glClearColor(f32, f32, f32, f32)
30fn C.glClear(u32)
31fn C.glReadBuffer(u32)
32fn C.glReadPixels(i32, i32, i32, i32, u32, u32, voidptr)
33fn C.glGetError() u32
34
35fn main() {
36 display := C.XOpenDisplay(unsafe { nil })
37 assert display != unsafe { nil }
38 defer { C.XCloseDisplay(display) }
39 screen := C.XDefaultScreen(display)
40 mut attributes := [i32(C.GLX_RGBA), C.GLX_RED_SIZE, 8, C.GLX_GREEN_SIZE, 8, C.GLX_BLUE_SIZE,
41 8, 0]!
42 visual := C.glXChooseVisual(display, screen, &attributes[0])
43 assert visual != unsafe { nil }
44 defer { C.XFree(visual) }
45 pixmap := C.XCreatePixmap(display, C.XRootWindow(display, screen), 1, 1, u32(visual.depth))
46 assert pixmap != 0
47 defer { C.XFreePixmap(display, pixmap) }
48 drawable := C.glXCreateGLXPixmap(display, visual, pixmap)
49 assert drawable != 0
50 defer { C.glXDestroyGLXPixmap(display, drawable) }
51 context := C.glXCreateContext(display, visual, unsafe { nil }, C.False)
52 assert context != unsafe { nil }
53 defer { C.glXDestroyContext(display, context) }
54 assert C.glXIsDirect(display, context) == C.False
55 assert C.glXMakeCurrent(display, drawable, context) != C.False
56 defer { C.glXMakeCurrent(display, 0, unsafe { nil }) }
57 C.glClearColor(0, 1, 0, 1)
58 C.glClear(C.GL_COLOR_BUFFER_BIT)
59 C.glReadBuffer(C.GL_FRONT)
60 mut pixel := [4]u8{}
61 C.glReadPixels(0, 0, 1, 1, C.GL_RGBA, C.GL_UNSIGNED_BYTE, &pixel[0])
62 assert C.glGetError() == C.GL_NO_ERROR
63 assert pixel[0] == 0 && pixel[1] == 255 && pixel[2] == 0
64 println('Indirect GLX rendered a green pixel in Xvfb.')
65}
66