OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrGLProgramDesc.h" | 8 #include "GrGLProgramDesc.h" |
9 #include "GrBackendEffectFactory.h" | 9 #include "GrBackendEffectFactory.h" |
10 #include "GrDrawEffect.h" | 10 #include "GrDrawEffect.h" |
11 #include "GrEffect.h" | 11 #include "GrEffect.h" |
12 #include "GrGLShaderBuilder.h" | 12 #include "GrGLShaderBuilder.h" |
13 #include "GrGpuGL.h" | 13 #include "GrGpuGL.h" |
14 | 14 |
| 15 #include "SkChecksum.h" |
| 16 |
15 void GrGLProgramDesc::Build(const GrDrawState& drawState, | 17 void GrGLProgramDesc::Build(const GrDrawState& drawState, |
16 bool isPoints, | 18 bool isPoints, |
17 GrDrawState::BlendOptFlags blendOpts, | 19 GrDrawState::BlendOptFlags blendOpts, |
18 GrBlendCoeff srcCoeff, | 20 GrBlendCoeff srcCoeff, |
19 GrBlendCoeff dstCoeff, | 21 GrBlendCoeff dstCoeff, |
20 const GrGpuGL* gpu, | 22 const GrGpuGL* gpu, |
21 const GrDeviceCoordTexture* dstCopy, | 23 const GrDeviceCoordTexture* dstCopy, |
| 24 const GrEffectStage* stages[], |
22 GrGLProgramDesc* desc) { | 25 GrGLProgramDesc* desc) { |
23 | |
24 // This should already have been caught | 26 // This should already have been caught |
25 GrAssert(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts)); | 27 GrAssert(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts)); |
26 | 28 |
27 bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendO
ptFlag); | 29 bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendO
ptFlag); |
28 | 30 |
29 bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOpt
Flag | | 31 bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOpt
Flag | |
30 GrDrawState::kEmitCoverage_BlendOptFl
ag)); | 32 GrDrawState::kEmitCoverage_BlendOptFl
ag)); |
31 | 33 |
32 // The descriptor is used as a cache key. Thus when a field of the | 34 // 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 | 35 // descriptor will not affect program generation (because of the attribute |
34 // bindings in use or other descriptor field settings) it should be set | 36 // bindings in use or other descriptor field settings) it should be set |
35 // to a canonical value to avoid duplicate programs with different keys. | 37 // to a canonical value to avoid duplicate programs with different keys. |
36 | 38 |
37 | |
38 desc->fEmitsPointSize = isPoints; | |
39 | |
40 bool requiresColorAttrib = !skipColor && drawState.hasColorVertexAttribute()
; | 39 bool requiresColorAttrib = !skipColor && drawState.hasColorVertexAttribute()
; |
41 bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAt
tribute(); | 40 bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAt
tribute(); |
42 // we only need the local coords if we're actually going to generate effect
code | 41 // we only need the local coords if we're actually going to generate effect
code |
43 bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) && | 42 bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) && |
44 drawState.hasLocalCoordAttribute(); | 43 drawState.hasLocalCoordAttribute(); |
45 | 44 |
46 // fColorInput/fCoverageInput records how colors are specified for the progr
am so we strip the | |
47 // bits from the bindings to avoid false negatives when searching for an exi
sting program in the | |
48 // cache. | |
49 | |
50 desc->fColorFilterXfermode = skipColor ? SkXfermode::kDst_Mode : drawState.g
etColorFilterMode(); | |
51 | |
52 | |
53 bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_B
lendOptFlag); | 45 bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_B
lendOptFlag); |
54 bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFla
g) || | 46 bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFla
g) || |
55 (!requiresColorAttrib && 0xffffffff == drawState.ge
tColor()); | 47 (!requiresColorAttrib && 0xffffffff == drawState.ge
tColor()); |
56 if (colorIsTransBlack) { | 48 |
57 desc->fColorInput = kTransBlack_ColorInput; | 49 // Do an initial loop over the stages to count them. We count the color and
coverage effects |
58 } else if (colorIsSolidWhite) { | 50 // separately here. Later we may decide the distinction doesn't matter and w
ill count all |
59 desc->fColorInput = kSolidWhite_ColorInput; | 51 // effects as color in desc. Two things will allow simplication of this mess
: GrDrawState will |
60 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresColorAttrib) { | 52 // have tight lists of color and coverage stages rather than a fixed size ar
ray with NULLS and |
61 desc->fColorInput = kUniform_ColorInput; | 53 // the xfermode-color filter will be removed. |
62 } else { | 54 int colorEffectCnt = 0; |
63 desc->fColorInput = kAttribute_ColorInput; | 55 int coverageEffectCnt = 0; |
| 56 if (!skipColor) { |
| 57 for (int s = 0; s < drawState.getFirstCoverageStage(); ++s) { |
| 58 if (drawState.isStageEnabled(s)) { |
| 59 stages[colorEffectCnt] = &drawState.getStage(s); |
| 60 ++colorEffectCnt; |
| 61 } |
| 62 } |
| 63 } |
| 64 if (!skipCoverage) { |
| 65 for (int s = drawState.getFirstCoverageStage(); s < GrDrawState::kNumSta
ges; ++s) { |
| 66 if (drawState.isStageEnabled(s)) { |
| 67 stages[colorEffectCnt + coverageEffectCnt] = &drawState.getStage
(s); |
| 68 ++coverageEffectCnt; |
| 69 } |
| 70 } |
64 } | 71 } |
65 | 72 |
66 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.ge
tCoverage(); | 73 size_t newKeyLength = KeyLength(colorEffectCnt + coverageEffectCnt); |
| 74 bool allocChanged; |
| 75 desc->fKey.reset(newKeyLength, SkAutoMalloc::kAlloc_OnShrink, &allocChanged)
; |
| 76 if (allocChanged || !desc->fInitialized) { |
| 77 // make sure any padding in the header is zero if we we haven't used thi
s allocation before. |
| 78 memset(desc->header(), 0, kHeaderSize); |
| 79 } |
| 80 // write the key length |
| 81 *desc->atOffset<uint32_t, kLengthOffset>() = newKeyLength; |
67 | 82 |
68 if (skipCoverage) { | 83 KeyHeader* header = desc->header(); |
69 desc->fCoverageInput = kTransBlack_ColorInput; | 84 EffectKey* effectKeys = desc->effectKeys(); |
70 } else if (covIsSolidWhite) { | |
71 desc->fCoverageInput = kSolidWhite_ColorInput; | |
72 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresCoverageAttrib) { | |
73 desc->fCoverageInput = kUniform_ColorInput; | |
74 } else { | |
75 desc->fCoverageInput = kAttribute_ColorInput; | |
76 } | |
77 | 85 |
| 86 int currEffectKey = 0; |
78 bool readsDst = false; | 87 bool readsDst = false; |
79 bool readFragPosition = false; | 88 bool readFragPosition = false; |
80 int lastEnabledStage = -1; | |
81 | |
82 for (int s = 0; s < GrDrawState::kNumStages; ++s) { | 89 for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
83 | |
84 bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCove
rage; | 90 bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCove
rage; |
85 if (!skip && drawState.isStageEnabled(s)) { | 91 if (!skip && drawState.isStageEnabled(s)) { |
86 lastEnabledStage = s; | |
87 const GrEffectRef& effect = *drawState.getStage(s).getEffect(); | 92 const GrEffectRef& effect = *drawState.getStage(s).getEffect(); |
88 const GrBackendEffectFactory& factory = effect->getFactory(); | 93 const GrBackendEffectFactory& factory = effect->getFactory(); |
89 GrDrawEffect drawEffect(drawState.getStage(s), requiresLocalCoordAtt
rib); | 94 GrDrawEffect drawEffect(drawState.getStage(s), requiresLocalCoordAtt
rib); |
90 desc->fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps()
); | 95 effectKeys[currEffectKey] = factory.glEffectKey(drawEffect, gpu->glC
aps()); |
| 96 ++currEffectKey; |
91 if (effect->willReadDstColor()) { | 97 if (effect->willReadDstColor()) { |
92 readsDst = true; | 98 readsDst = true; |
93 } | 99 } |
94 if (effect->willReadFragmentPosition()) { | 100 if (effect->willReadFragmentPosition()) { |
95 readFragPosition = true; | 101 readFragPosition = true; |
96 } | 102 } |
97 } else { | |
98 desc->fEffectKeys[s] = 0; | |
99 } | 103 } |
100 } | 104 } |
101 | 105 |
| 106 header->fEmitsPointSize = isPoints; |
| 107 header->fColorFilterXfermode = skipColor ? SkXfermode::kDst_Mode : drawState
.getColorFilterMode(); |
| 108 |
| 109 // Currently the experimental GS will only work with triangle prims (and it
doesn't do anything |
| 110 // other than pass through values from the VS to the FS anyway). |
| 111 #if GR_GL_EXPERIMENTAL_GS |
| 112 #if 0 |
| 113 header->fExperimentalGS = gpu->caps().geometryShaderSupport(); |
| 114 #else |
| 115 header->fExperimentalGS = false; |
| 116 #endif |
| 117 #endif |
| 118 if (colorIsTransBlack) { |
| 119 header->fColorInput = kTransBlack_ColorInput; |
| 120 } else if (colorIsSolidWhite) { |
| 121 header->fColorInput = kSolidWhite_ColorInput; |
| 122 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresColorAttrib) { |
| 123 header->fColorInput = kUniform_ColorInput; |
| 124 } else { |
| 125 header->fColorInput = kAttribute_ColorInput; |
| 126 } |
| 127 |
| 128 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.ge
tCoverage(); |
| 129 |
| 130 if (skipCoverage) { |
| 131 header->fCoverageInput = kTransBlack_ColorInput; |
| 132 } else if (covIsSolidWhite) { |
| 133 header->fCoverageInput = kSolidWhite_ColorInput; |
| 134 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresCoverageAttrib) { |
| 135 header->fCoverageInput = kUniform_ColorInput; |
| 136 } else { |
| 137 header->fCoverageInput = kAttribute_ColorInput; |
| 138 } |
| 139 |
102 if (readsDst) { | 140 if (readsDst) { |
103 GrAssert(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport()); | 141 GrAssert(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport()); |
104 const GrTexture* dstCopyTexture = NULL; | 142 const GrTexture* dstCopyTexture = NULL; |
105 if (NULL != dstCopy) { | 143 if (NULL != dstCopy) { |
106 dstCopyTexture = dstCopy->texture(); | 144 dstCopyTexture = dstCopy->texture(); |
107 } | 145 } |
108 desc->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, gpu
->glCaps()); | 146 header->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, g
pu->glCaps()); |
109 GrAssert(0 != desc->fDstReadKey); | 147 GrAssert(0 != header->fDstReadKey); |
110 } else { | 148 } else { |
111 desc->fDstReadKey = 0; | 149 header->fDstReadKey = 0; |
112 } | 150 } |
113 | 151 |
114 if (readFragPosition) { | 152 if (readFragPosition) { |
115 desc->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(drawState.
getRenderTarget(), | 153 header->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(drawStat
e.getRenderTarget(), |
116 gpu->glCap
s()); | 154 gpu->glCap
s()); |
117 } else { | 155 } else { |
118 desc->fFragPosKey = 0; | 156 header->fFragPosKey = 0; |
119 } | 157 } |
120 | 158 |
121 desc->fCoverageOutput = kModulate_CoverageOutput; | 159 // Record attribute indices |
122 | 160 header->fPositionAttributeIndex = drawState.positionAttributeIndex(); |
123 // Currently the experimental GS will only work with triangle prims (and it
doesn't do anything | 161 header->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex(); |
124 // other than pass through values from the VS to the FS anyway). | 162 |
125 #if GR_GL_EXPERIMENTAL_GS | 163 // For constant color and coverage we need an attribute with an index beyond
those already set |
126 #if 0 | 164 int availableAttributeIndex = drawState.getVertexAttribCount(); |
127 desc->fExperimentalGS = gpu->caps().geometryShaderSupport(); | 165 if (requiresColorAttrib) { |
128 #else | 166 header->fColorAttributeIndex = drawState.colorVertexAttributeIndex(); |
129 desc->fExperimentalGS = false; | 167 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) { |
130 #endif | 168 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
131 #endif | 169 header->fColorAttributeIndex = availableAttributeIndex; |
132 | 170 availableAttributeIndex++; |
133 // We leave this set to kNumStages until we discover that the coverage/color
distinction is | 171 } else { |
134 // material to the generated program. We do this to avoid distinct keys that
generate equivalent | 172 header->fColorAttributeIndex = -1; |
135 // programs. | 173 } |
136 desc->fFirstCoverageStage = GrDrawState::kNumStages; | 174 |
137 // This tracks the actual first coverage stage. | 175 if (requiresCoverageAttrib) { |
138 int firstCoverageStage = GrDrawState::kNumStages; | 176 header->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex
(); |
139 desc->fDiscardIfZeroCoverage = false; // Enabled below if stenciling and the
re is coverage. | 177 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput)
{ |
140 bool hasCoverage = false; | 178 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
141 // If we're rendering coverage-as-color then it's as though there are no cov
erage stages. | 179 header->fCoverageAttributeIndex = availableAttributeIndex; |
142 if (!drawState.isCoverageDrawing()) { | 180 } else { |
143 // We can have coverage either through a stage or coverage vertex attrib
utes. | 181 header->fCoverageAttributeIndex = -1; |
144 if (drawState.getFirstCoverageStage() <= lastEnabledStage) { | |
145 firstCoverageStage = drawState.getFirstCoverageStage(); | |
146 hasCoverage = true; | |
147 } else { | |
148 hasCoverage = requiresCoverageAttrib; | |
149 } | |
150 } | 182 } |
151 | 183 |
152 if (hasCoverage) { | 184 // Here we deal with whether/how we handle color and coverage separately. |
| 185 |
| 186 // Set these defaults and then possibly change our mind if there is coverage
. |
| 187 header->fDiscardIfZeroCoverage = false; |
| 188 header->fCoverageOutput = kModulate_CoverageOutput; |
| 189 |
| 190 // If we do have coverage determine whether it matters. |
| 191 bool separateCoverageFromColor = false; |
| 192 if (!drawState.isCoverageDrawing() && (coverageEffectCnt > 0 || requiresCove
rageAttrib)) { |
153 // color filter is applied between color/coverage computation | 193 // color filter is applied between color/coverage computation |
154 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) { | 194 if (SkXfermode::kDst_Mode != header->fColorFilterXfermode) { |
155 desc->fFirstCoverageStage = firstCoverageStage; | 195 separateCoverageFromColor = true; |
156 } | 196 } |
157 | 197 |
158 // If we're stenciling then we want to discard samples that have zero co
verage | 198 // If we're stenciling then we want to discard samples that have zero co
verage |
159 if (drawState.getStencil().doesWrite()) { | 199 if (drawState.getStencil().doesWrite()) { |
160 desc->fDiscardIfZeroCoverage = true; | 200 header->fDiscardIfZeroCoverage = true; |
161 desc->fFirstCoverageStage = firstCoverageStage; | 201 separateCoverageFromColor = true; |
162 } | 202 } |
163 | 203 |
164 if (gpu->caps()->dualSourceBlendingSupport() && | 204 if (gpu->caps()->dualSourceBlendingSupport() && |
165 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | | 205 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | |
166 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { | 206 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { |
167 if (kZero_GrBlendCoeff == dstCoeff) { | 207 if (kZero_GrBlendCoeff == dstCoeff) { |
168 // write the coverage value to second color | 208 // write the coverage value to second color |
169 desc->fCoverageOutput = kSecondaryCoverage_CoverageOutput; | 209 header->fCoverageOutput = kSecondaryCoverage_CoverageOutput; |
170 desc->fFirstCoverageStage = firstCoverageStage; | 210 separateCoverageFromColor = true; |
171 } else if (kSA_GrBlendCoeff == dstCoeff) { | 211 } else if (kSA_GrBlendCoeff == dstCoeff) { |
172 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. | 212 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. |
173 desc->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput; | 213 header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput; |
174 desc->fFirstCoverageStage = firstCoverageStage; | 214 separateCoverageFromColor = true; |
175 } else if (kSC_GrBlendCoeff == dstCoeff) { | 215 } else if (kSC_GrBlendCoeff == dstCoeff) { |
176 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. | 216 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. |
177 desc->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput; | 217 header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput; |
178 desc->fFirstCoverageStage = firstCoverageStage; | 218 separateCoverageFromColor = true; |
179 } | 219 } |
180 } else if (readsDst && | 220 } else if (readsDst && |
181 kOne_GrBlendCoeff == srcCoeff && | 221 kOne_GrBlendCoeff == srcCoeff && |
182 kZero_GrBlendCoeff == dstCoeff) { | 222 kZero_GrBlendCoeff == dstCoeff) { |
183 desc->fCoverageOutput = kCombineWithDst_CoverageOutput; | 223 header->fCoverageOutput = kCombineWithDst_CoverageOutput; |
184 desc->fFirstCoverageStage = firstCoverageStage; | 224 separateCoverageFromColor = true; |
185 } | 225 } |
186 } | 226 } |
187 | 227 if (separateCoverageFromColor) { |
188 desc->fPositionAttributeIndex = drawState.positionAttributeIndex(); | 228 header->fColorEffectCnt = colorEffectCnt; |
189 desc->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex(); | 229 header->fCoverageEffectCnt = coverageEffectCnt; |
190 | |
191 // For constant color and coverage we need an attribute with an index beyond
those already set | |
192 int availableAttributeIndex = drawState.getVertexAttribCount(); | |
193 if (requiresColorAttrib) { | |
194 desc->fColorAttributeIndex = drawState.colorVertexAttributeIndex(); | |
195 } else if (GrGLProgramDesc::kAttribute_ColorInput == desc->fColorInput) { | |
196 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); | |
197 desc->fColorAttributeIndex = availableAttributeIndex; | |
198 availableAttributeIndex++; | |
199 } else { | 230 } else { |
200 desc->fColorAttributeIndex = -1; | 231 header->fColorEffectCnt = colorEffectCnt + coverageEffectCnt; |
| 232 header->fCoverageEffectCnt = 0; |
201 } | 233 } |
202 | 234 |
203 if (requiresCoverageAttrib) { | 235 *desc->checksum() = 0; |
204 desc->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex()
; | 236 *desc->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(desc->fK
ey.get()), |
205 } else if (GrGLProgramDesc::kAttribute_ColorInput == desc->fCoverageInput) { | 237 newKeyLength); |
206 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); | 238 desc->fInitialized = true; |
207 desc->fCoverageAttributeIndex = availableAttributeIndex; | 239 } |
208 } else { | 240 |
209 desc->fCoverageAttributeIndex = -1; | 241 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
| 242 fInitialized = other.fInitialized; |
| 243 if (fInitialized) { |
| 244 size_t keyLength = other.keyLength(); |
| 245 fKey.reset(keyLength); |
| 246 memcpy(fKey.get(), other.fKey.get(), keyLength); |
210 } | 247 } |
| 248 return *this; |
211 } | 249 } |
| 250 |
OLD | NEW |