shithub: qk1

Download patch

ref: 218ec193660b021d3ebe16e49d696bf3828fbdf1
parent: a0e7f7500b6b252fa15b28d0d0810a881e4766e2
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sat Dec 30 19:55:09 EST 2023

organize a global dvars struct

--- a/d_edge.c
+++ b/d_edge.c
@@ -34,7 +34,7 @@
 	int u, u2;
 
 	for(span = surf->spans; span; span=span->pnext){
-		pdest = d_viewbuffer + screenwidth*span->v;
+		pdest = dvars.viewbuffer + screenwidth*span->v;
 		u2 = span->u + span->count - 1;
 		for(u = span->u; u <= u2; u++)
 			pdest[u] = color;
@@ -55,31 +55,30 @@
 	TransformVector (pface->texinfo->vecs[1], p_taxis);
 
 	t = xscaleinv * mipscale;
-	d_sdivzstepu = p_saxis[0] * t;
-	d_tdivzstepu = p_taxis[0] * t;
 
+	dvars.sdivzstepu = p_saxis[0] * t;
+	dvars.tdivzstepu = p_taxis[0] * t;
+
 	t = yscaleinv * mipscale;
-	d_sdivzstepv = -p_saxis[1] * t;
-	d_tdivzstepv = -p_taxis[1] * t;
+	dvars.sdivzstepv = -p_saxis[1] * t;
+	dvars.tdivzstepv = -p_taxis[1] * t;
 
-	d_sdivzorigin = p_saxis[2] * mipscale - xcenter * d_sdivzstepu -
-			ycenter * d_sdivzstepv;
-	d_tdivzorigin = p_taxis[2] * mipscale - xcenter * d_tdivzstepu -
-			ycenter * d_tdivzstepv;
+	dvars.sdivzorigin = p_saxis[2] * mipscale - xcenter * dvars.sdivzstepu - ycenter * dvars.sdivzstepv;
+	dvars.tdivzorigin = p_taxis[2] * mipscale - xcenter * dvars.tdivzstepu - ycenter * dvars.tdivzstepv;
 
 	VectorScale (transformed_modelorg, mipscale, p_temp1);
 
 	t = 0x10000*mipscale;
-	sadjust = ((fixed16_t)(DotProduct (p_temp1, p_saxis) * 0x10000 + 0.5)) -
+	dvars.sadjust = ((fixed16_t)(DotProduct (p_temp1, p_saxis) * 0x10000 + 0.5)) -
 			((pface->texturemins[0] << 16) >> miplevel)
 			+ pface->texinfo->vecs[0][3]*t;
-	tadjust = ((fixed16_t)(DotProduct (p_temp1, p_taxis) * 0x10000 + 0.5)) -
+	dvars.tadjust = ((fixed16_t)(DotProduct (p_temp1, p_taxis) * 0x10000 + 0.5)) -
 			((pface->texturemins[1] << 16) >> miplevel)
 			+ pface->texinfo->vecs[1][3]*t;
 
 	// -1 (-epsilon) so we never wander off the edge of the texture
-	bbextents = ((pface->extents[0] << 16) >> miplevel) - 1;
-	bbextentt = ((pface->extents[1] << 16) >> miplevel) - 1;
+	dvars.bbextents = ((pface->extents[0] << 16) >> miplevel) - 1;
+	dvars.bbextentt = ((pface->extents[1] << 16) >> miplevel) - 1;
 }
 
 void
@@ -90,6 +89,7 @@
 	surfcache_t *pcurrentcache;
 	vec3_t world_transformed_modelorg, local_modelorg, transformed_modelorg;
 	byte alpha;
+	bool blend;
 
 	currententity = &cl_entities[0];
 	TransformVector(modelorg, transformed_modelorg);
@@ -110,10 +110,12 @@
 		if(alpha < 1)
 			alpha = 255;
 
-		d_zistepu = s->d_zistepu;
-		d_zistepv = s->d_zistepv;
-		d_ziorigin = s->d_ziorigin;
+		blend = (s->flags & SURF_TRANS) || (r_drawflags & DRAW_BLEND);
 
+		dvars.zistepu = s->d_zistepu;
+		dvars.zistepv = s->d_zistepv;
+		dvars.ziorigin = s->d_ziorigin;
+
 		if(s->insubmodel){
 			// FIXME: we don't want to do all this for every polygon!
 			// TODO: store once at start of frame
@@ -128,14 +130,14 @@
 
 		if(s->flags & SURF_DRAWSKY){
 			D_DrawSkyScans8(s->spans);
-			d_ziorigin = -0.8;
+			dvars.ziorigin = -0.8;
 			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;
-			d_zistepv = 0;
-			d_ziorigin = -0.9;
+			dvars.zistepu = 0;
+			dvars.zistepv = 0;
+			dvars.ziorigin = -0.9;
 
 			D_DrawSolidSurface(s, q1pal[(int)r_clearcolor.value & 0xFF]);
 			D_DrawZSpans(s->spans);
@@ -142,12 +144,13 @@
 		}else if(s->flags & SURF_DRAWTURB){
 			pface = s->data;
 			miplevel = 0;
-			cacheblock = pface->texinfo->texture->pixels + pface->texinfo->texture->offsets[0];
-			cachewidth = 64;
+			dvars.cacheblock = pface->texinfo->texture->pixels + pface->texinfo->texture->offsets[0];
+			dvars.cachewidth = 64;
 
 			D_CalcGradients (pface, transformed_modelorg);
 			Turbulent8 (s->spans, alpha);
-			D_DrawZSpans (s->spans);
+			if(!blend)
+				D_DrawZSpans (s->spans);
 		}else{
 			pface = s->data;
 			miplevel = D_MipLevelForScale(s->nearzi * scale_for_mip * pface->texinfo->mipadjust);
@@ -157,13 +160,14 @@
 			// FIXME: make this passed in to D_CacheSurface
 			pcurrentcache = D_CacheSurface(pface, miplevel);
 
-			cacheblock = pcurrentcache->pixels;
-			cachewidth = pcurrentcache->width;
+			dvars.cacheblock = pcurrentcache->pixels;
+			dvars.cachewidth = pcurrentcache->width;
 
 			D_CalcGradients(pface, transformed_modelorg);
 
-			D_DrawSpans16(s->spans, s->flags & SURF_FENCE, alpha);
-			D_DrawZSpans(s->spans);
+			D_DrawSpans16(s->spans, blend, alpha);
+			if(!blend)
+				D_DrawZSpans(s->spans);
 		}
 
 		if(s->insubmodel){
--- a/d_init.c
+++ b/d_init.c
@@ -25,7 +25,7 @@
 {
 	int i;
 
-	d_viewbuffer = r_dowarp ? r_warpbuffer : vid.buffer;
+	dvars.viewbuffer = r_dowarp ? r_warpbuffer : vid.buffer;
 	screenwidth = vid.rowbytes;
 
 	d_roverwrapped = false;
--- a/d_local.h
+++ b/d_local.h
@@ -9,6 +9,19 @@
 	SURFCACHE_SIZE_AT_320X200 = 600*1024,
 };
 
+typedef struct {
+	pixel_t *cacheblock;
+	pixel_t *viewbuffer;
+	uzint *zbuffer;
+	unsigned cachewidth;
+	unsigned zwidth;
+	fixed16_t sadjust, tadjust;
+	fixed16_t bbextents, bbextentt;
+	float sdivzstepu, tdivzstepu, zistepu;
+	float sdivzstepv, tdivzstepv, zistepv;
+	float sdivzorigin, tdivzorigin, ziorigin;
+}dvars_t;
+
 typedef struct surfcache_s
 {
 	struct surfcache_s	*next;
@@ -37,14 +50,9 @@
 extern surfcache_t	*sc_rover;
 extern surfcache_t	*d_initial_rover;
 
-extern float	d_sdivzstepu, d_tdivzstepu, d_zistepu;
-extern float	d_sdivzstepv, d_tdivzstepv, d_zistepv;
-extern float	d_sdivzorigin, d_tdivzorigin, d_ziorigin;
+extern dvars_t dvars;
 
-extern fixed16_t	sadjust, tadjust;
-extern fixed16_t	bbextents, bbextentt;
-
-void D_DrawSpans16 (espan_t *pspans, int forceblend, byte alpha);
+void D_DrawSpans16 (espan_t *pspans, bool blend, byte alpha);
 void D_DrawZSpans (espan_t *pspans);
 void Turbulent8 (espan_t *pspan, byte alpha);
 
@@ -53,9 +61,6 @@
 void R_ShowSubDiv (void);
 surfcache_t	*D_CacheSurface (msurface_t *surface, int miplevel);
 
-extern uzint *d_pzbuffer;
-extern unsigned int d_zrowbytes, d_zwidth;
-
 extern int	*d_pscantable;
 extern int	d_scantable[MAXHEIGHT];
 
@@ -62,7 +67,6 @@
 extern int	d_vrectx, d_vrecty, d_vrectright_particle, d_vrectbottom_particle;
 
 extern int d_pix_min, d_pix_max;
-extern pixel_t *d_viewbuffer;
 extern uzint *zspantable[MAXHEIGHT];
 extern int d_minmip;
 extern float d_scalemip[3];
--- a/d_modech.c
+++ b/d_modech.c
@@ -11,7 +11,7 @@
 {
 	int rowbytes, i;
 
-	d_zwidth = vid.width;
+	dvars.zwidth = vid.width;
 	rowbytes = vid.rowbytes;
 	scale_for_mip = max(xscale, yscale);
 
@@ -27,7 +27,7 @@
 
 	for(i = 0; i < vid.height; i++){
 		d_scantable[i] = i*rowbytes;
-		zspantable[i] = d_pzbuffer + i*d_zwidth;
+		zspantable[i] = dvars.zbuffer + i*dvars.zwidth;
 	}
 }
 
--- a/d_part.c
+++ b/d_part.c
@@ -35,8 +35,8 @@
 		return;
 	}
 
-	pz = d_pzbuffer + d_zwidth*v + u;
-	pdest = d_viewbuffer + d_scantable[v] + u;
+	pz = dvars.zbuffer + dvars.zwidth*v + u;
+	pdest = dvars.viewbuffer + d_scantable[v] + u;
 	izi = zi * 0x8000 * 0x10000;
 
 	zi = 1024.0 * d_pix_scale / Length(local);
@@ -45,7 +45,7 @@
 		pix = 1;
 	color = pparticle->color;
 
-	for(count = pix; count; count--, pz += d_zwidth, pdest += screenwidth){
+	for(count = pix; count; count--, pz += dvars.zwidth, pdest += screenwidth){
 		for(i = 0; i < pix; i++){
 			if(pz[i] <= izi){
 				pz[i] = izi;
--- a/d_polyse.c
+++ b/d_polyse.c
@@ -123,9 +123,9 @@
 				pixel_t p = addlight(skintable[fv->t >> 16][fv->s >> 16], fv->l[0], fv->l[1], fv->l[2]);
 				int n = d_scantable[fv->v] + fv->u;
 				if(r_drawflags & DRAW_BLEND){
-					d_viewbuffer[n] = blendalpha(p, d_viewbuffer[n], alpha, z);
+					dvars.viewbuffer[n] = blendalpha(p, dvars.viewbuffer[n], alpha, z);
 				}else{
-					d_viewbuffer[n] = p;
+					dvars.viewbuffer[n] = p;
 					*zbuf = z;
 				}
 			}
@@ -311,9 +311,9 @@
 		pixel_t p = addlight(skintable[new.t >> 16][new.s >> 16], l[0], l[1], l[2]);
 		int n = d_scantable[new.v] + new.u;
 		if(r_drawflags & DRAW_BLEND){
-			d_viewbuffer[n] = blendalpha(p, d_viewbuffer[n], alpha, z);
+			dvars.viewbuffer[n] = blendalpha(p, dvars.viewbuffer[n], alpha, z);
 		}else{
-			d_viewbuffer[n] = p;
+			dvars.viewbuffer[n] = p;
 			*zbuf = z;
 		}
 	}
@@ -628,8 +628,8 @@
 	d_light[1] = plefttop->l[1];
 	d_light[2] = plefttop->l[2];
 
-	d_pdest = d_viewbuffer + ystart * screenwidth + plefttop->u;
-	d_pz = d_pzbuffer + ystart * d_zwidth + plefttop->u;
+	d_pdest = dvars.viewbuffer + ystart * screenwidth + plefttop->u;
+	d_pz = dvars.zbuffer + ystart * dvars.zwidth + plefttop->u;
 
 	if (initialleftheight == 1)
 	{
@@ -654,7 +654,7 @@
 		D_PolysetSetUpForLineScan(plefttop->u, plefttop->v,
 							  pleftbottom->u, pleftbottom->v);
 
-		d_pzbasestep = d_zwidth + ubasestep;
+		d_pzbasestep = dvars.zwidth + ubasestep;
 		d_pzextrastep = d_pzbasestep + 1;
 		d_pdestbasestep = screenwidth + ubasestep;
 		d_pdestextrastep = d_pdestbasestep + 1;
@@ -720,8 +720,8 @@
 		d_light[1] = plefttop->l[1];
 		d_light[2] = plefttop->l[2];
 
-		d_pdest = d_viewbuffer + ystart * screenwidth + plefttop->u;
-		d_pz = d_pzbuffer + ystart * d_zwidth + plefttop->u;
+		d_pdest = dvars.viewbuffer + ystart * screenwidth + plefttop->u;
+		d_pz = dvars.zbuffer + ystart * dvars.zwidth + plefttop->u;
 
 		if (height == 1)
 		{
@@ -746,7 +746,7 @@
 
 			d_pdestbasestep = screenwidth + ubasestep;
 			d_pdestextrastep = d_pdestbasestep + 1;
-			d_pzbasestep = d_zwidth + ubasestep;
+			d_pzbasestep = dvars.zwidth + ubasestep;
 			d_pzextrastep = d_pzbasestep + 1;
 
 			if (ubasestep < 0){
--- a/d_scan.c
+++ b/d_scan.c
@@ -30,7 +30,7 @@
 	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)));
+		rowptr[v] = dvars.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));
@@ -76,9 +76,9 @@
 void
 Turbulent8(espan_t *pspan, byte alpha)
 {
-	int			count;
+	int			count, spancountminus1;
 	fixed16_t	snext, tnext;
-	float		sdivz, tdivz, zi, z, du, dv, spancountminus1;
+	float		sdivz, tdivz, zi, z, du, dv;
 	float		sdivz16stepu, tdivz16stepu, zi16stepu;
 
 	r_turb_turb = sintable + ((int)(cl.time*SPEED)&(CYCLE-1));
@@ -86,15 +86,15 @@
 	r_turb_sstep = 0;	// keep compiler happy
 	r_turb_tstep = 0;	// ditto
 
-	r_turb_pbase = cacheblock;
+	r_turb_pbase = dvars.cacheblock;
 
-	sdivz16stepu = d_sdivzstepu * 16;
-	tdivz16stepu = d_tdivzstepu * 16;
-	zi16stepu = d_zistepu * 16;
+	sdivz16stepu = dvars.sdivzstepu * 16;
+	tdivz16stepu = dvars.tdivzstepu * 16;
+	zi16stepu = dvars.zistepu * 16;
 
 	do{
-		r_turb_pdest = d_viewbuffer + screenwidth*pspan->v + pspan->u;
-		r_turb_z = d_pzbuffer + d_zwidth*pspan->v + pspan->u;
+		r_turb_pdest = dvars.viewbuffer + screenwidth*pspan->v + pspan->u;
+		r_turb_z = dvars.zbuffer + dvars.zwidth*pspan->v + pspan->u;
 
 		count = pspan->count;
 
@@ -102,16 +102,16 @@
 		du = pspan->u;
 		dv = 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;
+		sdivz = dvars.sdivzorigin + dv*dvars.sdivzstepv + du*dvars.sdivzstepu;
+		tdivz = dvars.tdivzorigin + dv*dvars.tdivzstepv + du*dvars.tdivzstepu;
+		zi = dvars.ziorigin + dv*dvars.zistepv + du*dvars.zistepu;
 		z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 
-		r_turb_s = (int)(sdivz * z) + sadjust;
-		r_turb_s = clamp(r_turb_s, 0, bbextents);
+		r_turb_s = (int)(sdivz * z) + dvars.sadjust;
+		r_turb_s = clamp(r_turb_s, 0, dvars.bbextents);
 
-		r_turb_t = (int)(tdivz * z) + tadjust;
-		r_turb_t = clamp(r_turb_t, 0, bbextentt);
+		r_turb_t = (int)(tdivz * z) + dvars.tadjust;
+		r_turb_t = clamp(r_turb_t, 0, dvars.bbextentt);
 
 		do{
 			// calculate s and t at the far end of the span
@@ -126,12 +126,12 @@
 				zi += zi16stepu;
 				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 
-				snext = (int)(sdivz * z) + sadjust;
+				snext = (int)(sdivz * z) + dvars.sadjust;
 				// prevent round-off error on <0 steps from causing overstepping & running off the edge of the texture
-				snext = clamp(snext, 16, bbextents);
+				snext = clamp(snext, 16, dvars.bbextents);
 
-				tnext = (int)(tdivz * z) + tadjust;
-				tnext = clamp(tnext, 16, bbextentt); // guard against round-off error on <0 steps
+				tnext = (int)(tdivz * z) + dvars.tadjust;
+				tnext = clamp(tnext, 16, dvars.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;
@@ -141,20 +141,20 @@
 				// span by division, biasing steps low so we don't run off the
 				// texture
 				spancountminus1 = r_turb_spancount - 1;
-				sdivz += d_sdivzstepu * spancountminus1;
-				tdivz += d_tdivzstepu * spancountminus1;
-				zi += d_zistepu * spancountminus1;
+				sdivz += dvars.sdivzstepu * spancountminus1;
+				tdivz += dvars.tdivzstepu * spancountminus1;
+				zi += dvars.zistepu * spancountminus1;
 				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
-				snext = (int)(sdivz * z) + sadjust;
+				snext = (int)(sdivz * z) + dvars.sadjust;
 				// prevent round-off error on <0 steps from causing overstepping & running off the edge of the texture
-				snext = clamp(snext, 16, bbextents);
+				snext = clamp(snext, 16, dvars.bbextents);
 
-				tnext = (int)(tdivz * z) + tadjust;
-				tnext = clamp(tnext, 16, bbextentt); // guard against round-off error on <0 steps
+				tnext = (int)(tdivz * z) + dvars.tadjust;
+				tnext = clamp(tnext, 16, dvars.bbextentt); // guard against round-off error on <0 steps
 
 				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);
+					r_turb_sstep = (snext - r_turb_s) / spancountminus1;
+					r_turb_tstep = (tnext - r_turb_t) / spancountminus1;
 				}
 			}
 
@@ -172,28 +172,28 @@
 }
 
 void
-D_DrawSpans16(espan_t *pspan, int forceblend, byte alpha) //qbism- up it from 8 to 16
+D_DrawSpans16(espan_t *pspan, bool blend, byte alpha) //qbism- up it from 8 to 16
 {
-	int			count, spancount, izistep;
+	int			count, spancount, izistep, spancountminus1;
 	pixel_t		*pbase, *pdest;
 	uzint		*pz;
 	fixed16_t	s, t, snext, tnext, sstep, tstep;
-	float		sdivz, tdivz, zi, z, du, dv, spancountminus1;
+	float		sdivz, tdivz, zi, z, du, dv;
 	float		sdivzstepu, tdivzstepu, zistepu;
 
 	sstep = 0;	// keep compiler happy
 	tstep = 0;	// ditto
 
-	pbase = cacheblock;
+	pbase = dvars.cacheblock;
 
-	sdivzstepu = d_sdivzstepu * 16;
-	tdivzstepu = d_tdivzstepu * 16;
-	zistepu = d_zistepu * 16;
-	izistep = (int)(d_zistepu * 0x8000 * 0x10000);
+	sdivzstepu = dvars.sdivzstepu * 16;
+	tdivzstepu = dvars.tdivzstepu * 16;
+	zistepu = dvars.zistepu * 16;
+	izistep = (int)(dvars.zistepu * 0x8000 * 0x10000);
 
 	do{
-		pdest = d_viewbuffer + screenwidth*pspan->v + pspan->u;
-		pz = d_pzbuffer + d_zwidth*pspan->v + pspan->u;
+		pdest = dvars.viewbuffer + screenwidth*pspan->v + pspan->u;
+		pz = dvars.zbuffer + dvars.zwidth*pspan->v + pspan->u;
 
 		count = pspan->count;
 
@@ -201,21 +201,20 @@
 		du = pspan->u;
 		dv = 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;
+		sdivz = dvars.sdivzorigin + dv*dvars.sdivzstepv + du*dvars.sdivzstepu;
+		tdivz = dvars.tdivzorigin + dv*dvars.tdivzstepv + du*dvars.tdivzstepu;
+		zi = dvars.ziorigin + dv*dvars.zistepv + du*dvars.zistepu;
 		z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 
-		s = (int)(sdivz * z) + sadjust;
-		s = clamp(s, 0, bbextents);
+		s = (int)(sdivz * z) + dvars.sadjust;
+		s = clamp(s, 0, dvars.bbextents);
 
-		t = (int)(tdivz * z) + tadjust;
-		t = clamp(t, 0, bbextentt);
+		t = (int)(tdivz * z) + dvars.tadjust;
+		t = clamp(t, 0, dvars.bbextentt);
 
 		do{
 			// calculate s and t at the far end of the span
 			spancount = min(count, 16);
-			spancountminus1 = spancount - 1;
 			count -= spancount;
 
 			if(count){
@@ -225,15 +224,15 @@
 				tdivz += tdivzstepu;
 				z = (float)0x10000 / (zi + zistepu);	// prescale to 16.16 fixed-point
 
-				snext = (int)(sdivz * z) + sadjust;
+				snext = (int)(sdivz * z) + dvars.sadjust;
 				// prevent round-off error on <0 steps from
 				//  from causing overstepping & running off the
 				//  edge of the texture
-				snext = clamp(snext, 16, bbextents);
+				snext = clamp(snext, 16, dvars.bbextents);
 
-				tnext = (int)(tdivz * z) + tadjust;
+				tnext = (int)(tdivz * z) + dvars.tadjust;
 				// guard against round-off error on <0 steps
-				tnext = clamp(tnext, 16, bbextentt);
+				tnext = clamp(tnext, 16, dvars.bbextentt);
 
 				sstep = (snext - s) >> 4;
 				tstep = (tnext - t) >> 4;
@@ -242,37 +241,35 @@
 				// 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
-				sdivz += d_sdivzstepu * spancountminus1;
-				tdivz += d_tdivzstepu * spancountminus1;
-				z = (float)0x10000 / (zi + d_zistepu * spancountminus1);	// prescale to 16.16 fixed-point
-				snext = (int)(sdivz * z) + sadjust;
+				spancountminus1 = spancount - 1;
+				sdivz += dvars.sdivzstepu * spancountminus1;
+				tdivz += dvars.tdivzstepu * spancountminus1;
+				z = (float)0x10000 / (zi + dvars.zistepu * spancountminus1);	// prescale to 16.16 fixed-point
+				snext = (int)(sdivz * z) + dvars.sadjust;
 				// prevent round-off error on <0 steps from
 				//  from causing overstepping & running off the
 				//  edge of the texture
-				snext = clamp(snext, 16, bbextents);
+				snext = clamp(snext, 16, dvars.bbextents);
 
-				tnext = (int)(tdivz * z) + tadjust;
+				tnext = (int)(tdivz * z) + dvars.tadjust;
 				// guard against round-off error on <0 steps
-				tnext = clamp(tnext, 16, bbextentt);
+				tnext = clamp(tnext, 16, dvars.bbextentt);
 
 				if(spancount > 1){
-					sstep = (snext - s) / (spancount - 1);
-					tstep = (tnext - t) / (spancount - 1);
+					sstep = (snext - s) / spancountminus1;
+					tstep = (tnext - t) / spancountminus1;
 				}
 			}
 
-			if(spancount > 0){
-				void dospan(pixel_t *, pixel_t *, int, int, int, int, int, int);
-				void dospan_alpha(pixel_t *, pixel_t *, 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;
-			}
-
-			zi += count ? zistepu : d_zistepu*spancountminus1;
+			void dospan(pixel_t *, pixel_t *, int, int, int, int, int, int);
+			void dospan_alpha(pixel_t *, pixel_t *, int, int, int, int, int, int, byte, uzint *, int, int);
+			if(blend)
+				dospan_alpha(pdest, pbase, s, t, sstep, tstep, spancount, dvars.cachewidth, alpha, pz, (int)(zi * 0x8000 * 0x10000), izistep);
+			else
+				dospan(pdest, pbase, s, t, sstep, tstep, spancount, dvars.cachewidth);
+			pdest += spancount;
+			pz += spancount;
+			zi += zistepu;
 			s = snext;
 			t = tnext;
 		}while(count > 0);
@@ -287,15 +284,12 @@
 	float		zi;
 	float		zistepu;
 
-	if((r_drawflags & DRAW_BLEND) != 0)
-		return;
+	zistepu = dvars.zistepu * 16;
+	izistep = dvars.zistepu * 0x8000 * 0x10000;
 
-	zistepu = d_zistepu * 16;
-	izistep = d_zistepu * 0x8000 * 0x10000;
-
 	do{
-		pz = d_pzbuffer + d_zwidth*pspan->v + pspan->u;
-		zi = d_ziorigin + pspan->v*d_zistepv + pspan->u*d_zistepu;
+		pz = dvars.zbuffer + pspan->v*dvars.zwidth + pspan->u;
+		zi = dvars.ziorigin + pspan->v*dvars.zistepv + pspan->u*dvars.zistepu;
 		count = pspan->count;
 
 		do{
@@ -308,7 +302,7 @@
 				*pz++ = izi;
 				izi += izistep;
 			}
-			zi += count ? zistepu : d_zistepu*spancountminus1;
+			zi += count ? zistepu : dvars.zistepu*spancountminus1;
 		}while(count > 0);
 	}while((pspan = pspan->pnext) != nil);
 }
--- a/d_sky.c
+++ b/d_sky.c
@@ -52,7 +52,7 @@
 
 	do
 	{
-		pdest = d_viewbuffer + (screenwidth * pspan->v) + pspan->u;
+		pdest = dvars.viewbuffer + (screenwidth * pspan->v) + pspan->u;
 		count = pspan->count;
 
 		// calculate the initial s & t
--- a/d_sprite.c
+++ b/d_sprite.c
@@ -22,19 +22,19 @@
 	sstep = 0;	// keep compiler happy
 	tstep = 0;	// ditto
 
-	pbase = cacheblock;
+	pbase = dvars.cacheblock;
 
-	sdivz8stepu = d_sdivzstepu * 8;
-	tdivz8stepu = d_tdivzstepu * 8;
-	zi8stepu = d_zistepu * 8;
+	sdivz8stepu = dvars.sdivzstepu * 8;
+	tdivz8stepu = dvars.tdivzstepu * 8;
+	zi8stepu = dvars.zistepu * 8;
 
 	// we count on FP exceptions being turned off to avoid range problems
-	izistep = (int)(d_zistepu * 0x8000 * 0x10000);
+	izistep = (int)(dvars.zistepu * 0x8000 * 0x10000);
 
 	do
 	{
-		pdest = d_viewbuffer + (screenwidth * pspan->v) + pspan->u;
-		pz = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
+		pdest = dvars.viewbuffer + screenwidth * pspan->v + pspan->u;
+		pz = dvars.zbuffer + dvars.zwidth * pspan->v + pspan->u;
 
 		count = pspan->count;
 
@@ -45,37 +45,26 @@
 		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;
+		sdivz = dvars.sdivzorigin + dv*dvars.sdivzstepv + du*dvars.sdivzstepu;
+		tdivz = dvars.tdivzorigin + dv*dvars.tdivzstepv + du*dvars.tdivzstepu;
+		zi = dvars.ziorigin + dv*dvars.zistepv + du*dvars.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;
+		s = (int)(sdivz * z) + dvars.sadjust;
+		s = clamp(s, 0, dvars.bbextents);
 
-		t = (int)(tdivz * z) + tadjust;
-		if (t > bbextentt)
-			t = bbextentt;
-		else if (t < 0)
-			t = 0;
+		t = (int)(tdivz * z) + dvars.tadjust;
+		t = clamp(t, 0, dvars.bbextentt);
 
 		do
 		{
 			// calculate s and t at the far end of the span
-			if (count >= 8)
-				spancount = 8;
-			else
-				spancount = count;
-
+			spancount = min(count, 8);
 			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 += sdivz8stepu;
@@ -83,19 +72,14 @@
 				zi += zi8stepu;
 				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
 
-				snext = (int)(sdivz * z) + sadjust;
-				if (snext > bbextents)
-					snext = bbextents;
-				else if (snext < 8)
-					snext = 8;	// prevent round-off error on <0 steps from
-								//  from causing overstepping & running off the
-								//  edge of the texture
+				snext = (int)(sdivz * z) + dvars.sadjust;
+				snext = clamp(snext, 8, dvars.bbextents);
+				// prevent round-off error on <0 steps from
+				//  from causing overstepping & running off the
+				//  edge of the texture
 
-				tnext = (int)(tdivz * z) + tadjust;
-				if (tnext > bbextentt)
-					tnext = bbextentt;
-				else if (tnext < 8)
-					tnext = 8;	// guard against round-off error on <0 steps
+				tnext = (int)(tdivz * z) + dvars.tadjust;
+				tnext = clamp(tnext, 8, dvars.bbextentt); // guard against round-off error on <0 steps
 
 				sstep = (snext - s) >> 3;
 				tstep = (tnext - t) >> 3;
@@ -107,26 +91,20 @@
 				// 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;
+				sdivz += dvars.sdivzstepu * spancountminus1;
+				tdivz += dvars.tdivzstepu * spancountminus1;
+				zi += dvars.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 < 8)
-					snext = 8;	// prevent round-off error on <0 steps from
-								//  from causing overstepping & running off the
-								//  edge of the texture
+				snext = (int)(sdivz * z) + dvars.sadjust;
+				snext = clamp(snext, 8, dvars.bbextents);
+				// prevent round-off error on <0 steps from
+				//  from causing overstepping & running off the
+				//  edge of the texture
 
-				tnext = (int)(tdivz * z) + tadjust;
-				if (tnext > bbextentt)
-					tnext = bbextentt;
-				else if (tnext < 8)
-					tnext = 8;	// guard against round-off error on <0 steps
+				tnext = (int)(tdivz * z) + dvars.tadjust;
+				tnext = clamp(tnext, 8, dvars.bbextentt); // guard against round-off error on <0 steps
 
-				if (spancount > 1)
-				{
+				if(spancount > 1){
 					sstep = (snext - s) / (spancount - 1);
 					tstep = (tnext - t) / (spancount - 1);
 				}
@@ -134,7 +112,7 @@
 
 			do
 			{
-				btemp = *(pbase + (s >> 16) + (t >> 16) * cachewidth);
+				btemp = *(pbase + (s >> 16) + (t >> 16) * dvars.cachewidth);
 				if(opaque(btemp) && *pz <= izi){
 					if(r_drawflags & DRAW_BLEND){
 						*pdest = blendalpha(btemp, *pdest, alpha, izi);
@@ -201,8 +179,7 @@
 			slope = du / dv;
 			u_step = (int)(slope * 0x10000);
 			// adjust u to ceil the integer portion
-			u = (int)((pvert->u + (slope * (vtop - pvert->v))) * 0x10000) +
-					(0x10000 - 1);
+			u = (int)((pvert->u + (slope * (vtop - pvert->v))) * 0x10000) + (0x10000 - 1);
 			itop = (int)vtop;
 			ibottom = (int)vbottom;
 
@@ -281,8 +258,7 @@
 			slope = du / dv;
 			u_step = (int)(slope * 0x10000);
 			// adjust u to ceil the integer portion
-			u = (int)((uvert + (slope * (vtop - vvert))) * 0x10000) +
-					(0x10000 - 1);
+			u = (int)((uvert + (slope * (vtop - vvert))) * 0x10000) + (0x10000 - 1);
 			itop = (int)vtop;
 			ibottom = (int)vbottom;
 
@@ -324,32 +300,27 @@
 
 	distinv = 1.0 / (-DotProduct (modelorg, r_spritedesc.vpn));
 
-	d_sdivzstepu = p_saxis[0] * xscaleinv;
-	d_tdivzstepu = p_taxis[0] * xscaleinv;
+	dvars.sdivzstepu = p_saxis[0] * xscaleinv;
+	dvars.tdivzstepu = p_taxis[0] * xscaleinv;
 
-	d_sdivzstepv = -p_saxis[1] * yscaleinv;
-	d_tdivzstepv = -p_taxis[1] * yscaleinv;
+	dvars.sdivzstepv = -p_saxis[1] * yscaleinv;
+	dvars.tdivzstepv = -p_taxis[1] * yscaleinv;
 
-	d_zistepu = p_normal[0] * xscaleinv * distinv;
-	d_zistepv = -p_normal[1] * yscaleinv * distinv;
+	dvars.zistepu = p_normal[0] * xscaleinv * distinv;
+	dvars.zistepv = -p_normal[1] * yscaleinv * distinv;
 
-	d_sdivzorigin = p_saxis[2] - xcenter * d_sdivzstepu -
-			ycenter * d_sdivzstepv;
-	d_tdivzorigin = p_taxis[2] - xcenter * d_tdivzstepu -
-			ycenter * d_tdivzstepv;
-	d_ziorigin = p_normal[2] * distinv - xcenter * d_zistepu -
-			ycenter * d_zistepv;
+	dvars.sdivzorigin = p_saxis[2] - xcenter*dvars.sdivzstepu - ycenter*dvars.sdivzstepv;
+	dvars.tdivzorigin = p_taxis[2] - xcenter*dvars.tdivzstepu - ycenter*dvars.tdivzstepv;
+	dvars.ziorigin = p_normal[2]*distinv - xcenter*dvars.zistepu - ycenter*dvars.zistepv;
 
 	TransformVector (modelorg, p_temp1);
 
-	sadjust = ((fixed16_t)(DotProduct (p_temp1, p_saxis) * 0x10000 + 0.5)) -
-			(-(cachewidth >> 1) << 16);
-	tadjust = ((fixed16_t)(DotProduct (p_temp1, p_taxis) * 0x10000 + 0.5)) -
-			(-(sprite_height >> 1) << 16);
+	dvars.sadjust = ((fixed16_t)(DotProduct (p_temp1, p_saxis) * 0x10000 + 0.5)) - (-(dvars.cachewidth >> 1) << 16);
+	dvars.tadjust = ((fixed16_t)(DotProduct (p_temp1, p_taxis) * 0x10000 + 0.5)) - (-(sprite_height >> 1) << 16);
 
 	// -1 (-epsilon) so we never wander off the edge of the texture
-	bbextents = (cachewidth << 16) - 1;
-	bbextentt = (sprite_height << 16) - 1;
+	dvars.bbextents = (dvars.cachewidth << 16) - 1;
+	dvars.bbextentt = (sprite_height << 16) - 1;
 }
 
 
@@ -396,9 +367,9 @@
 	if (ymin >= ymax)
 		return;		// doesn't cross any scans at all
 
-	cachewidth = r_spritedesc.pspriteframe->width;
+	dvars.cachewidth = r_spritedesc.pspriteframe->width;
 	sprite_height = r_spritedesc.pspriteframe->height;
-	cacheblock = &r_spritedesc.pspriteframe->pixels[0];
+	dvars.cacheblock = &r_spritedesc.pspriteframe->pixels[0];
 
 	// copy the first vertex to the last vertex, so we don't have to deal with
 	// wrapping
@@ -411,4 +382,3 @@
 	D_SpriteScanRightEdge ();
 	D_SpriteDrawSpans (sprite_spans, currententity->alpha);
 }
-
--- a/d_vars.c
+++ b/d_vars.c
@@ -7,17 +7,4 @@
 // global refresh variables
 //-------------------------------------------------------
 
-// FIXME: make into one big structure, like cl or sv
-// FIXME: do separately for refresh engine and driver
-
-float	d_sdivzstepu, d_tdivzstepu, d_zistepu;
-float	d_sdivzstepv, d_tdivzstepv, d_zistepv;
-float	d_sdivzorigin, d_tdivzorigin, d_ziorigin;
-
-fixed16_t	sadjust, tadjust, bbextents, bbextentt;
-
-pixel_t			*cacheblock;
-int				cachewidth;
-pixel_t			*d_viewbuffer;
-uzint			*d_pzbuffer;
-unsigned int	d_zwidth;
+dvars_t dvars;
--- a/r_fog.c
+++ b/r_fog.c
@@ -120,7 +120,7 @@
 	for(y = r_refdef.vrect.y; y < r_refdef.vrectbottom; y++){
 		i = y * vid.width + r_refdef.vrect.x;
 		pix = vid.buffer + i;
-		z = d_pzbuffer + i;
+		z = dvars.zbuffer + i;
 		for(x = r_refdef.vrect.x; x < r_refdef.vrectright; x++, i++, pix++, z++)
 			*pix = blend_fog(*pix, *z);
 	}
--- a/r_local.h
+++ b/r_local.h
@@ -152,9 +152,6 @@
 // !!! if this is changed, it must be changed in asm_draw.h too !!!
 #define	NEAR_CLIP	0.01
 
-extern fixed16_t	sadjust, tadjust;
-extern fixed16_t	bbextents, bbextentt;
-
 #define MAXBVERTINDEXES	1000	// new clipped vertices when clipping bmodels
 								//  to the world BSP
 extern mvertex_t	*r_ptverts, *r_ptvertsmax;
--- a/r_shared.h
+++ b/r_shared.h
@@ -17,8 +17,6 @@
 
 //===================================================================
 
-extern int		cachewidth;
-extern pixel_t	*cacheblock;
 extern int		screenwidth;
 
 extern	float	pixelAspect;
--- a/vid_plan9.c
+++ b/vid_plan9.c
@@ -33,16 +33,16 @@
 	vid.height = Dy(screen->r);
 	if(vid.height < 160)
 		vid.height = 160;
-	if(d_pzbuffer != nil)
+	if(dvars.zbuffer != nil)
 		D_FlushCaches();
 
 	// alloc an extra line in case we want to wrap, and allocate the z-buffer
-	hunkvbuf = vid.width * vid.height * sizeof *d_pzbuffer;
+	hunkvbuf = vid.width * vid.height * sizeof(*dvars.zbuffer);
 	scachesz = D_SurfaceCacheForRes(vid.width, vid.height);
 	hunkvbuf += scachesz;
-	if((d_pzbuffer = realloc(d_pzbuffer, hunkvbuf)) == nil)
+	if((dvars.zbuffer = realloc(dvars.zbuffer, hunkvbuf)) == nil)
 		sysfatal("%r");
-	surfcache = (byte*)(d_pzbuffer + vid.width * vid.height);
+	surfcache = (byte*)(dvars.zbuffer + vid.width * vid.height);
 	D_InitCaches(surfcache, scachesz);
 
 	vid.rowbytes = vid.width;
--- a/vid_sdl.c
+++ b/vid_sdl.c
@@ -50,18 +50,17 @@
 	vid.buffer = vidbuffer;
 	vid.conbuffer = vid.buffer;
 
-	if(d_pzbuffer != nil){
+	if(dvars.zbuffer != nil){
 		D_FlushCaches();
-		free(d_pzbuffer);
-		d_pzbuffer = nil;
+		free(dvars.zbuffer);
 	}
 
 	// alloc an extra line in case we want to wrap, and allocate the z-buffer
-	hunkvbuf = vid.width * vid.height * sizeof *d_pzbuffer;
+	hunkvbuf = vid.width * vid.height * sizeof(*dvars.zbuffer);
 	scachesz = D_SurfaceCacheForRes(vid.width, vid.height);
 	hunkvbuf += scachesz;
-	d_pzbuffer = emalloc(hunkvbuf);
-	surfcache = (byte *)(d_pzbuffer + vid.width * vid.height);
+	dvars.zbuffer = emalloc(hunkvbuf);
+	surfcache = (byte *)(dvars.zbuffer + vid.width * vid.height);
 	D_InitCaches(surfcache, scachesz);
 }