blob: c6560be2b257e91991d1dffd106a7a2fdcd46f38 [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,
166 const 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
egdaniela7dc0a82014-09-17 08:25:05 -0700263 // 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.org0a6fe712014-04-23 19:26:26 +0000269 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000270
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.orgc4dc0ad2013-10-09 14:11:33 +0000280 bool defaultToUniformInputs = GR_GL_NO_CONSTANT_ATTRIBUTES || gpu->caps()->pathRenderingSupport();
281
egdaniel170f90b2014-09-16 12:54:40 -0700282 if (!inputColorIsUsed) {
egdaniel842b0862014-09-02 10:01:30 -0700283 header->fColorInput = kAllOnes_ColorInput;
egdaniel170f90b2014-09-16 12:54:40 -0700284 } else if (defaultToUniformInputs && !requiresColorAttrib) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000285 header->fColorInput = kUniform_ColorInput;
286 } else {
287 header->fColorInput = kAttribute_ColorInput;
kkinnunenec56e452014-08-25 22:21:16 -0700288 header->fRequiresVertexShader = true;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000289 }
290
egdaniel170f90b2014-09-16 12:54:40 -0700291 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == optState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000292
egdaniel170f90b2014-09-16 12:54:40 -0700293 if (covIsSolidWhite || !inputCoverageIsUsed) {
egdaniel842b0862014-09-02 10:01:30 -0700294 header->fCoverageInput = kAllOnes_ColorInput;
egdaniel170f90b2014-09-16 12:54:40 -0700295 } else if (defaultToUniformInputs && !requiresCoverageAttrib) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000296 header->fCoverageInput = kUniform_ColorInput;
297 } else {
298 header->fCoverageInput = kAttribute_ColorInput;
kkinnunenec56e452014-08-25 22:21:16 -0700299 header->fRequiresVertexShader = true;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000300 }
301
egdaniela7dc0a82014-09-17 08:25:05 -0700302 if (optState.readsDst()) {
bsalomon49f085d2014-09-05 13:34:00 -0700303 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000304 const GrTexture* dstCopyTexture = NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700305 if (dstCopy) {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000306 dstCopyTexture = dstCopy->texture();
307 }
joshualitt30ba4362014-08-21 20:18:45 -0700308 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
309 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000310 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000311 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000312 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000313 }
314
egdaniela7dc0a82014-09-17 08:25:05 -0700315 if (optState.readsFragPosition()) {
joshualitt30ba4362014-08-21 20:18:45 -0700316 header->fFragPosKey = GrGLFragmentShaderBuilder::KeyForFragmentPosition(
egdaniel170f90b2014-09-16 12:54:40 -0700317 optState.getRenderTarget(), gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000318 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000319 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000320 }
321
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000322 // Record attribute indices
egdaniel170f90b2014-09-16 12:54:40 -0700323 header->fPositionAttributeIndex = optState.positionAttributeIndex();
324 header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000325
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000326 // For constant color and coverage we need an attribute with an index beyond those already set
egdaniel170f90b2014-09-16 12:54:40 -0700327 int availableAttributeIndex = optState.getVertexAttribCount();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000328 if (requiresColorAttrib) {
egdaniel170f90b2014-09-16 12:54:40 -0700329 header->fColorAttributeIndex = optState.colorVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000330 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000331 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000332 header->fColorAttributeIndex = availableAttributeIndex;
333 availableAttributeIndex++;
334 } else {
335 header->fColorAttributeIndex = -1;
336 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000337
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000338 if (requiresCoverageAttrib) {
egdaniel170f90b2014-09-16 12:54:40 -0700339 header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000340 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000341 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000342 header->fCoverageAttributeIndex = availableAttributeIndex;
343 } else {
344 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000345 }
346
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000347 // Here we deal with whether/how we handle color and coverage separately.
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000348
commit-bot@chromium.org8a135882014-02-05 16:29:12 +0000349 // Set this default and then possibly change our mind if there is coverage.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000350 header->fCoverageOutput = kModulate_CoverageOutput;
351
352 // If we do have coverage determine whether it matters.
egdaniel170f90b2014-09-16 12:54:40 -0700353 bool separateCoverageFromColor = optState.hasGeometryProcessor();
354 if (!optState.isCoverageDrawing() &&
355 (optState.numCoverageStages() > 0 ||
356 optState.hasGeometryProcessor() ||
joshualittbd769d02014-09-04 08:56:46 -0700357 requiresCoverageAttrib)) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000358
egdaniel170f90b2014-09-16 12:54:40 -0700359 if (gpu->caps()->dualSourceBlendingSupport()) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000360 if (kZero_GrBlendCoeff == dstCoeff) {
361 // write the coverage value to second color
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000362 header->fCoverageOutput = kSecondaryCoverage_CoverageOutput;
363 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000364 } else if (kSA_GrBlendCoeff == dstCoeff) {
365 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000366 header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput;
367 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000368 } else if (kSC_GrBlendCoeff == dstCoeff) {
369 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000370 header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput;
371 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000372 }
egdaniela7dc0a82014-09-17 08:25:05 -0700373 } else if (optState.readsDst() &&
bsalomon@google.com0c89db22013-05-15 17:53:04 +0000374 kOne_GrBlendCoeff == srcCoeff &&
375 kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000376 header->fCoverageOutput = kCombineWithDst_CoverageOutput;
377 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000378 }
379 }
joshualittbd769d02014-09-04 08:56:46 -0700380
egdaniel170f90b2014-09-16 12:54:40 -0700381 for (int s = 0; s < optState.numColorStages(); ++s) {
382 colorStages->push_back(&optState.getColorStage(s));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000383 }
egdaniel170f90b2014-09-16 12:54:40 -0700384 SkTArray<const GrEffectStage*, true>* array;
385 if (separateCoverageFromColor) {
386 array = coverageStages;
387 } else {
388 array = colorStages;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000389 }
egdaniel170f90b2014-09-16 12:54:40 -0700390 for (int s = 0; s < optState.numCoverageStages(); ++s) {
391 array->push_back(&optState.getCoverageStage(s));
392 }
393
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000394 header->fColorEffectCnt = colorStages->count();
395 header->fCoverageEffectCnt = coverageStages->count();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000396
bsalomon848faf02014-07-11 10:01:02 -0700397 desc->finalize();
398 return true;
399}
400
401void 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.com798c8c42013-03-27 19:50:27 +0000409}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000410
411GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
bsalomon848faf02014-07-11 10:01:02 -0700412 size_t keyLength = other.keyLength();
413 fKey.reset(keyLength);
414 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000415 return *this;
416}