Skip to content

Shader: LDS bytes and halves, constant sample offsets, descriptor-data reasons - #383

Closed
xmvo wants to merge 2 commits into
Marvisak:mainfrom
xmvo:shader/more
Closed

xmvo wants to merge 2 commits into
Marvisak:mainfrom
xmvo:shader/more

Conversation

@xmvo

@xmvo xmvo commented Nov 24, 2026

Copy link
Copy Markdown

Builds on #318 (its commit is the first of the two here); merge that first, or this one instead of it.

What

LDS bytes and halves. ds_read_{u,i}{8,16}, ds_write_b{8,16} and their _d16 / _d16_hi forms now lower within their dword:

  • reads load the dword, shift by the byte offset and zero- or sign-extend; d16 forms keep the VGPR's other half,
  • writes clear then set their bits with two atomics (and, then or), so lanes writing other bytes of the same dword keep theirs.

Constant sample offsets. image_sample_o, image_sample_lz_o and image_sample_l_o pass a constant offset word as the sample's immediate offsets. A computed word, or an offset outside -8..7, stops the program with its own reason. image_sample_l(_o) now reads its level after the extra words (it assumed none).

Descriptor data. A buffer descriptor read as data and by s_buffer_load is now sized from its raw SRV, as buffer-instruction uses already were. The other paths that returned "a scalar memory read as data" now each have their own reason. The title's census stopped 573 programs with that message, and this tells the causes apart.

