Skip to content

Instantly share code, notes, and snippets.

@yui0
Created October 6, 2017 14:26
Show Gist options
  • Save yui0/8542f40cfe212429c78b9f796572dc56 to your computer and use it in GitHub Desktop.
Save yui0/8542f40cfe212429c78b9f796572dc56 to your computer and use it in GitHub Desktop.
OpenGLES2 on Linux console
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <assert.h>
#include <fcntl.h>
#include <gbm.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int32_t __fd;
struct gbm_device *__gbm;
EGLDisplay __egl_dpy;
EGLContext __core_ctx;
void coInit()
{
bool res;
int32_t __fd = open("/dev/dri/renderD128", O_RDWR);
assert(__fd > 0);
__gbm = gbm_create_device(__fd);
assert(__gbm != NULL);
/* setup EGL from the GBM device */
__egl_dpy = eglGetPlatformDisplay(EGL_PLATFORM_GBM_MESA, __gbm, NULL);
assert(__egl_dpy != NULL);
res = eglInitialize(__egl_dpy, NULL, NULL);
assert(res);
const char *egl_extension_st = eglQueryString(__egl_dpy, EGL_EXTENSIONS);
assert(strstr(egl_extension_st, "EGL_KHR_create_context") != NULL);
assert(strstr(egl_extension_st, "EGL_KHR_surfaceless_context") != NULL);
static const EGLint config_attribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLConfig cfg;
EGLint count;
res = eglChooseConfig(__egl_dpy, config_attribs, &cfg, 1, &count);
assert(res);
res = eglBindAPI(EGL_OPENGL_ES_API);
assert(res);
static const EGLint attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
__core_ctx = eglCreateContext(__egl_dpy, cfg, EGL_NO_CONTEXT, attribs);
assert(__core_ctx != EGL_NO_CONTEXT);
res = eglMakeCurrent(__egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, __core_ctx);
assert(res);
}
void coTerm()
{
eglDestroyContext(__egl_dpy, __core_ctx);
eglTerminate(__egl_dpy);
gbm_device_destroy(__gbm);
close(__fd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment