diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2022-04-28 15:52:10 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2022-04-28 16:29:42 -0700 |
commit | 44aa595269516d44f6642890a9211bcdde8b29fd (patch) | |
tree | ba1cce01703cff14894516aa31387f04564ba623 | |
parent | fd9a7050e9afba9da061b5a9ca83c95a36eba44a (diff) |
libs/din: premultiply din->width * din->height
dotgradient() is very hot and needs this result when indexing
din->grid[]. Since it doesn't change for a given din instance,
just cache the result @ din_new().
-rw-r--r-- | src/libs/din/din.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/libs/din/din.c b/src/libs/din/din.c index ad87642..44c6ec8 100644 --- a/src/libs/din/din.c +++ b/src/libs/din/din.c @@ -9,6 +9,7 @@ typedef struct din_t { int width, height, depth; + int W_x_H; v3f_t grid[]; } din_t; @@ -33,7 +34,7 @@ void din_randomize(din_t *din) r.y = randf(); r.z = randf(); - din->grid[z * din->width * din->height + y * din->width + x] = v3f_normalize(&r); + din->grid[z * din->W_x_H + y * din->width + x] = v3f_normalize(&r); } } } @@ -56,6 +57,9 @@ din_t * din_new(int width, int height, int depth) din->height = height; din->depth = depth; + /* premultiply this since we do it a lot in addressing din->grid[] */ + din->W_x_H = width * height; + din_randomize(din); return din; @@ -76,7 +80,7 @@ static inline float dotgradient(const din_t *din, int x, int y, int z, const v3f assert(y < din->height); assert(z < din->depth); - return v3f_dot(&din->grid[z * din->width * din->height + y * din->width + x], &distance); + return v3f_dot(&din->grid[z * din->W_x_H + y * din->width + x], &distance); } |