fix(shader): convert formatted buffer stores to the element format #330

Merged
adriadam10 merged 3 commits from upstream-pr/330 into main 2026-10-04 09:53:55 +00:00
adriadam10 commented 2026-10-02 06:31:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T06:31:13Z.


What

Bug: formatted buffer stores (buffer_store_format_* with the descriptor's format, tbuffer_store_format_* with the instruction's) did not convert anything:

  • An 8_UNORM store of 1.0f wrote 0x00, the low byte of 0x3f800000. The hardware writes 0xff.
  • Packed formats (2_10_10_10, 10_11_11) wrote the full register into the word, once per component.
  • Components the instruction doesn't provide were left untouched. The hardware writes the whole element: store_format_x on 8_8_8_8 writes 0x000000ff, and on 32_32 it zeroes the second dword.

Only 32-bit FLOAT/UINT/SINT stores were right, which is likely why nothing noticed.

Semantics measured on an RDNA2 GPU (ROCm HSA, raw kernels; 2048 vectors per format, all matched by a Python model):

Component type Stored value
UNORM NaN → 0, clamp to [0, 1], round x·M exactly to nearest even
SNORM NaN → 0, clamp to [−1, 1], round x·M exactly, symmetric
USCALED / SSCALED same as UNORM / SNORM (1.0 → 0xff in 8_USCALED, 2048/2048)
UINT / SINT saturate the 32-bit register value
16-bit FLOAT, 11/10-bit UFLOAT round toward zero, finite overflow saturates, inf stays inf, NaN keeps its top payload bits (at least 1); unsigned: negatives → 0
32-bit FLOAT / UINT / SINT unchanged

Implementation:

  • EmitFormatStoreComponent encodes one component. Exact x·M uses OpUMulExtended of the mantissa and M, held as 16.16 fixed point plus a sticky bit, so it is 32-bit integer ops only. Going through an f32 multiply would round twice.
  • The f16/UFLOAT conversion generalizes the RTZ helper from #329 to any 5-bit-exponent float. #329's D16 loads use it unchanged.
  • StoreFormattedElement writes every component of the element (missing ones as 0). Packed formats are composed into a single word store. The single-component and wide store paths both use it.

Test agc_driver_formatted_store: 16 results × 32 lanes captured on the RDNA2 GPU. It covers every component type, NaN/inf/−0/denormal inputs, x/xy/xyz/xyzw stores into packed (2_10_10_10 UNORM/SNORM, 10_11_11) and plain (8_8_8_8_SINT, 16_16_FLOAT, 32_32_UINT) formats, and checks the zero fill.

Tested

Linux, full ctest (101) passes.

  • RADV (Radeon 660M) and lavapipe: the test passes.
  • Without the conversion: 8unorm is 0xaaaaaa00, expected 0xaaaaaaff. Without the zero fill: uint32x2_y is 0xaaaaaaaa, expected 0x00000000.

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: #251 (test harness), #319 → #329 (RTZ helper); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4713436603 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/330). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T06:31:13Z. --- ### What **Bug:** formatted buffer stores (`buffer_store_format_*` with the descriptor's format, `tbuffer_store_format_*` with the instruction's) did not convert anything: - An `8_UNORM` store of `1.0f` wrote `0x00`, the low byte of `0x3f800000`. The hardware writes `0xff`. - Packed formats (`2_10_10_10`, `10_11_11`) wrote the full register into the word, once per component. - Components the instruction doesn't provide were left untouched. The hardware writes the whole element: `store_format_x` on `8_8_8_8` writes `0x000000ff`, and on `32_32` it zeroes the second dword. Only 32-bit FLOAT/UINT/SINT stores were right, which is likely why nothing noticed. Semantics measured on an RDNA2 GPU (ROCm HSA, raw kernels; 2048 vectors per format, all matched by a Python model): | Component type | Stored value | |---|---| | UNORM | NaN → 0, clamp to [0, 1], round x·M **exactly** to nearest even | | SNORM | NaN → 0, clamp to [−1, 1], round x·M exactly, symmetric | | USCALED / SSCALED | **same as UNORM / SNORM** (1.0 → `0xff` in `8_USCALED`, 2048/2048) | | UINT / SINT | saturate the 32-bit register value | | 16-bit FLOAT, 11/10-bit UFLOAT | round toward zero, finite overflow saturates, inf stays inf, NaN keeps its top payload bits (at least 1); unsigned: negatives → 0 | | 32-bit FLOAT / UINT / SINT | unchanged | Implementation: - `EmitFormatStoreComponent` encodes one component. Exact x·M uses `OpUMulExtended` of the mantissa and M, held as 16.16 fixed point plus a sticky bit, so it is 32-bit integer ops only. Going through an f32 multiply would round twice. - The f16/UFLOAT conversion generalizes the RTZ helper from #329 to any 5-bit-exponent float. #329's D16 loads use it unchanged. - `StoreFormattedElement` writes every component of the element (missing ones as 0). Packed formats are composed into a single word store. The single-component and wide store paths both use it. Test `agc_driver_formatted_store`: 16 results × 32 lanes **captured on the RDNA2 GPU**. It covers every component type, NaN/inf/−0/denormal inputs, `x`/`xy`/`xyz`/`xyzw` stores into packed (`2_10_10_10` UNORM/SNORM, `10_11_11`) and plain (`8_8_8_8_SINT`, `16_16_FLOAT`, `32_32_UINT`) formats, and checks the zero fill. ### Tested Linux, full ctest (101) passes. - RADV (Radeon 660M) and lavapipe: the test passes. - Without the conversion: `8unorm is 0xaaaaaa00, expected 0xaaaaaaff`. Without the zero fill: `uint32x2_y is 0xaaaaaaaa, expected 0x00000000`. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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: #251 (test harness), #319 → #329 (RTZ helper); review only the last commit - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-02 06:31:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T06:31:37Z.


