the reference rasterizer: PS1 polygons, lines, rectangles, fills and copies into VRAM.
(defstruct rctx) source(defstruct (rctx (:conc-name rc-))
(textured 0 :type u8) (raw 0 :type u8) (shaded 0 :type u8) (transp 0 :type u8) (select 0 :type u8)
(ca 0 :type u8) (cb 0 :type u8) (cd 0 :type u8) (cfix 0 :type u8) (dither 0 :type u8)
(tpx 0 :type u16) (tpy 0 :type u16) (depth 0 :type u8) (clutx 0 :type u16) (cluty 0 :type u16)
(twmx 0 :type u8) (twmy 0 :type u8) (twox 0 :type u8) (twoy 0 :type u8)
(mask-or 0 :type u16) (mask-check 0 :type u8)
(x1 0 :type s32) (y1 0 :type s32) (x2 0 :type s32) (y2 0 :type s32))(defvar *rc*) source(defvar *rc* (make-rctx))(defvar *vx-x*) source(defvar *vx-x* (make-array 4 :element-type '(signed-byte 32) :initial-element 0))(defvar *vx-y*) source(defvar *vx-y* (make-array 4 :element-type '(signed-byte 32) :initial-element 0))(defvar *vx-c*) source(defvar *vx-c* (make-array 4 :element-type '(unsigned-byte 32) :initial-element 0))(defvar *vx-u*) source(defvar *vx-u* (make-array 4 :element-type '(signed-byte 32) :initial-element 0))(defvar *vx-v*) source(defvar *vx-v* (make-array 4 :element-type '(signed-byte 32) :initial-element 0))(defun rctx-init) source(defun rctx-init (gpu)
"the primitive the abstract state describes into *RC* and *VX-...*"
(declare (type gpu gpu))
(let ((rc *rc*) (gfx (gpu-gfx gpu)))
(setf (rc-x1 rc) (gs scissor 0 x) (rc-y1 rc) (gs scissor 0 y)
(rc-x2 rc) (- (+ (gs scissor 0 x) (gs scissor 0 width)) 1)
(rc-y2 rc) (- (+ (gs scissor 0 y) (gs scissor 0 height)) 1)
(rc-twmx rc) (logand (lognot (gs sampler 0 region-u0)) #xFF)
(rc-twmy rc) (logand (lognot (gs sampler 0 region-v0)) #xFF)
(rc-twox rc) (gs sampler 0 region-u1) (rc-twoy rc) (gs sampler 0 region-v1)
(rc-mask-or rc) (if (= (gs mask-bit force) 1) #x8000 0)
(rc-mask-check rc) (gs mask-bit test)
(rc-dither rc) (gs raster dither)
(rc-textured rc) (gs texture 0 enable)
(rc-raw rc) (if (= (gs texture 0 function) (gfx-code texture-function decal)) 1 0)
(rc-shaded rc) (if (= (gs raster shade-model) (gfx-code shade-model smooth)) 1 0)
(rc-transp rc) (gs combine enable)
(rc-select rc) (if (= (gs combine select) (gfx-code combine-select all)) 0 1)
(rc-ca rc) (gs combine a) (rc-cb rc) (gs combine b) (rc-cd rc) (gs combine d)
(rc-cfix rc) (gs combine fixed))
(when (= (rc-textured rc) 1)
(let ((a (ash (gs texture 0 address) -1)) (c (ash (gs texture 0 clut-address) -1))
(f (gs texture 0 format)))
(setf (rc-tpx rc) (logand a 1023) (rc-tpy rc) (logand (ash a -10) 511)
(rc-clutx rc) (logand c 1023) (rc-cluty rc) (logand (ash c -10) 511)
(rc-depth rc) (cond ((= f (texfmt-code index4)) 0) ((= f (texfmt-code index8)) 1) (t 2)))))
(dotimes (i (gs immediate count))
(setf (aref *vx-x* i) (gs immediate i x) (aref *vx-y* i) (gs immediate i y)
(aref *vx-c* i) (gs immediate i color) (aref *vx-u* i) (gs immediate i u)
(aref *vx-v* i) (gs immediate i v)))
nil))(defun gpu-dither) source(defun gpu-dither (x y)
"the dither matrix at pixel (X, Y): -4..3 ([PSXSPX] \"Dithering\")"
(declare (type s32 x y))
(let ((k (if (logbitp 1 y) #x26374051 #x37265140)))
(- (logand (ash k (- (logior (ash (logand y 1) 4) (ash (logand x 3) 2)))) 15) 4)))(defmacro sat5) source(defmacro sat5 (v)
"an 8-bit channel value (maybe out of range) as 5 bits, saturated"
(let ((s (gensym "V")))
`(let ((,s ,v)) (cond ((<= ,s 0) 0) ((>= ,s 255) 31) (t (ash ,s -3))))))(defmacro clamp31) source(defmacro clamp31 (v)
(let ((s (gensym "V")))
`(let ((,s ,v)) (cond ((< ,s 0) 0) ((> ,s 31) 31) (t ,s)))))(defmacro combine-term) source(defmacro combine-term (sel b f)
"a combine colour operand: the source F, the destination B, or 0"
`(case ,sel (0 ,f) (1 ,b) (t 0)))(defun gpu-blend) source(defun gpu-blend (b f)
"the combine equation of *RC* on 5-bit channels of back pixel B and front pixel F:
((A - B) * FIX >> 7) + D, clamped; bit 15 of the result 0"
(declare (type u16 b f))
(let ((rc *rc*) (out 0))
(declare (type u16 out))
(dotimes (k 3)
(let* ((bc (logand (ash b (- (* 5 k))) 31)) (fc (logand (ash f (- (* 5 k))) 31))
(v (+ (ash (* (- (combine-term (rc-ca rc) bc fc) (combine-term (rc-cb rc) bc fc)) (rc-cfix rc)) -7)
(combine-term (rc-cd rc) bc fc))))
(declare (type s32 bc fc v))
(setq out (logior out (ash (clamp31 v) (* 5 k))))))
out))(defun texel) source(defun texel (gpu u v)
"the texel at (U, V) of *RC*'s page, through the window and the CLUT"
(declare (type gpu gpu) (type u32 u v))
(let* ((rc *rc*)
(uu (logand (logior (logand u (lognot (rc-twmx rc))) (rc-twox rc)) #xFF))
(vv (logand (logior (logand v (lognot (rc-twmy rc))) (rc-twoy rc)) #xFF))
(y (+ (rc-tpy rc) vv)))
(case (rc-depth rc)
(0 (let ((t4 (vram-ref gpu (+ (rc-tpx rc) (ash uu -2)) y)))
(vram-ref gpu (+ (rc-clutx rc) (logand (ash t4 (- (ash (logand uu 3) 2))) 15)) (rc-cluty rc))))
(1 (let ((t8 (vram-ref gpu (+ (rc-tpx rc) (ash uu -1)) y)))
(vram-ref gpu (+ (rc-clutx rc) (logand (ash t8 (- (ash (logand uu 1) 3))) #xFF)) (rc-cluty rc))))
(t (vram-ref gpu (+ (rc-tpx rc) uu) y)))))(defun plot) source(defun plot (gpu x y c u v dith)
"one pixel at (X, Y), inside VRAM and the drawing area: flat or
interpolated colour C (#xBBGGRR), texture coordinates U V, DITH 1 to dither"
(declare (type gpu gpu) (type s32 x y) (type u32 c u v) (type u8 dith))
(let* ((rc *rc*) (i (+ (logand x 1023) (* 1024 (logand y 511))))
(dst (aref (gpu-vram gpu) i))
(semi (rc-transp rc)) (bit15 0) (pr 0) (pg 0) (pb 0))
(declare (type u16 bit15) (type u32 pr pg pb) (type u8 semi))
(when (and (= (rc-mask-check rc) 1) (logbitp 15 dst))
(return-from plot nil))
(if (= (rc-textured rc) 1)
(let ((tx (texel gpu u v)))
(when (= tx 0) (return-from plot nil))
(setq bit15 (logand tx #x8000)
semi (if (and (= (rc-transp rc) 1) (or (= (rc-select rc) 0) (/= bit15 0))) 1 0))
(if (= (rc-raw rc) 1)
(setq pr (logand tx 31) pg (logand (ash tx -5) 31) pb (logand (ash tx -10) 31))
(let ((d (if (= dith 1) (gpu-dither x y) 0)))
(setq pr (sat5 (+ (ash (* (logand tx 31) (logand c #xFF)) -4) d))
pg (sat5 (+ (ash (* (logand (ash tx -5) 31) (logand (ash c -8) #xFF)) -4) d))
pb (sat5 (+ (ash (* (logand (ash tx -10) 31) (logand (ash c -16) #xFF)) -4) d))))))
(let ((d (if (= dith 1) (gpu-dither x y) 0)))
(setq pr (sat5 (+ (logand c #xFF) d))
pg (sat5 (+ (logand (ash c -8) #xFF) d))
pb (sat5 (+ (logand (ash c -16) #xFF) d)))))
(let ((f (logior pr (ash pg 5) (ash pb 10))))
(when (= semi 1)
(setq f (gpu-blend dst f)))
(setf (aref (gpu-vram gpu) i) (logior f bit15 (rc-mask-or rc)))
(setf (gpu-pixels gpu) (wrap 64 (+ (gpu-pixels gpu) 1))))
nil))(defun gradient) source(defun gradient (a b c area fa fb fc dir)
"the 20.12 derivative of an attribute over the plane through vertices A
B C (indices into *vx-...*) with values FA FB FC: DIR 0 d/dx, 1 d/dy"
(declare (type u8 a b c dir) (type s64 area) (type s32 fa fb fc))
(let ((d1 (- fb fa)) (d2 (- fc fa))
(y1 (- (aref *vx-y* b) (aref *vx-y* a))) (y2 (- (aref *vx-y* c) (aref *vx-y* a)))
(x1 (- (aref *vx-x* b) (aref *vx-x* a))) (x2 (- (aref *vx-x* c) (aref *vx-x* a))))
(declare (type s64 d1 d2 y1 y2 x1 x2))
(sext 32 (wrap 32 (truncate (* (if (= dir 0) (- (* d1 y2) (* d2 y1)) (- (* d2 x1) (* d1 x2))) 4096) area)))))(defmacro chan) source(defmacro chan (c n) `(logand (ash ,c ,(- n)) #xFF))(defun raster-triangle) source(defun raster-triangle (gpu i0 i1 i2)
"the triangle of the abstract state's immediate vertices I0 I1 I2"
(declare (type gpu gpu) (type u8 i0 i1 i2))
(rctx-init gpu)
(let ((rc *rc*))
(let ((dither (rc-dither rc))
(flat (aref *vx-c* i0))
(area (- (* (- (aref *vx-x* i1) (aref *vx-x* i0)) (- (aref *vx-y* i2) (aref *vx-y* i0)))
(* (- (aref *vx-x* i2) (aref *vx-x* i0)) (- (aref *vx-y* i1) (aref *vx-y* i0)))))
(top i0) (mid i1) (bot i2) (tmp 0)
(r0 0) (r1 0) (r2 0) (g0 0) (g1 0) (g2 0) (b0 0) (b1 0) (b2 0)
(drdx 0) (drdy 0) (dgdx 0) (dgdy 0) (dbdx 0) (dbdy 0) (dudx 0) (dudy 0) (dvdx 0) (dvdy 0))
(declare (type u8 dither top mid bot tmp) (type s64 area)
(type s32 r0 r1 r2 g0 g1 g2 b0 b1 b2 drdx drdy dgdx dgdy dbdx dbdy dudx dudy dvdx dvdy))
(when (= area 0) (return-from raster-triangle nil))
;; sorted by y
(when (< (aref *vx-y* mid) (aref *vx-y* top)) (setq tmp top top mid mid tmp))
(when (< (aref *vx-y* bot) (aref *vx-y* top)) (setq tmp top top bot bot tmp))
(when (< (aref *vx-y* bot) (aref *vx-y* mid)) (setq tmp mid mid bot bot tmp))
(let ((xmin (min (aref *vx-x* i0) (aref *vx-x* i1) (aref *vx-x* i2)))
(xmax (max (aref *vx-x* i0) (aref *vx-x* i1) (aref *vx-x* i2))))
(when (or (>= (- xmax xmin) 1024) (>= (- (aref *vx-y* bot) (aref *vx-y* top)) 512))
(return-from raster-triangle nil)))
(if (= (rc-shaded rc) 1)
(let ((c0 (aref *vx-c* i0)) (c1 (aref *vx-c* i1)) (c2 (aref *vx-c* i2)))
(setq r0 (chan c0 0) r1 (chan c1 0) r2 (chan c2 0)
g0 (chan c0 8) g1 (chan c1 8) g2 (chan c2 8)
b0 (chan c0 16) b1 (chan c1 16) b2 (chan c2 16))
(setq drdx (gradient i0 i1 i2 area r0 r1 r2 0) drdy (gradient i0 i1 i2 area r0 r1 r2 1)
dgdx (gradient i0 i1 i2 area g0 g1 g2 0) dgdy (gradient i0 i1 i2 area g0 g1 g2 1)
dbdx (gradient i0 i1 i2 area b0 b1 b2 0) dbdy (gradient i0 i1 i2 area b0 b1 b2 1)))
(setq r0 (chan flat 0) g0 (chan flat 8) b0 (chan flat 16)))
(when (= (rc-textured rc) 1)
(let ((u0 (aref *vx-u* i0)) (u1 (aref *vx-u* i1)) (u2 (aref *vx-u* i2))
(v0 (aref *vx-v* i0)) (v1 (aref *vx-v* i1)) (v2 (aref *vx-v* i2)))
(setq dudx (gradient i0 i1 i2 area u0 u1 u2 0) dudy (gradient i0 i1 i2 area u0 u1 u2 1)
dvdx (gradient i0 i1 i2 area v0 v1 v2 0) dvdy (gradient i0 i1 i2 area v0 v1 v2 1))))
(when (= (aref *vx-y* bot) (aref *vx-y* top)) (return-from raster-triangle nil))
(setf (gpu-draws gpu) (wrap 64 (+ (gpu-draws gpu) 1)))
(let* ((tx (aref *vx-x* top)) (ty (aref *vx-y* top)) (mx (aref *vx-x* mid)) (my (aref *vx-y* mid))
(bx (aref *vx-x* bot)) (by (aref *vx-y* bot))
(long-dx (truncate (* (- bx tx) 65536) (- by ty)))
(lx (* tx 65536))
(long-left (> (- (* (- mx tx) (- by ty)) (* (- bx tx) (- my ty))) 0))
(x0 (aref *vx-x* i0)) (y0 (aref *vx-y* i0))
(tu (aref *vx-u* i0)) (tv (aref *vx-v* i0)))
(declare (type s32 tx ty mx my bx by long-dx lx x0 y0))
(dotimes (half 2)
(let* ((sa (if (= half 0) top mid)) (sb (if (= half 0) mid bot))
(ya (aref *vx-y* sa)) (yb (aref *vx-y* sb)))
(declare (type s32 ya yb))
(unless (= ya yb)
(let ((short-dx (truncate (* (- (aref *vx-x* sb) (aref *vx-x* sa)) 65536) (- yb ya)))
(sx (* (aref *vx-x* sa) 65536))
(y ya))
(declare (type s32 short-dx sx y))
(loop while (< y yb) do
(when (and (>= y (rc-y1 rc)) (<= y (rc-y2 rc)))
(let* ((xl (if long-left lx sx)) (xr (if long-left sx lx))
(x (ash (+ xl #xFFFF) -16)) (xe (ash (+ xr #xFFFF) -16)))
...28 more lines: the source.
(defun sext11) source(defun sext11 (v)
(declare (type s32 v))
(sext 11 (wrap 11 v)))(defun raster-rect) source(defun raster-rect (gpu)
"the rectangle of the abstract state's immediate vertices 0 (its top left
corner, texel coordinates there) and 1 (the bottom right, excluded); the
colour vertex 0's, never dithered"
(declare (type gpu gpu))
(rctx-init gpu)
(let* ((rc *rc*)
(x0 (aref *vx-x* 0)) (y0 (aref *vx-y* 0))
(u (wrap 32 (aref *vx-u* 0))) (v (wrap 32 (aref *vx-v* 0))) (c (aref *vx-c* 0))
(cx0 (max x0 (rc-x1 rc))) (cy0 (max y0 (rc-y1 rc)))
(cx1 (min (- (aref *vx-x* 1) 1) (rc-x2 rc))) (cy1 (min (- (aref *vx-y* 1) 1) (rc-y2 rc))))
(declare (type s32 x0 y0 cx0 cy0 cx1 cy1) (type u32 u v c))
(when (or (> cx0 cx1) (> cy0 cy1)) (return-from raster-rect nil))
(setf (gpu-draws gpu) (wrap 64 (+ (gpu-draws gpu) 1)))
(let ((yy cy0))
(declare (type s32 yy))
(loop while (<= yy cy1) do
(let ((xx cx0))
(declare (type s32 xx))
(loop while (<= xx cx1) do
(plot gpu xx yy c (wrap 32 (+ u (- xx x0))) (wrap 32 (+ v (- yy y0))) 0)
(setq xx (+ xx 1))))
(setq yy (+ yy 1))))
nil))(defun raster-line) source(defun raster-line (gpu)
"the line from the abstract state's immediate vertex 0 to 1, both ends drawn"
(declare (type gpu gpu))
(rctx-init gpu)
(let* ((rc *rc*) (i0 0) (i1 1)
(x0 (aref *vx-x* i0)) (y0 (aref *vx-y* i0)) (x1 (aref *vx-x* i1)) (y1 (aref *vx-y* i1))
(dx (- x1 x0)) (dy (- y1 y0))
(steps (max (abs dx) (abs dy)))
(dither (rc-dither rc))
(a (aref *vx-c* i0)) (b (if (= (rc-shaded rc) 1) (aref *vx-c* i1) a)))
(declare (type s32 x0 y0 x1 y1 dx dy steps))
(when (or (>= (abs dx) 1024) (>= (abs dy) 512)) (return-from raster-line nil))
(setf (gpu-draws gpu) (wrap 64 (+ (gpu-draws gpu) 1)))
(let* ((x (+ (* x0 65536) #x8000)) (y (+ (* y0 65536) #x8000))
(sx (if (= steps 0) 0 (truncate (* dx 65536) steps)))
(sy (if (= steps 0) 0 (truncate (* dy 65536) steps)))
(cr (ash (chan a 0) 16)) (cg (ash (chan a 8) 16)) (cb (ash (chan a 16) 16))
(sr (if (= steps 0) 0 (truncate (- (ash (chan b 0) 16) cr) steps)))
(sg (if (= steps 0) 0 (truncate (- (ash (chan b 8) 16) cg) steps)))
(sb (if (= steps 0) 0 (truncate (- (ash (chan b 16) 16) cb) steps)))
(i 0))
(declare (type s32 x y sx sy cr cg cb sr sg sb i))
(loop while (<= i steps) do
(let ((px (ash x -16)) (py (ash y -16)))
(when (and (>= px (rc-x1 rc)) (<= px (rc-x2 rc)) (>= py (rc-y1 rc)) (<= py (rc-y2 rc)))
(plot gpu px py (logior (ash cr -16) (ash (ash cg -16) 8) (ash (ash cb -16) 16)) 0 0 dither)))
(setq x (+ x sx) y (+ y sy) cr (+ cr sr) cg (+ cg sg) cb (+ cb sb) i (+ i 1))))
nil))(defun raster-fill) source(defun raster-fill (gpu x y w h c)
"GP0(02h): the rectangle (X AND 3F0h, Y AND 1FFh) of ((W AND 3FFh) + 0Fh)
AND NOT 0Fh by H AND 1FFh pixels in colour C (#xBBGGRR, 5 bits a
channel kept, bit 15 0), wrapping; no drawing area, offset or mask"
(declare (type gpu gpu) (type u32 x y w h c))
(let ((x0 (logand x #x3F0)) (y0 (logand y #x1FF))
(ww (logand (+ (logand w #x3FF) 15) #xFFF0)) (hh (logand h #x1FF))
(p (logior (ash (chan c 0) -3) (ash (ash (chan c 8) -3) 5) (ash (ash (chan c 16) -3) 10))))
(dotimes (j hh)
(dotimes (i ww)
(vram-set gpu (+ x0 i) (+ y0 j) p)))
(setf (gpu-pixels gpu) (wrap 64 (+ (gpu-pixels gpu) (* ww hh))))
(gpu-written gpu x0 y0 ww hh)
nil))(defun raster-copy) source(defun raster-copy (gpu sx sy dx dy w h)
"GP0(80h): VRAM to VRAM, sizes 0 meaning the maximum, wrapping; the mask
bits apply (\"Masking for COPY Commands parameters\")"
(declare (type gpu gpu) (type u32 sx sy dx dy w h))
(let* ((gfx (gpu-gfx gpu))
(ww (+ (logand (- w 1) #x3FF) 1)) (hh (+ (logand (- h 1) #x1FF) 1))
(mask-or (if (= (gs mask-bit force) 1) #x8000 0)) (check (gs mask-bit test)))
(dotimes (j hh)
(dotimes (i ww)
(let ((p (vram-ref gpu (+ sx i) (+ sy j))))
(unless (and (= check 1) (logbitp 15 (vram-ref gpu (+ dx i) (+ dy j))))
(vram-set gpu (+ dx i) (+ dy j) (logior p mask-or))))))
(gpu-written gpu dx dy ww hh)
nil))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.