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