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