Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
SkSL::ConstructorCompound Class Referencefinal

#include <SkSLConstructorCompound.h>

Inheritance diagram for SkSL::ConstructorCompound:
SkSL::MultiArgumentConstructor SkSL::AnyConstructor SkSL::Expression SkSL::IRNode SkSL::Poolable

Public Member Functions

 ConstructorCompound (Position pos, const Type &type, ExpressionArray args)
 
std::unique_ptr< Expressionclone (Position pos) const override
 
- Public Member Functions inherited from SkSL::MultiArgumentConstructor
 MultiArgumentConstructor (Position pos, Kind kind, const Type *type, ExpressionArray arguments)
 
ExpressionArrayarguments ()
 
const ExpressionArrayarguments () const
 
SkSpan< std::unique_ptr< Expression > > argumentSpan () final
 
SkSpan< const std::unique_ptr< Expression > > argumentSpan () const final
 
- Public Member Functions inherited from SkSL::AnyConstructor
 AnyConstructor (Position pos, Kind kind, const Type *type)
 
virtual SkSpan< std::unique_ptr< Expression > > argumentSpan ()=0
 
virtual SkSpan< const std::unique_ptr< Expression > > argumentSpan () const =0
 
std::string description (OperatorPrecedence) const override
 
const TypecomponentType () const
 
bool supportsConstantValues () const override
 
std::optional< double > getConstantValue (int n) const override
 
ComparisonResult compareConstant (const Expression &other) const override
 
- Public Member Functions inherited from SkSL::Expression
 Expression (Position pos, Kind kind, const Type *type)
 
Kind kind () const
 
const Typetype () const
 
bool isAnyConstructor () const
 
bool isIntLiteral () const
 
bool isFloatLiteral () const
 
bool isBoolLiteral () const
 
AnyConstructorasAnyConstructor ()
 
const AnyConstructorasAnyConstructor () const
 
bool isIncomplete (const Context &context) const
 
virtual ComparisonResult compareConstant (const Expression &other) const
 
CoercionCost coercionCost (const Type &target) const
 
virtual bool supportsConstantValues () const
 
virtual std::optional< double > getConstantValue (int n) const
 
virtual std::unique_ptr< Expressionclone (Position pos) const =0
 
std::unique_ptr< Expressionclone () const
 
std::string description () const final
 
virtual std::string description (OperatorPrecedence parentPrecedence) const =0
 
- Public Member Functions inherited from SkSL::IRNode
virtual ~IRNode ()
 
virtual std::string description () const =0
 
 IRNode (const IRNode &)=delete
 
IRNodeoperator= (const IRNode &)=delete
 
Position position () const
 
void setPosition (Position p)
 
template<typename T >
bool is () const
 
template<typename T >
const Tas () const
 
template<typename T >
Tas ()
 

Static Public Member Functions

static std::unique_ptr< ExpressionMake (const Context &context, Position pos, const Type &type, ExpressionArray args)
 
static std::unique_ptr< ExpressionMakeFromConstants (const Context &context, Position pos, const Type &type, const double values[])
 
- Static Public Member Functions inherited from SkSL::Poolable
static void * operator new (const size_t size)
 
static void operator delete (void *ptr)
 

Static Public Attributes

static constexpr Kind kIRNodeKind = Kind::kConstructorCompound
 

Additional Inherited Members

- Public Types inherited from SkSL::Expression
enum class  ComparisonResult { kUnknown = -1 , kNotEqual , kEqual }
 
using Kind = ExpressionKind
 
- Public Attributes inherited from SkSL::IRNode
Position fPosition
 
- Protected Member Functions inherited from SkSL::IRNode
 IRNode (Position position, int kind)
 
- Protected Attributes inherited from SkSL::IRNode
int fKind
 

Detailed Description

Represents a vector or matrix that is composed from other expressions, such as half3(pos.xy, 1) or mat3(a.xyz, b.xyz, 0, 0, 1)

These can contain a mix of scalars and aggregates. The total number of scalar values inside the constructor must always match the type's slot count. (e.g. pos.xy consumes two slots.) The inner values must have the same component type as the vector/matrix.

Definition at line 33 of file SkSLConstructorCompound.h.

Constructor & Destructor Documentation

◆ ConstructorCompound()

SkSL::ConstructorCompound::ConstructorCompound ( Position  pos,
const Type type,
ExpressionArray  args 
)
inline

Definition at line 37 of file SkSLConstructorCompound.h.

38 : INHERITED(pos, kIRNodeKind, &type, std::move(args)) {}
SkPoint pos
static constexpr Kind kIRNodeKind
const Type & type() const
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args

Member Function Documentation

◆ clone()

std::unique_ptr< Expression > SkSL::ConstructorCompound::clone ( Position  pos) const
inlineoverridevirtual

Implements SkSL::Expression.

Definition at line 50 of file SkSLConstructorCompound.h.

50 {
51 return std::make_unique<ConstructorCompound>(pos, this->type(), this->arguments().clone());
52 }
std::unique_ptr< Expression > clone() const
ExpressionArray & arguments()

◆ Make()

std::unique_ptr< Expression > SkSL::ConstructorCompound::Make ( const Context context,
Position  pos,
const Type type,
ExpressionArray  args 
)
static

Definition at line 78 of file SkSLConstructorCompound.cpp.

