blob: 73846b8df380371d1c74999fed985ea5e14f407d [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
15void GrGLProgramDesc::Build(const GrDrawState& drawState,
16 bool isPoints,
17 GrDrawState::BlendOptFlags blendOpts,
18 GrBlendCoeff srcCoeff,
19 GrBlendCoeff dstCoeff,
20 const GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +000021 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com798c8c42013-03-27 19:50:27 +000022 GrGLProgramDesc* desc) {
23
24 // This should already have been caught
25 GrAssert(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts));
26
27 bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
28
29 bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag |
30 GrDrawState::kEmitCoverage_BlendOptFlag));
31
32 // The descriptor is used as a cache key. Thus when a field of the
33 // descriptor will not affect program generation (because of the attribute
34 // bindings in use or other descriptor field settings) it should be set
35 // to a canonical value to avoid duplicate programs with different keys.
36
bsalomon@google.com798c8c42013-03-27 19:50:27 +000037
38 desc->fEmitsPointSize = isPoints;
39
jvanverth@google.com054ae992013-04-01 20:06:51 +000040 bool requiresColorAttrib = !skipColor && drawState.hasColorVertexAttribute();
41 bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAttribute();
42 // we only need the local coords if we're actually going to generate effect code
43 bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) &&
44 drawState.hasLocalCoordAttribute();
bsalomon@google.com798c8c42013-03-27 19:50:27 +000045
46 // fColorInput/fCoverageInput records how colors are specified for the program so we strip the
47 // bits from the bindings to avoid false negatives when searching for an existing program in the
48 // cache.
bsalomon@google.com798c8c42013-03-27 19:50:27 +000049
50 desc->fColorFilterXfermode = skipColor ? SkXfermode::kDst_Mode : drawState.getColorFilterMode();
51
bsalomon@google.com798c8c42013-03-27 19:50:27 +000052
53 bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
54 bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) ||
jvanverth@google.com054ae992013-04-01 20:06:51 +000055 (!requiresColorAttrib && 0xffffffff == drawState.getColor());
bsalomon@google.com798c8c42013-03-27 19:50:27 +000056 if (colorIsTransBlack) {
57 desc->fColorInput = kTransBlack_ColorInput;
58 } else if (colorIsSolidWhite) {
59 desc->fColorInput = kSolidWhite_ColorInput;
jvanverth@google.com054ae992013-04-01 20:06:51 +000060 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresColorAttrib) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +000061 desc->fColorInput = kUniform_ColorInput;
62 } else {
63 desc->fColorInput = kAttribute_ColorInput;
64 }
65
jvanverth@google.com054ae992013-04-01 20:06:51 +000066 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.getCoverage();
bsalomon@google.com798c8c42013-03-27 19:50:27 +000067
68 if (skipCoverage) {
69 desc->fCoverageInput = kTransBlack_ColorInput;
70 } else if (covIsSolidWhite) {
71 desc->fCoverageInput = kSolidWhite_ColorInput;
jvanverth@google.com054ae992013-04-01 20:06:51 +000072 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresCoverageAttrib) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +000073 desc->fCoverageInput = kUniform_ColorInput;
74 } else {
75 desc->fCoverageInput = kAttribute_ColorInput;
76 }
77
bsalomon@google.com26e18b52013-03-29 19:22:36 +000078 bool readsDst = false;
bsalomon@google.com798c8c42013-03-27 19:50:27 +000079 int lastEnabledStage = -1;
80
81 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
82
83 bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCoverage;
84 if (!skip && drawState.isStageEnabled(s)) {
85 lastEnabledStage = s;
86 const GrEffectRef& effect = *drawState.getStage(s).getEffect();
87 const GrBackendEffectFactory& factory = effect->getFactory();
jvanverth@google.com054ae992013-04-01 20:06:51 +000088 GrDrawEffect drawEffect(drawState.getStage(s), requiresLocalCoordAttrib);
bsalomon@google.com798c8c42013-03-27 19:50:27 +000089 desc->fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
bsalomon@google.com26e18b52013-03-29 19:22:36 +000090 if (effect->willReadDst()) {
91 readsDst = true;
92 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +000093 } else {
94 desc->fEffectKeys[s] = 0;
95 }
96 }
97
bsalomon@google.com26e18b52013-03-29 19:22:36 +000098 if (readsDst) {
99 GrAssert(NULL != dstCopy);
100 desc->fDstRead = GrGLShaderBuilder::KeyForDstRead(dstCopy->texture(), gpu->glCaps());
101 GrAssert(0 != desc->fDstRead);
102 } else {
103 desc->fDstRead = 0;
104 }
105
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000106 desc->fDualSrcOutput = kNone_DualSrcOutput;
107
108 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
109 // other than pass through values from the VS to the FS anyway).
110#if GR_GL_EXPERIMENTAL_GS
111#if 0
112 desc->fExperimentalGS = gpu->caps().geometryShaderSupport();
113#else
114 desc->fExperimentalGS = false;
115#endif
116#endif
117
118 // We leave this set to kNumStages until we discover that the coverage/color distinction is
119 // material to the generated program. We do this to avoid distinct keys that generate equivalent
120 // programs.
121 desc->fFirstCoverageStage = GrDrawState::kNumStages;
122 // This tracks the actual first coverage stage.
123 int firstCoverageStage = GrDrawState::kNumStages;
124 desc->fDiscardIfZeroCoverage = false; // Enabled below if stenciling and there is coverage.
125 bool hasCoverage = false;
126 // If we're rendering coverage-as-color then it's as though there are no coverage stages.
127 if (!drawState.isCoverageDrawing()) {
128 // We can have coverage either through a stage or coverage vertex attributes.
129 if (drawState.getFirstCoverageStage() <= lastEnabledStage) {
130 firstCoverageStage = drawState.getFirstCoverageStage();
131 hasCoverage = true;
132 } else {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000133 hasCoverage = requiresCoverageAttrib;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000134 }
135 }
136
137 if (hasCoverage) {
138 // color filter is applied between color/coverage computation
139 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
140 desc->fFirstCoverageStage = firstCoverageStage;
141 }
142
143 // If we're stenciling then we want to discard samples that have zero coverage
144 if (drawState.getStencil().doesWrite()) {
145 desc->fDiscardIfZeroCoverage = true;
146 desc->fFirstCoverageStage = firstCoverageStage;
147 }
148
149 if (gpu->caps()->dualSourceBlendingSupport() &&
150 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag |
151 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) {
152 if (kZero_GrBlendCoeff == dstCoeff) {
153 // write the coverage value to second color
154 desc->fDualSrcOutput = kCoverage_DualSrcOutput;
155 desc->fFirstCoverageStage = firstCoverageStage;
156 } else if (kSA_GrBlendCoeff == dstCoeff) {
157 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
158 desc->fDualSrcOutput = kCoverageISA_DualSrcOutput;
159 desc->fFirstCoverageStage = firstCoverageStage;
160 } else if (kSC_GrBlendCoeff == dstCoeff) {
161 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
162 desc->fDualSrcOutput = kCoverageISC_DualSrcOutput;
163 desc->fFirstCoverageStage = firstCoverageStage;
164 }
165 }
166 }
167
jvanverth@google.com054ae992013-04-01 20:06:51 +0000168 desc->fPositionAttributeIndex = drawState.positionAttributeIndex();
169 desc->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex();
170
171 // For constant color and coverage we need an attribute with an index beyond those already set
172 int availableAttributeIndex = drawState.getVertexAttribCount();
173 if (requiresColorAttrib) {
174 desc->fColorAttributeIndex = drawState.colorVertexAttributeIndex();
175 } else if (GrGLProgramDesc::kAttribute_ColorInput == desc->fColorInput) {
176 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
177 desc->fColorAttributeIndex = availableAttributeIndex;
178 availableAttributeIndex++;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000179 } else {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000180 desc->fColorAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000181 }
182
jvanverth@google.com054ae992013-04-01 20:06:51 +0000183 if (requiresCoverageAttrib) {
184 desc->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex();
185 } else if (GrGLProgramDesc::kAttribute_ColorInput == desc->fCoverageInput) {
186 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
187 desc->fCoverageAttributeIndex = availableAttributeIndex;
188 } else {
189 desc->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000190 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000191}