ethannicholas | f789b38 | 2016-08-03 12:43:36 -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 | */ |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 7 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 8 | #ifndef SKSL_GLSLCODEGENERATOR |
| 9 | #define SKSL_GLSLCODEGENERATOR |
| 10 | |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 11 | #include <set> |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 12 | #include <stack> |
| 13 | #include <tuple> |
| 14 | #include <unordered_map> |
| 15 | |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 16 | #include "include/private/SkSLProgramElement.h" |
| 17 | #include "include/private/SkSLStatement.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 18 | #include "src/sksl/SkSLOperators.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/sksl/SkSLStringStream.h" |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 20 | #include "src/sksl/codegen/SkSLCodeGenerator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 22 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 23 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLConstructorScalarCast.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 26 | #include "src/sksl/ir/SkSLExtension.h" |
| 27 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 28 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 29 | #include "src/sksl/ir/SkSLForStatement.h" |
| 30 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 31 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 32 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 33 | #include "src/sksl/ir/SkSLFunctionPrototype.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 34 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 35 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 36 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 37 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 38 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 39 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 40 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 41 | #include "src/sksl/ir/SkSLSetting.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 42 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 43 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 44 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 45 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 46 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 47 | |
| 48 | namespace SkSL { |
| 49 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Converts a Program into GLSL code. |
| 52 | */ |
| 53 | class GLSLCodeGenerator : public CodeGenerator { |
| 54 | public: |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 55 | GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 56 | OutputStream* out) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 57 | : INHERITED(program, errors, out) |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 58 | , fLineEnding("\n") |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 59 | , fContext(*context) {} |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 60 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 61 | bool generateCode() override; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 62 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 63 | protected: |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 64 | using Precedence = Operator::Precedence; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 65 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 66 | void write(const char* s); |
| 67 | |
| 68 | void writeLine(); |
| 69 | |
| 70 | void writeLine(const char* s); |
| 71 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 72 | void write(const String& s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 73 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 74 | void write(StringFragment s); |
| 75 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 76 | void writeLine(const String& s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 77 | |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 78 | void finishLine(); |
| 79 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 80 | virtual void writeHeader(); |
| 81 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 82 | virtual bool usesPrecisionModifiers() const; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 83 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 84 | virtual String getTypeName(const Type& type); |
| 85 | |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 86 | void writeStructDefinition(const StructDefinition& s); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 87 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 88 | void writeType(const Type& type); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 89 | |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 90 | void writeExtension(const String& name); |
| 91 | |
| 92 | void writeExtension(const String& name, bool require); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 93 | |
| 94 | void writeInterfaceBlock(const InterfaceBlock& intf); |
| 95 | |
| 96 | void writeFunctionStart(const FunctionDeclaration& f); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 97 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 98 | void writeFunctionDeclaration(const FunctionDeclaration& f); |
| 99 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 100 | void writeFunctionPrototype(const FunctionPrototype& f); |
| 101 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 102 | virtual void writeFunction(const FunctionDefinition& f); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 103 | |
| 104 | void writeLayout(const Layout& layout); |
| 105 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 106 | void writeModifiers(const Modifiers& modifiers, bool globalContext); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 107 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 108 | virtual void writeInputVars(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 109 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 110 | virtual void writeVarInitializer(const Variable& var, const Expression& value); |
| 111 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 112 | const char* getTypePrecision(const Type& type); |
| 113 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 114 | void writeTypePrecision(const Type& type); |
| 115 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 116 | void writeVarDeclaration(const VarDeclaration& var, bool global); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 117 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 118 | void writeFragCoord(); |
| 119 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 120 | virtual void writeVariableReference(const VariableReference& ref); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 121 | |
| 122 | void writeExpression(const Expression& expr, Precedence parentPrecedence); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 123 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 124 | void writeIntrinsicCall(const FunctionCall& c); |
| 125 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 126 | void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); |
| 127 | |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 128 | void writeDeterminantHack(const Expression& mat); |
| 129 | |
| 130 | void writeInverseHack(const Expression& mat); |
| 131 | |
| 132 | void writeTransposeHack(const Expression& mat); |
| 133 | |
| 134 | void writeInverseSqrtHack(const Expression& x); |
| 135 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 136 | virtual void writeFunctionCall(const FunctionCall& c); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 137 | |
John Stiles | 9568023 | 2021-04-22 15:05:20 -0400 | [diff] [blame^] | 138 | virtual void writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence); |
John Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 139 | |
John Stiles | 9568023 | 2021-04-22 15:05:20 -0400 | [diff] [blame^] | 140 | virtual void writeCastConstructor(const AnyConstructor& c, Precedence parentPrecedence); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 141 | |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 142 | virtual void writeFieldAccess(const FieldAccess& f); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 143 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 144 | virtual void writeSwizzle(const Swizzle& swizzle); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 145 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 146 | virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
John Stiles | 933043b | 2021-03-31 15:23:16 -0400 | [diff] [blame] | 147 | |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 148 | void writeShortCircuitWorkaroundExpression(const BinaryExpression& b, |
| 149 | Precedence parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 150 | |
John Stiles | 9568023 | 2021-04-22 15:05:20 -0400 | [diff] [blame^] | 151 | virtual void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 152 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 153 | virtual void writeIndexExpression(const IndexExpression& expr); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 154 | |
| 155 | void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 156 | |
| 157 | void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 158 | |
| 159 | void writeBoolLiteral(const BoolLiteral& b); |
| 160 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 161 | virtual void writeIntLiteral(const IntLiteral& i); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 162 | |
| 163 | void writeFloatLiteral(const FloatLiteral& f); |
| 164 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 165 | virtual void writeSetting(const Setting& s); |
| 166 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 167 | void writeStatement(const Statement& s); |
| 168 | |
| 169 | void writeBlock(const Block& b); |
| 170 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 171 | virtual void writeIfStatement(const IfStatement& stmt); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 172 | |
| 173 | void writeForStatement(const ForStatement& f); |
| 174 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 175 | void writeDoStatement(const DoStatement& d); |
| 176 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 177 | virtual void writeSwitchStatement(const SwitchStatement& s); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 178 | |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 179 | virtual void writeReturnStatement(const ReturnStatement& r); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 180 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 181 | virtual void writeProgramElement(const ProgramElement& e); |
| 182 | |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 183 | const ShaderCapsClass& caps() const { return fContext.fCaps; } |
| 184 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 185 | const char* fLineEnding; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 186 | const Context& fContext; |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 187 | StringStream fExtensions; |
| 188 | StringStream fGlobals; |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 189 | StringStream fExtraFunctions; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 190 | String fFunctionHeader; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 191 | int fVarCount = 0; |
| 192 | int fIndentation = 0; |
| 193 | bool fAtLineStart = false; |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 194 | std::set<String> fWrittenIntrinsics; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 195 | // true if we have run into usages of dFdx / dFdy |
| 196 | bool fFoundDerivatives = false; |
Brian Osman | 4b2f915 | 2018-04-17 11:19:57 -0400 | [diff] [blame] | 197 | bool fFoundExternalSamplerDecl = false; |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 198 | bool fFoundRectSamplerDecl = false; |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 199 | bool fFoundGSInvocations = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 200 | bool fSetupFragPositionGlobal = false; |
| 201 | bool fSetupFragPositionLocal = false; |
Brian Salomon | dba65f9 | 2018-01-22 08:43:38 -0500 | [diff] [blame] | 202 | bool fSetupFragCoordWorkaround = false; |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 203 | // if non-empty, replace all texture / texture2D / textureProj / etc. calls with this name |
| 204 | String fTextureFunctionOverride; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 205 | |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 206 | // We map function names to function class so we can quickly deal with function calls that need |
| 207 | // extra processing |
| 208 | enum class FunctionClass { |
Adrienne Walker | 92b161f | 2018-08-22 10:41:52 -0700 | [diff] [blame] | 209 | kAbs, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 210 | kAtan, |
| 211 | kDeterminant, |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 212 | kDFdx, |
| 213 | kDFdy, |
| 214 | kFwidth, |
Chris Dalton | a708618 | 2018-11-16 09:33:43 -0500 | [diff] [blame] | 215 | kFMA, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 216 | kFract, |
| 217 | kInverse, |
| 218 | kInverseSqrt, |
| 219 | kMin, |
Adrienne Walker | 2f4c09b | 2018-08-22 16:04:57 -0700 | [diff] [blame] | 220 | kPow, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 221 | kSaturate, |
| 222 | kTexture, |
Ethan Nicholas | 5a9a9b8 | 2019-09-20 12:59:22 -0400 | [diff] [blame] | 223 | kTranspose |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 224 | }; |
| 225 | static std::unordered_map<StringFragment, FunctionClass>* fFunctionClasses; |
| 226 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 227 | using INHERITED = CodeGenerator; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 228 | }; |
| 229 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 230 | } // namespace SkSL |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 231 | |
| 232 | #endif |