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