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

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

;; DECOMP BEGINS

;; WARN: Return type mismatch symbol vs none.
(defun edge-debug-lines ((arg0 (array vector-array)))
  "Draw tfrag debug lines. These debug-lines are not stored in retail copies."
  (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))
  "Top-level function to generate DMA for tfrag."
  (local-vars (sv-16 int))
  (when (logtest? (-> *display* vu1-enable-user) (vu1-renderer-mask tfrag))
    (let ((s5-0 (+ (-> arg0 length) -1)))
      (when (nonzero? s5-0)
        ; (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 #x3800 #x70000000))
        ;          (a0-8 (+ a0-6 #x3800 #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-14 (-> arg0 arrays s5-0))
             (s4-1 (&+ v1-14 32))
             (s3-0 (-> v1-14 length))
             )
        (set! sv-16 (+ (/ (-> s4-1 id) 8) #x3800 #x70000000))
        (let ((s5-1 (-> *display* frames (-> *display* on-screen) global-buf base)))
          (with-dma-buffer-add-bucket ((s1-0 (-> *display* frames (-> *display* on-screen) global-buf))
                                       (-> (new 'static 'array bucket-id 10
                                             (bucket-id tfrag-l0-tfrag)
                                             (bucket-id tfrag-l1-tfrag)
                                             (bucket-id tfrag-l2-tfrag)
                                             (bucket-id tfrag-l3-tfrag)
                                             (bucket-id tfrag-l4-tfrag)
                                             (bucket-id tfrag-l5-tfrag)
                                             (bucket-id tfrag-l6-tfrag)
                                             (bucket-id tfrag-l7-tfrag)
                                             (bucket-id tfrag-l8-tfrag)
                                             (bucket-id tfrag-l9-tfrag)
                                             )
                                           *draw-index*
                                           )
                                       )
            (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) (-> *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)
              )
            )
          ; (with-dma-buffer-add-bucket ((s1-1 (-> *display* frames (-> *display* on-screen) global-buf))
          ;                              (-> (new 'static 'array bucket-id 10
          ;                                    (bucket-id tfrag-scissor-l0-tfrag)
          ;                                    (bucket-id tfrag-scissor-l1-tfrag)
          ;                                    (bucket-id tfrag-scissor-l2-tfrag)
          ;                                    (bucket-id tfrag-scissor-l3-tfrag)
          ;                                    (bucket-id tfrag-scissor-l4-tfrag)
          ;                                    (bucket-id tfrag-scissor-l5-tfrag)
          ;                                    (bucket-id tfrag-scissor-l6-tfrag)
          ;                                    (bucket-id tfrag-scissor-l7-tfrag)
          ;                                    (bucket-id tfrag-scissor-l8-tfrag)
          ;                                    (bucket-id tfrag-scissor-l9-tfrag)
          ;                                    )
          ;                                  *draw-index*
          ;                                  )
          ;                              )
          ;   (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) (-> *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 ((a0-33 *dma-mem-usage*))
            (when (nonzero? a0-33)
              (set! (-> a0-33 length) (max 2 (-> a0-33 length)))
              (set! (-> a0-33 data 1 name) "tfragment")
              (+! (-> a0-33 data 1 count) 1)
              (+! (-> a0-33 data 1 used)
                  (&- (-> *display* frames (-> *display* on-screen) global-buf base) (the-as uint s5-1))
                  )
              (set! (-> a0-33 data 1 total) (-> a0-33 data 1 used))
              )
            )
          )
        )
      )
    )
  (none)
  )

;; WARN: Return type mismatch drawable-tree-tfrag vs none.
(defun draw-drawable-tree-tfrag-trans ((arg0 drawable-tree-tfrag))
  "Top-level function to generate DMA for tfrag."
  (local-vars (sv-16 int))
  (when (logtest? (vu1-renderer-mask tfrag-trans) (-> *display* vu1-enable-user))
    (let ((s5-0 (+ (-> arg0 length) -1)))
      (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 #x3800 #x70000000))
        ;          (a0-10 (+ a0-8 #x3800 #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 (-> arg0 arrays s5-0))
             (s5-1 (&+ v1-13 32))
             (s4-1 (-> v1-13 length))
             )
        (set! sv-16 (+ (/ (-> s5-1 id) 8) #x3800 #x70000000))
        (with-dma-buffer-add-bucket ((s2-0 (-> *display* frames (-> *display* on-screen) global-buf))
                                     (-> (new 'static 'array bucket-id 12
                                           (bucket-id tfrag-l0-alpha)
                                           (bucket-id tfrag-l1-alpha)
                                           (bucket-id tfrag-l2-alpha)
                                           (bucket-id tfrag-l3-alpha)
                                           (bucket-id tfrag-l4-alpha)
                                           (bucket-id tfrag-l5-alpha)
                                           (bucket-id tfrag-l6-alpha)
                                           (bucket-id tfrag-l7-alpha)
                                           (bucket-id tfrag-l8-alpha)
                                           (bucket-id tfrag-l9-alpha)
                                           (bucket-id bucket0)
                                           (bucket-id bucket0)
                                           )
                                         *draw-index*
                                         )
                                     )
          (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) (-> *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)
            )
          )
        ; (with-dma-buffer-add-bucket ((s2-1 (-> *display* frames (-> *display* on-screen) global-buf))
        ;                              (-> (new 'static 'array bucket-id 12
        ;                                    (bucket-id tfrag-scissor-l0-alpha)
        ;                                    (bucket-id tfrag-scissor-l1-alpha)
        ;                                    (bucket-id tfrag-scissor-l2-alpha)
        ;                                    (bucket-id tfrag-scissor-l3-alpha)
        ;                                    (bucket-id tfrag-scissor-l4-alpha)
        ;                                    (bucket-id tfrag-scissor-l5-alpha)
        ;                                    (bucket-id tfrag-scissor-l6-alpha)
        ;                                    (bucket-id tfrag-scissor-l7-alpha)
        ;                                    (bucket-id tfrag-scissor-l8-alpha)
        ;                                    (bucket-id tfrag-scissor-l9-alpha)
        ;                                    (bucket-id bucket0)
        ;                                    (bucket-id bucket0)
        ;                                    )
        ;                                  *draw-index*
        ;                                  )
        ;                              )
        ;   (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) (-> *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)
        ;     )
        ;   )
        )
      )
    )
  (none)
  )

;; WARN: Return type mismatch drawable-tree-tfrag vs none.
(defun draw-drawable-tree-tfrag-water ((arg0 drawable-tree-tfrag))
  "Top-level function to generate DMA for tfrag."
  (local-vars (sv-16 int))
  (when (logtest? (vu1-renderer-mask tfrag-water) (-> *display* vu1-enable-user))
    (let ((s5-0 (+ (-> arg0 length) -1)))
      (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 #x3800 #x70000000))
        ;          (a0-10 (+ a0-8 #x3800 #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 (-> arg0 arrays s5-0))
             (s5-1 (&+ v1-13 32))
             (s4-1 (-> v1-13 length))
             )
        (set! sv-16 (+ (/ (-> s5-1 id) 8) #x3800 #x70000000))
        (with-dma-buffer-add-bucket ((s2-0 (-> *display* frames (-> *display* on-screen) global-buf))
                                     (-> (new 'static 'array bucket-id 12
                                           (bucket-id tfrag-l0-water)
                                           (bucket-id tfrag-l1-water)
                                           (bucket-id tfrag-l2-water)
                                           (bucket-id tfrag-l3-water)
                                           (bucket-id tfrag-l4-water)
                                           (bucket-id tfrag-l5-water)
                                           (bucket-id tfrag-l6-water)
                                           (bucket-id tfrag-l7-water)
                                           (bucket-id tfrag-l8-water)
                                           (bucket-id tfrag-l9-water)
                                           (bucket-id bucket0)
                                           (bucket-id bucket0)
                                           )
                                         *draw-index*
                                         )
                                     )
          (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) (-> *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)
            )
          )
        ; (with-dma-buffer-add-bucket ((s2-1 (-> *display* frames (-> *display* on-screen) global-buf))
        ;                              (-> (new 'static 'array bucket-id 12
        ;                                    (bucket-id tfrag-scissor-l0-water)
        ;                                    (bucket-id tfrag-scissor-l1-water)
        ;                                    (bucket-id tfrag-scissor-l2-water)
        ;                                    (bucket-id tfrag-scissor-l3-water)
        ;                                    (bucket-id tfrag-scissor-l4-water)
        ;                                    (bucket-id tfrag-scissor-l5-water)
        ;                                    (bucket-id tfrag-scissor-l6-water)
        ;                                    (bucket-id tfrag-scissor-l7-water)
        ;                                    (bucket-id tfrag-scissor-l8-water)
        ;                                    (bucket-id tfrag-scissor-l9-water)
        ;                                    (bucket-id bucket0)
        ;                                    (bucket-id bucket0)
        ;                                    )
        ;                                  *draw-index*
        ;                                  )
        ;                              )
        ;   (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) (-> *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)
        ;     )
        ;   )
        )
      )
    )
  (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))
  "Do all tfrag buffer setup for a single bucket."
  (let ((v1-0 *display*)
        (a0-1 32)
        )
    (+! (-> v1-0 mem-reserve-size) a0-1)
    (when (not (-> v1-0 dma-buffer-overflow))
      (let ((t1-0 (-> v1-0 frames (-> v1-0 on-screen) global-buf)))
        (if (< (-> t1-0 real-buffer-end) (the-as int (&+ (-> t1-0 base) a0-1)))
            (set! (-> v1-0 dma-buffer-overflow) #t)
            )
        )
      (when (not (-> v1-0 dma-buffer-overflow))

        (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-14 (the-as object (-> s3-0 base))))
                (set! (-> (the-as dma-packet v1-14) dma) (new 'static 'dma-tag :id (dma-tag-id next) :addr (-> s4-0 next)))
                (set! (-> (the-as dma-packet v1-14) vif0) (new 'static 'vif-tag))
                (set! (-> (the-as dma-packet v1-14) vif1) (new 'static 'vif-tag))
                (set! (-> s3-0 base) (the-as pointer (&+ (the-as dma-packet v1-14) 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-5 (-> s3-1 base)))
                (let ((v1-28 (the-as object (-> s3-1 base))))
                  (set! (-> (the-as dma-packet v1-28) dma) (new 'static 'dma-tag :id (dma-tag-id next)))
                  (set! (-> (the-as dma-packet v1-28) vif0) (new 'static 'vif-tag))
                  (set! (-> (the-as dma-packet v1-28) vif1) (new 'static 'vif-tag))
                  (set! (-> s3-1 base) (&+ (the-as pointer v1-28) 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-5))
                )
              )
            )
          )
        )
      )
    )
  (none)
  )

;; WARN: Return type mismatch pointer vs none.
;; ERROR: Failed store: (s.w! (+ v1-14 8) 0) at op 52
;; ERROR: Failed store: (s.w! (+ v1-14 12) 0) at op 53
;; ERROR: Failed store: (s.w! (+ v1-28 8) 0) at op 84
;; ERROR: Failed store: (s.w! (+ v1-28 12) 0) at op 85
;; ERROR: Failed store: (s.w! (+ v1-30 4) s4-1) at op 89
(defun tfrag-scissor-vu1-init-buf ((arg0 bucket-id) (arg1 gs-test) (arg2 int) (arg3 uint) (arg4 symbol))
  (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 10
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l0-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l0-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l0-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l0-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l0-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l0-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l1-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l1-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l1-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l1-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l1-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l1-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l2-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l2-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l2-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l2-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l2-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l2-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l3-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l3-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l3-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l3-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l3-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l3-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l4-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l4-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l4-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l4-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l4-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l4-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l5-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l5-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l5-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l5-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l5-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l5-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l6-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l6-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l6-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l6-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l6-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l6-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l7-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l7-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l7-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l7-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l7-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l7-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l8-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l8-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l8-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l8-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l8-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l8-water)
                               )
                             (new 'static 'tfrag-init-data
                               :tfrag-bucket (bucket-id tfrag-l9-tfrag)
                               :tfrag-scissor-bucket (bucket-id tfrag-scissor-l9-tfrag)
                               :tfrag-trans-bucket (bucket-id tfrag-l9-alpha)
                               :tfrag-scissor-trans-bucket (bucket-id tfrag-scissor-l9-alpha)
                               :tfrag-water-bucket (bucket-id tfrag-l9-water)
                               :tfrag-water-scissor-bucket (bucket-id tfrag-scissor-l9-water)
                               )
                             )
        )

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

