| 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 "include/core/SkTypes.h" |
| 11 |
#include "src/sksl/ir/SkSLType.h" |
| 12 |
|
| 13 |
namespace SkSL { |
| 14 |
|
| 15 |
std::unique_ptr<Expression> ConstructorMatrixResize::Make(const Context& context, |
| 16 |
Position pos, |
| 17 |
const Type& type, |
| 18 |
std::unique_ptr<Expression> arg) { |
| 19 |
SkASSERT(type.isMatrix()); |
| 20 |
SkASSERT(type.isAllowedInES2(context)); |
| 21 |
SkASSERT(arg->type().componentType().matches(type.componentType())); |
| 22 |
|
| 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()) { |
| 25 |
return arg; |
| 26 |
} |
| 27 |
|
| 28 |
return std::make_unique<ConstructorMatrixResize>(pos, type, std::move(arg)); |
| 29 |
} |
| 30 |
|
| 31 |
std::optional<double> ConstructorMatrixResize::getConstantValue(int n) const { |
| 32 |
int rows = this->type().rows(); |
| 33 |
int row = n % rows; |
| 34 |
int col = n / rows; |
| 35 |
|
| 36 |
SkASSERT(col >= 0); |
| 37 |
SkASSERT(row >= 0); |
| 38 |
SkASSERT(col < this->type().columns()); |
| 39 |
SkASSERT(row < this->type().rows()); |
| 40 |
|
| 41 |
// GLSL resize matrices are of the form: |
| 42 |
// |m m 0| |
| 43 |
// |m m 0| |
| 44 |
// |0 0 1| |
| 45 |
// Where `m` is the matrix being wrapped, and other cells contain the identity matrix. |
| 46 |
|
| 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); |
| 52 |
} |
| 53 |
|
| 54 |
// Synthesize an identity matrix for out-of-bounds positions. |
| 55 |
return (col == row) ? 1.0 : 0.0; |
| 56 |
} |
| 57 |
|
| 58 |
} // namespace SkSL |