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 "SkDisplacementMapEffect.h" | 8 #include "SkDisplacementMapEffect.h" |
9 #include "SkFlattenableBuffers.h" | 9 #include "SkFlattenableBuffers.h" |
10 #include "SkUnPreMultiply.h" | 10 #include "SkUnPreMultiply.h" |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 } | 402 } |
403 | 403 |
404 void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder, | 404 void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder, |
405 const GrDrawEffect&, | 405 const GrDrawEffect&, |
406 EffectKey key, | 406 EffectKey key, |
407 const char* outputColor, | 407 const char* outputColor, |
408 const char* inputColor, | 408 const char* inputColor, |
409 const TextureSamplerArray& samplers) { | 409 const TextureSamplerArray& samplers) { |
410 sk_ignore_unused_variable(inputColor); | 410 sk_ignore_unused_variable(inputColor); |
411 | 411 |
412 fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 412 fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
413 kVec2f_GrSLType, "Scale"); | 413 kVec2f_GrSLType, "Scale"); |
414 const char* scaleUni = builder->getUniformCStr(fScaleUni); | 414 const char* scaleUni = builder->getUniformCStr(fScaleUni); |
415 | 415 |
416 SkString dCoordsIn; | 416 SkString dCoordsIn; |
417 GrSLType dCoordsType = fDisplacementEffectMatrix.emitCode( | 417 GrSLType dCoordsType = fDisplacementEffectMatrix.emitCode( |
418 builder, key, &dCoordsIn, NULL, "DISPL"); | 418 builder, key, &dCoordsIn, NULL, "DISPL"); |
419 SkString cCoordsIn; | 419 SkString cCoordsIn; |
420 GrSLType cCoordsType = fColorEffectMatrix.emitCode( | 420 GrSLType cCoordsType = fColorEffectMatrix.emitCode( |
421 builder, key, &cCoordsIn, NULL, "COLOR"); | 421 builder, key, &cCoordsIn, NULL, "COLOR"); |
422 | 422 |
423 const char* dColor = "dColor"; | 423 const char* dColor = "dColor"; |
424 const char* cCoords = "cCoords"; | 424 const char* cCoords = "cCoords"; |
425 const char* outOfBounds = "outOfBounds"; | 425 const char* outOfBounds = "outOfBounds"; |
426 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use | 426 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use |
427 // a number smaller than that to approximate
0, but | 427 // a number smaller than that to approximate
0, but |
428 // leave room for 32-bit float GPU rounding e
rrors. | 428 // leave room for 32-bit float GPU rounding e
rrors. |
429 | 429 |
430 builder->fsCodeAppendf("\t\tvec4 %s = ", dColor); | 430 builder->fsCodeAppendf("\t\tvec4 %s = ", dColor); |
431 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, | 431 builder->fsAppendTextureLookup(samplers[0], dCoordsIn.c_str(), dCoordsType); |
432 samplers[0], | |
433 dCoordsIn.c_str(), | |
434 dCoordsType); | |
435 builder->fsCodeAppend(";\n"); | 432 builder->fsCodeAppend(";\n"); |
436 | 433 |
437 // Unpremultiply the displacement | 434 // Unpremultiply the displacement |
438 builder->fsCodeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb
/ %s.a, 0.0, 1.0);", | 435 builder->fsCodeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb
/ %s.a, 0.0, 1.0);", |
439 dColor, dColor, nearZero, dColor, dColor); | 436 dColor, dColor, nearZero, dColor, dColor); |
440 | 437 |
441 builder->fsCodeAppendf("\t\tvec2 %s = %s + %s*(%s.", | 438 builder->fsCodeAppendf("\t\tvec2 %s = %s + %s*(%s.", |
442 cCoords, cCoordsIn.c_str(), scaleUni, dColor); | 439 cCoords, cCoordsIn.c_str(), scaleUni, dColor); |
443 | 440 |
444 switch (fXChannelSelector) { | 441 switch (fXChannelSelector) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 SkDEBUGFAIL("Unknown Y channel selector"); | 474 SkDEBUGFAIL("Unknown Y channel selector"); |
478 } | 475 } |
479 builder->fsCodeAppend("-vec2(0.5));\t\t"); | 476 builder->fsCodeAppend("-vec2(0.5));\t\t"); |
480 | 477 |
481 // FIXME : This can be achieved with a "clamp to border" texture repeat mode
and | 478 // FIXME : This can be achieved with a "clamp to border" texture repeat mode
and |
482 // a 0 border color instead of computing if cCoords is out of bounds
here. | 479 // a 0 border color instead of computing if cCoords is out of bounds
here. |
483 builder->fsCodeAppendf( | 480 builder->fsCodeAppendf( |
484 "bool %s = (%s.x < 0.0) || (%s.y < 0.0) || (%s.x > 1.0) || (%s.y > 1.0);
\t\t", | 481 "bool %s = (%s.x < 0.0) || (%s.y < 0.0) || (%s.x > 1.0) || (%s.y > 1.0);
\t\t", |
485 outOfBounds, cCoords, cCoords, cCoords, cCoords); | 482 outOfBounds, cCoords, cCoords, cCoords, cCoords); |
486 builder->fsCodeAppendf("%s = %s ? vec4(0.0) : ", outputColor, outOfBounds); | 483 builder->fsCodeAppendf("%s = %s ? vec4(0.0) : ", outputColor, outOfBounds); |
487 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, | 484 builder->fsAppendTextureLookup(samplers[1], cCoords, cCoordsType); |
488 samplers[1], | |
489 cCoords, | |
490 cCoordsType); | |
491 builder->fsCodeAppend(";\n"); | 485 builder->fsCodeAppend(";\n"); |
492 } | 486 } |
493 | 487 |
494 void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman, | 488 void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman, |
495 const GrDrawEffect& drawEffect) { | 489 const GrDrawEffect& drawEffect) { |
496 const GrDisplacementMapEffect& displacementMap = | 490 const GrDisplacementMapEffect& displacementMap = |
497 drawEffect.castEffect<GrDisplacementMapEffect>(); | 491 drawEffect.castEffect<GrDisplacementMapEffect>(); |
498 GrTexture* displTex = displacementMap.texture(0); | 492 GrTexture* displTex = displacementMap.texture(0); |
499 GrTexture* colorTex = displacementMap.texture(1); | 493 GrTexture* colorTex = displacementMap.texture(1); |
500 fDisplacementEffectMatrix.setData(uman, | 494 fDisplacementEffectMatrix.setData(uman, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 colorTex); | 526 colorTex); |
533 | 527 |
534 colorKey <<= GrGLEffectMatrix::kKeyBits; | 528 colorKey <<= GrGLEffectMatrix::kKeyBits; |
535 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits); | 529 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits); |
536 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits + | 530 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits + |
537 SkDisplacementMapEff
ect::kKeyBits); | 531 SkDisplacementMapEff
ect::kKeyBits); |
538 | 532 |
539 return xKey | yKey | displKey | colorKey; | 533 return xKey | yKey | displKey | colorKey; |
540 } | 534 } |
541 #endif | 535 #endif |
OLD | NEW |