Add wlr_renderer and move quad rendering there
This commit is contained in:
parent
211488131f
commit
83f8864f0a
|
@ -16,37 +16,13 @@
|
|||
#include <math.h>
|
||||
#include "cat.h"
|
||||
|
||||
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[] =
|
||||
"precision mediump float;\n"
|
||||
"varying vec2 v_texcoord;\n"
|
||||
"uniform sampler2D tex;\n"
|
||||
"void main() {\n"
|
||||
" gl_FragColor = texture2D(tex, v_texcoord);\n"
|
||||
"}\n";
|
||||
|
||||
struct state {
|
||||
struct wl_listener output_add;
|
||||
struct wl_listener output_remove;
|
||||
struct wl_list outputs;
|
||||
struct wl_list config;
|
||||
|
||||
struct gl {
|
||||
struct wlr_shader *shader;
|
||||
GLuint vao;
|
||||
GLuint vbo;
|
||||
GLuint ebo;
|
||||
GLuint tex;
|
||||
} gl;
|
||||
struct wlr_renderer *renderer;
|
||||
struct wlr_surface *cat_texture;
|
||||
};
|
||||
|
||||
struct output_state {
|
||||
|
@ -62,59 +38,9 @@ struct output_state {
|
|||
struct output_config {
|
||||
char *name;
|
||||
enum wl_output_transform transform;
|
||||
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
static void init_gl(struct gl *gl) {
|
||||
gl->shader = wlr_shader_init(vert_src);
|
||||
if (!gl->shader) {
|
||||
exit(1);
|
||||
}
|
||||
if (!wlr_shader_add_format(gl->shader, WL_SHM_FORMAT_RGB332, frag_src)) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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, &gl->vao);
|
||||
glGenBuffers(1, &gl->vbo);
|
||||
|
||||
glBindVertexArray(gl->vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, gl->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, &gl->ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl->ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW);
|
||||
|
||||
glGenTextures(1, &gl->tex);
|
||||
glBindTexture(GL_TEXTURE_2D, gl->tex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, cat_tex.width, cat_tex.height,
|
||||
0, GL_RGB, GL_UNSIGNED_BYTE, cat_tex.pixel_data);
|
||||
}
|
||||
|
||||
static void cleanup_gl(struct gl *gl) {
|
||||
wlr_shader_destroy(gl->shader);
|
||||
glDeleteVertexArrays(1, &gl->vao);
|
||||
glDeleteBuffers(1, &gl->vbo);
|
||||
glDeleteTextures(1, &gl->tex);
|
||||
}
|
||||
|
||||
static void output_frame(struct wl_listener *listener, void *data) {
|
||||
struct output_state *ostate = wl_container_of(listener, ostate, frame);
|
||||
struct wlr_output *output = ostate->output;
|
||||
|
@ -128,26 +54,8 @@ static void output_frame(struct wl_listener *listener, void *data) {
|
|||
glViewport(0, 0, width, height);
|
||||
wlr_output_effective_resolution(output, &width, &height);
|
||||
|
||||
wlr_shader_use(s->gl.shader, WL_SHM_FORMAT_RGB332);
|
||||
|
||||
glBindVertexArray(s->gl.vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, s->gl.vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->gl.ebo);
|
||||
glActiveTexture(GL_TEXTURE0 + 1);
|
||||
glBindTexture(GL_TEXTURE_2D, s->gl.tex);
|
||||
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, ostate->x, ostate->y, 0);
|
||||
wlr_matrix_mul(&final, &world, &final);
|
||||
wlr_matrix_scale(&world, 128, 128, 1);
|
||||
wlr_matrix_mul(&final, &world, &final);
|
||||
wlr_matrix_mul(&output->transform_matrix, &final, &final);
|
||||
glUniformMatrix4fv(0, 1, GL_TRUE, final);
|
||||
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
wlr_render_quad(s->renderer, s->cat_texture,
|
||||
&output->transform_matrix, ostate->x, ostate->y);
|
||||
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
|
@ -316,7 +224,10 @@ int main(int argc, char *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
init_gl(&state.gl);
|
||||
state.renderer = wlr_renderer_init();
|
||||
state.cat_texture = wlr_surface_init();
|
||||
wlr_surface_attach_pixels(state.cat_texture, GL_RGB,
|
||||
cat_tex.width, cat_tex.height, cat_tex.pixel_data);
|
||||
|
||||
bool done = false;
|
||||
struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
|
||||
|
@ -328,8 +239,8 @@ int main(int argc, char *argv[]) {
|
|||
wl_event_loop_dispatch(event_loop, 0);
|
||||
}
|
||||
|
||||
cleanup_gl(&state.gl);
|
||||
|
||||
wlr_renderer_destroy(state.renderer);
|
||||
wlr_surface_destroy(state.cat_texture);
|
||||
wl_event_source_remove(timer);
|
||||
wlr_backend_destroy(wlr);
|
||||
wlr_session_finish(session);
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
#include <GLES3/gl3.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct wlr_texture {
|
||||
struct wlr_surface {
|
||||
bool valid;
|
||||
GLuint tex_id;
|
||||
uint32_t format;
|
||||
int width, height;
|
||||
|
@ -17,7 +18,7 @@ struct wlr_shader {
|
|||
uint32_t format;
|
||||
GLuint vert;
|
||||
GLuint program;
|
||||
struct wl_list link;
|
||||
struct wlr_shader *next;
|
||||
};
|
||||
|
||||
struct wlr_renderer {
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
#include <wayland-server-protocol.h>
|
||||
|
||||
struct wlr_surface;
|
||||
struct wlr_surface *wlr_surface_create();
|
||||
void wlr_surface_attach_pixels(struct wlr_surface *tex, uint32_t format,
|
||||
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 *tex, uint32_t format,
|
||||
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);
|
||||
|
@ -23,7 +23,9 @@ 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 *tex,
|
||||
float x, float y, float width, float height);
|
||||
bool wlr_render_quad(struct wlr_renderer *renderer,
|
||||
struct wlr_surface *surf, float (*transform)[16],
|
||||
float x, float y);
|
||||
void wlr_renderer_destroy(struct wlr_renderer *renderer);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
add_library(wlr-render STATIC
|
||||
matrix.c
|
||||
shader.c
|
||||
surface.c
|
||||
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
|
||||
}
|
|
@ -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;
|
||||
while (shader) {
|
||||
struct wlr_shader *_shader = shader;
|
||||
glDeleteProgram(shader->vert);
|
||||
glDeleteProgram(shader->program);
|
||||
shader = shader->next;
|
||||
free(_shader);
|
||||
}
|
||||
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);
|
||||
}
|
||||
free(shader);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue