blob: e655210a459c04163718731718dec42f6264fe7a [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
8#include "GrGLProgramDesc.h"
9#include "GrBackendEffectFactory.h"
10#include "GrDrawEffect.h"
11#include "GrEffect.h"
bsalomon@google.com26e18b52013-03-29 19:22:36 +000012#include "GrGLShaderBuilder.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +000013#include "GrGpuGL.h"
14
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000015#include "SkChecksum.h"
16
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000017namespace {
18inline GrGLEffect::EffectKey get_key_and_update_stats(const GrEffectStage& stage,
19 const GrGLCaps& caps,
20 bool useExplicitLocalCoords,
21 bool* setTrueIfReadsDst,
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000022 bool* setTrueIfReadsPos,
23 bool* setTrueIfHasVertexCode) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000024 const GrEffectRef& effect = *stage.getEffect();
25 const GrBackendEffectFactory& factory = effect->getFactory();
26 GrDrawEffect drawEffect(stage, useExplicitLocalCoords);
27 if (effect->willReadDstColor()) {
28 *setTrueIfReadsDst = true;
29 }
30 if (effect->willReadFragmentPosition()) {
31 *setTrueIfReadsPos = true;
32 }
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000033 if (effect->hasVertexCode()) {
34 *setTrueIfHasVertexCode = true;
35 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000036 return factory.glEffectKey(drawEffect, caps);
37}
38}
bsalomon@google.com798c8c42013-03-27 19:50:27 +000039void GrGLProgramDesc::Build(const GrDrawState& drawState,
40 bool isPoints,
41 GrDrawState::BlendOptFlags blendOpts,
42 GrBlendCoeff srcCoeff,
43 GrBlendCoeff dstCoeff,
44 const GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +000045 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000046 SkTArray<const GrEffectStage*, true>* colorStages,
47 SkTArray<const GrEffectStage*, true>* coverageStages,
bsalomon@google.com798c8c42013-03-27 19:50:27 +000048 GrGLProgramDesc* desc) {
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000049 colorStages->reset();
50 coverageStages->reset();
51
bsalomon@google.com798c8c42013-03-27 19:50:27 +000052 // This should already have been caught
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000053 SkASSERT(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts));
bsalomon@google.com798c8c42013-03-27 19:50:27 +000054
55 bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
56
57 bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag |
58 GrDrawState::kEmitCoverage_BlendOptFlag));
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000059 int firstEffectiveColorStage = 0;
60 bool inputColorIsUsed = true;
61 if (!skipColor) {
62 firstEffectiveColorStage = drawState.numColorStages();
63 while (firstEffectiveColorStage > 0 && inputColorIsUsed) {
64 --firstEffectiveColorStage;
65 const GrEffect* effect = drawState.getColorStage(firstEffectiveColorStage).getEffect()->get();
66 inputColorIsUsed = effect->willUseInputColor();
67 }
68 }
69
70 int firstEffectiveCoverageStage = 0;
71 bool inputCoverageIsUsed = true;
72 if (!skipCoverage) {
73 firstEffectiveCoverageStage = drawState.numCoverageStages();
74 while (firstEffectiveCoverageStage > 0 && inputCoverageIsUsed) {
75 --firstEffectiveCoverageStage;
76 const GrEffect* effect = drawState.getCoverageStage(firstEffectiveCoverageStage).getEffect()->get();
77 inputCoverageIsUsed = effect->willUseInputColor();
78 }
79 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +000080
81 // The descriptor is used as a cache key. Thus when a field of the
82 // descriptor will not affect program generation (because of the attribute
83 // bindings in use or other descriptor field settings) it should be set
84 // to a canonical value to avoid duplicate programs with different keys.
85
jvanverth@google.com054ae992013-04-01 20:06:51 +000086 bool requiresColorAttrib = !skipColor && drawState.hasColorVertexAttribute();
87 bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAttribute();
88 // we only need the local coords if we're actually going to generate effect code
89 bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) &&
90 drawState.hasLocalCoordAttribute();
bsalomon@google.com798c8c42013-03-27 19:50:27 +000091
bsalomon@google.com798c8c42013-03-27 19:50:27 +000092 bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
93 bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) ||
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000094 (!requiresColorAttrib && 0xffffffff == drawState.getColor()) ||
95 (!inputColorIsUsed);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000096
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000097 int numEffects = (skipColor ? 0 : (drawState.numColorStages() - firstEffectiveColorStage)) +
98 (skipCoverage ? 0 : (drawState.numCoverageStages() - firstEffectiveCoverageStage));
bsalomon@google.com798c8c42013-03-27 19:50:27 +000099
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000100 size_t newKeyLength = KeyLength(numEffects);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000101 bool allocChanged;
102 desc->fKey.reset(newKeyLength, SkAutoMalloc::kAlloc_OnShrink, &allocChanged);
103 if (allocChanged || !desc->fInitialized) {
104 // make sure any padding in the header is zero if we we haven't used this allocation before.
105 memset(desc->header(), 0, kHeaderSize);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000106 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000107 // write the key length
robertphillips@google.coma4662862013-11-21 14:24:16 +0000108 *desc->atOffset<uint32_t, kLengthOffset>() = SkToU32(newKeyLength);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000109
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000110 KeyHeader* header = desc->header();
111 EffectKey* effectKeys = desc->effectKeys();
112
113 int currEffectKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000114 bool readsDst = false;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000115 bool readFragPosition = false;
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000116 bool hasVertexCode = false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000117 if (!skipColor) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000118 for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000119 effectKeys[currEffectKey++] =
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000120 get_key_and_update_stats(drawState.getColorStage(s), gpu->glCaps(),
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000121 requiresLocalCoordAttrib, &readsDst, &readFragPosition,
122 &hasVertexCode);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000123 }
124 }
125 if (!skipCoverage) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000126 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000127 effectKeys[currEffectKey++] =
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000128 get_key_and_update_stats(drawState.getCoverageStage(s), gpu->glCaps(),
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000129 requiresLocalCoordAttrib, &readsDst, &readFragPosition,
130 &hasVertexCode);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000131 }
132 }
133
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000134 header->fHasVertexCode = hasVertexCode || requiresLocalCoordAttrib;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000135 header->fEmitsPointSize = isPoints;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000136
137 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
138 // other than pass through values from the VS to the FS anyway).
139#if GR_GL_EXPERIMENTAL_GS
140#if 0
141 header->fExperimentalGS = gpu->caps().geometryShaderSupport();
142#else
143 header->fExperimentalGS = false;
144#endif
145#endif
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000146 bool defaultToUniformInputs = GR_GL_NO_CONSTANT_ATTRIBUTES || gpu->caps()->pathRenderingSupport();
147
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000148 if (colorIsTransBlack) {
149 header->fColorInput = kTransBlack_ColorInput;
150 } else if (colorIsSolidWhite) {
151 header->fColorInput = kSolidWhite_ColorInput;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000152 } else if (defaultToUniformInputs && !requiresColorAttrib) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000153 header->fColorInput = kUniform_ColorInput;
154 } else {
155 header->fColorInput = kAttribute_ColorInput;
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000156 header->fHasVertexCode = true;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000157 }
158
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000159 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.getCoverage();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000160
161 if (skipCoverage) {
162 header->fCoverageInput = kTransBlack_ColorInput;
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000163 } else if (covIsSolidWhite || !inputCoverageIsUsed) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000164 header->fCoverageInput = kSolidWhite_ColorInput;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000165 } else if (defaultToUniformInputs && !requiresCoverageAttrib) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000166 header->fCoverageInput = kUniform_ColorInput;
167 } else {
168 header->fCoverageInput = kAttribute_ColorInput;
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000169 header->fHasVertexCode = true;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000170 }
171
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000172 if (readsDst) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000173 SkASSERT(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000174 const GrTexture* dstCopyTexture = NULL;
175 if (NULL != dstCopy) {
176 dstCopyTexture = dstCopy->texture();
177 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000178 header->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000179 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000180 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000181 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000182 }
183
184 if (readFragPosition) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000185 header->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(drawState.getRenderTarget(),
bsalomon@google.comb5158812013-05-13 18:50:25 +0000186 gpu->glCaps());
187 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000188 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000189 }
190
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000191 // Record attribute indices
192 header->fPositionAttributeIndex = drawState.positionAttributeIndex();
193 header->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000194
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000195 // For constant color and coverage we need an attribute with an index beyond those already set
196 int availableAttributeIndex = drawState.getVertexAttribCount();
197 if (requiresColorAttrib) {
198 header->fColorAttributeIndex = drawState.colorVertexAttributeIndex();
199 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000200 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000201 header->fColorAttributeIndex = availableAttributeIndex;
202 availableAttributeIndex++;
203 } else {
204 header->fColorAttributeIndex = -1;
205 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000206
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000207 if (requiresCoverageAttrib) {
208 header->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex();
209 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000210 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000211 header->fCoverageAttributeIndex = availableAttributeIndex;
212 } else {
213 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000214 }
215
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000216 // Here we deal with whether/how we handle color and coverage separately.
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000217
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000218 // Set these defaults and then possibly change our mind if there is coverage.
219 header->fDiscardIfZeroCoverage = false;
220 header->fCoverageOutput = kModulate_CoverageOutput;
221
222 // If we do have coverage determine whether it matters.
223 bool separateCoverageFromColor = false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000224 if (!drawState.isCoverageDrawing() && !skipCoverage &&
225 (drawState.numCoverageStages() > 0 || requiresCoverageAttrib)) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000226
227 // If we're stenciling then we want to discard samples that have zero coverage
228 if (drawState.getStencil().doesWrite()) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000229 header->fDiscardIfZeroCoverage = true;
230 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000231 }
232
233 if (gpu->caps()->dualSourceBlendingSupport() &&
234 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag |
235 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) {
236 if (kZero_GrBlendCoeff == dstCoeff) {
237 // write the coverage value to second color
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000238 header->fCoverageOutput = kSecondaryCoverage_CoverageOutput;
239 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000240 } else if (kSA_GrBlendCoeff == dstCoeff) {
241 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000242 header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput;
243 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000244 } else if (kSC_GrBlendCoeff == dstCoeff) {
245 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000246 header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput;
247 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000248 }
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000249 } else if (readsDst &&
bsalomon@google.com0c89db22013-05-15 17:53:04 +0000250 kOne_GrBlendCoeff == srcCoeff &&
251 kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000252 header->fCoverageOutput = kCombineWithDst_CoverageOutput;
253 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000254 }
255 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000256 if (!skipColor) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000257 for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000258 colorStages->push_back(&drawState.getColorStage(s));
259 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000260 }
261 if (!skipCoverage) {
262 SkTArray<const GrEffectStage*, true>* array;
263 if (separateCoverageFromColor) {
264 array = coverageStages;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000265 } else {
266 array = colorStages;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000267 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000268 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000269 array->push_back(&drawState.getCoverageStage(s));
270 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000271 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000272 header->fColorEffectCnt = colorStages->count();
273 header->fCoverageEffectCnt = coverageStages->count();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000274
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000275 *desc->checksum() = 0;
276 *desc->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(desc->fKey.get()),
277 newKeyLength);
278 desc->fInitialized = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000279}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000280
281GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
282 fInitialized = other.fInitialized;
283 if (fInitialized) {
284 size_t keyLength = other.keyLength();
285 fKey.reset(keyLength);
286 memcpy(fKey.get(), other.fKey.get(), keyLength);
287 }
288 return *this;
289}