tombox-xshader: typed buffer integer formats assumed float4 (TECHNICAL_DEBT) #105

Open
opened 2026-11-26 06:10:33 +00:00 by github-bot · 4 comments
Owner

@⁠Trwthh on GitHub · 2026-11-22 09:16 UTC · original

Component: crates/tombox-xshader/src/hlsl.rs

What happens: At line 4919-4920, typed buffer (TBUFFER/MUBUF) access assumes the view format is float4:

// Typed buffer. The element type is declared float4: the view format is not
// visible in the shader, so integer-format buffers are a known gap (TECHNICAL_DEBT).

The translator infers some integer types via tbuf_ty (line 318-319) for atomics, but loads from integer-format typed buffers may misinterpret data as float, and stores may write incorrect bit patterns when the actual view format is R32_UINT, R32G32_SINT, etc.

Impact: Console games that read structured integer data through typed buffer views (common for index buffers, instance data, and compute dispatch arguments) may get garbled values. This affects both MUBUF typed loads and TBUFFER loads where the format specifies an integer type.

ISA reference: RDNA2 ISA manual, section 9.1 (Buffer Resources), nfmt field determines the data format interpretation.

Suggested fix: The shader already tracks tbuf_ty for atomic inference. Extending this to propagate the inferred integer type to typed buffer load/store emissions (using asuint() / asint() casts) would cover the common cases.

**@⁠Trwthh** on GitHub · 2026-11-22 09:16 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/105) **Component:** `crates/tombox-xshader/src/hlsl.rs` **What happens:** At line 4919-4920, typed buffer (TBUFFER/MUBUF) access assumes the view format is float4: ``` // Typed buffer. The element type is declared float4: the view format is not // visible in the shader, so integer-format buffers are a known gap (TECHNICAL_DEBT). ``` The translator infers some integer types via `tbuf_ty` (line 318-319) for atomics, but loads from integer-format typed buffers may misinterpret data as float, and stores may write incorrect bit patterns when the actual view format is R32_UINT, R32G32_SINT, etc. **Impact:** Console games that read structured integer data through typed buffer views (common for index buffers, instance data, and compute dispatch arguments) may get garbled values. This affects both MUBUF typed loads and TBUFFER loads where the format specifies an integer type. **ISA reference:** RDNA2 ISA manual, section 9.1 (Buffer Resources), `nfmt` field determines the data format interpretation. **Suggested fix:** The shader already tracks `tbuf_ty` for atomic inference. Extending this to propagate the inferred integer type to typed buffer load/store emissions (using `asuint()` / `asint()` casts) would cover the common cases.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:10:33 +00:00
Author
Owner

@⁠NemesisDawn on GitHub · 2026-11-23 03:10 UTC · original

I'm interested in helping address this with AI-assisted Rust development and independent RTX 5080 testing. Is this issue available to work on, and is the proposed integer-format handling still the preferred approach?

**@⁠NemesisDawn** on GitHub · 2026-11-23 03:10 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/105#issuecomment-7090528251) I'm interested in helping address this with AI-assisted Rust development and independent RTX 5080 testing. Is this issue available to work on, and is the proposed integer-format handling still the preferred approach?
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 03:22 UTC · original

Yes, it's still open and available. The gap is still there on current main: typed buffer loads and stores declare float4 unless the slot is used by an atomic (tbuf_uint, hlsl.rs around the TECHNICAL_DEBT note near line 5613).

What a PR needs to be accepted:

  • No guessing. The view format isn't visible in the shader, so don't infer an integer type from how the data is later used. A sound approach takes the type from something that is actually known: the MTBUF dfmt/nfmt fields of the instruction (TBUFFER loads and stores carry their format), or the buffer descriptor's format bits where the V# words are known at translate time. Keep float4 where neither is known, and record the remaining gap in docs/BLOCKERS.md instead of picking a type.
  • Tests: an exec test (tests/exec_mem) for each integer nfmt you handle (UINT, SINT, USCALED or SSCALED if you cover them), checked bit-exact on WARP, plus a case showing the float path is unchanged. A hardware run on your RTX 5080 on top is welcome.
  • The normal rules: no comment lines in code, cargo test -p tombox-xshader passes, and the PR names the ISA section it follows.

It changes translator output, so it'll get a review from the graphics lead before merge.