81 {
83
84 // All the arguments must have matching component type.
85 SkASSERT(std::all_of(args.begin(), args.end(), [&](const std::unique_ptr<Expression>& arg) {
86 const Type& argType = arg->type();
87 return (argType.isScalar() || argType.isVector() || argType.isMatrix()) &&
88 (argType.componentType().matches(type.componentType()));
89 }));
90
91 // The slot count of the combined argument list must match the composite type's slot count.
93 std::accumulate(args.begin(), args.end(), /*initial value*/ (size_t)0,
94 [](size_t n, const std::unique_ptr<Expression>& arg) {
95 return n + arg->type().slotCount();
96 }));
97 // No-op compound constructors (containing a single argument of the same type) are eliminated.
98 // (Even though this is a "compound constructor," we let scalars pass through here; it's
99 // harmless to allow and simplifies call sites which need to narrow a vector and may sometimes
100 // end up with a scalar.)
101 if (args.size() == 1 && is_safe_to_eliminate(type, *args.front())) {
102 args.front()->fPosition = pos;
103 return std::move(args.front());
104 }
105 // Beyond this point, the type must be a vector or matrix.
107
108 if (context.fConfig->fSettings.fOptimize) {
109 // Find ConstructorCompounds embedded inside other ConstructorCompounds and flatten them.
110 // - float4(float2(1, 2), 3, 4) --> float4(1, 2, 3, 4)
111 // - float4(w, float3(sin(x), cos(y), tan(z))) --> float4(w, sin(x), cos(y), tan(z))
112 // - mat2(float2(a, b), float2(c, d)) --> mat2(a, b, c, d)
113
114 // See how many fields we would have if composite constructors were flattened out.
115 int fields = 0;
116 for (const std::unique_ptr<Expression>& arg : args) {
117 fields += arg->is<ConstructorCompound>()
118 ? arg->as<ConstructorCompound>().arguments().size()
119 : 1;
120 }
121
122 // If we added up more fields than we're starting with, we found at least one input that can
123 // be flattened out.
124 if (fields > args.size()) {
125 ExpressionArray flattened;
126 flattened.reserve_exact(fields);
127 for (std::unique_ptr<Expression>& arg : args) {
128 // For non-ConstructorCompound fields, move them over as-is.
129 if (!arg->is<ConstructorCompound>()) {
130 flattened.push_back(std::move(arg));
131 continue;
132 }
133 // For ConstructorCompound fields, move over their inner arguments individually.
134 ConstructorCompound& compositeCtor = arg->as<ConstructorCompound>();
135 for (std::unique_ptr<Expression>& innerArg : compositeCtor.arguments()) {
136 flattened.push_back(std::move(innerArg));
137 }
138 }
139 args = std::move(flattened);
140 }
141 }
142
143 // Replace constant variables with their corresponding values, so `float2(one, two)` can
144 // compile down to `float2(1.0, 2.0)` (the latter is a compile-time constant).
145 for (std::unique_ptr<Expression>& arg : args) {
147 }
148
149 if (context.fConfig->fSettings.fOptimize) {
150 // Reduce compound constructors to splats where possible.
151 if (const Expression* splat = make_splat_from_arguments(type, args)) {
152 return ConstructorSplat::Make(context, pos, type, splat->clone());
153 }
154 }
155
156 return std::make_unique<ConstructorCompound>(pos, type, std::move(args));
157}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static std::unique_ptr< Expression > MakeConstantValueForVariable(Position pos, std::unique_ptr< Expression > expr)
ConstructorCompound(Position pos, const Type &type, ExpressionArray args)
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, std::unique_ptr< Expression > arg)
Expression(Position pos, Kind kind, const Type *type)
virtual bool isVector() const
Definition: SkSLType.h:524
bool isAllowedInES2(const Context &context) const
virtual bool isMatrix() const
Definition: SkSLType.h:528
virtual size_t slotCount() const
Definition: SkSLType.h:457
const Type * clone(const Context &context, SymbolTable *symbolTable) const
static const Expression * make_splat_from_arguments(const Type &type, const ExpressionArray &args)
static bool is_safe_to_eliminate(const Type &type, const Expression &arg)

◆ MakeFromConstants()

std::unique_ptr< Expression > SkSL::ConstructorCompound::MakeFromConstants ( const Context context,
Position  pos,
const Type type,
const double  values[] 
)
static

Definition at line 159 of file SkSLConstructorCompound.cpp.

162 {
163 int numSlots = returnType.slotCount();
164 ExpressionArray array;
165 array.reserve_exact(numSlots);
166 for (int index = 0; index < numSlots; ++index) {
167 array.push_back(Literal::Make(pos, value[index], &returnType.componentType()));
168 }
169 return ConstructorCompound::Make(context, pos, returnType, std::move(array));
170}
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, ExpressionArray args)
static std::unique_ptr< Literal > Make(Position pos, double value, const Type *type)
Definition: SkSLLiteral.h:81
uint8_t value

Member Data Documentation

◆ kIRNodeKind

constexpr Kind SkSL::ConstructorCompound::kIRNodeKind = Kind::kConstructorCompound
inlinestaticconstexpr

Definition at line 35 of file SkSLConstructorCompound.h.


The documentation for this class was generated from the following files: