Merge pull request #722 from emersion/matrix-redesign

Matrix redesign
This commit is contained in:
Drew DeVault 2018-03-17 15:22:00 -04:00 committed by GitHub
commit 1956d3cedb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 334 additions and 468 deletions

View file

@ -9,7 +9,7 @@
#include <wlr/render.h>
#include <wlr/render/egl.h>
#include <wlr/render/interface.h>
#include <wlr/render/matrix.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/util/log.h>
#include "render/gles2.h"
#include "glapi.h"
@ -85,11 +85,13 @@ static void init_default_shaders() {
if (!compile_program(quad_vertex_src, quad_fragment_src, &shaders.quad)) {
goto error;
}
if (!compile_program(quad_vertex_src, ellipse_fragment_src, &shaders.ellipse)) {
if (!compile_program(quad_vertex_src, ellipse_fragment_src,
&shaders.ellipse)) {
goto error;
}
if (glEGLImageTargetTexture2DOES) {
if (!compile_program(quad_vertex_src, fragment_src_external, &shaders.external)) {
if (!compile_program(quad_vertex_src, fragment_src_external,
&shaders.external)) {
goto error;
}
}
@ -122,8 +124,8 @@ static void wlr_gles2_end(struct wlr_renderer *wlr_renderer) {
}
static void wlr_gles2_clear(struct wlr_renderer *wlr_renderer,
const float (*color)[4]) {
glClearColor((*color)[0], (*color)[1], (*color)[2], (*color)[3]);
const float color[static 4]) {
glClearColor(color[0], color[1], color[2], color[3]);
glClear(GL_COLOR_BUFFER_BIT);
}
@ -170,15 +172,16 @@ static void draw_quad() {
GL_CALL(glDisableVertexAttribArray(1));
}
static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer,
struct wlr_texture *texture, const float (*matrix)[16], float alpha) {
static bool wlr_gles2_render_texture_with_matrix(
struct wlr_renderer *wlr_renderer, struct wlr_texture *texture,
const float matrix[static 9], float alpha) {
if (!texture || !texture->valid) {
wlr_log(L_ERROR, "attempt to render invalid texture");
return false;
}
wlr_texture_bind(texture);
GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, *matrix));
GL_CALL(glUniformMatrix3fv(0, 1, GL_TRUE, matrix));
GL_CALL(glUniform1i(1, texture->inverted_y));
GL_CALL(glUniform1f(3, alpha));
draw_quad();
@ -187,18 +190,18 @@ static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer,
static void wlr_gles2_render_quad(struct wlr_renderer *wlr_renderer,
const float (*color)[4], const float (*matrix)[16]) {
const float color[static 4], const float matrix[static 9]) {
GL_CALL(glUseProgram(shaders.quad));
GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, *matrix));
GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3]));
GL_CALL(glUniformMatrix3fv(0, 1, GL_TRUE, matrix));
GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3]));
draw_quad();
}
static void wlr_gles2_render_ellipse(struct wlr_renderer *wlr_renderer,
const float (*color)[4], const float (*matrix)[16]) {
const float color[static 4], const float matrix[static 9]) {
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]));
GL_CALL(glUniformMatrix3fv(0, 1, GL_TRUE, matrix));
GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3]));
draw_quad();
}
@ -258,7 +261,7 @@ static struct wlr_renderer_impl wlr_renderer_impl = {
.clear = wlr_gles2_clear,
.scissor = wlr_gles2_scissor,
.texture_create = wlr_gles2_texture_create,
.render_with_matrix = wlr_gles2_render_texture,
.render_texture_with_matrix = wlr_gles2_render_texture_with_matrix,
.render_quad = wlr_gles2_render_quad,
.render_ellipse = wlr_gles2_render_ellipse,
.formats = wlr_gles2_formats,

View file

@ -3,35 +3,24 @@
// Colored quads
const GLchar quad_vertex_src[] =
"uniform mat4 proj;"
"uniform mat3 proj;"
"uniform vec4 color;"
"attribute vec2 pos;"
"attribute vec2 texcoord;"
"varying vec4 v_color;"
"varying vec2 v_texcoord;"
"mat4 transpose(in mat4 inMatrix) {"
" vec4 i0 = inMatrix[0];"
" vec4 i1 = inMatrix[1];"
" vec4 i2 = inMatrix[2];"
" vec4 i3 = inMatrix[3];"
" mat4 outMatrix = mat4("
" vec4(i0.x, i1.x, i2.x, i3.x),"
" vec4(i0.y, i1.y, i2.y, i3.y),"
" vec4(i0.z, i1.z, i2.z, i3.z),"
" vec4(i0.w, i1.w, i2.w, i3.w)"
" );"
" return outMatrix;"
"}"
""
"void main() {"
" gl_Position = transpose(proj) * vec4(pos, 0.0, 1.0);"
" v_color = color;"
" v_texcoord = texcoord;"
" gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);"
" v_color = color;"
" v_texcoord = texcoord;"
"}";
const GLchar quad_fragment_src[] =
"precision mediump float;"
"varying vec4 v_color;"
"varying vec2 v_texcoord;"
""
"void main() {"
" gl_FragColor = v_color;"
"}";
@ -41,6 +30,7 @@ const GLchar ellipse_fragment_src[] =
"precision mediump float;"
"varying vec4 v_color;"
"varying vec2 v_texcoord;"
""
"void main() {"
" float l = length(v_texcoord - vec2(0.5, 0.5));"
" if (l > 0.5) discard;"
@ -49,26 +39,14 @@ const GLchar ellipse_fragment_src[] =
// Textured quads
const GLchar vertex_src[] =
"uniform mat4 proj;"
"uniform mat3 proj;"
"uniform bool invert_y;"
"attribute vec2 pos;"
"attribute vec2 texcoord;"
"varying vec2 v_texcoord;"
"mat4 transpose(in mat4 inMatrix) {"
" vec4 i0 = inMatrix[0];"
" vec4 i1 = inMatrix[1];"
" vec4 i2 = inMatrix[2];"
" vec4 i3 = inMatrix[3];"
" mat4 outMatrix = mat4("
" vec4(i0.x, i1.x, i2.x, i3.x),"
" vec4(i0.y, i1.y, i2.y, i3.y),"
" vec4(i0.z, i1.z, i2.z, i3.z),"
" vec4(i0.w, i1.w, i2.w, i3.w)"
" );"
" return outMatrix;"
"}"
""
"void main() {"
" gl_Position = transpose(proj) * vec4(pos, 0.0, 1.0);"
" gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);"
" if (invert_y) {"
" v_texcoord = vec2(texcoord.s, 1.0 - texcoord.t);"
" } else {"
@ -81,8 +59,9 @@ const GLchar fragment_src_rgba[] =
"varying vec2 v_texcoord;"
"uniform sampler2D tex;"
"uniform float alpha;"
""
"void main() {"
" gl_FragColor = alpha * texture2D(tex, v_texcoord);"
" gl_FragColor = alpha * texture2D(tex, v_texcoord);"
"}";
const GLchar fragment_src_rgbx[] =
@ -90,9 +69,10 @@ const GLchar fragment_src_rgbx[] =
"varying vec2 v_texcoord;"
"uniform sampler2D tex;"
"uniform float alpha;"
""
"void main() {"
" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;"
" gl_FragColor.a = alpha;"
" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;"
" gl_FragColor.a = alpha;"
"}";
const GLchar fragment_src_external[] =
@ -100,7 +80,8 @@ const GLchar fragment_src_external[] =
"precision mediump float;"
"varying vec2 v_texcoord;"
"uniform samplerExternalOES texture0;"
""
"void main() {"
" vec4 col = texture2D(texture0, v_texcoord);"
" gl_FragColor = vec4(col.rgb, col.a);"
" vec4 col = texture2D(texture0, v_texcoord);"
" gl_FragColor = vec4(col.rgb, col.a);"
"}";

View file

@ -8,7 +8,7 @@
#include <wlr/render.h>
#include <wlr/render/egl.h>
#include <wlr/render/interface.h>
#include <wlr/render/matrix.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/util/log.h>
#include "render/gles2.h"
#include "util/signal.h"
@ -229,7 +229,6 @@ static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex,
return true;
}
static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex,
struct wl_resource *dmabuf_resource) {
struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)_tex;
@ -265,20 +264,6 @@ static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex,
return true;
}
static void gles2_texture_get_matrix(struct wlr_texture *_texture,
float (*matrix)[16], const float (*projection)[16], int x, int y) {
struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture;
float world[16];
wlr_matrix_identity(matrix);
wlr_matrix_translate(&world, x, y, 0);
wlr_matrix_mul(matrix, &world, matrix);
wlr_matrix_scale(&world,
texture->wlr_texture.width, texture->wlr_texture.height, 1);
wlr_matrix_mul(matrix, &world, matrix);
wlr_matrix_mul(projection, matrix, matrix);
}
static bool gles2_texture_get_dmabuf_size(struct wlr_texture *texture, struct
wl_resource *resource, int *width, int *height) {
if (!wlr_dmabuf_resource_is_buffer(resource)) {
@ -292,7 +277,6 @@ static bool gles2_texture_get_dmabuf_size(struct wlr_texture *texture, struct
return true;
}
static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct
wl_resource *resource, int *width, int *height) {
struct wl_shm_buffer *buffer = wl_shm_buffer_get(resource);
@ -349,7 +333,6 @@ static struct wlr_texture_impl wlr_texture_impl = {
.upload_drm = gles2_texture_upload_drm,
.upload_dmabuf = gles2_texture_upload_dmabuf,
.upload_eglimage = gles2_texture_upload_eglimage,
.get_matrix = gles2_texture_get_matrix,
.get_buffer_size = gles2_texture_get_buffer_size,
.bind = gles2_texture_bind,
.destroy = gles2_texture_destroy,

View file

@ -1,210 +0,0 @@
#include <math.h>
#include <string.h>
#include <wayland-server-protocol.h>
#include <wlr/render/matrix.h>
#include <wlr/types/wlr_box.h>
#include <wlr/types/wlr_output.h>
/* Obtains the index for the given row/column */
static inline int mind(int row, int col) {
return (row - 1) * 4 + col - 1;
}
void wlr_matrix_identity(float (*output)[16]) {
static const float identity[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
memcpy(*output, identity, sizeof(identity));
}
void wlr_matrix_translate(float (*output)[16], float x, float y, float z) {
wlr_matrix_identity(output);
(*output)[mind(1, 4)] = x;
(*output)[mind(2, 4)] = y;
(*output)[mind(3, 4)] = z;
}
void wlr_matrix_scale(float (*output)[16], float x, float y, float z) {
wlr_matrix_identity(output);
(*output)[mind(1, 1)] = x;
(*output)[mind(2, 2)] = y;
(*output)[mind(3, 3)] = z;
}
void wlr_matrix_rotate(float (*output)[16], float radians) {
wlr_matrix_identity(output);
float _cos = cosf(radians);
float _sin = sinf(radians);
(*output)[mind(1, 1)] = _cos;
(*output)[mind(1, 2)] = _sin;
(*output)[mind(2, 1)] = -_sin;
(*output)[mind(2, 2)] = _cos;
}
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)],
(*x)[mind(1, 1)] * (*y)[mind(1, 2)] + (*x)[mind(1, 2)] * (*y)[mind(2, 2)] +
(*x)[mind(1, 3)] * (*y)[mind(3, 2)] + (*x)[mind(1, 4)] * (*y)[mind(4, 2)],
(*x)[mind(1, 1)] * (*y)[mind(1, 3)] + (*x)[mind(1, 2)] * (*y)[mind(2, 3)] +
(*x)[mind(1, 3)] * (*y)[mind(3, 3)] + (*x)[mind(1, 4)] * (*y)[mind(4, 3)],
(*x)[mind(1, 1)] * (*y)[mind(1, 4)] + (*x)[mind(1, 2)] * (*y)[mind(2, 4)] +
(*x)[mind(1, 4)] * (*y)[mind(3, 4)] + (*x)[mind(1, 4)] * (*y)[mind(4, 4)],
(*x)[mind(2, 1)] * (*y)[mind(1, 1)] + (*x)[mind(2, 2)] * (*y)[mind(2, 1)] +
(*x)[mind(2, 3)] * (*y)[mind(3, 1)] + (*x)[mind(2, 4)] * (*y)[mind(4, 1)],
(*x)[mind(2, 1)] * (*y)[mind(1, 2)] + (*x)[mind(2, 2)] * (*y)[mind(2, 2)] +
(*x)[mind(2, 3)] * (*y)[mind(3, 2)] + (*x)[mind(2, 4)] * (*y)[mind(4, 2)],
(*x)[mind(2, 1)] * (*y)[mind(1, 3)] + (*x)[mind(2, 2)] * (*y)[mind(2, 3)] +
(*x)[mind(2, 3)] * (*y)[mind(3, 3)] + (*x)[mind(2, 4)] * (*y)[mind(4, 3)],
(*x)[mind(2, 1)] * (*y)[mind(1, 4)] + (*x)[mind(2, 2)] * (*y)[mind(2, 4)] +
(*x)[mind(2, 4)] * (*y)[mind(3, 4)] + (*x)[mind(2, 4)] * (*y)[mind(4, 4)],
(*x)[mind(3, 1)] * (*y)[mind(1, 1)] + (*x)[mind(3, 2)] * (*y)[mind(2, 1)] +
(*x)[mind(3, 3)] * (*y)[mind(3, 1)] + (*x)[mind(3, 4)] * (*y)[mind(4, 1)],
(*x)[mind(3, 1)] * (*y)[mind(1, 2)] + (*x)[mind(3, 2)] * (*y)[mind(2, 2)] +
(*x)[mind(3, 3)] * (*y)[mind(3, 2)] + (*x)[mind(3, 4)] * (*y)[mind(4, 2)],
(*x)[mind(3, 1)] * (*y)[mind(1, 3)] + (*x)[mind(3, 2)] * (*y)[mind(2, 3)] +
(*x)[mind(3, 3)] * (*y)[mind(3, 3)] + (*x)[mind(3, 4)] * (*y)[mind(4, 3)],
(*x)[mind(3, 1)] * (*y)[mind(1, 4)] + (*x)[mind(3, 2)] * (*y)[mind(2, 4)] +
(*x)[mind(3, 4)] * (*y)[mind(3, 4)] + (*x)[mind(3, 4)] * (*y)[mind(4, 4)],
(*x)[mind(4, 1)] * (*y)[mind(1, 1)] + (*x)[mind(4, 2)] * (*y)[mind(2, 1)] +
(*x)[mind(4, 3)] * (*y)[mind(3, 1)] + (*x)[mind(4, 4)] * (*y)[mind(4, 1)],
(*x)[mind(4, 1)] * (*y)[mind(1, 2)] + (*x)[mind(4, 2)] * (*y)[mind(2, 2)] +
(*x)[mind(4, 3)] * (*y)[mind(3, 2)] + (*x)[mind(4, 4)] * (*y)[mind(4, 2)],
(*x)[mind(4, 1)] * (*y)[mind(1, 3)] + (*x)[mind(4, 2)] * (*y)[mind(2, 3)] +
(*x)[mind(4, 3)] * (*y)[mind(3, 3)] + (*x)[mind(4, 4)] * (*y)[mind(4, 3)],
(*x)[mind(4, 1)] * (*y)[mind(1, 4)] + (*x)[mind(4, 2)] * (*y)[mind(2, 4)] +
(*x)[mind(4, 4)] * (*y)[mind(3, 4)] + (*x)[mind(4, 4)] * (*y)[mind(4, 4)],
};
memcpy(*product, _product, sizeof(_product));
}
static const float transforms[][4] = {
[WL_OUTPUT_TRANSFORM_NORMAL] = {
1.0f, 0.0f,
0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_90] = {
0.0f, -1.0f,
1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_180] = {
-1.0f, 0.0f,
0.0f, -1.0f,
},
[WL_OUTPUT_TRANSFORM_270] = {
0.0f, 1.0f,
-1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED] = {
-1.0f, 0.0f,
0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_90] = {
0.0f, -1.0f,
-1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_180] = {
1.0f, 0.0f,
0.0f, -1.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_270] = {
0.0f, 1.0f,
1.0f, 0.0f,
},
};
void wlr_matrix_transform(float mat[static 16],
enum wl_output_transform transform) {
memset(mat, 0, sizeof(*mat) * 16);
const float *t = transforms[transform];
// Rotation + reflection
mat[0] = t[0];
mat[1] = t[1];
mat[4] = t[2];
mat[5] = t[3];
// Identity
mat[10] = 1.0f;
mat[15] = 1.0f;
}
// Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied
void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height,
enum wl_output_transform transform) {
memset(mat, 0, sizeof(*mat) * 16);
const float *t = transforms[transform];
float x = 2.0f / width;
float y = 2.0f / height;
// Rotation + reflection
mat[0] = x * t[0];
mat[1] = x * t[1];
mat[4] = y * -t[2];
mat[5] = y * -t[3];
// Translation
mat[3] = -copysign(1.0f, mat[0] + mat[1]);
mat[7] = -copysign(1.0f, mat[4] + mat[5]);
// Identity
mat[10] = 1.0f;
mat[15] = 1.0f;
}
void wlr_matrix_project_box(float (*mat)[16], struct wlr_box *box,
enum wl_output_transform transform, float rotation,
float (*projection)[16]) {
int x = box->x;
int y = box->y;
int width = box->width;
int height = box->height;
wlr_matrix_translate(mat, x, y, 0);
if (rotation != 0) {
float translate_center[16];
wlr_matrix_translate(&translate_center, width/2, height/2, 0);
float rotate[16];
wlr_matrix_rotate(&rotate, rotation);
float translate_origin[16];
wlr_matrix_translate(&translate_origin, -width/2, -height/2, 0);
wlr_matrix_mul(mat, &translate_center, mat);
wlr_matrix_mul(mat, &rotate, mat);
wlr_matrix_mul(mat, &translate_origin, mat);
}
float scale[16];
wlr_matrix_scale(&scale, width, height, 1);
wlr_matrix_mul(mat, &scale, mat);
if (transform != WL_OUTPUT_TRANSFORM_NORMAL) {
float surface_translate_center[16];
wlr_matrix_translate(&surface_translate_center, 0.5, 0.5, 0);
float surface_transform[16];
wlr_matrix_transform(surface_transform, transform);
float surface_translate_origin[16];
wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0);
wlr_matrix_mul(mat, &surface_translate_center, mat);
wlr_matrix_mul(mat, &surface_transform, mat);
wlr_matrix_mul(mat, &surface_translate_origin, mat);
}
wlr_matrix_mul(projection, mat, mat);
}

View file

@ -15,7 +15,6 @@ lib_wlr_render = static_library(
'gles2/shaders.c',
'gles2/texture.c',
'gles2/util.c',
'matrix.c',
'wlr_renderer.c',
'wlr_texture.c',
),

View file

@ -1,6 +1,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <wlr/render/interface.h>
#include <wlr/types/wlr_matrix.h>
void wlr_renderer_init(struct wlr_renderer *renderer,
struct wlr_renderer_impl *impl) {
@ -23,7 +24,7 @@ void wlr_renderer_end(struct wlr_renderer *r) {
r->impl->end(r);
}
void wlr_renderer_clear(struct wlr_renderer *r, const float (*color)[4]) {
void wlr_renderer_clear(struct wlr_renderer *r, const float color[static 4]) {
r->impl->clear(r, color);
}
@ -35,18 +36,30 @@ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r) {
return r->impl->texture_create(r);
}
bool wlr_render_with_matrix(struct wlr_renderer *r,
struct wlr_texture *texture, const float (*matrix)[16], float alpha) {
return r->impl->render_with_matrix(r, texture, matrix, alpha);
bool wlr_render_texture(struct wlr_renderer *r, struct wlr_texture *texture,
const float projection[static 9], int x, int y, float alpha) {
float mat[9];
wlr_matrix_identity(mat);
wlr_matrix_translate(mat, x, y);
wlr_matrix_scale(mat, texture->width, texture->height);
wlr_matrix_multiply(mat, projection, mat);
return wlr_render_texture_with_matrix(r, texture, mat, alpha);
}
bool wlr_render_texture_with_matrix(struct wlr_renderer *r,
struct wlr_texture *texture, const float matrix[static 9],
float alpha) {
return r->impl->render_texture_with_matrix(r, texture, matrix, alpha);
}
void wlr_render_colored_quad(struct wlr_renderer *r,
const float (*color)[4], const float (*matrix)[16]) {
const float color[static 4], const float matrix[static 9]) {
r->impl->render_quad(r, color, matrix);
}
void wlr_render_colored_ellipse(struct wlr_renderer *r,
const float (*color)[4], const float (*matrix)[16]) {
const float color[static 4], const float matrix[static 9]) {
r->impl->render_ellipse(r, color, matrix);
}

View file

@ -58,11 +58,6 @@ bool wlr_texture_upload_dmabuf(struct wlr_texture *texture,
return texture->impl->upload_dmabuf(texture, dmabuf_resource);
}
void wlr_texture_get_matrix(struct wlr_texture *texture,
float (*matrix)[16], const float (*projection)[16], int x, int y) {
texture->impl->get_matrix(texture, matrix, projection, x, y);
}
void wlr_texture_get_buffer_size(struct wlr_texture *texture, struct wl_resource
*resource, int *width, int *height) {
texture->impl->get_buffer_size(texture, resource, width, height);