[Impeller] combine translate* scale mat mul when computing shader transform. (flutter/engine#56352)

For each draw we do 4 matrix multiplications, which isn't slow but does show up in CPU profiles at a few % of a frame. We can cut the number of multiplications down to 3 by constructing the translate*scale matrix in one go. The translate * scale matrix construction is much simpler than a full multiplication as we can ignore all of the known zero values.
This commit is contained in:
Jonah Williams
2024-11-04 11:24:49 -08:00
committed by GitHub
parent ad216a299d
commit 38a225b1ce
3 changed files with 27 additions and 2 deletions

View File

@@ -52,8 +52,8 @@ Matrix Entity::GetShaderTransform(const RenderPass& pass) const {
Matrix Entity::GetShaderTransform(Scalar shader_clip_depth,
const RenderPass& pass,
const Matrix& transform) {
return Matrix::MakeTranslation({0, 0, shader_clip_depth}) *
Matrix::MakeScale({1, 1, Entity::kDepthEpsilon}) *
return Matrix::MakeTranslateScale({1, 1, Entity::kDepthEpsilon},
{0, 0, shader_clip_depth}) *
pass.GetOrthographicTransform() * transform;
}

View File

@@ -110,6 +110,16 @@ struct Matrix {
// clang-format on
}
static constexpr Matrix MakeTranslateScale(const Vector3& s,
const Vector3& t) {
// clang-format off
return Matrix(s.x, 0.0f, 0.0f, 0.0f,
0.0f, s.y, 0.0f, 0.0f,
0.0f, 0.0f, s.z, 0.0f,
t.x , t.y, t.z, 1.0f);
// clang-format on
}
static constexpr Matrix MakeScale(const Vector2& s) {
return MakeScale(Vector3(s.x, s.y, 1.0f));
}

View File

@@ -211,5 +211,20 @@ TEST(MatrixTest, TranslateWithPerspective) {
0.0, 2.0, 0.0, 430.0)));
}
TEST(MatrixTest, MakeScaleTranslate) {
EXPECT_TRUE(MatrixNear(
Matrix::MakeTranslateScale({1, 1, 1.0 / 1024}, {10, 10, 1.0 / 1024}),
Matrix::MakeTranslation({10, 10, 1.0 / 1024}) *
Matrix::MakeScale({1, 1, 1.0 / 1024})));
EXPECT_TRUE(MatrixNear(
Matrix::MakeTranslateScale({2, 2, 2}, {10, 10, 0}),
Matrix::MakeTranslation({10, 10, 0}) * Matrix::MakeScale({2, 2, 2})));
EXPECT_TRUE(MatrixNear(
Matrix::MakeTranslateScale({0, 0, 0}, {0, 0, 0}),
Matrix::MakeTranslation({0, 0, 0}) * Matrix::MakeScale({0, 0, 0})));
}
} // namespace testing
} // namespace impeller