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 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 15 | #include "SkChecksum.h" |
| 16 | |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 17 | void GrGLProgramDesc::Build(const GrDrawState& drawState, |
| 18 | bool isPoints, |
| 19 | GrDrawState::BlendOptFlags blendOpts, |
| 20 | GrBlendCoeff srcCoeff, |
| 21 | GrBlendCoeff dstCoeff, |
| 22 | const GrGpuGL* gpu, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 23 | const GrDeviceCoordTexture* dstCopy, |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 24 | const GrEffectStage* stages[], |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 25 | GrGLProgramDesc* desc) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 26 | // This should already have been caught |
| 27 | GrAssert(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts)); |
| 28 | |
| 29 | bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 30 | |
| 31 | bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag | |
| 32 | GrDrawState::kEmitCoverage_BlendOptFlag)); |
| 33 | |
| 34 | // The descriptor is used as a cache key. Thus when a field of the |
| 35 | // descriptor will not affect program generation (because of the attribute |
| 36 | // bindings in use or other descriptor field settings) it should be set |
| 37 | // to a canonical value to avoid duplicate programs with different keys. |
| 38 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 39 | bool requiresColorAttrib = !skipColor && drawState.hasColorVertexAttribute(); |
| 40 | bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAttribute(); |
| 41 | // we only need the local coords if we're actually going to generate effect code |
| 42 | bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) && |
| 43 | drawState.hasLocalCoordAttribute(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 45 | bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 46 | bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) || |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 47 | (!requiresColorAttrib && 0xffffffff == drawState.getColor()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 48 | |
| 49 | // Do an initial loop over the stages to count them. We count the color and coverage effects |
| 50 | // separately here. Later we may decide the distinction doesn't matter and will count all |
| 51 | // effects as color in desc. Two things will allow simplication of this mess: GrDrawState will |
| 52 | // have tight lists of color and coverage stages rather than a fixed size array with NULLS and |
| 53 | // the xfermode-color filter will be removed. |
| 54 | int colorEffectCnt = 0; |
| 55 | int coverageEffectCnt = 0; |
| 56 | if (!skipColor) { |
| 57 | for (int s = 0; s < drawState.getFirstCoverageStage(); ++s) { |
| 58 | if (drawState.isStageEnabled(s)) { |
| 59 | stages[colorEffectCnt] = &drawState.getStage(s); |
| 60 | ++colorEffectCnt; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | if (!skipCoverage) { |
| 65 | for (int s = drawState.getFirstCoverageStage(); s < GrDrawState::kNumStages; ++s) { |
| 66 | if (drawState.isStageEnabled(s)) { |
| 67 | stages[colorEffectCnt + coverageEffectCnt] = &drawState.getStage(s); |
| 68 | ++coverageEffectCnt; |
| 69 | } |
| 70 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 71 | } |
| 72 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 73 | size_t newKeyLength = KeyLength(colorEffectCnt + coverageEffectCnt); |
| 74 | bool allocChanged; |
| 75 | desc->fKey.reset(newKeyLength, SkAutoMalloc::kAlloc_OnShrink, &allocChanged); |
| 76 | if (allocChanged || !desc->fInitialized) { |
| 77 | // make sure any padding in the header is zero if we we haven't used this allocation before. |
| 78 | memset(desc->header(), 0, kHeaderSize); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 79 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 80 | // write the key length |
| 81 | *desc->atOffset<uint32_t, kLengthOffset>() = newKeyLength; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 82 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 83 | KeyHeader* header = desc->header(); |
| 84 | EffectKey* effectKeys = desc->effectKeys(); |
| 85 | |
| 86 | int currEffectKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 87 | bool readsDst = false; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 88 | bool readFragPosition = false; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 89 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 90 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCoverage; |
| 91 | if (!skip && drawState.isStageEnabled(s)) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 92 | const GrEffectRef& effect = *drawState.getStage(s).getEffect(); |
| 93 | const GrBackendEffectFactory& factory = effect->getFactory(); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 94 | GrDrawEffect drawEffect(drawState.getStage(s), requiresLocalCoordAttrib); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 95 | effectKeys[currEffectKey] = factory.glEffectKey(drawEffect, gpu->glCaps()); |
| 96 | ++currEffectKey; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 97 | if (effect->willReadDstColor()) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 98 | readsDst = true; |
| 99 | } |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 100 | if (effect->willReadFragmentPosition()) { |
| 101 | readFragPosition = true; |
| 102 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 106 | header->fEmitsPointSize = isPoints; |
| 107 | header->fColorFilterXfermode = skipColor ? SkXfermode::kDst_Mode : drawState.getColorFilterMode(); |
| 108 | |
| 109 | // Currently the experimental GS will only work with triangle prims (and it doesn't do anything |
| 110 | // other than pass through values from the VS to the FS anyway). |
| 111 | #if GR_GL_EXPERIMENTAL_GS |
| 112 | #if 0 |
| 113 | header->fExperimentalGS = gpu->caps().geometryShaderSupport(); |
| 114 | #else |
| 115 | header->fExperimentalGS = false; |
| 116 | #endif |
| 117 | #endif |
| 118 | if (colorIsTransBlack) { |
| 119 | header->fColorInput = kTransBlack_ColorInput; |
| 120 | } else if (colorIsSolidWhite) { |
| 121 | header->fColorInput = kSolidWhite_ColorInput; |
| 122 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresColorAttrib) { |
| 123 | header->fColorInput = kUniform_ColorInput; |
| 124 | } else { |
| 125 | header->fColorInput = kAttribute_ColorInput; |
| 126 | } |
| 127 | |
| 128 | bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.getCoverage(); |
| 129 | |
| 130 | if (skipCoverage) { |
| 131 | header->fCoverageInput = kTransBlack_ColorInput; |
| 132 | } else if (covIsSolidWhite) { |
| 133 | header->fCoverageInput = kSolidWhite_ColorInput; |
| 134 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresCoverageAttrib) { |
| 135 | header->fCoverageInput = kUniform_ColorInput; |
| 136 | } else { |
| 137 | header->fCoverageInput = kAttribute_ColorInput; |
| 138 | } |
| 139 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 140 | if (readsDst) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 141 | GrAssert(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport()); |
| 142 | const GrTexture* dstCopyTexture = NULL; |
| 143 | if (NULL != dstCopy) { |
| 144 | dstCopyTexture = dstCopy->texture(); |
| 145 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 146 | header->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, gpu->glCaps()); |
| 147 | GrAssert(0 != header->fDstReadKey); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 148 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 149 | header->fDstReadKey = 0; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (readFragPosition) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 153 | header->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(drawState.getRenderTarget(), |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 154 | gpu->glCaps()); |
| 155 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 156 | header->fFragPosKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 157 | } |
| 158 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 159 | // Record attribute indices |
| 160 | header->fPositionAttributeIndex = drawState.positionAttributeIndex(); |
| 161 | header->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex(); |
| 162 | |
| 163 | // For constant color and coverage we need an attribute with an index beyond those already set |
| 164 | int availableAttributeIndex = drawState.getVertexAttribCount(); |
| 165 | if (requiresColorAttrib) { |
| 166 | header->fColorAttributeIndex = drawState.colorVertexAttributeIndex(); |
| 167 | } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) { |
| 168 | GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
| 169 | header->fColorAttributeIndex = availableAttributeIndex; |
| 170 | availableAttributeIndex++; |
| 171 | } else { |
| 172 | header->fColorAttributeIndex = -1; |
| 173 | } |
| 174 | |
| 175 | if (requiresCoverageAttrib) { |
| 176 | header->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex(); |
| 177 | } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) { |
| 178 | GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
| 179 | header->fCoverageAttributeIndex = availableAttributeIndex; |
| 180 | } else { |
| 181 | header->fCoverageAttributeIndex = -1; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 182 | } |
| 183 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 184 | // Here we deal with whether/how we handle color and coverage separately. |
| 185 | |
| 186 | // Set these defaults and then possibly change our mind if there is coverage. |
| 187 | header->fDiscardIfZeroCoverage = false; |
| 188 | header->fCoverageOutput = kModulate_CoverageOutput; |
| 189 | |
| 190 | // If we do have coverage determine whether it matters. |
| 191 | bool separateCoverageFromColor = false; |
| 192 | if (!drawState.isCoverageDrawing() && (coverageEffectCnt > 0 || requiresCoverageAttrib)) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 193 | // color filter is applied between color/coverage computation |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 194 | if (SkXfermode::kDst_Mode != header->fColorFilterXfermode) { |
| 195 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // If we're stenciling then we want to discard samples that have zero coverage |
| 199 | if (drawState.getStencil().doesWrite()) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 200 | header->fDiscardIfZeroCoverage = true; |
| 201 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (gpu->caps()->dualSourceBlendingSupport() && |
| 205 | !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | |
| 206 | GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { |
| 207 | if (kZero_GrBlendCoeff == dstCoeff) { |
| 208 | // write the coverage value to second color |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 209 | header->fCoverageOutput = kSecondaryCoverage_CoverageOutput; |
| 210 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 211 | } else if (kSA_GrBlendCoeff == dstCoeff) { |
| 212 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 213 | header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput; |
| 214 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 215 | } else if (kSC_GrBlendCoeff == dstCoeff) { |
| 216 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 217 | header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput; |
| 218 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 219 | } |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 220 | } else if (readsDst && |
bsalomon@google.com | 0c89db2 | 2013-05-15 17:53:04 +0000 | [diff] [blame] | 221 | kOne_GrBlendCoeff == srcCoeff && |
| 222 | kZero_GrBlendCoeff == dstCoeff) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 223 | header->fCoverageOutput = kCombineWithDst_CoverageOutput; |
| 224 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 227 | if (separateCoverageFromColor) { |
| 228 | header->fColorEffectCnt = colorEffectCnt; |
| 229 | header->fCoverageEffectCnt = coverageEffectCnt; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 230 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 231 | header->fColorEffectCnt = colorEffectCnt + coverageEffectCnt; |
| 232 | header->fCoverageEffectCnt = 0; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 233 | } |
| 234 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 235 | *desc->checksum() = 0; |
| 236 | *desc->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(desc->fKey.get()), |
| 237 | newKeyLength); |
| 238 | desc->fInitialized = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 239 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 240 | |
| 241 | GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
| 242 | fInitialized = other.fInitialized; |
| 243 | if (fInitialized) { |
| 244 | size_t keyLength = other.keyLength(); |
| 245 | fKey.reset(keyLength); |
| 246 | memcpy(fKey.get(), other.fKey.get(), keyLength); |
| 247 | } |
| 248 | return *this; |
| 249 | } |
| 250 | |