Permalink
Browse files

Hand optimized the mod() operations for additional speedup

  • Loading branch information...
1 parent db450db commit 7acfc61c749adbd00c87f80ab6408f3b06184156 @stegu stegu committed Aug 13, 2011
Showing with 38 additions and 20 deletions.
  1. +13 −6 src/noise2D.glsl
  2. +13 −6 src/noise3D.glsl
  3. +12 −8 src/noise4D.glsl
View
@@ -2,14 +2,21 @@
// Description : Array and textureless GLSL 2D simplex noise function.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
-// Lastmod : 20110410 (stegu)
+// Lastmod : 20110813 (stegu)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
//
-vec3 permute(vec3 x)
-{
- return mod(((x*34.0)+1.0)*x, 289.0);
+vec3 mod289(vec3 x) {
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
+}
+
+vec2 mod289(vec2 x) {
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
+}
+
+vec3 permute(vec3 x) {
+ return mod289(((x*34.0)+1.0)*x);
}
float snoise(vec2 v)
@@ -34,7 +41,7 @@ float snoise(vec2 v)
x12.xy -= i1;
// Permutations
- i = mod(i, 289.0); // Avoid truncation effects in permutation
+ i = mod289(i); // Avoid truncation effects in permutation
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
+ i.x + vec3(0.0, i1.x, 1.0 ));
@@ -51,7 +58,7 @@ float snoise(vec2 v)
vec3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
-// Inlined for speed: m *= taylorInvSqrt( a0*a0 + h*h );
+// Approximation of: m *= inversesqrt( a0*a0 + h*h );
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
// Compute final noise value at P
View
@@ -3,16 +3,23 @@
// noise functions.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
-// Lastmod : 20110410 (stegu)
+// Lastmod : 20110813 (stegu)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
//
-vec4 permute(vec4 x)
-{
- return mod(((x*34.0)+1.0)*x, 289.0);
+vec3 mod289(vec3 x) {
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
+}
+
+vec4 mod289(vec4 x) {
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
}
-
+
+vec4 permute(vec4 x) {
+ return mod289(((x*34.0)+1.0)*x);
+}
+
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
@@ -42,7 +49,7 @@ float snoise(vec3 v)
vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
// Permutations
- i = mod(i, 289.0 );
+ i = mod289(i);
vec4 p = permute( permute( permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
View
@@ -3,19 +3,23 @@
// noise functions.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
-// Lastmod : 20110410 (stegu)
+// Lastmod : 20110813 (stegu)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
//
-vec4 permute(vec4 x)
-{
- return mod(((x*34.0)+1.0)*x, 289.0);
+vec4 mod289(vec4 x) {
+ return x - floor(x * (1.0 / 289.0)) * 289.0; }
+
+float mod289(float x) {
+ return x - floor(x * (1.0 / 289.0)) * 289.0; }
+
+vec4 permute(vec4 x) {
+ return mod289(((x*34.0)+1.0)*x);
}
-float permute(float x)
-{
- return floor(mod(((x*34.0)+1.0)*x, 289.0));
+float permute(float x) {
+ return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
@@ -86,7 +90,7 @@ float snoise(vec4 v)
vec4 x4 = x0 + C.wwww;
// Permutations
- i = mod(i, 289.0);
+ i = mod289(i);
float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
vec4 j1 = permute( permute( permute( permute (
i.w + vec4(i1.w, i2.w, i3.w, 1.0 ))

0 comments on commit 7acfc61

Please sign in to comment.