| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrGLEffectArray_DEFINED | 8 #ifndef GrGLEffectArray_DEFINED |
| 9 #define GrGLEffectArray_DEFINED | 9 #define GrGLEffectArray_DEFINED |
| 10 | 10 |
| 11 #include "GrBackendEffectFactory.h" | 11 #include "GrBackendEffectFactory.h" |
| 12 #include "GrTexture.h" | 12 #include "GrTexture.h" |
| 13 #include "GrTextureAccess.h" | 13 #include "GrTextureAccess.h" |
| 14 #include "GrGLUniformManager.h" | 14 #include "GrGLUniformManager.h" |
| 15 | 15 |
| 16 class GrEffectStage; | 16 class GrEffectStage; |
| 17 class GrGLEffectArrayBuilder; | 17 class GrGLEffectArrayBuilder; |
| 18 class GrGLVertexEffectArrayBuilder; |
| 18 class GrGLShaderBuilder; | 19 class GrGLShaderBuilder; |
| 20 class GrGLFullShaderBuilder; |
| 19 | 21 |
| 20 /** | 22 /** |
| 21 * This class encapsulates an array of GrGLEffects and their supporting data (co
ord transforms | 23 * This class encapsulates an array of GrGLEffects and their supporting data (co
ord transforms |
| 22 * and textures). It is built with GrGLEffectArrayBuilder, then used to manage t
he necessary GL | 24 * and textures). It is built with GrGLEffectArrayBuilder, then used to manage t
he necessary GL |
| 23 * state and shader uniforms. | 25 * state and shader uniforms. |
| 24 */ | 26 */ |
| 25 class GrGLEffectArray { | 27 class GrGLEffectArray { |
| 26 public: | 28 public: |
| 27 typedef GrBackendEffectFactory::EffectKey EffectKey; | 29 typedef GrBackendEffectFactory::EffectKey EffectKey; |
| 28 typedef GrGLUniformManager::UniformHandle UniformHandle; | 30 typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 29 | 31 |
| 30 /** | 32 /** |
| 31 * These methods generate different portions of an effect's final key. | 33 * These methods generate different portions of an effect's final key. |
| 32 */ | 34 */ |
| 33 static EffectKey GenAttribKey(const GrDrawEffect&); | 35 static EffectKey GenAttribKey(const GrDrawEffect&); |
| 34 static EffectKey GenTransformKey(const GrDrawEffect&); | 36 static EffectKey GenTransformKey(const GrDrawEffect&); |
| 35 static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&); | 37 static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&); |
| 36 | 38 |
| 37 ~GrGLEffectArray() { fGLEffects.deleteAll(); } | 39 virtual ~GrGLEffectArray() { fGLEffects.deleteAll(); } |
| 38 | 40 |
| 39 /** | 41 /** |
| 40 * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writ
es the next | 42 * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writ
es the next |
| 41 * available unit to *texUnitIdx when it returns. | 43 * available unit to *texUnitIdx when it returns. |
| 42 */ | 44 */ |
| 43 void initSamplers(const GrGLUniformManager&, int* texUnitIdx); | 45 void initSamplers(const GrGLUniformManager&, int* texUnitIdx); |
| 44 | 46 |
| 45 /** | 47 /** |
| 46 * Calls setData() on each effect, and sets their transformation matrices an
d texture bindings. | 48 * Calls setData() on each effect, and sets their transformation matrices an
d texture bindings. |
| 47 */ | 49 */ |
| 48 void setData(GrGpuGL*, | 50 virtual void setData(GrGpuGL*, |
| 49 const GrGLUniformManager&, | 51 const GrGLUniformManager&, |
| 50 const GrEffectStage* effectStages[]); | 52 const GrEffectStage* effectStages[]) = 0; |
| 51 | 53 |
| 52 /** | 54 /** |
| 53 * Passed to GrGLEffects so they can add transformed coordinates to their sh
ader code. | 55 * Passed to GrGLEffects so they can add transformed coordinates to their sh
ader code. |
| 54 */ | 56 */ |
| 55 class TransformedCoords { | 57 class TransformedCoords { |
| 56 public: | 58 public: |
| 57 TransformedCoords(const char* name, GrSLType type, const char* vsName) | 59 TransformedCoords(const char* name, GrSLType type, const char* vsName) |
| 58 : fName(name), fType(type), fVSName(vsName) { | 60 : fName(name), fType(type), fVSName(vsName) { |
| 59 } | 61 } |
| 60 | 62 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 90 const char* swizzle() const { return fSwizzle; } | 92 const char* swizzle() const { return fSwizzle; } |
| 91 | 93 |
| 92 private: | 94 private: |
| 93 UniformHandle fSamplerUniform; | 95 UniformHandle fSamplerUniform; |
| 94 uint32_t fConfigComponentMask; | 96 uint32_t fConfigComponentMask; |
| 95 char fSwizzle[5]; | 97 char fSwizzle[5]; |
| 96 }; | 98 }; |
| 97 | 99 |
| 98 typedef SkTArray<TextureSampler> TextureSamplerArray; | 100 typedef SkTArray<TextureSampler> TextureSamplerArray; |
| 99 | 101 |
| 100 private: | 102 protected: |
| 101 friend class GrGLEffectArrayBuilder; | 103 friend class GrGLEffectArrayBuilder; |
| 102 | 104 |
| 103 GrGLEffectArray(int reserveCount, bool explicitLocalCoords) | 105 GrGLEffectArray(int reserveCount) : fSamplers(reserveCount) {} |
| 104 : fTransforms(reserveCount) | 106 |
| 105 , fSamplers(reserveCount) | 107 /** |
| 108 * Helper for setData(). Binds all the textures for an effect. |
| 109 */ |
| 110 void bindTextures(GrGpuGL*, const GrEffectRef&, int effectIdx); |
| 111 |
| 112 struct Sampler { |
| 113 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) |
| 114 UniformHandle fUniform; |
| 115 int fTextureUnit; |
| 116 }; |
| 117 |
| 118 SkTDArray<GrGLEffect*> fGLEffects; |
| 119 SkTArray<SkSTArray<4, Sampler, true> > fSamplers; |
| 120 }; |
| 121 |
| 122 /** |
| 123 * This is a GrGLEffectArray implementation that does coord transforms with the
vertex shader. |
| 124 */ |
| 125 class GrGLVertexEffectArray : public GrGLEffectArray { |
| 126 public: |
| 127 /** |
| 128 * Calls setData() on each effect, and sets their transformation matrices an
d texture bindings. |
| 129 */ |
| 130 virtual void setData(GrGpuGL*, |
| 131 const GrGLUniformManager&, |
| 132 const GrEffectStage* effectStages[]) SK_OVERRIDE; |
| 133 |
| 134 private: |
| 135 friend class GrGLVertexEffectArrayBuilder; |
| 136 |
| 137 GrGLVertexEffectArray(int reserveCount, bool explicitLocalCoords) |
| 138 : INHERITED(reserveCount) |
| 139 , fTransforms(reserveCount) |
| 106 , fHasExplicitLocalCoords(explicitLocalCoords) { | 140 , fHasExplicitLocalCoords(explicitLocalCoords) { |
| 107 } | 141 } |
| 108 | 142 |
| 109 /** | 143 /** |
| 110 * Helper for setData(). Sets all the transform matrices for an effect. | 144 * Helper for setData(). Sets all the transform matrices for an effect. |
| 111 */ | 145 */ |
| 112 void setTransformData(const GrGLUniformManager&, const GrDrawEffect&, int ef
fectIdx); | 146 void setTransformData(const GrGLUniformManager&, const GrDrawEffect&, int ef
fectIdx); |
| 113 | 147 |
| 114 /** | |
| 115 * Helper for setData(). Binds all the textures for an effect. | |
| 116 */ | |
| 117 void bindTextures(GrGpuGL*, const GrEffectRef&, int effectIdx); | |
| 118 | |
| 119 struct Transform { | 148 struct Transform { |
| 120 Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); } | 149 Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); } |
| 121 UniformHandle fHandle; | 150 UniformHandle fHandle; |
| 122 GrSLType fType; | 151 GrSLType fType; |
| 123 SkMatrix fCurrentValue; | 152 SkMatrix fCurrentValue; |
| 124 }; | 153 }; |
| 125 | 154 |
| 126 struct Sampler { | 155 SkTArray<SkSTArray<2, Transform, true> > fTransforms; |
| 127 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) | 156 bool fHasExplicitLocalCoords; |
| 128 UniformHandle fUniform; | |
| 129 int fTextureUnit; | |
| 130 }; | |
| 131 | 157 |
| 132 SkTDArray<GrGLEffect*> fGLEffects; | 158 typedef GrGLEffectArray INHERITED; |
| 133 SkTArray<SkSTArray<2, Transform, true> > fTransforms; | |
| 134 SkTArray<SkSTArray<4, Sampler, true> > fSamplers; | |
| 135 bool fHasExplicitLocalCoords; | |
| 136 }; | 159 }; |
| 137 | 160 |
| 138 //////////////////////////////////////////////////////////////////////////////// | 161 //////////////////////////////////////////////////////////////////////////////// |
| 139 | 162 |
| 140 /** | 163 /** |
| 141 * This class is used to construct a GrGLEffectArray. | 164 * This class is used to construct a GrGLEffectArray. |
| 142 */ | 165 */ |
| 143 class GrGLEffectArrayBuilder { | 166 class GrGLEffectArrayBuilder { |
| 144 public: | 167 public: |
| 145 GrGLEffectArrayBuilder(GrGLShaderBuilder* builder, int reserveCount); | |
| 146 | |
| 147 /** | 168 /** |
| 148 * Emits the effect's shader code, and stores the necessary uniforms interna
lly. | 169 * Emits the effect's shader code, and stores the necessary uniforms interna
lly. |
| 149 */ | 170 */ |
| 150 void emitEffect(const GrEffectStage&, | 171 virtual void emitEffect(const GrEffectStage&, |
| 151 GrGLEffectArray::EffectKey, | 172 GrGLEffectArray::EffectKey, |
| 152 const char* outColor, | 173 const char* outColor, |
| 153 const char* inColor, | 174 const char* inColor, |
| 154 int stageIndex); | 175 int stageIndex) = 0; |
| 176 |
| 177 protected: |
| 178 /** |
| 179 * Helper for emitEffect(). Emits uniforms for an effect's texture accesses
and appends the |
| 180 * necessary data to the TextureSamplerArray* object so effects can add text
ure lookups. |
| 181 */ |
| 182 static void emitSamplers(GrGLShaderBuilder*, |
| 183 GrGLEffectArray*, |
| 184 const GrEffectRef&, |
| 185 GrGLEffectArray::TextureSamplerArray*); |
| 186 }; |
| 187 |
| 188 /** |
| 189 * This class is used by GrGLFullShaderBuilder to construct a GrGLVertexEffectAr
ray. |
| 190 */ |
| 191 class GrGLVertexEffectArrayBuilder : public GrGLEffectArrayBuilder { |
| 192 public: |
| 193 GrGLVertexEffectArrayBuilder(GrGLFullShaderBuilder*, int reserveCount); |
| 194 |
| 195 virtual void emitEffect(const GrEffectStage&, |
| 196 GrGLEffectArray::EffectKey, |
| 197 const char* outColor, |
| 198 const char* inColor, |
| 199 int stageIndex) SK_OVERRIDE; |
| 155 | 200 |
| 156 /** | 201 /** |
| 157 * Finalizes the building process and returns the effect array. After this c
all, the builder | 202 * Finalizes the building process and returns the effect array. After this c
all, the builder |
| 158 * becomes invalid. | 203 * becomes invalid. |
| 159 */ | 204 */ |
| 160 GrGLEffectArray* finish() { return fEffectArray.detach(); } | 205 GrGLEffectArray* finish() { return fEffectArray.detach(); } |
| 161 | 206 |
| 162 private: | 207 private: |
| 163 /** | 208 /** |
| 164 * Helper for emitEffect(). Emits any attributes an effect might have. | 209 * Helper for emitEffect(). Emits any attributes an effect might have. |
| 165 */ | 210 */ |
| 166 void emitAttributes(const GrEffectStage&); | 211 void emitAttributes(const GrEffectStage&); |
| 167 | 212 |
| 168 /** | 213 /** |
| 169 * Helper for emitEffect(). Emits code to implement an effect's coord transf
orms in the VS. | 214 * Helper for emitEffect(). Emits code to implement an effect's coord transf
orms in the VS. |
| 170 * Varyings are added as an outputs of the VS and inputs to the FS. The vary
ings may be either a | 215 * Varyings are added as an outputs of the VS and inputs to the FS. The vary
ings may be either a |
| 171 * vec2f or vec3f depending upon whether perspective interpolation is requir
ed or not. The names | 216 * vec2f or vec3f depending upon whether perspective interpolation is requir
ed or not. The names |
| 172 * of the varyings in the VS and FS as well their types are appended to the | 217 * of the varyings in the VS and FS as well their types are appended to the |
| 173 * TransformedCoordsArray* object, which is in turn passed to the effect's e
mitCode() function. | 218 * TransformedCoordsArray* object, which is in turn passed to the effect's e
mitCode() function. |
| 174 */ | 219 */ |
| 175 void emitTransforms(const GrEffectRef&, | 220 void emitTransforms(const GrEffectRef&, |
| 176 GrGLEffectArray::EffectKey, | 221 GrGLEffectArray::EffectKey, |
| 177 GrGLEffectArray::TransformedCoordsArray*); | 222 GrGLEffectArray::TransformedCoordsArray*); |
| 178 | 223 |
| 179 /** | 224 GrGLFullShaderBuilder* fBuilder; |
| 180 * Helper for emitEffect(). Emits uniforms for an effect's texture accesses.
The uniform info | 225 SkAutoTDelete<GrGLVertexEffectArray> fEffectArray; |
| 181 * as well as texture access parameters are appended to the TextureSamplerAr
ray* object, which | |
| 182 * is in turn passed to the effect's emitCode() function. | |
| 183 */ | |
| 184 void emitSamplers(const GrEffectRef&, | |
| 185 GrGLEffectArray::TextureSamplerArray*); | |
| 186 | 226 |
| 187 GrGLShaderBuilder* fBuilder; | 227 typedef GrGLEffectArrayBuilder INHERITED; |
| 188 SkAutoTDelete<GrGLEffectArray> fEffectArray; | |
| 189 }; | 228 }; |
| 190 | 229 |
| 191 #endif | 230 #endif |
| OLD | NEW |