vinix-ports / tests / x11 / glx.v
70 lines · 66 sloc · 2.46 KB · 4ee175cd6737df8c5cae22a38bd9321911a90083
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 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