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

[web] Optimize Matrix4.identity #22622

Merged
merged 3 commits into from
Dec 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions lib/web_ui/lib/src/engine/vector_math.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ class Matrix4 {
Matrix4.zero() : _m4storage = Float32List(16);

/// Identity matrix.
factory Matrix4.identity() => Matrix4.zero()..setIdentity();
Matrix4.identity() : _m4storage = Float32List(16) {
_m4storage[15] = 1.0;
_m4storage[0] = 1.0;
_m4storage[5] = 1.0;
_m4storage[10] = 1.0;
}

/// Copies values from [other].
factory Matrix4.copy(Matrix4 other) => Matrix4.zero()..setFrom(other);
Expand Down Expand Up @@ -98,14 +103,12 @@ class Matrix4 {
..setRotationZ(radians);

/// Translation matrix.
factory Matrix4.translation(Vector3 translation) => Matrix4.zero()
..setIdentity()
factory Matrix4.translation(Vector3 translation) => Matrix4.identity()
..setTranslation(translation);

/// Translation matrix.
factory Matrix4.translationValues(double x, double y, double z) =>
Matrix4.zero()
..setIdentity()
Matrix4.identity()
..setTranslationRaw(x, y, z);

/// Scale matrix.
Expand Down Expand Up @@ -198,6 +201,9 @@ class Matrix4 {
/// Copy into [arg].
Matrix4 copyInto(Matrix4 arg) {
final Float32List argStorage = arg._m4storage;
// Start reading from the last element to eliminate range checks
// in subsequent reads.
argStorage[15] = _m4storage[15];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the optimization! Shall we also add a comment about the compiler optimization we are doing(why are we accessing the 15th index before all the others)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

argStorage[0] = _m4storage[0];
argStorage[1] = _m4storage[1];
argStorage[2] = _m4storage[2];
Expand All @@ -213,7 +219,6 @@ class Matrix4 {
argStorage[12] = _m4storage[12];
argStorage[13] = _m4storage[13];
argStorage[14] = _m4storage[14];
argStorage[15] = _m4storage[15];
return arg;
}

Expand Down Expand Up @@ -248,6 +253,7 @@ class Matrix4 {
final double sy = y ?? x;
final double sz = z ?? x;
const double sw = 1.0;
_m4storage[15] *= sw;
_m4storage[0] *= sx;
_m4storage[1] *= sx;
_m4storage[2] *= sx;
Expand All @@ -263,7 +269,6 @@ class Matrix4 {
_m4storage[12] *= sw;
_m4storage[13] *= sw;
_m4storage[14] *= sw;
_m4storage[15] *= sw;
}

/// Create a copy of [this] scaled by a [Vector3], [Vector4] or [x],[y], and
Expand All @@ -272,6 +277,7 @@ class Matrix4 {

/// Zeros [this].
void setZero() {
_m4storage[15] = 0.0;
_m4storage[0] = 0.0;
_m4storage[1] = 0.0;
_m4storage[2] = 0.0;
Expand All @@ -287,11 +293,11 @@ class Matrix4 {
_m4storage[12] = 0.0;
_m4storage[13] = 0.0;
_m4storage[14] = 0.0;
_m4storage[15] = 0.0;
}

/// Makes [this] into the identity matrix.
void setIdentity() {
_m4storage[15] = 1.0;
_m4storage[0] = 1.0;
_m4storage[1] = 0.0;
_m4storage[2] = 0.0;
Expand All @@ -307,7 +313,6 @@ class Matrix4 {
_m4storage[12] = 0.0;
_m4storage[13] = 0.0;
_m4storage[14] = 0.0;
_m4storage[15] = 1.0;
}

/// Returns the tranpose of this.
Expand Down Expand Up @@ -337,34 +342,35 @@ class Matrix4 {

/// Returns the determinant of this matrix.
double determinant() {
final Float32List m = _m4storage;
final double det2_01_01 =
_m4storage[0] * _m4storage[5] - _m4storage[1] * _m4storage[4];
m[0] * m[5] - m[1] * m[4];
final double det2_01_02 =
_m4storage[0] * _m4storage[6] - _m4storage[2] * _m4storage[4];
m[0] * m[6] - m[2] * m[4];
final double det2_01_03 =
_m4storage[0] * _m4storage[7] - _m4storage[3] * _m4storage[4];
m[0] * m[7] - m[3] * m[4];
final double det2_01_12 =
_m4storage[1] * _m4storage[6] - _m4storage[2] * _m4storage[5];
m[1] * m[6] - m[2] * m[5];
final double det2_01_13 =
_m4storage[1] * _m4storage[7] - _m4storage[3] * _m4storage[5];
m[1] * m[7] - m[3] * m[5];
final double det2_01_23 =
_m4storage[2] * _m4storage[7] - _m4storage[3] * _m4storage[6];
final double det3_201_012 = _m4storage[8] * det2_01_12 -
_m4storage[9] * det2_01_02 +
_m4storage[10] * det2_01_01;
final double det3_201_013 = _m4storage[8] * det2_01_13 -
_m4storage[9] * det2_01_03 +
_m4storage[11] * det2_01_01;
final double det3_201_023 = _m4storage[8] * det2_01_23 -
_m4storage[10] * det2_01_03 +
_m4storage[11] * det2_01_02;
final double det3_201_123 = _m4storage[9] * det2_01_23 -
_m4storage[10] * det2_01_13 +
_m4storage[11] * det2_01_12;
return -det3_201_123 * _m4storage[12] +
det3_201_023 * _m4storage[13] -
det3_201_013 * _m4storage[14] +
det3_201_012 * _m4storage[15];
m[2] * m[7] - m[3] * m[6];
final double det3_201_012 = m[8] * det2_01_12 -
m[9] * det2_01_02 +
m[10] * det2_01_01;
final double det3_201_013 = m[8] * det2_01_13 -
m[9] * det2_01_03 +
m[11] * det2_01_01;
final double det3_201_023 = m[8] * det2_01_23 -
m[10] * det2_01_03 +
m[11] * det2_01_02;
final double det3_201_123 = m[9] * det2_01_23 -
m[10] * det2_01_13 +
m[11] * det2_01_12;
return -det3_201_123 * m[12] +
det3_201_023 * m[13] -
det3_201_013 * m[14] +
det3_201_012 * m[15];
}

/// Returns a new vector or matrix by multiplying [this] with [arg].
Expand All @@ -387,6 +393,7 @@ class Matrix4 {
/// defined by [this].
Vector3 perspectiveTransform(Vector3 arg) {
final Float32List argStorage = arg._v3storage;

final double x = (_m4storage[0] * argStorage[0]) +
(_m4storage[4] * argStorage[1]) +
(_m4storage[8] * argStorage[2]) +
Expand Down Expand Up @@ -733,6 +740,7 @@ class Matrix4 {

/// Multiply [this] by [arg].
void multiply(Matrix4 arg) {
final double m33 = _m4storage[15];
final double m00 = _m4storage[0];
final double m01 = _m4storage[4];
final double m02 = _m4storage[8];
Expand All @@ -748,8 +756,8 @@ class Matrix4 {
final double m30 = _m4storage[3];
final double m31 = _m4storage[7];
final double m32 = _m4storage[11];
final double m33 = _m4storage[15];
final Float32List argStorage = arg._m4storage;
final double n33 = argStorage[15];
final double n00 = argStorage[0];
final double n01 = argStorage[4];
final double n02 = argStorage[8];
Expand All @@ -765,7 +773,6 @@ class Matrix4 {
final double n30 = argStorage[3];
final double n31 = argStorage[7];
final double n32 = argStorage[11];
final double n33 = argStorage[15];
_m4storage[0] = (m00 * n00) + (m01 * n10) + (m02 * n20) + (m03 * n30);
_m4storage[4] = (m00 * n01) + (m01 * n11) + (m02 * n21) + (m03 * n31);
_m4storage[8] = (m00 * n02) + (m01 * n12) + (m02 * n22) + (m03 * n32);
Expand All @@ -789,6 +796,7 @@ class Matrix4 {

/// Multiply a transposed [this] with [arg].
void transposeMultiply(Matrix4 arg) {
final double m33 = _m4storage[15];
final double m00 = _m4storage[0];
final double m01 = _m4storage[1];
final double m02 = _m4storage[2];
Expand All @@ -804,7 +812,7 @@ class Matrix4 {
final double m30 = _m4storage[12];
final double m31 = _m4storage[13];
final double m32 = _m4storage[14];
final double m33 = _m4storage[15];

final Float32List argStorage = arg._m4storage;
_m4storage[0] = (m00 * argStorage[0]) +
(m01 * argStorage[1]) +
Expand Down