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 
Make(const Context & context,int offset,const Type & type,std::unique_ptr<Expression> arg)15 std::unique_ptr<Expression> ConstructorMatrixResize::Make(const Context& context,
16                                                           int offset,
17                                                           const Type& type,
18                                                           std::unique_ptr<Expression> arg) {
19     SkASSERT(type.isMatrix());
20     SkASSERT(arg->type().componentType() == type.componentType());
21 
22     // If the matrix isn't actually changing size, return it as-is.
23     if (type.rows() == arg->type().rows() && type.columns() == arg->type().columns()) {
24         return arg;
25     }
26 
27     return std::make_unique<ConstructorMatrixResize>(offset, type, std::move(arg));
28 }
29 
getConstantSubexpression(int n) const30 const Expression* ConstructorMatrixResize::getConstantSubexpression(int n) const {
31     int rows = this->type().rows();
32     int row = n % rows;
33     int col = n / rows;
34 
35     SkASSERT(col >= 0);
36     SkASSERT(row >= 0);
37     SkASSERT(col < this->type().columns());
38     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     if (col < this->argument()->type().columns() && row < this->argument()->type().rows()) {
48         // Recalculate `n` in terms of the inner matrix's dimensions.
49         n = row + (col * this->argument()->type().rows());
50         return this->argument()->getConstantSubexpression(n);
51     }
52 
53     // Synthesize an identity matrix for out-of-bounds positions.
54     return (col == row) ? &fOneLiteral : &fZeroLiteral;
55 }
56 
57 }  // namespace SkSL
58