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 "SkDither.h" | 8 #include "SkDither.h" |
9 #include "SkPerlinNoiseShader2.h" | 9 #include "SkPerlinNoiseShader2.h" |
10 #include "SkColorFilter.h" | 10 #include "SkColorFilter.h" |
(...skipping 801 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 // Get texture coordinates and normalize | 812 // Get texture coordinates and normalize |
813 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n", | 813 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n", |
814 floorVal, floorVal); | 814 floorVal, floorVal); |
815 | 815 |
816 // Get permutation for x | 816 // Get permutation for x |
817 { | 817 { |
818 SkString xCoords(""); | 818 SkString xCoords(""); |
819 xCoords.appendf("vec2(%s.x, 0.5)", floorVal); | 819 xCoords.appendf("vec2(%s.x, 0.5)", floorVal); |
820 | 820 |
821 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); | 821 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); |
822 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_
str(), | 822 fsBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords
.c_str(), |
823 kVec2f_GrSLType); | 823 kVec2f_GrSLType); |
824 noiseCode.append(".r;"); | 824 noiseCode.append(".r;"); |
825 } | 825 } |
826 | 826 |
827 // Get permutation for x + 1 | 827 // Get permutation for x + 1 |
828 { | 828 { |
829 SkString xCoords(""); | 829 SkString xCoords(""); |
830 xCoords.appendf("vec2(%s.z, 0.5)", floorVal); | 830 xCoords.appendf("vec2(%s.z, 0.5)", floorVal); |
831 | 831 |
832 noiseCode.appendf("\n\t%s.y = ", latticeIdx); | 832 noiseCode.appendf("\n\t%s.y = ", latticeIdx); |
833 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_
str(), | 833 fsBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords
.c_str(), |
834 kVec2f_GrSLType); | 834 kVec2f_GrSLType); |
835 noiseCode.append(".r;"); | 835 noiseCode.append(".r;"); |
836 } | 836 } |
837 | 837 |
838 #if defined(SK_BUILD_FOR_ANDROID) | 838 #if defined(SK_BUILD_FOR_ANDROID) |
839 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Ne
xus 7 (Tegra 3). | 839 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Ne
xus 7 (Tegra 3). |
840 // The issue is that colors aren't accurate enough on Tegra devices. For exa
mple, if an 8 bit | 840 // The issue is that colors aren't accurate enough on Tegra devices. For exa
mple, if an 8 bit |
841 // value of 124 (or 0.486275 here) is entered, we can get a texture value of
123.513725 | 841 // value of 124 (or 0.486275 here) is entered, we can get a texture value of
123.513725 |
842 // (or 0.484368 here). The following rounding operation prevents these preci
sion issues from | 842 // (or 0.484368 here). The following rounding operation prevents these preci
sion issues from |
843 // affecting the result of the noise by making sure that we only have multip
les of 1/255. | 843 // affecting the result of the noise by making sure that we only have multip
les of 1/255. |
844 // (Note that 1/255 is about 0.003921569, which is the value used here). | 844 // (Note that 1/255 is about 0.003921569, which is the value used here). |
845 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003
921569);", | 845 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003
921569);", |
846 latticeIdx, latticeIdx); | 846 latticeIdx, latticeIdx); |
847 #endif | 847 #endif |
848 | 848 |
849 // Get (x,y) coordinates with the permutated x | 849 // Get (x,y) coordinates with the permutated x |
850 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, lattic
eIdx, floorVal); | 850 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, lattic
eIdx, floorVal); |
851 | 851 |
852 noiseCode.appendf("\n\n\tvec2 %s;", uv); | 852 noiseCode.appendf("\n\n\tvec2 %s;", uv); |
853 // Compute u, at offset (0,0) | 853 // Compute u, at offset (0,0) |
854 { | 854 { |
855 SkString latticeCoords(""); | 855 SkString latticeCoords(""); |
856 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord); | 856 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord); |
857 noiseCode.appendf("\n\tvec4 %s = ", lattice); | 857 noiseCode.appendf("\n\tvec4 %s = ", lattice); |
858 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoo
rds.c_str(), | 858 fsBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], lattice
Coords.c_str(), |
859 kVec2f_GrSLType); | 859 kVec2f_GrSLType); |
860 noiseCode.appendf(".bgra;\n\t%s.x = ", uv); | 860 noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
861 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); | 861 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
862 } | 862 } |
863 | 863 |
864 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); | 864 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); |
865 // Compute v, at offset (-1,0) | 865 // Compute v, at offset (-1,0) |
866 { | 866 { |
867 SkString latticeCoords(""); | 867 SkString latticeCoords(""); |
868 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord); | 868 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord); |
869 noiseCode.append("\n\tlattice = "); | 869 noiseCode.append("\n\tlattice = "); |
870 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoo
rds.c_str(), | 870 fsBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], lattice
Coords.c_str(), |
871 kVec2f_GrSLType); | 871 kVec2f_GrSLType); |
872 noiseCode.appendf(".bgra;\n\t%s.y = ", uv); | 872 noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
873 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); | 873 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
874 } | 874 } |
875 | 875 |
876 // Compute 'a' as a linear interpolation of 'u' and 'v' | 876 // Compute 'a' as a linear interpolation of 'u' and 'v' |
877 noiseCode.appendf("\n\tvec2 %s;", ab); | 877 noiseCode.appendf("\n\tvec2 %s;", ab); |
878 noiseCode.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmoo
th); | 878 noiseCode.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmoo
th); |
879 | 879 |
880 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); | 880 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); |
881 // Compute v, at offset (-1,-1) | 881 // Compute v, at offset (-1,-1) |
882 { | 882 { |
883 SkString latticeCoords(""); | 883 SkString latticeCoords(""); |
884 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord); | 884 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord); |
885 noiseCode.append("\n\tlattice = "); | 885 noiseCode.append("\n\tlattice = "); |
886 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoo
rds.c_str(), | 886 fsBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], lattice
Coords.c_str(), |
887 kVec2f_GrSLType); | 887 kVec2f_GrSLType); |
888 noiseCode.appendf(".bgra;\n\t%s.y = ", uv); | 888 noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
889 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); | 889 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
890 } | 890 } |
891 | 891 |
892 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); | 892 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); |
893 // Compute u, at offset (0,-1) | 893 // Compute u, at offset (0,-1) |
894 { | 894 { |
895 SkString latticeCoords(""); | 895 SkString latticeCoords(""); |
896 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord); | 896 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord); |
897 noiseCode.append("\n\tlattice = "); | 897 noiseCode.append("\n\tlattice = "); |
898 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoo
rds.c_str(), | 898 fsBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], lattice
Coords.c_str(), |
899 kVec2f_GrSLType); | 899 kVec2f_GrSLType); |
900 noiseCode.appendf(".bgra;\n\t%s.x = ", uv); | 900 noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
901 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); | 901 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
902 } | 902 } |
903 | 903 |
904 // Compute 'b' as a linear interpolation of 'u' and 'v' | 904 // Compute 'b' as a linear interpolation of 'u' and 'v' |
905 noiseCode.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmoo
th); | 905 noiseCode.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmoo
th); |
906 // Compute the noise as a linear interpolation of 'a' and 'b' | 906 // Compute the noise as a linear interpolation of 'a' and 'b' |
907 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth
); | 907 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth
); |
908 | 908 |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1168 &fadeFuncName); | 1168 &fadeFuncName); |
1169 | 1169 |
1170 // perm function | 1170 // perm function |
1171 static const GrGLSLShaderVar permArgs[] = { | 1171 static const GrGLSLShaderVar permArgs[] = { |
1172 GrGLSLShaderVar("x", kFloat_GrSLType) | 1172 GrGLSLShaderVar("x", kFloat_GrSLType) |
1173 }; | 1173 }; |
1174 SkString permFuncName; | 1174 SkString permFuncName; |
1175 SkString permCode("return "); | 1175 SkString permCode("return "); |
1176 // FIXME even though I'm creating these textures with kRepeat_TileMode, they
're clamped. Not | 1176 // FIXME even though I'm creating these textures with kRepeat_TileMode, they
're clamped. Not |
1177 // sure why. Using fract() (here and the next texture lookup) as a workaroun
d. | 1177 // sure why. Using fract() (here and the next texture lookup) as a workaroun
d. |
1178 fsBuilder->appendTextureLookup(&permCode, args.fSamplers[0], "vec2(fract(x /
256.0), 0.0)", | 1178 fsBuilder->appendTextureLookup(&permCode, args.fTexSamplers[0], "vec2(fract(
x / 256.0), 0.0)", |
1179 kVec2f_GrSLType); | 1179 kVec2f_GrSLType); |
1180 permCode.append(".r * 255.0;"); | 1180 permCode.append(".r * 255.0;"); |
1181 fsBuilder->emitFunction(kFloat_GrSLType, "perm", SK_ARRAY_COUNT(permArgs), p
ermArgs, | 1181 fsBuilder->emitFunction(kFloat_GrSLType, "perm", SK_ARRAY_COUNT(permArgs), p
ermArgs, |
1182 permCode.c_str(), &permFuncName); | 1182 permCode.c_str(), &permFuncName); |
1183 | 1183 |
1184 // grad function | 1184 // grad function |
1185 static const GrGLSLShaderVar gradArgs[] = { | 1185 static const GrGLSLShaderVar gradArgs[] = { |
1186 GrGLSLShaderVar("x", kFloat_GrSLType), | 1186 GrGLSLShaderVar("x", kFloat_GrSLType), |
1187 GrGLSLShaderVar("p", kVec3f_GrSLType) | 1187 GrGLSLShaderVar("p", kVec3f_GrSLType) |
1188 }; | 1188 }; |
1189 SkString gradFuncName; | 1189 SkString gradFuncName; |
1190 SkString gradCode("return dot("); | 1190 SkString gradCode("return dot("); |
1191 fsBuilder->appendTextureLookup(&gradCode, args.fSamplers[1], "vec2(fract(x /
16.0), 0.0)", | 1191 fsBuilder->appendTextureLookup(&gradCode, args.fTexSamplers[1], "vec2(fract(
x / 16.0), 0.0)", |
1192 kVec2f_GrSLType); | 1192 kVec2f_GrSLType); |
1193 gradCode.append(".rgb * 255.0 - vec3(1.0), p);"); | 1193 gradCode.append(".rgb * 255.0 - vec3(1.0), p);"); |
1194 fsBuilder->emitFunction(kFloat_GrSLType, "grad", SK_ARRAY_COUNT(gradArgs), g
radArgs, | 1194 fsBuilder->emitFunction(kFloat_GrSLType, "grad", SK_ARRAY_COUNT(gradArgs), g
radArgs, |
1195 gradCode.c_str(), &gradFuncName); | 1195 gradCode.c_str(), &gradFuncName); |
1196 | 1196 |
1197 // lerp function | 1197 // lerp function |
1198 static const GrGLSLShaderVar lerpArgs[] = { | 1198 static const GrGLSLShaderVar lerpArgs[] = { |
1199 GrGLSLShaderVar("a", kFloat_GrSLType), | 1199 GrGLSLShaderVar("a", kFloat_GrSLType), |
1200 GrGLSLShaderVar("b", kFloat_GrSLType), | 1200 GrGLSLShaderVar("b", kFloat_GrSLType), |
1201 GrGLSLShaderVar("w", kFloat_GrSLType) | 1201 GrGLSLShaderVar("w", kFloat_GrSLType) |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1396 str->append(" seed: "); | 1396 str->append(" seed: "); |
1397 str->appendScalar(fSeed); | 1397 str->appendScalar(fSeed); |
1398 str->append(" stitch tiles: "); | 1398 str->append(" stitch tiles: "); |
1399 str->append(fStitchTiles ? "true " : "false "); | 1399 str->append(fStitchTiles ? "true " : "false "); |
1400 | 1400 |
1401 this->INHERITED::toString(str); | 1401 this->INHERITED::toString(str); |
1402 | 1402 |
1403 str->append(")"); | 1403 str->append(")"); |
1404 } | 1404 } |
1405 #endif | 1405 #endif |
OLD | NEW |