/src/skia/src/sksl/ir/SkSLConstructorMatrixResize.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2021 Google LLC |
3 | | * |
4 | | * Use of this source code is governed by a BSD-style license that can be |
5 | | * found in the LICENSE file. |
6 | | */ |
7 | | |
8 | | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
9 | | |
10 | | #include "src/sksl/ir/SkSLConstructor.h" |
11 | | #include "src/sksl/ir/SkSLType.h" |
12 | | |
13 | | namespace SkSL { |
14 | | |
15 | | std::unique_ptr<Expression> ConstructorMatrixResize::Make(const Context& context, |
16 | | int offset, |
17 | | const Type& type, |
18 | 3.06k | std::unique_ptr<Expression> arg) { |
19 | 3.06k | SkASSERT(type.isMatrix()); |
20 | 3.06k | SkASSERT(arg->type().componentType() == type.componentType()); |
21 | | |
22 | | // If the matrix isn't actually changing size, return it as-is. |
23 | 3.06k | if (type.rows() == arg->type().rows() && type.columns() == arg->type().columns()) { |
24 | 433 | return arg; |
25 | 433 | } |
26 | | |
27 | 2.63k | return std::make_unique<ConstructorMatrixResize>(offset, type, std::move(arg)); |
28 | 2.63k | } |
29 | | |
30 | 1.85k | const Expression* ConstructorMatrixResize::getConstantSubexpression(int n) const { |
31 | 1.85k | int rows = this->type().rows(); |
32 | 1.85k | int row = n % rows; |
33 | 1.85k | int col = n / rows; |
34 | | |
35 | 1.85k | SkASSERT(col >= 0); |
36 | 1.85k | SkASSERT(row >= 0); |
37 | 1.85k | SkASSERT(col < this->type().columns()); |
38 | 1.85k | SkASSERT(row < this->type().rows()); |
39 | | |
40 | | // GLSL resize matrices are of the form: |
41 | | // |m m 0| |
42 | | // |m m 0| |
43 | | // |0 0 1| |
44 | | // Where `m` is the matrix being wrapped, and other cells contain the identity matrix. |
45 | | |
46 | | // Forward `getConstantSubexpression` to the wrapped matrix if the position is in its bounds. |
47 | 1.85k | if (col < this->argument()->type().columns() && row < this->argument()->type().rows()) { |
48 | | // Recalculate `n` in terms of the inner matrix's dimensions. |
49 | 1.22k | n = row + (col * this->argument()->type().rows()); |
50 | 1.22k | return this->argument()->getConstantSubexpression(n); |
51 | 1.22k | } |
52 | | |
53 | | // Synthesize an identity matrix for out-of-bounds positions. |
54 | 627 | return (col == row) ? &fOneLiteral : &fZeroLiteral; |
55 | 627 | } |
56 | | |
57 | | } // namespace SkSL |