home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / texmap / tmap_ld.asm < prev    next >
Assembly Source File  |  1998-06-08  |  10KB  |  390 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_ld.asm $
  13. ; $Revision: 1.4 $
  14. ; $Author: mike $
  15. ; $Date: 1994/11/30 00:56:53 $
  16. ;
  17. ; Inner loop of rgb lighted dithered linear texture mapper
  18. ;
  19. ; $Log: tmap_ld.asm $
  20. ; Revision 1.4  1994/11/30  00:56:53  mike
  21. ; optimization.
  22. ; Revision 1.3  1994/11/12  16:39:40  mike
  23. ; jae to ja.
  24. ; Revision 1.2  1993/11/22  10:23:58  mike
  25. ; *** empty log message ***
  26. ; Revision 1.1  1993/09/08  17:29:49  mike
  27. ; Initial revision
  28. ;
  29. ;
  30.  
  31. ; Inner loop of rgb lighted dithered linear texture mapper
  32. ; Unlike tmap_rgb.asm, this version does lighting by writing alternate
  33. ; pixels in the light color, and the other colors based on the texture map pixel data.
  34.  
  35.     .386
  36.  
  37.     public    asm_tmap_scanline_lin_ld_
  38.  
  39.     include    tmap_inc.asm
  40.  
  41. _DATA    SEGMENT DWORD PUBLIC USE32 'DATA'
  42.  
  43.     extrn    _fx_u:dword
  44.     extrn    _fx_v:dword
  45.     extrn    _fx_du_dx:dword
  46.     extrn    _fx_dv_dx:dword
  47.     extrn    _fx_y:dword
  48.     extrn    _fx_xleft:dword
  49.     extrn    _fx_xright:dword
  50.  
  51.     extrn    _pixptr:dword
  52.  
  53.     extrn    _x:dword
  54.     extrn    _loop_count:dword
  55.  
  56.     extrn    _reds_16:byte
  57.  
  58.     extrn    _fx_rgb:dword,_fx_drgb_dx:dword
  59.  
  60.     extrn    _fx_r:dword,_fx_g:dword,_fx_b:dword,_fx_dr_dx:dword,_fx_dg_dx:dword,_fx_db_dx:dword
  61.  
  62. _DATA    ENDS
  63.  
  64. DGROUP    GROUP    _DATA
  65.  
  66. _TEXT   SEGMENT PARA PUBLIC USE32 'CODE'
  67.  
  68.     ASSUME    DS:_DATA
  69.     ASSUME    CS:_TEXT
  70.  
  71. ; --------------------------------------------------------------------------------------------------
  72. ; Enter:
  73. ;    _xleft    fixed point left x coordinate
  74. ;    _xright    fixed point right x coordinate
  75. ;    _y    fixed point y coordinate
  76. ;    _pixptr    address of source pixel map
  77. ;    _u    fixed point initial u coordinate
  78. ;    _v    fixed point initial v coordinate
  79. ;    _du_dx    fixed point du/dx
  80. ;    _dv_dx    fixed point dv/dx
  81.  
  82. ;   for (x = (int) xleft; x <= (int) xright; x++) {
  83. ;      _setcolor(read_pixel_from_tmap(srcb,((int) (u/z)) & 63,((int) (v/z)) & 63));
  84. ;      _setpixel(x,y);
  85. ;
  86. ;      u += du_dx;
  87. ;      v += dv_dx;
  88. ;      z += dz_dx;
  89. ;   }
  90.  
  91.     align    4
  92. asm_tmap_scanline_lin_ld_:
  93.     pusha
  94.  
  95. ; Setup for loop:    _loop_count  iterations = (int) xright - (int) xleft
  96. ;    esi    source pixel pointer = pixptr
  97. ;    edi    initial row pointer = y*320+x
  98.  
  99. ; set esi = pointer to start of texture map data
  100.     mov    esi,_pixptr
  101.  
  102. ; set edi = address of first pixel to modify
  103.     mov    edi,_fx_y
  104.  
  105.     cmp    edi,_window_bottom
  106.     ja    _none_to_do
  107.  
  108.     imul    edi,_bytes_per_row
  109.     mov    eax,_fx_xleft
  110.     sar    eax,16
  111.     jns    eax_ok
  112.     sub    eax,eax
  113. eax_ok:
  114.     add    edi,eax
  115.     add    edi,write_buffer
  116.  
  117. ; set _loop_count = # of iterations
  118.     mov    eax,_fx_xright
  119.     sar    eax,16
  120.     cmp    eax,_window_right
  121.     jb    eax_ok1
  122.     mov    eax,_window_right
  123. eax_ok1:    cmp    eax,_window_left
  124.     ja    eax_ok2
  125.     mov    eax,_window_left
  126. eax_ok2:
  127.  
  128.     mov    ebx,_fx_xleft
  129.     sar    ebx,16
  130.     sub    eax,ebx
  131.     js    _none_to_do
  132.     cmp    eax,_window_width
  133.     jbe    _ok_to_do
  134.     mov    eax,_window_width
  135. _ok_to_do:
  136.     mov    _loop_count,eax
  137.  
  138. ;    edi    destination pixel pointer
  139.  
  140.     mov    ebx,_fx_u
  141.     mov    ebp,_fx_v
  142.  
  143.     shl    ebx,10
  144.     shl    ebp,10
  145.  
  146.     shl    _fx_du_dx,10
  147.     shl    _fx_dv_dx,10
  148.  
  149. ; rgb values are passed in the following peculiar, confidential, trade secreted, copyrighted, patented format:
  150. ; [ 5 bits ] [ 5 bits ] [ 5 bits ] [ 5 bits ] [ 2 bits ] [ 5 bits ] [ 5 bits ]
  151. ;   red int   red frac   blue int  blue frac    unused   green int  green frac
  152. ; The reason they are stored in the order red, blue, green is to optimize the process of packing together the three 5 bit
  153. ; values for red, green, blue in the conventional manner, suitable for an inverse table lookup
  154.  
  155. ; convert fixed point values in _fx_dr_dx, _fx_dg_dx, _fx_db_dx to _fx_drgb_dx
  156.     mov    eax,_fx_dg_dx    ; get green value
  157.     sar    eax,11    ; after shift, low order 10 bits are what we want
  158.     jns    dgok1
  159.     inc    eax
  160. dgok1:    shrd    ecx,eax,10    ; shift green 5i.5f into destination
  161.  
  162.     shr    ecx,2    ; shift in two don't care bits
  163.  
  164.     mov    eax,_fx_db_dx
  165.     sar    eax,11
  166.     jns    dbok1
  167.     inc    eax
  168. dbok1:    shrd    ecx,eax,10
  169.  
  170.     mov    eax,_fx_dr_dx
  171.     sar    eax,11
  172.     jns    drok1
  173.     inc    eax
  174. drok1:    shrd    ecx,eax,10    ; now %ecx is correct!
  175.     mov    _fx_drgb_dx,ecx
  176.  
  177. ; convert fixed point values in _fx_r, _fx_g, _fx_b to _fx_rgb (which is the above peculiar format)
  178.     mov    eax,_fx_g    ; get green value
  179.     sar    eax,11    ; after shift, low order 10 bits are what we want
  180.     jns    rok1
  181.     sub    eax,eax
  182. rok1:    shrd    ecx,eax,10    ; shift green 5i.5f into destination
  183.  
  184.     shr    ecx,2    ; shift in two don't care bits
  185.  
  186.     mov    eax,_fx_b
  187.     sar    eax,11
  188.     jns    bok1
  189.     sub    eax,eax
  190. bok1:    shrd    ecx,eax,10
  191.  
  192.     mov    eax,_fx_r
  193.     sar    eax,11
  194.     jns    gok1
  195.     sub    eax,eax
  196. gok1:    shrd    ecx,eax,10    ; now %ecx is correct!
  197.  
  198.  
  199. ; double deltas because we are only writing half as many pixels
  200.     sal    _fx_du_dx,1
  201.     sal    _fx_dv_dx,1
  202.     sal    _fx_drgb_dx,1
  203.  
  204. ; ---------- Check x and y coords to figure whether to write pixel:light or light:pixel ----------
  205.     mov    eax,_fx_xleft    ; get x coordinate
  206.     shr    eax,16    ; preserve only integer portion
  207.     xor    eax,_fx_y    ; get checkerboard status
  208.     shr    eax,1    ; if cy set, then we were odd, else even
  209.     jc    _xy_odd
  210.  
  211. ; even case, write pixel:light
  212.     test    edi,1    ; even align to start
  213.     je    _even1    ; we are already even aligned
  214.  
  215. ; we are going to write to an odd address, so write pixel first
  216.  
  217. ; do the regular texture mapper linear interpolation to get the pixel from the source bitmap
  218.     sub    edx,edx
  219.     shld    edx,ebp,6    ; shift in v coordinate
  220.     add    ebp,_fx_dv_dx    ; update v coordinate
  221.     shld    edx,ebx,6    ; shift in u coordinate while shifting up v coordinate
  222.     add    ebx,_fx_du_dx    ; update u coordinate
  223.     mov    al,[esi+edx]    ; get pixel from source bitmap
  224.  
  225. ; write the pixel
  226.     mov    [edi],al    ; write texture map pixel, then red value
  227.     inc    edi
  228.  
  229.     dec    _loop_count
  230.     js    _none_to_do    ; all done
  231.  
  232.     inc    _loop_count
  233.     shr    _loop_count,1
  234.     je    _odd_do_last_pixel    ; want to write light for last pixel
  235.  
  236.     pushf
  237.     jmp    write_light_pixel_loop
  238.  
  239. _even1:
  240.  
  241. ; now write all the double pixels, light, then pixel
  242.     inc    _loop_count
  243.     shr    _loop_count,1
  244.     je    _even_do_last_pixel
  245.  
  246.  
  247.     pushf
  248. ; usage:
  249. ;    eax    work
  250. ;    ebx    u coordinate
  251. ;    ecx    rgb (actually rbg, each i5.f5 with 2 bits between blue and green)
  252. ;    edx    work
  253. ;    ebp    v coordinate
  254. ;    esi    pointer to source bitmap
  255. ;    edi    write address
  256.  
  257. ; do all but the last pixel in the unwound loop, last pixel done separately because less work is needed
  258. ; interpolate the rgb values
  259. write_pixel_light_loop:
  260. _loop1:    sub    eax,eax
  261.     shld    eax,ecx,4    ; shift in high 4 bits of red gun
  262.     add    ecx,_fx_drgb_dx
  263.  
  264.     mov    ah,_reds_16[eax]    ; get color index for 4 bit red value
  265.  
  266. ; do the regular texture mapper linear interpolation to get the pixel from the source bitmap
  267.     sub    edx,edx
  268.     shld    edx,ebp,6    ; shift in v coordinate
  269.     add    ebp,_fx_dv_dx    ; update v coordinate
  270.     shld    edx,ebx,6    ; shift in u coordinate while shifting up v coordinate
  271.     add    ebx,_fx_du_dx    ; update u coordinate
  272.     mov    al,[esi+edx]    ; get pixel from source bitmap
  273.  
  274. ; write the pixel
  275.     mov    [edi],ax    ; write texture map pixel, then red value
  276.     add    edi,2
  277.  
  278.     dec    _loop_count
  279.     jne    _loop1
  280.  
  281.     popf
  282.     jnc    _none_to_do
  283.  
  284. ; now do the leftover pixel
  285.  
  286. ; do the regular texture mapper linear interpolation to get the pixel from the source bitmap
  287. _even_do_last_pixel:
  288.     sub    edx,edx
  289.     shld    edx,ebp,6    ; shift in v coordinate
  290.     shld    edx,ebx,6    ; shift in u coordinate while shifting up v coordinate
  291.     mov    al,[esi+edx]    ; get pixel from source bitmap
  292.  
  293. ; write the pixel
  294.     mov    [edi],al
  295.  
  296. _none_to_do:    popa
  297.  
  298.     ret
  299.  
  300. _xy_odd:
  301. ; even case, write pixel:light
  302.     test    edi,1    ; even align to start
  303.     je    _even2    ; we are already even aligned
  304.  
  305. ; we are going to write to an odd address, so write light first
  306.  
  307.     sub    eax,eax
  308.     shld    eax,ecx,4    ; shift in high 4 bits of red gun
  309.     add    ecx,_fx_drgb_dx
  310.  
  311.     mov    al,_reds_16[eax]    ; get color index for 4 bit red value
  312.     mov    [edi],al    ; write texture map pixel, then red value
  313.     inc    edi
  314.  
  315.     dec    _loop_count
  316.     js    _done2    ; all done
  317.  
  318. ; now write all the double pixels, light, then pixel
  319.     inc    _loop_count
  320.     shr    _loop_count,1
  321.     je    _even_do_last_pixel
  322.  
  323.     pushf
  324.     jmp    write_pixel_light_loop
  325. _even2:
  326.  
  327. ; now write all the double pixels, light, then pixel
  328.     inc    _loop_count
  329.     shr    _loop_count,1
  330.     je    _odd_do_last_pixel
  331.  
  332.  
  333.     pushf
  334. ; usage:
  335. ;    eax    work
  336. ;    ebx    u coordinate
  337. ;    ecx    rgb (actually rbg, each i5.f5 with 2 bits between blue and green)
  338. ;    edx    work
  339. ;    ebp    v coordinate
  340. ;    esi    pointer to source bitmap
  341. ;    edi    write address
  342.  
  343. ; do all but the last pixel in the unwound loop, last pixel done separately because less work is needed
  344. ; interpolate the rgb values
  345. write_light_pixel_loop:
  346. _loop2:    sub    eax,eax
  347.     shld    eax,ecx,4    ; shift in high 4 bits of red gun
  348.     add    ecx,_fx_drgb_dx
  349.  
  350.     mov    al,_reds_16[eax]    ; get color index for 4 bit red value
  351.  
  352. ; do the regular texture mapper linear interpolation to get the pixel from the source bitmap
  353.     sub    edx,edx
  354.     shld    edx,ebp,6    ; shift in v coordinate
  355.     add    ebp,_fx_dv_dx    ; update v coordinate
  356.     shld    edx,ebx,6    ; shift in u coordinate while shifting up v coordinate
  357.     add    ebx,_fx_du_dx    ; update u coordinate
  358.     mov    ah,[esi+edx]    ; get pixel from source bitmap
  359.  
  360. ; write the pixel
  361.     mov    [edi],ax    ; write texture map pixel, then red value
  362.     add    edi,2
  363.  
  364.     dec    _loop_count
  365.     jne    _loop2
  366.  
  367.     popf
  368.     jnc    _done2
  369.  
  370. ; now do the leftover light pixel
  371. _odd_do_last_pixel:
  372.     sub    eax,eax
  373.     shld    eax,ecx,4    ; shift in high 4 bits of red gun
  374.     mov    al,_reds_16[eax]    ; get color index for 4 bit red value
  375.     mov    [edi],al    ; write texture map pixel, then red value
  376.  
  377. _done2:
  378.     popa
  379.     ret
  380.  
  381. _TEXT    ends
  382.  
  383.     end
  384.  
  385.  
  386.