| 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 GrGLProgramEffects_DEFINED | 8 #ifndef GrGLProgramEffects_DEFINED |
| 9 #define GrGLProgramEffects_DEFINED | 9 #define GrGLProgramEffects_DEFINED |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 /** | 24 /** |
| 25 * This class encapsulates an array of GrGLEffects and their supporting data (co
ord transforms | 25 * This class encapsulates an array of GrGLEffects and their supporting data (co
ord transforms |
| 26 * and textures). It is built with GrGLProgramEffectsBuilder, then used to manag
e the necessary GL | 26 * and textures). It is built with GrGLProgramEffectsBuilder, then used to manag
e the necessary GL |
| 27 * state and shader uniforms. | 27 * state and shader uniforms. |
| 28 */ | 28 */ |
| 29 class GrGLProgramEffects : public SkRefCnt { | 29 class GrGLProgramEffects : public SkRefCnt { |
| 30 public: | 30 public: |
| 31 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 31 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 32 typedef GrGLProgramDataManager::VaryingHandle VaryingHandle; | 32 typedef GrGLProgramDataManager::VaryingHandle VaryingHandle; |
| 33 | 33 |
| 34 /** | |
| 35 * This class emits some of the code inserted into the shaders for an effect
. The code it | |
| 36 * creates may be dependent on properties of the effect that the effect itse
lf doesn't use | |
| 37 * in its key (e.g. the pixel format of textures used). So this class insert
s a meta-key for | |
| 38 * every effect using this function. It is also responsible for inserting th
e effect's class ID | |
| 39 * which must be different for every GrEffect subclass. It can fail if an ef
fect uses too many | |
| 40 * textures, attributes, etc for the space allotted in the meta-key. | |
| 41 */ | |
| 42 static bool GenEffectMetaKey(const GrEffectStage&, | |
| 43 bool, | |
| 44 const GrGLCaps&, | |
| 45 GrEffectKeyBuilder*); | |
| 46 | |
| 47 virtual ~GrGLProgramEffects(); | 34 virtual ~GrGLProgramEffects(); |
| 48 | 35 |
| 49 /** | 36 /** |
| 50 * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writ
es the next | 37 * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writ
es the next |
| 51 * available unit to *texUnitIdx when it returns. | 38 * available unit to *texUnitIdx when it returns. |
| 52 */ | 39 */ |
| 53 void initSamplers(const GrGLProgramDataManager&, int* texUnitIdx); | 40 void initSamplers(const GrGLProgramDataManager&, int* texUnitIdx); |
| 54 | 41 |
| 55 /** | 42 /** |
| 56 * Calls setData() on each effect, and sets their transformation matrices an
d texture bindings. | 43 * Calls setData() on each effect, and sets their transformation matrices an
d texture bindings. |
| 57 */ | 44 */ |
| 58 virtual void setData(GrGpuGL*, | 45 virtual void setData(GrGpuGL*, |
| 59 GrGpu::DrawType, | 46 GrGpu::DrawType, |
| 60 const GrGLProgramDataManager&, | 47 const GrGLProgramDataManager&, |
| 61 const GrEffectStage* effectStages[]) = 0; | 48 const GrEffectStage* effectStages[]) = 0; |
| 62 | 49 |
| 63 virtual void setData(GrGpuGL*, | |
| 64 GrGpu::DrawType, | |
| 65 const GrGLProgramDataManager&, | |
| 66 const GrEffectStage* effectStages) { SkFAIL("DO NOT USE
"); } | |
| 67 | |
| 68 void addEffect(GrGLEffect* effect) { fGLEffects.push_back(effect); } | |
| 69 | |
| 70 /** | |
| 71 * Passed to GrGLEffects so they can add transformed coordinates to their sh
ader code. | |
| 72 */ | |
| 73 class TransformedCoords { | |
| 74 public: | |
| 75 TransformedCoords(const SkString& name, GrSLType type) | |
| 76 : fName(name), fType(type) { | |
| 77 } | |
| 78 | |
| 79 const char* c_str() const { return fName.c_str(); } | |
| 80 GrSLType type() const { return fType; } | |
| 81 const SkString& getName() const { return fName; } | |
| 82 | |
| 83 private: | |
| 84 SkString fName; | |
| 85 GrSLType fType; | |
| 86 }; | |
| 87 | |
| 88 typedef SkTArray<TransformedCoords> TransformedCoordsArray; | |
| 89 | |
| 90 /** | |
| 91 * Passed to GrGLEffects so they can add texture reads to their shader code. | |
| 92 */ | |
| 93 class TextureSampler { | |
| 94 public: | |
| 95 TextureSampler(UniformHandle uniform, const GrTextureAccess& access) | |
| 96 : fSamplerUniform(uniform) | |
| 97 , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture(
)->config())) { | |
| 98 SkASSERT(0 != fConfigComponentMask); | |
| 99 memcpy(fSwizzle, access.getSwizzle(), 5); | |
| 100 } | |
| 101 | |
| 102 UniformHandle samplerUniform() const { return fSamplerUniform; } | |
| 103 // bitfield of GrColorComponentFlags present in the texture's config. | |
| 104 uint32_t configComponentMask() const { return fConfigComponentMask; } | |
| 105 const char* swizzle() const { return fSwizzle; } | |
| 106 | |
| 107 private: | |
| 108 UniformHandle fSamplerUniform; | |
| 109 uint32_t fConfigComponentMask; | |
| 110 char fSwizzle[5]; | |
| 111 }; | |
| 112 | |
| 113 typedef SkTArray<TextureSampler> TextureSamplerArray; | |
| 114 | |
| 115 protected: | 50 protected: |
| 116 | |
| 117 /** | |
| 118 * Helpers for GenEffectMetaKey. | |
| 119 */ | |
| 120 static uint32_t GenAttribKey(const GrEffect*); | |
| 121 static uint32_t GenTransformKey(const GrEffectStage&, bool useExplicitLocalC
oords); | |
| 122 static uint32_t GenTextureKey(const GrEffect*, const GrGLCaps&); | |
| 123 | |
| 124 GrGLProgramEffects(int reserveCount) | 51 GrGLProgramEffects(int reserveCount) |
| 125 : fGLEffects(reserveCount) | 52 : fGLEffects(reserveCount) |
| 126 , fSamplers(reserveCount) { | 53 , fSamplers(reserveCount) { |
| 127 } | 54 } |
| 128 | 55 |
| 129 /** | 56 /** |
| 130 * Helper for emitEffect() in a subclasses. Emits uniforms for an effect's t
exture accesses and | |
| 131 * appends the necessary data to the TextureSamplerArray* object so effects
can add texture | |
| 132 * lookups to their code. This method is only meant to be called during the
construction phase. | |
| 133 */ | |
| 134 void emitSamplers(GrGLProgramBuilder*, const GrEffect&, TextureSamplerArray*
); | |
| 135 | |
| 136 /** | |
| 137 * Helper for setData(). Binds all the textures for an effect. | 57 * Helper for setData(). Binds all the textures for an effect. |
| 138 */ | 58 */ |
| 139 void bindTextures(GrGpuGL*, const GrEffect&, int effectIdx); | 59 void bindTextures(GrGpuGL*, const GrEffect&, int effectIdx); |
| 140 | 60 |
| 141 struct Sampler { | 61 struct Sampler { |
| 142 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) | 62 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) |
| 143 UniformHandle fUniform; | 63 UniformHandle fUniform; |
| 144 int fTextureUnit; | 64 int fTextureUnit; |
| 145 }; | 65 }; |
| 146 | 66 |
| 67 /* |
| 68 * Helpers for shader builders to build up program effects objects alongside
shader code |
| 69 */ |
| 70 void addEffect(GrGLEffect* effect) { fGLEffects.push_back(effect); } |
| 71 SkTArray<Sampler, true>& addSamplers() { return fSamplers.push_back(); } |
| 72 |
| 147 SkTArray<GrGLEffect*> fGLEffects; | 73 SkTArray<GrGLEffect*> fGLEffects; |
| 148 SkTArray<SkSTArray<4, Sampler, true> > fSamplers; | 74 SkTArray<SkSTArray<4, Sampler, true> > fSamplers; |
| 149 | 75 |
| 150 private: | 76 private: |
| 77 friend class GrGLProgramBuilder; |
| 78 friend class GrGLFullProgramBuilder; |
| 79 friend class GrGLFragmentOnlyShaderBuilder; |
| 80 |
| 151 typedef SkRefCnt INHERITED; | 81 typedef SkRefCnt INHERITED; |
| 152 }; | 82 }; |
| 153 | 83 |
| 154 /** | |
| 155 * This is an abstract base class for constructing different types of GrGLProgra
mEffects objects. | |
| 156 */ | |
| 157 class GrGLProgramEffectsBuilder { | |
| 158 public: | |
| 159 virtual ~GrGLProgramEffectsBuilder() { } | |
| 160 /** | |
| 161 * Emits the effect's shader code, and stores the necessary uniforms interna
lly. | |
| 162 */ | |
| 163 virtual void emitEffect(const GrEffectStage&, | |
| 164 const GrEffectKey&, | |
| 165 const char* outColor, | |
| 166 const char* inColor, | |
| 167 int stageIndex) = 0; | |
| 168 }; | |
| 169 | |
| 170 //////////////////////////////////////////////////////////////////////////////// | 84 //////////////////////////////////////////////////////////////////////////////// |
| 171 | 85 |
| 172 /** | 86 /** |
| 173 * This is a GrGLProgramEffects implementation that does coord transforms with t
he vertex shader. | 87 * This is a GrGLProgramEffects implementation that does coord transforms with t
he vertex shader. |
| 174 */ | 88 */ |
| 175 class GrGLVertexProgramEffects : public GrGLProgramEffects { | 89 class GrGLVertexProgramEffects : public GrGLProgramEffects { |
| 176 public: | 90 public: |
| 177 virtual void setData(GrGpuGL*, | 91 virtual void setData(GrGpuGL*, |
| 178 GrGpu::DrawType, | 92 GrGpu::DrawType, |
| 179 const GrGLProgramDataManager&, | 93 const GrGLProgramDataManager&, |
| 180 const GrEffectStage* effectStages[]) SK_OVERRIDE; | 94 const GrEffectStage* effectStages[]) SK_OVERRIDE; |
| 181 | 95 |
| 182 virtual void setData(GrGpuGL*, | |
| 183 GrGpu::DrawType, | |
| 184 const GrGLProgramDataManager&, | |
| 185 const GrEffectStage* effectStages) SK_OVERRIDE; | |
| 186 | |
| 187 private: | 96 private: |
| 188 friend class GrGLFullProgramBuilder; | |
| 189 | |
| 190 GrGLVertexProgramEffects(int reserveCount, bool explicitLocalCoords) | 97 GrGLVertexProgramEffects(int reserveCount, bool explicitLocalCoords) |
| 191 : INHERITED(reserveCount) | 98 : INHERITED(reserveCount) |
| 192 , fTransforms(reserveCount) | 99 , fTransforms(reserveCount) |
| 193 , fHasExplicitLocalCoords(explicitLocalCoords) { | 100 , fHasExplicitLocalCoords(explicitLocalCoords) { |
| 194 } | 101 } |
| 195 /** | |
| 196 * This method is meant to only be called during the construction phase. | |
| 197 */ | |
| 198 void emitEffect(GrGLFullProgramBuilder*, | |
| 199 const GrEffectStage&, | |
| 200 const GrEffectKey&, | |
| 201 const char* outColor, | |
| 202 const char* inColor, | |
| 203 int stageIndex); | |
| 204 | |
| 205 /** | |
| 206 * Helper for emitEffect(). Emits any attributes an effect may have. | |
| 207 */ | |
| 208 void emitAttributes(GrGLFullProgramBuilder*, const GrEffectStage&); | |
| 209 | |
| 210 /** | |
| 211 * Helper for emitEffect(). Emits code to implement an effect's coord transf
orms in the VS. | |
| 212 * Varyings are added as an outputs of the VS and inputs to the FS. The vary
ings may be either a | |
| 213 * vec2f or vec3f depending upon whether perspective interpolation is requir
ed or not. The names | |
| 214 * of the varyings in the VS and FS as well their types are appended to the | |
| 215 * TransformedCoordsArray* object, which is in turn passed to the effect's e
mitCode() function. | |
| 216 */ | |
| 217 void emitTransforms(GrGLFullProgramBuilder*, | |
| 218 const GrEffectStage&, | |
| 219 TransformedCoordsArray*); | |
| 220 | |
| 221 /** | |
| 222 * Helper for setData(). Sets all the transform matrices for an effect. | |
| 223 */ | |
| 224 void setTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&, const GrE
ffectStage&, | |
| 225 int effectIdx); | |
| 226 void setPathTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&, const
GrEffectStage&, | |
| 227 int effectIdx); | |
| 228 | 102 |
| 229 struct Transform { | 103 struct Transform { |
| 230 Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); } | 104 Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); } |
| 231 UniformHandle fHandle; | 105 UniformHandle fHandle; |
| 232 SkMatrix fCurrentValue; | 106 SkMatrix fCurrentValue; |
| 233 }; | 107 }; |
| 234 | 108 |
| 235 struct PathTransform { | 109 struct PathTransform { |
| 236 PathTransform() { fCurrentValue = SkMatrix::InvalidMatrix(); } | 110 PathTransform() { fCurrentValue = SkMatrix::InvalidMatrix(); } |
| 237 VaryingHandle fHandle; | 111 VaryingHandle fHandle; |
| 238 SkMatrix fCurrentValue; | 112 SkMatrix fCurrentValue; |
| 239 GrSLType fType; | 113 GrSLType fType; |
| 240 }; | 114 }; |
| 241 | 115 |
| 116 /* |
| 117 * These functions are used by the builders to build up program effects alon
g side the shader |
| 118 * code itself |
| 119 */ |
| 120 SkSTArray<2, Transform, true>& addTransforms() { return fTransforms.push_bac
k(); } |
| 121 SkTArray<PathTransform, true>& addPathTransforms() { return fPathTransforms.
push_back(); } |
| 122 |
| 123 /** |
| 124 * Helper for setData(). Sets all the transform matrices for an effect. |
| 125 */ |
| 126 void setTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&, const GrE
ffectStage&, |
| 127 int effectIdx); |
| 128 void setPathTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&, const
GrEffectStage&, |
| 129 int effectIdx); |
| 130 |
| 242 SkTArray<SkSTArray<2, Transform, true> > fTransforms; | 131 SkTArray<SkSTArray<2, Transform, true> > fTransforms; |
| 243 SkTArray<SkTArray<PathTransform, true> > fPathTransforms; | 132 SkTArray<SkTArray<PathTransform, true> > fPathTransforms; |
| 244 bool fHasExplicitLocalCoords; | 133 bool fHasExplicitLocalCoords; |
| 245 | 134 |
| 246 friend class GrGLVertexProgramEffectsBuilder; | 135 friend class GrGLFullProgramBuilder; |
| 247 | 136 |
| 248 typedef GrGLProgramEffects INHERITED; | 137 typedef GrGLProgramEffects INHERITED; |
| 249 }; | 138 }; |
| 250 | 139 |
| 251 /** | |
| 252 * This class is used to construct a GrGLVertexProgramEffects* object. | |
| 253 */ | |
| 254 class GrGLVertexProgramEffectsBuilder : public GrGLProgramEffectsBuilder { | |
| 255 public: | |
| 256 GrGLVertexProgramEffectsBuilder(GrGLFullProgramBuilder*, int reserveCount); | |
| 257 virtual ~GrGLVertexProgramEffectsBuilder() { } | |
| 258 virtual void emitEffect(const GrEffectStage&, | |
| 259 const GrEffectKey&, | |
| 260 const char* outColor, | |
| 261 const char* inColor, | |
| 262 int stageIndex) SK_OVERRIDE; | |
| 263 /** | |
| 264 * Finalizes the building process and returns the effect array. After this c
all, the builder | |
| 265 * becomes invalid. | |
| 266 */ | |
| 267 GrGLProgramEffects* finish() { return fProgramEffects.detach(); } | |
| 268 private: | |
| 269 GrGLFullProgramBuilder* fBuilder; | |
| 270 SkAutoTDelete<GrGLVertexProgramEffects> fProgramEffects; | |
| 271 typedef GrGLProgramEffectsBuilder INHERITED; | |
| 272 }; | |
| 273 | |
| 274 //////////////////////////////////////////////////////////////////////////////// | 140 //////////////////////////////////////////////////////////////////////////////// |
| 275 | 141 |
| 276 /** | 142 /** |
| 277 * This is a GrGLProgramEffects implementation that does coord transforms with | 143 * This is a GrGLProgramEffects implementation that does coord transforms with |
| 278 * the the NV_path_rendering PathTexGen functionality. | 144 * the the NV_path_rendering PathTexGen functionality. |
| 279 */ | 145 */ |
| 280 class GrGLPathTexGenProgramEffects : public GrGLProgramEffects { | 146 class GrGLPathTexGenProgramEffects : public GrGLProgramEffects { |
| 281 public: | 147 public: |
| 282 virtual void setData(GrGpuGL*, | 148 virtual void setData(GrGpuGL*, |
| 283 GrGpu::DrawType, | 149 GrGpu::DrawType, |
| 284 const GrGLProgramDataManager&, | 150 const GrGLProgramDataManager&, |
| 285 const GrEffectStage* effectStages[]) SK_OVERRIDE; | 151 const GrEffectStage* effectStages[]) SK_OVERRIDE; |
| 286 | 152 |
| 287 private: | 153 private: |
| 288 friend class GrGLFragmentOnlyProgramBuilder; | |
| 289 | |
| 290 GrGLPathTexGenProgramEffects(int reserveCount) | 154 GrGLPathTexGenProgramEffects(int reserveCount) |
| 291 : INHERITED(reserveCount) | 155 : INHERITED(reserveCount) |
| 292 , fTransforms(reserveCount) { | 156 , fTransforms(reserveCount) { |
| 293 } | 157 } |
| 294 | 158 |
| 295 /** | 159 /** |
| 296 * This method is meant to only be called during the construction phase. | |
| 297 */ | |
| 298 void emitEffect(GrGLFragmentOnlyProgramBuilder*, | |
| 299 const GrEffectStage&, | |
| 300 const GrEffectKey&, | |
| 301 const char* outColor, | |
| 302 const char* inColor, | |
| 303 int stageIndex); | |
| 304 | |
| 305 /** | |
| 306 * Helper for emitEffect(). Allocates texture units from the builder for eac
h transform in an | |
| 307 * effect. The transforms all use adjacent texture units. They either use tw
o or three of the | |
| 308 * coordinates at a given texture unit, depending on if they need perspectiv
e interpolation. | |
| 309 * The expressions to access the transformed coords (i.e. 'vec2(gl_TexCoord[
0])') as well as the | |
| 310 * types are appended to the TransformedCoordsArray* object, which is in tur
n passed to the | |
| 311 * effect's emitCode() function. | |
| 312 */ | |
| 313 void setupPathTexGen(GrGLFragmentOnlyProgramBuilder*, | |
| 314 const GrEffectStage&, | |
| 315 TransformedCoordsArray*); | |
| 316 | |
| 317 /** | |
| 318 * Helper for setData(). Sets the PathTexGen state for each transform in an
effect. | 160 * Helper for setData(). Sets the PathTexGen state for each transform in an
effect. |
| 319 */ | 161 */ |
| 320 void setPathTexGenState(GrGpuGL*, const GrEffectStage&, int effectIdx); | 162 void setPathTexGenState(GrGpuGL*, const GrEffectStage&, int effectIdx); |
| 321 | 163 |
| 322 struct Transforms { | 164 struct Transforms { |
| 323 Transforms(uint32_t transformKey, int texCoordIndex) | 165 Transforms(int texCoordIndex) |
| 324 : fTransformKey(transformKey), fTexCoordIndex(texCoordIndex) {} | 166 : fTexCoordIndex(texCoordIndex) {} |
| 325 uint32_t fTransformKey; | 167 int fTexCoordIndex; |
| 326 int fTexCoordIndex; | |
| 327 }; | 168 }; |
| 328 | 169 |
| 170 /* |
| 171 * Helper for fragment only shader builder to build up the program effects a
longside the shader |
| 172 */ |
| 173 void addTransforms(int coordIndex) { |
| 174 fTransforms.push_back(Transforms(coordIndex)); |
| 175 } |
| 176 |
| 329 SkTArray<Transforms> fTransforms; | 177 SkTArray<Transforms> fTransforms; |
| 330 | 178 |
| 331 friend class GrGLPathTexGenProgramEffectsBuilder; | 179 friend class GrGLFragmentOnlyProgramBuilder; |
| 180 |
| 332 typedef GrGLProgramEffects INHERITED; | 181 typedef GrGLProgramEffects INHERITED; |
| 333 }; | 182 }; |
| 334 | 183 |
| 335 /** | |
| 336 * This class is used to construct a GrGLPathTexGenProgramEffects* object. | |
| 337 */ | |
| 338 class GrGLPathTexGenProgramEffectsBuilder : public GrGLProgramEffectsBuilder { | |
| 339 public: | |
| 340 GrGLPathTexGenProgramEffectsBuilder(GrGLFragmentOnlyProgramBuilder*, int res
erveCount); | |
| 341 virtual ~GrGLPathTexGenProgramEffectsBuilder() { } | |
| 342 virtual void emitEffect(const GrEffectStage&, | |
| 343 const GrEffectKey&, | |
| 344 const char* outColor, | |
| 345 const char* inColor, | |
| 346 int stageIndex) SK_OVERRIDE; | |
| 347 /** | |
| 348 * Finalizes the building process and returns the effect array. After this c
all, the builder | |
| 349 * becomes invalid. | |
| 350 */ | |
| 351 GrGLProgramEffects* finish() { return fProgramEffects.detach(); } | |
| 352 private: | |
| 353 GrGLFragmentOnlyProgramBuilder* fBuilder; | |
| 354 SkAutoTDelete<GrGLPathTexGenProgramEffects> fProgramEffects; | |
| 355 typedef GrGLProgramEffectsBuilder INHERITED; | |
| 356 }; | |
| 357 | |
| 358 | |
| 359 #endif | 184 #endif |
| OLD | NEW |