Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | */ |
| 7 | |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 8 | #include "src/sksl/codegen/SkSLPipelineStageCodeGenerator.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 9 | |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 10 | #include "include/private/SkSLProgramElement.h" |
| 11 | #include "include/private/SkSLStatement.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLCompiler.h" |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 13 | #include "src/sksl/SkSLOperators.h" |
| 14 | #include "src/sksl/SkSLStringStream.h" |
| 15 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLChildCall.h" |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 17 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 18 | #include "src/sksl/ir/SkSLConstructorArrayCast.h" |
John Stiles | b82e931 | 2021-05-20 15:30:22 -0400 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLDoStatement.h" |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 20 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 21 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 22 | #include "src/sksl/ir/SkSLForStatement.h" |
| 23 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 24 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 25 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLFunctionPrototype.h" |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 27 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 28 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 29 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 30 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 31 | #include "src/sksl/ir/SkSLReturnStatement.h" |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 32 | #include "src/sksl/ir/SkSLStructDefinition.h" |
John Stiles | ed2baba | 2021-09-20 19:09:13 -0400 | [diff] [blame^] | 33 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 34 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 35 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 36 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 37 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 38 | |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 39 | #include <unordered_map> |
| 40 | |
Brian Osman | 62b039b | 2021-02-08 13:49:53 -0500 | [diff] [blame] | 41 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 42 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 43 | namespace SkSL { |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 44 | namespace PipelineStage { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 45 | |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 46 | class PipelineStageCodeGenerator { |
| 47 | public: |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 48 | PipelineStageCodeGenerator(const Program& program, |
| 49 | const char* sampleCoords, |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 50 | const char* inputColor, |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 51 | const char* destColor, |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 52 | Callbacks* callbacks) |
| 53 | : fProgram(program) |
| 54 | , fSampleCoords(sampleCoords) |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 55 | , fInputColor(inputColor) |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 56 | , fDestColor(destColor) |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 57 | , fCallbacks(callbacks) {} |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 58 | |
| 59 | void generateCode(); |
| 60 | |
| 61 | private: |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 62 | using Precedence = Operator::Precedence; |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 63 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 64 | void write(skstd::string_view s); |
| 65 | void writeLine(skstd::string_view s = skstd::string_view()); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 66 | |
Brian Osman | 55761ae | 2021-02-09 16:52:53 -0500 | [diff] [blame] | 67 | String typeName(const Type& type); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 68 | void writeType(const Type& type); |
| 69 | |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 70 | String functionName(const FunctionDeclaration& decl); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 71 | void writeFunction(const FunctionDefinition& f); |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 72 | void writeFunctionPrototype(const FunctionPrototype& f); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 73 | |
Brian Osman | 4f3f64c | 2021-03-09 16:35:41 -0500 | [diff] [blame] | 74 | String modifierString(const Modifiers& modifiers); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 75 | |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 76 | // Handles arrays correctly, eg: `float x[2]` |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 77 | String typedVariable(const Type& type, skstd::string_view name); |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 78 | |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 79 | void writeVarDeclaration(const VarDeclaration& var); |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 80 | void writeGlobalVarDeclaration(const GlobalVarDeclaration& g); |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 81 | void writeStructDefinition(const StructDefinition& s); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 82 | |
| 83 | void writeExpression(const Expression& expr, Precedence parentPrecedence); |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 84 | void writeChildCall(const ChildCall& c); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 85 | void writeFunctionCall(const FunctionCall& c); |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 86 | void writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 87 | void writeFieldAccess(const FieldAccess& f); |
| 88 | void writeSwizzle(const Swizzle& swizzle); |
| 89 | void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
| 90 | void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
| 91 | void writeIndexExpression(const IndexExpression& expr); |
| 92 | void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 93 | void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 94 | void writeVariableReference(const VariableReference& ref); |
| 95 | |
| 96 | void writeStatement(const Statement& s); |
| 97 | void writeBlock(const Block& b); |
| 98 | void writeIfStatement(const IfStatement& stmt); |
John Stiles | b82e931 | 2021-05-20 15:30:22 -0400 | [diff] [blame] | 99 | void writeDoStatement(const DoStatement& d); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 100 | void writeForStatement(const ForStatement& f); |
| 101 | void writeReturnStatement(const ReturnStatement& r); |
John Stiles | ed2baba | 2021-09-20 19:09:13 -0400 | [diff] [blame^] | 102 | void writeSwitchStatement(const SwitchStatement& s); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 103 | |
| 104 | void writeProgramElement(const ProgramElement& e); |
| 105 | |
| 106 | struct AutoOutputBuffer { |
| 107 | AutoOutputBuffer(PipelineStageCodeGenerator* generator) : fGenerator(generator) { |
| 108 | fOldBuffer = fGenerator->fBuffer; |
| 109 | fGenerator->fBuffer = &fBuffer; |
| 110 | } |
| 111 | |
| 112 | ~AutoOutputBuffer() { |
| 113 | fGenerator->fBuffer = fOldBuffer; |
| 114 | } |
| 115 | |
| 116 | PipelineStageCodeGenerator* fGenerator; |
| 117 | StringStream* fOldBuffer; |
| 118 | StringStream fBuffer; |
| 119 | }; |
| 120 | |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 121 | const Program& fProgram; |
| 122 | const char* fSampleCoords; |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 123 | const char* fInputColor; |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 124 | const char* fDestColor; |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 125 | Callbacks* fCallbacks; |
| 126 | |
Brian Osman | 5e603c3 | 2021-02-17 15:39:06 -0500 | [diff] [blame] | 127 | std::unordered_map<const Variable*, String> fVariableNames; |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 128 | std::unordered_map<const FunctionDeclaration*, String> fFunctionNames; |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 129 | std::unordered_map<const Type*, String> fStructNames; |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 130 | |
| 131 | StringStream* fBuffer = nullptr; |
| 132 | bool fCastReturnsToHalf = false; |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 133 | }; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 134 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 135 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 136 | void PipelineStageCodeGenerator::write(skstd::string_view s) { |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 137 | fBuffer->write(s.data(), s.length()); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 140 | void PipelineStageCodeGenerator::writeLine(skstd::string_view s) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 141 | fBuffer->write(s.data(), s.length()); |
| 142 | fBuffer->writeText("\n"); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 145 | void PipelineStageCodeGenerator::writeChildCall(const ChildCall& c) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 146 | const ExpressionArray& arguments = c.arguments(); |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 147 | SkASSERT(arguments.size() >= 1); |
| 148 | int index = 0; |
| 149 | bool found = false; |
| 150 | for (const ProgramElement* p : fProgram.elements()) { |
| 151 | if (p->is<GlobalVarDeclaration>()) { |
| 152 | const GlobalVarDeclaration& global = p->as<GlobalVarDeclaration>(); |
| 153 | const VarDeclaration& decl = global.declaration()->as<VarDeclaration>(); |
| 154 | if (&decl.var() == &c.child()) { |
| 155 | found = true; |
| 156 | } else if (decl.var().type().isEffectChild()) { |
| 157 | ++index; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 158 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 159 | } |
| 160 | if (found) { |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | SkASSERT(found); |
| 165 | |
| 166 | // Shaders require a coordinate argument. Color filters require a color argument. |
| 167 | // Blenders require two color arguments. |
| 168 | String sampleOutput; |
| 169 | { |
| 170 | AutoOutputBuffer exprBuffer(this); |
| 171 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 172 | |
| 173 | switch (c.child().type().typeKind()) { |
| 174 | case Type::TypeKind::kShader: { |
| 175 | SkASSERT(arguments.size() == 1); |
| 176 | SkASSERT(arguments[0]->type() == *fProgram.fContext->fTypes.fFloat2); |
| 177 | sampleOutput = fCallbacks->sampleShader(index, exprBuffer.fBuffer.str()); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 178 | break; |
| 179 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 180 | case Type::TypeKind::kColorFilter: { |
| 181 | SkASSERT(arguments.size() == 1); |
| 182 | SkASSERT(arguments[0]->type() == *fProgram.fContext->fTypes.fHalf4 || |
| 183 | arguments[0]->type() == *fProgram.fContext->fTypes.fFloat4); |
| 184 | sampleOutput = fCallbacks->sampleColorFilter(index, exprBuffer.fBuffer.str()); |
| 185 | break; |
| 186 | } |
| 187 | case Type::TypeKind::kBlender: { |
| 188 | SkASSERT(arguments.size() == 2); |
| 189 | SkASSERT(arguments[0]->type() == *fProgram.fContext->fTypes.fHalf4 || |
| 190 | arguments[0]->type() == *fProgram.fContext->fTypes.fFloat4); |
| 191 | SkASSERT(arguments[1]->type() == *fProgram.fContext->fTypes.fHalf4 || |
| 192 | arguments[1]->type() == *fProgram.fContext->fTypes.fFloat4); |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 193 | |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 194 | AutoOutputBuffer exprBuffer2(this); |
| 195 | this->writeExpression(*arguments[1], Precedence::kSequence); |
John Stiles | 8050a4b | 2021-07-23 15:50:20 -0400 | [diff] [blame] | 196 | |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 197 | sampleOutput = fCallbacks->sampleBlender(index, exprBuffer.fBuffer.str(), |
| 198 | exprBuffer2.fBuffer.str()); |
| 199 | break; |
| 200 | } |
| 201 | default: { |
| 202 | SkDEBUGFAILF("cannot sample from type '%s'", |
| 203 | c.child().type().description().c_str()); |
Brian Osman | c9125aa | 2021-04-21 09:57:19 -0400 | [diff] [blame] | 204 | } |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 205 | } |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 206 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 207 | this->write(sampleOutput); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | void PipelineStageCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
| 212 | const FunctionDeclaration& function = c.function(); |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 213 | |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 214 | if (function.isBuiltin()) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 215 | this->write(function.name()); |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 216 | } else { |
John Stiles | 699736b | 2021-08-05 16:35:27 -0400 | [diff] [blame] | 217 | this->write(this->functionName(function)); |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 218 | } |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 219 | |
| 220 | this->write("("); |
| 221 | const char* separator = ""; |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 222 | for (const auto& arg : c.arguments()) { |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 223 | this->write(separator); |
| 224 | separator = ", "; |
| 225 | this->writeExpression(*arg, Precedence::kSequence); |
| 226 | } |
| 227 | this->write(")"); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 230 | void PipelineStageCodeGenerator::writeVariableReference(const VariableReference& ref) { |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 231 | const Variable* var = ref.variable(); |
| 232 | const Modifiers& modifiers = var->modifiers(); |
Brian Osman | 3c35842 | 2020-03-23 10:44:12 -0400 | [diff] [blame] | 233 | |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 234 | if (modifiers.fLayout.fBuiltin == SK_MAIN_COORDS_BUILTIN) { |
| 235 | this->write(fSampleCoords); |
| 236 | return; |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 237 | } else if (modifiers.fLayout.fBuiltin == SK_INPUT_COLOR_BUILTIN) { |
| 238 | this->write(fInputColor); |
| 239 | return; |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 240 | } else if (modifiers.fLayout.fBuiltin == SK_DEST_COLOR_BUILTIN) { |
| 241 | this->write(fDestColor); |
| 242 | return; |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 243 | } |
| 244 | |
Brian Osman | e49703f | 2021-04-19 11:15:24 -0400 | [diff] [blame] | 245 | auto it = fVariableNames.find(var); |
| 246 | if (it != fVariableNames.end()) { |
| 247 | this->write(it->second); |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 248 | } else { |
Brian Osman | e49703f | 2021-04-19 11:15:24 -0400 | [diff] [blame] | 249 | this->write(var->name()); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 253 | void PipelineStageCodeGenerator::writeIfStatement(const IfStatement& stmt) { |
| 254 | if (stmt.isStatic()) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 255 | this->write("@"); |
| 256 | } |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 257 | this->write("if ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 258 | this->writeExpression(*stmt.test(), Precedence::kTopLevel); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 259 | this->write(") "); |
| 260 | this->writeStatement(*stmt.ifTrue()); |
| 261 | if (stmt.ifFalse()) { |
| 262 | this->write(" else "); |
| 263 | this->writeStatement(*stmt.ifFalse()); |
| 264 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 267 | void PipelineStageCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
| 268 | this->write("return"); |
| 269 | if (r.expression()) { |
| 270 | this->write(" "); |
| 271 | if (fCastReturnsToHalf) { |
| 272 | this->write("half4("); |
| 273 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 274 | this->writeExpression(*r.expression(), Precedence::kTopLevel); |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 275 | if (fCastReturnsToHalf) { |
| 276 | this->write(")"); |
| 277 | } |
| 278 | } |
| 279 | this->write(";"); |
| 280 | } |
| 281 | |
John Stiles | ed2baba | 2021-09-20 19:09:13 -0400 | [diff] [blame^] | 282 | void PipelineStageCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 283 | this->write("switch ("); |
| 284 | this->writeExpression(*s.value(), Precedence::kTopLevel); |
| 285 | this->writeLine(") {"); |
| 286 | for (const std::unique_ptr<Statement>& stmt : s.cases()) { |
| 287 | const SwitchCase& c = stmt->as<SwitchCase>(); |
| 288 | if (c.value()) { |
| 289 | this->write("case "); |
| 290 | this->writeExpression(*c.value(), Precedence::kTopLevel); |
| 291 | this->writeLine(":"); |
| 292 | } else { |
| 293 | this->writeLine("default:"); |
| 294 | } |
| 295 | if (!c.statement()->isEmpty()) { |
| 296 | this->writeStatement(*c.statement()); |
| 297 | this->writeLine(); |
| 298 | } |
| 299 | } |
| 300 | this->writeLine(); |
| 301 | this->write("}"); |
| 302 | } |
| 303 | |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 304 | String PipelineStageCodeGenerator::functionName(const FunctionDeclaration& decl) { |
| 305 | if (decl.isMain()) { |
| 306 | return String(decl.name()); |
| 307 | } |
| 308 | |
| 309 | auto it = fFunctionNames.find(&decl); |
| 310 | if (it != fFunctionNames.end()) { |
| 311 | return it->second; |
| 312 | } |
| 313 | |
| 314 | String mangledName = fCallbacks->getMangledName(String(decl.name()).c_str()); |
| 315 | fFunctionNames.insert({&decl, mangledName}); |
| 316 | return mangledName; |
| 317 | } |
| 318 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 319 | void PipelineStageCodeGenerator::writeFunction(const FunctionDefinition& f) { |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 320 | AutoOutputBuffer body(this); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 321 | |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 322 | // We allow public SkSL's main() to return half4 -or- float4 (ie vec4). When we emit |
| 323 | // our code in the processor, the surrounding code is going to expect half4, so we |
| 324 | // explicitly cast any returns (from main) to half4. This is only strictly necessary |
| 325 | // if the return type is float4 - injecting it unconditionally reduces the risk of an |
| 326 | // obscure bug. |
John Stiles | aa4f4c7 | 2021-03-22 16:08:15 -0400 | [diff] [blame] | 327 | const FunctionDeclaration& decl = f.declaration(); |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 328 | if (decl.isMain()) { |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 329 | fCastReturnsToHalf = true; |
| 330 | } |
| 331 | |
| 332 | for (const std::unique_ptr<Statement>& stmt : f.body()->as<Block>().children()) { |
| 333 | this->writeStatement(*stmt); |
| 334 | this->writeLine(); |
| 335 | } |
| 336 | |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 337 | if (decl.isMain()) { |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 338 | fCastReturnsToHalf = false; |
| 339 | } |
| 340 | |
John Stiles | aa4f4c7 | 2021-03-22 16:08:15 -0400 | [diff] [blame] | 341 | // This is similar to decl.description(), but substitutes a mangled name, and handles modifiers |
| 342 | // on the function (e.g. `inline`) and its parameters (e.g. `inout`). |
Brian Osman | 55761ae | 2021-02-09 16:52:53 -0500 | [diff] [blame] | 343 | String declString = |
John Stiles | aa4f4c7 | 2021-03-22 16:08:15 -0400 | [diff] [blame] | 344 | String::printf("%s%s%s %s(", |
| 345 | (decl.modifiers().fFlags & Modifiers::kInline_Flag) ? "inline " : "", |
| 346 | (decl.modifiers().fFlags & Modifiers::kNoInline_Flag) ? "noinline " : "", |
| 347 | this->typeName(decl.returnType()).c_str(), |
John Stiles | 699736b | 2021-08-05 16:35:27 -0400 | [diff] [blame] | 348 | this->functionName(decl).c_str()); |
Brian Osman | 55761ae | 2021-02-09 16:52:53 -0500 | [diff] [blame] | 349 | const char* separator = ""; |
John Stiles | aa4f4c7 | 2021-03-22 16:08:15 -0400 | [diff] [blame] | 350 | for (const Variable* p : decl.parameters()) { |
Brian Osman | 55761ae | 2021-02-09 16:52:53 -0500 | [diff] [blame] | 351 | // TODO: Handle arrays |
Brian Osman | 4f3f64c | 2021-03-09 16:35:41 -0500 | [diff] [blame] | 352 | declString.appendf("%s%s%s %s", |
| 353 | separator, |
| 354 | this->modifierString(p->modifiers()).c_str(), |
| 355 | this->typeName(p->type()).c_str(), |
Brian Osman | 55761ae | 2021-02-09 16:52:53 -0500 | [diff] [blame] | 356 | String(p->name()).c_str()); |
| 357 | separator = ", "; |
| 358 | } |
| 359 | declString.append(")"); |
| 360 | |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 361 | fCallbacks->defineFunction(declString.c_str(), body.fBuffer.str().c_str(), decl.isMain()); |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 362 | } |
| 363 | |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 364 | void PipelineStageCodeGenerator::writeFunctionPrototype(const FunctionPrototype& f) { |
| 365 | const FunctionDeclaration& decl = f.declaration(); |
| 366 | (void)this->functionName(decl); |
| 367 | } |
| 368 | |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 369 | void PipelineStageCodeGenerator::writeGlobalVarDeclaration(const GlobalVarDeclaration& g) { |
| 370 | const VarDeclaration& decl = g.declaration()->as<VarDeclaration>(); |
| 371 | const Variable& var = decl.var(); |
| 372 | |
Brian Osman | ddcb4d9 | 2021-03-12 14:19:01 -0500 | [diff] [blame] | 373 | if (var.isBuiltin() || var.type().isOpaque()) { |
| 374 | // Don't re-declare these. (eg, sk_FragCoord, or fragmentProcessor children) |
Brian Osman | 5e603c3 | 2021-02-17 15:39:06 -0500 | [diff] [blame] | 375 | } else if (var.modifiers().fFlags & Modifiers::kUniform_Flag) { |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 376 | String uniformName = fCallbacks->declareUniform(&decl); |
Brian Osman | 5e603c3 | 2021-02-17 15:39:06 -0500 | [diff] [blame] | 377 | fVariableNames.insert({&var, std::move(uniformName)}); |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 378 | } else { |
Brian Osman | 5e603c3 | 2021-02-17 15:39:06 -0500 | [diff] [blame] | 379 | String mangledName = fCallbacks->getMangledName(String(var.name()).c_str()); |
Brian Osman | 4f3f64c | 2021-03-09 16:35:41 -0500 | [diff] [blame] | 380 | String declaration = this->modifierString(var.modifiers()) + |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 381 | this->typedVariable(var.type(), |
| 382 | skstd::string_view(mangledName.c_str())); |
Brian Osman | 5e603c3 | 2021-02-17 15:39:06 -0500 | [diff] [blame] | 383 | if (decl.value()) { |
| 384 | AutoOutputBuffer outputToBuffer(this); |
| 385 | this->writeExpression(*decl.value(), Precedence::kTopLevel); |
| 386 | declaration += " = "; |
| 387 | declaration += outputToBuffer.fBuffer.str(); |
| 388 | } |
| 389 | declaration += ";\n"; |
| 390 | fCallbacks->declareGlobal(declaration.c_str()); |
| 391 | fVariableNames.insert({&var, std::move(mangledName)}); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 395 | void PipelineStageCodeGenerator::writeStructDefinition(const StructDefinition& s) { |
| 396 | const Type& type = s.type(); |
| 397 | String mangledName = fCallbacks->getMangledName(String(type.name()).c_str()); |
| 398 | String definition = "struct " + mangledName + " {\n"; |
| 399 | for (const auto& f : type.fields()) { |
| 400 | definition += this->typedVariable(*f.fType, f.fName) + ";\n"; |
| 401 | } |
| 402 | definition += "};\n"; |
| 403 | fStructNames.insert({&type, std::move(mangledName)}); |
| 404 | fCallbacks->defineStruct(definition.c_str()); |
| 405 | } |
| 406 | |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 407 | void PipelineStageCodeGenerator::writeProgramElement(const ProgramElement& e) { |
| 408 | switch (e.kind()) { |
| 409 | case ProgramElement::Kind::kGlobalVar: |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 410 | this->writeGlobalVarDeclaration(e.as<GlobalVarDeclaration>()); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 411 | break; |
| 412 | case ProgramElement::Kind::kFunction: |
| 413 | this->writeFunction(e.as<FunctionDefinition>()); |
| 414 | break; |
| 415 | case ProgramElement::Kind::kFunctionPrototype: |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 416 | this->writeFunctionPrototype(e.as<FunctionPrototype>()); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 417 | break; |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 418 | case ProgramElement::Kind::kStructDefinition: |
| 419 | this->writeStructDefinition(e.as<StructDefinition>()); |
| 420 | break; |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 421 | |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 422 | case ProgramElement::Kind::kExtension: |
| 423 | case ProgramElement::Kind::kInterfaceBlock: |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 424 | case ProgramElement::Kind::kModifiers: |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 425 | default: |
| 426 | SkDEBUGFAILF("unsupported program element %s\n", e.description().c_str()); |
| 427 | break; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 428 | } |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Brian Osman | 55761ae | 2021-02-09 16:52:53 -0500 | [diff] [blame] | 431 | String PipelineStageCodeGenerator::typeName(const Type& type) { |
John Stiles | 958a939 | 2021-04-28 16:03:05 -0400 | [diff] [blame] | 432 | if (type.isArray()) { |
| 433 | // This is necessary so that name mangling on arrays-of-structs works properly. |
| 434 | String arrayName = this->typeName(type.componentType()); |
| 435 | arrayName.push_back('['); |
Brian Osman | b42c383 | 2021-08-27 15:19:03 -0400 | [diff] [blame] | 436 | arrayName += to_string(type.columns()); |
John Stiles | 958a939 | 2021-04-28 16:03:05 -0400 | [diff] [blame] | 437 | arrayName.push_back(']'); |
| 438 | return arrayName; |
| 439 | } |
| 440 | |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 441 | auto it = fStructNames.find(&type); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 442 | return it != fStructNames.end() ? it->second : String(type.name()); |
Brian Osman | 55761ae | 2021-02-09 16:52:53 -0500 | [diff] [blame] | 443 | } |
| 444 | |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 445 | void PipelineStageCodeGenerator::writeType(const Type& type) { |
Brian Osman | 55761ae | 2021-02-09 16:52:53 -0500 | [diff] [blame] | 446 | this->write(this->typeName(type)); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void PipelineStageCodeGenerator::writeExpression(const Expression& expr, |
| 450 | Precedence parentPrecedence) { |
| 451 | switch (expr.kind()) { |
| 452 | case Expression::Kind::kBinary: |
| 453 | this->writeBinaryExpression(expr.as<BinaryExpression>(), parentPrecedence); |
| 454 | break; |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 455 | case Expression::Kind::kLiteral: |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 456 | this->write(expr.description()); |
| 457 | break; |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 458 | case Expression::Kind::kChildCall: |
| 459 | this->writeChildCall(expr.as<ChildCall>()); |
| 460 | break; |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 461 | case Expression::Kind::kConstructorArray: |
John Stiles | 2648716 | 2021-08-10 16:03:44 -0400 | [diff] [blame] | 462 | case Expression::Kind::kConstructorArrayCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 463 | case Expression::Kind::kConstructorCompound: |
| 464 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 465 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 466 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 467 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 468 | case Expression::Kind::kConstructorSplat: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 469 | case Expression::Kind::kConstructorStruct: |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 470 | this->writeAnyConstructor(expr.asAnyConstructor(), parentPrecedence); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 471 | break; |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 472 | case Expression::Kind::kFieldAccess: |
| 473 | this->writeFieldAccess(expr.as<FieldAccess>()); |
| 474 | break; |
| 475 | case Expression::Kind::kFunctionCall: |
| 476 | this->writeFunctionCall(expr.as<FunctionCall>()); |
| 477 | break; |
| 478 | case Expression::Kind::kPrefix: |
| 479 | this->writePrefixExpression(expr.as<PrefixExpression>(), parentPrecedence); |
| 480 | break; |
| 481 | case Expression::Kind::kPostfix: |
| 482 | this->writePostfixExpression(expr.as<PostfixExpression>(), parentPrecedence); |
| 483 | break; |
| 484 | case Expression::Kind::kSwizzle: |
| 485 | this->writeSwizzle(expr.as<Swizzle>()); |
| 486 | break; |
| 487 | case Expression::Kind::kVariableReference: |
| 488 | this->writeVariableReference(expr.as<VariableReference>()); |
| 489 | break; |
| 490 | case Expression::Kind::kTernary: |
| 491 | this->writeTernaryExpression(expr.as<TernaryExpression>(), parentPrecedence); |
| 492 | break; |
| 493 | case Expression::Kind::kIndex: |
| 494 | this->writeIndexExpression(expr.as<IndexExpression>()); |
| 495 | break; |
| 496 | case Expression::Kind::kSetting: |
| 497 | default: |
| 498 | SkDEBUGFAILF("unsupported expression: %s", expr.description().c_str()); |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 503 | void PipelineStageCodeGenerator::writeAnyConstructor(const AnyConstructor& c, |
| 504 | Precedence parentPrecedence) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 505 | this->writeType(c.type()); |
| 506 | this->write("("); |
| 507 | const char* separator = ""; |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 508 | for (const auto& arg : c.argumentSpan()) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 509 | this->write(separator); |
| 510 | separator = ", "; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 511 | this->writeExpression(*arg, Precedence::kSequence); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 512 | } |
| 513 | this->write(")"); |
| 514 | } |
| 515 | |
| 516 | void PipelineStageCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 517 | this->writeExpression(*expr.base(), Precedence::kPostfix); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 518 | this->write("["); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 519 | this->writeExpression(*expr.index(), Precedence::kTopLevel); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 520 | this->write("]"); |
| 521 | } |
| 522 | |
| 523 | void PipelineStageCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
| 524 | if (f.ownerKind() == FieldAccess::OwnerKind::kDefault) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 525 | this->writeExpression(*f.base(), Precedence::kPostfix); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 526 | this->write("."); |
| 527 | } |
| 528 | const Type& baseType = f.base()->type(); |
| 529 | this->write(baseType.fields()[f.fieldIndex()].fName); |
| 530 | } |
| 531 | |
| 532 | void PipelineStageCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 533 | this->writeExpression(*swizzle.base(), Precedence::kPostfix); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 534 | this->write("."); |
| 535 | for (int c : swizzle.components()) { |
| 536 | SkASSERT(c >= 0 && c <= 3); |
| 537 | this->write(&("x\0y\0z\0w\0"[c * 2])); |
| 538 | } |
| 539 | } |
| 540 | |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 541 | void PipelineStageCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
| 542 | Precedence parentPrecedence) { |
| 543 | const Expression& left = *b.left(); |
| 544 | const Expression& right = *b.right(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 545 | Operator op = b.getOperator(); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 546 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 547 | Precedence precedence = op.getBinaryPrecedence(); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 548 | if (precedence >= parentPrecedence) { |
| 549 | this->write("("); |
| 550 | } |
| 551 | this->writeExpression(left, precedence); |
| 552 | this->write(" "); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 553 | this->write(op.operatorName()); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 554 | this->write(" "); |
| 555 | this->writeExpression(right, precedence); |
| 556 | if (precedence >= parentPrecedence) { |
| 557 | this->write(")"); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void PipelineStageCodeGenerator::writeTernaryExpression(const TernaryExpression& t, |
| 562 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 563 | if (Precedence::kTernary >= parentPrecedence) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 564 | this->write("("); |
| 565 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 566 | this->writeExpression(*t.test(), Precedence::kTernary); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 567 | this->write(" ? "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 568 | this->writeExpression(*t.ifTrue(), Precedence::kTernary); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 569 | this->write(" : "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 570 | this->writeExpression(*t.ifFalse(), Precedence::kTernary); |
| 571 | if (Precedence::kTernary >= parentPrecedence) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 572 | this->write(")"); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | void PipelineStageCodeGenerator::writePrefixExpression(const PrefixExpression& p, |
| 577 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 578 | if (Precedence::kPrefix >= parentPrecedence) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 579 | this->write("("); |
| 580 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 581 | this->write(p.getOperator().operatorName()); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 582 | this->writeExpression(*p.operand(), Precedence::kPrefix); |
| 583 | if (Precedence::kPrefix >= parentPrecedence) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 584 | this->write(")"); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | void PipelineStageCodeGenerator::writePostfixExpression(const PostfixExpression& p, |
| 589 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 590 | if (Precedence::kPostfix >= parentPrecedence) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 591 | this->write("("); |
| 592 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 593 | this->writeExpression(*p.operand(), Precedence::kPostfix); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 594 | this->write(p.getOperator().operatorName()); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 595 | if (Precedence::kPostfix >= parentPrecedence) { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 596 | this->write(")"); |
| 597 | } |
| 598 | } |
| 599 | |
Brian Osman | 4f3f64c | 2021-03-09 16:35:41 -0500 | [diff] [blame] | 600 | String PipelineStageCodeGenerator::modifierString(const Modifiers& modifiers) { |
| 601 | String result; |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 602 | if (modifiers.fFlags & Modifiers::kConst_Flag) { |
Brian Osman | 4f3f64c | 2021-03-09 16:35:41 -0500 | [diff] [blame] | 603 | result.append("const "); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 604 | } |
Brian Osman | 4f3f64c | 2021-03-09 16:35:41 -0500 | [diff] [blame] | 605 | |
| 606 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kOut_Flag)) { |
| 607 | result.append("inout "); |
| 608 | } else if (modifiers.fFlags & Modifiers::kIn_Flag) { |
| 609 | result.append("in "); |
| 610 | } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
| 611 | result.append("out "); |
| 612 | } |
| 613 | |
| 614 | return result; |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 615 | } |
| 616 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 617 | String PipelineStageCodeGenerator::typedVariable(const Type& type, skstd::string_view name) { |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 618 | const Type& baseType = type.isArray() ? type.componentType() : type; |
| 619 | |
| 620 | String decl = this->typeName(baseType) + " " + name; |
| 621 | if (type.isArray()) { |
| 622 | decl += "[" + to_string(type.columns()) + "]"; |
| 623 | } |
| 624 | return decl; |
| 625 | } |
| 626 | |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 627 | void PipelineStageCodeGenerator::writeVarDeclaration(const VarDeclaration& var) { |
Brian Osman | 4f3f64c | 2021-03-09 16:35:41 -0500 | [diff] [blame] | 628 | this->write(this->modifierString(var.var().modifiers())); |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 629 | this->write(this->typedVariable(var.var().type(), var.var().name())); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 630 | if (var.value()) { |
| 631 | this->write(" = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 632 | this->writeExpression(*var.value(), Precedence::kTopLevel); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 633 | } |
| 634 | this->write(";"); |
| 635 | } |
| 636 | |
| 637 | void PipelineStageCodeGenerator::writeStatement(const Statement& s) { |
| 638 | switch (s.kind()) { |
| 639 | case Statement::Kind::kBlock: |
| 640 | this->writeBlock(s.as<Block>()); |
| 641 | break; |
| 642 | case Statement::Kind::kBreak: |
| 643 | this->write("break;"); |
| 644 | break; |
| 645 | case Statement::Kind::kContinue: |
| 646 | this->write("continue;"); |
| 647 | break; |
| 648 | case Statement::Kind::kExpression: |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 649 | this->writeExpression(*s.as<ExpressionStatement>().expression(), Precedence::kTopLevel); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 650 | this->write(";"); |
| 651 | break; |
John Stiles | b82e931 | 2021-05-20 15:30:22 -0400 | [diff] [blame] | 652 | case Statement::Kind::kDo: |
| 653 | this->writeDoStatement(s.as<DoStatement>()); |
| 654 | break; |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 655 | case Statement::Kind::kFor: |
| 656 | this->writeForStatement(s.as<ForStatement>()); |
| 657 | break; |
| 658 | case Statement::Kind::kIf: |
| 659 | this->writeIfStatement(s.as<IfStatement>()); |
| 660 | break; |
| 661 | case Statement::Kind::kReturn: |
| 662 | this->writeReturnStatement(s.as<ReturnStatement>()); |
| 663 | break; |
John Stiles | ed2baba | 2021-09-20 19:09:13 -0400 | [diff] [blame^] | 664 | case Statement::Kind::kSwitch: |
| 665 | this->writeSwitchStatement(s.as<SwitchStatement>()); |
| 666 | break; |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 667 | case Statement::Kind::kVarDeclaration: |
| 668 | this->writeVarDeclaration(s.as<VarDeclaration>()); |
| 669 | break; |
| 670 | case Statement::Kind::kDiscard: |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 671 | SkDEBUGFAIL("Unsupported control flow"); |
| 672 | break; |
| 673 | case Statement::Kind::kInlineMarker: |
| 674 | case Statement::Kind::kNop: |
| 675 | this->write(";"); |
| 676 | break; |
| 677 | default: |
| 678 | SkDEBUGFAILF("unsupported statement: %s", s.description().c_str()); |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | void PipelineStageCodeGenerator::writeBlock(const Block& b) { |
| 684 | // Write scope markers if this block is a scope, or if the block is empty (since we need to emit |
| 685 | // something here to make the code valid). |
| 686 | bool isScope = b.isScope() || b.isEmpty(); |
| 687 | if (isScope) { |
| 688 | this->writeLine("{"); |
| 689 | } |
| 690 | for (const std::unique_ptr<Statement>& stmt : b.children()) { |
| 691 | if (!stmt->isEmpty()) { |
| 692 | this->writeStatement(*stmt); |
| 693 | this->writeLine(); |
| 694 | } |
| 695 | } |
| 696 | if (isScope) { |
| 697 | this->write("}"); |
| 698 | } |
| 699 | } |
| 700 | |
John Stiles | b82e931 | 2021-05-20 15:30:22 -0400 | [diff] [blame] | 701 | void PipelineStageCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 702 | this->write("do "); |
| 703 | this->writeStatement(*d.statement()); |
| 704 | this->write(" while ("); |
| 705 | this->writeExpression(*d.test(), Precedence::kTopLevel); |
| 706 | this->write(");"); |
| 707 | return; |
| 708 | } |
| 709 | |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 710 | void PipelineStageCodeGenerator::writeForStatement(const ForStatement& f) { |
John Stiles | b82e931 | 2021-05-20 15:30:22 -0400 | [diff] [blame] | 711 | // Emit loops of the form 'for(;test;)' as 'while(test)', which is probably how they started |
| 712 | if (!f.initializer() && f.test() && !f.next()) { |
| 713 | this->write("while ("); |
| 714 | this->writeExpression(*f.test(), Precedence::kTopLevel); |
| 715 | this->write(") "); |
| 716 | this->writeStatement(*f.statement()); |
| 717 | return; |
| 718 | } |
| 719 | |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 720 | this->write("for ("); |
| 721 | if (f.initializer() && !f.initializer()->isEmpty()) { |
| 722 | this->writeStatement(*f.initializer()); |
| 723 | } else { |
| 724 | this->write("; "); |
| 725 | } |
| 726 | if (f.test()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 727 | this->writeExpression(*f.test(), Precedence::kTopLevel); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 728 | } |
| 729 | this->write("; "); |
| 730 | if (f.next()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 731 | this->writeExpression(*f.next(), Precedence::kTopLevel); |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 732 | } |
| 733 | this->write(") "); |
| 734 | this->writeStatement(*f.statement()); |
| 735 | } |
| 736 | |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 737 | void PipelineStageCodeGenerator::generateCode() { |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 738 | // Write all the program elements except for functions. |
| 739 | for (const ProgramElement* e : fProgram.elements()) { |
| 740 | if (!e->is<FunctionDefinition>()) { |
| 741 | this->writeProgramElement(*e); |
| 742 | } |
| 743 | } |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 744 | |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 745 | // We always place FunctionDefinition elements last, because the inliner likes to move function |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 746 | // bodies around. After inlining, code can inadvertently move upwards, above ProgramElements |
| 747 | // that the code relies on. |
| 748 | for (const ProgramElement* e : fProgram.elements()) { |
| 749 | if (e->is<FunctionDefinition>()) { |
| 750 | this->writeProgramElement(*e); |
| 751 | } |
| 752 | } |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 753 | } |
| 754 | |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 755 | void ConvertProgram(const Program& program, |
| 756 | const char* sampleCoords, |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 757 | const char* inputColor, |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 758 | const char* destColor, |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 759 | Callbacks* callbacks) { |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 760 | PipelineStageCodeGenerator generator(program, sampleCoords, inputColor, destColor, callbacks); |
Brian Osman | 236ddb3 | 2021-02-05 12:15:29 -0500 | [diff] [blame] | 761 | generator.generateCode(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 762 | } |
| 763 | |
Brian Osman | 690b6f3 | 2021-02-05 16:30:34 -0500 | [diff] [blame] | 764 | } // namespace PipelineStage |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 765 | } // namespace SkSL |
Brian Osman | ca21e14 | 2021-02-03 15:34:29 -0500 | [diff] [blame] | 766 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 767 | #endif |