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