shithub: tinygl

Download patch

ref: b99349490192aa284dbf1cde3d5f86154e3c9c9d
parent: 61100b1d5795d82168cd9d08b13dae5f26425dc8
author: David <gek@katherine>
date: Tue Feb 23 07:37:45 EST 2021

Perf boost!

--- a/src/clip.c
+++ b/src/clip.c
@@ -20,17 +20,10 @@
 	v->zp.z = (GLint)(v->pc.Z * winv * c->viewport.scale.Z + c->viewport.trans.Z);
 	}
 	/* color */
-	//{
-	//GLuint val;
 	v->zp.r = (GLuint)(v->color.v[0] * COLOR_CORRECTED_MULT_MASK + COLOR_MIN_MULT) & COLOR_MASK;
-	//v->zp.r = (val<COLOR_MIN_MULT)?COLOR_MIN_MULT:val;
-	//v->zp.r = val;
 	v->zp.g = (GLuint)(v->color.v[1] * COLOR_CORRECTED_MULT_MASK + COLOR_MIN_MULT) & COLOR_MASK;
-	//v->zp.g = (val<COLOR_MIN_MULT)?COLOR_MIN_MULT:val;
-	//v->zp.g = val;
 	v->zp.b = (GLuint)(v->color.v[2] * COLOR_CORRECTED_MULT_MASK + COLOR_MIN_MULT) & COLOR_MASK;
-	//v->zp.b = val;
-	//}
+
 	/* texture */
 
 	if (c->texture_2d_enabled) {
@@ -69,7 +62,7 @@
 }
 
 /* line */
-
+//Used only for lines.
 static inline void GLinterpolate(GLVertex* q, GLVertex* p0, GLVertex* p1, GLfloat t) {
 	q->pc.X = p0->pc.X + (p1->pc.X - p0->pc.X) * t;
 	q->pc.Y = p0->pc.Y + (p1->pc.Y - p0->pc.Y) * t;
@@ -172,7 +165,7 @@
  * We compute the point 'c' of GLintersection and the value of the parameter 't'
  * of the GLintersection if x=a+t(b-a).
  */
-
+//MARK <POSSIBLE_PERF_BONUS>
 #define clip_func(name, sign, dir, dir1, dir2)                                                                                                                 \
 	static GLfloat name(V4* c, V4* a, V4* b) {                                                                                                                 \
 		GLfloat t, dX, dY, dZ, dW, den;                                                                                                                        \
@@ -191,7 +184,7 @@
 		c->dir = sign c->W;                                                                                                                                    \
 		return t;                                                                                                                                              \
 	}
-
+//MARK <POSSIBLE_PERF_BONUS>
 clip_func(clip_xmin, -, X, Y, Z)
 
 	clip_func(clip_xmax, +, X, Y, Z)
@@ -199,7 +192,7 @@
 		clip_func(clip_ymin, -, Y, X, Z)
 
 			clip_func(clip_ymax, +, Y, X, Z)
-
+//MARK <POSSIBLE_PERF_BONUS>
 				clip_func(clip_zmin, -, Z, X, Y)
 
 					clip_func(clip_zmax, +, Z, X, Y)
@@ -242,7 +235,7 @@
 		GLfloat norm;
 		norm = (GLfloat)(p1->zp.x - p0->zp.x) * (GLfloat)(p2->zp.y - p0->zp.y) - (GLfloat)(p2->zp.x - p0->zp.x) * (GLfloat)(p1->zp.y - p0->zp.y);
 
-		if (norm == 0)
+		if (norm == 0) //MARK <POSSIBLE_PERF_BONUS>
 			return;
 
 		front = norm < 0.0;
@@ -304,7 +297,7 @@
 		}
 
 		/* this test can be true only in case of rounding errors */
