From 786a7306b5be596e5e1ee4eb2a6c331d9ce3b308 Mon Sep 17 00:00:00 2001 From: Tzeentchnet <58480142+Tzeentchnet@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:32:48 -0800 Subject: [PATCH] graphics: complete GPU milestones M9-M16 Activate the production Newport resident path and explicit frontend modes. Complete IMPACT engine fidelity and snapshot lifecycle, make VPro GPU failures fail closed with full Octane snapshots, and add complete GR2 lifecycle state plus measured resident-GPU decision telemetry. Keep VPro bit-exact on Vulkan and D3D12, and retain gr2-jit after the evidence gate does not justify a resident RE3 wgpu implementation. --- CHANGELOG.md | 58 +- FEATURES.md | 64 +- HACKING.md | 164 +++-- HELP.md | 56 +- STORAGE.md | 26 +- TESTING.md | 74 ++- clanker/INDEX.md | 2 +- clanker/TODO.md | 46 +- clanker/gr2/DESIGN.md | 24 +- clanker/mgras/DESIGN.md | 36 +- clanker/plans/GPUTodo.md | 429 +++++++++---- clanker/plans/wgpu-raster.md | 32 +- clanker/wgpu/gpu-floats-are-not-the-hosts.md | 24 +- docs/graphics/gr2-xz-extreme.md | 24 +- docs/graphics/newport.md | 57 +- docs/octane-ip30.md | 6 +- iris-gui-README.md | 7 + iris-gui/Cargo.toml | 2 + iris-gui/src/framebuffer.rs | 13 + iris.toml | 3 + scripts/test-resident-gate.ps1 | 7 +- src/config.rs | 154 ++++- src/dev/gr2/debug.rs | 17 + src/dev/gr2/gl.rs | 116 +++- src/dev/gr2/gr2disp.rs | 24 + src/dev/gr2/hq2.rs | 57 +- src/dev/gr2/hq2_tests.rs | 36 ++ src/dev/gr2/mod.rs | 192 +++++- src/dev/gr2/re3.rs | 153 +++++ src/dev/gr2/re3_jit_tests.rs | 65 ++ src/dev/gr2/snapshot.rs | 499 +++++++++++++++ src/dev/mgras/debug.rs | 24 +- src/dev/mgras/disp.rs | 12 +- src/dev/mgras/ge11.rs | 14 +- src/dev/mgras/hq3.rs | 158 +++++ src/dev/mgras/hq4.rs | 264 ++++++++ src/dev/mgras/mgras_tests.rs | 153 ++++- src/dev/mgras/microcode.rs | 4 + src/dev/mgras/mod.rs | 352 +++++++++- src/dev/mgras/plain.rs | 6 + src/dev/mgras/record.rs | 5 +- src/dev/mgras/rss.rs | 170 ++++- src/dev/mgras/te1.rs | 50 ++ src/dev/mgras/wgpu/mod.rs | 186 +++++- src/dev/mgras/wgpu/tests.rs | 26 +- src/dev/ng1/rex3.rs | 351 ++++++---- src/dev/ng1/rex3_flat_fill.rs | 24 +- src/dev/ng1/rex3_frame.rs | 16 +- src/dev/ng1/rex3_lifecycle.rs | 2 +- src/dev/ng1/rex3_lifecycle_tests.rs | 43 +- src/dev/ng1/rex3_rec.rs | 2 +- src/dev/ng1/rex3_tests.rs | 12 +- src/dev/ng1/rex3_trace.rs | 4 +- src/dev/ng1/wgpu/direct.rs | 10 +- src/dev/ng1/wgpu/mod.rs | 15 +- src/dev/ng1/wgpu/resident.rs | 2 +- src/dev/odyssey/assembly.rs | 6 +- src/dev/odyssey/board.rs | 565 ++++++++++++++++- src/dev/odyssey/clip.rs | 3 +- src/dev/odyssey/copy.rs | 18 +- src/dev/odyssey/debug.rs | 4 +- src/dev/odyssey/display.rs | 9 + src/dev/odyssey/display_fifo.rs | 95 ++- src/dev/odyssey/dma.rs | 17 +- src/dev/odyssey/draw.rs | 35 +- src/dev/odyssey/gl.rs | 10 +- src/dev/odyssey/golden/replay.rs | 1 + src/dev/odyssey/gpu_unit.rs | 57 +- src/dev/odyssey/interface.rs | 98 +++ src/dev/odyssey/mmio.rs | 12 + src/dev/odyssey/mod.rs | 18 +- src/dev/odyssey/pipe/mod.rs | 19 +- src/dev/odyssey/pipe/slot.rs | 97 +++ src/dev/odyssey/raster_unit.rs | 50 +- src/dev/odyssey/reader.rs | 137 +++- src/dev/odyssey/scanout.rs | 2 +- src/dev/odyssey/snapshot.rs | 552 ++++++++++++++++ src/dev/odyssey/tests/fault.rs | 59 ++ src/dev/odyssey/tests/mod.rs | 1 + src/dev/odyssey/wgpu/mod.rs | 223 +++++-- src/dev/odyssey/wgpu/shaders/vpro.wgsl | 13 +- src/dev/odyssey/wgpu/tests.rs | 40 +- src/dev/w1/bus.rs | 157 ++++- src/disp.rs | 38 +- src/display/renderer.rs | 10 +- src/gfifo.rs | 3 - src/gfx_display.rs | 15 +- src/ip30_machine.rs | 244 ++++++- src/machine.rs | 635 ++++++++++++++++--- src/main.rs | 20 +- src/snapshot.rs | 245 ++++++- src/ui.rs | 36 +- src/wgpu/batch.rs | 28 +- src/wgpu/scanout.rs | 1 - src/workstation.rs | 27 +- 95 files changed, 7016 insertions(+), 956 deletions(-) create mode 100644 src/dev/gr2/snapshot.rs create mode 100644 src/dev/odyssey/snapshot.rs create mode 100644 src/dev/odyssey/tests/fault.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 778b421ca..fc0d4fcb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,42 @@ hashes are given where a change is easiest to understand by reading the commit. - IMPACT and VPro: a double-buffered program costs one screen update per buffer swap, not one per retrace. With atlantis the display thread's load fell from 80% to 20% of a core on IMPACT and from 39% to 9% on VPro. +- VPro's wgpu raster no longer draws unsupported framebuffer work on a stale + CPU copy. Unsupported work or a GPU/scanout failure now stops the board + with a bus error until reset instead of presenting incorrect pixels. +- VPro wgpu rendering remains bit-exact with the CPU raster on D3D12 for + interpolated depth, matching the Vulkan path. + +### Octane snapshots + +- One-processor Octanes with VPro V6, V8, V10 or V12 graphics now save and + restore the complete board state and 32 or 128 MiB VRAM. The state and VRAM + are named content-addressed parts, so unchanged chunks are shared between + snapshots. +- A stopped VPro snapshot drains and observes GPU-authoritative memory before + capture. Restore validates every payload before changing the board, rebuilds + runtime-only state, and reseeds an enabled wgpu raster from restored VRAM. +- Snapshot schema 9 adds the VPro parts; schemas 6 through 8 remain readable. + +### GR2 lifecycle and acceleration evidence + +- XZ and Extreme now save and restore complete architectural graphics state, + including HQ2/GL command continuation, RE3 context, VRAM, Z/stencil, FIN2 + waits and unread GEDMA output. Snapshot schema 10 stores exact named + `state`, `vram` and `zbuf` parts in the content-addressed store; disk + snapshots, CI rollback and live checkpoints use the same representation. +- Stopping GR2 drains accepted HQ2 and RE3 work before its workers exit. + Power-on clears the complete board state while retaining the configured + `gr2-jit` mode, and restore validates every part before changing the board. +- `gr2 metrics` reports bounded span-size, same-pipeline batching, + synchronization, candidate memory-traffic, host-readback, presentation and + JIT counters. Captured GL workloads contain many long, consecutively + batchable spans, but ordinary GR2 presentation still scans host VRAM. +- A resident RE3 wgpu path remains deferred. On a Ryzen 7 9800X3D, two + interleaved sustained runs measured warmed `gr2-jit` at 178-187 Mpx/s for + 4-pixel spans and 520-568 Mpx/s for 1280-pixel spans; there is not yet a + GPU prototype, no-readback scanout or representative guest result that + clears the acceleration gate. ### Built-in PROMs @@ -279,11 +315,23 @@ hashes are given where a change is easiest to understand by reading the commit. ### Newport: experimental live rendering on the host GPU -- Experimental, off by default: a `rex-gpu` build draws Newport's flat fills on - the host GPU when `IRIS_REX3_GPU=auto|vulkan|metal|dx12|gl` is set. -- Drawing the GPU path does not support, and everything after a GPU failure, - runs on the CPU with nothing lost. -- Newport refuses `raster = "wgpu"` instead of ignoring it. +- A `wgpu` build can select GPU-authoritative Newport raster with + `[graphics] raster = "wgpu"`. Direct mode shares one Vulkan or D3D12 device + with the window presenter and both Newport heads, and fails closed instead + of falling back to GL, CPU raster or another device. +- Explicit `newport_mode = "readback"` retains the ordinary compositor as a + slow compatibility path and attributes its full two-plane 16 MiB + observations separately. It is never an automatic fallback. +- Production startup, snapshots and restore, CI rollback, reset/restart, + screenshots, headless capture and VNC preserve GPU authority. The exact + 131-case resident gate and production lifecycle matrix pass on Vulkan and + D3D12. An installed IRIX 6.5 desktop, Showcase and Atlantis render correctly + on both direct backends; the Showcase canvas matches CPU raster exactly. + In the 30-second Atlantis sample, Vulkan and D3D12 improve live REX3 + throughput by 42.1% and 24.3% while using 5.9% and 7.7% less host CPU. + The mode remains experimental while the application/adapter matrix expands. +- The older `rex-gpu`/`IRIS_REX3_GPU` experiment remains CPU-canonical, + separate from and mutually exclusive with production resident raster. - Developers: a REX3 replay corpus (`src/dev/ng1/testdata`) checks the interpreter, Cranelift JIT and GPU engine against each other (`TESTING.md`). diff --git a/FEATURES.md b/FEATURES.md index 8d14d31bd..47cd827ca 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -44,7 +44,7 @@ Select with `cargo build --release --features `; add | `default` | Default bundle: `tlbvmap`, `rexdiag`, `lineedit`, `embedded-proms` and `http-media`. | `tlbvmap`, `rexdiag`, `lineedit`, `embedded-proms`, `http-media` | | `rex-dirty-upload` | Track conservative Newport framebuffer damage and copy/upload only dirty regions. Opt-in; the default retains the original REX3 and presentation paths. | — | | `rex-jit` | Cranelift compiler for Newport draw shaders; complements precompiled shaders. | `cranelift-codegen`, `cranelift-frontend`, `cranelift-jit`, `cranelift-module`, `cranelift-native` | -| `wgpu` | Host-GPU rasterisation through wgpu compute: the common batch layer in `src/wgpu` and the boards' GPU raster units under `src/dev//wgpu` (VPro, IMPACT), and wgpu window composition including CPU-raster Newport. A separate Newport resident raster is internal to tests only. Work in progress; see `clanker/plans/wgpu-raster.md`. | `dep:bytemuck`, `dep:pollster`, `dep:wgpu`, `dep:ash` | +| `wgpu` | Host-GPU rasterisation through the common wgpu layer (VPro, IMPACT and experimental GPU-authoritative Newport), plus wgpu window composition. Newport production activation is initially validated on Vulkan and D3D12 and fails closed without CPU fallback. Work in progress; see `clanker/plans/GPUTodo.md`. | `dep:bytemuck`, `dep:pollster`, `dep:wgpu`, `dep:ash` | | `rex-gpu` | Experimental Newport wgpu/WGSL flat-fill engine for replay and, with `IRIS_REX3_GPU`, live rendering. It uses the common wgpu device/presentation layer. The CPU renderer remains the default; this feature is not part of `gpujit` or release builds. | `gputrace`, `rex-dirty-upload`, `wgpu` | | `gr4-jit` | Cranelift compiler for IMPACT (GR4) raster pipelines (RSS/TE1 shaders). | `cranelift-codegen`, `cranelift-frontend`, `cranelift-jit`, `cranelift-module`, `cranelift-native` | | `gr2-jit` | Cranelift compiler for GR2 (XZ / Extreme) RE3 span pipelines (SHADED / FLAT shaders). | `cranelift-codegen`, `cranelift-frontend`, `cranelift-jit`, `cranelift-module`, `cranelift-native` | @@ -86,12 +86,14 @@ turn off those dependency defaults. | `hostgl` | Enable hostcall and host OpenGL on macOS (CGL) and Linux (EGL) in the embedded core. | `hostcall`, `iris/hostgl` | | `perfetto` | Perfetto recording controls in the Debug tab. | `iris/perfetto` | | `rex-dirty-upload` | Enable opt-in Newport damage tracking and partial uploads in the embedded core. | `iris/rex-dirty-upload` | +| `wgpu` | Enable experimental GPU-authoritative raster backends in the embedded core. iris-gui can host Newport only in explicit `newport_mode = "readback"`; direct presentation still requires the standalone frontend. | `iris/wgpu` | Every core feature above is also selectable for the GUI via `iris/`: ```sh cargo build -p iris-gui --release --features iris/jitv2 cargo build -p iris-gui --release --features premiere,hostgl +cargo build -p iris-gui --release --features wgpu ``` The GUI offers every machine profile and compatible graphics boards. **Advanced** @@ -326,8 +328,9 @@ one of them is set. See [the Onyx 3000 guide](docs/onyx3000-ip35.md). | `graphics.resolution` | `guest` | guest, 1024x768, 1280x960, 1280x1024; presets require Newport. | | `graphics.threads` | `2` | Worker threads the board's fragment pipeline draws on, at least 1. VPro and InfiniteReality: the screen tiles' owners, besides its front-end and raster threads; more help on a host with cores to spare. | | `graphics.tile` | `32` | VPro and InfiniteReality: the fragment pipeline's screen tile size in pixels, a power of two from 8 to 256. Each tile's work runs in order on one worker at a time; with many threads, 64 can be faster. | -| `graphics.raster` | `cpu` | VPro and IMPACT: where the raster unit runs. `cpu` draws on the threads above (IMPACT: its raster engine threads); `wgpu` draws on the host GPU through wgpu compute (needs a build with the `wgpu` feature; work in progress, see `clanker/plans/wgpu-raster.md`). Newport rejects `wgpu` until M3; its separate resident path is test-only. The older CPU-canonical prototype has a live opt-in through `IRIS_REX3_GPU` in a `rex-gpu` build. An IMPACT board then has one raster engine, whatever `IRIS_MGRAS_ENGINES` says. | -| `graphics.gpu_backend` | `auto` | The wgpu backend for `raster = "wgpu"`: auto, vulkan, metal, dx12 or gl. `WGPU_BACKEND` overrides it. | +| `graphics.raster` | `cpu` | VPro, IMPACT and Newport: where the raster unit runs. `cpu` keeps the existing board path; `wgpu` draws on the host GPU through wgpu compute (needs a build with the `wgpu` feature). Newport's experimental production mode makes its complete RGB and auxiliary planes GPU-authoritative, has no unsupported-operation CPU fallback, and is separate from the older CPU-canonical `IRIS_REX3_GPU` prototype. An IMPACT board then has one raster engine, whatever `IRIS_MGRAS_ENGINES` says. | +| `graphics.newport_mode` | `direct` when Newport uses wgpu | `direct` shares the resident GPU with the wgpu window presenter and fails startup rather than falling back to GL/CPU; `readback` explicitly selects the slow compatibility/validation compositor, synchronizing both complete planes for each newly composed frame. It applies only to Newport with `raster = "wgpu"`. | +| `graphics.gpu_backend` | `auto` | The wgpu backend for `raster = "wgpu"`: auto, vulkan, metal, dx12 or gl. `WGPU_BACKEND` overrides it. Experimental production Newport initially accepts only Vulkan or D3D12; `auto` must resolve to one of them. | @@ -826,7 +829,7 @@ mistaken for GUI settings. | `IRIS_ODSY_SHADER` | `inline` walks and shades VPro filled triangles on the raster thread; the tile workers then only store. | [src/dev/odyssey/raster_unit.rs](src/dev/odyssey/raster_unit.rs) | | `IRIS_ODSY_TRACE` | Trace VPro command words, GL commands, bus accesses and DMA descriptors from power-on to this path (`gputrace` builds; `vpro trace` from the monitor). | [src/dev/odyssey/trace.rs](src/dev/odyssey/trace.rs) | | `IRIS_QOS` | HostGL QoS: default/off disables promotion; quiet suppresses logging. | [iris-hostgl/src/qos.rs](iris-hostgl/src/qos.rs) | -| `IRIS_REX3_GPU` | `auto`, `vulkan`, `metal`, `dx12`, or `gl` enables the CPU-canonical legacy live REX3 GPU prototype in a `rex-gpu` build. Unset keeps Newport rasterization on the CPU; this does not select the test-only resident path. | [src/dev/ng1/rex3.rs](src/dev/ng1/rex3.rs) | +| `IRIS_REX3_GPU` | `auto`, `vulkan`, `metal`, `dx12`, or `gl` enables the CPU-canonical legacy live REX3 GPU prototype in a `rex-gpu` build. Unset keeps that prototype off; this does not select production `[graphics] raster = "wgpu"`, and the two modes are mutually exclusive. | [src/dev/ng1/rex3.rs](src/dev/ng1/rex3.rs) | | `IRIS_REX3_REC` | Record REX3 bus operations from board start to this path (`gputrace` builds). | [src/dev/ng1/rex3_rec.rs](src/dev/ng1/rex3_rec.rs) | | `IRIS_REX3_TRACE` | Trace REX3 GFIFO and CPU accesses from board start to this path (`gputrace` builds). | [src/dev/ng1/rex3_trace.rs](src/dev/ng1/rex3_trace.rs) | | `IRIS_REX_JIT_PROFILE` | Where the REX3 shader JIT keeps its profile of draw modes between runs, instead of `~/.iris/rex-jit-profile.bin` (`rex-jit` builds). | [src/dev/ng1/rex3_profile.rs](src/dev/ng1/rex3_profile.rs) | @@ -1125,7 +1128,45 @@ monitor inspects and controls it. cargo run --release --features rex-jit ``` -### REX3 live GPU renderer (experimental, `--features rex-gpu`) +### Newport GPU-authoritative raster (experimental, `--features wgpu`) + +With `[graphics] raster = "wgpu"`, Newport uses the production resident +backend rather than the CPU-canonical prototype below. One validated +`Arc` is shared by both Newport heads. Complete linear 2048 x 1024 RGB +and auxiliary planes are seeded once, then ordinary DRAW, SCR2SCR, HostW, +HostR ordering and scanout run through the bounded resident command stream. +Unsupported work and device loss are fatal to the machine; neither can switch +to CPU raster or claim success from stale CPU shadows. + +The default direct mode requires the visible window to use +`renderer = "wgpu"` on that same device. Vulkan and D3D12 are the initial +validated production backends. Direct-mode screenshots, CI capture and VNC +map only requested composed output; ordinary presentation maps neither raw +planes nor composed output. If the required presenter cannot be created, +startup fails and the already-started machine is stopped and flushed. + +Set `[graphics] newport_mode = "readback"` only when deliberately validating +through the ordinary GL/CPU compositor. Each newly composed frame observes +both full planes (16 MiB) and is attributed as compatibility traffic. The +frontend gives that explicit operation a two-second minimum timeout. Readback +mode is fixed at startup and is never an automatic fallback from direct mode. +An iris-gui build with its local `wgpu` feature exposes this readback path +through its CPU or optional GL capture renderer. iris-gui does not yet provide +a same-device eframe wgpu presenter, so it cannot host Newport direct mode. + +`rex status` reports the selected mode, resolved backend and adapter, +`no_cpu_fallback=true`, aggregate command/batch/GFIFO/latency telemetry, and +all attributed raw and composed transfer bytes. Snapshots, restore, CI +rollback, reset, Stop/restart and dual-head construction retain GPU authority. +The embedded PROM command monitor and an installed IRIX 6.5 Indigo Magic +desktop have booted and captured frames on Vulkan and D3D12 with zero +unsupported operations. Showcase produced pixels identical to CPU raster on +both backends, and the animated Atlantis demo improved live REX3 throughput by +42.1% on Vulkan and 24.3% on D3D12 while using 5.9% and 7.7% less host CPU, +respectively, on the acceptance host. The mode remains experimental and +opt-in while that application/adapter matrix is broadened. + +### REX3 live GPU renderer (legacy experiment, `--features rex-gpu`) `rex-gpu` adds a wgpu/WGSL compute path for Newport flat fills, used for differential replay of REX3 recordings and, when `IRIS_REX3_GPU` is set, as an @@ -1137,15 +1178,10 @@ REX3 record/replay harness, and `rex-dirty-upload`, whose damage snapshots publish completed GPU work to the existing presentation path. The prototype still makes the CPU framebuffer canonical and synchronizes -before presentation and CPU fallback. `[graphics] raster = "wgpu"` is rejected -for Newport until M3; it does not silently select this prototype. A separate -GPU-authoritative resident flat-fill and direct-scanout path exists only in -tests, with no user-facing selector. It observes selected raw planes only on -request and captures screenshots/VNC/headless frames from composed output; -ordinary frames have no raw-plane transfer, CPU-plane access or CPU fallback. -Board-level raw consumers and full opcode/lifecycle integration remain open, -and changed decoded DID metadata can still be uploaded. See -[TESTING.md](TESTING.md) for the hardware gate and remaining limits. +before presentation and CPU fallback. It is not selected by +`[graphics] raster = "wgpu"` and cannot be enabled at the same time as that +GPU-authoritative production mode. See [TESTING.md](TESTING.md) for the +separate gates and remaining limits. The GPU subset is intentionally narrow: complete `DRAW` blocks and spans with positive integer coordinates, 24-bit RGB planes, RGB color mode, `SRC` logic diff --git a/HACKING.md b/HACKING.md index c34292da4..071111a50 100644 --- a/HACKING.md +++ b/HACKING.md @@ -175,11 +175,12 @@ pub trait Resettable { fn power_on(&self); } save → load → save round-trip test. State too big for a TOML table goes to the chunk store instead: the machine calls the device's own `save_chunks` and `load_chunks` (as for the framebuffers), keeps the hash lists in `ChunksManifest` - (`kona_chunks` for the KONA pipe's pixel store and cursor memory) and lists them in - `referenced_hashes`, so `gc` keeps them. A new field in `ChunksManifest` changes - `chunks.bin`, so it bumps `SCHEMA_VERSION`. A field appended at the end can - keep the older snapshots readable: `ChunksManifest::from_bytes` decodes the - previous layout too and `OLDEST_SCHEMA` names the floor. + (`kona_chunks` for KONA, `mgras_chunks` for IMPACT and `odyssey_chunks` for + VPro) and lists them in `referenced_hashes`, so `gc` keeps them. A new field + in `ChunksManifest` changes `chunks.bin`, so it bumps `SCHEMA_VERSION`. A + field appended at the end can keep the older snapshots readable: + `ChunksManifest::from_bytes` decodes the previous layouts too and + `OLDEST_SCHEMA` names the floor. - **Resettable** is power-on state. A device that answers every width the same way writes one inherent @@ -380,6 +381,29 @@ Every device that has real work to do runs on its own thread: | `odyssey-work`, `odyssey-raster`, `odyssey-pool-N` | VPro: the front end decodes the queued command-FIFO stores (the processor's stores only queue); the raster unit sets each draw up and walks its primitives into 2x2 quads; a pool of workers (`[graphics] threads`, default 2) walks, shades and stores filled triangles and stores every other fragment, one screen tile at a time; and the display composes each whole frame from board memory (64-bit words, as main memory is: a field is a bit range of its word, read by shift and mask with no byte order, `vram.rs`) as the scan-out registers describe it, counts the retrace and presents the window at 60 Hz. No board-wide lock: the front end owns its state, an access that needs the drawing is answered busy until it is done, the display FIFO (PB&J's register file) has its own lock and never waits for the drawing (`display_fifo.rs`), and the display reads board memory while it is drawn, as the board's own scan-out does; nobody tracks which rows changed. The raster thread sets primitives up and writes each as one command in a ring of 64-byte slots (`pipe/ring.rs`): a triangle's record and bounding box, a quad it shaded itself (lines, points), or a clear's rectangle; the slot's index goes on the queue of every screen tile (`[graphics] tile`, default 32 pixels) it touches. A worker takes a ready tile and owns it until its queue is empty, walking each triangle within the tile and shading and storing quad by quad, so every pixel sees its fragments in command order with no other ordering, and the depth pre-test reads depth its own tile stored. A slot is retired once all its tiles have run it. The raster thread drains the ring before any other work on board memory (a texture upload waits for the shader alone). A completion (a fence's token, a query's words) does not drain: it rides the ring and runs once the ring retires past the quads queued before it. A quad's four pixels are lanes: the walk's depth, the texture unit's positions and the colour weights run on the host's SIMD unit (`fearless_simd`, `pipe/lanes.rs`, the level detected at start-up), bit for bit the scalar arithmetic. When its queue runs dry the raster thread does not drain: it keeps one count of the raster unit's pending jobs for the quads still in flight, and a front-end wait or a refused processor read asks it to store them. `IRIS_ODSY_SHADER=inline` makes the raster thread shade every quad itself, and `IRIS_ODSY_BACKEND=inline` stores on the raster thread with no pool. The texture registers a draw reads live in an array of draw state slots (`pipe/slot.rs`): the front end writes the current slot, a draw latches it and holds it until its job is given back, and a write while a queued draw holds the current slot first copies it to a free one, so the stages read their draw's registers with no lock | | `kona-hip`, `kona-tile-N` | InfiniteReality (KONA): the processor's stores to the pipe only queue in a ring (`src/dev/kona/mod.rs`); `kona-hip` takes them in order under the pipe lock, frames the commands and runs the back end inline, so the front end's state needs no lock of its own. A draw is set up on `kona-hip` and its job goes on the index queue of every screen tile it touches (`[graphics] tile`); `kona-tile-N` workers (`[graphics] threads`; none means `kona-hip` runs the tiles itself) each take a ready tile and own it until its queue is empty, so every pixel sees its work in command order. `kona-hip` drains the tiles before any other use of the pixel store. The display refresh thread (`Display-Refresh`, section 7) runs the board at 60 Hz and, at each retrace, completes the retrace requests made since the last one (through the display handle, not the pipe lock); it composes the whole frame from the store and the display tables when drawing finished since the last frame, a screenshot or frame is wanted, or six frames have passed | +With VPro's wgpu raster selected, GPU memory is authoritative. CPU geometry, +clipping and fragment preparation may feed ordered GPU records, but an +unsupported draw, clear, copy, DMA destination or image destination never +writes the CPU VRAM mirror as a fallback. Explicit front-end, DMA, copy, +snapshot, monitor, hash and validation observations read only the required +dirty GPU ranges and clear their dirty metadata only after a successful +readback. A rejected operation, device loss or scanout failure sets one shared +sticky fault: later processor accesses return `BUS_ERR` and the display +returns `NotReady` rather than composing stale CPU memory. Power-on reset +clears the fault and reseeds the GPU from reset VRAM. + +A VPro disk snapshot requires stopped workers and an empty command FIFO. It +uses an observation attributed to `Snapshot` to finish GPU work and bring +GPU-authoritative dirty ranges into the CPU mirror, then writes two named +schema-v9 CAS parts: `state` and the complete 32 or 128 MiB `vram`. `state` +contains the architectural front end, partial command/image/query and +primitive state, current texture-register slot, host interface, display FIFO +and shared registers, raster line phase and 1-Wire transaction. It excludes +threads, locks, callbacks, GPU objects and derived caches. Restore decodes and +validates the complete part set, board product, memory size, vector lengths and +parser bounds before mutation, rebuilds runtime links and caches, and uploads +the restored CPU VRAM to an enabled GPU unit. + Threads that have nothing to do park instead of spinning: the graphics consumers when their FIFO is empty, and the CPU thread in `idle-pause` builds when the guest is in its idle loop. A producer that adds work, or a device @@ -603,7 +627,7 @@ How each board uses it: | Board | Producer side (CPU / VDMA) | Consumers | |---|---|---| -| Newport (`src/dev/ng1/`) | REX3 register writes, GO commands, HOSTRW data | `REX3-Processor` executes REX3 drawing; the refresh thread captures VC2/XMAP/CMAP and framebuffer state, which the window's renderer composes | +| Newport (`src/dev/ng1/`) | REX3 register writes, GO commands, HOSTRW data | CPU raster uses `REX3-Processor` and the shared display refresh; GPU-authoritative raster uses `REX3-Resident-Processor` and a fixed direct-wgpu or readback-compatibility `REX3-Resident-Display` worker | | GR2 (`src/dev/gr2/`) | HQ2 command FIFO (`hq_fifo`) | `GR2-HQ2` interprets HQ2/GE commands at the GL level and feeds `re3_fifo`; `GR2-RE3` rasterises; the refresh thread scans out | | IMPACT (`src/dev/mgras/`) | HQ3 command FIFO and direct raster writes (`hq_fifo`) | `MGRAS-HQ3` runs the command and geometry front end and feeds `rss_fifo`; one RSS thread per raster engine draws (Maximum IMPACT: two, each owning alternate rows, fed by the broadcast FIFO; `IRIS_MGRAS_ENGINES` of 1, 2 or 4 overrides, and a GPU raster unit uses one); the refresh thread scans out | | VPro (`src/dev/odyssey/`) | the command FIFO (`fifo.rs`) | `odyssey-work` decodes, transforms and sets primitives up and hands them to the raster queue; `odyssey-raster` and its `odyssey-pool` tile workers rasterise, texture and store; the refresh thread scans out. With `[graphics] raster = "wgpu"` the raster and pool threads do not start: the front end records into batches the host GPU draws (below) | @@ -615,14 +639,26 @@ three boards have optional JITs (`rex-jit`, `gr2-jit`, `gr4-jit`) that compile pixel pipelines with Cranelift; the interpreter remains the reference, and each JIT has an equivalence test against it. -The optional `rex-gpu` feature builds on `rex-dirty-upload` and adds a wgpu -path for Newport flat fills on the `REX3-Processor` thread. It is used by +GR2's quiescent boundary drains HQ2 and RE3 while both workers still run, then +sends their exit sentinels. Schema 10 serializes exact named `state`, `vram` +and `zbuf` parts; disk, rollback and live checkpoints all restore that same +validated representation. Partial HQ2/GL commands, RE3 continuation, FIN2 +waits and unread GEDMA output are architectural. Threads, FIFOs, trace and +presentation objects, telemetry and JIT memoization are runtime-only. A +power-on clears the full board in place and preserves only the selected +`gr2-jit` mode. + +The RE3 worker publishes fixed-size diagnostic counters atomically for +`gr2 metrics`: span-size buckets, consecutive same-pipeline opportunities, +synchronization boundaries, candidate framebuffer/Z traffic, host reads and +JIT activity. The display path separately counts frames and the host pixels it +scans. These counters reset rather than round-trip in a snapshot. + +The optional `rex-gpu` feature builds on `rex-dirty-upload` and adds the older +wgpu path for Newport flat fills on the `REX3-Processor` thread. It is used by recording replay and, only when `IRIS_REX3_GPU` is set, by the experimental -live renderer. It uses the common `wgpu` feature's `Arc` below and -exposes that device to the wgpu window presenter. The prototype still keeps -the CPU framebuffer canonical and synchronizes through it; sharing the device -does not make this the authoritative-buffer mode described in -`clanker/plans/GPUTodo.md`. +CPU-canonical live renderer. It is deliberately separate from production +`[graphics] raster = "wgpu"` and the two selectors are mutually exclusive. The CPU framebuffer stays canonical at every observation point. Supported fills are batched; the engine synchronizes (reads the GPU work back into the @@ -742,18 +778,33 @@ gets a retrace for each. InfiniteReality runs at 60 Hz. The O2's four timing interrupts are line compares and come from this thread too, at their lines. -### Newport resident GPU foundation (tests only) - -The separate resident raster in `src/dev/ng1/wgpu/resident.rs` is an -experimental test-only path, not the `rex-gpu` prototype or a configuration -option. A stopped, idle, exclusively owned board can seed its complete linear -2048 x 1024 RGB and auxiliary planes once on an injected common `Arc`. -The real GFIFO processor then uses bounded, ordered batches of supported flat -fills, writing only columns 0 through 1343 and leaving the rest of each -plane intact. It never falls back to CPU raster; an unsupported draw submits -the accepted prefix before latching a typed error. GPU failures also latch -typed errors. The ordinary start does not create a resident refresh thread; -tests may explicitly start a resident display worker on the injected device. +### Newport resident GPU backend + +The resident raster in `src/dev/ng1/wgpu/resident.rs` is the experimental +GPU-authoritative production path selected by `[graphics] raster = "wgpu"`; +it is not the `rex-gpu` prototype. Machine construction creates one validated +Vulkan or D3D12 `Arc`, shares it across both Newport heads, and activates +each board while it is still exclusively owned. Activation seeds complete +linear 2048 x 1024 RGB and auxiliary planes before the board is published. + +The GFIFO processor then uses bounded ordered batches for the complete +resident command set and never falls back to CPU raster. Unsupported work +submits the accepted prefix before latching a typed fatal error; device loss +is sticky and reaches machine control once. Production `Device::start` +automatically starts the processor and the fixed direct-wgpu or explicit +readback-compatibility display worker. Start is idempotent because CI mode can +start peripherals once while the CPU is parked and again when the guest is +released. Tests retain an injected-backend/manual-display seam so lifecycle +and failure policy can be checked without constructing a physical GPU. + +Direct mode rejects CPU-plane debug overlays (`rex cmap on`, `disp debug on` +and the developer `draw debug on`) before changing their state; leaving one +enabled would otherwise make every direct presentation fail. Explicit +readback mode retains those overlays. Disk snapshots always rewrite the second +head's two named planes, including under CPU raster, so reusing a snapshot name +cannot mix current registers with stale resident planes. Missing, truncated or +failed resident plane restores propagate as snapshot errors rather than +panicking a service thread. The first M3 slice routes those fills through the command ABI that later opcodes extend: one fixed 64-byte command names a 256-byte normalized REX3 @@ -936,10 +987,10 @@ Each activation or quiescent reset establishes a new epoch. Typed stream positions distinguish accepted, submitted and callback-confirmed work; each plane separately records its last possible write and whether its CPU shadow is current. Supported no-op fills advance the stream and REX3 context without -changing a plane generation. A same-device, read-only binding seam exists for -the test-only direct compositor (the two planes only; display metadata is -bound separately). The binding is epoch-tagged and does not submit raster -work. The test-only display worker inserts a typed GFIFO frame marker and +changing a plane generation. A same-device, read-only binding seam exposes the +two authoritative planes to direct scanout; display metadata is bound +separately. The binding is epoch-tagged and does not submit raster work. The +resident display worker inserts a typed GFIFO frame marker and holds the consumer at that cut while it submits the exact resident prefix, composes from the resident planes, and queues same-device presentation and optional composed-output readback. Later draws cannot overtake the scanout. @@ -956,7 +1007,10 @@ unchanged. A first resident frame checks the preserved parts of the CPU DID map once and rejects nonzero history that cannot be reconstructed from current VC2 state; it does not upload the decoded map. Moving that history between devices or back to a CPU renderer is not implemented. A mode-table fence published after the cut defers the frame rather than forcing -fence progress or presenting stale state. GL and presenters using another +fence progress or presenting stale state. The real common wgpu presenter is +tested with a winit window and surface on the resident device on both +backends; it presents the composed buffer without a plane transfer. GL and +presenters using another device are rejected by the direct path. Screenshot, frame request, VNC and headless test capture read the composed output only, and publish pixels and complete requests only after successful staging. An ignored hardware test @@ -966,7 +1020,7 @@ the presenter's unpacked frame texture with the software compositor using an explicit test-only readback. This exercises surface submission but does not inspect the final swapchain or on-screen pixels. The threaded window test is available on Windows and Linux; macOS requires a main-thread window harness. -The test-only display selection is a fixed typed `DirectWgpu` or +Production and test display selection is a fixed typed `DirectWgpu` or `ReadbackCompatibility` mode; renderer rejection, capture demand and capture failure never switch it. The explicitly selected compatibility display attributes a full two-plane observation to `Compatibility`, copies the 16 MiB @@ -979,10 +1033,10 @@ After the initial seed, ordinary resident frames and idle/fence processing perform no raw-plane transfer, CPU-plane access or CPU raster fallback. Raw-plane observations explicitly select RGB, auxiliary or both, and return attributed snapshots; installing and acknowledging a CPU shadow is a separate -operation. Board-level HostR is wired through attributed `HostRead` snapshots; -standalone snapshots, recording and other raw consumers are not wired up while -resident. The idle processor polls the device error sink, -and the first fault is sent once through the machine's fatal channel. +operation. Board-level HostR, snapshots, recording and monitor consumers use +that boundary and report their reason and bytes. The idle processor polls the +device error sink, and the first fault is sent once through the machine's +fatal channel. Fatal GPU errors reject reset and restart before touching CPU planes; a successful quiescent reset after a recoverable rejection rearms notification. Stopped resident snapshots take one lifecycle write lock against PIO, DMA and @@ -1020,11 +1074,21 @@ completion and 0.26 ms for the frame cut with no capture readback. HostR was four operations and reused two selected-plane maps, so the measured design keeps full-plane HostR snapshots. The same evidence retains 8 x 8 tiles, GPU-generated active-tile dispatch, 1,024 state slots and three reusable sets. -These are gated replay measurements, not production IRIX application timing. - -Newport still rejects `[graphics] raster = "wgpu"` until production activation; -live frontend integration remains future work. The legacy prototype remains -CPU-canonical. +These are replay measurements, not representative IRIX application timing. + +Production activation accepts `[graphics] raster = "wgpu"` only in a `wgpu` +build and initially only when the resolved backend is Vulkan or D3D12. Direct +mode requires a visible same-device wgpu presenter and fails startup rather +than falling back to GL or CPU raster. Explicit readback compatibility is +fixed at startup and allows a two-second fresh-frame request because its first +16 MiB observation may exceed the frontend's historical 250 ms timeout. +Machine construction shares one device across Newport heads, automatic +processor/display startup is idempotent, and `rex status` reports the +backend, adapter, no-fallback policy and aggregate telemetry. Embedded-PROM +boots, direct and readback captures, loopback VNC, dual-head construction, +disk restore, CI rollback and reset/restart have run in production mode. +Representative IRIX desktop/application performance remains open. The legacy +prototype remains CPU-canonical and mutually exclusive. Residency tests can opt into replacing each CPU shadow with its own page-aligned OS mapping before the seed. Scoped no-access protection, @@ -1206,15 +1270,17 @@ constructor, answers `GfxDisplay::cycles` from it, and does not build a monitor's `screen`/`ptr` commands and the VNC server see of a board, whichever board it is. The refresh thread leaves the composed guest frame in `screen().rgba` (stride 2048, `0xAABBGGRR`, no status bar) and bumps -`screen().frame_seq` each time it rewrites it. It does so only when asked: -`request_frame` for one frame, `set_capture(true)` for every frame while a -consumer (VNC) watches. A board that composes on the CPU has its finished -frame copied; Newport composes inside the window's renderer, which reads the -frame back. Continuous capture switches a GL window to its software -compositor, while a wgpu window reads its composed GPU output. With no window -(`--ci`) the refresh thread composes Newport's state in software itself. The -test-only resident display also reads its composed GPU output, as described -above. +`screen().frame_seq` each time it rewrites it. GR2, the O2 and InfiniteReality +compose on the CPU and do this every frame, as do IMPACT and VPro unless the +window's renderer takes GPU frames (above). Newport composes inside the window's +renderer, so it fills `rgba` only when asked: `request_frame` for one frame, +`set_capture(true)` for every frame while a consumer (VNC) watches. In CPU-raster Newport operation, continuous capture switches the GL window to +its software compositor, while a wgpu window reads its composed GPU output. +With no window (`--ci`) the CPU refresh thread composes in software itself. +Direct resident Newport instead reads requested composed output from the +resident scanout buffer; its ordinary frames stay on the shared GPU. Explicit +resident readback compatibility observes both complete raw planes into a +separate CPU cache before using the ordinary renderer. `copy_frame`, `wait_fresh_frame` and `fresh_frame` are the helpers that read it; `wait_fresh_frame` says whether the board composed a new frame in time, so a caller can tell a fresh screenshot from a repeat of the last one. diff --git a/HELP.md b/HELP.md index ed477cfe1..f5a640f81 100644 --- a/HELP.md +++ b/HELP.md @@ -535,7 +535,34 @@ standalone `iris` window if you need them. The window is freely resizable. At 1× and 2× scale the display is rendered with nearest-neighbour pixel-perfect sampling; at all other sizes trilinear filtering with mipmaps is used. `lock_aspect_ratio = false` in `iris.toml` allows free resizing with letterboxing. When the guest changes video mode (PROM, then IRIX during boot) the window snaps to the new resolution times the configured `scale` / `--2x`. -`renderer = "wgpu"` (or `--renderer wgpu`, in a build with the `wgpu` feature) draws the window through wgpu instead of OpenGL, with the same scaling rules. With a VPro or IMPACT whose raster unit is on the GPU (`raster = "wgpu"`) the window shares that GPU, and each frame goes from the board's memory to the screen without passing through main memory; screenshots and VNC still read frames back when they ask for them. Newport's display (Indy, Indigo2 XL) is composed on the GPU as well. Its legacy `IRIS_REX3_GPU` prototype shares the device in a `rex-gpu` build but still synchronizes through the CPU framebuffer. A separate GPU-resident Newport path is internal to tests and cannot be selected; Newport rejects `raster = "wgpu"` until M3 (complete REX3 command coverage). If wgpu cannot open the window, iris says so and draws with OpenGL. +`renderer = "wgpu"` (or `--renderer wgpu`, in a build with the `wgpu` +feature) draws the window through wgpu instead of OpenGL, with the same scaling +rules. With a VPro or IMPACT whose raster unit is on the GPU +(`raster = "wgpu"`) the window shares that GPU, and each frame goes from the +board's memory to the screen without passing through main memory; screenshots +and VNC still read frames back when they ask for them. + +Newport can also select the experimental GPU-authoritative raster with +`[graphics] raster = "wgpu"`. Its default `newport_mode = "direct"` requires a +visible window to use `renderer = "wgpu"` on the same device; startup fails +rather than falling back to GL or CPU raster. The initial production backends +are Vulkan and D3D12. `gpu_backend = "auto"` is allowed only when it resolves +to one of those, and explicit Metal or GL selection is rejected. Screenshots, +CI capture and VNC read the composed GPU output when requested, not the raw +planes. + +`newport_mode = "readback"` (or `--newport-mode readback`) is an explicit slow +compatibility/validation mode for a GL or CPU compositor. It synchronizes both +complete 2048 x 1024 planes -- 16 MiB -- for each newly composed frame and may +take up to two seconds on first capture. It is never selected because direct +presentation failed. The older `IRIS_REX3_GPU` prototype remains +CPU-canonical, requires a `rex-gpu` build, and is mutually exclusive with the +production resident selector. Other boards retain their historical +wgpu-to-OpenGL window fallback; direct resident Newport does not. + +iris-gui can run only that explicit Newport readback mode when built with +`cargo run -p iris-gui --release --features wgpu`; its eframe frontend does not +yet provide the same-device wgpu presenter required by direct mode. ## VNC @@ -582,9 +609,11 @@ it, and a port already in use is reported rather than failing the start. What you see is the guest's screen, without the window's status bar. Only the parts of the screen that change are sent, so an idle desktop costs next -to nothing. With Newport, the window uses the software compositor while a +to nothing. With CPU-raster Newport, the window uses the software compositor while a viewer is connected (`disp compositor gl` is refused) and switches back when -the last one leaves. +the last one leaves. Direct resident Newport instead captures composed GPU +output while viewers are connected; it does not refresh the raw CPU plane +shadows. **The mouse.** The guest's mouse sends movements, not positions, and IRIX accelerates fast ones. iris steers the guest pointer onto the spot you point @@ -672,8 +701,9 @@ heads = 1 # 2 = dual-head Newport (second REX3 in GIO slot 0; resolution = "guest" # or "1024x768", "1280x960", "1280x1024" # threads = 2 # VPro: fragment pipeline workers (more on a host with cores to spare) # tile = 32 # VPro: screen tile size, a power of two from 8 to 256 -# raster = "cpu" # VPro, IMPACT: "wgpu" draws on the host GPU (builds with the wgpu feature; work in progress). Newport refuses it. -# gpu_backend = "auto" # with raster = "wgpu": "vulkan", "metal", "dx12" or "gl"; WGPU_BACKEND=gl (and so on) overrides it +# raster = "cpu" # VPro, IMPACT, Newport: "wgpu" draws on the host GPU (needs the wgpu feature) +# newport_mode = "direct" # Newport wgpu only: "direct" or explicit slow "readback" compatibility +# gpu_backend = "auto" # with raster = "wgpu"; Newport initially supports only Vulkan and D3D12; WGPU_BACKEND overrides it [clock] # fixed_mhz = 33 # Count MHz: default half the part's clock: 75 on the Indy and Indigo2 R4400, 97.5 on IP28 @@ -1048,16 +1078,16 @@ load base/desktop # restore saved state (graphics limitations below) `iris-ci` adds restore/rollback checkpoints, `tree`, `diff`, `gc`, `validate` and HTTP `push`/`pull`; iris-gui has Save/Restore under the Machine menu. The -on-disk format is schema version 7 — a `snapshot.toml` manifest, postcard-encoded +on-disk format is schema version 10 — a `snapshot.toml` manifest, postcard-encoded device state, and RAM stored as content-addressed chunks under `saves/.cas/`. A snapshot refuses to load onto a different CPU model or host architecture. See [snapshot coverage](STORAGE.md#snapshots-and-rollback) and `clanker/snapshot/`. -GR2 currently omits VRAM/full drawing state; IMPACT saves no board state yet. -See [TODO.md](clanker/TODO.md) for device/checkpoint completion work. The Indy, Indigo2, O2 and Octane have snapshots. An Octane snapshot is -refused with two processors or with an IMPACT or VPro board fitted: the stop -does not yet bring both processors to one recorded instruction boundary, and -neither board saves its state. Loading an Octane snapshot writes its boot flash and -NVRAM over the configured flash and NVRAM files, so a PROM flashed since the -save is replaced by the one the snapshot holds. +refused with two processors or virtio-serial: the stop does not yet bring both +processors to one recorded instruction boundary, and virtio-serial does not +save its queues. IMPACT and VPro V6/V8/V10/V12 board state and graphics memory +are included. XZ and Extreme snapshots include the complete GR2 command, +drawing, display, VRAM and Z/stencil state. Loading an Octane snapshot writes +its boot flash and NVRAM over the configured flash and NVRAM files, so a PROM +flashed since the save is replaced by the one the snapshot holds. diff --git a/STORAGE.md b/STORAGE.md index d8a6ed425..0dcc28867 100644 --- a/STORAGE.md +++ b/STORAGE.md @@ -218,15 +218,14 @@ resumed afterwards still has its disks and its writes. ## Snapshots and rollback -Graphics coverage is incomplete: GR2 saves register/microcode/display tables -but omits VRAM and full drawing-engine state; IMPACT board state is not saved -yet. A snapshot of either board cannot reproduce its complete graphics state. -See [TODO.md](clanker/TODO.md) for remaining device and restore work. +Graphics snapshots include Newport framebuffers, complete XZ/Extreme GR2 +command and drawing state, IMPACT, and VPro V6/V8/V10/V12. See +[TODO.md](clanker/TODO.md) for remaining device and restore work. Capture RAM, supported device state, and the COW overlay into `saves//`, and restore it later. CPU, MC, IOC, HPC3, REX3, RTC, EEPROM, SCSI controller, and the Seeq Ethernet chip all round-trip. The current schema -version is 7: postcard-encoded binary device state plus content-addressable +version is 10: postcard-encoded binary device state plus content-addressable chunked RAM under `saves/.cas/`. A second snapshot taken from the same parent adds **zero bytes** to disk for any RAM region that didn't change — same storage model as Docker layers. A snapshot records which CPU it was taken on @@ -234,10 +233,17 @@ storage model as Docker layers. A snapshot records which CPU it was taken on Snapshots work on the Indy, Indigo2 (IP22 and IP28), O2 and Octane. The Origin, Onyx, Fuel and Tezro machines refuse a save or load, since their node -hardware has no saved state yet. The O2 and the Octane save in this format too. An Octane snapshot covers one -processor and no graphics board: with two processors, an IMPACT or VPro board -or virtio-serial, a save or load is refused with the reason. None of those -saves its state. +hardware has no saved state yet. The O2 and the Octane save in this format too. +An Octane snapshot covers one processor and may include IMPACT or VPro +V6/V8/V10/V12 graphics. With two processors or virtio-serial, a save or load +is refused with the reason. + +Schema 10 stores XZ and Extreme as exact named `state`, `vram` and `zbuf` +parts. The state includes partial HQ2/GL commands, RE3 continuation, FIN2 +waits and unread GEDMA output. Disk restore, CI rollback and live debugger +checkpoints validate and restore the same parts; XZ/Extreme and board-presence +mismatches are errors. Runtime threads, traces, JIT code and telemetry are +rebuilt or reset rather than serialized. From the interactive monitor (`telnet 127.0.0.1 8888`): ``` @@ -376,5 +382,3 @@ Manual/raw paths (if you want to drive `dd` yourself): - Reads MUST use `bs=512` (or any 512-multiple); `bs=64` returns "I/O error". - Writes must be padded to `bs`; add `conv=sync` for short inputs. - Inside IRIX: `dd if=/dev/rdsk/dks0d2s0 bs=512 | tar xf -` - - diff --git a/TESTING.md b/TESTING.md index c62b45daf..6e7a61764 100644 --- a/TESTING.md +++ b/TESTING.md @@ -252,8 +252,10 @@ stale pixels even when the later final state matches: REX3_GPU_BACKEND=vulkan cargo test -p iris --lib --features rex-gpu,rex-jit,rexdiag rex3_corpus_gpu_ -- --ignored --nocapture --test-threads=1 ``` -The separate **test-only resident Newport raster** has CPU-only limit, -status and GFIFO gate tests, plus ignored hardware parity tests. The hardware +The **resident Newport raster** has CPU-only limit, status and GFIFO gate +tests, plus ignored hardware parity tests. Production activation uses the same +backend; the hardware tests retain direct injection so they can isolate plane +authority, ordering and failure behavior. The hardware tests require the named hardware adapter/backend rather than silently skipping; they compare both complete linear planes and the final architectural `Rex3Context` with the CPU oracle, including overlapping fills, clipping, @@ -261,8 +263,8 @@ masks, tile boundaries, retries, and offscreen columns. They exercise the real GFIFO, reset, and partial-prefix restore. Scissor/CID clip modes outside the gated slice are rejected without advancing context or falling back. Explicit observation selects RGB, auxiliary or both and returns a snapshot; installing -and acknowledging a CPU shadow is separate. These tests do not enable resident -raster or direct scanout in a running emulator. +and acknowledging a CPU shadow is separate. These tests do not depend on the production configuration selector; live +activation is validated separately below. The opt-in CPU-plane residency tripwire copies each board shadow into its own page-aligned OS mapping, seeds the GPU, then protects both complete mappings. @@ -289,10 +291,11 @@ published frame sequence. Additional cases inject loss into the shared asynchronous device-error sink before and after submission and while idle, including a full GFIFO and a full ordinary machine event queue. They require a single first-fault notification and reject restart. -These tests opt in to the test-only display worker; they do not enable resident -raster in the emulator. They do not complete board-level snapshot or monitor -raw consumers, full opcode/lifecycle coverage, or production guest -validation. On Windows and Linux, an additional hardware test sends two +These tests opt in to the manual display seam rather than production startup, +so failures remain attributable to scanout/capture behavior. Production tests +and live runs cover automatic startup, board-level snapshot/monitor consumers +and the visible same-device window. On Windows and Linux, an additional +hardware test sends two ordered draws and a resize through the common wgpu presenter on a native window. It explicitly reads the presenter's unpacked frame texture, including padded rows after the resize, and compares it pixel-for-pixel with the software @@ -435,12 +438,12 @@ Remove-Item Env:\IRIS_NO_JIT -ErrorAction SilentlyContinue .\scripts\test-resident-gate.ps1 ``` -The exact 130-case script requires 43 `resident_` CPU tests, four flat-fill +The exact 131-case script requires 44 `resident_` CPU tests, four flat-fill tests, two line tests, two HostW walker tests, one HostW interpreter/JIT source test, one shade-DDA test, one pattern-state test, two resident HostR port/retirement tests, three command-ABI tests and three direct-state tests, then **24 resident GPU tests and ten direct scanout tests on each backend**. -That is 130 tests in total. The window test requires an available native +That is 131 tests in total. The window test requires an available native desktop and a GPU that can present to it. The script checks each libtest pass count, rejects hardware skips and prints the selected adapter; both Vulkan and D3D12 must succeed for the full gate. `.\scripts\test-resident-gate.ps1 @@ -470,6 +473,57 @@ mappings remain protected. Missing, duplicate, incomplete or failed snapshot halves must not publish registers, pixels, captures or a restartable board. Metal remains outside this gate. +Production activation has two additional ignored hardware regressions. They +construct a normal Newport machine rather than injecting the backend directly: +both heads must share one `Arc`, repeated peripheral start must leave both +processor/display workers alive, direct mode must require the same-device +presenter, and explicit readback mode must advertise its two-second minimum +capture timeout. Run them on both supported backends: + +```powershell +$env:REX3_RESIDENT_BACKEND = 'vulkan' +cargo test --locked -p iris --lib --features wgpu production_newport_wgpu_tests -- --ignored --nocapture --test-threads=1 +$env:REX3_RESIDENT_BACKEND = 'dx12' +cargo test --locked -p iris --lib --features wgpu production_newport_wgpu_tests -- --ignored --nocapture --test-threads=1 +cargo test --locked -p iris --lib --features wgpu power_on_reset_fits_default_service_thread_stack -- --test-threads=1 +``` + +The final live matrix uses an ordinary `iris` binary and CI socket, not a test +fixture. For each of Vulkan and D3D12 it boots the embedded PROM in direct +mode, requests a composed screenshot, saves and restores a disk snapshot, +performs CI rollback, resets through the monitor, restarts, captures again and +checks `rex status` for `no_cpu_fallback=true` and zero unsupported work. +Vulkan additionally exercises a visible same-device wgpu window, explicit +readback compatibility and a real RFB 3.8 loopback VNC framebuffer update. +The direct VNC run must show composed direct bytes with zero compatibility raw +bytes. These runs establish production lifecycle wiring independently of the +representative application run below. + +The application-level acceptance run uses an installed IRIX 6.5 IP22 system +with Xsgi, 4Dwm, toolchest, Showcase and the Atlantis demo. It boots the same +disk through three isolated overlays: CPU raster, Vulkan resident direct and +D3D12 resident direct. A scripted Showcase rectangle/ellipse workload must +produce the same canvas pixels on all three paths. Save/restore and rollback +must reproduce the complete pre-mutation resident screenshot exactly. +Atlantis then runs for a 15-second warm-up and a 30-second measured interval. +On the RTX 5070 / 16-logical-CPU acceptance host, the measured results were: + +| Engine | REX3 GO/s | Host core equivalents | Throughput vs CPU | Host CPU vs CPU | +|---|---:|---:|---:|---:| +| CPU raster/JIT | 4,761.5 | 1.673 | baseline | baseline | +| Vulkan direct | 6,765.6 | 1.574 | +42.1% | -5.9% | +| D3D12 direct | 5,918.0 | 1.545 | +24.3% | -7.7% | + +CPU throughput is the `rex status` `GO TOTAL` delta. Resident throughput is +the telemetry accepted-command delta, and host core equivalents are process +CPU seconds divided by wall seconds. Both resident samples require +`unsupported=0`, `no_cpu_fallback=true`, no increase in raw/composed readback +counters during the timed interval, and conservative p95 presentation buckets +no higher than 4.2 ms. The accepted Showcase canvas crop SHA-256 was +`b733d604c1a34554578e69fd9874d9ab892a470d54e67428d6a7fc081eec4190` +on all three paths. Screenshots and snapshot plane observations occur outside +the timing window and remain explicitly attributed. + The M8 cases add checked histogram/delta accounting, exact GFIFO full-versus-contention telemetry, bounded-set wait accounting, a 4 ms continuous-stream batch-age boundary and profiling-only validation of the diff --git a/clanker/INDEX.md b/clanker/INDEX.md index 10d003a74..e32cb2fec 100644 --- a/clanker/INDEX.md +++ b/clanker/INDEX.md @@ -488,7 +488,7 @@ The host-GPU raster path (`src/wgpu`, `src/dev//wgpu`): what the kernels | Note | Kind | Title | |---|---|---| | [gl-backend-presents-only-on-the-windows-display.md](wgpu/gl-backend-presents-only-on-the-windows-display.md) | rule | wgpu's GL backend presents only from an instance opened on the window's display; without it, surfaceless EGL and a readback per frame through the GL renderer. Headless/CI/iris-gui stay displayless; keep PreferEgl when the GL renderer shares a thread with a board device | -| [gpu-floats-are-not-the-hosts.md](wgpu/gpu-floats-are-not-the-hosts.md) | rule | GPU float division is not correctly rounded and products get fused: `div_exact` and the multiply-by-one guard before comparing with the CPU model (measured on RADV) | +| [gpu-floats-are-not-the-hosts.md](wgpu/gpu-floats-are-not-the-hosts.md) | rule | GPU float division is not correctly rounded and products get fused: use `div_exact` and a bit-observable f32 barrier before comparing with the CPU model (measured on RADV and D3D12) | | [vram-accesses-are-atomic.md](wgpu/vram-accesses-are-atomic.md) | design | Board memory on the GPU is accessed with atomics, because two pixels can share a word; texel reads use a second plain binding (1%); what relaxed atomics compile to; never mix atomic and plain on one word | | [the-mirror-and-its-dirty-maps.md](wgpu/the-mirror-and-its-dirty-maps.md) | design | The GPU unit's board memory has a CPU mirror; dirty maps both ways keep them in step, and the front end must wait before it touches memory | | [read-storage-in-place.md](wgpu/read-storage-in-place.md) | rule | Read records, slots and tables from their buffers in place: a 4.8 KB struct copied by value gave silently wrong texels on RADV once the kernel grew | diff --git a/clanker/TODO.md b/clanker/TODO.md index 39c8aaf76..31adb95ea 100644 --- a/clanker/TODO.md +++ b/clanker/TODO.md @@ -32,22 +32,31 @@ guide and changelog when it lands. ## First: complete restore and CPU correctness -- [ ] **Implementation — GR2 snapshots and rollback.** Serialize RE3 VRAM, - raster registers, HQ2 parser/geometry/context state, and pending work at a - defined quiescent boundary. Current `Saveable` only saves selected registers, - microcode, SRAM, and display tables. Add equivalent support to the in-memory - checkpoint path. Complete when a restored X/irisGL scene, framebuffer hash, - and subsequent drawing match uninterrupted execution. +- [x] **Implementation — GR2 snapshots and rollback.** Schema 10 stores exact + named state, VRAM and Z/stencil parts after draining accepted HQ2 and RE3 + work. It preserves partial HQ2/GL commands, full RE3 continuation, FIN2 waits + and unread GEDMA output. Disk, rollback and live checkpoints share the same + validated representation; XZ/Extreme and board-presence mismatches fail. + Full XZ/Extreme round trips, continued drawing, reset, invalid late state, + named-part errors and machine-level disk/in-memory paths have regressions. Sources: [GR2 save/load](../src/dev/gr2/mod.rs), [machine snapshots/checkpoints](../src/machine.rs). -- [ ] **Implementation — IMPACT snapshots and rollback.** `Mgras::save_state` - returns an empty table and `load_state` is a no-op. Include HQ3/GE state, - ERAM contexts, RSS registers, tiled pixel memory, TE1 texture memory, DCB - tables, and interrupt state; include the same board state in checkpoints. - Complete when save/load and rollback survive context eviction, textured - drawing, and guest resolution changes with matching output. +- [x] **Implementation — IMPACT snapshots and rollback.** Schema 8 stores + named content-addressed Front/DCB/GE, HQ3, RSS, pixel/clip-ID and TE1 parts, + plus HQ4/MicroLAN state on Octane. Indigo2 disk snapshots and CI/live + checkpoints, and Octane disk snapshots, restore the same validated state. + Restore invalidates runtime JIT state and reseeds the wgpu mirror without + serializing host GPU objects. Product round trips, partial-CFIFO + continuation, invalid-state rollback, Octane lifecycle and Vulkan/D3D12 + reseeding have regressions. Sources: [MGRAS save/load](../src/dev/mgras/mod.rs), [board design](mgras/DESIGN.md). +- [ ] **Validation — IMPACT snapshot workload matrix.** Extend the structural + and drawing-continuation regressions to guest context eviction, textured + scenes and guest-programmed resolution changes, with framebuffer/readback + comparisons before and after disk restore and in-memory rollback. + Sources: [MGRAS tests](../src/dev/mgras/mgras_tests.rs), + [Octane HQ4](../src/dev/mgras/hq4.rs). - [ ] **Implementation/validation — snapshot every active device.** Audit HAL2, VINO/CDMC, DaynaPort, Ultra64, host networking, and HostGL against the state actually captured in `Machine`. Define reset/reconnect behavior for @@ -201,11 +210,14 @@ guide and changelog when it lands. - [ ] **Implementation — IMPACT buffer formats and 3D state.** Landed since the 2026-10-07 review: DRBpointers' second buffer (`[19:10]`), 12-bit RGB double buffering in the halves of one word, colour-index double buffering - by pixel type, stencil and depth reads and clears, and two raster engines - for Maximum IMPACT (rows split by `y % 2`). Still open: per-window - front/back selection and swaps in general, the remaining 36-bit pixel - formats, the raw RDRAM window (device space is still a register map), and - the GE count and destination-RE selection on Maximum IMPACT. + by pixel type, 4:4:4, 5:5:5 and 12:12:12 RGB, per-window front/back bank + selection and swaps, stencil and depth reads and clears, and two raster + engines for Maximum IMPACT (rows split by `y % 2`). Solid/High now expose + one fitted GE11, Maximum exposes two, and CONFIG.DestRE routes ordinary + register state and execution to either RE or both. Still open: the raw + RDRAM diagnostic window, whose physical swizzle has no available oracle; + independent per-RSS texture subsystems; the DMA line-count mask; and gaps + found by broader hardware/application validation. Complete when independent guest diagnostics and depth/stencil/double-buffer workloads pass on Solid, High, and Maximum configurations. Sources: [recorded gaps](mgras/DESIGN.md), diff --git a/clanker/gr2/DESIGN.md b/clanker/gr2/DESIGN.md index 848569e80..73a7750d5 100644 --- a/clanker/gr2/DESIGN.md +++ b/clanker/gr2/DESIGN.md @@ -9,9 +9,25 @@ boards. HQ2, RE3, and display each have a worker thread. GE7 stores diagnostic microcode but does not execute it. `gl.rs` and `gl_light.rs` hold the geometry and lighting HLE; this is functional emulation, not GE7 instruction emulation. -Saveable currently covers register/microcode/display tables, not VRAM or full -drawing/context state. See [current board guide](../../docs/graphics/gr2-xz-extreme.md) -and [feature-completion backlog](../TODO.md) for remaining work. +Snapshot schema 10 stores exact named `state`, `vram` and `zbuf` parts. The +state includes partial HQ2/GL parsing, complete RE3 continuation, FIN2 waits +and unread GEDMA output. Disk, rollback and live checkpoints share this +representation and validate it before mutation. Stop drains both engine +queues; power-on clears architectural state while preserving the selected JIT +mode. Runtime threads, queues, trace/presentation objects, telemetry and JIT +memoization are rebuilt or reset. + +Runtime telemetry counts span sizes, consecutive same-pipeline opportunities, +synchronization boundaries, candidate framebuffer/Z traffic, host reads, +presentation scans and JIT hits/misses. It is published atomically by the RE3 +worker for `gr2 metrics`; it is evidence, not architectural state. + +The M16 resident-GPU gate is a measured no-go for now. Captured GL traces show +useful long-span batching, but two interleaved sustained runs put warmed +`gr2-jit` at 178-187 Mpx/s for 4-pixel spans and 520-568 Mpx/s for +1280-pixel spans on the measured host. Ordinary presentation scans host VRAM, +and no resident GPU prototype or representative guest result exists. Retain +the JIT and revisit only with evidence that clears every gate. ## Original implementation proposal @@ -72,5 +88,3 @@ the design phases - - diff --git a/clanker/mgras/DESIGN.md b/clanker/mgras/DESIGN.md index 81923cbd7..dc6d207b5 100644 --- a/clanker/mgras/DESIGN.md +++ b/clanker/mgras/DESIGN.md @@ -82,9 +82,17 @@ each with its own 12MB; scanline y belongs to RE4 y % 2 (ignore/gr4/PP1.h MGRAS_REPLAY_BOARD=max replays goldens on them (desktop2: 35/35, same hashes), MGRAS_REPLAY_NOCHECK=1 times a mid-session recording. A 2-engine replay of a gltest fill/triangle session: same final state, 1.75x faster. - - not modelled: the GE count (both GE11s' work is one HLE in the HQ3 - thread; ERAM slot 1 is unused), per-RE4 register access (config DestRE), - the DMA line-count mask. + - Solid and High expose one fitted GE11 diagnostic port; Maximum exposes + two. An absent port no longer aliases GE0. Geometry work still converges + on one HLE in the HQ thread rather than executing independent GE11 + microcode, and ERAM slot 1 remains unused. + - CONFIG.DestRE bits 11:10 route ordinary raster-register writes and + execution to RE0 (0), RE1 (1), or both (3). TE1/TRAM state and DMA lines + deliberately remain broadcast because this model has one shared texture + subsystem; targeting either copy would deadlock or leave the shared state + inconsistent. Reset routing is broadcast so setup traffic sent before the + guest first programs CONFIG reaches every fitted RSS. Fully independent + per-RSS texture subsystems and the DMA line-count mask are not modelled. locks: Mgras' locks are crate::locks::SpinLock (an atomic flag with backoff that records its holder's thread and call site) and are registered with the @@ -162,9 +170,16 @@ phases: record::tests::replay_recs, no checkpoints (they start mid-session), then asserted ignore/mgras-golden power-on goldens, every checkpoint checked - Not yet: snapshots (save_state is empty). With a non-COW disk a restore - would leave the filesystem behind RAM, and the framebuffer belongs in - the machine's chunk manifest like REX3's; do both together. + - snapshots (schema 8): named content-addressed parts preserve Front/DCB/GE + state, partial HQ3 parsers and contexts, every RSS, tiled pixel and + clip-ID memory, TE1 tables/TRAM, and the Octane HQ4 protocol, GE uploads, + interrupt routing and MicroLAN transaction. Indigo2 disk snapshots, + CI/live in-memory checkpoints, and Octane disk snapshots use the same + parts. All parts and board/raster-engine identity are validated before + application; runtime JIT/GPU objects stay attached, their caches are + invalidated, and a wgpu unit is reseeded from the restored CPU mirrors. + Round-trip tests cover Solid, High and Maximum, a partial CFIFO packet, + invalid-state rollback, post-restore drawing and the Octane wrapper. - 2D: tools/xwd-compare.py checks XGetImage (xwd -root, fetched over the built-in NFS share) against `mgras fbdump`; 8-bit dumps match exactly, and xwud (XPutImage) redisplays them exactly apart from the cursor @@ -241,5 +256,10 @@ scanout to the XMAP DIB pointers ([9:0] main, [31:20] overlay; PROM value until written); DRBsize gives tiles per row (PROM 1280x1024 layout until written). state_bytes rebuilds the old 1280x1024 fb/overlay views through the scanout pointers plus a hash of any other set bit, so desktop2.rec -still replays 35/35. Not yet: the raw RDRAM window (device space is still a -register map). Two raster engines: see above. +still replays 35/35. Not yet: the raw RDRAM diagnostic window (device space +is still a register map). Public diagnostics establish addresses of the form +`(pp << 28) | (rdram << 24) | offset` and the raw memory range +`0x04000000..0x061ffffe`, but the physical pixel/RDRAM swizzle needed to map +those addresses onto PixMem is not available in the checked sources. Do not +invent a linear mapping; this remains blocked on a diagnostic or hardware +oracle. Two raster engines: see above. diff --git a/clanker/plans/GPUTodo.md b/clanker/plans/GPUTodo.md index 9d2150c3e..f2ae0bd1a 100644 --- a/clanker/plans/GPUTodo.md +++ b/clanker/plans/GPUTodo.md @@ -1,22 +1,21 @@ # REX3 proper GPU mode: implementation plan -Status: upstream integration in progress 2026-11-15. Upstream -[#569](https://github.com/techomancer/iris/pull/569) merged M0, the gated -test-only M1 resident slice and a minimum M2 scanout slice. -[#731](https://github.com/techomancer/iris/pull/731) merged the test-only -GPU DID expansion and retained-history handling. The presenter follow-up -(`c46203a4`, `b2184f70`, `a6e5aa30`, 2026-11-14: resident frames through the -common wgpu presenter on a native window, test-only) is on `main` too -(checked 2026-11-15 at `dcd54c20`). #748 reduced DID dispatch latency and -#749 completed the gated M2 readback compatibility slice. #750 is the open -upstream M3 branch and is being expanded to carry paired M3+M4 coverage. The -local cumulative paired branches now carry M5+M6 and M7+M8. M0-M8 are -complete for the gated, test-only candidate path, but those paired branches -are not yet in upstream `main`. Production activation, application-level M8 -acceptance and M9/M10 remain open. No production GPU-authoritative Newport -mode has landed, and live `raster = "wgpu"` remains rejected. The tile-binned -`IRIS_REX3_GPU` prototype is separate and CPU-canonical, not the target -architecture. +Status: the M0-M16 implementation campaign is complete. The remaining M9-M16 +Newport, IMPACT, VPro and GR2 work is consolidated into one commit for upstream +submission, per maintainer request. Experimental Newport production activation +and representative application acceptance are complete for the standalone +`iris` frontend. +`[graphics] raster = "wgpu"` now selects the GPU-authoritative Newport path, +with direct same-device presentation by default and explicit readback +compatibility when requested. The exact dual-backend gate and the production +lifecycle matrix pass on Vulkan and D3D12. An installed IRIX 6.5 desktop, +Showcase and the animated Atlantis demo also pass on CPU raster and both +resident backends with matching pixels, zero unsupported work, no fallback, +bounded presentation latency and better live command throughput. The +tile-binned `IRIS_REX3_GPU` prototype remains separate and CPU-canonical. +The submission branch remains cumulative with #757 and #758 until those +prerequisites merge. Its isolated M9-M16 delta is one commit; rebase that +commit onto current upstream after the prerequisites land. ## Goal @@ -75,24 +74,27 @@ has landed on upstream `main`, not what has been implemented in a local branch. 3. **M7+M8:** pair snapshot/reset/failure lifecycle with telemetry and latency bounds. Adapt Tzeentchnet/iris#25 and Tzeentchnet/iris#26, then rerun representative live workloads before making an efficiency claim. -4. **M9/M10 activation and frontends:** adapt Tzeentchnet/iris#27 and - Tzeentchnet/iris#28 only after the required raster, HostRW, observer and - lifecycle coverage has landed. Enable the direct resident path explicitly; - keep readback compatibility labeled and never silently fall back. - -The three paired PRs target upstream `main` and remain buildable while -production activation is gated. The second and third PRs are cumulative until -their prerequisites merge because GitHub cannot use a fork-only branch as an -upstream base; put the isolated fork compare link at the top of each body. -After a prerequisite lands, rebase the next branch onto the updated `main` and -rerun its complete gate. Reconcile shared code and documentation instead of -copying a stale fork version over upstream changes. +4. **M9-M16 consolidated follow-up:** deliver Newport activation and + frontends, IMPACT fidelity and lifecycle, VPro residency and snapshots, + and GR2 lifecycle plus the evidence-gated resident-GPU decision as one + commit. Enable GPU paths explicitly, keep compatibility modes labeled, + fail closed, and retain `gr2-jit` after the measured GR2 no-go decision. + +The three paired PRs through M8 target upstream `main` and remain buildable +while production activation is gated. The second and third PRs are cumulative +until their prerequisites merge because GitHub cannot use a fork-only branch +as an upstream base. The M9-M16 follow-up is one additional commit atop M8 and +is likewise cumulative only until #757 and #758 land. Put the isolated fork +compare link at the top of every cumulative PR body. After a prerequisite +lands, rebase the next branch onto the updated `main` and rerun its complete +gate. Reconcile shared code and documentation instead of copying a stale fork +version over upstream changes. ### Next follow-up and review feedback -- M2 and its presenter follow-up are on `main`. Update existing #750 with the - paired M3+M4 stack from the latest upstream base, then rerun the resident - gate and core/GUI checks before force-pushing it. +- #750 has merged. #757 and #758 carry M5+M6 and M7+M8. Keep the M9-M16 + submission cumulative until they land, then rebase its single delta commit + onto current `main` and rerun the complete gate. - The maintainer's broader concern is: "i am not sure efficient wgpu acceleration of rex3 is possible. i hope that it is, we could definitely use some speed boost". Treat M3-M7 as correctness, ownership and lifecycle @@ -160,11 +162,14 @@ before an otherwise independent Newport PR. The renderer and rasterizer selections remain independent, but a user asking for the proper accelerated path receives wgpu for both. -This is the target matrix, not a current Newport selector: normal Newport -supports CPU raster with GL/CPU or wgpu composition. Its separate -`IRIS_REX3_GPU` experiment is CPU-canonical. The resident wgpu raster and -direct scanout are internal tests only, and the readback compatibility mode -is not implemented as a live option. +This matrix is now available in the standalone `iris` frontend. Newport +`raster = "wgpu"` defaults to direct same-device wgpu presentation and fails +startup rather than falling back to GL/CPU. `newport_mode = "readback"` +explicitly selects the slow compatibility path. The separate +`IRIS_REX3_GPU` experiment remains CPU-canonical and mutually exclusive. +`iris-gui` still lacks a direct eframe wgpu presenter and therefore cannot +host the proper visible direct mode. A build with the iris-gui `wgpu` feature +can deliberately host resident Newport through `newport_mode = "readback"`. ## Existing evidence and prior art @@ -701,10 +706,9 @@ of the updated main branch, so none were cherry-picked. Main uses upstream wgpu 30.0.1 and has no active vendored wgpu-core override to preserve. The legacy `rex-gpu` prototype now builds on the common `Arc` and exposes it through `GfxDisplay`, so a wgpu window reuses the same device. The prototype -still synchronizes through its CPU-canonical framebuffer. Newport explicitly -rejects `raster = "wgpu"` rather than silently treating that selection as -CPU raster. Keep it unavailable until the coverage and activation gates are -met. +still synchronizes through its CPU-canonical framebuffer. The later +production activation gives Newport `raster = "wgpu"` its distinct +GPU-authoritative meaning rather than selecting this prototype. ### M1 - Full authoritative GPU planes @@ -745,37 +749,29 @@ consumers; the legacy `rex-gpu` replay/live prototype remains CPU-canonical. Complete when a GPU-rendered live frame reaches the window with zero framebuffer readback and without a CPU plane upload. -Partial, test-only: a read-only same-device binding and GFIFO frame marker +The original gated slice established a read-only same-device binding and GFIFO frame marker to order resident fills, direct scanout and a GPU frame consumer. Screenshot, frame-request, headless and VNC tests capture composed output only. Ordinary frames after seeding have zero raw-plane upload/readback, CPU-plane access -and CPU fallback. In the presenter follow-up (now on `main`), a test-only -`WgpuRenderer` handoff makes three presentations, including a resize, on a -real native window using the same GPU. An explicit diagnostic reads the -presenter's unpacked frame -texture for pixel parity with the software compositor; it does not inspect -final swapchain or on-screen pixels. This threaded test is available on -Windows and Linux; macOS needs a main-thread window harness. Resident refresh -no longer decodes DID on the CPU. Changed VC2 state uploads at most the 64 KiB -VC2 RAM and a 32-byte parameter block, -then a GPU kernel expands DID before scanout; the 2 MiB decoded map is never -uploaded. The complete initial display-state transfer is below 128 KiB. -CPU-oracle parity covers disabled DID, malformed pointers, line-table -termination with stale-row preservation, zero scanline length, and dimension -changes on Vulkan and D3D12. The GPU DID buffer belongs to the display and -survives scanout rebinds and raster epochs; an inactive display preserves its -contents. The initial resident frame explicitly rejects nonzero retained CPU -DID history outside the area reconstructed by the current VC2 state. Before -production activation, define how a CPU-to-GPU transition or device -replacement preserves that history without violating the transfer contract. -An explicit compatibility display attributes a complete two-plane observation -to `Compatibility`, copies the 16 MiB snapshot into a separate CPU cache and -calls the ordinary renderer without acknowledging the protected CPU shadows. -It is deliberately slow and never an automatic fallback. M2 is complete -within this gated test-only scope. Production live integration and final -on-screen validation remain open; `iris-gui` cannot select this mode. - -The Windows gate `.\scripts\test-resident-gate.ps1` checks 130 cases: 43 +and CPU fallback. The `WgpuRenderer` common-presenter handoff is exercised +through a real winit window and surface on the resident device on Vulkan and +D3D12; the presenter submits and presents a frame without a plane transfer. +Resident refresh no longer decodes DID on the CPU. Changed VC2 state uploads +at most the 64 KiB VC2 RAM and a 32-byte parameter block, then a GPU kernel +expands DID before scanout; the 2 MiB decoded map is never uploaded. The +complete initial display-state transfer is below 128 KiB. CPU-oracle parity +covers disabled DID, malformed pointers, line-table termination with +stale-row preservation, zero scanline length, and dimension changes on Vulkan +and D3D12. An explicit compatibility display attributes a complete two-plane +observation to `Compatibility`, copies the snapshots into a separate CPU +screen cache, and calls the ordinary renderer without acknowledging the +board's stale CPU shadows. Its 16 MiB full-plane readback per frame is +deliberately slow and never an automatic fallback. Production activation now +wires both fixed modes into the standalone frontend. Direct visible windows +require the same-device wgpu presenter; CI capture uses direct offscreen +scanout. `iris-gui` still cannot select visible direct mode. + +The Windows gate `.\scripts\test-resident-gate.ps1` checks 130 cases: 44 resident CPU, 4 flat-fill, 2 line, 1 shade-DDA, 1 pattern-state, 3 command-ABI and 3 direct-state tests, 2 HostW walker tests, 1 HostW interpreter/JIT source oracle, 2 resident HostR port/retirement tests, plus 24 resident GPU and 10 @@ -797,11 +793,17 @@ complete 130-test gate. See dithering. - [x] Implement screen-to-screen copies and overlap hazards. - [x] Implement continuation and empty/no-op context behavior. -- [ ] Remove the production CPU framebuffer fallback path. +- [x] Remove the production CPU framebuffer fallback path. Complete when the strict corpus and representative live workloads report zero unsupported or CPU-fallback draws. +Production activation is fail-closed: unsupported commands and device loss +stop the machine rather than executing against CPU framebuffer shadows. The +embedded-PROM workload reports zero unsupported commands on Vulkan and D3D12; +the installed IRIX 6.5 desktop, Showcase and Atlantis acceptance workloads +also report zero unsupported commands on both backends. + The first M3 infrastructure slice is complete in the gated test-only backend. Its Rust and WGSL sides share a fixed 64-byte `RexCommand` and 256-byte normalized state slot. Each bounded reusable batch set permanently binds a @@ -1053,9 +1055,10 @@ through wgpu scanout, resident planes through same-device wgpu scanout and resident readback through the ordinary CPU renderer. Capture tickets are coalesced at the frame cut, newer tickets remain pending, and failures neither publish stale pixels nor advance `frame_seq`. The complete screenshot, -headless frame and loopback VNC cases run in both resident modes. Live -frontend integration remains open; no production selector exposes either -resident mode yet. +headless frame and loopback VNC cases run in both resident modes. The +production selector now exposes both modes with the same fixed-mode and +no-fallback rules; a live direct VNC session captures composed output without +compatibility raw-plane traffic. ### M7 - Snapshot, reset and failure lifecycle @@ -1156,27 +1159,235 @@ three reusable sets and full selected-plane HostR snapshots. The exact 130-case Vulkan+D3D12 gate covers the scheduling change and includes an independent mixed workload with fills, copies, all line families, HostW, HostR, direct presentation and explicit observation. The M8 implementation -checklist is complete within the gated backend. Its milestone acceptance for -normal use remains open until the production path runs representative IRIX -desktop applications and satisfies the end-to-end throughput, CPU-use and -frame-rate criteria below; the embedded PROM capture does not claim that -coverage. - -### M9/M10 - Production activation and frontends - -- [ ] Compile the resident path in normal wgpu builds after its prerequisites - land, and enable Newport GPU raster only through an explicit selection. -- [ ] Share the validated device across raster and direct presentation; fail - explicitly on initialization, presentation or device loss. -- [ ] Offer readback as an explicit slow compatibility mode, never a fallback; - keep `iris-gui` direct mode unavailable until it has a wgpu presenter. -- [ ] Preserve upstream machine-construction, PROM and configuration errors - while wiring GPU construction and lifecycle. -- [ ] Re-run the resident gate, production lifecycle, visible-window and - representative IRIX application tests on the integrated code. - -The candidate stack implements and tests an activation path, but these -checkboxes remain open until the corresponding changes land upstream. +checklist is complete within the gated backend. The production path has now +also passed the representative IRIX desktop/application and end-to-end +throughput, CPU-use and presentation-latency acceptance described below. + +### M9/M10 - Production activation and frontend + +- [x] Compile the resident command, raster, scanout, telemetry and lifecycle + paths in normal `wgpu` builds while retaining test-only fault probes. +- [x] Add explicit `direct` and `readback` Newport modes; default to direct. +- [x] Create one validated GPU and share it across both Newport heads. +- [x] Seed authoritative planes before publishing each board. +- [x] Start resident processor/display workers automatically and idempotently. +- [x] Require a visible direct window to use the same-device wgpu presenter. +- [x] Fail startup instead of falling back to GL, CPU raster or another GPU. +- [x] Wire HostR, snapshots, rollback, reset, screenshots, CI and VNC through + explicit attributed observation boundaries. +- [x] Report mode, backend, adapter, no-fallback policy and telemetry at run + time. +- [x] Expose explicit readback compatibility through both iris-gui capture + renderers while keeping direct mode unavailable there. +- [x] Validate production lifecycle on Vulkan and D3D12. +- [x] Validate an installed IRIX desktop and representative applications on + CPU raster, Vulkan direct and D3D12 direct. + +Production configuration uses `[graphics] raster = "wgpu"` in a build with +the `wgpu` feature. `newport_mode = "direct"` is the default. +`newport_mode = "readback"` is an explicit slow compatibility/validation +selection and never a fallback. Explicit Metal and GL resident backends are +rejected; `auto` must resolve to Vulkan or D3D12. `IRIS_REX3_GPU` cannot be +combined with this mode. + +Live embedded-PROM boots on Vulkan and D3D12 each accepted 7,319 operations +and 204 architectural no-ops with zero unsupported work. Direct offscreen +screenshots and a visible Vulkan same-device window succeeded without a +compatibility-plane observation. Explicit readback succeeded with one +attributed 16 MiB two-plane observation; the frontend grants this mode a +two-second minimum fresh-frame timeout. A production RFB 3.8 client received a +1282 x 1024 direct frame while telemetry showed direct composed captures and +zero compatibility bytes. + +Ignored production regressions construct two Newport heads on both backends, +require `Arc::ptr_eq` for their devices, and verify repeated start leaves both +processor/display pairs alive. Live save, disk restore, in-memory CI rollback, +power-on reset, restart and fresh captures pass on both backends. That matrix +exposed a CI-handler stack overflow in the 1 MiB REX3 context reset; reset now +initializes the context in place and has a default-service-thread regression. +The exact 130-case Vulkan+D3D12 gate and all four supported feature checks pass +after activation. + +Application acceptance used a clean IRIX 6.5 IP22 install with the 24-bit Indy +graphics personality, Xsgi, 4Dwm and toolchest. The same scripted Showcase +rectangle/ellipse workload produced an identical 450 x 450 RGB crop on CPU +raster, Vulkan direct and D3D12 direct: +`b733d604c1a34554578e69fd9874d9ab892a470d54e67428d6a7fc081eec4190`. +Full-screen save/restore captures were byte-identical on each resident +backend, and CI rollback reproduced the same saved frame. + +The animated Atlantis demo then exercised 12-bit RGB spans, shading, Z, +dithering, clipping and double-source draws for a 30-second sample after a +15-second warm-up on an RTX 5070 host with 16 logical CPUs: + +| Engine | REX3 GO/s | Host core equivalents | Throughput vs CPU | Host CPU vs CPU | Resident p95 presentation | +|---|---:|---:|---:|---:|---:| +| CPU raster/JIT | 4,761.5 | 1.673 | baseline | baseline | n/a | +| Vulkan direct | 6,765.6 | 1.574 | +42.1% | -5.9% | <= 4.2 ms | +| D3D12 direct | 5,918.0 | 1.545 | +24.3% | -7.7% | <= 1.1 ms | + +CPU throughput is the `GO TOTAL` delta; resident throughput is the accepted +command delta. Host core equivalents are process CPU seconds divided by wall +seconds. During both resident samples `unsupported=0`, +`no_cpu_fallback=true`, and all raw/composed readback counters remained +unchanged. Screenshots and snapshots were taken outside the timed interval and +remained explicitly attributed. This completes the normal-use acceptance gate +for the supported workload set; the mode remains experimental pending review +and broader deployment rather than an unfinished roadmap item. + +### M11/M12 - IMPACT fidelity and lifecycle + +M11 reconciled the roadmap with the IMPACT implementation already present +rather than replacing tested behavior: + +- [x] Preserve the existing per-window swaps, 4:4:4, 5:5:5, 12:12:12, + depth/stencil, texture, context-eviction and two-RSS coverage. +- [x] Expose the fitted GE11 count: one on Solid/High and two on Maximum, + without aliasing an absent diagnostic port to GE0. +- [x] Decode CONFIG.DestRE and route ordinary register writes and execution + to RE0, RE1 or both on Maximum. +- [x] Keep TE1/TRAM state and DMA broadcast while the model deliberately + shares one texture subsystem between RSS engines. +- [x] Record the raw-RDRAM blocker instead of guessing: public diagnostics + establish PP/RDRAM address fields, but not the physical swizzle needed + to map them onto PixMem. + +M12 makes IMPACT a full snapshot participant on both supported machines: + +- [x] Bump the snapshot schema to 8 and add named content-addressed IMPACT + parts with exact schema-6/schema-7 manifest compatibility decoding. +- [x] Preserve Front/DCB/GE, partial HQ3 parsers, ERAM and active/parked GL + contexts, every RSS, pixel/clip-ID memory and TE1 tables/TRAM. +- [x] Preserve Octane HQ4 control, context protocol, pending/enabled + interrupts, mappings, GE uploads/fingerprints and MicroLAN transactions. +- [x] Use the same parts for Indigo2 disk snapshots and CI/live checkpoints, + and for Octane disk snapshots; remove the Octane IMPACT refusal. +- [x] Decode and validate every part, product identity, GE count and runtime + raster-engine count before changing board state. +- [x] Rebuild runtime-only JIT/GPU links after restore, reseed GPU memory from + the CPU mirrors, and reject a CPU two-RSS snapshot on a one-unit wgpu + configuration. +- [x] Make wgpu observation reasons and byte counts visible, retain dirty + pages after a failed readback, and turn GPU failures into sticky + bus-visible faults instead of stale success or CPU fallback. +- [x] Cover Solid/High/Maximum round trips, invalid-state rollback, a partial + CFIFO packet, reset and post-restore continuation, the Octane machine + lifecycle, and Vulkan/D3D12 mirror reseeding. + +Remaining IMPACT snapshot work is validation breadth rather than missing +lifecycle plumbing: add guest context-eviction, textured-scene and +guest-programmed-resolution restore matrices before submission. + +Local M11/M12 gates on an RTX 5070: + +- default, `gr4-jit`, and `wgpu` library checks passed; +- all 216 default MGRAS tests passed, including dual-RSS, swaps, RGB12/36, + texture/context and snapshot continuation coverage; +- all 27 active snapshot-manifest tests passed (one benchmark ignored), plus + Octane machine round-trip and invalid-state rollback tests; +- the JIT passed all-product snapshot continuation and representative fill, + stipple and textured-triangle interpreter parity sweeps; +- Vulkan and D3D12 passed IMPACT mirror snapshot/reseed and sticky-fault + accounting; Vulkan also passed fill and textured-triangle GPU parity sweeps. + +### M13/M14 - VPro residency and Octane lifecycle + +M13 makes selected VPro wgpu mode GPU-authoritative at every framebuffer +boundary: + +- [x] Reject unsupported draw slots, clears, copies, DMA destinations, image + destinations and 2D destination slots instead of drawing into the CPU + VRAM mirror. +- [x] Keep CPU geometry and fragment preparation only where the resulting + framebuffer effects remain ordered GPU records. +- [x] Share one first-error sticky fault between raster, scanout and bus + access; return `BUS_ERR` after a failure and `NotReady` from display + composition rather than using stale CPU scanout. +- [x] Preserve dirty GPU ranges until readback and device-health checks both + succeed. +- [x] Attribute successful observations and bytes to front-end, DMA-read, + copy-source, snapshot, monitor, state-hash and validation reasons, with + aggregate rejection/fault and wait/readback-latency counters. +- [x] Clear the sticky fault only at power-on reset and reseed the selected + GPU path from reset VRAM. + +The M13 default and wgpu checks pass. Sticky bus/display closure passes in the +CPU build; typed range accounting and injected device-loss dirty retention +pass on Vulkan and D3D12 on an RTX 5070. Existing unrelated atomic +`fetch_update` deprecation warnings remain. + +M14 adds VPro to Octane's content-addressed snapshot lifecycle: + +- [x] Save stopped V6, V8, V10 and V12 boards as exact named `state` and + complete `vram` parts under schema 9, while reading schema 6 through 8 + manifests with an empty VPro list. +- [x] Preserve frontend registers, partial command/image/query and primitive + continuation, geometry and current texture state, host interface, + display FIFO/shared registers, raster line phase and 1-Wire transaction + state without serializing threads, locks, callbacks, GPU objects or + derived caches. +- [x] Reject duplicate, missing and unknown parts, product or memory-size + mismatches, malformed vector/parser state and late 1-Wire decode errors + before live board mutation. +- [x] Synchronize GPU-authoritative VRAM with the typed `Snapshot` observation + and reseed an enabled GPU unit from restored CPU VRAM. +- [x] Restore VPro after Octane's power-on reset and remove only the VPro + refusal; two-processor and virtio-serial refusals remain. + +M14 passes default and wgpu checks; schema 6/7/8 compatibility, all-product +board round trips, partial-parser continuation, exact named-part checks, +invalid-state rollback, product/memory mismatch, reset, atomic 1-Wire load and +Octane disk save/load tests pass. GPU mirror reseeding passes on Vulkan and +D3D12 on an RTX 5070. Existing unrelated atomic `fetch_update` deprecation +warnings remain. + +### M15/M16 - GR2 lifecycle and resident-GPU decision + +M15 makes XZ and Extreme complete lifecycle participants: + +- [x] Bump the snapshot schema to 10 and store exact named `state`, `vram` and + `zbuf` parts while retaining exact schema 6-9 manifest decoding. +- [x] Preserve all board registers, SRAM/microcode/display devices, complete + HQ2 parser and GL continuation, RE3 context, FIN2 waits and unread GEDMA + output without serializing runtime threads, queues, traces, presentation + objects, telemetry or JIT code. +- [x] Drain accepted HQ2 and RE3 work before stop, validate every part before + mutation, and reject malformed state, missing/duplicate/renamed parts, + board presence and XZ/Extreme mismatches. +- [x] Use the same complete representation for disk snapshots, CI rollback and + live debugger checkpoints. Power-on clears the complete board in place + while preserving the configured `gr2-jit` mode. +- [x] Preserve the ten command tokens observed in the checked-in GL traces, + apply evidenced edge-flag line-mode behavior, and avoid guessing + non-default line-width, point-size, smoothing, stipple or polygon-offset + semantics that the captures do not establish. + +M16 adds runtime-only evidence counters and closes the predefined gate: + +- `glprim_lit`: 1,116 spans / 160,740 pixels, 1,114 batchable following + spans, maximum run 816, 11 synchronization boundaries. +- `glprim_litlocal`: the same span distribution and maximum run, with five + synchronization boundaries. +- `glprim_twoside`: 660 spans / 170,400 pixels, 658 batchable following + spans, maximum run 360, three synchronization boundaries. +- `glprim_fog`: 480 spans / 177,600 pixels, all in the `<=1024` bucket, 478 + batchable following spans, maximum run 300, three synchronization boundaries. +- On Windows and a Ryzen 7 9800X3D, the benchmark uses six interleaved + 64-million-pixel samples per engine and reports their median. Across two + runs, interpreter throughput for 4/16/64/400/1280-pixel spans was + 132-133/165-184/185-200/193-210/182-215 Mpx/s; warmed `gr2-jit` was + 178-187/296-333/471-480/510-533/520-568 Mpx/s. The corresponding JIT + speedups were 1.33-1.42x, 1.80-1.81x, 2.35-2.60x, 2.54-2.65x and + 2.64-2.86x. + +Decision: **no-go for a resident RE3 wgpu implementation now; retain +`gr2-jit`**. The trace data establishes batching potential, but there is no +GPU prototype to beat the warm-JIT large-span baseline or prove the <=5% +short-span regression bound. Ordinary presentation still scans all +1,310,720 host pixels per frame, so the no-readback gate fails, and no +repeatable representative guest benefit has been measured. This is an +evidence-gated deferral, not a conclusion that efficient acceleration is +impossible. ## Validation requirements @@ -1236,22 +1447,18 @@ criteria: Do not require high GPU utilization. A lightly used GPU at base clock is an expected result when the emulated guest CPU is the producer bottleneck. -## Open implementation questions +## Follow-on questions -These are engineering choices to settle with prototypes or measurements, not -reasons to change the architecture: +The implementation and acceptance questions settled by M0-M10 are recorded +above. Remaining work is optional frontend or platform expansion: -- how to use the current or future shared offscreen presenter for Newport - while retaining explicit failure and hardware-gate semantics; -- linear versus tile-major framebuffer storage after routine checkpoints are - removed; -- command and state-slot packing; -- active-tile list versus full-screen dispatch; -- batch time limit that balances submission overhead and latency; -- full-plane versus region HOSTR readback after real frequency measurements; -- how `iris-gui` selects or hosts its wgpu renderer; -- carrying the gated fatal/restart policy through the production lifecycle; and -- which backend becomes the first supported default after validation. +- how `iris-gui` selects or hosts a same-device eframe wgpu renderer; +- how broadly to expand the IRIX application and host-adapter matrix before a + default change; +- whether Metal can satisfy the same completion, destruction and lifecycle + gate before it is allowed; and +- whether measurements justify enabling the experimental path by default in a + later release. Geometry offload and native GL/wgpu texture sharing are not open questions. They are out of scope unless new evidence changes the maintainer's decision. @@ -1274,3 +1481,9 @@ REX3 GPU mode is ready to consider for normal use only when: gates; and 8. user documentation states the available raster/compositor combinations, fallback behavior and explicit readback costs. + +As of 2026-11-12, production wiring and evidence satisfy items 1-8 for the +exercised command and application set. The implementation plan is complete. +The mode stays experimental and opt-in while the stack is reviewed and tested +on a broader application/adapter matrix; becoming the default is a separate +release decision. diff --git a/clanker/plans/wgpu-raster.md b/clanker/plans/wgpu-raster.md index fe0af1422..110ee312d 100644 --- a/clanker/plans/wgpu-raster.md +++ b/clanker/plans/wgpu-raster.md @@ -185,7 +185,7 @@ the same inputs into records and slots instead. | monitor `vpro pix/raw/peek/shot/fbdump/hash` | quiesce as now, then readback | | fences (`Board::fence`, `query_finish`, completions on the quad ring tail) | `Gpu::on_done` after the batch that precedes them: guest-RAM token write and softsync, as the closures do today | | replay: `raster_wait` then `memory_hash` | flush + wait, `read_all`, blake3 over the same byte order | - | snapshots | refused for VPro today; unchanged | + | snapshots | stop and drain, read back with the `Snapshot` observation, save named state and VRAM CAS parts, and reseed the GPU on restore | - **Threads.** The raster thread and the pool go away in the GPU instantiation; the front end records and flushes. `RasterQueue`'s @@ -623,17 +623,37 @@ sampler `Te1::sampler` resolves and the polygon stipple rows. Open: per-row edge sharing (eight lanes of a tile row compute the same bounds); the host GL composite still uploads rows the CPU wrote. -## GR2 (XZ / Extreme), open +## GR2 (XZ / Extreme), M16 decision The boundary is `Re3Sink` into `re3_fifo` (one RE3; the GE7 only stores microcode, all geometry is HLE in `hq2.rs`/`gl.rs`): register writes with IR execute (SHADED, FLAT, FLAT4 spans with iterators in 14-bit fractions), the private ops (copy, Z fill, pixel format, Z control, stencil, blend, alpha) and reads after `drain_re3`. Memory is two -1280x1024 u32 planes. Spans are short and a frame has few of them, so -the batch amortisation argument is weak; decide after IMPACT with -numbers from the span counts of a GL scene. The REX3 lesson applies -until then. +1280x1024 u32 planes. + +The November 2026 M16 measurement found real batching potential in the four +checked-in GL traces: 480-1,116 spans and 160,740-177,600 requested pixels per +trace, 478-1,114 consecutive same-pipeline opportunities, maximum runs of +300-816, and only 3-11 synchronization boundaries. Span lengths are not all +short: `glprim_fog` emits 480 spans in the 257-1024 bucket, while the lighting +traces cover every bucket through 1024. + +That evidence does not clear the implementation gate. On Windows and a Ryzen +7 9800X3D, six interleaved 64-million-pixel samples per engine gave these +ranges across two runs for 4, 16, 64, 400 and 1280-pixel spans: the +interpreter measured 132-133, 165-184, 185-200, 193-210 and 182-215 Mpx/s; +warmed `gr2-jit` measured 178-187, 296-333, 471-480, 510-533 and +520-568 Mpx/s. There is no resident GPU prototype to show materially higher +large-span throughput or a short-span regression no worse than 5%. Ordinary +presentation composes by scanning all 1,310,720 host-VRAM pixels, and no +representative guest benefit has been measured. + +**Decision: no-go now; retain `gr2-jit`.** This is an evidence-gated deferral, +not a claim that efficient RE3 acceleration is impossible. Reopen only with a +resident raster-and-scanout prototype that clears all four gates: large-span +throughput above warm JIT, short spans within 5%, zero routine presentation +readback, and repeatable representative guest improvement. ## Measurements to take before each milestone closes diff --git a/clanker/wgpu/gpu-floats-are-not-the-hosts.md b/clanker/wgpu/gpu-floats-are-not-the-hosts.md index 0a2f5cef7..b2941efa9 100644 --- a/clanker/wgpu/gpu-floats-are-not-the-hosts.md +++ b/clanker/wgpu/gpu-floats-are-not-the-hosts.md @@ -1,7 +1,8 @@ # GPU float division is not correctly rounded, and products get fused: guard both before comparing with the CPU model -Status: 2026-11-06, rule, measured on an RX 6600 XT (RADV, Vulkan 1.4) with -the VPro kernels in `src/dev/odyssey/wgpu`. +Status: 2026-11-16, rule, measured on an RX 6600 XT (RADV, Vulkan 1.4) and +an RTX 5070 (Vulkan and D3D12) with the VPro kernels in +`src/dev/odyssey/wgpu`. ## Symptom @@ -19,8 +20,9 @@ Two things the host's f32 arithmetic does that a GPU's need not: neither variant where `rcp` itself was a unit off). - **Contraction.** SPIR-V without `NoContraction` lets the driver fuse `a * b + c` into one rounding, naga emits no such decoration, and RADV - fuses. Removing the guard below brought three of the eight parity tests - down (422 differing words in the random-state test). + fuses. D3D12 also contracted through the original `x * nc` guard even + though `nc` came from a storage record: nine parity tests failed, with + 1,914 differing depth words in the flat-triangle test alone. Add, subtract and multiply are correctly rounded everywhere wgpu runs; those never differed. @@ -32,11 +34,15 @@ those never differed. the host does. Use it wherever a quotient feeds a value the hashes see (the depth ratio, the colour weights' `W_ONE / sum`; later the texture unit's divides by Q and the level of detail). -- Keep a product apart from a sum by multiplying it by a one the compiler - cannot see: each board kernel reads `1.0` from its record (`Rec::one`) - and wraps products with `p(x) = x * nc`. `fma(x, one, c)` rounds like - `x + c`, so it is safe whether the driver fuses or not. Any product that - feeds an add or a subtract, including `1.0 - l1`, needs the wrap. +- Keep a product apart from a sum by making its f32 bits observable through + a one the compiler cannot see. Each board kernel reads `1.0` from its + record (`Rec::one`); `p(x)` bitcasts `x` and XORs it with + `bitcast(nc) ^ 0x3f800000`. The mask is zero at runtime, but the + integer dependency prevents D3D12 and Vulkan compilers from contracting + the operation that produced `x` into the following add. A plain + `p(x) = x * nc` is not a sufficient barrier on D3D12. Any product that + feeds an add or a subtract, including `1.0 - l1`, needs the bit-observable + wrap. - Match the CPU's operation order exactly (the lanes code in `pipe/raster.rs` and `pipe/interp.rs` is the reference) and reproduce Rust's saturating `as i32` (`sat_i32`) and `as f32` of an i64 diff --git a/docs/graphics/gr2-xz-extreme.md b/docs/graphics/gr2-xz-extreme.md index e2f60482e..a6e69e074 100644 --- a/docs/graphics/gr2-xz-extreme.md +++ b/docs/graphics/gr2-xz-extreme.md @@ -50,11 +50,19 @@ Both frontends obtain frames through `Machine::get_display()` and `GfxDisplay`. Graphics do not require HostGL; `hostgl` is a separate optional service for programs using the replacement guest libGL. -## Snapshot limitation +## Snapshots and reset -GR2 save/load covers selected registers, microcode, SRAM, and display tables. -VRAM and full drawing/context state are not saved yet, so snapshots do not -reproduce the complete rendered scene. See [TODO.md](../../clanker/TODO.md). +Snapshot schema 10 stores XZ and Extreme as exact named `state`, `vram` and +`zbuf` content-addressed parts. State includes all board registers, SRAM and +microcode, display devices, complete HQ2 parser/GL continuation, RE3 context, +FIN2 waits and unread GEDMA output. Disk snapshots, CI rollback and live +debugger checkpoints use the same representation. Restore rejects malformed +parts and XZ/Extreme or board-presence mismatches before changing live state. + +Stop drains accepted HQ2 and RE3 work before the workers exit. Power-on clears +the complete architectural board state; it retains only the configured +`gr2-jit` mode and rebuilds runtime threads, queues, traces, presentation +objects, telemetry and JIT memoization. ## Inspect and trace @@ -63,6 +71,7 @@ Use `gr2 help` for the full command list: ```text gr2 status gr2 hq +gr2 metrics re3 regs gr2 trace gr2.log hq,re3,cpu # needs a build with the gputrace feature gr2 trace flush @@ -70,6 +79,13 @@ gr2 trace off gr2 fbdump gr2dump ``` +`gr2 metrics` reports span-size buckets, consecutive spans with the same +pipeline shape, synchronization boundaries, candidate framebuffer/Z traffic, +host reads, presentation scans and, with `gr2-jit`, JIT hits and misses. The +counters are runtime diagnostics and reset on power-on or snapshot restore. +Ordinary presentation currently composes from host VRAM; there is no resident +RE3 GPU raster or scanout path. + Coverage and synchronization rules are in [GR2 design](../../clanker/gr2/DESIGN.md), [bring-up notes](../../clanker/gr2/bringup.md), and the individual notes under diff --git a/docs/graphics/newport.md b/docs/graphics/newport.md index e991fad8e..bba33eadd 100644 --- a/docs/graphics/newport.md +++ b/docs/graphics/newport.md @@ -148,26 +148,36 @@ stays the default. The `wgpu` feature composes the display on the GPU for the window (`Ng1Scanout`, in `wgpu/`, used from `src/ui.rs`) when `renderer = "wgpu"` is set. That path does not change what the processor does. -An independent resident GPU raster and direct scanout exist only in tests; -neither a feature flag nor an environment variable selects them in a running -emulator. Newport rejects `[graphics] raster = "wgpu"` until M3. The tests -seed full linear RGB and auxiliary planes, execute the supported flat fills -through the real GFIFO and use a marker to order same-device composition. -Ordinary test frames do not transfer or access raw CPU planes or fall back to -CPU raster. Explicit selected-plane observation returns a raw snapshot; -screenshots, VNC and headless frame capture instead read the composed output. -Device loss is fatal on this resident path. HOSTR, snapshots and other -board-level raw consumers, full opcode and lifecycle coverage, and a live -integration remain future work. Changed decoded DID metadata can still be -uploaded as a cached 2 MiB map. See [TESTING.md](../../TESTING.md) for the -gate and [HACKING.md](../../HACKING.md) for the ownership rules. +A `wgpu` build can select the production GPU-authoritative backend with +`[graphics] raster = "wgpu"`. Complete linear RGB and auxiliary planes are +seeded into resident buffers before the board is published. DRAW, SCR2SCR, +HostW, ordered HostR synchronization and scanout then use the bounded resident +command stream; ordinary work neither accesses CPU framebuffer shadows nor +falls back to CPU raster. Unsupported work and device loss stop the machine. + +`newport_mode = "direct"` is the default. It requires the standalone window +to use `renderer = "wgpu"` on the same validated Vulkan or D3D12 device. +Screenshots, CI and VNC map requested composed output, but ordinary +presentation performs no readback. Explicit `newport_mode = "readback"` uses +the ordinary compositor after an attributed 16 MiB two-plane observation and +is never selected as a fallback. HostR, snapshots and other raw consumers use +separately attributed observation boundaries. + +The exact dual-backend gate covers the command stream, full-plane ownership, +HostW/HostR, direct scanout and lifecycle behavior. Installed IRIX 6.5 +desktop, Showcase and Atlantis runs additionally pass on Vulkan and D3D12 with +pixels matching CPU raster, zero unsupported work and no fallback. See +[TESTING.md](../../TESTING.md) for the gates and measurements and +[HACKING.md](../../HACKING.md) for the ownership rules. ## Board memory and scan-out -There is no video memory apart from the frame buffers. In normal operation, -`fb_rgb` and `fb_aux` are two `Box<[u32]>` of 2048 x 1024 words. Each bit of -a pixel is one plane. The auxiliary plane carries the overlay, popup and CID -planes (`CompositorSource`, `src/compositor.rs`). +There is no video memory apart from the frame buffers. With CPU raster, +`fb_rgb` and `fb_aux` are two `Box<[u32]>` allocations of 2048 x 1024 words. +Resident mode instead makes equally sized wgpu buffers authoritative; the CPU +allocations remain stale compatibility/snapshot storage until an explicit +observation. Each bit of a pixel is one plane. The auxiliary plane carries the +overlay, popup and CID planes (`CompositorSource`, `src/compositor.rs`). `rex-dirty-upload`, off by default, tracks damage in tiles (`rex3_dirty.rs`), so a frame uploads only the parts that changed. @@ -193,11 +203,14 @@ screenshot or frame is wanted, or a run of frames has gone by with nothing to do (`IDLE_HEARTBEAT_FRAMES`). An idle frame still asserts VBLANK and latches the cursor Y, so guest timing does not depend on the host. -The installed renderer composes the CPU-owned planes and draws the result in -a window with the status bar. Requested screenshots and VNC frames use the -composed `screen.rgba` (`0xAABBGGRR`). With no renderer, as with `--ci`, the -software compositor builds a requested frame instead -(`SwCompositor::compose_pixels`, `src/compositor.rs`). +CPU raster uses the installed renderer to compose the CPU-owned planes and +draw the result with the status bar. Resident direct mode composes on the +board's wgpu device and hands that buffer to the same-device presenter. +Requested screenshots and VNC frames use explicitly composed output; direct +presentation does not update stale CPU planes. Resident readback mode first +observes both complete planes, then uses the ordinary renderer. With no +renderer, as with `--ci`, the software compositor builds a requested CPU frame +for CPU raster (`SwCompositor::compose_pixels`, `src/compositor.rs`). ## Monitor commands diff --git a/docs/octane-ip30.md b/docs/octane-ip30.md index 0bac3000e..7ffb07875 100644 --- a/docs/octane-ip30.md +++ b/docs/octane-ip30.md @@ -277,9 +277,9 @@ through its event channel. `HACKING.md` lists the thread names. - RAD audio input records silence; only the analog output reaches the host. - Board identity records carry made-up barcodes and serial numbers. - Snapshots (the monitor's `save` and `load`, iris-ci's `save`, `restore` and - `rollback`) cover a one-processor Octane without a graphics board; a - two-processor Octane, an IMPACT or VPro board and virtio-serial are - refused with the reason. + `rollback`) cover a one-processor Octane, including IMPACT or VPro + V6/V8/V10/V12 graphics. A two-processor Octane and virtio-serial are refused + with the reason. Restoring also rewrites the configured flash and NVRAM files, and the guest's clocks resume from the time they showed at the save. diff --git a/iris-gui-README.md b/iris-gui-README.md index 41c6b04fe..e009d8ff9 100644 --- a/iris-gui-README.md +++ b/iris-gui-README.md @@ -49,6 +49,7 @@ emulation will be noticeably slow. | `hostgl` | Host OpenGL for IRIX programs (implies `hostcall`); IRIX's replacement libGL replays its GL calls on the host GPU. macOS (CGL) and Linux (EGL; needs libGL to build, libEGL and Mesa's EGL/DRI drivers at run time; see CHANGELOG) — builds elsewhere but registers no backend | | `premiere` | `iris/lightning` + `iris/idle-pause` for maximum in-process speed | | `perfetto` | Perfetto trace recording; the Debug tab gains Start/Stop for a `.pftrace` (see `docs/perfetto.md`) | +| `wgpu` | Experimental GPU-authoritative raster backends in the embedded core. Newport requires explicit `newport_mode = "readback"` because iris-gui has no same-device direct presenter yet | | `bundled` | Distributed build: hides source-checkout tools such as premiere preparation. Machine TOML storage is shared by source, Release, and App Store builds. Set by the Release workflow | | `appstore` | Mac App Store build: implies `bundled`, hides the CI tab, enables security-scoped bookmarks and folder grants | @@ -76,6 +77,12 @@ cargo build -p iris-gui --release --features iris/jitv2 **Help → Diagnostics** lists what's compiled in. +To exercise resident Newport through iris-gui, build with `--features wgpu` +and set `[graphics] raster = "wgpu"` plus `newport_mode = "readback"` in the +machine TOML. Both the default CPU capture renderer and `IRIS_GUI_GL=1` path +accept the synchronized compatibility frames. The default resident direct mode +remains available only in the standalone `iris` frontend. + ### Verify that the default iris build is unaffected ``` diff --git a/iris-gui/Cargo.toml b/iris-gui/Cargo.toml index b08b7fbf7..e373b718d 100644 --- a/iris-gui/Cargo.toml +++ b/iris-gui/Cargo.toml @@ -84,6 +84,8 @@ hostgl = ["hostcall", "iris/hostgl"] perfetto = ["iris/perfetto"] # Opt-in Newport framebuffer damage tracking and partial GL uploads. rex-dirty-upload = ["iris/rex-dirty-upload"] +# Experimental GPU-authoritative raster backends in the embedded core. +wgpu = ["iris/wgpu"] [dependencies] # The GUI always enables the graphics JITs (`gpujit`: REX3 draw shaders and diff --git a/iris-gui/src/framebuffer.rs b/iris-gui/src/framebuffer.rs index 27659a8c2..3309c29d2 100644 --- a/iris-gui/src/framebuffer.rs +++ b/iris-gui/src/framebuffer.rs @@ -270,6 +270,9 @@ impl Renderer for CaptureRenderer { } fn resize(&mut self, _width: usize, _height: usize) {} + + #[cfg(feature = "wgpu")] + fn takes_resident_readback_frames(&self) -> bool { true } } /// GPU compositor path: `GlCompositor` on a hidden GL context (REX3-Refresh thread). @@ -356,6 +359,9 @@ impl Renderer for GlCaptureRenderer { } fn resize(&mut self, _width: usize, _height: usize) {} + + #[cfg(feature = "wgpu")] + fn takes_resident_readback_frames(&self) -> bool { true } } impl Drop for GlCaptureRenderer { @@ -562,6 +568,13 @@ mod tests { r.present(screen, &mut d.overlay, &mut d.status, &mut d.sbtex, &d.stats, false, None, None); } + #[cfg(feature = "wgpu")] + #[test] + fn capture_renderer_accepts_resident_readback_frames() { + let renderer = CaptureRenderer::new(FrameSink::new()); + assert!(renderer.takes_resident_readback_frames()); + } + /// Copy `w`×`h` of a strided picture, as the GUI's texture would end up /// holding after uploading a frame. fn copy_pic(dst: &mut [u32], src: &[u32], w: usize, h: usize) { diff --git a/iris.toml b/iris.toml index 0f4938961..6097a58f6 100644 --- a/iris.toml +++ b/iris.toml @@ -242,6 +242,9 @@ mode = "nat" #board = "newport" #heads = 1 #resolution = "guest" +#raster = "cpu" # "wgpu" needs that feature; Newport is experimental and has no CPU fallback +#newport_mode = "direct" # Newport wgpu only: "direct" or explicit slow "readback" compatibility +#gpu_backend = "auto" # Newport wgpu initially accepts Vulkan or D3D12 # CP0 Count frequency in MHz. Default: half the CPU's clock (75 for the Indy's # 150 MHz R4400), which IRIX reports as the CPU's MHz. Set it only if a guest diff --git a/scripts/test-resident-gate.ps1 b/scripts/test-resident-gate.ps1 index 3dbabf57d..e21f7fed5 100644 --- a/scripts/test-resident-gate.ps1 +++ b/scripts/test-resident-gate.ps1 @@ -15,7 +15,7 @@ $profile = Join-Path 'target' "resident-gate-jit-$([Guid]::NewGuid().ToString('N $env:IRIS_REX_JIT_PROFILE = $profile function Invoke-ResidentTests { - param([string] $Filter, [int] $Expected, [bool] $Ignored) + param([string] $Filter, [int] $Expected, [bool] $Ignored, [string] $Skip = '') $testArgs = @( 'test', '--locked', '-p', 'iris', '--lib', '--features', 'wgpu,rex-jit,rexdiag', @@ -24,6 +24,9 @@ function Invoke-ResidentTests { if ($Ignored) { $testArgs += @('--ignored', '--nocapture') } + if ($Skip) { + $testArgs += @('--skip', $Skip) + } Write-Host "Running $Filter ($Expected tests, backend=$env:REX3_RESIDENT_BACKEND)" # Windows PowerShell treats redirected native stderr as an error record. @@ -66,7 +69,7 @@ function Invoke-ResidentTests { } try { - Invoke-ResidentTests 'resident_' 43 $false + Invoke-ResidentTests 'resident_' 44 $false 'wgpu_snapshot_reseeds_the_resident_mirror' Invoke-ResidentTests 'dev::ng1::rex3_flat_fill::tests' 4 $false Invoke-ResidentTests 'dev::ng1::wgpu::line::tests' 2 $false Invoke-ResidentTests 'dev::ng1::wgpu::host::tests' 2 $false diff --git a/src/config.rs b/src/config.rs index 92a90e665..4374bfaa4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1341,11 +1341,16 @@ pub struct GraphicsSection { /// two from 8 to 256. #[serde(default = "default_graphics_tile")] pub tile: u32, - /// VPro and IMPACT: where the raster unit runs: `cpu` (the threads - /// above; IMPACT's raster engine threads) or `wgpu` (the host GPU - /// through wgpu compute; needs the `wgpu` feature). + /// Where the raster unit runs: `cpu` (the threads above; IMPACT's raster + /// engine threads) or `wgpu` (the host GPU through wgpu compute; needs + /// the `wgpu` feature). #[serde(default)] pub raster: RasterUnit, + /// Newport wgpu raster presentation mode. Unset means `direct`, which + /// shares authoritative GPU planes with the wgpu renderer. `readback` + /// explicitly selects the slower GL/CPU compatibility path. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub newport_mode: Option, /// The wgpu backend for `raster = "wgpu"`: `auto`, `vulkan`, `metal`, /// `dx12` or `gl`. #[serde(default = "default_gpu_backend")] @@ -1375,6 +1380,15 @@ pub enum RasterUnit { Wgpu, } +/// How an authoritative Newport wgpu framebuffer reaches display consumers. +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default, clap::ValueEnum)] +#[serde(rename_all = "lowercase")] +pub enum NewportWgpuMode { + #[default] + Direct, + Readback, +} + impl GraphicsSection { /// Pixel size for host window layout when a preset is selected. pub fn host_display_size(&self) -> (u32, u32) { @@ -1382,6 +1396,10 @@ impl GraphicsSection { .visible_size() .unwrap_or((1280, 1024)) } + + pub fn newport_wgpu_mode(&self) -> NewportWgpuMode { + self.newport_mode.unwrap_or_default() + } } fn default_graphics_heads() -> u8 { 1 } @@ -1398,6 +1416,7 @@ impl Default for GraphicsSection { threads: default_graphics_threads(), tile: default_graphics_tile(), raster: RasterUnit::Cpu, + newport_mode: None, gpu_backend: default_gpu_backend(), flash: None, } @@ -3459,17 +3478,40 @@ impl MachineConfig { self.graphics.board.unwrap_or_else(|| GraphicsBoard::default_for(self.machine.profile)) } - /// Newport's authoritative GPU raster is not selectable until M3. - /// Keep this separate so checked machine construction also enforces it - /// when a caller has not run the full configuration validation. + /// Validate Newport's authoritative GPU mode independently so checked + /// machine construction enforces it even without full config validation. pub(crate) fn validate_newport_raster_gate(&self) -> Result<(), String> { - if self.graphics.raster == RasterUnit::Wgpu - && self.graphics_board() == GraphicsBoard::Newport + let newport = self.graphics_board() == GraphicsBoard::Newport; + if self.graphics.newport_mode.is_some() + && (!newport || self.graphics.raster != RasterUnit::Wgpu) { return Err( - "graphics.raster = \"wgpu\" is not available for Newport yet; \ - the resident backend remains gated until M3. A rex-gpu build may use \ - IRIS_REX3_GPU for the legacy experimental prototype" + "graphics.newport_mode applies only to graphics.board = \"newport\" \ + with graphics.raster = \"wgpu\"" + .into(), + ); + } + if self.graphics.raster != RasterUnit::Wgpu || !newport { + return Ok(()); + } + if !cfg!(feature = "wgpu") { + return Err("graphics.raster = \"wgpu\" needs a build with the wgpu feature".into()); + } + if matches!(self.graphics.gpu_backend.as_str(), "metal" | "gl") { + return Err(format!( + "Newport graphics.raster = \"wgpu\" supports only validated Vulkan and \ + D3D12 devices; graphics.gpu_backend {:?} is not supported", + self.graphics.gpu_backend + )); + } + let visible_window = !self.headless && (!self.ci || self.ci_display); + if visible_window + && self.graphics.newport_wgpu_mode() == NewportWgpuMode::Direct + && self.renderer != UiRenderer::Wgpu + { + return Err( + "Newport direct wgpu raster requires renderer = \"wgpu\"; select \ + graphics.newport_mode = \"readback\" explicitly for GL/CPU compatibility" .into(), ); } @@ -4155,10 +4197,14 @@ pub struct Cli { #[arg(long = "graphics", visible_alias = "board", visible_alias = "graphics-board", value_name = "BOARD")] pub graphics: Option, - /// VPro and IMPACT: where the raster unit runs: `cpu` (default) or `wgpu` (the host GPU through wgpu compute; needs a build with the `wgpu` feature). + /// Where the raster unit runs: `cpu` (default) or `wgpu` (the host GPU through wgpu compute; needs a build with the `wgpu` feature). #[arg(long = "raster", value_name = "UNIT")] pub raster: Option, + /// Newport wgpu presentation: `direct` (default, same-device wgpu) or explicit `readback` compatibility. + #[arg(long = "newport-mode", value_name = "MODE")] + pub newport_mode: Option, + /// What draws the window: `gl` (default) or `wgpu` (needs a build with the `wgpu` feature). #[arg(long = "renderer", value_name = "RENDERER")] pub renderer: Option, @@ -4359,6 +4405,7 @@ impl Cli { if let Some(cache) = self.cache { cfg.machine.cache = cache; } if let Some(b) = self.graphics { cfg.graphics.board = Some(b); } if let Some(r) = self.raster { cfg.graphics.raster = r; } + if let Some(m) = self.newport_mode { cfg.graphics.newport_mode = Some(m); } if let Some(r) = self.renderer { cfg.renderer = r; } if self.scale2x { cfg.scale = 2; } if self.headless { cfg.headless = true; } @@ -4530,31 +4577,56 @@ mod export_tests { } #[test] - fn newport_rejects_the_unimplemented_wgpu_raster_selection() { + fn newport_resident_wgpu_raster_requires_the_feature_and_direct_renderer() { let mut cfg = MachineConfig::default(); cfg.graphics.raster = RasterUnit::Wgpu; let error = cfg.validate().unwrap_err(); - assert!( - error.contains("not available for Newport yet") - && error.contains("until M3") - && error.contains("IRIS_REX3_GPU"), - "{error}" - ); + if cfg!(feature = "wgpu") { + assert!(error.contains("renderer = \"wgpu\""), "{error}"); + } else { + assert!(error.contains("wgpu feature"), "{error}"); + } + cfg.renderer = UiRenderer::Wgpu; + if cfg!(feature = "wgpu") { + cfg.validate().expect("direct Newport wgpu selection"); + } else { + assert!(cfg.validate().unwrap_err().contains("wgpu feature")); + } } #[test] - fn newport_resident_raster_has_no_toml_or_cli_selector() { - assert_eq!( - ::value_variants(), - &[RasterUnit::Cpu, RasterUnit::Wgpu] - ); - assert!(toml::from_str::("[graphics]\nraster = \"resident\"\n").is_err()); - assert!(Cli::try_parse_from(["iris", "--raster", "resident"]).is_err()); + fn newport_readback_mode_is_explicit_in_toml_and_cli() { + let toml_cfg: MachineConfig = toml::from_str( + "[graphics]\nraster = \"wgpu\"\nnewport_mode = \"readback\"\n", + ).unwrap(); + assert_eq!(toml_cfg.graphics.newport_wgpu_mode(), NewportWgpuMode::Readback); + if cfg!(feature = "wgpu") { + toml_cfg.validate().expect("explicit readback compatibility"); + } else { + assert!(toml_cfg.validate().unwrap_err().contains("wgpu feature")); + } + + let cli = Cli::try_parse_from([ + "iris", "--raster", "wgpu", "--newport-mode", "direct", "--renderer", "wgpu", + ]).unwrap(); + let cfg = cli.apply(MachineConfig::default()); + assert_eq!(cfg.graphics.newport_wgpu_mode(), NewportWgpuMode::Direct); + if cfg!(feature = "wgpu") { + cfg.validate().expect("direct CLI selection"); + } else { + assert!(cfg.validate().unwrap_err().contains("wgpu feature")); + } + } - let toml_cfg: MachineConfig = toml::from_str("[graphics]\nraster = \"wgpu\"\n").unwrap(); - assert!(toml_cfg.validate().unwrap_err().contains("until M3")); - let cli = Cli::try_parse_from(["iris", "--raster", "wgpu"]).unwrap(); - assert!(cli.apply(MachineConfig::default()).validate().unwrap_err().contains("until M3")); + #[test] + fn newport_mode_is_rejected_when_it_cannot_apply() { + let mut cfg = MachineConfig::default(); + cfg.graphics.newport_mode = Some(NewportWgpuMode::Readback); + let error = cfg.validate().unwrap_err(); + assert!(error.contains("applies only"), "{error}"); + cfg.graphics.raster = RasterUnit::Wgpu; + cfg.graphics.board = Some(GraphicsBoard::SolidImpact); + assert!(cfg.validate().unwrap_err().contains("applies only")); } #[test] @@ -4568,11 +4640,31 @@ mod export_tests { cfg.graphics.board = Some(GraphicsBoard::V10); assert!(cfg.validate_newport_raster_gate().is_ok()); cfg.graphics.board = Some(GraphicsBoard::Newport); - assert!(cfg.validate_newport_raster_gate().is_err()); + cfg.renderer = UiRenderer::Wgpu; + assert_eq!( + cfg.validate_newport_raster_gate().is_ok(), + cfg!(feature = "wgpu") + ); cfg.graphics.raster = RasterUnit::Cpu; assert!(cfg.validate_newport_raster_gate().is_ok()); } + #[test] + fn newport_wgpu_rejects_unvalidated_backends() { + let mut cfg = MachineConfig::default(); + cfg.graphics.raster = RasterUnit::Wgpu; + cfg.renderer = UiRenderer::Wgpu; + for backend in ["metal", "gl"] { + cfg.graphics.gpu_backend = backend.into(); + let error = cfg.validate_newport_raster_gate().unwrap_err(); + if cfg!(feature = "wgpu") { + assert!(error.contains("only validated Vulkan and D3D12"), "{error}"); + } else { + assert!(error.contains("wgpu feature"), "{error}"); + } + } + } + /// IDs 1 to 15 are valid on the Octane's wide buses, and 1 to 7 on the /// narrow Indy; the controller's own ID 0 is refused everywhere. #[test] diff --git a/src/dev/gr2/debug.rs b/src/dev/gr2/debug.rs index 5d88e9e36..2a7601a0d 100644 --- a/src/dev/gr2/debug.rs +++ b/src/dev/gr2/debug.rs @@ -170,6 +170,7 @@ impl Gr2 { t.line(format_args!("RE3 #{seq:06} op COPYRECT dst=({}, {}) [GL y, bottom-up]", f(0), f(16))); } re3::RE3_OP_READ_ADVANCE => t.line(format_args!("RE3 #{seq:06} op READBUF advance")), + re3::RE3_OP_SYNC_BOUNDARY => t.line(format_args!("RE3 #{seq:06} op synchronization boundary")), re3::RE3_OP_PIXFMT => t.line(format_args!("RE3 #{seq:06} op PIXFMT {}", match val { 1 => "RGB12", 2 => "CI12", 3 => "CI8", 4 => "RGB8", 5 => "RGB4", _ => "native" })), re3::RE3_OP_ZCTL => t.line(format_args!("RE3 #{seq:06} op ZCTL test={} func={} zmask={:#08x}", val & 1, (val >> 1) & 7, (val >> 8) & 0xff_ffff)), re3::RE3_OP_STENCIL => t.line(format_args!("RE3 #{seq:06} op STENCIL on={} func={} ref={} mask={:#x} wmask={:#x} ops fail={} zfail={} zpass={}", @@ -350,6 +351,21 @@ impl Gr2 { re3::FB_H - 1 - y, p >> 28, (p >> 24) & 0xf, p & 0xffffff, xm.mode[did])?; } "trace" => return self.cmd_trace(&args[1..], w), + "metrics" => { + let m = self.telemetry_snapshot(); + let r = m.raster; + writeln!(w, "GR2 telemetry: {} spans, {} pixels, buckets <=4/8/16/32/64/256/1024/>1024 = {:?}", + r.spans, r.pixels, r.span_buckets)?; + writeln!(w, " batching: {} following spans share a pipeline, max run {}; {} synchronization boundaries", + r.batchable_spans, r.max_batch, r.sync_boundaries)?; + writeln!(w, " candidate traffic: framebuffer {} reads / {} writes, Z {} reads / {} writes; host readback {} pixels", + r.framebuffer_read_words, r.framebuffer_write_words, r.z_read_words, r.z_write_words, r.host_read_pixels)?; + writeln!(w, " presentation: {} frames scanned {} host pixels", m.presentation_frames, m.presentation_host_read_pixels)?; + #[cfg(feature = "gr2-jit")] + writeln!(w, " warm JIT: {} shader spans, {} interpreter spans", m.jit_hits, m.jit_misses)?; + let unknown = unsafe { (&*self.hq_engine.get()).unimplemented_commands() }; + writeln!(w, " unimplemented commands observed: {unknown:#x?}")?; + } #[cfg(feature = "gr2-jit")] "jit" => return self.cmd_jit(&args[1..], w), "fbdump" => { @@ -372,6 +388,7 @@ impl Gr2 { writeln!(w, "gr2 clut [start] [n] [ch] CLUT entries")?; writeln!(w, "gr2 dac Bt457 state")?; writeln!(w, "gr2 pix pixel at display (x, y), through the XMAP")?; + writeln!(w, "gr2 metrics bounded span/batch/traffic/presentation evidence")?; writeln!(w, "gr2 fbdump [dir] VRAM planes + composed screen to PNG/raw files")?; writeln!(w, "gr2 trace [hq,re3,cpu|all] capture annotated traffic; gr2 trace flush|off")?; writeln!(w, "re3 [regs] | re3 pix [w] [h]")?; diff --git a/src/dev/gr2/gl.rs b/src/dev/gr2/gl.rs index dcb87f566..75e6efed5 100644 --- a/src/dev/gr2/gl.rs +++ b/src/dev/gr2/gl.rs @@ -73,6 +73,23 @@ pub const T_SWAP_BUFFERS: u32 = 0x1e7; /// glEnable/glDisable(GL_DITHER) (dither_enable, 0x2044); libglcore also /// turns it off around clears with an integral colour. pub const T_DITHER: u32 = 0x011; +/// Rasterization controls emitted as fixed-layout GE state. The captured +/// libglcore streams use the default width/size, disabled smoothing, an +/// all-ones stipple, and zero polygon offset. Preserve the complete packets; +/// the current RE3 path is one-pixel aliased and those observed values do +/// not alter its output. +pub const T_LINE_STIPPLE_PATTERN: u32 = 0x016; +pub const T_LINE_WIDTH: u32 = 0x017; +pub const T_LINE_STIPPLE: u32 = 0x018; +pub const T_POINT_SIZE: u32 = 0x020; +pub const T_POINT_SMOOTH: u32 = 0x021; +pub const T_LINE_SMOOTH: u32 = 0x022; +pub const T_POLYGON_OFFSET: u32 = 0x0e4; +pub const T_CONTEXT_FLUSH: u32 = 0x0e6; +pub const T_EDGE_FLAG: u32 = 0x0f6; +/// Ten-word libglcore setup packet seen before material/light uploads. Its +/// hardware interpretation is not documented in the available headers. +pub const T_STATE_106: u32 = 0x106; /// glPolygonStipple + enable (__glExpPassPolygonStipple, 0x207C): 1; DATA /// 32 words in the layout __glExpConvertStipple builds from the GL mask /// (row 0 = bottom = window y mod 32, MSB = leftmost = window x mod 32): @@ -435,6 +452,8 @@ pub struct GlState { vhead: u32, pn: u32, pcont: u32, + /// Edge flag captured with each vertex for polygon line mode. + vb_edge: [u32; VB], /// Character position (cmov): window x, y; 0 valid, 1 clipped. cpos: [i32; 3], /// Colour latched by cmov for the characters drawn there. @@ -470,6 +489,16 @@ pub struct GlState { /// and the aux plane write mask (0x003). aux_draw: u32, aux_wmask: u32, + line_width: [u32; 2], + line_stipple_pattern: [u32; 2], + polygon_offset: [u32; 2], + state_106: [u32; 10], + line_stipple: u32, + point_size: u32, + point_smooth: u32, + line_smooth: u32, + edge_flag: u32, + raster_pad: u32, /// Spare (keeps the count of 4-byte fields even, see st_bits). pad: u32, /// Vertices drawn / primitives emitted since the last trace note. @@ -477,6 +506,20 @@ pub struct GlState { } impl GlState { + pub(super) fn validate_snapshot(&self) -> Result<(), String> { + if self.port_n as usize > self.port_buf.len() { + return Err(format!("GL streaming port holds {} words (maximum {})", self.port_n, self.port_buf.len())); + } + let valid_vertex = |v: u32| v == u32::MAX || (v as usize) < RING; + if !self.pv.into_iter().all(valid_vertex) || !valid_vertex(self.vfirst) { + return Err("GL primitive assembly contains an invalid vertex index".into()); + } + if self.pix_pack > 2 { + return Err(format!("GL pixel packing mode {} is invalid", self.pix_pack)); + } + Ok(()) + } + /// Range of the R channel in GL colour units (see to_fixed). /// UAUXDATA for colour index `idx` in overlay / popup drawing: 4-bit /// overlay takes it as is; the 2-bit planes take it shifted up when the @@ -512,6 +555,7 @@ impl GlState { self.normal = [0.0, 0.0, 1.0]; self.front_ccw = 1; self.polymode = 1; + self.edge_flag = 1; self.scissor = [0, 0, 0x7ff, 0x7ff]; self.win = [0, 0, re3::FB_W as i32, SCREEN_H]; self.vp = Viewport { x0: 0.0, x1: re3::FB_W as f32 - 1.0, y0: 0.0, y1: SCREEN_H as f32 - 1.0, zscale: 1.0, zcenter: 0.0 }; @@ -725,6 +769,7 @@ fn port_words(index: u32) -> Option { T_NORMAL | T_IRIS_NORMAL => Some(3), T_COLOR_WRITEMASK | T_READ_BUFFER => Some(2), T_BLEND_FACTOR => Some(3), + T_LINE_WIDTH | T_LINE_STIPPLE_PATTERN | T_POLYGON_OFFSET => Some(2), t => light::port_words(t), } } @@ -781,6 +826,18 @@ pub struct GlCx { pub(super) restore_reject: u32, } +impl GlCx { + pub(super) fn validate_snapshot(&self) -> Result<(), String> { + if self.got > self.expect { + return Err(format!("GL context restore received {} of {} words", self.got, self.expect)); + } + if self.restore_reject > 3 { + return Err(format!("GL context restore rejection code {} is invalid", self.restore_reject)); + } + Ok(()) + } +} + impl Hq2Engine { /// 0x1E1 save main: the image the kernel now reads from HQ2_GEDMA. pub(super) fn gl_cx_save(&mut self, out: &mut dyn Re3Sink) { @@ -990,7 +1047,9 @@ impl Hq2Engine { | T_POLYGON_MODE | T_SWAP_BUFFERS | T_DITHER | T_STIPPLE_OFF | T_DEPTH_TEST | T_DEPTH_FUNC | T_DEPTH_MASK | T_STENCIL_WMASK | T_STENCIL_CONFIG | T_BLEND_MODE | T_LOGIC_OP | T_GET_COLOR | T_GET_NORMAL | T_GET_RASTERPOS | T_READ_DONE - | T_READ_SETUP_A | T_READ_SETUP_B | T_READ_MODE => 1, + | T_READ_SETUP_A | T_READ_SETUP_B | T_READ_MODE | T_LINE_STIPPLE | T_POINT_SIZE + | T_POINT_SMOOTH | T_LINE_SMOOTH | T_CONTEXT_FLUSH | T_EDGE_FLAG => 1, + T_STATE_106 => 10, T_FRAGMENT => 7, T_STENCIL_CLEAR | T_IRIS_BUFFER | T_IRIS_DB => 2, T_IRIS_WRITEMASK | T_IRIS_CLEAR | T_IRIS_ZCLEAR | T_IRIS_BGNPOLYGON | T_IRIS_ENDPOLYGON @@ -1184,6 +1243,9 @@ impl Hq2Engine { d(format!("GL_BLEND on={} src={} dst={}", g.blend_on, g.blend_src, g.blend_dst)); } } + T_LINE_WIDTH => g.line_width = [b[0], b[1]], + T_LINE_STIPPLE_PATTERN => g.line_stipple_pattern = [b[0], b[1]], + T_POLYGON_OFFSET => g.polygon_offset = [b[0], b[1]], T_NORMAL => g.normal = [f(b[0]), f(b[1]), f(b[2])], T_IRIS_NORMAL => { g.normal = [f(b[0]), f(b[1]), f(b[2])]; @@ -1286,6 +1348,13 @@ impl Hq2Engine { T_POLYGON_MODE => g.polymode = a[0], T_SWAP_BUFFERS => g.swap = a[0] & 1, T_DITHER => g.dither = a[0] & 1, + T_LINE_STIPPLE => g.line_stipple = a[0] & 1, + T_POINT_SIZE => g.point_size = a[0], + T_POINT_SMOOTH => g.point_smooth = a[0] & 1, + T_LINE_SMOOTH => g.line_smooth = a[0] & 1, + T_EDGE_FLAG => g.edge_flag = a[0] & 1, + T_STATE_106 => g.state_106.copy_from_slice(&a[..10]), + T_CONTEXT_FLUSH => {} T_STIPPLE_ON => { g.stipple_on = 1; let w = &a[1..33]; @@ -1553,6 +1622,13 @@ impl Hq2Engine { T_CULL_FRONT | T_CULL_BACK => format!("GL_CULL front={} back={}", g.cull_front, g.cull_back), T_POLYGON_MODE => format!("GL_POLYGON_MODE {}", match g.polymode { 1 => "fill", 2 => "point", 3 => "line", _ => "?" }), T_DITHER => format!("GL_DITHER {}", if g.dither != 0 { "on" } else { "off" }), + T_LINE_STIPPLE => format!("GL_LINE_STIPPLE {}", g.line_stipple), + T_POINT_SIZE => format!("GL_POINT_SIZE packet {:#x}", g.point_size), + T_POINT_SMOOTH => format!("GL_POINT_SMOOTH {}", g.point_smooth), + T_LINE_SMOOTH => format!("GL_LINE_SMOOTH {}", g.line_smooth), + T_EDGE_FLAG => format!("GL_EDGE_FLAG {}", g.edge_flag), + T_CONTEXT_FLUSH => "GL_CONTEXT_FLUSH".to_string(), + T_STATE_106 => format!("GL_STATE_106 {:08x?}", g.state_106), T_STIPPLE_ON => format!("GL_POLYGON_STIPPLE on rows[0..4] {:08x} {:08x} {:08x} {:08x}", g.stipple[0], g.stipple[1], g.stipple[2], g.stipple[3]), T_STIPPLE_OFF => "GL_POLYGON_STIPPLE off".to_string(), @@ -1627,6 +1703,12 @@ impl Hq2Engine { } /// Primitive assembly for one transformed vertex. + fn gl_edge_mask(&self, idx: &[u8]) -> u64 { + idx.iter().enumerate().fold(0, |mask, (bit, &vertex)| { + mask | (u64::from(self.gl.vb_edge[vertex as usize] != 0) << bit) + }) + } + fn gl_vertex(&mut self, v: Wv, out: &mut dyn Re3Sink, done: &mut Option<&mut dyn FnMut(String)>) { let polygon = matches!(self.gl.vroutine, VR_POLYGON | VR_IRIS_POLYGON); if polygon { @@ -1637,7 +1719,9 @@ impl Hq2Engine { self.gl_poly_flush(false, out, done); } let g = &mut self.gl; - g.vb[g.pn as usize] = v; + let slot = g.pn as usize; + g.vb[slot] = v; + g.vb_edge[slot] = g.edge_flag; g.pn += 1; g.nv += 1; return; @@ -1647,6 +1731,7 @@ impl Hq2Engine { g.nv += 1; let s = g.vb_alloc(); g.vb[s] = v; + g.vb_edge[s] = g.edge_flag; if n == 0 { g.vfirst = s as u32; } @@ -1657,19 +1742,31 @@ impl Hq2Engine { VR_POINTS => self.gl_point(v, v.c, out), VR_LINES => if n % 2 == 1 { self.gl_line(self.gl.vb[p0 as usize], v, v.c, out) }, VR_LSTRIP | VR_LLOOP => if n >= 1 { self.gl_line(self.gl.vb[p0 as usize], v, v.c, out) }, - VR_TRIANGLES => if n % 3 == 2 { self.gl_poly(&[p1, p0, s], s, ALL_EDGES, out, done) }, + VR_TRIANGLES => if n % 3 == 2 { + let idx = [p1, p0, s]; + self.gl_poly(&idx, s, self.gl_edge_mask(&idx), out, done) + }, VR_TSTRIP => if n >= 2 { // (older, newer, v); every other triangle reversed to keep // one winding. A swaptmesh also flips it, so fans built with // swaps keep one winding too. let t = if g.tflip == 0 { [p1, p0, s] } else { [p0, p1, s] }; g.tflip ^= 1; - self.gl_poly(&t, s, ALL_EDGES, out, done) + self.gl_poly(&t, s, self.gl_edge_mask(&t), out, done) + }, + VR_TFAN => if n >= 2 { + let idx = [first, p0, s]; + self.gl_poly(&idx, s, self.gl_edge_mask(&idx), out, done) + }, + VR_QUADS => if n % 4 == 3 { + let idx = [p2, p1, p0, s]; + self.gl_poly(&idx, s, self.gl_edge_mask(&idx), out, done) }, - VR_TFAN => if n >= 2 { self.gl_poly(&[first, p0, s], s, ALL_EDGES, out, done) }, - VR_QUADS => if n % 4 == 3 { self.gl_poly(&[p2, p1, p0, s], s, ALL_EDGES, out, done) }, // Quad (v0, v1, v3, v2) of the pair (p2, p1) + (p0, v). - VR_QSTRIP => if n >= 3 && n % 2 == 1 { self.gl_poly(&[p2, p1, s, p0], s, ALL_EDGES, out, done) }, + VR_QSTRIP => if n >= 3 && n % 2 == 1 { + let idx = [p2, p1, s, p0]; + self.gl_poly(&idx, s, self.gl_edge_mask(&idx), out, done) + }, _ => {} } } @@ -1683,7 +1780,9 @@ impl Hq2Engine { return; } let n = g.pn as usize; - let mut edges = if n >= 64 { u64::MAX } else { (1u64 << n) - 1 }; + let mut edges = (0..n).fold(0, |mask, bit| { + mask | (u64::from(g.vb_edge[bit] != 0) << bit) + }); if !last { edges &= !(1u64 << (n - 1)); } @@ -1698,6 +1797,7 @@ impl Hq2Engine { if !last { let g = &mut self.gl; g.vb[1] = g.vb[n - 1]; + g.vb_edge[1] = g.vb_edge[n - 1]; g.pn = 2; g.pcont = 1; } diff --git a/src/dev/gr2/gr2disp.rs b/src/dev/gr2/gr2disp.rs index 9cb42c8cd..bd4f3af8d 100644 --- a/src/dev/gr2/gr2disp.rs +++ b/src/dev/gr2/gr2disp.rs @@ -50,6 +50,9 @@ impl Head for Gr2 { fn capture(&self, screen: &mut Rex3Screen, _want: Want) -> bool { let r = self.regs(); gr2comp::compose(self.vram(), &r.vc1, &r.xmap, &r.dac, &mut screen.fb_rgb); + self.presentation_frames.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + self.presentation_host_read_pixels + .fetch_add((FB_W * FB_H) as u64, std::sync::atomic::Ordering::Relaxed); screen.prebuilt = true; screen.width = FB_W; screen.height = FB_H; @@ -64,3 +67,24 @@ impl Head for Gr2 { self.hq_fifo.len() } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::dev::gr2::re3_tests::live_gr2; + use crate::dev::gr2::Gr2Variant; + + #[test] + fn ordinary_presentation_scans_the_complete_host_framebuffer() { + let g = live_gr2(Gr2Variant::Xz); + let mut screen = Rex3Screen::new(); + let want = Want { readback: false, gpu: true }; + + assert!(Head::capture(&*g, &mut screen, want)); + assert!(matches!(Head::compose(&*g, &mut screen, want), Composed::Frame)); + + let telemetry = g.telemetry_snapshot(); + assert_eq!(telemetry.presentation_frames, 1); + assert_eq!(telemetry.presentation_host_read_pixels, (FB_W * FB_H) as u64); + } +} diff --git a/src/dev/gr2/hq2.rs b/src/dev/gr2/hq2.rs index 86e3ecbf8..496ec0ca9 100644 --- a/src/dev/gr2/hq2.rs +++ b/src/dev/gr2/hq2.rs @@ -433,7 +433,8 @@ pub fn gl_token_name(tok: u32) -> Option<&'static str> { 0x0e9 => "GL_GET_COLOR", 0x0ea => "GL_GET_NORMAL", 0x107 => "GL_GET_RASTERPOS", 0x105 => "GL_RASTER_POS", 0x18c..=0x18e => "GL_BITMAP", 0x0fe => "GL_INDEX", 0x010 => "GL_STENCIL_WMASK", 0x011 => "GL_DITHER", 0x013 => "GL_SHADE_MODEL", - 0x017 => "GL_LINE_WIDTH", 0x018 => "GL_LINE_STIPPLE", 0x01b => "GL_CULL_A", 0x01c => "GL_CULL_B", + 0x016 => "GL_LINE_STIPPLE_PATTERN", 0x017 => "GL_LINE_WIDTH", 0x018 => "GL_LINE_STIPPLE", + 0x01b => "GL_CULL_A", 0x01c => "GL_CULL_B", 0x020 => "GL_POINT_SIZE", 0x021 => "GL_POINT_SMOOTH", 0x022 => "GL_LINE_SMOOTH", 0x025 => "GL_BLEND_FACTOR", 0x028 => "GL_FOG", 0x037 => "GL_MODELVIEW", 0x038 => "GL_PROJECTION", 0x039 => "GL_NORMAL_MATRIX", 0x03a => "GL_TEXTURE_MATRIX", 0x03b => "GL_VIEWPORT", 0x03c => "GL_SCISSOR", @@ -448,7 +449,7 @@ pub fn gl_token_name(tok: u32) -> Option<&'static str> { 0x0f0 => "GL_BEGIN_TRIANGLES", 0x0f1 => "GL_END_TRIANGLES", 0x0f2 => "GL_VTX_TRIANGLES", 0x0f3 => "GL_BEGIN_QUADS", 0x0f4 => "GL_END_QUADS", 0x0f5 => "GL_VTX_QUADS", 0x0f6 => "GL_EDGE_FLAG", 0x0f7 => "GL_BEGIN_POLYGON", 0x0f8 => "GL_VTX_POLYGON", - 0x0fc | 0x0fd => "GL_END_POLYGON", 0x104 => "GL_CLEAR_COLOR", 0x108 => "GL_FRONT_FACE", + 0x0fc | 0x0fd => "GL_END_POLYGON", 0x104 => "GL_CLEAR_COLOR", 0x106 => "GL_STATE_106", 0x108 => "GL_FRONT_FACE", 0x10a => "GL_READ_BUFFER", 0x10b => "GL_COLOR_WRITEMASK", 0x10d => "GL_NORMAL", 0x10e => "GL_LIGHTPATH", 0x11d => "GL_BEGIN_POINTS", 0x123 => "GL_VTX_POINTS", 0x168 => "GL_END_POINTS", 0x17c => "GL_BEGIN_LINES", 0x182 => "COLOR", @@ -647,6 +648,58 @@ pub struct Hq2Engine { } impl Hq2Engine { + pub(super) fn scrub_snapshot_image(bytes: &mut [u8]) { + let start = std::mem::offset_of!(Hq2Engine, warned); + let len = std::mem::size_of::<[u64; FIFO_WORDS / 64]>(); + if let Some(warned) = bytes.get_mut(start..start + len) { + warned.fill(0); + } + } + + const fn zero_warned() -> [u64; FIFO_WORDS / 64] { + [0; FIFO_WORDS / 64] + } + + pub(super) fn clear_runtime_diagnostics(&mut self) { + self.warned = Self::zero_warned(); + } + + pub(super) fn unimplemented_commands(&self) -> Vec { + self.warned + .iter() + .enumerate() + .flat_map(|(word, &bits)| { + (0..64).filter_map(move |bit| { + (bits & (1u64 << bit) != 0).then_some((word * 64 + bit) as u32) + }) + }) + .collect() + } + + pub(super) fn validate_snapshot(&self) -> Result<(), String> { + if self.need != 0 && self.need != OPEN && self.need != STREAM && self.need as usize > MAX_ARGS { + return Err(format!("HQ2 command needs {} arguments (maximum {MAX_ARGS})", self.need)); + } + if self.need != OPEN && self.need != STREAM && self.nargs > self.need { + return Err(format!("HQ2 command has {} of {} arguments", self.nargs, self.need)); + } + if self.need != OPEN && self.nargs as usize > MAX_ARGS { + return Err(format!("HQ2 command retains {} arguments (maximum {MAX_ARGS})", self.nargs)); + } + if self.last_nargs as usize > MAX_ARGS { + return Err(format!("HQ2 last command retained {} arguments (maximum {MAX_ARGS})", self.last_nargs)); + } + if self.line_have_prev > 1 { + return Err(format!("HQ2 line continuation flag is {}", self.line_have_prev)); + } + if self.pix_n as usize > PIX_WORDS { + return Err(format!("HQ2 pixel chunk contains {} words (maximum {PIX_WORDS})", self.pix_n)); + } + self.gl.validate_snapshot()?; + self.gl_ctx.validate_snapshot()?; + Ok(()) + } + /// Total words (command word + PUC_DATA words) each command consumes. fn arg_count(cmd: u32) -> u32 { match cmd { diff --git a/src/dev/gr2/hq2_tests.rs b/src/dev/gr2/hq2_tests.rs index 1653c9406..c3540290d 100644 --- a/src/dev/gr2/hq2_tests.rs +++ b/src/dev/gr2/hq2_tests.rs @@ -1009,6 +1009,32 @@ fn gl_polygon_mode_line_outlines_quads() { assert_eq!(gl_px(g, 200, 150), 0, "interior not filled, no diagonal"); } +#[test] +fn gl_edge_flag_suppresses_polygon_mode_edges() { + let g = &live_gr2(Gr2Variant::Xz); + gl_setup_window(g); + let fl = |v: f32| v.to_bits(); + cmd(g, 0x0e2, 3); + cmd(g, 0x0f3, 0); + cmd(g, 0x4f5, 0); + for (i, v) in [[80.0f32, 60.], [320., 60.], [320., 240.], [80., 240.]].into_iter().enumerate() { + cmd(g, 0x0f6, u32::from(i != 0)); + for x in [1.0f32, 1.0, 0.0] { + cmd(g, 0x1982, fl(x)); + } + for x in v { + cmd(g, 0x1263, fl(x)); + } + } + cmd(g, 0x0f4, 0); + cmd(g, 0x465, 0); + g.wait_idle(); + assert_eq!(gl_px(g, 200, 60), 0, "disabled bottom edge"); + assert_eq!(gl_px(g, 320, 150), 0xffff, "right edge"); + assert_eq!(gl_px(g, 200, 240), 0xffff, "top edge"); + assert_eq!(gl_px(g, 80, 150), 0xffff, "left edge"); +} + /// 12-bit RGB double-buffered visual: MakeCurrent mode 2, write masks per /// swap state; pixels are 4:4:4 (R low) in the selected 12-bit buffer. #[test] @@ -1579,6 +1605,12 @@ fn replay_trace(g: &Gr2, text: &str) { g.wait_idle(); } +fn assert_trace_is_supported(g: &Gr2, name: &str) { + let unsupported = g.unimplemented_commands(); + assert!(unsupported.is_empty(), "{name} used unimplemented HQ2 commands: {unsupported:#x?}"); + eprintln!("{name}: {:?}", g.telemetry_snapshot()); +} + /// Screen pixel of a GL window pixel, given the window origin the trace set. fn win_px(g: &Gr2, origin: (usize, usize), x: usize, y: usize) -> u32 { g.vram()[(origin.1 + y) * FB_W + origin.0 + x] & 0xff_ffff @@ -1600,6 +1632,7 @@ fn assert_rgb_near(got: u32, want: u32, tol: u32, what: &str) { fn gl_lighting_directional_from_trace() { let g = &live_gr2(Gr2Variant::Xz); replay_trace(g, include_str!("testdata/glprim_lit.trace")); + assert_trace_is_supported(g, "glprim_lit"); let origin = (32, 692); // Colours are stored R in the low byte: (0.94, 0.53, 0.55) = 0x8d88f0. assert_rgb_near(win_px(g, origin, 200, 150), 0x8d88f0, 4, "centre vertex"); @@ -1615,6 +1648,7 @@ fn gl_lighting_directional_from_trace() { fn gl_lighting_local_and_spot_from_trace() { let g = &live_gr2(Gr2Variant::Xz); replay_trace(g, include_str!("testdata/glprim_litlocal.trace")); + assert_trace_is_supported(g, "glprim_litlocal"); let origin = (0x40, 0x294); assert_rgb_near(win_px(g, origin, 200, 150), 0x4f417d, 6, "centre vertex (no spot)"); // Rim vertex (320, 150), normal (0.866, 0, 0.5): light 0 is behind it @@ -1632,6 +1666,7 @@ fn gl_lighting_local_and_spot_from_trace() { fn gl_lighting_two_sided_from_trace() { let g = &live_gr2(Gr2Variant::Xz); replay_trace(g, include_str!("testdata/glprim_twoside.trace")); + assert_trace_is_supported(g, "glprim_twoside"); let origin = (0x60, 0x274); assert_rgb_near(win_px(g, origin, 110, 150), 0x2424f0, 3, "front face"); assert_rgb_near(win_px(g, origin, 290, 150), 0x0a0a0a, 3, "back face"); @@ -1643,6 +1678,7 @@ fn gl_lighting_two_sided_from_trace() { fn gl_fog_linear_from_trace() { let g = &live_gr2(Gr2Variant::Xz); replay_trace(g, include_str!("testdata/glprim_fog.trace")); + assert_trace_is_supported(g, "glprim_fog"); let origin = (0x80, 0x254); assert_rgb_near(win_px(g, origin, 41, 150), 0xffffff, 4, "no fog at the left"); assert_rgb_near(win_px(g, origin, 358, 150), 0x00ff00, 4, "full fog at the right"); diff --git a/src/dev/gr2/mod.rs b/src/dev/gr2/mod.rs index 78b41f728..28b8ad9c2 100644 --- a/src/dev/gr2/mod.rs +++ b/src/dev/gr2/mod.rs @@ -27,9 +27,12 @@ pub mod hq2; pub mod re3; #[cfg(feature = "gr2-jit")] pub mod re3_jit; +mod snapshot; pub mod vc1; pub mod xmap5; +pub use snapshot::Gr2SnapshotParts; + #[cfg(test)] #[path = "re3_tests.rs"] mod re3_tests; @@ -223,6 +226,9 @@ pub struct Gr2 { /// shadow (`hq_loop`'s Sink) no longer knows what RE3 holds then. re3_cpu_writes: crossbeam_utils::CachePadded, re3_asleep: crossbeam_utils::CachePadded, + published_raster: PublishedRe3Telemetry, + presentation_frames: AtomicU64, + presentation_host_read_pixels: AtomicU64, /// The display channel: screen, renderer slot and frame requests, run /// by the machine's refresh thread (`crate::display`). @@ -234,6 +240,78 @@ pub struct Gr2 { trace_mask: AtomicU32, } +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Gr2TelemetrySnapshot { + pub raster: re3::Re3TelemetrySnapshot, + pub presentation_frames: u64, + pub presentation_host_read_pixels: u64, + #[cfg(feature = "gr2-jit")] + pub jit_hits: u64, + #[cfg(feature = "gr2-jit")] + pub jit_misses: u64, +} + +struct PublishedRe3Telemetry { + spans: AtomicU64, + pixels: AtomicU64, + span_buckets: [AtomicU64; 8], + batchable_spans: AtomicU64, + max_batch: AtomicU32, + sync_boundaries: AtomicU64, + framebuffer_read_words: AtomicU64, + framebuffer_write_words: AtomicU64, + z_read_words: AtomicU64, + z_write_words: AtomicU64, + host_read_pixels: AtomicU64, + #[cfg(feature = "gr2-jit")] + jit_hits: AtomicU64, + #[cfg(feature = "gr2-jit")] + jit_misses: AtomicU64, +} + +impl PublishedRe3Telemetry { + fn load(&self) -> re3::Re3TelemetrySnapshot { + re3::Re3TelemetrySnapshot { + spans: self.spans.load(Ordering::Relaxed), + pixels: self.pixels.load(Ordering::Relaxed), + span_buckets: std::array::from_fn(|i| self.span_buckets[i].load(Ordering::Relaxed)), + batchable_spans: self.batchable_spans.load(Ordering::Relaxed), + max_batch: self.max_batch.load(Ordering::Relaxed), + sync_boundaries: self.sync_boundaries.load(Ordering::Relaxed), + framebuffer_read_words: self.framebuffer_read_words.load(Ordering::Relaxed), + framebuffer_write_words: self.framebuffer_write_words.load(Ordering::Relaxed), + z_read_words: self.z_read_words.load(Ordering::Relaxed), + z_write_words: self.z_write_words.load(Ordering::Relaxed), + host_read_pixels: self.host_read_pixels.load(Ordering::Relaxed), + } + } + + fn store(&self, value: re3::Re3TelemetrySnapshot) { + self.spans.store(value.spans, Ordering::Relaxed); + self.pixels.store(value.pixels, Ordering::Relaxed); + for (slot, value) in self.span_buckets.iter().zip(value.span_buckets) { + slot.store(value, Ordering::Relaxed); + } + self.batchable_spans.store(value.batchable_spans, Ordering::Relaxed); + self.max_batch.store(value.max_batch, Ordering::Relaxed); + self.sync_boundaries.store(value.sync_boundaries, Ordering::Relaxed); + self.framebuffer_read_words.store(value.framebuffer_read_words, Ordering::Relaxed); + self.framebuffer_write_words.store(value.framebuffer_write_words, Ordering::Relaxed); + self.z_read_words.store(value.z_read_words, Ordering::Relaxed); + self.z_write_words.store(value.z_write_words, Ordering::Relaxed); + self.host_read_pixels.store(value.host_read_pixels, Ordering::Relaxed); + } + + fn reset(&self) { + self.store(re3::Re3TelemetrySnapshot::default()); + #[cfg(feature = "gr2-jit")] + { + self.jit_hits.store(0, Ordering::Relaxed); + self.jit_misses.store(0, Ordering::Relaxed); + } + } +} + // SAFETY: same model as Rex3. `regs` is written only by the CPU thread (plus // monitor/snapshot with the machine stopped). `re3` and `hq_engine` are owned // by their threads, and the CPU only touches them after both FIFOs are @@ -277,6 +355,33 @@ impl Gr2 { pub fn variant(&self) -> Gr2Variant { self.variant } + pub fn telemetry_snapshot(&self) -> Gr2TelemetrySnapshot { + Gr2TelemetrySnapshot { + raster: self.published_raster.load(), + presentation_frames: self.presentation_frames.load(Ordering::Relaxed), + presentation_host_read_pixels: self.presentation_host_read_pixels.load(Ordering::Relaxed), + #[cfg(feature = "gr2-jit")] + jit_hits: self.published_raster.jit_hits.load(Ordering::Relaxed), + #[cfg(feature = "gr2-jit")] + jit_misses: self.published_raster.jit_misses.load(Ordering::Relaxed), + } + } + + fn publish_re3_telemetry(&self, re3: &re3::Re3) { + self.published_raster.store(re3.telemetry_snapshot()); + #[cfg(feature = "gr2-jit")] + { + self.published_raster.jit_hits.store(re3.jit.hits, Ordering::Relaxed); + self.published_raster.jit_misses.store(re3.jit.misses, Ordering::Relaxed); + } + } + + #[cfg(test)] + fn unimplemented_commands(&self) -> Vec { + // SAFETY: tests call this after wait_idle. + unsafe { (&*self.hq_engine.get()).unimplemented_commands() } + } + pub fn set_retrace_callback(&self, cb: Arc) { *self.retrace_cb.lock() = Some(cb); } @@ -790,6 +895,7 @@ impl Gr2 { fn finish(&mut self, flag: usize) { // The host polls the flag to learn the request is done, so it // must not be seen before the drawing it follows: drain RE3. + self.push(re3::RE3_OP_SYNC_BOUNDARY, 0); if !self.drain_re3() { return; } @@ -813,12 +919,15 @@ impl Gr2 { self.push(op, val); } fn read_image(&mut self, req: &hq2::ReadImage, dest: hq2::ReadDest) { + self.push(re3::RE3_OP_SYNC_BOUNDARY, 0); if !self.drain_re3() { return; } // SAFETY: RE3 is drained and idle; VRAM and Z are only read // here, as the display thread does. - let re3 = unsafe { &*self.g.re3.get() }; + let re3 = unsafe { &mut *self.g.re3.get() }; + re3.record_host_read(req.w as u64 * req.rows as u64); + self.g.publish_re3_telemetry(re3); let plane: &[u32] = if matches!(req.decode, hq2::ReadDecode::Depth { .. }) { &re3.zbuf } else { &re3.vram }; let read = |x: i32, y: i32| { if x < 0 || y < 0 || x as usize >= re3::FB_W || y as usize >= re3::FB_H { @@ -898,11 +1007,14 @@ impl Gr2 { if self.tracing(debug::TRACE_RE3) { self.trace_re3(addr, val, &re3.ctx.reg); } - let drew = match addr { - a if (a & !re3::RE3_SRC_HQ) < 64 => re3.write_reg((a & !re3::RE3_SRC_HQ) as usize, val as u32), + let (drew, metrics_changed) = match addr { + a if (a & !re3::RE3_SRC_HQ) < 64 => { + let drew = re3.write_reg((a & !re3::RE3_SRC_HQ) as usize, val as u32); + (drew, drew) + } re3::RE3_OP_COPY_A => { self.copy_a.set(val); - false + (false, false) } re3::RE3_OP_COPY_B => { let a = self.copy_a.get(); @@ -913,44 +1025,51 @@ impl Gr2 { } else { re3.copy_rect_gl(f(a, 0), f(a, 16), f(a, 32), f(a, 48), f(val, 0), f(val, 16), mode); } - true + (true, true) } re3::RE3_OP_READ_ADVANCE => { re3.read_buffer(); - false + (false, true) + } + re3::RE3_OP_SYNC_BOUNDARY => { + re3.record_sync_boundary(); + (false, true) } re3::RE3_OP_PIXFMT => { re3.set_pixfmt(val as u32); - false + (false, false) } re3::RE3_OP_ZCTL => { re3.set_zctl(val as u32); - false + (false, false) } re3::RE3_OP_STENCIL => { re3.set_stencil(val); - false + (false, false) } re3::RE3_OP_BLEND => { re3.set_blend(val as u32); - false + (false, false) } re3::RE3_OP_ALPHA => { re3.ctx.a = (val as u32) as i64; re3.ctx.da = (val >> 32) as u32 as i32 as i64; - false + (false, false) } re3::RE3_OP_ZFILL_A => { re3.ctx.zfill_rect = val; - false + (false, false) } re3::RE3_OP_ZFILL_B => { let rect = re3.ctx.zfill_rect; re3.zfill(rect, val); - false + (true, true) } - _ => false, + _ => (false, false), }; + if metrics_changed { + self.publish_re3_telemetry(re3); + } if drew { self.head.damage(); } @@ -996,12 +1115,37 @@ impl crate::gfx_display::GfxDisplay for Gr2 { impl Resettable for Gr2 { fn power_on(&self) { - let r = self.regs(); - r.ge.count = self.variant.ges(); - for d in r.dac.iter_mut() { - *d = unsafe { std::mem::zeroed() }; + debug_assert!(!self.running.load(Ordering::Acquire)); + #[cfg(feature = "gr2-jit")] + let jit_mode = unsafe { (*self.re3.get()).jit.mode }; + // These objects are large and plain data. Clear them in place so a + // reset cannot overflow a worker thread's stack. + unsafe { + std::ptr::write_bytes(self.regs.get(), 0, 1); + std::ptr::write_bytes(self.re3.get(), 0, 1); + std::ptr::write_bytes(self.hq_engine.get(), 0, 1); + (*self.regs.get()).ge.count = self.variant.ges(); + #[cfg(feature = "gr2-jit")] + { + (*self.re3.get()).jit.mode = jit_mode; + } } + self.hq_fifo.reset(); + self.re3_fifo.reset(); + self.fin3_pending.store(0, Ordering::Release); + self.fin2_wait.store(0, Ordering::Release); + self.fin2_progress.store(0, Ordering::Release); + self.fin2_armed_ns.store(0, Ordering::Release); + self.re3_cpu_writes.store(0, Ordering::Release); + self.published_raster.reset(); + self.presentation_frames.store(0, Ordering::Release); + self.presentation_host_read_pixels.store(0, Ordering::Release); + self.gedma_out_head.store(0, Ordering::Release); + self.gedma_out_tail.store(0, Ordering::Release); + self.copy_a.set(0); + self.head.damage(); } + } impl BusDevice for Gr2 { @@ -1113,6 +1257,13 @@ impl Device for Gr2 { fn stop(&self) { // First, so the stop is seen to come from this thread (`DisplayStop`). self.head.stop(); + if !self.running.load(Ordering::Acquire) { + return; + } + // Producers have already stopped at the machine boundary. Let HQ2 + // and RE3 finish every accepted FIFO entry before sending their exit + // sentinels; partial command streams live in Hq2Engine and survive. + self.wait_idle(); if !self.running.swap(false, Ordering::SeqCst) { return; } @@ -1123,12 +1274,9 @@ impl Device for Gr2 { for t in self.threads.lock().drain(..) { let _ = t.join(); } - // Discard the exit sentinels (and anything behind them). + // Both queues were drained before the sentinels were appended. self.hq_fifo.reset(); self.fin3_pending.store(0, Ordering::Release); - self.fin2_wait.store(0, Ordering::Release); - let head = self.gedma_out_head.load(Ordering::Acquire); - self.gedma_out_tail.store(head, Ordering::Release); self.re3_fifo.reset(); } diff --git a/src/dev/gr2/re3.rs b/src/dev/gr2/re3.rs index fbabc9349..a489ada95 100644 --- a/src/dev/gr2/re3.rs +++ b/src/dev/gr2/re3.rs @@ -174,6 +174,9 @@ pub fn copy_mode(zx: u32, zy: u32, bits: u32, bank: u32) -> u32 { } /// CPU consumed RWDATA during a READBUF stream; fetch the next pixel. pub const RE3_OP_READ_ADVANCE: u32 = 0xFFFF_1003; +/// A host-visible finish/readback boundary, queued so telemetry observes it +/// in exact RE3 order. +pub const RE3_OP_SYNC_BOUNDARY: u32 = 0xFFFF_100B; /// Emulator-private stand-in for RE3's (unidentified) packing-mode register: /// the GE sends neutral 8-bit-per-channel colour and RE3 packs each pixel /// into the window's frame-buffer format (RE3.h "Colour packing"). 0 = 24-bit / colour index as the iterators @@ -270,6 +273,35 @@ pub struct Re3Context { pub da: i64, } +pub const SPAN_BUCKET_LIMITS: [u32; 8] = [4, 8, 16, 32, 64, 256, 1024, u32::MAX]; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Re3TelemetrySnapshot { + pub spans: u64, + pub pixels: u64, + pub span_buckets: [u64; 8], + pub batchable_spans: u64, + pub max_batch: u32, + pub sync_boundaries: u64, + pub framebuffer_read_words: u64, + pub framebuffer_write_words: u64, + pub z_read_words: u64, + pub z_write_words: u64, + pub host_read_pixels: u64, +} + +/// Fixed-size, runtime-only evidence counters. They are deliberately outside +/// `Re3Context`, so snapshots restore rendering state without restoring a +/// measurement window. +#[derive(Clone, Copy, Default)] +#[repr(C)] +pub struct Re3Telemetry { + snapshot: Re3TelemetrySnapshot, + last_pipeline: u64, + current_batch: u32, + have_pipeline: u32, +} + /// RE3 state owned by the RE3 thread. Plain data, zero-valid. #[repr(C)] pub struct Re3 { @@ -279,9 +311,91 @@ pub struct Re3 { /// The span JIT's state (`re3_jit`): mode, memoised shaders, counts. #[cfg(feature = "gr2-jit")] pub jit: super::re3_jit::JitState, + pub telemetry: Re3Telemetry, } impl Re3 { + pub fn telemetry_snapshot(&self) -> Re3TelemetrySnapshot { + self.telemetry.snapshot + } + + pub fn reset_telemetry(&mut self) { + self.telemetry = Re3Telemetry::default(); + #[cfg(feature = "gr2-jit")] + { + self.jit.hits = 0; + self.jit.misses = 0; + } + } + + pub fn record_sync_boundary(&mut self) { + self.telemetry.snapshot.sync_boundaries = + self.telemetry.snapshot.sync_boundaries.saturating_add(1); + self.telemetry.have_pipeline = 0; + self.telemetry.current_batch = 0; + } + + pub fn record_host_read(&mut self, pixels: u64) { + self.telemetry.snapshot.host_read_pixels = + self.telemetry.snapshot.host_read_pixels.saturating_add(pixels); + } + + fn record_span(&mut self) { + let c = &self.ctx; + let pixels = c.numpix as u64; + let bucket = SPAN_BUCKET_LIMITS + .iter() + .position(|&limit| c.numpix <= limit) + .unwrap_or(SPAN_BUCKET_LIMITS.len() - 1); + let s = &mut self.telemetry.snapshot; + s.spans = s.spans.saturating_add(1); + s.pixels = s.pixels.saturating_add(pixels); + s.span_buckets[bucket] = s.span_buckets[bucket].saturating_add(1); + + let mut signature = 0xcbf2_9ce4_8422_2325u64; + for value in [ + c.reg[REG_IR], + c.reg[REG_FUNC], + c.reg[REG_NOPUP], + c.reg[REG_ENABPAT], + c.reg[REG_ALIGNPAT], + c.reg[REG_ENABDITH], + c.reg[REG_ENABWID], + c.reg[REG_ENABLWID], + c.reg[REG_FBOPTION], + c.reg[REG_DEPTHFN], + c.pixfmt, + c.zctl & 0xff, + (c.stencil & 0x000f_ffff_fff) as u32, + (c.stencil >> 32) as u32, + c.blend, + ] { + signature ^= value as u64; + signature = signature.wrapping_mul(0x100_0000_01b3); + } + if self.telemetry.have_pipeline != 0 && self.telemetry.last_pipeline == signature { + s.batchable_spans = s.batchable_spans.saturating_add(1); + self.telemetry.current_batch = self.telemetry.current_batch.saturating_add(1); + } else { + self.telemetry.current_batch = 1; + } + self.telemetry.have_pipeline = 1; + self.telemetry.last_pipeline = signature; + s.max_batch = s.max_batch.max(self.telemetry.current_batch); + + let fb_reads_per_pixel = 1 + + u64::from(c.reg[REG_ENABWID] != 0) + + u64::from(c.blend & 1 != 0); + s.framebuffer_read_words = s + .framebuffer_read_words + .saturating_add(pixels.saturating_mul(fb_reads_per_pixel)); + s.framebuffer_write_words = s.framebuffer_write_words.saturating_add(pixels); + if c.zctl & 1 != 0 || c.stencil & 1 != 0 { + s.z_read_words = s.z_read_words.saturating_add(pixels); + s.z_write_words = s.z_write_words.saturating_add(pixels); + } + } + /// Apply one register write (masked), with MAME's fan-out side effects. /// Returns true when the write executed something that touches VRAM. pub fn write_reg(&mut self, reg: usize, val: u32) -> bool { @@ -395,6 +509,9 @@ impl Re3 { fn execute(&mut self) { self.load_iterators(); + if matches!(self.ctx.reg[REG_IR], IR_SHADED | IR_FLAT | IR_FLAT4) { + self.record_span(); + } #[cfg(feature = "gr2-jit")] if super::re3_jit::run(self, self.ctx.reg[REG_IR]) { return; @@ -642,6 +759,11 @@ impl Re3 { let f = |s: u32| ((rect >> s) & 0xffff) as usize; let (x0, y0, x1, y1) = (f(0), f(16), f(32).min(FB_W), f(48).min(FB_H)); let (value, mask) = (val as u32, (val >> 32) as u32); + let pixels = y1.saturating_sub(y0) as u64 * x1.saturating_sub(x0) as u64; + self.telemetry.snapshot.z_read_words = + self.telemetry.snapshot.z_read_words.saturating_add(pixels); + self.telemetry.snapshot.z_write_words = + self.telemetry.snapshot.z_write_words.saturating_add(pixels); for y in y0..y1 { for z in &mut self.zbuf[y * FB_W + x0..y * FB_W + x1.max(x0)] { *z = (*z & !mask) | (value & mask); @@ -741,6 +863,14 @@ impl Re3 { } let x = (self.ctx.x >> 14) as i32; let y = (self.ctx.y >> 14) as i32; + self.record_host_read(1); + if matches!(self.ctx.reg[REG_RWMODE], RWMODE_ZB | RWMODE_ZB_P) { + self.telemetry.snapshot.z_read_words = + self.telemetry.snapshot.z_read_words.saturating_add(1); + } else { + self.telemetry.snapshot.framebuffer_read_words = + self.telemetry.snapshot.framebuffer_read_words.saturating_add(1); + } self.ctx.reg[REG_RWDATA] = if x >= 0 && y >= 0 && (x as usize) < FB_W && (y as usize) < FB_H { self.plane_read(y as usize * FB_W + x as usize) } else { @@ -782,6 +912,18 @@ impl Re3 { return; } let (zx, zy) = ((mode & 0xff).max(1) as i32, ((mode >> 8) & 0xff).max(1) as i32); + let source = (w as u64).saturating_mul(h as u64); + let dest = source.saturating_mul(zx as u64).saturating_mul(zy as u64); + self.telemetry.snapshot.framebuffer_read_words = self + .telemetry + .snapshot + .framebuffer_read_words + .saturating_add(source.saturating_add(dest)); + self.telemetry.snapshot.framebuffer_write_words = self + .telemetry + .snapshot + .framebuffer_write_words + .saturating_add(dest); let bits = ((mode >> 16) & 3) * 4; let bank = (mode >> 18) & 1; let pix = |v: u32| -> u32 { @@ -822,6 +964,17 @@ impl Re3 { if w <= 0 || h <= 0 { return; } + let pixels = (w as u64).saturating_mul(h as u64); + self.telemetry.snapshot.framebuffer_read_words = self + .telemetry + .snapshot + .framebuffer_read_words + .saturating_add(pixels.saturating_mul(2)); + self.telemetry.snapshot.framebuffer_write_words = self + .telemetry + .snapshot + .framebuffer_write_words + .saturating_add(pixels); let mask = ((self.ctx.reg[REG_AUXMASK] & 0xff) << 24) | self.ctx.reg[REG_PIXMASK]; // Walk away from the overlap: bottom-up rows when moving down in // memory, right-to-left columns when moving right. diff --git a/src/dev/gr2/re3_jit_tests.rs b/src/dev/gr2/re3_jit_tests.rs index 3121312df..fa1869425 100644 --- a/src/dev/gr2/re3_jit_tests.rs +++ b/src/dev/gr2/re3_jit_tests.rs @@ -356,3 +356,68 @@ fn bench_interpreter_vs_jit() { println!("{name:20} interpreter {:7.1} Mpx/s jit {:7.1} Mpx/s x{:.1}", t[0], t[1], t[1] / t[0]); } } + +/// Warm-JIT baseline across the span lengths used by the M16 acceleration +/// gate (`--release --ignored --nocapture`). +#[test] +#[ignore] +fn bench_span_lengths_interpreter_vs_jit() { + const TARGET_PIXELS: u64 = 64_000_000; + const REPEATS: usize = 6; + + for n in [4u32, 16, 64, 400, 1280] { + let mut engines = [new_re3(MODE_OFF), new_re3(MODE_SYNC)]; + for r in &mut engines { + for (reg, value) in [ + (REG_FUNC, ROP_COPY), + (REG_PIXMASK, 0x00ff_ffff), + (REG_XMAX, FB_W as u32 - 1), + (REG_YMAX, FB_H as u32 - 1), + (REG_DX, 1 << 14), + (REG_DR, 0x40), + (REG_DG, 0x20), + (REG_DB, 0x10), + ] { + r.write_reg(reg, value); + } + r.write_reg(REG_NUMPIX, n); + r.write_reg(REG_IR, IR_SHADED); + } + + let spans = (TARGET_PIXELS / n as u64).max(1); + let pixels = spans * n as u64; + let mut samples = [[0.0f64; REPEATS]; 2]; + for repeat in 0..REPEATS { + for pass in 0..2 { + let slot = (repeat + pass) & 1; + let r = &mut engines[slot]; + r.reset_telemetry(); + let start = std::time::Instant::now(); + for i in 0..spans { + let rows = FB_H as u64; + r.write_reg(REG_X, 0); + r.write_reg(REG_Y, (i % rows) as u32); + r.write_reg(REG_R, 0x1000); + r.write_reg(REG_NUMPIX, n); + r.write_reg(REG_IR, IR_SHADED); + } + samples[slot][repeat] = pixels as f64 / start.elapsed().as_secs_f64() / 1e6; + let telemetry = r.telemetry_snapshot(); + assert_eq!(telemetry.spans, spans); + assert_eq!(telemetry.pixels, pixels); + } + } + + let mut throughput = [0.0f64; 2]; + for (slot, samples) in samples.iter_mut().enumerate() { + samples.sort_by(f64::total_cmp); + throughput[slot] = (samples[REPEATS / 2 - 1] + samples[REPEATS / 2]) / 2.0; + } + println!( + "{n:4}-pixel spans interpreter {:7.1} Mpx/s warm jit {:7.1} Mpx/s x{:.2} ({REPEATS} interleaved samples)", + throughput[0], + throughput[1], + throughput[1] / throughput[0] + ); + } +} diff --git a/src/dev/gr2/snapshot.rs b/src/dev/gr2/snapshot.rs new file mode 100644 index 000000000..8ca22e906 --- /dev/null +++ b/src/dev/gr2/snapshot.rs @@ -0,0 +1,499 @@ +use super::hq2::Hq2Engine; +use super::re3::{Re3Context, FB_H, FB_W, REGMASK, STREAM_WRITE}; +use super::{Gr2, Gr2Regs, Gr2Variant, GEDMA_OUT_WORDS}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::mem::size_of; +use std::sync::atomic::Ordering; + +const GR2_SNAPSHOT_VERSION: u32 = 1; +const GR2_STATE_MAX_BYTES: usize = 8 << 20; + +pub type Gr2SnapshotParts = (String, Vec<(String, Vec)>); + +#[derive(Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct Gr2Metadata { + version: u32, + variant: String, + width: u32, + height: u32, + state_bytes: u64, +} + +#[derive(Serialize, Deserialize)] +struct Gr2State { + version: u32, + regs: Vec, + hq: Vec, + re3: Vec, + fin2_wait: u32, + fin3_pending: u32, + gedma: Vec, +} + +fn pack_bytes(bytes: &[u8]) -> Result, String> { + let len = u32::try_from(bytes.len()) + .map_err(|_| format!("GR2 snapshot part is too large: {} bytes", bytes.len()))?; + let mut words = Vec::with_capacity(1 + bytes.len().div_ceil(4)); + words.push(len); + for chunk in bytes.chunks(4) { + let mut word = [0; 4]; + word[..chunk.len()].copy_from_slice(chunk); + words.push(u32::from_be_bytes(word)); + } + Ok(words) +} + +fn unpack_bytes(name: &str, words: &[u32]) -> Result, String> { + let Some((&len, body)) = words.split_first() else { + return Err(format!("GR2 snapshot part {name} is empty")); + }; + let len = len as usize; + if body.len() != len.div_ceil(4) { + return Err(format!( + "GR2 snapshot part {name} has {} payload words for {len} bytes", + body.len() + )); + } + let mut bytes = Vec::with_capacity(body.len() * 4); + for word in body { + bytes.extend_from_slice(&word.to_be_bytes()); + } + bytes.truncate(len); + Ok(bytes) +} + +fn decode_state(bytes: &[u8]) -> Result { + if bytes.len() > GR2_STATE_MAX_BYTES { + return Err(format!( + "GR2 state is {} bytes, maximum is {GR2_STATE_MAX_BYTES}", + bytes.len() + )); + } + let (state, rest) = + postcard::take_from_bytes(bytes).map_err(|e| format!("decode GR2 state: {e}"))?; + if !rest.is_empty() { + return Err(format!("GR2 state has {} trailing bytes", rest.len())); + } + Ok(state) +} + +fn raw_bytes(value: &T) -> Vec { + let mut bytes = vec![0; size_of::()]; + // SAFETY: every captured GR2 type is fixed-layout plain data. Workers + // are stopped, so no field changes while its object representation is + // copied. + unsafe { + std::ptr::copy_nonoverlapping( + value as *const T as *const u8, + bytes.as_mut_ptr(), + bytes.len(), + ); + } + bytes +} + +fn decode_raw(name: &str, bytes: &[u8]) -> Result, String> { + if bytes.len() != size_of::() { + return Err(format!( + "GR2 {name} image is {} bytes, expected {}", + bytes.len(), + size_of::() + )); + } + let mut value = Box::::new_uninit(); + // SAFETY: callers use this only for GR2's fixed-layout, numeric, + // all-bit-pattern-valid plain-data structures. + unsafe { + std::ptr::copy_nonoverlapping(bytes.as_ptr(), value.as_mut_ptr() as *mut u8, bytes.len()); + Ok(value.assume_init()) + } +} + +fn variant_name(variant: Gr2Variant) -> &'static str { + match variant { + Gr2Variant::Xz => "xz", + Gr2Variant::Extreme => "extreme", + } +} + +fn validate_regs(regs: &Gr2Regs, variant: Gr2Variant) -> Result<(), String> { + if regs.ge.count != variant.ges() { + return Err(format!( + "GR2 register image has {} GE7s, {} requires {}", + regs.ge.count, + variant.name(), + variant.ges() + )); + } + for (index, xmap) in regs.xmap.iter().enumerate() { + if xmap.addr as usize >= super::xmap5::CLUT_SIZE { + return Err(format!("GR2 XMAP{index} address {:#x} is invalid", xmap.addr)); + } + if xmap.clut_phase > 2 { + return Err(format!( + "GR2 XMAP{index} CLUT phase {} is invalid", + xmap.clut_phase + )); + } + } + Ok(()) +} + +fn validate_re3(ctx: &Re3Context) -> Result<(), String> { + for (index, (&value, &mask)) in ctx.reg.iter().zip(REGMASK.iter()).enumerate() { + if value & !mask != 0 { + return Err(format!( + "GR2 RE3 register {index:#x} contains bits outside {mask:#010x}" + )); + } + } + if ctx.stream > STREAM_WRITE { + return Err(format!("GR2 RE3 stream state {} is invalid", ctx.stream)); + } + if ctx.pixfmt > super::re3::PIXFMT_RGB4 { + return Err(format!("GR2 RE3 pixel format {} is invalid", ctx.pixfmt)); + } + Ok(()) +} + +impl Gr2 { + /// Complete architectural GR2 state split into exact named word streams. + /// HQ2, RE3, and the display worker must already be stopped. + pub fn snapshot_parts(&self) -> Result { + if self.running.load(Ordering::Acquire) { + return Err("GR2 workers must be stopped before taking a snapshot".into()); + } + if !self.hq_fifo.is_empty() + || !self.re3_fifo.is_empty() + || self.hq_busy.load(Ordering::Acquire) + || self.re3_busy.load(Ordering::Acquire) + { + return Err("GR2 pipeline is not quiescent".into()); + } + let fin3_pending = self.fin3_pending.load(Ordering::Acquire); + if fin3_pending != 0 { + return Err(format!( + "GR2 has {fin3_pending} pending FIN3 command(s) at a quiescent boundary" + )); + } + let head = self.gedma_out_head.load(Ordering::Acquire); + let tail = self.gedma_out_tail.load(Ordering::Acquire); + let gedma_len = head.wrapping_sub(tail) as usize; + if gedma_len > GEDMA_OUT_WORDS { + return Err(format!( + "GR2 GEDMA output contains {gedma_len} words, capacity is {GEDMA_OUT_WORDS}" + )); + } + let gedma = (0..gedma_len) + .map(|i| { + self.gedma_out[(tail as usize + i) & (GEDMA_OUT_WORDS - 1)] + .load(Ordering::Relaxed) + }) + .collect(); + // SAFETY: the ownership contract above leaves all three objects + // immutable until this method returns. + let (regs, mut hq, re3) = unsafe { + ( + raw_bytes(&*self.regs.get()), + raw_bytes(&*self.hq_engine.get()), + &*self.re3.get(), + ) + }; + Hq2Engine::scrub_snapshot_image(&mut hq); + let state = Gr2State { + version: GR2_SNAPSHOT_VERSION, + regs, + hq, + re3: raw_bytes(&re3.ctx), + fin2_wait: self.fin2_wait.load(Ordering::Acquire), + fin3_pending, + gedma, + }; + let state = postcard::to_allocvec(&state).map_err(|e| format!("encode GR2 state: {e}"))?; + let metadata = toml::to_string(&Gr2Metadata { + version: GR2_SNAPSHOT_VERSION, + variant: variant_name(self.variant).into(), + width: FB_W as u32, + height: FB_H as u32, + state_bytes: state.len() as u64, + }) + .map_err(|e| format!("encode GR2 snapshot metadata: {e}"))?; + Ok(( + metadata, + vec![ + ("state".into(), pack_bytes(&state)?), + ("vram".into(), re3.vram.to_vec()), + ("zbuf".into(), re3.zbuf.to_vec()), + ], + )) + } + + /// Restore a complete GR2 snapshot. All payloads are decoded and + /// validated before any live architectural state changes. + pub fn restore_snapshot_parts( + &self, + metadata: &str, + parts: &[(String, Vec)], + ) -> Result<(), String> { + if self.running.load(Ordering::Acquire) { + return Err("GR2 workers must be stopped before restoring a snapshot".into()); + } + let metadata: Gr2Metadata = + toml::from_str(metadata).map_err(|e| format!("decode GR2 snapshot metadata: {e}"))?; + if metadata.version != GR2_SNAPSHOT_VERSION { + return Err(format!( + "GR2 snapshot version {} is not supported", + metadata.version + )); + } + if metadata.variant != variant_name(self.variant) { + return Err(format!( + "GR2 snapshot is for {}, running board is {}", + metadata.variant, + variant_name(self.variant) + )); + } + if metadata.width != FB_W as u32 || metadata.height != FB_H as u32 { + return Err(format!( + "GR2 snapshot framebuffer is {}x{}, expected {FB_W}x{FB_H}", + metadata.width, metadata.height + )); + } + let mut named = HashMap::with_capacity(parts.len()); + for (name, words) in parts { + if named.insert(name.as_str(), words.as_slice()).is_some() { + return Err(format!("duplicate GR2 snapshot part {name}")); + } + } + if named.len() != 3 { + return Err(format!("GR2 snapshot has {} parts, expected 3", named.len())); + } + let state_words = named + .remove("state") + .ok_or("GR2 snapshot part state is missing")?; + let vram = named + .remove("vram") + .ok_or("GR2 snapshot part vram is missing")?; + let zbuf = named + .remove("zbuf") + .ok_or("GR2 snapshot part zbuf is missing")?; + if let Some(name) = named.keys().next() { + return Err(format!("unknown GR2 snapshot part {name}")); + } + if vram.len() != FB_W * FB_H { + return Err(format!( + "GR2 VRAM part has {} words, expected {}", + vram.len(), + FB_W * FB_H + )); + } + if zbuf.len() != FB_W * FB_H { + return Err(format!( + "GR2 Z-buffer part has {} words, expected {}", + zbuf.len(), + FB_W * FB_H + )); + } + let state_bytes = unpack_bytes("state", state_words)?; + if metadata.state_bytes != state_bytes.len() as u64 { + return Err(format!( + "GR2 metadata records {} state bytes, part has {}", + metadata.state_bytes, + state_bytes.len() + )); + } + let state = decode_state(&state_bytes)?; + if state.version != GR2_SNAPSHOT_VERSION { + return Err(format!("GR2 state version {} is not supported", state.version)); + } + if state.fin3_pending != 0 { + return Err(format!( + "GR2 state has {} pending FIN3 command(s) but no queued FIFO entries", + state.fin3_pending + )); + } + if state.fin2_wait > 1 { + return Err(format!("GR2 FIN2 wait flag {} is invalid", state.fin2_wait)); + } + if state.gedma.len() > GEDMA_OUT_WORDS { + return Err(format!( + "GR2 GEDMA output contains {} words, capacity is {GEDMA_OUT_WORDS}", + state.gedma.len() + )); + } + let regs = decode_raw::("register", &state.regs)?; + let mut hq = decode_raw::("HQ2", &state.hq)?; + let re3 = decode_raw::("RE3 context", &state.re3)?; + validate_regs(®s, self.variant)?; + hq.validate_snapshot()?; + validate_re3(&re3)?; + hq.clear_runtime_diagnostics(); + + // SAFETY: validation is complete, workers are stopped, and these are + // plain-data objects with no destructors or runtime-owned pointers. + unsafe { + std::ptr::copy_nonoverlapping(&*regs, self.regs.get(), 1); + std::ptr::copy_nonoverlapping(&*hq, self.hq_engine.get(), 1); + let live_re3 = &mut *self.re3.get(); + live_re3.ctx = *re3; + live_re3.vram.copy_from_slice(vram); + live_re3.zbuf.copy_from_slice(zbuf); + #[cfg(feature = "gr2-jit")] + { + let mode = live_re3.jit.mode; + live_re3.jit = Default::default(); + live_re3.jit.mode = mode; + } + live_re3.reset_telemetry(); + } + self.hq_fifo.reset(); + self.re3_fifo.reset(); + self.fin3_pending.store(0, Ordering::Release); + self.fin2_wait.store(state.fin2_wait, Ordering::Release); + self.fin2_armed_ns.store(0, Ordering::Release); + self.fin2_progress.store(0, Ordering::Release); + self.re3_cpu_writes.store(0, Ordering::Release); + self.published_raster.reset(); + self.presentation_frames.store(0, Ordering::Release); + self.presentation_host_read_pixels.store(0, Ordering::Release); + self.copy_a.set(0); + self.gedma_out_tail.store(0, Ordering::Release); + for (slot, &word) in self.gedma_out.iter().zip(&state.gedma) { + slot.store(word, Ordering::Relaxed); + } + self.gedma_out_head + .store(state.gedma.len() as u32, Ordering::Release); + self.head.damage(); + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::dev::gr2::hq2::{PUC_COLOR, PUC_DATA, PUC_INIT, PUC_RECTI2D}; + use crate::dev::gr2::re3_tests::{live_gr2, w32}; + use crate::dev::gr2::GR2_BASE; + use crate::traits::{BusDevice, Device, Resettable, BUS_OK}; + + const FIFO_BASE: u32 = 0x40000; + + fn command(g: &Gr2, index: u32, value: u32) { + w32(g, FIFO_BASE + index * 4, value); + } + + fn data(g: &Gr2, value: u32) { + command(g, PUC_DATA, value); + } + + #[test] + fn xz_and_extreme_round_trip_complete_state_and_partial_hq2_command() { + for variant in [Gr2Variant::Xz, Gr2Variant::Extreme] { + let g = live_gr2(variant); + command(&g, PUC_INIT, 1); + command(&g, PUC_COLOR, 7); + command(&g, PUC_RECTI2D, 10); + data(&g, 20); + g.wait_idle(); + unsafe { + (*g.re3.get()).vram[3 * FB_W + 4] = 0x0012_3456; + (*g.re3.get()).zbuf[3 * FB_W + 4] = 0x89ab_cdef; + } + assert!(g.gedma_push(0xfeed_cafe)); + g.stop(); + let (metadata, parts) = g.snapshot_parts().unwrap(); + + g.power_on(); + g.restore_snapshot_parts(&metadata, &parts).unwrap(); + assert_eq!(g.regs().ge.count, variant.ges()); + assert_eq!(g.vram()[3 * FB_W + 4], 0x0012_3456); + assert_eq!(unsafe { (*g.re3.get()).zbuf[3 * FB_W + 4] }, 0x89ab_cdef); + let readback = g.read32(GR2_BASE + 0x6a068); + assert_eq!(readback.status, BUS_OK); + assert_eq!(readback.data, 0xfeed_cafe); + + g.start(); + data(&g, 12); + data(&g, 21); + g.wait_idle(); + assert_ne!(g.vram()[20 * FB_W + 10], 0); + g.stop(); + #[cfg(feature = "gr2-jit")] + let jit_mode = unsafe { (*g.re3.get()).jit.mode }; + g.power_on(); + assert_eq!(g.regs().ge.count, variant.ges()); + assert_eq!(g.vram()[3 * FB_W + 4], 0); + assert_eq!(unsafe { (*g.re3.get()).zbuf[3 * FB_W + 4] }, 0); + assert_eq!(g.gedma_out_head.load(Ordering::Acquire), 0); + assert_eq!(g.gedma_out_tail.load(Ordering::Acquire), 0); + #[cfg(feature = "gr2-jit")] + assert_eq!(unsafe { (*g.re3.get()).jit.mode }, jit_mode); + } + } + + #[test] + fn restore_rejects_variant_and_named_part_mismatches() { + let xz = live_gr2(Gr2Variant::Xz); + xz.stop(); + let (metadata, parts) = xz.snapshot_parts().unwrap(); + let extreme = live_gr2(Gr2Variant::Extreme); + extreme.stop(); + assert!(extreme + .restore_snapshot_parts(&metadata, &parts) + .unwrap_err() + .contains("running board")); + + let mut missing = parts.clone(); + missing.pop(); + assert!(xz + .restore_snapshot_parts(&metadata, &missing) + .unwrap_err() + .contains("expected 3")); + let mut duplicate = parts.clone(); + duplicate[2].0 = "vram".into(); + assert!(xz + .restore_snapshot_parts(&metadata, &duplicate) + .unwrap_err() + .contains("duplicate")); + let mut renamed = parts; + renamed[2].0 = "depth".into(); + assert!(xz + .restore_snapshot_parts(&metadata, &renamed) + .unwrap_err() + .contains("zbuf")); + } + + #[test] + fn invalid_late_state_does_not_mutate_the_board() { + let g = live_gr2(Gr2Variant::Xz); + g.stop(); + let (metadata, mut parts) = g.snapshot_parts().unwrap(); + let state_part = parts.iter_mut().find(|(name, _)| name == "state").unwrap(); + let bytes = unpack_bytes("state", &state_part.1).unwrap(); + let mut state = decode_state(&bytes).unwrap(); + state.re3[4 * size_of::()..5 * size_of::()] + .copy_from_slice(&2u32.to_ne_bytes()); + let bytes = postcard::to_allocvec(&state).unwrap(); + state_part.1 = pack_bytes(&bytes).unwrap(); + let mut metadata_value: Gr2Metadata = toml::from_str(&metadata).unwrap(); + metadata_value.state_bytes = bytes.len() as u64; + let metadata = toml::to_string(&metadata_value).unwrap(); + + unsafe { + (*g.re3.get()).vram[0] = 0x00ab_cdef; + } + assert!(g + .restore_snapshot_parts(&metadata, &parts) + .unwrap_err() + .contains("outside")); + assert_eq!(g.vram()[0], 0x00ab_cdef); + } + + #[test] + fn snapshot_requires_stopped_workers() { + let g = live_gr2(Gr2Variant::Xz); + assert!(g.snapshot_parts().unwrap_err().contains("must be stopped")); + } +} diff --git a/src/dev/mgras/debug.rs b/src/dev/mgras/debug.rs index ed7ce7f85..65b9faae6 100644 --- a/src/dev/mgras/debug.rs +++ b/src/dev/mgras/debug.rs @@ -412,7 +412,10 @@ impl Mgras { } pub(super) fn cmd_rss(&self, args: &[&str], w: &mut dyn Write) -> Result<(), String> { - self.mirror_now(); + self.mirror_now( + #[cfg(feature = "wgpu")] + super::wgpu::Observation::Debug, + ); let e = |r: std::io::Result<()>| r.map_err(|e| e.to_string()); // SAFETY: read-only peek at the RSS thread's state (may tear). let rss = unsafe { &*self.rss.get() }; @@ -458,7 +461,10 @@ impl Mgras { } fn mgras_status(&self, w: &mut dyn Write) -> std::io::Result<()> { - self.mirror_now(); + self.mirror_now( + #[cfg(feature = "wgpu")] + super::wgpu::Observation::Debug, + ); // SAFETY: read-only peek at the RSS (may tear while drawing). let rss = unsafe { &*self.rss.get() }; let f = self.front(); @@ -534,6 +540,15 @@ impl Mgras { writeln!(w, "GPU unit: {records} records in {batches} batches, {} dropped; {} MiB uploaded, {} MiB read back in {} syncs; scan-out frames presented on the GPU {}, read back {}", u.dropped, u.uploaded >> 20, u.read_back >> 20, u.syncs, self.gpu_frames[0].load(Ordering::Relaxed), self.gpu_frames[1].load(Ordering::Relaxed))?; + let observations: Vec<_> = u + .observation_counts() + .filter(|(_, count, _)| *count != 0) + .map(|(name, count, bytes)| format!("{name}:{count}/{bytes}B")) + .collect(); + writeln!(w, " observations (reason:count/bytes): {}", observations.join(" "))?; + if let Some(error) = self.gpu_fault.lock().as_ref() { + writeln!(w, " fault: {error}")?; + } } #[cfg(feature = "gr4-jit")] { @@ -593,7 +608,10 @@ impl Mgras { /// overlay.bin: stride 2048, display-height rows, row 0 at the bottom), /// and PNG views of them at the displayed size, top-down. fn dump_framebuffer(&self, dir: &std::path::Path) -> std::io::Result<()> { - self.mirror_now(); + self.mirror_now( + #[cfg(feature = "wgpu")] + super::wgpu::Observation::Debug, + ); use rss::WIDTH; std::fs::create_dir_all(dir)?; // SAFETY: read-only views; tearing tolerated. diff --git a/src/dev/mgras/disp.rs b/src/dev/mgras/disp.rs index 09d5eb170..dd2cbb4ec 100644 --- a/src/dev/mgras/disp.rs +++ b/src/dev/mgras/disp.rs @@ -387,14 +387,22 @@ impl crate::display::Head for Mgras { self.gpu_frames[0].fetch_add(1, Ordering::Relaxed); scan.taken = Taken::Gpu(buffer, w, h); } - Err(e) => eprintln!("mgras: GPU scan-out: {e}"), + Err(e) => self.fail_gpu("scan-out compose", e), } } } let _ = want; + if self.gpu_failed() { + crate::pf_end!(crate::perfetto::events::MGRAS_CAPTURE); + return false; + } if matches!(scan.taken, Taken::None) { scan.taken = match self.compose_gpu_into(rss, &mut screen.fb_rgb) { Some((w, h)) => Taken::Read(w, h), + None if self.gpu_failed() => { + crate::pf_end!(crate::perfetto::events::MGRAS_CAPTURE); + return false; + } None => Taken::Pages, }; } @@ -493,7 +501,7 @@ impl Mgras { self.gpu_frames[1].fetch_add(1, Ordering::Relaxed); return Some(size); } - Err(e) => eprintln!("mgras: GPU scan-out: {e}"), + Err(e) => self.fail_gpu("scan-out readback", e), } } let _ = (rss, out); diff --git a/src/dev/mgras/ge11.rs b/src/dev/mgras/ge11.rs index 18a600133..9972fe3b6 100644 --- a/src/dev/mgras/ge11.rs +++ b/src/dev/mgras/ge11.rs @@ -58,22 +58,27 @@ pub struct Ge11 { ge: [GeDiag; 2], /// Pending diagnostic readback words, oldest first. pub out: Ring<{ 1 << 18 }>, + count: u32, } impl Ge11 { + pub fn set_count(&mut self, count: u32) { + self.count = count.min(self.ge.len() as u32); + } + /// Engine `ge`'s current diagnostic address. pub fn diag_addr(&self, ge: usize) -> u32 { - self.ge[ge & 1].addr + self.ge.get(ge).filter(|_| ge < self.count as usize).map_or(0, |g| g.addr) } /// Engine `ge`'s microcode line `i` (from the start of microcode). pub fn ucode_line(&self, ge: usize, i: usize) -> Option<[u32; 3]> { - self.ge[ge & 1].ucode.get(i).copied() + self.ge.get(ge).filter(|_| ge < self.count as usize)?.ucode.get(i).copied() } /// A fingerprint of engine `ge`'s downloaded program (0 if none). pub fn fingerprint(&self, ge: usize) -> u64 { - self.ge[ge & 1].hash.get() + self.ge.get(ge).filter(|_| ge < self.count as usize).map_or(0, |g| g.hash.get()) } /// Whether `off` is one of the engines' diagnostic data or address ports. @@ -86,6 +91,9 @@ impl Ge11 { /// the readback registers. pub fn write(&mut self, off: u32, val: u32) -> bool { let n = host::GE_DIAG.iter().position(|&(d, a)| off == d || off == a).unwrap(); + if n >= self.count as usize { + return false; + } let (data_port, _) = host::GE_DIAG[n]; let ge = &mut self.ge[n]; if off != data_port { diff --git a/src/dev/mgras/hq3.rs b/src/dev/mgras/hq3.rs index b33de8580..4e27aa2ab 100644 --- a/src/dev/mgras/hq3.rs +++ b/src/dev/mgras/hq3.rs @@ -14,6 +14,8 @@ use std::sync::Arc; +use serde::{Deserialize, Serialize}; + use super::plain::RegMap; use crate::traits::BusDevice; @@ -605,6 +607,40 @@ pub struct Hq3Engine { pub stats: Hq3Stats, } +#[derive(Serialize, Deserialize)] +pub(super) struct Hq3Snapshot { + version: u32, + context_words: u32, + context_image: Vec, + cfifo: [Vec; 3], + dl_logged: u32, + dma_regs: Vec, + raster_if_regs: Vec, + formatter: u32, + dma_read_pending: Option, + kick_starts_read: bool, + kick_read_ran: bool, + dma_logged: u32, + gl: Vec, + slot: Option, + nosave_next: bool, + eram_in: Option>, + lut: Vec, + ges: usize, + eram_write_ge: Option, + eram_write_ge0: Option, + eram_read_ge0: Option<(usize, usize)>, + bitmap_in: Vec, + bitmap_parked: Vec<(u32, Vec)>, + pix_unaligned: Vec, + payload_for: Option, + payload: Vec, + readback_parked: Vec<(u32, u32, Option<[u32; 2]>)>, + eram: Vec, + ge_return: Option<[u32; 2]>, + last_token: Option, +} + impl Hq3Engine { pub fn new(ges: usize) -> Self { Hq3Engine { @@ -643,6 +679,128 @@ impl Hq3Engine { } } + pub(super) fn snapshot_bytes(&self) -> Result, String> { + let mut gl = vec![0u32; GL_WORDS]; + // SAFETY: Gl is plain numeric state, and the destination has room + // for its bytes rounded up to a word. + unsafe { + std::ptr::copy_nonoverlapping( + &self.gl as *const super::gl::Gl as *const u8, + gl.as_mut_ptr() as *mut u8, + std::mem::size_of::(), + ); + } + let cfifo = std::array::from_fn(|i| { + let mut words = vec![0u32; PARSER_WORDS]; + self.cfifo[i].store(&mut words); + words + }); + let snapshot = Hq3Snapshot { + version: 1, + context_words: self.context_words, + context_image: self.context_image.to_vec(), + cfifo, + dl_logged: self.dl_logged, + dma_regs: self.dma_regs.to_vec(), + raster_if_regs: self.raster_if_regs.to_vec(), + formatter: self.formatter, + dma_read_pending: self.dma_read_pending, + kick_starts_read: self.kick_starts_read, + kick_read_ran: self.kick_read_ran, + dma_logged: self.dma_logged, + gl, + slot: self.slot, + nosave_next: self.nosave_next, + eram_in: self.eram_in.clone(), + lut: self.lut.0.to_vec(), + ges: self.ges, + eram_write_ge: self.eram_write_ge, + eram_write_ge0: self.eram_write_ge0, + eram_read_ge0: self.eram_read_ge0, + bitmap_in: self.bitmap_in.clone(), + bitmap_parked: self.bitmap_parked.clone(), + pix_unaligned: self.pix_unaligned.clone(), + payload_for: self.payload_for, + payload: self.payload.clone(), + readback_parked: self.readback_parked.clone(), + eram: self.eram.words.iter().flatten().copied().collect(), + ge_return: self.ge_return, + last_token: self.last_token, + }; + postcard::to_allocvec(&snapshot).map_err(|e| format!("encode HQ3 snapshot: {e}")) + } + + pub(super) fn decode_snapshot(bytes: &[u8], expected_ges: usize) -> Result { + let s: Hq3Snapshot = postcard::from_bytes(bytes).map_err(|e| format!("decode HQ3 snapshot: {e}"))?; + if s.version != 1 { + return Err(format!("HQ3 snapshot version {} is not supported", s.version)); + } + let exact = |name: &str, got: usize, want: usize| { + (got == want).then_some(()).ok_or_else(|| format!("HQ3 {name}: {got} words, expected {want}")) + }; + exact("context image", s.context_image.len(), host::CONTEXT_SWITCH_WORDS as usize)?; + for words in &s.cfifo { + exact("FIFO parser", words.len(), PARSER_WORDS)?; + } + exact("DMA registers", s.dma_regs.len(), 0x80)?; + exact("raster-interface registers", s.raster_if_regs.len(), 0x10)?; + exact("GL state", s.gl.len(), GL_WORDS)?; + exact("colour LUT", s.lut.len(), super::pixpipe::LUT_WORDS)?; + exact("ERAM", s.eram.len(), 2 * ERAM_WORDS)?; + if s.ges != expected_ges { + return Err(format!("HQ3 snapshot has {} GE11s, this board has {expected_ges}", s.ges)); + } + if s.context_words > host::CONTEXT_SWITCH_WORDS { + return Err(format!("HQ3 context switch has {} words left", s.context_words)); + } + if s.eram_write_ge.is_some_and(|n| n >= expected_ges) { + return Err(format!("HQ3 ERAM write targets GE {}", s.eram_write_ge.unwrap())); + } + Ok(s) + } + + pub(super) fn apply_snapshot(&mut self, s: Hq3Snapshot) { + self.context_words = s.context_words; + self.context_image.copy_from_slice(&s.context_image); + self.cfifo = std::array::from_fn(|i| Cfifo::load(&s.cfifo[i])); + self.dl_logged = s.dl_logged; + self.dma_regs.copy_from_slice(&s.dma_regs); + self.raster_if_regs.copy_from_slice(&s.raster_if_regs); + self.formatter = s.formatter; + self.dma_read_pending = s.dma_read_pending; + self.kick_starts_read = s.kick_starts_read; + self.kick_read_ran = s.kick_read_ran; + self.dma_logged = s.dma_logged; + // SAFETY: decode_snapshot checked the exact rounded word count, and + // Gl is plain numeric state. + unsafe { + std::ptr::copy_nonoverlapping( + s.gl.as_ptr() as *const u8, + &mut self.gl as *mut super::gl::Gl as *mut u8, + std::mem::size_of::(), + ); + } + self.slot = s.slot; + self.nosave_next = s.nosave_next; + self.eram_in = s.eram_in; + self.lut.0.copy_from_slice(&s.lut); + self.eram_write_ge = s.eram_write_ge; + self.eram_write_ge0 = s.eram_write_ge0; + self.eram_read_ge0 = s.eram_read_ge0; + self.bitmap_in = s.bitmap_in; + self.bitmap_parked = s.bitmap_parked; + self.pix_unaligned = s.pix_unaligned; + self.payload_for = s.payload_for; + self.payload = s.payload; + self.readback_parked = s.readback_parked; + for (dst, src) in self.eram.words.iter_mut().flatten().zip(s.eram) { + *dst = src; + } + self.ge_return = s.ge_return; + self.last_token = s.last_token; + self.stats = Hq3Stats::ZERO; + } + /// The host requested a context switch: the save phase completes at once /// (the caller raises the flag). The outgoing context's GE state and /// user-port parser go to its ERAM slot, as the HQ microcode saves them diff --git a/src/dev/mgras/hq4.rs b/src/dev/mgras/hq4.rs index c6acf1825..b80b1dbbd 100644 --- a/src/dev/mgras/hq4.rs +++ b/src/dev/mgras/hq4.rs @@ -56,6 +56,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Arc; use parking_lot::Mutex; +use serde::{Deserialize, Serialize}; use super::{Gl, Hq3Board, Mgras, MGRAS_SLOT_GFX_BASE}; use crate::config::GraphicsBoard; @@ -547,6 +548,57 @@ struct GeUpload { hash: [super::microcode::Running; 2], } +#[derive(Serialize, Deserialize)] +struct GeUploadSnapshot { + words: [Vec; 2], + write_ptr: [usize; 2], + read_ptr: [usize; 2], + read_odd: [bool; 2], + load_mode: [bool; 2], + active: usize, + hash: [u64; 2], +} + +#[derive(Serialize, Deserialize)] +struct Hq4Snapshot { + version: u32, + control: u32, + int_dest: [u32; 2], + context_word: u32, + context_word_written: bool, + entry: u32, + pending: u32, + status_extra: u32, + enable: u32, + hq_enable: u32, + vector_dest: [u32; 2], + vectors: u32, + plain: Vec<(u32, u32)>, + i2c: Vec<(u32, u8)>, + mapping: u64, + flag_shadow: u32, + ge: GeUploadSnapshot, + nic_control: u32, + entry_selected: bool, + protocol: u8, + first_ack: bool, + context_words: u32, + ready: bool, + lowater_asserted: bool, + general_asserted: bool, + no_dest_logged: bool, + commands: bool, + nic: Option, +} + +#[derive(Serialize, Deserialize)] +struct ImpactSnapshot { + version: u32, + board: String, + base: u64, + mgras: String, +} + impl GeUpload { /// A control write. When it ends a download, the fingerprint of the /// words loaded. @@ -769,6 +821,183 @@ impl ImpactXio { self.mgras.clone() } + pub fn snapshot_parts(&self) -> Result<(String, Vec<(String, Vec)>), String> { + let (mgras, mut parts) = self.mgras.snapshot_parts()?; + let s = self.shared.hq4.lock(); + let mut plain: Vec<_> = s.plain.iter().map(|(&k, &v)| (k, v)).collect(); + plain.sort_unstable(); + let mut i2c: Vec<_> = s.i2c.iter().map(|(&k, &v)| (k, v)).collect(); + i2c.sort_unstable(); + let ge = GeUploadSnapshot { + words: s.ge.words.clone(), + write_ptr: s.ge.write_ptr, + read_ptr: s.ge.read_ptr, + read_odd: s.ge.read_odd, + load_mode: s.ge.load_mode, + active: s.ge.active, + hash: std::array::from_fn(|i| s.ge.hash[i].get()), + }; + let nic = self.shared.nic.lock().as_ref().map(|b| crate::snapshot::BinValue::from_toml(&b.save_state())); + let state = Hq4Snapshot { + version: 1, + control: s.control, + int_dest: s.int_dest, + context_word: s.context_word, + context_word_written: s.context_word_written, + entry: s.entry, + pending: s.pending, + status_extra: s.status_extra, + enable: s.enable, + hq_enable: s.hq_enable, + vector_dest: s.vector_dest, + vectors: s.vectors, + plain, + i2c, + mapping: s.mapping, + flag_shadow: s.flag_shadow, + ge, + nic_control: s.nic_control, + entry_selected: s.entry_selected, + protocol: s.protocol, + first_ack: s.first_ack, + context_words: s.context_words, + ready: s.ready, + lowater_asserted: s.lowater_asserted, + general_asserted: s.general_asserted, + no_dest_logged: s.no_dest_logged, + commands: self.shared.commands.load(Ordering::Acquire), + nic, + }; + drop(s); + let bytes = postcard::to_allocvec(&state).map_err(|e| format!("encode HQ4 snapshot: {e}"))?; + parts.push(("hq4".into(), super::pack_snapshot_bytes(&bytes))); + let metadata = toml::to_string(&ImpactSnapshot { + version: 1, + board: self.board.name.to_string(), + base: self.base, + mgras, + }) + .map_err(|e| format!("encode IMPACT XIO snapshot metadata: {e}"))?; + Ok((metadata, parts)) + } + + pub fn restore_snapshot_parts(&self, metadata: &str, parts: &[(String, Vec)]) -> Result<(), String> { + let meta: ImpactSnapshot = + toml::from_str(metadata).map_err(|e| format!("decode IMPACT XIO snapshot metadata: {e}"))?; + if meta.version != 1 { + return Err(format!("IMPACT XIO snapshot version {} is not supported", meta.version)); + } + if meta.board != self.board.name || meta.base != self.base { + return Err(format!( + "IMPACT XIO snapshot is for {} at {:#x}, running board is {} at {:#x}", + meta.board, meta.base, self.board.name, self.base + )); + } + let mut hq4_words = None; + let mut mgras_parts = Vec::with_capacity(parts.len().saturating_sub(1)); + for (name, words) in parts { + if name == "hq4" { + if hq4_words.replace(words.as_slice()).is_some() { + return Err("duplicate IMPACT snapshot part hq4".into()); + } + } else { + mgras_parts.push((name.clone(), words.clone())); + } + } + let hq4_words = hq4_words.ok_or("IMPACT snapshot part hq4 is missing")?; + let bytes = super::unpack_snapshot_bytes("hq4", hq4_words)?; + let state: Hq4Snapshot = + postcard::from_bytes(&bytes).map_err(|e| format!("decode HQ4 snapshot: {e}"))?; + if state.version != 1 { + return Err(format!("HQ4 snapshot version {} is not supported", state.version)); + } + if state.ge.active >= 2 + || state.ge.words.iter().any(|w| w.len() > ge::WORDS) + || state.ge.write_ptr.iter().any(|&p| p > ge::WORDS) + || !matches!(state.protocol, 0 | 1 | 3) + || state.context_words > CONTEXT_WORDS + { + return Err("HQ4 snapshot contains invalid protocol or GE upload state".into()); + } + let mut nic = match state.nic.as_ref() { + Some(saved) => { + let mut bus = nic_bus(&self.board); + bus.load_state(&saved.clone().into_toml()).map_err(|e| format!("HQ4 MicroLAN: {e}"))?; + Some(bus) + } + None => None, + }; + let plain: HashMap<_, _> = state.plain.iter().copied().collect(); + let i2c: HashMap<_, _> = state.i2c.iter().copied().collect(); + if plain.len() != state.plain.len() || i2c.len() != state.i2c.len() { + return Err("HQ4 snapshot contains duplicate register entries".into()); + } + self.mgras.restore_snapshot_parts(&meta.mgras, &mgras_parts)?; + + let _order = self.shared.order.lock(); + let mut old = self.shared.hq4.lock(); + let old_lines = [ + (old.vector(0), old.lowater_asserted), + (old.vector(8), old.general_asserted), + ]; + let mut ge = GeUpload { + words: state.ge.words, + write_ptr: state.ge.write_ptr, + read_ptr: state.ge.read_ptr, + read_odd: state.ge.read_odd, + load_mode: state.ge.load_mode, + active: state.ge.active, + ..Default::default() + }; + for (hash, value) in ge.hash.iter_mut().zip(state.ge.hash) { + hash.restore(value); + } + *old = Hq4 { + control: state.control, + int_dest: state.int_dest, + context_word: state.context_word, + context_word_written: state.context_word_written, + entry: state.entry, + pending: state.pending, + status_extra: state.status_extra, + enable: state.enable, + hq_enable: state.hq_enable, + vector_dest: state.vector_dest, + vectors: state.vectors, + plain, + i2c, + mapping: state.mapping, + flag_shadow: state.flag_shadow, + ge, + nic_control: state.nic_control, + entry_selected: state.entry_selected, + protocol: state.protocol, + first_ack: state.first_ack, + context_words: state.context_words, + ready: state.ready, + lowater_asserted: state.lowater_asserted, + general_asserted: state.general_asserted, + no_dest_logged: state.no_dest_logged, + }; + let new_lines = [ + (old.vector(0), old.lowater_asserted), + (old.vector(8), old.general_asserted), + ]; + drop(old); + self.shared.commands.store(state.commands, Ordering::Release); + *self.shared.nic.lock() = nic.take(); + self.shared.noted.lock().clear(); + for (vector, set) in old_lines { + if set && !new_lines.contains(&(vector, true)) { + self.shared.irq.set_line(vector as u32, false); + } + } + for (vector, set) in new_lines { + self.shared.irq.set_line(vector as u32, set); + } + Ok(()) + } + /// Put the board's locks in the `locks` monitor listing. pub fn register_locks(self: &Arc) { use crate::locks::register_mutex_fn; @@ -1508,6 +1737,41 @@ mod tests { assert!(!s.first_ack && !s.ready); } + #[test] + fn snapshot_restores_hq4_and_raster_continuation() { + let r = rig(); + r.enter_context(); + r.w32(CONTROL, 0xC1D2_8354); + r.w64(MAPPING, 0x1234_5678_9ABC_DEF0); + r.w32(0x040000, 0x75E6_8192); + r.x_server(); + r.rss(0x110, 1 << 20, false); + r.rss(0x176, 0x12, false); + r.block(70, 80, 70, 80); + assert_eq!(r.b.mgras().fb_pixel(70, 80), 0x12); + r.b.mgras().stop_engines(); + let (metadata, parts) = r.b.snapshot_parts().unwrap(); + + r.b.mgras().start_engines(); + r.w32(CONTROL, 0x0102_0304); + r.w64(MAPPING, 0); + r.rss(0x176, 0x56, false); + r.block(70, 80, 70, 80); + assert_eq!(r.b.mgras().fb_pixel(70, 80), 0x56); + r.b.mgras().stop_engines(); + r.b.reset(); + assert_eq!(r.r32(CONTROL), 0); + + r.b.restore_snapshot_parts(&metadata, &parts).unwrap(); + assert_eq!(r.r32(CONTROL), 0xC1D2_8354); + assert_eq!(r.r(64, MAPPING), (BUS_OK, 0x1234_5678_9ABC_DEF0)); + assert_eq!(r.r32(0x040000), 0x75E6_8192); + assert_eq!(r.b.mgras().fb_pixel(70, 80), 0x12); + r.b.mgras().start_engines(); + r.block(71, 80, 71, 80); + assert_eq!(r.b.mgras().fb_pixel(71, 80), 0x12); + } + #[test] fn context_switch_handshake() { let r = rig(); diff --git a/src/dev/mgras/mgras_tests.rs b/src/dev/mgras/mgras_tests.rs index 1f9a74e59..4a0f69d74 100644 --- a/src/dev/mgras/mgras_tests.rs +++ b/src/dev/mgras/mgras_tests.rs @@ -9,9 +9,10 @@ use std::sync::Arc; use parking_lot::Mutex; -use super::record::tests::{live_board, poll_until, read, replay_recs, write}; +use super::record::tests::{live_board, live_board_of, poll_until, read, replay_recs, write}; use super::record::load_text; use super::Mgras; +use crate::config::GraphicsBoard; use crate::traits::{BusDevice, BusRead32, BusRead8, BUS_OK}; const CFIFO: u32 = 0x70080; @@ -116,6 +117,89 @@ fn fifo_and_direct_writes_keep_program_order() { m.stop_engines(); } +#[test] +fn snapshots_round_trip_and_drawing_continues_on_every_impact_product() { + for kind in [GraphicsBoard::SolidImpact, GraphicsBoard::HighImpact, GraphicsBoard::MaxImpact] { + let m = live_board_of(kind); + x_server(&m); + direct_rss(&m, FILLMODE, FILL_FAST, false); + direct_rss(&m, FILL_COLOR_R, 0x12, false); + block(&m, 30, 40, 30, 40); + assert_eq!(m.fb_pixel(30, 40), 0x12, "{kind:?}"); + m.stop_engines(); + let (metadata, mut parts) = m.snapshot_parts().unwrap(); + + m.start_engines(); + direct_rss(&m, FILL_COLOR_R, 0x56, false); + block(&m, 30, 40, 30, 40); + assert_eq!(m.fb_pixel(30, 40), 0x56, "{kind:?}"); + m.stop_engines(); + + let hq = parts.iter().position(|(name, _)| name == "hq3").unwrap(); + let saved = std::mem::take(&mut parts[hq].1); + let error = m.restore_snapshot_parts(&metadata, &parts).unwrap_err(); + assert!(error.contains("hq3"), "{error}"); + assert_eq!(m.fb_pixel(30, 40), 0x56, "invalid restore changed {kind:?}"); + parts[hq].1 = saved; + + m.restore_snapshot_parts(&metadata, &parts).unwrap(); + assert_eq!(m.fb_pixel(30, 40), 0x12, "{kind:?}"); + m.start_engines(); + block(&m, 31, 40, 31, 40); + assert_eq!(m.fb_pixel(31, 40), 0x12, "continued {kind:?}"); + m.stop_engines(); + } +} + +#[test] +fn snapshot_preserves_a_partial_command_fifo_packet() { + let m = live_board(); + x_server(&m); + direct_rss(&m, FILLMODE, FILL_FAST, false); + let command = 0x1000 | FILL_COLOR_R; + write(&m, 32, CFIFO, ((command << 8) | 4) as u64); + m.stop_engines(); + let (metadata, parts) = m.snapshot_parts().unwrap(); + + m.restore_snapshot_parts(&metadata, &parts).unwrap(); + m.start_engines(); + write(&m, 32, CFIFO, 0x2A); + fifo_rss(&m, IR_ALIAS, 0x18, false); + fifo_rss(&m, BLOCKXYSTARTI, 12 << 16 | 13, false); + fifo_rss(&m, BLOCKXYENDI, 12 << 16 | 13, true); + assert_eq!(m.fb_pixel(12, 13), 0x2A); + m.stop_engines(); +} + +#[cfg(feature = "wgpu")] +#[test] +fn wgpu_snapshot_reseeds_the_resident_mirror() { + let m = super::record::tests::live_board_raster( + GraphicsBoard::HighImpact, + crate::config::RasterUnit::Wgpu, + ); + x_server(&m); + direct_rss(&m, FILLMODE, FILL_FAST, false); + direct_rss(&m, FILL_COLOR_R, 0x21, false); + block(&m, 45, 55, 45, 55); + assert_eq!(m.fb_pixel(45, 55), 0x21); + m.stop_engines(); + let (metadata, parts) = m.snapshot_parts().unwrap(); + + m.start_engines(); + direct_rss(&m, FILL_COLOR_R, 0x65, false); + block(&m, 45, 55, 45, 55); + assert_eq!(m.fb_pixel(45, 55), 0x65); + m.stop_engines(); + m.restore_snapshot_parts(&metadata, &parts).unwrap(); + assert_eq!(m.fb_pixel(45, 55), 0x21); + + m.start_engines(); + block(&m, 46, 55, 46, 55); + assert_eq!(m.fb_pixel(46, 55), 0x21); + m.stop_engines(); +} + #[test] fn pio_read_through_the_bus() { let m = live_board(); @@ -393,6 +477,73 @@ fn ge11_microcode_reads_back_as_the_driver_verifies_it() { m.stop_engines(); } +#[test] +fn geometry_diagnostics_follow_the_fitted_engine_count() { + use crate::config::GraphicsBoard; + + for (kind, second_present) in [ + (GraphicsBoard::SolidImpact, false), + (GraphicsBoard::HighImpact, false), + (GraphicsBoard::MaxImpact, true), + ] { + let m = super::record::tests::live_board_of(kind); + let (data, addr) = (0x50048, 0x5004C); + write(&m, 32, addr, 0x20_0000); + for w in [0x1111_1111u32, 0x2222_2222, 0x33, 0x4444_4444, 0x5555_5555, 0x66] { + write(&m, 32, data, w as u64); + } + write(&m, 32, addr, 0x20_0000); + write(&m, 32, addr, 0x8000_0002); + assert_eq!( + read(&m, 32, FLAGS) as u32 & 1 << 18 != 0, + second_present, + "{kind:?}" + ); + assert_eq!( + read(&m, 32, 0x5022C) as u32, + if second_present { 0x1111_1111 } else { 0 }, + "{kind:?}" + ); + m.stop_engines(); + } +} + +#[test] +fn maximum_impact_config_routes_work_to_one_raster_engine() { + let m = super::record::tests::live_board_of(crate::config::GraphicsBoard::MaxImpact); + x_server(&m); + direct_rss(&m, FILLMODE, FILL_FAST, false); + + direct_rss(&m, CONFIG, 0x0AC, false); + direct_rss(&m, FILL_COLOR_R, 0x11, false); + direct_rss(&m, CONFIG, 0x4AC, false); + direct_rss(&m, FILL_COLOR_R, 0x22, false); + direct_rss(&m, CONFIG, 0xCAC, false); + block(&m, 19, 10, 19, 13); + assert_eq!( + (10..=13).map(|y| m.fb_pixel(19, y)).collect::>(), + [0x22, 0x11, 0x22, 0x11], + "each RE keeps its selected register state" + ); + + direct_rss(&m, CONFIG, 0x0AC, false); + block(&m, 20, 10, 20, 13); + assert_eq!( + (10..=13).map(|y| m.fb_pixel(20, y)).collect::>(), + [0, 0x11, 0, 0x11], + "RE0 receives only its alternating scanlines" + ); + + direct_rss(&m, CONFIG, 0x4AC, false); + block(&m, 21, 10, 21, 13); + assert_eq!( + (10..=13).map(|y| m.fb_pixel(21, y)).collect::>(), + [0x22, 0, 0x22, 0], + "RE1 receives the other alternating scanlines" + ); + m.stop_engines(); +} + /// A text recording replays through the same engine as the goldens. #[test] fn text_recording_replays() { diff --git a/src/dev/mgras/microcode.rs b/src/dev/mgras/microcode.rs index 03d62ebe3..b5fa0f9cc 100644 --- a/src/dev/mgras/microcode.rs +++ b/src/dev/mgras/microcode.rs @@ -37,6 +37,10 @@ impl Running { pub fn get(&self) -> u64 { self.0 } + + pub(super) fn restore(&mut self, value: u64) { + self.0 = value; + } } /// The HQ microcode and the two GE11 programs; 0 is "not loaded". diff --git a/src/dev/mgras/mod.rs b/src/dev/mgras/mod.rs index ec50005fd..48cea7e44 100644 --- a/src/dev/mgras/mod.rs +++ b/src/dev/mgras/mod.rs @@ -67,8 +67,9 @@ mod rss_jit_tests; mod test_gen; use parking_lot::Mutex; +use serde::{Deserialize, Serialize}; use std::cell::UnsafeCell; -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::io::Write as IoWrite; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::mem::MaybeUninit; @@ -80,7 +81,7 @@ use crate::config::GraphicsBoard; use crate::dev::ng1::rex3::Renderer; use crate::gfifo::GFifo; use crate::locks::SpinLock; -use crate::traits::{BusDevice, BusRead8, BusRead16, BusRead32, BusRead64, BUS_BUSY, BUS_OK, Device, Saveable}; +use crate::traits::{BusDevice, BusRead8, BusRead16, BusRead32, BusRead64, BUS_BUSY, BUS_ERR, BUS_OK, Device, Saveable}; use ge11::Ge11; use hq3::{host, Hq3Engine, Hq3Regs, Hq3Sink}; @@ -140,6 +141,36 @@ fn rss_entry(r: u32, exec: bool) -> u32 { (r & 0x3FF) << 1 | exec as u32 } +fn pack_snapshot_bytes(bytes: &[u8]) -> Vec { + let mut words = Vec::with_capacity(1 + bytes.len().div_ceil(4)); + words.push(bytes.len() as u32); + for chunk in bytes.chunks(4) { + let mut word = [0; 4]; + word[..chunk.len()].copy_from_slice(chunk); + words.push(u32::from_be_bytes(word)); + } + words +} + +fn unpack_snapshot_bytes(name: &str, words: &[u32]) -> Result, String> { + let Some((&len, body)) = words.split_first() else { + return Err(format!("IMPACT snapshot part {name} is empty")); + }; + let len = len as usize; + if body.len() != len.div_ceil(4) { + return Err(format!( + "IMPACT snapshot part {name} has {} payload words for {len} bytes", + body.len() + )); + } + let mut bytes = Vec::with_capacity(body.len() * 4); + for word in body { + bytes.extend_from_slice(&word.to_be_bytes()); + } + bytes.truncate(len); + Ok(bytes) +} + /// CPU-side board state: host interface registers, the geometry engines' /// diagnostic ports, the display control bus. Plain data, valid zeroed. #[repr(C)] @@ -178,6 +209,7 @@ impl Front { GraphicsBoard::MaxImpact => [0x0A, 0x02], _ => [0, 0], }; + self.ge.set_count(if kind == GraphicsBoard::MaxImpact { 2 } else { 1 }); self.dcb.init(bdvers, [0xFB, 0xFB]); } @@ -344,6 +376,8 @@ pub struct Mgras { /// (and holders of `submit` with the board idle). #[cfg(feature = "wgpu")] gpu_unit: UnsafeCell>>, + #[cfg(feature = "wgpu")] + gpu_fault: SpinLock>, /// PIO read advances engine 0 has done; engine 1 waits for each before /// it goes on drawing (see `rss_loop`). pio_advances: AtomicU64, @@ -405,6 +439,19 @@ pub struct Mgras { pf_fifo_intr: crate::PfLevel, } +#[derive(Serialize, Deserialize)] +struct MgrasSnapshot { + version: u32, + board: String, + raster_engines: usize, + fifo_cfg: u32, + flags: u32, + pio_advances: u64, + ge_upload_fingerprint: [u64; 2], + probes: [u64; 8], + busy_bound: bool, +} + // SAFETY: `eng` is touched only by the HQ3 thread, `rss` only by the RSS // thread, except by holders of `submit` while the board is idle (no producer // can push, no engine is running). The display thread reads `rss`'s @@ -441,7 +488,10 @@ impl Hq3Sink for HqSink<'_> { fn dma_read_line(&mut self, line: u32) -> Vec { self.0.drain_rss(); // SAFETY: the RSS is drained and idle; only the HQ3 feeds it now. - unsafe { &mut *self.0.rss.get() }.gpu_sync_transfer(); + let _ = unsafe { &mut *self.0.rss.get() }.gpu_sync_transfer( + #[cfg(feature = "wgpu")] + wgpu::Observation::DmaRead, + ); // SAFETY: as in `transfer`. unsafe { &*self.0.rss.get() }.dma_read_line(line) } @@ -584,6 +634,7 @@ impl Mgras { (*p).rss_fifo.set_heads(nrss); #[cfg(feature = "wgpu")] { + addr_of_mut!((*p).gpu_fault).write(SpinLock::new(None)); addr_of_mut!((*p).gpu_scanout).write(UnsafeCell::new(scanout)); addr_of_mut!((*p).gpu_device).write(device); addr_of_mut!((*p).gpu_unit).write(UnsafeCell::new(unit)); @@ -592,6 +643,7 @@ impl Mgras { // from the planes as they are (zeroed). u.attach(&(*planes).mem, &(*planes).cid, &mut (*planes).te); addr_of_mut!((*(*p).rss.get()).gpu).write(wgpu::GpuLink::new(u)); + addr_of_mut!((*(*p).rss.get()).gpu_fault).write(&(*p).gpu_fault); } } addr_of_mut!((*p).mem).write(SpinLock::new(None)); @@ -627,6 +679,186 @@ impl Mgras { Ok(m) } + /// Architectural board state split into named word streams for the + /// snapshot chunk store. Engine threads must already be stopped. + pub fn snapshot_parts(&self) -> Result<(String, Vec<(String, Vec)>), String> { + if self.engines.load(Ordering::Acquire) { + return Err("IMPACT engines must be stopped before taking a snapshot".into()); + } + #[cfg(feature = "wgpu")] + if let Some(error) = self.gpu_fault.lock().as_ref() { + return Err(format!("IMPACT GPU raster unit is faulted: {error}")); + } + let _submit = self.submit.lock(); + let front = { + let _front = self.front_lock.lock(); + // Front and all of its members are plain numeric state. The board + // is allocated zeroed, so its padding is initialized as well. + unsafe { + std::slice::from_raw_parts( + self.front.get().cast::(), + std::mem::size_of::(), + ) + .to_vec() + } + }; + // SAFETY: the engine threads are stopped and submit excludes producers. + unsafe { + for id in 0..self.nrss { + (*self.engine_cell(id).get()).gpu_sync( + #[cfg(feature = "wgpu")] + wgpu::Observation::Snapshot, + )?; + } + } + let meta = MgrasSnapshot { + version: 1, + board: self.kind.as_str().to_string(), + raster_engines: self.nrss, + fifo_cfg: self.fifo_cfg.load(Ordering::Acquire), + flags: self.flags.load(Ordering::Acquire), + pio_advances: self.pio_advances.load(Ordering::Acquire), + ge_upload_fingerprint: std::array::from_fn(|i| self.ge_upload_fingerprint[i].load(Ordering::Acquire)), + probes: std::array::from_fn(|i| self.probe[i].load(Ordering::Acquire)), + busy_bound: self.busy_bound.load(Ordering::Acquire), + }; + let metadata = toml::to_string(&meta).map_err(|e| format!("encode IMPACT snapshot metadata: {e}"))?; + let mut parts = vec![("front".into(), pack_snapshot_bytes(&front))]; + let hq = unsafe { &*self.eng.get() }.snapshot_bytes()?; + parts.push(("hq3".into(), pack_snapshot_bytes(&hq))); + for id in 0..self.nrss { + let rss = unsafe { &*self.engine_cell(id).get() }.snapshot_bytes()?; + parts.push((format!("rss{id}"), pack_snapshot_bytes(&rss))); + } + let planes = unsafe { &*self.planes.get() }; + let mut pixels = Vec::with_capacity(planes.mem.words.len() * 8); + for word in &planes.mem.words { + pixels.extend_from_slice(&word.to_le_bytes()); + } + parts.push(("pixmem".into(), pack_snapshot_bytes(&pixels))); + parts.push(("cid".into(), pack_snapshot_bytes(&planes.cid))); + parts.push(("te1".into(), pack_snapshot_bytes(&planes.te.snapshot_bytes()?))); + Ok((metadata, parts)) + } + + /// Restore a complete set of named architectural parts. Every part is + /// decoded and checked before any live board state is changed. + pub fn restore_snapshot_parts(&self, metadata: &str, parts: &[(String, Vec)]) -> Result<(), String> { + if self.engines.load(Ordering::Acquire) { + return Err("IMPACT engines must be stopped before restoring a snapshot".into()); + } + let meta: MgrasSnapshot = + toml::from_str(metadata).map_err(|e| format!("decode IMPACT snapshot metadata: {e}"))?; + if meta.version != 1 { + return Err(format!("IMPACT snapshot version {} is not supported", meta.version)); + } + if meta.board != self.kind.as_str() { + return Err(format!( + "IMPACT snapshot is for {}, running board is {}", + meta.board, + self.kind.as_str() + )); + } + if meta.raster_engines != self.nrss { + return Err(format!( + "IMPACT snapshot uses {} raster engines, running board uses {}", + meta.raster_engines, self.nrss + )); + } + let mut named = HashMap::with_capacity(parts.len()); + for (name, words) in parts { + if named.insert(name.as_str(), words.as_slice()).is_some() { + return Err(format!("duplicate IMPACT snapshot part {name}")); + } + } + let expected = 5 + self.nrss; + if named.len() != expected { + return Err(format!("IMPACT snapshot has {} parts, expected {expected}", named.len())); + } + let mut take = |name: &str| -> Result, String> { + let words = named.remove(name).ok_or_else(|| format!("IMPACT snapshot part {name} is missing"))?; + unpack_snapshot_bytes(name, words) + }; + let front = take("front")?; + if front.len() != std::mem::size_of::() { + return Err(format!( + "IMPACT front state is {} bytes, expected {}", + front.len(), + std::mem::size_of::() + )); + } + let hq = Hq3Engine::decode_snapshot(&take("hq3")?, if self.kind == GraphicsBoard::MaxImpact { 2 } else { 1 })?; + let mut rss = Vec::with_capacity(self.nrss); + for id in 0..self.nrss { + rss.push(Rss::decode_snapshot(&take(&format!("rss{id}"))?)?); + } + let pixels = take("pixmem")?; + let pixel_bytes = unsafe { &*self.planes.get() }.mem.words.len() * 8; + if pixels.len() != pixel_bytes { + return Err(format!("IMPACT pixel memory is {} bytes, expected {pixel_bytes}", pixels.len())); + } + let cid = take("cid")?; + let cid_bytes = unsafe { &*self.planes.get() }.cid.len(); + if cid.len() != cid_bytes { + return Err(format!("IMPACT clip-ID memory is {} bytes, expected {cid_bytes}", cid.len())); + } + let te = te1::Te1::decode_snapshot(&take("te1")?)?; + if let Some(name) = named.keys().next() { + return Err(format!("unknown IMPACT snapshot part {name}")); + } + + let _submit = self.submit.lock(); + { + let _front = self.front_lock.lock(); + // SAFETY: the length was checked and Front is plain numeric state. + unsafe { + std::ptr::copy_nonoverlapping(front.as_ptr(), self.front.get().cast::(), front.len()); + } + } + unsafe { &mut *self.eng.get() }.apply_snapshot(hq); + let planes = unsafe { &mut *self.planes.get() }; + for (dst, src) in planes.mem.words.iter_mut().zip(pixels.chunks_exact(8)) { + *dst = u64::from_le_bytes(src.try_into().unwrap()); + } + planes.cid.copy_from_slice(&cid); + planes.te.apply_snapshot(te); + for (id, state) in rss.into_iter().enumerate() { + let engine = unsafe { &mut *self.engine_cell(id).get() }; + engine.apply_snapshot(state); + engine.share_te(self.nrss as u32); + } + #[cfg(feature = "wgpu")] + unsafe { + if let Some(unit) = (*self.gpu_unit.get()).as_mut() { + unit.attach(&planes.mem, &planes.cid, &mut planes.te); + (*self.rss.get()).gpu = wgpu::GpuLink::new(unit); + (*self.rss.get()).gpu_fault = &self.gpu_fault; + } + } + self.fifo_cfg.store(meta.fifo_cfg, Ordering::Release); + self.flags.store(meta.flags, Ordering::Release); + self.pio_advances.store(meta.pio_advances, Ordering::Release); + for (dst, value) in self.ge_upload_fingerprint.iter().zip(meta.ge_upload_fingerprint) { + dst.store(value, Ordering::Release); + } + for (dst, value) in self.probe.iter().zip(meta.probes) { + dst.store(value, Ordering::Release); + } + self.busy_bound.store(meta.busy_bound, Ordering::Release); + self.busy_since.store(0, Ordering::Release); + self.hq_busy.store(false, Ordering::Release); + for busy in &self.rss_busy { + busy.store(false, Ordering::Release); + } + self.hq_fifo.reset(); + self.rss_fifo.reset(); + self.timing_stale.store(true, Ordering::Release); + self.marks.mark_all(); + self.head.damage(); + self.refresh_general(); + Ok(()) + } + /// Put the board's locks in the `locks` monitor listing. pub fn register_locks(self: &Arc) { use crate::locks::{register_mutex_fn, register_spin}; @@ -811,7 +1043,10 @@ impl Mgras { fn state_bytes(&self) -> Vec { const RW: u32 = 1280; const RH: u32 = 1024; - self.mirror_sync(); + self.mirror_sync( + #[cfg(feature = "wgpu")] + wgpu::Observation::StateHash, + ); // SAFETY: idle, and the caller holds `submit` (see the Sync impl). let rss = unsafe { &*self.rss.get() }; let f = self.front(); @@ -907,25 +1142,48 @@ impl Mgras { /// readback of what drawing changed since the last). Call with the /// board idle and `submit` held, or on the HQ3 thread after /// `drain_rss`. - fn mirror_sync(&self) { + fn mirror_sync( + &self, + #[cfg(feature = "wgpu")] reason: wgpu::Observation, + ) { #[cfg(feature = "wgpu")] // SAFETY: the board is idle (see above): engine 0's thread does not // touch its state or the unit. - unsafe { &mut *self.rss.get() }.gpu_sync(); + let _ = unsafe { &mut *self.rss.get() }.gpu_sync(reason); } /// `mirror_sync` from anywhere: holds `submit` and waits for the board /// to go idle first. Nothing to do on the CPU raster path. Not with /// `submit` or the front lock held. - fn mirror_now(&self) { + fn mirror_now( + &self, + #[cfg(feature = "wgpu")] reason: wgpu::Observation, + ) { #[cfg(feature = "wgpu")] if unsafe { (*self.gpu_unit.get()).is_some() } { let _sub = self.submit.lock(); self.wait_idle(); - self.mirror_sync(); + self.mirror_sync(reason); } } + #[cfg(feature = "wgpu")] + fn fail_gpu(&self, operation: &str, error: String) { + let mut fault = self.gpu_fault.lock(); + if fault.is_none() { + let message = format!("{operation}: {error}"); + eprintln!("mgras: GPU raster unit failed: {message}"); + *fault = Some(message); + } + } + + fn gpu_failed(&self) -> bool { + #[cfg(feature = "wgpu")] + return self.gpu_fault.lock().is_some(); + #[cfg(not(feature = "wgpu"))] + false + } + /// Whether frames are composed on the GPU (the GPU unit's scan-out). fn presents_on_gpu(&self) -> bool { #[cfg(feature = "wgpu")] @@ -960,7 +1218,10 @@ impl Mgras { pub fn composite(&self, x: i32, y: i32, bgra: &[u8], stride: usize, w: usize, h: usize) -> bool { let sub = self.submit.lock(); self.wait_idle(); - self.mirror_sync(); + self.mirror_sync( + #[cfg(feature = "wgpu")] + wgpu::Observation::HostComposite, + ); // SAFETY: idle with `submit` held. let rss = unsafe { &mut *self.rss.get() }; let ok = disp::composite(rss, &self.front().dcb, x, y, bgra, stride, w, h); @@ -979,7 +1240,10 @@ impl Mgras { pub fn composite_index(&self, x: i32, y: i32, bgra: &[u8], stride: usize, w: usize, h: usize, overlay: bool) -> bool { let sub = self.submit.lock(); self.wait_idle(); - self.mirror_sync(); + self.mirror_sync( + #[cfg(feature = "wgpu")] + wgpu::Observation::HostComposite, + ); // SAFETY: idle with `submit` held. let rss = unsafe { &mut *self.rss.get() }; let ok = disp::composite_index(rss, &self.front().dcb, x, y, bgra, stride, w, h, overlay); @@ -1000,7 +1264,10 @@ impl Mgras { pub(crate) fn fb_pixel(&self, x: usize, y: usize) -> u32 { let _sub = self.submit.lock(); self.wait_idle(); - self.mirror_sync(); + self.mirror_sync( + #[cfg(feature = "wgpu")] + wgpu::Observation::Validation, + ); // SAFETY: idle with `submit` held. let dcb = &self.front().dcb; let h = frame::display_size(dcb).1; @@ -1032,7 +1299,10 @@ impl Mgras { pub(crate) fn compare_scanouts(&self) -> Option<(usize, Vec<(usize, usize, u32, u32)>)> { let _sub = self.submit.lock(); self.wait_idle(); - self.mirror_sync(); + self.mirror_sync( + #[cfg(feature = "wgpu")] + wgpu::Observation::Validation, + ); // SAFETY: idle with `submit` held; no display thread runs in tests. let sc = unsafe { &mut *self.gpu_scanout.get() }.as_mut()?; let rss = unsafe { &*self.rss.get() }; @@ -1062,7 +1332,10 @@ impl Mgras { /// A frame snapshot of what is displayed now (monitor, screenshots). fn snapshot_frame(&self) -> Box { - self.mirror_now(); + self.mirror_now( + #[cfg(feature = "wgpu")] + wgpu::Observation::Display, + ); let mut f = plain::boxed_zeroed::(); // SAFETY: a read-only view; tearing tolerated. let rss = unsafe { &*self.rss.get() }; @@ -1365,7 +1638,10 @@ impl Mgras { // Taking the high half moves the stream on; the RSS does // that, in order (the FIFO is empty, so this fits). // SAFETY: idle with `submit` held. - unsafe { &mut *self.rss.get() }.gpu_sync_transfer(); + let _ = unsafe { &mut *self.rss.get() }.gpu_sync_transfer( + #[cfg(feature = "wgpu")] + wgpu::Observation::PioRead, + ); let v = unsafe { &*self.rss.get() }.pio_peek_hi(); self.hq_fifo.push(tag::PIO_ADVANCE, 0); self.wake_hq(); @@ -1655,7 +1931,17 @@ impl Mgras { } let changed = match t { tag::EXIT => break, - t if t < 0x1000 => rss.write((t & 0x7FF) >> 1, val as u32, t & 1 != 0), + t if t < 0x1000 => { + let reg = (t & 0x7FF) >> 1; + if reg == rss::reg::CONFIG + || rss.accepts_destination() + || Rss::uses_shared_texture_state(reg) + { + rss.write(reg, val as u32, t & 1 != 0) + } else { + false + } + } tag::PIO_ADVANCE => { advances += 1; if id == 0 { @@ -1846,25 +2132,45 @@ fn read_status(v: Option) -> (u32, u64) { impl BusDevice for Mgras { fn read32(&self, addr: u64) -> BusRead32 { + if self.gpu_failed() { + return BusRead32::err(); + } let (status, data) = read_status(self.do_read(addr, 32)); - BusRead32 { status, data: data as u32 } + if self.gpu_failed() { BusRead32::err() } else { BusRead32 { status, data: data as u32 } } + } + fn write32(&self, addr: u64, val: u32) -> u32 { + if self.gpu_failed() { BUS_ERR } else { self.do_write(addr, 32, val as u64) } } - fn write32(&self, addr: u64, val: u32) -> u32 { self.do_write(addr, 32, val as u64) } fn read8(&self, addr: u64) -> BusRead8 { + if self.gpu_failed() { + return BusRead8::err(); + } let (status, data) = read_status(self.do_read(addr, 8)); - BusRead8 { status, data: data as u8 } + if self.gpu_failed() { BusRead8::err() } else { BusRead8 { status, data: data as u8 } } + } + fn write8(&self, addr: u64, val: u8) -> u32 { + if self.gpu_failed() { BUS_ERR } else { self.do_write(addr, 8, val as u64) } } - fn write8(&self, addr: u64, val: u8) -> u32 { self.do_write(addr, 8, val as u64) } fn read16(&self, addr: u64) -> BusRead16 { + if self.gpu_failed() { + return BusRead16::err(); + } let (status, data) = read_status(self.do_read(addr, 16)); - BusRead16 { status, data: data as u16 } + if self.gpu_failed() { BusRead16::err() } else { BusRead16 { status, data: data as u16 } } + } + fn write16(&self, addr: u64, val: u16) -> u32 { + if self.gpu_failed() { BUS_ERR } else { self.do_write(addr, 16, val as u64) } } - fn write16(&self, addr: u64, val: u16) -> u32 { self.do_write(addr, 16, val as u64) } fn read64(&self, addr: u64) -> BusRead64 { + if self.gpu_failed() { + return BusRead64::err(); + } let (status, data) = read_status(self.do_read(addr, 64)); - BusRead64 { status, data } + if self.gpu_failed() { BusRead64::err() } else { BusRead64 { status, data } } + } + fn write64(&self, addr: u64, val: u64) -> u32 { + if self.gpu_failed() { BUS_ERR } else { self.do_write(addr, 64, val) } } - fn write64(&self, addr: u64, val: u64) -> u32 { self.do_write(addr, 64, val) } } /// The board as the display host GL presents into: frames land in the diff --git a/src/dev/mgras/plain.rs b/src/dev/mgras/plain.rs index 2c4ec1d13..44828f806 100644 --- a/src/dev/mgras/plain.rs +++ b/src/dev/mgras/plain.rs @@ -61,6 +61,12 @@ impl RegMap { } } + pub fn clear(&mut self) { + self.used.fill(false); + self.len = 0; + self.overflowed = false; + } + /// Entries in slot order. pub fn iter(&self) -> impl Iterator + '_ { (0..N).filter(|&i| self.used[i]).map(|i| (self.keys[i], self.vals[i])) diff --git a/src/dev/mgras/record.rs b/src/dev/mgras/record.rs index a736e86b0..3f412e4e0 100644 --- a/src/dev/mgras/record.rs +++ b/src/dev/mgras/record.rs @@ -541,7 +541,10 @@ pub(super) mod tests { if n.parse::().ok() == Some(taken.len().saturating_sub(1)) { let _sub = m.submit.lock(); m.wait_idle(); - m.mirror_sync(); + m.mirror_sync( + #[cfg(feature = "wgpu")] + super::super::wgpu::Observation::StateHash, + ); // SAFETY: idle with `submit` held. let rss = unsafe { &*m.rss.get() }; let mut out: Vec = rss.mem.words.iter().flat_map(|w| w.to_le_bytes()).collect(); diff --git a/src/dev/mgras/rss.rs b/src/dev/mgras/rss.rs index 302704e6f..28d5b758c 100644 --- a/src/dev/mgras/rss.rs +++ b/src/dev/mgras/rss.rs @@ -26,6 +26,7 @@ use super::fixed; use super::pixmem::{Buffer, Kind, PixMem}; use super::plain::{RegMap, Slot}; use super::te1::{reg as te_reg, tex_env, Sampler, Te1, ITER_ONE, TEXMODE1_ENABLE}; +use serde::{Deserialize, Serialize}; /// Framebuffer coordinates the model accepts (x and y, 0..2048); where a /// pixel lands in memory depends on its buffer (see `pixmem`). @@ -442,6 +443,8 @@ pub(super) const FILL_LINE_STIPPLE_OPAQUE: u32 = 1 << 6; const STATUS_IDLE: u32 = 0x100 | (1 << 4); /// Config: Y-flip. pub(super) const CONFIG_YFLIP: u32 = 1 << 3; +const CONFIG_DEST_RE_SHIFT: u32 = 10; +const CONFIG_DEST_RE_BROADCAST: u32 = 3; /// Fill mode: fast fill (solid, from the fill colour registers). pub(super) const FILL_FAST: u32 = 1 << 20; /// Fill mode: char data stipples the block (with block type 1), and the @@ -938,7 +941,7 @@ pub(super) fn signed16(v: u32) -> i32 { /// A block, in window coordinates, with its colour. #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Serialize, Deserialize)] pub(super) struct Block { pub xs: i32, pub ys: i32, @@ -964,7 +967,7 @@ impl Block { /// A character block being filled with stipple data, one row at a time. #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Serialize, Deserialize)] pub(super) struct Stipple { pub block: Block, pub col: i32, @@ -974,7 +977,7 @@ pub(super) struct Stipple { /// An armed pixel transfer. #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Serialize, Deserialize)] pub(super) struct Xfer { pub block: Block, pub read: bool, @@ -1053,7 +1056,7 @@ pub(crate) fn bytes_per_pixel(xfrmode: u32) -> u32 { /// What the raster subsystem has done, for `mgras stats`. #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Serialize, Deserialize)] pub struct RssStats { /// Executed primitives by IR opcode (bits 3:0). pub prims: [u64; 16], @@ -1208,6 +1211,28 @@ pub struct Rss { /// stores. Null on the CPU path. #[cfg(feature = "wgpu")] pub gpu: super::wgpu::GpuLink, + #[cfg(feature = "wgpu")] + pub gpu_fault: *const crate::locks::SpinLock>, +} + +#[derive(Serialize, Deserialize)] +pub(super) struct RssSnapshot { + version: u32, + regs: Vec, + device: Vec<(u32, u32)>, + device_overflowed: bool, + line_buf: Vec, + stipple: Option, + xfer: Option, + stats: RssStats, + gl_line_stipple_pos: u32, + gl_stash: Vec, + fillmodes: Vec, + fillmodes_n: usize, + stale_events: u32, + stale_last: [u32; 2], + te_load: u32, + te_read: [u32; 3], } impl Drop for Rss { @@ -1381,10 +1406,25 @@ impl Rss { std::ptr::addr_of_mut!((*p).cid).write(Plane(std::ptr::addr_of_mut!((*planes).cid))); std::ptr::addr_of_mut!((*p).te).write(Plane(std::ptr::addr_of_mut!((*planes).te))); std::ptr::addr_of_mut!((*p).te_sync).write(Plane(std::ptr::addr_of_mut!((*planes).te_sync))); + (*p).regs[reg::CONFIG as usize] = CONFIG_DEST_RE_BROADCAST << CONFIG_DEST_RE_SHIFT; #[cfg(feature = "gr4-jit")] std::ptr::addr_of_mut!((*p).jit.mode).write(super::rss_jit::default_mode()); } + /// Whether CONFIG.DestRE routes the next raster operation to this RE4. + pub fn accepts_destination(&self) -> bool { + let dest = (self.reg(reg::CONFIG) >> CONFIG_DEST_RE_SHIFT) & 3; + dest == CONFIG_DEST_RE_BROADCAST || dest == self.engine + } + + /// Registers kept common because this model has one TE1/TRAM shared by + /// both RE4s instead of one texture subsystem per engine. + pub fn uses_shared_texture_state(r: u32) -> bool { + matches!(r, reg::XFRCONTROL | reg::XFRSIZE | reg::XFRMODE | te_reg::TXADDR) + || super::te1::CONTEXT_REGS.contains(&r) + || super::te1::TABLE_REGS.contains(&r) + } + /// Draw only the scanlines `y` with `(y ^ engine) & mask == 0`. pub fn set_engine(&mut self, engine: u32, mask: u32) { self.engine = engine; @@ -1393,6 +1433,75 @@ impl Rss { self.jit.invalidate(); } + pub(super) fn snapshot_bytes(&self) -> Result, String> { + let snapshot = RssSnapshot { + version: 1, + regs: self.regs.to_vec(), + device: self.device.iter().collect(), + device_overflowed: self.device.overflowed, + line_buf: self.line_buf.to_vec(), + stipple: self.stipple.get(), + xfer: self.xfer.get(), + stats: self.stats, + gl_line_stipple_pos: self.gl_line_stipple_pos, + gl_stash: self.gl_stash.to_vec(), + fillmodes: self.fillmodes.to_vec(), + fillmodes_n: self.fillmodes_n, + stale_events: self.stale_events, + stale_last: self.stale_last, + te_load: self.te_load, + te_read: self.te_read, + }; + postcard::to_allocvec(&snapshot).map_err(|e| format!("encode RSS snapshot: {e}")) + } + + pub(super) fn decode_snapshot(bytes: &[u8]) -> Result { + let s: RssSnapshot = postcard::from_bytes(bytes).map_err(|e| format!("decode RSS snapshot: {e}"))?; + if s.version != 1 { + return Err(format!("RSS snapshot version {} is not supported", s.version)); + } + for (name, got, want) in [ + ("registers", s.regs.len(), 0x400), + ("line buffer", s.line_buf.len(), MAX_LINE_BYTES), + ("GL register stash", s.gl_stash.len(), 0x400), + ("fill-mode log", s.fillmodes.len(), 32), + ] { + if got != want { + return Err(format!("RSS {name}: {got} entries, expected {want}")); + } + } + if s.device.len() > 256 { + return Err(format!("RSS indirect device map has {} entries", s.device.len())); + } + if s.fillmodes_n > s.fillmodes.len() { + return Err(format!("RSS fill-mode count {} exceeds {}", s.fillmodes_n, s.fillmodes.len())); + } + Ok(s) + } + + pub(super) fn apply_snapshot(&mut self, s: RssSnapshot) { + self.regs.copy_from_slice(&s.regs); + self.device.clear(); + for (key, value) in s.device { + self.device.insert(key, value); + } + self.device.overflowed = s.device_overflowed; + self.line_buf.copy_from_slice(&s.line_buf); + self.stipple.set(s.stipple); + self.xfer.set(s.xfer); + self.stats = s.stats; + self.gl_line_stipple_pos = s.gl_line_stipple_pos; + self.gl_stash.copy_from_slice(&s.gl_stash); + self.fillmodes.copy_from_slice(&s.fillmodes); + self.fillmodes_n = s.fillmodes_n; + self.stale_events = s.stale_events; + self.stale_last = s.stale_last; + self.te_load = s.te_load; + self.te_read = s.te_read; + #[cfg(feature = "gr4-jit")] + self.jit.invalidate(); + } + /// Share `Te1` among `engines` engines (`te_write`), counting writes /// from zero. Call with the engines stopped, on each. pub fn share_te(&mut self, engines: u32) { @@ -1431,6 +1540,7 @@ impl Rss { wait.snooze(); } } + f(&mut self.te, &self.regs); self.te_sync.done.store(n, Ordering::Release); } else { @@ -2975,7 +3085,10 @@ impl Rss { /// PIO read, high half: takes the next doubleword. pub fn pio_read_hi(&mut self) -> u32 { - self.gpu_sync_transfer(); + let _ = self.gpu_sync_transfer( + #[cfg(feature = "wgpu")] + super::wgpu::Observation::PioRead, + ); let dw = self.pio_doubleword(); self.stats.pio_read_dw += 1; let Some(x) = self.xfer.as_mut() else { return 0 }; @@ -2989,29 +3102,60 @@ impl Rss { /// Bring the pixel memory and clip IDs current from the GPU unit, if /// one draws (its readers: PIO and DMA reads, hashes, the display). - pub fn gpu_sync(&mut self) { + pub fn gpu_sync( + &mut self, + #[cfg(feature = "wgpu")] reason: super::wgpu::Observation, + ) -> Result { #[cfg(feature = "wgpu")] if let Some(g) = self.gpu.get() { - if let Err(e) = g.sync(&mut self.mem, &mut self.cid) { - eprintln!("mgras: GPU raster unit: {e}"); - } + let result = g.sync(&mut self.mem, &mut self.cid, reason); + return self.gpu_result("mirror readback", result); } + Ok(0) } /// With the GPU unit: bring the mirror current where the armed read /// transfer reads (its block, in the buffer reads come from): the /// guest's pixel reads cost a readback of what they read, not of /// everything drawn since the last. - pub fn gpu_sync_transfer(&mut self) { + pub fn gpu_sync_transfer( + &mut self, + #[cfg(feature = "wgpu")] reason: super::wgpu::Observation, + ) -> Result { #[cfg(feature = "wgpu")] if let Some(g) = self.gpu.get() { - let Some(x) = self.xfer.get().filter(|x| x.read) else { return }; + let Some(x) = self.xfer.get().filter(|x| x.read) else { return Ok(0) }; let b = x.block; let (ax, ay) = self.to_fb(b.xs, b.ys); let (bx, by) = self.to_fb(b.xe, b.ye); let src = self.source(); - if let Err(e) = g.sync_box(&mut self.mem, &[src], (ax.min(bx), ay.min(by), ax.max(bx), ay.max(by))) { - eprintln!("mgras: GPU raster unit: {e}"); + let result = g.sync_box( + &mut self.mem, + &[src], + (ax.min(bx), ay.min(by), ax.max(bx), ay.max(by)), + reason, + ); + return self.gpu_result("transfer readback", result); + } + Ok(0) + } + + #[cfg(feature = "wgpu")] + fn gpu_result(&self, operation: &str, result: Result) -> Result { + match result { + Ok(bytes) => Ok(bytes), + Err(error) => { + let message = format!("{operation}: {error}"); + if let Some(fault) = unsafe { self.gpu_fault.as_ref() } { + let mut fault = fault.lock(); + if fault.is_none() { + eprintln!("mgras: GPU raster unit failed: {message}"); + *fault = Some(message.clone()); + } + Err(fault.as_ref().unwrap().clone()) + } else { + Err(message) + } } } } diff --git a/src/dev/mgras/te1.rs b/src/dev/mgras/te1.rs index fbbabdb28..9ddd0595a 100644 --- a/src/dev/mgras/te1.rs +++ b/src/dev/mgras/te1.rs @@ -60,6 +60,7 @@ //! texture. use super::fixed; +use serde::{Deserialize, Serialize}; /// TE1 / texture-related RSS registers. pub mod reg { @@ -258,7 +259,56 @@ pub struct Te1 { pub dirty: [u64; 4], } +#[derive(Serialize, Deserialize)] +pub(super) struct Te1Snapshot { + version: u32, + tram: Vec, + tables: Vec, + texels_loaded: u64, + owner: Vec, + dirty: [u64; 4], +} + impl Te1 { + pub(super) fn snapshot_bytes(&self) -> Result, String> { + let snapshot = Te1Snapshot { + version: 1, + tram: self.tram.to_vec(), + tables: self.tables.iter().flatten().copied().collect(), + texels_loaded: self.texels_loaded, + owner: self.owner.to_vec(), + dirty: self.dirty, + }; + postcard::to_allocvec(&snapshot).map_err(|e| format!("encode TE1 snapshot: {e}")) + } + + pub(super) fn decode_snapshot(bytes: &[u8]) -> Result { + let s: Te1Snapshot = postcard::from_bytes(bytes).map_err(|e| format!("decode TE1 snapshot: {e}"))?; + if s.version != 1 { + return Err(format!("TE1 snapshot version {} is not supported", s.version)); + } + if s.tram.len() != TRAM_BYTES { + return Err(format!("TE1 TRAM: {} bytes, expected {TRAM_BYTES}", s.tram.len())); + } + if s.tables.len() != 3 * TABLE_LEN { + return Err(format!("TE1 tables: {} words, expected {}", s.tables.len(), 3 * TABLE_LEN)); + } + if s.owner.len() != 256 { + return Err(format!("TE1 ownership table: {} entries, expected 256", s.owner.len())); + } + Ok(s) + } + + pub(super) fn apply_snapshot(&mut self, s: Te1Snapshot) { + self.tram.copy_from_slice(&s.tram); + for (dst, src) in self.tables.iter_mut().flatten().zip(s.tables) { + *dst = src; + } + self.texels_loaded = s.texels_loaded; + self.owner.copy_from_slice(&s.owner); + self.dirty = s.dirty; + } + /// TRAM, for the JIT's texel fetches. pub fn tram_ptr(&self) -> *const u8 { self.tram.as_ptr() diff --git a/src/dev/mgras/wgpu/mod.rs b/src/dev/mgras/wgpu/mod.rs index 28f98b0da..aa57a314b 100644 --- a/src/dev/mgras/wgpu/mod.rs +++ b/src/dev/mgras/wgpu/mod.rs @@ -451,6 +451,49 @@ const TABLE_WORDS: usize = 1024 + 256 + 256; /// TRAM pages, as the dirty map counts them (`Te1::dirty`). const TRAM_PAGES: usize = te1::TRAM_BYTES / (te1::PAGE_NIBBLES / 2); +#[derive(Clone, Copy)] +pub(crate) enum Observation { + DmaRead, + PioRead, + Snapshot, + Display, + HostComposite, + Debug, + StateHash, + Validation, +} + +impl Observation { + const COUNT: usize = 8; + const ALL: [Observation; Self::COUNT] = [ + Self::DmaRead, + Self::PioRead, + Self::Snapshot, + Self::Display, + Self::HostComposite, + Self::Debug, + Self::StateHash, + Self::Validation, + ]; + + fn index(self) -> usize { + self as usize + } + + fn name(self) -> &'static str { + match self { + Self::DmaRead => "dma-read", + Self::PioRead => "pio-read", + Self::Snapshot => "snapshot", + Self::Display => "display", + Self::HostComposite => "host-composite", + Self::Debug => "debug", + Self::StateHash => "state-hash", + Self::Validation => "validation", + } + } +} + /// A batch set's own buffers on the board's side and its group 2. struct RasterSet { stage: HostBuffer, @@ -488,6 +531,9 @@ pub(crate) struct GpuRaster { pub uploaded: u64, pub read_back: u64, pub syncs: u64, + observations: [u64; Observation::COUNT], + observation_bytes: [u64; Observation::COUNT], + fault: Option, } fn log2_q8_table() -> [u32; 256] { @@ -548,6 +594,9 @@ impl GpuRaster { uploaded: 0, read_back: 0, syncs: 0, + observations: [0; Observation::COUNT], + observation_bytes: [0; Observation::COUNT], + fault: None, }) } @@ -708,40 +757,52 @@ impl GpuRaster { /// `bufs` (a transfer's read): the pages there that drawing marked are /// read back, after a wait; the others stay marked. Nothing waits when /// none is marked. - pub fn sync_box(&mut self, mem: &mut PixMem, bufs: &[Buffer], b: (i32, i32, i32, i32)) -> Result<(), String> { + pub fn sync_box( + &mut self, + mem: &mut PixMem, + bufs: &[Buffer], + b: (i32, i32, i32, i32), + reason: Observation, + ) -> Result { let mut pages = Vec::new(); for buf in bufs { - let dirty = &mut self.dirty_pages; + let dirty = &self.dirty_pages; Self::buffer_pages(buf, b, |p| { if dirty[p / 64] & 1 << (p % 64) != 0 { - dirty[p / 64] &= !(1 << (p % 64)); pages.push(p); } }); } + pages.sort_unstable(); + pages.dedup(); if pages.is_empty() { - return Ok(()); + self.observe(reason, 0); + return Ok(0); } self.wait()?; self.syncs += 1; - pages.sort_unstable(); let page_bytes = (PAGE_WORDS * 8) as u64; let mut runs: Vec<(u64, u64)> = Vec::new(); - for p in pages { + for &p in &pages { match runs.last_mut() { Some(r) if r.0 + r.1 == p as u64 * page_bytes => r.1 += page_bytes, _ => runs.push((p as u64 * page_bytes, page_bytes)), } } - let bytes = self.pix.read_ranges(&runs)?; + let read = self.pix.read_ranges(&runs); + let bytes = self.remember(read)?; + for p in pages { + self.dirty_pages[p / 64] &= !(1 << (p % 64)); + } self.read_back += bytes.len() as u64; + self.observe(reason, bytes.len() as u64); let mut at = 0usize; for (off, len) in runs { let w0 = off as usize / 8; mem.words[w0..w0 + len as usize / 8].copy_from_slice(bytemuck::cast_slice(&bytes[at..at + len as usize])); at += len as usize; } - Ok(()) + Ok(bytes.len() as u64) } /// Note the pages and clip-ID rows a record may write. @@ -833,52 +894,62 @@ impl GpuRaster { /// Submit and wait until everything recorded is stored. pub fn wait(&mut self) -> Result<(), String> { - self.flush(); - if let Some(sub) = self.pending.take() { - self.gpu.wait(Some(sub))?; + if let Some(error) = &self.fault { + return Err(error.clone()); } - self.gpu.check() + self.flush(); + let result = if let Some(sub) = self.pending.take() { + self.gpu.wait(Some(sub)).and_then(|()| self.gpu.check()) + } else { + self.gpu.check() + }; + self.remember(result) } /// Bring the CPU's mirror current: wait, then read back the pages and /// clip-ID rows drawing may have changed since the last sync. - pub fn sync(&mut self, mem: &mut PixMem, cid: &mut CidPlanes) -> Result<(), String> { + pub fn sync( + &mut self, + mem: &mut PixMem, + cid: &mut CidPlanes, + reason: Observation, + ) -> Result { let any = self.dirty_pages.iter().chain(&self.dirty_cid).any(|&w| w != 0); if !any && self.batch.is_empty() { - return Ok(()); + self.observe(reason, 0); + return Ok(0); } self.wait()?; self.syncs += 1; let page_bytes = (PAGE_WORDS * 8) as u64; + let mut pages = Vec::new(); let mut runs: Vec<(u64, u64)> = Vec::new(); for i in 0..self.dirty_pages.len() { - let mut bits = std::mem::take(&mut self.dirty_pages[i]); + let mut bits = self.dirty_pages[i]; while bits != 0 { let p = (i * 64) as u64 + bits.trailing_zeros() as u64; bits &= bits - 1; + pages.push(p as usize); match runs.last_mut() { Some(r) if r.0 + r.1 == p * page_bytes => r.1 += page_bytes, _ => runs.push((p * page_bytes, page_bytes)), } } } - if !runs.is_empty() { - let bytes = self.pix.read_ranges(&runs)?; - self.read_back += bytes.len() as u64; - let mut at = 0usize; - for (off, len) in runs { - let w0 = off as usize / 8; - let n = len as usize / 8; - mem.words[w0..w0 + n].copy_from_slice(bytemuck::cast_slice(&bytes[at..at + len as usize])); - at += len as usize; - } - } + let pixel_bytes = if runs.is_empty() { + Vec::new() + } else { + let read = self.pix.read_ranges(&runs); + self.remember(read)? + }; + let mut dirty_rows = Vec::new(); let mut rows: Vec<(u64, u64)> = Vec::new(); for i in 0..self.dirty_cid.len() { - let mut bits = std::mem::take(&mut self.dirty_cid[i]); + let mut bits = self.dirty_cid[i]; while bits != 0 { let y = (i * 64) as u64 + bits.trailing_zeros() as u64; bits &= bits - 1; + dirty_rows.push(y as usize); let row = WIDTH as u64; match rows.last_mut() { Some(r) if r.0 + r.1 == y * row => r.1 += row, @@ -886,16 +957,69 @@ impl GpuRaster { } } } + let cid_bytes = if rows.is_empty() { + Vec::new() + } else { + let read = self.cid.read_ranges(&rows); + self.remember(read)? + }; + for p in pages { + self.dirty_pages[p / 64] &= !(1 << (p % 64)); + } + for y in dirty_rows { + self.dirty_cid[y / 64] &= !(1 << (y % 64)); + } + if !runs.is_empty() { + let mut at = 0usize; + for (off, len) in runs { + let w0 = off as usize / 8; + let n = len as usize / 8; + mem.words[w0..w0 + n].copy_from_slice(bytemuck::cast_slice(&pixel_bytes[at..at + len as usize])); + at += len as usize; + } + } if !rows.is_empty() { - let bytes = self.cid.read_ranges(&rows)?; - self.read_back += bytes.len() as u64; let mut at = 0usize; for (off, len) in rows { - cid[off as usize..(off + len) as usize].copy_from_slice(&bytes[at..at + len as usize]); + cid[off as usize..(off + len) as usize].copy_from_slice(&cid_bytes[at..at + len as usize]); at += len as usize; } } - Ok(()) + let bytes = (pixel_bytes.len() + cid_bytes.len()) as u64; + self.read_back += bytes; + self.observe(reason, bytes); + Ok(bytes) + } + + fn remember(&mut self, result: Result) -> Result { + match result { + Ok(value) => Ok(value), + Err(error) => { + if self.fault.is_none() { + self.fault = Some(error.clone()); + } + Err(self.fault.as_ref().unwrap().clone()) + } + } + } + + fn observe(&mut self, reason: Observation, bytes: u64) { + self.observations[reason.index()] += 1; + self.observation_bytes[reason.index()] += bytes; + } + + pub fn observation_counts(&self) -> impl Iterator + '_ { + Observation::ALL.into_iter().map(|reason| { + ( + reason.name(), + self.observations[reason.index()], + self.observation_bytes[reason.index()], + ) + }) + } + + pub fn fault(&self) -> Option<&str> { + self.fault.as_deref() } /// Pixel memory words `range`, after a wait (tests, hashes). diff --git a/src/dev/mgras/wgpu/tests.rs b/src/dev/mgras/wgpu/tests.rs index 4a0f319c8..2f7befffe 100644 --- a/src/dev/mgras/wgpu/tests.rs +++ b/src/dev/mgras/wgpu/tests.rs @@ -17,7 +17,7 @@ use super::super::pixmem::{PAGE_WORDS, RES, RE_WORDS}; use super::super::rss::{self, reg, Rss, WIDTH}; use super::super::te1::reg as te; use super::super::test_gen::{self, hilo, pos, Prim, Regs, Rng}; -use super::{xline_step, GpuLink, GpuRaster}; +use super::{xline_step, GpuLink, GpuRaster, Observation}; use crate::wgpu::{Gpu, GpuOptions}; fn gpu() -> Option> { @@ -84,6 +84,7 @@ impl Pair { cpu.jit.mode = super::super::rss_jit::MODE_OFF; gpu.jit.mode = super::super::rss_jit::MODE_OFF; } + let mut rng = Rng(seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1); for b in cpu.te.tram_mut().iter_mut() { *b = rng.next() as u8; @@ -273,6 +274,27 @@ impl Pair { } } +#[test] +fn observation_accounting_and_faults_are_sticky() { + let Some(g) = gpu() else { return }; + let mut p = Pair::new(&g, 1); + assert_eq!( + p.unit.sync(&mut p.gpu.mem, &mut p.gpu.cid, Observation::Validation).unwrap(), + 0 + ); + assert_eq!( + p.unit.observation_counts().find(|(name, _, _)| *name == "validation"), + Some(("validation", 1, 0)) + ); + + p.unit.dirty_pages[0] = 1; + let dirty = p.unit.dirty_pages.clone(); + p.unit.fault = Some("injected readback failure".into()); + assert!(p.unit.sync(&mut p.gpu.mem, &mut p.gpu.cid, Observation::Snapshot).is_err()); + assert_eq!(p.unit.dirty_pages, dirty, "a failed readback keeps every dirty page"); + assert_eq!(p.unit.fault(), Some("injected readback failure")); +} + /// `MGRAS_GPU_CHECK_EACH=1`: compare after every primitive, not every step. fn each() -> bool { std::env::var("MGRAS_GPU_CHECK_EACH").is_ok() @@ -676,7 +698,7 @@ fn transfer_reads_see_the_drawing() { p.exec(reg::IR, 0x8); for line in 0..p.rng.range(1, 8) as u32 { let want = p.cpu.dma_read_line(line); - p.gpu.gpu_sync_transfer(); + p.gpu.gpu_sync_transfer(Observation::DmaRead).unwrap(); let got = p.gpu.dma_read_line(line); assert_eq!(got, want, "step {step}, line {line}: the read differs\n{}", p.recent()); lines_read += 1; diff --git a/src/dev/ng1/rex3.rs b/src/dev/ng1/rex3.rs index 3f964cb8f..662a2e390 100644 --- a/src/dev/ng1/rex3.rs +++ b/src/dev/ng1/rex3.rs @@ -24,10 +24,12 @@ use std::io::Write; #[cfg(all(test, feature = "wgpu"))] #[path = "rex3_plane.rs"] mod plane; -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] #[path = "rex3_frame.rs"] pub(super) mod frame; -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] +pub(crate) use frame::ResidentDisplayMode; +#[cfg(feature = "wgpu")] #[path = "rex3_lifecycle.rs"] mod lifecycle; #[cfg(all(test, feature = "wgpu"))] @@ -55,9 +57,9 @@ pub const GFIFO_EXIT: u32 = 0xFFFF_0001; /// Consumer advances gfifo_fence to the payload value when it processes this entry. /// No GO bit set, so process_register receives this value directly as reg_offset. pub const GFIFO_DISP_SYNC: u32 = 0xFFFF_0002; -/// Internal test-only stream cut; the processor pauses at this entry until +/// Internal resident stream cut; the processor pauses at this entry until /// the marked scanout and its consumers have been submitted. -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] const GFIFO_RESIDENT_FRAME: u32 = 0xFFFF_0004; /// Internal-only offset (not a real REX3 register — top of the 8KB window, well past /// any hardware register) used as a "pure GO, no register write" alias for `dma_read64`/ @@ -1117,6 +1119,17 @@ unsafe impl Send for Rex3Context {} unsafe impl Sync for Rex3Context {} impl Rex3Context { + unsafe fn write_power_on(dst: *mut Self) { + // Rex3Context is plain integer/bitfield state plus a nullable pointer; + // initialize the 1 MiB HOSTRW tail in place so reset works on ordinary + // service-thread stacks. + unsafe { + std::ptr::write_bytes(dst, 0, 1); + (*dst).drawmode1 = DrawMode1(0x7 << 12); + (*dst).host_len = 1; + } + } + /// Power-on/reset state: like `Default::default()`, but with DRAWMODE1.COMPARE /// at its documented hardware reset value (0x7 = afunction disabled/always-pass). /// `#[derive(Default)]` zero-inits DrawMode1, which would leave COMPARE=0x0 @@ -1320,24 +1333,24 @@ struct GoDamage { rect: DamageRect, } -#[cfg(any(feature = "rex-gpu", all(test, feature = "wgpu")))] +#[cfg(any(feature = "rex-gpu", feature = "wgpu"))] #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum RasterModeKind { Cpu, #[cfg(feature = "rex-gpu")] LegacyReplay, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] Resident, } -#[cfg(any(feature = "rex-gpu", all(test, feature = "wgpu")))] +#[cfg(any(feature = "rex-gpu", feature = "wgpu"))] impl RasterModeKind { fn name(self) -> &'static str { match self { Self::Cpu => "CPU raster", #[cfg(feature = "rex-gpu")] Self::LegacyReplay => "GPU replay", - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] Self::Resident => "resident raster", } } @@ -1353,23 +1366,23 @@ impl RasterModeKind { } } -#[cfg(any(feature = "rex-gpu", all(test, feature = "wgpu")))] +#[cfg(any(feature = "rex-gpu", feature = "wgpu"))] enum RasterMode { Cpu, #[cfg(feature = "rex-gpu")] LegacyReplay(crate::dev::ng1::rex3_gpu::Rex3Gpu), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] Resident(crate::dev::ng1::wgpu::resident::ResidentRaster), } -#[cfg(any(feature = "rex-gpu", all(test, feature = "wgpu")))] +#[cfg(any(feature = "rex-gpu", feature = "wgpu"))] impl RasterMode { fn kind(&self) -> RasterModeKind { match self { Self::Cpu => RasterModeKind::Cpu, #[cfg(feature = "rex-gpu")] Self::LegacyReplay(_) => RasterModeKind::LegacyReplay, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] Self::Resident(_) => RasterModeKind::Resident, } } @@ -1402,16 +1415,18 @@ pub struct Rex3 { // Each bit of a pixel represents a plane. pub fb_rgb: UnsafeCell, pub fb_aux: UnsafeCell, - #[cfg(any(feature = "rex-gpu", all(test, feature = "wgpu")))] + #[cfg(any(feature = "rex-gpu", feature = "wgpu"))] raster: Mutex, fatal_sender: Mutex>>, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_fatal_notified: AtomicBool, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_frame: frame::ResidentFrameControl, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] + resident_display_mode: Mutex>, + #[cfg(feature = "wgpu")] resident_lifecycle: RwLock, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_telemetry: Mutex, pub gfifo: GFifo, @@ -1436,7 +1451,7 @@ pub struct Rex3 { /// Handle to the REX3-Processor thread, set once when it starts, used by /// `gfifo_push` to unpark it. OnceLock gives lock-free reads on the hot path. processor_unparker: std::sync::OnceLock, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_unparker: Mutex>, /// Original coarse dirty flag, retained verbatim when partial uploads are /// not compiled in. @@ -1619,16 +1634,18 @@ impl Rex3 { context: UnsafeCell::new(Rex3Context::power_on_default()), fb_rgb: UnsafeCell::new(fb_rgb), fb_aux: UnsafeCell::new(fb_aux), - #[cfg(any(feature = "rex-gpu", all(test, feature = "wgpu")))] + #[cfg(any(feature = "rex-gpu", feature = "wgpu"))] raster: Mutex::new(RasterMode::Cpu), fatal_sender: Mutex::new(None), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_fatal_notified: AtomicBool::new(false), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_frame: frame::ResidentFrameControl::default(), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] + resident_display_mode: Mutex::new(None), + #[cfg(feature = "wgpu")] resident_lifecycle: RwLock::new(lifecycle::ResidentLifecycle::default()), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_telemetry: Mutex::new(crate::dev::ng1::wgpu::telemetry::Board::default()), gfifo: GFifo::new(), vc2: Mutex::new(Vc2::new()), @@ -1645,7 +1662,7 @@ impl Rex3 { refresh_thread: Mutex::new(None), processor_parked: AtomicBool::new(false), processor_unparker: std::sync::OnceLock::new(), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_unparker: Mutex::new(None), #[cfg(not(feature = "rex-dirty-upload"))] fb_dirty: AtomicBool::new(true), @@ -1776,7 +1793,7 @@ impl Rex3 { /// The caller serializes producers and lifecycle transitions. A stopped /// worker and an empty, fully published FIFO are the only boundary at /// which the context and both authoritative planes can be snapshotted. - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn check_resident_quiescent( &self, ) -> Result<(), crate::dev::ng1::wgpu::resident::ResidentError> { @@ -1807,7 +1824,7 @@ impl Rex3 { Ok(()) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn resident_active(&self) -> bool { self.resident_frame.is_active() } @@ -1832,7 +1849,7 @@ impl Rex3 { /// Seed both CPU planes on the supplied common device before making the /// resident backend visible to the GFIFO consumer. - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn activate_resident_gpu( &mut self, gpu: Arc, @@ -1840,7 +1857,24 @@ impl Rex3 { self.activate_resident_gpu_with_profiling(gpu, false) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] + pub(crate) fn activate_resident_gpu_for_runtime( + &mut self, + gpu: Arc, + mode: frame::ResidentDisplayMode, + ) -> Result<(), crate::dev::ng1::wgpu::resident::ResidentError> { + use crate::dev::ng1::wgpu::resident::ResidentError; + if std::env::var_os("IRIS_REX3_GPU").is_some() { + return Err(ResidentError::InvalidState( + "IRIS_REX3_GPU and production Newport wgpu raster are mutually exclusive", + )); + } + self.activate_resident_gpu(gpu)?; + *self.resident_display_mode.get_mut() = Some(mode); + Ok(()) + } + + #[cfg(feature = "wgpu")] pub(crate) fn activate_resident_gpu_with_profiling( &mut self, gpu: Arc, @@ -1865,14 +1899,14 @@ impl Rex3 { /// Test seam retained for existing callers; power-on uses the same /// stopped resident lifecycle. - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn reset_resident_gpu( &mut self, ) -> Result<(), crate::dev::ng1::wgpu::resident::ResidentError> { self.reset_resident_board() } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn try_resident_draw( &self, context: &mut Rex3Context, @@ -1908,7 +1942,7 @@ impl Rex3 { result } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn resident_ready(&self) -> bool { if let RasterMode::Resident(raster) = &*self.raster.lock() { return raster.fault().is_none(); @@ -1916,7 +1950,7 @@ impl Rex3 { true } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn submit_resident( &self, reason: crate::dev::ng1::wgpu::telemetry::FlushReason, @@ -1931,7 +1965,7 @@ impl Rex3 { result } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn resident_status( &self, ) -> Result { @@ -1946,14 +1980,14 @@ impl Rex3 { result.unwrap_or(Err(ResidentError::InvalidState("resident raster is not active"))) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn resident_telemetry_snapshot( &self, ) -> Result { self.resident_snapshot_from_status(self.resident_status()?) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn resident_telemetry_diagnostics( &self, ) -> Result< @@ -1968,7 +2002,7 @@ impl Rex3 { Ok((self.resident_snapshot_from_status(diagnostics.status)?, diagnostics.fault)) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn resident_snapshot_from_status( &self, status: crate::dev::ng1::wgpu::resident::ResidentStatus, @@ -1998,7 +2032,7 @@ impl Rex3 { Ok(Snapshot { epoch: status.accepted.epoch.value(), workload: status.workload, board, traffic }) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn flush_resident_if_due(&self) { if !self.resident_frame.is_active() { return; } let result = match &mut *self.raster.lock() { @@ -2010,7 +2044,7 @@ impl Rex3 { } } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn poll_resident_device_health(&self) { let result = match &mut *self.raster.lock() { RasterMode::Resident(raster) => Some(raster.poll_device_health()), @@ -2021,7 +2055,7 @@ impl Rex3 { } } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn resident_fault(&self) -> Option { match &*self.raster.lock() { RasterMode::Resident(raster) => raster.fault().cloned(), @@ -2029,7 +2063,7 @@ impl Rex3 { } } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn fail_resident(&self, error: crate::dev::ng1::wgpu::resident::ResidentError) { let error = match &mut *self.raster.lock() { RasterMode::Resident(raster) => { @@ -2048,7 +2082,7 @@ impl Rex3 { } } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn start_resident_processor( &self, ) -> Result<(), crate::dev::ng1::wgpu::resident::ResidentError> { @@ -2069,7 +2103,7 @@ impl Rex3 { self.running.store(true, Ordering::SeqCst); let rex3 = unsafe { std::mem::transmute::<&Rex3, &'static Rex3>(self) }; unsafe { (*self.context.get()).host = rex3 as *const Rex3; } - match thread::Builder::new().name("REX3-Resident-Processor-Test".into()) + match thread::Builder::new().name("REX3-Resident-Processor".into()) .spawn(move || { if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| rex3.register_processor())).is_err() { rex3.fail_resident(ResidentError::Failed("resident GFIFO worker panicked".into())); @@ -2087,9 +2121,9 @@ impl Rex3 { Ok(()) } - /// The test-only direct compositor binds the resident planes as part of - /// its same-device, read-only scanout group. - #[cfg(all(test, feature = "wgpu"))] + /// The direct compositor binds the resident planes as part of its + /// same-device, read-only scanout group. + #[cfg(feature = "wgpu")] pub(super) fn resident_scanout_bindings( &self, gpu: &Arc, @@ -2104,10 +2138,10 @@ impl Rex3 { } } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn reject_resident_operation(&self, operation: &str) -> Result<(), String> { if self.raster.lock().kind() == RasterModeKind::Resident { - Err(format!("REX3 resident test raster does not support {operation}")) + Err(format!("REX3 resident raster does not support {operation}")) } else { Ok(()) } @@ -2159,7 +2193,7 @@ impl Rex3 { /// GPU work is synchronized first, and the next GPU draw re-uploads the /// plane, so neither copy can overwrite the other. fn with_cpu_rgb_write(&self, write: impl FnOnce(&mut [u32])) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.reject_resident_operation("CPU framebuffer writes").unwrap(); #[cfg(feature = "rex-gpu")] let mut mode = self.raster.lock(); @@ -2289,7 +2323,7 @@ impl Rex3 { /// Program VC2 with a host-side Newport timing preset (see `[graphics] resolution`). pub fn apply_display_resolution(&self, mode: crate::dev::ng1::vc2_timings::NewportResolution) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_active() { self.apply_resident_display_resolution(mode, false) .unwrap_or_else(|error| panic!("REX3 {error}")); @@ -2505,6 +2539,21 @@ impl Rex3 { #[inline(always)] fn draw_debug_active(&self) -> bool { false } + fn ensure_cpu_overlay_available(&self, enable: bool, overlay: &str) -> Result<(), String> { + #[cfg(feature = "wgpu")] + if enable + && self.resident_frame.is_active() + && *self.resident_display_mode.lock() == Some(ResidentDisplayMode::DirectWgpu) + { + return Err(format!( + "{overlay} requires CPU display planes and is unavailable in resident direct mode" + )); + } + #[cfg(not(feature = "wgpu"))] + let _ = (enable, overlay); + Ok(()) + } + /// Record a HOSTRW write/read against the pending draw-debug entry. No-op in /// non-developer builds. #[cfg(feature = "developer")] @@ -2942,7 +2991,7 @@ impl Rex3 { } 4 | 5 | 6 => { // XMAP let write_xmap = |crs: u8, v: u32| { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let fence = { // Publish the fence while holding the same XMAP lock used // by resident display snapshots; enqueue it after releasing @@ -2952,12 +3001,12 @@ impl Rex3 { (crs == crate::dev::ng1::xmap9::XMAP9_REG_MODE_TABLE_WRITE) .then(|| self.xmap_fence.fetch_add(1, Ordering::AcqRel).wrapping_add(1)) }; - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] if addr == 4 || addr == 5 { self.xmap0.lock().write_crs(crs, v); } if addr == 4 || addr == 6 { self.xmap1.lock().write_crs(crs, v); } // Mode table write (CRS 5) = buf_sel flip: push a DISP_SYNC fence // so the display thread waits for all prior draws before snapshotting. - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let fence = (crs == crate::dev::ng1::xmap9::XMAP9_REG_MODE_TABLE_WRITE) .then(|| self.xmap_fence.fetch_add(1, Ordering::Relaxed) + 1); if let Some(fence) = fence { @@ -3163,7 +3212,7 @@ impl Rex3 { if !self.processor_parked.load(Ordering::Acquire) { return; } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { if let Some(t) = self.resident_unparker.lock().as_ref() { t.unpark(); @@ -3176,7 +3225,7 @@ impl Rex3 { } fn gfifo_push(&self, addr: u32, val: u64) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { while !self.gfifo_try_push(addr, val) { std::hint::spin_loop(); @@ -3214,7 +3263,7 @@ impl Rex3 { if len > hwm { Some(len) } else { None } }); } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let accepted = if self.resident_frame.is_active() { let before = self.gfifo.len(); let result = self.gfifo.try_push_traced(addr, val); @@ -3223,7 +3272,7 @@ impl Rex3 { } else { self.gfifo.try_push(addr, val) }; - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let accepted = self.gfifo.try_push(addr, val); if !accepted { return false; @@ -3252,7 +3301,7 @@ impl Rex3 { if len > hwm { Some(len) } else { None } }); } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let accepted = if self.resident_frame.is_active() { let before = self.gfifo.len(); let result = self.gfifo.try_push2_traced(addr0, val0, addr1, val1); @@ -3261,7 +3310,7 @@ impl Rex3 { } else { self.gfifo.try_push2(addr0, val0, addr1, val1) }; - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let accepted = self.gfifo.try_push2(addr0, val0, addr1, val1); if !accepted { return false; @@ -3283,7 +3332,7 @@ impl Rex3 { if len > hwm { Some(len) } else { None } }); } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let accepted = if self.resident_frame.is_active() { let before = self.gfifo.len(); let result = self.gfifo.try_push_batch_traced(token, vals.len() as u64, vals); @@ -3292,7 +3341,7 @@ impl Rex3 { } else { self.gfifo.try_push_batch(token, vals.len() as u64, vals) }; - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let accepted = self.gfifo.try_push_batch(token, vals.len() as u64, vals); if !accepted { return false; @@ -3301,13 +3350,13 @@ impl Rex3 { true } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn note_resident_reservation(&self, before: usize, result: Result<(), crate::gfifo::PushFailure>) { let occupancy = before.max(self.gfifo.len()); self.resident_telemetry.lock().reservation(occupancy, result); } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn note_resident_bus_busy(&self) { if self.resident_frame.is_active() { self.resident_telemetry.lock().bus_busy(); @@ -3329,7 +3378,7 @@ impl Rex3 { } } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn wait_resident_idle( &self, ) -> Result<(), crate::dev::ng1::wgpu::resident::ResidentError> { @@ -3356,7 +3405,7 @@ impl Rex3 { } pub(crate) fn wait_idle(&self) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.raster.lock().kind() == RasterModeKind::Resident { self.wait_resident_idle().unwrap_or_else(|error| panic!("REX3 {error}")); return; @@ -3368,7 +3417,7 @@ impl Rex3 { // A resident DMA read can return BUS_BUSY only before publishing its GO. fn queue_dma_host_read(&self, token: u32, count: u64) -> u32 { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] { let mode = self.raster.lock(); if let RasterMode::Resident(raster) = &*mode { @@ -3390,7 +3439,7 @@ impl Rex3 { // Unlike wait_idle, a resident failure must reach the bus rather than panic. fn wait_dma_host_read(&self) -> bool { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.raster.lock().kind() == RasterModeKind::Resident { return self.wait_resident_idle().is_ok(); } @@ -3400,13 +3449,13 @@ impl Rex3 { fn register_processor(&self) { // Publish our thread handle so gfifo_push can unpark us when we park. - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { *self.resident_unparker.lock() = Some(thread::current()); } else { let _ = self.processor_unparker.set(thread::current()); } - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let _ = self.processor_unparker.set(thread::current()); let backoff = crossbeam_utils::Backoff::new(); let mut is_busy = false; @@ -3414,11 +3463,11 @@ impl Rex3 { if let Some((addr, val)) = self.gfifo.peek() { backoff.reset(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if addr == GFIFO_RESIDENT_FRAME { self.process_resident_frame_marker(val); self.gfifo.consume(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.flush_resident_if_due(); continue; } @@ -3459,7 +3508,7 @@ impl Rex3 { //self.update_gfifo_irqs(); if exit { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let _ = self.submit_resident(crate::dev::ng1::wgpu::telemetry::FlushReason::Exit); #[cfg(feature = "rex-gpu")] self.flush_gpu_replay(FlushReason::Exit); @@ -3467,11 +3516,11 @@ impl Rex3 { self.gfifo.flush_head(); break; } // GFIFO_EXIT - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.flush_resident_if_due(); } else { if is_busy { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let _ = self.submit_resident(crate::dev::ng1::wgpu::telemetry::FlushReason::Idle); #[cfg(feature = "rex-gpu")] self.flush_gpu_replay(FlushReason::Idle); @@ -3488,7 +3537,7 @@ impl Rex3 { // CPU at ~100%. Tests leak their Rex3 and its processor thread, // so spinning ones also starve a CI runner. if backoff.is_completed() { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.poll_resident_device_health(); // Set parked BEFORE the final emptiness re-check so a racing // gfifo_push either (a) is seen by the peek below, or (b) sees @@ -3516,7 +3565,7 @@ impl Rex3 { /// current pattern bit clear; the guest's ZPATTERN, and its bit position /// when it had not enabled the pattern itself, are put back afterwards. pub(crate) fn execute_stepz_go(&self) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if !self.resident_ready() { return; } let (dm0, zpattern, bit, had) = { let ctx = unsafe { &mut *self.context.get() }; @@ -3539,7 +3588,7 @@ impl Rex3 { } pub(crate) fn execute_go(&self) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if !self.resident_ready() { return; } #[cfg(feature = "rexdiag")] self.diag.fetch_or(Self::DIAG_LOOP_EXECUTE_GO, Ordering::Relaxed); @@ -3606,7 +3655,7 @@ impl Rex3 { ctx.xend as f32 / 2048.0, ctx.yend as f32 / 2048.0); } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if let Some(result) = self.try_resident_draw(ctx) { #[cfg(feature = "rexdiag")] self.diag @@ -3998,7 +4047,7 @@ impl Rex3 { // Resident tests submit the prefix here; actual GPU completion // is tracked separately from this ordered submission fence. GFIFO_DISP_SYNC => { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if matches!(self.submit_resident(crate::dev::ng1::wgpu::telemetry::FlushReason::DisplayFence), Some(Err(_))) { // A failed prefix is not a completed display fence. return false; @@ -4226,7 +4275,7 @@ impl Rex3 { /// Save RGB/aux planes with a custom filename prefix (e.g. `rex3_head1`). pub fn save_framebuffers_named(&self, dir: &std::path::Path, prefix: &str) -> std::io::Result<()> { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { return self.save_resident_framebuffers(dir, prefix); } @@ -4260,7 +4309,7 @@ impl Rex3 { /// buffers. Pair with `restore_framebuffers_inmem` for the in-memory /// rollback checkpoint; bypasses the byte-shuffle the disk path needs. pub fn snapshot_framebuffers_inmem(&self) -> (Vec, Vec) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { return self.snapshot_resident_planes().unwrap_or_else(|error| panic!("REX3 {error}")); } @@ -4273,11 +4322,10 @@ impl Rex3 { /// Restore framebuffers from buffers captured by /// `snapshot_framebuffers_inmem`. Lengths are clamped to the actual /// framebuffer size. - pub fn restore_framebuffers_inmem(&self, rgb: &[u32], aux: &[u32]) { - #[cfg(all(test, feature = "wgpu"))] + pub(crate) fn try_restore_framebuffers_inmem(&self, rgb: &[u32], aux: &[u32]) -> Result<(), String> { + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { - self.stage_resident_planes(rgb, aux).unwrap_or_else(|error| panic!("REX3 {error}")); - return; + return self.stage_resident_planes(rgb, aux).map_err(|error| error.to_string()); } self.wait_idle(); let dst_rgb = unsafe { &mut *self.fb_rgb.get() }; @@ -4297,6 +4345,12 @@ impl Rex3 { if n != 0 { self.mark_framebuffer_full(DamagePlane::Aux); } + Ok(()) + } + + pub fn restore_framebuffers_inmem(&self, rgb: &[u32], aux: &[u32]) { + self.try_restore_framebuffers_inmem(rgb, aux) + .unwrap_or_else(|error| panic!("REX3 {error}")); } pub fn load_framebuffers(&self, dir: &std::path::Path) -> std::io::Result<()> { @@ -4308,7 +4362,7 @@ impl Rex3 { /// Unlike `save_framebuffers_named`, this also captures the decoded DID /// plane and renders PNG previews — for `rex fbdump`, not snapshotting. pub fn dump_framebuffer_debug(&self, dir: &std::path::Path) -> std::io::Result<()> { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.reject_resident_operation("framebuffer dumps").map_err(std::io::Error::other)?; const W: usize = 2048; const H: usize = 1024; @@ -4347,7 +4401,7 @@ impl Rex3 { } pub fn load_framebuffers_named(&self, dir: &std::path::Path, prefix: &str) -> std::io::Result<()> { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { return self.load_resident_framebuffers(dir, prefix); } @@ -4431,7 +4485,7 @@ impl Device for Rex3 { } fn stop(&self) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.raster.lock().kind() == RasterModeKind::Resident { let _lifecycle = self.resident_lifecycle.write(); self.head.display_stop().request(); @@ -4531,9 +4585,22 @@ impl Device for Rex3 { } fn start(&self) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.raster.lock().kind() == RasterModeKind::Resident { self.start_resident_processor().unwrap_or_else(|error| panic!("REX3 {error}")); + if let Some(mode) = *self.resident_display_mode.lock() { + if self.refresh_thread.lock().is_none() { + let gpu = match &*self.raster.lock() { + RasterMode::Resident(raster) => raster.gpu(), + _ => None, + } + .expect("production resident raster must own a GPU"); + if let Err(error) = self.start_resident_display(gpu, mode) { + self.stop(); + panic!("REX3 {error}"); + } + } + } return; } if self.running.swap(true, Ordering::SeqCst) { return; } @@ -4633,7 +4700,7 @@ impl Device for Rex3 { return Err(format!("Usage: {} debug | {} status", cmd, cmd)); } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let _resident_monitor = self.resident_monitor_guard()?; if cmd == "rex" && args[0] == "status" { @@ -4721,6 +4788,37 @@ impl Device for Rex3 { } #[cfg(not(feature = "rex-jit"))] writeln!(writer, "JIT : not compiled in").unwrap(); + #[cfg(feature = "wgpu")] + if self.resident_active() { + let display_mode = *self.resident_display_mode.lock(); + let mode = match display_mode { + Some(ResidentDisplayMode::DirectWgpu) => "direct", + Some(ResidentDisplayMode::ReadbackCompatibility) => "readback", + None => "manual", + }; + let gpu = match &*self.raster.lock() { + RasterMode::Resident(raster) => raster.gpu(), + _ => None, + }; + match gpu { + Some(gpu) => writeln!( + writer, + "RESIDENT : mode={mode} backend={:?} adapter={} no_cpu_fallback=true", + gpu.caps.backend, + gpu.caps.adapter, + ), + None if display_mode.is_none() => writeln!( + writer, + "RESIDENT : mode=manual backend=manual adapter=none no_cpu_fallback=true", + ), + None => return Err("production resident raster has no GPU".to_owned()), + } + .map_err(|error| error.to_string())?; + let telemetry = self.resident_telemetry_snapshot() + .map_err(|error| error.to_string())?; + writeln!(writer, "TELEMETRY : {telemetry}") + .map_err(|error| error.to_string())?; + } return Ok(()); } @@ -4788,6 +4886,7 @@ impl Device for Rex3 { Some("off") => false, _ => return Err("Usage: rex cmap ".to_string()), }; + self.ensure_cpu_overlay_available(val, "CMAP overlay")?; self.show_cmap.store(val, Ordering::Relaxed); writeln!(writer, "CMAP overlay {}", if val { "enabled" } else { "disabled" }).unwrap(); return Ok(()); @@ -4938,6 +5037,7 @@ impl Device for Rex3 { Some("off") => false, _ => return Err("Usage: disp debug ".to_string()), }; + self.ensure_cpu_overlay_available(val, "display debug overlay")?; self.show_disp_debug.store(val, Ordering::Relaxed); writeln!(writer, "Display debug overlay {}", if val { "enabled" } else { "disabled" }).unwrap(); return Ok(()); @@ -5021,6 +5121,7 @@ impl Device for Rex3 { } #[cfg(feature = "developer")] "draw" => { + self.ensure_cpu_overlay_available(val, "draw debug overlay")?; self.draw_debug.store(val, Ordering::Relaxed); if !val { self.draw_ring.lock().count = 0; } writeln!(writer, "Draw debug overlay {}", if val { "enabled" } else { "disabled" }).unwrap(); @@ -5065,7 +5166,7 @@ impl Rex3 { let reg_offset = offset & !0x0800; let is_go = offset & 0x0800 != 0; // A failed worker may leave the FIFO permanently busy. - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if matches!(reg_offset, REX3_HOSTRW0 | REX3_HOSTRW1) && self.resident_fatal_notified.load(Ordering::Acquire) { @@ -5082,12 +5183,12 @@ impl Rex3 { // Contention here means a producer is mid-push, which is itself // "not empty" — report busy and let the CPU retry. if !self.gfifo.publish_tail() { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.note_resident_bus_busy(); return BusRead32::busy(); } if self.gfxbusy.load(Ordering::Acquire) || !self.gfifo.is_empty() { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.note_resident_bus_busy(); return BusRead32::busy(); } @@ -5236,7 +5337,7 @@ impl Rex3 { } }; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if matches!(reg_offset, REX3_HOSTRW0 | REX3_HOSTRW1) && !self.resident_ready() { return BusRead32::err(); } @@ -5392,7 +5493,7 @@ impl Rex3 { // as the queue stays full. if !self.gfifo_try_push(offset, val as u64) { crate::pf_level!(self.pf_fifo_full, crate::perfetto::events::REX3_FIFO_FULL, true); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.note_resident_bus_busy(); return BUS_BUSY; } @@ -5411,7 +5512,7 @@ impl Rex3 { let offset = addr & (REX3_SIZE - 1); let is_go64r = (offset & 0x0800) != 0; let reg_offset64r = offset & !0x0800; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if reg_offset64r == REX3_HOSTRW0 && self.resident_fatal_notified.load(Ordering::Acquire) { @@ -5420,16 +5521,16 @@ impl Rex3 { if reg_offset64r == REX3_HOSTRW0 { // Same rule as busy_or_val! on the 32-bit path: publish, then test. if !self.gfifo.publish_tail() { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.note_resident_bus_busy(); return BusRead64::busy(); } if self.gfxbusy.load(Ordering::Acquire) || !self.gfifo.is_empty() { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.note_resident_bus_busy(); return BusRead64::busy(); } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if !self.resident_ready() { return BusRead64::err(); } @@ -5525,7 +5626,7 @@ impl Rex3 { // the transfer ordered correctly against concurrent CPU register writes // — the words arrive in the stream at the point the producer put them. if !self.gfifo_try_push_batch(REX3_DMA_BATCH_W, vals) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.note_resident_bus_busy(); return BUS_BUSY; } @@ -5594,7 +5695,7 @@ impl Rex3 { // Sole effect of this write, so a full queue reports BUS_BUSY and // the CPU retries the store (see write32's default arm). if !self.gfifo_try_push(REX3_HOSTRW64 | (offset & 0x0800), val) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.note_resident_bus_busy(); return BUS_BUSY; } @@ -5626,7 +5727,7 @@ impl Rex3 { // `is_go` puts the GO bit on the low word, matching the two-write order. let go_lo = if is_go { off_lo | 0x0800 } else { off_lo }; if !self.gfifo_try_push2(off_hi, high as u64, go_lo, low as u64) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.note_resident_bus_busy(); return BUS_BUSY; } @@ -5750,7 +5851,7 @@ impl Rex3 { } } -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] macro_rules! resident_bus_gate { ($board:expr, $on_error:expr) => { let _resident_bus = match $board.resident_bus_guard() { @@ -5763,7 +5864,7 @@ macro_rules! resident_bus_gate { }; } -#[cfg(not(all(test, feature = "wgpu")))] +#[cfg(not(feature = "wgpu"))] macro_rules! resident_bus_gate { ($board:expr, $on_error:expr) => {}; } @@ -5969,7 +6070,7 @@ impl crate::gfx_display::GfxDisplay for Rex3 { fn display_stop(&self) -> &crate::gfx_display::DisplayStop { self.head.display_stop() } fn screen(&self) -> &Mutex { self.head.screen() } fn request_screenshot(&self) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { self.resident_frame.request_screenshot(); return; @@ -5977,7 +6078,7 @@ impl crate::gfx_display::GfxDisplay for Rex3 { self.head.request_screenshot(); } fn screenshot_pending(&self) -> bool { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { return self.resident_frame.screenshot_pending(); } @@ -5985,7 +6086,7 @@ impl crate::gfx_display::GfxDisplay for Rex3 { } fn cycles(&self) -> crate::cpu::mips_core::CyclesPtr { self.status.cycles() } fn request_frame(&self) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { self.resident_frame.request_frame(); return; @@ -5993,14 +6094,24 @@ impl crate::gfx_display::GfxDisplay for Rex3 { self.head.request_frame(); } fn frame_pending(&self) -> bool { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { return self.resident_frame.frame_pending(); } self.head.frame_pending() } + fn minimum_frame_request_timeout(&self) -> std::time::Duration { + #[cfg(feature = "wgpu")] + if self.resident_frame.is_active() + && *self.resident_display_mode.lock() + == Some(ResidentDisplayMode::ReadbackCompatibility) + { + return std::time::Duration::from_secs(2); + } + std::time::Duration::ZERO + } fn frame_available(&self) -> bool { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { return !self.resident_fatal_notified.load(Ordering::Acquire) && self.resident_frame.frame_available(); @@ -6010,14 +6121,13 @@ impl crate::gfx_display::GfxDisplay for Rex3 { fn set_capture(&self, on: bool) { self.head.set_capture(on); // Repaint at once so the new consumer gets a frame. - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if on && self.resident_frame.is_active() { self.resident_frame.request_frame(); } } #[cfg(feature = "wgpu")] fn gpu(&self) -> Option> { - #[cfg(test)] if let RasterMode::Resident(raster) = &*self.raster.lock() { return raster.gpu(); } @@ -6030,20 +6140,25 @@ impl crate::gfx_display::GfxDisplay for Rex3 { None } } + #[cfg(feature = "wgpu")] + fn requires_same_device_wgpu_presenter(&self) -> bool { + self.resident_frame.is_active() + && *self.resident_display_mode.lock() == Some(ResidentDisplayMode::DirectWgpu) + } fn hw_cursor(&self) -> Option { use crate::dev::ng1::vc2::{VC2_REG_CURRENT_CURSOR_X, VC2_REG_CURSOR_Y_LOC, VC2_REG_CURSOR_ENTRY_PTR, VC2_REG_DISPLAY_CONTROL, VC2_CTRL_CURSOR_EN, VC2_CTRL_CURSOR_SIZE}; let screen = self.head.screen().lock(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() && (self.resident_fatal_notified.load(Ordering::Acquire) || screen.width == 0 || screen.height == 0) { return None; } let x_adjust = screen.cursor_x_adjust; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let _resident_screen = self.resident_frame.is_active().then_some(screen); - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] drop(screen); let vc2 = self.vc2.lock(); let ctrl = vc2.regs[VC2_REG_DISPLAY_CONTROL as usize]; @@ -6077,7 +6192,7 @@ impl crate::gfx_display::GfxDisplay for Rex3 { impl Rex3 { fn reset_registers(&self) { - unsafe { *self.context.get() = Rex3Context::power_on_default(); } + unsafe { Rex3Context::write_power_on(self.context.get()); } self.config.config.store(0, Ordering::Relaxed); self.config.status.store(0, Ordering::Relaxed); *self.dcb.lock() = Rex3DcbState::default(); @@ -6092,7 +6207,7 @@ impl Rex3 { impl Resettable for Rex3 { fn power_on(&self) { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { self.reset_resident_board().unwrap_or_else(|error| panic!("REX3 {error}")); return; @@ -6304,7 +6419,7 @@ impl Rex3 { impl Saveable for Rex3 { fn save_state(&self) -> toml::Value { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { return self.snapshot_resident_registers().unwrap_or_else(|error| panic!("REX3 {error}")); } @@ -6312,7 +6427,7 @@ impl Saveable for Rex3 { } fn load_state(&self, v: &toml::Value) -> Result<(), String> { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.resident_frame.is_active() { return self.stage_resident_registers(v); } @@ -6436,7 +6551,7 @@ mod tests; mod plane_tests; #[cfg(feature = "gputrace")] -#[cfg_attr(all(test, feature = "wgpu"), allow(dangerous_implicit_autorefs))] +#[cfg_attr(feature = "wgpu", allow(dangerous_implicit_autorefs))] #[path = "rex3_rec.rs"] pub mod rec; #[cfg(feature = "gputrace")] diff --git a/src/dev/ng1/rex3_flat_fill.rs b/src/dev/ng1/rex3_flat_fill.rs index ee23f2384..4cb16c539 100644 --- a/src/dev/ng1/rex3_flat_fill.rs +++ b/src/dev/ng1/rex3_flat_fill.rs @@ -13,13 +13,13 @@ use super::rex3::{ }; use super::rex3_generic::{fastclear_color, ls_pattern_state_after, Mode}; use super::rex3_shape; -#[cfg(test)] +#[cfg(feature = "wgpu")] use super::wgpu::{ fill_walk::{prepare_fill_walk, FillCompletion, FillOperation, FillWalkKind}, shade::ShadeDda, }; -#[cfg(test)] +#[cfg(feature = "wgpu")] const RESIDENT_PAYLOAD_WORDS: u64 = 1024 * 1024 / size_of::() as u64; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -68,7 +68,7 @@ pub(crate) struct FlatFill { enum ContextAdvance { Block { xsave: i32, y_after: i32 }, Span { xend: i32 }, - #[cfg(test)] + #[cfg(feature = "wgpu")] Resident(FillCompletion), } @@ -83,9 +83,9 @@ pub(crate) struct PreparedFlatFill { fill: Option, advance: ContextAdvance, final_lsrcount: Option, - #[cfg(test)] + #[cfg(feature = "wgpu")] shade_payload: Option>, - #[cfg(test)] + #[cfg(feature = "wgpu")] final_shade: Option, } @@ -94,7 +94,7 @@ impl PreparedFlatFill { self.fill } - #[cfg(test)] + #[cfg(feature = "wgpu")] pub(crate) fn shade_payload(&self) -> Option<&[u32]> { self.shade_payload.as_deref() } @@ -104,7 +104,7 @@ impl PreparedFlatFill { /// A refused/full batch must leave both this command and the post-setup /// context untouched so they can be retried after submission. pub(crate) fn finish(self, context: &mut Rex3Context) { - #[cfg(test)] + #[cfg(feature = "wgpu")] if let Some(shade) = self.final_shade { shade.write_back(context); } @@ -116,7 +116,7 @@ impl PreparedFlatFill { ContextAdvance::Span { xend } => { context.xstart = xend + (1 << 11); } - #[cfg(test)] + #[cfg(feature = "wgpu")] ContextAdvance::Resident(completion) => { completion.apply(context); return; @@ -141,7 +141,7 @@ pub(crate) fn prepare_flat_fill(context: &Rex3Context) -> Result Result { @@ -438,7 +438,7 @@ fn prepare(context: &Rex3Context, allow_aux: bool) -> Result Result, @@ -357,7 +359,7 @@ impl Rex3 { return Err(ResidentError::InvalidState("resident GFIFO is stopped")); } let rex = unsafe { std::mem::transmute::<&Rex3, &'static Rex3>(self) }; - let handle = std::thread::Builder::new().name("REX3-Resident-Display-Test".into()) + let handle = std::thread::Builder::new().name("REX3-Resident-Display".into()) .spawn(move || { if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| display.run(rex))).is_err() { rex.fail_resident(ResidentError::Failed("resident display worker panicked".into())); @@ -450,6 +452,7 @@ struct ResidentCompatibilityDisplay { sbtex: StatusBarTexture, compositor: Option, capture: CaptureCount, + #[cfg(test)] capture_fault: Option, save_screenshots: bool, resize_pending: bool, @@ -466,6 +469,7 @@ impl ResidentCompatibilityDisplay { sbtex: StatusBarTexture::new(), compositor: None, capture: CaptureCount::default(), + #[cfg(test)] capture_fault: None, save_screenshots: true, resize_pending: true, @@ -586,12 +590,15 @@ impl ResidentCompatibilityDisplay { let bytes = (width * height * size_of::()) as u64; self.capture.attempt(bytes)?; rex.resident_frame.capture_attempt(ResidentDisplayMode::ReadbackCompatibility, bytes)?; + #[cfg(test)] let fault = self.capture_fault.take(); + #[cfg(test)] if matches!(fault, Some(CaptureFault::BeforeCapture)) { return Err(ResidentError::Failed("injected composed capture failure before capture".into())); } let compositor = self.compositor.get_or_insert_with(SwCompositor::new); compositor.compose_pixels(&screen.compositor_source()); + #[cfg(test)] if matches!(fault, Some(CaptureFault::AfterStaging)) { return Err(ResidentError::Failed("injected composed capture failure after staging".into())); } @@ -795,6 +802,7 @@ impl ResidentDisplay { } } + #[cfg(test)] pub(crate) fn fail_next_capture(&mut self, fault: CaptureFault) { match &mut self.driver { ResidentDisplayDriver::Direct(display) => display.scanout.fail_next_capture(fault), diff --git a/src/dev/ng1/rex3_lifecycle.rs b/src/dev/ng1/rex3_lifecycle.rs index a86ae3050..f5acf1dd9 100644 --- a/src/dev/ng1/rex3_lifecycle.rs +++ b/src/dev/ng1/rex3_lifecycle.rs @@ -1,4 +1,4 @@ -//! Stopped, test-only resident snapshot, restore and reset boundaries. +//! Stopped resident snapshot, restore and reset boundaries. use std::io; use std::path::Path; diff --git a/src/dev/ng1/rex3_lifecycle_tests.rs b/src/dev/ng1/rex3_lifecycle_tests.rs index 8e2a705ee..3bc3cced2 100644 --- a/src/dev/ng1/rex3_lifecycle_tests.rs +++ b/src/dev/ng1/rex3_lifecycle_tests.rs @@ -369,6 +369,14 @@ fn resident_lifecycle_machine_head1_checkpoint_round_trip() { cfg.nveeprom.clear(); cfg.monitor_port = Some(0); cfg.graphics.resolution = crate::dev::ng1::vc2_timings::NewportResolution::Res1024x768; + { + let mut cpu_machine = Box::new(crate::machine::Machine::new(cfg.clone())); + cpu_machine.save_snapshot("m7-head1").unwrap(); + let snapshot = Path::new("saves").join("m7-head1"); + assert!(snapshot.join("rex3_head1_rgb.bin").is_file()); + assert!(snapshot.join("rex3_head1_aux.bin").is_file()); + cpu_machine.stop(); + } let mut machine = Box::new(crate::machine::Machine::new(cfg)); let primary = machine.get_rex3().expect("primary Newport head"); let rex = machine.get_rex3_head1().expect("second Newport head"); @@ -459,7 +467,6 @@ fn resident_lifecycle_machine_head1_checkpoint_round_trip() { assert_eq!((data.rgb[0], data.aux[0]), (0x1234_5678, 0x89ab_cdef)); assert_eq!((data.observations, data.restores), (1, 1)); } - machine.save_snapshot("m7-head1").unwrap(); let snapshot = Path::new("saves").join("m7-head1"); assert!(snapshot.join("rex3_head1_rgb.bin").is_file()); @@ -540,6 +547,15 @@ fn resident_lifecycle_machine_head1_checkpoint_round_trip() { assert_eq!(visible(&rex), (1024, 768)); assert!(primary_state.lock().rgb.iter().all(|&word| word == 0)); assert!(state.lock().rgb.iter().all(|&word| word == 0)); + std::fs::write(&chunks_path, &chunks_file).unwrap(); + let mut chunks: crate::snapshot::ChunksManifest = + postcard::from_bytes(&std::fs::read(&chunks_path).unwrap()).unwrap(); + chunks.framebuffer_chunks = Some((Vec::new(), Vec::new())); + std::fs::write(&chunks_path, postcard::to_allocvec(&chunks).unwrap()).unwrap(); + let error = machine.load_snapshot_paused("m7-head1").unwrap_err(); + assert!(error.contains("resident RGB seed has 0 words"), "{error}"); + assert!(primary.start_resident_processor().is_err()); + machine.reset(); std::fs::write(&chunks_path, chunks_file).unwrap(); std::fs::remove_file(&rgb_path).unwrap(); std::fs::create_dir(&rgb_path).unwrap(); @@ -571,6 +587,31 @@ fn resident_lifecycle_machine_head1_checkpoint_round_trip() { }); } +#[test] +fn resident_direct_rejects_cpu_plane_debug_overlays() { + on_large_stack(|| { + let (_owner, rex, _state, _rgb_guard, _aux_guard) = fixture(); + *rex.resident_display_mode.lock() = Some(ResidentDisplayMode::DirectWgpu); + + for (command, args) in [ + ("rex", &["cmap", "on"][..]), + ("disp", &["debug", "on"][..]), + ] { + let error = rex.execute_command(command, args, Box::new(io::sink())).unwrap_err(); + assert!(error.contains("unavailable in resident direct mode"), "{error}"); + } + assert!(!rex.show_cmap.load(Ordering::Relaxed)); + assert!(!rex.show_disp_debug.load(Ordering::Relaxed)); + + #[cfg(feature = "developer")] + { + let error = rex.execute_command("draw", &["debug", "on"], Box::new(io::sink())).unwrap_err(); + assert!(error.contains("unavailable in resident direct mode"), "{error}"); + assert!(!rex.draw_debug.load(Ordering::Relaxed)); + } + }); +} + #[test] fn resident_lifecycle_invalid_inputs_cannot_mix_staged_snapshots() { on_large_stack(|| { diff --git a/src/dev/ng1/rex3_rec.rs b/src/dev/ng1/rex3_rec.rs index a7f6873dd..f8828dd05 100644 --- a/src/dev/ng1/rex3_rec.rs +++ b/src/dev/ng1/rex3_rec.rs @@ -454,7 +454,7 @@ impl Rex3 { /// or stop the running one with `None` (checkpointing where it ends). /// Returns the stopped recording's path and record count. pub fn set_recording(&self, path: Option<&str>, state: bool) -> std::io::Result> { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.reject_resident_operation("recording").map_err(std::io::Error::other)?; // Raise the tap first so every producer that starts an access from // here on blocks on the lock below; whatever slipped in before is diff --git a/src/dev/ng1/rex3_tests.rs b/src/dev/ng1/rex3_tests.rs index 386eba300..b184b8468 100644 --- a/src/dev/ng1/rex3_tests.rs +++ b/src/dev/ng1/rex3_tests.rs @@ -82,8 +82,8 @@ fn start_rex3_processor_only(rex: &Rex3) { /// Run `f` on a thread with the same 64 MB stack the construction helpers use. /// /// `Rex3Context` is over a megabyte (the HOSTRW data port alone is 1 MiB) and -/// `Resettable::power_on` assigns a freshly built one — twice, counting the -/// temporary — which overflows the 2 MB default Rust test thread stack. +/// construction and tests that return it by value can move it through enough +/// temporaries to overflow the 2 MB default Rust test thread stack. fn with_big_stack(f: impl FnOnce() -> T + Send + 'static) -> T { std::thread::Builder::new() .stack_size(64 * 1024 * 1024) @@ -93,6 +93,14 @@ fn with_big_stack(f: impl FnOnce() -> T + Send + 'static) -> .expect("big-stack test thread panicked") } +#[test] +fn power_on_reset_fits_default_service_thread_stack() { + let rex = with_big_stack(|| Arc::new(Rex3::new(crate::disp::StatusSource::detached()))); + std::thread::spawn(move || rex.power_on()) + .join() + .expect("REX3 reset overflowed the service thread stack"); +} + #[cfg(feature = "wgpu")] mod resident_gate_tests { use super::*; diff --git a/src/dev/ng1/rex3_trace.rs b/src/dev/ng1/rex3_trace.rs index 550552f6a..e97c42219 100644 --- a/src/dev/ng1/rex3_trace.rs +++ b/src/dev/ng1/rex3_trace.rs @@ -56,7 +56,7 @@ impl Rex3Trace { Self { out: None, start: None, gf_seq: 0, cpu_seq: 0, run: None, last_flush: None } } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(super) fn is_active(&self) -> bool { self.out.is_some() } @@ -226,7 +226,7 @@ impl Rex3 { /// Start the annotated trace into `path` for the categories in `mask`. pub fn start_trace(&self, path: &str, mask: u32) -> std::io::Result<()> { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.reject_resident_operation("tracing").map_err(std::io::Error::other)?; let f = TraceWriter::create(path)?; let mut t = self.trace.lock(); diff --git a/src/dev/ng1/wgpu/direct.rs b/src/dev/ng1/wgpu/direct.rs index 90fd3ec0e..564edcb52 100644 --- a/src/dev/ng1/wgpu/direct.rs +++ b/src/dev/ng1/wgpu/direct.rs @@ -196,6 +196,7 @@ impl Ng1ResidentFrame<'_> { } #[derive(Clone, Copy)] +#[cfg(test)] pub(crate) enum CaptureFault { BeforeCapture, AfterStaging, @@ -221,6 +222,7 @@ pub(crate) struct Ng1ResidentScanout { traffic: ResidentScanoutTraffic, last_frame: Option, capture_scratch: Vec, + #[cfg(test)] capture_fault: Option, } @@ -288,7 +290,9 @@ impl Ng1Scanout { pipeline, scanout, bindings, display_layout: layout, did_layout, did_source: None, did: None, vc2, did_params, did_pipeline, did_group: None, display_group: None, uploaded: None, dimensions: None, traffic: ResidentScanoutTraffic::default(), - last_frame: None, capture_scratch: Vec::new(), capture_fault: None, + last_frame: None, capture_scratch: Vec::new(), + #[cfg(test)] + capture_fault: None, }) } } @@ -307,6 +311,7 @@ impl Ng1ResidentScanout { self.did.as_ref().expect("DID buffer is bound after composition") } + #[cfg(test)] pub(crate) fn fail_next_capture(&mut self, fault: CaptureFault) { self.capture_fault = Some(fault); } @@ -353,13 +358,16 @@ impl Ng1ResidentScanout { } let bytes = pixels as u64 * 4; self.traffic.composed_readbacks.attempt(bytes)?; + #[cfg(test)] let fault = self.capture_fault.take(); + #[cfg(test)] if matches!(fault, Some(CaptureFault::BeforeCapture)) { return Err(ResidentError::Failed("injected composed capture failure before capture".into())); } self.capture_scratch.resize(pixels, 0); self.scanout.read_output(stamp.width, stamp.height, &mut self.capture_scratch) .map_err(scanout_error)?; + #[cfg(test)] if matches!(fault, Some(CaptureFault::AfterStaging)) { return Err(ResidentError::Failed("injected composed capture failure after staging".into())); } diff --git a/src/dev/ng1/wgpu/mod.rs b/src/dev/ng1/wgpu/mod.rs index ef38b95d6..c39375f58 100644 --- a/src/dev/ng1/wgpu/mod.rs +++ b/src/dev/ng1/wgpu/mod.rs @@ -3,8 +3,8 @@ //! on the common scan-out driver (`crate::wgpu::scanout`), so the wgpu //! window renderer composes REX3 frames on the device it presents with. //! -//! CPU raster uploads its planes from `CompositorSource`. The test-only -//! resident raster instead binds its authoritative planes read-only and +//! CPU raster uploads its planes from `CompositorSource`. The resident raster +//! instead binds its authoritative planes read-only and //! supplies display state separately. use std::sync::Arc; @@ -22,31 +22,22 @@ use crate::wgpu::{Gpu, Scanout, ScanoutPipeline, ScanoutShaders}; #[cfg(test)] mod tests; -#[cfg(test)] mod command; -#[cfg(test)] mod copy; -#[cfg(test)] pub(super) mod fill_walk; -#[cfg(test)] mod host; -#[cfg(test)] mod line; -#[cfg(test)] pub(crate) mod shade; -#[cfg(test)] pub(crate) mod resident; -#[cfg(test)] pub(crate) mod telemetry; -#[cfg(test)] pub(crate) mod direct; #[cfg(test)] @@ -57,7 +48,6 @@ mod did_bench_tests; const SCANOUT: Fragment = Fragment::new("ng1_scanout.wgsl", include_str!("shaders/scanout.wgsl")); const CPU_BINDINGS: Fragment = Fragment::new("ng1_bindings.wgsl", include_str!("shaders/scanout_cpu_bindings.wgsl")); -#[cfg(test)] const RESIDENT_BINDINGS: Fragment = Fragment::new("ng1_bindings.wgsl", include_str!("shaders/scanout_resident_bindings.wgsl")); /// REX3's planes the display reads: 2048 x 1024. @@ -98,7 +88,6 @@ impl Ng1Slot { }) } - #[cfg(test)] fn of_display(src: &crate::disp::Ng1DisplaySource<'_>) -> Ng1Slot { Self::new(src.vc2_regs, src.xmap_mode, src.ramdac, src.cmap, Ng1HeadState { cursor_x_adjust: src.cursor_x_adjust, diff --git a/src/dev/ng1/wgpu/resident.rs b/src/dev/ng1/wgpu/resident.rs index 2b3485480..17f122347 100644 --- a/src/dev/ng1/wgpu/resident.rs +++ b/src/dev/ng1/wgpu/resident.rs @@ -902,7 +902,7 @@ impl ResidentRaster { } } -/// The test-only GPU implementation. Draws change the GPU planes directly; +/// The authoritative GPU implementation. Draws change the GPU planes directly; /// reset and explicit restore are the only later raw-plane upload boundaries. pub(crate) struct ResidentGpu { gpu: Arc, diff --git a/src/dev/odyssey/assembly.rs b/src/dev/odyssey/assembly.rs index e2266a46f..7b153fca5 100644 --- a/src/dev/odyssey/assembly.rs +++ b/src/dev/odyssey/assembly.rs @@ -67,7 +67,7 @@ pub(super) fn report_w_plane_clip() { /// model-view, eye to clip by the projection, clip to window by the /// viewport, window depth through the depth range, and the user clip /// planes, in eye coordinates, those whose bit is set enabled. -#[derive(Clone, Copy)] +#[derive(Clone, Copy, serde::Serialize, serde::Deserialize)] pub(super) struct XfState { /// Column-major. pub modelview: [f32; 16], @@ -152,7 +152,7 @@ pub(super) trait PrimSink { } /// A polygon's vertices, kept until the end of the primitive. -#[derive(Clone, Default)] +#[derive(Clone, Default, serde::Serialize, serde::Deserialize)] pub(super) struct PolygonBuffer { pub vertices: Vec, pub count: u32, @@ -185,7 +185,7 @@ impl PolygonBuffer { } /// The board's primitive assembly and its transform state. -#[derive(Clone)] +#[derive(Clone, serde::Serialize, serde::Deserialize)] pub(super) struct Assembly { /// The board's context identity. pub hw_id: u32, diff --git a/src/dev/odyssey/board.rs b/src/dev/odyssey/board.rs index ea805c9aa..9a8f70867 100644 --- a/src/dev/odyssey/board.rs +++ b/src/dev/odyssey/board.rs @@ -4,9 +4,9 @@ //! Ported from include/hw/misc/sgi-odyssey.h in jamesbraid/qemu-sgi-origami. use super::consts::{ - clip, config, flctl, gl_cmd, intr, nic, write, ODSY_CONTROL_WIDGET_MASK, - ODSY_DEPTH_FUNC_RESET, ODSY_DRAW_MASK_RESET, ODSY_F32_ONE, ODSY_F32_POINT_EIGHT, - ODSY_F32_POINT_TWO, ODSY_XT_REQ_TIMEOUT_RESET, + clip, cmd as hw_cmd, config, flctl, gl_cmd, intr, nic, pattern, write, + ODSY_CONTROL_WIDGET_MASK, ODSY_DEPTH_FUNC_RESET, ODSY_DRAW_MASK_RESET, ODSY_F32_ONE, + ODSY_F32_POINT_EIGHT, ODSY_F32_POINT_TWO, ODSY_XT_REQ_TIMEOUT_RESET, }; use super::fifo::CommandFifo; use super::geometry::LightScratch; @@ -61,7 +61,7 @@ pub(super) const ODSY_GL_WINDOW_ENTRIES: u32 = 0xc7; pub(super) const ODSY_WIDTH: u32 = 1280; pub(super) const ODSY_HEIGHT: u32 = 1024; -#[derive(Clone, Copy, Default)] +#[derive(Clone, Copy, Default, serde::Serialize, serde::Deserialize)] pub(super) struct OpenVertex { pub word: u32, pub operands: [u32; 4], @@ -120,6 +120,7 @@ pub(super) struct Reports { /// The vertex side's texture registers: the current texture coordinate /// and the linear texgen planes. The fragment side's are in the draw state /// slots (pipe::texture::TexRegs). +#[derive(Clone, serde::Serialize, serde::Deserialize)] pub(super) struct TexState { pub coord: [u32; 2], pub coord_r: u32, @@ -131,6 +132,7 @@ pub(super) struct TexState { } /// The current-query and raster-position records the kernel reads back. +#[derive(Clone, serde::Serialize, serde::Deserialize)] pub(super) struct Query { // The command-FIFO interpreter (0x110000). pub current: [[u32; 5]; 5], @@ -186,6 +188,7 @@ pub(super) struct ImageRaster { /// The command-FIFO interpreter (0x110000): the word being parsed and its /// operands. +#[derive(Clone, serde::Serialize, serde::Deserialize)] pub(super) struct CommandParse { pub state: u32, pub tag: u32, @@ -197,6 +200,7 @@ pub(super) struct CommandParse { /// Where the current context's window is, and the window as the kernel last /// pushed it on its own port. +#[derive(Clone, serde::Serialize, serde::Deserialize)] pub(super) struct Window { // Where the current context's window is. pub origin: [i32; 2], @@ -218,6 +222,7 @@ pub(super) struct Window { /// The widget's configuration and the host-side registers that live on the /// board: the properties, the modelled registers and the back end's indexed /// file. +#[derive(Clone, serde::Serialize, serde::Deserialize)] pub(super) struct Widget { // Properties. pub revision: u32, @@ -312,6 +317,7 @@ pub(super) struct Geometry { /// The 2D stream's GL immediate-mode subset, the X server's and greeter's /// drawing: points, lines, polygons and the one-bit text pattern. +#[derive(Clone, serde::Serialize, serde::Deserialize)] pub(super) struct Stream2d { // The 2D stream's GL immediate-mode subset. pub primitive: u32, @@ -334,6 +340,7 @@ pub(super) struct Stream2d { /// The GL primitive in flight between glBegin and glEnd, and what a context /// switch needs to restart it. +#[derive(Clone, serde::Serialize, serde::Deserialize)] pub(super) struct OpenPrimitive { pub in_prim: bool, pub begin_open: bool, @@ -374,6 +381,8 @@ pub(super) struct Board { /// The queue to BUZZ's raster unit (raster_unit.rs), which owns the /// planes below while it has jobs. pub raster: std::sync::Arc, + /// Sticky host-GPU failure shared with the raster unit and scanout. + pub gpu_fault: super::reader::GpuFault, /// Guest memory as the board's DMA reaches it (HEART's RAM on the /// Octane). pub guest: GuestMemory, @@ -512,12 +521,14 @@ impl Default for Board { let fifo = front.lock().fifo.clone(); let raster: std::sync::Arc = Default::default(); let display = std::sync::Arc::new(DisplayShared::new(front.clone())); + let gpu_fault = display.gpu_fault.clone(); let display_fifo = std::sync::Arc::new(parking_lot::Mutex::new(DisplayFifo::new(display.clone()))); Board { slot: 0, front, fifo, raster, + gpu_fault, guest: None, nic_bus: None, widget: Widget { @@ -1250,6 +1261,7 @@ impl Board { self.work_reset(); // The raster unit finishes what it was given before the planes clear. self.raster_wait(); + *self.gpu_fault.lock() = None; self.reset_registers(); self.front.lock().update_irq(); // Board::configure() also calls Board::reset_registers(), before @@ -1318,6 +1330,7 @@ impl Board { } let gpu = crate::wgpu::Gpu::new(opts)?; let mut unit = GpuRaster::new(&gpu, self.vram.size(), PLANE_SPAN, PLANE_SPAN)?; + unit.attach_fault(self.gpu_fault.clone()); self.vram.track_dirty(); unit.attach_mirror(self.vram.ptr()); // The display composes on the GPU too, from the same memory. @@ -1368,3 +1381,547 @@ impl Board { s } } + +#[derive(serde::Serialize, serde::Deserialize)] +pub(super) struct BoardSnapshot { + widget: Widget, + draw_mask: u32, + draw_word: u32, + read_word: u32, + dest_base: u32, + pixel_mode: [u32; 4], + slots: Vec, + tex: TexState, + dma_seq: u64, + query: Query, + cmd: CommandParse, + win: Window, + stream: Stream2d, + assembly: Assembly, + prim: OpenPrimitive, + gl_matrix_slot: u32, + gl_matrix: [u32; 16], + gl_texture_matrix: [u32; 16], + gl_rect: [u32; 10], + gl_depth_func: u32, + gl_write_mask: u32, + alpha_word: u32, + stencil_word: u32, + logic_op_word: u32, + polygon_mode_word: u32, + polygon_offset_word: u32, + line_point_words: [u32; 2], + gl_combined: [u32; 16], + gl_block16: [u32; 4], + gl_clip_box: [u32; 2], + gl_fog: [u32; 6], + geom: GeometrySnapshot, + gl_color_index: u32, + gl_color_is_index: bool, + colour_mode: [u32; 2], + dest_buffer_word: u32, + plane_write_mask: u32, + const_colour: [u32; 2], + blend_word: u32, + image: ImageSnapshot, + nic: Option, +} + +#[derive(serde::Serialize, serde::Deserialize)] +struct GeometrySnapshot { + constants: Vec, + constant_index: u32, + entry: Vec, + light_colour: Vec, + light_vec: Vec, + light_colour_sel: u32, + light_vec_sel: u32, + material: [u32; 16], + back_material: [u32; 16], + shininess_sqrt: u32, + back_shininess_sqrt: u32, + colour: [u32; 4], + normal: [u32; 3], + normal_matrix: [u32; 16], + modelview: [u32; 16], + clip_eye: [u32; CLIP_PLANES as usize * 4], + colour_material: u32, + lit: bool, + two_sided: bool, + separate_specular: bool, + local_viewer: bool, + normalize: bool, + colour_overridden: bool, + program_mem: Vec, + program_addr: u32, + regs_20_35: Vec, + program_half: u32, + linear_texgen: u32, + sphere_texgen: u32, +} + +#[derive(serde::Serialize, serde::Deserialize)] +struct ImageSnapshot { + active: bool, + x1: u32, + y1: u32, + x2: u32, + y2: u32, + box_words: [u32; 10], + cur_x: u32, + cur_y: u32, + fmt: u32, + group_len: u32, + group_painted: u32, + code: u32, + write_code: u32, + swap: bool, + pixel: [u8; 16], + bytes: u32, + nbytes: u32, +} + +fn exact_vec(name: &str, values: Vec) -> Result<[T; N], String> { + let len = values.len(); + values + .try_into() + .map_err(|_| format!("VPro {name} has {len} entries, expected {N}")) +} + +impl GeometrySnapshot { + fn capture(geom: &Geometry) -> Self { + Self { + constants: geom.constants.to_vec(), + constant_index: geom.constant_index, + entry: geom.entry.to_vec(), + light_colour: geom.light_colour.to_vec(), + light_vec: geom.light_vec.to_vec(), + light_colour_sel: geom.light_colour_sel, + light_vec_sel: geom.light_vec_sel, + material: geom.material, + back_material: geom.back_material, + shininess_sqrt: geom.shininess_sqrt, + back_shininess_sqrt: geom.back_shininess_sqrt, + colour: geom.colour, + normal: geom.normal, + normal_matrix: geom.normal_matrix, + modelview: geom.modelview, + clip_eye: geom.clip_eye, + colour_material: geom.colour_material, + lit: geom.lit, + two_sided: geom.two_sided, + separate_specular: geom.separate_specular, + local_viewer: geom.local_viewer, + normalize: geom.normalize, + colour_overridden: geom.colour_overridden, + program_mem: geom.program_mem.clone(), + program_addr: geom.program_addr, + regs_20_35: geom.regs_20_35.to_vec(), + program_half: geom.program_half, + linear_texgen: geom.linear_texgen, + sphere_texgen: geom.sphere_texgen, + } + } + + fn validate(&self) -> Result<(), String> { + for (name, len, expected) in [ + ( + "geometry constants", + self.constants.len(), + ODSY_GL_CONSTANTS as usize, + ), + ( + "geometry entries", + self.entry.len(), + ODSY_GL_ENTRIES as usize, + ), + ( + "geometry light colours", + self.light_colour.len(), + ODSY_GL_LIGHT_COLOURS as usize * 4, + ), + ( + "geometry light vectors", + self.light_vec.len(), + ODSY_GL_LIGHTS as usize * 16, + ), + ( + "geometry program", + self.program_mem.len(), + (ODSY_GL_PROGRAM_ENTRIES * 6) as usize, + ), + ("geometry registers", self.regs_20_35.len(), 22), + ] { + if len != expected { + return Err(format!( + "VPro {name} has {len} entries, expected {expected}" + )); + } + } + Ok(()) + } + + fn into_geometry(self) -> Result { + self.validate()?; + Ok(Geometry { + constants: exact_vec("geometry constants", self.constants)?, + constant_index: self.constant_index, + entry: exact_vec("geometry entries", self.entry)?, + light_colour: exact_vec("geometry light colours", self.light_colour)?, + light_vec: exact_vec("geometry light vectors", self.light_vec)?, + light_colour_sel: self.light_colour_sel, + light_vec_sel: self.light_vec_sel, + material: self.material, + back_material: self.back_material, + shininess_sqrt: self.shininess_sqrt, + back_shininess_sqrt: self.back_shininess_sqrt, + colour: self.colour, + normal: self.normal, + normal_matrix: self.normal_matrix, + modelview: self.modelview, + clip_eye: self.clip_eye, + colour_material: self.colour_material, + lit: self.lit, + two_sided: self.two_sided, + separate_specular: self.separate_specular, + local_viewer: self.local_viewer, + normalize: self.normalize, + colour_overridden: self.colour_overridden, + program_mem: self.program_mem, + program_addr: self.program_addr, + regs_20_35: exact_vec("geometry registers", self.regs_20_35)?, + program_half: self.program_half, + linear_texgen: self.linear_texgen, + sphere_texgen: self.sphere_texgen, + }) + } +} + +impl ImageSnapshot { + fn capture(image: &ImageRaster) -> Self { + Self { + active: image.active, + x1: image.x1, + y1: image.y1, + x2: image.x2, + y2: image.y2, + box_words: image.box_words, + cur_x: image.cur_x, + cur_y: image.cur_y, + fmt: image.fmt, + group_len: image.group_len, + group_painted: image.group_painted, + code: image.code, + write_code: image.write_code, + swap: image.swap, + pixel: image.pixel, + bytes: image.bytes, + nbytes: image.nbytes, + } + } + + fn into_image(self) -> ImageRaster { + ImageRaster { + active: self.active, + x1: self.x1, + y1: self.y1, + x2: self.x2, + y2: self.y2, + box_words: self.box_words, + cur_x: self.cur_x, + cur_y: self.cur_y, + fmt: self.fmt, + group_len: self.group_len, + group_painted: self.group_painted, + code: self.code, + write_code: self.write_code, + swap: self.swap, + pixel: self.pixel, + bytes: self.bytes, + nbytes: self.nbytes, + fragment: None, + gpu_slot: None, + } + } +} + +impl BoardSnapshot { + pub(super) fn capture(board: &Board) -> Result { + Ok(Self { + widget: board.widget.clone(), + draw_mask: board.draw_mask, + draw_word: board.draw_word, + read_word: board.read_word, + dest_base: board.dest_base, + pixel_mode: board.pixel_mode, + slots: board.slots.snapshot_bytes()?, + tex: board.tex.clone(), + dma_seq: board.dma_seq, + query: board.query.clone(), + cmd: board.cmd.clone(), + win: board.win.clone(), + stream: board.stream.clone(), + assembly: board.assembly.clone(), + prim: board.prim.clone(), + gl_matrix_slot: board.gl_matrix_slot, + gl_matrix: board.gl_matrix, + gl_texture_matrix: board.gl_texture_matrix, + gl_rect: board.gl_rect, + gl_depth_func: board.gl_depth_func, + gl_write_mask: board.gl_write_mask, + alpha_word: board.alpha_word, + stencil_word: board.stencil_word, + logic_op_word: board.logic_op_word, + polygon_mode_word: board.polygon_mode_word, + polygon_offset_word: board.polygon_offset_word, + line_point_words: board.line_point_words, + gl_combined: board.gl_combined, + gl_block16: board.gl_block16, + gl_clip_box: board.gl_clip_box, + gl_fog: board.gl_fog, + geom: GeometrySnapshot::capture(&board.geom), + gl_color_index: board.gl_color_index, + gl_color_is_index: board.gl_color_is_index, + colour_mode: board.colour_mode, + dest_buffer_word: board.dest_buffer_word, + plane_write_mask: board.plane_write_mask, + const_colour: board.const_colour, + blend_word: board.blend_word, + image: ImageSnapshot::capture(&board.image), + nic: board + .nic_bus + .as_ref() + .map(|bus| crate::snapshot::BinValue::from_toml(&bus.save_state())), + }) + } + + pub(super) fn validate(&self, board: &Board) -> Result<(), String> { + let saved = &self.widget; + let running = &board.widget; + if ( + saved.product.as_str(), + saved.revision, + saved.memory_size, + saved.memory_banks, + saved.cas_latency, + saved.backend_version, + saved.nic_serial, + saved.nic_record.as_str(), + saved.product_record.as_str(), + ) != ( + running.product.as_str(), + running.revision, + running.memory_size, + running.memory_banks, + running.cas_latency, + running.backend_version, + running.nic_serial, + running.nic_record.as_str(), + running.product_record.as_str(), + ) { + return Err(format!( + "VPro snapshot board configuration does not match the running {}", + running.product + )); + } + self.geom.validate()?; + super::pipe::slot::Slots::decode_snapshot(&self.slots)?; + if self.query.stage > 4 { + return Err(format!( + "VPro query parser stage {} is invalid", + self.query.stage + )); + } + use super::dma::{ + ODSY_CMD_COPY_RECT, ODSY_CMD_GL_RECT, ODSY_CMD_IDLE, ODSY_CMD_IMAGE_BOX_LOAD, + ODSY_CMD_IMAGE_BURST, ODSY_CMD_MATRIX_LOAD, ODSY_CMD_OPERANDS, ODSY_CMD_PATTERN_LOAD, + ODSY_CMD_SKIP, + }; + let active = |name: &str, expected: Option| -> Result<(), String> { + if let Some(expected) = expected { + if self.cmd.operand_count != expected { + return Err(format!( + "VPro {name} parser expects {expected} words, snapshot has {}", + self.cmd.operand_count + )); + } + } + if self.cmd.operand_count == 0 || self.cmd.operand_index >= self.cmd.operand_count { + return Err(format!( + "VPro {name} parser is at word {} of {}", + self.cmd.operand_index, self.cmd.operand_count + )); + } + Ok(()) + }; + match self.cmd.state { + ODSY_CMD_IDLE => {} + ODSY_CMD_OPERANDS => { + active("operand", None)?; + if self.cmd.operand_count > ODSY_CMD_OPERANDS_MAX { + return Err(format!( + "VPro operand parser has {} words, maximum is {ODSY_CMD_OPERANDS_MAX}", + self.cmd.operand_count + )); + } + } + ODSY_CMD_COPY_RECT => { + if self.cmd.operand_index >= hw_cmd::COPY_RECT_WORDS { + return Err(format!( + "VPro copy parser is at word {}, maximum is {}", + self.cmd.operand_index, + hw_cmd::COPY_RECT_WORDS - 1 + )); + } + } + ODSY_CMD_PATTERN_LOAD => { + active("pattern", Some(pattern::WORDS))?; + if !matches!(self.stream.pattern_load_offset, 0 | pattern::WORDS) { + return Err(format!( + "VPro pattern load offset {} is invalid", + self.stream.pattern_load_offset + )); + } + } + ODSY_CMD_IMAGE_BOX_LOAD => active("image box", Some(10))?, + ODSY_CMD_IMAGE_BURST => active("image burst", None)?, + ODSY_CMD_MATRIX_LOAD => active("matrix", Some(gl_cmd::MATRIX_FLOATS))?, + ODSY_CMD_GL_RECT => active("rectangle", Some(gl_cmd::RECT_FLOATS))?, + ODSY_CMD_SKIP => active("skip", None)?, + state => return Err(format!("VPro command parser state {state} is invalid")), + } + if self.win.clip_kept > ODSY_CLIP_RECTS { + return Err(format!( + "VPro window keeps too many clip rectangles: {}", + self.win.clip_kept + )); + } + if self.prim.open_held > self.prim.open_vertex.len() as u32 { + return Err(format!( + "VPro open primitive holds too many vertices: {}", + self.prim.open_held + )); + } + if self.prim.restart_absorb > self.prim.restart_total || self.prim.restart_total > 3 { + return Err(format!( + "VPro primitive restart has {} of {} vertices left", + self.prim.restart_absorb, self.prim.restart_total + )); + } + if self.image.nbytes >= self.image.pixel.len() as u32 { + return Err(format!( + "VPro image parser holds too many bytes: {}", + self.image.nbytes + )); + } + if self.image.group_len != 0 && self.image.group_painted > self.image.group_len { + return Err(format!( + "VPro image group painted {} of {} pixels", + self.image.group_painted, self.image.group_len + )); + } + let expected_marks = ODSY_WIDTH as usize * ODSY_HEIGHT as usize; + if self.stream.poly_marks.len() != expected_marks { + return Err(format!( + "VPro polygon marks have {} entries, expected {expected_marks}", + self.stream.poly_marks.len() + )); + } + if self.stream.poly_row_lo > ODSY_HEIGHT + || self.stream.poly_row_hi > ODSY_HEIGHT + || self.stream.poly_col_lo > ODSY_WIDTH + { + return Err(format!( + "VPro polygon bounds ({}, {}) from column {} exceed {}x{}", + self.stream.poly_row_lo, + self.stream.poly_row_hi, + self.stream.poly_col_lo, + ODSY_WIDTH, + ODSY_HEIGHT + )); + } + let polygon_floats = (self.assembly.polygon.count as usize) + .checked_mul(vtx::FLOATS) + .ok_or("VPro polygon vertex count overflows")?; + if polygon_floats > self.assembly.polygon.vertices.len() + || self.assembly.polygon.vertices.len() % vtx::FLOATS != 0 + { + return Err(format!( + "VPro polygon buffer has {} floats for {} vertices", + self.assembly.polygon.vertices.len(), + self.assembly.polygon.count + )); + } + if self.assembly.nheld > super::assembly::HELD_VERTICES { + return Err(format!( + "VPro assembly holds too many vertices: {}", + self.assembly.nheld + )); + } + match (&board.nic_bus, &self.nic) { + (Some(bus), Some(nic)) => bus.validate_state(&nic.clone().into_toml())?, + (None, None) => {} + _ => return Err("VPro snapshot 1-Wire configuration does not match the board".into()), + } + Ok(()) + } +} + +impl Board { + pub(super) fn restore_snapshot(&mut self, snapshot: BoardSnapshot) -> Result<(), String> { + snapshot.validate(self)?; + let geom = snapshot.geom.into_geometry()?; + let (slot_tex, slot_dirty) = super::pipe::slot::Slots::decode_snapshot(&snapshot.slots)?; + if let (Some(bus), Some(nic)) = (&mut self.nic_bus, snapshot.nic) { + bus.load_state(&nic.into_toml())?; + } + self.widget = snapshot.widget; + self.draw_mask = snapshot.draw_mask; + self.draw_word = snapshot.draw_word; + self.read_word = snapshot.read_word; + self.dest_base = snapshot.dest_base; + self.pixel_mode = snapshot.pixel_mode; + self.slots.apply_snapshot(slot_tex, slot_dirty); + self.tex = snapshot.tex; + self.dma_seq = snapshot.dma_seq; + self.query = snapshot.query; + self.cmd = snapshot.cmd; + self.win = snapshot.win; + self.stream = snapshot.stream; + self.assembly = snapshot.assembly; + self.prim = snapshot.prim; + self.gl_matrix_slot = snapshot.gl_matrix_slot; + self.gl_matrix = snapshot.gl_matrix; + self.gl_texture_matrix = snapshot.gl_texture_matrix; + self.gl_rect = snapshot.gl_rect; + self.gl_depth_func = snapshot.gl_depth_func; + self.gl_write_mask = snapshot.gl_write_mask; + self.alpha_word = snapshot.alpha_word; + self.stencil_word = snapshot.stencil_word; + self.logic_op_word = snapshot.logic_op_word; + self.polygon_mode_word = snapshot.polygon_mode_word; + self.polygon_offset_word = snapshot.polygon_offset_word; + self.line_point_words = snapshot.line_point_words; + self.gl_combined = snapshot.gl_combined; + self.gl_block16 = snapshot.gl_block16; + self.gl_clip_box = snapshot.gl_clip_box; + self.gl_fog = snapshot.gl_fog; + self.geom = geom; + self.gl_color_index = snapshot.gl_color_index; + self.gl_color_is_index = snapshot.gl_color_is_index; + self.colour_mode = snapshot.colour_mode; + self.dest_buffer_word = snapshot.dest_buffer_word; + self.plane_write_mask = snapshot.plane_write_mask; + self.const_colour = snapshot.const_colour; + self.blend_word = snapshot.blend_word; + self.image = snapshot.image.into_image(); + self.gl_light_scratch = None; + self.dest_cache.set(None); + self.slot_cache.set(None); + self.fifo.set_threshold_6b2(self.widget.fifo_setup[2]); + if self.image.active { + self.image_box_loaded(); + } + Ok(()) + } +} diff --git a/src/dev/odyssey/clip.rs b/src/dev/odyssey/clip.rs index fa2bd8297..d1ee5460d 100644 --- a/src/dev/odyssey/clip.rs +++ b/src/dev/odyssey/clip.rs @@ -4,7 +4,7 @@ /// is no clip, unless empty is set: then it keeps no pixel. pub(super) const CLIP_MAX: usize = 16; -#[derive(Clone, Copy, Default, Debug)] +#[derive(Clone, Copy, Default, Debug, serde::Serialize, serde::Deserialize)] pub(super) struct ClipRect { pub x0: i32, pub y0: i32, @@ -19,4 +19,3 @@ pub(super) struct ClipList { pub empty: bool, pub r: [ClipRect; CLIP_MAX], } - diff --git a/src/dev/odyssey/copy.rs b/src/dev/odyssey/copy.rs index 2b2853ffb..1e6f2e439 100644 --- a/src/dev/odyssey/copy.rs +++ b/src/dev/odyssey/copy.rs @@ -318,8 +318,12 @@ impl Board { // records and with no readback, when it can take the copy. if !fragment { let op = self.copy_op(&c, &src, &dst, &from, ab.as_ref(), &to, (ox, oy), (dx0, dy0), (mirror_x, mirror_y), raw, byte_masks, filter); - if self.raster.run_now(|ctx| ctx.gpu.as_mut().is_some_and(|g| g.copy(&op))) { - return; + match self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").copy(&op)) { + Ok(()) => return, + Err(error) => { + self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").reject("GPU framebuffer copy", error.0)); + return; + } } } // Otherwise the source and destination rectangles come back, @@ -327,7 +331,14 @@ impl Board { let region = |rows: &Rows, x: i32, y: i32| { self.raster.run_now(|ctx| { if let Some(g) = &mut ctx.gpu { - if let Err(e) = g.read_back_view(&rows.v, x, y, x.wrapping_add(w as i32 - 1), y.wrapping_add(h as i32 - 1)) { + if let Err(e) = g.read_back_view( + &rows.v, + x, + y, + x.wrapping_add(w as i32 - 1), + y.wrapping_add(h as i32 - 1), + super::gpu_unit::Observation::CopySource, + ) { crate::dlog!(crate::devlog::LogModule::Odyssey, "sgi-odyssey: gpu readback: {e}"); } } @@ -383,6 +394,7 @@ impl Board { let q = BackQuad { x: px, y: py, cov: 1, zq: [zq, 0, 0, 0], rgba: [src4.map(|c| c as u16), [0; 4], [0; 4], [0; 4]], ..Default::default() }; self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").push_frag(&q, &slot)); } + None if gpu => {} None => f.0.fragment_q(&self.dest_mem(), px, py, zq, src4), } } else if let (true, Some(m)) = (raw, byte_masks) { diff --git a/src/dev/odyssey/debug.rs b/src/dev/odyssey/debug.rs index 6349c2be0..57f34563c 100644 --- a/src/dev/odyssey/debug.rs +++ b/src/dev/odyssey/debug.rs @@ -182,7 +182,7 @@ impl Odyssey { #[cfg(feature = "wgpu")] if self.state.shared.gpu_scanout.get().is_some() { if let Some(g) = self.state.quiesce(QUIESCE) { - g.gpu_sync(); + g.gpu_sync_for(super::gpu_unit::Observation::Monitor); } } let regs = self.state.shared.regs.read(); @@ -201,7 +201,7 @@ impl Odyssey { /// read board memory read what the unit drew. fn quiesced(&self) -> Result, String> { self.state.quiesce(QUIESCE).map(|g| { - g.gpu_sync(); + g.gpu_sync_for(super::gpu_unit::Observation::Monitor); g }).ok_or_else(|| { format!( diff --git a/src/dev/odyssey/display.rs b/src/dev/odyssey/display.rs index ba584daff..ef24fde10 100644 --- a/src/dev/odyssey/display.rs +++ b/src/dev/odyssey/display.rs @@ -69,6 +69,9 @@ impl Display { /// memory while the raster unit and the front end write it, as the /// board's own scan-out does. pub(super) fn compose(&self, shared: &Arc, screen: &mut Rex3Screen, want: Want) -> Composed { + if shared.gpu_fault.lock().is_some() { + return Composed::NotReady; + } let mut reader = self.reader.lock(); reader.attach(shared); crate::pf_begin!(crate::perfetto::events::ODSY_FRAME_PLANES); @@ -82,6 +85,9 @@ impl Display { return Composed::Gpu { frame, width, height }; } } + if shared.gpu_fault.lock().is_some() { + return Composed::NotReady; + } let _ = want; reader.screen_order = true; // Checking the unchanged verdicts (`display::verify`): compose a @@ -92,6 +98,9 @@ impl Display { } reader.force = verify; let composed = reader.update_to(Some((&mut screen.fb_rgb, FB_STRIDE))); + if shared.gpu_fault.lock().is_some() { + return Composed::NotReady; + } if verify && matches!(composed, super::reader::Composed::InTarget) { self.verify.lock().after("vpro", screen, !reader.skipped); } diff --git a/src/dev/odyssey/display_fifo.rs b/src/dev/odyssey/display_fifo.rs index 5d47e5638..67d423526 100644 --- a/src/dev/odyssey/display_fifo.rs +++ b/src/dev/odyssey/display_fifo.rs @@ -40,8 +40,8 @@ pub(super) struct DisplayFifo { pub i2c_opt_cmd: u32, /// What the control words spell on each port, and the clock parts' /// registers as written through them. - two_wire: [TwoWire; 2], - clock: ClockParts, + pub(super) two_wire: [TwoWire; 2], + pub(super) clock: ClockParts, /// The scan-out mode the channel-size entries set, and the display's /// shared state. pub scanout: Scanout, @@ -60,11 +60,11 @@ pub(super) struct DisplayFifo { /// address, a register number, then that register's byte and its /// successors'. #[derive(Clone, Copy, Default)] -struct TwoWire { +pub(super) struct TwoWire { /// The address byte of the transfer open, when it is a write. - address: Option, + pub(super) address: Option, /// The register the next data byte goes to. - register: Option, + pub(super) register: Option, } impl TwoWire { @@ -104,11 +104,11 @@ impl TwoWire { /// take a 12 MHz reference (the video format compiler's constant, which /// every shipped format's achieved frequency follows from). #[derive(Clone, Copy, Default)] -struct ClockParts { - ics: [u8; 8], - ami: [u8; 8], +pub(super) struct ClockParts { + pub(super) ics: [u8; 8], + pub(super) ami: [u8; 8], /// The part written last, which is the one the board has. - last: Option, + pub(super) last: Option, } impl ClockParts { @@ -554,6 +554,83 @@ impl DisplayFifo { } } +#[derive(serde::Serialize, serde::Deserialize)] +pub(super) struct DisplayFifoSnapshot { + ctl: Vec, + index: u32, + count: u32, + i2c_main_cmd: u32, + i2c_opt_cmd: u32, + two_wire: [(Option, Option); 2], + clock_ics: [u8; 8], + clock_ami: [u8; 8], + clock_last: Option, + mode: ScanoutMode, +} + +impl DisplayFifoSnapshot { + pub(super) fn validate(&self) -> Result<(), String> { + if self.ctl.len() != backend::CTL_ENTRIES as usize { + return Err(format!( + "VPro display register file has {} entries, expected {}", + self.ctl.len(), + backend::CTL_ENTRIES + )); + } + if self.index & !backend::SELECTOR_INDEX_MASK != 0 { + return Err(format!( + "VPro display FIFO index is out of range: {:#x}", + self.index + )); + } + if self.count & !backend::COUNT_MASK != 0 { + return Err(format!( + "VPro display FIFO count is out of range: {:#x}", + self.count + )); + } + let mut mode = self.mode; + if !mode.sanitize() || mode != self.mode { + return Err("VPro scanout mode is invalid".into()); + } + Ok(()) + } +} + +impl DisplayFifo { + pub(super) fn snapshot(&self) -> DisplayFifoSnapshot { + DisplayFifoSnapshot { + ctl: self.ctl.clone(), + index: self.index, + count: self.count, + i2c_main_cmd: self.i2c_main_cmd, + i2c_opt_cmd: self.i2c_opt_cmd, + two_wire: self.two_wire.map(|wire| (wire.address, wire.register)), + clock_ics: self.clock.ics, + clock_ami: self.clock.ami, + clock_last: self.clock.last, + mode: self.scanout.mode, + } + } + + pub(super) fn restore_snapshot(&mut self, snapshot: DisplayFifoSnapshot) -> Result<(), String> { + snapshot.validate()?; + self.ctl = snapshot.ctl; + self.index = snapshot.index; + self.count = snapshot.count; + self.i2c_main_cmd = snapshot.i2c_main_cmd; + self.i2c_opt_cmd = snapshot.i2c_opt_cmd; + self.two_wire = snapshot + .two_wire + .map(|(address, register)| TwoWire { address, register }); + self.clock.ics = snapshot.clock_ics; + self.clock.ami = snapshot.clock_ami; + self.clock.last = snapshot.clock_last; + self.scanout.set_mode(&snapshot.mode); + Ok(()) + } +} + #[cfg(test)] mod timing_tests { use super::*; diff --git a/src/dev/odyssey/dma.rs b/src/dev/odyssey/dma.rs index ca77e74c8..1b9f5595c 100644 --- a/src/dev/odyssey/dma.rs +++ b/src/dev/odyssey/dma.rs @@ -164,12 +164,17 @@ impl Board { #[cfg(feature = "wgpu")] if self.gpu_unit() { // A blit into the put's own surface, in order, no wait. - if let Ok(view) = super::gpu_unit::Slot::own_view(to) { - let key = super::gpu_unit::BlitKey { view: super::gpu_unit::BlitView::Own(view), ..super::gpu_unit::BlitKey::store(0, u32::MAX) }; - let slot = super::gpu_unit::Slot::none(); - self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").blit_pixel(&slot, &key, x as i32, y as i32, word)); - return; + match super::gpu_unit::Slot::own_view(to) { + Ok(view) => { + let key = super::gpu_unit::BlitKey { view: super::gpu_unit::BlitView::Own(view), ..super::gpu_unit::BlitKey::store(0, u32::MAX) }; + let slot = super::gpu_unit::Slot::none(); + self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").blit_pixel(&slot, &key, x as i32, y as i32, word)); + } + Err(error) => { + self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").reject("GPU DMA destination", error.0)); + } } + return; } to.store_word(x as i32, y as i32, word); } @@ -1161,7 +1166,7 @@ impl Board { // What the read covers, current in the mirror. let (x0, y0, x1, y1) = (src.x, src.y, src.x + w as i64 - 1, src.y + h as i64 - 1); let planes = super::pipe::backend::DestMem { colour: src.fields, depth: src.depth, stencil: src.stencil, did: src.fields[0], overlay: src.fields[1] }; - self.gpu_read_back(&planes, x0, y0, x1, y1); + self.gpu_read_back(&planes, x0, y0, x1, y1, super::gpu_unit::Observation::DmaRead); } let Ok(n) = bus.entries(table, seq, &mut e) else { return; diff --git a/src/dev/odyssey/draw.rs b/src/dev/odyssey/draw.rs index fb8b21945..94ce09807 100644 --- a/src/dev/odyssey/draw.rs +++ b/src/dev/odyssey/draw.rs @@ -50,7 +50,7 @@ impl Board { pub fn dest_mem(&self) -> DestMem { // Everything the drawing wrote, back: the general path; the hot // ones use planes_in() or store through the unit. - self.gpu_sync(); + self.gpu_sync_for(super::gpu_unit::Observation::Frontend); DestMem::of(self.vram_ptr(), &self.dest_views()) } @@ -63,19 +63,19 @@ impl Board { return self.dest_mem(); } let planes = DestMem::of(self.vram_ptr(), &self.dest_views()); - self.gpu_read_back(&planes, x0, y0, x1, y1); + self.gpu_read_back(&planes, x0, y0, x1, y1, super::gpu_unit::Observation::Frontend); planes } /// Bring the mirror current under a box of every plane of `planes` /// (GpuRaster::read_back_view). - pub fn gpu_read_back(&self, planes: &DestMem, x0: i64, y0: i64, x1: i64, y1: i64) { + pub fn gpu_read_back(&self, planes: &DestMem, x0: i64, y0: i64, x1: i64, y1: i64, reason: super::gpu_unit::Observation) { let clamp = |v: i64| v.clamp(-1, MAX_DIM as i64) as i32; let (x0, y0, x1, y1) = (clamp(x0), clamp(y0), clamp(x1), clamp(y1)); self.raster.run_now(|ctx| { if let Some(g) = &mut ctx.gpu { for rows in [&planes.colour[0], &planes.colour[1], &planes.depth, &planes.stencil, &planes.did, &planes.overlay] { - if let Err(e) = g.read_back_view(&rows.v, x0, y0, x1, y1) { + if let Err(e) = g.read_back_view(&rows.v, x0, y0, x1, y1, reason) { crate::dlog!(LogModule::Odyssey, "sgi-odyssey: gpu readback: {e}"); } } @@ -291,7 +291,7 @@ impl Board { } let planes = DestMem::of(self.vram_ptr(), &self.dest_views()); let Some(slot) = self.gpu_dest_slot(&planes) else { - return false; + return true; }; let plane_mask = (self.plane_write_mask >> ODSY_PLANE_MASK_SHIFT) as u8; self.raster.run_now(|ctx| { @@ -317,7 +317,7 @@ impl Board { } let planes = DestMem::of(self.vram_ptr(), &self.dest_views()); let Some(slot) = self.gpu_dest_slot(&planes) else { - return false; + return true; }; let key = super::gpu_unit::BlitKey::store(view, mask); self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").blit_pixel(&slot, &key, x as i32, y as i32, word)); @@ -345,7 +345,15 @@ impl Board { } } let words = super::pipe::state::RasterWords { clip_rect: self.win.clip_rect, clip_kept: self.win.clip_kept, id_compare: self.win.id_compare, ..Default::default() }; - let s = super::gpu_unit::Slot::clear(planes, &words).ok(); + let s = match super::gpu_unit::Slot::clear(planes, &words) { + Ok(slot) => Some(slot), + Err(error) => { + self.raster.run_now(|ctx| { + ctx.gpu.as_mut().expect("the unit").reject("GPU destination slot", error.0); + }); + None + } + }; self.slot_cache.set(Some((key, s))); s } @@ -563,7 +571,7 @@ impl Board { } return; } - _ => { + Some(_) => { // The kernels cannot take it: a pixel at a time. for y in y0..y1 { let runs = self.row_runs(y); @@ -575,6 +583,7 @@ impl Board { } return; } + None => return, } } let planes = self.dest_mem(); @@ -642,8 +651,6 @@ impl Board { if let Some(slot) = self.gpu_dest_slot(&planes) { self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").blit_pixel(&slot, &key, x as i32, y as i32, f.word)); mirror_byte_store(&planes.colour[b as usize], x, y, f.word, &key); - } else { - f.store(&rows, x as i32, y as i32, plane_mask); } } } @@ -814,6 +821,7 @@ impl Board { let q = super::pipe::ring::BackQuad { x: x as i32, y: y as i32, cov: 1, zq: [zq, 0, 0, 0], rgba: [src.map(|c| c as u16), [0; 4], [0; 4], [0; 4]], ..Default::default() }; self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").push_frag(&q, &slot)); } + None if self.gpu_unit() => {} None => f.0.fragment_q(&self.dest_mem(), x as i32, y as i32, zq, src), } } @@ -858,7 +866,12 @@ impl Board { self.image.gpu_slot = None; if self.gpu_unit() { let planes = DestMem::of(self.vram_ptr(), &self.dest_views()); - self.image.gpu_slot = super::gpu_unit::Slot::new(&words.backend, &planes, false, false, &words.raster, None, None).ok(); + match super::gpu_unit::Slot::new(&words.backend, &planes, false, false, &words.raster, None, None) { + Ok(slot) => self.image.gpu_slot = Some(slot), + Err(error) => { + self.raster.run_now(|ctx| ctx.gpu.as_mut().expect("the unit").reject("GPU image slot", error.0)); + } + } } if self.trace.on(super::trace::TRACE_FIFO) { let depth_kind = self.dest_views().depth.kind; diff --git a/src/dev/odyssey/gl.rs b/src/dev/odyssey/gl.rs index 8c2ea944b..c51fa8a35 100644 --- a/src/dev/odyssey/gl.rs +++ b/src/dev/odyssey/gl.rs @@ -289,7 +289,9 @@ impl GlDrawJob { let st: &'static DrawState = unsafe { &*job.slot }; let tex = Unit { regs: &st.tex, latch: &st.latch, mem: job.pixels.obj }; let (ring, gpu) = (ctx.ring.as_ref(), ctx.gpu.as_deref_mut()); - let mut p = PipeDraw::new(&job.words, tex, job.pixels.state.mem, job.index, job.pixels.state.to_aux, xf.depth_range(), job.depth.slack, ring, fr.words_stipple.as_ref(), job.background, gpu); + let Ok(mut p) = PipeDraw::new(&job.words, tex, job.pixels.state.mem, job.index, job.pixels.state.to_aux, xf.depth_range(), job.depth.slack, ring, fr.words_stipple.as_ref(), job.background, gpu) else { + return (d.line_phase, frame); + }; if p.ring.is_none() { ctx.drain_back(); } @@ -627,10 +629,8 @@ impl Board { continue; } Err(e) => { - crate::dlog!(LogModule::Odyssey, "sgi-odyssey: gpu unit: {} cleared on the CPU", e.0); - if let Err(e) = g.read_back() { - crate::dlog!(LogModule::Odyssey, "sgi-odyssey: gpu readback: {e}"); - } + g.reject("GPU clear slot", e.0); + continue; } } } diff --git a/src/dev/odyssey/golden/replay.rs b/src/dev/odyssey/golden/replay.rs index 30b2640bf..197e18875 100644 --- a/src/dev/odyssey/golden/replay.rs +++ b/src/dev/odyssey/golden/replay.rs @@ -180,6 +180,7 @@ fn programs_scan_out(recs: &[Rec]) -> bool { #[derive(Clone, Copy, Debug, Default)] pub struct Opts { pub gpu: bool, + #[cfg_attr(not(feature = "wgpu"), allow(dead_code))] pub lazy: bool, pub tick: Option, } diff --git a/src/dev/odyssey/gpu_unit.rs b/src/dev/odyssey/gpu_unit.rs index 2e3e21044..41ff17152 100644 --- a/src/dev/odyssey/gpu_unit.rs +++ b/src/dev/odyssey/gpu_unit.rs @@ -5,6 +5,45 @@ use super::view::View; +/// Why GPU-owned board memory is made visible to the CPU mirror. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(not(feature = "wgpu"), allow(dead_code))] +pub(crate) enum Observation { + Frontend, + DmaRead, + CopySource, + Snapshot, + Monitor, + StateHash, + Validation, +} + +#[cfg_attr(not(feature = "wgpu"), allow(dead_code))] +impl Observation { + pub const COUNT: usize = 7; + pub const ALL: [Observation; Self::COUNT] = [ + Observation::Frontend, + Observation::DmaRead, + Observation::CopySource, + Observation::Snapshot, + Observation::Monitor, + Observation::StateHash, + Observation::Validation, + ]; + + pub const fn name(self) -> &'static str { + match self { + Observation::Frontend => "frontend", + Observation::DmaRead => "dma-read", + Observation::CopySource => "copy-source", + Observation::Snapshot => "snapshot", + Observation::Monitor => "monitor", + Observation::StateHash => "state-hash", + Observation::Validation => "validation", + } + } +} + /// Which view a blit stores into: one of its slot's, or one of its own /// (a DMA put's surface). #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -138,15 +177,11 @@ mod stub { pub(crate) struct Unsupported(pub String); impl GpuRaster { - pub fn read_back(&mut self) -> Result<(), String> { - match *self {} - } - - pub fn copy(&mut self, _op: &super::CopyOp) -> bool { + pub fn copy(&mut self, _op: &super::CopyOp) -> Result<(), Unsupported> { match *self {} } - pub fn read_back_now(&mut self) -> Result<(), String> { + pub fn read_back_now(&mut self, _reason: super::Observation) -> Result<(), String> { match *self {} } @@ -202,13 +237,21 @@ mod stub { match *self {} } - pub fn read_back_view(&mut self, _v: &super::super::view::View, _x0: i32, _y0: i32, _x1: i32, _y1: i32) -> Result<(), String> { + pub fn read_back_view(&mut self, _v: &super::super::view::View, _x0: i32, _y0: i32, _x1: i32, _y1: i32, _reason: super::Observation) -> Result<(), String> { + match *self {} + } + + pub fn reject(&mut self, _operation: &str, _reason: String) { match *self {} } pub fn upload_region(&mut self, _v: &super::super::view::View, _x0: i32, _y0: i32, _x1: i32, _y1: i32) { match *self {} } + + pub fn reseed_from_mirror(&mut self) -> Result<(), String> { + match *self {} + } } #[allow(dead_code, reason = "the own-view blit paths are compiled with the wgpu feature only")] diff --git a/src/dev/odyssey/interface.rs b/src/dev/odyssey/interface.rs index 26fe1557a..f6dd01b30 100644 --- a/src/dev/odyssey/interface.rs +++ b/src/dev/odyssey/interface.rs @@ -170,6 +170,104 @@ impl HostInterface { } } +#[derive(serde::Serialize, serde::Deserialize)] +pub(super) struct HostInterfaceSnapshot { + identity: u32, + widget_control: u32, + xt_req_timeout: u32, + int_dest_upper: u32, + int_dest_lower: u32, + error_command: u32, + intr_flags: u32, + intr_enable: u32, + sw_token: u32, + flctl_target: u32, + route_dest_upper: u32, + route_dest_lower: u32, + route_vectors: u32, + swap_arm: u32, + pending_swaps: u32, + swap_control: u32, + swap_control_companion: u32, + swap_set: u32, + swap_clear: u32, + displayed_buffer: u32, +} + +impl HostInterfaceSnapshot { + pub(super) fn validate(&self, expected_identity: u32) -> Result<(), String> { + if self.identity != expected_identity { + return Err(format!( + "VPro host identity is {:#010x}, expected {expected_identity:#010x}", + self.identity + )); + } + Ok(()) + } +} + +impl HostInterface { + pub(super) fn snapshot(&self) -> HostInterfaceSnapshot { + HostInterfaceSnapshot { + identity: self.identity, + widget_control: self.widget_control, + xt_req_timeout: self.xt_req_timeout, + int_dest_upper: self.int_dest_upper, + int_dest_lower: self.int_dest_lower, + error_command: self.error_command, + intr_flags: self.intr_flags, + intr_enable: self.intr_enable, + sw_token: self.sw_token, + flctl_target: self.flctl_target, + route_dest_upper: self.route_dest_upper, + route_dest_lower: self.route_dest_lower, + route_vectors: self.route_vectors, + swap_arm: self.swap_arm, + pending_swaps: self.swap_pending, + swap_control: self.swap_control, + swap_control_companion: self.swap_control_companion, + swap_set: self.swap_set, + swap_clear: self.swap_clear, + displayed_buffer: self.displayed_buffer, + } + } + + pub(super) fn restore_snapshot( + &mut self, + snapshot: HostInterfaceSnapshot, + ) -> Result<(), String> { + snapshot.validate(self.identity)?; + let stream_pos = self.fifo.pushed(); + self.identity = snapshot.identity; + self.widget_control = snapshot.widget_control; + self.xt_req_timeout = snapshot.xt_req_timeout; + self.int_dest_upper = snapshot.int_dest_upper; + self.int_dest_lower = snapshot.int_dest_lower; + self.error_command = snapshot.error_command; + self.intr_flags = snapshot.intr_flags; + self.intr_enable = snapshot.intr_enable; + self.sw_token = snapshot.sw_token; + self.flctl_target = snapshot.flctl_target; + self.route_dest_upper = snapshot.route_dest_upper; + self.route_dest_lower = snapshot.route_dest_lower; + self.route_vectors = snapshot.route_vectors; + self.swap_arm = snapshot.swap_arm; + self.swap_pending = snapshot.pending_swaps; + self.swap_control = snapshot.swap_control; + self.swap_control_companion = snapshot.swap_control_companion; + self.swap_set = snapshot.swap_set; + self.swap_clear = snapshot.swap_clear; + self.displayed_buffer = snapshot.displayed_buffer; + self.displayed_written = true; + self.swap_after = stream_pos; + self.stream_pos = stream_pos; + self.interrupt_asserted = false; + self.flag_held = false; + self.update_irq(); + Ok(()) + } +} + impl HostInterface { /// An interrupt packet into the widget-addressed fabric. pub fn send_packet(&mut self, where_: u64, vector: u8, set: bool) { diff --git a/src/dev/odyssey/mmio.rs b/src/dev/odyssey/mmio.rs index 3375f99eb..c6de1d345 100644 --- a/src/dev/odyssey/mmio.rs +++ b/src/dev/odyssey/mmio.rs @@ -487,6 +487,10 @@ impl Default for RefusalStats { } impl BoardLocks { + pub fn gpu_fault(&self) -> Option { + self.shared.gpu_fault.lock().clone() + } + /// The board for a processor holding `submit`, which `cpu` proves. /// /// # Safety @@ -515,6 +519,14 @@ impl BoardLocks { } } + pub(super) fn with_stopped_board(&self, f: impl FnOnce(&mut Board) -> R) -> R { + let cpu = self.submit.lock(); + // SAFETY: snapshot callers require the board and raster workers to + // be stopped; the submit lock also excludes processor accesses. + let mut board = unsafe { self.board(cpu) }; + f(&mut board) + } + /// Record a bus access, if a recording runs. Called with `submit` held /// for the accesses that take it, so the records come out in the order /// the stores were taken (lock order: `submit`, then the recorder). diff --git a/src/dev/odyssey/mod.rs b/src/dev/odyssey/mod.rs index 9700d1c7f..c80d9780f 100644 --- a/src/dev/odyssey/mod.rs +++ b/src/dev/odyssey/mod.rs @@ -93,14 +93,15 @@ mod record; mod reader; mod registers; mod scanout; +mod snapshot; +#[cfg(test)] +mod tests; mod trace; mod view; mod vram; #[cfg(feature = "wgpu")] mod wgpu; mod worker; -#[cfg(test)] -mod tests; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -242,9 +243,13 @@ impl Odyssey { } fn read(&self, addr: u64, size: u32) -> (u32, u64) { + if self.state.gpu_fault().is_some() { + return (BUS_ERR, 0); + } match self.offset(addr) { Some(off) => match self.state.bus_read(off, size) { - Some(v) => (BUS_OK, v), + Some(v) if self.state.gpu_fault().is_none() => (BUS_OK, v), + Some(_) => (BUS_ERR, 0), None => (BUS_BUSY, 0), }, None => (BUS_ERR, 0), @@ -252,10 +257,15 @@ impl Odyssey { } fn write(&self, addr: u64, size: u32, val: u64) -> u32 { + if self.state.gpu_fault().is_some() { + return BUS_ERR; + } match self.offset(addr) { Some(off) => { - if self.state.bus_write(off, val, size) { + if self.state.bus_write(off, val, size) && self.state.gpu_fault().is_none() { BUS_OK + } else if self.state.gpu_fault().is_some() { + BUS_ERR } else { BUS_BUSY } diff --git a/src/dev/odyssey/pipe/mod.rs b/src/dev/odyssey/pipe/mod.rs index f21796d3e..ac1f78ed3 100644 --- a/src/dev/odyssey/pipe/mod.rs +++ b/src/dev/odyssey/pipe/mod.rs @@ -66,9 +66,6 @@ pub(crate) struct PipeDraw { pub gpu_tex: Option>, } -/// A draw the GPU unit could not take was reported. -static GPU_REFUSED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); - impl PipeDraw { /// Store a shaded quad (q.draw is set here): on the back-end thread, or /// now. @@ -125,20 +122,15 @@ impl PipeDraw { stipple: Option<&[u32; 32]>, background: Option<[f64; 4]>, gpu: Option<&mut super::gpu_unit::GpuRaster>, - ) -> PipeDraw { + ) -> Result { // The draw's slot on the GPU unit; a draw into planes the kernels // cannot address takes the CPU path on a memory brought current. let (gpu, gpu_slot): (*mut super::gpu_unit::GpuRaster, Option) = match gpu { Some(g) => match super::gpu_unit::Slot::new(&w.backend, &planes, index, to_aux, &w.raster, stipple, background) { Ok(s) => (g as *mut _, Some(s)), Err(e) => { - if !GPU_REFUSED.swap(true, std::sync::atomic::Ordering::Relaxed) { - crate::dlog!(crate::devlog::LogModule::Odyssey, "sgi-odyssey: gpu unit: {} drawn on the CPU", e.0); - } - if let Err(e) = g.read_back() { - crate::dlog!(crate::devlog::LogModule::Odyssey, "sgi-odyssey: gpu readback: {e}"); - } - (std::ptr::null_mut(), None) + g.reject("GPU draw slot", e.0.clone()); + return Err(e.0); } }, None => (std::ptr::null_mut(), None), @@ -171,7 +163,7 @@ impl PipeDraw { let gpu_tex = (!gpu.is_null() && tex.regs.target() != texture::Target::None).then(|| Box::new(super::gpu_unit::TexSlot::new(tex.regs, tex.latch))); - PipeDraw { + Ok(PipeDraw { raster, shader: ShaderState::of(&w.shader, tex.regs), backend, @@ -188,7 +180,7 @@ impl PipeDraw { gpu_slot, gpu_words: (w.raster.mode, w.shader.alpha), gpu_tex, - } + }) } } @@ -198,4 +190,3 @@ impl PipeDraw { pub(crate) fn aux_targets(targets: [bool; 2], index: bool) -> [bool; 2] { [false, index && targets[1]] } - diff --git a/src/dev/odyssey/pipe/slot.rs b/src/dev/odyssey/pipe/slot.rs index bed42ae33..2e21da369 100644 --- a/src/dev/odyssey/pipe/slot.rs +++ b/src/dev/odyssey/pipe/slot.rs @@ -40,6 +40,22 @@ pub(crate) struct Slots { cur: usize, } +#[derive(serde::Serialize, serde::Deserialize)] +struct SlotsSnapshot { + version: u32, + dirty: bool, + path: u32, + sampler: Vec, + env: Vec, + lod: Vec, + level0: u32, + levels: Vec, + scale_bias: Vec, + offset: Vec, + colour_table: Vec, + detail: Vec, +} + // SAFETY: the front end alone writes a slot, and only one no draw holds; // the stages read the slots their draws hold. unsafe impl Send for Slots {} @@ -96,6 +112,87 @@ impl Slots { unsafe { &mut *self.slot.add(self.cur) } } + pub fn snapshot_bytes(&self) -> Result, String> { + for i in 0..SLOTS { + // SAFETY: the stopped raster pipeline holds no slots. + if unsafe { &*self.slot.add(i) }.users.load(Ordering::Acquire) != 0 { + return Err("VPro draw state is still in use".into()); + } + } + let s = self.cur(); + let snapshot = SlotsSnapshot { + version: 1, + dirty: s.dirty, + path: s.tex.path, + sampler: s.tex.sampler.to_vec(), + env: s.tex.env.to_vec(), + lod: s.tex.lod.to_vec(), + level0: s.tex.level0, + levels: s.tex.levels.to_vec(), + scale_bias: s.tex.scale_bias.to_vec(), + offset: s.tex.offset.to_vec(), + colour_table: s.tex.colour_table.iter().flatten().copied().collect(), + detail: s.tex.detail.to_vec(), + }; + postcard::to_allocvec(&snapshot).map_err(|e| format!("encode VPro draw state: {e}")) + } + + pub fn decode_snapshot(bytes: &[u8]) -> Result<(TexRegs, bool), String> { + let s: SlotsSnapshot = + postcard::from_bytes(bytes).map_err(|e| format!("decode VPro draw state: {e}"))?; + if s.version != 1 { + return Err(format!( + "VPro draw-state snapshot version {} is not supported", + s.version + )); + } + let mut tex = TexRegs::default(); + let copy = |name: &str, dst: &mut [u32], src: &[u32]| -> Result<(), String> { + if dst.len() != src.len() { + return Err(format!( + "VPro draw-state {name} has {} words, expected {}", + src.len(), + dst.len() + )); + } + dst.copy_from_slice(src); + Ok(()) + }; + tex.path = s.path; + copy("sampler", &mut tex.sampler, &s.sampler)?; + copy("environment", &mut tex.env, &s.env)?; + copy("LOD", &mut tex.lod, &s.lod)?; + tex.level0 = s.level0; + copy("levels", &mut tex.levels, &s.levels)?; + copy("scale/bias", &mut tex.scale_bias, &s.scale_bias)?; + copy("offset", &mut tex.offset, &s.offset)?; + let table_len = tex.colour_table.len() * tex.colour_table[0].len(); + if table_len != s.colour_table.len() { + return Err(format!( + "VPro draw-state colour table has {} words, expected {}", + s.colour_table.len(), + table_len + )); + } + let table = tex.colour_table.iter_mut().flatten(); + for (dst, src) in table.zip(s.colour_table) { + *dst = src; + } + copy("detail table", &mut tex.detail, &s.detail)?; + Ok((tex, s.dirty)) + } + + pub fn apply_snapshot(&mut self, tex: TexRegs, dirty: bool) { + let mut slots = Slots::default(); + let state = slots.cur_mut(); + state.tex = tex; + state.dirty = dirty; + if !dirty { + state.latch = Latch::of(&state.tex); + } + *self = slots; + } + /// A draw takes the current slot: latched if its registers changed, /// then held until the draw releases it (DrawState::release()). The /// latch's unimplemented bits come back for the caller to report. diff --git a/src/dev/odyssey/raster_unit.rs b/src/dev/odyssey/raster_unit.rs index 4f4620fc4..0161ddbfd 100644 --- a/src/dev/odyssey/raster_unit.rs +++ b/src/dev/odyssey/raster_unit.rs @@ -840,17 +840,57 @@ impl Board { /// `raster_wait`, and with the GPU unit the mirror brought current even /// when the unit is set to skip readbacks (the replay's checkpoints). + #[cfg_attr( + not(test), + allow(dead_code, reason = "used by validation tests and golden replay") + )] pub fn gpu_sync(&self) { - self.raster_wait(); + self.gpu_sync_for(super::gpu_unit::Observation::Validation); + } + + pub fn gpu_sync_for(&self, reason: super::gpu_unit::Observation) { + if let Err(e) = self.gpu_sync_result(reason) { + crate::dlog!( + crate::devlog::LogModule::Odyssey, + "sgi-odyssey: gpu readback: {e}" + ); + } + } + + pub fn gpu_sync_result(&self, reason: super::gpu_unit::Observation) -> Result<(), String> { + self.raster.wait_idle(); if !self.raster.threaded() { - self.raster.run_now(|ctx| { + return self.raster.run_now(|ctx| { if let Some(g) = &mut ctx.gpu { - if let Err(e) = g.read_back_now() { - crate::dlog!(crate::devlog::LogModule::Odyssey, "sgi-odyssey: gpu readback: {e}"); - } + g.wait()?; + g.read_back_now(reason)?; } + Ok(()) }); } + Ok(()) + } + + pub fn snapshot_line_phase(&self) -> Result { + self.raster.wait_idle(); + if self.raster.threaded() { + return Err("VPro raster unit is still running".into()); + } + Ok(self.raster.run_now(|ctx| ctx.line_phase)) + } + + pub fn restore_raster_snapshot(&self, line_phase: u32) -> Result<(), String> { + self.raster.wait_idle(); + if self.raster.threaded() { + return Err("VPro raster unit is still running".into()); + } + self.raster.run_now(|ctx| { + ctx.line_phase = line_phase; + if let Some(gpu) = &mut ctx.gpu { + gpu.reseed_from_mirror()?; + } + Ok(()) + }) } /// Whether the board draws on the GPU unit. diff --git a/src/dev/odyssey/reader.rs b/src/dev/odyssey/reader.rs index 2f2066bcc..07087d85d 100644 --- a/src/dev/odyssey/reader.rs +++ b/src/dev/odyssey/reader.rs @@ -33,6 +33,18 @@ use std::sync::{Arc, OnceLock}; use parking_lot::Mutex; +pub(super) type GpuFault = Arc>>; + +pub(super) fn fail_gpu(fault: &GpuFault, operation: &str, error: impl std::fmt::Display) -> String { + let message = format!("{operation}: {error}"); + let mut current = fault.lock(); + if current.is_none() { + crate::dlog!(crate::devlog::LogModule::Odyssey, "sgi-odyssey: GPU unit failed: {message}"); + *current = Some(message.clone()); + } + current.clone().unwrap_or(message) +} + /// The back end's registers the scanout resolves through: the colour /// lookup RAM, the per-drawable display modes, the overlay modes and the /// hardware cursor. @@ -315,6 +327,110 @@ impl SharedRegs { self.snapshot(&mut regs, u64::MAX); regs } + + pub fn restore(&self, regs: &DisplayRegs) -> Result<(), String> { + if regs.lut.len() != self.lut.len() { + return Err(format!( + "VPro display LUT has {} words, expected {}", + regs.lut.len(), + self.lut.len() + )); + } + for (dst, &src) in self.lut.iter().zip(®s.lut) { + dst.store(src, Relaxed); + } + for (dst, &src) in self.dispmode.iter().zip(®s.dispmode) { + dst.store(src, Relaxed); + } + for (dst, &src) in self.overlay_mode.iter().zip(®s.overlay_mode) { + dst.store(src, Relaxed); + } + for (dst, &src) in self.cursor.iter().zip(®s.cursor) { + dst.store(src, Relaxed); + } + for (dst, &src) in self.pbj.iter().zip(®s.pbj) { + dst.store(src, Relaxed); + } + self.gen.fetch_add(1, Release); + Ok(()) + } +} + +#[derive(serde::Serialize, serde::Deserialize)] +pub(super) struct DisplayRegsSnapshot { + lut: Vec, + dispmode: Vec, + overlay_mode: Vec, + cursor: Vec, + pbj: Vec, +} + +impl DisplayRegsSnapshot { + pub(super) fn capture(regs: &SharedRegs) -> Self { + let regs = regs.read(); + Self { + lut: regs.lut, + dispmode: regs.dispmode.to_vec(), + overlay_mode: regs.overlay_mode.to_vec(), + cursor: regs.cursor.to_vec(), + pbj: regs.pbj.to_vec(), + } + } + + pub(super) fn validate(&self) -> Result<(), String> { + let expected = DisplayRegs::default(); + for (name, actual, wanted) in [ + ("LUT", self.lut.len(), expected.lut.len()), + ( + "display modes", + self.dispmode.len(), + expected.dispmode.len(), + ), + ( + "overlay modes", + self.overlay_mode.len(), + expected.overlay_mode.len(), + ), + ("cursor registers", self.cursor.len(), expected.cursor.len()), + ("scanout registers", self.pbj.len(), expected.pbj.len()), + ] { + if actual != wanted { + return Err(format!( + "VPro display {name} has {actual} words, expected {wanted}" + )); + } + } + Ok(()) + } + + fn decode(self) -> Result { + self.validate()?; + Ok(DisplayRegs { + lut: self.lut, + dispmode: self + .dispmode + .try_into() + .map_err(|_| "VPro display modes length changed")?, + overlay_mode: self + .overlay_mode + .try_into() + .map_err(|_| "VPro overlay modes length changed")?, + cursor: self + .cursor + .try_into() + .map_err(|_| "VPro cursor registers length changed")?, + pbj: self + .pbj + .try_into() + .map_err(|_| "VPro scanout registers length changed")?, + }) + } +} + +impl SharedRegs { + pub(super) fn restore_snapshot(&self, snapshot: DisplayRegsSnapshot) -> Result<(), String> { + self.restore(&snapshot.decode()?) + } } /// The scanout mode as the front end last set it, and the counts a display @@ -362,6 +478,8 @@ pub(super) struct DisplayShared { /// composes there from the memory the unit draws into, instead of /// from the mirror. pub gpu_scanout: OnceLock>, + /// A raster or scanout failure is sticky and closes board accesses. + pub gpu_fault: GpuFault, /// The command stream position (`CommandFifo::retired`) the raster /// unit's submitted work covers: a flip armed after it waits /// (`HostInterface::retrace`). The GPU unit publishes it @@ -380,6 +498,7 @@ impl DisplayShared { mode: ModeCell::new(), vram: OnceLock::new(), gpu_scanout: OnceLock::new(), + gpu_fault: Arc::new(Mutex::new(None)), covered: Arc::new(AtomicU64::new(u64::MAX)), front, swaps: AtomicU64::new(0), @@ -450,8 +569,6 @@ pub(super) struct ScanoutReader { /// mark is not ordered against its data, so the frame may have missed /// the data; one more is composed to catch it. settling: bool, - /// The GPU scan-out failed once (reported), and the CPU composes. - gpu_failed: bool, /// The tile workers' draw marks of the frame in progress. marks: Vec, /// Compose even a frame nothing changed (`display::verify`), and say @@ -549,7 +666,7 @@ impl ScanoutReader { pub fn update_on_gpu(&mut self) -> Option<(wgpu::Buffer, u32, u32)> { let shared = self.shared(); let scanout = shared.gpu_scanout.get()?; - if self.gpu_failed { + if shared.gpu_fault.lock().is_some() { return None; } self.sync(&shared); @@ -564,8 +681,7 @@ impl ScanoutReader { match composed { Ok(buffer) => Some((buffer, w, h)), Err(e) => { - self.gpu_failed = true; - crate::dlog!(crate::devlog::LogModule::Odyssey, "sgi-odyssey: gpu scanout: {e}; composing on the CPU"); + fail_gpu(&shared.gpu_fault, "GPU scanout", e); None } } @@ -596,6 +712,9 @@ impl ScanoutReader { /// readback still lands in `frame`, as 0x00RRGGBB. pub fn update_to(&mut self, target: Option<(&mut [u32], usize)>) -> Composed { let shared = self.shared(); + if shared.gpu_fault.lock().is_some() { + return Composed::No; + } // Taken before anything is read: a store that lands from here on // marks the next frame. // The tile workers' draws, which mark their planes and box after @@ -633,7 +752,7 @@ impl ScanoutReader { } self.composed = now; self.settling = stored; - let gpu = shared.gpu_scanout.get().filter(|_| !self.gpu_failed); + let gpu = shared.gpu_scanout.get(); let (w, h) = (self.frame_width as usize, self.frame_height as usize); let (Some(frame), Some(mem)) = (snapshot, shared.vram.get()) else { // No scan-out programmed (out of reset, before the PROM sets @@ -668,10 +787,8 @@ impl ScanoutReader { return Composed::InFrame; } Err(e) => { - if !self.gpu_failed { - self.gpu_failed = true; - crate::dlog!(crate::devlog::LogModule::Odyssey, "sgi-odyssey: gpu scanout: {e}; composing on the CPU"); - } + fail_gpu(&shared.gpu_fault, "GPU scanout readback", e); + return Composed::No; } } } diff --git a/src/dev/odyssey/scanout.rs b/src/dev/odyssey/scanout.rs index b11a65d49..bc56f228c 100644 --- a/src/dev/odyssey/scanout.rs +++ b/src/dev/odyssey/scanout.rs @@ -14,7 +14,7 @@ pub(super) const SCANOUT_MAX_DIM: u32 = 16384; /// 1 kHz: well above any SGI display's field rate. pub(super) const SCANOUT_MIN_PERIOD_NS: i64 = 1000000; -#[derive(Clone, Copy, Default, Debug)] +#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub(super) struct ScanoutMode { pub width: u32, // visible pixels and lines pub height: u32, diff --git a/src/dev/odyssey/snapshot.rs b/src/dev/odyssey/snapshot.rs new file mode 100644 index 000000000..8e5868139 --- /dev/null +++ b/src/dev/odyssey/snapshot.rs @@ -0,0 +1,552 @@ +use super::board::BoardSnapshot; +#[cfg(test)] +use super::board::{ODSY_HEIGHT, ODSY_WIDTH}; +use super::display_fifo::DisplayFifoSnapshot; +use super::interface::HostInterfaceSnapshot; +use super::reader::DisplayRegsSnapshot; +use super::Odyssey; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::sync::atomic::Ordering; + +const ODYSSEY_SNAPSHOT_VERSION: u32 = 1; +const ODYSSEY_STATE_MAX_BYTES: usize = 16 << 20; + +#[derive(Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct OdysseyMetadata { + version: u32, + board: String, + memory_bytes: u64, +} + +#[derive(Serialize, Deserialize)] +struct OdysseyState { + version: u32, + board: BoardSnapshot, + front: HostInterfaceSnapshot, + display_fifo: DisplayFifoSnapshot, + display_regs: DisplayRegsSnapshot, + line_phase: u32, +} + +fn pack_bytes(bytes: &[u8]) -> Result, String> { + let len = u32::try_from(bytes.len()) + .map_err(|_| format!("VPro snapshot part is too large: {} bytes", bytes.len()))?; + let mut words = Vec::with_capacity(1 + bytes.len().div_ceil(4)); + words.push(len); + for chunk in bytes.chunks(4) { + let mut word = [0; 4]; + word[..chunk.len()].copy_from_slice(chunk); + words.push(u32::from_be_bytes(word)); + } + Ok(words) +} + +fn unpack_bytes(name: &str, words: &[u32]) -> Result, String> { + let Some((&len, body)) = words.split_first() else { + return Err(format!("VPro snapshot part {name} is empty")); + }; + let len = len as usize; + if body.len() != len.div_ceil(4) { + return Err(format!( + "VPro snapshot part {name} has {} payload words for {len} bytes", + body.len() + )); + } + let mut bytes = Vec::with_capacity(body.len() * 4); + for word in body { + bytes.extend_from_slice(&word.to_be_bytes()); + } + bytes.truncate(len); + Ok(bytes) +} + +fn pack_vram(words: &[u64], bytes: u64) -> Result, String> { + let len = u32::try_from(bytes).map_err(|_| format!("VPro VRAM is too large: {bytes} bytes"))?; + let mut packed = Vec::with_capacity(1 + words.len() * 2); + packed.push(len); + for word in words { + let bytes = word.to_ne_bytes(); + packed.push(u32::from_be_bytes( + bytes[..4].try_into().expect("four-byte halfword"), + )); + packed.push(u32::from_be_bytes( + bytes[4..].try_into().expect("four-byte halfword"), + )); + } + Ok(packed) +} + +fn decode_state(bytes: &[u8]) -> Result { + if bytes.len() > ODYSSEY_STATE_MAX_BYTES { + return Err(format!( + "VPro state is {} bytes, maximum is {ODYSSEY_STATE_MAX_BYTES}", + bytes.len() + )); + } + let (state, rest) = + postcard::take_from_bytes(bytes).map_err(|e| format!("decode VPro state: {e}"))?; + if !rest.is_empty() { + return Err(format!("VPro state has {} trailing bytes", rest.len())); + } + Ok(state) +} + +impl Odyssey { + /// Architectural state and VRAM split into named word streams. The + /// board and raster workers must already be stopped. + pub fn snapshot_parts(&self) -> Result<(String, Vec<(String, Vec)>), String> { + if self.running.load(Ordering::Acquire) { + return Err("VPro workers must be stopped before taking a snapshot".into()); + } + if self.state.fifo.level() != 0 { + return Err("VPro command FIFO is not empty".into()); + } + if let Some(error) = self.state.shared.gpu_fault.lock().as_ref() { + return Err(format!("VPro GPU raster unit is faulted: {error}")); + } + self.state.with_stopped_board(|board| { + board.gpu_sync_result(super::gpu_unit::Observation::Snapshot) + })?; + if let Some(error) = self.state.shared.gpu_fault.lock().as_ref() { + return Err(format!("VPro GPU raster unit is faulted: {error}")); + } + let (board_name, memory_bytes, board, vram, line_phase) = + self.state.with_stopped_board(|board| { + let memory_bytes = board.vram.size(); + let memory = board.vram.ptr_shared(); + let vram = pack_vram(memory.words_at(0, memory_bytes), memory_bytes)?; + Ok::<_, String>(( + board.widget.product.clone(), + memory_bytes, + BoardSnapshot::capture(board)?, + vram, + board.snapshot_line_phase()?, + )) + })?; + let state = OdysseyState { + version: ODYSSEY_SNAPSHOT_VERSION, + board, + front: self.state.front.lock().snapshot(), + display_fifo: self.state.display_fifo.lock().snapshot(), + display_regs: DisplayRegsSnapshot::capture(&self.state.shared.regs), + line_phase, + }; + let state = postcard::to_allocvec(&state).map_err(|e| format!("encode VPro state: {e}"))?; + let metadata = toml::to_string(&OdysseyMetadata { + version: ODYSSEY_SNAPSHOT_VERSION, + board: board_name, + memory_bytes, + }) + .map_err(|e| format!("encode VPro snapshot metadata: {e}"))?; + Ok(( + metadata, + vec![("state".into(), pack_bytes(&state)?), ("vram".into(), vram)], + )) + } + + /// Restore a complete VPro snapshot after the machine's power-on reset. + /// Every named payload is decoded and validated before live state changes. + pub fn restore_snapshot_parts( + &self, + metadata: &str, + parts: &[(String, Vec)], + ) -> Result<(), String> { + if self.running.load(Ordering::Acquire) { + return Err("VPro workers must be stopped before restoring a snapshot".into()); + } + let metadata: OdysseyMetadata = + toml::from_str(metadata).map_err(|e| format!("decode VPro snapshot metadata: {e}"))?; + if metadata.version != ODYSSEY_SNAPSHOT_VERSION { + return Err(format!( + "VPro snapshot version {} is not supported", + metadata.version + )); + } + let mut named = HashMap::with_capacity(parts.len()); + for (name, words) in parts { + if named.insert(name.as_str(), words.as_slice()).is_some() { + return Err(format!("duplicate VPro snapshot part {name}")); + } + } + if named.len() != 2 { + return Err(format!( + "VPro snapshot has {} parts, expected 2", + named.len() + )); + } + let state_words = named + .remove("state") + .ok_or("VPro snapshot part state is missing")?; + let vram_words = named + .remove("vram") + .ok_or("VPro snapshot part vram is missing")?; + if let Some(name) = named.keys().next() { + return Err(format!("unknown VPro snapshot part {name}")); + } + let state = decode_state(&unpack_bytes("state", state_words)?)?; + if state.version != ODYSSEY_SNAPSHOT_VERSION { + return Err(format!( + "VPro state version {} is not supported", + state.version + )); + } + let vram = unpack_bytes("vram", vram_words)?; + self.state.with_stopped_board(|board| { + if metadata.board != board.widget.product { + return Err(format!( + "VPro snapshot is for {}, running board is {}", + metadata.board, board.widget.product + )); + } + if metadata.memory_bytes != board.vram.size() { + return Err(format!( + "VPro snapshot has {} bytes of memory, running board has {}", + metadata.memory_bytes, + board.vram.size() + )); + } + if vram.len() as u64 != metadata.memory_bytes { + return Err(format!( + "VPro VRAM part is {} bytes, expected {}", + vram.len(), + metadata.memory_bytes + )); + } + state.board.validate(board) + })?; + let current_identity = self.state.front.lock().identity; + state.front.validate(current_identity)?; + state.display_fifo.validate()?; + state.display_regs.validate()?; + + let OdysseyState { + board, + front, + display_fifo, + display_regs, + line_phase, + .. + } = state; + self.state.with_stopped_board(|running| { + let ptr = running.vram.ptr_shared(); + for (index, bytes) in vram.chunks(64 << 10).enumerate() { + let mut words = Vec::with_capacity(bytes.len() / 8); + for word in bytes.chunks_exact(8) { + words.push(u64::from_ne_bytes( + word.try_into().expect("eight-byte VRAM word"), + )); + } + ptr.write_words_untracked((index * (64 << 10)) as u64, &words); + } + ptr.mark_written(); + running.restore_snapshot(board)?; + running.restore_raster_snapshot(line_phase) + })?; + self.state.front.lock().restore_snapshot(front)?; + self.state + .display_fifo + .lock() + .restore_snapshot(display_fifo)?; + self.state.shared.regs.restore_snapshot(display_regs)?; + let gpu_enabled = self.state.with_stopped_board(|board| board.gpu_unit()); + self.state.shared.covered.store( + if gpu_enabled { + self.state.fifo.pushed() + } else { + u64::MAX + }, + Ordering::Release, + ); + self.state.shared.swaps.fetch_add(1, Ordering::Release); + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::dev::heart::{host_clock, Heart, HeartConfig}; + use crate::dev::heart_mem::HeartRam; + use crate::traits::Resettable; + use std::sync::Arc; + + fn board(product: &str) -> Arc { + let heart = Arc::new(Heart::new( + HeartConfig::octane([128 << 20, 0, 0, 0]), + host_clock(), + )); + let ram = Arc::new(HeartRam::try_new([128, 0, 0, 0]).expect("ram")); + Odyssey::new( + 11, + product, + heart, + ram.dma_view(), + crate::disp::StatusSource::detached(), + (2, 32), + (crate::config::RasterUnit::Cpu, "auto"), + ) + .expect("VPro board") + } + + #[test] + fn byte_parts_reject_truncation_and_trailing_words() { + let packed = pack_bytes(&[1, 2, 3, 4, 5]).unwrap(); + assert_eq!(unpack_bytes("test", &packed).unwrap(), [1, 2, 3, 4, 5]); + assert!(unpack_bytes("test", &packed[..packed.len() - 1]).is_err()); + let mut trailing = packed; + trailing.push(0); + assert!(unpack_bytes("test", &trailing).is_err()); + } + + #[test] + fn state_bound_covers_the_fixed_board_working_sets() { + assert!(ODYSSEY_STATE_MAX_BYTES > ODSY_WIDTH as usize * ODSY_HEIGHT as usize); + } + + #[test] + fn every_vpro_product_round_trips_architectural_state_and_vram() { + for (index, product, memory_mb) in [ + (1u32, "v6", 32u64), + (2, "v8", 128), + (3, "v10", 32), + (4, "v12", 128), + ] { + let vpro = board(product); + let marker = 0x5100_0000 | index; + vpro.state.with_stopped_board(|board| { + assert_eq!(board.vram.size(), memory_mb << 20); + board.widget.memory_control = marker; + board.query.stage = index; + board.query.count = marker; + board.cmd.state = 1; + board.cmd.tag = marker ^ 0x1111; + board.cmd.operand_count = 2; + board.cmd.operand_index = 1; + board.cmd.operand[0] = marker ^ 0x2222; + board.cmd.from_kernel = true; + board.prim.in_prim = true; + board.prim.begin_open = true; + board.prim.restart_armed = true; + board.prim.open_vertex[0].word = marker ^ 0x3333; + board.assembly.polygon.vertices = + vec![index as f32; super::super::assembly::vtx::FLOATS]; + board.assembly.polygon.count = 1; + board.tex_mut().path = marker ^ 0x4444; + board + .vram + .ptr_shared() + .write_words_untracked(0, &[u64::from(marker), !u64::from(marker)]); + }); + { + let mut front = vpro.state.front.lock(); + front.sw_token = marker; + front.swap_arm = 1 << index; + front.swap_pending = marker ^ 0x5555; + } + { + let mut fifo = vpro.state.display_fifo.lock(); + fifo.index = index; + fifo.count = 1; + fifo.ctl[index as usize] = marker ^ 0x6666; + } + + let (metadata, parts) = vpro.snapshot_parts().unwrap(); + vpro.power_on(); + vpro.restore_snapshot_parts(&metadata, &parts).unwrap(); + + vpro.state.with_stopped_board(|board| { + assert_eq!(board.widget.memory_control, marker, "{product}"); + assert_eq!( + (board.query.stage, board.query.count), + (index, marker), + "{product}" + ); + assert_eq!( + ( + board.cmd.state, + board.cmd.tag, + board.cmd.operand_count, + board.cmd.operand_index, + board.cmd.operand[0], + board.cmd.from_kernel, + ), + (1, marker ^ 0x1111, 2, 1, marker ^ 0x2222, true), + "{product}" + ); + assert!( + board.prim.in_prim && board.prim.begin_open && board.prim.restart_armed, + "{product}" + ); + assert_eq!(board.prim.open_vertex[0].word, marker ^ 0x3333, "{product}"); + assert_eq!(board.assembly.polygon.count, 1, "{product}"); + assert_eq!( + board.assembly.polygon.vertices, + vec![index as f32; super::super::assembly::vtx::FLOATS], + "{product}" + ); + assert_eq!(board.tex_regs().path, marker ^ 0x4444, "{product}"); + assert_eq!( + board.vram.ptr_shared().words_at(0, 16), + [u64::from(marker), !u64::from(marker)], + "{product}" + ); + }); + let front = vpro.state.front.lock(); + assert_eq!( + (front.sw_token, front.swap_arm, front.swap_pending), + (marker, 1 << index, marker ^ 0x5555), + "{product}" + ); + drop(front); + let fifo = vpro.state.display_fifo.lock(); + assert_eq!( + (fifo.index, fifo.count, fifo.ctl[index as usize]), + (index, 1, marker ^ 0x6666), + "{product}" + ); + } + } + + #[test] + fn invalid_or_mismatched_snapshots_do_not_mutate_the_board() { + let source = board("v6"); + source.state.with_stopped_board(|board| { + board.widget.memory_control = 0x1111_1111; + board + .vram + .ptr_shared() + .write_words_untracked(0, &[0x1111_1111_1111_1111]); + }); + let (metadata, parts) = source.snapshot_parts().unwrap(); + + let target = board("v10"); + target.state.with_stopped_board(|board| { + board.widget.memory_control = 0xaaaa_aaaa; + board + .vram + .ptr_shared() + .write_words_untracked(0, &[0xaaaa_aaaa_aaaa_aaaa]); + }); + let error = target + .restore_snapshot_parts(&metadata, &parts) + .unwrap_err(); + assert!(error.contains("running board is v10"), "{error}"); + target.state.with_stopped_board(|board| { + assert_eq!(board.widget.memory_control, 0xaaaa_aaaa); + assert_eq!( + board.vram.ptr_shared().words_at(0, 8), + [0xaaaa_aaaa_aaaa_aaaa] + ); + }); + + let mut bad_metadata: toml::Value = toml::from_str(&metadata).unwrap(); + bad_metadata + .as_table_mut() + .unwrap() + .insert("memory_bytes".into(), toml::Value::Integer(64 << 20)); + let bad_metadata = toml::to_string(&bad_metadata).unwrap(); + source.state.with_stopped_board(|board| { + board.widget.memory_control = 0xbbbb_bbbb; + board + .vram + .ptr_shared() + .write_words_untracked(0, &[0xbbbb_bbbb_bbbb_bbbb]); + }); + let error = source + .restore_snapshot_parts(&bad_metadata, &parts) + .unwrap_err(); + assert!( + error.contains("64") && error.contains("running board"), + "{error}" + ); + source.state.with_stopped_board(|board| { + assert_eq!(board.widget.memory_control, 0xbbbb_bbbb); + assert_eq!( + board.vram.ptr_shared().words_at(0, 8), + [0xbbbb_bbbb_bbbb_bbbb] + ); + }); + } + + #[test] + fn named_parts_are_exact_and_power_on_still_resets_restored_state() { + let vpro = board("v6"); + vpro.state + .with_stopped_board(|board| board.widget.memory_control = 0x1234_5678); + let (metadata, mut parts) = vpro.snapshot_parts().unwrap(); + + assert!(vpro + .restore_snapshot_parts(&metadata, &parts[..1]) + .unwrap_err() + .contains("expected 2")); + let duplicate = vec![parts[0].clone(), parts[0].clone()]; + assert!(vpro + .restore_snapshot_parts(&metadata, &duplicate) + .unwrap_err() + .contains("duplicate")); + parts[1].0 = "other".into(); + assert!(vpro + .restore_snapshot_parts(&metadata, &parts) + .unwrap_err() + .contains("vram")); + parts[1].0 = "vram".into(); + + vpro.power_on(); + vpro.restore_snapshot_parts(&metadata, &parts).unwrap(); + vpro.state + .with_stopped_board(|board| assert_eq!(board.widget.memory_control, 0x1234_5678)); + vpro.power_on(); + vpro.state.with_stopped_board(|board| { + assert_eq!(board.widget.memory_control, 0); + assert_eq!(board.vram.ptr_shared().words_at(0, 8), [0]); + }); + } + + #[cfg(feature = "wgpu")] + #[test] + fn gpu_restore_reseeds_authoritative_memory() { + let heart = Arc::new(Heart::new( + HeartConfig::octane([128 << 20, 0, 0, 0]), + host_clock(), + )); + let ram = Arc::new(HeartRam::try_new([128, 0, 0, 0]).expect("ram")); + let vpro = match Odyssey::new( + 11, + "v6", + heart, + ram.dma_view(), + crate::disp::StatusSource::detached(), + (2, 32), + (crate::config::RasterUnit::Wgpu, "auto"), + ) { + Ok(vpro) => vpro, + Err(error) => { + eprintln!("skipped: {error}"); + return; + } + }; + let marker = 0x51c2_93a4_b5d6_e7f8; + vpro.state.with_stopped_board(|board| { + board.vram.ptr_shared().store(0, 8, marker); + board.gpu_upload_tick(true); + }); + let (metadata, parts) = vpro.snapshot_parts().unwrap(); + vpro.power_on(); + vpro.restore_snapshot_parts(&metadata, &parts).unwrap(); + let (mirror, gpu) = vpro.state.with_stopped_board(|board| { + let mirror = board.vram.ptr_shared().words_at(0, 8)[0]; + let gpu = board.raster.run_now(|ctx| { + ctx.gpu + .as_mut() + .expect("GPU unit") + .read_words_for(super::super::gpu_unit::Observation::Validation) + .map(|words| words[0]) + }); + (mirror, gpu) + }); + assert_eq!(mirror, marker); + assert_eq!(gpu.unwrap(), marker); + } +} diff --git a/src/dev/odyssey/tests/fault.rs b/src/dev/odyssey/tests/fault.rs new file mode 100644 index 000000000..b54792aa3 --- /dev/null +++ b/src/dev/odyssey/tests/fault.rs @@ -0,0 +1,59 @@ +//! GPU residency failures stop processor and display access until reset. + +use super::VPRO_WIDGET; +use crate::dev::heart::{host_clock, Heart, HeartConfig}; +use crate::dev::heart_mem::HeartRam; +use crate::dev::odyssey::Odyssey; +use crate::display::{Composed, Want}; +use crate::traits::{BusDevice, Resettable, BUS_ERR, BUS_OK}; +use std::sync::Arc; + +fn board() -> Arc { + let heart = Arc::new(Heart::new( + HeartConfig::octane([128 << 20, 0, 0, 0]), + host_clock(), + )); + let ram = Arc::new(HeartRam::try_new([128, 0, 0, 0]).expect("ram")); + Odyssey::new( + VPRO_WIDGET as u8, + "v12", + heart, + ram.dma_view(), + crate::disp::StatusSource::detached(), + (2, 32), + (crate::config::RasterUnit::Cpu, "auto"), + ) + .expect("board") +} + +fn identity() -> u64 { + crate::ip30::widget_large_base(VPRO_WIDGET as u8) + 0x4 +} + +#[test] +fn gpu_fault_fails_bus_and_scanout_closed_until_reset() { + let v = board(); + assert_eq!(v.read32(identity()).status, BUS_OK); + + *v.state.shared.gpu_fault.lock() = Some("injected GPU fault".into()); + assert_eq!(v.read32(identity()).status, BUS_ERR); + assert_eq!(v.write32(identity(), 0), BUS_ERR); + + let mut screen = v.display.screen().lock(); + assert!(matches!( + v.display.compose( + &v.state.shared, + &mut screen, + Want { + readback: true, + gpu: false + } + ), + Composed::NotReady + )); + drop(screen); + + v.power_on(); + assert!(v.state.gpu_fault().is_none()); + assert_eq!(v.read32(identity()).status, BUS_OK); +} diff --git a/src/dev/odyssey/tests/mod.rs b/src/dev/odyssey/tests/mod.rs index 96ea31208..4360567c3 100644 --- a/src/dev/odyssey/tests/mod.rs +++ b/src/dev/odyssey/tests/mod.rs @@ -23,6 +23,7 @@ mod display; mod fills; mod dma; mod fifo; +mod fault; mod index; mod interrupts; mod monitor; diff --git a/src/dev/odyssey/wgpu/mod.rs b/src/dev/odyssey/wgpu/mod.rs index f2fd469a8..a9eb60422 100644 --- a/src/dev/odyssey/wgpu/mod.rs +++ b/src/dev/odyssey/wgpu/mod.rs @@ -32,7 +32,7 @@ use crate::wgpu::{library, ArchShaders, Batch, BatchPipelines, Fragment, Full, G use super::consts::{dispmode, write, ODSY_DID_MAIN_SHIFT}; use super::field::Codec; -use super::gpu_unit::{BlitKey, BlitView, CopyOp, CopySelect, LineWords}; +use super::gpu_unit::{BlitKey, BlitView, CopyOp, CopySelect, LineWords, Observation}; use super::pipe::backend::{ClearDraw, DestMem}; use super::pipe::ring::BackQuad; use super::pipe::setup::Tri; @@ -40,6 +40,7 @@ use super::pipe::state::{BackendWords, RasterWords}; use super::pipe::texture; use super::view::{Kind, Rows, View, MAX_DIM}; use super::vram::{VramPtr, DIRTY_BLOCK_SHIFT}; +use super::reader::{fail_gpu, GpuFault}; /// The plane the batch grid covers, per axis, in pixels: records beyond /// it are dropped and counted. @@ -817,6 +818,7 @@ pub(crate) struct GpuRaster { /// before each batch; the blocks the batches wrote come back on /// `read_back`. mirror: VramPtr, + fault: GpuFault, /// Blocks (`DIRTY_BLOCK_SHIFT`) the batches since the last readback may /// have written, from the records' boxes and their slots' views. gpu_dirty: Vec, @@ -830,9 +832,12 @@ pub(crate) struct GpuRaster { pub uploaded_tiles: u64, pub read_back_bytes: u64, pub readbacks: u64, + observations: [(u64, u64); Observation::COUNT], /// Waits for a submission (`wait`), and batches `tick` submitted /// because the board went idle or the batch had been open too long. pub waits: u64, + pub wait_ns: u64, + pub readback_ns: u64, pub flushes_idle: u64, pub flushes_timed: u64, /// Measurement only: `read_back` flushes and returns without waiting @@ -849,6 +854,8 @@ pub(crate) struct GpuRaster { copy: CopyKernel, /// Copies the kernel took (`copy`). pub copies: u64, + pub rejections: u64, + pub faults: u64, } @@ -943,6 +950,7 @@ impl GpuRaster { stream_now: 0, stream: None, mirror: VramPtr::null(), + fault: Arc::new(parking_lot::Mutex::new(None)), gpu_dirty: vec![0; blocks.div_ceil(64)], gpu_dirty_any: false, dropped: 0, @@ -951,7 +959,10 @@ impl GpuRaster { uploaded_tiles: 0, read_back_bytes: 0, readbacks: 0, + observations: [(0, 0); Observation::COUNT], waits: 0, + wait_ns: 0, + readback_ns: 0, flushes_idle: 0, flushes_timed: 0, lazy: false, @@ -959,6 +970,43 @@ impl GpuRaster { flushes_by: std::env::var("VPRO_GPU_TRACE_WAITS").is_ok().then(std::collections::BTreeMap::new), copy, copies: 0, + rejections: 0, + faults: 0, + }) + } + + pub fn attach_fault(&mut self, fault: GpuFault) { + self.fault = fault; + } + + pub fn fault(&self) -> Option { + self.fault.lock().clone() + } + + fn fail(&mut self, operation: &str, error: impl std::fmt::Display) -> String { + let first = self.fault.lock().is_none(); + let message = fail_gpu(&self.fault, operation, error); + if first { + self.faults += 1; + } + message + } + + pub fn reject(&mut self, operation: &str, reason: String) { + self.rejections += 1; + self.fail(operation, reason); + } + + fn note_observation(&mut self, reason: Observation, bytes: u64) { + let entry = &mut self.observations[reason as usize]; + entry.0 += 1; + entry.1 += bytes; + } + + pub fn observation_counts(&self) -> impl Iterator + '_ { + Observation::ALL.into_iter().map(|reason| { + let (calls, bytes) = self.observations[reason as usize]; + (reason.name(), calls, bytes) }) } @@ -1011,13 +1059,13 @@ impl GpuRaster { } } - /// The marked blocks as byte runs, unmarked. - fn take_gpu_dirty(&mut self) -> Vec<(u64, u64)> { + /// The marked blocks as byte runs. + fn gpu_dirty_runs(&self) -> Vec<(u64, u64)> { let mut runs: Vec<(u64, u64)> = Vec::new(); let block = 1u64 << DIRTY_BLOCK_SHIFT; let blocks = self.vram.size() >> DIRTY_BLOCK_SHIFT; for i in 0..self.gpu_dirty.len() { - let mut bits = std::mem::take(&mut self.gpu_dirty[i]); + let mut bits = self.gpu_dirty[i]; while bits != 0 { let b = (i as u64) * 64 + bits.trailing_zeros() as u64; bits &= bits - 1; @@ -1031,10 +1079,20 @@ impl GpuRaster { } } } - self.gpu_dirty_any = false; runs } + fn clear_gpu_dirty(&mut self, runs: &[(u64, u64)]) { + for &(off, len) in runs { + let first = off >> DIRTY_BLOCK_SHIFT; + let last = (off + len - 1) >> DIRTY_BLOCK_SHIFT; + for block in first..=last { + self.gpu_dirty[(block >> 6) as usize] &= !(1 << (block & 63)); + } + } + self.gpu_dirty_any = self.gpu_dirty.iter().any(|word| *word != 0); + } + /// Upload the mirror's bytes of view `v` over its pixel box (inclusive) /// now, tile row by tile row, ahead of what is submitted next: for a /// writer that stored into memory no pending drawing touches (a @@ -1042,6 +1100,9 @@ impl GpuRaster { /// upload is the pixels it wrote and not their whole 16 KB blocks, /// which may hold GPU-written bytes the mirror has not read back. pub fn upload_region(&mut self, v: &super::view::View, x0: i32, y0: i32, x1: i32, y1: i32) { + if self.fault().is_some() { + return; + } // An upload lands ahead of the batch: the pixels recorded so far // must be in a batch of their own first. self.flush(); @@ -1125,7 +1186,7 @@ impl GpuRaster { pub fn report(&self) -> String { let (flushes, records) = self.counts(); let mut out = format!( - " gpu unit: {records} records in {flushes} batches ({} on idle, {} timed, {} dropped), {} open now; {} MiB up ({} MiB blocks, {} MiB regions), {} waits, {} readbacks ({} MiB), {} copies\n", + " gpu unit: {records} records in {flushes} batches ({} on idle, {} timed, {} dropped), {} open now; {} MiB up ({} MiB blocks, {} MiB regions), {} waits ({} us), {} readbacks ({} MiB, {} us), {} copies, {} rejections, {} faults\n", self.flushes_idle, self.flushes_timed, self.dropped, @@ -1134,10 +1195,20 @@ impl GpuRaster { self.uploaded_blocks >> 20, self.uploaded_tiles >> 20, self.waits, + self.wait_ns / 1_000, self.readbacks, self.read_back_bytes >> 20, + self.readback_ns / 1_000, self.copies, + self.rejections, + self.faults, ); + for (reason, calls, bytes) in self.observation_counts().filter(|(_, calls, _)| *calls != 0) { + out.push_str(&format!(" {reason}: {calls} readbacks, {bytes} bytes\n")); + } + if let Some(fault) = self.fault() { + out.push_str(&format!(" fault: {fault}\n")); + } if let Some(by) = &self.waits_by { let mut v: Vec<_> = by.iter().collect(); v.sort_by(|a, b| b.1.cmp(a.1)); @@ -1158,43 +1229,49 @@ impl GpuRaster { } /// Submit and wait, then bring every block the batches may have written - /// back into the mirror: `Board::raster_wait` for the GPU unit. Cheap - /// when nothing was recorded since the last readback. - pub fn read_back(&mut self) -> Result<(), String> { - if self.lazy { + /// back into the mirror. Cheap when nothing was recorded since the + /// last readback. + pub fn read_back_now(&mut self, reason: Observation) -> Result<(), String> { + if let Some(fault) = self.fault() { + return Err(fault); + } + if self.lazy && matches!(reason, Observation::Frontend | Observation::DmaRead | Observation::CopySource) { self.flush(); - return self.gpu.check(); + return self.gpu.check().map_err(|e| self.fail("GPU lazy readback check", e)); } - self.read_back_now() - } - - /// `read_back` whether lazy or not. - pub fn read_back_now(&mut self) -> Result<(), String> { self.flush(); if !self.gpu_dirty_any { - return self.gpu.check(); + return self.gpu.check().map_err(|e| self.fail("GPU readback check", e)); } self.note_wait(); - let runs = self.take_gpu_dirty(); - let bytes = self.vram.read_ranges(&runs)?; + let runs = self.gpu_dirty_runs(); + let started = std::time::Instant::now(); + let bytes = self.vram.read_ranges(&runs).map_err(|e| self.fail("GPU readback", e))?; + self.gpu.check().map_err(|e| self.fail("GPU readback check", e))?; self.pending = None; let words: &[u64] = bytemuck::cast_slice(&bytes); let mut at = 0usize; - for (off, len) in runs { + for (off, len) in &runs { let n = (len >> 3) as usize; - self.mirror.write_words_untracked(off, &words[at..at + n]); + self.mirror.write_words_untracked(*off, &words[at..at + n]); at += n; } + self.clear_gpu_dirty(&runs); self.read_back_bytes += bytes.len() as u64; self.readbacks += 1; - self.gpu.check() + self.readback_ns += started.elapsed().as_nanos() as u64; + self.note_observation(reason, bytes.len() as u64); + Ok(()) } /// Bring the mirror current over view `v`'s pixel box (inclusive): the /// blocks of its tile rows the batches may have written, and only /// those. Nothing waits when none of them is marked, which is the /// common case for a front end reading pixels one at a time. - pub fn read_back_view(&mut self, v: &super::view::View, x0: i32, y0: i32, x1: i32, y1: i32) -> Result<(), String> { + pub fn read_back_view(&mut self, v: &super::view::View, x0: i32, y0: i32, x1: i32, y1: i32, reason: Observation) -> Result<(), String> { + if let Some(fault) = self.fault() { + return Err(fault); + } // A blit run still being collected has marked nothing yet: close // it, so its blocks count. self.end_pending(); @@ -1214,14 +1291,13 @@ impl GpuRaster { let hi = base + (((ty * pitch + tx1 + 1) << 8) << log2); if hi > size { // Wrapping addresses: everything. - return self.read_back_now(); + return self.read_back_now(reason); } for b in (lo >> DIRTY_BLOCK_SHIFT)..=((hi - 1) >> DIRTY_BLOCK_SHIFT) { let (w, bit) = ((b >> 6) as usize, 1u64 << (b & 63)); if self.gpu_dirty[w] & bit == 0 { continue; } - self.gpu_dirty[w] &= !bit; let off = b * block; match runs.last_mut() { Some((o, l)) if *o + *l == off => *l += block, @@ -1236,7 +1312,9 @@ impl GpuRaster { // drawn once the flush below has run. self.flush(); self.note_wait(); - let bytes = self.vram.read_ranges(&runs)?; + let started = std::time::Instant::now(); + let bytes = self.vram.read_ranges(&runs).map_err(|e| self.fail("GPU region readback", e))?; + self.gpu.check().map_err(|e| self.fail("GPU region readback check", e))?; let words: &[u64] = bytemuck::cast_slice(&bytes); let mut at = 0usize; for (off, len) in &runs { @@ -1244,24 +1322,31 @@ impl GpuRaster { self.mirror.write_words_untracked(*off, &words[at..at + n]); at += n; } + self.clear_gpu_dirty(&runs); self.read_back_bytes += bytes.len() as u64; self.readbacks += 1; - self.gpu_dirty_any = self.gpu_dirty.iter().any(|w| *w != 0); - self.gpu.check() + self.readback_ns += started.elapsed().as_nanos() as u64; + self.note_observation(reason, bytes.len() as u64); + Ok(()) } /// The copy `op` (block 0x2031) on the unit: the records before it are /// submitted first, then a gather of the source into the scratch and a /// scatter into the destination, in order on the queue; nothing waits - /// and nothing is read back. False when the unit cannot take it (a view - /// the kernels cannot address, more pixels than the scratch holds): the - /// CPU then copies on a mirror brought current. - pub fn copy(&mut self, op: &CopyOp) -> bool { + /// and nothing is read back. An unsupported copy is returned to the + /// board, which faults rather than changing the framebuffer on the CPU. + pub fn copy(&mut self, op: &CopyOp) -> Result<(), Unsupported> { + if let Some(fault) = self.fault() { + return Err(Unsupported(fault)); + } let (w, h) = op.size; - if w == 0 || h == 0 || w as u64 * h as u64 > COPY_SCRATCH_PIXELS as u64 { - return false; + if w == 0 || h == 0 { + return Ok(()); + } + if w as u64 * h as u64 > COPY_SCRATCH_PIXELS as u64 { + return Err(Unsupported(format!("a {w}x{h} copy exceeds the GPU scratch"))); } - let Ok(u) = CopyUniform::of(op) else { return false }; + let u = CopyUniform::of(op)?; // What was recorded goes first, and the mirror's changes with it // (the source may be the CPU's). self.flush(); @@ -1281,7 +1366,7 @@ impl GpuRaster { let (x, y) = op.dst_xy; self.mark_gpu(&s, (x, y, x.wrapping_add(w as i32 - 1), y.wrapping_add(h as i32 - 1))); self.copies += 1; - true + Ok(()) } // The quad ring's commands (pipe/ring.rs) as records. @@ -1337,6 +1422,9 @@ impl GpuRaster { /// `push` with the record's texture slot resolved in the same batch. fn push_with_tex(&mut self, mut r: Rec, s: &Slot, tex: Option<&TexSlot>) { + if self.fault().is_some() { + return; + } self.end_pending(); loop { let ok = match tex { @@ -1361,6 +1449,9 @@ impl GpuRaster { /// same slot: a line's pixels, each a quad of its own, take one record /// per P_MAX pixels instead of one each. pub fn push_frag(&mut self, q: &BackQuad, slot: &Slot) { + if self.fault().is_some() { + return; + } if q.cov == 0b1111 { self.push(Rec::frag(q), slot); return; @@ -1421,6 +1512,9 @@ impl GpuRaster { /// Record `r` under slot `s`, flushing a full batch first. pub fn push(&mut self, r: Rec, s: &Slot) { + if self.fault().is_some() { + return; + } self.end_pending(); self.push_record(r, s); } @@ -1438,6 +1532,9 @@ impl GpuRaster { /// touches): the rectangle grows and goes up when it closes, after the /// records before it and before any after it. pub fn note_region(&mut self, v: &super::view::View, x: i32, y: i32) { + if self.fault().is_some() { + return; + } match &mut self.region { Some((rv, x0, y0, x1, y1)) if *rv == *v => { *x0 = (*x0).min(x); @@ -1474,6 +1571,9 @@ impl GpuRaster { /// with one key join one blit record; anything else the unit does /// first closes the run, so the pixels keep their place in the order. pub fn blit_pixel(&mut self, s: &Slot, key: &BlitKey, x: i32, y: i32, word: u32) { + if self.fault().is_some() { + return; + } let joins = self.run.as_ref().is_some_and(|r| r.y == y && r.next_x == x && r.key == *key && r.slot == *s); if !joins { self.end_pending(); @@ -1561,6 +1661,9 @@ impl GpuRaster { /// waits. #[track_caller] pub fn flush(&mut self) { + if self.fault().is_some() { + return; + } if let Some(by) = &mut self.flushes_by { if !self.batch.is_empty() { *by.entry(std::panic::Location::caller().line()).or_insert(0) += 1; @@ -1601,16 +1704,23 @@ impl GpuRaster { /// Submit and wait until the GPU has stored everything recorded, so /// memory can be read. pub fn wait(&mut self) -> Result<(), String> { + if let Some(fault) = self.fault() { + return Err(fault); + } self.flush(); // Nothing submitted since the last wait: nothing to wait for, and // a poll would also wait for the display's scanout. let Some(sub) = self.pending.take() else { - return self.gpu.check(); + return self.gpu.check().map_err(|e| self.fail("GPU wait check", e)); }; self.waits += 1; + let started = std::time::Instant::now(); let r = self.gpu.wait(Some(sub)); - self.gpu.check()?; - r + self.wait_ns += started.elapsed().as_nanos() as u64; + if let Err(e) = self.gpu.check() { + return Err(self.fail("GPU wait check", e)); + } + r.map_err(|e| self.fail("GPU wait", e)) } /// Batches flushed and records drawn so far. @@ -1626,6 +1736,9 @@ impl GpuRaster { /// Write `words` at byte offset `offset` of board memory, in the /// model's word order (vram.rs: a `Vram`'s words as bytes). pub fn write_words(&mut self, offset: u64, words: &[u64]) { + if self.fault().is_some() { + return; + } self.flush(); self.vram.write(offset, bytemuck::cast_slice(words)); self.uploaded += 8 * words.len() as u64; @@ -1642,10 +1755,38 @@ impl GpuRaster { self.mirror.clear_dirty(); } + pub fn reseed_from_mirror(&mut self) -> Result<(), String> { + self.wait()?; + self.vram.write( + 0, + bytemuck::cast_slice(self.mirror.words_at(0, self.mirror.size())), + ); + self.gpu + .check() + .map_err(|e| self.fail("GPU memory restore", e))?; + for word in &mut self.gpu_dirty { + *word = 0; + } + self.gpu_dirty_any = false; + self.mirror.clear_dirty(); + self.uploaded += self.mirror.size(); + Ok(()) + } + /// Board memory as the model's words, after a wait. pub fn read_words(&mut self) -> Result, String> { + self.read_words_for(Observation::Validation) + } + + pub fn read_words_for(&mut self, reason: Observation) -> Result, String> { self.wait()?; - let bytes = self.vram.read_all()?; + let started = std::time::Instant::now(); + let bytes = self.vram.read_all().map_err(|e| self.fail("GPU memory read", e))?; + self.gpu.check().map_err(|e| self.fail("GPU memory read check", e))?; + self.readback_ns += started.elapsed().as_nanos() as u64; + self.read_back_bytes += bytes.len() as u64; + self.readbacks += 1; + self.note_observation(reason, bytes.len() as u64); Ok(bytemuck::cast_slice(&bytes).to_vec()) } @@ -1653,7 +1794,7 @@ impl GpuRaster { /// highest byte of each word (`VramPtr::address_bytes`, what /// `debug::memory_hash_bytes` hashes). pub fn hash(&mut self) -> Result<[u8; 32], String> { - let words = self.read_words()?; + let words = self.read_words_for(Observation::StateHash)?; let mut h = blake3::Hasher::new(); let mut buf = Vec::with_capacity(1 << 16); for w in words { diff --git a/src/dev/odyssey/wgpu/shaders/vpro.wgsl b/src/dev/odyssey/wgpu/shaders/vpro.wgsl index 224bdf4f6..6b6b4fff7 100644 --- a/src/dev/odyssey/wgpu/shaders/vpro.wgsl +++ b/src/dev/odyssey/wgpu/shaders/vpro.wgsl @@ -4,9 +4,9 @@ // (setup::Tri); a fragment record is a quad the CPU shaded itself; a // rectangle record is a clear's. The arithmetic follows pipe/raster.rs, // pipe/interp.rs, pipe/fixed.rs and pipe/backend.rs operation for -// operation; where a product feeds a sum, it is multiplied by a one the -// compiler cannot see through (`nc`), so no driver fuses it into a -// multiply-add the CPU does not do, and quotients go through +// operation; where a product feeds a sum, its f32 bits are made observable +// through a one the compiler cannot see through (`nc`), so no driver fuses +// it into a multiply-add the CPU does not do, and quotients go through // `div_exact` (src/wgpu/shaders/float.wgsl), since a GPU's division need // not be correctly rounded. // @@ -107,10 +107,11 @@ fn r64(pi: u32, i: u32) -> I64 { return I64(recs[pi].w[i], recs[pi].w[i + 1u]); } -// x * 1.0 through a value the compiler cannot fold: the product is -// rounded on its own before it joins a sum. +// Make x's f32 bits observable through a value the compiler cannot fold, +// so its producing operation is rounded before it joins a sum. fn p(x: f32) -> f32 { - return x * nc; + let mask = bitcast(nc) ^ 0x3f800000u; + return bitcast(bitcast(x) ^ mask); } // Rust's `as i32`: truncation, saturating, NaN to 0. diff --git a/src/dev/odyssey/wgpu/tests.rs b/src/dev/odyssey/wgpu/tests.rs index 82f3f8d41..082bbce07 100644 --- a/src/dev/odyssey/wgpu/tests.rs +++ b/src/dev/odyssey/wgpu/tests.rs @@ -22,7 +22,7 @@ use super::super::pipe::texture::{Latch, TexRegs, Unit, Wrap}; use super::super::consts::tex; use super::super::view::DestViews; use super::super::vram::Vram; -use super::{GpuRaster, Rec, Slot, TexSlot}; +use super::{GpuRaster, Observation, Rec, Slot, TexSlot, DIRTY_BLOCK_SHIFT}; use crate::wgpu::{Gpu, GpuOptions}; /// The test memory: 4 MB, a power of two. @@ -78,6 +78,44 @@ fn gpu() -> Option> { } } +#[test] +fn readback_observations_count_calls_and_bytes() { + let Some(gpu) = gpu() else { return }; + let mut mirror = Vram::new(VRAM_BYTES); + let mut raster = GpuRaster::new(&gpu, VRAM_BYTES, GRID, GRID).expect("raster"); + raster.attach_mirror(mirror.ptr()); + raster.gpu_dirty[0] = 1; + raster.gpu_dirty_any = true; + + raster.read_back_now(Observation::DmaRead).expect("readback"); + + let counts: Vec<_> = raster.observation_counts().collect(); + assert_eq!( + counts.iter().copied().find(|(name, _, _)| *name == "dma-read"), + Some(("dma-read", 1, 1 << DIRTY_BLOCK_SHIFT)) + ); + assert!(counts.iter().filter(|(name, _, _)| *name != "dma-read").all(|(_, calls, bytes)| *calls == 0 && *bytes == 0)); +} + +#[test] +fn device_loss_keeps_dirty_readback_metadata_and_sets_fault() { + let Some(gpu) = gpu() else { return }; + let mut mirror = Vram::new(VRAM_BYTES); + let mut raster = GpuRaster::new(&gpu, VRAM_BYTES, GRID, GRID).expect("raster"); + raster.attach_mirror(mirror.ptr()); + raster.gpu_dirty[0] = 1; + raster.gpu_dirty_any = true; + gpu.inject_device_loss("dirty readback"); + + let error = raster.read_back_now(Observation::Validation).expect_err("device loss"); + + assert!(error.contains("device lost"), "{error}"); + assert_eq!(raster.gpu_dirty[0], 1); + assert!(raster.gpu_dirty_any); + assert!(raster.fault().is_some()); + assert_eq!(raster.faults, 1); +} + /// Both memories and the destination they share. struct Pair { cpu: Vram, diff --git a/src/dev/w1/bus.rs b/src/dev/w1/bus.rs index 9a5fb86a9..54c9d0f5a 100644 --- a/src/dev/w1/bus.rs +++ b/src/dev/w1/bus.rs @@ -51,6 +51,15 @@ pub struct Bus { cmd: u8, } +struct BusState { + sel: Vec, + replies: Vec>, + phase: Phase, + acc: u32, + nbits: u32, + cmd: u8, +} + impl Default for Bus { fn default() -> Bus { Bus::new() @@ -229,6 +238,20 @@ impl Phase { if matches!(tag, 1..=3) && a >= 64 { return Err(format!("1-Wire phase {tag}: ROM bit {a}, past the 64")); } + if tag == 3 && b > 2 { + return Err(format!("1-Wire search phase step {b}, expected 0 to 2")); + } + if tag == 6 && usize::try_from(a).is_err() { + return Err(format!("1-Wire stream bit {a} does not fit this host")); + } + if !matches!(tag, 1..=3 | 6) && (a != 0 || b != 0) { + return Err(format!( + "1-Wire phase {tag} has unexpected arguments {a}, {b}" + )); + } + if matches!(tag, 1 | 2 | 6) && b != 0 { + return Err(format!("1-Wire phase {tag} has unexpected argument {b}")); + } Ok(match tag { 0 => Phase::Rom, 1 => Phase::ReadRom(a as usize), @@ -259,7 +282,7 @@ impl Bus { .build() } - pub fn load_state(&mut self, v: &toml::Value) -> Result<(), String> { + fn decode_state(&self, v: &toml::Value) -> Result { if field_bytes(v, "roms")? != self.roms().concat() { return Err("the saved 1-Wire bus has other parts".into()); } @@ -267,16 +290,128 @@ impl Bus { if sel.len() != self.parts.len() { return Err("selected: not one entry per part".into()); } - let replies = field(v, "replies")?.as_array().ok_or("replies: not a list")?; - let replies = replies.iter() - .map(|r| r.as_array()?.iter().map(toml_u8).collect::>>()) - .collect::>>().ok_or("replies: an entry is not a byte list")?; - self.phase = Phase::from_words(field_u64s(v, "phase")?)?; - self.sel = sel.into_iter().map(|s| s != 0).collect(); - self.replies = replies; - self.acc = field_u32(v, "acc")?; - self.nbits = field_u32(v, "nbits")?; - self.cmd = field_u8(v, "cmd")?; + if sel.iter().any(|&selected| selected > 1) { + return Err("selected: entries must be zero or one".into()); + } + let replies = field(v, "replies")? + .as_array() + .ok_or("replies: not a list")?; + let replies = replies + .iter() + .map(|r| { + r.as_array()? + .iter() + .map(toml_u8) + .collect::>>() + }) + .collect::>>() + .ok_or("replies: an entry is not a byte list")?; + let phase = Phase::from_words(field_u64s(v, "phase")?)?; + let acc = field_u32(v, "acc")?; + let nbits = field_u32(v, "nbits")?; + let pending_bits = match phase { + Phase::Rom | Phase::Func => 8, + Phase::Addr => 16, + _ => 0, + }; + if nbits >= pending_bits && (pending_bits != 0 || nbits != 0) { + return Err(format!( + "1-Wire phase has {nbits} pending bits, expected less than {pending_bits}" + )); + } + if nbits < 32 && acc >> nbits != 0 { + return Err(format!( + "1-Wire accumulator {acc:#x} has bits above its {nbits}-bit length" + )); + } + let selected = sel.iter().filter(|&&selected| selected != 0).count(); + if matches!(phase, Phase::Stream(_)) { + if replies.len() != selected { + return Err(format!( + "replies: {} streams for {selected} selected parts", + replies.len() + )); + } + } else if !replies.is_empty() { + return Err("replies: streams exist outside the stream phase".into()); + } + Ok(BusState { + sel: sel.into_iter().map(|s| s != 0).collect(), + replies, + phase, + acc, + nbits, + cmd: field_u8(v, "cmd")?, + }) + } + + pub fn validate_state(&self, v: &toml::Value) -> Result<(), String> { + self.decode_state(v).map(|_| ()) + } + + pub fn load_state(&mut self, v: &toml::Value) -> Result<(), String> { + let state = self.decode_state(v)?; + self.phase = state.phase; + self.sel = state.sel; + self.replies = state.replies; + self.acc = state.acc; + self.nbits = state.nbits; + self.cmd = state.cmd; Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + struct TestPart; + + impl Part for TestPart { + fn rom(&self) -> [u8; 8] { + [0x91, 1, 2, 3, 4, 5, 6, 7] + } + + fn reply(&self, _cmd: u8, _addr: u16) -> Option> { + Some(vec![1, 2, 3]) + } + } + + #[test] + fn a_late_decode_error_does_not_partially_change_the_bus() { + let mut bus = Bus::new(); + bus.add(Box::new(TestPart)); + bus.reset(); + let before = bus.save_state(); + let mut invalid = before.clone(); + let table = invalid.as_table_mut().unwrap(); + table.insert( + "phase".into(), + crate::snapshot::u64_slice_to_toml(&[4, 0, 0]), + ); + table.insert("cmd".into(), toml::Value::String("not a byte".into())); + + assert!(bus.load_state(&invalid).is_err()); + assert_eq!(bus.save_state(), before); + } + + #[test] + fn impossible_selection_and_search_state_are_rejected() { + let mut bus = Bus::new(); + bus.add(Box::new(TestPart)); + bus.reset(); + let mut invalid = bus.save_state(); + invalid + .as_table_mut() + .unwrap() + .insert("selected".into(), crate::snapshot::u8_slice_to_toml(&[2])); + assert!(bus.validate_state(&invalid).is_err()); + + let mut invalid = bus.save_state(); + invalid.as_table_mut().unwrap().insert( + "phase".into(), + crate::snapshot::u64_slice_to_toml(&[3, 0, 3]), + ); + assert!(bus.validate_state(&invalid).is_err()); + } +} diff --git a/src/disp.rs b/src/disp.rs index c00c26687..5db545ba4 100644 --- a/src/disp.rs +++ b/src/disp.rs @@ -30,7 +30,7 @@ pub const FB_MAX_H: usize = 2048; /// Independent revisions of the cached Newport display state. The source /// identifies the screen that issued them, so replacing a screen cannot make /// an old compositor cache appear current. -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct Ng1DisplayGenerations { pub(crate) source: u64, @@ -43,7 +43,7 @@ pub(crate) struct Ng1DisplayGenerations { pub(crate) geometry: u64, } -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] impl Ng1DisplayGenerations { fn new() -> Self { static NEXT_SOURCE: AtomicU64 = AtomicU64::new(1); @@ -60,7 +60,7 @@ impl Ng1DisplayGenerations { /// A Newport display snapshot with no access to either CPU framebuffer. /// The generations change when the corresponding cached state changes. -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] pub(crate) struct Ng1DisplaySource<'a> { pub(crate) generations: Ng1DisplayGenerations, pub(crate) did: &'a [u8], @@ -142,11 +142,11 @@ pub struct Rex3Screen { /// Bumped each time `rgba` is rewritten with a composed frame, so frame /// consumers (VNC) can skip unchanged ones without comparing pixels. pub frame_seq: u64, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] display_generations: Ng1DisplayGenerations, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] display_geometry: (usize, usize, usize, i32), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_did: Mutex>, } @@ -183,11 +183,11 @@ impl Rex3Screen { fb_borrowed: false, prebuilt: false, frame_seq: 0, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] display_generations: Ng1DisplayGenerations::new(), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] display_geometry: (0, 0, 0, 0), - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_did: Mutex::new(None), } } @@ -200,9 +200,9 @@ impl Rex3Screen { did_buf: &mut [u8], ) -> bool { fn fill(dst: &mut [u8], value: u8) -> bool { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let changed = dst.iter().any(|&old| old != value); - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let changed = false; dst.fill(value); changed @@ -397,7 +397,7 @@ impl Rex3Screen { { let mut vc2 = vc2.lock(); if vc2.dirty { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] { if self.vc2_ram != vc2.ram { Ng1DisplayGenerations::bump(&mut self.display_generations.vc2_ram); @@ -417,7 +417,7 @@ impl Rex3Screen { { let mut cmap = cmap.lock(); if cmap.dirty { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.cmap != cmap.palette { Ng1DisplayGenerations::bump(&mut self.display_generations.cmap); } @@ -431,7 +431,7 @@ impl Rex3Screen { let mut dac = bt445.lock(); if dac.dirty { let palette = dac.palette_as_rgb(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.ramdac_palette != palette { Ng1DisplayGenerations::bump(&mut self.display_generations.ramdac); } @@ -444,7 +444,7 @@ impl Rex3Screen { { let mut xmap = xmap.lock(); if xmap.dirty { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if self.xmap_mode != xmap.mode_table || self.xmap_config != xmap.config || self.xmap_cursor_cmap != xmap.cursor_cmap_msb @@ -486,11 +486,11 @@ impl Rex3Screen { if decode_did { diag.fetch_or(Rex3::DIAG_LOOP_DECODE_DID, Ordering::Relaxed); let did_changed = self.decode_did(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if did_changed { Ng1DisplayGenerations::bump(&mut self.display_generations.did); } - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let _ = did_changed; diag.fetch_and(!Rex3::DIAG_LOOP_DECODE_DID, Ordering::Relaxed); } @@ -509,7 +509,7 @@ impl Rex3Screen { self.refresh_display_state_inner(vc2, xmap, cmap, bt445, diag, true) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn refresh_resident_display_state( &mut self, vc2: &Mutex, @@ -523,7 +523,7 @@ impl Rex3Screen { /// Borrow only display state. Geometry includes externally supplied /// topscan as well as the decoded dimensions and cursor adjustment. - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] pub(crate) fn display_state(&mut self) -> Ng1DisplaySource<'_> { let geometry = (self.width, self.height, self.topscan, self.cursor_x_adjust); if self.display_geometry != geometry { diff --git a/src/display/renderer.rs b/src/display/renderer.rs index a1b099395..63550dfdb 100644 --- a/src/display/renderer.rs +++ b/src/display/renderer.rs @@ -1,7 +1,7 @@ //! What a front-end installs into a head to put frames on the screen. use crate::disp::Rex3Screen; -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] use std::sync::Arc; pub trait Renderer: Send { @@ -55,12 +55,12 @@ pub trait Renderer: Send { _stats: &crate::disp::BarStats, ) {} - /// The test-only resident path must verify the presenter's actual device, + /// The resident path must verify the presenter's actual device, /// rather than relying on a renderer's claim that it takes GPU frames. - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn resident_gpu_device(&self) -> Option<&Arc> { None } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] #[allow(clippy::too_many_arguments)] fn present_resident_gpu_frame( &mut self, @@ -82,6 +82,6 @@ pub trait Renderer: Send { #[cfg(all(test, feature = "wgpu"))] fn resident_presented_texture(&self) -> Option<&wgpu::Texture> { None } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn takes_resident_readback_frames(&self) -> bool { false } } diff --git a/src/gfifo.rs b/src/gfifo.rs index 916771bfe..936daa315 100644 --- a/src/gfifo.rs +++ b/src/gfifo.rs @@ -228,7 +228,6 @@ impl GFifo { self.push_one_result(addr, val).is_ok() } - #[cfg(test)] #[inline] pub(crate) fn try_push_traced(&self, addr: u32, val: u64) -> Result<(), PushFailure> { self.push_one_result(addr, val) @@ -286,7 +285,6 @@ impl GFifo { self.push_pair_result(addr0, val0, addr1, val1).is_ok() } - #[cfg(test)] #[inline] pub(crate) fn try_push2_traced(&self, addr0: u32, val0: u64, addr1: u32, val1: u64) -> Result<(), PushFailure> { self.push_pair_result(addr0, val0, addr1, val1) @@ -374,7 +372,6 @@ impl GFifo { self.push_batch_result(token, count_val, vals).is_ok() } - #[cfg(test)] pub(crate) fn try_push_batch_traced(&self, token: u32, count_val: u64, vals: &[u64]) -> Result<(), PushFailure> { self.push_batch_result(token, count_val, vals) } diff --git a/src/gfx_display.rs b/src/gfx_display.rs index 48490a099..cfbb8b39f 100644 --- a/src/gfx_display.rs +++ b/src/gfx_display.rs @@ -47,6 +47,12 @@ pub trait GfxDisplay: Send + Sync { /// True while a [`request_frame`](Self::request_frame) is still pending. fn frame_pending(&self) -> bool { false } + /// Minimum time a frontend should allow for an explicit fresh-frame + /// request. Slow compatibility paths can raise the caller's timeout. + fn minimum_frame_request_timeout(&self) -> std::time::Duration { + std::time::Duration::ZERO + } + /// Whether a completed composed frame is available to consumers. This /// may be called with `screen()` locked and must not lock it again. fn frame_available(&self) -> bool { true } @@ -54,7 +60,7 @@ pub trait GfxDisplay: Send + Sync { /// Keep `screen().rgba` current with every composed frame while at least /// one consumer (VNC) has turned capture on. Calls nest: each `true` is /// matched by a `false`. Newport's GL capture composes in software; its - /// wgpu window and gated resident tests read back composed GPU output. + /// wgpu window and resident mode read back composed GPU output. /// Boards that always compose into `rgba` ignore it. fn set_capture(&self, _on: bool) {} @@ -67,6 +73,11 @@ pub trait GfxDisplay: Send + Sync { /// need not come back to the CPU. #[cfg(feature = "wgpu")] fn gpu(&self) -> Option> { None } + + /// Whether a visible host window must use a presenter on [`Self::gpu`]. + /// Frontends must fail startup instead of selecting another renderer. + #[cfg(feature = "wgpu")] + fn requires_same_device_wgpu_presenter(&self) -> bool { false } } /// Whether the display thread tears its renderer down when it ends. @@ -161,7 +172,7 @@ pub fn copy_frame(display: &dyn GfxDisplay, out: &mut Vec) -> Option<(usize /// pending, so the screen still holds an older frame. pub fn wait_fresh_frame(display: &dyn GfxDisplay, timeout: std::time::Duration) -> bool { display.request_frame(); - let deadline = std::time::Instant::now() + timeout; + let deadline = std::time::Instant::now() + timeout.max(display.minimum_frame_request_timeout()); while display.frame_pending() { if std::time::Instant::now() >= deadline { return false; diff --git a/src/ip30_machine.rs b/src/ip30_machine.rs index 0e76534dd..194f67bb1 100644 --- a/src/ip30_machine.rs +++ b/src/ip30_machine.rs @@ -16,6 +16,7 @@ use std::path::Path; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::Arc; +use crate::chunk_store::{get_chunks_as_words, put_words_as_chunks, ChunkStore}; use crate::config::{MachineConfig, MachineProfile}; use crate::cpu::parts::{self, Part}; use crate::cpu::mips_cache_v2::MipsCache; @@ -43,8 +44,9 @@ use crate::ip30::{ WIDGET_XBOW, }; use crate::pci::{BoardIrq, DevFn}; -use crate::chunk_store::{get_chunks_as_words, put_words_as_chunks, ChunkStore}; -use crate::snapshot::{field, ChunksManifest, Manifest, Snapshot, StateTable, SCHEMA_VERSION}; +use crate::snapshot::{ + field, ChunksManifest, Manifest, MgrasChunk, OdysseyChunk, Snapshot, StateTable, SCHEMA_VERSION, +}; use crate::traits::{BusDevice, Device, MachineEvent, Resettable, Saveable}; use crate::workstation::{MachineRef, MachineSlot, Workstation}; use crate::xtalk::{HostPort, Upstream}; @@ -963,23 +965,13 @@ impl OctaneMachine { // state in the devices is saved relative to the stop, so the guest's clocks // resume where they stopped. // -// Not supported, and refused with the reason: an IMPACT or a VPro board -// (neither has saved state yet) and two processors (the stop -// does not yet bring both to a common instruction boundary that a snapshot -// can record). In a jitv2 build, putting memory back bumps every page's -// generation, so code compiled from the memory before the load is not run. - -/// Why an Octane with an IMPACT board, a VPro board or `cpus` processors -/// cannot be saved, when it cannot. -fn snapshot_refusal_for(impact: bool, vpro: bool, virtio: bool, cpus: usize) -> Option { - if impact { - return Some("snapshots of an Octane with an IMPACT board are not supported yet: \ - its engines and display threads have no saved state".into()); - } - if vpro { - return Some("snapshots of an Octane with a VPro board are not supported yet: \ - the board has no saved state".into()); - } +// Not supported, and refused with the reason: two processors (the stop does +// not yet bring both to a common instruction boundary that a snapshot can +// record). In a jitv2 build, putting memory back bumps every page's generation, +// so code compiled from the memory before the load is not run. + +/// Why an Octane with this configuration cannot be saved, when it cannot. +fn snapshot_refusal_for(_impact: bool, _vpro: bool, virtio: bool, cpus: usize) -> Option { if virtio { return Some("snapshots of an Octane with virtio-serial are not supported yet: \ the device's queues have no saved state".into()); @@ -1008,6 +1000,8 @@ struct OctaneSnapshot { nvme_overlays: Option, /// The Fibre Channel loop's, when the snapshot has them. fibre_overlays: Option, + impact: Option<(String, Vec<(String, Vec)>)>, + vpro: Option<(String, Vec<(String, Vec)>)>, dir: std::path::PathBuf, } @@ -1138,12 +1132,44 @@ impl OctaneMachine { let io = |e: std::io::Error| e.to_string(); snap.write_state("cpu", &cpu.save_state(), sv).map_err(io)?; snap.write_state("octane", &self.save_board(), sv).map_err(io)?; - snap.write_bin("flash.bin", &self.flash.contents()).map_err(io)?; + let impact = self.gfx.as_ref().map(|gfx| gfx.snapshot_parts()).transpose()?; + if let Some((metadata, _)) = &impact { + snap.write_state("impact", &toml::Value::String(metadata.clone()), sv).map_err(io)?; + } + let vpro = self + .vpro + .as_ref() + .map(|vpro| vpro.snapshot_parts()) + .transpose()?; + if let Some((metadata, _)) = &vpro { + snap.write_state("vpro", &toml::Value::String(metadata.clone()), sv) + .map_err(io)?; + } + snap.write_bin("flash.bin", &self.flash.contents()) + .map_err(io)?; let store = ChunkStore::new(saves); let mut chunks = ChunksManifest::default(); for words in self.ram.bank_words() { chunks.bank_chunks[0].extend(put_words_as_chunks(&store, words).map_err(|e| format!("CAS memory put: {e}"))?); } + if let Some((_, parts)) = impact { + for (name, words) in parts { + let hashes = put_words_as_chunks(&store, &words) + .map_err(|e| format!("CAS IMPACT {name} put: {e}"))?; + chunks.mgras_chunks.push(MgrasChunk { name, words: words.len(), hashes }); + } + } + if let Some((_, parts)) = vpro { + for (name, words) in parts { + let hashes = put_words_as_chunks(&store, &words) + .map_err(|e| format!("CAS VPro {name} put: {e}"))?; + chunks.odyssey_chunks.push(OdysseyChunk { + name, + words: words.len(), + hashes, + }); + } + } snap.write_chunks_manifest(&chunks).map_err(io)?; for (i, ctl) in self.scsi.iter().enumerate() { let dir = snap.dir.join(format!("ctrl{i}")); @@ -1226,6 +1252,66 @@ impl OctaneMachine { } let nvme_overlays = snap.read_toml("cow_nvme.toml").ok().map(|t| crate::machine::parse_cow_table(&t)); let fibre_overlays = snap.read_toml("cow_fibre_channel.toml").ok().map(|t| crate::machine::parse_cow_table(&t)); + let impact = match (&self.gfx, chunks.mgras_chunks.is_empty()) { + (Some(_), false) if sv >= 8 => { + let metadata = state("impact")? + .as_str() + .ok_or("impact snapshot metadata is not a string")? + .to_string(); + let store = ChunkStore::new(saves); + let mut parts = Vec::with_capacity(chunks.mgras_chunks.len()); + for part in &chunks.mgras_chunks { + let words = get_chunks_as_words(&store, &part.hashes) + .map_err(|e| format!("CAS IMPACT {} get: {e}", part.name))?; + if words.len() != part.words { + return Err(format!( + "CAS IMPACT {} has {} words, expected {}", + part.name, + words.len(), + part.words + )); + } + parts.push((part.name.clone(), words)); + } + Some((metadata, parts)) + } + (Some(_), true) if sv >= 8 => return Err("snapshot has no state for the configured IMPACT board".into()), + (Some(_), _) => return Err("snapshot schema predates Octane IMPACT state".into()), + (None, false) => return Err("snapshot contains IMPACT state but this Octane has no IMPACT board".into()), + (None, true) => None, + }; + let vpro = match (&self.vpro, chunks.odyssey_chunks.is_empty()) { + (Some(_), false) if sv >= 9 => { + let metadata = state("vpro")? + .as_str() + .ok_or("VPro snapshot metadata is not a string")? + .to_string(); + let store = ChunkStore::new(saves); + let mut parts = Vec::with_capacity(chunks.odyssey_chunks.len()); + for part in &chunks.odyssey_chunks { + let words = get_chunks_as_words(&store, &part.hashes) + .map_err(|e| format!("CAS VPro {} get: {e}", part.name))?; + if words.len() != part.words { + return Err(format!( + "CAS VPro {} has {} words, expected {}", + part.name, + words.len(), + part.words + )); + } + parts.push((part.name.clone(), words)); + } + Some((metadata, parts)) + } + (Some(_), true) if sv >= 9 => { + return Err("snapshot has no state for the configured VPro board".into()) + } + (Some(_), _) => return Err("snapshot schema predates Octane VPro state".into()), + (None, false) => { + return Err("snapshot contains VPro state but this Octane has no VPro board".into()) + } + (None, true) => None, + }; Ok(OctaneSnapshot { cpu: state("cpu")?, board: state("octane")?, @@ -1234,6 +1320,8 @@ impl OctaneMachine { overlays, nvme_overlays, fibre_overlays, + impact, + vpro, dir: snap.dir, }) } @@ -1261,7 +1349,14 @@ impl OctaneMachine { f.bus().import_overlays(&s.dir.join("fibre_channel"), sets) .map_err(|e| format!("COW overlay import (Fibre Channel): {e}"))?; } - self.load_board(&s.board) + self.load_board(&s.board)?; + if let (Some(gfx), Some((metadata, parts))) = (&self.gfx, s.impact) { + gfx.restore_snapshot_parts(&metadata, &parts)?; + } + if let (Some(vpro), Some((metadata, parts))) = (&self.vpro, s.vpro) { + vpro.restore_snapshot_parts(&metadata, &parts)?; + } + Ok(()) } } @@ -2003,26 +2098,113 @@ mod snapshot_tests { #[test] fn each_refused_case_gives_its_reason() { assert_eq!(snapshot_refusal_for(false, false, false, 1), None); - assert!(snapshot_refusal_for(true, false, false, 1).unwrap().contains("IMPACT")); - assert!(snapshot_refusal_for(false, true, false, 1).unwrap().contains("VPro")); - assert!(snapshot_refusal_for(false, false, true, 1).unwrap().contains("virtio-serial")); - assert!(snapshot_refusal_for(false, false, false, 2).unwrap().contains("two-processor")); + assert_eq!(snapshot_refusal_for(true, false, false, 1), None); + assert_eq!(snapshot_refusal_for(false, true, false, 1), None); + assert!(snapshot_refusal_for(false, false, true, 1) + .unwrap() + .contains("virtio-serial")); + assert!(snapshot_refusal_for(false, false, false, 2) + .unwrap() + .contains("two-processor")); } - /// A machine applies the refusal to both save and load. + /// The Octane wrapper and its IMPACT pipeline return to the saved point. #[test] - fn an_impact_board_is_refused_with_the_reason() { + fn an_octane_impact_board_round_trips_through_a_machine_snapshot() { std::thread::Builder::new().stack_size(64 << 20).spawn(|| { let dir = scratch("impact"); let prom = empty_prom(&dir); let mut m = machine_with(&prom, &dir, |cfg| cfg.graphics.board = Some(crate::config::GraphicsBoard::SolidImpact)); - let e = m.save_snapshot_in(&dir.join("saves"), "x").unwrap_err(); - assert!(e.contains("IMPACT"), "{e}"); - let e = m.load_snapshot_in(&dir.join("saves"), "x").unwrap_err(); - assert!(e.contains("IMPACT"), "{e}"); + let gfx = m.gfx.as_ref().unwrap().clone(); + let board = gfx.mgras().clone(); + board.start_engines(); + let rss = |reg: u32, value: u32, exec: bool| { + let addr = crate::dev::mgras::MGRAS_SLOT_GFX_BASE as u64 + + 0x7C000 + 4 * reg as u64 + if exec { 0x1000 } else { 0 }; + loop { + let status = board.write32(addr, value); + if status != crate::traits::BUS_BUSY { + assert_eq!(status, crate::traits::BUS_OK); + break; + } + std::thread::yield_now(); + } + }; + rss(0x112, 0xCAC, false); + rss(0x115, 1023 << 16, false); + rss(0x16E, 0x31E, false); + rss(0x16D, 0x240, false); + rss(0x163, 0xFFF, false); + rss(0x161, 0x0C00_4504, false); + rss(0x110, 1 << 20, false); + rss(0x176, 0x12, false); + rss(0x045, 0x18, false); + rss(0x046, 25 << 16 | 35, false); + rss(0x047, 25 << 16 | 35, true); + assert_eq!(board.fb_pixel(25, 35), 0x12); + let base = crate::ip30::widget_small_base(crate::ip30_machine::WIDGET_GFX); + assert_eq!(gfx.write32(base + 0x24, 0xC1D2_8354), crate::traits::BUS_OK); + m.save_snapshot_in(&dir.join("saves"), "x").unwrap(); + + board.start_engines(); + rss(0x176, 0x56, false); + rss(0x045, 0x18, false); + rss(0x046, 25 << 16 | 35, false); + rss(0x047, 25 << 16 | 35, true); + assert_eq!(board.fb_pixel(25, 35), 0x56); + assert_eq!(gfx.write32(base + 0x24, 0), crate::traits::BUS_OK); + m.load_snapshot_in(&dir.join("saves"), "x").unwrap(); + assert_eq!(board.fb_pixel(25, 35), 0x12); + assert_eq!(gfx.read32(base + 0x24).data, 0xC1D2_8354); + m.stop(); }).unwrap().join().unwrap(); } + /// The Octane wrapper persists both named VPro parts and restores the + /// host-visible board state after its power-on reset. + #[test] + fn an_octane_vpro_board_round_trips_through_a_machine_snapshot() { + std::thread::Builder::new() + .stack_size(64 << 20) + .spawn(|| { + let dir = scratch("vpro"); + let prom = empty_prom(&dir); + let mut m = machine_with(&prom, &dir, |cfg| { + cfg.graphics.board = Some(crate::config::GraphicsBoard::V6) + }); + let vpro = m.vpro.as_ref().unwrap().clone(); + let sw_token = widget_small_base(WIDGET_GFX) + 0x107c; + vpro.start(); + assert_eq!(vpro.write32(sw_token, 0x51c2_93a4), crate::traits::BUS_OK); + assert_eq!(vpro.read32(sw_token).data, 0x51c2_93a4); + m.save_snapshot_in(&dir.join("saves"), "x").unwrap(); + + let snap = Snapshot::new(dir.join("saves/x")); + let chunks = snap.read_chunks_manifest().unwrap(); + let mut names: Vec<_> = chunks + .odyssey_chunks + .iter() + .map(|part| part.name.as_str()) + .collect(); + names.sort_unstable(); + assert_eq!(names, ["state", "vram"]); + assert!(chunks + .odyssey_chunks + .iter() + .all(|part| part.words != 0 && !part.hashes.is_empty())); + + vpro.start(); + assert_eq!(vpro.write32(sw_token, 0xa51c_2738), crate::traits::BUS_OK); + assert_eq!(vpro.read32(sw_token).data, 0xa51c_2738); + m.load_snapshot_in(&dir.join("saves"), "x").unwrap(); + assert_eq!(vpro.read32(sw_token).data, 0x51c2_93a4); + m.stop(); + }) + .unwrap() + .join() + .unwrap(); + } + /// A snapshot of another machine, or with a boot flash of another size, /// is refused before the machine is touched: it carries on as it was. /// (One machine for both: every Octane a test builds holds one of the diff --git a/src/machine.rs b/src/machine.rs index bcc916e9a..db629a71e 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -35,7 +35,7 @@ use crate::dev::hpc3::Hpc3; use crate::dev::ioc::{Ioc, GioSlot, GIO_SLOT_MAP, profile_idx}; use crate::monitor::Monitor; use crate::dev::ng1::rex3::Rex3; -use crate::snapshot::{Snapshot, Manifest, SCHEMA_VERSION, ChunksManifest, DiskRef, enabled_features}; +use crate::snapshot::{Snapshot, Manifest, SCHEMA_VERSION, ChunksManifest, DiskRef, Gr2Chunk, MgrasChunk, enabled_features}; use crate::chunk_store::{ChunkStore, get_chunks_as_words, put_words_as_chunks}; use crate::hptimer::TimerManager; use crate::workstation::{MachineRef, MachineSlot, SystemController, Workstation}; @@ -173,8 +173,10 @@ pub struct Machine { lifecycle: GuestState, } -#[cfg(all(test, feature = "wgpu"))] +#[cfg(feature = "wgpu")] type ResidentCheckpoint = (toml::Value, (Vec, Vec)); +type MgrasCheckpoint = (String, Vec<(String, Vec)>); +type Gr2Checkpoint = crate::dev::gr2::Gr2SnapshotParts; /// In-memory snapshot of the just-restored guest state. Populated at the end /// of `ci_restore`; consumed by `ci_rollback`. Trades ~270 MB of RSS for @@ -196,7 +198,7 @@ struct RollbackCheckpoint { /// Framebuffer contents (RGB, aux). `None` when running headless. framebuffers: Option<(Vec, Vec)>, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_head1_framebuffers: Option<(Vec, Vec)>, /// Parsed device save_state TOMLs. Holding `toml::Value` directly skips @@ -216,6 +218,8 @@ struct RollbackCheckpoint { hpc3: toml::Value, rex3: Option, rex3_head1: Option, + mgras: Option, + gr2: Option, } /// Parse a `cow.toml`/`cow1.toml`-shaped table (`{"scsi": [dirty @@ -274,7 +278,7 @@ pub(crate) fn parse_cow_table(cow_toml: &toml::Value) -> Vec<(usize, Vec)> pub(crate) struct LiveCheckpoint { bank_words: [Vec; 4], framebuffers: Option<(Vec, Vec)>, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_head1_framebuffers: Option<(Vec, Vec)>, cpu: toml::Value, mc: toml::Value, @@ -288,6 +292,8 @@ pub(crate) struct LiveCheckpoint { hpc3: toml::Value, rex3: Option, rex3_head1: Option, + mgras: Option, + gr2: Option, } /// The test device, the ultra64 dev board and the second Newport head all @@ -360,8 +366,7 @@ impl Machine { Self::new_with_testdev(cfg, None) } - /// `new`, but a `prom` file that cannot be loaded is an error for the caller to - /// report, not a panic. + /// `new`, but construction errors are returned to the caller instead of panicking. pub fn try_new(cfg: MachineConfig) -> Result { Self::register_host_services(); Self::try_new_with_testdev(cfg, None) @@ -387,23 +392,25 @@ impl Machine { cfg: MachineConfig, testdev_override: Option>, ) -> Self { - Self::try_new_with_testdev(cfg, testdev_override).unwrap_or_else(|e| panic!("{e}")) + Self::try_new_with_testdev(cfg, testdev_override) + .unwrap_or_else(|error| panic!("machine construction failed: {error}")) } - /// [`new_with_testdev`](Self::new_with_testdev), reporting a `prom` that cannot be loaded. + /// [`new_with_testdev`](Self::new_with_testdev), returning construction errors. pub fn try_new_with_testdev( cfg: MachineConfig, testdev_override: Option>, ) -> Result { let prom = Prom::for_profile(cfg.machine.profile, &cfg.prom)?; - Ok(Self::with_prom(cfg, testdev_override, prom)) + Self::try_with_prom(cfg, testdev_override, prom) } - fn with_prom( + fn try_with_prom( mut cfg: MachineConfig, testdev_override: Option>, prom: Prom, - ) -> Self { + ) -> Result { + cfg.validate_newport_raster_gate()?; // The rig takes any part: `MachineConfig::cpu_part` looks at this. cfg.test_device |= testdev_override.is_some(); // Capture config flags that are needed after the local `cfg` binding @@ -598,11 +605,59 @@ impl Machine { .collect(); let nvram_provenance = cfg.nvram.clone(); + #[cfg(feature = "wgpu")] + let resident_gpu = if newport_active + && cfg.graphics.raster == crate::config::RasterUnit::Wgpu + { + if std::env::var_os("IRIS_REX3_GPU").is_some() { + return Err( + "IRIS_REX3_GPU and production Newport graphics.raster = \"wgpu\" are mutually exclusive" + .into(), + ); + } + let options = crate::wgpu::GpuOptions { + backend: cfg.graphics.gpu_backend.clone(), + ..Default::default() + }; + let gpu = crate::wgpu::Gpu::new(&options) + .map_err(|error| format!("create Newport resident GPU: {error}"))?; + if !matches!(gpu.caps.backend, wgpu::Backend::Vulkan | wgpu::Backend::Dx12) { + return Err(format!( + "Newport resident GPU resolved graphics.gpu_backend = {:?} to unsupported {:?}; \ + use Vulkan or D3D12", + cfg.graphics.gpu_backend, gpu.caps.backend + )); + } + gpu.check().map_err(|error| format!("initialize Newport resident GPU: {error}"))?; + eprintln!("iris: Newport resident GPU enabled without CPU fallback: {}", gpu.caps); + Some(gpu) + } else { + None + }; + + #[cfg(feature = "wgpu")] + let resident_mode = match cfg.graphics.newport_wgpu_mode() { + crate::config::NewportWgpuMode::Direct => + crate::dev::ng1::rex3::ResidentDisplayMode::DirectWgpu, + crate::config::NewportWgpuMode::Readback => + crate::dev::ng1::rex3::ResidentDisplayMode::ReadbackCompatibility, + }; + // REX3 Graphics — Newport only; skipped in headless mode or when non-Newport board selected. let rex3: Option> = if cfg.headless || cfg.graphics_board() != crate::config::GraphicsBoard::Newport { None } else { - let r = Arc::new(Rex3::new(status.clone())); + let r = Rex3::new(status.clone()); + #[cfg(feature = "wgpu")] + let r = { + let mut r = r; + if let Some(gpu) = &resident_gpu { + r.activate_resident_gpu_for_runtime(Arc::clone(gpu), resident_mode) + .map_err(|error| format!("activate primary Newport resident GPU: {error}"))?; + } + r + }; + let r = Arc::new(r); r.set_fatal_sender(fatal_tx.clone()); let wiring = GIO_SLOT_MAP[profile_idx(guinness)][GioSlot::Gfx as usize]; r.set_vblank_irq(ioc.irq_line(wiring.retrace)); @@ -613,7 +668,17 @@ impl Machine { let rex3_head1: Option> = if cfg.headless || cfg.graphics.heads < 2 { None } else { - let r = Arc::new(Rex3::new(status.clone())); + let r = Rex3::new(status.clone()); + #[cfg(feature = "wgpu")] + let r = { + let mut r = r; + if let Some(gpu) = &resident_gpu { + r.activate_resident_gpu_for_runtime(Arc::clone(gpu), resident_mode) + .map_err(|error| format!("activate secondary Newport resident GPU: {error}"))?; + } + r + }; + let r = Arc::new(r); r.set_fatal_sender(fatal_tx.clone()); r.set_head_index(1); // An Indy baseboard older than revision 4 does not take a second @@ -1018,7 +1083,7 @@ impl Machine { crate::thread_affinity::init(perf); - Self { + Ok(Self { memory, cpu, board, mc, @@ -1043,7 +1108,7 @@ impl Machine { controller_slot: None, controller_registered: false, lifecycle: GuestState::Idle, - } + }) } fn apply_host_display_resolution(&self) { @@ -1063,7 +1128,7 @@ impl Machine { self.display_resolution }; let apply = |rex3: &Rex3| { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if rex3.resident_active() { rex3.apply_resident_display_resolution(mode, true) .unwrap_or_else(|error| panic!("REX3 {error}")); @@ -1528,14 +1593,14 @@ impl Machine { result } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn capture_resident_checkpoint(rex: Option<&Rex3>) -> Result, String> { rex.filter(|rex| rex.resident_active()) .map(|rex| rex.snapshot_resident_checkpoint().map_err(|error| error.to_string())) .transpose() } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn validate_resident_primary_checkpoint(&self, registers: bool, planes: bool) -> Result<(), String> { if self.board.rex3.as_ref().is_some_and(|rex| rex.resident_active()) && (!registers || !planes) { return Err("resident REX3 checkpoint requires registers and both planes".into()); @@ -1543,7 +1608,7 @@ impl Machine { Ok(()) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn validate_resident_head1_checkpoint(&self, registers: bool, planes: bool) -> Result<(), String> { if self.board.rex3_head1.as_ref().is_some_and(|rex| rex.resident_active()) && (!registers || !planes) { return Err("resident REX3 head 1 checkpoint requires registers and both planes".into()); @@ -1551,7 +1616,7 @@ impl Machine { Ok(()) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn restore_resident_head1_framebuffers( &self, has_registers: bool, @@ -1562,7 +1627,8 @@ impl Machine { return Err("resident REX3 head 1 checkpoint has no registers".into()); } let (rgb, aux) = planes.ok_or("resident REX3 head 1 checkpoint has no planes")?; - rex.restore_framebuffers_inmem(rgb, aux); + rex.try_restore_framebuffers_inmem(rgb, aux) + .map_err(|error| format!("restore resident REX3 head 1 framebuffers: {error}"))?; } Ok(()) } @@ -1571,9 +1637,9 @@ impl Machine { fn capture_rollback_checkpoint(&mut self, name: &str) -> Result { self.stop(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_primary = Self::capture_resident_checkpoint(self.board.rex3.as_deref())?; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_head1 = Self::capture_resident_checkpoint(self.board.rex3_head1.as_deref())?; let cpu = self.cpu.save_state(); let mc = self.mc.save_state(); @@ -1588,15 +1654,17 @@ impl Machine { let seeq = self.hpc3.seeq().save_state(); let hpc3 = self.hpc3.save_state(); let rex3 = self.board.rex3.as_ref().map(|r| { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if let Some((state, _)) = &resident_primary { return state.clone(); } r.save_state() }); let rex3_head1 = self.board.rex3_head1.as_ref().map(|r| { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if let Some((state, _)) = &resident_head1 { return state.clone(); } r.save_state() }); + let mgras = self.board.mgras.as_ref().map(|m| m.snapshot_parts()).transpose()?; + let gr2 = self.board.gr2.as_ref().map(|g| g.snapshot_parts()).transpose()?; let bank_words: [Vec; 4] = [ self.board.phys.snapshot_bank_inmem(0), @@ -1605,14 +1673,14 @@ impl Machine { self.board.phys.snapshot_bank_inmem(3), ]; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let framebuffers = match resident_primary { Some((_, planes)) => Some(planes), None => self.board.rex3.as_ref().map(|r| r.snapshot_framebuffers_inmem()), }; - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let framebuffers = self.board.rex3.as_ref().map(|r| r.snapshot_framebuffers_inmem()); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_head1_framebuffers = resident_head1.map(|(_, planes)| planes); // Re-read cow.toml (and cow1.toml, for controller 1) so rollback @@ -1633,9 +1701,9 @@ impl Machine { overlay_sets1, bank_words, framebuffers, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_head1_framebuffers, - cpu, mc, ioc, scc, pit, ps2, rtc, eeprom, scsi, scsi1, seeq, hpc3, rex3, rex3_head1, + cpu, mc, ioc, scc, pit, ps2, rtc, eeprom, scsi, scsi1, seeq, hpc3, rex3, rex3_head1, mgras, gr2, }) } @@ -1643,9 +1711,9 @@ impl Machine { /// the moment of capture. Skips disk IO and TOML string-parsing. fn apply_rollback_checkpoint(&mut self, cp: &RollbackCheckpoint) -> Result<(), String> { self.stop(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.validate_resident_primary_checkpoint(cp.rex3.is_some(), cp.framebuffers.is_some())?; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.validate_resident_head1_checkpoint(cp.rex3_head1.is_some(), cp.resident_head1_framebuffers.is_some())?; self.power_on_devices(); @@ -1674,10 +1742,23 @@ impl Machine { self.board.phys.restore_bank_inmem(i, words); } if let (Some(rex3), Some((rgb, aux))) = (&self.board.rex3, &cp.framebuffers) { - rex3.restore_framebuffers_inmem(rgb, aux); + rex3.try_restore_framebuffers_inmem(rgb, aux) + .map_err(|error| format!("restore REX3 framebuffers: {error}"))?; } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.restore_resident_head1_framebuffers(cp.rex3_head1.is_some(), cp.resident_head1_framebuffers.as_ref())?; + match (&self.board.mgras, &cp.mgras) { + (Some(mgras), Some((metadata, parts))) => mgras.restore_snapshot_parts(metadata, parts)?, + (Some(_), None) => return Err("rollback checkpoint has no state for the configured IMPACT board".into()), + (None, Some(_)) => return Err("rollback checkpoint contains IMPACT state but the machine has no IMPACT board".into()), + (None, None) => {} + } + match (&self.board.gr2, &cp.gr2) { + (Some(gr2), Some((metadata, parts))) => gr2.restore_snapshot_parts(metadata, parts)?, + (Some(_), None) => return Err("rollback checkpoint has no state for the configured GR2 board".into()), + (None, Some(_)) => return Err("rollback checkpoint contains GR2 state but the machine has no GR2 board".into()), + (None, None) => {} + } // Reflink the overlay back into place. saves//scsi*.overlay is // unchanged by guest writes (writes go to the live overlay), so this @@ -1709,10 +1790,10 @@ impl Machine { pub(crate) fn capture_live_checkpoint(&mut self) -> LiveCheckpoint { self.stop(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_primary = Self::capture_resident_checkpoint(self.board.rex3.as_deref()) .unwrap_or_else(|error| panic!("REX3 {error}")); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_head1 = Self::capture_resident_checkpoint(self.board.rex3_head1.as_deref()) .unwrap_or_else(|error| panic!("REX3 {error}")); let cpu = self.cpu.save_state(); @@ -1726,15 +1807,29 @@ impl Machine { let seeq = self.hpc3.seeq().save_state(); let hpc3 = self.hpc3.save_state(); let rex3 = self.board.rex3.as_ref().map(|r| { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if let Some((state, _)) = &resident_primary { return state.clone(); } r.save_state() }); let rex3_head1 = self.board.rex3_head1.as_ref().map(|r| { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if let Some((state, _)) = &resident_head1 { return state.clone(); } r.save_state() }); + let mgras = self + .board + .mgras + .as_ref() + .map(|m| m.snapshot_parts()) + .transpose() + .unwrap_or_else(|error| panic!("IMPACT {error}")); + let gr2 = self + .board + .gr2 + .as_ref() + .map(|g| g.snapshot_parts()) + .transpose() + .unwrap_or_else(|error| panic!("GR2 {error}")); let bank_words: [Vec; 4] = [ self.board.phys.snapshot_bank_inmem(0), @@ -1743,14 +1838,14 @@ impl Machine { self.board.phys.snapshot_bank_inmem(3), ]; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let framebuffers = match resident_primary { Some((_, planes)) => Some(planes), None => self.board.rex3.as_ref().map(|r| r.snapshot_framebuffers_inmem()), }; - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let framebuffers = self.board.rex3.as_ref().map(|r| r.snapshot_framebuffers_inmem()); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_head1_framebuffers = resident_head1.map(|(_, planes)| planes); // Deliberately NOT restart_peripherals()/cpu.start() here — see this @@ -1759,9 +1854,9 @@ impl Machine { LiveCheckpoint { bank_words, framebuffers, - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] resident_head1_framebuffers, - cpu, mc, ioc, scc, pit, ps2, rtc, eeprom, seeq, hpc3, rex3, rex3_head1, + cpu, mc, ioc, scc, pit, ps2, rtc, eeprom, seeq, hpc3, rex3, rex3_head1, mgras, gr2, } } @@ -1780,9 +1875,9 @@ impl Machine { /// stayed unresponsive after every `jitcheck` for no reason). pub(crate) fn restore_live_checkpoint(&mut self, cp: &LiveCheckpoint) -> Result<(), String> { self.stop(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.validate_resident_primary_checkpoint(cp.rex3.is_some(), cp.framebuffers.is_some())?; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.validate_resident_head1_checkpoint(cp.rex3_head1.is_some(), cp.resident_head1_framebuffers.is_some())?; self.cpu.load_state(&cp.cpu)?; @@ -1806,10 +1901,23 @@ impl Machine { self.board.phys.restore_bank_inmem(i, words); } if let (Some(rex3), Some((rgb, aux))) = (&self.board.rex3, &cp.framebuffers) { - rex3.restore_framebuffers_inmem(rgb, aux); + rex3.try_restore_framebuffers_inmem(rgb, aux) + .map_err(|error| format!("restore REX3 framebuffers: {error}"))?; } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.restore_resident_head1_framebuffers(cp.rex3_head1.is_some(), cp.resident_head1_framebuffers.as_ref())?; + match (&self.board.mgras, &cp.mgras) { + (Some(mgras), Some((metadata, parts))) => mgras.restore_snapshot_parts(metadata, parts)?, + (Some(_), None) => return Err("live checkpoint has no state for the configured IMPACT board".into()), + (None, Some(_)) => return Err("live checkpoint contains IMPACT state but the machine has no IMPACT board".into()), + (None, None) => {} + } + match (&self.board.gr2, &cp.gr2) { + (Some(gr2), Some((metadata, parts))) => gr2.restore_snapshot_parts(metadata, parts)?, + (Some(_), None) => return Err("live checkpoint has no state for the configured GR2 board".into()), + (None, Some(_)) => return Err("live checkpoint contains GR2 state but the machine has no GR2 board".into()), + (None, None) => {} + } // Peripherals back up; CPU deliberately left stopped — see this // function's own doc comment. @@ -1888,16 +1996,16 @@ impl Machine { pub fn save_snapshot(&mut self, name: &str) -> Result<(), String> { self.stop(); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let mut resident_primary = Self::capture_resident_checkpoint(self.board.rex3.as_deref())?; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_head1 = Self::capture_resident_checkpoint(self.board.rex3_head1.as_deref())?; let dir = std::path::PathBuf::from("saves").join(name); let snap = Snapshot::new(&dir); snap.ensure_dir().map_err(|e| e.to_string())?; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let incomplete = dir.join("resident_snapshot_incomplete"); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let remove_incomplete = { let resident = resident_primary.is_some() || resident_head1.is_some(); if resident { @@ -1944,23 +2052,23 @@ impl Machine { // included in the chunks manifest below for v3+; v2 wrote them as // standalone .bin files. if let Some(rex3) = &self.board.rex3 { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_state = resident_primary.as_ref().map(|(state, _)| state); - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let resident_state: Option<&toml::Value> = None; match resident_state { Some(state) => snap.write_state("rex3", state, sv), None => snap.write_state("rex3", &rex3.save_state(), sv), }.map_err(|e| e.to_string())?; if sv < 3 { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if let Some((_, (rgb, aux))) = &resident_primary { Rex3::write_captured_resident_framebuffers(&snap.dir, "rex3", rgb, aux) .map_err(|e| e.to_string())?; } else { rex3.save_framebuffers(&snap.dir).map_err(|e| e.to_string())?; } - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] rex3.save_framebuffers(&snap.dir).map_err(|e| e.to_string())?; } } @@ -1968,38 +2076,35 @@ impl Machine { snap.write_state("testdev", &td.save_state(), sv).map_err(|e| e.to_string())?; } if let Some(rex3) = &self.board.rex3_head1 { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let resident_state = resident_head1.as_ref().map(|(state, _)| state); - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let resident_state: Option<&toml::Value> = None; match resident_state { Some(state) => snap.write_state("rex3_head1", state, sv), None => snap.write_state("rex3_head1", &rex3.save_state(), sv), }.map_err(|e| e.to_string())?; // The v3 chunk manifest covers only the primary head. Keep the - // gated second head in the existing named-plane format. - #[cfg(all(test, feature = "wgpu"))] - let save_planes = sv < 3 || rex3.resident_active(); - #[cfg(not(all(test, feature = "wgpu")))] - let save_planes = sv < 3; - if save_planes { - let dir = &snap.dir; - #[cfg(all(test, feature = "wgpu"))] - if let Some((_, (rgb, aux))) = &resident_head1 { - Rex3::write_captured_resident_framebuffers(dir, "rex3_head1", rgb, aux) - .map_err(|e| e.to_string())?; - } else { - rex3.save_framebuffers_named(dir, "rex3_head1").map_err(|e| e.to_string())?; - } - #[cfg(not(all(test, feature = "wgpu")))] + // second head in the existing named-plane format in every raster + // mode so overwriting a snapshot cannot retain stale resident planes. + let dir = &snap.dir; + #[cfg(feature = "wgpu")] + if let Some((_, (rgb, aux))) = &resident_head1 { + Rex3::write_captured_resident_framebuffers(dir, "rex3_head1", rgb, aux) + .map_err(|e| e.to_string())?; + } else { rex3.save_framebuffers_named(dir, "rex3_head1").map_err(|e| e.to_string())?; } + #[cfg(not(feature = "wgpu"))] + rex3.save_framebuffers_named(dir, "rex3_head1").map_err(|e| e.to_string())?; } - if let Some(gr2) = &self.board.gr2 { - snap.write_state("gr2", &gr2.save_state(), sv).map_err(|e| e.to_string())?; + let gr2_snapshot = self.board.gr2.as_ref().map(|gr2| gr2.snapshot_parts()).transpose()?; + if let Some((metadata, _)) = &gr2_snapshot { + snap.write_state("gr2", &toml::Value::String(metadata.clone()), sv).map_err(|e| e.to_string())?; } - if let Some(mgras) = &self.board.mgras { - snap.write_state("mgras", &mgras.save_state(), sv).map_err(|e| e.to_string())?; + let mgras_snapshot = self.board.mgras.as_ref().map(|mgras| mgras.snapshot_parts()).transpose()?; + if let Some((metadata, _)) = &mgras_snapshot { + snap.write_state("mgras", &toml::Value::String(metadata.clone()), sv).map_err(|e| e.to_string())?; } // Bulk memory: v3+ goes to the content-addressable chunk store @@ -2015,9 +2120,9 @@ impl Machine { .map_err(|e| format!("CAS bank{} put: {}", i, e))?; } if let Some(rex3) = &self.board.rex3 { - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] let planes = resident_primary.take().map(|(_, planes)| planes); - #[cfg(not(all(test, feature = "wgpu")))] + #[cfg(not(feature = "wgpu"))] let planes: Option<(Vec, Vec)> = None; let (rgb, aux) = planes.unwrap_or_else(|| rex3.snapshot_framebuffers_inmem()); let rgb_chunks = put_words_as_chunks(&store, &rgb) @@ -2026,6 +2131,20 @@ impl Machine { .map_err(|e| format!("CAS rex3 aux put: {}", e))?; chunks.framebuffer_chunks = Some((rgb_chunks, aux_chunks)); } + if let Some((_, parts)) = mgras_snapshot { + for (name, words) in parts { + let hashes = put_words_as_chunks(&store, &words) + .map_err(|e| format!("CAS IMPACT {name} put: {e}"))?; + chunks.mgras_chunks.push(MgrasChunk { name, words: words.len(), hashes }); + } + } + if let Some((_, parts)) = gr2_snapshot { + for (name, words) in parts { + let hashes = put_words_as_chunks(&store, &words) + .map_err(|e| format!("CAS GR2 {name} put: {e}"))?; + chunks.gr2_chunks.push(Gr2Chunk { name, words: words.len(), hashes }); + } + } snap.write_chunks_manifest(&chunks).map_err(|e| e.to_string())?; } else { for i in 0..4 { @@ -2051,7 +2170,7 @@ impl Machine { .map_err(|e| e.to_string())?; } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if remove_incomplete { for rex3 in [self.board.rex3.as_ref(), self.board.rex3_head1.as_ref()].into_iter().flatten() { if rex3.resident_active() { @@ -2106,7 +2225,7 @@ impl Machine { self.stop(); let dir = std::path::PathBuf::from("saves").join(name); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if dir.join("resident_snapshot_incomplete").try_exists().map_err(|e| e.to_string())? { return Err("resident machine snapshot is incomplete".into()); } @@ -2278,7 +2397,7 @@ impl Machine { } if let Some(rex3) = &self.board.rex3_head1 { let head1_state = snap.read_state("rex3_head1", schema_version); - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] if rex3.resident_active() { if let Err(error) = &head1_state { return Err(format!("resident REX3 head 1 state: {error}")); @@ -2286,25 +2405,38 @@ impl Machine { } if let Ok(rex3_v) = head1_state { rex3.load_state(&rex3_v)?; - #[cfg(all(test, feature = "wgpu"))] - let load_planes = schema_version < 3 || rex3.resident_active(); - #[cfg(not(all(test, feature = "wgpu")))] - let load_planes = schema_version < 3; + let rgb_path = snap.dir.join("rex3_head1_rgb.bin"); + let aux_path = snap.dir.join("rex3_head1_aux.bin"); + let named_planes_present = rgb_path.try_exists().map_err(|e| e.to_string())? + || aux_path.try_exists().map_err(|e| e.to_string())?; + #[cfg(feature = "wgpu")] + let load_planes = schema_version < 3 || rex3.resident_active() || named_planes_present; + #[cfg(not(feature = "wgpu"))] + let load_planes = schema_version < 3 || named_planes_present; if load_planes { rex3.load_framebuffers_named(&snap.dir, "rex3_head1").map_err(|e| e.to_string())?; } } } - if let Some(gr2) = &self.board.gr2 { - if let Ok(gr2_v) = snap.read_state("gr2", schema_version) { - gr2.load_state(&gr2_v)?; - } - } - if let Some(mgras) = &self.board.mgras { - if let Ok(mgras_v) = snap.read_state("mgras", schema_version) { - mgras.load_state(&mgras_v)?; + let gr2_metadata = if schema_version >= 10 && self.board.gr2.is_some() { + let value = snap.read_state("gr2", schema_version).map_err(|e| e.to_string())?; + Some(value.as_str().ok_or("GR2 snapshot metadata is not a string")?.to_string()) + } else { + if schema_version < 10 { + if let Some(gr2) = &self.board.gr2 { + if let Ok(gr2_v) = snap.read_state("gr2", schema_version) { + gr2.load_state(&gr2_v)?; + } + } } - } + None + }; + let mgras_metadata = if schema_version >= 8 && self.board.mgras.is_some() { + let value = snap.read_state("mgras", schema_version).map_err(|e| e.to_string())?; + Some(value.as_str().ok_or("mgras snapshot metadata is not a string")?.to_string()) + } else { + None + }; // Bulk memory: v3+ comes from the content-addressable chunk store // shared across snapshots; v2 reads raw bank{N}.bin files. @@ -2312,7 +2444,7 @@ impl Machine { let store = ChunkStore::new("saves"); let chunks = snap.read_chunks_manifest() .map_err(|e| format!("read chunks.bin: {}", e))?; - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] self.validate_resident_primary_checkpoint(true, chunks.framebuffer_chunks.is_some())?; for (i, hashes) in chunks.bank_chunks.iter().enumerate() { if hashes.is_empty() { continue; } @@ -2325,7 +2457,57 @@ impl Machine { .map_err(|e| format!("CAS rex3 rgb get: {}", e))?; let aux = get_chunks_as_words(&store, aux_h) .map_err(|e| format!("CAS rex3 aux get: {}", e))?; - rex3.restore_framebuffers_inmem(&rgb, &aux); + rex3.try_restore_framebuffers_inmem(&rgb, &aux) + .map_err(|error| format!("restore REX3 framebuffer chunks: {error}"))?; + } + match (&self.board.mgras, mgras_metadata.as_deref(), chunks.mgras_chunks.is_empty()) { + (Some(mgras), Some(metadata), false) => { + let mut parts = Vec::with_capacity(chunks.mgras_chunks.len()); + for part in &chunks.mgras_chunks { + let words = get_chunks_as_words(&store, &part.hashes) + .map_err(|e| format!("CAS IMPACT {} get: {e}", part.name))?; + if words.len() != part.words { + return Err(format!( + "CAS IMPACT {} has {} words, expected {}", + part.name, + words.len(), + part.words + )); + } + parts.push((part.name.clone(), words)); + } + mgras.restore_snapshot_parts(metadata, &parts)?; + } + (Some(_), Some(_), true) => return Err("snapshot has IMPACT metadata but no IMPACT chunks".into()), + (Some(_), None, false) => return Err("snapshot has IMPACT chunks but no IMPACT metadata".into()), + (None, _, false) => return Err("snapshot contains IMPACT state but this machine has no IMPACT board".into()), + _ => {} + } + if schema_version >= 10 { + match (&self.board.gr2, gr2_metadata.as_deref(), chunks.gr2_chunks.is_empty()) { + (Some(gr2), Some(metadata), false) => { + let mut parts = Vec::with_capacity(chunks.gr2_chunks.len()); + for part in &chunks.gr2_chunks { + let words = get_chunks_as_words(&store, &part.hashes) + .map_err(|e| format!("CAS GR2 {} get: {e}", part.name))?; + if words.len() != part.words { + return Err(format!( + "CAS GR2 {} has {} words, expected {}", + part.name, + words.len(), + part.words + )); + } + parts.push((part.name.clone(), words)); + } + gr2.restore_snapshot_parts(metadata, &parts)?; + } + (Some(_), Some(_), true) => return Err("snapshot has GR2 metadata but no GR2 chunks".into()), + (Some(_), None, false) => return Err("snapshot has GR2 chunks but no GR2 metadata".into()), + (Some(_), None, true) => return Err("snapshot has no state for the configured GR2 board".into()), + (None, _, false) => return Err("snapshot contains GR2 state but this machine has no GR2 board".into()), + _ => {} + } } } else { for i in 0..4 { @@ -2716,6 +2898,117 @@ mod controller_lifetime_tests { } } +#[cfg(test)] +mod construction_error_tests { + use super::*; + use crate::display_stop_tests::big_stack; + use crate::test_temp::TempPath; + + #[test] + fn checked_constructors_return_explicit_prom_errors() { + let path = TempPath::new("machine-prom-missing"); + let expected = path.display().to_string(); + let mut cfg = MachineConfig::default(); + cfg.prom = expected.clone(); + let with_testdev = cfg.clone(); + + let error = big_stack(move || Machine::try_new(cfg).err().expect("missing PROM")); + assert!(error.contains(&expected), "{error}"); + let error = big_stack(move || { + Machine::try_new_with_testdev(with_testdev, None).err().expect("missing PROM") + }); + assert!(error.contains(&expected), "{error}"); + } + + #[test] + fn checked_constructor_returns_newport_configuration_errors() { + let prom = TempPath::file("machine-prom", &[0, 0, 0, 0]); + let mut cfg = MachineConfig::default(); + cfg.prom = prom.display().to_string(); + cfg.graphics.newport_mode = Some(crate::config::NewportWgpuMode::Readback); + + let error = big_stack(move || Machine::try_new(cfg).err().expect("invalid Newport mode")); + assert!(error.contains("applies only"), "{error}"); + } +} + +#[cfg(all(test, feature = "wgpu"))] +mod production_newport_wgpu_tests { + use super::*; + use crate::config::NewportWgpuMode; + use crate::gfx_display::GfxDisplay; + use crate::test_temp::{nvram_in, TempPath}; + + fn production_config(nv_dir: &std::path::Path) -> MachineConfig { + let mut cfg = MachineConfig::default(); + nvram_in(&mut cfg, nv_dir); + cfg.scsi.clear(); + cfg.no_audio = true; + cfg.ci = true; + cfg.monitor_port = Some(0); + cfg.graphics.raster = crate::config::RasterUnit::Wgpu; + cfg.graphics.gpu_backend = std::env::var("REX3_RESIDENT_BACKEND") + .unwrap_or_else(|_| "vulkan".into()); + cfg + } + + #[test] + #[ignore = "requires a validated Vulkan or D3D12 adapter"] + fn production_dual_head_wgpu_shares_device_and_restarts_idempotently() { + let nv = TempPath::dir("newport-production-nv"); + let nv_dir = nv.to_path_buf(); + std::thread::Builder::new() + .stack_size(64 * 1024 * 1024) + .spawn(move || { + let mut cfg = production_config(&nv_dir); + cfg.graphics.heads = 2; + + let mut machine = Machine::try_new(cfg).expect("construct production resident machine"); + let primary = machine.get_rex3().expect("primary Newport"); + let secondary = machine.get_rex3_head1().expect("secondary Newport"); + let primary_gpu = GfxDisplay::gpu(&*primary).expect("primary resident GPU"); + let secondary_gpu = GfxDisplay::gpu(&*secondary).expect("secondary resident GPU"); + assert!(Arc::ptr_eq(&primary_gpu, &secondary_gpu)); + assert!(GfxDisplay::requires_same_device_wgpu_presenter(&*primary)); + assert!(GfxDisplay::requires_same_device_wgpu_presenter(&*secondary)); + + machine.start(); + machine.restart_peripherals(); + for rex in [&primary, &secondary] { + assert!(rex.processor_thread.lock().is_some()); + assert!(rex.refresh_thread.lock().is_some()); + } + machine.stop(); + }) + .expect("spawn production Newport test") + .join() + .unwrap_or_else(|panic| std::panic::resume_unwind(panic)); + } + + #[test] + #[ignore = "requires a validated Vulkan or D3D12 adapter"] + fn production_readback_mode_raises_frontend_capture_timeout() { + let nv = TempPath::dir("newport-readback-nv"); + let nv_dir = nv.to_path_buf(); + std::thread::Builder::new() + .stack_size(64 * 1024 * 1024) + .spawn(move || { + let mut cfg = production_config(&nv_dir); + cfg.graphics.newport_mode = Some(NewportWgpuMode::Readback); + let machine = Machine::try_new(cfg).expect("construct production resident machine"); + let rex = machine.get_rex3().expect("primary Newport"); + assert_eq!( + GfxDisplay::minimum_frame_request_timeout(&*rex), + std::time::Duration::from_secs(2) + ); + assert!(!GfxDisplay::requires_same_device_wgpu_presenter(&*rex)); + }) + .expect("spawn production Newport test") + .join() + .unwrap_or_else(|panic| std::panic::resume_unwind(panic)); + } +} + #[cfg(test)] mod stop_order_tests { use super::*; @@ -2746,6 +3039,156 @@ mod stop_order_tests { } } +#[cfg(test)] +mod gr2_checkpoint_tests { + use super::*; + use crate::config::{GraphicsBoard, MachineProfile}; + use crate::dev::gr2::re3::{ + FB_W, IR_FLAT, REG_FUNC, REG_IR, REG_NUMPIX, REG_PIXMASK, REG_R, REG_XMAX, REG_YMAX, + REG_YX, ROP_COPY, + }; + use crate::dev::gr2::{Gr2, GR2_BASE}; + use crate::traits::{BusDevice, Resettable, BUS_BUSY}; + use std::path::Path; + + fn config(dir: &Path, board: GraphicsBoard) -> MachineConfig { + let mut cfg = crate::display_stop_tests::config(dir, MachineProfile::Indigo2Ip22, board); + cfg.headless = true; + cfg + } + + fn re3_reg(reg: usize) -> u32 { + if reg < 0x20 { 0x6c200 + reg as u32 * 4 } else { 0x6c280 + (reg as u32 - 0x20) * 4 } + } + + fn write(g: &Gr2, reg: usize, value: u32) { + while g.write32(GR2_BASE + re3_reg(reg) as u64, value) == BUS_BUSY { + std::hint::spin_loop(); + } + } + + fn draw_test_pixel(g: &Gr2, value: u32) { + g.start(); + for (reg, data) in [ + (REG_FUNC, ROP_COPY), + (REG_PIXMASK, 0x00ff_ffff), + (REG_XMAX, 1279), + (REG_YMAX, 1023), + (REG_R, value << 11), + (REG_YX, (20 << 12) | 30), + (REG_NUMPIX, 1), + (REG_IR, IR_FLAT), + ] { + write(g, reg, data); + } + g.wait_idle(); + assert_eq!(g.vram()[20 * FB_W + 30], value); + g.stop(); + } + + #[test] + fn gr2_live_and_rollback_checkpoints_restore_complete_state() { + crate::display_stop_tests::big_stack(|| { + let dir = crate::display_stop_tests::scratch("gr2-memory-checkpoints"); + let mut machine = Box::new(Machine::new(config(&dir, GraphicsBoard::Xz))); + let gr2 = machine.get_gr2().unwrap(); + draw_test_pixel(&gr2, 0x31); + + let live = machine.capture_live_checkpoint(); + gr2.power_on(); + assert_eq!(gr2.vram()[20 * FB_W + 30], 0); + machine.restore_live_checkpoint(&live).unwrap(); + assert_eq!(gr2.vram()[20 * FB_W + 30], 0x31); + machine.stop(); + + draw_test_pixel(&gr2, 0x52); + let rollback = machine.capture_rollback_checkpoint("gr2-memory-checkpoint").unwrap(); + machine.stop(); + gr2.power_on(); + assert_eq!(gr2.vram()[20 * FB_W + 30], 0); + machine.apply_rollback_checkpoint(&rollback).unwrap(); + assert_eq!(gr2.vram()[20 * FB_W + 30], 0x52); + machine.stop(); + + let mut missing = machine.capture_live_checkpoint(); + missing.gr2 = None; + assert!(machine + .restore_live_checkpoint(&missing) + .unwrap_err() + .contains("no state for the configured GR2 board")); + machine.stop(); + }); + } + + #[test] + fn gr2_disk_snapshot_round_trip_and_board_mismatches() { + const CHILD: &str = "IRIS_GR2_M15_DISK_CHILD"; + if std::env::var_os(CHILD).is_none() { + struct Remove(std::path::PathBuf); + impl Drop for Remove { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + + let path = std::env::current_dir() + .unwrap() + .join("target") + .join(format!("gr2-m15-disk-{}", std::process::id())); + std::fs::create_dir(&path).unwrap(); + let _cleanup = Remove(path.clone()); + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .current_dir(&path) + .env(CHILD, &path) + .args([ + "--exact", + "machine::gr2_checkpoint_tests::gr2_disk_snapshot_round_trip_and_board_mismatches", + "--nocapture", + "--test-threads=1", + ]) + .output() + .unwrap(); + assert!( + output.status.success(), + "GR2 disk snapshot child failed:\n{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + return; + } + + crate::display_stop_tests::big_stack(|| { + let path = std::path::PathBuf::from(std::env::var_os(CHILD).unwrap()); + assert_eq!(std::env::current_dir().unwrap(), path); + + let mut xz = Box::new(Machine::new(config(&path, GraphicsBoard::Xz))); + let gr2 = xz.get_gr2().unwrap(); + draw_test_pixel(&gr2, 0x73); + xz.save_snapshot("gr2-m15").unwrap(); + xz.stop(); + gr2.power_on(); + assert_eq!(gr2.vram()[20 * FB_W + 30], 0); + xz.load_snapshot_paused("gr2-m15").unwrap(); + assert_eq!(gr2.vram()[20 * FB_W + 30], 0x73); + xz.stop(); + + let mut extreme = Box::new(Machine::new(config(&path, GraphicsBoard::Extreme))); + assert!(extreme + .load_snapshot_paused("gr2-m15") + .unwrap_err() + .contains("running board")); + extreme.stop(); + + let mut newport = Box::new(Machine::new(config(&path, GraphicsBoard::Newport))); + assert!(newport + .load_snapshot_paused("gr2-m15") + .unwrap_err() + .contains("machine has no GR2 board")); + newport.stop(); + }); + } +} + /// virtio-serial on the Indy and Indigo2: the device answers in its GIO /// expansion slot, through the real physical map. #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index 7f597d470..b56e505cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -215,9 +215,24 @@ fn main() { #[cfg(feature = "wgpu")] iris::wgpu::set_display(event_loop.owned_display_handle()); let display = machine.get_display().expect("a graphics board must be present in non-headless mode"); - let mut ui = Ui::new(machine.get_ps2(), display, machine.disc_changer(), machine.get_timer_manager(), &event_loop, scale, scroll_pixels_per_line, lock_aspect_ratio, ui_renderer); + let mut ui = match Ui::new(machine.get_ps2(), display, machine.disc_changer(), machine.get_timer_manager(), &event_loop, scale, scroll_pixels_per_line, lock_aspect_ratio, ui_renderer) { + Ok(ui) => ui, + Err(error) => { + eprintln!("iris: cannot initialize the graphics window: {error}"); + iris::workstation::exit_process("graphics initialization failure", 1, || { + iris::workstation::stop_and_flush(&mut *machine, "graphics initialization failure") + }); + return; + } + }; if let Some(head1) = machine.get_display_head1() { - ui.add_head(head1, &event_loop); + if let Err(error) = ui.add_head(head1, &event_loop) { + eprintln!("iris: cannot initialize the secondary graphics window: {error}"); + iris::workstation::exit_process("secondary graphics initialization failure", 1, || { + iris::workstation::stop_and_flush(&mut *machine, "secondary graphics initialization failure") + }); + return; + } } ui.run(event_loop); } @@ -271,4 +286,3 @@ fn start_perfetto((path, cats, clock): (Option, Option, Option, Vec)>, /// The KONA pipe's pixel store and cursor memory; empty when the machine has none. pub kona_chunks: Vec, + /// Named IMPACT architectural state; empty when the machine has no IMPACT board. + pub mgras_chunks: Vec, + /// Named VPro architectural state and VRAM; empty when the machine has no VPro board. + pub odyssey_chunks: Vec, + /// Named GR2 architectural state, VRAM, and Z-buffer; empty without GR2. + pub gr2_chunks: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct MgrasChunk { + pub name: String, + pub words: usize, + pub hashes: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct OdysseyChunk { + pub name: String, + pub words: usize, + pub hashes: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Gr2Chunk { + pub name: String, + pub words: usize, + pub hashes: Vec, +} + +/// `chunks.bin` as schema 9 wrote it: `ChunksManifest` before `gr2_chunks`. +#[derive(Serialize, Deserialize)] +struct ChunksManifestV9 { + bank_chunks: [Vec; 4], + framebuffer_chunks: Option<(Vec, Vec)>, + kona_chunks: Vec, + mgras_chunks: Vec, + odyssey_chunks: Vec, +} + +/// `chunks.bin` as schema 8 wrote it: `ChunksManifest` before `odyssey_chunks`. +#[derive(Serialize, Deserialize)] +struct ChunksManifestV8 { + bank_chunks: [Vec; 4], + framebuffer_chunks: Option<(Vec, Vec)>, + kona_chunks: Vec, + mgras_chunks: Vec, +} + +/// `chunks.bin` as schema 7 wrote it: `ChunksManifest` before `mgras_chunks`. +#[derive(Serialize, Deserialize)] +struct ChunksManifestV7 { + bank_chunks: [Vec; 4], + framebuffer_chunks: Option<(Vec, Vec)>, + kona_chunks: Vec, } /// `chunks.bin` as schema 6 wrote it: `ChunksManifest` before `kona_chunks`. @@ -501,17 +563,52 @@ struct ChunksManifestV6 { } impl ChunksManifest { - /// Decode a `chunks.bin` of schema 6 or 7. A v6 file is the v7 layout cut - /// short before `kona_chunks`; postcard reports that as a short read, so - /// it is tried as v6, which must use every byte. Whatever else fails to - /// parse is reported as a v7 failure. + /// Decode a `chunks.bin` of schema 6 through 10. Older layouts must consume + /// every byte so a corrupt current manifest cannot be accepted as one. pub fn from_bytes(bytes: &[u8]) -> Result { let current = match postcard::from_bytes::(bytes) { Ok(m) => return Ok(m), Err(e) => e, }; + if let Ok((old, [])) = postcard::take_from_bytes::(bytes) { + return Ok(ChunksManifest { + bank_chunks: old.bank_chunks, + framebuffer_chunks: old.framebuffer_chunks, + kona_chunks: old.kona_chunks, + mgras_chunks: old.mgras_chunks, + odyssey_chunks: old.odyssey_chunks, + gr2_chunks: Vec::new(), + }); + } + if let Ok((old, [])) = postcard::take_from_bytes::(bytes) { + return Ok(ChunksManifest { + bank_chunks: old.bank_chunks, + framebuffer_chunks: old.framebuffer_chunks, + kona_chunks: old.kona_chunks, + mgras_chunks: old.mgras_chunks, + odyssey_chunks: Vec::new(), + gr2_chunks: Vec::new(), + }); + } + if let Ok((old, [])) = postcard::take_from_bytes::(bytes) { + return Ok(ChunksManifest { + bank_chunks: old.bank_chunks, + framebuffer_chunks: old.framebuffer_chunks, + kona_chunks: old.kona_chunks, + mgras_chunks: Vec::new(), + odyssey_chunks: Vec::new(), + gr2_chunks: Vec::new(), + }); + } match postcard::take_from_bytes::(bytes) { - Ok((old, [])) => Ok(ChunksManifest { bank_chunks: old.bank_chunks, framebuffer_chunks: old.framebuffer_chunks, kona_chunks: Vec::new() }), + Ok((old, [])) => Ok(ChunksManifest { + bank_chunks: old.bank_chunks, + framebuffer_chunks: old.framebuffer_chunks, + kona_chunks: Vec::new(), + mgras_chunks: Vec::new(), + odyssey_chunks: Vec::new(), + gr2_chunks: Vec::new(), + }), _ => Err(current), } } @@ -519,13 +616,24 @@ impl ChunksManifest { /// Iterate every chunk hash referenced by this manifest. Used by `gc` /// to build the live set across all kept snapshots. pub fn referenced_hashes(&self) -> impl Iterator { - self.bank_chunks.iter().flatten().chain( - self.framebuffer_chunks - .iter() - .flat_map(|(rgb, aux)| rgb.iter().chain(aux.iter())), - ) - // A zero chunk's place holds the zero hash, which the store never holds. - .chain(self.kona_chunks.iter().flat_map(|c| c.hashes.iter()).filter(|h| **h != [0u8; 32])) + self.bank_chunks + .iter() + .flatten() + .chain( + self.framebuffer_chunks + .iter() + .flat_map(|(rgb, aux)| rgb.iter().chain(aux.iter())), + ) + // A zero chunk's place holds the zero hash, which the store never holds. + .chain( + self.kona_chunks + .iter() + .flat_map(|c| c.hashes.iter()) + .filter(|h| **h != [0u8; 32]), + ) + .chain(self.mgras_chunks.iter().flat_map(|c| c.hashes.iter())) + .chain(self.odyssey_chunks.iter().flat_map(|c| c.hashes.iter())) + .chain(self.gr2_chunks.iter().flat_map(|c| c.hashes.iter())) } } @@ -872,23 +980,116 @@ mod tests { assert!(chunks.bank_chunks[1..].iter().all(|b| b.is_empty())); assert_eq!(chunks.framebuffer_chunks, framebuffer.then(|| (vec![[0xcd; 32]], vec![]))); assert!(chunks.kona_chunks.is_empty()); + assert!(chunks.mgras_chunks.is_empty()); + assert!(chunks.odyssey_chunks.is_empty()); + assert!(chunks.gr2_chunks.is_empty()); assert_eq!(chunks.referenced_hashes().count(), 1 + framebuffer as usize); let _ = fs::remove_dir_all(&dir); } } - /// The current layout still round-trips, and a file that is neither layout - /// is refused: a v7 file cut short, or a v6 file with something after it. + #[test] + fn a_schema_7_chunk_manifest_loads_with_no_impact_chunks() { + let old = ChunksManifestV7 { + bank_chunks: std::array::from_fn(|i| if i == 1 { vec![[7; 32]] } else { Vec::new() }), + framebuffer_chunks: None, + kona_chunks: vec![crate::dev::kona::KonaChunk { name: "state".into(), hashes: vec![[8; 32]] }], + }; + let bytes = postcard::to_allocvec(&old).unwrap(); + let back = ChunksManifest::from_bytes(&bytes).unwrap(); + assert_eq!(back.bank_chunks[1], vec![[7; 32]]); + assert_eq!(back.kona_chunks, old.kona_chunks); + assert!(back.mgras_chunks.is_empty()); + assert!(back.odyssey_chunks.is_empty()); + assert!(back.gr2_chunks.is_empty()); + } + + #[test] + fn a_schema_8_chunk_manifest_loads_with_no_vpro_chunks() { + let old = ChunksManifestV8 { + bank_chunks: std::array::from_fn(|i| if i == 2 { vec![[7; 32]] } else { Vec::new() }), + framebuffer_chunks: None, + kona_chunks: vec![crate::dev::kona::KonaChunk { + name: "state".into(), + hashes: vec![[8; 32]], + }], + mgras_chunks: vec![MgrasChunk { + name: "front".into(), + words: 19, + hashes: vec![[9; 32]], + }], + }; + let bytes = postcard::to_allocvec(&old).unwrap(); + let back = ChunksManifest::from_bytes(&bytes).unwrap(); + assert_eq!(back.bank_chunks[2], old.bank_chunks[2]); + assert_eq!(back.kona_chunks, old.kona_chunks); + assert_eq!(back.mgras_chunks, old.mgras_chunks); + assert!(back.odyssey_chunks.is_empty()); + assert!(back.gr2_chunks.is_empty()); + } + + #[test] + fn a_schema_9_chunk_manifest_loads_with_no_gr2_chunks() { + let old = ChunksManifestV9 { + bank_chunks: std::array::from_fn(|i| if i == 3 { vec![[7; 32]] } else { Vec::new() }), + framebuffer_chunks: None, + kona_chunks: vec![], + mgras_chunks: vec![MgrasChunk { + name: "front".into(), + words: 19, + hashes: vec![[9; 32]], + }], + odyssey_chunks: vec![OdysseyChunk { + name: "vram".into(), + words: 23, + hashes: vec![[10; 32]], + }], + }; + let bytes = postcard::to_allocvec(&old).unwrap(); + let back = ChunksManifest::from_bytes(&bytes).unwrap(); + assert_eq!(back.bank_chunks[3], old.bank_chunks[3]); + assert_eq!(back.mgras_chunks, old.mgras_chunks); + assert_eq!(back.odyssey_chunks, old.odyssey_chunks); + assert!(back.gr2_chunks.is_empty()); + } + + /// The current layout still round-trips, and a file that is neither a + /// complete current layout nor an exact older layout is refused. #[test] fn a_chunks_manifest_of_the_current_layout_round_trips_and_garbage_is_refused() { let mut m = ChunksManifest::default(); m.bank_chunks[2].push([7; 32]); - m.kona_chunks.push(crate::dev::kona::KonaChunk { name: "state-9".into(), hashes: vec![[9; 32]] }); + m.kona_chunks.push(crate::dev::kona::KonaChunk { + name: "state-9".into(), + hashes: vec![[9; 32]], + }); + m.mgras_chunks.push(MgrasChunk { + name: "front".into(), + words: 17, + hashes: vec![[10; 32]], + }); + m.odyssey_chunks.push(OdysseyChunk { + name: "vram".into(), + words: 23, + hashes: vec![[11; 32]], + }); + m.gr2_chunks.push(Gr2Chunk { + name: "zbuf".into(), + words: 29, + hashes: vec![[12; 32]], + }); let bytes = postcard::to_allocvec(&m).unwrap(); let back = ChunksManifest::from_bytes(&bytes).unwrap(); assert_eq!(back.bank_chunks[2], m.bank_chunks[2]); assert_eq!(back.kona_chunks, m.kona_chunks); - assert!(ChunksManifest::from_bytes(&bytes[..bytes.len() - 1]).is_err(), "cut short"); + assert_eq!(back.mgras_chunks, m.mgras_chunks); + assert_eq!(back.odyssey_chunks, m.odyssey_chunks); + assert_eq!(back.gr2_chunks, m.gr2_chunks); + assert!(back.referenced_hashes().any(|hash| *hash == [12; 32])); + assert!( + ChunksManifest::from_bytes(&bytes[..bytes.len() - 1]).is_err(), + "cut short" + ); let mut padded = v6_chunks_bin(false); padded.extend([5, 5]); assert!(ChunksManifest::from_bytes(&padded).is_err(), "trailing bytes after a v6 layout"); diff --git a/src/ui.rs b/src/ui.rs index d1f6dc69b..2eebaac76 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -744,7 +744,7 @@ impl Renderer for GlRenderer { format!("compositor={} shader=integer+fallback", comp) } - #[cfg(all(test, feature = "wgpu"))] + #[cfg(feature = "wgpu")] fn takes_resident_readback_frames(&self) -> bool { true } // Called from the monitor/CI socket thread — NOT the context-owning refresh @@ -901,7 +901,6 @@ impl Renderer for WgpuRenderer { self.board_device } - #[cfg(test)] fn takes_resident_readback_frames(&self) -> bool { true } fn present_gpu_frame( @@ -928,12 +927,10 @@ impl Renderer for WgpuRenderer { self.report(r); } - #[cfg(test)] fn resident_gpu_device(&self) -> Option<&Arc> { Some(self.presenter.gpu()) } - #[cfg(test)] fn present_resident_gpu_frame( &mut self, frame: &wgpu::Buffer, @@ -1013,20 +1010,30 @@ pub struct Ui { } impl Ui { - pub fn new(ps2: Arc, display: Arc, discs: Option>, timer_manager: Arc, event_loop: &EventLoop<()>, scale: u32, scroll_pixels_per_line: f64, lock_aspect_ratio: bool, renderer: crate::config::UiRenderer) -> Self { - let view = Self::open(display, event_loop, crate::machine::emulator_name().to_string(), scale, renderer); - Self { ps2, views: vec![view], discs, timer_manager, initial_scale: scale, scroll_pixels_per_line, lock_aspect_ratio, renderer } + pub fn new(ps2: Arc, display: Arc, discs: Option>, timer_manager: Arc, event_loop: &EventLoop<()>, scale: u32, scroll_pixels_per_line: f64, lock_aspect_ratio: bool, renderer: crate::config::UiRenderer) -> Result { + let view = Self::open(display, event_loop, crate::machine::emulator_name().to_string(), scale, renderer)?; + Ok(Self { ps2, views: vec![view], discs, timer_manager, initial_scale: scale, scroll_pixels_per_line, lock_aspect_ratio, renderer }) } /// A window of its own for another head of the machine (a second Newport). /// Keyboard and mouse reach the guest from either window. - pub fn add_head(&mut self, display: Arc, event_loop: &EventLoop<()>) { + pub fn add_head(&mut self, display: Arc, event_loop: &EventLoop<()>) -> Result<(), String> { let title = format!("{} (head {})", crate::machine::emulator_name(), self.views.len()); - self.views.push(Self::open(display, event_loop, title, self.initial_scale, self.renderer)); + self.views.push(Self::open(display, event_loop, title, self.initial_scale, self.renderer)?); + Ok(()) } /// Open a window and put its renderer in `display`'s renderer slot. - fn open(display: Arc, event_loop: &EventLoop<()>, title: String, scale: u32, renderer: crate::config::UiRenderer) -> View { + fn open(display: Arc, event_loop: &EventLoop<()>, title: String, scale: u32, renderer: crate::config::UiRenderer) -> Result { + #[cfg(feature = "wgpu")] + if display.requires_same_device_wgpu_presenter() + && renderer != crate::config::UiRenderer::Wgpu + { + return Err( + "the Newport direct resident mode requires renderer = \"wgpu\" and cannot fall back to GL" + .into(), + ); + } // The Indy's default video mode is 1280×1024; open the window at that // size (plus the status bar). The renderer snaps to the real resolution // via resize() once the PROM/IRIX programs its actual mode, keeping `scale`. @@ -1041,7 +1048,12 @@ impl Ui { if renderer == crate::config::UiRenderer::Wgpu { match Self::new_wgpu(&display, event_loop, window_builder.clone(), scale) { Ok((window, window_size, resize_request, display_res)) => { - return View::new(display, window, window_size, resize_request, display_res); + return Ok(View::new(display, window, window_size, resize_request, display_res)); + } + Err(e) if display.requires_same_device_wgpu_presenter() => { + return Err(format!( + "the Newport direct resident mode requires its same-device wgpu presenter: {e}" + )); } Err(e) => eprintln!("iris: the wgpu renderer is unavailable ({e}); drawing with GL"), } @@ -1199,7 +1211,7 @@ impl Ui { *display.renderer_slot().lock() = Some(Box::new(renderer)); - View::new(display, window, window_size, resize_request, display_res) + Ok(View::new(display, window, window_size, resize_request, display_res)) } /// The window and its wgpu renderer, installed in the board's renderer diff --git a/src/wgpu/batch.rs b/src/wgpu/batch.rs index 12b615c82..7ab112155 100644 --- a/src/wgpu/batch.rs +++ b/src/wgpu/batch.rs @@ -420,7 +420,6 @@ enum SetChoice { Wait(usize), } -#[cfg(test)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub(crate) struct BatchWaitStats { pub(crate) waits: u64, @@ -550,7 +549,6 @@ pub struct Batch { pub prims_total: u64, /// GPU times, with WGPU_BATCH_TIMING. pub timing: Option, - #[cfg(test)] wait_stats: BatchWaitStats, #[cfg(test)] wait_probe: Option>, @@ -578,7 +576,6 @@ impl Batch { /// Resident work must not inherit the shared batch profiler from the /// environment: enabling its map-and-wait path requires an explicit opt-in. - #[cfg(test)] pub(crate) fn new_bounded_unprofiled( gpu: &Arc, pipelines: &BatchPipelines, @@ -589,7 +586,6 @@ impl Batch { Self::with_set_policy(gpu, pipelines, grid, max_slots, SetPolicy::Bounded(max_sets), false) } - #[cfg(test)] pub(crate) fn enable_profiling(&mut self) -> Result<(), String> { if !self.is_empty() || self.timing.is_some() { return Err("batch profiling must be enabled once, between batches".into()); @@ -631,7 +627,6 @@ impl Batch { flushes: 0, prims_total: 0, timing: if environment_timing { Timing::new(gpu) } else { None }, - #[cfg(test)] wait_stats: BatchWaitStats::default(), #[cfg(test)] wait_probe: None, @@ -718,13 +713,11 @@ impl Batch { } SetChoice::Wait(k) => { let start = Instant::now(); + self.wait_stats.waits = self.wait_stats.waits.checked_add(1) + .ok_or("wgpu bounded set wait counter overflow")?; #[cfg(test)] - { - self.wait_stats.waits = self.wait_stats.waits.checked_add(1) - .ok_or("wgpu bounded set wait counter overflow")?; - if let Some(probe) = self.wait_probe.take() { - probe.send(()).map_err(|_| "wgpu bounded set wait test receiver closed")?; - } + if let Some(probe) = self.wait_probe.take() { + probe.send(()).map_err(|_| "wgpu bounded set wait test receiver closed")?; } let result = loop { self.gpu.instance.poll_all(false); @@ -739,13 +732,10 @@ impl Batch { } std::thread::sleep(Duration::from_micros(100)); }; - #[cfg(test)] - { - let elapsed = u64::try_from(start.elapsed().as_nanos()) - .map_err(|_| "wgpu bounded set wait duration overflow")?; - self.wait_stats.nanoseconds = self.wait_stats.nanoseconds.checked_add(elapsed) - .ok_or("wgpu bounded set wait duration overflow")?; - } + let elapsed = u64::try_from(start.elapsed().as_nanos()) + .map_err(|_| "wgpu bounded set wait duration overflow")?; + self.wait_stats.nanoseconds = self.wait_stats.nanoseconds.checked_add(elapsed) + .ok_or("wgpu bounded set wait duration overflow")?; result?; k } @@ -788,12 +778,10 @@ impl Batch { self.recording.slots.len() } - #[cfg(test)] pub(crate) fn tile_pair_bound(&self) -> u64 { u64::from(self.recording.pairs_bound) } - #[cfg(test)] pub(crate) fn wait_stats(&self) -> BatchWaitStats { self.wait_stats } diff --git a/src/wgpu/scanout.rs b/src/wgpu/scanout.rs index 0b9246a2f..86e29d647 100644 --- a/src/wgpu/scanout.rs +++ b/src/wgpu/scanout.rs @@ -189,7 +189,6 @@ impl Scanout { /// Reuse an already submitted slot, and only update the frame header when /// its dimensions change. The first nonempty frame must supply a slot. - #[cfg(test)] pub(crate) fn compose_cached( &mut self, pipeline: &ScanoutPipeline, diff --git a/src/workstation.rs b/src/workstation.rs index 866da6b70..5589f4a65 100644 --- a/src/workstation.rs +++ b/src/workstation.rs @@ -70,18 +70,21 @@ pub fn build(cfg: MachineConfig) -> Result, String> { #[cfg(test)] #[test] -fn build_rejects_newport_resident_wgpu_raster_without_prior_config_validation() { - for profile in [ - crate::config::MachineProfile::IndyIp24, - crate::config::MachineProfile::Indigo2Ip22, - crate::config::MachineProfile::Indigo2Ip28, - ] { - let mut cfg = MachineConfig::default(); - cfg.machine.profile = profile; - cfg.graphics.raster = crate::config::RasterUnit::Wgpu; - let error = build(cfg).err().expect("Newport raster gate"); - assert!(error.contains("until M3") && error.contains("IRIS_REX3_GPU"), "{error}"); - } +fn build_checks_newport_resident_mode_without_prior_full_validation() { + let mut cfg = MachineConfig::default(); + cfg.graphics.newport_mode = Some(crate::config::NewportWgpuMode::Readback); + let error = build(cfg).err().expect("Newport mode gate"); + assert!(error.contains("applies only"), "{error}"); +} + +#[cfg(all(test, not(feature = "wgpu")))] +#[test] +fn build_rejects_newport_wgpu_raster_without_the_feature() { + let mut cfg = MachineConfig::default(); + cfg.graphics.raster = crate::config::RasterUnit::Wgpu; + cfg.renderer = crate::config::UiRenderer::Wgpu; + let error = build(cfg).err().expect("Newport wgpu feature gate"); + assert!(error.contains("wgpu feature"), "{error}"); } /// The line a machine that takes `[machine] emu_id` prints at start-up: the