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