diff options
Diffstat (limited to 'render')
-rw-r--r-- | render/CMakeLists.txt | 2 | ||||
-rw-r--r-- | render/renderer.c | 138 | ||||
-rw-r--r-- | render/shader.c | 45 | ||||
-rw-r--r-- | render/surface.c | 34 |
4 files changed, 195 insertions, 24 deletions
diff --git a/render/CMakeLists.txt b/render/CMakeLists.txt index 597077ed..bd247d56 100644 --- a/render/CMakeLists.txt +++ b/render/CMakeLists.txt @@ -1,4 +1,6 @@ add_library(wlr-render STATIC matrix.c shader.c + surface.c + renderer.c ) diff --git a/render/renderer.c b/render/renderer.c new file mode 100644 index 00000000..174272a9 --- /dev/null +++ b/render/renderer.c @@ -0,0 +1,138 @@ +#include <stdint.h> +#include <stdlib.h> +#include <assert.h> +#include <GLES3/gl3.h> +#include <wayland-util.h> +#include <wayland-server-protocol.h> +#include <wlr/render.h> +#include <wlr/render/matrix.h> +#include <common/log.h> +#include "render.h" + +static struct wlr_shader *default_shader = NULL; + +static const GLchar vert_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"; + +static const GLchar frag_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"; + +static const GLchar frag_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"; + +static void default_shader_init() { + if (default_shader) { + return; + } + default_shader = wlr_shader_init(vert_src); + if (!default_shader) { + goto error; + } + if (!wlr_shader_add_format(default_shader, GL_RGB, frag_src_RGB)) { + goto error; + } + if (!wlr_shader_add_format(default_shader, GL_RGBA, frag_src_RGBA)) { + goto error; + } + return; +error: + wlr_shader_destroy(default_shader); + wlr_log(L_ERROR, "Failed to set up default shaders!"); +} + +static GLuint vao, vbo, ebo; + +static void default_quad_init() { + 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 global_init() { + default_shader_init(); + default_quad_init(); +} + +struct wlr_renderer *wlr_renderer_init() { + global_init(); + struct wlr_renderer *r = calloc(sizeof(struct wlr_renderer), 1); + r->shader = default_shader; + return r; +} + +void wlr_renderer_set_shader(struct wlr_renderer *renderer, + struct wlr_shader *shader) { + assert(renderer); + renderer->shader = shader; +} + +bool wlr_render_quad(struct wlr_renderer *renderer, + struct wlr_surface *surf, float (*transform)[16], + float x, float y) { + assert(renderer && surf && renderer->shader && surf->valid); + wlr_shader_use(renderer->shader, surf->format); + + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glActiveTexture(GL_TEXTURE0 + 1); + glBindTexture(GL_TEXTURE_2D, surf->tex_id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + float world[16], final[16]; + wlr_matrix_identity(&final); + wlr_matrix_translate(&world, x, y, 0); + wlr_matrix_mul(&final, &world, &final); + wlr_matrix_scale(&world, surf->width, surf->height, 1); + wlr_matrix_mul(&final, &world, &final); + wlr_matrix_mul(transform, &final, &final); + glUniformMatrix4fv(0, 1, GL_TRUE, final); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + return false; +} + +void wlr_renderer_destroy(struct wlr_renderer *renderer) { + // TODO +} diff --git a/render/shader.c b/render/shader.c index d7c10614..63797b8a 100644 --- a/render/shader.c +++ b/render/shader.c @@ -6,7 +6,6 @@ #include <GLES3/gl3.h> #include <wlr/render.h> #include <wlr/render/matrix.h> -#include <wayland-util.h> #include "render.h" static bool create_shader(GLenum type, const GLchar *src, GLint len, GLuint *shader) { @@ -44,6 +43,11 @@ bool wlr_shader_add_format(struct wlr_shader *shader, uint32_t format, if (_shader->valid) { shader = calloc(sizeof(struct wlr_shader), 1); shader->vert = _shader->vert; + } else { + while (shader->next) { + _shader = shader; + shader = shader->next; + } } shader->format = format; GLuint _frag; @@ -55,11 +59,10 @@ bool wlr_shader_add_format(struct wlr_shader *shader, uint32_t format, glAttachShader(shader->program, _frag); glLinkProgram(shader->program); glDeleteProgram(_frag); - if (!_shader->valid) { - _shader->valid = true; - } else { - wl_list_insert(&_shader->link, &shader->link); + if (_shader->valid) { + _shader->next = shader; } + shader->valid = true; return true; error: if (_shader->valid) { @@ -69,32 +72,26 @@ error: } bool wlr_shader_use(struct wlr_shader *shader, uint32_t format) { - struct wlr_shader *s = shader; - if (s->format == format) { - glUseProgram(s->program); + if (shader->format == format) { + glUseProgram(shader->program); return true; } - if (shader->link.next) { - wl_list_for_each(s, &shader->link, link) { - if (s->format == format) { - glUseProgram(s->program); - return true; - } + while (shader->next) { + shader = shader->next; + if (shader->format == format) { + glUseProgram(shader->program); + return true; } } return false; } void wlr_shader_destroy(struct wlr_shader *shader) { - if (!shader) { - return; - } - glDeleteProgram(shader->vert); - glDeleteProgram(shader->program); - if (shader->link.next) { - struct wlr_shader *next = wl_container_of(shader->link.next, - next, link); - wlr_shader_destroy(next); + while (shader) { + struct wlr_shader *_shader = shader; + glDeleteProgram(shader->vert); + glDeleteProgram(shader->program); + shader = shader->next; + free(_shader); } - free(shader); } diff --git a/render/surface.c b/render/surface.c new file mode 100644 index 00000000..930e118d --- /dev/null +++ b/render/surface.c @@ -0,0 +1,34 @@ +#include <stdint.h> +#include <stdlib.h> +#include <assert.h> +#include <GLES3/gl3.h> +#include <wayland-util.h> +#include <wayland-server-protocol.h> +#include <wlr/render.h> +#include <wlr/render/matrix.h> +#include "render.h" + +struct wlr_surface *wlr_surface_init() { + return calloc(sizeof(struct wlr_surface), 1); +} + +void wlr_surface_attach_pixels(struct wlr_surface *surf, uint32_t format, + int width, int height, const unsigned char *pixels) { + assert(surf); + surf->width = width; + surf->height = height; + surf->format = format; + // TODO: Error handling + glGenTextures(1, &surf->tex_id); + glBindTexture(GL_TEXTURE_2D, surf->tex_id); + glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, + format, GL_UNSIGNED_BYTE, pixels); + surf->valid = true; +} + +void wlr_surface_attach_shm(struct wlr_surface *surf, uint32_t format, + struct wl_shm_buffer *shm); + +void wlr_surface_destroy(struct wlr_surface *tex) { + // TODO +} |