Skip to content

Commit

Permalink
Cleanup, slight speedup, improved 4D uniformity
Browse files Browse the repository at this point in the history
  • Loading branch information
stegu committed Mar 26, 2011
1 parent 46d926d commit fa6321b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
7 changes: 4 additions & 3 deletions benchmark/common/noisebench.c
Expand Up @@ -372,9 +372,10 @@ int main(int argc, char *argv[]) {


logfile = fopen(LOGFILENAME, "w"); logfile = fopen(LOGFILENAME, "w");


fprintf(logfile, "GL vendor: %s\n", glGetString(GL_VENDOR)); fprintf(logfile, "GL vendor: %s\n", glGetString(GL_VENDOR));
fprintf(logfile, "GL renderer: %s\n", glGetString(GL_RENDERER)); fprintf(logfile, "GL renderer: %s\n", glGetString(GL_RENDERER));
fprintf(logfile, "GL version: %s\n", glGetString(GL_VERSION)); fprintf(logfile, "GL version: %s\n", glGetString(GL_VERSION));
fprintf(logfile, "Noise version: %s\n", NOISEVERSION);


// Create the shader object from two external GLSL source files // Create the shader object from two external GLSL source files
createShader(&programObject, VERTEXSHADERFILE2D, FRAGMENTSHADERFILE2D); createShader(&programObject, VERTEXSHADERFILE2D, FRAGMENTSHADERFILE2D);
Expand Down
42 changes: 20 additions & 22 deletions src/noise3D.glsl
Expand Up @@ -10,24 +10,24 @@


float simplexNoise(vec3 v) float simplexNoise(vec3 v)
{ {
const vec2 C = vec2(1./6. , 1./3. ) ; const vec2 C = vec2(1.0/6.0, 1.0/3.0 ) ;
const vec4 D = vec4(0., 0.5, 1.0, 2.0); const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);


// First corner // First corner
vec3 i = floor(v + dot(v, C.yyy) ); vec3 i = floor(v + dot(v, C.yyy) );
vec3 x0 = v - i + dot(i, C.xxx) ; vec3 x0 = v - i + dot(i, C.xxx) ;

// Other corners // Other corners
#ifdef COLLAPSE_SORTNET #ifdef COLLAPSE_SORTNET
vec3 g = vec3( greaterThan( x0.xyz, x0.yzx) ); vec3 g = vec3( greaterThan( x0.xyz, x0.yzx) );
// vec3 l = vec3( lessThanEqual( x0.xyz, x0.yzx) ); // vec3 l = vec3( lessThanEqual( x0.xyz, x0.yzx) );
vec3 l = 1. - g; vec3 l = 1.0 - g;
vec3 i1 = g.xyz * l.zxy; vec3 i1 = min( g.xyz, l.zxy );
vec3 i2 = max( g.xyz, l.zxy); vec3 i2 = max( g.xyz, l.zxy );
#else #else
// Keeping this clean - let the compiler optimize. // Keeping this clean - let the compiler optimize.
// Force existance of strict total ordering in sort. // Force existence of strict total ordering in sort.
vec3 q0 = floor(x0 * 1024.0) + vec3( 0., 1./4., 2./4.); vec3 q0 = floor(x0 * 1024.0) + vec3( 0.0, 1.0/4.0, 2.0/4.0);
vec3 q1; vec3 q1;
q1.x = max(q0.x, q0.y); q1.x = max(q0.x, q0.y);
q1.y = min(q0.x, q0.y); q1.y = min(q0.x, q0.y);
Expand All @@ -47,17 +47,17 @@ float simplexNoise(vec3 v)
vec3 i2 = vec3(lessThanEqual(q3.yyy, q0)); vec3 i2 = vec3(lessThanEqual(q3.yyy, q0));
#endif #endif


// x0 = x0 - 0. + 0. * C // x0 = x0 - 0. + 0.0 * C
vec3 x1 = x0 - i1 + 1. * C.xxx; vec3 x1 = x0 - i1 + 1.0 * C.xxx;
vec3 x2 = x0 - i2 + 2. * C.xxx; vec3 x2 = x0 - i2 + 2.0 * C.xxx;
vec3 x3 = x0 - 1. + 3. * C.xxx; vec3 x3 = x0 - 1. + 3.0 * C.xxx;


// Permutations // Permutations
i = mod(i, pParam.x ); i = mod(i, pParam.x );
vec4 p = permute( permute( permute( vec4 p = permute( permute( permute(
i.z + vec4(0., i1.z, i2.z, 1. ), pParam.xyz) i.z + vec4(0.0, i1.z, i2.z, 1.0 ), pParam.xyz)
+ i.y + vec4(0., i1.y, i2.y, 1. ), pParam.xyz) + i.y + vec4(0.0, i1.y, i2.y, 1.0 ), pParam.xyz)
+ i.x + vec4(0., i1.x, i2.x, 1. ), pParam.xyz); + i.x + vec4(0.0, i1.x, i2.x, 1.0 ), pParam.xyz);


// Gradients // Gradients
// ( N*N points uniformly over a square, mapped onto a octohedron.) // ( N*N points uniformly over a square, mapped onto a octohedron.)
Expand All @@ -71,15 +71,15 @@ float simplexNoise(vec3 v)


vec4 x = x_ *ns.x + ns.yyyy; vec4 x = x_ *ns.x + ns.yyyy;
vec4 y = y_ *ns.x + ns.yyyy; vec4 y = y_ *ns.x + ns.yyyy;
vec4 h = 1. - abs(x) - abs(y); vec4 h = 1.0 - abs(x) - abs(y);


vec4 b0 = vec4( x.xy, y.xy ); vec4 b0 = vec4( x.xy, y.xy );
vec4 b1 = vec4( x.zw, y.zw ); vec4 b1 = vec4( x.zw, y.zw );


//vec4 s0 = vec4(lessThan(b0,D.xxxx)) *2. -1.; //vec4 s0 = vec4(lessThan(b0,D.xxxx)) *2.0 -1.0;
//vec4 s1 = vec4(lessThan(b1,D.xxxx)) *2. -1.; //vec4 s1 = vec4(lessThan(b1,D.xxxx)) *2.0 -1.0;
vec4 s0 = floor(b0) *2. +1.; vec4 s0 = floor(b0) *2.0 +1.0;
vec4 s1 = floor(b1) *2. +1.; vec4 s1 = floor(b1) *2.0 +1.0;
vec4 sh = -vec4(lessThan(h, D.xxxx)); vec4 sh = -vec4(lessThan(h, D.xxxx));


vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ; vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
Expand All @@ -104,5 +104,3 @@ float simplexNoise(vec3 v)
return 58.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), return 58.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
dot(p2,x2), dot(p3,x3) ) ); dot(p2,x2), dot(p3,x3) ) );
} }


