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

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

Powered by Google App Engine
This is Rietveld 408576698