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 "include/core/SkSpan.h"
9 #include "include/core/SkTypes.h"
10 #include "include/private/SkSLIRNode.h"
11 #include "include/private/SkSLLayout.h"
12 #include "include/private/SkSLModifiers.h"
13 #include "include/private/SkSLProgramElement.h"
14 #include "include/private/SkSLSymbol.h"
15 #include "src/core/SkTHash.h"
16 #include "src/sksl/SkSLBuiltinTypes.h"
17 #include "src/sksl/SkSLCompiler.h"
18 #include "src/sksl/SkSLContext.h"
19 #include "src/sksl/SkSLProgramSettings.h"
20 #include "src/sksl/SkSLUtil.h"
21 #include "src/sksl/analysis/SkSLProgramUsage.h"
22 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
23 #include "src/sksl/ir/SkSLFunctionDefinition.h"
24 #include "src/sksl/ir/SkSLInterfaceBlock.h"
25 #include "src/sksl/ir/SkSLProgram.h"
26 #include "src/sksl/ir/SkSLSymbolTable.h"
27 #include "src/sksl/ir/SkSLType.h"
28 #include "src/sksl/ir/SkSLVarDeclarations.h"
29 #include "src/sksl/ir/SkSLVariable.h"
30 #include "src/sksl/transform/SkSLTransform.h"
31
32 #include <algorithm>
33 #include <memory>
34 #include <string_view>
35 #include <vector>
36
37 namespace SkSL {
38 namespace Transform {
39 namespace {
40
41 class BuiltinVariableScanner {
42 public:
BuiltinVariableScanner(const Context & context,const SymbolTable & symbols)43 BuiltinVariableScanner(const Context& context, const SymbolTable& symbols)
44 : fContext(context)
45 , fSymbols(symbols) {}
46
addDeclaringElement(const ProgramElement * decl)47 void addDeclaringElement(const ProgramElement* decl) {
48 // Make sure we only add a built-in variable once. We only have a small handful of built-in
49 // variables to declare, so linear search here is good enough.
50 if (std::find(fNewElements.begin(), fNewElements.end(), decl) == fNewElements.end()) {
51 fNewElements.push_back(decl);
52 }
53 }
54
addDeclaringElement(const Symbol * symbol)55 void addDeclaringElement(const Symbol* symbol) {
56 if (!symbol || !symbol->is<Variable>()) {
57 return;
58 }
59 const Variable& var = symbol->as<Variable>();
60 if (const GlobalVarDeclaration* decl = var.globalVarDeclaration()) {
61 this->addDeclaringElement(decl);
62 } else if (const InterfaceBlock* block = var.interfaceBlock()) {
63 this->addDeclaringElement(block);
64 } else {
65 // Double-check that this variable isn't associated with a global or an interface block.
66 // (Locals and parameters will come along naturally as part of the associated function.)
67 SkASSERTF(var.storage() != VariableStorage::kGlobal &&
68 var.storage() != VariableStorage::kInterfaceBlock,
69 "%.*s", (int)var.name().size(), var.name().data());
70 }
71 }
72
addImplicitFragColorWrite(SkSpan<const std::unique_ptr<ProgramElement>> elements)73 void addImplicitFragColorWrite(SkSpan<const std::unique_ptr<ProgramElement>> elements) {
74 for (const std::unique_ptr<ProgramElement>& pe : elements) {
75 if (!pe->is<FunctionDefinition>()) {
76 continue;
77 }
78 const FunctionDefinition& funcDef = pe->as<FunctionDefinition>();
79 if (funcDef.declaration().isMain()) {
80 if (funcDef.declaration().returnType().matches(*fContext.fTypes.fHalf4)) {
81 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
82 // otherwise unreferenced.
83 this->addDeclaringElement(fSymbols.findBuiltinSymbol(Compiler::FRAGCOLOR_NAME));
84 }
85 // Now that main() has been found, we can stop scanning.
86 break;
87 }
88 }
89 }
90
GlobalVarBuiltinName(const ProgramElement & elem)91 static std::string_view GlobalVarBuiltinName(const ProgramElement& elem) {
92 return elem.as<GlobalVarDeclaration>().varDeclaration().var()->name();
93 }
94
InterfaceBlockName(const ProgramElement & elem)95 static std::string_view InterfaceBlockName(const ProgramElement& elem) {
96 return elem.as<InterfaceBlock>().instanceName();
97 }
98
sortNewElements()99 void sortNewElements() {
100 std::sort(fNewElements.begin(),
101 fNewElements.end(),
102 [](const ProgramElement* a, const ProgramElement* b) {
103 if (a->kind() != b->kind()) {
104 return a->kind() < b->kind();
105 }
106 switch (a->kind()) {
107 case ProgramElement::Kind::kGlobalVar:
108 SkASSERT(GlobalVarBuiltinName(*a) != GlobalVarBuiltinName(*b));
109 return GlobalVarBuiltinName(*a) < GlobalVarBuiltinName(*b);
110
111 case ProgramElement::Kind::kInterfaceBlock:
112 SkASSERT(InterfaceBlockName(*a) != InterfaceBlockName(*b));
113 return InterfaceBlockName(*a) < InterfaceBlockName(*b);
114
115 default:
116 SkUNREACHABLE;
117 }
118 });
119 }
120
121 const Context& fContext;
122 const SymbolTable& fSymbols;
123 std::vector<const ProgramElement*> fNewElements;
124 };
125
126 } // namespace
127
FindAndDeclareBuiltinVariables(Program & program)128 void FindAndDeclareBuiltinVariables(Program& program) {
129 const Context& context = *program.fContext;
130 const SymbolTable& symbols = *program.fSymbols;
131 BuiltinVariableScanner scanner(context, symbols);
132
133 if (ProgramConfig::IsFragment(program.fConfig->fKind)) {
134 // Find main() in the program and check its return type.
135 // If it's half4, we treat that as an implicit write to sk_FragColor and add a reference.
136 scanner.addImplicitFragColorWrite(program.fOwnedElements);
137
138 // Vulkan requires certain builtin variables be present, even if they're unused. At one
139 // time, validation errors would result if sk_Clockwise was missing. Now, it's just (Adreno)
140 // driver bugs that drop or corrupt draws if they're missing.
141 scanner.addDeclaringElement(symbols.findBuiltinSymbol("sk_Clockwise"));
142 }
143
144 // Scan all the variables used by the program and declare any built-ins.
145 for (const auto& [var, counts] : program.fUsage->fVariableCounts) {
146 if (var->isBuiltin()) {
147 scanner.addDeclaringElement(var);
148
149 // Set the FlipRT program input if we find sk_FragCoord or sk_Clockwise.
150 switch (var->modifiers().fLayout.fBuiltin) {
151 case SK_FRAGCOORD_BUILTIN:
152 if (context.fCaps->fCanUseFragCoord) {
153 program.fInputs.fUseFlipRTUniform =
154 !context.fConfig->fSettings.fForceNoRTFlip;
155 }
156 break;
157
158 case SK_CLOCKWISE_BUILTIN:
159 program.fInputs.fUseFlipRTUniform = !context.fConfig->fSettings.fForceNoRTFlip;
160 break;
161 }
162 }
163 }
164
165 // Sort the referenced builtin functions into a consistent order; otherwise our output will
166 // become non-deterministic. The exact order isn't particularly important.
167 scanner.sortNewElements();
168
169 // Add all the newly-declared elements to the program, and update ProgramUsage to match.
170 program.fSharedElements.insert(program.fSharedElements.begin(),
171 scanner.fNewElements.begin(),
172 scanner.fNewElements.end());
173
174 for (const ProgramElement* element : scanner.fNewElements) {
175 program.fUsage->add(*element);
176 }
177 }
178
179 } // namespace Transform
180 } // namespace SkSL
181