;;-*-Lisp-*-
(in-package goal)

;; name: sprite-glow.gc
;; name in dgo: sprite-glow
;; dgos: ENGINE, GAME

;; DECOMP BEGINS

(deftype sprite-glow-template (structure)
  ((clear-init-giftag    gs-gif-tag      :inline)
   (clear-init-adcmds    gs-adcmd        5 :inline)
   (clear-draw-giftag    gs-gif-tag      :inline)
   (clear-draw-clr-0     gs-packed-rgba  :inline)
   (clear-draw-xyz-0     gs-packed-xyzw  2 :inline)
   (clear-draw-clr-1     gs-packed-rgba  :inline)
   (clear-draw-xyz-1     vector          2 :inline)
   (offscr-setup-giftag  gs-gif-tag      :inline)
   (offscr-setup-adcmds  gs-adcmd        8 :inline)
   (offscr-first-giftag  gs-gif-tag      :inline)
   (offscr-first-clr     gs-packed-rgba  :inline)
   (offscr-first-uv-0    gs-packed-uv    :inline)
   (offscr-first-xyzw-0  gs-packed-xyzw  :inline)
   (offscr-first-uv-1    gs-packed-uv    :inline)
   (offscr-first-xyzw-1  gs-packed-xyzw  :inline)
   (repeat-draw-giftag   gs-gif-tag      :inline)
   (repeat-draw-adcmds   gs-adcmd        29 :inline)
   (flare-alpha-giftag   gs-gif-tag      :inline)
   (flare-alpha-clr      gs-packed-rgba  :inline)
   (flare-alpha-uv       gs-packed-uv    :inline)
   (flare-alpha-xyzw-0   gs-packed-xyzw  :inline)
   (flare-alpha-xyzw-1   gs-packed-xyzw  :inline)
   (flare-alpha-xyzw-2   gs-packed-xyzw  :inline)
   (flare-alpha-xyzw-3   gs-packed-xyzw  :inline)
   (flare-init-giftag    gs-gif-tag      :inline)
   (flare-init-adcmds    gs-adcmd        8 :inline)
   (flare-draw-giftag    gs-gif-tag      :inline)
   (flare-draw-clr       gs-packed-rgba  :inline)
   (flare-draw-stq-0     gs-packed-stq   :inline)
   (flare-draw-xyzw-0    gs-packed-xyzw  :inline)
   (flare-draw-stq-1     gs-packed-stq   :inline)
   (flare-draw-xyzw-1    gs-packed-xyzw  :inline)
   (flare-draw-stq-2     gs-packed-stq   :inline)
   (flare-draw-xyzw-2    gs-packed-xyzw  :inline)
   (flare-draw-stq-3     gs-packed-stq   :inline)
   (flare-draw-xyzw-3    gs-packed-xyzw  :inline)
   )
  )


(deftype sprite-glow-consts (structure)
  ((camera       matrix  :inline)
   (perspective  matrix  :inline)
   (hvdf-offset  vector  :inline)
   (hmge-scale   vector  :inline)
   (consts       vector  :inline)
   (pfog0        float   :overlay-at (-> consts data 0))
   (deg-to-rad   float   :overlay-at (-> consts data 1))
   (min-scale    float   :overlay-at (-> consts data 2))
   (inv-area     float   :overlay-at (-> consts data 3))
   (sincos-01    vector  :inline)
   (sincos-23    vector  :inline)
   (sincos-45    vector  :inline)
   (sincos-67    vector  :inline)
   (sincos-89    vector  :inline)
   (basis-x      vector  :inline)
   (basis-y      vector  :inline)
   (xy-array     vector  4 :inline)
   (clamp-min    vector  :inline)
   (clamp-max    vector  :inline)
   )
  )


(define *sprite-glow-template*
  (new 'static 'sprite-glow-template
    :clear-init-giftag (new 'static 'gs-gif-tag
      :tag (new 'static 'gif-tag64 :nloop #x5 :nreg #x1)
      :regs (new 'static 'gif-tag-regs :regs0 (gif-reg-id a+d))
      )
    :clear-init-adcmds (new 'static 'inline-array gs-adcmd 5
      (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
      ;; (new 'static 'gs-alpha :a 2 :b 2 :c 2 :d 1)
      (new 'static 'gs-adcmd :cmds (gs-reg64 alpha-1) :x #x6a)
      ;; (new 'static 'gs-test :ate 1 :afail 1 :zte 1 :ztst 2)
      (new 'static 'gs-adcmd :cmds (gs-reg64 test-1) :x #x51001)
      ;; (new 'static 'gs-zbuf :zbp 304 :psm 1 :zmsk 1)
      (new 'static 'gs-adcmd :cmds (gs-reg64 zbuf-1) :x #x1000130 :y #x1)
      ;; (new 'static 'gs-frame :fbp 408 :fbw 8 :fbmsk #xffffff)
      (new 'static 'gs-adcmd :cmds (gs-reg64 frame-1) :x #x80198 :y #xffffff)
      )
    :clear-draw-giftag (new 'static 'gs-gif-tag
      :tag (new 'static 'gif-tag64
        :nloop #x2
        :pre #x1
        :prim (new 'static 'gs-prim :prim (gs-prim-type sprite))
        :nreg #x3
        )
      :regs (new 'static 'gif-tag-regs :regs0 (gif-reg-id rgbaq) :regs1 (gif-reg-id xyzf2) :regs2 (gif-reg-id xyzf2))
      )
    :clear-draw-xyz-0 (new 'static 'inline-array gs-packed-xyzw 2
      (new 'static 'gs-packed-xyzw :iz -1)
      (new 'static 'gs-packed-xyzw :iz -1)
      )
    :clear-draw-clr-1 (new 'static 'gs-packed-rgba :x #xff :w #xff)
    :offscr-setup-giftag (new 'static 'gs-gif-tag
      :tag (new 'static 'gif-tag64 :nloop #x8 :nreg #x1)
      :regs (new 'static 'gif-tag-regs :regs0 (gif-reg-id a+d))
      )
    :offscr-setup-adcmds (new 'static 'inline-array gs-adcmd 8
      ;; (new 'static 'gs-frame :fbp 144 :fbw 1 :fbmsk #xffffff)
      (new 'static 'gs-adcmd :cmds (gs-reg64 frame-2) :x #x10090 :y #xffffff)
      ;; (new 'static 'gs-zbuf :zbp 304 :psm 1)
      (new 'static 'gs-adcmd :cmds (gs-reg64 zbuf-2) :x #x1000130)
      ;; mmag/mmin on
      (new 'static 'gs-adcmd :cmds (gs-reg64 tex1-2) :x #x60)
      ;; (new 'static 'gs-tex0 :tbp0 #x3300 :tbw 8 :tw 9 :th 9 :tcc 1 :tfx 1)
      (new 'static 'gs-adcmd :cmds (gs-reg64 tex0-2) :x #x64023300 :y #xe)
      ;; wms, wmt both 1
      (new 'static 'gs-adcmd :cmds (gs-reg64 clamp-2) :x #x5)
      ;; ate 1, afail 1, zte 1, ztst 1
      (new 'static 'gs-adcmd :cmds (gs-reg64 test-2) :x #x31001)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyoffset-2))
      (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
      )
    :offscr-first-giftag (new 'static 'gs-gif-tag
      :tag (new 'static 'gif-tag64
        :nloop #x1
        :pre #x1
        :prim (new 'static 'gs-prim :prim (gs-prim-type sprite) :tme #x1 :fst #x1 :ctxt #x1)
        :nreg #x5
        )
      :regs (new 'static 'gif-tag-regs
        :regs0 (gif-reg-id rgbaq)
        :regs1 (gif-reg-id uv)
        :regs2 (gif-reg-id xyz2)
        :regs3 (gif-reg-id uv)
        :regs4 (gif-reg-id xyz2)
        )
      )
    :offscr-first-clr (new 'static 'gs-packed-rgba :x #x80 :y #x80 :z #x80 :w #x80)
    :offscr-first-xyzw-1 (new 'static 'gs-packed-xyzw :ix #x200 :iy #x200)
    :repeat-draw-giftag (new 'static 'gs-gif-tag
      :tag (new 'static 'gif-tag64 :nloop #x1d :nreg #x1)
      :regs (new 'static 'gif-tag-regs :regs0 (gif-reg-id a+d))
      )
    :repeat-draw-adcmds (new 'static 'inline-array gs-adcmd 29
      ;; tbp: 0x1200, tbw 1, tw = 5, th = 5, tcc = 1, tfx = 1
      (new 'static 'gs-adcmd :cmds (gs-reg64 tex0-2) :x #x54005200 :y #xd)
      (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
      (new 'static 'gs-adcmd :cmds (gs-reg64 uv) :x #x100010)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyz2))
      (new 'static 'gs-adcmd :cmds (gs-reg64 uv) :x #x2100210)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyz2) :x #x1000100)
      (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
      (new 'static 'gs-adcmd :cmds (gs-reg64 uv) :x #x100010)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyz2))
      (new 'static 'gs-adcmd :cmds (gs-reg64 uv) :x #x1100110)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyz2) :x #x800080)
      (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
      (new 'static 'gs-adcmd :cmds (gs-reg64 uv) :x #x100010)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyz2))
      (new 'static 'gs-adcmd :cmds (gs-reg64 uv) :x #x900090)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyz2) :x #x400040)
      (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
      (new 'static 'gs-adcmd :cmds (gs-reg64 uv) :x #x100010)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyz2))
      (new 'static 'gs-adcmd :cmds (gs-reg64 uv) :x #x500050)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyz2) :x #x200020)
      ;; fbp = 408, fbw = 8 (normal screen drawing)
      (new 'static 'gs-adcmd :cmds (gs-reg64 frame-2) :x #x80198 :y #xffffff)
      ;; (new 'static 'gs-test :ate 1 :afail 1 :zte 1 :ztst 2)
      (new 'static 'gs-adcmd :cmds (gs-reg64 test-2) :x #x51001)
      ;; (new 'static 'gs-zbuf :zbp 304 :psm 1 :zmsk 1)
      (new 'static 'gs-adcmd :cmds (gs-reg64 zbuf-2) :x #x1000130 :y #x1)
      (new 'static 'gs-adcmd :cmds (gs-reg64 xyoffset-2) :x #x7000 :y #x7300)
      (new 'static 'gs-adcmd :cmds (gs-reg64 scissor-2) :x #x1ff0000 :y #x19f0000)
      ;; zte = 1, ztst = 2
      (new 'static 'gs-adcmd :cmds (gs-reg64 test-2) :x #x50000)
      ;; (new 'static 'gs-alpha :a 2 :b 2 :c 2 :d 1)
      (new 'static 'gs-adcmd :cmds (gs-reg64 alpha-2) :x #x6a)
      (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
      )
    :flare-alpha-giftag (new 'static 'gs-gif-tag
      :tag (new 'static 'gif-tag64
        :nloop #x1
        :pre #x1
        :prim (new 'static 'gs-prim :prim (gs-prim-type tri-fan) :tme #x1 :fst #x1 :ctxt #x1)
        :nreg #x6
        )
      :regs (new 'static 'gif-tag-regs
        :regs0 (gif-reg-id rgbaq)
        :regs1 (gif-reg-id uv)
        :regs2 (gif-reg-id xyzf2)
        :regs3 (gif-reg-id xyzf2)
        :regs4 (gif-reg-id xyzf2)
        :regs5 (gif-reg-id xyzf2)
        )
      )
    :flare-alpha-clr (new 'static 'gs-packed-rgba :x #x80 :y #x80 :z #x80 :w #x80)
    :flare-alpha-uv (new 'static 'gs-packed-uv :x (the-as float #x10) :y (the-as float #x10))
    :flare-alpha-xyzw-0 (new 'static 'gs-packed-xyzw :iz -1)
    :flare-alpha-xyzw-1 (new 'static 'gs-packed-xyzw :iz -1)
    :flare-alpha-xyzw-2 (new 'static 'gs-packed-xyzw :iz -1)
    :flare-alpha-xyzw-3 (new 'static 'gs-packed-xyzw :iz -1)
    :flare-init-giftag (new 'static 'gs-gif-tag
      :tag (new 'static 'gif-tag64 :nloop #x8 :nreg #x1)
      :regs (new 'static 'gif-tag-regs :regs0 (gif-reg-id a+d))
      )
    :flare-init-adcmds (new 'static 'inline-array gs-adcmd 8
      (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
      ;; normal frame, no mask
      (new 'static 'gs-adcmd :cmds (gs-reg64 frame-1) :x #x80198)
      (new 'static 'gs-adcmd :cmds (gs-reg64 hack))
      (new 'static 'gs-adcmd :cmds (gs-reg64 hack))
      (new 'static 'gs-adcmd :cmds (gs-reg64 hack))
      (new 'static 'gs-adcmd :cmds (gs-reg64 hack))
      (new 'static 'gs-adcmd :cmds (gs-reg64 hack))
      ;; a = 0, b = 2, c = 1, d = 1
      (new 'static 'gs-adcmd :cmds (gs-reg64 alpha-1) :x #x58)
      )
    :flare-draw-giftag (new 'static 'gs-gif-tag
      :tag (new 'static 'gif-tag64
        :nloop #x1
        :eop #x1
        :pre #x1
        :prim (new 'static 'gs-prim :prim (gs-prim-type tri-fan) :tme #x1 :abe #x1)
        :nreg #x9
        )
      :regs (new 'static 'gif-tag-regs
        :regs0 (gif-reg-id rgbaq)
        :regs1 (gif-reg-id st)
        :regs2 (gif-reg-id xyzf2)
        :regs3 (gif-reg-id st)
        :regs4 (gif-reg-id xyzf2)
        :regs5 (gif-reg-id st)
        :regs6 (gif-reg-id xyzf2)
        :regs7 (gif-reg-id st)
        :regs8 (gif-reg-id xyzf2)
        )
      )
    :flare-draw-clr (new 'static 'gs-packed-rgba :y 64 :w #x80)
    :flare-draw-stq-0 (new 'static 'gs-packed-stq :z 1.0)
    :flare-draw-xyzw-0 (new 'static 'gs-packed-xyzw :iz -1)
    :flare-draw-stq-1 (new 'static 'gs-packed-stq :x 1.0 :z 1.0)
    :flare-draw-xyzw-1 (new 'static 'gs-packed-xyzw :iz -1)
    :flare-draw-stq-2 (new 'static 'gs-packed-stq :x 1.0 :y 1.0 :z 1.0)
    :flare-draw-xyzw-2 (new 'static 'gs-packed-xyzw :iz -1)
    :flare-draw-stq-3 (new 'static 'gs-packed-stq :y 1.0 :z 1.0)
    :flare-draw-xyzw-3 (new 'static 'gs-packed-xyzw :iz -1)
    )
  )

(define sprite-glow-vu1-block (new 'static 'vu-function))

;; WARN: Return type mismatch vector vs none.
(defun sprite-glow-init-consts ((arg0 sprite-glow-consts))
  (let ((v1-0 *math-camera*))
    (let* ((t0-0 (-> arg0 camera))
           (t1-0 (-> v1-0 camera-rot))
           (a1-0 (-> t1-0 quad 0))
           (a2-0 (-> t1-0 quad 1))
           (a3-0 (-> t1-0 quad 2))
           (t1-1 (-> t1-0 trans quad))
           )
      (set! (-> t0-0 quad 0) a1-0)
      (set! (-> t0-0 quad 1) a2-0)
      (set! (-> t0-0 quad 2) a3-0)
      (set! (-> t0-0 trans quad) t1-1)
      )
    (let* ((t0-1 (-> arg0 perspective))
           (t1-2 (-> v1-0 perspective))
           (a1-1 (-> t1-2 quad 0))
           (a2-1 (-> t1-2 quad 1))
           (a3-1 (-> t1-2 quad 2))
           (t1-3 (-> t1-2 trans quad))
           )
      (set! (-> t0-1 quad 0) a1-1)
      (set! (-> t0-1 quad 1) a2-1)
      (set! (-> t0-1 quad 2) a3-1)
      (set! (-> t0-1 trans quad) t1-3)
      )
    (set! (-> arg0 hvdf-offset quad) (-> v1-0 hvdf-off quad))
    (set! (-> arg0 hmge-scale quad) (-> v1-0 hmge-scale quad))
    (set! (-> arg0 basis-x quad) (the-as uint128 0))
    (set! (-> arg0 basis-x x) (- (-> *math-camera* perspective vector 0 x)))
    (set! (-> arg0 basis-y quad) (the-as uint128 0))
    (set! (-> arg0 basis-y y) (- (-> *math-camera* perspective vector 1 y)))
    (set! (-> arg0 pfog0) (-> v1-0 pfog0))
    )
  (set! (-> arg0 deg-to-rad) 0.000095873795)
  (set! (-> arg0 min-scale) (sqrtf (* (/ 1.0 (-> arg0 basis-x x)) (/ 1.0 (-> arg0 basis-y y)))))
  (set! (-> arg0 inv-area) (/ 1.0 (* (-> arg0 min-scale) (-> arg0 min-scale))))
  (let ((v1-5 (-> arg0 sincos-01)))
    (set! (-> v1-5 z) 0.999998)
    (set! (-> v1-5 w) 1.0)
    )
  (let ((v1-6 (-> arg0 sincos-23)))
    (set! (-> v1-6 z) -0.16666014)
    (set! (-> v1-6 w) -0.49998003)
    )
  (let ((v1-7 (-> arg0 sincos-45)))
    (set! (-> v1-7 z) 0.008326521)
    (set! (-> v1-7 w) 0.041620404)
    )
  (let ((v1-8 (-> arg0 sincos-67)))
    (set! (-> v1-8 z) -0.0001956241)
    (set! (-> v1-8 w) -0.0013636408)
    )
  (let ((v1-9 (-> arg0 sincos-89)))
    (set! (-> v1-9 z) 0.0000023042373)
    (set! (-> v1-9 w) 0.000020170546)
    )
  (set-vector! (-> arg0 xy-array 0) -0.5 -0.5 0.0 0.0)
  (set-vector! (-> arg0 xy-array 1) 0.5 -0.5 0.0 0.0)
  (set-vector! (-> arg0 xy-array 2) 0.5 0.5 0.0 0.0)
  (set-vector! (-> arg0 xy-array 3) -0.5 0.5 0.0 0.0)
  (set-vector! (-> arg0 clamp-min) 1792.0 1840.0 0.0 0.0)
  (set-vector! (-> arg0 clamp-max) 2304.0 2256.0 2048.0 50.0)
  (none)
  )

(defun sprite-glow-init-engine ((arg0 dma-buffer))
  (dma-buffer-add-vu-function arg0 sprite-glow-vu1-block 1)
  (let ((s5-0 24))
    (let* ((v1-0 arg0)
           (a0-2 (the-as dma-packet (-> v1-0 base)))
           )
      (set! (-> a0-2 dma) (new 'static 'dma-tag :id (dma-tag-id cnt) :qwc s5-0))
      (set! (-> a0-2 vif0) (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl)))
      (set! (-> a0-2 vif1) (new 'static 'vif-tag :imm #x3d4 :cmd (vif-cmd unpack-v4-32) :num s5-0))
      (set! (-> v1-0 base) (the-as pointer (&+ a0-2 16)))
      )
    (sprite-glow-init-consts (the-as sprite-glow-consts (-> arg0 base)))
    (&+! (-> arg0 base) (* s5-0 16))
    )
  (let ((v1-3 84))
    (let* ((a0-6 arg0)
           (a1-6 (the-as dma-packet (-> a0-6 base)))
           )
      (set! (-> a1-6 dma)
            (new 'static 'dma-tag :id (dma-tag-id ref) :addr (the-as int *sprite-glow-template*) :qwc v1-3)
            )
      (set! (-> a1-6 vif0) (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl)))
      (set! (-> a1-6 vif1) (new 'static 'vif-tag :imm #x320 :cmd (vif-cmd unpack-v4-32) :num v1-3))
      (set! (-> a0-6 base) (the-as pointer (&+ a1-6 16)))
      )
    (let* ((a0-7 arg0)
           (a1-8 (the-as dma-packet (-> a0-7 base)))
           )
      (set! (-> a1-8 dma)
            (new 'static 'dma-tag :id (dma-tag-id ref) :addr (the-as int *sprite-glow-template*) :qwc v1-3)
            )
      (set! (-> a1-8 vif0) (new 'static 'vif-tag :cmd (vif-cmd mscal) :msk #x1 :imm #x0))
      (set! (-> a1-8 vif1) (new 'static 'vif-tag :imm #x374 :cmd (vif-cmd unpack-v4-32) :num v1-3))
      (set! (-> a0-7 base) (the-as pointer (&+ a1-8 16)))
      )
    )
  (let* ((v1-8 arg0)
         (a0-8 (the-as dma-packet (-> v1-8 base)))
         )
    (set! (-> a0-8 dma) (new 'static 'dma-tag :id (dma-tag-id cnt)))
    (set! (-> a0-8 vif0) (new 'static 'vif-tag :cmd (vif-cmd base)))
    (set! (-> a0-8 vif1) (new 'static 'vif-tag :imm #x190 :cmd (vif-cmd offset)))
    (set! (-> v1-8 base) (the-as pointer (&+ a0-8 16)))
    )
  (let ((v1-9 (the-as dma-packet (-> arg0 base))))
    (set! (-> v1-9 dma) (new 'static 'dma-tag :id (dma-tag-id cnt)))
    (set! (-> v1-9 vif0) (new 'static 'vif-tag))
    (set! (-> v1-9 vif1) (new 'static 'vif-tag :cmd (vif-cmd flushe) :msk #x1))
    (set! (-> arg0 base) (the-as pointer (&+ v1-9 16)))
    )
  0
  (none)
  )

(deftype sprite-glow-dma-packet-data (structure)
  ((control-packet     dma-packet  :inline)
   (vecdata-packet     dma-packet  :inline)
   (shader-cnt-packet  dma-packet  :inline)
   (shader-ref-packet  dma-packet  :inline)
   (mscal-packet       dma-packet  :inline)
   )
  )


(deftype sprite-glow-cnt-template (structure)
  ((control-packet    dma-packet        :inline)
   (num-sprites       uint32)
   (dummys            uint32            3)
   (num-sprites-quad  uint128           :overlay-at num-sprites)
   (vecdata-packet    dma-packet        :inline)
   (vecdata           sprite-glow-data  :inline)
   (shader-packet     dma-packet        :inline)
   (shader            adgif-shader      :inline)
   (mscal-packet      dma-packet        :inline)
   )
  )


(deftype sprite-glow-ref-template (structure)
  ((control-packet     dma-packet        :inline)
   (num-sprites        uint32)
   (dummys             uint32            3)
   (num-sprites-quad   uint128           :overlay-at num-sprites)
   (vecdata-packet     dma-packet        :inline)
   (vecdata            sprite-glow-data  :inline)
   (shader-packet      dma-packet        :inline)
   (shader-packet-ptr  pointer           :offset 116)
   (mscal-packet       dma-packet        :inline)
   )
  )


(define *sprite-glow-dma-packet-data*
  (new 'static 'sprite-glow-dma-packet-data
    :control-packet (new 'static 'dma-packet
      :dma (new 'static 'dma-tag :qwc #x1 :id (dma-tag-id cnt))
      :vif0 (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl))
      :vif1 (new 'static 'vif-tag :imm #x8000 :num #x1 :cmd (vif-cmd unpack-v4-32))
      )
    :vecdata-packet (new 'static 'dma-packet
      :dma (new 'static 'dma-tag :qwc #x4 :id (dma-tag-id cnt))
      :vif0 (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl))
      :vif1 (new 'static 'vif-tag :imm #x8001 :num #x4 :cmd (vif-cmd unpack-v4-32))
      )
    :shader-cnt-packet (new 'static 'dma-packet
      :dma (new 'static 'dma-tag :qwc #x5 :id (dma-tag-id cnt))
      :vif0 (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl))
      :vif1 (new 'static 'vif-tag :imm #x8091 :num #x5 :cmd (vif-cmd unpack-v4-32))
      )
    :shader-ref-packet (new 'static 'dma-packet
      :dma (new 'static 'dma-tag :qwc #x5 :id (dma-tag-id ref))
      :vif0 (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl))
      :vif1 (new 'static 'vif-tag :imm #x8091 :num #x5 :cmd (vif-cmd unpack-v4-32))
      )
    :mscal-packet (new 'static 'dma-packet
      :dma (new 'static 'dma-tag :id (dma-tag-id cnt))
      :vif1 (new 'static 'vif-tag :cmd (vif-cmd flushe) :msk #x1)
      )
    )
  )

(set! (-> *sprite-glow-dma-packet-data* mscal-packet vif0)
      (new 'static 'vif-tag :cmd (vif-cmd mscalf) :msk #x1 :imm #xa)
      )

;; WARN: Return type mismatch sprite-glow-cnt-template vs none.
(defun sprite-glow-add-sprite ((arg0 dma-buffer) (arg1 sprite-vec-data-2d) (arg2 float) (arg3 float) (arg4 float) (arg5 adgif-shader))
  (let ((v1-0 (the-as sprite-glow-cnt-template (-> arg0 base))))
    (let* ((t5-0 *sprite-glow-dma-packet-data*)
           (t2-0 (-> t5-0 control-packet quad))
           (t3-0 (-> t5-0 vecdata-packet quad))
           (t4-0 (-> t5-0 shader-cnt-packet quad))
           (t5-1 (-> t5-0 mscal-packet quad))
           )
      (set! (-> v1-0 control-packet quad) t2-0)
      (set! (-> v1-0 num-sprites-quad) (the-as uint128 0))
      (set! (-> v1-0 vecdata-packet quad) t3-0)
      (set! (-> v1-0 shader-packet quad) t4-0)
      (set! (-> v1-0 mscal-packet quad) t5-1)
      )
    (let ((t2-1 (-> arg1 x-y-z-sx quad))
          (t3-1 (-> arg1 flag-rot-sy quad))
          (a1-1 (-> arg1 r-g-b-a quad))
          )
      (set! (-> v1-0 vecdata position quad) t2-1)
      (set! (-> v1-0 vecdata quads 1 quad) t3-1)
      (set! (-> v1-0 vecdata color quad) a1-1)
      )
    (let ((a1-2 1))
      (set! (-> v1-0 vecdata z-offset) arg4)
      (set! (-> v1-0 vecdata fade-a) arg2)
      (set! (-> v1-0 vecdata fade-b) arg3)
      (set! (-> v1-0 num-sprites) (the-as uint a1-2))
      )
    (let ((a1-3 (-> arg5 quad 0 quad))
          (a2-1 (-> arg5 quad 1 quad))
          (a3-1 (-> arg5 quad 2 quad))
          (t0-1 (-> arg5 quad 3 quad))
          (t1-1 (-> arg5 quad 4 quad))
          )
      (set! (-> v1-0 shader quad 0 quad) a1-3)
      (set! (-> v1-0 shader quad 1 quad) a2-1)
      (set! (-> v1-0 shader quad 2 quad) a3-1)
      (set! (-> v1-0 shader quad 3 quad) t0-1)
      (set! (-> v1-0 shader quad 4 quad) t1-1)
      )
    (set! (-> arg0 base) (the-as pointer (&+ v1-0 224)))
    )
  (none)
  )

;; WARN: Return type mismatch sprite-glow-ref-template vs none.
(defun sprite-glow-add-simple-sprite ((arg0 dma-buffer) (arg1 sprite-glow-dma-packet-data) (arg2 sprite-glow-data) (arg3 pointer))
  (let ((v1-0 (the-as sprite-glow-ref-template (-> arg0 base))))
    (let ((t0-0 (-> arg1 control-packet quad))
          (t1-0 (-> arg1 vecdata-packet quad))
          (t2-0 (-> arg1 shader-ref-packet quad))
          (a1-1 (-> arg1 mscal-packet quad))
          )
      (set! (-> v1-0 control-packet quad) t0-0)
      (set! (-> v1-0 num-sprites-quad) (the-as uint128 0))
      (set! (-> v1-0 vecdata-packet quad) t1-0)
      (set! (-> v1-0 shader-packet quad) t2-0)
      (set! (-> v1-0 mscal-packet quad) a1-1)
      )
    (let ((a1-2 (-> arg2 position quad))
          (t0-1 (-> arg2 quads 1 quad))
          (t1-1 (-> arg2 color quad))
          (a2-1 (-> arg2 quads 3 quad))
          (t2-1 1)
          )
      (set! (-> v1-0 vecdata position quad) a1-2)
      (set! (-> v1-0 vecdata quads 1 quad) t0-1)
      (set! (-> v1-0 vecdata color quad) t1-1)
      (set! (-> v1-0 vecdata quads 3 quad) a2-1)
      (set! (-> v1-0 num-sprites) (the-as uint t2-1))
      )
    (set! (-> v1-0 shader-packet-ptr) arg3)
    (set! (-> arg0 base) (the-as pointer (&+ v1-0 144)))
    )
  (none)
  )

(defun sprite-glow-draw ((arg0 dma-buffer))
  (local-vars (a2-0 float))
  (let* ((s5-0 *sprite-aux-list*)
         (s4-0 (-> s5-0 entry))
         (s3-0 #f)
         )
    (dotimes (s2-0 s4-0)
      (let ((v1-2 (-> s5-0 data s2-0)))
        (when (and (not s3-0) (= (-> v1-2 aux-type) (sprite-aux-type glow)))
          (let* ((a0-5 (-> v1-2 aux-data omega))
                 (a3-0 (cond
                         ((zero? a0-5)
                          (set! a2-0 0.0)
                          1.0
                          )
                         (else
                           (let* ((f1-1 (/ a0-5 METER_LENGTH))
                                  (f0-4 (- f1-1 (the float (the int f1-1))))
                                  )
                             (let ((f1-3 (* 4096.0 (- f1-1 f0-4))))
                               (set! a2-0 (/ -1.0 (* (- 1.0 f0-4) f1-3)))
                               )
                             (/ f0-4 (- 1.0 f0-4))
                             )
                           )
                         )
                       )
                 )
            (sprite-glow-add-sprite arg0 (-> v1-2 vec-data) a2-0 a3-0 (-> v1-2 aux-data user-float) (-> v1-2 gif-data))
            )
          )
        )
      )
    )
  0
  (none)
  )

(defmethod add! ((this simple-sprite-system) (arg0 sprite-glow-data))
  (let ((v1-0 (-> this count)))
    (when (< v1-0 (-> this max-count))
      (let* ((a2-3 (-> this data v1-0))
             (v1-2 arg0)
             (a1-1 a2-3)
             (a2-4 (-> v1-2 position quad))
             (a3-0 (-> v1-2 quads 1 quad))
             (t0-0 (-> v1-2 color quad))
             (v1-3 (-> v1-2 quads 3 quad))
             )
        (set! (-> a1-1 position quad) a2-4)
        (set! (-> a1-1 quads 1 quad) a3-0)
        (set! (-> a1-1 color quad) t0-0)
        (set! (-> a1-1 quads 3 quad) v1-3)
        )
      (+! (-> this count) 1)
      )
    )
  0
  (none)
  )

;; WARN: Return type mismatch pointer vs adgif-shader.
(defun add-shader-to-dma ((arg0 dma-buffer))
  (let* ((a1-0 (new 'static 'dma-packet :dma (new 'static 'dma-tag :id (dma-tag-id next))))
         (v1-0 (-> arg0 base))
         (a1-1 (-> a1-0 quad))
         (v0-0 (&+ v1-0 16))
         )
    (let ((a2-0 (&+ v1-0 96)))
      (set! (-> (the-as (pointer uint128) v1-0)) a1-1)
      (set! (-> (the-as (pointer uint32) v1-0) 1) (the-as uint a2-0))
      (set! (-> arg0 base) a2-0)
      )
    (the-as adgif-shader v0-0)
    )
  )

(defmethod draw-all-sprites! ((this simple-sprite-system) (arg0 dma-buffer))
  (local-vars (sv-528 sprite-glow-dma-packet-data) (sv-532 (pointer texture-id)) (sv-536 pointer))
  (b! (zero? (-> this count)) cfg-13 :delay (nop!))
  (set! sv-528 *sprite-glow-dma-packet-data*)
  (set! sv-532 (new 'stack-no-clear 'array 'texture-id 128))
  (set! sv-536 (-> arg0 base))
  ;; og:preserve-this
  (when (> (-> this count) 128)
    (format 0 "TOO MANY SPRITES (~D)~%" (-> this count))
    (break!)
    )
  (dotimes (v1-5 (-> this count))
    (set! (-> sv-532 v1-5) (-> this data v1-5 tex-id))
    )
  (countdown (s4-0 (-> this count))
    (let ((s3-0 (-> sv-532 s4-0)))
      (when (nonzero? s3-0)
        (let ((s2-0 (add-shader-to-dma arg0)))
          (adgif-shader<-texture-simple! s2-0 (lookup-texture-by-id s3-0))
          (countdown (s1-1 (+ s4-0 1))
            (when (= s3-0 (-> sv-532 s1-1))
              (set! (-> sv-532 s1-1) (new 'static 'texture-id))
              (let ((a2-1 (-> this data s1-1)))
                (sprite-glow-add-simple-sprite arg0 sv-528 a2-1 (the-as pointer s2-0))
                )
              )
            )
          )
        )
      )
    )
  (label cfg-13)
  0
  (none)
  )

(defmethod clear! ((this simple-sprite-system))
  (set! (-> this count) 0)
  0
  (none)
  )

(define *simple-sprite-system*
  (new 'static 'simple-sprite-system :max-count #x80 :data (new 'static 'inline-array sprite-glow-data 128
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             (new 'static 'sprite-glow-data)
                                                             )
                                     )
  )
