summaryrefslogtreecommitdiffstats
path: root/gfx/skia/skia/src/sksl/ir/SkSLConstructorDiagonalMatrix.cpp
blob: e863babfa9b69a2e24d7c4d4e6b6677c48253556 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * Copyright 2021 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"

#include "include/core/SkTypes.h"
#include "src/sksl/SkSLConstantFolder.h"
#include "src/sksl/ir/SkSLType.h"

namespace SkSL {

std::unique_ptr<Expression> ConstructorDiagonalMatrix::Make(const Context& context,
                                                            Position pos,
                                                            const Type& type,
                                                            std::unique_ptr<Expression> arg) {
    SkASSERT(type.isMatrix());
    SkASSERT(type.isAllowedInES2(context));
    SkASSERT(arg->type().isScalar());
    SkASSERT(arg->type().matches(type.componentType()));

    // Look up the value of constant variables. This allows constant-expressions like `mat4(five)`
    // to be replaced with `mat4(5.0)`.
    arg = ConstantFolder::MakeConstantValueForVariable(pos, std::move(arg));

    return std::make_unique<ConstructorDiagonalMatrix>(pos, type, std::move(arg));
}

std::optional<double> ConstructorDiagonalMatrix::getConstantValue(int n) const {
    int rows = this->type().rows();
    int row = n % rows;
    int col = n / rows;

    SkASSERT(col >= 0);
    SkASSERT(row >= 0);
    SkASSERT(col < this->type().columns());
    SkASSERT(row < this->type().rows());

    return (col == row) ? this->argument()->getConstantValue(0) : 0.0;
}

}  // namespace SkSL