(defun tfrag-vu1-init-buffers ()
  "Initialize all tfrag buckets."
  (dotimes (gp-0 10)
    (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
        (set-subdivide-settings! 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 0)
            (logtest? (-> s5-0 info level-flags) (level-flags use-camera-other))
            )
          (tfrag-scissor-vu1-init-buf
            (-> s4-0 tfrag-scissor-bucket)
            s3-0
            0
            (-> s5-0 tfrag-last-calls 1)
            (logtest? (-> s5-0 info level-flags) (level-flags 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 2)
          (logtest? (-> s5-0 info level-flags) (level-flags 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 3)
          (logtest? (-> s5-0 info level-flags) (level-flags 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 4)
          (logtest? (-> s5-0 info level-flags) (level-flags 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 5)
          (logtest? (-> s5-0 info level-flags) (level-flags use-camera-other))
          )
        )
      )
    )
  (none)
  )

(defmethod draw ((this drawable-tree-tfrag))
  "Draw the drawable, and typically its children.
   This usually means adding stuff to a list to be drawn later, rather than expensive drawing here."
  (let ((v1-1 (-> *background-work* tfrag-tree-count))
        (a1-3 (-> *level* draw-level *draw-index*))
        )
    (set! (-> *background-work* tfrag-trees v1-1) this)
    (set! (-> *background-work* tfrag-levels v1-1) a1-3)
    )
  (+! (-> *background-work* tfrag-tree-count) 1)
  0
  (none)
  )

(defmethod draw ((this drawable-tree-tfrag-trans))
  "Draw the drawable, and typically its children.
   This usually means adding stuff to a list to be drawn later, rather than expensive drawing here."
  (let ((v1-1 (-> *background-work* tfrag-trans-tree-count))
        (a1-3 (-> *level* draw-level *draw-index*))
        )
    (set! (-> *background-work* tfrag-trans-trees v1-1) this)
    (set! (-> *background-work* tfrag-trans-levels v1-1) a1-3)
    )
  (+! (-> *background-work* tfrag-trans-tree-count) 1)
  0
  (none)
  )

(defmethod draw ((this drawable-tree-tfrag-water))
  "Draw the drawable, and typically its children.
   This usually means adding stuff to a list to be drawn later, rather than expensive drawing here."
  (let ((v1-1 (-> *background-work* tfrag-water-tree-count))
        (a1-3 (-> *level* draw-level *draw-index*))
        )
    (set! (-> *background-work* tfrag-water-trees v1-1) this)
    (set! (-> *background-work* tfrag-water-levels v1-1) a1-3)
    )
  (+! (-> *background-work* tfrag-water-tree-count) 1)
  0
  (none)
  )

(defmethod collect-stats ((this tfragment))
  "Collect triangle/perf statistics for rendering.
   This is only called when viewing stats.
   The vis-bits and culling registers are loaded during this time."
  (stats-tfrag-asm this)
  0
  (none)
  )

(defmethod collect-stats ((this drawable-tree-tfrag))
  "Collect triangle/perf statistics for rendering.
   This is only called when viewing stats.
   The vis-bits and culling registers are loaded during this time."
  (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))
  "Collect triangle/perf statistics for rendering.
   This is only called when viewing stats.
   The vis-bits and culling registers are loaded during this time."
  (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))
  "Collect triangle/perf statistics for rendering.
   This is only called when viewing stats.
   The vis-bits and culling registers are loaded during this time."
  (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))
  "Collect triangle/perf statistics for rendering.
   This is only called when viewing stats.
   The vis-bits and culling registers are loaded during this time."
  (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))
  "Collect triangle/perf statistics for rendering.
   This is only called when viewing stats.
   The vis-bits and culling registers are loaded during this time."
  (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))
  "Collect triangle/perf statistics for rendering.
   This is only called when viewing stats.
   The vis-bits and culling registers are loaded during this time."
  (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))
  "Debug-draw a drawable and its children. Typically uses the debug-draw functions."
  (when (logtest? (-> *display* vu1-enable-user) (vu1-renderer-mask tfrag))
    (dotimes (s5-0 (-> this length))
      (debug-draw (-> this arrays s5-0))
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this drawable-tree-tfrag-trans))
  "Debug-draw a drawable and its children. Typically uses the debug-draw functions."
  (when (logtest? (vu1-renderer-mask tfrag-trans) (-> *display* vu1-enable-user))
    (dotimes (s5-0 (-> this length))
      (debug-draw (-> this arrays s5-0))
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this drawable-tree-tfrag-water))
  "Debug-draw a drawable and its children. Typically uses the debug-draw functions."
  (when (logtest? (vu1-renderer-mask tfrag-water) (-> *display* vu1-enable-user))
    (dotimes (s5-0 (-> this length))
      (debug-draw (-> this arrays s5-0))
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this drawable-inline-array-tfrag))
  "Debug-draw a drawable and its children. Typically uses the debug-draw functions."
  (dotimes (s5-0 (-> this length))
    (let ((s4-0 (-> this data s5-0)))
      (if (vis-cull (-> s4-0 id))
          (debug-draw s4-0)
          )
      )
    )
  0
  (none)
  )

(defmethod debug-draw ((this tfragment))
  "Debug-draw a drawable and its children. Typically uses the debug-draw functions."
  (-> *display* frames (-> *display* on-screen) global-buf)
  (edge-debug-lines (-> this debug-data debug-lines))
  0
  (none)
  )
