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 void* oldKey = desc->fKey.get(); | |
75 void* newKey = desc->fKey.reset(newKeyLength); | |
robertphillips
2013/05/21 19:22:37
Does oldKey survive reset?
bsalomon
2013/05/22 14:11:25
I just wanted to check whether reset() actually al
| |
76 if (oldKey != newKey || !desc->fInitialized) { | |
77 // make sure any padding in the header is zero if we malloc'ed or if we haven't used this | |
78 // allocation before. | |
79 memset(desc->header(), 0, kHeaderSize); | |
80 } | |
81 // write the key length | |
robertphillips
2013/05/21 19:22:37
reset doesn't do this?
bsalomon
2013/05/22 14:11:25
reset() was called on an SkAutoSMalloc.
| |
82 *desc->atOffset<uint32_t, kLengthOffset>() = newKeyLength; | |
67 | 83 |
68 if (skipCoverage) { | 84 KeyHeader* header = desc->header(); |
69 desc->fCoverageInput = kTransBlack_ColorInput; | 85 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 | 86 |
87 int currEffectKey = 0; | |
78 bool readsDst = false; | 88 bool readsDst = false; |
79 bool readFragPosition = false; | 89 bool readFragPosition = false; |
80 int lastEnabledStage = -1; | |
81 | |
82 for (int s = 0; s < GrDrawState::kNumStages; ++s) { | 90 for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
83 | |
84 bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCove rage; | 91 bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCove rage; |
85 if (!skip && drawState.isStageEnabled(s)) { | 92 if (!skip && drawState.isStageEnabled(s)) { |
86 lastEnabledStage = s; | |
87 const GrEffectRef& effect = *drawState.getStage(s).getEffect(); | 93 const GrEffectRef& effect = *drawState.getStage(s).getEffect(); |
88 const GrBackendEffectFactory& factory = effect->getFactory(); | 94 const GrBackendEffectFactory& factory = effect->getFactory(); |
89 GrDrawEffect drawEffect(drawState.getStage(s), requiresLocalCoordAtt rib); | 95 GrDrawEffect drawEffect(drawState.getStage(s), requiresLocalCoordAtt rib); |
90 desc->fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps() ); | 96 effectKeys[currEffectKey] = factory.glEffectKey(drawEffect, gpu->glC aps()); |
97 ++currEffectKey; | |
91 if (effect->willReadDstColor()) { | 98 if (effect->willReadDstColor()) { |
92 readsDst = true; | 99 readsDst = true; |
93 } | 100 } |
94 if (effect->willReadFragmentPosition()) { | 101 if (effect->willReadFragmentPosition()) { |
95 readFragPosition = true; | 102 readFragPosition = true; |
96 } | 103 } |
97 } else { | |
98 desc->fEffectKeys[s] = 0; | |
99 } | 104 } |
100 } | 105 } |
101 | 106 |
107 header->fEmitsPointSize = isPoints; | |
108 header->fColorFilterXfermode = skipColor ? SkXfermode::kDst_Mode : drawState .getColorFilterMode(); | |
109 | |
110 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything | |
111 // other than pass through values from the VS to the FS anyway). | |
112 #if GR_GL_EXPERIMENTAL_GS | |
113 #if 0 | |
114 header->fExperimentalGS = gpu->caps().geometryShaderSupport(); | |
115 #else | |
116 header->fExperimentalGS = false; | |
117 #endif | |
118 #endif | |
119 if (colorIsTransBlack) { | |
120 header->fColorInput = kTransBlack_ColorInput; | |
121 } else if (colorIsSolidWhite) { | |
122 header->fColorInput = kSolidWhite_ColorInput; | |
123 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresColorAttrib) { | |
124 header->fColorInput = kUniform_ColorInput; | |
125 } else { | |
126 header->fColorInput = kAttribute_ColorInput; | |
127 } | |
128 | |
129 bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.ge tCoverage(); | |
130 | |
131 if (skipCoverage) { | |
132 header->fCoverageInput = kTransBlack_ColorInput; | |
133 } else if (covIsSolidWhite) { | |
134 header->fCoverageInput = kSolidWhite_ColorInput; | |
135 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresCoverageAttrib) { | |
136 header->fCoverageInput = kUniform_ColorInput; | |
137 } else { | |
138 header->fCoverageInput = kAttribute_ColorInput; | |
139 } | |
140 | |
102 if (readsDst) { | 141 if (readsDst) { |
103 GrAssert(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport()); | 142 GrAssert(NULL != dstCopy || gpu->caps()->dstReadInShaderSupport()); |
104 const GrTexture* dstCopyTexture = NULL; | 143 const GrTexture* dstCopyTexture = NULL; |
105 if (NULL != dstCopy) { | 144 if (NULL != dstCopy) { |
106 dstCopyTexture = dstCopy->texture(); | 145 dstCopyTexture = dstCopy->texture(); |
107 } | 146 } |
108 desc->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, gpu ->glCaps()); | 147 header->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, g pu->glCaps()); |
109 GrAssert(0 != desc->fDstReadKey); | 148 GrAssert(0 != header->fDstReadKey); |
110 } else { | 149 } else { |
111 desc->fDstReadKey = 0; | 150 header->fDstReadKey = 0; |
112 } | 151 } |
113 | 152 |
114 if (readFragPosition) { | 153 if (readFragPosition) { |
115 desc->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(drawState. getRenderTarget(), | 154 header->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(drawStat e.getRenderTarget(), |
116 gpu->glCap s()); | 155 gpu->glCap s()); |
117 } else { | 156 } else { |
118 desc->fFragPosKey = 0; | 157 header->fFragPosKey = 0; |
119 } | 158 } |
120 | 159 |
121 desc->fCoverageOutput = kModulate_CoverageOutput; | 160 // Record attribute indices |
122 | 161 header->fPositionAttributeIndex = drawState.positionAttributeIndex(); |
123 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything | 162 header->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex(); |
124 // other than pass through values from the VS to the FS anyway). | 163 |
125 #if GR_GL_EXPERIMENTAL_GS | 164 // For constant color and coverage we need an attribute with an index beyond those already set |
126 #if 0 | 165 int availableAttributeIndex = drawState.getVertexAttribCount(); |
127 desc->fExperimentalGS = gpu->caps().geometryShaderSupport(); | 166 if (requiresColorAttrib) { |
128 #else | 167 header->fColorAttributeIndex = drawState.colorVertexAttributeIndex(); |
129 desc->fExperimentalGS = false; | 168 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) { |
130 #endif | 169 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
131 #endif | 170 header->fColorAttributeIndex = availableAttributeIndex; |
132 | 171 availableAttributeIndex++; |
133 // We leave this set to kNumStages until we discover that the coverage/color distinction is | 172 } else { |
134 // material to the generated program. We do this to avoid distinct keys that generate equivalent | 173 header->fColorAttributeIndex = -1; |
135 // programs. | 174 } |
136 desc->fFirstCoverageStage = GrDrawState::kNumStages; | 175 |
137 // This tracks the actual first coverage stage. | 176 if (requiresCoverageAttrib) { |
138 int firstCoverageStage = GrDrawState::kNumStages; | 177 header->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex (); |
139 desc->fDiscardIfZeroCoverage = false; // Enabled below if stenciling and the re is coverage. | 178 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) { |
140 bool hasCoverage = false; | 179 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
141 // If we're rendering coverage-as-color then it's as though there are no cov erage stages. | 180 header->fCoverageAttributeIndex = availableAttributeIndex; |
142 if (!drawState.isCoverageDrawing()) { | 181 } else { |
143 // We can have coverage either through a stage or coverage vertex attrib utes. | 182 header->fCoverageAttributeIndex = -1; |
144 if (drawState.getFirstCoverageStage() <= lastEnabledStage) { | |
145 firstCoverageStage = drawState.getFirstCoverageStage(); | |
146 hasCoverage = true; | |
147 } else { | |
148 hasCoverage = requiresCoverageAttrib; | |
149 } | |
150 } | 183 } |
151 | 184 |
152 if (hasCoverage) { | 185 // Here we deal with whether/how we handle color and coverage separately. |
186 | |
187 // Set these defaults and then possibly change our mind if there is coverage . | |
188 header->fDiscardIfZeroCoverage = false; | |
189 header->fCoverageOutput = kModulate_CoverageOutput; | |
190 | |
191 // If we do have coverage determine whether it matters. | |
192 bool separateCoverageFromColor = false; | |
193 if (!drawState.isCoverageDrawing() && (coverageEffectCnt > 0 || requiresCove rageAttrib)) { | |
153 // color filter is applied between color/coverage computation | 194 // color filter is applied between color/coverage computation |
154 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) { | 195 if (SkXfermode::kDst_Mode != header->fColorFilterXfermode) { |
155 desc->fFirstCoverageStage = firstCoverageStage; | 196 separateCoverageFromColor = true; |
156 } | 197 } |
157 | 198 |
158 // If we're stenciling then we want to discard samples that have zero co verage | 199 // If we're stenciling then we want to discard samples that have zero co verage |
159 if (drawState.getStencil().doesWrite()) { | 200 if (drawState.getStencil().doesWrite()) { |
160 desc->fDiscardIfZeroCoverage = true; | 201 header->fDiscardIfZeroCoverage = true; |
161 desc->fFirstCoverageStage = firstCoverageStage; | 202 separateCoverageFromColor = true; |
162 } | 203 } |
163 | 204 |
164 if (gpu->caps()->dualSourceBlendingSupport() && | 205 if (gpu->caps()->dualSourceBlendingSupport() && |
165 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | | 206 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | |
166 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { | 207 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { |
167 if (kZero_GrBlendCoeff == dstCoeff) { | 208 if (kZero_GrBlendCoeff == dstCoeff) { |
168 // write the coverage value to second color | 209 // write the coverage value to second color |
169 desc->fCoverageOutput = kSecondaryCoverage_CoverageOutput; | 210 header->fCoverageOutput = kSecondaryCoverage_CoverageOutput; |
170 desc->fFirstCoverageStage = firstCoverageStage; | 211 separateCoverageFromColor = true; |
171 } else if (kSA_GrBlendCoeff == dstCoeff) { | 212 } else if (kSA_GrBlendCoeff == dstCoeff) { |
172 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. | 213 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
173 desc->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput; | 214 header->fCoverageOutput = kSecondaryCoverageISA_CoverageOutput; |
174 desc->fFirstCoverageStage = firstCoverageStage; | 215 separateCoverageFromColor = true; |
175 } else if (kSC_GrBlendCoeff == dstCoeff) { | 216 } else if (kSC_GrBlendCoeff == dstCoeff) { |
176 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. | 217 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
177 desc->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput; | 218 header->fCoverageOutput = kSecondaryCoverageISC_CoverageOutput; |
178 desc->fFirstCoverageStage = firstCoverageStage; | 219 separateCoverageFromColor = true; |
179 } | 220 } |
180 } else if (readsDst && | 221 } else if (readsDst && |
181 kOne_GrBlendCoeff == srcCoeff && | 222 kOne_GrBlendCoeff == srcCoeff && |
182 kZero_GrBlendCoeff == dstCoeff) { | 223 kZero_GrBlendCoeff == dstCoeff) { |
183 desc->fCoverageOutput = kCombineWithDst_CoverageOutput; | 224 header->fCoverageOutput = kCombineWithDst_CoverageOutput; |
184 desc->fFirstCoverageStage = firstCoverageStage; | 225 separateCoverageFromColor = true; |
185 } | 226 } |
186 } | 227 } |
187 | 228 if (separateCoverageFromColor) { |
188 desc->fPositionAttributeIndex = drawState.positionAttributeIndex(); | 229 header->fColorEffectCnt = colorEffectCnt; |
189 desc->fLocalCoordAttributeIndex = drawState.localCoordAttributeIndex(); | 230 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 { | 231 } else { |
200 desc->fColorAttributeIndex = -1; | 232 header->fColorEffectCnt = colorEffectCnt + coverageEffectCnt; |
233 header->fCoverageEffectCnt = 0; | |
201 } | 234 } |
202 | 235 |
robertphillips
2013/05/21 19:22:37
Do we need the first clear to 0?
bsalomon
2013/05/22 14:11:25
Yeah, the memory where the checksum will live is a
| |
203 if (requiresCoverageAttrib) { | 236 *desc->checksum() = 0; |
204 desc->fCoverageAttributeIndex = drawState.coverageVertexAttributeIndex() ; | 237 *desc->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(desc->fK ey.get()), |
205 } else if (GrGLProgramDesc::kAttribute_ColorInput == desc->fCoverageInput) { | 238 newKeyLength); |
206 GrAssert(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); | 239 desc->fInitialized = true; |
207 desc->fCoverageAttributeIndex = availableAttributeIndex; | 240 } |
208 } else { | 241 |
209 desc->fCoverageAttributeIndex = -1; | 242 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
243 fInitialized = other.fInitialized; | |
244 if (fInitialized) { | |
245 size_t keyLength = other.keyLength(); | |
246 fKey.reset(keyLength); | |
247 memcpy(fKey.get(), other.fKey.get(), keyLength); | |
210 } | 248 } |
249 return *this; | |
211 } | 250 } |
251 | |
OLD | NEW |