Skip to content

feat(shader): implement v_sad_u8, v_sad_hi_u8, v_sad_u16 and v_msad_u8 - #379

Merged
boykopovar merged 1 commit into
boykopovar:mainfrom
DotDebian:feat/shader-vop3-sad
Oct 2, 2026
Merged

boykopovar merged 1 commit into
boykopovar:mainfrom
DotDebian:feat/shader-vop3-sad

Conversation

@DotDebian

Copy link
Copy Markdown
Contributor

What

v_sad_u8 (VOP3 0x15a), v_sad_hi_u8 (0x15b), v_sad_u16 (0x15c) and v_msad_u8 (0x171) threw VOP3 opcode is not implemented. Only v_sad_u32 (0x15d) of the family was decoded.

All four take three 32-bit sources and share one translation (subwordSad): split S0 and S1 into unsigned fields, sum the absolute differences of the fields, add S2, wrap at 32 bits.

Instruction Fields Result
v_sad_u8 4 bytes sum + S2
v_sad_hi_u8 4 bytes (sum << 16) + S2
v_sad_u16 2 words sum + S2
v_msad_u8 4 bytes, skipping those whose S1 byte is zero sum + S2

Sources:

  • Opcode numbers: LLVM VOP3Instructions.td (VOP3_Real_gfx6_gfx7_gfx10<0x15a> to <0x15c>, <0x171>) and the encodings in llvm/test/MC/AMDGPU/gfx10_asm_vop3.s.
  • Semantics of the three v_sad_*: AMD's ISA description of each instruction, and the existing v_sad_u32.
  • v_msad_u8: AMD's description only names it a masked byte SAD of (S0, S1, S2). That the mask is the zero bytes of S1 is from the CLRX GCN ISA documentation (GcnInstrsVop3), and is the reference-byte rule of HLSL msad4.

LLVM declares the four with VOP3_CLAMP. I found no public definition of the clamped result, so the clamp bit stays refused (VOP3 source modifiers are not implemented), as it already is for v_sad_u32.

Test agc_driver_vop3_sad, code words assembled by hand: a 64-thread compute shader loads three dwords per thread and stores ten results: the four new instructions and v_sad_u32 on (a, b, c), v_sad_u8 against inline constants 0 and -1, v_sad_hi_u8 accumulating into a v_sad_u8 result, v_sad_u16 against a literal, and v_msad_u8 with the sources swapped. Inputs are 12 edge triples (wrap of the 32-bit sum, 0x80 against 0x7f fields, zero reference bytes) and random values. It also checks that the four opcodes with the clamp bit set are refused.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, NVIDIA RTX 5080. Full ctest: 109/109 pass.

  • agc_driver_vop3_sad passes.
  • Without the recompiler change it fails with ShaderRecompiler::Recompile: VOP3 opcode is not implemented.

Not tested on Linux, on AMD hardware or with a title.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

Decode VOP3 0x15a, 0x15b, 0x15c and 0x171 and translate them as the
unsigned sum of absolute differences over the byte or word fields of
the first two sources, accumulated into the third. v_sad_hi_u8 shifts
the sum into the high half first, v_msad_u8 skips the fields whose
reference byte in the second source is zero.

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

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown

Progress report (313de1c → 9f2319b)

📈 GPU shader instructions: 59.95% (+0.34%, +4 implemented instructions)

✅ 4 implemented
Encoding Name
VOP3 V_MSAD_U8
VOP3 V_SAD_HI_U8
VOP3 V_SAD_U16
VOP3 V_SAD_U8

@github-actions github-actions Bot added shader core/shader agc libSceAgc* labels Oct 2, 2026
@boykopovar
boykopovar merged commit b68c8c2 into boykopovar:main Oct 2, 2026
3 checks passed
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 3, 2026
…oykopovar#304 boykopovar#305 boykopovar#306 boykopovar#322 boykopovar#409 boykopovar#416, boykopovar#276 boykopovar#277 boykopovar#275 boykopovar#307, shader ops boykopovar#226 boykopovar#255 boykopovar#260 boykopovar#280 boykopovar#308 boykopovar#309 boykopovar#311 boykopovar#318 boykopovar#320 boykopovar#324 boykopovar#326 boykopovar#379 boykopovar#384 boykopovar#399, boykopovar#261, kernel boykopovar#335 boykopovar#341 boykopovar#349 boykopovar#355, libs boykopovar#227 boykopovar#244 boykopovar#245 boykopovar#273 boykopovar#274 boykopovar#279 boykopovar#286 boykopovar#287 boykopovar#289 boykopovar#296 boykopovar#300 boykopovar#328 boykopovar#358 boykopovar#402) into astrobot