32 changes: 20 additions & 12 deletions src/noise4D.glsl
Expand Up @@ -10,10 +10,10 @@


vec4 grad4(float j, vec4 ip) vec4 grad4(float j, vec4 ip)
{ {
const vec4 ones = vec4(1.0,1.0,1.0,-1.0); const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0);
vec4 p,s; vec4 p,s;


p.xyz = floor( fract (vec3(j) * ip.xyz) *pParam.w) * ip.z -1.0; p.xyz = floor( fract (vec3(j) * ip.xyz) *pParam.w) * ip.z - 1.0;
p.w = 1.5 - dot(abs(p.xyz), ones.xyz); p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
s = vec4(lessThan(p, vec4(0.0))); s = vec4(lessThan(p, vec4(0.0)));
p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www; p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
Expand All @@ -32,15 +32,17 @@ float simplexNoise(vec4 v)
// Other corners // Other corners


#ifdef COLLAPSE_SORTNET #ifdef COLLAPSE_SORTNET
// Rank sorting contributed by Bill Licea-Kane, AMD (formerly ATI) // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
vec4 i0; vec4 i0;


vec3 isX = step( x0.yzw, x0.xxx ); vec3 isX = step( x0.yzw, x0.xxx );
i0.x = dot( isX, vec3( 1.0 ) ); vec3 isYZ = step( x0.zww, x0.yyz );
// i0.x = dot( isX, vec3( 1.0 ) );
i0.x = isX.x + isX.y + isX.z;
i0.yzw = 1.0 - isX; i0.yzw = 1.0 - isX;


vec3 isYZ = step( x0.zww, x0.yyz ); // i0.y += dot( isYZ.xy, vec2( 1.0 ) );
i0.y += dot( isYZ.xy, vec2( 1.0 ) ); i0.y += isYZ.x + isYZ.y;
i0.zw += 1.0 - isYZ.xy; i0.zw += 1.0 - isYZ.xy;


i0.z += isYZ.z; i0.z += isYZ.z;
Expand All @@ -51,7 +53,7 @@ float simplexNoise(vec4 v)
vec4 i2 = clamp( i0-1.0, 0.0, 1.0 ); vec4 i2 = clamp( i0-1.0, 0.0, 1.0 );
vec4 i1 = clamp( i0-2.0, 0.0, 1.0 ); vec4 i1 = clamp( i0-2.0, 0.0, 1.0 );
#else #else
// Force existance of strict total ordering in sort. // Force existence of strict total ordering in sort.
vec4 q0 = floor(x0 * 1024.0) + vec4( 0.0, 1.0/4.0, 2.0/4.0 , 3.0/4.0); vec4 q0 = floor(x0 * 1024.0) + vec4( 0.0, 1.0/4.0, 2.0/4.0 , 3.0/4.0);
vec4 q1; vec4 q1;
q1.xy = max(q0.xy, q0.zw); // x:z y:w q1.xy = max(q0.xy, q0.zw); // x:z y:w
Expand Down Expand Up @@ -88,11 +90,17 @@ float simplexNoise(vec4 v)
+ i.y + vec4(i1.y, i2.y, i3.y, 1.0 ), pParam.xyz) + i.y + vec4(i1.y, i2.y, i3.y, 1.0 ), pParam.xyz)
+ i.x + vec4(i1.x, i2.x, i3.x, 1.0 ), pParam.xyz); + i.x + vec4(i1.x, i2.x, i3.x, 1.0 ), pParam.xyz);
// Gradients // Gradients
// ( N*N*N points uniformly over a cube, mapped onto a 4-octohedron.) // ( 7*7*6 points uniformly over a cube, mapped onto a 4-octahedron.)
vec4 ip = vec4(pParam.w) ; // The permutation ring is 17*17, and that should be close
ip.xy *= pParam.w ; // to an even multiple of the number of points to avoid
ip.x *= pParam.w ; // directional preferences for the noise field.
ip = vec4(1.0,1.0,1.0,2.0) / ip ; // 7*7*6 = 294, which is close to 17*17 = 289.

vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;
// vec4 ip = vec4(pParam.w) ;
// ip.xy *= pParam.w ;
// ip.x *= pParam.w ;
// ip = vec4(1.0, 1.0, 1.0, 2.0) / ip ;


vec4 p0 = grad4(j0, ip); vec4 p0 = grad4(j0, ip);
vec4 p1 = grad4(j1.x, ip); vec4 p1 = grad4(j1.x, ip);
Expand Down

0 comments on commit fa6321b

Please sign in to comment.