;;-*-Lisp-*-
(in-package goal)
(bundles "ENGINE.CGO" "GAME.CGO")
(require "kernel-defs.gc")

;; Generic tie converts tie data to generic and supports environment mapping.

;; DECOMP BEGINS

(deftype generic-tie-instance (structure)
  ((matrix-tag  dma-packet :inline)
   (matrix-data vector 6 :inline)
   (index-tag   dma-packet :inline)
   (indices     uint8 224)
   (end-tag     dma-packet :inline)))

(deftype generic-tie-input (structure)
  ((palette-tag dma-packet :inline)
   (palette     rgba 128)
   (model-tag   dma-packet :inline)
   (model       vector 146 :inline)
   (matrix-tag  dma-packet :inline)
   (matrix-data vector 6 :inline)
   (index-tag   dma-packet :inline)
   (indices     uint8 224)
   (end-tag     dma-packet :inline)))

(deftype generic-tie-run-control (structure)
  ((skip-bp2    uint8)
   (skip-ips    uint8)
   (gifbuf-skip uint8)
   (strips      uint8)
   (target-bp1  uint8)
   (target-bp2  uint8)
   (target-ip1  uint8)
   (target-ip2  uint8)
   (target-bps  uint8)
   (target-ips  uint8)
   (is-generic  uint8)
   (reserved    uint8)))

(deftype generic-tie-base-point (structure)
  ((x    int16)
   (y    int16)
   (z    int16)
   (d0   int16)
   (vtx  uint64 :overlay-at x)
   (u    int16)
   (v    int16)
   (tex  uint32 :overlay-at u)
   (w    int16)
   (d1   int16)
   (data uint16 8 :overlay-at x)
   (quad uint128 :overlay-at x)))

(deftype generic-tie-bps (structure)
  ((bp generic-tie-base-point 4 :inline)))

(deftype generic-tie-interp-point (structure)
  ((x      int16)
   (y      int16)
   (z      int16)
   (d0     int16)
   (vtx0   uint64 :overlay-at x)
   (dx     int16)
   (dy     int16)
   (dz     int16)
   (unused int16)
   (vtx1   uint64 :overlay-at dx)
   (u      int16)
   (v      int16)
   (tex    uint32 :overlay-at u)
   (w      int16)
   (d1     int16)
   (data   uint16 12 :overlay-at x))
  :pack-me)

(deftype generic-tie-ips (structure)
  ((ip generic-tie-interp-point 2 :inline)))

(deftype generic-tie-header (structure)
  ((effect              uint8)
   (interp-table-size   uint8)
   (num-bps             uint8)
   (num-ips             uint8)
   (tint-color          uint32)
   (index-table-offset  uint16)
   (kick-table-offset   uint16)
   (normal-table-offset uint16)
   (interp-table-offset uint16)
   (gsf-header          gsf-header :inline)))

(deftype generic-tie-matrix (structure)
  ((matrix matrix :inline)
   (morph  vector :inline)
   (fog    qword :inline)))

(deftype generic-tie-normal (structure)
  ((x     int8)
   (y     int8)
   (z     int8)
   (dummy int8)))

(deftype generic-tie-control (structure)
  ((ptr-palette   uint32)
   (ptr-shaders   uint32)
   (ptr-runctrl   generic-tie-run-control)
   (ptr-verts     uint32)
   (ptr-generic   generic-tie-header)
   (ptr-dps       uint32)
   (ptr-kicks     uint32)
   (ptr-normals   uint32)
   (ptr-interp    uint32)
   (ptr-mtxs      generic-tie-matrix)
   (ptr-cinds     uint32)
   (next-instance uint32)
   (next-model    uint32)
   (next-is-model uint32)
   (tie-type      uint32)))

(deftype generic-tie-stats (structure)
  ((num-bps       uint32)
   (num-ips       uint32)
   (num-dps       uint32)
   (num-shaders   uint32)
   (num-models    uint32)
   (num-instances uint32)
   (num-waits     uint32)
   (num-qwc       uint32)
   (max-qwc       uint32)))

(deftype generic-tie-calls (structure)
  ((generic-prepare-dma-double basic)
   (generic-envmap-dproc       basic)
   (generic-interp-dproc       basic)
   (generic-no-light-dproc     basic))
  :pack-me)

(deftype generic-tie-shadow (structure)
  ((out-buf      gsf-buffer)
   (cur-buf      uint32)
   (tie-type     int32)
   (ptr-inst     uint32)
   (ptr-buf      uint32)
   (inst-xor     int32)
   (end-of-chain uint32)
   (write-limit  uint32)
   (calls        generic-tie-calls :inline))
  :pack-me)

(deftype generic-tie-work (structure)
  ((control     generic-tie-control :inline)
   (interp-job  generic-interp-job :inline)
   (shadow      generic-tie-shadow :inline)
   (input-a     generic-tie-input :inline)
   (input-b     generic-tie-input :inline)
   (inst-buf    generic-tie-instance :inline)
   (palette-buf rgba 128)))

(define-extern generic-tie-execute (function generic-dma-foreground-sink dma-buffer basic none))