Where our PR merged in a revised form, main's version is taken; where
astrobot carries the same work plus later local commits, astrobot's code
stays and main's additions appear once.

Conflicts:
- AvPlayer (Export.cpp both libs, include/AvPlayer.hpp, src/Player.cpp,
  src/Source.cpp, AvPlayerLibrary.cmake, CMakeLists.txt, SceTypes.hpp,
  tests/GuestAvPlayer.cpp): main's boykopovar#307 (our reviewed squash, which carries
  9c8e7fd e9ea5c8 adcede3 fb4eeb9 953057c ac456ee). It links the
  shared ffmpeg-core target, so astrobot's second FFmpeg build goes:
  3rdparty/FFmpeg submodule, core/libs/FFmpeg.cmake and src/Exports.cpp.
- libScePngEnc: main's boykopovar#227 (supersedes OverkillLabs' encoder).
- libScePad Export.cpp: main's boykopovar#287 tilt correction (supersedes 49d45f5).
- libSceAudioPropagation Export.cpp: astrobot's object model, which
  defines every function boykopovar#289 declares as a stub.
- libSceSaveData.native Export.cpp: both (SaveDataHoldWrites and boykopovar#289's
  unknown exports).
- VideoOutDriver.cpp: astrobot's (2546bd1 counters around boykopovar#416's
  lastFlipVblank store, same value). tests/Flip.cpp: boykopovar#416's pacing test and
  boykopovar#417's after-GPU test both kept.
- boykopovar#278 files (DisplayFormat, VulkanDevice, GpuColorTransfer,
  ResidentPresent, agc CMakeLists): main's comment-free boykopovar#278 plus
  astrobot's P5 present-direct (b29728d), which removed DetileImage,
  ResidentPresent.hpp and the recorder-test hook; the resident present test
  stays its own executable.
- VulkanDevice.cpp/.hpp: astrobot's GDS, local memory probe, sample dumps
  and P5 code; boykopovar#303/boykopovar#409 comments as main has them; main's shader clock
  (boykopovar#324) and DepthSurface-free includes.
- RequestSerializer.cpp: version stays 8 (6 mesh passthrough, 7
  nonConstantImageOffsets, 8 astrobot's later field). boykopovar#303's auto-merged
  second write and read of nonConstantImageOffsets removed.
- SpirvEmitter.hpp, SpirvEmitterState.hpp, Recompiler.hpp/.cpp, StageIO.hpp,
  SpirvOptimizer.cpp, InstructionTranslator.cpp, ShaderReplay.cpp: local
  fields kept, boykopovar#303/boykopovar#305 comments as main has them.
- SpirvAluEmitterMath.cpp: v_readlane keeps the split-wave path and takes
  boykopovar#322's wrap at the wave size for the others.
- SpirvEmitter.cpp: astrobot's single-lane layout check.
- SpirvFlowEmitter.cpp: boykopovar#304 as merged (the second lane emits a shared
  value itself when the first lane defined nothing).
- SpirvImageEmitter.cpp: main's Offset operand placed before MinLod.
- boykopovar#304: main's WaveUniformValues (SpirvAnalysis.cpp) replaces astrobot's
  copy in SpirvWaveExchange.cpp.
- Recorder.hpp/.cpp, Draw.hpp, Graphics Draw.cpp, ShaderResources.cpp:
  astrobot's boykopovar#276/boykopovar#277 with its later snapshot work (per-use budgets,
  stale-snapshot refresh, profile counters, boykopovar#410, boykopovar#424, fan GS); boykopovar#409/boykopovar#306
  comments as main has them.
- PacketMemory.cpp: boykopovar#409 as merged plus astrobot's two opt-outs.
- tests: ShaderMemory.cpp takes boykopovar#303's and boykopovar#304's new tests; Recorder.cpp
  takes boykopovar#409's afterRecordedWorkTests and drops main's copies of the
  boykopovar#276/boykopovar#277 tests astrobot already has; Structurizer.cpp keeps boykopovar#413's case.

boykopovar#143 (NGG passthrough) stays as on astrobot: upstream did not touch its
files since the boykopovar#258 revert.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants