mirror of
https://gitlab.freedesktop.org/wayland/weston
synced 2024-09-06 18:05:39 +00:00
![Derek Foreman](/assets/img/avatar_default.png)
If a transformation matrix causes a scale, a rotation not a multiple of 90 degrees or a non-integral translation then textures rendered with it would benefit from bilinear filtering. This test is done in a lazy fashion by examining elements of the matrix to check for a simple pattern that indicates these conditions are met. Signed-off-by: Derek Foreman <derek.foreman@collabora.com>
398 lines
9.7 KiB
C
398 lines
9.7 KiB
C
/*
|
|
* Copyright © 2011 Intel Corporation
|
|
* Copyright © 2012 Collabora, Ltd.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
* a copy of this software and associated documentation files (the
|
|
* "Software"), to deal in the Software without restriction, including
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
* the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice (including the
|
|
* next paragraph) shall be included in all copies or substantial
|
|
* portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include <assert.h>
|
|
#include <float.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
|
|
#include <wayland-server.h>
|
|
#include <libweston/matrix.h>
|
|
|
|
|
|
/*
|
|
* Matrices are stored in column-major order, that is the array indices are:
|
|
* 0 4 8 12
|
|
* 1 5 9 13
|
|
* 2 6 10 14
|
|
* 3 7 11 15
|
|
*/
|
|
|
|
WL_EXPORT void
|
|
weston_matrix_init(struct weston_matrix *matrix)
|
|
{
|
|
static const struct weston_matrix identity = {
|
|
.d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
|
|
.type = 0,
|
|
};
|
|
|
|
memcpy(matrix, &identity, sizeof identity);
|
|
}
|
|
|
|
/* m <- n * m, that is, m is multiplied on the LEFT. */
|
|
WL_EXPORT void
|
|
weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
|
|
{
|
|
struct weston_matrix tmp;
|
|
const float *row, *column;
|
|
div_t d;
|
|
int i, j;
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
tmp.d[i] = 0;
|
|
d = div(i, 4);
|
|
row = m->d + d.quot * 4;
|
|
column = n->d + d.rem;
|
|
for (j = 0; j < 4; j++)
|
|
tmp.d[i] += row[j] * column[j * 4];
|
|
}
|
|
tmp.type = m->type | n->type;
|
|
memcpy(m, &tmp, sizeof tmp);
|
|
}
|
|
|
|
WL_EXPORT void
|
|
weston_matrix_translate(struct weston_matrix *matrix, float x, float y, float z)
|
|
{
|
|
struct weston_matrix translate = {
|
|
.d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 },
|
|
.type = WESTON_MATRIX_TRANSFORM_TRANSLATE,
|
|
};
|
|
|
|
weston_matrix_multiply(matrix, &translate);
|
|
}
|
|
|
|
WL_EXPORT void
|
|
weston_matrix_scale(struct weston_matrix *matrix, float x, float y,float z)
|
|
{
|
|
struct weston_matrix scale = {
|
|
.d = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 },
|
|
.type = WESTON_MATRIX_TRANSFORM_SCALE,
|
|
};
|
|
|
|
weston_matrix_multiply(matrix, &scale);
|
|
}
|
|
|
|
WL_EXPORT void
|
|
weston_matrix_rotate_xy(struct weston_matrix *matrix, float cos, float sin)
|
|
{
|
|
struct weston_matrix translate = {
|
|
.d = { cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
|
|
.type = WESTON_MATRIX_TRANSFORM_ROTATE,
|
|
};
|
|
|
|
weston_matrix_multiply(matrix, &translate);
|
|
}
|
|
|
|
/* v <- m * v */
|
|
WL_EXPORT void
|
|
weston_matrix_transform(const struct weston_matrix *matrix,
|
|
struct weston_vector *v)
|
|
{
|
|
int i, j;
|
|
struct weston_vector t;
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
t.f[i] = 0;
|
|
for (j = 0; j < 4; j++)
|
|
t.f[i] += v->f[j] * matrix->d[i + j * 4];
|
|
}
|
|
|
|
*v = t;
|
|
}
|
|
|
|
static inline void
|
|
swap_rows(double *a, double *b)
|
|
{
|
|
unsigned k;
|
|
double tmp;
|
|
|
|
for (k = 0; k < 13; k += 4) {
|
|
tmp = a[k];
|
|
a[k] = b[k];
|
|
b[k] = tmp;
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
swap_unsigned(unsigned *a, unsigned *b)
|
|
{
|
|
unsigned tmp;
|
|
|
|
tmp = *a;
|
|
*a = *b;
|
|
*b = tmp;
|
|
}
|
|
|
|
static inline unsigned
|
|
find_pivot(double *column, unsigned k)
|
|
{
|
|
unsigned p = k;
|
|
for (++k; k < 4; ++k)
|
|
if (fabs(column[p]) < fabs(column[k]))
|
|
p = k;
|
|
|
|
return p;
|
|
}
|
|
|
|
/*
|
|
* reference: Gene H. Golub and Charles F. van Loan. Matrix computations.
|
|
* 3rd ed. The Johns Hopkins University Press. 1996.
|
|
* LU decomposition, forward and back substitution: Chapter 3.
|
|
*/
|
|
|
|
static int
|
|
matrix_invert(double *A, unsigned *p, const struct weston_matrix *matrix)
|
|
{
|
|
unsigned i, j, k;
|
|
unsigned pivot;
|
|
double pv;
|
|
|
|
for (i = 0; i < 4; ++i)
|
|
p[i] = i;
|
|
for (i = 16; i--; )
|
|
A[i] = matrix->d[i];
|
|
|
|
/* LU decomposition with partial pivoting */
|
|
for (k = 0; k < 4; ++k) {
|
|
pivot = find_pivot(&A[k * 4], k);
|
|
if (pivot != k) {
|
|
swap_unsigned(&p[k], &p[pivot]);
|
|
swap_rows(&A[k], &A[pivot]);
|
|
}
|
|
|
|
pv = A[k * 4 + k];
|
|
if (fabs(pv) < 1e-9)
|
|
return -1; /* zero pivot, not invertible */
|
|
|
|
for (i = k + 1; i < 4; ++i) {
|
|
A[i + k * 4] /= pv;
|
|
|
|
for (j = k + 1; j < 4; ++j)
|
|
A[i + j * 4] -= A[i + k * 4] * A[k + j * 4];
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
inverse_transform(const double *LU, const unsigned *p, float *v)
|
|
{
|
|
/* Solve A * x = v, when we have P * A = L * U.
|
|
* P * A * x = P * v => L * U * x = P * v
|
|
* Let U * x = b, then L * b = P * v.
|
|
*/
|
|
double b[4];
|
|
unsigned j;
|
|
|
|
/* Forward substitution, column version, solves L * b = P * v */
|
|
/* The diagonal of L is all ones, and not explicitly stored. */
|
|
b[0] = v[p[0]];
|
|
b[1] = (double)v[p[1]] - b[0] * LU[1 + 0 * 4];
|
|
b[2] = (double)v[p[2]] - b[0] * LU[2 + 0 * 4];
|
|
b[3] = (double)v[p[3]] - b[0] * LU[3 + 0 * 4];
|
|
b[2] -= b[1] * LU[2 + 1 * 4];
|
|
b[3] -= b[1] * LU[3 + 1 * 4];
|
|
b[3] -= b[2] * LU[3 + 2 * 4];
|
|
|
|
/* backward substitution, column version, solves U * y = b */
|
|
#if 1
|
|
/* hand-unrolled, 25% faster for whole function */
|
|
b[3] /= LU[3 + 3 * 4];
|
|
b[0] -= b[3] * LU[0 + 3 * 4];
|
|
b[1] -= b[3] * LU[1 + 3 * 4];
|
|
b[2] -= b[3] * LU[2 + 3 * 4];
|
|
|
|
b[2] /= LU[2 + 2 * 4];
|
|
b[0] -= b[2] * LU[0 + 2 * 4];
|
|
b[1] -= b[2] * LU[1 + 2 * 4];
|
|
|
|
b[1] /= LU[1 + 1 * 4];
|
|
b[0] -= b[1] * LU[0 + 1 * 4];
|
|
|
|
b[0] /= LU[0 + 0 * 4];
|
|
#else
|
|
for (j = 3; j > 0; --j) {
|
|
unsigned k;
|
|
b[j] /= LU[j + j * 4];
|
|
for (k = 0; k < j; ++k)
|
|
b[k] -= b[j] * LU[k + j * 4];
|
|
}
|
|
|
|
b[0] /= LU[0 + 0 * 4];
|
|
#endif
|
|
|
|
/* the result */
|
|
for (j = 0; j < 4; ++j)
|
|
v[j] = b[j];
|
|
}
|
|
|
|
WL_EXPORT int
|
|
weston_matrix_invert(struct weston_matrix *inverse,
|
|
const struct weston_matrix *matrix)
|
|
{
|
|
double LU[16]; /* column-major */
|
|
unsigned perm[4]; /* permutation */
|
|
unsigned c;
|
|
|
|
if (matrix_invert(LU, perm, matrix) < 0)
|
|
return -1;
|
|
|
|
weston_matrix_init(inverse);
|
|
for (c = 0; c < 4; ++c)
|
|
inverse_transform(LU, perm, &inverse->d[c * 4]);
|
|
inverse->type = matrix->type;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static bool
|
|
near_zero(float a)
|
|
{
|
|
if (fabs(a) > 0.00001)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
static float
|
|
get_el(const struct weston_matrix *matrix, int row, int col)
|
|
{
|
|
assert(row >= 0 && row <= 3);
|
|
assert(col >= 0 && col <= 3);
|
|
|
|
return matrix->d[col * 4 + row];
|
|
}
|
|
|
|
static bool
|
|
near_zero_at(const struct weston_matrix *matrix, int row, int col)
|
|
{
|
|
return near_zero(get_el(matrix, row, col));
|
|
}
|
|
|
|
static bool
|
|
near_pm_one_at(const struct weston_matrix *matrix, int row, int col)
|
|
{
|
|
return near_zero(fabs(get_el(matrix, row, col)) - 1.0);
|
|
}
|
|
|
|
static bool
|
|
near_int_at(const struct weston_matrix *matrix, int row, int col)
|
|
{
|
|
float el = get_el(matrix, row, col);
|
|
|
|
return near_zero(roundf(el) - el);
|
|
}
|
|
|
|
/* Lazy decompose the matrix to figure out whether its operations will
|
|
* cause an image to look ugly without some kind of filtering.
|
|
*
|
|
* while this is a 3D transformation matrix, we only concern ourselves
|
|
* with 2D for this test. We do use some small rounding to try to catch
|
|
* sequences of operations that lead back to a matrix that doesn't
|
|
* require filters.
|
|
*
|
|
* We assume the matrix won't be used to transform a vector with w != 1.0
|
|
*
|
|
* Filtering will be necessary when:
|
|
* a non-integral translation is applied
|
|
* non-affine (perspective) translation is in use
|
|
* any scaling (other than -1) is in use
|
|
* a rotation that isn't a multiple of 90 degrees about Z is present
|
|
*/
|
|
WL_EXPORT bool
|
|
weston_matrix_needs_filtering(const struct weston_matrix *matrix)
|
|
{
|
|
/* check for non-integral X/Y translation - ignore Z */
|
|
if (!near_int_at(matrix, 0, 3) ||
|
|
!near_int_at(matrix, 1, 3))
|
|
return true;
|
|
|
|
/* Any transform matrix that matches this will be non-affine. */
|
|
if (!near_zero_at(matrix, 3, 0) ||
|
|
!near_zero_at(matrix, 3, 1) ||
|
|
!near_zero_at(matrix, 3, 2) ||
|
|
!near_pm_one_at(matrix, 3, 3))
|
|
return true;
|
|
|
|
/* Check for anything that could come from a rotation that isn't
|
|
* around the Z axis:
|
|
* [ ? ? 0 ? ]
|
|
* [ ? ? 0 ? ]
|
|
* [ 0 0 ±1 ? ]
|
|
* [ ? ? ? 1 ]
|
|
* It's not clear that we'd realistically see a -1 in [2][2], but
|
|
* it wouldn't require filtering if we did, so allow it.
|
|
*/
|
|
if (!near_zero_at(matrix, 0, 2) ||
|
|
!near_zero_at(matrix, 1, 2) ||
|
|
!near_zero_at(matrix, 2, 0) ||
|
|
!near_zero_at(matrix, 2, 1) ||
|
|
!near_pm_one_at(matrix, 2, 2))
|
|
return true;
|
|
|
|
/* We've culled the low hanging fruit, now let's match the only
|
|
* matrices left we don't have to filter, before defaulting to
|
|
* filtering.
|
|
*
|
|
* These are a combination of testing rotation and scaling at once: */
|
|
if (near_pm_one_at(matrix, 0, 0)) {
|
|
/* This could be a multiple of 90 degree rotation about Z,
|
|
* possibly with a flip, if the matrix is of the form:
|
|
* [ ±1 0 0 ? ]
|
|
* [ 0 ±1 0 ? ]
|
|
* [ 0 0 1 ? ]
|
|
* [ 0 0 0 1 ]
|
|
* Forcing ±1 excludes non-unity scale.
|
|
*/
|
|
if (near_zero_at(matrix, 1, 0) &&
|
|
near_zero_at(matrix, 0, 1) &&
|
|
near_pm_one_at(matrix, 1, 1))
|
|
return false;
|
|
}
|
|
if (near_zero_at(matrix, 0, 0)) {
|
|
/* This could be a multiple of 90 degree rotation about Z,
|
|
* possibly with a flip, if the matrix is of the form:
|
|
* [ 0 ±1 0 ? ]
|
|
* [ ±1 0 0 ? ]
|
|
* [ 0 0 1 ? ]
|
|
* [ 0 0 0 1 ]
|
|
* Forcing ±1 excludes non-unity scale.
|
|
*/
|
|
if (near_zero_at(matrix, 1, 1) &&
|
|
near_pm_one_at(matrix, 1, 0) &&
|
|
near_pm_one_at(matrix, 0, 1))
|
|
return false;
|
|
}
|
|
|
|
/* The matrix wasn't "simple" enough to classify with dumb
|
|
* heuristics, so recommend filtering */
|
|
return true;
|
|
}
|