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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLCPPCodeGenerator.h" |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame^] | 17 | #include "src/sksl/SkSLConstantFolder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/sksl/SkSLGLSLCodeGenerator.h" |
| 19 | #include "src/sksl/SkSLHCodeGenerator.h" |
| 20 | #include "src/sksl/SkSLIRGenerator.h" |
| 21 | #include "src/sksl/SkSLMetalCodeGenerator.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 22 | #include "src/sksl/SkSLOperators.h" |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 23 | #include "src/sksl/SkSLProgramSettings.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 24 | #include "src/sksl/SkSLRehydrator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/sksl/SkSLSPIRVCodeGenerator.h" |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 26 | #include "src/sksl/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 | // Set these flags to `false` to disable optimization passes unilaterally. |
| 76 | // These flags allow tools like Viewer or Nanobench to override the compiler's ProgramSettings. |
| 77 | bool gSkSLOptimizer = true; |
| 78 | bool gSkSLInliner = true; |
John Stiles | 8ef4d6c | 2021-03-05 16:01:45 -0500 | [diff] [blame] | 79 | |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 80 | using RefKind = VariableReference::RefKind; |
| 81 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 82 | class AutoSource { |
| 83 | public: |
| 84 | AutoSource(Compiler* compiler, const String* source) |
| 85 | : fCompiler(compiler), fOldSource(fCompiler->fSource) { |
| 86 | fCompiler->fSource = source; |
| 87 | } |
| 88 | |
| 89 | ~AutoSource() { fCompiler->fSource = fOldSource; } |
| 90 | |
| 91 | Compiler* fCompiler; |
| 92 | const String* fOldSource; |
| 93 | }; |
| 94 | |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 95 | class AutoProgramConfig { |
| 96 | public: |
| 97 | AutoProgramConfig(std::shared_ptr<Context>& context, ProgramConfig* config) |
| 98 | : fContext(context.get()) { |
| 99 | SkASSERT(!fContext->fConfig); |
| 100 | fContext->fConfig = config; |
| 101 | } |
| 102 | |
| 103 | ~AutoProgramConfig() { |
| 104 | fContext->fConfig = nullptr; |
| 105 | } |
| 106 | |
| 107 | Context* fContext; |
| 108 | }; |
| 109 | |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 110 | Compiler::Compiler(const ShaderCapsClass* caps) |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 111 | : fContext(std::make_shared<Context>(/*errors=*/*this, *caps)) |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 112 | , fInliner(fContext.get()) |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 113 | , fErrorCount(0) { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 114 | SkASSERT(caps); |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 115 | fRootSymbolTable = std::make_shared<SymbolTable>(this, /*builtin=*/true); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 116 | fPrivateSymbolTable = std::make_shared<SymbolTable>(fRootSymbolTable, /*builtin=*/true); |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 117 | fIRGenerator = std::make_unique<IRGenerator>(fContext.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 118 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 119 | #define TYPE(t) fContext->fTypes.f ## t .get() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 120 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 121 | const SkSL::Symbol* rootTypes[] = { |
| 122 | TYPE(Void), |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 123 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 124 | TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4), |
| 125 | TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4), |
| 126 | TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 127 | TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4), |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 128 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 129 | TYPE(Float2x2), TYPE(Float3x3), TYPE(Float4x4), |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 130 | TYPE( Half2x2), TYPE( Half3x3), TYPE(Half4x4), |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 131 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 132 | TYPE(SquareMat), TYPE(SquareHMat), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 133 | |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 134 | TYPE(GenType), TYPE(GenHType), TYPE(GenIType), TYPE(GenBType), |
| 135 | TYPE(Vec), TYPE(HVec), TYPE(IVec), TYPE(BVec), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 136 | |
| 137 | TYPE(FragmentProcessor), |
| 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), |
| 169 | }; |
| 170 | |
| 171 | for (const SkSL::Symbol* type : rootTypes) { |
| 172 | fRootSymbolTable->addWithoutOwnership(type); |
| 173 | } |
| 174 | for (const SkSL::Symbol* type : privateTypes) { |
| 175 | fPrivateSymbolTable->addWithoutOwnership(type); |
| 176 | } |
| 177 | |
| 178 | #undef TYPE |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 179 | |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 180 | // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to |
| 181 | // 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] | 182 | fPrivateSymbolTable->add( |
| 183 | std::make_unique<Variable>(/*offset=*/-1, |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 184 | fIRGenerator->fModifiers->addToPool(Modifiers()), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 185 | "sk_Caps", |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 186 | fContext->fTypes.fSkCaps.get(), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 187 | /*builtin=*/false, |
| 188 | Variable::Storage::kGlobal)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 189 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 190 | fRootModule = {fRootSymbolTable, /*fIntrinsics=*/nullptr}; |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 191 | fPrivateModule = {fPrivateSymbolTable, /*fIntrinsics=*/nullptr}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
John Stiles | dd13dba | 2020-10-29 10:45:34 -0400 | [diff] [blame] | 194 | Compiler::~Compiler() {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 195 | |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 196 | const ParsedModule& Compiler::loadGPUModule() { |
| 197 | if (!fGPUModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 198 | fGPUModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(gpu), fPrivateModule); |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 199 | } |
| 200 | return fGPUModule; |
| 201 | } |
| 202 | |
| 203 | const ParsedModule& Compiler::loadFragmentModule() { |
| 204 | if (!fFragmentModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 205 | fFragmentModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(frag), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 206 | this->loadGPUModule()); |
| 207 | } |
| 208 | return fFragmentModule; |
| 209 | } |
| 210 | |
| 211 | const ParsedModule& Compiler::loadVertexModule() { |
| 212 | if (!fVertexModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 213 | fVertexModule = this->parseModule(ProgramKind::kVertex, MODULE_DATA(vert), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 214 | this->loadGPUModule()); |
| 215 | } |
| 216 | return fVertexModule; |
| 217 | } |
| 218 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 219 | const ParsedModule& Compiler::loadGeometryModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 220 | if (!fGeometryModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 221 | fGeometryModule = this->parseModule(ProgramKind::kGeometry, MODULE_DATA(geom), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 222 | this->loadGPUModule()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 223 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 224 | return fGeometryModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 227 | const ParsedModule& Compiler::loadFPModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 228 | if (!fFPModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 229 | fFPModule = this->parseModule(ProgramKind::kFragmentProcessor, MODULE_DATA(fp), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 230 | this->loadGPUModule()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 231 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 232 | return fFPModule; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 235 | const ParsedModule& Compiler::loadPublicModule() { |
| 236 | if (!fPublicModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 237 | fPublicModule = this->parseModule(ProgramKind::kGeneric, MODULE_DATA(public), fRootModule); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 238 | } |
| 239 | return fPublicModule; |
| 240 | } |
| 241 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 242 | const ParsedModule& Compiler::loadRuntimeEffectModule() { |
| 243 | if (!fRuntimeEffectModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 244 | fRuntimeEffectModule = this->parseModule(ProgramKind::kRuntimeEffect, MODULE_DATA(runtime), |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 245 | this->loadPublicModule()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 246 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 247 | // 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] | 248 | fRuntimeEffectModule.fSymbols->addAlias("shader", fContext->fTypes.fFragmentProcessor.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 249 | |
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 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 254 | fRuntimeEffectModule.fSymbols->addAlias("bvec2", fContext->fTypes.fBool2.get()); |
| 255 | fRuntimeEffectModule.fSymbols->addAlias("bvec3", fContext->fTypes.fBool3.get()); |
| 256 | fRuntimeEffectModule.fSymbols->addAlias("bvec4", fContext->fTypes.fBool4.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 257 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 258 | fRuntimeEffectModule.fSymbols->addAlias("mat2", fContext->fTypes.fFloat2x2.get()); |
| 259 | fRuntimeEffectModule.fSymbols->addAlias("mat3", fContext->fTypes.fFloat3x3.get()); |
| 260 | fRuntimeEffectModule.fSymbols->addAlias("mat4", fContext->fTypes.fFloat4x4.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 261 | } |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 262 | return fRuntimeEffectModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 263 | } |
| 264 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 265 | const ParsedModule& Compiler::moduleForProgramKind(ProgramKind kind) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 266 | switch (kind) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 267 | case ProgramKind::kVertex: return this->loadVertexModule(); break; |
| 268 | case ProgramKind::kFragment: return this->loadFragmentModule(); break; |
| 269 | case ProgramKind::kGeometry: return this->loadGeometryModule(); break; |
| 270 | case ProgramKind::kFragmentProcessor: return this->loadFPModule(); break; |
| 271 | case ProgramKind::kRuntimeEffect: return this->loadRuntimeEffectModule(); break; |
| 272 | case ProgramKind::kGeneric: return this->loadPublicModule(); break; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 273 | } |
| 274 | SkUNREACHABLE; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 275 | } |
| 276 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 277 | LoadedModule Compiler::loadModule(ProgramKind kind, |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 278 | ModuleData data, |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 279 | std::shared_ptr<SymbolTable> base, |
| 280 | bool dehydrate) { |
| 281 | if (dehydrate) { |
| 282 | // NOTE: This is a workaround. When dehydrating includes, skslc doesn't know which module |
| 283 | // it's preparing, nor what the correct base module is. We can't use 'Root', because many |
| 284 | // GPU intrinsics reference private types, like samplers or textures. Today, 'Private' does |
| 285 | // contain the union of all known types, so this is safe. If we ever have types that only |
| 286 | // exist in 'Public' (for example), this logic needs to be smarter (by choosing the correct |
| 287 | // base for the module we're compiling). |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 288 | base = fPrivateSymbolTable; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 289 | } |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 290 | SkASSERT(base); |
| 291 | |
| 292 | // Built-in modules always use default program settings. |
| 293 | ProgramConfig config; |
| 294 | config.fKind = kind; |
| 295 | config.fSettings.fReplaceSettings = !dehydrate; |
| 296 | AutoProgramConfig autoConfig(fContext, &config); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 297 | |
| 298 | #if defined(SKSL_STANDALONE) |
| 299 | SkASSERT(data.fPath); |
| 300 | std::ifstream in(data.fPath); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 301 | std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in), |
| 302 | std::istreambuf_iterator<char>()); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 303 | if (in.rdstate()) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 304 | printf("error reading %s\n", data.fPath); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 305 | abort(); |
| 306 | } |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 307 | const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text)); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 308 | AutoSource as(this, source); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 309 | |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 310 | SkASSERT(fIRGenerator->fCanInline); |
| 311 | fIRGenerator->fCanInline = false; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 312 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 313 | ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr}; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 314 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true, |
| 315 | source->c_str(), source->length(), |
| 316 | /*externalFunctions=*/nullptr); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 317 | SkASSERT(ir.fSharedElements.empty()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 318 | LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) }; |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 319 | fIRGenerator->fCanInline = true; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 320 | if (this->fErrorCount) { |
| 321 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 322 | SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str()); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 323 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 324 | fModifiers.push_back(std::move(ir.fModifiers)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 325 | #else |
| 326 | SkASSERT(data.fData && (data.fSize != 0)); |
| 327 | Rehydrator rehydrator(fContext.get(), fIRGenerator->fModifiers.get(), base, this, |
| 328 | data.fData, data.fSize); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 329 | LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() }; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 330 | fModifiers.push_back(fIRGenerator->releaseModifiers()); |
| 331 | #endif |
| 332 | |
| 333 | return module; |
| 334 | } |
| 335 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 336 | ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) { |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 337 | LoadedModule module = this->loadModule(kind, data, base.fSymbols, /*dehydrate=*/false); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 338 | this->optimize(module); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 339 | |
| 340 | // For modules that just declare (but don't define) intrinsic functions, there will be no new |
| 341 | // 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] | 342 | if (module.fElements.empty()) { |
| 343 | return {module.fSymbols, base.fIntrinsics}; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get()); |
| 347 | |
| 348 | // Now, transfer all of the program elements to an intrinsic map. This maps certain types of |
| 349 | // global objects to the declaring ProgramElement. |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 350 | for (std::unique_ptr<ProgramElement>& element : module.fElements) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 351 | switch (element->kind()) { |
| 352 | case ProgramElement::Kind::kFunction: { |
| 353 | const FunctionDefinition& f = element->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 354 | SkASSERT(f.declaration().isBuiltin()); |
| 355 | intrinsics->insertOrDie(f.declaration().description(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 356 | break; |
| 357 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 358 | case ProgramElement::Kind::kFunctionPrototype: { |
| 359 | // These are already in the symbol table. |
| 360 | break; |
| 361 | } |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 362 | case ProgramElement::Kind::kEnum: { |
| 363 | const Enum& e = element->as<Enum>(); |
| 364 | SkASSERT(e.isBuiltin()); |
| 365 | intrinsics->insertOrDie(e.typeName(), std::move(element)); |
| 366 | break; |
| 367 | } |
| 368 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 369 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 370 | const Variable& var = global.declaration()->as<VarDeclaration>().var(); |
| 371 | SkASSERT(var.isBuiltin()); |
| 372 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 373 | break; |
| 374 | } |
| 375 | case ProgramElement::Kind::kInterfaceBlock: { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 376 | const Variable& var = element->as<InterfaceBlock>().variable(); |
| 377 | SkASSERT(var.isBuiltin()); |
| 378 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | default: |
| 382 | printf("Unsupported element: %s\n", element->description().c_str()); |
| 383 | SkASSERT(false); |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 388 | return {module.fSymbols, std::move(intrinsics)}; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 391 | std::unique_ptr<Program> Compiler::convertProgram( |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 392 | ProgramKind kind, |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 393 | String text, |
| 394 | const Program::Settings& settings, |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 395 | const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions) { |
Brian Osman | d010f65 | 2021-02-24 13:59:39 -0500 | [diff] [blame] | 396 | TRACE_EVENT0("skia.gpu", "SkSL::Compiler::convertProgram"); |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 397 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 398 | SkASSERT(!externalFunctions || (kind == ProgramKind::kGeneric)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 399 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 400 | // Loading and optimizing our base module might reset the inliner, so do that first, |
| 401 | // *then* configure the inliner with the settings for this program. |
| 402 | const ParsedModule& baseModule = this->moduleForProgramKind(kind); |
| 403 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 404 | // Update our context to point to the program configuration for the duration of compilation. |
| 405 | auto config = std::make_unique<ProgramConfig>(ProgramConfig{kind, settings}); |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 406 | AutoProgramConfig autoConfig(fContext, config.get()); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 407 | |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 408 | // Honor our global optimization-disable flags. |
| 409 | config->fSettings.fOptimize &= gSkSLOptimizer; |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 410 | config->fSettings.fInlineThreshold *= (int)gSkSLInliner; |
| 411 | |
| 412 | // 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] | 413 | config->fSettings.fInlineThreshold *= (int)config->fSettings.fOptimize; |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 414 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 415 | fErrorText = ""; |
| 416 | fErrorCount = 0; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 417 | fInliner.reset(fIRGenerator->fModifiers.get()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 418 | |
| 419 | // 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] | 420 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 421 | fSource = textPtr.get(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 422 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 423 | // Enable node pooling while converting and optimizing the program for a performance boost. |
| 424 | // The Program will take ownership of the pool. |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 425 | std::unique_ptr<Pool> pool; |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 426 | if (fContext->fCaps.useNodePools()) { |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 427 | pool = Pool::Create(); |
| 428 | pool->attachToThread(); |
| 429 | } |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 430 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false, |
| 431 | textPtr->c_str(), textPtr->size(), |
| 432 | externalFunctions); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 433 | auto program = std::make_unique<Program>(std::move(textPtr), |
| 434 | std::move(config), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 435 | fContext, |
| 436 | std::move(ir.fElements), |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 437 | std::move(ir.fSharedElements), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 438 | std::move(ir.fModifiers), |
| 439 | std::move(ir.fSymbolTable), |
| 440 | std::move(pool), |
| 441 | ir.fInputs); |
| 442 | bool success = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 443 | if (fErrorCount) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 444 | // Do not return programs that failed to compile. |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 445 | } else if (!this->optimize(*program)) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 446 | // Do not return programs that failed to optimize. |
| 447 | } else { |
| 448 | // We have a successful program! |
| 449 | success = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 450 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 451 | |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 452 | if (program->fPool) { |
| 453 | program->fPool->detachFromThread(); |
| 454 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 455 | return success ? std::move(program) : nullptr; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 456 | } |
| 457 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 458 | void Compiler::verifyStaticTests(const Program& program) { |
| 459 | class StaticTestVerifier : public ProgramVisitor { |
| 460 | public: |
| 461 | StaticTestVerifier(ErrorReporter* r) : fReporter(r) {} |
| 462 | |
| 463 | using ProgramVisitor::visitProgramElement; |
| 464 | |
| 465 | bool visitStatement(const Statement& stmt) override { |
| 466 | switch (stmt.kind()) { |
| 467 | case Statement::Kind::kIf: |
| 468 | if (stmt.as<IfStatement>().isStatic()) { |
| 469 | fReporter->error(stmt.fOffset, "static if has non-static test"); |
| 470 | } |
| 471 | break; |
| 472 | |
| 473 | case Statement::Kind::kSwitch: |
| 474 | if (stmt.as<SwitchStatement>().isStatic()) { |
| 475 | fReporter->error(stmt.fOffset, "static switch has non-static test"); |
| 476 | } |
| 477 | break; |
| 478 | |
| 479 | default: |
| 480 | break; |
| 481 | } |
| 482 | return INHERITED::visitStatement(stmt); |
| 483 | } |
| 484 | |
John Stiles | 59e3456 | 2021-02-12 16:56:39 -0500 | [diff] [blame] | 485 | bool visitExpression(const Expression&) override { |
| 486 | // We aren't looking for anything inside an Expression, so skip them entirely. |
| 487 | return false; |
| 488 | } |
| 489 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 490 | private: |
| 491 | using INHERITED = ProgramVisitor; |
| 492 | ErrorReporter* fReporter; |
| 493 | }; |
| 494 | |
| 495 | // 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] | 496 | if (fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
| 500 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
| 501 | StaticTestVerifier visitor{this}; |
| 502 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
| 503 | if (element->is<FunctionDefinition>()) { |
| 504 | visitor.visitProgramElement(*element); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 509 | bool Compiler::optimize(LoadedModule& module) { |
| 510 | SkASSERT(!fErrorCount); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 511 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 512 | // Create a temporary program configuration with default settings. |
| 513 | ProgramConfig config; |
| 514 | config.fKind = module.fKind; |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 515 | AutoProgramConfig autoConfig(fContext, &config); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 516 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 517 | // Reset the Inliner. |
| 518 | fInliner.reset(fModifiers.back().get()); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 519 | |
| 520 | std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 521 | |
| 522 | while (fErrorCount == 0) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 523 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame^] | 524 | if (!fInliner.analyze(module.fElements, module.fSymbols, usage.get())) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | } |
| 528 | return fErrorCount == 0; |
| 529 | } |
| 530 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 531 | bool Compiler::optimize(Program& program) { |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 532 | // The optimizer only needs to run when it is enabled. |
| 533 | if (!program.fConfig->fSettings.fOptimize) { |
| 534 | return true; |
| 535 | } |
| 536 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 537 | SkASSERT(!fErrorCount); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 538 | ProgramUsage* usage = program.fUsage.get(); |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 539 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 540 | while (fErrorCount == 0) { |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 541 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame^] | 542 | bool madeChanges = fInliner.analyze(program.ownedElements(), program.fSymbols, usage); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 543 | |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame^] | 544 | // Remove dead functions. |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 545 | if (program.fConfig->fSettings.fRemoveDeadFunctions) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 546 | auto isDeadFunction = [&](const ProgramElement* element) { |
| 547 | if (!element->is<FunctionDefinition>()) { |
| 548 | return false; |
| 549 | } |
| 550 | const FunctionDefinition& fn = element->as<FunctionDefinition>(); |
| 551 | if (fn.declaration().name() != "main" && usage->get(fn.declaration()) == 0) { |
| 552 | usage->remove(*element); |
| 553 | madeChanges = true; |
| 554 | return true; |
| 555 | } |
| 556 | return false; |
| 557 | }; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 558 | program.fElements.erase( |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 559 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 560 | [&](const std::unique_ptr<ProgramElement>& element) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 561 | return isDeadFunction(element.get()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 562 | }), |
| 563 | program.fElements.end()); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 564 | program.fSharedElements.erase( |
| 565 | std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(), |
| 566 | isDeadFunction), |
| 567 | program.fSharedElements.end()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 568 | } |
| 569 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 570 | if (program.fConfig->fKind != ProgramKind::kFragmentProcessor) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 571 | // Remove declarations of dead global variables |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 572 | auto isDeadVariable = [&](const ProgramElement* element) { |
| 573 | if (!element->is<GlobalVarDeclaration>()) { |
| 574 | return false; |
| 575 | } |
| 576 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 577 | const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>(); |
| 578 | if (usage->isDead(varDecl.var())) { |
| 579 | madeChanges = true; |
| 580 | return true; |
| 581 | } |
| 582 | return false; |
| 583 | }; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 584 | program.fElements.erase( |
| 585 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
| 586 | [&](const std::unique_ptr<ProgramElement>& element) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 587 | return isDeadVariable(element.get()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 588 | }), |
| 589 | program.fElements.end()); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 590 | program.fSharedElements.erase( |
| 591 | std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(), |
| 592 | isDeadVariable), |
| 593 | program.fSharedElements.end()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 594 | } |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 595 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 596 | if (!madeChanges) { |
| 597 | break; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 598 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 599 | } |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 600 | |
| 601 | if (fErrorCount == 0) { |
| 602 | this->verifyStaticTests(program); |
| 603 | } |
| 604 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 605 | return fErrorCount == 0; |
| 606 | } |
| 607 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 608 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 609 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 610 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 611 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 612 | StringStream buffer; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 613 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 614 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 615 | bool result = cg.generateCode(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 616 | if (result && program.fConfig->fSettings.fValidateSPIRV) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 617 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 618 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 619 | SkASSERT(0 == data.size() % 4); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 620 | String errors; |
| 621 | auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&, |
| 622 | const char* m) { |
| 623 | errors.appendf("SPIR-V validation error: %s\n", m); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 624 | }; |
| 625 | tools.SetMessageConsumer(dumpmsg); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 626 | |
| 627 | // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message |
| 628 | // explaining the error. In standalone mode (skslc), we will send the message, plus the |
| 629 | // entire disassembled SPIR-V (for easier context & debugging) as *our* error message. |
| 630 | result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4); |
| 631 | |
| 632 | if (!result) { |
| 633 | #if defined(SKSL_STANDALONE) |
| 634 | // Convert the string-stream to a SPIR-V disassembly. |
| 635 | std::string disassembly; |
| 636 | if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) { |
| 637 | errors.append(disassembly); |
| 638 | } |
| 639 | this->error(-1, errors); |
| 640 | #else |
| 641 | SkDEBUGFAILF("%s", errors.c_str()); |
| 642 | #endif |
| 643 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 644 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 645 | } |
| 646 | #else |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 647 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 648 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 649 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 650 | #endif |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 651 | return result; |
| 652 | } |
| 653 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 654 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 655 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 656 | bool result = this->toSPIRV(program, buffer); |
| 657 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 658 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 659 | } |
| 660 | return result; |
| 661 | } |
| 662 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 663 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Brian Osman | d010f65 | 2021-02-24 13:59:39 -0500 | [diff] [blame] | 664 | TRACE_EVENT0("skia.gpu", "SkSL::Compiler::toGLSL"); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 665 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 666 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 667 | bool result = cg.generateCode(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 668 | return result; |
| 669 | } |
| 670 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 671 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 672 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 673 | bool result = this->toGLSL(program, buffer); |
| 674 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 675 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 676 | } |
| 677 | return result; |
| 678 | } |
| 679 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 680 | bool Compiler::toHLSL(Program& program, String* out) { |
| 681 | String spirv; |
| 682 | if (!this->toSPIRV(program, &spirv)) { |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | return SPIRVtoHLSL(spirv, out); |
| 687 | } |
| 688 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 689 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 690 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 691 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 692 | return result; |
| 693 | } |
| 694 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 695 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 696 | StringStream buffer; |
| 697 | bool result = this->toMetal(program, buffer); |
| 698 | if (result) { |
| 699 | *out = buffer.str(); |
| 700 | } |
| 701 | return result; |
| 702 | } |
| 703 | |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 704 | #if defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 705 | bool Compiler::toCPP(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 706 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 707 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 708 | bool result = cg.generateCode(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 709 | return result; |
| 710 | } |
| 711 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 712 | bool Compiler::toH(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 713 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 714 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 715 | bool result = cg.generateCode(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 716 | return result; |
| 717 | } |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 718 | #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 719 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 720 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 721 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 722 | Position Compiler::position(int offset) { |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 723 | if (fSource && offset >= 0) { |
| 724 | int line = 1; |
| 725 | int column = 1; |
| 726 | for (int i = 0; i < offset; i++) { |
| 727 | if ((*fSource)[i] == '\n') { |
| 728 | ++line; |
| 729 | column = 1; |
| 730 | } |
| 731 | else { |
| 732 | ++column; |
| 733 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 734 | } |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 735 | return Position(line, column); |
| 736 | } else { |
| 737 | return Position(-1, -1); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 738 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 742 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 743 | Position pos = this->position(offset); |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 744 | fErrorTextLength.push_back(fErrorText.length()); |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 745 | fErrorText += "error: " + (pos.fLine >= 1 ? to_string(pos.fLine) + ": " : "") + msg + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 746 | } |
| 747 | |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 748 | void Compiler::setErrorCount(int c) { |
| 749 | if (c < fErrorCount) { |
| 750 | fErrorText.resize(fErrorTextLength[c]); |
| 751 | fErrorTextLength.resize(c); |
| 752 | fErrorCount = c; |
| 753 | } |
| 754 | } |
| 755 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 756 | String Compiler::errorText(bool showCount) { |
| 757 | if (showCount) { |
| 758 | this->writeErrorCount(); |
| 759 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 760 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 761 | String result = fErrorText; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 762 | fErrorText = ""; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 763 | return result; |
| 764 | } |
| 765 | |
| 766 | void Compiler::writeErrorCount() { |
| 767 | if (fErrorCount) { |
| 768 | fErrorText += to_string(fErrorCount) + " error"; |
| 769 | if (fErrorCount > 1) { |
| 770 | fErrorText += "s"; |
| 771 | } |
| 772 | fErrorText += "\n"; |
| 773 | } |
| 774 | } |
| 775 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 776 | } // namespace SkSL |