From 37539a19060be822b8d6a9dc5717b3850d661cc2 Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Wed, 7 Aug 2024 15:50:09 +0200 Subject: [PATCH] add minor improvements to perlin noise performance --- .../render/texture/PerlinTexture.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/jonahbauer/raytracing/render/texture/PerlinTexture.java b/src/main/java/eu/jonahbauer/raytracing/render/texture/PerlinTexture.java index f8d0db0..041a2b7 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/texture/PerlinTexture.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/texture/PerlinTexture.java @@ -91,11 +91,11 @@ public final class PerlinTexture implements Texture { int j = (int) Math.floor(y); 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 dj = 0; dj < 2; dj++) { 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; for (int i = 0; i < depth; i++) { - accum += weight * getNoise(temp); + accum = Math.fma(weight, getNoise(temp), accum); weight *= 0.5; temp = temp.times(2); } @@ -124,20 +124,21 @@ public final class PerlinTexture implements Texture { return color.apply(t); } - private static double interpolate(Vec3[][][] c, double u, double v, double w) { - var uu = u * u * (3 - 2 * u); - var vv = v * v * (3 - 2 * v); - var ww = w * w * (3 - 2 * w); + private static double interpolate(Vec3[] c, double u, double v, double w) { + var uu = u * u * Math.fma(-2, u, 3); + var vv = v * v * Math.fma(-2, v, 3); + var ww = w * w * Math.fma(-2, w, 3); var accum = 0.0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { - var weight = new Vec3(u - i, v - j, w - k); - accum += (i * uu + (1 - i) * (1 - uu)) - * (j * vv + (1 - j) * (1 - vv)) - * (k * ww + (1 - k) * (1 - ww)) - * c[i][j][k].times(weight); + var vec = c[i << 2 | j << 1 | k]; + var dot = (u - i) * vec.x() + (v - j) * vec.y() + (w - k) * vec.z(); + accum += Math.fma(i, uu, (1 - i) * (1 - uu)) + * Math.fma(j, vv, (1 - j) * (1 - vv)) + * Math.fma(k, ww, (1 - k) * (1 - ww)) + * dot; } } }