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