ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLCompiler.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 9 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 10 | #include <memory> |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 11 | #include <unordered_set> |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 12 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 13 | #include "include/sksl/DSLCore.h" |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 14 | #include "src/core/SkScopeExit.h" |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 15 | #include "src/core/SkTraceEvent.h" |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 16 | #include "src/sksl/SkSLAnalysis.h" |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame] | 17 | #include "src/sksl/SkSLConstantFolder.h" |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 18 | #include "src/sksl/SkSLDSLParser.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/sksl/SkSLIRGenerator.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 20 | #include "src/sksl/SkSLOperators.h" |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 21 | #include "src/sksl/SkSLProgramSettings.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 22 | #include "src/sksl/SkSLRehydrator.h" |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 23 | #include "src/sksl/codegen/SkSLGLSLCodeGenerator.h" |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 24 | #include "src/sksl/codegen/SkSLMetalCodeGenerator.h" |
| 25 | #include "src/sksl/codegen/SkSLSPIRVCodeGenerator.h" |
| 26 | #include "src/sksl/codegen/SkSLSPIRVtoHLSL.h" |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 27 | #include "src/sksl/dsl/priv/DSLWriter.h" |
| 28 | #include "src/sksl/dsl/priv/DSL_priv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/sksl/ir/SkSLExpression.h" |
| 30 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 31 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 32 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 33 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 34 | #include "src/sksl/ir/SkSLNop.h" |
| 35 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 36 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 37 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 38 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 39 | #include "src/utils/SkBitSet.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 40 | |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 41 | #include <fstream> |
| 42 | |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 43 | #if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU |
| 44 | #include "include/gpu/GrContextOptions.h" |
| 45 | #include "src/gpu/GrShaderCaps.h" |
| 46 | #endif |
| 47 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 48 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 49 | #include "spirv-tools/libspirv.hpp" |
| 50 | #endif |
| 51 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 52 | #if defined(SKSL_STANDALONE) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 53 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 54 | // In standalone mode, we load the textual sksl source files. GN generates or copies these files |
| 55 | // to the skslc executable directory. The "data" in this mode is just the filename. |
| 56 | #define MODULE_DATA(name) MakeModulePath("sksl_" #name ".sksl") |
| 57 | |
| 58 | #else |
| 59 | |
| 60 | // At runtime, we load the dehydrated sksl data files. The data is a (pointer, size) pair. |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 61 | #include "src/sksl/generated/sksl_frag.dehydrated.sksl" |
| 62 | #include "src/sksl/generated/sksl_geom.dehydrated.sksl" |
| 63 | #include "src/sksl/generated/sksl_gpu.dehydrated.sksl" |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 64 | #include "src/sksl/generated/sksl_public.dehydrated.sksl" |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 65 | #include "src/sksl/generated/sksl_rt_blend.dehydrated.sksl" |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 66 | #include "src/sksl/generated/sksl_rt_colorfilter.dehydrated.sksl" |
| 67 | #include "src/sksl/generated/sksl_rt_shader.dehydrated.sksl" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 68 | #include "src/sksl/generated/sksl_vert.dehydrated.sksl" |
| 69 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 70 | #define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\ |
| 71 | SKSL_INCLUDE_sksl_##name##_LENGTH) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 72 | |
| 73 | #endif |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 74 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 75 | namespace SkSL { |
| 76 | |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 77 | // These flags allow tools like Viewer or Nanobench to override the compiler's ProgramSettings. |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 78 | Compiler::OverrideFlag Compiler::sOptimizer = OverrideFlag::kDefault; |
| 79 | Compiler::OverrideFlag Compiler::sInliner = OverrideFlag::kDefault; |
John Stiles | 8ef4d6c | 2021-03-05 16:01:45 -0500 | [diff] [blame] | 80 | |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 81 | using RefKind = VariableReference::RefKind; |
| 82 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 83 | class AutoSource { |
| 84 | public: |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 85 | AutoSource(Compiler* compiler, const char* source) |
John Stiles | a289ac2 | 2021-05-06 07:35:35 -0400 | [diff] [blame] | 86 | : fCompiler(compiler) { |
| 87 | SkASSERT(!fCompiler->fSource); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 88 | fCompiler->fSource = source; |
| 89 | } |
| 90 | |
John Stiles | a289ac2 | 2021-05-06 07:35:35 -0400 | [diff] [blame] | 91 | ~AutoSource() { |
| 92 | fCompiler->fSource = nullptr; |
| 93 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 94 | |
| 95 | Compiler* fCompiler; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 98 | class AutoProgramConfig { |
| 99 | public: |
| 100 | AutoProgramConfig(std::shared_ptr<Context>& context, ProgramConfig* config) |
| 101 | : fContext(context.get()) { |
| 102 | SkASSERT(!fContext->fConfig); |
| 103 | fContext->fConfig = config; |
| 104 | } |
| 105 | |
| 106 | ~AutoProgramConfig() { |
| 107 | fContext->fConfig = nullptr; |
| 108 | } |
| 109 | |
| 110 | Context* fContext; |
| 111 | }; |
| 112 | |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 113 | class AutoModifiersPool { |
| 114 | public: |
| 115 | AutoModifiersPool(std::shared_ptr<Context>& context, ModifiersPool* modifiersPool) |
| 116 | : fContext(context.get()) { |
| 117 | SkASSERT(!fContext->fModifiersPool); |
| 118 | fContext->fModifiersPool = modifiersPool; |
| 119 | } |
| 120 | |
| 121 | ~AutoModifiersPool() { |
| 122 | fContext->fModifiersPool = nullptr; |
| 123 | } |
| 124 | |
| 125 | Context* fContext; |
| 126 | }; |
| 127 | |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 128 | Compiler::Compiler(const ShaderCapsClass* caps) |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 129 | : fContext(std::make_shared<Context>(/*errors=*/*this, *caps)) |
John Stiles | a47b351 | 2021-05-04 16:15:00 -0400 | [diff] [blame] | 130 | , fInliner(fContext.get()) { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 131 | SkASSERT(caps); |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 132 | fRootModule.fSymbols = this->makeRootSymbolTable(); |
| 133 | fPrivateModule.fSymbols = this->makePrivateSymbolTable(fRootModule.fSymbols); |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 134 | fIRGenerator = std::make_unique<IRGenerator>(fContext.get()); |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 135 | } |
| 136 | |
| 137 | Compiler::~Compiler() {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 138 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 139 | #define TYPE(t) fContext->fTypes.f ## t .get() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 140 | |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 141 | std::shared_ptr<SymbolTable> Compiler::makeRootSymbolTable() { |
| 142 | auto rootSymbolTable = std::make_shared<SymbolTable>(this, /*builtin=*/true); |
| 143 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 144 | const SkSL::Symbol* rootTypes[] = { |
| 145 | TYPE(Void), |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 146 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 147 | TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4), |
| 148 | TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4), |
| 149 | TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 150 | TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4), |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 151 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 152 | TYPE(Float2x2), TYPE(Float3x3), TYPE(Float4x4), |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 153 | TYPE( Half2x2), TYPE( Half3x3), TYPE(Half4x4), |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 154 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 155 | TYPE(SquareMat), TYPE(SquareHMat), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 156 | |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 157 | TYPE(GenType), TYPE(GenHType), TYPE(GenIType), TYPE(GenBType), |
| 158 | TYPE(Vec), TYPE(HVec), TYPE(IVec), TYPE(BVec), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 159 | |
Brian Osman | 14d0096 | 2021-04-02 17:04:35 -0400 | [diff] [blame] | 160 | TYPE(ColorFilter), |
| 161 | TYPE(Shader), |
John Stiles | bb2ef92 | 2021-07-26 08:32:07 -0400 | [diff] [blame] | 162 | TYPE(Blender), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 163 | }; |
| 164 | |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 165 | for (const SkSL::Symbol* type : rootTypes) { |
| 166 | rootSymbolTable->addWithoutOwnership(type); |
| 167 | } |
| 168 | |
| 169 | return rootSymbolTable; |
| 170 | } |
| 171 | |
| 172 | std::shared_ptr<SymbolTable> Compiler::makePrivateSymbolTable(std::shared_ptr<SymbolTable> parent) { |
| 173 | auto privateSymbolTable = std::make_shared<SymbolTable>(parent, /*builtin=*/true); |
| 174 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 175 | const SkSL::Symbol* privateTypes[] = { |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 176 | TYPE( UInt), TYPE( UInt2), TYPE( UInt3), TYPE( UInt4), |
| 177 | TYPE( Short), TYPE( Short2), TYPE( Short3), TYPE( Short4), |
| 178 | TYPE(UShort), TYPE(UShort2), TYPE(UShort3), TYPE(UShort4), |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 179 | |
| 180 | TYPE(GenUType), TYPE(UVec), |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 181 | TYPE(SVec), TYPE(USVec), |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 182 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 183 | TYPE(Float2x3), TYPE(Float2x4), |
| 184 | TYPE(Float3x2), TYPE(Float3x4), |
| 185 | TYPE(Float4x2), TYPE(Float4x3), |
| 186 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 187 | TYPE(Half2x3), TYPE(Half2x4), |
| 188 | TYPE(Half3x2), TYPE(Half3x4), |
| 189 | TYPE(Half4x2), TYPE(Half4x3), |
| 190 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 191 | TYPE(Mat), TYPE(HMat), |
| 192 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 193 | TYPE(Sampler1D), TYPE(Sampler2D), TYPE(Sampler3D), |
| 194 | TYPE(SamplerExternalOES), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 195 | TYPE(Sampler2DRect), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 196 | |
| 197 | TYPE(ISampler2D), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 198 | TYPE(SubpassInput), TYPE(SubpassInputMS), |
| 199 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 200 | TYPE(Sampler), |
| 201 | TYPE(Texture2D), |
| 202 | }; |
| 203 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 204 | for (const SkSL::Symbol* type : privateTypes) { |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 205 | privateSymbolTable->addWithoutOwnership(type); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 208 | // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to |
| 209 | // treat it as builtin (ie, no need to clone it into the Program). |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 210 | privateSymbolTable->add(std::make_unique<Variable>(/*offset=*/-1, |
| 211 | fCoreModifiers.add(Modifiers{}), |
| 212 | "sk_Caps", |
| 213 | fContext->fTypes.fSkCaps.get(), |
| 214 | /*builtin=*/false, |
| 215 | Variable::Storage::kGlobal)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 216 | |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 217 | return privateSymbolTable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 218 | } |
| 219 | |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 220 | #undef TYPE |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 221 | |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 222 | const ParsedModule& Compiler::loadGPUModule() { |
| 223 | if (!fGPUModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 224 | fGPUModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(gpu), fPrivateModule); |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 225 | } |
| 226 | return fGPUModule; |
| 227 | } |
| 228 | |
| 229 | const ParsedModule& Compiler::loadFragmentModule() { |
| 230 | if (!fFragmentModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 231 | fFragmentModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(frag), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 232 | this->loadGPUModule()); |
| 233 | } |
| 234 | return fFragmentModule; |
| 235 | } |
| 236 | |
| 237 | const ParsedModule& Compiler::loadVertexModule() { |
| 238 | if (!fVertexModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 239 | fVertexModule = this->parseModule(ProgramKind::kVertex, MODULE_DATA(vert), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 240 | this->loadGPUModule()); |
| 241 | } |
| 242 | return fVertexModule; |
| 243 | } |
| 244 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 245 | const ParsedModule& Compiler::loadGeometryModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 246 | if (!fGeometryModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 247 | fGeometryModule = this->parseModule(ProgramKind::kGeometry, MODULE_DATA(geom), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 248 | this->loadGPUModule()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 249 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 250 | return fGeometryModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 253 | const ParsedModule& Compiler::loadPublicModule() { |
| 254 | if (!fPublicModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 255 | fPublicModule = this->parseModule(ProgramKind::kGeneric, MODULE_DATA(public), fRootModule); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 256 | } |
| 257 | return fPublicModule; |
| 258 | } |
| 259 | |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 260 | static void add_glsl_type_aliases(SkSL::SymbolTable* symbols, const SkSL::BuiltinTypes& types) { |
| 261 | // Add some aliases to the runtime effect modules so that it's friendlier, and more like GLSL |
| 262 | symbols->addAlias("vec2", types.fFloat2.get()); |
| 263 | symbols->addAlias("vec3", types.fFloat3.get()); |
| 264 | symbols->addAlias("vec4", types.fFloat4.get()); |
| 265 | |
| 266 | symbols->addAlias("ivec2", types.fInt2.get()); |
| 267 | symbols->addAlias("ivec3", types.fInt3.get()); |
| 268 | symbols->addAlias("ivec4", types.fInt4.get()); |
| 269 | |
| 270 | symbols->addAlias("bvec2", types.fBool2.get()); |
| 271 | symbols->addAlias("bvec3", types.fBool3.get()); |
| 272 | symbols->addAlias("bvec4", types.fBool4.get()); |
| 273 | |
| 274 | symbols->addAlias("mat2", types.fFloat2x2.get()); |
| 275 | symbols->addAlias("mat3", types.fFloat3x3.get()); |
| 276 | symbols->addAlias("mat4", types.fFloat4x4.get()); |
| 277 | } |
| 278 | |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 279 | const ParsedModule& Compiler::loadRuntimeColorFilterModule() { |
| 280 | if (!fRuntimeColorFilterModule.fSymbols) { |
| 281 | fRuntimeColorFilterModule = this->parseModule(ProgramKind::kRuntimeColorFilter, |
| 282 | MODULE_DATA(rt_colorfilter), |
| 283 | this->loadPublicModule()); |
| 284 | add_glsl_type_aliases(fRuntimeColorFilterModule.fSymbols.get(), fContext->fTypes); |
| 285 | } |
| 286 | return fRuntimeColorFilterModule; |
| 287 | } |
| 288 | |
| 289 | const ParsedModule& Compiler::loadRuntimeShaderModule() { |
| 290 | if (!fRuntimeShaderModule.fSymbols) { |
| 291 | fRuntimeShaderModule = this->parseModule( |
| 292 | ProgramKind::kRuntimeShader, MODULE_DATA(rt_shader), this->loadPublicModule()); |
| 293 | add_glsl_type_aliases(fRuntimeShaderModule.fSymbols.get(), fContext->fTypes); |
| 294 | } |
| 295 | return fRuntimeShaderModule; |
| 296 | } |
| 297 | |
John Stiles | 2d8b835 | 2021-06-16 11:33:13 -0400 | [diff] [blame] | 298 | const ParsedModule& Compiler::loadRuntimeBlenderModule() { |
| 299 | if (!fRuntimeBlenderModule.fSymbols) { |
| 300 | fRuntimeBlenderModule = this->parseModule( |
| 301 | ProgramKind::kRuntimeBlender, MODULE_DATA(rt_blend), this->loadPublicModule()); |
| 302 | add_glsl_type_aliases(fRuntimeBlenderModule.fSymbols.get(), fContext->fTypes); |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 303 | } |
John Stiles | 2d8b835 | 2021-06-16 11:33:13 -0400 | [diff] [blame] | 304 | return fRuntimeBlenderModule; |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 305 | } |
| 306 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 307 | const ParsedModule& Compiler::moduleForProgramKind(ProgramKind kind) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 308 | switch (kind) { |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 309 | case ProgramKind::kVertex: return this->loadVertexModule(); break; |
| 310 | case ProgramKind::kFragment: return this->loadFragmentModule(); break; |
| 311 | case ProgramKind::kGeometry: return this->loadGeometryModule(); break; |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 312 | case ProgramKind::kRuntimeColorFilter: return this->loadRuntimeColorFilterModule(); break; |
| 313 | case ProgramKind::kRuntimeShader: return this->loadRuntimeShaderModule(); break; |
John Stiles | 2d8b835 | 2021-06-16 11:33:13 -0400 | [diff] [blame] | 314 | case ProgramKind::kRuntimeBlender: return this->loadRuntimeBlenderModule(); break; |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 315 | case ProgramKind::kGeneric: return this->loadPublicModule(); break; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 316 | } |
| 317 | SkUNREACHABLE; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 318 | } |
| 319 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 320 | LoadedModule Compiler::loadModule(ProgramKind kind, |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 321 | ModuleData data, |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 322 | std::shared_ptr<SymbolTable> base, |
| 323 | bool dehydrate) { |
| 324 | if (dehydrate) { |
| 325 | // NOTE: This is a workaround. When dehydrating includes, skslc doesn't know which module |
| 326 | // it's preparing, nor what the correct base module is. We can't use 'Root', because many |
| 327 | // GPU intrinsics reference private types, like samplers or textures. Today, 'Private' does |
| 328 | // contain the union of all known types, so this is safe. If we ever have types that only |
| 329 | // exist in 'Public' (for example), this logic needs to be smarter (by choosing the correct |
| 330 | // base for the module we're compiling). |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 331 | base = fPrivateModule.fSymbols; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 332 | } |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 333 | SkASSERT(base); |
| 334 | |
John Stiles | a47b351 | 2021-05-04 16:15:00 -0400 | [diff] [blame] | 335 | // Put the core-module modifier pool into the context. |
| 336 | AutoModifiersPool autoPool(fContext, &fCoreModifiers); |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 337 | |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 338 | // Built-in modules always use default program settings. |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 339 | Program::Settings settings; |
| 340 | settings.fReplaceSettings = !dehydrate; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 341 | |
| 342 | #if defined(SKSL_STANDALONE) |
| 343 | SkASSERT(data.fPath); |
| 344 | std::ifstream in(data.fPath); |
John Stiles | d51c979 | 2021-03-18 11:40:14 -0400 | [diff] [blame] | 345 | String text{std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()}; |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 346 | if (in.rdstate()) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 347 | printf("error reading %s\n", data.fPath); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 348 | abort(); |
| 349 | } |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame^] | 350 | const String* source = fRootModule.fSymbols->takeOwnershipOfString(std::move(text)); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 351 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 352 | ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr}; |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 353 | std::vector<std::unique_ptr<ProgramElement>> elements; |
| 354 | std::vector<const ProgramElement*> sharedElements; |
| 355 | dsl::StartModule(this, kind, settings, baseModule); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 356 | AutoSource as(this, source->c_str()); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 357 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true, |
Ethan Nicholas | 6823b50 | 2021-06-15 11:42:07 -0400 | [diff] [blame] | 358 | *source); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 359 | SkASSERT(ir.fSharedElements.empty()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 360 | LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) }; |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 361 | dsl::End(); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 362 | if (this->fErrorCount) { |
| 363 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 364 | SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str()); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 365 | } |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 366 | #else |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 367 | ProgramConfig config; |
| 368 | config.fKind = kind; |
| 369 | config.fSettings = settings; |
| 370 | AutoProgramConfig autoConfig(fContext, &config); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 371 | SkASSERT(data.fData && (data.fSize != 0)); |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 372 | Rehydrator rehydrator(fContext.get(), base, data.fData, data.fSize); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 373 | LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() }; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 374 | #endif |
| 375 | |
| 376 | return module; |
| 377 | } |
| 378 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 379 | ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) { |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 380 | LoadedModule module = this->loadModule(kind, data, base.fSymbols, /*dehydrate=*/false); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 381 | this->optimize(module); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 382 | |
| 383 | // For modules that just declare (but don't define) intrinsic functions, there will be no new |
| 384 | // program elements. In that case, we can share our parent's intrinsic map: |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 385 | if (module.fElements.empty()) { |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 386 | return ParsedModule{module.fSymbols, base.fIntrinsics}; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get()); |
| 390 | |
| 391 | // Now, transfer all of the program elements to an intrinsic map. This maps certain types of |
| 392 | // global objects to the declaring ProgramElement. |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 393 | for (std::unique_ptr<ProgramElement>& element : module.fElements) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 394 | switch (element->kind()) { |
| 395 | case ProgramElement::Kind::kFunction: { |
| 396 | const FunctionDefinition& f = element->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 397 | SkASSERT(f.declaration().isBuiltin()); |
| 398 | intrinsics->insertOrDie(f.declaration().description(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 399 | break; |
| 400 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 401 | case ProgramElement::Kind::kFunctionPrototype: { |
| 402 | // These are already in the symbol table. |
| 403 | break; |
| 404 | } |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 405 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 406 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 407 | const Variable& var = global.declaration()->as<VarDeclaration>().var(); |
| 408 | SkASSERT(var.isBuiltin()); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 409 | intrinsics->insertOrDie(String(var.name()), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 410 | break; |
| 411 | } |
| 412 | case ProgramElement::Kind::kInterfaceBlock: { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 413 | const Variable& var = element->as<InterfaceBlock>().variable(); |
| 414 | SkASSERT(var.isBuiltin()); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 415 | intrinsics->insertOrDie(String(var.name()), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 416 | break; |
| 417 | } |
| 418 | default: |
| 419 | printf("Unsupported element: %s\n", element->description().c_str()); |
| 420 | SkASSERT(false); |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 425 | return ParsedModule{module.fSymbols, std::move(intrinsics)}; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 426 | } |
| 427 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 428 | std::unique_ptr<Program> Compiler::convertProgram( |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 429 | ProgramKind kind, |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 430 | String text, |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 431 | Program::Settings settings) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 432 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::convertProgram"); |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 433 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 434 | SkASSERT(!settings.fExternalFunctions || (kind == ProgramKind::kGeneric)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 435 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 436 | #if !SKSL_DSL_PARSER |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 437 | // Loading and optimizing our base module might reset the inliner, so do that first, |
| 438 | // *then* configure the inliner with the settings for this program. |
| 439 | const ParsedModule& baseModule = this->moduleForProgramKind(kind); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 440 | #endif |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 441 | |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 442 | // Honor our optimization-override flags. |
| 443 | switch (sOptimizer) { |
| 444 | case OverrideFlag::kDefault: |
| 445 | break; |
| 446 | case OverrideFlag::kOff: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 447 | settings.fOptimize = false; |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 448 | break; |
| 449 | case OverrideFlag::kOn: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 450 | settings.fOptimize = true; |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | |
| 454 | switch (sInliner) { |
| 455 | case OverrideFlag::kDefault: |
| 456 | break; |
| 457 | case OverrideFlag::kOff: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 458 | settings.fInlineThreshold = 0; |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 459 | break; |
| 460 | case OverrideFlag::kOn: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 461 | if (settings.fInlineThreshold == 0) { |
| 462 | settings.fInlineThreshold = kDefaultInlineThreshold; |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 463 | } |
| 464 | break; |
| 465 | } |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 466 | |
| 467 | // Disable optimization settings that depend on a parent setting which has been disabled. |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 468 | settings.fInlineThreshold *= (int)settings.fOptimize; |
| 469 | settings.fRemoveDeadFunctions &= settings.fOptimize; |
| 470 | settings.fRemoveDeadVariables &= settings.fOptimize; |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 471 | |
John Stiles | addccaf | 2021-08-02 19:03:30 -0400 | [diff] [blame] | 472 | // Runtime effects always allow narrowing conversions. |
| 473 | if (ProgramConfig::IsRuntimeEffect(kind)) { |
| 474 | settings.fAllowNarrowingConversions = true; |
| 475 | } |
| 476 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 477 | fErrorText = ""; |
| 478 | fErrorCount = 0; |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 479 | fInliner.reset(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 480 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 481 | #if SKSL_DSL_PARSER |
| 482 | settings.fDSLMangling = false; |
| 483 | return DSLParser(this, settings, kind, text).program(); |
| 484 | #else |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 485 | auto textPtr = std::make_unique<String>(std::move(text)); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 486 | AutoSource as(this, textPtr->c_str()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 487 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 488 | dsl::Start(this, kind, settings); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 489 | dsl::SetErrorHandler(this); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 490 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false, |
Ethan Nicholas | 6823b50 | 2021-06-15 11:42:07 -0400 | [diff] [blame] | 491 | *textPtr); |
Ethan Nicholas | 4f3e6a2 | 2021-06-15 09:17:05 -0400 | [diff] [blame] | 492 | // Ideally, we would just use dsl::ReleaseProgram and not have to do any manual mucking about |
| 493 | // with the memory pool, but we've got some impedance mismatches to solve first |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 494 | Pool* memoryPool = dsl::DSLWriter::MemoryPool().get(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 495 | auto program = std::make_unique<Program>(std::move(textPtr), |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 496 | std::move(dsl::DSLWriter::GetProgramConfig()), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 497 | fContext, |
| 498 | std::move(ir.fElements), |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 499 | std::move(ir.fSharedElements), |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 500 | std::move(dsl::DSLWriter::GetModifiersPool()), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 501 | std::move(ir.fSymbolTable), |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 502 | std::move(dsl::DSLWriter::MemoryPool()), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 503 | ir.fInputs); |
| 504 | bool success = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 505 | if (fErrorCount) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 506 | // Do not return programs that failed to compile. |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 507 | } else if (!this->optimize(*program)) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 508 | // Do not return programs that failed to optimize. |
| 509 | } else { |
| 510 | // We have a successful program! |
| 511 | success = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 512 | } |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 513 | dsl::End(); |
| 514 | if (memoryPool) { |
| 515 | memoryPool->detachFromThread(); |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 516 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 517 | return success ? std::move(program) : nullptr; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 518 | #endif // SKSL_DSL_PARSER |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 519 | } |
| 520 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 521 | void Compiler::verifyStaticTests(const Program& program) { |
| 522 | class StaticTestVerifier : public ProgramVisitor { |
| 523 | public: |
| 524 | StaticTestVerifier(ErrorReporter* r) : fReporter(r) {} |
| 525 | |
| 526 | using ProgramVisitor::visitProgramElement; |
| 527 | |
| 528 | bool visitStatement(const Statement& stmt) override { |
| 529 | switch (stmt.kind()) { |
| 530 | case Statement::Kind::kIf: |
| 531 | if (stmt.as<IfStatement>().isStatic()) { |
| 532 | fReporter->error(stmt.fOffset, "static if has non-static test"); |
| 533 | } |
| 534 | break; |
| 535 | |
| 536 | case Statement::Kind::kSwitch: |
| 537 | if (stmt.as<SwitchStatement>().isStatic()) { |
| 538 | fReporter->error(stmt.fOffset, "static switch has non-static test"); |
| 539 | } |
| 540 | break; |
| 541 | |
| 542 | default: |
| 543 | break; |
| 544 | } |
| 545 | return INHERITED::visitStatement(stmt); |
| 546 | } |
| 547 | |
John Stiles | 59e3456 | 2021-02-12 16:56:39 -0500 | [diff] [blame] | 548 | bool visitExpression(const Expression&) override { |
| 549 | // We aren't looking for anything inside an Expression, so skip them entirely. |
| 550 | return false; |
| 551 | } |
| 552 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 553 | private: |
| 554 | using INHERITED = ProgramVisitor; |
| 555 | ErrorReporter* fReporter; |
| 556 | }; |
| 557 | |
| 558 | // If invalid static tests are permitted, we don't need to check anything. |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 559 | if (fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | |
| 563 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
| 564 | StaticTestVerifier visitor{this}; |
| 565 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
| 566 | if (element->is<FunctionDefinition>()) { |
| 567 | visitor.visitProgramElement(*element); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 572 | bool Compiler::optimize(LoadedModule& module) { |
| 573 | SkASSERT(!fErrorCount); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 574 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 575 | // Create a temporary program configuration with default settings. |
| 576 | ProgramConfig config; |
| 577 | config.fKind = module.fKind; |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 578 | AutoProgramConfig autoConfig(fContext, &config); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 579 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 580 | // Reset the Inliner. |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 581 | fInliner.reset(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 582 | |
| 583 | std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 584 | |
| 585 | while (fErrorCount == 0) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 586 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame] | 587 | if (!fInliner.analyze(module.fElements, module.fSymbols, usage.get())) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | return fErrorCount == 0; |
| 592 | } |
| 593 | |
John Stiles | 0bfeae6 | 2021-03-11 09:09:42 -0500 | [diff] [blame] | 594 | bool Compiler::removeDeadFunctions(Program& program, ProgramUsage* usage) { |
| 595 | bool madeChanges = false; |
| 596 | |
| 597 | if (program.fConfig->fSettings.fRemoveDeadFunctions) { |
| 598 | auto isDeadFunction = [&](const ProgramElement* element) { |
| 599 | if (!element->is<FunctionDefinition>()) { |
| 600 | return false; |
| 601 | } |
| 602 | const FunctionDefinition& fn = element->as<FunctionDefinition>(); |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 603 | if (fn.declaration().isMain() || usage->get(fn.declaration()) > 0) { |
John Stiles | 0bfeae6 | 2021-03-11 09:09:42 -0500 | [diff] [blame] | 604 | return false; |
| 605 | } |
| 606 | usage->remove(*element); |
| 607 | madeChanges = true; |
| 608 | return true; |
| 609 | }; |
| 610 | |
| 611 | program.fElements.erase(std::remove_if(program.fElements.begin(), |
| 612 | program.fElements.end(), |
| 613 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 614 | return isDeadFunction(element.get()); |
| 615 | }), |
| 616 | program.fElements.end()); |
| 617 | program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(), |
| 618 | program.fSharedElements.end(), |
| 619 | isDeadFunction), |
| 620 | program.fSharedElements.end()); |
| 621 | } |
| 622 | return madeChanges; |
| 623 | } |
| 624 | |
| 625 | bool Compiler::removeDeadGlobalVariables(Program& program, ProgramUsage* usage) { |
| 626 | bool madeChanges = false; |
| 627 | |
| 628 | if (program.fConfig->fSettings.fRemoveDeadVariables) { |
| 629 | auto isDeadVariable = [&](const ProgramElement* element) { |
| 630 | if (!element->is<GlobalVarDeclaration>()) { |
| 631 | return false; |
| 632 | } |
| 633 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 634 | const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>(); |
| 635 | if (!usage->isDead(varDecl.var())) { |
| 636 | return false; |
| 637 | } |
| 638 | madeChanges = true; |
| 639 | return true; |
| 640 | }; |
| 641 | |
| 642 | program.fElements.erase(std::remove_if(program.fElements.begin(), |
| 643 | program.fElements.end(), |
| 644 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 645 | return isDeadVariable(element.get()); |
| 646 | }), |
| 647 | program.fElements.end()); |
| 648 | program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(), |
| 649 | program.fSharedElements.end(), |
| 650 | isDeadVariable), |
| 651 | program.fSharedElements.end()); |
| 652 | } |
| 653 | return madeChanges; |
| 654 | } |
| 655 | |
John Stiles | 2654187 | 2021-03-16 12:19:54 -0400 | [diff] [blame] | 656 | bool Compiler::removeDeadLocalVariables(Program& program, ProgramUsage* usage) { |
| 657 | class DeadLocalVariableEliminator : public ProgramWriter { |
| 658 | public: |
| 659 | DeadLocalVariableEliminator(const Context& context, ProgramUsage* usage) |
| 660 | : fContext(context) |
| 661 | , fUsage(usage) {} |
| 662 | |
| 663 | using ProgramWriter::visitProgramElement; |
| 664 | |
| 665 | bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override { |
| 666 | // We don't need to look inside expressions at all. |
| 667 | return false; |
| 668 | } |
| 669 | |
| 670 | bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override { |
| 671 | if (stmt->is<VarDeclaration>()) { |
| 672 | VarDeclaration& varDecl = stmt->as<VarDeclaration>(); |
| 673 | const Variable* var = &varDecl.var(); |
| 674 | ProgramUsage::VariableCounts* counts = fUsage->fVariableCounts.find(var); |
| 675 | SkASSERT(counts); |
| 676 | SkASSERT(counts->fDeclared); |
| 677 | if (CanEliminate(var, *counts)) { |
| 678 | if (var->initialValue()) { |
| 679 | // The variable has an initial-value expression, which might have side |
| 680 | // effects. ExpressionStatement::Make will preserve side effects, but |
| 681 | // replaces pure expressions with Nop. |
| 682 | fUsage->remove(stmt.get()); |
| 683 | stmt = ExpressionStatement::Make(fContext, std::move(varDecl.value())); |
| 684 | fUsage->add(stmt.get()); |
| 685 | } else { |
| 686 | // The variable has no initial-value and can be cleanly eliminated. |
| 687 | fUsage->remove(stmt.get()); |
| 688 | stmt = std::make_unique<Nop>(); |
| 689 | } |
| 690 | fMadeChanges = true; |
| 691 | } |
| 692 | return false; |
| 693 | } |
| 694 | return INHERITED::visitStatementPtr(stmt); |
| 695 | } |
| 696 | |
| 697 | static bool CanEliminate(const Variable* var, const ProgramUsage::VariableCounts& counts) { |
| 698 | if (!counts.fDeclared || counts.fRead || var->storage() != VariableStorage::kLocal) { |
| 699 | return false; |
| 700 | } |
| 701 | if (var->initialValue()) { |
| 702 | SkASSERT(counts.fWrite >= 1); |
| 703 | return counts.fWrite == 1; |
| 704 | } else { |
| 705 | return counts.fWrite == 0; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | bool fMadeChanges = false; |
| 710 | const Context& fContext; |
| 711 | ProgramUsage* fUsage; |
| 712 | |
| 713 | using INHERITED = ProgramWriter; |
| 714 | }; |
| 715 | |
| 716 | DeadLocalVariableEliminator visitor{*fContext, usage}; |
| 717 | |
| 718 | if (program.fConfig->fSettings.fRemoveDeadVariables) { |
| 719 | for (auto& [var, counts] : usage->fVariableCounts) { |
| 720 | if (DeadLocalVariableEliminator::CanEliminate(var, counts)) { |
| 721 | // This program contains at least one dead local variable. |
| 722 | // Scan the program for any dead local variables and eliminate them all. |
| 723 | for (std::unique_ptr<ProgramElement>& pe : program.ownedElements()) { |
| 724 | if (pe->is<FunctionDefinition>()) { |
| 725 | visitor.visitProgramElement(*pe); |
| 726 | } |
| 727 | } |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | return visitor.fMadeChanges; |
| 734 | } |
| 735 | |
John Stiles | 25be58e | 2021-05-20 14:38:40 -0400 | [diff] [blame] | 736 | void Compiler::removeUnreachableCode(Program& program, ProgramUsage* usage) { |
| 737 | class UnreachableCodeEliminator : public ProgramWriter { |
| 738 | public: |
| 739 | UnreachableCodeEliminator(const Context& context, ProgramUsage* usage) |
| 740 | : fContext(context) |
| 741 | , fUsage(usage) { |
| 742 | fFoundFunctionExit.push(false); |
| 743 | fFoundLoopExit.push(false); |
| 744 | } |
| 745 | |
| 746 | using ProgramWriter::visitProgramElement; |
| 747 | |
| 748 | bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override { |
| 749 | // We don't need to look inside expressions at all. |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override { |
| 754 | if (fFoundFunctionExit.top() || fFoundLoopExit.top()) { |
| 755 | // If we already found an exit in this section, anything beyond it is dead code. |
| 756 | if (!stmt->is<Nop>()) { |
| 757 | // Eliminate the dead statement by substituting a Nop. |
| 758 | fUsage->remove(stmt.get()); |
| 759 | stmt = std::make_unique<Nop>(); |
| 760 | } |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | switch (stmt->kind()) { |
| 765 | case Statement::Kind::kReturn: |
| 766 | case Statement::Kind::kDiscard: |
| 767 | // We found a function exit on this path. |
| 768 | fFoundFunctionExit.top() = true; |
| 769 | break; |
| 770 | |
| 771 | case Statement::Kind::kBreak: |
| 772 | case Statement::Kind::kContinue: |
| 773 | // We found a loop exit on this path. Note that we skip over switch statements |
| 774 | // completely when eliminating code, so any `break` statement would be breaking |
| 775 | // out of a loop, not out of a switch. |
| 776 | fFoundLoopExit.top() = true; |
| 777 | break; |
| 778 | |
| 779 | case Statement::Kind::kExpression: |
| 780 | case Statement::Kind::kInlineMarker: |
| 781 | case Statement::Kind::kNop: |
| 782 | case Statement::Kind::kVarDeclaration: |
| 783 | // These statements don't affect control flow. |
| 784 | break; |
| 785 | |
| 786 | case Statement::Kind::kBlock: |
| 787 | // Blocks are on the straight-line path and don't affect control flow. |
| 788 | return INHERITED::visitStatementPtr(stmt); |
| 789 | |
| 790 | case Statement::Kind::kDo: { |
| 791 | // Function-exits are allowed to propagate outside of a do-loop, because it |
| 792 | // always executes its body at least once. |
| 793 | fFoundLoopExit.push(false); |
| 794 | bool result = INHERITED::visitStatementPtr(stmt); |
| 795 | fFoundLoopExit.pop(); |
| 796 | return result; |
| 797 | } |
| 798 | case Statement::Kind::kFor: { |
| 799 | // Function-exits are not allowed to propagate out, because a for-loop or while- |
| 800 | // loop could potentially run zero times. |
| 801 | fFoundFunctionExit.push(false); |
| 802 | fFoundLoopExit.push(false); |
| 803 | bool result = INHERITED::visitStatementPtr(stmt); |
| 804 | fFoundLoopExit.pop(); |
| 805 | fFoundFunctionExit.pop(); |
| 806 | return result; |
| 807 | } |
| 808 | case Statement::Kind::kIf: { |
| 809 | // This statement is conditional and encloses two inner sections of code. |
| 810 | // If both sides contain a function-exit or loop-exit, that exit is allowed to |
| 811 | // propagate out. |
| 812 | IfStatement& ifStmt = stmt->as<IfStatement>(); |
| 813 | |
| 814 | fFoundFunctionExit.push(false); |
| 815 | fFoundLoopExit.push(false); |
| 816 | bool result = (ifStmt.ifTrue() && this->visitStatementPtr(ifStmt.ifTrue())); |
| 817 | bool foundFunctionExitOnTrue = fFoundFunctionExit.top(); |
| 818 | bool foundLoopExitOnTrue = fFoundLoopExit.top(); |
| 819 | fFoundFunctionExit.pop(); |
| 820 | fFoundLoopExit.pop(); |
| 821 | |
| 822 | fFoundFunctionExit.push(false); |
| 823 | fFoundLoopExit.push(false); |
| 824 | result |= (ifStmt.ifFalse() && this->visitStatementPtr(ifStmt.ifFalse())); |
| 825 | bool foundFunctionExitOnFalse = fFoundFunctionExit.top(); |
| 826 | bool foundLoopExitOnFalse = fFoundLoopExit.top(); |
| 827 | fFoundFunctionExit.pop(); |
| 828 | fFoundLoopExit.pop(); |
| 829 | |
| 830 | fFoundFunctionExit.top() |= foundFunctionExitOnTrue && foundFunctionExitOnFalse; |
| 831 | fFoundLoopExit.top() |= foundLoopExitOnTrue && foundLoopExitOnFalse; |
| 832 | return result; |
| 833 | } |
| 834 | case Statement::Kind::kSwitch: |
| 835 | case Statement::Kind::kSwitchCase: |
| 836 | // We skip past switch statements entirely when scanning for dead code. Their |
| 837 | // control flow is quite complex and we already do a good job of flattening out |
| 838 | // switches on constant values. |
| 839 | break; |
| 840 | } |
| 841 | |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | const Context& fContext; |
| 846 | ProgramUsage* fUsage; |
| 847 | std::stack<bool> fFoundFunctionExit; |
| 848 | std::stack<bool> fFoundLoopExit; |
| 849 | |
| 850 | using INHERITED = ProgramWriter; |
| 851 | }; |
| 852 | |
| 853 | for (std::unique_ptr<ProgramElement>& pe : program.ownedElements()) { |
| 854 | if (pe->is<FunctionDefinition>()) { |
| 855 | UnreachableCodeEliminator visitor{*fContext, usage}; |
| 856 | visitor.visitProgramElement(*pe); |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 861 | bool Compiler::optimize(Program& program) { |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 862 | // The optimizer only needs to run when it is enabled. |
| 863 | if (!program.fConfig->fSettings.fOptimize) { |
| 864 | return true; |
| 865 | } |
| 866 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 867 | SkASSERT(!fErrorCount); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 868 | ProgramUsage* usage = program.fUsage.get(); |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 869 | |
John Stiles | b666458 | 2021-03-19 09:46:00 -0400 | [diff] [blame] | 870 | if (fErrorCount == 0) { |
John Stiles | 87fc657 | 2021-04-01 14:56:34 +0000 | [diff] [blame] | 871 | // Run the inliner only once; it is expensive! Multiple passes can occasionally shake out |
| 872 | // more wins, but it's diminishing returns. |
| 873 | fInliner.analyze(program.ownedElements(), program.fSymbols, usage); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 874 | |
John Stiles | b666458 | 2021-03-19 09:46:00 -0400 | [diff] [blame] | 875 | while (this->removeDeadFunctions(program, usage)) { |
| 876 | // Removing dead functions may cause more functions to become unreferenced. Try again. |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 877 | } |
John Stiles | b666458 | 2021-03-19 09:46:00 -0400 | [diff] [blame] | 878 | while (this->removeDeadLocalVariables(program, usage)) { |
| 879 | // Removing dead variables may cause more variables to become unreferenced. Try again. |
| 880 | } |
John Stiles | 25be58e | 2021-05-20 14:38:40 -0400 | [diff] [blame] | 881 | // Unreachable code can confuse some drivers, so it's worth removing. (skia:12012) |
| 882 | this->removeUnreachableCode(program, usage); |
| 883 | |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 884 | this->removeDeadGlobalVariables(program, usage); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 885 | } |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 886 | |
| 887 | if (fErrorCount == 0) { |
| 888 | this->verifyStaticTests(program); |
| 889 | } |
| 890 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 891 | return fErrorCount == 0; |
| 892 | } |
| 893 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 894 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 895 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 896 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 897 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toSPIRV"); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 898 | AutoSource as(this, program.fSource->c_str()); |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 899 | ProgramSettings settings; |
| 900 | settings.fDSLUseMemoryPool = false; |
| 901 | dsl::Start(this, program.fConfig->fKind, settings); |
| 902 | dsl::DSLWriter::IRGenerator().fSymbolTable = program.fSymbols; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 903 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 904 | StringStream buffer; |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 905 | SPIRVCodeGenerator cg(fContext.get(), &program, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 906 | bool result = cg.generateCode(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 907 | if (result && program.fConfig->fSettings.fValidateSPIRV) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 908 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 909 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 910 | SkASSERT(0 == data.size() % 4); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 911 | String errors; |
| 912 | auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&, |
| 913 | const char* m) { |
| 914 | errors.appendf("SPIR-V validation error: %s\n", m); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 915 | }; |
| 916 | tools.SetMessageConsumer(dumpmsg); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 917 | |
| 918 | // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message |
| 919 | // explaining the error. In standalone mode (skslc), we will send the message, plus the |
| 920 | // entire disassembled SPIR-V (for easier context & debugging) as *our* error message. |
| 921 | result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4); |
| 922 | |
| 923 | if (!result) { |
| 924 | #if defined(SKSL_STANDALONE) |
| 925 | // Convert the string-stream to a SPIR-V disassembly. |
| 926 | std::string disassembly; |
| 927 | if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) { |
| 928 | errors.append(disassembly); |
| 929 | } |
| 930 | this->error(-1, errors); |
| 931 | #else |
| 932 | SkDEBUGFAILF("%s", errors.c_str()); |
| 933 | #endif |
| 934 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 935 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 936 | } |
| 937 | #else |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 938 | SPIRVCodeGenerator cg(fContext.get(), &program, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 939 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 940 | #endif |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 941 | dsl::End(); |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 942 | return result; |
| 943 | } |
| 944 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 945 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 946 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 947 | bool result = this->toSPIRV(program, buffer); |
| 948 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 949 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 950 | } |
| 951 | return result; |
| 952 | } |
| 953 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 954 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 955 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toGLSL"); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 956 | AutoSource as(this, program.fSource->c_str()); |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 957 | GLSLCodeGenerator cg(fContext.get(), &program, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 958 | bool result = cg.generateCode(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 959 | return result; |
| 960 | } |
| 961 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 962 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 963 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 964 | bool result = this->toGLSL(program, buffer); |
| 965 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 966 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 967 | } |
| 968 | return result; |
| 969 | } |
| 970 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 971 | bool Compiler::toHLSL(Program& program, String* out) { |
| 972 | String spirv; |
| 973 | if (!this->toSPIRV(program, &spirv)) { |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | return SPIRVtoHLSL(spirv, out); |
| 978 | } |
| 979 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 980 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 981 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toMetal"); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 982 | AutoSource as(this, program.fSource->c_str()); |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 983 | MetalCodeGenerator cg(fContext.get(), &program, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 984 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 985 | return result; |
| 986 | } |
| 987 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 988 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 989 | StringStream buffer; |
| 990 | bool result = this->toMetal(program, buffer); |
| 991 | if (result) { |
| 992 | *out = buffer.str(); |
| 993 | } |
| 994 | return result; |
| 995 | } |
| 996 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 997 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 998 | |
Ethan Nicholas | a40ddcd | 2021-08-06 09:17:18 -0400 | [diff] [blame] | 999 | void Compiler::handleError(const char* msg, dsl::PositionInfo pos) { |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 1000 | if (strstr(msg, POISON_TAG)) { |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1001 | // don't report errors on poison values |
| 1002 | return; |
| 1003 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1004 | fErrorCount++; |
Ethan Nicholas | a40ddcd | 2021-08-06 09:17:18 -0400 | [diff] [blame] | 1005 | fErrorText += "error: " + (pos.line() >= 1 ? to_string(pos.line()) + ": " : "") + msg + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1008 | String Compiler::errorText(bool showCount) { |
| 1009 | if (showCount) { |
| 1010 | this->writeErrorCount(); |
| 1011 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1012 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1013 | String result = fErrorText; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1014 | fErrorText = ""; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1015 | return result; |
| 1016 | } |
| 1017 | |
| 1018 | void Compiler::writeErrorCount() { |
| 1019 | if (fErrorCount) { |
| 1020 | fErrorText += to_string(fErrorCount) + " error"; |
| 1021 | if (fErrorCount > 1) { |
| 1022 | fErrorText += "s"; |
| 1023 | } |
| 1024 | fErrorText += "\n"; |
| 1025 | } |
| 1026 | } |
| 1027 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1028 | } // namespace SkSL |