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/SkSLConstructorSplat.h"
9
10 #include "src/sksl/SkSLConstantFolder.h"
11
12 namespace SkSL {
13
Make(const Context & context,Position pos,const Type & type,std::unique_ptr<Expression> arg)14 std::unique_ptr<Expression> ConstructorSplat::Make(const Context& context,
15 Position pos,
16 const Type& type,
17 std::unique_ptr<Expression> arg) {
18 SkASSERT(type.isAllowedInES2(context));
19 SkASSERT(type.isScalar() || type.isVector());
20 SkASSERT(arg->type().scalarTypeForLiteral().matches(
21 type.componentType().scalarTypeForLiteral()));
22 SkASSERT(arg->type().isScalar());
23
24 // A "splat" to a scalar type is a no-op and can be eliminated.
25 if (type.isScalar()) {
26 arg->fPosition = pos;
27 return arg;
28 }
29
30 // Replace constant variables with their corresponding values, so `float3(five)` can compile
31 // down to `float3(5.0)` (the latter is a compile-time constant).
32 arg = ConstantFolder::MakeConstantValueForVariable(pos, std::move(arg));
33
34 SkASSERT(type.isVector());
35 return std::make_unique<ConstructorSplat>(pos, type, std::move(arg));
36 }
37
38 } // namespace SkSL
39