ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_PROGRAM |
| 9 | #define SKSL_PROGRAM |
| 10 | |
| 11 | #include <vector> |
| 12 | #include <memory> |
| 13 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 14 | #include "SkSLBoolLiteral.h" |
| 15 | #include "SkSLExpression.h" |
| 16 | #include "SkSLIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 17 | #include "SkSLModifiers.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 18 | #include "SkSLProgramElement.h" |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 19 | #include "SkSLSymbolTable.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 21 | // name of the render target height uniform |
| 22 | #define SKSL_RTHEIGHT_NAME "u_skRTHeight" |
| 23 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | namespace SkSL { |
| 25 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 26 | class Context; |
| 27 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Represents a fully-digested program, ready for code generation. |
| 30 | */ |
| 31 | struct Program { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 32 | struct Settings { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 33 | struct Value { |
| 34 | Value(bool b) |
| 35 | : fKind(kBool_Kind) |
| 36 | , fValue(b) {} |
| 37 | |
| 38 | Value(int i) |
| 39 | : fKind(kInt_Kind) |
| 40 | , fValue(i) {} |
| 41 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 42 | std::unique_ptr<Expression> literal(const Context& context, int offset) const { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 43 | switch (fKind) { |
| 44 | case Program::Settings::Value::kBool_Kind: |
| 45 | return std::unique_ptr<Expression>(new BoolLiteral(context, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 46 | offset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 47 | fValue)); |
| 48 | case Program::Settings::Value::kInt_Kind: |
| 49 | return std::unique_ptr<Expression>(new IntLiteral(context, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 50 | offset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 51 | fValue)); |
| 52 | default: |
| 53 | ASSERT(false); |
| 54 | return nullptr; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | enum { |
| 59 | kBool_Kind, |
| 60 | kInt_Kind, |
| 61 | } fKind; |
| 62 | |
| 63 | int fValue; |
| 64 | }; |
| 65 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 66 | #ifdef SKSL_STANDALONE |
| 67 | const StandaloneShaderCaps* fCaps = &standaloneCaps; |
| 68 | #else |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 69 | const GrShaderCaps* fCaps = nullptr; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 70 | #endif |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 71 | // if false, sk_FragCoord is exactly the same as gl_FragCoord. If true, the y coordinate |
| 72 | // must be flipped. |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 73 | bool fFlipY = false; |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 74 | // If true the destination fragment color is read sk_FragColor. It must be declared inout. |
| 75 | bool fFragColorIsInOut = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 76 | // if true, Setting objects (e.g. sk_Caps.fbFetchSupport) should be replaced with their |
| 77 | // constant equivalents during compilation |
| 78 | bool fReplaceSettings = true; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 79 | // if true, all halfs are forced to be floats |
| 80 | bool fForceHighPrecision = false; |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 81 | // if true, add -0.5 bias to LOD of all texture lookups |
| 82 | bool fSharpenTextures = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 83 | std::unordered_map<String, Value> fArgs; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct Inputs { |
| 87 | // if true, this program requires the render target height uniform to be defined |
| 88 | bool fRTHeight; |
| 89 | |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 90 | // if true, this program must be recompiled if the flipY setting changes. If false, the |
| 91 | // program will compile to the same code regardless of the flipY setting. |
| 92 | bool fFlipY; |
| 93 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 94 | void reset() { |
| 95 | fRTHeight = false; |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 96 | fFlipY = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool isEmpty() { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 100 | return !fRTHeight && !fFlipY; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 104 | enum Kind { |
| 105 | kFragment_Kind, |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 106 | kVertex_Kind, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 107 | kGeometry_Kind, |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 108 | kFragmentProcessor_Kind, |
| 109 | kCPU_Kind |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 112 | Program(Kind kind, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 113 | std::unique_ptr<String> source, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 114 | Settings settings, |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 115 | std::shared_ptr<Context> context, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 116 | std::vector<std::unique_ptr<ProgramElement>> elements, |
| 117 | std::shared_ptr<SymbolTable> symbols, |
| 118 | Inputs inputs) |
| 119 | : fKind(kind) |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 120 | , fSource(std::move(source)) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 121 | , fSettings(settings) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 122 | , fContext(context) |
Ethan Nicholas | 6415e0d | 2017-01-19 16:31:32 +0000 | [diff] [blame] | 123 | , fSymbols(symbols) |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 124 | , fElements(std::move(elements)) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 125 | , fInputs(inputs) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 126 | |
| 127 | Kind fKind; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 128 | std::unique_ptr<String> fSource; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 129 | Settings fSettings; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 130 | std::shared_ptr<Context> fContext; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 131 | // it's important to keep fElements defined after (and thus destroyed before) fSymbols, |
| 132 | // because destroying elements can modify reference counts in symbols |
Ethan Nicholas | 6415e0d | 2017-01-19 16:31:32 +0000 | [diff] [blame] | 133 | std::shared_ptr<SymbolTable> fSymbols; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 134 | std::vector<std::unique_ptr<ProgramElement>> fElements; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 135 | Inputs fInputs; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | } // namespace |
| 139 | |
| 140 | #endif |