Switch to GLES2

Closes #13
This commit is contained in:
Drew DeVault 2017-06-23 11:38:45 -04:00
parent 168f0955ab
commit b18209c904
14 changed files with 97 additions and 111 deletions

View File

@ -136,7 +136,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display) {
goto error; 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->context = eglCreateContext(egl->display, egl->config,
EGL_NO_CONTEXT, attribs); EGL_NO_CONTEXT, attribs);

View File

@ -9,9 +9,9 @@
#include <wayland-server.h> #include <wayland-server.h>
#include <wayland-server-protocol.h> #include <wayland-server-protocol.h>
#include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wlr/render/matrix.h> #include <wlr/render/matrix.h>
#include <wlr/render/gles3.h> #include <wlr/render/gles2.h>
#include <wlr/render.h> #include <wlr/render.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/session.h> #include <wlr/session.h>
@ -121,7 +121,7 @@ int main(int argc, char *argv[]) {
compositor.pointer_axis_cb = handle_pointer_axis; compositor.pointer_axis_cb = handle_pointer_axis;
compositor_init(&compositor); compositor_init(&compositor);
state.renderer = wlr_gles3_renderer_init(); state.renderer = wlr_gles2_renderer_init();
state.cat_texture = wlr_render_surface_init(state.renderer); state.cat_texture = wlr_render_surface_init(state.renderer);
wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, wlr_surface_attach_pixels(state.cat_texture, GL_RGBA,
cat_tex.width, cat_tex.height, cat_tex.pixel_data); cat_tex.width, cat_tex.height, cat_tex.pixel_data);

View File

@ -9,9 +9,9 @@
#include <wayland-server.h> #include <wayland-server.h>
#include <wayland-server-protocol.h> #include <wayland-server-protocol.h>
#include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wlr/render/matrix.h> #include <wlr/render/matrix.h>
#include <wlr/render/gles3.h> #include <wlr/render/gles2.h>
#include <wlr/render.h> #include <wlr/render.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/session.h> #include <wlr/session.h>
@ -202,7 +202,7 @@ int main(int argc, char *argv[]) {
compositor.keyboard_key_cb = handle_keyboard_key; compositor.keyboard_key_cb = handle_keyboard_key;
compositor_init(&compositor); compositor_init(&compositor);
state.renderer = wlr_gles3_renderer_init(); state.renderer = wlr_gles2_renderer_init();
state.cat_texture = wlr_render_surface_init(state.renderer); state.cat_texture = wlr_render_surface_init(state.renderer);
wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, wlr_surface_attach_pixels(state.cat_texture, GL_RGBA,
cat_tex.width, cat_tex.height, cat_tex.pixel_data); cat_tex.width, cat_tex.height, cat_tex.pixel_data);

View File

@ -5,7 +5,7 @@
#include <time.h> #include <time.h>
#include <inttypes.h> #include <inttypes.h>
#include <wayland-server.h> #include <wayland-server.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/session.h> #include <wlr/session.h>
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>

View File

@ -8,9 +8,9 @@
#include <wayland-server.h> #include <wayland-server.h>
#include <wayland-server-protocol.h> #include <wayland-server-protocol.h>
#include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wlr/render/matrix.h> #include <wlr/render/matrix.h>
#include <wlr/render/gles3.h> #include <wlr/render/gles2.h>
#include <wlr/render.h> #include <wlr/render.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/session.h> #include <wlr/session.h>
@ -150,7 +150,7 @@ int main(int argc, char *argv[]) {
}; };
compositor_init(&compositor); compositor_init(&compositor);
state.renderer = wlr_gles3_renderer_init(); state.renderer = wlr_gles2_renderer_init();
compositor_run(&compositor); compositor_run(&compositor);

View File