Adds BitcodeModule::int_value (an integer constant's value).

Census buckets addressed

Reason Programs
an LDS write of bytes or halves 9
an LDS read of bytes or halves 7
image_sample_o 4
a scalar memory read as data 573 (split into specific reasons; the s_buffer_load + raw SRV case translates)

Testing

  • lds_byte_and_half_writes_keep_the_rest_of_the_dword / lds_byte_and_half_reads_extend_as_the_console (valu_tests): validated by dxil.dll and run on the hardware device. They need a one-wave group with LDS, which takes the console's wave size, and WARP can't run that, so run_cases_on lets these choose backends.
  • New image_tests module: a synthetic program loads a UAV, a 4×4 texture and a sampler from tables and samples it. Validated, then run on hardware and WARP:
  • cargo test -p victory-shader: 76 pass (hardware: RTX 3060). cargo clippy -p victory-shader and rustfmt clean.
  • Not yet re-run against the title's census.

localshotgun added 2 commits November 23, 2026 21:17
With a16 the address VGPRs were read as one run of 16-bit pairs. The
hardware (and image_addr_dwords, which counts them) gives each extra word
(texel offsets, bias, comparison value) a whole VGPR, pads each direction
of an odd number of gradients to whole VGPRs and begins the coordinates
in a new VGPR. image_address_place now places each address that way, so
a16 biased samples translate (the bias is the f16 in its word's low half)
and a16 gathers with an offset word read their coordinates from the right
VGPR.

image_sample_d_cl lowers to sampleGrad with the clamp that follows the
coordinates.

In a census of the title's shaders these stopped 108 ("a biased image
sample with 16-bit coordinates") and 27 (image_sample_d_cl) programs.
…a reasons

- ds_read_{u,i}{8,16}, ds_write_b{8,16} and their _d16/_d16_hi forms
  read and write within their dword: reads shift and zero- or
  sign-extend (d16 keeps the VGPR's other half), writes clear then set
  their bits with two atomics so lanes writing other bytes of the same
  dword keep theirs.
- image_sample_o, image_sample_lz_o and image_sample_l_o pass a constant
  offset word as the sample's immediate offsets (each 6-bit offset
  within -8..7; a computed word or a wider offset stops the program with
  its own reason). image_sample_l(_o) reads its level after the extra
  words.
- A buffer descriptor read as data and by s_buffer_load is sized from
  its raw SRV. The other ways a scalar memory read as data stops now
  have their own reasons, to tell apart the 573 programs a census of
  the title stopped with "a scalar memory read as data".

Tests: LDS byte/half reads and writes run on the hardware device (one
wave with LDS takes the console's wave size, which WARP lacks); a new
image_tests module samples a 4x4 texture on hardware and WARP with
offsets, an a16 extra word, and gradients with a clamp.
@SpaghettiMaster545

Copy link
Copy Markdown

Tested with the title at b6af8b1. The branch also carries #318's commit 1d7bf9b, so this measures both together.

Not tested: this PR without #318, AMD GPUs, a release build, image results.

(Claude Code, Claude Opus 5.5: ran the title on both builds and compared the logs program by program.)

@jeremygold02

Copy link
Copy Markdown
Contributor

I ran the shader crate’s unit suite at head b6af8b177e46e14b0ac9e372c287a8db63c83630:

cargo test --locked -p victory-shader --lib -- --test-threads=1

Result: 76 passed, 0 failed, 0 ignored. Relevant checks include lds_byte_and_half_reads_extend_as_the_console, lds_byte_and_half_writes_keep_the_rest_of_the_dword, the image sampling offset tests, and the shader lowering/bitcode tests. These are synthetic unit checks; this run does not validate rendered image output or title behavior.

@ghost ghost mentioned this pull request Nov 24, 2026
@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

Review note on head b6af8b1: for ds_read_*_d16 / _d16_hi, crates/victory-shader/src/emit/lower.rs:2138 keeps the destination's other half by reading its old value with inst.read(Reg::V(reg)), and falls back to 0 when that is None.

On main, ir.rs effects() (the Fields::Ds arm, around line 842) adds a ds_read destination only to the written registers, and Inst::read only searches the read list. So for these reads inst.read is always None and the kept half is always zeroed: a _d16 read clears the high 16 bits, a _d16_hi read clears the low 16.

#116 fixes this in effects():

if name.starts_with("ds_read") && name.contains("_d16") {
    regs(d, &mut r)?;
}

This PR would need the same, plus a test that writes a known value to the destination first and checks the other half survives.

Read-only review, nothing built. AI-assisted (Claude Code); the claim above was checked against this head and main at d2a4bae. Context: the triage in #366.

@iurysf

iurysf commented Nov 24, 2026

Copy link
Copy Markdown

Measured offline against main d2a4bae with victory translate --list --validate over the title's shaders.rpf + executable (26,096 programs), dxil.dll 1.9.2602.17: failures 3,733 -> 3,588 (−145), with no regressions (194 rejected by the validator, against 185). Most of the change is reclassification. "a scalar memory read as data" (573) goes to 0 here, but mostly by splitting into "descriptor address/format" (71 -> 595) rather than by translating. For comparison, #136 + #180 together take failures to 3,080 and clear both categories (details on #136).

AI-assisted.

@slint6614-dev

Copy link
Copy Markdown

Measured on the full retail shader pack (26,503 containers) with victory translate --validate, against main d2a4bae (emitted / validated). Windows 11, dxil.dll from SDK 10.0.26100.

VS/PS CS MS sub-dword LDS refusals (7 reads, 9 writes on main) tests
main 12272 / 12172 1693 / 1670 8191 / 8129 16 ok
#101 same 1709 / 1686 same 0 ok
#632 same 1707 / 1684 same 0, but 2 new "an LDS read into half a register" ok
#383 12361 / 12252 1720 / 1697 8220 / 8158 0 see below

pablogonz12 added a commit to pablogonz12/VIctory that referenced this pull request Nov 24, 2026
…ffsets, descriptor data

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
#	crates/victory-shader/src/emit/mod.rs
#	crates/victory-shader/src/emit/valu_tests.rs
pablogonz12 added a commit to pablogonz12/VIctory that referenced this pull request Nov 24, 2026

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test-merge with current main (ffe310e) conflicts — needs a rebase. Main moved recently (PR #566 waterfall loops touched lower.rs/resource.rs/valu_tests.rs), so shader PRs opened before it need rebasing. I'll re-review once it merges cleanly.

@adamdotpy

Copy link
Copy Markdown

Reviewed b6af8b1. Two things first, then what I found in each part.

It no longer applies to main. The branch is based on d2a4bae; git merge-tree against ffe310e conflicts in crates/victory-shader/src/emit/lower.rs and .../valu_tests.rs, which is why the PR reads as dirty. @SpaghettiMaster545's title test merged onto d2a4bae cleanly, so the conflict came in with the commits after that; a rebase is needed before anyone can judge the merge or re-run a census.

Run here (Windows 11, Rust 1.98.1): cargo test -p victory-shader — 76 passed, 0 failed, matching @jeremygold02.

_d16 / _d16_hi kept half: the source is the wrong one, and this test cannot see it

@TrashTalker3460 is right that the kept half is unreliable, but the mechanism is worth stating precisely, because it explains why lds_byte_and_half_reads_extend_as_the_console passes on hardware:

  • ir.rs's effects() puts a ds destination only in the write list, and chains data into the read list (Fields::Ds, addr.iter().chain(data)).
  • So inst.read(Reg::V(vdst)) returns something only because the encoding's data0 field is parsed as an operand and names that register. In the test ds(op, 0, 5, 0, offset) has data0 = 0, i.e. v0, and vdst is also v0, preloaded with the LDS content — so the "kept" half happens to be the value the test expects. In a title shader whose data0 names another register (hardware ignores that field on a read), the kept half comes from that register instead.
  • The robust source is the destination register's previous SSA definition, not an operand of this instruction. If Shaders: LDS halfwords straddling two dwords, and hardware tests of byte and halfword LDS accesses #116 does that, it fixes both _d16 and _d16_hi.

A test that would catch it: same program, but vdst = 2 with the address in v5 and data0 naming a third register (or 0 while vdst != 0), so the kept half can only come from v2's own previous value.

LDS bytes and halves

The part I expected to be wrong is right. hole = mask << shift, keep = ~hole, value masked into hole: mask and value are disjoint per write, so the and then or pair commutes and lanes writing different bytes of one dword cannot lose each other's bits. Two notes:

  • A lane reading that dword between the two atomics sees the hole zeroed. The console program has the same race unlocked, but "two atomics" reads as if the pair were atomic; a sentence saying it is not would stop someone relying on it.
  • The byte-3 half shortcut is documented; the read at byte 3 (the high half coming from the next dword) is where this differs from hardware, so it is the case to name in the shortcut comment.

image_address_place

per = grads / 2 is safe only because the caller computes grads = 2 * spatial, so it is never 1. Nothing in the function says that, and the comment right above talks about odd gradient counts being padded — a debug_assert!(per > 0 || grads == 0) or a sentence would keep the next edit from dividing by zero inside the translator rather than failing a shader.

Descriptor data — three PRs, one case

Your Fields::Smem arm is the same fix as #953 (a buffer descriptor also read by s_buffer_load becomes a raw SRV, (11 | u, 0)), reached independently, and the reason split around it is what #1083 does. Whichever lands first, the other two rebase:

  • if Shaders: handle s_buffer_load SMEM uses in descriptor_data (#798) #953 goes in first, this keeps the reclassification (Fields::Flat, the other arms) and drops the Fields::Smem translation;
  • @iurysf's census says the split accounts for most of the change here anyway ("descriptor address/format" 71 → 595 rather than new translations), so the two PRs are complementary once one carries the arm.

One shared open question: both use (11 | u, 0) regardless of used.stride, so getDimensions().x on that raw view is bytes/4 while a console structured descriptor's dword 2 is bytes/stride — 4× at stride 16, and it is the record count. I raised it on #953; if it turns out to matter, it matters identically here.

@adamdotpy

Copy link
Copy Markdown

For the LDS half of this PR, #571 is the better implementation of the same ground: it handles the halfword at byte 3 by straddling into the next dword (64-bit shift on read, 64-bit mask/data on write, past-the-end clamped with reads seeing 0), where this PR documents the straddle as a shortcut that loses the spilling byte. I would land #571 for LDS and rebase this one to keep only the image_sample_o and descriptor-data work, which nothing else covers.

Both PRs share the same question about the _d16 kept half — the destination register is read via inst.read(Reg::V(dst)) while ir.rs::effects() only declares dst as written, and the tests cannot see it because vdst and the data field coincide. I left the details with an experiment on #571. One fix upstream would close both.

@pablogonz12

Copy link
Copy Markdown
Contributor

I ran the CI clippy on head b6af8b1 (the branch has no checks): cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings fails on two points, both the first error in their crate:

  • crates/victory-shader/src/emit/valu_tests.rs:757: type_complexity on the [(u8, u16, fn(u32) -> u32); 6] table in lds_byte_and_half_reads_extend_as_the_console — a type alias (or #[allow(clippy::type_complexity)]) clears it.
  • crates/victory-gpu/src/host.rs:267: undocumented_unsafe_blocks, which is not from this PR: current main has since added a // SAFETY: comment above that raw.cast::<IDXGIAdapter1>() line, so rebasing on main fixes it.
    Also, when this meets current main: image_tests.rs needs emit(..., Target::ANY) and raw_view_tests.rs needs the extra lanes argument of resource::track (I hit both in my tree).
    No game data, just the two compiler messages.

@adamdotpy

Copy link
Copy Markdown

Reproduced the flaky release-test crash on b6af8b1, and found the cause. Windows 11, Rust 1.98.1, the branch's release test binary run 6 times with default (parallel) threads:

run 1 exit=139 (segfault)     run 4 exit=139 (segfault)
runs 2, 3, 5, 6 exit=0, "test result: ok"

That is 2 of 6, same as your report, and --test-threads=1 passes every time. So it is a parallel-run race, not a broken assertion.

The cause is that this branch's base predates the GPU-test serialization. Current main's valu_tests.rs has:

/// … two GPU tests at once on WARP can end the test process with an
/// access violation. Other GPU test modules take this lock too.
pub(super) static ONE_AT_A_TIME: std::sync::Mutex<()> = …

and run_cases takes it before touching a device. Your merge base's valu_tests.rs has no ONE_AT_A_TIME at all (git show <merge-base>:…valu_tests.rs | grep -c ONE_AT_A_TIME → 0), and the new run_cases_on in this branch — called from the two new LDS tests and from image_tests.rs — creates Host::new and dispatches without taking any lock. Two of those on the same device (hardware or WARP) at once is precisely the access violation main's comment names. I could not make the LDS tests alone crash in 10 parallel runs, so the full suite's contention is what tips it over; the missing lock is still the defect.

The fix is one line after the rebase: have run_cases_on take ONE_AT_A_TIME like run_cases does on current main (it will be in scope after rebasing, since it is pub(super) in the same module). Until then, --test-threads=1 is the honest way to run it, as you found.

On @pablogonz12's two clippy points: the type_complexity on the [(u8, u16, fn(u32) -> u32); 6] table at valu_tests.rs:757 is yours — an alias or #[allow] clears it. The undocumented_unsafe_blocks at host.rs:267 is a rebase artifact, confirmed: current main has the // SAFETY: EnumWarpAdapter returns a DXGI adapter… comment on exactly that raw.cast::<IDXGIAdapter1>() line, so merging main in clears it. Your other two notes (the Target::ANY and lanes signature changes) are the same class — they come from meeting current main, not from this diff.

Nothing changed on your branch from me; this is evidence only. The shader-side substance (LDS sub-dword handling, sample offsets, descriptor-data reasons) I reviewed earlier still stands, and the #571 comparison note from before is unchanged: for the LDS half, #571 is the stronger implementation.

jlefebure added a commit to jlefebure/VIctory that referenced this pull request Nov 24, 2026
… offsets, descriptor-data reasons

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
#	crates/victory-shader/src/emit/valu_tests.rs
jlefebure added a commit to jlefebure/VIctory that referenced this pull request Nov 24, 2026
…fer accesses

Brings byte and half loads/stores on buffers, flat and global memory,
sub-word cbuffer reads, and structured UAVs for 64-bit atomics.

Resolutions against the branch:
- scalar loads through root descriptors keep Marvisak#294's built-source path;
- root view bounds (Marvisak#650) gate the sub-dword and atomic paths too
  (atomics narrow their active lanes by the bound);
- the Marvisak#383 LDS byte path uses lds_ptr and refuses 64-bit LDS atomics;
- memory_tests fill root view sizes in user data, as Marvisak#650 requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
…descriptor data

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
#	crates/victory-shader/src/emit/mod.rs
#	crates/victory-shader/src/emit/valu_tests.rs
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants