fix(shader): read the tenth opcode bit of PS4 VOP3 instructions #25598

Merged
adam25136 merged 1 commit from upstream-pr/25598 into main 2026-11-22 21:31:03 +00:00
adam25136 commented 2026-11-22 11:03:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adam25136 on 2026-11-22T11:03:24Z.


What

A PS4 (GCN) VOP3 instruction's opcode is read from bits 25:17 of its first word, nine bits. PS4 Pro shaders also use VOP3 opcodes from 512 up, whose tenth bit is bit 16: shadPS4 reads it for every VOP3 instruction (op_msb = bit::extract(hexInstruction, 16, 16); op = op + op_msb * (1 << 9); in decodeInstructionVOP3, decode.cpp L736-737; its opcode list ends the range with "in neo mode, VOP3 gets an additional bit for OP so round up", opcodes.h L700-L719). GcnOpcodeTables.hpp already maps 16 of those opcodes (0x303 to 0x372, for example v_lshl_add_u32 0x346 and v_add3_u32 0x36d) to the RDNA opcodes of the same numbers, but without the bit no instruction reaches them: each decodes as the opcode 512 below, or is refused where that one is unmapped (0x30d, 0x30e). v_add3_u32 (877) becomes v_div_scale_f32 (365), a VOP3b, and that word's bits 14:8, which hold the ABS, CLAMP and reserved bits of a VOP3a, are taken as its scalar destination.

