// Exercise the current Gallium runtime without an X server or hardware GPU. module main #include #include #include #flag -lEGL #flag -lGLESv2 fn C.eglGetPlatformDisplay(u32, voidptr, voidptr) voidptr fn C.eglInitialize(voidptr, &i32, &i32) u32 fn C.eglBindAPI(u32) u32 fn C.eglChooseConfig(voidptr, &i32, &voidptr, i32, &i32) u32 fn C.eglCreatePbufferSurface(voidptr, voidptr, &i32) voidptr fn C.eglCreateContext(voidptr, voidptr, voidptr, &i32) voidptr fn C.eglMakeCurrent(voidptr, voidptr, voidptr, voidptr) u32 fn C.eglDestroyContext(voidptr, voidptr) u32 fn C.eglDestroySurface(voidptr, voidptr) u32 fn C.eglTerminate(voidptr) u32 fn C.eglGetError() i32 fn C.glClearColor(f32, f32, f32, f32) fn C.glClear(u32) fn C.glReadPixels(i32, i32, i32, i32, u32, u32, voidptr) fn C.glGetError() u32 fn C.glGetString(u32) &u8 fn render() ! { display := C.eglGetPlatformDisplay(C.EGL_PLATFORM_SURFACELESS_MESA, unsafe { nil }, unsafe { nil }) mut major := i32(0) mut minor := i32(0) if isnil(display) || C.eglInitialize(display, &major, &minor) == 0 { return error('surfaceless EGL initialization failed: 0x${C.eglGetError():x}') } defer { C.eglTerminate(display) } mut config := voidptr(0) mut count := i32(0) // EGLint entries are 32-bit even when the V compiler uses a wider int. config_attributes := [i32(C.EGL_SURFACE_TYPE), C.EGL_PBUFFER_BIT, C.EGL_RENDERABLE_TYPE, C.EGL_OPENGL_ES2_BIT, C.EGL_RED_SIZE, 8, C.EGL_GREEN_SIZE, 8, C.EGL_BLUE_SIZE, 8, C.EGL_ALPHA_SIZE, 8, C.EGL_NONE]! if C.eglBindAPI(C.EGL_OPENGL_ES_API) == 0 || C.eglChooseConfig(display, &config_attributes[0], &config, 1, &count) == 0 || count != 1 { return error('could not choose an RGBA GLES2 pbuffer configuration: 0x${C.eglGetError():x}') } size := [i32(C.EGL_WIDTH), 1, C.EGL_HEIGHT, 1, C.EGL_NONE]! surface := C.eglCreatePbufferSurface(display, config, &size[0]) if isnil(surface) { return error('could not create an EGL pbuffer: 0x${C.eglGetError():x}') } defer { C.eglDestroySurface(display, surface) } attributes := [i32(C.EGL_CONTEXT_CLIENT_VERSION), 2, C.EGL_NONE]! context := C.eglCreateContext(display, config, unsafe { nil }, &attributes[0]) if isnil(context) { return error('could not create a GLES2 context: 0x${C.eglGetError():x}') } defer { C.eglDestroyContext(display, context) } if C.eglMakeCurrent(display, surface, surface, context) == 0 { return error('could not bind the GLES2 context: 0x${C.eglGetError():x}') } defer { C.eglMakeCurrent(display, unsafe { nil }, unsafe { nil }, unsafe { nil }) } renderer := C.glGetString(C.GL_RENDERER) if isnil(renderer) { return error('missing OpenGL renderer name') } C.glClearColor(0, 1, 0, 1) C.glClear(C.GL_COLOR_BUFFER_BIT) mut pixel := [4]u8{} C.glReadPixels(0, 0, 1, 1, C.GL_RGBA, C.GL_UNSIGNED_BYTE, &pixel[0]) if C.glGetError() != C.GL_NO_ERROR || pixel != [u8(0), 255, 0, 255]! { return error('GLES2 readback did not match the clear color: ${pixel}') } println('EGL ${major}.${minor}, renderer ${unsafe { tos_clone(renderer) }}, RGBA ${pixel}.') } fn main() { render() or { eprintln(err) exit(1) } }