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

;; name: tfrag-methods.gc
;; name in dgo: tfrag-methods
;; dgos: ENGINE, GAME

;; DECOMP BEGINS

;; WARN: Return type mismatch symbol vs none.
(defun edge-debug-lines ((arg0 (array vector-array)))
  (when (nonzero? arg0)
    (dotimes (s5-0 (-> arg0 length))
      (when (logtest? *display-strip-lines* (ash 1 s5-0))
        (let ((s4-0 (-> arg0 s5-0)))
          (dotimes (s3-0 (/ (-> s4-0 length) 2))
            (add-debug-line
              #t
              (bucket-id debug-no-zbuf1)
              (-> s4-0 data (* s3-0 2))
              (-> s4-0 data (+ (* s3-0 2) 1))
              (new 'static 'rgba :r #xff :g #xff :b #xff :a #x80)
              #f
              (the-as rgba -1)
              )
            )
          )
        )
      )
    )
  (none)
  )

;; WARN: Return type mismatch drawable-tree-tfrag vs none.
(defun draw-drawable-tree-tfrag ((arg0 drawable-tree-tfrag))
  (local-vars (sv-16 (pointer uint8)))
  (when (logtest? (-> *display* vu1-enable-user) (vu1-renderer-mask tfrag))
    (let ((s5-0 (+ (-> arg0 length) -1)))
      (when (nonzero? s5-0)
        ;; TODO draw node cull (not really needed because c++ does it already)
        ; (dotimes (s4-0 s5-0)
        ;   (let* ((v1-8 (-> arg0 arrays s4-0))
        ;          (a0-4 (-> arg0 arrays (+ s4-0 1)))
        ;          (a1-1 (/ (-> (the-as drawable-inline-array-node v1-8) data 0 id) 8))
        ;          (a0-6 (/ (-> (the-as drawable-inline-array-node a0-4) data 0 id) 8))
        ;          (a1-3 (+ a1-1 #x38a0 #x70000000))
        ;          (a0-8 (+ a0-6 #x38a0 #x70000000))
        ;          )
        ;     (draw-node-cull
        ;       (the-as pointer a0-8)
        ;       (the-as pointer a1-3)
        ;       (the-as (inline-array draw-node) (&+ v1-8 32))
        ;       (-> v1-8 length)
        ;       )
        ;     )
        ;   )
        )
      (let* ((v1-13 (the-as drawable-inline-array-tfrag (-> arg0 arrays s5-0)))
             (s4-1 (-> v1-13 data))
             (s3-0 (-> v1-13 length))
             )
        (set! sv-16 (&-> (scratchpad-object terrain-context) work background vis-list (/ (-> s4-1 0 id) 8)))
        (let ((s5-1 (-> *display* frames (-> *display* on-screen) global-buf base)))
          (let* ((s1-0 (-> *display* frames (-> *display* on-screen) global-buf))
                 (s2-0 (-> s1-0 base))
                 )
            (set! (-> *tfrag-work* wait-to-spr) (the-as uint 0))
            (set! (-> *tfrag-work* wait-from-spr) (the-as uint 0))
            (set! (-> *tfrag-work* texture-dists) (the-as uint (-> *level* draw-level *draw-index* bsp tfrag-closest)))
            (set! (-> *tfrag-work* last-call) (the-as uint 0))
            ;(draw-inline-array-tfrag (the-as pointer sv-16) s4-1 s3-0 s1-0)
            (set! (-> *level* draw-level *draw-index* tfrag-last-calls 0) (the-as int (-> *tfrag-work* last-call)))
            (update-wait-stats
              (-> *perf-stats* data 42)
              (the-as uint 0)
              (-> *tfrag-work* wait-to-spr)
              (-> *tfrag-work* wait-from-spr)
              )
            (let ((a3-3 (-> s1-0 base)))
              (let ((v1-46 (the-as dma-packet (-> s1-0 base))))
                (set! (-> v1-46 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
                (set! (-> v1-46 vif0) (new 'static 'vif-tag))
                (set! (-> v1-46 vif1) (new 'static 'vif-tag))
                (set! (-> s1-0 base) (the-as pointer (&+ v1-46 16)))
                )
              (let ((t9-3 dma-bucket-insert-tag)
                    (a0-22 (-> *display* frames (-> *display* on-screen) bucket-group))
                    (v1-53 *draw-index*)
                    )
                (t9-3
                  a0-22
                  (cond
                    ((zero? v1-53)
                     (bucket-id tfrag-l0-tfrag)
                     )
                    ((= v1-53 1)
                     (bucket-id tfrag-l1-tfrag)
                     )
                    ((= v1-53 2)
                     (bucket-id tfrag-l2-tfrag)
                     )
                    ((= v1-53 3)
                     (bucket-id tfrag-l3-tfrag)
                     )
                    ((= v1-53 4)
                     (bucket-id tfrag-l4-tfrag)
                     )
                    (else
                      (bucket-id tfrag-l5-tfrag)
                      )
                    )
                  s2-0
                  (the-as (pointer dma-tag) a3-3)
                  )
                )
              )
            )
          ; (let* ((s1-1 (-> *display* frames (-> *display* on-screen) global-buf))
          ;        (s2-1 (-> s1-1 base))
          ;        )
          ;   (set! (-> *tfrag-work* near-wait-to-spr) (the-as uint 0))
          ;   (set! (-> *tfrag-work* near-wait-from-spr) (the-as uint 0))
          ;   (set! (-> *tfrag-work* last-call) (the-as uint 0))
          ;   (draw-inline-array-tfrag-scissor (the-as pointer sv-16) s4-1 s3-0 s1-1)
          ;   (set! (-> *level* draw-level *draw-index* tfrag-last-calls 1) (the-as int (-> *tfrag-work* last-call)))
          ;   (update-wait-stats
          ;     (-> *perf-stats* data 43)
          ;     (the-as uint 0)
          ;     (-> *tfrag-work* near-wait-to-spr)
          ;     (-> *tfrag-work* near-wait-from-spr)
          ;     )
          ;   (let ((a3-6 (-> s1-1 base)))
          ;     (let ((v1-70 (the-as dma-packet (-> s1-1 base))))
          ;       (set! (-> v1-70 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
          ;       (set! (-> v1-70 vif0) (new 'static 'vif-tag))
          ;       (set! (-> v1-70 vif1) (new 'static 'vif-tag))
          ;       (set! (-> s1-1 base) (the-as pointer (&+ v1-70 16)))
          ;       )
          ;     (let ((t9-6 dma-bucket-insert-tag)
          ;           (a0-32 (-> *display* frames (-> *display* on-screen) bucket-group))
          ;           (v1-77 *draw-index*)
          ;           )
          ;       (t9-6
          ;         a0-32
          ;         (cond
          ;           ((zero? v1-77)
          ;            (bucket-id tfrag-s-l0-tfrag)
          ;            )
          ;           ((= v1-77 1)
          ;            (bucket-id tfrag-s-l1-tfrag)
          ;            )
          ;           ((= v1-77 2)
          ;            (bucket-id tfrag-s-l2-tfrag)
          ;            )
          ;           ((= v1-77 3)
          ;            (bucket-id tfrag-s-l3-tfrag)
          ;            )
          ;           ((= v1-77 4)
          ;            (bucket-id tfrag-s-l4-tfrag)
          ;            )
          ;           (else
          ;             (bucket-id tfrag-s-l5-tfrag)
          ;             )
          ;           )
          ;         s2-1
          ;         (the-as (pointer dma-tag) a3-6)
          ;         )
          ;       )
          ;     )
          ;   )
          (let ((v1-78 *dma-mem-usage*))
            (when (nonzero? v1-78)
              (set! (-> v1-78 length) (max 2 (-> v1-78 length)))
              (set! (-> v1-78 data 1 name) "tfragment")
              (+! (-> v1-78 data 1 count) 1)
              (+! (-> v1-78 data 1 used)
                  (&- (-> *display* frames (-> *display* on-screen) global-buf base) (the-as uint s5-1))
                  )
              (set! (-> v1-78 data 1 total) (-> v1-78 data 1 used))
              )
            )
          )
        )
      )
    )
  (none)
  )

;; WARN: Return type mismatch drawable-tree-tfrag vs none.
(defun draw-drawable-tree-tfrag-trans ((arg0 drawable-tree-tfrag))
  (local-vars sv-16 (pointer uint8))
  (when (logtest? (vu1-renderer-mask tfrag-trans) (-> *display* vu1-enable-user))
    (let ((s5-0 (+ (-> arg0 length) -1)))
      ;; TODO draw node cull (not really needed because c++ does it already)
      ; (when (nonzero? s5-0)
      ;   (dotimes (s4-0 s5-0)
      ;     (let* ((v1-7 (-> arg0 arrays s4-0))
      ;            (a0-6 (-> arg0 arrays (+ s4-0 1)))
      ;            (a1-1 (/ (-> (the-as drawable-inline-array-node v1-7) data 0 id) 8))
      ;            (a0-8 (/ (-> (the-as drawable-inline-array-node a0-6) data 0 id) 8))
      ;            (a1-3 (+ a1-1 #x38a0 #x70000000))
      ;            (a0-10 (+ a0-8 #x38a0 #x70000000))
      ;            )
      ;       (draw-node-cull
      ;         (the-as pointer a0-10)
      ;         (the-as pointer a1-3)
      ;         (the-as (inline-array draw-node) (&+ v1-7 32))
      ;         (-> v1-7 length)
      ;         )
      ;       )
      ;     )
      ;   )
      (let* ((v1-13 (the-as drawable-inline-array-tfrag (-> arg0 arrays s5-0)))
             (s5-1 (-> v1-13 data))
             (s4-1 (-> v1-13 length))
             )
        (set! sv-16 (&-> (scratchpad-object terrain-context) work background vis-list (/ (-> s5-1 0 id) 8)))
        (let* ((s2-0 (-> *display* frames (-> *display* on-screen) global-buf))
               (s3-0 (-> s2-0 base))
               )
          (set! (-> *tfrag-work* wait-to-spr) (the-as uint 0))
          (set! (-> *tfrag-work* wait-from-spr) (the-as uint 0))
          (set! (-> *tfrag-work* texture-dists) (the-as uint (-> *level* draw-level *draw-index* bsp alpha-closest)))
          (set! (-> *tfrag-work* last-call) (the-as uint 0))
          ; (draw-inline-array-tfrag (the-as pointer sv-16) s5-1 s4-1 s2-0)
          (set! (-> *level* draw-level *draw-index* tfrag-last-calls 2) (the-as int (-> *tfrag-work* last-call)))
          (update-wait-stats
            (-> *perf-stats* data 42)
            (the-as uint 0)
            (-> *tfrag-work* wait-to-spr)
            (-> *tfrag-work* wait-from-spr)
            )
          (let ((a3-3 (-> s2-0 base)))
            (let ((v1-39 (the-as dma-packet (-> s2-0 base))))
              (set! (-> v1-39 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
              (set! (-> v1-39 vif0) (new 'static 'vif-tag))
              (set! (-> v1-39 vif1) (new 'static 'vif-tag))
              (set! (-> s2-0 base) (the-as pointer (&+ v1-39 16)))
              )
            (let ((t9-3 dma-bucket-insert-tag)
                  (a0-23 (-> *display* frames (-> *display* on-screen) bucket-group))
                  (v1-46 *draw-index*)
                  )
              (t9-3
                a0-23
                (cond
                  ((zero? v1-46)
                   (bucket-id tfrag-t-l0-alpha)
                   )
                  ((= v1-46 1)
                   (bucket-id tfrag-t-l1-alpha)
                   )
                  ((= v1-46 2)
                   (bucket-id tfrag-t-l2-alpha)
                   )
                  ((= v1-46 3)
                   (bucket-id tfrag-t-l3-alpha)
                   )
                  ((= v1-46 4)
                   (bucket-id tfrag-t-l4-alpha)
                   )
                  (else
                    (bucket-id tfrag-t-l5-alpha)
                    )
                  )
                s3-0
                (the-as (pointer dma-tag) a3-3)
                )
              )
            )
          )
        ; (let* ((s2-1 (-> *display* frames (-> *display* on-screen) global-buf))
        ;        (s3-1 (-> s2-1 base))
        ;        )
        ;   (set! (-> *tfrag-work* near-wait-to-spr) (the-as uint 0))
        ;   (set! (-> *tfrag-work* near-wait-from-spr) (the-as uint 0))
        ;   (set! (-> *tfrag-work* last-call) (the-as uint 0))
        ;   (draw-inline-array-tfrag-scissor (the-as pointer sv-16) s5-1 s4-1 s2-1)
        ;   (set! (-> *level* draw-level *draw-index* tfrag-last-calls 3) (the-as int (-> *tfrag-work* last-call)))
        ;   (update-wait-stats
        ;     (-> *perf-stats* data 43)
        ;     (the-as uint 0)
        ;     (-> *tfrag-work* near-wait-to-spr)
        ;     (-> *tfrag-work* near-wait-from-spr)
        ;     )
        ;   (let ((a3-6 (-> s2-1 base)))
        ;     (let ((v1-63 (the-as dma-packet (-> s2-1 base))))
        ;       (set! (-> v1-63 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
        ;       (set! (-> v1-63 vif0) (new 'static 'vif-tag))
        ;       (set! (-> v1-63 vif1) (new 'static 'vif-tag))
        ;       (set! (-> s2-1 base) (the-as pointer (&+ v1-63 16)))
        ;       )
        ;     (let ((t9-6 dma-bucket-insert-tag)
        ;           (a0-33 (-> *display* frames (-> *display* on-screen) bucket-group))
        ;           (v1-70 *draw-index*)
        ;           )
        ;       (t9-6
        ;         a0-33
        ;         (cond
        ;           ((zero? v1-70)
        ;            (bucket-id tfrag-st-l0-alpha)
        ;            )
        ;           ((= v1-70 1)
        ;            (bucket-id tfrag-st-l1-alpha)
        ;            )
        ;           ((= v1-70 2)
        ;            (bucket-id tfrag-st-l2-alpha)
        ;            )
        ;           ((= v1-70 3)
        ;            (bucket-id tfrag-st-l3-alpha)
        ;            )
        ;           ((= v1-70 4)
        ;            (bucket-id tfrag-st-l4-alpha)
        ;            )
        ;           (else
        ;             (bucket-id tfrag-st-l5-alpha)
        ;             )
        ;           )
        ;         s3-1
        ;         (the-as (pointer dma-tag) a3-6)
        ;         )
        ;       )
        ;     )
        ;   )
        )
      )
    )
  (none)
  )

;; WARN: Return type mismatch drawable-tree-tfrag vs none.
(defun draw-drawable-tree-tfrag-water ((arg0 drawable-tree-tfrag))
  (local-vars (sv-16 (pointer uint8)))
  (when (logtest? (vu1-renderer-mask tfrag-water) (-> *display* vu1-enable-user))
    (let ((s5-0 (+ (-> arg0 length) -1)))
      ;; TODO draw node cull (not really needed because c++ does it already)
      ; (when (nonzero? s5-0)
      ;   (dotimes (s4-0 s5-0)
      ;     (let* ((v1-7 (-> arg0 arrays s4-0))
      ;            (a0-6 (-> arg0 arrays (+ s4-0 1)))
      ;            (a1-1 (/ (-> (the-as drawable-inline-array-node v1-7) data 0 id) 8))
      ;            (a0-8 (/ (-> (the-as drawable-inline-array-node a0-6) data 0 id) 8))
      ;            (a1-3 (+ a1-1 #x38a0 #x70000000))
      ;            (a0-10 (+ a0-8 #x38a0 #x70000000))
      ;            )
      ;       (draw-node-cull
      ;         (the-as pointer a0-10)
      ;         (the-as pointer a1-3)
      ;         (the-as (inline-array draw-node) (&+ v1-7 32))
      ;         (-> v1-7 length)
      ;         )
      ;       )
      ;     )
      ;   )
      (let* ((v1-13 (the-as drawable-inline-array-tfrag (-> arg0 arrays s5-0)))
             (s5-1 (-> v1-13 data))
             (s4-1 (-> v1-13 length))
             )
        (set! sv-16 (&-> (scratchpad-object terrain-context) work background vis-list (/ (-> s5-1 0 id) 8)))
        (let* ((s2-0 (-> *display* frames (-> *display* on-screen) global-buf))
               (s3-0 (-> s2-0 base))
               )
          (set! (-> *tfrag-work* wait-to-spr) (the-as uint 0))
          (set! (-> *tfrag-work* wait-from-spr) (the-as uint 0))
          (set! (-> *tfrag-work* texture-dists) (the-as uint (-> *level* draw-level *draw-index* bsp water-closest)))
          (set! (-> *tfrag-work* last-call) (the-as uint 0))
          ; (draw-inline-array-tfrag (the-as pointer sv-16) s5-1 s4-1 s2-0)
          (set! (-> *level* draw-level *draw-index* tfrag-last-calls 4) (the-as int (-> *tfrag-work* last-call)))
          (update-wait-stats
            (-> *perf-stats* data 42)
            (the-as uint 0)
            (-> *tfrag-work* wait-to-spr)
            (-> *tfrag-work* wait-from-spr)
            )
          (let ((a3-3 (-> s2-0 base)))
            (let ((v1-39 (the-as dma-packet (-> s2-0 base))))
              (set! (-> v1-39 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
              (set! (-> v1-39 vif0) (new 'static 'vif-tag))
              (set! (-> v1-39 vif1) (new 'static 'vif-tag))
              (set! (-> s2-0 base) (the-as pointer (&+ v1-39 16)))
              )
            (let ((t9-3 dma-bucket-insert-tag)
                  (a0-23 (-> *display* frames (-> *display* on-screen) bucket-group))
                  (v1-46 *draw-index*)
                  )
              (t9-3
                a0-23
                (cond
                  ((zero? v1-46)
                   (bucket-id tfrag-w-l0-water)
                   )
                  ((= v1-46 1)
                   (bucket-id tfrag-w-l1-water)
                   )
                  ((= v1-46 2)
                   (bucket-id tfrag-w-l2-water)
                   )
                  ((= v1-46 3)
                   (bucket-id tfrag-w-l3-water)
                   )
                  ((= v1-46 4)
                   (bucket-id tfrag-w-l4-water)
                   )
                  (else
                    (bucket-id tfrag-w-l5-water)
                    )
                  )
                s3-0
                (the-as (pointer dma-tag) a3-3)
                )
              )
            )
          )
        ; (let* ((s2-1 (-> *display* frames (-> *display* on-screen) global-buf))
        ;        (s3-1 (-> s2-1 base))
        ;        )
        ;   (set! (-> *tfrag-work* near-wait-to-spr) (the-as uint 0))
        ;   (set! (-> *tfrag-work* near-wait-from-spr) (the-as uint 0))
        ;   (set! (-> *tfrag-work* last-call) (the-as uint 0))
        ;   (draw-inline-array-tfrag-scissor (the-as pointer sv-16) s5-1 s4-1 s2-1)
        ;   (set! (-> *level* draw-level *draw-index* tfrag-last-calls 5) (the-as int (-> *tfrag-work* last-call)))
        ;   (update-wait-stats
        ;     (-> *perf-stats* data 43)
        ;     (the-as uint 0)
        ;     (-> *tfrag-work* near-wait-to-spr)
        ;     (-> *tfrag-work* near-wait-from-spr)
        ;     )
        ;   (let ((a3-6 (-> s2-1 base)))
        ;     (let ((v1-63 (the-as dma-packet (-> s2-1 base))))
        ;       (set! (-> v1-63 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
        ;       (set! (-> v1-63 vif0) (new 'static 'vif-tag))
        ;       (set! (-> v1-63 vif1) (new 'static 'vif-tag))
        ;       (set! (-> s2-1 base) (the-as pointer (&+ v1-63 16)))
        ;       )
        ;     (let ((t9-6 dma-bucket-insert-tag)
        ;           (a0-33 (-> *display* frames (-> *display* on-screen) bucket-group))
        ;           (v1-70 *draw-index*)
        ;           )
        ;       (t9-6
        ;         a0-33
        ;         (cond
        ;           ((zero? v1-70)
        ;            (bucket-id tfrag-ws-l0-water)
        ;            )
        ;           ((= v1-70 1)
        ;            (bucket-id tfrag-ws-l1-water)
        ;            )
        ;           ((= v1-70 2)
        ;            (bucket-id tfrag-ws-l2-water)
        ;            )
        ;           ((= v1-70 3)
        ;            (bucket-id tfrag-ws-l3-water)
        ;            )
        ;           ((= v1-70 4)
        ;            (bucket-id tfrag-ws-l4-water)
        ;            )
        ;           (else
        ;             (bucket-id tfrag-ws-l5-water)
        ;             )
        ;           )
        ;         s3-1
        ;         (the-as (pointer dma-tag) a3-6)
        ;         )
        ;       )
        ;     )
        ;   )
        )
      )
    )
  (none)
  )

;; WARN: Return type mismatch pointer vs none.
(defun tfrag-vu1-init-buf ((arg0 bucket-id) (arg1 gs-test) (arg2 int) (arg3 uint) (arg4 symbol))
  (let ((s4-0 (-> *display* frames (-> *display* on-screen) bucket-group arg0)))
    (when (!= s4-0 (-> s4-0 last))
      (let* ((s3-0 (-> *display* frames (-> *display* on-screen) global-buf))
             (s2-1 (-> s3-0 base))
             )
        (tfrag-init-buffer s3-0 arg1 arg2 arg4)
        (let ((v1-12 (the-as object (-> s3-0 base))))
          (set! (-> (the-as dma-packet v1-12) dma) (new 'static 'dma-tag :id (dma-tag-id next) :addr (-> s4-0 next)))
          (set! (-> (the-as dma-packet v1-12) vif0) (new 'static 'vif-tag))
          (set! (-> (the-as dma-packet v1-12) vif1) (new 'static 'vif-tag))
          (set! (-> s3-0 base) (the-as pointer (&+ (the-as dma-packet v1-12) 16)))
          )
        (set! (-> s4-0 next) (the-as uint s2-1))
        )
      )
    )
  (let ((s5-1 (-> *display* frames (-> *display* on-screen) bucket-group arg0)))
    (when (!= s5-1 (-> s5-1 last))
      (let* ((s3-1 (-> *display* frames (-> *display* on-screen) global-buf))
             (s4-1 (-> s3-1 base))
             )
        (tfrag-end-buffer s3-1 (the-as int arg3))
        (let ((v0-3 (-> s3-1 base)))
          (let ((v1-26 (the-as object (-> s3-1 base))))
            (set! (-> (the-as dma-packet v1-26) dma) (new 'static 'dma-tag :id (dma-tag-id next)))
            (set! (-> (the-as dma-packet v1-26) vif0) (new 'static 'vif-tag))
            (set! (-> (the-as dma-packet v1-26) vif1) (new 'static 'vif-tag))
            (set! (-> s3-1 base) (&+ (the-as pointer v1-26) 16))
            )
          (set! (-> (the-as (pointer int32) (-> s5-1 last)) 1) (the-as int s4-1))
          (set! (-> s5-1 last) (the-as (pointer dma-tag) v0-3))
          )
        )
      )
    )
  (none)
  )

;; WARN: Return type mismatch pointer vs none.
(defun tfrag-scissor-vu1-init-buf ((arg0 bucket-id) (arg1 gs-test) (arg2 int) (arg3 uint) (arg4 symbol))
  ;; (break!) ;; no scissor
  (none)
  )

(deftype tfrag-init-data (structure)
  ((tfrag-bucket                bucket-id)
   (tfrag-scissor-bucket        bucket-id)
   (tfrag-trans-bucket          bucket-id)
   (tfrag-scissor-trans-bucket  bucket-id)
   (tfrag-water-bucket          bucket-id)
   (tfrag-water-scissor-bucket  bucket-id)
   )
  )

(define *tfrag-init-table* (new 'static 'inline-array tfrag-init-data LEVEL_MAX
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l0-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-s-l0-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-t-l0-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-st-l0-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-w-l0-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-ws-l0-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l1-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-s-l1-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-t-l1-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-st-l1-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-w-l1-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-ws-l1-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l2-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-s-l2-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-t-l2-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-st-l2-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-w-l2-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-ws-l2-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l3-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-s-l3-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-t-l3-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-st-l3-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-w-l3-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-ws-l3-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l4-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-s-l4-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-t-l4-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-st-l4-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-w-l4-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-ws-l4-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l5-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-s-l5-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-t-l5-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-st-l5-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-w-l5-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-ws-l5-water)
                               )
                             )
        )

(define *pc-tfrag-draw-level* (the level #f))

(defun tfrag-vu1-init-buffers ()
  (dotimes (gp-0 LEVEL_MAX)
    (let ((s5-0 (-> *level* draw-level gp-0))
          (s4-0 (-> *tfrag-init-table* gp-0))
          )
      (set! *pc-tfrag-draw-level* s5-0)
      (when s5-0
        (let ((s3-0 (-> s5-0 tfrag-gs-test)))
          (tfrag-vu1-init-buf
            (-> s4-0 tfrag-bucket)
            s3-0
            0
            (-> s5-0 tfrag-last-calls-u32 0)
            (-> s5-0 info use-camera-other)
            )
          (tfrag-scissor-vu1-init-buf
            (-> s4-0 tfrag-scissor-bucket)
            s3-0
            0
            (-> s5-0 tfrag-last-calls-u32 1)
            (-> s5-0 info use-camera-other)
            )
          )
        (tfrag-vu1-init-buf
          (-> s4-0 tfrag-trans-bucket)
          (new 'static 'gs-test
            :ate #x1
            :atst (gs-atest greater-equal)
            :aref #x26
            :afail #x1
            :zte #x1
            :ztst (gs-ztest greater-equal)
            )
          1
          (-> s5-0 tfrag-last-calls-u32 2)
          (-> s5-0 info use-camera-other)
          )
        (tfrag-scissor-vu1-init-buf
          (-> s4-0 tfrag-scissor-trans-bucket)
          (new 'static 'gs-test
            :ate #x1
            :atst (gs-atest greater-equal)
            :aref #x26
            :afail #x1
            :zte #x1
            :ztst (gs-ztest greater-equal)
            )
          1
          (-> s5-0 tfrag-last-calls-u32 3)
          (-> s5-0 info use-camera-other)
          )
        (tfrag-vu1-init-buf
          (-> s4-0 tfrag-water-bucket)
          (new 'static 'gs-test :ate #x1 :afail #x1 :zte #x1 :ztst (gs-ztest greater-equal))
          1
          (-> s5-0 tfrag-last-calls-u32 4)
          (-> s5-0 info use-camera-other)
          )
        (tfrag-scissor-vu1-init-buf
          (-> s4-0 tfrag-water-scissor-bucket)
          (new 'static 'gs-test :ate #x1 :afail #x1 :zte #x1 :ztst (gs-ztest greater-equal))
          1
          (-> s5-0 tfrag-last-calls-u32 5)
          (-> s5-0 info use-camera-other)
          )
        )
      )
    )
  (none)
  )

(defmethod draw ((this drawable-tree-tfrag) (arg0 drawable-tree-tfrag) (arg1 display-frame))
  (let ((v1-1 (-> *background-work* tfrag-tree-count))
        (a1-4 (-> *level* draw-level *draw-index*))
        )
    (set! (-> *background-work* tfrag-trees v1-1) this)
    (set! (-> *background-work* tfrag-levels v1-1) a1-4)
    )
  (+! (-> *background-work* tfrag-tree-count) 1)
  0
  (none)
  )

(defmethod draw ((this drawable-tree-tfrag-trans) (arg0 drawable-tree-tfrag-trans) (arg1 display-frame))
  (let ((v1-1 (-> *background-work* tfrag-trans-tree-count))
        (a1-4 (-> *level* draw-level *draw-index*))
        )
    (set! (-> *background-work* tfrag-trans-trees v1-1) this)
    (set! (-> *background-work* tfrag-trans-levels v1-1) a1-4)
    )
  (+! (-> *background-work* tfrag-trans-tree-count) 1)
  0
  (none)
  )

(defmethod draw ((this drawable-tree-tfrag-water) (arg0 drawable-tree-tfrag-water) (arg1 display-frame))
  (let ((v1-1 (-> *background-work* tfrag-water-tree-count))
        (a1-4 (-> *level* draw-level *draw-index*))
        )
    (set! (-> *background-work* tfrag-water-trees v1-1) this)
    (set! (-> *background-work* tfrag-water-levels v1-1) a1-4)
    )
  (+! (-> *background-work* tfrag-water-tree-count) 1)
  0
  (none)
  )

(defmethod collect-stats ((this tfragment))
  (stats-tfrag-asm this)
  0
  (none)
  )

(defmethod collect-stats ((this drawable-tree-tfrag))
  (when (logtest? (-> *display* vu1-enable-user) (vu1-renderer-mask tfrag))
    (set! (-> *tfrag-work* vu1-enable-tfrag)
          (the-as int (logand (-> *display* vu1-enable-user) (vu1-renderer-mask tfrag)))
          )
    (set! (-> *tfrag-work* vu1-enable-tfrag-scissor)
          (the-as int (logand (-> *display* vu1-enable-user) (vu1-renderer-mask tfrag)))
          )
    (set! (-> *tfrag-work* tr-stat-tfrag) (-> *terrain-stats* tfrag))
    (set! (-> *tfrag-work* tr-stat-tfrag-scissor) (-> *terrain-stats* tfrag-scissor))
    (let ((v1-15 (-> *tfrag-work* frag-dists quad)))
      (set! (-> *tfrag-work* frag-dists quad) v1-15)
      )
    (dotimes (s5-0 (-> this length))
      (collect-stats (-> this arrays s5-0))
      )
    )
  0
  (none)
  )

(defmethod collect-stats ((this drawable-tree-tfrag-trans))
  (when (logtest? (vu1-renderer-mask tfrag-trans) (-> *display* vu1-enable-user))
    (set! (-> *tfrag-work* vu1-enable-tfrag)
          (the-as int (logand (vu1-renderer-mask tfrag-trans) (-> *display* vu1-enable-user)))
          )
    (set! (-> *tfrag-work* vu1-enable-tfrag-scissor)
          (the-as int (logand (vu1-renderer-mask tfrag-trans) (-> *display* vu1-enable-user)))
          )
    (set! (-> *tfrag-work* tr-stat-tfrag) (-> *terrain-stats* tfrag-trans))
    (set! (-> *tfrag-work* tr-stat-tfrag-scissor) (-> *terrain-stats* tfrag-scissor-trans))
    (let ((v1-12 (-> *tfrag-work* frag-dists quad)))
      (set! (-> *tfrag-work* frag-dists quad) v1-12)
      )
    (dotimes (s5-0 (-> this length))
      (collect-stats (-> this arrays s5-0))
      )
    )
  0
  (none)
  )

(defmethod collect-stats ((this drawable-tree-tfrag-water))
  (when (logtest? (vu1-renderer-mask tfrag-water) (-> *display* vu1-enable-user))
    (set! (-> *tfrag-work* vu1-enable-tfrag)
          (the-as int (logand (vu1-renderer-mask tfrag-water) (-> *display* vu1-enable-user)))
          )
    (set! (-> *tfrag-work* vu1-enable-tfrag-scissor)
          (the-as int (logand (vu1-renderer-mask tfrag-water) (-> *display* vu1-enable-user)))
          )
    (set! (-> *tfrag-work* tr-stat-tfrag) (-> *terrain-stats* tfrag-water))
    (set! (-> *tfrag-work* tr-stat-tfrag-scissor) (-> *terrain-stats* tfrag-scissor-water))
    (let ((v1-12 (-> *tfrag-work* frag-dists quad)))
      (set! (-> *tfrag-work* frag-dists quad) v1-12)
      )
    (dotimes (s5-0 (-> this length))
      (collect-stats (-> this arrays s5-0))
      )
    )
  0
  (none)
  )

(defmethod collect-stats ((this drawable-inline-array-tfrag))
  (when (logtest? (-> *display* vu1-enable-user) (vu1-renderer-mask tfrag))
    (dotimes (s5-0 (-> this length))
      (let ((s4-0 (-> this data s5-0)))
        (if (vis-cull (-> s4-0 id))
            (collect-stats s4-0)
            )
        )
      )
    )
  0
  (none)
  )

(defmethod collect-stats ((this drawable-inline-array-tfrag-trans))
  (when (logtest? (vu1-renderer-mask tfrag-trans) (-> *display* vu1-enable-user))
    (dotimes (s5-0 (-> this length))
      (let ((s4-0 (-> this data s5-0)))
        (if (vis-cull (-> s4-0 id))
            (collect-stats s4-0)
            )
        )
      )
    )
  0
  (none)
  )

(defmethod collect-stats ((this drawable-inline-array-tfrag-water))
  (when (logtest? (vu1-renderer-mask tfrag-water) (-> *display* vu1-enable-user))
    (dotimes (s5-0 (-> this length))
      (let ((s4-0 (-> this data s5-0)))
        (if (vis-cull (-> s4-0 id))
            (collect-stats s4-0)
            )
        )
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this drawable-tree-tfrag) (arg0 drawable) (arg1 display-frame))
  (when (logtest? (-> *display* vu1-enable-user) (vu1-renderer-mask tfrag))
    (dotimes (s4-0 (-> this length))
      (let ((a1-1 (-> this arrays s4-0)))
        (debug-draw a1-1 a1-1 arg1)
        )
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this drawable-tree-tfrag-trans) (arg0 drawable) (arg1 display-frame))
  (when (logtest? (vu1-renderer-mask tfrag-trans) (-> *display* vu1-enable-user))
    (dotimes (s4-0 (-> this length))
      (let ((a1-1 (-> this arrays s4-0)))
        (debug-draw a1-1 a1-1 arg1)
        )
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this drawable-tree-tfrag-water) (arg0 drawable) (arg1 display-frame))
  (when (logtest? (vu1-renderer-mask tfrag-water) (-> *display* vu1-enable-user))
    (dotimes (s4-0 (-> this length))
      (let ((a1-1 (-> this arrays s4-0)))
        (debug-draw a1-1 a1-1 arg1)
        )
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this drawable-inline-array-tfrag) (arg0 drawable) (arg1 display-frame))
  (dotimes (s4-0 (-> this length))
    (let ((s3-0 (-> this data s4-0)))
      (if (vis-cull (-> s3-0 id))
          (debug-draw s3-0 s3-0 arg1)
          )
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this tfragment) (arg0 drawable) (arg1 display-frame))
  (-> arg1 global-buf)
  (edge-debug-lines (-> this debug-data debug-lines))
  0
  (none)
  )
