OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "GrGLProgram.h" | 8 #include "GrGLProgram.h" |
9 | 9 |
10 #include "GrAllocator.h" | 10 #include "GrAllocator.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 const GrGLProgramDesc& desc, | 51 const GrGLProgramDesc& desc, |
52 const GrEffectStage* stages[]) | 52 const GrEffectStage* stages[]) |
53 : fContext(gl) | 53 : fContext(gl) |
54 , fUniformManager(gl) { | 54 , fUniformManager(gl) { |
55 fDesc = desc; | 55 fDesc = desc; |
56 fVShaderID = 0; | 56 fVShaderID = 0; |
57 fGShaderID = 0; | 57 fGShaderID = 0; |
58 fFShaderID = 0; | 58 fFShaderID = 0; |
59 fProgramID = 0; | 59 fProgramID = 0; |
60 | 60 |
| 61 fDstCopyTexUnit = -1; |
| 62 |
61 fColor = GrColor_ILLEGAL; | 63 fColor = GrColor_ILLEGAL; |
62 fColorFilterColor = GrColor_ILLEGAL; | 64 fColorFilterColor = GrColor_ILLEGAL; |
63 | 65 |
64 for (int s = 0; s < GrDrawState::kNumStages; ++s) { | 66 fEffectStates.reset(desc.numTotalEffects()); |
65 fEffects[s] = NULL; | |
66 } | |
67 | 67 |
68 this->genProgram(stages); | 68 this->genProgram(stages); |
69 } | 69 } |
70 | 70 |
71 GrGLProgram::~GrGLProgram() { | 71 GrGLProgram::~GrGLProgram() { |
72 if (fVShaderID) { | 72 if (fVShaderID) { |
73 GL_CALL(DeleteShader(fVShaderID)); | 73 GL_CALL(DeleteShader(fVShaderID)); |
74 } | 74 } |
75 if (fGShaderID) { | 75 if (fGShaderID) { |
76 GL_CALL(DeleteShader(fGShaderID)); | 76 GL_CALL(DeleteShader(fGShaderID)); |
77 } | 77 } |
78 if (fFShaderID) { | 78 if (fFShaderID) { |
79 GL_CALL(DeleteShader(fFShaderID)); | 79 GL_CALL(DeleteShader(fFShaderID)); |
80 } | 80 } |
81 if (fProgramID) { | 81 if (fProgramID) { |
82 GL_CALL(DeleteProgram(fProgramID)); | 82 GL_CALL(DeleteProgram(fProgramID)); |
83 } | 83 } |
84 | |
85 for (int i = 0; i < GrDrawState::kNumStages; ++i) { | |
86 delete fEffects[i]; | |
87 } | |
88 } | 84 } |
89 | 85 |
90 void GrGLProgram::abandon() { | 86 void GrGLProgram::abandon() { |
91 fVShaderID = 0; | 87 fVShaderID = 0; |
92 fGShaderID = 0; | 88 fGShaderID = 0; |
93 fFShaderID = 0; | 89 fFShaderID = 0; |
94 fProgramID = 0; | 90 fProgramID = 0; |
95 } | 91 } |
96 | 92 |
97 void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, | 93 void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 SkXfermode::Coeff colorCoeff; | 477 SkXfermode::Coeff colorCoeff; |
482 SkXfermode::Coeff filterColorCoeff; | 478 SkXfermode::Coeff filterColorCoeff; |
483 SkAssertResult( | 479 SkAssertResult( |
484 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>(header.fColorFilte
rXfermode), | 480 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>(header.fColorFilte
rXfermode), |
485 &filterColorCoeff, | 481 &filterColorCoeff, |
486 &colorCoeff)); | 482 &colorCoeff)); |
487 bool needColor, needFilterColor; | 483 bool needColor, needFilterColor; |
488 need_blend_inputs(filterColorCoeff, colorCoeff, &needFilterColor, &needColor
); | 484 need_blend_inputs(filterColorCoeff, colorCoeff, &needFilterColor, &needColor
); |
489 | 485 |
490 // used in order for builder to return the per-stage uniform handles. | 486 // used in order for builder to return the per-stage uniform handles. |
491 SkTArray<GrGLUniformManager::UniformHandle, true>* stageUniformArrays[GrDraw
State::kNumStages]; | 487 typedef SkTArray<GrGLUniformManager::UniformHandle, true>* UniHandleArrayPtr
; |
| 488 int maxColorOrCovEffectCnt = GrMax(fDesc.numColorEffects(), fDesc.numCoverag
eEffects()); |
| 489 SkAutoTArray<UniHandleArrayPtr> effectUniformArrays(maxColorOrCovEffectCnt); |
| 490 SkAutoTArray<GrGLEffect*> glEffects(maxColorOrCovEffectCnt); |
492 | 491 |
493 if (needColor) { | 492 if (needColor) { |
494 for (int s = 0; s < fDesc.numColorEffects(); ++s) { | 493 for (int e = 0; e < fDesc.numColorEffects(); ++e) { |
495 stageUniformArrays[s] = &fUniformHandles.fEffectSamplerUnis[s]; | 494 effectUniformArrays[e] = &fEffectStates[e].fSamplerUnis; |
496 } | 495 } |
497 | 496 |
498 builder.emitEffects(stages, | 497 builder.emitEffects(stages, |
499 fDesc.effectKeys(), | 498 fDesc.effectKeys(), |
500 fDesc.numColorEffects(), | 499 fDesc.numColorEffects(), |
501 &inColor, | 500 &inColor, |
502 &knownColorValue, | 501 &knownColorValue, |
503 stageUniformArrays, | 502 effectUniformArrays.get(), |
504 fEffects); | 503 glEffects.get()); |
| 504 |
| 505 for (int e = 0; e < fDesc.numColorEffects(); ++e) { |
| 506 fEffectStates[e].fGLEffect = glEffects[e]; |
| 507 } |
505 } | 508 } |
506 | 509 |
507 // Insert the color filter. This will soon be replaced by a color effect. | 510 // Insert the color filter. This will soon be replaced by a color effect. |
508 if (SkXfermode::kDst_Mode != header.fColorFilterXfermode) { | 511 if (SkXfermode::kDst_Mode != header.fColorFilterXfermode) { |
509 const char* colorFilterColorUniName = NULL; | 512 const char* colorFilterColorUniName = NULL; |
510 fUniformHandles.fColorFilterUni = builder.addUniform(GrGLShaderBuilder::
kFragment_ShaderType, | 513 fUniformHandles.fColorFilterUni = builder.addUniform(GrGLShaderBuilder::
kFragment_ShaderType, |
511 kVec4f_GrSLType, "F
ilterColor", | 514 kVec4f_GrSLType, "F
ilterColor", |
512 &colorFilterColorUn
iName); | 515 &colorFilterColorUn
iName); |
513 | 516 |
514 builder.fsCodeAppend("\tvec4 filteredColor;\n"); | 517 builder.fsCodeAppend("\tvec4 filteredColor;\n"); |
515 const char* color; | 518 const char* color; |
516 // add_color_filter requires a real input string. | 519 // add_color_filter requires a real input string. |
517 if (knownColorValue == kOnes_GrSLConstantVec) { | 520 if (knownColorValue == kOnes_GrSLConstantVec) { |
518 color = GrGLSLOnesVecf(4); | 521 color = GrGLSLOnesVecf(4); |
519 } else if (knownColorValue == kZeros_GrSLConstantVec) { | 522 } else if (knownColorValue == kZeros_GrSLConstantVec) { |
520 color = GrGLSLZerosVecf(4); | 523 color = GrGLSLZerosVecf(4); |
521 } else { | 524 } else { |
522 color = inColor.c_str(); | 525 color = inColor.c_str(); |
523 } | 526 } |
524 add_color_filter(&builder, "filteredColor", filterColorCoeff, | 527 add_color_filter(&builder, "filteredColor", filterColorCoeff, |
525 colorCoeff, colorFilterColorUniName, color); | 528 colorCoeff, colorFilterColorUniName, color); |
526 inColor = "filteredColor"; | 529 inColor = "filteredColor"; |
527 } | 530 } |
528 | 531 |
529 /////////////////////////////////////////////////////////////////////////// | 532 /////////////////////////////////////////////////////////////////////////// |
530 // compute the partial coverage | 533 // compute the partial coverage |
531 SkString inCoverage; | 534 SkString inCoverage; |
532 GrSLConstantVec knownCoverageValue = this->genInputCoverage(&builder, &inCov
erage); | 535 GrSLConstantVec knownCoverageValue = this->genInputCoverage(&builder, &inCov
erage); |
533 | 536 |
534 for (int s = 0; s < fDesc.numCoverageEffects(); ++s) { | 537 for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { |
535 stageUniformArrays[s] = &fUniformHandles.fEffectSamplerUnis[s + fDesc.nu
mColorEffects()]; | 538 effectUniformArrays[e] = &fEffectStates[e + fDesc.numColorEffects()].fSa
mplerUnis; |
536 } | 539 } |
537 | 540 |
538 builder.emitEffects(stages + fDesc.numColorEffects(), | 541 builder.emitEffects(stages + fDesc.numColorEffects(), |
539 fDesc.getEffectKeys() + fDesc.numColorEffects(), | 542 fDesc.getEffectKeys() + fDesc.numColorEffects(), |
540 fDesc.numCoverageEffects(), | 543 fDesc.numCoverageEffects(), |
541 &inCoverage, | 544 &inCoverage, |
542 &knownCoverageValue, | 545 &knownCoverageValue, |
543 stageUniformArrays, | 546 effectUniformArrays.get(), |
544 fEffects + fDesc.numColorEffects()); | 547 glEffects.get()); |
| 548 for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { |
| 549 fEffectStates[e + fDesc.numColorEffects()].fGLEffect = glEffects[e]; |
| 550 } |
545 | 551 |
546 // discard if coverage is zero | 552 // discard if coverage is zero |
547 if (header.fDiscardIfZeroCoverage && kOnes_GrSLConstantVec != knownCoverageV
alue) { | 553 if (header.fDiscardIfZeroCoverage && kOnes_GrSLConstantVec != knownCoverageV
alue) { |
548 if (kZeros_GrSLConstantVec == knownCoverageValue) { | 554 if (kZeros_GrSLConstantVec == knownCoverageValue) { |
549 // This is unfortunate. | 555 // This is unfortunate. |
550 builder.fsCodeAppend("\tdiscard;\n"); | 556 builder.fsCodeAppend("\tdiscard;\n"); |
551 } else { | 557 } else { |
552 builder.fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\
t\tdiscard;\n\t}\n", | 558 builder.fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\
t\tdiscard;\n\t}\n", |
553 inCoverage.c_str()); | 559 inCoverage.c_str()); |
554 } | 560 } |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 GrAssert(!"Error linking program"); | 741 GrAssert(!"Error linking program"); |
736 GL_CALL(DeleteProgram(fProgramID)); | 742 GL_CALL(DeleteProgram(fProgramID)); |
737 fProgramID = 0; | 743 fProgramID = 0; |
738 return false; | 744 return false; |
739 } | 745 } |
740 return true; | 746 return true; |
741 } | 747 } |
742 | 748 |
743 void GrGLProgram::initSamplerUniforms() { | 749 void GrGLProgram::initSamplerUniforms() { |
744 GL_CALL(UseProgram(fProgramID)); | 750 GL_CALL(UseProgram(fProgramID)); |
745 // We simply bind the uniforms to successive texture units beginning at 0. s
etData() assumes | |
746 // this behavior. | |
747 GrGLint texUnitIdx = 0; | 751 GrGLint texUnitIdx = 0; |
748 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopySam
plerUni) { | 752 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopySam
plerUni) { |
749 fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitId
x); | 753 fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitId
x); |
750 ++texUnitIdx; | 754 fDstCopyTexUnit = texUnitIdx++; |
751 } | 755 } |
752 | 756 |
753 for (int s = 0; s < GrDrawState::kNumStages; ++s) { | 757 for (int e = 0; e < fEffectStates.count(); ++e) { |
754 int numSamplers = fUniformHandles.fEffectSamplerUnis[s].count(); | 758 int numSamplers = fEffectStates[e].fSamplerUnis.count(); |
755 for (int u = 0; u < numSamplers; ++u) { | 759 fEffectStates[e].fTextureUnits.reset(numSamplers); |
756 UniformHandle handle = fUniformHandles.fEffectSamplerUnis[s][u]; | 760 for (int s = 0; s < numSamplers; ++s) { |
| 761 UniformHandle handle = fEffectStates[e].fSamplerUnis[s]; |
757 if (GrGLUniformManager::kInvalidUniformHandle != handle) { | 762 if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
758 fUniformManager.setSampler(handle, texUnitIdx); | 763 fUniformManager.setSampler(handle, texUnitIdx); |
759 ++texUnitIdx; | 764 fEffectStates[e].fTextureUnits[s] = texUnitIdx++; |
760 } | 765 } |
761 } | 766 } |
762 } | 767 } |
763 } | 768 } |
764 | 769 |
765 /////////////////////////////////////////////////////////////////////////////// | 770 /////////////////////////////////////////////////////////////////////////////// |
766 | 771 |
767 void GrGLProgram::setData(GrGpuGL* gpu, | 772 void GrGLProgram::setData(GrGpuGL* gpu, |
768 GrDrawState::BlendOptFlags blendOpts, | 773 GrDrawState::BlendOptFlags blendOpts, |
769 const GrEffectStage* stages[], | 774 const GrEffectStage* stages[], |
(...skipping 20 matching lines...) Expand all Loading... |
790 | 795 |
791 // Setup the SkXfermode::Mode-based colorfilter uniform if necessary | 796 // Setup the SkXfermode::Mode-based colorfilter uniform if necessary |
792 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fColorFilte
rUni && | 797 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fColorFilte
rUni && |
793 fColorFilterColor != drawState.getColorFilterColor()) { | 798 fColorFilterColor != drawState.getColorFilterColor()) { |
794 GrGLfloat c[4]; | 799 GrGLfloat c[4]; |
795 GrColorToRGBAFloat(drawState.getColorFilterColor(), c); | 800 GrColorToRGBAFloat(drawState.getColorFilterColor(), c); |
796 fUniformManager.set4fv(fUniformHandles.fColorFilterUni, 0, 1, c); | 801 fUniformManager.set4fv(fUniformHandles.fColorFilterUni, 0, 1, c); |
797 fColorFilterColor = drawState.getColorFilterColor(); | 802 fColorFilterColor = drawState.getColorFilterColor(); |
798 } | 803 } |
799 | 804 |
800 GrGLint texUnitIdx = 0; | |
801 if (NULL != dstCopy) { | 805 if (NULL != dstCopy) { |
802 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCop
yTopLeftUni) { | 806 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCop
yTopLeftUni) { |
803 GrAssert(GrGLUniformManager::kInvalidUniformHandle != fUniformHandle
s.fDstCopyScaleUni); | 807 GrAssert(GrGLUniformManager::kInvalidUniformHandle != fUniformHandle
s.fDstCopyScaleUni); |
804 GrAssert(GrGLUniformManager::kInvalidUniformHandle != | 808 GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
805 fUniformHandles.fDstCopySamplerUni); | 809 fUniformHandles.fDstCopySamplerUni); |
806 fUniformManager.set2f(fUniformHandles.fDstCopyTopLeftUni, | 810 fUniformManager.set2f(fUniformHandles.fDstCopyTopLeftUni, |
807 static_cast<GrGLfloat>(dstCopy->offset().fX), | 811 static_cast<GrGLfloat>(dstCopy->offset().fX), |
808 static_cast<GrGLfloat>(dstCopy->offset().fY)); | 812 static_cast<GrGLfloat>(dstCopy->offset().fY)); |
809 fUniformManager.set2f(fUniformHandles.fDstCopyScaleUni, | 813 fUniformManager.set2f(fUniformHandles.fDstCopyScaleUni, |
810 1.f / dstCopy->texture()->width(), | 814 1.f / dstCopy->texture()->width(), |
811 1.f / dstCopy->texture()->height()); | 815 1.f / dstCopy->texture()->height()); |
812 GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture())
; | 816 GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture())
; |
813 static GrTextureParams kParams; // the default is clamp, nearest fil
tering. | 817 static GrTextureParams kParams; // the default is clamp, nearest fil
tering. |
814 gpu->bindTexture(texUnitIdx, kParams, texture); | 818 gpu->bindTexture(fDstCopyTexUnit, kParams, texture); |
815 ++texUnitIdx; | |
816 } else { | 819 } else { |
817 GrAssert(GrGLUniformManager::kInvalidUniformHandle == | 820 GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
818 fUniformHandles.fDstCopyScaleUni); | 821 fUniformHandles.fDstCopyScaleUni); |
819 GrAssert(GrGLUniformManager::kInvalidUniformHandle == | 822 GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
820 fUniformHandles.fDstCopySamplerUni); | 823 fUniformHandles.fDstCopySamplerUni); |
821 } | 824 } |
822 } else { | 825 } else { |
823 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopyTopLeftUni); | 826 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopyTopLeftUni); |
824 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopyScaleUni); | 827 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopyScaleUni); |
825 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopySamplerUni); | 828 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopySamplerUni); |
826 } | 829 } |
827 | 830 |
828 int numEffects = fDesc.numTotalEffects(); | 831 int numEffects = fDesc.numTotalEffects(); |
829 for (int e = 0; e < numEffects; ++e) { | 832 for (int e = 0; e < numEffects; ++e) { |
830 GrAssert(NULL != stages[e]); | 833 GrAssert(NULL != stages[e]); |
831 // We may have omitted the GrGLEffect because of the color filter logic
in genProgram. | 834 // We may have omitted the GrGLEffect because of the color filter logic
in genProgram. |
832 // This can be removed when the color filter is an effect. | 835 // This can be removed when the color filter is an effect. |
833 if (NULL != fEffects[e]) { | 836 if (NULL != fEffectStates[e].fGLEffect) { |
834 bool explicitLocalCoords = -1 != fDesc.getHeader().fLocalCoordAttrib
uteIndex; | 837 bool explicitLocalCoords = -1 != fDesc.getHeader().fLocalCoordAttrib
uteIndex; |
835 GrDrawEffect drawEffect(*stages[e], explicitLocalCoords); | 838 GrDrawEffect drawEffect(*stages[e], explicitLocalCoords); |
836 fEffects[e]->setData(fUniformManager, drawEffect); | 839 fEffectStates[e].fGLEffect->setData(fUniformManager, drawEffect); |
837 int numSamplers = fUniformHandles.fEffectSamplerUnis[e].count(); | 840 int numSamplers = fEffectStates[e].fSamplerUnis.count(); |
838 for (int u = 0; u < numSamplers; ++u) { | 841 GrAssert((*stages[e]->getEffect())->numTextures() == numSamplers); |
839 UniformHandle handle = fUniformHandles.fEffectSamplerUnis[e][u]; | 842 for (int s = 0; s < numSamplers; ++s) { |
| 843 UniformHandle handle = fEffectStates[e].fSamplerUnis[s]; |
840 if (GrGLUniformManager::kInvalidUniformHandle != handle) { | 844 if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
841 const GrTextureAccess& access = (*stages[e]->getEffect())->t
extureAccess(u); | 845 const GrTextureAccess& access = (*stages[e]->getEffect())->t
extureAccess(s); |
842 GrGLTexture* texture = static_cast<GrGLTexture*>(access.getT
exture()); | 846 GrGLTexture* texture = static_cast<GrGLTexture*>(access.getT
exture()); |
843 gpu->bindTexture(texUnitIdx, access.getParams(), texture); | 847 int unit = fEffectStates[e].fTextureUnits[s]; |
844 ++texUnitIdx; | 848 gpu->bindTexture(unit, access.getParams(), texture); |
845 } | 849 } |
846 } | 850 } |
847 } | 851 } |
848 } | 852 } |
849 } | 853 } |
850 | 854 |
851 void GrGLProgram::setColor(const GrDrawState& drawState, | 855 void GrGLProgram::setColor(const GrDrawState& drawState, |
852 GrColor color, | 856 GrColor color, |
853 SharedGLState* sharedState) { | 857 SharedGLState* sharedState) { |
854 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); | 858 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
970 SkScalarToFloat(m[SkMatrix::kMTransX]), | 974 SkScalarToFloat(m[SkMatrix::kMTransX]), |
971 SkScalarToFloat(m[SkMatrix::kMTransY]), | 975 SkScalarToFloat(m[SkMatrix::kMTransY]), |
972 SkScalarToFloat(m[SkMatrix::kMPersp2]) | 976 SkScalarToFloat(m[SkMatrix::kMPersp2]) |
973 }; | 977 }; |
974 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); | 978 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); |
975 fMatrixState.fViewMatrix = drawState.getViewMatrix(); | 979 fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
976 fMatrixState.fRenderTargetSize = size; | 980 fMatrixState.fRenderTargetSize = size; |
977 fMatrixState.fRenderTargetOrigin = rt->origin(); | 981 fMatrixState.fRenderTargetOrigin = rt->origin(); |
978 } | 982 } |
979 } | 983 } |
OLD | NEW |