1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2021 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLConstructorCompoundCast.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "src/sksl/SkSLConstantFolder.h"
11cb93a386Sopenharmony_ci#include "src/sksl/SkSLProgramSettings.h"
12cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLConstructor.h"
13cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLConstructorCompound.h"
14cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
15cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLConstructorScalarCast.h"
16cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLConstructorSplat.h"
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_cinamespace SkSL {
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_cistatic std::unique_ptr<Expression> cast_constant_composite(const Context& context,
21cb93a386Sopenharmony_ci                                                           const Type& destType,
22cb93a386Sopenharmony_ci                                                           std::unique_ptr<Expression> constCtor) {
23cb93a386Sopenharmony_ci    const Type& scalarType = destType.componentType();
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci    // We generate nicer code for splats and diagonal matrices by handling them separately instead
26cb93a386Sopenharmony_ci    // of relying on the constant-subexpression code below. This is not truly necessary but it makes
27cb93a386Sopenharmony_ci    // our output look a little better; human beings prefer `half4(0)` to `half4(0, 0, 0, 0)`.
28cb93a386Sopenharmony_ci    if (constCtor->is<ConstructorSplat>()) {
29cb93a386Sopenharmony_ci        // This is a typecast of a splat containing a constant value, e.g. `half4(7)`. We can
30cb93a386Sopenharmony_ci        // replace it with a splat of a different type, e.g. `int4(7)`.
31cb93a386Sopenharmony_ci        ConstructorSplat& splat = constCtor->as<ConstructorSplat>();
32cb93a386Sopenharmony_ci        return ConstructorSplat::Make(
33cb93a386Sopenharmony_ci                context, constCtor->fLine, destType,
34cb93a386Sopenharmony_ci                ConstructorScalarCast::Make(context, constCtor->fLine, scalarType,
35cb93a386Sopenharmony_ci                                            std::move(splat.argument())));
36cb93a386Sopenharmony_ci    }
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci    if (constCtor->is<ConstructorDiagonalMatrix>() && destType.isMatrix()) {
39cb93a386Sopenharmony_ci        // This is a typecast of a constant diagonal matrix, e.g. `float3x3(2)`. We can replace it
40cb93a386Sopenharmony_ci        // with a diagonal matrix of a different type, e.g. `half3x3(2)`.
41cb93a386Sopenharmony_ci        ConstructorDiagonalMatrix& matrixCtor = constCtor->as<ConstructorDiagonalMatrix>();
42cb93a386Sopenharmony_ci        return ConstructorDiagonalMatrix::Make(
43cb93a386Sopenharmony_ci                context, constCtor->fLine, destType,
44cb93a386Sopenharmony_ci                ConstructorScalarCast::Make(context, constCtor->fLine, scalarType,
45cb93a386Sopenharmony_ci                                            std::move(matrixCtor.argument())));
46cb93a386Sopenharmony_ci    }
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    // Create a compound Constructor(literal, ...) which typecasts each scalar value inside.
49cb93a386Sopenharmony_ci    size_t numSlots = destType.slotCount();
50cb93a386Sopenharmony_ci    SkASSERT(numSlots == constCtor->type().slotCount());
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci    ExpressionArray typecastArgs;
53cb93a386Sopenharmony_ci    typecastArgs.reserve_back(numSlots);
54cb93a386Sopenharmony_ci    for (size_t index = 0; index < numSlots; ++index) {
55cb93a386Sopenharmony_ci        skstd::optional<double> slotVal = constCtor->getConstantValue(index);
56cb93a386Sopenharmony_ci        if (scalarType.checkForOutOfRangeLiteral(context, *slotVal, constCtor->fLine)) {
57cb93a386Sopenharmony_ci            // We've reported an error because the literal is out of range for this type. Zero out
58cb93a386Sopenharmony_ci            // the value to avoid a cascade of errors.
59cb93a386Sopenharmony_ci            *slotVal = 0.0;
60cb93a386Sopenharmony_ci        }
61cb93a386Sopenharmony_ci        typecastArgs.push_back(Literal::Make(constCtor->fLine, *slotVal, &scalarType));
62cb93a386Sopenharmony_ci    }
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci    return ConstructorCompound::Make(context, constCtor->fLine, destType,
65cb93a386Sopenharmony_ci                                     std::move(typecastArgs));
66cb93a386Sopenharmony_ci}
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_cistd::unique_ptr<Expression> ConstructorCompoundCast::Make(const Context& context,
69cb93a386Sopenharmony_ci                                                          int line,
70cb93a386Sopenharmony_ci                                                          const Type& type,
71cb93a386Sopenharmony_ci                                                          std::unique_ptr<Expression> arg) {
72cb93a386Sopenharmony_ci    // Only vectors or matrices of the same dimensions are allowed.
73cb93a386Sopenharmony_ci    SkASSERT(type.isVector() || type.isMatrix());
74cb93a386Sopenharmony_ci    SkASSERT(type.isAllowedInES2(context));
75cb93a386Sopenharmony_ci    SkASSERT(arg->type().isVector() == type.isVector());
76cb93a386Sopenharmony_ci    SkASSERT(arg->type().isMatrix() == type.isMatrix());
77cb93a386Sopenharmony_ci    SkASSERT(type.columns() == arg->type().columns());
78cb93a386Sopenharmony_ci    SkASSERT(type.rows() == arg->type().rows());
79cb93a386Sopenharmony_ci
80cb93a386Sopenharmony_ci    // If this is a no-op cast, return the expression as-is.
81cb93a386Sopenharmony_ci    if (type == arg->type()) {
82cb93a386Sopenharmony_ci        return arg;
83cb93a386Sopenharmony_ci    }
84cb93a386Sopenharmony_ci    // Look up the value of constant variables. This allows constant-expressions like
85cb93a386Sopenharmony_ci    // `int4(colorGreen)` to be replaced with the compile-time constant `int4(0, 1, 0, 1)`.
86cb93a386Sopenharmony_ci    arg = ConstantFolder::MakeConstantValueForVariable(std::move(arg));
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci    // We can cast a vector of compile-time constants at compile-time.
89cb93a386Sopenharmony_ci    if (arg->isCompileTimeConstant()) {
90cb93a386Sopenharmony_ci        return cast_constant_composite(context, type, std::move(arg));
91cb93a386Sopenharmony_ci    }
92cb93a386Sopenharmony_ci    return std::make_unique<ConstructorCompoundCast>(line, type, std::move(arg));
93cb93a386Sopenharmony_ci}
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_ci}  // namespace SkSL
96