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 | |
| 11 | #include "GrAllocator.h" |
| 12 | #include "GrGeometryProcessor.h" |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 13 | #include "GrShaderVar.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 14 | #include "GrTypesPriv.h" |
| 15 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 16 | |
| 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; } |
| 65 | const char* gsIn() const { return fGsIn; } |
| 66 | const char* gsOut() const { return fGsOut; } |
| 67 | const char* fsIn() const { SkASSERT(this->isInFragmentShader()); return fFsIn; } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 68 | |
| 69 | private: |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 70 | GrSLType fType = kVoid_GrSLType; |
| 71 | Scope fScope = Scope::kVertToFrag; |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 72 | const char* fVsOut = nullptr; |
| 73 | const char* fGsIn = nullptr; |
| 74 | const char* fGsOut = nullptr; |
| 75 | const char* fFsIn = nullptr; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 76 | |
| 77 | friend class GrGLSLVaryingHandler; |
| 78 | }; |
| 79 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 80 | static const int kVaryingsPerBlock = 8; |
| 81 | |
| 82 | class GrGLSLVaryingHandler { |
| 83 | public: |
| 84 | explicit GrGLSLVaryingHandler(GrGLSLProgramBuilder* program) |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 85 | : fVaryings(kVaryingsPerBlock) |
| 86 | , fVertexInputs(kVaryingsPerBlock) |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 87 | , fVertexOutputs(kVaryingsPerBlock) |
| 88 | , fGeomInputs(kVaryingsPerBlock) |
| 89 | , fGeomOutputs(kVaryingsPerBlock) |
| 90 | , fFragInputs(kVaryingsPerBlock) |
| 91 | , fFragOutputs(kVaryingsPerBlock) |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 92 | , fProgramBuilder(program) |
| 93 | , fDefaultInterpolationModifier(nullptr) {} |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 94 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 95 | virtual ~GrGLSLVaryingHandler() {} |
| 96 | |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 97 | /* |
| 98 | * Notifies the varying handler that this shader will never emit geometry in perspective and |
| 99 | * therefore does not require perspective-correct interpolation. When supported, this allows |
| 100 | * varyings to use the "noperspective" keyword, which means the GPU can use cheaper math for |
| 101 | * interpolation. |
| 102 | */ |
| 103 | void setNoPerspective(); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 104 | |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 105 | enum class Interpolation { |
| 106 | kInterpolated, |
| 107 | kCanBeFlat, // Use "flat" if it will be faster. |
| 108 | kMustBeFlat // Use "flat" even if it is known to be slow. |
| 109 | }; |
| 110 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 111 | /* |
| 112 | * 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] | 113 | * 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] | 114 | * responsible for setting up all shader code (e.g "vOut = vIn;") If you just need to take an |
| 115 | * attribute and pass it through to an output value in a fragment shader, use |
| 116 | * addPassThroughAttribute. |
| 117 | * TODO convert most uses of addVarying to addPassThroughAttribute |
| 118 | */ |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 119 | void addVarying(const char* name, GrGLSLVarying* varying, |
| 120 | Interpolation = Interpolation::kInterpolated); |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * The GP can use these calls to pass an attribute through all shaders directly to 'output' in |
| 124 | * the fragment shader. Though these calls affect both the vertex shader and fragment shader, |
| 125 | * they expect 'output' to be defined in the fragment shader before the call is made. If there |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 126 | * is a geometry shader, we will simply take the value of the varying from the first vertex and |
| 127 | * that will be set as the output varying for all emitted vertices. |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 128 | * 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] | 129 | */ |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 130 | void addPassThroughAttribute(const GrGeometryProcessor::Attribute&, const char* output, |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 131 | Interpolation = Interpolation::kInterpolated); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 132 | |
| 133 | void emitAttributes(const GrGeometryProcessor& gp); |
| 134 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 135 | // This should be called once all attributes and varyings have been added to the |
| 136 | // GrGLSLVaryingHanlder and before getting/adding any of the declarations to the shaders. |
| 137 | void finalize(); |
| 138 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 139 | void getVertexDecls(SkString* inputDecls, SkString* outputDecls) const; |
| 140 | void getGeomDecls(SkString* inputDecls, SkString* outputDecls) const; |
| 141 | void getFragDecls(SkString* inputDecls, SkString* outputDecls) const; |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 142 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 143 | protected: |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 144 | struct VaryingInfo { |
| 145 | GrSLType fType; |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 146 | bool fIsFlat; |
| 147 | SkString fVsOut; |
| 148 | SkString fGsOut; |
| 149 | GrShaderFlags fVisibility; |
| 150 | }; |
| 151 | |
| 152 | typedef GrTAllocator<VaryingInfo> VaryingList; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 153 | typedef GrTAllocator<GrShaderVar> VarArray; |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 154 | typedef GrGLSLProgramDataManager::VaryingHandle VaryingHandle; |
| 155 | |
| 156 | VaryingList fVaryings; |
| 157 | VarArray fVertexInputs; |
| 158 | VarArray fVertexOutputs; |
| 159 | VarArray fGeomInputs; |
| 160 | VarArray fGeomOutputs; |
| 161 | VarArray fFragInputs; |
| 162 | VarArray fFragOutputs; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 163 | |
| 164 | // This is not owned by the class |
| 165 | GrGLSLProgramBuilder* fProgramBuilder; |
| 166 | |
| 167 | private: |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 168 | void addAttribute(const GrShaderVar& var); |
| 169 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 170 | virtual void onFinalize() = 0; |
| 171 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 172 | // helper function for get*Decls |
| 173 | void appendDecls(const VarArray& vars, SkString* out) const; |
| 174 | |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 175 | const char* fDefaultInterpolationModifier; |
| 176 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 177 | friend class GrGLSLProgramBuilder; |
| 178 | }; |
| 179 | |
| 180 | #endif |