Repository navigation
d3d12: guard biased raw twins against sub-dword offsets and 32-bit wrap (D3D12-TWIN-BIAS-RDNA3) - #2757
Conversation
…it wrap Require dword alignment in settle_twin so sub-dword typed buffer views (such as odd R16 or unaligned R8 elements) do not hand misaligned byte offsets to 4-byte-aligned rawBufferLoad, rawBufferStore, atomic, or cbuffer accesses when LEONIDA_TWIN_BIAS is enabled. Clamp biased shader byte offsets past 0xFFFF_FFE0 to out-of-bounds 0xFFFF_FFF0 instead of wrapping 32-bit sentinel indices (u32::MAX * stride + field, x - 4 at x = 0) modulo 2^32 into the 16-byte prefix before the view, where loads read adjacent suballocation bytes and UAV writes corrupted them. Clear stale twin bias bits in HostHeap::constant_raw_at when recycling dynamic slots, publish stride bias words before twin descriptors in mirror and make_listed, and bound twin_window byte spans to u32. Fixes ProjectLeonida#2702 AI-assisted.
|
Verified by OreoFR on macOS 27.2.1, Apple M5, Rust (rustup),
AI-assisted: Claude Code |
|
Independent check on Windows (not the author), at
Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), Rust 1.98.1 pinned. No regressions in either crate. I did not run the title, so the biased-twin picture behaviour itself needs a game run. AI-assisted: yes (opencode). |
|
RUN · VERIFY-2757-RTX4060 · by MoonEyes (another person than the author) on head
AI-assisted (Claude Code). |
|
Merging. Head AI-assisted (Claude Code). |
Summary
Fixes #2702 (
D3D12-TWIN-BIAS-RDNA3).Commit
1bb188cf5(LEONIDA_TWIN_BIAS, active on AMD adapters whereraw_views()allocates raw twins) kept raw twins starting at the 16-byte boundary before an unaligned buffer view and recorded the byte deltaby(1..15) in bits24..27(VIEW_BIAS_MASK) ofstrides[slot]for translated shaders to add to raw byte offsets. On RDNA 3 (RX 7900 XT), that exposed three bugs that vanish underLEONIDA_TWIN_BIAS=0:Sub-dword
by % 4 != 0twins accepted bysettle_twin(d3d12/crates/leonida-gpu/src/descriptor/host.rs):shader.rs(VIEW_BIAS_MASKdoc) andlower.rs(rawBufferLoad/rawBufferStorewithalignment = 4, buffer atomics,cbuffer_dword,format_load, andnarrowwithin = byte & 3) assumebiasis a multiple of 4 (0, 4, 8, 12).twin_windowproducesbyin1..15for sub-dword typed views (R16_*at oddFirstElementgivesby = 2, 6, 10, 14;R8_*gives oddby), andsettle_twinonly checkedby < 16 && by < bytes, neverby.is_multiple_of(4). On GFX11/RDNA 3, misaligned byte offsets fed to 4-byte-aligned raw loads/stores/atomics truncate& !3or misalign, splicing adjacent half/snorm/index values.settle_twinnow requiresby.is_multiple_of(4), keeping the primary typed view intact while nulling sub-dword raw twins just asLEONIDA_TWIN_BIAS=0does.32-bit unsigned wrap-around on
offset + bias(d3d12/crates/leonida-shader/src/emit/lower.rs):u32::MAX * stride + field(0xFFFF_FFF4..0xFFFF_FFFCforstride = 4, 8, 12, 24, 48, ...) orx - 4atx = 0(0xFFFF_FFFC).bias == 0, these offsets remain near4 GiBout of bounds (0on D3D12 raw loads, dropped on stores/atomics). Addingbiasin{4, 8, 12}wrapped modulo2^32into[0, 12)— in bounds of the biased twin's 16-byte pre-view prefix, which holds bytes belonging to the preceding suballocation in the ring/heap. Vertex/geometry loads at sentinel-1read arbitrary preceding bytes as positions (stretching triangles across clip space and spiking rasterizer cost), while UAV stores/atomics at sentinel offsets overwrote preceding buffer allocations.Lower::add_biasnow clampsoffset > 0xFFFF_FFD0to out-of-bounds0xFFFF_FFE0(oob + 16 = 0xFFFF_FFF0, non-wrapping across 16-bytedwordx4accesses and constant-folded away byopt.rson literal offsets), andLower::narrowguardsaligned == 0xFFFF_FFFCbefore readingaligned + 4.Stride-word hygiene and ordering (
d3d12/crates/leonida-gpu/src/descriptor/host.rs):HostHeap::constant_raw_at(LEONIDA_PORT_CBV) now clears staleVIEW_BIAS_MASKbits instrides[index]when recycling a dynamic slot.HostHeap::{make_at, mirror, make_listed}publishstrides[index]before exposing twin descriptors (andmake_listedrefreshesexisting.stride_wordonstale_at), andtwin_windowboundsend - fromtou32soNUM_RECORDSbyte counts cannot wrap.Verification
Tested on Windows 11 (
10.0.26200), NVIDIA GeForce RTX 3090 (driver610.88), and D3D12 WARP withLEONIDA_D3D12_DEBUG=1and Windows SDKdxil.dllvalidation:leonida-shader:emit::twin_bias_tests::sentinel_and_wrapped_offsets_stay_out_of_bounds_on_biased_twins(includingbuffer_load_dword,buffer_load_dwordx4,buffer_load_ushortatu32::MAX,buffer_store_dword, andbuffer_atomic_addagainst non-zero pre-view canary bytes) andemit::twin_bias_tests::resource_heap_structured_twins_validate_and_guard_wrapped_elements.leonida-gpu:descriptor::host::listed_raw_tests::sub_dword_views_null_twins_and_recycled_raw_cbv_clears_bias(default andLEONIDA_TWIN_BIAS=0negative control) andlisted_off_alignment_raw_twin_readback.cargo test -p leonida-shader(387passed,0failed)cargo test -p leonida-gpu --lib descriptor::andport_cbv::(all passed)cargo clippy -p leonida-shader -p leonida-gpu --tests(0warnings in touched files)python -I scripts/check_no_game_data.py(rc=0)AI-assisted: authored with Gemini 4 Carbon (High) and independently reviewed with Gemini 4 Neon (XHigh).