Debian Codegraph @

src/sksl/SkSLModuleLoader.cpp

raw
1 /*
2 * Copyright 2022 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/SkSLModuleLoader.h"
9
10 #include "include/core/SkTypes.h"
11 #include "include/private/base/SkMutex.h"
12 #include "src/base/SkNoDestructor.h"
13 #include "src/sksl/SkSLBuiltinTypes.h"
14 #include "src/sksl/SkSLCompiler.h"
15 #include "src/sksl/SkSLModule.h"
16 #include "src/sksl/SkSLPosition.h"
17 #include "src/sksl/SkSLProgramKind.h"
18 #include "src/sksl/ir/SkSLIRNode.h"
19 #include "src/sksl/ir/SkSLLayout.h"
20 #include "src/sksl/ir/SkSLModifierFlags.h"
21 #include "src/sksl/ir/SkSLProgramElement.h"
22 #include "src/sksl/ir/SkSLSymbolTable.h"
23 #include "src/sksl/ir/SkSLType.h"
24 #include "src/sksl/ir/SkSLVariable.h"
25
26 #include <algorithm>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 #include <vector>
31
32 #define MODULE_DATA(type) ModuleType::type, GetModuleData(ModuleType::type, #type ".sksl")
33
34 namespace SkSL {
35
36 #define TYPE(t) &BuiltinTypes::f ## t
37
38 static constexpr BuiltinTypePtr kRootTypes[] = {
39 TYPE(Void),
40
41 TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4),
42 TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4),
43 TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4),
44 TYPE( UInt), TYPE( UInt2), TYPE( UInt3), TYPE( UInt4),
45 TYPE( Short), TYPE( Short2), TYPE( Short3), TYPE( Short4),
46 TYPE(UShort), TYPE(UShort2), TYPE(UShort3), TYPE(UShort4),
47 TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4),
48
49 TYPE(Float2x2), TYPE(Float2x3), TYPE(Float2x4),
50 TYPE(Float3x2), TYPE(Float3x3), TYPE(Float3x4),
51 TYPE(Float4x2), TYPE(Float4x3), TYPE(Float4x4),
52
53 TYPE(Half2x2), TYPE(Half2x3), TYPE(Half2x4),
54 TYPE(Half3x2), TYPE(Half3x3), TYPE(Half3x4),
55 TYPE(Half4x2), TYPE(Half4x3), TYPE(Half4x4),
56
57 TYPE(SquareMat), TYPE(SquareHMat),
58 TYPE(Mat), TYPE(HMat),
59
60 // TODO(skbug.com/40043431): generic short/ushort
61 TYPE(GenType), TYPE(GenIType), TYPE(GenUType),
62 TYPE(GenHType), /* (GenSType) (GenUSType) */
63 TYPE(GenBType),
64 TYPE(IntLiteral),
65 TYPE(FloatLiteral),
66
67 TYPE(Vec), TYPE(IVec), TYPE(UVec),
68 TYPE(HVec), TYPE(SVec), TYPE(USVec),
69 TYPE(BVec),
70
71 TYPE(ColorFilter),
72 TYPE(Shader),
73 TYPE(Blender),
74 };
75
76 static constexpr BuiltinTypePtr kPrivateTypes[] = {
77 TYPE(Sampler2D), TYPE(SamplerExternalOES), TYPE(Sampler2DRect),
78
79 TYPE(SubpassInput), TYPE(SubpassInputMS),
80
81 TYPE(Sampler),
82 TYPE(Texture2D_sample),
83 TYPE(Texture2D), TYPE(ReadOnlyTexture2D), TYPE(WriteOnlyTexture2D),
84 TYPE(GenTexture2D), TYPE(ReadableTexture2D), TYPE(WritableTexture2D),
85
86 TYPE(AtomicUInt), TYPE(Atomic_uint),
87 };
88
89 #undef TYPE
90
91 struct ModuleLoader::Impl {
92 Impl();
93
94 void makeRootSymbolTable();
95
96 // This mutex is taken when ModuleLoader::Get is called, and released when the returned
97 // ModuleLoader object falls out of scope.
98 SkMutex fMutex;
99 const BuiltinTypes fBuiltinTypes;
100
101 std::unique_ptr<const Module> fRootModule;
102
103 std::unique_ptr<const Module> fSharedModule; // [Root] + Public intrinsics
104 std::unique_ptr<const Module> fGPUModule; // [Shared] + Non-public intrinsics/
105 // helper functions
106 std::unique_ptr<const Module> fVertexModule; // [GPU] + Vertex stage decls
107 std::unique_ptr<const Module> fFragmentModule; // [GPU] + Fragment stage decls
108 std::unique_ptr<const Module> fComputeModule; // [GPU] + Compute stage decls
109 std::unique_ptr<const Module> fGraphiteVertexModule; // [Vert] + Graphite vertex helpers
110 std::unique_ptr<const Module> fGraphiteFragmentModule; // [Frag] + Graphite fragment helpers
111
112 std::unique_ptr<const Module> fPublicModule; // [Shared] minus Private types +
113 // Runtime effect intrinsics
114 std::unique_ptr<const Module> fRuntimeShaderModule; // [Public] + Runtime shader decls
115 };
116
117 ModuleLoader ModuleLoader::Get() {
118 static SkNoDestructor<ModuleLoader::Impl> sModuleLoaderImpl;
119 return ModuleLoader(*sModuleLoaderImpl);
120 }
121
122 ModuleLoader::ModuleLoader(ModuleLoader::Impl& m) : fModuleLoader(m) {
123 fModuleLoader.fMutex.acquire();
124 }
125
126 ModuleLoader::~ModuleLoader() {
127 fModuleLoader.fMutex.release();
128 }
129
130 void ModuleLoader::unloadModules() {
131 fModuleLoader.fSharedModule = nullptr;
132 fModuleLoader.fGPUModule = nullptr;
133 fModuleLoader.fVertexModule = nullptr;
134 fModuleLoader.fFragmentModule = nullptr;
135 fModuleLoader.fComputeModule = nullptr;
136 fModuleLoader.fGraphiteVertexModule = nullptr;
137 fModuleLoader.fGraphiteFragmentModule = nullptr;
138 fModuleLoader.fPublicModule = nullptr;
139 fModuleLoader.fRuntimeShaderModule = nullptr;
140 }
141
142 ModuleLoader::Impl::Impl() {
143 this->makeRootSymbolTable();
144 }
145
146 static std::unique_ptr<Module> compile_and_shrink(SkSL::Compiler* compiler,
147 ProgramKind kind,
148 ModuleType moduleType,
149 std::string moduleSource,
150 const Module* parent) {
151 std::unique_ptr<Module> m = compiler->compileModule(kind,
152 moduleType,
153 std::move(moduleSource),
154 parent,
155 /*shouldInline=*/true);
156 if (!m) {
157 SK_ABORT("Unable to load module %s", ModuleTypeToString(moduleType));
158 }
159
160 // We can eliminate FunctionPrototypes without changing the meaning of the module; the function
161 // declaration is still safely in the symbol table. This only impacts our ability to recreate
162 // the input verbatim, which we don't care about at runtime.
163 m->fElements.erase(std::remove_if(m->fElements.begin(), m->fElements.end(),
164 [](const std::unique_ptr<ProgramElement>& element) {
165 switch (element->kind()) {
166 case ProgramElement::Kind::kFunction:
167 case ProgramElement::Kind::kGlobalVar:
168 case ProgramElement::Kind::kInterfaceBlock:
169 case ProgramElement::Kind::kStructDefinition:
170 // We need to preserve these.
171 return false;
172
173 case ProgramElement::Kind::kFunctionPrototype:
174 // These are already in the symbol table; the
175 // ProgramElement isn't needed anymore.
176 return true;
177
178 default:
179 SkDEBUGFAILF("Unsupported element: %s\n",
180 element->description().c_str());
181 return false;
182 }
183 }),
184 m->fElements.end());
185
186 m->fElements.shrink_to_fit();
187 return m;
188 }
189
190 const BuiltinTypes& ModuleLoader::builtinTypes() {
191 return fModuleLoader.fBuiltinTypes;
192 }
193
194 const Module* ModuleLoader::rootModule() {
195 return fModuleLoader.fRootModule.get();
196 }
197
198 void ModuleLoader::addPublicTypeAliases(const SkSL::Module* module) {
199 const SkSL::BuiltinTypes& types = this->builtinTypes();
200 SymbolTable* symbols = module->fSymbols.get();
201
202 // Add some aliases to the runtime effect modules so that it's friendlier, and more like GLSL.
203 symbols->addWithoutOwnershipOrDie(types.fVec2.get());
204 symbols->addWithoutOwnershipOrDie(types.fVec3.get());
205 symbols->addWithoutOwnershipOrDie(types.fVec4.get());
206
207 symbols->addWithoutOwnershipOrDie(types.fIVec2.get());
208 symbols->addWithoutOwnershipOrDie(types.fIVec3.get());
209 symbols->addWithoutOwnershipOrDie(types.fIVec4.get());
210
211 symbols->addWithoutOwnershipOrDie(types.fUVec2.get());
212 symbols->addWithoutOwnershipOrDie(types.fUVec3.get());
213 symbols->addWithoutOwnershipOrDie(types.fUVec4.get());
214
215 symbols->addWithoutOwnershipOrDie(types.fBVec2.get());
216 symbols->addWithoutOwnershipOrDie(types.fBVec3.get());
217 symbols->addWithoutOwnershipOrDie(types.fBVec4.get());
218
219 symbols->addWithoutOwnershipOrDie(types.fMat2.get());
220 symbols->addWithoutOwnershipOrDie(types.fMat3.get());
221 symbols->addWithoutOwnershipOrDie(types.fMat4.get());
222
223 symbols->addWithoutOwnershipOrDie(types.fMat2x2.get());
224 symbols->addWithoutOwnershipOrDie(types.fMat2x3.get());
225 symbols->addWithoutOwnershipOrDie(types.fMat2x4.get());
226 symbols->addWithoutOwnershipOrDie(types.fMat3x2.get());
227 symbols->addWithoutOwnershipOrDie(types.fMat3x3.get());
228 symbols->addWithoutOwnershipOrDie(types.fMat3x4.get());
229 symbols->addWithoutOwnershipOrDie(types.fMat4x2.get());
230 symbols->addWithoutOwnershipOrDie(types.fMat4x3.get());
231 symbols->addWithoutOwnershipOrDie(types.fMat4x4.get());
232
233 // Hide all the private symbols by aliasing them all to "invalid". This will prevent code from
234 // using built-in names like `sampler2D` as variable names.
235 for (BuiltinTypePtr privateType : kPrivateTypes) {
236 symbols->inject(Type::MakeAliasType((types.*privateType)->name(), *types.fInvalid));
237 }
238 }
239
240 const Module* ModuleLoader::loadPublicModule(SkSL::Compiler* compiler) {
241 if (!fModuleLoader.fPublicModule) {
242 const Module* sharedModule = this->loadSharedModule(compiler);
243 fModuleLoader.fPublicModule = compile_and_shrink(compiler,
244 ProgramKind::kFragment,
245 MODULE_DATA(sksl_public),
246 sharedModule);
247 this->addPublicTypeAliases(fModuleLoader.fPublicModule.get());
248 }
249 return fModuleLoader.fPublicModule.get();
250 }
251
252 const Module* ModuleLoader::loadPrivateRTShaderModule(SkSL::Compiler* compiler) {
253 if (!fModuleLoader.fRuntimeShaderModule) {
254 const Module* publicModule = this->loadPublicModule(compiler);
255 fModuleLoader.fRuntimeShaderModule = compile_and_shrink(compiler,
256 ProgramKind::kFragment,
257 MODULE_DATA(sksl_rt_shader),
258 publicModule);
259 }
260 return fModuleLoader.fRuntimeShaderModule.get();
261 }
262
263 const Module* ModuleLoader::loadSharedModule(SkSL::Compiler* compiler) {
264 if (!fModuleLoader.fSharedModule) {
265 const Module* rootModule = this->rootModule();
266 fModuleLoader.fSharedModule = compile_and_shrink(compiler,
267 ProgramKind::kFragment,
268 MODULE_DATA(sksl_shared),
269 rootModule);
270 }
271 return fModuleLoader.fSharedModule.get();
272 }
273
274 const Module* ModuleLoader::loadGPUModule(SkSL::Compiler* compiler) {
275 if (!fModuleLoader.fGPUModule) {
276 const Module* sharedModule = this->loadSharedModule(compiler);
277 fModuleLoader.fGPUModule = compile_and_shrink(compiler,
278 ProgramKind::kFragment,
279 MODULE_DATA(sksl_gpu),
280 sharedModule);
281 }
282 return fModuleLoader.fGPUModule.get();
283 }
284
285 const Module* ModuleLoader::loadFragmentModule(SkSL::Compiler* compiler) {
286 if (!fModuleLoader.fFragmentModule) {
287 const Module* gpuModule = this->loadGPUModule(compiler);
288 fModuleLoader.fFragmentModule = compile_and_shrink(compiler,
289 ProgramKind::kFragment,
290 MODULE_DATA(sksl_frag),
291 gpuModule);
292 }
293 return fModuleLoader.fFragmentModule.get();
294 }
295
296 const Module* ModuleLoader::loadVertexModule(SkSL::Compiler* compiler) {
297 if (!fModuleLoader.fVertexModule) {
298 const Module* gpuModule = this->loadGPUModule(compiler);
299 fModuleLoader.fVertexModule = compile_and_shrink(compiler,
300 ProgramKind::kVertex,
301 MODULE_DATA(sksl_vert),
302 gpuModule);
303 }
304 return fModuleLoader.fVertexModule.get();
305 }
306
307 const Module* ModuleLoader::loadComputeModule(SkSL::Compiler* compiler) {
308 if (!fModuleLoader.fComputeModule) {
309 const Module* gpuModule = this->loadGPUModule(compiler);
310 fModuleLoader.fComputeModule = compile_and_shrink(compiler,
311 ProgramKind::kCompute,
312 MODULE_DATA(sksl_compute),
313 gpuModule);
314 }
315 return fModuleLoader.fComputeModule.get();
316 }
317
318 const Module* ModuleLoader::loadGraphiteFragmentModule(SkSL::Compiler* compiler) {
319 if (!fModuleLoader.fGraphiteFragmentModule) {
320 const Module* fragmentModule = this->loadFragmentModule(compiler);
321 fModuleLoader.fGraphiteFragmentModule = compile_and_shrink(compiler,
322 ProgramKind::kGraphiteFragment,
323 MODULE_DATA(sksl_graphite_frag),
324 fragmentModule);
325 }
326 return fModuleLoader.fGraphiteFragmentModule.get();
327 }
328
329 const Module* ModuleLoader::loadGraphiteVertexModule(SkSL::Compiler* compiler) {
330 if (!fModuleLoader.fGraphiteVertexModule) {
331 const Module* vertexModule = this->loadVertexModule(compiler);
332 fModuleLoader.fGraphiteVertexModule = compile_and_shrink(compiler,
333 ProgramKind::kGraphiteVertex,
334 MODULE_DATA(sksl_graphite_vert),
335 vertexModule);
336 }
337 return fModuleLoader.fGraphiteVertexModule.get();
338 }
339
340 void ModuleLoader::Impl::makeRootSymbolTable() {
341 auto rootModule = std::make_unique<Module>();
342 rootModule->fSymbols = std::make_unique<SymbolTable>(/*builtin=*/true);
343
344 for (BuiltinTypePtr rootType : kRootTypes) {
345 rootModule->fSymbols->addWithoutOwnershipOrDie((fBuiltinTypes.*rootType).get());
346 }
347
348 for (BuiltinTypePtr privateType : kPrivateTypes) {
349 rootModule->fSymbols->addWithoutOwnershipOrDie((fBuiltinTypes.*privateType).get());
350 }
351
352 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
353 // treat it as builtin (ie, no need to clone it into the Program).
354 rootModule->fSymbols->addOrDie(Variable::Make(/*pos=*/Position(),
355 /*modifiersPosition=*/Position(),
356 Layout{},
357 ModifierFlag::kNone,
358 fBuiltinTypes.fSkCaps.get(),
359 "sk_Caps",
360 /*mangledName=*/"",
361 /*builtin=*/false,
362 Variable::Storage::kGlobal));
363 fRootModule = std::move(rootModule);
364 }
365
366 } // namespace SkSL