Flutter Engine
The Flutter Engine
SkSLIRHelpers.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google Inc.
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_IRHELPERS
9#define SKSL_IRHELPERS
10
18#include "src/sksl/SkSLUtil.h"
30
31#include <memory>
32#include <utility>
33
34namespace SkSL {
35
36class Statement;
37class Variable;
38
39struct IRHelpers {
40 IRHelpers(const Context& c) : fContext(c) {}
41
42 // Note that this class doesn't adhere to the typical Skia style rules; function names are
43 // capitalized, and we don't use `this->` prefixes. This helps nested expressions flow more
44 // naturally.
45
46 std::unique_ptr<Expression> Ref(const Variable* var) const {
47 return VariableReference::Make(Position(), var);
48 }
49
50 std::unique_ptr<Expression> Field(const Variable* var, int idx) const {
51 return FieldAccess::Make(fContext, Position(), Ref(var), idx,
52 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
53 }
54
55 std::unique_ptr<Expression> Swizzle(std::unique_ptr<Expression> base, ComponentArray c) const {
56 Position pos = base->fPosition;
57 return Swizzle::Make(fContext, pos, std::move(base), std::move(c));
58 }
59
60 std::unique_ptr<Expression> Index(std::unique_ptr<Expression> base,
61 std::unique_ptr<Expression> idx) const {
62 Position pos = base->fPosition.rangeThrough(idx->fPosition);
63 return IndexExpression::Make(fContext, pos, std::move(base), std::move(idx));
64 }
65
66 std::unique_ptr<Expression> Binary(std::unique_ptr<Expression> l,
67 Operator op,
68 std::unique_ptr<Expression> r) const {
69 Position pos = l->fPosition.rangeThrough(r->fPosition);
70 return BinaryExpression::Make(fContext, pos, std::move(l), op, std::move(r));
71 }
72
73 std::unique_ptr<Expression> Mul(std::unique_ptr<Expression> l,
74 std::unique_ptr<Expression> r) const {
75 return Binary(std::move(l), OperatorKind::STAR, std::move(r));
76 }
77
78 std::unique_ptr<Expression> Add(std::unique_ptr<Expression> l,
79 std::unique_ptr<Expression> r) const {
80 return Binary(std::move(l), OperatorKind::PLUS, std::move(r));
81 }
82
83 std::unique_ptr<Expression> Float(float value) const {
85 }
86
87 std::unique_ptr<Expression> Int(int value) const {
89 }
90
91 std::unique_ptr<Expression> CtorXYZW(std::unique_ptr<Expression> xy,
92 std::unique_ptr<Expression> z,
93 std::unique_ptr<Expression> w) const {
95 args.push_back(std::move(xy));
96 args.push_back(std::move(z));
97 args.push_back(std::move(w));
99 std::move(args));
100 }
101
102 std::unique_ptr<Statement> Assign(std::unique_ptr<Expression> l,
103 std::unique_ptr<Expression> r) const {
106 Binary(std::move(l), OperatorKind::EQ, std::move(r)));
107 }
108
109 static std::unique_ptr<Expression> LoadFloatBuffer(const Context& context,
110 const SkSL::ShaderCaps& shaderCaps,
111 Position position,
112 std::unique_ptr<Expression> idx) {
113 SkASSERT(shaderCaps.fFloatBufferArrayName != nullptr);
114
115 const Symbol* floatBufferArraySymbol =
116 context.fSymbolTable->find(shaderCaps.fFloatBufferArrayName);
117 SkASSERT(floatBufferArraySymbol->is<FieldSymbol>());
118
119 const FieldSymbol& floatBufferArrayField = floatBufferArraySymbol->as<FieldSymbol>();
120 auto floatBufferArrayAccess = std::make_unique<FieldAccess>(
121 position,
122 std::make_unique<VariableReference>(
123 position,
124 &floatBufferArrayField.owner(),
126 ),
127 floatBufferArrayField.fieldIndex(),
129
131 context,
132 position,
133 std::move(floatBufferArrayAccess),
134 std::move(idx));
135 }
136
138};
139
140} // namespace SkSL
141
142#endif
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
SkPoint pos
#define SkASSERT(cond)
Definition: SkAssert.h:116
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > left, Operator op, std::unique_ptr< Expression > right)
const std::unique_ptr< Type > fInt
const std::unique_ptr< Type > fFloat4
const std::unique_ptr< Type > fFloat
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, ExpressionArray args)
const BuiltinTypes & fTypes
Definition: SkSLContext.h:30
SymbolTable * fSymbolTable
Definition: SkSLContext.h:48
static std::unique_ptr< Statement > Make(const Context &context, std::unique_ptr< Expression > expr)
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > base, int fieldIndex, OwnerKind ownerKind=OwnerKind::kDefault)
const Variable & owner() const
int fieldIndex() const
bool is() const
Definition: SkSLIRNode.h:124
const T & as() const
Definition: SkSLIRNode.h:133
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > base, std::unique_ptr< Expression > index)
static std::unique_ptr< Literal > MakeInt(const Context &context, Position pos, SKSL_INT value)
Definition: SkSLLiteral.h:54
static std::unique_ptr< Literal > MakeFloat(const Context &context, Position pos, float value)
Definition: SkSLLiteral.h:43
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > expr, ComponentArray inComponents)
const Symbol * find(std::string_view name) const
static std::unique_ptr< Expression > Make(Position pos, const Variable *variable, RefKind refKind=RefKind::kRead)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint8_t value
bool UpdateVariableRefKind(Expression *expr, VariableRefKind kind, ErrorReporter *errors=nullptr)
SkScalar w
std::unique_ptr< Expression > Ref(const Variable *var) const
Definition: SkSLIRHelpers.h:46
std::unique_ptr< Expression > Swizzle(std::unique_ptr< Expression > base, ComponentArray c) const
Definition: SkSLIRHelpers.h:55
std::unique_ptr< Expression > Add(std::unique_ptr< Expression > l, std::unique_ptr< Expression > r) const
Definition: SkSLIRHelpers.h:78
IRHelpers(const Context &c)
Definition: SkSLIRHelpers.h:40
std::unique_ptr< Expression > Field(const Variable *var, int idx) const
Definition: SkSLIRHelpers.h:50
const Context & fContext
std::unique_ptr< Expression > Mul(std::unique_ptr< Expression > l, std::unique_ptr< Expression > r) const
Definition: SkSLIRHelpers.h:73
std::unique_ptr< Expression > Index(std::unique_ptr< Expression > base, std::unique_ptr< Expression > idx) const
Definition: SkSLIRHelpers.h:60
std::unique_ptr< Expression > CtorXYZW(std::unique_ptr< Expression > xy, std::unique_ptr< Expression > z, std::unique_ptr< Expression > w) const
Definition: SkSLIRHelpers.h:91
static std::unique_ptr< Expression > LoadFloatBuffer(const Context &context, const SkSL::ShaderCaps &shaderCaps, Position position, std::unique_ptr< Expression > idx)
std::unique_ptr< Expression > Float(float value) const
Definition: SkSLIRHelpers.h:83
std::unique_ptr< Expression > Int(int value) const
Definition: SkSLIRHelpers.h:87
std::unique_ptr< Expression > Binary(std::unique_ptr< Expression > l, Operator op, std::unique_ptr< Expression > r) const
Definition: SkSLIRHelpers.h:66
std::unique_ptr< Statement > Assign(std::unique_ptr< Expression > l, std::unique_ptr< Expression > r) const
const char * fFloatBufferArrayName
Definition: SkSLUtil.h:160