1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/gl/GrGLProgramEffects.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,325 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef GrGLProgramEffects_DEFINED 1.12 +#define GrGLProgramEffects_DEFINED 1.13 + 1.14 +#include "GrBackendEffectFactory.h" 1.15 +#include "GrTexture.h" 1.16 +#include "GrTextureAccess.h" 1.17 +#include "GrGLUniformManager.h" 1.18 + 1.19 +class GrEffectStage; 1.20 +class GrGLVertexProgramEffectsBuilder; 1.21 +class GrGLShaderBuilder; 1.22 +class GrGLFullShaderBuilder; 1.23 +class GrGLFragmentOnlyShaderBuilder; 1.24 + 1.25 +/** 1.26 + * This class encapsulates an array of GrGLEffects and their supporting data (coord transforms 1.27 + * and textures). It is built with GrGLProgramEffectsBuilder, then used to manage the necessary GL 1.28 + * state and shader uniforms. 1.29 + */ 1.30 +class GrGLProgramEffects { 1.31 +public: 1.32 + typedef GrBackendEffectFactory::EffectKey EffectKey; 1.33 + typedef GrGLUniformManager::UniformHandle UniformHandle; 1.34 + 1.35 + /** 1.36 + * These methods generate different portions of an effect's final key. 1.37 + */ 1.38 + static EffectKey GenAttribKey(const GrDrawEffect&); 1.39 + static EffectKey GenTransformKey(const GrDrawEffect&); 1.40 + static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&); 1.41 + 1.42 + virtual ~GrGLProgramEffects(); 1.43 + 1.44 + /** 1.45 + * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writes the next 1.46 + * available unit to *texUnitIdx when it returns. 1.47 + */ 1.48 + void initSamplers(const GrGLUniformManager&, int* texUnitIdx); 1.49 + 1.50 + /** 1.51 + * Calls setData() on each effect, and sets their transformation matrices and texture bindings. 1.52 + */ 1.53 + virtual void setData(GrGpuGL*, 1.54 + const GrGLUniformManager&, 1.55 + const GrEffectStage* effectStages[]) = 0; 1.56 + 1.57 + /** 1.58 + * Passed to GrGLEffects so they can add transformed coordinates to their shader code. 1.59 + */ 1.60 + class TransformedCoords { 1.61 + public: 1.62 + TransformedCoords(const SkString& name, GrSLType type) 1.63 + : fName(name), fType(type) { 1.64 + } 1.65 + 1.66 + const char* c_str() const { return fName.c_str(); } 1.67 + GrSLType type() const { return fType; } 1.68 + const SkString& getName() const { return fName; } 1.69 + 1.70 + private: 1.71 + SkString fName; 1.72 + GrSLType fType; 1.73 + }; 1.74 + 1.75 + typedef SkTArray<TransformedCoords> TransformedCoordsArray; 1.76 + 1.77 + /** 1.78 + * Passed to GrGLEffects so they can add texture reads to their shader code. 1.79 + */ 1.80 + class TextureSampler { 1.81 + public: 1.82 + TextureSampler(UniformHandle uniform, const GrTextureAccess& access) 1.83 + : fSamplerUniform(uniform) 1.84 + , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) { 1.85 + SkASSERT(0 != fConfigComponentMask); 1.86 + memcpy(fSwizzle, access.getSwizzle(), 5); 1.87 + } 1.88 + 1.89 + UniformHandle samplerUniform() const { return fSamplerUniform; } 1.90 + // bitfield of GrColorComponentFlags present in the texture's config. 1.91 + uint32_t configComponentMask() const { return fConfigComponentMask; } 1.92 + const char* swizzle() const { return fSwizzle; } 1.93 + 1.94 + private: 1.95 + UniformHandle fSamplerUniform; 1.96 + uint32_t fConfigComponentMask; 1.97 + char fSwizzle[5]; 1.98 + }; 1.99 + 1.100 + typedef SkTArray<TextureSampler> TextureSamplerArray; 1.101 + 1.102 +protected: 1.103 + GrGLProgramEffects(int reserveCount) 1.104 + : fGLEffects(reserveCount) 1.105 + , fSamplers(reserveCount) { 1.106 + } 1.107 + 1.108 + /** 1.109 + * Helper for emitEffect() in a subclasses. Emits uniforms for an effect's texture accesses and 1.110 + * appends the necessary data to the TextureSamplerArray* object so effects can add texture 1.111 + * lookups to their code. This method is only meant to be called during the construction phase. 1.112 + */ 1.113 + void emitSamplers(GrGLShaderBuilder*, const GrEffectRef&, TextureSamplerArray*); 1.114 + 1.115 + /** 1.116 + * Helper for setData(). Binds all the textures for an effect. 1.117 + */ 1.118 + void bindTextures(GrGpuGL*, const GrEffectRef&, int effectIdx); 1.119 + 1.120 + struct Sampler { 1.121 + SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) 1.122 + UniformHandle fUniform; 1.123 + int fTextureUnit; 1.124 + }; 1.125 + 1.126 + SkTArray<GrGLEffect*> fGLEffects; 1.127 + SkTArray<SkSTArray<4, Sampler, true> > fSamplers; 1.128 +}; 1.129 + 1.130 +/** 1.131 + * This is an abstract base class for constructing different types of GrGLProgramEffects objects. 1.132 + */ 1.133 +class GrGLProgramEffectsBuilder { 1.134 +public: 1.135 + virtual ~GrGLProgramEffectsBuilder() { } 1.136 + 1.137 + /** 1.138 + * Emits the effect's shader code, and stores the necessary uniforms internally. 1.139 + */ 1.140 + virtual void emitEffect(const GrEffectStage&, 1.141 + GrGLProgramEffects::EffectKey, 1.142 + const char* outColor, 1.143 + const char* inColor, 1.144 + int stageIndex) = 0; 1.145 +}; 1.146 + 1.147 +//////////////////////////////////////////////////////////////////////////////// 1.148 + 1.149 +/** 1.150 + * This is a GrGLProgramEffects implementation that does coord transforms with the vertex shader. 1.151 + */ 1.152 +class GrGLVertexProgramEffects : public GrGLProgramEffects { 1.153 +public: 1.154 + virtual void setData(GrGpuGL*, 1.155 + const GrGLUniformManager&, 1.156 + const GrEffectStage* effectStages[]) SK_OVERRIDE; 1.157 + 1.158 +private: 1.159 + friend class GrGLVertexProgramEffectsBuilder; 1.160 + 1.161 + GrGLVertexProgramEffects(int reserveCount, bool explicitLocalCoords) 1.162 + : INHERITED(reserveCount) 1.163 + , fTransforms(reserveCount) 1.164 + , fHasExplicitLocalCoords(explicitLocalCoords) { 1.165 + } 1.166 + 1.167 + /** 1.168 + * Helper for GrGLProgramEffectsBuilder::emitEfffect(). This method is meant to only be called 1.169 + * during the construction phase. 1.170 + */ 1.171 + void emitEffect(GrGLFullShaderBuilder*, 1.172 + const GrEffectStage&, 1.173 + GrGLProgramEffects::EffectKey, 1.174 + const char* outColor, 1.175 + const char* inColor, 1.176 + int stageIndex); 1.177 + 1.178 + /** 1.179 + * Helper for emitEffect(). Emits any attributes an effect may have. 1.180 + */ 1.181 + void emitAttributes(GrGLFullShaderBuilder*, const GrEffectStage&); 1.182 + 1.183 + /** 1.184 + * Helper for emitEffect(). Emits code to implement an effect's coord transforms in the VS. 1.185 + * Varyings are added as an outputs of the VS and inputs to the FS. The varyings may be either a 1.186 + * vec2f or vec3f depending upon whether perspective interpolation is required or not. The names 1.187 + * of the varyings in the VS and FS as well their types are appended to the 1.188 + * TransformedCoordsArray* object, which is in turn passed to the effect's emitCode() function. 1.189 + */ 1.190 + void emitTransforms(GrGLFullShaderBuilder*, 1.191 + const GrEffectRef&, 1.192 + EffectKey, 1.193 + TransformedCoordsArray*); 1.194 + 1.195 + /** 1.196 + * Helper for setData(). Sets all the transform matrices for an effect. 1.197 + */ 1.198 + void setTransformData(const GrGLUniformManager&, const GrDrawEffect&, int effectIdx); 1.199 + 1.200 + struct Transform { 1.201 + Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); } 1.202 + UniformHandle fHandle; 1.203 + GrSLType fType; 1.204 + SkMatrix fCurrentValue; 1.205 + }; 1.206 + 1.207 + SkTArray<SkSTArray<2, Transform, true> > fTransforms; 1.208 + bool fHasExplicitLocalCoords; 1.209 + 1.210 + typedef GrGLProgramEffects INHERITED; 1.211 +}; 1.212 + 1.213 +/** 1.214 + * This class is used to construct a GrGLVertexProgramEffects* object. 1.215 + */ 1.216 +class GrGLVertexProgramEffectsBuilder : public GrGLProgramEffectsBuilder { 1.217 +public: 1.218 + GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder*, int reserveCount); 1.219 + virtual ~GrGLVertexProgramEffectsBuilder() { } 1.220 + 1.221 + virtual void emitEffect(const GrEffectStage&, 1.222 + GrGLProgramEffects::EffectKey, 1.223 + const char* outColor, 1.224 + const char* inColor, 1.225 + int stageIndex) SK_OVERRIDE; 1.226 + 1.227 + /** 1.228 + * Finalizes the building process and returns the effect array. After this call, the builder 1.229 + * becomes invalid. 1.230 + */ 1.231 + GrGLProgramEffects* finish() { return fProgramEffects.detach(); } 1.232 + 1.233 +private: 1.234 + GrGLFullShaderBuilder* fBuilder; 1.235 + SkAutoTDelete<GrGLVertexProgramEffects> fProgramEffects; 1.236 + 1.237 + typedef GrGLProgramEffectsBuilder INHERITED; 1.238 +}; 1.239 + 1.240 +//////////////////////////////////////////////////////////////////////////////// 1.241 + 1.242 +/** 1.243 + * This is a GrGLProgramEffects implementation that does coord transforms with the the built-in GL 1.244 + * TexGen functionality. 1.245 + */ 1.246 +class GrGLTexGenProgramEffects : public GrGLProgramEffects { 1.247 +public: 1.248 + virtual void setData(GrGpuGL*, 1.249 + const GrGLUniformManager&, 1.250 + const GrEffectStage* effectStages[]) SK_OVERRIDE; 1.251 + 1.252 +private: 1.253 + friend class GrGLTexGenProgramEffectsBuilder; 1.254 + 1.255 + GrGLTexGenProgramEffects(int reserveCount) 1.256 + : INHERITED(reserveCount) 1.257 + , fTransforms(reserveCount) { 1.258 + } 1.259 + 1.260 + /** 1.261 + * Helper for GrGLProgramEffectsBuilder::emitEfffect(). This method is meant to only be called 1.262 + * during the construction phase. 1.263 + */ 1.264 + void emitEffect(GrGLFragmentOnlyShaderBuilder*, 1.265 + const GrEffectStage&, 1.266 + GrGLProgramEffects::EffectKey, 1.267 + const char* outColor, 1.268 + const char* inColor, 1.269 + int stageIndex); 1.270 + 1.271 + /** 1.272 + * Helper for emitEffect(). Allocates texture units from the builder for each transform in an 1.273 + * effect. The transforms all use adjacent texture units. They either use two or three of the 1.274 + * coordinates at a given texture unit, depending on if they need perspective interpolation. 1.275 + * The expressions to access the transformed coords (i.e. 'vec2(gl_TexCoord[0])') as well as the 1.276 + * types are appended to the TransformedCoordsArray* object, which is in turn passed to the 1.277 + * effect's emitCode() function. 1.278 + */ 1.279 + void setupTexGen(GrGLFragmentOnlyShaderBuilder*, 1.280 + const GrEffectRef&, 1.281 + EffectKey, 1.282 + TransformedCoordsArray*); 1.283 + 1.284 + /** 1.285 + * Helper for setData(). Sets the TexGen state for each transform in an effect. 1.286 + */ 1.287 + void setTexGenState(GrGpuGL*, const GrDrawEffect&, int effectIdx); 1.288 + 1.289 + struct Transforms { 1.290 + Transforms(EffectKey transformKey, int texCoordIndex) 1.291 + : fTransformKey(transformKey), fTexCoordIndex(texCoordIndex) {} 1.292 + EffectKey fTransformKey; 1.293 + int fTexCoordIndex; 1.294 + }; 1.295 + 1.296 + SkTArray<Transforms> fTransforms; 1.297 + 1.298 + typedef GrGLProgramEffects INHERITED; 1.299 +}; 1.300 + 1.301 +/** 1.302 + * This class is used to construct a GrGLTexGenProgramEffects* object. 1.303 + */ 1.304 +class GrGLTexGenProgramEffectsBuilder : public GrGLProgramEffectsBuilder { 1.305 +public: 1.306 + GrGLTexGenProgramEffectsBuilder(GrGLFragmentOnlyShaderBuilder*, int reserveCount); 1.307 + virtual ~GrGLTexGenProgramEffectsBuilder() { } 1.308 + 1.309 + virtual void emitEffect(const GrEffectStage&, 1.310 + GrGLProgramEffects::EffectKey, 1.311 + const char* outColor, 1.312 + const char* inColor, 1.313 + int stageIndex) SK_OVERRIDE; 1.314 + 1.315 + /** 1.316 + * Finalizes the building process and returns the effect array. After this call, the builder 1.317 + * becomes invalid. 1.318 + */ 1.319 + GrGLProgramEffects* finish() { return fProgramEffects.detach(); } 1.320 + 1.321 +private: 1.322 + GrGLFragmentOnlyShaderBuilder* fBuilder; 1.323 + SkAutoTDelete<GrGLTexGenProgramEffects> fProgramEffects; 1.324 + 1.325 + typedef GrGLProgramEffectsBuilder INHERITED; 1.326 +}; 1.327 + 1.328 +#endif