Subversion Repositories Kolibri OS

Rev

Rev 1776 | 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.         mov     bx,[size_y_var]
  444.         dec     bx
  445.         cmp     ax,bx  ;SIZE_Y
  446.         jge     .tl_quit
  447.  
  448.         mov     ax,.x1
  449.         cmp     ax,.x2
  450.         je      .tl_quit
  451.         jl      .tl_ok
  452.  
  453.         xchg    ax,.x2    ; sort params
  454.         mov     .x1,ax
  455. if Ext >= MMX
  456.         movq    mm0,[.tex_x1]
  457.         movq    mm1,[.tex_x2]
  458.         movq    [.tex_x2],mm0
  459.         movq    [.tex_x1],mm1
  460.  
  461. else
  462.         mov     eax,dword[.tex_x1]
  463.         xchg    eax,dword[.tex_x2]
  464.         mov     dword[.tex_x1],eax
  465.  
  466.         mov     eax,dword[.tex_y1]
  467.         xchg    eax,dword[.tex_y2]
  468.         mov     dword[.tex_y1],eax
  469.  
  470. end if
  471.  
  472.         mov     eax,.z1
  473.         xchg    eax,.z2
  474.         mov     .z1,eax
  475.  
  476.      .tl_ok:
  477.         mov     cx,[size_x_var]
  478.         dec     cx
  479.         cmp     .x1,cx   ;SIZE_X
  480.         jge     .tl_quit
  481.         cmp     .x2,0
  482.         jle     .tl_quit
  483.  
  484.         mov     bx,.x2
  485.         sub     bx,.x1
  486.         movsx   ebx,bx
  487.  
  488.         mov     eax,dword[.tex_y2]       ; calc .dty
  489.         sub     eax,dword[.tex_y1]
  490.         cdq
  491.         idiv    ebx
  492.         push    eax
  493.  
  494.         mov     eax,dword[.tex_x2]       ; calc .dtx
  495.         sub     eax,dword[.tex_x1]
  496.         cdq
  497.         idiv    ebx
  498.         push    eax
  499.  
  500.         mov     eax,.z2       ; calc .dz
  501.         sub     eax,.z1
  502.         cdq
  503.         idiv    ebx
  504.         push    eax
  505.  
  506.         cmp    .x1,0          ; clipping
  507.         jg     @f
  508.  
  509.         movsx   ebx,.x1
  510.         neg     ebx
  511.         imul    ebx           ; eax = .dz * abs(.x1)
  512.         add     .z1,eax
  513.         mov     .x1,0
  514.  
  515.         mov     eax,.tex_dy
  516.         imul    ebx
  517.         add     dword[.tex_y1],eax
  518.  
  519.         mov     eax,.tex_dx
  520.         imul    ebx
  521.         add     dword[.tex_x1],eax
  522.  
  523.       @@:
  524.         cmp     .x2,cx ;SIZE_X
  525.         jl      @f
  526.         mov     .x2,cx  ;SIZE_X
  527.       @@:
  528.  
  529.         movsx   ebx,.y        ; calc mem begin in buffers
  530.         movzx   eax,word[size_x_var]  ;SIZE_X
  531.         mul     ebx
  532.         movsx   ebx,.x1
  533.         add     eax,ebx
  534.         mov     ebx,eax
  535.  
  536.         lea     eax,[eax*3]
  537.         add     edi,eax               ; edi - scr buff
  538.         shl     ebx,2
  539.         add     .z_ptr,ebx            ; z buffer pointer
  540.  
  541.         mov     cx,.x2
  542.         sub     cx,.x1
  543.         movzx   ecx,cx
  544.  
  545. ;if Ext >= MMX
  546. ;        movq    mm0,[.tex_x1]
  547. ;        movq    mm4,mm0
  548. ;        movq    mm1,qword[.tex_dxM]
  549. ;        mov     ebx,.z1
  550. ;        mov     eax,.dz
  551. ;else
  552.         mov     eax,dword[.tex_x1]
  553.         mov     ebx,dword[.tex_y1]
  554.         push    .z1           ; .cz
  555.         push    eax           ;.c_tex_x
  556. ;end if
  557.         mov     edx,.z_ptr
  558.  
  559.      .tl_loop:
  560.  
  561. ;if Ext >= MMX
  562. ;        cmp     ebx,[edx]                       ;  ebx - current z
  563. ;        jge     @f
  564. ;        movq    mm2,mm0
  565. ;        psrad   mm2,ROUND
  566. ;        movq    mm3,mm2
  567. ;        psrlq   mm2,32-TEX_SHIFT
  568. ;        paddd   mm3,mm2
  569. ;        movd    esi,mm3
  570. ;        mov     dword[edx],ebx    ; renew z buffer
  571. ;else
  572.                                                         ;  eax - temp
  573.         mov     eax,.cz                         ;  ebx - cur tex y shl ROUND
  574.         cmp     eax,[edx]                       ;  ecx - l.lenght
  575.         jge     @f         ; ebx - cur tex_y ;  edx - temp
  576.         mov     esi,ebx                 ;  edi - scr buff
  577.         sar     esi,ROUND               ;  esi - tex_ptr temp
  578.         shl     esi,TEX_SHIFT           ;  .z_ptr - cur pointer to z buff
  579.         mov     eax,.c_tex_x            ;  .cz - cur z coord shl CATMULL_SHIFT
  580.         sar     eax,ROUND
  581.         add     esi,eax
  582.         mov     eax,.cz
  583.         mov     dword[edx],eax    ; renew z buffer
  584. ;end if
  585.         and     esi,TEXTURE_SIZE
  586.         lea     esi,[esi*3]
  587.         add     esi,.tex_ptr
  588.         movsd
  589.         dec     edi
  590.         jmp     .no_skip
  591.       @@:
  592.         add     edi,3
  593.      .no_skip:
  594.         add     edx,4
  595. ;if Ext >= MMX
  596. ;        add     ebx,eax
  597. ;        paddd   mm0,mm1
  598. ;else
  599.         mov     eax,.dz
  600.         add     .cz,eax
  601.         mov     eax,.tex_dx
  602.         add     .c_tex_x,eax
  603.         add     ebx,.tex_dy
  604. ;end if
  605.         loop    .tl_loop
  606.  .tl_quit:
  607.  
  608.         mov     esp,ebp
  609.  
  610. ret 30+8
  611.  
  612.