commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 GrGLProgramEffects_DEFINED |
| 9 | #define GrGLProgramEffects_DEFINED |
| 10 | |
| 11 | #include "GrBackendEffectFactory.h" |
| 12 | #include "GrTexture.h" |
| 13 | #include "GrTextureAccess.h" |
| 14 | #include "GrGLUniformManager.h" |
| 15 | |
| 16 | class GrEffectStage; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 17 | class GrGLVertexProgramEffectsBuilder; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 18 | class GrGLShaderBuilder; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 19 | class GrGLFullShaderBuilder; |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 20 | class GrGLFragmentOnlyShaderBuilder; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * This class encapsulates an array of GrGLEffects and their supporting data (coord transforms |
| 24 | * and textures). It is built with GrGLProgramEffectsBuilder, then used to manage the necessary GL |
| 25 | * state and shader uniforms. |
| 26 | */ |
| 27 | class GrGLProgramEffects { |
| 28 | public: |
| 29 | typedef GrBackendEffectFactory::EffectKey EffectKey; |
| 30 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 31 | |
| 32 | /** |
| 33 | * These methods generate different portions of an effect's final key. |
| 34 | */ |
| 35 | static EffectKey GenAttribKey(const GrDrawEffect&); |
| 36 | static EffectKey GenTransformKey(const GrDrawEffect&); |
| 37 | static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&); |
| 38 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 39 | virtual ~GrGLProgramEffects(); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writes the next |
| 43 | * available unit to *texUnitIdx when it returns. |
| 44 | */ |
| 45 | void initSamplers(const GrGLUniformManager&, int* texUnitIdx); |
| 46 | |
| 47 | /** |
| 48 | * Calls setData() on each effect, and sets their transformation matrices and texture bindings. |
| 49 | */ |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 50 | virtual void setData(GrGpuGL*, |
| 51 | const GrGLUniformManager&, |
| 52 | const GrEffectStage* effectStages[]) = 0; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Passed to GrGLEffects so they can add transformed coordinates to their shader code. |
| 56 | */ |
| 57 | class TransformedCoords { |
| 58 | public: |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 59 | TransformedCoords(const SkString& name, GrSLType type) |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 60 | : fName(name), fType(type) { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | const char* c_str() const { return fName.c_str(); } |
| 64 | GrSLType type() const { return fType; } |
| 65 | const SkString& getName() const { return fName; } |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | SkString fName; |
| 69 | GrSLType fType; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | typedef SkTArray<TransformedCoords> TransformedCoordsArray; |
| 73 | |
| 74 | /** |
| 75 | * Passed to GrGLEffects so they can add texture reads to their shader code. |
| 76 | */ |
| 77 | class TextureSampler { |
| 78 | public: |
| 79 | TextureSampler(UniformHandle uniform, const GrTextureAccess& access) |
| 80 | : fSamplerUniform(uniform) |
| 81 | , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) { |
| 82 | SkASSERT(0 != fConfigComponentMask); |
| 83 | memcpy(fSwizzle, access.getSwizzle(), 5); |
| 84 | } |
| 85 | |
| 86 | UniformHandle samplerUniform() const { return fSamplerUniform; } |
| 87 | // bitfield of GrColorComponentFlags present in the texture's config. |
| 88 | uint32_t configComponentMask() const { return fConfigComponentMask; } |
| 89 | const char* swizzle() const { return fSwizzle; } |
| 90 | |
| 91 | private: |
| 92 | UniformHandle fSamplerUniform; |
| 93 | uint32_t fConfigComponentMask; |
| 94 | char fSwizzle[5]; |
| 95 | }; |
| 96 | |
| 97 | typedef SkTArray<TextureSampler> TextureSamplerArray; |
| 98 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 99 | protected: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 100 | GrGLProgramEffects(int reserveCount) |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 101 | : fGLEffects(reserveCount) |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 102 | , fSamplers(reserveCount) { |
| 103 | } |
| 104 | |
| 105 | /** |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 106 | * Helper for emitEffect() in a subclasses. Emits uniforms for an effect's texture accesses and |
| 107 | * appends the necessary data to the TextureSamplerArray* object so effects can add texture |
| 108 | * lookups to their code. This method is only meant to be called during the construction phase. |
| 109 | */ |
| 110 | void emitSamplers(GrGLShaderBuilder*, const GrEffectRef&, TextureSamplerArray*); |
| 111 | |
| 112 | /** |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 113 | * Helper for setData(). Binds all the textures for an effect. |
| 114 | */ |
| 115 | void bindTextures(GrGpuGL*, const GrEffectRef&, int effectIdx); |
| 116 | |
| 117 | struct Sampler { |
| 118 | SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) |
| 119 | UniformHandle fUniform; |
| 120 | int fTextureUnit; |
| 121 | }; |
| 122 | |
| 123 | SkTArray<GrGLEffect*> fGLEffects; |
| 124 | SkTArray<SkSTArray<4, Sampler, true> > fSamplers; |
| 125 | }; |
| 126 | |
| 127 | /** |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 128 | * This is an abstract base class for constructing different types of GrGLProgramEffects objects. |
| 129 | */ |
| 130 | class GrGLProgramEffectsBuilder { |
| 131 | public: |
| 132 | /** |
| 133 | * Emits the effect's shader code, and stores the necessary uniforms internally. |
| 134 | */ |
| 135 | virtual void emitEffect(const GrEffectStage&, |
| 136 | GrGLProgramEffects::EffectKey, |
| 137 | const char* outColor, |
| 138 | const char* inColor, |
| 139 | int stageIndex) = 0; |
| 140 | }; |
| 141 | |
| 142 | //////////////////////////////////////////////////////////////////////////////// |
| 143 | |
| 144 | /** |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 145 | * This is a GrGLProgramEffects implementation that does coord transforms with the vertex shader. |
| 146 | */ |
| 147 | class GrGLVertexProgramEffects : public GrGLProgramEffects { |
| 148 | public: |
| 149 | virtual void setData(GrGpuGL*, |
| 150 | const GrGLUniformManager&, |
| 151 | const GrEffectStage* effectStages[]) SK_OVERRIDE; |
| 152 | |
| 153 | private: |
| 154 | friend class GrGLVertexProgramEffectsBuilder; |
| 155 | |
| 156 | GrGLVertexProgramEffects(int reserveCount, bool explicitLocalCoords) |
| 157 | : INHERITED(reserveCount) |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 158 | , fTransforms(reserveCount) |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 159 | , fHasExplicitLocalCoords(explicitLocalCoords) { |
| 160 | } |
| 161 | |
| 162 | /** |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 163 | * Helper for GrGLProgramEffectsBuilder::emitEfffect(). This method is meant to only be called |
| 164 | * during the construction phase. |
| 165 | */ |
| 166 | void emitEffect(GrGLFullShaderBuilder*, |
| 167 | const GrEffectStage&, |
| 168 | GrGLProgramEffects::EffectKey, |
| 169 | const char* outColor, |
| 170 | const char* inColor, |
| 171 | int stageIndex); |
| 172 | |
| 173 | /** |
| 174 | * Helper for emitEffect(). Emits any attributes an effect may have. |
| 175 | */ |
| 176 | void emitAttributes(GrGLFullShaderBuilder*, const GrEffectStage&); |
| 177 | |
| 178 | /** |
| 179 | * Helper for emitEffect(). Emits code to implement an effect's coord transforms in the VS. |
| 180 | * Varyings are added as an outputs of the VS and inputs to the FS. The varyings may be either a |
| 181 | * vec2f or vec3f depending upon whether perspective interpolation is required or not. The names |
| 182 | * of the varyings in the VS and FS as well their types are appended to the |
| 183 | * TransformedCoordsArray* object, which is in turn passed to the effect's emitCode() function. |
| 184 | */ |
| 185 | void emitTransforms(GrGLFullShaderBuilder*, |
| 186 | const GrEffectRef&, |
| 187 | EffectKey, |
| 188 | TransformedCoordsArray*); |
| 189 | |
| 190 | /** |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 191 | * Helper for setData(). Sets all the transform matrices for an effect. |
| 192 | */ |
| 193 | void setTransformData(const GrGLUniformManager&, const GrDrawEffect&, int effectIdx); |
| 194 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 195 | struct Transform { |
| 196 | Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); } |
| 197 | UniformHandle fHandle; |
| 198 | GrSLType fType; |
| 199 | SkMatrix fCurrentValue; |
| 200 | }; |
| 201 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 202 | SkTArray<SkSTArray<2, Transform, true> > fTransforms; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 203 | bool fHasExplicitLocalCoords; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 204 | |
| 205 | typedef GrGLProgramEffects INHERITED; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 208 | /** |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 209 | * This class is used to construct a GrGLVertexProgramEffects* object. |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 210 | */ |
| 211 | class GrGLVertexProgramEffectsBuilder : public GrGLProgramEffectsBuilder { |
| 212 | public: |
| 213 | GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder*, int reserveCount); |
| 214 | |
| 215 | virtual void emitEffect(const GrEffectStage&, |
| 216 | GrGLProgramEffects::EffectKey, |
| 217 | const char* outColor, |
| 218 | const char* inColor, |
| 219 | int stageIndex) SK_OVERRIDE; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Finalizes the building process and returns the effect array. After this call, the builder |
| 223 | * becomes invalid. |
| 224 | */ |
| 225 | GrGLProgramEffects* finish() { return fProgramEffects.detach(); } |
| 226 | |
| 227 | private: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 228 | GrGLFullShaderBuilder* fBuilder; |
| 229 | SkAutoTDelete<GrGLVertexProgramEffects> fProgramEffects; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 230 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 231 | typedef GrGLProgramEffectsBuilder INHERITED; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 234 | //////////////////////////////////////////////////////////////////////////////// |
| 235 | |
| 236 | /** |
| 237 | * This is a GrGLProgramEffects implementation that does coord transforms with the the built-in GL |
| 238 | * TexGen functionality. |
| 239 | */ |
| 240 | class GrGLTexGenProgramEffects : public GrGLProgramEffects { |
| 241 | public: |
| 242 | virtual void setData(GrGpuGL*, |
| 243 | const GrGLUniformManager&, |
| 244 | const GrEffectStage* effectStages[]) SK_OVERRIDE; |
| 245 | |
| 246 | private: |
| 247 | friend class GrGLTexGenProgramEffectsBuilder; |
| 248 | |
| 249 | GrGLTexGenProgramEffects(int reserveCount) |
| 250 | : INHERITED(reserveCount) |
| 251 | , fTransforms(reserveCount) { |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Helper for GrGLProgramEffectsBuilder::emitEfffect(). This method is meant to only be called |
| 256 | * during the construction phase. |
| 257 | */ |
| 258 | void emitEffect(GrGLFragmentOnlyShaderBuilder*, |
| 259 | const GrEffectStage&, |
| 260 | GrGLProgramEffects::EffectKey, |
| 261 | const char* outColor, |
| 262 | const char* inColor, |
| 263 | int stageIndex); |
| 264 | |
| 265 | /** |
| 266 | * Helper for emitEffect(). Allocates texture units from the builder for each transform in an |
| 267 | * effect. The transforms all use adjacent texture units. They either use two or three of the |
| 268 | * coordinates at a given texture unit, depending on if they need perspective interpolation. |
| 269 | * The expressions to access the transformed coords (i.e. 'vec2(gl_TexCoord[0])') as well as the |
| 270 | * types are appended to the TransformedCoordsArray* object, which is in turn passed to the |
| 271 | * effect's emitCode() function. |
| 272 | */ |
| 273 | void setupTexGen(GrGLFragmentOnlyShaderBuilder*, |
| 274 | const GrEffectRef&, |
| 275 | EffectKey, |
| 276 | TransformedCoordsArray*); |
| 277 | |
| 278 | /** |
| 279 | * Helper for setData(). Sets the TexGen state for each transform in an effect. |
| 280 | */ |
| 281 | void setTexGenState(GrGpuGL*, const GrDrawEffect&, int effectIdx); |
| 282 | |
| 283 | struct Transforms { |
| 284 | Transforms(EffectKey transformKey, int texCoordIndex) |
| 285 | : fTransformKey(transformKey), fTexCoordIndex(texCoordIndex) {} |
| 286 | EffectKey fTransformKey; |
| 287 | int fTexCoordIndex; |
| 288 | }; |
| 289 | |
| 290 | SkTArray<Transforms> fTransforms; |
| 291 | |
| 292 | typedef GrGLProgramEffects INHERITED; |
| 293 | }; |
| 294 | |
| 295 | /** |
| 296 | * This class is used to construct a GrGLTexGenProgramEffects* object. |
| 297 | */ |
| 298 | class GrGLTexGenProgramEffectsBuilder : public GrGLProgramEffectsBuilder { |
| 299 | public: |
| 300 | GrGLTexGenProgramEffectsBuilder(GrGLFragmentOnlyShaderBuilder*, int reserveCount); |
| 301 | |
| 302 | virtual void emitEffect(const GrEffectStage&, |
| 303 | GrGLProgramEffects::EffectKey, |
| 304 | const char* outColor, |
| 305 | const char* inColor, |
| 306 | int stageIndex) SK_OVERRIDE; |
| 307 | |
| 308 | /** |
| 309 | * Finalizes the building process and returns the effect array. After this call, the builder |
| 310 | * becomes invalid. |
| 311 | */ |
| 312 | GrGLProgramEffects* finish() { return fProgramEffects.detach(); } |
| 313 | |
| 314 | private: |
| 315 | GrGLFragmentOnlyShaderBuilder* fBuilder; |
| 316 | SkAutoTDelete<GrGLTexGenProgramEffects> fProgramEffects; |
| 317 | |
| 318 | typedef GrGLProgramEffectsBuilder INHERITED; |
| 319 | }; |
| 320 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 321 | #endif |