| 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 GrGLProgramEffects* GrGLShaderBuilder::createAndEmitEffects( | 509 void GrGLShaderBuilder::createAndEmitEffects(GrGLProgramEffectsBuilder* programE
ffectsBuilder, |
| 555 const GrEffectStage* effectStages[], | 510 const GrEffectStage* effectStages[]
, |
| 556 const EffectKey effectKeys[], | 511 const EffectKey effectKeys[], |
| 557 int effectCnt, | 512 int effectCnt, |
| 558 SkString* fsInOutColor, | 513 SkString* fsInOutColor, |
| 559 GrSLConstantVec* fsInOutColorKnownValue) { | 514 GrSLConstantVec* fsInOutColorKnownV
alue) { |
| 560 | |
| 561 GrGLProgramEffectsBuilder programEffectsBuilder(this, effectCnt); | |
| 562 bool effectEmitted = false; | 515 bool effectEmitted = false; |
| 563 | 516 |
| 564 SkString inColor = *fsInOutColor; | 517 SkString inColor = *fsInOutColor; |
| 565 SkString outColor; | 518 SkString outColor; |
| 566 | 519 |
| 567 for (int e = 0; e < effectCnt; ++e) { | 520 for (int e = 0; e < effectCnt; ++e) { |
| 568 SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()
); | 521 SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()
); |
| 569 const GrEffectStage& stage = *effectStages[e]; | 522 const GrEffectStage& stage = *effectStages[e]; |
| 570 | 523 |
| 571 CodeStage::AutoStageRestore csar(&fCodeStage, &stage); | 524 CodeStage::AutoStageRestore csar(&fCodeStage, &stage); |
| 572 | 525 |
| 573 if (kZeros_GrSLConstantVec == *fsInOutColorKnownValue) { | 526 if (kZeros_GrSLConstantVec == *fsInOutColorKnownValue) { |
| 574 // 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. |
| 575 this->nameVariable(&inColor, '\0', "input"); | 528 this->nameVariable(&inColor, '\0', "input"); |
| 576 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)); |
| 577 } | 530 } |
| 578 | 531 |
| 579 // create var to hold stage result | 532 // create var to hold stage result |
| 580 this->nameVariable(&outColor, '\0', "output"); | 533 this->nameVariable(&outColor, '\0', "output"); |
| 581 this->fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); | 534 this->fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); |
| 582 | 535 |
| 583 programEffectsBuilder.emitEffect(stage, | 536 programEffectsBuilder->emitEffect(stage, |
| 584 effectKeys[e], | 537 effectKeys[e], |
| 585 outColor.c_str(), | 538 outColor.c_str(), |
| 586 inColor.isEmpty() ? NULL : inColor.c_st
r(), | 539 inColor.isEmpty() ? NULL : inColor.c_s
tr(), |
| 587 fCodeStage.stageIndex()); | 540 fCodeStage.stageIndex()); |
| 588 | 541 |
| 589 inColor = outColor; | 542 inColor = outColor; |
| 590 *fsInOutColorKnownValue = kNone_GrSLConstantVec; | 543 *fsInOutColorKnownValue = kNone_GrSLConstantVec; |
| 591 effectEmitted = true; | 544 effectEmitted = true; |
| 592 } | 545 } |
| 593 | 546 |
| 594 if (effectEmitted) { | 547 if (effectEmitted) { |
| 595 *fsInOutColor = outColor; | 548 *fsInOutColor = outColor; |
| 596 } | 549 } |
| 597 | |
| 598 return programEffectsBuilder.finish(); | |
| 599 } | 550 } |
| 600 | 551 |
| 601 const char* GrGLShaderBuilder::getColorOutputName() const { | 552 const char* GrGLShaderBuilder::getColorOutputName() const { |
| 602 return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"
; | 553 return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"
; |
| 603 } | 554 } |
| 604 | 555 |
| 605 const char* GrGLShaderBuilder::enableSecondaryOutput() { | 556 const char* GrGLShaderBuilder::enableSecondaryOutput() { |
| 606 if (!fHasSecondaryOutput) { | 557 if (!fHasSecondaryOutput) { |
| 607 fFSOutputs.push_back().set(kVec4f_GrSLType, | 558 fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 608 GrGLShaderVar::kOut_TypeModifier, | 559 GrGLShaderVar::kOut_TypeModifier, |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 } | 650 } |
| 700 | 651 |
| 701 GR_GL_CALL(gli, AttachShader(programId, shaderId)); | 652 GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
| 702 GR_GL_CALL(gli, DeleteShader(shaderId)); | 653 GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 703 return true; | 654 return true; |
| 704 } | 655 } |
| 705 | 656 |
| 706 } | 657 } |
| 707 | 658 |
| 708 bool GrGLShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { | 659 bool GrGLShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { |
| 709 if (NULL != fVertexBuilder.get() && !fVertexBuilder->compileAndAttachShaders
(programId)) { | |
| 710 return false; | |
| 711 } | |
| 712 | |
| 713 SkString fragShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); | 660 SkString fragShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 714 fragShaderSrc.append(fFSExtensions); | 661 fragShaderSrc.append(fFSExtensions); |
| 715 append_default_precision_qualifier(kDefaultFragmentPrecision, | 662 append_default_precision_qualifier(kDefaultFragmentPrecision, |
| 716 fGpu->glBinding(), | 663 fGpu->glBinding(), |
| 717 &fragShaderSrc); | 664 &fragShaderSrc); |
| 718 this->appendUniformDecls(kFragment_Visibility, &fragShaderSrc); | 665 this->appendUniformDecls(kFragment_Visibility, &fragShaderSrc); |
| 719 this->appendDecls(fFSInputs, &fragShaderSrc); | 666 this->appendDecls(fFSInputs, &fragShaderSrc); |
| 720 // We shouldn't have declared outputs on 1.10 | 667 // We shouldn't have declared outputs on 1.10 |
| 721 SkASSERT(k110_GrGLSLGeneration != fGpu->glslGeneration() || fFSOutputs.empty
()); | 668 SkASSERT(k110_GrGLSLGeneration != fGpu->glslGeneration() || fFSOutputs.empty
()); |
| 722 this->appendDecls(fFSOutputs, &fragShaderSrc); | 669 this->appendDecls(fFSOutputs, &fragShaderSrc); |
| 723 fragShaderSrc.append(fFSFunctions); | 670 fragShaderSrc.append(fFSFunctions); |
| 724 fragShaderSrc.append("void main() {\n"); | 671 fragShaderSrc.append("void main() {\n"); |
| 725 fragShaderSrc.append(fFSCode); | 672 fragShaderSrc.append(fFSCode); |
| 726 fragShaderSrc.append("}\n"); | 673 fragShaderSrc.append("}\n"); |
| 727 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)) { |
| 728 return false; | 675 return false; |
| 729 } | 676 } |
| 730 | 677 |
| 731 return true; | 678 return true; |
| 732 } | 679 } |
| 733 | 680 |
| 734 void GrGLShaderBuilder::bindProgramLocations(GrGLuint programId) const { | 681 void GrGLShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
| 735 if (NULL != fVertexBuilder.get()) { | |
| 736 fVertexBuilder->bindProgramLocations(programId); | |
| 737 } | |
| 738 | |
| 739 if (fHasCustomColorOutput) { | 682 if (fHasCustomColorOutput) { |
| 740 GL_CALL(BindFragDataLocation(programId, 0, declared_color_output_name())
); | 683 GL_CALL(BindFragDataLocation(programId, 0, declared_color_output_name())
); |
| 741 } | 684 } |
| 742 if (fHasSecondaryOutput) { | 685 if (fHasSecondaryOutput) { |
| 743 GL_CALL(BindFragDataLocationIndexed(programId, 0, 1, dual_source_output_
name())); | 686 GL_CALL(BindFragDataLocationIndexed(programId, 0, 1, dual_source_output_
name())); |
| 744 } | 687 } |
| 745 } | 688 } |
| 746 | 689 |
| 747 const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const { | 690 const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const { |
| 748 return fGpu->ctxInfo(); | 691 return fGpu->ctxInfo(); |
| 749 } | 692 } |
| 750 | 693 |
| 751 //////////////////////////////////////////////////////////////////////////// | 694 //////////////////////////////////////////////////////////////////////////////// |
| 752 | 695 |
| 753 GrGLShaderBuilder::VertexBuilder::VertexBuilder(GrGLShaderBuilder* parent, | 696 GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGpuGL* gpu, |
| 754 GrGpuGL* gpu, | 697 GrGLUniformManager& uniformManager, |
| 755 const GrGLProgramDesc& desc) | 698 const GrGLProgramDesc& desc) |
| 756 : fParent(parent) | 699 : INHERITED(gpu, uniformManager, desc) |
| 757 , fGpu(gpu) | |
| 758 , fDesc(desc) | 700 , fDesc(desc) |
| 759 , fVSAttrs(kVarsPerBlock) | 701 , fVSAttrs(kVarsPerBlock) |
| 760 , fVSOutputs(kVarsPerBlock) | 702 , fVSOutputs(kVarsPerBlock) |
| 761 , fGSInputs(kVarsPerBlock) | 703 , fGSInputs(kVarsPerBlock) |
| 762 , fGSOutputs(kVarsPerBlock) { | 704 , fGSOutputs(kVarsPerBlock) { |
| 763 | 705 |
| 764 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); | 706 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 765 | 707 |
| 766 fPositionVar = &fVSAttrs.push_back(); | 708 fPositionVar = &fVSAttrs.push_back(); |
| 767 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
aPosition"); | 709 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
aPosition"); |
| 768 if (-1 != header.fLocalCoordAttributeIndex) { | 710 if (-1 != header.fLocalCoordAttributeIndex) { |
| 769 fLocalCoordsVar = &fVSAttrs.push_back(); | 711 fLocalCoordsVar = &fVSAttrs.push_back(); |
| 770 fLocalCoordsVar->set(kVec2f_GrSLType, | 712 fLocalCoordsVar->set(kVec2f_GrSLType, |
| 771 GrGLShaderVar::kAttribute_TypeModifier, | 713 GrGLShaderVar::kAttribute_TypeModifier, |
| 772 "aLocalCoords"); | 714 "aLocalCoords"); |
| 773 } else { | 715 } else { |
| 774 fLocalCoordsVar = fPositionVar; | 716 fLocalCoordsVar = fPositionVar; |
| 775 } | 717 } |
| 776 | 718 |
| 777 const char* viewMName; | 719 const char* viewMName; |
| 778 fViewMatrixUniform = fParent->addUniform(GrGLShaderBuilder::kVertex_Visibili
ty, | 720 fViewMatrixUniform = this->addUniform(GrGLShaderBuilder::kVertex_Visibility, |
| 779 kMat33f_GrSLType, "ViewM", &viewMNa
me); | 721 kMat33f_GrSLType, "ViewM", &viewMName)
; |
| 780 | 722 |
| 781 this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" | 723 this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
| 782 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", | 724 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
| 783 viewMName, fPositionVar->c_str()); | 725 viewMName, fPositionVar->c_str()); |
| 784 | 726 |
| 785 // we output point size in the GS if present | 727 // we output point size in the GS if present |
| 786 if (header.fEmitsPointSize | 728 if (header.fEmitsPointSize |
| 787 #if GR_GL_EXPERIMENTAL_GS | 729 #if GR_GL_EXPERIMENTAL_GS |
| 788 && !header.fExperimentalGS | 730 && !header.fExperimentalGS |
| 789 #endif | 731 #endif |
| 790 ) { | 732 ) { |
| 791 this->vsCodeAppend("\tgl_PointSize = 1.0;\n"); | 733 this->vsCodeAppend("\tgl_PointSize = 1.0;\n"); |
| 792 } | 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 } |
| 793 } | 751 } |
| 794 | 752 |
| 795 bool GrGLShaderBuilder::VertexBuilder::addAttribute(GrSLType type, | 753 bool GrGLFullShaderBuilder::addAttribute(GrSLType type, const char* name) { |
| 796 const char* name) { | |
| 797 for (int i = 0; i < fVSAttrs.count(); ++i) { | 754 for (int i = 0; i < fVSAttrs.count(); ++i) { |
| 798 const GrGLShaderVar& attr = fVSAttrs[i]; | 755 const GrGLShaderVar& attr = fVSAttrs[i]; |
| 799 // if attribute already added, don't add it again | 756 // if attribute already added, don't add it again |
| 800 if (attr.getName().equals(name)) { | 757 if (attr.getName().equals(name)) { |
| 801 SkASSERT(attr.getType() == type); | 758 SkASSERT(attr.getType() == type); |
| 802 return false; | 759 return false; |
| 803 } | 760 } |
| 804 } | 761 } |
| 805 fVSAttrs.push_back().set(type, | 762 fVSAttrs.push_back().set(type, |
| 806 GrGLShaderVar::kAttribute_TypeModifier, | 763 GrGLShaderVar::kAttribute_TypeModifier, |
| 807 name); | 764 name); |
| 808 return true; | 765 return true; |
| 809 } | 766 } |
| 810 | 767 |
| 811 bool GrGLShaderBuilder::VertexBuilder::addEffectAttribute(int attributeIndex, | 768 bool GrGLFullShaderBuilder::addEffectAttribute(int attributeIndex, |
| 812 GrSLType type, | 769 GrSLType type, |
| 813 const SkString& name)
{ | 770 const SkString& name) { |
| 814 if (!this->addAttribute(type, name.c_str())) { | 771 if (!this->addAttribute(type, name.c_str())) { |
| 815 return false; | 772 return false; |
| 816 } | 773 } |
| 817 | 774 |
| 818 fEffectAttributes.push_back().set(attributeIndex, name); | 775 fEffectAttributes.push_back().set(attributeIndex, name); |
| 819 return true; | 776 return true; |
| 820 } | 777 } |
| 821 | 778 |
| 822 void GrGLShaderBuilder::VertexBuilder::addVarying(GrSLType type, | 779 void GrGLFullShaderBuilder::addVarying(GrSLType type, |
| 823 const char* name, | 780 const char* name, |
| 824 const char** vsOutName, | 781 const char** vsOutName, |
| 825 const char** fsInName) { | 782 const char** fsInName) { |
| 826 fVSOutputs.push_back(); | 783 fVSOutputs.push_back(); |
| 827 fVSOutputs.back().setType(type); | 784 fVSOutputs.back().setType(type); |
| 828 fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); | 785 fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| 829 fParent->nameVariable(fVSOutputs.back().accessName(), 'v', name); | 786 this->nameVariable(fVSOutputs.back().accessName(), 'v', name); |
| 830 | 787 |
| 831 if (vsOutName) { | 788 if (vsOutName) { |
| 832 *vsOutName = fVSOutputs.back().getName().c_str(); | 789 *vsOutName = fVSOutputs.back().getName().c_str(); |
| 833 } | 790 } |
| 834 // input to FS comes either from VS or GS | 791 // input to FS comes either from VS or GS |
| 835 const SkString* fsName; | 792 const SkString* fsName; |
| 836 #if GR_GL_EXPERIMENTAL_GS | 793 #if GR_GL_EXPERIMENTAL_GS |
| 837 if (fDesc.getHeader().fExperimentalGS) { | 794 if (fDesc.getHeader().fExperimentalGS) { |
| 838 // 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 |
| 839 // and output as non-array. | 796 // and output as non-array. |
| 840 fGSInputs.push_back(); | 797 fGSInputs.push_back(); |
| 841 fGSInputs.back().setType(type); | 798 fGSInputs.back().setType(type); |
| 842 fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier)
; | 799 fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier)
; |
| 843 fGSInputs.back().setUnsizedArray(); | 800 fGSInputs.back().setUnsizedArray(); |
| 844 *fGSInputs.back().accessName() = fVSOutputs.back().getName(); | 801 *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 845 fGSOutputs.push_back(); | 802 fGSOutputs.push_back(); |
| 846 fGSOutputs.back().setType(type); | 803 fGSOutputs.back().setType(type); |
| 847 fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifie
r); | 804 fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifie
r); |
| 848 fParent->nameVariable(fGSOutputs.back().accessName(), 'g', name); | 805 this->nameVariable(fGSOutputs.back().accessName(), 'g', name); |
| 849 fsName = fGSOutputs.back().accessName(); | 806 fsName = fGSOutputs.back().accessName(); |
| 850 } else | 807 } else |
| 851 #endif | 808 #endif |
| 852 { | 809 { |
| 853 fsName = fVSOutputs.back().accessName(); | 810 fsName = fVSOutputs.back().accessName(); |
| 854 } | 811 } |
| 855 fParent->fsInputAppend().set(type, | 812 this->fsInputAppend().set(type, GrGLShaderVar::kVaryingIn_TypeModifier, *fsN
ame); |
| 856 GrGLShaderVar::kVaryingIn_TypeModifier, | |
| 857 *fsName); | |
| 858 if (fsInName) { | 813 if (fsInName) { |
| 859 *fsInName = fsName->c_str(); | 814 *fsInName = fsName->c_str(); |
| 860 } | 815 } |
| 861 } | 816 } |
| 862 | 817 |
| 863 const SkString* GrGLShaderBuilder::VertexBuilder::getEffectAttributeName(int att
ributeIndex) const { | 818 const SkString* GrGLFullShaderBuilder::getEffectAttributeName(int attributeIndex
) const { |
| 864 const AttributePair* attribEnd = fEffectAttributes.end(); | 819 const AttributePair* attribEnd = fEffectAttributes.end(); |
| 865 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { | 820 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { |
| 866 if (attrib->fIndex == attributeIndex) { | 821 if (attrib->fIndex == attributeIndex) { |
| 867 return &attrib->fName; | 822 return &attrib->fName; |
| 868 } | 823 } |
| 869 } | 824 } |
| 870 | 825 |
| 871 return NULL; | 826 return NULL; |
| 872 } | 827 } |
| 873 | 828 |
| 874 bool GrGLShaderBuilder::VertexBuilder::compileAndAttachShaders(GrGLuint programI
d) const { | 829 GrGLProgramEffects* GrGLFullShaderBuilder::createAndEmitEffects( |
| 875 SkString vertShaderSrc(GrGetGLSLVersionDecl(fParent->ctxInfo())); | 830 const GrEffectStage* effectStages[], |
| 876 fParent->appendUniformDecls(kVertex_Visibility, &vertShaderSrc); | 831 const EffectKey effectKeys[], |
| 877 fParent->appendDecls(fVSAttrs, &vertShaderSrc); | 832 int effectCnt, |
| 878 fParent->appendDecls(fVSOutputs, &vertShaderSrc); | 833 SkString* inOutFSColor, |
| 834 GrSLConstantVec* fsInOutColorKnownValue) { |
| 835 |
| 836 GrGLVertexProgramEffectsBuilder programEffectsBuilder(this, effectCnt); |
| 837 this->INHERITED::createAndEmitEffects(&programEffectsBuilder, |
| 838 effectStages, |
| 839 effectKeys, |
| 840 effectCnt, |
| 841 inOutFSColor, |
| 842 fsInOutColorKnownValue); |
| 843 return programEffectsBuilder.finish(); |
| 844 } |
| 845 |
| 846 bool GrGLFullShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { |
| 847 const GrGLInterface* glInterface = this->gpu()->glInterface(); |
| 848 SkString vertShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 849 this->appendUniformDecls(kVertex_Visibility, &vertShaderSrc); |
| 850 this->appendDecls(fVSAttrs, &vertShaderSrc); |
| 851 this->appendDecls(fVSOutputs, &vertShaderSrc); |
| 879 vertShaderSrc.append("void main() {\n"); | 852 vertShaderSrc.append("void main() {\n"); |
| 880 vertShaderSrc.append(fVSCode); | 853 vertShaderSrc.append(fVSCode); |
| 881 vertShaderSrc.append("}\n"); | 854 vertShaderSrc.append("}\n"); |
| 882 if (!attach_shader(fGpu->glInterface(), programId, GR_GL_VERTEX_SHADER, vert
ShaderSrc)) { | 855 if (!attach_shader(glInterface, programId, GR_GL_VERTEX_SHADER, vertShaderSr
c)) { |
| 883 return false; | 856 return false; |
| 884 } | 857 } |
| 885 | 858 |
| 886 #if GR_GL_EXPERIMENTAL_GS | 859 #if GR_GL_EXPERIMENTAL_GS |
| 887 if (fDesc.getHeader().fExperimentalGS) { | 860 if (fDesc.getHeader().fExperimentalGS) { |
| 888 SkASSERT(fGpu->glslGeneration() >= k150_GrGLSLGeneration); | 861 SkASSERT(this->ctxInfo().glslGeneration() >= k150_GrGLSLGeneration); |
| 889 SkString geomShaderSrc(GrGetGLSLVersionDecl(fParent->ctxInfo())); | 862 SkString geomShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 890 geomShaderSrc.append("layout(triangles) in;\n" | 863 geomShaderSrc.append("layout(triangles) in;\n" |
| 891 "layout(triangle_strip, max_vertices = 6) out;\n"); | 864 "layout(triangle_strip, max_vertices = 6) out;\n"); |
| 892 fParent->appendDecls(fGSInputs, &geomShaderSrc); | 865 this->appendDecls(fGSInputs, &geomShaderSrc); |
| 893 fParent->appendDecls(fGSOutputs, &geomShaderSrc); | 866 this->appendDecls(fGSOutputs, &geomShaderSrc); |
| 894 geomShaderSrc.append("void main() {\n"); | 867 geomShaderSrc.append("void main() {\n"); |
| 895 geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n" | 868 geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n" |
| 896 "\t\tgl_Position = gl_in[i].gl_Position;\n"); | 869 "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
| 897 if (fDesc.getHeader().fEmitsPointSize) { | 870 if (fDesc.getHeader().fEmitsPointSize) { |
| 898 geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n"); | 871 geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n"); |
| 899 } | 872 } |
| 900 SkASSERT(fGSInputs.count() == fGSOutputs.count()); | 873 SkASSERT(fGSInputs.count() == fGSOutputs.count()); |
| 901 for (int i = 0; i < fGSInputs.count(); ++i) { | 874 for (int i = 0; i < fGSInputs.count(); ++i) { |
| 902 geomShaderSrc.appendf("\t\t%s = %s[i];\n", | 875 geomShaderSrc.appendf("\t\t%s = %s[i];\n", |
| 903 fGSOutputs[i].getName().c_str(), | 876 fGSOutputs[i].getName().c_str(), |
| 904 fGSInputs[i].getName().c_str()); | 877 fGSInputs[i].getName().c_str()); |
| 905 } | 878 } |
| 906 geomShaderSrc.append("\t\tEmitVertex();\n" | 879 geomShaderSrc.append("\t\tEmitVertex();\n" |
| 907 "\t}\n" | 880 "\t}\n" |
| 908 "\tEndPrimitive();\n"); | 881 "\tEndPrimitive();\n"); |
| 909 geomShaderSrc.append("}\n"); | 882 geomShaderSrc.append("}\n"); |
| 910 if (!attach_shader(fGpu->glInterface(), programId, GR_GL_GEOMETRY_SHADER
, geomShaderSrc)) { | 883 if (!attach_shader(glInterface, programId, GR_GL_GEOMETRY_SHADER, geomSh
aderSrc)) { |
| 911 return false; | 884 return false; |
| 912 } | 885 } |
| 913 } | 886 } |
| 914 #endif | 887 #endif |
| 915 | 888 |
| 916 return true; | 889 return this->INHERITED::compileAndAttachShaders(programId); |
| 917 } | 890 } |
| 918 | 891 |
| 919 void GrGLShaderBuilder::VertexBuilder::bindProgramLocations(GrGLuint programId)
const { | 892 void GrGLFullShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
| 893 this->INHERITED::bindProgramLocations(programId); |
| 894 |
| 920 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); | 895 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 921 | 896 |
| 922 // Bind the attrib locations to same values for all shaders | 897 // Bind the attrib locations to same values for all shaders |
| 923 SkASSERT(-1 != header.fPositionAttributeIndex); | 898 SkASSERT(-1 != header.fPositionAttributeIndex); |
| 924 GL_CALL(BindAttribLocation(programId, | 899 GL_CALL(BindAttribLocation(programId, |
| 925 header.fPositionAttributeIndex, | 900 header.fPositionAttributeIndex, |
| 926 fPositionVar->c_str())); | 901 fPositionVar->c_str())); |
| 927 if (-1 != header.fLocalCoordAttributeIndex) { | 902 if (-1 != header.fLocalCoordAttributeIndex) { |
| 928 GL_CALL(BindAttribLocation(programId, | 903 GL_CALL(BindAttribLocation(programId, |
| 929 header.fLocalCoordAttributeIndex, | 904 header.fLocalCoordAttributeIndex, |
| 930 fLocalCoordsVar->c_str())); | 905 fLocalCoordsVar->c_str())); |
| 931 } | 906 } |
| 932 if (-1 != header.fColorAttributeIndex) { | 907 if (-1 != header.fColorAttributeIndex) { |
| 933 GL_CALL(BindAttribLocation(programId, | 908 GL_CALL(BindAttribLocation(programId, |
| 934 header.fColorAttributeIndex, | 909 header.fColorAttributeIndex, |
| 935 color_attribute_name())); | 910 color_attribute_name())); |
| 936 } | 911 } |
| 937 if (-1 != header.fCoverageAttributeIndex) { | 912 if (-1 != header.fCoverageAttributeIndex) { |
| 938 GL_CALL(BindAttribLocation(programId, | 913 GL_CALL(BindAttribLocation(programId, |
| 939 header.fCoverageAttributeIndex, | 914 header.fCoverageAttributeIndex, |
| 940 coverage_attribute_name())); | 915 coverage_attribute_name())); |
| 941 } | 916 } |
| 942 | 917 |
| 943 const AttributePair* attribEnd = fEffectAttributes.end(); | 918 const AttributePair* attribEnd = fEffectAttributes.end(); |
| 944 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { | 919 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { |
| 945 GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_s
tr())); | 920 GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_s
tr())); |
| 946 } | 921 } |
| 947 } | 922 } |
| OLD | NEW |