Skip to content

Instantly share code, notes, and snippets.

@peko
Created January 27, 2017 15:55
Show Gist options
  • Select an option

  • Save peko/635ebcadc75081a8fe3bea9b0bdee734 to your computer and use it in GitHub Desktop.

Select an option

Save peko/635ebcadc75081a8fe3bea9b0bdee734 to your computer and use it in GitHub Desktop.
EGL sample
cmake_minimum_required(VERSION 3.6)
project(egl_a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(egl_a ${SOURCE_FILES})
target_link_libraries(egl_a GL EGL)
#include <EGL/egl.h>
#include <GLES3/gl31.h>
#include <assert.h>
#include <stdio.h>
/* a dummy compute shader that does nothing */
#define COMPUTE_SHADER_SRC " \
#version 310 es\n \
\
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in; \
\
void main(void) { \
/* awesome compute code here */ \
} \
"
static const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_DEPTH_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE
};
static const int pbufferWidth = 9;
static const int pbufferHeight = 9;
static const EGLint pbufferAttribs[] = {
EGL_WIDTH, pbufferWidth,
EGL_HEIGHT, pbufferHeight,
EGL_NONE,
};
int main(int argc, char *argv[]) {
// 1. Initialize EGL
EGLDisplay eglDpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint major, minor;
eglInitialize(eglDpy, &major, &minor);
// 2. Select an appropriate configuration
EGLint numConfigs;
EGLConfig eglCfg;
eglChooseConfig(eglDpy, configAttribs, &eglCfg, 1, &numConfigs);
// 3. Create a surface
EGLSurface eglSurf = eglCreatePbufferSurface(eglDpy, eglCfg,
pbufferAttribs);
// 4. Bind the API
eglBindAPI(EGL_OPENGL_API);
// 5. Create a context and make it current
EGLContext eglCtx = eglCreateContext(eglDpy, eglCfg, EGL_NO_CONTEXT,
NULL);
eglMakeCurrent(eglDpy, eglSurf, eglSurf, eglCtx);
// from now on use your OpenGL context
{
/* setup a compute shader */
GLuint compute_shader = glCreateShader(GL_COMPUTE_SHADER);
assert (glGetError() == GL_NO_ERROR);
const char *shader_source = COMPUTE_SHADER_SRC;
glShaderSource(compute_shader, 1, &shader_source, NULL);
assert (glGetError() == GL_NO_ERROR);
glCompileShader(compute_shader);
assert (glGetError() == GL_NO_ERROR);
GLuint shader_program = glCreateProgram();
glAttachShader(shader_program, compute_shader);
assert (glGetError() == GL_NO_ERROR);
glLinkProgram(shader_program);
assert (glGetError() == GL_NO_ERROR);
glDeleteShader(compute_shader);
glUseProgram(shader_program);
assert (glGetError() == GL_NO_ERROR);
/* dispatch computation */
glDispatchCompute(1, 1, 1);
assert (glGetError() == GL_NO_ERROR);
printf("Compute shader dispatched and finished successfully\n");
/* free stuff */
glDeleteProgram(shader_program);
}
// 6. Terminate EGL when finished
eglTerminate(eglDpy);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment