From 842a15ee45fedaf5ebe106707b4d83d710232941 Mon Sep 17 00:00:00 2001
From: mittorn <mittorn@sibmail.com>
Date: Fri, 22 Mar 2019 23:58:43 +0700
Subject: [PATCH] ref_soft: Merge kernel texture filter

---
 r_local.h |  1 +
 r_main.c  |  3 ++-
 r_scan.c  | 70 ++++++++++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 60 insertions(+), 14 deletions(-)

diff --git a/r_local.h b/r_local.h
index ed56f0d5..53b1d5f2 100644
--- a/r_local.h
+++ b/r_local.h
@@ -1195,6 +1195,7 @@ extern cvar_t   *sw_reportedgeout;
 extern cvar_t   *sw_stipplealpha;
 extern cvar_t   *sw_surfcacheoverride;
 extern cvar_t *sw_waterwarp;
+extern cvar_t   *sw_texfilt;
 
 extern vec3_t modelorg;
 extern vec3_t r_origin;
diff --git a/r_main.c b/r_main.c
index 7cfc8517..57e69b61 100644
--- a/r_main.c
+++ b/r_main.c
@@ -83,6 +83,7 @@ cvar_t	*sw_reportsurfout;
 cvar_t  *sw_stipplealpha;
 cvar_t	*sw_surfcacheoverride;
 cvar_t	*sw_waterwarp;
+cvar_t	*sw_texfilt;
 
 cvar_t	*r_drawworld;
 cvar_t	*r_drawentities;
@@ -1654,7 +1655,7 @@ qboolean R_Init()
 	sw_surfcacheoverride = gEngfuncs.Cvar_Get ("sw_surfcacheoverride", "0", 0, "");
 	sw_waterwarp = gEngfuncs.Cvar_Get ("sw_waterwarp", "1", 0, "");
 	sw_mode = gEngfuncs.Cvar_Get( "sw_mode", "0", FCVAR_ARCHIVE, "");
-
+	sw_texfilt = gEngfuncs.Cvar_Get ("sw_texfilt", "0", 0, "texture dither");
 	//r_lefthand = ri.Cvar_Get( "hand", "0", FCVAR_USERINFO | FCVAR_ARCHIVE );
 //	r_speeds = ri.Cvar_Get ("r_speeds", "0", 0);
 
diff --git a/r_scan.c b/r_scan.c
index f0361371..5e51b8bc 100644
--- a/r_scan.c
+++ b/r_scan.c
@@ -391,6 +391,20 @@ void NonTurbulent8 (espan_t *pspan)
 
 #if	!id386
 
+
+int kernel[2][2][2] =
+{
+   {
+        {16384,0},
+        {49152,32768}
+   }
+   ,
+   {
+      {32768,49152},
+      {0,16384}
+   }
+};
+
 /*
 =============
 D_DrawSpans16
@@ -481,10 +495,10 @@ void D_DrawSpans16 (espan_t *pspan)
 			}
 			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
-			// texture
+			  // 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;
@@ -509,17 +523,47 @@ void D_DrawSpans16 (espan_t *pspan)
 					sstep = (snext - s) / (spancount - 1);
 					tstep = (tnext - t) / (spancount - 1);
 				}
-			}
+			  }
 
-			do
-			{
-				*pdest++ = *(pbase + (s >> 16) + (t >> 16) * cachewidth);
-				s += sstep;
-				t += tstep;
-			} while (--spancount > 0);
+            
+			// Drawing phrase
+				if (sw_texfilt->value == 0.0f)
+				{
+					do
+					{
+						*pdest++ = *(pbase + (s >> 16) + (t >> 16) * cachewidth);
+						s += sstep;
+						t += tstep;
+					} while (--spancount > 0);
+				}
+				else if (sw_texfilt->value == 1.0f)
+				{
+					do
+					{ 
+						int idiths = s;
+						int iditht = t;
 
-			s = snext;
-			t = tnext;
+						int X = (pspan->u + spancount) & 1;
+						int Y = (pspan->v)&1;
+
+						//Using the kernel
+						idiths += kernel[X][Y][0];
+						iditht += kernel[X][Y][1];
+
+						idiths = idiths >> 16;
+						idiths = idiths ? idiths -1 : idiths;
+										
+
+						iditht = iditht >> 16;
+						iditht = iditht ? iditht -1 : iditht;
+										
+
+						*pdest++ = *(pbase + idiths + iditht * cachewidth);
+						s += sstep;
+						t += tstep;
+					} while (--spancount > 0);
+				}
+ 
 
 		} while (count > 0);