fix(shader): apply VOP3P neg_lo/neg_hi to packed 16-bit integer sources #4268
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4268"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T03:37:47Z.
Sword of the Sea (PPSA25344) menu: the TSR history no longer loses ~17 % of its energy every frame; top luma 77 → 87 (PS5 capture 89-91), and the glow at the top of the menu is back.
What
The packed integer instructions (
v_pk_add/sub_{u,i}16,v_pk_mul_lo_u16,v_pk_mad_{u,i}16,v_pk_min/max_{u,i}16,v_pk_lshlrev/lshrrev/ashrrev_b16) read their 16-bit sources throughreadU16LaneAsU32, which appliedop_sel/op_sel_hibut droppedneg_lo/neg_hi. The decoder already sets both flags for every VOP3P source; only the f16 path used them.Now
readU16LaneAsU32flips bit 15 of the selected 16-bit half whenneg_lo(low lane) orneg_hi(high lane) is set for that source, before the optional sign extension. This is the same sign-bit flip the f16 path applies. The integer dot instructions also read through this function, but the decoder still rejectsnegon them, so they are unchanged.How the neg bit works on 16-bit integer halves
The RDNA 2 ISA (13.3.6, VOP3P) describes
NEGandNEG_HIas "negate input for low/high 16-bits of sources". Unlike the VOP3NEGfield ("floating-point negation"), it does not limit them to floating-point opcodes. LLVM's assembler accepts them only on floating-point operands. That is an assembler rule, so it says nothing either way about what the hardware does with the bit on integer opcodes.The compiled code settles it. Sony's shader compiler emits
negon integer packed ops, always asneg(literal 0x7fff), which only makes sense if the result is0xffff, i.e. −1:UpdateHistoryclamps its 3x3 neighbour offsets withv_pk_max_i16 v14, neg(0x7fff), v14(neg_lo:[1,0] neg_hi:[1,0] op_sel_hi:[0,1]), i.e.max(-1, min - pos). The other side usesv_pk_min_i16 v11, 1, v11. Two such sites are in this program, and a third program has the samemax(-1, ·)/min(1, ·)pair.v_pk_add_u16 v5, neg(0x7fff), v3.These are the only uses of
negon packed integer ops among the 133 shader programs of one Sword of the Sea menu frame. A compiler that meant arithmetic negation would have written −1 asneg(1)with an inline constant. A compiler that expected the bit to be ignored would not emit it at all. Only the bit-15 flip turns0x7fffinto −1.Ignoring the bit turned the clamp into
max(0x7fff, x). The x−1 and y−1 taps of TSR's input filter were then loaded at0x7fff + x, out of bounds as A16 coordinates, and read as 0. The weights were still normalised over all nine taps. Per-pixel register probes on a replayed menu frame:With the change, the reconstructed input is 1.00× (mean 0.0243 against 0.0244). The history blend is a convex combination of the rectified history and the input again: 94 % of pixels lie between the two, with history weights in [0, 1].
Tested
Windows 11 (26200), WinLibs GCC 15.2.0, Release, on
main34a16b7f0.agc_driver_packed_integer_negate(new) fails onmain(439 of 512 results wrong; the TSR case returns0x7fff7fffinstead ofmax(-1, x)) and passes with this change. It runs 16 packed integer instructions over 32 lanes of edge values (0, ±1, 0x7fff, 0x8000, 0x8001, 0xffff, …). The cases:neg_loandneg_hion different sources;op_sel/op_sel_hiswaps combined withneg;v_pk_add_i16withneg;v_pk_max_i16 neg(lit 0x7fff), which must givemax(-1, x)per lane.Expected values come from a per-lane reference: select the half, flip bit 15, then apply the opcode.
GPU suites on NVIDIA (RTX 5070 Laptop, AMD iGPU hidden), every
^agc_ctest entry built on the Lenovo node and run here: 423/423 pass. Onmainwith only the new test added,agc_driver_packed_integer_negatefails, andpacked_integer_clamp,packed_aluandinteger_dotpass.Non-GPU suite (Lenovo node,
ctest -E "^agc_driver|^video_out|^guest_video"): 578/579 pass.log_macrosfails the same way onmain34a16b7f0without this change.Sword of the Sea menu, 240 s, shots at 60/120/230 s, on a lab branch that also carries a pending HTILE fix (without it the menu has black blobs): dark pixels 0.0 % and top luma 87/87/88 at frames 164/472/1026. On the same branch without this change, top luma was 77/73/80. The PS5 capture shows 89-91. The same holds with the shared direct-memory branch underneath: top luma 88 at frame 585.
Conventions:
tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-12T03:44:28Z.
Second-GPU data point, NVIDIA RTX 4060 (Windows 10, MinGW build):
agc_driver_packed_integer_negatepasses (0.4 s).SubwordPacking.cppreverted to the pre-fix version (only the test rebuilt): fails with 439 mismatches, matching the 439/512 onmain. Examples:In-game on the same machine (Sword of the Sea main menu, on the working-group stack): with the fix the top glow, light shafts and particle glow come back and match the PS5 frame; without it the scene settles noticeably darker.
Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-12T12:54:30Z.
Measured on hardware: on gfx1036 (RDNA2),
neg_lo/neg_hion a packed 16-bit integer source flips bit 15 of the selected half, as this PR models. The gfx1036 output equals the values thatReference()inPackedIntegerNegate.cppexpects, in all 512 cells, in wave32 and in wave64.Setup:
tools/hw-oraclefrommain3d8ffeb2on gfx1036 (RDNA2 iGPU of a Ryzen 9 9950X), Linux 7.0 (Ubuntu 24.04), ROCm HSA runtime 5.7.1, LLVM 18.1.3. Float controls:ieee 0,dx10_clamp 1,denorm32 0,denorm16 3,round32 0,round16 0,fp16_overflow 0. The body is the test's 33 instruction dwords as raw.longwords, run over its 32 rows. A second body runs 5 probes that are not in the test, over the same 32 rows.Cells where gfx1036 matches each model of the neg bit:
main)Wave32 and wave64 gave identical tables. Three runs gave byte-identical output.
The extra probes:
v_pk_add_u16 v10, neg(0x7fff), v4subtracts 1 per half:0x000f8000gives0x000e7fff. This is the decrement form from the description.v_pk_max_i16 v11, neg(1), v4compares with 0x8001 (−32767), not −1:0x80008000gives0x80018001.v_pk_min_i16 v12, neg(-1), v4,v_pk_sub_u16 v13, v4, neg(0x7fff)(adds 1) andv_pk_mad_u16 v14, v4, neg(1), v5(multiplies by 0x8001) also follow the bit-15 flip.Controls:
neg_lo/neg_hibit cleared in both bodies, gfx1036 matches the "ignore" model in 512/512 and 160/160 cells. So the match comes from the neg bits.v_pk_madsrc2 accumulator, the clamped add, the literal and inline-constant sources, and theop_selswaps.v_pk_ashrrev_i16src0.hi,v_pk_lshrrev_b16src0.lo). Only the low 4 bits of the count are used, so flipping bit 15 and ignoring the bit give the same result. Two's complement is still refuted there (16 and 14 of 32 half-lanes), and the XOR in this PR is harmless.Recompiler before and after: Ubuntu 24.04, GCC 13.3, Release with
BUILD_TESTING=ONandANYPS5_ENABLE_SPIRV_TOOLS=ON. I ranagc_driver_packed_integer_negate,agc_driver_packed_integer_clamp,agc_driver_packed_aluandagc_driver_integer_dotwithANYPS5_REQUIRE_VULKAN=1, so a missing device fails instead of skipping. The devices were RADV (gfx1036, Mesa 25.2.8), an RTX 3090 (NVIDIA 595.99.02) and llvmpipe (Mesa 25.2.8). parl0v's RTX 4060 run already covers NVIDIA; RADV and llvmpipe are new.main3d8ffeb2with onlyPackedIntegerNegate.cppadded: 3/4 on each device.agc_driver_packed_integer_negatefails with 439 mismatches. That is exactly the number of cells where ignoring the neg bit differs from the bit-15 flip (512 − 73).48654bdc: 4/4 on each device.One correction to the description: LLVM's assembler does accept
negon these integer ops.llvm-mc18.1.3 and 22.1.8 assembleneg_lo/neg_hionv_pk_max_i16(with a literal),v_pk_mad_i16andv_pk_lshrrev_b16for-mcpu=gfx1013andgfx1030, with identical encodings.Not measured: gfx1013 itself. gfx1036 is gfx10.3 and the PS5 is gfx10.1.
HW_ORACLE.mdtreats VALU encodings as shared apart from the gfx10.1-only ops, and none of these opcodes is one of them. Neg on the integer dot ops was not measured; the decoder still rejects it.Reproduce
WORDSisCode[5..37]ofPackedIntegerNegate.cpp(33 dwords).ROWSis itsRows. Resultiof each row isv(10 + i); compareout[row][:16]withReference(Cases[i], Rows[row])from the test. The extra probes are0xcc0a410a 0x300208ff 0x00007fff 0xcc07410b 0x30020881 0xcc08410c 0x300208c1 0xcc0b420d 0x4801ff04 0x00007fff 0xcc09420e 0x4c150304. Run assg render -c "python3 repro.py".Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T15:42:27Z.
@simonbbby thanks a lot for running it on gfx1036. Hardware matching all 512 cells in both wave sizes is exactly the evidence this one needed.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-20T08:34:30Z.
Hardware confirmation of this rule on RDNA 2, for the record: RX 6700 XT (gfx1031), raw ISA through the HIP runtime of AMD's Windows driver 32.0.21045.5002. All 14 packed 16-bit integer VOP3P ops (
v_pk_mad_i16/u16,v_pk_mul_lo_u16,v_pk_add/sub_i16/u16,v_pk_lshlrev/lshrrev_b16,v_pk_ashrrev_i16,v_pk_max/min_i16/u16) ran with theneg_lo/neg_hibits set directly in the encoding: each source alone, both halves, all three sources for the mads. Inputs were 144 edge pairs (0, 1, 0x7fff, 0x8000, 0x8001, 0xffff, ...) and 112 random words, 256 rows per combination.Every row of every combination matches flipping bit 15 of the selected half before the operation, which is what this PR implements. A two's-complement negate of the half matches 0-46% of the rows, and ignoring the bits 0-100% depending on the op (shifts ignore a negated shift amount in their low bits by construction).