Progress report (d3ec1f1 → 4d90c0b)

📈 GPU shader instructions: 74.96% (+3.95%, +46 implemented instructions)

✅ 46 implemented
Encoding Name
DS DS_READ_I8_D16
DS DS_READ_I8_D16_HI
DS DS_READ_U8_D16
DS DS_READ_U8_D16_HI
DS DS_WRITE_B8_D16_HI
FLAT FLAT_LOAD_SBYTE_D16
FLAT FLAT_LOAD_SBYTE_D16_HI
FLAT FLAT_LOAD_SHORT_D16
FLAT FLAT_LOAD_SHORT_D16_HI
FLAT FLAT_LOAD_UBYTE_D16
FLAT FLAT_LOAD_UBYTE_D16_HI
FLAT FLAT_STORE_BYTE_D16_HI
FLAT FLAT_STORE_SHORT_D16_HI
GLOBAL GLOBAL_LOAD_SBYTE_D16
GLOBAL GLOBAL_LOAD_SBYTE_D16_HI
GLOBAL GLOBAL_LOAD_SHORT_D16
GLOBAL GLOBAL_LOAD_SHORT_D16_HI
GLOBAL GLOBAL_LOAD_UBYTE_D16
GLOBAL GLOBAL_LOAD_UBYTE_D16_HI
GLOBAL GLOBAL_STORE_BYTE_D16_HI
GLOBAL GLOBAL_STORE_SHORT_D16_HI
MTBUF TBUFFER_LOAD_FORMAT_D16_X
MTBUF TBUFFER_LOAD_FORMAT_D16_XY
MTBUF TBUFFER_LOAD_FORMAT_D16_XYZ
MTBUF TBUFFER_LOAD_FORMAT_D16_XYZW
MUBUF BUFFER_LOAD_FORMAT_D16_HI_X
MUBUF BUFFER_LOAD_FORMAT_D16_X
MUBUF BUFFER_LOAD_FORMAT_D16_XY
MUBUF BUFFER_LOAD_FORMAT_D16_XYZ
MUBUF BUFFER_LOAD_FORMAT_D16_XYZW
MUBUF BUFFER_LOAD_SBYTE_D16
MUBUF BUFFER_LOAD_SBYTE_D16_HI
MUBUF BUFFER_LOAD_SHORT_D16
MUBUF BUFFER_LOAD_SHORT_D16_HI
MUBUF BUFFER_LOAD_UBYTE_D16
MUBUF BUFFER_LOAD_UBYTE_D16_HI
MUBUF BUFFER_STORE_BYTE_D16_HI
MUBUF BUFFER_STORE_SHORT_D16_HI
SCRATCH SCRATCH_LOAD_SBYTE_D16
SCRATCH SCRATCH_LOAD_SBYTE_D16_HI
SCRATCH SCRATCH_LOAD_SHORT_D16
SCRATCH SCRATCH_LOAD_SHORT_D16_HI
SCRATCH SCRATCH_LOAD_UBYTE_D16
SCRATCH SCRATCH_LOAD_UBYTE_D16_HI
SCRATCH SCRATCH_STORE_BYTE_D16_HI
SCRATCH SCRATCH_STORE_SHORT_D16_HI
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5946761878 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/330#issuecomment-5946761878). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T06:31:37Z. --- <!-- progress-report --> ### Progress report (d3ec1f1 → 4d90c0b) 📈 **GPU shader instructions**: 74.96% (+3.95%, +46 implemented instructions) <details> <summary>✅ 46 implemented</summary> | Encoding | Name | | - | - | | DS | `DS_READ_I8_D16` | | DS | `DS_READ_I8_D16_HI` | | DS | `DS_READ_U8_D16` | | DS | `DS_READ_U8_D16_HI` | | DS | `DS_WRITE_B8_D16_HI` | | FLAT | `FLAT_LOAD_SBYTE_D16` | | FLAT | `FLAT_LOAD_SBYTE_D16_HI` | | FLAT | `FLAT_LOAD_SHORT_D16` | | FLAT | `FLAT_LOAD_SHORT_D16_HI` | | FLAT | `FLAT_LOAD_UBYTE_D16` | | FLAT | `FLAT_LOAD_UBYTE_D16_HI` | | FLAT | `FLAT_STORE_BYTE_D16_HI` | | FLAT | `FLAT_STORE_SHORT_D16_HI` | | GLOBAL | `GLOBAL_LOAD_SBYTE_D16` | | GLOBAL | `GLOBAL_LOAD_SBYTE_D16_HI` | | GLOBAL | `GLOBAL_LOAD_SHORT_D16` | | GLOBAL | `GLOBAL_LOAD_SHORT_D16_HI` | | GLOBAL | `GLOBAL_LOAD_UBYTE_D16` | | GLOBAL | `GLOBAL_LOAD_UBYTE_D16_HI` | | GLOBAL | `GLOBAL_STORE_BYTE_D16_HI` | | GLOBAL | `GLOBAL_STORE_SHORT_D16_HI` | | MTBUF | `TBUFFER_LOAD_FORMAT_D16_X` | | MTBUF | `TBUFFER_LOAD_FORMAT_D16_XY` | | MTBUF | `TBUFFER_LOAD_FORMAT_D16_XYZ` | | MTBUF | `TBUFFER_LOAD_FORMAT_D16_XYZW` | | MUBUF | `BUFFER_LOAD_FORMAT_D16_HI_X` | | MUBUF | `BUFFER_LOAD_FORMAT_D16_X` | | MUBUF | `BUFFER_LOAD_FORMAT_D16_XY` | | MUBUF | `BUFFER_LOAD_FORMAT_D16_XYZ` | | MUBUF | `BUFFER_LOAD_FORMAT_D16_XYZW` | | MUBUF | `BUFFER_LOAD_SBYTE_D16` | | MUBUF | `BUFFER_LOAD_SBYTE_D16_HI` | | MUBUF | `BUFFER_LOAD_SHORT_D16` | | MUBUF | `BUFFER_LOAD_SHORT_D16_HI` | | MUBUF | `BUFFER_LOAD_UBYTE_D16` | | MUBUF | `BUFFER_LOAD_UBYTE_D16_HI` | | MUBUF | `BUFFER_STORE_BYTE_D16_HI` | | MUBUF | `BUFFER_STORE_SHORT_D16_HI` | | SCRATCH | `SCRATCH_LOAD_SBYTE_D16` | | SCRATCH | `SCRATCH_LOAD_SBYTE_D16_HI` | | SCRATCH | `SCRATCH_LOAD_SHORT_D16` | | SCRATCH | `SCRATCH_LOAD_SHORT_D16_HI` | | SCRATCH | `SCRATCH_LOAD_UBYTE_D16` | | SCRATCH | `SCRATCH_LOAD_UBYTE_D16_HI` | | SCRATCH | `SCRATCH_STORE_BYTE_D16_HI` | | SCRATCH | `SCRATCH_STORE_SHORT_D16_HI` | </details>
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!330
No description provided.