home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / texmap / tmap_16l.asm < prev    next >
Assembly Source File  |  1998-06-08  |  8KB  |  305 lines

  1. ;THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  2. ;SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  3. ;END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  4. ;ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  5. ;IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  6. ;SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  7. ;FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  8. ;CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  9. ;AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  10. ;COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  11. ;
  12. ; $Source: f:/miner/source/texmap/rcs/tmap_16l.asm $
  13. ; $Revision: 1.4 $
  14. ; $Author: mike $
  15. ; $Date: 1994/11/30 00:56:39 $
  16. ;
  17. ; inner loop of 16 bit per pixel lighted texture mapper
  18. ;
  19. ; $Log: tmap_16l.asm $
  20. ; Revision 1.4  1994/11/30  00:56:39  mike
  21. ; optimization.
  22. ; Revision 1.3  1994/11/12  16:39:31  mike
  23. ; jae to ja.
  24. ; Revision 1.2  1993/11/22  10:24:32  mike
  25. ; *** empty log message ***
  26. ; Revision 1.1  1993/09/08  17:29:20  mike
  27. ; Initial revision
  28. ;
  29. ;
  30.  
  31.  
  32.     .386
  33.  
  34.     public    asm_tmap_scanline_lin_16l_
  35.  
  36.     include    tmap_inc.asm
  37.  
  38. _DATA    SEGMENT DWORD PUBLIC USE32 'DATA'
  39.  
  40.     extrn    _fx_u:dword
  41.     extrn    _fx_v:dword
  42.     extrn    _fx_du_dx:dword
  43.     extrn    _fx_dv_dx:dword
  44.     extrn    _fx_y:dword
  45.     extrn    _fx_xleft:dword
  46.     extrn    _fx_xright:dword
  47.  
  48.     extrn    _pixptr:dword
  49.  
  50.     extrn    _x:dword
  51.     extrn    _loop_count:dword
  52.  
  53.     extrn    _fx_rgb:dword
  54.     extrn    _fx_drgb_dx:dword
  55.  
  56.     extrn    _fx_r:dword
  57.     extrn    _fx_g:dword
  58.     extrn    _fx_b:dword
  59.  
  60.     extrn    _fx_dr_dx:dword
  61.     extrn    _fx_dg_dx:dword
  62.     extrn    _fx_db_dx:dword
  63.  
  64. _DATA    ENDS
  65.  
  66. DGROUP    GROUP    _DATA
  67.  
  68. _TEXT   SEGMENT PARA PUBLIC USE32 'CODE'
  69.  
  70.     ASSUME    DS:_DATA
  71.     ASSUME    CS:_TEXT
  72.  
  73. ; --------------------------------------------------------------------------------------------------
  74. ; Enter:
  75. ;    _xleft    fixed point left x coordinate
  76. ;    _xright    fixed point right x coordinate
  77. ;    _y    fixed point y coordinate
  78. ;    _pixptr    address of source pixel map
  79. ;    _u    fixed point initial u coordinate
  80. ;    _v    fixed point initial v coordinate
  81. ;    _du_dx    fixed point du/dx
  82. ;    _dv_dx    fixed point dv/dx
  83.  
  84. ;   for (x = (int) xleft; x <= (int) xright; x++) {
  85. ;      _setcolor(read_pixel_from_tmap(srcb,((int) (u/z)) & 63,((int) (v/z)) & 63));
  86. ;      _setpixel(x,y);
  87. ;
  88. ;      u += du_dx;
  89. ;      v += dv_dx;
  90. ;      z += dz_dx;
  91. ;   }
  92.  
  93.     align    4
  94. asm_tmap_scanline_lin_rgb_:
  95.     pusha
  96.  
  97. ; Setup for loop:    _loop_count  iterations = (int) xright - (int) xleft
  98. ;    esi    source pixel pointer = pixptr
  99. ;    edi    initial row pointer = y*320+x
  100.  
  101. ; set esi = pointer to start of texture map data
  102.     mov    esi,_pixptr
  103.  
  104. ; set edi = address of first pixel to modify
  105.     mov    edi,_fx_y
  106.  
  107.     cmp    edi,_window_bottom
  108.     ja    _none_to_do
  109.     sub    edi,_window_top
  110.  
  111.     imul    edi,_bytes_per_row
  112.     mov    eax,_fx_xleft
  113.     sar    eax,16
  114.     jns    eax_ok
  115.     sub    eax,eax
  116. eax_ok:
  117.     sub    eax,_window_left
  118.     add    edi,eax
  119.     add    edi,eax
  120.     add    edi,write_buffer
  121.  
  122. ; set _loop_count = # of iterations
  123.     mov    eax,_fx_xright
  124.     sar    eax,16
  125.     cmp    eax,_window_right
  126.     jb    eax_ok1
  127.     mov    eax,_window_right
  128. eax_ok1:    cmp    eax,_window_left
  129.     ja    eax_ok2
  130.     mov    eax,_window_left
  131. eax_ok2:
  132.  
  133.     mov    ebx,_fx_xleft
  134.     sar    ebx,16
  135.     sub    eax,ebx
  136.     js    _none_to_do
  137.     cmp    eax,_window_width
  138.     jbe    _ok_to_do
  139.     mov    eax,_window_width
  140. _ok_to_do:
  141.     mov    _loop_count,eax
  142.  
  143. ;    edi    destination pixel pointer
  144.  
  145.     mov    ebx,_fx_u
  146.     mov    ebp,_fx_v
  147.  
  148.     shl    ebx,10
  149.     shl    ebp,10
  150.  
  151.     shl    _fx_du_dx,10
  152.     shl    _fx_dv_dx,10
  153.  
  154. ; rgb values are passed in the following peculiar, confidential, trade secreted, copyrighted, patented format:
  155. ; [ 5 bits ] [ 5 bits ] [ 5 bits ] [ 5 bits ] [ 2 bits ] [ 5 bits ] [ 5 bits ]
  156. ;   red int   red frac   blue int  blue frac    unused   green int  green frac
  157. ; The reason they are stored in the order red, blue, green is to optimize the process of packing together the three 5 bit
  158. ; values for red, green, blue in the conventional manner, suitable for an inverse table lookup
  159.  
  160. ; convert fixed point values in _fx_dr_dx, _fx_dg_dx, _fx_db_dx to _fx_drgb_dx
  161. ;** int 3
  162.     mov    eax,_fx_dg_dx    ; get green value
  163.     sar    eax,11    ; after shift, low order 10 bits are what we want
  164.     jns    dgok1
  165.     inc    eax
  166. dgok1:    shrd    ecx,eax,10    ; shift green 5i.5f into destination
  167.  
  168.     shr    ecx,2    ; shift in two don't care bits
  169.  
  170.     mov    eax,_fx_db_dx
  171.     sar    eax,11
  172.     jns    dbok1
  173.     inc    eax
  174. dbok1:    shrd    ecx,eax,10
  175.  
  176.     mov    eax,_fx_dr_dx
  177.     sar    eax,11
  178.     jns    drok1
  179.     inc    eax
  180. drok1:    shrd    ecx,eax,10    ; now %ecx is correct!
  181.     mov    _fx_drgb_dx,ecx
  182.  
  183. ; convert fixed point values in _fx_r, _fx_g, _fx_b to _fx_rgb (which is the above peculiar format)
  184.     mov    eax,_fx_g    ; get green value
  185.     sar    eax,11    ; after shift, low order 10 bits are what we want
  186.     jns    rok1
  187.     sub    eax,eax
  188. rok1:    shrd    ecx,eax,10    ; shift green 5i.5f into destination
  189.  
  190.     shr    ecx,2    ; shift in two don't care bits
  191.  
  192.     mov    eax,_fx_b
  193.     sar    eax,11
  194.     jns    bok1
  195.     sub    eax,eax
  196. bok1:    shrd    ecx,eax,10
  197.  
  198.     mov    eax,_fx_r
  199.     sar    eax,11
  200.     jns    gok1
  201.     sub    eax,eax
  202. gok1:    shrd    ecx,eax,10    ; now %ecx is correct!
  203.  
  204. ;**    mov    ecx,_fx_rgb
  205.  
  206.  
  207. _size = (_end1 - _start1)/(num_iters-1)
  208.     mov    eax,num_iters-1
  209.     sub    eax,_loop_count
  210.  jns itsok
  211.  int 3
  212. itsok:
  213.     imul    eax,eax,dword ptr _size
  214.     add    eax,offset _start1
  215. ;**    dec    edi    ; inc edi moved before write to separate instructions
  216.     jmp    eax
  217.  
  218.     align    4
  219. _start1:
  220.  
  221. ; usage:
  222. ;    eax    work
  223. ;    ebx    u coordinate
  224. ;    ecx    rgb (actually rbg, each i5.f5 with 2 bits between blue and green)
  225. ;    edx    work
  226. ;    ebp    v coordinate
  227. ;    esi    pointer to source bitmap
  228. ;    edi    write address
  229.  
  230. ; do all but the last pixel in the unwound loop, last pixel done separately because less work is needed
  231.   rept num_iters-1
  232. ; interpolate the rgb values
  233.     sub    eax,eax
  234.     mov    edx,ecx    ; note, these are in order red, blue, green
  235.     add    ecx,_fx_drgb_dx
  236.     and    edx,11111000001111100000001111100000b    ; preserve ri, bi, gi, strip out fractional portions and 2 extra bits
  237.     shld    eax,edx,15    ; shift in ri, bi
  238.     or    ax,dx    ; mask in gi
  239.  
  240. ; do the regular texture mapper linear interpolation to get the pixel from the source bitmap
  241.     sub    edx,edx
  242.     shld    edx,ebp,6    ; shift in v coordinate
  243.     add    ebp,_fx_dv_dx    ; update v coordinate
  244.     shld    edx,ebx,6    ; shift in u coordinate while shifting up v coordinate
  245.     add    ebx,_fx_du_dx    ; update u coordinate
  246.     mov    ax,[esi+2*edx]    ; get pixel from source bitmap
  247.  
  248. ; get color value based on looked up value from texture map (%al) and light value (%ah)
  249.     mov    al,_pixel_average[eax]    ; get color value based on palette light and color
  250.  
  251. ; write the pixel
  252.     mov    [edi],ax
  253.     add    edi,2
  254.  
  255.   endm
  256.  
  257. _end1:
  258.  
  259. ; now do the leftover pixel
  260.  
  261. ; interpolate the rgb values
  262.     sub    eax,eax
  263.     mov    edx,ecx    ; note, these are in order red, blue, green
  264.     and    edx,11111000001111100000001111100000b    ; preserve ri, bi, gi, strip out fractional portions and 2 extra bits
  265.     shld    eax,edx,15    ; shift in ri, bi
  266.     or    ax,dx    ; mask in gi
  267.  
  268.     mov    ah,_rgb_to_palette[eax]    ; get color value for 15 bit rgb value
  269.  
  270. ; do the regular texture mapper linear interpolation to get the pixel from the source bitmap
  271.     sub    edx,edx
  272.     shld    edx,ebp,6    ; shift in v coordinate
  273.     shld    edx,ebx,6    ; shift in u coordinate while shifting up v coordinate
  274.     mov    al,[esi+edx]    ; get pixel from source bitmap
  275.  
  276. ; get color value based on looked up value from texture map (%al) and light value (%ah)
  277.     mov    al,_pixel_average[eax]    ; get color value based on palette light and color
  278.  
  279. ; write the pixel
  280.     mov    [edi],al
  281.  
  282. _none_to_do:    popa
  283.  
  284.     ret
  285.  
  286. ; -- Code to get rgb 5 bits integer, 5 bits fraction value into 5 bits integer (for each gun)
  287. ; suitable for inverse color lookup
  288. ;**__test:
  289. ;** int 3
  290. ;**;                  rrrrrfffffrrrrrfffffxxbbbbbfffff
  291. ;**    mov    eax,11111001001010101110101101110111b
  292. ;**    and    eax,11111000001111100000001111100000b
  293. ;**    shld    ebx,eax,15
  294. ;**    or    bx,ax
  295.  
  296. _TEXT    ends
  297.  
  298.     end
  299.  
  300.  
  301.