blob: 0c85c99a8a4725659760dd60c92eba7ebeb154a9 [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
joshualitt47bb3822014-10-07 16:43:25 -07008#include "gl/builders/GrGLFragmentShaderBuilder.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
joshualitt89c7a2e2014-10-10 14:11:59 -070063static uint32_t gen_attrib_key(const GrGeometryProcessor* effect) {
joshualitt23e280d2014-09-18 12:26:38 -070064 uint32_t key = 0;
65
joshualitt89c7a2e2014-10-10 14:11:59 -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
joshualitt89c7a2e2014-10-10 14:11:59 -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
joshualitt89c7a2e2014-10-10 14:11:59 -070099static uint32_t gen_texture_key(const GrProcessor* effect, const GrGLCaps& caps) {
joshualitt23e280d2014-09-18 12:26:38 -0700100 uint32_t key = 0;
joshualitt89c7a2e2014-10-10 14:11:59 -0700101 int numTextures = effect->numTextures();
joshualitt23e280d2014-09-18 12:26:38 -0700102 for (int t = 0; t < numTextures; ++t) {
joshualitt89c7a2e2014-10-10 14:11:59 -0700103 const GrTextureAccess& access = effect->textureAccess(t);
joshualitt23e280d2014-09-18 12:26:38 -0700104 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
joshualitt89c7a2e2014-10-10 14:11:59 -0700118 * textures, transforms, etc, for the space allotted in the meta-key.
joshualitt23e280d2014-09-18 12:26:38 -0700119 */
joshualitt89c7a2e2014-10-10 14:11:59 -0700120
121static uint32_t* get_processor_meta_key(const GrProcessorStage& processorStage,
122 bool useExplicitLocalCoords,
123 const GrGLCaps& caps,
124 GrProcessorKeyBuilder* b) {
125
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();
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);
133 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
134 return NULL;
135 }
136
137 uint32_t* key = b->add32n(2);
138 key[0] = (textureKey << 16 | transformKey);
139 key[1] = (classID << 16);
140 return key;
141}
142
143static bool get_fp_key(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);
joshualitt71856d52014-10-10 09:56:55 -0700151 size_t size = b->size();
152 if (size > SK_MaxU16) {
153 *processorKeySize = 0; // suppresses a warning.
154 return false;
155 }
156 *processorKeySize = SkToU16(size);
joshualitt89c7a2e2014-10-10 14:11:59 -0700157 if (NULL == get_processor_meta_key(stage, useExplicitLocalCoords, caps, b)) {
158 return false;
159 }
160 return true;
161}
162
163static bool get_gp_key(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);
171 size_t size = b->size();
172 if (size > SK_MaxU16) {
173 *processorKeySize = 0; // suppresses a warning.
174 return false;
175 }
176 *processorKeySize = SkToU16(size);
177 uint32_t* key = get_processor_meta_key(stage, useExplicitLocalCoords, caps, b);
178 if (NULL == key) {
179 return false;
180 }
181 uint32_t attribKey = gen_attrib_key(stage.getProcessor());
joshualitt23e280d2014-09-18 12:26:38 -0700182
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);
joshualitt89c7a2e2014-10-10 14:11:59 -0700186 if ((attribKey) & kMetaKeyInvalidMask) {
187 return false;
joshualitt23e280d2014-09-18 12:26:38 -0700188 }
189
joshualitt89c7a2e2014-10-10 14:11:59 -0700190 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
joshualitt65171342014-10-09 07:25:36 -0700194struct GeometryProcessorKeyBuilder {
joshualitt89c7a2e2014-10-10 14:11:59 -0700195 typedef GrGeometryStage StagedProcessor;
196 static bool GetProcessorKey(const GrGeometryStage& gpStage,
joshualitt65171342014-10-09 07:25:36 -0700197 const GrGLCaps& caps,
joshualitt89c7a2e2014-10-10 14:11:59 -0700198 bool requiresLocalCoordAttrib,
joshualitt65171342014-10-09 07:25:36 -0700199 GrProcessorKeyBuilder* b,
joshualitt89c7a2e2014-10-10 14:11:59 -0700200 uint16_t* processorKeySize) {
201 return get_gp_key(gpStage, caps, requiresLocalCoordAttrib, b, processorKeySize);
joshualitt65171342014-10-09 07:25:36 -0700202 }
203};
204
205struct FragmentProcessorKeyBuilder {
206 typedef GrFragmentStage StagedProcessor;
joshualitt89c7a2e2014-10-10 14:11:59 -0700207 static bool GetProcessorKey(const GrFragmentStage& fpStage,
joshualitt65171342014-10-09 07:25:36 -0700208 const GrGLCaps& caps,
joshualitt89c7a2e2014-10-10 14:11:59 -0700209 bool requiresLocalCoordAttrib,
joshualitt65171342014-10-09 07:25:36 -0700210 GrProcessorKeyBuilder* b,
joshualitt89c7a2e2014-10-10 14:11:59 -0700211 uint16_t* processorKeySize) {
212 return get_fp_key(fpStage, caps, requiresLocalCoordAttrib, b, processorKeySize);
joshualitt65171342014-10-09 07:25:36 -0700213 }
214};
215
216
217template <class ProcessorKeyBuilder>
218bool
219GrGLProgramDesc::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}
joshualittb0a8a372014-09-23 09:50:21 -0700242
egdaniel170f90b2014-09-16 12:54:40 -0700243bool GrGLProgramDesc::Build(const GrOptDrawState& optState,
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000244 GrGpu::DrawType drawType,
joshualitt89c7a2e2014-10-10 14:11:59 -0700245 GrBlendCoeff srcCoeff,
246 GrBlendCoeff dstCoeff,
egdanielae444962014-09-22 12:29:52 -0700247 GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000248 const GrDeviceCoordTexture* dstCopy,
joshualitt89c7a2e2014-10-10 14:11:59 -0700249 const GrGeometryStage** geometryProcessor,
250 SkTArray<const GrFragmentStage*, true>* colorStages,
251 SkTArray<const GrFragmentStage*, true>* coverageStages,
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000252 GrGLProgramDesc* desc) {
joshualitt89c7a2e2014-10-10 14:11:59 -0700253 colorStages->reset();
254 coverageStages->reset();
255
egdaniel170f90b2014-09-16 12:54:40 -0700256 bool inputColorIsUsed = optState.inputColorIsUsed();
egdaniel033ea7f2014-09-23 08:14:13 -0700257 bool inputCoverageIsUsed = optState.inputCoverageIsUsed();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000258
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
egdaniela7dc0a82014-09-17 08:25:05 -0700264 bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib();
kkinnunenec56e452014-08-25 22:21:16 -0700265
egdaniel170f90b2014-09-16 12:54:40 -0700266 int numStages = optState.numTotalStages();
267
bsalomon929f29a2014-07-17 07:55:11 -0700268 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t));
bsalomon848faf02014-07-11 10:01:02 -0700269 // Make room for everything up to and including the array of offsets to effect keys.
270 desc->fKey.reset();
bsalomon929f29a2014-07-17 07:55:11 -0700271 desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * numStages);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000272
bsalomon929f29a2014-07-17 07:55:11 -0700273 int offsetAndSizeIndex = 0;
joshualittbd769d02014-09-04 08:56:46 -0700274
joshualittbd769d02014-09-04 08:56:46 -0700275 // We can only have one effect which touches the vertex shader
egdaniel170f90b2014-09-16 12:54:40 -0700276 if (optState.hasGeometryProcessor()) {
joshualitt89c7a2e2014-10-10 14:11:59 -0700277 const GrGeometryStage& gpStage = *optState.getGeometryProcessor();
278 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(gpStage,
joshualitt65171342014-10-09 07:25:36 -0700279 gpu->glCaps(),
joshualitt89c7a2e2014-10-10 14:11:59 -0700280 requiresLocalCoordAttrib,
281 desc,
282 &offsetAndSizeIndex)) {
283 return false;
284 }
285 *geometryProcessor = &gpStage;
286 }
287
288 for (int s = 0; s < optState.numColorStages(); ++s) {
289 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getColorStage(s),
290 gpu->glCaps(),
291 requiresLocalCoordAttrib,
joshualitt65171342014-10-09 07:25:36 -0700292 desc,
293 &offsetAndSizeIndex)) {
joshualittb0a8a372014-09-23 09:50:21 -0700294 return false;
295 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000296 }
bsalomon929f29a2014-07-17 07:55:11 -0700297
joshualitt89c7a2e2014-10-10 14:11:59 -0700298 for (int s = 0; s < optState.numCoverageStages(); ++s) {
299 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getCoverageStage(s),
joshualitt65171342014-10-09 07:25:36 -0700300 gpu->glCaps(),
301 requiresLocalCoordAttrib,
302 desc,
303 &offsetAndSizeIndex)) {
joshualittb0a8a372014-09-23 09:50:21 -0700304 return false;
305 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000306 }
egdaniel170f90b2014-09-16 12:54:40 -0700307
joshualitt65171342014-10-09 07:25:36 -0700308 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700309 // Because header is a pointer into the dynamic array, we can't push any new data into the key
310 // below here.
joshualitt65171342014-10-09 07:25:36 -0700311 KeyHeader* header = desc->header();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000312
joshualitt65171342014-10-09 07:25:36 -0700313 // make sure any padding in the header is zeroed.
314 memset(header, 0, kHeaderSize);
315
316 header->fHasGeometryProcessor = optState.hasGeometryProcessor();
egdanielae444962014-09-22 12:29:52 -0700317
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000318 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000319
bsalomoncd523eb2014-09-23 08:19:00 -0700320 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.com2db3ded2013-05-22 14:34:04 +0000325 } else {
egdanielae444962014-09-22 12:29:52 -0700326 header->fUseFragShaderOnly = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000327 }
328
bsalomoncd523eb2014-09-23 08:19:00 -0700329 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.com2db3ded2013-05-22 14:34:04 +0000343
egdaniel170f90b2014-09-16 12:54:40 -0700344 if (covIsSolidWhite || !inputCoverageIsUsed) {
egdaniel842b0862014-09-02 10:01:30 -0700345 header->fCoverageInput = kAllOnes_ColorInput;
bsalomoncd523eb2014-09-23 08:19:00 -0700346 } else if (defaultToUniformInputs && !optState.hasCoverageVertexAttribute()) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000347 header->fCoverageInput = kUniform_ColorInput;
348 } else {
349 header->fCoverageInput = kAttribute_ColorInput;
bsalomoncd523eb2014-09-23 08:19:00 -0700350 SkASSERT(!header->fUseFragShaderOnly);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000351 }
352
egdaniela7dc0a82014-09-17 08:25:05 -0700353 if (optState.readsDst()) {
bsalomon49f085d2014-09-05 13:34:00 -0700354 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000355 const GrTexture* dstCopyTexture = NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700356 if (dstCopy) {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000357 dstCopyTexture = dstCopy->texture();
358 }
joshualitt30ba4362014-08-21 20:18:45 -0700359 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
joshualitt47bb3822014-10-07 16:43:25 -0700360 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000361 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000362 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000363 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000364 }
365
egdaniela7dc0a82014-09-17 08:25:05 -0700366 if (optState.readsFragPosition()) {
joshualitt47bb3822014-10-07 16:43:25 -0700367 header->fFragPosKey =
368 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRenderTarget(),
369 gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000370 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000371 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000372 }
373
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000374 // Record attribute indices
egdaniel170f90b2014-09-16 12:54:40 -0700375 header->fPositionAttributeIndex = optState.positionAttributeIndex();
376 header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000377
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000378 // For constant color and coverage we need an attribute with an index beyond those already set
egdaniel170f90b2014-09-16 12:54:40 -0700379 int availableAttributeIndex = optState.getVertexAttribCount();
bsalomoncd523eb2014-09-23 08:19:00 -0700380 if (optState.hasColorVertexAttribute()) {
egdaniel170f90b2014-09-16 12:54:40 -0700381 header->fColorAttributeIndex = optState.colorVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000382 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000383 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000384 header->fColorAttributeIndex = availableAttributeIndex;
385 availableAttributeIndex++;
386 } else {
387 header->fColorAttributeIndex = -1;
388 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000389
bsalomoncd523eb2014-09-23 08:19:00 -0700390 if (optState.hasCoverageVertexAttribute()) {
egdaniel170f90b2014-09-16 12:54:40 -0700391 header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000392 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000393 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000394 header->fCoverageAttributeIndex = availableAttributeIndex;
395 } else {
396 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000397 }
398
egdanielc0648242014-09-22 13:17:02 -0700399 header->fPrimaryOutputType = optState.getPrimaryOutputType();
400 header->fSecondaryOutputType = optState.getSecondaryOutputType();
joshualittbd769d02014-09-04 08:56:46 -0700401
joshualitt89c7a2e2014-10-10 14:11:59 -0700402 for (int s = 0; s < optState.numColorStages(); ++s) {
403 colorStages->push_back(&optState.getColorStage(s));
404 }
405 for (int s = 0; s < optState.numCoverageStages(); ++s) {
406 coverageStages->push_back(&optState.getCoverageStage(s));
407 }
408
409 header->fColorEffectCnt = colorStages->count();
410 header->fCoverageEffectCnt = coverageStages->count();
bsalomon848faf02014-07-11 10:01:02 -0700411 desc->finalize();
412 return true;
413}
414
415void 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.com798c8c42013-03-27 19:50:27 +0000423}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000424
425GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
bsalomon848faf02014-07-11 10:01:02 -0700426 size_t keyLength = other.keyLength();
427 fKey.reset(keyLength);
428 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000429 return *this;
430}