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 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 8 | #include "gl/builders/GrGLProgramBuilder.h" |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 9 | #include "GrGLProgramDesc.h" |
| 10 | #include "GrBackendEffectFactory.h" |
| 11 | #include "GrDrawEffect.h" |
| 12 | #include "GrEffect.h" |
| 13 | #include "GrGpuGL.h" |
| 14 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 15 | #include "SkChecksum.h" |
| 16 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 17 | bool GrGLProgramDesc::GetEffectKeyAndUpdateStats(const GrEffectStage& stage, |
| 18 | const GrGLCaps& caps, |
| 19 | bool useExplicitLocalCoords, |
| 20 | GrEffectKeyBuilder* b, |
| 21 | uint16_t* effectKeySize, |
| 22 | bool* setTrueIfReadsDst, |
| 23 | bool* setTrueIfReadsPos, |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 24 | bool* setTrueIfRequiresVertexShader) { |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 25 | const GrBackendEffectFactory& factory = stage.getEffect()->getFactory(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 26 | GrDrawEffect drawEffect(stage, useExplicitLocalCoords); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 27 | if (stage.getEffect()->willReadDstColor()) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 28 | *setTrueIfReadsDst = true; |
| 29 | } |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 30 | if (stage.getEffect()->willReadFragmentPosition()) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 31 | *setTrueIfReadsPos = true; |
| 32 | } |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 33 | if (stage.getEffect()->requiresVertexShader()) { |
| 34 | *setTrueIfRequiresVertexShader = true; |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 35 | } |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 36 | factory.getGLEffectKey(drawEffect, caps, b); |
| 37 | size_t size = b->size(); |
| 38 | if (size > SK_MaxU16) { |
| 39 | *effectKeySize = 0; // suppresses a warning. |
| 40 | return false; |
| 41 | } |
| 42 | *effectKeySize = SkToU16(size); |
| 43 | if (!GrGLProgramEffects::GenEffectMetaKey(drawEffect, caps, b)) { |
| 44 | return false; |
| 45 | } |
| 46 | return true; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 47 | } |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 48 | |
| 49 | bool GrGLProgramDesc::Build(const GrDrawState& drawState, |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 50 | GrGpu::DrawType drawType, |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 51 | GrDrawState::BlendOptFlags blendOpts, |
| 52 | GrBlendCoeff srcCoeff, |
| 53 | GrBlendCoeff dstCoeff, |
| 54 | const GrGpuGL* gpu, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 55 | const GrDeviceCoordTexture* dstCopy, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 56 | SkTArray<const GrEffectStage*, true>* colorStages, |
| 57 | SkTArray<const GrEffectStage*, true>* coverageStages, |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 58 | GrGLProgramDesc* desc) { |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 59 | colorStages->reset(); |
| 60 | coverageStages->reset(); |
| 61 | |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 62 | // This should already have been caught |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 63 | SkASSERT(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts)); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 64 | |
| 65 | bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 66 | |
| 67 | bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag | |
| 68 | GrDrawState::kEmitCoverage_BlendOptFlag)); |
egdaniel | 02cafcc | 2014-07-21 11:37:28 -0700 | [diff] [blame] | 69 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 70 | int firstEffectiveColorStage = 0; |
| 71 | bool inputColorIsUsed = true; |
| 72 | if (!skipColor) { |
| 73 | firstEffectiveColorStage = drawState.numColorStages(); |
| 74 | while (firstEffectiveColorStage > 0 && inputColorIsUsed) { |
| 75 | --firstEffectiveColorStage; |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 76 | const GrEffect* effect = drawState.getColorStage(firstEffectiveColorStage).getEffect(); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 77 | inputColorIsUsed = effect->willUseInputColor(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | int firstEffectiveCoverageStage = 0; |
| 82 | bool inputCoverageIsUsed = true; |
| 83 | if (!skipCoverage) { |
| 84 | firstEffectiveCoverageStage = drawState.numCoverageStages(); |
| 85 | while (firstEffectiveCoverageStage > 0 && inputCoverageIsUsed) { |
| 86 | --firstEffectiveCoverageStage; |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 87 | const GrEffect* effect = drawState.getCoverageStage(firstEffectiveCoverageStage).getEffect(); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 88 | inputCoverageIsUsed = effect->willUseInputColor(); |
| 89 | } |
| 90 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 91 | |
| 92 | // The descriptor is used as a cache key. Thus when a field of the |
| 93 | // descriptor will not affect program generation (because of the attribute |
| 94 | // bindings in use or other descriptor field settings) it should be set |
| 95 | // to a canonical value to avoid duplicate programs with different keys. |
| 96 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 97 | bool requiresColorAttrib = !skipColor && drawState.hasColorVertexAttribute(); |
| 98 | bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAttribute(); |
| 99 | // we only need the local coords if we're actually going to generate effect code |
| 100 | bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) && |
| 101 | drawState.hasLocalCoordAttribute(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 103 | bool readsDst = false; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 104 | bool readFragPosition = false; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 105 | |
| 106 | // Provide option for shader programs without vertex shader only when drawing paths. |
| 107 | bool requiresVertexShader = !GrGpu::IsPathRenderingDrawType(drawType); |
| 108 | |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 109 | int numStages = 0; |
| 110 | if (!skipColor) { |
| 111 | numStages += drawState.numColorStages() - firstEffectiveColorStage; |
| 112 | } |
| 113 | if (!skipCoverage) { |
| 114 | numStages += drawState.numCoverageStages() - firstEffectiveCoverageStage; |
| 115 | } |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 116 | GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t)); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 117 | // Make room for everything up to and including the array of offsets to effect keys. |
| 118 | desc->fKey.reset(); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 119 | desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * numStages); |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 120 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 121 | int offsetAndSizeIndex = 0; |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 122 | bool effectKeySuccess = true; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 123 | if (!skipColor) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 124 | for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) { |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 125 | uint16_t* offsetAndSize = |
| 126 | reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset + |
| 127 | offsetAndSizeIndex * 2 * sizeof(uint16_t)); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 128 | |
| 129 | GrEffectKeyBuilder b(&desc->fKey); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 130 | uint16_t effectKeySize; |
| 131 | uint32_t effectOffset = desc->fKey.count(); |
| 132 | effectKeySuccess |= GetEffectKeyAndUpdateStats( |
| 133 | drawState.getColorStage(s), gpu->glCaps(), |
| 134 | requiresLocalCoordAttrib, &b, |
| 135 | &effectKeySize, &readsDst, |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 136 | &readFragPosition, &requiresVertexShader); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 137 | effectKeySuccess |= (effectOffset <= SK_MaxU16); |
| 138 | |
| 139 | offsetAndSize[0] = SkToU16(effectOffset); |
| 140 | offsetAndSize[1] = effectKeySize; |
| 141 | ++offsetAndSizeIndex; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | if (!skipCoverage) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 145 | for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) { |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 146 | uint16_t* offsetAndSize = |
| 147 | reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset + |
| 148 | offsetAndSizeIndex * 2 * sizeof(uint16_t)); |
| 149 | |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 150 | GrEffectKeyBuilder b(&desc->fKey); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 151 | uint16_t effectKeySize; |
| 152 | uint32_t effectOffset = desc->fKey.count(); |
| 153 | effectKeySuccess |= GetEffectKeyAndUpdateStats( |
| 154 | drawState.getCoverageStage(s), gpu->glCaps(), |
| 155 | requiresLocalCoordAttrib, &b, |
| 156 | &effectKeySize, &readsDst, |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 157 | &readFragPosition, &requiresVertexShader); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 158 | effectKeySuccess |= (effectOffset <= SK_MaxU16); |
| 159 | |
| 160 | offsetAndSize[0] = SkToU16(effectOffset); |
| 161 | offsetAndSize[1] = effectKeySize; |
| 162 | ++offsetAndSizeIndex; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 165 | if (!effectKeySuccess) { |
| 166 | desc->fKey.reset(); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | KeyHeader* header = desc->header(); |
| 171 | // make sure any padding in the header is zeroed. |
| 172 | memset(desc->header(), 0, kHeaderSize); |
| 173 | |
| 174 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 175 | // below here. |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 176 | |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 177 | header->fRequiresVertexShader = requiresVertexShader || requiresLocalCoordAttrib; |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 178 | header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 179 | |
| 180 | // Currently the experimental GS will only work with triangle prims (and it doesn't do anything |
| 181 | // other than pass through values from the VS to the FS anyway). |
| 182 | #if GR_GL_EXPERIMENTAL_GS |
| 183 | #if 0 |
| 184 | header->fExperimentalGS = gpu->caps().geometryShaderSupport(); |
| 185 | #else |
| 186 | header->fExperimentalGS = false; |
| 187 | #endif |
| 188 | #endif |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 189 | bool defaultToUniformInputs = GR_GL_NO_CONSTANT_ATTRIBUTES || gpu->caps()->pathRenderingSupport(); |
| 190 | |
egdaniel | 842b086 | 2014-09-02 10:01:30 -0700 | [diff] [blame] | 191 | if (!inputColorIsUsed && !skipColor) { |
| 192 | header->fColorInput = kAllOnes_ColorInput; |
| 193 | } else if (defaultToUniformInputs && !requiresColorAttrib && inputColorIsUsed) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 194 | header->fColorInput = kUniform_ColorInput; |
| 195 | } else { |
| 196 | header->fColorInput = kAttribute_ColorInput; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 197 | header->fRequiresVertexShader = true; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 198 | } |
| 199 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 200 | bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.getCoverageColor(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 201 | |
egdaniel | 02cafcc | 2014-07-21 11:37:28 -0700 | [diff] [blame] | 202 | if ((covIsSolidWhite || !inputCoverageIsUsed) && !skipCoverage) { |
egdaniel | 842b086 | 2014-09-02 10:01:30 -0700 | [diff] [blame] | 203 | header->fCoverageInput = kAllOnes_ColorInput; |
egdaniel | 02cafcc | 2014-07-21 11:37:28 -0700 | [diff] [blame] | 204 | } else if (defaultToUniformInputs && !requiresCoverageAttrib && inputCoverageIsUsed) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 205 | header->fCoverageInput = kUniform_ColorInput; |
| 206 | } else { |
| 207 | header->fCoverageInput = kAttribute_ColorInput; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 208 | header->fRequiresVertexShader = true; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 209 | } |
| 210 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 211 | if (readsDst) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 212 | SkASSERT(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport()); |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 213 | const GrTexture* dstCopyTexture = NULL; |
| 214 | if (NULL != dstCopy) { |
| 215 | dstCopyTexture = dstCopy->texture(); |
| 216 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 217 | header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture, |
| 218 | gpu->glCaps()); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 219 | SkASSERT(0 != header->fDstReadKey); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 220 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 221 | header->fDstReadKey = 0; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | if (readFragPosition) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 225 | header->fFragPosKey = GrGLFragmentShaderBuilder::KeyForFragmentPosition( |
| 226 | drawState.getRenderTarget(), gpu->glCaps()); |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 227 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 228 | header->fFragPosKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 229 | } |
| 230 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 231 | // Record attribute indices |
| 232 | header->fPositionAttributeIndex = drawState.positionAttributeIndex(); |
| 233 | header->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex(); |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 234 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 235 | // For constant color and coverage we need an attribute with an index beyond those already set |
| 236 | int availableAttributeIndex = drawState.getVertexAttribCount(); |
| 237 | if (requiresColorAttrib) { |
| 238 | header->fColorAttributeIndex = drawState.colorVertexAttributeIndex(); |
| 239 | } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 240 | SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 241 | header->fColorAttributeIndex = availableAttributeIndex; |
| 242 | availableAttributeIndex++; |
| 243 | } else { |
| 244 | header->fColorAttributeIndex = -1; |
| 245 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 246 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 247 | if (requiresCoverageAttrib) { |
| 248 | header->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex(); |
| 249 | } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 250 | SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 251 | header->fCoverageAttributeIndex = availableAttributeIndex; |
| 252 | } else { |
| 253 | header->fCoverageAttributeIndex = -1; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 254 | } |
| 255 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 256 | // Here we deal with whether/how we handle color and coverage separately. |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 257 | |
commit-bot@chromium.org | 8a13588 | 2014-02-05 16:29:12 +0000 | [diff] [blame] | 258 | // Set this default and then possibly change our mind if there is coverage. |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 259 | header->fCoverageOutput = kModulate_CoverageOutput; |
| 260 | |
| 261 | // If we do have coverage determine whether it matters. |
| 262 | bool separateCoverageFromColor = false; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 263 | if (!drawState.isCoverageDrawing() && !skipCoverage && |
| 264 | (drawState.numCoverageStages() > 0 || requiresCoverageAttrib)) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 265 | |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 266 | if (gpu->caps()->dualSourceBlendingSupport() && |
| 267 | !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | |
| 268 | GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { |
| 269 | if (kZero_GrBlendCoeff == dstCoeff) { |
| 270 | // write the coverage value to second color |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 271 | header->fCoverageOutput = kSecondaryCoverage_CoverageOutput; |
| 272 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 273 | } else if (kSA_GrBlendCoeff == dstCoeff) { |
| 274 | // 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] | 275 | header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput; |
| 276 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 277 | } else if (kSC_GrBlendCoeff == dstCoeff) { |
| 278 | // 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] | 279 | header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput; |
| 280 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 281 | } |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 282 | } else if (readsDst && |
bsalomon@google.com | 0c89db2 | 2013-05-15 17:53:04 +0000 | [diff] [blame] | 283 | kOne_GrBlendCoeff == srcCoeff && |
| 284 | kZero_GrBlendCoeff == dstCoeff) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 285 | header->fCoverageOutput = kCombineWithDst_CoverageOutput; |
| 286 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 289 | if (!skipColor) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 290 | for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 291 | colorStages->push_back(&drawState.getColorStage(s)); |
| 292 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 293 | } |
| 294 | if (!skipCoverage) { |
| 295 | SkTArray<const GrEffectStage*, true>* array; |
| 296 | if (separateCoverageFromColor) { |
| 297 | array = coverageStages; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 298 | } else { |
| 299 | array = colorStages; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 300 | } |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 301 | for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 302 | array->push_back(&drawState.getCoverageStage(s)); |
| 303 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 304 | } |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 305 | header->fColorEffectCnt = colorStages->count(); |
| 306 | header->fCoverageEffectCnt = coverageStages->count(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 307 | |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 308 | desc->finalize(); |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | void GrGLProgramDesc::finalize() { |
| 313 | int keyLength = fKey.count(); |
| 314 | SkASSERT(0 == (keyLength % 4)); |
| 315 | *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength); |
| 316 | |
| 317 | uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>(); |
| 318 | *checksum = 0; |
| 319 | *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 320 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 321 | |
| 322 | GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 323 | size_t keyLength = other.keyLength(); |
| 324 | fKey.reset(keyLength); |
| 325 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 326 | return *this; |
| 327 | } |