diff --git a/HACKING.md b/HACKING.md index 11ed80c0c..952ed2b6e 100644 --- a/HACKING.md +++ b/HACKING.md @@ -612,14 +612,24 @@ work. The test-only 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. -Display-state refresh copies VC2, XMAP, palette and cursor state and decodes -DID metadata; it never forms CPU plane slices. Changed decoded DID metadata -may still be uploaded as a cached 2 MiB map, so this is not yet a -small-state-only or GPU-DID scanout. 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 device are rejected. Screenshot, frame -request, VNC and headless test capture read the composed output only, and -publish pixels and complete requests only after a successful copy/map. +Display-state refresh copies VC2, XMAP, palette and cursor state without +decoding DID or forming CPU plane slices. Changed VC2 state uploads at most +the 64 KiB VC2 RAM and a 32-byte parameter block; a GPU kernel expands DID +before scanout, so 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, table termination, zero scanline length and +dimension changes on Vulkan and D3D12. The display retains GPU DID history +across scanout replacement and raster epochs, including rows and columns the +current VC2 tables leave untouched. An inactive display leaves this history +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 device are rejected. Screenshot, +frame request, VNC and headless test capture read the composed output only, +and publish pixels and complete requests only after a successful copy/map. 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 diff --git a/TESTING.md b/TESTING.md index 9a16697d2..6b1b9c0e5 100644 --- a/TESTING.md +++ b/TESTING.md @@ -291,13 +291,21 @@ 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 HOSTR, snapshot or other raw consumers, full opcode/lifecycle coverage, or live-window validation. -Changed decoded DID metadata may still upload a cached 2 MiB map, so this is not -yet the small-state-only/GPU-DID completion of direct scanout. +Resident refresh skips CPU DID decoding. A GPU kernel expands changed VC2 DID +tables before scanout, and traffic assertions require zero decoded-map uploads +and less than 128 KiB for the complete initial display-state transfer. The +hardware oracle covers disabled DID, malformed line pointers, line-table +termination with stale-row preservation, zero scanline length, and width and +height changes on both backends. It explicitly reads the GPU DID metadata for +byte-for-byte comparison with the CPU map; ordinary frames do not perform that +test readback. The tests also cover scanout replacement, reset and restore +epochs, switching displays, inactive display controls, and rejection of +unreconstructible initial DID history or a different GPU device. ```sh cargo test --locked -p iris --lib --features wgpu,rex-jit,rexdiag resident_ -- --test-threads=1 cargo test --locked -p iris --lib --features wgpu,rex-jit,rexdiag dev::ng1::rex3_flat_fill::tests -- --test-threads=1 -cargo test --locked -p iris --lib --features wgpu,rex-jit,rexdiag direct_ng1_scanout_display_generations_track_real_changes -- --test-threads=1 +cargo test --locked -p iris --lib --features wgpu,rex-jit,rexdiag direct_ng1_scanout_ -- --test-threads=1 REX3_RESIDENT_BACKEND=vulkan cargo test --locked -p iris --lib --features wgpu,rex-jit,rexdiag resident_gpu_ -- --ignored --nocapture --test-threads=1 REX3_RESIDENT_BACKEND=vulkan cargo test --locked -p iris --lib --features wgpu,rex-jit,rexdiag dev::ng1::wgpu::direct_tests -- --ignored --nocapture --test-threads=1 REX3_RESIDENT_BACKEND=dx12 cargo test --locked -p iris --lib --features wgpu,rex-jit,rexdiag resident_gpu_ -- --ignored --nocapture --test-threads=1 @@ -319,8 +327,8 @@ Remove-Item Env:\IRIS_NO_JIT -ErrorAction SilentlyContinue .\scripts\test-resident-gate.ps1 ``` -The script requires 25 `resident_` CPU tests, three flat-fill tests and one -display-generation test, then **10 resident GPU tests and five direct +The script requires 25 `resident_` CPU tests, three flat-fill tests and three +direct-state tests, then **10 resident GPU tests and six direct scanout tests on each backend**. It 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 -Backend vulkan` diff --git a/clanker/INDEX.md b/clanker/INDEX.md index 5615d5c3b..7bd052ada 100644 --- a/clanker/INDEX.md +++ b/clanker/INDEX.md @@ -499,7 +499,7 @@ Plans, task briefs and reviews written for agents. Each states its own status; m |---|---|---| | [wgpu-ui.md](plans/wgpu-ui.md) | plan | The iris window on wgpu: a presenter in `src/wgpu`, the board's device shared, VPro frames presented with no readback, then Newport's compositor in WGSL | | [cow-chd-sync-plan.md](plans/cow-chd-sync-plan.md) | plan | Plan: copy-on-write protection + "Syncing CHD file…" apply-on-shutdown | -| [GPUTodo.md](plans/GPUTodo.md) | plan | REX3 proper GPU mode: M0 and gated test-only M1 complete, minimum M2 partial; live raster remains unavailable until M3, with HostRW, lifecycle and full opcode coverage still open | +| [GPUTodo.md](plans/GPUTodo.md) | plan | REX3 proper GPU mode: merged gated foundation, unmerged M2-M10 candidate stack and planned upstream PRs, with presenter/CI coordination | | [gui-benchmark-plan.md](plans/gui-benchmark-plan.md) | plan | Benchmark in iris-gui | | [iris-daynaport-target.md](plans/iris-daynaport-target.md) | plan | Task: add a DaynaPort SCSI/Link target to the IRIS emulator | | [origin-octane-o2-landing-review.md](plans/origin-octane-o2-landing-review.md) | plan | Review of James Braid's SGI series (547 commits to 2026-11-11, two passes): Origin/Onyx2 two-node, Octane, O2, Fuel/Tezro (IP35), InfiniteReality (KONA); cache-as-RAM, DMA and interrupt paths per machine; ranked findings with status (KONA texture-bind race, Origin node-1 code never JIT-compiled, cross-node IRQs land on node 0, byte-wise DMA on Origin/IP35, BCM5701 lost coalescing wake) | diff --git a/clanker/plans/GPUTodo.md b/clanker/plans/GPUTodo.md index 47ed1b3b2..eb6d275d0 100644 --- a/clanker/plans/GPUTodo.md +++ b/clanker/plans/GPUTodo.md @@ -1,10 +1,12 @@ # REX3 proper GPU mode: implementation plan -Status: in progress 2026-11-11. M0 is complete; M1 is complete for the -gated, test-only resident slice; minimum M2 is partial. No production -GPU-authoritative Newport mode has landed, and live `raster = "wgpu"` -remains rejected until M3. The tile-binned `IRIS_REX3_GPU` prototype is -separate and CPU-canonical, not the target architecture. +Status: upstream integration in progress 2026-11-13. Upstream +[#569](https://github.com/techomancer/iris/pull/569) merged M0, the gated +test-only M1 resident slice and a minimum M2 scanout slice. The remaining +M2-M10 work exists in an unmerged candidate stack, not in upstream `main`. +No production GPU-authoritative Newport mode has landed. The tile-binned +`IRIS_REX3_GPU` prototype is separate and CPU-canonical, not the target +architecture. ## Goal @@ -24,6 +26,93 @@ The target is correctness and complete GPU ownership first. Optimize batching, tile size and command encoding only after representative guest workloads run without fallback or routine readback. +## Upstream delivery and existing work + +Build new, reviewable PRs against upstream `main` on top of the foundation +that has already merged; do not start a second implementation or replay the +old foundation commits. As of 2026-11-13, the baseline is `1c9d16ad`, which +contains the squash merge `bbbe5de0` of #569. Refresh the baseline before +preparing each PR. + +The local `feature/gpu-m2-display-state` through +`feature/gpu-m10-frontend` branches contain the rebased candidate work. The +25 existing PRs, Tzeentchnet/iris#4 through Tzeentchnet/iris#28, are still +open against other branches in the fork, not upstream `main`; their GitHub +heads have not been rebased. They are source and review references, not +evidence that their changes landed upstream. Do not change or close them as +part of updating this plan. + +The rebased M10 candidate passed the 130-case resident gate on Windows with +Vulkan and D3D12 on an NVIDIA RTX 5070, plus constructor-error and +production-machine tests. Five intermediate branches passed `cargo check` +with wgpu. These are candidate results, not tests of merged upstream +functionality; re-run the relevant checks after adapting each PR. Repeat live +application and window acceptance on the final integrated code. + +### Proposed PR sequence + +Use the existing patches and tests as candidates, splitting or combining them +for review as appropriate. The milestones and checkboxes below describe what +has landed on upstream `main`, not what has been implemented in a local branch. + +1. **Finish M2:** GPU DID/display state, same-device common presentation and + explicit readback compatibility. Start with Tzeentchnet/iris#4 through + Tzeentchnet/iris#6, keeping the resident path gated. +2. **M3 raster coverage:** command ABI and ordered blocks, lines, clipping, + pixel operations, blending, patterns, shading, copies and continuation. + Adapt Tzeentchnet/iris#7 through Tzeentchnet/iris#16 in reviewable slices; + keep strict CPU/JIT/GPU equivalence and no-fallback tests with each slice. +3. **M4 HostW:** staged payloads, segment semantics, lines, zoom, producer + retry and fast clears. Adapt Tzeentchnet/iris#17 through + Tzeentchnet/iris#22, preserving ordering and all-or-nothing `BUS_BUSY`. +4. **M5-M7 observation and lifecycle:** HOSTR, explicit observers, + snapshot/reset and failure handling. Adapt Tzeentchnet/iris#23 through + Tzeentchnet/iris#25, with exact observation and restore tests. +5. **M8 telemetry:** use Tzeentchnet/iris#26 for counters and latency + bounds; tune only after representative live execution is correct. +6. **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. + +Each upstream-facing PR should start from the latest upstream `main`, include +only its incremental change and tests, and remain buildable while production +activation is gated. Land prerequisites first, then rebase the next PR onto +the updated `main`; use an unmerged PR as a base only if the maintainer agrees +to that review arrangement. Reconcile shared code and documentation instead +of copying a stale fork version over upstream changes. + +### Pending upstream PRs to coordinate with + +- [#698](https://github.com/techomancer/iris/pull/698) is an open draft for + Odyssey/VPro changed-frame composition and a shared offscreen wgpu + presenter plus `ci-gpu` coverage. It is not Newport resident raster + coverage. Its `src/main.rs` and `src/ui.rs` changes overlap the candidate + stack, and `src/wgpu/present.rs` is a shared presenter API. If it lands, + adapt M2/M10 to that API rather than adding a competing offscreen path. + Preserve the same-device handoff, explicit resident failure behavior and + zero routine raw-plane readback. Its VPro tests count frames; add Newport + pixel, ordering and no-readback assertions rather than treating those counts + as proof of Newport correctness. Its software Vulkan and Metal CI are + useful additional coverage, not a replacement for the real Vulkan and + D3D12 resident hardware gate or a visible-window test. +- [#679](https://github.com/techomancer/iris/pull/679) is a broad pending + documentation and GUI pass with shared Newport, configuration, GUI and + documentation files. The inspected REX3 hunks are documentation edits, + not the resident implementation. Reconcile its changes if it lands; it is + not a prerequisite for the GPU stack. +- The pending audio/configuration work in + [#690](https://github.com/techomancer/iris/pull/690) and + [#694](https://github.com/techomancer/iris/pull/694), and the CI-clock work + in [#696](https://github.com/techomancer/iris/pull/696), also touch machine + or configuration files used by the candidate. They are not graphics + dependencies; preserve their changes, upstream constructor and PROM errors, + and explicit GPU errors when preparing activation. + +Recheck PR status and actual diffs before each submission: pending work may +change or merge. Do not assume a pending PR has landed or require it to land +before an otherwise independent Newport PR. + ## Non-negotiable decisions 1. **GPU raster mode makes GPU memory authoritative.** Once activated, normal @@ -107,7 +196,8 @@ Parts that do not carry into the proper design: ### MGRAS and Odyssey wgpu work -Use the common architecture on the `wgpuopt` branch as prior art: +The common architecture from the `wgpuopt` branch is already in upstream +`main`. Its original commits remain useful as prior art: - [common wgpu branch](https://github.com/techomancer/iris/tree/wgpuopt) - [VPro raster and wgpu window](https://github.com/techomancer/iris/commit/60a770076b3a30df421bbfeed149a3a0244b452a) @@ -125,9 +215,9 @@ Reuse or port: - board-specific scanout kernels over the rasterizer's real buffers; and - wgpu presentation on the board's device. -Do not blindly cherry-pick the branch. Reconcile its common layer with the -current REX3 branch's wgpu version, vendored wgpu-core changes, lifecycle -barriers and device-loss handling. +Do not blindly cherry-pick the branch or replace the merged common layer. +Build on the current upstream APIs and reconcile later presenter, lifecycle +and device-loss changes as they land. ## Target ownership model @@ -289,7 +379,9 @@ There is no CPU framebuffer fence between these steps. - The CLI/window frontend uses the common wgpu presenter. - `iris-gui` needs an eframe wgpu path before it can consume direct GPU frames - without readback. Its current Glow path remains for CPU raster mode. + without readback. Its current Glow path remains for CPU raster mode; the + candidate's explicit readback mode is a separate, labeled compatibility + option, not direct presentation. - CPU raster plus wgpu composition uploads dirty CPU framebuffer regions. - Wgpu raster plus GL/CPU composition explicitly reads back and is labeled a slow validation/compatibility mode. @@ -511,8 +603,9 @@ 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"` until M3 rather than silently treating that -selection as CPU raster. +rejects `raster = "wgpu"` rather than silently treating that selection as +CPU raster. Keep it unavailable until the coverage and activation gates are +met. ### M1 - Full authoritative GPU planes @@ -542,7 +635,7 @@ consumers; the legacy `rex-gpu` replay/live prototype remains CPU-canonical. ### M2 - Direct wgpu scanout - [x] Add a REX3 scanout kernel over the authoritative GPU planes. -- [ ] Upload only small display state/tables needed for each changed frame. +- [x] Upload only small display state/tables needed for each changed frame. - [ ] Present through the common wgpu presenter on the same device. - [x] Add CPU-raster plus wgpu-compositor uploads. - [x] Add an eframe wgpu path or explicitly keep proper GPU mode unavailable in @@ -558,12 +651,23 @@ 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. The `WgpuRenderer` has a test-only common-presenter handoff, but the marker/presentation tests use a same-device test presenter, not a -real live window. Changed decoded DID metadata can still upload a cached -2 MiB map; small-state-only/GPU DID expansion, live integration and the -readback compositor mode remain open. `iris-gui` cannot select this mode. +real live window. 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. +Live integration and the labeled readback compositor mode remain open. +`iris-gui` cannot select this mode. The Windows gate `.\scripts\test-resident-gate.ps1` checks 25 resident CPU, -3 flat-fill and 1 display-generation tests, plus 10 resident GPU and 5 +3 flat-fill and 3 direct-state tests, plus 10 resident GPU and 6 direct-scanout tests on each of Vulkan and D3D12. It requires the named backend on an integrated/discrete adapter and fails rather than skipping when unavailable; a single-backend run is not the complete gate. See @@ -661,6 +765,22 @@ snapshot support remain open. Complete when the selected design passes correctness and end-to-end gates on representative applications, not only synthetic fills. +### 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. + ## Validation requirements ### Correctness @@ -674,6 +794,10 @@ representative applications, not only synthetic fills. - Display output matches the existing compositor, including DID, palettes, overlays and cursor. - Reset, resolution changes and context continuation remain exact. +- An offscreen presenter exercises the same-device Newport path, including + changed and unchanged frames; visible-window acquisition and presentation + are tested separately. +- Software-adapter CI augments rather than weakens the hardware-adapter gate. ### HostRW @@ -720,7 +844,8 @@ expected result when the emulated guest CPU is the producer bottleneck. These are engineering choices to settle with prototypes or measurements, not reasons to change the architecture: -- exact common-wgpu commit/base to port into the current branch; +- 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; diff --git a/scripts/test-resident-gate.ps1 b/scripts/test-resident-gate.ps1 index bf37b307f..bc1d84845 100644 --- a/scripts/test-resident-gate.ps1 +++ b/scripts/test-resident-gate.ps1 @@ -68,11 +68,11 @@ function Invoke-ResidentTests { try { Invoke-ResidentTests 'resident_' 25 $false Invoke-ResidentTests 'dev::ng1::rex3_flat_fill::tests' 3 $false - Invoke-ResidentTests 'direct_ng1_scanout_display_generations_track_real_changes' 1 $false + Invoke-ResidentTests 'direct_ng1_scanout_' 3 $false foreach ($name in $requestedBackends) { $env:REX3_RESIDENT_BACKEND = $name Invoke-ResidentTests 'resident_gpu_' 10 $true - Invoke-ResidentTests 'dev::ng1::wgpu::direct_tests' 5 $true + Invoke-ResidentTests 'dev::ng1::wgpu::direct_tests' 6 $true } } finally { [Environment]::SetEnvironmentVariable('REX3_RESIDENT_BACKEND', $previousBackend, 'Process') diff --git a/src/dev/ng1/rex3_frame.rs b/src/dev/ng1/rex3_frame.rs index 263d2faba..cc5d90909 100644 --- a/src/dev/ng1/rex3_frame.rs +++ b/src/dev/ng1/rex3_frame.rs @@ -365,7 +365,13 @@ impl ResidentDisplay { rex.resident_frame.invalidate(); screen.topscan = cut.topscan; screen.status_bar_only = false; - let resized = screen.refresh_display_state(&rex.vc2, &rex.xmap0, &rex.cmap0, &rex.bt445, &rex.diag); + let resized = screen.refresh_resident_display_state( + &rex.vc2, + &rex.xmap0, + &rex.cmap0, + &rex.bt445, + &rex.diag, + ); self.resize_pending |= resized; // A mode write after the initial wait may have published a later // fence. Its GFIFO entry cannot pass this marker, so defer rather diff --git a/src/dev/ng1/wgpu/direct.rs b/src/dev/ng1/wgpu/direct.rs index 553a3ee3e..a7db429c8 100644 --- a/src/dev/ng1/wgpu/direct.rs +++ b/src/dev/ng1/wgpu/direct.rs @@ -5,12 +5,18 @@ use std::sync::Arc; use super::resident::{Position, ResidentError, ResidentRaster, ResidentScanoutBindings, ResidentStatus}; use super::{Ng1Scanout, Ng1Slot, RESIDENT_BINDINGS, SCANOUT, VC2_WORDS}; use crate::dev::ng1::rex3::{Rex3, REX3_SCREEN_HEIGHT}; +use crate::dev::ng1::vc2::{ + VC2_CTRL_BLACKOUT, VC2_CTRL_DID_EN, VC2_CTRL_VIDEO_TIMING_EN, VC2_REG_DID_ENTRY_PTR, + VC2_REG_DISPLAY_CONTROL, VC2_REG_SCANLINE_LEN, +}; use crate::disp::{Ng1DisplayGenerations, Ng1DisplaySource, FB_STRIDE}; -use crate::wgpu::{Gpu, Scanout, ScanoutPipeline, ScanoutShaders}; +use crate::wgpu::{Fragment, Gpu, Scanout, ScanoutPipeline, ScanoutShaders, Source}; const HEIGHT: usize = REX3_SCREEN_HEIGHT as usize; const DID_BYTES: usize = FB_STRIDE * HEIGHT; const MAX_PIXELS: u32 = (FB_STRIDE * HEIGHT) as u32; +const DID_EXPAND: Fragment = + Fragment::new("ng1_did_expand.wgsl", include_str!("shaders/did_expand.wgsl")); fn scanout_error(error: String) -> ResidentError { if error.starts_with("wgpu device lost (") { @@ -67,12 +73,14 @@ impl UploadCount { } /// Traffic caused by this compositor, not by raster initialization or -/// separately requested composed-frame capture. DID is a subset of display; -/// submitted counts mean queued with scanout, not GPU completion. +/// separately requested composed-frame capture. `did` counts decoded-map +/// uploads and must remain zero; `did_expansions` counts GPU expansion passes. +/// Submitted counts mean queued with scanout, not GPU completion. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub(crate) struct ResidentScanoutTraffic { pub(crate) display: UploadCount, pub(crate) did: UploadCount, + pub(crate) did_expansions: DispatchCount, pub(crate) raw_plane_uploads: UploadCount, pub(crate) raw_plane_readbacks: UploadCount, pub(crate) composed_readbacks: CaptureCount, @@ -87,6 +95,61 @@ pub(crate) struct CaptureCount { pub(crate) successful_bytes: u64, } +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) struct DispatchCount { + pub(crate) attempts: u64, + pub(crate) submitted: u64, +} + +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct DidParams { + did_ptr: u32, + scan_len: u32, + width: u32, + height: u32, + enabled: u32, + display_on: u32, + pad: [u32; 2], +} + +/// Display-owned DID history survives scanout replacement and raster epochs. +pub(crate) struct ResidentDid { + gpu: Arc, + buffer: wgpu::Buffer, + initialized: bool, +} + +pub(super) fn validate_initial_did(src: &Ng1DisplaySource<'_>) -> Result<(), ResidentError> { + let control = src.vc2_regs[VC2_REG_DISPLAY_CONTROL as usize]; + let active = control & (VC2_CTRL_BLACKOUT | VC2_CTRL_VIDEO_TIMING_EN) + == (VC2_CTRL_BLACKOUT | VC2_CTRL_VIDEO_TIMING_EN); + if active && control & VC2_CTRL_DID_EN == 0 { + return Ok(()); + } + + let did = src.did.get(..DID_BYTES) + .ok_or(ResidentError::InvalidState("resident DID history is unavailable"))?; + // A zero-initialized GPU map can retain only zeroes outside this decode. + let rows = if active { + let table = src.vc2_regs[VC2_REG_DID_ENTRY_PTR as usize] as usize; + (0..src.height).take_while(|&y| { + src.vc2_ram.get(table + y).is_some_and(|&line| line != u16::MAX) + }).count() + } else { + 0 + }; + let scan_len = (src.vc2_regs[VC2_REG_SCANLINE_LEN as usize] >> 5) as usize; + let written = if scan_len > 0 && scan_len <= FB_STRIDE { scan_len } else { src.width }; + for y in 0..HEIGHT { + let start = y * FB_STRIDE + if y < rows { written } else { 0 }; + if did[start..(y + 1) * FB_STRIDE].iter().any(|&value| value != 0) { + return Err(ResidentError::Unsupported("initial resident DID history requires a seed")); + } + } + Ok(()) +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct ResidentFrameStamp { serial: u64, @@ -119,14 +182,20 @@ pub(crate) enum CaptureFault { } /// The group retains the raster's plane buffers without exposing their -/// handles. Only DID, VC2 RAM and the small display slot are owned here. +/// handles. The display owns DID history; this scanout owns its bindings. pub(crate) struct Ng1ResidentScanout { pipeline: ScanoutPipeline, scanout: Scanout, bindings: ResidentScanoutBindings, - did: wgpu::Buffer, + display_layout: wgpu::BindGroupLayout, + did_layout: wgpu::BindGroupLayout, + did_source: Option, + did: Option, vc2: wgpu::Buffer, - display_group: wgpu::BindGroup, + did_params: wgpu::Buffer, + did_pipeline: wgpu::ComputePipeline, + did_group: Option, + display_group: Option, uploaded: Option, dimensions: Option<(u32, u32)>, traffic: ResidentScanoutTraffic, @@ -166,19 +235,38 @@ impl Ng1Scanout { }).map_err(scanout_error)?; let scanout = Scanout::new(gpu, &pipeline, MAX_PIXELS, size_of::() as u64); let usage = wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST; - let did = gpu.buffer("resident ng1 decoded DID", DID_BYTES as u64, usage); let vc2 = gpu.buffer("resident ng1 vc2 ram", (2 * VC2_WORDS) as u64, usage); - let display_group = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("resident ng1 display state"), - layout: &layout, + let did_params = gpu.buffer( + "resident ng1 DID parameters", + size_of::() as u64, + wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + ); + let did_entry = |binding, ty| wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::COMPUTE, + ty: wgpu::BindingType::Buffer { + ty, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }; + let did_layout = gpu.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("resident ng1 DID expansion"), entries: &[ - wgpu::BindGroupEntry { binding: 0, resource: did.as_entire_binding() }, - wgpu::BindGroupEntry { binding: 1, resource: vc2.as_entire_binding() }, + did_entry(0, wgpu::BufferBindingType::Storage { read_only: true }), + did_entry(1, wgpu::BufferBindingType::Storage { read_only: false }), + did_entry(2, wgpu::BufferBindingType::Uniform), ], }); + let did_pipeline = Source::new("resident ng1 DID expansion") + .body(DID_EXPAND) + .compute_pipeline(&gpu.device, "expand_did", &[&did_layout]) + .map_err(scanout_error)?; gpu.check().map_err(scanout_error)?; Ok(Ng1ResidentScanout { - pipeline, scanout, bindings, did, vc2, display_group, + 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, }) @@ -194,10 +282,36 @@ impl Ng1ResidentScanout { self.traffic } + #[cfg(test)] + pub(super) fn did_for_test(&self) -> &wgpu::Buffer { + self.did.as_ref().expect("DID buffer is bound after composition") + } + pub(crate) fn fail_next_capture(&mut self, fault: CaptureFault) { self.capture_fault = Some(fault); } + fn expand_did(&self, params: &DidParams) -> Result<(), ResidentError> { + let gpu = self.scanout.gpu(); + let group = self.did_group.as_ref() + .ok_or(ResidentError::InvalidState("resident DID bindings are missing"))?; + gpu.write(&self.did_params, 0, bytemuck::bytes_of(params)); + let mut encoder = gpu.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("resident ng1 DID expansion"), + }); + { + let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { + label: Some("resident ng1 DID expansion"), + timestamp_writes: None, + }); + pass.set_pipeline(&self.did_pipeline); + pass.set_bind_group(0, group, &[]); + pass.dispatch_workgroups((HEIGHT as u32).div_ceil(64), 1, 1); + } + gpu.submit(encoder); + gpu.check().map_err(scanout_error) + } + /// Capture only the still-current composed output. An older frame token /// cannot read a newer composition, even when its dimensions are equal. pub(crate) fn capture( @@ -258,8 +372,7 @@ impl Ng1ResidentScanout { } let cursor_x = src.vc2_regs[crate::dev::ng1::vc2::VC2_REG_CURRENT_CURSOR_X as usize] as i64 - 31 + src.cursor_x_adjust as i64; - if src.did.len() < DID_BYTES || src.vc2_ram.len() != VC2_WORDS - || src.topscan.checked_add(1).is_none() + if src.vc2_ram.len() != VC2_WORDS || src.topscan.checked_add(1).is_none() || !(i32::MIN as i64..=i32::MAX as i64).contains(&cursor_x) { return Err(ResidentError::InvalidState("resident scanout display state is incomplete")); @@ -267,46 +380,117 @@ impl Ng1ResidentScanout { self.scanout.gpu().check().map_err(scanout_error)?; let generation = src.generations; + let mut did_state = src.resident_did.lock(); + if let Some(did) = did_state.as_ref() { + if !Arc::ptr_eq(&did.gpu, self.scanout.gpu()) { + return Err(ResidentError::DeviceMismatch); + } + } + if did_state.as_ref().is_none_or(|did| !did.initialized) { + validate_initial_did(src)?; + } + if did_state.is_none() { + let gpu = self.scanout.gpu(); + let buffer = gpu.buffer( + "resident ng1 decoded DID", + DID_BYTES as u64, + wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC, + ); + gpu.check().map_err(scanout_error)?; + *did_state = Some(ResidentDid { gpu: Arc::clone(gpu), buffer, initialized: false }); + } + if self.did_source != Some(generation.source) { + let gpu = self.scanout.gpu(); + let did = did_state.as_ref() + .ok_or(ResidentError::InvalidState("resident DID state is missing"))? + .buffer.clone(); + let did_group = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("resident ng1 DID expansion"), + layout: &self.did_layout, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: self.vc2.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 1, resource: did.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 2, resource: self.did_params.as_entire_binding() }, + ], + }); + let display_group = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("resident ng1 display state"), + layout: &self.display_layout, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: did.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 1, resource: self.vc2.as_entire_binding() }, + ], + }); + gpu.check().map_err(scanout_error)?; + self.did = Some(did); + self.did_group = Some(did_group); + self.display_group = Some(display_group); + self.did_source = Some(generation.source); + } let previous = self.uploaded; let changed = |select: fn(Ng1DisplayGenerations) -> u64| { previous.is_none_or(|old| old.source != generation.source || select(old) != select(generation)) }; - let did_changed = changed(|g| g.did); let vc2_changed = changed(|g| g.vc2_ram); - let slot_changed = changed(|g| g.vc2_regs) + let vc2_regs_changed = changed(|g| g.vc2_regs); + let geometry_changed = changed(|g| g.geometry); + let dimensions = (src.width as u32, src.height as u32); + let header_changed = self.dimensions != Some(dimensions); + let did_changed = vc2_changed || vc2_regs_changed || header_changed; + let slot_changed = vc2_regs_changed || changed(|g| g.xmap) || changed(|g| g.cmap) || changed(|g| g.ramdac) - || changed(|g| g.geometry); - let dimensions = (src.width as u32, src.height as u32); - let header_changed = self.dimensions != Some(dimensions); + || geometry_changed; let slot = slot_changed.then(|| Ng1Slot::of_display(src)); - let did_bytes = if did_changed { DID_BYTES as u64 } else { 0 }; - let display_count = did_changed as u64 + vc2_changed as u64 + slot_changed as u64 + header_changed as u64; - let display_bytes = did_bytes - + if vc2_changed { (2 * VC2_WORDS) as u64 } else { 0 } - + if slot_changed { size_of::() as u64 } else { 0 } + let control = src.vc2_regs[VC2_REG_DISPLAY_CONTROL as usize]; + let did_params = DidParams { + did_ptr: src.vc2_regs[VC2_REG_DID_ENTRY_PTR as usize] as u32, + scan_len: (src.vc2_regs[VC2_REG_SCANLINE_LEN as usize] >> 5) as u32, + width: dimensions.0, + height: dimensions.1, + enabled: u32::from(control & VC2_CTRL_DID_EN != 0), + display_on: u32::from( + control & (VC2_CTRL_BLACKOUT | VC2_CTRL_VIDEO_TIMING_EN) + == (VC2_CTRL_BLACKOUT | VC2_CTRL_VIDEO_TIMING_EN), + ), + pad: [0; 2], + }; + let expansion_uploads = vc2_changed as u64 + did_changed as u64; + let expansion_bytes = if vc2_changed { (2 * VC2_WORDS) as u64 } else { 0 } + + if did_changed { size_of::() as u64 } else { 0 }; + let scanout_uploads = slot_changed as u64 + header_changed as u64; + let scanout_bytes = if slot_changed { size_of::() as u64 } else { 0 } + if header_changed { 16 } else { 0 }; - self.traffic.display.attempt(display_count, display_bytes); - self.traffic.did.attempt(did_changed as u64, did_bytes); - if did_changed { - self.scanout.gpu().write(&self.did, 0, &src.did[..DID_BYTES]); - } + self.traffic.display.attempt( + expansion_uploads + scanout_uploads, + expansion_bytes + scanout_bytes, + ); if vc2_changed { self.scanout.gpu().write(&self.vc2, 0, bytemuck::cast_slice(src.vc2_ram)); } + if did_changed { + self.traffic.did_expansions.attempts += 1; + self.expand_did(&did_params)?; + did_state.as_mut() + .ok_or(ResidentError::InvalidState("resident DID state is missing"))? + .initialized = true; + self.traffic.display.submit(expansion_uploads, expansion_bytes); + self.traffic.did_expansions.submitted += 1; + } let before_submission = self.scanout.frames; + let display_group = self.display_group.as_ref() + .ok_or(ResidentError::InvalidState("resident display bindings are missing"))?; let result = self.scanout.compose_cached( &self.pipeline, dimensions.0, dimensions.1, slot.as_ref().map(|slot| bytemuck::bytes_of(slot).as_ref()), - &[&self.bindings.group, &self.display_group], + &[&self.bindings.group, display_group], ); if self.scanout.frames != before_submission { - self.traffic.display.submit(display_count, display_bytes); - self.traffic.did.submit(did_changed as u64, did_bytes); + self.traffic.display.submit(scanout_uploads, scanout_bytes); self.traffic.frames_submitted += 1; } result.map_err(scanout_error)?; diff --git a/src/dev/ng1/wgpu/direct_tests.rs b/src/dev/ng1/wgpu/direct_tests.rs index ae66bc2eb..93fc445ec 100644 --- a/src/dev/ng1/wgpu/direct_tests.rs +++ b/src/dev/ng1/wgpu/direct_tests.rs @@ -4,7 +4,10 @@ use std::time::Instant; use parking_lot::Mutex; -use super::direct::{CaptureFault, Ng1ResidentScanout, ResidentScanoutSource, ResidentScanoutTraffic}; +use super::direct::{ + validate_initial_did, CaptureFault, Ng1ResidentScanout, ResidentScanoutSource, + ResidentScanoutTraffic, +}; use super::resident::{ObservationCounters, ResidentError, ResidentFaultInjection, ResidentGpu, ResidentRaster}; use super::{Ng1Scanout, Ng1Slot, VC2_WORDS}; use crate::compositor::{CompositorSource, SwCompositor}; @@ -37,7 +40,7 @@ use crate::wgpu::{Gpu, GpuOptions, Staging}; const WIDTH: usize = 64; const HEIGHT: usize = 48; const PLANE_WORDS: usize = FB_STRIDE * 1024; -const DID_BYTES: u64 = PLANE_WORDS as u64; +const DID_PARAMS_BYTES: u64 = 8 * size_of::() as u64; const DID_LINE: usize = 0x500; const CURSOR: usize = 0x900; @@ -113,6 +116,16 @@ impl Display { fn refresh(&mut self) -> bool { self.screen.refresh_display_state(&self.vc2, &self.xmap, &self.cmap, &self.dac, &self.diag) } + + fn refresh_resident(&mut self) -> bool { + self.screen.refresh_resident_display_state( + &self.vc2, + &self.xmap, + &self.cmap, + &self.dac, + &self.diag, + ) + } } fn cpu_source<'a>(state: &'a Ng1DisplaySource<'_>, rgb: &'a [u32], aux: &'a [u32]) -> CompositorSource<'a> { @@ -229,7 +242,21 @@ fn compare_frame( let bytes = staging.read(frame.gpu, frame.buffer, &[(0, old_gpu.len() as u64 * 4)]) .expect("composed frame readback"); let direct = bytemuck::cast_slice::(&bytes).to_vec(); - assert_eq!(direct, old_gpu, "direct and CPU-source GPU compositors differ"); + assert_pixels_eq("direct and CPU-source GPU compositors", &direct, &old_gpu); + let did = staging.read(scan.gpu(), scan.did_for_test(), &[(0, PLANE_WORDS as u64)]) + .expect("decoded DID readback"); + assert!(state.did.len() >= PLANE_WORDS, "CPU DID map is too small"); + let expected_did = &state.did[..PLANE_WORDS]; + assert_eq!(did.len(), expected_did.len(), "decoded DID map size"); + if let Some(index) = did.iter().zip(expected_did).position(|(gpu, cpu)| gpu != cpu) { + panic!( + "decoded DID mismatch at ({}, {}): GPU={}, CPU={}", + index % FB_STRIDE, + index / FB_STRIDE, + did[index], + expected_did[index], + ); + } for y in 0..state.height { for x in 0..state.width { let pixel = sw.pixels()[y * FB_STRIDE + x]; @@ -247,6 +274,10 @@ fn assert_no_plane_traffic(traffic: ResidentScanoutTraffic) { assert_eq!(traffic.raw_plane_readbacks.attempts, 0); assert_eq!(traffic.raw_plane_readbacks.attempted_bytes, 0); assert_eq!(traffic.raw_plane_readbacks.submitted, 0); + assert_eq!(traffic.did.attempts, 0, "decoded DID map upload attempted"); + assert_eq!(traffic.did.attempted_bytes, 0, "decoded DID map bytes attempted"); + assert_eq!(traffic.did.submitted, 0, "decoded DID map upload submitted"); + assert_eq!(traffic.did.submitted_bytes, 0, "decoded DID map bytes submitted"); } #[test] @@ -294,6 +325,80 @@ fn direct_ng1_scanout_display_generations_track_real_changes() { assert_ne!(Display::new().screen.display_state().generations.source, first.source); } +#[test] +fn direct_ng1_scanout_refresh_skips_cpu_did_decode() { + let mut display = Display::new(); + display.screen.did.fill(0x5a); + let before = display.screen.display_state().generations; + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[DID_LINE] = 7; + vc2.regs[VC2_REG_DID_ENTRY_PTR as usize] += 1; + vc2.dirty = true; + } + display.refresh_resident(); + + let after = display.screen.display_state().generations; + assert!(after.vc2_ram > before.vc2_ram); + assert!(after.vc2_regs > before.vc2_regs); + assert_eq!(after.did, before.did); + assert!(display.screen.did.iter().all(|&did| did == 0x5a)); +} + +#[test] +fn direct_ng1_scanout_initial_did_history_is_checked() { + let mut display = Display::new(); + assert_eq!(validate_initial_did(&display.screen.display_state()), Ok(())); + + display.screen.did[WIDTH] = 4; + assert_eq!( + validate_initial_did(&display.screen.display_state()), + Err(ResidentError::Unsupported("initial resident DID history requires a seed")), + ); + display.screen.did[WIDTH] = 0; + display.screen.did[3 * FB_STRIDE] = 6; + assert_eq!(validate_initial_did(&display.screen.display_state()), Ok(())); + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[0x300 + 3] = 0xffff; + vc2.dirty = true; + } + display.refresh_resident(); + assert_eq!( + validate_initial_did(&display.screen.display_state()), + Err(ResidentError::Unsupported("initial resident DID history requires a seed")), + ); + + { + let mut vc2 = display.vc2.lock(); + vc2.regs[VC2_REG_DISPLAY_CONTROL as usize] &= !VC2_CTRL_DID_EN; + vc2.dirty = true; + } + display.refresh_resident(); + let mut no_cpu_map = display.screen.display_state(); + no_cpu_map.did = &[]; + assert_eq!(validate_initial_did(&no_cpu_map), Ok(())); + + { + let mut vc2 = display.vc2.lock(); + vc2.regs[VC2_REG_DISPLAY_CONTROL as usize] &= !VC2_CTRL_BLACKOUT; + vc2.dirty = true; + } + display.refresh_resident(); + assert_eq!( + validate_initial_did(&display.screen.display_state()), + Err(ResidentError::Unsupported("initial resident DID history requires a seed")), + ); + let mut no_cpu_map = display.screen.display_state(); + no_cpu_map.did = &[]; + assert_eq!( + validate_initial_did(&no_cpu_map), + Err(ResidentError::InvalidState("resident DID history is unavailable")), + ); +} + #[test] #[ignore = "requires the requested conformant hardware GPU backend"] fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { @@ -316,16 +421,21 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { let first = compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); let traffic = scan.traffic(); assert_eq!(traffic.frames_submitted, 1); - assert_eq!(traffic.did.submitted, 1); - assert_eq!(traffic.did.submitted_bytes, DID_BYTES); + assert_eq!(traffic.did_expansions.attempts, 1); + assert_eq!(traffic.did_expansions.submitted, 1); assert_eq!(traffic.display.submitted, 4); - assert_eq!(traffic.display.submitted_bytes, DID_BYTES + (2 * VC2_WORDS) as u64 + size_of::() as u64 + 16); + assert_eq!( + traffic.display.submitted_bytes, + (2 * VC2_WORDS) as u64 + DID_PARAMS_BYTES + size_of::() as u64 + 16, + ); + assert!(traffic.display.submitted_bytes < 128 * 1024, "initial display state is not small"); assert_no_plane_traffic(traffic); let again = compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); assert_eq!(again, first); assert_eq!(scan.traffic().display, traffic.display, "unchanged display state was uploaded"); assert_eq!(scan.traffic().did, traffic.did); + assert_eq!(scan.traffic().did_expansions, traffic.did_expansions); let mut gpu_context = fill_context(); resident.enqueue(&mut gpu_context).expect("resident fill"); @@ -354,7 +464,7 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { display.refresh(); let palette = compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); assert_ne!(palette, drawn, "palette change must affect the composed frame"); - assert_eq!(scan.traffic().did.submitted, 1); + assert_eq!(scan.traffic().did_expansions.submitted, 1); { let mut dac = display.dac.lock(); @@ -364,7 +474,7 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { display.refresh(); let gamma = compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); assert_ne!(gamma, palette, "gamma change must affect the composed frame"); - assert_eq!(scan.traffic().did.submitted, 1); + assert_eq!(scan.traffic().did_expansions.submitted, 1); { let mut vc2 = display.vc2.lock(); @@ -376,12 +486,12 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { display.refresh(); let cursor = compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); assert_ne!(cursor, gamma, "cursor changes must affect the composed frame"); - assert_eq!(scan.traffic().did.submitted, 1); + assert_eq!(scan.traffic().did_expansions.submitted, 2); display.screen.topscan = 1023; let scroll = compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); assert_ne!(scroll, cursor, "topscan must wrap through the resident plane"); - assert_eq!(scan.traffic().did.submitted, 1); + assert_eq!(scan.traffic().did_expansions.submitted, 2); { let mut xmap = display.xmap.lock(); @@ -391,7 +501,7 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { display.refresh(); let xmap = compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); assert_ne!(xmap, scroll, "XMAP change must affect the composed frame"); - assert_eq!(scan.traffic().did.submitted, 1); + assert_eq!(scan.traffic().did_expansions.submitted, 2); { let mut vc2 = display.vc2.lock(); @@ -401,15 +511,14 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { display.refresh(); let did = compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); assert_ne!(did, xmap, "decoded DID change must affect the composed frame"); - assert_eq!(scan.traffic().did.submitted, 2); - assert_eq!(scan.traffic().did.submitted_bytes, 2 * DID_BYTES); + assert_eq!(scan.traffic().did_expansions.submitted, 3); let after_did = scan.traffic(); compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); assert_eq!(scan.traffic().display, after_did.display); assert_eq!(scan.traffic().did, after_did.did); - // Shrinking and restoring the visible height cannot require a DID - // upload: the complete Newport DID map was already uploaded. + // Geometry changes re-expand on the GPU but never upload a + // decoded map. for height in [HEIGHT / 2, HEIGHT] { { let mut vc2 = display.vc2.lock(); @@ -419,8 +528,8 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { display.refresh(); assert_eq!(display.screen.height, height); compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); - assert_eq!(scan.traffic().did.submitted, 2); } + assert_eq!(scan.traffic().did_expansions.submitted, 5); assert_no_plane_traffic(scan.traffic()); let status = resident.status().unwrap(); assert_eq!((status.initialization_uploads, status.initialization_bytes), (2, 2 * PLANE_WORDS as u64 * 4)); @@ -433,12 +542,37 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { let mut other_display = Display::new(); let before_other_source = scan.traffic(); compare_frame(&mut scan, &mut resident, &mut other_display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); - assert_eq!(scan.traffic().did.submitted, before_other_source.did.submitted + 1); + assert_eq!( + scan.traffic().did_expansions.submitted, + before_other_source.did_expansions.submitted + 1, + ); + { + let mut vc2 = other_display.vc2.lock(); + vc2.ram[DID_LINE] = 6; + vc2.ram[DID_LINE + 1] = 0xffe0; + vc2.dirty = true; + } + other_display.refresh(); + compare_frame(&mut scan, &mut resident, &mut other_display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); + { + let mut vc2 = other_display.vc2.lock(); + vc2.ram[DID_LINE] = 2; + vc2.ram[0x300 + 3] = 0xffff; + vc2.dirty = true; + } + other_display.refresh(); + compare_frame(&mut scan, &mut resident, &mut other_display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); + assert_eq!(other_display.screen.did[3 * FB_STRIDE], 6); + compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); + compare_frame(&mut scan, &mut resident, &mut other_display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); - assert_eq!(scan.traffic().did.submitted, before_other_source.did.submitted + 2); + assert_eq!( + scan.traffic().did_expansions.submitted, + before_other_source.did_expansions.submitted + 6, + ); let stale_stamp = scan.compose(&display.screen.display_state(), &mut resident).unwrap().stamp(); - let current_stamp = scan.compose(&display.screen.display_state(), &mut resident).unwrap().stamp(); + let mut current_stamp = scan.compose(&display.screen.display_state(), &mut resident).unwrap().stamp(); let mut captured = vec![0xfeed_beef; WIDTH * HEIGHT]; assert!(matches!(scan.capture(stale_stamp, &mut resident, &mut captured), Err(ResidentError::InvalidState("resident composed frame is stale")))); @@ -448,14 +582,21 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { assert_eq!((composed.attempts, composed.successes), (1, 1)); assert_eq!(composed.successful_bytes, (WIDTH * HEIGHT * 4) as u64); + let before_undecoded = scan.traffic(); + let mut undecoded = display.screen.display_state(); + undecoded.did = &[]; + let undecoded_stamp = scan.compose(&undecoded, &mut resident).expect("compose without CPU DID").stamp(); + let mut undecoded_frame = vec![0; WIDTH * HEIGHT]; + scan.capture(undecoded_stamp, &mut resident, &mut undecoded_frame) + .expect("capture composition without CPU DID"); + assert_eq!(undecoded_frame, captured); + current_stamp = undecoded_stamp; + assert_eq!(scan.traffic().display, before_undecoded.display); + assert_eq!(scan.traffic().did_expansions, before_undecoded.did_expansions); + assert_no_plane_traffic(scan.traffic()); + let before_invalid = scan.traffic(); let mut invalid = display.screen.display_state(); - invalid.did = &[]; - assert!(matches!( - scan.compose(&invalid, &mut resident), - Err(ResidentError::InvalidState("resident scanout display state is incomplete")) - )); - let mut invalid = display.screen.display_state(); invalid.cursor_x_adjust = i32::MAX; assert!(matches!( scan.compose(&invalid, &mut resident), @@ -471,6 +612,16 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { let other_gpu = required_gpu(); assert!(matches!(Ng1Scanout::new_resident(&other_gpu, &resident), Err(ResidentError::DeviceMismatch))); + let mut other_resident = ResidentRaster::new( + ResidentGpu::activate(Arc::clone(&other_gpu), &rgb, &aux).expect("activate other device"), + ); + let mut other_scan = Ng1Scanout::new_resident(&other_gpu, &other_resident) + .expect("bind other device"); + assert!(matches!( + other_scan.compose(&display.screen.display_state(), &mut other_resident), + Err(ResidentError::DeviceMismatch) + )); + assert_eq!(other_scan.traffic(), ResidentScanoutTraffic::default()); resident.reset(&rgb, &aux).expect("reset resident planes"); assert!(matches!(scan.capture(current_stamp, &mut resident, &mut captured), @@ -518,6 +669,335 @@ fn direct_ng1_scanout_gpu_reuses_planes_and_caches_display_state() { .unwrap_or_else(|panic| std::panic::resume_unwind(panic)); } +#[test] +#[ignore = "requires the requested conformant hardware GPU backend"] +fn direct_ng1_scanout_gpu_did_expansion_matches_cpu_edge_cases() { + std::thread::Builder::new().name("direct-ng1-did".into()).stack_size(64 * 1024 * 1024) + .spawn(|| { + let gpu = required_gpu(); + let (rgb, aux) = seed_planes(); + let mut resident = ResidentRaster::new( + ResidentGpu::activate(Arc::clone(&gpu), &rgb, &aux).expect("activate resident planes"), + ); + let mut scan = Ng1Scanout::new_resident(&gpu, &resident).expect("direct bindings"); + let mut legacy = Ng1Scanout::new(&gpu).expect("CPU-source GPU compositor"); + let mut display = Display::new(); + let mut sw = SwCompositor::new(); + let mut staging = Staging::default(); + + let mut stale_display = Display::new(); + stale_display.screen.did[3 * FB_STRIDE] = 6; + { + let mut vc2 = stale_display.vc2.lock(); + vc2.ram[0x300 + 3] = 0xffff; + vc2.dirty = true; + } + stale_display.refresh_resident(); + let mut stale_scan = Ng1Scanout::new_resident(&gpu, &resident).expect("stale bindings"); + let state = stale_display.screen.display_state(); + assert!(matches!( + stale_scan.compose(&state, &mut resident), + Err(ResidentError::Unsupported("initial resident DID history requires a seed")) + )); + assert_eq!(stale_scan.traffic(), ResidentScanoutTraffic::default()); + { + let mut vc2 = stale_display.vc2.lock(); + vc2.regs[VC2_REG_DISPLAY_CONTROL as usize] &= !VC2_CTRL_DID_EN; + vc2.dirty = true; + } + stale_display.refresh(); + compare_frame( + &mut stale_scan, + &mut resident, + &mut stale_display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + assert_no_plane_traffic(stale_scan.traffic()); + + let initial = compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + + { + let mut vc2 = display.vc2.lock(); + vc2.regs[VC2_REG_DISPLAY_CONTROL as usize] &= !VC2_CTRL_DID_EN; + vc2.dirty = true; + } + display.refresh(); + let disabled = compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + assert_ne!(disabled, initial); + assert!(display.screen.did.iter().all(|&did| did == 0)); + + { + let mut vc2 = display.vc2.lock(); + vc2.regs[VC2_REG_DISPLAY_CONTROL as usize] |= VC2_CTRL_DID_EN; + vc2.ram[DID_LINE] = 6; + vc2.ram[DID_LINE + 1] = 0xffe0; + vc2.dirty = true; + } + display.refresh(); + compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + assert!(display.screen.did[..WIDTH].iter().all(|&did| did == 6)); + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[DID_LINE] = 2; + vc2.ram[0x300 + 3] = 0xffff; + vc2.dirty = true; + } + display.refresh(); + let preserved = compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + for y in 0..3 { + assert!(display.screen.did[y * FB_STRIDE..y * FB_STRIDE + WIDTH] + .iter() + .all(|&did| did == 2)); + } + assert!(display.screen.did[3 * FB_STRIDE..3 * FB_STRIDE + WIDTH] + .iter() + .all(|&did| did == 6)); + + let mut rebound = Ng1Scanout::new_resident(&gpu, &resident).expect("rebind scanout"); + let mut state = display.screen.display_state(); + state.did = &[]; + let frame = rebound.compose(&state, &mut resident).expect("rebind without CPU DID"); + let bytes = staging.read(frame.gpu, frame.buffer, &[(0, preserved.len() as u64 * 4)]) + .expect("rebound frame readback"); + assert_pixels_eq("rebound frame", bytemuck::cast_slice(&bytes), &preserved); + compare_frame( + &mut rebound, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[DID_LINE] = 4; + vc2.ram[0x300 + 2] = 0xfffe; + vc2.ram[0x300 + 3] = DID_LINE as u16; + vc2.dirty = true; + } + display.refresh(); + compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + assert!(display.screen.did[2 * FB_STRIDE..2 * FB_STRIDE + WIDTH] + .iter() + .all(|&did| did == 0)); + assert!(display.screen.did[3 * FB_STRIDE..3 * FB_STRIDE + WIDTH] + .iter() + .all(|&did| did == 4)); + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[DID_LINE] = 5; + vc2.ram[0x300 + 2] = DID_LINE as u16; + vc2.regs[VC2_REG_SCANLINE_LEN as usize] = ((WIDTH / 2) << 5) as u16; + vc2.dirty = true; + } + display.refresh(); + compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + assert!(display.screen.did[..WIDTH / 2].iter().all(|&did| did == 5)); + assert!(display.screen.did[WIDTH / 2..WIDTH].iter().all(|&did| did == 4)); + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[DID_LINE] = 7; + vc2.regs[VC2_REG_SCANLINE_LEN as usize] = 0; + vc2.dirty = true; + } + display.refresh(); + compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + assert!(display.screen.did[..WIDTH].iter().all(|&did| did == 7)); + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[0x101] = (HEIGHT / 2) as u16; + vc2.ram[0x200] = 0x8000 | (((WIDTH - 16) as u16 / 2) << 8); + vc2.dirty = true; + } + display.refresh(); + assert_eq!((display.screen.width, display.screen.height), (WIDTH - 16, HEIGHT / 2)); + compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[0x101] = HEIGHT as u16; + vc2.ram[0x200] = 0x8000 | ((WIDTH as u16 / 2) << 8); + vc2.dirty = true; + } + display.refresh(); + assert_eq!((display.screen.width, display.screen.height), (WIDTH, HEIGHT)); + compare_frame( + &mut scan, + &mut resident, + &mut display, + &mut legacy, + &mut sw, + &mut staging, + &rgb, + &aux, + ); + + assert_eq!(scan.traffic().did_expansions.submitted, 9); + assert_no_plane_traffic(scan.traffic()); + + { + let mut vc2 = display.vc2.lock(); + vc2.ram[DID_LINE] = 6; + vc2.ram[DID_LINE + 1] = 0xffe0; + vc2.ram[0x300 + 3] = DID_LINE as u16; + vc2.regs[VC2_REG_SCANLINE_LEN as usize] = (WIDTH << 5) as u16; + vc2.dirty = true; + } + display.refresh(); + compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); + { + let mut vc2 = display.vc2.lock(); + vc2.ram[DID_LINE] = 2; + vc2.ram[0x300 + 3] = 0xffff; + vc2.dirty = true; + } + display.refresh(); + compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); + + for inactive_bit in [VC2_CTRL_BLACKOUT, VC2_CTRL_VIDEO_TIMING_EN] { + for did_enabled in [false, true] { + let previous_did = display.screen.did[0]; + let next_did = if did_enabled { 10 } else { 9 }; + { + let mut vc2 = display.vc2.lock(); + vc2.regs[VC2_REG_DISPLAY_CONTROL as usize] &= !inactive_bit; + if !did_enabled { + vc2.regs[VC2_REG_DISPLAY_CONTROL as usize] &= !VC2_CTRL_DID_EN; + } + vc2.ram[DID_LINE] = next_did; + vc2.dirty = true; + } + display.refresh(); + compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); + assert_eq!(display.screen.did[0], previous_did); + assert_eq!(display.screen.did[3 * FB_STRIDE], 6); + { + let mut vc2 = display.vc2.lock(); + vc2.regs[VC2_REG_DISPLAY_CONTROL as usize] |= inactive_bit | VC2_CTRL_DID_EN; + vc2.dirty = true; + } + display.refresh(); + compare_frame(&mut scan, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux); + assert_eq!(display.screen.did[0], next_did as u8); + assert_eq!(display.screen.did[3 * FB_STRIDE], 6); + } + } + + let before_reset = scan.traffic(); + resident.reset(&rgb, &aux).expect("reset resident planes"); + assert!(matches!( + scan.compose(&display.screen.display_state(), &mut resident), + Err(ResidentError::InvalidState("resident scanout bindings belong to an old or different epoch")) + )); + assert_eq!(scan.traffic(), before_reset); + let mut reset = Ng1Scanout::new_resident(&gpu, &resident).expect("bind reset epoch"); + let reset_frame = compare_frame( + &mut reset, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux, + ); + assert_eq!(display.screen.did[3 * FB_STRIDE], 6); + assert!(reset.traffic().display.submitted_bytes < 128 * 1024); + assert_no_plane_traffic(reset.traffic()); + + resident.restore(&rgb[..WIDTH], &[]).expect("restore resident prefix"); + assert!(matches!( + reset.compose(&display.screen.display_state(), &mut resident), + Err(ResidentError::InvalidState("resident scanout bindings belong to an old or different epoch")) + )); + let mut restored = Ng1Scanout::new_resident(&gpu, &resident).expect("bind restored epoch"); + let restored_frame = compare_frame( + &mut restored, &mut resident, &mut display, &mut legacy, &mut sw, &mut staging, &rgb, &aux, + ); + assert_pixels_eq("restored frame", &restored_frame, &reset_frame); + assert_no_plane_traffic(restored.traffic()); + }) + .expect("spawn DID expansion test") + .join() + .unwrap_or_else(|panic| std::panic::resume_unwind(panic)); +} + struct TestBoard(Box); impl Drop for TestBoard { @@ -633,8 +1113,10 @@ fn draw_reference(mut context: Rex3Context, rgb: &mut [u32], aux: &mut [u32]) { fn software_frame(rex: &Rex3, rgb: &[u32], aux: &[u32]) -> Vec { let mut screen = rex.screen.lock(); + let did = screen.decoded_did_oracle(); let state = screen.display_state(); - let src = cpu_source(&state, rgb, aux); + let mut src = cpu_source(&state, rgb, aux); + src.did = &did; let mut sw = SwCompositor::new(); sw.compose_pixels(&src); let mut pixels = Vec::with_capacity(state.width * state.height); @@ -644,6 +1126,17 @@ fn software_frame(rex: &Rex3, rgb: &[u32], aux: &[u32]) -> Vec { pixels } +fn software_display_frame(display: &mut Display, rgb: &[u32], aux: &[u32]) -> Vec { + let state = display.screen.display_state(); + let mut sw = SwCompositor::new(); + sw.compose_pixels(&cpu_source(&state, rgb, aux)); + let mut pixels = Vec::with_capacity(state.width * state.height); + for y in 0..state.height { + pixels.extend_from_slice(&sw.pixels()[y * FB_STRIDE..y * FB_STRIDE + state.width]); + } + pixels +} + struct QueuePresenter { gpu: Option>, accepts: bool, @@ -1064,6 +1557,30 @@ fn resident_frame_gpu_headless_requests_and_vnc_capture() { let mut screenshot = Vec::new(); assert!(crate::gfx_display::copy_frame(&*board.0, &mut screenshot).is_some()); assert_pixels_eq("headless screenshot", &screenshot, &software_frame(&board.0, &rgb, &aux)); + + let mut oracle = Display::new(); + oracle.screen.topscan = 1023; + for (did, line) in [(6, DID_LINE as u16), (2, 0xffff)] { + for vc2 in [&board.0.vc2, &oracle.vc2] { + let mut vc2 = vc2.lock(); + vc2.ram[DID_LINE] = did; + vc2.ram[DID_LINE + 1] = 0xffe0; + vc2.ram[0x300 + 3] = line; + vc2.dirty = true; + } + oracle.refresh(); + board.0.request_frame(); + headless.render(&board.0, Duration::from_secs(10), |_| {}).unwrap(); + assert!(crate::gfx_display::copy_frame(&*board.0, &mut screenshot).is_some()); + assert_pixels_eq( + "headless DID history", + &screenshot, + &software_display_frame(&mut oracle, &rgb, &aux), + ); + } + assert_eq!(oracle.screen.did[3 * FB_STRIDE], 6); + assert!(board.0.screen.lock().did.iter().all(|&did| did == 0)); + let expected = software_display_frame(&mut oracle, &rgb, &aux); drop(headless); board.0.start_resident_display(Arc::clone(&gpu)).unwrap(); @@ -1072,7 +1589,7 @@ fn resident_frame_gpu_headless_requests_and_vnc_capture() { &*board.0, &mut captured, Duration::from_secs(10), ).expect("headless composed frame"); assert_eq!((width, height), (WIDTH, HEIGHT)); - assert_pixels_eq("headless composed frame", &captured, &software_frame(&board.0, &rgb, &aux)); + assert_pixels_eq("headless composed frame", &captured, &expected); let initial = captured.clone(); let cursor_y = { let mut vc2 = board.0.vc2.lock(); @@ -1092,7 +1609,13 @@ fn resident_frame_gpu_headless_requests_and_vnc_capture() { ).expect("headless cursor refresh"); assert!(updated_seq > seq); assert!(captured != initial, "vblank cursor changes must reach the composed frame"); - assert_pixels_eq("headless cursor update", &captured, &software_frame(&board.0, &rgb, &aux)); + { + let mut vc2 = oracle.vc2.lock(); + vc2.regs[VC2_REG_WORKING_CURSOR_Y as usize] = cursor_y; + vc2.dirty = true; + } + oracle.refresh(); + assert_pixels_eq("headless cursor update", &captured, &software_display_frame(&mut oracle, &rgb, &aux)); let seq = updated_seq; std::thread::sleep(Duration::from_millis(100)); assert_eq!(board.0.screen.lock().frame_seq, seq, diff --git a/src/dev/ng1/wgpu/resident.rs b/src/dev/ng1/wgpu/resident.rs index 065d97f20..2b687c2f5 100644 --- a/src/dev/ng1/wgpu/resident.rs +++ b/src/dev/ng1/wgpu/resident.rs @@ -54,7 +54,7 @@ impl std::fmt::Display for ResidentError { Self::Limit { name, required, available } => { write!(f, "resident GPU limit {name} is {available}, requires {required}") } - Self::DeviceMismatch => write!(f, "resident planes and scanout must use the same GPU"), + Self::DeviceMismatch => write!(f, "resident raster, display and presenter must use the same GPU"), Self::Timeout(reason) => write!(f, "resident frame timed out waiting for {reason}"), Self::DeviceLost(reason) => write!(f, "resident GPU device lost: {reason}"), Self::Failed(reason) => write!(f, "resident raster failed: {reason}"), diff --git a/src/dev/ng1/wgpu/shaders/did_expand.wgsl b/src/dev/ng1/wgpu/shaders/did_expand.wgsl new file mode 100644 index 000000000..dfb4f3f4f --- /dev/null +++ b/src/dev/ng1/wgpu/shaders/did_expand.wgsl @@ -0,0 +1,113 @@ +struct DidParams { + did_ptr: u32, + scan_len: u32, + width: u32, + height: u32, + enabled: u32, + display_on: u32, + pad0: u32, + pad1: u32, +} + +@group(0) @binding(0) var vc2_ram: array; +@group(0) @binding(1) var did_map: array; +@group(0) @binding(2) var params: DidParams; + +fn vc2_word(address: u32) -> u32 { + let word = vc2_ram[address >> 1u]; + return select(word & 0xffffu, word >> 16u, (address & 1u) != 0u); +} + +fn set_did(index: u32, value: u32) { + let word = index >> 2u; + let shift = 8u * (index & 3u); + let mask = 0xffu << shift; + did_map[word] = (did_map[word] & ~mask) | ((value & 0x1fu) << shift); +} + +fn fill_run(y: u32, start: u32, end: u32, value: u32) { + var x = start; + while x < end { + set_did(y * 2048u + x, value); + x += 1u; + } +} + +@compute @workgroup_size(64) +fn expand_did(@builtin(global_invocation_id) id: vec3) { + let y = id.x; + if y >= 1024u { + return; + } + // The CPU decoder also preserves DID while the display is inactive. + if params.display_on == 0u { + return; + } + if params.enabled == 0u { + fill_run(y, 0u, 2048u, 0u); + return; + } + if y >= params.height { + return; + } + + let ram_words = arrayLength(&vc2_ram) * 2u; + var prior = 0u; + while prior < y { + let prior_table = params.did_ptr + prior; + if prior_table >= ram_words || vc2_word(prior_table) == 0xffffu { + return; + } + prior += 1u; + } + + let table = params.did_ptr + y; + if table >= ram_words { + return; + } + let line_ptr = vc2_word(table); + if line_ptr == 0xffffu { + return; + } + + let effective_len = select( + params.width, + params.scan_len, + params.scan_len > 0u && params.scan_len <= 2048u, + ); + if line_ptr >= ram_words { + fill_run(y, 0u, effective_len, 0u); + return; + } + + var ptr = line_ptr; + let first = vc2_word(ptr); + ptr += 1u; + var current_did = first & 0x1fu; + var current_x = 0u; + + loop { + if current_x >= effective_len { + break; + } + if ptr >= ram_words { + fill_run(y, current_x, effective_len, current_did); + break; + } + let next = vc2_word(ptr); + ptr += 1u; + let next_x_raw = (next >> 5u) & 0x7ffu; + let next_did = next & 0x1fu; + let eol = next_x_raw == 0x7ffu; + let next_x = select(next_x_raw, effective_len, eol); + let run_end = min(max(next_x, current_x), effective_len); + if run_end > current_x { + fill_run(y, current_x, run_end, current_did); + } + current_x = run_end; + current_did = next_did; + if eol { + break; + } + } +} diff --git a/src/disp.rs b/src/disp.rs index c4643cd6a..c2fa4b4f9 100644 --- a/src/disp.rs +++ b/src/disp.rs @@ -64,6 +64,7 @@ impl Ng1DisplayGenerations { pub(crate) struct Ng1DisplaySource<'a> { pub(crate) generations: Ng1DisplayGenerations, pub(crate) did: &'a [u8], + pub(crate) resident_did: &'a Mutex>, pub(crate) xmap_mode: &'a [u32; 32], pub(crate) xmap_config: u8, pub(crate) xmap_cursor_cmap: u8, @@ -143,6 +144,8 @@ pub struct Rex3Screen { display_generations: Ng1DisplayGenerations, #[cfg(all(test, feature = "wgpu"))] display_geometry: (usize, usize, usize, i32), + #[cfg(all(test, feature = "wgpu"))] + resident_did: Mutex>, } impl Rex3Screen { @@ -181,10 +184,18 @@ impl Rex3Screen { display_generations: Ng1DisplayGenerations::new(), #[cfg(all(test, feature = "wgpu"))] display_geometry: (0, 0, 0, 0), + #[cfg(all(test, feature = "wgpu"))] + resident_did: Mutex::new(None), } } - fn decode_did(&mut self) -> bool { + fn decode_did_into( + vc2_regs: &[u16; 32], + vc2_ram: &[u16], + width: usize, + height: usize, + did_buf: &mut [u8], + ) -> bool { fn fill(dst: &mut [u8], value: u8) -> bool { #[cfg(all(test, feature = "wgpu"))] let changed = dst.iter().any(|&old| old != value); @@ -194,18 +205,15 @@ impl Rex3Screen { changed } - let display_ctrl = self.vc2_regs[VC2_REG_DISPLAY_CONTROL as usize]; + let display_ctrl = vc2_regs[VC2_REG_DISPLAY_CONTROL as usize]; if (display_ctrl & VC2_CTRL_DID_EN) == 0 { - return fill(&mut self.did, 0); + return fill(did_buf, 0); } let mut changed = false; - let did_ptr = self.vc2_regs[VC2_REG_DID_ENTRY_PTR as usize]; - let scan_len = (self.vc2_regs[VC2_REG_SCANLINE_LEN as usize] >> 5) as usize; - let width = self.width; - let height = self.height; - let ram = &self.vc2_ram; - let did_buf = &mut self.did; + let did_ptr = vc2_regs[VC2_REG_DID_ENTRY_PTR as usize]; + let scan_len = (vc2_regs[VC2_REG_SCANLINE_LEN as usize] >> 5) as usize; + let ram = vc2_ram; let effective_len = if scan_len > 0 && scan_len <= 2048 { scan_len } else { width }; @@ -258,6 +266,29 @@ impl Rex3Screen { changed } + fn decode_did(&mut self) -> bool { + Self::decode_did_into( + &self.vc2_regs, + &self.vc2_ram, + self.width, + self.height, + &mut self.did, + ) + } + + #[cfg(all(test, feature = "wgpu"))] + pub(crate) fn decoded_did_oracle(&self) -> Vec { + let mut did = self.did.clone(); + Self::decode_did_into( + &self.vc2_regs, + &self.vc2_ram, + self.width, + self.height, + &mut did, + ); + did + } + // Returns (width, height, cursor_x_adjust). fn decode_video_timings(&self) -> (usize, usize, i32) { decode_vc2_timings(&self.vc2_regs, &self.vc2_ram) @@ -331,13 +362,14 @@ impl Rex3Screen { /// Snapshot Newport display controls and tables without accessing either /// CPU framebuffer. Returns `true` if the display resolution changed. - pub(crate) fn refresh_display_state( + fn refresh_display_state_inner( &mut self, vc2: &Mutex, xmap: &Mutex, cmap: &Mutex, bt445: &Mutex, diag: &AtomicU64, + decode_did: bool, ) -> bool { let mut resized = false; @@ -434,19 +466,44 @@ impl Rex3Screen { resized = true; } - diag.fetch_or(Rex3::DIAG_LOOP_DECODE_DID, Ordering::Relaxed); - let did_changed = self.decode_did(); - #[cfg(all(test, feature = "wgpu"))] - if did_changed { - Ng1DisplayGenerations::bump(&mut self.display_generations.did); + if decode_did { + diag.fetch_or(Rex3::DIAG_LOOP_DECODE_DID, Ordering::Relaxed); + let did_changed = self.decode_did(); + #[cfg(all(test, feature = "wgpu"))] + if did_changed { + Ng1DisplayGenerations::bump(&mut self.display_generations.did); + } + #[cfg(not(all(test, feature = "wgpu")))] + let _ = did_changed; + diag.fetch_and(!Rex3::DIAG_LOOP_DECODE_DID, Ordering::Relaxed); } - #[cfg(not(all(test, feature = "wgpu")))] - let _ = did_changed; - diag.fetch_and(!Rex3::DIAG_LOOP_DECODE_DID, Ordering::Relaxed); resized } + pub(crate) fn refresh_display_state( + &mut self, + vc2: &Mutex, + xmap: &Mutex, + cmap: &Mutex, + bt445: &Mutex, + diag: &AtomicU64, + ) -> bool { + self.refresh_display_state_inner(vc2, xmap, cmap, bt445, diag, true) + } + + #[cfg(all(test, feature = "wgpu"))] + pub(crate) fn refresh_resident_display_state( + &mut self, + vc2: &Mutex, + xmap: &Mutex, + cmap: &Mutex, + bt445: &Mutex, + diag: &AtomicU64, + ) -> bool { + self.refresh_display_state_inner(vc2, xmap, cmap, bt445, diag, false) + } + /// Borrow only display state. Geometry includes externally supplied /// topscan as well as the decoded dimensions and cursor adjustment. #[cfg(all(test, feature = "wgpu"))] @@ -459,6 +516,7 @@ impl Rex3Screen { Ng1DisplaySource { generations: self.display_generations, did: &self.did, + resident_did: &self.resident_did, xmap_mode: &self.xmap_mode, xmap_config: self.xmap_config, xmap_cursor_cmap: self.xmap_cursor_cmap,