Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 580777b

Browse files
author
John O'Neil
committed
Provide a matrix inverse shim for GLES 2.0.
1 parent b0753c0 commit 580777b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

impeller/compiler/shader_lib/impeller/gradient.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#define GRADIENT_GLSL_
77

88
#include <impeller/texture.glsl>
9+
#include <impeller/transform.glsl>
910

1011
mat3 IPMapToUnitX(vec2 p0, vec2 p1) {
1112
// Returns a matrix that maps [p0, p1] to [(0, 0), (1, 0)]. Results are
1213
// undefined if p0 = p1.
1314
return mat3(0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0) *
14-
inverse(mat3(p1.y - p0.y, p0.x - p1.x, 0.0, p1.x - p0.x, p1.y - p0.y,
15+
IPMat3Inverse(mat3(p1.y - p0.y, p0.x - p1.x, 0.0, p1.x - p0.x, p1.y - p0.y,
1516
0.0, p0.x, p0.y, 1.0));
1617
}
1718

impeller/compiler/shader_lib/impeller/transform.glsl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,32 @@ vec4 IPPositionForGlyphPosition(mat4 mvp,
2626
unit_position.y * destination_size.y, 0.0, 1.0);
2727
}
2828

29-
#endif
29+
#ifdef IMPELLER_TARGET_OPENGLES
30+
31+
// Shim matrix `inverse` for versions that lack it.
32+
// TODO: This could be gated on GLSL < 1.4.
33+
mat3 IPMat3Inverse( mat3 m) {
34+
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
35+
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
36+
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
37+
38+
float b01 = a22 * a11 - a12 * a21;
39+
float b11 = -a22 * a10 + a12 * a20;
40+
float b21 = a21 * a10 - a11 * a20;
41+
42+
float det = a00 * b01 + a01 * b11 + a02 * b21;
43+
44+
return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
45+
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
46+
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
47+
}
48+
49+
#else // IMPELLER_TARGET_OPENGLES
50+
51+
mat3 IPMat3Inverse(mat3 m) {
52+
return inverse(m);
53+
}
54+
55+
#endif // IMPELLER_TARGET_OPENGLES
56+
57+
#endif // TRANSFORM_GLSL_

0 commit comments

Comments
 (0)