shader: refuse view-address fields the table gives as 0; test a shader that needs both tables (follow-up to #311's merge) #311

Merged
Olima34 merged 4 commits from upstream-pr/311 into main 2026-11-27 16:04:49 +00:00
Olima34 commented 2026-11-27 10:56:21 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T10:56:21Z.


Current head (3cc078cf, rebased on main with #264 and #304): opens 44 programs (42 compute, 2 pixel), and refuses any read of a field the table gives as 0 (3fa85bb4). See the latest comment.

Refs #306.

The native host refused every shader that needs the view-address table (root SRV t1, space 2), which the D3D12 path builds and binds. This adds it to the native host.

What changes:

  • One table per resource heap (NativeHeap::addresses), made by NativeHost::view_addresses the first time a shader needs it. It's filled from the heap's records with four dwords per entry:
    • a buffer view's title address;
    • a texture view's size fields, through descriptor::texture_words, moved out of the D3D12-only descriptor/host.rs so both paths share it;
    • zeros for anything else.
  • Kept up to date in NativeHost::mirror once made, and bound after the page table, for compute and for vertex/pixel programs.
  • unsupported() now refuses only write marks ("needs write marks").
  • Pages::write_at is the same helper #264 adds, so the two should merge cleanly there.

Test. heap_tests::a_compute_shader_reads_title_memory_at_a_heap_views_address runs a compute that reads an SRV's address from t1, loads the word there (0x12345678) and stores it. It fails two ways without the full change:

  • without it at all, the shader is refused;
  • with the refusal lifted but no table, the converter refuses the pipeline ("SRV space 2 register 1 … not referenced by the root signature").

In the title. 7 compute shaders were refused for this reason alone in our boot dumps: 00bc6a9b, 0c748826, 48828947, 550d92da, a2d0209f, c3873dc0 and fcd8ef10. Six of them read a buffer view's address and walk title memory through the page table, which looks like BVH traversal. On main + this + #304, all 7 now make their Metal pipelines (pipeline creation only, nothing run). We haven't measured them in game: game runs are paused on our Mac for now (#79).

Memory: 16 bytes per entry, only for resource heaps that such a shader reads through. That's 16 MiB per 2^20 entries. Our world heap reaches at least index 505,076, so at least 7.7 MiB.

Checks: victory-gpu 320, victory-shader 332 and victory-hle 141 tests pass, and victory and xtask pass. Windows clippy is clean with -D warnings, and cargo xtask policy passes. docs/native-host.md has a new section, "View addresses on the native host (#306)".

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5525444196 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/311). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T10:56:21Z. --- > **Current head (3cc078cf, rebased on main with #264 and #304):** opens 44 programs (42 compute, 2 pixel), and refuses any read of a field the table gives as 0 (3fa85bb4). See the latest comment. Refs #306. The native host refused every shader that needs the view-address table (root SRV `t1`, space 2), which the D3D12 path builds and binds. This adds it to the native host. **What changes:** - **One table per resource heap** (`NativeHeap::addresses`), made by `NativeHost::view_addresses` the first time a shader needs it. It's filled from the heap's records with four dwords per entry: - a buffer view's title address; - a texture view's size fields, through `descriptor::texture_words`, moved out of the D3D12-only `descriptor/host.rs` so both paths share it; - zeros for anything else. - **Kept up to date** in `NativeHost::mirror` once made, and **bound** after the page table, for compute and for vertex/pixel programs. - **`unsupported()`** now refuses only write marks ("needs write marks"). - **`Pages::write_at`** is the same helper #264 adds, so the two should merge cleanly there. **Test.** `heap_tests::a_compute_shader_reads_title_memory_at_a_heap_views_address` runs a compute that reads an SRV's address from `t1`, loads the word there (0x12345678) and stores it. It fails two ways without the full change: - without it at all, the shader is refused; - with the refusal lifted but no table, the converter refuses the pipeline ("SRV space 2 register 1 … not referenced by the root signature"). **In the title.** 7 compute shaders were refused for this reason alone in our boot dumps: `00bc6a9b`, `0c748826`, `48828947`, `550d92da`, `a2d0209f`, `c3873dc0` and `fcd8ef10`. Six of them read a buffer view's address and walk title memory through the page table, which looks like BVH traversal. On main + this + #304, all 7 now make their Metal pipelines (pipeline creation only, nothing run). We haven't measured them in game: game runs are paused on our Mac for now (#79). **Memory:** 16 bytes per entry, only for resource heaps that such a shader reads through. That's 16 MiB per 2^20 entries. Our world heap reaches at least index 505,076, so at least 7.7 MiB. **Checks:** victory-gpu 320, victory-shader 332 and victory-hle 141 tests pass, and victory and xtask pass. Windows clippy is clean with `-D warnings`, and `cargo xtask policy` passes. `docs/native-host.md` has a new section, "View addresses on the native host (#306)".
LakshanSivakumar commented 2026-11-27 11:54:11 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T11:54:11Z.


Thank you. The test with real values (the word read at a view's address) is what I wanted.

Two things before I merge it.

  1. How many shaders in your dump have view_addresses set, by stage (compute, vertex, pixel)? Your change opens all of them on the native host, not only the seven compute ones. A shader that reads a field this table gives as 0 (format, tiling, base address) would now draw something wrong instead of being refused. Please say how the translator refuses those reads, and point to the test.
  2. #264 adds the same write_at and edits mirror in the same lines, and it conflicts with main today. I will take #264 first. Please rebase on it when it is in, and check the order of the arguments in the argument buffer when a shader needs both tables.

I have not run this in the game. It needs a run with a world frame before I merge it.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7217607278 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/311#issuecomment-7217607278). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T11:54:11Z. --- Thank you. The test with real values (the word read at a view's address) is what I wanted. Two things before I merge it. 1. How many shaders in your dump have `view_addresses` set, by stage (compute, vertex, pixel)? Your change opens all of them on the native host, not only the seven compute ones. A shader that reads a field this table gives as 0 (format, tiling, base address) would now draw something wrong instead of being refused. Please say how the translator refuses those reads, and point to the test. 2. #264 adds the same `write_at` and edits `mirror` in the same lines, and it conflicts with main today. I will take #264 first. Please rebase on it when it is in, and check the order of the arguments in the argument buffer when a shader needs both tables. I have not run this in the game. It needs a run with a world frame before I merge it.
Olima34 commented 2026-11-27 13:06:01 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T13:06:01Z.


Thanks. Both answers below were measured offline on a boot set of every shader the prewarm
translated: 4,921 containers, main 465db23b + #304.

1. Shaders with view_addresses, by stage. 44 translated programs read the table: 42 compute,
2 pixel, 0 vertex. On main alone it is 35 compute and 2 pixel; the 7 named above need #304 to
translate. So this PR opens 44 programs, not 7. What they read:

  • 40 read a buffer's dword 1. 23 mask it to its low 16 bits with s_and_b32, 12 pack those bits with
    s_pack_ll_b32_b16, and 5 shift the address pair right with s_lshr_b64 and then mask the result.
  • None reads an image's dwords through the table.
  • 4 read only dword 0, or nothing the program uses.

2. Fields the table gives as 0. Before this push, the translator didn't refuse them. Inside the
dwords it fetched from t1 (0–1 of a buffer, 0–3 of an image loaded whole), every field read as its
table value. That includes the 0s: a buffer's stride and swizzle, and an image's base address,
format and tiling. Only dwords outside those (a buffer's 2–3, an image's 4–7) were refused.

3fa85bb4 now refuses a program that can see any bit of a zero field.

  • Refused fields:
    • a buffer's dword 1 bits 16–31 and dwords 2–3;
    • an image's dword 0, dword 1 bits 0–29 (base address, minimum level, format), dword 2 bits 30–31,
      dword 3 bits 20–27 (tiling) and dwords 4–7.
  • Message: "a view address field the table gives as 0: " followed by the field.
  • Where: Lower::view_addresses and Lower::seen_bits.
  • How the visible bits are found: they're followed through the readers.
    • s_and_b32 with a constant keeps that constant's bits.
    • s_pack_ll_b32_b16 keeps the low half.
    • s_or_b32 with a constant, and s_lshr_b64 by a constant (with this dword as the high half),
      are followed into their results, four readers deep.
    • Any other reader, or a phi, sees every bit.
  • Test: gfx_refusal_tests::view_address_fields_the_table_gives_as_0_are_refused. It runs one
    refusal per field class. It also checks accepted reads of a buffer's address (dword 0; dword 1
    masked to 16 bits; the pair shifted by s_lshr_b64 and masked) and of an image's size fields.
    Without the check, a read of a buffer's whole dword 1 translates.
  • Re-sweep of the 44:
    • all 44 still translate;
    • 40 have the same output;
    • 3 drop the load of a dword nothing lowered reads;
    • 1 changes only the wording of a refusal in its thread-count variant.
    • 11 programs that were already refused now name the field: 10 a buffer's stride or swizzle, 1 its
      record count or format.

descriptor_data, the path for a descriptor that's also used as a resource, doesn't use t1. It
refuses by dword (gfx_refusal_tests::descriptor_dwords_read_as_data_say_which).

Two limits remain, and they're marked in the code. The kind of view comes from the load (eight dwords
at a whole descriptor means an image). The swizzle reads as the identity.

Rebased on main with #264, and the argument order. The PR is now rebased on main, which has
#264 and #304. There is one Pages::write_at (#264's), and mirror updates both tables. A shader
that needs both tables gets its argument buffer in this order:

  1. the user-data block;
  2. the root views, the stride table among them (root SRV 255 of the root-view space);
  3. the page table (t0 of the memory space);
  4. the view addresses (t1).

Binding::layout declares that order to the converter, and translated_args, then memory_args,
then view_address_args fill it in the same order.

New Metal test: heap_tests::a_compute_shader_reads_both_the_view_strides_and_the_view_addresses.
Its compute program stores through a buffer it takes from the heap itself, which uses the stride
table, and loads the word at an SRV's title address, which uses t1 after t0. I checked that the
test catches a wrong order: swapping t0 and t1, either in the layout or in the fill, makes it
read a decoy view's word instead (and the existing view-address test fails too).

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219055799 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/311#issuecomment-7219055799). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T13:06:01Z. --- Thanks. Both answers below were measured offline on a boot set of every shader the prewarm translated: 4,921 containers, main 465db23b + #304. **1. Shaders with `view_addresses`, by stage.** 44 translated programs read the table: 42 compute, 2 pixel, 0 vertex. On main alone it is 35 compute and 2 pixel; the 7 named above need #304 to translate. So this PR opens 44 programs, not 7. What they read: - 40 read a buffer's dword 1. 23 mask it to its low 16 bits with `s_and_b32`, 12 pack those bits with `s_pack_ll_b32_b16`, and 5 shift the address pair right with `s_lshr_b64` and then mask the result. - None reads an image's dwords through the table. - 4 read only dword 0, or nothing the program uses. **2. Fields the table gives as 0.** Before this push, the translator didn't refuse them. Inside the dwords it fetched from `t1` (0–1 of a buffer, 0–3 of an image loaded whole), every field read as its table value. That includes the 0s: a buffer's stride and swizzle, and an image's base address, format and tiling. Only dwords outside those (a buffer's 2–3, an image's 4–7) were refused. 3fa85bb4 now refuses a program that can see any bit of a zero field. - **Refused fields:** - a buffer's dword 1 bits 16–31 and dwords 2–3; - an image's dword 0, dword 1 bits 0–29 (base address, minimum level, format), dword 2 bits 30–31, dword 3 bits 20–27 (tiling) and dwords 4–7. - **Message:** "a view address field the table gives as 0: " followed by the field. - **Where:** `Lower::view_addresses` and `Lower::seen_bits`. - **How the visible bits are found:** they're followed through the readers. - `s_and_b32` with a constant keeps that constant's bits. - `s_pack_ll_b32_b16` keeps the low half. - `s_or_b32` with a constant, and `s_lshr_b64` by a constant (with this dword as the high half), are followed into their results, four readers deep. - Any other reader, or a phi, sees every bit. - **Test:** `gfx_refusal_tests::view_address_fields_the_table_gives_as_0_are_refused`. It runs one refusal per field class. It also checks accepted reads of a buffer's address (dword 0; dword 1 masked to 16 bits; the pair shifted by `s_lshr_b64` and masked) and of an image's size fields. Without the check, a read of a buffer's whole dword 1 translates. - **Re-sweep of the 44:** - all 44 still translate; - 40 have the same output; - 3 drop the load of a dword nothing lowered reads; - 1 changes only the wording of a refusal in its thread-count variant. - 11 programs that were already refused now name the field: 10 a buffer's stride or swizzle, 1 its record count or format. `descriptor_data`, the path for a descriptor that's also used as a resource, doesn't use `t1`. It refuses by dword (`gfx_refusal_tests::descriptor_dwords_read_as_data_say_which`). Two limits remain, and they're marked in the code. The kind of view comes from the load (eight dwords at a whole descriptor means an image). The swizzle reads as the identity. **Rebased on main with #264, and the argument order.** The PR is now rebased on main, which has #264 and #304. There is one `Pages::write_at` (#264's), and `mirror` updates both tables. A shader that needs both tables gets its argument buffer in this order: 1. the user-data block; 2. the root views, the stride table among them (root SRV 255 of the root-view space); 3. the page table (`t0` of the memory space); 4. the view addresses (`t1`). `Binding::layout` declares that order to the converter, and `translated_args`, then `memory_args`, then `view_address_args` fill it in the same order. New Metal test: `heap_tests::a_compute_shader_reads_both_the_view_strides_and_the_view_addresses`. Its compute program stores through a buffer it takes from the heap itself, which uses the stride table, and loads the word at an SRV's title address, which uses `t1` after `t0`. I checked that the test catches a wrong order: swapping `t0` and `t1`, either in the layout or in the fill, makes it read a decoy view's word instead (and the existing view-address test fails too).
LakshanSivakumar commented 2026-11-27 13:36:26 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T13:36:26Z.


This is on main as of b639c175, merged from the head before your latest push, so GitHub still shows the PR open. Your commits are in the history under your name. Checked as part of a batch of nine PRs on main 60baf130: tests, Windows clippy, policy, a full-corpus comparison and one world run (240 s, no crash, no GPU errors, scene colour lit).

If your new head has changes beyond a rebase, please rebase it onto main so only those remain; otherwise please close this. Thank you.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219707179 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/311#issuecomment-7219707179). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T13:36:26Z. --- This is on main as of b639c175, merged from the head before your latest push, so GitHub still shows the PR open. Your commits are in the history under your name. Checked as part of a batch of nine PRs on main 60baf130: tests, Windows clippy, policy, a full-corpus comparison and one world run (240 s, no crash, no GPU errors, scene colour lit). If your new head has changes beyond a rebase, please rebase it onto main so only those remain; otherwise please close this. Thank you.
Olima34 commented 2026-11-27 13:44:09 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T13:44:09Z.


Rebased onto main b639c175. Only the two commits that came after the head you merged remain:

  • c4faf9c4: shader: refuse view-address fields the table gives as 0. Inside the dwords fetched from t1, a field the table gives as 0 read as 0 instead of being refused:

    • a buffer's stride and swizzle;
    • an image's base address, format and tiling.

    Now a program that can see any bit of such a field is refused, with the visible bits followed through the program's instructions (Lower::seen_bits). Test: gfx_refusal_tests::view_address_fields_the_table_gives_as_0_are_refused, with 10 refusals and 6 accepted reads. On our boot set, all 44 view-address programs still translate. 40 have the same output, 3 drop a load nothing reads, and 1 changes only a refusal message.

  • 00853efa: a Metal test, heap_tests::a_compute_shader_reads_both_the_view_strides_and_the_view_addresses. It checks the argument order when one shader needs both tables, and swapping them makes it fail.

Checks on this head: victory-shader 371 and victory-gpu 358 tests pass, Windows clippy is clean, and cargo xtask policy passes. The first commit closes a wrong-accept gap that main has today, so it matters more than the test.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219879244 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/311#issuecomment-7219879244). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T13:44:09Z. --- Rebased onto main b639c175. Only the two commits that came after the head you merged remain: - **c4faf9c4: shader: refuse view-address fields the table gives as 0.** Inside the dwords fetched from `t1`, a field the table gives as 0 read as 0 instead of being refused: - a buffer's stride and swizzle; - an image's base address, format and tiling. Now a program that can see any bit of such a field is refused, with the visible bits followed through the program's instructions (`Lower::seen_bits`). Test: `gfx_refusal_tests::view_address_fields_the_table_gives_as_0_are_refused`, with 10 refusals and 6 accepted reads. On our boot set, all 44 view-address programs still translate. 40 have the same output, 3 drop a load nothing reads, and 1 changes only a refusal message. - **00853efa:** a Metal test, `heap_tests::a_compute_shader_reads_both_the_view_strides_and_the_view_addresses`. It checks the argument order when one shader needs both tables, and swapping them makes it fail. Checks on this head: victory-shader 371 and victory-gpu 358 tests pass, Windows clippy is clean, and `cargo xtask policy` passes. The first commit closes a wrong-accept gap that main has today, so it matters more than the test.
LakshanSivakumar commented 2026-11-27 16:05:20 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:05:20Z.


Merged in be7dc1b5. Ran the tests, Windows clippy, the policy check and a release build, and swept my full corpus (21,600 shaders): nothing lost; 690 refusals now name the zero field instead of "a descriptor read only as data", and 33 outputs change (5 compute, 28 pixel), which matches your note about loads nothing reads being dropped (I did not diff each one). Not run in the game yet. Thanks.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7222841650 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/311#issuecomment-7222841650). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T16:05:20Z. --- Merged in be7dc1b5. Ran the tests, Windows clippy, the policy check and a release build, and swept my full corpus (21,600 shaders): nothing lost; 690 refusals now name the zero field instead of "a descriptor read only as data", and 33 outputs change (5 compute, 28 pixel), which matches your note about loads nothing reads being dropped (I did not diff each one). Not run in the game yet. Thanks.
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
LakshanSivakumar/Native-Victory-macOS!311
No description provided.