-		if (clip_bit == 6) {
+		if (clip_bit == 6) { //The 2 bit and the 4 bit.
 #if 0
       tgl_warning("Error:\n");tgl_warning("%f %f %f %f\n",p0->pc.X,p0->pc.Y,p0->pc.Z,p0->pc.W);tgl_warning("%f %f %f %f\n",p1->pc.X,p1->pc.Y,p1->pc.Z,p1->pc.W);tgl_warning("%f %f %f %f\n",p2->pc.X,p2->pc.Y,p2->pc.Z,p2->pc.W);
 #endif
@@ -394,42 +387,11 @@
 
 #ifdef PROFILE
 int count_triangles, count_triangles_textured, count_pixels;
+#warning "Compile with PROFILE slows down everything"
 #endif
 
 //see vertex.c to see how the draw functions are assigned.
 void gl_draw_triangle_fill(GLContext* c, GLVertex* p0, GLVertex* p1, GLVertex* p2) {
-	// puts("\n <yes, it's draw_triangle_fill>");
-#ifdef PROFILE
-	{
-		GLint norm;
-#if TGL_FEATURE_ERROR_CHECK == 1
-	if(!(
-		(p0->zp.x >= 0 && p0->zp.x < c->zb->xsize) &&
-		(p0->zp.y >= 0 && p0->zp.y < c->zb->ysize) &&
-		(p1->zp.x >= 0 && p1->zp.x < c->zb->xsize) &&
-		
-		(p1->zp.y >= 0 && p1->zp.y < c->zb->ysize) &&
-		(p2->zp.x >= 0 && p2->zp.x < c->zb->xsize) &&
-		(p2->zp.y >= 0 && p2->zp.y < c->zb->ysize)
-	))
-#define ERROR_FLAG GL_INVALID_VALUE
-#include "error_check.h"
-#else
-	if(!(
-			(p0->zp.x >= 0 && p0->zp.x < c->zb->xsize) &&
-			(p0->zp.y >= 0 && p0->zp.y < c->zb->ysize) &&
-			(p1->zp.x >= 0 && p1->zp.x < c->zb->xsize) &&
-			
-			(p1->zp.y >= 0 && p1->zp.y < c->zb->ysize) &&
-			(p2->zp.x >= 0 && p2->zp.x < c->zb->xsize) &&
-			(p2->zp.y >= 0 && p2->zp.y < c->zb->ysize)
-		)) return;
-#endif
-		norm = (p1->zp.x - p0->zp.x) * (p2->zp.y - p0->zp.y) - (p2->zp.x - p0->zp.x) * (p1->zp.y - p0->zp.y);
-		count_pixels += abs(norm) / 2;
-		count_triangles++;
-	}
-#endif
 
 	if (c->texture_2d_enabled) {
 		//if(c->current_texture)
@@ -445,9 +407,9 @@
 		}
 #endif
 		{
-#ifdef PROFILE
-			count_triangles_textured++;
-#endif
+//#ifdef PROFILE
+//			count_triangles_textured++;
+//#endif
 			ZB_setTexture(c->zb, c->current_texture->images[0].pixmap);
 #if TGL_FEATURE_BLEND == 1
 			if(c->zb->enable_blend) 
--- a/src/light.c
+++ b/src/light.c
@@ -32,19 +32,10 @@
 	case GL_AMBIENT:
 		for (i = 0; i < 4; i++)
 			m->ambient.v[i] = clampf(v[i],0,1);
-		// c->current_color.X=v[0];
-		// c->current_color.Y=v[1];
-		// c->current_color.Z=v[2];
-		// c->current_color.W=v[3];
 		break;
 	case GL_DIFFUSE:
 		for (i = 0; i < 4; i++)
 			m->diffuse.v[i] = clampf(v[i],0,1);
-
-		// c->current_color.X=v[0];
-		// c->current_color.Y=v[1];
-		// c->current_color.Z=v[2];
-		// c->current_color.W=v[3];
 		break;
 	case GL_SPECULAR:
 		for (i = 0; i < 4; i++)
@@ -57,27 +48,19 @@
 #endif
 		break;
 	case GL_AMBIENT_AND_DIFFUSE:
-		// printf("\nRECEIVED AMBIENT AND DIFFUSE COLOR %f, %f, %f, %f", v[0], v[1], v[2], v[3]);
 		for (i = 0; i < 4; i++)
 			m->diffuse.v[i] = clampf(v[i],0,1);
-		// c->current_color.X=v[0];
-		// c->current_color.Y=v[1];
-		// c->current_color.Z=v[2];
-		// c->current_color.W=v[3];
 
 		for (i = 0; i < 4; i++)
 			m->ambient.v[i] = clampf(v[i],0,1);
 		break;
-	default:
-		//c->current_color.X = clampf(v[0],0,1)
-		//c->current_color.Y = clampf(v[1],0,1)
-		//c->current_color.Z = clampf(v[2],0,1)
-		//c->current_color.W = clampf(v[3],0,1)
+	
 #if TGL_FEATURE_ERROR_CHECK == 1
+	default:
 #define ERROR_FLAG GL_INVALID_ENUM
 #include "error_check.h"
 #else
-	return;
+	//default: return;
 #endif
 	}
 }
@@ -103,8 +86,9 @@
 	if(!(light >= GL_LIGHT0 && light < GL_LIGHT0 + MAX_LIGHTS))
 #define ERROR_FLAG GL_INVALID_OPERATION
 #include "error_check.h"
+
 #else
-	//assert(light >= GL_LIGHT0 && light < GL_LIGHT0 + MAX_LIGHTS);
+//	if(!(light >= GL_LIGHT0 && light < GL_LIGHT0 + MAX_LIGHTS)) return;
 #endif
 
 	l = &c->lights[light - GL_LIGHT0];
--- a/src/vertex.c
+++ b/src/vertex.c
@@ -28,9 +28,6 @@
 	c->current_color.Y = p[2].f;
 	c->current_color.Z = p[3].f;
 	c->current_color.W = p[4].f;
-	// c->longcurrent_color[0] = p[5].ui; //MARKED
-	// c->longcurrent_color[1] = p[6].ui; //MARKED
-	// c->longcurrent_color[2] = p[7].ui; //MARKED
 
 	if (c->color_material_enabled) {
 		GLParam q[7];
@@ -68,7 +65,7 @@
 #define ERROR_FLAG GL_INVALID_OPERATION
 #include "error_check.h"
 #else
-	if(c->in_begin != 0)return; //<COST>
+	//if(c->in_begin != 0)return; //<COST>
 #endif
 	type = p[1].i;
 	c->begin_type = type;
--- a/src/zmath.c
+++ b/src/zmath.c
@@ -2,7 +2,7 @@
    the function names :-) */
 
 #include "zmath.h"
-#include <math.h>
+
 #include <stdlib.h>
 #include <string.h>
 
@@ -237,25 +237,10 @@
 
 //NEW
 
-int gl_V3_Norm_Fast(V3* a) {
-	GLfloat n;
-#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;
-	return 0;
-}
 
+
 // OLD
+/*
 int gl_V3_Norm(V3* a) {
 	GLfloat n;
 	n = sqrt(a->X * a->X + a->Y * a->Y + a->Z * a->Z);
@@ -266,7 +251,7 @@
 	a->Z /= n;
 	return 0;
 }
-
+*/
 V3 gl_V3_New(GLfloat x, GLfloat y, GLfloat z) {
 	V3 a;
 	a.X = x;
--- a/src/zmath.h
+++ b/src/zmath.h
@@ -3,6 +3,7 @@
 #include "../include/GL/gl.h"
 #include <stdlib.h>
 #include <string.h> //For memcpy
+#include <math.h>
 /* Matrix & Vertex */
 
 typedef struct {
@@ -44,9 +45,11 @@
 void gl_M4_MulLeft(M4* c, M4* a);
 void gl_M4_Transpose(M4* a, M4* b);
 void gl_M4_Rotate(M4* c, GLfloat t, GLint u);
-int gl_V3_Norm(V3* a);
-int gl_V3_Norm_Fast(V3* a);
+//int gl_V3_Norm(V3* a);
+//int gl_V3_Norm_Fast(V3* a);
 
+
+
 V3 gl_V3_New(GLfloat x, GLfloat y, GLfloat z);
 V4 gl_V4_New(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
 
@@ -63,7 +66,7 @@
 }
 */
 #if TGL_FEATURE_FISR == 1
-static inline GLfloat fastInvSqrt(float x){
+inline GLfloat fastInvSqrt(float x){
 	union{GLfloat f; GLint i;} conv;
 	conv.f = x;
 	conv.i = 0x5F1FFFF9 - (conv.i>>1);
@@ -71,5 +74,24 @@
 	return conv.f;
 }
 #endif
+
+
+inline int gl_V3_Norm_Fast(V3* a) {
+	GLfloat n;
+#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;
+	return 0;
+}
 #endif
 // __ZMATH__