No public AMD document covers this encoding: the Sea Islands ISA has no VOP3 opcodes from 512 up, the bit comes from shadPS4, the opcode numbers are the ones RDNA 2 gives the same instructions (all 16 are at these numbers in AMD's "RDNA 2" Instruction Set Architecture reference guide, section 12.12 VOP3A & VOP3B Instructions, and RdnaVectorOpDecoder.cpp decodes them), and it is not measured on a PS4 Pro. inFAMOUS's shader supports it two ways. None of the 14 words that read as v_div_scale_f32 with nine bits meets that instruction's rule in the Sea Islands ISA, "s0 must equal s1 or s2" (its V_DIV_SCALE_F32 entry), while as v_add3_u32 they do address arithmetic on the thread and group IDs (for example v6 = v1 + s15 - 1 at pc 0x11c: thread y plus group y × 8, minus 1). And of the 28 compute shaders the driver saved from inFAMOUS (5,125 VOP3 words), only this shader's 19 words set bit 16, each an opcode the table maps, and no word sets any other reserved bit.

  • The fix: encodeVop3 reads the opcode from bits 25:17 and bit 16 (a vop3Opcode helper). In the Sea Islands ISA bit 16 is reserved in both VOP3 forms (VOP3a "reserved [16:12]", VOP3b "[16:15] Reserved", AMD Sea Islands Series Instruction Set Architecture, chapter 13, section 13.3), so instructions that leave it clear decode as before.
  • OP_SEL: shadPS4 reads OP_SEL from bits 15:12 of these words (InstControlVOP3 in instruction.h L132-L140). The re-encoding to RDNA does not carry those bits, and ten of the sixteen opcodes are 16-bit operations whose halves OP_SEL picks, so a mapped ten-bit opcode with any of them set now throws: dropping them would run the 16-bit ones on the wrong halves. inFAMOUS's shader has 19 ten-bit-opcode words (v_lshl_add_u32 once, v_add3_u32 14 times, v_and_or_b32 4 times), none with them set.
  • What it does not do: it adds no opcodes. A ten-bit opcode the table does not map is refused (unsupported VOP3), as an unmapped nine-bit one is; the two opcodes in shadPS4's list that the table leaves out, 784 (v_sub_co_u32) and 833 (v_mad_f16), decoded on main as v_max_f32 and v_mad_f32 (their low nine bits, 0x110 and 0x141) and are now refused.
  • Technical debt: a fragment adds an Unknown function info entry, GCN VOP3 opcodes from 512 up: read the shadPS4 way, not measured on a PS4 Pro.

Tested

Windows 10 Pro 22H2 (19045), NVIDIA GeForce RTX 5080 (driver 621.14), WinLibs GCC 15.2.0 (r7), Release. The selected tests, the validation-layer run and the inFAMOUS starts ran on this change on main 6c70b74203; the decode test, the breaks and the replay on it before a rebase, on dac08f50bc; it is now on 7eb79b0c48. None of the main commits in between change core/shader or libSceAgcDriver, and its changed lines are the same.

  • agc_gcn_decode (new Vop3OpcodeTakesBit16): the word inFAMOUS uses, 0xd2db0011 0x04104100, decodes as v_add3_u32 v17, v0, s32, v4 with no scalar destination; the same word with bit 16 clear still decodes as v_div_scale_f32 with s[0:1] as its scalar destination, and with bit 12 set too, which in that word makes the scalar destination s[16:17], it still decodes with that destination, so the OP_SEL refusal leaves nine-bit opcodes alone; each of the 16 ten-bit opcodes the table maps decodes as the RDNA opcode of the same number; a word with bit 16 set over v_rsq_clamp_f32's low bits (0x3ac, unmapped) is refused rather than read as v_rsq_clamp_f32; and v_add_nc_u16 (0x303) with any one OP_SEL bit set is refused. It passes, and each of 5 deliberate breaks fails it: bits 25:17 alone (as on main), bit 16 as the ninth opcode bit (bit 8), bit 15 as the tenth, the OP_SEL refusal removed, and that refusal applied to nine-bit opcodes too.
  • agc_shader_replay --gcn on the request the driver saved for inFAMOUS's compute shader 0x2f2fb2100 (APS5_DUMP_SHADERS): with main's decoder, resource analysis fails with GetImageResource dword 0 is not a valid runtime value; chain: CompositeExtractU32x4(Ballot(...)), because the image reads from pc 0x14c on use s[0:7] after thirteen such instructions (pc 0x3c to 0x130) wrote s[0:1]; with this change it passes, and the replay then stops on descriptor memory the request does not hold.
  • The 3,415 tests tools/select_tests.py selects for the 3 changed files (ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1), on this branch and on main 6c70b74203: agc_driver_bda_device fails on both and agc_driver_store_over_untracked only on main; the same 24 tests are skipped on both.
  • With VK_LAYER_KHRONOS_validation 1.4.367 (VK_ADD_LAYER_PATH=<the layer's folder> VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer VK_LOADER_LAYERS_DISABLE=~implicit~ ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1), the tests matching gcn|Gcn|shader_memory|decode, ctest -V: 48 on the branch and on main, all pass; each log has 16 Insert instance layer lines, no was not found and no VUID.
  • inFAMOUS Second Son (CUSA00223 01.07, my own dump; it runs in PS4 Pro mode): eboot, libc.prx and libSceFios2.prx unwrapped with our local PS4 variant of tools/unwrap_self.py, converted with each build's relinker (--windows --registry --trace-imports), two starts of up to 60 s per build, no input. Both builds are main 6c70b74203 with #18184, #21961 and #25557 merged locally and one local relinker change in no PR (a title that needs libSceGnmDriver.prx also loads libSceGnmDriverForNeoMode.prx, standing in for #17711, which conflicts with main); without those the title stops earlier.
    • without this change: both starts stop after 21.9 s and 21.0 s, after presenting a frame, in compute shader 0x2f2fb2100 with ShaderRecompiler::Recompile: GetImageResource dword 0 is not a valid runtime value;
    • with it: the recompile error is gone, and both starts stop after 28.4 s and 21.4 s in the same shader at the driver's PS4 multisampled textures outside the depth tile modes are not implemented, which is outside this PR.
  • python tools/check_conventions.py --base origin/main with this title and description: all checks passed.
  • python tools/pr_overlap.py --branch fix/gcn-vop3-opcode-msb (2,381 open pull requests probed): no overlap. The one that shares files, my #23237 (refusing GCN DPP words), changes other lines of the same two files and merges with this branch without conflicts. That PR refuses DPP because no title is known to use it, its layout is not measured on a PS4 Pro and some of its controls have no RDNA counterpart; this one reads bit 16, though it is not measured on a PS4 Pro either, because a title uses it, and 14 of its shader's words only make sense with the bit.

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: CUSA00223 #8214
  • GPUs tested: NVIDIA GeForce RTX 5080, driver 621.14, Windows 10 22H2
  • AI-assisted: yes

AI-assisted: Claude Code found the cause, wrote the change and its test, ran the tests and the inFAMOUS starts, and drafted this description; separate review agents checked it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5438352806 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25598). Originally posted by [adam25136](https://github.com/adam25136) on 2026-11-22T11:03:24Z. --- ### What A PS4 (GCN) VOP3 instruction's opcode is read from bits 25:17 of its first word, nine bits. PS4 Pro shaders also use VOP3 opcodes from 512 up, whose tenth bit is bit 16: shadPS4 reads it for every VOP3 instruction (`op_msb = bit::extract(hexInstruction, 16, 16); op = op + op_msb * (1 << 9);` in `decodeInstructionVOP3`, [decode.cpp L736-737](https://github.com/shadps4-emu/shadPS4/blob/6999be18c67e84a7ebe6a654dc4c6616528d0229/src/shader_recompiler/frontend/decode.cpp#L736-L737); its opcode list ends the range with "in neo mode, VOP3 gets an additional bit for OP so round up", [opcodes.h L700-L719](https://github.com/shadps4-emu/shadPS4/blob/6999be18c67e84a7ebe6a654dc4c6616528d0229/src/shader_recompiler/frontend/opcodes.h#L700-L719)). `GcnOpcodeTables.hpp` already maps 16 of those opcodes (0x303 to 0x372, for example `v_lshl_add_u32` 0x346 and `v_add3_u32` 0x36d) to the RDNA opcodes of the same numbers, but without the bit no instruction reaches them: each decodes as the opcode 512 below, or is refused where that one is unmapped (0x30d, 0x30e). `v_add3_u32` (877) becomes `v_div_scale_f32` (365), a VOP3b, and that word's bits 14:8, which hold the ABS, CLAMP and reserved bits of a VOP3a, are taken as its scalar destination. No public AMD document covers this encoding: the Sea Islands ISA has no VOP3 opcodes from 512 up, the bit comes from shadPS4, the opcode numbers are the ones RDNA 2 gives the same instructions (all 16 are at these numbers in AMD's *"RDNA 2" Instruction Set Architecture* reference guide, section 12.12 VOP3A & VOP3B Instructions, and `RdnaVectorOpDecoder.cpp` decodes them), and it is not measured on a PS4 Pro. inFAMOUS's shader supports it two ways. None of the 14 words that read as `v_div_scale_f32` with nine bits meets that instruction's rule in the Sea Islands ISA, "s0 must equal s1 or s2" (its `V_DIV_SCALE_F32` entry), while as `v_add3_u32` they do address arithmetic on the thread and group IDs (for example `v6 = v1 + s15 - 1` at pc 0x11c: thread y plus group y × 8, minus 1). And of the 28 compute shaders the driver saved from inFAMOUS (5,125 VOP3 words), only this shader's 19 words set bit 16, each an opcode the table maps, and no word sets any other reserved bit. - **The fix:** `encodeVop3` reads the opcode from bits 25:17 and bit 16 (a `vop3Opcode` helper). In the Sea Islands ISA bit 16 is reserved in both VOP3 forms (VOP3a "reserved [16:12]", VOP3b "[16:15] Reserved", AMD *Sea Islands Series Instruction Set Architecture*, chapter 13, section 13.3), so instructions that leave it clear decode as before. - **OP_SEL:** shadPS4 reads OP_SEL from bits 15:12 of these words (`InstControlVOP3` in [instruction.h L132-L140](https://github.com/shadps4-emu/shadPS4/blob/6999be18c67e84a7ebe6a654dc4c6616528d0229/src/shader_recompiler/frontend/instruction.h#L132-L140)). The re-encoding to RDNA does not carry those bits, and ten of the sixteen opcodes are 16-bit operations whose halves OP_SEL picks, so a mapped ten-bit opcode with any of them set now throws: dropping them would run the 16-bit ones on the wrong halves. inFAMOUS's shader has 19 ten-bit-opcode words (`v_lshl_add_u32` once, `v_add3_u32` 14 times, `v_and_or_b32` 4 times), none with them set. - **What it does not do:** it adds no opcodes. A ten-bit opcode the table does not map is refused (`unsupported VOP3`), as an unmapped nine-bit one is; the two opcodes in shadPS4's list that the table leaves out, 784 (`v_sub_co_u32`) and 833 (`v_mad_f16`), decoded on `main` as `v_max_f32` and `v_mad_f32` (their low nine bits, 0x110 and 0x141) and are now refused. - **Technical debt:** a fragment adds an Unknown function info entry, `GCN VOP3 opcodes from 512 up`: read the shadPS4 way, not measured on a PS4 Pro. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 10 Pro 22H2 (19045), NVIDIA GeForce RTX 5080 (driver 621.14), WinLibs GCC 15.2.0 (r7), Release. The selected tests, the validation-layer run and the inFAMOUS starts ran on this change on `main` `6c70b74203`; the decode test, the breaks and the replay on it before a rebase, on `dac08f50bc`; it is now on `7eb79b0c48`. None of the `main` commits in between change `core/shader` or `libSceAgcDriver`, and its changed lines are the same. - **`agc_gcn_decode`** (new `Vop3OpcodeTakesBit16`): the word inFAMOUS uses, `0xd2db0011 0x04104100`, decodes as `v_add3_u32 v17, v0, s32, v4` with no scalar destination; the same word with bit 16 clear still decodes as `v_div_scale_f32` with `s[0:1]` as its scalar destination, and with bit 12 set too, which in that word makes the scalar destination `s[16:17]`, it still decodes with that destination, so the OP_SEL refusal leaves nine-bit opcodes alone; each of the 16 ten-bit opcodes the table maps decodes as the RDNA opcode of the same number; a word with bit 16 set over `v_rsq_clamp_f32`'s low bits (0x3ac, unmapped) is refused rather than read as `v_rsq_clamp_f32`; and `v_add_nc_u16` (0x303) with any one OP_SEL bit set is refused. It passes, and each of 5 deliberate breaks fails it: bits 25:17 alone (as on `main`), bit 16 as the ninth opcode bit (bit 8), bit 15 as the tenth, the OP_SEL refusal removed, and that refusal applied to nine-bit opcodes too. - **`agc_shader_replay --gcn`** on the request the driver saved for inFAMOUS's compute shader `0x2f2fb2100` (`APS5_DUMP_SHADERS`): with `main`'s decoder, resource analysis fails with `GetImageResource dword 0 is not a valid runtime value; chain: CompositeExtractU32x4(Ballot(...))`, because the image reads from pc 0x14c on use `s[0:7]` after thirteen such instructions (pc 0x3c to 0x130) wrote `s[0:1]`; with this change it passes, and the replay then stops on descriptor memory the request does not hold. - **The 3,415 tests `tools/select_tests.py` selects for the 3 changed files** (`ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`), on this branch and on `main` `6c70b74203`: `agc_driver_bda_device` fails on both and `agc_driver_store_over_untracked` only on `main`; the same 24 tests are skipped on both. - **With `VK_LAYER_KHRONOS_validation`** 1.4.367 (`VK_ADD_LAYER_PATH=<the layer's folder> VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer VK_LOADER_LAYERS_DISABLE=~implicit~ ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`), the tests matching `gcn|Gcn|shader_memory|decode`, `ctest -V`: 48 on the branch and on `main`, all pass; each log has 16 `Insert instance layer` lines, no `was not found` and no VUID. - **inFAMOUS Second Son** (CUSA00223 01.07, my own dump; it runs in PS4 Pro mode): eboot, `libc.prx` and `libSceFios2.prx` unwrapped with our local PS4 variant of `tools/unwrap_self.py`, converted with each build's relinker (`--windows --registry --trace-imports`), two starts of up to 60 s per build, no input. Both builds are `main` `6c70b74203` with #18184, #21961 and #25557 merged locally and one local relinker change in no PR (a title that needs `libSceGnmDriver.prx` also loads `libSceGnmDriverForNeoMode.prx`, standing in for #17711, which conflicts with `main`); without those the title stops earlier. - without this change: both starts stop after 21.9 s and 21.0 s, after presenting a frame, in compute shader `0x2f2fb2100` with `ShaderRecompiler::Recompile: GetImageResource dword 0 is not a valid runtime value`; - with it: the recompile error is gone, and both starts stop after 28.4 s and 21.4 s in the same shader at the driver's `PS4 multisampled textures outside the depth tile modes are not implemented`, which is outside this PR. - **`python tools/check_conventions.py --base origin/main`** with this title and description: all checks passed. - **`python tools/pr_overlap.py --branch fix/gcn-vop3-opcode-msb`** (2,381 open pull requests probed): no overlap. The one that shares files, my #23237 (refusing GCN DPP words), changes other lines of the same two files and merges with this branch without conflicts. That PR refuses DPP because no title is known to use it, its layout is not measured on a PS4 Pro and some of its controls have no RDNA counterpart; this one reads bit 16, though it is not measured on a PS4 Pro either, because a title uses it, and 14 of its shader's words only make sense with the bit. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: CUSA00223 #8214 <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: NVIDIA GeForce RTX 5080, driver 621.14, Windows 10 22H2 <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> AI-assisted: Claude Code found the cause, wrote the change and its test, ran the tests and the inFAMOUS starts, and drafted this description; separate review agents checked it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-22 11:14:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T11:14:12Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the evidence supports merging, with the console validation noted below.

What I ran

Reviewed head 5db5a44acde47c648549d590ecd433bc1febe94a. I read the description, commits, diff, manifest, and all three changed files using repo_read.py. No tests or builds were run.

Results

Check Result Note
Changed code review passed All three changed files read
Tests and build not run Author reports test results; not independently run

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not independently run the reported tests, inspect the excluded-file listing (files.json was unavailable through the reader), or verify behavior on a PS4 Pro console. The author explicitly notes the opcode interpretation has not been measured on console.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6011937736 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25598#pullrequestreview-6011937736). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T11:14:12Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the evidence supports merging, with the console validation noted below. ## What I ran Reviewed head `5db5a44acde47c648549d590ecd433bc1febe94a`. I read the description, commits, diff, manifest, and all three changed files using `repo_read.py`. No tests or builds were run. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All three changed files read | | Tests and build | not run | Author reports test results; not independently run | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not independently run the reported tests, inspect the excluded-file listing (`files.json` was unavailable through the reader), or verify behavior on a PS4 Pro console. The author explicitly notes the opcode interpretation has not been measured on console. ## To get this to a good state Nothing is required.
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!25598
No description provided.