| 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 "SkDevice.h" | 9 #include "SkDevice.h" |
| 10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
| 11 #include "SkWriteBuffer.h" | 11 #include "SkWriteBuffer.h" |
| 12 #include "SkUnPreMultiply.h" | 12 #include "SkUnPreMultiply.h" |
| 13 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
| 14 #if SK_SUPPORT_GPU | 14 #if SK_SUPPORT_GPU |
| 15 #include "GrContext.h" | 15 #include "GrContext.h" |
| 16 #include "GrDrawContext.h" | 16 #include "GrDrawContext.h" |
| 17 #include "GrCoordTransform.h" | 17 #include "GrCoordTransform.h" |
| 18 #include "GrInvariantOutput.h" | 18 #include "GrInvariantOutput.h" |
| 19 #include "effects/GrTextureDomain.h" | 19 #include "effects/GrTextureDomain.h" |
| 20 #include "gl/GrGLFragmentProcessor.h" | 20 #include "gl/GrGLFragmentProcessor.h" |
| 21 #include "gl/builders/GrGLProgramBuilder.h" | 21 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 22 #include "glsl/GrGLSLProgramBuilder.h" |
| 22 #include "glsl/GrGLSLProgramDataManager.h" | 23 #include "glsl/GrGLSLProgramDataManager.h" |
| 23 #endif | 24 #endif |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 #define kChannelSelectorKeyBits 3; // Max value is 4, so 3 bits are required at
most | 28 #define kChannelSelectorKeyBits 3; // Max value is 4, so 3 bits are required at
most |
| 28 | 29 |
| 29 template<SkDisplacementMapEffect::ChannelSelectorType type> | 30 template<SkDisplacementMapEffect::ChannelSelectorType type> |
| 30 uint32_t getValue(SkColor, const SkUnPreMultiply::Scale*) { | 31 uint32_t getValue(SkColor, const SkUnPreMultiply::Scale*) { |
| 31 SkDEBUGFAIL("Unknown channel selector"); | 32 SkDEBUGFAIL("Unknown channel selector"); |
| (...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 : fXChannelSelector(proc.cast<GrDisplacementMapEffect>().xChannelSelector()) | 541 : fXChannelSelector(proc.cast<GrDisplacementMapEffect>().xChannelSelector()) |
| 541 , fYChannelSelector(proc.cast<GrDisplacementMapEffect>().yChannelSelector())
{ | 542 , fYChannelSelector(proc.cast<GrDisplacementMapEffect>().yChannelSelector())
{ |
| 542 } | 543 } |
| 543 | 544 |
| 544 GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { | 545 GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { |
| 545 } | 546 } |
| 546 | 547 |
| 547 void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) { | 548 void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) { |
| 548 const GrTextureDomain& domain = args.fFp.cast<GrDisplacementMapEffect>().dom
ain(); | 549 const GrTextureDomain& domain = args.fFp.cast<GrDisplacementMapEffect>().dom
ain(); |
| 549 | 550 |
| 550 fScaleUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, | 551 fScaleUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_Visibi
lity, |
| 551 kVec2f_GrSLType, kDefault_GrSLPrecision, "Sc
ale"); | 552 kVec2f_GrSLType, kDefault_GrSLPrecisio
n, "Scale"); |
| 552 const char* scaleUni = args.fBuilder->getUniformCStr(fScaleUni); | 553 const char* scaleUni = args.fBuilder->getUniformCStr(fScaleUni); |
| 553 const char* dColor = "dColor"; | 554 const char* dColor = "dColor"; |
| 554 const char* cCoords = "cCoords"; | 555 const char* cCoords = "cCoords"; |
| 555 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use | 556 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use |
| 556 // a number smaller than that to approximate
0, but | 557 // a number smaller than that to approximate
0, but |
| 557 // leave room for 32-bit float GPU rounding e
rrors. | 558 // leave room for 32-bit float GPU rounding e
rrors. |
| 558 | 559 |
| 559 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | 560 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder()
; |
| 560 fsBuilder->codeAppendf("\t\tvec4 %s = ", dColor); | 561 fsBuilder->codeAppendf("\t\tvec4 %s = ", dColor); |
| 561 fsBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(), | 562 fsBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(), |
| 562 args.fCoords[0].getType()); | 563 args.fCoords[0].getType()); |
| 563 fsBuilder->codeAppend(";\n"); | 564 fsBuilder->codeAppend(";\n"); |
| 564 | 565 |
| 565 // Unpremultiply the displacement | 566 // Unpremultiply the displacement |
| 566 fsBuilder->codeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb
/ %s.a, 0.0, 1.0);", | 567 fsBuilder->codeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb
/ %s.a, 0.0, 1.0);", |
| 567 dColor, dColor, nearZero, dColor, dColor); | 568 dColor, dColor, nearZero, dColor, dColor); |
| 568 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 1); | 569 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 1); |
| 569 fsBuilder->codeAppendf("\t\tvec2 %s = %s + %s*(%s.", | 570 fsBuilder->codeAppendf("\t\tvec2 %s = %s + %s*(%s.", |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 const GrGLSLCaps&, GrProcessorKeyBuilder*
b) { | 628 const GrGLSLCaps&, GrProcessorKeyBuilder*
b) { |
| 628 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); | 629 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); |
| 629 | 630 |
| 630 uint32_t xKey = displacementMap.xChannelSelector(); | 631 uint32_t xKey = displacementMap.xChannelSelector(); |
| 631 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBit
s; | 632 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBit
s; |
| 632 | 633 |
| 633 b->add32(xKey | yKey); | 634 b->add32(xKey | yKey); |
| 634 } | 635 } |
| 635 #endif | 636 #endif |
| 636 | 637 |
| OLD | NEW |