Skip to content

feat(agc): implement PM4 CONTEXT_REG_RMW - #16878

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
AlexWhitehouse:feat/agc-context-reg-rmw
Nov 14, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
AlexWhitehouse:feat/agc-context-reg-rmw

Conversation

@AlexWhitehouse

@AlexWhitehouse AlexWhitehouse commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What

The PM4 packet CONTEXT_REG_RMW (type-3 opcode 0x51) wasn't in the AGC driver's reference catalogue, so a command buffer containing it failed validation with opcode is not known in the reference. This PR implements it from PAL at c5e80007, the revision my #16184 (COND_WRITE) and #16345 (DMA_DATA) cite:

The packet has four dwords: the header, the context-register offset (16 bits, upper 16 reserved), a mask and data. Its effect is new = (old & ~mask) | (data & mask). PAL emits it for colour and depth state in gfx9UniversalCmdBuffer.cpp.

  • Validation: the packet is accepted only on graphics queues, as SET_CONTEXT_REG is. It must be exactly four dwords, and set reserved offset bits are rejected.
  • Execution:
    • it reads the old value from the modelled context-register state;
    • it writes the result through the same path SET_CONTEXT_REG uses, so later reads, COPY_DATA from the register and the register fingerprint all see it;
    • a register with no known value (never written, and no reset value in the model) throws instead of assuming zero.
  • Catalogue: the reference opcode catalogue grows from 56 to 57.
  • Debt: a CONTEXT_REG_RMW entry in docs/dev/debt/libSceAgcDriver.md records the throw for a register with no known value. Of the registers PAL modifies with this packet, CB_COLORn_DCC_CONTROL, DB_COUNT_CONTROL and DB_RESERVED_REG_2 have no reset value in the model. Console behaviour is unmeasured, as for the other PM4 packets.

Tested

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
  • 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: NVIDIA GeForce RTX 3060 (driver 610.88, Windows 11 26300) and lavapipe (Mesa, Ubuntu 26.04); the PM4 tests are CPU-side and create no Vulkan device
  • AI-assisted: yes

🤖 Generated with Claude Code

Decode opcode 0x51 using the four-dword layout and masked update specified by PAL c5e800072a32f68b6ccc4422936d96167c6e0728 BuildContextRegRmw and PM4_ME_CONTEXT_REG_RMW. Reuse context-register writes and reject unknown prior values.

