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