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 "SkPerlinNoiseShader.h" | 8 #include "SkPerlinNoiseShader.h" |
9 #include "SkColorFilter.h" | 9 #include "SkColorFilter.h" |
10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 // Get texture coordinates and normalize | 676 // Get texture coordinates and normalize |
677 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n", | 677 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n", |
678 floorVal, floorVal); | 678 floorVal, floorVal); |
679 | 679 |
680 // Get permutation for x | 680 // Get permutation for x |
681 { | 681 { |
682 SkString xCoords(""); | 682 SkString xCoords(""); |
683 xCoords.appendf("vec2(%s.x, 0.5)", floorVal); | 683 xCoords.appendf("vec2(%s.x, 0.5)", floorVal); |
684 | 684 |
685 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); | 685 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); |
686 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.
c_str(), | 686 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoor
ds.c_str(), |
687 kVec2f_GrSLType); | 687 kVec2f_GrSLType); |
688 noiseCode.append(".r;"); | 688 noiseCode.append(".r;"); |
689 } | 689 } |
690 | 690 |
691 // Get permutation for x + 1 | 691 // Get permutation for x + 1 |
692 { | 692 { |
693 SkString xCoords(""); | 693 SkString xCoords(""); |
694 xCoords.appendf("vec2(%s.z, 0.5)", floorVal); | 694 xCoords.appendf("vec2(%s.z, 0.5)", floorVal); |
695 | 695 |
696 noiseCode.appendf("\n\t%s.y = ", latticeIdx); | 696 noiseCode.appendf("\n\t%s.y = ", latticeIdx); |
697 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.
c_str(), | 697 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoor
ds.c_str(), |
698 kVec2f_GrSLType); | 698 kVec2f_GrSLType); |
699 noiseCode.append(".r;"); | 699 noiseCode.append(".r;"); |
700 } | 700 } |
701 | 701 |
702 #if defined(SK_BUILD_FOR_ANDROID) | 702 #if defined(SK_BUILD_FOR_ANDROID) |
703 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Ne
xus 7 (Tegra 3). | 703 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Ne
xus 7 (Tegra 3). |
704 // The issue is that colors aren't accurate enough on Tegra devices. For exa
mple, if an 8 bit | 704 // The issue is that colors aren't accurate enough on Tegra devices. For exa
mple, if an 8 bit |
705 // value of 124 (or 0.486275 here) is entered, we can get a texture value of
123.513725 | 705 // value of 124 (or 0.486275 here) is entered, we can get a texture value of
123.513725 |
706 // (or 0.484368 here). The following rounding operation prevents these preci
sion issues from | 706 // (or 0.484368 here). The following rounding operation prevents these preci
sion issues from |
707 // affecting the result of the noise by making sure that we only have multip
les of 1/255. | 707 // affecting the result of the noise by making sure that we only have multip
les of 1/255. |
708 // (Note that 1/255 is about 0.003921569, which is the value used here). | 708 // (Note that 1/255 is about 0.003921569, which is the value used here). |
709 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003
921569);", | 709 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003
921569);", |
710 latticeIdx, latticeIdx); | 710 latticeIdx, latticeIdx); |
711 #endif | 711 #endif |
712 | 712 |
713 // Get (x,y) coordinates with the permutated x | 713 // Get (x,y) coordinates with the permutated x |
714 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, lattic
eIdx, floorVal); | 714 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, lattic
eIdx, floorVal); |
715 | 715 |
716 noiseCode.appendf("\n\n\tvec2 %s;", uv); | 716 noiseCode.appendf("\n\n\tvec2 %s;", uv); |
717 // Compute u, at offset (0,0) | 717 // Compute u, at offset (0,0) |
718 { | 718 { |
719 SkString latticeCoords(""); | 719 SkString latticeCoords(""); |
720 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord); | 720 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord); |
721 noiseCode.appendf("\n\tvec4 %s = ", lattice); | 721 noiseCode.appendf("\n\tvec4 %s = ", lattice); |
722 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeC
oords.c_str(), | 722 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latti
ceCoords.c_str(), |
723 kVec2f_GrSLType); | 723 kVec2f_GrSLType); |
724 noiseCode.appendf(".bgra;\n\t%s.x = ", uv); | 724 noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
725 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); | 725 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
726 } | 726 } |
727 | 727 |
728 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); | 728 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); |
729 // Compute v, at offset (-1,0) | 729 // Compute v, at offset (-1,0) |
730 { | 730 { |
731 SkString latticeCoords(""); | 731 SkString latticeCoords(""); |
732 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord); | 732 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord); |
733 noiseCode.append("\n\tlattice = "); | 733 noiseCode.append("\n\tlattice = "); |
734 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeC
oords.c_str(), | 734 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latti
ceCoords.c_str(), |
735 kVec2f_GrSLType); | 735 kVec2f_GrSLType); |
736 noiseCode.appendf(".bgra;\n\t%s.y = ", uv); | 736 noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
737 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); | 737 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
738 } | 738 } |
739 | 739 |
740 // Compute 'a' as a linear interpolation of 'u' and 'v' | 740 // Compute 'a' as a linear interpolation of 'u' and 'v' |
741 noiseCode.appendf("\n\tvec2 %s;", ab); | 741 noiseCode.appendf("\n\tvec2 %s;", ab); |
742 noiseCode.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmoo
th); | 742 noiseCode.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmoo
th); |
743 | 743 |
744 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); | 744 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); |
745 // Compute v, at offset (-1,-1) | 745 // Compute v, at offset (-1,-1) |
746 { | 746 { |
747 SkString latticeCoords(""); | 747 SkString latticeCoords(""); |
748 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord); | 748 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord); |
749 noiseCode.append("\n\tlattice = "); | 749 noiseCode.append("\n\tlattice = "); |
750 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeC
oords.c_str(), | 750 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latti
ceCoords.c_str(), |
751 kVec2f_GrSLType); | 751 kVec2f_GrSLType); |
752 noiseCode.appendf(".bgra;\n\t%s.y = ", uv); | 752 noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
753 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); | 753 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
754 } | 754 } |
755 | 755 |
756 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); | 756 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); |
757 // Compute u, at offset (0,-1) | 757 // Compute u, at offset (0,-1) |
758 { | 758 { |
759 SkString latticeCoords(""); | 759 SkString latticeCoords(""); |
760 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord); | 760 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord); |
761 noiseCode.append("\n\tlattice = "); | 761 noiseCode.append("\n\tlattice = "); |
762 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeC
oords.c_str(), | 762 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latti
ceCoords.c_str(), |
763 kVec2f_GrSLType); | 763 kVec2f_GrSLType); |
764 noiseCode.appendf(".bgra;\n\t%s.x = ", uv); | 764 noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
765 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); | 765 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
766 } | 766 } |
767 | 767 |
768 // Compute 'b' as a linear interpolation of 'u' and 'v' | 768 // Compute 'b' as a linear interpolation of 'u' and 'v' |
769 noiseCode.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmoo
th); | 769 noiseCode.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmoo
th); |
770 // Compute the noise as a linear interpolation of 'a' and 'b' | 770 // Compute the noise as a linear interpolation of 'a' and 'b' |
771 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth
); | 771 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth
); |
772 | 772 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 str->append(" seed: "); | 975 str->append(" seed: "); |
976 str->appendScalar(fSeed); | 976 str->appendScalar(fSeed); |
977 str->append(" stitch tiles: "); | 977 str->append(" stitch tiles: "); |
978 str->append(fStitchTiles ? "true " : "false "); | 978 str->append(fStitchTiles ? "true " : "false "); |
979 | 979 |
980 this->INHERITED::toString(str); | 980 this->INHERITED::toString(str); |
981 | 981 |
982 str->append(")"); | 982 str->append(")"); |
983 } | 983 } |
984 #endif | 984 #endif |
OLD | NEW |