shader: support BUF_FMT_16_16_SNORM fetch format in gfx2hlsl (Part of #1723) #1998

Closed
tedin7 wants to merge 1 commit from upstream-pr/2005 into main
tedin7 commented 2026-11-24 22:57:52 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T22:57:52Z.


Part of #1723

What this unblocks: typed buffer loads of BUF_FMT_16_16_SNORM now translate in gfx2hlsl.py instead of being refused with buffer format BUF_FMT_16_16_SNORM.

Change:

  • FETCH_FORMATS gets BUF_FMT_16_16_SNORM: one unaligned dword load, each half sign-extended, divided by 32767 and clamped at -1.0. Missing z reads 0 and missing w reads 1.0, the same default as the other formats.
  • New test startup_gfx2hlsl_tbuffer_16_16_snorm (registered in CMake). The instruction bytes are built in the test from the decoder's field layout; no game-derived bytes.

Verification:

  • Linux dev host (translator is Python; no Win2025 run needed for this table entry, the generated HLSL was not compiled on Win2025):
    • python3 -B tests/startup_gfx2hlsl_tbuffer_16_16_snorm_check.py with the fix: PASS (10 checks ok).
    • Same command on the unfixed gfx2hlsl.py: fails with Unsupported: buffer format BUF_FMT_16_16_SNORM.
    • python3 -B on startup_gfx2hlsl_tbuffer_16f_check.py, startup_gfx2hlsl_refusals_check.py, startup_gfx2hlsl_op_refusals_check.py: exit 0.
    • git diff --check: clean.
  • Win2025: not run. Suggested: ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_tbuffer_16_16_snorm$' -V.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6593095903 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2005). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T22:57:52Z. --- Part of #1723 What this unblocks: typed buffer loads of `BUF_FMT_16_16_SNORM` now translate in `gfx2hlsl.py` instead of being refused with `buffer format BUF_FMT_16_16_SNORM`. Change: - `FETCH_FORMATS` gets `BUF_FMT_16_16_SNORM`: one unaligned dword load, each half sign-extended, divided by 32767 and clamped at -1.0. Missing z reads 0 and missing w reads 1.0, the same default as the other formats. - New test `startup_gfx2hlsl_tbuffer_16_16_snorm` (registered in CMake). The instruction bytes are built in the test from the decoder's field layout; no game-derived bytes. Verification: - Linux dev host (translator is Python; no Win2025 run needed for this table entry, the generated HLSL was not compiled on Win2025): - `python3 -B tests/startup_gfx2hlsl_tbuffer_16_16_snorm_check.py` with the fix: PASS (10 checks ok). - Same command on the unfixed `gfx2hlsl.py`: fails with `Unsupported: buffer format BUF_FMT_16_16_SNORM`. - `python3 -B` on `startup_gfx2hlsl_tbuffer_16f_check.py`, `startup_gfx2hlsl_refusals_check.py`, `startup_gfx2hlsl_op_refusals_check.py`: exit 0. - `git diff --check`: clean. - Win2025: not run. Suggested: `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_tbuffer_16_16_snorm$' -V`. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-24 23:03:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T23:00:28Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2d67c89ec2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6048760277 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2005#pullrequestreview-6048760277). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T23:00:28Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `2d67c89ec2` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". </details>
chatgpt-codex-connector[bot commented 2026-11-24 23:03:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T22:58:01Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T23:00:30.543664Z 2d67c89 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7146854305 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2005#issuecomment-7146854305). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T22:58:01Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"2d67c89ec218e2fb4aa8bc579846dfd90d4ae397","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T23:00:30.543664Z">2026-11-24T23:00:30.543664Z</relative-time> | `2d67c89` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
chatgpt-codex-connector[bot commented 2026-11-24 23:03:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T23:00:28Z.

Code review location: tools/startup/gfx2hlsl.py; line 89

@@ -84,6 +84,9 @@ def strip_header(code):
     'BUF_FMT_8_UNORM': ('ldu({0}, a)', ['U((w & 255) / 255.0)', '0u', '0u', 'U(1.0)']),
     'BUF_FMT_8_UINT': ('{0}.Load(a & ~3u) >> ((a & 3u) * 8u)', ['w & 255u', '0u', '0u', '1u']),
     'BUF_FMT_16_16_UNORM': ('ldu({0}, a)', ['U((w & 65535) / 65535.0)', 'U((w >> 16) / 65535.0)', '0u', 'U(1.0)']),
+    # Signed 16-bit fields: sign-extend, divide by 32767, and clamp -32768 to -1.0 (the SNORM rule).
+    'BUF_FMT_16_16_SNORM': ('ldu({0}, a)', ['U(max(float(asint(w << 16) >> 16) / 32767.0, -1.0))',
+                                            'U(max(float(asint(w) >> 16) / 32767.0, -1.0))', '0u', 'U(1.0)']),

P1 Badge Document the new SNORM typed-buffer capability

This changes a previously refused typed-buffer format into a supported one, but neither README.md nor docs/experiments.md was updated; a repository-wide search finds no documentation for the new format or test. AGENTS.md requires a documentation update when a public capability or reproduction command changes. Add an experiment note describing the supported format and test invocation, distinguishing the passing decode/emitted-HLSL checks from the unrun shader-compilation and GPU checks, so contributors can discover and reproduce the supported scope.

AGENTS.md reference: AGENTS.md:L207-L208

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4687708391 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2005#discussion_r4687708391). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T23:00:28Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 89 ~~~~diff @@ -84,6 +84,9 @@ def strip_header(code): 'BUF_FMT_8_UNORM': ('ldu({0}, a)', ['U((w & 255) / 255.0)', '0u', '0u', 'U(1.0)']), 'BUF_FMT_8_UINT': ('{0}.Load(a & ~3u) >> ((a & 3u) * 8u)', ['w & 255u', '0u', '0u', '1u']), 'BUF_FMT_16_16_UNORM': ('ldu({0}, a)', ['U((w & 65535) / 65535.0)', 'U((w >> 16) / 65535.0)', '0u', 'U(1.0)']), + # Signed 16-bit fields: sign-extend, divide by 32767, and clamp -32768 to -1.0 (the SNORM rule). + 'BUF_FMT_16_16_SNORM': ('ldu({0}, a)', ['U(max(float(asint(w << 16) >> 16) / 32767.0, -1.0))', + 'U(max(float(asint(w) >> 16) / 32767.0, -1.0))', '0u', 'U(1.0)']), ~~~~ --- **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Document the new SNORM typed-buffer capability** This changes a previously refused typed-buffer format into a supported one, but neither `README.md` nor `docs/experiments.md` was updated; a repository-wide search finds no documentation for the new format or test. `AGENTS.md` requires a documentation update when a public capability or reproduction command changes. Add an experiment note describing the supported format and test invocation, distinguishing the passing decode/emitted-HLSL checks from the unrun shader-compilation and GPU checks, so contributors can discover and reproduce the supported scope. AGENTS.md reference: [AGENTS.md:L207-L208](https://github.com/R0Y00/Xbox2Windows/blob/2d67c89ec218e2fb4aa8bc579846dfd90d4ae397/AGENTS.md#L207-L208) Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
upstream-sync closed this pull request 2026-11-25 01:01:01 +00:00
ChazY11 commented 2026-11-25 01:01:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T23:22:15Z.


Ran both, broke each one four ways — all caught — and checked the SNORM mapping over all 65,536 halves

Two small format additions, reviewed together because they share FETCH_FORMATS and both are Part of #1723.

# BUF_FMT_16_16_SNORM
out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_tbuffer_16_16_snorm_check.py   # exit 0
# BUF_FMT_8_8_8_8_UINT
out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_buf_8888_uint_check.py          # exit 0
mutation result
SNORM: x and y halves swapped caught
SNORM: the -1.0 clamp removed caught
SNORM: / 32767.0 → / 32768.0 caught
SNORM: x read with asint(w & 0xffffu) instead of the sign-extending shift caught
UINT: x and y channels swapped caught
UINT: z loses its & 255u caught
UINT: w becomes w >> 8 caught

Both fixtures carry the boundary values, not just mid-range ones — the sign-extending shift and the removed clamp are exactly the two that would
survive a fixture built from small positives.

The SNORM mapping, checked exhaustively

I reimplemented the emitted expression (max(float(asint(w << 16) >> 16) / 32767.0, -1.0)) and compared it with the DXGI SNORM rule over every
16-bit half:

values checked: 65536  mismatches: 0  worst delta: 0

0x0000 -> 0.0000000      0x7fff -> 1.0000000       0x8000 -> -1.0000000
0x8001 -> -1.0000000     0xffff -> -0.0000305      0x4000 -> 0.5000153
-32768 with the clamp removed -> -1.0000305 (outside SNORM)

-32768 is the only input that needs the clamp: /32767 gives -1.0000305, and every other half already lands inside [-1, 1]. That is worth a
sentence in the description, because it explains why the clamp is a max on one specific value rather than a general guard, and why a reader who
replaces it with saturate()-style clamping would change nothing for 65,535 of the 65,536 inputs.

The +32767 → 1.0 endpoint is exact and -32767 → -1.0 too, so both extremes of the representable range are hit exactly, which is the property a
signed-normalised vertex attribute depends on.

UINT

w & 255u, (w >> 8) & 255u, (w >> 16) & 255u, w >> 24 — the last channel needs no mask because the shift already bounds it to 0..255, and a
mask there would be dead code rather than a bug. The three masks that exist are each pinned by the mutations above.

Worth noting the asymmetry with SNORM in the same table: one format sign-extends and divides, the other masks and shifts, and both are now covered by
fixtures that fail when the wrong one is used.

Merge state, and the overlap I flagged earlier

Both branches are clean against main individually, but they are the pair that overlaps at lines 87–89 in FETCH_FORMATS: whichever lands second
needs a one-line rebase. Nothing else in either PR touches the other.

What is not covered

No vertex shader was compiled or run; the values above are the emitted expressions evaluated in Python, not a draw. And the z = 0u, w = 1.0
filler for the two-channel SNORM format matches the other two-channel entries — that is a convention I checked against the table, not a format
property.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7147322205 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2005#issuecomment-7147322205). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T23:22:15Z. --- ## Ran both, broke each one four ways — all caught — and checked the SNORM mapping over all 65,536 halves Two small format additions, reviewed together because they share `FETCH_FORMATS` and both are `Part of #1723`. ```powershell # BUF_FMT_16_16_SNORM out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_tbuffer_16_16_snorm_check.py # exit 0 # BUF_FMT_8_8_8_8_UINT out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_buf_8888_uint_check.py # exit 0 ``` | mutation | result | |---|---| | SNORM: x and y halves swapped | **caught** | | SNORM: the `-1.0` clamp removed | **caught** | | SNORM: `/ 32767.0` → `/ 32768.0` | **caught** | | SNORM: x read with `asint(w & 0xffffu)` instead of the sign-extending shift | **caught** | | UINT: x and y channels swapped | **caught** | | UINT: `z` loses its `& 255u` | **caught** | | UINT: `w` becomes `w >> 8` | **caught** | Both fixtures carry the boundary values, not just mid-range ones — the sign-extending shift and the removed clamp are exactly the two that would survive a fixture built from small positives. ## The SNORM mapping, checked exhaustively I reimplemented the emitted expression (`max(float(asint(w << 16) >> 16) / 32767.0, -1.0)`) and compared it with the DXGI SNORM rule over every 16-bit half: ```text values checked: 65536 mismatches: 0 worst delta: 0 0x0000 -> 0.0000000 0x7fff -> 1.0000000 0x8000 -> -1.0000000 0x8001 -> -1.0000000 0xffff -> -0.0000305 0x4000 -> 0.5000153 -32768 with the clamp removed -> -1.0000305 (outside SNORM) ``` `-32768` is the **only** input that needs the clamp: `/32767` gives `-1.0000305`, and every other half already lands inside `[-1, 1]`. That is worth a sentence in the description, because it explains why the clamp is a `max` on one specific value rather than a general guard, and why a reader who replaces it with `saturate()`-style clamping would change nothing for 65,535 of the 65,536 inputs. The `+32767 → 1.0` endpoint is exact and `-32767 → -1.0` too, so both extremes of the representable range are hit exactly, which is the property a signed-normalised vertex attribute depends on. ## UINT `w & 255u`, `(w >> 8) & 255u`, `(w >> 16) & 255u`, `w >> 24` — the last channel needs no mask because the shift already bounds it to 0..255, and a mask there would be dead code rather than a bug. The three masks that exist are each pinned by the mutations above. Worth noting the asymmetry with SNORM in the same table: one format sign-extends and divides, the other masks and shifts, and both are now covered by fixtures that fail when the wrong one is used. ## Merge state, and the overlap I flagged earlier Both branches are clean against `main` individually, but they are the pair that overlaps at lines 87–89 in `FETCH_FORMATS`: whichever lands second needs a one-line rebase. Nothing else in either PR touches the other. ## What is not covered No vertex shader was compiled or run; the values above are the emitted expressions evaluated in Python, not a draw. And the `z = 0u`, `w = 1.0` filler for the two-channel SNORM format matches the other two-channel entries — that is a convention I checked against the table, not a format property. ## Notes - AI-assisted review.
tedin7 commented 2026-11-25 01:02:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T00:30:54Z.


Closing as superseded by #2099 which also wires FETCH_DXGI 37 and the WARP/hardware vertex-fetch suite.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148586893 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2005#issuecomment-7148586893). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:30:54Z. --- Closing as superseded by #2099 which also wires FETCH_DXGI 37 and the WARP/hardware vertex-fetch suite.

Pull request closed

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
R0Y00/Xbox2Windows!1998
No description provided.