feat(agc): sample unnormalized S#s with mirror and mirror-once clamp modes #11126

Merged
shahulmubeen-a merged 5 commits from upstream-pr/11126 into main 2026-11-02 10:27:04 +00:00
shahulmubeen-a commented 2026-10-25 18:56:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by shahulmubeen-a on 2026-10-25T18:56:58Z.


What

A FORCE_UNNORMALIZED S# with clamp mode 1 (mirror) or 3 (mirror-once) on X or Y is refused today:

guest sampler descriptor uses unnormalized coordinates with clamp mode 1 on X; only clamp-to-last-texel and clamp-to-border are implemented

Vulkan allows only clamp-to-edge and clamp-to-border with unnormalized coordinates, so the mirroring has to happen in the shader. I measured what the hardware does with these modes first. The setup:

  • GPU: RDNA2 (gfx1036), with tools/hw-oracle through the AMD Windows driver's comgr and HIP runtime.
  • Settings: wave32, IEEE off, DX10 clamp on, denormals preserved, round to nearest even.
  • Instructions: image_sample_lz and image_sample_lz_o.
  • T#: linear 32_FLOAT, 64x1 and 64x4, each texel holding x + 1000 * y.
  • Filters: point and bilinear.
  • Coordinates: -200.5 to 191.5 on X, -9.5 to 7.5 on Y. Offsets: -8 to +3.

