• 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 #ifdef SKSL_EXT
111                                && storage != Variable::Storage::kGlobal
112 #endif
113                                && storage != Variable::Storage::kParameter) {
114         context.fErrors->error(pos, "unsized arrays are not permitted here");
115     }
116     if (ProgramConfig::IsCompute(context.fConfig->fKind) && layout.fBuiltin == -1) {
117         if (storage == Variable::Storage::kGlobal) {
118             if (flags & ModifierFlag::kIn) {
119                 context.fErrors->error(pos, "pipeline inputs not permitted in compute shaders");
120             } else if (flags & ModifierFlag::kOut) {
121                 context.fErrors->error(pos, "pipeline outputs not permitted in compute shaders");
122             }
123         }
124     }
125     if (storage == Variable::Storage::kParameter) {
126         // The `in` modifier on function parameters is implicit, so we can replace `in float x` with
127         // `float x`. This prevents any ambiguity when matching a function by its param types.
128         if ((flags & (ModifierFlag::kOut | ModifierFlag::kIn)) == ModifierFlag::kIn) {
129             flags &= ~(ModifierFlag::kOut | ModifierFlag::kIn);
130         }
131     }
132 
133     // Invent a mangled name for the variable, if it needs one.
134     std::string mangledName;
135     if (skstd::starts_with(name, '$')) {
136         // The $ prefix will fail to compile in GLSL, so replace it with `sk_Priv`.
137         mangledName = "sk_Priv" + std::string(name.substr(1));
138     } else if (FindIntrinsicKind(name) != kNotIntrinsic) {
139         // Having a variable name overlap an intrinsic name will prevent us from calling the
140         // intrinsic, but it's not illegal for user names to shadow a global symbol.
141         // Mangle the name to avoid a possible collision.
142         mangledName = Mangler{}.uniqueName(name, context.fSymbolTable);
143     }
144 
145     return Make(pos, modifiersPos, layout, flags, type, name, std::move(mangledName),
146                 context.fConfig->isBuiltinCode(), storage);
147 }
148 
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)149 std::unique_ptr<Variable> Variable::Make(Position pos,
150                                          Position modifiersPosition,
151                                          const Layout& layout,
152                                          ModifierFlags flags,
153                                          const Type* type,
154                                          std::string_view name,
155                                          std::string mangledName,
156                                          bool builtin,
157                                          Variable::Storage storage) {
158     // the `in` modifier on function parameters is implicit and should have been removed
159     SkASSERT(!(storage == Variable::Storage::kParameter &&
160                (flags & (ModifierFlag::kOut | ModifierFlag::kIn)) == ModifierFlag::kIn));
161 
162     if (type->componentType().isInterfaceBlock() || !mangledName.empty() ||
163         layout != kDefaultLayout) {
164         return std::make_unique<ExtendedVariable>(pos,
165                                                   modifiersPosition,
166                                                   layout,
167                                                   flags,
168                                                   name,
169                                                   type,
170                                                   builtin,
171                                                   storage,
172                                                   std::move(mangledName));
173     } else {
174         return std::make_unique<Variable>(pos,
175                                           modifiersPosition,
176                                           flags,
177                                           name,
178                                           type,
179                                           builtin,
180                                           storage);
181     }
182 }
183 
MakeScratchVariable(const Context & context,Mangler & mangler,std::string_view baseName,const Type * type,SymbolTable * symbolTable,std::unique_ptr<Expression> initialValue)184 Variable::ScratchVariable Variable::MakeScratchVariable(const Context& context,
185                                                         Mangler& mangler,
186                                                         std::string_view baseName,
187                                                         const Type* type,
188                                                         SymbolTable* symbolTable,
189                                                         std::unique_ptr<Expression> initialValue) {
190     // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so
191     // replace them if they ever appear here. If this happens, we likely forgot to coerce a type
192     // somewhere during compilation.
193     if (type->isLiteral()) {
194         SkDEBUGFAIL("found a $literal type in MakeScratchVariable");
195         type = &type->scalarTypeForLiteral();
196     }
197 
198     // Provide our new variable with a unique name, and add it to our symbol table.
199     const std::string* name =
200             symbolTable->takeOwnershipOfString(mangler.uniqueName(baseName, symbolTable));
201 
202     // Create our new variable and add it to the symbol table.
203     ScratchVariable result;
204     auto var = std::make_unique<Variable>(initialValue ? initialValue->fPosition : Position(),
205                                           /*modifiersPosition=*/Position(),
206                                           ModifierFlag::kNone,
207                                           name->c_str(),
208                                           type,
209                                           symbolTable->isBuiltin(),
210                                           Variable::Storage::kLocal);
211 
212     // If we are creating an array type, reduce it to base type plus array-size.
213     int arraySize = 0;
214     if (type->isArray()) {
215         arraySize = type->columns();
216         type = &type->componentType();
217     }
218     // Create our variable declaration.
219     result.fVarDecl = VarDeclaration::Make(context, var.get(), type, arraySize,
220                                            std::move(initialValue));
221     result.fVarSymbol = symbolTable->add(context, std::move(var));
222     return result;
223 }
224 
225 } // namespace SkSL
226