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