| 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 #include "gl/GrGLShaderBuilder.h" | 8 #include "gl/GrGLShaderBuilder.h" |
| 9 #include "gl/GrGLProgram.h" | 9 #include "gl/GrGLProgram.h" |
| 10 #include "gl/GrGLUniformHandle.h" | 10 #include "gl/GrGLUniformHandle.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 } | 88 } |
| 89 | 89 |
| 90 static const char kDstCopyColorName[] = "_dstColor"; | 90 static const char kDstCopyColorName[] = "_dstColor"; |
| 91 | 91 |
| 92 /////////////////////////////////////////////////////////////////////////////// | 92 /////////////////////////////////////////////////////////////////////////////// |
| 93 | 93 |
| 94 GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctxInfo, | 94 GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctxInfo, |
| 95 GrGLUniformManager& uniformManager, | 95 GrGLUniformManager& uniformManager, |
| 96 const GrGLProgramDesc& desc, | 96 const GrGLProgramDesc& desc, |
| 97 bool needsVertexShader) | 97 bool requiresVertexShader) |
| 98 : fUniforms(kVarsPerBlock) | 98 : fUniforms(kVarsPerBlock) |
| 99 , fCtxInfo(ctxInfo) | 99 , fCtxInfo(ctxInfo) |
| 100 , fUniformManager(uniformManager) | 100 , fUniformManager(uniformManager) |
| 101 , fFSFeaturesAddedMask(0) | 101 , fFSFeaturesAddedMask(0) |
| 102 , fFSInputs(kVarsPerBlock) | 102 , fFSInputs(kVarsPerBlock) |
| 103 , fFSOutputs(kMaxFSOutputs) | 103 , fFSOutputs(kMaxFSOutputs) |
| 104 , fSetupFragPosition(false) | 104 , fSetupFragPosition(false) |
| 105 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFr
agPosKey) { | 105 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFr
agPosKey) { |
| 106 | 106 |
| 107 const GrGLProgramDesc::KeyHeader& header = desc.getHeader(); | 107 const GrGLProgramDesc::KeyHeader& header = desc.getHeader(); |
| 108 | 108 |
| 109 if (needsVertexShader) { | 109 if (requiresVertexShader) { |
| 110 fVertexBuilder.reset(SkNEW_ARGS(VertexBuilder, (this, desc))); | 110 fVertexBuilder.reset(SkNEW_ARGS(VertexBuilder, (this, desc))); |
| 111 } | 111 } |
| 112 | 112 |
| 113 // Emit code to read the dst copy textue if necessary. | 113 // Emit code to read the dst copy textue if necessary. |
| 114 if (kNoDstRead_DstReadKey != header.fDstReadKey && | 114 if (kNoDstRead_DstReadKey != header.fDstReadKey && |
| 115 GrGLCaps::kNone_FBFetchType == ctxInfo.caps()->fbFetchType()) { | 115 GrGLCaps::kNone_FBFetchType == ctxInfo.caps()->fbFetchType()) { |
| 116 bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & header.fDstReadKe
y); | 116 bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & header.fDstReadKe
y); |
| 117 const char* dstCopyTopLeftName; | 117 const char* dstCopyTopLeftName; |
| 118 const char* dstCopyCoordScaleName; | 118 const char* dstCopyCoordScaleName; |
| 119 uint32_t configMask; | 119 uint32_t configMask; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 if (out->endsWith('_')) { | 210 if (out->endsWith('_')) { |
| 211 // Names containing "__" are reserved. | 211 // Names containing "__" are reserved. |
| 212 out->append("x"); | 212 out->append("x"); |
| 213 } | 213 } |
| 214 out->appendf("_Stage%d", fCodeStage.stageIndex()); | 214 out->appendf("_Stage%d", fCodeStage.stageIndex()); |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 const char* GrGLShaderBuilder::dstColor() { | 218 const char* GrGLShaderBuilder::dstColor() { |
| 219 if (fCodeStage.inStageCode()) { | 219 if (fCodeStage.inStageCode()) { |
| 220 const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); | 220 const GrEffectRef& effect = *fCodeStage.effect(); |
| 221 if (!effect->willReadDstColor()) { | 221 if (!effect->willReadDstColor()) { |
| 222 GrDebugCrash("GrGLEffect asked for dst color but its generating GrEf
fect " | 222 GrDebugCrash("GrGLEffect asked for dst color but its generating GrEf
fect " |
| 223 "did not request access."); | 223 "did not request access."); |
| 224 return ""; | 224 return ""; |
| 225 } | 225 } |
| 226 } | 226 } |
| 227 static const char kFBFetchColorName[] = "gl_LastFragData[0]"; | 227 static const char kFBFetchColorName[] = "gl_LastFragData[0]"; |
| 228 GrGLCaps::FBFetchType fetchType = fCtxInfo.caps()->fbFetchType(); | 228 GrGLCaps::FBFetchType fetchType = fCtxInfo.caps()->fbFetchType(); |
| 229 if (GrGLCaps::kEXT_FBFetchType == fetchType) { | 229 if (GrGLCaps::kEXT_FBFetchType == fetchType) { |
| 230 SkAssertResult(this->enablePrivateFeature(kEXTShaderFramebufferFetch_GLS
LPrivateFeature)); | 230 SkAssertResult(this->enablePrivateFeature(kEXTShaderFramebufferFetch_GLS
LPrivateFeature)); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 | 356 |
| 357 if (NULL != outName) { | 357 if (NULL != outName) { |
| 358 *outName = uni.fVariable.c_str(); | 358 *outName = uni.fVariable.c_str(); |
| 359 } | 359 } |
| 360 | 360 |
| 361 return h; | 361 return h; |
| 362 } | 362 } |
| 363 | 363 |
| 364 const char* GrGLShaderBuilder::fragmentPosition() { | 364 const char* GrGLShaderBuilder::fragmentPosition() { |
| 365 if (fCodeStage.inStageCode()) { | 365 if (fCodeStage.inStageCode()) { |
| 366 const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); | 366 const GrEffectRef& effect = *fCodeStage.effect(); |
| 367 if (!effect->willReadFragmentPosition()) { | 367 if (!effect->willReadFragmentPosition()) { |
| 368 GrDebugCrash("GrGLEffect asked for frag position but its generating
GrEffect " | 368 GrDebugCrash("GrGLEffect asked for frag position but its generating
GrEffect " |
| 369 "did not request access."); | 369 "did not request access."); |
| 370 return ""; | 370 return ""; |
| 371 } | 371 } |
| 372 } | 372 } |
| 373 if (fTopLeftFragPosRead) { | 373 if (fTopLeftFragPosRead) { |
| 374 if (!fSetupFragPosition) { | 374 if (!fSetupFragPosition) { |
| 375 fFSInputs.push_back().set(kVec4f_GrSLType, | 375 fFSInputs.push_back().set(kVec4f_GrSLType, |
| 376 GrGLShaderVar::kIn_TypeModifier, | 376 GrGLShaderVar::kIn_TypeModifier, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 shaderStr->append("void main() {\n"); | 493 shaderStr->append("void main() {\n"); |
| 494 shaderStr->append(fFSCode); | 494 shaderStr->append(fFSCode); |
| 495 shaderStr->append("}\n"); | 495 shaderStr->append("}\n"); |
| 496 } | 496 } |
| 497 | 497 |
| 498 void GrGLShaderBuilder::finished(GrGLuint programID) { | 498 void GrGLShaderBuilder::finished(GrGLuint programID) { |
| 499 fUniformManager.getUniformLocations(programID, fUniforms); | 499 fUniformManager.getUniformLocations(programID, fUniforms); |
| 500 } | 500 } |
| 501 | 501 |
| 502 void GrGLShaderBuilder::emitEffects( | 502 void GrGLShaderBuilder::emitEffects( |
| 503 const GrEffectStage* effectStages[], | 503 GrGLEffect* const glEffects[], |
| 504 const GrBackendEffectFactory::EffectKey effectKeys[], | 504 GrDrawEffect const drawEffects[], |
| 505 GrBackendEffectFactory::EffectKey const effectKeys[], |
| 505 int effectCnt, | 506 int effectCnt, |
| 506 SkString* fsInOutColor, | 507 SkString* fsInOutColor, |
| 507 GrSLConstantVec* fsInOutColorKnownValue, | 508 GrSLConstantVec* fsInOutColorKnownValue, |
| 508 SkTArray<GrGLUniformManager::UniformHandle, true>* effec
tSamplerHandles[], | 509 SkTArray<GrGLUniformManager::UniformHandle, true>* effec
tSamplerHandles[]) { |
| 509 GrGLEffect* glEffects[]) { | |
| 510 bool effectEmitted = false; | 510 bool effectEmitted = false; |
| 511 | 511 |
| 512 SkString inColor = *fsInOutColor; | 512 SkString inColor = *fsInOutColor; |
| 513 SkString outColor; | 513 SkString outColor; |
| 514 | 514 |
| 515 for (int e = 0; e < effectCnt; ++e) { | 515 for (int e = 0; e < effectCnt; ++e) { |
| 516 SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()
); | 516 const GrDrawEffect& drawEffect = drawEffects[e]; |
| 517 const GrEffectStage& stage = *effectStages[e]; | 517 const GrEffectRef& effect = *drawEffect.effect(); |
| 518 const GrEffectRef& effect = *stage.getEffect(); | |
| 519 | 518 |
| 520 CodeStage::AutoStageRestore csar(&fCodeStage, &stage); | 519 CodeStage::AutoStageRestore csar(&fCodeStage, &effect); |
| 521 | 520 |
| 522 int numTextures = effect->numTextures(); | 521 int numTextures = effect->numTextures(); |
| 523 SkSTArray<8, GrGLShaderBuilder::TextureSampler> textureSamplers; | 522 SkSTArray<8, GrGLShaderBuilder::TextureSampler> textureSamplers; |
| 524 textureSamplers.push_back_n(numTextures); | 523 textureSamplers.push_back_n(numTextures); |
| 525 for (int t = 0; t < numTextures; ++t) { | 524 for (int t = 0; t < numTextures; ++t) { |
| 526 textureSamplers[t].init(this, &effect->textureAccess(t), t); | 525 textureSamplers[t].init(this, &effect->textureAccess(t), t); |
| 527 effectSamplerHandles[e]->push_back(textureSamplers[t].fSamplerUnifor
m); | 526 effectSamplerHandles[e]->push_back(textureSamplers[t].fSamplerUnifor
m); |
| 528 } | 527 } |
| 529 GrDrawEffect drawEffect(stage, fVertexBuilder.get() | |
| 530 && fVertexBuilder->hasExplicitLocalCoords
()); | |
| 531 | 528 |
| 532 int numAttributes = stage.getVertexAttribIndexCount(); | 529 int numAttributes = drawEffect.getVertexAttribIndexCount(); |
| 533 const int* attributeIndices = stage.getVertexAttribIndices(); | 530 const int* attributeIndices = drawEffect.getVertexAttribIndices(); |
| 534 SkSTArray<GrEffect::kMaxVertexAttribs, SkString> attributeNames; | 531 SkSTArray<GrEffect::kMaxVertexAttribs, SkString> attributeNames; |
| 535 for (int a = 0; a < numAttributes; ++a) { | 532 for (int a = 0; a < numAttributes; ++a) { |
| 536 // TODO: Make addAttribute mangle the name. | 533 // TODO: Make addAttribute mangle the name. |
| 537 SkASSERT(fVertexBuilder.get()); | 534 SkASSERT(fVertexBuilder.get()); |
| 538 SkString attributeName("aAttr"); | 535 SkString attributeName("aAttr"); |
| 539 attributeName.appendS32(attributeIndices[a]); | 536 attributeName.appendS32(attributeIndices[a]); |
| 540 fVertexBuilder->addEffectAttribute(attributeIndices[a], | 537 fVertexBuilder->addEffectAttribute(attributeIndices[a], |
| 541 effect->vertexAttribType(a), | 538 effect->vertexAttribType(a), |
| 542 attributeName); | 539 attributeName); |
| 543 } | 540 } |
| 544 | 541 |
| 545 glEffects[e] = effect->getFactory().createGLInstance(drawEffect); | |
| 546 | |
| 547 if (kZeros_GrSLConstantVec == *fsInOutColorKnownValue) { | 542 if (kZeros_GrSLConstantVec == *fsInOutColorKnownValue) { |
| 548 // Effects have no way to communicate zeros, they treat an empty str
ing as ones. | 543 // Effects have no way to communicate zeros, they treat an empty str
ing as ones. |
| 549 this->nameVariable(&inColor, '\0', "input"); | 544 this->nameVariable(&inColor, '\0', "input"); |
| 550 this->fsCodeAppendf("\tvec4 %s = %s;\n", inColor.c_str(), GrGLSLZero
sVecf(4)); | 545 this->fsCodeAppendf("\tvec4 %s = %s;\n", inColor.c_str(), GrGLSLZero
sVecf(4)); |
| 551 } | 546 } |
| 552 | 547 |
| 553 // create var to hold stage result | 548 // create var to hold stage result |
| 554 this->nameVariable(&outColor, '\0', "output"); | 549 this->nameVariable(&outColor, '\0', "output"); |
| 555 this->fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); | 550 this->fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); |
| 556 | 551 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 for (const AttributePair* attrib = this->getEffectAttributes().begin(); | 704 for (const AttributePair* attrib = this->getEffectAttributes().begin(); |
| 710 attrib != attribEnd; | 705 attrib != attribEnd; |
| 711 ++attrib) { | 706 ++attrib) { |
| 712 if (attrib->fIndex == attributeIndex) { | 707 if (attrib->fIndex == attributeIndex) { |
| 713 return &attrib->fName; | 708 return &attrib->fName; |
| 714 } | 709 } |
| 715 } | 710 } |
| 716 | 711 |
| 717 return NULL; | 712 return NULL; |
| 718 } | 713 } |
| OLD | NEW |