Cover masks, readback, fingerprints, reset values, packet sizes and queue restrictions in a registered focused test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added build agc libSceAgc* labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 19191 core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp L280-281

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4448 docs/dev/debt/libSceAgcDriver.md
PR 5925 docs/dev/debt/libSceAgcDriver.md
PR 6186 core/libs/prx/libSceAgcDriver/tests/Pm4.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6188 core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
PR 6280 docs/dev/debt/libSceAgcDriver.md
PR 6412 docs/dev/debt/libSceAgcDriver.md
PR 6481 docs/dev/debt/libSceAgcDriver.md
PR 6804 docs/dev/debt/libSceAgcDriver.md
PR 6808 docs/dev/debt/libSceAgcDriver.md
PR 7085 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
docs/dev/debt/libSceAgcDriver.md
PR 7256 docs/dev/debt/libSceAgcDriver.md
PR 7449 docs/dev/debt/libSceAgcDriver.md
PR 7642 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
docs/dev/debt/libSceAgcDriver.md
PR 8592 docs/dev/debt/libSceAgcDriver.md
PR 9335 docs/dev/debt/libSceAgcDriver.md
PR 9356 docs/dev/debt/libSceAgcDriver.md
PR 9387 docs/dev/debt/libSceAgcDriver.md
PR 9508 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
docs/dev/debt/libSceAgcDriver.md
PR 9895 docs/dev/debt/libSceAgcDriver.md
PR 9897 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
PR 9950 docs/dev/debt/libSceAgcDriver.md
PR 10049 docs/dev/debt/libSceAgcDriver.md
PR 10193 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
docs/dev/debt/libSceAgcDriver.md
PR 10249 core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
PR 10351 docs/dev/debt/libSceAgcDriver.md
PR 10359 docs/dev/debt/libSceAgcDriver.md
PR 11551 docs/dev/debt/libSceAgcDriver.md
PR 11825 docs/dev/debt/libSceAgcDriver.md
PR 12840 docs/dev/debt/libSceAgcDriver.md
PR 12904 docs/dev/debt/libSceAgcDriver.md
PR 12944 core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
core/libs/prx/libSceAgcDriver/tests/Pm4.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
PR 13809 docs/dev/debt/libSceAgcDriver.md
PR 13863 docs/dev/debt/libSceAgcDriver.md
PR 13920 docs/dev/debt/libSceAgcDriver.md
PR 13928 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
docs/dev/debt/libSceAgcDriver.md
PR 13929 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
docs/dev/debt/libSceAgcDriver.md
PR 14032 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
PR 14944 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
docs/dev/debt/libSceAgcDriver.md
PR 15136 core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
docs/dev/debt/libSceAgcDriver.md
PR 15456 docs/dev/debt/libSceAgcDriver.md
PR 15529 docs/dev/debt/libSceAgcDriver.md
PR 15548 docs/dev/debt/libSceAgcDriver.md
PR 15621 docs/dev/debt/libSceAgcDriver.md
PR 16054 core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
core/libs/prx/libSceAgcDriver/tests/Pm4.cpp
docs/dev/debt/libSceAgcDriver.md
PR 16413 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
PR 16753 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
PR 17024 docs/dev/debt/libSceAgcDriver.md
PR 17056 docs/dev/debt/libSceAgcDriver.md
PR 17611 docs/dev/debt/libSceAgcDriver.md
PR 17796 docs/dev/debt/libSceAgcDriver.md
PR 17899 docs/dev/debt/libSceAgcDriver.md
PR 17943 docs/dev/debt/libSceAgcDriver.md
PR 18029 docs/dev/debt/libSceAgcDriver.md
PR 18082 docs/dev/debt/libSceAgcDriver.md
PR 18101 docs/dev/debt/libSceAgcDriver.md
PR 18139 docs/dev/debt/libSceAgcDriver.md
PR 18390 docs/dev/debt/libSceAgcDriver.md
PR 18515 docs/dev/debt/libSceAgcDriver.md
PR 18914 core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
core/libs/prx/libSceAgcDriver/tests/Pm4.cpp
PR 19119 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
PR 19373 docs/dev/debt/libSceAgcDriver.md

@gabichuuuuu

Copy link
Copy Markdown
Contributor

Ran 6cec611cd0 merged on main 302864d on Windows 11 x64 (WinLibs GCC 15.2), with an NVIDIA laptop GPU (Turing, driver 621) and an Intel iGPU (driver 101.70xx) selected, ctest -R '^agc_driver_pm4' -V (17 tests).

NVIDIA Intel
change 17/17, 3 runs 17/17, 2 runs
main + only this PR's tests/Pm4.cpp diff and its two test registrations 15/17 15/17
  • Without the Pm4.cpp/Pm4Opcodes.hpp change, agc_driver_pm4 fails with "reference opcode catalog is incomplete" and the new agc_driver_pm4_context_reg_rmw with "opcode is not known in the reference". With it, both pass every time.
  • None of these 17 tests opened a Vulkan device (the validation layer, set in the environment, was never inserted), so this covers the decoder and not a submission on real hardware.

AI-assisted: yes.

@Jirka135

Copy link
Copy Markdown
Contributor

Reviewed PM4 CONTEXT_REG_RMW — no blocking findings

The decoding (offset, mask, data) and the read-modify-write order are correct and go through the same register path as SET_CONTEXT_REG; I found nothing that must change, only two questions.

What I ran

