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

;; name: shadow-vu1.gc
;; name in dgo: shadow-vu1
;; dgos: ENGINE, GAME

;; DECOMP BEGINS

(deftype shadow-vu1-constants (structure)
  ((hmgescale  vector      :inline)
   (invhscale  vector      :inline)
   (texoffset  vector      :inline)
   (texscale   vector      :inline)
   (hvdfoff    vector      :inline)
   (fog        vector      :inline)
   (clrs       vector      2 :inline)
   (adgif      gs-gif-tag  :inline)
   (texflush   gs-adcmd    :inline)
   (flush      gs-adcmd    :inline)
   (trigif     gs-gif-tag  :inline)
   (quadgif    gs-gif-tag  :inline)
   )
  )


(deftype shadow-vu1-data (structure)
  ((adgif      gs-gif-tag  :inline)
   (ad         gs-adcmd    :inline)
   (flush      gs-adcmd    :inline)
   (trigif     gs-gif-tag  :inline)
   (quadgif    gs-gif-tag  :inline)
   (texoffset  vector      :inline)
   (texscale   vector      :inline)
   (clrs       qword       2 :inline)
   )
  )


(define *shadow-vu1-data* (new 'static 'shadow-vu1-data
                            :adgif (new 'static 'gs-gif-tag
                              :tag (new 'static 'gif-tag64 :nloop #x1 :nreg #x1)
                              :regs (new 'static 'gif-tag-regs :regs0 (gif-reg-id a+d))
                              )
                            :ad (new 'static 'gs-adcmd :cmds (gs-reg64 texflush))
                            :flush (new 'static 'gs-adcmd :cmds (gs-reg64 rgbaq) :y #x3f800000)
                            :trigif (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)
                                :nreg #x7
                                )
                              :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)
                                )
                              )
                            :quadgif (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)
                                :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)
                                )
                              )
                            :texoffset (new 'static 'vector :x 256.5 :y 208.5)
                            :texscale (new 'static 'vector :x 0.001953125 :y 0.001953125)
                            :clrs (new 'static 'inline-array qword 2
                              (new 'static 'qword :data (new 'static 'array uint32 4 #x80 #x0 #x0 #x82))
                              (new 'static 'qword :data (new 'static 'array uint32 4 #x0 #x80 #x0 #x7f))
                              )
                            )
        )

(define shadow-vu1-block (new 'static 'vu-function #|:length #x2e9 :qlength #x175|#))

;; WARN: Return type mismatch dma-buffer vs none.
(defun shadow-vu1-add-constants ((arg0 dma-buffer) (arg1 int))
  (let* ((a3-0 13)
         (v1-0 arg0)
         (a2-0 (the-as object (-> v1-0 base)))
         )
    (set! (-> (the-as dma-packet a2-0) dma) (new 'static 'dma-tag :id (dma-tag-id cnt) :qwc a3-0))
    (set! (-> (the-as dma-packet a2-0) vif0) (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl)))
    (set! (-> (the-as dma-packet a2-0) vif1)
          (new 'static 'vif-tag :imm #x370 :cmd (vif-cmd unpack-v4-32) :num a3-0)
          )
    (set! (-> v1-0 base) (the-as pointer (&+ (the-as dma-packet a2-0) 16)))
    )
  (let ((v1-1 (the-as object (-> arg0 base))))
    (let ((a3-4 *math-camera*)
          (a2-2 *shadow-vu1-data*)
          )
      (set! (-> (the-as shadow-vu1-constants v1-1) hmgescale quad) (-> a3-4 hmge-scale quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) invhscale quad) (-> a3-4 inv-hmge-scale quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) hvdfoff quad) (-> a3-4 hvdf-off quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) hvdfoff x) 2048.0)
      (set! (-> (the-as shadow-vu1-constants v1-1) hvdfoff y) 2048.0)
      (set! (-> (the-as shadow-vu1-constants v1-1) fog x) (-> a3-4 pfog0))
      (set! (-> (the-as shadow-vu1-constants v1-1) clrs 0 quad) (-> a2-2 clrs 0 quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) clrs 1 quad) (-> a2-2 clrs 1 quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) texoffset quad) (-> a2-2 texoffset quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) texscale quad) (-> a2-2 texscale quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) adgif qword) (-> a2-2 adgif qword))
      (set! (-> (the-as shadow-vu1-constants v1-1) texflush quad) (-> a2-2 ad quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) flush quad) (-> a2-2 flush quad))
      (set! (-> (the-as shadow-vu1-constants v1-1) trigif qword) (-> a2-2 trigif qword))
      (set! (-> (the-as shadow-vu1-constants v1-1) quadgif qword) (-> a2-2 quadgif qword))
      )
    (set! (-> *shadow-globals* bucket arg1 constants) (the-as shadow-vu1-constants v1-1))
    )
  (&+! (-> arg0 base) 208)
  (let* ((v1-4 arg0)
         (a1-3 (the-as object (-> v1-4 base)))
         )
    (set! (-> (the-as dma-packet a1-3) dma) (new 'static 'dma-tag :qwc #x4 :id (dma-tag-id cnt)))
    (set! (-> (the-as dma-packet a1-3) vif0) (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl)))
    (set! (-> (the-as dma-packet a1-3) vif1)
          (new 'static 'vif-tag :imm #x3ac :num #x4 :cmd (vif-cmd unpack-v4-32))
          )
    (set! (-> v1-4 base) (&+ (the-as pointer a1-3) 16))
    )
  (let* ((v1-5 arg0)
         (a1-5 (the-as object (-> v1-5 base)))
         )
    (set! (-> (the-as shadow-vu1-data a1-5) adgif tag) (new 'static 'gif-tag64 :nloop #x1 :nreg #x2))
    (set! (-> (the-as shadow-vu1-data a1-5) adgif regs)
          (new 'static 'gif-tag-regs :regs0 (gif-reg-id a+d) :regs1 (gif-reg-id rgbaq))
          )
    (set! (-> (the-as shadow-vu1-data a1-5) ad data) (the-as uint 0))
    (set! (-> (the-as shadow-vu1-data a1-5) ad cmds) (gs-reg64 texflush))
    (set! (-> (the-as shadow-vu1-data a1-5) flush data) (the-as uint 0))
    (set! (-> (the-as shadow-vu1-data a1-5) flush cmds) (gs-reg64 prim))
    (set! (-> (the-as shadow-vu1-data a1-5) trigif tag)
          (new 'static 'gif-tag64
            :nloop #x3
            :eop #x1
            :pre #x1
            :prim (new 'static 'gs-prim :prim (gs-prim-type tri-fan) :tme #x1)
            :nreg #x2
            )
          )
    (set! (-> (the-as shadow-vu1-data a1-5) trigif regs)
          (new 'static 'gif-tag-regs :regs0 (gif-reg-id st) :regs1 (gif-reg-id xyzf2))
          )
    (set! (-> v1-5 base) (&+ (the-as pointer a1-5) 64))
    )
  (none)
  )

(defun shadow-vu1-patch-consts ((arg0 symbol) (arg1 int))
  (let ((v1-2 (-> *shadow-globals* bucket arg1 constants)))
    (cond
      (arg0
        (set! (-> v1-2 hvdfoff z) (- 1.0 (-> v1-2 hvdfoff z)))
        (set! (-> v1-2 fog w) -1.0)
        )
      (else
        (set! (-> v1-2 fog w) 1.0)
        )
      )
    )
  0
  (none)
  )

;; WARN: Return type mismatch dma-buffer vs none.
(defun shadow-vu1-add-matrix ((arg0 dma-buffer) (arg1 math-camera))
  (let ((v1-0 4))
    (let* ((a2-4 arg0)
           (a3-0 (the-as object (-> a2-4 base)))
           )
      (set! (-> (the-as dma-packet a3-0) dma) (new 'static 'dma-tag :id (dma-tag-id cnt) :qwc v1-0))
      (set! (-> (the-as dma-packet a3-0) vif0) (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl)))
      (set! (-> (the-as dma-packet a3-0) vif1) (new 'static 'vif-tag :cmd (vif-cmd unpack-v4-32) :num v1-0))
      (set! (-> a2-4 base) (&+ (the-as pointer a3-0) 16))
      )
    )
  (let ((v1-5 (the-as object (-> arg0 base))))
    (let ((a2-5 (-> arg1 perspective quad 0))
          (a3-1 (-> arg1 perspective quad 1))
          (t0-4 (-> arg1 perspective quad 2))
          (a1-1 (-> arg1 perspective trans quad))
          )
      (set! (-> (the-as matrix v1-5) quad 0) a2-5)
      (set! (-> (the-as matrix v1-5) quad 1) a3-1)
      (set! (-> (the-as matrix v1-5) quad 2) t0-4)
      (set! (-> (the-as matrix v1-5) trans quad) a1-1)
      )
    (set! (-> arg0 base) (&+ (the-as pointer v1-5) 64))
    )
  (none)
  )

;; WARN: Return type mismatch dma-buffer vs none.
(defun shadow-vu1-init-buffer ((arg0 dma-buffer) (arg1 int))
  (dma-buffer-add-vu-function arg0 shadow-vu1-block 1)
  (shadow-vu1-add-constants arg0 arg1)
  (shadow-vu1-add-matrix arg0 *math-camera*)
  (let* ((v1-0 arg0)
         (a0-4 (the-as object (-> v1-0 base)))
         )
    (set! (-> (the-as dma-packet a0-4) dma) (new 'static 'dma-tag :id (dma-tag-id cnt)))
    (set! (-> (the-as dma-packet a0-4) vif0) (new 'static 'vif-tag :cmd (vif-cmd mscalf) :msk #x1 :imm #xa))
    (set! (-> (the-as dma-packet a0-4) vif1) (new 'static 'vif-tag :cmd (vif-cmd flushe) :msk #x1))
    (set! (-> v1-0 base) (&+ (the-as pointer a0-4) 16))
    )
  (none)
  )
