Subversion Repositories Kolibri OS

Rev

Rev 1245 | Blame | Last modification | View Log | Download | RSS feed

  1. ;TEX_X = 512
  2. ;TEX_Y = 512
  3. ;ROUND equ 8
  4. ;SIZE_X = 512
  5. ;SIZE_Y = 512
  6. ;TEX_SHIFT = 9
  7. CATMULL_SHIFT equ 8
  8.  
  9. ;------------------------------------------------------------------------
  10. ;- Procedure drawing textured triangle using Catmull Z-buffer algorithm -
  11. ;------------------------------------------------------------------------
  12. tex_triangle_z:
  13. ;----------in - eax - x1 shl 16 + y1
  14. ;-------------- ebx - x2 shl 16 + y2
  15. ;---------------ecx - x3 shl 16 + y3
  16. ;---------------edx - pointer to Z-buffer
  17. ;---------------esi - pointer to texture buffer
  18. ;---------------edi - pointer to screen buffer
  19. ;-------------stack - texture coordinates
  20. ;------------------ - z coordinates
  21. .tex_x1 equ  ebp+4
  22. .tex_y1 equ  ebp+6
  23. .tex_x2 equ  ebp+8
  24. .tex_y2 equ  ebp+10
  25. .tex_x3 equ  ebp+12
  26. .tex_y3 equ  ebp+14
  27. .z1     equ  word[ebp+16]
  28. .z2     equ  word[ebp+18]
  29. .z3     equ  word[ebp+20]
  30.  
  31. .tex_ptr equ dword[ebp-4]        ; pointer to texture
  32. .z_ptr   equ dword[ebp-8]        ; pointer to z-buffer
  33. .x1      equ word[ebp-10]
  34. .y1      equ word[ebp-12]
  35. .x2      equ word[ebp-14]
  36. .y2      equ word[ebp-16]
  37. .x3      equ word[ebp-18]
  38. .y3      equ word[ebp-20]
  39.  
  40. .dx12     equ dword[ebp-24]
  41. .tex_dx12 equ dword[ebp-28]
  42. .tex_dy12 equ dword[ebp-32]
  43. .dz12     equ dword[ebp-36]
  44.  
  45. .dx13     equ dword[ebp-40]
  46. .tex_dx13 equ dword[ebp-44]
  47. .tex_dy13 equ dword[ebp-48]
  48. .dz13     equ dword[ebp-52]
  49.  
  50. .dx23     equ dword[ebp-56]
  51. .tex_dx23 equ dword[ebp-60]
  52. .tex_dy23 equ dword[ebp-64]
  53. .dz23     equ dword[ebp-68]
  54.  
  55. .scan_x1  equ dword[ebp-72]
  56. .scan_x2  equ dword[ebp-76]
  57. .scan_y1  equ dword[ebp-80]
  58. .scan_y2  equ dword[ebp-84]
  59. .cz1      equ dword[ebp-88]
  60. .cz2      equ dword[ebp-92]
  61.  
  62.         mov     ebp,esp
  63.         push    esi               ; store memory pointers
  64.         push    edx
  65. .tt_sort3:
  66.         cmp     ax,bx                     ;sort all parameters
  67.         jle     .tt_sort1
  68.         xchg    eax,ebx
  69.         mov     edx,dword [.tex_x1]
  70.         xchg    edx,dword [.tex_x2]
  71.         mov     dword[.tex_x1],edx
  72.         mov     dx,.z1
  73.         xchg    dx,.z2
  74.         mov     .z1,dx
  75. .tt_sort1:
  76.         cmp     bx,cx
  77.         jle     .tt_sort2
  78.         xchg    ebx,ecx
  79.         mov     edx,dword [.tex_x2]
  80.         xchg    edx,dword [.tex_x3]
  81.         mov     dword [.tex_x2],edx
  82.         mov     dx,.z2
  83.         xchg    dx,.z3
  84.         mov     .z2,dx
  85.         jmp     .tt_sort3
  86. .tt_sort2:
  87.  
  88.         push    eax     ; and store to user friendly variables
  89.         push    ebx
  90.         push    ecx
  91.  
  92.         mov      edx,80008000h  ; eax,ebx,ecx are ANDd together into edx which means that
  93.         and      edx,ebx        ; if *all* of them are negative a sign flag is raised
  94.         and      edx,ecx
  95.         and      edx,eax
  96.         test     edx,80008000h  ; Check both X&Y at once
  97.         jne      .tt_loop2_end
  98.         cmp      ax,SIZE_Y
  99.         jl       @f
  100.         cmp      bx,SIZE_Y
  101.         jl       @f
  102.         cmp      cx,SIZE_Y
  103.         jl       @f
  104.         ror      eax,16
  105.         ror      ebx,16
  106.         ror      ecx,16
  107.         cmp      ax,SIZE_X
  108.         jl       @f
  109.         cmp      bx,SIZE_X
  110.         jl       @f
  111.         cmp      cx,SIZE_X
  112.         jl       @f
  113.         jmp      .tt_loop2_end
  114.      @@:
  115.         mov      eax,dword[.tex_x1] ; texture coords must be in [0..TEX_X(Y)]
  116.         mov      ebx,dword[.tex_x2]
  117.         mov      ecx,dword[.tex_x3]
  118.         mov      edx,eax
  119.         or       edx,ebx
  120.         or       edx,ecx
  121.         test     edx,80008000h
  122.         jne      .tt_loop2_end
  123.         cmp      ax,TEX_X
  124.         jge      .tt_loop2_end
  125.         cmp      bx,TEX_X
  126.         jge      .tt_loop2_end
  127.         cmp      cx,TEX_X
  128.         jge      .tt_loop2_end
  129.         ror      eax,16
  130.         ror      ebx,16
  131.         ror      ecx,16
  132.         cmp      ax,TEX_Y
  133.         jge      .tt_loop2_end
  134.         cmp      bx,TEX_Y
  135.         jge      .tt_loop2_end
  136.         cmp      cx,TEX_Y
  137.         jge      .tt_loop2_end
  138.  
  139.  
  140.         movsx    ebx,.y2    ; calc delta
  141.         sub      bx,.y1
  142.         jnz      .tt_dx12_make
  143.         xor      edx,edx
  144.         mov      ecx,4
  145.     @@:
  146.         push     edx
  147.         loop     @b
  148.         jmp      .tt_dx12_done
  149.     .tt_dx12_make:
  150.         mov      ax,.x2
  151.         sub      ax,.x1
  152.         cwde
  153.         shl      eax,ROUND
  154.         cdq
  155.         idiv     ebx
  156.        ; mov      .dx12,eax                      ; dx12 = (x2-x1)/(y2-y1)
  157.         push     eax
  158.  
  159.         mov      ax,word[.tex_x2]
  160.         sub      ax,word[.tex_x1]
  161.         cwde
  162.         shl      eax,ROUND
  163.         cdq
  164.         idiv     ebx
  165.        ; mov     [.tex_dx12],eax               ; tex_dx12 = (tex_x2-tex_x1)/(y2-y1)
  166.         push     eax
  167.  
  168.         mov      ax,word[.tex_y2]
  169.         sub      ax,word[.tex_y1]
  170.         cwde
  171.         shl      eax,ROUND
  172.         cdq
  173.         idiv     ebx
  174.      ;   mov    [.tex_dy12],eax              ; tex_dy12 = (tex_y2-tex_y1)/(y2-y1)
  175.         push     eax
  176.  
  177.         mov      ax,.z2
  178.         sub      ax,.z1
  179.         cwde
  180.         shl      eax,CATMULL_SHIFT
  181.         cdq
  182.         idiv     ebx
  183.         push     eax
  184.    .tt_dx12_done:
  185.  
  186.         movsx    ebx,.y3    ; calc delta
  187.         sub      bx,.y1
  188.         jnz      .tt_dx13_make
  189.         xor      edx,edx
  190.         mov      ecx,4
  191.     @@:
  192.         push     edx
  193.         loop     @b
  194.         jmp      .tt_dx13_done
  195.     .tt_dx13_make:
  196.         mov      ax,.x3
  197.         sub      ax,.x1
  198.         cwde
  199.         shl      eax,ROUND
  200.         cdq
  201.         idiv     ebx
  202.        ; mov      .dx12,eax                      ; dx13 = (x3-x1)/(y3-y1)
  203.         push      eax
  204.  
  205.         mov      ax,word[.tex_x3]
  206.         sub      ax,word[.tex_x1]
  207.         cwde
  208.         shl      eax,ROUND
  209.         cdq
  210.         idiv     ebx
  211.        ; mov     [.tex_dx12],eax               ; tex_dx13 = (tex_x3-tex_x1)/(y3-y1)
  212.         push     eax
  213.  
  214.         mov      ax,word[.tex_y3]
  215.         sub      ax,word[.tex_y1]
  216.         cwde
  217.         shl      eax,ROUND
  218.         cdq
  219.         idiv     ebx
  220.      ;   mov    [.tex_dy12],eax              ; tex_dy13 = (tex_y3-tex_y1)/(y3-y1)
  221.         push     eax
  222.  
  223.         mov      ax,.z3
  224.         sub      ax,.z1
  225.         cwde
  226.         shl      eax,CATMULL_SHIFT
  227.         cdq
  228.         idiv     ebx
  229.         push     eax
  230.    .tt_dx13_done:
  231.  
  232.         mov      bx,.y3    ; calc delta
  233.         sub      bx,.y2
  234.         jnz      .tt_dx23_make
  235.         xor      edx,edx
  236.         mov      ecx,4
  237.     @@:
  238.         push     edx
  239.         loop     @b
  240.         jmp      .tt_dx23_done
  241.     .tt_dx23_make:
  242.         mov      ax,.x3
  243.         sub      ax,.x2
  244.         cwde
  245.         shl      eax,ROUND
  246.         cdq
  247.         movzx    ebx,bx
  248.         idiv     ebx
  249.        ; mov      .dx23,eax                      ; dx23 = (x3-x2)/(y3-y2)
  250.         push      eax
  251.  
  252.         mov      ax,word[.tex_x3]
  253.         sub      ax,word[.tex_x2]
  254.         cwde
  255.         shl      eax,ROUND
  256.         cdq
  257.         idiv     ebx
  258.        ; mov     [.tex_dx23],eax               ; tex_dx23 = (tex_x3-tex_x2)/(y3-y2)
  259.         push     eax
  260.  
  261.         mov      ax,word[.tex_y3]
  262.         sub      ax,word[.tex_y2]
  263.         cwde
  264.         shl      eax,ROUND
  265.         cdq
  266.         idiv     ebx
  267.      ;   mov    [.tex_dy23],eax              ; tex_dy23 = (tex_y3-tex_y2)/(y3-y2)
  268.         push     eax
  269.  
  270.         mov      ax,.z3
  271.         sub      ax,.z2
  272.         cwde
  273.         shl      eax,CATMULL_SHIFT
  274.         cdq
  275.         idiv     ebx
  276.         push     eax
  277.    .tt_dx23_done:
  278.  
  279.         movsx    eax,.x1     ;eax - cur x1
  280.         shl      eax,ROUND   ;ebx - cur x2
  281.         mov      ebx,eax
  282.  
  283.         movsx    edx, word[.tex_x1]
  284.         shl      edx,ROUND
  285.       ;  mov [.scan_x1],edx
  286.       ;  mov [.scan_x2],edx
  287.         push     edx
  288.         push     edx
  289.         movsx    edx, word[.tex_y1]
  290.         shl      edx,ROUND
  291.        ; mov [.scan_y1],edx
  292.        ; mov [.scan_y2],edx
  293.         push     edx
  294.         push     edx
  295.         movsx    edx,.z1
  296.         shl      edx,CATMULL_SHIFT
  297.         push     edx
  298.         push     edx
  299.         mov      cx,.y1
  300.         cmp      cx,.y2
  301.         jge      .tt_loop1_end
  302.  
  303.    .tt_loop1:
  304.         pushad
  305.  
  306.         push    .z_ptr
  307.         push    .cz1      ; z coords shifted shl catmull_shift
  308.         push    .cz2
  309.         push    .scan_y2
  310.         push    .scan_x2
  311.         push    .scan_y1
  312.         push    .scan_x1
  313.         push    esi               ;[.tex_ptr]
  314.  
  315.         push    cx
  316.         sar     ebx,ROUND
  317.         push    bx
  318.         sar     eax,ROUND
  319.         push    ax
  320.         call    textured_line_z
  321.  
  322.         popad
  323.         mov     edx,.dz13
  324.         add     .cz1,edx
  325.         mov     edx,.dz12
  326.         add     .cz2,edx
  327.  
  328.         mov     edx, .tex_dx13
  329.         add     .scan_x1, edx
  330.         mov     edx, .tex_dx12
  331.         add     .scan_x2, edx
  332.         mov     edx, .tex_dy13
  333.         add     .scan_y1, edx
  334.         mov     edx, .tex_dy12
  335.         add     .scan_y2, edx
  336.  
  337.         add     eax, .dx13
  338.         add     ebx, .dx12
  339.         inc     cx
  340.         cmp     cx,.y2
  341.         jl      .tt_loop1
  342.  
  343.   .tt_loop1_end:
  344.  
  345.  
  346.         mov     cx,.y2
  347.         cmp     cx,.y3
  348.         jge     .tt_loop2_end
  349.  
  350.         movsx   ebx,.x2
  351.         shl     ebx,ROUND
  352.         movsx   edx,.z2
  353.         shl     edx,CATMULL_SHIFT
  354.         mov     .cz2,edx
  355.         movzx   edx, word [.tex_x2]
  356.         shl     edx,ROUND
  357.         mov     .scan_x2,edx
  358.         movzx   edx, word[.tex_y2]
  359.         shl     edx,ROUND
  360.         mov     .scan_y2,edx
  361.  
  362. .tt_loop2:
  363.  
  364.         pushad
  365.  
  366.         push    .z_ptr
  367.         push    .cz1      ; z coords shifted shl catmull_shift
  368.         push    .cz2
  369.  
  370.         push    .scan_y2
  371.         push    .scan_x2
  372.         push    .scan_y1
  373.         push    .scan_x1
  374.         push    esi               ;[.tex_ptr]
  375.  
  376.         push    cx
  377.         sar     ebx,ROUND
  378.         push    bx
  379.         sar     eax,ROUND
  380.         push    ax
  381.         call    textured_line_z
  382.  
  383.         popad
  384.  
  385.  
  386.         mov     edx,.dz13
  387.         add     .cz1,edx
  388.         mov     edx,.dz23
  389.         add     .cz2,edx
  390.  
  391.         mov     edx, .tex_dx13
  392.         add     .scan_x1, edx
  393.         mov     edx, .tex_dx23
  394.         add     .scan_x2, edx
  395.         mov     edx, .tex_dy13
  396.         add     .scan_y1, edx
  397.         mov     edx, .tex_dy23
  398.         add     .scan_y2, edx
  399.  
  400.         add     eax, .dx13
  401.         add     ebx, .dx23
  402.         inc     cx
  403.         cmp     cx,.y3
  404.         jl      .tt_loop2
  405.  
  406. .tt_loop2_end:
  407.  
  408. .tt_end:
  409.         mov esp,ebp
  410. ret 18
  411.  
  412. textured_line_z:
  413. ;-----in -edi screen buffer pointer
  414. ;------------ stack:
  415.   .x1 equ word [ebp+4]
  416.   .x2 equ word [ebp+6]
  417.   .y  equ word [ebp+8]
  418.  
  419.   .tex_ptr equ dword [ebp+10]
  420.   .tex_x1  equ ebp+14
  421.   .tex_y1  equ ebp+18
  422.   .tex_x2  equ ebp+22
  423.   .tex_y2  equ ebp+26
  424.   .z2      equ dword [ebp+30]     ;z1, z2  coords shifted shl CATMULL_SHIFT
  425.   .z1      equ dword [ebp+34]
  426.   .z_ptr   equ dword [ebp+38]
  427.  
  428.   .tex_dy  equ dword [ebp-4]
  429.   .tex_dx  equ dword [ebp-8]
  430.   .dz      equ dword [ebp-12]
  431.   .cz      equ dword [ebp-16]
  432.   .c_tex_x equ dword [ebp-20]  ; current tex x
  433.   .m_sft1 equ ebp-28
  434.   .m_sft2 equ ebp-32
  435. ;  .c_tex_xM equ ebp+14
  436.   .tex_dxM  equ ebp-8
  437.  
  438.         mov     ebp,esp
  439.  
  440.         mov     ax,.y
  441.         or      ax,ax
  442.         jl      .tl_quit
  443.         cmp     ax,SIZE_Y
  444.         jge     .tl_quit
  445.  
  446.         mov     ax,.x1
  447.         cmp     ax,.x2
  448.         je      .tl_quit
  449.         jl      .tl_ok
  450.  
  451.         xchg    ax,.x2    ; sort params
  452.         mov     .x1,ax
  453. if Ext >= MMX
  454.         movq    mm0,[.tex_x1]
  455.         movq    mm1,[.tex_x2]
  456.         movq    [.tex_x2],mm0
  457.         movq    [.tex_x1],mm1
  458.  
  459. else
  460.         mov     eax,dword[.tex_x1]
  461.         xchg    eax,dword[.tex_x2]
  462.         mov     dword[.tex_x1],eax
  463.  
  464.         mov     eax,dword[.tex_y1]
  465.         xchg    eax,dword[.tex_y2]
  466.         mov     dword[.tex_y1],eax
  467.  
  468. end if
  469.  
  470.         mov     eax,.z1
  471.         xchg    eax,.z2
  472.         mov     .z1,eax
  473.  
  474.      .tl_ok:
  475.         cmp     .x1,SIZE_X
  476.         jge     .tl_quit
  477.         cmp     .x2,0
  478.         jle     .tl_quit
  479.  
  480.         mov     bx,.x2
  481.         sub     bx,.x1
  482.         movsx   ebx,bx
  483.  
  484.         mov     eax,dword[.tex_y2]       ; calc .dty
  485.         sub     eax,dword[.tex_y1]
  486.         cdq
  487.         idiv    ebx
  488.         push    eax
  489.  
  490.         mov     eax,dword[.tex_x2]       ; calc .dtx
  491.         sub     eax,dword[.tex_x1]
  492.         cdq
  493.         idiv    ebx
  494.         push    eax
  495.  
  496.         mov     eax,.z2       ; calc .dz
  497.         sub     eax,.z1
  498.         cdq
  499.         idiv    ebx
  500.         push    eax
  501.  
  502.         cmp    .x1,0          ; clipping
  503.         jg     @f
  504.  
  505.         movsx   ebx,.x1
  506.         neg     ebx
  507.         imul    ebx           ; eax = .dz * abs(.x1)
  508.         add     .z1,eax
  509.         mov     .x1,0
  510.  
  511.         mov     eax,.tex_dy
  512.         imul    ebx
  513.         add     dword[.tex_y1],eax
  514.  
  515.         mov     eax,.tex_dx
  516.         imul    ebx
  517.         add     dword[.tex_x1],eax
  518.  
  519.       @@:
  520.         cmp     .x2,SIZE_X
  521.         jl      @f
  522.         mov     .x2,SIZE_X
  523.       @@:
  524.  
  525.         movsx   ebx,.y        ; calc mem begin in buffers
  526.         mov     eax,SIZE_X
  527.         mul     ebx
  528.         movsx   ebx,.x1
  529.         add     eax,ebx
  530.         mov     ebx,eax
  531.  
  532.         lea     eax,[eax*3]
  533.         add     edi,eax               ; edi - scr buff
  534.         shl     ebx,2
  535.         add     .z_ptr,ebx            ; z buffer pointer
  536.  
  537.         mov     cx,.x2
  538.         sub     cx,.x1
  539.         movzx   ecx,cx
  540.  
  541. ;if Ext >= MMX
  542. ;        movq    mm0,[.tex_x1]
  543. ;        movq    mm4,mm0
  544. ;        movq    mm1,qword[.tex_dxM]
  545. ;        mov     ebx,.z1
  546. ;        mov     eax,.dz
  547. ;else
  548.         mov     eax,dword[.tex_x1]
  549.         mov     ebx,dword[.tex_y1]
  550.         push    .z1           ; .cz
  551.         push    eax           ;.c_tex_x
  552. ;end if
  553.         mov     edx,.z_ptr
  554.  
  555.      .tl_loop:
  556.  
  557. ;if Ext >= MMX
  558. ;        cmp     ebx,[edx]                       ;  ebx - current z
  559. ;        jge     @f
  560. ;        movq    mm2,mm0
  561. ;        psrad   mm2,ROUND
  562. ;        movq    mm3,mm2
  563. ;        psrlq   mm2,32-TEX_SHIFT
  564. ;        paddd   mm3,mm2
  565. ;        movd    esi,mm3
  566. ;        mov     dword[edx],ebx    ; renew z buffer
  567. ;else
  568.                                                         ;  eax - temp
  569.         mov     eax,.cz                         ;  ebx - cur tex y shl ROUND
  570.         cmp     eax,[edx]                       ;  ecx - l.lenght
  571.         jge     @f         ; ebx - cur tex_y ;  edx - temp
  572.         mov     esi,ebx                 ;  edi - scr buff
  573.         sar     esi,ROUND               ;  esi - tex_ptr temp
  574.         shl     esi,TEX_SHIFT           ;  .z_ptr - cur pointer to z buff
  575.         mov     eax,.c_tex_x            ;  .cz - cur z coord shl CATMULL_SHIFT
  576.         sar     eax,ROUND
  577.         add     esi,eax
  578.         mov     eax,.cz
  579.         mov     dword[edx],eax    ; renew z buffer
  580. ;end if
  581.         and     esi,TEXTURE_SIZE
  582.         lea     esi,[esi*3]
  583.         add     esi,.tex_ptr
  584.         movsd
  585.         dec     edi
  586.         jmp     .no_skip
  587.       @@:
  588.         add     edi,3
  589.      .no_skip:
  590.         add     edx,4
  591. ;if Ext >= MMX
  592. ;        add     ebx,eax
  593. ;        paddd   mm0,mm1
  594. ;else
  595.         mov     eax,.dz
  596.         add     .cz,eax
  597.         mov     eax,.tex_dx
  598.         add     .c_tex_x,eax
  599.         add     ebx,.tex_dy
  600. ;end if
  601.         loop    .tl_loop
  602.  .tl_quit:
  603.  
  604.         mov     esp,ebp
  605.  
  606. ret 30+8
  607.  
  608.