add minor improvements to perlin noise performance

main
jbb01 6 months ago
parent 70f2f38e96
commit 37539a1906

@ -91,11 +91,11 @@ public final class PerlinTexture implements Texture {
int j = (int) Math.floor(y); int j = (int) Math.floor(y);
int k = (int) Math.floor(z); int k = (int) Math.floor(z);
var c = new Vec3[2][2][2]; var c = new Vec3[8];
for (int di = 0; di < 2; di++) { for (int di = 0; di < 2; di++) {
for (int dj = 0; dj < 2; dj++) { for (int dj = 0; dj < 2; dj++) {
for (int dk = 0; dk < 2; dk++) { for (int dk = 0; dk < 2; dk++) {
c[di][dj][dk] = randvec[permX[(i + di) & mask] ^ permY[(j + dj) & mask] ^ permZ[(k + dk) & mask]]; c[di << 2 | dj << 1 | dk] = randvec[permX[(i + di) & mask] ^ permY[(j + dj) & mask] ^ permZ[(k + dk) & mask]];
} }
} }
} }
@ -109,7 +109,7 @@ public final class PerlinTexture implements Texture {
var weight = 1.0; var weight = 1.0;
for (int i = 0; i < depth; i++) { for (int i = 0; i < depth; i++) {
accum += weight * getNoise(temp); accum = Math.fma(weight, getNoise(temp), accum);
weight *= 0.5; weight *= 0.5;
temp = temp.times(2); temp = temp.times(2);
} }
@ -124,20 +124,21 @@ public final class PerlinTexture implements Texture {
return color.apply(t); return color.apply(t);
} }
private static double interpolate(Vec3[][][] c, double u, double v, double w) { private static double interpolate(Vec3[] c, double u, double v, double w) {
var uu = u * u * (3 - 2 * u); var uu = u * u * Math.fma(-2, u, 3);
var vv = v * v * (3 - 2 * v); var vv = v * v * Math.fma(-2, v, 3);
var ww = w * w * (3 - 2 * w); var ww = w * w * Math.fma(-2, w, 3);
var accum = 0.0; var accum = 0.0;
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) { for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) { for (int k = 0; k < 2; k++) {
var weight = new Vec3(u - i, v - j, w - k); var vec = c[i << 2 | j << 1 | k];
accum += (i * uu + (1 - i) * (1 - uu)) var dot = (u - i) * vec.x() + (v - j) * vec.y() + (w - k) * vec.z();
* (j * vv + (1 - j) * (1 - vv)) accum += Math.fma(i, uu, (1 - i) * (1 - uu))
* (k * ww + (1 - k) * (1 - ww)) * Math.fma(j, vv, (1 - j) * (1 - vv))
* c[i][j][k].times(weight); * Math.fma(k, ww, (1 - k) * (1 - ww))
* dot;
} }
} }
} }

Loading…
Cancel
Save