More header unification...
[btb/d2x.git] / texmap / scanline.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13 /*
14  * $Source: /cvs/cvsroot/d2x/texmap/scanline.c,v $
15  * $Revision: 1.1.1.1 $
16  * $Author: bradleyb $
17  * $Date: 2001-01-19 03:30:16 $
18  * 
19  * Routines to draw the texture mapped scanlines.
20  * 
21  * $Log: not supported by cvs2svn $
22  * Revision 1.4  1999/10/18 00:31:01  donut
23  * more alpha fixes from Falk Hueffner
24  *
25  * Revision 1.3  1999/10/14 04:48:21  donut
26  * alpha fixes, and gl_font args
27  *
28  * Revision 1.2  1999/09/21 07:22:40  sekmu
29  * remove unused var warning
30  *
31  * Revision 1.1.1.1  1999/06/14 22:14:08  donut
32  * Import of d1x 1.37 source.
33  *
34  * Revision 1.2  1995/02/20  18:23:39  john
35  * Added new module for C versions of inner loops.
36  * 
37  * Revision 1.1  1995/02/20  17:42:27  john
38  * Initial revision
39  * 
40  * 
41  */
42
43
44 #ifdef RCS
45 static char rcsid[] = "$Id: scanline.c,v 1.1.1.1 2001-01-19 03:30:16 bradleyb Exp $";
46 #endif
47
48 #include <conf.h>
49 #include <math.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "maths.h"
56 #include "mono.h"
57 #include "gr.h"
58 #include "grdef.h"
59 #include "texmap.h"
60 #include "texmapl.h"
61 #include "scanline.h"
62
63 void c_tmap_scanline_flat()
64 {
65         ubyte *dest;
66 //        int x;
67
68         dest = (ubyte *)(write_buffer + fx_xleft + (bytes_per_row * fx_y )  );
69
70 /*      for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
71                 *dest++ = tmap_flat_color;
72         }*/
73         memset(dest,tmap_flat_color,fx_xright-fx_xleft+1);
74 }
75
76 void c_tmap_scanline_shaded()
77 {
78         int fade;
79         ubyte *dest;
80         int x;
81
82         dest = (ubyte *)(write_buffer + fx_xleft + (bytes_per_row * fx_y)  );
83
84         fade = tmap_flat_shade_value<<8;
85         for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
86                 *dest++ = gr_fade_table[ fade |(*dest)];
87         }
88 }
89
90 void c_tmap_scanline_lin_nolight()
91 {
92         ubyte *dest;
93         uint c;
94         int x;
95         fix u,v,dudx, dvdx;
96
97         u = fx_u;
98         v = fx_v*64;
99         dudx = fx_du_dx; 
100         dvdx = fx_dv_dx*64; 
101
102         dest = (ubyte *)(write_buffer + fx_xleft + (bytes_per_row * fx_y)  );
103
104         if (!Transparency_on)   {
105                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
106                         *dest++ = (uint)pixptr[ (f2i(v)&(64*63)) + (f2i(u)&63) ];
107                         u += dudx;
108                         v += dvdx;
109                 }
110         } else {
111                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
112                         c = (uint)pixptr[ (f2i(v)&(64*63)) + (f2i(u)&63) ];
113                         if ( c!=255)
114                                 *dest = c;
115                         dest++;
116                         u += dudx;
117                         v += dvdx;
118                 }
119         }
120 }
121
122
123 #if 1
124 void c_tmap_scanline_lin()
125 {
126         ubyte *dest;
127         uint c;
128         int x, j;
129         fix u,v,l,dudx, dvdx, dldx;
130
131         u = fx_u;
132         v = fx_v*64;
133         dudx = fx_du_dx; 
134         dvdx = fx_dv_dx*64; 
135
136         l = fx_l>>8;
137         dldx = fx_dl_dx>>8;
138         dest = (ubyte *)(write_buffer + fx_xleft + (bytes_per_row * fx_y)  );
139
140         if (!Transparency_on)   {
141                 ubyte*                  pixPtrLocalCopy = pixptr;
142                 ubyte*                  fadeTableLocalCopy = gr_fade_table;
143                 unsigned long   destlong;
144
145                 x = fx_xright-fx_xleft+1;
146
147                 if ((j = (unsigned long) dest & 3) != 0)
148                         {
149                         j = 4 - j;
150
151                         if (j > x)
152                                 j = x;
153
154                         while (j > 0)
155                                 {       
156                                 //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
157                                 *dest++ = (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint) pixPtrLocalCopy[ (f2i(v)&(64*63)) + (f2i(u)&63) ] ];
158                                 //end edit -MM
159                                 l += dldx;
160                                 u += dudx;
161                                 v += dvdx;
162                                 x--;
163                                 j--;
164                                 }
165                         }
166
167                 j &= ~3;
168                 while (j > 0)
169                         {
170                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
171                         destlong = (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint) pixPtrLocalCopy[ (f2i(v)&(64*63)) + (f2i(u)&63) ] ] << 24;
172                         //end edit -MM
173                         l += dldx;
174                         u += dudx;
175                         v += dvdx;
176                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
177                         destlong |= (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint) pixPtrLocalCopy[ (f2i(v)&(64*63)) + (f2i(u)&63) ] ] << 16;
178                         //end edit -MM
179                         l += dldx;
180                         u += dudx;
181                         v += dvdx;
182                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
183                         destlong |= (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint) pixPtrLocalCopy[ (f2i(v)&(64*63)) + (f2i(u)&63) ] ] << 8;
184                         //end edit -MM
185                         l += dldx;
186                         u += dudx;
187                         v += dvdx;
188                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
189                         destlong |= (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint) pixPtrLocalCopy[ (f2i(v)&(64*63)) + (f2i(u)&63) ] ];
190                         //end edit -MM
191                         l += dldx;
192                         u += dudx;
193                         v += dvdx;
194                         *((unsigned long *) dest) = destlong;
195                         dest += 4;
196                         x -= 4;
197                         j -= 4;
198                         }
199
200                 while (x-- > 0)
201                         {
202                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
203                         *dest++ = (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint) pixPtrLocalCopy[ (f2i(v)&(64*63)) + (f2i(u)&63) ] ];
204                         //end edit -MM
205                         l += dldx;
206                         u += dudx;
207                         v += dvdx;
208                         }
209
210         } else {
211                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
212                         c = (uint)pixptr[ (f2i(v)&(64*63)) + (f2i(u)&63) ];
213                         if ( c!=TRANSPARENCY_COLOR)
214                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
215                                 *dest = gr_fade_table[ (l&(0x7f00)) + c ];
216                         //end edit -MM
217                         dest++;
218                         l += dldx;
219                         u += dudx;
220                         v += dvdx;
221                 }
222         }
223 }
224
225 #else
226 void c_tmap_scanline_lin()
227 {
228         ubyte *dest;
229         uint c;
230         int x;
231         fix u,v,l,dudx, dvdx, dldx;
232
233         u = fx_u;
234         v = fx_v*64;
235         dudx = fx_du_dx; 
236         dvdx = fx_dv_dx*64; 
237
238         l = fx_l>>8;
239         dldx = fx_dl_dx>>8;
240         dest = (ubyte *)(write_buffer + fx_xleft + (bytes_per_row * fx_y)  );
241
242         if (!Transparency_on)   {
243                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
244                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
245                         *dest++ = gr_fade_table[ (l&(0x7f00)) + (uint)pixptr[ (f2i(v)&(64*63)) + (f2i(u)&63) ] ];
246                         //end edit -MM
247                         l += dldx;
248                         u += dudx;
249                         v += dvdx;
250                 }
251         } else {
252                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
253                         c = (uint)pixptr[ (f2i(v)&(64*63)) + (f2i(u)&63) ];
254                         if ( c!=255)
255                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
256                                 *dest = gr_fade_table[ (l&(0x7f00)) + c ];
257                         //end edit -MM
258                         dest++;
259                         l += dldx;
260                         u += dudx;
261                         v += dvdx;
262                 }
263         }
264 }
265 #endif
266
267 #ifdef FP_TMAP
268 void            c_tmap_scanline_per_nolight()
269 {
270         ubyte          *dest;
271         uint            c;
272         int             x, j;
273         double          u, v, z, dudx, dvdx, dzdx, rec_z;
274         u_int64_t       destlong;
275
276         u = f2db(fx_u);
277         v = f2db(fx_v) * 64.0;
278         z = f2db(fx_z);
279         dudx = f2db(fx_du_dx);
280         dvdx = f2db(fx_dv_dx) * 64.0;
281         dzdx = f2db(fx_dz_dx);
282
283         rec_z = 1.0 / z;
284
285         dest = (ubyte *) (write_buffer + fx_xleft + (bytes_per_row * fx_y));
286
287         x = fx_xright - fx_xleft + 1;
288         if (!Transparency_on) {
289                 if (x >= 8) {
290                         if ((j = (size_t) dest & 7) != 0) {
291                                 j = 8 - j;
292
293                                 while (j > 0) {
294                                         *dest++ =
295                                             (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
296                                                          (((int) (u * rec_z)) & 63)];
297                                         u += dudx;
298                                         v += dvdx;
299                                         z += dzdx;
300                                         rec_z = 1.0 / z;
301                                         x--;
302                                         j--;
303                                 }
304                         }
305
306                         while (j >= 8) {
307                                 destlong =
308                                     (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
309                                                        (((int) (u * rec_z)) & 63)];
310                                 u += dudx;
311                                 v += dvdx;
312                                 z += dzdx;
313                                 rec_z = 1.0 / z;
314                                 destlong |=
315                                     (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
316                                                        (((int) (u * rec_z)) & 63)] << 8;
317                                 u += dudx;
318                                 v += dvdx;
319                                 z += dzdx;
320                                 rec_z = 1.0 / z;
321                                 destlong |=
322                                     (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
323                                                        (((int) (u * rec_z)) & 63)] << 16;
324                                 u += dudx;
325                                 v += dvdx;
326                                 z += dzdx;
327                                 rec_z = 1.0 / z;
328                                 destlong |=
329                                     (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
330                                                        (((int) (u * rec_z)) & 63)] << 24;
331                                 u += dudx;
332                                 v += dvdx;
333                                 z += dzdx;
334                                 rec_z = 1.0 / z;
335                                 destlong |=
336                                     (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
337                                                        (((int) (u * rec_z)) & 63)] << 32;
338                                 u += dudx;
339                                 v += dvdx;
340                                 z += dzdx;
341                                 rec_z = 1.0 / z;
342                                 destlong |=
343                                     (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
344                                                        (((int) (u * rec_z)) & 63)] << 40;
345                                 u += dudx;
346                                 v += dvdx;
347                                 z += dzdx;
348                                 rec_z = 1.0 / z;
349                                 destlong |=
350                                     (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
351                                                        (((int) (u * rec_z)) & 63)] << 48;
352                                 u += dudx;
353                                 v += dvdx;
354                                 z += dzdx;
355                                 rec_z = 1.0 / z;
356                                 destlong |=
357                                     (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
358                                                        (((int) (u * rec_z)) & 63)] << 56;
359                                 u += dudx;
360                                 v += dvdx;
361                                 z += dzdx;
362                                 rec_z = 1.0 / z;
363
364                                 *((u_int64_t *) dest) = destlong;
365                                 dest += 8;
366                                 x -= 8;
367                                 j -= 8;
368                         }
369                 }
370                 while (x-- > 0) {
371                         *dest++ =
372                             (u_int64_t) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
373                                                (((int) (u * rec_z)) & 63)];
374                         u += dudx;
375                         v += dvdx;
376                         z += dzdx;
377                         rec_z = 1.0 / z;
378                 }
379         } else {
380                 x = fx_xright - fx_xleft + 1;
381
382                 if (x >= 8) {
383                         if ((j = (size_t) dest & 7) != 0) {
384                                 j = 8 - j;
385
386                                 while (j > 0) {
387                                         c =
388                                             (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
389                                                          (((int) (u * rec_z)) & 63)];
390                                         if (c != 255)
391                                                 *dest = c;
392                                         dest++;
393                                         u += dudx;
394                                         v += dvdx;
395                                         z += dzdx;
396                                         rec_z = 1.0 / z;
397                                         x--;
398                                         j--;
399                                 }
400                         }
401
402                         j = x;
403                         while (j >= 8) {
404                                 destlong = *((u_int64_t *) dest);
405                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
406                                                   (((int) (u * rec_z)) & 63)];
407                                 if (c != 255) {
408                                         destlong &= ~(u_int64_t)0xFF;
409                                         destlong |= (u_int64_t) c;
410                                 }
411                                 u += dudx;
412                                 v += dvdx;
413                                 z += dzdx;
414                                 rec_z = 1.0 / z;
415                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
416                                                   (((int) (u * rec_z)) & 63)];
417                                 if (c != 255) {
418                                         destlong &= ~((u_int64_t)0xFF << 8);
419                                         destlong |= (u_int64_t) c << 8;
420                                 }
421                                 u += dudx;
422                                 v += dvdx;
423                                 z += dzdx;
424                                 rec_z = 1.0 / z;
425                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
426                                                   (((int) (u * rec_z)) & 63)];
427                                 if (c != 255) {
428                                         destlong &= ~((u_int64_t)0xFF << 16);
429                                         destlong |= (u_int64_t) c << 16;
430                                 }
431                                 u += dudx;
432                                 v += dvdx;
433                                 z += dzdx;
434                                 rec_z = 1.0 / z;
435                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
436                                                   (((int) (u * rec_z)) & 63)];
437                                 if (c != 255) {
438                                         destlong &= ~((u_int64_t)0xFF << 24);
439                                         destlong |= (u_int64_t) c << 24;
440                                 }
441                                 u += dudx;
442                                 v += dvdx;
443                                 z += dzdx;
444                                 rec_z = 1.0 / z;
445                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
446                                                   (((int) (u * rec_z)) & 63)];
447                                 if (c != 255) {
448                                         destlong &= ~((u_int64_t)0xFF << 32);
449                                         destlong |= (u_int64_t) c << 32;
450                                 }
451                                 u += dudx;
452                                 v += dvdx;
453                                 z += dzdx;
454                                 rec_z = 1.0 / z;
455                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
456                                                   (((int) (u * rec_z)) & 63)];
457                                 if (c != 255) {
458                                         destlong &= ~((u_int64_t)0xFF << 40);
459                                         destlong |= (u_int64_t) c << 40;
460                                 }
461                                 u += dudx;
462                                 v += dvdx;
463                                 z += dzdx;
464                                 rec_z = 1.0 / z;
465                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
466                                                   (((int) (u * rec_z)) & 63)];
467                                 if (c != 255) {
468                                         destlong &= ~((u_int64_t)0xFF << 48);
469                                         destlong |= (u_int64_t) c << 48;
470                                 }
471                                 u += dudx;
472                                 v += dvdx;
473                                 z += dzdx;
474                                 rec_z = 1.0 / z;
475                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
476                                                   (((int) (u * rec_z)) & 63)];
477                                 if (c != 255) {
478                                         destlong &= ~((u_int64_t)0xFF << 56);
479                                         destlong |= (u_int64_t) c << 56;
480                                 }
481                                 u += dudx;
482                                 v += dvdx;
483                                 z += dzdx;
484                                 rec_z = 1.0 / z;
485
486                                 *((u_int64_t *) dest) = destlong;
487                                 dest += 8;
488                                 x -= 8;
489                                 j -= 8;
490                         }
491                 }
492                 while (x-- > 0) {
493                         c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
494                                           (((int) (u * rec_z)) & 63)];
495                         if (c != 255)
496                                 *dest = c;
497                         dest++;
498                         u += dudx;
499                         v += dvdx;
500                         z += dzdx;
501                         rec_z = 1.0 / z;
502                 }
503         }
504 }
505 #else
506 void c_tmap_scanline_per_nolight()
507 {
508         ubyte *dest;
509         uint c;
510         int x;
511         fix u,v,z,dudx, dvdx, dzdx;
512
513         u = fx_u;
514         v = fx_v*64;
515         z = fx_z;
516         dudx = fx_du_dx; 
517         dvdx = fx_dv_dx*64; 
518         dzdx = fx_dz_dx;
519
520         dest = (ubyte *)(write_buffer + fx_xleft + (bytes_per_row * fx_y)  );
521
522         if (!Transparency_on)   {
523                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
524                         *dest++ = (uint)pixptr[ ( (v/z)&(64*63) ) + ((u/z)&63) ];
525                         u += dudx;
526                         v += dvdx;
527                         z += dzdx;
528                 }
529         } else {
530                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
531                         c = (uint)pixptr[ ( (v/z)&(64*63) ) + ((u/z)&63) ];
532                         if ( c!=255)
533                                 *dest = c;
534                         dest++;
535                         u += dudx;
536                         v += dvdx;
537                         z += dzdx;
538                 }
539         }
540 }
541 #endif
542
543 #ifdef FP_TMAP
544 void c_tmap_scanline_per()
545 {
546         ubyte          *dest;
547         uint            c;
548         int             x, j;
549         double          u, v, z, l, dudx, dvdx, dzdx, dldx, rec_z;
550         u_int64_t       destlong;
551
552         u = f2db(fx_u);
553         v = f2db(fx_v) * 64.0;
554         z = f2db(fx_z);
555         l = f2db(fx_l);
556         dudx = f2db(fx_du_dx);
557         dvdx = f2db(fx_dv_dx) * 64.0;
558         dzdx = f2db(fx_dz_dx);
559         dldx = f2db(fx_dl_dx);
560
561         rec_z = 1.0 / z; // gcc 2.95.2 is won't do this optimization itself
562
563         dest = (ubyte *) (write_buffer + fx_xleft + (bytes_per_row * fx_y));
564         x = fx_xright - fx_xleft + 1;
565
566         if (!Transparency_on) {
567                 if (x >= 8) {
568                         if ((j = (size_t) dest & 7) != 0) {
569                                 j = 8 - j;
570
571                                 while (j > 0) {
572                                         *dest++ =
573                                             gr_fade_table[((int) fabs(l)) * 256 +
574                                                           (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
575                                                                         (((int) (u * rec_z)) & 63)]];
576                                         l += dldx;
577                                         u += dudx;
578                                         v += dvdx;
579                                         z += dzdx;
580                                         rec_z = 1.0 / z;
581                                         x--;
582                                         j--;
583                                 }
584                         }
585
586                         j = x;
587                         while (j >= 8) {
588                                 destlong =
589                                     (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 +
590                                                               (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
591                                                                             (((int) (u * rec_z)) & 63)]];
592                                 l += dldx;
593                                 u += dudx;
594                                 v += dvdx;
595                                 z += dzdx;
596                                 rec_z = 1.0 / z;
597                                 destlong |=
598                                     (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 +
599                                                               (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
600                                                                             (((int) (u * rec_z)) & 63)]] << 8;
601                                 l += dldx;
602                                 u += dudx;
603                                 v += dvdx;
604                                 z += dzdx;
605                                 rec_z = 1.0 / z;
606                                 destlong |=
607                                     (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 +
608                                                               (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
609                                                                             (((int) (u * rec_z)) & 63)]] << 16;
610                                 l += dldx;
611                                 u += dudx;
612                                 v += dvdx;
613                                 z += dzdx;
614                                 rec_z = 1.0 / z;
615                                 destlong |=
616                                     (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 +
617                                                               (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
618                                                                             (((int) (u * rec_z)) & 63)]] << 24;
619                                 l += dldx;
620                                 u += dudx;
621                                 v += dvdx;
622                                 z += dzdx;
623                                 rec_z = 1.0 / z;
624                                 destlong |=
625                                     (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 +
626                                                               (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
627                                                                             (((int) (u * rec_z)) & 63)]] << 32;
628                                 l += dldx;
629                                 u += dudx;
630                                 v += dvdx;
631                                 z += dzdx;
632                                 rec_z = 1.0 / z;
633                                 destlong |=
634                                     (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 +
635                                                               (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
636                                                                             (((int) (u * rec_z)) & 63)]] << 40;
637                                 l += dldx;
638                                 u += dudx;
639                                 v += dvdx;
640                                 z += dzdx;
641                                 rec_z = 1.0 / z;
642                                 destlong |=
643                                     (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 +
644                                                               (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
645                                                                             (((int) (u * rec_z)) & 63)]] << 48;
646                                 l += dldx;
647                                 u += dudx;
648                                 v += dvdx;
649                                 z += dzdx;
650                                 rec_z = 1.0 / z;
651                                 destlong |=
652                                     (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 +
653                                                               (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) +
654                                                                             (((int) (u * rec_z)) & 63)]] << 56;
655                                 l += dldx;
656                                 u += dudx;
657                                 v += dvdx;
658                                 z += dzdx;
659                                 rec_z = 1.0 / z;
660
661                                 *((u_int64_t *) dest) = destlong;
662                                 dest += 8;
663                                 x -= 8;
664                                 j -= 8;
665                         }
666                 }
667                 while (x-- > 0) {
668                         *dest++ =
669                             gr_fade_table[((int) fabs(l)) * 256 +
670                                           (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)]];
671                         l += dldx;
672                         u += dudx;
673                         v += dvdx;
674                         z += dzdx;
675                         rec_z = 1.0 / z;
676                 }
677         } else {
678                 if (x >= 8) {
679                         if ((j = (size_t) dest & 7) != 0) {
680                                 j = 8 - j;
681
682                                 while (j > 0) {
683                                         c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
684                                         if (c != 255)
685                                                 *dest = gr_fade_table[((int) fabs(l)) * 256 + c];
686                                         dest++;
687                                         l += dldx;
688                                         u += dudx;
689                                         v += dvdx;
690                                         z += dzdx;
691                                         rec_z = 1.0 / z;
692                                         x--;
693                                         j--;
694                                 }
695                         }
696
697                         j = x;
698                         while (j >= 8) {
699                                 destlong = *((u_int64_t *) dest);
700                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
701                                 if (c != 255) {
702                                         destlong &= ~(u_int64_t)0xFF;
703                                         destlong |= (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 + c];
704                                 }
705                                 l += dldx;
706                                 u += dudx;
707                                 v += dvdx;
708                                 z += dzdx;
709                                 rec_z = 1.0 / z;
710                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
711                                 if (c != 255) {
712                                         destlong &= ~((u_int64_t)0xFF << 8);
713                                         destlong |= (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 + c] << 8;
714                                 }
715                                 l += dldx;
716                                 u += dudx;
717                                 v += dvdx;
718                                 z += dzdx;
719                                 rec_z = 1.0 / z;
720                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
721                                 if (c != 255) {
722                                         destlong &= ~((u_int64_t)0xFF << 16);
723                                         destlong |= (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 + c] << 16;
724                                 }
725                                 l += dldx;
726                                 u += dudx;
727                                 v += dvdx;
728                                 z += dzdx;
729                                 rec_z = 1.0 / z;
730                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
731                                 if (c != 255) {
732                                         destlong &= ~((u_int64_t)0xFF << 24);
733                                         destlong |= (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 + c] << 24;
734                                 }
735                                 l += dldx;
736                                 u += dudx;
737                                 v += dvdx;
738                                 z += dzdx;
739                                 rec_z = 1.0 / z;
740                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
741                                 if (c != 255) {
742                                         destlong &= ~((u_int64_t)0xFF << 32);
743                                         destlong |= (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 + c] << 32;
744                                 }
745                                 l += dldx;
746                                 u += dudx;
747                                 v += dvdx;
748                                 z += dzdx;
749                                 rec_z = 1.0 / z;
750                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
751                                 if (c != 255) {
752                                         destlong &= ~((u_int64_t)0xFF << 40);
753                                         destlong |= (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 + c] << 40;
754                                 }
755                                 l += dldx;
756                                 u += dudx;
757                                 v += dvdx;
758                                 z += dzdx;
759                                 rec_z = 1.0 / z;
760                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
761                                 if (c != 255) {
762                                         destlong &= ~((u_int64_t)0xFF << 48);
763                                         destlong |= (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 + c] << 48;
764                                 }
765                                 l += dldx;
766                                 u += dudx;
767                                 v += dvdx;
768                                 z += dzdx;
769                                 rec_z = 1.0 / z;
770                                 c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
771                                 if (c != 255) {
772                                         destlong &= ~((u_int64_t)0xFF << 56);
773                                         destlong |= (u_int64_t) gr_fade_table[((int) fabs(l)) * 256 + c] << 56;
774                                 }
775                                 l += dldx;
776                                 u += dudx;
777                                 v += dvdx;
778                                 z += dzdx;
779                                 rec_z = 1.0 / z;
780
781                                 *((u_int64_t *) dest) = destlong;
782                                 dest += 8;
783                                 x -= 8;
784                                 j -= 8;
785                         }
786                 }
787                 while (x-- > 0) {
788                         c = (uint) pixptr[(((int) (v * rec_z)) & (64 * 63)) + (((int) (u * rec_z)) & 63)];
789                         if (c != 255)
790                                 *dest = gr_fade_table[((int) fabs(l)) * 256 + c];
791                         dest++;
792                         l += dldx;
793                         u += dudx;
794                         v += dvdx;
795                         z += dzdx;
796                         rec_z = 1.0 / z;
797                 }
798         }
799 }
800
801 #elif 1
802 // note the unrolling loop is broken. It is never called, and uses big endian. -- FH
803 void c_tmap_scanline_per()
804 {
805         ubyte *dest;
806         uint c;
807         int x, j;
808         fix l,u,v,z;
809         fix dudx, dvdx, dzdx, dldx;
810
811         u = fx_u;
812         v = fx_v*64;
813         z = fx_z;
814         dudx = fx_du_dx; 
815         dvdx = fx_dv_dx*64; 
816         dzdx = fx_dz_dx;
817
818         l = fx_l>>8;
819         dldx = fx_dl_dx>>8;
820         dest = (ubyte *)(write_buffer + fx_xleft + (bytes_per_row * fx_y)  );
821
822         if (!Transparency_on)   {
823                 ubyte*                  pixPtrLocalCopy = pixptr;
824                 ubyte*                  fadeTableLocalCopy = gr_fade_table;
825                 unsigned long   destlong;
826
827                 x = fx_xright-fx_xleft+1;
828
829                 if ((j = (unsigned long) dest & 3) != 0)
830                         {
831                         j = 4 - j;
832
833                         if (j > x)
834                                 j = x;
835
836                         while (j > 0)
837                                 {       
838                                 //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
839                                 *dest++ = fadeTableLocalCopy[ (l&(0x7f00)) + (uint)pixPtrLocalCopy[ ( (v/z)&(64*63) ) + ((u/z)&63) ] ];
840                                 //end edit -MM
841                                 l += dldx;
842                                 u += dudx;
843                                 v += dvdx;
844                                 z += dzdx;
845                                 x--;
846                                 j--;
847                                 }
848                         }
849
850                 j &= ~3;
851                 while (j > 0)
852                         {
853                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
854                         destlong = (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint)pixPtrLocalCopy[ ( (v/z)&(64*63) ) + ((u/z)&63) ] ] << 24;
855                         //end edit -MM
856                         l += dldx;
857                         u += dudx;
858                         v += dvdx;
859                         z += dzdx;
860                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
861                         destlong |= (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint)pixPtrLocalCopy[ ( (v/z)&(64*63) ) + ((u/z)&63) ] ] << 16;
862                         //end edit -MM
863                         l += dldx;
864                         u += dudx;
865                         v += dvdx;
866                         z += dzdx;
867                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
868                         destlong |= (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint)pixPtrLocalCopy[ ( (v/z)&(64*63) ) + ((u/z)&63) ] ] << 8;
869                         //end edit -MM
870                         l += dldx;
871                         u += dudx;
872                         v += dvdx;
873                         z += dzdx;
874                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
875                         destlong |= (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint)pixPtrLocalCopy[ ( (v/z)&(64*63) ) + ((u/z)&63) ] ];
876                         //end edit -MM
877                         l += dldx;
878                         u += dudx;
879                         v += dvdx;
880                         z += dzdx;
881                         *((unsigned long *) dest) = destlong;
882                         dest += 4;
883                         x -= 4;
884                         j -= 4;
885                         }
886
887                 while (x-- > 0)
888                         {
889                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
890                         *dest++ = (unsigned long) fadeTableLocalCopy[ (l&(0x7f00)) + (uint)pixPtrLocalCopy[ ( (v/z)&(64*63) ) + ((u/z)&63) ] ];
891                         //end edit -MM
892                         l += dldx;
893                         u += dudx;
894                         v += dvdx;
895                         z += dzdx;
896                         }
897
898         } else {
899                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
900                         c = (uint)pixptr[ ( (v/z)&(64*63) ) + ((u/z)&63) ];
901                         if ( c!=TRANSPARENCY_COLOR)
902                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
903                                 *dest = gr_fade_table[ (l&(0x7f00)) + c ];
904                         //end edit -MM
905                         dest++;
906                         l += dldx;
907                         u += dudx;
908                         v += dvdx;
909                         z += dzdx;
910                 }
911         }
912 }
913
914 #else
915 void c_tmap_scanline_per()
916 {
917         ubyte *dest;
918         uint c;
919         int x;
920         fix u,v,z,l,dudx, dvdx, dzdx, dldx;
921
922         u = fx_u;
923         v = fx_v*64;
924         z = fx_z;
925         dudx = fx_du_dx; 
926         dvdx = fx_dv_dx*64; 
927         dzdx = fx_dz_dx;
928
929         l = fx_l>>8;
930         dldx = fx_dl_dx>>8;
931         dest = (ubyte *)(write_buffer + fx_xleft + (bytes_per_row * fx_y)  );
932
933         if (!Transparency_on)   {
934                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
935                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
936                         *dest++ = gr_fade_table[ (l&(0x7f00)) + (uint)pixptr[ ( (v/z)&(64*63) ) + ((u/z)&63) ] ];
937                         //end edit -MM
938                         l += dldx;
939                         u += dudx;
940                         v += dvdx;
941                         z += dzdx;
942                 }
943         } else {
944                 for (x= fx_xright-fx_xleft+1 ; x > 0; --x ) {
945                         c = (uint)pixptr[ ( (v/z)&(64*63) ) + ((u/z)&63) ];
946                         if ( c!=255)
947                         //edited 05/18/99 Matt Mueller - changed from 0xff00 to 0x7f00 to fix glitches
948                                 *dest = gr_fade_table[ (l&(0x7f00)) + c ];
949                         //end edit -MM
950                         dest++;
951                         l += dldx;
952                         u += dudx;
953                         v += dvdx;
954                         z += dzdx;
955                 }
956         }
957 }
958
959
960 #endif