@ -10,9 +10,9 @@
#include <wayland-server.h> #include <wayland-server.h>
#include <wayland-server-protocol.h> #include <wayland-server-protocol.h>
#include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wlr/render/matrix.h> #include <wlr/render/matrix.h>
#include <wlr/render/gles3.h> #include <wlr/render/gles2.h>
#include <wlr/render.h> #include <wlr/render.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/session.h> #include <wlr/session.h>
@ -101,7 +101,7 @@ int main(int argc, char *argv[]) {
}; };
compositor_init(&compositor); compositor_init(&compositor);
state.renderer = wlr_gles3_renderer_init(); state.renderer = wlr_gles2_renderer_init();
state.cat_texture = wlr_render_surface_init(state.renderer); state.cat_texture = wlr_render_surface_init(state.renderer);
wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, wlr_surface_attach_pixels(state.cat_texture, GL_RGBA,
cat_tex.width, cat_tex.height, cat_tex.pixel_data); cat_tex.width, cat_tex.height, cat_tex.pixel_data);

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wlr/render.h> #include <wlr/render.h>
struct wlr_surface_state { struct wlr_surface_state {
@ -11,7 +11,7 @@ struct wlr_surface_state {
GLuint tex_id; GLuint tex_id;
}; };
struct wlr_surface *gles3_surface_init(); struct wlr_surface *gles2_surface_init();
extern const GLchar quad_vertex_src[]; extern const GLchar quad_vertex_src[];
extern const GLchar quad_fragment_src[]; extern const GLchar quad_fragment_src[];
@ -20,10 +20,10 @@ extern const GLchar vertex_src[];
extern const GLchar fragment_src_RGB[]; extern const GLchar fragment_src_RGB[];
extern const GLchar fragment_src_RGBA[]; extern const GLchar fragment_src_RGBA[];
bool _gles3_flush_errors(const char *file, int line); bool _gles2_flush_errors(const char *file, int line);
#define gles3_flush_errors(...) \ #define gles2_flush_errors(...) \
_gles3_flush_errors(__FILE__ + strlen(WLR_SRC_DIR) + 1, __LINE__) _gles2_flush_errors(__FILE__ + strlen(WLR_SRC_DIR) + 1, __LINE__)
#define GL_CALL(func) func; gles3_flush_errors() #define GL_CALL(func) func; gles2_flush_errors()
#endif #endif

View File

@ -0,0 +1,7 @@
#ifndef _WLR_GLES2_RENDERER_H
#define _WLR_GLES2_RENDERER_H
#include <wlr/render.h>
struct wlr_renderer *wlr_gles2_renderer_init();
#endif

View File

@ -1,7 +0,0 @@
#ifndef _WLR_GLES3_RENDERER_H
#define _WLR_GLES3_RENDERER_H
#include <wlr/render.h>
struct wlr_renderer *wlr_gles3_renderer_init();
#endif

View File

@ -2,8 +2,8 @@ add_library(wlr-render STATIC
matrix.c matrix.c
wlr_renderer.c wlr_renderer.c
wlr_surface.c wlr_surface.c
gles3/shaders.c gles2/shaders.c
gles3/renderer.c gles2/renderer.c
gles3/surface.c gles2/surface.c
gles3/util.c gles2/util.c
) )

View File

