vinix-ports / tests / x11 / egl.v
80 lines · 76 sloc · 3.07 KB · 66d5abf95c8d6b5b55160e281857033d694c6b3b
Raw
1// Exercise the current Gallium runtime without an X server or hardware GPU.
2module main
3
4#include <EGL/egl.h>
5#include <EGL/eglext.h>
6#include <GLES2/gl2.h>
7#flag -lEGL
8#flag -lGLESv2
9
10fn C.eglGetPlatformDisplay(u32, voidptr, voidptr) voidptr
11fn C.eglInitialize(voidptr, &i32, &i32) u32
12fn C.eglBindAPI(u32) u32
13fn C.eglChooseConfig(voidptr, &i32, &voidptr, i32, &i32) u32
14fn C.eglCreatePbufferSurface(voidptr, voidptr, &i32) voidptr
15fn C.eglCreateContext(voidptr, voidptr, voidptr, &i32) voidptr
16fn C.eglMakeCurrent(voidptr, voidptr, voidptr, voidptr) u32
17fn C.eglDestroyContext(voidptr, voidptr) u32
18fn C.eglDestroySurface(voidptr, voidptr) u32
19fn C.eglTerminate(voidptr) u32
20fn C.eglGetError() i32
21fn C.glClearColor(f32, f32, f32, f32)
22fn C.glClear(u32)
23fn C.glReadPixels(i32, i32, i32, i32, u32, u32, voidptr)
24fn C.glGetError() u32
25fn C.glGetString(u32) &u8
26
27fn render() ! {
28 display := C.eglGetPlatformDisplay(C.EGL_PLATFORM_SURFACELESS_MESA, unsafe { nil }, unsafe { nil })
29 mut major := i32(0)
30 mut minor := i32(0)
31 if isnil(display) || C.eglInitialize(display, &major, &minor) == 0 {
32 return error('surfaceless EGL initialization failed: 0x${C.eglGetError():x}')
33 }
34 defer { C.eglTerminate(display) }
35 mut config := voidptr(0)
36 mut count := i32(0)
37 // EGLint entries are 32-bit even when the V compiler uses a wider int.
38 config_attributes := [i32(C.EGL_SURFACE_TYPE), C.EGL_PBUFFER_BIT, C.EGL_RENDERABLE_TYPE,
39 C.EGL_OPENGL_ES2_BIT, C.EGL_RED_SIZE, 8, C.EGL_GREEN_SIZE, 8, C.EGL_BLUE_SIZE, 8,
40 C.EGL_ALPHA_SIZE, 8, C.EGL_NONE]!
41 if C.eglBindAPI(C.EGL_OPENGL_ES_API) == 0
42 || C.eglChooseConfig(display, &config_attributes[0], &config, 1, &count) == 0 || count != 1 {
43 return error('could not choose an RGBA GLES2 pbuffer configuration: 0x${C.eglGetError():x}')
44 }
45 size := [i32(C.EGL_WIDTH), 1, C.EGL_HEIGHT, 1, C.EGL_NONE]!
46 surface := C.eglCreatePbufferSurface(display, config, &size[0])
47 if isnil(surface) {
48 return error('could not create an EGL pbuffer: 0x${C.eglGetError():x}')
49 }
50 defer { C.eglDestroySurface(display, surface) }
51 attributes := [i32(C.EGL_CONTEXT_CLIENT_VERSION), 2, C.EGL_NONE]!
52 context := C.eglCreateContext(display, config, unsafe { nil }, &attributes[0])
53 if isnil(context) {
54 return error('could not create a GLES2 context: 0x${C.eglGetError():x}')
55 }
56 defer { C.eglDestroyContext(display, context) }
57 if C.eglMakeCurrent(display, surface, surface, context) == 0 {
58 return error('could not bind the GLES2 context: 0x${C.eglGetError():x}')
59 }
60 defer { C.eglMakeCurrent(display, unsafe { nil }, unsafe { nil }, unsafe { nil }) }
61 renderer := C.glGetString(C.GL_RENDERER)
62 if isnil(renderer) {
63 return error('missing OpenGL renderer name')
64 }
65 C.glClearColor(0, 1, 0, 1)
66 C.glClear(C.GL_COLOR_BUFFER_BIT)
67 mut pixel := [4]u8{}
68 C.glReadPixels(0, 0, 1, 1, C.GL_RGBA, C.GL_UNSIGNED_BYTE, &pixel[0])
69 if C.glGetError() != C.GL_NO_ERROR || pixel != [u8(0), 255, 0, 255]! {
70 return error('GLES2 readback did not match the clear color: ${pixel}')
71 }
72 println('EGL ${major}.${minor}, renderer ${unsafe { tos_clone(renderer) }}, RGBA ${pixel}.')
73}
74
75fn main() {
76 render() or {
77 eprintln(err)
78 exit(1)
79 }
80}
81