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 "gl/builders/GrGLFragmentShaderBuilder.h" | 8 #include "gl/builders/GrGLFragmentShaderBuilder.h" |
9 #include "GrGLProgramDesc.h" | 9 #include "GrGLProgramDesc.h" |
10 #include "GrBackendProcessorFactory.h" | 10 #include "GrBackendProcessorFactory.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 } | 53 } |
54 if (kRGB_GrColorComponentFlags & swizzleComponentMask) { | 54 if (kRGB_GrColorComponentFlags & swizzleComponentMask) { |
55 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our
semantics that | 55 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our
semantics that |
56 // alpha-only textures smear alpha across all four channels when rea
d. | 56 // alpha-only textures smear alpha across all four channels when rea
d. |
57 return true; | 57 return true; |
58 } | 58 } |
59 } | 59 } |
60 return false; | 60 return false; |
61 } | 61 } |
62 | 62 |
63 static uint32_t gen_attrib_key(const GrGeometryProcessor* effect) { | 63 static uint32_t gen_attrib_key(const GrGeometryProcessor& proc) { |
64 uint32_t key = 0; | 64 uint32_t key = 0; |
65 | 65 |
66 const GrGeometryProcessor::VertexAttribArray& vars = effect->getVertexAttrib
s(); | 66 const GrGeometryProcessor::VertexAttribArray& vars = proc.getVertexAttribs()
; |
67 int numAttributes = vars.count(); | 67 int numAttributes = vars.count(); |
68 SkASSERT(numAttributes <= 2); | 68 SkASSERT(numAttributes <= 2); |
69 for (int a = 0; a < numAttributes; ++a) { | 69 for (int a = 0; a < numAttributes; ++a) { |
70 uint32_t value = 1 << a; | 70 uint32_t value = 1 << a; |
71 key |= value; | 71 key |= value; |
72 } | 72 } |
73 return key; | 73 return key; |
74 } | 74 } |
75 | 75 |
76 static uint32_t gen_transform_key(const GrProcessorStage& effectStage, | 76 static uint32_t gen_transform_key(const GrFragmentStage& effectStage, |
77 bool useExplicitLocalCoords) { | 77 bool useExplicitLocalCoords) { |
78 uint32_t totalKey = 0; | 78 uint32_t totalKey = 0; |
79 int numTransforms = effectStage.getProcessor()->numTransforms(); | 79 int numTransforms = effectStage.getProcessor()->numTransforms(); |
80 for (int t = 0; t < numTransforms; ++t) { | 80 for (int t = 0; t < numTransforms; ++t) { |
81 uint32_t key = 0; | 81 uint32_t key = 0; |
82 if (effectStage.isPerspectiveCoordTransform(t, useExplicitLocalCoords))
{ | 82 if (effectStage.isPerspectiveCoordTransform(t, useExplicitLocalCoords))
{ |
83 key |= kGeneral_MatrixType; | 83 key |= kGeneral_MatrixType; |
84 } else { | 84 } else { |
85 key |= kNoPersp_MatrixType; | 85 key |= kNoPersp_MatrixType; |
86 } | 86 } |
87 | 87 |
88 const GrCoordTransform& coordTransform = effectStage.getProcessor()->coo
rdTransform(t); | 88 const GrCoordTransform& coordTransform = effectStage.getProcessor()->coo
rdTransform(t); |
89 if (kLocal_GrCoordSet != coordTransform.sourceCoords() && useExplicitLoc
alCoords) { | 89 if (kLocal_GrCoordSet != coordTransform.sourceCoords() && useExplicitLoc
alCoords) { |
90 key |= kPositionCoords_Flag; | 90 key |= kPositionCoords_Flag; |
91 } | 91 } |
92 key <<= kTransformKeyBits * t; | 92 key <<= kTransformKeyBits * t; |
93 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to
overlap | 93 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to
overlap |
94 totalKey |= key; | 94 totalKey |= key; |
95 } | 95 } |
96 return totalKey; | 96 return totalKey; |
97 } | 97 } |
98 | 98 |
99 static uint32_t gen_texture_key(const GrProcessor* effect, const GrGLCaps& caps)
{ | 99 static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) { |
100 uint32_t key = 0; | 100 uint32_t key = 0; |
101 int numTextures = effect->numTextures(); | 101 int numTextures = proc.numTextures(); |
102 for (int t = 0; t < numTextures; ++t) { | 102 for (int t = 0; t < numTextures; ++t) { |
103 const GrTextureAccess& access = effect->textureAccess(t); | 103 const GrTextureAccess& access = proc.textureAccess(t); |
104 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getText
ure()->config()); | 104 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getText
ure()->config()); |
105 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.s
wizzleMask())) { | 105 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.s
wizzleMask())) { |
106 key |= 1 << t; | 106 key |= 1 << t; |
107 } | 107 } |
108 } | 108 } |
109 return key; | 109 return key; |
110 } | 110 } |
111 | 111 |
112 /** | 112 /** |
113 * A function which emits a meta key into the key builder. This is required bec
ause shader code may | 113 * A function which emits a meta key into the key builder. This is required bec
ause shader code may |
114 * be dependent on properties of the effect that the effect itself doesn't use | 114 * be dependent on properties of the effect that the effect itself doesn't use |
115 * in its key (e.g. the pixel format of textures used). So we create a meta-key
for | 115 * in its key (e.g. the pixel format of textures used). So we create a meta-key
for |
116 * every effect using this function. It is also responsible for inserting the ef
fect's class ID | 116 * every effect using this function. It is also responsible for inserting the ef
fect's class ID |
117 * which must be different for every GrProcessor subclass. It can fail if an eff
ect uses too many | 117 * which must be different for every GrProcessor subclass. It can fail if an eff
ect uses too many |
118 * textures, transforms, etc, for the space allotted in the meta-key. | 118 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, bot
h FPs and GPs share |
| 119 * this function because it is hairy, though FPs do not have attribs, and GPs do
not have transforms |
119 */ | 120 */ |
120 | 121 static bool get_meta_key(const GrProcessor& proc, |
121 static uint32_t* get_processor_meta_key(const GrProcessorStage& processorStage, | 122 const GrGLCaps& caps, |
122 bool useExplicitLocalCoords, | 123 uint32_t transformKey, |
123 const GrGLCaps& caps, | 124 uint32_t attribKey, |
124 GrProcessorKeyBuilder* b) { | 125 GrProcessorKeyBuilder* b, |
125 | 126 uint16_t* processorKeySize) { |
126 uint32_t textureKey = gen_texture_key(processorStage.getProcessor(), caps); | 127 const GrBackendProcessorFactory& factory = proc.getFactory(); |
127 uint32_t transformKey = gen_transform_key(processorStage,useExplicitLocalCoo
rds); | 128 factory.getGLProcessorKey(proc, caps, b); |
128 uint32_t classID = processorStage.getProcessor()->getFactory().effectClassID
(); | |
129 | |
130 // Currently we allow 16 bits for each of the above portions of the meta-key
. Fail if they | |
131 // don't fit. | |
132 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); | |
133 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) { | |
134 return NULL; | |
135 } | |
136 | |
137 uint32_t* key = b->add32n(2); | |
138 key[0] = (textureKey << 16 | transformKey); | |
139 key[1] = (classID << 16); | |
140 return key; | |
141 } | |
142 | |
143 static bool get_fp_key(const GrProcessorStage& stage, | |
144 const GrGLCaps& caps, | |
145 bool useExplicitLocalCoords, | |
146 GrProcessorKeyBuilder* b, | |
147 uint16_t* processorKeySize) { | |
148 const GrProcessor& effect = *stage.getProcessor(); | |
149 const GrBackendProcessorFactory& factory = effect.getFactory(); | |
150 factory.getGLProcessorKey(effect, caps, b); | |
151 size_t size = b->size(); | 129 size_t size = b->size(); |
152 if (size > SK_MaxU16) { | 130 if (size > SK_MaxU16) { |
153 *processorKeySize = 0; // suppresses a warning. | 131 *processorKeySize = 0; // suppresses a warning. |
154 return false; | 132 return false; |
155 } | 133 } |
156 *processorKeySize = SkToU16(size); | 134 *processorKeySize = SkToU16(size); |
157 if (NULL == get_processor_meta_key(stage, useExplicitLocalCoords, caps, b))
{ | 135 uint32_t textureKey = gen_texture_key(proc, caps); |
158 return false; | 136 uint32_t classID = proc.getFactory().effectClassID(); |
159 } | |
160 return true; | |
161 } | |
162 | |
163 static bool get_gp_key(const GrGeometryStage& stage, | |
164 const GrGLCaps& caps, | |
165 bool useExplicitLocalCoords, | |
166 GrProcessorKeyBuilder* b, | |
167 uint16_t* processorKeySize) { | |
168 const GrProcessor& effect = *stage.getProcessor(); | |
169 const GrBackendProcessorFactory& factory = effect.getFactory(); | |
170 factory.getGLProcessorKey(effect, caps, b); | |
171 size_t size = b->size(); | |
172 if (size > SK_MaxU16) { | |
173 *processorKeySize = 0; // suppresses a warning. | |
174 return false; | |
175 } | |
176 *processorKeySize = SkToU16(size); | |
177 uint32_t* key = get_processor_meta_key(stage, useExplicitLocalCoords, caps,
b); | |
178 if (NULL == key) { | |
179 return false; | |
180 } | |
181 uint32_t attribKey = gen_attrib_key(stage.getProcessor()); | |
182 | 137 |
183 // Currently we allow 16 bits for each of the above portions of the meta-key
. Fail if they | 138 // Currently we allow 16 bits for each of the above portions of the meta-key
. Fail if they |
184 // don't fit. | 139 // don't fit. |
185 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); | 140 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
186 if ((attribKey) & kMetaKeyInvalidMask) { | 141 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) { |
187 return false; | 142 return false; |
188 } | 143 } |
189 | 144 |
190 key[1] |= attribKey; | 145 uint32_t* key = b->add32n(2); |
| 146 key[0] = (textureKey << 16 | transformKey); |
| 147 key[1] = (classID << 16); |
191 return true; | 148 return true; |
192 } | 149 } |
193 | 150 |
194 struct GeometryProcessorKeyBuilder { | 151 struct GeometryProcessorKeyBuilder { |
195 typedef GrGeometryStage StagedProcessor; | 152 typedef GrGeometryProcessor StagedProcessor; |
196 static bool GetProcessorKey(const GrGeometryStage& gpStage, | 153 static bool GetProcessorKey(const GrGeometryProcessor& gp, |
197 const GrGLCaps& caps, | 154 const GrGLCaps& caps, |
198 bool requiresLocalCoordAttrib, | 155 bool, |
199 GrProcessorKeyBuilder* b, | 156 GrProcessorKeyBuilder* b, |
200 uint16_t* processorKeySize) { | 157 uint16_t* keySize) { |
201 return get_gp_key(gpStage, caps, requiresLocalCoordAttrib, b, processorK
eySize); | 158 /* 0 because no transforms on a GP */ |
| 159 return get_meta_key(gp, caps, 0, gen_attrib_key(gp), b, keySize); |
202 } | 160 } |
203 }; | 161 }; |
204 | 162 |
205 struct FragmentProcessorKeyBuilder { | 163 struct FragmentProcessorKeyBuilder { |
206 typedef GrFragmentStage StagedProcessor; | 164 typedef GrFragmentStage StagedProcessor; |
207 static bool GetProcessorKey(const GrFragmentStage& fpStage, | 165 static bool GetProcessorKey(const GrFragmentStage& fps, |
208 const GrGLCaps& caps, | 166 const GrGLCaps& caps, |
209 bool requiresLocalCoordAttrib, | 167 bool useLocalCoords, |
210 GrProcessorKeyBuilder* b, | 168 GrProcessorKeyBuilder* b, |
211 uint16_t* processorKeySize) { | 169 uint16_t* keySize) { |
212 return get_fp_key(fpStage, caps, requiresLocalCoordAttrib, b, processorK
eySize); | 170 /* 0 because no attribs on a fP */ |
| 171 return get_meta_key(*fps.getProcessor(), caps, gen_transform_key(fps, us
eLocalCoords), 0, |
| 172 b, keySize); |
213 } | 173 } |
214 }; | 174 }; |
215 | 175 |
216 | 176 |
217 template <class ProcessorKeyBuilder> | 177 template <class ProcessorKeyBuilder> |
218 bool | 178 bool |
219 GrGLProgramDesc::BuildStagedProcessorKey(const typename ProcessorKeyBuilder::Sta
gedProcessor& stage, | 179 GrGLProgramDesc::BuildStagedProcessorKey(const typename ProcessorKeyBuilder::Sta
gedProcessor& stage, |
220 const GrGLCaps& caps, | 180 const GrGLCaps& caps, |
221 bool requiresLocalCoordAttrib, | 181 bool requiresLocalCoordAttrib, |
222 GrGLProgramDesc* desc, | 182 GrGLProgramDesc* desc, |
(...skipping 12 matching lines...) Expand all Loading... |
235 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAn
dLengthOffset + | 195 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAn
dLengthOffset + |
236 *offsetAndSizeIndex * 2 * sizeof(uint16_
t)); | 196 *offsetAndSizeIndex * 2 * sizeof(uint16_
t)); |
237 offsetAndSize[0] = SkToU16(processorOffset); | 197 offsetAndSize[0] = SkToU16(processorOffset); |
238 offsetAndSize[1] = processorKeySize; | 198 offsetAndSize[1] = processorKeySize; |
239 ++(*offsetAndSizeIndex); | 199 ++(*offsetAndSizeIndex); |
240 return true; | 200 return true; |
241 } | 201 } |
242 | 202 |
243 bool GrGLProgramDesc::Build(const GrOptDrawState& optState, | 203 bool GrGLProgramDesc::Build(const GrOptDrawState& optState, |
244 GrGpu::DrawType drawType, | 204 GrGpu::DrawType drawType, |
245 GrBlendCoeff srcCoeff, | |
246 GrBlendCoeff dstCoeff, | |
247 GrGpuGL* gpu, | 205 GrGpuGL* gpu, |
248 const GrDeviceCoordTexture* dstCopy, | 206 const GrDeviceCoordTexture* dstCopy, |
249 const GrGeometryStage** geometryProcessor, | |
250 SkTArray<const GrFragmentStage*, true>* colorStages, | |
251 SkTArray<const GrFragmentStage*, true>* coverageStag
es, | |
252 GrGLProgramDesc* desc) { | 207 GrGLProgramDesc* desc) { |
253 colorStages->reset(); | |
254 coverageStages->reset(); | |
255 | |
256 bool inputColorIsUsed = optState.inputColorIsUsed(); | 208 bool inputColorIsUsed = optState.inputColorIsUsed(); |
257 bool inputCoverageIsUsed = optState.inputCoverageIsUsed(); | 209 bool inputCoverageIsUsed = optState.inputCoverageIsUsed(); |
258 | 210 |
259 // The descriptor is used as a cache key. Thus when a field of the | 211 // The descriptor is used as a cache key. Thus when a field of the |
260 // descriptor will not affect program generation (because of the attribute | 212 // descriptor will not affect program generation (because of the attribute |
261 // bindings in use or other descriptor field settings) it should be set | 213 // bindings in use or other descriptor field settings) it should be set |
262 // to a canonical value to avoid duplicate programs with different keys. | 214 // to a canonical value to avoid duplicate programs with different keys. |
263 | 215 |
264 bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib(); | 216 bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib(); |
265 | 217 |
266 int numStages = optState.numTotalStages(); | 218 int numStages = optState.numTotalStages(); |
267 | 219 |
268 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t)); | 220 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t)); |
269 // Make room for everything up to and including the array of offsets to effe
ct keys. | 221 // Make room for everything up to and including the array of offsets to effe
ct keys. |
270 desc->fKey.reset(); | 222 desc->fKey.reset(); |
271 desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_
t) * numStages); | 223 desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_
t) * numStages); |
272 | 224 |
273 int offsetAndSizeIndex = 0; | 225 int offsetAndSizeIndex = 0; |
274 | 226 |
275 // We can only have one effect which touches the vertex shader | 227 // We can only have one effect which touches the vertex shader |
276 if (optState.hasGeometryProcessor()) { | 228 if (optState.hasGeometryProcessor()) { |
277 const GrGeometryStage& gpStage = *optState.getGeometryProcessor(); | 229 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(*optState.getG
eometryProcessor(), |
278 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(gpStage, | |
279 gpu->glCaps(), | 230 gpu->glCaps(), |
280 requiresLocalC
oordAttrib, | 231 false, |
281 desc, | |
282 &offsetAndSize
Index)) { | |
283 return false; | |
284 } | |
285 *geometryProcessor = &gpStage; | |
286 } | |
287 | |
288 for (int s = 0; s < optState.numColorStages(); ++s) { | |
289 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getCo
lorStage(s), | |
290 gpu->glCaps(), | |
291 requiresLocalC
oordAttrib, | |
292 desc, | 232 desc, |
293 &offsetAndSize
Index)) { | 233 &offsetAndSize
Index)) { |
294 return false; | 234 return false; |
295 } | 235 } |
296 } | 236 } |
297 | 237 |
298 for (int s = 0; s < optState.numCoverageStages(); ++s) { | 238 for (int s = 0; s < optState.numFragmentStages(); ++s) { |
299 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getCo
verageStage(s), | 239 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getFr
agmentStage(s), |
300 gpu->glCaps(), | 240 gpu->glCaps(), |
301 requiresLocalC
oordAttrib, | 241 requiresLocalC
oordAttrib, |
302 desc, | 242 desc, |
303 &offsetAndSize
Index)) { | 243 &offsetAndSize
Index)) { |
304 return false; | 244 return false; |
305 } | 245 } |
306 } | 246 } |
307 | 247 |
308 // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------
------------------- | 248 // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------
------------------- |
309 // Because header is a pointer into the dynamic array, we can't push any new
data into the key | 249 // Because header is a pointer into the dynamic array, we can't push any new
data into the key |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput)
{ | 332 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput)
{ |
393 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); | 333 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); |
394 header->fCoverageAttributeIndex = availableAttributeIndex; | 334 header->fCoverageAttributeIndex = availableAttributeIndex; |
395 } else { | 335 } else { |
396 header->fCoverageAttributeIndex = -1; | 336 header->fCoverageAttributeIndex = -1; |
397 } | 337 } |
398 | 338 |
399 header->fPrimaryOutputType = optState.getPrimaryOutputType(); | 339 header->fPrimaryOutputType = optState.getPrimaryOutputType(); |
400 header->fSecondaryOutputType = optState.getSecondaryOutputType(); | 340 header->fSecondaryOutputType = optState.getSecondaryOutputType(); |
401 | 341 |
402 for (int s = 0; s < optState.numColorStages(); ++s) { | 342 header->fColorEffectCnt = optState.numColorStages(); |
403 colorStages->push_back(&optState.getColorStage(s)); | 343 header->fCoverageEffectCnt = optState.numCoverageStages(); |
404 } | |
405 for (int s = 0; s < optState.numCoverageStages(); ++s) { | |
406 coverageStages->push_back(&optState.getCoverageStage(s)); | |
407 } | |
408 | |
409 header->fColorEffectCnt = colorStages->count(); | |
410 header->fCoverageEffectCnt = coverageStages->count(); | |
411 desc->finalize(); | 344 desc->finalize(); |
412 return true; | 345 return true; |
413 } | 346 } |
414 | 347 |
415 void GrGLProgramDesc::finalize() { | 348 void GrGLProgramDesc::finalize() { |
416 int keyLength = fKey.count(); | 349 int keyLength = fKey.count(); |
417 SkASSERT(0 == (keyLength % 4)); | 350 SkASSERT(0 == (keyLength % 4)); |
418 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength); | 351 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength); |
419 | 352 |
420 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>(); | 353 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>(); |
421 *checksum = 0; | 354 *checksum = 0; |
422 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), k
eyLength); | 355 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), k
eyLength); |
423 } | 356 } |
424 | 357 |
425 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { | 358 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
426 size_t keyLength = other.keyLength(); | 359 size_t keyLength = other.keyLength(); |
427 fKey.reset(keyLength); | 360 fKey.reset(keyLength); |
428 memcpy(fKey.begin(), other.fKey.begin(), keyLength); | 361 memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
429 return *this; | 362 return *this; |
430 } | 363 } |
OLD | NEW |