What the hardware does:

  • Modes 1 and 3 give the same result. Texel index i < 0 reads texel -i - 1, mirrored only once. Past the last texel it clamps; there is no second reflection or repeat. Each bilinear tap is mirrored on its own. The texel offset is added before the mirroring.
  • Mode 0 samples exactly like mode 2. This confirms the clamp-to-edge mapping from e1d402c6c (#4736): unnormalized repeat does not wrap on this hardware.
Measured rows (excerpt)

X, texel = x, row 1, values are the sampled texel:

clamp 1 point   -200.5:63 -130.5:63 -64.5:63 -63.5:63 -2:1 -1.5:1 -1:0 -0.5:0 0.5:0 ... 63.75:63 64.5:63 128.5:63
clamp 1 linear  -200.5:63 -65.5:63 -2:1.5 -1.5:1 -1:0.5 -0.75:0.25 -0.5:0 ... 64.5:63
clamp 3         same rows as clamp 1
clamp 0         same rows as clamp 2 (0 for every negative coordinate, 63 past the end)

Y (64x4, u = 5.5): clamp 1/3 point -9.5:row 3 -4.5:row 3 -3.5:row 3 -1:row 0; linear -1:row 0.5 -0.75:row 0.25.

Offsets (image_sample_lz_o, mirror): 1.5 + (-3) reads texel 1, 2.5 + (-5) texel 2, 5.5 + (-8) texel 2. With clamp 2 the same rows read texel 0.

How the change implements this:

  • DecodeSamplerResource accepts modes 1 and 3 with unnormalized coordinates and binds clamp-to-edge for them, as it already does for mode 0.
  • A per-sampler specialization constant (MirrorUnnormalizedBase) carries the mirrored axes. ProveUnnormalized sets it from the S# clamp fields, and the binding plan key already includes all four S# words.
  • When the constant is nonzero, EmitSampleOp samples from a coordinate in which each negative component on a mirrored axis is replaced with the float just below its negation, after any texel offset. That maps index i < 0 to -i - 1 and puts each bilinear tap where the hardware does. Clamp-to-edge then gives the clamp past the last texel.
  • When the constant is 0, the branch is the same shape as the texel-offset fold and is removed after specialization. agc_shader_memory still sees the plain folded coordinate.

The debt/libSceAgcDriver.md entry now records the measured rule for modes 0, 1 and 3 and that gfx1013 is not measured. Modes 4, 5 and 7 still throw.

No title I know of uses these modes yet, so Unblocks is none. Ghost of Yōtei hits only mode 0, which main already accepts. I found this while re-checking an older local change of mine that wrapped mode 0 and mirrored modes 1 and 3 fully; this measurement showed that change was wrong.

Tested

Windows 11 Pro 26300, NVIDIA GeForce RTX 4070 Ti SUPER (610.88), WinLibs GCC 15.2.0, Release. Tested head: 098fc2772 on main 8a29e0883. The review follow-ups are specialization id 57536, a debt note, and dropping the refusal loops for modes 4, 5 and 7 from the decode test. The last test run was on f636ecd34 with the validation settings below plus agc_driver_graphics: 72 of 72 pass, no VUID- errors. 098fc2772 only shortens the debt entry to under 1000 characters.

  • agc_driver_image_sample_unnormalized gains three samplers, checked against an index-mirroring reference through image_sample_lz, image_sample_l, image_sample, the offset variants and image_sample_d, on 1-level and 4-level views:

    • bilinear mirror on X and Y;
    • point mirror-once on X with clamp-to-last-texel on Y;
    • bilinear clamp-to-last-texel on X with mirror-once on Y.

    The test also adds more negative coordinates.

    • With GuestSamplerResource.cpp from main, it fails with guest sampler descriptor uses unnormalized coordinates with clamp mode 1 on X; only clamp-to-last-texel and clamp-to-border are implemented.
    • With SpirvImageEmitter.cpp from main, it fails with image_sample_lz, bilinear mirror on X and Y, point mips, 1-level image: (-1.000000, 2.500000) offset (0, 0) component 0 is 0.000000, expected 120.000000.
  • agc_driver_graphics (the sampler decode test) now expects modes 1 and 3 to decode to clamp-to-edge. It no longer requires modes 4, 5 and 7 to throw: they are valid values that are not supported yet. The code still refuses them.

  • I ran 71 sampling and shader tests on main and on this head, each with ctest -V, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1. The regex was sample|gather|sampler|shader_memory|offset|unnormalized|shader_disk_cache|on_line|on_plane|on_volume|lod.

    • The layer was loaded in both runs: there is no was not found but was requested, and each log has the same 9 validation warnings.
    • Neither log has a VUID- error.
    • Both runs have the same 4 non-passes, none caused by this change:
      • agc_sampled_storage_base_level_usage, agc_multisample_guest_access and agc_sample_dumps: Not Run, because I did not build them.
      • elf_offsets: a Python helper that cannot find its executable on this machine.
  • tools/check_conventions.py --base origin/main passes.

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 4070 Ti SUPER, driver 610.88, Windows 11 Pro 26300 (tests); AMD Radeon Graphics iGPU of a Ryzen 9 7950X (gfx1036), AMD Windows driver, for the oracle measurement
  • AI-assisted: yes (written with Claude Code; I reviewed and ran it)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5007689902 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11126). Originally posted by [shahulmubeen-a](https://github.com/shahulmubeen-a) on 2026-10-25T18:56:58Z. --- ### What A FORCE_UNNORMALIZED S# with clamp mode 1 (mirror) or 3 (mirror-once) on X or Y is refused today: ``` guest sampler descriptor uses unnormalized coordinates with clamp mode 1 on X; only clamp-to-last-texel and clamp-to-border are implemented ``` Vulkan allows only clamp-to-edge and clamp-to-border with unnormalized coordinates, so the mirroring has to happen in the shader. I measured what the hardware does with these modes first. The setup: - GPU: RDNA2 (gfx1036), with `tools/hw-oracle` through the AMD Windows driver's comgr and HIP runtime. - Settings: wave32, IEEE off, DX10 clamp on, denormals preserved, round to nearest even. - Instructions: `image_sample_lz` and `image_sample_lz_o`. - T#: linear 32_FLOAT, 64x1 and 64x4, each texel holding `x + 1000 * y`. - Filters: point and bilinear. - Coordinates: -200.5 to 191.5 on X, -9.5 to 7.5 on Y. Offsets: -8 to +3. What the hardware does: - **Modes 1 and 3** give the same result. Texel index i < 0 reads texel -i - 1, mirrored only once. Past the last texel it clamps; there is no second reflection or repeat. Each bilinear tap is mirrored on its own. The texel offset is added before the mirroring. - **Mode 0** samples exactly like mode 2. This confirms the clamp-to-edge mapping from e1d402c6c (#4736): unnormalized repeat does not wrap on this hardware. <details><summary>Measured rows (excerpt)</summary> X, texel = x, row 1, values are the sampled texel: ``` clamp 1 point -200.5:63 -130.5:63 -64.5:63 -63.5:63 -2:1 -1.5:1 -1:0 -0.5:0 0.5:0 ... 63.75:63 64.5:63 128.5:63 clamp 1 linear -200.5:63 -65.5:63 -2:1.5 -1.5:1 -1:0.5 -0.75:0.25 -0.5:0 ... 64.5:63 clamp 3 same rows as clamp 1 clamp 0 same rows as clamp 2 (0 for every negative coordinate, 63 past the end) ``` Y (64x4, u = 5.5): clamp 1/3 point `-9.5:row 3 -4.5:row 3 -3.5:row 3 -1:row 0`; linear `-1:row 0.5 -0.75:row 0.25`. Offsets (`image_sample_lz_o`, mirror): `1.5 + (-3)` reads texel 1, `2.5 + (-5)` texel 2, `5.5 + (-8)` texel 2. With clamp 2 the same rows read texel 0. </details> How the change implements this: - `DecodeSamplerResource` accepts modes 1 and 3 with unnormalized coordinates and binds clamp-to-edge for them, as it already does for mode 0. - A per-sampler specialization constant (`MirrorUnnormalizedBase`) carries the mirrored axes. `ProveUnnormalized` sets it from the S# clamp fields, and the binding plan key already includes all four S# words. - When the constant is nonzero, `EmitSampleOp` samples from a coordinate in which each negative component on a mirrored axis is replaced with the float just below its negation, after any texel offset. That maps index i < 0 to -i - 1 and puts each bilinear tap where the hardware does. Clamp-to-edge then gives the clamp past the last texel. - When the constant is 0, the branch is the same shape as the texel-offset fold and is removed after specialization. `agc_shader_memory` still sees the plain folded coordinate. The `debt/libSceAgcDriver.md` entry now records the measured rule for modes 0, 1 and 3 and that gfx1013 is not measured. Modes 4, 5 and 7 still throw. No title I know of uses these modes yet, so `Unblocks` is none. Ghost of Yōtei hits only mode 0, which main already accepts. I found this while re-checking an older local change of mine that wrapped mode 0 and mirrored modes 1 and 3 fully; this measurement showed that change was wrong. ### Tested Windows 11 Pro 26300, NVIDIA GeForce RTX 4070 Ti SUPER (610.88), WinLibs GCC 15.2.0, Release. Tested head: 098fc2772 on `main` 8a29e0883. The review follow-ups are specialization id 57536, a debt note, and dropping the refusal loops for modes 4, 5 and 7 from the decode test. The last test run was on f636ecd34 with the validation settings below plus `agc_driver_graphics`: 72 of 72 pass, no `VUID-` errors. 098fc2772 only shortens the debt entry to under 1000 characters. - `agc_driver_image_sample_unnormalized` gains three samplers, checked against an index-mirroring reference through `image_sample_lz`, `image_sample_l`, `image_sample`, the offset variants and `image_sample_d`, on 1-level and 4-level views: - bilinear mirror on X and Y; - point mirror-once on X with clamp-to-last-texel on Y; - bilinear clamp-to-last-texel on X with mirror-once on Y. The test also adds more negative coordinates. - With `GuestSamplerResource.cpp` from `main`, it fails with `guest sampler descriptor uses unnormalized coordinates with clamp mode 1 on X; only clamp-to-last-texel and clamp-to-border are implemented`. - With `SpirvImageEmitter.cpp` from `main`, it fails with `image_sample_lz, bilinear mirror on X and Y, point mips, 1-level image: (-1.000000, 2.500000) offset (0, 0) component 0 is 0.000000, expected 120.000000`. - `agc_driver_graphics` (the sampler decode test) now expects modes 1 and 3 to decode to clamp-to-edge. It no longer requires modes 4, 5 and 7 to throw: they are valid values that are not supported yet. The code still refuses them. - I ran 71 sampling and shader tests on `main` and on this head, each with `ctest -V`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`. The regex was `sample|gather|sampler|shader_memory|offset|unnormalized|shader_disk_cache|on_line|on_plane|on_volume|lod`. - The layer was loaded in both runs: there is no `was not found but was requested`, and each log has the same 9 validation warnings. - Neither log has a `VUID-` error. - Both runs have the same 4 non-passes, none caused by this change: - `agc_sampled_storage_base_level_usage`, `agc_multisample_guest_access` and `agc_sample_dumps`: Not Run, because I did not build them. - `elf_offsets`: a Python helper that cannot find its executable on this machine. - `tools/check_conventions.py --base origin/main` passes. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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: NVIDIA GeForce RTX 4070 Ti SUPER, driver 610.88, Windows 11 Pro 26300 (tests); AMD Radeon Graphics iGPU of a Ryzen 9 7950X (gfx1036), AMD Windows driver, for the oracle measurement - [x] AI-assisted: yes (written with Claude Code; I reviewed and ran it) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 19:02:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T19:02:09Z.

Review: APPROVED


Reviewed the eight-file change against base 8a29e088 and head e2f17064. The unnormalized proof limits this path to supported 1D/2D sampled images; the emitter applies the negative-coordinate transform after folded offsets, and positive coordinates still use clamp-to-edge. Depth-compare and format-conversion paths are excluded or handled separately. The specialization IDs are in a separate range from the existing sampler and image IDs. The change adds focused decoding and sampling cases for mirror and mirror-once on both axes, point/bilinear filtering, offsets, and mip views. I could not independently run the Vulkan suite on this host; the PR reports 71 focused tests on both the base and change on the same RTX 4070 Ti SUPER with validation enabled, matching warning counts and no VUID errors, plus a separate AMD gfx1036 hardware-oracle measurement. Conventions, eligibility, intake, labels, and report checks pass; product build jobs are skipped on this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644769113 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11126#pullrequestreview-5644769113). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T19:02:09Z. **Review:** APPROVED --- Reviewed the eight-file change against base `8a29e088` and head `e2f17064`. The unnormalized proof limits this path to supported 1D/2D sampled images; the emitter applies the negative-coordinate transform after folded offsets, and positive coordinates still use clamp-to-edge. Depth-compare and format-conversion paths are excluded or handled separately. The specialization IDs are in a separate range from the existing sampler and image IDs. The change adds focused decoding and sampling cases for mirror and mirror-once on both axes, point/bilinear filtering, offsets, and mip views. I could not independently run the Vulkan suite on this host; the PR reports 71 focused tests on both the base and change on the same RTX 4070 Ti SUPER with validation enabled, matching warning counts and no VUID errors, plus a separate AMD gfx1036 hardware-oracle measurement. Conventions, eligibility, intake, labels, and report checks pass; product build jobs are skipped on this PR.
klosixgaming-arch commented 2026-10-25 21:45:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-25T21:45:40Z.

Review: APPROVED


Reviewed head e2f17064b8 by reading the diff and the existing review thread. I did not build or run this, so everything below is from the code, not from a test of mine.

What it does. An unnormalized S# with clamp mode 1 (mirror) or 3 (mirror-once) was refused. It is now accepted; the host sampler still uses clamp-to-edge, and the shader folds negative coordinates in a pipeline specialization (MirrorUnnormalizedBase + sampler), so positive coordinates keep clamp-to-edge behaviour and negative ones are reflected. Mode 0 keeps its existing mapping.

Checked the emitted maths. MirroredUnnormalizedCoord takes x < 0 and returns bitcast(bitcast_u32(-x) - 1), i.e. -x minus one ULP. That looked like a bug at first read (it is not -x - 1), but it is the intended continuous reflection: texel floor(u) = k < 0 must map to -k - 1, and subtracting one ULP makes an exact negative integer such as -1.0 land in texel 0 rather than 1. The test coordinates include -1.0, -2.0, -width, -0.25, -0.75, -3.25, so the boundary case is exercised. I'd still put that reasoning in a one-line debt note, because the next reader will make the same mistake I nearly did.

Other checks:

  • The new specialization id 57728 does not collide with anything in PipelineSpecialization.hpp. The other per-sampler bases are 64 apart (57344, 57408, 57472) and this one jumps to 57728, which wastes the 57536 slot for no reason I can see. Harmless, but please use the next free 64-aligned id or explain the gap.
  • MirrorsIfUnnormalized excludes depth-compare, table-indexed samplers and dimension other than 1D/2D, and the emitter additionally excludes cube, so unsupported shapes keep the old rejection path rather than silently sampling wrongly.
  • Real-GPU verification exists in the thread (RADV on an RX Vega 56, with validation layers).

One caveat to keep in view: the debt entry says the behaviour follows RDNA2 gfx1036 oracle measurements, not gfx1013 (the PS5 GPU family). That is disclosed, which is the honest way to do it, and I'd leave the caveat exactly as written.

Approving.

AI-assisted: yes. Written with AI assistance and reviewed by me.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645479308 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11126#pullrequestreview-5645479308). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-25T21:45:40Z. **Review:** APPROVED --- Reviewed head `e2f17064b8` by reading the diff and the existing review thread. I did not build or run this, so everything below is from the code, not from a test of mine. **What it does.** An unnormalized S# with clamp mode 1 (mirror) or 3 (mirror-once) was refused. It is now accepted; the host sampler still uses clamp-to-edge, and the shader folds negative coordinates in a pipeline specialization (`MirrorUnnormalizedBase + sampler`), so positive coordinates keep clamp-to-edge behaviour and negative ones are reflected. Mode 0 keeps its existing mapping. **Checked the emitted maths.** `MirroredUnnormalizedCoord` takes `x < 0` and returns `bitcast(bitcast_u32(-x) - 1)`, i.e. `-x` minus one ULP. That looked like a bug at first read (it is not `-x - 1`), but it is the intended continuous reflection: texel `floor(u) = k < 0` must map to `-k - 1`, and subtracting one ULP makes an exact negative integer such as `-1.0` land in texel 0 rather than 1. The test coordinates include `-1.0`, `-2.0`, `-width`, `-0.25`, `-0.75`, `-3.25`, so the boundary case is exercised. I'd still put that reasoning in a one-line debt note, because the next reader will make the same mistake I nearly did. **Other checks:** - The new specialization id `57728` does not collide with anything in `PipelineSpecialization.hpp`. The other per-sampler bases are 64 apart (57344, 57408, 57472) and this one jumps to 57728, which wastes the 57536 slot for no reason I can see. Harmless, but please use the next free 64-aligned id or explain the gap. - `MirrorsIfUnnormalized` excludes depth-compare, table-indexed samplers and `dimension` other than 1D/2D, and the emitter additionally excludes cube, so unsupported shapes keep the old rejection path rather than silently sampling wrongly. - Real-GPU verification exists in the thread (RADV on an RX Vega 56, with validation layers). **One caveat to keep in view:** the debt entry says the behaviour follows RDNA2 gfx1036 oracle measurements, not gfx1013 (the PS5 GPU family). That is disclosed, which is the honest way to do it, and I'd leave the caveat exactly as written. Approving. AI-assisted: yes. Written with AI assistance and reviewed by me.
CnRJay commented 2026-11-01 19:48:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:48:09Z.

Review: COMMENTED


Withdrawn. The validation comparison this review asked for is already in the PR's Tested section: main vs this change, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, no new VUID-. The title-ID demand was not a CONTRIBUTING requirement.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729918118 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11126#pullrequestreview-5729918118). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:48:09Z. **Review:** COMMENTED --- Withdrawn. The validation comparison this review asked for is already in the PR's `Tested` section: `main` vs this change, `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, no new `VUID-`. The title-ID demand was not a CONTRIBUTING requirement.
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!11126
No description provided.