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

Commit b7f070c

Browse files
[Impeller] combine translate* scale mat mul when computing shader transform. (#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.
1 parent 4a04e6e commit b7f070c

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

impeller/entity/entity.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ Matrix Entity::GetShaderTransform(const RenderPass& pass) const {
5252
Matrix Entity::GetShaderTransform(Scalar shader_clip_depth,
5353
const RenderPass& pass,
5454
const Matrix& transform) {
55-
return Matrix::MakeTranslation({0, 0, shader_clip_depth}) *
56-
Matrix::MakeScale({1, 1, Entity::kDepthEpsilon}) *
55+
return Matrix::MakeTranslateScale({1, 1, Entity::kDepthEpsilon},
56+
{0, 0, shader_clip_depth}) *
5757
pass.GetOrthographicTransform() * transform;
5858
}
5959

impeller/geometry/matrix.h

+10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ struct Matrix {
110110
// clang-format on
111111
}
112112

113+
static constexpr Matrix MakeTranslateScale(const Vector3& s,
114+
const Vector3& t) {
115+
// clang-format off
116+
return Matrix(s.x, 0.0f, 0.0f, 0.0f,
117+
0.0f, s.y, 0.0f, 0.0f,
118+
0.0f, 0.0f, s.z, 0.0f,
119+
t.x , t.y, t.z, 1.0f);
120+
// clang-format on
121+
}
122+
113123
static constexpr Matrix MakeScale(const Vector2& s) {
114124
return MakeScale(Vector3(s.x, s.y, 1.0f));
115125
}

impeller/geometry/matrix_unittests.cc

+15
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,20 @@ TEST(MatrixTest, TranslateWithPerspective) {
211211
0.0, 2.0, 0.0, 430.0)));
212212
}
213213

214+
TEST(MatrixTest, MakeScaleTranslate) {
215+
EXPECT_TRUE(MatrixNear(
216+
Matrix::MakeTranslateScale({1, 1, 1.0 / 1024}, {10, 10, 1.0 / 1024}),
217+
Matrix::MakeTranslation({10, 10, 1.0 / 1024}) *
218+
Matrix::MakeScale({1, 1, 1.0 / 1024})));
219+
220+
EXPECT_TRUE(MatrixNear(
221+
Matrix::MakeTranslateScale({2, 2, 2}, {10, 10, 0}),
222+
Matrix::MakeTranslation({10, 10, 0}) * Matrix::MakeScale({2, 2, 2})));
223+
224+
EXPECT_TRUE(MatrixNear(
225+
Matrix::MakeTranslateScale({0, 0, 0}, {0, 0, 0}),
226+
Matrix::MakeTranslation({0, 0, 0}) * Matrix::MakeScale({0, 0, 0})));
227+
}
228+
214229
} // namespace testing
215230
} // namespace impeller

0 commit comments

Comments
 (0)