Merge PR #182: descriptor: move list views' unaligned twins instead of nulling them (tree flicker on AMD)

This commit is contained in:
DoubleGhosty
2026-12-01 10:42:29 +01:00

View File

@ -3905,6 +3905,89 @@ fn buffer_view_ok(format: DXGI_FORMAT, first: u64, count: u32, stride: u32, raw:
/// these that could not be moved there (null).
pub(crate) static TWINS: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3];
/// Whether `at` is a slot of this thread's own heaps the card does not see,
/// where a descriptor is plain memory that [`move_raw`] may read and change:
/// its staged twin slot ([`staged`]) or a slot of its list views
/// ([`ListViews`], whose twins are copied into the heap after). Before, only
/// the staged slot was taken, so every unaligned twin of a list's dynamic
/// views was left null: about 870 a frame in the world on an RX 9070 XT
/// (2026-11-28), among them the tree culling's batch lists. (Port of
/// trustno1-coder/VIctory-NVIDIA#2248.)
fn beside(at: D3D12_CPU_DESCRIPTOR_HANDLE) -> bool {
staged(false, 1).is_some_and(|s| s.ptr == at.ptr)
|| LIST_VIEW_SLOTS.with(|range| {
let (start, end) = range.get();
(start..end).contains(&at.ptr)
})
}
/// [`beside`], but a list view's twin is moved only by whole dwords
/// (`VICTORY_LIST_TWINS_ODD=1`: any move, as #2248 has it). A raw view whose
/// address is off its dwords is one the card's buffer loads may fault on; the
/// staged slot keeps every move, as before #2248. (Three device hangs on an
/// RX 9070 XT after the port, each while textures were made ready.)
///
/// `VICTORY_LIST_TWINS`: which list views' twins are moved: `all` (the
/// default), `uav` (unordered access views only), `srv` (shader resource
/// views only) or `none` (as before #2248), to find which ones the culling
/// needs and which ones break characters' skin on AMD.
fn beside_for(at: D3D12_CPU_DESCRIPTOR_HANDLE, by: u32, uav: bool) -> bool {
static ODD: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
static WHICH: std::sync::OnceLock<(bool, bool)> = std::sync::OnceLock::new();
let odd = *ODD.get_or_init(|| std::env::var_os("VICTORY_LIST_TWINS_ODD").is_some_and(|v| v == "1"));
let (srvs, uavs) = *WHICH.get_or_init(|| {
let which = std::env::var("VICTORY_LIST_TWINS").unwrap_or_default().to_ascii_lowercase();
let which = match which.trim() {
"uav" => (false, true),
"srv" => (true, false),
"none" | "0" => (false, false),
_ => (true, true),
};
tracing::info!(srvs = which.0, uavs = which.1, "VICTORY_LIST_TWINS: list views' unaligned twins moved");
which
});
if staged(false, 1).is_some_and(|s| s.ptr == at.ptr) {
return true;
}
(if uav { uavs } else { srvs }) && (odd || by.is_multiple_of(4)) && beside(at)
}
/// `VICTORY_LIST_TWINS_SRV`: of the shader resource views whose twins
/// [`beside_for`] moves, which kinds: any of `raw`, `structured`, `typed`,
/// `all`, and `4`/`8`/`12` for the bytes moved (default: typed views, any
/// bytes). Only list views'
/// twins; the staged slot's are moved as before. (To find which SRV twins
/// the culling needs and which break characters' skin on AMD.)
fn srv_twin_kind(structured: bool, raw: bool, by: u32) -> bool {
static WHICH: std::sync::OnceLock<([bool; 3], [bool; 3])> = std::sync::OnceLock::new();
let (kinds, bys) = *WHICH.get_or_init(|| {
let text = std::env::var("VICTORY_LIST_TWINS_SRV").unwrap_or_default().to_ascii_lowercase();
let has = |w: &str| text.split([',', ' ', ';']).any(|t| t.trim() == w);
let any_kind = ["raw", "structured", "typed"].iter().any(|k| has(k));
let any_by = ["4", "8", "12"].iter().any(|k| has(k));
// Default: typed views only. Moving them is what the tree culling
// needs; moving raw and structured ones besides turned characters'
// skin into coloured noise on an RX 9070 XT (2026-11-29). `all`: every kind.
let all = has("all");
let kinds = if any_kind {
[has("raw"), has("structured"), has("typed")]
} else {
[all, all, true]
};
let bys = [!any_by || has("4"), !any_by || has("8"), !any_by || has("12")];
tracing::info!(?kinds, ?bys, "VICTORY_LIST_TWINS_SRV: raw, structured, typed; moved by 4, 8, 12");
(kinds, bys)
});
let kind = if structured { 1 } else if raw { 0 } else { 2 };
let by_ok = match by {
4 => bys[0],
8 => bys[1],
12 => bys[2],
_ => true,
};
kinds[kind] && by_ok
}
/// Moves the raw view at `at`, a slot of our heaps the card does not see,
/// which shows `bytes` from `start` on (GPU address), `by` bytes on: for a
/// twin whose view begins between two multiples of 16, where the host makes
@ -3999,6 +4082,8 @@ fn buffer_srv(
Flags: D3D12_BUFFER_SRV_FLAG_RAW,
};
}
// What the view was before it was made a raw one ([`beside_for`]).
let was_structured = window.is_some();
let element = element_size(&desc);
// SAFETY: a buffer view's union member.
let buffer = unsafe { &mut desc.Anonymous.Buffer };
@ -4055,7 +4140,7 @@ fn buffer_srv(
.CreateShaderResourceView(&shadow.buffer, Some(&desc), twin);
shadow.buffer.GetGPUVirtualAddress() + first * 4
};
if by > 0 && !(staged(false, 1).is_some_and(|s| s.ptr == twin.ptr) && move_raw(twin, start, count.saturating_mul(4), by)) {
if by > 0 && !(beside_for(twin, by, false) && (staged(false, 1).is_some_and(|s| s.ptr == twin.ptr) || srv_twin_kind(was_structured, raw, by)) && move_raw(twin, start, count.saturating_mul(4), by)) {
TWINS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
null_buffer_view(twin);
}
@ -4179,7 +4264,7 @@ fn buffer_uav(
.CreateUnorderedAccessView(&shadow.buffer, None::<&ID3D12Resource>, Some(&desc), twin);
shadow.buffer.GetGPUVirtualAddress() + first * 4
};
if by > 0 && !(staged(false, 1).is_some_and(|s| s.ptr == twin.ptr) && move_raw(twin, start, count.saturating_mul(4), by)) {
if by > 0 && !(beside_for(twin, by, true) && move_raw(twin, start, count.saturating_mul(4), by)) {
TWINS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
null_buffer_view(twin);
}
@ -6314,6 +6399,7 @@ impl ListViews {
.ok()?;
let step = host.device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize;
let start = heap.GetCPUDescriptorHandleForHeapStart().ptr;
LIST_VIEW_SLOTS.with(|range| range.set((start, start + Self::SLOTS as usize * step)));
Some(ListViews { recording: 0, heap, step, start, made: Default::default() })
}
}
@ -6326,6 +6412,9 @@ impl ListViews {
thread_local! {
static LIST_VIEWS: std::cell::RefCell<Option<ListViews>> = const { std::cell::RefCell::new(None) };
/// The CPU addresses of this thread's [`ListViews`] slots ([`beside`]),
/// kept apart so they can be asked while `LIST_VIEWS` is borrowed.
static LIST_VIEW_SLOTS: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) };
}
/// The view the record `words` names, as this thread made it for `recording`.