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

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

Issue 15753005: Fixed gpu perlin noise on Windows (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 6 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 | « no previous file | 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 && !defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_WI N) 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 // CPU noise is faster on Android, so the GPU implementation is only for desktop
499 499
500 #include "GrTBackendEffectFactory.h" 500 #include "GrTBackendEffectFactory.h"
501 501
502 class GrGLNoise : public GrGLEffect { 502 class GrGLNoise : public GrGLEffect {
503 public: 503 public:
504 GrGLNoise(const GrBackendEffectFactory& factory, 504 GrGLNoise(const GrBackendEffectFactory& factory,
505 const GrDrawEffect& drawEffect); 505 const GrDrawEffect& drawEffect);
506 virtual ~GrGLNoise() {} 506 virtual ~GrGLNoise() {}
507 507
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after
979 kFloat_GrSLType, "alpha"); 979 kFloat_GrSLType, "alpha");
980 const char* alphaUni = builder->getUniformCStr(fAlphaUni); 980 const char* alphaUni = builder->getUniformCStr(fAlphaUni);
981 981
982 const char* stitchDataUni = NULL; 982 const char* stitchDataUni = NULL;
983 if (fStitchTiles) { 983 if (fStitchTiles) {
984 fStitchDataUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader Type, 984 fStitchDataUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader Type,
985 kVec4f_GrSLType, "stitchData"); 985 kVec4f_GrSLType, "stitchData");
986 stitchDataUni = builder->getUniformCStr(fStitchDataUni); 986 stitchDataUni = builder->getUniformCStr(fStitchDataUni);
987 } 987 }
988 988
989 const char* chanCoords = "chanCoords"; 989 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
990 const char* chanCoordR = "0.125";
991 const char* chanCoordG = "0.375";
992 const char* chanCoordB = "0.625";
993 const char* chanCoordA = "0.875";
994 const char* chanCoord = "chanCoord";
990 const char* stitchData = "stitchData"; 995 const char* stitchData = "stitchData";
991 const char* ratio = "ratio"; 996 const char* ratio = "ratio";
992 const char* noise = "noise";
993 const char* noiseXY = "noiseXY"; 997 const char* noiseXY = "noiseXY";
994 const char* noiseVec = "noiseVec"; 998 const char* noiseVec = "noiseVec";
995 const char* noiseVecIni = "noiseVecIni";
996 const char* noiseSmooth = "noiseSmooth"; 999 const char* noiseSmooth = "noiseSmooth";
997 const char* fractVal = "fractVal"; 1000 const char* fractVal = "fractVal";
998 const char* uv = "uv"; 1001 const char* uv = "uv";
999 const char* ab = "ab"; 1002 const char* ab = "ab";
1000 const char* latticeIdx = "latticeIdx"; 1003 const char* latticeIdx = "latticeIdx";
1001 const char* lattice = "lattice"; 1004 const char* lattice = "lattice";
1002 const char* perlinNoise = "4096.0"; 1005 const char* perlinNoise = "4096.0";
1003 const char* inc8bit = "0.00390625"; // 1.0 / 256.0 1006 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
1004 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a 1007 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
1005 // [-1,1] vector and perform a dot product between that vector and the provi ded vector. 1008 // [-1,1] vector and perform a dot product between that vector and the provi ded vector.
1006 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec 2(1.0)), %s);"; 1009 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec 2(1.0)), %s);";
1007 1010
1008 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 1011 // Add noise function
1009 builder->fsCodeAppendf("\t\tconst vec4 %s = vec4(0.125, 0.375, 0.625, 0.875) ;", chanCoords); 1012 static const GrGLShaderVar perlinnoise_Args[] = {
bsalomon 2013/06/10 13:39:25 We already have two conventions for static const g
1013 GrGLShaderVar(chanCoord, kFloat_GrSLType),
1014 GrGLShaderVar(noiseVec, kVec2f_GrSLType)
1015 };
1010 1016
1011 // There are rounding errors if the floor operation is not performed here 1017 static const GrGLShaderVar perlinnoise_stitch_Args[] = {
1012 builder->fsCodeAppendf("\t\tvec2 %s = floor((%s*vec3(%s, 1.0)).xy) * %s;", 1018 GrGLShaderVar(chanCoord, kFloat_GrSLType),
1013 noiseVecIni, invMatrixUni, vCoords, baseFrequencyUni) ; 1019 GrGLShaderVar(noiseVec, kVec2f_GrSLType),
1020 GrGLShaderVar(stitchData, kVec4f_GrSLType)
1021 };
1014 1022
1015 // Loop over the 4 channels 1023 SkString noise_code;
bsalomon 2013/06/10 13:39:25 localVarsAreLikeThis.
1016 builder->fsCodeAppend("\t\tfor (int channel = 3; channel >= 0; --channel) {" );
1017 1024
1018 if (fStitchTiles) { 1025 noise_code.appendf(
1019 // Set up TurbulenceInitial stitch values. 1026 "\tvec4 %s = vec4(floor(%s) + vec2(%s), fract(%s));",
1020 builder->fsCodeAppendf("\t\tvec4 %s = %s;", stitchData, stitchDataUni); 1027 noiseXY, noiseVec, perlinNoise, noiseVec);
1021 }
1022
1023 builder->fsCodeAppendf("\t\t%s[channel] = 0.0;", outputColor);
1024
1025 builder->fsCodeAppendf("\t\tfloat %s = 1.0;", ratio);
1026 builder->fsCodeAppendf("\t\tvec2 %s = %s;", noiseVec, noiseVecIni);
1027
1028 // Loop over all octaves
1029 builder->fsCodeAppendf("\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves);
1030
1031 builder->fsCodeAppendf("\t\tvec4 %s = vec4(floor(%s) + vec2(%s), fract(%s)); ",
1032 noiseXY, noiseVec, perlinNoise, noiseVec);
1033 1028
1034 // smooth curve : t * t * (3 - 2 * t) 1029 // smooth curve : t * t * (3 - 2 * t)
1035 builder->fsCodeAppendf("\t\tvec2 %s = %s.zw * %s.zw * (vec2(3.0) - vec2(2.0) * %s.zw);", 1030 noise_code.appendf("\n\tvec2 %s = %s.zw * %s.zw * (vec2(3.0) - vec2(2.0) * % s.zw);",
1036 noiseSmooth, noiseXY, noiseXY, noiseXY); 1031 noiseSmooth, noiseXY, noiseXY, noiseXY);
1037 1032
1038 // Adjust frequencies if we're stitching tiles 1033 // Adjust frequencies if we're stitching tiles
1039 if (fStitchTiles) { 1034 if (fStitchTiles) {
1040 builder->fsCodeAppendf("\t\tif(%s.x >= %s.y) { %s.x -= %s.x; }", 1035 noise_code.appendf("\n\tif(%s.x >= %s.y) { %s.x -= %s.x; }",
1041 noiseXY, stitchData, noiseXY, stitchData); 1036 noiseXY, stitchData, noiseXY, stitchData);
1042 builder->fsCodeAppendf("\t\tif(%s.x >= (%s.y - 1.0)) { %s.x -= (%s.x - 1 .0); }", 1037 noise_code.appendf("\n\tif(%s.x >= (%s.y - 1.0)) { %s.x -= (%s.x - 1.0); }",
1043 noiseXY, stitchData, noiseXY, stitchData); 1038 noiseXY, stitchData, noiseXY, stitchData);
1044 builder->fsCodeAppendf("\t\tif(%s.y >= %s.w) { %s.y -= %s.z; }", 1039 noise_code.appendf("\n\tif(%s.y >= %s.w) { %s.y -= %s.z; }",
1045 noiseXY, stitchData, noiseXY, stitchData); 1040 noiseXY, stitchData, noiseXY, stitchData);
1046 builder->fsCodeAppendf("\t\tif(%s.y >= (%s.w - 1.0)) { %s.y -= (%s.z - 1 .0); }", 1041 noise_code.appendf("\n\tif(%s.y >= (%s.w - 1.0)) { %s.y -= (%s.z - 1.0); }",
1047 noiseXY, stitchData, noiseXY, stitchData); 1042 noiseXY, stitchData, noiseXY, stitchData);
1048 } 1043 }
1049 1044
1050 // Get texture coordinates and normalize 1045 // Get texture coordinates and normalize
1051 builder->fsCodeAppendf("\t\t%s.xy = fract(floor(mod(%s.xy, 256.0)) / vec2(25 6.0));", 1046 noise_code.appendf("\n\t%s.xy = fract(floor(mod(%s.xy, 256.0)) / vec2(256.0) );\n",
1052 noiseXY, noiseXY); 1047 noiseXY, noiseXY);
1053 1048
1054 // Get permutation for x 1049 // Get permutation for x
1055 { 1050 {
1056 SkString xCoords(""); 1051 SkString xCoords("");
1057 xCoords.appendf("vec2(%s.x, 0.5)", noiseXY); 1052 xCoords.appendf("vec2(%s.x, 0.5)", noiseXY);
1058 1053
1059 builder->fsCodeAppendf("\t\tvec2 %s;\t\t%s.x = ", latticeIdx, latticeIdx ); 1054 noise_code.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
1060 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, 1055 builder->appendTextureLookup(&noise_code, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
1061 samplers[0], xCoords.c_str(), kVec2f_GrSLTy pe); 1056 noise_code.append(".r;");
1062 builder->fsCodeAppend(".r;\n");
1063 } 1057 }
1064 1058
1065 // Get permutation for x + 1 1059 // Get permutation for x + 1
1066 { 1060 {
1067 SkString xCoords(""); 1061 SkString xCoords("");
1068 xCoords.appendf("vec2(fract(%s.x + %s), 0.5)", noiseXY, inc8bit); 1062 xCoords.appendf("vec2(fract(%s.x + %s), 0.5)", noiseXY, inc8bit);
1069 1063
1070 builder->fsCodeAppendf("\t\t%s.y = ", latticeIdx); 1064 noise_code.appendf("\n\t%s.y = ", latticeIdx);
1071 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, 1065 builder->appendTextureLookup(&noise_code, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
1072 samplers[0], xCoords.c_str(), kVec2f_GrSLTy pe); 1066 noise_code.append(".r;");
1073 builder->fsCodeAppend(".r;\n");
1074 } 1067 }
1075 1068
1076 // Get (x,y) coordinates with the permutated x 1069 // Get (x,y) coordinates with the permutated x
1077 builder->fsCodeAppendf("\t\t%s = fract(%s + %s.yy);", latticeIdx, latticeIdx , noiseXY); 1070 noise_code.appendf("\n\t%s = fract(%s + %s.yy);", latticeIdx, latticeIdx, no iseXY);
1078 1071
1079 builder->fsCodeAppendf("\t\tvec2 %s = %s.zw;", fractVal, noiseXY); 1072 noise_code.appendf("\n\tvec2 %s = %s.zw;", fractVal, noiseXY);
1080 1073
1081 builder->fsCodeAppendf("\t\tvec2 %s;", uv); 1074 noise_code.appendf("\n\n\tvec2 %s;", uv);
1082 // Compute u, at offset (0,0) 1075 // Compute u, at offset (0,0)
1083 { 1076 {
1084 SkString latticeCoords(""); 1077 SkString latticeCoords("");
1085 latticeCoords.appendf("vec2(%s.x, %s[channel])", latticeIdx, chanCoords) ; 1078 latticeCoords.appendf("vec2(%s.x, %s)", latticeIdx, chanCoord);
1086 builder->fsCodeAppendf("vec4 %s = ", lattice); 1079 noise_code.appendf("\n\tvec4 %s = ", lattice);
1087 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, 1080 builder->appendTextureLookup(&noise_code, samplers[1], latticeCoords.c_s tr(),
1088 samplers[1], latticeCoords.c_str(), kVec2f_ GrSLType); 1081 kVec2f_GrSLType);
1089 builder->fsCodeAppendf(".bgra;\n\t\t%s.x = ", uv); 1082 noise_code.appendf(".bgra;\n\t%s.x = ", uv);
1090 builder->fsCodeAppendf(dotLattice, lattice, lattice, inc8bit, fractVal); 1083 noise_code.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
1091 } 1084 }
1092 1085
1093 builder->fsCodeAppendf("\t\t%s.x -= 1.0;", fractVal); 1086 noise_code.appendf("\n\t%s.x -= 1.0;", fractVal);
1094 // Compute v, at offset (-1,0) 1087 // Compute v, at offset (-1,0)
1095 { 1088 {
1096 SkString latticeCoords(""); 1089 SkString latticeCoords("");
1097 latticeCoords.appendf("vec2(%s.y, %s[channel])", latticeIdx, chanCoords) ; 1090 latticeCoords.appendf("vec2(%s.y, %s)", latticeIdx, chanCoord);
1098 builder->fsCodeAppend("lattice = "); 1091 noise_code.append("lattice = ");
1099 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, 1092 builder->appendTextureLookup(&noise_code, samplers[1], latticeCoords.c_s tr(),
1100 samplers[1], latticeCoords.c_str(), kVec2f_ GrSLType); 1093 kVec2f_GrSLType);
1101 builder->fsCodeAppendf(".bgra;\n\t\t%s.y = ", uv); 1094 noise_code.appendf(".bgra;\n\t%s.y = ", uv);
1102 builder->fsCodeAppendf(dotLattice, lattice, lattice, inc8bit, fractVal); 1095 noise_code.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
1103 } 1096 }
1104 1097
1105 // Compute 'a' as a linear interpolation of 'u' and 'v' 1098 // Compute 'a' as a linear interpolation of 'u' and 'v'
1106 builder->fsCodeAppendf("\t\tvec2 %s;", ab); 1099 noise_code.appendf("\n\tvec2 %s;", ab);
1107 builder->fsCodeAppendf("\t\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, nois eSmooth); 1100 noise_code.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmo oth);
1108 1101
1109 builder->fsCodeAppendf("\t\t%s.y -= 1.0;", fractVal); 1102 noise_code.appendf("\n\t%s.y -= 1.0;", fractVal);
1110 // Compute v, at offset (-1,-1) 1103 // Compute v, at offset (-1,-1)
1111 { 1104 {
1112 SkString latticeCoords(""); 1105 SkString latticeCoords("");
1113 latticeCoords.appendf("vec2(fract(%s.y + %s), %s[channel])", 1106 latticeCoords.appendf("vec2(fract(%s.y + %s), %s)", latticeIdx, inc8bit, chanCoord);
1114 latticeIdx, inc8bit, chanCoords); 1107 noise_code.append("lattice = ");
1115 builder->fsCodeAppend("lattice = "); 1108 builder->appendTextureLookup(&noise_code, samplers[1], latticeCoords.c_s tr(),
1116 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, 1109 kVec2f_GrSLType);
1117 samplers[1], latticeCoords.c_str(), kVec2f_ GrSLType); 1110 noise_code.appendf(".bgra;\n\t%s.y = ", uv);
1118 builder->fsCodeAppendf(".bgra;\n\t\t%s.y = ", uv); 1111 noise_code.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
1119 builder->fsCodeAppendf(dotLattice, lattice, lattice, inc8bit, fractVal);
1120 } 1112 }
1121 1113
1122 builder->fsCodeAppendf("\t\t%s.x += 1.0;", fractVal); 1114 noise_code.appendf("\n\t%s.x += 1.0;", fractVal);
1123 // Compute u, at offset (0,-1) 1115 // Compute u, at offset (0,-1)
1124 { 1116 {
1125 SkString latticeCoords(""); 1117 SkString latticeCoords("");
1126 latticeCoords.appendf("vec2(fract(%s.x + %s), %s[channel])", 1118 latticeCoords.appendf("vec2(fract(%s.x + %s), %s)", latticeIdx, inc8bit, chanCoord);
1127 latticeIdx, inc8bit, chanCoords); 1119 noise_code.append("lattice = ");
1128 builder->fsCodeAppend("lattice = "); 1120 builder->appendTextureLookup(&noise_code, samplers[1], latticeCoords.c_s tr(),
1129 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, 1121 kVec2f_GrSLType);
1130 samplers[1], latticeCoords.c_str(), kVec2f_ GrSLType); 1122 noise_code.appendf(".bgra;\n\t%s.x = ", uv);
1131 builder->fsCodeAppendf(".bgra;\n\t\t%s.x = ", uv); 1123 noise_code.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
1132 builder->fsCodeAppendf(dotLattice, lattice, lattice, inc8bit, fractVal);
1133 } 1124 }
1134 1125
1135 // Compute 'b' as a linear interpolation of 'u' and 'v' 1126 // Compute 'b' as a linear interpolation of 'u' and 'v'
1136 builder->fsCodeAppendf("\t\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, nois eSmooth); 1127 noise_code.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmo oth);
1137 // Compute the noise as a linear interpolation of 'a' and 'b' 1128 // Compute the noise as a linear interpolation of 'a' and 'b'
1138 builder->fsCodeAppendf("\t\tfloat %s = mix(%s.x, %s.y, %s.y);", noise, ab, a b, noiseSmooth); 1129 noise_code.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmoot h);
1139 1130
1140 builder->fsCodeAppendf("\t\t%s[channel] += ", outputColor); 1131 SkString noise_funcName;
1141 builder->fsCodeAppendf((fType == SkPerlinNoiseShader::kFractalNoise_Type) ? 1132 builder->emitFunction(GrGLShaderBuilder::kFragment_ShaderType, kFloat_GrSLTy pe, "perlinnoise",
bsalomon 2013/06/10 13:39:25 Would this be easier to read if the there was just
1142 "%s / %s;" : "abs(%s) / %s;", noise, ratio); 1133 fStitchTiles ? SK_ARRAY_COUNT(perlinnoise_stitch_Args) :
1134 SK_ARRAY_COUNT(perlinnoise_Args),
1135 fStitchTiles ? perlinnoise_stitch_Args : perlinnoise_A rgs,
1136 noise_code.c_str(), &noise_funcName);
1143 1137
1144 builder->fsCodeAppendf("\t\t%s *= vec2(2.0);", noiseVec); 1138 // There are rounding errors if the floor operation is not performed here
1145 builder->fsCodeAppendf("\t\t%s *= 2.0;", ratio); 1139 builder->fsCodeAppendf("\n\t\tvec2 %s = floor((%s * vec3(%s, 1.0)).xy) * %s; ",
1140 noiseVec, invMatrixUni, vCoords, baseFrequencyUni);
1141
1142 // Clear the color accumulator
1143 builder->fsCodeAppendf("\n\t\t%s = vec4(0.0);", outputColor);
1146 1144
1147 if (fStitchTiles) { 1145 if (fStitchTiles) {
1148 builder->fsCodeAppendf("\t\t%s.xz *= vec2(2.0);", stitchData); 1146 // Set up TurbulenceInitial stitch values.
1149 builder->fsCodeAppendf("\t\t%s.yw = %s.xz + vec2(%s);", stitchData, stit chData, perlinNoise); 1147 builder->fsCodeAppendf("\n\t\tvec4 %s = %s;", stitchData, stitchDataUni) ;
1150 } 1148 }
1151 builder->fsCodeAppend("\t\t}"); // end of the for loop on octaves
1152 1149
1153 builder->fsCodeAppend("\t\t}"); // end of the for loop on channels 1150 builder->fsCodeAppendf("\n\t\tfloat %s = 1.0;", ratio);
1151
1152 // Loop over all octaves
1153 builder->fsCodeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {" , fNumOctaves);
1154
1155 builder->fsCodeAppendf("\n\t\t\t%s += ", outputColor);
1156 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
1157 builder->fsCodeAppend("abs(");
1158 }
1159 if (fStitchTiles) {
1160 builder->fsCodeAppendf(
1161 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
1162 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
1163 noise_funcName.c_str(), chanCoordR, noiseVec, stitchData,
1164 noise_funcName.c_str(), chanCoordG, noiseVec, stitchData,
1165 noise_funcName.c_str(), chanCoordB, noiseVec, stitchData,
1166 noise_funcName.c_str(), chanCoordA, noiseVec, stitchData);
1167 } else {
1168 builder->fsCodeAppendf(
1169 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
1170 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
1171 noise_funcName.c_str(), chanCoordR, noiseVec,
1172 noise_funcName.c_str(), chanCoordG, noiseVec,
1173 noise_funcName.c_str(), chanCoordB, noiseVec,
1174 noise_funcName.c_str(), chanCoordA, noiseVec);
1175 }
1176 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
1177 builder->fsCodeAppendf(")"); // end of "abs("
1178 }
1179 builder->fsCodeAppendf(" * %s;", ratio);
1180
1181 builder->fsCodeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
1182 builder->fsCodeAppendf("\n\t\t\t%s *= 0.5;", ratio);
1183
1184 if (fStitchTiles) {
1185 builder->fsCodeAppendf("\n\t\t\t%s.xz *= vec2(2.0);", stitchData);
1186 builder->fsCodeAppendf("\n\t\t\t%s.yw = %s.xz + vec2(%s);", stitchData, stitchData, perlinNoise);
1187 }
1188 builder->fsCodeAppend("\n\t\t}"); // end of the for loop on octaves
1154 1189
1155 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { 1190 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
1156 // The value of turbulenceFunctionResult comes from ((turbulenceFunction Result) + 1) / 2 1191 // The value of turbulenceFunctionResult comes from ((turbulenceFunction Result) + 1) / 2
1157 // by fractalNoise and (turbulenceFunctionResult) by turbulence. 1192 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
1158 builder->fsCodeAppendf("\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputCol or, outputColor); 1193 builder->fsCodeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputC olor, outputColor);
1159 } 1194 }
1160 1195
1161 builder->fsCodeAppendf("\t\t%s.a *= %s;", outputColor, alphaUni); 1196 builder->fsCodeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni);
1162 1197
1163 // Clamp values 1198 // Clamp values
1164 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputC olor); 1199 builder->fsCodeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outpu tColor);
1165 1200
1166 // Pre-multiply the result 1201 // Pre-multiply the result
1167 builder->fsCodeAppendf("\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", 1202 builder->fsCodeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
1168 outputColor, outputColor, outputColor, outputColor); 1203 outputColor, outputColor, outputColor, outputColor);
1169 } 1204 }
1170 1205
1171 GrGLNoise::GrGLNoise(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect) 1206 GrGLNoise::GrGLNoise(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
1172 : INHERITED (factory) 1207 : INHERITED (factory)
1173 , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type()) 1208 , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type())
1174 , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles()) 1209 , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles())
1175 , fNumOctaves(drawEffect.castEffect<GrPerlinNoiseEffect>().numOctaves()) 1210 , fNumOctaves(drawEffect.castEffect<GrPerlinNoiseEffect>().numOctaves())
1176 , fEffectMatrix(drawEffect.castEffect<GrPerlinNoiseEffect>().coordsType()) { 1211 , fEffectMatrix(drawEffect.castEffect<GrPerlinNoiseEffect>().coordsType()) {
1177 } 1212 }
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
1288 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture); 1323 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture);
1289 } 1324 }
1290 #endif 1325 #endif
1291 1326
1292 return effect; 1327 return effect;
1293 } 1328 }
1294 1329
1295 #else 1330 #else
1296 1331
1297 GrEffectRef* SkPerlinNoiseShader::asNewEffect(GrContext*, const SkPaint&) const { 1332 GrEffectRef* SkPerlinNoiseShader::asNewEffect(GrContext*, const SkPaint&) const {
1298 #if !defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_WIN) 1333 #if !defined(SK_BUILD_FOR_ANDROID)
1299 SkDEBUGFAIL("Should not call in GPU-less build"); 1334 SkDEBUGFAIL("Should not call in GPU-less build");
1300 #endif 1335 #endif
1301 return NULL; 1336 return NULL;
1302 } 1337 }
1303 1338
1304 #endif 1339 #endif
1305 1340
1306 #ifdef SK_DEVELOPER 1341 #ifdef SK_DEVELOPER
1307 void SkPerlinNoiseShader::toString(SkString* str) const { 1342 void SkPerlinNoiseShader::toString(SkString* str) const {
1308 str->append("SkPerlinNoiseShader: ("); 1343 str->append("SkPerlinNoiseShader: (");
(...skipping 19 matching lines...) Expand all
1328 str->append(" seed: "); 1363 str->append(" seed: ");
1329 str->appendScalar(fSeed); 1364 str->appendScalar(fSeed);
1330 str->append(" stitch tiles: "); 1365 str->append(" stitch tiles: ");
1331 str->append(fStitchTiles ? "true " : "false "); 1366 str->append(fStitchTiles ? "true " : "false ");
1332 1367
1333 this->INHERITED::toString(str); 1368 this->INHERITED::toString(str);
1334 1369
1335 str->append(")"); 1370 str->append(")");
1336 } 1371 }
1337 #endif 1372 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698