xref: /aosp_15_r20/external/skia/src/sksl/SkSLModuleDataDefault.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 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/SkTypes.h"
9 #include "src/sksl/SkSLGraphiteModules.h"
10 #include "src/sksl/SkSLModule.h"
11 
12 #include <string>
13 
14 // We include minified SkSL module code and pass it directly to the compiler.
15 #if defined(SK_ENABLE_OPTIMIZE_SIZE) || !defined(SK_DEBUG)
16 #include "src/sksl/generated/sksl_compute.minified.sksl"
17 #include "src/sksl/generated/sksl_frag.minified.sksl"
18 #include "src/sksl/generated/sksl_gpu.minified.sksl"
19 #include "src/sksl/generated/sksl_public.minified.sksl"
20 #include "src/sksl/generated/sksl_rt_shader.minified.sksl"
21 #include "src/sksl/generated/sksl_shared.minified.sksl"
22 #include "src/sksl/generated/sksl_vert.minified.sksl"
23 #else
24 #include "src/sksl/generated/sksl_compute.unoptimized.sksl"
25 #include "src/sksl/generated/sksl_frag.unoptimized.sksl"
26 #include "src/sksl/generated/sksl_gpu.unoptimized.sksl"
27 #include "src/sksl/generated/sksl_public.unoptimized.sksl"
28 #include "src/sksl/generated/sksl_rt_shader.unoptimized.sksl"
29 #include "src/sksl/generated/sksl_shared.unoptimized.sksl"
30 #include "src/sksl/generated/sksl_vert.unoptimized.sksl"
31 #endif
32 
33 // We don't load the graphite modules by default because we don't want to bloat the Ganesh-only
34 // build with Graphite's modules. These will be filled in during initialization of the
35 // Graphite backend.
36 static const char* sdata_sksl_graphite_frag = "";
37 static const char* sdata_sksl_graphite_frag_es2 = "";
38 static const char* sdata_sksl_graphite_vert = "";
39 static const char* sdata_sksl_graphite_vert_es2 = "";
40 
41 namespace SkSL {
42 
GetModuleData(ModuleType type,const char *)43 std::string GetModuleData(ModuleType type, const char* /*filename*/) {
44 #define M(name) case ModuleType::name: return std::string(SKSL_MINIFIED_##name);
45 // Creating a std::string with a nullptr is UB
46 #define G(name)                               \
47     case ModuleType::name:                    \
48         if (sdata_##name) {                   \
49             return std::string(sdata_##name); \
50         } else {                              \
51             return "";                        \
52         }
53 
54     switch (type) {
55         M(sksl_shared)
56         M(sksl_compute)
57         M(sksl_frag)
58         M(sksl_gpu)
59         M(sksl_public)
60         M(sksl_rt_shader)
61         M(sksl_vert)
62 
63         G(sksl_graphite_frag)
64         G(sksl_graphite_frag_es2)
65         G(sksl_graphite_vert)
66         G(sksl_graphite_vert_es2)
67         default:
68             SkUNREACHABLE;
69     }
70 #undef M
71 }
72 
73 namespace Loader {
SetGraphiteModuleData(const GraphiteModules & modules)74 void SetGraphiteModuleData(const GraphiteModules& modules) {
75     SkASSERTF(sdata_sksl_graphite_frag[0] == '\0', "We should only initialize this once");
76     sdata_sksl_graphite_frag = modules.fFragmentShader;
77     sdata_sksl_graphite_frag_es2 = modules.fFragmentShaderES2;
78     sdata_sksl_graphite_vert = modules.fVertexShader;
79     sdata_sksl_graphite_vert_es2 = modules.fVertexShaderES2;
80 
81     SkASSERT(sdata_sksl_graphite_frag[0] != '\0');
82     SkASSERT(sdata_sksl_graphite_vert[0] != '\0');
83     // TODO(jamesgk, kjlubick) Add check when ES2 code is included
84 }
85 }  // namespace Loader
86 
87 }  // namespace SkSL
88