blob: c0a9e13ed19ca4eddf30e6cd283886dc82da5ab0 [file] [log] [blame]
bsalomon@google.com798c8c42013-03-27 19:50:27 +00001/*
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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +00009#include "GrGLProgramDesc.h"
10#include "GrBackendEffectFactory.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +000011#include "GrEffect.h"
12#include "GrGpuGL.h"
egdaniel170f90b2014-09-16 12:54:40 -070013#include "GrOptDrawState.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +000014
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000015#include "SkChecksum.h"
16
joshualitt23e280d2014-09-18 12:26:38 -070017/**
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 */
21enum {
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 */
31enum 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 */
41static 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
63static 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
76static 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
99static 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
121static 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
egdaniela7dc0a82014-09-17 08:25:05 -0700144bool GrGLProgramDesc::GetEffectKey(const GrEffectStage& stage, const GrGLCaps& caps,
145 bool useExplicitLocalCoords, GrEffectKeyBuilder* b,
146 uint16_t* effectKeySize) {
bsalomon848faf02014-07-11 10:01:02 -0700147 const GrBackendEffectFactory& factory = stage.getEffect()->getFactory();
joshualitt49586be2014-09-16 08:21:41 -0700148 const GrEffect& effect = *stage.getEffect();
joshualitt49586be2014-09-16 08:21:41 -0700149 factory.getGLEffectKey(effect, caps, b);
bsalomon929f29a2014-07-17 07:55:11 -0700150 size_t size = b->size();
151 if (size > SK_MaxU16) {
152 *effectKeySize = 0; // suppresses a warning.
153 return false;
154 }
155 *effectKeySize = SkToU16(size);
joshualitt23e280d2014-09-18 12:26:38 -0700156 if (!gen_effect_meta_key(stage, useExplicitLocalCoords, caps, b)) {
bsalomon929f29a2014-07-17 07:55:11 -0700157 return false;
158 }
159 return true;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000160}
bsalomon848faf02014-07-11 10:01:02 -0700161
egdaniel170f90b2014-09-16 12:54:40 -0700162bool GrGLProgramDesc::Build(const GrOptDrawState& optState,
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000163 GrGpu::DrawType drawType,
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000164 GrBlendCoeff srcCoeff,
165 GrBlendCoeff dstCoeff,
egdanielae444962014-09-22 12:29:52 -0700166 GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000167 const GrDeviceCoordTexture* dstCopy,
joshualittbd769d02014-09-04 08:56:46 -0700168 const GrEffectStage** geometryProcessor,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000169 SkTArray<const GrEffectStage*, true>* colorStages,
170 SkTArray<const GrEffectStage*, true>* coverageStages,
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000171 GrGLProgramDesc* desc) {
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000172 colorStages->reset();
173 coverageStages->reset();
174
egdaniel170f90b2014-09-16 12:54:40 -0700175 bool inputColorIsUsed = optState.inputColorIsUsed();
176 bool inputCoverageIsUsed = optState.inputColorIsUsed();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000177
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
egdaniel170f90b2014-09-16 12:54:40 -0700183 bool requiresColorAttrib = optState.hasColorVertexAttribute();
184 bool requiresCoverageAttrib = optState.hasCoverageVertexAttribute();
egdaniela7dc0a82014-09-17 08:25:05 -0700185 bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib();
kkinnunenec56e452014-08-25 22:21:16 -0700186
egdaniel170f90b2014-09-16 12:54:40 -0700187 int numStages = optState.numTotalStages();
188
bsalomon929f29a2014-07-17 07:55:11 -0700189 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t));
bsalomon848faf02014-07-11 10:01:02 -0700190 // Make room for everything up to and including the array of offsets to effect keys.
191 desc->fKey.reset();
bsalomon929f29a2014-07-17 07:55:11 -0700192 desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * numStages);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000193
bsalomon929f29a2014-07-17 07:55:11 -0700194 int offsetAndSizeIndex = 0;
bsalomon848faf02014-07-11 10:01:02 -0700195 bool effectKeySuccess = true;
joshualittbd769d02014-09-04 08:56:46 -0700196
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
egdaniel170f90b2014-09-16 12:54:40 -0700202 if (optState.hasGeometryProcessor()) {
joshualittbd769d02014-09-04 08:56:46 -0700203 uint16_t* offsetAndSize =
bsalomon929f29a2014-07-17 07:55:11 -0700204 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset +
205 offsetAndSizeIndex * 2 * sizeof(uint16_t));
bsalomon848faf02014-07-11 10:01:02 -0700206
207 GrEffectKeyBuilder b(&desc->fKey);
bsalomon929f29a2014-07-17 07:55:11 -0700208 uint16_t effectKeySize;
209 uint32_t effectOffset = desc->fKey.count();
egdaniela7dc0a82014-09-17 08:25:05 -0700210 effectKeySuccess |= GetEffectKey(*optState.getGeometryProcessor(), gpu->glCaps(),
211 requiresLocalCoordAttrib, &b, &effectKeySize);
bsalomon929f29a2014-07-17 07:55:11 -0700212 effectKeySuccess |= (effectOffset <= SK_MaxU16);
213
214 offsetAndSize[0] = SkToU16(effectOffset);
215 offsetAndSize[1] = effectKeySize;
216 ++offsetAndSizeIndex;
egdaniel170f90b2014-09-16 12:54:40 -0700217 *geometryProcessor = optState.getGeometryProcessor();
joshualittbd769d02014-09-04 08:56:46 -0700218 header->fHasGeometryProcessor = true;
219 }
220
egdaniel170f90b2014-09-16 12:54:40 -0700221 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));
joshualittbd769d02014-09-04 08:56:46 -0700225
egdaniel170f90b2014-09-16 12:54:40 -0700226 GrEffectKeyBuilder b(&desc->fKey);
227 uint16_t effectKeySize;
228 uint32_t effectOffset = desc->fKey.count();
egdaniela7dc0a82014-09-17 08:25:05 -0700229 effectKeySuccess |= GetEffectKey(optState.getColorStage(s), gpu->glCaps(),
230 requiresLocalCoordAttrib, &b, &effectKeySize);
egdaniel170f90b2014-09-16 12:54:40 -0700231 effectKeySuccess |= (effectOffset <= SK_MaxU16);
joshualittbd769d02014-09-04 08:56:46 -0700232
egdaniel170f90b2014-09-16 12:54:40 -0700233 offsetAndSize[0] = SkToU16(effectOffset);
234 offsetAndSize[1] = effectKeySize;
235 ++offsetAndSizeIndex;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000236 }
bsalomon929f29a2014-07-17 07:55:11 -0700237
egdaniel170f90b2014-09-16 12:54:40 -0700238 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));
bsalomon929f29a2014-07-17 07:55:11 -0700242
egdaniel170f90b2014-09-16 12:54:40 -0700243 GrEffectKeyBuilder b(&desc->fKey);
244 uint16_t effectKeySize;
245 uint32_t effectOffset = desc->fKey.count();
egdaniela7dc0a82014-09-17 08:25:05 -0700246 effectKeySuccess |= GetEffectKey(optState.getCoverageStage(s), gpu->glCaps(),
247 requiresLocalCoordAttrib, &b, &effectKeySize);
egdaniel170f90b2014-09-16 12:54:40 -0700248 effectKeySuccess |= (effectOffset <= SK_MaxU16);
249
250 offsetAndSize[0] = SkToU16(effectOffset);
251 offsetAndSize[1] = effectKeySize;
252 ++offsetAndSizeIndex;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000253 }
egdaniel170f90b2014-09-16 12:54:40 -0700254
bsalomon848faf02014-07-11 10:01:02 -0700255 if (!effectKeySuccess) {
256 desc->fKey.reset();
257 return false;
258 }
259
bsalomon848faf02014-07-11 10:01:02 -0700260 // Because header is a pointer into the dynamic array, we can't push any new data into the key
261 // below here.
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000262
egdanielae444962014-09-22 12:29:52 -0700263 header->fUseFragShaderOnly = gpu->caps()->pathRenderingSupport() &&
264 GrGpu::IsPathRenderingDrawType(drawType) &&
265 gpu->glPathRendering()->texturingMode() == GrGLPathRendering::FixedFunction_TexturingMode;
266 SkASSERT(!header->fUseFragShaderOnly || !optState.hasGeometryProcessor());
267
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000268 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000269
270 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
271 // other than pass through values from the VS to the FS anyway).
272#if GR_GL_EXPERIMENTAL_GS
273#if 0
274 header->fExperimentalGS = gpu->caps().geometryShaderSupport();
275#else
276 header->fExperimentalGS = false;
277#endif
278#endif
egdanielae444962014-09-22 12:29:52 -0700279 bool defaultToUniformInputs = GR_GL_NO_CONSTANT_ATTRIBUTES || header->fUseFragShaderOnly;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000280
egdaniel170f90b2014-09-16 12:54:40 -0700281 if (!inputColorIsUsed) {
egdaniel842b0862014-09-02 10:01:30 -0700282 header->fColorInput = kAllOnes_ColorInput;
egdaniel170f90b2014-09-16 12:54:40 -0700283 } else if (defaultToUniformInputs && !requiresColorAttrib) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000284 header->fColorInput = kUniform_ColorInput;
285 } else {
286 header->fColorInput = kAttribute_ColorInput;
egdanielae444962014-09-22 12:29:52 -0700287 header->fUseFragShaderOnly = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000288 }
289
egdaniel170f90b2014-09-16 12:54:40 -0700290 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == optState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000291
egdaniel170f90b2014-09-16 12:54:40 -0700292 if (covIsSolidWhite || !inputCoverageIsUsed) {
egdaniel842b0862014-09-02 10:01:30 -0700293 header->fCoverageInput = kAllOnes_ColorInput;
egdaniel170f90b2014-09-16 12:54:40 -0700294 } else if (defaultToUniformInputs && !requiresCoverageAttrib) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000295 header->fCoverageInput = kUniform_ColorInput;
296 } else {
297 header->fCoverageInput = kAttribute_ColorInput;
egdanielae444962014-09-22 12:29:52 -0700298 header->fUseFragShaderOnly = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000299 }
300
egdaniela7dc0a82014-09-17 08:25:05 -0700301 if (optState.readsDst()) {
bsalomon49f085d2014-09-05 13:34:00 -0700302 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000303 const GrTexture* dstCopyTexture = NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700304 if (dstCopy) {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000305 dstCopyTexture = dstCopy->texture();
306 }
joshualitt30ba4362014-08-21 20:18:45 -0700307 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
308 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000309 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000310 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000311 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000312 }
313
egdaniela7dc0a82014-09-17 08:25:05 -0700314 if (optState.readsFragPosition()) {
joshualitt30ba4362014-08-21 20:18:45 -0700315 header->fFragPosKey = GrGLFragmentShaderBuilder::KeyForFragmentPosition(
egdaniel170f90b2014-09-16 12:54:40 -0700316 optState.getRenderTarget(), gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000317 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000318 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000319 }
320
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000321 // Record attribute indices
egdaniel170f90b2014-09-16 12:54:40 -0700322 header->fPositionAttributeIndex = optState.positionAttributeIndex();
323 header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000324
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000325 // For constant color and coverage we need an attribute with an index beyond those already set
egdaniel170f90b2014-09-16 12:54:40 -0700326 int availableAttributeIndex = optState.getVertexAttribCount();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000327 if (requiresColorAttrib) {
egdaniel170f90b2014-09-16 12:54:40 -0700328 header->fColorAttributeIndex = optState.colorVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000329 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000330 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000331 header->fColorAttributeIndex = availableAttributeIndex;
332 availableAttributeIndex++;
333 } else {
334 header->fColorAttributeIndex = -1;
335 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000336
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000337 if (requiresCoverageAttrib) {
egdaniel170f90b2014-09-16 12:54:40 -0700338 header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000339 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000340 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000341 header->fCoverageAttributeIndex = availableAttributeIndex;
342 } else {
343 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000344 }
345
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000346 // Here we deal with whether/how we handle color and coverage separately.
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000347
commit-bot@chromium.org8a135882014-02-05 16:29:12 +0000348 // Set this default and then possibly change our mind if there is coverage.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000349 header->fCoverageOutput = kModulate_CoverageOutput;
350
351 // If we do have coverage determine whether it matters.
egdaniel170f90b2014-09-16 12:54:40 -0700352 bool separateCoverageFromColor = optState.hasGeometryProcessor();
353 if (!optState.isCoverageDrawing() &&
354 (optState.numCoverageStages() > 0 ||
355 optState.hasGeometryProcessor() ||
joshualittbd769d02014-09-04 08:56:46 -0700356 requiresCoverageAttrib)) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000357
egdaniel170f90b2014-09-16 12:54:40 -0700358 if (gpu->caps()->dualSourceBlendingSupport()) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000359 if (kZero_GrBlendCoeff == dstCoeff) {
360 // write the coverage value to second color
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000361 header->fCoverageOutput = kSecondaryCoverage_CoverageOutput;
362 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000363 } else if (kSA_GrBlendCoeff == dstCoeff) {
364 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000365 header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput;
366 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000367 } else if (kSC_GrBlendCoeff == dstCoeff) {
368 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000369 header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput;
370 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000371 }
egdaniela7dc0a82014-09-17 08:25:05 -0700372 } else if (optState.readsDst() &&
bsalomon@google.com0c89db22013-05-15 17:53:04 +0000373 kOne_GrBlendCoeff == srcCoeff &&
374 kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000375 header->fCoverageOutput = kCombineWithDst_CoverageOutput;
376 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000377 }
378 }
joshualittbd769d02014-09-04 08:56:46 -0700379
egdaniel170f90b2014-09-16 12:54:40 -0700380 for (int s = 0; s < optState.numColorStages(); ++s) {
381 colorStages->push_back(&optState.getColorStage(s));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000382 }
egdaniel170f90b2014-09-16 12:54:40 -0700383 SkTArray<const GrEffectStage*, true>* array;
384 if (separateCoverageFromColor) {
385 array = coverageStages;
386 } else {
387 array = colorStages;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000388 }
egdaniel170f90b2014-09-16 12:54:40 -0700389 for (int s = 0; s < optState.numCoverageStages(); ++s) {
390 array->push_back(&optState.getCoverageStage(s));
391 }
392
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000393 header->fColorEffectCnt = colorStages->count();
394 header->fCoverageEffectCnt = coverageStages->count();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000395
bsalomon848faf02014-07-11 10:01:02 -0700396 desc->finalize();
397 return true;
398}
399
400void GrGLProgramDesc::finalize() {
401 int keyLength = fKey.count();
402 SkASSERT(0 == (keyLength % 4));
403 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength);
404
405 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>();
406 *checksum = 0;
407 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000408}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000409
410GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
bsalomon848faf02014-07-11 10:01:02 -0700411 size_t keyLength = other.keyLength();
412 fKey.reset(keyLength);
413 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000414 return *this;
415}