egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 8 | #ifndef GrGLSLVarying_DEFINED |
| 9 | #define GrGLSLVarying_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/GrTypesPriv.h" |
Michael Ludwig | c1ed11d | 2021-08-24 11:49:55 -0400 | [diff] [blame] | 12 | #include "src/core/SkTBlockList.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrShaderVar.h" |
| 14 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 15 | |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 16 | class GrGeometryProcessor; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 17 | class GrGLSLProgramBuilder; |
| 18 | |
Ethan Nicholas | 56b4e3d | 2019-01-08 09:34:40 -0500 | [diff] [blame] | 19 | #ifdef SK_DEBUG |
| 20 | static bool is_matrix(GrSLType type) { |
| 21 | switch (type) { |
| 22 | case kFloat2x2_GrSLType: |
| 23 | case kFloat3x3_GrSLType: |
| 24 | case kFloat4x4_GrSLType: |
| 25 | case kHalf2x2_GrSLType: |
| 26 | case kHalf3x3_GrSLType: |
| 27 | case kHalf4x4_GrSLType: |
| 28 | return true; |
| 29 | default: |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | #endif |
| 34 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 35 | class GrGLSLVarying { |
| 36 | public: |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 37 | enum class Scope { |
| 38 | kVertToFrag, |
| 39 | kVertToGeo, |
| 40 | kGeoToFrag |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 43 | GrGLSLVarying() = default; |
Ethan Nicholas | 56b4e3d | 2019-01-08 09:34:40 -0500 | [diff] [blame] | 44 | GrGLSLVarying(GrSLType type, Scope scope = Scope::kVertToFrag) |
| 45 | : fType(type) |
| 46 | , fScope(scope) { |
| 47 | // Metal doesn't support varying matrices, so we disallow them everywhere for consistency |
| 48 | SkASSERT(!is_matrix(type)); |
| 49 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 50 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 51 | void reset(GrSLType type, Scope scope = Scope::kVertToFrag) { |
Ethan Nicholas | 56b4e3d | 2019-01-08 09:34:40 -0500 | [diff] [blame] | 52 | // Metal doesn't support varying matrices, so we disallow them everywhere for consistency |
| 53 | SkASSERT(!is_matrix(type)); |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 54 | *this = GrGLSLVarying(); |
| 55 | fType = type; |
| 56 | fScope = scope; |
| 57 | } |
| 58 | |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 59 | GrSLType type() const { return fType; } |
| 60 | Scope scope() const { return fScope; } |
| 61 | bool isInVertexShader() const { return Scope::kGeoToFrag != fScope; } |
| 62 | bool isInFragmentShader() const { return Scope::kVertToGeo != fScope; } |
| 63 | |
| 64 | const char* vsOut() const { SkASSERT(this->isInVertexShader()); return fVsOut; } |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 65 | const char* fsIn() const { SkASSERT(this->isInFragmentShader()); return fFsIn; } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 66 | |
Brian Salomon | 6b7ec00 | 2021-07-12 13:54:24 -0400 | [diff] [blame] | 67 | GrShaderVar vsOutVar() const { |
| 68 | SkASSERT(this->isInVertexShader()); |
| 69 | return GrShaderVar(this->vsOut(), fType, GrShaderVar::TypeModifier::Out); |
| 70 | } |
| 71 | |
| 72 | GrShaderVar fsInVar() const { |
| 73 | SkASSERT(this->isInFragmentShader()); |
| 74 | return GrShaderVar(this->fsIn(), fType, GrShaderVar::TypeModifier::In); |
| 75 | } |
| 76 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 77 | private: |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 78 | GrSLType fType = kVoid_GrSLType; |
| 79 | Scope fScope = Scope::kVertToFrag; |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 80 | const char* fVsOut = nullptr; |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 81 | const char* fFsIn = nullptr; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 82 | |
| 83 | friend class GrGLSLVaryingHandler; |
| 84 | }; |
| 85 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 86 | static const int kVaryingsPerBlock = 8; |
| 87 | |
| 88 | class GrGLSLVaryingHandler { |
| 89 | public: |
| 90 | explicit GrGLSLVaryingHandler(GrGLSLProgramBuilder* program) |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 91 | : fVaryings(kVaryingsPerBlock) |
| 92 | , fVertexInputs(kVaryingsPerBlock) |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 93 | , fVertexOutputs(kVaryingsPerBlock) |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 94 | , fFragInputs(kVaryingsPerBlock) |
| 95 | , fFragOutputs(kVaryingsPerBlock) |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 96 | , fProgramBuilder(program) |
| 97 | , fDefaultInterpolationModifier(nullptr) {} |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 98 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 99 | virtual ~GrGLSLVaryingHandler() {} |
| 100 | |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 101 | /** |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 102 | * Notifies the varying handler that this shader will never emit geometry in perspective and |
| 103 | * therefore does not require perspective-correct interpolation. When supported, this allows |
| 104 | * varyings to use the "noperspective" keyword, which means the GPU can use cheaper math for |
| 105 | * interpolation. |
| 106 | */ |
| 107 | void setNoPerspective(); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 108 | |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 109 | enum class Interpolation { |
| 110 | kInterpolated, |
| 111 | kCanBeFlat, // Use "flat" if it will be faster. |
| 112 | kMustBeFlat // Use "flat" even if it is known to be slow. |
| 113 | }; |
| 114 | |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 115 | /** |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 116 | * addVarying allows fine grained control for setting up varyings between stages. Calling this |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 117 | * function will make sure all necessary decls are setup for the client. The client however is |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 118 | * responsible for setting up all shader code (e.g "vOut = vIn;") If you just need to take an |
| 119 | * attribute and pass it through to an output value in a fragment shader, use |
| 120 | * addPassThroughAttribute. |
| 121 | * TODO convert most uses of addVarying to addPassThroughAttribute |
| 122 | */ |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 123 | void addVarying(const char* name, GrGLSLVarying* varying, |
| 124 | Interpolation = Interpolation::kInterpolated); |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 125 | |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 126 | /** |
| 127 | * The GP can use these calls to pass a vertex shader variable directly to 'output' in the |
| 128 | * fragment shader. Though this adds code to vertex and fragment stages, 'output' is expected to |
Brian Osman | 99ddd2a | 2021-08-27 11:21:12 -0400 | [diff] [blame] | 129 | * be defined in the fragment shader before the call is made. |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 130 | * TODO it might be nicer behavior to have a flag to declare output inside these calls |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 131 | */ |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 132 | void addPassThroughAttribute(const GrShaderVar& vsVar, |
| 133 | const char* output, |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 134 | Interpolation = Interpolation::kInterpolated); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 135 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 136 | void emitAttributes(const GrGeometryProcessor&); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 137 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 138 | // This should be called once all attributes and varyings have been added to the |
| 139 | // GrGLSLVaryingHanlder and before getting/adding any of the declarations to the shaders. |
| 140 | void finalize(); |
| 141 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 142 | void getVertexDecls(SkString* inputDecls, SkString* outputDecls) const; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 143 | void getFragDecls(SkString* inputDecls, SkString* outputDecls) const; |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 144 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 145 | protected: |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 146 | struct VaryingInfo { |
| 147 | GrSLType fType; |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 148 | bool fIsFlat; |
| 149 | SkString fVsOut; |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 150 | GrShaderFlags fVisibility; |
| 151 | }; |
| 152 | |
Michael Ludwig | c1ed11d | 2021-08-24 11:49:55 -0400 | [diff] [blame] | 153 | typedef SkTBlockList<VaryingInfo> VaryingList; |
| 154 | typedef SkTBlockList<GrShaderVar> VarArray; |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 155 | |
| 156 | VaryingList fVaryings; |
| 157 | VarArray fVertexInputs; |
| 158 | VarArray fVertexOutputs; |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 159 | VarArray fFragInputs; |
| 160 | VarArray fFragOutputs; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 161 | |
| 162 | // This is not owned by the class |
| 163 | GrGLSLProgramBuilder* fProgramBuilder; |
| 164 | |
| 165 | private: |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 166 | void addAttribute(const GrShaderVar& var); |
| 167 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 168 | virtual void onFinalize() = 0; |
| 169 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 170 | // helper function for get*Decls |
| 171 | void appendDecls(const VarArray& vars, SkString* out) const; |
| 172 | |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 173 | const char* fDefaultInterpolationModifier; |
| 174 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 175 | friend class GrGLSLProgramBuilder; |
| 176 | }; |
| 177 | |
| 178 | #endif |