| 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" |
| 11 #include "GrCoordTransform.h" | 11 #include "GrCoordTransform.h" |
| 12 #include "GrDrawEffect.h" | 12 #include "GrDrawEffect.h" |
| 13 #include "GrGpuGL.h" | 13 #include "GrGpuGL.h" |
| 14 #include "GrTexture.h" | 14 #include "GrTexture.h" |
| 15 #include "SkRTConf.h" | 15 #include "SkRTConf.h" |
| 16 #include "SkTrace.h" | 16 #include "SkTrace.h" |
| 17 | 17 |
| 18 #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X) | 18 #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 19 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->glInterface(), R, X) | 19 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| 20 | 20 |
| 21 // number of each input/output type in a single allocation block | 21 // number of each input/output type in a single allocation block |
| 22 static const int kVarsPerBlock = 8; | 22 static const int kVarsPerBlock = 8; |
| 23 | 23 |
| 24 // except FS outputs where we expect 2 at most. | 24 // except FS outputs where we expect 2 at most. |
| 25 static const int kMaxFSOutputs = 2; | 25 static const int kMaxFSOutputs = 2; |
| 26 | 26 |
| 27 // ES2 FS only guarantees mediump and lowp support | 27 // ES2 FS only guarantees mediump and lowp support |
| 28 static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar:
:kMedium_Precision; | 28 static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar:
:kMedium_Precision; |
| 29 | 29 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 } | 84 } |
| 85 | 85 |
| 86 } | 86 } |
| 87 | 87 |
| 88 static const char kDstCopyColorName[] = "_dstColor"; | 88 static const char kDstCopyColorName[] = "_dstColor"; |
| 89 | 89 |
| 90 /////////////////////////////////////////////////////////////////////////////// | 90 /////////////////////////////////////////////////////////////////////////////// |
| 91 | 91 |
| 92 GrGLShaderBuilder::GrGLShaderBuilder(GrGpuGL* gpu, | 92 GrGLShaderBuilder::GrGLShaderBuilder(GrGpuGL* gpu, |
| 93 GrGLUniformManager& uniformManager, | 93 GrGLUniformManager& uniformManager, |
| 94 const GrGLProgramDesc& desc, | 94 const GrGLProgramDesc& desc) |
| 95 bool needsVertexShader) | 95 : fGpu(gpu) |
| 96 : fUniforms(kVarsPerBlock) | |
| 97 , fGpu(gpu) | |
| 98 , fUniformManager(uniformManager) | 96 , fUniformManager(uniformManager) |
| 99 , fFSFeaturesAddedMask(0) | 97 , fFSFeaturesAddedMask(0) |
| 100 , fFSInputs(kVarsPerBlock) | 98 , fFSInputs(kVarsPerBlock) |
| 101 , fFSOutputs(kMaxFSOutputs) | 99 , fFSOutputs(kMaxFSOutputs) |
| 100 , fUniforms(kVarsPerBlock) |
| 102 , fSetupFragPosition(false) | 101 , fSetupFragPosition(false) |
| 103 , fKnownColorValue(kNone_GrSLConstantVec) | 102 , fKnownColorValue(GrGLProgramDesc::KnownColorInputValue(desc.getHeader().fC
olorInput)) |
| 104 , fKnownCoverageValue(kNone_GrSLConstantVec) | 103 , fKnownCoverageValue(GrGLProgramDesc::KnownColorInputValue(desc.getHeader()
.fCoverageInput)) |
| 105 , fHasCustomColorOutput(false) | 104 , fHasCustomColorOutput(false) |
| 106 , fHasSecondaryOutput(false) | 105 , fHasSecondaryOutput(false) |
| 107 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFr
agPosKey) { | 106 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFr
agPosKey) { |
| 108 | 107 |
| 109 const GrGLProgramDesc::KeyHeader& header = desc.getHeader(); | 108 const GrGLProgramDesc::KeyHeader& header = desc.getHeader(); |
| 110 | 109 |
| 111 if (needsVertexShader) { | |
| 112 fVertexBuilder.reset(SkNEW_ARGS(VertexBuilder, (this, fGpu, desc))); | |
| 113 } | |
| 114 | |
| 115 // Emit code to read the dst copy textue if necessary. | 110 // Emit code to read the dst copy textue if necessary. |
| 116 if (kNoDstRead_DstReadKey != header.fDstReadKey && | 111 if (kNoDstRead_DstReadKey != header.fDstReadKey && |
| 117 GrGLCaps::kNone_FBFetchType == fGpu->glCaps().fbFetchType()) { | 112 GrGLCaps::kNone_FBFetchType == fGpu->glCaps().fbFetchType()) { |
| 118 bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & header.fDstReadKe
y); | 113 bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & header.fDstReadKe
y); |
| 119 const char* dstCopyTopLeftName; | 114 const char* dstCopyTopLeftName; |
| 120 const char* dstCopyCoordScaleName; | 115 const char* dstCopyCoordScaleName; |
| 121 uint32_t configMask; | 116 uint32_t configMask; |
| 122 if (SkToBool(kUseAlphaConfig_DstReadKeyBit & header.fDstReadKey)) { | 117 if (SkToBool(kUseAlphaConfig_DstReadKeyBit & header.fDstReadKey)) { |
| 123 configMask = kA_GrColorComponentFlag; | 118 configMask = kA_GrColorComponentFlag; |
| 124 } else { | 119 } else { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 145 this->fsCodeAppendf("\tvec4 %s = ", kDstCopyColorName); | 140 this->fsCodeAppendf("\tvec4 %s = ", kDstCopyColorName); |
| 146 append_texture_lookup(&fFSCode, | 141 append_texture_lookup(&fFSCode, |
| 147 fGpu, | 142 fGpu, |
| 148 this->getUniformCStr(fDstCopySamplerUniform), | 143 this->getUniformCStr(fDstCopySamplerUniform), |
| 149 "_dstTexCoord", | 144 "_dstTexCoord", |
| 150 configMask, | 145 configMask, |
| 151 "rgba"); | 146 "rgba"); |
| 152 this->fsCodeAppend(";\n\n"); | 147 this->fsCodeAppend(";\n\n"); |
| 153 } | 148 } |
| 154 | 149 |
| 155 switch (header.fColorInput) { | 150 if (GrGLProgramDesc::kUniform_ColorInput == header.fColorInput) { |
| 156 case GrGLProgramDesc::kAttribute_ColorInput: { | 151 const char* name; |
| 157 SkASSERT(NULL != fVertexBuilder.get()); | 152 fColorUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibility
, |
| 158 fVertexBuilder->addAttribute(kVec4f_GrSLType, color_attribute_name()
); | 153 kVec4f_GrSLType, "Color", &name); |
| 159 const char *vsName, *fsName; | 154 fInputColor = name; |
| 160 fVertexBuilder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsNam
e); | |
| 161 fVertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, color_attribut
e_name()); | |
| 162 fInputColor = fsName; | |
| 163 break; | |
| 164 } | |
| 165 case GrGLProgramDesc::kUniform_ColorInput: { | |
| 166 const char* name; | |
| 167 fColorUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, | |
| 168 kVec4f_GrSLType, "Color", &name); | |
| 169 fInputColor = name; | |
| 170 break; | |
| 171 } | |
| 172 case GrGLProgramDesc::kTransBlack_ColorInput: | |
| 173 fKnownColorValue = kZeros_GrSLConstantVec; | |
| 174 break; | |
| 175 case GrGLProgramDesc::kSolidWhite_ColorInput: | |
| 176 fKnownColorValue = kOnes_GrSLConstantVec; | |
| 177 break; | |
| 178 default: | |
| 179 GrCrash("Unknown color type."); | |
| 180 } | 155 } |
| 181 | 156 |
| 182 switch (header.fCoverageInput) { | 157 if (GrGLProgramDesc::kUniform_ColorInput == header.fCoverageInput) { |
| 183 case GrGLProgramDesc::kAttribute_ColorInput: { | 158 const char* name; |
| 184 SkASSERT(NULL != fVertexBuilder.get()); | 159 fCoverageUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibil
ity, |
| 185 fVertexBuilder->addAttribute(kVec4f_GrSLType, coverage_attribute_nam
e()); | 160 kVec4f_GrSLType, "Coverage", &name); |
| 186 const char *vsName, *fsName; | 161 fInputCoverage = name; |
| 187 fVertexBuilder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fs
Name); | |
| 188 fVertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, coverage_attri
bute_name()); | |
| 189 fInputCoverage = fsName; | |
| 190 break; | |
| 191 } | |
| 192 case GrGLProgramDesc::kUniform_ColorInput: { | |
| 193 const char* name; | |
| 194 fCoverageUniform = this->addUniform(GrGLShaderBuilder::kFragment_Vis
ibility, | |
| 195 kVec4f_GrSLType, "Coverage", &na
me); | |
| 196 fInputCoverage = name; | |
| 197 break; | |
| 198 } | |
| 199 case GrGLProgramDesc::kTransBlack_ColorInput: | |
| 200 fKnownCoverageValue = kZeros_GrSLConstantVec; | |
| 201 break; | |
| 202 case GrGLProgramDesc::kSolidWhite_ColorInput: | |
| 203 fKnownCoverageValue = kOnes_GrSLConstantVec; | |
| 204 break; | |
| 205 default: | |
| 206 GrCrash("Unknown coverage type."); | |
| 207 } | 162 } |
| 208 | 163 |
| 209 if (k110_GrGLSLGeneration != fGpu->glslGeneration()) { | 164 if (k110_GrGLSLGeneration != fGpu->glslGeneration()) { |
| 210 fFSOutputs.push_back().set(kVec4f_GrSLType, | 165 fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 211 GrGLShaderVar::kOut_TypeModifier, | 166 GrGLShaderVar::kOut_TypeModifier, |
| 212 declared_color_output_name()); | 167 declared_color_output_name()); |
| 213 fHasCustomColorOutput = true; | 168 fHasCustomColorOutput = true; |
| 214 } | 169 } |
| 215 } | 170 } |
| 216 | 171 |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 void GrGLShaderBuilder::appendUniformDecls(ShaderVisibility visibility, | 499 void GrGLShaderBuilder::appendUniformDecls(ShaderVisibility visibility, |
| 545 SkString* out) const { | 500 SkString* out) const { |
| 546 for (int i = 0; i < fUniforms.count(); ++i) { | 501 for (int i = 0; i < fUniforms.count(); ++i) { |
| 547 if (fUniforms[i].fVisibility & visibility) { | 502 if (fUniforms[i].fVisibility & visibility) { |
| 548 fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); | 503 fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
| 549 out->append(";\n"); | 504 out->append(";\n"); |
| 550 } | 505 } |
| 551 } | 506 } |
| 552 } | 507 } |
| 553 | 508 |
| 554 GrGLEffectArray* GrGLShaderBuilder::emitEffects(const GrEffectStage* effectStage
s[], | 509 void GrGLShaderBuilder::emitEffects(GrGLEffectArrayBuilder* effectArrayBuilder, |
| 555 const EffectKey effectKeys[], | 510 const GrEffectStage* effectStages[], |
| 556 int effectCnt, | 511 const EffectKey effectKeys[], |
| 557 SkString* fsInOutColor, | 512 int effectCnt, |
| 558 GrSLConstantVec* fsInOutColorKno
wnValue) { | 513 SkString* fsInOutColor, |
| 559 GrGLEffectArrayBuilder effectArrayBuilder(this, effectCnt); | 514 GrSLConstantVec* fsInOutColorKnownValue) { |
| 560 bool effectEmitted = false; | 515 bool effectEmitted = false; |
| 561 | 516 |
| 562 SkString inColor = *fsInOutColor; | 517 SkString inColor = *fsInOutColor; |
| 563 SkString outColor; | 518 SkString outColor; |
| 564 | 519 |
| 565 for (int e = 0; e < effectCnt; ++e) { | 520 for (int e = 0; e < effectCnt; ++e) { |
| 566 SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()
); | 521 SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()
); |
| 567 const GrEffectStage& stage = *effectStages[e]; | 522 const GrEffectStage& stage = *effectStages[e]; |
| 568 | 523 |
| 569 CodeStage::AutoStageRestore csar(&fCodeStage, &stage); | 524 CodeStage::AutoStageRestore csar(&fCodeStage, &stage); |
| 570 | 525 |
| 571 if (kZeros_GrSLConstantVec == *fsInOutColorKnownValue) { | 526 if (kZeros_GrSLConstantVec == *fsInOutColorKnownValue) { |
| 572 // Effects have no way to communicate zeros, they treat an empty str
ing as ones. | 527 // Effects have no way to communicate zeros, they treat an empty str
ing as ones. |
| 573 this->nameVariable(&inColor, '\0', "input"); | 528 this->nameVariable(&inColor, '\0', "input"); |
| 574 this->fsCodeAppendf("\tvec4 %s = %s;\n", inColor.c_str(), GrGLSLZero
sVecf(4)); | 529 this->fsCodeAppendf("\tvec4 %s = %s;\n", inColor.c_str(), GrGLSLZero
sVecf(4)); |
| 575 } | 530 } |
| 576 | 531 |
| 577 // create var to hold stage result | 532 // create var to hold stage result |
| 578 this->nameVariable(&outColor, '\0', "output"); | 533 this->nameVariable(&outColor, '\0', "output"); |
| 579 this->fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); | 534 this->fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); |
| 580 | 535 |
| 581 effectArrayBuilder.emitEffect(stage, | 536 effectArrayBuilder->emitEffect(stage, |
| 582 effectKeys[e], | 537 effectKeys[e], |
| 583 outColor.c_str(), | 538 outColor.c_str(), |
| 584 inColor.isEmpty() ? NULL : inColor.c_str()
, | 539 inColor.isEmpty() ? NULL : inColor.c_str(
), |
| 585 fCodeStage.stageIndex()); | 540 fCodeStage.stageIndex()); |
| 586 | 541 |
| 587 inColor = outColor; | 542 inColor = outColor; |
| 588 *fsInOutColorKnownValue = kNone_GrSLConstantVec; | 543 *fsInOutColorKnownValue = kNone_GrSLConstantVec; |
| 589 effectEmitted = true; | 544 effectEmitted = true; |
| 590 } | 545 } |
| 591 | 546 |
| 592 if (effectEmitted) { | 547 if (effectEmitted) { |
| 593 *fsInOutColor = outColor; | 548 *fsInOutColor = outColor; |
| 594 } | 549 } |
| 595 | |
| 596 return effectArrayBuilder.finish(); | |
| 597 } | 550 } |
| 598 | 551 |
| 599 const char* GrGLShaderBuilder::getColorOutputName() const { | 552 const char* GrGLShaderBuilder::getColorOutputName() const { |
| 600 return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"
; | 553 return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"
; |
| 601 } | 554 } |
| 602 | 555 |
| 603 const char* GrGLShaderBuilder::enableSecondaryOutput() { | 556 const char* GrGLShaderBuilder::enableSecondaryOutput() { |
| 604 if (!fHasSecondaryOutput) { | 557 if (!fHasSecondaryOutput) { |
| 605 fFSOutputs.push_back().set(kVec4f_GrSLType, | 558 fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 606 GrGLShaderVar::kOut_TypeModifier, | 559 GrGLShaderVar::kOut_TypeModifier, |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 } | 650 } |
| 698 | 651 |
| 699 GR_GL_CALL(gli, AttachShader(programId, shaderId)); | 652 GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
| 700 GR_GL_CALL(gli, DeleteShader(shaderId)); | 653 GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 701 return true; | 654 return true; |
| 702 } | 655 } |
| 703 | 656 |
| 704 } | 657 } |
| 705 | 658 |
| 706 bool GrGLShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { | 659 bool GrGLShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { |
| 707 if (NULL != fVertexBuilder.get() && !fVertexBuilder->compileAndAttachShaders
(programId)) { | |
| 708 return false; | |
| 709 } | |
| 710 | |
| 711 SkString fragShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); | 660 SkString fragShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 712 fragShaderSrc.append(fFSExtensions); | 661 fragShaderSrc.append(fFSExtensions); |
| 713 append_default_precision_qualifier(kDefaultFragmentPrecision, | 662 append_default_precision_qualifier(kDefaultFragmentPrecision, |
| 714 fGpu->glBinding(), | 663 fGpu->glBinding(), |
| 715 &fragShaderSrc); | 664 &fragShaderSrc); |
| 716 this->appendUniformDecls(kFragment_Visibility, &fragShaderSrc); | 665 this->appendUniformDecls(kFragment_Visibility, &fragShaderSrc); |
| 717 this->appendDecls(fFSInputs, &fragShaderSrc); | 666 this->appendDecls(fFSInputs, &fragShaderSrc); |
| 718 // We shouldn't have declared outputs on 1.10 | 667 // We shouldn't have declared outputs on 1.10 |
| 719 SkASSERT(k110_GrGLSLGeneration != fGpu->glslGeneration() || fFSOutputs.empty
()); | 668 SkASSERT(k110_GrGLSLGeneration != fGpu->glslGeneration() || fFSOutputs.empty
()); |
| 720 this->appendDecls(fFSOutputs, &fragShaderSrc); | 669 this->appendDecls(fFSOutputs, &fragShaderSrc); |
| 721 fragShaderSrc.append(fFSFunctions); | 670 fragShaderSrc.append(fFSFunctions); |
| 722 fragShaderSrc.append("void main() {\n"); | 671 fragShaderSrc.append("void main() {\n"); |
| 723 fragShaderSrc.append(fFSCode); | 672 fragShaderSrc.append(fFSCode); |
| 724 fragShaderSrc.append("}\n"); | 673 fragShaderSrc.append("}\n"); |
| 725 if (!attach_shader(fGpu->glInterface(), programId, GR_GL_FRAGMENT_SHADER, fr
agShaderSrc)) { | 674 if (!attach_shader(fGpu->glInterface(), programId, GR_GL_FRAGMENT_SHADER, fr
agShaderSrc)) { |
| 726 return false; | 675 return false; |
| 727 } | 676 } |
| 728 | 677 |
| 729 return true; | 678 return true; |
| 730 } | 679 } |
| 731 | 680 |
| 732 void GrGLShaderBuilder::bindProgramLocations(GrGLuint programId) const { | 681 void GrGLShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
| 733 if (NULL != fVertexBuilder.get()) { | |
| 734 fVertexBuilder->bindProgramLocations(programId); | |
| 735 } | |
| 736 | |
| 737 if (fHasCustomColorOutput) { | 682 if (fHasCustomColorOutput) { |
| 738 GL_CALL(BindFragDataLocation(programId, 0, declared_color_output_name())
); | 683 GL_CALL(BindFragDataLocation(programId, 0, declared_color_output_name())
); |
| 739 } | 684 } |
| 740 if (fHasSecondaryOutput) { | 685 if (fHasSecondaryOutput) { |
| 741 GL_CALL(BindFragDataLocationIndexed(programId, 0, 1, dual_source_output_
name())); | 686 GL_CALL(BindFragDataLocationIndexed(programId, 0, 1, dual_source_output_
name())); |
| 742 } | 687 } |
| 743 } | 688 } |
| 744 | 689 |
| 745 const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const { | 690 const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const { |
| 746 return fGpu->ctxInfo(); | 691 return fGpu->ctxInfo(); |
| 747 } | 692 } |
| 748 | 693 |
| 749 //////////////////////////////////////////////////////////////////////////// | 694 //////////////////////////////////////////////////////////////////////////////// |
| 750 | 695 |
| 751 GrGLShaderBuilder::VertexBuilder::VertexBuilder(GrGLShaderBuilder* parent, | 696 GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGpuGL* gpu, |
| 752 GrGpuGL* gpu, | 697 GrGLUniformManager& uniformManager, |
| 753 const GrGLProgramDesc& desc) | 698 const GrGLProgramDesc& desc) |
| 754 : fParent(parent) | 699 : INHERITED(gpu, uniformManager, desc) |
| 755 , fGpu(gpu) | |
| 756 , fDesc(desc) | 700 , fDesc(desc) |
| 757 , fVSAttrs(kVarsPerBlock) | 701 , fVSAttrs(kVarsPerBlock) |
| 758 , fVSOutputs(kVarsPerBlock) | 702 , fVSOutputs(kVarsPerBlock) |
| 759 , fGSInputs(kVarsPerBlock) | 703 , fGSInputs(kVarsPerBlock) |
| 760 , fGSOutputs(kVarsPerBlock) { | 704 , fGSOutputs(kVarsPerBlock) { |
| 761 | 705 |
| 762 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); | 706 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 763 | 707 |
| 764 fPositionVar = &fVSAttrs.push_back(); | 708 fPositionVar = &fVSAttrs.push_back(); |
| 765 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
aPosition"); | 709 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
aPosition"); |
| 766 if (-1 != header.fLocalCoordAttributeIndex) { | 710 if (-1 != header.fLocalCoordAttributeIndex) { |
| 767 fLocalCoordsVar = &fVSAttrs.push_back(); | 711 fLocalCoordsVar = &fVSAttrs.push_back(); |
| 768 fLocalCoordsVar->set(kVec2f_GrSLType, | 712 fLocalCoordsVar->set(kVec2f_GrSLType, |
| 769 GrGLShaderVar::kAttribute_TypeModifier, | 713 GrGLShaderVar::kAttribute_TypeModifier, |
| 770 "aLocalCoords"); | 714 "aLocalCoords"); |
| 771 } else { | 715 } else { |
| 772 fLocalCoordsVar = fPositionVar; | 716 fLocalCoordsVar = fPositionVar; |
| 773 } | 717 } |
| 774 | 718 |
| 775 const char* viewMName; | 719 const char* viewMName; |
| 776 fViewMatrixUniform = fParent->addUniform(GrGLShaderBuilder::kVertex_Visibili
ty, | 720 fViewMatrixUniform = this->addUniform(GrGLShaderBuilder::kVertex_Visibility, |
| 777 kMat33f_GrSLType, "ViewM", &viewMNa
me); | 721 kMat33f_GrSLType, "ViewM", &viewMName)
; |
| 778 | 722 |
| 779 this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" | 723 this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
| 780 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", | 724 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
| 781 viewMName, fPositionVar->c_str()); | 725 viewMName, fPositionVar->c_str()); |
| 782 | 726 |
| 783 // we output point size in the GS if present | 727 // we output point size in the GS if present |
| 784 if (header.fEmitsPointSize | 728 if (header.fEmitsPointSize |
| 785 #if GR_GL_EXPERIMENTAL_GS | 729 #if GR_GL_EXPERIMENTAL_GS |
| 786 && !header.fExperimentalGS | 730 && !header.fExperimentalGS |
| 787 #endif | 731 #endif |
| 788 ) { | 732 ) { |
| 789 this->vsCodeAppend("\tgl_PointSize = 1.0;\n"); | 733 this->vsCodeAppend("\tgl_PointSize = 1.0;\n"); |
| 790 } | 734 } |
| 735 |
| 736 if (GrGLProgramDesc::kAttribute_ColorInput == header.fColorInput) { |
| 737 this->addAttribute(kVec4f_GrSLType, color_attribute_name()); |
| 738 const char *vsName, *fsName; |
| 739 this->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
| 740 this->vsCodeAppendf("\t%s = %s;\n", vsName, color_attribute_name()); |
| 741 this->setInputColor(fsName); |
| 742 } |
| 743 |
| 744 if (GrGLProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { |
| 745 this->addAttribute(kVec4f_GrSLType, coverage_attribute_name()); |
| 746 const char *vsName, *fsName; |
| 747 this->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
| 748 this->vsCodeAppendf("\t%s = %s;\n", vsName, coverage_attribute_name()); |
| 749 this->setInputCoverage(fsName); |
| 750 } |
| 791 } | 751 } |
| 792 | 752 |
| 793 bool GrGLShaderBuilder::VertexBuilder::addAttribute(GrSLType type, | 753 bool GrGLFullShaderBuilder::addAttribute(GrSLType type, const char* name) { |
| 794 const char* name) { | |
| 795 for (int i = 0; i < fVSAttrs.count(); ++i) { | 754 for (int i = 0; i < fVSAttrs.count(); ++i) { |
| 796 const GrGLShaderVar& attr = fVSAttrs[i]; | 755 const GrGLShaderVar& attr = fVSAttrs[i]; |
| 797 // if attribute already added, don't add it again | 756 // if attribute already added, don't add it again |
| 798 if (attr.getName().equals(name)) { | 757 if (attr.getName().equals(name)) { |
| 799 SkASSERT(attr.getType() == type); | 758 SkASSERT(attr.getType() == type); |
| 800 return false; | 759 return false; |
| 801 } | 760 } |
| 802 } | 761 } |
| 803 fVSAttrs.push_back().set(type, | 762 fVSAttrs.push_back().set(type, |
| 804 GrGLShaderVar::kAttribute_TypeModifier, | 763 GrGLShaderVar::kAttribute_TypeModifier, |
| 805 name); | 764 name); |
| 806 return true; | 765 return true; |
| 807 } | 766 } |
| 808 | 767 |
| 809 bool GrGLShaderBuilder::VertexBuilder::addEffectAttribute(int attributeIndex, | 768 bool GrGLFullShaderBuilder::addEffectAttribute(int attributeIndex, |
| 810 GrSLType type, | 769 GrSLType type, |
| 811 const SkString& name)
{ | 770 const SkString& name) { |
| 812 if (!this->addAttribute(type, name.c_str())) { | 771 if (!this->addAttribute(type, name.c_str())) { |
| 813 return false; | 772 return false; |
| 814 } | 773 } |
| 815 | 774 |
| 816 fEffectAttributes.push_back().set(attributeIndex, name); | 775 fEffectAttributes.push_back().set(attributeIndex, name); |
| 817 return true; | 776 return true; |
| 818 } | 777 } |
| 819 | 778 |
| 820 void GrGLShaderBuilder::VertexBuilder::addVarying(GrSLType type, | 779 void GrGLFullShaderBuilder::addVarying(GrSLType type, |
| 821 const char* name, | 780 const char* name, |
| 822 const char** vsOutName, | 781 const char** vsOutName, |
| 823 const char** fsInName) { | 782 const char** fsInName) { |
| 824 fVSOutputs.push_back(); | 783 fVSOutputs.push_back(); |
| 825 fVSOutputs.back().setType(type); | 784 fVSOutputs.back().setType(type); |
| 826 fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); | 785 fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| 827 fParent->nameVariable(fVSOutputs.back().accessName(), 'v', name); | 786 this->nameVariable(fVSOutputs.back().accessName(), 'v', name); |
| 828 | 787 |
| 829 if (vsOutName) { | 788 if (vsOutName) { |
| 830 *vsOutName = fVSOutputs.back().getName().c_str(); | 789 *vsOutName = fVSOutputs.back().getName().c_str(); |
| 831 } | 790 } |
| 832 // input to FS comes either from VS or GS | 791 // input to FS comes either from VS or GS |
| 833 const SkString* fsName; | 792 const SkString* fsName; |
| 834 #if GR_GL_EXPERIMENTAL_GS | 793 #if GR_GL_EXPERIMENTAL_GS |
| 835 if (fDesc.getHeader().fExperimentalGS) { | 794 if (fDesc.getHeader().fExperimentalGS) { |
| 836 // if we have a GS take each varying in as an array | 795 // if we have a GS take each varying in as an array |
| 837 // and output as non-array. | 796 // and output as non-array. |
| 838 fGSInputs.push_back(); | 797 fGSInputs.push_back(); |
| 839 fGSInputs.back().setType(type); | 798 fGSInputs.back().setType(type); |
| 840 fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier)
; | 799 fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier)
; |
| 841 fGSInputs.back().setUnsizedArray(); | 800 fGSInputs.back().setUnsizedArray(); |
| 842 *fGSInputs.back().accessName() = fVSOutputs.back().getName(); | 801 *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 843 fGSOutputs.push_back(); | 802 fGSOutputs.push_back(); |
| 844 fGSOutputs.back().setType(type); | 803 fGSOutputs.back().setType(type); |
| 845 fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifie
r); | 804 fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifie
r); |
| 846 fParent->nameVariable(fGSOutputs.back().accessName(), 'g', name); | 805 this->nameVariable(fGSOutputs.back().accessName(), 'g', name); |
| 847 fsName = fGSOutputs.back().accessName(); | 806 fsName = fGSOutputs.back().accessName(); |
| 848 } else | 807 } else |
| 849 #endif | 808 #endif |
| 850 { | 809 { |
| 851 fsName = fVSOutputs.back().accessName(); | 810 fsName = fVSOutputs.back().accessName(); |
| 852 } | 811 } |
| 853 fParent->fsInputAppend().set(type, | 812 this->fsInputAppend().set(type, GrGLShaderVar::kVaryingIn_TypeModifier, *fsN
ame); |
| 854 GrGLShaderVar::kVaryingIn_TypeModifier, | |
| 855 *fsName); | |
| 856 if (fsInName) { | 813 if (fsInName) { |
| 857 *fsInName = fsName->c_str(); | 814 *fsInName = fsName->c_str(); |
| 858 } | 815 } |
| 859 } | 816 } |
| 860 | 817 |
| 861 const SkString* GrGLShaderBuilder::VertexBuilder::getEffectAttributeName(int att
ributeIndex) const { | 818 const SkString* GrGLFullShaderBuilder::getEffectAttributeName(int attributeIndex
) const { |
| 862 const AttributePair* attribEnd = fEffectAttributes.end(); | 819 const AttributePair* attribEnd = fEffectAttributes.end(); |
| 863 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { | 820 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { |
| 864 if (attrib->fIndex == attributeIndex) { | 821 if (attrib->fIndex == attributeIndex) { |
| 865 return &attrib->fName; | 822 return &attrib->fName; |
| 866 } | 823 } |
| 867 } | 824 } |
| 868 | 825 |
| 869 return NULL; | 826 return NULL; |
| 870 } | 827 } |
| 871 | 828 |
| 872 bool GrGLShaderBuilder::VertexBuilder::compileAndAttachShaders(GrGLuint programI
d) const { | 829 GrGLEffectArray* GrGLFullShaderBuilder::emitEffects(const GrEffectStage* effectS
tages[], |
| 873 SkString vertShaderSrc(GrGetGLSLVersionDecl(fParent->ctxInfo())); | 830 const EffectKey effectKeys[]
, |
| 874 fParent->appendUniformDecls(kVertex_Visibility, &vertShaderSrc); | 831 int effectCnt, |
| 875 fParent->appendDecls(fVSAttrs, &vertShaderSrc); | 832 SkString* inOutFSColor, |
| 876 fParent->appendDecls(fVSOutputs, &vertShaderSrc); | 833 GrSLConstantVec* fsInOutColo
rKnownValue) { |
| 834 GrGLVertexEffectArrayBuilder effectArrayBuilder(this, effectCnt); |
| 835 this->INHERITED::emitEffects(&effectArrayBuilder, |
| 836 effectStages, |
| 837 effectKeys, |
| 838 effectCnt, |
| 839 inOutFSColor, |
| 840 fsInOutColorKnownValue); |
| 841 return effectArrayBuilder.finish(); |
| 842 } |
| 843 |
| 844 bool GrGLFullShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { |
| 845 const GrGLInterface* glInterface = this->gpu()->glInterface(); |
| 846 SkString vertShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 847 this->appendUniformDecls(kVertex_Visibility, &vertShaderSrc); |
| 848 this->appendDecls(fVSAttrs, &vertShaderSrc); |
| 849 this->appendDecls(fVSOutputs, &vertShaderSrc); |
| 877 vertShaderSrc.append("void main() {\n"); | 850 vertShaderSrc.append("void main() {\n"); |
| 878 vertShaderSrc.append(fVSCode); | 851 vertShaderSrc.append(fVSCode); |
| 879 vertShaderSrc.append("}\n"); | 852 vertShaderSrc.append("}\n"); |
| 880 if (!attach_shader(fGpu->glInterface(), programId, GR_GL_VERTEX_SHADER, vert
ShaderSrc)) { | 853 if (!attach_shader(glInterface, programId, GR_GL_VERTEX_SHADER, vertShaderSr
c)) { |
| 881 return false; | 854 return false; |
| 882 } | 855 } |
| 883 | 856 |
| 884 #if GR_GL_EXPERIMENTAL_GS | 857 #if GR_GL_EXPERIMENTAL_GS |
| 885 if (fDesc.getHeader().fExperimentalGS) { | 858 if (fDesc.getHeader().fExperimentalGS) { |
| 886 SkASSERT(fGpu->glslGeneration() >= k150_GrGLSLGeneration); | 859 SkASSERT(this->ctxInfo().glslGeneration() >= k150_GrGLSLGeneration); |
| 887 SkString geomShaderSrc(GrGetGLSLVersionDecl(fParent->ctxInfo())); | 860 SkString geomShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 888 geomShaderSrc.append("layout(triangles) in;\n" | 861 geomShaderSrc.append("layout(triangles) in;\n" |
| 889 "layout(triangle_strip, max_vertices = 6) out;\n"); | 862 "layout(triangle_strip, max_vertices = 6) out;\n"); |
| 890 fParent->appendDecls(fGSInputs, &geomShaderSrc); | 863 this->appendDecls(fGSInputs, &geomShaderSrc); |
| 891 fParent->appendDecls(fGSOutputs, &geomShaderSrc); | 864 this->appendDecls(fGSOutputs, &geomShaderSrc); |
| 892 geomShaderSrc.append("void main() {\n"); | 865 geomShaderSrc.append("void main() {\n"); |
| 893 geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n" | 866 geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n" |
| 894 "\t\tgl_Position = gl_in[i].gl_Position;\n"); | 867 "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
| 895 if (fDesc.getHeader().fEmitsPointSize) { | 868 if (fDesc.getHeader().fEmitsPointSize) { |
| 896 geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n"); | 869 geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n"); |
| 897 } | 870 } |
| 898 SkASSERT(fGSInputs.count() == fGSOutputs.count()); | 871 SkASSERT(fGSInputs.count() == fGSOutputs.count()); |
| 899 for (int i = 0; i < fGSInputs.count(); ++i) { | 872 for (int i = 0; i < fGSInputs.count(); ++i) { |
| 900 geomShaderSrc.appendf("\t\t%s = %s[i];\n", | 873 geomShaderSrc.appendf("\t\t%s = %s[i];\n", |
| 901 fGSOutputs[i].getName().c_str(), | 874 fGSOutputs[i].getName().c_str(), |
| 902 fGSInputs[i].getName().c_str()); | 875 fGSInputs[i].getName().c_str()); |
| 903 } | 876 } |
| 904 geomShaderSrc.append("\t\tEmitVertex();\n" | 877 geomShaderSrc.append("\t\tEmitVertex();\n" |
| 905 "\t}\n" | 878 "\t}\n" |
| 906 "\tEndPrimitive();\n"); | 879 "\tEndPrimitive();\n"); |
| 907 geomShaderSrc.append("}\n"); | 880 geomShaderSrc.append("}\n"); |
| 908 if (!attach_shader(fGpu->glInterface(), programId, GR_GL_GEOMETRY_SHADER
, geomShaderSrc)) { | 881 if (!attach_shader(glInterface, programId, GR_GL_GEOMETRY_SHADER, geomSh
aderSrc)) { |
| 909 return false; | 882 return false; |
| 910 } | 883 } |
| 911 } | 884 } |
| 912 #endif | 885 #endif |
| 913 | 886 |
| 914 return true; | 887 return this->INHERITED::compileAndAttachShaders(programId); |
| 915 } | 888 } |
| 916 | 889 |
| 917 void GrGLShaderBuilder::VertexBuilder::bindProgramLocations(GrGLuint programId)
const { | 890 void GrGLFullShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
| 891 this->INHERITED::bindProgramLocations(programId); |
| 892 |
| 918 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); | 893 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 919 | 894 |
| 920 // Bind the attrib locations to same values for all shaders | 895 // Bind the attrib locations to same values for all shaders |
| 921 SkASSERT(-1 != header.fPositionAttributeIndex); | 896 SkASSERT(-1 != header.fPositionAttributeIndex); |
| 922 GL_CALL(BindAttribLocation(programId, | 897 GL_CALL(BindAttribLocation(programId, |
| 923 header.fPositionAttributeIndex, | 898 header.fPositionAttributeIndex, |
| 924 fPositionVar->c_str())); | 899 fPositionVar->c_str())); |
| 925 if (-1 != header.fLocalCoordAttributeIndex) { | 900 if (-1 != header.fLocalCoordAttributeIndex) { |
| 926 GL_CALL(BindAttribLocation(programId, | 901 GL_CALL(BindAttribLocation(programId, |
| 927 header.fLocalCoordAttributeIndex, | 902 header.fLocalCoordAttributeIndex, |
| 928 fLocalCoordsVar->c_str())); | 903 fLocalCoordsVar->c_str())); |
| 929 } | 904 } |
| 930 if (-1 != header.fColorAttributeIndex) { | 905 if (-1 != header.fColorAttributeIndex) { |
| 931 GL_CALL(BindAttribLocation(programId, | 906 GL_CALL(BindAttribLocation(programId, |
| 932 header.fColorAttributeIndex, | 907 header.fColorAttributeIndex, |
| 933 color_attribute_name())); | 908 color_attribute_name())); |
| 934 } | 909 } |
| 935 if (-1 != header.fCoverageAttributeIndex) { | 910 if (-1 != header.fCoverageAttributeIndex) { |
| 936 GL_CALL(BindAttribLocation(programId, | 911 GL_CALL(BindAttribLocation(programId, |
| 937 header.fCoverageAttributeIndex, | 912 header.fCoverageAttributeIndex, |
| 938 coverage_attribute_name())); | 913 coverage_attribute_name())); |
| 939 } | 914 } |
| 940 | 915 |
| 941 const AttributePair* attribEnd = fEffectAttributes.end(); | 916 const AttributePair* attribEnd = fEffectAttributes.end(); |
| 942 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { | 917 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { |
| 943 GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_s
tr())); | 918 GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_s
tr())); |
| 944 } | 919 } |
| 945 } | 920 } |
| OLD | NEW |