xshader: fill texture size and buffer record count fields read from a descriptor #315

Merged
NickStallman merged 7 commits from upstream-pr/315 into main 2026-11-27 18:36:29 +00:00
NickStallman commented 2026-11-27 17:16:59 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-27T17:16:59Z.


A console program can read a texture's size or a buffer's record count out of the descriptor it loaded, instead of asking for it. A descriptor load writes nothing in the translation, so that arithmetic ran on whatever the registers held, usually zero: a 1×1 texture, or an empty buffer. The result was a translation that ran and computed with the wrong numbers.

Texture sizes (e8fb84e6). The image descriptor keeps three fields (WIDTH_LO, WIDTH_HI, HEIGHT and BASE_LEVEL of SQ_IMG_RSRC_WORD1..3 in Mesa's gfx10-rsrc.json):

  • the width less one, in bits 30–31 of word 1 and the low bits of word 2;
  • the height less one, from bit 14 of word 2;
  • the view's first mip level, in bits 12–15 of word 3.

Compiled code computes (field + 1) >> level. The load now fills those fields from GetDimensions, with the size of the view's first mip and level 0. That gives the console's result for this computation only. So the values are followed from the descriptor words to the shift, across control flow, and a program that reads one of them any other way is refused.

Buffer record counts (1ad86d0b, f7e404cd). Word 2 of a buffer descriptor (SQ_BUF_RSRC_WORD2) is the record count, which is what GetDimensions returned to the program's source: bytes for a raw buffer, elements for a structured or typed one.

  • The load now fills word 2 from GetDimensions of the buffer the translation declares for that slot. The view type PSV names decides which buffer that is. The count is the whole word, so every later use of it is right.
  • Where the read is certain (in the same straight run as the load) and no single host buffer answers for the descriptor, the program is refused. Where the read is only possible, nothing changes.
  • A structured buffer is counted only with the stride the program's records give, because a host may divide the view's size by the element the shader declares.
  • A buffer a program asks only for its count is declared for the query, where the root declares the slot and PSV names the view.

Shared helpers (80c57b38): a new hlsl/operand_fields.rs says which source fields an instruction reads, whether it touches memory, where the straight run before an instruction starts, and what literal a register holds there. The three-operand encoding has three source fields whatever the instruction reads, and an unread field decodes as s0. The number of fields read now follows the opcode, so a one- or two-operand instruction no longer looks like a read of s0. This module is shared with the guest-memory work, which isn't part of this PR.

Corpus effect against 65e3e48 (27,116 recorded ProjectScarlett requests, .wave32 ones with --wave32-host):

  • 0 recovered.
  • 1,944 changed: every program that reads a size or a count from a descriptor now gets the real value.
  • 19 translated programs are now refused:
    • 14 × "buffer record count read from a descriptor with no one host buffer";
    • 3 × "texture size field from a descriptor used outside its size computation";
    • 2 × "texture size read from a descriptor that is not a texture".

Before, those 19 ran with a size or count of zero.

The same port stacked on the guest-memory branch changes a strict superset of these requests, plus 6 that depend on that branch's code. So this standalone form changes nothing that one didn't.

Tests (src/tests/hlsl/descriptor_size.rs, src/tests/hlsl/operand_fields.rs):

  • the load fills the size fields from the texture;
  • a descriptor used only as a descriptor isn't filled;
  • a size value used outside the computation is refused;
  • a size value lost at a join is refused only when read;
  • the mask may be held in a register;
  • the load fills a buffer's record count;
  • a buffer used only as a buffer isn't asked for its count;
  • a buffer asked only for its count is declared for the query;
  • a count with no host buffer to ask is refused;
  • unused source fields aren't operands, and a constant is known only where every path ran its move.

Two GPU checks compare the translated computation with real views of known size: tools/check_descriptor_size.py (--only descriptor-size, 9 cases) and tools/check_buffer_count.py (--only buffer-count, 15 cases: raw, structured and typed views up to the largest the check allocates).

Per commit (against its parent on 65e3e48, 27,116 recorded requests):

Commit What it does Changed Newly refused Translates again
80c57b38 shared operand-field module 0 (byte-identical) 0 0
e8fb84e6 texture size fields 1,718 5 0
1ad86d0b buffer record counts 654 15 0
f7e404cd structured stride, count-only buffer 0 0 1 (refused by 1ad86d0b)

The two filling commits overlap, so 1,944 requests change in total.

Checks

  • python tuxbox.py check --no-gpu on f7e404c: passed, all six steps.
  • AMD and NVIDIA full checks passed on the same commits on 95480b5 (94e4dfa), 146 gates each. descriptor-size: 9 cases, 0 mismatches; buffer-count: 15 cases, 0 mismatches.

🤖 Generated with Claude Code

<!-- greyfinch-sync:brcly/TuxBox:issue:5530527524 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/315). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-27T17:16:59Z. --- A console program can read a texture's size or a buffer's record count out of the descriptor it loaded, instead of asking for it. A descriptor load writes nothing in the translation, so that arithmetic ran on whatever the registers held, usually zero: a 1×1 texture, or an empty buffer. The result was a translation that ran and computed with the wrong numbers. **Texture sizes** (`e8fb84e6`). The image descriptor keeps three fields (`WIDTH_LO`, `WIDTH_HI`, `HEIGHT` and `BASE_LEVEL` of `SQ_IMG_RSRC_WORD1..3` in Mesa's `gfx10-rsrc.json`): - the width less one, in bits 30–31 of word 1 and the low bits of word 2; - the height less one, from bit 14 of word 2; - the view's first mip level, in bits 12–15 of word 3. Compiled code computes `(field + 1) >> level`. The load now fills those fields from `GetDimensions`, with the size of the view's first mip and level 0. That gives the console's result for this computation only. So the values are followed from the descriptor words to the shift, across control flow, and a program that reads one of them any other way is refused. **Buffer record counts** (`1ad86d0b`, `f7e404cd`). Word 2 of a buffer descriptor (`SQ_BUF_RSRC_WORD2`) is the record count, which is what `GetDimensions` returned to the program's source: bytes for a raw buffer, elements for a structured or typed one. - The load now fills word 2 from `GetDimensions` of the buffer the translation declares for that slot. The view type PSV names decides which buffer that is. The count is the whole word, so every later use of it is right. - Where the read is certain (in the same straight run as the load) and no single host buffer answers for the descriptor, the program is refused. Where the read is only possible, nothing changes. - A structured buffer is counted only with the stride the program's records give, because a host may divide the view's size by the element the shader declares. - A buffer a program asks only for its count is declared for the query, where the root declares the slot and PSV names the view. **Shared helpers** (`80c57b38`): a new `hlsl/operand_fields.rs` says which source fields an instruction reads, whether it touches memory, where the straight run before an instruction starts, and what literal a register holds there. The three-operand encoding has three source fields whatever the instruction reads, and an unread field decodes as `s0`. The number of fields read now follows the opcode, so a one- or two-operand instruction no longer looks like a read of `s0`. This module is shared with the guest-memory work, which isn't part of this PR. **Corpus effect** against `65e3e48` (27,116 recorded ProjectScarlett requests, `.wave32` ones with `--wave32-host`): - 0 recovered. - 1,944 changed: every program that reads a size or a count from a descriptor now gets the real value. - 19 translated programs are now refused: - 14 × "buffer record count read from a descriptor with no one host buffer"; - 3 × "texture size field from a descriptor used outside its size computation"; - 2 × "texture size read from a descriptor that is not a texture". Before, those 19 ran with a size or count of zero. The same port stacked on the guest-memory branch changes a strict superset of these requests, plus 6 that depend on that branch's code. So this standalone form changes nothing that one didn't. **Tests** (`src/tests/hlsl/descriptor_size.rs`, `src/tests/hlsl/operand_fields.rs`): - the load fills the size fields from the texture; - a descriptor used only as a descriptor isn't filled; - a size value used outside the computation is refused; - a size value lost at a join is refused only when read; - the mask may be held in a register; - the load fills a buffer's record count; - a buffer used only as a buffer isn't asked for its count; - a buffer asked only for its count is declared for the query; - a count with no host buffer to ask is refused; - unused source fields aren't operands, and a constant is known only where every path ran its move. Two GPU checks compare the translated computation with real views of known size: `tools/check_descriptor_size.py` (`--only descriptor-size`, 9 cases) and `tools/check_buffer_count.py` (`--only buffer-count`, 15 cases: raw, structured and typed views up to the largest the check allocates). **Per commit** (against its parent on `65e3e48`, 27,116 recorded requests): | Commit | What it does | Changed | Newly refused | Translates again | |---|---|---|---|---| | `80c57b38` | shared operand-field module | 0 (byte-identical) | 0 | 0 | | `e8fb84e6` | texture size fields | 1,718 | 5 | 0 | | `1ad86d0b` | buffer record counts | 654 | 15 | 0 | | `f7e404cd` | structured stride, count-only buffer | 0 | 0 | 1 (refused by `1ad86d0b`) | The two filling commits overlap, so 1,944 requests change in total. **Checks** - `python tuxbox.py check --no-gpu` on `f7e404c`: passed, all six steps. - AMD and NVIDIA full checks passed on the same commits on `95480b5` (`94e4dfa`), 146 gates each. `descriptor-size`: 9 cases, 0 mismatches; `buffer-count`: 15 cases, 0 mismatches. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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
brcly/TuxBox!315
No description provided.