From 38a225b1ce75cbf20d59bd1747761e01dd755a87 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 4 Nov 2024 11:24:49 -0800 Subject: [PATCH] [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. --- engine/src/flutter/impeller/entity/entity.cc | 4 ++-- engine/src/flutter/impeller/geometry/matrix.h | 10 ++++++++++ .../flutter/impeller/geometry/matrix_unittests.cc | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/impeller/entity/entity.cc b/engine/src/flutter/impeller/entity/entity.cc index 5c7fbddb3d..92d6a10201 100644 --- a/engine/src/flutter/impeller/entity/entity.cc +++ b/engine/src/flutter/impeller/entity/entity.cc @@ -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; } diff --git a/engine/src/flutter/impeller/geometry/matrix.h b/engine/src/flutter/impeller/geometry/matrix.h index 166fc11f96..848d004505 100644 --- a/engine/src/flutter/impeller/geometry/matrix.h +++ b/engine/src/flutter/impeller/geometry/matrix.h @@ -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)); } diff --git a/engine/src/flutter/impeller/geometry/matrix_unittests.cc b/engine/src/flutter/impeller/geometry/matrix_unittests.cc index 91a2a50f2a..8d99466e01 100644 --- a/engine/src/flutter/impeller/geometry/matrix_unittests.cc +++ b/engine/src/flutter/impeller/geometry/matrix_unittests.cc @@ -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