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