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