vinix-ports / tests / x11 / egl.v
79 lines · 75 sloc · 2.98 KB · 46e272bf1903924a39919a8bb534e91ffccca664
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, &int, &int) u32
12fn C.eglBindAPI(u32) u32
13fn C.eglChooseConfig(voidptr, &int, &voidptr, int, &int) u32
14fn C.eglCreatePbufferSurface(voidptr, voidptr, &int) voidptr
15fn C.eglCreateContext(voidptr, voidptr, voidptr, &int) 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() int
21fn C.glClearColor(f32, f32, f32, f32)
22fn C.glClear(u32)
23fn C.glReadPixels(int, int, int, int, 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 := 0
30 mut minor := 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 := 0
37 config_attributes := [int(C.EGL_SURFACE_TYPE), C.EGL_PBUFFER_BIT, C.EGL_RENDERABLE_TYPE,
38 C.EGL_OPENGL_ES2_BIT, C.EGL_RED_SIZE, 8, C.EGL_GREEN_SIZE, 8, C.EGL_BLUE_SIZE, 8,
39 C.EGL_ALPHA_SIZE, 8, C.EGL_NONE]!
40 if C.eglBindAPI(C.EGL_OPENGL_ES_API) == 0
41 || C.eglChooseConfig(display, &config_attributes[0], &config, 1, &count) == 0 || count != 1 {
42 return error('could not choose an RGBA GLES2 pbuffer configuration: 0x${C.eglGetError():x}')
43 }
44 size := [int(C.EGL_WIDTH), 1, C.EGL_HEIGHT, 1, C.EGL_NONE]!
45 surface := C.eglCreatePbufferSurface(display, config, &size[0])
46 if isnil(surface) {
47 return error('could not create an EGL pbuffer: 0x${C.eglGetError():x}')
48 }
49 defer { C.eglDestroySurface(display, surface) }
50 attributes := [int(C.EGL_CONTEXT_CLIENT_VERSION), 2, C.EGL_NONE]!
51 context := C.eglCreateContext(display, config, unsafe { nil }, &attributes[0])
52 if isnil(context) {
53 return error('could not create a GLES2 context: 0x${C.eglGetError():x}')
54 }
55 defer { C.eglDestroyContext(display, context) }
56 if C.eglMakeCurrent(display, surface, surface, context) == 0 {
57 return error('could not bind the GLES2 context: 0x${C.eglGetError():x}')
58 }
59 defer { C.eglMakeCurrent(display, unsafe { nil }, unsafe { nil }, unsafe { nil }) }
60 renderer := C.glGetString(C.GL_RENDERER)
61 if isnil(renderer) {
62 return error('missing OpenGL renderer name')
63 }
64 C.glClearColor(0, 1, 0, 1)
65 C.glClear(C.GL_COLOR_BUFFER_BIT)
66 mut pixel := [4]u8{}
67 C.glReadPixels(0, 0, 1, 1, C.GL_RGBA, C.GL_UNSIGNED_BYTE, &pixel[0])
68 if C.glGetError() != C.GL_NO_ERROR || pixel != [u8(0), 255, 0, 255]! {
69 return error('GLES2 readback did not match the clear color: ${pixel}')
70 }
71 println('EGL ${major}.${minor}, renderer ${unsafe { tos_clone(renderer) }}, RGBA ${pixel}.')
72}
73
74fn main() {
75 render() or {
76 eprintln(err)
77 exit(1)
78 }
79}
80