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