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 | |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame^] | 17 | /** |
| 18 | * The key for an individual coord transform is made up of a matrix type and a bit that |
| 19 | * indicates the source of the input coords. |
| 20 | */ |
| 21 | enum { |
| 22 | kMatrixTypeKeyBits = 1, |
| 23 | kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1, |
| 24 | kPositionCoords_Flag = (1 << kMatrixTypeKeyBits), |
| 25 | kTransformKeyBits = kMatrixTypeKeyBits + 1, |
| 26 | }; |
| 27 | |
| 28 | /** |
| 29 | * We specialize the vertex code for each of these matrix types. |
| 30 | */ |
| 31 | enum MatrixType { |
| 32 | kNoPersp_MatrixType = 0, |
| 33 | kGeneral_MatrixType = 1, |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are |
| 38 | * present in the texture's config. swizzleComponentMask indicates the channels present in the |
| 39 | * shader swizzle. |
| 40 | */ |
| 41 | static bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, |
| 42 | uint32_t configComponentMask, |
| 43 | uint32_t swizzleComponentMask) { |
| 44 | if (caps.textureSwizzleSupport()) { |
| 45 | // Any remapping is handled using texture swizzling not shader modifications. |
| 46 | return false; |
| 47 | } |
| 48 | // check if the texture is alpha-only |
| 49 | if (kA_GrColorComponentFlag == configComponentMask) { |
| 50 | if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) { |
| 51 | // we must map the swizzle 'a's to 'r'. |
| 52 | return true; |
| 53 | } |
| 54 | if (kRGB_GrColorComponentFlags & swizzleComponentMask) { |
| 55 | // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that |
| 56 | // alpha-only textures smear alpha across all four channels when read. |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | static uint32_t gen_attrib_key(const GrEffect* effect) { |
| 64 | uint32_t key = 0; |
| 65 | |
| 66 | const GrEffect::VertexAttribArray& vars = effect->getVertexAttribs(); |
| 67 | int numAttributes = vars.count(); |
| 68 | SkASSERT(numAttributes <= 2); |
| 69 | for (int a = 0; a < numAttributes; ++a) { |
| 70 | uint32_t value = 1 << a; |
| 71 | key |= value; |
| 72 | } |
| 73 | return key; |
| 74 | } |
| 75 | |
| 76 | static uint32_t gen_transform_key(const GrEffectStage& effectStage, |
| 77 | bool useExplicitLocalCoords) { |
| 78 | uint32_t totalKey = 0; |
| 79 | int numTransforms = effectStage.getEffect()->numTransforms(); |
| 80 | for (int t = 0; t < numTransforms; ++t) { |
| 81 | uint32_t key = 0; |
| 82 | if (effectStage.isPerspectiveCoordTransform(t, useExplicitLocalCoords)) { |
| 83 | key |= kGeneral_MatrixType; |
| 84 | } else { |
| 85 | key |= kNoPersp_MatrixType; |
| 86 | } |
| 87 | |
| 88 | const GrCoordTransform& coordTransform = effectStage.getEffect()->coordTransform(t); |
| 89 | if (kLocal_GrCoordSet != coordTransform.sourceCoords() && useExplicitLocalCoords) { |
| 90 | key |= kPositionCoords_Flag; |
| 91 | } |
| 92 | key <<= kTransformKeyBits * t; |
| 93 | SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap |
| 94 | totalKey |= key; |
| 95 | } |
| 96 | return totalKey; |
| 97 | } |
| 98 | |
| 99 | static uint32_t gen_texture_key(const GrEffect* effect, const GrGLCaps& caps) { |
| 100 | uint32_t key = 0; |
| 101 | int numTextures = effect->numTextures(); |
| 102 | for (int t = 0; t < numTextures; ++t) { |
| 103 | const GrTextureAccess& access = effect->textureAccess(t); |
| 104 | uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config()); |
| 105 | if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) { |
| 106 | key |= 1 << t; |
| 107 | } |
| 108 | } |
| 109 | return key; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * A function which emits a meta key into the key builder. This is required because shader code may |
| 114 | * be dependent on properties of the effect that the effect itself doesn't use |
| 115 | * in its key (e.g. the pixel format of textures used). So we create a meta-key for |
| 116 | * every effect using this function. It is also responsible for inserting the effect's class ID |
| 117 | * which must be different for every GrEffect subclass. It can fail if an effect uses too many |
| 118 | * textures, attributes, etc for the space allotted in the meta-key. |
| 119 | */ |
| 120 | |
| 121 | static bool gen_effect_meta_key(const GrEffectStage& effectStage, |
| 122 | bool useExplicitLocalCoords, |
| 123 | const GrGLCaps& caps, |
| 124 | GrEffectKeyBuilder* b) { |
| 125 | |
| 126 | uint32_t textureKey = gen_texture_key(effectStage.getEffect(), caps); |
| 127 | uint32_t transformKey = gen_transform_key(effectStage,useExplicitLocalCoords); |
| 128 | uint32_t attribKey = gen_attrib_key(effectStage.getEffect()); |
| 129 | uint32_t classID = effectStage.getEffect()->getFactory().effectClassID(); |
| 130 | |
| 131 | // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they |
| 132 | // don't fit. |
| 133 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
| 134 | if ((textureKey | transformKey | attribKey | classID) & kMetaKeyInvalidMask) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | uint32_t* key = b->add32n(2); |
| 139 | key[0] = (textureKey << 16 | transformKey); |
| 140 | key[1] = (classID << 16 | attribKey); |
| 141 | return true; |
| 142 | } |
| 143 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 144 | bool GrGLProgramDesc::GetEffectKey(const GrEffectStage& stage, const GrGLCaps& caps, |
| 145 | bool useExplicitLocalCoords, GrEffectKeyBuilder* b, |
| 146 | uint16_t* effectKeySize) { |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 147 | const GrBackendEffectFactory& factory = stage.getEffect()->getFactory(); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 148 | const GrEffect& effect = *stage.getEffect(); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 149 | factory.getGLEffectKey(effect, caps, b); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 150 | size_t size = b->size(); |
| 151 | if (size > SK_MaxU16) { |
| 152 | *effectKeySize = 0; // suppresses a warning. |
| 153 | return false; |
| 154 | } |
| 155 | *effectKeySize = SkToU16(size); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame^] | 156 | if (!gen_effect_meta_key(stage, useExplicitLocalCoords, caps, b)) { |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | return true; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 160 | } |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 161 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 162 | bool GrGLProgramDesc::Build(const GrOptDrawState& optState, |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 163 | GrGpu::DrawType drawType, |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 164 | GrBlendCoeff srcCoeff, |
| 165 | GrBlendCoeff dstCoeff, |
| 166 | const GrGpuGL* gpu, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 167 | const GrDeviceCoordTexture* dstCopy, |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 168 | const GrEffectStage** geometryProcessor, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 169 | SkTArray<const GrEffectStage*, true>* colorStages, |
| 170 | SkTArray<const GrEffectStage*, true>* coverageStages, |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 171 | GrGLProgramDesc* desc) { |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 172 | colorStages->reset(); |
| 173 | coverageStages->reset(); |
| 174 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 175 | bool inputColorIsUsed = optState.inputColorIsUsed(); |
| 176 | bool inputCoverageIsUsed = optState.inputColorIsUsed(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 177 | |
| 178 | // The descriptor is used as a cache key. Thus when a field of the |
| 179 | // descriptor will not affect program generation (because of the attribute |
| 180 | // bindings in use or other descriptor field settings) it should be set |
| 181 | // to a canonical value to avoid duplicate programs with different keys. |
| 182 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 183 | bool requiresColorAttrib = optState.hasColorVertexAttribute(); |
| 184 | bool requiresCoverageAttrib = optState.hasCoverageVertexAttribute(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 185 | bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib(); |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 186 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 187 | int numStages = optState.numTotalStages(); |
| 188 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 189 | GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t)); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 190 | // Make room for everything up to and including the array of offsets to effect keys. |
| 191 | desc->fKey.reset(); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 192 | 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] | 193 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 194 | int offsetAndSizeIndex = 0; |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 195 | bool effectKeySuccess = true; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 196 | |
| 197 | KeyHeader* header = desc->header(); |
| 198 | // make sure any padding in the header is zeroed. |
| 199 | memset(desc->header(), 0, kHeaderSize); |
| 200 | |
| 201 | // We can only have one effect which touches the vertex shader |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 202 | if (optState.hasGeometryProcessor()) { |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 203 | uint16_t* offsetAndSize = |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 204 | reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset + |
| 205 | offsetAndSizeIndex * 2 * sizeof(uint16_t)); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 206 | |
| 207 | GrEffectKeyBuilder b(&desc->fKey); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 208 | uint16_t effectKeySize; |
| 209 | uint32_t effectOffset = desc->fKey.count(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 210 | effectKeySuccess |= GetEffectKey(*optState.getGeometryProcessor(), gpu->glCaps(), |
| 211 | requiresLocalCoordAttrib, &b, &effectKeySize); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 212 | effectKeySuccess |= (effectOffset <= SK_MaxU16); |
| 213 | |
| 214 | offsetAndSize[0] = SkToU16(effectOffset); |
| 215 | offsetAndSize[1] = effectKeySize; |
| 216 | ++offsetAndSizeIndex; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 217 | *geometryProcessor = optState.getGeometryProcessor(); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 218 | header->fHasGeometryProcessor = true; |
| 219 | } |
| 220 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 221 | for (int s = 0; s < optState.numColorStages(); ++s) { |
| 222 | uint16_t* offsetAndSize = |
| 223 | reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset + |
| 224 | offsetAndSizeIndex * 2 * sizeof(uint16_t)); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 225 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 226 | GrEffectKeyBuilder b(&desc->fKey); |
| 227 | uint16_t effectKeySize; |
| 228 | uint32_t effectOffset = desc->fKey.count(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 229 | effectKeySuccess |= GetEffectKey(optState.getColorStage(s), gpu->glCaps(), |
| 230 | requiresLocalCoordAttrib, &b, &effectKeySize); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 231 | effectKeySuccess |= (effectOffset <= SK_MaxU16); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 232 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 233 | offsetAndSize[0] = SkToU16(effectOffset); |
| 234 | offsetAndSize[1] = effectKeySize; |
| 235 | ++offsetAndSizeIndex; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 236 | } |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 237 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 238 | for (int s = 0; s < optState.numCoverageStages(); ++s) { |
| 239 | uint16_t* offsetAndSize = |
| 240 | reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset + |
| 241 | offsetAndSizeIndex * 2 * sizeof(uint16_t)); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 242 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 243 | GrEffectKeyBuilder b(&desc->fKey); |
| 244 | uint16_t effectKeySize; |
| 245 | uint32_t effectOffset = desc->fKey.count(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 246 | effectKeySuccess |= GetEffectKey(optState.getCoverageStage(s), gpu->glCaps(), |
| 247 | requiresLocalCoordAttrib, &b, &effectKeySize); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 248 | effectKeySuccess |= (effectOffset <= SK_MaxU16); |
| 249 | |
| 250 | offsetAndSize[0] = SkToU16(effectOffset); |
| 251 | offsetAndSize[1] = effectKeySize; |
| 252 | ++offsetAndSizeIndex; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 253 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 254 | |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 255 | if (!effectKeySuccess) { |
| 256 | desc->fKey.reset(); |
| 257 | return false; |
| 258 | } |
| 259 | |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 260 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 261 | // below here. |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 262 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 263 | // We will only require a vertex shader if we have more than just the position VA attrib. |
| 264 | // If we have a geom processor we must us a vertex shader and we should not have a geometry |
| 265 | // processor if we are doing path rendering. |
| 266 | SkASSERT(!GrGpu::IsPathRenderingDrawType(drawType) || !optState.requiresVertexShader()); |
| 267 | header->fRequiresVertexShader = optState.requiresVertexShader() || |
| 268 | !GrGpu::IsPathRenderingDrawType(drawType); |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 269 | header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 270 | |
| 271 | // Currently the experimental GS will only work with triangle prims (and it doesn't do anything |
| 272 | // other than pass through values from the VS to the FS anyway). |
| 273 | #if GR_GL_EXPERIMENTAL_GS |
| 274 | #if 0 |
| 275 | header->fExperimentalGS = gpu->caps().geometryShaderSupport(); |
| 276 | #else |
| 277 | header->fExperimentalGS = false; |
| 278 | #endif |
| 279 | #endif |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 280 | bool defaultToUniformInputs = GR_GL_NO_CONSTANT_ATTRIBUTES || gpu->caps()->pathRenderingSupport(); |
| 281 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 282 | if (!inputColorIsUsed) { |
egdaniel | 842b086 | 2014-09-02 10:01:30 -0700 | [diff] [blame] | 283 | header->fColorInput = kAllOnes_ColorInput; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 284 | } else if (defaultToUniformInputs && !requiresColorAttrib) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 285 | header->fColorInput = kUniform_ColorInput; |
| 286 | } else { |
| 287 | header->fColorInput = kAttribute_ColorInput; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 288 | header->fRequiresVertexShader = true; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 289 | } |
| 290 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 291 | bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == optState.getCoverageColor(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 292 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 293 | if (covIsSolidWhite || !inputCoverageIsUsed) { |
egdaniel | 842b086 | 2014-09-02 10:01:30 -0700 | [diff] [blame] | 294 | header->fCoverageInput = kAllOnes_ColorInput; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 295 | } else if (defaultToUniformInputs && !requiresCoverageAttrib) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 296 | header->fCoverageInput = kUniform_ColorInput; |
| 297 | } else { |
| 298 | header->fCoverageInput = kAttribute_ColorInput; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 299 | header->fRequiresVertexShader = true; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 300 | } |
| 301 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 302 | if (optState.readsDst()) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 303 | SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport()); |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 304 | const GrTexture* dstCopyTexture = NULL; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 305 | if (dstCopy) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 306 | dstCopyTexture = dstCopy->texture(); |
| 307 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 308 | header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture, |
| 309 | gpu->glCaps()); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 310 | SkASSERT(0 != header->fDstReadKey); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 311 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 312 | header->fDstReadKey = 0; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 313 | } |
| 314 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 315 | if (optState.readsFragPosition()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 316 | header->fFragPosKey = GrGLFragmentShaderBuilder::KeyForFragmentPosition( |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 317 | optState.getRenderTarget(), gpu->glCaps()); |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 318 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 319 | header->fFragPosKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 320 | } |
| 321 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 322 | // Record attribute indices |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 323 | header->fPositionAttributeIndex = optState.positionAttributeIndex(); |
| 324 | header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex(); |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 325 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 326 | // 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] | 327 | int availableAttributeIndex = optState.getVertexAttribCount(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 328 | if (requiresColorAttrib) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 329 | header->fColorAttributeIndex = optState.colorVertexAttributeIndex(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 330 | } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 331 | SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 332 | header->fColorAttributeIndex = availableAttributeIndex; |
| 333 | availableAttributeIndex++; |
| 334 | } else { |
| 335 | header->fColorAttributeIndex = -1; |
| 336 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 337 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 338 | if (requiresCoverageAttrib) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 339 | header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 340 | } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 341 | SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 342 | header->fCoverageAttributeIndex = availableAttributeIndex; |
| 343 | } else { |
| 344 | header->fCoverageAttributeIndex = -1; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 345 | } |
| 346 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 347 | // 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] | 348 | |
commit-bot@chromium.org | 8a13588 | 2014-02-05 16:29:12 +0000 | [diff] [blame] | 349 | // 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] | 350 | header->fCoverageOutput = kModulate_CoverageOutput; |
| 351 | |
| 352 | // If we do have coverage determine whether it matters. |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 353 | bool separateCoverageFromColor = optState.hasGeometryProcessor(); |
| 354 | if (!optState.isCoverageDrawing() && |
| 355 | (optState.numCoverageStages() > 0 || |
| 356 | optState.hasGeometryProcessor() || |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 357 | requiresCoverageAttrib)) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 358 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 359 | if (gpu->caps()->dualSourceBlendingSupport()) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 360 | if (kZero_GrBlendCoeff == dstCoeff) { |
| 361 | // write the coverage value to second color |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 362 | header->fCoverageOutput = kSecondaryCoverage_CoverageOutput; |
| 363 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 364 | } else if (kSA_GrBlendCoeff == dstCoeff) { |
| 365 | // 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] | 366 | header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput; |
| 367 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 368 | } else if (kSC_GrBlendCoeff == dstCoeff) { |
| 369 | // 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] | 370 | header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput; |
| 371 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 372 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 373 | } else if (optState.readsDst() && |
bsalomon@google.com | 0c89db2 | 2013-05-15 17:53:04 +0000 | [diff] [blame] | 374 | kOne_GrBlendCoeff == srcCoeff && |
| 375 | kZero_GrBlendCoeff == dstCoeff) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 376 | header->fCoverageOutput = kCombineWithDst_CoverageOutput; |
| 377 | separateCoverageFromColor = true; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 380 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 381 | for (int s = 0; s < optState.numColorStages(); ++s) { |
| 382 | colorStages->push_back(&optState.getColorStage(s)); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 383 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 384 | SkTArray<const GrEffectStage*, true>* array; |
| 385 | if (separateCoverageFromColor) { |
| 386 | array = coverageStages; |
| 387 | } else { |
| 388 | array = colorStages; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 389 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 390 | for (int s = 0; s < optState.numCoverageStages(); ++s) { |
| 391 | array->push_back(&optState.getCoverageStage(s)); |
| 392 | } |
| 393 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 394 | header->fColorEffectCnt = colorStages->count(); |
| 395 | header->fCoverageEffectCnt = coverageStages->count(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 396 | |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 397 | desc->finalize(); |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | void GrGLProgramDesc::finalize() { |
| 402 | int keyLength = fKey.count(); |
| 403 | SkASSERT(0 == (keyLength % 4)); |
| 404 | *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength); |
| 405 | |
| 406 | uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>(); |
| 407 | *checksum = 0; |
| 408 | *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 409 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 410 | |
| 411 | GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 412 | size_t keyLength = other.keyLength(); |
| 413 | fKey.reset(keyLength); |
| 414 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 415 | return *this; |
| 416 | } |