@ -1,14 +1,14 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wayland-util.h> #include <wayland-util.h>
#include <wayland-server-protocol.h> #include <wayland-server-protocol.h>
#include <wlr/render.h> #include <wlr/render.h>
#include <wlr/render/interface.h> #include <wlr/render/interface.h>
#include <wlr/render/matrix.h> #include <wlr/render/matrix.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "render/gles3.h" #include "render/gles2.h"
static struct { static struct {
bool initialized; bool initialized;
@ -16,7 +16,6 @@ static struct {
GLuint quad; GLuint quad;
GLuint ellipse; GLuint ellipse;
} shaders; } shaders;
static GLuint vao, vbo, ebo;
static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) { static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) {
*shader = GL_CALL(glCreateShader(type)); *shader = GL_CALL(glCreateShader(type));
@ -77,41 +76,11 @@ error:
wlr_log(L_ERROR, "Failed to set up default shaders!"); 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() { static void init_globals() {
init_default_shaders(); init_default_shaders();
init_default_quad();
} }
static void wlr_gles3_begin(struct wlr_renderer_state *state, static void wlr_gles2_begin(struct wlr_renderer_state *state,
struct wlr_output *output) { struct wlr_output *output) {
// TODO: let users customize the clear color? // TODO: let users customize the clear color?
GL_CALL(glClearColor(0.25f, 0.25f, 0.25f, 1)); GL_CALL(glClearColor(0.25f, 0.25f, 0.25f, 1));
@ -123,15 +92,41 @@ static void wlr_gles3_begin(struct wlr_renderer_state *state,
// for users to sling matricies themselves // for users to sling matricies themselves
} }
static void wlr_gles3_end(struct wlr_renderer_state *state) { static void wlr_gles2_end(struct wlr_renderer_state *state) {
// no-op // no-op
} }
static struct wlr_surface *wlr_gles3_surface_init(struct wlr_renderer_state *state) { static struct wlr_surface *wlr_gles2_surface_init(struct wlr_renderer_state *state) {
return gles3_surface_init(); return gles2_surface_init();
} }
static bool wlr_gles3_render_surface(struct wlr_renderer_state *state, 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]) { struct wlr_surface *surface, const float (*matrix)[16]) {
assert(surface && surface->valid); assert(surface && surface->valid);
switch (surface->format) { switch (surface->format) {
@ -145,53 +140,44 @@ static bool wlr_gles3_render_surface(struct wlr_renderer_state *state,
wlr_log(L_ERROR, "No shader for this surface format"); wlr_log(L_ERROR, "No shader for this surface format");
return false; return false;
} }
gles3_flush_errors(); gles2_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); wlr_surface_bind(surface);
GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
GL_CALL(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)); draw_quad();
return true; return true;
} }
static void wlr_gles3_render_quad(struct wlr_renderer_state *state, static void wlr_gles2_render_quad(struct wlr_renderer_state *state,
const float (*color)[4], const float (*matrix)[16]) { const float (*color)[4], const float (*matrix)[16]) {
GL_CALL(glUseProgram(shaders.quad)); 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(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3])); GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3]));
GL_CALL(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)); draw_quad();
} }
static void wlr_gles3_render_ellipse(struct wlr_renderer_state *state, static void wlr_gles2_render_ellipse(struct wlr_renderer_state *state,
const float (*color)[4], const float (*matrix)[16]) { const float (*color)[4], const float (*matrix)[16]) {
GL_CALL(glUseProgram(shaders.ellipse)); 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(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3])); GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3]));
GL_CALL(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)); draw_quad();
} }
static void wlr_gles3_destroy(struct wlr_renderer_state *state) { static void wlr_gles2_destroy(struct wlr_renderer_state *state) {
// no-op // no-op
} }
static struct wlr_renderer_impl wlr_renderer_impl = { static struct wlr_renderer_impl wlr_renderer_impl = {
.begin = wlr_gles3_begin, .begin = wlr_gles2_begin,
.end = wlr_gles3_end, .end = wlr_gles2_end,
.surface_init = wlr_gles3_surface_init, .surface_init = wlr_gles2_surface_init,
.render_with_matrix = wlr_gles3_render_surface, .render_with_matrix = wlr_gles2_render_surface,
.render_quad = wlr_gles3_render_quad, .render_quad = wlr_gles2_render_quad,
.render_ellipse = wlr_gles3_render_ellipse, .render_ellipse = wlr_gles2_render_ellipse,
.destroy = wlr_gles3_destroy .destroy = wlr_gles2_destroy
}; };
struct wlr_renderer *wlr_gles3_renderer_init() { struct wlr_renderer *wlr_gles2_renderer_init() {
init_globals(); init_globals();
return wlr_renderer_init(NULL, &wlr_renderer_impl); return wlr_renderer_init(NULL, &wlr_renderer_impl);
} }

View File

@ -1,5 +1,5 @@
#include "render/gles3.h" #include "render/gles2.h"
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
// Colored quads // Colored quads
const GLchar quad_vertex_src[] = const GLchar quad_vertex_src[] =

View File

