Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef GrGLShaderBuilder_DEFINED |
michael@0 | 9 | #define GrGLShaderBuilder_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "GrAllocator.h" |
michael@0 | 12 | #include "GrBackendEffectFactory.h" |
michael@0 | 13 | #include "GrColor.h" |
michael@0 | 14 | #include "GrEffect.h" |
michael@0 | 15 | #include "SkTypes.h" |
michael@0 | 16 | #include "gl/GrGLProgramEffects.h" |
michael@0 | 17 | #include "gl/GrGLSL.h" |
michael@0 | 18 | #include "gl/GrGLUniformManager.h" |
michael@0 | 19 | |
michael@0 | 20 | #include <stdarg.h> |
michael@0 | 21 | |
michael@0 | 22 | class GrGLContextInfo; |
michael@0 | 23 | class GrEffectStage; |
michael@0 | 24 | class GrGLProgramDesc; |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | Contains all the incremental state of a shader as it is being built,as well as helpers to |
michael@0 | 28 | manipulate that state. |
michael@0 | 29 | */ |
michael@0 | 30 | class GrGLShaderBuilder { |
michael@0 | 31 | public: |
michael@0 | 32 | typedef GrTAllocator<GrGLShaderVar> VarArray; |
michael@0 | 33 | typedef GrBackendEffectFactory::EffectKey EffectKey; |
michael@0 | 34 | typedef GrGLProgramEffects::TextureSampler TextureSampler; |
michael@0 | 35 | typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; |
michael@0 | 36 | typedef GrGLUniformManager::BuilderUniform BuilderUniform; |
michael@0 | 37 | |
michael@0 | 38 | enum ShaderVisibility { |
michael@0 | 39 | kVertex_Visibility = 0x1, |
michael@0 | 40 | kGeometry_Visibility = 0x2, |
michael@0 | 41 | kFragment_Visibility = 0x4, |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | GrGLShaderBuilder(GrGpuGL*, GrGLUniformManager&, const GrGLProgramDesc&); |
michael@0 | 45 | virtual ~GrGLShaderBuilder() {} |
michael@0 | 46 | |
michael@0 | 47 | /** |
michael@0 | 48 | * Use of these features may require a GLSL extension to be enabled. Shaders may not compile |
michael@0 | 49 | * if code is added that uses one of these features without calling enableFeature() |
michael@0 | 50 | */ |
michael@0 | 51 | enum GLSLFeature { |
michael@0 | 52 | kStandardDerivatives_GLSLFeature = 0, |
michael@0 | 53 | |
michael@0 | 54 | kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
michael@0 | 55 | }; |
michael@0 | 56 | |
michael@0 | 57 | /** |
michael@0 | 58 | * If the feature is supported then true is returned and any necessary #extension declarations |
michael@0 | 59 | * are added to the shaders. If the feature is not supported then false will be returned. |
michael@0 | 60 | */ |
michael@0 | 61 | bool enableFeature(GLSLFeature); |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * Called by GrGLEffects to add code the fragment shader. |
michael@0 | 65 | */ |
michael@0 | 66 | void fsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
michael@0 | 67 | va_list args; |
michael@0 | 68 | va_start(args, format); |
michael@0 | 69 | fFSCode.appendVAList(format, args); |
michael@0 | 70 | va_end(args); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | void fsCodeAppend(const char* str) { fFSCode.append(str); } |
michael@0 | 74 | |
michael@0 | 75 | /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or |
michael@0 | 76 | Vec3f. The latter is interpreted as projective texture coords. The vec length and swizzle |
michael@0 | 77 | order of the result depends on the GrTextureAccess associated with the TextureSampler. */ |
michael@0 | 78 | void appendTextureLookup(SkString* out, |
michael@0 | 79 | const TextureSampler&, |
michael@0 | 80 | const char* coordName, |
michael@0 | 81 | GrSLType coordType = kVec2f_GrSLType) const; |
michael@0 | 82 | |
michael@0 | 83 | /** Version of above that appends the result to the fragment shader code instead.*/ |
michael@0 | 84 | void fsAppendTextureLookup(const TextureSampler&, |
michael@0 | 85 | const char* coordName, |
michael@0 | 86 | GrSLType coordType = kVec2f_GrSLType); |
michael@0 | 87 | |
michael@0 | 88 | |
michael@0 | 89 | /** Does the work of appendTextureLookup and modulates the result by modulation. The result is |
michael@0 | 90 | always a vec4. modulation and the swizzle specified by TextureSampler must both be vec4 or |
michael@0 | 91 | float. If modulation is "" or NULL it this function acts as though appendTextureLookup were |
michael@0 | 92 | called. */ |
michael@0 | 93 | void fsAppendTextureLookupAndModulate(const char* modulation, |
michael@0 | 94 | const TextureSampler&, |
michael@0 | 95 | const char* coordName, |
michael@0 | 96 | GrSLType coordType = kVec2f_GrSLType); |
michael@0 | 97 | |
michael@0 | 98 | /** Emits a helper function outside of main() in the fragment shader. */ |
michael@0 | 99 | void fsEmitFunction(GrSLType returnType, |
michael@0 | 100 | const char* name, |
michael@0 | 101 | int argCnt, |
michael@0 | 102 | const GrGLShaderVar* args, |
michael@0 | 103 | const char* body, |
michael@0 | 104 | SkString* outName); |
michael@0 | 105 | |
michael@0 | 106 | typedef uint8_t DstReadKey; |
michael@0 | 107 | typedef uint8_t FragPosKey; |
michael@0 | 108 | |
michael@0 | 109 | /** Returns a key for adding code to read the copy-of-dst color in service of effects that |
michael@0 | 110 | require reading the dst. It must not return 0 because 0 indicates that there is no dst |
michael@0 | 111 | copy read at all (in which case this function should not be called). */ |
michael@0 | 112 | static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
michael@0 | 113 | |
michael@0 | 114 | /** Returns a key for reading the fragment location. This should only be called if there is an |
michael@0 | 115 | effect that will requires the fragment position. If the fragment position is not required, |
michael@0 | 116 | the key is 0. */ |
michael@0 | 117 | static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&); |
michael@0 | 118 | |
michael@0 | 119 | /** If texture swizzling is available using tex parameters then it is preferred over mangling |
michael@0 | 120 | the generated shader code. This potentially allows greater reuse of cached shaders. */ |
michael@0 | 121 | static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps); |
michael@0 | 122 | |
michael@0 | 123 | /** Add a uniform variable to the current program, that has visibility in one or more shaders. |
michael@0 | 124 | visibility is a bitfield of ShaderVisibility values indicating from which shaders the |
michael@0 | 125 | uniform should be accessible. At least one bit must be set. Geometry shader uniforms are not |
michael@0 | 126 | supported at this time. The actual uniform name will be mangled. If outName is not NULL then |
michael@0 | 127 | it will refer to the final uniform name after return. Use the addUniformArray variant to add |
michael@0 | 128 | an array of uniforms. |
michael@0 | 129 | */ |
michael@0 | 130 | GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, |
michael@0 | 131 | GrSLType type, |
michael@0 | 132 | const char* name, |
michael@0 | 133 | const char** outName = NULL) { |
michael@0 | 134 | return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName); |
michael@0 | 135 | } |
michael@0 | 136 | GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, |
michael@0 | 137 | GrSLType type, |
michael@0 | 138 | const char* name, |
michael@0 | 139 | int arrayCount, |
michael@0 | 140 | const char** outName = NULL); |
michael@0 | 141 | |
michael@0 | 142 | const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle u) const { |
michael@0 | 143 | return fUniformManager.getBuilderUniform(fUniforms, u).fVariable; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | /** |
michael@0 | 147 | * Shortcut for getUniformVariable(u).c_str() |
michael@0 | 148 | */ |
michael@0 | 149 | const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const { |
michael@0 | 150 | return this->getUniformVariable(u).c_str(); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | /** |
michael@0 | 154 | * This returns a variable name to access the 2D, perspective correct version of the coords in |
michael@0 | 155 | * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a |
michael@0 | 156 | * perspective divide into the fragment shader (xy / z) to convert them to 2D. |
michael@0 | 157 | */ |
michael@0 | 158 | SkString ensureFSCoords2D(const TransformedCoordsArray&, int index); |
michael@0 | 159 | |
michael@0 | 160 | /** Returns a variable name that represents the position of the fragment in the FS. The position |
michael@0 | 161 | is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */ |
michael@0 | 162 | const char* fragmentPosition(); |
michael@0 | 163 | |
michael@0 | 164 | /** Returns the color of the destination pixel. This may be NULL if no effect advertised |
michael@0 | 165 | that it will read the destination. */ |
michael@0 | 166 | const char* dstColor(); |
michael@0 | 167 | |
michael@0 | 168 | /** |
michael@0 | 169 | * Interfaces used by GrGLProgram. |
michael@0 | 170 | */ |
michael@0 | 171 | const GrGLSLExpr4& getInputColor() const { |
michael@0 | 172 | return fInputColor; |
michael@0 | 173 | } |
michael@0 | 174 | const GrGLSLExpr4& getInputCoverage() const { |
michael@0 | 175 | return fInputCoverage; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * Adds code for effects and returns a GrGLProgramEffects* object. The caller is responsible for |
michael@0 | 180 | * deleting it when finished. effectStages contains the effects to add. effectKeys[i] is the key |
michael@0 | 181 | * generated from effectStages[i]. inOutFSColor specifies the input color to the first stage and |
michael@0 | 182 | * is updated to be the output color of the last stage. |
michael@0 | 183 | * The handles to texture samplers for effectStage[i] are added to |
michael@0 | 184 | * effectSamplerHandles[i]. |
michael@0 | 185 | */ |
michael@0 | 186 | virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effectStages[], |
michael@0 | 187 | const EffectKey effectKeys[], |
michael@0 | 188 | int effectCnt, |
michael@0 | 189 | GrGLSLExpr4* inOutFSColor) = 0; |
michael@0 | 190 | |
michael@0 | 191 | const char* getColorOutputName() const; |
michael@0 | 192 | const char* enableSecondaryOutput(); |
michael@0 | 193 | |
michael@0 | 194 | GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHeightUniform; } |
michael@0 | 195 | GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { |
michael@0 | 196 | return fDstCopyTopLeftUniform; |
michael@0 | 197 | } |
michael@0 | 198 | GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { |
michael@0 | 199 | return fDstCopyScaleUniform; |
michael@0 | 200 | } |
michael@0 | 201 | GrGLUniformManager::UniformHandle getColorUniform() const { return fColorUniform; } |
michael@0 | 202 | GrGLUniformManager::UniformHandle getCoverageUniform() const { return fCoverageUniform; } |
michael@0 | 203 | GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { |
michael@0 | 204 | return fDstCopySamplerUniform; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | bool finish(GrGLuint* outProgramId); |
michael@0 | 208 | |
michael@0 | 209 | const GrGLContextInfo& ctxInfo() const; |
michael@0 | 210 | |
michael@0 | 211 | /** |
michael@0 | 212 | * Helper for begining and ending a block in the fragment code. TODO: Make GrGLShaderBuilder |
michael@0 | 213 | * aware of all blocks and turn single \t's into the correct number of tabs (or spaces) so that |
michael@0 | 214 | * our shaders print pretty without effect writers tracking indentation. |
michael@0 | 215 | */ |
michael@0 | 216 | class FSBlock { |
michael@0 | 217 | public: |
michael@0 | 218 | FSBlock(GrGLShaderBuilder* builder) : fBuilder(builder) { |
michael@0 | 219 | SkASSERT(NULL != builder); |
michael@0 | 220 | fBuilder->fsCodeAppend("\t{\n"); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | ~FSBlock() { |
michael@0 | 224 | fBuilder->fsCodeAppend("\t}\n"); |
michael@0 | 225 | } |
michael@0 | 226 | private: |
michael@0 | 227 | GrGLShaderBuilder* fBuilder; |
michael@0 | 228 | }; |
michael@0 | 229 | |
michael@0 | 230 | protected: |
michael@0 | 231 | GrGpuGL* gpu() const { return fGpu; } |
michael@0 | 232 | |
michael@0 | 233 | void setInputColor(const GrGLSLExpr4& inputColor) { fInputColor = inputColor; } |
michael@0 | 234 | void setInputCoverage(const GrGLSLExpr4& inputCoverage) { fInputCoverage = inputCoverage; } |
michael@0 | 235 | |
michael@0 | 236 | /** Add input/output variable declarations (i.e. 'varying') to the fragment shader. */ |
michael@0 | 237 | GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); } |
michael@0 | 238 | |
michael@0 | 239 | // Generates a name for a variable. The generated string will be name prefixed by the prefix |
michael@0 | 240 | // char (unless the prefix is '\0'). It also mangles the name to be stage-specific if we're |
michael@0 | 241 | // generating stage code. |
michael@0 | 242 | void nameVariable(SkString* out, char prefix, const char* name); |
michael@0 | 243 | |
michael@0 | 244 | // Helper for emitEffects(). |
michael@0 | 245 | void createAndEmitEffects(GrGLProgramEffectsBuilder*, |
michael@0 | 246 | const GrEffectStage* effectStages[], |
michael@0 | 247 | const EffectKey effectKeys[], |
michael@0 | 248 | int effectCnt, |
michael@0 | 249 | GrGLSLExpr4* inOutFSColor); |
michael@0 | 250 | |
michael@0 | 251 | virtual bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const; |
michael@0 | 252 | virtual void bindProgramLocations(GrGLuint programId) const; |
michael@0 | 253 | |
michael@0 | 254 | void appendDecls(const VarArray&, SkString*) const; |
michael@0 | 255 | void appendUniformDecls(ShaderVisibility, SkString*) const; |
michael@0 | 256 | |
michael@0 | 257 | private: |
michael@0 | 258 | class CodeStage : public SkNoncopyable { |
michael@0 | 259 | public: |
michael@0 | 260 | CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {} |
michael@0 | 261 | |
michael@0 | 262 | bool inStageCode() const { |
michael@0 | 263 | this->validate(); |
michael@0 | 264 | return NULL != fEffectStage; |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | const GrEffectStage* effectStage() const { |
michael@0 | 268 | this->validate(); |
michael@0 | 269 | return fEffectStage; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | int stageIndex() const { |
michael@0 | 273 | this->validate(); |
michael@0 | 274 | return fCurrentIndex; |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | class AutoStageRestore : public SkNoncopyable { |
michael@0 | 278 | public: |
michael@0 | 279 | AutoStageRestore(CodeStage* codeStage, const GrEffectStage* newStage) { |
michael@0 | 280 | SkASSERT(NULL != codeStage); |
michael@0 | 281 | fSavedIndex = codeStage->fCurrentIndex; |
michael@0 | 282 | fSavedEffectStage = codeStage->fEffectStage; |
michael@0 | 283 | |
michael@0 | 284 | if (NULL == newStage) { |
michael@0 | 285 | codeStage->fCurrentIndex = -1; |
michael@0 | 286 | } else { |
michael@0 | 287 | codeStage->fCurrentIndex = codeStage->fNextIndex++; |
michael@0 | 288 | } |
michael@0 | 289 | codeStage->fEffectStage = newStage; |
michael@0 | 290 | |
michael@0 | 291 | fCodeStage = codeStage; |
michael@0 | 292 | } |
michael@0 | 293 | ~AutoStageRestore() { |
michael@0 | 294 | fCodeStage->fCurrentIndex = fSavedIndex; |
michael@0 | 295 | fCodeStage->fEffectStage = fSavedEffectStage; |
michael@0 | 296 | } |
michael@0 | 297 | private: |
michael@0 | 298 | CodeStage* fCodeStage; |
michael@0 | 299 | int fSavedIndex; |
michael@0 | 300 | const GrEffectStage* fSavedEffectStage; |
michael@0 | 301 | }; |
michael@0 | 302 | private: |
michael@0 | 303 | void validate() const { SkASSERT((NULL == fEffectStage) == (-1 == fCurrentIndex)); } |
michael@0 | 304 | int fNextIndex; |
michael@0 | 305 | int fCurrentIndex; |
michael@0 | 306 | const GrEffectStage* fEffectStage; |
michael@0 | 307 | } fCodeStage; |
michael@0 | 308 | |
michael@0 | 309 | /** |
michael@0 | 310 | * Features that should only be enabled by GrGLShaderBuilder itself. |
michael@0 | 311 | */ |
michael@0 | 312 | enum GLSLPrivateFeature { |
michael@0 | 313 | kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
michael@0 | 314 | kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
michael@0 | 315 | kNVShaderFramebufferFetch_GLSLPrivateFeature, |
michael@0 | 316 | }; |
michael@0 | 317 | bool enablePrivateFeature(GLSLPrivateFeature); |
michael@0 | 318 | |
michael@0 | 319 | // If we ever have VS/GS features we can expand this to take a bitmask of ShaderVisibility and |
michael@0 | 320 | // track the enables separately for each shader. |
michael@0 | 321 | void addFSFeature(uint32_t featureBit, const char* extensionName); |
michael@0 | 322 | |
michael@0 | 323 | // Interpretation of DstReadKey when generating code |
michael@0 | 324 | enum { |
michael@0 | 325 | kNoDstRead_DstReadKey = 0, |
michael@0 | 326 | kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
michael@0 | 327 | kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
michael@0 | 328 | kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left. |
michael@0 | 329 | }; |
michael@0 | 330 | |
michael@0 | 331 | enum { |
michael@0 | 332 | kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed. |
michael@0 | 333 | kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left. |
michael@0 | 334 | kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left. |
michael@0 | 335 | }; |
michael@0 | 336 | |
michael@0 | 337 | GrGpuGL* fGpu; |
michael@0 | 338 | GrGLUniformManager& fUniformManager; |
michael@0 | 339 | uint32_t fFSFeaturesAddedMask; |
michael@0 | 340 | SkString fFSFunctions; |
michael@0 | 341 | SkString fFSExtensions; |
michael@0 | 342 | VarArray fFSInputs; |
michael@0 | 343 | VarArray fFSOutputs; |
michael@0 | 344 | GrGLUniformManager::BuilderUniformArray fUniforms; |
michael@0 | 345 | |
michael@0 | 346 | SkString fFSCode; |
michael@0 | 347 | |
michael@0 | 348 | bool fSetupFragPosition; |
michael@0 | 349 | GrGLUniformManager::UniformHandle fDstCopySamplerUniform; |
michael@0 | 350 | |
michael@0 | 351 | GrGLSLExpr4 fInputColor; |
michael@0 | 352 | GrGLSLExpr4 fInputCoverage; |
michael@0 | 353 | |
michael@0 | 354 | bool fHasCustomColorOutput; |
michael@0 | 355 | bool fHasSecondaryOutput; |
michael@0 | 356 | |
michael@0 | 357 | GrGLUniformManager::UniformHandle fRTHeightUniform; |
michael@0 | 358 | GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; |
michael@0 | 359 | GrGLUniformManager::UniformHandle fDstCopyScaleUniform; |
michael@0 | 360 | GrGLUniformManager::UniformHandle fColorUniform; |
michael@0 | 361 | GrGLUniformManager::UniformHandle fCoverageUniform; |
michael@0 | 362 | |
michael@0 | 363 | bool fTopLeftFragPosRead; |
michael@0 | 364 | }; |
michael@0 | 365 | |
michael@0 | 366 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 367 | |
michael@0 | 368 | class GrGLFullShaderBuilder : public GrGLShaderBuilder { |
michael@0 | 369 | public: |
michael@0 | 370 | GrGLFullShaderBuilder(GrGpuGL*, GrGLUniformManager&, const GrGLProgramDesc&); |
michael@0 | 371 | |
michael@0 | 372 | /** |
michael@0 | 373 | * Called by GrGLEffects to add code to one of the shaders. |
michael@0 | 374 | */ |
michael@0 | 375 | void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
michael@0 | 376 | va_list args; |
michael@0 | 377 | va_start(args, format); |
michael@0 | 378 | fVSCode.appendVAList(format, args); |
michael@0 | 379 | va_end(args); |
michael@0 | 380 | } |
michael@0 | 381 | |
michael@0 | 382 | void vsCodeAppend(const char* str) { fVSCode.append(str); } |
michael@0 | 383 | |
michael@0 | 384 | /** Add a vertex attribute to the current program that is passed in from the vertex data. |
michael@0 | 385 | Returns false if the attribute was already there, true otherwise. */ |
michael@0 | 386 | bool addAttribute(GrSLType type, const char* name); |
michael@0 | 387 | |
michael@0 | 388 | /** Add a varying variable to the current program to pass values between vertex and fragment |
michael@0 | 389 | shaders. If the last two parameters are non-NULL, they are filled in with the name |
michael@0 | 390 | generated. */ |
michael@0 | 391 | void addVarying(GrSLType type, |
michael@0 | 392 | const char* name, |
michael@0 | 393 | const char** vsOutName = NULL, |
michael@0 | 394 | const char** fsInName = NULL); |
michael@0 | 395 | |
michael@0 | 396 | /** Returns a vertex attribute that represents the vertex position in the VS. This is the |
michael@0 | 397 | pre-matrix position and is commonly used by effects to compute texture coords via a matrix. |
michael@0 | 398 | */ |
michael@0 | 399 | const GrGLShaderVar& positionAttribute() const { return *fPositionVar; } |
michael@0 | 400 | |
michael@0 | 401 | /** Returns a vertex attribute that represents the local coords in the VS. This may be the same |
michael@0 | 402 | as positionAttribute() or it may not be. It depends upon whether the rendering code |
michael@0 | 403 | specified explicit local coords or not in the GrDrawState. */ |
michael@0 | 404 | const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoordsVar; } |
michael@0 | 405 | |
michael@0 | 406 | /** |
michael@0 | 407 | * Are explicit local coordinates provided as input to the vertex shader. |
michael@0 | 408 | */ |
michael@0 | 409 | bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositionVar); } |
michael@0 | 410 | |
michael@0 | 411 | bool addEffectAttribute(int attributeIndex, GrSLType type, const SkString& name); |
michael@0 | 412 | const SkString* getEffectAttributeName(int attributeIndex) const; |
michael@0 | 413 | |
michael@0 | 414 | virtual GrGLProgramEffects* createAndEmitEffects( |
michael@0 | 415 | const GrEffectStage* effectStages[], |
michael@0 | 416 | const EffectKey effectKeys[], |
michael@0 | 417 | int effectCnt, |
michael@0 | 418 | GrGLSLExpr4* inOutFSColor) SK_OVERRIDE; |
michael@0 | 419 | |
michael@0 | 420 | GrGLUniformManager::UniformHandle getViewMatrixUniform() const { |
michael@0 | 421 | return fViewMatrixUniform; |
michael@0 | 422 | } |
michael@0 | 423 | |
michael@0 | 424 | protected: |
michael@0 | 425 | virtual bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const SK_OVERRIDE; |
michael@0 | 426 | virtual void bindProgramLocations(GrGLuint programId) const SK_OVERRIDE; |
michael@0 | 427 | |
michael@0 | 428 | private: |
michael@0 | 429 | const GrGLProgramDesc& fDesc; |
michael@0 | 430 | VarArray fVSAttrs; |
michael@0 | 431 | VarArray fVSOutputs; |
michael@0 | 432 | VarArray fGSInputs; |
michael@0 | 433 | VarArray fGSOutputs; |
michael@0 | 434 | |
michael@0 | 435 | SkString fVSCode; |
michael@0 | 436 | |
michael@0 | 437 | struct AttributePair { |
michael@0 | 438 | void set(int index, const SkString& name) { |
michael@0 | 439 | fIndex = index; fName = name; |
michael@0 | 440 | } |
michael@0 | 441 | int fIndex; |
michael@0 | 442 | SkString fName; |
michael@0 | 443 | }; |
michael@0 | 444 | SkSTArray<10, AttributePair, true> fEffectAttributes; |
michael@0 | 445 | |
michael@0 | 446 | GrGLUniformManager::UniformHandle fViewMatrixUniform; |
michael@0 | 447 | |
michael@0 | 448 | GrGLShaderVar* fPositionVar; |
michael@0 | 449 | GrGLShaderVar* fLocalCoordsVar; |
michael@0 | 450 | |
michael@0 | 451 | typedef GrGLShaderBuilder INHERITED; |
michael@0 | 452 | }; |
michael@0 | 453 | |
michael@0 | 454 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 455 | |
michael@0 | 456 | class GrGLFragmentOnlyShaderBuilder : public GrGLShaderBuilder { |
michael@0 | 457 | public: |
michael@0 | 458 | GrGLFragmentOnlyShaderBuilder(GrGpuGL*, GrGLUniformManager&, const GrGLProgramDesc&); |
michael@0 | 459 | |
michael@0 | 460 | int getNumTexCoordSets() const { return fNumTexCoordSets; } |
michael@0 | 461 | int addTexCoordSets(int count); |
michael@0 | 462 | |
michael@0 | 463 | virtual GrGLProgramEffects* createAndEmitEffects( |
michael@0 | 464 | const GrEffectStage* effectStages[], |
michael@0 | 465 | const EffectKey effectKeys[], |
michael@0 | 466 | int effectCnt, |
michael@0 | 467 | GrGLSLExpr4* inOutFSColor) SK_OVERRIDE; |
michael@0 | 468 | |
michael@0 | 469 | private: |
michael@0 | 470 | int fNumTexCoordSets; |
michael@0 | 471 | |
michael@0 | 472 | typedef GrGLShaderBuilder INHERITED; |
michael@0 | 473 | }; |
michael@0 | 474 | |
michael@0 | 475 | #endif |