Chromium Code Reviews| 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 | 98 |
| 99 typedef SkTArray<TextureSampler> TextureSamplerArray; | 99 typedef SkTArray<TextureSampler> TextureSamplerArray; |
| 100 typedef GrTAllocator<GrGLShaderVar> VarArray; | 100 typedef GrTAllocator<GrGLShaderVar> VarArray; |
| 101 | 101 |
| 102 enum ShaderVisibility { | 102 enum ShaderVisibility { |
| 103 kVertex_Visibility = 0x1, | 103 kVertex_Visibility = 0x1, |
| 104 kGeometry_Visibility = 0x2, | 104 kGeometry_Visibility = 0x2, |
| 105 kFragment_Visibility = 0x4, | 105 kFragment_Visibility = 0x4, |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 GrGLShaderBuilder(const GrGLContextInfo&, | 108 GrGLShaderBuilder(GrGpuGL*, |
| 109 GrGLUniformManager&, | 109 GrGLUniformManager&, |
| 110 const GrGLProgramDesc&, | 110 const GrGLProgramDesc&, |
| 111 bool needsVertexShader); | 111 bool needsVertexShader); |
| 112 | 112 |
| 113 /** | 113 /** |
| 114 * Use of these features may require a GLSL extension to be enabled. Shaders may not compile | 114 * Use of these features may require a GLSL extension to be enabled. Shaders may not compile |
| 115 * if code is added that uses one of these features without calling enableFe ature() | 115 * if code is added that uses one of these features without calling enableFe ature() |
| 116 */ | 116 */ |
| 117 enum GLSLFeature { | 117 enum GLSLFeature { |
| 118 kStandardDerivatives_GLSLFeature = 0, | 118 kStandardDerivatives_GLSLFeature = 0, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 /** Emits a helper function outside of main() in the fragment shader. */ | 164 /** Emits a helper function outside of main() in the fragment shader. */ |
| 165 void fsEmitFunction(GrSLType returnType, | 165 void fsEmitFunction(GrSLType returnType, |
| 166 const char* name, | 166 const char* name, |
| 167 int argCnt, | 167 int argCnt, |
| 168 const GrGLShaderVar* args, | 168 const GrGLShaderVar* args, |
| 169 const char* body, | 169 const char* body, |
| 170 SkString* outName); | 170 SkString* outName); |
| 171 | 171 |
| 172 /** Add input/output variable declarations (i.e. 'varying') to the fragment shader. */ | 172 /** Add input/output variable declarations (i.e. 'varying') to the fragment shader. */ |
| 173 GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); } | 173 GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); } |
| 174 GrGLShaderVar& fsOutputAppend() { return fFSOutputs.push_back(); } | |
| 175 GrGLShaderVar& fsInputAppend(const GrGLShaderVar& var) { return fFSInputs.pu sh_back(var); } | |
| 176 GrGLShaderVar& fsOutputAppend(const GrGLShaderVar& var) { return fFSOutputs. push_back(var); } | |
| 177 | 174 |
| 178 /** Generates a EffectKey for the shader code based on the texture access pa rameters and the | 175 /** Generates a EffectKey for the shader code based on the texture access pa rameters and the |
| 179 capabilities of the GL context. This is useful for keying the shader pr ograms that may | 176 capabilities of the GL context. This is useful for keying the shader pr ograms that may |
| 180 have multiple representations, based on the type/format of textures used . */ | 177 have multiple representations, based on the type/format of textures used . */ |
| 181 static GrBackendEffectFactory::EffectKey KeyForTextureAccess(const GrTexture Access&, | 178 static GrBackendEffectFactory::EffectKey KeyForTextureAccess(const GrTexture Access&, |
| 182 const GrGLCaps& ); | 179 const GrGLCaps& ); |
| 183 | 180 |
| 184 typedef uint8_t DstReadKey; | 181 typedef uint8_t DstReadKey; |
| 185 typedef uint8_t FragPosKey; | 182 typedef uint8_t FragPosKey; |
| 186 | 183 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 /** Returns a variable name that represents the position of the fragment in the FS. The position | 228 /** Returns a variable name that represents the position of the fragment in the FS. The position |
| 232 is in device space (e.g. 0,0 is the top left and pixel centers are at ha lf-integers). */ | 229 is in device space (e.g. 0,0 is the top left and pixel centers are at ha lf-integers). */ |
| 233 const char* fragmentPosition(); | 230 const char* fragmentPosition(); |
| 234 | 231 |
| 235 /** Returns the color of the destination pixel. This may be NULL if no effec t advertised | 232 /** Returns the color of the destination pixel. This may be NULL if no effec t advertised |
| 236 that it will read the destination. */ | 233 that it will read the destination. */ |
| 237 const char* dstColor(); | 234 const char* dstColor(); |
| 238 | 235 |
| 239 /** | 236 /** |
| 240 * Interfaces used by GrGLProgram. | 237 * Interfaces used by GrGLProgram. |
| 241 * TODO: Hide these from the GrEffects using friend or splitting this into t wo related classes. | 238 * TODO: Hide these from the GrEffects using friend or splitting this into t wo related classes. |
|
bsalomon
2013/09/18 20:03:25
Can we change this comment to say:
TODO: These ar
| |
| 242 * Also, GrGLProgram's shader string construction should be moved to this cl ass. | 239 * Also, GrGLProgram's shader string construction should be moved to this cl ass. |
| 243 */ | 240 */ |
| 244 | 241 const SkString& getInputColor() const { return fInputColor; } |
| 245 /** Called after building is complete to get the final shader string. To acc es the vertex | 242 GrSLConstantVec getKnownColorValue() const { return fKnownColorValue; } |
| 246 and geometry shaders, use the VertexBuilder. */ | 243 const SkString& getInputCoverage() const { return fInputCoverage; } |
| 247 void fsGetShader(SkString*) const; | 244 GrSLConstantVec getKnownCoverageValue() const { return fKnownCoverageValue; } |
| 248 | 245 |
| 249 /** | 246 /** |
| 250 * Adds code for effects. effectStages contains the effects to add. effectKe ys[i] is the key | 247 * Adds code for effects. effectStages contains the effects to add. effectKe ys[i] is the key |
| 251 * generated from effectStages[i]. An entry in effectStages can be NULL, in which case it is | 248 * generated from effectStages[i]. An entry in effectStages can be NULL, in which case it is |
| 252 * skipped. Moreover, if the corresponding key is GrGLEffect::NoEffectKey th en it is skipped. | 249 * skipped. Moreover, if the corresponding key is GrGLEffect::NoEffectKey th en it is skipped. |
| 253 * inOutFSColor specifies the input color to the first stage and is updated to be the | 250 * inOutFSColor specifies the input color to the first stage and is updated to be the |
| 254 * output color of the last stage. fsInOutColorKnownValue specifies whether the input color | 251 * output color of the last stage. fsInOutColorKnownValue specifies whether the input color |
| 255 * has a known constant value and is updated to refer to the status of the o utput color. | 252 * has a known constant value and is updated to refer to the status of the o utput color. |
| 256 * The handles to texture samplers for effectStage[i] are added to effectSam plerHandles[i]. The | 253 * The handles to texture samplers for effectStage[i] are added to effectSam plerHandles[i]. The |
| 257 * glEffects array is updated to contain the GrGLEffect generated for each e ntry in | 254 * glEffects array is updated to contain the GrGLEffect generated for each e ntry in |
| 258 * effectStages. | 255 * effectStages. |
| 259 */ | 256 */ |
| 260 void emitEffects(const GrEffectStage* effectStages[], | 257 void emitEffects(const GrEffectStage* effectStages[], |
| 261 const GrBackendEffectFactory::EffectKey effectKeys[], | 258 const GrBackendEffectFactory::EffectKey effectKeys[], |
| 262 int effectCnt, | 259 int effectCnt, |
| 263 SkString* inOutFSColor, | 260 SkString* inOutFSColor, |
| 264 GrSLConstantVec* fsInOutColorKnownValue, | 261 GrSLConstantVec* fsInOutColorKnownValue, |
| 265 SkTArray<GrGLUniformManager::UniformHandle, true>* effectSa mplerHandles[], | 262 SkTArray<GrGLUniformManager::UniformHandle, true>* effectSa mplerHandles[], |
| 266 GrGLEffect* glEffects[]); | 263 GrGLEffect* glEffects[]); |
| 267 | 264 |
| 265 const char* getColorOutputName() const; | |
| 266 const char* enableSecondaryOutput(); | |
| 267 | |
| 268 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei ghtUniform; } | 268 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei ghtUniform; } |
| 269 GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { | 269 GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { |
| 270 return fDstCopyTopLeftUniform; | 270 return fDstCopyTopLeftUniform; |
| 271 } | 271 } |
| 272 GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { | 272 GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { |
| 273 return fDstCopyScaleUniform; | 273 return fDstCopyScaleUniform; |
| 274 } | 274 } |
| 275 GrGLUniformManager::UniformHandle getColorUniform() const { return fColorUni form; } | |
| 276 GrGLUniformManager::UniformHandle getCoverageUniform() const { return fCover ageUniform; } | |
| 275 GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { | 277 GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { |
| 276 return fDstCopySampler.fSamplerUniform; | 278 return fDstCopySampler.fSamplerUniform; |
| 277 } | 279 } |
| 278 | 280 |
| 279 /** Helper class used to build the vertex and geometry shaders. This functio nality | 281 /** Helper class used to build the vertex and geometry shaders. This functio nality |
| 280 is kept separate from the rest of GrGLShaderBuilder to allow for shaders programs | 282 is kept separate from the rest of GrGLShaderBuilder to allow for shaders programs |
| 281 that only use the fragment shader. */ | 283 that only use the fragment shader. */ |
| 282 class VertexBuilder { | 284 class VertexBuilder { |
| 283 public: | 285 public: |
| 284 VertexBuilder(GrGLShaderBuilder* parent, const GrGLProgramDesc&); | 286 VertexBuilder(GrGLShaderBuilder* parent, GrGpuGL* gpu, const GrGLProgram Desc&); |
| 285 | 287 |
| 286 /** | 288 /** |
| 287 * Called by GrGLEffects to add code to one of the shaders. | 289 * Called by GrGLEffects to add code to one of the shaders. |
| 288 */ | 290 */ |
| 289 void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { | 291 void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 290 va_list args; | 292 va_list args; |
| 291 va_start(args, format); | 293 va_start(args, format); |
| 292 fVSCode.appendf(format, args); | 294 fVSCode.appendf(format, args); |
| 293 va_end(args); | 295 va_end(args); |
| 294 } | 296 } |
| 295 | 297 |
| 296 void gsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { | |
| 297 va_list args; | |
| 298 va_start(args, format); | |
| 299 fGSCode.appendf(format, args); | |
| 300 va_end(args); | |
| 301 } | |
| 302 | |
| 303 void vsCodeAppend(const char* str) { fVSCode.append(str); } | 298 void vsCodeAppend(const char* str) { fVSCode.append(str); } |
| 304 void gsCodeAppend(const char* str) { fGSCode.append(str); } | |
| 305 | 299 |
| 306 /** Add a vertex attribute to the current program that is passed in from the vertex data. | 300 /** Add a vertex attribute to the current program that is passed in from the vertex data. |
| 307 Returns false if the attribute was already there, true otherwise. */ | 301 Returns false if the attribute was already there, true otherwise. */ |
| 308 bool addAttribute(GrSLType type, const char* name); | 302 bool addAttribute(GrSLType type, const char* name); |
| 309 | 303 |
| 310 /** Add a varying variable to the current program to pass values between vertex and fragment | 304 /** Add a varying variable to the current program to pass values between vertex and fragment |
| 311 shaders. If the last two parameters are non-NULL, they are filled in with the name | 305 shaders. If the last two parameters are non-NULL, they are filled in with the name |
| 312 generated. */ | 306 generated. */ |
| 313 void addVarying(GrSLType type, | 307 void addVarying(GrSLType type, |
| 314 const char* name, | 308 const char* name, |
| 315 const char** vsOutName = NULL, | 309 const char** vsOutName = NULL, |
| 316 const char** fsInName = NULL); | 310 const char** fsInName = NULL); |
| 317 | 311 |
| 318 /** Returns a vertex attribute that represents the vertex position in th e VS. This is the | 312 /** Returns a vertex attribute that represents the vertex position in th e VS. This is the |
| 319 pre-matrix position and is commonly used by effects to compute textu re coords via a matrix. | 313 pre-matrix position and is commonly used by effects to compute textu re coords via a matrix. |
| 320 */ | 314 */ |
| 321 const GrGLShaderVar& positionAttribute() const { return *fPositionVar; } | 315 const GrGLShaderVar& positionAttribute() const { return *fPositionVar; } |
| 322 | 316 |
| 323 /** Returns a vertex attribute that represents the local coords in the V S. This may be the same | 317 /** Returns a vertex attribute that represents the local coords in the V S. This may be the same |
| 324 as positionAttribute() or it may not be. It depends upon whether the rendering code | 318 as positionAttribute() or it may not be. It depends upon whether the rendering code |
| 325 specified explicit local coords or not in the GrDrawState. */ | 319 specified explicit local coords or not in the GrDrawState. */ |
| 326 const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoords Var; } | 320 const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoords Var; } |
| 327 | 321 |
| 328 /** | 322 /** |
| 329 * Are explicit local coordinates provided as input to the vertex shader . | 323 * Are explicit local coordinates provided as input to the vertex shader . |
| 330 */ | 324 */ |
| 331 bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositi onVar); } | 325 bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositi onVar); } |
| 332 | 326 |
| 333 /** Called after building is complete to get the final shader string. */ | 327 bool addEffectAttribute(int attributeIndex, GrSLType type, const SkStrin g& name); |
| 334 void vsGetShader(SkString*) const; | 328 const SkString* getEffectAttributeName(int attributeIndex) const; |
| 335 void gsGetShader(SkString*) const; | 329 |
| 330 GrGLUniformManager::UniformHandle getViewMatrixUniform() const { | |
| 331 return fViewMatrixUniform; | |
| 332 } | |
| 333 | |
| 334 bool compileAndAttachShaders(GrGLuint programId) const; | |
| 335 void bindProgramLocations(GrGLuint programId) const; | |
| 336 | |
| 337 private: | |
| 338 GrGLShaderBuilder* fParent; | |
| 339 GrGpuGL* fGpu; | |
| 340 const GrGLProgramDesc& fDesc; | |
| 341 VarArray fVSAttrs; | |
| 342 VarArray fVSOutputs; | |
| 343 VarArray fGSInputs; | |
| 344 VarArray fGSOutputs; | |
| 345 | |
| 346 SkString fVSCode; | |
| 336 | 347 |
| 337 struct AttributePair { | 348 struct AttributePair { |
| 338 void set(int index, const SkString& name) { | 349 void set(int index, const SkString& name) { |
| 339 fIndex = index; fName = name; | 350 fIndex = index; fName = name; |
| 340 } | 351 } |
| 341 int fIndex; | 352 int fIndex; |
| 342 SkString fName; | 353 SkString fName; |
| 343 }; | 354 }; |
| 344 const SkTArray<AttributePair, true>& getEffectAttributes() const { | 355 SkSTArray<10, AttributePair, true> fEffectAttributes; |
| 345 return fEffectAttributes; | |
| 346 } | |
| 347 bool addEffectAttribute(int attributeIndex, GrSLType type, const SkStrin g& name); | |
| 348 const SkString* getEffectAttributeName(int attributeIndex) const; | |
| 349 | 356 |
| 350 // TODO: Everything below here private. | 357 GrGLUniformManager::UniformHandle fViewMatrixUniform; |
| 351 public: | |
| 352 | |
| 353 VarArray fVSAttrs; | |
| 354 VarArray fVSOutputs; | |
| 355 VarArray fGSInputs; | |
| 356 VarArray fGSOutputs; | |
| 357 SkString fGSHeader; // layout qualifiers specific to GS | |
| 358 | |
| 359 private: | |
| 360 GrGLShaderBuilder* fParent; | |
| 361 | |
| 362 bool fUsesGS; | |
| 363 | |
| 364 SkString fVSCode; | |
| 365 SkString fGSCode; | |
| 366 | |
| 367 SkSTArray<10, AttributePair, true> fEffectAttributes; | |
| 368 | 358 |
| 369 GrGLShaderVar* fPositionVar; | 359 GrGLShaderVar* fPositionVar; |
| 370 GrGLShaderVar* fLocalCoordsVar; | 360 GrGLShaderVar* fLocalCoordsVar; |
| 371 }; | 361 }; |
| 372 | 362 |
| 373 /** Gets the vertex builder that is used to construct the vertex and geometr y shaders. | 363 /** Gets the vertex builder that is used to construct the vertex and geometr y shaders. |
| 374 It may be NULL if this shader program is only meant to have a fragment s hader. */ | 364 It may be NULL if this shader program is only meant to have a fragment s hader. */ |
| 375 VertexBuilder* getVertexBuilder() const { return fVertexBuilder.get(); } | 365 VertexBuilder* getVertexBuilder() const { return fVertexBuilder.get(); } |
| 376 | 366 |
| 377 // TODO: Make this do all the compiling, linking, etc. | 367 bool finish(GrGLuint* outProgramId); |
| 378 void finished(GrGLuint programID); | |
| 379 | 368 |
| 380 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } | 369 const GrGLContextInfo& ctxInfo() const; |
| 381 | 370 |
| 382 private: | 371 private: |
| 383 void appendDecls(const VarArray&, SkString*) const; | 372 void appendDecls(const VarArray&, SkString*) const; |
| 384 void appendUniformDecls(ShaderVisibility, SkString*) const; | 373 void appendUniformDecls(ShaderVisibility, SkString*) const; |
| 385 | 374 |
| 375 bool compileAndAttachShaders(GrGLuint programId) const; | |
| 376 void bindProgramLocations(GrGLuint programId) const; | |
| 377 | |
| 386 typedef GrGLUniformManager::BuilderUniform BuilderUniform; | 378 typedef GrGLUniformManager::BuilderUniform BuilderUniform; |
| 387 GrGLUniformManager::BuilderUniformArray fUniforms; | 379 GrGLUniformManager::BuilderUniformArray fUniforms; |
| 388 | 380 |
| 389 private: | 381 private: |
| 390 class CodeStage : public SkNoncopyable { | 382 class CodeStage : public SkNoncopyable { |
| 391 public: | 383 public: |
| 392 CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {} | 384 CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {} |
| 393 | 385 |
| 394 bool inStageCode() const { | 386 bool inStageCode() const { |
| 395 this->validate(); | 387 this->validate(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. | 456 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
| 465 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-le ft. | 457 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-le ft. |
| 466 }; | 458 }; |
| 467 | 459 |
| 468 enum { | 460 enum { |
| 469 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil l not be needed. | 461 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil l not be needed. |
| 470 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t op-left. | 462 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t op-left. |
| 471 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b ottom-left. | 463 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b ottom-left. |
| 472 }; | 464 }; |
| 473 | 465 |
| 474 const GrGLContextInfo& fCtxInfo; | 466 GrGpuGL* fGpu; |
| 475 GrGLUniformManager& fUniformManager; | 467 GrGLUniformManager& fUniformManager; |
| 476 uint32_t fFSFeaturesAddedMask; | 468 uint32_t fFSFeaturesAddedMask; |
| 477 SkString fFSFunctions; | 469 SkString fFSFunctions; |
| 478 SkString fFSExtensions; | 470 SkString fFSExtensions; |
| 479 VarArray fFSInputs; | 471 VarArray fFSInputs; |
| 480 VarArray fFSOutputs; | 472 VarArray fFSOutputs; |
| 481 | 473 |
| 482 SkString fFSCode; | 474 SkString fFSCode; |
| 483 | 475 |
| 484 bool fSetupFragPosition; | 476 bool fSetupFragPosition; |
| 485 TextureSampler fDstCopySampler; | 477 TextureSampler fDstCopySampler; |
| 486 | 478 |
| 479 SkString fInputColor; | |
| 480 GrSLConstantVec fKnownColorValue; | |
| 481 SkString fInputCoverage; | |
| 482 GrSLConstantVec fKnownCoverageValue; | |
| 483 | |
| 484 bool fHasCustomColorOutput; | |
| 485 bool fHasSecondaryOutput; | |
| 486 | |
| 487 GrGLUniformManager::UniformHandle fRTHeightUniform; | 487 GrGLUniformManager::UniformHandle fRTHeightUniform; |
| 488 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; | 488 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; |
| 489 GrGLUniformManager::UniformHandle fDstCopyScaleUniform; | 489 GrGLUniformManager::UniformHandle fDstCopyScaleUniform; |
| 490 GrGLUniformManager::UniformHandle fColorUniform; | |
| 491 GrGLUniformManager::UniformHandle fCoverageUniform; | |
| 490 | 492 |
| 491 bool fTopLeftFragPosRead; | 493 bool fTopLeftFragPosRead; |
| 492 | 494 |
| 493 SkAutoTDelete<VertexBuilder> fVertexBuilder; | 495 SkAutoTDelete<VertexBuilder> fVertexBuilder; |
| 494 }; | 496 }; |
| 495 | 497 |
| 496 #endif | 498 #endif |
| OLD | NEW |