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