the RSX fragment program: encodings, decoder, semantics on the shading machine, the interpreter.
(deffp nop #x00 "No operation")
(deffp mov #x01 "Move" (fp-write (fmap (a) a (fp-src 0))))
(deffp mul #x02 "Multiply" (fp-write (fmap (a b) (fmul :f32 :rne a b) (fp-src 0) (fp-src 1))))
(deffp add #x03 "Add" (fp-write (fmap (a b) (fadd :f32 :rne a b) (fp-src 0) (fp-src 1))))
(deffp mad #x04 "Multiply and add, rounded twice"
(fp-write (fmap (a b c) (fadd :f32 :rne (fmul :f32 :rne a b) c) (fp-src 0) (fp-src 1) (fp-src 2))))
(deffp dp3 #x05 "Three-component dot product" (fp-write (vec4-splat (vec4-dot 3 (fp-src 0) (fp-src 1)))))
(deffp dp4 #x06 "Four-component dot product" (fp-write (vec4-splat (vec4-dot 4 (fp-src 0) (fp-src 1)))))
(deffp dst #x07 "Distance vector: (1, a.y * b.y, a.z, b.w)"
(let ((a (fp-src 0)) (b (fp-src 1)))
(fp-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)))))
(deffp fmin #x08 "Minimum: b if b < a, else a" (fp-write (fmap (a b) (if (= (flt :f32 b a) 1) b a) (fp-src 0) (fp-src 1))))
(deffp fmax #x09 "Maximum: b if a < b, else a" (fp-write (fmap (a b) (if (= (flt :f32 a b) 1) b a) (fp-src 0) (fp-src 1))))
(deffp slt #x0A "1.0 if less" (fp-write (fmap (a b) (if (= (flt :f32 a b) 1) (fconst :f32 1.0) 0) (fp-src 0) (fp-src 1))))
(deffp sge #x0B "1.0 if greater or equal" (fp-write (fmap (a b) (if (= (fle :f32 b a) 1) (fconst :f32 1.0) 0) (fp-src 0) (fp-src 1))))
(deffp sle #x0C "1.0 if less or equal" (fp-write (fmap (a b) (if (= (fle :f32 a b) 1) (fconst :f32 1.0) 0) (fp-src 0) (fp-src 1))))
(deffp sgt #x0D "1.0 if greater" (fp-write (fmap (a b) (if (= (flt :f32 b a) 1) (fconst :f32 1.0) 0) (fp-src 0) (fp-src 1))))
(deffp sne #x0E "1.0 if not equal" (fp-write (fmap (a b) (if (= (feq :f32 a b) 1) 0 (fconst :f32 1.0)) (fp-src 0) (fp-src 1))))
(deffp seq #x0F "1.0 if equal" (fp-write (fmap (a b) (if (= (feq :f32 a b) 1) (fconst :f32 1.0) 0) (fp-src 0) (fp-src 1))))
(deffp frc #x10 "Fraction: x - floor(x)"
(fp-write (fmap (a) (fsub :f32 :rne a (fround-int :f32 :rdn a)) (fp-src 0))))
(deffp flr #x11 "Floor" (fp-write (fmap (a) (fround-int :f32 :rdn a) (fp-src 0))))
(deffp kil #x12 "Discard the fragment when the condition holds in any lane"
(when (fp-cond-any cpu insn) (setf (sh-killed cpu) 1 (sh-done cpu) 1)))
(deffp pk4 #x13 "Pack four signed bytes (x lowest) into every component"
(fp-write (vsplat 32 (shade-pack-snorm8 (fp-src 0)))))
(deffp up4 #x14 "Unpack four signed bytes from x" (fp-write (shade-unpack-snorm8 (vec4-ref (fp-src 0) 0))))
(deffp ddx #x15 "Derivative along x" (fp-write (shade-derivative cpu (fp-src 0) 0)))
(deffp ddy #x16 "Derivative along y" (fp-write (shade-derivative cpu (fp-src 0) 1)))
(deffp tex #x17 "Texture sample"
(fp-write (fp-sample cpu insn (fp-src 0) (gfx-code shade-lod implicit) 0 0 0 0)))
(deffp txp #x18 "Texture sample, coordinates divided by w"
(fp-write (fp-sample cpu insn (fp-src 0) (gfx-code shade-lod implicit) 0 0 0 +shade-sample-project+)))
(deffp txd #x19 "Texture sample with gradients (sources 1 and 2)"
(fp-write (fp-sample cpu insn (fp-src 0) (gfx-code shade-lod gradient) 0 (fp-src 1) (fp-src 2) 0)))
(deffp rcp #x1A "Reciprocal of x" (fp-write (fmap (a) (fdiv :f32 :rne (fconst :f32 1.0) a) (fp-srcx 0))))
(deffp rsq #x1B "Reciprocal square root of |x|" (fp-write (fmap (a) (fdiv :f32 :rne (fconst :f32 1.0) (fsqrt :f32 :rne (fabs :f32 a))) (fp-srcx 0))))
(deffp ex2 #x1C "2^x" (fp-write (fmap (a) (fexp2 :f32 a) (fp-srcx 0))))
(deffp lg2 #x1D "log2(x)" (fp-write (fmap (a) (flog2 :f32 a) (fp-srcx 0))))
(deffp lit #x1E "Lighting: (1, max(x,0), x > 0 ? max(y,0)^w : 0, 1), w clamped to +-127.9961"
(let* ((s (fp-src 0)) (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))))
(fp-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)))))
(deffp lrp #x1F "Linear interpolation: c + a * (b - c), as a * b + (1 - a) * c"
(fp-write (fmap (a b c) (fadd :f32 :rne (fmul :f32 :rne a b) (fmul :f32 :rne (fsub :f32 :rne (fconst :f32 1.0) a) c))
(fp-src 0) (fp-src 1) (fp-src 2))))
(deffp str #x20 "1.0 (set on true)" (fp-write (vec4-splat (fconst :f32 1.0))))
(deffp sfl #x21 "0.0 (set on false)" (fp-write (vec4-splat 0)))
(deffp fcos #x22 "Cosine of x" (fp-write (fmap (a) (fcos :f32 a) (fp-srcx 0))))
(deffp fsin #x23 "Sine of x" (fp-write (fmap (a) (fsin :f32 a) (fp-srcx 0))))
(deffp pk2 #x24 "Pack x and y as halves into every component"
(fp-write (vsplat 32 (shade-pack-half (fp-src 0)))))
(deffp up2 #x25 "Unpack two halves from x: x y x y" (fp-write (shade-unpack-half (vec4-ref (fp-src 0) 0))))
(deffp pow #x26 "x^y of the x components, as the RSX computes it: 2^(y * log2 x)" (fp-write (fmap (a b) (fexp2 :f32 (fmul :f32 :rne b (flog2 :f32 a))) (fp-srcx 0) (fp-srcx 1))))
(deffp pkb #x27 "Pack four unsigned bytes" (fp-write (vsplat 32 (shade-pack-unorm8 (fp-src 0)))))
(deffp upb #x28 "Unpack four unsigned bytes" (fp-write (shade-unpack-unorm8 (vec4-ref (fp-src 0) 0))))
(deffp pk16 #x29 "Pack x and y as unsigned 16-bit" (fp-write (vsplat 32 (shade-pack-unorm16 (fp-src 0)))))
(deffp up16 #x2A "Unpack two unsigned 16-bit from x: x y x y"
(fp-write (shade-unpack-unorm16 (vec4-ref (fp-src 0) 0))))
(deffp bem #x2B "Bump environment: a.xyxy + b.xxxx * c.xzxz + b.yyyy * c.ywyw"
(let ((a (fp-src 0)) (b (fp-src 1)) (c (fp-src 2)))
(fp-write (fmap (a b0 c0 b1 c1) (fadd :f32 :rne (fadd :f32 :rne a (fmul :f32 :rne b0 c0)) (fmul :f32 :rne b1 c1))
(vec4-swizzle a #b01000100) (vec4-swizzle b 0) (vec4-swizzle c #b10001000)
(vec4-swizzle b #b01010101) (vec4-swizzle c #b11011101)))))
(deffp pkg #x2C "Pack four unsigned bytes, colour channels to sRGB"
(let ((a (fp-src 0)))
(fp-write (vsplat 32 (shade-pack-unorm8 (vec4-bits (shade-srgb-encode (vec4-ref a 0))
(shade-srgb-encode (vec4-ref a 1))
(shade-srgb-encode (vec4-ref a 2)) (vec4-ref a 3)))))))
(deffp upg #x2D "Unpack four unsigned bytes, colour channels from sRGB"
(let ((u (shade-unpack-unorm8 (vec4-ref (fp-src 0) 0))))
(fp-write (vec4-bits (shade-srgb-decode (vec4-ref u 0)) (shade-srgb-decode (vec4-ref u 1))
(shade-srgb-decode (vec4-ref u 2)) (vec4-ref u 3)))))
(deffp dp2a #x2E "a.x * b.x + a.y * b.y + c.z"
(fp-write (vec4-splat (fadd :f32 :rne (vec4-dot 2 (fp-src 0) (fp-src 1)) (vec4-ref (fp-src 2) 2)))))
(deffp txl #x2F "Texture sample at an explicit level (source 1's x, unverified)"
(fp-write (fp-sample cpu insn (fp-src 0) (gfx-code shade-lod explicit) (vec4-ref (fp-src 1) 0) 0 0 0)))
(deffp txb #x31 "Texture sample with a level bias (source 1's x, unverified)"
(fp-write (fp-sample cpu insn (fp-src 0) (gfx-code shade-lod bias) (vec4-ref (fp-src 1) 0) 0 0 0)))
(deffp texbem #x33 "Bump-mapped texture sample (not done)")
(deffp txpbem #x34 "Bump-mapped projective texture sample (not done)")
(deffp bemlum #x35 "Bump-mapped luminance (not done)")
(deffp refl #x36 "Reflection: b - 2 * a * dot(a, b) / dot(a, a) (a: the normal), over x y z"
(let* ((a (fp-src 0)) (b (fp-src 1))
(k (fdiv :f32 :rne (fmul :f32 :rne (fconst :f32 2.0) (vec4-dot 3 a b)) (vec4-dot 3 a a))))
(fp-write (fmap (n i) (fsub :f32 :rne i (fmul :f32 :rne k n)) a b))))
(deffp timeswtex #x37 "(not done)")
(deffp dp2 #x38 "Two-component dot product" (fp-write (vec4-splat (vec4-dot 2 (fp-src 0) (fp-src 1)))))
(deffp nrm #x39 "Normalise x y z (a zero vector is kept); w := z"
(let* ((a (fp-src 0)) (l (fsqrt :f32 :rne (vec4-dot 3 a a))))
(fp-write (if (= (flt :f32 0 l) 1)
(vec4-bits (fdiv :f32 :rne (vec4-ref a 0) l) (fdiv :f32 :rne (vec4-ref a 1) l)
(fdiv :f32 :rne (vec4-ref a 2) l) (fdiv :f32 :rne (vec4-ref a 2) l))
(vec4-swizzle a #b10100100)))))
(deffp div #x3A "a / b.x" (fp-write (fmap (a b) (fdiv :f32 :rne a b) (fp-src 0) (fp-srcx 1))))
(deffp divsq #x3B "a / sqrt(|b.x|); a zero numerator stays itself"
(fp-write (fmap (a b) (if (= (flt :f32 0 (fabs :f32 a)) 1) (fdiv :f32 :rne a (fsqrt :f32 :rne (fabs :f32 b))) a)
(fp-src 0) (fp-srcx 1))))
(deffp lif #x3C "Lighting, final part: (1, y, x > 0... ) as LIT (unverified)"
(let* ((s (fp-src 0)) (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))))
(fp-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)))))
(deffp fenct #x3D "Fence (no effect here)")
(deffp fencb #x3E "Fence (no effect here)")
(deffp brk #x40 "Leave the innermost loop when the condition holds"
(when (and (fp-cond-any cpu insn) (> (sh-loops cpu) 0))
(let ((l (- (sh-loops cpu) 1)))
(setf (sh-next cpu) (aref (sh-loop-end cpu) l) (sh-loops cpu) l))))
(deffp cal #x41 "Call (not done: the Cg compiler does not use it)")
(deffp ife #x42 "If: the next slots up to else run when the condition holds, else those from else to end"
:ife
(let ((else-slot (ash else -2)) (end-slot (ash endo -2)))
(if (fp-cond-any cpu insn)
(when (and (< else-slot end-slot) (< (sh-ifs cpu) 8))
(setf (aref (sh-if-else cpu) (sh-ifs cpu)) else-slot (aref (sh-if-end cpu) (sh-ifs cpu)) end-slot
(sh-ifs cpu) (+ (sh-ifs cpu) 1)))
(setf (sh-next cpu) else-slot))))
(deffp loop #x43 "Loop COUNT times up to end; the counter starts at START and steps by STEP"
:loop
(fp-enter-loop cpu insn count start step (ash endo -2)))
(deffp rep #x44 "Repeat COUNT times up to end" :loop
(fp-enter-loop cpu insn count start step (ash endo -2)))
(deffp ret #x45 "End the program when the condition holds" (when (fp-cond-any cpu insn) (setf (sh-done cpu) 1)))
(defisa rsx-fp) source(defisa rsx-fp :width 128 :endian :big :state shade :doc "The RSX fragment program")(defmacro deffp) source(defmacro deffp (name opcode doc &body body)
(let ((form (if (keywordp (car body)) (pop body) :alu)))
`(definsn rsx-fp ,name ,(fp-encoding opcode form) (:doc ,doc) ,@body)))(defun fp-fetch-word) source(defun fp-fetch-word (address)
"the program word at ADDRESS: big-endian, 16-bit halves swapped back"
(declare (type u32 address))
(let ((w (mem-read 32 address)))
(logior (ash (ldb (byte 16 0) w) 16) (ldb (byte 16 16) w))))(defun fp-fetch) source(defun fp-fetch (address slot)
"the 128-bit slot SLOT of the program at ADDRESS"
(declare (type u32 address slot))
(let ((a (wrap 32 (+ address (* 16 slot)))))
(logior (ash (fp-fetch-word a) 96) (ash (fp-fetch-word (wrap 32 (+ a 4))) 64)
(ash (fp-fetch-word (wrap 32 (+ a 8))) 32) (fp-fetch-word (wrap 32 (+ a 12))))))(defun fp-opcode) source(defun fp-opcode (insn)
"the 7-bit opcode"
(declare (type (unsigned-byte 128) insn))
(logior (ldb (byte 6 120) insn) (ash (ldb (byte 1 63) insn) 6)))(defun fp-flow-p) source(defun fp-flow-p (op)
"BRK CAL FENCT FENCB IFE LOOP REP RET: no sources, never a constant after"
(declare (type u8 op))
(or (= op #x3D) (= op #x3E) (and (>= op #x40) (<= op #x45))))(defun fp-has-constant) source(defun fp-has-constant (insn)
"1 when the slot after INSN is a constant: a source of type 2 (not a flow operation)"
(declare (type (unsigned-byte 128) insn))
(if (or (fp-flow-p (fp-opcode insn))
(and (/= (ldb (byte 2 64) insn) 2) (/= (ldb (byte 2 32) insn) 2) (/= (ldb (byte 2 0) insn) 2)))
0
1))(defun fp-length) source(defun fp-length (address limit)
"the slots of the program at ADDRESS, constants included, up to LIMIT"
(declare (type u32 address limit))
(let ((slot 0))
(declare (type u32 slot))
(loop
(when (>= slot limit) (return limit))
(let ((insn (fp-fetch address slot)))
(setq slot (+ slot 1 (fp-has-constant insn)))
(when (= (ldb (byte 1 96) insn) 1) (return slot))))))(defun fp-half) source(defun fp-half (x)
"an f32 (bits) as the RSX stores it in a half: magnitudes from 65536 are
infinities (NaN kept), the significand truncated to 10 bits, values below
2^-14 truncated to multiples of 2^-24 (unverified: RPCS3's model)"
(declare (type u32 x))
(cond ((>= (ldb (byte 31 0) x) #x7F800000) x)
((>= (ldb (byte 31 0) x) #x47800000) (logior (logand x #x80000000) #x7F800000))
((< (ldb (byte 31 0) x) #x38800000) ; below 2^-14: in units of 2^-24, truncated
(logior (logand x #x80000000)
(fdiv :f32 :rne (fround-int :f32 :rtz (fmul :f32 :rne (fabs :f32 x) (fconst :f32 16777216.0)))
(fconst :f32 16777216.0))))
(t (logand x #xFFFFE000))))(defun fp-f16-32) source(defun fp-f16-32 (x)
"an f32 result under f16 precision kept in an f32 register: denormals 0, magnitudes from 65536 infinities"
(declare (type u32 x))
(cond ((>= (ldb (byte 31 0) x) #x7F800000) x)
((= (ldb (byte 8 23) x) 0) (logand x #x80000000))
((>= (ldb (byte 31 0) x) #x47800000) (logior (logand x #x80000000) #x7F800000))
(t x)))(defun fp-clamp) source(defun fp-clamp (x lo hi)
"precision_clamp: NaNs, -0 and denormals become +0, then clamped to [LO, HI]"
(declare (type u32 x lo hi))
(if (or (fnan-p :f32 x) (fzero-p :f32 x) (fsubnormal-p :f32 x))
(fclamp 0 lo hi)
(fclamp x lo hi)))(defun fp-precision) source(defun fp-precision (v prec)
"V clamped as precision PREC asks: 1 f16, 2 fixed12 [-2, 2], 3 fixed9 [-1, 1], 4 saturate [0, 1]"
(declare (type vec4 v) (type u8 prec))
(case prec
(1 (vmap 32 (lambda (x) (fp-half x)) v))
(2 (fmap (x) (fp-clamp x (fconst :f32 -2.0) (fconst :f32 2.0)) v))
(3 (fmap (x) (fp-clamp x (fconst :f32 -1.0) (fconst :f32 1.0)) v))
(4 (fmap (x) (fp-clamp x 0 (fconst :f32 1.0)) v))
(otherwise v)))(defun fp-read-h) source(defun fp-read-h (sh h)
"half register H as a vec4 of f32"
(declare (type shade sh) (type u8 h))
(let* ((r (shade-temp sh (ash h -1))) (k (* 2 (logand h 1)))
(a (lane 32 r k)) (b (lane 32 r (+ k 1))))
(vec4-bits (fcvt :f32 :f16 :rne (ldb (byte 16 0) a)) (fcvt :f32 :f16 :rne (ldb (byte 16 16) a))
(fcvt :f32 :f16 :rne (ldb (byte 16 0) b)) (fcvt :f32 :f16 :rne (ldb (byte 16 16) b)))))(defun fp-write-h) source(defun fp-write-h (sh h v mask)
"half register H := V (f32 values, stored as halves) in the components of MASK"
(declare (type shade sh) (type u8 h) (type vec4 v) (type (unsigned-byte 4) mask))
(let* ((n (ash h -1)) (r (shade-temp sh n)) (k (* 2 (logand h 1)))
(a (lane 32 r k)) (b (lane 32 r (+ k 1))))
(when (logbitp 0 mask) (setq a (dpb (fcvt :f16 :f32 :rne (vec4-ref v 0)) (byte 16 0) a)))
(when (logbitp 1 mask) (setq a (dpb (fcvt :f16 :f32 :rne (vec4-ref v 1)) (byte 16 16) a)))
(when (logbitp 2 mask) (setq b (dpb (fcvt :f16 :f32 :rne (vec4-ref v 2)) (byte 16 0) b)))
(when (logbitp 3 mask) (setq b (dpb (fcvt :f16 :f32 :rne (vec4-ref v 3)) (byte 16 16) b)))
(shade-set-temp sh n (set-lane 32 (set-lane 32 r k a) (+ k 1) b))
nil))(defun fp-input) source(defun fp-input (sh insn index)
"input INDEX as the source reads it: COL0/COL1 clamped, persp multiplies by 1/w (WPOS.w)"
(declare (type shade sh) (type (unsigned-byte 128) insn) (type u32 index))
(let ((v (if (< index 15) (shade-in sh index) 0)))
(cond ((or (= index 1) (= index 2)) (fmap (x) (fsat x) v))
((and (>= index 4) (<= index 13) (= (ldb (byte 1 31) insn) 1))
(let ((w (vec4-ref (shade-in sh 0) 3))) (fmap (x) (fmul :f32 :rne x w) v)))
(t v))))(defun fp-operand) source(defun fp-operand (sh insn k)
"source K (0..2) of INSN: the register, swizzled, its precision modifier,
absolute, negated"
(declare (type shade sh) (type (unsigned-byte 128) insn) (type (unsigned-byte 2) k))
(let* ((at (case k (0 64) (1 32) (otherwise 0)))
(operand (ldb (byte 18 at) insn))
(type (ldb (byte 2 0) operand)) (index (ldb (byte 6 2) operand)) (fp16 (ldb (byte 1 8) operand))
(prec (ldb (byte 3 (case k (0 51) (1 54) (otherwise 57))) insn))
(abs (ldb (byte 1 (case k (0 93) (1 50) (otherwise 18))) insn))
(raw (case type
(0 (if (= fp16 1) (fp-read-h sh index) (shade-temp sh index)))
(1 (let ((i (if (= (ldb (byte 1 30) insn) 1)
(+ 4 (ldb (byte 11 19) insn)
(if (> (sh-loops sh) 0) (aref (sh-loop-index sh) (- (sh-loops sh) 1)) 0))
(ldb (byte 4 109) insn))))
(when (or (< i 4) (> i 13)) (setq prec 0))
(fp-input sh insn i)))
(2 (shade-const sh (logand (+ (sh-pc sh) 1) 511)))
(otherwise 0)))
(prec (if (and (= type 0) (= fp16 1) (= prec 1)) 0 prec))
(v (vec4-swizzle raw (ldb (byte 8 9) operand)))
(v (if (= prec 4) (fp-precision v 4) v))
(v (if (= abs 1) (fmap (x) (fabs :f32 x) v) v))
(v (if (and (/= prec 0) (/= prec 4)) (fp-precision v prec) v)))
(if (= (ldb (byte 1 17) operand) 1) (fmap (x) (fneg :f32 x) v) v)))(defmacro fp-src) source(defmacro fp-src (k) `(fp-operand cpu insn ,k))(defmacro fp-srcx) source(defmacro fp-srcx (k) `(vec4-swizzle (fp-operand cpu insn ,k) 0))(defun fp-cond-lanes) source(defun fp-cond-lanes (sh insn)
"the lanes where the condition holds (all for lt eq gt, none for none)"
(declare (type shade sh) (type (unsigned-byte 128) insn))
(shade-cond-mask (shade-cc sh (ldb (byte 1 95) insn)) (ldb (byte 8 85) insn)
(ldb (byte 1 82) insn) (ldb (byte 1 83) insn) (ldb (byte 1 84) insn)))(defun fp-cond-any) source(defun fp-cond-any (sh insn)
"a flow operation's condition: any lane"
(declare (type shade sh) (type (unsigned-byte 128) insn))
(/= (fp-cond-lanes sh insn) 0))(defun fp-exempt-p) source(defun fp-exempt-p (op)
"operations whose results the destination precision does not clamp (RPCS3's hardware notes)"
(declare (type u8 op))
(or (= op #x39) (= op #x09) (= op #x08) (= op #x22) (= op #x23) (= op #x36) (= op #x10)
(= op #x1E) (= op #x3C) (= op #x1D)))(defun fp-write-result) source(defun fp-write-result (sh insn v)
"the result V: scaled, to the destination register (R or H, masked, under
the condition) with its precision, and to a condition register (setcc)"
(declare (type shade sh) (type (unsigned-byte 128) insn) (type vec4 v))
(let ((cond (fp-cond-lanes sh insn)))
(when (= (ldb (byte 3 82) insn) 0)
(return-from fp-write-result nil))
(let* ((scale (ldb (byte 3 60) insn))
(v (case scale
(1 (fmap (x) (fmul :f32 :rne x (fconst :f32 2.0)) v))
(2 (fmap (x) (fmul :f32 :rne x (fconst :f32 4.0)) v))
(3 (fmap (x) (fmul :f32 :rne x (fconst :f32 8.0)) v))
(5 (fmap (x) (fdiv :f32 :rne x (fconst :f32 2.0)) v))
(6 (fmap (x) (fdiv :f32 :rne x (fconst :f32 4.0)) v))
(7 (fmap (x) (fdiv :f32 :rne x (fconst :f32 8.0)) v))
(otherwise v)))
(mask (logand cond (ldb (byte 4 105) insn)))
(ccm (ldb (byte 1 94) insn)) (setcc (ldb (byte 1 104) insn)))
(if (= (ldb (byte 1 126) insn) 1)
(when (= setcc 1)
(shade-set-cc sh ccm (vec4-merge (shade-cc sh ccm) v mask)))
(let* ((dh (ldb (byte 1 103) insn)) (prec (ldb (byte 2 118) insn)) (op (fp-opcode insn))
(v (if (= dh 1) (fp-precision v 1) v))
(v (cond ((= (ldb (byte 1 127) insn) 1) (fp-precision v 4))
((or (= prec 0) (fp-exempt-p op)) v)
((= prec 1) (if (= dh 1) v (vmap 32 (lambda (x) (fp-f16-32 x)) v)))
(t (fp-precision v prec))))
(dreg (ldb (byte 6 97) insn)))
(if (= dh 1)
(fp-write-h sh dreg v mask)
(shade-set-temp sh dreg (vec4-merge (shade-temp sh dreg) v mask)))
(when (= setcc 1)
(shade-set-cc sh ccm (vec4-merge (shade-cc sh ccm) v mask)))))
nil)))(defmacro fp-write) source(defmacro fp-write (v) `(fp-write-result cpu insn ,v))(defun fp-sample) source(defun fp-sample (sh insn coords lod-kind lod dx dy flags)
"texture unit tex sampled; bx2 expands the result: x * 2 - 1"
(declare (type shade sh) (type (unsigned-byte 128) insn) (type vec4 coords dx dy) (type u8 lod-kind flags)
(type u32 lod))
(let ((v (shade-sample sh (ldb (byte 4 113) insn) (gfx-code shade-dim dim-2d) coords lod-kind lod dx dy flags)))
(if (= (ldb (byte 1 117) insn) 1)
(fmap (x) (fsub :f32 :rne (fmul :f32 :rne x (fconst :f32 2.0)) (fconst :f32 1.0)) v)
v)))(defun fp-enter-loop) source(defun fp-enter-loop (sh insn count start step end)
(declare (type shade sh) (type (unsigned-byte 128) insn) (type u8 count start step) (type u32 end))
(if (or (not (fp-cond-any sh insn)) (= count 0) (>= (sh-loops sh) 4))
(setf (sh-next sh) end)
(let ((l (sh-loops sh)))
(setf (aref (sh-loop-count sh) l) count (aref (sh-loop-index sh) l) start
(aref (sh-loop-step sh) l) (wrap 32 (sext 8 step))
(aref (sh-loop-start sh) l) (sh-next sh) (aref (sh-loop-end sh) l) end
(sh-loops sh) (+ l 1))))
nil)(define-decoder fp-decode) source(define-decoder fp-decode rsx-fp)(define-executor fp-execute) source(define-executor fp-execute rsx-fp)(defun fp-structure) source(defun fp-structure (sh)
"at SH's pc: leave a taken if's code at its else (to its end), close a
finished if, run a loop's next iteration at its end"
(declare (type shade sh))
(loop
(let ((pc (sh-pc sh)) (moved 0))
(declare (type u1 moved))
(when (and (> (sh-ifs sh) 0) (= pc (aref (sh-if-else sh) (- (sh-ifs sh) 1))))
(setf (sh-pc sh) (aref (sh-if-end sh) (- (sh-ifs sh) 1)) (sh-ifs sh) (- (sh-ifs sh) 1) moved 1))
(when (and (= moved 0) (> (sh-loops sh) 0) (= pc (aref (sh-loop-end sh) (- (sh-loops sh) 1))))
(let ((l (- (sh-loops sh) 1)))
(if (> (aref (sh-loop-count sh) l) 1)
(setf (aref (sh-loop-count sh) l) (- (aref (sh-loop-count sh) l) 1)
(aref (sh-loop-index sh) l) (wrap 32 (+ (aref (sh-loop-index sh) l) (aref (sh-loop-step sh) l)))
(sh-pc sh) (aref (sh-loop-start sh) l))
(setf (sh-loops sh) l))
(setq moved 1)))
(when (= moved 0) (return))))
nil)(defun fp-run) source(defun fp-run (sh address limit)
"run the fragment program at ADDRESS on SH (inputs set, SHADE-RESET
first) until the end bit, a kill or LIMIT instructions -> the instructions run"
(declare (type shade sh) (type u32 address limit))
(setf (sh-pc sh) 0 (sh-base sh) address)
(loop
(fp-structure sh)
(when (or (= (sh-done sh) 1) (>= (sh-steps sh) limit) (>= (sh-pc sh) 4096))
(return))
(let ((insn (fp-fetch address (sh-pc sh))))
(when (= (fp-has-constant insn) 1)
(shade-set-const sh (logand (+ (sh-pc sh) 1) 511) (fp-fetch address (+ (sh-pc sh) 1))))
(setf (sh-next sh) (+ (sh-pc sh) 1 (fp-has-constant insn)))
(fp-execute sh insn)
(setf (sh-steps sh) (+ (sh-steps sh) 1))
(when (= (ldb (byte 1 96) 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.