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