NV4097 textures: the 16 fragment and 4 vertex texture units, decoded into the abstract state.
(defun rsx-fixed) source(defun rsx-fixed (value bits frac signed)
"a BITS-wide fixed-point VALUE with FRAC fraction bits as a float"
(declare (type u32 value) (type u8 bits frac) (type u1 signed))
(let ((v (ldb (byte bits 0) value)))
(/ (float (if (and (= signed 1) (logbitp (- bits 1) v)) (- v (ash 1 bits)) v) 1.0)
(float (ash 1 frac) 1.0))))(defun rsx-anisotropy) source(defun rsx-anisotropy (code)
"CONTROL0 bits 4..6 as a sample count"
(declare (type u8 code))
(case code (0 1.0) (1 2.0) (2 4.0) (3 6.0) (4 8.0) (5 10.0) (6 12.0) (otherwise 16.0)))(defun rsx-remap-low) source(defun rsx-remap-low (format remap)
"the remap's low byte (the sources) as the format takes it"
(declare (type u32 format remap))
(let ((lo (ldb (byte 8 0) remap)) (over (/= (ldb (byte 16 16) remap) 0)))
(case format
((#x9A #x9B #x9C) #xE4)
(#x9F (if over #x56 #x66))
((#x94 #x95 #x98 #x99)
(case lo (#xE4 (if over #x56 #x66)) (#x4E (if over #xA9 #x99)) (#xEE #xAA) (#x44 #x55) (otherwise lo)))
(otherwise lo))))(defun rsx-remap-swizzle) source(defun rsx-remap-swizzle (lo hi k)
"the swizzle (canonical component-swizzle) of output K (0 A, 1 R, 2 G, 3 B) of a remap"
(declare (type u8 lo hi k))
(case (ldb (byte 2 (* 2 k)) hi)
(0 (gfx-code component-swizzle zero))
(1 (gfx-code component-swizzle one))
(otherwise (case (ldb (byte 2 (* 2 k)) lo)
(0 (gfx-code component-swizzle a)) (1 (gfx-code component-swizzle r))
(2 (gfx-code component-swizzle g)) (otherwise (gfx-code component-swizzle b))))))(defun rsx-argb-to-rgba) source(defun rsx-argb-to-rgba (m)
"a 4-bit mask over A R G B (A in bit 0) as one over R G B A (R in bit 0)"
(declare (type u8 m))
(logior (ldb (byte 3 1) m) (ash (ldb (byte 1 0) m) 3)))(defun rsx-remap-mask) source(defun rsx-remap-mask (lo hi m)
"MASK (over source channels A R G B) carried through the remap to the outputs, over A R G B"
(declare (type u8 lo hi m))
(let ((out 0))
(declare (type u8 out))
(dotimes (k 4)
(when (and (= (ldb (byte 2 (* 2 k)) hi) 2) (logbitp (ldb (byte 2 (* 2 k)) lo) m))
(setq out (logior out (ash 1 k)))))
out))(defun rsx-mirror-compare) source(defun rsx-mirror-compare (zfunc)
"the canonical compare-op of a texture compare function (texel OP reference), mirrored"
(declare (type u8 zfunc))
(case zfunc (1 (gfx-code compare-op greater)) (3 (gfx-code compare-op greater-or-equal))
(4 (gfx-code compare-op less)) (6 (gfx-code compare-op less-or-equal)) (otherwise zfunc)))(defun rsx-min-filter) source(defun rsx-min-filter (rsx i code)
"sampler I's min filter and mipmap mode from FILTER bits 16..18"
(declare (type rsx rsx) (type u8 i code))
(let ((gfx (rsx-gfx rsx)))
(setf (gs sampler i min-filter) (case code ((2 4 6) (gfx-code filter linear))
(7 (gfx-code filter convolution))
(otherwise (gfx-code filter nearest)))
(gs sampler i mipmap-mode) (case code ((3 4) (gfx-code mipmap-mode nearest))
((5 6) (gfx-code mipmap-mode linear))
(otherwise (gfx-code mipmap-mode none))))
nil))(defun rsx-texture-levels) source(defun rsx-texture-levels (f width height levels)
"LEVELS clamped to what WIDTH x HEIGHT has (a DXT texture stops at a 4-texel side)"
(declare (type u8 f) (type u16 width height levels))
(let* ((dxt (<= #x86 f #x88))
(side (if dxt (max 1 (floor (min width height) 4)) (max 1 width height))))
(max 1 (min levels (integer-length side)))))(defun rsx-derive-texture) source(defun rsx-derive-texture (rsx i)
"fragment texture unit I (0..15)"
(declare (type rsx rsx) (type u16 i))
(rsx-derive-texture-unit rsx i (reg NV4097_SET_TEXTURE_OFFSET i) (reg NV4097_SET_TEXTURE_FORMAT i)
(reg NV4097_SET_TEXTURE_ADDRESS i) (reg NV4097_SET_TEXTURE_CONTROL0 i)
(reg NV4097_SET_TEXTURE_CONTROL1 i) (reg NV4097_SET_TEXTURE_FILTER i)
(reg NV4097_SET_TEXTURE_IMAGE_RECT i) (reg NV4097_SET_TEXTURE_BORDER_COLOR i)
(reg NV4097_SET_TEXTURE_CONTROL3 i)))(defun rsx-derive-vertex-texture) source(defun rsx-derive-vertex-texture (rsx i)
"vertex texture unit I (0..3), abstract unit 16 + I: no remap, no gamma, no signed channels"
(declare (type rsx rsx) (type u16 i))
(rsx-derive-texture-unit rsx (+ 16 i) (reg NV4097_SET_VERTEX_TEXTURE_OFFSET i)
(reg NV4097_SET_VERTEX_TEXTURE_FORMAT i)
(logand (reg NV4097_SET_VERTEX_TEXTURE_ADDRESS i) #xF000FFFF)
(reg NV4097_SET_VERTEX_TEXTURE_CONTROL0 i) #xAAE4
(logand (reg NV4097_SET_VERTEX_TEXTURE_FILTER i) #x0FFFFFFF)
(reg NV4097_SET_VERTEX_TEXTURE_IMAGE_RECT i)
(reg NV4097_SET_VERTEX_TEXTURE_BORDER_COLOR i)
(reg NV4097_SET_VERTEX_TEXTURE_CONTROL3 i)))(defmacro define-rsx-format-block-side) source(defmacro define-rsx-format-block-side ()
`(defun rsx-format-block-side (code)
"the texels on a side of a block of format CODE: 4 for DXT, else 1"
(declare (type u32 code))
(case code
(,(loop for x in *texfmts* when (and (>= (tf-code x) #x10000) (tf-codec x)) collect (tf-code x)) 4)
(otherwise 1))))(defun rsx-level-extent) source(defun rsx-level-extent (size level)
(declare (type u32 size) (type u8 level))
(max 1 (ash size (- level))))(defun rsx-level-blocks) source(defun rsx-level-blocks (code size level)
"blocks along a side SIZE texels long at LEVEL"
(declare (type u32 code size) (type u8 level))
(let ((side (rsx-format-block-side code)))
(floor (+ (rsx-level-extent size level) side -1) side)))(defun rsx-texture-row-bytes) source(defun rsx-texture-row-bytes (code layout width pitch level)
"the bytes from one block row to the next at LEVEL"
(declare (type u32 code width pitch) (type u8 layout level))
(if (and (= layout (gfx-code texture-layout linear)) (/= pitch 0))
pitch
(* (rsx-level-blocks code width level) (rsx-format-bytes code))))(defun rsx-texture-level-bytes) source(defun rsx-texture-level-bytes (code layout width height depth pitch border level)
"the guest bytes of LEVEL of one face"
(declare (type u32 code width height depth pitch) (type u8 layout border level))
(let ((b (if (= layout (gfx-code texture-layout linear)) border 0)))
(* (rsx-texture-row-bytes code layout (+ width (* 2 b)) pitch level)
(+ (rsx-level-blocks code height level) (* 2 b))
(rsx-level-extent depth level))))(defun rsx-texture-face-bytes) source(defun rsx-texture-face-bytes (code layout width height depth pitch border levels)
"the guest bytes from one cube face to the next"
(declare (type u32 code width height depth pitch) (type u8 layout border levels))
(let ((n 0))
(declare (type u32 n))
(dotimes (l levels)
(setq n (+ n (rsx-texture-level-bytes code layout width height depth pitch border l))))
(if (or (= layout (gfx-code texture-layout morton)) (and (= pitch 0) (= (rsx-format-block-side code) 4)))
(logand (+ n 127) (lognot 127))
n)))(defun rsx-texture-level-offset) source(defun rsx-texture-level-offset (code layout width height depth pitch border level)
"where LEVEL starts in a face"
(declare (type u32 code width height depth pitch) (type u8 layout border level))
(let ((n 0))
(declare (type u32 n))
(dotimes (l level n)
(setq n (+ n (rsx-texture-level-bytes code layout width height depth pitch border l))))))(defun rsx-texel-offset) source(defun rsx-texel-offset (code layout width height depth pitch border level x y z)
"where block (X, Y) of slice Z lies in LEVEL (from the level's start)"
(declare (type u32 code width height depth pitch x y z) (type u8 layout border level))
(let ((bytes (rsx-format-bytes code)))
(if (= layout (gfx-code texture-layout linear))
(let ((row (rsx-texture-row-bytes code layout (+ width (* 2 border)) pitch level)))
(+ (* (+ (* z (+ (rsx-level-blocks code height level) (* 2 border))) y border) row)
(* (+ x border) bytes)))
(let ((lw (integer-length (- (rsx-level-blocks code width level) 1)))
(lh (integer-length (- (rsx-level-blocks code height level) 1))))
(* bytes (texfmt-morton-3d x y z lw lh (integer-length (- (rsx-level-extent depth level) 1))))))))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.