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 | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 8 | #include "gl/builders/GrGLFragmentShaderBuilder.h" |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 9 | #include "GrGLProgramDesc.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 10 | #include "GrBackendProcessorFactory.h" |
| 11 | #include "GrProcessor.h" |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 63 | static uint32_t gen_attrib_key(const GrGeometryProcessor* effect) { |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 64 | uint32_t key = 0; |
| 65 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 66 | const GrGeometryProcessor::VertexAttribArray& vars = effect->getVertexAttribs(); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 76 | static uint32_t gen_transform_key(const GrProcessorStage& effectStage, |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 77 | bool useExplicitLocalCoords) { |
| 78 | uint32_t totalKey = 0; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 79 | int numTransforms = effectStage.getProcessor()->numTransforms(); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 88 | const GrCoordTransform& coordTransform = effectStage.getProcessor()->coordTransform(t); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 99 | static uint32_t gen_texture_key(const GrProcessor* effect, const GrGLCaps& caps) { |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 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 |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 117 | * which must be different for every GrProcessor subclass. It can fail if an effect uses too many |
| 118 | * textures, transforms, etc, for the space allotted in the meta-key. |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 119 | */ |
| 120 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 121 | static uint32_t* get_processor_meta_key(const GrProcessorStage& processorStage, |
| 122 | bool useExplicitLocalCoords, |
| 123 | const GrGLCaps& caps, |
| 124 | GrProcessorKeyBuilder* b) { |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 125 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 126 | uint32_t textureKey = gen_texture_key(processorStage.getProcessor(), caps); |
| 127 | uint32_t transformKey = gen_transform_key(processorStage,useExplicitLocalCoords); |
| 128 | uint32_t classID = processorStage.getProcessor()->getFactory().effectClassID(); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 129 | |
| 130 | // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they |
| 131 | // don't fit. |
| 132 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 133 | if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) { |
| 134 | return NULL; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | uint32_t* key = b->add32n(2); |
| 138 | key[0] = (textureKey << 16 | transformKey); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 139 | key[1] = (classID << 16); |
| 140 | return key; |
| 141 | } |
| 142 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 143 | static bool get_fp_key(const GrProcessorStage& stage, |
| 144 | const GrGLCaps& caps, |
| 145 | bool useExplicitLocalCoords, |
| 146 | GrProcessorKeyBuilder* b, |
| 147 | uint16_t* processorKeySize) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 148 | const GrProcessor& effect = *stage.getProcessor(); |
| 149 | const GrBackendProcessorFactory& factory = effect.getFactory(); |
| 150 | factory.getGLProcessorKey(effect, caps, b); |
| 151 | size_t size = b->size(); |
| 152 | if (size > SK_MaxU16) { |
| 153 | *processorKeySize = 0; // suppresses a warning. |
| 154 | return false; |
| 155 | } |
| 156 | *processorKeySize = SkToU16(size); |
| 157 | if (NULL == get_processor_meta_key(stage, useExplicitLocalCoords, caps, b)) { |
| 158 | return false; |
| 159 | } |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 163 | static bool get_gp_key(const GrGeometryStage& stage, |
| 164 | const GrGLCaps& caps, |
| 165 | bool useExplicitLocalCoords, |
| 166 | GrProcessorKeyBuilder* b, |
| 167 | uint16_t* processorKeySize) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 168 | const GrProcessor& effect = *stage.getProcessor(); |
| 169 | const GrBackendProcessorFactory& factory = effect.getFactory(); |
| 170 | factory.getGLProcessorKey(effect, caps, b); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 171 | size_t size = b->size(); |
| 172 | if (size > SK_MaxU16) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 173 | *processorKeySize = 0; // suppresses a warning. |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 174 | return false; |
| 175 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 176 | *processorKeySize = SkToU16(size); |
| 177 | uint32_t* key = get_processor_meta_key(stage, useExplicitLocalCoords, caps, b); |
| 178 | if (NULL == key) { |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 179 | return false; |
| 180 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 181 | uint32_t attribKey = gen_attrib_key(stage.getProcessor()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 182 | |
| 183 | // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they |
| 184 | // don't fit. |
| 185 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
| 186 | if ((attribKey) & kMetaKeyInvalidMask) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | key[1] |= attribKey; |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 191 | return true; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 192 | } |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 193 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 194 | struct GeometryProcessorKeyBuilder { |
| 195 | typedef GrGeometryStage StagedProcessor; |
| 196 | static bool GetProcessorKey(const GrGeometryStage& gpStage, |
| 197 | const GrGLCaps& caps, |
| 198 | bool requiresLocalCoordAttrib, |
| 199 | GrProcessorKeyBuilder* b, |
| 200 | uint16_t* processorKeySize) { |
| 201 | return get_gp_key(gpStage, caps, requiresLocalCoordAttrib, b, processorKeySize); |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | struct FragmentProcessorKeyBuilder { |
| 206 | typedef GrFragmentStage StagedProcessor; |
| 207 | static bool GetProcessorKey(const GrFragmentStage& fpStage, |
| 208 | const GrGLCaps& caps, |
| 209 | bool requiresLocalCoordAttrib, |
| 210 | GrProcessorKeyBuilder* b, |
| 211 | uint16_t* processorKeySize) { |
| 212 | return get_fp_key(fpStage, caps, requiresLocalCoordAttrib, b, processorKeySize); |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | |
| 217 | template <class ProcessorKeyBuilder> |
| 218 | bool |
| 219 | GrGLProgramDesc::BuildStagedProcessorKey(const typename ProcessorKeyBuilder::StagedProcessor& stage, |
| 220 | const GrGLCaps& caps, |
| 221 | bool requiresLocalCoordAttrib, |
| 222 | GrGLProgramDesc* desc, |
| 223 | int* offsetAndSizeIndex) { |
| 224 | GrProcessorKeyBuilder b(&desc->fKey); |
| 225 | uint16_t processorKeySize; |
| 226 | uint32_t processorOffset = desc->fKey.count(); |
| 227 | if (processorOffset > SK_MaxU16 || |
| 228 | !ProcessorKeyBuilder::GetProcessorKey(stage, caps, requiresLocalCoordAttrib, &b, |
| 229 | &processorKeySize)){ |
| 230 | desc->fKey.reset(); |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | uint16_t* offsetAndSize = |
| 235 | reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset + |
| 236 | *offsetAndSizeIndex * 2 * sizeof(uint16_t)); |
| 237 | offsetAndSize[0] = SkToU16(processorOffset); |
| 238 | offsetAndSize[1] = processorKeySize; |
| 239 | ++(*offsetAndSizeIndex); |
| 240 | return true; |
| 241 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 242 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 243 | bool GrGLProgramDesc::Build(const GrOptDrawState& optState, |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 244 | GrGpu::DrawType drawType, |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 245 | GrBlendCoeff srcCoeff, |
| 246 | GrBlendCoeff dstCoeff, |
egdaniel | ae44496 | 2014-09-22 12:29:52 -0700 | [diff] [blame] | 247 | GrGpuGL* gpu, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 248 | const GrDeviceCoordTexture* dstCopy, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 249 | const GrGeometryStage** geometryProcessor, |
| 250 | SkTArray<const GrFragmentStage*, true>* colorStages, |
| 251 | SkTArray<const GrFragmentStage*, true>* coverageStages, |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 252 | GrGLProgramDesc* desc) { |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 253 | colorStages->reset(); |
| 254 | coverageStages->reset(); |
| 255 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 256 | bool inputColorIsUsed = optState.inputColorIsUsed(); |
egdaniel | 033ea7f | 2014-09-23 08:14:13 -0700 | [diff] [blame] | 257 | bool inputCoverageIsUsed = optState.inputCoverageIsUsed(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 258 | |
| 259 | // The descriptor is used as a cache key. Thus when a field of the |
| 260 | // descriptor will not affect program generation (because of the attribute |
| 261 | // bindings in use or other descriptor field settings) it should be set |
| 262 | // to a canonical value to avoid duplicate programs with different keys. |
| 263 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 264 | bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib(); |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 265 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 266 | int numStages = optState.numTotalStages(); |
| 267 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 268 | GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t)); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 269 | // Make room for everything up to and including the array of offsets to effect keys. |
| 270 | desc->fKey.reset(); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 271 | 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] | 272 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 273 | int offsetAndSizeIndex = 0; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 274 | |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 275 | // We can only have one effect which touches the vertex shader |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 276 | if (optState.hasGeometryProcessor()) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 277 | const GrGeometryStage& gpStage = *optState.getGeometryProcessor(); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 278 | if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(gpStage, |
| 279 | gpu->glCaps(), |
| 280 | requiresLocalCoordAttrib, |
| 281 | desc, |
| 282 | &offsetAndSizeIndex)) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 283 | return false; |
| 284 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 285 | *geometryProcessor = &gpStage; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 286 | } |
| 287 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 288 | for (int s = 0; s < optState.numColorStages(); ++s) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 289 | if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getColorStage(s), |
| 290 | gpu->glCaps(), |
| 291 | requiresLocalCoordAttrib, |
| 292 | desc, |
| 293 | &offsetAndSizeIndex)) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 294 | return false; |
| 295 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 296 | } |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 297 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 298 | for (int s = 0; s < optState.numCoverageStages(); ++s) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 299 | if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getCoverageStage(s), |
| 300 | gpu->glCaps(), |
| 301 | requiresLocalCoordAttrib, |
| 302 | desc, |
| 303 | &offsetAndSizeIndex)) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 304 | return false; |
| 305 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 306 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 307 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 308 | // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------------------------- |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 309 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 310 | // below here. |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 311 | KeyHeader* header = desc->header(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 312 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame^] | 313 | // make sure any padding in the header is zeroed. |
| 314 | memset(header, 0, kHeaderSize); |
| 315 | |
| 316 | header->fHasGeometryProcessor = optState.hasGeometryProcessor(); |
egdaniel | ae44496 | 2014-09-22 12:29:52 -0700 | [diff] [blame] | 317 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 318 | header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 319 | |
bsalomon | cd523eb | 2014-09-23 08:19:00 -0700 | [diff] [blame] | 320 | if (gpu->caps()->pathRenderingSupport() && |
| 321 | GrGpu::IsPathRenderingDrawType(drawType) && |
| 322 | gpu->glPathRendering()->texturingMode() == GrGLPathRendering::FixedFunction_TexturingMode) { |
| 323 | header->fUseFragShaderOnly = true; |
| 324 | SkASSERT(!optState.hasGeometryProcessor()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 325 | } else { |
egdaniel | ae44496 | 2014-09-22 12:29:52 -0700 | [diff] [blame] | 326 | header->fUseFragShaderOnly = false; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 327 | } |
| 328 | |
bsalomon | cd523eb | 2014-09-23 08:19:00 -0700 | [diff] [blame] | 329 | bool defaultToUniformInputs = GrGpu::IsPathRenderingDrawType(drawType) || |
| 330 | GR_GL_NO_CONSTANT_ATTRIBUTES; |
| 331 | |
| 332 | if (!inputColorIsUsed) { |
| 333 | header->fColorInput = kAllOnes_ColorInput; |
| 334 | } else if (defaultToUniformInputs && !optState.hasColorVertexAttribute()) { |
| 335 | header->fColorInput = kUniform_ColorInput; |
| 336 | } else { |
| 337 | header->fColorInput = kAttribute_ColorInput; |
| 338 | SkASSERT(!header->fUseFragShaderOnly); |
| 339 | } |
| 340 | |
| 341 | bool covIsSolidWhite = !optState.hasCoverageVertexAttribute() && |
| 342 | 0xffffffff == optState.getCoverageColor(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 343 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 344 | if (covIsSolidWhite || !inputCoverageIsUsed) { |
egdaniel | 842b086 | 2014-09-02 10:01:30 -0700 | [diff] [blame] | 345 | header->fCoverageInput = kAllOnes_ColorInput; |
bsalomon | cd523eb | 2014-09-23 08:19:00 -0700 | [diff] [blame] | 346 | } else if (defaultToUniformInputs && !optState.hasCoverageVertexAttribute()) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 347 | header->fCoverageInput = kUniform_ColorInput; |
| 348 | } else { |
| 349 | header->fCoverageInput = kAttribute_ColorInput; |
bsalomon | cd523eb | 2014-09-23 08:19:00 -0700 | [diff] [blame] | 350 | SkASSERT(!header->fUseFragShaderOnly); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 351 | } |
| 352 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 353 | if (optState.readsDst()) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 354 | SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport()); |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 355 | const GrTexture* dstCopyTexture = NULL; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 356 | if (dstCopy) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 357 | dstCopyTexture = dstCopy->texture(); |
| 358 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 359 | header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 360 | gpu->glCaps()); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 361 | SkASSERT(0 != header->fDstReadKey); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 362 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 363 | header->fDstReadKey = 0; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 364 | } |
| 365 | |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 366 | if (optState.readsFragPosition()) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 367 | header->fFragPosKey = |
| 368 | GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRenderTarget(), |
| 369 | gpu->glCaps()); |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 370 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 371 | header->fFragPosKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 372 | } |
| 373 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 374 | // Record attribute indices |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 375 | header->fPositionAttributeIndex = optState.positionAttributeIndex(); |
| 376 | header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex(); |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 377 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 378 | // 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] | 379 | int availableAttributeIndex = optState.getVertexAttribCount(); |
bsalomon | cd523eb | 2014-09-23 08:19:00 -0700 | [diff] [blame] | 380 | if (optState.hasColorVertexAttribute()) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 381 | header->fColorAttributeIndex = optState.colorVertexAttributeIndex(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 382 | } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 383 | SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 384 | header->fColorAttributeIndex = availableAttributeIndex; |
| 385 | availableAttributeIndex++; |
| 386 | } else { |
| 387 | header->fColorAttributeIndex = -1; |
| 388 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 389 | |
bsalomon | cd523eb | 2014-09-23 08:19:00 -0700 | [diff] [blame] | 390 | if (optState.hasCoverageVertexAttribute()) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 391 | header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 392 | } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 393 | SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 394 | header->fCoverageAttributeIndex = availableAttributeIndex; |
| 395 | } else { |
| 396 | header->fCoverageAttributeIndex = -1; |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 397 | } |
| 398 | |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 399 | header->fPrimaryOutputType = optState.getPrimaryOutputType(); |
| 400 | header->fSecondaryOutputType = optState.getSecondaryOutputType(); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 401 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 402 | for (int s = 0; s < optState.numColorStages(); ++s) { |
| 403 | colorStages->push_back(&optState.getColorStage(s)); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 404 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 405 | for (int s = 0; s < optState.numCoverageStages(); ++s) { |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 406 | coverageStages->push_back(&optState.getCoverageStage(s)); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 407 | } |
| 408 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 409 | header->fColorEffectCnt = colorStages->count(); |
| 410 | header->fCoverageEffectCnt = coverageStages->count(); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 411 | desc->finalize(); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | void GrGLProgramDesc::finalize() { |
| 416 | int keyLength = fKey.count(); |
| 417 | SkASSERT(0 == (keyLength % 4)); |
| 418 | *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength); |
| 419 | |
| 420 | uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>(); |
| 421 | *checksum = 0; |
| 422 | *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 423 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 424 | |
| 425 | GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 426 | size_t keyLength = other.keyLength(); |
| 427 | fKey.reset(keyLength); |
| 428 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 429 | return *this; |
| 430 | } |