shithub: qk1

Download patch

ref: 4fe828ca722d298bcb90d8874df9c0aae6fa68cc
parent: 0f07da746048c1452a42d3a964157448967c357f
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Tue Nov 21 13:04:59 EST 2023

clean up a bunch

--- a/common.h
+++ b/common.h
@@ -32,7 +32,7 @@
 
 #define max(a,b) ((a)>(b)?(a):(b))
 #define min(a,b) ((a)<(b)?(a):(b))
-#define clamp(x,a,b) (min(max((x),(a)),(b)))
+#define clamp(x,a,b) ((x)>(b)?(b):((x)<(a)?(a):(x)))
 
 #define Q_MAXCHAR ((char)0x7f)
 #define Q_MAXSHORT ((short)0x7fff)
--- a/d_edge.c
+++ b/d_edge.c
@@ -2,92 +2,65 @@
 
 static int	miplevel;
 
-float		scale_for_mip;
-int			ubasestep, errorterm, erroradjustup, erroradjustdown;
+float scale_for_mip;
 
 // FIXME: should go away
-extern void			R_RotateBmodel (void);
-extern void			R_TransformFrustum (void);
+extern void R_RotateBmodel (void);
+extern void R_TransformFrustum (void);
 
-/*
-=============
-D_MipLevelForScale
-=============
-*/
-static int D_MipLevelForScale (float scale)
+static int
+D_MipLevelForScale(float scale)
 {
-	int		lmiplevel;
+	int lmiplevel;
 
-	if (scale >= d_scalemip[0] )
+	if(scale >= d_scalemip[0])
 		lmiplevel = 0;
-	else if (scale >= d_scalemip[1] )
+	else if(scale >= d_scalemip[1])
 		lmiplevel = 1;
-	else if (scale >= d_scalemip[2] )
+	else if(scale >= d_scalemip[2])
 		lmiplevel = 2;
 	else
 		lmiplevel = 3;
 
-	if (lmiplevel < d_minmip)
-		lmiplevel = d_minmip;
-
-	return lmiplevel;
+	return max(d_minmip, lmiplevel);
 }
 
-
-/*
-==============
-D_DrawSolidSurface
-==============
-*/
-
 // FIXME: clean this up
-
-static void D_DrawSolidSurface (surf_t *surf, int color)
+static void
+D_DrawSolidSurface(surf_t *surf, int color)
 {
-	espan_t	*span;
-	byte	*pdest;
-	int		u, u2, pix;
+	espan_t *span;
+	byte *pdest;
+	int u, u2, pix;
 
 	pix = (color<<24) | (color<<16) | (color<<8) | color;
-	for (span=surf->spans ; span ; span=span->pnext)
-	{
+	for(span = surf->spans; span; span=span->pnext){
 		pdest = (byte *)d_viewbuffer + screenwidth*span->v;
 		u = span->u;
 		u2 = span->u + span->count - 1;
-		((byte *)pdest)[u] = pix;
+		pdest[u] = pix;
 
-		if (u2 - u < 8)
-		{
-			for (u++ ; u <= u2 ; u++)
-				((byte *)pdest)[u] = pix;
+		if (u2 - u < 8){
+			for(u++; u <= u2; u++)
+				pdest[u] = pix;
+		}else{
+			for(u++ ; u & 3; u++)
+				pdest[u] = pix;
+			for(u2 -= 4; u <= u2; u += 4)
+				*(int *)(pdest + u) = pix;
+			for(u2 += 4; u <= u2; u++)
+				pdest[u] = pix;
 		}
-		else
-		{
-			for (u++ ; u & 3 ; u++)
-				((byte *)pdest)[u] = pix;
-
-			u2 -= 4;
-			for ( ; u <= u2 ; u+=4)
-				*(int *)((byte *)pdest + u) = pix;
-			u2 += 4;
-			for ( ; u <= u2 ; u++)
-				((byte *)pdest)[u] = pix;
-		}
 	}
 }
 
 
-/*
-==============
-D_CalcGradients
-==============
-*/
-static void D_CalcGradients (msurface_t *pface, vec3_t transformed_modelorg)
+static void
+D_CalcGradients(msurface_t *pface, vec3_t transformed_modelorg)
 {
-	float		mipscale;
-	vec3_t		p_temp1;
-	vec3_t		p_saxis, p_taxis;
-	float		t;
+	float mipscale;
+	vec3_t p_temp1, p_saxis, p_taxis;
+	float t;
 
 	mipscale = 1.0 / (float)(1 << miplevel);
 
@@ -122,28 +95,21 @@
 	bbextentt = ((pface->extents[1] << 16) >> miplevel) - 1;
 }
 
-/*
-==============
-D_DrawSurfaces
-==============
-*/
-void D_DrawSurfaces (void)
+void
+D_DrawSurfaces (void)
 {
-	surf_t			*s;
-	msurface_t		*pface;
-	surfcache_t		*pcurrentcache;
-	vec3_t			world_transformed_modelorg;
-	vec3_t			local_modelorg;
-	vec3_t			transformed_modelorg;
-	byte			alpha;
+	surf_t *s;
+	msurface_t *pface;
+	surfcache_t *pcurrentcache;
+	vec3_t world_transformed_modelorg, local_modelorg, transformed_modelorg;
+	byte alpha;
 
 	currententity = &cl_entities[0];
-	TransformVector (modelorg, transformed_modelorg);
-	VectorCopy (transformed_modelorg, world_transformed_modelorg);
+	TransformVector(modelorg, transformed_modelorg);
+	VectorCopy(transformed_modelorg, world_transformed_modelorg);
 
 	// TODO: could preset a lot of this at mode set time
-	for (s = &surfaces[1] ; s<surface_p ; s++)
-	{
+	for(s = &surfaces[1]; s < surface_p; s++){
 		if (!s->spans)
 			continue;
 
@@ -157,19 +123,14 @@
 		if(alpha < 1)
 			alpha = 255;
 
-		r_drawnpolycount++;
-
 		d_zistepu = s->d_zistepu;
 		d_zistepv = s->d_zistepv;
 		d_ziorigin = s->d_ziorigin;
 
-		if (s->flags & SURF_DRAWSKY)
-		{
-			D_DrawSkyScans8 (s->spans);
-			D_DrawZSpans (s->spans);
-		}
-		else if (s->flags & SURF_DRAWBACKGROUND)
-		{
+		if(s->flags & SURF_DRAWSKY){
+			D_DrawSkyScans8(s->spans);
+			D_DrawZSpans(s->spans);
+		}else if(s->flags & SURF_DRAWBACKGROUND){
 			// set up a gradient for the background surface that places it
 			// effectively at infinity distance from the viewpoint
 			d_zistepu = 0;
@@ -176,29 +137,23 @@
 			d_zistepv = 0;
 			d_ziorigin = -0.9;
 
-			D_DrawSolidSurface (s, (int)r_clearcolor.value & 0xFF);
-			D_DrawZSpans (s->spans);
-		}
-		else if (s->flags & SURF_DRAWTURB)
-		{
+			D_DrawSolidSurface(s, (int)r_clearcolor.value & 0xFF);
+			D_DrawZSpans(s->spans);
+		}else if(s->flags & SURF_DRAWTURB){
 			pface = s->data;
 			miplevel = 0;
-			cacheblock = (pixel_t *)
-					((byte *)pface->texinfo->texture +
-					pface->texinfo->texture->offsets[0]);
+			cacheblock = (pixel_t *)((byte *)pface->texinfo->texture + pface->texinfo->texture->offsets[0]);
 			cachewidth = 64;
 
-			if (s->insubmodel)
-			{
+			if(s->insubmodel){
 				// FIXME: we don't want to do all this for every polygon!
 				// TODO: store once at start of frame
 				currententity = s->entity;	//FIXME: make this passed in to
-											// R_RotateBmodel ()
-				VectorSubtract (r_origin, currententity->origin,
-						local_modelorg);
-				TransformVector (local_modelorg, transformed_modelorg);
+											// R_RotateBmodel()
+				VectorSubtract(r_origin, currententity->origin, local_modelorg);
+				TransformVector(local_modelorg, transformed_modelorg);
 
-				R_RotateBmodel ();	// FIXME: don't mess with the frustum,
+				R_RotateBmodel();	// FIXME: don't mess with the frustum,
 									// make entity passed in
 			}
 
@@ -206,33 +161,29 @@
 			Turbulent8 (s->spans, alpha);
 			D_DrawZSpans (s->spans);
 
-			if (s->insubmodel)
-			{
+			if(s->insubmodel){
 				// restore the old drawing state
 				// FIXME: we don't want to do this every time!
 				// TODO: speed up
 				currententity = &cl_entities[0];
-				VectorCopy (world_transformed_modelorg,
-							transformed_modelorg);
-				VectorCopy (base_vpn, vpn);
-				VectorCopy (base_vup, vup);
-				VectorCopy (base_vright, vright);
-				VectorCopy (base_modelorg, modelorg);
-				R_TransformFrustum ();
+				VectorCopy(world_transformed_modelorg, transformed_modelorg);
+				VectorCopy(base_vpn, vpn);
+				VectorCopy(base_vup, vup);
+				VectorCopy(base_vright, vright);
+				VectorCopy(base_modelorg, modelorg);
+				R_TransformFrustum();
 			}
 		}
 		else
 		{
-			if (s->insubmodel)
-			{
+			if(s->insubmodel){
 				// FIXME: we don't want to do all this for every polygon!
 				// TODO: store once at start of frame
 				currententity = s->entity;	//FIXME: make this passed in to
-											// R_RotateBmodel ()
-				VectorSubtract (r_origin, currententity->origin, local_modelorg);
-				TransformVector (local_modelorg, transformed_modelorg);
-
-				R_RotateBmodel ();	// FIXME: don't mess with the frustum,
+											// R_RotateBmodel()
+				VectorSubtract(r_origin, currententity->origin, local_modelorg);
+				TransformVector(local_modelorg, transformed_modelorg);
+				R_RotateBmodel();	// FIXME: don't mess with the frustum,
 									// make entity passed in
 			}
 
@@ -240,26 +191,21 @@
 			if(s->flags & SURF_FENCE)
 				miplevel = 0;
 			else
-				miplevel = D_MipLevelForScale (s->nearzi * scale_for_mip
-				* pface->texinfo->mipadjust);
+				miplevel = D_MipLevelForScale(s->nearzi * scale_for_mip * pface->texinfo->mipadjust);
 
 			// FIXME: make this passed in to D_CacheSurface
-			pcurrentcache = D_CacheSurface (pface, miplevel);
+			pcurrentcache = D_CacheSurface(pface, miplevel);
 
 			cacheblock = (pixel_t *)pcurrentcache->data;
 			cachewidth = pcurrentcache->width;
 
-			D_CalcGradients (pface, transformed_modelorg);
+			D_CalcGradients(pface, transformed_modelorg);
 
-			if(s->flags & SURF_FENCE)
-				D_DrawSpans16_Fence(s->spans, alpha);
-			else{
-				(*d_drawspans) (s->spans, alpha);
-				D_DrawZSpans (s->spans);
-			}
+			D_DrawSpans16(s->spans, s->flags & SURF_FENCE, alpha);
+			if((r_drawflags & DRAW_BLEND) == 0)
+				D_DrawZSpans(s->spans);
 
-			if (s->insubmodel)
-			{
+			if(s->insubmodel){
 				//
 				// restore the old drawing state
 				// FIXME: we don't want to do this every time!
@@ -266,13 +212,12 @@
 				// TODO: speed up
 				//
 				currententity = &cl_entities[0];
-				VectorCopy (world_transformed_modelorg,
-							transformed_modelorg);
-				VectorCopy (base_vpn, vpn);
-				VectorCopy (base_vup, vup);
-				VectorCopy (base_vright, vright);
-				VectorCopy (base_modelorg, modelorg);
-				R_TransformFrustum ();
+				VectorCopy(world_transformed_modelorg, transformed_modelorg);
+				VectorCopy(base_vpn, vpn);
+				VectorCopy(base_vup, vup);
+				VectorCopy(base_vright, vright);
+				VectorCopy(base_modelorg, modelorg);
+				R_TransformFrustum();
 			}
 		}
 	}
--- a/d_iface.h
+++ b/d_iface.h
@@ -74,7 +74,6 @@
 	float			nearzi;
 } spritedesc_t;
 
-extern int		d_spanpixcount;
 extern int		r_framecount;		// sequence # of current frame since Quake
 									//  started
 extern bool	r_recursiveaffinetriangles;	// true if a driver wants to use
--- a/d_init.c
+++ b/d_init.c
@@ -1,64 +1,43 @@
 #include "quakedef.h"
 
-#define NUM_MIPS	4
+static cvar_t d_mipcap = {"d_mipcap", "0"};
+static cvar_t d_mipscale = {"d_mipscale", "1"};
 
-static cvar_t	d_mipcap = {"d_mipcap", "0"};
-static cvar_t	d_mipscale = {"d_mipscale", "1"};
+surfcache_t *d_initial_rover;
+bool d_roverwrapped;
+int d_minmip;
+float d_scalemip[MIPLEVELS-1];
 
-surfcache_t		*d_initial_rover;
-bool		d_roverwrapped;
-int				d_minmip;
-float			d_scalemip[NUM_MIPS-1];
+static float basemip[MIPLEVELS-1] = {1.0, 0.5*0.8, 0.25*0.8};
 
-static float	basemip[NUM_MIPS-1] = {1.0, 0.5*0.8, 0.25*0.8};
-
-void (*d_drawspans) (espan_t *pspan, byte alpha);
-
-
-/*
-===============
-D_Init
-===============
-*/
-void D_Init (void)
+void
+D_Init(void)
 {
-	Cvar_RegisterVariable (&d_mipcap);
-	Cvar_RegisterVariable (&d_mipscale);
+	Cvar_RegisterVariable(&d_mipcap);
+	Cvar_RegisterVariable(&d_mipscale);
 
 	r_recursiveaffinetriangles = true;
 	r_aliasuvscale = 1.0;
 }
 
-/*
-===============
-D_SetupFrame
-===============
-*/
-void D_SetupFrame (void)
+void
+D_SetupFrame(void)
 {
-	int		i;
+	int i;
 
-	if (r_dowarp)
+	if(r_dowarp){
 		d_viewbuffer = r_warpbuffer;
-	else
-		d_viewbuffer = (void *)(byte *)vid.buffer;
-
-	if (r_dowarp)
 		screenwidth = WARP_WIDTH;
-	else
+	}else{
+		d_viewbuffer = (void *)vid.buffer;
 		screenwidth = vid.rowbytes;
+	}
 
 	d_roverwrapped = false;
 	d_initial_rover = sc_rover;
 
-	d_minmip = d_mipcap.value;
-	if (d_minmip > 3)
-		d_minmip = 3;
-	else if (d_minmip < 0)
-		d_minmip = 0;
+	d_minmip = clamp(d_mipcap.value, 0, MIPLEVELS-1);
 
-	for (i=0 ; i<(NUM_MIPS-1) ; i++)
+	for(i = 0; i < MIPLEVELS-1; i++)
 		d_scalemip[i] = basemip[i] * d_mipscale.value;
-
-	d_drawspans = D_DrawSpans16;
 }
--- a/d_local.h
+++ b/d_local.h
@@ -50,14 +50,11 @@
 extern fixed16_t	sadjust, tadjust;
 extern fixed16_t	bbextents, bbextentt;
 
-void D_DrawSpans8 (espan_t *pspans);
-void D_DrawSpans16 (espan_t *pspans, byte alpha);
+void D_DrawSpans16 (espan_t *pspans, int forceblend, byte alpha);
 void D_DrawZSpans (espan_t *pspans);
 void Turbulent8 (espan_t *pspan, byte alpha);
-void D_DrawSpans16_Fence (espan_t *pspan, byte alpha);
 
 void D_DrawSkyScans8 (espan_t *pspan);
-void D_DrawSkyScans16 (espan_t *pspan);
 
 void R_ShowSubDiv (void);
 surfcache_t	*D_CacheSurface (msurface_t *surface, int miplevel);
@@ -78,8 +75,6 @@
 
 extern int		d_minmip;
 extern float	d_scalemip[3];
-
-extern void (*d_drawspans) (espan_t *pspan, byte alpha);
 
 enum {
 	// perhaps a bit too much, but looks ok
--- a/d_polyse.c
+++ b/d_polyse.c
@@ -63,6 +63,7 @@
 static int d_sfracbasestep, d_tfracbasestep;
 static int d_ziextrastep, d_zibasestep;
 static int d_pzextrastep, d_pzbasestep;
+static int ubasestep, errorterm, erroradjustup, erroradjustdown;
 
 typedef struct {
 	int		quotient;
--- a/d_scan.c
+++ b/d_scan.c
@@ -32,28 +32,20 @@
 	wratio = w / (float)scr_vrect.width;
 	hratio = h / (float)scr_vrect.height;
 
-	for (v=0 ; v<scr_vrect.height+AMP2*2 ; v++)
-	{
-		rowptr[v] = d_viewbuffer + (r_refdef.vrect.y * screenwidth) +
-				 (screenwidth * (int)((float)v * hratio * h / (h + AMP2 * 2)));
-	}
+	for(v = 0; v < scr_vrect.height+AMP2*2; v++)
+		rowptr[v] = d_viewbuffer + (r_refdef.vrect.y * screenwidth) + (screenwidth * (int)((float)v * hratio * h / (h + AMP2 * 2)));
 
-	for (u=0 ; u<scr_vrect.width+AMP2*2 ; u++)
-	{
-		column[u] = r_refdef.vrect.x +
-				(int)((float)u * wratio * w / (w + AMP2 * 2));
-	}
+	for(u = 0; u < scr_vrect.width+AMP2*2; u++)
+		column[u] = r_refdef.vrect.x + (int)((float)u * wratio * w / (w + AMP2 * 2));
 
 	turb = intsintable + ((int)(cl.time*SPEED)&(CYCLE-1));
 	dest = vid.buffer + scr_vrect.y * vid.rowbytes + scr_vrect.x;
 
-	for (v=0 ; v<scr_vrect.height ; v++, dest += vid.rowbytes)
-	{
+	for(v = 0; v < scr_vrect.height; v++, dest += vid.rowbytes){
 		col = &column[turb[v]];
 		row = &rowptr[v];
 
-		for (u=0 ; u<scr_vrect.width ; u+=4)
-		{
+		for(u = 0; u < scr_vrect.width; u += 4){
 			dest[u+0] = row[turb[u+0]][col[u+0]];
 			dest[u+1] = row[turb[u+1]][col[u+1]];
 			dest[u+2] = row[turb[u+2]][col[u+2]];
@@ -69,33 +61,28 @@
 */
 void D_DrawTurbulent8Span (int izi, byte alpha)
 {
-	int		sturb, tturb;
+	int sturb, tturb;
 
-	do
-	{
+	do{
 		sturb = ((r_turb_s + r_turb_turb[(r_turb_t>>16)&(CYCLE-1)])>>16)&63;
 		tturb = ((r_turb_t + r_turb_turb[(r_turb_s>>16)&(CYCLE-1)])>>16)&63;
-		if (*r_turb_z <= (izi >> 16) || (r_drawflags & DRAW_BLEND) == 0)
+		if(*r_turb_z <= (izi >> 16) || (r_drawflags & DRAW_BLEND) == 0)
 			*r_turb_pdest = blendalpha(*(r_turb_pbase + (tturb<<6) + sturb), *r_turb_pdest, alpha);
 		r_turb_s += r_turb_sstep;
 		r_turb_t += r_turb_tstep;
 		r_turb_pdest++;
 		r_turb_z++;
-	} while (--r_turb_spancount > 0);
+	}while(--r_turb_spancount > 0);
 }
 
 
-/*
-=============
-Turbulent8
-=============
-*/
-void Turbulent8 (espan_t *pspan, byte alpha)
+void
+Turbulent8(espan_t *pspan, byte alpha)
 {
-	int				count;
-	fixed16_t		snext, tnext;
-	float			sdivz, tdivz, zi, z, du, dv, spancountminus1;
-	float			sdivz16stepu, tdivz16stepu, zi16stepu;
+	int			count;
+	fixed16_t	snext, tnext;
+	double		sdivz, tdivz, zi, z, du, dv, spancountminus1;
+	double		sdivz16stepu, tdivz16stepu, zi16stepu;
 
 	r_turb_turb = sintable + ((int)(cl.time*SPEED)&(CYCLE-1));
 
@@ -102,23 +89,21 @@
 	r_turb_sstep = 0;	// keep compiler happy
 	r_turb_tstep = 0;	// ditto
 
-	r_turb_pbase = (unsigned char *)cacheblock;
+	r_turb_pbase = (byte*)cacheblock;
 
 	sdivz16stepu = d_sdivzstepu * 16;
 	tdivz16stepu = d_tdivzstepu * 16;
 	zi16stepu = d_zistepu * 16;
 
-	do
-	{
-		r_turb_pdest = (unsigned char *)((byte *)d_viewbuffer +
-				(screenwidth * pspan->v) + pspan->u);
-		r_turb_z = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
+	do{
+		r_turb_pdest = (byte *)d_viewbuffer + screenwidth*pspan->v + pspan->u;
+		r_turb_z = d_pzbuffer + d_zwidth*pspan->v + pspan->u;
 
 		count = pspan->count;
 
 		// calculate the initial s/z, t/z, 1/z, s, and t and clamp
-		du = (float)pspan->u;
-		dv = (float)pspan->v;
+		du = pspan->u;
+		dv = pspan->v;
 
 		sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
 		tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
@@ -126,29 +111,17 @@
 		z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 
 		r_turb_s = (int)(sdivz * z) + sadjust;
-		if (r_turb_s > bbextents)
-			r_turb_s = bbextents;
-		else if (r_turb_s < 0)
-			r_turb_s = 0;
+		r_turb_s = clamp(r_turb_s, 0, bbextents);
 
 		r_turb_t = (int)(tdivz * z) + tadjust;
-		if (r_turb_t > bbextentt)
-			r_turb_t = bbextentt;
-		else if (r_turb_t < 0)
-			r_turb_t = 0;
+		r_turb_t = clamp(r_turb_t, 0, bbextentt);
 
-		do
-		{
+		do{
 			// calculate s and t at the far end of the span
-			if (count >= 16)
-				r_turb_spancount = 16;
-			else
-				r_turb_spancount = count;
-
+			r_turb_spancount = min(count, 16);
 			count -= r_turb_spancount;
 
-			if (count)
-			{
+			if(count){
 				// calculate s/z, t/z, zi->fixed s and t at far end of span,
 				// calculate s and t steps across span by shifting
 				sdivz += sdivz16stepu;
@@ -157,24 +130,15 @@
 				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 
 				snext = (int)(sdivz * z) + sadjust;
-				if (snext > bbextents)
-					snext = bbextents;
-				else if (snext < 16)
-					snext = 16;	// prevent round-off error on <0 steps from
-								//  from causing overstepping & running off the
-								//  edge of the texture
+				// prevent round-off error on <0 steps from causing overstepping & running off the edge of the texture
+				snext = clamp(snext, 16, bbextents);
 
 				tnext = (int)(tdivz * z) + tadjust;
-				if (tnext > bbextentt)
-					tnext = bbextentt;
-				else if (tnext < 16)
-					tnext = 16;	// guard against round-off error on <0 steps
+				tnext = clamp(tnext, 16, bbextentt); // guard against round-off error on <0 steps
 
 				r_turb_sstep = (snext - r_turb_s) >> 4;
 				r_turb_tstep = (tnext - r_turb_t) >> 4;
-			}
-			else
-			{
+			}else{
 				// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
 				// can't step off polygon), clamp, calculate s and t steps across
 				// span by division, biasing steps low so we don't run off the
@@ -185,21 +149,13 @@
 				zi += d_zistepu * spancountminus1;
 				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 				snext = (int)(sdivz * z) + sadjust;
-				if (snext > bbextents)
-					snext = bbextents;
-				else if (snext < 16)
-					snext = 16;	// prevent round-off error on <0 steps from
-								//  from causing overstepping & running off the
-								//  edge of the texture
+				// prevent round-off error on <0 steps from causing overstepping & running off the edge of the texture
+				snext = clamp(snext, 16, bbextents);
 
 				tnext = (int)(tdivz * z) + tadjust;
-				if (tnext > bbextentt)
-					tnext = bbextentt;
-				else if (tnext < 16)
-					tnext = 16;	// guard against round-off error on <0 steps
+				tnext = clamp(tnext, 16, bbextentt); // guard against round-off error on <0 steps
 
-				if (r_turb_spancount > 1)
-				{
+				if(r_turb_spancount > 1){
 					r_turb_sstep = (snext - r_turb_s) / (r_turb_spancount - 1);
 					r_turb_tstep = (tnext - r_turb_t) / (r_turb_spancount - 1);
 				}
@@ -208,209 +164,45 @@
 			r_turb_s = r_turb_s & ((CYCLE<<16)-1);
 			r_turb_t = r_turb_t & ((CYCLE<<16)-1);
 
-			D_DrawTurbulent8Span ((int)(zi * 0x8000 * 0x10000), alpha);
+			D_DrawTurbulent8Span((int)(zi * 0x8000 * 0x10000), alpha);
 
 			r_turb_s = snext;
 			r_turb_t = tnext;
 
-		} while (count > 0);
+		}while(count > 0);
 
-	} while ((pspan = pspan->pnext) != nil);
+	}while((pspan = pspan->pnext) != nil);
 }
 
-#define WRITEFENCE(i) do{ \
-	fencepix = *(pbase + (s >> 16) + (t >> 16) * cachewidth); \
-	if (fencepix != 255 && pz[i] <= (izi >> 16)){ \
-		pdest[i] = blendalpha(fencepix, pdest[i], alpha); \
-		if(alpha == 255) \
-			pz[i] = (izi >> 16); \
-	} \
-	izi += izistep; \
-	s += sstep; \
-	t += tstep; \
-}while(0)
-
-void D_DrawSpans16_Fence (espan_t *pspan, byte alpha)
+void
+D_DrawSpans16(espan_t *pspan, int forceblend, byte alpha) //qbism- up it from 8 to 16
 {
-	byte fencepix;
-	byte *pbase = (byte *)cacheblock, *pdest;
-	int count, spancount, izi, izistep;
-	fixed16_t s, t, snext, tnext, sstep, tstep;
-	float sdivz, tdivz, zi, z, du, dv, spancountminus1;
-	float sdivzstepu, tdivzstepu, zistepu;
-	uzint *pz;
+	int			count, spancount, izistep;
+	byte		*pbase, *pdest;
+	uzint		*pz;
+	fixed16_t	s, t, snext, tnext, sstep, tstep;
+	double		sdivz, tdivz, zi, z, du, dv, spancountminus1;
+	double		sdivzstepu, tdivzstepu, zistepu;
 
-	sdivzstepu = d_sdivzstepu * 16;
-	tdivzstepu = d_tdivzstepu * 16;
-	zistepu = d_zistepu * 16;
-
-	// we count on FP exceptions being turned off to avoid range problems
-	izistep = (int)(d_zistepu * 0x8000 * 0x10000);
-	sstep = tstep = 0;
-
-	do
-	{
-		pdest = (byte *)((byte *)d_viewbuffer + (screenwidth * pspan->v) + pspan->u);
-		pz = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
-
-		count = pspan->count >> 4;
-		spancount = pspan->count % 16;
-
-		// calculate the initial s/z, t/z, 1/z, s, and t and clamp
-		du = (float)pspan->u;
-		dv = (float)pspan->v;
-
-		sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
-		tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
-		zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
-		z = (float)0x10000 / zi;		// prescale to 16.16 fixed-point
-		// we count on FP exceptions being turned off to avoid range problems
-		izi = (int)(zi * 0x8000 * 0x10000);
-
-		s = (int)(sdivz * z) + sadjust;
-		if (s > bbextents)
-			s = bbextents;
-		else if (s < 0)
-			s = 0;
-
-		t = (int)(tdivz * z) + tadjust;
-		if (t > bbextentt)
-			t = bbextentt;
-		else if (t < 0)
-			t = 0;
-
-		while (count--){
-			// calculate s/z, t/z, zi->fixed s and t at far end of span,
-			// calculate s and t steps across span by shifting
-			sdivz += sdivzstepu;
-			tdivz += tdivzstepu;
-			zi += zistepu;
-			z = (float)0x10000 / zi;   // prescale to 16.16 fixed-point
-
-			snext = (int) (sdivz * z) + sadjust;
-			if (snext > bbextents)
-				snext = bbextents;
-			else if (snext <= 16)
-				snext = 16;   // prevent round-off error on <0 steps causing overstepping & running off the edge of the texture
-
-			tnext = (int) (tdivz * z) + tadjust;
-			if (tnext > bbextentt)
-				tnext = bbextentt;
-			else if (tnext < 16)
-				tnext = 16;   // guard against round-off error on <0 steps
-
-			sstep = (snext - s) >> 4;
-			tstep = (tnext - t) >> 4;
-
-			pdest += 16;
-			pz += 16;
-			WRITEFENCE(-16);
-			WRITEFENCE(-15);
-			WRITEFENCE(-14);
-			WRITEFENCE(-13);
-			WRITEFENCE(-12);
-			WRITEFENCE(-11);
-			WRITEFENCE(-10);
-			WRITEFENCE(-9);
-			WRITEFENCE(-8);
-			WRITEFENCE(-7);
-			WRITEFENCE(-6);
-			WRITEFENCE(-5);
-			WRITEFENCE(-4);
-			WRITEFENCE(-3);
-			WRITEFENCE(-2);
-			WRITEFENCE(-1);
-			USED(s); USED(t);
-
-			s = snext;
-			t = tnext;
-		}
-		if (spancount > 0)
-		{
-			// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so can't step off polygon),
-			// clamp, calculate s and t steps across span by division, biasing steps low so we don't run off the texture
-			spancountminus1 = (float)(spancount - 1);
-			sdivz += d_sdivzstepu * spancountminus1;
-			tdivz += d_tdivzstepu * spancountminus1;
-			zi += d_zistepu * spancountminus1;
-			z = (float)0x10000 / zi;   // prescale to 16.16 fixed-point
-			snext = (int)(sdivz * z) + sadjust;
-			if (snext > bbextents)
-				snext = bbextents;
-			else if (snext < 16)
-				snext = 16;   // prevent round-off error on <0 steps from causing overstepping & running off the edge of the texture
-
-			tnext = (int)(tdivz * z) + tadjust;
-			if (tnext > bbextentt)
-				tnext = bbextentt;
-			else if (tnext < 16)
-				tnext = 16;   // guard against round-off error on <0 steps
-
-			if (spancount > 1){
-				sstep = (snext - s) / (spancount - 1);
-				tstep = (tnext - t) / (spancount - 1);
-			}
-
-			pdest += spancount;
-			pz += spancount;
-			switch (spancount){
-			case 16: WRITEFENCE(-16);
-			case 15: WRITEFENCE(-15);
-			case 14: WRITEFENCE(-14);
-			case 13: WRITEFENCE(-13);
-			case 12: WRITEFENCE(-12);
-			case 11: WRITEFENCE(-11);
-			case 10: WRITEFENCE(-10);
-			case  9: WRITEFENCE(-9);
-			case  8: WRITEFENCE(-8);
-			case  7: WRITEFENCE(-7);
-			case  6: WRITEFENCE(-6);
-			case  5: WRITEFENCE(-5);
-			case  4: WRITEFENCE(-4);
-			case  3: WRITEFENCE(-3);
-			case  2: WRITEFENCE(-2);
-			case  1: WRITEFENCE(-1);
-			USED(izi); USED(s); USED(t);
-			}
-		}
-	}
-	while ((pspan = pspan->pnext) != nil);
-}
-
-/*
-=============
-D_DrawSpans16
-=============
-*/
-void D_DrawSpans16 (espan_t *pspan, byte alpha) //qbism- up it from 8 to 16
-{
-	int				count, spancount;
-	unsigned char	*pbase, *pdest;
-	uzint			*pz;
-	fixed16_t		s, t, snext, tnext, sstep, tstep;
-	float			sdivz, tdivz, zi, z, du, dv, spancountminus1;
-	float			sdivzstepu, tdivzstepu, zistepu;
-
 	sstep = 0;	// keep compiler happy
 	tstep = 0;	// ditto
 
-	pbase = (unsigned char *)cacheblock;
+	pbase = (byte*)cacheblock;
 
 	sdivzstepu = d_sdivzstepu * 16;
 	tdivzstepu = d_tdivzstepu * 16;
 	zistepu = d_zistepu * 16;
+	izistep = (int)(d_zistepu * 0x8000 * 0x10000);
 
-	do
-	{
-		pdest = (unsigned char *)((byte *)d_viewbuffer +
-				(screenwidth * pspan->v) + pspan->u);
-		pz = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
+	do{
+		pdest = (byte *)d_viewbuffer + screenwidth*pspan->v + pspan->u;
+		pz = d_pzbuffer + d_zwidth*pspan->v + pspan->u;
 
 		count = pspan->count;
 
 		// calculate the initial s/z, t/z, 1/z, s, and t and clamp
-		du = (float)pspan->u;
-		dv = (float)pspan->v;
+		du = pspan->u;
+		dv = pspan->v;
 
 		sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
 		tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
@@ -418,29 +210,17 @@
 		z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 
 		s = (int)(sdivz * z) + sadjust;
-		if (s > bbextents)
-			s = bbextents;
-		else if (s < 0)
-			s = 0;
+		s = clamp(s, 0, bbextents);
 
 		t = (int)(tdivz * z) + tadjust;
-		if (t > bbextentt)
-			t = bbextentt;
-		else if (t < 0)
-			t = 0;
+		t = clamp(t, 0, bbextentt);
 
-		do
-		{
+		do{
 			// calculate s and t at the far end of the span
-			if (count >= 16)
-				spancount = 16;
-			else
-				spancount = count;
-
+			spancount = min(count, 16);
 			count -= spancount;
 
-			if (count)
-			{
+			if(count){
 				// calculate s/z, t/z, zi->fixed s and t at far end of span,
 				// calculate s and t steps across span by shifting
 				sdivz += sdivzstepu;
@@ -449,24 +229,18 @@
 				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 
 				snext = (int)(sdivz * z) + sadjust;
-				if (snext > bbextents)
-					snext = bbextents;
-				else if (snext <= 16)
-					snext = 16;	// prevent round-off error on <0 steps from
-								//  from causing overstepping & running off the
-								//  edge of the texture
+				// prevent round-off error on <0 steps from
+				//  from causing overstepping & running off the
+				//  edge of the texture
+				snext = clamp(snext, 16, bbextents);
 
 				tnext = (int)(tdivz * z) + tadjust;
-				if (tnext > bbextentt)
-					tnext = bbextentt;
-				else if (tnext < 16)
-					tnext = 16;	// guard against round-off error on <0 steps
+				// guard against round-off error on <0 steps
+				tnext = clamp(tnext, 16, bbextentt);
 
 				sstep = (snext - s) >> 4;
 				tstep = (tnext - t) >> 4;
-			}
-			else
-			{
+			}else{
 				// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
 				// can't step off polygon), clamp, calculate s and t steps across
 				// span by division, biasing steps low so we don't run off the
@@ -477,21 +251,16 @@
 				zi += d_zistepu * spancountminus1;
 				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 				snext = (int)(sdivz * z) + sadjust;
-				if (snext > bbextents)
-					snext = bbextents;
-				else if (snext < 16)
-					snext = 16;	// prevent round-off error on <0 steps from
-								//  from causing overstepping & running off the
-								//  edge of the texture
+				// prevent round-off error on <0 steps from
+				//  from causing overstepping & running off the
+				//  edge of the texture
+				snext = clamp(snext, 16, bbextents);
 
 				tnext = (int)(tdivz * z) + tadjust;
-				if (tnext > bbextentt)
-					tnext = bbextentt;
-				else if (tnext < 16)
-					tnext = 16;	// guard against round-off error on <0 steps
+				// guard against round-off error on <0 steps
+				tnext = clamp(tnext, 16, bbextentt);
 
-				if (spancount > 1)
-				{
+				if(spancount > 1){
 					sstep = (snext - s) / (spancount - 1);
 					tstep = (tnext - t) / (spancount - 1);
 				}
@@ -498,36 +267,32 @@
 			}
 
 			if(spancount > 0){
-				void dospan(uchar *, uchar *, int, int, int, int, int, int);
-				void dospan_alpha(uchar *, uchar *, int, int, int, int, int, int, byte, uzint *, int);
-				if(r_drawflags & DRAW_BLEND)
-					dospan_alpha(pdest, pbase, s, t, sstep, tstep, spancount, cachewidth, alpha, pz, (int)(zi * 0x8000 * 0x10000));
+				void dospan(byte *, byte *, int, int, int, int, int, int);
+				void dospan_alpha(byte *, byte *, int, int, int, int, int, int, byte, uzint *, int,int);
+				if((r_drawflags & DRAW_BLEND) != 0 || forceblend)
+					dospan_alpha(pdest, pbase, s, t, sstep, tstep, spancount, cachewidth, alpha, pz, (int)(zi * 0x8000 * 0x10000), izistep);
 				else
 					dospan(pdest, pbase, s, t, sstep, tstep, spancount, cachewidth);
+				pdest += spancount;
+				pz += spancount;
 			}
-			pdest += spancount;
-			pz += spancount;
 			s = snext;
 			t = tnext;
 
-		} while (count > 0);
+		}while(count > 0);
 
-	} while ((pspan = pspan->pnext) != nil);
+	}while((pspan = pspan->pnext) != nil);
 }
 
-/*
-=============
-D_DrawZSpans
-=============
-*/
-void D_DrawZSpans (espan_t *pspan)
+void
+D_DrawZSpans(espan_t *pspan)
 {
-	int				count, doublecount, izistep;
-	int				izi;
-	uzint			*pdest;
-	unsigned		ltemp;
-	double			zi;
-	float			du, dv;
+	int			count, doublecount, izistep;
+	int			izi;
+	uzint		*pdest;
+	unsigned	ltemp;
+	double		zi;
+	float		du, dv;
 
 	if((r_drawflags & DRAW_BLEND) != 0)
 		return;
@@ -536,10 +301,8 @@
 	// we count on FP exceptions being turned off to avoid range problems
 	izistep = (int)(d_zistepu * 0x8000 * 0x10000);
 
-	do
-	{
+	do{
 		pdest = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
-
 		count = pspan->count;
 
 		// calculate the initial 1/z
@@ -550,17 +313,14 @@
 		// we count on FP exceptions being turned off to avoid range problems
 		izi = (int)(zi * 0x8000 * 0x10000);
 
-		if ((uintptr)pdest & 0x02)
-		{
+		if((uintptr)pdest & 0x02){
 			*pdest++ = (short)(izi >> 16);
 			izi += izistep;
 			count--;
 		}
 
-		if ((doublecount = count >> 1) > 0)
-		{
-			do
-			{
+		if((doublecount = count >> 1) > 0){
+			do{
 				ltemp = izi >> 16;
 				izi += izistep;
 				ltemp |= izi & 0xFFFF0000;
@@ -567,11 +327,11 @@
 				izi += izistep;
 				*(int *)pdest = ltemp;
 				pdest += 2;
-			} while (--doublecount > 0);
+			}while (--doublecount > 0);
 		}
 
-		if (count & 1)
+		if(count & 1)
 			*pdest = (short)(izi >> 16);
 
-	} while ((pspan = pspan->pnext) != nil);
+	}while((pspan = pspan->pnext) != nil);
 }
--- a/r_draw.c
+++ b/r_draw.c
@@ -493,8 +493,6 @@
 	if (!r_emitted)
 		return 1;
 
-	r_polycount++;
-
 	surface_p->data = (void *)fa;
 	surface_p->nearzi = r_nearzi;
 	surface_p->flags = fa->flags;
@@ -608,8 +606,6 @@
 	// if no edges made it out, return without posting the surface
 	if (!r_emitted)
 		return;
-
-	r_polycount++;
 
 	surface_p->data = (void *)psurf;
 	surface_p->nearzi = r_nearzi;
--- a/r_local.h
+++ b/r_local.h
@@ -143,7 +143,6 @@
 
 extern int	c_faceclip;
 extern int	r_polycount;
-extern int	r_wholepolycount;
 
 extern	model_t		*cl_worldmodel;
 
@@ -152,9 +151,6 @@
 // !!! if this is changed, it must be changed in asm_draw.h too !!!
 #define	NEAR_CLIP	0.01
 
-extern int			ubasestep, errorterm, erroradjustup, erroradjustdown;
-extern int			vstartscan;
-
 extern fixed16_t	sadjust, tadjust;
 extern fixed16_t	bbextents, bbextentt;
 
@@ -161,8 +157,6 @@
 #define MAXBVERTINDEXES	1000	// new clipped vertices when clipping bmodels
 								//  to the world BSP
 extern mvertex_t	*r_ptverts, *r_ptvertsmax;
-
-extern int		reinit_surfcache;
 
 extern int		r_currentkey;
 extern int		r_currentbkey;
--- a/r_main.c
+++ b/r_main.c
@@ -1,7 +1,5 @@
 #include "quakedef.h"
 
-//define	PASSAGES
-
 void		*colormap;
 static vec3_t viewlightvec;
 static alight_t r_viewlighting = {128, 192, viewlightvec};
@@ -57,17 +55,10 @@
 //
 int		r_framecount = 1;	// so frame counts initialized to 0 don't match
 int		r_visframecount;
-int		d_spanpixcount;
-int		r_polycount;
-int		r_drawnpolycount;
-int		r_wholepolycount;
 
 int			*pfrustum_indexes[4];
 int			r_frustum_indexes[4*6];
 
-int		reinit_surfcache = 1;	// if 1, surface cache is currently empty and
-								// must be reinitialized for current cache size
-
 mleaf_t		*r_viewleaf, *r_oldviewleaf;
 
 texture_t	*r_notexture_mip;
@@ -215,9 +206,6 @@
 	r_maxsurfsseen = 0;
 	r_dowarpold = false;
 	r_viewchanged = false;
-#ifdef PASSAGES
-CreatePassages ();
-#endif
 }
 
 
@@ -710,13 +698,7 @@
 	r_warpbuffer = warpbuffer;
 
 	R_SetupFrame ();
-
-#ifdef PASSAGES
-	SetVisibilityByPassages ();
-#endif
-#ifndef PASSAGES
 	R_MarkLeaves ();	// done here so we know if we're in water
-#endif
 
 	if (!cl_entities[0].model || !cl.worldmodel)
 		fatal ("R_RenderView: NULL worldmodel");
--- a/r_misc.c
+++ b/r_misc.c
@@ -232,10 +232,6 @@
 
 	// clear frame counts
 	c_faceclip = 0;
-	d_spanpixcount = 0;
-	r_polycount = 0;
-	r_drawnpolycount = 0;
-	r_wholepolycount = 0;
 
 	D_SetupFrame ();
 }
--- a/r_shared.h
+++ b/r_shared.h
@@ -99,8 +99,6 @@
 extern void SetUpForLineScan(fixed8_t startvertu, fixed8_t startvertv,
 	fixed8_t endvertu, fixed8_t endvertv);
 
-extern int	ubasestep, errorterm, erroradjustup, erroradjustdown;
-
 // flags in finalvert_t.flags
 #define ALIAS_LEFT_CLIP				0x0001
 #define ALIAS_TOP_CLIP				0x0002
--- a/render.h
+++ b/render.h
@@ -88,9 +88,7 @@
 //
 // refresh
 //
-extern	int		reinit_surfcache;
 
-
 extern	refdef_t	r_refdef;
 extern vec3_t	r_origin, vpn, vright, vup;
 
@@ -127,7 +125,6 @@
 //
 // surface cache related
 //
-extern	int		reinit_surfcache;	// if 1, surface cache is currently empty and
 extern bool	r_cache_thrash;	// set if thrashing the surface cache
 
 int	D_SurfaceCacheForRes (int width, int height);
@@ -135,4 +132,3 @@
 void D_DeleteSurfaceCache (void);
 void D_InitCaches (void *buffer, int size);
 void R_SetVrect (vrect_t *pvrect, vrect_t *pvrectin, int lineadj);
-
--- a/span.c
+++ b/span.c
@@ -1,19 +1,11 @@
 #include "quakedef.h"
 
-#define P \
-	do{ \
-		*pdest++ = pbase[(s >> 16) + (t >> 16) * cachewidth]; \
-		s += sstep; \
-		t += tstep; \
-	}while(0);
-
 void
-dospan(uchar *pdest, uchar *pbase, int s, int t, int sstep, int tstep, int spancount, int cachewidth)
+dospan(byte *pdest, byte *pbase, int s, int t, int sstep, int tstep, int spancount, int cachewidth)
 {
-	switch(spancount)
-	{
-	case 16: P; case 15: P; case 14: P; case 13: P; case 12: P; case 11: P; case 10: P; case 9: P;
-	case  8: P; case  7: P; case  6: P; case  5: P; case  4: P; case  3: P; case  2: P; case 1: P;
-	}
-	USED(pdest); USED(s); USED(t);
+	do{
+		*pdest++ = pbase[(s >> 16) + (t >> 16) * cachewidth];
+		s += sstep;
+		t += tstep;
+	}while(--spancount);
 }
--- a/span_alpha.c
+++ b/span_alpha.c
@@ -1,22 +1,33 @@
 #include "quakedef.h"
 
-#define P \
-	do{ \
-		if(*z <= (izi >> 16)) \
-			*pdest = blendalpha(pbase[(s >> 16) + (t >> 16) * cachewidth], *pdest, alpha); \
-		pdest++; \
-		z++; \
-		s += sstep; \
-		t += tstep; \
-	}while(0)
-
 void
-dospan_alpha(uchar *pdest, uchar *pbase, int s, int t, int sstep, int tstep, int spancount, int cachewidth, u8int alpha, uzint *z, int izi)
+dospan_alpha(byte *pdest, byte *pbase, int s, int t, int sstep, int tstep, int spancount, int cachewidth, u8int alpha, uzint *pz, int izi, int izistep)
 {
-	switch(spancount)
-	{
-	case 16: P; case 15: P; case 14: P; case 13: P; case 12: P; case 11: P; case 10: P; case 9: P;
-	case  8: P; case  7: P; case  6: P; case  5: P; case  4: P; case  3: P; case  2: P; case 1: P;
+	uchar pix;
+
+	if(alpha != 255){
+		do{
+			pix = pbase[(s >> 16) + (t >> 16) * cachewidth];
+			if(pix != 255 && *pz <= (izi >> 16))
+				*pdest = blendalpha(pix, *pdest, alpha);
+			pdest++;
+			pz++;
+			izi += izistep;
+			s += sstep;
+			t += tstep;
+		}while(--spancount);
+	}else{
+		do{
+			pix = pbase[(s >> 16) + (t >> 16) * cachewidth];
+			if(pix != 255 && *pz <= (izi >> 16)){
+				*pdest = pix;
+				*pz = izi >> 16;
+			}
+			pdest++;
+			pz++;
+			izi += izistep;
+			s += sstep;
+			t += tstep;
+		}while(--spancount);
 	}
-	USED(pdest); USED(s); USED(t); USED(z);
 }