xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLConstructorSplat.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #ifndef SKSL_CONSTRUCTOR_SPLAT
9 #define SKSL_CONSTRUCTOR_SPLAT
10 
11 #include "include/core/SkTypes.h"
12 #include "src/sksl/SkSLPosition.h"
13 #include "src/sksl/ir/SkSLConstructor.h"
14 #include "src/sksl/ir/SkSLExpression.h"
15 #include "src/sksl/ir/SkSLIRNode.h"
16 #include "src/sksl/ir/SkSLType.h"
17 
18 #include <memory>
19 #include <optional>
20 #include <utility>
21 
22 namespace SkSL {
23 
24 class Context;
25 
26 /**
27  * Represents the construction of a vector splat, such as `half3(n)`.
28  *
29  * These always contain exactly 1 scalar.
30  */
31 class ConstructorSplat final : public SingleArgumentConstructor {
32 public:
33     inline static constexpr Kind kIRNodeKind = Kind::kConstructorSplat;
34 
ConstructorSplat(Position pos,const Type & type,std::unique_ptr<Expression> arg)35     ConstructorSplat(Position pos, const Type& type, std::unique_ptr<Expression> arg)
36         : INHERITED(pos, kIRNodeKind, &type, std::move(arg)) {}
37 
38     // The input argument must be scalar. A "splat" to a scalar type will be optimized into a no-op.
39     static std::unique_ptr<Expression> Make(const Context& context,
40                                             Position pos,
41                                             const Type& type,
42                                             std::unique_ptr<Expression> arg);
43 
clone(Position pos)44     std::unique_ptr<Expression> clone(Position pos) const override {
45         return std::make_unique<ConstructorSplat>(pos, this->type(), argument()->clone());
46     }
47 
supportsConstantValues()48     bool supportsConstantValues() const override {
49         return true;
50     }
51 
getConstantValue(int n)52     std::optional<double> getConstantValue(int n) const override {
53         SkASSERT(n >= 0 && n < this->type().columns());
54         return this->argument()->getConstantValue(0);
55     }
56 
57 private:
58     using INHERITED = SingleArgumentConstructor;
59 };
60 
61 }  // namespace SkSL
62 
63 #endif
64