xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLConstructorCompound.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_COMPOUND
9 #define SKSL_CONSTRUCTOR_COMPOUND
10 
11 #include "src/sksl/SkSLDefines.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 
17 #include <memory>
18 #include <utility>
19 
20 namespace SkSL {
21 
22 class Context;
23 class Type;
24 
25 /**
26  * Represents a vector or matrix that is composed from other expressions, such as
27  * `half3(pos.xy, 1)` or `mat3(a.xyz, b.xyz, 0, 0, 1)`
28  *
29  * These can contain a mix of scalars and aggregates. The total number of scalar values inside the
30  * constructor must always match the type's slot count. (e.g. `pos.xy` consumes two slots.)
31  * The inner values must have the same component type as the vector/matrix.
32  */
33 class ConstructorCompound final : public MultiArgumentConstructor {
34 public:
35     inline static constexpr Kind kIRNodeKind = Kind::kConstructorCompound;
36 
ConstructorCompound(Position pos,const Type & type,ExpressionArray args)37     ConstructorCompound(Position pos, const Type& type, ExpressionArray args)
38             : INHERITED(pos, kIRNodeKind, &type, std::move(args)) {}
39 
40     static std::unique_ptr<Expression> Make(const Context& context,
41                                             Position pos,
42                                             const Type& type,
43                                             ExpressionArray args);
44 
45     static std::unique_ptr<Expression> MakeFromConstants(const Context& context,
46                                                          Position pos,
47                                                          const Type& type,
48                                                          const double values[]);
49 
clone(Position pos)50     std::unique_ptr<Expression> clone(Position pos) const override {
51         return std::make_unique<ConstructorCompound>(pos, this->type(), this->arguments().clone());
52     }
53 
54 private:
55     using INHERITED = MultiArgumentConstructor;
56 };
57 
58 }  // namespace SkSL
59 
60 #endif
61