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