rsx

171 definitions in 12 files, 0 with an encoding diagram. src/isa/rsx

Files

FileDefinitionsOtherWhat
package.lisp00the ISA.RSX package: the RSX (the PS3's GPU), described as an ISA.
defrsx.lisp011the RSX as an ISA: DEFMETHOD-RSX, the method table, RSX enumerations.
format.lisp08the RSX's texture, vertex attribute and surface formats, as core texel formats.
state.lisp021the RSX machine: registers, abstract state, memory windows, the FIFO's own state.
vp.lisp4529the RSX vertex program: encodings, decoders, semantics on the shading machine, the interpreter.
fp.lisp6729the RSX fragment program: encodings, decoder, semantics on the shading machine, the interpreter.
nv406e.lisp64NV406E (the channel's DMA class): reference, semaphores; and the GCM driver methods.
nv4097.lisp5011NV4097 (NV40 Curie, the RSX's 3D class): methods as effects on the abstract state.
texture.lisp019NV4097 textures: the 16 fragment and 4 vertex texture units, decoded into the abstract state.
xfer.lisp30the transfer classes: NV0039 (memory to memory), NV3062/NV309E (2D surfaces),
dispatch.lisp02RSX-METHOD: one method write, generated from the method table.
fifo.lisp03the command processor: GET chases PUT through the command buffer.

README

src/isa/rsx: the RSX, described as an ISA

The RSX (the PS3's GPU, an NV47) is translated the way the CPUs are: as a match problem (docs/isel.md). Its instructions are methods: the command processor (the FIFO) writes 32-bit values to method addresses. Each method's semantics is written as effects on the abstract graphics state of the core (src/core/gfx.lisp), in the same spelling as the Vulkan commands of src/isa/vk. ctil can then match an RSX state change against the cheapest Vulkan commands with the same effect (vk-state-change-plan, src/isa/vk/cost.lisp).

Package ISA.RSX (uses CL and CORE).

FileRole
package.lispthe package and its exports
defrsx.lispdefmethod-rsx and friends, the method table, RSX enumerants to canonical codes, RSX formats to VkFormat, guest-memory fields to host fields
state.lispthe machine: method registers, the abstract state, memory windows and the IO map, FIFO and draw state, transform program and constants, display buffers and flips, rsx-reset, the event sink rsx-event
nv406e.lispNV406E (the channel): reference, semaphores; the GCM driver's flip methods
nv4097.lispNV4097 (3D): surfaces, viewport, scissor, depth/stencil, alpha test, blending, raster, vertex and index arrays, draws, clears, programs, semaphores
vp.lisp, fp.lispthe vertex and fragment programs as ISAs: encodings, decoders, semantics on the shading machine (src/core/shade.lisp), interpreters, assemblers (docs/shade.md)
xfer.lispNV0039 (memory to memory), NV3062/NV309E (2D surfaces), NV308A (image from CPU), NV3089 (scaled image): scaffold
dispatch.lisprsx-method, generated from the table: store the register, run the body
fifo.lisprsx-command, rsx-run (the command processor); rsx-assemble (command buffers at compile time)

The method form

(defmethod-rsx NV4097_SET_DEPTH_FUNC #x0A6C (value)
  (setf (gs depth compare) (rsx->gfx compare-op value)))

(defmethod-rsx NV4097_SET_VERTEX_DATA_ARRAY_FORMAT (#x1740 16 4) (value i)
  (let ((type (ldb (byte 4 0) value)) (size (ldb (byte 4 4) value)))
    (setf (gs vertex-attribute i enable) (b2i (/= size 0))
          (gs vertex-attribute i format) (rsx-vertex-vk-format (+ (* 16 type) size))
          (gs vertex-binding i stride) (ldb (byte 8 8) value) ...)))

(define-rsx-registers (NV4097_SET_SURFACE_PITCH_Z #x022C) ...)   ; stored, read with REG

(defrsx-derive rsx-derive-cull ()                                ; shared by several methods
  (setf (gs raster cull-mode)
        (if (= (reg NV4097_SET_CULL_FACE_ENABLE) 0)
            (gfx-code cull-mode none)
            (rsx->gfx cull-mode (reg NV4097_SET_CULL_FACE)))))
(defmethods-rsx ((NV4097_SET_CULL_FACE_ENABLE #x183C) (NV4097_SET_CULL_FACE #x1830)) (value)
  (rsx-derive-cull rsx))

The machine (state.lisp)

(make-rsx) holds the registers (16384 words), gfx, the memory windows the HLE sets, the command processor's status and call return, the draw being collected, the transform program (544 instructions) and constants (468), flip and transfer counters.

LocationDMA objectEffective address
local (CELL_GCM_LOCATION_LOCAL 0)#xFEED0000local-base (#xC0000000) + offset
main (CELL_GCM_LOCATION_MAIN 1)#xFEED0001through the IO map (rsx-io)
semaphores#x66606660, #x66616661label-base + offset
reports#x66626660label-base + #x1400 + offset

IO map: io-map holds the effective 1 MiB page of each of the 512 IO pages (#xFFFF unmapped), filled by rsx-map-io / rsx-unmap-io. Until the first page is mapped (io-mapped 0) IO offset 0 is at io-base and the space is linear, which is what the hand-built tests use.

Display: display-offset/pitch/width/height (8 buffers, offsets in local memory) and display-count are set by cellGcmSetDisplayBuffer. A flip method (GCM_FLIP_COMMAND, GCM_FLIP_HEAD) calls rsx-flip: the buffer becomes flip-buffer, flips counts it, flip-status becomes 0 (CELL_GCM_DISPLAY_FLIP_STATUS_DONE), label 1 (16 bytes at label-base + #x10, what cellGcmSetWaitFlip waits on) is cleared, and flip-handlers counts a flip handler call the HLE owes the PPU.

Events: after each gfx-clear (one per surface of a CLEAR_SURFACE), each gfx-draw and each flip, the RSX calls (rsx-event rsx kind) with +rsx-event-clear+, +rsx-event-draw+ or +rsx-event-flip+, while the state is that of the event. The definition here does nothing. The presenter (src/present) defines it again: the last definition loaded is the one ECL calls and ctil lowers. Builds without the presenter (the HLE and loader tests, the disc test) keep the empty one.

Guest-memory objects in the abstract state are addresses (attachment address/pitch, depth-attachment, vertex-buffer address, index address); a run-time cache turns them into host objects. rsx-host-fields maps a changed memory field to the host fields it resolves to (vertex-buffer address to buffer and offset), which is what vk-state-change-plan takes.

The command processor (fifo.lisp)

(rsx-run rsx limit) runs commands from GET to PUT (IO offsets in the CellGcmControl registers at control: PUT +0, GET +4, REF +8), writes GET back after each, and leaves the reason it stopped in rsx-status:

StatusWhen
+rsx-idle+GET reached PUT, or a command's arguments are not all before PUT yet
+rsx-spin+a jump to itself (libgcm's idle loop)
+rsx-blocked+NV406E_SEMAPHORE_ACQUIRE does not hold; the command runs again next time
+rsx-limit+LIMIT commands ran
+rsx-error+an invalid header, a call within a call, a return without a call

Headers: increment and non-increment (bit 30) method writes with an 11-bit count (count 0 is a NOP), the old (001 + 29 bits) and new (..01) jumps, call (..10, one level, as the RSX) and return (0x00020000). The method field includes the subchannel bits, and the classes sit one per subchannel, so the address alone names the method.

Draws: SET_BEGIN_END(prim) sets the topology; each DRAW_ARRAYS / DRAW_INDEX_ARRAY value is a range of up to 256; contiguous ranges are merged (a long strip is sent in pieces) and become one gfx-draw at a gap or at SET_BEGIN_END(0). Indexed draws take SET_VERTEX_DATA_BASE_INDEX as the vertex offset. CLEAR_SURFACE makes one gfx-clear-masked per active colour surface and one depth/stencil gfx-clear.

rsx-assemble builds command buffers at compile time:

(rsx-assemble #x1000
  '((NV4097_SET_DEPTH_FUNC #x203 1 1)               ; incrementing: FUNC, MASK, TEST
    (:ni NV4097_DRAW_ARRAYS #xFF000000 #x2B000100)  ; non-incrementing
    (:call sub) done (:jump done)
    sub (NV4097_SET_BEGIN_END 0) (:return)))

RSX to Vulkan

tests/isa/rsx/plan.lisp runs a frame through the FIFO, takes the fields that change between draws, and plans them for a Vulkan 1.3 pipeline with every core dynamic state, and for one with extended dynamic state 3 as well:

draw 2 changes ((raster cull-mode) (raster front-face) (depth compare))
  1.3:  (:COST 3 :COMMANDS ("vkCmdSetCullMode" "vkCmdSetFrontFace" "vkCmdSetDepthCompareOp") :PIPELINE NIL)
draw 3 changes ((blend enable) (blend src-color) (blend dst-color) (blend src-alpha) (blend dst-alpha) (blend write-mask))
  1.3:  (:COST 25 :COMMANDS NIL :PIPELINE :SWITCH)
  eds3: (:COST 3 :COMMANDS ("vkCmdSetColorBlendEnableEXT" "vkCmdSetColorBlendEquationEXT" "vkCmdSetColorWriteMaskEXT") :PIPELINE NIL)
draw 5 changes ((primitive topology) (vertex-buffer address) (index address))
  1.3:  (:COST 5 :COMMANDS ("vkCmdSetPrimitiveTopology" "vkCmdBindVertexBuffers2" "vkCmdBindIndexBuffer2") :PIPELINE NIL)
draw 6 changes ((alpha-test enable) (alpha-test compare) (alpha-test reference))
  1.3:  (:COST 25 :COMMANDS NIL :PIPELINE :SWITCH)

The alpha test has no Vulkan state; it becomes part of the fragment shader, so a change is a pipeline switch.

The abstract state: what the RSX added (src/core/gfx.lisp)

How the HLE drives it (src/sys/ps3/lv2/gcmsys.lisp)

The HLE owns one machine, *gcm-rsx* (SYS.PS3.LV2 uses ISA.RSX).

Who runs the FIFO: gcm-service

The game moves PUT with a plain store (cellGcmFlush is inline), so the RSX runs when someone calls (gcm-service) (exported by SYS.PS3.LV2): it runs the FIFO from GET towards PUT (up to #x100000 commands) and returns rsx-status; it costs a few loads when GET = PUT. The GCM functions that need the RSX call it (SetFlip, GetFlipStatus, the callback, the report reads). The PPU scheduler must call it whenever a thread yields, sleeps (sys_timer_usleep, sys_ppu_thread_yield, lwmutex and event waits) and when every thread waits: games wait for the RSX by polling REF or a label between sleeps (GTA does: see below).

(gcm-vblank) is a vertical blank: the platform's timer should call it 60 times a second (the scheduler, until there is a timer). It counts cellGcmGetVBlankCount and owes the vblank handler a call. (gcm-next-handler) returns the OPD of a handler the RSX owes the PPU (0 for none), flip handlers first, with its argument in *gcm-handler-arg* (the head, 1); the scheduler runs it as the interrupt thread would: handler(arg) on a stack of its own, to completion, before the threads continue.

*gcm-trace* 1 prints each call that configures the RSX.

GTA San Andreas (BLES02219)

With XSPE_CONTINUE=2 and gcm-service after every 200000 instructions: _cellGcmInitBody(ctx, cmdSize #x100000, ioSize #x400000, ioAddress #x30200000), cellGcmMapMainMemory(#x30700000, 1 MiB) -> IO #x400000, cellGcmBindZcull(0, #x300000, 1280, 768), display buffers 0 and 1 at local #x6C0000 and #xA44000 (1280 x 720, pitch 5120), another 1 MiB at IO #x500000, cellGcmSetFlipMode(1) (hsync), then cellGcmSetFlip(ctx, 0): the RSX runs 59 commands (a call into the second mapped region and back, then colour mask, blend, depth test less, smooth shading, back-face culling, DMA objects, the surface: A8R8G8B8 + Z24S8 at local #x6C0000 / #x300000, pitch 5120) and the first flip, to buffer 0. Then GTA sets its vblank and flip handlers, calls a dozen functions of libraries the HLE does not have yet, and loops on sys_timer_usleep (syscall 141, the scheduler's, not in this branch) with PUT = GET: no clear or draw is sent before that loop ends.

Coverage and TODO

Methods with semantics: 93 of 210 named NV4097 methods (815 addresses), plus NV406E, GCM flips, NV0039, NV308A; the rest are named registers.

Tests

TestWhat
rsx_fifoa hand-built command buffer in steps of PUT: state, draws and clears, a call, labels, a blocking acquire, a flip, a copy, an error
rsx_planthe method table, format tables against the Vulkan registry, per-method and per-draw Vulkan plans
lower_rsxthe FIFO and methods through ctil --emit and cc, against golden output
hle_gcmcellGcmSys: init, the IO map, labels, the command buffer and its wrap, flips, flip status, handlers, tiles
present_trace, present_gpua frame through the presenter (src/present/README.md)

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