perf(libc): copy strided guest rows out with batched checked writes #20832

Merged
milescoviello merged 1 commit from upstream-pr/20832 into main 2026-11-15 21:49:57 +00:00
milescoviello commented 2026-11-15 16:26:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T16:26:19Z.


What

libSceVideodec's CopyPicture copies each decoded picture into the guest frame buffer with one GuestCopyOut_nid_no_patch per row: height luma rows plus height / 2 chroma rows. Each call is a process_vm_writev system call on Linux, so a 1080p frame costs 1,620 system calls, and it runs on every decoded frame.

This adds GuestCopyOutRows_nid_no_patch(destination, destinationPitch, source, sourcePitch, rowBytes, rows) to libc's checked-copy helpers. CopyPicture uses it for both planes.

How it works on each platform

  • Linux: each process_vm_writev call carries up to 1,024 rows, one local and one remote iovec per row. A 1080p frame takes two calls.
    • A fault returns false with errno preserved, as GuestCopyOut_nid_no_patch does. Rows before the fault are written, as the per-row loop wrote them.
    • Any other error throws, matching GuestCopyOut_nid_no_patch.
  • Windows: the rows are checked with VirtualQuery, reusing each queried region across rows, and then copied with memcpy. A failure is reported before anything is written. The per-row loop could leave earlier rows written; this cannot.
  • Other hosts: keeps the per-row GuestCopyOut_nid_no_patch loop.

Unchanged behaviour

  • Only rowBytes of each row is written. Padding between rows stays untouched, as before.
  • Memory in the gaps between rows need not be mapped. A destination pitch that steps over an unmapped page still works.
  • The write-watch HostWrite guard covers the span from the first row to the end of the last.
  • CopyPicture returns the same VideodecErrorFrameBufferPointer for an unwritable buffer, so the export's results don't change.

glReadPixels's clipped-row path (#20807) could use the same helper later. That's a follow-up, not part of this PR. libSceVideodec2's frame output is covered separately by #20614, which stages the picture and copies it out in one piece.

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • New guest_copy_out_rows test:

    • Strided rows with different source and destination pitches; padding bytes keep their fill value.
    • 1,500 rows, to cross the 1,024-row batch boundary.
    • Zero rows and zero-byte rows succeed without writing.
    • Two rows on mapped pages with an unmapped page between them succeed. A second row on a PROT_NONE page fails with errno left at 71.
    • Unmapped (0x10), null and overflowing destinations fail.
  • guest_videodec (decode through CopyPicture, plus the argument and frame-buffer checks) still passes.

  • Microbenchmark (i7-13700K pinned to one core, best of 5 × 50 copies, GuestCopyOut_nid_no_patch per row against GuestCopyOutRows_nid_no_patch):

    picture rows per-row batched
    1920×1080, pitch 2048 1,620 671 µs 260 µs
    1280×720, pitch 1280 1,080 419 µs 161 µs
  • Merge checks:

    • git merge-tree against main is clean.
    • Against the open PRs touching General.cpp/General.hpp or libSceVideodec/Export.cpp (#20315, #19451, #18112, #15973, #11446, #9371, #20678), the conflict count is the same as each already has with main.
  • tools/check_conventions.py passes on the branch and this body.

  • Windows 11 VM (GT 1030, WinLibs GCC 15.2 UCRT, Release): the head and its merge base were built with guest_copy_out_rows_tests and guest_videodec_tests. guest_copy_out_rows and guest_videodec pass 3 of 3 on the head, which exercises the VirtualQuery branch. guest_videodec also passes 3 of 3 on the base.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5324721825 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20832). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T16:26:19Z. --- ### What `libSceVideodec`'s `CopyPicture` copies each decoded picture into the guest frame buffer with one `GuestCopyOut_nid_no_patch` per row: `height` luma rows plus `height / 2` chroma rows. Each call is a `process_vm_writev` system call on Linux, so a 1080p frame costs 1,620 system calls, and it runs on every decoded frame. This adds `GuestCopyOutRows_nid_no_patch(destination, destinationPitch, source, sourcePitch, rowBytes, rows)` to libc's checked-copy helpers. `CopyPicture` uses it for both planes. **How it works on each platform** - **Linux:** each `process_vm_writev` call carries up to 1,024 rows, one local and one remote `iovec` per row. A 1080p frame takes two calls. - A fault returns false with `errno` preserved, as `GuestCopyOut_nid_no_patch` does. Rows before the fault are written, as the per-row loop wrote them. - Any other error throws, matching `GuestCopyOut_nid_no_patch`. - **Windows:** the rows are checked with `VirtualQuery`, reusing each queried region across rows, and then copied with `memcpy`. A failure is reported before anything is written. The per-row loop could leave earlier rows written; this cannot. - **Other hosts:** keeps the per-row `GuestCopyOut_nid_no_patch` loop. **Unchanged behaviour** - Only `rowBytes` of each row is written. Padding between rows stays untouched, as before. - Memory in the gaps between rows need not be mapped. A destination pitch that steps over an unmapped page still works. - The write-watch `HostWrite` guard covers the span from the first row to the end of the last. - `CopyPicture` returns the same `VideodecErrorFrameBufferPointer` for an unwritable buffer, so the export's results don't change. `glReadPixels`'s clipped-row path (#20807) could use the same helper later. That's a follow-up, not part of this PR. `libSceVideodec2`'s frame output is covered separately by #20614, which stages the picture and copies it out in one piece. ### Tested Linux (Ubuntu 26.04.1), GCC 15.2, Release. - New `guest_copy_out_rows` test: - Strided rows with different source and destination pitches; padding bytes keep their fill value. - 1,500 rows, to cross the 1,024-row batch boundary. - Zero rows and zero-byte rows succeed without writing. - Two rows on mapped pages with an unmapped page between them succeed. A second row on a `PROT_NONE` page fails with `errno` left at 71. - Unmapped (`0x10`), null and overflowing destinations fail. - `guest_videodec` (decode through `CopyPicture`, plus the argument and frame-buffer checks) still passes. - Microbenchmark (i7-13700K pinned to one core, best of 5 × 50 copies, `GuestCopyOut_nid_no_patch` per row against `GuestCopyOutRows_nid_no_patch`): | picture | rows | per-row | batched | |---|---|---|---| | 1920×1080, pitch 2048 | 1,620 | 671 µs | 260 µs | | 1280×720, pitch 1280 | 1,080 | 419 µs | 161 µs | - Merge checks: - `git merge-tree` against `main` is clean. - Against the open PRs touching `General.cpp`/`General.hpp` or `libSceVideodec/Export.cpp` (#20315, #19451, #18112, #15973, #11446, #9371, #20678), the conflict count is the same as each already has with `main`. - `tools/check_conventions.py` passes on the branch and this body. - Windows 11 VM (GT 1030, WinLibs GCC 15.2 UCRT, Release): the head and its merge base were built with `guest_copy_out_rows_tests` and `guest_videodec_tests`. `guest_copy_out_rows` and `guest_videodec` pass 3 of 3 on the head, which exercises the `VirtualQuery` branch. `guest_videodec` also passes 3 of 3 on the base. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!20832
No description provided.