Head reviewed: 6cec611cd0ef8b9f41b4e8bbfc8d090b04caf320 (base main). I read the description, the single commit, the full diff, the thread (gabichuuuuu's Windows run), and at head Pm4.cpp (registerOffset, writeRegister, Validate, Execute for 0x69/0x40), QueueState.hpp (BuildInitialContextRegisters) and PacketExecution.cpp.

No build or test was run here; this review is from reading the code only. The test results in the description are the author's.

Results

Check Result Note
new = (old & ~mask) | (data & mask) vs packet[2]=mask, packet[3]=data passed Pm4.cpp:1204-1210; hand-checked 0x125556aa in tests/Pm4.cpp:180-181
Write path equals SET_CONTEXT_REG passed both go through writeRegister (Pm4.cpp:132-135): RMW passes 0x69 (1208), SET_CONTEXT_REG passes opcode = 0x69 (1214), so the incremental fingerprint is updated; the execute helper checks this (tests/Pm4.cpp:75)
Offset bounds passed Validate rejects any bit above 15 (Pm4.cpp:350-354), stricter than registerOffset (Pm4.cpp:94-103); Execute then needs context.contains(offset)
Graphics-only, size 4 passed graphics(); size(4); tests at tests/Pm4.cpp:193-197
Tests fail without the fix passed (by reasoning; gabichuuuuu also reports it) the catalog count and unknown opcode would fail
Mutation claim (drop & mask on data) passed (by reasoning) the cases with data bits outside the mask (tests/Pm4.cpp:184-185, 189-190) catch it; the first case (180-181) does not, since its data lies inside the mask

Findings

Blocking

None.

Non-blocking

  1. 0x51 is not in keepsDrawFinisher / keepsDrawPipeline (PacketExecution.cpp:59, 77) or in the false list of NeedsRecordedLabels (DeferredLabels.cpp:45-46), where 0x69 is. While draw work is in flight an RMW therefore drains the DrawFinisher (PacketExecution.cpp:294) and the DrawPipeline (:338) instead of passing beside them, and NeedsRecordedLabels returns true for it, so it sets needsRecord while labels are queued (PacketFlush.cpp:34). This looks functionally safe, only slower; it would matter if titles emit it between draws, which is my inference (the description only says PAL emits it for colour and depth state). Was leaving it out deliberate?
  2. Execute throws when the register is not in the model (Pm4.cpp:1206), and Validate cannot catch it, so such a packet fails at execution. The reset set (QueueState.hpp:220-252) is sparse. I cannot check whether the registers PAL RMWs (gfx9UniversalCmdBuffer.cpp) are all in it. If a title RMWs one that is not, the packet throws at execution. The description states the throw as intended, so a short note on which registers PAL targets would help.

Not verified

  • The PAL packet layout (16-bit offset, reserved upper bits) from the two linked PAL sources; I could not open them.
  • Console behaviour for an RMW on a never-written register.
  • Any GPU or Vulkan run; the PM4 tests create no device. gabichuuuuu's run covered NVIDIA and Intel hosts; no AMD host run is in the thread.

To get this to a good state

Nothing is required. Optionally answer question 1 (or add 0x51 to the keep lists) and 2.

AI-assisted: yes (code read with Claude Code; no build or test run).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlexWhitehouse

Copy link
Copy Markdown
Contributor Author

Thanks for the review.

  1. Leaving 0x51 out of keepsDrawFinisher, keepsDrawPipeline and the NeedsRecordedLabels false list is intended for this PR. Those lists only let a packet run beside in-flight draw work; an opcode not on them drains first, which is correct and only slower. Adding it is a speed change, which needs a trace_digest.py comparison from a title that emits the packet between draws, and I don't have one. It can follow in its own PR if a trace shows it matters.

  2. PAL c5e80007 uses WriteContextRegRmw on CB_COLORn_INFO (gfx9UniversalCmdBuffer.cpp 4756, 4763, 7873, 7881), DB_RENDER_OVERRIDE (7940 and gfx9DepthStencilView.cpp 832), DB_Z_INFO (8023), DB_COUNT_CONTROL (8939), CB_COLORn_DCC_CONTROL (gfx9WorkaroundState.cpp 182) and DB_RESERVED_REG_2 (gfx9MsaaState.cpp 530). The first three have a value in BuildInitialContextRegisters. CB_COLORn_DCC_CONTROL (0x31e + 0xf * n), DB_COUNT_CONTROL (0x001) and DB_RESERVED_REG_2 (0x00f) do not, so an RMW of one of them throws until the queue writes it. I recorded that in a debt entry in 8778d83 and updated the Debt line in the description. The code is unchanged, and the console reset values of those registers are still unmeasured.

@adriadam10
adriadam10 merged commit f9854b5 into boykopovar:main Nov 14, 2026
13 of 15 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the RMW formula and mask/data order, the write path shared with SET_CONTEXT_REG, validation (graphics only, size 4, reserved offset bits), the throw for registers without a value and its debt entry, and that each new test case fails without the change; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants