shithub: tinygl

Download patch

ref: 8207a583af54b780757a278914e1deae8f2d1040
parent: 18949a3f82797cdde959037b56781e1437b56176
author: David <gek@katherine>
date: Fri Feb 19 11:53:15 EST 2021

Before ztriangle.h overhaul

--- a/include/zfeatures.h
+++ b/include/zfeatures.h
@@ -11,6 +11,8 @@
 //This slows down the renderer so we don't usually do it, but
 //it's part of the GL spec.
 #define TGL_FEATURE_STRICT_OOM_CHECKS 0
+//Use Fast Inverse Square Root. Toggleable because it's actually slower on some systems, i've heard.
+#define TGL_FEATURE_FISR 1
 //Clientside Arrays
 #define TGL_FEATURE_ARRAYS         1
 //This library has a super cool opcode system based on function pointers, it's pretty fast too.
--- a/src/get.c
+++ b/src/get.c
@@ -115,6 +115,9 @@
 #if TGL_FEATURE_STRICT_OOM_CHECKS == 1
 "TGL_FEATURE_STRICT_OOM_CHECKS "
 #endif
+#if TGL_FEATURE_FISR == 1
+"TGL_FEATURE_FISR "
+#endif 
 #if TGL_FEATURE_ARRAYS == 1
 "TGL_FEATURE_ARRAYS "
 #endif
--- a/src/light.c
+++ b/src/light.c
@@ -274,15 +274,24 @@
 			d.X = l->position.v[0] - v->ec.v[0];
 			d.Y = l->position.v[1] - v->ec.v[1];
 			d.Z = l->position.v[2] - v->ec.v[2];
-			tmp = fastInvSqrt(d.X * d.X + d.Y * d.Y + d.Z * d.Z);
-			//dist = sq_rt(d.X * d.X + d.Y * d.Y + d.Z * d.Z);
+#if TGL_FEATURE_FISR == 1
+			tmp = fastInvSqrt(d.X * d.X + d.Y * d.Y + d.Z * d.Z); //FISR IMPL, MATCHED!
 			dist = 1.0f/tmp;
 			if (dist > 1E-3) {
+				d.X *= tmp;
+				d.Y *= tmp;
+				d.Z *= tmp;
+			}
+#else
+			dist = sqrt(d.X * d.X + d.Y * d.Y + d.Z * d.Z);
+			//dist = 1.0f/tmp;
+			if (dist > 1E-3) {
 				tmp = 1 / dist;
 				d.X *= tmp;
 				d.Y *= tmp;
 				d.Z *= tmp;
 			}
+#endif
 			att = 1.0f / (l->attenuation[0] + dist * (l->attenuation[1] + dist * l->attenuation[2]));
 		}
 		dot = d.X * n.X + d.Y * n.Y + d.Z * n.Z;
@@ -335,12 +344,19 @@
 				if (dot_spec > 0) {
 					GLSpecBuf* specbuf;
 					GLint idx;
-					//tmp = sqrt(s.X * s.X + s.Y * s.Y + s.Z * s.Z);
-					tmp = fastInvSqrt(s.X * s.X + s.Y * s.Y + s.Z * s.Z);
+#if TGL_FEATURE_FISR == 1
+					tmp = fastInvSqrt(s.X * s.X + s.Y * s.Y + s.Z * s.Z); //FISR IMPL, MATCHED!
 					if (tmp < 1E+3) {
-//						dot_spec = dot_spec / tmp;
 						dot_spec = dot_spec * tmp;
 					} else dot_spec = 0;
+#else
+				//reference implementation.
+					tmp=sqrt(s.X*s.X+s.Y*s.Y+s.Z*s.Z);
+			        if (tmp > 1E-3) {
+			          dot_spec=dot_spec / tmp;
+			        } else dot_spec = 0;
+#endif
+					
 
 					/* TODO: optimize */
 					/* testing specular buffer code */
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -134,15 +134,18 @@
 		GLfloat cost, sint;
 
 		/* normalize vector */
+		
+#if TGL_FEATURE_FISR == 1
 		GLfloat len = u[0]  + u[1] + u[2];
 		if (len == 0.0f)
 			return;
-/*OLD
-		
-*/
-//NEW
-		len = fastInvSqrt(len);
-		//len = 1.0f / sqrt(len);
+		len = fastInvSqrt(len); //FISR
+#else
+		GLfloat len = u[0] * u[0]  + u[1] * u[1] + u[2] * u[2];
+		if (len == 0.0f)
+			return;
+		len = 1.0f / sqrt(len);
+#endif
 		u[0] *= len;
 		u[1] *= len;
 		u[2] *= len;
--- a/src/zmath.c
+++ b/src/zmath.c
@@ -239,9 +239,16 @@
 
 int gl_V3_Norm_Fast(V3* a) {
 	GLfloat n;
-	n = fastInvSqrt(a->X * a->X + a->Y * a->Y + a->Z * a->Z);
+#if TGL_FEATURE_FISR == 1
+	n = fastInvSqrt(a->X * a->X + a->Y * a->Y + a->Z * a->Z); //FISR
+	if(n>1E+3)
+		return 1;
+#else
+	n = sqrt(a->X * a->X + a->Y * a->Y + a->Z * a->Z); //NONFISR
 	if (n == 0)
 		return 1;
+	n = 1.0 / n;
+#endif
 	a->X *= n;
 	a->Y *= n;
 	a->Z *= n;
--- a/src/zmath.h
+++ b/src/zmath.h
@@ -52,7 +52,7 @@
 
 int gl_Matrix_Inv(GLfloat* r, GLfloat* m, GLint n);
 
-
+/*
 static inline GLfloat fastInvSqrt(float x){
 	GLint i; GLfloat y;
 	memcpy(&i, &x, 4);
@@ -61,5 +61,15 @@
 	memcpy(&y, &i, 4);
 	return y * (1.5F - 0.5F * x * y * y);
 }
+*/
+#if TGL_FEATURE_FISR == 1
+static inline GLfloat fastInvSqrt(float x){
+	union{GLfloat f; GLint i;} conv;
+	conv.f = x;
+	conv.i = 0x5F1FFFF9 - (conv.i>>1);
+	conv.f *= 0.703952253f * (2.38924456f - x * conv.f * conv.f);
+	return conv.f;
+}
+#endif
 #endif
 // __ZMATH__