rsx: vp.lisp

the RSX vertex program: encodings, decoders, semantics on the shading machine, the interpreter.

vec source

(defvp vec vnop #x00 "No operation")

vec source

(defvp vec vmov #x01 "Move" (vp-vec-write (fmap (a) a (vp-src 0))))

vec source

(defvp vec vmul #x02 "Multiply" (vp-vec-write (fmap (a b) (fmul :f32 :rne a b) (vp-src 0) (vp-src 1))))

vec source

(defvp vec vadd #x03 "Add (sources 0 and 2)" (vp-vec-write (fmap (a c) (fadd :f32 :rne a c) (vp-src 0) (vp-src 2))))

vec source

(defvp vec vmad #x04 "Multiply and add, rounded twice"
  (vp-vec-write (fmap (a b c) (fadd :f32 :rne (fmul :f32 :rne a b) c) (vp-src 0) (vp-src 1) (vp-src 2))))

vec source

(defvp vec vdp3 #x05 "Three-component dot product, in every component"
  (vp-vec-write (vec4-splat (vec4-dot 3 (vp-src 0) (vp-src 1)))))

vec source

(defvp vec vdph #x06 "Homogeneous dot product: a.xyz . b.xyz + b.w"
  (let ((b (vp-src 1)))
    (vp-vec-write (vec4-splat (fadd :f32 :rne (vec4-dot 3 (vp-src 0) b) (vec4-ref b 3))))))

vec source

(defvp vec vdp4 #x07 "Four-component dot product"
  (vp-vec-write (vec4-splat (vec4-dot 4 (vp-src 0) (vp-src 1)))))

vec source

(defvp vec vdst #x08 "Distance vector: (1, a.y * b.y, a.z, b.w)"
  (let ((a (vp-src 0)) (b (vp-src 1)))
    (vp-vec-write (vec4-bits (fconst :f32 1.0) (fmul :f32 :rne (vec4-ref a 1) (vec4-ref b 1)) (vec4-ref a 2) (vec4-ref b 3)))))

vec source

(defvp vec vmin #x09 "Minimum: b if b < a, else a" (vp-vec-write (fmap (a b) (if (= (flt :f32 b a) 1) b a) (vp-src 0) (vp-src 1))))

vec source

(defvp vec vmax #x0A "Maximum: b if a < b, else a" (vp-vec-write (fmap (a b) (if (= (flt :f32 a b) 1) b a) (vp-src 0) (vp-src 1))))

vec source

(defvp vec vslt #x0B "1.0 if less, else 0.0"
  (vp-vec-write (fmap (a b) (if (= (flt :f32 a b) 1) (fconst :f32 1.0) 0) (vp-src 0) (vp-src 1))))

vec source

(defvp vec vsge #x0C "1.0 if greater or equal"
  (vp-vec-write (fmap (a b) (if (= (fle :f32 b a) 1) (fconst :f32 1.0) 0) (vp-src 0) (vp-src 1))))

vec source

(defvp vec varl #x0D "Address register (D0 dtmp bit 0) := floor(source 0) as integers"
  (let ((a (vp-src 0)) (r (ldb (byte 1 0) dtmp)) (mask (logior vmx (ash vmy 1) (ash vmz 2) (ash vmw 3))))
    (shade-set-addr cpu r (vec4-merge (shade-addr cpu r)
                                      (fmap (x) (wrap 32 (fcvt-to-int :f32 :rdn x -4611686018427387904 4611686018427387904)) a)
                                      (vp-write-mask cpu insn mask)))))

vec source

(defvp vec vfrc #x0E "Fraction: x - floor(x)"
  (vp-vec-write (fmap (a) (fsub :f32 :rne a (fround-int :f32 :rdn a)) (vp-src 0))))

vec source

(defvp vec vflr #x0F "Floor" (vp-vec-write (fmap (a) (fround-int :f32 :rdn a) (vp-src 0))))

vec source

(defvp vec vseq #x10 "1.0 if equal"
  (vp-vec-write (fmap (a b) (if (= (feq :f32 a b) 1) (fconst :f32 1.0) 0) (vp-src 0) (vp-src 1))))

vec source

(defvp vec vsfl #x11 "0.0 (set on false)" (vp-vec-write (vec4-splat 0)))

vec source

(defvp vec vsgt #x12 "1.0 if greater"
  (vp-vec-write (fmap (a b) (if (= (flt :f32 b a) 1) (fconst :f32 1.0) 0) (vp-src 0) (vp-src 1))))

vec source

(defvp vec vsle #x13 "1.0 if less or equal"
  (vp-vec-write (fmap (a b) (if (= (fle :f32 a b) 1) (fconst :f32 1.0) 0) (vp-src 0) (vp-src 1))))

vec source

(defvp vec vsne #x14 "1.0 if not equal"
  (vp-vec-write (fmap (a b) (if (= (feq :f32 a b) 1) 0 (fconst :f32 1.0)) (vp-src 0) (vp-src 1))))

vec source

(defvp vec vstr #x15 "1.0 (set on true)" (vp-vec-write (vec4-splat (fconst :f32 1.0))))

vec source

(defvp vec vssg #x16 "Sign: 1.0, -1.0, or the zero itself"
  (vp-vec-write (fmap (a) (if (= (flt :f32 0 a) 1) (fconst :f32 1.0) (if (= (flt :f32 a 0) 1) (fconst :f32 -1.0) a))
                      (vp-src 0))))

vec source

(defvp vec vtxl #x19 "Vertex texture fetch at an explicit level (source 0; level in w, unverified)"
  (let ((c (vp-src 0)))
    (vp-vec-write (shade-sample cpu (+ 16 (ldb (byte 2 2) src1)) (gfx-code shade-dim dim-2d) c
                                (gfx-code shade-lod explicit) (vec4-ref c 3) 0 0 0))))

sca source

(defvp sca snop #x00 "No operation")

sca source

(defvp sca smov #x01 "Move" (vp-sca-write (fmap (c) c (vp-srcx 2))))

sca source

(defvp sca srcp #x02 "Reciprocal" (vp-sca-write (fmap (c) (fdiv :f32 :rne (fconst :f32 1.0) c) (vp-srcx 2))))

sca source

(defvp sca srcc #x03 "Reciprocal, magnitude clamped to [5.42101e-20, 1.884467e19]"
  (vp-sca-write (fmap (c) (let* ((r (fdiv :f32 :rne (fconst :f32 1.0) c)) (m (fabs :f32 r)))
                            (cond ((= (flt :f32 (fconst :f32 1.884467e19) m) 1)
                                   (if (= (flt :f32 r 0) 1) (fconst :f32 -1.884467e19) (fconst :f32 1.884467e19)))
                                  ((= (flt :f32 m (fconst :f32 5.42101e-20)) 1)
                                   (if (= (flt :f32 r 0) 1) (fconst :f32 -5.42101e-20) (fconst :f32 5.42101e-20)))
                                  (t r)))
                      (vp-srcx 2))))

sca source

(defvp sca srsq #x04 "Reciprocal square root of |x|"
  (vp-sca-write (fmap (c) (fdiv :f32 :rne (fconst :f32 1.0) (fsqrt :f32 :rne (fabs :f32 c))) (vp-srcx 2))))

sca source

(defvp sca sexp #x05 "Partial exponential: (2^floor(x), x - floor(x), 2^x, 1), NV_vertex_program's EXP (unverified)"
  (let* ((x (vec4-ref (vp-src 2) 0)) (f (fround-int :f32 :rdn x)))
    (vp-sca-write (vec4-bits (fexp2 :f32 f) (fsub :f32 :rne x f) (fexp2 :f32 x) (fconst :f32 1.0)))))

sca source

(defvp sca slog #x06 "Partial logarithm: (floor(log2 |x|), |x| / 2^that, log2 |x|, 1) (unverified)"
  (let* ((x (fabs :f32 (vec4-ref (vp-src 2) 0))) (l (flog2 :f32 x)) (e (fround-int :f32 :rdn l)))
    (vp-sca-write (vec4-bits e (fdiv :f32 :rne x (fexp2 :f32 e)) l (fconst :f32 1.0)))))

sca source

(defvp sca slit #x07 "Lighting: (1, max(x,0), x > 0 ? max(y,0)^w : 0, 1), w clamped to +-127.9961"
  (let* ((s (vp-src 2)) (x (vec4-ref s 0)) (y (fmax2 (vec4-ref s 1) 0))
         (w (fclamp (vec4-ref s 3) (fconst :f32 -127.9961) (fconst :f32 127.9961))))
    (vp-sca-write (vec4-bits (fconst :f32 1.0) (fmax2 x 0) (if (= (flt :f32 0 x) 1) (fpow :f32 y w) 0)
                        (fconst :f32 1.0)))))

sca source

(defvp sca sbra #x08 "Branch to the address register's x when the condition holds"
  (when (vp-dynamic-p cpu insn)
    (vp-jump cpu (lane 32 (shade-addr cpu ars) 0))))

sca source

(defvp sca sbri #x09 "Branch when the condition holds"
  (when (vp-dynamic-p cpu insn) (vp-jump cpu (vp-target insn))))

sca source

(defvp sca scal #x0A "Call when the condition holds"
  (when (vp-dynamic-p cpu insn) (vp-call cpu (vp-target insn))))

sca source

(defvp sca scli #x0B "Call by condition (as CAL)"
  (when (vp-dynamic-p cpu insn) (vp-call cpu (vp-target insn))))

sca source

(defvp sca sret #x0C "Return when the condition holds (from the program: end)"
  (when (vp-dynamic-p cpu insn) (vp-return cpu)))

sca source

(defvp sca slg2 #x0D "Logarithm base 2" (vp-sca-write (fmap (c) (flog2 :f32 c) (vp-srcx 2))))

sca source

(defvp sca sex2 #x0E "Exponential base 2" (vp-sca-write (fmap (c) (fexp2 :f32 c) (vp-srcx 2))))

sca source

(defvp sca ssin #x0F "Sine" (vp-sca-write (fmap (c) (fsin :f32 c) (vp-srcx 2))))

sca source

(defvp sca scos #x10 "Cosine" (vp-sca-write (fmap (c) (fcos :f32 c) (vp-srcx 2))))

sca source

(defvp sca sbrb #x11 "Branch on a transform branch bit"
  (when (vp-static-p cpu insn) (vp-jump cpu (vp-target insn))))

sca source

(defvp sca sclb #x12 "Call on a transform branch bit"
  (when (vp-static-p cpu insn) (vp-call cpu (vp-target insn))))

sca source

(defvp sca spsh #x13 "Push (not done)")

sca source

(defvp sca spop #x14 "Pop (not done)")

Other definitions

(defisa rsx-vpv) source
(defisa rsx-vpv :width 128 :endian :big :state shade
  :doc "The RSX vertex program, vector slot")
(defisa rsx-vps) source
(defisa rsx-vps :width 128 :endian :big :state shade
  :doc "The RSX vertex program, scalar slot")
(defmacro defvp) source
(defmacro defvp (slot name opcode doc &body body)
  (ecase slot
    (vec `(definsn rsx-vpv ,name ,(vp-encoding opcode nil) (:doc ,doc) ,@body))
    (sca `(definsn rsx-vps ,name ,(vp-encoding nil opcode) (:doc ,doc) ,@body))))
(defun vp-swizzle-sel) source
(defun vp-swizzle-sel (operand)
  "the core swizzle selector (component k from bits 2k) of a VP operand (x in bits 14..15)"
  (declare (type (unsigned-byte 17) operand))
  (logior (ldb (byte 2 14) operand) (ash (ldb (byte 2 12) operand) 2)
          (ash (ldb (byte 2 10) operand) 4) (ash (ldb (byte 2 8) operand) 6)))
(defun vp-const-index) source
(defun vp-const-index (sh insn)
  "the constant a source reads: csrc, or csrc + the address register's
   component (icst), modulo 512; 512 for an out of range direct read (0)"
  (declare (type shade sh) (type (unsigned-byte 128) insn))
  (let ((csrc (ldb (byte 10 76) insn)))
    (if (= (ldb (byte 1 1) insn) 1)
        (let ((a (lane 32 (shade-addr sh (ldb (byte 1 120) insn)) (ldb (byte 2 96) insn))))
          (ldb (byte 9 0) (+ csrc a)))
        (if (< csrc 468) csrc 512))))
(defun vp-operand) source
(defun vp-operand (sh insn operand abs)
  "the value of a source OPERAND (17 bits) of INSN, swizzled, made absolute (ABS) and negated"
  (declare (type shade sh) (type (unsigned-byte 128) insn) (type (unsigned-byte 17) operand) (type u1 abs))
  (let* ((type (ldb (byte 2 0) operand))
         (raw (case type
                (1 (shade-temp sh (ldb (byte 6 2) operand)))
                (2 (shade-in sh (ldb (byte 4 72) insn)))
                (3 (let ((c (vp-const-index sh insn))) (if (= c 512) 0 (shade-const sh c))))
                (otherwise 0)))
         (v (vec4-swizzle raw (vp-swizzle-sel operand)))
         (v (if (= abs 1) (fmap (x) (fabs :f32 x) v) v)))
    (if (= (ldb (byte 1 16) operand) 1) (fmap (x) (fneg :f32 x) v) v)))
(defmacro vp-src) source
(defmacro vp-src (k)
  (ecase k
    (0 '(vp-operand cpu insn src0 abs0))
    (1 '(vp-operand cpu insn src1 abs1))
    (2 '(vp-operand cpu insn src2 abs2))))
(defmacro vp-srcx) source
(defmacro vp-srcx (k) `(vec4-swizzle (vp-src ,k) 0))
(defun vp-cond-sel) source
(defun vp-cond-sel (insn)
  (declare (type (unsigned-byte 128) insn))
  (logior (ldb (byte 2 104) insn) (ash (ldb (byte 2 102) insn) 2)
          (ash (ldb (byte 2 100) insn) 4) (ash (ldb (byte 2 98) insn) 6)))
(defun vp-cond-lanes) source
(defun vp-cond-lanes (sh insn)
  "the lanes the condition (cond, its swizzle, register ccs) holds in"
  (declare (type shade sh) (type (unsigned-byte 128) insn))
  (let ((cond (ldb (byte 3 106) insn)))
    (shade-cond-mask (shade-cc sh (ldb (byte 1 121) insn)) (vp-cond-sel insn)
                     (ldb (byte 1 0) cond) (ldb (byte 1 1) cond) (ldb (byte 1 2) cond))))
(defun vp-write-mask) source
(defun vp-write-mask (sh insn mask)
  "MASK (bit k = component k) where a conditional write's condition holds"
  (declare (type shade sh) (type (unsigned-byte 128) insn) (type (unsigned-byte 4) mask))
  (if (= (ldb (byte 1 109) insn) 1) (logand mask (vp-cond-lanes sh insn)) mask))
(defun vp-sat) source
(defun vp-sat (insn v)
  (declare (type (unsigned-byte 128) insn) (type vec4 v))
  (if (= (ldb (byte 1 122) insn) 1) (fmap (x) (fsat x) v) v))
(defun vp-write-vec) source
(defun vp-write-vec (sh insn v)
  "the vector result V: to temporary dtmp and/or output dst (vres), else to
   the condition register; write mask x y z w = D3 bits 16..13"
  (declare (type shade sh) (type (unsigned-byte 128) insn) (type vec4 v))
  (let* ((v (vp-sat insn v))
         (mask (vp-write-mask sh insn (logior (ldb (byte 1 16) insn) (ash (ldb (byte 1 15) insn) 1)
                                              (ash (ldb (byte 1 14) insn) 2) (ash (ldb (byte 1 13) insn) 3))))
         (dtmp (ldb (byte 6 111) insn)) (dst (ldb (byte 5 2) insn)) (vres (ldb (byte 1 126) insn)))
    (unless (= dtmp 63)
      (shade-set-temp sh dtmp (vec4-merge (shade-temp sh dtmp) v mask)))
    (when (and (= vres 1) (< dst 16))
      (shade-set-out sh dst (vec4-merge (shade-out sh dst) v mask)))
    (when (and (= dtmp 63) (= vres 0))
      (let ((c (ldb (byte 1 121) insn)))
        (shade-set-cc sh c (vec4-merge (shade-cc sh c) v mask))))
    nil))
(defun vp-write-sca) source
(defun vp-write-sca (sh insn v)
  "the scalar result V: to temporary stmp, else to output dst (unless the
   vector slot has it), else to the condition register; mask = D3 bits 20..17"
  (declare (type shade sh) (type (unsigned-byte 128) insn) (type vec4 v))
  (let* ((v (vp-sat insn v))
         (mask (vp-write-mask sh insn (logior (ldb (byte 1 20) insn) (ash (ldb (byte 1 19) insn) 1)
                                              (ash (ldb (byte 1 18) insn) 2) (ash (ldb (byte 1 17) insn) 3))))
         (stmp (ldb (byte 6 7) insn)) (dst (ldb (byte 5 2) insn)) (vres (ldb (byte 1 126) insn)))
    (cond ((/= stmp 63) (shade-set-temp sh stmp (vec4-merge (shade-temp sh stmp) v mask)))
          ((= vres 0) (when (< dst 16) (shade-set-out sh dst (vec4-merge (shade-out sh dst) v mask))))
          (t (let ((c (ldb (byte 1 121) insn)))
               (shade-set-cc sh c (vec4-merge (shade-cc sh c) v mask)))))
    nil))
(defmacro vp-vec-write) source
(defmacro vp-vec-write (v) `(vp-write-vec cpu insn ,v))
(defmacro vp-sca-write) source
(defmacro vp-sca-write (v) `(vp-write-sca cpu insn ,v))
(defun vp-target) source
(defun vp-target (insn)
  "the slot a BRI/CAL/BRB/CLB names"
  (declare (type (unsigned-byte 128) insn))
  (let ((src2 (ldb (byte 17 21) insn)))
    (logior (ash (ldb (byte 1 119) insn) 9) (ash (ldb (byte 6 11) src2) 3) (ldb (byte 3 8) src2))))
(defun vp-dynamic-p) source
(defun vp-dynamic-p (sh insn)
  "a branch's condition: always (lt eq gt), never (none), else any lane"
  (declare (type shade sh) (type (unsigned-byte 128) insn))
  (let ((cond (ldb (byte 3 106) insn)))
    (cond ((= cond 7) t) ((= cond 0) nil) (t (/= (vp-cond-lanes sh insn) 0)))))
(defun vp-static-p) source
(defun vp-static-p (sh insn)
  "BRB/CLB: transform branch bit (src2 bits 2..6) equals src2 bit 7"
  (declare (type shade sh) (type (unsigned-byte 128) insn))
  (let ((src2 (ldb (byte 17 21) insn)))
    (= (ldb (byte 1 (ldb (byte 5 2) src2)) (sh-branch-bits sh)) (ldb (byte 1 7) src2))))
(defun vp-jump) source
(defun vp-jump (sh target) (declare (type shade sh) (type u32 target)) (setf (sh-next sh) target) nil)
(defun vp-call) source
(defun vp-call (sh target)
  (declare (type shade sh) (type u32 target))
  (if (< (sh-sp sh) (shade-stack))
      (progn (setf (aref (sh-stack sh) (sh-sp sh)) (sh-next sh) (sh-sp sh) (+ (sh-sp sh) 1) (sh-next sh) target))
      (setf (sh-done sh) 1))
  nil)
(defun vp-return) source
(defun vp-return (sh)
  (declare (type shade sh))
  (if (= (sh-sp sh) 0)
      (setf (sh-done sh) 1)
      (setf (sh-sp sh) (- (sh-sp sh) 1) (sh-next sh) (aref (sh-stack sh) (sh-sp sh))))
  nil)
(define-decoder vp-decode-vec) source
(define-decoder vp-decode-vec rsx-vpv)
(define-decoder vp-decode-sca) source
(define-decoder vp-decode-sca rsx-vps)
(define-executor vp-execute-vec) source
(define-executor vp-execute-vec rsx-vpv)
(define-executor vp-execute-sca) source
(define-executor vp-execute-sca rsx-vps)
(defun vp-word) source
(defun vp-word (program slot)
  "the 128-bit instruction in SLOT of PROGRAM (544 x 4 words, D0 first)"
  (declare (type (simple-array (unsigned-byte 32) (2176)) program) (type u32 slot))
  (let ((b (* 4 (min slot 543))))
    (logior (ash (aref program b) 96) (ash (aref program (+ b 1)) 64)
            (ash (aref program (+ b 2)) 32) (aref program (+ b 3)))))
(defun vp-step) source
(defun vp-step (sh insn)
  "one instruction: the vector operation, then the scalar one; a condition
   test that can never hold (cct with cond 0) makes both no-ops"
  (declare (type shade sh) (type (unsigned-byte 128) insn))
  (unless (and (= (ldb (byte 1 109) insn) 1) (= (ldb (byte 3 106) insn) 0))
    (vp-execute-vec sh insn)
    (vp-execute-sca sh insn))
  nil)
(defun vp-run) source
(defun vp-run (sh program start limit)
  "run the vertex program of PROGRAM from slot START on SH (inputs, constants
   and branch bits set; SHADE-RESET first) until the end bit, a return from
   the program, or LIMIT instructions -> the instructions run"
  (declare (type shade sh) (type (simple-array (unsigned-byte 32) (2176)) program) (type u32 start limit))
  (setf (sh-pc sh) start)
  (loop
    (when (or (= (sh-done sh) 1) (>= (sh-steps sh) limit) (>= (sh-pc sh) 544))
      (return))
    (let ((insn (vp-word program (sh-pc sh))))
      (setf (sh-next sh) (+ (sh-pc sh) 1))
      (vp-step sh insn)
      (setf (sh-steps sh) (+ (sh-steps sh) 1))
      (when (= (ldb (byte 1 0) insn) 1)
        (setf (sh-done sh) 1))
      (setf (sh-pc sh) (sh-next sh))))
  (sh-steps sh))

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