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 "SkReadBuffer.h" | 9 #include "SkReadBuffer.h" |
10 #include "SkWriteBuffer.h" | 10 #include "SkWriteBuffer.h" |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 #endif | 299 #endif |
300 | 300 |
301 /////////////////////////////////////////////////////////////////////////////// | 301 /////////////////////////////////////////////////////////////////////////////// |
302 | 302 |
303 #if SK_SUPPORT_GPU | 303 #if SK_SUPPORT_GPU |
304 class GrGLDisplacementMapEffect : public GrGLFragmentProcessor { | 304 class GrGLDisplacementMapEffect : public GrGLFragmentProcessor { |
305 public: | 305 public: |
306 GrGLDisplacementMapEffect(const GrProcessor&); | 306 GrGLDisplacementMapEffect(const GrProcessor&); |
307 virtual ~GrGLDisplacementMapEffect(); | 307 virtual ~GrGLDisplacementMapEffect(); |
308 | 308 |
309 virtual void emitCode(GrGLFPBuilder*, | 309 virtual void emitCode(EmitArgs&) override; |
310 const GrFragmentProcessor&, | |
311 const char* outputColor, | |
312 const char* inputColor, | |
313 const TransformedCoordsArray&, | |
314 const TextureSamplerArray&) override; | |
315 | 310 |
316 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); | 311 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); |
317 | 312 |
318 void setData(const GrGLProgramDataManager&, const GrProcessor&) override; | 313 void setData(const GrGLProgramDataManager&, const GrProcessor&) override; |
319 const GrTextureDomain::GLDomain& glDomain() const { return fGLDomain; } | 314 const GrTextureDomain::GLDomain& glDomain() const { return fGLDomain; } |
320 | 315 |
321 private: | 316 private: |
322 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; | 317 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
323 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; | 318 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
324 GrGLProgramDataManager::UniformHandle fScaleUni; | 319 GrGLProgramDataManager::UniformHandle fScaleUni; |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 /////////////////////////////////////////////////////////////////////////////// | 548 /////////////////////////////////////////////////////////////////////////////// |
554 | 549 |
555 GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrProcessor& proc) | 550 GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrProcessor& proc) |
556 : fXChannelSelector(proc.cast<GrDisplacementMapEffect>().xChannelSelector()) | 551 : fXChannelSelector(proc.cast<GrDisplacementMapEffect>().xChannelSelector()) |
557 , fYChannelSelector(proc.cast<GrDisplacementMapEffect>().yChannelSelector())
{ | 552 , fYChannelSelector(proc.cast<GrDisplacementMapEffect>().yChannelSelector())
{ |
558 } | 553 } |
559 | 554 |
560 GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { | 555 GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { |
561 } | 556 } |
562 | 557 |
563 void GrGLDisplacementMapEffect::emitCode(GrGLFPBuilder* builder, | 558 void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) { |
564 const GrFragmentProcessor& fp, | 559 const GrTextureDomain& domain = args.fFp.cast<GrDisplacementMapEffect>().dom
ain(); |
565 const char* outputColor, | |
566 const char* inputColor, | |
567 const TransformedCoordsArray& coords, | |
568 const TextureSamplerArray& samplers) { | |
569 const GrTextureDomain& domain = fp.cast<GrDisplacementMapEffect>().domain(); | |
570 sk_ignore_unused_variable(inputColor); | |
571 | 560 |
572 fScaleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 561 fScaleUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, |
573 kVec2f_GrSLType, kDefault_GrSLPrecision, "Sc
ale"); | 562 kVec2f_GrSLType, kDefault_GrSLPrecision, "Sc
ale"); |
574 const char* scaleUni = builder->getUniformCStr(fScaleUni); | 563 const char* scaleUni = args.fBuilder->getUniformCStr(fScaleUni); |
575 const char* dColor = "dColor"; | 564 const char* dColor = "dColor"; |
576 const char* cCoords = "cCoords"; | 565 const char* cCoords = "cCoords"; |
577 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use | 566 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use |
578 // a number smaller than that to approximate
0, but | 567 // a number smaller than that to approximate
0, but |
579 // leave room for 32-bit float GPU rounding e
rrors. | 568 // leave room for 32-bit float GPU rounding e
rrors. |
580 | 569 |
581 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 570 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
582 fsBuilder->codeAppendf("\t\tvec4 %s = ", dColor); | 571 fsBuilder->codeAppendf("\t\tvec4 %s = ", dColor); |
583 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].get
Type()); | 572 fsBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(), |
| 573 args.fCoords[0].getType()); |
584 fsBuilder->codeAppend(";\n"); | 574 fsBuilder->codeAppend(";\n"); |
585 | 575 |
586 // Unpremultiply the displacement | 576 // Unpremultiply the displacement |
587 fsBuilder->codeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb
/ %s.a, 0.0, 1.0);", | 577 fsBuilder->codeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb
/ %s.a, 0.0, 1.0);", |
588 dColor, dColor, nearZero, dColor, dColor); | 578 dColor, dColor, nearZero, dColor, dColor); |
589 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 1); | 579 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 1); |
590 fsBuilder->codeAppendf("\t\tvec2 %s = %s + %s*(%s.", | 580 fsBuilder->codeAppendf("\t\tvec2 %s = %s + %s*(%s.", |
591 cCoords, coords2D.c_str(), scaleUni, dColor); | 581 cCoords, coords2D.c_str(), scaleUni, dColor); |
592 | 582 |
593 switch (fXChannelSelector) { | 583 switch (fXChannelSelector) { |
594 case SkDisplacementMapEffect::kR_ChannelSelectorType: | 584 case SkDisplacementMapEffect::kR_ChannelSelectorType: |
595 fsBuilder->codeAppend("r"); | 585 fsBuilder->codeAppend("r"); |
596 break; | 586 break; |
597 case SkDisplacementMapEffect::kG_ChannelSelectorType: | 587 case SkDisplacementMapEffect::kG_ChannelSelectorType: |
598 fsBuilder->codeAppend("g"); | 588 fsBuilder->codeAppend("g"); |
599 break; | 589 break; |
(...skipping 20 matching lines...) Expand all Loading... |
620 break; | 610 break; |
621 case SkDisplacementMapEffect::kA_ChannelSelectorType: | 611 case SkDisplacementMapEffect::kA_ChannelSelectorType: |
622 fsBuilder->codeAppend("a"); | 612 fsBuilder->codeAppend("a"); |
623 break; | 613 break; |
624 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: | 614 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
625 default: | 615 default: |
626 SkDEBUGFAIL("Unknown Y channel selector"); | 616 SkDEBUGFAIL("Unknown Y channel selector"); |
627 } | 617 } |
628 fsBuilder->codeAppend("-vec2(0.5));\t\t"); | 618 fsBuilder->codeAppend("-vec2(0.5));\t\t"); |
629 | 619 |
630 fGLDomain.sampleTexture(fsBuilder, domain, outputColor, SkString(cCoords), s
amplers[1]); | 620 fGLDomain.sampleTexture(fsBuilder, domain, args.fOutputColor, SkString(cCoor
ds), |
| 621 args.fSamplers[1]); |
631 fsBuilder->codeAppend(";\n"); | 622 fsBuilder->codeAppend(";\n"); |
632 } | 623 } |
633 | 624 |
634 void GrGLDisplacementMapEffect::setData(const GrGLProgramDataManager& pdman, | 625 void GrGLDisplacementMapEffect::setData(const GrGLProgramDataManager& pdman, |
635 const GrProcessor& proc) { | 626 const GrProcessor& proc) { |
636 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); | 627 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); |
637 GrTexture* colorTex = displacementMap.texture(1); | 628 GrTexture* colorTex = displacementMap.texture(1); |
638 SkScalar scaleX = displacementMap.scale().fX / colorTex->width(); | 629 SkScalar scaleX = displacementMap.scale().fX / colorTex->width(); |
639 SkScalar scaleY = displacementMap.scale().fY / colorTex->height(); | 630 SkScalar scaleY = displacementMap.scale().fY / colorTex->height(); |
640 pdman.set2f(fScaleUni, SkScalarToFloat(scaleX), | 631 pdman.set2f(fScaleUni, SkScalarToFloat(scaleX), |
641 colorTex->origin() == kTopLeft_GrSurfaceOrigin ? | 632 colorTex->origin() == kTopLeft_GrSurfaceOrigin ? |
642 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); | 633 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); |
643 fGLDomain.setData(pdman, displacementMap.domain(), colorTex->origin()); | 634 fGLDomain.setData(pdman, displacementMap.domain(), colorTex->origin()); |
644 } | 635 } |
645 | 636 |
646 void GrGLDisplacementMapEffect::GenKey(const GrProcessor& proc, | 637 void GrGLDisplacementMapEffect::GenKey(const GrProcessor& proc, |
647 const GrGLSLCaps&, GrProcessorKeyBuilder*
b) { | 638 const GrGLSLCaps&, GrProcessorKeyBuilder*
b) { |
648 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); | 639 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); |
649 | 640 |
650 uint32_t xKey = displacementMap.xChannelSelector(); | 641 uint32_t xKey = displacementMap.xChannelSelector(); |
651 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBit
s; | 642 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBit
s; |
652 | 643 |
653 b->add32(xKey | yKey); | 644 b->add32(xKey | yKey); |
654 } | 645 } |
655 #endif | 646 #endif |
656 | 647 |
OLD | NEW |