| 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 "SkPerlinNoiseShader.h" | 9 #include "SkPerlinNoiseShader.h" |
| 10 #include "SkFlattenableBuffers.h" | 10 #include "SkFlattenableBuffers.h" |
| (...skipping 963 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 974 fBaseFrequencyUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderT
ype, | 974 fBaseFrequencyUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderT
ype, |
| 975 kVec2f_GrSLType, "baseFrequency"); | 975 kVec2f_GrSLType, "baseFrequency"); |
| 976 const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni); | 976 const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni); |
| 977 fAlphaUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 977 fAlphaUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 978 kFloat_GrSLType, "alpha"); | 978 kFloat_GrSLType, "alpha"); |
| 979 const char* alphaUni = builder->getUniformCStr(fAlphaUni); | 979 const char* alphaUni = builder->getUniformCStr(fAlphaUni); |
| 980 | 980 |
| 981 const char* stitchDataUni = NULL; | 981 const char* stitchDataUni = NULL; |
| 982 if (fStitchTiles) { | 982 if (fStitchTiles) { |
| 983 fStitchDataUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, | 983 fStitchDataUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, |
| 984 kVec4f_GrSLType, "stitchData"); | 984 kVec2f_GrSLType, "stitchData"); |
| 985 stitchDataUni = builder->getUniformCStr(fStitchDataUni); | 985 stitchDataUni = builder->getUniformCStr(fStitchDataUni); |
| 986 } | 986 } |
| 987 | 987 |
| 988 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 | 988 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 |
| 989 const char* chanCoordR = "0.125"; | 989 const char* chanCoordR = "0.125"; |
| 990 const char* chanCoordG = "0.375"; | 990 const char* chanCoordG = "0.375"; |
| 991 const char* chanCoordB = "0.625"; | 991 const char* chanCoordB = "0.625"; |
| 992 const char* chanCoordA = "0.875"; | 992 const char* chanCoordA = "0.875"; |
| 993 const char* chanCoord = "chanCoord"; | 993 const char* chanCoord = "chanCoord"; |
| 994 const char* stitchData = "stitchData"; | 994 const char* stitchData = "stitchData"; |
| 995 const char* ratio = "ratio"; | 995 const char* ratio = "ratio"; |
| 996 const char* noiseXY = "noiseXY"; | 996 const char* noiseXY = "noiseXY"; |
| 997 const char* noiseVec = "noiseVec"; | 997 const char* noiseVec = "noiseVec"; |
| 998 const char* noiseSmooth = "noiseSmooth"; | 998 const char* noiseSmooth = "noiseSmooth"; |
| 999 const char* fractVal = "fractVal"; | 999 const char* fractVal = "fractVal"; |
| 1000 const char* uv = "uv"; | 1000 const char* uv = "uv"; |
| 1001 const char* ab = "ab"; | 1001 const char* ab = "ab"; |
| 1002 const char* latticeIdx = "latticeIdx"; | 1002 const char* latticeIdx = "latticeIdx"; |
| 1003 const char* lattice = "lattice"; | 1003 const char* lattice = "lattice"; |
| 1004 const char* perlinNoise = "4096.0"; | |
| 1005 const char* inc8bit = "0.00390625"; // 1.0 / 256.0 | 1004 const char* inc8bit = "0.00390625"; // 1.0 / 256.0 |
| 1006 // This is the math to convert the two 16bit integer packed into rgba 8 bit
input into a | 1005 // This is the math to convert the two 16bit integer packed into rgba 8 bit
input into a |
| 1007 // [-1,1] vector and perform a dot product between that vector and the provi
ded vector. | 1006 // [-1,1] vector and perform a dot product between that vector and the provi
ded vector. |
| 1008 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec
2(1.0)), %s);"; | 1007 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec
2(1.0)), %s);"; |
| 1009 | 1008 |
| 1010 // Android precision fix for NON Tegra devices, like, for example: Nexus 10
(ARM's Mali-T604) | |
| 1011 // The value of perlinNoise is 4096.0, so we need a high precision float to
store this | |
| 1012 const GrGLShaderVar::Precision precision = GrGLShaderVar::kHigh_Precision; | |
| 1013 const char* precisionString = | |
| 1014 GrGLShaderVar::PrecisionString(precision, builder->ctxInfo().binding()); | |
| 1015 | |
| 1016 // Add noise function | 1009 // Add noise function |
| 1017 static const GrGLShaderVar gPerlinNoiseArgs[] = { | 1010 static const GrGLShaderVar gPerlinNoiseArgs[] = { |
| 1018 GrGLShaderVar(chanCoord, kFloat_GrSLType), | 1011 GrGLShaderVar(chanCoord, kFloat_GrSLType), |
| 1019 GrGLShaderVar(noiseVec, kVec2f_GrSLType, GrGLShaderVar::kNonArray, preci
sion) | 1012 GrGLShaderVar(noiseVec, kVec2f_GrSLType) |
| 1020 }; | 1013 }; |
| 1021 | 1014 |
| 1022 static const GrGLShaderVar gPerlinNoiseStitchArgs[] = { | 1015 static const GrGLShaderVar gPerlinNoiseStitchArgs[] = { |
| 1023 GrGLShaderVar(chanCoord, kFloat_GrSLType), | 1016 GrGLShaderVar(chanCoord, kFloat_GrSLType), |
| 1024 GrGLShaderVar(noiseVec, kVec2f_GrSLType, GrGLShaderVar::kNonArray, preci
sion), | 1017 GrGLShaderVar(noiseVec, kVec2f_GrSLType), |
| 1025 GrGLShaderVar(stitchData, kVec4f_GrSLType, GrGLShaderVar::kNonArray, pre
cision) | 1018 GrGLShaderVar(stitchData, kVec2f_GrSLType) |
| 1026 }; | 1019 }; |
| 1027 | 1020 |
| 1028 SkString noiseCode; | 1021 SkString noiseCode; |
| 1029 | 1022 |
| 1030 noiseCode.appendf( | 1023 noiseCode.appendf("\tvec4 %s = vec4(floor(%s), fract(%s));", noiseXY, noiseV
ec, noiseVec); |
| 1031 "\t%svec4 %s = vec4(floor(%s) + vec2(%s), fract(%s));", | |
| 1032 precisionString, noiseXY, noiseVec, perlinNoise, noiseVec); | |
| 1033 | 1024 |
| 1034 // smooth curve : t * t * (3 - 2 * t) | 1025 // smooth curve : t * t * (3 - 2 * t) |
| 1035 noiseCode.appendf("\n\tvec2 %s = %s.zw * %s.zw * (vec2(3.0) - vec2(2.0) * %s
.zw);", | 1026 noiseCode.appendf("\n\tvec2 %s = %s.zw * %s.zw * (vec2(3.0) - vec2(2.0) * %s
.zw);", |
| 1036 noiseSmooth, noiseXY, noiseXY, noiseXY); | 1027 noiseSmooth, noiseXY, noiseXY, noiseXY); |
| 1037 | 1028 |
| 1038 // Adjust frequencies if we're stitching tiles | 1029 // Adjust frequencies if we're stitching tiles |
| 1039 if (fStitchTiles) { | 1030 if (fStitchTiles) { |
| 1040 noiseCode.appendf("\n\tif(%s.x >= %s.y) { %s.x -= %s.x; }", | 1031 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }", |
| 1041 noiseXY, stitchData, noiseXY, stitchData); | 1032 noiseXY, stitchData, noiseXY, stitchData); |
| 1042 noiseCode.appendf("\n\tif(%s.x >= (%s.y - 1.0)) { %s.x -= (%s.x - 1.0);
}", | 1033 noiseCode.appendf("\n\tif(%s.x >= (%s.x - 1.0)) { %s.x -= (%s.x - 1.0);
}", |
| 1043 noiseXY, stitchData, noiseXY, stitchData); | 1034 noiseXY, stitchData, noiseXY, stitchData); |
| 1044 noiseCode.appendf("\n\tif(%s.y >= %s.w) { %s.y -= %s.z; }", | 1035 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }", |
| 1045 noiseXY, stitchData, noiseXY, stitchData); | 1036 noiseXY, stitchData, noiseXY, stitchData); |
| 1046 noiseCode.appendf("\n\tif(%s.y >= (%s.w - 1.0)) { %s.y -= (%s.z - 1.0);
}", | 1037 noiseCode.appendf("\n\tif(%s.y >= (%s.y - 1.0)) { %s.y -= (%s.y - 1.0);
}", |
| 1047 noiseXY, stitchData, noiseXY, stitchData); | 1038 noiseXY, stitchData, noiseXY, stitchData); |
| 1048 } | 1039 } |
| 1049 | 1040 |
| 1050 // Get texture coordinates and normalize | 1041 // Get texture coordinates and normalize |
| 1051 noiseCode.appendf("\n\t%s.xy = fract(floor(mod(%s.xy, 256.0)) / vec2(256.0))
;\n", | 1042 noiseCode.appendf("\n\t%s.xy = fract(floor(mod(%s.xy, 256.0)) / vec2(256.0))
;\n", |
| 1052 noiseXY, noiseXY); | 1043 noiseXY, noiseXY); |
| 1053 | 1044 |
| 1054 // Get permutation for x | 1045 // Get permutation for x |
| 1055 { | 1046 { |
| 1056 SkString xCoords(""); | 1047 SkString xCoords(""); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1157 | 1148 |
| 1158 // There are rounding errors if the floor operation is not performed here | 1149 // There are rounding errors if the floor operation is not performed here |
| 1159 builder->fsCodeAppendf("\n\t\tvec2 %s = floor((%s * vec3(%s, 1.0)).xy) * %s;
", | 1150 builder->fsCodeAppendf("\n\t\tvec2 %s = floor((%s * vec3(%s, 1.0)).xy) * %s;
", |
| 1160 noiseVec, invMatrixUni, vCoords, baseFrequencyUni); | 1151 noiseVec, invMatrixUni, vCoords, baseFrequencyUni); |
| 1161 | 1152 |
| 1162 // Clear the color accumulator | 1153 // Clear the color accumulator |
| 1163 builder->fsCodeAppendf("\n\t\t%s = vec4(0.0);", outputColor); | 1154 builder->fsCodeAppendf("\n\t\t%s = vec4(0.0);", outputColor); |
| 1164 | 1155 |
| 1165 if (fStitchTiles) { | 1156 if (fStitchTiles) { |
| 1166 // Set up TurbulenceInitial stitch values. | 1157 // Set up TurbulenceInitial stitch values. |
| 1167 builder->fsCodeAppendf("\n\t\t%s vec4 %s = %s;", precisionString, stitch
Data, stitchDataUni); | 1158 builder->fsCodeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni)
; |
| 1168 } | 1159 } |
| 1169 | 1160 |
| 1170 builder->fsCodeAppendf("\n\t\tfloat %s = 1.0;", ratio); | 1161 builder->fsCodeAppendf("\n\t\tfloat %s = 1.0;", ratio); |
| 1171 | 1162 |
| 1172 // Loop over all octaves | 1163 // Loop over all octaves |
| 1173 builder->fsCodeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {"
, fNumOctaves); | 1164 builder->fsCodeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {"
, fNumOctaves); |
| 1174 | 1165 |
| 1175 builder->fsCodeAppendf("\n\t\t\t%s += ", outputColor); | 1166 builder->fsCodeAppendf("\n\t\t\t%s += ", outputColor); |
| 1176 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { | 1167 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { |
| 1177 builder->fsCodeAppend("abs("); | 1168 builder->fsCodeAppend("abs("); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1195 } | 1186 } |
| 1196 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { | 1187 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { |
| 1197 builder->fsCodeAppendf(")"); // end of "abs(" | 1188 builder->fsCodeAppendf(")"); // end of "abs(" |
| 1198 } | 1189 } |
| 1199 builder->fsCodeAppendf(" * %s;", ratio); | 1190 builder->fsCodeAppendf(" * %s;", ratio); |
| 1200 | 1191 |
| 1201 builder->fsCodeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec); | 1192 builder->fsCodeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec); |
| 1202 builder->fsCodeAppendf("\n\t\t\t%s *= 0.5;", ratio); | 1193 builder->fsCodeAppendf("\n\t\t\t%s *= 0.5;", ratio); |
| 1203 | 1194 |
| 1204 if (fStitchTiles) { | 1195 if (fStitchTiles) { |
| 1205 builder->fsCodeAppendf("\n\t\t\t%s.xz *= vec2(2.0);", stitchData); | 1196 builder->fsCodeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData); |
| 1206 builder->fsCodeAppendf("\n\t\t\t%s.yw = %s.xz + vec2(%s);", | |
| 1207 stitchData, stitchData, perlinNoise); | |
| 1208 } | 1197 } |
| 1209 builder->fsCodeAppend("\n\t\t}"); // end of the for loop on octaves | 1198 builder->fsCodeAppend("\n\t\t}"); // end of the for loop on octaves |
| 1210 | 1199 |
| 1211 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { | 1200 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { |
| 1212 // The value of turbulenceFunctionResult comes from ((turbulenceFunction
Result) + 1) / 2 | 1201 // The value of turbulenceFunctionResult comes from ((turbulenceFunction
Result) + 1) / 2 |
| 1213 // by fractalNoise and (turbulenceFunctionResult) by turbulence. | 1202 // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
| 1214 builder->fsCodeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputC
olor, outputColor); | 1203 builder->fsCodeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputC
olor, outputColor); |
| 1215 } | 1204 } |
| 1216 | 1205 |
| 1217 builder->fsCodeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni); | 1206 builder->fsCodeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1284 m.postTranslate(SK_Scalar1, SK_Scalar1); | 1273 m.postTranslate(SK_Scalar1, SK_Scalar1); |
| 1285 fEffectMatrix.setData(uman, m, drawEffect, NULL); | 1274 fEffectMatrix.setData(uman, m, drawEffect, NULL); |
| 1286 } | 1275 } |
| 1287 | 1276 |
| 1288 void GrGLPerlinNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect
& drawEffect) { | 1277 void GrGLPerlinNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect
& drawEffect) { |
| 1289 INHERITED::setData(uman, drawEffect); | 1278 INHERITED::setData(uman, drawEffect); |
| 1290 | 1279 |
| 1291 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseE
ffect>(); | 1280 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseE
ffect>(); |
| 1292 if (turbulence.stitchTiles()) { | 1281 if (turbulence.stitchTiles()) { |
| 1293 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); | 1282 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); |
| 1294 uman.set4f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), | 1283 uman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
| 1295 SkIntToScalar(stitchData.fWrapX), | 1284 SkIntToScalar(stitchData.fHeight)); |
| 1296 SkIntToScalar(stitchData.fHeight), | |
| 1297 SkIntToScalar(stitchData.fWrapY)); | |
| 1298 } | 1285 } |
| 1299 } | 1286 } |
| 1300 | 1287 |
| 1301 void GrGLSimplexNoise::setData(const GrGLUniformManager& uman, const GrDrawEffec
t& drawEffect) { | 1288 void GrGLSimplexNoise::setData(const GrGLUniformManager& uman, const GrDrawEffec
t& drawEffect) { |
| 1302 INHERITED::setData(uman, drawEffect); | 1289 INHERITED::setData(uman, drawEffect); |
| 1303 | 1290 |
| 1304 const GrSimplexNoiseEffect& turbulence = drawEffect.castEffect<GrSimplexNois
eEffect>(); | 1291 const GrSimplexNoiseEffect& turbulence = drawEffect.castEffect<GrSimplexNois
eEffect>(); |
| 1305 uman.set1f(fSeedUni, turbulence.seed()); | 1292 uman.set1f(fSeedUni, turbulence.seed()); |
| 1306 } | 1293 } |
| 1307 | 1294 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1382 str->append(" seed: "); | 1369 str->append(" seed: "); |
| 1383 str->appendScalar(fSeed); | 1370 str->appendScalar(fSeed); |
| 1384 str->append(" stitch tiles: "); | 1371 str->append(" stitch tiles: "); |
| 1385 str->append(fStitchTiles ? "true " : "false "); | 1372 str->append(fStitchTiles ? "true " : "false "); |
| 1386 | 1373 |
| 1387 this->INHERITED::toString(str); | 1374 this->INHERITED::toString(str); |
| 1388 | 1375 |
| 1389 str->append(")"); | 1376 str->append(")"); |
| 1390 } | 1377 } |
| 1391 #endif | 1378 #endif |
| OLD | NEW |