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 20 matching lines...) Expand all Loading... |
31 #define COV_ATTR_NAME "aCoverage" | 31 #define COV_ATTR_NAME "aCoverage" |
32 #define EDGE_ATTR_NAME "aEdge" | 32 #define EDGE_ATTR_NAME "aEdge" |
33 | 33 |
34 namespace { | 34 namespace { |
35 inline const char* declared_color_output_name() { return "fsColorOut"; } | 35 inline const char* declared_color_output_name() { return "fsColorOut"; } |
36 inline const char* dual_source_output_name() { return "dualSourceOut"; } | 36 inline const char* dual_source_output_name() { return "dualSourceOut"; } |
37 } | 37 } |
38 | 38 |
39 GrGLProgram* GrGLProgram::Create(const GrGLContext& gl, | 39 GrGLProgram* GrGLProgram::Create(const GrGLContext& gl, |
40 const GrGLProgramDesc& desc, | 40 const GrGLProgramDesc& desc, |
41 const GrEffectStage* stages[]) { | 41 const GrEffectStage* colorStages[], |
42 GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, stages)); | 42 const GrEffectStage* coverageStages[]) { |
| 43 GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, colorStages, cover
ageStages)); |
43 if (!program->succeeded()) { | 44 if (!program->succeeded()) { |
44 delete program; | 45 delete program; |
45 program = NULL; | 46 program = NULL; |
46 } | 47 } |
47 return program; | 48 return program; |
48 } | 49 } |
49 | 50 |
50 GrGLProgram::GrGLProgram(const GrGLContext& gl, | 51 GrGLProgram::GrGLProgram(const GrGLContext& gl, |
51 const GrGLProgramDesc& desc, | 52 const GrGLProgramDesc& desc, |
52 const GrEffectStage* stages[]) | 53 const GrEffectStage* colorStages[], |
| 54 const GrEffectStage* coverageStages[]) |
53 : fContext(gl) | 55 : fContext(gl) |
54 , fUniformManager(gl) { | 56 , fUniformManager(gl) { |
55 fDesc = desc; | 57 fDesc = desc; |
56 fVShaderID = 0; | 58 fVShaderID = 0; |
57 fGShaderID = 0; | 59 fGShaderID = 0; |
58 fFShaderID = 0; | 60 fFShaderID = 0; |
59 fProgramID = 0; | 61 fProgramID = 0; |
60 | 62 |
61 fDstCopyTexUnit = -1; | 63 fDstCopyTexUnit = -1; |
62 | 64 |
63 fColor = GrColor_ILLEGAL; | 65 fColor = GrColor_ILLEGAL; |
64 fColorFilterColor = GrColor_ILLEGAL; | 66 fColorFilterColor = GrColor_ILLEGAL; |
65 | 67 |
66 fEffectStates.reset(desc.numTotalEffects()); | 68 fColorEffects.reset(desc.numColorEffects()); |
| 69 fCoverageEffects.reset(desc.numCoverageEffects()); |
67 | 70 |
68 this->genProgram(stages); | 71 this->genProgram(colorStages, coverageStages); |
69 } | 72 } |
70 | 73 |
71 GrGLProgram::~GrGLProgram() { | 74 GrGLProgram::~GrGLProgram() { |
72 if (fVShaderID) { | 75 if (fVShaderID) { |
73 GL_CALL(DeleteShader(fVShaderID)); | 76 GL_CALL(DeleteShader(fVShaderID)); |
74 } | 77 } |
75 if (fGShaderID) { | 78 if (fGShaderID) { |
76 GL_CALL(DeleteShader(fGShaderID)); | 79 GL_CALL(DeleteShader(fGShaderID)); |
77 } | 80 } |
78 if (fFShaderID) { | 81 if (fFShaderID) { |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 GrPrintf(shader.c_str()); | 428 GrPrintf(shader.c_str()); |
426 GrPrintf("\n"); | 429 GrPrintf("\n"); |
427 } | 430 } |
428 if (!(fFShaderID = compile_shader(fContext, GR_GL_FRAGMENT_SHADER, shader)))
{ | 431 if (!(fFShaderID = compile_shader(fContext, GR_GL_FRAGMENT_SHADER, shader)))
{ |
429 return false; | 432 return false; |
430 } | 433 } |
431 | 434 |
432 return true; | 435 return true; |
433 } | 436 } |
434 | 437 |
435 bool GrGLProgram::genProgram(const GrEffectStage* stages[]) { | 438 bool GrGLProgram::genProgram(const GrEffectStage* colorStages[], |
| 439 const GrEffectStage* coverageStages[]) { |
436 GrAssert(0 == fProgramID); | 440 GrAssert(0 == fProgramID); |
437 | 441 |
438 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); | 442 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
439 | 443 |
440 GrGLShaderBuilder builder(fContext.info(), fUniformManager, fDesc); | 444 GrGLShaderBuilder builder(fContext.info(), fUniformManager, fDesc); |
441 | 445 |
442 // the dual source output has no canonical var name, have to | 446 // the dual source output has no canonical var name, have to |
443 // declare an output, which is incompatible with gl_FragColor/gl_FragData. | 447 // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
444 bool dualSourceOutputWritten = false; | 448 bool dualSourceOutputWritten = false; |
445 | 449 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 need_blend_inputs(filterColorCoeff, colorCoeff, &needFilterColor, &needColor
); | 488 need_blend_inputs(filterColorCoeff, colorCoeff, &needFilterColor, &needColor
); |
485 | 489 |
486 // used in order for builder to return the per-stage uniform handles. | 490 // used in order for builder to return the per-stage uniform handles. |
487 typedef SkTArray<GrGLUniformManager::UniformHandle, true>* UniHandleArrayPtr
; | 491 typedef SkTArray<GrGLUniformManager::UniformHandle, true>* UniHandleArrayPtr
; |
488 int maxColorOrCovEffectCnt = GrMax(fDesc.numColorEffects(), fDesc.numCoverag
eEffects()); | 492 int maxColorOrCovEffectCnt = GrMax(fDesc.numColorEffects(), fDesc.numCoverag
eEffects()); |
489 SkAutoTArray<UniHandleArrayPtr> effectUniformArrays(maxColorOrCovEffectCnt); | 493 SkAutoTArray<UniHandleArrayPtr> effectUniformArrays(maxColorOrCovEffectCnt); |
490 SkAutoTArray<GrGLEffect*> glEffects(maxColorOrCovEffectCnt); | 494 SkAutoTArray<GrGLEffect*> glEffects(maxColorOrCovEffectCnt); |
491 | 495 |
492 if (needColor) { | 496 if (needColor) { |
493 for (int e = 0; e < fDesc.numColorEffects(); ++e) { | 497 for (int e = 0; e < fDesc.numColorEffects(); ++e) { |
494 effectUniformArrays[e] = &fEffectStates[e].fSamplerUnis; | 498 effectUniformArrays[e] = &fColorEffects[e].fSamplerUnis; |
495 } | 499 } |
496 | 500 |
497 builder.emitEffects(stages, | 501 builder.emitEffects(colorStages, |
498 fDesc.effectKeys(), | 502 fDesc.effectKeys(), |
499 fDesc.numColorEffects(), | 503 fDesc.numColorEffects(), |
500 &inColor, | 504 &inColor, |
501 &knownColorValue, | 505 &knownColorValue, |
502 effectUniformArrays.get(), | 506 effectUniformArrays.get(), |
503 glEffects.get()); | 507 glEffects.get()); |
504 | 508 |
505 for (int e = 0; e < fDesc.numColorEffects(); ++e) { | 509 for (int e = 0; e < fDesc.numColorEffects(); ++e) { |
506 fEffectStates[e].fGLEffect = glEffects[e]; | 510 fColorEffects[e].fGLEffect = glEffects[e]; |
507 } | 511 } |
508 } | 512 } |
509 | 513 |
510 // Insert the color filter. This will soon be replaced by a color effect. | 514 // Insert the color filter. This will soon be replaced by a color effect. |
511 if (SkXfermode::kDst_Mode != header.fColorFilterXfermode) { | 515 if (SkXfermode::kDst_Mode != header.fColorFilterXfermode) { |
512 const char* colorFilterColorUniName = NULL; | 516 const char* colorFilterColorUniName = NULL; |
513 fUniformHandles.fColorFilterUni = builder.addUniform(GrGLShaderBuilder::
kFragment_ShaderType, | 517 fUniformHandles.fColorFilterUni = builder.addUniform(GrGLShaderBuilder::
kFragment_ShaderType, |
514 kVec4f_GrSLType, "F
ilterColor", | 518 kVec4f_GrSLType, "F
ilterColor", |
515 &colorFilterColorUn
iName); | 519 &colorFilterColorUn
iName); |
516 | 520 |
(...skipping 11 matching lines...) Expand all Loading... |
528 colorCoeff, colorFilterColorUniName, color); | 532 colorCoeff, colorFilterColorUniName, color); |
529 inColor = "filteredColor"; | 533 inColor = "filteredColor"; |
530 } | 534 } |
531 | 535 |
532 /////////////////////////////////////////////////////////////////////////// | 536 /////////////////////////////////////////////////////////////////////////// |
533 // compute the partial coverage | 537 // compute the partial coverage |
534 SkString inCoverage; | 538 SkString inCoverage; |
535 GrSLConstantVec knownCoverageValue = this->genInputCoverage(&builder, &inCov
erage); | 539 GrSLConstantVec knownCoverageValue = this->genInputCoverage(&builder, &inCov
erage); |
536 | 540 |
537 for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { | 541 for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { |
538 effectUniformArrays[e] = &fEffectStates[e + fDesc.numColorEffects()].fSa
mplerUnis; | 542 effectUniformArrays[e] = &fCoverageEffects[e].fSamplerUnis; |
539 } | 543 } |
540 | 544 |
541 builder.emitEffects(stages + fDesc.numColorEffects(), | 545 builder.emitEffects(coverageStages, |
542 fDesc.getEffectKeys() + fDesc.numColorEffects(), | 546 fDesc.getEffectKeys() + fDesc.numColorEffects(), |
543 fDesc.numCoverageEffects(), | 547 fDesc.numCoverageEffects(), |
544 &inCoverage, | 548 &inCoverage, |
545 &knownCoverageValue, | 549 &knownCoverageValue, |
546 effectUniformArrays.get(), | 550 effectUniformArrays.get(), |
547 glEffects.get()); | 551 glEffects.get()); |
548 for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { | 552 for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { |
549 fEffectStates[e + fDesc.numColorEffects()].fGLEffect = glEffects[e]; | 553 fCoverageEffects[e].fGLEffect = glEffects[e]; |
550 } | 554 } |
551 | 555 |
552 // discard if coverage is zero | 556 // discard if coverage is zero |
553 if (header.fDiscardIfZeroCoverage && kOnes_GrSLConstantVec != knownCoverageV
alue) { | 557 if (header.fDiscardIfZeroCoverage && kOnes_GrSLConstantVec != knownCoverageV
alue) { |
554 if (kZeros_GrSLConstantVec == knownCoverageValue) { | 558 if (kZeros_GrSLConstantVec == knownCoverageValue) { |
555 // This is unfortunate. | 559 // This is unfortunate. |
556 builder.fsCodeAppend("\tdiscard;\n"); | 560 builder.fsCodeAppend("\tdiscard;\n"); |
557 } else { | 561 } else { |
558 builder.fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\
t\tdiscard;\n\t}\n", | 562 builder.fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\
t\tdiscard;\n\t}\n", |
559 inCoverage.c_str()); | 563 inCoverage.c_str()); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
747 } | 751 } |
748 | 752 |
749 void GrGLProgram::initSamplerUniforms() { | 753 void GrGLProgram::initSamplerUniforms() { |
750 GL_CALL(UseProgram(fProgramID)); | 754 GL_CALL(UseProgram(fProgramID)); |
751 GrGLint texUnitIdx = 0; | 755 GrGLint texUnitIdx = 0; |
752 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopySam
plerUni) { | 756 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopySam
plerUni) { |
753 fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitId
x); | 757 fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitId
x); |
754 fDstCopyTexUnit = texUnitIdx++; | 758 fDstCopyTexUnit = texUnitIdx++; |
755 } | 759 } |
756 | 760 |
757 for (int e = 0; e < fEffectStates.count(); ++e) { | 761 for (int e = 0; e < fColorEffects.count(); ++e) { |
758 int numSamplers = fEffectStates[e].fSamplerUnis.count(); | 762 this->initEffectSamplerUniforms(&fColorEffects[e], &texUnitIdx); |
759 fEffectStates[e].fTextureUnits.reset(numSamplers); | 763 } |
760 for (int s = 0; s < numSamplers; ++s) { | 764 |
761 UniformHandle handle = fEffectStates[e].fSamplerUnis[s]; | 765 for (int e = 0; e < fCoverageEffects.count(); ++e) { |
762 if (GrGLUniformManager::kInvalidUniformHandle != handle) { | 766 this->initEffectSamplerUniforms(&fCoverageEffects[e], &texUnitIdx); |
763 fUniformManager.setSampler(handle, texUnitIdx); | 767 } |
764 fEffectStates[e].fTextureUnits[s] = texUnitIdx++; | 768 } |
765 } | 769 |
| 770 void GrGLProgram::initEffectSamplerUniforms(EffectAndSamplers* effect, int* texU
nitIdx) { |
| 771 int numSamplers = effect->fSamplerUnis.count(); |
| 772 effect->fTextureUnits.reset(numSamplers); |
| 773 for (int s = 0; s < numSamplers; ++s) { |
| 774 UniformHandle handle = effect->fSamplerUnis[s]; |
| 775 if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
| 776 fUniformManager.setSampler(handle, *texUnitIdx); |
| 777 effect->fTextureUnits[s] = (*texUnitIdx)++; |
766 } | 778 } |
767 } | 779 } |
768 } | 780 } |
769 | 781 |
770 /////////////////////////////////////////////////////////////////////////////// | 782 /////////////////////////////////////////////////////////////////////////////// |
771 | 783 |
| 784 void GrGLProgram::setEffectData(GrGpuGL* gpu, |
| 785 const GrEffectStage& stage, |
| 786 const EffectAndSamplers& effect) { |
| 787 |
| 788 // Let the GrGLEffect set its data. |
| 789 bool explicitLocalCoords = -1 != fDesc.getHeader().fLocalCoordAttributeIndex
; |
| 790 GrDrawEffect drawEffect(stage, explicitLocalCoords); |
| 791 effect.fGLEffect->setData(fUniformManager, drawEffect); |
| 792 |
| 793 // Bind the texures for the effect. |
| 794 int numSamplers = effect.fSamplerUnis.count(); |
| 795 GrAssert((*stage.getEffect())->numTextures() == numSamplers); |
| 796 for (int s = 0; s < numSamplers; ++s) { |
| 797 UniformHandle handle = effect.fSamplerUnis[s]; |
| 798 if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
| 799 const GrTextureAccess& access = (*stage.getEffect())->textureAccess(
s); |
| 800 GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture()
); |
| 801 int unit = effect.fTextureUnits[s]; |
| 802 gpu->bindTexture(unit, access.getParams(), texture); |
| 803 } |
| 804 } |
| 805 } |
| 806 |
772 void GrGLProgram::setData(GrGpuGL* gpu, | 807 void GrGLProgram::setData(GrGpuGL* gpu, |
773 GrDrawState::BlendOptFlags blendOpts, | 808 GrDrawState::BlendOptFlags blendOpts, |
774 const GrEffectStage* stages[], | 809 const GrEffectStage* colorStages[], |
| 810 const GrEffectStage* coverageStages[], |
775 const GrDeviceCoordTexture* dstCopy, | 811 const GrDeviceCoordTexture* dstCopy, |
776 SharedGLState* sharedState) { | 812 SharedGLState* sharedState) { |
777 const GrDrawState& drawState = gpu->getDrawState(); | 813 const GrDrawState& drawState = gpu->getDrawState(); |
778 | 814 |
779 GrColor color; | 815 GrColor color; |
780 GrColor coverage; | 816 GrColor coverage; |
781 if (blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag) { | 817 if (blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag) { |
782 color = 0; | 818 color = 0; |
783 coverage = 0; | 819 coverage = 0; |
784 } else if (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) { | 820 } else if (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 fUniformHandles.fDstCopyScaleUni); | 857 fUniformHandles.fDstCopyScaleUni); |
822 GrAssert(GrGLUniformManager::kInvalidUniformHandle == | 858 GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
823 fUniformHandles.fDstCopySamplerUni); | 859 fUniformHandles.fDstCopySamplerUni); |
824 } | 860 } |
825 } else { | 861 } else { |
826 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopyTopLeftUni); | 862 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopyTopLeftUni); |
827 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopyScaleUni); | 863 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopyScaleUni); |
828 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopySamplerUni); | 864 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fUniformHandles.fD
stCopySamplerUni); |
829 } | 865 } |
830 | 866 |
831 int numEffects = fDesc.numTotalEffects(); | 867 for (int e = 0; e < fColorEffects.count(); ++e) { |
832 for (int e = 0; e < numEffects; ++e) { | |
833 GrAssert(NULL != stages[e]); | |
834 // We may have omitted the GrGLEffect because of the color filter logic
in genProgram. | 868 // We may have omitted the GrGLEffect because of the color filter logic
in genProgram. |
835 // This can be removed when the color filter is an effect. | 869 // This can be removed when the color filter is an effect. |
836 if (NULL != fEffectStates[e].fGLEffect) { | 870 if (NULL != fColorEffects[e].fGLEffect) { |
837 bool explicitLocalCoords = -1 != fDesc.getHeader().fLocalCoordAttrib
uteIndex; | 871 this->setEffectData(gpu, *colorStages[e], fColorEffects[e]); |
838 GrDrawEffect drawEffect(*stages[e], explicitLocalCoords); | 872 } |
839 fEffectStates[e].fGLEffect->setData(fUniformManager, drawEffect); | 873 } |
840 int numSamplers = fEffectStates[e].fSamplerUnis.count(); | 874 |
841 GrAssert((*stages[e]->getEffect())->numTextures() == numSamplers); | 875 for (int e = 0; e < fCoverageEffects.count(); ++e) { |
842 for (int s = 0; s < numSamplers; ++s) { | 876 if (NULL != fCoverageEffects[e].fGLEffect) { |
843 UniformHandle handle = fEffectStates[e].fSamplerUnis[s]; | 877 this->setEffectData(gpu, *coverageStages[e], fCoverageEffects[e]); |
844 if (GrGLUniformManager::kInvalidUniformHandle != handle) { | |
845 const GrTextureAccess& access = (*stages[e]->getEffect())->t
extureAccess(s); | |
846 GrGLTexture* texture = static_cast<GrGLTexture*>(access.getT
exture()); | |
847 int unit = fEffectStates[e].fTextureUnits[s]; | |
848 gpu->bindTexture(unit, access.getParams(), texture); | |
849 } | |
850 } | |
851 } | 878 } |
852 } | 879 } |
853 } | 880 } |
854 | 881 |
855 void GrGLProgram::setColor(const GrDrawState& drawState, | 882 void GrGLProgram::setColor(const GrDrawState& drawState, |
856 GrColor color, | 883 GrColor color, |
857 SharedGLState* sharedState) { | 884 SharedGLState* sharedState) { |
858 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); | 885 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
859 if (!drawState.hasColorVertexAttribute()) { | 886 if (!drawState.hasColorVertexAttribute()) { |
860 switch (header.fColorInput) { | 887 switch (header.fColorInput) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
974 SkScalarToFloat(m[SkMatrix::kMTransX]), | 1001 SkScalarToFloat(m[SkMatrix::kMTransX]), |
975 SkScalarToFloat(m[SkMatrix::kMTransY]), | 1002 SkScalarToFloat(m[SkMatrix::kMTransY]), |
976 SkScalarToFloat(m[SkMatrix::kMPersp2]) | 1003 SkScalarToFloat(m[SkMatrix::kMPersp2]) |
977 }; | 1004 }; |
978 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); | 1005 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); |
979 fMatrixState.fViewMatrix = drawState.getViewMatrix(); | 1006 fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
980 fMatrixState.fRenderTargetSize = size; | 1007 fMatrixState.fRenderTargetSize = size; |
981 fMatrixState.fRenderTargetOrigin = rt->origin(); | 1008 fMatrixState.fRenderTargetOrigin = rt->origin(); |
982 } | 1009 } |
983 } | 1010 } |
OLD | NEW |