blob: 545ed2114c7d5ea01512cae139f7f0537bb9e15b [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"
11#include "GrDrawEffect.h"
12#include "GrEffect.h"
13#include "GrGpuGL.h"
14
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000015#include "SkChecksum.h"
16
bsalomon929f29a2014-07-17 07:55:11 -070017bool GrGLProgramDesc::GetEffectKeyAndUpdateStats(const GrEffectStage& stage,
18 const GrGLCaps& caps,
19 bool useExplicitLocalCoords,
20 GrEffectKeyBuilder* b,
21 uint16_t* effectKeySize,
22 bool* setTrueIfReadsDst,
23 bool* setTrueIfReadsPos,
kkinnunenec56e452014-08-25 22:21:16 -070024 bool* setTrueIfRequiresVertexShader) {
bsalomon848faf02014-07-11 10:01:02 -070025 const GrBackendEffectFactory& factory = stage.getEffect()->getFactory();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000026 GrDrawEffect drawEffect(stage, useExplicitLocalCoords);
bsalomon848faf02014-07-11 10:01:02 -070027 if (stage.getEffect()->willReadDstColor()) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000028 *setTrueIfReadsDst = true;
29 }
bsalomon848faf02014-07-11 10:01:02 -070030 if (stage.getEffect()->willReadFragmentPosition()) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000031 *setTrueIfReadsPos = true;
32 }
kkinnunenec56e452014-08-25 22:21:16 -070033 if (stage.getEffect()->requiresVertexShader()) {
34 *setTrueIfRequiresVertexShader = true;
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000035 }
bsalomon929f29a2014-07-17 07:55:11 -070036 factory.getGLEffectKey(drawEffect, caps, b);
37 size_t size = b->size();
38 if (size > SK_MaxU16) {
39 *effectKeySize = 0; // suppresses a warning.
40 return false;
41 }
42 *effectKeySize = SkToU16(size);
43 if (!GrGLProgramEffects::GenEffectMetaKey(drawEffect, caps, b)) {
44 return false;
45 }
46 return true;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000047}
bsalomon848faf02014-07-11 10:01:02 -070048
49bool GrGLProgramDesc::Build(const GrDrawState& drawState,
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000050 GrGpu::DrawType drawType,
bsalomon@google.com798c8c42013-03-27 19:50:27 +000051 GrDrawState::BlendOptFlags blendOpts,
52 GrBlendCoeff srcCoeff,
53 GrBlendCoeff dstCoeff,
54 const GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +000055 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000056 SkTArray<const GrEffectStage*, true>* colorStages,
57 SkTArray<const GrEffectStage*, true>* coverageStages,
bsalomon@google.com798c8c42013-03-27 19:50:27 +000058 GrGLProgramDesc* desc) {
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000059 colorStages->reset();
60 coverageStages->reset();
61
bsalomon@google.com798c8c42013-03-27 19:50:27 +000062 // This should already have been caught
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000063 SkASSERT(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts));
bsalomon@google.com798c8c42013-03-27 19:50:27 +000064
65 bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
66
67 bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag |
68 GrDrawState::kEmitCoverage_BlendOptFlag));
egdaniel02cafcc2014-07-21 11:37:28 -070069
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000070 int firstEffectiveColorStage = 0;
71 bool inputColorIsUsed = true;
72 if (!skipColor) {
73 firstEffectiveColorStage = drawState.numColorStages();
74 while (firstEffectiveColorStage > 0 && inputColorIsUsed) {
75 --firstEffectiveColorStage;
bsalomonf99f8842014-07-07 11:54:23 -070076 const GrEffect* effect = drawState.getColorStage(firstEffectiveColorStage).getEffect();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000077 inputColorIsUsed = effect->willUseInputColor();
78 }
79 }
80
81 int firstEffectiveCoverageStage = 0;
82 bool inputCoverageIsUsed = true;
83 if (!skipCoverage) {
84 firstEffectiveCoverageStage = drawState.numCoverageStages();
85 while (firstEffectiveCoverageStage > 0 && inputCoverageIsUsed) {
86 --firstEffectiveCoverageStage;
bsalomonf99f8842014-07-07 11:54:23 -070087 const GrEffect* effect = drawState.getCoverageStage(firstEffectiveCoverageStage).getEffect();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000088 inputCoverageIsUsed = effect->willUseInputColor();
89 }
90 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +000091
92 // The descriptor is used as a cache key. Thus when a field of the
93 // descriptor will not affect program generation (because of the attribute
94 // bindings in use or other descriptor field settings) it should be set
95 // to a canonical value to avoid duplicate programs with different keys.
96
jvanverth@google.com054ae992013-04-01 20:06:51 +000097 bool requiresColorAttrib = !skipColor && drawState.hasColorVertexAttribute();
98 bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAttribute();
99 // we only need the local coords if we're actually going to generate effect code
100 bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) &&
101 drawState.hasLocalCoordAttribute();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000102
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000103 bool readsDst = false;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000104 bool readFragPosition = false;
kkinnunenec56e452014-08-25 22:21:16 -0700105
106 // Provide option for shader programs without vertex shader only when drawing paths.
107 bool requiresVertexShader = !GrGpu::IsPathRenderingDrawType(drawType);
108
bsalomon848faf02014-07-11 10:01:02 -0700109 int numStages = 0;
110 if (!skipColor) {
111 numStages += drawState.numColorStages() - firstEffectiveColorStage;
112 }
113 if (!skipCoverage) {
114 numStages += drawState.numCoverageStages() - firstEffectiveCoverageStage;
115 }
bsalomon929f29a2014-07-17 07:55:11 -0700116 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t));
bsalomon848faf02014-07-11 10:01:02 -0700117 // Make room for everything up to and including the array of offsets to effect keys.
118 desc->fKey.reset();
bsalomon929f29a2014-07-17 07:55:11 -0700119 desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * numStages);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000120
bsalomon929f29a2014-07-17 07:55:11 -0700121 int offsetAndSizeIndex = 0;
bsalomon848faf02014-07-11 10:01:02 -0700122 bool effectKeySuccess = true;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000123 if (!skipColor) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000124 for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) {
bsalomon929f29a2014-07-17 07:55:11 -0700125 uint16_t* offsetAndSize =
126 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset +
127 offsetAndSizeIndex * 2 * sizeof(uint16_t));
bsalomon848faf02014-07-11 10:01:02 -0700128
129 GrEffectKeyBuilder b(&desc->fKey);
bsalomon929f29a2014-07-17 07:55:11 -0700130 uint16_t effectKeySize;
131 uint32_t effectOffset = desc->fKey.count();
132 effectKeySuccess |= GetEffectKeyAndUpdateStats(
133 drawState.getColorStage(s), gpu->glCaps(),
134 requiresLocalCoordAttrib, &b,
135 &effectKeySize, &readsDst,
kkinnunenec56e452014-08-25 22:21:16 -0700136 &readFragPosition, &requiresVertexShader);
bsalomon929f29a2014-07-17 07:55:11 -0700137 effectKeySuccess |= (effectOffset <= SK_MaxU16);
138
139 offsetAndSize[0] = SkToU16(effectOffset);
140 offsetAndSize[1] = effectKeySize;
141 ++offsetAndSizeIndex;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000142 }
143 }
144 if (!skipCoverage) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000145 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) {
bsalomon929f29a2014-07-17 07:55:11 -0700146 uint16_t* offsetAndSize =
147 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset +
148 offsetAndSizeIndex * 2 * sizeof(uint16_t));
149
bsalomon848faf02014-07-11 10:01:02 -0700150 GrEffectKeyBuilder b(&desc->fKey);
bsalomon929f29a2014-07-17 07:55:11 -0700151 uint16_t effectKeySize;
152 uint32_t effectOffset = desc->fKey.count();
153 effectKeySuccess |= GetEffectKeyAndUpdateStats(
154 drawState.getCoverageStage(s), gpu->glCaps(),
155 requiresLocalCoordAttrib, &b,
156 &effectKeySize, &readsDst,
kkinnunenec56e452014-08-25 22:21:16 -0700157 &readFragPosition, &requiresVertexShader);
bsalomon929f29a2014-07-17 07:55:11 -0700158 effectKeySuccess |= (effectOffset <= SK_MaxU16);
159
160 offsetAndSize[0] = SkToU16(effectOffset);
161 offsetAndSize[1] = effectKeySize;
162 ++offsetAndSizeIndex;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000163 }
164 }
bsalomon848faf02014-07-11 10:01:02 -0700165 if (!effectKeySuccess) {
166 desc->fKey.reset();
167 return false;
168 }
169
170 KeyHeader* header = desc->header();
171 // make sure any padding in the header is zeroed.
172 memset(desc->header(), 0, kHeaderSize);
173
174 // Because header is a pointer into the dynamic array, we can't push any new data into the key
175 // below here.
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000176
kkinnunenec56e452014-08-25 22:21:16 -0700177 header->fRequiresVertexShader = requiresVertexShader || requiresLocalCoordAttrib;
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000178 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000179
180 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
181 // other than pass through values from the VS to the FS anyway).
182#if GR_GL_EXPERIMENTAL_GS
183#if 0
184 header->fExperimentalGS = gpu->caps().geometryShaderSupport();
185#else
186 header->fExperimentalGS = false;
187#endif
188#endif
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000189 bool defaultToUniformInputs = GR_GL_NO_CONSTANT_ATTRIBUTES || gpu->caps()->pathRenderingSupport();
190
egdaniel842b0862014-09-02 10:01:30 -0700191 if (!inputColorIsUsed && !skipColor) {
192 header->fColorInput = kAllOnes_ColorInput;
193 } else if (defaultToUniformInputs && !requiresColorAttrib && inputColorIsUsed) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000194 header->fColorInput = kUniform_ColorInput;
195 } else {
196 header->fColorInput = kAttribute_ColorInput;
kkinnunenec56e452014-08-25 22:21:16 -0700197 header->fRequiresVertexShader = true;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000198 }
199
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000200 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000201
egdaniel02cafcc2014-07-21 11:37:28 -0700202 if ((covIsSolidWhite || !inputCoverageIsUsed) && !skipCoverage) {
egdaniel842b0862014-09-02 10:01:30 -0700203 header->fCoverageInput = kAllOnes_ColorInput;
egdaniel02cafcc2014-07-21 11:37:28 -0700204 } else if (defaultToUniformInputs && !requiresCoverageAttrib && inputCoverageIsUsed) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000205 header->fCoverageInput = kUniform_ColorInput;
206 } else {
207 header->fCoverageInput = kAttribute_ColorInput;
kkinnunenec56e452014-08-25 22:21:16 -0700208 header->fRequiresVertexShader = true;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000209 }
210
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000211 if (readsDst) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000212 SkASSERT(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000213 const GrTexture* dstCopyTexture = NULL;
214 if (NULL != dstCopy) {
215 dstCopyTexture = dstCopy->texture();
216 }
joshualitt30ba4362014-08-21 20:18:45 -0700217 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
218 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000219 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000220 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000221 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000222 }
223
224 if (readFragPosition) {
joshualitt30ba4362014-08-21 20:18:45 -0700225 header->fFragPosKey = GrGLFragmentShaderBuilder::KeyForFragmentPosition(
226 drawState.getRenderTarget(), gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000227 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000228 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000229 }
230
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000231 // Record attribute indices
232 header->fPositionAttributeIndex = drawState.positionAttributeIndex();
233 header->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000234
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000235 // For constant color and coverage we need an attribute with an index beyond those already set
236 int availableAttributeIndex = drawState.getVertexAttribCount();
237 if (requiresColorAttrib) {
238 header->fColorAttributeIndex = drawState.colorVertexAttributeIndex();
239 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000240 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000241 header->fColorAttributeIndex = availableAttributeIndex;
242 availableAttributeIndex++;
243 } else {
244 header->fColorAttributeIndex = -1;
245 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000246
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000247 if (requiresCoverageAttrib) {
248 header->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex();
249 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000250 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000251 header->fCoverageAttributeIndex = availableAttributeIndex;
252 } else {
253 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000254 }
255
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000256 // Here we deal with whether/how we handle color and coverage separately.
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000257
commit-bot@chromium.org8a135882014-02-05 16:29:12 +0000258 // Set this default and then possibly change our mind if there is coverage.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000259 header->fCoverageOutput = kModulate_CoverageOutput;
260
261 // If we do have coverage determine whether it matters.
262 bool separateCoverageFromColor = false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000263 if (!drawState.isCoverageDrawing() && !skipCoverage &&
264 (drawState.numCoverageStages() > 0 || requiresCoverageAttrib)) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000265
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000266 if (gpu->caps()->dualSourceBlendingSupport() &&
267 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag |
268 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) {
269 if (kZero_GrBlendCoeff == dstCoeff) {
270 // write the coverage value to second color
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000271 header->fCoverageOutput = kSecondaryCoverage_CoverageOutput;
272 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000273 } else if (kSA_GrBlendCoeff == dstCoeff) {
274 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000275 header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput;
276 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000277 } else if (kSC_GrBlendCoeff == dstCoeff) {
278 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000279 header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput;
280 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000281 }
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000282 } else if (readsDst &&
bsalomon@google.com0c89db22013-05-15 17:53:04 +0000283 kOne_GrBlendCoeff == srcCoeff &&
284 kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000285 header->fCoverageOutput = kCombineWithDst_CoverageOutput;
286 separateCoverageFromColor = true;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000287 }
288 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000289 if (!skipColor) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000290 for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000291 colorStages->push_back(&drawState.getColorStage(s));
292 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000293 }
294 if (!skipCoverage) {
295 SkTArray<const GrEffectStage*, true>* array;
296 if (separateCoverageFromColor) {
297 array = coverageStages;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000298 } else {
299 array = colorStages;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000300 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000301 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000302 array->push_back(&drawState.getCoverageStage(s));
303 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000304 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000305 header->fColorEffectCnt = colorStages->count();
306 header->fCoverageEffectCnt = coverageStages->count();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000307
bsalomon848faf02014-07-11 10:01:02 -0700308 desc->finalize();
309 return true;
310}
311
312void GrGLProgramDesc::finalize() {
313 int keyLength = fKey.count();
314 SkASSERT(0 == (keyLength % 4));
315 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength);
316
317 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>();
318 *checksum = 0;
319 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000320}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000321
322GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
bsalomon848faf02014-07-11 10:01:02 -0700323 size_t keyLength = other.keyLength();
324 fKey.reset(keyLength);
325 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000326 return *this;
327}