2017-06-08 21:52:42 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2017-06-23 17:38:45 +02:00
|
|
|
#include <GLES2/gl2.h>
|
2017-06-08 21:52:42 +02:00
|
|
|
#include <wayland-util.h>
|
|
|
|
#include <wayland-server-protocol.h>
|
|
|
|
#include <wlr/render.h>
|
|
|
|
#include <wlr/render/interface.h>
|
|
|
|
#include <wlr/render/matrix.h>
|
2017-06-21 18:10:07 +02:00
|
|
|
#include <wlr/util/log.h>
|
2017-06-23 17:38:45 +02:00
|
|
|
#include "render/gles2.h"
|
2017-06-08 21:52:42 +02:00
|
|
|
|
2017-06-23 20:25:55 +02:00
|
|
|
struct shaders shaders;
|
2017-06-08 21:52:42 +02:00
|
|
|
|
|
|
|
static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) {
|
2017-06-09 16:28:50 +02:00
|
|
|
*shader = GL_CALL(glCreateShader(type));
|
2017-06-08 21:52:42 +02:00
|
|
|
int len = strlen(src);
|
2017-06-09 16:28:50 +02:00
|
|
|
GL_CALL(glShaderSource(*shader, 1, &src, &len));
|
|
|
|
GL_CALL(glCompileShader(*shader));
|
2017-06-08 21:52:42 +02:00
|
|
|
GLint success;
|
2017-06-09 16:28:50 +02:00
|
|
|
GL_CALL(glGetShaderiv(*shader, GL_COMPILE_STATUS, &success));
|
2017-06-08 21:52:42 +02:00
|
|
|
if (success == GL_FALSE) {
|
|
|
|
GLint loglen;
|
2017-06-09 16:28:50 +02:00
|
|
|
GL_CALL(glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &loglen));
|
2017-06-08 21:52:42 +02:00
|
|
|
GLchar msg[loglen];
|
2017-06-09 16:28:50 +02:00
|
|
|
GL_CALL(glGetShaderInfoLog(*shader, loglen, &loglen, msg));
|
2017-06-15 21:31:13 +02:00
|
|
|
wlr_log(L_ERROR, "Shader compilation failed");
|
|
|
|
wlr_log(L_ERROR, "%s", msg);
|
|
|
|
exit(1);
|
2017-06-08 21:52:42 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-06-09 16:28:50 +02:00
|
|
|
*program = GL_CALL(glCreateProgram());
|
|
|
|
GL_CALL(glAttachShader(*program, vertex));
|
|
|
|
GL_CALL(glAttachShader(*program, fragment));
|
|
|
|
GL_CALL(glLinkProgram(*program));
|
2017-06-08 21:52:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_default_shaders() {
|
|
|
|
if (shaders.initialized) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-23 20:25:55 +02:00
|
|
|
if (!compile_program(vertex_src, fragment_src_rgba, &shaders.rgba)) {
|
2017-06-08 21:52:42 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2017-06-23 20:25:55 +02:00
|
|
|
if (!compile_program(vertex_src, fragment_src_rgbx, &shaders.rgbx)) {
|
2017-06-08 21:52:42 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2017-06-15 21:31:13 +02:00
|
|
|
if (!compile_program(quad_vertex_src, quad_fragment_src, &shaders.quad)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!compile_program(quad_vertex_src, ellipse_fragment_src, &shaders.ellipse)) {
|
|
|
|
goto error;
|
|
|
|
}
|
2017-06-20 21:29:27 +02:00
|
|
|
wlr_log(L_DEBUG, "Compiled default shaders");
|
2017-06-08 21:52:42 +02:00
|
|
|
return;
|
|
|
|
error:
|
|
|
|
wlr_log(L_ERROR, "Failed to set up default shaders!");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_globals() {
|
|
|
|
init_default_shaders();
|
|
|
|
}
|
|
|
|
|
2017-06-23 17:38:45 +02:00
|
|
|
static void wlr_gles2_begin(struct wlr_renderer_state *state,
|
2017-06-08 21:52:42 +02:00
|
|
|
struct wlr_output *output) {
|
|
|
|
// TODO: let users customize the clear color?
|
2017-06-09 16:28:50 +02:00
|
|
|
GL_CALL(glClearColor(0.25f, 0.25f, 0.25f, 1));
|
|
|
|
GL_CALL(glClear(GL_COLOR_BUFFER_BIT));
|
2017-06-08 21:52:42 +02:00
|
|
|
int32_t width = output->width;
|
|
|
|
int32_t height = output->height;
|
2017-06-09 16:28:50 +02:00
|
|
|
GL_CALL(glViewport(0, 0, width, height));
|
2017-06-08 21:52:42 +02:00
|
|
|
// Note: maybe we should save output projection and remove some of the need
|
|
|
|
// for users to sling matricies themselves
|
|
|
|
}
|
|
|
|
|
2017-06-23 17:38:45 +02:00
|
|
|
static void wlr_gles2_end(struct wlr_renderer_state *state) {
|
2017-06-08 21:52:42 +02:00
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
2017-08-08 18:02:14 +02:00
|
|
|
static struct wlr_texture *wlr_gles2_texture_init(struct wlr_renderer_state *state) {
|
|
|
|
return gles2_texture_init();
|
2017-06-23 17:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2017-06-08 21:52:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 18:02:14 +02:00
|
|
|
static bool wlr_gles2_render_texture(struct wlr_renderer_state *state,
|
|
|
|
struct wlr_texture *texture, const float (*matrix)[16]) {
|
|
|
|
if(!texture || !texture->valid) {
|
|
|
|
wlr_log(L_ERROR, "attempt to render invalid texture");
|
2017-07-17 20:38:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-08 18:02:14 +02:00
|
|
|
wlr_texture_bind(texture);
|
2017-06-23 17:46:03 +02:00
|
|
|
GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, *matrix));
|
2017-06-23 20:25:55 +02:00
|
|
|
// TODO: source alpha from somewhere else I guess
|
|
|
|
GL_CALL(glUniform1f(2, 1.0f));
|
2017-06-23 17:38:45 +02:00
|
|
|
draw_quad();
|
2017-06-08 21:52:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-23 17:38:45 +02:00
|
|
|
static void wlr_gles2_render_quad(struct wlr_renderer_state *state,
|
2017-06-15 21:31:13 +02:00
|
|
|
const float (*color)[4], const float (*matrix)[16]) {
|
|
|
|
GL_CALL(glUseProgram(shaders.quad));
|
|
|
|
GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
|
|
|
|
GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3]));
|
2017-06-23 17:38:45 +02:00
|
|
|
draw_quad();
|
2017-06-15 21:31:13 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 17:38:45 +02:00
|
|
|
static void wlr_gles2_render_ellipse(struct wlr_renderer_state *state,
|
2017-06-15 21:31:13 +02:00
|
|
|
const float (*color)[4], const float (*matrix)[16]) {
|
|
|
|
GL_CALL(glUseProgram(shaders.ellipse));
|
|
|
|
GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
|
|
|
|
GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3]));
|
2017-06-23 17:38:45 +02:00
|
|
|
draw_quad();
|
2017-06-15 21:31:13 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 22:32:47 +02:00
|
|
|
static const enum wl_shm_format *wlr_gles2_formats(
|
|
|
|
struct wlr_renderer_state *state, size_t *len) {
|
|
|
|
static enum wl_shm_format formats[] = {
|
|
|
|
WL_SHM_FORMAT_ARGB8888,
|
|
|
|
WL_SHM_FORMAT_XRGB8888,
|
2017-06-23 20:25:55 +02:00
|
|
|
WL_SHM_FORMAT_ABGR8888,
|
|
|
|
WL_SHM_FORMAT_XBGR8888,
|
2017-06-22 22:32:47 +02:00
|
|
|
};
|
|
|
|
*len = sizeof(formats) / sizeof(formats[0]);
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2017-06-23 17:38:45 +02:00
|
|
|
static void wlr_gles2_destroy(struct wlr_renderer_state *state) {
|
2017-06-08 21:52:42 +02:00
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wlr_renderer_impl wlr_renderer_impl = {
|
2017-06-23 17:38:45 +02:00
|
|
|
.begin = wlr_gles2_begin,
|
|
|
|
.end = wlr_gles2_end,
|
2017-08-08 18:02:14 +02:00
|
|
|
.texture_init = wlr_gles2_texture_init,
|
|
|
|
.render_with_matrix = wlr_gles2_render_texture,
|
2017-06-23 17:38:45 +02:00
|
|
|
.render_quad = wlr_gles2_render_quad,
|
|
|
|
.render_ellipse = wlr_gles2_render_ellipse,
|
2017-06-22 22:32:47 +02:00
|
|
|
.formats = wlr_gles2_formats,
|
2017-06-23 17:38:45 +02:00
|
|
|
.destroy = wlr_gles2_destroy
|
2017-06-08 21:52:42 +02:00
|
|
|
};
|
|
|
|
|
2017-06-23 17:38:45 +02:00
|
|
|
struct wlr_renderer *wlr_gles2_renderer_init() {
|
2017-06-08 21:52:42 +02:00
|
|
|
init_globals();
|
|
|
|
return wlr_renderer_init(NULL, &wlr_renderer_impl);
|
|
|
|
}
|