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