diff --git a/example/rotation.c b/example/rotation.c index 9df18da9..6a13b2da 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -46,22 +47,23 @@ static void output_frame(struct wl_listener *listener, void *data) { struct wlr_output *output = ostate->output; struct state *s = ostate->state; - glClearColor(0.25f, 0.25f, 0.25f, 1); - glClear(GL_COLOR_BUFFER_BIT); + wlr_renderer_begin(s->renderer, output); - int32_t width = output->width; - int32_t height = output->height; - glViewport(0, 0, width, height); - wlr_output_effective_resolution(output, &width, &height); + float matrix[16]; + wlr_surface_get_matrix(s->cat_texture, &matrix, + &output->transform_matrix, ostate->x, ostate->y); + wlr_render_with_matrix(s->renderer, s->cat_texture, &matrix); - wlr_render_quad(s->renderer, s->cat_texture, - &output->transform_matrix, ostate->x, ostate->y); + wlr_renderer_end(s->renderer); struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); long ms = (now.tv_sec - ostate->last_frame.tv_sec) * 1000 + (now.tv_nsec - ostate->last_frame.tv_nsec) / 1000000; float seconds = ms / 1000.0f; + + int32_t width, height; + wlr_output_effective_resolution(output, &width, &height); ostate->x += ostate->vel_x * seconds; ostate->y += ostate->vel_y * seconds; if (ostate->y > height - 128) { @@ -224,8 +226,8 @@ int main(int argc, char *argv[]) { return 1; } - state.renderer = wlr_renderer_init(); - state.cat_texture = wlr_surface_init(); + state.renderer = wlr_gles3_renderer_init(); + state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGB, cat_tex.width, cat_tex.height, cat_tex.pixel_data); @@ -239,11 +241,11 @@ int main(int argc, char *argv[]) { wl_event_loop_dispatch(event_loop, 0); } - wlr_renderer_destroy(state.renderer); - wlr_surface_destroy(state.cat_texture); wl_event_source_remove(timer); wlr_backend_destroy(wlr); wlr_session_finish(session); + wlr_surface_destroy(state.cat_texture); + wlr_renderer_destroy(state.renderer); wl_display_destroy(display); struct output_config *ptr, *tmp; diff --git a/include/render.h b/include/render.h deleted file mode 100644 index 0f977eb0..00000000 --- a/include/render.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef _WLR_RENDER_INTERNAL_H -#define _WLR_RENDER_INTERNAL_H -#include -#include -#include -#include -#include - -struct wlr_surface { - bool valid; - GLuint tex_id; - uint32_t format; - int width, height; -}; - -struct wlr_shader { - bool valid; - uint32_t format; - GLuint vert; - GLuint program; - struct wlr_shader *next; -}; - -struct wlr_renderer { - struct wlr_shader *shader; - // TODO: EGL stuff -}; - -#endif diff --git a/include/render/gles3.h b/include/render/gles3.h new file mode 100644 index 00000000..91b738a6 --- /dev/null +++ b/include/render/gles3.h @@ -0,0 +1,18 @@ +#ifndef _WLR_RENDER_GLES2_INTERNAL_H +#define _WLR_RENDER_GLES2_INTERNAL_H +#include +#include +#include + +struct wlr_surface_state { + struct wlr_surface *wlr_surface; + GLuint tex_id; +}; + +struct wlr_surface *gles3_surface_init(); + +extern const GLchar vertex_src[]; +extern const GLchar fragment_src_RGB[]; +extern const GLchar fragment_src_RGBA[]; + +#endif diff --git a/include/wlr/render.h b/include/wlr/render.h index 3335b3d5..88aa615e 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -2,30 +2,74 @@ #define _WLR_RENDER_H #include #include +#include struct wlr_surface; -struct wlr_surface *wlr_surface_init(); -void wlr_surface_attach_pixels(struct wlr_surface *surf, uint32_t format, - int width, int height, const unsigned char *pixels); -void wlr_surface_attach_shm(struct wlr_surface *surf, uint32_t format, - struct wl_shm_buffer *shm); -// TODO: EGL -void wlr_surface_destroy(struct wlr_surface *tex); - -struct wlr_shader; -struct wlr_shader *wlr_shader_init(const char *vertex); -bool wlr_shader_add_format(struct wlr_shader *shader, uint32_t format, - const char *frag); -bool wlr_shader_use(struct wlr_shader *shader, uint32_t format); -void wlr_shader_destroy(struct wlr_shader *shader); - struct wlr_renderer; -struct wlr_renderer *wlr_renderer_init(); -void wlr_renderer_set_shader(struct wlr_renderer *renderer, - struct wlr_shader *shader); -bool wlr_render_quad(struct wlr_renderer *renderer, - struct wlr_surface *surf, float (*transform)[16], - float x, float y); + +void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *output); +void wlr_renderer_end(struct wlr_renderer *r); +/** + * Requests a surface handle from this renderer. + */ +struct wlr_surface *wlr_render_surface_init(struct wlr_renderer *r); +/** + * Renders the requested surface using the provided matrix. A typical surface + * rendering goes like so: + * + * struct wlr_renderer *renderer; + * struct wlr_surface *surface; + * float projection[16]; + * float matrix[16]; + * wlr_surface_get_matrix(surface, &matrix, &projection, 123, 321); + * wlr_render_with_matrix(renderer, surface, &matrix); + * + * This will render the surface at <123, 321>. + */ +bool wlr_render_with_matrix(struct wlr_renderer *r, + struct wlr_surface *surface, const float (*matrix)[16]); +/** + * Destroys this wlr_renderer. Surfaces must be destroyed separately. + */ void wlr_renderer_destroy(struct wlr_renderer *renderer); +struct wlr_surface_impl; +struct wlr_surface_state; + +struct wlr_surface { + struct wlr_surface_impl *impl; + struct wlr_surface_state *state; + bool valid; + uint32_t format; + int width, height; +}; + +/** + * Attaches a pixel buffer to this surface. The buffer may be discarded after + * calling this function. + */ +bool wlr_surface_attach_pixels(struct wlr_surface *surf, uint32_t format, + int width, int height, const unsigned char *pixels); +/** + * Attaches pixels from a wl_shm_buffer to this surface. The shm buffer may be + * invalidated after calling this function. + */ +bool wlr_surface_attach_shm(struct wlr_surface *surf, uint32_t format, + struct wl_shm_buffer *shm); +/** + * Prepares a matrix with the appropriate scale for the given surface and + * multiplies it with the projection, producing a matrix that the shader can + * muptlipy vertex coordinates with to get final screen coordinates. + * + * The projection matrix is assumed to be an orthographic projection of [0, + * width) and [0, height], and the x and y coordinates provided are used as + * such. + */ +void wlr_surface_get_matrix(struct wlr_surface *surface, + float (*matrix)[16], const float (*projection)[16], int x, int y); +/** + * Destroys this wlr_surface. + */ +void wlr_surface_destroy(struct wlr_surface *tex); + #endif diff --git a/include/wlr/render/gles3.h b/include/wlr/render/gles3.h new file mode 100644 index 00000000..7b60bb97 --- /dev/null +++ b/include/wlr/render/gles3.h @@ -0,0 +1,7 @@ +#ifndef _WLR_GLES3_RENDERER_H +#define _WLR_GLES3_RENDERER_H +#include + +struct wlr_renderer *wlr_gles3_renderer_init(); + +#endif diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h new file mode 100644 index 00000000..80471ac8 --- /dev/null +++ b/include/wlr/render/interface.h @@ -0,0 +1,43 @@ +#ifndef _WLR_RENDER_INTERFACE_H +#define _WLR_RENDER_INTERFACE_H +#include +#include +#include +#include + +struct wlr_renderer_impl; +struct wlr_renderer_state; + +struct wlr_renderer { + struct wlr_renderer_impl *impl; + struct wlr_renderer_state *state; +}; + +struct wlr_renderer_impl { + void (*begin)(struct wlr_renderer_state *state, struct wlr_output *output); + void (*end)(struct wlr_renderer_state *state); + struct wlr_surface *(*surface_init)(struct wlr_renderer_state *state); + bool (*render_with_matrix)(struct wlr_renderer_state *state, + struct wlr_surface *surface, const float (*matrix)[16]); + void (*destroy)(struct wlr_renderer_state *state); +}; + +struct wlr_renderer *wlr_renderer_init(struct wlr_renderer_state *state, + struct wlr_renderer_impl *impl); + +struct wlr_surface_impl { + bool (*attach_pixels)(struct wlr_surface_state *state, uint32_t format, + int width, int height, const unsigned char *pixels); + bool (*attach_shm)(struct wlr_surface_state *state, uint32_t format, + struct wl_shm_buffer *shm); + // TODO: egl + void (*get_matrix)(struct wlr_surface_state *state, + float (*matrix)[16], const float (*projection)[16], int x, int y); + void (*bind)(struct wlr_surface_state *state); + void (*destroy)(struct wlr_surface_state *state); +}; + +struct wlr_surface *wlr_surface_init(); +void wlr_surface_bind(struct wlr_surface *surface); + +#endif diff --git a/include/wlr/render/matrix.h b/include/wlr/render/matrix.h index a1053b00..954207da 100644 --- a/include/wlr/render/matrix.h +++ b/include/wlr/render/matrix.h @@ -5,6 +5,6 @@ void wlr_matrix_identity(float (*output)[16]); void wlr_matrix_translate(float (*output)[16], float x, float y, float z); void wlr_matrix_scale(float (*output)[16], float x, float y, float z); void wlr_matrix_rotate(float (*output)[16], float radians); -void wlr_matrix_mul(float (*x)[16], float (*y)[16], float (*product)[16]); +void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)[16]); #endif diff --git a/render/CMakeLists.txt b/render/CMakeLists.txt index bd247d56..aabf42d8 100644 --- a/render/CMakeLists.txt +++ b/render/CMakeLists.txt @@ -1,6 +1,8 @@ add_library(wlr-render STATIC matrix.c - shader.c - surface.c - renderer.c + wlr_renderer.c + wlr_surface.c + gles3/shaders.c + gles3/renderer.c + gles3/surface.c ) 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; +} diff --git a/render/matrix.c b/render/matrix.c index 7d111c38..4de20e5d 100644 --- a/render/matrix.c +++ b/render/matrix.c @@ -41,7 +41,7 @@ void wlr_matrix_rotate(float (*output)[16], float radians) { (*output)[mind(2, 2)] = _cos; } -void wlr_matrix_mul(float (*x)[16], float (*y)[16], float (*product)[16]) { +void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)[16]) { float _product[16] = { (*x)[mind(1, 1)] * (*y)[mind(1, 1)] + (*x)[mind(1, 2)] * (*y)[mind(2, 1)] + (*x)[mind(1, 3)] * (*y)[mind(3, 1)] + (*x)[mind(1, 4)] * (*y)[mind(4, 1)], diff --git a/render/renderer.c b/render/renderer.c deleted file mode 100644 index 174272a9..00000000 --- a/render/renderer.c +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#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 deleted file mode 100644 index 63797b8a..00000000 --- a/render/shader.c +++ /dev/null @@ -1,97 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "render.h" - -static bool create_shader(GLenum type, const GLchar *src, GLint len, GLuint *shader) { - *shader = glCreateShader(type); - 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; -} - -struct wlr_shader *wlr_shader_init(const char *vertex) { - struct wlr_shader *shader = calloc(sizeof(struct wlr_shader), 1); - if (!create_shader(GL_VERTEX_SHADER, vertex, strlen(vertex), &shader->vert)) { - wlr_shader_destroy(shader); - return NULL; - } - return shader; -} - -bool wlr_shader_add_format(struct wlr_shader *shader, uint32_t format, - const char *frag) { - assert(shader); - struct wlr_shader *_shader = shader; - 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; - if (!create_shader(GL_FRAGMENT_SHADER, frag, strlen(frag), &_frag)) { - goto error; - } - shader->program = glCreateProgram(); - glAttachShader(shader->program, shader->vert); - glAttachShader(shader->program, _frag); - glLinkProgram(shader->program); - glDeleteProgram(_frag); - if (_shader->valid) { - _shader->next = shader; - } - shader->valid = true; - return true; -error: - if (_shader->valid) { - wlr_shader_destroy(shader); - } - return false; -} - -bool wlr_shader_use(struct wlr_shader *shader, uint32_t format) { - if (shader->format == format) { - glUseProgram(shader->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) { - while (shader) { - struct wlr_shader *_shader = shader; - glDeleteProgram(shader->vert); - glDeleteProgram(shader->program); - shader = shader->next; - free(_shader); - } -} diff --git a/render/surface.c b/render/surface.c deleted file mode 100644 index 930e118d..00000000 --- a/render/surface.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#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 -} diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c new file mode 100644 index 00000000..2fd2fd31 --- /dev/null +++ b/render/wlr_renderer.c @@ -0,0 +1,33 @@ +#include +#include +#include + +struct wlr_renderer *wlr_renderer_init(struct wlr_renderer_state *state, + struct wlr_renderer_impl *impl) { + struct wlr_renderer *r = calloc(sizeof(struct wlr_renderer), 1); + r->state = state; + r->impl = impl; + return r; +} + +void wlr_renderer_destroy(struct wlr_renderer *r) { + r->impl->destroy(r->state); + free(r); +} + +void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *o) { + r->impl->begin(r->state, o); +} + +void wlr_renderer_end(struct wlr_renderer *r) { + r->impl->end(r->state); +} + +struct wlr_surface *wlr_render_surface_init(struct wlr_renderer *r) { + return r->impl->surface_init(r->state); +} + +bool wlr_render_with_matrix(struct wlr_renderer *r, + struct wlr_surface *surface, const float (*matrix)[16]) { + return r->impl->render_with_matrix(r->state, surface, matrix); +} diff --git a/render/wlr_surface.c b/render/wlr_surface.c new file mode 100644 index 00000000..46a12205 --- /dev/null +++ b/render/wlr_surface.c @@ -0,0 +1,36 @@ +#include +#include +#include + +struct wlr_surface *wlr_surface_init(struct wlr_surface_state *state, + struct wlr_surface_impl *impl) { + struct wlr_surface *s = calloc(sizeof(struct wlr_surface), 1); + s->state = state; + s->impl = impl; + return s; +} + +void wlr_surface_destroy(struct wlr_surface *surface) { + surface->impl->destroy(surface->state); + free(surface); +} + +void wlr_surface_bind(struct wlr_surface *surface) { + surface->impl->bind(surface->state); +} + +bool wlr_surface_attach_pixels(struct wlr_surface *surface, uint32_t format, + int width, int height, const unsigned char *pixels) { + return surface->impl->attach_pixels(surface->state, + format, width, height, pixels); +} + +bool wlr_surface_attach_shm(struct wlr_surface *surface, uint32_t format, + struct wl_shm_buffer *shm) { + return surface->impl->attach_shm(surface->state, format, shm); +} + +void wlr_surface_get_matrix(struct wlr_surface *surface, + float (*matrix)[16], const float (*projection)[16], int x, int y) { + surface->impl->get_matrix(surface->state, matrix, projection, x, y); +}