**@⁠omonkeyz** on GitHub · 2026-11-23 03:22 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/105#issuecomment-7090696724) Yes, it's still open and available. The gap is still there on current main: typed buffer loads and stores declare float4 unless the slot is used by an atomic (`tbuf_uint`, hlsl.rs around the TECHNICAL_DEBT note near line 5613). What a PR needs to be accepted: - No guessing. The view format isn't visible in the shader, so don't infer an integer type from how the data is later used. A sound approach takes the type from something that is actually known: the MTBUF dfmt/nfmt fields of the instruction (TBUFFER loads and stores carry their format), or the buffer descriptor's format bits where the V# words are known at translate time. Keep float4 where neither is known, and record the remaining gap in docs/BLOCKERS.md instead of picking a type. - Tests: an exec test (`tests/exec_mem`) for each integer nfmt you handle (UINT, SINT, USCALED or SSCALED if you cover them), checked bit-exact on WARP, plus a case showing the float path is unchanged. A hardware run on your RTX 5080 on top is welcome. - The normal rules: no comment lines in code, `cargo test -p tombox-xshader` passes, and the PR names the ISA section it follows. It changes translator output, so it'll get a review from the graphics lead before merge.
Author
Owner

@⁠NemesisDawn on GitHub · 2026-11-23 03:59 UTC · original

I've traced the typed-buffer handling through hlsl.rs, mtbuf.rs, shader_xlat.rs, and the D3D12 descriptor tracking code.

The MTBUF path already decodes the numeric format from the instruction. However, for typed MUBUF loads/stores, infer_tbuf_types() relies on register-use evidence and defaults to float4 when the type is unknown.

I also found that bindtrace records SRV/UAV descriptor information, but shader translation occurs separately during pipeline creation, so the actual bound view format isn't currently passed to the translator.

Before implementing a fix, would you prefer a narrower change to the translator's type handling, or is the intended solution to specialize shader translation using the runtime view format?

I'd like to avoid introducing another heuristic or breaking existing floating-point buffers. I can add synthetic regression tests covering UINT, SINT, and FLOAT once the intended approach is clear.

**@⁠NemesisDawn** on GitHub · 2026-11-23 03:59 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/105#issuecomment-7091185889) I've traced the typed-buffer handling through `hlsl.rs`, `mtbuf.rs`, `shader_xlat.rs`, and the D3D12 descriptor tracking code. The MTBUF path already decodes the numeric format from the instruction. However, for typed MUBUF loads/stores, `infer_tbuf_types()` relies on register-use evidence and defaults to `float4` when the type is unknown. I also found that `bindtrace` records SRV/UAV descriptor information, but shader translation occurs separately during pipeline creation, so the actual bound view format isn't currently passed to the translator. Before implementing a fix, would you prefer a narrower change to the translator's type handling, or is the intended solution to specialize shader translation using the runtime view format? I'd like to avoid introducing another heuristic or breaking existing floating-point buffers. I can add synthetic regression tests covering UINT, SINT, and FLOAT once the intended approach is clear.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 04:32 UTC · original

Thanks for tracing it. The graphics lead's answer is to specialize on the runtime view format, scoped tightly:

  1. Specialize only on 3 view-format classes: float, uint and sint.
  2. Only for shaders with a typed-buffer access that infer_tbuf_types() has no evidence for. Every other shader keeps a single variant.
  3. Use formats the instruction already gives first: MTBUF dfmt/nfmt, and any descriptor that is known statically. Use the runtime class only when neither exists.
  4. Put the class in the translation key, so the shader cache stays deterministic.
  5. Put it behind an env gate that is off by default.
  6. Include synthetic UINT, SINT and FLOAT tests that are bit-exact on WARP, run through the exec harness.
  7. Before it becomes the default, we'll run an A/B on our side: pipeline count, first-bind waits and fps.

The PR then gets a graphics-lead review. Your plan to avoid a new heuristic and leave float buffers unchanged fits this.

**@⁠omonkeyz** on GitHub · 2026-11-23 04:32 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/105#issuecomment-7091670763) Thanks for tracing it. The graphics lead's answer is to specialize on the runtime view format, scoped tightly: 1. Specialize only on 3 view-format classes: float, uint and sint. 2. Only for shaders with a typed-buffer access that `infer_tbuf_types()` has no evidence for. Every other shader keeps a single variant. 3. Use formats the instruction already gives first: MTBUF dfmt/nfmt, and any descriptor that is known statically. Use the runtime class only when neither exists. 4. Put the class in the translation key, so the shader cache stays deterministic. 5. Put it behind an env gate that is off by default. 6. Include synthetic UINT, SINT and FLOAT tests that are bit-exact on WARP, run through the exec harness. 7. Before it becomes the default, we'll run an A/B on our side: pipeline count, first-bind waits and fps. The PR then gets a graphics-lead review. Your plan to avoid a new heuristic and leave float buffers unchanged fits this.
This discussion has been locked. Commenting is limited to contributors.
No description provided.