Subversion Repositories Kolibri OS

Rev

Rev 6388 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. MAX_SPHERES = 4 ;8
  2. MAX_LIGHTS = 6
  3.  
  4. main_loop:
  5.   call normalize_surface
  6.   xor  eax,eax       ; y
  7. .next_line:
  8.   xor  ebx,ebx       ; x
  9.  
  10. @@:
  11.   push eax
  12.   push ebx
  13.  
  14.  
  15.  
  16.   call find_intersection
  17.   pop  ebx
  18.   pop  eax
  19.   add  ebx,1
  20.   cmp  ebx,XRES
  21.   jnz  @b
  22.  
  23.   add  eax,1
  24.   cmp  eax,YRES
  25.   jnz  .next_line
  26.  
  27. ret
  28.  
  29. normalize_surface:
  30.   movss   xmm0,[surface+8]
  31.   movlhps xmm0,xmm0
  32.   movlps  xmm0,[surface]
  33.   movaps  xmm1,xmm0
  34.   mulps   xmm0,xmm0
  35.   haddps  xmm0,xmm0
  36.   haddps  xmm0,xmm0
  37.   sqrtss  xmm0,xmm0
  38.   shufps  xmm0,xmm0,0
  39.   divps   xmm1,xmm0
  40.   movaps  xmm2,xmm1
  41.   pslldq  xmm2,4
  42.   psrldq  xmm2,4
  43.   movaps  [surface_without_d],xmm2
  44.   movlps  [surface],xmm1
  45.   movhlps xmm1,xmm1
  46.   movss   [surface+8],xmm1
  47. ret
  48.  
  49. find_intersection:
  50. ;   eax - y
  51. ;   ebx - x
  52.    push ebp
  53.    mov  ebp,esp
  54.    sub  esp,256
  55.    and  ebp,0xfffffff0
  56.    .dz    equ  dword[ebp-8]
  57.    .dy    equ  dword[ebp-12]
  58.    .dx    equ       [ebp-16]
  59.    .a     equ  dword[ebp-20]
  60.    .b     equ  dword[ebp-24]
  61.    .c     equ  dword[ebp-28]
  62.    .delta equ  dword[ebp-32]
  63.    .iy    equ  dword[ebp-36]
  64.    .ix    equ       [ebp-40]
  65.    .t1    equ       [ebp-44]
  66.    .t2    equ       [ebp-48]
  67.    .n     equ       [ebp-64]
  68.    .sph_xyz       equ       [ebp-80]
  69.    .one_write     equ byte  [ebp-81] ;tells if sth written in 'nearest' data
  70.    .sph_counter   equ dword [ebp-85]
  71.    .dx_sh         equ       [ebp-96]
  72.    .a_sh          equ       [ebp-100]
  73.    .b_sh          equ       [ebp-104]
  74.    .c_sh          equ       [ebp-108]
  75.    .delta_sh      equ       [ebp-112]
  76.    .sph_counter_sh equ dword[ebp-116]
  77.    .shadow_mark   equ dword [ebp-120]
  78.    .nearest_surface equ     [ebp-144]
  79.    .vd            equ       [ebp-148]   ; denominator in plane inters. equation
  80.  
  81.    mov      .iy,eax
  82.    mov      .ix,ebx
  83.  
  84.    mov      .one_write,0
  85.    mov      .shadow_mark,0
  86.    xorps    xmm0,xmm0
  87.    cvtpi2ps xmm0,.ix
  88.    mov      ecx,XRES
  89.    cvtsi2ss xmm2,ecx
  90.    shufps   xmm2,xmm2,0
  91.    divps    xmm0,xmm2
  92.    subps    xmm0,[camera]
  93.    movaps   .dx,xmm0
  94.    movaps   xmm1,xmm0
  95.    mulps    xmm1,xmm0
  96.    haddps   xmm1,xmm1
  97.    haddps   xmm1,xmm1
  98.    movss    .a,xmm1
  99.    mov      .sph_counter,0
  100.  .next_sph:                   ; intersection with sphere
  101.    movaps   xmm5,[camera]
  102.    mov      edx,.sph_counter
  103.    shl      edx,4
  104.    add      edx,sphere
  105.    subps    xmm5,[edx]
  106.    mulps    xmm5,[float2]
  107.    mulps    xmm5,.dx
  108.    haddps   xmm5,xmm5
  109.    haddps   xmm5,xmm5
  110.    movss    .b,xmm5
  111.  
  112.  
  113.    movaps    xmm4,[edx]
  114.    mulps     xmm4,xmm4
  115.    movaps    xmm5,[camera]
  116.    mulps     xmm5,xmm5
  117.    addps     xmm4,xmm5
  118.    haddps    xmm4,xmm4
  119.    haddps    xmm4,xmm4
  120.    movaps    xmm5,[edx]  ;;[sphere]  ;; [edx]
  121.    mulps     xmm5,[camera]
  122.    haddps    xmm5,xmm5
  123.    haddps    xmm5,xmm5
  124.    mulss     xmm5,[float2]
  125.    subss     xmm4,xmm5
  126.    mov       ebx,.sph_counter
  127.    shl       ebx,2
  128.    add       ebx,sph_radius
  129.    movss     xmm5,[ebx]  ;[R]  ; [ebx]
  130.    mulss     xmm5,xmm5
  131.    subss     xmm4,xmm5
  132.    movss     .c,xmm4
  133.  
  134.    movss     xmm5,.b
  135.    mulss     xmm5,xmm5
  136.    mulss     xmm4,.a
  137.    mulss     xmm4,[float4]
  138.    subss     xmm5,xmm4
  139.    movss     .delta,xmm5
  140.    xorps     xmm6,xmm6
  141.    cmpnltss  xmm5,xmm6
  142.    movd      ecx,xmm5  ; ecx = -1 => greater than 0.0
  143.    cmp       ecx,0
  144.    jnz       @f
  145.    jmp       .next_s   ; no intersection
  146.  
  147.  
  148.   @@:
  149.    movss     xmm5,.delta
  150.    sqrtss    xmm5,xmm5
  151.    movss     xmm4,xmm5
  152.    subss     xmm6,.b
  153.    movss     xmm7,xmm6
  154.    subss     xmm6,xmm5
  155.    divss     xmm6,[float2]
  156.    divss     xmm6,.a
  157.    movss     .t1,xmm6
  158.    addss     xmm4,xmm7
  159.    divss     xmm4,[float2]
  160.    divss     xmm4,.a
  161.    movss     .t2,xmm4
  162.    maxss     xmm6,xmm4
  163.    cmp       .one_write,0   ; test if sth in 'nearest' data is written
  164.    jz        @f
  165.    movss     xmm4,xmm6  ;5
  166.    cmpnltss  xmm4,[smalest_t]
  167.    movd      ecx,xmm4
  168.    or        ecx,ecx
  169.    jz       .next_s
  170.  @@:
  171.    movss     [smalest_t],xmm6  ;5
  172.    movaps    xmm0,[edx]
  173.    movaps    [nearest_sphere],xmm0
  174.    push      dword[ebx]
  175.    pop       dword[nearest_radius]
  176.    mov       .one_write,1        ; one_write - object index -> 1 = sphere
  177.  .next_s:
  178.   add       .sph_counter,1
  179.   cmp       .sph_counter,MAX_SPHERES
  180.   jnz       .next_sph
  181.  
  182. if 1
  183.   movaps    xmm0,[surface_without_d]  ; find with plane intersection
  184.   mulps     xmm0,[camera]             ; only one surface is computed
  185.   haddps    xmm0,xmm0
  186.   haddps    xmm0,xmm0
  187.   addss     xmm0,[surface+12]
  188.   movaps    xmm1,[surface_without_d]
  189.   mulps     xmm1,.dx
  190.   haddps    xmm1,xmm1
  191.   haddps    xmm1,xmm1
  192.   xorps     xmm2,xmm2
  193.   cmpnless  xmm2,xmm1
  194.   movd      ecx,xmm2
  195.   cmp       ecx,0  ;-1
  196.   je        .put_pixel ; denominator equal 'zero' - no intersection
  197.   xorps     xmm2,xmm2  ; denominator > 0 -> inters. not in screen area
  198.   movss     .vd,xmm1   ; write to memory this denom.
  199.   divss     xmm0,xmm1
  200.   subss     xmm2,xmm0
  201.   cmp       .one_write,0
  202.   jz        @f
  203.   movss     xmm0,xmm2
  204.   cmpnltss  xmm2,[smalest_t]
  205.   movd      ecx,xmm2
  206.   cmp       ecx,0
  207.   je        .put_pixel
  208.  @@:
  209.   movss     [smalest_t],xmm0
  210. ;  test      [smalest_t],0x80000000
  211. ;  jz        @f
  212. ;  and       [smalest_t],0x7fffffff
  213. ; @@:
  214.   movaps    xmm2,[surface]
  215.   movaps    .nearest_surface,xmm2
  216.   mov       .one_write,2      ; nearest object -> 2 = flat plane
  217.  
  218. end if
  219.  
  220.  .put_pixel:
  221.    cmp       .one_write,0  ; end if no intersection
  222.    je        .end
  223.  
  224.    movss     xmm5,[smalest_t]
  225.    shufps    xmm5,xmm5,0   ; calc and put pixel
  226.    movaps    xmm6,.dx
  227.    mulps     xmm6,xmm5
  228.    movaps    xmm4,[camera]
  229.    addps     xmm4,xmm6  ; xmm4 - x,y,z on the sphere or on surface
  230.    movaps    .sph_xyz,xmm4
  231. if 1
  232.    cmp       .one_write,2
  233.    jne       .shadow
  234. ;   movaps    xmm4,.sph_xyz
  235.    movaps    xmm7,xmm4
  236.    lea       ebx,.nearest_surface
  237.    movss     xmm4,[ebx+8]
  238.    movlhps   xmm4,xmm4
  239.    movlps    xmm4,[ebx]   ; xmm4 - normal to surface vector
  240.    test      dword .vd,0x80000000
  241.    jz        @f
  242.    andps     xmm4,[positive_mask]  ;0x7fffffff
  243.  @@:
  244.  
  245.    jmp       .calc_pix
  246.  
  247. end if
  248.  .shadow:
  249. if 1
  250. ; to find shadow intersect:
  251. ; P0 - point on sphere
  252. ; P1 - light
  253. ; with every other sphere in scene if any intersection occured -
  254. ;    - point is in shadow
  255.  ; next_sph_shad:
  256.    mov      ecx,MAX_LIGHTS
  257.  .next_light_sh:
  258.    push     ecx
  259.    shl      ecx,4
  260.    movaps   xmm0,[ecx+light]   ;xmm4  - point on nearest sphere
  261.    subps    xmm0,xmm4
  262.    movaps   .dx_sh,xmm0
  263.  
  264.    mulps    xmm0,xmm0
  265.    haddps   xmm0,xmm0
  266.    haddps   xmm0,xmm0
  267.    movss    .a_sh,xmm0
  268.  
  269.    mov      .sph_counter_sh,0
  270.  .next_sph_sh:      ; be sure you not intersect nearest sphere with itself
  271.    movaps   xmm5,.sph_xyz  ;[light]
  272.    mov      edx,.sph_counter_sh
  273.    shl      edx,4
  274.    add      edx,sphere
  275.    movaps   xmm7,[edx]
  276.    cmpeqps  xmm7,[nearest_sphere]
  277.    movmskps ecx,xmm7
  278.    and      ecx,0111b
  279.    cmp      ecx,0
  280.    jne       .next_s_sh
  281.  
  282.    subps    xmm5,[edx] ; [edx] - cur sph
  283.    mulps    xmm5,[float2]
  284.    mulps    xmm5,.dx_sh
  285.    haddps   xmm5,xmm5
  286.    haddps   xmm5,xmm5
  287.    movss    .b_sh,xmm5
  288.  
  289.    movaps    xmm4,[edx]
  290.    mulps     xmm4,xmm4
  291.    movaps    xmm5,.sph_xyz
  292.    mulps     xmm5,xmm5
  293.    addps     xmm4,xmm5
  294.    haddps    xmm4,xmm4
  295.    haddps    xmm4,xmm4
  296.    movaps    xmm5,.sph_xyz
  297.    mulps     xmm5,[edx]
  298.    haddps    xmm5,xmm5
  299.    haddps    xmm5,xmm5
  300.    mulss     xmm5,[float2]
  301.    subss     xmm4,xmm5
  302.    mov       ebx,.sph_counter_sh
  303.    shl       ebx,2
  304.    add       ebx,sph_radius
  305.    movss     xmm5,[ebx]
  306.    mulss     xmm5,xmm5
  307.    subss     xmm4,xmm5
  308.    movss     .c_sh,xmm4
  309.  
  310.    movss     xmm5,.b_sh
  311.    mulss     xmm5,xmm5
  312.    mulss     xmm4,.a_sh
  313.    mulss     xmm4,[float4]
  314.    subss     xmm5,xmm4
  315.    movss     .delta_sh,xmm5
  316.    xorps     xmm6,xmm6
  317.    cmpnltss  xmm5,xmm6
  318.    movd      ecx,xmm5  ; ecx = -1 greater than 0.0
  319.    cmp       ecx,0
  320.    jnz       @f
  321.    jmp       .next_s_sh   ; no intersection
  322.   @@:
  323.    add       .shadow_mark,1  ; mark ->point in shadow
  324.    pop       ecx
  325.    sub       ecx,1
  326.    jnz      .next_light_sh
  327.    jmp       .put_pix
  328.   .next_s_sh:
  329.    add       .sph_counter_sh,1
  330.    cmp       .sph_counter_sh,MAX_SPHERES
  331.    jnz       .next_sph_sh
  332.    pop       ecx
  333.    sub       ecx,1
  334.    jnz       .next_light_sh
  335.  
  336. end if
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  .put_pix:
  344.    movaps    xmm4,.sph_xyz
  345.    movaps    xmm7,xmm4
  346.    subps     xmm4,[nearest_sphere]
  347.  
  348.    movss     xmm0,[nearest_radius]
  349.    shufps    xmm0,xmm0,0
  350.    divps     xmm4,xmm0  ; xmm4 - normal to surface vector
  351.  .calc_pix:  ; normal computed
  352.    movaps    xmm1,xmm4  ; copy of normal in xmm1
  353.    xor       eax,eax
  354.    xorps     xmm3,xmm3
  355.  ;  movss     xmm2,[light_factor] ; other model of lighting
  356.  ;  shufps    xmm2,xmm2,0
  357.  
  358.  .next_light:
  359.    mov       ebx,eax
  360.    shl       ebx,4
  361.    movaps    xmm5,[light+ebx]
  362.    subps     xmm5,xmm7  ; calc light unit vector
  363.    movaps    xmm6,xmm5
  364.    mulps     xmm5,xmm5
  365.    haddps    xmm5,xmm5
  366.    haddps    xmm5,xmm5
  367.    sqrtss    xmm5,xmm5
  368.    shufps    xmm5,xmm5,0
  369.    divps     xmm6,xmm5  ; xmm6 - normalized light vector
  370.   ; dot_product
  371.    movaps    xmm4,xmm1  ; xmm4 - normal to surface
  372.    mulps     xmm4,xmm6
  373.    haddps    xmm4,xmm4
  374.    haddps    xmm4,xmm4
  375.    shufps    xmm4,xmm4,0
  376.    mulps     xmm4,[lights_color+ebx]  ; xmm4 - computed col. light vector dep.
  377.  
  378. ;   mulps     xmm4,xmm2  ; other model of lighting
  379. ;   addps     xmm3,xmm4
  380.  
  381.    maxps     xmm3,xmm4 ; will be this better ?
  382.  
  383.    add       eax,1
  384.    cmp       eax,MAX_LIGHTS
  385.    jnz       .next_light
  386.  
  387.    minps     xmm3,[float255]
  388.    cmp       .shadow_mark,0
  389.    je        @f
  390.    cvtsi2ss  xmm2,.shadow_mark
  391.    shufps    xmm2,xmm2,0
  392.    mulps     xmm2,[shadow_factor]
  393.    subps     xmm3,xmm2
  394.    xorps     xmm0,xmm0
  395.    maxps     xmm3,xmm0
  396.  @@:
  397.  
  398.    cvtps2dq  xmm3,xmm3
  399.    packssdw  xmm3,xmm3
  400.    packuswb  xmm3,xmm3
  401.    paddusb   xmm3,[ambient_col]
  402.  
  403. if 1
  404.    cmp       .one_write,2
  405.    jne       .perspective
  406.    movaps    xmm0,xmm3        ; calc texture on plane
  407.    movaps    xmm1,.sph_xyz
  408.    mov       ecx,XRES
  409.    cvtsi2ss  xmm4,ecx
  410.    shufps    xmm4,xmm4,0
  411.    mulps     xmm1,xmm4
  412.    cvtps2dq  xmm1,xmm1
  413.    movd      ecx,xmm1
  414.    test      ecx,0x8
  415.    jz        @f
  416.    mov       ecx,0xffffffff
  417.    jmp       .next_tex_test
  418.  @@:
  419.    xor       ecx,ecx
  420.  .next_tex_test:
  421.    psrldq    xmm1,8
  422.    movd      ebx,xmm1
  423.    test      ebx,0x20
  424.    jz        @f
  425.    mov       ebx,0xffffffff
  426.    jmp       .set_tex
  427.   @@:
  428.    xor       ebx,ebx
  429.   .set_tex:
  430.    xor       ebx,ecx
  431.    shr       ebx,28
  432.    mov       bh,bl
  433.    movd      xmm7,ebx
  434.    paddusb   xmm3,xmm7
  435.  
  436. end if
  437.  
  438.  
  439. .perspective:
  440.    movaps xmm0,.sph_xyz   ; perspective correction
  441.    subps  xmm0,[camera]
  442.    movss  xmm1,[camera]
  443.    movss  xmm2,xmm0
  444.    movaps xmm4,xmm0
  445.    shufps xmm4,xmm4,00000010b
  446.    divss  xmm2,xmm4
  447.    mulss  xmm2,[camera+8]
  448.    subss  xmm1,xmm2 ; xmm1 - x
  449.  
  450.    movaps xmm2,xmm0
  451.    shufps xmm2,xmm2,00000001b
  452.    movaps xmm4,xmm0
  453.    shufps xmm4,xmm4,00000010b
  454.    divss  xmm2,xmm4
  455.    mulss  xmm2,[camera+8]
  456.    movss  xmm4,[camera+4]
  457.    subss  xmm4,xmm2   ; xmm4 - y
  458.  
  459.    mov      ebx,XRES
  460.    cvtsi2ss xmm2,ebx
  461.    mulss    xmm1,xmm2
  462.    mulss    xmm4,xmm2
  463.    cvtss2si ecx,xmm1
  464.    mov      .ix,ecx
  465.    cvtss2si edx,xmm4
  466.    mov      .iy,edx
  467.  
  468.    mov   edi,screen
  469.    mov   ecx,XRES
  470.    imul  ecx,.iy
  471.    add   ecx,.ix
  472.    lea   ecx,[ecx*3]
  473.    add   edi,ecx
  474.    movd  [edi],xmm3
  475. .end:
  476.    add    esp,256
  477.    pop    ebp
  478.  
  479. ret
  480.  
  481.  
  482.  
  483.  
  484.  
  485.