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 "GrGLProgramEffects.h" | 8 #include "GrGLProgramEffects.h" |
9 #include "GrDrawEffect.h" | 9 #include "GrDrawEffect.h" |
10 #include "gl/GrGLEffect.h" | 10 #include "gl/GrGLEffect.h" |
11 #include "gl/GrGLShaderBuilder.h" | 11 #include "gl/GrGLShaderBuilder.h" |
| 12 #include "gl/GrGLVertexEffect.h" |
12 #include "gl/GrGpuGL.h" | 13 #include "gl/GrGpuGL.h" |
13 | 14 |
14 typedef GrGLProgramEffects::EffectKey EffectKey; | 15 typedef GrGLProgramEffects::EffectKey EffectKey; |
15 typedef GrGLProgramEffects::TransformedCoords TransformedCoords; | 16 typedef GrGLProgramEffects::TransformedCoords TransformedCoords; |
16 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; | 17 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; |
17 typedef GrGLProgramEffects::TextureSampler TextureSampler; | 18 typedef GrGLProgramEffects::TextureSampler TextureSampler; |
18 typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray; | 19 typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray; |
19 | 20 |
20 /** | 21 /** |
21 * We specialize the vertex code for each of these matrix types. | 22 * We specialize the vertex code for each of these matrix types. |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 SkTArray<Sampler, true>& samplers = fSamplers[e]; | 151 SkTArray<Sampler, true>& samplers = fSamplers[e]; |
151 int numSamplers = samplers.count(); | 152 int numSamplers = samplers.count(); |
152 for (int s = 0; s < numSamplers; ++s) { | 153 for (int s = 0; s < numSamplers; ++s) { |
153 SkASSERT(samplers[s].fUniform.isValid()); | 154 SkASSERT(samplers[s].fUniform.isValid()); |
154 uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx); | 155 uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx); |
155 samplers[s].fTextureUnit = (*texUnitIdx)++; | 156 samplers[s].fTextureUnit = (*texUnitIdx)++; |
156 } | 157 } |
157 } | 158 } |
158 } | 159 } |
159 | 160 |
160 void GrGLProgramEffects::setData(GrGpuGL* gpu, | 161 void GrGLVertexProgramEffects::setData(GrGpuGL* gpu, |
161 const GrGLUniformManager& uniformManager, | 162 const GrGLUniformManager& uniformManager, |
162 const GrEffectStage* effectStages[]) { | 163 const GrEffectStage* effectStages[]) { |
163 int numEffects = fGLEffects.count(); | 164 int numEffects = fGLEffects.count(); |
164 SkASSERT(numEffects == fTransforms.count()); | 165 SkASSERT(numEffects == fTransforms.count()); |
165 SkASSERT(numEffects == fSamplers.count()); | 166 SkASSERT(numEffects == fSamplers.count()); |
166 for (int e = 0; e < numEffects; ++e) { | 167 for (int e = 0; e < numEffects; ++e) { |
167 GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords); | 168 GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords); |
168 fGLEffects[e]->setData(uniformManager, drawEffect); | 169 fGLEffects[e]->setData(uniformManager, drawEffect); |
169 this->setTransformData(uniformManager, drawEffect, e); | 170 this->setTransformData(uniformManager, drawEffect, e); |
170 this->bindTextures(gpu, *drawEffect.effect(), e); | 171 this->bindTextures(gpu, *drawEffect.effect(), e); |
171 } | 172 } |
172 } | 173 } |
173 | 174 |
174 void GrGLProgramEffects::setTransformData(const GrGLUniformManager& uniformManag
er, | 175 void GrGLVertexProgramEffects::setTransformData(const GrGLUniformManager& unifor
mManager, |
175 const GrDrawEffect& drawEffect, | 176 const GrDrawEffect& drawEffect, |
176 int effectIdx) { | 177 int effectIdx) { |
177 SkTArray<Transform, true>& transforms = fTransforms[effectIdx]; | 178 SkTArray<Transform, true>& transforms = fTransforms[effectIdx]; |
178 int numTransforms = transforms.count(); | 179 int numTransforms = transforms.count(); |
179 SkASSERT(numTransforms == (*drawEffect.effect())->numTransforms()); | 180 SkASSERT(numTransforms == (*drawEffect.effect())->numTransforms()); |
180 for (int t = 0; t < numTransforms; ++t) { | 181 for (int t = 0; t < numTransforms; ++t) { |
181 const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTr
ansform(t); | 182 const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTr
ansform(t); |
182 const SkMatrix& matrix = coordTransform.getMatrix(); | 183 const SkMatrix& matrix = coordTransform.getMatrix(); |
183 const SkMatrix& coordChangeMatrix = kLocal_GrCoordSet == coordTransform.
sourceCoords() ? | 184 const SkMatrix& coordChangeMatrix = kLocal_GrCoordSet == coordTransform.
sourceCoords() ? |
184 drawEffect.getCoordChangeMatrix(
) : | 185 drawEffect.getCoordChangeMatrix(
) : |
185 SkMatrix::I(); | 186 SkMatrix::I(); |
186 SkASSERT(transforms[t].fHandle.isValid() != (kVoid_GrSLType == transform
s[t].fType)); | 187 SkASSERT(transforms[t].fHandle.isValid() != (kVoid_GrSLType == transform
s[t].fType)); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 SkASSERT(samplers[s].fTextureUnit >= 0); | 237 SkASSERT(samplers[s].fTextureUnit >= 0); |
237 const GrTextureAccess& textureAccess = effect->textureAccess(s); | 238 const GrTextureAccess& textureAccess = effect->textureAccess(s); |
238 gpu->bindTexture(samplers[s].fTextureUnit, | 239 gpu->bindTexture(samplers[s].fTextureUnit, |
239 textureAccess.getParams(), | 240 textureAccess.getParams(), |
240 static_cast<GrGLTexture*>(textureAccess.getTexture())); | 241 static_cast<GrGLTexture*>(textureAccess.getTexture())); |
241 } | 242 } |
242 } | 243 } |
243 | 244 |
244 //////////////////////////////////////////////////////////////////////////////// | 245 //////////////////////////////////////////////////////////////////////////////// |
245 | 246 |
246 GrGLProgramEffectsBuilder::GrGLProgramEffectsBuilder(GrGLShaderBuilder* builder,
int reserveCount) | 247 GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderB
uilder* builder, |
247 : fBuilder(builder) { | 248 int reserveCoun
t) |
248 GrGLShaderBuilder::VertexBuilder* vertexBuilder = fBuilder->getVertexBuilder
(); | 249 : fBuilder(builder) |
249 SkASSERT(NULL != vertexBuilder); | 250 , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects, |
250 fProgramEffects.reset(SkNEW_ARGS(GrGLProgramEffects, | 251 (reserveCount, fBuilder->hasExplicitLocalCoords
()))) { |
251 (reserveCount, vertexBuilder->hasExplicitLo
calCoords()))); | |
252 } | 252 } |
253 | 253 |
254 void GrGLProgramEffectsBuilder::emitEffect(const GrEffectStage& stage, | 254 void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage, |
255 EffectKey key, | 255 EffectKey key, |
256 const char* outColor, | 256 const char* outColor, |
257 const char* inColor, | 257 const char* inColor, |
258 int stageIndex) { | 258 int stageIndex) { |
259 GrGLShaderBuilder::VertexBuilder* vertexBuilder = fBuilder->getVertexBuilder
(); | |
260 SkASSERT(NULL != vertexBuilder); | |
261 SkASSERT(NULL != fProgramEffects.get()); | 259 SkASSERT(NULL != fProgramEffects.get()); |
262 | 260 |
263 GrDrawEffect drawEffect(stage, fProgramEffects->fHasExplicitLocalCoords); | 261 GrDrawEffect drawEffect(stage, fProgramEffects->fHasExplicitLocalCoords); |
264 const GrEffectRef& effect = *stage.getEffect(); | 262 const GrEffectRef& effect = *stage.getEffect(); |
265 SkSTArray<2, TransformedCoords> coords(effect->numTransforms()); | 263 SkSTArray<2, TransformedCoords> coords(effect->numTransforms()); |
266 SkSTArray<4, TextureSampler> samplers(effect->numTextures()); | 264 SkSTArray<4, TextureSampler> samplers(effect->numTextures()); |
267 | 265 |
268 this->emitAttributes(stage); | 266 this->emitAttributes(stage); |
269 this->emitTransforms(effect, key, &coords); | 267 this->emitTransforms(effect, key, &coords); |
270 this->emitSamplers(effect, &samplers); | 268 INHERITED::emitSamplers(fBuilder, fProgramEffects.get(), effect, &samplers); |
271 | 269 |
272 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); | 270 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); |
273 fProgramEffects->fGLEffects.push_back(glEffect); | 271 fProgramEffects->fGLEffects.push_back(glEffect); |
274 | 272 |
275 // Enclose custom code in a block to avoid namespace conflicts | 273 // Enclose custom code in a block to avoid namespace conflicts |
276 SkString openBrace; | 274 SkString openBrace; |
277 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name()); | 275 openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name()); |
278 vertexBuilder->vsCodeAppend(openBrace.c_str()); | 276 fBuilder->vsCodeAppend(openBrace.c_str()); |
279 fBuilder->fsCodeAppend(openBrace.c_str()); | 277 fBuilder->fsCodeAppend(openBrace.c_str()); |
280 | 278 |
281 glEffect->emitCode(fBuilder, drawEffect, key, outColor, inColor, coords, sam
plers); | 279 if (glEffect->isVertexEffect()) { |
| 280 GrGLVertexEffect* vertexEffect = static_cast<GrGLVertexEffect*>(glEffect
); |
| 281 vertexEffect->emitCode(fBuilder, drawEffect, key, outColor, inColor, coo
rds, samplers); |
| 282 } else { |
| 283 glEffect->emitCode(fBuilder, drawEffect, key, outColor, inColor, coords,
samplers); |
| 284 } |
282 | 285 |
283 vertexBuilder->vsCodeAppend("\t}\n"); | 286 fBuilder->vsCodeAppend("\t}\n"); |
284 fBuilder->fsCodeAppend("\t}\n"); | 287 fBuilder->fsCodeAppend("\t}\n"); |
285 } | 288 } |
286 | 289 |
287 void GrGLProgramEffectsBuilder::emitAttributes(const GrEffectStage& stage) { | 290 void GrGLVertexProgramEffectsBuilder::emitAttributes(const GrEffectStage& stage)
{ |
288 GrGLShaderBuilder::VertexBuilder* vertexBuilder = fBuilder->getVertexBuilder
(); | |
289 SkASSERT(NULL != vertexBuilder); | |
290 | |
291 int numAttributes = stage.getVertexAttribIndexCount(); | 291 int numAttributes = stage.getVertexAttribIndexCount(); |
292 const int* attributeIndices = stage.getVertexAttribIndices(); | 292 const int* attributeIndices = stage.getVertexAttribIndices(); |
293 for (int a = 0; a < numAttributes; ++a) { | 293 for (int a = 0; a < numAttributes; ++a) { |
294 // TODO: Make addAttribute mangle the name. | 294 // TODO: Make addAttribute mangle the name. |
295 SkString attributeName("aAttr"); | 295 SkString attributeName("aAttr"); |
296 attributeName.appendS32(attributeIndices[a]); | 296 attributeName.appendS32(attributeIndices[a]); |
297 vertexBuilder->addEffectAttribute(attributeIndices[a], | 297 fBuilder->addEffectAttribute(attributeIndices[a], |
298 (*stage.getEffect())->vertexAttribType
(a), | 298 (*stage.getEffect())->vertexAttribType(a), |
299 attributeName); | 299 attributeName); |
300 } | 300 } |
301 } | 301 } |
302 | 302 |
303 void GrGLProgramEffectsBuilder::emitTransforms(const GrEffectRef& effect, | 303 void GrGLVertexProgramEffectsBuilder::emitTransforms(const GrEffectRef& effect, |
304 EffectKey effectKey, | 304 EffectKey effectKey, |
305 TransformedCoordsArray* outCoords
) { | 305 TransformedCoordsArray* out
Coords) { |
306 GrGLShaderBuilder::VertexBuilder* vertexBuilder = fBuilder->getVertexBuilder
(); | 306 typedef GrGLVertexProgramEffects::Transform Transform; |
307 SkASSERT(NULL != vertexBuilder); | |
308 | |
309 typedef GrGLProgramEffects::Transform Transform; | |
310 SkTArray<Transform, true>& transforms = fProgramEffects->fTransforms.push_ba
ck(); | 307 SkTArray<Transform, true>& transforms = fProgramEffects->fTransforms.push_ba
ck(); |
311 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey); | 308 EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey); |
312 int numTransforms = effect->numTransforms(); | 309 int numTransforms = effect->numTransforms(); |
313 transforms.push_back_n(numTransforms); | 310 transforms.push_back_n(numTransforms); |
314 for (int t = 0; t < numTransforms; t++) { | 311 for (int t = 0; t < numTransforms; t++) { |
315 EffectKey key = (totalKey >> (kTransformKeyBits * t)) & kTransformKeyMas
k; | 312 EffectKey key = (totalKey >> (kTransformKeyBits * t)) & kTransformKeyMas
k; |
316 GrSLType varyingType = kVoid_GrSLType; | 313 GrSLType varyingType = kVoid_GrSLType; |
317 const char* uniName; | 314 const char* uniName; |
318 switch (key & kMatrixTypeKeyMask) { | 315 switch (key & kMatrixTypeKeyMask) { |
319 case kIdentity_MatrixType: | 316 case kIdentity_MatrixType: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 | 351 |
355 const char* varyingName = "MatrixCoord"; | 352 const char* varyingName = "MatrixCoord"; |
356 SkString suffixedVaryingName; | 353 SkString suffixedVaryingName; |
357 if (0 != t) { | 354 if (0 != t) { |
358 suffixedVaryingName.append(varyingName); | 355 suffixedVaryingName.append(varyingName); |
359 suffixedVaryingName.appendf("_%i", t); | 356 suffixedVaryingName.appendf("_%i", t); |
360 varyingName = suffixedVaryingName.c_str(); | 357 varyingName = suffixedVaryingName.c_str(); |
361 } | 358 } |
362 const char* vsVaryingName; | 359 const char* vsVaryingName; |
363 const char* fsVaryingName; | 360 const char* fsVaryingName; |
364 vertexBuilder->addVarying(varyingType, varyingName, &vsVaryingName, &fsV
aryingName); | 361 fBuilder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryin
gName); |
365 | 362 |
366 const GrGLShaderVar& coords = (kPositionCoords_Flag & key) ? | 363 const GrGLShaderVar& coords = (kPositionCoords_Flag & key) ? |
367 vertexBuilder->positionAttribute() : | 364 fBuilder->positionAttribute() : |
368 vertexBuilder->localCoordsAttribute(); | 365 fBuilder->localCoordsAttribute(); |
369 // varying = matrix * coords (logically) | 366 // varying = matrix * coords (logically) |
370 switch (transforms[t].fType) { | 367 switch (transforms[t].fType) { |
371 case kVoid_GrSLType: | 368 case kVoid_GrSLType: |
372 SkASSERT(kVec2f_GrSLType == varyingType); | 369 SkASSERT(kVec2f_GrSLType == varyingType); |
373 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coor
ds.c_str()); | 370 fBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords.c_
str()); |
374 break; | 371 break; |
375 case kVec2f_GrSLType: | 372 case kVec2f_GrSLType: |
376 SkASSERT(kVec2f_GrSLType == varyingType); | 373 SkASSERT(kVec2f_GrSLType == varyingType); |
377 vertexBuilder->vsCodeAppendf("\t%s = %s + %s;\n", | 374 fBuilder->vsCodeAppendf("\t%s = %s + %s;\n", |
378 vsVaryingName, uniName, coords.c_st
r()); | 375 vsVaryingName, uniName, coords.c_str()); |
379 break; | 376 break; |
380 case kMat33f_GrSLType: { | 377 case kMat33f_GrSLType: { |
381 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == va
ryingType); | 378 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == va
ryingType); |
382 if (kVec2f_GrSLType == varyingType) { | 379 if (kVec2f_GrSLType == varyingType) { |
383 vertexBuilder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\
n", | 380 fBuilder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n", |
384 vsVaryingName, uniName, coords.
c_str()); | 381 vsVaryingName, uniName, coords.c_str
()); |
385 } else { | 382 } else { |
386 vertexBuilder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", | 383 fBuilder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", |
387 vsVaryingName, uniName, coords.
c_str()); | 384 vsVaryingName, uniName, coords.c_str
()); |
388 } | 385 } |
389 break; | 386 break; |
390 } | 387 } |
391 default: | 388 default: |
392 GrCrash("Unexpected uniform type."); | 389 GrCrash("Unexpected uniform type."); |
393 } | 390 } |
394 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (fsVaryingName, var
yingType)); | 391 SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (fsVaryingName, var
yingType)); |
395 } | 392 } |
396 } | 393 } |
397 | 394 |
398 void GrGLProgramEffectsBuilder::emitSamplers(const GrEffectRef& effect, | 395 void GrGLProgramEffectsBuilder::emitSamplers(GrGLShaderBuilder* builder, |
| 396 GrGLProgramEffects* programEffects, |
| 397 const GrEffectRef& effect, |
399 TextureSamplerArray* outSamplers) { | 398 TextureSamplerArray* outSamplers) { |
400 typedef GrGLProgramEffects::Sampler Sampler; | 399 typedef GrGLProgramEffects::Sampler Sampler; |
401 SkTArray<Sampler, true>& samplers = fProgramEffects->fSamplers.push_back(); | 400 SkTArray<Sampler, true>& samplers = programEffects->fSamplers.push_back(); |
402 int numTextures = effect->numTextures(); | 401 int numTextures = effect->numTextures(); |
403 samplers.push_back_n(numTextures); | 402 samplers.push_back_n(numTextures); |
404 SkString name; | 403 SkString name; |
405 for (int t = 0; t < numTextures; ++t) { | 404 for (int t = 0; t < numTextures; ++t) { |
406 name.printf("Sampler%d", t); | 405 name.printf("Sampler%d", t); |
407 samplers[t].fUniform = fBuilder->addUniform(GrGLShaderBuilder::kFragment
_Visibility, | 406 samplers[t].fUniform = builder->addUniform(GrGLShaderBuilder::kFragment_
Visibility, |
408 kSampler2D_GrSLType, | 407 kSampler2D_GrSLType, |
409 name.c_str()); | 408 name.c_str()); |
410 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler, | 409 SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler, |
411 (samplers[t].fUniform, effect->textureAccess(t)))
; | 410 (samplers[t].fUniform, effect->textureAccess(t)))
; |
412 } | 411 } |
413 } | 412 } |
OLD | NEW |