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