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