From 3d422f2b3d311f1be3658ff1f44d818a99561a66 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 14:43:17 -0500 Subject: Minor style changes --- 3dmath.c | 2 ++ 1 file changed, 2 insertions(+) (limited to '3dmath.c') diff --git a/3dmath.c b/3dmath.c index eb9c4a8..ece6f83 100644 --- a/3dmath.c +++ b/3dmath.c @@ -1,3 +1,5 @@ +#include "3dmath.h" + #include #define POW2(x) ((x) * (x)) -- cgit v1.2.3 From f4afeeef49b12fffb4ce9e5860c0a79b6809d74b Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 14:54:28 -0500 Subject: 3dmath: Use const-correct function declarations --- 3dmath.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to '3dmath.c') diff --git a/3dmath.c b/3dmath.c index ece6f83..d5806b6 100644 --- a/3dmath.c +++ b/3dmath.c @@ -5,7 +5,7 @@ #define POW2(x) ((x) * (x)) float -dot(float x[3], float y[3]) { +dot(const float x[3], const float y[3]) { return x[0] * y[0] + x[1] * y[1] + x[2] * y[2]; } @@ -21,7 +21,7 @@ normalize(float x[3]) { } float -sphere_intersect(float y[3], float r[3], float s[3], float d[3], float c[3], float R) { +sphere_intersect(float y[3], float r[3], const float s[3], const float d[3], const float c[3], float R) { int i; float D, n[3], t, v[3]; -- cgit v1.2.3 From 8fdc2a63b118f37fa0bad11be0104296556aa5b0 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 14:58:22 -0500 Subject: 3dmath.c: In normalize, calculate reciprocal only once, and use sqrtf This saves about 12ms per frame on yelena. --- 3dmath.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to '3dmath.c') diff --git a/3dmath.c b/3dmath.c index d5806b6..5b876f2 100644 --- a/3dmath.c +++ b/3dmath.c @@ -11,13 +11,11 @@ dot(const float x[3], const float y[3]) { void normalize(float x[3]) { - float len; - int i; - - len = sqrt(dot(x, x)); + float len = 1.0f / sqrtf(dot(x, x)); - for(i = 0; i < 3; ++i) - x[i] /= len; + x[0] *= len; + x[1] *= len; + x[2] *= len; } float -- cgit v1.2.3 From ef3f2085ff238469207d099562d61635e1f9e138 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 15:02:25 -0500 Subject: 3dmath.c: Throw restrict on all parameters to sphere_intersect This saves 3ms / frame on yelena. --- 3dmath.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to '3dmath.c') diff --git a/3dmath.c b/3dmath.c index 5b876f2..2693c32 100644 --- a/3dmath.c +++ b/3dmath.c @@ -19,7 +19,9 @@ normalize(float x[3]) { } float -sphere_intersect(float y[3], float r[3], const float s[3], const float d[3], const float c[3], float R) { +sphere_intersect(float* restrict y, float* restrict r, + const float* restrict s, const float* restrict d, + const float* restrict c, float R) { int i; float D, n[3], t, v[3]; -- cgit v1.2.3 From 59eb492aac9d1d5a005858f9e32b34ac79cb02a6 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 15:10:58 -0500 Subject: 3dmath.c: Early out of sphere_intersect on t <= 0 The loop in ray.c discards any data from intersections with t <= 0 anyway, so we might as well not calculate it. This saves 15 ms/frame on yelena. --- 3dmath.c | 3 +++ 1 file changed, 3 insertions(+) (limited to '3dmath.c') diff --git a/3dmath.c b/3dmath.c index 2693c32..866af09 100644 --- a/3dmath.c +++ b/3dmath.c @@ -35,6 +35,9 @@ sphere_intersect(float* restrict y, float* restrict r, t = -dot(v, d) - D; + if (t <= 0) + return -1; + for(i = 0; i < 3; ++i) { y[i] = s[i] + t * d[i]; n[i] = y[i] - c[i]; -- cgit v1.2.3 From 4ddcfc0bb65d82d4e3fd2d424912086a4b454fdf Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 15:53:29 -0500 Subject: 3dmath.c: Remove sqrt from sphere_intersect Interestingly, this seems to only save 1ms/frame on yelena. But that's still 16% of a 60fps frame. --- 3dmath.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to '3dmath.c') diff --git a/3dmath.c b/3dmath.c index 866af09..9e7f7a1 100644 --- a/3dmath.c +++ b/3dmath.c @@ -43,10 +43,10 @@ sphere_intersect(float* restrict y, float* restrict r, n[i] = y[i] - c[i]; } - normalize(n); + float two_dot_nd_div_sq_n_mag = 2.0f * dot(n, d) / dot(n, n); - for(i = 0; i < 3; ++i) - r[i] = d[i] - 2 * dot(n, d) * n[i]; + for (i = 0; i < 3; ++i) + r[i] = d[i] - two_dot_nd_div_sq_n_mag * n[i]; return t; } -- cgit v1.2.3 From 6cee50ceb0d153622b89fb813060419f5985857d Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 21:32:20 -0500 Subject: Subtract the central sphere, and update the animation to match --- 3dmath.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to '3dmath.c') diff --git a/3dmath.c b/3dmath.c index 9e7f7a1..5bc1861 100644 --- a/3dmath.c +++ b/3dmath.c @@ -2,8 +2,6 @@ #include -#define POW2(x) ((x) * (x)) - float dot(const float x[3], const float y[3]) { return x[0] * y[0] + x[1] * y[1] + x[2] * y[2]; @@ -21,7 +19,7 @@ normalize(float x[3]) { float sphere_intersect(float* restrict y, float* restrict r, const float* restrict s, const float* restrict d, - const float* restrict c, float R) { + const float* restrict c, float R, int invert) { int i; float D, n[3], t, v[3]; @@ -33,7 +31,10 @@ sphere_intersect(float* restrict y, float* restrict r, if(D < 0) return -1; - t = -dot(v, d) - D; + if (invert) + t = -dot(v, d) + D; + else + t = -dot(v, d) - D; if (t <= 0) return -1; -- cgit v1.2.3