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