gs: draw.lisp

the GS's drawing: vertex kicks, primitives, and a reference rasterizer into local memory.

Other definitions

(defun gs-prim-attr) source
(defun gs-prim-attr (g)
  "the primitive's attributes: PRIM's word with PRMODE's bits 3-10 when PRMODECONT.AC is 0"
  (declare (type gsm g))
  (let ((prim (greg prim)))
    (if (= (logand (greg prmodecont) 1) 1)
        (ldb (byte 11 0) prim)
        (logior (logand prim 7) (logand (greg prmode) #x7F8)))))
(defun gs-prim-vertices) source
(defun gs-prim-vertices (p)
  "the vertices a primitive of type P takes (0 for the reserved type 7)"
  (declare (type (unsigned-byte 8) p))
  (case p (0 1) ((1 2 6) 2) ((3 4 5) 3) (t 0)))
(defun gs-vcopy) source
(defun gs-vcopy (g to from)
  (declare (type gsm g) (type (unsigned-byte 8) to from))
  (setf (aref (gsm-vx g) to) (aref (gsm-vx g) from) (aref (gsm-vy g) to) (aref (gsm-vy g) from)
        (aref (gsm-vz g) to) (aref (gsm-vz g) from) (aref (gsm-vc g) to) (aref (gsm-vc g) from)
        (aref (gsm-vq g) to) (aref (gsm-vq g) from) (aref (gsm-vs g) to) (aref (gsm-vs g) from)
        (aref (gsm-vt g) to) (aref (gsm-vt g) from) (aref (gsm-vu g) to) (aref (gsm-vu g) from)
        (aref (gsm-vv g) to) (aref (gsm-vv g) from) (aref (gsm-vf g) to) (aref (gsm-vf g) from))
  nil)
(defun gs-kick) source
(defun gs-kick (g x y z f kick)
  "queue a vertex at (X, Y) (12.4) with depth Z and fog F, and draw when the primitive is
   complete and KICK is 1"
  (declare (type gsm g) (type (unsigned-byte 32) x y z) (type (unsigned-byte 8) f) (type (unsigned-byte 1) kick))
  (let* ((attr (gs-prim-attr g)) (p (logand attr 7)) (ctx (ldb (byte 1 9) attr))
         (off (greg xyoffset ctx)) (i (gsm-vn g)) (need (gs-prim-vertices p)))
    (when (= need 0)
      (return-from gs-kick nil))
    (setf (aref (gsm-vx g) i) (- (logand x #xFFFF) (ldb (byte 16 0) off))
          (aref (gsm-vy g) i) (- (logand y #xFFFF) (ldb (byte 16 32) off))
          (aref (gsm-vz g) i) z (aref (gsm-vc g) i) (gsm-cur-c g) (aref (gsm-vq g) i) (gsm-cur-q g)
          (aref (gsm-vs g) i) (gsm-cur-s g) (aref (gsm-vt g) i) (gsm-cur-t g)
          (aref (gsm-vu g) i) (gsm-cur-u g) (aref (gsm-vv g) i) (gsm-cur-v g) (aref (gsm-vf g) i) f)
    (setf (gsm-vn g) (+ i 1) (gsm-vstrip g) (wrap 32 (+ (gsm-vstrip g) 1)))
    (when (>= (gsm-vn g) need)
      (when (= kick 1)
        (gs-draw-prim g attr))
      (case p
        (2 (gs-vcopy g 0 1) (setf (gsm-vn g) 1))
        (4 (gs-vcopy g 0 1) (gs-vcopy g 1 2) (setf (gsm-vn g) 2))
        (5 (gs-vcopy g 1 2) (setf (gsm-vn g) 2))
        (t (setf (gsm-vn g) 0)))))
  nil)
(defstruct gd) source
(defstruct (gd (:conc-name gd-))
  (fbp 0 :type (unsigned-byte 32)) (fbw 0 :type (unsigned-byte 32)) (fpsm 0 :type (unsigned-byte 8))
  (fbmsk 0 :type (unsigned-byte 32))
  (zbp 0 :type (unsigned-byte 32)) (zpsm 0 :type (unsigned-byte 8)) (zmsk 0 :type (unsigned-byte 1))
  (zte 0 :type (unsigned-byte 1)) (ztst 0 :type (unsigned-byte 8))
  (ate 0 :type (unsigned-byte 1)) (atst 0 :type (unsigned-byte 8)) (aref 0 :type (unsigned-byte 8))
  (afail 0 :type (unsigned-byte 8)) (date 0 :type (unsigned-byte 1)) (datm 0 :type (unsigned-byte 1))
  (abe 0 :type (unsigned-byte 1)) (ba 0 :type (unsigned-byte 8)) (bb 0 :type (unsigned-byte 8))
  (bc 0 :type (unsigned-byte 8)) (bd 0 :type (unsigned-byte 8)) (bfix 0 :type (unsigned-byte 8))
  (pabe 0 :type (unsigned-byte 1)) (fba 0 :type (unsigned-byte 1)) (clamp 1 :type (unsigned-byte 1))
  (dthe 0 :type (unsigned-byte 1)) (dimx 0 :type (unsigned-byte 64))
  (fge 0 :type (unsigned-byte 1)) (fogcol 0 :type (unsigned-byte 32))
  (tme 0 :type (unsigned-byte 1)) (tfx 0 :type (unsigned-byte 8)) (tcc 0 :type (unsigned-byte 1))
  (tbp 0 :type (unsigned-byte 32)) (tbw 0 :type (unsigned-byte 32)) (tpsm 0 :type (unsigned-byte 8))
  (tw 1 :type (unsigned-byte 32)) (th 1 :type (unsigned-byte 32))
  (cpsm 0 :type (unsigned-byte 8)) (csa 0 :type (unsigned-byte 8))
  (wms 0 :type (unsigned-byte 8)) (wmt 0 :type (unsigned-byte 8))
  (minu 0 :type (unsigned-byte 32)) (maxu 0 :type (unsigned-byte 32))
  (minv 0 :type (unsigned-byte 32)) (maxv 0 :type (unsigned-byte 32))
  (ta0 0 :type (unsigned-byte 8)) (ta1 0 :type (unsigned-byte 8)) (aem 0 :type (unsigned-byte 1))
  (fst 0 :type (unsigned-byte 1)) (iip 0 :type (unsigned-byte 1)) (mmag 0 :type (unsigned-byte 1))
  (scx0 0 :type (signed-byte 32)) (scx1 0 :type (signed-byte 32))
  (scy0 0 :type (signed-byte 32)) (scy1 0 :type (signed-byte 32))
  (scanmsk 0 :type (unsigned-byte 8)))
(defvar *gd*) source
(defvar *gd* (make-gd))
(defun gs-setup) source
(defun gs-setup (g attr)
  "decode the registers of the primitive's context into *GD*"
  (declare (type gsm g) (type (unsigned-byte 32) attr))
  (let* ((d *gd*) (ctx (ldb (byte 1 9) attr))
         (frame (greg frame ctx)) (zbuf (greg zbuf ctx)) (test (greg test ctx)) (alpha (greg alpha ctx))
         (tex0 (greg tex0 ctx)) (clamp (greg clamp ctx)) (sc (greg scissor ctx)) (texa (greg texa))
         (fpsm (gsf frame 24 6)))
    (setf (gd-fbp d) (* 32 (gsf frame 0 9)) (gd-fbw d) (gsf frame 16 6) (gd-fpsm d) fpsm
          (gd-fbmsk d) (gsf frame 32 32)
          (gd-zbp d) (* 32 (gsf zbuf 0 9)) (gd-zpsm d) (logior #x30 (gsf zbuf 24 4)) (gd-zmsk d) (gsf zbuf 32 1)
          (gd-ate d) (gsf test 0 1) (gd-atst d) (gsf test 1 3) (gd-aref d) (gsf test 4 8)
          ;; AFAIL RGB_ONLY acts as FB_ONLY on a frame buffer that is not 32-bit ([PCSX2] GSRegs.h GetAFAIL)
          (gd-afail d) (let ((af (gsf test 12 2))) (if (and (= af 3) (/= (logand fpsm 15) 0)) 1 af))
          (gd-date d) (gsf test 14 1) (gd-datm d) (gsf test 15 1)
          (gd-zte d) (gsf test 16 1) (gd-ztst d) (gsf test 17 2)
          (gd-abe d) (ldb (byte 1 6) attr) (gd-ba d) (gsf alpha 0 2) (gd-bb d) (gsf alpha 2 2)
          (gd-bc d) (gsf alpha 4 2) (gd-bd d) (gsf alpha 6 2) (gd-bfix d) (gsf alpha 32 8)
          (gd-pabe d) (logand (greg pabe) 1) (gd-fba d) (logand (greg fba ctx) 1)
          (gd-clamp d) (logand (greg colclamp) 1) (gd-dthe d) (logand (greg dthe) 1) (gd-dimx d) (greg dimx)
          (gd-fge d) (ldb (byte 1 5) attr) (gd-fogcol d) (ldb (byte 24 0) (greg fogcol))
          (gd-tme d) (ldb (byte 1 4) attr) (gd-tfx d) (gsf tex0 35 2) (gd-tcc d) (gsf tex0 34 1)
          (gd-tbp d) (gsf tex0 0 14) (gd-tbw d) (gsf tex0 14 6) (gd-tpsm d) (gsf tex0 20 6)
          (gd-tw d) (ash 1 (min 10 (gsf tex0 26 4))) (gd-th d) (ash 1 (min 10 (gsf tex0 30 4)))
          (gd-cpsm d) (gsf tex0 51 4) (gd-csa d) (gsf tex0 56 5)
          (gd-wms d) (gsf clamp 0 2) (gd-wmt d) (gsf clamp 2 2)
          (gd-minu d) (gsf clamp 4 10) (gd-maxu d) (gsf clamp 14 10)
          (gd-minv d) (gsf clamp 24 10) (gd-maxv d) (gsf clamp 34 10)
          (gd-ta0 d) (gsf texa 0 8) (gd-aem d) (gsf texa 15 1) (gd-ta1 d) (gsf texa 32 8)
          (gd-fst d) (ldb (byte 1 8) attr) (gd-iip d) (ldb (byte 1 3) attr)
          (gd-mmag d) (gsf (greg tex1 ctx) 5 1)
          (gd-scx0 d) (gsf sc 0 11) (gd-scx1 d) (gsf sc 16 11) (gd-scy0 d) (gsf sc 32 11) (gd-scy1 d) (gsf sc 48 11)
          (gd-scanmsk d) (logand (greg scanmsk) 3)))
  nil)
(defun gs-psm-format) source
(defun gs-psm-format (psm)
  "the texel format code of PSM (src/core/texfmt.lisp)"
  (declare (type (unsigned-byte 8) psm))
  (case psm
    (#x00 (texfmt-code a8b8g8r8)) (#x01 (texfmt-code x8b8g8r8)) ((#x02 #x0A) (texfmt-code a1b5g5r5))
    (#x13 (texfmt-code index8)) (#x14 (texfmt-code index4)) (#x1B (texfmt-code index8h))
    (#x24 (texfmt-code index4hl)) (#x2C (texfmt-code index4hh)) (#x30 (texfmt-code d32))
    (#x31 (texfmt-code x8d24)) (t (texfmt-code d16))))
(defun gs-psm-layout) source
(defun gs-psm-layout (psm)
  "the layout of PSM's buffers: the GS's swizzles; PSMCT16S and PSMZ16S's block order, the Z formats'"
  (declare (type (unsigned-byte 8) psm))
  (case psm
    (#x0A (gfx-code texture-layout gs-swizzle-s)) (#x3A (gfx-code texture-layout gs-swizzle-zs))
    ((#x30 #x31 #x32) (gfx-code texture-layout gs-swizzle-z))
    (t (gfx-code texture-layout gs-swizzle))))
(defun gs-atst-compare) source
(defun gs-atst-compare (atst)
  (declare (type (unsigned-byte 8) atst))
  (case atst
    (0 (gfx-code compare-op never)) (1 (gfx-code compare-op always)) (2 (gfx-code compare-op less))
    (3 (gfx-code compare-op less-or-equal)) (4 (gfx-code compare-op equal))
    (5 (gfx-code compare-op greater-or-equal)) (6 (gfx-code compare-op greater))
    (t (gfx-code compare-op not-equal))))
(defun gs-ztst-compare) source
(defun gs-ztst-compare (ztst)
  (declare (type (unsigned-byte 8) ztst))
  (case ztst
    (0 (gfx-code compare-op never)) (1 (gfx-code compare-op always))
    (2 (gfx-code compare-op greater-or-equal)) (t (gfx-code compare-op greater))))
(defun gs-wrap-mode) source
(defun gs-wrap-mode (m)
  (declare (type (unsigned-byte 8) m))
  (case m
    (0 (gfx-code address-mode repeat)) (1 (gfx-code address-mode clamp-to-edge))
    (2 (gfx-code address-mode region-clamp)) (t (gfx-code address-mode region-repeat))))
(defun gs-state) source
(defun gs-state (g attr)
  "the primitive of attributes ATTR, its queued vertices and *GD*'s registers as the abstract state; its draw"
  (declare (type gsm g) (type (unsigned-byte 32) attr))
  (let* ((gfx (gsm-gfx g)) (d *gd*) (p (logand attr 7)) (n (gs-prim-vertices p))
         (off (greg xyoffset (ldb (byte 1 9) attr))) (fpsm (gd-fpsm d)) (indexed (gs-indexed-p (gd-tpsm d))))
    (setf (gs primitive topology) (case p
                                    (0 (gfx-code topology point-list)) ((1 2) (gfx-code topology line-list))
                                    (6 (gfx-code topology rectangle-list)) (t (gfx-code topology triangle-list)))
          (gs immediate count) n
          (gs coordinates offset-x) (- (ldb (byte 16 0) off)) (gs coordinates offset-y) (- (ldb (byte 16 32) off))
          (gs coordinates xy-fraction) 4 (gs coordinates uv-fraction) 4 (gs coordinates perspective) (- 1 (gd-fst d)))
    (dotimes (i n)
      (setf (gs immediate i x) (aref (gsm-vx g) i) (gs immediate i y) (aref (gsm-vy g) i)
            (gs immediate i z) (aref (gsm-vz g) i) (gs immediate i color) (aref (gsm-vc g) i)
            (gs immediate i u) (aref (gsm-vu g) i) (gs immediate i v) (aref (gsm-vv g) i)
            (gs immediate i s) (aref (gsm-vs g) i) (gs immediate i tt) (aref (gsm-vt g) i)
            (gs immediate i q) (aref (gsm-vq g) i) (gs immediate i fog) (aref (gsm-vf g) i)))
    ;; where it lands
    (setf (gs scissor count) 1 (gs scissor 0 x) (gd-scx0 d) (gs scissor 0 y) (gd-scy0 d)
          (gs scissor 0 width) (if (>= (gd-scx1 d) (gd-scx0 d)) (+ 1 (- (gd-scx1 d) (gd-scx0 d))) 0)
          (gs scissor 0 height) (if (>= (gd-scy1 d) (gd-scy0 d)) (+ 1 (- (gd-scy1 d) (gd-scy0 d))) 0)
          (gs raster shade-model) (if (= (gd-iip d) 1) (gfx-code shade-model smooth) (gfx-code shade-model flat))
          (gs raster line-skip) (gd-scanmsk d) (gs raster rules) (gfx-code raster-rules gs-corners)
          ;; DIMX applies to 16-bit frame buffers only
          (gs raster dither) (if (and (= (gd-dthe d) 1) (= (gs-psm-bits fpsm) 16)) 1 0)
          (gs color-target count) 1 (gs color-target 0 format) (gs-psm-format fpsm)
          (gs attachment 0 address) (* 256 (gd-fbp d)) (gs attachment 0 pitch) (* 64 (gd-fbw d))
          (gs attachment 0 layout) (gs-psm-layout fpsm)
          (gs blend 0 write-bits) (wrap 32 (lognot (gd-fbmsk d)))
          (gs depth-target format) (gs-psm-format (gd-zpsm d))
          (gs depth-attachment address) (* 256 (gd-zbp d)) (gs depth-attachment pitch) (* 64 (gd-fbw d))
          (gs depth-attachment layout) (gs-psm-layout (gd-zpsm d)))
    (dotimes (k 16)
      (let ((v (ldb (byte 3 (* 4 k)) (gd-dimx d))))
        (setf (gs dither-matrix k value) (if (>= v 4) (- v 8) v))))
    ;; tests
    (setf (gs alpha-test enable) (gd-ate d) (gs alpha-test compare) (gs-atst-compare (gd-atst d))
          (gs alpha-test reference) (/ (float (gd-aref d) 1.0) 255.0) (gs alpha-test fail) (gd-afail d)
          (gs mask-bit force) (gd-fba d) (gs mask-bit test) (gd-date d) (gs mask-bit pass) (gd-datm d)
          (gs depth test) (gd-zte d) (gs depth compare) (gs-ztst-compare (gd-ztst d)) (gs depth write) (- 1 (gd-zmsk d)))
    ;; colour
    (setf (gs combine enable) (gd-abe d) (gs combine a) (gd-ba d) (gs combine b) (gd-bb d)
          (gs combine c) (gd-bc d) (gs combine d) (gd-bd d) (gs combine fixed) (gd-bfix d)
          (gs combine select) (if (= (gd-pabe d) 1) (gfx-code combine-select source-alpha-msb)
                                  (gfx-code combine-select all))
          (gs combine clamp) (gd-clamp d)
          (gs fog enable) (gd-fge d) (gs fog r) (ldb (byte 8 0) (gd-fogcol d))
          (gs fog g) (ldb (byte 8 8) (gd-fogcol d)) (gs fog b) (ldb (byte 8 16) (gd-fogcol d)))
    ;; the texture: texels from local memory, a CLUT from the CLUT buffer (loaded from CBP)
    (setf (gs texture 0 enable) (gd-tme d) (gs texture 0 kind) (gfx-code texture-kind d2)
          (gs texture 0 format) (gs-psm-format (gd-tpsm d)) (gs texture 0 address) (* 256 (gd-tbp d))
          (gs texture 0 layout) (gs-psm-layout (gd-tpsm d)) (gs texture 0 pitch) (* 64 (gd-tbw d))
          (gs texture 0 width) (gd-tw d) (gs texture 0 height) (gd-th d)
          (gs texture 0 function) (gd-tfx d) (gs texture 0 use-alpha) (gd-tcc d) (gs texture 0 zero-discard) 0
          (gs texture 0 alpha0) (gd-ta0 d) (gs texture 0 alpha1) (gd-ta1 d) (gs texture 0 black-clear) (gd-aem d)
          (gs texture 0 clut-format) (if indexed
                                         (if (= (gd-cpsm d) 0) (texfmt-code a8b8g8r8) (texfmt-code a1b5g5r5))
                                         0)
          (gs texture 0 clut-offset) (if (= (gs-psm-bits (gd-tpsm d)) 4) (* 16 (gd-csa d)) 0)
          (gs sampler 0 address-u) (gs-wrap-mode (gd-wms d)) (gs sampler 0 address-v) (gs-wrap-mode (gd-wmt d))
...

7 more lines: the source.

(defun gs-expand16) source
(defun gs-expand16 (d v)
  "a 16-bit texel or CLUT entry as A8B8G8R8: 5-bit channels shifted, alpha from TEXA"
  (declare (type gd d) (type (unsigned-byte 32) v))
  (let ((rgb (logior (ash (ldb (byte 5 0) v) 3) (ash (ldb (byte 5 5) v) 11) (ash (ldb (byte 5 10) v) 19))))
    (logior rgb
            (ash (cond ((logbitp 15 v) (gd-ta1 d))
                       ((and (= (gd-aem d) 1) (= (logand v #x7FFF) 0)) 0)
                       (t (gd-ta0 d)))
                 24))))
(defun gs-clut-color) source
(defun gs-clut-color (g d i)
  "CLUT entry I as A8B8G8R8"
  (declare (type gsm g) (type gd d) (type (unsigned-byte 32) i))
  (let ((e (aref (gsm-clut g) (logand i 255))))
    (if (= (gd-cpsm d) 0) e (gs-expand16 d (logand e #xFFFF)))))
(defun gs-texel) source
(defun gs-texel (g d u v)
  "the texel at (U, V), already wrapped, as A8B8G8R8"
  (declare (type gsm g) (type gd d) (type (unsigned-byte 32) u v))
  (let* ((psm (gd-tpsm d)) (raw (gs-read-pixel g psm (gd-tbp d) (gd-tbw d) u v)))
    (case psm
      ((#x00 #x30) raw)
      ((#x01 #x31) (logior raw (ash (if (and (= (gd-aem d) 1) (= raw 0)) 0 (gd-ta0 d)) 24)))
      ((#x02 #x0A #x32 #x3A) (gs-expand16 d raw))
      ((#x13 #x1B) (gs-clut-color g d raw))
      ((#x14 #x24 #x2C) (gs-clut-color g d (+ (* 16 (gd-csa d)) raw)))
      (t raw))))
(defun gs-wrap) source
(defun gs-wrap (c size mode lo hi)
  "texel coordinate C wrapped by CLAMP's mode: 0 repeat, 1 clamp, 2 region clamp, 3 region repeat"
  (declare (type (signed-byte 64) c) (type (unsigned-byte 32) size lo hi) (type (unsigned-byte 8) mode))
  (case mode
    (0 (logand c (- size 1)))
    (1 (max 0 (min c (- size 1))))
    (2 (max lo (min c hi)))
    (t (logior (logand c lo) hi))))
(defun gs-dither) source
(defun gs-dither (d x y)
  "DIMX's value for pixel (X, Y): signed 3 bits"
  (declare (type gd d) (type (unsigned-byte 32) x y))
  (let ((v (ldb (byte 3 (+ (* 16 (logand y 3)) (* 4 (logand x 3)))) (gd-dimx d))))
    (if (>= v 4) (- v 8) v)))
(defmacro gs-blend-sel) source
(defmacro gs-blend-sel (sel s dd)
  `(case ,sel (0 ,s) (1 ,dd) (t 0)))
(defun gs-pixel) source
(defun gs-pixel (g d x y z color fog)
  "the pixel pipeline for pixel (X, Y) with depth Z, colour COLOR (A8B8G8R8, after the
   texture function) and fog FOG"
  (declare (type gsm g) (type gd d) (type (signed-byte 32) x y) (type (unsigned-byte 32) z color)
           (type (unsigned-byte 8) fog))
  ;; scissor and scan mask
  (when (or (< x (gd-scx0 d)) (> x (gd-scx1 d)) (< y (gd-scy0 d)) (> y (gd-scy1 d))
            (and (= (gd-scanmsk d) 2) (= (logand y 1) 0)) (and (= (gd-scanmsk d) 3) (= (logand y 1) 1)))
    (return-from gs-pixel nil))
  (let* ((r (ldb (byte 8 0) color)) (gg (ldb (byte 8 8) color)) (b (ldb (byte 8 16) color))
         (a (ldb (byte 8 24) color)) (fpsm (gd-fpsm d))
         (write-fb 1) (write-z (if (or (= (gd-zte d) 0) (= (gd-zmsk d) 1)) 0 1)) (rgb-only 0)
         (ux (wrap 32 x)) (uy (wrap 32 y)))
    (declare (type (unsigned-byte 32) r gg b a) (type (unsigned-byte 1) write-fb write-z rgb-only))
    ;; fog: C = (F C + (255 - F) FOGCOL) >> 8
    (when (= (gd-fge d) 1)
      (let ((fc (gd-fogcol d)))
        (setq r (ash (+ (* fog r) (* (- 255 fog) (ldb (byte 8 0) fc))) -8)
              gg (ash (+ (* fog gg) (* (- 255 fog) (ldb (byte 8 8) fc))) -8)
              b (ash (+ (* fog b) (* (- 255 fog) (ldb (byte 8 16) fc))) -8))))
    ;; alpha test
    (when (= (gd-ate d) 1)
      (let* ((ar (gd-aref d))
             (pass (case (gd-atst d)
                     (0 nil) (1 t) (2 (< a ar)) (3 (<= a ar)) (4 (= a ar)) (5 (>= a ar)) (6 (> a ar))
                     (t (/= a ar)))))
        (unless pass
          (case (gd-afail d)
            (0 (return-from gs-pixel nil))
            (1 (setq write-z 0))
            (2 (setq write-fb 0))
            (t (setq write-z 0 rgb-only 1))))))
    ;; destination alpha test, depth test
    ;; the destination is read only when something needs it: blending, the destination alpha
    ;; test, a write mask (or RGB_ONLY's); otherwise the write replaces the pixel's bits
    (let* ((fw (gd-fbw d)) (fbp (gd-fbp d))
           (dst (if (or (= (gd-abe d) 1) (= (gd-date d) 1) (/= (gd-fbmsk d) 0) (= rgb-only 1))
                    (gs-read-pixel g fpsm fbp fw ux uy)
                    0)))
      (when (= (gd-date d) 1)
        (let ((bit (if (or (= fpsm #x02) (= fpsm #x0A)) (ldb (byte 1 15) dst) (ldb (byte 1 31) dst))))
          (unless (or (= fpsm #x01) (= bit (gd-datm d)))
            (return-from gs-pixel nil))))
      (when (= (gd-zte d) 1)
        (let* ((zpsm (gd-zpsm d))
               (zv (case zpsm (#x30 z) (#x31 (min z #xFFFFFF)) (t (min z #xFFFF))))
               (zd (gs-read-pixel g zpsm (gd-zbp d) fw ux uy)))
          (setq z zv)
          (case (gd-ztst d)
            (0 (return-from gs-pixel nil))
            (1)
            (2 (when (< zv zd) (return-from gs-pixel nil)))
            (t (when (<= zv zd) (return-from gs-pixel nil))))))
      (when (= write-z 1)
        (gs-write-pixel g (gd-zpsm d) (gd-zbp d) fw ux uy z))
      (when (= write-fb 0)
        (return-from gs-pixel nil))
      ;; blending: Cv = ((A - B) * C >> 7) + D
      (let* ((dc (gs-color32 fpsm dst))
             (ad (if (or (= fpsm #x01) (= fpsm #x31)) #x80 (ldb (byte 8 24) dc)))
...

33 more lines: the source.

(defun gs-texel-at) source
(defun gs-texel-at (g d u v)
  "the texel at (U, V) (whole texels, unwrapped) as A8B8G8R8"
  (declare (type gsm g) (type gd d) (type (signed-byte 64) u v))
  (gs-texel g d (gs-wrap u (gd-tw d) (gd-wms d) (gd-minu d) (gd-maxu d))
            (gs-wrap v (gd-th d) (gd-wmt d) (gd-minv d) (gd-maxv d))))
(defun gs-bilinear) source
(defun gs-bilinear (g d u v)
  "the texel at (U, V) in sixteenths of a texel, filtered: the four texels around the point
   (centres at half a texel) weighted by its 4-bit fractions [GSUM] \"TEX1\" LINEAR"
  (declare (type gsm g) (type gd d) (type (signed-byte 64) u v))
  (let* ((u8 (- u 8)) (v8 (- v 8)) (u0 (ash u8 -4)) (v0 (ash v8 -4)) (fu (logand u8 15)) (fv (logand v8 15))
         (t00 (gs-texel-at g d u0 v0)) (t10 (gs-texel-at g d (+ u0 1) v0))
         (t01 (gs-texel-at g d u0 (+ v0 1))) (t11 (gs-texel-at g d (+ u0 1) (+ v0 1)))
         (out 0))
    (declare (type (unsigned-byte 32) out))
    (dotimes (k 4)
      (let ((sh (* 8 k)))
        (setq out (logior out (ash (ash (+ (* (- 16 fu) (- 16 fv) (ldb (byte 8 sh) t00)) (* fu (- 16 fv) (ldb (byte 8 sh) t10))
                                           (* (- 16 fu) fv (ldb (byte 8 sh) t01)) (* fu fv (ldb (byte 8 sh) t11)))
                                        -8)
                                   sh)))))
    out))
(defun gs-shade) source
(defun gs-shade (g d color u v)
  "COLOR (A8B8G8R8, the vertex colour) through the texture at (U, V) (sixteenths of a texel,
   unwrapped) and TFX: the nearest texel, or the bilinear filter with TEX1.MMAG (minification
   and mipmaps are not modelled: MMAG serves every size)"
  (declare (type gsm g) (type gd d) (type (unsigned-byte 32) color) (type (signed-byte 64) u v))
  (if (= (gd-tme d) 0)
      color
      (let* ((t32 (if (= (gd-mmag d) 1) (gs-bilinear g d u v) (gs-texel-at g d (ash u -4) (ash v -4))))
             (fr (ldb (byte 8 0) color)) (fg (ldb (byte 8 8) color)) (fb (ldb (byte 8 16) color))
             (fa (ldb (byte 8 24) color))
             (tr (ldb (byte 8 0) t32)) (tg (ldb (byte 8 8) t32)) (tb (ldb (byte 8 16) t32))
             (ta (ldb (byte 8 24) t32)) (tcc (gd-tcc d)))
        (case (gd-tfx d)
          (0 (logior (min 255 (ash (* fr tr) -7)) (ash (min 255 (ash (* fg tg) -7)) 8)
                     (ash (min 255 (ash (* fb tb) -7)) 16)
                     (ash (if (= tcc 1) (min 255 (ash (* fa ta) -7)) fa) 24)))
          (1 (logior (logand t32 #xFFFFFF) (ash (if (= tcc 1) ta fa) 24)))
          (2 (logior (min 255 (+ (ash (* fr tr) -7) fa)) (ash (min 255 (+ (ash (* fg tg) -7) fa)) 8)
                     (ash (min 255 (+ (ash (* fb tb) -7) fa)) 16)
                     (ash (if (= tcc 1) (min 255 (+ ta fa)) fa) 24)))
          (t (logior (min 255 (+ (ash (* fr tr) -7) fa)) (ash (min 255 (+ (ash (* fg tg) -7) fa)) 8)
                     (ash (min 255 (+ (ash (* fb tb) -7) fa)) 16)
                     (ash (if (= tcc 1) ta fa) 24)))))))
(defun gs-texcoord) source
(defun gs-texcoord (d s q size)
  "an ST coordinate S / Q as a texel coordinate (in sixteenths, as UV's) of a texture SIZE texels across"
  (declare (type gd d) (type single-float s q) (type (unsigned-byte 32) size))
  (declare (ignore d))
  (let ((tc (if (= q 0.0) 0.0 (* (/ s q) (float (* 16 size) 1.0)))))
    (if (> (abs tc) 1.0e9) 0 (floor tc))))
(defmacro gs-chan) source
(defmacro gs-chan (c i) `(float (ldb (byte 8 ,i) ,c) 1.0))
(defun gs-lerp-color) source
(defun gs-lerp-color (c0 c1 c2 w0 w1 w2)
  "the colour W0 C0 + W1 C1 + W2 C2, channel by channel"
  (declare (type (unsigned-byte 32) c0 c1 c2) (type single-float w0 w1 w2))
  (let ((out 0))
    (declare (type (unsigned-byte 32) out))
    (dotimes (k 4)
      (let* ((sh (* 8 k))
             (v (+ (* w0 (float (ldb (byte 8 sh) c0) 1.0)) (* w1 (float (ldb (byte 8 sh) c1) 1.0))
                   (* w2 (float (ldb (byte 8 sh) c2) 1.0))))
             (iv (max 0 (min 255 (floor v)))))
        (setq out (logior out (ash iv sh)))))
    out))
(defun gs-sprite) source
(defun gs-sprite (g d)
  "a sprite: the rectangle between vertices 0 and 1, attributes of vertex 1, coordinates interpolated"
  (declare (type gsm g) (type gd d))
  (let* ((x0 (aref (gsm-vx g) 0)) (y0 (aref (gsm-vy g) 0)) (x1 (aref (gsm-vx g) 1)) (y1 (aref (gsm-vy g) 1))
         (z (aref (gsm-vz g) 1)) (c (aref (gsm-vc g) 1)) (f (aref (gsm-vf g) 1))
         (fst (gd-fst d))
         (u0 (if (= fst 1) (float (aref (gsm-vu g) 0) 1.0) (/ (aref (gsm-vs g) 0) (max 1.0e-30 (aref (gsm-vq g) 1)))))
         (v0 (if (= fst 1) (float (aref (gsm-vv g) 0) 1.0) (/ (aref (gsm-vt g) 0) (max 1.0e-30 (aref (gsm-vq g) 1)))))
         (u1 (if (= fst 1) (float (aref (gsm-vu g) 1) 1.0) (/ (aref (gsm-vs g) 1) (max 1.0e-30 (aref (gsm-vq g) 1)))))
         (v1 (if (= fst 1) (float (aref (gsm-vv g) 1) 1.0) (/ (aref (gsm-vt g) 1) (max 1.0e-30 (aref (gsm-vq g) 1))))))
    (when (> x0 x1)
      (let ((tx x0) (tu u0)) (setq x0 x1 x1 tx u0 u1 u1 tu)))
    (when (> y0 y1)
      (let ((ty y0) (tv v0)) (setq y0 y1 y1 ty v0 v1 v1 tv)))
    (let ((px0 (max (gd-scx0 d) (ash (+ x0 15) -4))) (px1 (min (+ 1 (gd-scx1 d)) (ash (+ x1 15) -4)))
          (py0 (max (gd-scy0 d) (ash (+ y0 15) -4))) (py1 (min (+ 1 (gd-scy1 d)) (ash (+ y1 15) -4)))
          (dx (float (max 1 (- x1 x0)) 1.0)) (dy (float (max 1 (- y1 y0)) 1.0)))
      (loop for py from py0 below py1 do
        (let* ((fy (/ (float (the (signed-byte 32) (- (* py 16) y0)) 1.0) dy))
               (tv (+ v0 (* (- v1 v0) fy))))
          (loop for px from px0 below px1 do
            (let* ((fx (/ (float (the (signed-byte 32) (- (* px 16) x0)) 1.0) dx))
                   (tu (+ u0 (* (- u1 u0) fx)))
                   (iu (if (= fst 1) (floor tu) (floor (* tu (float (* 16 (gd-tw d)) 1.0)))))
                   (iv (if (= fst 1) (floor tv) (floor (* tv (float (* 16 (gd-th d)) 1.0))))))
              (gs-pixel g d px py z (gs-shade g d c iu iv) f))))))))
(defun gs-edge) source
(defun gs-edge (ax ay bx by px py)
  "twice the signed area of (A, B, P): positive when P is to the left of A -> B (y down)"
  (declare (type (signed-byte 32) ax ay bx by px py))
  (the (signed-byte 64) (- (* (the (signed-byte 32) (- bx ax)) (the (signed-byte 32) (- py ay)))
                          (* (the (signed-byte 32) (- by ay)) (the (signed-byte 32) (- px ax))))))
(defun gs-top-left-p) source
(defun gs-top-left-p (ax ay bx by)
  "is the edge A -> B of a counter-clockwise (positive area) triangle a top or left edge"
  (declare (type (signed-byte 32) ax ay bx by))
  (or (and (= ay by) (< bx ax)) (> ay by)))
(defun gs-triangle) source
(defun gs-triangle (g d)
  "a triangle of vertices 0, 1, 2"
  (declare (type gsm g) (type gd d))
  (let* ((vx (gsm-vx g)) (vy (gsm-vy g))
         (i1 1) (i2 2)
         (area (gs-edge (aref vx 0) (aref vy 0) (aref vx 1) (aref vy 1) (aref vx 2) (aref vy 2))))
    (declare (type (unsigned-byte 8) i1 i2))
    (when (= area 0) (return-from gs-triangle nil))
    (when (< area 0) (setq i1 2 i2 1 area (- area)))
    (let* ((x0 (aref vx 0)) (y0 (aref vy 0)) (x1 (aref vx i1)) (y1 (aref vy i1)) (x2 (aref vx i2)) (y2 (aref vy i2))
           (bx0 (max (gd-scx0 d) (ash (+ (min x0 x1 x2) 15) -4)))
           (bx1 (min (gd-scx1 d) (ash (max x0 x1 x2) -4)))
           (by0 (max (gd-scy0 d) (ash (+ (min y0 y1 y2) 15) -4)))
           (by1 (min (gd-scy1 d) (ash (max y0 y1 y2) -4)))
           (tl0 (gs-top-left-p x1 y1 x2 y2)) (tl1 (gs-top-left-p x2 y2 x0 y0)) (tl2 (gs-top-left-p x0 y0 x1 y1))
           (fa (float area 1.0)) (fst (gd-fst d)) (gouraud (gd-iip d)) (tme (gd-tme d))
           (c0 (aref (gsm-vc g) 0)) (c1 (aref (gsm-vc g) i1)) (c2 (aref (gsm-vc g) i2)))
      (loop for py from by0 to by1 do
        (loop for px from bx0 to bx1 do
          (let* ((sx (the (signed-byte 32) (* px 16))) (sy (the (signed-byte 32) (* py 16)))
                 (e0 (gs-edge x1 y1 x2 y2 sx sy)) (e1 (gs-edge x2 y2 x0 y0 sx sy)) (e2 (gs-edge x0 y0 x1 y1 sx sy)))
            (when (and (or (> e0 0) (and (= e0 0) tl0)) (or (> e1 0) (and (= e1 0) tl1))
                       (or (> e2 0) (and (= e2 0) tl2)))
              (let* ((w0 (/ (float e0 1.0) fa)) (w1 (/ (float e1 1.0) fa)) (w2 (/ (float e2 1.0) fa))
                     (z (max 0 (min #xFFFFFFFF
                                    (floor (+ (* (float w0 1.0d0) (float (aref (gsm-vz g) 0) 1.0d0))
                                              (* (float w1 1.0d0) (float (aref (gsm-vz g) i1) 1.0d0))
                                              (* (float w2 1.0d0) (float (aref (gsm-vz g) i2) 1.0d0)))))))
                     (color (if (= gouraud 1) (gs-lerp-color c0 c1 c2 w0 w1 w2) c2))
                     (fog (max 0 (min 255 (floor (+ (* w0 (float (aref (gsm-vf g) 0) 1.0))
                                                    (* w1 (float (aref (gsm-vf g) i1) 1.0))
                                                    (* w2 (float (aref (gsm-vf g) i2) 1.0))))))))
                (if (= tme 0)
                    (gs-pixel g d px py z color fog)
                    (if (= fst 1)
                        (let ((u (+ (* w0 (float (aref (gsm-vu g) 0) 1.0)) (* w1 (float (aref (gsm-vu g) i1) 1.0))
                                    (* w2 (float (aref (gsm-vu g) i2) 1.0))))
                              (v (+ (* w0 (float (aref (gsm-vv g) 0) 1.0)) (* w1 (float (aref (gsm-vv g) i1) 1.0))
                                    (* w2 (float (aref (gsm-vv g) i2) 1.0)))))
                          (gs-pixel g d px py z (gs-shade g d color (floor u) (floor v)) fog))
                        (let ((s (+ (* w0 (aref (gsm-vs g) 0)) (* w1 (aref (gsm-vs g) i1)) (* w2 (aref (gsm-vs g) i2))))
                              (tt (+ (* w0 (aref (gsm-vt g) 0)) (* w1 (aref (gsm-vt g) i1)) (* w2 (aref (gsm-vt g) i2))))
                              (q (+ (* w0 (aref (gsm-vq g) 0)) (* w1 (aref (gsm-vq g) i1)) (* w2 (aref (gsm-vq g) i2)))))
                          (gs-pixel g d px py z
                                    (gs-shade g d color (gs-texcoord d s q (gd-tw d)) (gs-texcoord d tt q (gd-th d)))
                                    fog)))))))))))
  nil)
(defun gs-line) source
(defun gs-line (g d)
  "a line from vertex 0 to vertex 1, its last pixel left out, coordinates interpolated"
  (declare (type gsm g) (type gd d))
  (let* ((x0 (aref (gsm-vx g) 0)) (y0 (aref (gsm-vy g) 0)) (x1 (aref (gsm-vx g) 1)) (y1 (aref (gsm-vy g) 1))
         (n (max 1 (ash (max (abs (- x1 x0)) (abs (- y1 y0))) -4)))
         (c0 (aref (gsm-vc g) 0)) (c1 (aref (gsm-vc g) 1)))
    (dotimes (i n)
      (let* ((w1 (/ (float i 1.0) (float n 1.0))) (w0 (- 1.0 w1))
             (px (ash (+ x0 (floor (* w1 (float (- x1 x0) 1.0))) 8) -4))
             (py (ash (+ y0 (floor (* w1 (float (- y1 y0) 1.0))) 8) -4))
             (z (floor (+ (* (float w0 1.0d0) (float (aref (gsm-vz g) 0) 1.0d0))
                          (* (float w1 1.0d0) (float (aref (gsm-vz g) 1) 1.0d0)))))
             (color (if (= (gd-iip d) 1) (gs-lerp-color c0 c1 c1 w0 w1 0.0) c1)))
        (gs-pixel g d px py (max 0 (min #xFFFFFFFF z))
                  (if (= (gd-tme d) 0)
                      color
                      (if (= (gd-fst d) 1)
                          (gs-shade g d color
                                    (floor (+ (* w0 (float (aref (gsm-vu g) 0) 1.0)) (* w1 (float (aref (gsm-vu g) 1) 1.0))))
                                    (floor (+ (* w0 (float (aref (gsm-vv g) 0) 1.0)) (* w1 (float (aref (gsm-vv g) 1) 1.0)))))
                          (let ((q (+ (* w0 (aref (gsm-vq g) 0)) (* w1 (aref (gsm-vq g) 1)))))
                            (gs-shade g d color
                                      (gs-texcoord d (+ (* w0 (aref (gsm-vs g) 0)) (* w1 (aref (gsm-vs g) 1))) q (gd-tw d))
                                      (gs-texcoord d (+ (* w0 (aref (gsm-vt g) 0)) (* w1 (aref (gsm-vt g) 1))) q (gd-th d))))))
                  (aref (gsm-vf g) 1)))))
  nil)
(defun gs-draw-prim) source
(defun gs-draw-prim (g attr)
  "draw the primitive whose vertices are queued, with attributes ATTR"
  (declare (type gsm g) (type (unsigned-byte 32) attr))
  (gs-setup g attr)
  (gs-state g attr)
  (setf (gsm-prims g) (wrap 64 (+ (gsm-prims g) 1)))
  (let ((d *gd*))
    (case (logand attr 7)
      (0 (gs-pixel g d (ash (+ (aref (gsm-vx g) 0) 8) -4) (ash (+ (aref (gsm-vy g) 0) 8) -4)
                   (aref (gsm-vz g) 0)
                   (gs-shade g d (aref (gsm-vc g) 0) (aref (gsm-vu g) 0) (aref (gsm-vv g) 0))
                   (aref (gsm-vf g) 0)))
      ((1 2) (gs-line g d))
      ((3 4 5) (gs-triangle g d))
      (6 (gs-sprite g d))))
  (setf (gsm-draws g) (wrap 64 (+ (gsm-draws g) 1)))
  nil)
(defun gs-indexed-p) source
(defun gs-indexed-p (psm)
  "is PSM a CLUT format: PSMT8, PSMT4, PSMT8H, PSMT4HL, PSMT4HH"
  (declare (type (unsigned-byte 8) psm))
  (case psm ((#x13 #x14 #x1B #x24 #x2C) t) (t nil)))
(defun gs-clut-load) source
(defun gs-clut-load (g tex0)
  "TEX0's CLD [GSUM] \"TEX0\": 0 no load, 1 load, 2 load and CBP0 <- CBP, 3 load and CBP1 <-
   CBP, 4 load if CBP0 /= CBP (and set it), 5 the same with CBP1. A load reads 16 entries (a
   4-bit texture, at CSA * 16) or 256 (8-bit) of format CPSM from local memory: CSM1 as an
   8 x 2 or 16 x 16 block (8-bit indices with bits 3 and 4 exchanged), CSM2 from TEXCLUT's
   position, a row"
  (declare (type gsm g) (type (unsigned-byte 64) tex0))
  (let* ((cld (gsf tex0 61 3)) (cbp (gsf tex0 37 14)) (psm (gsf tex0 20 6)) (cpsm (gsf tex0 51 4))
         (csm (gsf tex0 55 1)) (csa (gsf tex0 56 5)) (cpsm-full (if (= cpsm 0) #x00 (if (= cpsm 2) #x02 #x0A)))
         (load (case cld
                 (0 nil) (1 t)
                 (2 (setf (gsm-cbp0 g) cbp) t)
                 (3 (setf (gsm-cbp1 g) cbp) t)
                 (4 (if (/= (gsm-cbp0 g) cbp) (progn (setf (gsm-cbp0 g) cbp) t) nil))
                 (5 (if (/= (gsm-cbp1 g) cbp) (progn (setf (gsm-cbp1 g) cbp) t) nil))
                 (t nil))))
    (when (and load (gs-indexed-p psm))
      (let* ((four (= (gs-psm-bits psm) 4)) (n (if four 16 256)) (base (if four (* 16 csa) 0))
             (tc (greg texclut)) (cbw (gsf tc 0 6)) (cou (gsf tc 6 6)) (cov (gsf tc 12 10)))
        (dotimes (i n)
          (let* ((x (if (= csm 0)
                        (if four
                            (logand i 7)
                            (logand (logior (logand i #xE7) (ash (logand i 8) 1) (ash (logand i 16) -1)) 15))
                        (+ (* cou 16) i)))
                 (y (if (= csm 0)
                        (if four
                            (ash i -3)
                            (ash (logior (logand i #xE7) (ash (logand i 8) 1) (ash (logand i 16) -1)) -4))
                        cov)))
            (setf (aref (gsm-clut g) (logand (+ base i) 255))
                  (gs-read-pixel g cpsm-full cbp (if (= csm 0) 1 cbw) x y)))))))
  nil)

Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.