Subversion Repositories Kolibri OS

Rev

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

  1. ; App written by randall ported to KolibriOS and MenuetOS64 by macgub (www.macgub.hekko.pl).
  2. ; Now it use static memory, it is mixed 32bit code and SSE2 instructions.
  3.  
  4. include '../../../macros.inc'
  5.  
  6. use32
  7.  
  8.                org    0x0
  9.  
  10.                db     'MENUET01'              ; 8 byte id
  11.                dd     0x01                    ; header version
  12.                dd     START                   ; start of code
  13.                dd     IMG_END                   ; size of image
  14.                dd     I_END ;0x100000                ; memory for app
  15.                dd     I_END ;0xbffff                 ; esp
  16.                dd     0x0 , 0x0               ; I_Param , I_Icon
  17.  
  18. START:                          ; start of execution
  19.  
  20.      call draw_window
  21.      call Main
  22.      call draw_from_buffer
  23.  
  24. still:
  25.  
  26. ;    mov  eax,23                ; wait here for event
  27. ;    mov  ebx,timeout
  28. ;    int  0x40
  29. ;    mov eax,11                 ; check for event no wait
  30.     mov eax,10                  ; wait for event
  31.     int 0x40
  32.  
  33.     cmp  eax,1                  ; redraw request ?
  34.     je   red
  35.     cmp  eax,2                  ; key in buffer ?
  36.     je   key
  37.     cmp  eax,3                  ; button in buffer ?
  38.     je   button
  39.  
  40.     jmp  noclose
  41.  
  42.   red:                          ; redraw
  43.     call draw_window
  44.     call draw_from_buffer
  45.     jmp  still
  46.  
  47.   key:                          ; key
  48.     mov  eax,2                  ; just read it and ignore
  49.     int  0x40
  50.     shr  eax,8
  51.     cmp  eax, 27
  52.     jne  still
  53.     mov  eax, -1
  54.     int  0x40
  55.  
  56.  
  57.   button:                       ; button
  58.     mov  eax,17                 ; get id
  59.     int  0x40
  60.  
  61.     cmp  ah,1                   ; button id=1 ?
  62.     jne  noclose
  63.  
  64.     mov  eax,-1                 ; close this program
  65.     int  0x40
  66.   noclose:
  67.  
  68.     jmp  still
  69.  
  70.  
  71. draw_from_buffer:
  72.  
  73.             mov eax,7
  74.             mov ebx,screen4
  75.             mov ecx,IMG_WIDTH*65536+IMG_HEIGHT
  76.             mov edx,0*65536+0
  77.             int 0x40
  78. ret
  79.  
  80.  
  81. ;-------------------------------------------------------------------------------
  82. ; NAME:     shuf
  83. ;-------------------------------------------------------------------------------
  84. macro       shuf        d*,s*,z*,y*,x*,w* {
  85.             shufps      d,s,(z shl 6) or (y shl 4) or (x shl 2) or w
  86. }
  87. ;-------------------------------------------------------------------------------
  88. ; NAME:     length3
  89. ; IN:       xmm0.xyz    input vector
  90. ; OUT:      xmm0.xyzw   vector length
  91. ;-------------------------------------------------------------------------------
  92. macro       length3     {
  93.             mulps       xmm0,xmm0
  94.             movaps      xmm1,xmm0
  95.             movaps      xmm2,xmm0
  96.             shufps      xmm0,xmm0,0x00
  97.             shufps      xmm1,xmm1,0x55
  98.             shufps      xmm2,xmm2,0xaa
  99.             addps       xmm0,xmm1
  100.             addps       xmm0,xmm2
  101.             sqrtps      xmm0,xmm0
  102. }
  103. ;-------------------------------------------------------------------------------
  104. ; NAME:     length4
  105. ; IN:       xmm0.xyzw   input vector
  106. ; OUT:      xmm0.xyzw   vector length
  107. ;-------------------------------------------------------------------------------
  108. macro       length4     {
  109.             mulps       xmm0,xmm0
  110.             movaps      xmm1,xmm0
  111.             movaps      xmm2,xmm0
  112.             movaps      xmm3,xmm0
  113.             shufps      xmm0,xmm0,0x00
  114.             shufps      xmm1,xmm1,0x55
  115.             shufps      xmm2,xmm2,0xaa
  116.             shufps      xmm3,xmm3,0xff
  117.             addps       xmm0,xmm1
  118.             addps       xmm2,xmm3
  119.             addps       xmm0,xmm2
  120.             sqrtps      xmm0,xmm0
  121. }
  122. ;-------------------------------------------------------------------------------
  123. ; NAME:     normalize3
  124. ; IN:       xmm0.xyz    input vector
  125. ; OUT:      xmm0.xyz    normalized vector
  126. ;-------------------------------------------------------------------------------
  127. macro       normalize3  {
  128.             movaps      xmm3,xmm0
  129.             mulps       xmm0,xmm0
  130.             movaps      xmm1,xmm0
  131.             movaps      xmm2,xmm0
  132.             shufps      xmm0,xmm0,0x00
  133.             shufps      xmm1,xmm1,0x55
  134.             shufps      xmm2,xmm2,0xaa
  135.             addps       xmm0,xmm1
  136.             addps       xmm0,xmm2
  137.             rsqrtps     xmm0,xmm0
  138.             mulps       xmm0,xmm3
  139. }
  140. ;-------------------------------------------------------------------------------
  141. ; NAME:     cross
  142. ; IN:       xmm0.xyz    first input vector
  143. ; IN:       xmm1.xyz    second input vector
  144. ; OUT:      xmm0.xyz    cross product result
  145. ;-------------------------------------------------------------------------------
  146. macro       cross       {
  147.             movaps      xmm2,xmm0
  148.             movaps      xmm3,xmm1
  149.             shuf        xmm0,xmm0,3,0,2,1
  150.             shuf        xmm1,xmm1,3,1,0,2
  151.             shuf        xmm2,xmm2,3,1,0,2
  152.             shuf        xmm3,xmm3,3,0,2,1
  153.             mulps       xmm0,xmm1
  154.             mulps       xmm2,xmm3
  155.             subps       xmm0,xmm2
  156. }
  157. ;-------------------------------------------------------------------------------
  158. ; NAME:     dot3
  159. ; IN:       xmm0.xyz    first input vector
  160. ; IN:       xmm1.xyz    second input vector
  161. ; OUT:      xmm0.xyzw   dot product result
  162. ;-------------------------------------------------------------------------------
  163. macro       dot3        {
  164.             mulps       xmm0,xmm1
  165.             movaps      xmm1,xmm0
  166.             movaps      xmm2,xmm0
  167.             shufps      xmm0,xmm0,0x00
  168.             shufps      xmm1,xmm1,0x55
  169.             shufps      xmm2,xmm2,0xaa
  170.             addps       xmm0,xmm1
  171.             addps       xmm0,xmm2
  172. }
  173. ;-------------------------------------------------------------------------------
  174. ; NAME:     dot4
  175. ; IN:       xmm0.xyzw   first input vector
  176. ; IN:       xmm1.xyzw   second input vector
  177. ; OUT:      xmm0.xyzw   dot product result
  178. ;-------------------------------------------------------------------------------
  179. macro       dot4        {
  180.             mulps       xmm0,xmm1
  181.             movaps      xmm1,xmm0
  182.             movaps      xmm2,xmm0
  183.             movaps      xmm3,xmm0
  184.             shufps      xmm0,xmm0,0x00
  185.             shufps      xmm1,xmm1,0x55
  186.             shufps      xmm2,xmm2,0xaa
  187.             shufps      xmm3,xmm3,0xff
  188.             addps       xmm0,xmm1
  189.             addps       xmm2,xmm3
  190.             addps       xmm0,xmm2
  191. }
  192. ;-------------------------------------------------------------------------------
  193. ; NAME:     qsq
  194. ; IN:       xmm0.xyzw   quaternion
  195. ; OUT:      xmm0.xyzw   squared quaternion
  196. ;-------------------------------------------------------------------------------
  197. macro       qsq         {
  198.             movaps      xmm3,xmm0
  199.             movaps      xmm4,xmm0
  200.             movaps      xmm1,xmm0
  201.             dot3
  202.             shufps      xmm3,xmm3,0xff
  203.             movaps      xmm5,xmm3
  204.             mulps       xmm3,xmm3
  205.             subps       xmm3,xmm0   ; r.___w
  206.             mulps       xmm4,xmm5
  207.             addps       xmm4,xmm4   ; r.xyz_
  208.             andps       xmm3,dqword [g_ClearXYZ]
  209.             andps       xmm4,dqword [g_ClearW]
  210.             orps        xmm3,xmm4
  211.             movaps      xmm0,xmm3
  212. }
  213. ;-------------------------------------------------------------------------------
  214. ; NAME:     qmul
  215. ; IN:       xmm0.xyzw   first quaternion
  216. ; IN:       xmm1.xyzw   second quaternion
  217. ; OUT:      xmm0.xyzw   first quaternion multiplied by second quaternion
  218. ;-------------------------------------------------------------------------------
  219. macro       qmul        {
  220.             movaps      xmm6,xmm0       ; q1.xyzw
  221.             movaps      xmm7,xmm1       ; q2.xyzw
  222.             dot3
  223.             movaps      xmm4,xmm6
  224.             movaps      xmm5,xmm7
  225.             shufps      xmm4,xmm4,0xff  ; q1.wwww
  226.             shufps      xmm5,xmm5,0xff  ; q2.wwww
  227.             movaps      xmm1,xmm4
  228.             mulps       xmm1,xmm5
  229.             subps       xmm1,xmm0
  230.             andps       xmm1,dqword [g_ClearXYZ]
  231.             movaps      [xxmm8],xmm1                   ; r.000w
  232.             movaps      xmm0,xmm6
  233.             movaps      xmm1,xmm7
  234.             cross
  235.             mulps       xmm7,xmm4
  236.             mulps       xmm6,xmm5
  237.             addps       xmm6,xmm7
  238.             addps       xmm0,xmm6
  239.             andps       xmm0,dqword [g_ClearW]
  240.             orps        xmm0,[xxmm8]
  241. }
  242. ;-------------------------------------------------------------------------------
  243. ; NAME:     logss
  244. ; IN:       xmm0.x      function argument
  245. ; OUT:      xmm0.x      function result
  246. ;-------------------------------------------------------------------------------
  247. macro       logss       {
  248.             maxss       xmm0,[g_MinNormPos]
  249.             movss       xmm1,[g_1_0]
  250.             movd        edx,xmm0
  251.             andps       xmm0,dqword [g_InvMantMask]
  252.             orps        xmm0,xmm1
  253.             movaps      xmm4,xmm0
  254.             subss       xmm0,xmm1
  255.             addss       xmm4,xmm1
  256.             shr         edx,23
  257.             rcpss       xmm4,xmm4
  258.             mulss       xmm0,xmm4
  259.             addss       xmm0,xmm0
  260.             movaps      xmm2,xmm0
  261.             mulss       xmm0,xmm0
  262.             sub         edx,0x7f
  263.             movss       xmm4,[g_log_p0]
  264.             movss       xmm6,[g_log_q0]
  265.             mulss       xmm4,xmm0
  266.             movss       xmm5,[g_log_p1]
  267.             mulss       xmm6,xmm0
  268.             movss       xmm7,[g_log_q1]
  269.             addss       xmm4,xmm5
  270.             addss       xmm6,xmm7
  271.             movss       xmm5,[g_log_p2]
  272.             mulss       xmm4,xmm0
  273.             movss       xmm7,[g_log_q2]
  274.             mulss       xmm6,xmm0
  275.             addss       xmm4,xmm5
  276.             movss       xmm5,[g_log_c0]
  277.             addss       xmm6,xmm7
  278.             cvtsi2ss    xmm1,edx
  279.             mulss       xmm0,xmm4
  280.             rcpss       xmm6,xmm6
  281.             mulss       xmm0,xmm6
  282.             mulss       xmm0,xmm2
  283.             mulss       xmm1,xmm5
  284.             addss       xmm0,xmm2
  285.             addss       xmm0,xmm1
  286. }
  287.  
  288. ;-------------------------------------------------------------------------------
  289. ; NAME:     QJuliaDist
  290. ; IN:       xmm0.xyz    position
  291. ; OUT:      xmm0.xyzw   distance to the nearest point in quaternion julia set
  292. ;-------------------------------------------------------------------------------
  293. align 64
  294. QJuliaDist:
  295.             Z           equ ebp-16
  296.             Zp          equ ebp-32
  297.             NormZ       equ ebp-36
  298.             NormZp      equ ebp-40
  299.             push        ebp
  300.             mov         ebp,esp
  301.             sub         esp,64
  302.             ; init Z and Zp
  303.             andps       xmm0,dqword [g_ClearW]
  304.             shuf        xmm0,xmm0,0,3,2,1
  305.             movups      [Z],xmm0
  306.             movaps      xmm1,dqword [g_UnitW]
  307.             movups      [Zp],xmm1
  308.             ; iterate
  309.             mov         ecx,10
  310. .Iterate:
  311.             ; compute and update Zp
  312.             movups      xmm0,[Z]
  313.             movups      xmm1,[Zp]
  314.             qmul
  315.             addps       xmm0,xmm0
  316.             movups      [Zp],xmm0
  317.             ; compute and update Z
  318.             movups      xmm0,[Z]
  319.             qsq
  320.             addps       xmm0,dqword [g_Quat]
  321.             movups      [Z],xmm0
  322.             ; check if squared length of Z is greater than g_EscapeThreshold,
  323.             ; break the loop if it is
  324.             movaps      xmm1,xmm0
  325.             dot4
  326.             movss       xmm1,[g_EscapeThreshold]
  327.             cmpltss     xmm1,xmm0
  328.             movd        eax,xmm1
  329.             cmp         eax,0xffffffff
  330.             je          .IterateEnd
  331.             ; continue the loop
  332.             sub         ecx,1
  333.             test        ecx,ecx
  334.             jnz         .Iterate
  335. .IterateEnd:
  336.             movups      xmm0,[Z]
  337.             length4
  338.             movss       [NormZ],xmm0
  339.             movups      xmm0,[Zp]
  340.             length4
  341.             movss       [NormZp],xmm0
  342.             movss       xmm0,[NormZ]
  343.             logss
  344.             divss       xmm0,[NormZp]
  345.             mulss       xmm0,[NormZ]
  346.             mulss       xmm0,[g_0_5]
  347.             shufps      xmm0,xmm0,0x00
  348.             mov         esp,ebp
  349.             pop         ebp
  350.            ; restore     Z,Zp,NormZ,NormZp
  351.             ret
  352. ;-------------------------------------------------------------------------------
  353. ; NAME:     Map
  354. ; IN:       xmm0.xyz    position
  355. ; OUT:      xmm0.xyzw   distance to the nearest object from input position
  356. ; OUT:      eax         material ID of the nearest object
  357. ;-------------------------------------------------------------------------------
  358. align 64
  359. Map:
  360.           ;  P           equ ebp-16
  361.           ;  MinDist     equ ebp-32
  362.             MatID       equ ebp-40
  363.             push        ebp
  364.             mov         ebp,esp
  365.             sub         esp,128
  366.             movaps      [.P],xmm0
  367.             movaps      xmm0,dqword [g_255_0]
  368.             movaps      [.MinDist],xmm0
  369.             mov         dword [MatID],0
  370.             ; QJulia
  371.             movaps      xmm0,[.P]
  372.             call        QJuliaDist
  373.             movaps      xmm1,xmm0
  374.             cmpltps     xmm1,[.MinDist]
  375.             movd        eax,xmm1
  376.             cmp         eax,0xffffffff
  377.             jne         @f
  378.             movaps      [.MinDist],xmm0
  379.             mov         dword [MatID],4
  380. @@:
  381.             ; sphere
  382.             movaps      xmm0,[.P]
  383.             subps       xmm0,dqword [g_UnitY]
  384.             length3
  385.             subps       xmm0,dqword [g_1_0]
  386.             movaps      xmm1,xmm0
  387.             cmpltps     xmm1,[.MinDist]
  388.             movd        eax,xmm1
  389.             cmp         eax,0xffffffff
  390.             jne         @f
  391.             ;movaps      [MinDist],xmm0
  392.             ;mov         dword [MatID],1
  393. @@:
  394.             ; plane
  395.             movaps      xmm0,[.P]
  396.             movaps      xmm1,dqword [g_UnitY]
  397.             dot3
  398.             addps       xmm0,dqword [g_1_0]
  399.             movaps      xmm1,xmm0
  400.             cmpltps     xmm1,[.MinDist]
  401.             movd        eax,xmm1
  402.             cmp         eax,0xffffffff
  403.             jne         @f
  404.             movaps      [.MinDist],xmm0
  405.             mov         dword [MatID],2
  406. @@:
  407.             ; box
  408.             xorps       xmm0,xmm0
  409.             subps       xmm0,[.P]
  410.             maxps       xmm0,[.P]
  411.             subps       xmm0,dqword [g_BoxSize]
  412.             maxps       xmm0,dqword [g_0_0]
  413.             length3
  414.             subps       xmm0,dqword [g_BoxEdge]
  415.             movaps      xmm1,xmm0
  416.             cmpltps     xmm1,[.MinDist]
  417.             movd        eax,xmm1
  418.             cmp         eax,0xffffffff
  419.             jne         @f
  420.             ;movaps      [MinDist],xmm0
  421.             ;mov         dword [MatID],3
  422. @@:
  423.             movaps      xmm0,[.MinDist]
  424.             mov         eax,[MatID]
  425.             mov         esp,ebp
  426.             pop         ebp
  427.             ret
  428. align 16
  429. .P:
  430.    rd 4
  431. .MinDist:
  432.    rd 4
  433.  
  434.        ;     restore     P,MinDist,MatID
  435. ;-------------------------------------------------------------------------------
  436. ; NAME:     CastRay
  437. ; IN:       xmm0.xyz    ray origin
  438. ; IN:       xmm1.xyz    ray direction
  439. ; OUT:      xmm0.xyzw   distance from ray orgin to the nearest intersected object
  440. ;                       or -1.0 if there is no intersection
  441. ;-------------------------------------------------------------------------------
  442. align 64
  443. CastRay:
  444.      ;       RO          equ ebp-16
  445.      ;       RD          equ ebp-32
  446.      ;       T           equ ebp-48
  447.             MatID       equ ebp-52
  448.             push        ebp
  449.             mov         ebp,esp
  450.             sub         esp,128
  451.             ; init stack variables
  452.             movaps      [.RO],xmm0
  453.             movaps      [.RD],xmm1
  454.             xorps       xmm0,xmm0
  455.             movaps      [.T],xmm0
  456. .March:
  457.             ; find distance to the nearest object
  458.             movaps      xmm0,[.RD]
  459.             mulps       xmm0,[.T]
  460.             addps       xmm0,[.RO]
  461.             call        Map
  462.             mov         [MatID],eax
  463.             ; return if distance is less than g_HitDist
  464.             movaps      xmm1,xmm0
  465.             cmpltps     xmm1,dqword [g_HitDist]
  466.             movd        eax,xmm1
  467.             cmp         eax,0xffffffff
  468.             je          .Hit
  469.             ; increment T with distance to the nearest object
  470.             movaps      xmm1,[.T]
  471.             addps       xmm1,xmm0
  472.             movaps      [.T],xmm1
  473.             ; continue loop only if distance is less than g_MaxDist
  474.             cmpltps     xmm1,dqword [g_MaxDist]
  475.             movd        eax,xmm1
  476.             cmp         eax,0xffffffff
  477.             je          .March
  478.             xorps       xmm0,xmm0
  479.             subps       xmm0,dqword [g_1_0]
  480.             movaps      [.T],xmm0
  481.             mov         dword [MatID],0
  482. .Hit:
  483.             movaps      xmm0,[.T]
  484.             mov         eax,[MatID]
  485.             mov         esp,ebp
  486.             pop         ebp
  487.             ret
  488. align 16
  489. .RO:
  490.    rd 4
  491. .RD:
  492.    rd 4
  493. .T:
  494.    rd 4
  495.       ;      restore     RO,RD,T,MatID
  496. ;-------------------------------------------------------------------------------
  497. ; NAME:     CastShadowRay
  498. ; IN:       xmm0.xyz    ray origin
  499. ; IN:       xmm1.xyz    ray direction
  500. ; OUT:      xmm0.xyzw   visibility factor [0.0, 1.0],
  501. ;                       0.0 means path is fully blocked,
  502. ;                       1.0 means path is fully clear
  503. ;-------------------------------------------------------------------------------
  504. align 64
  505. CastShadowRay:
  506.          ;   RO          equ ebp-16
  507.          ;   RD          equ ebp-32
  508.             R           equ ebp-48
  509.          ;   T           equ ebp-64
  510.             push        ebp
  511.             mov         ebp,esp
  512.             sub         esp,128
  513.             ; init stack variables
  514.             movaps      [.RO],xmm0
  515.             movaps      [.RD],xmm1
  516.             movaps      xmm0,dqword [g_0_01]
  517.             movaps      [.T],xmm0
  518.             movaps      xmm0,dqword [g_1_0]
  519.             movups      [R],xmm0
  520. .March:
  521.             ; find distance to the nearest object
  522.             movaps      xmm0,[.RD]
  523.             mulps       xmm0,[.T]
  524.             addps       xmm0,[.RO]
  525.             call        Map
  526.             ; return 0.0 if distance is less than g_ShadowHitDist
  527.             movaps      xmm1,xmm0
  528.             cmpltps     xmm1,dqword [g_ShadowHitDist]
  529.             movd        eax,xmm1
  530.             cmp         eax,0xffffffff
  531.             je          .Hit
  532.             ; compute R
  533.             movaps      xmm1,xmm0
  534.             rcpps       xmm2,[.T]
  535.             mulps       xmm1,xmm2
  536.             mulps       xmm1,dqword [g_16_0]
  537.             movups      xmm2,[R]
  538.             minps       xmm2,xmm1
  539.             movups      [R],xmm2
  540.             ; increment T with distance to the nearest object
  541.             movaps      xmm1,[.T]
  542.             addps       xmm1,xmm0
  543.             movaps      [.T],xmm1
  544.             ; continue loop only if distance is less than g_ShadowMaxDist
  545.             cmpltps     xmm1,dqword [g_ShadowMaxDist]
  546.             movd        eax,xmm1
  547.             cmp         eax,0xffffffff
  548.             je          .March
  549.             ; return (R,R,R,R)
  550.             movups      xmm0,[R]
  551.             mov         esp,ebp
  552.             pop         ebp
  553.             ret
  554. .Hit:
  555.             ; return (0,0,0,0)
  556.             xorps       xmm0,xmm0
  557.             mov         esp,ebp
  558.             pop         ebp
  559.             ret
  560.         ;    restore     RO,RD,R,T
  561. align 16
  562. .RO:
  563.    rd 4
  564. .RD:
  565.    rd 4
  566. .T:
  567.    rd 4
  568. ;-------------------------------------------------------------------------------
  569. ; NAME:     ComputeNormal
  570. ; IN:       xmm0.xyz    position
  571. ; OUT:      xmm0.xyz    normal vector
  572. ;-------------------------------------------------------------------------------
  573. align 64
  574. ComputeNormal:
  575.             P           equ ebp-16
  576.             N           equ ebp-32
  577.             push        ebp
  578.             mov         ebp,esp
  579.             sub         esp,128
  580.             movups      [P],xmm0
  581.             ; compute x coordinate
  582.             addps       xmm0,dqword [g_NormalDX]
  583.             call        Map
  584.             movss       [N+0],xmm0
  585.             movups      xmm0,[P]
  586.             subps       xmm0,dqword [g_NormalDX]
  587.             call        Map
  588.             movss       xmm1,[N+0]
  589.             subss       xmm1,xmm0
  590.             movss       [N+0],xmm1
  591.             ; compute y coordinate
  592.             movups      xmm0,[P]
  593.             addps       xmm0,dqword [g_NormalDY]
  594.             call        Map
  595.             movss       [N+4],xmm0
  596.             movups      xmm0,[P]
  597.             subps       xmm0,dqword [g_NormalDY]
  598.             call        Map
  599.             movss       xmm1,[N+4]
  600.             subss       xmm1,xmm0
  601.             movss       [N+4],xmm1
  602.             ; compute z coordinate
  603.             movups      xmm0,[P]
  604.             addps       xmm0,dqword [g_NormalDZ]
  605.             call        Map
  606.             movss       [N+8],xmm0
  607.             movups      xmm0,[P]
  608.             subps       xmm0,dqword [g_NormalDZ]
  609.             call        Map
  610.             movss       xmm1,[N+8]
  611.             subss       xmm1,xmm0
  612.             movss       [N+8],xmm1
  613.             ; normalize
  614.             movups      xmm0,[N]
  615.             normalize3
  616.             mov         esp,ebp
  617.             pop         ebp
  618.             ret
  619.           ;  restore     P,N
  620. ;-------------------------------------------------------------------------------
  621. ; NAME:     Shade
  622. ; IN:       xmm0.xyz    position
  623. ; IN:       xmm1.xyz    normal vector
  624. ; IN:       edi         material ID
  625. ; OUT:      xmm0.xyz    color
  626. ;-------------------------------------------------------------------------------
  627. align 64
  628. Shade:
  629.      ;       P           equ ebp-16
  630.      ;       N           equ ebp-32
  631.      ;       RGB         equ ebp-48
  632.             C           equ ebp-64
  633.             NdotL       equ ebp-80
  634.             L           equ ebp-96
  635.      ;       AOScale     equ ebp-112
  636.      ;       AO          equ ebp-128
  637.             Temp        equ ebp-144
  638.             Idx         equ ebp-148
  639.             MatID       equ ebp-152
  640.             push        ebp
  641.             mov         ebp,esp
  642.             sub         esp,256
  643.             movaps      [.P],xmm0
  644.             movaps      [.N],xmm1
  645.             mov         [MatID],edi
  646.             ;
  647.             ; AO
  648.             ;
  649.             xorps       xmm0,xmm0
  650.             movaps      [.AO],xmm0
  651.             movaps      xmm0,dqword [g_10_0]
  652.             movaps      [.AOscale],xmm0
  653.             mov         dword [Idx],0
  654. .AOLoop:
  655.             cvtsi2ss    xmm0,[Idx]
  656.             shufps      xmm0,xmm0,0x00
  657.             mulps       xmm0,xmm0
  658.             mulps       xmm0,dqword [g_0_015]
  659.             addps       xmm0,dqword [g_0_01]
  660.             movups      [Temp],xmm0
  661.             mulps       xmm0,[.N]
  662.             addps       xmm0,[.P]
  663.             call        Map
  664.             movups      xmm1,[Temp]
  665.             subps       xmm1,xmm0
  666.             mulps       xmm1,[.AOscale]
  667.             movaps      xmm0,[.AO]
  668.             addps       xmm0,xmm1
  669.             movaps      [.AO],xmm0
  670.             movaps      xmm0,[.AOscale]
  671.             mulps       xmm0,dqword [g_0_5]
  672.             movaps      [.AOscale],xmm0
  673.             add         dword [Idx],1
  674.             cmp         dword [Idx],5
  675.             jne         .AOLoop
  676.             movaps      xmm0,[.AO]
  677.             maxps       xmm0,dqword [g_0_0]
  678.             minps       xmm0,dqword [g_1_0]
  679.             movaps      xmm1,dqword [g_1_0]
  680.             subps       xmm1,xmm0
  681.             movaps      [.AO],xmm1
  682.             ;
  683.             ; Material ID Switch
  684.             ;
  685.             mov         edi,[MatID]
  686.             cmp         edi,1
  687.             je          .Mat1
  688.             cmp         edi,2
  689.             je          .Mat2
  690.             jmp         .MatDef
  691. .Mat1:
  692.             movaps      xmm0,dqword [g_Red]
  693.             movaps      [.RGB],xmm0
  694.             jmp         .MatBreak
  695. .Mat2:
  696.             movaps      xmm0,dqword [g_Green]
  697.             movaps      [.RGB],xmm0
  698.             jmp         .MatBreak
  699. .MatDef:
  700.             movaps      xmm0,dqword [g_1_0]
  701.             movaps      [.RGB],xmm0
  702. .MatBreak:
  703.             ;
  704.             ; Light0 Contribution
  705.             ;
  706.             ; compute light vector and "N dot L" value
  707.             movaps      xmm0,dqword [g_L0Pos]
  708.             subps       xmm0,[.P]
  709.             normalize3
  710.             movups      [L],xmm0
  711.             movups      xmm1,[N]
  712.             dot3
  713.             movups      [NdotL],xmm0
  714.             ; cast shadow ray
  715.             movaps      xmm0,[.P]
  716.             movups      xmm1,[L]
  717.             call        CastShadowRay
  718.             movups      xmm1,[NdotL]
  719.             mulps       xmm0,xmm1
  720.             mulps       xmm0,dqword [g_0_6]
  721.             addps       xmm0,dqword [g_0_4]
  722.             maxps       xmm0,dqword [g_0_0]
  723.             mulps       xmm0,dqword [g_0_7]
  724.             mulps       xmm0,dqword [.RGB]
  725.             movups      [C],xmm0
  726.             ;
  727.             ; Light1 Contribution
  728.             ;
  729.             ; compute light vector and "N dot L" value
  730.             movaps      xmm0,dqword [g_L1Pos]
  731.             subps       xmm0,[.P]
  732.             normalize3
  733.             movups      [L],xmm0
  734.             movaps      xmm1,[.N]
  735.             dot3
  736.             movups      [NdotL],xmm0
  737.             ; cast shadow ray
  738.             movaps      xmm0,[.P]
  739.             movups      xmm1,[L]
  740.             call        CastShadowRay
  741.             movups      xmm1,[NdotL]
  742.             mulps       xmm0,xmm1
  743.             mulps       xmm0,dqword [g_0_6]
  744.             addps       xmm0,dqword [g_0_4]
  745.             maxps       xmm0,dqword [g_0_0]
  746.             mulps       xmm0,dqword [g_0_3]
  747.             mulps       xmm0,dqword [.RGB]
  748.             movups      xmm1,[C]
  749.             addps       xmm0,xmm1
  750.             mulps       xmm0,[.AO]
  751.             mov         esp,ebp
  752.             pop         ebp
  753.             ret
  754.          ;   restore     P,N,C,RGB,NdotL,L,AOScale,AO,Temp,Idx,MatID
  755. align 16
  756. .N:
  757.     rd 4
  758. .P:
  759.     rd 4
  760. .RGB:
  761.     rd 4
  762. .AOscale:
  763.     rd 4
  764. .AO:
  765.     rd 4
  766. ;-------------------------------------------------------------------------------
  767. ; NAME:     ComputeColor
  768. ; IN:       xmm0.x      normalized x coordinate
  769. ; IN:       xmm0.y      normalized y coordinate
  770. ; OUT:      xmm0.xyz    pixel color
  771. ;-------------------------------------------------------------------------------
  772. align 64
  773. ComputeColor:
  774.         ;    X           equ ebp-32
  775.         ;    Y           equ ebp-48
  776.         ;    RD          equ ebp-64
  777.             P           equ ebp-80
  778.             N           equ ebp-96
  779.             MatID       equ ebp-100
  780.             push        ebp
  781.             mov         ebp,esp
  782.             sub         esp,128
  783.             ; save function parameters on the stack
  784.             shufps      xmm0,xmm0,0x00
  785.             shufps      xmm1,xmm1,0x00
  786.             movaps      [.X],xmm0
  787.             movaps      [.Y],xmm1
  788.             ; compute z axis
  789.             xorps       xmm0,xmm0
  790.             subps       xmm0,dqword [g_CamPos]
  791.             normalize3
  792.             movaps      xmm7,xmm0
  793.             ; compute x axis
  794.             movaps      xmm0,dqword [g_UnitY]
  795.             movaps      xmm1,xmm7
  796.             cross
  797.             normalize3
  798.             movaps      xmm6,xmm0
  799.             ; compute y axis
  800.             movaps      xmm0,xmm7
  801.             movaps      xmm1,xmm6
  802.             cross
  803.             normalize3
  804.             ; compute ray direction
  805.             mulps       xmm0,[.Y]
  806.             mulps       xmm6,[.X]
  807.             movaps      xmm1,xmm7
  808.             mulps       xmm1,dqword [g_0_5]
  809.             addps       xmm7,xmm1
  810.             addps       xmm0,xmm6
  811.             addps       xmm0,xmm7
  812.             normalize3
  813.             movaps      [.RD],xmm0
  814.             ; cast ray
  815.             movaps      xmm0,dqword [g_CamPos]
  816.             movaps      xmm1,[.RD]
  817.             call        CastRay
  818.             mov         [MatID],eax
  819.             ; return if there is no intersection
  820.             movaps      xmm1,xmm0
  821.             cmpltps     xmm0,dqword [g_0_0]
  822.             movd        eax,xmm0
  823.             cmp         eax,0xffffffff
  824.             je          .Return
  825.             ; compute intersection point
  826.             movaps      xmm0,[.RD]
  827.             mulps       xmm0,xmm1
  828.             addps       xmm0,dqword [g_CamPos]
  829.             movups      [P],xmm0
  830.             ; compute normal vector
  831.             call        ComputeNormal
  832.             movups      [N],xmm0
  833.             ; shade
  834.             mov         edi,[MatID]
  835.             movups      xmm0,[P]
  836.             movups      xmm1,[N]
  837.             call        Shade
  838. .Return:
  839.             mov         esp,ebp
  840.             pop         ebp
  841.             ret
  842.        ;     restore     X,Y,RD,P,N,MatID
  843. align 16
  844. .X:
  845.    rd 4
  846. .Y:
  847.    rd 4
  848. .RD:
  849.    rd 4
  850. ;-------------------------------------------------------------------------------
  851. ; NAME:     Main
  852. ; DESC:     Program entry point.
  853. ;-------------------------------------------------------------------------------
  854. align 64
  855. Main:
  856.             ImgPtr      equ ebp-8
  857.             Img8Ptr     equ ebp-16
  858.             X           equ ebp-20
  859.             Y           equ ebp-24
  860.             mov         ebp,esp
  861.             sub         esp,128
  862.             ;
  863.             ; generate floating point image
  864.             ;
  865.             mov         dword [ImgPtr],screen16
  866.             mov         ebx,screen16
  867.             ; begin loops
  868.             mov         dword [Y],0
  869. .LoopY:
  870.             mov         dword [X],0
  871. .LoopX:
  872.             ; compute normalized x coordinate [-1.777 , 1.777]
  873.             cvtsi2ss    xmm0,[X]
  874.             divss       xmm0,[g_ImgWidth]
  875.             subss       xmm0,[g_0_5]
  876.             addss       xmm0,xmm0
  877.             mov         eax,1.777
  878.             movd        xmm2,eax
  879.             mulss       xmm0,xmm2
  880.             ; compute normalized y coordinate [-1.0 , 1.0]
  881.             cvtsi2ss    xmm1,[Y]
  882.             divss       xmm1,[g_ImgHeight]
  883.             subss       xmm1,[g_0_5]
  884.             addss       xmm1,xmm1
  885.             ; compute and write pixel color to the buffer
  886.             call        ComputeColor
  887.             movaps      [ebx],xmm0
  888.             ; advance pixel pointer
  889.             add         ebx,16
  890.             ; continue .LoopX
  891.             add         dword [X],1
  892.             cmp         dword [X],IMG_WIDTH
  893.             jne         .LoopX
  894.             ; continue .LoopY
  895.             add         dword [Y],1
  896.             cmp         dword [Y],IMG_HEIGHT
  897.             jne         .LoopY
  898.             ;
  899.             ; convert image to 4 x 8 bpp
  900.             ;
  901.             mov         dword [Img8Ptr],screen4
  902.             ; set dst and src pointers and loop counter
  903.             mov         edi,screen4
  904.             mov         esi,[ImgPtr]
  905.             mov         ecx,IMG_WIDTH*IMG_HEIGHT
  906. @@:
  907.             ; clamp to [0.0 ,1.0]
  908.             movaps      xmm0,[esi]
  909.             maxps       xmm0,dqword [g_0_0]
  910.             minps       xmm0,dqword [g_1_0]
  911.             ; convert from [0.0 ,1.0] to [0 ,255]
  912.             mulps       xmm0,dqword [g_255_0]
  913.             cvttps2dq   xmm0,xmm0
  914.       ;      pshufb      xmm0,dqword [g_ImageConvMask]
  915.             ; set alpha to 0xff
  916.             movd        [edi],xmm0
  917.             or          dword [edi],0xff000000
  918.             ; continue loop
  919.             add         esi,16
  920.             add         edi,4
  921.             sub         ecx,1
  922.             jnz         @b
  923.             ;convert    to 24 bit per pixel
  924.             mov         esi,screen4
  925.             mov         edi,esi
  926.             mov         ecx,IMG_WIDTH*IMG_HEIGHT
  927.             cld
  928.        @@:
  929.             lodsd
  930.             mov         ah,al
  931.             push        ax
  932.             ror         eax,16
  933.             pop         ax
  934.             stosd
  935.             dec         edi
  936.             loop        @b
  937.  
  938.             mov         esp,ebp
  939.  
  940. ret
  941. ;   *********************************************
  942. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  943. ;   *********************************************
  944. draw_window:
  945.  
  946.     mcall 12, 1                                   ; function 12:tell os about windowdraw
  947.    
  948.         mcall 48, 4                                   ;get skin width
  949.         lea     ecx, [50*65536+IMG_HEIGHT+4+eax]            ; [y start] *65536 + [y size] + [skin_height]
  950.         mcall   0,<50,IMG_WIDTH+9>,,0x74000000,,labelt ;draw window
  951.  
  952.     mcall 12, 2                                   ; function 12:tell os about windowdraw
  953.  
  954.     ret
  955.  
  956.  
  957.  
  958. ;-------------------------------------------------------------------------------
  959. labelt:
  960.  db  'qjulia',0
  961. labelen:
  962.  
  963. align 4
  964. IMG_WIDTH               = 800
  965. IMG_HEIGHT              = 600
  966. g_ImgWidth              dd 800.0
  967. g_ImgHeight             dd 600.0
  968. g_EscapeThreshold       dd 16.0
  969. g_MinNormPos            dd 0x00800000
  970. g_log_p0                dd -0.789580278884799154124
  971. g_log_p1                dd 16.3866645699558079767
  972. g_log_p2                dd -64.1409952958715622951
  973. g_log_q0                dd -35.6722798256324312549
  974. g_log_q1                dd 312.093766372244180303
  975. g_log_q2                dd -769.691943550460008604
  976. g_log_c0                dd 0.693147180559945
  977.  
  978. align 16
  979. ;g_ImageConvMask         db 8,4,0,12,12 dup 0x80
  980. g_InvMantMask           dd 4 dup (not 0x7f800000)
  981. g_0_0                   dd 4 dup 0.0
  982. g_0_5                   dd 4 dup 0.5
  983. g_1_0                   dd 4 dup 1.0
  984. g_255_0                 dd 4 dup 255.0
  985. g_16_0                  dd 4 dup 128.0
  986. g_10_0                  dd 4 dup 10.0
  987. g_0_6                   dd 4 dup 0.6
  988. g_0_4                   dd 4 dup 0.4
  989. g_0_7                   dd 4 dup 0.7
  990. g_0_3                   dd 4 dup 0.3
  991. g_0_01                  dd 4 dup 0.01
  992. g_0_015                 dd 4 dup 0.015
  993. g_CamPos                dd 1.2,1.4,1.2,1.0
  994. g_UnitY                 dd 0.0,1.0,0.0,0.0
  995. g_UnitW                 dd 0.0,0.0,0.0,1.0
  996. g_HitDist               dd 4 dup 0.001
  997. g_ShadowHitDist         dd 4 dup 0.0005
  998. g_ShadowMaxDist         dd 4 dup 10.0
  999. g_MaxDist               dd 4 dup 40.0
  1000. g_NormalDX              dd 0.001,0.0,0.0,0.0
  1001. g_NormalDY              dd 0.0,0.001,0.0,0.0
  1002. g_NormalDZ              dd 0.0,0.0,0.001,0.0
  1003. g_L0Pos                 dd 10.0,8.0,-6.0,1.0
  1004. g_L1Pos                 dd -12.0,19.0,6.0,1.0
  1005. g_Red                   dd 1.0,1.0,1.0,1.0
  1006. g_Green                 dd 1.0,1.0,1.0,1.0
  1007. g_BoxSize               dd 1.5,1.0,1.5,1.0
  1008. g_BoxEdge               dd 4 dup 0.03
  1009. g_ClearXYZ              dd 0x00000000,0x00000000,0x00000000,0xffffffff
  1010. g_ClearW                dd 0xffffffff,0xffffffff,0xffffffff,0x00000000
  1011. g_Quat                  dd 0.2,0.0,0.0,-1.0
  1012. IMG_END:
  1013. align 16
  1014. screen16                rb IMG_WIDTH*IMG_HEIGHT*16
  1015. align 16
  1016. screen4                 rb IMG_WIDTH*IMG_HEIGHT*4
  1017. align 16
  1018. xxmm8:
  1019.                         rd 4
  1020. memStack:
  1021.                         rd 65536
  1022. align 16
  1023. I_END:
  1024.