• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ir/SkSLVariable.h"
9 
10 #include "src/base/SkEnumBitMask.h"
11 #include "src/base/SkStringView.h"
12 #include "src/sksl/SkSLCompiler.h"
13 #include "src/sksl/SkSLContext.h"
14 #include "src/sksl/SkSLErrorReporter.h"
15 #include "src/sksl/SkSLIntrinsicList.h"
16 #include "src/sksl/SkSLMangler.h"
17 #include "src/sksl/SkSLProgramSettings.h"
18 #include "src/sksl/ir/SkSLExpression.h"
19 #include "src/sksl/ir/SkSLIRNode.h"
20 #include "src/sksl/ir/SkSLInterfaceBlock.h"
21 #include "src/sksl/ir/SkSLLayout.h"
22 #include "src/sksl/ir/SkSLSymbolTable.h"
23 #include "src/sksl/ir/SkSLVarDeclarations.h"
24 
25 #include <utility>
26 
27 namespace SkSL {
28 
29 static constexpr Layout kDefaultLayout;
30 
~Variable()31 Variable::~Variable() {
32     // Unhook this Variable from its associated VarDeclaration, since we're being deleted.
33     if (VarDeclaration* declaration = this->varDeclaration()) {
34         declaration->detachDeadVariable();
35     }
36 }
37 
~ExtendedVariable()38 ExtendedVariable::~ExtendedVariable() {
39     // Unhook this Variable from its associated InterfaceBlock, since we're being deleted.
40     if (fInterfaceBlockElement) {
41         fInterfaceBlockElement->detachDeadVariable();
42     }
43 }
44 
initialValue() const45 const Expression* Variable::initialValue() const {
46     VarDeclaration* declaration = this->varDeclaration();
47     return declaration ? declaration->value().get() : nullptr;
48 }
49 
varDeclaration() const50 VarDeclaration* Variable::varDeclaration() const {
51     if (!fDeclaringElement) {
52         return nullptr;
53     }
54     SkASSERT(fDeclaringElement->is<VarDeclaration>() ||
55              fDeclaringElement->is<GlobalVarDeclaration>());
56     return fDeclaringElement->is<GlobalVarDeclaration>()
57                ? &fDeclaringElement->as<GlobalVarDeclaration>().varDeclaration()
58                : &fDeclaringElement->as<VarDeclaration>();
59 }
60 
globalVarDeclaration() const61 GlobalVarDeclaration* Variable::globalVarDeclaration() const {
62     if (!fDeclaringElement) {
63         return nullptr;
64     }
65     SkASSERT(fDeclaringElement->is<VarDeclaration>() ||
66              fDeclaringElement->is<GlobalVarDeclaration>());
67     return fDeclaringElement->is<GlobalVarDeclaration>()
68                ? &fDeclaringElement->as<GlobalVarDeclaration>()
69                : nullptr;
70 }
71 
setVarDeclaration(VarDeclaration * declaration)72 void Variable::setVarDeclaration(VarDeclaration* declaration) {
73     SkASSERT(!fDeclaringElement || this == declaration->var());
74     if (!fDeclaringElement) {
75         fDeclaringElement = declaration;
76     }
77 }
78 
setGlobalVarDeclaration(GlobalVarDeclaration * global)79 void Variable::setGlobalVarDeclaration(GlobalVarDeclaration* global) {
80     SkASSERT(!fDeclaringElement || this == global->varDeclaration().var());
81     fDeclaringElement = global;
82 }
83 
layout() const84 const Layout& Variable::layout() const {
85     return kDefaultLayout;
86 }
87 
mangledName() const88 std::string_view ExtendedVariable::mangledName() const {
89     return fMangledName.empty() ? this->name() : fMangledName;
90 }
91 
Convert(const Context & context,Position pos,Position modifiersPos,const Layout & layout,ModifierFlags flags,const Type * type,Position namePos,std::string_view name,Storage storage)92 std::unique_ptr<Variable> Variable::Convert(const Context& context,
93                                             Position pos,
94                                             Position modifiersPos,
95                                             const Layout& layout,
96                                             ModifierFlags flags,
97                                             const Type* type,
98                                             Position namePos,
99                                             std::string_view name,
100                                             Storage storage) {
101     if (layout.fLocation == 0 &&
102         layout.fIndex == 0 &&
103         (flags & ModifierFlag::kOut) &&
104         ProgramConfig::IsFragment(context.fConfig->fKind) &&
105         name != Compiler::FRAGCOLOR_NAME) {
106         context.fErrors->error(modifiersPos,
107                                "out location=0, index=0 is reserved for sk_FragColor");
108     }
109     if (type->isUnsizedArray() && storage != Variable::Storage::kInterfaceBlock) {
110         context.fErrors->error(pos, "unsized arrays are not permitted here");
111     }
112     if (ProgramConfig::IsCompute(context.fConfig->fKind) && layout.fBuiltin == -1) {
113         if (storage == Variable::Storage::kGlobal) {
114             if (flags & ModifierFlag::kIn) {
115                 context.fErrors->error(pos, "pipeline inputs not permitted in compute shaders");
116             } else if (flags & ModifierFlag::kOut) {
117                 context.fErrors->error(pos, "pipeline outputs not permitted in compute shaders");
118             }
119         }
120     }
121     if (storage == Variable::Storage::kParameter) {
122         // The `in` modifier on function parameters is implicit, so we can replace `in float x` with
123         // `float x`. This prevents any ambiguity when matching a function by its param types.
124         if ((flags & (ModifierFlag::kOut | ModifierFlag::kIn)) == ModifierFlag::kIn) {
125             flags &= ~(ModifierFlag::kOut | ModifierFlag::kIn);
126         }
127     }
128 
129     // Invent a mangled name for the variable, if it needs one.
130     std::string mangledName;
131     if (skstd::starts_with(name, '$')) {
132         // The $ prefix will fail to compile in GLSL, so replace it with `sk_Priv`.
133         mangledName = "sk_Priv" + std::string(name.substr(1));
134     } else if (FindIntrinsicKind(name) != kNotIntrinsic) {
135         // Having a variable name overlap an intrinsic name will prevent us from calling the
136         // intrinsic, but it's not illegal for user names to shadow a global symbol.
137         // Mangle the name to avoid a possible collision.
138         mangledName = Mangler{}.uniqueName(name, context.fSymbolTable);
139     }
140 
141     return Make(pos, modifiersPos, layout, flags, type, name, std::move(mangledName),
142                 context.fConfig->fIsBuiltinCode, storage);
143 }
144 
Make(Position pos,Position modifiersPosition,const Layout & layout,ModifierFlags flags,const Type * type,std::string_view name,std::string mangledName,bool builtin,Variable::Storage storage)145 std::unique_ptr<Variable> Variable::Make(Position pos,
146                                          Position modifiersPosition,
147                                          const Layout& layout,
148                                          ModifierFlags flags,
149                                          const Type* type,
150                                          std::string_view name,
151                                          std::string mangledName,
152                                          bool builtin,
153                                          Variable::Storage storage) {
154     // the `in` modifier on function parameters is implicit and should have been removed
155     SkASSERT(!(storage == Variable::Storage::kParameter &&
156                (flags & (ModifierFlag::kOut | ModifierFlag::kIn)) == ModifierFlag::kIn));
157 
158     if (type->componentType().isInterfaceBlock() || !mangledName.empty() ||
159         layout != kDefaultLayout) {
160         return std::make_unique<ExtendedVariable>(pos,
161                                                   modifiersPosition,
162                                                   layout,
163                                                   flags,
164                                                   name,
165                                                   type,
166                                                   builtin,
167                                                   storage,
168                                                   std::move(mangledName));
169     } else {
170         return std::make_unique<Variable>(pos,
171                                           modifiersPosition,
172                                           flags,
173                                           name,
174                                           type,
175                                           builtin,
176                                           storage);
177     }
178 }
179 
MakeScratchVariable(const Context & context,Mangler & mangler,std::string_view baseName,const Type * type,SymbolTable * symbolTable,std::unique_ptr<Expression> initialValue)180 Variable::ScratchVariable Variable::MakeScratchVariable(const Context& context,
181                                                         Mangler& mangler,
182                                                         std::string_view baseName,
183                                                         const Type* type,
184                                                         SymbolTable* symbolTable,
185                                                         std::unique_ptr<Expression> initialValue) {
186     // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so
187     // replace them if they ever appear here. If this happens, we likely forgot to coerce a type
188     // somewhere during compilation.
189     if (type->isLiteral()) {
190         SkDEBUGFAIL("found a $literal type in MakeScratchVariable");
191         type = &type->scalarTypeForLiteral();
192     }
193 
194     // Provide our new variable with a unique name, and add it to our symbol table.
195     const std::string* name =
196             symbolTable->takeOwnershipOfString(mangler.uniqueName(baseName, symbolTable));
197 
198     // Create our new variable and add it to the symbol table.
199     ScratchVariable result;
200     auto var = std::make_unique<Variable>(initialValue ? initialValue->fPosition : Position(),
201                                           /*modifiersPosition=*/Position(),
202                                           ModifierFlag::kNone,
203                                           name->c_str(),
204                                           type,
205                                           symbolTable->isBuiltin(),
206                                           Variable::Storage::kLocal);
207 
208     // If we are creating an array type, reduce it to base type plus array-size.
209     int arraySize = 0;
210     if (type->isArray()) {
211         arraySize = type->columns();
212         type = &type->componentType();
213     }
214     // Create our variable declaration.
215     result.fVarDecl = VarDeclaration::Make(context, var.get(), type, arraySize,
216                                            std::move(initialValue));
217     result.fVarSymbol = symbolTable->add(context, std::move(var));
218     return result;
219 }
220 
221 } // namespace SkSL
222