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" | 15 #include "SkChecksum.h" |
16 | 16 |
17 namespace { | 17 static inline bool get_key_and_update_stats(const GrEffectStage& stage, |
18 inline GrGLEffect::EffectKey get_key_and_update_stats(const GrEffectStage& stage
, | 18 const GrGLCaps& caps, |
19 const GrGLCaps& caps, | 19 bool useExplicitLocalCoords, |
20 bool useExplicitLocalCoord
s, | 20 GrEffectKeyBuilder* b, |
21 bool* setTrueIfReadsDst, | 21 bool* setTrueIfReadsDst, |
22 bool* setTrueIfReadsPos, | 22 bool* setTrueIfReadsPos, |
23 bool* setTrueIfHasVertexCo
de) { | 23 bool* setTrueIfHasVertexCode) { |
24 const GrEffect* effect = stage.getEffect(); | 24 const GrBackendEffectFactory& factory = stage.getEffect()->getFactory(); |
25 const GrBackendEffectFactory& factory = effect->getFactory(); | |
26 GrDrawEffect drawEffect(stage, useExplicitLocalCoords); | 25 GrDrawEffect drawEffect(stage, useExplicitLocalCoords); |
27 if (effect->willReadDstColor()) { | 26 if (stage.getEffect()->willReadDstColor()) { |
28 *setTrueIfReadsDst = true; | 27 *setTrueIfReadsDst = true; |
29 } | 28 } |
30 if (effect->willReadFragmentPosition()) { | 29 if (stage.getEffect()->willReadFragmentPosition()) { |
31 *setTrueIfReadsPos = true; | 30 *setTrueIfReadsPos = true; |
32 } | 31 } |
33 if (effect->hasVertexCode()) { | 32 if (stage.getEffect()->hasVertexCode()) { |
34 *setTrueIfHasVertexCode = true; | 33 *setTrueIfHasVertexCode = true; |
35 } | 34 } |
36 return factory.glEffectKey(drawEffect, caps); | 35 return factory.getGLEffectKey(drawEffect, caps, b); |
37 } | 36 } |
38 } | 37 |
39 void GrGLProgramDesc::Build(const GrDrawState& drawState, | 38 bool GrGLProgramDesc::Build(const GrDrawState& drawState, |
40 GrGpu::DrawType drawType, | 39 GrGpu::DrawType drawType, |
41 GrDrawState::BlendOptFlags blendOpts, | 40 GrDrawState::BlendOptFlags blendOpts, |
42 GrBlendCoeff srcCoeff, | 41 GrBlendCoeff srcCoeff, |
43 GrBlendCoeff dstCoeff, | 42 GrBlendCoeff dstCoeff, |
44 const GrGpuGL* gpu, | 43 const GrGpuGL* gpu, |
45 const GrDeviceCoordTexture* dstCopy, | 44 const GrDeviceCoordTexture* dstCopy, |
46 SkTArray<const GrEffectStage*, true>* colorStages, | 45 SkTArray<const GrEffectStage*, true>* colorStages, |
47 SkTArray<const GrEffectStage*, true>* coverageStages
, | 46 SkTArray<const GrEffectStage*, true>* coverageStages
, |
48 GrGLProgramDesc* desc) { | 47 GrGLProgramDesc* desc) { |
49 colorStages->reset(); | 48 colorStages->reset(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAt
tribute(); | 86 bool requiresCoverageAttrib = !skipCoverage && drawState.hasCoverageVertexAt
tribute(); |
88 // we only need the local coords if we're actually going to generate effect
code | 87 // we only need the local coords if we're actually going to generate effect
code |
89 bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) && | 88 bool requiresLocalCoordAttrib = !(skipCoverage && skipColor) && |
90 drawState.hasLocalCoordAttribute(); | 89 drawState.hasLocalCoordAttribute(); |
91 | 90 |
92 bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_B
lendOptFlag); | 91 bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_B
lendOptFlag); |
93 bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFla
g) || | 92 bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFla
g) || |
94 (!requiresColorAttrib && 0xffffffff == drawState.ge
tColor()) || | 93 (!requiresColorAttrib && 0xffffffff == drawState.ge
tColor()) || |
95 (!inputColorIsUsed); | 94 (!inputColorIsUsed); |
96 | 95 |
97 int numEffects = (skipColor ? 0 : (drawState.numColorStages() - firstEffecti
veColorStage)) + | |
98 (skipCoverage ? 0 : (drawState.numCoverageStages() - firstE
ffectiveCoverageStage)); | |
99 | |
100 size_t newKeyLength = KeyLength(numEffects); | |
101 bool allocChanged; | |
102 desc->fKey.reset(newKeyLength, SkAutoMalloc::kAlloc_OnShrink, &allocChanged)
; | |
103 if (allocChanged || !desc->fInitialized) { | |
104 // make sure any padding in the header is zero if we we haven't used thi
s allocation before. | |
105 memset(desc->header(), 0, kHeaderSize); | |
106 } | |
107 // write the key length | |
108 *desc->atOffset<uint32_t, kLengthOffset>() = SkToU32(newKeyLength); | |
109 | |
110 KeyHeader* header = desc->header(); | |
111 EffectKey* effectKeys = desc->effectKeys(); | |
112 | |
113 int currEffectKey = 0; | |
114 bool readsDst = false; | 96 bool readsDst = false; |
115 bool readFragPosition = false; | 97 bool readFragPosition = false; |
116 // We use vertexshader-less shader programs only when drawing paths. | 98 // We use vertexshader-less shader programs only when drawing paths. |
117 bool hasVertexCode = !(GrGpu::kDrawPath_DrawType == drawType || | 99 bool hasVertexCode = !(GrGpu::kDrawPath_DrawType == drawType || |
118 GrGpu::kDrawPaths_DrawType == drawType); | 100 GrGpu::kDrawPaths_DrawType == drawType); |
| 101 int numStages = 0; |
| 102 if (!skipColor) { |
| 103 numStages += drawState.numColorStages() - firstEffectiveColorStage; |
| 104 } |
| 105 if (!skipCoverage) { |
| 106 numStages += drawState.numCoverageStages() - firstEffectiveCoverageStage
; |
| 107 } |
| 108 GR_STATIC_ASSERT(0 == kEffectKeyLengthsOffset % sizeof(uint32_t)); |
| 109 // Make room for everything up to and including the array of offsets to effe
ct keys. |
| 110 desc->fKey.reset(); |
| 111 desc->fKey.push_back_n(kEffectKeyLengthsOffset + sizeof(uint32_t) * numStage
s); |
119 | 112 |
| 113 size_t offset = desc->fKey.count(); |
| 114 int offsetIndex = 0; |
| 115 |
| 116 bool effectKeySuccess = true; |
120 if (!skipColor) { | 117 if (!skipColor) { |
121 for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); +
+s) { | 118 for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); +
+s) { |
122 effectKeys[currEffectKey++] = | 119 uint32_t* offsetLocation = reinterpret_cast<uint32_t*>(desc->fKey.be
gin() + |
123 get_key_and_update_stats(drawState.getColorStage(s), gpu->glCaps
(), | 120 kEffectKeyLen
gthsOffset + |
124 requiresLocalCoordAttrib, &readsDst, &r
eadFragPosition, | 121 offsetIndex *
sizeof(uint32_t)); |
125 &hasVertexCode); | 122 *offsetLocation = offset; |
| 123 ++offsetIndex; |
| 124 |
| 125 GrEffectKeyBuilder b(&desc->fKey); |
| 126 effectKeySuccess |= get_key_and_update_stats(drawState.getColorStage
(s), gpu->glCaps(), |
| 127 requiresLocalCoordAttri
b, &b, &readsDst, |
| 128 &readFragPosition, &has
VertexCode); |
| 129 offset += b.size(); |
126 } | 130 } |
127 } | 131 } |
128 if (!skipCoverage) { | 132 if (!skipCoverage) { |
129 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStage
s(); ++s) { | 133 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStage
s(); ++s) { |
130 effectKeys[currEffectKey++] = | 134 uint32_t* offsetLocation = reinterpret_cast<uint32_t*>(desc->fKey.be
gin() + |
131 get_key_and_update_stats(drawState.getCoverageStage(s), gpu->glC
aps(), | 135 kEffectKeyLen
gthsOffset + |
132 requiresLocalCoordAttrib, &readsDst, &r
eadFragPosition, | 136 offsetIndex *
sizeof(uint32_t)); |
133 &hasVertexCode); | 137 *offsetLocation = offset; |
| 138 ++offsetIndex; |
| 139 GrEffectKeyBuilder b(&desc->fKey); |
| 140 effectKeySuccess |= get_key_and_update_stats(drawState.getCoverageSt
age(s), |
| 141 gpu->glCaps(), requires
LocalCoordAttrib, |
| 142 &b, &readsDst, &readFra
gPosition, |
| 143 &hasVertexCode); |
| 144 offset += b.size(); |
134 } | 145 } |
135 } | 146 } |
| 147 if (!effectKeySuccess) { |
| 148 desc->fKey.reset(); |
| 149 return false; |
| 150 } |
| 151 |
| 152 KeyHeader* header = desc->header(); |
| 153 // make sure any padding in the header is zeroed. |
| 154 memset(desc->header(), 0, kHeaderSize); |
| 155 |
| 156 // Because header is a pointer into the dynamic array, we can't push any new
data into the key |
| 157 // below here. |
136 | 158 |
137 header->fHasVertexCode = hasVertexCode || requiresLocalCoordAttrib; | 159 header->fHasVertexCode = hasVertexCode || requiresLocalCoordAttrib; |
138 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType; | 160 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType; |
139 | 161 |
140 // Currently the experimental GS will only work with triangle prims (and it
doesn't do anything | 162 // Currently the experimental GS will only work with triangle prims (and it
doesn't do anything |
141 // other than pass through values from the VS to the FS anyway). | 163 // other than pass through values from the VS to the FS anyway). |
142 #if GR_GL_EXPERIMENTAL_GS | 164 #if GR_GL_EXPERIMENTAL_GS |
143 #if 0 | 165 #if 0 |
144 header->fExperimentalGS = gpu->caps().geometryShaderSupport(); | 166 header->fExperimentalGS = gpu->caps().geometryShaderSupport(); |
145 #else | 167 #else |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } else { | 283 } else { |
262 array = colorStages; | 284 array = colorStages; |
263 } | 285 } |
264 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStage
s(); ++s) { | 286 for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStage
s(); ++s) { |
265 array->push_back(&drawState.getCoverageStage(s)); | 287 array->push_back(&drawState.getCoverageStage(s)); |
266 } | 288 } |
267 } | 289 } |
268 header->fColorEffectCnt = colorStages->count(); | 290 header->fColorEffectCnt = colorStages->count(); |
269 header->fCoverageEffectCnt = coverageStages->count(); | 291 header->fCoverageEffectCnt = coverageStages->count(); |
270 | 292 |
271 *desc->checksum() = 0; | 293 desc->finalize(); |
272 *desc->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(desc->fK
ey.get()), | 294 return true; |
273 newKeyLength); | 295 } |
274 desc->fInitialized = true; | 296 |
| 297 void GrGLProgramDesc::finalize() { |
| 298 int keyLength = fKey.count(); |
| 299 SkASSERT(0 == (keyLength % 4)); |
| 300 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength); |
| 301 |
| 302 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>(); |
| 303 *checksum = 0; |
| 304 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), k
eyLength); |
275 } | 305 } |
276 | 306 |
277 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { | 307 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
278 fInitialized = other.fInitialized; | 308 size_t keyLength = other.keyLength(); |
279 if (fInitialized) { | 309 fKey.reset(keyLength); |
280 size_t keyLength = other.keyLength(); | 310 memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
281 fKey.reset(keyLength); | |
282 memcpy(fKey.get(), other.fKey.get(), keyLength); | |
283 } | |
284 return *this; | 311 return *this; |
285 } | 312 } |
OLD | NEW |