blob: cff4a2bab6f46ab886fa4889d5c62e396b8239f8 [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) {
bsalomonf99f8842014-07-07 11:54:23 -070024 const GrEffect* effect = stage.getEffect();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000025 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,
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000040 GrGpu::DrawType drawType,
bsalomon@google.com798c8c42013-03-27 19:50:27 +000041 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;
bsalomonf99f8842014-07-07 11:54:23 -070065 const GrEffect* effect = drawState.getColorStage(firstEffectiveColorStage).getEffect();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000066 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;
bsalomonf99f8842014-07-07 11:54:23 -070076 const GrEffect* effect = drawState.getCoverageStage(firstEffectiveCoverageStage).getEffect();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000077 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
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000092 int numEffects = (skipColor ? 0 : (drawState.numColorStages() - firstEffectiveColorStage)) +
93 (skipCoverage ? 0 : (drawState.numCoverageStages() - firstEffectiveCoverageStage));
bsalomon@google.com798c8c42013-03-27 19:50:27 +000094
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000095 size_t newKeyLength = KeyLength(numEffects);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000096 bool allocChanged;
97 desc->fKey.reset(newKeyLength, SkAutoMalloc::kAlloc_OnShrink, &allocChanged);
98 if (allocChanged || !desc->fInitialized) {
99 // make sure any padding in the header is zero if we we haven't used this allocation before.
100 memset(desc->header(), 0, kHeaderSize);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000101 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000102 // write the key length
robertphillips@google.coma4662862013-11-21 14:24:16 +0000103 *desc->atOffset<uint32_t, kLengthOffset>() = SkToU32(newKeyLength);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000104
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000105 KeyHeader* header = desc->header();
106 EffectKey* effectKeys = desc->effectKeys();
107
108 int currEffectKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000109 bool readsDst = false;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000110 bool readFragPosition = false;
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000111 // We use vertexshader-less shader programs only when drawing paths.
112 bool hasVertexCode = !(GrGpu::kDrawPath_DrawType == drawType ||
113 GrGpu::kDrawPaths_DrawType == drawType);
114
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000115 if (!skipColor) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000116 for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000117 effectKeys[currEffectKey++] =
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000118 get_key_and_update_stats(drawState.getColorStage(s), gpu->glCaps(),
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000119 requiresLocalCoordAttrib, &readsDst, &readFragPosition,
120 &hasVertexCode);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000121 }
122 }
123 if (!skipCoverage) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000124 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000125 effectKeys[currEffectKey++] =
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000126 get_key_and_update_stats(drawState.getCoverageStage(s), gpu->glCaps(),
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000127 requiresLocalCoordAttrib, &readsDst, &readFragPosition,
128 &hasVertexCode);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000129 }
130 }
131
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000132 header->fHasVertexCode = hasVertexCode || requiresLocalCoordAttrib;
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000133 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000134
135 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
136 // other than pass through values from the VS to the FS anyway).
137#if GR_GL_EXPERIMENTAL_GS
138#if 0
139 header->fExperimentalGS = gpu->caps().geometryShaderSupport();
140#else
141 header->fExperimentalGS = false;
142#endif
143#endif
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000144 bool defaultToUniformInputs = GR_GL_NO_CONSTANT_ATTRIBUTES || gpu->caps()->pathRenderingSupport();
145
egdaniel5f78d222014-07-11 08:57:40 -0700146 if (defaultToUniformInputs && !requiresColorAttrib) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000147 header->fColorInput = kUniform_ColorInput;
148 } else {
149 header->fColorInput = kAttribute_ColorInput;
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000150 header->fHasVertexCode = true;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000151 }
152
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000153 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000154
egdaniel5f78d222014-07-11 08:57:40 -0700155 if (covIsSolidWhite || !inputCoverageIsUsed) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000156 header->fCoverageInput = kSolidWhite_ColorInput;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000157 } else if (defaultToUniformInputs && !requiresCoverageAttrib) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000158 header->fCoverageInput = kUniform_ColorInput;
159 } else {
160 header->fCoverageInput = kAttribute_ColorInput;
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000161 header->fHasVertexCode = true;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000162 }
163
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000164 if (readsDst) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000165 SkASSERT(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000166 const GrTexture* dstCopyTexture = NULL;
167 if (NULL != dstCopy) {
168 dstCopyTexture = dstCopy->texture();
169 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000170 header->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000171 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000172 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000173 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000174 }
175
176 if (readFragPosition) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000177 header->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(drawState.getRenderTarget(),
bsalomon@google.comb5158812013-05-13 18:50:25 +0000178 gpu->glCaps());
179 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000180 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000181 }
182
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000183 // Record attribute indices
184 header->fPositionAttributeIndex = drawState.positionAttributeIndex();
185 header->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000186
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000187 // For constant color and coverage we need an attribute with an index beyond those already set
188 int availableAttributeIndex = drawState.getVertexAttribCount();
189 if (requiresColorAttrib) {
190 header->fColorAttributeIndex = drawState.colorVertexAttributeIndex();
191 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000192 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000193 header->fColorAttributeIndex = availableAttributeIndex;
194 availableAttributeIndex++;
195 } else {
196 header->fColorAttributeIndex = -1;
197 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000198
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000199 if (requiresCoverageAttrib) {
200 header->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex();
201 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000202 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000203 header->fCoverageAttributeIndex = availableAttributeIndex;
204 } else {
205 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000206 }
207
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000208 // Here we deal with whether/how we handle color and coverage separately.
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000209
commit-bot@chromium.org8a135882014-02-05 16:29:12 +0000210 // Set this default and then possibly change our mind if there is coverage.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000211 header->fCoverageOutput = kModulate_CoverageOutput;
212
213 // If we do have coverage determine whether it matters.
214 bool separateCoverageFromColor = false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000215 if (!drawState.isCoverageDrawing() && !skipCoverage &&
216 (drawState.numCoverageStages() > 0 || requiresCoverageAttrib)) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000217
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000218 if (gpu->caps()->dualSourceBlendingSupport() &&
219 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag |
220 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) {
221 if (kZero_GrBlendCoeff == dstCoeff) {
222 // write the coverage value to second color
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000223 header->fCoverageOutput = kSecondaryCoverage_CoverageOutput;
224 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000225 } else if (kSA_GrBlendCoeff == dstCoeff) {
226 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000227 header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput;
228 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000229 } else if (kSC_GrBlendCoeff == dstCoeff) {
230 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000231 header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput;
232 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000233 }
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000234 } else if (readsDst &&
bsalomon@google.com0c89db22013-05-15 17:53:04 +0000235 kOne_GrBlendCoeff == srcCoeff &&
236 kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000237 header->fCoverageOutput = kCombineWithDst_CoverageOutput;
238 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000239 }
240 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000241 if (!skipColor) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000242 for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000243 colorStages->push_back(&drawState.getColorStage(s));
244 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000245 }
246 if (!skipCoverage) {
247 SkTArray<const GrEffectStage*, true>* array;
248 if (separateCoverageFromColor) {
249 array = coverageStages;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000250 } else {
251 array = colorStages;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000252 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000253 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000254 array->push_back(&drawState.getCoverageStage(s));
255 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000256 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000257 header->fColorEffectCnt = colorStages->count();
258 header->fCoverageEffectCnt = coverageStages->count();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000259
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000260 *desc->checksum() = 0;
261 *desc->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(desc->fKey.get()),
262 newKeyLength);
263 desc->fInitialized = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000264}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000265
266GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
267 fInitialized = other.fInitialized;
268 if (fInitialized) {
269 size_t keyLength = other.keyLength();
270 fKey.reset(keyLength);
271 memcpy(fKey.get(), other.fKey.get(), keyLength);
272 }
273 return *this;
274}