2 * Copyright 2021 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/sksl/ir/SkSLConstructorMatrixResize.h"
10 #include "src/sksl/ir/SkSLConstructor.h"
11 #include "src/sksl/ir/SkSLType.h"
15 std::unique_ptr<Expression> ConstructorMatrixResize::Make(const Context& context,
18 std::unique_ptr<Expression> arg) {
19 SkASSERT(type.isMatrix());
20 SkASSERT(type.isAllowedInES2(context));
21 SkASSERT(arg->type().componentType().matches(type.componentType()));
23 // If the matrix isn't actually changing size, return it as-is.
24 if (type.rows() == arg->type().rows() && type.columns() == arg->type().columns()) {
28 return std::make_unique<ConstructorMatrixResize>(pos, type, std::move(arg));
31 std::optional<double> ConstructorMatrixResize::getConstantValue(int n) const {
32 int rows = this->type().rows();
38 SkASSERT(col < this->type().columns());
39 SkASSERT(row < this->type().rows());
41 // GLSL resize matrices are of the form:
45 // Where `m` is the matrix being wrapped, and other cells contain the identity matrix.
47 // Forward `getConstantValue` to the wrapped matrix if the position is in its bounds.
48 if (col < this->argument()->type().columns() && row < this->argument()->type().rows()) {
49 // Recalculate `n` in terms of the inner matrix's dimensions.
50 n = row + (col * this->argument()->type().rows());
51 return this->argument()->getConstantValue(n);
54 // Synthesize an identity matrix for out-of-bounds positions.
55 return (col == row) ? 1.0 : 0.0;