| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 GrGLShaderBuilder_DEFINED | 8 #ifndef GrGLShaderBuilder_DEFINED |
| 9 #define GrGLShaderBuilder_DEFINED | 9 #define GrGLShaderBuilder_DEFINED |
| 10 | 10 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 // origin_upper_left is not supported. | 54 // origin_upper_left is not supported. |
| 55 UniformHandle fRTHeightUni; | 55 UniformHandle fRTHeightUni; |
| 56 | 56 |
| 57 // Uniforms for computing texture coords to do the dst-copy lookup | 57 // Uniforms for computing texture coords to do the dst-copy lookup |
| 58 UniformHandle fDstCopyTopLeftUni; | 58 UniformHandle fDstCopyTopLeftUni; |
| 59 UniformHandle fDstCopyScaleUni; | 59 UniformHandle fDstCopyScaleUni; |
| 60 UniformHandle fDstCopySamplerUni; | 60 UniformHandle fDstCopySamplerUni; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 struct GenProgramOutput { | 63 struct GenProgramOutput { |
| 64 GenProgramOutput() |
| 65 : fColorEffects(NULL) |
| 66 , fCoverageEffects(NULL) |
| 67 , fHasVertexShader(false) |
| 68 , fTexCoordSetCnt(0) |
| 69 , fProgramID(0) {} |
| 70 |
| 64 GrGLProgramEffects* fColorEffects; | 71 GrGLProgramEffects* fColorEffects; |
| 65 GrGLProgramEffects* fCoverageEffects; | 72 GrGLProgramEffects* fCoverageEffects; |
| 66 UniformHandles fUniformHandles; | 73 UniformHandles fUniformHandles; |
| 67 bool fHasVS; | 74 bool fHasVertexShader; |
| 68 int fNumTexCoordSets; | 75 int fTexCoordSetCnt; |
| 69 GrGLuint fProgramID; | 76 GrGLuint fProgramID; |
| 70 }; | 77 }; |
| 71 | 78 |
| 72 static bool GenProgram(GrGpuGL* gpu, | 79 static bool GenProgram(GrGpuGL* gpu, |
| 73 GrGLUniformManager* uman, | 80 GrGLUniformManager* uman, |
| 74 const GrGLProgramDesc& desc, | 81 const GrGLProgramDesc& desc, |
| 75 const GrEffectStage* inColorStages[], | 82 const GrEffectStage* inColorStages[], |
| 76 const GrEffectStage* inCoverageStages[], | 83 const GrEffectStage* inCoverageStages[], |
| 77 GenProgramOutput* output); | 84 GenProgramOutput* output); |
| 78 | 85 |
| 79 virtual ~GrGLShaderBuilder() {} | 86 virtual ~GrGLShaderBuilder() {} |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 159 |
| 153 /** If texture swizzling is available using tex parameters then it is prefer
red over mangling | 160 /** If texture swizzling is available using tex parameters then it is prefer
red over mangling |
| 154 the generated shader code. This potentially allows greater reuse of cach
ed shaders. */ | 161 the generated shader code. This potentially allows greater reuse of cach
ed shaders. */ |
| 155 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa
ps& caps); | 162 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa
ps& caps); |
| 156 | 163 |
| 157 /** Add a uniform variable to the current program, that has visibility in on
e or more shaders. | 164 /** Add a uniform variable to the current program, that has visibility in on
e or more shaders. |
| 158 visibility is a bitfield of ShaderVisibility values indicating from whic
h shaders the | 165 visibility is a bitfield of ShaderVisibility values indicating from whic
h shaders the |
| 159 uniform should be accessible. At least one bit must be set. Geometry sha
der uniforms are not | 166 uniform should be accessible. At least one bit must be set. Geometry sha
der uniforms are not |
| 160 supported at this time. The actual uniform name will be mangled. If outN
ame is not NULL then | 167 supported at this time. The actual uniform name will be mangled. If outN
ame is not NULL then |
| 161 it will refer to the final uniform name after return. Use the addUniform
Array variant to add | 168 it will refer to the final uniform name after return. Use the addUniform
Array variant to add |
| 162 an array of uniforms. | 169 an array of uniforms. */ |
| 163 */ | |
| 164 GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, | 170 GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, |
| 165 GrSLType type, | 171 GrSLType type, |
| 166 const char* name, | 172 const char* name, |
| 167 const char** outName = NULL) { | 173 const char** outName = NULL) { |
| 168 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNon
Array, outName); | 174 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNon
Array, outName); |
| 169 } | 175 } |
| 170 GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, | 176 GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, |
| 171 GrSLType type, | 177 GrSLType type, |
| 172 const char* name, | 178 const char* name, |
| 173 int arrayCount, | 179 int arrayCount, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 // generating stage code. | 251 // generating stage code. |
| 246 void nameVariable(SkString* out, char prefix, const char* name); | 252 void nameVariable(SkString* out, char prefix, const char* name); |
| 247 | 253 |
| 248 virtual bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>
* shaderIds) const; | 254 virtual bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>
* shaderIds) const; |
| 249 | 255 |
| 250 virtual void bindProgramLocations(GrGLuint programId) const; | 256 virtual void bindProgramLocations(GrGLuint programId) const; |
| 251 | 257 |
| 252 void appendDecls(const VarArray&, SkString*) const; | 258 void appendDecls(const VarArray&, SkString*) const; |
| 253 void appendUniformDecls(ShaderVisibility, SkString*) const; | 259 void appendUniformDecls(ShaderVisibility, SkString*) const; |
| 254 | 260 |
| 261 const GenProgramOutput& getOutput() const { return fOutput; } |
| 262 |
| 263 GenProgramOutput fOutput; |
| 264 |
| 255 private: | 265 private: |
| 256 class CodeStage : SkNoncopyable { | 266 class CodeStage : SkNoncopyable { |
| 257 public: | 267 public: |
| 258 CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {} | 268 CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {} |
| 259 | 269 |
| 260 bool inStageCode() const { | 270 bool inStageCode() const { |
| 261 this->validate(); | 271 this->validate(); |
| 262 return NULL != fEffectStage; | 272 return NULL != fEffectStage; |
| 263 } | 273 } |
| 264 | 274 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 int fSavedIndex; | 307 int fSavedIndex; |
| 298 const GrEffectStage* fSavedEffectStage; | 308 const GrEffectStage* fSavedEffectStage; |
| 299 }; | 309 }; |
| 300 private: | 310 private: |
| 301 void validate() const { SkASSERT((NULL == fEffectStage) == (-1 == fCurre
ntIndex)); } | 311 void validate() const { SkASSERT((NULL == fEffectStage) == (-1 == fCurre
ntIndex)); } |
| 302 int fNextIndex; | 312 int fNextIndex; |
| 303 int fCurrentIndex; | 313 int fCurrentIndex; |
| 304 const GrEffectStage* fEffectStage; | 314 const GrEffectStage* fEffectStage; |
| 305 } fCodeStage; | 315 } fCodeStage; |
| 306 | 316 |
| 307 bool genProgram(const GrEffectStage* colorStages[], | 317 bool genProgram(const GrEffectStage* colorStages[], const GrEffectStage* cov
erageStages[]); |
| 308 const GrEffectStage* coverageStages[], | |
| 309 GenProgramOutput* output); | |
| 310 | 318 |
| 311 /** | 319 /** |
| 312 * Adds code for effects and returns a GrGLProgramEffects* object. The caller
is responsible for | 320 * Adds code for effects and returns a GrGLProgramEffects* object. The caller
is responsible for |
| 313 * deleting it when finished. effectStages contains the effects to add. effec
tKeys[i] is the key | 321 * deleting it when finished. effectStages contains the effects to add. effec
tKeys[i] is the key |
| 314 * generated from effectStages[i]. inOutFSColor specifies the input color to
the first stage and | 322 * generated from effectStages[i]. inOutFSColor specifies the input color to
the first stage and |
| 315 * is updated to be the output color of the last stage. | 323 * is updated to be the output color of the last stage. |
| 316 * The handles to texture samplers for effectStage[i] are added to | 324 * The handles to texture samplers for effectStage[i] are added to |
| 317 * effectSamplerHandles[i]. | 325 * effectSamplerHandles[i]. |
| 318 */ | 326 */ |
| 319 virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effect
Stages[], | 327 virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effect
Stages[], |
| 320 const EffectKey effectKeys[
], | 328 const EffectKey effectKeys[
], |
| 321 int effectCnt, | 329 int effectCnt, |
| 322 GrGLSLExpr4* inOutFSColor)
= 0; | 330 GrGLSLExpr4* inOutFSColor)
= 0; |
| 323 | 331 |
| 324 /** Enables using the secondary color output and returns the name of the var
in which it is | 332 /** Enables using the secondary color output and returns the name of the var
in which it is |
| 325 to be stored */ | 333 to be stored */ |
| 326 const char* enableSecondaryOutput(); | 334 const char* enableSecondaryOutput(); |
| 327 /** Gets the name of the primary color output. */ | 335 /** Gets the name of the primary color output. */ |
| 328 const char* getColorOutputName() const; | 336 const char* getColorOutputName() const; |
| 329 | 337 |
| 330 bool finish(GrGLuint* outProgramId); | 338 /** |
| 339 * Compiles all the shaders, links them into a program, and writes the progr
am id to the output |
| 340 * struct. |
| 341 **/ |
| 342 bool finish(); |
| 331 | 343 |
| 332 const GrGLSLExpr4& getInputColor() const { | 344 const GrGLSLExpr4& getInputColor() const { |
| 333 return fInputColor; | 345 return fInputColor; |
| 334 } | 346 } |
| 335 | 347 |
| 336 const GrGLSLExpr4& getInputCoverage() const { | 348 const GrGLSLExpr4& getInputCoverage() const { |
| 337 return fInputCoverage; | 349 return fInputCoverage; |
| 338 } | 350 } |
| 339 | 351 |
| 340 /** | 352 /** |
| 341 * Features that should only be enabled by GrGLShaderBuilder itself. | 353 * Features that should only be enabled by GrGLShaderBuilder itself. |
| 342 */ | 354 */ |
| 343 enum GLSLPrivateFeature { | 355 enum GLSLPrivateFeature { |
| 344 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, | 356 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
| 345 kEXTShaderFramebufferFetch_GLSLPrivateFeature, | 357 kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
| 346 kNVShaderFramebufferFetch_GLSLPrivateFeature, | 358 kNVShaderFramebufferFetch_GLSLPrivateFeature, |
| 347 }; | 359 }; |
| 348 bool enablePrivateFeature(GLSLPrivateFeature); | 360 bool enablePrivateFeature(GLSLPrivateFeature); |
| 349 | 361 |
| 350 // If we ever have VS/GS features we can expand this to take a bitmask of Sh
aderVisibility and | 362 // If we ever have VS/GS features we can expand this to take a bitmask of Sh
aderVisibility and |
| 351 // track the enables separately for each shader. | 363 // track the enables separately for each shader. |
| 352 void addFSFeature(uint32_t featureBit, const char* extensionName); | 364 void addFSFeature(uint32_t featureBit, const char* extensionName); |
| 353 | 365 |
| 354 // Interpretation of DstReadKey when generating code | 366 // Interpretation of DstReadKey when generating code |
| 355 enum { | 367 enum { |
| 356 kNoDstRead_DstReadKey = 0, | 368 kNoDstRead_DstReadKey = 0, |
| 357 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. | 369 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 358 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha
only. | 370 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alph
a only. |
| 359 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-le
ft. | 371 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-
left. |
| 360 }; | 372 }; |
| 361 | 373 |
| 362 enum { | 374 enum { |
| 363 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. | 375 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. |
| 364 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. | 376 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. |
| 365 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. | 377 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. |
| 366 }; | 378 }; |
| 367 | 379 |
| 368 const GrGLProgramDesc& fDesc; | 380 const GrGLProgramDesc& fDesc; |
| 369 GrGpuGL* fGpu; | 381 GrGpuGL* fGpu; |
| 370 SkAutoTUnref<GrGLUniformManager> fUniformManager; | 382 SkAutoTUnref<GrGLUniformManager> fUniformManager; |
| 371 uint32_t fFSFeaturesAddedMask; | 383 uint32_t fFSFeaturesAddedMask; |
| 372 SkString fFSFunctions; | 384 SkString fFSFunctions; |
| 373 SkString fFSExtensions; | 385 SkString fFSExtensions; |
| 374 VarArray fFSInputs; | 386 VarArray fFSInputs; |
| 375 VarArray fFSOutputs; | 387 VarArray fFSOutputs; |
| 376 GrGLUniformManager::BuilderUniformArray fUniforms; | 388 GrGLUniformManager::BuilderUniformArray fUniforms; |
| 377 | 389 |
| 378 SkString fFSCode; | 390 SkString fFSCode; |
| 379 | 391 |
| 380 bool fSetupFragPosition; | 392 bool fSetupFragPosition; |
| 381 GrGLUniformManager::UniformHandle fDstCopySamplerUniform; | 393 bool fTopLeftFragPosRead; |
| 382 | 394 |
| 383 GrGLSLExpr4 fInputColor; | 395 GrGLSLExpr4 fInputColor; |
| 384 GrGLSLExpr4 fInputCoverage; | 396 GrGLSLExpr4 fInputCoverage; |
| 385 | 397 |
| 386 bool fHasCustomColorOutput; | 398 bool fHasCustomColorOutput; |
| 387 bool fHasSecondaryOutput; | 399 bool fHasSecondaryOutput; |
| 388 | |
| 389 GrGLUniformManager::UniformHandle fRTHeightUniform; | |
| 390 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; | |
| 391 GrGLUniformManager::UniformHandle fDstCopyScaleUniform; | |
| 392 GrGLUniformManager::UniformHandle fColorUniform; | |
| 393 GrGLUniformManager::UniformHandle fCoverageUniform; | |
| 394 | |
| 395 bool fTopLeftFragPosRead; | |
| 396 }; | 400 }; |
| 397 | 401 |
| 398 //////////////////////////////////////////////////////////////////////////////// | 402 //////////////////////////////////////////////////////////////////////////////// |
| 399 | 403 |
| 400 class GrGLFullShaderBuilder : public GrGLShaderBuilder { | 404 class GrGLFullShaderBuilder : public GrGLShaderBuilder { |
| 401 public: | 405 public: |
| 402 GrGLFullShaderBuilder(GrGpuGL*, GrGLUniformManager*, const GrGLProgramDesc&)
; | 406 GrGLFullShaderBuilder(GrGpuGL*, GrGLUniformManager*, const GrGLProgramDesc&)
; |
| 403 | 407 |
| 404 /** | 408 /** |
| 405 * Called by GrGLEffects to add code to one of the shaders. | 409 * Called by GrGLEffects to add code to one of the shaders. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 436 const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoordsVar;
} | 440 const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoordsVar;
} |
| 437 | 441 |
| 438 /** | 442 /** |
| 439 * Are explicit local coordinates provided as input to the vertex shader. | 443 * Are explicit local coordinates provided as input to the vertex shader. |
| 440 */ | 444 */ |
| 441 bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositionVa
r); } | 445 bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositionVa
r); } |
| 442 | 446 |
| 443 bool addEffectAttribute(int attributeIndex, GrSLType type, const SkString& n
ame); | 447 bool addEffectAttribute(int attributeIndex, GrSLType type, const SkString& n
ame); |
| 444 const SkString* getEffectAttributeName(int attributeIndex) const; | 448 const SkString* getEffectAttributeName(int attributeIndex) const; |
| 445 | 449 |
| 446 /** | |
| 447 * The view matrix uniform is only valid in the VS. It is always mat33. | |
| 448 */ | |
| 449 GrGLUniformManager::UniformHandle getViewMatrixUniform() const { | |
| 450 return fViewMatrixUniform; | |
| 451 } | |
| 452 | |
| 453 GrGLUniformManager::UniformHandle getRTAdjustmentVecUniform() const { | |
| 454 return fRTAdustmentVecUniform; | |
| 455 } | |
| 456 | |
| 457 private: | 450 private: |
| 458 virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effect
Stages[], | 451 virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effect
Stages[], |
| 459 const EffectKey effectKeys[
], | 452 const EffectKey effectKeys[
], |
| 460 int effectCnt, | 453 int effectCnt, |
| 461 GrGLSLExpr4* inOutFSColor)
SK_OVERRIDE; | 454 GrGLSLExpr4* inOutFSColor)
SK_OVERRIDE; |
| 462 virtual bool compileAndAttachShaders(GrGLuint programId, | 455 virtual bool compileAndAttachShaders(GrGLuint programId, |
| 463 SkTDArray<GrGLuint>* shaderIds) const S
K_OVERRIDE; | 456 SkTDArray<GrGLuint>* shaderIds) const S
K_OVERRIDE; |
| 464 | 457 |
| 465 virtual void bindProgramLocations(GrGLuint programId) const SK_OVERRIDE; | 458 virtual void bindProgramLocations(GrGLuint programId) const SK_OVERRIDE; |
| 466 | 459 |
| 467 VarArray fVSAttrs; | 460 VarArray fVSAttrs; |
| 468 VarArray fVSOutputs; | 461 VarArray fVSOutputs; |
| 469 VarArray fGSInputs; | 462 VarArray fGSInputs; |
| 470 VarArray fGSOutputs; | 463 VarArray fGSOutputs; |
| 471 | 464 |
| 472 SkString fVSCode; | 465 SkString fVSCode; |
| 473 | 466 |
| 474 struct AttributePair { | 467 struct AttributePair { |
| 475 void set(int index, const SkString& name) { | 468 void set(int index, const SkString& name) { |
| 476 fIndex = index; fName = name; | 469 fIndex = index; fName = name; |
| 477 } | 470 } |
| 478 int fIndex; | 471 int fIndex; |
| 479 SkString fName; | 472 SkString fName; |
| 480 }; | 473 }; |
| 481 SkSTArray<10, AttributePair, true> fEffectAttributes; | 474 SkSTArray<10, AttributePair, true> fEffectAttributes; |
| 482 | 475 |
| 483 GrGLUniformManager::UniformHandle fViewMatrixUniform; | |
| 484 GrGLUniformManager::UniformHandle fRTAdustmentVecUniform; | |
| 485 GrGLShaderVar* fPositionVar; | 476 GrGLShaderVar* fPositionVar; |
| 486 GrGLShaderVar* fLocalCoordsVar; | 477 GrGLShaderVar* fLocalCoordsVar; |
| 487 | 478 |
| 488 typedef GrGLShaderBuilder INHERITED; | 479 typedef GrGLShaderBuilder INHERITED; |
| 489 }; | 480 }; |
| 490 | 481 |
| 491 //////////////////////////////////////////////////////////////////////////////// | 482 //////////////////////////////////////////////////////////////////////////////// |
| 492 | 483 |
| 493 class GrGLFragmentOnlyShaderBuilder : public GrGLShaderBuilder { | 484 class GrGLFragmentOnlyShaderBuilder : public GrGLShaderBuilder { |
| 494 public: | 485 public: |
| 495 GrGLFragmentOnlyShaderBuilder(GrGpuGL*, GrGLUniformManager*, const GrGLProgr
amDesc&); | 486 GrGLFragmentOnlyShaderBuilder(GrGpuGL*, GrGLUniformManager*, const GrGLProgr
amDesc&); |
| 496 | 487 |
| 497 int getNumTexCoordSets() const { return fNumTexCoordSets; } | |
| 498 int addTexCoordSets(int count); | 488 int addTexCoordSets(int count); |
| 499 | 489 |
| 500 private: | 490 private: |
| 501 | 491 |
| 502 virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effect
Stages[], | 492 virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effect
Stages[], |
| 503 const EffectKey effectKeys[
], | 493 const EffectKey effectKeys[
], |
| 504 int effectCnt, | 494 int effectCnt, |
| 505 GrGLSLExpr4* inOutFSColor)
SK_OVERRIDE; | 495 GrGLSLExpr4* inOutFSColor)
SK_OVERRIDE; |
| 506 | 496 |
| 507 int fNumTexCoordSets; | |
| 508 | |
| 509 typedef GrGLShaderBuilder INHERITED; | 497 typedef GrGLShaderBuilder INHERITED; |
| 510 }; | 498 }; |
| 511 | 499 |
| 512 #endif | 500 #endif |
| OLD | NEW |