@ -1,15 +1,15 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wayland-util.h> #include <wayland-util.h>
#include <wayland-server-protocol.h> #include <wayland-server-protocol.h>
#include <wlr/render.h> #include <wlr/render.h>
#include <wlr/render/interface.h> #include <wlr/render/interface.h>
#include <wlr/render/matrix.h> #include <wlr/render/matrix.h>
#include "render/gles3.h" #include "render/gles2.h"
static bool gles3_surface_attach_pixels(struct wlr_surface_state *surface, static bool gles2_surface_attach_pixels(struct wlr_surface_state *surface,
uint32_t format, int width, int height, const unsigned char *pixels) { uint32_t format, int width, int height, const unsigned char *pixels) {
assert(surface); assert(surface);
surface->wlr_surface->width = width; surface->wlr_surface->width = width;
@ -23,7 +23,7 @@ static bool gles3_surface_attach_pixels(struct wlr_surface_state *surface,
return true; return true;
} }
static void gles3_surface_get_matrix(struct wlr_surface_state *surface, static void gles2_surface_get_matrix(struct wlr_surface_state *surface,
float (*matrix)[16], const float (*projection)[16], int x, int y) { float (*matrix)[16], const float (*projection)[16], int x, int y) {
struct wlr_surface *_surface = surface->wlr_surface; struct wlr_surface *_surface = surface->wlr_surface;
float world[16]; float world[16];
@ -35,27 +35,27 @@ static void gles3_surface_get_matrix(struct wlr_surface_state *surface,
wlr_matrix_mul(projection, matrix, matrix); wlr_matrix_mul(projection, matrix, matrix);
} }
static void gles3_surface_bind(struct wlr_surface_state *surface) { static void gles2_surface_bind(struct wlr_surface_state *surface) {
GL_CALL(glActiveTexture(GL_TEXTURE0 + 1)); GL_CALL(glActiveTexture(GL_TEXTURE0 + 1));
GL_CALL(glBindTexture(GL_TEXTURE_2D, surface->tex_id)); 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_MIN_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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) { static void gles2_surface_destroy(struct wlr_surface_state *surface) {
GL_CALL(glDeleteTextures(1, &surface->tex_id)); GL_CALL(glDeleteTextures(1, &surface->tex_id));
free(surface); free(surface);
} }
static struct wlr_surface_impl wlr_surface_impl = { static struct wlr_surface_impl wlr_surface_impl = {
.attach_pixels = gles3_surface_attach_pixels, .attach_pixels = gles2_surface_attach_pixels,
// .attach_shm = TODO // .attach_shm = TODO
.get_matrix = gles3_surface_get_matrix, .get_matrix = gles2_surface_get_matrix,
.bind = gles3_surface_bind, .bind = gles2_surface_bind,
.destroy = gles3_surface_destroy, .destroy = gles2_surface_destroy,
}; };
struct wlr_surface *gles3_surface_init() { struct wlr_surface *gles2_surface_init() {
struct wlr_surface_state *state = calloc(sizeof(struct wlr_surface_state), 1); struct wlr_surface_state *state = calloc(sizeof(struct wlr_surface_state), 1);
struct wlr_surface *surface = wlr_surface_init(state, &wlr_surface_impl); struct wlr_surface *surface = wlr_surface_init(state, &wlr_surface_impl);
state->wlr_surface = surface; state->wlr_surface = surface;

View File

@ -1,10 +1,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <GLES3/gl3.h> #include <GLES2/gl2.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "render/gles3.h" #include "render/gles2.h"
const char *gles3_strerror(GLenum err) { const char *gles2_strerror(GLenum err) {
switch (err) { switch (err) {
case GL_INVALID_ENUM: case GL_INVALID_ENUM:
return "Invalid enum"; return "Invalid enum";
@ -21,7 +21,7 @@ const char *gles3_strerror(GLenum err) {
} }
} }
bool _gles3_flush_errors(const char *file, int line) { bool _gles2_flush_errors(const char *file, int line) {
GLenum err; GLenum err;
bool failure = false; bool failure = false;
while ((err = glGetError()) != GL_NO_ERROR) { while ((err = glGetError()) != GL_NO_ERROR) {
@ -31,7 +31,7 @@ bool _gles3_flush_errors(const char *file, int line) {
_wlr_log(L_ERROR, "[%s:%d] Fatal GL error: out of memory", file, line); _wlr_log(L_ERROR, "[%s:%d] Fatal GL error: out of memory", file, line);
exit(1); exit(1);
} else { } else {
_wlr_log(L_ERROR, "[%s:%d] GL error %d %s", file, line, err, gles3_strerror(err)); _wlr_log(L_ERROR, "[%s:%d] GL error %d %s", file, line, err, gles2_strerror(err));
} }
} }
return failure; return failure;