From b18209c904f421f3065d610718d7d9e9b8b9211f Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 23 Jun 2017 11:38:45 -0400 Subject: Switch to GLES2 Closes #13 --- backend/egl.c | 2 +- example/pointer.c | 6 +- example/rotation.c | 6 +- example/simple.c | 2 +- example/tablet.c | 6 +- example/touch.c | 6 +- include/render/gles2.h | 29 +++++++ include/render/gles3.h | 29 ------- include/wlr/render/gles2.h | 7 ++ include/wlr/render/gles3.h | 7 -- render/CMakeLists.txt | 8 +- render/gles2/renderer.c | 183 +++++++++++++++++++++++++++++++++++++++++ render/gles2/shaders.c | 63 +++++++++++++++ render/gles2/surface.c | 63 +++++++++++++++ render/gles2/util.c | 38 +++++++++ render/gles3/renderer.c | 197 --------------------------------------------- render/gles3/shaders.c | 63 --------------- render/gles3/surface.c | 63 --------------- render/gles3/util.c | 38 --------- 19 files changed, 401 insertions(+), 415 deletions(-) create mode 100644 include/render/gles2.h delete mode 100644 include/render/gles3.h create mode 100644 include/wlr/render/gles2.h delete mode 100644 include/wlr/render/gles3.h create mode 100644 render/gles2/renderer.c create mode 100644 render/gles2/shaders.c create mode 100644 render/gles2/surface.c create mode 100644 render/gles2/util.c delete mode 100644 render/gles3/renderer.c delete mode 100644 render/gles3/shaders.c delete mode 100644 render/gles3/surface.c delete mode 100644 render/gles3/util.c diff --git a/backend/egl.c b/backend/egl.c index 66aa6a4e..8bb05f6f 100644 --- a/backend/egl.c +++ b/backend/egl.c @@ -136,7 +136,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display) { goto error; } - static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE}; + static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; egl->context = eglCreateContext(egl->display, egl->config, EGL_NO_CONTEXT, attribs); diff --git a/example/pointer.c b/example/pointer.c index 035af240..8dba5454 100644 --- a/example/pointer.c +++ b/example/pointer.c @@ -9,9 +9,9 @@ #include #include #include -#include +#include #include -#include +#include #include #include #include @@ -121,7 +121,7 @@ int main(int argc, char *argv[]) { compositor.pointer_axis_cb = handle_pointer_axis; compositor_init(&compositor); - state.renderer = wlr_gles3_renderer_init(); + state.renderer = wlr_gles2_renderer_init(); state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, cat_tex.width, cat_tex.height, cat_tex.pixel_data); diff --git a/example/rotation.c b/example/rotation.c index 7eed1c2e..b8c43d4c 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -9,9 +9,9 @@ #include #include #include -#include +#include #include -#include +#include #include #include #include @@ -202,7 +202,7 @@ int main(int argc, char *argv[]) { compositor.keyboard_key_cb = handle_keyboard_key; compositor_init(&compositor); - state.renderer = wlr_gles3_renderer_init(); + state.renderer = wlr_gles2_renderer_init(); state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, cat_tex.width, cat_tex.height, cat_tex.pixel_data); diff --git a/example/simple.c b/example/simple.c index 8a01fab8..4bbdf399 100644 --- a/example/simple.c +++ b/example/simple.c @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/example/tablet.c b/example/tablet.c index 69c530a6..0343bd0a 100644 --- a/example/tablet.c +++ b/example/tablet.c @@ -8,9 +8,9 @@ #include #include #include -#include +#include #include -#include +#include #include #include #include @@ -150,7 +150,7 @@ int main(int argc, char *argv[]) { }; compositor_init(&compositor); - state.renderer = wlr_gles3_renderer_init(); + state.renderer = wlr_gles2_renderer_init(); compositor_run(&compositor); diff --git a/example/touch.c b/example/touch.c index 4b4782f1..29380ed4 100644 --- a/example/touch.c +++ b/example/touch.c @@ -10,9 +10,9 @@ #include #include #include -#include +#include #include -#include +#include #include #include #include @@ -101,7 +101,7 @@ int main(int argc, char *argv[]) { }; compositor_init(&compositor); - state.renderer = wlr_gles3_renderer_init(); + state.renderer = wlr_gles2_renderer_init(); state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, cat_tex.width, cat_tex.height, cat_tex.pixel_data); diff --git a/include/render/gles2.h b/include/render/gles2.h new file mode 100644 index 00000000..bd0106b3 --- /dev/null +++ b/include/render/gles2.h @@ -0,0 +1,29 @@ +#ifndef _WLR_RENDER_GLES2_INTERNAL_H +#define _WLR_RENDER_GLES2_INTERNAL_H +#include +#include +#include +#include +#include + +struct wlr_surface_state { + struct wlr_surface *wlr_surface; + GLuint tex_id; +}; + +struct wlr_surface *gles2_surface_init(); + +extern const GLchar quad_vertex_src[]; +extern const GLchar quad_fragment_src[]; +extern const GLchar ellipse_fragment_src[]; +extern const GLchar vertex_src[]; +extern const GLchar fragment_src_RGB[]; +extern const GLchar fragment_src_RGBA[]; + +bool _gles2_flush_errors(const char *file, int line); +#define gles2_flush_errors(...) \ + _gles2_flush_errors(__FILE__ + strlen(WLR_SRC_DIR) + 1, __LINE__) + +#define GL_CALL(func) func; gles2_flush_errors() + +#endif diff --git a/include/render/gles3.h b/include/render/gles3.h deleted file mode 100644 index 9acc1088..00000000 --- a/include/render/gles3.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef _WLR_RENDER_GLES2_INTERNAL_H -#define _WLR_RENDER_GLES2_INTERNAL_H -#include -#include -#include -#include -#include - -struct wlr_surface_state { - struct wlr_surface *wlr_surface; - GLuint tex_id; -}; - -struct wlr_surface *gles3_surface_init(); - -extern const GLchar quad_vertex_src[]; -extern const GLchar quad_fragment_src[]; -extern const GLchar ellipse_fragment_src[]; -extern const GLchar vertex_src[]; -extern const GLchar fragment_src_RGB[]; -extern const GLchar fragment_src_RGBA[]; - -bool _gles3_flush_errors(const char *file, int line); -#define gles3_flush_errors(...) \ - _gles3_flush_errors(__FILE__ + strlen(WLR_SRC_DIR) + 1, __LINE__) - -#define GL_CALL(func) func; gles3_flush_errors() - -#endif diff --git a/include/wlr/render/gles2.h b/include/wlr/render/gles2.h new file mode 100644 index 00000000..a779ffff --- /dev/null +++ b/include/wlr/render/gles2.h @@ -0,0 +1,7 @@ +#ifndef _WLR_GLES2_RENDERER_H +#define _WLR_GLES2_RENDERER_H +#include + +struct wlr_renderer *wlr_gles2_renderer_init(); + +#endif diff --git a/include/wlr/render/gles3.h b/include/wlr/render/gles3.h deleted file mode 100644 index 7b60bb97..00000000 --- a/include/wlr/render/gles3.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef _WLR_GLES3_RENDERER_H -#define _WLR_GLES3_RENDERER_H -#include - -struct wlr_renderer *wlr_gles3_renderer_init(); - -#endif diff --git a/render/CMakeLists.txt b/render/CMakeLists.txt index 7b863d64..70ebced2 100644 --- a/render/CMakeLists.txt +++ b/render/CMakeLists.txt @@ -2,8 +2,8 @@ add_library(wlr-render STATIC matrix.c wlr_renderer.c wlr_surface.c - gles3/shaders.c - gles3/renderer.c - gles3/surface.c - gles3/util.c + gles2/shaders.c + gles2/renderer.c + gles2/surface.c + gles2/util.c ) diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c new file mode 100644 index 00000000..07647f2f --- /dev/null +++ b/render/gles2/renderer.c @@ -0,0 +1,183 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "render/gles2.h" + +static struct { + bool initialized; + GLuint rgb, rgba; + GLuint quad; + GLuint ellipse; +} shaders; + +static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) { + *shader = GL_CALL(glCreateShader(type)); + int len = strlen(src); + GL_CALL(glShaderSource(*shader, 1, &src, &len)); + GL_CALL(glCompileShader(*shader)); + GLint success; + GL_CALL(glGetShaderiv(*shader, GL_COMPILE_STATUS, &success)); + if (success == GL_FALSE) { + GLint loglen; + GL_CALL(glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &loglen)); + GLchar msg[loglen]; + GL_CALL(glGetShaderInfoLog(*shader, loglen, &loglen, msg)); + wlr_log(L_ERROR, "Shader compilation failed"); + wlr_log(L_ERROR, "%s", msg); + exit(1); + return false; + } + return true; +} + +static bool compile_program(const GLchar *vert_src, + const GLchar *frag_src, GLuint *program) { + GLuint vertex, fragment; + if (!compile_shader(GL_VERTEX_SHADER, vert_src, &vertex)) { + return false; + } + if (!compile_shader(GL_FRAGMENT_SHADER, frag_src, &fragment)) { + glDeleteProgram(vertex); + return false; + } + *program = GL_CALL(glCreateProgram()); + GL_CALL(glAttachShader(*program, vertex)); + GL_CALL(glAttachShader(*program, fragment)); + GL_CALL(glLinkProgram(*program)); + return true; +} + +static void init_default_shaders() { + if (shaders.initialized) { + return; + } + if (!compile_program(vertex_src, fragment_src_RGB, &shaders.rgb)) { + goto error; + } + if (!compile_program(vertex_src, fragment_src_RGBA, &shaders.rgba)) { + goto error; + } + if (!compile_program(quad_vertex_src, quad_fragment_src, &shaders.quad)) { + goto error; + } + if (!compile_program(quad_vertex_src, ellipse_fragment_src, &shaders.ellipse)) { + goto error; + } + wlr_log(L_DEBUG, "Compiled default shaders"); + return; +error: + wlr_log(L_ERROR, "Failed to set up default shaders!"); +} + +static void init_globals() { + init_default_shaders(); +} + +static void wlr_gles2_begin(struct wlr_renderer_state *state, + struct wlr_output *output) { + // TODO: let users customize the clear color? + GL_CALL(glClearColor(0.25f, 0.25f, 0.25f, 1)); + GL_CALL(glClear(GL_COLOR_BUFFER_BIT)); + int32_t width = output->width; + int32_t height = output->height; + GL_CALL(glViewport(0, 0, width, height)); + // Note: maybe we should save output projection and remove some of the need + // for users to sling matricies themselves +} + +static void wlr_gles2_end(struct wlr_renderer_state *state) { + // no-op +} + +static struct wlr_surface *wlr_gles2_surface_init(struct wlr_renderer_state *state) { + return gles2_surface_init(); +} + +static void draw_quad() { + GLfloat verts[] = { + 1, 0, // top right + 0, 0, // top left + 1, 1, // bottom right + 0, 1, // bottom left + }; + GLfloat texcoord[] = { + 1, 0, // top right + 0, 0, // top left + 1, 1, // bottom right + 0, 1, // bottom left + }; + + GL_CALL(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts)); + GL_CALL(glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord)); + + GL_CALL(glEnableVertexAttribArray(0)); + GL_CALL(glEnableVertexAttribArray(1)); + + GL_CALL(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); + + GL_CALL(glDisableVertexAttribArray(0)); + GL_CALL(glDisableVertexAttribArray(1)); +} + +static bool wlr_gles2_render_surface(struct wlr_renderer_state *state, + struct wlr_surface *surface, const float (*matrix)[16]) { + assert(surface && surface->valid); + switch (surface->format) { + case GL_RGB: + GL_CALL(glUseProgram(shaders.rgb)); + break; + case GL_RGBA: + GL_CALL(glUseProgram(shaders.rgba)); + break; + default: + wlr_log(L_ERROR, "No shader for this surface format"); + return false; + } + gles2_flush_errors(); + wlr_surface_bind(surface); + GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); + draw_quad(); + return true; +} + +static void wlr_gles2_render_quad(struct wlr_renderer_state *state, + const float (*color)[4], const float (*matrix)[16]) { + GL_CALL(glUseProgram(shaders.quad)); + GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); + GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3])); + draw_quad(); +} + +static void wlr_gles2_render_ellipse(struct wlr_renderer_state *state, + const float (*color)[4], const float (*matrix)[16]) { + GL_CALL(glUseProgram(shaders.ellipse)); + GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); + GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3])); + draw_quad(); +} + +static void wlr_gles2_destroy(struct wlr_renderer_state *state) { + // no-op +} + +static struct wlr_renderer_impl wlr_renderer_impl = { + .begin = wlr_gles2_begin, + .end = wlr_gles2_end, + .surface_init = wlr_gles2_surface_init, + .render_with_matrix = wlr_gles2_render_surface, + .render_quad = wlr_gles2_render_quad, + .render_ellipse = wlr_gles2_render_ellipse, + .destroy = wlr_gles2_destroy +}; + +struct wlr_renderer *wlr_gles2_renderer_init() { + init_globals(); + return wlr_renderer_init(NULL, &wlr_renderer_impl); +} diff --git a/render/gles2/shaders.c b/render/gles2/shaders.c new file mode 100644 index 00000000..850c0618 --- /dev/null +++ b/render/gles2/shaders.c @@ -0,0 +1,63 @@ +#include "render/gles2.h" +#include + +// Colored quads +const GLchar quad_vertex_src[] = +"uniform mat4 proj;" +"uniform vec4 color;" +"attribute vec2 pos;" +"attribute vec2 texcoord;" +"varying vec4 v_color;" +"varying vec2 v_texcoord;" +"void main() {" +" gl_Position = proj * vec4(pos, 0.0, 1.0);" +" v_color = color;" +" v_texcoord = texcoord;" +"}"; + +const GLchar quad_fragment_src[] = +"precision mediump float;" +"varying vec4 v_color;" +"varying vec2 v_texcoord;" +"void main() {" +" gl_FragColor = v_color;" +"}"; + +// Colored ellipses (TODO) + +const GLchar ellipse_fragment_src[] = +"precision mediump float;" +"varying vec4 v_color;" +"varying vec2 v_texcoord;" +"void main() {" +" float l = length(v_texcoord - vec2(0.5, 0.5));" +" if (l > 0.5) discard;" +" gl_FragColor = v_color;" +"}"; + +// Textured quads +const GLchar vertex_src[] = +"uniform mat4 proj;" +"attribute vec2 pos;" +"attribute vec2 texcoord;" +"varying vec2 v_texcoord;" +"void main() {" +" gl_Position = proj * vec4(pos, 0.0, 1.0);" +" v_texcoord = texcoord;" +"}"; + +const GLchar fragment_src_RGB[] = +"precision mediump float;" +"varying vec2 v_texcoord;" +"uniform sampler2D tex;" +"void main() {" +" gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0);" +"}"; + +const GLchar fragment_src_RGBA[] = +"precision mediump float;" +"varying vec2 v_texcoord;" +"uniform sampler2D tex;" +"void main() {" +" gl_FragColor = texture2D(tex, v_texcoord);" +"}"; diff --git a/render/gles2/surface.c b/render/gles2/surface.c new file mode 100644 index 00000000..278b3dc3 --- /dev/null +++ b/render/gles2/surface.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "render/gles2.h" + +static bool gles2_surface_attach_pixels(struct wlr_surface_state *surface, + uint32_t format, int width, int height, const unsigned char *pixels) { + assert(surface); + surface->wlr_surface->width = width; + surface->wlr_surface->height = height; + surface->wlr_surface->format = format; + GL_CALL(glGenTextures(1, &surface->tex_id)); + GL_CALL(glBindTexture(GL_TEXTURE_2D, surface->tex_id)); + GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, + format, GL_UNSIGNED_BYTE, pixels)); + surface->wlr_surface->valid = true; + return true; +} + +static void gles2_surface_get_matrix(struct wlr_surface_state *surface, + float (*matrix)[16], const float (*projection)[16], int x, int y) { + struct wlr_surface *_surface = surface->wlr_surface; + float world[16]; + wlr_matrix_identity(matrix); + wlr_matrix_translate(&world, x, y, 0); + wlr_matrix_mul(matrix, &world, matrix); + wlr_matrix_scale(&world, _surface->width, _surface->height, 1); + wlr_matrix_mul(matrix, &world, matrix); + wlr_matrix_mul(projection, matrix, matrix); +} + +static void gles2_surface_bind(struct wlr_surface_state *surface) { + GL_CALL(glActiveTexture(GL_TEXTURE0 + 1)); + GL_CALL(glBindTexture(GL_TEXTURE_2D, surface->tex_id)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); +} + +static void gles2_surface_destroy(struct wlr_surface_state *surface) { + GL_CALL(glDeleteTextures(1, &surface->tex_id)); + free(surface); +} + +static struct wlr_surface_impl wlr_surface_impl = { + .attach_pixels = gles2_surface_attach_pixels, + // .attach_shm = TODO + .get_matrix = gles2_surface_get_matrix, + .bind = gles2_surface_bind, + .destroy = gles2_surface_destroy, +}; + +struct wlr_surface *gles2_surface_init() { + struct wlr_surface_state *state = calloc(sizeof(struct wlr_surface_state), 1); + struct wlr_surface *surface = wlr_surface_init(state, &wlr_surface_impl); + state->wlr_surface = surface; + return surface; +} diff --git a/render/gles2/util.c b/render/gles2/util.c new file mode 100644 index 00000000..9498a1cd --- /dev/null +++ b/render/gles2/util.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include "render/gles2.h" + +const char *gles2_strerror(GLenum err) { + switch (err) { + case GL_INVALID_ENUM: + return "Invalid enum"; + case GL_INVALID_VALUE: + return "Invalid value"; + case GL_INVALID_OPERATION: + return "Invalid operation"; + case GL_OUT_OF_MEMORY: + return "Out of memory"; + case GL_INVALID_FRAMEBUFFER_OPERATION: + return "Invalid framebuffer operation"; + default: + return "Unknown error"; + } +} + +bool _gles2_flush_errors(const char *file, int line) { + GLenum err; + bool failure = false; + while ((err = glGetError()) != GL_NO_ERROR) { + failure = true; + if (err == GL_OUT_OF_MEMORY) { + // The OpenGL context is now undefined + _wlr_log(L_ERROR, "[%s:%d] Fatal GL error: out of memory", file, line); + exit(1); + } else { + _wlr_log(L_ERROR, "[%s:%d] GL error %d %s", file, line, err, gles2_strerror(err)); + } + } + return failure; +} diff --git a/render/gles3/renderer.c b/render/gles3/renderer.c deleted file mode 100644 index 4270bd14..00000000 --- a/render/gles3/renderer.c +++ /dev/null @@ -1,197 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "render/gles3.h" - -static struct { - bool initialized; - GLuint rgb, rgba; - GLuint quad; - GLuint ellipse; -} shaders; -static GLuint vao, vbo, ebo; - -static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) { - *shader = GL_CALL(glCreateShader(type)); - int len = strlen(src); - GL_CALL(glShaderSource(*shader, 1, &src, &len)); - GL_CALL(glCompileShader(*shader)); - GLint success; - GL_CALL(glGetShaderiv(*shader, GL_COMPILE_STATUS, &success)); - if (success == GL_FALSE) { - GLint loglen; - GL_CALL(glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &loglen)); - GLchar msg[loglen]; - GL_CALL(glGetShaderInfoLog(*shader, loglen, &loglen, msg)); - wlr_log(L_ERROR, "Shader compilation failed"); - wlr_log(L_ERROR, "%s", msg); - exit(1); - return false; - } - return true; -} - -static bool compile_program(const GLchar *vert_src, - const GLchar *frag_src, GLuint *program) { - GLuint vertex, fragment; - if (!compile_shader(GL_VERTEX_SHADER, vert_src, &vertex)) { - return false; - } - if (!compile_shader(GL_FRAGMENT_SHADER, frag_src, &fragment)) { - glDeleteProgram(vertex); - return false; - } - *program = GL_CALL(glCreateProgram()); - GL_CALL(glAttachShader(*program, vertex)); - GL_CALL(glAttachShader(*program, fragment)); - GL_CALL(glLinkProgram(*program)); - return true; -} - -static void init_default_shaders() { - if (shaders.initialized) { - return; - } - if (!compile_program(vertex_src, fragment_src_RGB, &shaders.rgb)) { - goto error; - } - if (!compile_program(vertex_src, fragment_src_RGBA, &shaders.rgba)) { - goto error; - } - if (!compile_program(quad_vertex_src, quad_fragment_src, &shaders.quad)) { - goto error; - } - if (!compile_program(quad_vertex_src, ellipse_fragment_src, &shaders.ellipse)) { - goto error; - } - wlr_log(L_DEBUG, "Compiled default shaders"); - return; -error: - wlr_log(L_ERROR, "Failed to set up default shaders!"); -} - -static void init_default_quad() { - GLfloat verticies[] = { - 1, 1, 1, 1, // bottom right - 1, 0, 1, 0, // top right - 0, 0, 0, 0, // top left - 0, 1, 0, 1, // bottom left - }; - GLuint indicies[] = { - 0, 1, 3, - 1, 2, 3, - }; - - GL_CALL(glGenVertexArrays(1, &vao)); - GL_CALL(glGenBuffers(1, &vbo)); - - GL_CALL(glBindVertexArray(vao)); - GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo)); - - GL_CALL(glEnableVertexAttribArray(0)); - GL_CALL(glEnableVertexAttribArray(1)); - GL_CALL(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)0)); - GL_CALL(glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat)))); - GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW)); - - GL_CALL(glGenBuffers(1, &ebo)); - GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)); - GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW)); -} - -static void init_globals() { - init_default_shaders(); - init_default_quad(); -} - -static void wlr_gles3_begin(struct wlr_renderer_state *state, - struct wlr_output *output) { - // TODO: let users customize the clear color? - GL_CALL(glClearColor(0.25f, 0.25f, 0.25f, 1)); - GL_CALL(glClear(GL_COLOR_BUFFER_BIT)); - int32_t width = output->width; - int32_t height = output->height; - GL_CALL(glViewport(0, 0, width, height)); - // Note: maybe we should save output projection and remove some of the need - // for users to sling matricies themselves -} - -static void wlr_gles3_end(struct wlr_renderer_state *state) { - // no-op -} - -static struct wlr_surface *wlr_gles3_surface_init(struct wlr_renderer_state *state) { - return gles3_surface_init(); -} - -static bool wlr_gles3_render_surface(struct wlr_renderer_state *state, - struct wlr_surface *surface, const float (*matrix)[16]) { - assert(surface && surface->valid); - switch (surface->format) { - case GL_RGB: - GL_CALL(glUseProgram(shaders.rgb)); - break; - case GL_RGBA: - GL_CALL(glUseProgram(shaders.rgba)); - break; - default: - wlr_log(L_ERROR, "No shader for this surface format"); - return false; - } - gles3_flush_errors(); - GL_CALL(glBindVertexArray(vao)); - GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo)); - GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)); - wlr_surface_bind(surface); - GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); - GL_CALL(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)); - return true; -} - -static void wlr_gles3_render_quad(struct wlr_renderer_state *state, - const float (*color)[4], const float (*matrix)[16]) { - GL_CALL(glUseProgram(shaders.quad)); - GL_CALL(glBindVertexArray(vao)); - GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo)); - GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)); - GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); - GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3])); - GL_CALL(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)); -} - -static void wlr_gles3_render_ellipse(struct wlr_renderer_state *state, - const float (*color)[4], const float (*matrix)[16]) { - GL_CALL(glUseProgram(shaders.ellipse)); - GL_CALL(glBindVertexArray(vao)); - GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo)); - GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)); - GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); - GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3])); - GL_CALL(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)); -} - -static void wlr_gles3_destroy(struct wlr_renderer_state *state) { - // no-op -} - -static struct wlr_renderer_impl wlr_renderer_impl = { - .begin = wlr_gles3_begin, - .end = wlr_gles3_end, - .surface_init = wlr_gles3_surface_init, - .render_with_matrix = wlr_gles3_render_surface, - .render_quad = wlr_gles3_render_quad, - .render_ellipse = wlr_gles3_render_ellipse, - .destroy = wlr_gles3_destroy -}; - -struct wlr_renderer *wlr_gles3_renderer_init() { - init_globals(); - return wlr_renderer_init(NULL, &wlr_renderer_impl); -} diff --git a/render/gles3/shaders.c b/render/gles3/shaders.c deleted file mode 100644 index dd7f790f..00000000 --- a/render/gles3/shaders.c +++ /dev/null @@ -1,63 +0,0 @@ -#include "render/gles3.h" -#include - -// Colored quads -const GLchar quad_vertex_src[] = -"uniform mat4 proj;" -"uniform vec4 color;" -"attribute vec2 pos;" -"attribute vec2 texcoord;" -"varying vec4 v_color;" -"varying vec2 v_texcoord;" -"void main() {" -" gl_Position = proj * vec4(pos, 0.0, 1.0);" -" v_color = color;" -" v_texcoord = texcoord;" -"}"; - -const GLchar quad_fragment_src[] = -"precision mediump float;" -"varying vec4 v_color;" -"varying vec2 v_texcoord;" -"void main() {" -" gl_FragColor = v_color;" -"}"; - -// Colored ellipses (TODO) - -const GLchar ellipse_fragment_src[] = -"precision mediump float;" -"varying vec4 v_color;" -"varying vec2 v_texcoord;" -"void main() {" -" float l = length(v_texcoord - vec2(0.5, 0.5));" -" if (l > 0.5) discard;" -" gl_FragColor = v_color;" -"}"; - -// Textured quads -const GLchar vertex_src[] = -"uniform mat4 proj;" -"attribute vec2 pos;" -"attribute vec2 texcoord;" -"varying vec2 v_texcoord;" -"void main() {" -" gl_Position = proj * vec4(pos, 0.0, 1.0);" -" v_texcoord = texcoord;" -"}"; - -const GLchar fragment_src_RGB[] = -"precision mediump float;" -"varying vec2 v_texcoord;" -"uniform sampler2D tex;" -"void main() {" -" gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0);" -"}"; - -const GLchar fragment_src_RGBA[] = -"precision mediump float;" -"varying vec2 v_texcoord;" -"uniform sampler2D tex;" -"void main() {" -" gl_FragColor = texture2D(tex, v_texcoord);" -"}"; diff --git a/render/gles3/surface.c b/render/gles3/surface.c deleted file mode 100644 index 0ddf3ae0..00000000 --- a/render/gles3/surface.c +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "render/gles3.h" - -static bool gles3_surface_attach_pixels(struct wlr_surface_state *surface, - uint32_t format, int width, int height, const unsigned char *pixels) { - assert(surface); - surface->wlr_surface->width = width; - surface->wlr_surface->height = height; - surface->wlr_surface->format = format; - GL_CALL(glGenTextures(1, &surface->tex_id)); - GL_CALL(glBindTexture(GL_TEXTURE_2D, surface->tex_id)); - GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, - format, GL_UNSIGNED_BYTE, pixels)); - surface->wlr_surface->valid = true; - return true; -} - -static void gles3_surface_get_matrix(struct wlr_surface_state *surface, - float (*matrix)[16], const float (*projection)[16], int x, int y) { - struct wlr_surface *_surface = surface->wlr_surface; - float world[16]; - wlr_matrix_identity(matrix); - wlr_matrix_translate(&world, x, y, 0); - wlr_matrix_mul(matrix, &world, matrix); - wlr_matrix_scale(&world, _surface->width, _surface->height, 1); - wlr_matrix_mul(matrix, &world, matrix); - wlr_matrix_mul(projection, matrix, matrix); -} - -static void gles3_surface_bind(struct wlr_surface_state *surface) { - GL_CALL(glActiveTexture(GL_TEXTURE0 + 1)); - GL_CALL(glBindTexture(GL_TEXTURE_2D, surface->tex_id)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); -} - -static void gles3_surface_destroy(struct wlr_surface_state *surface) { - GL_CALL(glDeleteTextures(1, &surface->tex_id)); - free(surface); -} - -static struct wlr_surface_impl wlr_surface_impl = { - .attach_pixels = gles3_surface_attach_pixels, - // .attach_shm = TODO - .get_matrix = gles3_surface_get_matrix, - .bind = gles3_surface_bind, - .destroy = gles3_surface_destroy, -}; - -struct wlr_surface *gles3_surface_init() { - struct wlr_surface_state *state = calloc(sizeof(struct wlr_surface_state), 1); - struct wlr_surface *surface = wlr_surface_init(state, &wlr_surface_impl); - state->wlr_surface = surface; - return surface; -} diff --git a/render/gles3/util.c b/render/gles3/util.c deleted file mode 100644 index a3aa87f3..00000000 --- a/render/gles3/util.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include "render/gles3.h" - -const char *gles3_strerror(GLenum err) { - switch (err) { - case GL_INVALID_ENUM: - return "Invalid enum"; - case GL_INVALID_VALUE: - return "Invalid value"; - case GL_INVALID_OPERATION: - return "Invalid operation"; - case GL_OUT_OF_MEMORY: - return "Out of memory"; - case GL_INVALID_FRAMEBUFFER_OPERATION: - return "Invalid framebuffer operation"; - default: - return "Unknown error"; - } -} - -bool _gles3_flush_errors(const char *file, int line) { - GLenum err; - bool failure = false; - while ((err = glGetError()) != GL_NO_ERROR) { - failure = true; - if (err == GL_OUT_OF_MEMORY) { - // The OpenGL context is now undefined - _wlr_log(L_ERROR, "[%s:%d] Fatal GL error: out of memory", file, line); - exit(1); - } else { - _wlr_log(L_ERROR, "[%s:%d] GL error %d %s", file, line, err, gles3_strerror(err)); - } - } - return failure; -} -- cgit v1.2.3