Advertisement
XProger_san

rasterizeG_asm

Mar 13th, 2021
2,456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 6.20 KB | None | 0 0
  1. #include "rasterizer.inc"
  2.  
  3. .global rasterizeG_asm
  4. rasterizeG_asm:
  5.     pixel   .req r0
  6.     index   .req r1
  7.     L       .req r2
  8.     R       .req r3
  9.  
  10.     DIVLUT  .req r4
  11.     tmp     .req r5
  12.     N       .req r6
  13.     Lh      .req r7
  14.     Rh      .req r8
  15.  
  16.     Lx      .req ip
  17.     Rx      .req lr
  18.     Lg      .req r9
  19.     Rg      .req r10
  20.     h       .req r11
  21.  
  22.     Ldx     .req h
  23.     Rdx     .req Ldx
  24.  
  25.     Ldg     .req Ldx
  26.     Rdg     .req Ldx
  27.  
  28.     color   .req N
  29.     Ry1     .req tmp
  30.     Ry2     .req Rh
  31.     Ly1     .req tmp
  32.     Ly2     .req Lh
  33.     LMAP    .req index
  34.  
  35.     PMAP    .req Lh
  36.     width   .req Rh
  37.     g       .req L
  38.     dgdx    .req R
  39.  
  40.     SP_LDX = 0
  41.     SP_LDG = 4
  42.     SP_RDX = 8
  43.     SP_RDG = 12
  44.  
  45.     stmfd sp!, {r4,r5,r6,r7,r8,r9,r10,r11,lr}
  46.     sub sp, #16 // reserve stack space for [Ldx, Ldg, Rdx, Rdg]
  47.  
  48.     ldr tmp, =lightmap
  49.     add LMAP, index, tmp  // LMAP = lightmap + index
  50.  
  51.     mov Lh, #0            // Lh = 0
  52.     mov Rh, #0            // Rh = 0
  53.  
  54. .loop:
  55.     ldr DIVLUT, =divTable
  56.  
  57.     .calc_left_start:
  58.         cmp Lh, #0
  59.           bne .calc_left_end        // if (Lh != 0) end with left
  60.         ldr N, [L, #VERTEX_PREV]    // N = L->prev
  61.         ldrsh Ly1, [L, #VERTEX_Y]   // Ly1 = L->v.y
  62.         ldrsh Ly2, [N, #VERTEX_Y]   // Ly2 = N->v.y
  63.         subs Lh, Ly2, Ly1           // Lh = Ly2 - Ly1
  64.           blt .exit                 // if (Lh < 0) return
  65.         ldrsh Lx, [L, #VERTEX_X]    // Lx = L->v.x
  66.         ldrb Lg, [L, #VERTEX_G]     // Lg = L->v.g
  67.         cmp Lh, #1                  // if (Lh <= 1) skip Ldx calc
  68.           ble .skip_left_dx
  69.         lsl tmp, Lh, #1
  70.         ldrh tmp, [DIVLUT, tmp]     // tmp = FixedInvU(Lh)
  71.  
  72.         ldrsh Ldx, [N, #VERTEX_X]
  73.         sub Ldx, Lx
  74.         mul Ldx, tmp                // Ldx = tmp * (N->v.x - Lx)
  75.         str Ldx, [sp, #SP_LDX]      // store Ldx to stack
  76.  
  77.         ldrb Ldg, [N, #VERTEX_G]
  78.         sub Ldg, Lg
  79.         mul Ldg, tmp                // Ldg = tmp * (N->v.g - Lg)
  80.         str Ldg, [sp, #SP_LDG]      // store Ldg to stack
  81.  
  82.         .skip_left_dx:
  83.         lsl Lx, #16                 // Lx <<= 16
  84.         lsl Lg, #16                 // Lg <<= 16
  85.         mov L, N                    // L = N
  86.         b .calc_left_start
  87.     .calc_left_end:
  88.  
  89.     .calc_right_start:
  90.         cmp Rh, #0
  91.           bne .calc_right_end       // if (Rh != 0) end with right
  92.         ldr N, [R, #VERTEX_NEXT]    // N = R->next
  93.         ldrsh Ry1, [R, #VERTEX_Y]   // Ry1 = R->v.y
  94.         ldrsh Ry2, [N, #VERTEX_Y]   // Ry2 = N->v.y
  95.         subs Rh, Ry2, Ry1           // Rh = Ry2 - Ry1
  96.           blt .exit                 // if (Rh < 0) return
  97.         ldrsh Rx, [R, #VERTEX_X]    // Rx = R->v.x
  98.         ldrb Rg, [R, #VERTEX_G]     // Rg = R->v.g
  99.         cmp Rh, #1                  // if (Rh <= 1) skip Rdx calc
  100.           ble .skip_right_dx
  101.         lsl tmp, Rh, #1
  102.         ldrh tmp, [DIVLUT, tmp]     // tmp = FixedInvU(Rh)
  103.  
  104.         ldrsh Rdx, [N, #VERTEX_X]
  105.         sub Rdx, Rx
  106.         mul Rdx, tmp                // Rdx = tmp * (N->v.x - Rx)
  107.         str Rdx, [sp, #SP_RDX]      // store Rdx to stack
  108.  
  109.         ldrb Rdg, [N, #VERTEX_G]
  110.         sub Rdg, Rg
  111.         mul Rdg, tmp                // Rdg = tmp * (N->v.g - Rg)
  112.         str Rdg, [sp, #SP_RDG]      // store Rdg to stack
  113.  
  114.         .skip_right_dx:
  115.         lsl Rx, #16                 // Rx <<= 16
  116.         lsl Rg, #16                 // Rg <<= 16
  117.         mov R, N                    // R = N
  118.         b .calc_right_start
  119.     .calc_right_end:
  120.  
  121.     cmp Rh, Lh              // if (Rh < Lh)
  122.       movlt h, Rh           //      h = Rh
  123.       movge h, Lh           // else h = Lh
  124.     sub Lh, h               // Lh -= h
  125.     sub Rh, h               // Rh -= h
  126.          
  127.     stmfd sp!, {L,R,Lh,Rh}  // sp-16
  128.  
  129.     ldr PMAP, =palette
  130.  
  131. .scanline_start:
  132.     asr tmp, Lx, #16                // x1 = (Lx >> 16)
  133.     rsbs width, tmp, Rx, asr #16    // width = (Rx >> 16) - x1
  134.       ble .scanline_end             // if (width <= 0) go next scanline
  135.  
  136.     sub dgdx, Rg, Lg
  137.     asr dgdx, #5                    // dgdx = (Rg - Lg) >> 5
  138.     lsl g, width, #1
  139.     ldrh g, [DIVLUT, g]
  140.     mul dgdx, g                     // dgdx *= FixedInvU(width)
  141.     asr dgdx, #10                   // dgdx >>= 10    
  142.     mov g, Lg                       // g = Lg
  143.  
  144.     add tmp, pixel, tmp, lsl #1     // tmp = pixel + x1
  145.  
  146. .scanline: // 8px per iteration
  147.   // 2px
  148.     lsr color, g, #8
  149.     and color, #0x1F00
  150.     ldrb color, [LMAP, color]
  151.     lsl color, #1
  152.     ldrh color, [PMAP, color]
  153.  
  154.     strh color, [tmp], #2
  155.     subs width, #1
  156.       beq .scanline_end
  157.  
  158.     strh color, [tmp], #2
  159.     subs width, #1
  160.       beq .scanline_end
  161.     add g, dgdx
  162.    
  163.   // 2px
  164.     lsr color, g, #8
  165.     and color, #0x1F00
  166.     ldrb color, [LMAP, color]
  167.     lsl color, #1
  168.     ldrh color, [PMAP, color]
  169.  
  170.     strh color, [tmp], #2
  171.     subs width, #1
  172.       beq .scanline_end
  173.  
  174.     strh color, [tmp], #2
  175.     subs width, #1
  176.       beq .scanline_end
  177.     add g, dgdx
  178.  
  179.   // 2px
  180.     lsr color, g, #8
  181.     and color, #0x1F00
  182.     ldrb color, [LMAP, color]
  183.     lsl color, #1
  184.     ldrh color, [PMAP, color]
  185.  
  186.     strh color, [tmp], #2
  187.     subs width, #1
  188.       beq .scanline_end
  189.  
  190.     strh color, [tmp], #2
  191.     subs width, #1
  192.       beq .scanline_end
  193.     add g, dgdx
  194.  
  195.   // 2px
  196.     lsr color, g, #8
  197.     and color, #0x1F00
  198.     ldrb color, [LMAP, color]
  199.     lsl color, #1
  200.     ldrh color, [PMAP, color]
  201.  
  202.     strh color, [tmp], #2
  203.     subs width, #1
  204.       beq .scanline_end
  205.  
  206.     strh color, [tmp], #2
  207.     add g, dgdx
  208.     subs width, #1
  209.       bne .scanline      
  210.  
  211.  
  212. .scanline_end:
  213.     ldr tmp, [sp, #(SP_LDX + 16)]
  214.     add Lx, tmp           // Lx += Ldx from stack
  215.  
  216.     ldr tmp, [sp, #(SP_LDG + 16)]
  217.     add Lg, tmp           // Lg += Ldg from stack
  218.  
  219.     ldr tmp, [sp, #(SP_RDX + 16)]
  220.     add Rx, tmp           // Rx += Rdx from stack
  221.  
  222.     ldr tmp, [sp, #(SP_RDG + 16)]
  223.     add Rg, tmp           // Rg += Rdg from stack
  224.  
  225.     add pixel, #320       // pixel += FRAME_WIDTH (160)
  226.  
  227.     subs h, #1
  228.       bne .scanline_start
  229.  
  230.     ldmfd sp!, {L,R,Lh,Rh}      // sp+16
  231.     b .loop
  232.  
  233. .exit:
  234.     add sp, #16                 // revert reserved space for [Ldx, Ldg, Rdx, Rdg]
  235.     ldmfd sp!, {r4,r5,r6,r7,r8,r9,r10,r11,pc}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement