From cd6a40d81679c37795e9d24354a8bbbf84cd53a6 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 8 Jun 2017 15:52:42 -0400 Subject: Further improvements to rendering subsystem --- render/gles3/renderer.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++ render/gles3/shaders.c | 28 +++++++++ render/gles3/surface.c | 63 +++++++++++++++++++ 3 files changed, 252 insertions(+) create mode 100644 render/gles3/renderer.c create mode 100644 render/gles3/shaders.c create mode 100644 render/gles3/surface.c (limited to 'render/gles3') diff --git a/render/gles3/renderer.c b/render/gles3/renderer.c new file mode 100644 index 00000000..8f602fcf --- /dev/null +++ b/render/gles3/renderer.c @@ -0,0 +1,161 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "render/gles3.h" + +static struct { + bool initialized; + GLuint rgb, rgba; +} shaders; +static GLuint vao, vbo, ebo; + +static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) { + *shader = glCreateShader(type); + int len = strlen(src); + glShaderSource(*shader, 1, &src, &len); + glCompileShader(*shader); + GLint success; + glGetShaderiv(*shader, GL_COMPILE_STATUS, &success); + if (success == GL_FALSE) { + GLint loglen; + glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &loglen); + GLchar msg[loglen]; + glGetShaderInfoLog(*shader, loglen, &loglen, msg); + 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 = glCreateProgram(); + glAttachShader(*program, vertex); + glAttachShader(*program, fragment); + glLinkProgram(*program); + glDeleteProgram(vertex); + glDeleteProgram(fragment); + 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; + } + 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, + }; + + glGenVertexArrays(1, &vao); + glGenBuffers(1, &vbo); + + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)0); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat))); + glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW); + + glGenBuffers(1, &ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + 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? + glClearColor(0.25f, 0.25f, 0.25f, 1); + glClear(GL_COLOR_BUFFER_BIT); + int32_t width = output->width; + int32_t height = output->height; + 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: + glUseProgram(shaders.rgb); + break; + case GL_RGBA: + glUseProgram(shaders.rgba); + break; + default: + return false; + } + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + wlr_surface_bind(surface); + glUniformMatrix4fv(0, 1, GL_TRUE, *matrix); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + return true; +} + +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, + .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 new file mode 100644 index 00000000..f53ef385 --- /dev/null +++ b/render/gles3/shaders.c @@ -0,0 +1,28 @@ +#include "render/gles3.h" +#include + +const GLchar vertex_src[] = +"uniform mat4 proj;\n" +"attribute vec2 pos;\n" +"attribute vec2 texcoord;\n" +"varying vec2 v_texcoord;\n" +"void main() {\n" +" gl_Position = proj * vec4(pos, 0.0, 1.0);\n" +" v_texcoord = texcoord;\n" +"}\n"; + +const GLchar fragment_src_RGB[] = +"precision mediump float;\n" +"varying vec2 v_texcoord;\n" +"uniform sampler2D tex;\n" +"void main() {\n" +" gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0);\n" +"}\n"; + +const GLchar fragment_src_RGBA[] = +"precision mediump float;\n" +"varying vec2 v_texcoord;\n" +"uniform sampler2D tex;\n" +"void main() {\n" +" gl_FragColor = texture2D(tex, v_texcoord);\n" +"}\n"; diff --git a/render/gles3/surface.c b/render/gles3/surface.c new file mode 100644 index 00000000..09d78715 --- /dev/null +++ b/render/gles3/surface.c @@ -0,0 +1,63 @@ +#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; + // TODO: Error handling + glGenTextures(1, &surface->tex_id); + glBindTexture(GL_TEXTURE_2D, surface->tex_id); + 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) { + glActiveTexture(GL_TEXTURE0 + 1); + glBindTexture(GL_TEXTURE_2D, surface->tex_id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); +} + +static void gles3_surface_destroy(struct wlr_surface_state *surface) { + // TODO +} + +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; +} -- cgit v1.2.3