bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrGLProgramDesc.h" |
| 9 | #include "GrBackendEffectFactory.h" |
| 10 | #include "GrDrawEffect.h" |
| 11 | #include "GrEffect.h" |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 12 | #include "GrGLShaderBuilder.h" |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 13 | #include "GrGpuGL.h" |
| 14 | |
| 15 | void GrGLProgramDesc::Build(const GrDrawState& drawState, |
| 16 | bool isPoints, |
| 17 | GrDrawState::BlendOptFlags blendOpts, |
| 18 | GrBlendCoeff srcCoeff, |
| 19 | GrBlendCoeff dstCoeff, |
| 20 | const GrGpuGL* gpu, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 21 | const GrDeviceCoordTexture* dstCopy, |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 22 | GrGLProgramDesc* desc) { |
| 23 | |
| 24 | // This should already have been caught |
| 25 | GrAssert(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts)); |
| 26 | |
| 27 | bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 28 | |
| 29 | bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag | |
| 30 | GrDrawState::kEmitCoverage_BlendOptFlag)); |
| 31 | |
| 32 | // The descriptor is used as a cache key. Thus when a field of the |
| 33 | // descriptor will not affect program generation (because of the attribute |
| 34 | // bindings in use or other descriptor field settings) it should be set |
| 35 | // to a canonical value to avoid duplicate programs with different keys. |
| 36 | |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 37 | |
| 38 | desc->fEmitsPointSize = isPoints; |
| 39 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 40 | bool requiresColorAttrib = !skipColor && drawState.hasColorVertexAttribute(); |
| 41 | bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAttribute(); |
| 42 | // we only need the local coords if we're actually going to generate effect code |
| 43 | bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) && |
| 44 | drawState.hasLocalCoordAttribute(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 45 | |
| 46 | // fColorInput/fCoverageInput records how colors are specified for the program so we strip the |
| 47 | // bits from the bindings to avoid false negatives when searching for an existing program in the |
| 48 | // cache. |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 49 | |
| 50 | desc->fColorFilterXfermode = skipColor ? SkXfermode::kDst_Mode : drawState.getColorFilterMode(); |
| 51 | |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 52 | |
| 53 | bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 54 | bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) || |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 55 | (!requiresColorAttrib && 0xffffffff == drawState.getColor()); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 56 | if (colorIsTransBlack) { |
| 57 | desc->fColorInput = kTransBlack_ColorInput; |
| 58 | } else if (colorIsSolidWhite) { |
| 59 | desc->fColorInput = kSolidWhite_ColorInput; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 60 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresColorAttrib) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 61 | desc->fColorInput = kUniform_ColorInput; |
| 62 | } else { |
| 63 | desc->fColorInput = kAttribute_ColorInput; |
| 64 | } |
| 65 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 66 | bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.getCoverage(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 67 | |
| 68 | if (skipCoverage) { |
| 69 | desc->fCoverageInput = kTransBlack_ColorInput; |
| 70 | } else if (covIsSolidWhite) { |
| 71 | desc->fCoverageInput = kSolidWhite_ColorInput; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 72 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresCoverageAttrib) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 73 | desc->fCoverageInput = kUniform_ColorInput; |
| 74 | } else { |
| 75 | desc->fCoverageInput = kAttribute_ColorInput; |
| 76 | } |
| 77 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 78 | bool readsDst = false; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 79 | int lastEnabledStage = -1; |
| 80 | |
| 81 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 82 | |
| 83 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCoverage; |
| 84 | if (!skip && drawState.isStageEnabled(s)) { |
| 85 | lastEnabledStage = s; |
| 86 | const GrEffectRef& effect = *drawState.getStage(s).getEffect(); |
| 87 | const GrBackendEffectFactory& factory = effect->getFactory(); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 88 | GrDrawEffect drawEffect(drawState.getStage(s), requiresLocalCoordAttrib); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 89 | desc->fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps()); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 90 | if (effect->willReadDst()) { |
| 91 | readsDst = true; |
| 92 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 93 | } else { |
| 94 | desc->fEffectKeys[s] = 0; |
| 95 | } |
| 96 | } |
| 97 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 98 | if (readsDst) { |
| 99 | GrAssert(NULL != dstCopy); |
| 100 | desc->fDstRead = GrGLShaderBuilder::KeyForDstRead(dstCopy->texture(), gpu->glCaps()); |
| 101 | GrAssert(0 != desc->fDstRead); |
| 102 | } else { |
| 103 | desc->fDstRead = 0; |
| 104 | } |
| 105 | |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 106 | desc->fDualSrcOutput = kNone_DualSrcOutput; |
| 107 | |
| 108 | // Currently the experimental GS will only work with triangle prims (and it doesn't do anything |
| 109 | // other than pass through values from the VS to the FS anyway). |
| 110 | #if GR_GL_EXPERIMENTAL_GS |
| 111 | #if 0 |
| 112 | desc->fExperimentalGS = gpu->caps().geometryShaderSupport(); |
| 113 | #else |
| 114 | desc->fExperimentalGS = false; |
| 115 | #endif |
| 116 | #endif |
| 117 | |
| 118 | // We leave this set to kNumStages until we discover that the coverage/color distinction is |
| 119 | // material to the generated program. We do this to avoid distinct keys that generate equivalent |
| 120 | // programs. |
| 121 | desc->fFirstCoverageStage = GrDrawState::kNumStages; |
| 122 | // This tracks the actual first coverage stage. |
| 123 | int firstCoverageStage = GrDrawState::kNumStages; |
| 124 | desc->fDiscardIfZeroCoverage = false; // Enabled below if stenciling and there is coverage. |
| 125 | bool hasCoverage = false; |
| 126 | // If we're rendering coverage-as-color then it's as though there are no coverage stages. |
| 127 | if (!drawState.isCoverageDrawing()) { |
| 128 | // We can have coverage either through a stage or coverage vertex attributes. |
| 129 | if (drawState.getFirstCoverageStage() <= lastEnabledStage) { |
| 130 | firstCoverageStage = drawState.getFirstCoverageStage(); |
| 131 | hasCoverage = true; |
| 132 | } else { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 133 | hasCoverage = requiresCoverageAttrib; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | if (hasCoverage) { |
| 138 | // color filter is applied between color/coverage computation |
| 139 | if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) { |
| 140 | desc->fFirstCoverageStage = firstCoverageStage; |
| 141 | } |
| 142 | |
| 143 | // If we're stenciling then we want to discard samples that have zero coverage |
| 144 | if (drawState.getStencil().doesWrite()) { |
| 145 | desc->fDiscardIfZeroCoverage = true; |
| 146 | desc->fFirstCoverageStage = firstCoverageStage; |
| 147 | } |
| 148 | |
| 149 | if (gpu->caps()->dualSourceBlendingSupport() && |
| 150 | !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | |
| 151 | GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { |
| 152 | if (kZero_GrBlendCoeff == dstCoeff) { |
| 153 | // write the coverage value to second color |
| 154 | desc->fDualSrcOutput = kCoverage_DualSrcOutput; |
| 155 | desc->fFirstCoverageStage = firstCoverageStage; |
| 156 | } else if (kSA_GrBlendCoeff == dstCoeff) { |
| 157 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
| 158 | desc->fDualSrcOutput = kCoverageISA_DualSrcOutput; |
| 159 | desc->fFirstCoverageStage = firstCoverageStage; |
| 160 | } else if (kSC_GrBlendCoeff == dstCoeff) { |
| 161 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
| 162 | desc->fDualSrcOutput = kCoverageISC_DualSrcOutput; |
| 163 | desc->fFirstCoverageStage = firstCoverageStage; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 168 | desc->fPositionAttributeIndex = drawState.positionAttributeIndex(); |
| 169 | desc->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex(); |
| 170 | |
| 171 | // For constant color and coverage we need an attribute with an index beyond those already set |
| 172 | int availableAttributeIndex = drawState.getVertexAttribCount(); |
| 173 | if (requiresColorAttrib) { |
| 174 | desc->fColorAttributeIndex = drawState.colorVertexAttributeIndex(); |
| 175 | } else if (GrGLProgramDesc::kAttribute_ColorInput == desc->fColorInput) { |
| 176 | GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
| 177 | desc->fColorAttributeIndex = availableAttributeIndex; |
| 178 | availableAttributeIndex++; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 179 | } else { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 180 | desc->fColorAttributeIndex = -1; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 181 | } |
| 182 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 183 | if (requiresCoverageAttrib) { |
| 184 | desc->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex(); |
| 185 | } else if (GrGLProgramDesc::kAttribute_ColorInput == desc->fCoverageInput) { |
| 186 | GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
| 187 | desc->fCoverageAttributeIndex = availableAttributeIndex; |
| 188 | } else { |
| 189 | desc->fCoverageAttributeIndex = -1; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 190 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 191 | } |