Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: src/effects/SkPerlinNoiseShader.cpp

Issue 14087002: Perlin noise adjustments (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« bench/PerlinNoiseBench.cpp ('K') | « gyp/bench.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkDither.h" 8 #include "SkDither.h"
9 #include "SkPerlinNoiseShader.h" 9 #include "SkPerlinNoiseShader.h"
10 #include "SkFlattenableBuffers.h" 10 #include "SkFlattenableBuffers.h"
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 for (int i = 0; i < count; ++i) { 487 for (int i = 0; i < count; ++i) {
488 unsigned dither = DITHER_VALUE(x); 488 unsigned dither = DITHER_VALUE(x);
489 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither); 489 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither);
490 DITHER_INC_X(x); 490 DITHER_INC_X(x);
491 point.fX += SK_Scalar1; 491 point.fX += SK_Scalar1;
492 } 492 }
493 } 493 }
494 494
495 ///////////////////////////////////////////////////////////////////// 495 /////////////////////////////////////////////////////////////////////
496 496
497 #if SK_SUPPORT_GPU 497 #if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID)
498 // CPU noise is faster on Android, so the GPU implementation is only for desktop
498 499
499 #include "GrTBackendEffectFactory.h" 500 #include "GrTBackendEffectFactory.h"
500 501
501 class GrGLPerlinNoise : public GrGLEffect { 502 class GrGLNoise : public GrGLEffect {
502 public: 503 public:
504 GrGLNoise(const GrBackendEffectFactory& factory,
505 const GrDrawEffect& drawEffect);
506 virtual ~GrGLNoise() {}
503 507
508 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
509
510 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&);
511
512 protected:
513 SkPerlinNoiseShader::Type fType;
514 bool fStitchTiles;
515 int fNumOctaves;
516 GrGLUniformManager::UniformHandle fBaseFrequencyUni;
517 GrGLUniformManager::UniformHandle fAlphaUni;
518 GrGLUniformManager::UniformHandle fInvMatrixUni;
519 GrGLEffectMatrix fEffectMatrix;
520
521 private:
522 typedef GrGLEffect INHERITED;
523 };
524
525 class GrGLPerlinNoise : public GrGLNoise {
526 public:
504 GrGLPerlinNoise(const GrBackendEffectFactory& factory, 527 GrGLPerlinNoise(const GrBackendEffectFactory& factory,
505 const GrDrawEffect& drawEffect); 528 const GrDrawEffect& drawEffect)
506 virtual ~GrGLPerlinNoise() { } 529 : GrGLNoise(factory, drawEffect) {}
530 virtual ~GrGLPerlinNoise() {}
507 531
508 virtual void emitCode(GrGLShaderBuilder*, 532 virtual void emitCode(GrGLShaderBuilder*,
509 const GrDrawEffect&, 533 const GrDrawEffect&,
510 EffectKey, 534 EffectKey,
511 const char* outputColor, 535 const char* outputColor,
512 const char* inputColor, 536 const char* inputColor,
513 const TextureSamplerArray&) SK_OVERRIDE; 537 const TextureSamplerArray&) SK_OVERRIDE;
514 538
515 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
516
517 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&); 539 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&);
518 540
519 private: 541 private:
520 SkPerlinNoiseShader::Type fType; 542 GrGLUniformManager::UniformHandle fStitchDataUni;
521 bool fStitchTiles;
522 int fNumOctaves;
523 GrGLUniformManager::UniformHandle fBaseFrequencyUni;
524 GrGLUniformManager::UniformHandle fStitchDataUni;
525 GrGLUniformManager::UniformHandle fAlphaUni;
526 GrGLUniformManager::UniformHandle fInvMatrixUni;
527 GrGLEffectMatrix fEffectMatrix;
528 543
529 typedef GrGLEffect INHERITED; 544 typedef GrGLNoise INHERITED;
545 };
546
547 class GrGLSimplexNoise : public GrGLNoise {
548 // Note : This is for reference only. GrGLPerlinNoise is used for processing .
549 public:
550 GrGLSimplexNoise(const GrBackendEffectFactory& factory,
551 const GrDrawEffect& drawEffect)
552 : GrGLNoise(factory, drawEffect) {}
553
554 virtual ~GrGLSimplexNoise() {}
555
556 virtual void emitCode(GrGLShaderBuilder*,
557 const GrDrawEffect&,
558 EffectKey,
559 const char* outputColor,
560 const char* inputColor,
561 const TextureSamplerArray&) SK_OVERRIDE;
562
563 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&);
Stephen White 2013/04/10 19:55:09 I think you should have SK_OVERRIDE here too.
sugoi 2013/04/10 20:02:42 Fixed in all 3 places
564
565 private:
566 GrGLUniformManager::UniformHandle fSeedUni;
567
568 typedef GrGLNoise INHERITED;
530 }; 569 };
531 570
532 ///////////////////////////////////////////////////////////////////// 571 /////////////////////////////////////////////////////////////////////
533 572
534 class GrPerlinNoiseEffect : public GrEffect { 573 class GrNoiseEffect : public GrEffect {
574 public:
575 virtual ~GrNoiseEffect() { }
576
577 SkPerlinNoiseShader::Type type() const { return fType; }
578 bool stitchTiles() const { return fStitchTiles; }
579 const SkVector& baseFrequency() const { return fBaseFrequency; }
580 int numOctaves() const { return fNumOctaves & 0xFF; /*[0,255] octaves allowe d*/ }
Stephen White 2013/04/10 19:55:09 Shouldn't this limit be applied on set, rather tha
sugoi 2013/04/10 20:02:42 It is already set in SkPerlinNoiseShader's constru
581 const SkMatrix& matrix() const { return fMatrix; }
582 uint8_t alpha() const { return fAlpha; }
583 GrGLEffectMatrix::CoordsType coordsType() const { return GrEffect::kLocal_Co ordsType; }
584
585 void getConstantColorComponents(GrColor*, uint32_t* validFlags) const SK_OVE RRIDE {
586 *validFlags = 0; // This is noise. Nothing is constant.
587 }
588
589 protected:
590 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
591 const GrNoiseEffect& s = CastEffect<GrNoiseEffect>(sBase);
592 return fType == s.fType &&
593 fBaseFrequency == s.fBaseFrequency &&
594 fStitchTiles == s.fStitchTiles &&
595 fMatrix == s.fMatrix &&
596 fAlpha == s.fAlpha;
Stephen White 2013/04/10 19:55:09 Need to compare fNumOctaves here too?
sugoi 2013/04/10 20:02:42 Done.
597 }
598
599 GrNoiseEffect(SkPerlinNoiseShader::Type type, const SkVector& baseFrequency, int numOctaves,
600 bool stitchTiles, const SkMatrix& matrix, uint8_t alpha)
601 : fType(type)
602 , fBaseFrequency(baseFrequency)
603 , fNumOctaves(numOctaves)
604 , fStitchTiles(stitchTiles)
605 , fMatrix(matrix)
606 , fAlpha(alpha) {
607 }
608
609 SkPerlinNoiseShader::Type fType;
610 SkVector fBaseFrequency;
611 int fNumOctaves;
612 bool fStitchTiles;
613 SkMatrix fMatrix;
614 uint8_t fAlpha;
615
616 private:
617 typedef GrEffect INHERITED;
618 };
619
620 class GrPerlinNoiseEffect : public GrNoiseEffect {
535 public: 621 public:
536 static GrEffectRef* Create(SkPerlinNoiseShader::Type type, const SkVector& b aseFrequency, 622 static GrEffectRef* Create(SkPerlinNoiseShader::Type type, const SkVector& b aseFrequency,
537 int numOctaves, bool stitchTiles, 623 int numOctaves, bool stitchTiles,
538 const SkPerlinNoiseShader::StitchData& stitchData , 624 const SkPerlinNoiseShader::StitchData& stitchData ,
539 GrTexture* permutationsTexture, GrTexture* noiseT exture, 625 GrTexture* permutationsTexture, GrTexture* noiseT exture,
540 const SkMatrix& matrix, uint8_t alpha) { 626 const SkMatrix& matrix, uint8_t alpha) {
541 AutoEffectUnref effect(SkNEW_ARGS(GrPerlinNoiseEffect, (type, baseFreque ncy, numOctaves, 627 AutoEffectUnref effect(SkNEW_ARGS(GrPerlinNoiseEffect, (type, baseFreque ncy, numOctaves,
542 stitchTiles, stitchData, permutationsTexture, noiseTexture, matrix, alpha))); 628 stitchTiles, stitchData, permutationsTexture, noiseTexture, matrix, alpha)));
543 return CreateEffectRef(effect); 629 return CreateEffectRef(effect);
544 } 630 }
545 631
546 virtual ~GrPerlinNoiseEffect() { } 632 virtual ~GrPerlinNoiseEffect() { }
547 633
548 static const char* Name() { return "PerlinNoise"; } 634 static const char* Name() { return "PerlinNoise"; }
549 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { 635 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
550 return GrTBackendEffectFactory<GrPerlinNoiseEffect>::getInstance(); 636 return GrTBackendEffectFactory<GrPerlinNoiseEffect>::getInstance();
551 } 637 }
552 SkPerlinNoiseShader::Type type() const { return fType; }
553 bool stitchTiles() const { return fStitchTiles; }
554 const SkVector& baseFrequency() const { return fBaseFrequency; }
555 int numOctaves() const { return fNumOctaves & 0xFF; /*[0,255] octaves allowe d*/ }
556 const SkPerlinNoiseShader::StitchData& stitchData() const { return fStitchDa ta; } 638 const SkPerlinNoiseShader::StitchData& stitchData() const { return fStitchDa ta; }
557 const SkMatrix& matrix() const { return fMatrix; }
558 uint8_t alpha() const { return fAlpha; }
559 GrGLEffectMatrix::CoordsType coordsType() const { return GrEffect::kLocal_Co ordsType; }
560 639
561 typedef GrGLPerlinNoise GLEffect; 640 typedef GrGLPerlinNoise GLEffect;
562 641
563 void getConstantColorComponents(GrColor*, uint32_t* validFlags) const SK_OVE RRIDE {
564 *validFlags = 0; // This is noise. Nothing is constant.
565 }
566
567 private: 642 private:
568 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { 643 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
569 const GrPerlinNoiseEffect& s = CastEffect<GrPerlinNoiseEffect>(sBase); 644 const GrPerlinNoiseEffect& s = CastEffect<GrPerlinNoiseEffect>(sBase);
570 return fPermutationsAccess.getTexture() == s.fPermutationsAccess.getText ure() && 645 return INHERITED::onIsEqual(sBase) &&
646 fPermutationsAccess.getTexture() == s.fPermutationsAccess.getText ure() &&
571 fNoiseAccess.getTexture() == s.fNoiseAccess.getTexture() && 647 fNoiseAccess.getTexture() == s.fNoiseAccess.getTexture() &&
572 fType == s.fType && 648 fStitchData == s.fStitchData;
573 fBaseFrequency == s.fBaseFrequency &&
574 fStitchTiles == s.fStitchTiles &&
575 fStitchData == s.fStitchData &&
576 fMatrix == s.fMatrix &&
577 fAlpha == s.fAlpha;
578 } 649 }
579 650
580 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type, const SkVector& baseFreq uency, 651 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type, const SkVector& baseFreq uency,
581 int numOctaves, bool stitchTiles, 652 int numOctaves, bool stitchTiles,
582 const SkPerlinNoiseShader::StitchData& stitchData, 653 const SkPerlinNoiseShader::StitchData& stitchData,
583 GrTexture* permutationsTexture, GrTexture* noiseTexture, 654 GrTexture* permutationsTexture, GrTexture* noiseTexture,
584 const SkMatrix& matrix, uint8_t alpha) 655 const SkMatrix& matrix, uint8_t alpha)
585 : fPermutationsAccess(permutationsTexture) 656 : GrNoiseEffect(type, baseFrequency, numOctaves, stitchTiles, matrix, alph a)
657 , fPermutationsAccess(permutationsTexture)
586 , fNoiseAccess(noiseTexture) 658 , fNoiseAccess(noiseTexture)
587 , fType(type) 659 , fStitchData(stitchData) {
588 , fBaseFrequency(baseFrequency)
589 , fNumOctaves(numOctaves)
590 , fStitchTiles(stitchTiles)
591 , fStitchData(stitchData)
592 , fMatrix(matrix)
593 , fAlpha(alpha)
594 {
595 this->addTextureAccess(&fPermutationsAccess); 660 this->addTextureAccess(&fPermutationsAccess);
596 this->addTextureAccess(&fNoiseAccess); 661 this->addTextureAccess(&fNoiseAccess);
597 } 662 }
598 663
599 // GR_DECLARE_EFFECT_TEST; 664 GR_DECLARE_EFFECT_TEST;
600 665
601 GrTextureAccess fPermutationsAccess; 666 GrTextureAccess fPermutationsAccess;
602 GrTextureAccess fNoiseAccess; 667 GrTextureAccess fNoiseAccess;
603 SkPerlinNoiseShader::Type fType;
604 SkVector fBaseFrequency;
605 int fNumOctaves;
606 bool fStitchTiles;
607 SkPerlinNoiseShader::StitchData fStitchData; 668 SkPerlinNoiseShader::StitchData fStitchData;
608 SkMatrix fMatrix;
609 uint8_t fAlpha;
610 669
611 typedef GrEffect INHERITED; 670 typedef GrNoiseEffect INHERITED;
671 };
672
673 class GrSimplexNoiseEffect : public GrNoiseEffect {
674 // Note : This is for reference only. GrPerlinNoiseEffect is used for proces sing.
675 public:
676 static GrEffectRef* Create(SkPerlinNoiseShader::Type type, const SkVector& b aseFrequency,
677 int numOctaves, bool stitchTiles, const SkScalar seed,
678 const SkMatrix& matrix, uint8_t alpha) {
679 AutoEffectUnref effect(SkNEW_ARGS(GrSimplexNoiseEffect, (type, baseFrequ ency, numOctaves,
680 stitchTiles, seed, matrix, alpha)));
681 return CreateEffectRef(effect);
682 }
683
684 virtual ~GrSimplexNoiseEffect() { }
685
686 static const char* Name() { return "SimplexNoise"; }
687 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
688 return GrTBackendEffectFactory<GrSimplexNoiseEffect>::getInstance();
689 }
690 const SkScalar& seed() const { return fSeed; }
691
692 typedef GrGLSimplexNoise GLEffect;
693
694 private:
695 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
696 const GrSimplexNoiseEffect& s = CastEffect<GrSimplexNoiseEffect>(sBase);
697 return INHERITED::onIsEqual(sBase) && fSeed == s.fSeed;
698 }
699
700 GrSimplexNoiseEffect(SkPerlinNoiseShader::Type type, const SkVector& baseFre quency,
701 int numOctaves, bool stitchTiles, const SkScalar seed,
702 const SkMatrix& matrix, uint8_t alpha)
703 : GrNoiseEffect(type, baseFrequency, numOctaves, stitchTiles, matrix, alph a)
704 , fSeed(seed) {
705 }
706
707 SkScalar fSeed;
708
709 typedef GrNoiseEffect INHERITED;
612 }; 710 };
613 711
614 ///////////////////////////////////////////////////////////////////// 712 /////////////////////////////////////////////////////////////////////
615 #if 0
616 GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect); 713 GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect);
617 714
618 GrEffectRef* GrPerlinNoiseEffect::TestCreate(SkMWCRandom* random, 715 GrEffectRef* GrPerlinNoiseEffect::TestCreate(SkMWCRandom* random,
619 GrContext* context, 716 GrContext* context,
620 const GrDrawTargetCaps&, 717 const GrDrawTargetCaps&,
621 GrTexture**) { 718 GrTexture**) {
622 int numOctaves = random->nextRangeU(2, 10); 719 int numOctaves = random->nextRangeU(2, 10);
623 bool stitchTiles = random->nextBool(); 720 bool stitchTiles = random->nextBool();
624 SkScalar seed = SkIntToScalar(random->nextU()); 721 SkScalar seed = SkIntToScalar(random->nextU());
625 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextR angeU(4, 4096)); 722 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextR angeU(4, 4096));
626 SkScalar baseFrequencyX = random->nextRangeScalar(SkFloatToScalar(0.01f), 723 SkScalar baseFrequencyX = random->nextRangeScalar(SkFloatToScalar(0.01f),
627 SkFloatToScalar(0.99f)); 724 SkFloatToScalar(0.99f));
628 SkScalar baseFrequencyY = random->nextRangeScalar(SkFloatToScalar(0.01f), 725 SkScalar baseFrequencyY = random->nextRangeScalar(SkFloatToScalar(0.01f),
629 SkFloatToScalar(0.99f)); 726 SkFloatToScalar(0.99f));
630 727
631 SkShader* shader = random->nextBool() ? 728 SkShader* shader = random->nextBool() ?
632 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, 729 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
633 stitchTiles ? &tileSize : NULL) : 730 stitchTiles ? &tileSize : NULL) :
634 SkPerlinNoiseShader::CreateTubulence(baseFrequencyX, baseFrequencyY, num Octaves, seed, 731 SkPerlinNoiseShader::CreateTubulence(baseFrequencyX, baseFrequencyY, num Octaves, seed,
635 stitchTiles ? &tileSize : NULL); 732 stitchTiles ? &tileSize : NULL);
636 733
637 SkPaint paint; 734 SkPaint paint;
638 GrEffectRef* effect = shader->asNewEffect(context, paint); 735 GrEffectRef* effect = shader->asNewEffect(context, paint);
639 736
640 SkDELETE(shader); 737 SkDELETE(shader);
641 738
642 return effect; 739 return effect;
643 } 740 }
644 #endif 741
645 ///////////////////////////////////////////////////////////////////// 742 /////////////////////////////////////////////////////////////////////
646 743
744 void GrGLSimplexNoise::emitCode(GrGLShaderBuilder* builder,
745 const GrDrawEffect&,
746 EffectKey key,
747 const char* outputColor,
748 const char* inputColor,
749 const TextureSamplerArray&) {
750 sk_ignore_unused_variable(inputColor);
751
752 const char* vCoords;
753 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &vCoords);
754
755 fSeedUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
756 kFloat_GrSLType, "seed");
757 const char* seedUni = builder->getUniformCStr(fSeedUni);
758 fInvMatrixUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
759 kMat33f_GrSLType, "invMatrix");
760 const char* invMatrixUni = builder->getUniformCStr(fInvMatrixUni);
761 fBaseFrequencyUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderT ype,
762 kVec2f_GrSLType, "baseFrequency");
763 const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni);
764 fAlphaUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
765 kFloat_GrSLType, "alpha");
766 const char* alphaUni = builder->getUniformCStr(fAlphaUni);
767
768 // Add vec3 modulo 289 function
769 static const GrGLShaderVar vec3_Args[] = {
770 GrGLShaderVar("x", kVec3f_GrSLType)
771 };
772
773 SkString mod289_3_funcName;
774 builder->emitFunction(GrGLShaderBuilder::kFragment_ShaderType, kVec3f_GrSLTy pe,
775 "mod289", SK_ARRAY_COUNT(vec3_Args), vec3_Args,
776 "const vec2 C = vec2(1.0 / 289.0, 289.0);\n"
777 "return x - floor(x * C.xxx) * C.yyy;", &mod289_3_func Name);
778
779 // Add vec4 modulo 289 function
780 static const GrGLShaderVar vec4_Args[] = {
781 GrGLShaderVar("x", kVec4f_GrSLType)
782 };
783
784 SkString mod289_4_funcName;
785 builder->emitFunction(GrGLShaderBuilder::kFragment_ShaderType, kVec4f_GrSLTy pe,
786 "mod289", SK_ARRAY_COUNT(vec4_Args), vec4_Args,
787 "const vec2 C = vec2(1.0 / 289.0, 289.0);\n"
788 "return x - floor(x * C.xxxx) * C.yyyy;", &mod289_4_fu ncName);
789
790 // Add vec4 permute function
791 SkString permute_code;
792 permute_code.appendf("const vec2 C = vec2(34.0, 1.0);\n"
793 "return %s(((x * C.xxxx) + C.yyyy) * x);", mod289_4_fun cName.c_str());
794 SkString permute_funcName;
795 builder->emitFunction(GrGLShaderBuilder::kFragment_ShaderType, kVec4f_GrSLTy pe,
796 "permute", SK_ARRAY_COUNT(vec4_Args), vec4_Args,
797 permute_code.c_str(), &permute_funcName);
798
799 // Add vec4 taylorInvSqrt function
800 SkString taylorInvSqrt_funcName;
801 builder->emitFunction(GrGLShaderBuilder::kFragment_ShaderType, kVec4f_GrSLTy pe,
802 "taylorInvSqrt", SK_ARRAY_COUNT(vec4_Args), vec4_Args,
803 "const vec2 C = vec2(-0.85373472095314, 1.792842914001 59);\n"
804 "return x * C.xxxx + C.yyyy;", &taylorInvSqrt_funcName );
805
806 // Add vec3 noise function
807 static const GrGLShaderVar noise_vec3_Args[] = {
808 GrGLShaderVar("v", kVec3f_GrSLType)
809 };
810
811 SkString noise_code;
812 noise_code.append(
813 "const vec2 C = vec2(1.0/6.0, 1.0/3.0);\n"
814 "const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);\n"
815
816 // First corner
817 "vec3 i = floor(v + dot(v, C.yyy));\n"
818 "vec3 x0 = v - i + dot(i, C.xxx);\n"
819
820 // Other corners
821 "vec3 g = step(x0.yzx, x0.xyz);\n"
822 "vec3 l = 1.0 - g;\n"
823 "vec3 i1 = min(g.xyz, l.zxy);\n"
824 "vec3 i2 = max(g.xyz, l.zxy);\n"
825
826 "vec3 x1 = x0 - i1 + C.xxx;\n"
827 "vec3 x2 = x0 - i2 + C.yyy;\n" // 2.0*C.x = 1/3 = C.y
828 "vec3 x3 = x0 - D.yyy;\n" // -1.0+3.0*C.x = -0.5 = -D.y
829 );
830
831 noise_code.appendf(
832 // Permutations
833 "i = %s(i);\n"
834 "vec4 p = %s(%s(%s(\n"
835 " i.z + vec4(0.0, i1.z, i2.z, 1.0)) +\n"
836 " i.y + vec4(0.0, i1.y, i2.y, 1.0)) +\n"
837 " i.x + vec4(0.0, i1.x, i2.x, 1.0));\n",
838 mod289_3_funcName.c_str(), permute_funcName.c_str(), permute_funcName.c_ str(),
839 permute_funcName.c_str());
840
841 noise_code.append(
842 // Gradients: 7x7 points over a square, mapped onto an octahedron.
843 // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
844 "float n_ = 0.142857142857;\n" // 1.0/7.0
845 "vec3 ns = n_ * D.wyz - D.xzx;\n"
846
847 "vec4 j = p - 49.0 * floor(p * ns.z * ns.z);\n" // mod(p,7*7)
848
849 "vec4 x_ = floor(j * ns.z);\n"
850 "vec4 y_ = floor(j - 7.0 * x_);" // mod(j,N)
851
852 "vec4 x = x_ *ns.x + ns.yyyy;\n"
853 "vec4 y = y_ *ns.x + ns.yyyy;\n"
854 "vec4 h = 1.0 - abs(x) - abs(y);\n"
855
856 "vec4 b0 = vec4(x.xy, y.xy);\n"
857 "vec4 b1 = vec4(x.zw, y.zw);\n"
858 );
859
860 noise_code.append(
861 "vec4 s0 = floor(b0) * 2.0 + 1.0;\n"
862 "vec4 s1 = floor(b1) * 2.0 + 1.0;\n"
863 "vec4 sh = -step(h, vec4(0.0));\n"
864
865 "vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;\n"
866 "vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;\n"
867
868 "vec3 p0 = vec3(a0.xy, h.x);\n"
869 "vec3 p1 = vec3(a0.zw, h.y);\n"
870 "vec3 p2 = vec3(a1.xy, h.z);\n"
871 "vec3 p3 = vec3(a1.zw, h.w);\n"
872 );
873
874 noise_code.appendf(
875 // Normalise gradients
876 "vec4 norm = %s(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));\ n"
877 "p0 *= norm.x;\n"
878 "p1 *= norm.y;\n"
879 "p2 *= norm.z;\n"
880 "p3 *= norm.w;\n"
881
882 // Mix final noise value
883 "vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)) , 0.0);\n"
884 "m = m * m;\n"
885 "return 42.0 * dot(m*m, vec4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3, x3)));",
886 taylorInvSqrt_funcName.c_str());
887
888 SkString noise_funcName;
889 builder->emitFunction(GrGLShaderBuilder::kFragment_ShaderType, kFloat_GrSLTy pe,
890 "snoise", SK_ARRAY_COUNT(noise_vec3_Args), noise_vec3_ Args,
891 noise_code.c_str(), &noise_funcName);
892
893 const char* noiseVecIni = "noiseVecIni";
894 const char* factors = "factors";
895 const char* sum = "sum";
896 const char* xOffsets = "xOffsets";
897 const char* yOffsets = "yOffsets";
898 const char* channel = "channel";
899
900 // Fill with some prime numbers
901 builder->fsCodeAppendf("\t\tconst vec4 %s = vec4(13.0, 53.0, 101.0, 151.0);\ n", xOffsets);
902 builder->fsCodeAppendf("\t\tconst vec4 %s = vec4(109.0, 167.0, 23.0, 67.0);\ n", yOffsets);
903
904 // There are rounding errors if the floor operation is not performed here
905 builder->fsCodeAppendf(
906 "\t\tvec3 %s = vec3(floor((%s*vec3(%s, 1.0)).xy) * vec2(0.66) * %s, 0.0) ;\n",
907 noiseVecIni, invMatrixUni, vCoords, baseFrequencyUni);
908
909 // Perturb the texcoords with three components of noise
910 builder->fsCodeAppendf("\t\t%s += 0.1 * vec3(%s(%s + vec3( 0.0, 0.0, %s)) ,"
911 "%s(%s + vec3( 43.0, 17.0, %s)) ,"
912 "%s(%s + vec3(-17.0, -43.0, %s)) );\n",
913 noiseVecIni, noise_funcName.c_str(), noiseVecIni, see dUni,
914 noise_funcName.c_str(), noiseVecIni, see dUni,
915 noise_funcName.c_str(), noiseVecIni, see dUni);
916
917 builder->fsCodeAppendf("\t\t%s = vec4(0.0);\n", outputColor);
918
919 builder->fsCodeAppendf("\t\tvec3 %s = vec3(1.0);\n", factors);
920 builder->fsCodeAppendf("\t\tfloat %s = 0.0;\n", sum);
921
922 // Loop over all octaves
923 builder->fsCodeAppendf("\t\tfor (int octave = 0; octave < %d; ++octave) {\n" , fNumOctaves);
924
925 // Loop over the 4 channels
926 builder->fsCodeAppendf("\t\t\tfor (int %s = 3; %s >= 0; --%s) {\n", channel, channel, channel);
927
928 builder->fsCodeAppendf(
929 "\t\t\t\t%s[channel] += %s.x * %s(%s * %s.yyy - vec3(%s[%s], %s[%s], %s * %s.z));\n",
930 outputColor, factors, noise_funcName.c_str(), noiseVecIni, factors, xOff sets, channel,
931 yOffsets, channel, seedUni, factors);
932
933 builder->fsCodeAppend("\t\t\t}\n"); // end of the for loop on channels
934
935 builder->fsCodeAppendf("\t\t\t%s += %s.x;\n", sum, factors);
936 builder->fsCodeAppendf("\t\t\t%s *= vec3(0.5, 2.0, 0.75);\n", factors);
937
938 builder->fsCodeAppend("\t\t}\n"); // end of the for loop on octaves
939
940 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
941 // The value of turbulenceFunctionResult comes from ((turbulenceFunction Result) + 1) / 2
942 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
943 builder->fsCodeAppendf("\t\t%s = %s * vec4(0.5 / %s) + vec4(0.5);\n",
944 outputColor, outputColor, sum);
945 } else {
946 builder->fsCodeAppendf("\t\t%s = abs(%s / vec4(%s));\n",
947 outputColor, outputColor, sum);
948 }
949
950 builder->fsCodeAppendf("\t\t%s.a *= %s;\n", outputColor, alphaUni);
951
952 // Clamp values
953 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outpu tColor);
954
955 // Pre-multiply the result
956 builder->fsCodeAppendf("\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
957 outputColor, outputColor, outputColor, outputColor);
958 }
959
647 void GrGLPerlinNoise::emitCode(GrGLShaderBuilder* builder, 960 void GrGLPerlinNoise::emitCode(GrGLShaderBuilder* builder,
648 const GrDrawEffect&, 961 const GrDrawEffect&,
649 EffectKey key, 962 EffectKey key,
650 const char* outputColor, 963 const char* outputColor,
651 const char* inputColor, 964 const char* inputColor,
652 const TextureSamplerArray& samplers) { 965 const TextureSamplerArray& samplers) {
653 sk_ignore_unused_variable(inputColor); 966 sk_ignore_unused_variable(inputColor);
654 967
655 const char* vCoords; 968 const char* vCoords;
656 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &vCoords); 969 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &vCoords);
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 builder->fsCodeAppendf("\t\t%s.a *= %s;", outputColor, alphaUni); 1160 builder->fsCodeAppendf("\t\t%s.a *= %s;", outputColor, alphaUni);
848 1161
849 // Clamp values 1162 // Clamp values
850 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputC olor); 1163 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputC olor);
851 1164
852 // Pre-multiply the result 1165 // Pre-multiply the result
853 builder->fsCodeAppendf("\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", 1166 builder->fsCodeAppendf("\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
854 outputColor, outputColor, outputColor, outputColor); 1167 outputColor, outputColor, outputColor, outputColor);
855 } 1168 }
856 1169
857 GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendEffectFactory& factory, 1170 GrGLNoise::GrGLNoise(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
858 const GrDrawEffect& drawEffect)
859 : INHERITED (factory) 1171 : INHERITED (factory)
860 , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type()) 1172 , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type())
861 , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles()) 1173 , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles())
862 , fNumOctaves(drawEffect.castEffect<GrPerlinNoiseEffect>().numOctaves()) 1174 , fNumOctaves(drawEffect.castEffect<GrPerlinNoiseEffect>().numOctaves())
863 , fEffectMatrix(drawEffect.castEffect<GrPerlinNoiseEffect>().coordsType()) { 1175 , fEffectMatrix(drawEffect.castEffect<GrPerlinNoiseEffect>().coordsType()) {
864 } 1176 }
865 1177
866 GrGLEffect::EffectKey GrGLPerlinNoise::GenKey(const GrDrawEffect& drawEffect, co nst GrGLCaps&) { 1178 GrGLEffect::EffectKey GrGLNoise::GenKey(const GrDrawEffect& drawEffect, const Gr GLCaps&) {
867 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseE ffect>(); 1179 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseE ffect>();
868 1180
869 EffectKey key = turbulence.numOctaves(); 1181 EffectKey key = turbulence.numOctaves();
870 1182
871 key = key << 3; // Make room for next 3 bits 1183 key = key << 3; // Make room for next 3 bits
872 1184
873 switch (turbulence.type()) { 1185 switch (turbulence.type()) {
874 case SkPerlinNoiseShader::kFractalNoise_Type: 1186 case SkPerlinNoiseShader::kFractalNoise_Type:
875 key |= 0x1; 1187 key |= 0x1;
876 break; 1188 break;
(...skipping 10 matching lines...) Expand all
887 } 1199 }
888 1200
889 key = key << GrGLEffectMatrix::kKeyBits; 1201 key = key << GrGLEffectMatrix::kKeyBits;
890 1202
891 SkMatrix m = turbulence.matrix(); 1203 SkMatrix m = turbulence.matrix();
892 m.postTranslate(SK_Scalar1, SK_Scalar1); 1204 m.postTranslate(SK_Scalar1, SK_Scalar1);
893 return key | GrGLEffectMatrix::GenKey(m, drawEffect, 1205 return key | GrGLEffectMatrix::GenKey(m, drawEffect,
894 drawEffect.castEffect<GrPerlinNoiseEffect>().coordsType(), NULL ); 1206 drawEffect.castEffect<GrPerlinNoiseEffect>().coordsType(), NULL );
895 } 1207 }
896 1208
897 void GrGLPerlinNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect & drawEffect) { 1209 void GrGLNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect& draw Effect) {
898 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseE ffect>(); 1210 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseE ffect>();
899 1211
900 const SkVector& baseFrequency = turbulence.baseFrequency(); 1212 const SkVector& baseFrequency = turbulence.baseFrequency();
901 uman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); 1213 uman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
902 if (turbulence.stitchTiles()) {
903 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat a();
904 uman.set4f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
905 SkIntToScalar(stitchData.fWrapX),
906 SkIntToScalar(stitchData.fHeight),
907 SkIntToScalar(stitchData.fWrapY));
908 }
909
910 uman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntTo Scalar(255))); 1214 uman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntTo Scalar(255)));
911 1215
912 SkMatrix m = turbulence.matrix(); 1216 SkMatrix m = turbulence.matrix();
913 SkMatrix invM; 1217 SkMatrix invM;
914 if (!m.invert(&invM)) { 1218 if (!m.invert(&invM)) {
915 invM.reset(); 1219 invM.reset();
916 } else { 1220 } else {
917 invM.postConcat(invM); // Square the matrix 1221 invM.postConcat(invM); // Square the matrix
918 } 1222 }
919 uman.setSkMatrix(fInvMatrixUni, invM); 1223 uman.setSkMatrix(fInvMatrixUni, invM);
920 1224
921 // This (1,1) translation is due to WebKit's 1 based coordinates for the noi se 1225 // This (1,1) translation is due to WebKit's 1 based coordinates for the noi se
922 // (as opposed to 0 based, usually). The same adjustment is in the shadeSpan () functions. 1226 // (as opposed to 0 based, usually). The same adjustment is in the shadeSpan () functions.
923 m.postTranslate(SK_Scalar1, SK_Scalar1); 1227 m.postTranslate(SK_Scalar1, SK_Scalar1);
924 fEffectMatrix.setData(uman, m, drawEffect, NULL); 1228 fEffectMatrix.setData(uman, m, drawEffect, NULL);
925 } 1229 }
926 1230
1231 void GrGLPerlinNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect & drawEffect) {
1232 INHERITED::setData(uman, drawEffect);
1233
1234 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseE ffect>();
1235 if (turbulence.stitchTiles()) {
1236 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat a();
1237 uman.set4f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
1238 SkIntToScalar(stitchData.fWrapX),
1239 SkIntToScalar(stitchData.fHeight),
1240 SkIntToScalar(stitchData.fWrapY));
1241 }
1242 }
1243
1244 void GrGLSimplexNoise::setData(const GrGLUniformManager& uman, const GrDrawEffec t& drawEffect) {
1245 INHERITED::setData(uman, drawEffect);
1246
1247 const GrSimplexNoiseEffect& turbulence = drawEffect.castEffect<GrSimplexNois eEffect>();
1248 uman.set1f(fSeedUni, turbulence.seed());
1249 }
1250
927 ///////////////////////////////////////////////////////////////////// 1251 /////////////////////////////////////////////////////////////////////
928 1252
929 GrEffectRef* SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint) const { 1253 GrEffectRef* SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint) const {
930 #if 0
931 SkASSERT(NULL != context); 1254 SkASSERT(NULL != context);
932 1255
933 // Either we don't stitch tiles, either we have a valid tile size 1256 // Either we don't stitch tiles, either we have a valid tile size
934 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); 1257 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
935 1258
1259 #ifdef SK_USE_SIMPLEX_NOISE
1260 // Simplex noise is currently disabled but can be enabled by defining SK_USE _SIMPLEX_NOISE
1261 sk_ignore_unused_variable(context);
1262 GrEffectRef* effect =
1263 GrSimplexNoiseEffect::Create(fType, fPaintingData->fBaseFrequency,
1264 fNumOctaves, fStitchTiles, fSeed,
1265 this->getLocalMatrix(), paint.getAlpha());
1266 #else
936 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture( 1267 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture(
937 context, *fPaintingData->getPermutationsBitmap(), NULL); 1268 context, *fPaintingData->getPermutationsBitmap(), NULL);
938 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture( 1269 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture(
939 context, *fPaintingData->getNoiseBitmap(), NULL); 1270 context, *fPaintingData->getNoiseBitmap(), NULL);
940 1271
941 GrEffectRef* effect = (NULL != permutationsTexture) && (NULL != noiseTexture ) ? 1272 GrEffectRef* effect = (NULL != permutationsTexture) && (NULL != noiseTexture ) ?
942 GrPerlinNoiseEffect::Create(fType, fPaintingData->fBaseFrequency, 1273 GrPerlinNoiseEffect::Create(fType, fPaintingData->fBaseFrequency,
943 fNumOctaves, fStitchTiles, 1274 fNumOctaves, fStitchTiles,
944 fPaintingData->fStitchDataInit, 1275 fPaintingData->fStitchDataInit,
945 permutationsTexture, noiseTexture, 1276 permutationsTexture, noiseTexture,
946 this->getLocalMatrix(), paint.getAlpha()) : 1277 this->getLocalMatrix(), paint.getAlpha()) :
947 NULL; 1278 NULL;
948 1279
949 // Unlock immediately, this is not great, but we don't have a way of 1280 // Unlock immediately, this is not great, but we don't have a way of
950 // knowing when else to unlock it currently. TODO: Remove this when 1281 // knowing when else to unlock it currently. TODO: Remove this when
951 // unref becomes the unlock replacement for all types of textures. 1282 // unref becomes the unlock replacement for all types of textures.
952 if (NULL != permutationsTexture) { 1283 if (NULL != permutationsTexture) {
953 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture); 1284 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture);
954 } 1285 }
955 if (NULL != noiseTexture) { 1286 if (NULL != noiseTexture) {
956 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture); 1287 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture);
957 } 1288 }
1289 #endif
958 1290
959 return effect; 1291 return effect;
960 #else
961 sk_ignore_unused_variable(context);
962 sk_ignore_unused_variable(paint);
963 return NULL;
964 #endif
965 } 1292 }
966 1293
967 #else 1294 #else
968 1295
969 GrEffectRef* SkPerlinNoiseShader::asNewEffect(GrContext*, const SkPaint&) const { 1296 GrEffectRef* SkPerlinNoiseShader::asNewEffect(GrContext*, const SkPaint&) const {
970 SkDEBUGFAIL("Should not call in GPU-less build"); 1297 SkDEBUGFAIL("Should not call in GPU-less build");
971 return NULL; 1298 return NULL;
972 } 1299 }
973 1300
974 #endif 1301 #endif
(...skipping 23 matching lines...) Expand all
998 str->append(" seed: "); 1325 str->append(" seed: ");
999 str->appendScalar(fSeed); 1326 str->appendScalar(fSeed);
1000 str->append(" stitch tiles: "); 1327 str->append(" stitch tiles: ");
1001 str->append(fStitchTiles ? "true " : "false "); 1328 str->append(fStitchTiles ? "true " : "false ");
1002 1329
1003 this->INHERITED::toString(str); 1330 this->INHERITED::toString(str);
1004 1331
1005 str->append(")"); 1332 str->append(")");
1006 } 1333 }
1007 #endif 1334 #endif
OLDNEW
« bench/PerlinNoiseBench.cpp ('K') | « gyp/bench.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698