blob: 19a26c9b08c887f69438174a9da8a88684613833 [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"
joshualittb0a8a372014-09-23 09:50:21 -070010#include "GrBackendProcessorFactory.h"
11#include "GrProcessor.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +000012#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
joshualittb0a8a372014-09-23 09:50:21 -070063static uint32_t gen_attrib_key(const GrGeometryProcessor* effect) {
joshualitt23e280d2014-09-18 12:26:38 -070064 uint32_t key = 0;
65
joshualittb0a8a372014-09-23 09:50:21 -070066 const GrGeometryProcessor::VertexAttribArray& vars = effect->getVertexAttribs();
joshualitt23e280d2014-09-18 12:26:38 -070067 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
joshualittb0a8a372014-09-23 09:50:21 -070076static uint32_t gen_transform_key(const GrProcessorStage& effectStage,
joshualitt23e280d2014-09-18 12:26:38 -070077 bool useExplicitLocalCoords) {
78 uint32_t totalKey = 0;
joshualittb0a8a372014-09-23 09:50:21 -070079 int numTransforms = effectStage.getProcessor()->numTransforms();
joshualitt23e280d2014-09-18 12:26:38 -070080 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
joshualittb0a8a372014-09-23 09:50:21 -070088 const GrCoordTransform& coordTransform = effectStage.getProcessor()->coordTransform(t);
joshualitt23e280d2014-09-18 12:26:38 -070089 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
joshualittb0a8a372014-09-23 09:50:21 -070099static uint32_t gen_texture_key(const GrProcessor* effect, const GrGLCaps& caps) {
joshualitt23e280d2014-09-18 12:26:38 -0700100 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
joshualittb0a8a372014-09-23 09:50:21 -0700117 * 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.
joshualitt23e280d2014-09-18 12:26:38 -0700119 */
120
joshualittb0a8a372014-09-23 09:50:21 -0700121static uint32_t* get_processor_meta_key(const GrProcessorStage& processorStage,
122 bool useExplicitLocalCoords,
123 const GrGLCaps& caps,
124 GrProcessorKeyBuilder* b) {
joshualitt23e280d2014-09-18 12:26:38 -0700125
joshualittb0a8a372014-09-23 09:50:21 -0700126 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();
joshualitt23e280d2014-09-18 12:26:38 -0700129
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);
joshualittb0a8a372014-09-23 09:50:21 -0700133 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
134 return NULL;
joshualitt23e280d2014-09-18 12:26:38 -0700135 }
136
137 uint32_t* key = b->add32n(2);
138 key[0] = (textureKey << 16 | transformKey);
joshualittb0a8a372014-09-23 09:50:21 -0700139 key[1] = (classID << 16);
140 return key;
141}
142
143bool GrGLProgramDesc::GetProcessorKey(const GrProcessorStage& stage,
144 const GrGLCaps& caps,
145 bool useExplicitLocalCoords,
146 GrProcessorKeyBuilder* b,
147 uint16_t* processorKeySize) {
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 }
joshualitt23e280d2014-09-18 12:26:38 -0700160 return true;
161}
162
joshualittb0a8a372014-09-23 09:50:21 -0700163bool GrGLProgramDesc::GetGeometryProcessorKey(const GrGeometryStage& stage,
164 const GrGLCaps& caps,
165 bool useExplicitLocalCoords,
166 GrProcessorKeyBuilder* b,
167 uint16_t* processorKeySize) {
168 const GrProcessor& effect = *stage.getProcessor();
169 const GrBackendProcessorFactory& factory = effect.getFactory();
170 factory.getGLProcessorKey(effect, caps, b);
bsalomon929f29a2014-07-17 07:55:11 -0700171 size_t size = b->size();
172 if (size > SK_MaxU16) {
joshualittb0a8a372014-09-23 09:50:21 -0700173 *processorKeySize = 0; // suppresses a warning.
bsalomon929f29a2014-07-17 07:55:11 -0700174 return false;
175 }
joshualittb0a8a372014-09-23 09:50:21 -0700176 *processorKeySize = SkToU16(size);
177 uint32_t* key = get_processor_meta_key(stage, useExplicitLocalCoords, caps, b);
178 if (NULL == key) {
bsalomon929f29a2014-07-17 07:55:11 -0700179 return false;
180 }
joshualittb0a8a372014-09-23 09:50:21 -0700181 uint32_t attribKey = gen_attrib_key(stage.getGeometryProcessor());
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;
bsalomon929f29a2014-07-17 07:55:11 -0700191 return true;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000192}
bsalomon848faf02014-07-11 10:01:02 -0700193
joshualittb0a8a372014-09-23 09:50:21 -0700194
egdaniel170f90b2014-09-16 12:54:40 -0700195bool GrGLProgramDesc::Build(const GrOptDrawState& optState,
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000196 GrGpu::DrawType drawType,
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000197 GrBlendCoeff srcCoeff,
198 GrBlendCoeff dstCoeff,
egdanielae444962014-09-22 12:29:52 -0700199 GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000200 const GrDeviceCoordTexture* dstCopy,
joshualittb0a8a372014-09-23 09:50:21 -0700201 const GrGeometryStage** geometryProcessor,
202 SkTArray<const GrFragmentStage*, true>* colorStages,
203 SkTArray<const GrFragmentStage*, true>* coverageStages,
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000204 GrGLProgramDesc* desc) {
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000205 colorStages->reset();
206 coverageStages->reset();
207
egdaniel170f90b2014-09-16 12:54:40 -0700208 bool inputColorIsUsed = optState.inputColorIsUsed();
egdaniel033ea7f2014-09-23 08:14:13 -0700209 bool inputCoverageIsUsed = optState.inputCoverageIsUsed();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000210
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
egdaniela7dc0a82014-09-17 08:25:05 -0700216 bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib();
kkinnunenec56e452014-08-25 22:21:16 -0700217
egdaniel170f90b2014-09-16 12:54:40 -0700218 int numStages = optState.numTotalStages();
219
bsalomon929f29a2014-07-17 07:55:11 -0700220 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t));
bsalomon848faf02014-07-11 10:01:02 -0700221 // Make room for everything up to and including the array of offsets to effect keys.
222 desc->fKey.reset();
bsalomon929f29a2014-07-17 07:55:11 -0700223 desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * numStages);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000224
bsalomon929f29a2014-07-17 07:55:11 -0700225 int offsetAndSizeIndex = 0;
joshualittbd769d02014-09-04 08:56:46 -0700226
227 KeyHeader* header = desc->header();
228 // make sure any padding in the header is zeroed.
229 memset(desc->header(), 0, kHeaderSize);
230
231 // We can only have one effect which touches the vertex shader
egdaniel170f90b2014-09-16 12:54:40 -0700232 if (optState.hasGeometryProcessor()) {
joshualittbd769d02014-09-04 08:56:46 -0700233 uint16_t* offsetAndSize =
bsalomon929f29a2014-07-17 07:55:11 -0700234 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset +
235 offsetAndSizeIndex * 2 * sizeof(uint16_t));
bsalomon848faf02014-07-11 10:01:02 -0700236
joshualittb0a8a372014-09-23 09:50:21 -0700237 GrProcessorKeyBuilder b(&desc->fKey);
238 uint16_t processorKeySize;
239 uint32_t processorOffset = desc->fKey.count();
240 const GrGeometryStage& gpStage = *optState.getGeometryProcessor();
241 if (processorOffset > SK_MaxU16 ||
242 !GetGeometryProcessorKey(gpStage, gpu->glCaps(), requiresLocalCoordAttrib, &b,
243 &processorKeySize)) {
244 desc->fKey.reset();
245 return false;
246 }
bsalomon929f29a2014-07-17 07:55:11 -0700247
joshualittb0a8a372014-09-23 09:50:21 -0700248 offsetAndSize[0] = SkToU16(processorOffset);
249 offsetAndSize[1] = processorKeySize;
250 ++offsetAndSizeIndex;
251 *geometryProcessor = &gpStage;
252 header->fHasGeometryProcessor = true;
joshualittbd769d02014-09-04 08:56:46 -0700253 }
254
egdaniel170f90b2014-09-16 12:54:40 -0700255 for (int s = 0; s < optState.numColorStages(); ++s) {
256 uint16_t* offsetAndSize =
257 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset +
258 offsetAndSizeIndex * 2 * sizeof(uint16_t));
joshualittbd769d02014-09-04 08:56:46 -0700259
joshualittb0a8a372014-09-23 09:50:21 -0700260 GrProcessorKeyBuilder b(&desc->fKey);
261 uint16_t processorKeySize;
262 uint32_t processorOffset = desc->fKey.count();
263 if (processorOffset > SK_MaxU16 ||
264 !GetProcessorKey(optState.getColorStage(s), gpu->glCaps(),
265 requiresLocalCoordAttrib, &b, &processorKeySize)) {
266 desc->fKey.reset();
267 return false;
268 }
joshualittbd769d02014-09-04 08:56:46 -0700269
joshualittb0a8a372014-09-23 09:50:21 -0700270 offsetAndSize[0] = SkToU16(processorOffset);
271 offsetAndSize[1] = processorKeySize;
egdaniel170f90b2014-09-16 12:54:40 -0700272 ++offsetAndSizeIndex;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000273 }
bsalomon929f29a2014-07-17 07:55:11 -0700274
egdaniel170f90b2014-09-16 12:54:40 -0700275 for (int s = 0; s < optState.numCoverageStages(); ++s) {
276 uint16_t* offsetAndSize =
277 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset +
278 offsetAndSizeIndex * 2 * sizeof(uint16_t));
bsalomon929f29a2014-07-17 07:55:11 -0700279
joshualittb0a8a372014-09-23 09:50:21 -0700280 GrProcessorKeyBuilder b(&desc->fKey);
281 uint16_t processorKeySize;
282 uint32_t processorOffset = desc->fKey.count();
283 if (processorOffset > SK_MaxU16 ||
284 !GetProcessorKey(optState.getCoverageStage(s), gpu->glCaps(),
285 requiresLocalCoordAttrib, &b, &processorKeySize)) {
286 desc->fKey.reset();
287 return false;
288 }
egdaniel170f90b2014-09-16 12:54:40 -0700289
joshualittb0a8a372014-09-23 09:50:21 -0700290 offsetAndSize[0] = SkToU16(processorOffset);
291 offsetAndSize[1] = processorKeySize;
egdaniel170f90b2014-09-16 12:54:40 -0700292 ++offsetAndSizeIndex;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000293 }
egdaniel170f90b2014-09-16 12:54:40 -0700294
bsalomon848faf02014-07-11 10:01:02 -0700295 // Because header is a pointer into the dynamic array, we can't push any new data into the key
296 // below here.
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000297
egdanielae444962014-09-22 12:29:52 -0700298
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000299 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000300
301 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
302 // other than pass through values from the VS to the FS anyway).
303#if GR_GL_EXPERIMENTAL_GS
304#if 0
305 header->fExperimentalGS = gpu->caps().geometryShaderSupport();
306#else
307 header->fExperimentalGS = false;
308#endif
309#endif
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000310
bsalomoncd523eb2014-09-23 08:19:00 -0700311 if (gpu->caps()->pathRenderingSupport() &&
312 GrGpu::IsPathRenderingDrawType(drawType) &&
313 gpu->glPathRendering()->texturingMode() == GrGLPathRendering::FixedFunction_TexturingMode) {
314 header->fUseFragShaderOnly = true;
315 SkASSERT(!optState.hasGeometryProcessor());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000316 } else {
egdanielae444962014-09-22 12:29:52 -0700317 header->fUseFragShaderOnly = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000318 }
319
bsalomoncd523eb2014-09-23 08:19:00 -0700320 bool defaultToUniformInputs = GrGpu::IsPathRenderingDrawType(drawType) ||
321 GR_GL_NO_CONSTANT_ATTRIBUTES;
322
323 if (!inputColorIsUsed) {
324 header->fColorInput = kAllOnes_ColorInput;
325 } else if (defaultToUniformInputs && !optState.hasColorVertexAttribute()) {
326 header->fColorInput = kUniform_ColorInput;
327 } else {
328 header->fColorInput = kAttribute_ColorInput;
329 SkASSERT(!header->fUseFragShaderOnly);
330 }
331
332 bool covIsSolidWhite = !optState.hasCoverageVertexAttribute() &&
333 0xffffffff == optState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000334
egdaniel170f90b2014-09-16 12:54:40 -0700335 if (covIsSolidWhite || !inputCoverageIsUsed) {
egdaniel842b0862014-09-02 10:01:30 -0700336 header->fCoverageInput = kAllOnes_ColorInput;
bsalomoncd523eb2014-09-23 08:19:00 -0700337 } else if (defaultToUniformInputs && !optState.hasCoverageVertexAttribute()) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000338 header->fCoverageInput = kUniform_ColorInput;
339 } else {
340 header->fCoverageInput = kAttribute_ColorInput;
bsalomoncd523eb2014-09-23 08:19:00 -0700341 SkASSERT(!header->fUseFragShaderOnly);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000342 }
343
egdaniela7dc0a82014-09-17 08:25:05 -0700344 if (optState.readsDst()) {
bsalomon49f085d2014-09-05 13:34:00 -0700345 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000346 const GrTexture* dstCopyTexture = NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700347 if (dstCopy) {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000348 dstCopyTexture = dstCopy->texture();
349 }
joshualitt30ba4362014-08-21 20:18:45 -0700350 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
351 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000352 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000353 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000354 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000355 }
356
egdaniela7dc0a82014-09-17 08:25:05 -0700357 if (optState.readsFragPosition()) {
joshualitt30ba4362014-08-21 20:18:45 -0700358 header->fFragPosKey = GrGLFragmentShaderBuilder::KeyForFragmentPosition(
egdaniel170f90b2014-09-16 12:54:40 -0700359 optState.getRenderTarget(), gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000360 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000361 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000362 }
363
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000364 // Record attribute indices
egdaniel170f90b2014-09-16 12:54:40 -0700365 header->fPositionAttributeIndex = optState.positionAttributeIndex();
366 header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000367
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000368 // For constant color and coverage we need an attribute with an index beyond those already set
egdaniel170f90b2014-09-16 12:54:40 -0700369 int availableAttributeIndex = optState.getVertexAttribCount();
bsalomoncd523eb2014-09-23 08:19:00 -0700370 if (optState.hasColorVertexAttribute()) {
egdaniel170f90b2014-09-16 12:54:40 -0700371 header->fColorAttributeIndex = optState.colorVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000372 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000373 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000374 header->fColorAttributeIndex = availableAttributeIndex;
375 availableAttributeIndex++;
376 } else {
377 header->fColorAttributeIndex = -1;
378 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000379
bsalomoncd523eb2014-09-23 08:19:00 -0700380 if (optState.hasCoverageVertexAttribute()) {
egdaniel170f90b2014-09-16 12:54:40 -0700381 header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000382 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000383 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000384 header->fCoverageAttributeIndex = availableAttributeIndex;
385 } else {
386 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000387 }
388
egdanielc0648242014-09-22 13:17:02 -0700389 header->fPrimaryOutputType = optState.getPrimaryOutputType();
390 header->fSecondaryOutputType = optState.getSecondaryOutputType();
joshualittbd769d02014-09-04 08:56:46 -0700391
egdaniel170f90b2014-09-16 12:54:40 -0700392 for (int s = 0; s < optState.numColorStages(); ++s) {
393 colorStages->push_back(&optState.getColorStage(s));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000394 }
egdaniel170f90b2014-09-16 12:54:40 -0700395 for (int s = 0; s < optState.numCoverageStages(); ++s) {
egdanielc0648242014-09-22 13:17:02 -0700396 coverageStages->push_back(&optState.getCoverageStage(s));
egdaniel170f90b2014-09-16 12:54:40 -0700397 }
398
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000399 header->fColorEffectCnt = colorStages->count();
400 header->fCoverageEffectCnt = coverageStages->count();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000401
bsalomon848faf02014-07-11 10:01:02 -0700402 desc->finalize();
403 return true;
404}
405
406void GrGLProgramDesc::finalize() {
407 int keyLength = fKey.count();
408 SkASSERT(0 == (keyLength % 4));
409 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength);
410
411 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>();
412 *checksum = 0;
413 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000414}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000415
416GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
bsalomon848faf02014-07-11 10:01:02 -0700417 size_t keyLength = other.keyLength();
418 fKey.reset(keyLength);
419 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000420 return *this;
421}