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